libnetlink.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. socklen_t addr_len;
  17. memset(rth, 0, sizeof(*rth));
  18. rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  19. rth->local.nl_family = AF_NETLINK;
  20. /*rth->local.nl_groups = subscriptions;*/
  21. xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
  22. addr_len = sizeof(rth->local);
  23. getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len);
  24. /* too much paranoia
  25. if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
  26. bb_perror_msg_and_die("getsockname");
  27. if (addr_len != sizeof(rth->local))
  28. bb_error_msg_and_die("wrong address length %d", addr_len);
  29. if (rth->local.nl_family != AF_NETLINK)
  30. bb_error_msg_and_die("wrong address family %d", rth->local.nl_family);
  31. */
  32. rth->seq = time(NULL);
  33. }
  34. int FAST_FUNC xrtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
  35. {
  36. struct {
  37. struct nlmsghdr nlh;
  38. struct rtgenmsg g;
  39. } req;
  40. req.nlh.nlmsg_len = sizeof(req);
  41. req.nlh.nlmsg_type = type;
  42. req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  43. req.nlh.nlmsg_pid = 0;
  44. req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
  45. req.g.rtgen_family = family;
  46. return rtnl_send(rth, (void*)&req, sizeof(req));
  47. }
  48. //TODO: pass rth->fd instead of full rth?
  49. int FAST_FUNC rtnl_send(struct rtnl_handle *rth, char *buf, int len)
  50. {
  51. struct sockaddr_nl nladdr;
  52. memset(&nladdr, 0, sizeof(nladdr));
  53. nladdr.nl_family = AF_NETLINK;
  54. return xsendto(rth->fd, buf, len, (struct sockaddr*)&nladdr, sizeof(nladdr));
  55. }
  56. int FAST_FUNC rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
  57. {
  58. struct {
  59. struct nlmsghdr nlh;
  60. struct msghdr msg;
  61. struct sockaddr_nl nladdr;
  62. } s;
  63. struct iovec iov[2] = { { &s.nlh, sizeof(s.nlh) }, { req, len } };
  64. memset(&s, 0, sizeof(s));
  65. s.msg.msg_name = (void*)&s.nladdr;
  66. s.msg.msg_namelen = sizeof(s.nladdr);
  67. s.msg.msg_iov = iov;
  68. s.msg.msg_iovlen = 2;
  69. /*s.msg.msg_control = NULL; - already is */
  70. /*s.msg.msg_controllen = 0; - already is */
  71. /*s.msg.msg_flags = 0; - already is */
  72. s.nladdr.nl_family = AF_NETLINK;
  73. s.nlh.nlmsg_len = NLMSG_LENGTH(len);
  74. s.nlh.nlmsg_type = type;
  75. s.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  76. /*s.nlh.nlmsg_pid = 0; - already is */
  77. s.nlh.nlmsg_seq = rth->dump = ++rth->seq;
  78. return sendmsg(rth->fd, &s.msg, 0);
  79. }
  80. static int rtnl_dump_filter(struct rtnl_handle *rth,
  81. int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *n, void *) FAST_FUNC,
  82. void *arg1/*,
  83. int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *),
  84. void *arg2*/)
  85. {
  86. int retval = -1;
  87. char *buf = xmalloc(8*1024); /* avoid big stack buffer */
  88. struct sockaddr_nl nladdr;
  89. struct iovec iov = { buf, 8*1024 };
  90. while (1) {
  91. int status;
  92. struct nlmsghdr *h;
  93. /* Use designated initializers, struct layout is non-portable */
  94. struct msghdr msg = {
  95. .msg_name = (void*)&nladdr,
  96. .msg_namelen = sizeof(nladdr),
  97. .msg_iov = &iov,
  98. .msg_iovlen = 1,
  99. .msg_control = NULL,
  100. .msg_controllen = 0,
  101. .msg_flags = 0
  102. };
  103. status = recvmsg(rth->fd, &msg, 0);
  104. if (status < 0) {
  105. if (errno == EINTR)
  106. continue;
  107. bb_perror_msg("OVERRUN");
  108. continue;
  109. }
  110. if (status == 0) {
  111. bb_error_msg("EOF on netlink");
  112. goto ret;
  113. }
  114. if (msg.msg_namelen != sizeof(nladdr)) {
  115. bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
  116. }
  117. h = (struct nlmsghdr*)buf;
  118. while (NLMSG_OK(h, status)) {
  119. int err;
  120. if (nladdr.nl_pid != 0 ||
  121. h->nlmsg_pid != rth->local.nl_pid ||
  122. h->nlmsg_seq != rth->dump
  123. ) {
  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 FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth,
  169. int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *) FAST_FUNC,
  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 FAST_FUNC 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 *, 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. /* Use designated initializers, struct layout is non-portable */
  196. struct msghdr msg = {
  197. .msg_name = (void*)&nladdr,
  198. .msg_namelen = sizeof(nladdr),
  199. .msg_iov = &iov,
  200. .msg_iovlen = 1,
  201. .msg_control = NULL,
  202. .msg_controllen = 0,
  203. .msg_flags = 0
  204. };
  205. memset(&nladdr, 0, sizeof(nladdr));
  206. nladdr.nl_family = AF_NETLINK;
  207. // nladdr.nl_pid = peer;
  208. // nladdr.nl_groups = groups;
  209. n->nlmsg_seq = seq = ++rtnl->seq;
  210. if (answer == NULL) {
  211. n->nlmsg_flags |= NLM_F_ACK;
  212. }
  213. status = sendmsg(rtnl->fd, &msg, 0);
  214. if (status < 0) {
  215. bb_perror_msg("can't talk to rtnetlink");
  216. goto ret;
  217. }
  218. iov.iov_base = buf;
  219. while (1) {
  220. iov.iov_len = 8*1024;
  221. status = recvmsg(rtnl->fd, &msg, 0);
  222. if (status < 0) {
  223. if (errno == EINTR) {
  224. continue;
  225. }
  226. bb_perror_msg("OVERRUN");
  227. continue;
  228. }
  229. if (status == 0) {
  230. bb_error_msg("EOF on netlink");
  231. goto ret;
  232. }
  233. if (msg.msg_namelen != sizeof(nladdr)) {
  234. bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
  235. }
  236. for (h = (struct nlmsghdr*)buf; status >= (int)sizeof(*h); ) {
  237. // int l_err;
  238. int len = h->nlmsg_len;
  239. int l = len - sizeof(*h);
  240. if (l < 0 || len > status) {
  241. if (msg.msg_flags & MSG_TRUNC) {
  242. bb_error_msg("truncated message");
  243. goto ret;
  244. }
  245. bb_error_msg_and_die("malformed message: len=%d!", len);
  246. }
  247. if (nladdr.nl_pid != peer ||
  248. h->nlmsg_pid != rtnl->local.nl_pid ||
  249. h->nlmsg_seq != seq
  250. ) {
  251. // if (junk) {
  252. // l_err = junk(&nladdr, h, jarg);
  253. // if (l_err < 0) {
  254. // retval = l_err;
  255. // goto ret;
  256. // }
  257. // }
  258. continue;
  259. }
  260. if (h->nlmsg_type == NLMSG_ERROR) {
  261. struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
  262. if (l < (int)sizeof(struct nlmsgerr)) {
  263. bb_error_msg("ERROR truncated");
  264. } else {
  265. errno = - err->error;
  266. if (errno == 0) {
  267. if (answer) {
  268. memcpy(answer, h, h->nlmsg_len);
  269. }
  270. goto ret_0;
  271. }
  272. bb_perror_msg("RTNETLINK answers");
  273. }
  274. goto ret;
  275. }
  276. if (answer) {
  277. memcpy(answer, h, h->nlmsg_len);
  278. goto ret_0;
  279. }
  280. bb_error_msg("unexpected reply!");
  281. status -= NLMSG_ALIGN(len);
  282. h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
  283. }
  284. if (msg.msg_flags & MSG_TRUNC) {
  285. bb_error_msg("message truncated");
  286. continue;
  287. }
  288. if (status) {
  289. bb_error_msg_and_die("remnant of size %d!", status);
  290. }
  291. } /* while (1) */
  292. ret_0:
  293. retval++; /* = 0 */
  294. ret:
  295. free(buf);
  296. return retval;
  297. }
  298. int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
  299. {
  300. int len = RTA_LENGTH(4);
  301. struct rtattr *rta;
  302. if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) {
  303. return -1;
  304. }
  305. rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
  306. rta->rta_type = type;
  307. rta->rta_len = len;
  308. move_to_unaligned32(RTA_DATA(rta), data);
  309. n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len);
  310. return 0;
  311. }
  312. int FAST_FUNC addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
  313. {
  314. int len = RTA_LENGTH(alen);
  315. struct rtattr *rta;
  316. if ((int)(NLMSG_ALIGN(n->nlmsg_len + len)) > maxlen) {
  317. return -1;
  318. }
  319. rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
  320. rta->rta_type = type;
  321. rta->rta_len = len;
  322. memcpy(RTA_DATA(rta), data, alen);
  323. n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len + len);
  324. return 0;
  325. }
  326. int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
  327. {
  328. int len = RTA_LENGTH(4);
  329. struct rtattr *subrta;
  330. if (RTA_ALIGN(rta->rta_len + len) > maxlen) {
  331. return -1;
  332. }
  333. subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
  334. subrta->rta_type = type;
  335. subrta->rta_len = len;
  336. move_to_unaligned32(RTA_DATA(subrta), data);
  337. rta->rta_len = NLMSG_ALIGN(rta->rta_len + len);
  338. return 0;
  339. }
  340. int FAST_FUNC rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen)
  341. {
  342. struct rtattr *subrta;
  343. int len = RTA_LENGTH(alen);
  344. if (RTA_ALIGN(rta->rta_len + len) > maxlen) {
  345. return -1;
  346. }
  347. subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
  348. subrta->rta_type = type;
  349. subrta->rta_len = len;
  350. memcpy(RTA_DATA(subrta), data, alen);
  351. rta->rta_len = NLMSG_ALIGN(rta->rta_len + len);
  352. return 0;
  353. }
  354. void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
  355. {
  356. while (RTA_OK(rta, len)) {
  357. if (rta->rta_type <= max) {
  358. tb[rta->rta_type] = rta;
  359. }
  360. rta = RTA_NEXT(rta, len);
  361. }
  362. if (len) {
  363. bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
  364. }
  365. }