libnetlink.c 10.0 KB

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