You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

libnetlink.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * libnetlink.c RTnetlink service routines.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. *
  11. * --
  12. *
  13. * This code has been lightly modified to adapt it in the Netsukuku source
  14. * code.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <syslog.h>
  20. #include <fcntl.h>
  21. #include <net/if_arp.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <time.h>
  27. #include <sys/uio.h>
  28. #include <stdarg.h>
  29. #include "libnetlink.h"
  30. #include "log.h"
  31. void rtnl_close(struct rtnl_handle *rth)
  32. {
  33. close(rth->fd);
  34. }
  35. int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions, int protocol)
  36. {
  37. socklen_t addr_len;
  38. int sndbuf = 32768;
  39. int rcvbuf = 32768;
  40. memset(rth, 0, sizeof(rth));
  41. rth->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
  42. if (rth->fd < 0) {
  43. perror("Cannot open netlink socket");
  44. return -1;
  45. }
  46. if (setsockopt(rth->fd,SOL_SOCKET,SO_SNDBUF,&sndbuf,sizeof(sndbuf)) < 0) {
  47. perror("SO_SNDBUF");
  48. return -1;
  49. }
  50. if (setsockopt(rth->fd,SOL_SOCKET,SO_RCVBUF,&rcvbuf,sizeof(rcvbuf)) < 0) {
  51. perror("SO_RCVBUF");
  52. return -1;
  53. }
  54. memset(&rth->local, 0, sizeof(rth->local));
  55. rth->local.nl_family = AF_NETLINK;
  56. rth->local.nl_groups = subscriptions;
  57. if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
  58. perror("Cannot bind netlink socket");
  59. return -1;
  60. }
  61. addr_len = sizeof(rth->local);
  62. if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
  63. perror("Cannot getsockname");
  64. return -1;
  65. }
  66. if (addr_len != sizeof(rth->local)) {
  67. error("Wrong address length %d\n", addr_len);
  68. return -1;
  69. }
  70. if (rth->local.nl_family != AF_NETLINK) {
  71. error("Wrong address family %d\n", rth->local.nl_family);
  72. return -1;
  73. }
  74. rth->seq = time(NULL);
  75. return 0;
  76. }
  77. int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
  78. {
  79. return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
  80. }
  81. int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
  82. {
  83. struct {
  84. struct nlmsghdr nlh;
  85. struct rtgenmsg g;
  86. } req;
  87. struct sockaddr_nl nladdr;
  88. memset(&nladdr, 0, sizeof(nladdr));
  89. nladdr.nl_family = AF_NETLINK;
  90. memset(&req, 0, sizeof(req));
  91. req.nlh.nlmsg_len = sizeof(req);
  92. req.nlh.nlmsg_type = type;
  93. req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  94. req.nlh.nlmsg_pid = 0;
  95. req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
  96. req.g.rtgen_family = family;
  97. return sendto(rth->fd, (void*)&req, sizeof(req), 0,
  98. (struct sockaddr*)&nladdr, sizeof(nladdr));
  99. }
  100. int rtnl_send(struct rtnl_handle *rth, const char *buf, int len)
  101. {
  102. struct sockaddr_nl nladdr;
  103. memset(&nladdr, 0, sizeof(nladdr));
  104. nladdr.nl_family = AF_NETLINK;
  105. return sendto(rth->fd, buf, len, 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
  106. }
  107. int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len)
  108. {
  109. struct nlmsghdr nlh;
  110. struct sockaddr_nl nladdr;
  111. struct iovec iov[2] = {
  112. { .iov_base = &nlh, .iov_len = sizeof(nlh) },
  113. { .iov_base = req, .iov_len = len }
  114. };
  115. struct msghdr msg = {
  116. .msg_name = &nladdr,
  117. .msg_namelen = sizeof(nladdr),
  118. .msg_iov = iov,
  119. .msg_iovlen = 2,
  120. };
  121. memset(&nladdr, 0, sizeof(nladdr));
  122. nladdr.nl_family = AF_NETLINK;
  123. nlh.nlmsg_len = NLMSG_LENGTH(len);
  124. nlh.nlmsg_type = type;
  125. nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
  126. nlh.nlmsg_pid = 0;
  127. nlh.nlmsg_seq = rth->dump = ++rth->seq;
  128. return sendmsg(rth->fd, &msg, 0);
  129. }
  130. int rtnl_dump_filter(struct rtnl_handle *rth,
  131. rtnl_filter_t filter,
  132. void *arg1,
  133. rtnl_filter_t junk,
  134. void *arg2)
  135. {
  136. struct sockaddr_nl nladdr;
  137. struct iovec iov;
  138. struct msghdr msg = {
  139. .msg_name = &nladdr,
  140. .msg_namelen = sizeof(nladdr),
  141. .msg_iov = &iov,
  142. .msg_iovlen = 1,
  143. };
  144. char buf[16384];
  145. iov.iov_base = buf;
  146. while (1) {
  147. int status;
  148. struct nlmsghdr *h;
  149. iov.iov_len = sizeof(buf);
  150. status = recvmsg(rth->fd, &msg, 0);
  151. if (status < 0) {
  152. if (errno == EINTR)
  153. continue;
  154. perror("OVERRUN");
  155. continue;
  156. }
  157. if (status == 0) {
  158. error("EOF on netlink\n");
  159. return -1;
  160. }
  161. h = (struct nlmsghdr*)buf;
  162. while (NLMSG_OK(h, status)) {
  163. int err;
  164. if (nladdr.nl_pid != 0 ||
  165. h->nlmsg_pid != rth->local.nl_pid ||
  166. h->nlmsg_seq != rth->dump) {
  167. if (junk) {
  168. err = junk(&nladdr, h, arg2);
  169. if (err < 0)
  170. return err;
  171. }
  172. goto skip_it;
  173. }
  174. if (h->nlmsg_type == NLMSG_DONE)
  175. return 0;
  176. if (h->nlmsg_type == NLMSG_ERROR) {
  177. struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
  178. if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
  179. error("ERROR truncated\n");
  180. } else {
  181. errno = -err->error;
  182. perror("RTNETLINK answers");
  183. }
  184. return -1;
  185. }
  186. err = filter(&nladdr, h, arg1);
  187. if (err < 0)
  188. return err;
  189. skip_it:
  190. h = NLMSG_NEXT(h, status);
  191. }
  192. if (msg.msg_flags & MSG_TRUNC) {
  193. error("Message truncated\n");
  194. continue;
  195. }
  196. if (status) {
  197. fatal("!!!Remnant of size %d\n", status);
  198. }
  199. }
  200. }
  201. int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
  202. unsigned groups, struct nlmsghdr *answer,
  203. rtnl_filter_t junk,
  204. void *jarg)
  205. {
  206. int status;
  207. unsigned seq;
  208. struct nlmsghdr *h;
  209. struct sockaddr_nl nladdr;
  210. struct iovec iov = {
  211. .iov_base = (void*) n,
  212. .iov_len = n->nlmsg_len
  213. };
  214. struct msghdr msg = {
  215. .msg_name = &nladdr,
  216. .msg_namelen = sizeof(nladdr),
  217. .msg_iov = &iov,
  218. .msg_iovlen = 1,
  219. };
  220. char buf[16384];
  221. memset(&nladdr, 0, sizeof(nladdr));
  222. nladdr.nl_family = AF_NETLINK;
  223. nladdr.nl_pid = peer;
  224. nladdr.nl_groups = groups;
  225. n->nlmsg_seq = seq = ++rtnl->seq;
  226. if (answer == NULL)
  227. n->nlmsg_flags |= NLM_F_ACK;
  228. status = sendmsg(rtnl->fd, &msg, 0);
  229. if (status < 0) {
  230. perror("Cannot talk to rtnetlink");
  231. return -1;
  232. }
  233. memset(buf,0,sizeof(buf));
  234. iov.iov_base = buf;
  235. while (1) {
  236. iov.iov_len = sizeof(buf);
  237. status = recvmsg(rtnl->fd, &msg, 0);
  238. if (status < 0) {
  239. if (errno == EINTR)
  240. continue;
  241. perror("OVERRUN");
  242. continue;
  243. }
  244. if (status == 0) {
  245. error("EOF on netlink\n");
  246. return -1;
  247. }
  248. if (msg.msg_namelen != sizeof(nladdr)) {
  249. fatal("sender address length == %d\n", msg.msg_namelen);
  250. }
  251. for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
  252. int err;
  253. int len = h->nlmsg_len;
  254. int l = len - sizeof(*h);
  255. if (l<0 || len>status) {
  256. if (msg.msg_flags & MSG_TRUNC) {
  257. error("Truncated message\n");
  258. return -1;
  259. }
  260. fatal("!!!malformed message: len=%d\n", len);
  261. }
  262. if (nladdr.nl_pid != peer ||
  263. h->nlmsg_pid != rtnl->local.nl_pid ||
  264. h->nlmsg_seq != seq) {
  265. if (junk) {
  266. err = junk(&nladdr, h, jarg);
  267. if (err < 0)
  268. return err;
  269. }
  270. continue;
  271. }
  272. if (h->nlmsg_type == NLMSG_ERROR) {
  273. struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
  274. if (l < sizeof(struct nlmsgerr)) {
  275. error("ERROR truncated\n");
  276. } else {
  277. errno = -err->error;
  278. if (errno == 0) {
  279. if (answer)
  280. memcpy(answer, h, h->nlmsg_len);
  281. return 0;
  282. }
  283. error("RTNETLINK answers (%d): %s", err->error, strerror(errno));
  284. }
  285. return -1;
  286. }
  287. if (answer) {
  288. memcpy(answer, h, h->nlmsg_len);
  289. return 0;
  290. }
  291. error("Unexpected reply!!!\n");
  292. status -= NLMSG_ALIGN(len);
  293. h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
  294. }
  295. if (msg.msg_flags & MSG_TRUNC) {
  296. error("Message truncated\n");
  297. continue;
  298. }
  299. if (status)
  300. fatal("!!!Remnant of size %d\n", status);
  301. }
  302. }
  303. int rtnl_listen(struct rtnl_handle *rtnl,
  304. rtnl_filter_t handler,
  305. void *jarg)
  306. {
  307. int status;
  308. struct nlmsghdr *h;
  309. struct sockaddr_nl nladdr;
  310. struct iovec iov;
  311. struct msghdr msg = {
  312. .msg_name = &nladdr,
  313. .msg_namelen = sizeof(nladdr),
  314. .msg_iov = &iov,
  315. .msg_iovlen = 1,
  316. };
  317. char buf[8192];
  318. memset(&nladdr, 0, sizeof(nladdr));
  319. nladdr.nl_family = AF_NETLINK;
  320. nladdr.nl_pid = 0;
  321. nladdr.nl_groups = 0;
  322. iov.iov_base = buf;
  323. while (1) {
  324. iov.iov_len = sizeof(buf);
  325. status = recvmsg(rtnl->fd, &msg, 0);
  326. if (status < 0) {
  327. if (errno == EINTR)
  328. continue;
  329. perror("OVERRUN");
  330. continue;
  331. }
  332. if (status == 0) {
  333. error("EOF on netlink\n");
  334. return -1;
  335. }
  336. if (msg.msg_namelen != sizeof(nladdr)) {
  337. fatal("Sender address length == %d\n", msg.msg_namelen);
  338. }
  339. for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
  340. int err;
  341. int len = h->nlmsg_len;
  342. int l = len - sizeof(*h);
  343. if (l<0 || len>status) {
  344. if (msg.msg_flags & MSG_TRUNC) {
  345. error("Truncated message\n");
  346. return -1;
  347. }
  348. fatal("!!!malformed message: len=%d\n", len);
  349. }
  350. err = handler(&nladdr, h, jarg);
  351. if (err < 0)
  352. return err;
  353. status -= NLMSG_ALIGN(len);
  354. h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
  355. }
  356. if (msg.msg_flags & MSG_TRUNC) {
  357. error("Message truncated\n");
  358. continue;
  359. }
  360. if (status) {
  361. fatal("!!!Remnant of size %d\n", status);
  362. }
  363. }
  364. }
  365. int rtnl_from_file(FILE *rtnl, rtnl_filter_t handler,
  366. void *jarg)
  367. {
  368. int status;
  369. struct sockaddr_nl nladdr;
  370. char buf[8192];
  371. struct nlmsghdr *h = (void*)buf;
  372. memset(&nladdr, 0, sizeof(nladdr));
  373. nladdr.nl_family = AF_NETLINK;
  374. nladdr.nl_pid = 0;
  375. nladdr.nl_groups = 0;
  376. while (1) {
  377. int err, len, type;
  378. int l;
  379. status = fread(&buf, 1, sizeof(*h), rtnl);
  380. if (status < 0) {
  381. if (errno == EINTR)
  382. continue;
  383. perror("rtnl_from_file: fread");
  384. return -1;
  385. }
  386. if (status == 0)
  387. return 0;
  388. len = h->nlmsg_len;
  389. type= h->nlmsg_type;
  390. l = len - sizeof(*h);
  391. if (l<0 || len>sizeof(buf)) {
  392. error("!!!malformed message: len=%d @%lu\n",
  393. len, ftell(rtnl));
  394. return -1;
  395. }
  396. status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
  397. if (status < 0) {
  398. perror("rtnl_from_file: fread");
  399. return -1;
  400. }
  401. if (status < l) {
  402. error("rtnl-from_file: truncated message\n");
  403. return -1;
  404. }
  405. err = handler(&nladdr, h, jarg);
  406. if (err < 0)
  407. return err;
  408. }
  409. }
  410. int addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data)
  411. {
  412. int len = RTA_LENGTH(4);
  413. struct rtattr *rta;
  414. if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) {
  415. error("addattr32: Error! max allowed bound %d exceeded\n",maxlen);
  416. return -1;
  417. }
  418. rta = NLMSG_TAIL(n);
  419. rta->rta_type = type;
  420. rta->rta_len = len;
  421. memcpy(RTA_DATA(rta), &data, 4);
  422. n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
  423. return 0;
  424. }
  425. int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
  426. int alen)
  427. {
  428. int len = RTA_LENGTH(alen);
  429. struct rtattr *rta;
  430. if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
  431. error("addattr_l ERROR: message exceeded bound of %d\n",maxlen);
  432. return -1;
  433. }
  434. rta = NLMSG_TAIL(n);
  435. rta->rta_type = type;
  436. rta->rta_len = len;
  437. memcpy(RTA_DATA(rta), data, alen);
  438. n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
  439. return 0;
  440. }
  441. int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
  442. {
  443. if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
  444. error("addraw_l ERROR: message exceeded bound of %d\n",maxlen);
  445. return -1;
  446. }
  447. memcpy(NLMSG_TAIL(n), data, len);
  448. memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
  449. n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
  450. return 0;
  451. }
  452. int rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data)
  453. {
  454. int len = RTA_LENGTH(4);
  455. struct rtattr *subrta;
  456. if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
  457. error("rta_addattr32: Error! max allowed bound %d exceeded\n",maxlen);
  458. return -1;
  459. }
  460. subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
  461. subrta->rta_type = type;
  462. subrta->rta_len = len;
  463. memcpy(RTA_DATA(subrta), &data, 4);
  464. rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
  465. return 0;
  466. }
  467. int rta_addattr_l(struct rtattr *rta, int maxlen, int type,
  468. const void *data, int alen)
  469. {
  470. struct rtattr *subrta;
  471. int len = RTA_LENGTH(alen);
  472. if (RTA_ALIGN(rta->rta_len) + len > maxlen) {
  473. error("rta_addattr_l: Error! max allowed bound %d exceeded\n",maxlen);
  474. return -1;
  475. }
  476. subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
  477. subrta->rta_type = type;
  478. subrta->rta_len = len;
  479. memcpy(RTA_DATA(subrta), data, alen);
  480. rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len;
  481. return 0;
  482. }
  483. int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
  484. {
  485. memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
  486. while (RTA_OK(rta, len)) {
  487. if (rta->rta_type <= max)
  488. tb[rta->rta_type] = rta;
  489. rta = RTA_NEXT(rta,len);
  490. }
  491. if (len)
  492. error("!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
  493. return 0;
  494. }
  495. int parse_rtattr_byindex(struct rtattr *tb[], int max, struct rtattr *rta, int len)
  496. {
  497. int i = 0;
  498. memset(tb, 0, sizeof(struct rtattr *) * max);
  499. while (RTA_OK(rta, len)) {
  500. if (rta->rta_type <= max && i < max)
  501. tb[i++] = rta;
  502. rta = RTA_NEXT(rta,len);
  503. }
  504. if (len)
  505. error("!!!Deficit %d, rta_len=%d\n", len, rta->rta_len);
  506. return i;
  507. }