2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
32 #include <sys/types.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/syslog.h>
41 #include <net/if_dl.h>
42 #include <net/if_media.h>
43 #include <net/if_var.h>
44 #include <net/if_clone.h>
45 #include <net/route.h>
46 #include <net/route/nhop.h>
47 #include <net/route/route_ctl.h>
48 #include <netlink/netlink.h>
49 #include <netlink/netlink_ctl.h>
50 #include <netlink/netlink_route.h>
51 #include <netlink/route/route_var.h>
53 #include <netinet6/scope6_var.h> /* scope deembedding */
55 #define DEBUG_MOD_NAME nl_iface
56 #define DEBUG_MAX_LEVEL LOG_DEBUG3
57 #include <netlink/netlink_debug.h>
58 _DECLARE_DEBUG(LOG_DEBUG);
60 struct netlink_walkargs {
71 static eventhandler_tag ifdetach_event, ifattach_event, iflink_event, ifaddr_event;
73 static SLIST_HEAD(, nl_cloner) nl_cloners = SLIST_HEAD_INITIALIZER(nl_cloners);
75 static struct sx rtnl_cloner_lock;
76 SX_SYSINIT(rtnl_cloner_lock, &rtnl_cloner_lock, "rtnl cloner lock");
78 static struct nl_cloner *rtnl_iface_find_cloner_locked(const char *name);
82 * sendto(3, {{len=32, type=RTM_GETLINK, flags=NLM_F_REQUEST|NLM_F_DUMP, seq=1641940952, pid=0},
83 * {ifi_family=AF_INET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}}, 32, 0, NULL, 0) = 32
86 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_ETHER, ifi_index=if_nametoindex("enp0s31f6"), ifi_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST|IFF_LOWER_UP, ifi_change=0},
87 {{nla_len=10, nla_type=IFLA_ADDRESS}, "\xfe\x54\x00\x52\x3e\x90"}
90 {{nla_len=14, nla_type=IFLA_IFNAME}, "enp0s31f6"},
91 {{nla_len=8, nla_type=IFLA_TXQLEN}, 1000},
92 {{nla_len=5, nla_type=IFLA_OPERSTATE}, 6},
93 {{nla_len=5, nla_type=IFLA_LINKMODE}, 0},
94 {{nla_len=8, nla_type=IFLA_MTU}, 1500},
95 {{nla_len=8, nla_type=IFLA_MIN_MTU}, 68},
96 {{nla_len=8, nla_type=IFLA_MAX_MTU}, 9000},
97 {{nla_len=8, nla_type=IFLA_GROUP}, 0},
98 {{nla_len=8, nla_type=IFLA_PROMISCUITY}, 0},
99 {{nla_len=8, nla_type=IFLA_NUM_TX_QUEUES}, 1},
100 {{nla_len=8, nla_type=IFLA_GSO_MAX_SEGS}, 65535},
101 {{nla_len=8, nla_type=IFLA_GSO_MAX_SIZE}, 65536},
102 {{nla_len=8, nla_type=IFLA_NUM_RX_QUEUES}, 1},
103 {{nla_len=5, nla_type=IFLA_CARRIER}, 1},
104 {{nla_len=13, nla_type=IFLA_QDISC}, "fq_codel"},
105 {{nla_len=8, nla_type=IFLA_CARRIER_CHANGES}, 2},
106 {{nla_len=5, nla_type=IFLA_PROTO_DOWN}, 0},
107 {{nla_len=8, nla_type=IFLA_CARRIER_UP_COUNT}, 1},
108 {{nla_len=8, nla_type=IFLA_CARRIER_DOWN_COUNT}, 1},
112 uint8_t ifla_operstate;
113 uint8_t ifla_carrier;
117 get_operstate_ether(struct ifnet *ifp, struct if_state *pstate)
119 struct ifmediareq ifmr = {};
121 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (void *)&ifmr);
124 NL_LOG(LOG_DEBUG, "error calling SIOCGIFMEDIA on %s: %d",
125 if_name(ifp), error);
129 switch (IFM_TYPE(ifmr.ifm_active)) {
131 if (ifmr.ifm_status & IFM_ACTIVE) {
132 pstate->ifla_carrier = 1;
133 if (ifp->if_flags & IFF_MONITOR)
134 pstate->ifla_operstate = IF_OPER_DORMANT;
136 pstate->ifla_operstate = IF_OPER_UP;
138 pstate->ifla_operstate = IF_OPER_DOWN;
143 get_stats(struct nl_writer *nw, struct ifnet *ifp)
145 struct rtnl_link_stats64 *stats;
147 int nla_len = sizeof(struct nlattr) + sizeof(*stats);
148 struct nlattr *nla = nlmsg_reserve_data(nw, nla_len, struct nlattr);
151 nla->nla_type = IFLA_STATS64;
152 nla->nla_len = nla_len;
153 stats = (struct rtnl_link_stats64 *)(nla + 1);
155 stats->rx_packets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
156 stats->tx_packets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
157 stats->rx_bytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
158 stats->tx_bytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
159 stats->rx_errors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
160 stats->tx_errors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
161 stats->rx_dropped = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
162 stats->tx_dropped = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
163 stats->multicast = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
164 stats->rx_nohandler = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
170 get_operstate(struct ifnet *ifp, struct if_state *pstate)
172 pstate->ifla_operstate = IF_OPER_UNKNOWN;
173 pstate->ifla_carrier = 0; /* no carrier */
175 switch (ifp->if_type) {
178 get_operstate_ether(ifp, pstate);
181 /* Map admin state to the operstate */
182 if (ifp->if_flags & IFF_UP) {
183 pstate->ifla_operstate = IF_OPER_UP;
184 pstate->ifla_carrier = 1;
186 pstate->ifla_operstate = IF_OPER_DOWN;
192 ifp_flags_to_netlink(const struct ifnet *ifp)
194 return (ifp->if_flags | ifp->if_drv_flags);
197 #define LLADDR_CONST(s) ((const void *)((s)->sdl_data + (s)->sdl_nlen))
199 dump_sa(struct nl_writer *nw, int attr, const struct sockaddr *sa)
201 uint32_t addr_len = 0;
202 const void *addr_data = NULL;
204 struct in6_addr addr6;
210 switch (sa->sa_family) {
213 addr_len = sizeof(struct in_addr);
214 addr_data = &((const struct sockaddr_in *)sa)->sin_addr;
219 in6_splitscope(&((const struct sockaddr_in6 *)sa)->sin6_addr, &addr6, &addr_len);
220 addr_len = sizeof(struct in6_addr);
225 addr_len = ((const struct sockaddr_dl *)sa)->sdl_alen;
226 addr_data = LLADDR_CONST((const struct sockaddr_dl *)sa);
229 NL_LOG(LOG_DEBUG, "unsupported family: %d, skipping", sa->sa_family);
233 return (nlattr_add(nw, attr, addr_len, addr_data));
237 * Dumps interface state, properties and metrics.
238 * @nw: message writer
239 * @ifp: target interface
240 * @hdr: template header
241 * @if_flags_mask: changed if_[drv]_flags bitmask
243 * This function is called without epoch and MAY sleep.
246 dump_iface(struct nl_writer *nw, struct ifnet *ifp, const struct nlmsghdr *hdr,
249 struct ifinfomsg *ifinfo;
251 NL_LOG(LOG_DEBUG3, "dumping interface %s data", if_name(ifp));
253 if (!nlmsg_reply(nw, hdr, sizeof(struct ifinfomsg)))
256 ifinfo = nlmsg_reserve_object(nw, struct ifinfomsg);
257 ifinfo->ifi_family = AF_UNSPEC;
258 ifinfo->__ifi_pad = 0;
259 ifinfo->ifi_type = ifp->if_type;
260 ifinfo->ifi_index = ifp->if_index;
261 ifinfo->ifi_flags = ifp_flags_to_netlink(ifp);
262 ifinfo->ifi_change = if_flags_mask;
264 struct if_state ifs = {};
265 get_operstate(ifp, &ifs);
267 if (ifs.ifla_operstate == IF_OPER_UP)
268 ifinfo->ifi_flags |= IFF_LOWER_UP;
270 nlattr_add_string(nw, IFLA_IFNAME, if_name(ifp));
271 nlattr_add_u8(nw, IFLA_OPERSTATE, ifs.ifla_operstate);
272 nlattr_add_u8(nw, IFLA_CARRIER, ifs.ifla_carrier);
275 nlattr_add_u8(nw, IFLA_PROTO_DOWN, val);
276 nlattr_add_u8(nw, IFLA_LINKMODE, val);
278 if ((ifp->if_addr != NULL)) {
279 dump_sa(nw, IFLA_ADDRESS, ifp->if_addr->ifa_addr);
282 if ((ifp->if_broadcastaddr != NULL)) {
283 nlattr_add(nw, IFLA_BROADCAST, ifp->if_addrlen,
284 ifp->if_broadcastaddr);
287 nlattr_add_u32(nw, IFLA_MTU, ifp->if_mtu);
289 nlattr_add_u32(nw, IFLA_MIN_MTU, 60);
290 nlattr_add_u32(nw, IFLA_MAX_MTU, 9000);
291 nlattr_add_u32(nw, IFLA_GROUP, 0);
294 if (ifp->if_description != NULL)
295 nlattr_add_string(nw, IFLA_IFALIAS, ifp->if_description);
299 uint32_t val = (ifp->if_flags & IFF_PROMISC) != 0;
300 nlattr_add_u32(nw, IFLA_PROMISCUITY, val);
302 sx_slock(&rtnl_cloner_lock);
303 struct nl_cloner *cloner = rtnl_iface_find_cloner_locked(ifp->if_dname);
304 if (cloner != NULL && cloner->dump_f != NULL) {
305 /* Ignore any dump error */
306 cloner->dump_f(ifp, nw);
308 sx_sunlock(&rtnl_cloner_lock);
314 NL_LOG(LOG_DEBUG, "unable to dump interface %s state (ENOMEM)", if_name(ifp));
320 check_ifmsg(void *hdr, struct nl_pstate *npt)
322 struct ifinfomsg *ifm = hdr;
324 if (ifm->__ifi_pad != 0 || ifm->ifi_type != 0 ||
325 ifm->ifi_flags != 0 || ifm->ifi_change != 0) {
326 nlmsg_report_err_msg(npt,
327 "strict checking: non-zero values in ifinfomsg header");
334 #define _IN(_field) offsetof(struct ifinfomsg, _field)
335 #define _OUT(_field) offsetof(struct nl_parsed_link, _field)
336 static const struct nlfield_parser nlf_p_if[] = {
337 { .off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = nlf_get_u16 },
338 { .off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = nlf_get_u32 },
339 { .off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = nlf_get_u32 },
340 { .off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = nlf_get_u32 },
343 static const struct nlattr_parser nla_p_linfo[] = {
344 { .type = IFLA_INFO_KIND, .off = _OUT(ifla_cloner), .cb = nlattr_get_stringn },
345 { .type = IFLA_INFO_DATA, .off = _OUT(ifla_idata), .cb = nlattr_get_nla },
347 NL_DECLARE_ATTR_PARSER(linfo_parser, nla_p_linfo);
349 static const struct nlattr_parser nla_p_if[] = {
350 { .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string },
351 { .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = nlattr_get_uint32 },
352 { .type = IFLA_LINK, .off = _OUT(ifi_index), .cb = nlattr_get_uint32 },
353 { .type = IFLA_LINKINFO, .arg = &linfo_parser, .cb = nlattr_get_nested },
354 { .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = nlattr_get_string },
355 { .type = IFLA_GROUP, .off = _OUT(ifla_group), .cb = nlattr_get_string },
356 { .type = IFLA_ALT_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string },
360 NL_DECLARE_STRICT_PARSER(ifmsg_parser, struct ifinfomsg, check_ifmsg, nlf_p_if, nla_p_if);
363 match_iface(struct ifnet *ifp, void *_arg)
365 struct nl_parsed_link *attrs = (struct nl_parsed_link *)_arg;
367 if (attrs->ifi_index != 0 && attrs->ifi_index != ifp->if_index)
369 if (attrs->ifi_type != 0 && attrs->ifi_index != ifp->if_type)
371 if (attrs->ifla_ifname != NULL && strcmp(attrs->ifla_ifname, if_name(ifp)))
373 /* TODO: add group match */
379 dump_cb(struct ifnet *ifp, void *_arg)
381 struct netlink_walkargs *wa = (struct netlink_walkargs *)_arg;
382 if (!dump_iface(wa->nw, ifp, &wa->hdr, 0))
388 * {nlmsg_len=52, nlmsg_type=RTM_GETLINK, nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=1662842818, nlmsg_pid=0},
389 * {ifi_family=AF_PACKET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
391 * [{nla_len=10, nla_type=IFLA_IFNAME}, "vnet9"],
392 * [{nla_len=8, nla_type=IFLA_EXT_MASK}, RTEXT_FILTER_VF]
396 rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
398 struct epoch_tracker et;
402 struct nl_parsed_link attrs = {};
403 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
407 struct netlink_walkargs wa = {
410 .hdr.nlmsg_pid = hdr->nlmsg_pid,
411 .hdr.nlmsg_seq = hdr->nlmsg_seq,
412 .hdr.nlmsg_flags = hdr->nlmsg_flags,
413 .hdr.nlmsg_type = NL_RTM_NEWLINK,
416 /* Fast track for an interface w/ explicit name or index match */
417 if ((attrs.ifi_index != 0) || (attrs.ifla_ifname != NULL)) {
418 if (attrs.ifi_index != 0) {
419 NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching index %u",
422 ifp = ifnet_byindex_ref(attrs.ifi_index);
425 NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching name %s",
427 ifp = ifunit_ref(attrs.ifla_ifname);
431 if (match_iface(ifp, &attrs)) {
432 if (!dump_iface(wa.nw, ifp, &wa.hdr, 0))
442 /* Always treat non-direct-match as a multipart message */
443 wa.hdr.nlmsg_flags |= NLM_F_MULTI;
446 * Fetching some link properties require performing ioctl's that may be blocking.
447 * Address it by saving referenced pointers of the matching links,
448 * exiting from epoch and going through the list one-by-one.
451 NL_LOG(LOG_DEBUG2, "Start dump");
452 if_foreach_sleep(match_iface, &attrs, dump_cb, &wa);
453 NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped);
455 if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) {
456 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
464 * sendmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base=[
465 * {nlmsg_len=60, nlmsg_type=RTM_NEWLINK, nlmsg_flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, nlmsg_seq=1662715618, nlmsg_pid=0},
466 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
467 * {nla_len=11, nla_type=IFLA_IFNAME}, "dummy0"],
469 * {nla_len=16, nla_type=IFLA_LINKINFO},
471 * {nla_len=9, nla_type=IFLA_INFO_KIND}, "dummy"...
477 rtnl_handle_dellink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
479 struct epoch_tracker et;
483 struct nl_parsed_link attrs = {};
484 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
489 ifp = ifnet_byindex_ref(attrs.ifi_index);
492 NLP_LOG(LOG_DEBUG, nlp, "unable to find interface %u", attrs.ifi_index);
495 NLP_LOG(LOG_DEBUG3, nlp, "mapped ifindex %u to %s", attrs.ifi_index, if_name(ifp));
497 sx_xlock(&ifnet_detach_sxlock);
498 error = if_clone_destroy(if_name(ifp));
499 sx_xunlock(&ifnet_detach_sxlock);
501 NLP_LOG(LOG_DEBUG2, nlp, "deleting interface %s returned %d", if_name(ifp), error);
509 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1668185590, pid=0},
510 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}
512 * {{nla_len=8, nla_type=IFLA_MTU}, 123},
513 * {{nla_len=10, nla_type=IFLA_IFNAME}, "vlan1"},
514 * {{nla_len=24, nla_type=IFLA_LINKINFO},
516 * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
517 * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x7b\x00\x00\x00"}]}]}
520 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=1668185923, pid=0},
521 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=if_nametoindex("lo"), ifi_flags=0, ifi_change=0},
522 * {{nla_len=8, nla_type=IFLA_MTU}, 123}}
525 * Check command availability:
526 * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=0, pid=0},
527 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}
532 create_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs,
533 struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
535 if (lattrs->ifla_ifname == NULL || strlen(lattrs->ifla_ifname) == 0) {
536 NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_IFNAME attribute");
539 if (lattrs->ifla_cloner == NULL || strlen(lattrs->ifla_cloner) == 0) {
540 NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_INFO_KIND attribute");
547 sx_slock(&rtnl_cloner_lock);
548 struct nl_cloner *cloner = rtnl_iface_find_cloner_locked(lattrs->ifla_cloner);
549 if (cloner != NULL) {
551 error = cloner->create_f(lattrs, bm, nlp, npt);
553 sx_sunlock(&rtnl_cloner_lock);
556 error = generic_cloner.create_f(lattrs, bm, nlp, npt);
562 modify_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs,
563 struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
565 struct ifnet *ifp = NULL;
566 struct epoch_tracker et;
568 if (lattrs->ifi_index == 0 && lattrs->ifla_ifname == NULL) {
570 * Applications like ip(8) verify RTM_NEWLINK command
571 * existence by calling it with empty arguments. Always
572 * return "innocent" error in that case.
574 NLMSG_REPORT_ERR_MSG(npt, "empty ifi_index field");
578 if (lattrs->ifi_index != 0) {
580 ifp = ifnet_byindex_ref(lattrs->ifi_index);
583 NLMSG_REPORT_ERR_MSG(npt, "unable to find interface #%u",
589 if (ifp == NULL && lattrs->ifla_ifname != NULL) {
590 ifp = ifunit_ref(lattrs->ifla_ifname);
592 NLMSG_REPORT_ERR_MSG(npt, "unable to find interface %s",
593 lattrs->ifla_ifname);
601 * There can be multiple kinds of interfaces:
602 * 1) cloned, with additional options
603 * 2) cloned, but w/o additional options
604 * 3) non-cloned (e.g. "physical).
606 * Thus, try to find cloner-specific callback and fallback to the
607 * "default" handler if not found.
612 sx_slock(&rtnl_cloner_lock);
613 struct nl_cloner *cloner = rtnl_iface_find_cloner_locked(ifp->if_dname);
614 if (cloner != NULL) {
616 error = cloner->modify_f(ifp, lattrs, bm, nlp, npt);
618 sx_sunlock(&rtnl_cloner_lock);
621 error = generic_cloner.modify_f(ifp, lattrs, bm, nlp, npt);
630 rtnl_handle_newlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
632 struct nlattr_bmask bm;
635 struct nl_parsed_link attrs = {};
636 error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
639 nl_get_attrs_bmask_nlmsg(hdr, &ifmsg_parser, &bm);
641 if (hdr->nlmsg_flags & NLM_F_CREATE)
642 return (create_link(hdr, &attrs, &bm, nlp, npt));
644 return (modify_link(hdr, &attrs, &bm, nlp, npt));
647 struct nl_parsed_ifa {
649 uint8_t ifa_prefixlen;
653 struct sockaddr *ifa_address;
654 struct sockaddr *ifa_local;
657 #define _IN(_field) offsetof(struct ifaddrmsg, _field)
658 #define _OUT(_field) offsetof(struct nl_parsed_ifa, _field)
659 static const struct nlfield_parser nlf_p_ifa[] = {
660 { .off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = nlf_get_u8 },
661 { .off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = nlf_get_u8 },
662 { .off_in = _IN(ifa_scope), .off_out = _OUT(ifa_scope), .cb = nlf_get_u8 },
663 { .off_in = _IN(ifa_flags), .off_out = _OUT(ifa_flags), .cb = nlf_get_u8_u32 },
664 { .off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = nlf_get_u32 },
667 static const struct nlattr_parser nla_p_ifa[] = {
668 { .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = nlattr_get_ip },
669 { .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = nlattr_get_ip },
670 { .type = IFA_FLAGS, .off = _OUT(ifa_flags), .cb = nlattr_get_uint32 },
674 NL_DECLARE_PARSER(ifaddrmsg_parser, struct ifaddrmsg, nlf_p_ifa, nla_p_ifa);
679 {ifa_family=AF_INET, ifa_prefixlen=8, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_HOST, ifa_index=if_nametoindex("lo")},
681 {{nla_len=8, nla_type=IFA_ADDRESS}, inet_addr("127.0.0.1")},
682 {{nla_len=8, nla_type=IFA_LOCAL}, inet_addr("127.0.0.1")},
683 {{nla_len=7, nla_type=IFA_LABEL}, "lo"},
684 {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT},
685 {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=3619, tstamp=3619}}]},
688 {{len=72, type=RTM_NEWADDR, flags=NLM_F_MULTI, seq=1642191126, pid=566735},
689 {ifa_family=AF_INET6, ifa_prefixlen=96, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_UNIVERSE, ifa_index=if_nametoindex("virbr0")},
691 {{nla_len=20, nla_type=IFA_ADDRESS}, inet_pton(AF_INET6, "2a01:4f8:13a:70c:ffff::1")},
692 {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=4283, tstamp=4283}},
693 {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT}]},
697 ifa_get_scope(const struct ifaddr *ifa)
699 const struct sockaddr *sa;
700 uint8_t addr_scope = RT_SCOPE_UNIVERSE;
703 switch (sa->sa_family) {
708 addr = ((const struct sockaddr_in *)sa)->sin_addr;
709 if (IN_LOOPBACK(addr.s_addr))
710 addr_scope = RT_SCOPE_HOST;
711 else if (IN_LINKLOCAL(addr.s_addr))
712 addr_scope = RT_SCOPE_LINK;
719 const struct in6_addr *addr;
720 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
721 if (IN6_IS_ADDR_LOOPBACK(addr))
722 addr_scope = RT_SCOPE_HOST;
723 else if (IN6_IS_ADDR_LINKLOCAL(addr))
724 addr_scope = RT_SCOPE_LINK;
734 inet6_get_plen(const struct in6_addr *addr)
737 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
738 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
742 get_sa_plen(const struct sockaddr *sa)
745 const struct in_addr *paddr;
748 const struct in6_addr *paddr6;
751 switch (sa->sa_family) {
754 paddr = &(((const struct sockaddr_in *)sa)->sin_addr);
755 return bitcount32(paddr->s_addr);;
759 paddr6 = &(((const struct sockaddr_in6 *)sa)->sin6_addr);
760 return inet6_get_plen(paddr6);
769 * {'attrs': [('IFA_ADDRESS', '12.0.0.1'),
770 ('IFA_LOCAL', '12.0.0.1'),
771 ('IFA_LABEL', 'eth10'),
773 ('IFA_CACHEINFO', {'ifa_preferred': 4294967295, 'ifa_valid': 4294967295, 'cstamp': 63745746, 'tstamp': 63745746})],
776 dump_iface_addr(struct nl_writer *nw, struct ifnet *ifp, struct ifaddr *ifa,
777 const struct nlmsghdr *hdr)
779 struct ifaddrmsg *ifamsg;
780 struct sockaddr *sa = ifa->ifa_addr;
782 NL_LOG(LOG_DEBUG3, "dumping ifa %p type %s(%d) for interface %s",
783 ifa, rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp));
785 if (!nlmsg_reply(nw, hdr, sizeof(struct ifaddrmsg)))
788 ifamsg = nlmsg_reserve_object(nw, struct ifaddrmsg);
789 ifamsg->ifa_family = sa->sa_family;
790 ifamsg->ifa_prefixlen = get_sa_plen(ifa->ifa_netmask);
791 ifamsg->ifa_flags = 0; // ifa_flags is useless
792 ifamsg->ifa_scope = ifa_get_scope(ifa);
793 ifamsg->ifa_index = ifp->if_index;
795 if (ifp->if_flags & IFF_POINTOPOINT) {
796 dump_sa(nw, IFA_ADDRESS, ifa->ifa_dstaddr);
797 dump_sa(nw, IFA_LOCAL, sa);
799 dump_sa(nw, IFA_ADDRESS, sa);
802 * In most cases, IFA_ADDRESS == IFA_LOCAL
803 * Skip IFA_LOCAL for anything except INET
805 if (sa->sa_family == AF_INET)
806 dump_sa(nw, IFA_LOCAL, sa);
809 if (ifp->if_flags & IFF_BROADCAST)
810 dump_sa(nw, IFA_BROADCAST, ifa->ifa_broadaddr);
812 nlattr_add_string(nw, IFA_LABEL, if_name(ifp));
814 uint32_t val = 0; // ifa->ifa_flags;
815 nlattr_add_u32(nw, IFA_FLAGS, val);
820 NL_LOG(LOG_DEBUG, "Failed to dump ifa type %s(%d) for interface %s",
821 rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp));
827 dump_iface_addrs(struct netlink_walkargs *wa, struct ifnet *ifp)
831 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
832 if (wa->family != 0 && wa->family != ifa->ifa_addr->sa_family)
834 if (ifa->ifa_addr->sa_family == AF_LINK)
837 if (!dump_iface_addr(wa->nw, ifp, ifa, &wa->hdr))
846 rtnl_handle_getaddr(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
851 struct nl_parsed_ifa attrs = {};
852 error = nl_parse_nlmsg(hdr, &ifaddrmsg_parser, npt, &attrs);
856 struct netlink_walkargs wa = {
859 .family = attrs.ifa_family,
860 .hdr.nlmsg_pid = hdr->nlmsg_pid,
861 .hdr.nlmsg_seq = hdr->nlmsg_seq,
862 .hdr.nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI,
863 .hdr.nlmsg_type = NL_RTM_NEWADDR,
866 NL_LOG(LOG_DEBUG2, "Start dump");
868 if (attrs.ifa_index != 0) {
869 ifp = ifnet_byindex(attrs.ifa_index);
873 error = dump_iface_addrs(&wa, ifp);
875 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
876 error = dump_iface_addrs(&wa, ifp);
882 NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped);
884 if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) {
885 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
893 rtnl_handle_ifaddr(void *arg __unused, struct ifaddr *ifa, int cmd)
895 struct nlmsghdr hdr = {};
896 struct nl_writer nw = {};
899 switch (ifa->ifa_addr->sa_family) {
902 group = RTNLGRP_IPV4_IFADDR;
907 group = RTNLGRP_IPV6_IFADDR;
911 NL_LOG(LOG_DEBUG2, "ifa notification for unknown AF: %d",
912 ifa->ifa_addr->sa_family);
916 if (!nl_has_listeners(NETLINK_ROUTE, group))
919 if (!nlmsg_get_group_writer(&nw, NLMSG_LARGE, NETLINK_ROUTE, group)) {
920 NL_LOG(LOG_DEBUG, "error allocating group writer");
924 hdr.nlmsg_type = (cmd == RTM_DELETE) ? NL_RTM_DELADDR : NL_RTM_NEWADDR;
926 dump_iface_addr(&nw, ifa->ifa_ifp, ifa, &hdr);
931 rtnl_handle_ifevent(struct ifnet *ifp, int nlmsg_type, int if_flags_mask)
933 struct nlmsghdr hdr = { .nlmsg_type = nlmsg_type };
934 struct nl_writer nw = {};
936 if (!nl_has_listeners(NETLINK_ROUTE, RTNLGRP_LINK))
939 if (!nlmsg_get_group_writer(&nw, NLMSG_LARGE, NETLINK_ROUTE, RTNLGRP_LINK)) {
940 NL_LOG(LOG_DEBUG, "error allocating mbuf");
943 dump_iface(&nw, ifp, &hdr, if_flags_mask);
948 rtnl_handle_ifattach(void *arg, struct ifnet *ifp)
950 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
951 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0);
955 rtnl_handle_ifdetach(void *arg, struct ifnet *ifp)
957 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
958 rtnl_handle_ifevent(ifp, NL_RTM_DELLINK, 0);
962 rtnl_handle_iflink(void *arg, struct ifnet *ifp)
964 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
965 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0);
969 rtnl_handle_ifnet_event(struct ifnet *ifp, int if_flags_mask)
971 NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
972 rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, if_flags_mask);
975 static const struct rtnl_cmd_handler cmd_handlers[] = {
977 .cmd = NL_RTM_GETLINK,
978 .name = "RTM_GETLINK",
979 .cb = &rtnl_handle_getlink,
980 .flags = RTNL_F_NOEPOCH,
983 .cmd = NL_RTM_DELLINK,
984 .name = "RTM_DELLINK",
985 .cb = &rtnl_handle_dellink,
986 .priv = PRIV_NET_IFDESTROY,
987 .flags = RTNL_F_NOEPOCH,
990 .cmd = NL_RTM_NEWLINK,
991 .name = "RTM_NEWLINK",
992 .cb = &rtnl_handle_newlink,
993 .priv = PRIV_NET_IFCREATE,
994 .flags = RTNL_F_NOEPOCH,
997 .cmd = NL_RTM_GETADDR,
998 .name = "RTM_GETADDR",
999 .cb = &rtnl_handle_getaddr,
1002 .cmd = NL_RTM_NEWADDR,
1003 .name = "RTM_NEWADDR",
1004 .cb = &rtnl_handle_getaddr,
1007 .cmd = NL_RTM_DELADDR,
1008 .name = "RTM_DELADDR",
1009 .cb = &rtnl_handle_getaddr,
1013 static const struct nlhdr_parser *all_parsers[] = { &ifmsg_parser, &ifaddrmsg_parser };
1016 rtnl_iface_add_cloner(struct nl_cloner *cloner)
1018 sx_xlock(&rtnl_cloner_lock);
1019 SLIST_INSERT_HEAD(&nl_cloners, cloner, next);
1020 sx_xunlock(&rtnl_cloner_lock);
1024 rtnl_iface_del_cloner(struct nl_cloner *cloner)
1026 sx_xlock(&rtnl_cloner_lock);
1027 SLIST_REMOVE(&nl_cloners, cloner, nl_cloner, next);
1028 sx_xunlock(&rtnl_cloner_lock);
1031 static struct nl_cloner *
1032 rtnl_iface_find_cloner_locked(const char *name)
1034 struct nl_cloner *cloner;
1036 SLIST_FOREACH(cloner, &nl_cloners, next) {
1037 if (!strcmp(name, cloner->name))
1045 rtnl_ifaces_init(void)
1047 ifattach_event = EVENTHANDLER_REGISTER(
1048 ifnet_arrival_event, rtnl_handle_ifattach, NULL,
1049 EVENTHANDLER_PRI_ANY);
1050 ifdetach_event = EVENTHANDLER_REGISTER(
1051 ifnet_departure_event, rtnl_handle_ifdetach, NULL,
1052 EVENTHANDLER_PRI_ANY);
1053 ifaddr_event = EVENTHANDLER_REGISTER(
1054 rt_addrmsg, rtnl_handle_ifaddr, NULL,
1055 EVENTHANDLER_PRI_ANY);
1056 iflink_event = EVENTHANDLER_REGISTER(
1057 ifnet_link_event, rtnl_handle_iflink, NULL,
1058 EVENTHANDLER_PRI_ANY);
1059 NL_VERIFY_PARSERS(all_parsers);
1060 rtnl_iface_drivers_register();
1061 rtnl_register_messages(cmd_handlers, NL_ARRAY_LEN(cmd_handlers));
1065 rtnl_ifaces_destroy(void)
1067 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ifattach_event);
1068 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_event);
1069 EVENTHANDLER_DEREGISTER(rt_addrmsg, ifaddr_event);
1070 EVENTHANDLER_DEREGISTER(ifnet_link_event, iflink_event);