2
0

bpf_skb_utils.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
  4. * Version: 2022-09-21
  5. */
  6. #ifndef __BPF_SKB_UTILS_H
  7. #define __BPF_SKB_UTILS_H
  8. #include <uapi/linux/bpf.h>
  9. #include <uapi/linux/if_ether.h>
  10. #include <uapi/linux/ip.h>
  11. #include <uapi/linux/ipv6.h>
  12. #include <linux/if_vlan.h>
  13. #include <linux/ip.h>
  14. #include <net/ipv6.h>
  15. #include <bpf/bpf_helpers.h>
  16. #include <bpf/bpf_endian.h>
  17. struct skb_parser_info {
  18. struct __sk_buff *skb;
  19. __u32 offset;
  20. int proto;
  21. };
  22. static __always_inline void *__skb_data(struct __sk_buff *skb)
  23. {
  24. return (void *)(long)READ_ONCE(skb->data);
  25. }
  26. static __always_inline void *
  27. skb_ptr(struct __sk_buff *skb, __u32 offset, __u32 len)
  28. {
  29. void *ptr = __skb_data(skb) + offset;
  30. void *end = (void *)(long)(skb->data_end);
  31. if (ptr + len >= end)
  32. return NULL;
  33. return ptr;
  34. }
  35. static __always_inline void *
  36. skb_info_ptr(struct skb_parser_info *info, __u32 len)
  37. {
  38. __u32 offset = info->offset;
  39. return skb_ptr(info->skb, offset, len);
  40. }
  41. static __always_inline void
  42. skb_parse_init(struct skb_parser_info *info, struct __sk_buff *skb)
  43. {
  44. *info = (struct skb_parser_info){
  45. .skb = skb
  46. };
  47. }
  48. static __always_inline struct ethhdr *
  49. skb_parse_ethernet(struct skb_parser_info *info)
  50. {
  51. struct ethhdr *eth;
  52. int len;
  53. len = sizeof(*eth) + 2 * sizeof(struct vlan_hdr) + sizeof(struct ipv6hdr);
  54. if (len > info->skb->len)
  55. len = info->skb->len;
  56. bpf_skb_pull_data(info->skb, len);
  57. eth = skb_info_ptr(info, sizeof(*eth));
  58. if (!eth)
  59. return NULL;
  60. info->proto = eth->h_proto;
  61. info->offset += sizeof(*eth);
  62. return eth;
  63. }
  64. static __always_inline struct vlan_hdr *
  65. skb_parse_vlan(struct skb_parser_info *info)
  66. {
  67. struct vlan_hdr *vlh;
  68. if (info->proto != bpf_htons(ETH_P_8021Q) &&
  69. info->proto != bpf_htons(ETH_P_8021AD))
  70. return NULL;
  71. vlh = skb_info_ptr(info, sizeof(*vlh));
  72. if (!vlh)
  73. return NULL;
  74. info->proto = vlh->h_vlan_encapsulated_proto;
  75. info->offset += sizeof(*vlh);
  76. return vlh;
  77. }
  78. static __always_inline struct iphdr *
  79. skb_parse_ipv4(struct skb_parser_info *info, int min_l4_bytes)
  80. {
  81. struct iphdr *iph;
  82. int proto, hdr_len;
  83. __u32 pull_len;
  84. if (info->proto != bpf_htons(ETH_P_IP))
  85. return NULL;
  86. iph = skb_info_ptr(info, sizeof(*iph));
  87. if (!iph)
  88. return NULL;
  89. hdr_len = iph->ihl * 4;
  90. hdr_len = READ_ONCE(hdr_len) & 0xff;
  91. if (hdr_len < sizeof(*iph))
  92. return NULL;
  93. pull_len = info->offset + hdr_len + min_l4_bytes;
  94. if (pull_len > info->skb->len)
  95. pull_len = info->skb->len;
  96. if (bpf_skb_pull_data(info->skb, pull_len))
  97. return NULL;
  98. iph = skb_info_ptr(info, sizeof(*iph));
  99. if (!iph)
  100. return NULL;
  101. info->proto = iph->protocol;
  102. info->offset += hdr_len;
  103. return iph;
  104. }
  105. static __always_inline struct ipv6hdr *
  106. skb_parse_ipv6(struct skb_parser_info *info, int max_l4_bytes)
  107. {
  108. struct ipv6hdr *ip6h;
  109. __u32 pull_len;
  110. if (info->proto != bpf_htons(ETH_P_IPV6))
  111. return NULL;
  112. pull_len = info->offset + sizeof(*ip6h) + max_l4_bytes;
  113. if (pull_len > info->skb->len)
  114. pull_len = info->skb->len;
  115. if (bpf_skb_pull_data(info->skb, pull_len))
  116. return NULL;
  117. ip6h = skb_info_ptr(info, sizeof(*ip6h));
  118. if (!ip6h)
  119. return NULL;
  120. info->proto = READ_ONCE(ip6h->nexthdr);
  121. info->offset += sizeof(*ip6h);
  122. return ip6h;
  123. }
  124. static __always_inline struct tcphdr *
  125. skb_parse_tcp(struct skb_parser_info *info)
  126. {
  127. struct tcphdr *tcph;
  128. if (info->proto != IPPROTO_TCP)
  129. return NULL;
  130. tcph = skb_info_ptr(info, sizeof(*tcph));
  131. if (!tcph)
  132. return NULL;
  133. info->offset += tcph->doff * 4;
  134. return tcph;
  135. }
  136. #endif