xdp_tx_iptunnel_kern.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Copyright (c) 2016 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. * This program shows how to use bpf_xdp_adjust_head() by
  8. * encapsulating the incoming packet in an IPv4/v6 header
  9. * and then XDP_TX it out.
  10. */
  11. #include <uapi/linux/bpf.h>
  12. #include <linux/in.h>
  13. #include <linux/if_ether.h>
  14. #include <linux/if_packet.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/ip.h>
  17. #include <linux/ipv6.h>
  18. #include "bpf_helpers.h"
  19. #include "xdp_tx_iptunnel_common.h"
  20. struct bpf_map_def SEC("maps") rxcnt = {
  21. .type = BPF_MAP_TYPE_PERCPU_ARRAY,
  22. .key_size = sizeof(__u32),
  23. .value_size = sizeof(__u64),
  24. .max_entries = 256,
  25. };
  26. struct bpf_map_def SEC("maps") vip2tnl = {
  27. .type = BPF_MAP_TYPE_HASH,
  28. .key_size = sizeof(struct vip),
  29. .value_size = sizeof(struct iptnl_info),
  30. .max_entries = MAX_IPTNL_ENTRIES,
  31. };
  32. static __always_inline void count_tx(u32 protocol)
  33. {
  34. u64 *rxcnt_count;
  35. rxcnt_count = bpf_map_lookup_elem(&rxcnt, &protocol);
  36. if (rxcnt_count)
  37. *rxcnt_count += 1;
  38. }
  39. static __always_inline int get_dport(void *trans_data, void *data_end,
  40. u8 protocol)
  41. {
  42. struct tcphdr *th;
  43. struct udphdr *uh;
  44. switch (protocol) {
  45. case IPPROTO_TCP:
  46. th = (struct tcphdr *)trans_data;
  47. if (th + 1 > data_end)
  48. return -1;
  49. return th->dest;
  50. case IPPROTO_UDP:
  51. uh = (struct udphdr *)trans_data;
  52. if (uh + 1 > data_end)
  53. return -1;
  54. return uh->dest;
  55. default:
  56. return 0;
  57. }
  58. }
  59. static __always_inline void set_ethhdr(struct ethhdr *new_eth,
  60. const struct ethhdr *old_eth,
  61. const struct iptnl_info *tnl,
  62. __be16 h_proto)
  63. {
  64. memcpy(new_eth->h_source, old_eth->h_dest, sizeof(new_eth->h_source));
  65. memcpy(new_eth->h_dest, tnl->dmac, sizeof(new_eth->h_dest));
  66. new_eth->h_proto = h_proto;
  67. }
  68. static __always_inline int handle_ipv4(struct xdp_md *xdp)
  69. {
  70. void *data_end = (void *)(long)xdp->data_end;
  71. void *data = (void *)(long)xdp->data;
  72. struct iptnl_info *tnl;
  73. struct ethhdr *new_eth;
  74. struct ethhdr *old_eth;
  75. struct iphdr *iph = data + sizeof(struct ethhdr);
  76. u16 *next_iph_u16;
  77. u16 payload_len;
  78. struct vip vip = {};
  79. int dport;
  80. u32 csum = 0;
  81. int i;
  82. if (iph + 1 > data_end)
  83. return XDP_DROP;
  84. dport = get_dport(iph + 1, data_end, iph->protocol);
  85. if (dport == -1)
  86. return XDP_DROP;
  87. vip.protocol = iph->protocol;
  88. vip.family = AF_INET;
  89. vip.daddr.v4 = iph->daddr;
  90. vip.dport = dport;
  91. payload_len = ntohs(iph->tot_len);
  92. tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
  93. /* It only does v4-in-v4 */
  94. if (!tnl || tnl->family != AF_INET)
  95. return XDP_PASS;
  96. /* The vip key is found. Add an IP header and send it out */
  97. if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr)))
  98. return XDP_DROP;
  99. data = (void *)(long)xdp->data;
  100. data_end = (void *)(long)xdp->data_end;
  101. new_eth = data;
  102. iph = data + sizeof(*new_eth);
  103. old_eth = data + sizeof(*iph);
  104. if (new_eth + 1 > data_end ||
  105. old_eth + 1 > data_end ||
  106. iph + 1 > data_end)
  107. return XDP_DROP;
  108. set_ethhdr(new_eth, old_eth, tnl, htons(ETH_P_IP));
  109. iph->version = 4;
  110. iph->ihl = sizeof(*iph) >> 2;
  111. iph->frag_off = 0;
  112. iph->protocol = IPPROTO_IPIP;
  113. iph->check = 0;
  114. iph->tos = 0;
  115. iph->tot_len = htons(payload_len + sizeof(*iph));
  116. iph->daddr = tnl->daddr.v4;
  117. iph->saddr = tnl->saddr.v4;
  118. iph->ttl = 8;
  119. next_iph_u16 = (u16 *)iph;
  120. #pragma clang loop unroll(full)
  121. for (i = 0; i < sizeof(*iph) >> 1; i++)
  122. csum += *next_iph_u16++;
  123. iph->check = ~((csum & 0xffff) + (csum >> 16));
  124. count_tx(vip.protocol);
  125. return XDP_TX;
  126. }
  127. static __always_inline int handle_ipv6(struct xdp_md *xdp)
  128. {
  129. void *data_end = (void *)(long)xdp->data_end;
  130. void *data = (void *)(long)xdp->data;
  131. struct iptnl_info *tnl;
  132. struct ethhdr *new_eth;
  133. struct ethhdr *old_eth;
  134. struct ipv6hdr *ip6h = data + sizeof(struct ethhdr);
  135. __u16 payload_len;
  136. struct vip vip = {};
  137. int dport;
  138. if (ip6h + 1 > data_end)
  139. return XDP_DROP;
  140. dport = get_dport(ip6h + 1, data_end, ip6h->nexthdr);
  141. if (dport == -1)
  142. return XDP_DROP;
  143. vip.protocol = ip6h->nexthdr;
  144. vip.family = AF_INET6;
  145. memcpy(vip.daddr.v6, ip6h->daddr.s6_addr32, sizeof(vip.daddr));
  146. vip.dport = dport;
  147. payload_len = ip6h->payload_len;
  148. tnl = bpf_map_lookup_elem(&vip2tnl, &vip);
  149. /* It only does v6-in-v6 */
  150. if (!tnl || tnl->family != AF_INET6)
  151. return XDP_PASS;
  152. /* The vip key is found. Add an IP header and send it out */
  153. if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr)))
  154. return XDP_DROP;
  155. data = (void *)(long)xdp->data;
  156. data_end = (void *)(long)xdp->data_end;
  157. new_eth = data;
  158. ip6h = data + sizeof(*new_eth);
  159. old_eth = data + sizeof(*ip6h);
  160. if (new_eth + 1 > data_end ||
  161. old_eth + 1 > data_end ||
  162. ip6h + 1 > data_end)
  163. return XDP_DROP;
  164. set_ethhdr(new_eth, old_eth, tnl, 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 = htons(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 == htons(ETH_P_IP))
  187. return handle_ipv4(xdp);
  188. else if (h_proto == htons(ETH_P_IPV6))
  189. return handle_ipv6(xdp);
  190. else
  191. return XDP_PASS;
  192. }
  193. char _license[] SEC("license") = "GPL";