xref: /freebsd/sys/netlink/route/iface.c (revision b333cd44de6db4d3182add2f80870c7a96d570d9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include "opt_netlink.h"
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include <sys/types.h>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/jail.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 #include <sys/syslog.h>
42 
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_media.h>
46 #include <net/if_var.h>
47 #include <net/if_clone.h>
48 #include <net/route.h>
49 #include <net/route/nhop.h>
50 #include <net/route/route_ctl.h>
51 #include <netinet6/in6_var.h>
52 #include <netlink/netlink.h>
53 #include <netlink/netlink_ctl.h>
54 #include <netlink/netlink_route.h>
55 #include <netlink/route/route_var.h>
56 
57 #include <netinet6/scope6_var.h> /* scope deembedding */
58 
59 #define	DEBUG_MOD_NAME	nl_iface
60 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
61 #include <netlink/netlink_debug.h>
62 _DECLARE_DEBUG(LOG_INFO);
63 
64 struct netlink_walkargs {
65 	struct nl_writer *nw;
66 	struct nlmsghdr hdr;
67 	struct nlpcb *so;
68 	struct ucred *cred;
69 	uint32_t fibnum;
70 	int family;
71 	int error;
72 	int count;
73 	int dumped;
74 };
75 
76 static eventhandler_tag ifdetach_event, ifattach_event, iflink_event, ifaddr_event;
77 
78 static SLIST_HEAD(, nl_cloner) nl_cloners = SLIST_HEAD_INITIALIZER(nl_cloners);
79 
80 static struct sx rtnl_cloner_lock;
81 SX_SYSINIT(rtnl_cloner_lock, &rtnl_cloner_lock, "rtnl cloner lock");
82 
83 /* These are external hooks for CARP. */
84 extern int	(*carp_get_vhid_p)(struct ifaddr *);
85 
86 /*
87  * RTM_GETLINK request
88  * sendto(3, {{len=32, type=RTM_GETLINK, flags=NLM_F_REQUEST|NLM_F_DUMP, seq=1641940952, pid=0},
89  *  {ifi_family=AF_INET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}}, 32, 0, NULL, 0) = 32
90  *
91  * Reply:
92  * {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},
93 {{nla_len=10, nla_type=IFLA_ADDRESS}, "\xfe\x54\x00\x52\x3e\x90"}
94 
95 [
96 {{nla_len=14, nla_type=IFLA_IFNAME}, "enp0s31f6"},
97 {{nla_len=8, nla_type=IFLA_TXQLEN}, 1000},
98 {{nla_len=5, nla_type=IFLA_OPERSTATE}, 6},
99 {{nla_len=5, nla_type=IFLA_LINKMODE}, 0},
100 {{nla_len=8, nla_type=IFLA_MTU}, 1500},
101 {{nla_len=8, nla_type=IFLA_MIN_MTU}, 68},
102  {{nla_len=8, nla_type=IFLA_MAX_MTU}, 9000},
103 {{nla_len=8, nla_type=IFLA_GROUP}, 0},
104 {{nla_len=8, nla_type=IFLA_PROMISCUITY}, 0},
105 {{nla_len=8, nla_type=IFLA_NUM_TX_QUEUES}, 1},
106 {{nla_len=8, nla_type=IFLA_GSO_MAX_SEGS}, 65535},
107 {{nla_len=8, nla_type=IFLA_GSO_MAX_SIZE}, 65536},
108 {{nla_len=8, nla_type=IFLA_NUM_RX_QUEUES}, 1},
109 {{nla_len=5, nla_type=IFLA_CARRIER}, 1},
110 {{nla_len=13, nla_type=IFLA_QDISC}, "fq_codel"},
111 {{nla_len=8, nla_type=IFLA_CARRIER_CHANGES}, 2},
112 {{nla_len=5, nla_type=IFLA_PROTO_DOWN}, 0},
113 {{nla_len=8, nla_type=IFLA_CARRIER_UP_COUNT}, 1},
114 {{nla_len=8, nla_type=IFLA_CARRIER_DOWN_COUNT}, 1},
115  */
116 
117 struct if_state {
118 	uint8_t		ifla_operstate;
119 	uint8_t		ifla_carrier;
120 };
121 
122 static void
123 get_operstate_ether(struct ifnet *ifp, struct if_state *pstate)
124 {
125 	struct ifmediareq ifmr = {};
126 	int error;
127 	error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (void *)&ifmr);
128 
129 	if (error != 0) {
130 		NL_LOG(LOG_DEBUG, "error calling SIOCGIFMEDIA on %s: %d",
131 		    if_name(ifp), error);
132 		return;
133 	}
134 
135 	switch (IFM_TYPE(ifmr.ifm_active)) {
136 	case IFM_ETHER:
137 		if (ifmr.ifm_status & IFM_ACTIVE) {
138 			pstate->ifla_carrier = 1;
139 			if (ifp->if_flags & IFF_MONITOR)
140 				pstate->ifla_operstate = IF_OPER_DORMANT;
141 			else
142 				pstate->ifla_operstate = IF_OPER_UP;
143 		} else
144 			pstate->ifla_operstate = IF_OPER_DOWN;
145 	}
146 }
147 
148 static bool
149 get_stats(struct nl_writer *nw, struct ifnet *ifp)
150 {
151 	struct rtnl_link_stats64 *stats;
152 
153 	int nla_len = sizeof(struct nlattr) + sizeof(*stats);
154 	struct nlattr *nla = nlmsg_reserve_data(nw, nla_len, struct nlattr);
155 	if (nla == NULL)
156 		return (false);
157 	nla->nla_type = IFLA_STATS64;
158 	nla->nla_len = nla_len;
159 	stats = (struct rtnl_link_stats64 *)(nla + 1);
160 
161 	stats->rx_packets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
162 	stats->tx_packets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
163 	stats->rx_bytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
164 	stats->tx_bytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
165 	stats->rx_errors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
166 	stats->tx_errors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
167 	stats->rx_dropped = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
168 	stats->tx_dropped = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
169 	stats->multicast = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
170 	stats->rx_nohandler = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
171 
172 	return (true);
173 }
174 
175 static void
176 get_operstate(struct ifnet *ifp, struct if_state *pstate)
177 {
178 	pstate->ifla_operstate = IF_OPER_UNKNOWN;
179 	pstate->ifla_carrier = 0; /* no carrier */
180 
181 	switch (ifp->if_type) {
182 	case IFT_ETHER:
183 	case IFT_L2VLAN:
184 		get_operstate_ether(ifp, pstate);
185 		break;
186 	default:
187 		/* Map admin state to the operstate */
188 		if (ifp->if_flags & IFF_UP) {
189 			pstate->ifla_operstate = IF_OPER_UP;
190 			pstate->ifla_carrier = 1;
191 		} else
192 			pstate->ifla_operstate = IF_OPER_DOWN;
193 		break;
194 	}
195 }
196 
197 static void
198 get_hwaddr(struct nl_writer *nw, struct ifnet *ifp)
199 {
200 	struct ifreq ifr = {};
201 
202 	if (if_gethwaddr(ifp, &ifr) == 0) {
203 		nlattr_add(nw, IFLAF_ORIG_HWADDR, if_getaddrlen(ifp),
204 		    ifr.ifr_addr.sa_data);
205 	}
206 }
207 
208 static unsigned
209 ifp_flags_to_netlink(const struct ifnet *ifp)
210 {
211         return (ifp->if_flags | ifp->if_drv_flags);
212 }
213 
214 #define LLADDR_CONST(s) ((const void *)((s)->sdl_data + (s)->sdl_nlen))
215 static bool
216 dump_sa(struct nl_writer *nw, int attr, const struct sockaddr *sa)
217 {
218         uint32_t addr_len = 0;
219         const void *addr_data = NULL;
220 #ifdef INET6
221         struct in6_addr addr6;
222 #endif
223 
224         if (sa == NULL)
225                 return (true);
226 
227         switch (sa->sa_family) {
228 #ifdef INET
229         case AF_INET:
230                 addr_len = sizeof(struct in_addr);
231                 addr_data = &((const struct sockaddr_in *)sa)->sin_addr;
232                 break;
233 #endif
234 #ifdef INET6
235         case AF_INET6:
236                 in6_splitscope(&((const struct sockaddr_in6 *)sa)->sin6_addr, &addr6, &addr_len);
237                 addr_len = sizeof(struct in6_addr);
238                 addr_data = &addr6;
239                 break;
240 #endif
241         case AF_LINK:
242                 addr_len = ((const struct sockaddr_dl *)sa)->sdl_alen;
243                 addr_data = LLADDR_CONST((const struct sockaddr_dl *)sa);
244                 break;
245         default:
246                 NL_LOG(LOG_DEBUG2, "unsupported family: %d, skipping", sa->sa_family);
247                 return (true);
248         }
249 
250         return (nlattr_add(nw, attr, addr_len, addr_data));
251 }
252 
253 /*
254  * Dumps interface state, properties and metrics.
255  * @nw: message writer
256  * @ifp: target interface
257  * @hdr: template header
258  * @if_flags_mask: changed if_[drv]_flags bitmask
259  *
260  * This function is called without epoch and MAY sleep.
261  */
262 static bool
263 dump_iface(struct nl_writer *nw, struct ifnet *ifp, const struct nlmsghdr *hdr,
264     int if_flags_mask)
265 {
266         struct ifinfomsg *ifinfo;
267 
268         NL_LOG(LOG_DEBUG3, "dumping interface %s data", if_name(ifp));
269 
270 	if (!nlmsg_reply(nw, hdr, sizeof(struct ifinfomsg)))
271 		goto enomem;
272 
273         ifinfo = nlmsg_reserve_object(nw, struct ifinfomsg);
274         ifinfo->ifi_family = AF_UNSPEC;
275         ifinfo->__ifi_pad = 0;
276         ifinfo->ifi_type = ifp->if_type;
277         ifinfo->ifi_index = ifp->if_index;
278         ifinfo->ifi_flags = ifp_flags_to_netlink(ifp);
279         ifinfo->ifi_change = if_flags_mask;
280 
281 	struct if_state ifs = {};
282 	get_operstate(ifp, &ifs);
283 
284 	if (ifs.ifla_operstate == IF_OPER_UP)
285 		ifinfo->ifi_flags |= IFF_LOWER_UP;
286 
287         nlattr_add_string(nw, IFLA_IFNAME, if_name(ifp));
288         nlattr_add_u8(nw, IFLA_OPERSTATE, ifs.ifla_operstate);
289         nlattr_add_u8(nw, IFLA_CARRIER, ifs.ifla_carrier);
290 
291 /*
292         nlattr_add_u8(nw, IFLA_PROTO_DOWN, val);
293         nlattr_add_u8(nw, IFLA_LINKMODE, val);
294 */
295         if (if_getaddrlen(ifp) != 0) {
296 		struct ifaddr *ifa = if_getifaddr(ifp);
297 
298                 dump_sa(nw, IFLA_ADDRESS, ifa->ifa_addr);
299         }
300 
301         if ((ifp->if_broadcastaddr != NULL)) {
302 		nlattr_add(nw, IFLA_BROADCAST, ifp->if_addrlen,
303 		    ifp->if_broadcastaddr);
304         }
305 
306         nlattr_add_u32(nw, IFLA_MTU, ifp->if_mtu);
307 /*
308         nlattr_add_u32(nw, IFLA_MIN_MTU, 60);
309         nlattr_add_u32(nw, IFLA_MAX_MTU, 9000);
310         nlattr_add_u32(nw, IFLA_GROUP, 0);
311 */
312 
313 	if (ifp->if_description != NULL)
314 		nlattr_add_string(nw, IFLA_IFALIAS, ifp->if_description);
315 
316 	/* Store FreeBSD-specific attributes */
317 	int off = nlattr_add_nested(nw, IFLA_FREEBSD);
318 	if (off != 0) {
319 		get_hwaddr(nw, ifp);
320 
321 		nlattr_set_len(nw, off);
322 	}
323 
324 	get_stats(nw, ifp);
325 
326 	uint32_t val = (ifp->if_flags & IFF_PROMISC) != 0;
327         nlattr_add_u32(nw, IFLA_PROMISCUITY, val);
328 
329 	ifc_dump_ifp_nl(ifp, nw);
330 
331         if (nlmsg_end(nw))
332 		return (true);
333 
334 enomem:
335         NL_LOG(LOG_DEBUG, "unable to dump interface %s state (ENOMEM)", if_name(ifp));
336         nlmsg_abort(nw);
337         return (false);
338 }
339 
340 static bool
341 check_ifmsg(void *hdr, struct nl_pstate *npt)
342 {
343 	struct ifinfomsg *ifm = hdr;
344 
345 	if (ifm->__ifi_pad != 0 || ifm->ifi_type != 0 ||
346 	    ifm->ifi_flags != 0 || ifm->ifi_change != 0) {
347 		nlmsg_report_err_msg(npt,
348 		    "strict checking: non-zero values in ifinfomsg header");
349 		return (false);
350 	}
351 
352 	return (true);
353 }
354 
355 #define	_IN(_field)	offsetof(struct ifinfomsg, _field)
356 #define	_OUT(_field)	offsetof(struct nl_parsed_link, _field)
357 static const struct nlfield_parser nlf_p_if[] = {
358 	{ .off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = nlf_get_u16 },
359 	{ .off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = nlf_get_u32 },
360 	{ .off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = nlf_get_u32 },
361 	{ .off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = nlf_get_u32 },
362 };
363 
364 static const struct nlattr_parser nla_p_linfo[] = {
365 	{ .type = IFLA_INFO_KIND, .off = _OUT(ifla_cloner), .cb = nlattr_get_stringn },
366 	{ .type = IFLA_INFO_DATA, .off = _OUT(ifla_idata), .cb = nlattr_get_nla },
367 };
368 NL_DECLARE_ATTR_PARSER(linfo_parser, nla_p_linfo);
369 
370 static const struct nlattr_parser nla_p_if[] = {
371 	{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string },
372 	{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = nlattr_get_uint32 },
373 	{ .type = IFLA_LINK, .off = _OUT(ifla_link), .cb = nlattr_get_uint32 },
374 	{ .type = IFLA_LINKINFO, .arg = &linfo_parser, .cb = nlattr_get_nested },
375 	{ .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = nlattr_get_string },
376 	{ .type = IFLA_GROUP, .off = _OUT(ifla_group), .cb = nlattr_get_string },
377 	{ .type = IFLA_ALT_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string },
378 };
379 #undef _IN
380 #undef _OUT
381 NL_DECLARE_STRICT_PARSER(ifmsg_parser, struct ifinfomsg, check_ifmsg, nlf_p_if, nla_p_if);
382 
383 static bool
384 match_iface(struct ifnet *ifp, void *_arg)
385 {
386 	struct nl_parsed_link *attrs = (struct nl_parsed_link *)_arg;
387 
388 	if (attrs->ifi_index != 0 && attrs->ifi_index != ifp->if_index)
389 		return (false);
390 	if (attrs->ifi_type != 0 && attrs->ifi_index != ifp->if_type)
391 		return (false);
392 	if (attrs->ifla_ifname != NULL && strcmp(attrs->ifla_ifname, if_name(ifp)))
393 		return (false);
394 	/* TODO: add group match */
395 
396 	return (true);
397 }
398 
399 static int
400 dump_cb(struct ifnet *ifp, void *_arg)
401 {
402 	struct netlink_walkargs *wa = (struct netlink_walkargs *)_arg;
403 	if (!dump_iface(wa->nw, ifp, &wa->hdr, 0))
404 		return (ENOMEM);
405 	return (0);
406 }
407 
408 /*
409  * {nlmsg_len=52, nlmsg_type=RTM_GETLINK, nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=1662842818, nlmsg_pid=0},
410  *  {ifi_family=AF_PACKET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
411  *   [
412  *    [{nla_len=10, nla_type=IFLA_IFNAME}, "vnet9"],
413  *    [{nla_len=8, nla_type=IFLA_EXT_MASK}, RTEXT_FILTER_VF]
414  *   ]
415  */
416 static int
417 rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
418 {
419 	struct epoch_tracker et;
420         struct ifnet *ifp;
421 	int error = 0;
422 
423 	struct nl_parsed_link attrs = {};
424 	error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
425 	if (error != 0)
426 		return (error);
427 
428 	struct netlink_walkargs wa = {
429 		.so = nlp,
430 		.nw = npt->nw,
431 		.hdr.nlmsg_pid = hdr->nlmsg_pid,
432 		.hdr.nlmsg_seq = hdr->nlmsg_seq,
433 		.hdr.nlmsg_flags = hdr->nlmsg_flags,
434 		.hdr.nlmsg_type = NL_RTM_NEWLINK,
435 	};
436 
437 	/* Fast track for an interface w/ explicit name or index match */
438 	if ((attrs.ifi_index != 0) || (attrs.ifla_ifname != NULL)) {
439 		if (attrs.ifi_index != 0) {
440 			NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching index %u",
441 			    attrs.ifi_index);
442 			NET_EPOCH_ENTER(et);
443 			ifp = ifnet_byindex_ref(attrs.ifi_index);
444 			NET_EPOCH_EXIT(et);
445 		} else {
446 			NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching name %s",
447 			    attrs.ifla_ifname);
448 			ifp = ifunit_ref(attrs.ifla_ifname);
449 		}
450 
451 		if (ifp != NULL) {
452 			if (match_iface(ifp, &attrs)) {
453 				if (!dump_iface(wa.nw, ifp, &wa.hdr, 0))
454 					error = ENOMEM;
455 			} else
456 				error = ENODEV;
457 			if_rele(ifp);
458 		} else
459 			error = ENODEV;
460 		return (error);
461 	}
462 
463 	/* Always treat non-direct-match as a multipart message */
464 	wa.hdr.nlmsg_flags |= NLM_F_MULTI;
465 
466 	/*
467 	 * Fetching some link properties require performing ioctl's that may be blocking.
468 	 * Address it by saving referenced pointers of the matching links,
469 	 * exiting from epoch and going through the list one-by-one.
470 	 */
471 
472 	NL_LOG(LOG_DEBUG2, "Start dump");
473 	if_foreach_sleep(match_iface, &attrs, dump_cb, &wa);
474 	NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped);
475 
476 	if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) {
477                 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
478                 return (ENOMEM);
479         }
480 
481 	return (error);
482 }
483 
484 /*
485  * sendmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base=[
486  * {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},
487  *  {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
488  *   {nla_len=11, nla_type=IFLA_IFNAME}, "dummy0"],
489  *   [
490  *    {nla_len=16, nla_type=IFLA_LINKINFO},
491  *     [
492  *      {nla_len=9, nla_type=IFLA_INFO_KIND}, "dummy"...
493  *     ]
494  *    ]
495  */
496 
497 static int
498 rtnl_handle_dellink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
499 {
500 	struct epoch_tracker et;
501         struct ifnet *ifp;
502 	int error;
503 
504 	struct nl_parsed_link attrs = {};
505 	error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
506 	if (error != 0)
507 		return (error);
508 
509 	NET_EPOCH_ENTER(et);
510 	ifp = ifnet_byindex_ref(attrs.ifi_index);
511 	NET_EPOCH_EXIT(et);
512 	if (ifp == NULL) {
513 		NLP_LOG(LOG_DEBUG, nlp, "unable to find interface %u", attrs.ifi_index);
514 		return (ENOENT);
515 	}
516 	NLP_LOG(LOG_DEBUG3, nlp, "mapped ifindex %u to %s", attrs.ifi_index, if_name(ifp));
517 
518 	sx_xlock(&ifnet_detach_sxlock);
519 	error = if_clone_destroy(if_name(ifp));
520 	sx_xunlock(&ifnet_detach_sxlock);
521 
522 	NLP_LOG(LOG_DEBUG2, nlp, "deleting interface %s returned %d", if_name(ifp), error);
523 
524 	if_rele(ifp);
525 	return (error);
526 }
527 
528 /*
529  * New link:
530  * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1668185590, pid=0},
531  *   {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}
532  *    [
533  *     {{nla_len=8, nla_type=IFLA_MTU}, 123},
534  *     {{nla_len=10, nla_type=IFLA_IFNAME}, "vlan1"},
535  *     {{nla_len=24, nla_type=IFLA_LINKINFO},
536  *      [
537  *       {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
538  *       {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x7b\x00\x00\x00"}]}]}
539  *
540  * Update link:
541  * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=1668185923, pid=0},
542  * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=if_nametoindex("lo"), ifi_flags=0, ifi_change=0},
543  * {{nla_len=8, nla_type=IFLA_MTU}, 123}}
544  *
545  *
546  * Check command availability:
547  * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=0, pid=0},
548  *  {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}
549  */
550 
551 
552 static int
553 create_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs,
554     struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
555 {
556 	if (lattrs->ifla_ifname == NULL || strlen(lattrs->ifla_ifname) == 0) {
557 		NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_IFNAME attribute");
558 		return (EINVAL);
559 	}
560 	if (lattrs->ifla_cloner == NULL || strlen(lattrs->ifla_cloner) == 0) {
561 		NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_INFO_KIND attribute");
562 		return (EINVAL);
563 	}
564 
565 	struct ifc_data_nl ifd = {
566 		.flags = IFC_F_CREATE,
567 		.lattrs = lattrs,
568 		.bm = bm,
569 		.npt = npt,
570 	};
571 	if (ifc_create_ifp_nl(lattrs->ifla_ifname, &ifd) && ifd.error == 0)
572 		nl_store_ifp_cookie(npt, ifd.ifp);
573 
574 	return (ifd.error);
575 }
576 
577 static int
578 modify_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs,
579     struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
580 {
581 	struct ifnet *ifp = NULL;
582 	struct epoch_tracker et;
583 
584 	if (lattrs->ifi_index == 0 && lattrs->ifla_ifname == NULL) {
585 		/*
586 		 * Applications like ip(8) verify RTM_NEWLINK command
587 		 * existence by calling it with empty arguments. Always
588 		 * return "innocent" error in that case.
589 		 */
590 		NLMSG_REPORT_ERR_MSG(npt, "empty ifi_index field");
591 		return (EPERM);
592 	}
593 
594 	if (lattrs->ifi_index != 0) {
595 		NET_EPOCH_ENTER(et);
596 		ifp = ifnet_byindex_ref(lattrs->ifi_index);
597 		NET_EPOCH_EXIT(et);
598 		if (ifp == NULL) {
599 			NLMSG_REPORT_ERR_MSG(npt, "unable to find interface #%u",
600 			    lattrs->ifi_index);
601 			return (ENOENT);
602 		}
603 	}
604 
605 	if (ifp == NULL && lattrs->ifla_ifname != NULL) {
606 		ifp = ifunit_ref(lattrs->ifla_ifname);
607 		if (ifp == NULL) {
608 			NLMSG_REPORT_ERR_MSG(npt, "unable to find interface %s",
609 			    lattrs->ifla_ifname);
610 			return (ENOENT);
611 		}
612 	}
613 
614 	MPASS(ifp != NULL);
615 
616 	/*
617 	 * Modification request can address either
618 	 * 1) cloned interface, in which case we call the cloner-specific
619 	 *  modification routine
620 	 * or
621 	 * 2) non-cloned (e.g. "physical") interface, in which case we call
622 	 *  generic modification routine
623 	 */
624 	struct ifc_data_nl ifd = { .lattrs = lattrs, .bm = bm, .npt = npt };
625 	if (!ifc_modify_ifp_nl(ifp, &ifd))
626 		ifd.error = nl_modify_ifp_generic(ifp, lattrs, bm, npt);
627 
628 	if_rele(ifp);
629 
630 	return (ifd.error);
631 }
632 
633 
634 static int
635 rtnl_handle_newlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
636 {
637 	struct nlattr_bmask bm;
638 	int error;
639 
640 	struct nl_parsed_link attrs = {};
641 	error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
642 	if (error != 0)
643 		return (error);
644 	nl_get_attrs_bmask_nlmsg(hdr, &ifmsg_parser, &bm);
645 
646 	if (hdr->nlmsg_flags & NLM_F_CREATE)
647 		return (create_link(hdr, &attrs, &bm, nlp, npt));
648 	else
649 		return (modify_link(hdr, &attrs, &bm, nlp, npt));
650 }
651 
652 struct nl_parsed_ifa {
653 	uint8_t		ifa_family;
654 	uint8_t		ifa_prefixlen;
655 	uint8_t		ifa_scope;
656 	uint32_t	ifa_index;
657 	uint32_t	ifa_flags;
658 	struct sockaddr	*ifa_address;
659 	struct sockaddr	*ifa_local;
660 };
661 
662 #define	_IN(_field)	offsetof(struct ifaddrmsg, _field)
663 #define	_OUT(_field)	offsetof(struct nl_parsed_ifa, _field)
664 static const struct nlfield_parser nlf_p_ifa[] = {
665 	{ .off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = nlf_get_u8 },
666 	{ .off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = nlf_get_u8 },
667 	{ .off_in = _IN(ifa_scope), .off_out = _OUT(ifa_scope), .cb = nlf_get_u8 },
668 	{ .off_in = _IN(ifa_flags), .off_out = _OUT(ifa_flags), .cb = nlf_get_u8_u32 },
669 	{ .off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = nlf_get_u32 },
670 };
671 
672 static const struct nlattr_parser nla_p_ifa[] = {
673 	{ .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = nlattr_get_ip },
674 	{ .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = nlattr_get_ip },
675 	{ .type = IFA_FLAGS, .off = _OUT(ifa_flags), .cb = nlattr_get_uint32 },
676 };
677 #undef _IN
678 #undef _OUT
679 NL_DECLARE_PARSER(ifaddrmsg_parser, struct ifaddrmsg, nlf_p_ifa, nla_p_ifa);
680 
681 
682 /*
683 
684 {ifa_family=AF_INET, ifa_prefixlen=8, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_HOST, ifa_index=if_nametoindex("lo")},
685  [
686         {{nla_len=8, nla_type=IFA_ADDRESS}, inet_addr("127.0.0.1")},
687         {{nla_len=8, nla_type=IFA_LOCAL}, inet_addr("127.0.0.1")},
688         {{nla_len=7, nla_type=IFA_LABEL}, "lo"},
689         {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT},
690         {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=3619, tstamp=3619}}]},
691 ---
692 
693 {{len=72, type=RTM_NEWADDR, flags=NLM_F_MULTI, seq=1642191126, pid=566735},
694  {ifa_family=AF_INET6, ifa_prefixlen=96, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_UNIVERSE, ifa_index=if_nametoindex("virbr0")},
695    [
696     {{nla_len=20, nla_type=IFA_ADDRESS}, inet_pton(AF_INET6, "2a01:4f8:13a:70c:ffff::1")},
697    {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=4283, tstamp=4283}},
698    {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT}]},
699 */
700 
701 static uint8_t
702 ifa_get_scope(const struct ifaddr *ifa)
703 {
704         const struct sockaddr *sa;
705         uint8_t addr_scope = RT_SCOPE_UNIVERSE;
706 
707         sa = ifa->ifa_addr;
708         switch (sa->sa_family) {
709 #ifdef INET
710         case AF_INET:
711                 {
712                         struct in_addr addr;
713                         addr = ((const struct sockaddr_in *)sa)->sin_addr;
714                         if (IN_LOOPBACK(addr.s_addr))
715                                 addr_scope = RT_SCOPE_HOST;
716                         else if (IN_LINKLOCAL(addr.s_addr))
717                                 addr_scope = RT_SCOPE_LINK;
718                         break;
719                 }
720 #endif
721 #ifdef INET6
722         case AF_INET6:
723                 {
724                         const struct in6_addr *addr;
725                         addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
726                         if (IN6_IS_ADDR_LOOPBACK(addr))
727                                 addr_scope = RT_SCOPE_HOST;
728                         else if (IN6_IS_ADDR_LINKLOCAL(addr))
729                                 addr_scope = RT_SCOPE_LINK;
730                         break;
731                 }
732 #endif
733         }
734 
735         return (addr_scope);
736 }
737 
738 #ifdef INET6
739 static uint8_t
740 inet6_get_plen(const struct in6_addr *addr)
741 {
742 
743 	return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
744 	    bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
745 }
746 #endif
747 
748 static uint8_t
749 get_sa_plen(const struct sockaddr *sa)
750 {
751 #ifdef INET
752         const struct in_addr *paddr;
753 #endif
754 #ifdef INET6
755         const struct in6_addr *paddr6;
756 #endif
757 
758         switch (sa->sa_family) {
759 #ifdef INET
760         case AF_INET:
761                 paddr = &(((const struct sockaddr_in *)sa)->sin_addr);
762                 return bitcount32(paddr->s_addr);;
763 #endif
764 #ifdef INET6
765         case AF_INET6:
766                 paddr6 = &(((const struct sockaddr_in6 *)sa)->sin6_addr);
767                 return inet6_get_plen(paddr6);
768 #endif
769         }
770 
771         return (0);
772 }
773 
774 #ifdef INET6
775 static uint32_t
776 in6_flags_to_nl(uint32_t flags)
777 {
778 	uint32_t nl_flags = 0;
779 
780 	if (flags & IN6_IFF_TEMPORARY)
781 		nl_flags |= IFA_F_TEMPORARY;
782 	if (flags & IN6_IFF_NODAD)
783 		nl_flags |= IFA_F_NODAD;
784 	if (flags & IN6_IFF_DEPRECATED)
785 		nl_flags |= IFA_F_DEPRECATED;
786 	if (flags & IN6_IFF_TENTATIVE)
787 		nl_flags |= IFA_F_TENTATIVE;
788 	if ((flags & (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) == 0)
789 		flags |= IFA_F_PERMANENT;
790 	if (flags & IN6_IFF_DUPLICATED)
791 		flags |= IFA_F_DADFAILED;
792 	return (nl_flags);
793 }
794 
795 static void
796 export_cache_info6(struct nl_writer *nw, const struct in6_ifaddr *ia)
797 {
798 	struct ifa_cacheinfo ci = {
799 		.cstamp = ia->ia6_createtime * 1000,
800 		.tstamp = ia->ia6_updatetime * 1000,
801 		.ifa_prefered = ia->ia6_lifetime.ia6t_pltime,
802 		.ifa_valid = ia->ia6_lifetime.ia6t_vltime,
803 	};
804 
805 	nlattr_add(nw, IFA_CACHEINFO, sizeof(ci), &ci);
806 }
807 #endif
808 
809 static void
810 export_cache_info(struct nl_writer *nw, struct ifaddr *ifa)
811 {
812 	switch (ifa->ifa_addr->sa_family) {
813 #ifdef INET6
814 	case AF_INET6:
815 		export_cache_info6(nw, (struct in6_ifaddr *)ifa);
816 		break;
817 #endif
818 	}
819 }
820 
821 /*
822  * {'attrs': [('IFA_ADDRESS', '12.0.0.1'),
823            ('IFA_LOCAL', '12.0.0.1'),
824            ('IFA_LABEL', 'eth10'),
825            ('IFA_FLAGS', 128),
826            ('IFA_CACHEINFO', {'ifa_preferred': 4294967295, 'ifa_valid': 4294967295, 'cstamp': 63745746, 'tstamp': 63745746})],
827  */
828 static bool
829 dump_iface_addr(struct nl_writer *nw, struct ifnet *ifp, struct ifaddr *ifa,
830     const struct nlmsghdr *hdr)
831 {
832         struct ifaddrmsg *ifamsg;
833         struct sockaddr *sa = ifa->ifa_addr;
834 
835         NL_LOG(LOG_DEBUG3, "dumping ifa %p type %s(%d) for interface %s",
836             ifa, rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp));
837 
838 	if (!nlmsg_reply(nw, hdr, sizeof(struct ifaddrmsg)))
839 		goto enomem;
840 
841         ifamsg = nlmsg_reserve_object(nw, struct ifaddrmsg);
842         ifamsg->ifa_family = sa->sa_family;
843         ifamsg->ifa_prefixlen = get_sa_plen(ifa->ifa_netmask);
844         ifamsg->ifa_flags = 0; // ifa_flags is useless
845         ifamsg->ifa_scope = ifa_get_scope(ifa);
846         ifamsg->ifa_index = ifp->if_index;
847 
848 	if (ifp->if_flags & IFF_POINTOPOINT) {
849 		dump_sa(nw, IFA_ADDRESS, ifa->ifa_dstaddr);
850 		dump_sa(nw, IFA_LOCAL, sa);
851 	} else {
852 		dump_sa(nw, IFA_ADDRESS, sa);
853 #ifdef INET
854 		/*
855 		 * In most cases, IFA_ADDRESS == IFA_LOCAL
856 		 * Skip IFA_LOCAL for anything except INET
857 		 */
858 		if (sa->sa_family == AF_INET)
859 			dump_sa(nw, IFA_LOCAL, sa);
860 #endif
861 	}
862 	if (ifp->if_flags & IFF_BROADCAST)
863 		dump_sa(nw, IFA_BROADCAST, ifa->ifa_broadaddr);
864 
865         nlattr_add_string(nw, IFA_LABEL, if_name(ifp));
866 
867         uint32_t nl_ifa_flags = 0;
868 #ifdef INET6
869 	if (sa->sa_family == AF_INET6) {
870 		struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
871 		nl_ifa_flags = in6_flags_to_nl(ia->ia6_flags);
872 	}
873 #endif
874         nlattr_add_u32(nw, IFA_FLAGS, nl_ifa_flags);
875 
876 	export_cache_info(nw, ifa);
877 
878 	/* Store FreeBSD-specific attributes */
879 	int off = nlattr_add_nested(nw, IFA_FREEBSD);
880 	if (off != 0) {
881 		if (ifa->ifa_carp != NULL && carp_get_vhid_p != NULL) {
882 			uint32_t vhid  = (uint32_t)(*carp_get_vhid_p)(ifa);
883 			nlattr_add_u32(nw, IFAF_VHID, vhid);
884 		}
885 #ifdef INET6
886 		if (sa->sa_family == AF_INET6) {
887 			uint32_t ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
888 
889 			nlattr_add_u32(nw, IFAF_FLAGS, ifa_flags);
890 		}
891 #endif
892 
893 		nlattr_set_len(nw, off);
894 	}
895 
896 	if (nlmsg_end(nw))
897 		return (true);
898 enomem:
899         NL_LOG(LOG_DEBUG, "Failed to dump ifa type %s(%d) for interface %s",
900             rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp));
901         nlmsg_abort(nw);
902         return (false);
903 }
904 
905 static int
906 dump_iface_addrs(struct netlink_walkargs *wa, struct ifnet *ifp)
907 {
908         struct ifaddr *ifa;
909 
910 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
911 		if (wa->family != 0 && wa->family != ifa->ifa_addr->sa_family)
912 			continue;
913 		if (ifa->ifa_addr->sa_family == AF_LINK)
914 			continue;
915 		if (prison_if(wa->cred, ifa->ifa_addr) != 0)
916 			continue;
917 		wa->count++;
918 		if (!dump_iface_addr(wa->nw, ifp, ifa, &wa->hdr))
919 			return (ENOMEM);
920 		wa->dumped++;
921 	}
922 
923 	return (0);
924 }
925 
926 static int
927 rtnl_handle_getaddr(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
928 {
929         struct ifnet *ifp;
930 	int error = 0;
931 
932 	struct nl_parsed_ifa attrs = {};
933 	error = nl_parse_nlmsg(hdr, &ifaddrmsg_parser, npt, &attrs);
934 	if (error != 0)
935 		return (error);
936 
937 	struct netlink_walkargs wa = {
938 		.so = nlp,
939 		.nw = npt->nw,
940 		.cred = nlp_get_cred(nlp),
941 		.family = attrs.ifa_family,
942 		.hdr.nlmsg_pid = hdr->nlmsg_pid,
943 		.hdr.nlmsg_seq = hdr->nlmsg_seq,
944 		.hdr.nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI,
945 		.hdr.nlmsg_type = NL_RTM_NEWADDR,
946 	};
947 
948 	NL_LOG(LOG_DEBUG2, "Start dump");
949 
950 	if (attrs.ifa_index != 0) {
951 		ifp = ifnet_byindex(attrs.ifa_index);
952 		if (ifp == NULL)
953 			error = ENOENT;
954 		else
955 			error = dump_iface_addrs(&wa, ifp);
956 	} else {
957 		CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
958 			error = dump_iface_addrs(&wa, ifp);
959 			if (error != 0)
960 				break;
961 		}
962 	}
963 
964 	NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped);
965 
966 	if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) {
967                 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
968                 return (ENOMEM);
969         }
970 
971 	return (error);
972 }
973 
974 static void
975 rtnl_handle_ifaddr(void *arg __unused, struct ifaddr *ifa, int cmd)
976 {
977 	struct nlmsghdr hdr = {};
978 	struct nl_writer nw = {};
979 	uint32_t group = 0;
980 
981 	switch (ifa->ifa_addr->sa_family) {
982 #ifdef INET
983 	case AF_INET:
984 		group = RTNLGRP_IPV4_IFADDR;
985 		break;
986 #endif
987 #ifdef INET6
988 	case AF_INET6:
989 		group = RTNLGRP_IPV6_IFADDR;
990 		break;
991 #endif
992 	default:
993 		NL_LOG(LOG_DEBUG2, "ifa notification for unknown AF: %d",
994 		    ifa->ifa_addr->sa_family);
995 		return;
996 	}
997 
998 	if (!nl_has_listeners(NETLINK_ROUTE, group))
999 		return;
1000 
1001 	if (!nlmsg_get_group_writer(&nw, NLMSG_LARGE, NETLINK_ROUTE, group)) {
1002 		NL_LOG(LOG_DEBUG, "error allocating group writer");
1003 		return;
1004 	}
1005 
1006 	hdr.nlmsg_type = (cmd == RTM_DELETE) ? NL_RTM_DELADDR : NL_RTM_NEWADDR;
1007 
1008 	dump_iface_addr(&nw, ifa->ifa_ifp, ifa, &hdr);
1009 	nlmsg_flush(&nw);
1010 }
1011 
1012 static void
1013 rtnl_handle_ifevent(struct ifnet *ifp, int nlmsg_type, int if_flags_mask)
1014 {
1015 	struct nlmsghdr hdr = { .nlmsg_type = nlmsg_type };
1016 	struct nl_writer nw = {};
1017 
1018 	if (!nl_has_listeners(NETLINK_ROUTE, RTNLGRP_LINK))
1019 		return;
1020 
1021 	if (!nlmsg_get_group_writer(&nw, NLMSG_LARGE, NETLINK_ROUTE, RTNLGRP_LINK)) {
1022 		NL_LOG(LOG_DEBUG, "error allocating mbuf");
1023 		return;
1024 	}
1025 	dump_iface(&nw, ifp, &hdr, if_flags_mask);
1026         nlmsg_flush(&nw);
1027 }
1028 
1029 static void
1030 rtnl_handle_ifattach(void *arg, struct ifnet *ifp)
1031 {
1032 	NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1033 	rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0);
1034 }
1035 
1036 static void
1037 rtnl_handle_ifdetach(void *arg, struct ifnet *ifp)
1038 {
1039 	NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1040 	rtnl_handle_ifevent(ifp, NL_RTM_DELLINK, 0);
1041 }
1042 
1043 static void
1044 rtnl_handle_iflink(void *arg, struct ifnet *ifp)
1045 {
1046 	NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1047 	rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0);
1048 }
1049 
1050 void
1051 rtnl_handle_ifnet_event(struct ifnet *ifp, int if_flags_mask)
1052 {
1053 	NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1054 	rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, if_flags_mask);
1055 }
1056 
1057 static const struct rtnl_cmd_handler cmd_handlers[] = {
1058 	{
1059 		.cmd = NL_RTM_GETLINK,
1060 		.name = "RTM_GETLINK",
1061 		.cb = &rtnl_handle_getlink,
1062 		.flags = RTNL_F_NOEPOCH | RTNL_F_ALLOW_NONVNET_JAIL,
1063 	},
1064 	{
1065 		.cmd = NL_RTM_DELLINK,
1066 		.name = "RTM_DELLINK",
1067 		.cb = &rtnl_handle_dellink,
1068 		.priv = PRIV_NET_IFDESTROY,
1069 		.flags = RTNL_F_NOEPOCH,
1070 	},
1071 	{
1072 		.cmd = NL_RTM_NEWLINK,
1073 		.name = "RTM_NEWLINK",
1074 		.cb = &rtnl_handle_newlink,
1075 		.priv = PRIV_NET_IFCREATE,
1076 		.flags = RTNL_F_NOEPOCH,
1077 	},
1078 	{
1079 		.cmd = NL_RTM_GETADDR,
1080 		.name = "RTM_GETADDR",
1081 		.cb = &rtnl_handle_getaddr,
1082 		.flags = RTNL_F_ALLOW_NONVNET_JAIL,
1083 	},
1084 	{
1085 		.cmd = NL_RTM_NEWADDR,
1086 		.name = "RTM_NEWADDR",
1087 		.cb = &rtnl_handle_getaddr,
1088 	},
1089 	{
1090 		.cmd = NL_RTM_DELADDR,
1091 		.name = "RTM_DELADDR",
1092 		.cb = &rtnl_handle_getaddr,
1093 	},
1094 };
1095 
1096 static const struct nlhdr_parser *all_parsers[] = { &ifmsg_parser, &ifaddrmsg_parser };
1097 
1098 void
1099 rtnl_iface_add_cloner(struct nl_cloner *cloner)
1100 {
1101 	sx_xlock(&rtnl_cloner_lock);
1102 	SLIST_INSERT_HEAD(&nl_cloners, cloner, next);
1103 	sx_xunlock(&rtnl_cloner_lock);
1104 }
1105 
1106 void
1107 rtnl_iface_del_cloner(struct nl_cloner *cloner)
1108 {
1109 	sx_xlock(&rtnl_cloner_lock);
1110 	SLIST_REMOVE(&nl_cloners, cloner, nl_cloner, next);
1111 	sx_xunlock(&rtnl_cloner_lock);
1112 }
1113 
1114 void
1115 rtnl_ifaces_init(void)
1116 {
1117 	ifattach_event = EVENTHANDLER_REGISTER(
1118 	    ifnet_arrival_event, rtnl_handle_ifattach, NULL,
1119 	    EVENTHANDLER_PRI_ANY);
1120 	ifdetach_event = EVENTHANDLER_REGISTER(
1121 	    ifnet_departure_event, rtnl_handle_ifdetach, NULL,
1122 	    EVENTHANDLER_PRI_ANY);
1123 	ifaddr_event = EVENTHANDLER_REGISTER(
1124 	    rt_addrmsg, rtnl_handle_ifaddr, NULL,
1125 	    EVENTHANDLER_PRI_ANY);
1126 	iflink_event = EVENTHANDLER_REGISTER(
1127 	    ifnet_link_event, rtnl_handle_iflink, NULL,
1128 	    EVENTHANDLER_PRI_ANY);
1129 	NL_VERIFY_PARSERS(all_parsers);
1130 	rtnl_register_messages(cmd_handlers, NL_ARRAY_LEN(cmd_handlers));
1131 }
1132 
1133 void
1134 rtnl_ifaces_destroy(void)
1135 {
1136 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ifattach_event);
1137 	EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_event);
1138 	EVENTHANDLER_DEREGISTER(rt_addrmsg, ifaddr_event);
1139 	EVENTHANDLER_DEREGISTER(ifnet_link_event, iflink_event);
1140 }
1141