tcbpf2_kern.c 8.1 KB

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