netlink.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /**
  2. * Copyright (C) 2017 Hans Dedecker <dedeckeh@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License v2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <errno.h>
  15. #include <string.h>
  16. #include <syslog.h>
  17. #include <linux/netlink.h>
  18. #include <linux/if_addr.h>
  19. #include <linux/neighbour.h>
  20. #include <linux/rtnetlink.h>
  21. #include <netlink/msg.h>
  22. #include <netlink/socket.h>
  23. #include <netlink/attr.h>
  24. #include <arpa/inet.h>
  25. #include <libubox/list.h>
  26. #include "odhcpd.h"
  27. struct event_socket {
  28. struct odhcpd_event ev;
  29. struct nl_sock *sock;
  30. int sock_bufsize;
  31. };
  32. static void handle_rtnl_event(struct odhcpd_event *ev);
  33. static int cb_rtnl_valid(struct nl_msg *msg, void *arg);
  34. static void catch_rtnl_err(struct odhcpd_event *e, int error);
  35. static struct nl_sock *create_socket(int protocol);
  36. static struct nl_sock *rtnl_socket = NULL;
  37. struct list_head netevent_handler_list = LIST_HEAD_INIT(netevent_handler_list);
  38. static struct event_socket rtnl_event = {
  39. .ev = {
  40. .uloop = {.fd = - 1, },
  41. .handle_dgram = NULL,
  42. .handle_error = catch_rtnl_err,
  43. .recv_msgs = handle_rtnl_event,
  44. },
  45. .sock = NULL,
  46. .sock_bufsize = 133120,
  47. };
  48. int netlink_init(void)
  49. {
  50. rtnl_socket = create_socket(NETLINK_ROUTE);
  51. if (!rtnl_socket) {
  52. syslog(LOG_ERR, "Unable to open nl socket: %m");
  53. goto err;
  54. }
  55. rtnl_event.sock = create_socket(NETLINK_ROUTE);
  56. if (!rtnl_event.sock) {
  57. syslog(LOG_ERR, "Unable to open nl event socket: %m");
  58. goto err;
  59. }
  60. rtnl_event.ev.uloop.fd = nl_socket_get_fd(rtnl_event.sock);
  61. if (nl_socket_set_buffer_size(rtnl_event.sock, rtnl_event.sock_bufsize, 0))
  62. goto err;
  63. nl_socket_disable_seq_check(rtnl_event.sock);
  64. nl_socket_modify_cb(rtnl_event.sock, NL_CB_VALID, NL_CB_CUSTOM,
  65. cb_rtnl_valid, NULL);
  66. /* Receive IPv4 address, IPv6 address, IPv6 routes and neighbor events */
  67. if (nl_socket_add_memberships(rtnl_event.sock, RTNLGRP_IPV4_IFADDR,
  68. RTNLGRP_IPV6_IFADDR, RTNLGRP_IPV6_ROUTE,
  69. RTNLGRP_NEIGH, RTNLGRP_LINK, 0))
  70. goto err;
  71. odhcpd_register(&rtnl_event.ev);
  72. return 0;
  73. err:
  74. if (rtnl_socket) {
  75. nl_socket_free(rtnl_socket);
  76. rtnl_socket = NULL;
  77. }
  78. if (rtnl_event.sock) {
  79. nl_socket_free(rtnl_event.sock);
  80. rtnl_event.sock = NULL;
  81. rtnl_event.ev.uloop.fd = -1;
  82. }
  83. return -1;
  84. }
  85. int netlink_add_netevent_handler(struct netevent_handler *handler)
  86. {
  87. if (!handler->cb)
  88. return -1;
  89. list_add(&handler->head, &netevent_handler_list);
  90. return 0;
  91. }
  92. static void call_netevent_handler_list(unsigned long event, struct netevent_handler_info *info)
  93. {
  94. struct netevent_handler *handler;
  95. list_for_each_entry(handler, &netevent_handler_list, head)
  96. handler->cb(event, info);
  97. }
  98. static void handle_rtnl_event(struct odhcpd_event *e)
  99. {
  100. struct event_socket *ev_sock = container_of(e, struct event_socket, ev);
  101. nl_recvmsgs_default(ev_sock->sock);
  102. }
  103. static void refresh_iface_addr4(int ifindex)
  104. {
  105. struct odhcpd_ipaddr *addr = NULL;
  106. struct interface *iface;
  107. ssize_t len = netlink_get_interface_addrs(ifindex, false, &addr);
  108. bool change = false;
  109. if (len < 0)
  110. return;
  111. avl_for_each_element(&interfaces, iface, avl) {
  112. struct netevent_handler_info event_info;
  113. if (iface->ifindex != ifindex)
  114. continue;
  115. memset(&event_info, 0, sizeof(event_info));
  116. event_info.iface = iface;
  117. event_info.addrs_old.addrs = iface->addr4;
  118. event_info.addrs_old.len = iface->addr4_len;
  119. if (!change) {
  120. change = len != (ssize_t)iface->addr4_len;
  121. for (ssize_t i = 0; !change && i < len; ++i) {
  122. if (addr[i].addr.in.s_addr != iface->addr4[i].addr.in.s_addr)
  123. change = true;
  124. }
  125. }
  126. iface->addr4 = addr;
  127. iface->addr4_len = len;
  128. if (change)
  129. call_netevent_handler_list(NETEV_ADDRLIST_CHANGE, &event_info);
  130. free(event_info.addrs_old.addrs);
  131. if (!len)
  132. continue;
  133. addr = malloc(len * sizeof(*addr));
  134. if (!addr)
  135. break;
  136. memcpy(addr, iface->addr4, len * sizeof(*addr));
  137. }
  138. free(addr);
  139. }
  140. static void refresh_iface_addr6(int ifindex)
  141. {
  142. struct odhcpd_ipaddr *addr = NULL;
  143. struct interface *iface;
  144. ssize_t len = netlink_get_interface_addrs(ifindex, true, &addr);
  145. time_t now = odhcpd_time();
  146. bool change = false;
  147. if (len < 0)
  148. return;
  149. avl_for_each_element(&interfaces, iface, avl) {
  150. struct netevent_handler_info event_info;
  151. if (iface->ifindex != ifindex)
  152. continue;
  153. memset(&event_info, 0, sizeof(event_info));
  154. event_info.iface = iface;
  155. event_info.addrs_old.addrs = iface->addr6;
  156. event_info.addrs_old.len = iface->addr6_len;
  157. if (!change) {
  158. change = len != (ssize_t)iface->addr6_len;
  159. for (ssize_t i = 0; !change && i < len; ++i) {
  160. if (!IN6_ARE_ADDR_EQUAL(&addr[i].addr.in6, &iface->addr6[i].addr.in6) ||
  161. addr[i].prefix != iface->addr6[i].prefix ||
  162. (addr[i].preferred_lt > (uint32_t)now) != (iface->addr6[i].preferred_lt > (uint32_t)now) ||
  163. addr[i].valid_lt < iface->addr6[i].valid_lt || addr[i].preferred_lt < iface->addr6[i].preferred_lt)
  164. change = true;
  165. }
  166. if (change) {
  167. /*
  168. * Keep track of removed prefixes, so we could advertise them as invalid
  169. * for at least a couple of times.
  170. *
  171. * RFC7084 § 4.3 :
  172. * L-13: If the delegated prefix changes, i.e., the current prefix is
  173. * replaced with a new prefix without any overlapping time
  174. * period, then the IPv6 CE router MUST immediately advertise the
  175. * old prefix with a Preferred Lifetime of zero and a Valid
  176. * Lifetime of either a) zero or b) the lower of the current
  177. * Valid Lifetime and two hours (which must be decremented in
  178. * real time) in a Router Advertisement message as described in
  179. * Section 5.5.3, (e) of [RFC4862].
  180. */
  181. for (size_t i = 0; i < iface->addr6_len; ++i) {
  182. bool removed = true;
  183. if (iface->addr6[i].valid_lt <= (uint32_t)now)
  184. continue;
  185. for (ssize_t j = 0; removed && j < len; ++j) {
  186. size_t plen = min(addr[j].prefix, iface->addr6[i].prefix);
  187. if (odhcpd_bmemcmp(&addr[j].addr.in6, &iface->addr6[i].addr.in6, plen) == 0)
  188. removed = false;
  189. }
  190. for (size_t j = 0; removed && j < iface->invalid_addr6_len; ++j) {
  191. size_t plen = min(iface->invalid_addr6[j].prefix, iface->addr6[i].prefix);
  192. if (odhcpd_bmemcmp(&iface->invalid_addr6[j].addr.in6, &iface->addr6[i].addr.in6, plen) == 0)
  193. removed = false;
  194. }
  195. if (removed) {
  196. size_t pos = iface->invalid_addr6_len;
  197. struct odhcpd_ipaddr *new_invalid_addr6 = realloc(iface->invalid_addr6,
  198. sizeof(*iface->invalid_addr6) * (pos + 1));
  199. if (!new_invalid_addr6)
  200. break;
  201. iface->invalid_addr6 = new_invalid_addr6;
  202. iface->invalid_addr6_len++;
  203. memcpy(&iface->invalid_addr6[pos], &iface->addr6[i], sizeof(*iface->invalid_addr6));
  204. iface->invalid_addr6[pos].valid_lt = iface->invalid_addr6[pos].preferred_lt = (uint32_t)now;
  205. if (iface->invalid_addr6[pos].prefix < 64)
  206. iface->invalid_addr6[pos].prefix = 64;
  207. }
  208. }
  209. }
  210. }
  211. iface->addr6 = addr;
  212. iface->addr6_len = len;
  213. if (change)
  214. call_netevent_handler_list(NETEV_ADDR6LIST_CHANGE, &event_info);
  215. free(event_info.addrs_old.addrs);
  216. if (!len)
  217. continue;
  218. addr = malloc(len * sizeof(*addr));
  219. if (!addr)
  220. break;
  221. memcpy(addr, iface->addr6, len * sizeof(*addr));
  222. }
  223. free(addr);
  224. }
  225. static int handle_rtm_link(struct nlmsghdr *hdr)
  226. {
  227. struct ifinfomsg *ifi = nlmsg_data(hdr);
  228. struct nlattr *nla[__IFLA_MAX];
  229. struct interface *iface;
  230. struct netevent_handler_info event_info;
  231. const char *ifname;
  232. memset(&event_info, 0, sizeof(event_info));
  233. if (!nlmsg_valid_hdr(hdr, sizeof(*ifi)) || ifi->ifi_family != AF_UNSPEC)
  234. return NL_SKIP;
  235. nlmsg_parse(hdr, sizeof(*ifi), nla, __IFLA_MAX - 1, NULL);
  236. if (!nla[IFLA_IFNAME])
  237. return NL_SKIP;
  238. ifname = nla_get_string(nla[IFLA_IFNAME]);
  239. avl_for_each_element(&interfaces, iface, avl) {
  240. if (strcmp(iface->ifname, ifname))
  241. continue;
  242. iface->ifflags = ifi->ifi_flags;
  243. /*
  244. * Assume for link event of the same index, that link changed
  245. * and reload services to enable or disable them based on the
  246. * RUNNING state of the interface.
  247. */
  248. if (iface->ifindex == ifi->ifi_index) {
  249. reload_services(iface);
  250. continue;
  251. }
  252. iface->ifindex = ifi->ifi_index;
  253. event_info.iface = iface;
  254. call_netevent_handler_list(NETEV_IFINDEX_CHANGE, &event_info);
  255. }
  256. return NL_OK;
  257. }
  258. static int handle_rtm_route(struct nlmsghdr *hdr, bool add)
  259. {
  260. struct rtmsg *rtm = nlmsg_data(hdr);
  261. struct nlattr *nla[__RTA_MAX];
  262. struct interface *iface;
  263. struct netevent_handler_info event_info;
  264. int ifindex = 0;
  265. if (!nlmsg_valid_hdr(hdr, sizeof(*rtm)) || rtm->rtm_family != AF_INET6)
  266. return NL_SKIP;
  267. nlmsg_parse(hdr, sizeof(*rtm), nla, __RTA_MAX - 1, NULL);
  268. memset(&event_info, 0, sizeof(event_info));
  269. event_info.rt.dst_len = rtm->rtm_dst_len;
  270. if (nla[RTA_DST])
  271. nla_memcpy(&event_info.rt.dst, nla[RTA_DST],
  272. sizeof(event_info.rt.dst));
  273. if (nla[RTA_OIF])
  274. ifindex = nla_get_u32(nla[RTA_OIF]);
  275. if (nla[RTA_GATEWAY])
  276. nla_memcpy(&event_info.rt.gateway, nla[RTA_GATEWAY],
  277. sizeof(event_info.rt.gateway));
  278. avl_for_each_element(&interfaces, iface, avl) {
  279. if (ifindex && iface->ifindex != ifindex)
  280. continue;
  281. event_info.iface = ifindex ? iface : NULL;
  282. call_netevent_handler_list(add ? NETEV_ROUTE6_ADD : NETEV_ROUTE6_DEL,
  283. &event_info);
  284. }
  285. return NL_OK;
  286. }
  287. static int handle_rtm_addr(struct nlmsghdr *hdr, bool add)
  288. {
  289. struct ifaddrmsg *ifa = nlmsg_data(hdr);
  290. struct nlattr *nla[__IFA_MAX];
  291. struct interface *iface;
  292. struct netevent_handler_info event_info;
  293. char buf[INET6_ADDRSTRLEN];
  294. if (!nlmsg_valid_hdr(hdr, sizeof(*ifa)) ||
  295. (ifa->ifa_family != AF_INET6 &&
  296. ifa->ifa_family != AF_INET))
  297. return NL_SKIP;
  298. memset(&event_info, 0, sizeof(event_info));
  299. nlmsg_parse(hdr, sizeof(*ifa), nla, __IFA_MAX - 1, NULL);
  300. if (ifa->ifa_family == AF_INET6) {
  301. if (!nla[IFA_ADDRESS])
  302. return NL_SKIP;
  303. nla_memcpy(&event_info.addr, nla[IFA_ADDRESS], sizeof(event_info.addr));
  304. if (IN6_IS_ADDR_MULTICAST(&event_info.addr))
  305. return NL_SKIP;
  306. inet_ntop(AF_INET6, &event_info.addr, buf, sizeof(buf));
  307. avl_for_each_element(&interfaces, iface, avl) {
  308. if (iface->ifindex != (int)ifa->ifa_index)
  309. continue;
  310. if (add && IN6_IS_ADDR_LINKLOCAL(&event_info.addr)) {
  311. iface->have_link_local = true;
  312. return NL_SKIP;
  313. }
  314. syslog(LOG_DEBUG, "Netlink %s %s on %s", add ? "newaddr" : "deladdr",
  315. buf, iface->name);
  316. event_info.iface = iface;
  317. call_netevent_handler_list(add ? NETEV_ADDR6_ADD : NETEV_ADDR6_DEL,
  318. &event_info);
  319. }
  320. refresh_iface_addr6(ifa->ifa_index);
  321. } else {
  322. if (!nla[IFA_LOCAL])
  323. return NL_SKIP;
  324. nla_memcpy(&event_info.addr, nla[IFA_LOCAL], sizeof(event_info.addr));
  325. inet_ntop(AF_INET, &event_info.addr, buf, sizeof(buf));
  326. avl_for_each_element(&interfaces, iface, avl) {
  327. if (iface->ifindex != (int)ifa->ifa_index)
  328. continue;
  329. syslog(LOG_DEBUG, "Netlink %s %s on %s", add ? "newaddr" : "deladdr",
  330. buf, iface->name);
  331. event_info.iface = iface;
  332. call_netevent_handler_list(add ? NETEV_ADDR_ADD : NETEV_ADDR_DEL,
  333. &event_info);
  334. }
  335. refresh_iface_addr4(ifa->ifa_index);
  336. }
  337. return NL_OK;
  338. }
  339. static int handle_rtm_neigh(struct nlmsghdr *hdr, bool add)
  340. {
  341. struct ndmsg *ndm = nlmsg_data(hdr);
  342. struct nlattr *nla[__NDA_MAX];
  343. struct interface *iface;
  344. struct netevent_handler_info event_info;
  345. char buf[INET6_ADDRSTRLEN];
  346. if (!nlmsg_valid_hdr(hdr, sizeof(*ndm)) ||
  347. ndm->ndm_family != AF_INET6)
  348. return NL_SKIP;
  349. nlmsg_parse(hdr, sizeof(*ndm), nla, __NDA_MAX - 1, NULL);
  350. if (!nla[NDA_DST])
  351. return NL_SKIP;
  352. memset(&event_info, 0, sizeof(event_info));
  353. nla_memcpy(&event_info.neigh.dst, nla[NDA_DST], sizeof(event_info.neigh.dst));
  354. if (IN6_IS_ADDR_LINKLOCAL(&event_info.neigh.dst) ||
  355. IN6_IS_ADDR_MULTICAST(&event_info.neigh.dst))
  356. return NL_SKIP;
  357. inet_ntop(AF_INET6, &event_info.neigh.dst, buf, sizeof(buf));
  358. avl_for_each_element(&interfaces, iface, avl) {
  359. if (iface->ifindex != ndm->ndm_ifindex)
  360. continue;
  361. syslog(LOG_DEBUG, "Netlink %s %s on %s", true ? "newneigh" : "delneigh",
  362. buf, iface->name);
  363. event_info.iface = iface;
  364. event_info.neigh.state = ndm->ndm_state;
  365. event_info.neigh.flags = ndm->ndm_flags;
  366. call_netevent_handler_list(add ? NETEV_NEIGH6_ADD : NETEV_NEIGH6_DEL,
  367. &event_info);
  368. }
  369. return NL_OK;
  370. }
  371. /* Handler for neighbor cache entries from the kernel. This is our source
  372. * to learn and unlearn hosts on interfaces. */
  373. static int cb_rtnl_valid(struct nl_msg *msg, _unused void *arg)
  374. {
  375. struct nlmsghdr *hdr = nlmsg_hdr(msg);
  376. int ret = NL_SKIP;
  377. bool add = false;
  378. switch (hdr->nlmsg_type) {
  379. case RTM_NEWLINK:
  380. ret = handle_rtm_link(hdr);
  381. break;
  382. case RTM_NEWROUTE:
  383. add = true;
  384. /* fall through */
  385. case RTM_DELROUTE:
  386. ret = handle_rtm_route(hdr, add);
  387. break;
  388. case RTM_NEWADDR:
  389. add = true;
  390. /* fall through */
  391. case RTM_DELADDR:
  392. ret = handle_rtm_addr(hdr, add);
  393. break;
  394. case RTM_NEWNEIGH:
  395. add = true;
  396. /* fall through */
  397. case RTM_DELNEIGH:
  398. ret = handle_rtm_neigh(hdr, add);
  399. break;
  400. default:
  401. break;
  402. }
  403. return ret;
  404. }
  405. static void catch_rtnl_err(struct odhcpd_event *e, int error)
  406. {
  407. struct event_socket *ev_sock = container_of(e, struct event_socket, ev);
  408. if (error != ENOBUFS)
  409. goto err;
  410. /* Double netlink event buffer size */
  411. ev_sock->sock_bufsize *= 2;
  412. if (nl_socket_set_buffer_size(ev_sock->sock, ev_sock->sock_bufsize, 0))
  413. goto err;
  414. netlink_dump_addr_table(true);
  415. return;
  416. err:
  417. odhcpd_deregister(e);
  418. }
  419. static struct nl_sock *create_socket(int protocol)
  420. {
  421. struct nl_sock *nl_sock;
  422. nl_sock = nl_socket_alloc();
  423. if (!nl_sock)
  424. goto err;
  425. if (nl_connect(nl_sock, protocol) < 0)
  426. goto err;
  427. return nl_sock;
  428. err:
  429. if (nl_sock)
  430. nl_socket_free(nl_sock);
  431. return NULL;
  432. }
  433. struct addr_info {
  434. int ifindex;
  435. int af;
  436. struct odhcpd_ipaddr **addrs;
  437. int pending;
  438. ssize_t ret;
  439. };
  440. static int cb_addr_valid(struct nl_msg *msg, void *arg)
  441. {
  442. struct addr_info *ctxt = (struct addr_info *)arg;
  443. struct odhcpd_ipaddr *addrs = *(ctxt->addrs);
  444. struct nlmsghdr *hdr = nlmsg_hdr(msg);
  445. struct ifaddrmsg *ifa;
  446. struct nlattr *nla[__IFA_MAX], *nla_addr = NULL;
  447. if (hdr->nlmsg_type != RTM_NEWADDR)
  448. return NL_SKIP;
  449. ifa = NLMSG_DATA(hdr);
  450. if (ifa->ifa_scope != RT_SCOPE_UNIVERSE ||
  451. (ctxt->af != ifa->ifa_family) ||
  452. (ctxt->ifindex && ifa->ifa_index != (unsigned)ctxt->ifindex))
  453. return NL_SKIP;
  454. nlmsg_parse(hdr, sizeof(*ifa), nla, __IFA_MAX - 1, NULL);
  455. switch (ifa->ifa_family) {
  456. case AF_INET6:
  457. if (nla[IFA_ADDRESS])
  458. nla_addr = nla[IFA_ADDRESS];
  459. break;
  460. case AF_INET:
  461. if (nla[IFA_LOCAL])
  462. nla_addr = nla[IFA_LOCAL];
  463. break;
  464. default:
  465. break;
  466. }
  467. if (!nla_addr)
  468. return NL_SKIP;
  469. addrs = realloc(addrs, sizeof(*addrs)*(ctxt->ret + 1));
  470. if (!addrs)
  471. return NL_SKIP;
  472. memset(&addrs[ctxt->ret], 0, sizeof(addrs[ctxt->ret]));
  473. addrs[ctxt->ret].prefix = ifa->ifa_prefixlen;
  474. nla_memcpy(&addrs[ctxt->ret].addr, nla_addr,
  475. sizeof(addrs[ctxt->ret].addr));
  476. if (nla[IFA_BROADCAST])
  477. nla_memcpy(&addrs[ctxt->ret].broadcast, nla[IFA_BROADCAST],
  478. sizeof(addrs[ctxt->ret].broadcast));
  479. if (nla[IFA_CACHEINFO]) {
  480. struct ifa_cacheinfo *ifc = nla_data(nla[IFA_CACHEINFO]);
  481. addrs[ctxt->ret].preferred_lt = ifc->ifa_prefered;
  482. addrs[ctxt->ret].valid_lt = ifc->ifa_valid;
  483. }
  484. if (ifa->ifa_flags & IFA_F_DEPRECATED)
  485. addrs[ctxt->ret].preferred_lt = 0;
  486. if (ifa->ifa_family == AF_INET6 &&
  487. ifa->ifa_flags & IFA_F_TENTATIVE)
  488. addrs[ctxt->ret].tentative = true;
  489. ctxt->ret++;
  490. *(ctxt->addrs) = addrs;
  491. return NL_OK;
  492. }
  493. static int cb_addr_finish(_unused struct nl_msg *msg, void *arg)
  494. {
  495. struct addr_info *ctxt = (struct addr_info *)arg;
  496. ctxt->pending = 0;
  497. return NL_STOP;
  498. }
  499. static int cb_addr_error(_unused struct sockaddr_nl *nla, struct nlmsgerr *err,
  500. void *arg)
  501. {
  502. struct addr_info *ctxt = (struct addr_info *)arg;
  503. ctxt->pending = 0;
  504. ctxt->ret = err->error;
  505. return NL_STOP;
  506. }
  507. static int prefix_cmp(const void *va, const void *vb)
  508. {
  509. const struct odhcpd_ipaddr *a = va, *b = vb;
  510. int ret = 0;
  511. if (a->prefix == b->prefix) {
  512. ret = (ntohl(a->addr.in.s_addr) < ntohl(b->addr.in.s_addr)) ? 1 :
  513. (ntohl(a->addr.in.s_addr) > ntohl(b->addr.in.s_addr)) ? -1 : 0;
  514. } else
  515. ret = a->prefix < b->prefix ? 1 : -1;
  516. return ret;
  517. }
  518. /* compare IPv6 prefixes */
  519. static int prefix6_cmp(const void *va, const void *vb)
  520. {
  521. const struct odhcpd_ipaddr *a = va, *b = vb;
  522. uint32_t a_pref_lt = IN6_IS_ADDR_ULA(&a->addr.in6) ? 1 : a->preferred_lt;
  523. uint32_t b_pref_lt = IN6_IS_ADDR_ULA(&b->addr.in6) ? 1 : b->preferred_lt;
  524. return (a_pref_lt < b_pref_lt) ? 1 : (a_pref_lt > b_pref_lt) ? -1 : 0;
  525. }
  526. /* Detect an IPV6-address currently assigned to the given interface */
  527. ssize_t netlink_get_interface_addrs(int ifindex, bool v6, struct odhcpd_ipaddr **addrs)
  528. {
  529. struct nl_msg *msg;
  530. struct ifaddrmsg ifa = {
  531. .ifa_family = v6? AF_INET6: AF_INET,
  532. .ifa_prefixlen = 0,
  533. .ifa_flags = 0,
  534. .ifa_scope = 0,
  535. .ifa_index = ifindex, };
  536. struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
  537. struct addr_info ctxt = {
  538. .ifindex = ifindex,
  539. .af = v6? AF_INET6: AF_INET,
  540. .addrs = addrs,
  541. .ret = 0,
  542. .pending = 1,
  543. };
  544. if (!cb) {
  545. ctxt.ret = -1;
  546. goto out;
  547. }
  548. msg = nlmsg_alloc_simple(RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP);
  549. if (!msg) {
  550. ctxt.ret = - 1;
  551. goto out;
  552. }
  553. nlmsg_append(msg, &ifa, sizeof(ifa), 0);
  554. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_addr_valid, &ctxt);
  555. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_addr_finish, &ctxt);
  556. nl_cb_err(cb, NL_CB_CUSTOM, cb_addr_error, &ctxt);
  557. ctxt.ret = nl_send_auto_complete(rtnl_socket, msg);
  558. if (ctxt.ret < 0)
  559. goto free;
  560. ctxt.ret = 0;
  561. while (ctxt.pending > 0)
  562. nl_recvmsgs(rtnl_socket, cb);
  563. if (ctxt.ret <= 0)
  564. goto free;
  565. time_t now = odhcpd_time();
  566. struct odhcpd_ipaddr *addr = *addrs;
  567. qsort(addr, ctxt.ret, sizeof(*addr), v6 ? prefix6_cmp : prefix_cmp);
  568. for (ssize_t i = 0; i < ctxt.ret; ++i) {
  569. if (addr[i].preferred_lt < UINT32_MAX - now)
  570. addr[i].preferred_lt += now;
  571. if (addr[i].valid_lt < UINT32_MAX - now)
  572. addr[i].valid_lt += now;
  573. }
  574. free:
  575. nlmsg_free(msg);
  576. out:
  577. nl_cb_put(cb);
  578. return ctxt.ret;
  579. }
  580. static int cb_linklocal_valid(struct nl_msg *msg, void *arg)
  581. {
  582. struct addr_info *ctxt = (struct addr_info *)arg;
  583. struct odhcpd_ipaddr *addrs = *(ctxt->addrs);
  584. struct nlmsghdr *hdr = nlmsg_hdr(msg);
  585. struct ifaddrmsg *ifa;
  586. struct nlattr *nla[__IFA_MAX], *nla_addr = NULL;
  587. struct in6_addr addr;
  588. if (hdr->nlmsg_type != RTM_NEWADDR)
  589. return NL_SKIP;
  590. ifa = NLMSG_DATA(hdr);
  591. if (ifa->ifa_scope != RT_SCOPE_LINK ||
  592. (ctxt->af != ifa->ifa_family) ||
  593. (ctxt->ifindex && ifa->ifa_index != (unsigned)ctxt->ifindex))
  594. return NL_SKIP;
  595. nlmsg_parse(hdr, sizeof(*ifa), nla, __IFA_MAX - 1, NULL);
  596. switch (ifa->ifa_family) {
  597. case AF_INET6:
  598. if (nla[IFA_ADDRESS])
  599. nla_addr = nla[IFA_ADDRESS];
  600. break;
  601. default:
  602. break;
  603. }
  604. if (!nla_addr)
  605. return NL_SKIP;
  606. nla_memcpy(&addr, nla_addr, sizeof(addr));
  607. if (!IN6_IS_ADDR_LINKLOCAL(&addr))
  608. return NL_SKIP;
  609. addrs = realloc(addrs, sizeof(*addrs)*(ctxt->ret + 1));
  610. if (!addrs)
  611. return NL_SKIP;
  612. memset(&addrs[ctxt->ret], 0, sizeof(addrs[ctxt->ret]));
  613. addrs = realloc(addrs, sizeof(*addrs)*(ctxt->ret + 1));
  614. if (!addrs)
  615. return NL_SKIP;
  616. memcpy(&addrs[ctxt->ret].addr, &addr, sizeof(addrs[ctxt->ret].addr));
  617. if (ifa->ifa_flags & IFA_F_TENTATIVE)
  618. addrs[ctxt->ret].tentative = true;
  619. ctxt->ret++;
  620. *(ctxt->addrs) = addrs;
  621. return NL_OK;
  622. }
  623. static int cb_linklocal_finish(_unused struct nl_msg *msg, void *arg)
  624. {
  625. struct addr_info *ctxt = (struct addr_info *)arg;
  626. ctxt->pending = 0;
  627. return NL_STOP;
  628. }
  629. static int cb_linklocal_error(_unused struct sockaddr_nl *nla, struct nlmsgerr *err,
  630. void *arg)
  631. {
  632. struct addr_info *ctxt = (struct addr_info *)arg;
  633. ctxt->pending = 0;
  634. ctxt->ret = err->error;
  635. return NL_STOP;
  636. }
  637. /* Detect a link local IPV6-address currently assigned to the given interface */
  638. ssize_t netlink_get_interface_linklocal(int ifindex, struct odhcpd_ipaddr **addrs)
  639. {
  640. struct nl_msg *msg;
  641. struct ifaddrmsg ifa = {
  642. .ifa_family = AF_INET6,
  643. .ifa_prefixlen = 0,
  644. .ifa_flags = 0,
  645. .ifa_scope = 0,
  646. .ifa_index = ifindex, };
  647. struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
  648. struct addr_info ctxt = {
  649. .ifindex = ifindex,
  650. .af = AF_INET6,
  651. .addrs = addrs,
  652. .ret = 0,
  653. .pending = 1,
  654. };
  655. if (!cb) {
  656. ctxt.ret = -1;
  657. goto out;
  658. }
  659. msg = nlmsg_alloc_simple(RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP);
  660. if (!msg) {
  661. ctxt.ret = - 1;
  662. goto out;
  663. }
  664. nlmsg_append(msg, &ifa, sizeof(ifa), 0);
  665. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_linklocal_valid, &ctxt);
  666. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_linklocal_finish, &ctxt);
  667. nl_cb_err(cb, NL_CB_CUSTOM, cb_linklocal_error, &ctxt);
  668. ctxt.ret = nl_send_auto_complete(rtnl_socket, msg);
  669. if (ctxt.ret < 0)
  670. goto free;
  671. ctxt.ret = 0;
  672. while (ctxt.pending > 0)
  673. nl_recvmsgs(rtnl_socket, cb);
  674. if (ctxt.ret <= 0)
  675. goto free;
  676. free:
  677. nlmsg_free(msg);
  678. out:
  679. nl_cb_put(cb);
  680. return ctxt.ret;
  681. }
  682. struct neigh_info {
  683. int ifindex;
  684. int pending;
  685. const struct in6_addr *addr;
  686. int ret;
  687. };
  688. static int cb_proxy_neigh_valid(struct nl_msg *msg, void *arg)
  689. {
  690. struct neigh_info *ctxt = (struct neigh_info *)arg;
  691. struct nlmsghdr *hdr = nlmsg_hdr(msg);
  692. struct ndmsg *ndm;
  693. struct nlattr *nla_dst;
  694. if (hdr->nlmsg_type != RTM_NEWNEIGH)
  695. return NL_SKIP;
  696. ndm = NLMSG_DATA(hdr);
  697. if (ndm->ndm_family != AF_INET6 ||
  698. (ctxt->ifindex && ndm->ndm_ifindex != ctxt->ifindex))
  699. return NL_SKIP;
  700. if (!(ndm->ndm_flags & NTF_PROXY))
  701. return NL_SKIP;
  702. nla_dst = nlmsg_find_attr(hdr, sizeof(*ndm), NDA_DST);
  703. if (!nla_dst)
  704. return NL_SKIP;
  705. if (nla_memcmp(nla_dst,ctxt->addr, 16) == 0)
  706. ctxt->ret = 1;
  707. return NL_OK;
  708. }
  709. static int cb_proxy_neigh_finish(_unused struct nl_msg *msg, void *arg)
  710. {
  711. struct neigh_info *ctxt = (struct neigh_info *)arg;
  712. ctxt->pending = 0;
  713. return NL_STOP;
  714. }
  715. static int cb_proxy_neigh_error(_unused struct sockaddr_nl *nla, struct nlmsgerr *err,
  716. void *arg)
  717. {
  718. struct neigh_info *ctxt = (struct neigh_info *)arg;
  719. ctxt->pending = 0;
  720. ctxt->ret = err->error;
  721. return NL_STOP;
  722. }
  723. /* Detect an IPV6-address proxy neighbor for the given interface */
  724. int netlink_get_interface_proxy_neigh(int ifindex, const struct in6_addr *addr)
  725. {
  726. struct nl_msg *msg;
  727. struct ndmsg ndm = {
  728. .ndm_family = AF_INET6,
  729. .ndm_flags = NTF_PROXY,
  730. .ndm_ifindex = ifindex,
  731. };
  732. struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
  733. struct neigh_info ctxt = {
  734. .ifindex = ifindex,
  735. .addr = addr,
  736. .ret = 0,
  737. .pending = 1,
  738. };
  739. if (!cb) {
  740. ctxt.ret = -1;
  741. goto out;
  742. }
  743. msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_MATCH);
  744. if (!msg) {
  745. ctxt.ret = -1;
  746. goto out;
  747. }
  748. nlmsg_append(msg, &ndm, sizeof(ndm), 0);
  749. nla_put(msg, NDA_DST, sizeof(*addr), addr);
  750. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_proxy_neigh_valid, &ctxt);
  751. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_proxy_neigh_finish, &ctxt);
  752. nl_cb_err(cb, NL_CB_CUSTOM, cb_proxy_neigh_error, &ctxt);
  753. ctxt.ret = nl_send_auto_complete(rtnl_socket, msg);
  754. if (ctxt.ret < 0)
  755. goto free;
  756. while (ctxt.pending > 0)
  757. nl_recvmsgs(rtnl_socket, cb);
  758. free:
  759. nlmsg_free(msg);
  760. out:
  761. nl_cb_put(cb);
  762. return ctxt.ret;
  763. }
  764. int netlink_setup_route(const struct in6_addr *addr, const int prefixlen,
  765. const int ifindex, const struct in6_addr *gw,
  766. const uint32_t metric, const bool add)
  767. {
  768. struct nl_msg *msg;
  769. struct rtmsg rtm = {
  770. .rtm_family = AF_INET6,
  771. .rtm_dst_len = prefixlen,
  772. .rtm_src_len = 0,
  773. .rtm_table = RT_TABLE_MAIN,
  774. .rtm_protocol = (add ? RTPROT_STATIC : RTPROT_UNSPEC),
  775. .rtm_scope = (add ? (gw ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK) : RT_SCOPE_NOWHERE),
  776. .rtm_type = (add ? RTN_UNICAST : RTN_UNSPEC),
  777. };
  778. int ret = 0;
  779. msg = nlmsg_alloc_simple(add ? RTM_NEWROUTE : RTM_DELROUTE,
  780. add ? NLM_F_CREATE | NLM_F_REPLACE : 0);
  781. if (!msg)
  782. return -1;
  783. nlmsg_append(msg, &rtm, sizeof(rtm), 0);
  784. nla_put(msg, RTA_DST, sizeof(*addr), addr);
  785. nla_put_u32(msg, RTA_OIF, ifindex);
  786. nla_put_u32(msg, RTA_PRIORITY, metric);
  787. if (gw)
  788. nla_put(msg, RTA_GATEWAY, sizeof(*gw), gw);
  789. ret = nl_send_auto_complete(rtnl_socket, msg);
  790. nlmsg_free(msg);
  791. if (ret < 0)
  792. return ret;
  793. return nl_wait_for_ack(rtnl_socket);
  794. }
  795. int netlink_setup_proxy_neigh(const struct in6_addr *addr,
  796. const int ifindex, const bool add)
  797. {
  798. struct nl_msg *msg;
  799. struct ndmsg ndm = {
  800. .ndm_family = AF_INET6,
  801. .ndm_flags = NTF_PROXY,
  802. .ndm_ifindex = ifindex,
  803. };
  804. int ret = 0, flags = NLM_F_REQUEST;
  805. if (add)
  806. flags |= NLM_F_REPLACE | NLM_F_CREATE;
  807. msg = nlmsg_alloc_simple(add ? RTM_NEWNEIGH : RTM_DELNEIGH, flags);
  808. if (!msg)
  809. return -1;
  810. nlmsg_append(msg, &ndm, sizeof(ndm), 0);
  811. nla_put(msg, NDA_DST, sizeof(*addr), addr);
  812. ret = nl_send_auto_complete(rtnl_socket, msg);
  813. nlmsg_free(msg);
  814. if (ret < 0)
  815. return ret;
  816. return nl_wait_for_ack(rtnl_socket);
  817. }
  818. int netlink_setup_addr(struct odhcpd_ipaddr *addr,
  819. const int ifindex, const bool v6, const bool add)
  820. {
  821. struct nl_msg *msg;
  822. struct ifaddrmsg ifa = {
  823. .ifa_family = v6 ? AF_INET6 : AF_INET,
  824. .ifa_prefixlen = addr->prefix,
  825. .ifa_flags = 0,
  826. .ifa_scope = 0,
  827. .ifa_index = ifindex, };
  828. int ret = 0, flags = NLM_F_REQUEST;
  829. if (add)
  830. flags |= NLM_F_REPLACE | NLM_F_CREATE;
  831. msg = nlmsg_alloc_simple(add ? RTM_NEWADDR : RTM_DELADDR, 0);
  832. if (!msg)
  833. return -1;
  834. nlmsg_append(msg, &ifa, sizeof(ifa), flags);
  835. nla_put(msg, IFA_LOCAL, v6 ? 16 : 4, &addr->addr);
  836. if (v6) {
  837. struct ifa_cacheinfo cinfo = { .ifa_prefered = 0xffffffffU,
  838. .ifa_valid = 0xffffffffU,
  839. .cstamp = 0,
  840. .tstamp = 0 };
  841. time_t now = odhcpd_time();
  842. if (addr->preferred_lt) {
  843. int64_t preferred_lt = addr->preferred_lt - now;
  844. if (preferred_lt < 0)
  845. preferred_lt = 0;
  846. else if (preferred_lt > UINT32_MAX)
  847. preferred_lt = UINT32_MAX;
  848. cinfo.ifa_prefered = preferred_lt;
  849. }
  850. if (addr->valid_lt) {
  851. int64_t valid_lt = addr->valid_lt - now;
  852. if (valid_lt <= 0) {
  853. nlmsg_free(msg);
  854. return -1;
  855. }
  856. else if (valid_lt > UINT32_MAX)
  857. valid_lt = UINT32_MAX;
  858. cinfo.ifa_valid = valid_lt;
  859. }
  860. nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo);
  861. nla_put_u32(msg, IFA_FLAGS, IFA_F_NOPREFIXROUTE);
  862. } else {
  863. if (addr->broadcast.s_addr)
  864. nla_put_u32(msg, IFA_BROADCAST, addr->broadcast.s_addr);
  865. }
  866. ret = nl_send_auto_complete(rtnl_socket, msg);
  867. nlmsg_free(msg);
  868. if (ret < 0)
  869. return ret;
  870. return nl_wait_for_ack(rtnl_socket);
  871. }
  872. void netlink_dump_neigh_table(const bool proxy)
  873. {
  874. struct nl_msg *msg;
  875. struct ndmsg ndm = {
  876. .ndm_family = AF_INET6,
  877. .ndm_flags = proxy ? NTF_PROXY : 0,
  878. };
  879. msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP);
  880. if (!msg)
  881. return;
  882. nlmsg_append(msg, &ndm, sizeof(ndm), 0);
  883. nl_send_auto_complete(rtnl_event.sock, msg);
  884. nlmsg_free(msg);
  885. }
  886. void netlink_dump_addr_table(const bool v6)
  887. {
  888. struct nl_msg *msg;
  889. struct ifaddrmsg ifa = {
  890. .ifa_family = v6 ? AF_INET6 : AF_INET,
  891. };
  892. msg = nlmsg_alloc_simple(RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP);
  893. if (!msg)
  894. return;
  895. nlmsg_append(msg, &ifa, sizeof(ifa), 0);
  896. nl_send_auto_complete(rtnl_event.sock, msg);
  897. nlmsg_free(msg);
  898. }