3
0

libnetlink.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * libnetlink.c RTnetlink service routines.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  11. *
  12. */
  13. #include <sys/socket.h>
  14. #include <sys/uio.h>
  15. #include "libbb.h"
  16. #include "libnetlink.h"
  17. void rtnl_close(struct rtnl_handle *rth)
  18. {
  19. close(rth->fd);
  20. }
  21. int xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
  22. {
  23. socklen_t addr_len;
  24. memset(rth, 0, sizeof(rth));
  25. rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  26. memset(&rth->local, 0, sizeof(rth->local));
  27. rth->local.nl_family = AF_NETLINK;
  28. /*rth->local.nl_groups = subscriptions;*/
  29. xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
  30. addr_len = sizeof(rth->local);
  31. if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
  32. bb_perror_msg_and_die("cannot getsockname");
  33. if (addr_len != sizeof(rth->local))
  34. bb_error_msg_and_die("wrong address length %d", addr_len);
  35. if (rth->local.nl_family != AF_NETLINK)
  36. bb_error_msg_and_die("wrong address family %d", rth->local.nl_family);
  37. rth->seq = time(NULL);
  38. return 0;
  39. }
  40. int xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
  41. {
  42. struct {
  43. struct nlmsghdr nlh;
  44. struct rtgenmsg g;
  45. } req;
  46. struct sockaddr_nl nladdr;
  47. memset(&nladdr, 0, sizeof(nladdr));
  48. nladdr.nl_family = AF_NETLINK;
  49. req.nlh.nlmsg_len = sizeof(req);
  50. req.nlh.nlmsg_type = type;
  51. req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  52. req.nlh.nlmsg_pid = 0;
  53. req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
  54. req.g.rtgen_family = family;
  55. return xsendto(rth->fd, (void*)&req, sizeof(req),
  56. (struct sockaddr*)&nladdr, sizeof(nladdr));
  57. }
  58. int rtnl_send(struct rtnl_handle *rth, char *buf, int len)
  59. {
  60. struct sockaddr_nl nladdr;
  61. memset(&nladdr, 0, sizeof(nladdr));
  62. nladdr.nl_family = AF_NETLINK;
  63. return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
  64. }
  65. int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
  66. {
  67. struct nlmsghdr nlh;
  68. struct sockaddr_nl nladdr;
  69. struct iovec iov[2] = { { &nlh, sizeof(nlh) }, { req, len } };
  70. struct msghdr msg = {
  71. (void*)&nladdr, sizeof(nladdr),
  72. iov, 2,
  73. NULL, 0,
  74. 0
  75. };
  76. memset(&nladdr, 0, sizeof(nladdr));
  77. nladdr.nl_family = AF_NETLINK;
  78. nlh.nlmsg_len = NLMSG_LENGTH(len);
  79. nlh.nlmsg_type = type;
  80. nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  81. nlh.nlmsg_pid = 0;
  82. nlh.nlmsg_seq = rth->dump = ++rth->seq;
  83. return sendmsg(rth->fd, &msg, 0);
  84. }
  85. static int rtnl_dump_filter(struct rtnl_handle *rth,
  86. int (*filter)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
  87. void *arg1/*,
  88. int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
  89. void *arg2*/)
  90. {
  91. int retval = -1;
  92. char *buf = xmalloc(8*1024); /* avoid big stack buffer */
  93. struct sockaddr_nl nladdr;
  94. struct iovec iov = { buf, 8*1024 };
  95. while (1) {
  96. int status;
  97. struct nlmsghdr *h;
  98. struct msghdr msg = {
  99. (void*)&nladdr, sizeof(nladdr),
  100. &iov, 1,
  101. NULL, 0,
  102. 0
  103. };
  104. status = recvmsg(rth->fd, &msg, 0);
  105. if (status < 0) {
  106. if (errno == EINTR)
  107. continue;
  108. bb_perror_msg("OVERRUN");
  109. continue;
  110. }
  111. if (status == 0) {
  112. bb_error_msg("EOF on netlink");
  113. goto ret;
  114. }
  115. if (msg.msg_namelen != sizeof(nladdr)) {
  116. bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
  117. }
  118. h = (struct nlmsghdr*)buf;
  119. while (NLMSG_OK(h, status)) {
  120. int err;
  121. if (nladdr.nl_pid != 0 ||
  122. h->nlmsg_pid != rth->local.nl_pid ||
  123. h->nlmsg_seq != rth->dump) {
  124. // if (junk) {
  125. // err = junk(&nladdr, h, arg2);
  126. // if (err < 0) {
  127. // retval = err;
  128. // goto ret;
  129. // }
  130. // }
  131. goto skip_it;
  132. }
  133. if (h->nlmsg_type == NLMSG_DONE) {
  134. goto ret_0;
  135. }
  136. if (h->nlmsg_type == NLMSG_ERROR) {
  137. struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
  138. if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
  139. bb_error_msg("ERROR truncated");
  140. } else {
  141. errno = -l_err->error;
  142. bb_perror_msg("RTNETLINK answers");
  143. }
  144. goto ret;
  145. }
  146. err = filter(&nladdr, h, arg1);
  147. if (err < 0) {
  148. retval = err;
  149. goto ret;
  150. }
  151. skip_it:
  152. h = NLMSG_NEXT(h, status);
  153. }
  154. if (msg.msg_flags & MSG_TRUNC) {
  155. bb_error_msg("message truncated");
  156. continue;
  157. }
  158. if (status) {
  159. bb_error_msg_and_die("remnant of size %d!", status);
  160. }
  161. } /* while (1) */
  162. ret_0:
  163. retval++; /* = 0 */
  164. ret:
  165. free(buf);
  166. return retval;
  167. }
  168. int xrtnl_dump_filter(struct rtnl_handle *rth,
  169. int (*filter)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
  170. void *arg1)
  171. {
  172. int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/);
  173. if (ret < 0)
  174. bb_error_msg_and_die("dump terminated");
  175. return ret;
  176. }
  177. int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
  178. pid_t peer, unsigned groups,
  179. struct nlmsghdr *answer,
  180. int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
  181. void *jarg)
  182. {
  183. /* bbox doesn't use parameters no. 3, 4, 6, 7, they are stubbed out */
  184. #define peer 0
  185. #define groups 0
  186. #define junk NULL
  187. #define jarg NULL
  188. int retval = -1;
  189. int status;
  190. unsigned seq;
  191. struct nlmsghdr *h;
  192. struct sockaddr_nl nladdr;
  193. struct iovec iov = { (void*)n, n->nlmsg_len };
  194. char *buf = xmalloc(8*1024); /* avoid big stack buffer */
  195. struct msghdr msg = {
  196. (void*)&nladdr, sizeof(nladdr),
  197. &iov, 1,
  198. NULL, 0,
  199. 0
  200. };
  201. memset(&nladdr, 0, sizeof(nladdr));
  202. nladdr.nl_family = AF_NETLINK;
  203. // nladdr.nl_pid = peer;
  204. // nladdr.nl_groups = groups;
  205. n->nlmsg_seq = seq = ++rtnl->seq;
  206. if (answer == NULL) {
  207. n->nlmsg_flags |= NLM_F_ACK;
  208. }
  209. status = sendmsg(rtnl->fd, &msg, 0);
  210. if (status < 0) {
  211. bb_perror_msg("cannot talk to rtnetlink");
  212. goto ret;
  213. }
  214. iov.iov_base = buf;
  215. while (1) {
  216. iov.iov_len = 8*1024;
  217. status = recvmsg(rtnl->fd, &msg, 0);
  218. if (status < 0) {
  219. if (errno == EINTR) {
  220. continue;
  221. }
  222. bb_perror_msg("OVERRUN");
  223. continue;
  224. }
  225. if (status == 0) {
  226. bb_error_msg("EOF on netlink");
  227. goto ret;
  228. }
  229. if (msg.msg_namelen != sizeof(nladdr)) {
  230. bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
  231. }
  232. for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
  233. // int l_err;
  234. int len = h->nlmsg_len;
  235. int l = len - sizeof(*h);
  236. if (l < 0 || len > status) {
  237. if (msg.msg_flags & MSG_TRUNC) {
  238. bb_error_msg("truncated message");
  239. goto ret;
  240. }
  241. bb_error_msg_and_die("malformed message: len=%d!", len);
  242. }
  243. if (nladdr.nl_pid != peer ||
  244. h->nlmsg_pid != rtnl->local.nl_pid ||
  245. h->nlmsg_seq != seq) {
  246. // if (junk) {
  247. // l_err = junk(&nladdr, h, jarg);
  248. // if (l_err < 0) {
  249. // retval = l_err;
  250. // goto ret;
  251. // }
  252. // }
  253. continue;
  254. }
  255. if (h->nlmsg_type == NLMSG_ERROR) {
  256. struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
  257. if (l < sizeof(struct nlmsgerr)) {
  258. bb_error_msg("ERROR truncated");
  259. } else {
  260. errno = - err->error;
  261. if (errno == 0) {
  262. if (answer) {
  263. memcpy(answer, h, h->nlmsg_len);
  264. }
  265. goto ret_0;
  266. }
  267. bb_perror_msg("RTNETLINK answers");
  268. }
  269. goto ret;
  270. }
  271. if (answer) {
  272. memcpy(answer, h, h->nlmsg_len);
  273. goto ret_0;
  274. }
  275. bb_error_msg("unexpected reply!");
  276. status -= NLMSG_ALIGN(len);
  277. h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
  278. }
  279. if (msg.msg_flags & MSG_TRUNC) {
  280. bb_error_msg("message truncated");
  281. continue;
  282. }
  283. if (status) {
  284. bb_error_msg_and_die("remnant of size %d!", status);
  285. }
  286. } /* while (1) */
  287. ret_0:
  288. retval++; /* = 0 */
  289. ret:
  290. free(buf);
  291. return retval;
  292. }
  293. int addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
  294. {
  295. int len = RTA_LENGTH(4);
  296. struct rtattr *rta;
  297. if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
  298. return -1;
  299. rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
  300. rta->rta_type = type;
  301. rta->rta_len = len;
  302. memcpy(RTA_DATA(rta), &data, 4);
  303. n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
  304. return 0;
  305. }
  306. int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
  307. {
  308. int len = RTA_LENGTH(alen);
  309. struct rtattr *rta;
  310. if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen)
  311. return -1;
  312. rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
  313. rta->rta_type = type;
  314. rta->rta_len = len;
  315. memcpy(RTA_DATA(rta), data, alen);
  316. n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
  317. return 0;
  318. }
  319. int rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
  320. {
  321. int len = RTA_LENGTH(4);
  322. struct rtattr *subrta;
  323. if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
  324. return -1;
  325. }
  326. subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
  327. subrta->rta_type = type;
  328. subrta->rta_len = len;
  329. memcpy(RTA_DATA(subrta), &data, 4);
  330. rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
  331. return 0;
  332. }
  333. int rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
  334. {
  335. struct rtattr *subrta;
  336. int len = RTA_LENGTH(alen);
  337. if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
  338. return -1;
  339. }
  340. subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
  341. subrta->rta_type = type;
  342. subrta->rta_len = len;
  343. memcpy(RTA_DATA(subrta), data, alen);
  344. rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
  345. return 0;
  346. }
  347. int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
  348. {
  349. while (RTA_OK(rta, len)) {
  350. if (rta->rta_type <= max) {
  351. tb[rta->rta_type] = rta;
  352. }
  353. rta = RTA_NEXT(rta,len);
  354. }
  355. if (len) {
  356. bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
  357. }
  358. return 0;
  359. }