test_tunnel_kern.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2016 VMware
  3. * Copyright (c) 2016 Facebook
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. */
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include <arpa/inet.h>
  12. #include <linux/bpf.h>
  13. #include <linux/if_ether.h>
  14. #include <linux/if_packet.h>
  15. #include <linux/ip.h>
  16. #include <linux/ipv6.h>
  17. #include <linux/types.h>
  18. #include <linux/tcp.h>
  19. #include <linux/socket.h>
  20. #include <linux/pkt_cls.h>
  21. #include <linux/erspan.h>
  22. #include "bpf_helpers.h"
  23. #include "bpf_endian.h"
  24. #define ERROR(ret) do {\
  25. char fmt[] = "ERROR line:%d ret:%d\n";\
  26. bpf_trace_printk(fmt, sizeof(fmt), __LINE__, ret); \
  27. } while (0)
  28. int _version SEC("version") = 1;
  29. struct geneve_opt {
  30. __be16 opt_class;
  31. __u8 type;
  32. __u8 length:5;
  33. __u8 r3:1;
  34. __u8 r2:1;
  35. __u8 r1:1;
  36. __u8 opt_data[8]; /* hard-coded to 8 byte */
  37. };
  38. struct vxlan_metadata {
  39. __u32 gbp;
  40. };
  41. SEC("gre_set_tunnel")
  42. int _gre_set_tunnel(struct __sk_buff *skb)
  43. {
  44. int ret;
  45. struct bpf_tunnel_key key;
  46. __builtin_memset(&key, 0x0, sizeof(key));
  47. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  48. key.tunnel_id = 2;
  49. key.tunnel_tos = 0;
  50. key.tunnel_ttl = 64;
  51. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
  52. BPF_F_ZERO_CSUM_TX | BPF_F_SEQ_NUMBER);
  53. if (ret < 0) {
  54. ERROR(ret);
  55. return TC_ACT_SHOT;
  56. }
  57. return TC_ACT_OK;
  58. }
  59. SEC("gre_get_tunnel")
  60. int _gre_get_tunnel(struct __sk_buff *skb)
  61. {
  62. int ret;
  63. struct bpf_tunnel_key key;
  64. char fmt[] = "key %d remote ip 0x%x\n";
  65. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  66. if (ret < 0) {
  67. ERROR(ret);
  68. return TC_ACT_SHOT;
  69. }
  70. bpf_trace_printk(fmt, sizeof(fmt), key.tunnel_id, key.remote_ipv4);
  71. return TC_ACT_OK;
  72. }
  73. SEC("ip6gretap_set_tunnel")
  74. int _ip6gretap_set_tunnel(struct __sk_buff *skb)
  75. {
  76. struct bpf_tunnel_key key;
  77. int ret;
  78. __builtin_memset(&key, 0x0, sizeof(key));
  79. key.remote_ipv6[3] = bpf_htonl(0x11); /* ::11 */
  80. key.tunnel_id = 2;
  81. key.tunnel_tos = 0;
  82. key.tunnel_ttl = 64;
  83. key.tunnel_label = 0xabcde;
  84. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
  85. BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
  86. BPF_F_SEQ_NUMBER);
  87. if (ret < 0) {
  88. ERROR(ret);
  89. return TC_ACT_SHOT;
  90. }
  91. return TC_ACT_OK;
  92. }
  93. SEC("ip6gretap_get_tunnel")
  94. int _ip6gretap_get_tunnel(struct __sk_buff *skb)
  95. {
  96. char fmt[] = "key %d remote ip6 ::%x label %x\n";
  97. struct bpf_tunnel_key key;
  98. int ret;
  99. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key),
  100. BPF_F_TUNINFO_IPV6);
  101. if (ret < 0) {
  102. ERROR(ret);
  103. return TC_ACT_SHOT;
  104. }
  105. bpf_trace_printk(fmt, sizeof(fmt),
  106. key.tunnel_id, key.remote_ipv6[3], key.tunnel_label);
  107. return TC_ACT_OK;
  108. }
  109. SEC("erspan_set_tunnel")
  110. int _erspan_set_tunnel(struct __sk_buff *skb)
  111. {
  112. struct bpf_tunnel_key key;
  113. struct erspan_metadata md;
  114. int ret;
  115. __builtin_memset(&key, 0x0, sizeof(key));
  116. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  117. key.tunnel_id = 2;
  118. key.tunnel_tos = 0;
  119. key.tunnel_ttl = 64;
  120. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
  121. BPF_F_ZERO_CSUM_TX);
  122. if (ret < 0) {
  123. ERROR(ret);
  124. return TC_ACT_SHOT;
  125. }
  126. __builtin_memset(&md, 0, sizeof(md));
  127. #ifdef ERSPAN_V1
  128. md.version = 1;
  129. md.u.index = bpf_htonl(123);
  130. #else
  131. __u8 direction = 1;
  132. __u8 hwid = 7;
  133. md.version = 2;
  134. md.u.md2.dir = direction;
  135. md.u.md2.hwid = hwid & 0xf;
  136. md.u.md2.hwid_upper = (hwid >> 4) & 0x3;
  137. #endif
  138. ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
  139. if (ret < 0) {
  140. ERROR(ret);
  141. return TC_ACT_SHOT;
  142. }
  143. return TC_ACT_OK;
  144. }
  145. SEC("erspan_get_tunnel")
  146. int _erspan_get_tunnel(struct __sk_buff *skb)
  147. {
  148. char fmt[] = "key %d remote ip 0x%x erspan version %d\n";
  149. struct bpf_tunnel_key key;
  150. struct erspan_metadata md;
  151. __u32 index;
  152. int ret;
  153. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  154. if (ret < 0) {
  155. ERROR(ret);
  156. return TC_ACT_SHOT;
  157. }
  158. ret = bpf_skb_get_tunnel_opt(skb, &md, sizeof(md));
  159. if (ret < 0) {
  160. ERROR(ret);
  161. return TC_ACT_SHOT;
  162. }
  163. bpf_trace_printk(fmt, sizeof(fmt),
  164. key.tunnel_id, key.remote_ipv4, md.version);
  165. #ifdef ERSPAN_V1
  166. char fmt2[] = "\tindex %x\n";
  167. index = bpf_ntohl(md.u.index);
  168. bpf_trace_printk(fmt2, sizeof(fmt2), index);
  169. #else
  170. char fmt2[] = "\tdirection %d hwid %x timestamp %u\n";
  171. bpf_trace_printk(fmt2, sizeof(fmt2),
  172. md.u.md2.dir,
  173. (md.u.md2.hwid_upper << 4) + md.u.md2.hwid,
  174. bpf_ntohl(md.u.md2.timestamp));
  175. #endif
  176. return TC_ACT_OK;
  177. }
  178. SEC("ip4ip6erspan_set_tunnel")
  179. int _ip4ip6erspan_set_tunnel(struct __sk_buff *skb)
  180. {
  181. struct bpf_tunnel_key key;
  182. struct erspan_metadata md;
  183. int ret;
  184. __builtin_memset(&key, 0x0, sizeof(key));
  185. key.remote_ipv6[3] = bpf_htonl(0x11);
  186. key.tunnel_id = 2;
  187. key.tunnel_tos = 0;
  188. key.tunnel_ttl = 64;
  189. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
  190. BPF_F_TUNINFO_IPV6);
  191. if (ret < 0) {
  192. ERROR(ret);
  193. return TC_ACT_SHOT;
  194. }
  195. __builtin_memset(&md, 0, sizeof(md));
  196. #ifdef ERSPAN_V1
  197. md.u.index = bpf_htonl(123);
  198. md.version = 1;
  199. #else
  200. __u8 direction = 0;
  201. __u8 hwid = 17;
  202. md.version = 2;
  203. md.u.md2.dir = direction;
  204. md.u.md2.hwid = hwid & 0xf;
  205. md.u.md2.hwid_upper = (hwid >> 4) & 0x3;
  206. #endif
  207. ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
  208. if (ret < 0) {
  209. ERROR(ret);
  210. return TC_ACT_SHOT;
  211. }
  212. return TC_ACT_OK;
  213. }
  214. SEC("ip4ip6erspan_get_tunnel")
  215. int _ip4ip6erspan_get_tunnel(struct __sk_buff *skb)
  216. {
  217. char fmt[] = "ip6erspan get key %d remote ip6 ::%x erspan version %d\n";
  218. struct bpf_tunnel_key key;
  219. struct erspan_metadata md;
  220. __u32 index;
  221. int ret;
  222. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key),
  223. BPF_F_TUNINFO_IPV6);
  224. if (ret < 0) {
  225. ERROR(ret);
  226. return TC_ACT_SHOT;
  227. }
  228. ret = bpf_skb_get_tunnel_opt(skb, &md, sizeof(md));
  229. if (ret < 0) {
  230. ERROR(ret);
  231. return TC_ACT_SHOT;
  232. }
  233. bpf_trace_printk(fmt, sizeof(fmt),
  234. key.tunnel_id, key.remote_ipv4, md.version);
  235. #ifdef ERSPAN_V1
  236. char fmt2[] = "\tindex %x\n";
  237. index = bpf_ntohl(md.u.index);
  238. bpf_trace_printk(fmt2, sizeof(fmt2), index);
  239. #else
  240. char fmt2[] = "\tdirection %d hwid %x timestamp %u\n";
  241. bpf_trace_printk(fmt2, sizeof(fmt2),
  242. md.u.md2.dir,
  243. (md.u.md2.hwid_upper << 4) + md.u.md2.hwid,
  244. bpf_ntohl(md.u.md2.timestamp));
  245. #endif
  246. return TC_ACT_OK;
  247. }
  248. SEC("vxlan_set_tunnel")
  249. int _vxlan_set_tunnel(struct __sk_buff *skb)
  250. {
  251. int ret;
  252. struct bpf_tunnel_key key;
  253. struct vxlan_metadata md;
  254. __builtin_memset(&key, 0x0, sizeof(key));
  255. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  256. key.tunnel_id = 2;
  257. key.tunnel_tos = 0;
  258. key.tunnel_ttl = 64;
  259. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
  260. BPF_F_ZERO_CSUM_TX);
  261. if (ret < 0) {
  262. ERROR(ret);
  263. return TC_ACT_SHOT;
  264. }
  265. md.gbp = 0x800FF; /* Set VXLAN Group Policy extension */
  266. ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
  267. if (ret < 0) {
  268. ERROR(ret);
  269. return TC_ACT_SHOT;
  270. }
  271. return TC_ACT_OK;
  272. }
  273. SEC("vxlan_get_tunnel")
  274. int _vxlan_get_tunnel(struct __sk_buff *skb)
  275. {
  276. int ret;
  277. struct bpf_tunnel_key key;
  278. struct vxlan_metadata md;
  279. char fmt[] = "key %d remote ip 0x%x vxlan gbp 0x%x\n";
  280. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  281. if (ret < 0) {
  282. ERROR(ret);
  283. return TC_ACT_SHOT;
  284. }
  285. ret = bpf_skb_get_tunnel_opt(skb, &md, sizeof(md));
  286. if (ret < 0) {
  287. ERROR(ret);
  288. return TC_ACT_SHOT;
  289. }
  290. bpf_trace_printk(fmt, sizeof(fmt),
  291. key.tunnel_id, key.remote_ipv4, md.gbp);
  292. return TC_ACT_OK;
  293. }
  294. SEC("ip6vxlan_set_tunnel")
  295. int _ip6vxlan_set_tunnel(struct __sk_buff *skb)
  296. {
  297. struct bpf_tunnel_key key;
  298. int ret;
  299. __builtin_memset(&key, 0x0, sizeof(key));
  300. key.remote_ipv6[3] = bpf_htonl(0x11); /* ::11 */
  301. key.tunnel_id = 22;
  302. key.tunnel_tos = 0;
  303. key.tunnel_ttl = 64;
  304. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
  305. BPF_F_TUNINFO_IPV6);
  306. if (ret < 0) {
  307. ERROR(ret);
  308. return TC_ACT_SHOT;
  309. }
  310. return TC_ACT_OK;
  311. }
  312. SEC("ip6vxlan_get_tunnel")
  313. int _ip6vxlan_get_tunnel(struct __sk_buff *skb)
  314. {
  315. char fmt[] = "key %d remote ip6 ::%x label %x\n";
  316. struct bpf_tunnel_key key;
  317. int ret;
  318. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key),
  319. BPF_F_TUNINFO_IPV6);
  320. if (ret < 0) {
  321. ERROR(ret);
  322. return TC_ACT_SHOT;
  323. }
  324. bpf_trace_printk(fmt, sizeof(fmt),
  325. key.tunnel_id, key.remote_ipv6[3], key.tunnel_label);
  326. return TC_ACT_OK;
  327. }
  328. SEC("geneve_set_tunnel")
  329. int _geneve_set_tunnel(struct __sk_buff *skb)
  330. {
  331. int ret, ret2;
  332. struct bpf_tunnel_key key;
  333. struct geneve_opt gopt;
  334. __builtin_memset(&key, 0x0, sizeof(key));
  335. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  336. key.tunnel_id = 2;
  337. key.tunnel_tos = 0;
  338. key.tunnel_ttl = 64;
  339. __builtin_memset(&gopt, 0x0, sizeof(gopt));
  340. gopt.opt_class = bpf_htons(0x102); /* Open Virtual Networking (OVN) */
  341. gopt.type = 0x08;
  342. gopt.r1 = 0;
  343. gopt.r2 = 0;
  344. gopt.r3 = 0;
  345. gopt.length = 2; /* 4-byte multiple */
  346. *(int *) &gopt.opt_data = bpf_htonl(0xdeadbeef);
  347. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
  348. BPF_F_ZERO_CSUM_TX);
  349. if (ret < 0) {
  350. ERROR(ret);
  351. return TC_ACT_SHOT;
  352. }
  353. ret = bpf_skb_set_tunnel_opt(skb, &gopt, sizeof(gopt));
  354. if (ret < 0) {
  355. ERROR(ret);
  356. return TC_ACT_SHOT;
  357. }
  358. return TC_ACT_OK;
  359. }
  360. SEC("geneve_get_tunnel")
  361. int _geneve_get_tunnel(struct __sk_buff *skb)
  362. {
  363. int ret;
  364. struct bpf_tunnel_key key;
  365. struct geneve_opt gopt;
  366. char fmt[] = "key %d remote ip 0x%x geneve class 0x%x\n";
  367. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  368. if (ret < 0) {
  369. ERROR(ret);
  370. return TC_ACT_SHOT;
  371. }
  372. ret = bpf_skb_get_tunnel_opt(skb, &gopt, sizeof(gopt));
  373. if (ret < 0) {
  374. ERROR(ret);
  375. return TC_ACT_SHOT;
  376. }
  377. bpf_trace_printk(fmt, sizeof(fmt),
  378. key.tunnel_id, key.remote_ipv4, gopt.opt_class);
  379. return TC_ACT_OK;
  380. }
  381. SEC("ip6geneve_set_tunnel")
  382. int _ip6geneve_set_tunnel(struct __sk_buff *skb)
  383. {
  384. struct bpf_tunnel_key key;
  385. struct geneve_opt gopt;
  386. int ret;
  387. __builtin_memset(&key, 0x0, sizeof(key));
  388. key.remote_ipv6[3] = bpf_htonl(0x11); /* ::11 */
  389. key.tunnel_id = 22;
  390. key.tunnel_tos = 0;
  391. key.tunnel_ttl = 64;
  392. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
  393. BPF_F_TUNINFO_IPV6);
  394. if (ret < 0) {
  395. ERROR(ret);
  396. return TC_ACT_SHOT;
  397. }
  398. __builtin_memset(&gopt, 0x0, sizeof(gopt));
  399. gopt.opt_class = bpf_htons(0x102); /* Open Virtual Networking (OVN) */
  400. gopt.type = 0x08;
  401. gopt.r1 = 0;
  402. gopt.r2 = 0;
  403. gopt.r3 = 0;
  404. gopt.length = 2; /* 4-byte multiple */
  405. *(int *) &gopt.opt_data = bpf_htonl(0xfeedbeef);
  406. ret = bpf_skb_set_tunnel_opt(skb, &gopt, sizeof(gopt));
  407. if (ret < 0) {
  408. ERROR(ret);
  409. return TC_ACT_SHOT;
  410. }
  411. return TC_ACT_OK;
  412. }
  413. SEC("ip6geneve_get_tunnel")
  414. int _ip6geneve_get_tunnel(struct __sk_buff *skb)
  415. {
  416. char fmt[] = "key %d remote ip 0x%x geneve class 0x%x\n";
  417. struct bpf_tunnel_key key;
  418. struct geneve_opt gopt;
  419. int ret;
  420. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key),
  421. BPF_F_TUNINFO_IPV6);
  422. if (ret < 0) {
  423. ERROR(ret);
  424. return TC_ACT_SHOT;
  425. }
  426. ret = bpf_skb_get_tunnel_opt(skb, &gopt, sizeof(gopt));
  427. if (ret < 0) {
  428. ERROR(ret);
  429. return TC_ACT_SHOT;
  430. }
  431. bpf_trace_printk(fmt, sizeof(fmt),
  432. key.tunnel_id, key.remote_ipv4, gopt.opt_class);
  433. return TC_ACT_OK;
  434. }
  435. SEC("ipip_set_tunnel")
  436. int _ipip_set_tunnel(struct __sk_buff *skb)
  437. {
  438. struct bpf_tunnel_key key = {};
  439. void *data = (void *)(long)skb->data;
  440. struct iphdr *iph = data;
  441. struct tcphdr *tcp = data + sizeof(*iph);
  442. void *data_end = (void *)(long)skb->data_end;
  443. int ret;
  444. /* single length check */
  445. if (data + sizeof(*iph) + sizeof(*tcp) > data_end) {
  446. ERROR(1);
  447. return TC_ACT_SHOT;
  448. }
  449. key.tunnel_ttl = 64;
  450. if (iph->protocol == IPPROTO_ICMP) {
  451. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  452. } else {
  453. if (iph->protocol != IPPROTO_TCP || iph->ihl != 5)
  454. return TC_ACT_SHOT;
  455. if (tcp->dest == bpf_htons(5200))
  456. key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
  457. else if (tcp->dest == bpf_htons(5201))
  458. key.remote_ipv4 = 0xac100165; /* 172.16.1.101 */
  459. else
  460. return TC_ACT_SHOT;
  461. }
  462. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0);
  463. if (ret < 0) {
  464. ERROR(ret);
  465. return TC_ACT_SHOT;
  466. }
  467. return TC_ACT_OK;
  468. }
  469. SEC("ipip_get_tunnel")
  470. int _ipip_get_tunnel(struct __sk_buff *skb)
  471. {
  472. int ret;
  473. struct bpf_tunnel_key key;
  474. char fmt[] = "remote ip 0x%x\n";
  475. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  476. if (ret < 0) {
  477. ERROR(ret);
  478. return TC_ACT_SHOT;
  479. }
  480. bpf_trace_printk(fmt, sizeof(fmt), key.remote_ipv4);
  481. return TC_ACT_OK;
  482. }
  483. SEC("ipip6_set_tunnel")
  484. int _ipip6_set_tunnel(struct __sk_buff *skb)
  485. {
  486. struct bpf_tunnel_key key = {};
  487. void *data = (void *)(long)skb->data;
  488. struct iphdr *iph = data;
  489. struct tcphdr *tcp = data + sizeof(*iph);
  490. void *data_end = (void *)(long)skb->data_end;
  491. int ret;
  492. /* single length check */
  493. if (data + sizeof(*iph) + sizeof(*tcp) > data_end) {
  494. ERROR(1);
  495. return TC_ACT_SHOT;
  496. }
  497. __builtin_memset(&key, 0x0, sizeof(key));
  498. key.remote_ipv6[3] = bpf_htonl(0x11); /* ::11 */
  499. key.tunnel_ttl = 64;
  500. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
  501. BPF_F_TUNINFO_IPV6);
  502. if (ret < 0) {
  503. ERROR(ret);
  504. return TC_ACT_SHOT;
  505. }
  506. return TC_ACT_OK;
  507. }
  508. SEC("ipip6_get_tunnel")
  509. int _ipip6_get_tunnel(struct __sk_buff *skb)
  510. {
  511. int ret;
  512. struct bpf_tunnel_key key;
  513. char fmt[] = "remote ip6 %x::%x\n";
  514. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key),
  515. BPF_F_TUNINFO_IPV6);
  516. if (ret < 0) {
  517. ERROR(ret);
  518. return TC_ACT_SHOT;
  519. }
  520. bpf_trace_printk(fmt, sizeof(fmt), bpf_htonl(key.remote_ipv6[0]),
  521. bpf_htonl(key.remote_ipv6[3]));
  522. return TC_ACT_OK;
  523. }
  524. SEC("ip6ip6_set_tunnel")
  525. int _ip6ip6_set_tunnel(struct __sk_buff *skb)
  526. {
  527. struct bpf_tunnel_key key = {};
  528. void *data = (void *)(long)skb->data;
  529. struct ipv6hdr *iph = data;
  530. struct tcphdr *tcp = data + sizeof(*iph);
  531. void *data_end = (void *)(long)skb->data_end;
  532. int ret;
  533. /* single length check */
  534. if (data + sizeof(*iph) + sizeof(*tcp) > data_end) {
  535. ERROR(1);
  536. return TC_ACT_SHOT;
  537. }
  538. key.remote_ipv6[0] = bpf_htonl(0x2401db00);
  539. key.tunnel_ttl = 64;
  540. if (iph->nexthdr == 58 /* NEXTHDR_ICMP */) {
  541. key.remote_ipv6[3] = bpf_htonl(1);
  542. } else {
  543. if (iph->nexthdr != 6 /* NEXTHDR_TCP */) {
  544. ERROR(iph->nexthdr);
  545. return TC_ACT_SHOT;
  546. }
  547. if (tcp->dest == bpf_htons(5200)) {
  548. key.remote_ipv6[3] = bpf_htonl(1);
  549. } else if (tcp->dest == bpf_htons(5201)) {
  550. key.remote_ipv6[3] = bpf_htonl(2);
  551. } else {
  552. ERROR(tcp->dest);
  553. return TC_ACT_SHOT;
  554. }
  555. }
  556. ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key),
  557. BPF_F_TUNINFO_IPV6);
  558. if (ret < 0) {
  559. ERROR(ret);
  560. return TC_ACT_SHOT;
  561. }
  562. return TC_ACT_OK;
  563. }
  564. SEC("ip6ip6_get_tunnel")
  565. int _ip6ip6_get_tunnel(struct __sk_buff *skb)
  566. {
  567. int ret;
  568. struct bpf_tunnel_key key;
  569. char fmt[] = "remote ip6 %x::%x\n";
  570. ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key),
  571. BPF_F_TUNINFO_IPV6);
  572. if (ret < 0) {
  573. ERROR(ret);
  574. return TC_ACT_SHOT;
  575. }
  576. bpf_trace_printk(fmt, sizeof(fmt), bpf_htonl(key.remote_ipv6[0]),
  577. bpf_htonl(key.remote_ipv6[3]));
  578. return TC_ACT_OK;
  579. }
  580. SEC("xfrm_get_state")
  581. int _xfrm_get_state(struct __sk_buff *skb)
  582. {
  583. struct bpf_xfrm_state x;
  584. char fmt[] = "reqid %d spi 0x%x remote ip 0x%x\n";
  585. int ret;
  586. ret = bpf_skb_get_xfrm_state(skb, 0, &x, sizeof(x), 0);
  587. if (ret < 0)
  588. return TC_ACT_OK;
  589. bpf_trace_printk(fmt, sizeof(fmt), x.reqid, bpf_ntohl(x.spi),
  590. bpf_ntohl(x.remote_ipv4));
  591. return TC_ACT_OK;
  592. }
  593. char _license[] SEC("license") = "GPL";