xdp_router_ipv4_user.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /* Copyright (C) 2017 Cavium, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify it
  4. * under the terms of version 2 of the GNU General Public License
  5. * as published by the Free Software Foundation.
  6. */
  7. #include <linux/bpf.h>
  8. #include <linux/netlink.h>
  9. #include <linux/rtnetlink.h>
  10. #include <assert.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/socket.h>
  17. #include <unistd.h>
  18. #include "bpf_load.h"
  19. #include <bpf/bpf.h>
  20. #include <arpa/inet.h>
  21. #include <fcntl.h>
  22. #include <poll.h>
  23. #include <net/if.h>
  24. #include <netdb.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/syscall.h>
  27. #include "bpf_util.h"
  28. int sock, sock_arp, flags = 0;
  29. static int total_ifindex;
  30. int *ifindex_list;
  31. char buf[8192];
  32. static int get_route_table(int rtm_family);
  33. static void int_exit(int sig)
  34. {
  35. int i = 0;
  36. for (i = 0; i < total_ifindex; i++)
  37. bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
  38. exit(0);
  39. }
  40. static void close_and_exit(int sig)
  41. {
  42. int i = 0;
  43. close(sock);
  44. close(sock_arp);
  45. for (i = 0; i < total_ifindex; i++)
  46. bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
  47. exit(0);
  48. }
  49. /* Get the mac address of the interface given interface name */
  50. static __be64 getmac(char *iface)
  51. {
  52. struct ifreq ifr;
  53. __be64 mac = 0;
  54. int fd, i;
  55. fd = socket(AF_INET, SOCK_DGRAM, 0);
  56. ifr.ifr_addr.sa_family = AF_INET;
  57. strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);
  58. if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
  59. printf("ioctl failed leaving....\n");
  60. return -1;
  61. }
  62. for (i = 0; i < 6 ; i++)
  63. *((__u8 *)&mac + i) = (__u8)ifr.ifr_hwaddr.sa_data[i];
  64. close(fd);
  65. return mac;
  66. }
  67. static int recv_msg(struct sockaddr_nl sock_addr, int sock)
  68. {
  69. struct nlmsghdr *nh;
  70. int len, nll = 0;
  71. char *buf_ptr;
  72. buf_ptr = buf;
  73. while (1) {
  74. len = recv(sock, buf_ptr, sizeof(buf) - nll, 0);
  75. if (len < 0)
  76. return len;
  77. nh = (struct nlmsghdr *)buf_ptr;
  78. if (nh->nlmsg_type == NLMSG_DONE)
  79. break;
  80. buf_ptr += len;
  81. nll += len;
  82. if ((sock_addr.nl_groups & RTMGRP_NEIGH) == RTMGRP_NEIGH)
  83. break;
  84. if ((sock_addr.nl_groups & RTMGRP_IPV4_ROUTE) == RTMGRP_IPV4_ROUTE)
  85. break;
  86. }
  87. return nll;
  88. }
  89. /* Function to parse the route entry returned by netlink
  90. * Updates the route entry related map entries
  91. */
  92. static void read_route(struct nlmsghdr *nh, int nll)
  93. {
  94. char dsts[24], gws[24], ifs[16], dsts_len[24], metrics[24];
  95. struct bpf_lpm_trie_key *prefix_key;
  96. struct rtattr *rt_attr;
  97. struct rtmsg *rt_msg;
  98. int rtm_family;
  99. int rtl;
  100. int i;
  101. struct route_table {
  102. int dst_len, iface, metric;
  103. char *iface_name;
  104. __be32 dst, gw;
  105. __be64 mac;
  106. } route;
  107. struct arp_table {
  108. __be64 mac;
  109. __be32 dst;
  110. };
  111. struct direct_map {
  112. struct arp_table arp;
  113. int ifindex;
  114. __be64 mac;
  115. } direct_entry;
  116. if (nh->nlmsg_type == RTM_DELROUTE)
  117. printf("DELETING Route entry\n");
  118. else if (nh->nlmsg_type == RTM_GETROUTE)
  119. printf("READING Route entry\n");
  120. else if (nh->nlmsg_type == RTM_NEWROUTE)
  121. printf("NEW Route entry\n");
  122. else
  123. printf("%d\n", nh->nlmsg_type);
  124. memset(&route, 0, sizeof(route));
  125. printf("Destination\t\tGateway\t\tGenmask\t\tMetric\t\tIface\n");
  126. for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
  127. rt_msg = (struct rtmsg *)NLMSG_DATA(nh);
  128. rtm_family = rt_msg->rtm_family;
  129. if (rtm_family == AF_INET)
  130. if (rt_msg->rtm_table != RT_TABLE_MAIN)
  131. continue;
  132. rt_attr = (struct rtattr *)RTM_RTA(rt_msg);
  133. rtl = RTM_PAYLOAD(nh);
  134. for (; RTA_OK(rt_attr, rtl); rt_attr = RTA_NEXT(rt_attr, rtl)) {
  135. switch (rt_attr->rta_type) {
  136. case NDA_DST:
  137. sprintf(dsts, "%u",
  138. (*((__be32 *)RTA_DATA(rt_attr))));
  139. break;
  140. case RTA_GATEWAY:
  141. sprintf(gws, "%u",
  142. *((__be32 *)RTA_DATA(rt_attr)));
  143. break;
  144. case RTA_OIF:
  145. sprintf(ifs, "%u",
  146. *((int *)RTA_DATA(rt_attr)));
  147. break;
  148. case RTA_METRICS:
  149. sprintf(metrics, "%u",
  150. *((int *)RTA_DATA(rt_attr)));
  151. default:
  152. break;
  153. }
  154. }
  155. sprintf(dsts_len, "%d", rt_msg->rtm_dst_len);
  156. route.dst = atoi(dsts);
  157. route.dst_len = atoi(dsts_len);
  158. route.gw = atoi(gws);
  159. route.iface = atoi(ifs);
  160. route.metric = atoi(metrics);
  161. route.iface_name = alloca(sizeof(char *) * IFNAMSIZ);
  162. route.iface_name = if_indextoname(route.iface, route.iface_name);
  163. route.mac = getmac(route.iface_name);
  164. if (route.mac == -1) {
  165. int i = 0;
  166. for (i = 0; i < total_ifindex; i++)
  167. bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
  168. exit(0);
  169. }
  170. assert(bpf_map_update_elem(map_fd[4], &route.iface, &route.iface, 0) == 0);
  171. if (rtm_family == AF_INET) {
  172. struct trie_value {
  173. __u8 prefix[4];
  174. __be64 value;
  175. int ifindex;
  176. int metric;
  177. __be32 gw;
  178. } *prefix_value;
  179. prefix_key = alloca(sizeof(*prefix_key) + 3);
  180. prefix_value = alloca(sizeof(*prefix_value));
  181. prefix_key->prefixlen = 32;
  182. prefix_key->prefixlen = route.dst_len;
  183. direct_entry.mac = route.mac & 0xffffffffffff;
  184. direct_entry.ifindex = route.iface;
  185. direct_entry.arp.mac = 0;
  186. direct_entry.arp.dst = 0;
  187. if (route.dst_len == 32) {
  188. if (nh->nlmsg_type == RTM_DELROUTE) {
  189. assert(bpf_map_delete_elem(map_fd[3], &route.dst) == 0);
  190. } else {
  191. if (bpf_map_lookup_elem(map_fd[2], &route.dst, &direct_entry.arp.mac) == 0)
  192. direct_entry.arp.dst = route.dst;
  193. assert(bpf_map_update_elem(map_fd[3], &route.dst, &direct_entry, 0) == 0);
  194. }
  195. }
  196. for (i = 0; i < 4; i++)
  197. prefix_key->data[i] = (route.dst >> i * 8) & 0xff;
  198. printf("%3d.%d.%d.%d\t\t%3x\t\t%d\t\t%d\t\t%s\n",
  199. (int)prefix_key->data[0],
  200. (int)prefix_key->data[1],
  201. (int)prefix_key->data[2],
  202. (int)prefix_key->data[3],
  203. route.gw, route.dst_len,
  204. route.metric,
  205. route.iface_name);
  206. if (bpf_map_lookup_elem(map_fd[0], prefix_key,
  207. prefix_value) < 0) {
  208. for (i = 0; i < 4; i++)
  209. prefix_value->prefix[i] = prefix_key->data[i];
  210. prefix_value->value = route.mac & 0xffffffffffff;
  211. prefix_value->ifindex = route.iface;
  212. prefix_value->gw = route.gw;
  213. prefix_value->metric = route.metric;
  214. assert(bpf_map_update_elem(map_fd[0],
  215. prefix_key,
  216. prefix_value, 0
  217. ) == 0);
  218. } else {
  219. if (nh->nlmsg_type == RTM_DELROUTE) {
  220. printf("deleting entry\n");
  221. printf("prefix key=%d.%d.%d.%d/%d",
  222. prefix_key->data[0],
  223. prefix_key->data[1],
  224. prefix_key->data[2],
  225. prefix_key->data[3],
  226. prefix_key->prefixlen);
  227. assert(bpf_map_delete_elem(map_fd[0],
  228. prefix_key
  229. ) == 0);
  230. /* Rereading the route table to check if
  231. * there is an entry with the same
  232. * prefix but a different metric as the
  233. * deleted enty.
  234. */
  235. get_route_table(AF_INET);
  236. } else if (prefix_key->data[0] ==
  237. prefix_value->prefix[0] &&
  238. prefix_key->data[1] ==
  239. prefix_value->prefix[1] &&
  240. prefix_key->data[2] ==
  241. prefix_value->prefix[2] &&
  242. prefix_key->data[3] ==
  243. prefix_value->prefix[3] &&
  244. route.metric >= prefix_value->metric) {
  245. continue;
  246. } else {
  247. for (i = 0; i < 4; i++)
  248. prefix_value->prefix[i] =
  249. prefix_key->data[i];
  250. prefix_value->value =
  251. route.mac & 0xffffffffffff;
  252. prefix_value->ifindex = route.iface;
  253. prefix_value->gw = route.gw;
  254. prefix_value->metric = route.metric;
  255. assert(bpf_map_update_elem(
  256. map_fd[0],
  257. prefix_key,
  258. prefix_value,
  259. 0) == 0);
  260. }
  261. }
  262. }
  263. memset(&route, 0, sizeof(route));
  264. memset(dsts, 0, sizeof(dsts));
  265. memset(dsts_len, 0, sizeof(dsts_len));
  266. memset(gws, 0, sizeof(gws));
  267. memset(ifs, 0, sizeof(ifs));
  268. memset(&route, 0, sizeof(route));
  269. }
  270. }
  271. /* Function to read the existing route table when the process is launched*/
  272. static int get_route_table(int rtm_family)
  273. {
  274. struct sockaddr_nl sa;
  275. struct nlmsghdr *nh;
  276. int sock, seq = 0;
  277. struct msghdr msg;
  278. struct iovec iov;
  279. int ret = 0;
  280. int nll;
  281. struct {
  282. struct nlmsghdr nl;
  283. struct rtmsg rt;
  284. char buf[8192];
  285. } req;
  286. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  287. if (sock < 0) {
  288. printf("open netlink socket: %s\n", strerror(errno));
  289. return -1;
  290. }
  291. memset(&sa, 0, sizeof(sa));
  292. sa.nl_family = AF_NETLINK;
  293. if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  294. printf("bind to netlink: %s\n", strerror(errno));
  295. ret = -1;
  296. goto cleanup;
  297. }
  298. memset(&req, 0, sizeof(req));
  299. req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  300. req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
  301. req.nl.nlmsg_type = RTM_GETROUTE;
  302. req.rt.rtm_family = rtm_family;
  303. req.rt.rtm_table = RT_TABLE_MAIN;
  304. req.nl.nlmsg_pid = 0;
  305. req.nl.nlmsg_seq = ++seq;
  306. memset(&msg, 0, sizeof(msg));
  307. iov.iov_base = (void *)&req.nl;
  308. iov.iov_len = req.nl.nlmsg_len;
  309. msg.msg_iov = &iov;
  310. msg.msg_iovlen = 1;
  311. ret = sendmsg(sock, &msg, 0);
  312. if (ret < 0) {
  313. printf("send to netlink: %s\n", strerror(errno));
  314. ret = -1;
  315. goto cleanup;
  316. }
  317. memset(buf, 0, sizeof(buf));
  318. nll = recv_msg(sa, sock);
  319. if (nll < 0) {
  320. printf("recv from netlink: %s\n", strerror(nll));
  321. ret = -1;
  322. goto cleanup;
  323. }
  324. nh = (struct nlmsghdr *)buf;
  325. read_route(nh, nll);
  326. cleanup:
  327. close(sock);
  328. return ret;
  329. }
  330. /* Function to parse the arp entry returned by netlink
  331. * Updates the arp entry related map entries
  332. */
  333. static void read_arp(struct nlmsghdr *nh, int nll)
  334. {
  335. struct rtattr *rt_attr;
  336. char dsts[24], mac[24];
  337. struct ndmsg *rt_msg;
  338. int rtl, ndm_family;
  339. struct arp_table {
  340. __be64 mac;
  341. __be32 dst;
  342. } arp_entry;
  343. struct direct_map {
  344. struct arp_table arp;
  345. int ifindex;
  346. __be64 mac;
  347. } direct_entry;
  348. if (nh->nlmsg_type == RTM_GETNEIGH)
  349. printf("READING arp entry\n");
  350. printf("Address\tHwAddress\n");
  351. for (; NLMSG_OK(nh, nll); nh = NLMSG_NEXT(nh, nll)) {
  352. rt_msg = (struct ndmsg *)NLMSG_DATA(nh);
  353. rt_attr = (struct rtattr *)RTM_RTA(rt_msg);
  354. ndm_family = rt_msg->ndm_family;
  355. rtl = RTM_PAYLOAD(nh);
  356. for (; RTA_OK(rt_attr, rtl); rt_attr = RTA_NEXT(rt_attr, rtl)) {
  357. switch (rt_attr->rta_type) {
  358. case NDA_DST:
  359. sprintf(dsts, "%u",
  360. *((__be32 *)RTA_DATA(rt_attr)));
  361. break;
  362. case NDA_LLADDR:
  363. sprintf(mac, "%lld",
  364. *((__be64 *)RTA_DATA(rt_attr)));
  365. break;
  366. default:
  367. break;
  368. }
  369. }
  370. arp_entry.dst = atoi(dsts);
  371. arp_entry.mac = atol(mac);
  372. printf("%x\t\t%llx\n", arp_entry.dst, arp_entry.mac);
  373. if (ndm_family == AF_INET) {
  374. if (bpf_map_lookup_elem(map_fd[3], &arp_entry.dst,
  375. &direct_entry) == 0) {
  376. if (nh->nlmsg_type == RTM_DELNEIGH) {
  377. direct_entry.arp.dst = 0;
  378. direct_entry.arp.mac = 0;
  379. } else if (nh->nlmsg_type == RTM_NEWNEIGH) {
  380. direct_entry.arp.dst = arp_entry.dst;
  381. direct_entry.arp.mac = arp_entry.mac;
  382. }
  383. assert(bpf_map_update_elem(map_fd[3],
  384. &arp_entry.dst,
  385. &direct_entry, 0
  386. ) == 0);
  387. memset(&direct_entry, 0, sizeof(direct_entry));
  388. }
  389. if (nh->nlmsg_type == RTM_DELNEIGH) {
  390. assert(bpf_map_delete_elem(map_fd[2], &arp_entry.dst) == 0);
  391. } else if (nh->nlmsg_type == RTM_NEWNEIGH) {
  392. assert(bpf_map_update_elem(map_fd[2],
  393. &arp_entry.dst,
  394. &arp_entry.mac, 0
  395. ) == 0);
  396. }
  397. }
  398. memset(&arp_entry, 0, sizeof(arp_entry));
  399. memset(dsts, 0, sizeof(dsts));
  400. }
  401. }
  402. /* Function to read the existing arp table when the process is launched*/
  403. static int get_arp_table(int rtm_family)
  404. {
  405. struct sockaddr_nl sa;
  406. struct nlmsghdr *nh;
  407. int sock, seq = 0;
  408. struct msghdr msg;
  409. struct iovec iov;
  410. int ret = 0;
  411. int nll;
  412. struct {
  413. struct nlmsghdr nl;
  414. struct ndmsg rt;
  415. char buf[8192];
  416. } req;
  417. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  418. if (sock < 0) {
  419. printf("open netlink socket: %s\n", strerror(errno));
  420. return -1;
  421. }
  422. memset(&sa, 0, sizeof(sa));
  423. sa.nl_family = AF_NETLINK;
  424. if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  425. printf("bind to netlink: %s\n", strerror(errno));
  426. ret = -1;
  427. goto cleanup;
  428. }
  429. memset(&req, 0, sizeof(req));
  430. req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  431. req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
  432. req.nl.nlmsg_type = RTM_GETNEIGH;
  433. req.rt.ndm_state = NUD_REACHABLE;
  434. req.rt.ndm_family = rtm_family;
  435. req.nl.nlmsg_pid = 0;
  436. req.nl.nlmsg_seq = ++seq;
  437. memset(&msg, 0, sizeof(msg));
  438. iov.iov_base = (void *)&req.nl;
  439. iov.iov_len = req.nl.nlmsg_len;
  440. msg.msg_iov = &iov;
  441. msg.msg_iovlen = 1;
  442. ret = sendmsg(sock, &msg, 0);
  443. if (ret < 0) {
  444. printf("send to netlink: %s\n", strerror(errno));
  445. ret = -1;
  446. goto cleanup;
  447. }
  448. memset(buf, 0, sizeof(buf));
  449. nll = recv_msg(sa, sock);
  450. if (nll < 0) {
  451. printf("recv from netlink: %s\n", strerror(nll));
  452. ret = -1;
  453. goto cleanup;
  454. }
  455. nh = (struct nlmsghdr *)buf;
  456. read_arp(nh, nll);
  457. cleanup:
  458. close(sock);
  459. return ret;
  460. }
  461. /* Function to keep track and update changes in route and arp table
  462. * Give regular statistics of packets forwarded
  463. */
  464. static int monitor_route(void)
  465. {
  466. unsigned int nr_cpus = bpf_num_possible_cpus();
  467. const unsigned int nr_keys = 256;
  468. struct pollfd fds_route, fds_arp;
  469. __u64 prev[nr_keys][nr_cpus];
  470. struct sockaddr_nl la, lr;
  471. __u64 values[nr_cpus];
  472. struct nlmsghdr *nh;
  473. int nll, ret = 0;
  474. int interval = 5;
  475. __u32 key;
  476. int i;
  477. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  478. if (sock < 0) {
  479. printf("open netlink socket: %s\n", strerror(errno));
  480. return -1;
  481. }
  482. fcntl(sock, F_SETFL, O_NONBLOCK);
  483. memset(&lr, 0, sizeof(lr));
  484. lr.nl_family = AF_NETLINK;
  485. lr.nl_groups = RTMGRP_IPV6_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_NOTIFY;
  486. if (bind(sock, (struct sockaddr *)&lr, sizeof(lr)) < 0) {
  487. printf("bind to netlink: %s\n", strerror(errno));
  488. ret = -1;
  489. goto cleanup;
  490. }
  491. fds_route.fd = sock;
  492. fds_route.events = POLL_IN;
  493. sock_arp = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  494. if (sock_arp < 0) {
  495. printf("open netlink socket: %s\n", strerror(errno));
  496. return -1;
  497. }
  498. fcntl(sock_arp, F_SETFL, O_NONBLOCK);
  499. memset(&la, 0, sizeof(la));
  500. la.nl_family = AF_NETLINK;
  501. la.nl_groups = RTMGRP_NEIGH | RTMGRP_NOTIFY;
  502. if (bind(sock_arp, (struct sockaddr *)&la, sizeof(la)) < 0) {
  503. printf("bind to netlink: %s\n", strerror(errno));
  504. ret = -1;
  505. goto cleanup;
  506. }
  507. fds_arp.fd = sock_arp;
  508. fds_arp.events = POLL_IN;
  509. memset(prev, 0, sizeof(prev));
  510. do {
  511. signal(SIGINT, close_and_exit);
  512. signal(SIGTERM, close_and_exit);
  513. sleep(interval);
  514. for (key = 0; key < nr_keys; key++) {
  515. __u64 sum = 0;
  516. assert(bpf_map_lookup_elem(map_fd[1], &key, values) == 0);
  517. for (i = 0; i < nr_cpus; i++)
  518. sum += (values[i] - prev[key][i]);
  519. if (sum)
  520. printf("proto %u: %10llu pkt/s\n",
  521. key, sum / interval);
  522. memcpy(prev[key], values, sizeof(values));
  523. }
  524. memset(buf, 0, sizeof(buf));
  525. if (poll(&fds_route, 1, 3) == POLL_IN) {
  526. nll = recv_msg(lr, sock);
  527. if (nll < 0) {
  528. printf("recv from netlink: %s\n", strerror(nll));
  529. ret = -1;
  530. goto cleanup;
  531. }
  532. nh = (struct nlmsghdr *)buf;
  533. printf("Routing table updated.\n");
  534. read_route(nh, nll);
  535. }
  536. memset(buf, 0, sizeof(buf));
  537. if (poll(&fds_arp, 1, 3) == POLL_IN) {
  538. nll = recv_msg(la, sock_arp);
  539. if (nll < 0) {
  540. printf("recv from netlink: %s\n", strerror(nll));
  541. ret = -1;
  542. goto cleanup;
  543. }
  544. nh = (struct nlmsghdr *)buf;
  545. read_arp(nh, nll);
  546. }
  547. } while (1);
  548. cleanup:
  549. close(sock);
  550. return ret;
  551. }
  552. int main(int ac, char **argv)
  553. {
  554. char filename[256];
  555. char **ifname_list;
  556. int i = 1;
  557. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  558. if (ac < 2) {
  559. printf("usage: %s [-S] Interface name list\n", argv[0]);
  560. return 1;
  561. }
  562. if (!strcmp(argv[1], "-S")) {
  563. flags = XDP_FLAGS_SKB_MODE;
  564. total_ifindex = ac - 2;
  565. ifname_list = (argv + 2);
  566. } else {
  567. flags = 0;
  568. total_ifindex = ac - 1;
  569. ifname_list = (argv + 1);
  570. }
  571. if (load_bpf_file(filename)) {
  572. printf("%s", bpf_log_buf);
  573. return 1;
  574. }
  575. printf("\n**************loading bpf file*********************\n\n\n");
  576. if (!prog_fd[0]) {
  577. printf("load_bpf_file: %s\n", strerror(errno));
  578. return 1;
  579. }
  580. ifindex_list = (int *)malloc(total_ifindex * sizeof(int *));
  581. for (i = 0; i < total_ifindex; i++) {
  582. ifindex_list[i] = if_nametoindex(ifname_list[i]);
  583. if (!ifindex_list[i]) {
  584. printf("Couldn't translate interface name: %s",
  585. strerror(errno));
  586. return 1;
  587. }
  588. }
  589. for (i = 0; i < total_ifindex; i++) {
  590. if (bpf_set_link_xdp_fd(ifindex_list[i], prog_fd[0], flags) < 0) {
  591. printf("link set xdp fd failed\n");
  592. int recovery_index = i;
  593. for (i = 0; i < recovery_index; i++)
  594. bpf_set_link_xdp_fd(ifindex_list[i], -1, flags);
  595. return 1;
  596. }
  597. printf("Attached to %d\n", ifindex_list[i]);
  598. }
  599. signal(SIGINT, int_exit);
  600. signal(SIGTERM, int_exit);
  601. printf("*******************ROUTE TABLE*************************\n\n\n");
  602. get_route_table(AF_INET);
  603. printf("*******************ARP TABLE***************************\n\n\n");
  604. get_arp_table(AF_INET);
  605. if (monitor_route() < 0) {
  606. printf("Error in receiving route update");
  607. return 1;
  608. }
  609. return 0;
  610. }