xref: /trueos/sys/net/if.c (revision 5868f7205430cd67aa3b655419d3f15f83b70119)
1 /*-
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)if.c	8.5 (Berkeley) 1/9/95
30  * $FreeBSD$
31  */
32 
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
35 #include "opt_inet.h"
36 
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/conf.h>
40 #include <sys/malloc.h>
41 #include <sys/sbuf.h>
42 #include <sys/bus.h>
43 #include <sys/mbuf.h>
44 #include <sys/systm.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/refcount.h>
53 #include <sys/module.h>
54 #include <sys/rwlock.h>
55 #include <sys/sockio.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/taskqueue.h>
59 #include <sys/domain.h>
60 #include <sys/jail.h>
61 #include <sys/priv.h>
62 
63 #include <machine/stdarg.h>
64 #include <vm/uma.h>
65 
66 #include <net/if.h>
67 #include <net/if_arp.h>
68 #include <net/if_clone.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/if_var.h>
72 #include <net/radix.h>
73 #include <net/route.h>
74 #include <net/vnet.h>
75 
76 #if defined(INET) || defined(INET6)
77 #include <net/ethernet.h>
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_carp.h>
82 #ifdef INET
83 #include <netinet/if_ether.h>
84 #endif /* INET */
85 #ifdef INET6
86 #include <netinet6/in6_var.h>
87 #include <netinet6/in6_ifattach.h>
88 #endif /* INET6 */
89 #endif /* INET || INET6 */
90 
91 #include <security/mac/mac_framework.h>
92 
93 #ifdef COMPAT_FREEBSD32
94 #include <sys/mount.h>
95 #include <compat/freebsd32/freebsd32.h>
96 #endif
97 
98 struct ifindex_entry {
99 	struct  ifnet *ife_ifnet;
100 };
101 
102 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
103 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
104 
105 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
106 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
107     &ifqmaxlen, 0, "max send queue size");
108 
109 /* Log link state change events */
110 static int log_link_state_change = 1;
111 
112 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
113 	&log_link_state_change, 0,
114 	"log interface link state change events");
115 
116 /* Interface description */
117 static unsigned int ifdescr_maxlen = 1024;
118 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
119 	&ifdescr_maxlen, 0,
120 	"administrative maximum length for interface description");
121 
122 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
123 
124 /* global sx for non-critical path ifdescr */
125 static struct sx ifdescr_sx;
126 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
127 
128 void	(*bridge_linkstate_p)(struct ifnet *ifp);
129 void	(*ng_ether_link_state_p)(struct ifnet *ifp, int state);
130 void	(*lagg_linkstate_p)(struct ifnet *ifp, int state);
131 /* These are external hooks for CARP. */
132 void	(*carp_linkstate_p)(struct ifnet *ifp);
133 void	(*carp_demote_adj_p)(int, char *);
134 int	(*carp_master_p)(struct ifaddr *);
135 #if defined(INET) || defined(INET6)
136 int	(*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
137 int	(*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
138     const struct sockaddr *sa);
139 int	(*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
140 int	(*carp_attach_p)(struct ifaddr *, int);
141 void	(*carp_detach_p)(struct ifaddr *);
142 #endif
143 #ifdef INET
144 int	(*carp_iamatch_p)(struct ifaddr *, uint8_t **);
145 #endif
146 #ifdef INET6
147 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
148 caddr_t	(*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
149     const struct in6_addr *taddr);
150 #endif
151 
152 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
153 
154 /*
155  * XXX: Style; these should be sorted alphabetically, and unprototyped
156  * static functions should be prototyped. Currently they are sorted by
157  * declaration order.
158  */
159 static void	if_attachdomain(void *);
160 static void	if_attachdomain1(struct ifnet *);
161 static int	ifconf(u_long, caddr_t);
162 static void	if_freemulti(struct ifmultiaddr *);
163 static void	if_init(void *);
164 static void	if_grow(void);
165 static void	if_input_default(struct ifnet *, struct mbuf *);
166 static void	if_route(struct ifnet *, int flag, int fam);
167 static int	if_setflag(struct ifnet *, int, int, int *, int);
168 static int	if_transmit(struct ifnet *ifp, struct mbuf *m);
169 static void	if_unroute(struct ifnet *, int flag, int fam);
170 static void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
171 static int	if_rtdel(struct radix_node *, void *);
172 static int	ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
173 static int	if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
174 static void	do_link_state_change(void *, int);
175 static int	if_getgroup(struct ifgroupreq *, struct ifnet *);
176 static int	if_getgroupmembers(struct ifgroupreq *);
177 static void	if_delgroups(struct ifnet *);
178 static void	if_attach_internal(struct ifnet *, int);
179 static void	if_detach_internal(struct ifnet *, int);
180 
181 #ifdef INET6
182 /*
183  * XXX: declare here to avoid to include many inet6 related files..
184  * should be more generalized?
185  */
186 extern void	nd6_setmtu(struct ifnet *);
187 #endif
188 
189 VNET_DEFINE(int, if_index);
190 int	ifqmaxlen = IFQ_MAXLEN;
191 VNET_DEFINE(struct ifnethead, ifnet);	/* depend on static init XXX */
192 VNET_DEFINE(struct ifgrouphead, ifg_head);
193 
194 static VNET_DEFINE(int, if_indexlim) = 8;
195 
196 /* Table of ifnet by index. */
197 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
198 
199 #define	V_if_indexlim		VNET(if_indexlim)
200 #define	V_ifindex_table		VNET(ifindex_table)
201 
202 /*
203  * The global network interface list (V_ifnet) and related state (such as
204  * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
205  * an rwlock.  Either may be acquired shared to stablize the list, but both
206  * must be acquired writable to modify the list.  This model allows us to
207  * both stablize the interface list during interrupt thread processing, but
208  * also to stablize it over long-running ioctls, without introducing priority
209  * inversions and deadlocks.
210  */
211 struct rwlock ifnet_rwlock;
212 struct sx ifnet_sxlock;
213 
214 /*
215  * The allocation of network interfaces is a rather non-atomic affair; we
216  * need to select an index before we are ready to expose the interface for
217  * use, so will use this pointer value to indicate reservation.
218  */
219 #define	IFNET_HOLD	(void *)(uintptr_t)(-1)
220 
221 static	if_com_alloc_t *if_com_alloc[256];
222 static	if_com_free_t *if_com_free[256];
223 
224 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
225 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
226 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
227 
228 struct ifnet *
ifnet_byindex_locked(u_short idx)229 ifnet_byindex_locked(u_short idx)
230 {
231 
232 	if (idx > V_if_index)
233 		return (NULL);
234 	if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
235 		return (NULL);
236 	return (V_ifindex_table[idx].ife_ifnet);
237 }
238 
239 struct ifnet *
ifnet_byindex(u_short idx)240 ifnet_byindex(u_short idx)
241 {
242 	struct ifnet *ifp;
243 
244 	IFNET_RLOCK_NOSLEEP();
245 	ifp = ifnet_byindex_locked(idx);
246 	IFNET_RUNLOCK_NOSLEEP();
247 	return (ifp);
248 }
249 
250 struct ifnet *
ifnet_byindex_ref(u_short idx)251 ifnet_byindex_ref(u_short idx)
252 {
253 	struct ifnet *ifp;
254 
255 	IFNET_RLOCK_NOSLEEP();
256 	ifp = ifnet_byindex_locked(idx);
257 	if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
258 		IFNET_RUNLOCK_NOSLEEP();
259 		return (NULL);
260 	}
261 	if_ref(ifp);
262 	IFNET_RUNLOCK_NOSLEEP();
263 	return (ifp);
264 }
265 
266 /*
267  * Allocate an ifindex array entry; return 0 on success or an error on
268  * failure.
269  */
270 static int
ifindex_alloc_locked(u_short * idxp)271 ifindex_alloc_locked(u_short *idxp)
272 {
273 	u_short idx;
274 
275 	IFNET_WLOCK_ASSERT();
276 
277 retry:
278 	/*
279 	 * Try to find an empty slot below V_if_index.  If we fail, take the
280 	 * next slot.
281 	 */
282 	for (idx = 1; idx <= V_if_index; idx++) {
283 		if (V_ifindex_table[idx].ife_ifnet == NULL)
284 			break;
285 	}
286 
287 	/* Catch if_index overflow. */
288 	if (idx < 1)
289 		return (ENOSPC);
290 	if (idx >= V_if_indexlim) {
291 		if_grow();
292 		goto retry;
293 	}
294 	if (idx > V_if_index)
295 		V_if_index = idx;
296 	*idxp = idx;
297 	return (0);
298 }
299 
300 static void
ifindex_free_locked(u_short idx)301 ifindex_free_locked(u_short idx)
302 {
303 
304 	IFNET_WLOCK_ASSERT();
305 
306 	V_ifindex_table[idx].ife_ifnet = NULL;
307 	while (V_if_index > 0 &&
308 	    V_ifindex_table[V_if_index].ife_ifnet == NULL)
309 		V_if_index--;
310 }
311 
312 static void
ifindex_free(u_short idx)313 ifindex_free(u_short idx)
314 {
315 
316 	IFNET_WLOCK();
317 	ifindex_free_locked(idx);
318 	IFNET_WUNLOCK();
319 }
320 
321 static void
ifnet_setbyindex_locked(u_short idx,struct ifnet * ifp)322 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
323 {
324 
325 	IFNET_WLOCK_ASSERT();
326 
327 	V_ifindex_table[idx].ife_ifnet = ifp;
328 }
329 
330 static void
ifnet_setbyindex(u_short idx,struct ifnet * ifp)331 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
332 {
333 
334 	IFNET_WLOCK();
335 	ifnet_setbyindex_locked(idx, ifp);
336 	IFNET_WUNLOCK();
337 }
338 
339 struct ifaddr *
ifaddr_byindex(u_short idx)340 ifaddr_byindex(u_short idx)
341 {
342 	struct ifaddr *ifa;
343 
344 	IFNET_RLOCK_NOSLEEP();
345 	ifa = ifnet_byindex_locked(idx)->if_addr;
346 	if (ifa != NULL)
347 		ifa_ref(ifa);
348 	IFNET_RUNLOCK_NOSLEEP();
349 	return (ifa);
350 }
351 
352 /*
353  * Network interface utility routines.
354  *
355  * Routines with ifa_ifwith* names take sockaddr *'s as
356  * parameters.
357  */
358 
359 static void
vnet_if_init(const void * unused __unused)360 vnet_if_init(const void *unused __unused)
361 {
362 
363 	TAILQ_INIT(&V_ifnet);
364 	TAILQ_INIT(&V_ifg_head);
365 	IFNET_WLOCK();
366 	if_grow();				/* create initial table */
367 	IFNET_WUNLOCK();
368 	vnet_if_clone_init();
369 }
370 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
371     NULL);
372 
373 /* ARGSUSED*/
374 static void
if_init(void * dummy __unused)375 if_init(void *dummy __unused)
376 {
377 
378 	IFNET_LOCK_INIT();
379 	if_clone_init();
380 }
381 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
382 
383 
384 #ifdef VIMAGE
385 static void
vnet_if_uninit(const void * unused __unused)386 vnet_if_uninit(const void *unused __unused)
387 {
388 
389 	VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
390 	    "not empty", __func__, __LINE__, &V_ifnet));
391 	VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
392 	    "not empty", __func__, __LINE__, &V_ifg_head));
393 
394 	free((caddr_t)V_ifindex_table, M_IFNET);
395 }
396 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
397     vnet_if_uninit, NULL);
398 #endif
399 
400 static void
if_grow(void)401 if_grow(void)
402 {
403 	int oldlim;
404 	u_int n;
405 	struct ifindex_entry *e;
406 
407 	IFNET_WLOCK_ASSERT();
408 	oldlim = V_if_indexlim;
409 	IFNET_WUNLOCK();
410 	n = (oldlim << 1) * sizeof(*e);
411 	e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
412 	IFNET_WLOCK();
413 	if (V_if_indexlim != oldlim) {
414 		free(e, M_IFNET);
415 		return;
416 	}
417 	if (V_ifindex_table != NULL) {
418 		memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
419 		free((caddr_t)V_ifindex_table, M_IFNET);
420 	}
421 	V_if_indexlim <<= 1;
422 	V_ifindex_table = e;
423 }
424 
425 /*
426  * Allocate a struct ifnet and an index for an interface.  A layer 2
427  * common structure will also be allocated if an allocation routine is
428  * registered for the passed type.
429  */
430 struct ifnet *
if_alloc(u_char type)431 if_alloc(u_char type)
432 {
433 	struct ifnet *ifp;
434 	u_short idx;
435 
436 	ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
437 	IFNET_WLOCK();
438 	if (ifindex_alloc_locked(&idx) != 0) {
439 		IFNET_WUNLOCK();
440 		free(ifp, M_IFNET);
441 		return (NULL);
442 	}
443 	ifnet_setbyindex_locked(idx, IFNET_HOLD);
444 	IFNET_WUNLOCK();
445 	ifp->if_index = idx;
446 	ifp->if_type = type;
447 	ifp->if_alloctype = type;
448 	if (if_com_alloc[type] != NULL) {
449 		ifp->if_l2com = if_com_alloc[type](type, ifp);
450 		if (ifp->if_l2com == NULL) {
451 			free(ifp, M_IFNET);
452 			ifindex_free(idx);
453 			return (NULL);
454 		}
455 	}
456 
457 	IF_ADDR_LOCK_INIT(ifp);
458 	TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
459 	ifp->if_afdata_initialized = 0;
460 	IF_AFDATA_LOCK_INIT(ifp);
461 	TAILQ_INIT(&ifp->if_addrhead);
462 	TAILQ_INIT(&ifp->if_multiaddrs);
463 	TAILQ_INIT(&ifp->if_groups);
464 #ifdef MAC
465 	mac_ifnet_init(ifp);
466 #endif
467 	ifq_init(&ifp->if_snd, ifp);
468 
469 	refcount_init(&ifp->if_refcount, 1);	/* Index reference. */
470 	ifnet_setbyindex(ifp->if_index, ifp);
471 	return (ifp);
472 }
473 
474 /*
475  * Do the actual work of freeing a struct ifnet, and layer 2 common
476  * structure.  This call is made when the last reference to an
477  * interface is released.
478  */
479 static void
if_free_internal(struct ifnet * ifp)480 if_free_internal(struct ifnet *ifp)
481 {
482 
483 	KASSERT((ifp->if_flags & IFF_DYING),
484 	    ("if_free_internal: interface not dying"));
485 
486 	if (if_com_free[ifp->if_alloctype] != NULL)
487 		if_com_free[ifp->if_alloctype](ifp->if_l2com,
488 		    ifp->if_alloctype);
489 
490 #ifdef MAC
491 	mac_ifnet_destroy(ifp);
492 #endif /* MAC */
493 	if (ifp->if_description != NULL)
494 		free(ifp->if_description, M_IFDESCR);
495 	IF_AFDATA_DESTROY(ifp);
496 	IF_ADDR_LOCK_DESTROY(ifp);
497 	ifq_delete(&ifp->if_snd);
498 	free(ifp, M_IFNET);
499 }
500 
501 /*
502  * Deregister an interface and free the associated storage.
503  */
504 void
if_free(struct ifnet * ifp)505 if_free(struct ifnet *ifp)
506 {
507 
508 	ifp->if_flags |= IFF_DYING;			/* XXX: Locking */
509 
510 	CURVNET_SET_QUIET(ifp->if_vnet);
511 	IFNET_WLOCK();
512 	KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
513 	    ("%s: freeing unallocated ifnet", ifp->if_xname));
514 
515 	ifindex_free_locked(ifp->if_index);
516 	IFNET_WUNLOCK();
517 
518 	if (refcount_release(&ifp->if_refcount))
519 		if_free_internal(ifp);
520 	CURVNET_RESTORE();
521 }
522 
523 /*
524  * Interfaces to keep an ifnet type-stable despite the possibility of the
525  * driver calling if_free().  If there are additional references, we defer
526  * freeing the underlying data structure.
527  */
528 void
if_ref(struct ifnet * ifp)529 if_ref(struct ifnet *ifp)
530 {
531 
532 	/* We don't assert the ifnet list lock here, but arguably should. */
533 	refcount_acquire(&ifp->if_refcount);
534 }
535 
536 void
if_rele(struct ifnet * ifp)537 if_rele(struct ifnet *ifp)
538 {
539 
540 	if (!refcount_release(&ifp->if_refcount))
541 		return;
542 	if_free_internal(ifp);
543 }
544 
545 void
ifq_init(struct ifaltq * ifq,struct ifnet * ifp)546 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
547 {
548 
549 	mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
550 
551 	if (ifq->ifq_maxlen == 0)
552 		ifq->ifq_maxlen = ifqmaxlen;
553 
554 	ifq->altq_type = 0;
555 	ifq->altq_disc = NULL;
556 	ifq->altq_flags &= ALTQF_CANTCHANGE;
557 	ifq->altq_tbr  = NULL;
558 	ifq->altq_ifp  = ifp;
559 }
560 
561 void
ifq_delete(struct ifaltq * ifq)562 ifq_delete(struct ifaltq *ifq)
563 {
564 	mtx_destroy(&ifq->ifq_mtx);
565 }
566 
567 /*
568  * Perform generic interface initalization tasks and attach the interface
569  * to the list of "active" interfaces.  If vmove flag is set on entry
570  * to if_attach_internal(), perform only a limited subset of initialization
571  * tasks, given that we are moving from one vnet to another an ifnet which
572  * has already been fully initialized.
573  *
574  * XXX:
575  *  - The decision to return void and thus require this function to
576  *    succeed is questionable.
577  *  - We should probably do more sanity checking.  For instance we don't
578  *    do anything to insure if_xname is unique or non-empty.
579  */
580 void
if_attach(struct ifnet * ifp)581 if_attach(struct ifnet *ifp)
582 {
583 
584 	if_attach_internal(ifp, 0);
585 }
586 
587 /*
588  * Compute the least common TSO limit.
589  */
590 void
if_hw_tsomax_common(struct ifnet * ifp,struct ifnet_hw_tsomax * pmax)591 if_hw_tsomax_common(struct ifnet *ifp, struct ifnet_hw_tsomax *pmax)
592 {
593 	/*
594 	 * 1) If there is no limit currently, take the limit from
595 	 * the network adapter.
596 	 *
597 	 * 2) If the network adapter has a limit below the current
598 	 * limit, apply it.
599 	 */
600 	if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
601 	    ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
602 		pmax->tsomaxbytes = ifp->if_hw_tsomax;
603 	}
604 	if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
605 	    ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
606 		pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
607 	}
608 	if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
609 	    ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
610 		pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
611 	}
612 }
613 
614 /*
615  * Update TSO limit of a network adapter.
616  *
617  * Returns zero if no change. Else non-zero.
618  */
619 int
if_hw_tsomax_update(struct ifnet * ifp,struct ifnet_hw_tsomax * pmax)620 if_hw_tsomax_update(struct ifnet *ifp, struct ifnet_hw_tsomax *pmax)
621 {
622 	int retval = 0;
623 	if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
624 		ifp->if_hw_tsomax = pmax->tsomaxbytes;
625 		retval++;
626 	}
627 	if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
628 		ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
629 		retval++;
630 	}
631 	if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
632 		ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
633 		retval++;
634 	}
635 	return (retval);
636 }
637 
638 static void
if_attach_internal(struct ifnet * ifp,int vmove)639 if_attach_internal(struct ifnet *ifp, int vmove)
640 {
641 	unsigned socksize, ifasize;
642 	int namelen, masklen;
643 	struct sockaddr_dl *sdl;
644 	struct ifaddr *ifa;
645 
646 	if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
647 		panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
648 		    ifp->if_xname);
649 
650 #ifdef VIMAGE
651 	ifp->if_vnet = curvnet;
652 	if (ifp->if_home_vnet == NULL)
653 		ifp->if_home_vnet = curvnet;
654 #endif
655 
656 	if_addgroup(ifp, IFG_ALL);
657 
658 	getmicrotime(&ifp->if_lastchange);
659 	ifp->if_data.ifi_epoch = time_uptime;
660 	ifp->if_data.ifi_datalen = sizeof(struct if_data);
661 
662 	KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
663 	    (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
664 	    ("transmit and qflush must both either be set or both be NULL"));
665 	if (ifp->if_transmit == NULL) {
666 		ifp->if_transmit = if_transmit;
667 		ifp->if_qflush = if_qflush;
668 	}
669 	if (ifp->if_input == NULL)
670 		ifp->if_input = if_input_default;
671 
672 	if (!vmove) {
673 #ifdef MAC
674 		mac_ifnet_create(ifp);
675 #endif
676 
677 		/*
678 		 * Create a Link Level name for this device.
679 		 */
680 		namelen = strlen(ifp->if_xname);
681 		/*
682 		 * Always save enough space for any possiable name so we
683 		 * can do a rename in place later.
684 		 */
685 		masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
686 		socksize = masklen + ifp->if_addrlen;
687 		if (socksize < sizeof(*sdl))
688 			socksize = sizeof(*sdl);
689 		socksize = roundup2(socksize, sizeof(long));
690 		ifasize = sizeof(*ifa) + 2 * socksize;
691 		ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
692 		ifa_init(ifa);
693 		sdl = (struct sockaddr_dl *)(ifa + 1);
694 		sdl->sdl_len = socksize;
695 		sdl->sdl_family = AF_LINK;
696 		bcopy(ifp->if_xname, sdl->sdl_data, namelen);
697 		sdl->sdl_nlen = namelen;
698 		sdl->sdl_index = ifp->if_index;
699 		sdl->sdl_type = ifp->if_type;
700 		ifp->if_addr = ifa;
701 		ifa->ifa_ifp = ifp;
702 		ifa->ifa_rtrequest = link_rtrequest;
703 		ifa->ifa_addr = (struct sockaddr *)sdl;
704 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
705 		ifa->ifa_netmask = (struct sockaddr *)sdl;
706 		sdl->sdl_len = masklen;
707 		while (namelen != 0)
708 			sdl->sdl_data[--namelen] = 0xff;
709 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
710 		/* Reliably crash if used uninitialized. */
711 		ifp->if_broadcastaddr = NULL;
712 
713 #if defined(INET) || defined(INET6)
714 		/* Use defaults for TSO, if nothing is set */
715 		if (ifp->if_hw_tsomax == 0 &&
716 		    ifp->if_hw_tsomaxsegcount == 0 &&
717 		    ifp->if_hw_tsomaxsegsize == 0) {
718 			/*
719 			 * The TSO defaults needs to be such that an
720 			 * NFS mbuf list of 35 mbufs totalling just
721 			 * below 64K works and that a chain of mbufs
722 			 * can be defragged into at most 32 segments:
723 			 */
724 			ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
725 			    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
726 			ifp->if_hw_tsomaxsegcount = 35;
727 			ifp->if_hw_tsomaxsegsize = 2048;	/* 2K */
728 
729 			/* XXX some drivers set IFCAP_TSO after ethernet attach */
730 			if (ifp->if_capabilities & IFCAP_TSO) {
731 				if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
732 				    ifp->if_hw_tsomax,
733 				    ifp->if_hw_tsomaxsegcount,
734 				    ifp->if_hw_tsomaxsegsize);
735 			}
736 		}
737 #endif
738 	}
739 #ifdef VIMAGE
740 	else {
741 		/*
742 		 * Update the interface index in the link layer address
743 		 * of the interface.
744 		 */
745 		for (ifa = ifp->if_addr; ifa != NULL;
746 		    ifa = TAILQ_NEXT(ifa, ifa_link)) {
747 			if (ifa->ifa_addr->sa_family == AF_LINK) {
748 				sdl = (struct sockaddr_dl *)ifa->ifa_addr;
749 				sdl->sdl_index = ifp->if_index;
750 			}
751 		}
752 	}
753 #endif
754 
755 	IFNET_WLOCK();
756 	TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
757 #ifdef VIMAGE
758 	curvnet->vnet_ifcnt++;
759 #endif
760 	IFNET_WUNLOCK();
761 
762 	if (domain_init_status >= 2)
763 		if_attachdomain1(ifp);
764 
765 	EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
766 	if (IS_DEFAULT_VNET(curvnet))
767 		devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
768 
769 	/* Announce the interface. */
770 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
771 }
772 
773 static void
if_attachdomain(void * dummy)774 if_attachdomain(void *dummy)
775 {
776 	struct ifnet *ifp;
777 
778 	TAILQ_FOREACH(ifp, &V_ifnet, if_link)
779 		if_attachdomain1(ifp);
780 }
781 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
782     if_attachdomain, NULL);
783 
784 static void
if_attachdomain1(struct ifnet * ifp)785 if_attachdomain1(struct ifnet *ifp)
786 {
787 	struct domain *dp;
788 
789 	/*
790 	 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
791 	 * cannot lock ifp->if_afdata initialization, entirely.
792 	 */
793 	if (IF_AFDATA_TRYLOCK(ifp) == 0)
794 		return;
795 	if (ifp->if_afdata_initialized >= domain_init_status) {
796 		IF_AFDATA_UNLOCK(ifp);
797 		log(LOG_WARNING, "%s called more than once on %s\n",
798 		    __func__, ifp->if_xname);
799 		return;
800 	}
801 	ifp->if_afdata_initialized = domain_init_status;
802 	IF_AFDATA_UNLOCK(ifp);
803 
804 	/* address family dependent data region */
805 	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
806 	for (dp = domains; dp; dp = dp->dom_next) {
807 		if (dp->dom_ifattach)
808 			ifp->if_afdata[dp->dom_family] =
809 			    (*dp->dom_ifattach)(ifp);
810 	}
811 }
812 
813 /*
814  * Remove any unicast or broadcast network addresses from an interface.
815  */
816 void
if_purgeaddrs(struct ifnet * ifp)817 if_purgeaddrs(struct ifnet *ifp)
818 {
819 	struct ifaddr *ifa, *next;
820 
821 	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
822 		if (ifa->ifa_addr->sa_family == AF_LINK)
823 			continue;
824 #ifdef INET
825 		/* XXX: Ugly!! ad hoc just for INET */
826 		if (ifa->ifa_addr->sa_family == AF_INET) {
827 			struct ifaliasreq ifr;
828 
829 			bzero(&ifr, sizeof(ifr));
830 			ifr.ifra_addr = *ifa->ifa_addr;
831 			if (ifa->ifa_dstaddr)
832 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
833 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
834 			    NULL) == 0)
835 				continue;
836 		}
837 #endif /* INET */
838 #ifdef INET6
839 		if (ifa->ifa_addr->sa_family == AF_INET6) {
840 			in6_purgeaddr(ifa);
841 			/* ifp_addrhead is already updated */
842 			continue;
843 		}
844 #endif /* INET6 */
845 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
846 		ifa_free(ifa);
847 	}
848 }
849 
850 /*
851  * Remove any multicast network addresses from an interface when an ifnet
852  * is going away.
853  */
854 static void
if_purgemaddrs(struct ifnet * ifp)855 if_purgemaddrs(struct ifnet *ifp)
856 {
857 	struct ifmultiaddr *ifma;
858 	struct ifmultiaddr *next;
859 
860 	IF_ADDR_WLOCK(ifp);
861 	TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
862 		if_delmulti_locked(ifp, ifma, 1);
863 	IF_ADDR_WUNLOCK(ifp);
864 }
865 
866 /*
867  * Detach an interface, removing it from the list of "active" interfaces.
868  * If vmove flag is set on entry to if_detach_internal(), perform only a
869  * limited subset of cleanup tasks, given that we are moving an ifnet from
870  * one vnet to another, where it must be fully operational.
871  *
872  * XXXRW: There are some significant questions about event ordering, and
873  * how to prevent things from starting to use the interface during detach.
874  */
875 void
if_detach(struct ifnet * ifp)876 if_detach(struct ifnet *ifp)
877 {
878 
879 	CURVNET_SET_QUIET(ifp->if_vnet);
880 	if_detach_internal(ifp, 0);
881 	CURVNET_RESTORE();
882 }
883 
884 static void
if_detach_internal(struct ifnet * ifp,int vmove)885 if_detach_internal(struct ifnet *ifp, int vmove)
886 {
887 	struct ifaddr *ifa;
888 	struct radix_node_head	*rnh;
889 	int i, j;
890 	struct domain *dp;
891  	struct ifnet *iter;
892  	int found = 0;
893 
894 	IFNET_WLOCK();
895 	TAILQ_FOREACH(iter, &V_ifnet, if_link)
896 		if (iter == ifp) {
897 			TAILQ_REMOVE(&V_ifnet, ifp, if_link);
898 			found = 1;
899 			break;
900 		}
901 #ifdef VIMAGE
902 	if (found)
903 		curvnet->vnet_ifcnt--;
904 #endif
905 	IFNET_WUNLOCK();
906 	if (!found) {
907 		if (vmove)
908 			panic("%s: ifp=%p not on the ifnet tailq %p",
909 			    __func__, ifp, &V_ifnet);
910 		else
911 			return; /* XXX this should panic as well? */
912 	}
913 
914 	/*
915 	 * Remove/wait for pending events.
916 	 */
917 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
918 
919 	/*
920 	 * Remove routes and flush queues.
921 	 */
922 	if_down(ifp);
923 #ifdef ALTQ
924 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
925 		altq_disable(&ifp->if_snd);
926 	if (ALTQ_IS_ATTACHED(&ifp->if_snd))
927 		altq_detach(&ifp->if_snd);
928 #endif
929 
930 	if_purgeaddrs(ifp);
931 
932 #ifdef INET
933 	in_ifdetach(ifp);
934 #endif
935 
936 #ifdef INET6
937 	/*
938 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
939 	 * before removing routing entries below, since IPv6 interface direct
940 	 * routes are expected to be removed by the IPv6-specific kernel API.
941 	 * Otherwise, the kernel will detect some inconsistency and bark it.
942 	 */
943 	in6_ifdetach(ifp);
944 #endif
945 	if_purgemaddrs(ifp);
946 
947 	if (!vmove) {
948 		/*
949 		 * Prevent further calls into the device driver via ifnet.
950 		 */
951 		if_dead(ifp);
952 
953 		/*
954 		 * Remove link ifaddr pointer and maybe decrement if_index.
955 		 * Clean up all addresses.
956 		 */
957 		ifp->if_addr = NULL;
958 
959 		/* We can now free link ifaddr. */
960 		if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
961 			ifa = TAILQ_FIRST(&ifp->if_addrhead);
962 			TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
963 			ifa_free(ifa);
964 		}
965 	}
966 
967 	/*
968 	 * Delete all remaining routes using this interface
969 	 * Unfortuneatly the only way to do this is to slog through
970 	 * the entire routing table looking for routes which point
971 	 * to this interface...oh well...
972 	 */
973 	for (i = 1; i <= AF_MAX; i++) {
974 		for (j = 0; j < rt_numfibs; j++) {
975 			rnh = rt_tables_get_rnh(j, i);
976 			if (rnh == NULL)
977 				continue;
978 			RADIX_NODE_HEAD_LOCK(rnh);
979 			(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
980 			RADIX_NODE_HEAD_UNLOCK(rnh);
981 		}
982 	}
983 
984 	/* Announce that the interface is gone. */
985 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
986 	EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
987 	if (IS_DEFAULT_VNET(curvnet))
988 		devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
989 	if_delgroups(ifp);
990 
991 	/*
992 	 * We cannot hold the lock over dom_ifdetach calls as they might
993 	 * sleep, for example trying to drain a callout, thus open up the
994 	 * theoretical race with re-attaching.
995 	 */
996 	IF_AFDATA_LOCK(ifp);
997 	i = ifp->if_afdata_initialized;
998 	ifp->if_afdata_initialized = 0;
999 	IF_AFDATA_UNLOCK(ifp);
1000 	for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1001 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1002 			(*dp->dom_ifdetach)(ifp,
1003 			    ifp->if_afdata[dp->dom_family]);
1004 	}
1005 }
1006 
1007 #ifdef VIMAGE
1008 /*
1009  * if_vmove() performs a limited version of if_detach() in current
1010  * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1011  * An attempt is made to shrink if_index in current vnet, find an
1012  * unused if_index in target vnet and calls if_grow() if necessary,
1013  * and finally find an unused if_xname for the target vnet.
1014  */
1015 void
if_vmove(struct ifnet * ifp,struct vnet * new_vnet)1016 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1017 {
1018 	u_short idx;
1019 
1020 	/*
1021 	 * Detach from current vnet, but preserve LLADDR info, do not
1022 	 * mark as dead etc. so that the ifnet can be reattached later.
1023 	 */
1024 	if_detach_internal(ifp, 1);
1025 
1026 	/*
1027 	 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1028 	 * the if_index for that vnet if possible.
1029 	 *
1030 	 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1031 	 * or we'd lock on one vnet and unlock on another.
1032 	 */
1033 	IFNET_WLOCK();
1034 	ifindex_free_locked(ifp->if_index);
1035 	IFNET_WUNLOCK();
1036 
1037 	/*
1038 	 * Perform interface-specific reassignment tasks, if provided by
1039 	 * the driver.
1040 	 */
1041 	if (ifp->if_reassign != NULL)
1042 		ifp->if_reassign(ifp, new_vnet, NULL);
1043 
1044 	/*
1045 	 * Switch to the context of the target vnet.
1046 	 */
1047 	CURVNET_SET_QUIET(new_vnet);
1048 
1049 	IFNET_WLOCK();
1050 	if (ifindex_alloc_locked(&idx) != 0) {
1051 		IFNET_WUNLOCK();
1052 		panic("if_index overflow");
1053 	}
1054 	ifp->if_index = idx;
1055 	ifnet_setbyindex_locked(ifp->if_index, ifp);
1056 	IFNET_WUNLOCK();
1057 
1058 	if_attach_internal(ifp, 1);
1059 
1060 	CURVNET_RESTORE();
1061 }
1062 
1063 /*
1064  * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1065  */
1066 static int
if_vmove_loan(struct thread * td,struct ifnet * ifp,char * ifname,int jid)1067 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1068 {
1069 	struct prison *pr;
1070 	struct ifnet *difp;
1071 
1072 	/* Try to find the prison within our visibility. */
1073 	sx_slock(&allprison_lock);
1074 	pr = prison_find_child(td->td_ucred->cr_prison, jid);
1075 	sx_sunlock(&allprison_lock);
1076 	if (pr == NULL)
1077 		return (ENXIO);
1078 	prison_hold_locked(pr);
1079 	mtx_unlock(&pr->pr_mtx);
1080 
1081 	/* Do not try to move the iface from and to the same prison. */
1082 	if (pr->pr_vnet == ifp->if_vnet) {
1083 		prison_free(pr);
1084 		return (EEXIST);
1085 	}
1086 
1087 	/* Make sure the named iface does not exists in the dst. prison/vnet. */
1088 	/* XXX Lock interfaces to avoid races. */
1089 	CURVNET_SET_QUIET(pr->pr_vnet);
1090 	difp = ifunit(ifname);
1091 	CURVNET_RESTORE();
1092 	if (difp != NULL) {
1093 		prison_free(pr);
1094 		return (EEXIST);
1095 	}
1096 
1097 	/* Move the interface into the child jail/vnet. */
1098 	if_vmove(ifp, pr->pr_vnet);
1099 
1100 	/* Report the new if_xname back to the userland. */
1101 	sprintf(ifname, "%s", ifp->if_xname);
1102 
1103 	prison_free(pr);
1104 	return (0);
1105 }
1106 
1107 static int
if_vmove_reclaim(struct thread * td,char * ifname,int jid)1108 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1109 {
1110 	struct prison *pr;
1111 	struct vnet *vnet_dst;
1112 	struct ifnet *ifp;
1113 
1114 	/* Try to find the prison within our visibility. */
1115 	sx_slock(&allprison_lock);
1116 	pr = prison_find_child(td->td_ucred->cr_prison, jid);
1117 	sx_sunlock(&allprison_lock);
1118 	if (pr == NULL)
1119 		return (ENXIO);
1120 	prison_hold_locked(pr);
1121 	mtx_unlock(&pr->pr_mtx);
1122 
1123 	/* Make sure the named iface exists in the source prison/vnet. */
1124 	CURVNET_SET(pr->pr_vnet);
1125 	ifp = ifunit(ifname);		/* XXX Lock to avoid races. */
1126 	if (ifp == NULL) {
1127 		CURVNET_RESTORE();
1128 		prison_free(pr);
1129 		return (ENXIO);
1130 	}
1131 
1132 	/* Do not try to move the iface from and to the same prison. */
1133 	vnet_dst = TD_TO_VNET(td);
1134 	if (vnet_dst == ifp->if_vnet) {
1135 		CURVNET_RESTORE();
1136 		prison_free(pr);
1137 		return (EEXIST);
1138 	}
1139 
1140 	/* Get interface back from child jail/vnet. */
1141 	if_vmove(ifp, vnet_dst);
1142 	CURVNET_RESTORE();
1143 
1144 	/* Report the new if_xname back to the userland. */
1145 	sprintf(ifname, "%s", ifp->if_xname);
1146 
1147 	prison_free(pr);
1148 	return (0);
1149 }
1150 #endif /* VIMAGE */
1151 
1152 /*
1153  * Add a group to an interface
1154  */
1155 int
if_addgroup(struct ifnet * ifp,const char * groupname)1156 if_addgroup(struct ifnet *ifp, const char *groupname)
1157 {
1158 	struct ifg_list		*ifgl;
1159 	struct ifg_group	*ifg = NULL;
1160 	struct ifg_member	*ifgm;
1161 	int 			 new = 0;
1162 
1163 	if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1164 	    groupname[strlen(groupname) - 1] <= '9')
1165 		return (EINVAL);
1166 
1167 	IFNET_WLOCK();
1168 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1169 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1170 			IFNET_WUNLOCK();
1171 			return (EEXIST);
1172 		}
1173 
1174 	if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1175 	    M_NOWAIT)) == NULL) {
1176 	    	IFNET_WUNLOCK();
1177 		return (ENOMEM);
1178 	}
1179 
1180 	if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1181 	    M_TEMP, M_NOWAIT)) == NULL) {
1182 		free(ifgl, M_TEMP);
1183 		IFNET_WUNLOCK();
1184 		return (ENOMEM);
1185 	}
1186 
1187 	TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1188 		if (!strcmp(ifg->ifg_group, groupname))
1189 			break;
1190 
1191 	if (ifg == NULL) {
1192 		if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1193 		    M_TEMP, M_NOWAIT)) == NULL) {
1194 			free(ifgl, M_TEMP);
1195 			free(ifgm, M_TEMP);
1196 			IFNET_WUNLOCK();
1197 			return (ENOMEM);
1198 		}
1199 		strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1200 		ifg->ifg_refcnt = 0;
1201 		TAILQ_INIT(&ifg->ifg_members);
1202 		TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1203 		new = 1;
1204 	}
1205 
1206 	ifg->ifg_refcnt++;
1207 	ifgl->ifgl_group = ifg;
1208 	ifgm->ifgm_ifp = ifp;
1209 
1210 	IF_ADDR_WLOCK(ifp);
1211 	TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1212 	TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1213 	IF_ADDR_WUNLOCK(ifp);
1214 
1215 	IFNET_WUNLOCK();
1216 
1217 	if (new)
1218 		EVENTHANDLER_INVOKE(group_attach_event, ifg);
1219 	EVENTHANDLER_INVOKE(group_change_event, groupname);
1220 
1221 	return (0);
1222 }
1223 
1224 /*
1225  * Remove a group from an interface
1226  */
1227 int
if_delgroup(struct ifnet * ifp,const char * groupname)1228 if_delgroup(struct ifnet *ifp, const char *groupname)
1229 {
1230 	struct ifg_list		*ifgl;
1231 	struct ifg_member	*ifgm;
1232 
1233 	IFNET_WLOCK();
1234 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1235 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1236 			break;
1237 	if (ifgl == NULL) {
1238 		IFNET_WUNLOCK();
1239 		return (ENOENT);
1240 	}
1241 
1242 	IF_ADDR_WLOCK(ifp);
1243 	TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1244 	IF_ADDR_WUNLOCK(ifp);
1245 
1246 	TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1247 		if (ifgm->ifgm_ifp == ifp)
1248 			break;
1249 
1250 	if (ifgm != NULL) {
1251 		TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1252 		free(ifgm, M_TEMP);
1253 	}
1254 
1255 	if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1256 		TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1257 		IFNET_WUNLOCK();
1258 		EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1259 		free(ifgl->ifgl_group, M_TEMP);
1260 	} else
1261 		IFNET_WUNLOCK();
1262 
1263 	free(ifgl, M_TEMP);
1264 
1265 	EVENTHANDLER_INVOKE(group_change_event, groupname);
1266 
1267 	return (0);
1268 }
1269 
1270 /*
1271  * Remove an interface from all groups
1272  */
1273 static void
if_delgroups(struct ifnet * ifp)1274 if_delgroups(struct ifnet *ifp)
1275 {
1276 	struct ifg_list		*ifgl;
1277 	struct ifg_member	*ifgm;
1278 	char groupname[IFNAMSIZ];
1279 
1280 	IFNET_WLOCK();
1281 	while (!TAILQ_EMPTY(&ifp->if_groups)) {
1282 		ifgl = TAILQ_FIRST(&ifp->if_groups);
1283 
1284 		strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1285 
1286 		IF_ADDR_WLOCK(ifp);
1287 		TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1288 		IF_ADDR_WUNLOCK(ifp);
1289 
1290 		TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1291 			if (ifgm->ifgm_ifp == ifp)
1292 				break;
1293 
1294 		if (ifgm != NULL) {
1295 			TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1296 			    ifgm_next);
1297 			free(ifgm, M_TEMP);
1298 		}
1299 
1300 		if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1301 			TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1302 			IFNET_WUNLOCK();
1303 			EVENTHANDLER_INVOKE(group_detach_event,
1304 			    ifgl->ifgl_group);
1305 			free(ifgl->ifgl_group, M_TEMP);
1306 		} else
1307 			IFNET_WUNLOCK();
1308 
1309 		free(ifgl, M_TEMP);
1310 
1311 		EVENTHANDLER_INVOKE(group_change_event, groupname);
1312 
1313 		IFNET_WLOCK();
1314 	}
1315 	IFNET_WUNLOCK();
1316 }
1317 
1318 /*
1319  * Stores all groups from an interface in memory pointed
1320  * to by data
1321  */
1322 static int
if_getgroup(struct ifgroupreq * data,struct ifnet * ifp)1323 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1324 {
1325 	int			 len, error;
1326 	struct ifg_list		*ifgl;
1327 	struct ifg_req		 ifgrq, *ifgp;
1328 	struct ifgroupreq	*ifgr = data;
1329 
1330 	if (ifgr->ifgr_len == 0) {
1331 		IF_ADDR_RLOCK(ifp);
1332 		TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1333 			ifgr->ifgr_len += sizeof(struct ifg_req);
1334 		IF_ADDR_RUNLOCK(ifp);
1335 		return (0);
1336 	}
1337 
1338 	len = ifgr->ifgr_len;
1339 	ifgp = ifgr->ifgr_groups;
1340 	/* XXX: wire */
1341 	IF_ADDR_RLOCK(ifp);
1342 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1343 		if (len < sizeof(ifgrq)) {
1344 			IF_ADDR_RUNLOCK(ifp);
1345 			return (EINVAL);
1346 		}
1347 		bzero(&ifgrq, sizeof ifgrq);
1348 		strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1349 		    sizeof(ifgrq.ifgrq_group));
1350 		if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1351 		    	IF_ADDR_RUNLOCK(ifp);
1352 			return (error);
1353 		}
1354 		len -= sizeof(ifgrq);
1355 		ifgp++;
1356 	}
1357 	IF_ADDR_RUNLOCK(ifp);
1358 
1359 	return (0);
1360 }
1361 
1362 /*
1363  * Stores all members of a group in memory pointed to by data
1364  */
1365 static int
if_getgroupmembers(struct ifgroupreq * data)1366 if_getgroupmembers(struct ifgroupreq *data)
1367 {
1368 	struct ifgroupreq	*ifgr = data;
1369 	struct ifg_group	*ifg;
1370 	struct ifg_member	*ifgm;
1371 	struct ifg_req		 ifgrq, *ifgp;
1372 	int			 len, error;
1373 
1374 	IFNET_RLOCK();
1375 	TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1376 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1377 			break;
1378 	if (ifg == NULL) {
1379 		IFNET_RUNLOCK();
1380 		return (ENOENT);
1381 	}
1382 
1383 	if (ifgr->ifgr_len == 0) {
1384 		TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1385 			ifgr->ifgr_len += sizeof(ifgrq);
1386 		IFNET_RUNLOCK();
1387 		return (0);
1388 	}
1389 
1390 	len = ifgr->ifgr_len;
1391 	ifgp = ifgr->ifgr_groups;
1392 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1393 		if (len < sizeof(ifgrq)) {
1394 			IFNET_RUNLOCK();
1395 			return (EINVAL);
1396 		}
1397 		bzero(&ifgrq, sizeof ifgrq);
1398 		strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1399 		    sizeof(ifgrq.ifgrq_member));
1400 		if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1401 			IFNET_RUNLOCK();
1402 			return (error);
1403 		}
1404 		len -= sizeof(ifgrq);
1405 		ifgp++;
1406 	}
1407 	IFNET_RUNLOCK();
1408 
1409 	return (0);
1410 }
1411 
1412 /*
1413  * Delete Routes for a Network Interface
1414  *
1415  * Called for each routing entry via the rnh->rnh_walktree() call above
1416  * to delete all route entries referencing a detaching network interface.
1417  *
1418  * Arguments:
1419  *	rn	pointer to node in the routing table
1420  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
1421  *
1422  * Returns:
1423  *	0	successful
1424  *	errno	failed - reason indicated
1425  *
1426  */
1427 static int
if_rtdel(struct radix_node * rn,void * arg)1428 if_rtdel(struct radix_node *rn, void *arg)
1429 {
1430 	struct rtentry	*rt = (struct rtentry *)rn;
1431 	struct ifnet	*ifp = arg;
1432 	int		err;
1433 
1434 	if (rt->rt_ifp == ifp) {
1435 
1436 		/*
1437 		 * Protect (sorta) against walktree recursion problems
1438 		 * with cloned routes
1439 		 */
1440 		if ((rt->rt_flags & RTF_UP) == 0)
1441 			return (0);
1442 
1443 		err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1444 				rt_mask(rt),
1445 				rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1446 				(struct rtentry **) NULL, rt->rt_fibnum);
1447 		if (err) {
1448 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
1449 		}
1450 	}
1451 
1452 	return (0);
1453 }
1454 
1455 /*
1456  * A compatibility function returns ifnet counter values.
1457  */
1458 uint64_t
if_get_counter_default(struct ifnet * ifp,ift_counter cnt)1459 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1460 {
1461 
1462 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1463 	switch (cnt) {
1464 	case IFCOUNTER_IPACKETS:
1465 		return (ifp->if_ipackets);
1466 	case IFCOUNTER_IERRORS:
1467 		return (ifp->if_ierrors);
1468 	case IFCOUNTER_OPACKETS:
1469 		return (ifp->if_opackets);
1470 	case IFCOUNTER_OERRORS:
1471 		return (ifp->if_oerrors);
1472 	case IFCOUNTER_COLLISIONS:
1473 		return (ifp->if_collisions);
1474 	case IFCOUNTER_IBYTES:
1475 		return (ifp->if_ibytes);
1476 	case IFCOUNTER_OBYTES:
1477 		return (ifp->if_obytes);
1478 	case IFCOUNTER_IMCASTS:
1479 		return (ifp->if_imcasts);
1480 	case IFCOUNTER_OMCASTS:
1481 		return (ifp->if_omcasts);
1482 	case IFCOUNTER_IQDROPS:
1483 		return (ifp->if_iqdrops);
1484 #ifdef _IFI_OQDROPS
1485 	case IFCOUNTER_OQDROPS:
1486 		return (ifp->if_oqdrops);
1487 #endif
1488 	case IFCOUNTER_NOPROTO:
1489 		return (ifp->if_noproto);
1490 	default:
1491 		break;
1492 	};
1493 	return (0);
1494 }
1495 
1496 /*
1497  * Increase an ifnet counter. Usually used for counters shared
1498  * between the stack and a driver, but function supports them all.
1499  */
1500 void
if_inc_counter(struct ifnet * ifp,ift_counter cnt,int64_t inc)1501 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1502 {
1503 
1504 	KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1505 	switch (cnt) {
1506 	case IFCOUNTER_IPACKETS:
1507 		ifp->if_ipackets += inc;
1508 		break;
1509 	case IFCOUNTER_IERRORS:
1510 		ifp->if_ierrors += inc;
1511 		break;
1512 	case IFCOUNTER_OPACKETS:
1513 		ifp->if_opackets += inc;
1514 		break;
1515 	case IFCOUNTER_OERRORS:
1516 		ifp->if_oerrors += inc;
1517 		break;
1518 	case IFCOUNTER_COLLISIONS:
1519 		ifp->if_collisions += inc;
1520 		break;
1521 	case IFCOUNTER_IBYTES:
1522 		ifp->if_ibytes += inc;
1523 		break;
1524 	case IFCOUNTER_OBYTES:
1525 		ifp->if_obytes += inc;
1526 		break;
1527 	case IFCOUNTER_IMCASTS:
1528 		ifp->if_imcasts += inc;
1529 		break;
1530 	case IFCOUNTER_OMCASTS:
1531 		ifp->if_omcasts += inc;
1532 		break;
1533 	case IFCOUNTER_IQDROPS:
1534 		ifp->if_iqdrops += inc;
1535 		break;
1536 #ifdef _IFI_OQDROPS
1537 	case IFCOUNTER_OQDROPS:
1538 		ifp->if_oqdrops += inc;
1539 		break;
1540 #endif
1541 	case IFCOUNTER_NOPROTO:
1542 		ifp->if_noproto += inc;
1543 		break;
1544 	default:
1545 		break;
1546 	};
1547 }
1548 
1549 /*
1550  * Wrapper functions for struct ifnet address list locking macros.  These are
1551  * used by kernel modules to avoid encoding programming interface or binary
1552  * interface assumptions that may be violated when kernel-internal locking
1553  * approaches change.
1554  */
1555 void
if_addr_rlock(struct ifnet * ifp)1556 if_addr_rlock(struct ifnet *ifp)
1557 {
1558 
1559 	IF_ADDR_RLOCK(ifp);
1560 }
1561 
1562 void
if_addr_runlock(struct ifnet * ifp)1563 if_addr_runlock(struct ifnet *ifp)
1564 {
1565 
1566 	IF_ADDR_RUNLOCK(ifp);
1567 }
1568 
1569 void
if_maddr_rlock(struct ifnet * ifp)1570 if_maddr_rlock(struct ifnet *ifp)
1571 {
1572 
1573 	IF_ADDR_RLOCK(ifp);
1574 }
1575 
1576 void
if_maddr_runlock(struct ifnet * ifp)1577 if_maddr_runlock(struct ifnet *ifp)
1578 {
1579 
1580 	IF_ADDR_RUNLOCK(ifp);
1581 }
1582 
1583 /*
1584  * Initialization, destruction and refcounting functions for ifaddrs.
1585  */
1586 void
ifa_init(struct ifaddr * ifa)1587 ifa_init(struct ifaddr *ifa)
1588 {
1589 
1590 	mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1591 	refcount_init(&ifa->ifa_refcnt, 1);
1592 	ifa->if_data.ifi_datalen = sizeof(ifa->if_data);
1593 }
1594 
1595 void
ifa_ref(struct ifaddr * ifa)1596 ifa_ref(struct ifaddr *ifa)
1597 {
1598 
1599 	refcount_acquire(&ifa->ifa_refcnt);
1600 }
1601 
1602 void
ifa_free(struct ifaddr * ifa)1603 ifa_free(struct ifaddr *ifa)
1604 {
1605 
1606 	if (refcount_release(&ifa->ifa_refcnt)) {
1607 		mtx_destroy(&ifa->ifa_mtx);
1608 		free(ifa, M_IFADDR);
1609 	}
1610 }
1611 
1612 int
ifa_add_loopback_route(struct ifaddr * ifa,struct sockaddr * ia)1613 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1614 {
1615 	int error = 0;
1616 	struct rtentry *rt = NULL;
1617 	struct rt_addrinfo info;
1618 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1619 
1620 	bzero(&info, sizeof(info));
1621 	info.rti_ifp = V_loif;
1622 	info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1623 	info.rti_info[RTAX_DST] = ia;
1624 	info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1625 	error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib);
1626 
1627 	if (error == 0 && rt != NULL) {
1628 		RT_LOCK(rt);
1629 		((struct sockaddr_dl *)rt->rt_gateway)->sdl_type  =
1630 			ifa->ifa_ifp->if_type;
1631 		((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1632 			ifa->ifa_ifp->if_index;
1633 		RT_REMREF(rt);
1634 		RT_UNLOCK(rt);
1635 	} else if (error != 0)
1636 		log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error);
1637 
1638 	return (error);
1639 }
1640 
1641 int
ifa_del_loopback_route(struct ifaddr * ifa,struct sockaddr * ia)1642 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1643 {
1644 	int error = 0;
1645 	struct rt_addrinfo info;
1646 	struct sockaddr_dl null_sdl;
1647 
1648 	bzero(&null_sdl, sizeof(null_sdl));
1649 	null_sdl.sdl_len = sizeof(null_sdl);
1650 	null_sdl.sdl_family = AF_LINK;
1651 	null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1652 	null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1653 	bzero(&info, sizeof(info));
1654 	info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1655 	info.rti_info[RTAX_DST] = ia;
1656 	info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1657 	error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib);
1658 
1659 	if (error != 0)
1660 		log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error);
1661 
1662 	return (error);
1663 }
1664 
1665 /*
1666  * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1667  * structs used to represent other address families, it is necessary
1668  * to perform a different comparison.
1669  */
1670 
1671 #define	sa_equal(a1, a2)	\
1672 	(bcmp((a1), (a2), ((a1))->sa_len) == 0)
1673 
1674 #define	sa_dl_equal(a1, a2)	\
1675 	((((struct sockaddr_dl *)(a1))->sdl_len ==			\
1676 	 ((struct sockaddr_dl *)(a2))->sdl_len) &&			\
1677 	 (bcmp(LLADDR((struct sockaddr_dl *)(a1)),			\
1678 	       LLADDR((struct sockaddr_dl *)(a2)),			\
1679 	       ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1680 
1681 /*
1682  * Locate an interface based on a complete address.
1683  */
1684 /*ARGSUSED*/
1685 static struct ifaddr *
ifa_ifwithaddr_internal(struct sockaddr * addr,int getref)1686 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1687 {
1688 	struct ifnet *ifp;
1689 	struct ifaddr *ifa;
1690 
1691 	IFNET_RLOCK_NOSLEEP();
1692 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1693 		IF_ADDR_RLOCK(ifp);
1694 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1695 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1696 				continue;
1697 			if (sa_equal(addr, ifa->ifa_addr)) {
1698 				if (getref)
1699 					ifa_ref(ifa);
1700 				IF_ADDR_RUNLOCK(ifp);
1701 				goto done;
1702 			}
1703 			/* IP6 doesn't have broadcast */
1704 			if ((ifp->if_flags & IFF_BROADCAST) &&
1705 			    ifa->ifa_broadaddr &&
1706 			    ifa->ifa_broadaddr->sa_len != 0 &&
1707 			    sa_equal(ifa->ifa_broadaddr, addr)) {
1708 				if (getref)
1709 					ifa_ref(ifa);
1710 				IF_ADDR_RUNLOCK(ifp);
1711 				goto done;
1712 			}
1713 		}
1714 		IF_ADDR_RUNLOCK(ifp);
1715 	}
1716 	ifa = NULL;
1717 done:
1718 	IFNET_RUNLOCK_NOSLEEP();
1719 	return (ifa);
1720 }
1721 
1722 struct ifaddr *
ifa_ifwithaddr(struct sockaddr * addr)1723 ifa_ifwithaddr(struct sockaddr *addr)
1724 {
1725 
1726 	return (ifa_ifwithaddr_internal(addr, 1));
1727 }
1728 
1729 int
ifa_ifwithaddr_check(struct sockaddr * addr)1730 ifa_ifwithaddr_check(struct sockaddr *addr)
1731 {
1732 
1733 	return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1734 }
1735 
1736 /*
1737  * Locate an interface based on the broadcast address.
1738  */
1739 /* ARGSUSED */
1740 struct ifaddr *
ifa_ifwithbroadaddr(struct sockaddr * addr)1741 ifa_ifwithbroadaddr(struct sockaddr *addr)
1742 {
1743 	struct ifnet *ifp;
1744 	struct ifaddr *ifa;
1745 
1746 	IFNET_RLOCK_NOSLEEP();
1747 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1748 		IF_ADDR_RLOCK(ifp);
1749 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1750 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1751 				continue;
1752 			if ((ifp->if_flags & IFF_BROADCAST) &&
1753 			    ifa->ifa_broadaddr &&
1754 			    ifa->ifa_broadaddr->sa_len != 0 &&
1755 			    sa_equal(ifa->ifa_broadaddr, addr)) {
1756 				ifa_ref(ifa);
1757 				IF_ADDR_RUNLOCK(ifp);
1758 				goto done;
1759 			}
1760 		}
1761 		IF_ADDR_RUNLOCK(ifp);
1762 	}
1763 	ifa = NULL;
1764 done:
1765 	IFNET_RUNLOCK_NOSLEEP();
1766 	return (ifa);
1767 }
1768 
1769 /*
1770  * Locate the point to point interface with a given destination address.
1771  */
1772 /*ARGSUSED*/
1773 struct ifaddr *
ifa_ifwithdstaddr_fib(struct sockaddr * addr,int fibnum)1774 ifa_ifwithdstaddr_fib(struct sockaddr *addr, int fibnum)
1775 {
1776 	struct ifnet *ifp;
1777 	struct ifaddr *ifa;
1778 
1779 	IFNET_RLOCK_NOSLEEP();
1780 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1781 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1782 			continue;
1783 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1784 			continue;
1785 		IF_ADDR_RLOCK(ifp);
1786 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1787 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1788 				continue;
1789 			if (ifa->ifa_dstaddr != NULL &&
1790 			    sa_equal(addr, ifa->ifa_dstaddr)) {
1791 				ifa_ref(ifa);
1792 				IF_ADDR_RUNLOCK(ifp);
1793 				goto done;
1794 			}
1795 		}
1796 		IF_ADDR_RUNLOCK(ifp);
1797 	}
1798 	ifa = NULL;
1799 done:
1800 	IFNET_RUNLOCK_NOSLEEP();
1801 	return (ifa);
1802 }
1803 
1804 struct ifaddr *
ifa_ifwithdstaddr(struct sockaddr * addr)1805 ifa_ifwithdstaddr(struct sockaddr *addr)
1806 {
1807 
1808 	return (ifa_ifwithdstaddr_fib(addr, RT_ALL_FIBS));
1809 }
1810 
1811 /*
1812  * Find an interface on a specific network.  If many, choice
1813  * is most specific found.
1814  */
1815 struct ifaddr *
ifa_ifwithnet_fib(struct sockaddr * addr,int ignore_ptp,int fibnum)1816 ifa_ifwithnet_fib(struct sockaddr *addr, int ignore_ptp, int fibnum)
1817 {
1818 	struct ifnet *ifp;
1819 	struct ifaddr *ifa;
1820 	struct ifaddr *ifa_maybe = NULL;
1821 	u_int af = addr->sa_family;
1822 	char *addr_data = addr->sa_data, *cplim;
1823 
1824 	/*
1825 	 * AF_LINK addresses can be looked up directly by their index number,
1826 	 * so do that if we can.
1827 	 */
1828 	if (af == AF_LINK) {
1829 	    struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1830 	    if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1831 		return (ifaddr_byindex(sdl->sdl_index));
1832 	}
1833 
1834 	/*
1835 	 * Scan though each interface, looking for ones that have addresses
1836 	 * in this address family and the requested fib.  Maintain a reference
1837 	 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1838 	 * kept it stable when we move onto the next interface.
1839 	 */
1840 	IFNET_RLOCK_NOSLEEP();
1841 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1842 		if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1843 			continue;
1844 		IF_ADDR_RLOCK(ifp);
1845 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1846 			char *cp, *cp2, *cp3;
1847 
1848 			if (ifa->ifa_addr->sa_family != af)
1849 next:				continue;
1850 			if (af == AF_INET &&
1851 			    ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1852 				/*
1853 				 * This is a bit broken as it doesn't
1854 				 * take into account that the remote end may
1855 				 * be a single node in the network we are
1856 				 * looking for.
1857 				 * The trouble is that we don't know the
1858 				 * netmask for the remote end.
1859 				 */
1860 				if (ifa->ifa_dstaddr != NULL &&
1861 				    sa_equal(addr, ifa->ifa_dstaddr)) {
1862 					ifa_ref(ifa);
1863 					IF_ADDR_RUNLOCK(ifp);
1864 					goto done;
1865 				}
1866 			} else {
1867 				/*
1868 				 * if we have a special address handler,
1869 				 * then use it instead of the generic one.
1870 				 */
1871 				if (ifa->ifa_claim_addr) {
1872 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1873 						ifa_ref(ifa);
1874 						IF_ADDR_RUNLOCK(ifp);
1875 						goto done;
1876 					}
1877 					continue;
1878 				}
1879 
1880 				/*
1881 				 * Scan all the bits in the ifa's address.
1882 				 * If a bit dissagrees with what we are
1883 				 * looking for, mask it with the netmask
1884 				 * to see if it really matters.
1885 				 * (A byte at a time)
1886 				 */
1887 				if (ifa->ifa_netmask == 0)
1888 					continue;
1889 				cp = addr_data;
1890 				cp2 = ifa->ifa_addr->sa_data;
1891 				cp3 = ifa->ifa_netmask->sa_data;
1892 				cplim = ifa->ifa_netmask->sa_len
1893 					+ (char *)ifa->ifa_netmask;
1894 				while (cp3 < cplim)
1895 					if ((*cp++ ^ *cp2++) & *cp3++)
1896 						goto next; /* next address! */
1897 				/*
1898 				 * If the netmask of what we just found
1899 				 * is more specific than what we had before
1900 				 * (if we had one), or if the virtual status
1901 				 * of new prefix is better than of the old one,
1902 				 * then remember the new one before continuing
1903 				 * to search for an even better one.
1904 				 */
1905 				if (ifa_maybe == NULL ||
1906 				    ifa_preferred(ifa_maybe, ifa) ||
1907 				    rn_refines((caddr_t)ifa->ifa_netmask,
1908 				    (caddr_t)ifa_maybe->ifa_netmask)) {
1909 					if (ifa_maybe != NULL)
1910 						ifa_free(ifa_maybe);
1911 					ifa_maybe = ifa;
1912 					ifa_ref(ifa_maybe);
1913 				}
1914 			}
1915 		}
1916 		IF_ADDR_RUNLOCK(ifp);
1917 	}
1918 	ifa = ifa_maybe;
1919 	ifa_maybe = NULL;
1920 done:
1921 	IFNET_RUNLOCK_NOSLEEP();
1922 	if (ifa_maybe != NULL)
1923 		ifa_free(ifa_maybe);
1924 	return (ifa);
1925 }
1926 
1927 struct ifaddr *
ifa_ifwithnet(struct sockaddr * addr,int ignore_ptp)1928 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1929 {
1930 
1931 	return (ifa_ifwithnet_fib(addr, ignore_ptp, RT_ALL_FIBS));
1932 }
1933 
1934 /*
1935  * Find an interface address specific to an interface best matching
1936  * a given address.
1937  */
1938 struct ifaddr *
ifaof_ifpforaddr(struct sockaddr * addr,struct ifnet * ifp)1939 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1940 {
1941 	struct ifaddr *ifa;
1942 	char *cp, *cp2, *cp3;
1943 	char *cplim;
1944 	struct ifaddr *ifa_maybe = NULL;
1945 	u_int af = addr->sa_family;
1946 
1947 	if (af >= AF_MAX)
1948 		return (NULL);
1949 	IF_ADDR_RLOCK(ifp);
1950 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1951 		if (ifa->ifa_addr->sa_family != af)
1952 			continue;
1953 		if (ifa_maybe == NULL)
1954 			ifa_maybe = ifa;
1955 		if (ifa->ifa_netmask == 0) {
1956 			if (sa_equal(addr, ifa->ifa_addr) ||
1957 			    (ifa->ifa_dstaddr &&
1958 			    sa_equal(addr, ifa->ifa_dstaddr)))
1959 				goto done;
1960 			continue;
1961 		}
1962 		if (ifp->if_flags & IFF_POINTOPOINT) {
1963 			if (sa_equal(addr, ifa->ifa_dstaddr))
1964 				goto done;
1965 		} else {
1966 			cp = addr->sa_data;
1967 			cp2 = ifa->ifa_addr->sa_data;
1968 			cp3 = ifa->ifa_netmask->sa_data;
1969 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1970 			for (; cp3 < cplim; cp3++)
1971 				if ((*cp++ ^ *cp2++) & *cp3)
1972 					break;
1973 			if (cp3 == cplim)
1974 				goto done;
1975 		}
1976 	}
1977 	ifa = ifa_maybe;
1978 done:
1979 	if (ifa != NULL)
1980 		ifa_ref(ifa);
1981 	IF_ADDR_RUNLOCK(ifp);
1982 	return (ifa);
1983 }
1984 
1985 /*
1986  * See whether new ifa is better than current one:
1987  * 1) A non-virtual one is preferred over virtual.
1988  * 2) A virtual in master state preferred over any other state.
1989  *
1990  * Used in several address selecting functions.
1991  */
1992 int
ifa_preferred(struct ifaddr * cur,struct ifaddr * next)1993 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1994 {
1995 
1996 	return (cur->ifa_carp && (!next->ifa_carp ||
1997 	    ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
1998 }
1999 
2000 #include <net/if_llatbl.h>
2001 
2002 /*
2003  * Default action when installing a route with a Link Level gateway.
2004  * Lookup an appropriate real ifa to point to.
2005  * This should be moved to /sys/net/link.c eventually.
2006  */
2007 static void
link_rtrequest(int cmd,struct rtentry * rt,struct rt_addrinfo * info)2008 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2009 {
2010 	struct ifaddr *ifa, *oifa;
2011 	struct sockaddr *dst;
2012 	struct ifnet *ifp;
2013 
2014 	RT_LOCK_ASSERT(rt);
2015 
2016 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
2017 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
2018 		return;
2019 	ifa = ifaof_ifpforaddr(dst, ifp);
2020 	if (ifa) {
2021 		oifa = rt->rt_ifa;
2022 		rt->rt_ifa = ifa;
2023 		ifa_free(oifa);
2024 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2025 			ifa->ifa_rtrequest(cmd, rt, info);
2026 	}
2027 }
2028 
2029 /*
2030  * Mark an interface down and notify protocols of
2031  * the transition.
2032  */
2033 static void
if_unroute(struct ifnet * ifp,int flag,int fam)2034 if_unroute(struct ifnet *ifp, int flag, int fam)
2035 {
2036 	struct ifaddr *ifa;
2037 
2038 	KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2039 
2040 	ifp->if_flags &= ~flag;
2041 	getmicrotime(&ifp->if_lastchange);
2042 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2043 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2044 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2045 	ifp->if_qflush(ifp);
2046 
2047 	if (ifp->if_carp)
2048 		(*carp_linkstate_p)(ifp);
2049 	rt_ifmsg(ifp);
2050 }
2051 
2052 /*
2053  * Mark an interface up and notify protocols of
2054  * the transition.
2055  */
2056 static void
if_route(struct ifnet * ifp,int flag,int fam)2057 if_route(struct ifnet *ifp, int flag, int fam)
2058 {
2059 	struct ifaddr *ifa;
2060 
2061 	KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2062 
2063 	ifp->if_flags |= flag;
2064 	getmicrotime(&ifp->if_lastchange);
2065 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2066 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2067 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
2068 	if (ifp->if_carp)
2069 		(*carp_linkstate_p)(ifp);
2070 	rt_ifmsg(ifp);
2071 #ifdef INET6
2072 	in6_if_up(ifp);
2073 #endif
2074 }
2075 
2076 void	(*vlan_link_state_p)(struct ifnet *);	/* XXX: private from if_vlan */
2077 void	(*vlan_trunk_cap_p)(struct ifnet *);		/* XXX: private from if_vlan */
2078 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2079 struct	ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2080 int	(*vlan_tag_p)(struct ifnet *, uint16_t *);
2081 int	(*vlan_setcookie_p)(struct ifnet *, void *);
2082 void	*(*vlan_cookie_p)(struct ifnet *);
2083 
2084 /*
2085  * Handle a change in the interface link state. To avoid LORs
2086  * between driver lock and upper layer locks, as well as possible
2087  * recursions, we post event to taskqueue, and all job
2088  * is done in static do_link_state_change().
2089  */
2090 void
if_link_state_change(struct ifnet * ifp,int link_state)2091 if_link_state_change(struct ifnet *ifp, int link_state)
2092 {
2093 	/* Return if state hasn't changed. */
2094 	if (ifp->if_link_state == link_state)
2095 		return;
2096 
2097 	ifp->if_link_state = link_state;
2098 
2099 	taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2100 }
2101 
2102 static void
do_link_state_change(void * arg,int pending)2103 do_link_state_change(void *arg, int pending)
2104 {
2105 	struct ifnet *ifp = (struct ifnet *)arg;
2106 	int link_state = ifp->if_link_state;
2107 	CURVNET_SET(ifp->if_vnet);
2108 
2109 	/* Notify that the link state has changed. */
2110 	rt_ifmsg(ifp);
2111 	if (ifp->if_vlantrunk != NULL)
2112 		(*vlan_link_state_p)(ifp);
2113 
2114 	if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2115 	    IFP2AC(ifp)->ac_netgraph != NULL)
2116 		(*ng_ether_link_state_p)(ifp, link_state);
2117 	if (ifp->if_carp)
2118 		(*carp_linkstate_p)(ifp);
2119 	if (ifp->if_bridge)
2120 		(*bridge_linkstate_p)(ifp);
2121 	if (ifp->if_lagg)
2122 		(*lagg_linkstate_p)(ifp, link_state);
2123 
2124 	if (IS_DEFAULT_VNET(curvnet))
2125 		devctl_notify("IFNET", ifp->if_xname,
2126 		    (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2127 		    NULL);
2128 	if (pending > 1)
2129 		if_printf(ifp, "%d link states coalesced\n", pending);
2130 	if (log_link_state_change)
2131 		log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2132 		    (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2133 	EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
2134 	CURVNET_RESTORE();
2135 }
2136 
2137 /*
2138  * Mark an interface down and notify protocols of
2139  * the transition.
2140  */
2141 void
if_down(struct ifnet * ifp)2142 if_down(struct ifnet *ifp)
2143 {
2144 
2145 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
2146 }
2147 
2148 /*
2149  * Mark an interface up and notify protocols of
2150  * the transition.
2151  */
2152 void
if_up(struct ifnet * ifp)2153 if_up(struct ifnet *ifp)
2154 {
2155 
2156 	if_route(ifp, IFF_UP, AF_UNSPEC);
2157 }
2158 
2159 /*
2160  * Flush an interface queue.
2161  */
2162 void
if_qflush(struct ifnet * ifp)2163 if_qflush(struct ifnet *ifp)
2164 {
2165 	struct mbuf *m, *n;
2166 	struct ifaltq *ifq;
2167 
2168 	ifq = &ifp->if_snd;
2169 	IFQ_LOCK(ifq);
2170 #ifdef ALTQ
2171 	if (ALTQ_IS_ENABLED(ifq))
2172 		ALTQ_PURGE(ifq);
2173 #endif
2174 	n = ifq->ifq_head;
2175 	while ((m = n) != 0) {
2176 		n = m->m_nextpkt;
2177 		m_freem(m);
2178 	}
2179 	ifq->ifq_head = 0;
2180 	ifq->ifq_tail = 0;
2181 	ifq->ifq_len = 0;
2182 	IFQ_UNLOCK(ifq);
2183 }
2184 
2185 /*
2186  * Map interface name to interface structure pointer, with or without
2187  * returning a reference.
2188  */
2189 struct ifnet *
ifunit_ref(const char * name)2190 ifunit_ref(const char *name)
2191 {
2192 	struct ifnet *ifp;
2193 
2194 	IFNET_RLOCK_NOSLEEP();
2195 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2196 		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2197 		    !(ifp->if_flags & IFF_DYING))
2198 			break;
2199 	}
2200 	if (ifp != NULL)
2201 		if_ref(ifp);
2202 	IFNET_RUNLOCK_NOSLEEP();
2203 	return (ifp);
2204 }
2205 
2206 struct ifnet *
ifunit(const char * name)2207 ifunit(const char *name)
2208 {
2209 	struct ifnet *ifp;
2210 
2211 	IFNET_RLOCK_NOSLEEP();
2212 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2213 		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2214 			break;
2215 	}
2216 	IFNET_RUNLOCK_NOSLEEP();
2217 	return (ifp);
2218 }
2219 
2220 /*
2221  * Hardware specific interface ioctls.
2222  */
2223 static int
ifhwioctl(u_long cmd,struct ifnet * ifp,caddr_t data,struct thread * td)2224 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2225 {
2226 	struct ifreq *ifr;
2227 	struct ifstat *ifs;
2228 	int error = 0;
2229 	int new_flags, temp_flags;
2230 	size_t namelen, onamelen;
2231 	size_t descrlen;
2232 	char *descrbuf, *odescrbuf;
2233 	char new_name[IFNAMSIZ];
2234 	struct ifaddr *ifa;
2235 	struct sockaddr_dl *sdl;
2236 
2237 	ifr = (struct ifreq *)data;
2238 	switch (cmd) {
2239 	case SIOCGIFINDEX:
2240 		ifr->ifr_index = ifp->if_index;
2241 		break;
2242 
2243 	case SIOCGIFFLAGS:
2244 		temp_flags = ifp->if_flags | ifp->if_drv_flags;
2245 		ifr->ifr_flags = temp_flags & 0xffff;
2246 		ifr->ifr_flagshigh = temp_flags >> 16;
2247 		break;
2248 
2249 	case SIOCGIFCAP:
2250 		ifr->ifr_reqcap = ifp->if_capabilities;
2251 		ifr->ifr_curcap = ifp->if_capenable;
2252 		break;
2253 
2254 #ifdef MAC
2255 	case SIOCGIFMAC:
2256 		error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2257 		break;
2258 #endif
2259 
2260 	case SIOCGIFMETRIC:
2261 		ifr->ifr_metric = ifp->if_metric;
2262 		break;
2263 
2264 	case SIOCGIFMTU:
2265 		ifr->ifr_mtu = ifp->if_mtu;
2266 		break;
2267 
2268 	case SIOCGIFPHYS:
2269 		ifr->ifr_phys = ifp->if_physical;
2270 		break;
2271 
2272 	case SIOCGIFDESCR:
2273 		error = 0;
2274 		sx_slock(&ifdescr_sx);
2275 		if (ifp->if_description == NULL)
2276 			error = ENOMSG;
2277 		else {
2278 			/* space for terminating nul */
2279 			descrlen = strlen(ifp->if_description) + 1;
2280 			if (ifr->ifr_buffer.length < descrlen)
2281 				ifr->ifr_buffer.buffer = NULL;
2282 			else
2283 				error = copyout(ifp->if_description,
2284 				    ifr->ifr_buffer.buffer, descrlen);
2285 			ifr->ifr_buffer.length = descrlen;
2286 		}
2287 		sx_sunlock(&ifdescr_sx);
2288 		break;
2289 
2290 	case SIOCSIFDESCR:
2291 		error = priv_check(td, PRIV_NET_SETIFDESCR);
2292 		if (error)
2293 			return (error);
2294 
2295 		/*
2296 		 * Copy only (length-1) bytes to make sure that
2297 		 * if_description is always nul terminated.  The
2298 		 * length parameter is supposed to count the
2299 		 * terminating nul in.
2300 		 */
2301 		if (ifr->ifr_buffer.length > ifdescr_maxlen)
2302 			return (ENAMETOOLONG);
2303 		else if (ifr->ifr_buffer.length == 0)
2304 			descrbuf = NULL;
2305 		else {
2306 			descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2307 			    M_WAITOK | M_ZERO);
2308 			error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2309 			    ifr->ifr_buffer.length - 1);
2310 			if (error) {
2311 				free(descrbuf, M_IFDESCR);
2312 				break;
2313 			}
2314 		}
2315 
2316 		sx_xlock(&ifdescr_sx);
2317 		odescrbuf = ifp->if_description;
2318 		ifp->if_description = descrbuf;
2319 		sx_xunlock(&ifdescr_sx);
2320 
2321 		getmicrotime(&ifp->if_lastchange);
2322 		free(odescrbuf, M_IFDESCR);
2323 		break;
2324 
2325 	case SIOCGIFFIB:
2326 		ifr->ifr_fib = ifp->if_fib;
2327 		break;
2328 
2329 	case SIOCSIFFIB:
2330 		error = priv_check(td, PRIV_NET_SETIFFIB);
2331 		if (error)
2332 			return (error);
2333 		if (ifr->ifr_fib >= rt_numfibs)
2334 			return (EINVAL);
2335 
2336 		ifp->if_fib = ifr->ifr_fib;
2337 		break;
2338 
2339 	case SIOCSIFFLAGS:
2340 		error = priv_check(td, PRIV_NET_SETIFFLAGS);
2341 		if (error)
2342 			return (error);
2343 		/*
2344 		 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2345 		 * check, so we don't need special handling here yet.
2346 		 */
2347 		new_flags = (ifr->ifr_flags & 0xffff) |
2348 		    (ifr->ifr_flagshigh << 16);
2349 		if (ifp->if_flags & IFF_SMART) {
2350 			/* Smart drivers twiddle their own routes */
2351 		} else if (ifp->if_flags & IFF_UP &&
2352 		    (new_flags & IFF_UP) == 0) {
2353 			if_down(ifp);
2354 		} else if (new_flags & IFF_UP &&
2355 		    (ifp->if_flags & IFF_UP) == 0) {
2356 			if_up(ifp);
2357 		}
2358 		/* See if permanently promiscuous mode bit is about to flip */
2359 		if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2360 			if (new_flags & IFF_PPROMISC)
2361 				ifp->if_flags |= IFF_PROMISC;
2362 			else if (ifp->if_pcount == 0)
2363 				ifp->if_flags &= ~IFF_PROMISC;
2364 			log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2365 			    ifp->if_xname,
2366 			    (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2367 		}
2368 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2369 			(new_flags &~ IFF_CANTCHANGE);
2370 		if (ifp->if_ioctl) {
2371 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
2372 		}
2373 		getmicrotime(&ifp->if_lastchange);
2374 		break;
2375 
2376 	case SIOCSIFCAP:
2377 		error = priv_check(td, PRIV_NET_SETIFCAP);
2378 		if (error)
2379 			return (error);
2380 		if (ifp->if_ioctl == NULL)
2381 			return (EOPNOTSUPP);
2382 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2383 			return (EINVAL);
2384 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2385 		if (error == 0)
2386 			getmicrotime(&ifp->if_lastchange);
2387 		break;
2388 
2389 #ifdef MAC
2390 	case SIOCSIFMAC:
2391 		error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2392 		break;
2393 #endif
2394 
2395 	case SIOCSIFNAME:
2396 		error = priv_check(td, PRIV_NET_SETIFNAME);
2397 		if (error)
2398 			return (error);
2399 		error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2400 		if (error != 0)
2401 			return (error);
2402 		if (new_name[0] == '\0')
2403 			return (EINVAL);
2404 		if (ifunit(new_name) != NULL)
2405 			return (EEXIST);
2406 
2407 		/*
2408 		 * XXX: Locking.  Nothing else seems to lock if_flags,
2409 		 * and there are numerous other races with the
2410 		 * ifunit() checks not being atomic with namespace
2411 		 * changes (renames, vmoves, if_attach, etc).
2412 		 */
2413 		ifp->if_flags |= IFF_RENAMING;
2414 
2415 		/* Announce the departure of the interface. */
2416 		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2417 		EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2418 
2419 		log(LOG_INFO, "%s: changing name to '%s'\n",
2420 		    ifp->if_xname, new_name);
2421 
2422 		strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2423 		ifa = ifp->if_addr;
2424 		IFA_LOCK(ifa);
2425 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2426 		namelen = strlen(new_name);
2427 		onamelen = sdl->sdl_nlen;
2428 		/*
2429 		 * Move the address if needed.  This is safe because we
2430 		 * allocate space for a name of length IFNAMSIZ when we
2431 		 * create this in if_attach().
2432 		 */
2433 		if (namelen != onamelen) {
2434 			bcopy(sdl->sdl_data + onamelen,
2435 			    sdl->sdl_data + namelen, sdl->sdl_alen);
2436 		}
2437 		bcopy(new_name, sdl->sdl_data, namelen);
2438 		sdl->sdl_nlen = namelen;
2439 		sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2440 		bzero(sdl->sdl_data, onamelen);
2441 		while (namelen != 0)
2442 			sdl->sdl_data[--namelen] = 0xff;
2443 		IFA_UNLOCK(ifa);
2444 
2445 		EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2446 		/* Announce the return of the interface. */
2447 		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2448 
2449 		ifp->if_flags &= ~IFF_RENAMING;
2450 		break;
2451 
2452 #ifdef VIMAGE
2453 	case SIOCSIFVNET:
2454 		error = priv_check(td, PRIV_NET_SETIFVNET);
2455 		if (error)
2456 			return (error);
2457 		error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2458 		break;
2459 #endif
2460 
2461 	case SIOCSIFMETRIC:
2462 		error = priv_check(td, PRIV_NET_SETIFMETRIC);
2463 		if (error)
2464 			return (error);
2465 		ifp->if_metric = ifr->ifr_metric;
2466 		getmicrotime(&ifp->if_lastchange);
2467 		break;
2468 
2469 	case SIOCSIFPHYS:
2470 		error = priv_check(td, PRIV_NET_SETIFPHYS);
2471 		if (error)
2472 			return (error);
2473 		if (ifp->if_ioctl == NULL)
2474 			return (EOPNOTSUPP);
2475 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2476 		if (error == 0)
2477 			getmicrotime(&ifp->if_lastchange);
2478 		break;
2479 
2480 	case SIOCSIFMTU:
2481 	{
2482 		u_long oldmtu = ifp->if_mtu;
2483 
2484 		error = priv_check(td, PRIV_NET_SETIFMTU);
2485 		if (error)
2486 			return (error);
2487 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2488 			return (EINVAL);
2489 		if (ifp->if_ioctl == NULL)
2490 			return (EOPNOTSUPP);
2491 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2492 		if (error == 0) {
2493 			getmicrotime(&ifp->if_lastchange);
2494 			rt_ifmsg(ifp);
2495 		}
2496 		/*
2497 		 * If the link MTU changed, do network layer specific procedure.
2498 		 */
2499 		if (ifp->if_mtu != oldmtu) {
2500 #ifdef INET6
2501 			nd6_setmtu(ifp);
2502 #endif
2503 		}
2504 		break;
2505 	}
2506 
2507 	case SIOCADDMULTI:
2508 	case SIOCDELMULTI:
2509 		if (cmd == SIOCADDMULTI)
2510 			error = priv_check(td, PRIV_NET_ADDMULTI);
2511 		else
2512 			error = priv_check(td, PRIV_NET_DELMULTI);
2513 		if (error)
2514 			return (error);
2515 
2516 		/* Don't allow group membership on non-multicast interfaces. */
2517 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
2518 			return (EOPNOTSUPP);
2519 
2520 		/* Don't let users screw up protocols' entries. */
2521 		if (ifr->ifr_addr.sa_family != AF_LINK)
2522 			return (EINVAL);
2523 
2524 		if (cmd == SIOCADDMULTI) {
2525 			struct ifmultiaddr *ifma;
2526 
2527 			/*
2528 			 * Userland is only permitted to join groups once
2529 			 * via the if_addmulti() KPI, because it cannot hold
2530 			 * struct ifmultiaddr * between calls. It may also
2531 			 * lose a race while we check if the membership
2532 			 * already exists.
2533 			 */
2534 			IF_ADDR_RLOCK(ifp);
2535 			ifma = if_findmulti(ifp, &ifr->ifr_addr);
2536 			IF_ADDR_RUNLOCK(ifp);
2537 			if (ifma != NULL)
2538 				error = EADDRINUSE;
2539 			else
2540 				error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2541 		} else {
2542 			error = if_delmulti(ifp, &ifr->ifr_addr);
2543 		}
2544 		if (error == 0)
2545 			getmicrotime(&ifp->if_lastchange);
2546 		break;
2547 
2548 	case SIOCSIFPHYADDR:
2549 	case SIOCDIFPHYADDR:
2550 #ifdef INET6
2551 	case SIOCSIFPHYADDR_IN6:
2552 #endif
2553 	case SIOCSLIFPHYADDR:
2554 	case SIOCSIFMEDIA:
2555 	case SIOCSIFGENERIC:
2556 		error = priv_check(td, PRIV_NET_HWIOCTL);
2557 		if (error)
2558 			return (error);
2559 		if (ifp->if_ioctl == NULL)
2560 			return (EOPNOTSUPP);
2561 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2562 		if (error == 0)
2563 			getmicrotime(&ifp->if_lastchange);
2564 		break;
2565 
2566 	case SIOCGIFSTATUS:
2567 		ifs = (struct ifstat *)data;
2568 		ifs->ascii[0] = '\0';
2569 
2570 	case SIOCGIFPSRCADDR:
2571 	case SIOCGIFPDSTADDR:
2572 	case SIOCGLIFPHYADDR:
2573 	case SIOCGIFMEDIA:
2574 	case SIOCGIFXMEDIA:
2575 	case SIOCGIFGENERIC:
2576 		if (ifp->if_ioctl == NULL)
2577 			return (EOPNOTSUPP);
2578 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2579 		break;
2580 
2581 	case SIOCSIFLLADDR:
2582 		error = priv_check(td, PRIV_NET_SETLLADDR);
2583 		if (error)
2584 			return (error);
2585 		error = if_setlladdr(ifp,
2586 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2587 		EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2588 		break;
2589 
2590 	case SIOCAIFGROUP:
2591 	{
2592 		struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2593 
2594 		error = priv_check(td, PRIV_NET_ADDIFGROUP);
2595 		if (error)
2596 			return (error);
2597 		if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2598 			return (error);
2599 		break;
2600 	}
2601 
2602 	case SIOCGIFGROUP:
2603 		if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2604 			return (error);
2605 		break;
2606 
2607 	case SIOCDIFGROUP:
2608 	{
2609 		struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2610 
2611 		error = priv_check(td, PRIV_NET_DELIFGROUP);
2612 		if (error)
2613 			return (error);
2614 		if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2615 			return (error);
2616 		break;
2617 	}
2618 
2619 	default:
2620 		error = ENOIOCTL;
2621 		break;
2622 	}
2623 	return (error);
2624 }
2625 
2626 #ifdef COMPAT_FREEBSD32
2627 struct ifconf32 {
2628 	int32_t	ifc_len;
2629 	union {
2630 		uint32_t	ifcu_buf;
2631 		uint32_t	ifcu_req;
2632 	} ifc_ifcu;
2633 };
2634 #define	SIOCGIFCONF32	_IOWR('i', 36, struct ifconf32)
2635 #endif
2636 
2637 /*
2638  * Interface ioctls.
2639  */
2640 int
ifioctl(struct socket * so,u_long cmd,caddr_t data,struct thread * td)2641 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2642 {
2643 	struct ifnet *ifp;
2644 	struct ifreq *ifr;
2645 	int error;
2646 	int oif_flags;
2647 
2648 	CURVNET_SET(so->so_vnet);
2649 	switch (cmd) {
2650 	case SIOCGIFCONF:
2651 	case OSIOCGIFCONF:
2652 		error = ifconf(cmd, data);
2653 		CURVNET_RESTORE();
2654 		return (error);
2655 
2656 #ifdef COMPAT_FREEBSD32
2657 	case SIOCGIFCONF32:
2658 		{
2659 			struct ifconf32 *ifc32;
2660 			struct ifconf ifc;
2661 
2662 			ifc32 = (struct ifconf32 *)data;
2663 			ifc.ifc_len = ifc32->ifc_len;
2664 			ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2665 
2666 			error = ifconf(SIOCGIFCONF, (void *)&ifc);
2667 			CURVNET_RESTORE();
2668 			if (error == 0)
2669 				ifc32->ifc_len = ifc.ifc_len;
2670 			return (error);
2671 		}
2672 #endif
2673 	}
2674 	ifr = (struct ifreq *)data;
2675 
2676 	switch (cmd) {
2677 #ifdef VIMAGE
2678 	case SIOCSIFRVNET:
2679 		error = priv_check(td, PRIV_NET_SETIFVNET);
2680 		if (error == 0)
2681 			error = if_vmove_reclaim(td, ifr->ifr_name,
2682 			    ifr->ifr_jid);
2683 		CURVNET_RESTORE();
2684 		return (error);
2685 #endif
2686 	case SIOCIFCREATE:
2687 	case SIOCIFCREATE2:
2688 		error = priv_check(td, PRIV_NET_IFCREATE);
2689 		if (error == 0)
2690 			error = if_clone_create(ifr->ifr_name,
2691 			    sizeof(ifr->ifr_name),
2692 			    cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2693 		CURVNET_RESTORE();
2694 		return (error);
2695 	case SIOCIFDESTROY:
2696 		error = priv_check(td, PRIV_NET_IFDESTROY);
2697 		if (error == 0)
2698 			error = if_clone_destroy(ifr->ifr_name);
2699 		CURVNET_RESTORE();
2700 		return (error);
2701 
2702 	case SIOCIFGCLONERS:
2703 		error = if_clone_list((struct if_clonereq *)data);
2704 		CURVNET_RESTORE();
2705 		return (error);
2706 	case SIOCGIFGMEMB:
2707 		error = if_getgroupmembers((struct ifgroupreq *)data);
2708 		CURVNET_RESTORE();
2709 		return (error);
2710 #if defined(INET) || defined(INET6)
2711 	case SIOCSVH:
2712 	case SIOCGVH:
2713 		if (carp_ioctl_p == NULL)
2714 			error = EPROTONOSUPPORT;
2715 		else
2716 			error = (*carp_ioctl_p)(ifr, cmd, td);
2717 		CURVNET_RESTORE();
2718 		return (error);
2719 #endif
2720 	}
2721 
2722 	ifp = ifunit_ref(ifr->ifr_name);
2723 	if (ifp == NULL) {
2724 		CURVNET_RESTORE();
2725 		return (ENXIO);
2726 	}
2727 
2728 	error = ifhwioctl(cmd, ifp, data, td);
2729 	if (error != ENOIOCTL) {
2730 		if_rele(ifp);
2731 		CURVNET_RESTORE();
2732 		return (error);
2733 	}
2734 
2735 	oif_flags = ifp->if_flags;
2736 	if (so->so_proto == NULL) {
2737 		if_rele(ifp);
2738 		CURVNET_RESTORE();
2739 		return (EOPNOTSUPP);
2740 	}
2741 
2742 	/*
2743 	 * Pass the request on to the socket control method, and if the
2744 	 * latter returns EOPNOTSUPP, directly to the interface.
2745 	 *
2746 	 * Make an exception for the legacy SIOCSIF* requests.  Drivers
2747 	 * trust SIOCSIFADDR et al to come from an already privileged
2748 	 * layer, and do not perform any credentials checks or input
2749 	 * validation.
2750 	 */
2751 #ifndef COMPAT_43
2752 	error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2753 								 data,
2754 								 ifp, td));
2755 	if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2756 	    cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2757 	    cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2758 		error = (*ifp->if_ioctl)(ifp, cmd, data);
2759 #else
2760 	{
2761 		u_long ocmd = cmd;
2762 
2763 		switch (cmd) {
2764 
2765 		case SIOCSIFDSTADDR:
2766 		case SIOCSIFADDR:
2767 		case SIOCSIFBRDADDR:
2768 		case SIOCSIFNETMASK:
2769 #if BYTE_ORDER != BIG_ENDIAN
2770 			if (ifr->ifr_addr.sa_family == 0 &&
2771 			    ifr->ifr_addr.sa_len < 16) {
2772 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2773 				ifr->ifr_addr.sa_len = 16;
2774 			}
2775 #else
2776 			if (ifr->ifr_addr.sa_len == 0)
2777 				ifr->ifr_addr.sa_len = 16;
2778 #endif
2779 			break;
2780 
2781 		case OSIOCGIFADDR:
2782 			cmd = SIOCGIFADDR;
2783 			break;
2784 
2785 		case OSIOCGIFDSTADDR:
2786 			cmd = SIOCGIFDSTADDR;
2787 			break;
2788 
2789 		case OSIOCGIFBRDADDR:
2790 			cmd = SIOCGIFBRDADDR;
2791 			break;
2792 
2793 		case OSIOCGIFNETMASK:
2794 			cmd = SIOCGIFNETMASK;
2795 		}
2796 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
2797 								   cmd,
2798 								   data,
2799 								   ifp, td));
2800 		if (error == EOPNOTSUPP && ifp != NULL &&
2801 		    ifp->if_ioctl != NULL &&
2802 		    cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2803 		    cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2804 			error = (*ifp->if_ioctl)(ifp, cmd, data);
2805 		switch (ocmd) {
2806 
2807 		case OSIOCGIFADDR:
2808 		case OSIOCGIFDSTADDR:
2809 		case OSIOCGIFBRDADDR:
2810 		case OSIOCGIFNETMASK:
2811 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2812 
2813 		}
2814 	}
2815 #endif /* COMPAT_43 */
2816 
2817 	if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2818 #ifdef INET6
2819 		if (ifp->if_flags & IFF_UP)
2820 			in6_if_up(ifp);
2821 #endif
2822 	}
2823 	if_rele(ifp);
2824 	CURVNET_RESTORE();
2825 	return (error);
2826 }
2827 
2828 /*
2829  * The code common to handling reference counted flags,
2830  * e.g., in ifpromisc() and if_allmulti().
2831  * The "pflag" argument can specify a permanent mode flag to check,
2832  * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2833  *
2834  * Only to be used on stack-owned flags, not driver-owned flags.
2835  */
2836 static int
if_setflag(struct ifnet * ifp,int flag,int pflag,int * refcount,int onswitch)2837 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2838 {
2839 	struct ifreq ifr;
2840 	int error;
2841 	int oldflags, oldcount;
2842 
2843 	/* Sanity checks to catch programming errors */
2844 	KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2845 	    ("%s: setting driver-owned flag %d", __func__, flag));
2846 
2847 	if (onswitch)
2848 		KASSERT(*refcount >= 0,
2849 		    ("%s: increment negative refcount %d for flag %d",
2850 		    __func__, *refcount, flag));
2851 	else
2852 		KASSERT(*refcount > 0,
2853 		    ("%s: decrement non-positive refcount %d for flag %d",
2854 		    __func__, *refcount, flag));
2855 
2856 	/* In case this mode is permanent, just touch refcount */
2857 	if (ifp->if_flags & pflag) {
2858 		*refcount += onswitch ? 1 : -1;
2859 		return (0);
2860 	}
2861 
2862 	/* Save ifnet parameters for if_ioctl() may fail */
2863 	oldcount = *refcount;
2864 	oldflags = ifp->if_flags;
2865 
2866 	/*
2867 	 * See if we aren't the only and touching refcount is enough.
2868 	 * Actually toggle interface flag if we are the first or last.
2869 	 */
2870 	if (onswitch) {
2871 		if ((*refcount)++)
2872 			return (0);
2873 		ifp->if_flags |= flag;
2874 	} else {
2875 		if (--(*refcount))
2876 			return (0);
2877 		ifp->if_flags &= ~flag;
2878 	}
2879 
2880 	/* Call down the driver since we've changed interface flags */
2881 	if (ifp->if_ioctl == NULL) {
2882 		error = EOPNOTSUPP;
2883 		goto recover;
2884 	}
2885 	ifr.ifr_flags = ifp->if_flags & 0xffff;
2886 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
2887 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2888 	if (error)
2889 		goto recover;
2890 	/* Notify userland that interface flags have changed */
2891 	rt_ifmsg(ifp);
2892 	return (0);
2893 
2894 recover:
2895 	/* Recover after driver error */
2896 	*refcount = oldcount;
2897 	ifp->if_flags = oldflags;
2898 	return (error);
2899 }
2900 
2901 /*
2902  * Set/clear promiscuous mode on interface ifp based on the truth value
2903  * of pswitch.  The calls are reference counted so that only the first
2904  * "on" request actually has an effect, as does the final "off" request.
2905  * Results are undefined if the "off" and "on" requests are not matched.
2906  */
2907 int
ifpromisc(struct ifnet * ifp,int pswitch)2908 ifpromisc(struct ifnet *ifp, int pswitch)
2909 {
2910 	int error;
2911 	int oldflags = ifp->if_flags;
2912 
2913 	error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2914 			   &ifp->if_pcount, pswitch);
2915 	/* If promiscuous mode status has changed, log a message */
2916 	if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2917 		log(LOG_INFO, "%s: promiscuous mode %s\n",
2918 		    ifp->if_xname,
2919 		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2920 	return (error);
2921 }
2922 
2923 /*
2924  * Return interface configuration
2925  * of system.  List may be used
2926  * in later ioctl's (above) to get
2927  * other information.
2928  */
2929 /*ARGSUSED*/
2930 static int
ifconf(u_long cmd,caddr_t data)2931 ifconf(u_long cmd, caddr_t data)
2932 {
2933 	struct ifconf *ifc = (struct ifconf *)data;
2934 	struct ifnet *ifp;
2935 	struct ifaddr *ifa;
2936 	struct ifreq ifr;
2937 	struct sbuf *sb;
2938 	int error, full = 0, valid_len, max_len;
2939 
2940 	/* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2941 	max_len = MAXPHYS - 1;
2942 
2943 	/* Prevent hostile input from being able to crash the system */
2944 	if (ifc->ifc_len <= 0)
2945 		return (EINVAL);
2946 
2947 again:
2948 	if (ifc->ifc_len <= max_len) {
2949 		max_len = ifc->ifc_len;
2950 		full = 1;
2951 	}
2952 	sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2953 	max_len = 0;
2954 	valid_len = 0;
2955 
2956 	IFNET_RLOCK();
2957 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2958 		int addrs;
2959 
2960 		/*
2961 		 * Zero the ifr_name buffer to make sure we don't
2962 		 * disclose the contents of the stack.
2963 		 */
2964 		memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2965 
2966 		if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2967 		    >= sizeof(ifr.ifr_name)) {
2968 			sbuf_delete(sb);
2969 			IFNET_RUNLOCK();
2970 			return (ENAMETOOLONG);
2971 		}
2972 
2973 		addrs = 0;
2974 		IF_ADDR_RLOCK(ifp);
2975 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2976 			struct sockaddr *sa = ifa->ifa_addr;
2977 
2978 			if (prison_if(curthread->td_ucred, sa) != 0)
2979 				continue;
2980 			addrs++;
2981 #ifdef COMPAT_43
2982 			if (cmd == OSIOCGIFCONF) {
2983 				struct osockaddr *osa =
2984 					 (struct osockaddr *)&ifr.ifr_addr;
2985 				ifr.ifr_addr = *sa;
2986 				osa->sa_family = sa->sa_family;
2987 				sbuf_bcat(sb, &ifr, sizeof(ifr));
2988 				max_len += sizeof(ifr);
2989 			} else
2990 #endif
2991 			if (sa->sa_len <= sizeof(*sa)) {
2992 				ifr.ifr_addr = *sa;
2993 				sbuf_bcat(sb, &ifr, sizeof(ifr));
2994 				max_len += sizeof(ifr);
2995 			} else {
2996 				sbuf_bcat(sb, &ifr,
2997 				    offsetof(struct ifreq, ifr_addr));
2998 				max_len += offsetof(struct ifreq, ifr_addr);
2999 				sbuf_bcat(sb, sa, sa->sa_len);
3000 				max_len += sa->sa_len;
3001 			}
3002 
3003 			if (sbuf_error(sb) == 0)
3004 				valid_len = sbuf_len(sb);
3005 		}
3006 		IF_ADDR_RUNLOCK(ifp);
3007 		if (addrs == 0) {
3008 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
3009 			sbuf_bcat(sb, &ifr, sizeof(ifr));
3010 			max_len += sizeof(ifr);
3011 
3012 			if (sbuf_error(sb) == 0)
3013 				valid_len = sbuf_len(sb);
3014 		}
3015 	}
3016 	IFNET_RUNLOCK();
3017 
3018 	/*
3019 	 * If we didn't allocate enough space (uncommon), try again.  If
3020 	 * we have already allocated as much space as we are allowed,
3021 	 * return what we've got.
3022 	 */
3023 	if (valid_len != max_len && !full) {
3024 		sbuf_delete(sb);
3025 		goto again;
3026 	}
3027 
3028 	ifc->ifc_len = valid_len;
3029 	sbuf_finish(sb);
3030 	error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3031 	sbuf_delete(sb);
3032 	return (error);
3033 }
3034 
3035 /*
3036  * Just like ifpromisc(), but for all-multicast-reception mode.
3037  */
3038 int
if_allmulti(struct ifnet * ifp,int onswitch)3039 if_allmulti(struct ifnet *ifp, int onswitch)
3040 {
3041 
3042 	return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3043 }
3044 
3045 struct ifmultiaddr *
if_findmulti(struct ifnet * ifp,struct sockaddr * sa)3046 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
3047 {
3048 	struct ifmultiaddr *ifma;
3049 
3050 	IF_ADDR_LOCK_ASSERT(ifp);
3051 
3052 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3053 		if (sa->sa_family == AF_LINK) {
3054 			if (sa_dl_equal(ifma->ifma_addr, sa))
3055 				break;
3056 		} else {
3057 			if (sa_equal(ifma->ifma_addr, sa))
3058 				break;
3059 		}
3060 	}
3061 
3062 	return ifma;
3063 }
3064 
3065 /*
3066  * Allocate a new ifmultiaddr and initialize based on passed arguments.  We
3067  * make copies of passed sockaddrs.  The ifmultiaddr will not be added to
3068  * the ifnet multicast address list here, so the caller must do that and
3069  * other setup work (such as notifying the device driver).  The reference
3070  * count is initialized to 1.
3071  */
3072 static struct ifmultiaddr *
if_allocmulti(struct ifnet * ifp,struct sockaddr * sa,struct sockaddr * llsa,int mflags)3073 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3074     int mflags)
3075 {
3076 	struct ifmultiaddr *ifma;
3077 	struct sockaddr *dupsa;
3078 
3079 	ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3080 	    M_ZERO);
3081 	if (ifma == NULL)
3082 		return (NULL);
3083 
3084 	dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3085 	if (dupsa == NULL) {
3086 		free(ifma, M_IFMADDR);
3087 		return (NULL);
3088 	}
3089 	bcopy(sa, dupsa, sa->sa_len);
3090 	ifma->ifma_addr = dupsa;
3091 
3092 	ifma->ifma_ifp = ifp;
3093 	ifma->ifma_refcount = 1;
3094 	ifma->ifma_protospec = NULL;
3095 
3096 	if (llsa == NULL) {
3097 		ifma->ifma_lladdr = NULL;
3098 		return (ifma);
3099 	}
3100 
3101 	dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3102 	if (dupsa == NULL) {
3103 		free(ifma->ifma_addr, M_IFMADDR);
3104 		free(ifma, M_IFMADDR);
3105 		return (NULL);
3106 	}
3107 	bcopy(llsa, dupsa, llsa->sa_len);
3108 	ifma->ifma_lladdr = dupsa;
3109 
3110 	return (ifma);
3111 }
3112 
3113 /*
3114  * if_freemulti: free ifmultiaddr structure and possibly attached related
3115  * addresses.  The caller is responsible for implementing reference
3116  * counting, notifying the driver, handling routing messages, and releasing
3117  * any dependent link layer state.
3118  */
3119 static void
if_freemulti(struct ifmultiaddr * ifma)3120 if_freemulti(struct ifmultiaddr *ifma)
3121 {
3122 
3123 	KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3124 	    ifma->ifma_refcount));
3125 	KASSERT(ifma->ifma_protospec == NULL,
3126 	    ("if_freemulti: protospec not NULL"));
3127 
3128 	if (ifma->ifma_lladdr != NULL)
3129 		free(ifma->ifma_lladdr, M_IFMADDR);
3130 	free(ifma->ifma_addr, M_IFMADDR);
3131 	free(ifma, M_IFMADDR);
3132 }
3133 
3134 /*
3135  * Register an additional multicast address with a network interface.
3136  *
3137  * - If the address is already present, bump the reference count on the
3138  *   address and return.
3139  * - If the address is not link-layer, look up a link layer address.
3140  * - Allocate address structures for one or both addresses, and attach to the
3141  *   multicast address list on the interface.  If automatically adding a link
3142  *   layer address, the protocol address will own a reference to the link
3143  *   layer address, to be freed when it is freed.
3144  * - Notify the network device driver of an addition to the multicast address
3145  *   list.
3146  *
3147  * 'sa' points to caller-owned memory with the desired multicast address.
3148  *
3149  * 'retifma' will be used to return a pointer to the resulting multicast
3150  * address reference, if desired.
3151  */
3152 int
if_addmulti(struct ifnet * ifp,struct sockaddr * sa,struct ifmultiaddr ** retifma)3153 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3154     struct ifmultiaddr **retifma)
3155 {
3156 	struct ifmultiaddr *ifma, *ll_ifma;
3157 	struct sockaddr *llsa;
3158 	int error;
3159 
3160 	/*
3161 	 * If the address is already present, return a new reference to it;
3162 	 * otherwise, allocate storage and set up a new address.
3163 	 */
3164 	IF_ADDR_WLOCK(ifp);
3165 	ifma = if_findmulti(ifp, sa);
3166 	if (ifma != NULL) {
3167 		ifma->ifma_refcount++;
3168 		if (retifma != NULL)
3169 			*retifma = ifma;
3170 		IF_ADDR_WUNLOCK(ifp);
3171 		return (0);
3172 	}
3173 
3174 	/*
3175 	 * The address isn't already present; resolve the protocol address
3176 	 * into a link layer address, and then look that up, bump its
3177 	 * refcount or allocate an ifma for that also.  If 'llsa' was
3178 	 * returned, we will need to free it later.
3179 	 */
3180 	llsa = NULL;
3181 	ll_ifma = NULL;
3182 	if (ifp->if_resolvemulti != NULL) {
3183 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
3184 		if (error)
3185 			goto unlock_out;
3186 	}
3187 
3188 	/*
3189 	 * Allocate the new address.  Don't hook it up yet, as we may also
3190 	 * need to allocate a link layer multicast address.
3191 	 */
3192 	ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3193 	if (ifma == NULL) {
3194 		error = ENOMEM;
3195 		goto free_llsa_out;
3196 	}
3197 
3198 	/*
3199 	 * If a link layer address is found, we'll need to see if it's
3200 	 * already present in the address list, or allocate is as well.
3201 	 * When this block finishes, the link layer address will be on the
3202 	 * list.
3203 	 */
3204 	if (llsa != NULL) {
3205 		ll_ifma = if_findmulti(ifp, llsa);
3206 		if (ll_ifma == NULL) {
3207 			ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3208 			if (ll_ifma == NULL) {
3209 				--ifma->ifma_refcount;
3210 				if_freemulti(ifma);
3211 				error = ENOMEM;
3212 				goto free_llsa_out;
3213 			}
3214 			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3215 			    ifma_link);
3216 		} else
3217 			ll_ifma->ifma_refcount++;
3218 		ifma->ifma_llifma = ll_ifma;
3219 	}
3220 
3221 	/*
3222 	 * We now have a new multicast address, ifma, and possibly a new or
3223 	 * referenced link layer address.  Add the primary address to the
3224 	 * ifnet address list.
3225 	 */
3226 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3227 
3228 	if (retifma != NULL)
3229 		*retifma = ifma;
3230 
3231 	/*
3232 	 * Must generate the message while holding the lock so that 'ifma'
3233 	 * pointer is still valid.
3234 	 */
3235 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3236 	IF_ADDR_WUNLOCK(ifp);
3237 
3238 	/*
3239 	 * We are certain we have added something, so call down to the
3240 	 * interface to let them know about it.
3241 	 */
3242 	if (ifp->if_ioctl != NULL) {
3243 		(void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3244 	}
3245 
3246 	if (llsa != NULL)
3247 		free(llsa, M_IFMADDR);
3248 
3249 	return (0);
3250 
3251 free_llsa_out:
3252 	if (llsa != NULL)
3253 		free(llsa, M_IFMADDR);
3254 
3255 unlock_out:
3256 	IF_ADDR_WUNLOCK(ifp);
3257 	return (error);
3258 }
3259 
3260 /*
3261  * Delete a multicast group membership by network-layer group address.
3262  *
3263  * Returns ENOENT if the entry could not be found. If ifp no longer
3264  * exists, results are undefined. This entry point should only be used
3265  * from subsystems which do appropriate locking to hold ifp for the
3266  * duration of the call.
3267  * Network-layer protocol domains must use if_delmulti_ifma().
3268  */
3269 int
if_delmulti(struct ifnet * ifp,struct sockaddr * sa)3270 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3271 {
3272 	struct ifmultiaddr *ifma;
3273 	int lastref;
3274 #ifdef INVARIANTS
3275 	struct ifnet *oifp;
3276 
3277 	IFNET_RLOCK_NOSLEEP();
3278 	TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3279 		if (ifp == oifp)
3280 			break;
3281 	if (ifp != oifp)
3282 		ifp = NULL;
3283 	IFNET_RUNLOCK_NOSLEEP();
3284 
3285 	KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3286 #endif
3287 	if (ifp == NULL)
3288 		return (ENOENT);
3289 
3290 	IF_ADDR_WLOCK(ifp);
3291 	lastref = 0;
3292 	ifma = if_findmulti(ifp, sa);
3293 	if (ifma != NULL)
3294 		lastref = if_delmulti_locked(ifp, ifma, 0);
3295 	IF_ADDR_WUNLOCK(ifp);
3296 
3297 	if (ifma == NULL)
3298 		return (ENOENT);
3299 
3300 	if (lastref && ifp->if_ioctl != NULL) {
3301 		(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3302 	}
3303 
3304 	return (0);
3305 }
3306 
3307 /*
3308  * Delete all multicast group membership for an interface.
3309  * Should be used to quickly flush all multicast filters.
3310  */
3311 void
if_delallmulti(struct ifnet * ifp)3312 if_delallmulti(struct ifnet *ifp)
3313 {
3314 	struct ifmultiaddr *ifma;
3315 	struct ifmultiaddr *next;
3316 
3317 	IF_ADDR_WLOCK(ifp);
3318 	TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3319 		if_delmulti_locked(ifp, ifma, 0);
3320 	IF_ADDR_WUNLOCK(ifp);
3321 }
3322 
3323 /*
3324  * Delete a multicast group membership by group membership pointer.
3325  * Network-layer protocol domains must use this routine.
3326  *
3327  * It is safe to call this routine if the ifp disappeared.
3328  */
3329 void
if_delmulti_ifma(struct ifmultiaddr * ifma)3330 if_delmulti_ifma(struct ifmultiaddr *ifma)
3331 {
3332 	struct ifnet *ifp;
3333 	int lastref;
3334 
3335 	ifp = ifma->ifma_ifp;
3336 #ifdef DIAGNOSTIC
3337 	if (ifp == NULL) {
3338 		printf("%s: ifma_ifp seems to be detached\n", __func__);
3339 	} else {
3340 		struct ifnet *oifp;
3341 
3342 		IFNET_RLOCK_NOSLEEP();
3343 		TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3344 			if (ifp == oifp)
3345 				break;
3346 		if (ifp != oifp) {
3347 			printf("%s: ifnet %p disappeared\n", __func__, ifp);
3348 			ifp = NULL;
3349 		}
3350 		IFNET_RUNLOCK_NOSLEEP();
3351 	}
3352 #endif
3353 	/*
3354 	 * If and only if the ifnet instance exists: Acquire the address lock.
3355 	 */
3356 	if (ifp != NULL)
3357 		IF_ADDR_WLOCK(ifp);
3358 
3359 	lastref = if_delmulti_locked(ifp, ifma, 0);
3360 
3361 	if (ifp != NULL) {
3362 		/*
3363 		 * If and only if the ifnet instance exists:
3364 		 *  Release the address lock.
3365 		 *  If the group was left: update the hardware hash filter.
3366 		 */
3367 		IF_ADDR_WUNLOCK(ifp);
3368 		if (lastref && ifp->if_ioctl != NULL) {
3369 			(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3370 		}
3371 	}
3372 }
3373 
3374 /*
3375  * Perform deletion of network-layer and/or link-layer multicast address.
3376  *
3377  * Return 0 if the reference count was decremented.
3378  * Return 1 if the final reference was released, indicating that the
3379  * hardware hash filter should be reprogrammed.
3380  */
3381 static int
if_delmulti_locked(struct ifnet * ifp,struct ifmultiaddr * ifma,int detaching)3382 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3383 {
3384 	struct ifmultiaddr *ll_ifma;
3385 
3386 	if (ifp != NULL && ifma->ifma_ifp != NULL) {
3387 		KASSERT(ifma->ifma_ifp == ifp,
3388 		    ("%s: inconsistent ifp %p", __func__, ifp));
3389 		IF_ADDR_WLOCK_ASSERT(ifp);
3390 	}
3391 
3392 	ifp = ifma->ifma_ifp;
3393 
3394 	/*
3395 	 * If the ifnet is detaching, null out references to ifnet,
3396 	 * so that upper protocol layers will notice, and not attempt
3397 	 * to obtain locks for an ifnet which no longer exists. The
3398 	 * routing socket announcement must happen before the ifnet
3399 	 * instance is detached from the system.
3400 	 */
3401 	if (detaching) {
3402 #ifdef DIAGNOSTIC
3403 		printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3404 #endif
3405 		/*
3406 		 * ifp may already be nulled out if we are being reentered
3407 		 * to delete the ll_ifma.
3408 		 */
3409 		if (ifp != NULL) {
3410 			rt_newmaddrmsg(RTM_DELMADDR, ifma);
3411 			ifma->ifma_ifp = NULL;
3412 		}
3413 	}
3414 
3415 	if (--ifma->ifma_refcount > 0)
3416 		return 0;
3417 
3418 	/*
3419 	 * If this ifma is a network-layer ifma, a link-layer ifma may
3420 	 * have been associated with it. Release it first if so.
3421 	 */
3422 	ll_ifma = ifma->ifma_llifma;
3423 	if (ll_ifma != NULL) {
3424 		KASSERT(ifma->ifma_lladdr != NULL,
3425 		    ("%s: llifma w/o lladdr", __func__));
3426 		if (detaching)
3427 			ll_ifma->ifma_ifp = NULL;	/* XXX */
3428 		if (--ll_ifma->ifma_refcount == 0) {
3429 			if (ifp != NULL) {
3430 				TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3431 				    ifma_link);
3432 			}
3433 			if_freemulti(ll_ifma);
3434 		}
3435 	}
3436 
3437 	if (ifp != NULL)
3438 		TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3439 
3440 	if_freemulti(ifma);
3441 
3442 	/*
3443 	 * The last reference to this instance of struct ifmultiaddr
3444 	 * was released; the hardware should be notified of this change.
3445 	 */
3446 	return 1;
3447 }
3448 
3449 /*
3450  * Set the link layer address on an interface.
3451  *
3452  * At this time we only support certain types of interfaces,
3453  * and we don't allow the length of the address to change.
3454  */
3455 int
if_setlladdr(struct ifnet * ifp,const u_char * lladdr,int len)3456 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3457 {
3458 	struct sockaddr_dl *sdl;
3459 	struct ifaddr *ifa;
3460 	struct ifreq ifr;
3461 
3462 	IF_ADDR_RLOCK(ifp);
3463 	ifa = ifp->if_addr;
3464 	if (ifa == NULL) {
3465 		IF_ADDR_RUNLOCK(ifp);
3466 		return (EINVAL);
3467 	}
3468 	ifa_ref(ifa);
3469 	IF_ADDR_RUNLOCK(ifp);
3470 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3471 	if (sdl == NULL) {
3472 		ifa_free(ifa);
3473 		return (EINVAL);
3474 	}
3475 	if (len != sdl->sdl_alen) {	/* don't allow length to change */
3476 		ifa_free(ifa);
3477 		return (EINVAL);
3478 	}
3479 	switch (ifp->if_type) {
3480 	case IFT_ETHER:
3481 	case IFT_FDDI:
3482 	case IFT_XETHER:
3483 	case IFT_ISO88025:
3484 	case IFT_L2VLAN:
3485 	case IFT_BRIDGE:
3486 	case IFT_ARCNET:
3487 	case IFT_IEEE8023ADLAG:
3488 	case IFT_IEEE80211:
3489 		bcopy(lladdr, LLADDR(sdl), len);
3490 		ifa_free(ifa);
3491 		break;
3492 	default:
3493 		ifa_free(ifa);
3494 		return (ENODEV);
3495 	}
3496 
3497 	/*
3498 	 * If the interface is already up, we need
3499 	 * to re-init it in order to reprogram its
3500 	 * address filter.
3501 	 */
3502 	if ((ifp->if_flags & IFF_UP) != 0) {
3503 		if (ifp->if_ioctl) {
3504 			ifp->if_flags &= ~IFF_UP;
3505 			ifr.ifr_flags = ifp->if_flags & 0xffff;
3506 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
3507 			(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3508 			ifp->if_flags |= IFF_UP;
3509 			ifr.ifr_flags = ifp->if_flags & 0xffff;
3510 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
3511 			(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3512 		}
3513 #ifdef INET
3514 		/*
3515 		 * Also send gratuitous ARPs to notify other nodes about
3516 		 * the address change.
3517 		 */
3518 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3519 			if (ifa->ifa_addr->sa_family == AF_INET)
3520 				arp_ifinit(ifp, ifa);
3521 		}
3522 #endif
3523 	}
3524 	return (0);
3525 }
3526 
3527 /*
3528  * The name argument must be a pointer to storage which will last as
3529  * long as the interface does.  For physical devices, the result of
3530  * device_get_name(dev) is a good choice and for pseudo-devices a
3531  * static string works well.
3532  */
3533 void
if_initname(struct ifnet * ifp,const char * name,int unit)3534 if_initname(struct ifnet *ifp, const char *name, int unit)
3535 {
3536 	ifp->if_dname = name;
3537 	ifp->if_dunit = unit;
3538 	if (unit != IF_DUNIT_NONE)
3539 		snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3540 	else
3541 		strlcpy(ifp->if_xname, name, IFNAMSIZ);
3542 }
3543 
3544 int
if_printf(struct ifnet * ifp,const char * fmt,...)3545 if_printf(struct ifnet *ifp, const char * fmt, ...)
3546 {
3547 	va_list ap;
3548 	int retval;
3549 
3550 	retval = printf("%s: ", ifp->if_xname);
3551 	va_start(ap, fmt);
3552 	retval += vprintf(fmt, ap);
3553 	va_end(ap);
3554 	return (retval);
3555 }
3556 
3557 void
if_start(struct ifnet * ifp)3558 if_start(struct ifnet *ifp)
3559 {
3560 
3561 	(*(ifp)->if_start)(ifp);
3562 }
3563 
3564 /*
3565  * Backwards compatibility interface for drivers
3566  * that have not implemented it
3567  */
3568 static int
if_transmit(struct ifnet * ifp,struct mbuf * m)3569 if_transmit(struct ifnet *ifp, struct mbuf *m)
3570 {
3571 	int error;
3572 
3573 	IFQ_HANDOFF(ifp, m, error);
3574 	return (error);
3575 }
3576 
3577 static void
if_input_default(struct ifnet * ifp __unused,struct mbuf * m)3578 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3579 {
3580 
3581 	m_freem(m);
3582 }
3583 
3584 int
if_handoff(struct ifqueue * ifq,struct mbuf * m,struct ifnet * ifp,int adjust)3585 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3586 {
3587 	int active = 0;
3588 
3589 	IF_LOCK(ifq);
3590 	if (_IF_QFULL(ifq)) {
3591 		_IF_DROP(ifq);
3592 		IF_UNLOCK(ifq);
3593 		m_freem(m);
3594 		return (0);
3595 	}
3596 	if (ifp != NULL) {
3597 		ifp->if_obytes += m->m_pkthdr.len + adjust;
3598 		if (m->m_flags & (M_BCAST|M_MCAST))
3599 			ifp->if_omcasts++;
3600 		active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3601 	}
3602 	_IF_ENQUEUE(ifq, m);
3603 	IF_UNLOCK(ifq);
3604 	if (ifp != NULL && !active)
3605 		(*(ifp)->if_start)(ifp);
3606 	return (1);
3607 }
3608 
3609 void
if_register_com_alloc(u_char type,if_com_alloc_t * a,if_com_free_t * f)3610 if_register_com_alloc(u_char type,
3611     if_com_alloc_t *a, if_com_free_t *f)
3612 {
3613 
3614 	KASSERT(if_com_alloc[type] == NULL,
3615 	    ("if_register_com_alloc: %d already registered", type));
3616 	KASSERT(if_com_free[type] == NULL,
3617 	    ("if_register_com_alloc: %d free already registered", type));
3618 
3619 	if_com_alloc[type] = a;
3620 	if_com_free[type] = f;
3621 }
3622 
3623 void
if_deregister_com_alloc(u_char type)3624 if_deregister_com_alloc(u_char type)
3625 {
3626 
3627 	KASSERT(if_com_alloc[type] != NULL,
3628 	    ("if_deregister_com_alloc: %d not registered", type));
3629 	KASSERT(if_com_free[type] != NULL,
3630 	    ("if_deregister_com_alloc: %d free not registered", type));
3631 	if_com_alloc[type] = NULL;
3632 	if_com_free[type] = NULL;
3633 }
3634