3
0

libnetlink.c 9.4 KB

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