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