xdp_synproxy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
  2. /* Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
  3. #include <stdnoreturn.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <getopt.h>
  10. #include <signal.h>
  11. #include <sys/types.h>
  12. #include <bpf/bpf.h>
  13. #include <bpf/libbpf.h>
  14. #include <net/if.h>
  15. #include <linux/if_link.h>
  16. #include <linux/limits.h>
  17. static unsigned int ifindex;
  18. static __u32 attached_prog_id;
  19. static bool attached_tc;
  20. static void noreturn cleanup(int sig)
  21. {
  22. LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
  23. int prog_fd;
  24. int err;
  25. if (attached_prog_id == 0)
  26. exit(0);
  27. if (attached_tc) {
  28. LIBBPF_OPTS(bpf_tc_hook, hook,
  29. .ifindex = ifindex,
  30. .attach_point = BPF_TC_INGRESS);
  31. err = bpf_tc_hook_destroy(&hook);
  32. if (err < 0) {
  33. fprintf(stderr, "Error: bpf_tc_hook_destroy: %s\n", strerror(-err));
  34. fprintf(stderr, "Failed to destroy the TC hook\n");
  35. exit(1);
  36. }
  37. exit(0);
  38. }
  39. prog_fd = bpf_prog_get_fd_by_id(attached_prog_id);
  40. if (prog_fd < 0) {
  41. fprintf(stderr, "Error: bpf_prog_get_fd_by_id: %s\n", strerror(-prog_fd));
  42. err = bpf_xdp_attach(ifindex, -1, 0, NULL);
  43. if (err < 0) {
  44. fprintf(stderr, "Error: bpf_set_link_xdp_fd: %s\n", strerror(-err));
  45. fprintf(stderr, "Failed to detach XDP program\n");
  46. exit(1);
  47. }
  48. } else {
  49. opts.old_prog_fd = prog_fd;
  50. err = bpf_xdp_attach(ifindex, -1, XDP_FLAGS_REPLACE, &opts);
  51. close(prog_fd);
  52. if (err < 0) {
  53. fprintf(stderr, "Error: bpf_set_link_xdp_fd_opts: %s\n", strerror(-err));
  54. /* Not an error if already replaced by someone else. */
  55. if (err != -EEXIST) {
  56. fprintf(stderr, "Failed to detach XDP program\n");
  57. exit(1);
  58. }
  59. }
  60. }
  61. exit(0);
  62. }
  63. static noreturn void usage(const char *progname)
  64. {
  65. fprintf(stderr, "Usage: %s [--iface <iface>|--prog <prog_id>] [--mss4 <mss ipv4> --mss6 <mss ipv6> --wscale <wscale> --ttl <ttl>] [--ports <port1>,<port2>,...] [--single] [--tc]\n",
  66. progname);
  67. exit(1);
  68. }
  69. static unsigned long parse_arg_ul(const char *progname, const char *arg, unsigned long limit)
  70. {
  71. unsigned long res;
  72. char *endptr;
  73. errno = 0;
  74. res = strtoul(arg, &endptr, 10);
  75. if (errno != 0 || *endptr != '\0' || arg[0] == '\0' || res > limit)
  76. usage(progname);
  77. return res;
  78. }
  79. static void parse_options(int argc, char *argv[], unsigned int *ifindex, __u32 *prog_id,
  80. __u64 *tcpipopts, char **ports, bool *single, bool *tc)
  81. {
  82. static struct option long_options[] = {
  83. { "help", no_argument, NULL, 'h' },
  84. { "iface", required_argument, NULL, 'i' },
  85. { "prog", required_argument, NULL, 'x' },
  86. { "mss4", required_argument, NULL, 4 },
  87. { "mss6", required_argument, NULL, 6 },
  88. { "wscale", required_argument, NULL, 'w' },
  89. { "ttl", required_argument, NULL, 't' },
  90. { "ports", required_argument, NULL, 'p' },
  91. { "single", no_argument, NULL, 's' },
  92. { "tc", no_argument, NULL, 'c' },
  93. { NULL, 0, NULL, 0 },
  94. };
  95. unsigned long mss4, wscale, ttl;
  96. unsigned long long mss6;
  97. unsigned int tcpipopts_mask = 0;
  98. if (argc < 2)
  99. usage(argv[0]);
  100. *ifindex = 0;
  101. *prog_id = 0;
  102. *tcpipopts = 0;
  103. *ports = NULL;
  104. *single = false;
  105. *tc = false;
  106. while (true) {
  107. int opt;
  108. opt = getopt_long(argc, argv, "", long_options, NULL);
  109. if (opt == -1)
  110. break;
  111. switch (opt) {
  112. case 'h':
  113. usage(argv[0]);
  114. break;
  115. case 'i':
  116. *ifindex = if_nametoindex(optarg);
  117. if (*ifindex == 0)
  118. usage(argv[0]);
  119. break;
  120. case 'x':
  121. *prog_id = parse_arg_ul(argv[0], optarg, UINT32_MAX);
  122. if (*prog_id == 0)
  123. usage(argv[0]);
  124. break;
  125. case 4:
  126. mss4 = parse_arg_ul(argv[0], optarg, UINT16_MAX);
  127. tcpipopts_mask |= 1 << 0;
  128. break;
  129. case 6:
  130. mss6 = parse_arg_ul(argv[0], optarg, UINT16_MAX);
  131. tcpipopts_mask |= 1 << 1;
  132. break;
  133. case 'w':
  134. wscale = parse_arg_ul(argv[0], optarg, 14);
  135. tcpipopts_mask |= 1 << 2;
  136. break;
  137. case 't':
  138. ttl = parse_arg_ul(argv[0], optarg, UINT8_MAX);
  139. tcpipopts_mask |= 1 << 3;
  140. break;
  141. case 'p':
  142. *ports = optarg;
  143. break;
  144. case 's':
  145. *single = true;
  146. break;
  147. case 'c':
  148. *tc = true;
  149. break;
  150. default:
  151. usage(argv[0]);
  152. }
  153. }
  154. if (optind < argc)
  155. usage(argv[0]);
  156. if (tcpipopts_mask == 0xf) {
  157. if (mss4 == 0 || mss6 == 0 || wscale == 0 || ttl == 0)
  158. usage(argv[0]);
  159. *tcpipopts = (mss6 << 32) | (ttl << 24) | (wscale << 16) | mss4;
  160. } else if (tcpipopts_mask != 0) {
  161. usage(argv[0]);
  162. }
  163. if (*ifindex != 0 && *prog_id != 0)
  164. usage(argv[0]);
  165. if (*ifindex == 0 && *prog_id == 0)
  166. usage(argv[0]);
  167. }
  168. static int syncookie_attach(const char *argv0, unsigned int ifindex, bool tc)
  169. {
  170. struct bpf_prog_info info = {};
  171. __u32 info_len = sizeof(info);
  172. char xdp_filename[PATH_MAX];
  173. struct bpf_program *prog;
  174. struct bpf_object *obj;
  175. int prog_fd;
  176. int err;
  177. snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.bpf.o", argv0);
  178. obj = bpf_object__open_file(xdp_filename, NULL);
  179. err = libbpf_get_error(obj);
  180. if (err < 0) {
  181. fprintf(stderr, "Error: bpf_object__open_file: %s\n", strerror(-err));
  182. return err;
  183. }
  184. err = bpf_object__load(obj);
  185. if (err < 0) {
  186. fprintf(stderr, "Error: bpf_object__open_file: %s\n", strerror(-err));
  187. return err;
  188. }
  189. prog = bpf_object__find_program_by_name(obj, tc ? "syncookie_tc" : "syncookie_xdp");
  190. if (!prog) {
  191. fprintf(stderr, "Error: bpf_object__find_program_by_name: program was not found\n");
  192. return -ENOENT;
  193. }
  194. prog_fd = bpf_program__fd(prog);
  195. err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
  196. if (err < 0) {
  197. fprintf(stderr, "Error: bpf_prog_get_info_by_fd: %s\n",
  198. strerror(-err));
  199. goto out;
  200. }
  201. attached_tc = tc;
  202. attached_prog_id = info.id;
  203. signal(SIGINT, cleanup);
  204. signal(SIGTERM, cleanup);
  205. if (tc) {
  206. LIBBPF_OPTS(bpf_tc_hook, hook,
  207. .ifindex = ifindex,
  208. .attach_point = BPF_TC_INGRESS);
  209. LIBBPF_OPTS(bpf_tc_opts, opts,
  210. .handle = 1,
  211. .priority = 1,
  212. .prog_fd = prog_fd);
  213. err = bpf_tc_hook_create(&hook);
  214. if (err < 0) {
  215. fprintf(stderr, "Error: bpf_tc_hook_create: %s\n",
  216. strerror(-err));
  217. goto fail;
  218. }
  219. err = bpf_tc_attach(&hook, &opts);
  220. if (err < 0) {
  221. fprintf(stderr, "Error: bpf_tc_attach: %s\n",
  222. strerror(-err));
  223. goto fail;
  224. }
  225. } else {
  226. err = bpf_xdp_attach(ifindex, prog_fd,
  227. XDP_FLAGS_UPDATE_IF_NOEXIST, NULL);
  228. if (err < 0) {
  229. fprintf(stderr, "Error: bpf_set_link_xdp_fd: %s\n",
  230. strerror(-err));
  231. goto fail;
  232. }
  233. }
  234. err = 0;
  235. out:
  236. bpf_object__close(obj);
  237. return err;
  238. fail:
  239. signal(SIGINT, SIG_DFL);
  240. signal(SIGTERM, SIG_DFL);
  241. attached_prog_id = 0;
  242. goto out;
  243. }
  244. static int syncookie_open_bpf_maps(__u32 prog_id, int *values_map_fd, int *ports_map_fd)
  245. {
  246. struct bpf_prog_info prog_info;
  247. __u32 map_ids[8];
  248. __u32 info_len;
  249. int prog_fd;
  250. int err;
  251. int i;
  252. *values_map_fd = -1;
  253. *ports_map_fd = -1;
  254. prog_fd = bpf_prog_get_fd_by_id(prog_id);
  255. if (prog_fd < 0) {
  256. fprintf(stderr, "Error: bpf_prog_get_fd_by_id: %s\n", strerror(-prog_fd));
  257. return prog_fd;
  258. }
  259. prog_info = (struct bpf_prog_info) {
  260. .nr_map_ids = 8,
  261. .map_ids = (__u64)(unsigned long)map_ids,
  262. };
  263. info_len = sizeof(prog_info);
  264. err = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &info_len);
  265. if (err != 0) {
  266. fprintf(stderr, "Error: bpf_prog_get_info_by_fd: %s\n",
  267. strerror(-err));
  268. goto out;
  269. }
  270. if (prog_info.nr_map_ids < 2) {
  271. fprintf(stderr, "Error: Found %u BPF maps, expected at least 2\n",
  272. prog_info.nr_map_ids);
  273. err = -ENOENT;
  274. goto out;
  275. }
  276. for (i = 0; i < prog_info.nr_map_ids; i++) {
  277. struct bpf_map_info map_info = {};
  278. int map_fd;
  279. err = bpf_map_get_fd_by_id(map_ids[i]);
  280. if (err < 0) {
  281. fprintf(stderr, "Error: bpf_map_get_fd_by_id: %s\n", strerror(-err));
  282. goto err_close_map_fds;
  283. }
  284. map_fd = err;
  285. info_len = sizeof(map_info);
  286. err = bpf_map_get_info_by_fd(map_fd, &map_info, &info_len);
  287. if (err != 0) {
  288. fprintf(stderr, "Error: bpf_map_get_info_by_fd: %s\n",
  289. strerror(-err));
  290. close(map_fd);
  291. goto err_close_map_fds;
  292. }
  293. if (strcmp(map_info.name, "values") == 0) {
  294. *values_map_fd = map_fd;
  295. continue;
  296. }
  297. if (strcmp(map_info.name, "allowed_ports") == 0) {
  298. *ports_map_fd = map_fd;
  299. continue;
  300. }
  301. close(map_fd);
  302. }
  303. if (*values_map_fd != -1 && *ports_map_fd != -1) {
  304. err = 0;
  305. goto out;
  306. }
  307. err = -ENOENT;
  308. err_close_map_fds:
  309. if (*values_map_fd != -1)
  310. close(*values_map_fd);
  311. if (*ports_map_fd != -1)
  312. close(*ports_map_fd);
  313. *values_map_fd = -1;
  314. *ports_map_fd = -1;
  315. out:
  316. close(prog_fd);
  317. return err;
  318. }
  319. int main(int argc, char *argv[])
  320. {
  321. int values_map_fd, ports_map_fd;
  322. __u64 tcpipopts;
  323. bool firstiter;
  324. __u64 prevcnt;
  325. __u32 prog_id;
  326. char *ports;
  327. bool single;
  328. int err = 0;
  329. bool tc;
  330. parse_options(argc, argv, &ifindex, &prog_id, &tcpipopts, &ports,
  331. &single, &tc);
  332. if (prog_id == 0) {
  333. if (!tc) {
  334. err = bpf_xdp_query_id(ifindex, 0, &prog_id);
  335. if (err < 0) {
  336. fprintf(stderr, "Error: bpf_get_link_xdp_id: %s\n",
  337. strerror(-err));
  338. goto out;
  339. }
  340. }
  341. if (prog_id == 0) {
  342. err = syncookie_attach(argv[0], ifindex, tc);
  343. if (err < 0)
  344. goto out;
  345. prog_id = attached_prog_id;
  346. }
  347. }
  348. err = syncookie_open_bpf_maps(prog_id, &values_map_fd, &ports_map_fd);
  349. if (err < 0)
  350. goto out;
  351. if (ports) {
  352. __u16 port_last = 0;
  353. __u32 port_idx = 0;
  354. char *p = ports;
  355. fprintf(stderr, "Replacing allowed ports\n");
  356. while (p && *p != '\0') {
  357. char *token = strsep(&p, ",");
  358. __u16 port;
  359. port = parse_arg_ul(argv[0], token, UINT16_MAX);
  360. err = bpf_map_update_elem(ports_map_fd, &port_idx, &port, BPF_ANY);
  361. if (err != 0) {
  362. fprintf(stderr, "Error: bpf_map_update_elem: %s\n", strerror(-err));
  363. fprintf(stderr, "Failed to add port %u (index %u)\n",
  364. port, port_idx);
  365. goto out_close_maps;
  366. }
  367. fprintf(stderr, "Added port %u\n", port);
  368. port_idx++;
  369. }
  370. err = bpf_map_update_elem(ports_map_fd, &port_idx, &port_last, BPF_ANY);
  371. if (err != 0) {
  372. fprintf(stderr, "Error: bpf_map_update_elem: %s\n", strerror(-err));
  373. fprintf(stderr, "Failed to add the terminator value 0 (index %u)\n",
  374. port_idx);
  375. goto out_close_maps;
  376. }
  377. }
  378. if (tcpipopts) {
  379. __u32 key = 0;
  380. fprintf(stderr, "Replacing TCP/IP options\n");
  381. err = bpf_map_update_elem(values_map_fd, &key, &tcpipopts, BPF_ANY);
  382. if (err != 0) {
  383. fprintf(stderr, "Error: bpf_map_update_elem: %s\n", strerror(-err));
  384. goto out_close_maps;
  385. }
  386. }
  387. if ((ports || tcpipopts) && attached_prog_id == 0 && !single)
  388. goto out_close_maps;
  389. prevcnt = 0;
  390. firstiter = true;
  391. while (true) {
  392. __u32 key = 1;
  393. __u64 value;
  394. err = bpf_map_lookup_elem(values_map_fd, &key, &value);
  395. if (err != 0) {
  396. fprintf(stderr, "Error: bpf_map_lookup_elem: %s\n", strerror(-err));
  397. goto out_close_maps;
  398. }
  399. if (firstiter) {
  400. prevcnt = value;
  401. firstiter = false;
  402. }
  403. if (single) {
  404. printf("Total SYNACKs generated: %llu\n", value);
  405. break;
  406. }
  407. printf("SYNACKs generated: %llu (total %llu)\n", value - prevcnt, value);
  408. prevcnt = value;
  409. sleep(1);
  410. }
  411. out_close_maps:
  412. close(values_map_fd);
  413. close(ports_map_fd);
  414. out:
  415. return err == 0 ? 0 : 1;
  416. }