2 * Copyright (C) 2017 Hans Dedecker <dedeckeh@gmail.com>
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.
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.
19 #include <linux/netlink.h>
20 #include <linux/if_addr.h>
21 #include <linux/neighbour.h>
22 #include <linux/rtnetlink.h>
24 #include <netlink/msg.h>
25 #include <netlink/socket.h>
26 #include <netlink/attr.h>
28 #include <arpa/inet.h>
29 #include <libubox/list.h>
34 struct odhcpd_event ev
;
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
);
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
= {
48 .uloop
= {.fd
= - 1, },
50 .handle_error
= catch_rtnl_err
,
51 .recv_msgs
= handle_rtnl_event
,
54 .sock_bufsize
= 133120,
57 int netlink_init(void)
59 rtnl_socket
= create_socket(NETLINK_ROUTE
);
61 syslog(LOG_ERR
, "Unable to open nl socket: %m");
65 rtnl_event
.sock
= create_socket(NETLINK_ROUTE
);
66 if (!rtnl_event
.sock
) {
67 syslog(LOG_ERR
, "Unable to open nl event socket: %m");
71 rtnl_event
.ev
.uloop
.fd
= nl_socket_get_fd(rtnl_event
.sock
);
73 if (nl_socket_set_buffer_size(rtnl_event
.sock
, rtnl_event
.sock_bufsize
, 0))
76 nl_socket_disable_seq_check(rtnl_event
.sock
);
78 nl_socket_modify_cb(rtnl_event
.sock
, NL_CB_VALID
, NL_CB_CUSTOM
,
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))
87 odhcpd_register(&rtnl_event
.ev
);
93 nl_socket_free(rtnl_socket
);
97 if (rtnl_event
.sock
) {
98 nl_socket_free(rtnl_event
.sock
);
99 rtnl_event
.sock
= NULL
;
100 rtnl_event
.ev
.uloop
.fd
= -1;
107 int netlink_add_netevent_handler(struct netevent_handler
*handler
)
112 list_add(&handler
->head
, &netevent_handler_list
);
117 static void call_netevent_handler_list(unsigned long event
, struct netevent_handler_info
*info
)
119 struct netevent_handler
*handler
;
121 list_for_each_entry(handler
, &netevent_handler_list
, head
)
122 handler
->cb(event
, info
);
125 static void handle_rtnl_event(struct odhcpd_event
*e
)
127 struct event_socket
*ev_sock
= container_of(e
, struct event_socket
, ev
);
129 nl_recvmsgs_default(ev_sock
->sock
);
132 static void refresh_iface_addr4(int ifindex
)
134 struct odhcpd_ipaddr
*addr
= NULL
;
135 struct interface
*iface
;
136 ssize_t len
= netlink_get_interface_addrs(ifindex
, false, &addr
);
142 avl_for_each_element(&interfaces
, iface
, avl
) {
143 struct netevent_handler_info event_info
;
145 if (iface
->ifindex
!= ifindex
)
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
;
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
)
162 iface
->addr4_len
= len
;
165 call_netevent_handler_list(NETEV_ADDRLIST_CHANGE
, &event_info
);
167 free(event_info
.addrs_old
.addrs
);
172 addr
= malloc(len
* sizeof(*addr
));
176 memcpy(addr
, iface
->addr4
, len
* sizeof(*addr
));
182 static void refresh_iface_addr6(int ifindex
)
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();
193 avl_for_each_element(&interfaces
, iface
, avl
) {
194 struct netevent_handler_info event_info
;
196 if (iface
->ifindex
!= ifindex
)
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
;
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
)
216 * Keep track on removed prefixes, so we could advertise them as invalid
217 * for at least a couple of times.
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].
230 for (size_t i
= 0; i
< iface
->addr6_len
; ++i
) {
233 if (iface
->addr6
[i
].valid
<= (uint32_t)now
)
236 for (ssize_t j
= 0; removed
&& j
< len
; ++j
) {
237 size_t plen
= min(addr
[j
].prefix
, iface
->addr6
[i
].prefix
);
239 if (odhcpd_bmemcmp(&addr
[j
].addr
.in6
, &iface
->addr6
[i
].addr
.in6
, plen
) == 0)
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
);
246 if (odhcpd_bmemcmp(&iface
->invalid_addr6
[j
].addr
.in6
, &iface
->addr6
[i
].addr
.in6
, plen
) == 0)
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));
255 if (!new_invalid_addr6
)
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
;
263 if (iface
->invalid_addr6
[pos
].prefix
< 64)
264 iface
->invalid_addr6
[pos
].prefix
= 64;
271 iface
->addr6_len
= len
;
274 call_netevent_handler_list(NETEV_ADDR6LIST_CHANGE
, &event_info
);
276 free(event_info
.addrs_old
.addrs
);
281 addr
= malloc(len
* sizeof(*addr
));
285 memcpy(addr
, iface
->addr6
, len
* sizeof(*addr
));
291 static int handle_rtm_link(struct nlmsghdr
*hdr
)
293 struct ifinfomsg
*ifi
= nlmsg_data(hdr
);
294 struct nlattr
*nla
[__IFLA_MAX
];
295 struct interface
*iface
;
296 struct netevent_handler_info event_info
;
299 memset(&event_info
, 0, sizeof(event_info
));
301 if (!nlmsg_valid_hdr(hdr
, sizeof(*ifi
)) || ifi
->ifi_family
!= AF_UNSPEC
)
304 nlmsg_parse(hdr
, sizeof(*ifi
), nla
, __IFLA_MAX
- 1, NULL
);
305 if (!nla
[IFLA_IFNAME
])
308 ifname
= nla_get_string(nla
[IFLA_IFNAME
]);
310 avl_for_each_element(&interfaces
, iface
, avl
) {
311 if (strcmp(iface
->ifname
, ifname
) || iface
->ifindex
== ifi
->ifi_index
)
314 iface
->ifindex
= ifi
->ifi_index
;
315 event_info
.iface
= iface
;
316 call_netevent_handler_list(NETEV_IFINDEX_CHANGE
, &event_info
);
322 static int handle_rtm_route(struct nlmsghdr
*hdr
, bool add
)
324 struct rtmsg
*rtm
= nlmsg_data(hdr
);
325 struct nlattr
*nla
[__RTA_MAX
];
326 struct interface
*iface
;
327 struct netevent_handler_info event_info
;
330 if (!nlmsg_valid_hdr(hdr
, sizeof(*rtm
)) || rtm
->rtm_family
!= AF_INET6
)
333 nlmsg_parse(hdr
, sizeof(*rtm
), nla
, __RTA_MAX
- 1, NULL
);
335 memset(&event_info
, 0, sizeof(event_info
));
336 event_info
.rt
.dst_len
= rtm
->rtm_dst_len
;
339 nla_memcpy(&event_info
.rt
.dst
, nla
[RTA_DST
],
340 sizeof(event_info
.rt
.dst
));
343 ifindex
= nla_get_u32(nla
[RTA_OIF
]);
345 if (nla
[RTA_GATEWAY
])
346 nla_memcpy(&event_info
.rt
.gateway
, nla
[RTA_GATEWAY
],
347 sizeof(event_info
.rt
.gateway
));
349 avl_for_each_element(&interfaces
, iface
, avl
) {
350 if (ifindex
&& iface
->ifindex
!= ifindex
)
353 event_info
.iface
= ifindex
? iface
: NULL
;
354 call_netevent_handler_list(add
? NETEV_ROUTE6_ADD
: NETEV_ROUTE6_DEL
,
361 static int handle_rtm_addr(struct nlmsghdr
*hdr
, bool add
)
363 struct ifaddrmsg
*ifa
= nlmsg_data(hdr
);
364 struct nlattr
*nla
[__IFA_MAX
];
365 struct interface
*iface
;
366 struct netevent_handler_info event_info
;
367 char buf
[INET6_ADDRSTRLEN
];
369 if (!nlmsg_valid_hdr(hdr
, sizeof(*ifa
)) ||
370 (ifa
->ifa_family
!= AF_INET6
&&
371 ifa
->ifa_family
!= AF_INET
))
374 memset(&event_info
, 0, sizeof(event_info
));
376 nlmsg_parse(hdr
, sizeof(*ifa
), nla
, __IFA_MAX
- 1, NULL
);
378 if (ifa
->ifa_family
== AF_INET6
) {
379 if (!nla
[IFA_ADDRESS
])
382 nla_memcpy(&event_info
.addr
, nla
[IFA_ADDRESS
], sizeof(event_info
.addr
));
384 if (IN6_IS_ADDR_LINKLOCAL(&event_info
.addr
) || IN6_IS_ADDR_MULTICAST(&event_info
.addr
))
387 inet_ntop(AF_INET6
, &event_info
.addr
, buf
, sizeof(buf
));
389 avl_for_each_element(&interfaces
, iface
, avl
) {
390 if (iface
->ifindex
!= (int)ifa
->ifa_index
)
393 syslog(LOG_DEBUG
, "Netlink %s %s on %s", add
? "newaddr" : "deladdr",
396 event_info
.iface
= iface
;
397 call_netevent_handler_list(add
? NETEV_ADDR6_ADD
: NETEV_ADDR6_DEL
,
401 refresh_iface_addr6(ifa
->ifa_index
);
406 nla_memcpy(&event_info
.addr
, nla
[IFA_LOCAL
], sizeof(event_info
.addr
));
408 inet_ntop(AF_INET
, &event_info
.addr
, buf
, sizeof(buf
));
410 avl_for_each_element(&interfaces
, iface
, avl
) {
411 if (iface
->ifindex
!= (int)ifa
->ifa_index
)
414 syslog(LOG_DEBUG
, "Netlink %s %s on %s", add
? "newaddr" : "deladdr",
417 event_info
.iface
= iface
;
418 call_netevent_handler_list(add
? NETEV_ADDR_ADD
: NETEV_ADDR_DEL
,
422 refresh_iface_addr4(ifa
->ifa_index
);
428 static int handle_rtm_neigh(struct nlmsghdr
*hdr
, bool add
)
430 struct ndmsg
*ndm
= nlmsg_data(hdr
);
431 struct nlattr
*nla
[__NDA_MAX
];
432 struct interface
*iface
;
433 struct netevent_handler_info event_info
;
434 char buf
[INET6_ADDRSTRLEN
];
436 if (!nlmsg_valid_hdr(hdr
, sizeof(*ndm
)) ||
437 ndm
->ndm_family
!= AF_INET6
)
440 nlmsg_parse(hdr
, sizeof(*ndm
), nla
, __NDA_MAX
- 1, NULL
);
444 memset(&event_info
, 0, sizeof(event_info
));
446 nla_memcpy(&event_info
.neigh
.dst
, nla
[NDA_DST
], sizeof(event_info
.neigh
.dst
));
448 if (IN6_IS_ADDR_LINKLOCAL(&event_info
.neigh
.dst
) ||
449 IN6_IS_ADDR_MULTICAST(&event_info
.neigh
.dst
))
452 inet_ntop(AF_INET6
, &event_info
.neigh
.dst
, buf
, sizeof(buf
));
454 avl_for_each_element(&interfaces
, iface
, avl
) {
455 if (iface
->ifindex
!= ndm
->ndm_ifindex
)
458 syslog(LOG_DEBUG
, "Netlink %s %s on %s", true ? "newneigh" : "delneigh",
461 event_info
.iface
= iface
;
462 event_info
.neigh
.state
= ndm
->ndm_state
;
463 event_info
.neigh
.flags
= ndm
->ndm_flags
;
465 call_netevent_handler_list(add
? NETEV_NEIGH6_ADD
: NETEV_NEIGH6_DEL
,
472 /* Handler for neighbor cache entries from the kernel. This is our source
473 * to learn and unlearn hosts on interfaces. */
474 static int cb_rtnl_valid(struct nl_msg
*msg
, _unused
void *arg
)
476 struct nlmsghdr
*hdr
= nlmsg_hdr(msg
);
480 switch (hdr
->nlmsg_type
) {
482 ret
= handle_rtm_link(hdr
);
489 ret
= handle_rtm_route(hdr
, add
);
496 ret
= handle_rtm_addr(hdr
, add
);
503 ret
= handle_rtm_neigh(hdr
, add
);
513 static void catch_rtnl_err(struct odhcpd_event
*e
, int error
)
515 struct event_socket
*ev_sock
= container_of(e
, struct event_socket
, ev
);
517 if (error
!= ENOBUFS
)
520 /* Double netlink event buffer size */
521 ev_sock
->sock_bufsize
*= 2;
523 if (nl_socket_set_buffer_size(ev_sock
->sock
, ev_sock
->sock_bufsize
, 0))
526 netlink_dump_addr_table(true);
530 odhcpd_deregister(e
);
533 static struct nl_sock
*create_socket(int protocol
)
535 struct nl_sock
*nl_sock
;
537 nl_sock
= nl_socket_alloc();
541 if (nl_connect(nl_sock
, protocol
) < 0)
548 nl_socket_free(nl_sock
);
557 struct odhcpd_ipaddr
**addrs
;
563 static int cb_addr_valid(struct nl_msg
*msg
, void *arg
)
565 struct addr_info
*ctxt
= (struct addr_info
*)arg
;
566 struct odhcpd_ipaddr
*addrs
= *(ctxt
->addrs
);
567 struct nlmsghdr
*hdr
= nlmsg_hdr(msg
);
568 struct ifaddrmsg
*ifa
;
569 struct nlattr
*nla
[__IFA_MAX
], *nla_addr
= NULL
;
571 if (hdr
->nlmsg_type
!= RTM_NEWADDR
)
574 ifa
= NLMSG_DATA(hdr
);
575 if (ifa
->ifa_scope
!= RT_SCOPE_UNIVERSE
||
576 (ctxt
->af
!= ifa
->ifa_family
) ||
577 (ctxt
->ifindex
&& ifa
->ifa_index
!= (unsigned)ctxt
->ifindex
))
580 nlmsg_parse(hdr
, sizeof(*ifa
), nla
, __IFA_MAX
- 1, NULL
);
582 switch (ifa
->ifa_family
) {
584 if (nla
[IFA_ADDRESS
])
585 nla_addr
= nla
[IFA_ADDRESS
];
590 nla_addr
= nla
[IFA_LOCAL
];
599 addrs
= realloc(addrs
, sizeof(*addrs
)*(ctxt
->ret
+ 1));
603 memset(&addrs
[ctxt
->ret
], 0, sizeof(addrs
[ctxt
->ret
]));
604 addrs
[ctxt
->ret
].prefix
= ifa
->ifa_prefixlen
;
606 nla_memcpy(&addrs
[ctxt
->ret
].addr
, nla_addr
,
607 sizeof(addrs
[ctxt
->ret
].addr
));
609 if (nla
[IFA_BROADCAST
])
610 nla_memcpy(&addrs
[ctxt
->ret
].broadcast
, nla
[IFA_BROADCAST
],
611 sizeof(addrs
[ctxt
->ret
].broadcast
));
613 if (nla
[IFA_CACHEINFO
]) {
614 struct ifa_cacheinfo
*ifc
= nla_data(nla
[IFA_CACHEINFO
]);
616 addrs
[ctxt
->ret
].preferred
= ifc
->ifa_prefered
;
617 addrs
[ctxt
->ret
].valid
= ifc
->ifa_valid
;
620 if (ifa
->ifa_flags
& IFA_F_DEPRECATED
)
621 addrs
[ctxt
->ret
].preferred
= 0;
624 *(ctxt
->addrs
) = addrs
;
630 static int cb_addr_finish(_unused
struct nl_msg
*msg
, void *arg
)
632 struct addr_info
*ctxt
= (struct addr_info
*)arg
;
640 static int cb_addr_error(_unused
struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
643 struct addr_info
*ctxt
= (struct addr_info
*)arg
;
646 ctxt
->ret
= err
->error
;
652 static int prefix_cmp(const void *va
, const void *vb
)
654 const struct odhcpd_ipaddr
*a
= va
, *b
= vb
;
657 if (a
->prefix
== b
->prefix
) {
658 ret
= (ntohl(a
->addr
.in
.s_addr
) < ntohl(b
->addr
.in
.s_addr
)) ? 1 :
659 (ntohl(a
->addr
.in
.s_addr
) > ntohl(b
->addr
.in
.s_addr
)) ? -1 : 0;
661 ret
= a
->prefix
< b
->prefix
? 1 : -1;
667 /* compare IPv6 prefixes */
668 static int prefix6_cmp(const void *va
, const void *vb
)
670 const struct odhcpd_ipaddr
*a
= va
, *b
= vb
;
671 uint32_t a_pref
= IN6_IS_ADDR_ULA(&a
->addr
.in6
) ? 1 : a
->preferred
;
672 uint32_t b_pref
= IN6_IS_ADDR_ULA(&b
->addr
.in6
) ? 1 : b
->preferred
;
673 return (a_pref
< b_pref
) ? 1 : (a_pref
> b_pref
) ? -1 : 0;
677 /* Detect an IPV6-address currently assigned to the given interface */
678 ssize_t
netlink_get_interface_addrs(int ifindex
, bool v6
, struct odhcpd_ipaddr
**addrs
)
681 struct ifaddrmsg ifa
= {
682 .ifa_family
= v6
? AF_INET6
: AF_INET
,
686 .ifa_index
= ifindex
, };
687 struct nl_cb
*cb
= nl_cb_alloc(NL_CB_DEFAULT
);
688 struct addr_info ctxt
= {
690 .af
= v6
? AF_INET6
: AF_INET
,
701 msg
= nlmsg_alloc_simple(RTM_GETADDR
, NLM_F_REQUEST
| NLM_F_DUMP
);
708 nlmsg_append(msg
, &ifa
, sizeof(ifa
), 0);
710 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_addr_valid
, &ctxt
);
711 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, cb_addr_finish
, &ctxt
);
712 nl_cb_err(cb
, NL_CB_CUSTOM
, cb_addr_error
, &ctxt
);
714 ctxt
.ret
= nl_send_auto_complete(rtnl_socket
, msg
);
719 while (ctxt
.pending
> 0)
720 nl_recvmsgs(rtnl_socket
, cb
);
725 time_t now
= odhcpd_time();
726 struct odhcpd_ipaddr
*addr
= *addrs
;
728 qsort(addr
, ctxt
.ret
, sizeof(*addr
), v6
? prefix6_cmp
: prefix_cmp
);
730 for (ssize_t i
= 0; i
< ctxt
.ret
; ++i
) {
731 if (addr
[i
].preferred
< UINT32_MAX
- now
)
732 addr
[i
].preferred
+= now
;
734 if (addr
[i
].valid
< UINT32_MAX
- now
)
735 addr
[i
].valid
+= now
;
750 const struct in6_addr
*addr
;
755 static int cb_proxy_neigh_valid(struct nl_msg
*msg
, void *arg
)
757 struct neigh_info
*ctxt
= (struct neigh_info
*)arg
;
758 struct nlmsghdr
*hdr
= nlmsg_hdr(msg
);
760 struct nlattr
*nla_dst
;
762 if (hdr
->nlmsg_type
!= RTM_NEWNEIGH
)
765 ndm
= NLMSG_DATA(hdr
);
766 if (ndm
->ndm_family
!= AF_INET6
||
767 (ctxt
->ifindex
&& ndm
->ndm_ifindex
!= ctxt
->ifindex
))
770 if (!(ndm
->ndm_flags
& NTF_PROXY
))
773 nla_dst
= nlmsg_find_attr(hdr
, sizeof(*ndm
), NDA_DST
);
777 if (nla_memcmp(nla_dst
,ctxt
->addr
, 16) == 0)
784 static int cb_proxy_neigh_finish(_unused
struct nl_msg
*msg
, void *arg
)
786 struct neigh_info
*ctxt
= (struct neigh_info
*)arg
;
794 static int cb_proxy_neigh_error(_unused
struct sockaddr_nl
*nla
, struct nlmsgerr
*err
,
797 struct neigh_info
*ctxt
= (struct neigh_info
*)arg
;
800 ctxt
->ret
= err
->error
;
805 /* Detect an IPV6-address proxy neighbor for the given interface */
806 int netlink_get_interface_proxy_neigh(int ifindex
, const struct in6_addr
*addr
)
810 .ndm_family
= AF_INET6
,
811 .ndm_flags
= NTF_PROXY
,
812 .ndm_ifindex
= ifindex
,
814 struct nl_cb
*cb
= nl_cb_alloc(NL_CB_DEFAULT
);
815 struct neigh_info ctxt
= {
827 msg
= nlmsg_alloc_simple(RTM_GETNEIGH
, NLM_F_REQUEST
| NLM_F_MATCH
);
834 nlmsg_append(msg
, &ndm
, sizeof(ndm
), 0);
835 nla_put(msg
, NDA_DST
, sizeof(*addr
), addr
);
837 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, cb_proxy_neigh_valid
, &ctxt
);
838 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, cb_proxy_neigh_finish
, &ctxt
);
839 nl_cb_err(cb
, NL_CB_CUSTOM
, cb_proxy_neigh_error
, &ctxt
);
841 ctxt
.ret
= nl_send_auto_complete(rtnl_socket
, msg
);
845 while (ctxt
.pending
> 0)
846 nl_recvmsgs(rtnl_socket
, cb
);
857 int netlink_setup_route(const struct in6_addr
*addr
, const int prefixlen
,
858 const int ifindex
, const struct in6_addr
*gw
,
859 const uint32_t metric
, const bool add
)
863 .rtm_family
= AF_INET6
,
864 .rtm_dst_len
= prefixlen
,
866 .rtm_table
= RT_TABLE_MAIN
,
867 .rtm_protocol
= (add
? RTPROT_STATIC
: RTPROT_UNSPEC
),
868 .rtm_scope
= (add
? (gw
? RT_SCOPE_UNIVERSE
: RT_SCOPE_LINK
) : RT_SCOPE_NOWHERE
),
869 .rtm_type
= (add
? RTN_UNICAST
: RTN_UNSPEC
),
873 msg
= nlmsg_alloc_simple(add
? RTM_NEWROUTE
: RTM_DELROUTE
,
874 add
? NLM_F_CREATE
| NLM_F_REPLACE
: 0);
878 nlmsg_append(msg
, &rtm
, sizeof(rtm
), 0);
880 nla_put(msg
, RTA_DST
, sizeof(*addr
), addr
);
881 nla_put_u32(msg
, RTA_OIF
, ifindex
);
882 nla_put_u32(msg
, RTA_PRIORITY
, metric
);
885 nla_put(msg
, RTA_GATEWAY
, sizeof(*gw
), gw
);
887 ret
= nl_send_auto_complete(rtnl_socket
, msg
);
893 return nl_wait_for_ack(rtnl_socket
);
897 int netlink_setup_proxy_neigh(const struct in6_addr
*addr
,
898 const int ifindex
, const bool add
)
902 .ndm_family
= AF_INET6
,
903 .ndm_flags
= NTF_PROXY
,
904 .ndm_ifindex
= ifindex
,
906 int ret
= 0, flags
= NLM_F_REQUEST
;
909 flags
|= NLM_F_REPLACE
| NLM_F_CREATE
;
911 msg
= nlmsg_alloc_simple(add
? RTM_NEWNEIGH
: RTM_DELNEIGH
, flags
);
915 nlmsg_append(msg
, &ndm
, sizeof(ndm
), 0);
917 nla_put(msg
, NDA_DST
, sizeof(*addr
), addr
);
919 ret
= nl_send_auto_complete(rtnl_socket
, msg
);
925 return nl_wait_for_ack(rtnl_socket
);
929 int netlink_setup_addr(struct odhcpd_ipaddr
*addr
,
930 const int ifindex
, const bool v6
, const bool add
)
933 struct ifaddrmsg ifa
= {
934 .ifa_family
= v6
? AF_INET6
: AF_INET
,
935 .ifa_prefixlen
= addr
->prefix
,
938 .ifa_index
= ifindex
, };
939 int ret
= 0, flags
= NLM_F_REQUEST
;
942 flags
|= NLM_F_REPLACE
| NLM_F_CREATE
;
944 msg
= nlmsg_alloc_simple(add
? RTM_NEWADDR
: RTM_DELADDR
, 0);
948 nlmsg_append(msg
, &ifa
, sizeof(ifa
), flags
);
949 nla_put(msg
, IFA_LOCAL
, v6
? 16 : 4, &addr
->addr
);
951 struct ifa_cacheinfo cinfo
= { .ifa_prefered
= 0xffffffffU
,
952 .ifa_valid
= 0xffffffffU
,
955 time_t now
= odhcpd_time();
957 if (addr
->preferred
) {
958 int64_t preferred
= addr
->preferred
- now
;
961 else if (preferred
> UINT32_MAX
)
962 preferred
= UINT32_MAX
;
964 cinfo
.ifa_prefered
= preferred
;
968 int64_t valid
= addr
->valid
- now
;
973 else if (valid
> UINT32_MAX
)
976 cinfo
.ifa_valid
= valid
;
979 nla_put(msg
, IFA_CACHEINFO
, sizeof(cinfo
), &cinfo
);
981 nla_put_u32(msg
, IFA_FLAGS
, IFA_F_NOPREFIXROUTE
);
983 if (addr
->broadcast
.s_addr
)
984 nla_put_u32(msg
, IFA_BROADCAST
, addr
->broadcast
.s_addr
);
987 ret
= nl_send_auto_complete(rtnl_socket
, msg
);
993 return nl_wait_for_ack(rtnl_socket
);
996 void netlink_dump_neigh_table(const bool proxy
)
1000 .ndm_family
= AF_INET6
,
1001 .ndm_flags
= proxy
? NTF_PROXY
: 0,
1004 msg
= nlmsg_alloc_simple(RTM_GETNEIGH
, NLM_F_REQUEST
| NLM_F_DUMP
);
1008 nlmsg_append(msg
, &ndm
, sizeof(ndm
), 0);
1010 nl_send_auto_complete(rtnl_event
.sock
, msg
);
1015 void netlink_dump_addr_table(const bool v6
)
1018 struct ifaddrmsg ifa
= {
1019 .ifa_family
= v6
? AF_INET6
: AF_INET
,
1022 msg
= nlmsg_alloc_simple(RTM_GETADDR
, NLM_F_REQUEST
| NLM_F_DUMP
);
1026 nlmsg_append(msg
, &ifa
, sizeof(ifa
), 0);
1028 nl_send_auto_complete(rtnl_event
.sock
, msg
);