libnetlink.c 9.6 KB

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