3
0

libnetlink.c 11 KB

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