test_xdp.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Copyright (c) 2016,2017 Facebook
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <stddef.h>
  8. #include <string.h>
  9. #include <linux/bpf.h>
  10. #include <linux/if_ether.h>
  11. #include <linux/if_packet.h>
  12. #include <linux/ip.h>
  13. #include <linux/ipv6.h>
  14. #include <linux/in.h>
  15. #include <linux/udp.h>
  16. #include <linux/tcp.h>
  17. #include <linux/pkt_cls.h>
  18. #include <sys/socket.h>
  19. #include "bpf_helpers.h"
  20. #include "bpf_endian.h"
  21. #include "test_iptunnel_common.h"
  22. int _version SEC("version") = 1;
  23. struct bpf_map_def SEC("maps") rxcnt = {
  24. .type = BPF_MAP_TYPE_PERCPU_ARRAY,
  25. .key_size = sizeof(__u32),
  26. .value_size = sizeof(__u64),
  27. .max_entries = 256,
  28. };
  29. struct bpf_map_def SEC("maps") vip2tnl = {
  30. .type = BPF_MAP_TYPE_HASH,
  31. .key_size = sizeof(struct vip),
  32. .value_size = sizeof(struct iptnl_info),
  33. .max_entries = MAX_IPTNL_ENTRIES,
  34. };
  35. static __always_inline void count_tx(__u32 protocol)
  36. {
  37. __u64 *rxcnt_count;
  38. rxcnt_count = bpf_map_lookup_elem(&rxcnt, &protocol);
  39. if (rxcnt_count)
  40. *rxcnt_count += 1;
  41. }
  42. static __always_inline int get_dport(void *trans_data, void *data_end,
  43. __u8 protocol)
  44. {
  45. struct tcphdr *th;
  46. struct udphdr *uh;
  47. switch (protocol) {
  48. case IPPROTO_TCP:
  49. th = (struct tcphdr *)trans_data;
  50. if (th + 1 > data_end)
  51. return -1;
  52. return th->dest;
  53. case IPPROTO_UDP:
  54. uh = (struct udphdr *)trans_data;
  55. if (uh + 1 > data_end)
  56. return -1;
  57. return uh->dest;
  58. default:
  59. return 0;
  60. }
  61. }
  62. static __always_inline void set_ethhdr(struct ethhdr *new_eth,
  63. const struct ethhdr *old_eth,
  64. const struct iptnl_info *tnl,
  65. __be16 h_proto)
  66. {
  67. memcpy(new_eth->h_source, old_eth->h_dest, sizeof(new_eth->h_source));
  68. memcpy(new_eth->h_dest, tnl->dmac, sizeof(new_eth->h_dest));
  69. new_eth->h_proto = h_proto;
  70. }
  71. static __always_inline int handle_ipv4(struct xdp_md *xdp)
  72. {
  73. void *data_end = (void *)(long)xdp->data_end;
  74. void *data = (void *)(long)xdp->data;
  75. struct iptnl_info *tnl;
  76. struct ethhdr *new_eth;
  77. struct ethhdr *old_eth;
  78. struct iphdr *iph = data + sizeof(struct ethhdr);
  79. __u16 *next_iph;
  80. __u16 payload_len;
  81. struct vip vip = {};
  82. int dport;
  83. __u32 csum = 0;
  84. int i;
  85. if (iph + 1 > data_end)
  86. return XDP_DROP;
  87. dport = get_dport(iph + 1, data_end, iph->protocol);
  88. if (dport == -1)
  89. return XDP_DROP;
  90. vip.protocol = iph->protocol;
  91. vip.family = AF_INET;
  92. vip.daddr.v4 = iph->daddr;
  93. vip.dport = dport;
  94. payload_len = bpf_ntohs(iph->tot_len);
  95. tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
  96. /* It only does v4-in-v4 */
  97. if (!tnl || tnl->family != AF_INET)
  98. return XDP_PASS;
  99. if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr)))
  100. return XDP_DROP;
  101. data = (void *)(long)xdp->data;
  102. data_end = (void *)(long)xdp->data_end;
  103. new_eth = data;
  104. iph = data + sizeof(*new_eth);
  105. old_eth = data + sizeof(*iph);
  106. if (new_eth + 1 > data_end ||
  107. old_eth + 1 > data_end ||
  108. iph + 1 > data_end)
  109. return XDP_DROP;
  110. set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IP));
  111. iph->version = 4;
  112. iph->ihl = sizeof(*iph) >> 2;
  113. iph->frag_off = 0;
  114. iph->protocol = IPPROTO_IPIP;
  115. iph->check = 0;
  116. iph->tos = 0;
  117. iph->tot_len = bpf_htons(payload_len + sizeof(*iph));
  118. iph->daddr = tnl->daddr.v4;
  119. iph->saddr = tnl->saddr.v4;
  120. iph->ttl = 8;
  121. next_iph = (__u16 *)iph;
  122. #pragma clang loop unroll(full)
  123. for (i = 0; i < sizeof(*iph) >> 1; i++)
  124. csum += *next_iph++;
  125. iph->check = ~((csum & 0xffff) + (csum >> 16));
  126. count_tx(vip.protocol);
  127. return XDP_TX;
  128. }
  129. static __always_inline int handle_ipv6(struct xdp_md *xdp)
  130. {
  131. void *data_end = (void *)(long)xdp->data_end;
  132. void *data = (void *)(long)xdp->data;
  133. struct iptnl_info *tnl;
  134. struct ethhdr *new_eth;
  135. struct ethhdr *old_eth;
  136. struct ipv6hdr *ip6h = data + sizeof(struct ethhdr);
  137. __u16 payload_len;
  138. struct vip vip = {};
  139. int dport;
  140. if (ip6h + 1 > data_end)
  141. return XDP_DROP;
  142. dport = get_dport(ip6h + 1, data_end, ip6h->nexthdr);
  143. if (dport == -1)
  144. return XDP_DROP;
  145. vip.protocol = ip6h->nexthdr;
  146. vip.family = AF_INET6;
  147. memcpy(vip.daddr.v6, ip6h->daddr.s6_addr32, sizeof(vip.daddr));
  148. vip.dport = dport;
  149. payload_len = ip6h->payload_len;
  150. tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
  151. /* It only does v6-in-v6 */
  152. if (!tnl || tnl->family != AF_INET6)
  153. return XDP_PASS;
  154. if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr)))
  155. return XDP_DROP;
  156. data = (void *)(long)xdp->data;
  157. data_end = (void *)(long)xdp->data_end;
  158. new_eth = data;
  159. ip6h = data + sizeof(*new_eth);
  160. old_eth = data + sizeof(*ip6h);
  161. if (new_eth + 1 > data_end || old_eth + 1 > data_end ||
  162. ip6h + 1 > data_end)
  163. return XDP_DROP;
  164. set_ethhdr(new_eth, old_eth, tnl, bpf_htons(ETH_P_IPV6));
  165. ip6h->version = 6;
  166. ip6h->priority = 0;
  167. memset(ip6h->flow_lbl, 0, sizeof(ip6h->flow_lbl));
  168. ip6h->payload_len = bpf_htons(bpf_ntohs(payload_len) + sizeof(*ip6h));
  169. ip6h->nexthdr = IPPROTO_IPV6;
  170. ip6h->hop_limit = 8;
  171. memcpy(ip6h->saddr.s6_addr32, tnl->saddr.v6, sizeof(tnl->saddr.v6));
  172. memcpy(ip6h->daddr.s6_addr32, tnl->daddr.v6, sizeof(tnl->daddr.v6));
  173. count_tx(vip.protocol);
  174. return XDP_TX;
  175. }
  176. SEC("xdp_tx_iptunnel")
  177. int _xdp_tx_iptunnel(struct xdp_md *xdp)
  178. {
  179. void *data_end = (void *)(long)xdp->data_end;
  180. void *data = (void *)(long)xdp->data;
  181. struct ethhdr *eth = data;
  182. __u16 h_proto;
  183. if (eth + 1 > data_end)
  184. return XDP_DROP;
  185. h_proto = eth->h_proto;
  186. if (h_proto == bpf_htons(ETH_P_IP))
  187. return handle_ipv4(xdp);
  188. else if (h_proto == bpf_htons(ETH_P_IPV6))
  189. return handle_ipv6(xdp);
  190. else
  191. return XDP_DROP;
  192. }
  193. char _license[] SEC("license") = "GPL";