xref: /freebsd/sys/netlink/route/iface.c (revision 2f8f892ca344d884abbed0305886bb532f16368f)
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 <sys/cdefs.h>
29 #include "opt_inet.h"
30 #include "opt_inet6.h"
31 #include <sys/types.h>
32 #include <sys/eventhandler.h>
33 #include <sys/kernel.h>
34 #include <sys/jail.h>
35 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/syslog.h>
39 #include <sys/proc.h>
40 
41 #include <net/if.h>
42 #include <net/if_dl.h>
43 #include <net/if_media.h>
44 #include <net/if_var.h>
45 #include <net/if_clone.h>
46 #include <net/route.h>
47 #include <net/route/nhop.h>
48 #include <net/route/route_ctl.h>
49 #include <netinet/in_var.h>
50 #include <netinet6/in6_var.h>
51 #include <netinet6/scope6_var.h> /* scope deembedding */
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 #define	DEBUG_MOD_NAME	nl_iface
58 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
59 #include <netlink/netlink_debug.h>
60 _DECLARE_DEBUG(LOG_INFO);
61 
62 struct netlink_walkargs {
63 	struct nl_writer *nw;
64 	struct nlmsghdr hdr;
65 	struct nlpcb *so;
66 	struct ucred *cred;
67 	uint32_t fibnum;
68 	int family;
69 	int error;
70 	int count;
71 	int dumped;
72 };
73 
74 static eventhandler_tag ifdetach_event, ifattach_event, ifrename_event,
75     iflink_event, ifaddr_event;
76 
77 static SLIST_HEAD(, nl_cloner) nl_cloners = SLIST_HEAD_INITIALIZER(nl_cloners);
78 
79 static struct sx rtnl_cloner_lock;
80 SX_SYSINIT(rtnl_cloner_lock, &rtnl_cloner_lock, "rtnl cloner lock");
81 
82 /* These are external hooks for CARP. */
83 extern int	(*carp_get_vhid_p)(struct ifaddr *);
84 
85 /*
86  * RTM_GETLINK request
87  * sendto(3, {{len=32, type=RTM_GETLINK, flags=NLM_F_REQUEST|NLM_F_DUMP, seq=1641940952, pid=0},
88  *  {ifi_family=AF_INET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}}, 32, 0, NULL, 0) = 32
89  *
90  * Reply:
91  * {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},
92 {{nla_len=10, nla_type=IFLA_ADDRESS}, "\xfe\x54\x00\x52\x3e\x90"}
93 
94 [
95 {{nla_len=14, nla_type=IFLA_IFNAME}, "enp0s31f6"},
96 {{nla_len=8, nla_type=IFLA_TXQLEN}, 1000},
97 {{nla_len=5, nla_type=IFLA_OPERSTATE}, 6},
98 {{nla_len=5, nla_type=IFLA_LINKMODE}, 0},
99 {{nla_len=8, nla_type=IFLA_MTU}, 1500},
100 {{nla_len=8, nla_type=IFLA_MIN_MTU}, 68},
101  {{nla_len=8, nla_type=IFLA_MAX_MTU}, 9000},
102 {{nla_len=8, nla_type=IFLA_GROUP}, 0},
103 {{nla_len=8, nla_type=IFLA_PROMISCUITY}, 0},
104 {{nla_len=8, nla_type=IFLA_NUM_TX_QUEUES}, 1},
105 {{nla_len=8, nla_type=IFLA_GSO_MAX_SEGS}, 65535},
106 {{nla_len=8, nla_type=IFLA_GSO_MAX_SIZE}, 65536},
107 {{nla_len=8, nla_type=IFLA_NUM_RX_QUEUES}, 1},
108 {{nla_len=5, nla_type=IFLA_CARRIER}, 1},
109 {{nla_len=13, nla_type=IFLA_QDISC}, "fq_codel"},
110 {{nla_len=8, nla_type=IFLA_CARRIER_CHANGES}, 2},
111 {{nla_len=5, nla_type=IFLA_PROTO_DOWN}, 0},
112 {{nla_len=8, nla_type=IFLA_CARRIER_UP_COUNT}, 1},
113 {{nla_len=8, nla_type=IFLA_CARRIER_DOWN_COUNT}, 1},
114  */
115 
116 struct if_state {
117 	uint8_t		ifla_operstate;
118 	uint8_t		ifla_carrier;
119 };
120 
121 static void
get_operstate_ether(if_t ifp,struct if_state * pstate)122 get_operstate_ether(if_t ifp, struct if_state *pstate)
123 {
124 	struct ifmediareq ifmr = {};
125 	int error;
126 	error = if_ioctl(ifp, SIOCGIFMEDIA, (void *)&ifmr);
127 
128 	if (error != 0) {
129 		NL_LOG(LOG_DEBUG, "error calling SIOCGIFMEDIA on %s: %d",
130 		    if_name(ifp), error);
131 		return;
132 	}
133 
134 	switch (IFM_TYPE(ifmr.ifm_active)) {
135 	case IFM_ETHER:
136 		if (ifmr.ifm_status & IFM_ACTIVE) {
137 			pstate->ifla_carrier = 1;
138 			if (if_getflags(ifp) & IFF_MONITOR)
139 				pstate->ifla_operstate = IF_OPER_DORMANT;
140 			else
141 				pstate->ifla_operstate = IF_OPER_UP;
142 		} else
143 			pstate->ifla_operstate = IF_OPER_DOWN;
144 	}
145 }
146 
147 static bool
get_stats(struct nl_writer * nw,if_t ifp)148 get_stats(struct nl_writer *nw, if_t ifp)
149 {
150 	struct rtnl_link_stats64 *stats;
151 
152 	int nla_len = sizeof(struct nlattr) + sizeof(*stats);
153 	struct nlattr *nla = nlmsg_reserve_data(nw, nla_len, struct nlattr);
154 	if (nla == NULL)
155 		return (false);
156 	nla->nla_type = IFLA_STATS64;
157 	nla->nla_len = nla_len;
158 	stats = (struct rtnl_link_stats64 *)(nla + 1);
159 
160 	stats->rx_packets = if_getcounter(ifp, IFCOUNTER_IPACKETS);
161 	stats->tx_packets = if_getcounter(ifp, IFCOUNTER_OPACKETS);
162 	stats->rx_bytes = if_getcounter(ifp, IFCOUNTER_IBYTES);
163 	stats->tx_bytes = if_getcounter(ifp, IFCOUNTER_OBYTES);
164 	stats->rx_errors = if_getcounter(ifp, IFCOUNTER_IERRORS);
165 	stats->tx_errors = if_getcounter(ifp, IFCOUNTER_OERRORS);
166 	stats->rx_dropped = if_getcounter(ifp, IFCOUNTER_IQDROPS);
167 	stats->tx_dropped = if_getcounter(ifp, IFCOUNTER_OQDROPS);
168 	stats->multicast = if_getcounter(ifp, IFCOUNTER_IMCASTS);
169 	stats->rx_nohandler = if_getcounter(ifp, IFCOUNTER_NOPROTO);
170 
171 	return (true);
172 }
173 
174 static void
get_operstate(if_t ifp,struct if_state * pstate)175 get_operstate(if_t ifp, struct if_state *pstate)
176 {
177 	pstate->ifla_operstate = IF_OPER_UNKNOWN;
178 	pstate->ifla_carrier = 0; /* no carrier */
179 
180 	switch (if_gettype(ifp)) {
181 	case IFT_ETHER:
182 	case IFT_L2VLAN:
183 		get_operstate_ether(ifp, pstate);
184 		break;
185 	default:
186 		/* Map admin state to the operstate */
187 		if (if_getflags(ifp) & IFF_UP) {
188 			pstate->ifla_operstate = IF_OPER_UP;
189 			pstate->ifla_carrier = 1;
190 		} else
191 			pstate->ifla_operstate = IF_OPER_DOWN;
192 		break;
193 	}
194 }
195 
196 static void
get_hwaddr(struct nl_writer * nw,if_t ifp)197 get_hwaddr(struct nl_writer *nw, if_t ifp)
198 {
199 	struct ifreq ifr = {};
200 
201 	if (if_gethwaddr(ifp, &ifr) == 0) {
202 		nlattr_add(nw, IFLAF_ORIG_HWADDR, if_getaddrlen(ifp),
203 		    ifr.ifr_addr.sa_data);
204 	}
205 }
206 
207 static int
dump_group_cb(struct ifg_group * ifg,void * _arg)208 dump_group_cb(struct ifg_group *ifg, void *_arg)
209 {
210 	struct nl_writer *nw = (struct nl_writer *)_arg;
211 	int off, ret = 0;
212 
213 	off = nlattr_add_nested(nw, IFLAF_GROUP);
214 	if (off == 0)
215 		return (ENOMEM);
216 
217 	if (!nlattr_add_string(nw, IFLAF_GROUP, ifg->ifg_group))
218 		ret = ENOMEM;
219 
220 	nlattr_set_len(nw, off);
221 	return (ret);
222 }
223 
224 static int
dump_group(struct nl_writer * nw,if_t ifp)225 dump_group(struct nl_writer *nw, if_t ifp)
226 {
227 	int off, ret;
228 
229 	off = nlattr_add_nested(nw, IFLAF_GROUP);
230 	if (off == 0)
231 		return (ENOMEM);
232 
233 	ret = if_foreach_group(ifp, dump_group_cb, nw);
234 	nlattr_set_len(nw, off);
235 
236 	return (ret);
237 }
238 
239 static unsigned
ifp_flags_to_netlink(const if_t ifp)240 ifp_flags_to_netlink(const if_t ifp)
241 {
242         return (if_getflags(ifp) | if_getdrvflags(ifp));
243 }
244 
245 #define LLADDR_CONST(s) ((const void *)((s)->sdl_data + (s)->sdl_nlen))
246 static bool
dump_sa(struct nl_writer * nw,int attr,const struct sockaddr * sa)247 dump_sa(struct nl_writer *nw, int attr, const struct sockaddr *sa)
248 {
249         uint32_t addr_len = 0;
250         const void *addr_data = NULL;
251 #ifdef INET6
252         struct in6_addr addr6;
253 #endif
254 
255         if (sa == NULL)
256                 return (true);
257 
258         switch (sa->sa_family) {
259 #ifdef INET
260         case AF_INET:
261                 addr_len = sizeof(struct in_addr);
262                 addr_data = &((const struct sockaddr_in *)sa)->sin_addr;
263                 break;
264 #endif
265 #ifdef INET6
266         case AF_INET6:
267                 in6_splitscope(&((const struct sockaddr_in6 *)sa)->sin6_addr, &addr6, &addr_len);
268                 addr_len = sizeof(struct in6_addr);
269                 addr_data = &addr6;
270                 break;
271 #endif
272         case AF_LINK:
273                 addr_len = ((const struct sockaddr_dl *)sa)->sdl_alen;
274                 addr_data = LLADDR_CONST((const struct sockaddr_dl *)sa);
275                 break;
276 	case AF_UNSPEC:
277 		/* Ignore empty SAs without warning */
278 		return (true);
279         default:
280                 NL_LOG(LOG_DEBUG2, "unsupported family: %d, skipping", sa->sa_family);
281                 return (true);
282         }
283 
284         return (nlattr_add(nw, attr, addr_len, addr_data));
285 }
286 
287 static bool
dump_iface_caps(struct nl_writer * nw,struct ifnet * ifp)288 dump_iface_caps(struct nl_writer *nw, struct ifnet *ifp)
289 {
290 	int off = nlattr_add_nested(nw, IFLAF_CAPS);
291 	uint32_t active_caps[roundup2(IFCAP_B_SIZE, 32) / 32] = {};
292 	uint32_t all_caps[roundup2(IFCAP_B_SIZE, 32) / 32] = {};
293 
294 	MPASS(sizeof(active_caps) >= 8);
295 	MPASS(sizeof(all_caps) >= 8);
296 
297 	if (off == 0)
298 		return (false);
299 
300 	active_caps[0] = (uint32_t)if_getcapabilities(ifp);
301 	all_caps[0] = (uint32_t)if_getcapenable(ifp);
302 	active_caps[1] = (uint32_t)if_getcapabilities2(ifp);
303 	all_caps[1] = (uint32_t)if_getcapenable2(ifp);
304 
305 	nlattr_add_u32(nw, NLA_BITSET_SIZE, IFCAP_B_SIZE);
306 	nlattr_add(nw, NLA_BITSET_MASK, sizeof(all_caps), all_caps);
307 	nlattr_add(nw, NLA_BITSET_VALUE, sizeof(active_caps), active_caps);
308 
309 	nlattr_set_len(nw, off);
310 
311 	return (true);
312 }
313 
314 /*
315  * Dumps interface state, properties and metrics.
316  * @nw: message writer
317  * @ifp: target interface
318  * @hdr: template header
319  * @if_flags_mask: changed if_[drv]_flags bitmask
320  *
321  * This function is called without epoch and MAY sleep.
322  */
323 static bool
dump_iface(struct nl_writer * nw,if_t ifp,const struct nlmsghdr * hdr,int if_flags_mask,const char * ifname)324 dump_iface(struct nl_writer *nw, if_t ifp, const struct nlmsghdr *hdr,
325     int if_flags_mask, const char *ifname)
326 {
327 	struct epoch_tracker et;
328         struct ifinfomsg *ifinfo;
329 
330         NL_LOG(LOG_DEBUG3, "dumping interface %s data", if_name(ifp));
331 
332 	if (!nlmsg_reply(nw, hdr, sizeof(struct ifinfomsg)))
333 		goto enomem;
334 
335         ifinfo = nlmsg_reserve_object(nw, struct ifinfomsg);
336         ifinfo->ifi_family = AF_UNSPEC;
337         ifinfo->__ifi_pad = 0;
338         ifinfo->ifi_type = if_gettype(ifp);
339         ifinfo->ifi_index = if_getindex(ifp);
340         ifinfo->ifi_flags = ifp_flags_to_netlink(ifp);
341         ifinfo->ifi_change = if_flags_mask;
342 
343 	struct if_state ifs = {};
344 	get_operstate(ifp, &ifs);
345 
346 	if (ifs.ifla_operstate == IF_OPER_UP)
347 		ifinfo->ifi_flags |= IFF_LOWER_UP;
348 
349         nlattr_add_string(nw, IFLA_IFNAME,
350 	    ifname != NULL ? ifname : if_name(ifp));
351         nlattr_add_u8(nw, IFLA_OPERSTATE, ifs.ifla_operstate);
352         nlattr_add_u8(nw, IFLA_CARRIER, ifs.ifla_carrier);
353 
354 /*
355         nlattr_add_u8(nw, IFLA_PROTO_DOWN, val);
356         nlattr_add_u8(nw, IFLA_LINKMODE, val);
357 */
358 	if (if_getaddrlen(ifp) != 0) {
359 		struct ifaddr *ifa;
360 		struct ifa_iter it;
361 
362 		NET_EPOCH_ENTER(et);
363 		ifa = ifa_iter_start(ifp, &it);
364 		if (ifa != NULL)
365 			dump_sa(nw, IFLA_ADDRESS, ifa->ifa_addr);
366 		ifa_iter_finish(&it);
367 		NET_EPOCH_EXIT(et);
368 	}
369 
370         if ((if_getbroadcastaddr(ifp) != NULL)) {
371 		nlattr_add(nw, IFLA_BROADCAST, if_getaddrlen(ifp),
372 		    if_getbroadcastaddr(ifp));
373         }
374 
375         nlattr_add_u32(nw, IFLA_MTU, if_getmtu(ifp));
376 /*
377         nlattr_add_u32(nw, IFLA_MIN_MTU, 60);
378         nlattr_add_u32(nw, IFLA_MAX_MTU, 9000);
379 */
380 
381 	if (if_getdescr(ifp) != NULL)
382 		nlattr_add_string(nw, IFLA_IFALIAS, if_getdescr(ifp));
383 
384 	/* Store FreeBSD-specific attributes */
385 	int off = nlattr_add_nested(nw, IFLA_FREEBSD);
386 	if (off != 0) {
387 		get_hwaddr(nw, ifp);
388 		dump_iface_caps(nw, ifp);
389 		if (dump_group(nw, ifp) != 0)
390 			goto enomem;
391 
392 		nlattr_set_len(nw, off);
393 	}
394 
395 	get_stats(nw, ifp);
396 
397 	uint32_t val = (if_getflags(ifp) & IFF_PROMISC) != 0;
398         nlattr_add_u32(nw, IFLA_PROMISCUITY, val);
399 
400 	ifc_dump_ifp_nl(ifp, nw);
401 
402 	nw->ifp = ifp;
403 
404         if (nlmsg_end(nw))
405 		return (true);
406 
407 enomem:
408         NL_LOG(LOG_DEBUG, "unable to dump interface %s state (ENOMEM)", if_name(ifp));
409         nlmsg_abort(nw);
410         return (false);
411 }
412 
413 static bool
check_ifmsg(void * hdr,struct nl_pstate * npt)414 check_ifmsg(void *hdr, struct nl_pstate *npt)
415 {
416 	struct ifinfomsg *ifm = hdr;
417 
418 	if (ifm->__ifi_pad != 0 || ifm->ifi_type != 0 ||
419 	    ifm->ifi_flags != 0 || ifm->ifi_change != 0) {
420 		nlmsg_report_err_msg(npt,
421 		    "strict checking: non-zero values in ifinfomsg header");
422 		return (false);
423 	}
424 
425 	return (true);
426 }
427 
428 #define	_IN(_field)	offsetof(struct ifinfomsg, _field)
429 #define	_OUT(_field)	offsetof(struct nl_parsed_link, _field)
430 static const struct nlfield_parser nlf_p_if[] = {
431 	{ .off_in = _IN(ifi_type), .off_out = _OUT(ifi_type), .cb = nlf_get_u16 },
432 	{ .off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = nlf_get_u32 },
433 	{ .off_in = _IN(ifi_flags), .off_out = _OUT(ifi_flags), .cb = nlf_get_u32 },
434 	{ .off_in = _IN(ifi_change), .off_out = _OUT(ifi_change), .cb = nlf_get_u32 },
435 };
436 
437 static const struct nlattr_parser nla_p_linfo[] = {
438 	{ .type = IFLA_INFO_KIND, .off = _OUT(ifla_cloner), .cb = nlattr_get_stringn },
439 	{ .type = IFLA_INFO_DATA, .off = _OUT(ifla_idata), .cb = nlattr_get_nla },
440 };
441 NL_DECLARE_ATTR_PARSER(linfo_parser, nla_p_linfo);
442 
443 static const struct nlattr_parser nla_p_if[] = {
444 	{ .type = IFLA_ADDRESS, .off = _OUT(ifla_address), .cb = nlattr_get_nla },
445 	{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string },
446 	{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = nlattr_get_uint32 },
447 	{ .type = IFLA_LINK, .off = _OUT(ifla_link), .cb = nlattr_get_uint32 },
448 	{ .type = IFLA_LINKINFO, .arg = &linfo_parser, .cb = nlattr_get_nested },
449 	{ .type = IFLA_IFALIAS, .off = _OUT(ifla_ifalias), .cb = nlattr_get_string },
450 	{ .type = IFLA_GROUP, .off = _OUT(ifla_group), .cb = nlattr_get_string },
451 	{ .type = IFLA_ALT_IFNAME, .off = _OUT(ifla_ifname), .cb = nlattr_get_string },
452 };
453 #undef _IN
454 #undef _OUT
455 NL_DECLARE_STRICT_PARSER(ifmsg_parser, struct ifinfomsg, check_ifmsg, nlf_p_if, nla_p_if);
456 
457 static bool
match_iface(if_t ifp,void * _arg)458 match_iface(if_t ifp, void *_arg)
459 {
460 	struct nl_parsed_link *attrs = (struct nl_parsed_link *)_arg;
461 
462 	if (attrs->ifi_index != 0 && attrs->ifi_index != if_getindex(ifp))
463 		return (false);
464 	if (attrs->ifi_type != 0 && attrs->ifi_type != if_gettype(ifp))
465 		return (false);
466 	if (attrs->ifla_ifname != NULL && strcmp(attrs->ifla_ifname, if_name(ifp)))
467 		return (false);
468 	/* TODO: add group match */
469 
470 	return (true);
471 }
472 
473 static int
dump_cb(if_t ifp,void * _arg)474 dump_cb(if_t ifp, void *_arg)
475 {
476 	struct netlink_walkargs *wa = (struct netlink_walkargs *)_arg;
477 	if (!dump_iface(wa->nw, ifp, &wa->hdr, 0, NULL))
478 		return (ENOMEM);
479 	return (0);
480 }
481 
482 /*
483  * {nlmsg_len=52, nlmsg_type=RTM_GETLINK, nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=1662842818, nlmsg_pid=0},
484  *  {ifi_family=AF_PACKET, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
485  *   [
486  *    [{nla_len=10, nla_type=IFLA_IFNAME}, "vnet9"],
487  *    [{nla_len=8, nla_type=IFLA_EXT_MASK}, RTEXT_FILTER_VF]
488  *   ]
489  */
490 static int
rtnl_handle_getlink(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)491 rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
492 {
493 	struct epoch_tracker et;
494         if_t ifp;
495 	int error = 0;
496 
497 	struct nl_parsed_link attrs = {};
498 	error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
499 	if (error != 0)
500 		return (error);
501 
502 	struct netlink_walkargs wa = {
503 		.so = nlp,
504 		.nw = npt->nw,
505 		.hdr.nlmsg_pid = hdr->nlmsg_pid,
506 		.hdr.nlmsg_seq = hdr->nlmsg_seq,
507 		.hdr.nlmsg_flags = hdr->nlmsg_flags,
508 		.hdr.nlmsg_type = NL_RTM_NEWLINK,
509 	};
510 
511 	/* Fast track for an interface w/ explicit name or index match */
512 	if ((attrs.ifi_index != 0) || (attrs.ifla_ifname != NULL)) {
513 		if (attrs.ifi_index != 0) {
514 			NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching index %u",
515 			    attrs.ifi_index);
516 			NET_EPOCH_ENTER(et);
517 			ifp = ifnet_byindex_ref(attrs.ifi_index);
518 			NET_EPOCH_EXIT(et);
519 		} else {
520 			NLP_LOG(LOG_DEBUG3, nlp, "fast track -> searching name %s",
521 			    attrs.ifla_ifname);
522 			ifp = ifunit_ref(attrs.ifla_ifname);
523 		}
524 
525 		if (ifp != NULL) {
526 			if (match_iface(ifp, &attrs)) {
527 				if (!dump_iface(wa.nw, ifp, &wa.hdr, 0, NULL))
528 					error = ENOMEM;
529 			} else
530 				error = ENODEV;
531 			if_rele(ifp);
532 		} else
533 			error = ENODEV;
534 		return (error);
535 	}
536 
537 	/* Always treat non-direct-match as a multipart message */
538 	wa.hdr.nlmsg_flags |= NLM_F_MULTI;
539 
540 	/*
541 	 * Fetching some link properties require performing ioctl's that may be blocking.
542 	 * Address it by saving referenced pointers of the matching links,
543 	 * exiting from epoch and going through the list one-by-one.
544 	 */
545 
546 	NL_LOG(LOG_DEBUG2, "Start dump");
547 	if_foreach_sleep(match_iface, &attrs, dump_cb, &wa);
548 	NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped);
549 
550 	if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) {
551                 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
552                 return (ENOMEM);
553         }
554 
555 	return (error);
556 }
557 
558 /*
559  * sendmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base=[
560  * {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},
561  *  {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
562  *   {nla_len=11, nla_type=IFLA_IFNAME}, "dummy0"],
563  *   [
564  *    {nla_len=16, nla_type=IFLA_LINKINFO},
565  *     [
566  *      {nla_len=9, nla_type=IFLA_INFO_KIND}, "dummy"...
567  *     ]
568  *    ]
569  */
570 
571 static int
rtnl_handle_dellink(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)572 rtnl_handle_dellink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
573 {
574 	struct epoch_tracker et;
575         if_t ifp;
576 	int error;
577 
578 	struct nl_parsed_link attrs = {};
579 	error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
580 	if (error != 0)
581 		return (error);
582 
583 	NET_EPOCH_ENTER(et);
584 	ifp = ifnet_byindex_ref(attrs.ifi_index);
585 	NET_EPOCH_EXIT(et);
586 	if (ifp == NULL) {
587 		NLP_LOG(LOG_DEBUG, nlp, "unable to find interface %u", attrs.ifi_index);
588 		return (ENOENT);
589 	}
590 	NLP_LOG(LOG_DEBUG3, nlp, "mapped ifindex %u to %s", attrs.ifi_index, if_name(ifp));
591 
592 	error = if_clone_destroy(if_name(ifp));
593 	NLP_LOG(LOG_DEBUG2, nlp, "deleting interface %s returned %d", if_name(ifp), error);
594 
595 	if_rele(ifp);
596 	return (error);
597 }
598 
599 /*
600  * New link:
601  * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1668185590, pid=0},
602  *   {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}
603  *    [
604  *     {{nla_len=8, nla_type=IFLA_MTU}, 123},
605  *     {{nla_len=10, nla_type=IFLA_IFNAME}, "vlan1"},
606  *     {{nla_len=24, nla_type=IFLA_LINKINFO},
607  *      [
608  *       {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
609  *       {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x7b\x00\x00\x00"}]}]}
610  *
611  * Update link:
612  * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=1668185923, pid=0},
613  * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=if_nametoindex("lo"), ifi_flags=0, ifi_change=0},
614  * {{nla_len=8, nla_type=IFLA_MTU}, 123}}
615  *
616  *
617  * Check command availability:
618  * type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK, seq=0, pid=0},
619  *  {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0}
620  */
621 
622 
623 static int
create_link(struct nlmsghdr * hdr,struct nl_parsed_link * lattrs,struct nlattr_bmask * bm,struct nlpcb * nlp,struct nl_pstate * npt)624 create_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs,
625     struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
626 {
627 	if (lattrs->ifla_ifname == NULL || strlen(lattrs->ifla_ifname) == 0) {
628 		NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_IFNAME attribute");
629 		return (EINVAL);
630 	}
631 	if (lattrs->ifla_cloner == NULL || strlen(lattrs->ifla_cloner) == 0) {
632 		NLMSG_REPORT_ERR_MSG(npt, "empty IFLA_INFO_KIND attribute");
633 		return (EINVAL);
634 	}
635 
636 	struct ifc_data_nl ifd = {
637 		.flags = IFC_F_CREATE,
638 		.lattrs = lattrs,
639 		.bm = bm,
640 		.npt = npt,
641 	};
642 	if (ifc_create_ifp_nl(lattrs->ifla_ifname, &ifd) && ifd.error == 0)
643 		nl_store_ifp_cookie(npt, ifd.ifp);
644 
645 	return (ifd.error);
646 }
647 
648 static int
modify_link(struct nlmsghdr * hdr,struct nl_parsed_link * lattrs,struct nlattr_bmask * bm,struct nlpcb * nlp,struct nl_pstate * npt)649 modify_link(struct nlmsghdr *hdr, struct nl_parsed_link *lattrs,
650     struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
651 {
652 	if_t ifp = NULL;
653 	struct epoch_tracker et;
654 
655 	if (lattrs->ifi_index == 0 && lattrs->ifla_ifname == NULL) {
656 		/*
657 		 * Applications like ip(8) verify RTM_NEWLINK command
658 		 * existence by calling it with empty arguments. Always
659 		 * return "innocent" error in that case.
660 		 */
661 		NLMSG_REPORT_ERR_MSG(npt, "empty ifi_index field");
662 		return (EPERM);
663 	}
664 
665 	if (lattrs->ifi_index != 0) {
666 		NET_EPOCH_ENTER(et);
667 		ifp = ifnet_byindex_ref(lattrs->ifi_index);
668 		NET_EPOCH_EXIT(et);
669 		if (ifp == NULL) {
670 			NLMSG_REPORT_ERR_MSG(npt, "unable to find interface #%u",
671 			    lattrs->ifi_index);
672 			return (ENOENT);
673 		}
674 	}
675 
676 	if (ifp == NULL && lattrs->ifla_ifname != NULL) {
677 		ifp = ifunit_ref(lattrs->ifla_ifname);
678 		if (ifp == NULL) {
679 			NLMSG_REPORT_ERR_MSG(npt, "unable to find interface %s",
680 			    lattrs->ifla_ifname);
681 			return (ENOENT);
682 		}
683 	}
684 
685 	MPASS(ifp != NULL);
686 
687 	/*
688 	 * Modification request can address either
689 	 * 1) cloned interface, in which case we call the cloner-specific
690 	 *  modification routine
691 	 * or
692 	 * 2) non-cloned (e.g. "physical") interface, in which case we call
693 	 *  generic modification routine
694 	 */
695 	struct ifc_data_nl ifd = { .lattrs = lattrs, .bm = bm, .npt = npt };
696 	if (!ifc_modify_ifp_nl(ifp, &ifd))
697 		ifd.error = nl_modify_ifp_generic(ifp, lattrs, bm, npt);
698 
699 	if_rele(ifp);
700 
701 	return (ifd.error);
702 }
703 
704 
705 static int
rtnl_handle_newlink(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)706 rtnl_handle_newlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
707 {
708 	struct nlattr_bmask bm;
709 	struct thread *td = curthread;
710 	struct ucred *cred;
711 	int error;
712 
713 	struct nl_parsed_link attrs = {};
714 	error = nl_parse_nlmsg(hdr, &ifmsg_parser, npt, &attrs);
715 	if (error != 0)
716 		return (error);
717 	nl_get_attrs_bmask_nlmsg(hdr, &ifmsg_parser, &bm);
718 
719 	/* XXX: temporary patch until the D39180 review lands */
720 	cred = td->td_ucred;
721 	td->td_ucred = nlp_get_cred(nlp);
722 	if (hdr->nlmsg_flags & NLM_F_CREATE)
723 		error = create_link(hdr, &attrs, &bm, nlp, npt);
724 	else
725 		error = modify_link(hdr, &attrs, &bm, nlp, npt);
726 	td->td_ucred = cred;
727 
728 	return (error);
729 }
730 
731 static void
set_scope6(struct sockaddr * sa,uint32_t ifindex)732 set_scope6(struct sockaddr *sa, uint32_t ifindex)
733 {
734 #ifdef INET6
735 	if (sa != NULL && sa->sa_family == AF_INET6) {
736 		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
737 
738 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
739 			in6_set_unicast_scopeid(&sa6->sin6_addr, ifindex);
740 	}
741 #endif
742 }
743 
744 static bool
check_sa_family(const struct sockaddr * sa,int family,const char * attr_name,struct nl_pstate * npt)745 check_sa_family(const struct sockaddr *sa, int family, const char *attr_name,
746     struct nl_pstate *npt)
747 {
748 	if (sa == NULL || sa->sa_family == family)
749 		return (true);
750 
751 	nlmsg_report_err_msg(npt, "wrong family for %s attribute: %d != %d",
752 	    attr_name, family, sa->sa_family);
753 	return (false);
754 }
755 
756 struct nl_parsed_ifa {
757 	uint8_t			ifa_family;
758 	uint8_t			ifa_prefixlen;
759 	uint8_t			ifa_scope;
760 	uint32_t		ifa_index;
761 	uint32_t		ifa_flags;
762 	uint32_t		ifaf_vhid;
763 	uint32_t		ifaf_flags;
764 	struct sockaddr		*ifa_address;
765 	struct sockaddr		*ifa_local;
766 	struct sockaddr		*ifa_broadcast;
767 	struct ifa_cacheinfo	*ifa_cacheinfo;
768 	struct sockaddr		*f_ifa_addr;
769 	struct sockaddr		*f_ifa_dst;
770 };
771 
772 static int
nlattr_get_cinfo(struct nlattr * nla,struct nl_pstate * npt,const void * arg __unused,void * target)773 nlattr_get_cinfo(struct nlattr *nla, struct nl_pstate *npt,
774     const void *arg __unused, void *target)
775 {
776 	if (__predict_false(NLA_DATA_LEN(nla) != sizeof(struct ifa_cacheinfo))) {
777 		NLMSG_REPORT_ERR_MSG(npt, "nla type %d size(%u) is not ifa_cacheinfo",
778 		    nla->nla_type, NLA_DATA_LEN(nla));
779 		return (EINVAL);
780 	}
781 	*((struct ifa_cacheinfo **)target) = (struct ifa_cacheinfo *)NL_RTA_DATA(nla);
782 	return (0);
783 }
784 
785 #define	_IN(_field)	offsetof(struct ifaddrmsg, _field)
786 #define	_OUT(_field)	offsetof(struct nl_parsed_ifa, _field)
787 static const struct nlfield_parser nlf_p_ifa[] = {
788 	{ .off_in = _IN(ifa_family), .off_out = _OUT(ifa_family), .cb = nlf_get_u8 },
789 	{ .off_in = _IN(ifa_prefixlen), .off_out = _OUT(ifa_prefixlen), .cb = nlf_get_u8 },
790 	{ .off_in = _IN(ifa_scope), .off_out = _OUT(ifa_scope), .cb = nlf_get_u8 },
791 	{ .off_in = _IN(ifa_flags), .off_out = _OUT(ifa_flags), .cb = nlf_get_u8_u32 },
792 	{ .off_in = _IN(ifa_index), .off_out = _OUT(ifa_index), .cb = nlf_get_u32 },
793 };
794 
795 static const struct nlattr_parser nla_p_ifa_fbsd[] = {
796 	{ .type = IFAF_VHID, .off = _OUT(ifaf_vhid), .cb = nlattr_get_uint32 },
797 	{ .type = IFAF_FLAGS, .off = _OUT(ifaf_flags), .cb = nlattr_get_uint32 },
798 };
799 NL_DECLARE_ATTR_PARSER(ifa_fbsd_parser, nla_p_ifa_fbsd);
800 
801 static const struct nlattr_parser nla_p_ifa[] = {
802 	{ .type = IFA_ADDRESS, .off = _OUT(ifa_address), .cb = nlattr_get_ip },
803 	{ .type = IFA_LOCAL, .off = _OUT(ifa_local), .cb = nlattr_get_ip },
804 	{ .type = IFA_BROADCAST, .off = _OUT(ifa_broadcast), .cb = nlattr_get_ip },
805 	{ .type = IFA_CACHEINFO, .off = _OUT(ifa_cacheinfo), .cb = nlattr_get_cinfo },
806 	{ .type = IFA_FLAGS, .off = _OUT(ifa_flags), .cb = nlattr_get_uint32 },
807 	{ .type = IFA_FREEBSD, .arg = &ifa_fbsd_parser, .cb = nlattr_get_nested },
808 };
809 #undef _IN
810 #undef _OUT
811 
812 static bool
post_p_ifa(void * _attrs,struct nl_pstate * npt)813 post_p_ifa(void *_attrs, struct nl_pstate *npt)
814 {
815 	struct nl_parsed_ifa *attrs = (struct nl_parsed_ifa *)_attrs;
816 
817 	if (!check_sa_family(attrs->ifa_address, attrs->ifa_family, "IFA_ADDRESS", npt))
818 		return (false);
819 	if (!check_sa_family(attrs->ifa_local, attrs->ifa_family, "IFA_LOCAL", npt))
820 		return (false);
821 	if (!check_sa_family(attrs->ifa_broadcast, attrs->ifa_family, "IFA_BROADADDR", npt))
822 		return (false);
823 
824 	set_scope6(attrs->ifa_address, attrs->ifa_index);
825 	set_scope6(attrs->ifa_local, attrs->ifa_index);
826 
827 	return (true);
828 }
829 
830 NL_DECLARE_PARSER_EXT(ifa_parser, struct ifaddrmsg, NULL, nlf_p_ifa, nla_p_ifa, post_p_ifa);
831 
832 
833 /*
834 
835 {ifa_family=AF_INET, ifa_prefixlen=8, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_HOST, ifa_index=if_nametoindex("lo")},
836  [
837         {{nla_len=8, nla_type=IFA_ADDRESS}, inet_addr("127.0.0.1")},
838         {{nla_len=8, nla_type=IFA_LOCAL}, inet_addr("127.0.0.1")},
839         {{nla_len=7, nla_type=IFA_LABEL}, "lo"},
840         {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT},
841         {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=3619, tstamp=3619}}]},
842 ---
843 
844 {{len=72, type=RTM_NEWADDR, flags=NLM_F_MULTI, seq=1642191126, pid=566735},
845  {ifa_family=AF_INET6, ifa_prefixlen=96, ifa_flags=IFA_F_PERMANENT, ifa_scope=RT_SCOPE_UNIVERSE, ifa_index=if_nametoindex("virbr0")},
846    [
847     {{nla_len=20, nla_type=IFA_ADDRESS}, inet_pton(AF_INET6, "2a01:4f8:13a:70c:ffff::1")},
848    {{nla_len=20, nla_type=IFA_CACHEINFO}, {ifa_prefered=4294967295, ifa_valid=4294967295, cstamp=4283, tstamp=4283}},
849    {{nla_len=8, nla_type=IFA_FLAGS}, IFA_F_PERMANENT}]},
850 */
851 
852 static uint8_t
ifa_get_scope(const struct ifaddr * ifa)853 ifa_get_scope(const struct ifaddr *ifa)
854 {
855         const struct sockaddr *sa;
856         uint8_t addr_scope = RT_SCOPE_UNIVERSE;
857 
858         sa = ifa->ifa_addr;
859         switch (sa->sa_family) {
860 #ifdef INET
861         case AF_INET:
862                 {
863                         struct in_addr addr;
864                         addr = ((const struct sockaddr_in *)sa)->sin_addr;
865                         if (IN_LOOPBACK(ntohl(addr.s_addr)))
866                                 addr_scope = RT_SCOPE_HOST;
867                         else if (IN_LINKLOCAL(ntohl(addr.s_addr)))
868                                 addr_scope = RT_SCOPE_LINK;
869                         break;
870                 }
871 #endif
872 #ifdef INET6
873         case AF_INET6:
874                 {
875                         const struct in6_addr *addr;
876                         addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
877                         if (IN6_IS_ADDR_LOOPBACK(addr))
878                                 addr_scope = RT_SCOPE_HOST;
879                         else if (IN6_IS_ADDR_LINKLOCAL(addr))
880                                 addr_scope = RT_SCOPE_LINK;
881                         break;
882                 }
883 #endif
884         }
885 
886         return (addr_scope);
887 }
888 
889 #ifdef INET6
890 static uint8_t
inet6_get_plen(const struct in6_addr * addr)891 inet6_get_plen(const struct in6_addr *addr)
892 {
893 
894 	return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
895 	    bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
896 }
897 #endif
898 
899 static uint8_t
get_sa_plen(const struct sockaddr * sa)900 get_sa_plen(const struct sockaddr *sa)
901 {
902 #ifdef INET
903         const struct in_addr *paddr;
904 #endif
905 #ifdef INET6
906         const struct in6_addr *paddr6;
907 #endif
908 
909         switch (sa->sa_family) {
910 #ifdef INET
911         case AF_INET:
912                 paddr = &(((const struct sockaddr_in *)sa)->sin_addr);
913                 return bitcount32(paddr->s_addr);
914 #endif
915 #ifdef INET6
916         case AF_INET6:
917                 paddr6 = &(((const struct sockaddr_in6 *)sa)->sin6_addr);
918                 return inet6_get_plen(paddr6);
919 #endif
920         }
921 
922         return (0);
923 }
924 
925 #ifdef INET6
926 static uint32_t
in6_flags_to_nl(uint32_t flags)927 in6_flags_to_nl(uint32_t flags)
928 {
929 	uint32_t nl_flags = 0;
930 
931 	if (flags & IN6_IFF_TEMPORARY)
932 		nl_flags |= IFA_F_TEMPORARY;
933 	if (flags & IN6_IFF_NODAD)
934 		nl_flags |= IFA_F_NODAD;
935 	if (flags & IN6_IFF_DEPRECATED)
936 		nl_flags |= IFA_F_DEPRECATED;
937 	if (flags & IN6_IFF_TENTATIVE)
938 		nl_flags |= IFA_F_TENTATIVE;
939 	if ((flags & (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) == 0)
940 		flags |= IFA_F_PERMANENT;
941 	if (flags & IN6_IFF_DUPLICATED)
942 		flags |= IFA_F_DADFAILED;
943 	return (nl_flags);
944 }
945 
946 static uint32_t
nl_flags_to_in6(uint32_t flags)947 nl_flags_to_in6(uint32_t flags)
948 {
949 	uint32_t in6_flags = 0;
950 
951 	if (flags & IFA_F_TEMPORARY)
952 		in6_flags |= IN6_IFF_TEMPORARY;
953 	if (flags & IFA_F_NODAD)
954 		in6_flags |= IN6_IFF_NODAD;
955 	if (flags & IFA_F_DEPRECATED)
956 		in6_flags |= IN6_IFF_DEPRECATED;
957 	if (flags & IFA_F_TENTATIVE)
958 		in6_flags |= IN6_IFF_TENTATIVE;
959 	if (flags & IFA_F_DADFAILED)
960 		in6_flags |= IN6_IFF_DUPLICATED;
961 
962 	return (in6_flags);
963 }
964 
965 static void
export_cache_info6(struct nl_writer * nw,const struct in6_ifaddr * ia)966 export_cache_info6(struct nl_writer *nw, const struct in6_ifaddr *ia)
967 {
968 	struct ifa_cacheinfo ci = {
969 		.cstamp = ia->ia6_createtime * 1000,
970 		.tstamp = ia->ia6_updatetime * 1000,
971 		.ifa_prefered = ia->ia6_lifetime.ia6t_pltime,
972 		.ifa_valid = ia->ia6_lifetime.ia6t_vltime,
973 	};
974 
975 	nlattr_add(nw, IFA_CACHEINFO, sizeof(ci), &ci);
976 }
977 #endif
978 
979 static void
export_cache_info(struct nl_writer * nw,struct ifaddr * ifa)980 export_cache_info(struct nl_writer *nw, struct ifaddr *ifa)
981 {
982 	switch (ifa->ifa_addr->sa_family) {
983 #ifdef INET6
984 	case AF_INET6:
985 		export_cache_info6(nw, (struct in6_ifaddr *)ifa);
986 		break;
987 #endif
988 	}
989 }
990 
991 /*
992  * {'attrs': [('IFA_ADDRESS', '12.0.0.1'),
993            ('IFA_LOCAL', '12.0.0.1'),
994            ('IFA_LABEL', 'eth10'),
995            ('IFA_FLAGS', 128),
996            ('IFA_CACHEINFO', {'ifa_preferred': 4294967295, 'ifa_valid': 4294967295, 'cstamp': 63745746, 'tstamp': 63745746})],
997  */
998 static bool
dump_iface_addr(struct nl_writer * nw,if_t ifp,struct ifaddr * ifa,const struct nlmsghdr * hdr)999 dump_iface_addr(struct nl_writer *nw, if_t ifp, struct ifaddr *ifa,
1000     const struct nlmsghdr *hdr)
1001 {
1002         struct ifaddrmsg *ifamsg;
1003         struct sockaddr *sa = ifa->ifa_addr;
1004         struct sockaddr *sa_dst = ifa->ifa_dstaddr;
1005 
1006         NL_LOG(LOG_DEBUG3, "dumping ifa %p type %s(%d) for interface %s",
1007             ifa, rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp));
1008 
1009 	if (!nlmsg_reply(nw, hdr, sizeof(struct ifaddrmsg)))
1010 		goto enomem;
1011 
1012         ifamsg = nlmsg_reserve_object(nw, struct ifaddrmsg);
1013         ifamsg->ifa_family = sa->sa_family;
1014         ifamsg->ifa_prefixlen = get_sa_plen(ifa->ifa_netmask);
1015         ifamsg->ifa_flags = 0; // ifa_flags is useless
1016         ifamsg->ifa_scope = ifa_get_scope(ifa);
1017         ifamsg->ifa_index = if_getindex(ifp);
1018 
1019 	if ((if_getflags(ifp) & IFF_POINTOPOINT) && sa_dst != NULL && sa_dst->sa_family != 0) {
1020 		/* P2P interface may have IPv6 LL with no dst address */
1021 		dump_sa(nw, IFA_ADDRESS, sa_dst);
1022 		dump_sa(nw, IFA_LOCAL, sa);
1023 	} else {
1024 		dump_sa(nw, IFA_ADDRESS, sa);
1025 #ifdef INET
1026 		/*
1027 		 * In most cases, IFA_ADDRESS == IFA_LOCAL
1028 		 * Skip IFA_LOCAL for anything except INET
1029 		 */
1030 		if (sa->sa_family == AF_INET)
1031 			dump_sa(nw, IFA_LOCAL, sa);
1032 #endif
1033 	}
1034 	if (if_getflags(ifp) & IFF_BROADCAST)
1035 		dump_sa(nw, IFA_BROADCAST, ifa->ifa_broadaddr);
1036 
1037         nlattr_add_string(nw, IFA_LABEL, if_name(ifp));
1038 
1039         uint32_t nl_ifa_flags = 0;
1040 #ifdef INET6
1041 	if (sa->sa_family == AF_INET6) {
1042 		struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1043 		nl_ifa_flags = in6_flags_to_nl(ia->ia6_flags);
1044 	}
1045 #endif
1046         nlattr_add_u32(nw, IFA_FLAGS, nl_ifa_flags);
1047 
1048 	export_cache_info(nw, ifa);
1049 
1050 	/* Store FreeBSD-specific attributes */
1051 	int off = nlattr_add_nested(nw, IFA_FREEBSD);
1052 	if (off != 0) {
1053 		if (ifa->ifa_carp != NULL && carp_get_vhid_p != NULL) {
1054 			uint32_t vhid  = (uint32_t)(*carp_get_vhid_p)(ifa);
1055 			nlattr_add_u32(nw, IFAF_VHID, vhid);
1056 		}
1057 #ifdef INET6
1058 		if (sa->sa_family == AF_INET6) {
1059 			uint32_t ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
1060 
1061 			nlattr_add_u32(nw, IFAF_FLAGS, ifa_flags);
1062 		}
1063 #endif
1064 
1065 		nlattr_set_len(nw, off);
1066 	}
1067 
1068 	if (nlmsg_end(nw))
1069 		return (true);
1070 enomem:
1071         NL_LOG(LOG_DEBUG, "Failed to dump ifa type %s(%d) for interface %s",
1072             rib_print_family(sa->sa_family), sa->sa_family, if_name(ifp));
1073         nlmsg_abort(nw);
1074         return (false);
1075 }
1076 
1077 static int
dump_iface_addrs(struct netlink_walkargs * wa,if_t ifp)1078 dump_iface_addrs(struct netlink_walkargs *wa, if_t ifp)
1079 {
1080         struct ifaddr *ifa;
1081 	struct ifa_iter it;
1082 	int error = 0;
1083 
1084 	for (ifa = ifa_iter_start(ifp, &it); ifa != NULL; ifa = ifa_iter_next(&it)) {
1085 		if (wa->family != 0 && wa->family != ifa->ifa_addr->sa_family)
1086 			continue;
1087 		if (ifa->ifa_addr->sa_family == AF_LINK)
1088 			continue;
1089 		if (prison_if(wa->cred, ifa->ifa_addr) != 0)
1090 			continue;
1091 		wa->count++;
1092 		if (!dump_iface_addr(wa->nw, ifp, ifa, &wa->hdr)) {
1093 			error = ENOMEM;
1094 			break;
1095 		}
1096 		wa->dumped++;
1097 	}
1098 	ifa_iter_finish(&it);
1099 
1100 	return (error);
1101 }
1102 
1103 static int
rtnl_handle_getaddr(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)1104 rtnl_handle_getaddr(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
1105 {
1106         if_t ifp;
1107 	int error = 0;
1108 
1109 	struct nl_parsed_ifa attrs = {};
1110 	error = nl_parse_nlmsg(hdr, &ifa_parser, npt, &attrs);
1111 	if (error != 0)
1112 		return (error);
1113 
1114 	struct netlink_walkargs wa = {
1115 		.so = nlp,
1116 		.nw = npt->nw,
1117 		.cred = nlp_get_cred(nlp),
1118 		.family = attrs.ifa_family,
1119 		.hdr.nlmsg_pid = hdr->nlmsg_pid,
1120 		.hdr.nlmsg_seq = hdr->nlmsg_seq,
1121 		.hdr.nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI,
1122 		.hdr.nlmsg_type = NL_RTM_NEWADDR,
1123 	};
1124 
1125 	NL_LOG(LOG_DEBUG2, "Start dump");
1126 
1127 	if (attrs.ifa_index != 0) {
1128 		ifp = ifnet_byindex(attrs.ifa_index);
1129 		if (ifp == NULL)
1130 			error = ENOENT;
1131 		else
1132 			error = dump_iface_addrs(&wa, ifp);
1133 	} else {
1134 		struct if_iter it;
1135 
1136 		for (ifp = if_iter_start(&it); ifp != NULL; ifp = if_iter_next(&it)) {
1137 			error = dump_iface_addrs(&wa, ifp);
1138 			if (error != 0)
1139 				break;
1140 		}
1141 		if_iter_finish(&it);
1142 	}
1143 
1144 	NL_LOG(LOG_DEBUG2, "End dump, iterated %d dumped %d", wa.count, wa.dumped);
1145 
1146 	if (!nlmsg_end_dump(wa.nw, error, &wa.hdr)) {
1147                 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
1148                 return (ENOMEM);
1149         }
1150 
1151 	return (error);
1152 }
1153 
1154 #ifdef INET
1155 static int
handle_newaddr_inet(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1156 handle_newaddr_inet(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1157     if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1158 {
1159 	int plen = attrs->ifa_prefixlen;
1160 	int if_flags = if_getflags(ifp);
1161 	struct sockaddr_in *addr, *dst;
1162 
1163 	if (plen > 32) {
1164 		nlmsg_report_err_msg(npt, "invalid ifa_prefixlen");
1165 		return (EINVAL);
1166 	};
1167 
1168 	if (if_flags & IFF_POINTOPOINT) {
1169 		/*
1170 		 * Only P2P IFAs are allowed by the implementation.
1171 		 */
1172 		if (attrs->ifa_address == NULL || attrs->ifa_local == NULL) {
1173 			nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1174 			return (EINVAL);
1175 		}
1176 		addr = (struct sockaddr_in *)attrs->ifa_local;
1177 		dst = (struct sockaddr_in *)attrs->ifa_address;
1178 	} else {
1179 		/*
1180 		 * Map the Netlink attributes to FreeBSD ifa layout.
1181 		 * If only IFA_ADDRESS or IFA_LOCAL is set OR
1182 		 * both are set to the same value => ifa is not p2p
1183 		 * and the attribute value contains interface address.
1184 		 *
1185 		 * Otherwise (both IFA_ADDRESS and IFA_LOCAL are set and
1186 		 * different), IFA_LOCAL contains an interface address and
1187 		 * IFA_ADDRESS contains peer address.
1188 		 */
1189 		addr = (struct sockaddr_in *)attrs->ifa_local;
1190 		if (addr == NULL)
1191 			addr = (struct sockaddr_in *)attrs->ifa_address;
1192 
1193 		if (addr == NULL) {
1194 			nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1195 			return (EINVAL);
1196 		}
1197 
1198 		/* Generate broadcast address if not set */
1199 		if ((if_flags & IFF_BROADCAST) && attrs->ifa_broadcast == NULL) {
1200 			uint32_t s_baddr;
1201 			struct sockaddr_in *sin_brd;
1202 
1203 			if (plen == 31)
1204 				s_baddr = INADDR_BROADCAST; /* RFC 3021 */
1205 			else {
1206 				uint32_t s_mask;
1207 
1208 				s_mask = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0);
1209 				s_baddr = addr->sin_addr.s_addr | ~s_mask;
1210 			}
1211 
1212 			sin_brd = (struct sockaddr_in *)npt_alloc(npt, sizeof(*sin_brd));
1213 			if (sin_brd == NULL)
1214 				return (ENOMEM);
1215 			sin_brd->sin_family = AF_INET;
1216 			sin_brd->sin_len = sizeof(*sin_brd);
1217 			sin_brd->sin_addr.s_addr = s_baddr;
1218 			attrs->ifa_broadcast = (struct sockaddr *)sin_brd;
1219 		}
1220 		dst = (struct sockaddr_in *)attrs->ifa_broadcast;
1221 	}
1222 
1223 	struct sockaddr_in mask = {
1224 		.sin_len = sizeof(struct sockaddr_in),
1225 		.sin_family = AF_INET,
1226 		.sin_addr.s_addr = htonl(plen ? ~((1 << (32 - plen)) - 1) : 0),
1227 	};
1228 	struct in_aliasreq req = {
1229 		.ifra_addr = *addr,
1230 		.ifra_mask = mask,
1231 		.ifra_vhid = attrs->ifaf_vhid,
1232 	};
1233 	if (dst != NULL)
1234 		req.ifra_dstaddr = *dst;
1235 
1236 	return (in_control_ioctl(SIOCAIFADDR, &req, ifp, nlp_get_cred(nlp)));
1237 }
1238 
1239 static int
handle_deladdr_inet(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1240 handle_deladdr_inet(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1241     if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1242 {
1243 	struct sockaddr *addr = attrs->ifa_local;
1244 
1245 	if (addr == NULL)
1246 		addr = attrs->ifa_address;
1247 
1248 	if (addr == NULL) {
1249 		nlmsg_report_err_msg(npt, "empty IFA_ADDRESS/IFA_LOCAL");
1250 		return (EINVAL);
1251 	}
1252 
1253 	struct ifreq req = { .ifr_addr = *addr };
1254 
1255 	return (in_control_ioctl(SIOCDIFADDR, &req, ifp, nlp_get_cred(nlp)));
1256 }
1257 #endif
1258 
1259 #ifdef INET6
1260 static int
handle_newaddr_inet6(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1261 handle_newaddr_inet6(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1262     if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1263 {
1264 	struct sockaddr_in6 *addr, *dst;
1265 
1266 	if (attrs->ifa_prefixlen > 128) {
1267 		nlmsg_report_err_msg(npt, "invalid ifa_prefixlen");
1268 		return (EINVAL);
1269 	}
1270 
1271 	/*
1272 	 * In IPv6 implementation, adding non-P2P address to the P2P interface
1273 	 * is allowed.
1274 	 */
1275 	addr = (struct sockaddr_in6 *)(attrs->ifa_local);
1276 	dst = (struct sockaddr_in6 *)(attrs->ifa_address);
1277 
1278 	if (addr == NULL) {
1279 		addr = dst;
1280 		dst = NULL;
1281 	} else if (dst != NULL) {
1282 		if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr, &dst->sin6_addr)) {
1283 			/*
1284 			 * Sometimes Netlink users fills in both attributes
1285 			 * with the same address. It still means "non-p2p".
1286 			 */
1287 			dst = NULL;
1288 		}
1289 	}
1290 
1291 	if (addr == NULL) {
1292 		nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1293 		return (EINVAL);
1294 	}
1295 
1296 	uint32_t flags = nl_flags_to_in6(attrs->ifa_flags) | attrs->ifaf_flags;
1297 
1298 	uint32_t pltime = 0, vltime = 0;
1299 	if (attrs->ifa_cacheinfo != 0) {
1300 		pltime = attrs->ifa_cacheinfo->ifa_prefered;
1301 		vltime = attrs->ifa_cacheinfo->ifa_valid;
1302 	}
1303 
1304 	struct sockaddr_in6 mask = {
1305 		.sin6_len = sizeof(struct sockaddr_in6),
1306 		.sin6_family = AF_INET6,
1307 	};
1308 	ip6_writemask(&mask.sin6_addr, attrs->ifa_prefixlen);
1309 
1310 	struct in6_aliasreq req = {
1311 		.ifra_addr = *addr,
1312 		.ifra_prefixmask = mask,
1313 		.ifra_flags = flags,
1314 		.ifra_lifetime = { .ia6t_vltime = vltime, .ia6t_pltime = pltime },
1315 		.ifra_vhid = attrs->ifaf_vhid,
1316 	};
1317 	if (dst != NULL)
1318 		req.ifra_dstaddr = *dst;
1319 
1320 	return (in6_control_ioctl(SIOCAIFADDR_IN6, &req, ifp, nlp_get_cred(nlp)));
1321 }
1322 
1323 static int
handle_deladdr_inet6(struct nlmsghdr * hdr,struct nl_parsed_ifa * attrs,if_t ifp,struct nlpcb * nlp,struct nl_pstate * npt)1324 handle_deladdr_inet6(struct nlmsghdr *hdr, struct nl_parsed_ifa *attrs,
1325     if_t ifp, struct nlpcb *nlp, struct nl_pstate *npt)
1326 {
1327 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)attrs->ifa_local;
1328 
1329 	if (addr == NULL)
1330 		addr = (struct sockaddr_in6 *)(attrs->ifa_address);
1331 
1332 	if (addr == NULL) {
1333 		nlmsg_report_err_msg(npt, "Empty IFA_LOCAL/IFA_ADDRESS");
1334 		return (EINVAL);
1335 	}
1336 
1337 	struct in6_ifreq req = { .ifr_addr = *addr };
1338 
1339 	return (in6_control_ioctl(SIOCDIFADDR_IN6, &req, ifp, nlp_get_cred(nlp)));
1340 }
1341 #endif
1342 
1343 
1344 static int
rtnl_handle_addr(struct nlmsghdr * hdr,struct nlpcb * nlp,struct nl_pstate * npt)1345 rtnl_handle_addr(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *npt)
1346 {
1347 	struct epoch_tracker et;
1348 	int error;
1349 
1350 	struct nl_parsed_ifa attrs = {};
1351 	error = nl_parse_nlmsg(hdr, &ifa_parser, npt, &attrs);
1352 	if (error != 0)
1353 		return (error);
1354 
1355 	NET_EPOCH_ENTER(et);
1356 	if_t ifp = ifnet_byindex_ref(attrs.ifa_index);
1357 	NET_EPOCH_EXIT(et);
1358 
1359 	if (ifp == NULL) {
1360 		nlmsg_report_err_msg(npt, "Unable to find interface with index %u",
1361 		    attrs.ifa_index);
1362 		return (ENOENT);
1363 	}
1364 	int if_flags = if_getflags(ifp);
1365 
1366 #if defined(INET) || defined(INET6)
1367 	bool new = hdr->nlmsg_type == NL_RTM_NEWADDR;
1368 #endif
1369 
1370 	/*
1371 	 * TODO: Properly handle NLM_F_CREATE / NLM_F_EXCL.
1372 	 * The current ioctl-based KPI always does an implicit create-or-replace.
1373 	 * It is not possible to specify fine-grained options.
1374 	 */
1375 
1376 	switch (attrs.ifa_family) {
1377 #ifdef INET
1378 	case AF_INET:
1379 		if (new)
1380 			error = handle_newaddr_inet(hdr, &attrs, ifp, nlp, npt);
1381 		else
1382 			error = handle_deladdr_inet(hdr, &attrs, ifp, nlp, npt);
1383 		break;
1384 #endif
1385 #ifdef INET6
1386 	case AF_INET6:
1387 		if (new)
1388 			error = handle_newaddr_inet6(hdr, &attrs, ifp, nlp, npt);
1389 		else
1390 			error = handle_deladdr_inet6(hdr, &attrs, ifp, nlp, npt);
1391 		break;
1392 #endif
1393 	default:
1394 		error = EAFNOSUPPORT;
1395 	}
1396 
1397 	if (error == 0 && !(if_flags & IFF_UP) && (if_getflags(ifp) & IFF_UP))
1398 		if_up(ifp);
1399 
1400 	if_rele(ifp);
1401 
1402 	return (error);
1403 }
1404 
1405 
1406 static void
rtnl_handle_ifaddr(void * arg __unused,struct ifaddr * ifa,int cmd)1407 rtnl_handle_ifaddr(void *arg __unused, struct ifaddr *ifa, int cmd)
1408 {
1409 	struct nlmsghdr hdr = {};
1410 	struct nl_writer nw;
1411 	uint32_t group = 0;
1412 
1413 	switch (ifa->ifa_addr->sa_family) {
1414 #ifdef INET
1415 	case AF_INET:
1416 		group = RTNLGRP_IPV4_IFADDR;
1417 		break;
1418 #endif
1419 #ifdef INET6
1420 	case AF_INET6:
1421 		group = RTNLGRP_IPV6_IFADDR;
1422 		break;
1423 #endif
1424 	default:
1425 		NL_LOG(LOG_DEBUG2, "ifa notification for unknown AF: %d",
1426 		    ifa->ifa_addr->sa_family);
1427 		return;
1428 	}
1429 
1430 	if (!nl_writer_group(&nw, NLMSG_LARGE, NETLINK_ROUTE, group, 0,
1431 	    false)) {
1432 		NL_LOG(LOG_DEBUG, "error allocating group writer");
1433 		return;
1434 	}
1435 
1436 	hdr.nlmsg_type = (cmd == RTM_DELETE) ? NL_RTM_DELADDR : NL_RTM_NEWADDR;
1437 
1438 	dump_iface_addr(&nw, ifa->ifa_ifp, ifa, &hdr);
1439 	nlmsg_flush(&nw);
1440 }
1441 
1442 static void
rtnl_handle_ifevent(if_t ifp,int nlmsg_type,int if_flags_mask,const char * ifname)1443 rtnl_handle_ifevent(if_t ifp, int nlmsg_type, int if_flags_mask,
1444     const char *ifname)
1445 {
1446 	struct nlmsghdr hdr = { .nlmsg_type = nlmsg_type };
1447 	struct nl_writer nw;
1448 
1449 	if (!nl_writer_group(&nw, NLMSG_LARGE, NETLINK_ROUTE, RTNLGRP_LINK, 0,
1450 	    false)) {
1451 		NL_LOG(LOG_DEBUG, "error allocating group writer");
1452 		return;
1453 	}
1454 	dump_iface(&nw, ifp, &hdr, if_flags_mask, ifname);
1455         nlmsg_flush(&nw);
1456 }
1457 
1458 static void
rtnl_handle_ifattach(void * arg,if_t ifp)1459 rtnl_handle_ifattach(void *arg, if_t ifp)
1460 {
1461 	NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1462 	rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0, NULL);
1463 }
1464 
1465 static void
rtnl_handle_ifdetach(void * arg,if_t ifp)1466 rtnl_handle_ifdetach(void *arg, if_t ifp)
1467 {
1468 	NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1469 	rtnl_handle_ifevent(ifp, NL_RTM_DELLINK, 0, NULL);
1470 }
1471 
1472 static void
rtnl_handle_ifrename(void * arg,if_t ifp,const char * old_name)1473 rtnl_handle_ifrename(void *arg, if_t ifp, const char *old_name)
1474 {
1475 	rtnl_handle_ifevent(ifp, NL_RTM_DELLINK, 0, old_name);
1476 	rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0, NULL);
1477 }
1478 
1479 static void
rtnl_handle_iflink(void * arg,if_t ifp,int link_state __unused)1480 rtnl_handle_iflink(void *arg, if_t ifp, int link_state __unused)
1481 {
1482 	NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1483 	rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, 0, NULL);
1484 }
1485 
1486 void
rtnl_handle_ifnet_event(if_t ifp,int if_flags_mask)1487 rtnl_handle_ifnet_event(if_t ifp, int if_flags_mask)
1488 {
1489 	NL_LOG(LOG_DEBUG2, "ifnet %s", if_name(ifp));
1490 	rtnl_handle_ifevent(ifp, NL_RTM_NEWLINK, if_flags_mask, NULL);
1491 }
1492 
1493 static const struct rtnl_cmd_handler cmd_handlers[] = {
1494 	{
1495 		.cmd = NL_RTM_GETLINK,
1496 		.name = "RTM_GETLINK",
1497 		.cb = &rtnl_handle_getlink,
1498 		.flags = RTNL_F_NOEPOCH | RTNL_F_ALLOW_NONVNET_JAIL,
1499 	},
1500 	{
1501 		.cmd = NL_RTM_DELLINK,
1502 		.name = "RTM_DELLINK",
1503 		.cb = &rtnl_handle_dellink,
1504 		.priv = PRIV_NET_IFDESTROY,
1505 		.flags = RTNL_F_NOEPOCH,
1506 	},
1507 	{
1508 		.cmd = NL_RTM_NEWLINK,
1509 		.name = "RTM_NEWLINK",
1510 		.cb = &rtnl_handle_newlink,
1511 		.priv = PRIV_NET_IFCREATE,
1512 		.flags = RTNL_F_NOEPOCH,
1513 	},
1514 	{
1515 		.cmd = NL_RTM_GETADDR,
1516 		.name = "RTM_GETADDR",
1517 		.cb = &rtnl_handle_getaddr,
1518 		.flags = RTNL_F_ALLOW_NONVNET_JAIL,
1519 	},
1520 	{
1521 		.cmd = NL_RTM_NEWADDR,
1522 		.name = "RTM_NEWADDR",
1523 		.cb = &rtnl_handle_addr,
1524 		.priv = PRIV_NET_ADDIFADDR,
1525 		.flags = RTNL_F_NOEPOCH,
1526 	},
1527 	{
1528 		.cmd = NL_RTM_DELADDR,
1529 		.name = "RTM_DELADDR",
1530 		.cb = &rtnl_handle_addr,
1531 		.priv = PRIV_NET_DELIFADDR,
1532 		.flags = RTNL_F_NOEPOCH,
1533 	},
1534 };
1535 
1536 static const struct nlhdr_parser *all_parsers[] = {
1537 	&ifmsg_parser, &ifa_parser, &ifa_fbsd_parser,
1538 };
1539 
1540 void
rtnl_iface_add_cloner(struct nl_cloner * cloner)1541 rtnl_iface_add_cloner(struct nl_cloner *cloner)
1542 {
1543 	sx_xlock(&rtnl_cloner_lock);
1544 	SLIST_INSERT_HEAD(&nl_cloners, cloner, next);
1545 	sx_xunlock(&rtnl_cloner_lock);
1546 }
1547 
1548 void
rtnl_iface_del_cloner(struct nl_cloner * cloner)1549 rtnl_iface_del_cloner(struct nl_cloner *cloner)
1550 {
1551 	sx_xlock(&rtnl_cloner_lock);
1552 	SLIST_REMOVE(&nl_cloners, cloner, nl_cloner, next);
1553 	sx_xunlock(&rtnl_cloner_lock);
1554 }
1555 
1556 void
rtnl_ifaces_init(void)1557 rtnl_ifaces_init(void)
1558 {
1559 	ifattach_event = EVENTHANDLER_REGISTER(
1560 	    ifnet_attached_event, rtnl_handle_ifattach, NULL,
1561 	    EVENTHANDLER_PRI_ANY);
1562 	ifdetach_event = EVENTHANDLER_REGISTER(
1563 	    ifnet_departure_event, rtnl_handle_ifdetach, NULL,
1564 	    EVENTHANDLER_PRI_ANY);
1565 	ifrename_event = EVENTHANDLER_REGISTER(
1566 	    ifnet_rename_event, rtnl_handle_ifrename, NULL,
1567 	    EVENTHANDLER_PRI_ANY);
1568 	ifaddr_event = EVENTHANDLER_REGISTER(
1569 	    rt_addrmsg, rtnl_handle_ifaddr, NULL,
1570 	    EVENTHANDLER_PRI_ANY);
1571 	iflink_event = EVENTHANDLER_REGISTER(
1572 	    ifnet_link_event, rtnl_handle_iflink, NULL,
1573 	    EVENTHANDLER_PRI_ANY);
1574 	NL_VERIFY_PARSERS(all_parsers);
1575 	rtnl_register_messages(cmd_handlers, nitems(cmd_handlers));
1576 }
1577 
1578 void
rtnl_ifaces_destroy(void)1579 rtnl_ifaces_destroy(void)
1580 {
1581 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ifattach_event);
1582 	EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_event);
1583 	EVENTHANDLER_DEREGISTER(ifnet_rename_event, ifrename_event);
1584 	EVENTHANDLER_DEREGISTER(rt_addrmsg, ifaddr_event);
1585 	EVENTHANDLER_DEREGISTER(ifnet_link_event, iflink_event);
1586 }
1587