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