xref: /dragonfly/sys/net/if.c (revision 35e996c9)
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  * 3. 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.3 (Berkeley) 1/4/94
30  * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
31  */
32 
33 #include "opt_inet6.h"
34 #include "opt_inet.h"
35 #include "opt_ifpoll.h"
36 
37 #include <sys/param.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/caps.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/socketops.h>
47 #include <sys/kernel.h>
48 #include <sys/ktr.h>
49 #include <sys/mutex.h>
50 #include <sys/lock.h>
51 #include <sys/sockio.h>
52 #include <sys/syslog.h>
53 #include <sys/sysctl.h>
54 #include <sys/domain.h>
55 #include <sys/thread.h>
56 #include <sys/serialize.h>
57 #include <sys/bus.h>
58 #include <sys/jail.h>
59 
60 #include <sys/thread2.h>
61 #include <sys/msgport2.h>
62 #include <sys/mutex2.h>
63 
64 #include <net/if.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/if_var.h>
69 #include <net/if_ringmap.h>
70 #include <net/ifq_var.h>
71 #include <net/radix.h>
72 #include <net/route.h>
73 #include <net/if_clone.h>
74 #include <net/netisr2.h>
75 #include <net/netmsg2.h>
76 
77 #include <machine/atomic.h>
78 #include <machine/stdarg.h>
79 #include <machine/smp.h>
80 
81 #if defined(INET) || defined(INET6)
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/if_ether.h>
85 #ifdef INET6
86 #include <netinet6/in6_var.h>
87 #include <netinet6/in6_ifattach.h>
88 #endif /* INET6 */
89 #endif /* INET || INET6 */
90 
91 struct netmsg_ifaddr {
92 	struct netmsg_base base;
93 	struct ifaddr	*ifa;
94 	struct ifnet	*ifp;
95 	int		tail;
96 };
97 
98 struct ifsubq_stage_head {
99 	TAILQ_HEAD(, ifsubq_stage)	stg_head;
100 } __cachealign;
101 
102 struct if_ringmap {
103 	int		rm_cnt;
104 	int		rm_grid;
105 	int		rm_cpumap[];
106 };
107 
108 #define RINGMAP_FLAG_NONE		0x0
109 #define RINGMAP_FLAG_POWEROF2		0x1
110 
111 /*
112  * System initialization
113  */
114 static void	if_attachdomain(void *);
115 static void	if_attachdomain1(struct ifnet *);
116 static int	ifconf(u_long, caddr_t, struct ucred *);
117 static void	ifinit(void *);
118 static void	ifnetinit(void *);
119 static void	if_slowtimo(void *);
120 static void	link_rtrequest(int, struct rtentry *);
121 static int	if_rtdel(struct radix_node *, void *);
122 static void	if_slowtimo_dispatch(netmsg_t);
123 
124 /* Helper functions */
125 static void	ifsq_watchdog_reset(struct ifsubq_watchdog *);
126 static int	if_delmulti_serialized(struct ifnet *, struct sockaddr *);
127 static struct ifnet_array *ifnet_array_alloc(int);
128 static void	ifnet_array_free(struct ifnet_array *);
129 static struct ifnet_array *ifnet_array_add(struct ifnet *,
130 		    const struct ifnet_array *);
131 static struct ifnet_array *ifnet_array_del(struct ifnet *,
132 		    const struct ifnet_array *);
133 static struct ifg_group *if_creategroup(const char *);
134 static int	if_destroygroup(struct ifg_group *);
135 static int	if_delgroup_locked(struct ifnet *, const char *);
136 static int	if_getgroups(struct ifgroupreq *, struct ifnet *);
137 static int	if_getgroupmembers(struct ifgroupreq *);
138 
139 #ifdef INET6
140 /*
141  * XXX: declare here to avoid to include many inet6 related files..
142  * should be more generalized?
143  */
144 extern void	nd6_setmtu(struct ifnet *);
145 #endif
146 
147 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
148 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
149 SYSCTL_NODE(_net_link, OID_AUTO, ringmap, CTLFLAG_RW, 0, "link ringmap");
150 
151 static int ifsq_stage_cntmax = 16;
152 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
153 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
154     &ifsq_stage_cntmax, 0, "ifq staging packet count max");
155 
156 static int if_stats_compat = 0;
157 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
158     &if_stats_compat, 0, "Compat the old ifnet stats");
159 
160 static int if_ringmap_dumprdr = 0;
161 SYSCTL_INT(_net_link_ringmap, OID_AUTO, dump_rdr, CTLFLAG_RW,
162     &if_ringmap_dumprdr, 0, "dump redirect table");
163 
164 /* Interface description */
165 static unsigned int ifdescr_maxlen = 1024;
166 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
167 	&ifdescr_maxlen, 0,
168 	"administrative maximum length for interface description");
169 
170 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL);
171 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, ifnetinit, NULL);
172 
173 static if_com_alloc_t *if_com_alloc[256];
174 static if_com_free_t *if_com_free[256];
175 
176 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
177 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
178 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
179 MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
180 
181 int			ifqmaxlen = IFQ_MAXLEN;
182 struct ifnethead	ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
183 struct ifgrouphead	ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
184 static struct lock	ifgroup_lock;
185 
186 static struct ifnet_array	ifnet_array0;
187 static struct ifnet_array	*ifnet_array = &ifnet_array0;
188 
189 static struct callout		if_slowtimo_timer;
190 static struct netmsg_base	if_slowtimo_netmsg;
191 
192 int			if_index = 0;
193 struct ifnet		**ifindex2ifnet = NULL;
194 static struct mtx	ifnet_mtx = MTX_INITIALIZER("ifnet");
195 
196 static struct ifsubq_stage_head	ifsubq_stage_heads[MAXCPU];
197 
198 #ifdef notyet
199 #define IFQ_KTR_STRING		"ifq=%p"
200 #define IFQ_KTR_ARGS		struct ifaltq *ifq
201 #ifndef KTR_IFQ
202 #define KTR_IFQ			KTR_ALL
203 #endif
204 KTR_INFO_MASTER(ifq);
205 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
206 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
207 #define logifq(name, arg)	KTR_LOG(ifq_ ## name, arg)
208 
209 #define IF_START_KTR_STRING	"ifp=%p"
210 #define IF_START_KTR_ARGS	struct ifnet *ifp
211 #ifndef KTR_IF_START
212 #define KTR_IF_START		KTR_ALL
213 #endif
214 KTR_INFO_MASTER(if_start);
215 KTR_INFO(KTR_IF_START, if_start, run, 0,
216 	 IF_START_KTR_STRING, IF_START_KTR_ARGS);
217 KTR_INFO(KTR_IF_START, if_start, sched, 1,
218 	 IF_START_KTR_STRING, IF_START_KTR_ARGS);
219 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
220 	 IF_START_KTR_STRING, IF_START_KTR_ARGS);
221 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
222 	 IF_START_KTR_STRING, IF_START_KTR_ARGS);
223 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
224 	 IF_START_KTR_STRING, IF_START_KTR_ARGS);
225 #define logifstart(name, arg)	KTR_LOG(if_start_ ## name, arg)
226 #endif /* notyet */
227 
228 /*
229  * Network interface utility routines.
230  *
231  * Routines with ifa_ifwith* names take sockaddr *'s as
232  * parameters.
233  */
234 /* ARGSUSED */
235 static void
236 ifinit(void *dummy)
237 {
238 	lockinit(&ifgroup_lock, "ifgroup", 0, 0);
239 
240 	callout_init_mp(&if_slowtimo_timer);
241 	netmsg_init(&if_slowtimo_netmsg, NULL, &netisr_adone_rport,
242 	    MSGF_PRIORITY, if_slowtimo_dispatch);
243 
244 	/* Start if_slowtimo */
245 	lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg.lmsg);
246 }
247 
248 static void
249 ifsq_ifstart_ipifunc(void *arg)
250 {
251 	struct ifaltq_subque *ifsq = arg;
252 	struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
253 
254 	crit_enter();
255 	if (lmsg->ms_flags & MSGF_DONE)
256 		lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg);
257 	crit_exit();
258 }
259 
260 static __inline void
261 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
262 {
263 	KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
264 	TAILQ_REMOVE(&head->stg_head, stage, stg_link);
265 	stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
266 	stage->stg_cnt = 0;
267 	stage->stg_len = 0;
268 }
269 
270 static __inline void
271 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
272 {
273 	KKASSERT((stage->stg_flags &
274 	    (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
275 	stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
276 	TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
277 }
278 
279 /*
280  * Schedule ifnet.if_start on the subqueue owner CPU
281  */
282 static void
283 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
284 {
285 	int cpu;
286 
287 	if (!force && curthread->td_type == TD_TYPE_NETISR &&
288 	    ifsq_stage_cntmax > 0) {
289 		struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
290 
291 		stage->stg_cnt = 0;
292 		stage->stg_len = 0;
293 		if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
294 			ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
295 		stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
296 		return;
297 	}
298 
299 	cpu = ifsq_get_cpuid(ifsq);
300 	if (cpu != mycpuid)
301 		lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
302 	else
303 		ifsq_ifstart_ipifunc(ifsq);
304 }
305 
306 /*
307  * NOTE:
308  * This function will release ifnet.if_start subqueue interlock,
309  * if ifnet.if_start for the subqueue does not need to be scheduled
310  */
311 static __inline int
312 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
313 {
314 	if (!running || ifsq_is_empty(ifsq)
315 #ifdef ALTQ
316 	    || ifsq->ifsq_altq->altq_tbr != NULL
317 #endif
318 	) {
319 		ALTQ_SQ_LOCK(ifsq);
320 		/*
321 		 * ifnet.if_start subqueue interlock is released, if:
322 		 * 1) Hardware can not take any packets, due to
323 		 *    o  interface is marked down
324 		 *    o  hardware queue is full (ifsq_is_oactive)
325 		 *    Under the second situation, hardware interrupt
326 		 *    or polling(4) will call/schedule ifnet.if_start
327 		 *    on the subqueue when hardware queue is ready
328 		 * 2) There is no packet in the subqueue.
329 		 *    Further ifq_dispatch or ifq_handoff will call/
330 		 *    schedule ifnet.if_start on the subqueue.
331 		 * 3) TBR is used and it does not allow further
332 		 *    dequeueing.
333 		 *    TBR callout will call ifnet.if_start on the
334 		 *    subqueue.
335 		 */
336 		if (!running || !ifsq_data_ready(ifsq)) {
337 			ifsq_clr_started(ifsq);
338 			ALTQ_SQ_UNLOCK(ifsq);
339 			return 0;
340 		}
341 		ALTQ_SQ_UNLOCK(ifsq);
342 	}
343 	return 1;
344 }
345 
346 static void
347 ifsq_ifstart_dispatch(netmsg_t msg)
348 {
349 	struct lwkt_msg *lmsg = &msg->base.lmsg;
350 	struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
351 	struct ifnet *ifp = ifsq_get_ifp(ifsq);
352 	struct globaldata *gd = mycpu;
353 	int running = 0, need_sched;
354 
355 	crit_enter_gd(gd);
356 
357 	lwkt_replymsg(lmsg, 0);	/* reply ASAP */
358 
359 	if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) {
360 		/*
361 		 * We need to chase the subqueue owner CPU change.
362 		 */
363 		ifsq_ifstart_schedule(ifsq, 1);
364 		crit_exit_gd(gd);
365 		return;
366 	}
367 
368 	ifsq_serialize_hw(ifsq);
369 	if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
370 		ifp->if_start(ifp, ifsq);
371 		if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
372 			running = 1;
373 	}
374 	need_sched = ifsq_ifstart_need_schedule(ifsq, running);
375 	ifsq_deserialize_hw(ifsq);
376 
377 	if (need_sched) {
378 		/*
379 		 * More data need to be transmitted, ifnet.if_start is
380 		 * scheduled on the subqueue owner CPU, and we keep going.
381 		 * NOTE: ifnet.if_start subqueue interlock is not released.
382 		 */
383 		ifsq_ifstart_schedule(ifsq, 0);
384 	}
385 
386 	crit_exit_gd(gd);
387 }
388 
389 /* Device driver ifnet.if_start helper function */
390 void
391 ifsq_devstart(struct ifaltq_subque *ifsq)
392 {
393 	struct ifnet *ifp = ifsq_get_ifp(ifsq);
394 	int running = 0;
395 
396 	ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
397 
398 	ALTQ_SQ_LOCK(ifsq);
399 	if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
400 		ALTQ_SQ_UNLOCK(ifsq);
401 		return;
402 	}
403 	ifsq_set_started(ifsq);
404 	ALTQ_SQ_UNLOCK(ifsq);
405 
406 	ifp->if_start(ifp, ifsq);
407 
408 	if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
409 		running = 1;
410 
411 	if (ifsq_ifstart_need_schedule(ifsq, running)) {
412 		/*
413 		 * More data need to be transmitted, ifnet.if_start is
414 		 * scheduled on ifnet's CPU, and we keep going.
415 		 * NOTE: ifnet.if_start interlock is not released.
416 		 */
417 		ifsq_ifstart_schedule(ifsq, 0);
418 	}
419 }
420 
421 void
422 if_devstart(struct ifnet *ifp)
423 {
424 	ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
425 }
426 
427 /* Device driver ifnet.if_start schedule helper function */
428 void
429 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
430 {
431 	ifsq_ifstart_schedule(ifsq, 1);
432 }
433 
434 void
435 if_devstart_sched(struct ifnet *ifp)
436 {
437 	ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
438 }
439 
440 static void
441 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
442 {
443 	lwkt_serialize_enter(ifp->if_serializer);
444 }
445 
446 static void
447 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
448 {
449 	lwkt_serialize_exit(ifp->if_serializer);
450 }
451 
452 static int
453 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
454 {
455 	return lwkt_serialize_try(ifp->if_serializer);
456 }
457 
458 #ifdef INVARIANTS
459 static void
460 if_default_serialize_assert(struct ifnet *ifp,
461 			    enum ifnet_serialize slz __unused,
462 			    boolean_t serialized)
463 {
464 	if (serialized)
465 		ASSERT_SERIALIZED(ifp->if_serializer);
466 	else
467 		ASSERT_NOT_SERIALIZED(ifp->if_serializer);
468 }
469 #endif
470 
471 /*
472  * Attach an interface to the list of "active" interfaces.
473  *
474  * The serializer is optional.
475  */
476 void
477 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
478 {
479 	unsigned socksize;
480 	int namelen, masklen;
481 	struct sockaddr_dl *sdl, *sdl_addr;
482 	struct ifaddr *ifa;
483 	struct ifaltq *ifq;
484 	struct ifnet **old_ifindex2ifnet = NULL;
485 	struct ifnet_array *old_ifnet_array;
486 	int i, q, qlen;
487 	char qlenname[64];
488 
489 	static int if_indexlim = 8;
490 
491 	if (ifp->if_serialize != NULL) {
492 		KASSERT(ifp->if_deserialize != NULL &&
493 			ifp->if_tryserialize != NULL &&
494 			ifp->if_serialize_assert != NULL,
495 			("serialize functions are partially setup"));
496 
497 		/*
498 		 * If the device supplies serialize functions,
499 		 * then clear if_serializer to catch any invalid
500 		 * usage of this field.
501 		 */
502 		KASSERT(serializer == NULL,
503 			("both serialize functions and default serializer "
504 			 "are supplied"));
505 		ifp->if_serializer = NULL;
506 	} else {
507 		KASSERT(ifp->if_deserialize == NULL &&
508 			ifp->if_tryserialize == NULL &&
509 			ifp->if_serialize_assert == NULL,
510 			("serialize functions are partially setup"));
511 		ifp->if_serialize = if_default_serialize;
512 		ifp->if_deserialize = if_default_deserialize;
513 		ifp->if_tryserialize = if_default_tryserialize;
514 #ifdef INVARIANTS
515 		ifp->if_serialize_assert = if_default_serialize_assert;
516 #endif
517 
518 		/*
519 		 * The serializer can be passed in from the device,
520 		 * allowing the same serializer to be used for both
521 		 * the interrupt interlock and the device queue.
522 		 * If not specified, the netif structure will use an
523 		 * embedded serializer.
524 		 */
525 		if (serializer == NULL) {
526 			serializer = &ifp->if_default_serializer;
527 			lwkt_serialize_init(serializer);
528 		}
529 		ifp->if_serializer = serializer;
530 	}
531 
532 	/*
533 	 * Make if_addrhead available on all CPUs, since they
534 	 * could be accessed by any threads.
535 	 */
536 	ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
537 				    M_IFADDR, M_WAITOK | M_ZERO);
538 	for (i = 0; i < ncpus; ++i)
539 		TAILQ_INIT(&ifp->if_addrheads[i]);
540 
541 	TAILQ_INIT(&ifp->if_multiaddrs);
542 	TAILQ_INIT(&ifp->if_groups);
543 	getmicrotime(&ifp->if_lastchange);
544 	if_addgroup(ifp, IFG_ALL);
545 
546 	/*
547 	 * create a Link Level name for this device
548 	 */
549 	namelen = strlen(ifp->if_xname);
550 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
551 	socksize = masklen + ifp->if_addrlen;
552 	if (socksize < sizeof(*sdl))
553 		socksize = sizeof(*sdl);
554 	socksize = RT_ROUNDUP(socksize);
555 	ifa = ifa_create(sizeof(struct ifaddr) + 2 * socksize);
556 	sdl = sdl_addr = (struct sockaddr_dl *)(ifa + 1);
557 	sdl->sdl_len = socksize;
558 	sdl->sdl_family = AF_LINK;
559 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
560 	sdl->sdl_nlen = namelen;
561 	sdl->sdl_type = ifp->if_type;
562 	ifp->if_lladdr = ifa;
563 	ifa->ifa_ifp = ifp;
564 	ifa->ifa_rtrequest = link_rtrequest;
565 	ifa->ifa_addr = (struct sockaddr *)sdl;
566 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
567 	ifa->ifa_netmask = (struct sockaddr *)sdl;
568 	sdl->sdl_len = masklen;
569 	while (namelen != 0)
570 		sdl->sdl_data[--namelen] = 0xff;
571 	ifa_iflink(ifa, ifp, 0 /* Insert head */);
572 
573 	/*
574 	 * Make if_data available on all CPUs, since they could
575 	 * be updated by hardware interrupt routing, which could
576 	 * be bound to any CPU.
577 	 */
578 	ifp->if_data_pcpu = kmalloc(ncpus * sizeof(struct ifdata_pcpu),
579 				    M_DEVBUF,
580 				    M_WAITOK | M_ZERO | M_CACHEALIGN);
581 
582 	if (ifp->if_mapsubq == NULL)
583 		ifp->if_mapsubq = ifq_mapsubq_default;
584 
585 	ifq = &ifp->if_snd;
586 	ifq->altq_type = 0;
587 	ifq->altq_disc = NULL;
588 	ifq->altq_flags &= ALTQF_CANTCHANGE;
589 	ifq->altq_tbr = NULL;
590 	ifq->altq_ifp = ifp;
591 
592 	if (ifq->altq_subq_cnt <= 0)
593 		ifq->altq_subq_cnt = 1;
594 	ifq->altq_subq =
595 		kmalloc(ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
596 			M_DEVBUF,
597 			M_WAITOK | M_ZERO | M_CACHEALIGN);
598 
599 	if (ifq->altq_maxlen == 0) {
600 		if_printf(ifp, "driver didn't set altq_maxlen\n");
601 		ifq_set_maxlen(ifq, ifqmaxlen);
602 	}
603 
604 	/* Allow user to override driver's setting. */
605 	ksnprintf(qlenname, sizeof(qlenname), "net.%s.qlenmax", ifp->if_xname);
606 	qlen = -1;
607 	TUNABLE_INT_FETCH(qlenname, &qlen);
608 	if (qlen > 0) {
609 		if_printf(ifp, "qlenmax -> %d\n", qlen);
610 		ifq_set_maxlen(ifq, qlen);
611 	}
612 
613 	for (q = 0; q < ifq->altq_subq_cnt; ++q) {
614 		struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
615 
616 		ALTQ_SQ_LOCK_INIT(ifsq);
617 		ifsq->ifsq_index = q;
618 
619 		ifsq->ifsq_altq = ifq;
620 		ifsq->ifsq_ifp = ifp;
621 
622 		ifsq->ifsq_maxlen = ifq->altq_maxlen;
623 		ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
624 		ifsq->ifsq_prepended = NULL;
625 		ifsq->ifsq_started = 0;
626 		ifsq->ifsq_hw_oactive = 0;
627 		ifsq_set_cpuid(ifsq, 0);
628 		if (ifp->if_serializer != NULL)
629 			ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
630 
631 		/* XXX: netisr_ncpus */
632 		ifsq->ifsq_stage =
633 			kmalloc(ncpus * sizeof(struct ifsubq_stage),
634 				M_DEVBUF,
635 				M_WAITOK | M_ZERO | M_CACHEALIGN);
636 		for (i = 0; i < ncpus; ++i)
637 			ifsq->ifsq_stage[i].stg_subq = ifsq;
638 
639 		/*
640 		 * Allocate one if_start message for each CPU, since
641 		 * the hardware TX ring could be assigned to any CPU.
642 		 *
643 		 * NOTE:
644 		 * If the hardware TX ring polling CPU and the hardware
645 		 * TX ring interrupt CPU are same, one if_start message
646 		 * should be enough.
647 		 */
648 		ifsq->ifsq_ifstart_nmsg =
649 		    kmalloc(ncpus * sizeof(struct netmsg_base),
650 		    M_LWKTMSG, M_WAITOK);
651 		for (i = 0; i < ncpus; ++i) {
652 			netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
653 			    &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
654 			ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
655 		}
656 	}
657 	ifq_set_classic(ifq);
658 
659 	/*
660 	 * Increase mbuf cluster/jcluster limits for the mbufs that
661 	 * could sit on the device queues for quite some time.
662 	 */
663 	if (ifp->if_nmbclusters > 0)
664 		mcl_inclimit(ifp->if_nmbclusters);
665 	if (ifp->if_nmbjclusters > 0)
666 		mjcl_inclimit(ifp->if_nmbjclusters);
667 
668 	/*
669 	 * Install this ifp into ifindex2inet, ifnet queue and ifnet
670 	 * array after it is setup.
671 	 *
672 	 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
673 	 * by ifnet lock, so that non-netisr threads could get a
674 	 * consistent view.
675 	 */
676 	ifnet_lock();
677 
678 	/* Don't update if_index until ifindex2ifnet is setup */
679 	ifp->if_index = if_index + 1;
680 	sdl_addr->sdl_index = ifp->if_index;
681 
682 	/*
683 	 * Install this ifp into ifindex2ifnet
684 	 */
685 	if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
686 		unsigned int n;
687 		struct ifnet **q;
688 
689 		/*
690 		 * Grow ifindex2ifnet
691 		 */
692 		if_indexlim <<= 1;
693 		n = if_indexlim * sizeof(*q);
694 		q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
695 		if (ifindex2ifnet != NULL) {
696 			bcopy(ifindex2ifnet, q, n/2);
697 			/* Free old ifindex2ifnet after sync all netisrs */
698 			old_ifindex2ifnet = ifindex2ifnet;
699 		}
700 		ifindex2ifnet = q;
701 	}
702 	ifindex2ifnet[ifp->if_index] = ifp;
703 	/*
704 	 * Update if_index after this ifp is installed into ifindex2ifnet,
705 	 * so that netisrs could get a consistent view of ifindex2ifnet.
706 	 */
707 	cpu_sfence();
708 	if_index = ifp->if_index;
709 
710 	/*
711 	 * Install this ifp into ifnet array.
712 	 */
713 	/* Free old ifnet array after sync all netisrs */
714 	old_ifnet_array = ifnet_array;
715 	ifnet_array = ifnet_array_add(ifp, old_ifnet_array);
716 
717 	/*
718 	 * Install this ifp into ifnet queue.
719 	 */
720 	TAILQ_INSERT_TAIL(&ifnetlist, ifp, if_link);
721 
722 	ifnet_unlock();
723 
724 	/*
725 	 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
726 	 * are no longer accessed and we can free them safely later on.
727 	 */
728 	netmsg_service_sync();
729 	if (old_ifindex2ifnet != NULL)
730 		kfree(old_ifindex2ifnet, M_IFADDR);
731 	ifnet_array_free(old_ifnet_array);
732 
733 	if (!SLIST_EMPTY(&domains))
734 		if_attachdomain1(ifp);
735 
736 	/* Announce the interface. */
737 	EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
738 	devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
739 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
740 }
741 
742 static void
743 if_attachdomain(void *dummy)
744 {
745 	struct ifnet *ifp;
746 
747 	ifnet_lock();
748 	TAILQ_FOREACH(ifp, &ifnetlist, if_list)
749 		if_attachdomain1(ifp);
750 	ifnet_unlock();
751 }
752 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
753 	if_attachdomain, NULL);
754 
755 static void
756 if_attachdomain1(struct ifnet *ifp)
757 {
758 	struct domain *dp;
759 
760 	crit_enter();
761 
762 	/* address family dependent data region */
763 	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
764 	SLIST_FOREACH(dp, &domains, dom_next)
765 		if (dp->dom_ifattach)
766 			ifp->if_afdata[dp->dom_family] =
767 				(*dp->dom_ifattach)(ifp);
768 	crit_exit();
769 }
770 
771 /*
772  * Purge all addresses whose type is _not_ AF_LINK
773  */
774 static void
775 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg)
776 {
777 	struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
778 	struct ifaddr_container *ifac, *next;
779 
780 	ASSERT_NETISR0;
781 
782 	/*
783 	 * The ifaddr processing in the following loop will block,
784 	 * however, this function is called in netisr0, in which
785 	 * ifaddr list changes happen, so we don't care about the
786 	 * blockness of the ifaddr processing here.
787 	 */
788 	TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
789 			      ifa_link, next) {
790 		struct ifaddr *ifa = ifac->ifa;
791 
792 		/* Ignore marker */
793 		if (ifa->ifa_addr->sa_family == AF_UNSPEC)
794 			continue;
795 
796 		/* Leave link ifaddr as it is */
797 		if (ifa->ifa_addr->sa_family == AF_LINK)
798 			continue;
799 #ifdef INET
800 		/* XXX: Ugly!! ad hoc just for INET */
801 		if (ifa->ifa_addr->sa_family == AF_INET) {
802 			struct ifaliasreq ifr;
803 			struct sockaddr_in saved_addr, saved_dst;
804 #ifdef IFADDR_DEBUG_VERBOSE
805 			int i;
806 
807 			kprintf("purge in4 addr %p: ", ifa);
808 			for (i = 0; i < ncpus; ++i) {
809 				kprintf("%d ",
810 				    ifa->ifa_containers[i].ifa_refcnt);
811 			}
812 			kprintf("\n");
813 #endif
814 
815 			/* Save information for panic. */
816 			memcpy(&saved_addr, ifa->ifa_addr, sizeof(saved_addr));
817 			if (ifa->ifa_dstaddr != NULL) {
818 				memcpy(&saved_dst, ifa->ifa_dstaddr,
819 				    sizeof(saved_dst));
820 			} else {
821 				memset(&saved_dst, 0, sizeof(saved_dst));
822 			}
823 
824 			bzero(&ifr, sizeof ifr);
825 			ifr.ifra_addr = *ifa->ifa_addr;
826 			if (ifa->ifa_dstaddr)
827 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
828 			if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp,
829 				       NULL) == 0)
830 				continue;
831 
832 			/* MUST NOT HAPPEN */
833 			panic("%s: in_control failed %x, dst %x", ifp->if_xname,
834 			    ntohl(saved_addr.sin_addr.s_addr),
835 			    ntohl(saved_dst.sin_addr.s_addr));
836 		}
837 #endif /* INET */
838 #ifdef INET6
839 		if (ifa->ifa_addr->sa_family == AF_INET6) {
840 #ifdef IFADDR_DEBUG_VERBOSE
841 			int i;
842 
843 			kprintf("purge in6 addr %p: ", ifa);
844 			for (i = 0; i < ncpus; ++i) {
845 				kprintf("%d ",
846 				    ifa->ifa_containers[i].ifa_refcnt);
847 			}
848 			kprintf("\n");
849 #endif
850 
851 			in6_purgeaddr(ifa);
852 			/* ifp_addrhead is already updated */
853 			continue;
854 		}
855 #endif /* INET6 */
856 		if_printf(ifp, "destroy ifaddr family %d\n",
857 		    ifa->ifa_addr->sa_family);
858 		ifa_ifunlink(ifa, ifp);
859 		ifa_destroy(ifa);
860 	}
861 
862 	netisr_replymsg(&nmsg->base, 0);
863 }
864 
865 void
866 if_purgeaddrs_nolink(struct ifnet *ifp)
867 {
868 	struct netmsg_base nmsg;
869 
870 	netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
871 	    if_purgeaddrs_nolink_dispatch);
872 	nmsg.lmsg.u.ms_resultp = ifp;
873 	netisr_domsg(&nmsg, 0);
874 }
875 
876 static void
877 ifq_stage_detach_handler(netmsg_t nmsg)
878 {
879 	struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
880 	int q;
881 
882 	for (q = 0; q < ifq->altq_subq_cnt; ++q) {
883 		struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
884 		struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
885 
886 		if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
887 			ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
888 	}
889 	lwkt_replymsg(&nmsg->lmsg, 0);
890 }
891 
892 static void
893 ifq_stage_detach(struct ifaltq *ifq)
894 {
895 	struct netmsg_base base;
896 	int cpu;
897 
898 	netmsg_init(&base, NULL, &curthread->td_msgport, 0,
899 	    ifq_stage_detach_handler);
900 	base.lmsg.u.ms_resultp = ifq;
901 
902 	/* XXX netisr_ncpus */
903 	for (cpu = 0; cpu < ncpus; ++cpu)
904 		lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
905 }
906 
907 struct netmsg_if_rtdel {
908 	struct netmsg_base	base;
909 	struct ifnet		*ifp;
910 };
911 
912 static void
913 if_rtdel_dispatch(netmsg_t msg)
914 {
915 	struct netmsg_if_rtdel *rmsg = (void *)msg;
916 	int i, cpu;
917 
918 	cpu = mycpuid;
919 	ASSERT_NETISR_NCPUS(cpu);
920 
921 	for (i = 1; i <= AF_MAX; i++) {
922 		struct radix_node_head	*rnh;
923 
924 		if ((rnh = rt_tables[cpu][i]) == NULL)
925 			continue;
926 		rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
927 	}
928 	netisr_forwardmsg(&msg->base, cpu + 1);
929 }
930 
931 /*
932  * Detach an interface, removing it from the
933  * list of "active" interfaces.
934  */
935 void
936 if_detach(struct ifnet *ifp)
937 {
938 	struct ifnet_array *old_ifnet_array;
939 	struct ifg_list *ifgl;
940 	struct netmsg_if_rtdel msg;
941 	struct domain *dp;
942 	int q;
943 
944 	/* Announce that the interface is gone. */
945 	EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
946 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
947 	devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
948 
949 	/*
950 	 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
951 	 * array before it is whacked.
952 	 *
953 	 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
954 	 * by ifnet lock, so that non-netisr threads could get a
955 	 * consistent view.
956 	 */
957 	ifnet_lock();
958 
959 	/*
960 	 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
961 	 */
962 	ifindex2ifnet[ifp->if_index] = NULL;
963 	while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
964 		if_index--;
965 
966 	/*
967 	 * Remove this ifp from ifnet queue.
968 	 */
969 	TAILQ_REMOVE(&ifnetlist, ifp, if_link);
970 
971 	/*
972 	 * Remove this ifp from ifnet array.
973 	 */
974 	/* Free old ifnet array after sync all netisrs */
975 	old_ifnet_array = ifnet_array;
976 	ifnet_array = ifnet_array_del(ifp, old_ifnet_array);
977 
978 	ifnet_unlock();
979 
980 	ifgroup_lockmgr(LK_EXCLUSIVE);
981 	while ((ifgl = TAILQ_FIRST(&ifp->if_groups)) != NULL)
982 		if_delgroup_locked(ifp, ifgl->ifgl_group->ifg_group);
983 	ifgroup_lockmgr(LK_RELEASE);
984 
985 	/*
986 	 * Sync all netisrs so that the old ifnet array is no longer
987 	 * accessed and we can free it safely later on.
988 	 */
989 	netmsg_service_sync();
990 	ifnet_array_free(old_ifnet_array);
991 
992 	/*
993 	 * Remove routes and flush queues.
994 	 */
995 	crit_enter();
996 #ifdef IFPOLL_ENABLE
997 	if (ifp->if_flags & IFF_NPOLLING)
998 		ifpoll_deregister(ifp);
999 #endif
1000 	if_down(ifp);
1001 
1002 	/* Decrease the mbuf clusters/jclusters limits increased by us */
1003 	if (ifp->if_nmbclusters > 0)
1004 		mcl_inclimit(-ifp->if_nmbclusters);
1005 	if (ifp->if_nmbjclusters > 0)
1006 		mjcl_inclimit(-ifp->if_nmbjclusters);
1007 
1008 #ifdef ALTQ
1009 	if (ifq_is_enabled(&ifp->if_snd))
1010 		altq_disable(&ifp->if_snd);
1011 	if (ifq_is_attached(&ifp->if_snd))
1012 		altq_detach(&ifp->if_snd);
1013 #endif
1014 
1015 	/*
1016 	 * Clean up all addresses.
1017 	 */
1018 	ifp->if_lladdr = NULL;
1019 
1020 	if_purgeaddrs_nolink(ifp);
1021 	if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
1022 		struct ifaddr *ifa;
1023 
1024 		ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1025 		KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
1026 			("non-link ifaddr is left on if_addrheads"));
1027 
1028 		ifa_ifunlink(ifa, ifp);
1029 		ifa_destroy(ifa);
1030 		KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
1031 			("there are still ifaddrs left on if_addrheads"));
1032 	}
1033 
1034 #ifdef INET
1035 	/*
1036 	 * Remove all IPv4 kernel structures related to ifp.
1037 	 */
1038 	in_ifdetach(ifp);
1039 #endif
1040 
1041 #ifdef INET6
1042 	/*
1043 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
1044 	 * before removing routing entries below, since IPv6 interface direct
1045 	 * routes are expected to be removed by the IPv6-specific kernel API.
1046 	 * Otherwise, the kernel will detect some inconsistency and bark it.
1047 	 */
1048 	in6_ifdetach(ifp);
1049 #endif
1050 
1051 	/*
1052 	 * Delete all remaining routes using this interface
1053 	 */
1054 	netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
1055 	    if_rtdel_dispatch);
1056 	msg.ifp = ifp;
1057 	netisr_domsg_global(&msg.base);
1058 
1059 	SLIST_FOREACH(dp, &domains, dom_next) {
1060 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1061 			(*dp->dom_ifdetach)(ifp,
1062 				ifp->if_afdata[dp->dom_family]);
1063 	}
1064 
1065 	kfree(ifp->if_addrheads, M_IFADDR);
1066 
1067 	lwkt_synchronize_ipiqs("if_detach");
1068 	ifq_stage_detach(&ifp->if_snd);
1069 
1070 	for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
1071 		struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
1072 
1073 		kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
1074 		kfree(ifsq->ifsq_stage, M_DEVBUF);
1075 	}
1076 	kfree(ifp->if_snd.altq_subq, M_DEVBUF);
1077 
1078 	kfree(ifp->if_data_pcpu, M_DEVBUF);
1079 
1080 	crit_exit();
1081 }
1082 
1083 int
1084 ifgroup_lockmgr(u_int flags)
1085 {
1086 	return lockmgr(&ifgroup_lock, flags);
1087 }
1088 
1089 /*
1090  * Create an empty interface group.
1091  */
1092 static struct ifg_group *
1093 if_creategroup(const char *groupname)
1094 {
1095 	struct ifg_group *ifg;
1096 
1097 	ifg = kmalloc(sizeof(*ifg), M_IFNET, M_WAITOK);
1098 	strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1099 	ifg->ifg_refcnt = 0;
1100 	ifg->ifg_carp_demoted = 0;
1101 	TAILQ_INIT(&ifg->ifg_members);
1102 
1103 	ifgroup_lockmgr(LK_EXCLUSIVE);
1104 	TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
1105 	ifgroup_lockmgr(LK_RELEASE);
1106 
1107 	EVENTHANDLER_INVOKE(group_attach_event, ifg);
1108 
1109 	return (ifg);
1110 }
1111 
1112 /*
1113  * Destroy an empty interface group.
1114  */
1115 static int
1116 if_destroygroup(struct ifg_group *ifg)
1117 {
1118 	KASSERT(ifg->ifg_refcnt == 0,
1119 		("trying to delete a non-empty interface group"));
1120 
1121 	ifgroup_lockmgr(LK_EXCLUSIVE);
1122 	TAILQ_REMOVE(&ifg_head, ifg, ifg_next);
1123 	ifgroup_lockmgr(LK_RELEASE);
1124 
1125 	EVENTHANDLER_INVOKE(group_detach_event, ifg);
1126 	kfree(ifg, M_IFNET);
1127 
1128 	return (0);
1129 }
1130 
1131 /*
1132  * Add the interface to a group.
1133  * The target group will be created if it doesn't exist.
1134  */
1135 int
1136 if_addgroup(struct ifnet *ifp, const char *groupname)
1137 {
1138 	struct ifg_list *ifgl;
1139 	struct ifg_group *ifg;
1140 	struct ifg_member *ifgm;
1141 
1142 	if (groupname[0] &&
1143 	    groupname[strlen(groupname) - 1] >= '0' &&
1144 	    groupname[strlen(groupname) - 1] <= '9')
1145 		return (EINVAL);
1146 
1147 	ifgroup_lockmgr(LK_SHARED);
1148 
1149 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1150 		if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0) {
1151 			ifgroup_lockmgr(LK_RELEASE);
1152 			return (EEXIST);
1153 		}
1154 	}
1155 
1156 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next) {
1157 		if (strcmp(ifg->ifg_group, groupname) == 0)
1158 			break;
1159 	}
1160 
1161 	ifgroup_lockmgr(LK_RELEASE);
1162 
1163 	if (ifg == NULL)
1164 		ifg = if_creategroup(groupname);
1165 
1166 	ifgl = kmalloc(sizeof(*ifgl), M_IFNET, M_WAITOK);
1167 	ifgm = kmalloc(sizeof(*ifgm), M_IFNET, M_WAITOK);
1168 	ifgl->ifgl_group = ifg;
1169 	ifgm->ifgm_ifp = ifp;
1170 	ifg->ifg_refcnt++;
1171 
1172 	ifgroup_lockmgr(LK_EXCLUSIVE);
1173 	TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1174 	TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1175 	ifgroup_lockmgr(LK_RELEASE);
1176 
1177 	EVENTHANDLER_INVOKE(group_change_event, groupname);
1178 
1179 	return (0);
1180 }
1181 
1182 /*
1183  * Remove the interface from a group.
1184  * The group will be destroyed if it becomes empty.
1185  *
1186  * The 'ifgroup_lock' must be hold exclusively when calling this.
1187  */
1188 static int
1189 if_delgroup_locked(struct ifnet *ifp, const char *groupname)
1190 {
1191 	struct ifg_list *ifgl;
1192 	struct ifg_member *ifgm;
1193 
1194 	KKASSERT(lockstatus(&ifgroup_lock, curthread) == LK_EXCLUSIVE);
1195 
1196 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1197 		if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1198 			break;
1199 	}
1200 	if (ifgl == NULL)
1201 		return (ENOENT);
1202 
1203 	TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1204 
1205 	TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1206 		if (ifgm->ifgm_ifp == ifp)
1207 			break;
1208 	}
1209 
1210 	if (ifgm != NULL) {
1211 		TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1212 
1213 		ifgroup_lockmgr(LK_RELEASE);
1214 		EVENTHANDLER_INVOKE(group_change_event, groupname);
1215 		ifgroup_lockmgr(LK_EXCLUSIVE);
1216 
1217 		kfree(ifgm, M_IFNET);
1218 		ifgl->ifgl_group->ifg_refcnt--;
1219 	}
1220 
1221 	if (ifgl->ifgl_group->ifg_refcnt == 0) {
1222 		ifgroup_lockmgr(LK_RELEASE);
1223 		if_destroygroup(ifgl->ifgl_group);
1224 		ifgroup_lockmgr(LK_EXCLUSIVE);
1225 	}
1226 
1227 	kfree(ifgl, M_IFNET);
1228 
1229 	return (0);
1230 }
1231 
1232 int
1233 if_delgroup(struct ifnet *ifp, const char *groupname)
1234 {
1235 	int error;
1236 
1237 	ifgroup_lockmgr(LK_EXCLUSIVE);
1238 	error = if_delgroup_locked(ifp, groupname);
1239 	ifgroup_lockmgr(LK_RELEASE);
1240 
1241 	return (error);
1242 }
1243 
1244 /*
1245  * Store all the groups that the interface belongs to in memory
1246  * pointed to by data.
1247  */
1248 static int
1249 if_getgroups(struct ifgroupreq *ifgr, struct ifnet *ifp)
1250 {
1251 	struct ifg_list *ifgl;
1252 	struct ifg_req *ifgrq, *p;
1253 	int len, error;
1254 
1255 	len = 0;
1256 	ifgroup_lockmgr(LK_SHARED);
1257 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1258 		len += sizeof(struct ifg_req);
1259 	ifgroup_lockmgr(LK_RELEASE);
1260 
1261 	if (ifgr->ifgr_len == 0) {
1262 		/*
1263 		 * Caller is asking how much memory should be allocated in
1264 		 * the next request in order to hold all the groups.
1265 		 */
1266 		ifgr->ifgr_len = len;
1267 		return (0);
1268 	} else if (ifgr->ifgr_len != len) {
1269 		return (EINVAL);
1270 	}
1271 
1272 	ifgrq = kmalloc(len, M_TEMP, M_INTWAIT | M_NULLOK | M_ZERO);
1273 	if (ifgrq == NULL)
1274 		return (ENOMEM);
1275 
1276 	ifgroup_lockmgr(LK_SHARED);
1277 	p = ifgrq;
1278 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1279 		if (len < sizeof(struct ifg_req)) {
1280 			ifgroup_lockmgr(LK_RELEASE);
1281 			error = EINVAL;
1282 			goto failed;
1283 		}
1284 
1285 		strlcpy(p->ifgrq_group, ifgl->ifgl_group->ifg_group,
1286 			sizeof(ifgrq->ifgrq_group));
1287 		len -= sizeof(struct ifg_req);
1288 		p++;
1289 	}
1290 	ifgroup_lockmgr(LK_RELEASE);
1291 
1292 	error = copyout(ifgrq, ifgr->ifgr_groups, ifgr->ifgr_len);
1293 failed:
1294 	kfree(ifgrq, M_TEMP);
1295 	return error;
1296 }
1297 
1298 /*
1299  * Store all the members of a group in memory pointed to by data.
1300  */
1301 static int
1302 if_getgroupmembers(struct ifgroupreq *ifgr)
1303 {
1304 	struct ifg_group *ifg;
1305 	struct ifg_member *ifgm;
1306 	struct ifg_req *ifgrq, *p;
1307 	int len, error;
1308 
1309 	ifgroup_lockmgr(LK_SHARED);
1310 
1311 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next) {
1312 		if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1313 			break;
1314 	}
1315 	if (ifg == NULL) {
1316 		ifgroup_lockmgr(LK_RELEASE);
1317 		return (ENOENT);
1318 	}
1319 
1320 	len = 0;
1321 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1322 		len += sizeof(struct ifg_req);
1323 
1324 	ifgroup_lockmgr(LK_RELEASE);
1325 
1326 	if (ifgr->ifgr_len == 0) {
1327 		ifgr->ifgr_len = len;
1328 		return (0);
1329 	} else if (ifgr->ifgr_len != len) {
1330 		return (EINVAL);
1331 	}
1332 
1333 	ifgrq = kmalloc(len, M_TEMP, M_INTWAIT | M_NULLOK | M_ZERO);
1334 	if (ifgrq == NULL)
1335 		return (ENOMEM);
1336 
1337 	ifgroup_lockmgr(LK_SHARED);
1338 	p = ifgrq;
1339 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1340 		if (len < sizeof(struct ifg_req)) {
1341 			ifgroup_lockmgr(LK_RELEASE);
1342 			error = EINVAL;
1343 			goto failed;
1344 		}
1345 
1346 		strlcpy(p->ifgrq_member, ifgm->ifgm_ifp->if_xname,
1347 			sizeof(p->ifgrq_member));
1348 		len -= sizeof(struct ifg_req);
1349 		p++;
1350 	}
1351 	ifgroup_lockmgr(LK_RELEASE);
1352 
1353 	error = copyout(ifgrq, ifgr->ifgr_groups, ifgr->ifgr_len);
1354 failed:
1355 	kfree(ifgrq, M_TEMP);
1356 	return error;
1357 }
1358 
1359 /*
1360  * Delete Routes for a Network Interface
1361  *
1362  * Called for each routing entry via the rnh->rnh_walktree() call above
1363  * to delete all route entries referencing a detaching network interface.
1364  *
1365  * Arguments:
1366  *	rn	pointer to node in the routing table
1367  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
1368  *
1369  * Returns:
1370  *	0	successful
1371  *	errno	failed - reason indicated
1372  *
1373  */
1374 static int
1375 if_rtdel(struct radix_node *rn, void *arg)
1376 {
1377 	struct rtentry	*rt = (struct rtentry *)rn;
1378 	struct ifnet	*ifp = arg;
1379 	int		err;
1380 
1381 	if (rt->rt_ifp == ifp) {
1382 
1383 		/*
1384 		 * Protect (sorta) against walktree recursion problems
1385 		 * with cloned routes
1386 		 */
1387 		if (!(rt->rt_flags & RTF_UP))
1388 			return (0);
1389 
1390 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1391 				rt_mask(rt), rt->rt_flags,
1392 				NULL);
1393 		if (err) {
1394 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
1395 		}
1396 	}
1397 
1398 	return (0);
1399 }
1400 
1401 static __inline boolean_t
1402 ifa_prefer(const struct ifaddr *cur_ifa, const struct ifaddr *old_ifa)
1403 {
1404 	if (old_ifa == NULL)
1405 		return TRUE;
1406 
1407 	if ((old_ifa->ifa_ifp->if_flags & IFF_UP) == 0 &&
1408 	    (cur_ifa->ifa_ifp->if_flags & IFF_UP))
1409 		return TRUE;
1410 	if ((old_ifa->ifa_flags & IFA_ROUTE) == 0 &&
1411 	    (cur_ifa->ifa_flags & IFA_ROUTE))
1412 		return TRUE;
1413 	return FALSE;
1414 }
1415 
1416 /*
1417  * Locate an interface based on a complete address.
1418  */
1419 struct ifaddr *
1420 ifa_ifwithaddr(struct sockaddr *addr)
1421 {
1422 	const struct ifnet_array *arr;
1423 	int i;
1424 
1425 	arr = ifnet_array_get();
1426 	for (i = 0; i < arr->ifnet_count; ++i) {
1427 		struct ifnet *ifp = arr->ifnet_arr[i];
1428 		struct ifaddr_container *ifac;
1429 
1430 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1431 			struct ifaddr *ifa = ifac->ifa;
1432 
1433 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1434 				continue;
1435 			if (sa_equal(addr, ifa->ifa_addr))
1436 				return (ifa);
1437 			if ((ifp->if_flags & IFF_BROADCAST) &&
1438 			    ifa->ifa_broadaddr &&
1439 			    /* IPv6 doesn't have broadcast */
1440 			    ifa->ifa_broadaddr->sa_len != 0 &&
1441 			    sa_equal(ifa->ifa_broadaddr, addr))
1442 				return (ifa);
1443 		}
1444 	}
1445 	return (NULL);
1446 }
1447 
1448 /*
1449  * Locate the point to point interface with a given destination address.
1450  */
1451 struct ifaddr *
1452 ifa_ifwithdstaddr(struct sockaddr *addr)
1453 {
1454 	const struct ifnet_array *arr;
1455 	int i;
1456 
1457 	arr = ifnet_array_get();
1458 	for (i = 0; i < arr->ifnet_count; ++i) {
1459 		struct ifnet *ifp = arr->ifnet_arr[i];
1460 		struct ifaddr_container *ifac;
1461 
1462 		if (!(ifp->if_flags & IFF_POINTOPOINT))
1463 			continue;
1464 
1465 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1466 			struct ifaddr *ifa = ifac->ifa;
1467 
1468 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1469 				continue;
1470 			if (ifa->ifa_dstaddr &&
1471 			    sa_equal(addr, ifa->ifa_dstaddr))
1472 				return (ifa);
1473 		}
1474 	}
1475 	return (NULL);
1476 }
1477 
1478 /*
1479  * Find an interface on a specific network.  If many, choice
1480  * is most specific found.
1481  */
1482 struct ifaddr *
1483 ifa_ifwithnet(struct sockaddr *addr)
1484 {
1485 	struct ifaddr *ifa_maybe = NULL;
1486 	u_int af = addr->sa_family;
1487 	char *addr_data = addr->sa_data, *cplim;
1488 	const struct ifnet_array *arr;
1489 	int i;
1490 
1491 	/*
1492 	 * AF_LINK addresses can be looked up directly by their index number,
1493 	 * so do that if we can.
1494 	 */
1495 	if (af == AF_LINK) {
1496 		struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1497 
1498 		if (sdl->sdl_index && sdl->sdl_index <= if_index)
1499 			return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1500 	}
1501 
1502 	/*
1503 	 * Scan though each interface, looking for ones that have
1504 	 * addresses in this address family.
1505 	 */
1506 	arr = ifnet_array_get();
1507 	for (i = 0; i < arr->ifnet_count; ++i) {
1508 		struct ifnet *ifp = arr->ifnet_arr[i];
1509 		struct ifaddr_container *ifac;
1510 
1511 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1512 			struct ifaddr *ifa = ifac->ifa;
1513 			char *cp, *cp2, *cp3;
1514 
1515 			if (ifa->ifa_addr->sa_family != af)
1516 next:				continue;
1517 			if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1518 				/*
1519 				 * This is a bit broken as it doesn't
1520 				 * take into account that the remote end may
1521 				 * be a single node in the network we are
1522 				 * looking for.
1523 				 * The trouble is that we don't know the
1524 				 * netmask for the remote end.
1525 				 */
1526 				if (ifa->ifa_dstaddr != NULL &&
1527 				    sa_equal(addr, ifa->ifa_dstaddr))
1528 					return (ifa);
1529 			} else {
1530 				/*
1531 				 * if we have a special address handler,
1532 				 * then use it instead of the generic one.
1533 				 */
1534 				if (ifa->ifa_claim_addr) {
1535 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1536 						return (ifa);
1537 					} else {
1538 						continue;
1539 					}
1540 				}
1541 
1542 				/*
1543 				 * Scan all the bits in the ifa's address.
1544 				 * If a bit dissagrees with what we are
1545 				 * looking for, mask it with the netmask
1546 				 * to see if it really matters.
1547 				 * (A byte at a time)
1548 				 */
1549 				if (ifa->ifa_netmask == 0)
1550 					continue;
1551 				cp = addr_data;
1552 				cp2 = ifa->ifa_addr->sa_data;
1553 				cp3 = ifa->ifa_netmask->sa_data;
1554 				cplim = ifa->ifa_netmask->sa_len +
1555 					(char *)ifa->ifa_netmask;
1556 				while (cp3 < cplim)
1557 					if ((*cp++ ^ *cp2++) & *cp3++)
1558 						goto next; /* next address! */
1559 				/*
1560 				 * If the netmask of what we just found
1561 				 * is more specific than what we had before
1562 				 * (if we had one) then remember the new one
1563 				 * before continuing to search for an even
1564 				 * better one.  If the netmasks are equal,
1565 				 * we prefer the this ifa based on the result
1566 				 * of ifa_prefer().
1567 				 */
1568 				if (ifa_maybe == NULL ||
1569 				    rn_refines(ifa->ifa_netmask,
1570 					       ifa_maybe->ifa_netmask) ||
1571 				    (sa_equal(ifa_maybe->ifa_netmask,
1572 					      ifa->ifa_netmask) &&
1573 				     ifa_prefer(ifa, ifa_maybe)))
1574 					ifa_maybe = ifa;
1575 			}
1576 		}
1577 	}
1578 	return (ifa_maybe);
1579 }
1580 
1581 /*
1582  * Find an interface address specific to an interface best matching
1583  * a given address.
1584  */
1585 struct ifaddr *
1586 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1587 {
1588 	struct ifaddr_container *ifac;
1589 	char *cp, *cp2, *cp3;
1590 	char *cplim;
1591 	struct ifaddr *ifa_maybe = NULL;
1592 	u_int af = addr->sa_family;
1593 
1594 	if (af >= AF_MAX)
1595 		return (0);
1596 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1597 		struct ifaddr *ifa = ifac->ifa;
1598 
1599 		if (ifa->ifa_addr->sa_family != af)
1600 			continue;
1601 		if (ifa_maybe == NULL)
1602 			ifa_maybe = ifa;
1603 		if (ifa->ifa_netmask == NULL) {
1604 			if (sa_equal(addr, ifa->ifa_addr) ||
1605 			    (ifa->ifa_dstaddr != NULL &&
1606 			     sa_equal(addr, ifa->ifa_dstaddr)))
1607 				return (ifa);
1608 			continue;
1609 		}
1610 		if (ifp->if_flags & IFF_POINTOPOINT) {
1611 			if (sa_equal(addr, ifa->ifa_dstaddr))
1612 				return (ifa);
1613 		} else {
1614 			cp = addr->sa_data;
1615 			cp2 = ifa->ifa_addr->sa_data;
1616 			cp3 = ifa->ifa_netmask->sa_data;
1617 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1618 			for (; cp3 < cplim; cp3++)
1619 				if ((*cp++ ^ *cp2++) & *cp3)
1620 					break;
1621 			if (cp3 == cplim)
1622 				return (ifa);
1623 		}
1624 	}
1625 	return (ifa_maybe);
1626 }
1627 
1628 /*
1629  * Default action when installing a route with a Link Level gateway.
1630  * Lookup an appropriate real ifa to point to.
1631  * This should be moved to /sys/net/link.c eventually.
1632  */
1633 static void
1634 link_rtrequest(int cmd, struct rtentry *rt)
1635 {
1636 	struct ifaddr *ifa;
1637 	struct sockaddr *dst;
1638 	struct ifnet *ifp;
1639 
1640 	if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1641 	    (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1642 		return;
1643 	ifa = ifaof_ifpforaddr(dst, ifp);
1644 	if (ifa != NULL) {
1645 		IFAFREE(rt->rt_ifa);
1646 		IFAREF(ifa);
1647 		rt->rt_ifa = ifa;
1648 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1649 			ifa->ifa_rtrequest(cmd, rt);
1650 	}
1651 }
1652 
1653 struct netmsg_if {
1654 	struct netmsg_base	base;
1655 	struct ifnet		*ifp;
1656 };
1657 
1658 /*
1659  * Mark an interface down and notify protocols of the transition.
1660  */
1661 static void
1662 if_down_dispatch(netmsg_t nmsg)
1663 {
1664 	struct netmsg_if *msg = (struct netmsg_if *)nmsg;
1665 	struct ifnet *ifp = msg->ifp;
1666 	struct ifaddr_container *ifac;
1667 	struct domain *dp;
1668 
1669 	ASSERT_NETISR0;
1670 
1671 	ifp->if_flags &= ~IFF_UP;
1672 	getmicrotime(&ifp->if_lastchange);
1673 	rt_ifmsg(ifp);
1674 
1675 	/*
1676 	 * The ifaddr processing in the following loop will block,
1677 	 * however, this function is called in netisr0, in which
1678 	 * ifaddr list changes happen, so we don't care about the
1679 	 * blockness of the ifaddr processing here.
1680 	 */
1681 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1682 		struct ifaddr *ifa = ifac->ifa;
1683 
1684 		/* Ignore marker */
1685 		if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1686 			continue;
1687 
1688 		kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1689 	}
1690 
1691 	SLIST_FOREACH(dp, &domains, dom_next)
1692 		if (dp->dom_if_down != NULL)
1693 			dp->dom_if_down(ifp);
1694 
1695 	ifq_purge_all(&ifp->if_snd);
1696 	netisr_replymsg(&nmsg->base, 0);
1697 }
1698 
1699 /*
1700  * Mark an interface up and notify protocols of the transition.
1701  */
1702 static void
1703 if_up_dispatch(netmsg_t nmsg)
1704 {
1705 	struct netmsg_if *msg = (struct netmsg_if *)nmsg;
1706 	struct ifnet *ifp = msg->ifp;
1707 	struct ifaddr_container *ifac;
1708 	struct domain *dp;
1709 
1710 	ASSERT_NETISR0;
1711 
1712 	ifq_purge_all(&ifp->if_snd);
1713 	ifp->if_flags |= IFF_UP;
1714 	getmicrotime(&ifp->if_lastchange);
1715 	rt_ifmsg(ifp);
1716 
1717 	/*
1718 	 * The ifaddr processing in the following loop will block,
1719 	 * however, this function is called in netisr0, in which
1720 	 * ifaddr list changes happen, so we don't care about the
1721 	 * blockness of the ifaddr processing here.
1722 	 */
1723 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1724 		struct ifaddr *ifa = ifac->ifa;
1725 
1726 		/* Ignore marker */
1727 		if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1728 			continue;
1729 
1730 		kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1731 	}
1732 
1733 	SLIST_FOREACH(dp, &domains, dom_next)
1734 		if (dp->dom_if_up != NULL)
1735 			dp->dom_if_up(ifp);
1736 
1737 	netisr_replymsg(&nmsg->base, 0);
1738 }
1739 
1740 /*
1741  * Mark an interface down and notify protocols of the transition.  An
1742  * interface going down is also considered to be a synchronizing event.
1743  * We must ensure that all packet processing related to the interface
1744  * has completed before we return so e.g. the caller can free the ifnet
1745  * structure that the mbufs may be referencing.
1746  *
1747  * NOTE: must be called at splnet or eqivalent.
1748  */
1749 void
1750 if_down(struct ifnet *ifp)
1751 {
1752 	struct netmsg_if msg;
1753 
1754 	EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
1755 	netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1756 	    if_down_dispatch);
1757 	msg.ifp = ifp;
1758 	netisr_domsg(&msg.base, 0);
1759 	netmsg_service_sync();
1760 }
1761 
1762 /*
1763  * Mark an interface up and notify protocols of
1764  * the transition.
1765  * NOTE: must be called at splnet or eqivalent.
1766  */
1767 void
1768 if_up(struct ifnet *ifp)
1769 {
1770 	struct netmsg_if msg;
1771 
1772 	netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1773 	    if_up_dispatch);
1774 	msg.ifp = ifp;
1775 	netisr_domsg(&msg.base, 0);
1776 	EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
1777 }
1778 
1779 /*
1780  * Process a link state change.
1781  * NOTE: must be called at splsoftnet or equivalent.
1782  */
1783 void
1784 if_link_state_change(struct ifnet *ifp)
1785 {
1786 	int link_state = ifp->if_link_state;
1787 
1788 	rt_ifmsg(ifp);
1789 	devctl_notify("IFNET", ifp->if_xname,
1790 	    (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1791 
1792 	EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
1793 }
1794 
1795 /*
1796  * Handle interface watchdog timer routines.  Called
1797  * from softclock, we decrement timers (if set) and
1798  * call the appropriate interface routine on expiration.
1799  */
1800 static void
1801 if_slowtimo_dispatch(netmsg_t nmsg)
1802 {
1803 	struct globaldata *gd = mycpu;
1804 	const struct ifnet_array *arr;
1805 	int i;
1806 
1807 	ASSERT_NETISR0;
1808 
1809 	crit_enter_gd(gd);
1810 	lwkt_replymsg(&nmsg->lmsg, 0);  /* reply ASAP */
1811 	crit_exit_gd(gd);
1812 
1813 	arr = ifnet_array_get();
1814 	for (i = 0; i < arr->ifnet_count; ++i) {
1815 		struct ifnet *ifp = arr->ifnet_arr[i];
1816 
1817 		crit_enter_gd(gd);
1818 
1819 		if (if_stats_compat) {
1820 			IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1821 			IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1822 			IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1823 			IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1824 			IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1825 			IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1826 			IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1827 			IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1828 			IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1829 			IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1830 			IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1831 			IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1832 		}
1833 
1834 		if (ifp->if_timer == 0 || --ifp->if_timer) {
1835 			crit_exit_gd(gd);
1836 			continue;
1837 		}
1838 		if (ifp->if_watchdog) {
1839 			if (ifnet_tryserialize_all(ifp)) {
1840 				(*ifp->if_watchdog)(ifp);
1841 				ifnet_deserialize_all(ifp);
1842 			} else {
1843 				/* try again next timeout */
1844 				++ifp->if_timer;
1845 			}
1846 		}
1847 
1848 		crit_exit_gd(gd);
1849 	}
1850 
1851 	callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1852 }
1853 
1854 static void
1855 if_slowtimo(void *arg __unused)
1856 {
1857 	struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1858 
1859 	KASSERT(mycpuid == 0, ("not on cpu0"));
1860 	crit_enter();
1861 	if (lmsg->ms_flags & MSGF_DONE)
1862 		lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1863 	crit_exit();
1864 }
1865 
1866 /*
1867  * Map interface name to
1868  * interface structure pointer.
1869  */
1870 struct ifnet *
1871 ifunit(const char *name)
1872 {
1873 	struct ifnet *ifp;
1874 
1875 	/*
1876 	 * Search all the interfaces for this name/number
1877 	 */
1878 	KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked"));
1879 
1880 	TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1881 		if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1882 			break;
1883 	}
1884 	return (ifp);
1885 }
1886 
1887 struct ifnet *
1888 ifunit_netisr(const char *name)
1889 {
1890 	const struct ifnet_array *arr;
1891 	int i;
1892 
1893 	/*
1894 	 * Search all the interfaces for this name/number
1895 	 */
1896 
1897 	arr = ifnet_array_get();
1898 	for (i = 0; i < arr->ifnet_count; ++i) {
1899 		struct ifnet *ifp = arr->ifnet_arr[i];
1900 
1901 		if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1902 			return ifp;
1903 	}
1904 	return NULL;
1905 }
1906 
1907 /*
1908  * Interface ioctls.
1909  */
1910 int
1911 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1912 {
1913 	struct ifnet *ifp;
1914 	struct ifgroupreq *ifgr;
1915 	struct ifreq *ifr;
1916 	struct ifstat *ifs;
1917 	int error, do_ifup = 0;
1918 	short oif_flags;
1919 	int new_flags;
1920 	size_t namelen, onamelen;
1921 	size_t descrlen;
1922 	char *descrbuf, *odescrbuf;
1923 	char new_name[IFNAMSIZ];
1924 	struct ifaddr *ifa;
1925 	struct sockaddr_dl *sdl;
1926 
1927 	switch (cmd) {
1928 	case SIOCGIFCONF:
1929 		return (ifconf(cmd, data, cred));
1930 	default:
1931 		break;
1932 	}
1933 
1934 	ifr = (struct ifreq *)data;
1935 
1936 	switch (cmd) {
1937 	case SIOCIFCREATE:
1938 	case SIOCIFCREATE2:
1939 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
1940 		if (error)
1941 			return (error);
1942 		return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1943 			(cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL), NULL));
1944 	case SIOCIFDESTROY:
1945 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
1946 		if (error)
1947 			return (error);
1948 		return (if_clone_destroy(ifr->ifr_name));
1949 	case SIOCIFGCLONERS:
1950 		return (if_clone_list((struct if_clonereq *)data));
1951 	case SIOCGIFGMEMB:
1952 		return (if_getgroupmembers((struct ifgroupreq *)data));
1953 	default:
1954 		break;
1955 	}
1956 
1957 	/*
1958 	 * Nominal ioctl through interface, lookup the ifp and obtain a
1959 	 * lock to serialize the ifconfig ioctl operation.
1960 	 */
1961 	ifnet_lock();
1962 
1963 	ifp = ifunit(ifr->ifr_name);
1964 	if (ifp == NULL) {
1965 		ifnet_unlock();
1966 		return (ENXIO);
1967 	}
1968 	error = 0;
1969 
1970 	switch (cmd) {
1971 	case SIOCGIFINDEX:
1972 		ifr->ifr_index = ifp->if_index;
1973 		break;
1974 
1975 	case SIOCGIFFLAGS:
1976 		ifr->ifr_flags = ifp->if_flags;
1977 		ifr->ifr_flagshigh = ifp->if_flags >> 16;
1978 		break;
1979 
1980 	case SIOCGIFCAP:
1981 		ifr->ifr_reqcap = ifp->if_capabilities;
1982 		ifr->ifr_curcap = ifp->if_capenable;
1983 		break;
1984 
1985 	case SIOCGIFMETRIC:
1986 		ifr->ifr_metric = ifp->if_metric;
1987 		break;
1988 
1989 	case SIOCGIFMTU:
1990 		ifr->ifr_mtu = ifp->if_mtu;
1991 		break;
1992 
1993 	case SIOCGIFTSOLEN:
1994 		ifr->ifr_tsolen = ifp->if_tsolen;
1995 		break;
1996 
1997 	case SIOCGIFDATA:
1998 		error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1999 				sizeof(ifp->if_data));
2000 		break;
2001 
2002 	case SIOCGIFPHYS:
2003 		ifr->ifr_phys = ifp->if_physical;
2004 		break;
2005 
2006 	case SIOCGIFPOLLCPU:
2007 		ifr->ifr_pollcpu = -1;
2008 		break;
2009 
2010 	case SIOCSIFPOLLCPU:
2011 		break;
2012 
2013 	case SIOCGIFDESCR:
2014 		error = 0;
2015 		ifnet_lock();
2016 		if (ifp->if_description == NULL) {
2017 			ifr->ifr_buffer.length = 0;
2018 			error = ENOMSG;
2019 		} else {
2020 			/* space for terminating nul */
2021 			descrlen = strlen(ifp->if_description) + 1;
2022 			if (ifr->ifr_buffer.length < descrlen)
2023 				error = ENAMETOOLONG;
2024 			else
2025 				error = copyout(ifp->if_description,
2026 				    ifr->ifr_buffer.buffer, descrlen);
2027 			ifr->ifr_buffer.length = descrlen;
2028 		}
2029 		ifnet_unlock();
2030 		break;
2031 
2032 	case SIOCSIFDESCR:
2033 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2034 		if (error)
2035 			break;
2036 
2037 		/*
2038 		 * Copy only (length-1) bytes to make sure that
2039 		 * if_description is always nul terminated.  The
2040 		 * length parameter is supposed to count the
2041 		 * terminating nul in.
2042 		 */
2043 		if (ifr->ifr_buffer.length > ifdescr_maxlen)
2044 			return (ENAMETOOLONG);
2045 		else if (ifr->ifr_buffer.length == 0)
2046 			descrbuf = NULL;
2047 		else {
2048 			descrbuf = kmalloc(ifr->ifr_buffer.length, M_IFDESCR,
2049 			    M_WAITOK | M_ZERO);
2050 			error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2051 			    ifr->ifr_buffer.length - 1);
2052 			if (error) {
2053 				kfree(descrbuf, M_IFDESCR);
2054 				break;
2055 			}
2056 		}
2057 
2058 		ifnet_lock();
2059 		odescrbuf = ifp->if_description;
2060 		ifp->if_description = descrbuf;
2061 		ifnet_unlock();
2062 
2063 		if (odescrbuf)
2064 			kfree(odescrbuf, M_IFDESCR);
2065 
2066 	case SIOCSIFFLAGS:
2067 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2068 		if (error)
2069 			break;
2070 		new_flags = (ifr->ifr_flags & 0xffff) |
2071 		    (ifr->ifr_flagshigh << 16);
2072 		if (ifp->if_flags & IFF_SMART) {
2073 			/* Smart drivers twiddle their own routes */
2074 		} else if (ifp->if_flags & IFF_UP &&
2075 		    (new_flags & IFF_UP) == 0) {
2076 			if_down(ifp);
2077 		} else if (new_flags & IFF_UP &&
2078 		    (ifp->if_flags & IFF_UP) == 0) {
2079 			do_ifup = 1;
2080 		}
2081 
2082 #ifdef IFPOLL_ENABLE
2083 		if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
2084 			if (new_flags & IFF_NPOLLING)
2085 				ifpoll_register(ifp);
2086 			else
2087 				ifpoll_deregister(ifp);
2088 		}
2089 #endif
2090 
2091 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2092 			(new_flags &~ IFF_CANTCHANGE);
2093 		if (new_flags & IFF_PPROMISC) {
2094 			/* Permanently promiscuous mode requested */
2095 			ifp->if_flags |= IFF_PROMISC;
2096 		} else if (ifp->if_pcount == 0) {
2097 			ifp->if_flags &= ~IFF_PROMISC;
2098 		}
2099 		if (ifp->if_ioctl) {
2100 			ifnet_serialize_all(ifp);
2101 			ifp->if_ioctl(ifp, cmd, data, cred);
2102 			ifnet_deserialize_all(ifp);
2103 		}
2104 		if (do_ifup)
2105 			if_up(ifp);
2106 		getmicrotime(&ifp->if_lastchange);
2107 		break;
2108 
2109 	case SIOCSIFCAP:
2110 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2111 		if (error)
2112 			break;
2113 		if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
2114 			error = EINVAL;
2115 			break;
2116 		}
2117 		ifnet_serialize_all(ifp);
2118 		ifp->if_ioctl(ifp, cmd, data, cred);
2119 		ifnet_deserialize_all(ifp);
2120 		break;
2121 
2122 	case SIOCSIFNAME:
2123 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2124 		if (error)
2125 			break;
2126 		error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2127 		if (error)
2128 			break;
2129 		if (new_name[0] == '\0') {
2130 			error = EINVAL;
2131 			break;
2132 		}
2133 		if (ifunit(new_name) != NULL) {
2134 			error = EEXIST;
2135 			break;
2136 		}
2137 
2138 		EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
2139 
2140 		/* Announce the departure of the interface. */
2141 		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2142 
2143 		strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2144 		ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2145 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2146 		namelen = strlen(new_name);
2147 		onamelen = sdl->sdl_nlen;
2148 		/*
2149 		 * Move the address if needed.  This is safe because we
2150 		 * allocate space for a name of length IFNAMSIZ when we
2151 		 * create this in if_attach().
2152 		 */
2153 		if (namelen != onamelen) {
2154 			bcopy(sdl->sdl_data + onamelen,
2155 			    sdl->sdl_data + namelen, sdl->sdl_alen);
2156 		}
2157 		bcopy(new_name, sdl->sdl_data, namelen);
2158 		sdl->sdl_nlen = namelen;
2159 		sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2160 		bzero(sdl->sdl_data, onamelen);
2161 		while (namelen != 0)
2162 			sdl->sdl_data[--namelen] = 0xff;
2163 
2164 		EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
2165 
2166 		/* Announce the return of the interface. */
2167 		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2168 		break;
2169 
2170 	case SIOCSIFMETRIC:
2171 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2172 		if (error)
2173 			break;
2174 		ifp->if_metric = ifr->ifr_metric;
2175 		getmicrotime(&ifp->if_lastchange);
2176 		break;
2177 
2178 	case SIOCSIFPHYS:
2179 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2180 		if (error)
2181 			break;
2182 		if (ifp->if_ioctl == NULL) {
2183 		        error = EOPNOTSUPP;
2184 			break;
2185 		}
2186 		ifnet_serialize_all(ifp);
2187 		error = ifp->if_ioctl(ifp, cmd, data, cred);
2188 		ifnet_deserialize_all(ifp);
2189 		if (error == 0)
2190 			getmicrotime(&ifp->if_lastchange);
2191 		break;
2192 
2193 	case SIOCSIFMTU:
2194 	{
2195 		u_long oldmtu = ifp->if_mtu;
2196 
2197 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2198 		if (error)
2199 			break;
2200 		if (ifp->if_ioctl == NULL) {
2201 			error = EOPNOTSUPP;
2202 			break;
2203 		}
2204 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2205 			error = EINVAL;
2206 			break;
2207 		}
2208 		ifnet_serialize_all(ifp);
2209 		error = ifp->if_ioctl(ifp, cmd, data, cred);
2210 		ifnet_deserialize_all(ifp);
2211 		if (error == 0) {
2212 			getmicrotime(&ifp->if_lastchange);
2213 			rt_ifmsg(ifp);
2214 		}
2215 		/*
2216 		 * If the link MTU changed, do network layer specific procedure.
2217 		 */
2218 		if (ifp->if_mtu != oldmtu) {
2219 #ifdef INET6
2220 			nd6_setmtu(ifp);
2221 #endif
2222 		}
2223 		break;
2224 	}
2225 
2226 	case SIOCSIFTSOLEN:
2227 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2228 		if (error)
2229 			break;
2230 
2231 		/* XXX need driver supplied upper limit */
2232 		if (ifr->ifr_tsolen <= 0) {
2233 			error = EINVAL;
2234 			break;
2235 		}
2236 		ifp->if_tsolen = ifr->ifr_tsolen;
2237 		break;
2238 
2239 	case SIOCADDMULTI:
2240 	case SIOCDELMULTI:
2241 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2242 		if (error)
2243 			break;
2244 
2245 		/* Don't allow group membership on non-multicast interfaces. */
2246 		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2247 			error = EOPNOTSUPP;
2248 			break;
2249 		}
2250 
2251 		/* Don't let users screw up protocols' entries. */
2252 		if (ifr->ifr_addr.sa_family != AF_LINK) {
2253 			error = EINVAL;
2254 			break;
2255 		}
2256 
2257 		if (cmd == SIOCADDMULTI) {
2258 			struct ifmultiaddr *ifma;
2259 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2260 		} else {
2261 			error = if_delmulti(ifp, &ifr->ifr_addr);
2262 		}
2263 		if (error == 0)
2264 			getmicrotime(&ifp->if_lastchange);
2265 		break;
2266 
2267 	case SIOCSIFPHYADDR:
2268 	case SIOCDIFPHYADDR:
2269 #ifdef INET6
2270 	case SIOCSIFPHYADDR_IN6:
2271 #endif
2272 	case SIOCSLIFPHYADDR:
2273 	case SIOCSIFMEDIA:
2274 	case SIOCSIFGENERIC:
2275 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2276 		if (error)
2277 			break;
2278 		if (ifp->if_ioctl == NULL) {
2279 			error = EOPNOTSUPP;
2280 			break;
2281 		}
2282 		ifnet_serialize_all(ifp);
2283 		error = ifp->if_ioctl(ifp, cmd, data, cred);
2284 		ifnet_deserialize_all(ifp);
2285 		if (error == 0)
2286 			getmicrotime(&ifp->if_lastchange);
2287 		break;
2288 
2289 	case SIOCGIFSTATUS:
2290 		ifs = (struct ifstat *)data;
2291 		ifs->ascii[0] = '\0';
2292 		/* fall through */
2293 	case SIOCGIFPSRCADDR:
2294 	case SIOCGIFPDSTADDR:
2295 	case SIOCGLIFPHYADDR:
2296 	case SIOCGIFMEDIA:
2297 	case SIOCGIFXMEDIA:
2298 	case SIOCGIFGENERIC:
2299 		if (ifp->if_ioctl == NULL) {
2300 			error = EOPNOTSUPP;
2301 			break;
2302 		}
2303 		ifnet_serialize_all(ifp);
2304 		error = ifp->if_ioctl(ifp, cmd, data, cred);
2305 		ifnet_deserialize_all(ifp);
2306 		break;
2307 
2308 	case SIOCSIFLLADDR:
2309 		error = caps_priv_check(cred, SYSCAP_RESTRICTEDROOT);
2310 		if (error)
2311 			break;
2312 		error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
2313 				     ifr->ifr_addr.sa_len);
2314 		EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2315 		break;
2316 
2317 	case SIOCAIFGROUP:
2318 		ifgr = (struct ifgroupreq *)ifr;
2319 		error = caps_priv_check(cred, SYSCAP_NONET_IFCONFIG);
2320 		if (error)
2321 			return (error);
2322 		if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2323 			return (error);
2324 		break;
2325 
2326 	case SIOCDIFGROUP:
2327 		ifgr = (struct ifgroupreq *)ifr;
2328 		error = caps_priv_check(cred, SYSCAP_NONET_IFCONFIG);
2329 		if (error)
2330 			return (error);
2331 		if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2332 			return (error);
2333 		break;
2334 
2335 	case SIOCGIFGROUP:
2336 		ifgr = (struct ifgroupreq *)ifr;
2337 		if ((error = if_getgroups(ifgr, ifp)))
2338 			return (error);
2339 		break;
2340 
2341 	default:
2342 		oif_flags = ifp->if_flags;
2343 		if (so->so_proto == 0) {
2344 			error = EOPNOTSUPP;
2345 			break;
2346 		}
2347 		error = so_pru_control_direct(so, cmd, data, ifp);
2348 
2349 		/*
2350 		 * If the socket control method returns EOPNOTSUPP, pass the
2351 		 * request directly to the interface.
2352 		 *
2353 		 * Exclude the SIOCSIF{ADDR,BRDADDR,DSTADDR,NETMASK} ioctls,
2354 		 * because drivers may trust these ioctls to come from an
2355 		 * already privileged layer and thus do not perform credentials
2356 		 * checks or input validation.
2357 		 */
2358 		if (error == EOPNOTSUPP &&
2359 		    ifp->if_ioctl != NULL &&
2360 		    cmd != SIOCSIFADDR &&
2361 		    cmd != SIOCSIFBRDADDR &&
2362 		    cmd != SIOCSIFDSTADDR &&
2363 		    cmd != SIOCSIFNETMASK) {
2364 			ifnet_serialize_all(ifp);
2365 			error = ifp->if_ioctl(ifp, cmd, data, cred);
2366 			ifnet_deserialize_all(ifp);
2367 		}
2368 
2369 		if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2370 #ifdef INET6
2371 			DELAY(100);/* XXX: temporary workaround for fxp issue*/
2372 			if (ifp->if_flags & IFF_UP) {
2373 				crit_enter();
2374 				in6_if_up(ifp);
2375 				crit_exit();
2376 			}
2377 #endif
2378 		}
2379 		break;
2380 	}
2381 
2382 	ifnet_unlock();
2383 	return (error);
2384 }
2385 
2386 /*
2387  * Set/clear promiscuous mode on interface ifp based on the truth value
2388  * of pswitch.  The calls are reference counted so that only the first
2389  * "on" request actually has an effect, as does the final "off" request.
2390  * Results are undefined if the "off" and "on" requests are not matched.
2391  */
2392 int
2393 ifpromisc(struct ifnet *ifp, int pswitch)
2394 {
2395 	struct ifreq ifr;
2396 	int error;
2397 	int oldflags;
2398 
2399 	oldflags = ifp->if_flags;
2400 	if (ifp->if_flags & IFF_PPROMISC) {
2401 		/* Do nothing if device is in permanently promiscuous mode */
2402 		ifp->if_pcount += pswitch ? 1 : -1;
2403 		return (0);
2404 	}
2405 	if (pswitch) {
2406 		/*
2407 		 * If the device is not configured up, we cannot put it in
2408 		 * promiscuous mode.
2409 		 */
2410 		if ((ifp->if_flags & IFF_UP) == 0)
2411 			return (ENETDOWN);
2412 		if (ifp->if_pcount++ != 0)
2413 			return (0);
2414 		ifp->if_flags |= IFF_PROMISC;
2415 		log(LOG_INFO, "%s: promiscuous mode enabled\n",
2416 		    ifp->if_xname);
2417 	} else {
2418 		if (--ifp->if_pcount > 0)
2419 			return (0);
2420 		ifp->if_flags &= ~IFF_PROMISC;
2421 		log(LOG_INFO, "%s: promiscuous mode disabled\n",
2422 		    ifp->if_xname);
2423 	}
2424 	ifr.ifr_flags = ifp->if_flags;
2425 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
2426 	ifnet_serialize_all(ifp);
2427 	error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2428 	ifnet_deserialize_all(ifp);
2429 	if (error == 0)
2430 		rt_ifmsg(ifp);
2431 	else
2432 		ifp->if_flags = oldflags;
2433 	return error;
2434 }
2435 
2436 /*
2437  * Return interface configuration
2438  * of system.  List may be used
2439  * in later ioctl's (above) to get
2440  * other information.
2441  */
2442 static int
2443 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2444 {
2445 	struct ifconf *ifc = (struct ifconf *)data;
2446 	struct ifnet *ifp;
2447 	struct sockaddr *sa;
2448 	struct ifreq ifr, *ifrp;
2449 	int space = ifc->ifc_len, error = 0;
2450 
2451 	ifrp = ifc->ifc_req;
2452 
2453 	ifnet_lock();
2454 	TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2455 		struct ifaddr_container *ifac, *ifac_mark;
2456 		struct ifaddr_marker mark;
2457 		struct ifaddrhead *head;
2458 		int addrs;
2459 
2460 		if (space <= sizeof ifr)
2461 			break;
2462 
2463 		/*
2464 		 * Zero the stack declared structure first to prevent
2465 		 * memory disclosure.
2466 		 */
2467 		bzero(&ifr, sizeof(ifr));
2468 		if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2469 		    >= sizeof(ifr.ifr_name)) {
2470 			error = ENAMETOOLONG;
2471 			break;
2472 		}
2473 
2474 		/*
2475 		 * Add a marker, since copyout() could block and during that
2476 		 * period the list could be changed.  Inserting the marker to
2477 		 * the header of the list will not cause trouble for the code
2478 		 * assuming that the first element of the list is AF_LINK; the
2479 		 * marker will be moved to the next position w/o blocking.
2480 		 */
2481 		ifa_marker_init(&mark, ifp);
2482 		ifac_mark = &mark.ifac;
2483 		head = &ifp->if_addrheads[mycpuid];
2484 
2485 		addrs = 0;
2486 		TAILQ_INSERT_HEAD(head, ifac_mark, ifa_link);
2487 		while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
2488 			struct ifaddr *ifa = ifac->ifa;
2489 
2490 			TAILQ_REMOVE(head, ifac_mark, ifa_link);
2491 			TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
2492 
2493 			/* Ignore marker */
2494 			if (ifa->ifa_addr->sa_family == AF_UNSPEC)
2495 				continue;
2496 
2497 			if (space <= sizeof ifr)
2498 				break;
2499 			sa = ifa->ifa_addr;
2500 			if (cred->cr_prison && prison_if(cred, sa))
2501 				continue;
2502 			addrs++;
2503 			/*
2504 			 * Keep a reference on this ifaddr, so that it will
2505 			 * not be destroyed when its address is copied to
2506 			 * the userland, which could block.
2507 			 */
2508 			IFAREF(ifa);
2509 			if (sa->sa_len <= sizeof(*sa)) {
2510 				ifr.ifr_addr = *sa;
2511 				error = copyout(&ifr, ifrp, sizeof ifr);
2512 				ifrp++;
2513 			} else {
2514 				if (space < (sizeof ifr) + sa->sa_len -
2515 					    sizeof(*sa)) {
2516 					IFAFREE(ifa);
2517 					break;
2518 				}
2519 				space -= sa->sa_len - sizeof(*sa);
2520 				error = copyout(&ifr, ifrp,
2521 						sizeof ifr.ifr_name);
2522 				if (error == 0)
2523 					error = copyout(sa, &ifrp->ifr_addr,
2524 							sa->sa_len);
2525 				ifrp = (struct ifreq *)
2526 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2527 			}
2528 			IFAFREE(ifa);
2529 			if (error)
2530 				break;
2531 			space -= sizeof ifr;
2532 		}
2533 		TAILQ_REMOVE(head, ifac_mark, ifa_link);
2534 		if (error)
2535 			break;
2536 		if (!addrs) {
2537 			bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2538 			error = copyout(&ifr, ifrp, sizeof ifr);
2539 			if (error)
2540 				break;
2541 			space -= sizeof ifr;
2542 			ifrp++;
2543 		}
2544 	}
2545 	ifnet_unlock();
2546 
2547 	ifc->ifc_len -= space;
2548 	return (error);
2549 }
2550 
2551 /*
2552  * Just like if_promisc(), but for all-multicast-reception mode.
2553  */
2554 int
2555 if_allmulti(struct ifnet *ifp, int onswitch)
2556 {
2557 	int error = 0;
2558 	struct ifreq ifr;
2559 
2560 	crit_enter();
2561 
2562 	if (onswitch) {
2563 		if (ifp->if_amcount++ == 0) {
2564 			ifp->if_flags |= IFF_ALLMULTI;
2565 			ifr.ifr_flags = ifp->if_flags;
2566 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
2567 			ifnet_serialize_all(ifp);
2568 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2569 					      NULL);
2570 			ifnet_deserialize_all(ifp);
2571 		}
2572 	} else {
2573 		if (ifp->if_amcount > 1) {
2574 			ifp->if_amcount--;
2575 		} else {
2576 			ifp->if_amcount = 0;
2577 			ifp->if_flags &= ~IFF_ALLMULTI;
2578 			ifr.ifr_flags = ifp->if_flags;
2579 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
2580 			ifnet_serialize_all(ifp);
2581 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2582 					      NULL);
2583 			ifnet_deserialize_all(ifp);
2584 		}
2585 	}
2586 
2587 	crit_exit();
2588 
2589 	if (error == 0)
2590 		rt_ifmsg(ifp);
2591 	return error;
2592 }
2593 
2594 /*
2595  * Add a multicast listenership to the interface in question.
2596  * The link layer provides a routine which converts
2597  */
2598 int
2599 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2600     struct ifmultiaddr **retifma)
2601 {
2602 	struct sockaddr *llsa, *dupsa;
2603 	int error;
2604 	struct ifmultiaddr *ifma;
2605 
2606 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
2607 
2608 	/*
2609 	 * If the matching multicast address already exists
2610 	 * then don't add a new one, just add a reference
2611 	 */
2612 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2613 		if (sa_equal(sa, ifma->ifma_addr)) {
2614 			ifma->ifma_refcount++;
2615 			if (retifma)
2616 				*retifma = ifma;
2617 			return 0;
2618 		}
2619 	}
2620 
2621 	/*
2622 	 * Give the link layer a chance to accept/reject it, and also
2623 	 * find out which AF_LINK address this maps to, if it isn't one
2624 	 * already.
2625 	 */
2626 	if (ifp->if_resolvemulti) {
2627 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
2628 		if (error)
2629 			return error;
2630 	} else {
2631 		llsa = NULL;
2632 	}
2633 
2634 	ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2635 	dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_INTWAIT);
2636 	bcopy(sa, dupsa, sa->sa_len);
2637 
2638 	ifma->ifma_addr = dupsa;
2639 	ifma->ifma_lladdr = llsa;
2640 	ifma->ifma_ifp = ifp;
2641 	ifma->ifma_refcount = 1;
2642 	ifma->ifma_protospec = NULL;
2643 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2644 
2645 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2646 	if (retifma)
2647 		*retifma = ifma;
2648 
2649 	if (llsa != NULL) {
2650 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2651 			if (sa_equal(ifma->ifma_addr, llsa))
2652 				break;
2653 		}
2654 		if (ifma) {
2655 			ifma->ifma_refcount++;
2656 		} else {
2657 			ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2658 			dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_INTWAIT);
2659 			bcopy(llsa, dupsa, llsa->sa_len);
2660 			ifma->ifma_addr = dupsa;
2661 			ifma->ifma_ifp = ifp;
2662 			ifma->ifma_refcount = 1;
2663 			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2664 		}
2665 	}
2666 	/*
2667 	 * We are certain we have added something, so call down to the
2668 	 * interface to let them know about it.
2669 	 */
2670 	if (ifp->if_ioctl)
2671 		ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2672 
2673 	return 0;
2674 }
2675 
2676 int
2677 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2678     struct ifmultiaddr **retifma)
2679 {
2680 	int error;
2681 
2682 	ifnet_serialize_all(ifp);
2683 	error = if_addmulti_serialized(ifp, sa, retifma);
2684 	ifnet_deserialize_all(ifp);
2685 
2686 	return error;
2687 }
2688 
2689 /*
2690  * Remove a reference to a multicast address on this interface.  Yell
2691  * if the request does not match an existing membership.
2692  */
2693 static int
2694 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2695 {
2696 	struct ifmultiaddr *ifma;
2697 
2698 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
2699 
2700 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2701 		if (sa_equal(sa, ifma->ifma_addr))
2702 			break;
2703 	if (ifma == NULL)
2704 		return ENOENT;
2705 
2706 	if (ifma->ifma_refcount > 1) {
2707 		ifma->ifma_refcount--;
2708 		return 0;
2709 	}
2710 
2711 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
2712 	sa = ifma->ifma_lladdr;
2713 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2714 	/*
2715 	 * Make sure the interface driver is notified
2716 	 * in the case of a link layer mcast group being left.
2717 	 */
2718 	if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2719 		ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2720 	kfree(ifma->ifma_addr, M_IFMADDR);
2721 	kfree(ifma, M_IFMADDR);
2722 	if (sa == NULL)
2723 		return 0;
2724 
2725 	/*
2726 	 * Now look for the link-layer address which corresponds to
2727 	 * this network address.  It had been squirreled away in
2728 	 * ifma->ifma_lladdr for this purpose (so we don't have
2729 	 * to call ifp->if_resolvemulti() again), and we saved that
2730 	 * value in sa above.  If some nasty deleted the
2731 	 * link-layer address out from underneath us, we can deal because
2732 	 * the address we stored was is not the same as the one which was
2733 	 * in the record for the link-layer address.  (So we don't complain
2734 	 * in that case.)
2735 	 */
2736 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2737 		if (sa_equal(sa, ifma->ifma_addr))
2738 			break;
2739 	if (ifma == NULL)
2740 		return 0;
2741 
2742 	if (ifma->ifma_refcount > 1) {
2743 		ifma->ifma_refcount--;
2744 		return 0;
2745 	}
2746 
2747 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2748 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2749 	kfree(ifma->ifma_addr, M_IFMADDR);
2750 	kfree(sa, M_IFMADDR);
2751 	kfree(ifma, M_IFMADDR);
2752 
2753 	return 0;
2754 }
2755 
2756 int
2757 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2758 {
2759 	int error;
2760 
2761 	ifnet_serialize_all(ifp);
2762 	error = if_delmulti_serialized(ifp, sa);
2763 	ifnet_deserialize_all(ifp);
2764 
2765 	return error;
2766 }
2767 
2768 /*
2769  * Delete all multicast group membership for an interface.
2770  * Should be used to quickly flush all multicast filters.
2771  */
2772 void
2773 if_delallmulti_serialized(struct ifnet *ifp)
2774 {
2775 	struct ifmultiaddr *ifma, mark;
2776 	struct sockaddr sa;
2777 
2778 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
2779 
2780 	bzero(&sa, sizeof(sa));
2781 	sa.sa_family = AF_UNSPEC;
2782 	sa.sa_len = sizeof(sa);
2783 
2784 	bzero(&mark, sizeof(mark));
2785 	mark.ifma_addr = &sa;
2786 
2787 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2788 	while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2789 		TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2790 		TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2791 		    ifma_link);
2792 
2793 		if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2794 			continue;
2795 
2796 		if_delmulti_serialized(ifp, ifma->ifma_addr);
2797 	}
2798 	TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2799 }
2800 
2801 
2802 /*
2803  * Set the link layer address on an interface.
2804  *
2805  * At this time we only support certain types of interfaces,
2806  * and we don't allow the length of the address to change.
2807  */
2808 int
2809 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2810 {
2811 	struct sockaddr_dl *sdl;
2812 	struct ifreq ifr;
2813 
2814 	sdl = IF_LLSOCKADDR(ifp);
2815 	if (sdl == NULL)
2816 		return (EINVAL);
2817 	if (len != sdl->sdl_alen)	/* don't allow length to change */
2818 		return (EINVAL);
2819 	switch (ifp->if_type) {
2820 	case IFT_ETHER:			/* these types use struct arpcom */
2821 	case IFT_XETHER:
2822 	case IFT_L2VLAN:
2823 	case IFT_IEEE8023ADLAG:
2824 		bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2825 		bcopy(lladdr, LLADDR(sdl), len);
2826 		break;
2827 	default:
2828 		return (ENODEV);
2829 	}
2830 	/*
2831 	 * If the interface is already up, we need
2832 	 * to re-init it in order to reprogram its
2833 	 * address filter.
2834 	 */
2835 	ifnet_serialize_all(ifp);
2836 	if ((ifp->if_flags & IFF_UP) != 0) {
2837 #ifdef INET
2838 		struct ifaddr_container *ifac;
2839 #endif
2840 
2841 		ifp->if_flags &= ~IFF_UP;
2842 		ifr.ifr_flags = ifp->if_flags;
2843 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
2844 		ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2845 			      NULL);
2846 		ifp->if_flags |= IFF_UP;
2847 		ifr.ifr_flags = ifp->if_flags;
2848 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
2849 		ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2850 				 NULL);
2851 #ifdef INET
2852 		/*
2853 		 * Also send gratuitous ARPs to notify other nodes about
2854 		 * the address change.
2855 		 */
2856 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2857 			struct ifaddr *ifa = ifac->ifa;
2858 
2859 			if (ifa->ifa_addr != NULL &&
2860 			    ifa->ifa_addr->sa_family == AF_INET)
2861 				arp_gratuitous(ifp, ifa);
2862 		}
2863 #endif
2864 	}
2865 	ifnet_deserialize_all(ifp);
2866 	return (0);
2867 }
2868 
2869 
2870 /*
2871  * Tunnel interfaces can nest, also they may cause infinite recursion
2872  * calls when misconfigured.  Introduce an upper limit to prevent infinite
2873  * recursions, as well as to constrain the nesting depth.
2874  *
2875  * Return 0, if tunnel nesting count is equal or less than limit.
2876  */
2877 int
2878 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
2879 			int limit)
2880 {
2881 	struct m_tag *mtag;
2882 	int count;
2883 
2884 	count = 1;
2885 	mtag = m_tag_locate(m, cookie, 0 /* type */, NULL);
2886 	if (mtag != NULL)
2887 		count += *(int *)(mtag + 1);
2888 	if (count > limit) {
2889 		log(LOG_NOTICE,
2890 		    "%s: packet looped too many times (%d), limit %d\n",
2891 		    ifp->if_xname, count, limit);
2892 		return (ELOOP);
2893 	}
2894 
2895 	if (mtag == NULL) {
2896 		mtag = m_tag_alloc(cookie, 0, sizeof(int), M_NOWAIT);
2897 		if (mtag == NULL)
2898 			return (ENOMEM);
2899 		m_tag_prepend(m, mtag);
2900 	}
2901 
2902 	*(int *)(mtag + 1) = count;
2903 	return (0);
2904 }
2905 
2906 
2907 /*
2908  * Locate an interface based on a complete address.
2909  */
2910 struct ifnet *
2911 if_bylla(const void *lla, unsigned char lla_len)
2912 {
2913 	const struct ifnet_array *arr;
2914 	struct ifnet *ifp;
2915 	struct sockaddr_dl *sdl;
2916 	int i;
2917 
2918 	arr = ifnet_array_get();
2919 	for (i = 0; i < arr->ifnet_count; ++i) {
2920 		ifp = arr->ifnet_arr[i];
2921 		if (ifp->if_addrlen != lla_len)
2922 			continue;
2923 
2924 		sdl = IF_LLSOCKADDR(ifp);
2925 		if (memcmp(lla, LLADDR(sdl), lla_len) == 0)
2926 			return (ifp);
2927 	}
2928 	return (NULL);
2929 }
2930 
2931 struct ifmultiaddr *
2932 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2933 {
2934 	struct ifmultiaddr *ifma;
2935 
2936 	/* TODO: need ifnet_serialize_main */
2937 	ifnet_serialize_all(ifp);
2938 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2939 		if (sa_equal(ifma->ifma_addr, sa))
2940 			break;
2941 	ifnet_deserialize_all(ifp);
2942 
2943 	return ifma;
2944 }
2945 
2946 /*
2947  * This function locates the first real ethernet MAC from a network
2948  * card and loads it into node, returning 0 on success or ENOENT if
2949  * no suitable interfaces were found.  It is used by the uuid code to
2950  * generate a unique 6-byte number.
2951  */
2952 int
2953 if_getanyethermac(uint16_t *node, int minlen)
2954 {
2955 	struct ifnet *ifp;
2956 	struct sockaddr_dl *sdl;
2957 
2958 	ifnet_lock();
2959 	TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2960 		if (ifp->if_type != IFT_ETHER)
2961 			continue;
2962 		sdl = IF_LLSOCKADDR(ifp);
2963 		if (sdl->sdl_alen < minlen)
2964 			continue;
2965 		bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2966 		      minlen);
2967 		ifnet_unlock();
2968 		return(0);
2969 	}
2970 	ifnet_unlock();
2971 	return (ENOENT);
2972 }
2973 
2974 /*
2975  * The name argument must be a pointer to storage which will last as
2976  * long as the interface does.  For physical devices, the result of
2977  * device_get_name(dev) is a good choice and for pseudo-devices a
2978  * static string works well.
2979  */
2980 void
2981 if_initname(struct ifnet *ifp, const char *name, int unit)
2982 {
2983 	ifp->if_dname = name;
2984 	ifp->if_dunit = unit;
2985 	if (unit != IF_DUNIT_NONE)
2986 		ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2987 	else
2988 		strlcpy(ifp->if_xname, name, IFNAMSIZ);
2989 }
2990 
2991 int
2992 if_printf(struct ifnet *ifp, const char *fmt, ...)
2993 {
2994 	__va_list ap;
2995 	int retval;
2996 
2997 	retval = kprintf("%s: ", ifp->if_xname);
2998 	__va_start(ap, fmt);
2999 	retval += kvprintf(fmt, ap);
3000 	__va_end(ap);
3001 	return (retval);
3002 }
3003 
3004 struct ifnet *
3005 if_alloc(uint8_t type)
3006 {
3007 	struct ifnet *ifp;
3008 	size_t size;
3009 
3010 	/*
3011 	 * XXX temporary hack until arpcom is setup in if_l2com
3012 	 */
3013 	if (type == IFT_ETHER)
3014 		size = sizeof(struct arpcom);
3015 	else
3016 		size = sizeof(struct ifnet);
3017 
3018 	ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
3019 
3020 	ifp->if_type = type;
3021 
3022 	if (if_com_alloc[type] != NULL) {
3023 		ifp->if_l2com = if_com_alloc[type](type, ifp);
3024 		if (ifp->if_l2com == NULL) {
3025 			kfree(ifp, M_IFNET);
3026 			return (NULL);
3027 		}
3028 	}
3029 	return (ifp);
3030 }
3031 
3032 void
3033 if_free(struct ifnet *ifp)
3034 {
3035 	if (ifp->if_description != NULL)
3036 		kfree(ifp->if_description, M_IFDESCR);
3037 	kfree(ifp, M_IFNET);
3038 }
3039 
3040 void
3041 ifq_set_classic(struct ifaltq *ifq)
3042 {
3043 	ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
3044 	    ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
3045 }
3046 
3047 void
3048 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
3049     ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
3050 {
3051 	int q;
3052 
3053 	KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
3054 	KASSERT(enqueue != NULL, ("enqueue is not specified"));
3055 	KASSERT(dequeue != NULL, ("dequeue is not specified"));
3056 	KASSERT(request != NULL, ("request is not specified"));
3057 
3058 	ifq->altq_mapsubq = mapsubq;
3059 	for (q = 0; q < ifq->altq_subq_cnt; ++q) {
3060 		struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
3061 
3062 		ifsq->ifsq_enqueue = enqueue;
3063 		ifsq->ifsq_dequeue = dequeue;
3064 		ifsq->ifsq_request = request;
3065 	}
3066 }
3067 
3068 static void
3069 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
3070 {
3071 
3072 	classq_add(&ifsq->ifsq_norm, m);
3073 	ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
3074 }
3075 
3076 static void
3077 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
3078 {
3079 
3080 	classq_add(&ifsq->ifsq_prio, m);
3081 	ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
3082 	ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
3083 }
3084 
3085 static struct mbuf *
3086 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
3087 {
3088 	struct mbuf *m;
3089 
3090 	m = classq_get(&ifsq->ifsq_norm);
3091 	if (m != NULL)
3092 		ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
3093 	return (m);
3094 }
3095 
3096 static struct mbuf *
3097 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
3098 {
3099 	struct mbuf *m;
3100 
3101 	m = classq_get(&ifsq->ifsq_prio);
3102 	if (m != NULL) {
3103 		ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
3104 		ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
3105 	}
3106 	return (m);
3107 }
3108 
3109 int
3110 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
3111     struct altq_pktattr *pa __unused)
3112 {
3113 
3114 	M_ASSERTPKTHDR(m);
3115 again:
3116 	if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
3117 	    ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
3118 		struct mbuf *m_drop;
3119 
3120 		if (m->m_flags & M_PRIO) {
3121 			m_drop = NULL;
3122 			if (ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen >> 1) &&
3123 			    ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt >> 1)) {
3124 				/* Try dropping some from normal queue. */
3125 				m_drop = ifsq_norm_dequeue(ifsq);
3126 			}
3127 			if (m_drop == NULL)
3128 				m_drop = ifsq_prio_dequeue(ifsq);
3129 		} else {
3130 			m_drop = ifsq_norm_dequeue(ifsq);
3131 		}
3132 		if (m_drop != NULL) {
3133 			IFNET_STAT_INC(ifsq->ifsq_ifp, oqdrops, 1);
3134 			m_freem(m_drop);
3135 			goto again;
3136 		}
3137 		/*
3138 		 * No old packets could be dropped!
3139 		 * NOTE: Caller increases oqdrops.
3140 		 */
3141 		m_freem(m);
3142 		return (ENOBUFS);
3143 	} else {
3144 		if (m->m_flags & M_PRIO)
3145 			ifsq_prio_enqueue(ifsq, m);
3146 		else
3147 			ifsq_norm_enqueue(ifsq, m);
3148 		return (0);
3149 	}
3150 }
3151 
3152 struct mbuf *
3153 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
3154 {
3155 	struct mbuf *m;
3156 
3157 	switch (op) {
3158 	case ALTDQ_POLL:
3159 		m = classq_head(&ifsq->ifsq_prio);
3160 		if (m == NULL)
3161 			m = classq_head(&ifsq->ifsq_norm);
3162 		break;
3163 
3164 	case ALTDQ_REMOVE:
3165 		m = ifsq_prio_dequeue(ifsq);
3166 		if (m == NULL)
3167 			m = ifsq_norm_dequeue(ifsq);
3168 		break;
3169 
3170 	default:
3171 		panic("unsupported ALTQ dequeue op: %d", op);
3172 	}
3173 	return m;
3174 }
3175 
3176 int
3177 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
3178 {
3179 	switch (req) {
3180 	case ALTRQ_PURGE:
3181 		for (;;) {
3182 			struct mbuf *m;
3183 
3184 			m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
3185 			if (m == NULL)
3186 				break;
3187 			m_freem(m);
3188 		}
3189 		break;
3190 
3191 	default:
3192 		panic("unsupported ALTQ request: %d", req);
3193 	}
3194 	return 0;
3195 }
3196 
3197 static void
3198 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
3199 {
3200 	struct ifnet *ifp = ifsq_get_ifp(ifsq);
3201 	int running = 0, need_sched;
3202 
3203 	/*
3204 	 * Try to do direct ifnet.if_start on the subqueue first, if there is
3205 	 * contention on the subqueue hardware serializer, ifnet.if_start on
3206 	 * the subqueue will be scheduled on the subqueue owner CPU.
3207 	 */
3208 	if (!ifsq_tryserialize_hw(ifsq)) {
3209 		/*
3210 		 * Subqueue hardware serializer contention happened,
3211 		 * ifnet.if_start on the subqueue is scheduled on
3212 		 * the subqueue owner CPU, and we keep going.
3213 		 */
3214 		ifsq_ifstart_schedule(ifsq, 1);
3215 		return;
3216 	}
3217 
3218 	if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
3219 		ifp->if_start(ifp, ifsq);
3220 		if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
3221 			running = 1;
3222 	}
3223 	need_sched = ifsq_ifstart_need_schedule(ifsq, running);
3224 
3225 	ifsq_deserialize_hw(ifsq);
3226 
3227 	if (need_sched) {
3228 		/*
3229 		 * More data need to be transmitted, ifnet.if_start on the
3230 		 * subqueue is scheduled on the subqueue owner CPU, and we
3231 		 * keep going.
3232 		 * NOTE: ifnet.if_start subqueue interlock is not released.
3233 		 */
3234 		ifsq_ifstart_schedule(ifsq, force_sched);
3235 	}
3236 }
3237 
3238 /*
3239  * Subqeue packets staging mechanism:
3240  *
3241  * The packets enqueued into the subqueue are staged to a certain amount
3242  * before the ifnet.if_start on the subqueue is called.  In this way, the
3243  * driver could avoid writing to hardware registers upon every packet,
3244  * instead, hardware registers could be written when certain amount of
3245  * packets are put onto hardware TX ring.  The measurement on several modern
3246  * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
3247  * registers writing aggregation could save ~20% CPU time when 18bytes UDP
3248  * datagrams are transmitted at 1.48Mpps.  The performance improvement by
3249  * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
3250  * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
3251  *
3252  * Subqueue packets staging is performed for two entry points into drivers'
3253  * transmission function:
3254  * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
3255  * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
3256  *
3257  * Subqueue packets staging will be stopped upon any of the following
3258  * conditions:
3259  * - If the count of packets enqueued on the current CPU is great than or
3260  *   equal to ifsq_stage_cntmax. (XXX this should be per-interface)
3261  * - If the total length of packets enqueued on the current CPU is great
3262  *   than or equal to the hardware's MTU - max_protohdr.  max_protohdr is
3263  *   cut from the hardware's MTU mainly bacause a full TCP segment's size
3264  *   is usually less than hardware's MTU.
3265  * - ifsq_ifstart_schedule() is not pending on the current CPU and
3266  *   ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
3267  *   released.
3268  * - The if_start_rollup(), which is registered as low priority netisr
3269  *   rollup function, is called; probably because no more work is pending
3270  *   for netisr.
3271  *
3272  * NOTE:
3273  * Currently subqueue packet staging is only performed in netisr threads.
3274  */
3275 int
3276 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
3277 {
3278 	struct ifaltq *ifq = &ifp->if_snd;
3279 	struct ifaltq_subque *ifsq;
3280 	int error, start = 0, len, mcast = 0, avoid_start = 0;
3281 	struct ifsubq_stage_head *head = NULL;
3282 	struct ifsubq_stage *stage = NULL;
3283 	struct globaldata *gd = mycpu;
3284 	struct thread *td = gd->gd_curthread;
3285 
3286 	crit_enter_quick(td);
3287 
3288 	ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
3289 	ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
3290 
3291 	len = m->m_pkthdr.len;
3292 	if (m->m_flags & M_MCAST)
3293 		mcast = 1;
3294 
3295 	if (td->td_type == TD_TYPE_NETISR) {
3296 		head = &ifsubq_stage_heads[mycpuid];
3297 		stage = ifsq_get_stage(ifsq, mycpuid);
3298 
3299 		stage->stg_cnt++;
3300 		stage->stg_len += len;
3301 		if (stage->stg_cnt < ifsq_stage_cntmax &&
3302 		    stage->stg_len < (ifp->if_mtu - max_protohdr))
3303 			avoid_start = 1;
3304 	}
3305 
3306 	ALTQ_SQ_LOCK(ifsq);
3307 	error = ifsq_enqueue_locked(ifsq, m, pa);
3308 	if (error) {
3309 		IFNET_STAT_INC(ifp, oqdrops, 1);
3310 		if (!ifsq_data_ready(ifsq)) {
3311 			ALTQ_SQ_UNLOCK(ifsq);
3312 			goto done;
3313 		}
3314 		avoid_start = 0;
3315 	} else {
3316 		IFNET_STAT_INC(ifp, obytes, len);
3317 		if (mcast)
3318 			IFNET_STAT_INC(ifp, omcasts, 1);
3319 	}
3320 	if (!ifsq_is_started(ifsq)) {
3321 		if (avoid_start) {
3322 			ALTQ_SQ_UNLOCK(ifsq);
3323 
3324 			KKASSERT(!error);
3325 			if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
3326 				ifsq_stage_insert(head, stage);
3327 
3328 			goto done;
3329 		}
3330 
3331 		/*
3332 		 * Hold the subqueue interlock of ifnet.if_start
3333 		 */
3334 		ifsq_set_started(ifsq);
3335 		start = 1;
3336 	}
3337 	ALTQ_SQ_UNLOCK(ifsq);
3338 
3339 	if (stage != NULL) {
3340 		if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
3341 			KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3342 			if (!avoid_start) {
3343 				ifsq_stage_remove(head, stage);
3344 				ifsq_ifstart_schedule(ifsq, 1);
3345 			}
3346 			goto done;
3347 		}
3348 
3349 		if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
3350 			ifsq_stage_remove(head, stage);
3351 		} else {
3352 			stage->stg_cnt = 0;
3353 			stage->stg_len = 0;
3354 		}
3355 	}
3356 
3357 	if (start)
3358 		ifsq_ifstart_try(ifsq, 0);
3359 
3360 done:
3361 	crit_exit_quick(td);
3362 	return error;
3363 }
3364 
3365 void *
3366 ifa_create(int size)
3367 {
3368 	struct ifaddr *ifa;
3369 	int i;
3370 
3371 	KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
3372 
3373 	ifa = kmalloc(size, M_IFADDR, M_INTWAIT | M_ZERO);
3374 
3375 	/*
3376 	 * Make ifa_container availabel on all CPUs, since they
3377 	 * could be accessed by any threads.
3378 	 */
3379 	ifa->ifa_containers =
3380 		kmalloc(ncpus * sizeof(struct ifaddr_container),
3381 			M_IFADDR,
3382 			M_INTWAIT | M_ZERO | M_CACHEALIGN);
3383 
3384 	ifa->ifa_ncnt = ncpus;
3385 	for (i = 0; i < ncpus; ++i) {
3386 		struct ifaddr_container *ifac = &ifa->ifa_containers[i];
3387 
3388 		ifac->ifa_magic = IFA_CONTAINER_MAGIC;
3389 		ifac->ifa = ifa;
3390 		ifac->ifa_refcnt = 1;
3391 	}
3392 #ifdef IFADDR_DEBUG
3393 	kprintf("alloc ifa %p %d\n", ifa, size);
3394 #endif
3395 	return ifa;
3396 }
3397 
3398 void
3399 ifac_free(struct ifaddr_container *ifac, int cpu_id)
3400 {
3401 	struct ifaddr *ifa = ifac->ifa;
3402 
3403 	KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
3404 	KKASSERT(ifac->ifa_refcnt == 0);
3405 	KASSERT(ifac->ifa_listmask == 0,
3406 		("ifa is still on %#x lists", ifac->ifa_listmask));
3407 
3408 	ifac->ifa_magic = IFA_CONTAINER_DEAD;
3409 
3410 #ifdef IFADDR_DEBUG_VERBOSE
3411 	kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
3412 #endif
3413 
3414 	KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
3415 		("invalid # of ifac, %d", ifa->ifa_ncnt));
3416 	if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
3417 #ifdef IFADDR_DEBUG
3418 		kprintf("free ifa %p\n", ifa);
3419 #endif
3420 		kfree(ifa->ifa_containers, M_IFADDR);
3421 		kfree(ifa, M_IFADDR);
3422 	}
3423 }
3424 
3425 static void
3426 ifa_iflink_dispatch(netmsg_t nmsg)
3427 {
3428 	struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3429 	struct ifaddr *ifa = msg->ifa;
3430 	struct ifnet *ifp = msg->ifp;
3431 	int cpu = mycpuid;
3432 	struct ifaddr_container *ifac;
3433 
3434 	crit_enter();
3435 
3436 	ifac = &ifa->ifa_containers[cpu];
3437 	ASSERT_IFAC_VALID(ifac);
3438 	KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
3439 		("ifaddr is on if_addrheads"));
3440 
3441 	ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
3442 	if (msg->tail)
3443 		TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
3444 	else
3445 		TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
3446 
3447 	crit_exit();
3448 
3449 	netisr_forwardmsg_all(&nmsg->base, cpu + 1);
3450 }
3451 
3452 void
3453 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
3454 {
3455 	struct netmsg_ifaddr msg;
3456 
3457 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3458 		    0, ifa_iflink_dispatch);
3459 	msg.ifa = ifa;
3460 	msg.ifp = ifp;
3461 	msg.tail = tail;
3462 
3463 	netisr_domsg(&msg.base, 0);
3464 }
3465 
3466 static void
3467 ifa_ifunlink_dispatch(netmsg_t nmsg)
3468 {
3469 	struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3470 	struct ifaddr *ifa = msg->ifa;
3471 	struct ifnet *ifp = msg->ifp;
3472 	int cpu = mycpuid;
3473 	struct ifaddr_container *ifac;
3474 
3475 	crit_enter();
3476 
3477 	ifac = &ifa->ifa_containers[cpu];
3478 	ASSERT_IFAC_VALID(ifac);
3479 	KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3480 		("ifaddr is not on if_addrhead"));
3481 
3482 	TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3483 	ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3484 
3485 	crit_exit();
3486 
3487 	netisr_forwardmsg_all(&nmsg->base, cpu + 1);
3488 }
3489 
3490 void
3491 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3492 {
3493 	struct netmsg_ifaddr msg;
3494 
3495 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3496 		    0, ifa_ifunlink_dispatch);
3497 	msg.ifa = ifa;
3498 	msg.ifp = ifp;
3499 
3500 	netisr_domsg(&msg.base, 0);
3501 }
3502 
3503 static void
3504 ifa_destroy_dispatch(netmsg_t nmsg)
3505 {
3506 	struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3507 
3508 	IFAFREE(msg->ifa);
3509 	netisr_forwardmsg_all(&nmsg->base, mycpuid + 1);
3510 }
3511 
3512 void
3513 ifa_destroy(struct ifaddr *ifa)
3514 {
3515 	struct netmsg_ifaddr msg;
3516 
3517 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3518 		    0, ifa_destroy_dispatch);
3519 	msg.ifa = ifa;
3520 
3521 	netisr_domsg(&msg.base, 0);
3522 }
3523 
3524 static void
3525 if_start_rollup(void)
3526 {
3527 	struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3528 	struct ifsubq_stage *stage;
3529 
3530 	crit_enter();
3531 
3532 	while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3533 		struct ifaltq_subque *ifsq = stage->stg_subq;
3534 		int is_sched = 0;
3535 
3536 		if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3537 			is_sched = 1;
3538 		ifsq_stage_remove(head, stage);
3539 
3540 		if (is_sched) {
3541 			ifsq_ifstart_schedule(ifsq, 1);
3542 		} else {
3543 			int start = 0;
3544 
3545 			ALTQ_SQ_LOCK(ifsq);
3546 			if (!ifsq_is_started(ifsq)) {
3547 				/*
3548 				 * Hold the subqueue interlock of
3549 				 * ifnet.if_start
3550 				 */
3551 				ifsq_set_started(ifsq);
3552 				start = 1;
3553 			}
3554 			ALTQ_SQ_UNLOCK(ifsq);
3555 
3556 			if (start)
3557 				ifsq_ifstart_try(ifsq, 1);
3558 		}
3559 		KKASSERT((stage->stg_flags &
3560 		    (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3561 	}
3562 
3563 	crit_exit();
3564 }
3565 
3566 static void
3567 ifnetinit(void *dummy __unused)
3568 {
3569 	int i;
3570 
3571 	/* XXX netisr_ncpus */
3572 	for (i = 0; i < ncpus; ++i)
3573 		TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3574 	netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3575 }
3576 
3577 void
3578 if_register_com_alloc(u_char type,
3579     if_com_alloc_t *a, if_com_free_t *f)
3580 {
3581 
3582         KASSERT(if_com_alloc[type] == NULL,
3583             ("if_register_com_alloc: %d already registered", type));
3584         KASSERT(if_com_free[type] == NULL,
3585             ("if_register_com_alloc: %d free already registered", type));
3586 
3587         if_com_alloc[type] = a;
3588         if_com_free[type] = f;
3589 }
3590 
3591 void
3592 if_deregister_com_alloc(u_char type)
3593 {
3594 
3595         KASSERT(if_com_alloc[type] != NULL,
3596             ("if_deregister_com_alloc: %d not registered", type));
3597         KASSERT(if_com_free[type] != NULL,
3598             ("if_deregister_com_alloc: %d free not registered", type));
3599         if_com_alloc[type] = NULL;
3600         if_com_free[type] = NULL;
3601 }
3602 
3603 void
3604 ifq_set_maxlen(struct ifaltq *ifq, int len)
3605 {
3606 	ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3607 }
3608 
3609 int
3610 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3611 {
3612 	return ALTQ_SUBQ_INDEX_DEFAULT;
3613 }
3614 
3615 int
3616 ifq_mapsubq_modulo(struct ifaltq *ifq, int cpuid)
3617 {
3618 
3619 	return (cpuid % ifq->altq_subq_mappriv);
3620 }
3621 
3622 /*
3623  * Watchdog timeout.  Process callback as appropriate.  If we cannot
3624  * serialize the ifnet just try again on the next timeout.
3625  *
3626  * NOTE: The ifnet can adjust wd_timer while holding the serializer.  We
3627  *	 can only safely adjust it under the same circumstances.
3628  */
3629 static void
3630 ifsq_watchdog(void *arg)
3631 {
3632 	struct ifsubq_watchdog *wd = arg;
3633 	struct ifnet *ifp;
3634 	int count;
3635 
3636 	/*
3637 	 * Fast track.  Try to avoid acquiring the serializer when not
3638 	 * near the terminal count, unless asked to.  If the atomic op
3639 	 * to decrement the count fails just retry on the next callout.
3640 	 */
3641 	count = wd->wd_timer;
3642 	cpu_ccfence();
3643 	if (count == 0)
3644 		goto done;
3645 	if (count > 2 && (wd->wd_flags & IF_WDOG_ALLTICKS) == 0) {
3646 		(void)atomic_cmpset_int(&wd->wd_timer, count, count - 1);
3647 		goto done;
3648 	}
3649 
3650 	/*
3651 	 * Obtain the serializer and then re-test all wd_timer conditions
3652 	 * as it may have changed.  NICs do not mess with wd_timer without
3653 	 * holding the serializer.
3654 	 *
3655 	 * If we are unable to obtain the serializer just retry the same
3656 	 * count on the next callout.
3657 	 *
3658 	 * - call watchdog in terminal count (0)
3659 	 * - call watchdog on last tick (1) if requested
3660 	 * - call watchdog on all ticks if requested
3661 	 */
3662 	ifp = ifsq_get_ifp(wd->wd_subq);
3663 	if (ifnet_tryserialize_all(ifp) == 0)
3664 		goto done;
3665 	if (atomic_cmpset_int(&wd->wd_timer, count, count - 1)) {
3666 		--count;
3667 		if (count == 0 ||
3668 		    (wd->wd_flags & IF_WDOG_ALLTICKS) ||
3669 		    ((wd->wd_flags & IF_WDOG_LASTTICK) && count == 1)) {
3670 			wd->wd_watchdog(wd->wd_subq);
3671 		}
3672 	}
3673 	ifnet_deserialize_all(ifp);
3674 done:
3675 	ifsq_watchdog_reset(wd);
3676 }
3677 
3678 static void
3679 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3680 {
3681 	callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3682 	    ifsq_get_cpuid(wd->wd_subq));
3683 }
3684 
3685 void
3686 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3687 		   ifsq_watchdog_t watchdog, int flags)
3688 {
3689 	callout_init_mp(&wd->wd_callout);
3690 	wd->wd_timer = 0;
3691 	wd->wd_flags = flags;
3692 	wd->wd_subq = ifsq;
3693 	wd->wd_watchdog = watchdog;
3694 }
3695 
3696 void
3697 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3698 {
3699 	atomic_swap_int(&wd->wd_timer, 0);
3700 	ifsq_watchdog_reset(wd);
3701 }
3702 
3703 void
3704 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3705 {
3706 	atomic_swap_int(&wd->wd_timer, 0);
3707 	callout_stop(&wd->wd_callout);
3708 }
3709 
3710 void
3711 ifsq_watchdog_set_count(struct ifsubq_watchdog *wd, int count)
3712 {
3713 	atomic_swap_int(&wd->wd_timer, count);
3714 }
3715 
3716 void
3717 ifnet_lock(void)
3718 {
3719 	KASSERT(curthread->td_type != TD_TYPE_NETISR,
3720 	    ("try holding ifnet lock in netisr"));
3721 	mtx_lock(&ifnet_mtx);
3722 }
3723 
3724 void
3725 ifnet_unlock(void)
3726 {
3727 	KASSERT(curthread->td_type != TD_TYPE_NETISR,
3728 	    ("try holding ifnet lock in netisr"));
3729 	mtx_unlock(&ifnet_mtx);
3730 }
3731 
3732 static struct ifnet_array *
3733 ifnet_array_alloc(int count)
3734 {
3735 	struct ifnet_array *arr;
3736 
3737 	arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]),
3738 	    M_IFNET, M_WAITOK);
3739 	arr->ifnet_count = count;
3740 
3741 	return arr;
3742 }
3743 
3744 static void
3745 ifnet_array_free(struct ifnet_array *arr)
3746 {
3747 	if (arr == &ifnet_array0)
3748 		return;
3749 	kfree(arr, M_IFNET);
3750 }
3751 
3752 static struct ifnet_array *
3753 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr)
3754 {
3755 	struct ifnet_array *arr;
3756 	int count, i;
3757 
3758 	KASSERT(old_arr->ifnet_count >= 0,
3759 	    ("invalid ifnet array count %d", old_arr->ifnet_count));
3760 	count = old_arr->ifnet_count + 1;
3761 	arr = ifnet_array_alloc(count);
3762 
3763 	/*
3764 	 * Save the old ifnet array and append this ifp to the end of
3765 	 * the new ifnet array.
3766 	 */
3767 	for (i = 0; i < old_arr->ifnet_count; ++i) {
3768 		KASSERT(old_arr->ifnet_arr[i] != ifp,
3769 		    ("%s is already in ifnet array", ifp->if_xname));
3770 		arr->ifnet_arr[i] = old_arr->ifnet_arr[i];
3771 	}
3772 	KASSERT(i == count - 1,
3773 	    ("add %s, ifnet array index mismatch, should be %d, but got %d",
3774 	     ifp->if_xname, count - 1, i));
3775 	arr->ifnet_arr[i] = ifp;
3776 
3777 	return arr;
3778 }
3779 
3780 static struct ifnet_array *
3781 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr)
3782 {
3783 	struct ifnet_array *arr;
3784 	int count, i, idx, found = 0;
3785 
3786 	KASSERT(old_arr->ifnet_count > 0,
3787 	    ("invalid ifnet array count %d", old_arr->ifnet_count));
3788 	count = old_arr->ifnet_count - 1;
3789 	arr = ifnet_array_alloc(count);
3790 
3791 	/*
3792 	 * Save the old ifnet array, but skip this ifp.
3793 	 */
3794 	idx = 0;
3795 	for (i = 0; i < old_arr->ifnet_count; ++i) {
3796 		if (old_arr->ifnet_arr[i] == ifp) {
3797 			KASSERT(!found,
3798 			    ("dup %s is in ifnet array", ifp->if_xname));
3799 			found = 1;
3800 			continue;
3801 		}
3802 		KASSERT(idx < count,
3803 		    ("invalid ifnet array index %d, count %d", idx, count));
3804 		arr->ifnet_arr[idx] = old_arr->ifnet_arr[i];
3805 		++idx;
3806 	}
3807 	KASSERT(found, ("%s is not in ifnet array", ifp->if_xname));
3808 	KASSERT(idx == count,
3809 	    ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3810 	     ifp->if_xname, count, idx));
3811 
3812 	return arr;
3813 }
3814 
3815 const struct ifnet_array *
3816 ifnet_array_get(void)
3817 {
3818 	const struct ifnet_array *ret;
3819 
3820 	KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3821 	ret = ifnet_array;
3822 	/* Make sure 'ret' is really used. */
3823 	cpu_ccfence();
3824 	return (ret);
3825 }
3826 
3827 int
3828 ifnet_array_isempty(void)
3829 {
3830 	KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3831 	if (ifnet_array->ifnet_count == 0)
3832 		return 1;
3833 	else
3834 		return 0;
3835 }
3836 
3837 void
3838 ifa_marker_init(struct ifaddr_marker *mark, struct ifnet *ifp)
3839 {
3840 	struct ifaddr *ifa;
3841 
3842 	memset(mark, 0, sizeof(*mark));
3843 	ifa = &mark->ifa;
3844 
3845 	mark->ifac.ifa = ifa;
3846 
3847 	ifa->ifa_addr = &mark->addr;
3848 	ifa->ifa_dstaddr = &mark->dstaddr;
3849 	ifa->ifa_netmask = &mark->netmask;
3850 	ifa->ifa_ifp = ifp;
3851 }
3852 
3853 static int
3854 if_ringcnt_fixup(int ring_cnt, int ring_cntmax)
3855 {
3856 
3857 	KASSERT(ring_cntmax > 0, ("invalid ring count max %d", ring_cntmax));
3858 
3859 	if (ring_cnt <= 0 || ring_cnt > ring_cntmax)
3860 		ring_cnt = ring_cntmax;
3861 	if (ring_cnt > netisr_ncpus)
3862 		ring_cnt = netisr_ncpus;
3863 	return (ring_cnt);
3864 }
3865 
3866 static void
3867 if_ringmap_set_grid(device_t dev, struct if_ringmap *rm, int grid)
3868 {
3869 	int i, offset;
3870 
3871 	KASSERT(grid > 0, ("invalid if_ringmap grid %d", grid));
3872 	KASSERT(grid >= rm->rm_cnt, ("invalid if_ringmap grid %d, count %d",
3873 	    grid, rm->rm_cnt));
3874 	rm->rm_grid = grid;
3875 
3876 	offset = (rm->rm_grid * device_get_unit(dev)) % netisr_ncpus;
3877 	for (i = 0; i < rm->rm_cnt; ++i) {
3878 		rm->rm_cpumap[i] = offset + i;
3879 		KASSERT(rm->rm_cpumap[i] < netisr_ncpus,
3880 		    ("invalid cpumap[%d] = %d, offset %d", i,
3881 		     rm->rm_cpumap[i], offset));
3882 	}
3883 }
3884 
3885 static struct if_ringmap *
3886 if_ringmap_alloc_flags(device_t dev, int ring_cnt, int ring_cntmax,
3887     uint32_t flags)
3888 {
3889 	struct if_ringmap *rm;
3890 	int i, grid = 0, prev_grid;
3891 
3892 	ring_cnt = if_ringcnt_fixup(ring_cnt, ring_cntmax);
3893 	rm = kmalloc(__offsetof(struct if_ringmap, rm_cpumap[ring_cnt]),
3894 	    M_DEVBUF, M_WAITOK | M_ZERO);
3895 
3896 	rm->rm_cnt = ring_cnt;
3897 	if (flags & RINGMAP_FLAG_POWEROF2)
3898 		rm->rm_cnt = 1 << (fls(rm->rm_cnt) - 1);
3899 
3900 	prev_grid = netisr_ncpus;
3901 	for (i = 0; i < netisr_ncpus; ++i) {
3902 		if (netisr_ncpus % (i + 1) != 0)
3903 			continue;
3904 
3905 		grid = netisr_ncpus / (i + 1);
3906 		if (rm->rm_cnt > grid) {
3907 			grid = prev_grid;
3908 			break;
3909 		}
3910 
3911 		if (rm->rm_cnt > netisr_ncpus / (i + 2))
3912 			break;
3913 		prev_grid = grid;
3914 	}
3915 	if_ringmap_set_grid(dev, rm, grid);
3916 
3917 	return (rm);
3918 }
3919 
3920 struct if_ringmap *
3921 if_ringmap_alloc(device_t dev, int ring_cnt, int ring_cntmax)
3922 {
3923 
3924 	return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3925 	    RINGMAP_FLAG_NONE));
3926 }
3927 
3928 struct if_ringmap *
3929 if_ringmap_alloc2(device_t dev, int ring_cnt, int ring_cntmax)
3930 {
3931 
3932 	return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3933 	    RINGMAP_FLAG_POWEROF2));
3934 }
3935 
3936 void
3937 if_ringmap_free(struct if_ringmap *rm)
3938 {
3939 
3940 	kfree(rm, M_DEVBUF);
3941 }
3942 
3943 /*
3944  * Align the two ringmaps.
3945  *
3946  * e.g. 8 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3947  *
3948  * Before:
3949  *
3950  * CPU      0  1  2  3   4  5  6  7
3951  * NIC_RX               n0 n1 n2 n3
3952  * NIC_TX        N0 N1
3953  *
3954  * After:
3955  *
3956  * CPU      0  1  2  3   4  5  6  7
3957  * NIC_RX               n0 n1 n2 n3
3958  * NIC_TX               N0 N1
3959  */
3960 void
3961 if_ringmap_align(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3962 {
3963 
3964 	if (rm0->rm_grid > rm1->rm_grid)
3965 		if_ringmap_set_grid(dev, rm1, rm0->rm_grid);
3966 	else if (rm0->rm_grid < rm1->rm_grid)
3967 		if_ringmap_set_grid(dev, rm0, rm1->rm_grid);
3968 }
3969 
3970 void
3971 if_ringmap_match(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3972 {
3973 	int subset_grid, cnt, divisor, mod, offset, i;
3974 	struct if_ringmap *subset_rm, *rm;
3975 	int old_rm0_grid, old_rm1_grid;
3976 
3977 	if (rm0->rm_grid == rm1->rm_grid)
3978 		return;
3979 
3980 	/* Save grid for later use */
3981 	old_rm0_grid = rm0->rm_grid;
3982 	old_rm1_grid = rm1->rm_grid;
3983 
3984 	if_ringmap_align(dev, rm0, rm1);
3985 
3986 	/*
3987 	 * Re-shuffle rings to get more even distribution.
3988 	 *
3989 	 * e.g. 12 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3990 	 *
3991 	 * CPU       0  1  2  3   4  5  6  7   8  9 10 11
3992 	 *
3993 	 * NIC_RX   a0 a1 a2 a3  b0 b1 b2 b3  c0 c1 c2 c3
3994 	 * NIC_TX   A0 A1        B0 B1        C0 C1
3995 	 *
3996 	 * NIC_RX   d0 d1 d2 d3  e0 e1 e2 e3  f0 f1 f2 f3
3997 	 * NIC_TX         D0 D1        E0 E1        F0 F1
3998 	 */
3999 
4000 	if (rm0->rm_cnt >= (2 * old_rm1_grid)) {
4001 		cnt = rm0->rm_cnt;
4002 		subset_grid = old_rm1_grid;
4003 		subset_rm = rm1;
4004 		rm = rm0;
4005 	} else if (rm1->rm_cnt > (2 * old_rm0_grid)) {
4006 		cnt = rm1->rm_cnt;
4007 		subset_grid = old_rm0_grid;
4008 		subset_rm = rm0;
4009 		rm = rm1;
4010 	} else {
4011 		/* No space to shuffle. */
4012 		return;
4013 	}
4014 
4015 	mod = cnt / subset_grid;
4016 	KKASSERT(mod >= 2);
4017 	divisor = netisr_ncpus / rm->rm_grid;
4018 	offset = ((device_get_unit(dev) / divisor) % mod) * subset_grid;
4019 
4020 	for (i = 0; i < subset_rm->rm_cnt; ++i) {
4021 		subset_rm->rm_cpumap[i] += offset;
4022 		KASSERT(subset_rm->rm_cpumap[i] < netisr_ncpus,
4023 		    ("match: invalid cpumap[%d] = %d, offset %d",
4024 		     i, subset_rm->rm_cpumap[i], offset));
4025 	}
4026 #ifdef INVARIANTS
4027 	for (i = 0; i < subset_rm->rm_cnt; ++i) {
4028 		int j;
4029 
4030 		for (j = 0; j < rm->rm_cnt; ++j) {
4031 			if (rm->rm_cpumap[j] == subset_rm->rm_cpumap[i])
4032 				break;
4033 		}
4034 		KASSERT(j < rm->rm_cnt,
4035 		    ("subset cpumap[%d] = %d not found in superset",
4036 		     i, subset_rm->rm_cpumap[i]));
4037 	}
4038 #endif
4039 }
4040 
4041 int
4042 if_ringmap_count(const struct if_ringmap *rm)
4043 {
4044 
4045 	return (rm->rm_cnt);
4046 }
4047 
4048 int
4049 if_ringmap_cpumap(const struct if_ringmap *rm, int ring)
4050 {
4051 
4052 	KASSERT(ring >= 0 && ring < rm->rm_cnt, ("invalid ring %d", ring));
4053 	return (rm->rm_cpumap[ring]);
4054 }
4055 
4056 void
4057 if_ringmap_rdrtable(const struct if_ringmap *rm, int table[], int table_nent)
4058 {
4059 	int i, grid_idx, grid_cnt, patch_off, patch_cnt, ncopy;
4060 
4061 	KASSERT(table_nent > 0 && (table_nent & NETISR_CPUMASK) == 0,
4062 	    ("invalid redirect table entries %d", table_nent));
4063 
4064 	grid_idx = 0;
4065 	for (i = 0; i < NETISR_CPUMAX; ++i) {
4066 		table[i] = grid_idx++ % rm->rm_cnt;
4067 
4068 		if (grid_idx == rm->rm_grid)
4069 			grid_idx = 0;
4070 	}
4071 
4072 	/*
4073 	 * Make the ring distributed more evenly for the remainder
4074 	 * of each grid.
4075 	 *
4076 	 * e.g. 12 netisrs, rm contains 8 rings.
4077 	 *
4078 	 * Redirect table before:
4079 	 *
4080 	 *  0  1  2  3  4  5  6  7  0  1  2  3  0  1  2  3
4081 	 *  4  5  6  7  0  1  2  3  0  1  2  3  4  5  6  7
4082 	 *  0  1  2  3  0  1  2  3  4  5  6  7  0  1  2  3
4083 	 *  ....
4084 	 *
4085 	 * Redirect table after being patched (pX, patched entries):
4086 	 *
4087 	 *  0  1  2  3  4  5  6  7 p0 p1 p2 p3  0  1  2  3
4088 	 *  4  5  6  7 p4 p5 p6 p7  0  1  2  3  4  5  6  7
4089 	 * p0 p1 p2 p3  0  1  2  3  4  5  6  7 p4 p5 p6 p7
4090 	 *  ....
4091 	 */
4092 	patch_cnt = rm->rm_grid % rm->rm_cnt;
4093 	if (patch_cnt == 0)
4094 		goto done;
4095 	patch_off = rm->rm_grid - (rm->rm_grid % rm->rm_cnt);
4096 
4097 	grid_cnt = roundup(NETISR_CPUMAX, rm->rm_grid) / rm->rm_grid;
4098 	grid_idx = 0;
4099 	for (i = 0; i < grid_cnt; ++i) {
4100 		int j;
4101 
4102 		for (j = 0; j < patch_cnt; ++j) {
4103 			int fix_idx;
4104 
4105 			fix_idx = (i * rm->rm_grid) + patch_off + j;
4106 			if (fix_idx >= NETISR_CPUMAX)
4107 				goto done;
4108 			table[fix_idx] = grid_idx++ % rm->rm_cnt;
4109 		}
4110 	}
4111 done:
4112 	/*
4113 	 * If the device supports larger redirect table, duplicate
4114 	 * the first NETISR_CPUMAX entries to the rest of the table,
4115 	 * so that it matches upper layer's expectation:
4116 	 * (hash & NETISR_CPUMASK) % netisr_ncpus
4117 	 */
4118 	ncopy = table_nent / NETISR_CPUMAX;
4119 	for (i = 1; i < ncopy; ++i) {
4120 		memcpy(&table[i * NETISR_CPUMAX], table,
4121 		    NETISR_CPUMAX * sizeof(table[0]));
4122 	}
4123 	if (if_ringmap_dumprdr) {
4124 		for (i = 0; i < table_nent; ++i) {
4125 			if (i != 0 && i % 16 == 0)
4126 				kprintf("\n");
4127 			kprintf("%03d ", table[i]);
4128 		}
4129 		kprintf("\n");
4130 	}
4131 }
4132 
4133 int
4134 if_ringmap_cpumap_sysctl(SYSCTL_HANDLER_ARGS)
4135 {
4136 	struct if_ringmap *rm = arg1;
4137 	int i, error = 0;
4138 
4139 	for (i = 0; i < rm->rm_cnt; ++i) {
4140 		int cpu = rm->rm_cpumap[i];
4141 
4142 		error = SYSCTL_OUT(req, &cpu, sizeof(cpu));
4143 		if (error)
4144 			break;
4145 	}
4146 	return (error);
4147 }
4148