3
0

libnetlink.c 9.4 KB

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