tcbpf2_kern.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Copyright (c) 2016 VMware
  2. * Copyright (c) 2016 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. */
  8. #include <uapi/linux/bpf.h>
  9. #include <uapi/linux/if_ether.h>
  10. #include <uapi/linux/if_packet.h>
  11. #include <uapi/linux/ip.h>
  12. #include <uapi/linux/ipv6.h>
  13. #include <uapi/linux/in.h>
  14. #include <uapi/linux/tcp.h>
  15. #include <uapi/linux/filter.h>
  16. #include <uapi/linux/pkt_cls.h>
  17. #include <net/ipv6.h>
  18. #include "bpf_helpers.h"
  19. #define _htonl __builtin_bswap32
  20. #define ERROR(ret) do {\
  21. char fmt[] = "ERROR line:%d ret:%d\n";\
  22. bpf_trace_printk(fmt, sizeof(fmt), __LINE__, ret); \
  23. } while(0)
  24. struct geneve_opt {
  25. __be16 opt_class;
  26. u8 type;
  27. u8 length:5;
  28. u8 r3:1;
  29. u8 r2:1;
  30. u8 r1:1;
  31. u8 opt_data[8]; /* hard-coded to 8 byte */
  32. };
  33. struct vxlan_metadata {
  34. u32 gbp;
  35. };
  36. SEC("gre_set_tunnel")
  37. int _gre_set_tunnel(struct __sk_buff *skb)
  38. {
  39. int ret;
  40. struct bpf_tunnel_key key;
  41. __builtin_memset(&key, 0x0, sizeof(key));
  42. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  43. key.tunnel_id = 2;
  44. key.tunnel_tos = 0;
  45. key.tunnel_ttl = 64;
  46. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
  47. if (ret < 0) {
  48. ERROR(ret);
  49. return TC_ACT_SHOT;
  50. }
  51. return TC_ACT_OK;
  52. }
  53. SEC("gre_get_tunnel")
  54. int _gre_get_tunnel(struct __sk_buff *skb)
  55. {
  56. int ret;
  57. struct bpf_tunnel_key key;
  58. char fmt[] = "key %d remote ip 0x%x\n";
  59. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  60. if (ret < 0) {
  61. ERROR(ret);
  62. return TC_ACT_SHOT;
  63. }
  64. bpf_trace_printk(fmt, sizeof(fmt), key.tunnel_id, key.remote_ipv4);
  65. return TC_ACT_OK;
  66. }
  67. SEC("vxlan_set_tunnel")
  68. int _vxlan_set_tunnel(struct __sk_buff *skb)
  69. {
  70. int ret;
  71. struct bpf_tunnel_key key;
  72. struct vxlan_metadata md;
  73. __builtin_memset(&key, 0x0, sizeof(key));
  74. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  75. key.tunnel_id = 2;
  76. key.tunnel_tos = 0;
  77. key.tunnel_ttl = 64;
  78. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
  79. if (ret < 0) {
  80. ERROR(ret);
  81. return TC_ACT_SHOT;
  82. }
  83. md.gbp = 0x800FF; /* Set VXLAN Group Policy extension */
  84. ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
  85. if (ret < 0) {
  86. ERROR(ret);
  87. return TC_ACT_SHOT;
  88. }
  89. return TC_ACT_OK;
  90. }
  91. SEC("vxlan_get_tunnel")
  92. int _vxlan_get_tunnel(struct __sk_buff *skb)
  93. {
  94. int ret;
  95. struct bpf_tunnel_key key;
  96. struct vxlan_metadata md;
  97. char fmt[] = "key %d remote ip 0x%x vxlan gbp 0x%x\n";
  98. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  99. if (ret < 0) {
  100. ERROR(ret);
  101. return TC_ACT_SHOT;
  102. }
  103. ret = bpf_skb_get_tunnel_opt(skb, &md, sizeof(md));
  104. if (ret < 0) {
  105. ERROR(ret);
  106. return TC_ACT_SHOT;
  107. }
  108. bpf_trace_printk(fmt, sizeof(fmt),
  109. key.tunnel_id, key.remote_ipv4, md.gbp);
  110. return TC_ACT_OK;
  111. }
  112. SEC("geneve_set_tunnel")
  113. int _geneve_set_tunnel(struct __sk_buff *skb)
  114. {
  115. int ret, ret2;
  116. struct bpf_tunnel_key key;
  117. struct geneve_opt gopt;
  118. __builtin_memset(&key, 0x0, sizeof(key));
  119. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  120. key.tunnel_id = 2;
  121. key.tunnel_tos = 0;
  122. key.tunnel_ttl = 64;
  123. __builtin_memset(&gopt, 0x0, sizeof(gopt));
  124. gopt.opt_class = 0x102; /* Open Virtual Networking (OVN) */
  125. gopt.type = 0x08;
  126. gopt.r1 = 1;
  127. gopt.r2 = 0;
  128. gopt.r3 = 1;
  129. gopt.length = 2; /* 4-byte multiple */
  130. *(int *) &gopt.opt_data = 0xdeadbeef;
  131. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
  132. if (ret < 0) {
  133. ERROR(ret);
  134. return TC_ACT_SHOT;
  135. }
  136. ret = bpf_skb_set_tunnel_opt(skb, &gopt, sizeof(gopt));
  137. if (ret < 0) {
  138. ERROR(ret);
  139. return TC_ACT_SHOT;
  140. }
  141. return TC_ACT_OK;
  142. }
  143. SEC("geneve_get_tunnel")
  144. int _geneve_get_tunnel(struct __sk_buff *skb)
  145. {
  146. int ret;
  147. struct bpf_tunnel_key key;
  148. struct geneve_opt gopt;
  149. char fmt[] = "key %d remote ip 0x%x geneve class 0x%x\n";
  150. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  151. if (ret < 0) {
  152. ERROR(ret);
  153. return TC_ACT_SHOT;
  154. }
  155. ret = bpf_skb_get_tunnel_opt(skb, &gopt, sizeof(gopt));
  156. if (ret < 0) {
  157. ERROR(ret);
  158. return TC_ACT_SHOT;
  159. }
  160. bpf_trace_printk(fmt, sizeof(fmt),
  161. key.tunnel_id, key.remote_ipv4, gopt.opt_class);
  162. return TC_ACT_OK;
  163. }
  164. SEC("ipip_set_tunnel")
  165. int _ipip_set_tunnel(struct __sk_buff *skb)
  166. {
  167. struct bpf_tunnel_key key = {};
  168. void *data = (void *)(long)skb->data;
  169. struct iphdr *iph = data;
  170. struct tcphdr *tcp = data + sizeof(*iph);
  171. void *data_end = (void *)(long)skb->data_end;
  172. int ret;
  173. /* single length check */
  174. if (data + sizeof(*iph) + sizeof(*tcp) > data_end) {
  175. ERROR(1);
  176. return TC_ACT_SHOT;
  177. }
  178. key.tunnel_ttl = 64;
  179. if (iph->protocol == IPPROTO_ICMP) {
  180. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  181. } else {
  182. if (iph->protocol != IPPROTO_TCP || iph->ihl != 5)
  183. return TC_ACT_SHOT;
  184. if (tcp->dest == htons(5200))
  185. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  186. else if (tcp->dest == htons(5201))
  187. key.remote_ipv4 = 0xac100165; /* 172.16.1.101 */
  188. else
  189. return TC_ACT_SHOT;
  190. }
  191. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0);
  192. if (ret < 0) {
  193. ERROR(ret);
  194. return TC_ACT_SHOT;
  195. }
  196. return TC_ACT_OK;
  197. }
  198. SEC("ipip_get_tunnel")
  199. int _ipip_get_tunnel(struct __sk_buff *skb)
  200. {
  201. int ret;
  202. struct bpf_tunnel_key key;
  203. char fmt[] = "remote ip 0x%x\n";
  204. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  205. if (ret < 0) {
  206. ERROR(ret);
  207. return TC_ACT_SHOT;
  208. }
  209. bpf_trace_printk(fmt, sizeof(fmt), key.remote_ipv4);
  210. return TC_ACT_OK;
  211. }
  212. SEC("ipip6_set_tunnel")
  213. int _ipip6_set_tunnel(struct __sk_buff *skb)
  214. {
  215. struct bpf_tunnel_key key = {};
  216. void *data = (void *)(long)skb->data;
  217. struct iphdr *iph = data;
  218. struct tcphdr *tcp = data + sizeof(*iph);
  219. void *data_end = (void *)(long)skb->data_end;
  220. int ret;
  221. /* single length check */
  222. if (data + sizeof(*iph) + sizeof(*tcp) > data_end) {
  223. ERROR(1);
  224. return TC_ACT_SHOT;
  225. }
  226. key.remote_ipv6[0] = _htonl(0x2401db00);
  227. key.tunnel_ttl = 64;
  228. if (iph->protocol == IPPROTO_ICMP) {
  229. key.remote_ipv6[3] = _htonl(1);
  230. } else {
  231. if (iph->protocol != IPPROTO_TCP || iph->ihl != 5) {
  232. ERROR(iph->protocol);
  233. return TC_ACT_SHOT;
  234. }
  235. if (tcp->dest == htons(5200)) {
  236. key.remote_ipv6[3] = _htonl(1);
  237. } else if (tcp->dest == htons(5201)) {
  238. key.remote_ipv6[3] = _htonl(2);
  239. } else {
  240. ERROR(tcp->dest);
  241. return TC_ACT_SHOT;
  242. }
  243. }
  244. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_TUNINFO_IPV6);
  245. if (ret < 0) {
  246. ERROR(ret);
  247. return TC_ACT_SHOT;
  248. }
  249. return TC_ACT_OK;
  250. }
  251. SEC("ipip6_get_tunnel")
  252. int _ipip6_get_tunnel(struct __sk_buff *skb)
  253. {
  254. int ret;
  255. struct bpf_tunnel_key key;
  256. char fmt[] = "remote ip6 %x::%x\n";
  257. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), BPF_F_TUNINFO_IPV6);
  258. if (ret < 0) {
  259. ERROR(ret);
  260. return TC_ACT_SHOT;
  261. }
  262. bpf_trace_printk(fmt, sizeof(fmt), _htonl(key.remote_ipv6[0]),
  263. _htonl(key.remote_ipv6[3]));
  264. return TC_ACT_OK;
  265. }
  266. SEC("ip6ip6_set_tunnel")
  267. int _ip6ip6_set_tunnel(struct __sk_buff *skb)
  268. {
  269. struct bpf_tunnel_key key = {};
  270. void *data = (void *)(long)skb->data;
  271. struct ipv6hdr *iph = data;
  272. struct tcphdr *tcp = data + sizeof(*iph);
  273. void *data_end = (void *)(long)skb->data_end;
  274. int ret;
  275. /* single length check */
  276. if (data + sizeof(*iph) + sizeof(*tcp) > data_end) {
  277. ERROR(1);
  278. return TC_ACT_SHOT;
  279. }
  280. key.remote_ipv6[0] = _htonl(0x2401db00);
  281. key.tunnel_ttl = 64;
  282. if (iph->nexthdr == NEXTHDR_ICMP) {
  283. key.remote_ipv6[3] = _htonl(1);
  284. } else {
  285. if (iph->nexthdr != NEXTHDR_TCP) {
  286. ERROR(iph->nexthdr);
  287. return TC_ACT_SHOT;
  288. }
  289. if (tcp->dest == htons(5200)) {
  290. key.remote_ipv6[3] = _htonl(1);
  291. } else if (tcp->dest == htons(5201)) {
  292. key.remote_ipv6[3] = _htonl(2);
  293. } else {
  294. ERROR(tcp->dest);
  295. return TC_ACT_SHOT;
  296. }
  297. }
  298. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_TUNINFO_IPV6);
  299. if (ret < 0) {
  300. ERROR(ret);
  301. return TC_ACT_SHOT;
  302. }
  303. return TC_ACT_OK;
  304. }
  305. SEC("ip6ip6_get_tunnel")
  306. int _ip6ip6_get_tunnel(struct __sk_buff *skb)
  307. {
  308. int ret;
  309. struct bpf_tunnel_key key;
  310. char fmt[] = "remote ip6 %x::%x\n";
  311. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), BPF_F_TUNINFO_IPV6);
  312. if (ret < 0) {
  313. ERROR(ret);
  314. return TC_ACT_SHOT;
  315. }
  316. bpf_trace_printk(fmt, sizeof(fmt), _htonl(key.remote_ipv6[0]),
  317. _htonl(key.remote_ipv6[3]));
  318. return TC_ACT_OK;
  319. }
  320. char _license[] SEC("license") = "GPL";