sockex2_kern.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <uapi/linux/bpf.h>
  2. #include "bpf_helpers.h"
  3. #include <uapi/linux/in.h>
  4. #include <uapi/linux/if.h>
  5. #include <uapi/linux/if_ether.h>
  6. #include <uapi/linux/ip.h>
  7. #include <uapi/linux/ipv6.h>
  8. #include <uapi/linux/if_tunnel.h>
  9. #define IP_MF 0x2000
  10. #define IP_OFFSET 0x1FFF
  11. struct vlan_hdr {
  12. __be16 h_vlan_TCI;
  13. __be16 h_vlan_encapsulated_proto;
  14. };
  15. struct bpf_flow_keys {
  16. __be32 src;
  17. __be32 dst;
  18. union {
  19. __be32 ports;
  20. __be16 port16[2];
  21. };
  22. __u16 thoff;
  23. __u8 ip_proto;
  24. };
  25. static inline int proto_ports_offset(__u64 proto)
  26. {
  27. switch (proto) {
  28. case IPPROTO_TCP:
  29. case IPPROTO_UDP:
  30. case IPPROTO_DCCP:
  31. case IPPROTO_ESP:
  32. case IPPROTO_SCTP:
  33. case IPPROTO_UDPLITE:
  34. return 0;
  35. case IPPROTO_AH:
  36. return 4;
  37. default:
  38. return 0;
  39. }
  40. }
  41. static inline int ip_is_fragment(struct __sk_buff *ctx, __u64 nhoff)
  42. {
  43. return load_half(ctx, nhoff + offsetof(struct iphdr, frag_off))
  44. & (IP_MF | IP_OFFSET);
  45. }
  46. static inline __u32 ipv6_addr_hash(struct __sk_buff *ctx, __u64 off)
  47. {
  48. __u64 w0 = load_word(ctx, off);
  49. __u64 w1 = load_word(ctx, off + 4);
  50. __u64 w2 = load_word(ctx, off + 8);
  51. __u64 w3 = load_word(ctx, off + 12);
  52. return (__u32)(w0 ^ w1 ^ w2 ^ w3);
  53. }
  54. static inline __u64 parse_ip(struct __sk_buff *skb, __u64 nhoff, __u64 *ip_proto,
  55. struct bpf_flow_keys *flow)
  56. {
  57. __u64 verlen;
  58. if (unlikely(ip_is_fragment(skb, nhoff)))
  59. *ip_proto = 0;
  60. else
  61. *ip_proto = load_byte(skb, nhoff + offsetof(struct iphdr, protocol));
  62. if (*ip_proto != IPPROTO_GRE) {
  63. flow->src = load_word(skb, nhoff + offsetof(struct iphdr, saddr));
  64. flow->dst = load_word(skb, nhoff + offsetof(struct iphdr, daddr));
  65. }
  66. verlen = load_byte(skb, nhoff + 0/*offsetof(struct iphdr, ihl)*/);
  67. if (likely(verlen == 0x45))
  68. nhoff += 20;
  69. else
  70. nhoff += (verlen & 0xF) << 2;
  71. return nhoff;
  72. }
  73. static inline __u64 parse_ipv6(struct __sk_buff *skb, __u64 nhoff, __u64 *ip_proto,
  74. struct bpf_flow_keys *flow)
  75. {
  76. *ip_proto = load_byte(skb,
  77. nhoff + offsetof(struct ipv6hdr, nexthdr));
  78. flow->src = ipv6_addr_hash(skb,
  79. nhoff + offsetof(struct ipv6hdr, saddr));
  80. flow->dst = ipv6_addr_hash(skb,
  81. nhoff + offsetof(struct ipv6hdr, daddr));
  82. nhoff += sizeof(struct ipv6hdr);
  83. return nhoff;
  84. }
  85. static inline bool flow_dissector(struct __sk_buff *skb, struct bpf_flow_keys *flow)
  86. {
  87. __u64 nhoff = ETH_HLEN;
  88. __u64 ip_proto;
  89. __u64 proto = load_half(skb, 12);
  90. int poff;
  91. if (proto == ETH_P_8021AD) {
  92. proto = load_half(skb, nhoff + offsetof(struct vlan_hdr,
  93. h_vlan_encapsulated_proto));
  94. nhoff += sizeof(struct vlan_hdr);
  95. }
  96. if (proto == ETH_P_8021Q) {
  97. proto = load_half(skb, nhoff + offsetof(struct vlan_hdr,
  98. h_vlan_encapsulated_proto));
  99. nhoff += sizeof(struct vlan_hdr);
  100. }
  101. if (likely(proto == ETH_P_IP))
  102. nhoff = parse_ip(skb, nhoff, &ip_proto, flow);
  103. else if (proto == ETH_P_IPV6)
  104. nhoff = parse_ipv6(skb, nhoff, &ip_proto, flow);
  105. else
  106. return false;
  107. switch (ip_proto) {
  108. case IPPROTO_GRE: {
  109. struct gre_hdr {
  110. __be16 flags;
  111. __be16 proto;
  112. };
  113. __u64 gre_flags = load_half(skb,
  114. nhoff + offsetof(struct gre_hdr, flags));
  115. __u64 gre_proto = load_half(skb,
  116. nhoff + offsetof(struct gre_hdr, proto));
  117. if (gre_flags & (GRE_VERSION|GRE_ROUTING))
  118. break;
  119. proto = gre_proto;
  120. nhoff += 4;
  121. if (gre_flags & GRE_CSUM)
  122. nhoff += 4;
  123. if (gre_flags & GRE_KEY)
  124. nhoff += 4;
  125. if (gre_flags & GRE_SEQ)
  126. nhoff += 4;
  127. if (proto == ETH_P_8021Q) {
  128. proto = load_half(skb,
  129. nhoff + offsetof(struct vlan_hdr,
  130. h_vlan_encapsulated_proto));
  131. nhoff += sizeof(struct vlan_hdr);
  132. }
  133. if (proto == ETH_P_IP)
  134. nhoff = parse_ip(skb, nhoff, &ip_proto, flow);
  135. else if (proto == ETH_P_IPV6)
  136. nhoff = parse_ipv6(skb, nhoff, &ip_proto, flow);
  137. else
  138. return false;
  139. break;
  140. }
  141. case IPPROTO_IPIP:
  142. nhoff = parse_ip(skb, nhoff, &ip_proto, flow);
  143. break;
  144. case IPPROTO_IPV6:
  145. nhoff = parse_ipv6(skb, nhoff, &ip_proto, flow);
  146. break;
  147. default:
  148. break;
  149. }
  150. flow->ip_proto = ip_proto;
  151. poff = proto_ports_offset(ip_proto);
  152. if (poff >= 0) {
  153. nhoff += poff;
  154. flow->ports = load_word(skb, nhoff);
  155. }
  156. flow->thoff = (__u16) nhoff;
  157. return true;
  158. }
  159. struct pair {
  160. long packets;
  161. long bytes;
  162. };
  163. struct bpf_map_def SEC("maps") hash_map = {
  164. .type = BPF_MAP_TYPE_HASH,
  165. .key_size = sizeof(__be32),
  166. .value_size = sizeof(struct pair),
  167. .max_entries = 1024,
  168. };
  169. SEC("socket2")
  170. int bpf_prog2(struct __sk_buff *skb)
  171. {
  172. struct bpf_flow_keys flow;
  173. struct pair *value;
  174. u32 key;
  175. if (!flow_dissector(skb, &flow))
  176. return 0;
  177. key = flow.dst;
  178. value = bpf_map_lookup_elem(&hash_map, &key);
  179. if (value) {
  180. __sync_fetch_and_add(&value->packets, 1);
  181. __sync_fetch_and_add(&value->bytes, skb->len);
  182. } else {
  183. struct pair val = {1, skb->len};
  184. bpf_map_update_elem(&hash_map, &key, &val, BPF_ANY);
  185. }
  186. return 0;
  187. }
  188. char _license[] SEC("license") = "GPL";