netlink.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. /* Copyright (c) 2018 Facebook */
  3. #include <stdlib.h>
  4. #include <memory.h>
  5. #include <unistd.h>
  6. #include <linux/bpf.h>
  7. #include <linux/rtnetlink.h>
  8. #include <sys/socket.h>
  9. #include <errno.h>
  10. #include <time.h>
  11. #include "bpf.h"
  12. #include "libbpf.h"
  13. #include "nlattr.h"
  14. #ifndef SOL_NETLINK
  15. #define SOL_NETLINK 270
  16. #endif
  17. typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, libbpf_dump_nlmsg_t,
  18. void *cookie);
  19. int libbpf_netlink_open(__u32 *nl_pid)
  20. {
  21. struct sockaddr_nl sa;
  22. socklen_t addrlen;
  23. int one = 1, ret;
  24. int sock;
  25. memset(&sa, 0, sizeof(sa));
  26. sa.nl_family = AF_NETLINK;
  27. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  28. if (sock < 0)
  29. return -errno;
  30. if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
  31. &one, sizeof(one)) < 0) {
  32. fprintf(stderr, "Netlink error reporting not supported\n");
  33. }
  34. if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  35. ret = -errno;
  36. goto cleanup;
  37. }
  38. addrlen = sizeof(sa);
  39. if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
  40. ret = -errno;
  41. goto cleanup;
  42. }
  43. if (addrlen != sizeof(sa)) {
  44. ret = -LIBBPF_ERRNO__INTERNAL;
  45. goto cleanup;
  46. }
  47. *nl_pid = sa.nl_pid;
  48. return sock;
  49. cleanup:
  50. close(sock);
  51. return ret;
  52. }
  53. static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq,
  54. __dump_nlmsg_t _fn, libbpf_dump_nlmsg_t fn,
  55. void *cookie)
  56. {
  57. bool multipart = true;
  58. struct nlmsgerr *err;
  59. struct nlmsghdr *nh;
  60. char buf[4096];
  61. int len, ret;
  62. while (multipart) {
  63. multipart = false;
  64. len = recv(sock, buf, sizeof(buf), 0);
  65. if (len < 0) {
  66. ret = -errno;
  67. goto done;
  68. }
  69. if (len == 0)
  70. break;
  71. for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
  72. nh = NLMSG_NEXT(nh, len)) {
  73. if (nh->nlmsg_pid != nl_pid) {
  74. ret = -LIBBPF_ERRNO__WRNGPID;
  75. goto done;
  76. }
  77. if (nh->nlmsg_seq != seq) {
  78. ret = -LIBBPF_ERRNO__INVSEQ;
  79. goto done;
  80. }
  81. if (nh->nlmsg_flags & NLM_F_MULTI)
  82. multipart = true;
  83. switch (nh->nlmsg_type) {
  84. case NLMSG_ERROR:
  85. err = (struct nlmsgerr *)NLMSG_DATA(nh);
  86. if (!err->error)
  87. continue;
  88. ret = err->error;
  89. libbpf_nla_dump_errormsg(nh);
  90. goto done;
  91. case NLMSG_DONE:
  92. return 0;
  93. default:
  94. break;
  95. }
  96. if (_fn) {
  97. ret = _fn(nh, fn, cookie);
  98. if (ret)
  99. return ret;
  100. }
  101. }
  102. }
  103. ret = 0;
  104. done:
  105. return ret;
  106. }
  107. int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
  108. {
  109. int sock, seq = 0, ret;
  110. struct nlattr *nla, *nla_xdp;
  111. struct {
  112. struct nlmsghdr nh;
  113. struct ifinfomsg ifinfo;
  114. char attrbuf[64];
  115. } req;
  116. __u32 nl_pid;
  117. sock = libbpf_netlink_open(&nl_pid);
  118. if (sock < 0)
  119. return sock;
  120. memset(&req, 0, sizeof(req));
  121. req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
  122. req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
  123. req.nh.nlmsg_type = RTM_SETLINK;
  124. req.nh.nlmsg_pid = 0;
  125. req.nh.nlmsg_seq = ++seq;
  126. req.ifinfo.ifi_family = AF_UNSPEC;
  127. req.ifinfo.ifi_index = ifindex;
  128. /* started nested attribute for XDP */
  129. nla = (struct nlattr *)(((char *)&req)
  130. + NLMSG_ALIGN(req.nh.nlmsg_len));
  131. nla->nla_type = NLA_F_NESTED | IFLA_XDP;
  132. nla->nla_len = NLA_HDRLEN;
  133. /* add XDP fd */
  134. nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
  135. nla_xdp->nla_type = IFLA_XDP_FD;
  136. nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
  137. memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
  138. nla->nla_len += nla_xdp->nla_len;
  139. /* if user passed in any flags, add those too */
  140. if (flags) {
  141. nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
  142. nla_xdp->nla_type = IFLA_XDP_FLAGS;
  143. nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
  144. memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
  145. nla->nla_len += nla_xdp->nla_len;
  146. }
  147. req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
  148. if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
  149. ret = -errno;
  150. goto cleanup;
  151. }
  152. ret = bpf_netlink_recv(sock, nl_pid, seq, NULL, NULL, NULL);
  153. cleanup:
  154. close(sock);
  155. return ret;
  156. }
  157. static int __dump_link_nlmsg(struct nlmsghdr *nlh,
  158. libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
  159. {
  160. struct nlattr *tb[IFLA_MAX + 1], *attr;
  161. struct ifinfomsg *ifi = NLMSG_DATA(nlh);
  162. int len;
  163. len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
  164. attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
  165. if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
  166. return -LIBBPF_ERRNO__NLPARSE;
  167. return dump_link_nlmsg(cookie, ifi, tb);
  168. }
  169. int libbpf_nl_get_link(int sock, unsigned int nl_pid,
  170. libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
  171. {
  172. struct {
  173. struct nlmsghdr nlh;
  174. struct ifinfomsg ifm;
  175. } req = {
  176. .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
  177. .nlh.nlmsg_type = RTM_GETLINK,
  178. .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
  179. .ifm.ifi_family = AF_PACKET,
  180. };
  181. int seq = time(NULL);
  182. req.nlh.nlmsg_seq = seq;
  183. if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
  184. return -errno;
  185. return bpf_netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg,
  186. dump_link_nlmsg, cookie);
  187. }
  188. static int __dump_class_nlmsg(struct nlmsghdr *nlh,
  189. libbpf_dump_nlmsg_t dump_class_nlmsg,
  190. void *cookie)
  191. {
  192. struct nlattr *tb[TCA_MAX + 1], *attr;
  193. struct tcmsg *t = NLMSG_DATA(nlh);
  194. int len;
  195. len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
  196. attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
  197. if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
  198. return -LIBBPF_ERRNO__NLPARSE;
  199. return dump_class_nlmsg(cookie, t, tb);
  200. }
  201. int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
  202. libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie)
  203. {
  204. struct {
  205. struct nlmsghdr nlh;
  206. struct tcmsg t;
  207. } req = {
  208. .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
  209. .nlh.nlmsg_type = RTM_GETTCLASS,
  210. .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
  211. .t.tcm_family = AF_UNSPEC,
  212. .t.tcm_ifindex = ifindex,
  213. };
  214. int seq = time(NULL);
  215. req.nlh.nlmsg_seq = seq;
  216. if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
  217. return -errno;
  218. return bpf_netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg,
  219. dump_class_nlmsg, cookie);
  220. }
  221. static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
  222. libbpf_dump_nlmsg_t dump_qdisc_nlmsg,
  223. void *cookie)
  224. {
  225. struct nlattr *tb[TCA_MAX + 1], *attr;
  226. struct tcmsg *t = NLMSG_DATA(nlh);
  227. int len;
  228. len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
  229. attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
  230. if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
  231. return -LIBBPF_ERRNO__NLPARSE;
  232. return dump_qdisc_nlmsg(cookie, t, tb);
  233. }
  234. int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
  235. libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
  236. {
  237. struct {
  238. struct nlmsghdr nlh;
  239. struct tcmsg t;
  240. } req = {
  241. .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
  242. .nlh.nlmsg_type = RTM_GETQDISC,
  243. .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
  244. .t.tcm_family = AF_UNSPEC,
  245. .t.tcm_ifindex = ifindex,
  246. };
  247. int seq = time(NULL);
  248. req.nlh.nlmsg_seq = seq;
  249. if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
  250. return -errno;
  251. return bpf_netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg,
  252. dump_qdisc_nlmsg, cookie);
  253. }
  254. static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
  255. libbpf_dump_nlmsg_t dump_filter_nlmsg,
  256. void *cookie)
  257. {
  258. struct nlattr *tb[TCA_MAX + 1], *attr;
  259. struct tcmsg *t = NLMSG_DATA(nlh);
  260. int len;
  261. len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
  262. attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
  263. if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
  264. return -LIBBPF_ERRNO__NLPARSE;
  265. return dump_filter_nlmsg(cookie, t, tb);
  266. }
  267. int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
  268. libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie)
  269. {
  270. struct {
  271. struct nlmsghdr nlh;
  272. struct tcmsg t;
  273. } req = {
  274. .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
  275. .nlh.nlmsg_type = RTM_GETTFILTER,
  276. .nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
  277. .t.tcm_family = AF_UNSPEC,
  278. .t.tcm_ifindex = ifindex,
  279. .t.tcm_parent = handle,
  280. };
  281. int seq = time(NULL);
  282. req.nlh.nlmsg_seq = seq;
  283. if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
  284. return -errno;
  285. return bpf_netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg,
  286. dump_filter_nlmsg, cookie);
  287. }