xref: /dragonfly/sys/net/if.c (revision 0a9108eb)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if.c	8.3 (Berkeley) 1/4/94
34  * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
35  * $DragonFly: src/sys/net/if.c,v 1.58 2007/12/29 12:51:17 sephe Exp $
36  */
37 
38 #include "opt_compat.h"
39 #include "opt_inet6.h"
40 #include "opt_inet.h"
41 #include "opt_polling.h"
42 
43 #include <sys/param.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/socketops.h>
52 #include <sys/protosw.h>
53 #include <sys/kernel.h>
54 #include <sys/sockio.h>
55 #include <sys/syslog.h>
56 #include <sys/sysctl.h>
57 #include <sys/domain.h>
58 #include <sys/thread.h>
59 #include <sys/serialize.h>
60 
61 #include <net/if.h>
62 #include <net/if_arp.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/if_var.h>
66 #include <net/ifq_var.h>
67 #include <net/radix.h>
68 #include <net/route.h>
69 #include <machine/stdarg.h>
70 
71 #include <sys/thread2.h>
72 
73 #if defined(INET) || defined(INET6)
74 /*XXX*/
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
77 #include <netinet/if_ether.h>
78 #ifdef INET6
79 #include <netinet6/in6_var.h>
80 #include <netinet6/in6_ifattach.h>
81 #endif
82 #endif
83 
84 #if defined(COMPAT_43)
85 #include <emulation/43bsd/43bsd_socket.h>
86 #endif /* COMPAT_43 */
87 
88 /*
89  * Support for non-ALTQ interfaces.
90  */
91 static int	ifq_classic_enqueue(struct ifaltq *, struct mbuf *,
92 				    struct altq_pktattr *);
93 static struct mbuf *
94 		ifq_classic_dequeue(struct ifaltq *, struct mbuf *, int);
95 static int	ifq_classic_request(struct ifaltq *, int, void *);
96 
97 /*
98  * System initialization
99  */
100 static void	if_attachdomain(void *);
101 static void	if_attachdomain1(struct ifnet *);
102 static int	ifconf(u_long, caddr_t, struct ucred *);
103 static void	ifinit(void *);
104 static void	if_slowtimo(void *);
105 static void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
106 static int	if_rtdel(struct radix_node *, void *);
107 
108 #ifdef INET6
109 /*
110  * XXX: declare here to avoid to include many inet6 related files..
111  * should be more generalized?
112  */
113 extern void	nd6_setmtu(struct ifnet *);
114 #endif
115 
116 struct if_clone	*if_clone_lookup(const char *, int *);
117 int		if_clone_list(struct if_clonereq *);
118 
119 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
120 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
121 
122 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
123 
124 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
125 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
126 MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
127 
128 int			ifqmaxlen = IFQ_MAXLEN;
129 struct ifnethead	ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
130 
131 LIST_HEAD(, if_clone)	if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
132 int			if_cloners_count;
133 
134 struct callout		if_slowtimo_timer;
135 
136 int			if_index = 0;
137 struct ifnet		**ifindex2ifnet = NULL;
138 
139 /*
140  * Network interface utility routines.
141  *
142  * Routines with ifa_ifwith* names take sockaddr *'s as
143  * parameters.
144  */
145 /* ARGSUSED*/
146 void
147 ifinit(void *dummy)
148 {
149 	struct ifnet *ifp;
150 
151 	callout_init(&if_slowtimo_timer);
152 
153 	crit_enter();
154 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
155 		if (ifp->if_snd.ifq_maxlen == 0) {
156 			if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
157 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
158 		}
159 	}
160 	crit_exit();
161 
162 	if_slowtimo(0);
163 }
164 
165 /*
166  * Attach an interface to the list of "active" interfaces.
167  *
168  * The serializer is optional.  If non-NULL access to the interface
169  * may be MPSAFE.
170  */
171 void
172 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
173 {
174 	unsigned socksize, ifasize;
175 	int namelen, masklen;
176 	struct sockaddr_dl *sdl;
177 	struct ifaddr *ifa;
178 	struct ifaltq *ifq;
179 
180 	static int if_indexlim = 8;
181 
182 	/*
183 	 * The serializer can be passed in from the device, allowing the
184 	 * same serializer to be used for both the interrupt interlock and
185 	 * the device queue.  If not specified, the netif structure will
186 	 * use an embedded serializer.
187 	 */
188 	if (serializer == NULL) {
189 		serializer = &ifp->if_default_serializer;
190 		lwkt_serialize_init(serializer);
191 	}
192 	ifp->if_serializer = serializer;
193 
194 #ifdef DEVICE_POLLING
195 	/* Device is not in polling mode by default */
196 	ifp->if_poll_cpuid = -1;
197 #endif
198 
199 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
200 	ifp->if_index = ++if_index;
201 	/*
202 	 * XXX -
203 	 * The old code would work if the interface passed a pre-existing
204 	 * chain of ifaddrs to this code.  We don't trust our callers to
205 	 * properly initialize the tailq, however, so we no longer allow
206 	 * this unlikely case.
207 	 */
208 	TAILQ_INIT(&ifp->if_addrhead);
209 	TAILQ_INIT(&ifp->if_prefixhead);
210 	LIST_INIT(&ifp->if_multiaddrs);
211 	getmicrotime(&ifp->if_lastchange);
212 	if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
213 		unsigned int n;
214 		struct ifnet **q;
215 
216 		if_indexlim <<= 1;
217 
218 		/* grow ifindex2ifnet */
219 		n = if_indexlim * sizeof(*q);
220 		q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
221 		if (ifindex2ifnet) {
222 			bcopy(ifindex2ifnet, q, n/2);
223 			kfree(ifindex2ifnet, M_IFADDR);
224 		}
225 		ifindex2ifnet = q;
226 	}
227 
228 	ifindex2ifnet[if_index] = ifp;
229 
230 	/*
231 	 * create a Link Level name for this device
232 	 */
233 	namelen = strlen(ifp->if_xname);
234 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
235 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
236 	socksize = masklen + ifp->if_addrlen;
237 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
238 	if (socksize < sizeof(*sdl))
239 		socksize = sizeof(*sdl);
240 	socksize = ROUNDUP(socksize);
241 	ifasize = sizeof(struct ifaddr) + 2 * socksize;
242 	ifa = kmalloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
243 	sdl = (struct sockaddr_dl *)(ifa + 1);
244 	sdl->sdl_len = socksize;
245 	sdl->sdl_family = AF_LINK;
246 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
247 	sdl->sdl_nlen = namelen;
248 	sdl->sdl_index = ifp->if_index;
249 	sdl->sdl_type = ifp->if_type;
250 	ifp->if_lladdr = ifa;
251 	ifa->ifa_ifp = ifp;
252 	ifa->ifa_rtrequest = link_rtrequest;
253 	ifa->ifa_addr = (struct sockaddr *)sdl;
254 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
255 	ifa->ifa_netmask = (struct sockaddr *)sdl;
256 	sdl->sdl_len = masklen;
257 	while (namelen != 0)
258 		sdl->sdl_data[--namelen] = 0xff;
259 	TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
260 
261 	EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
262 
263 	ifq = &ifp->if_snd;
264 	ifq->altq_type = 0;
265 	ifq->altq_disc = NULL;
266 	ifq->altq_flags &= ALTQF_CANTCHANGE;
267 	ifq->altq_tbr = NULL;
268 	ifq->altq_ifp = ifp;
269 	ifq_set_classic(ifq);
270 
271 	if (!SLIST_EMPTY(&domains))
272 		if_attachdomain1(ifp);
273 
274 	/* Announce the interface. */
275 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
276 }
277 
278 static void
279 if_attachdomain(void *dummy)
280 {
281 	struct ifnet *ifp;
282 
283 	crit_enter();
284 	TAILQ_FOREACH(ifp, &ifnet, if_list)
285 		if_attachdomain1(ifp);
286 	crit_exit();
287 }
288 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
289 	if_attachdomain, NULL);
290 
291 static void
292 if_attachdomain1(struct ifnet *ifp)
293 {
294 	struct domain *dp;
295 
296 	crit_enter();
297 
298 	/* address family dependent data region */
299 	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
300 	SLIST_FOREACH(dp, &domains, dom_next)
301 		if (dp->dom_ifattach)
302 			ifp->if_afdata[dp->dom_family] =
303 				(*dp->dom_ifattach)(ifp);
304 	crit_exit();
305 }
306 
307 /*
308  * Detach an interface, removing it from the
309  * list of "active" interfaces.
310  */
311 void
312 if_detach(struct ifnet *ifp)
313 {
314 	struct ifaddr *ifa;
315 	struct radix_node_head	*rnh;
316 	int i;
317 	int cpu, origcpu;
318 	struct domain *dp;
319 
320 	EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
321 
322 	/*
323 	 * Remove routes and flush queues.
324 	 */
325 	crit_enter();
326 #ifdef DEVICE_POLLING
327 	if (ifp->if_flags & IFF_POLLING)
328 		ether_poll_deregister(ifp);
329 #endif
330 	if_down(ifp);
331 
332 	if (ifq_is_enabled(&ifp->if_snd))
333 		altq_disable(&ifp->if_snd);
334 	if (ifq_is_attached(&ifp->if_snd))
335 		altq_detach(&ifp->if_snd);
336 
337 	/*
338 	 * Clean up all addresses.
339 	 */
340 	ifp->if_lladdr = NULL;
341 
342 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
343 	     ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
344 #ifdef INET
345 		/* XXX: Ugly!! ad hoc just for INET */
346 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
347 			struct ifaliasreq ifr;
348 
349 			bzero(&ifr, sizeof ifr);
350 			ifr.ifra_addr = *ifa->ifa_addr;
351 			if (ifa->ifa_dstaddr)
352 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
353 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
354 				       NULL) == 0)
355 				continue;
356 		}
357 #endif /* INET */
358 #ifdef INET6
359 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
360 			in6_purgeaddr(ifa);
361 			/* ifp_addrhead is already updated */
362 			continue;
363 		}
364 #endif /* INET6 */
365 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
366 		IFAFREE(ifa);
367 	}
368 
369 #ifdef INET
370 	/*
371 	 * Remove all IPv4 kernel structures related to ifp.
372 	 */
373 	in_ifdetach(ifp);
374 #endif
375 
376 #ifdef INET6
377 	/*
378 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
379 	 * before removing routing entries below, since IPv6 interface direct
380 	 * routes are expected to be removed by the IPv6-specific kernel API.
381 	 * Otherwise, the kernel will detect some inconsistency and bark it.
382 	 */
383 	in6_ifdetach(ifp);
384 #endif
385 
386 	/*
387 	 * Delete all remaining routes using this interface
388 	 * Unfortuneatly the only way to do this is to slog through
389 	 * the entire routing table looking for routes which point
390 	 * to this interface...oh well...
391 	 */
392 	origcpu = mycpuid;
393 	for (cpu = 0; cpu < ncpus2; cpu++) {
394 		lwkt_migratecpu(cpu);
395 		for (i = 1; i <= AF_MAX; i++) {
396 			if ((rnh = rt_tables[mycpuid][i]) == NULL)
397 				continue;
398 			rnh->rnh_walktree(rnh, if_rtdel, ifp);
399 		}
400 	}
401 	lwkt_migratecpu(origcpu);
402 
403 	/* Announce that the interface is gone. */
404 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
405 
406 	SLIST_FOREACH(dp, &domains, dom_next)
407 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
408 			(*dp->dom_ifdetach)(ifp,
409 				ifp->if_afdata[dp->dom_family]);
410 
411 	/*
412 	 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
413 	 */
414 	ifindex2ifnet[ifp->if_index] = NULL;
415 	while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
416 		if_index--;
417 
418 	TAILQ_REMOVE(&ifnet, ifp, if_link);
419 	crit_exit();
420 }
421 
422 /*
423  * Delete Routes for a Network Interface
424  *
425  * Called for each routing entry via the rnh->rnh_walktree() call above
426  * to delete all route entries referencing a detaching network interface.
427  *
428  * Arguments:
429  *	rn	pointer to node in the routing table
430  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
431  *
432  * Returns:
433  *	0	successful
434  *	errno	failed - reason indicated
435  *
436  */
437 static int
438 if_rtdel(struct radix_node *rn, void *arg)
439 {
440 	struct rtentry	*rt = (struct rtentry *)rn;
441 	struct ifnet	*ifp = arg;
442 	int		err;
443 
444 	if (rt->rt_ifp == ifp) {
445 
446 		/*
447 		 * Protect (sorta) against walktree recursion problems
448 		 * with cloned routes
449 		 */
450 		if (!(rt->rt_flags & RTF_UP))
451 			return (0);
452 
453 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
454 				rt_mask(rt), rt->rt_flags,
455 				(struct rtentry **) NULL);
456 		if (err) {
457 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
458 		}
459 	}
460 
461 	return (0);
462 }
463 
464 /*
465  * Create a clone network interface.
466  */
467 int
468 if_clone_create(char *name, int len)
469 {
470 	struct if_clone *ifc;
471 	char *dp;
472 	int wildcard, bytoff, bitoff;
473 	int unit;
474 	int err;
475 
476 	ifc = if_clone_lookup(name, &unit);
477 	if (ifc == NULL)
478 		return (EINVAL);
479 
480 	if (ifunit(name) != NULL)
481 		return (EEXIST);
482 
483 	bytoff = bitoff = 0;
484 	wildcard = (unit < 0);
485 	/*
486 	 * Find a free unit if none was given.
487 	 */
488 	if (wildcard) {
489 		while (bytoff < ifc->ifc_bmlen &&
490 		    ifc->ifc_units[bytoff] == 0xff)
491 			bytoff++;
492 		if (bytoff >= ifc->ifc_bmlen)
493 			return (ENOSPC);
494 		while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
495 			bitoff++;
496 		unit = (bytoff << 3) + bitoff;
497 	}
498 
499 	if (unit > ifc->ifc_maxunit)
500 		return (ENXIO);
501 
502 	err = (*ifc->ifc_create)(ifc, unit);
503 	if (err != 0)
504 		return (err);
505 
506 	if (!wildcard) {
507 		bytoff = unit >> 3;
508 		bitoff = unit - (bytoff << 3);
509 	}
510 
511 	/*
512 	 * Allocate the unit in the bitmap.
513 	 */
514 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
515 	    ("%s: bit is already set", __func__));
516 	ifc->ifc_units[bytoff] |= (1 << bitoff);
517 
518 	/* In the wildcard case, we need to update the name. */
519 	if (wildcard) {
520 		for (dp = name; *dp != '\0'; dp++);
521 		if (ksnprintf(dp, len - (dp-name), "%d", unit) >
522 		    len - (dp-name) - 1) {
523 			/*
524 			 * This can only be a programmer error and
525 			 * there's no straightforward way to recover if
526 			 * it happens.
527 			 */
528 			panic("if_clone_create(): interface name too long");
529 		}
530 
531 	}
532 
533 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
534 
535 	return (0);
536 }
537 
538 /*
539  * Destroy a clone network interface.
540  */
541 int
542 if_clone_destroy(const char *name)
543 {
544 	struct if_clone *ifc;
545 	struct ifnet *ifp;
546 	int bytoff, bitoff;
547 	int unit;
548 
549 	ifc = if_clone_lookup(name, &unit);
550 	if (ifc == NULL)
551 		return (EINVAL);
552 
553 	if (unit < ifc->ifc_minifs)
554 		return (EINVAL);
555 
556 	ifp = ifunit(name);
557 	if (ifp == NULL)
558 		return (ENXIO);
559 
560 	if (ifc->ifc_destroy == NULL)
561 		return (EOPNOTSUPP);
562 
563 	(*ifc->ifc_destroy)(ifp);
564 
565 	/*
566 	 * Compute offset in the bitmap and deallocate the unit.
567 	 */
568 	bytoff = unit >> 3;
569 	bitoff = unit - (bytoff << 3);
570 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
571 	    ("%s: bit is already cleared", __func__));
572 	ifc->ifc_units[bytoff] &= ~(1 << bitoff);
573 	return (0);
574 }
575 
576 /*
577  * Look up a network interface cloner.
578  */
579 struct if_clone *
580 if_clone_lookup(const char *name, int *unitp)
581 {
582 	struct if_clone *ifc;
583 	const char *cp;
584 	int i;
585 
586 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
587 		for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
588 			if (ifc->ifc_name[i] != *cp)
589 				goto next_ifc;
590 		}
591 		goto found_name;
592  next_ifc:
593 		ifc = LIST_NEXT(ifc, ifc_list);
594 	}
595 
596 	/* No match. */
597 	return ((struct if_clone *)NULL);
598 
599  found_name:
600 	if (*cp == '\0') {
601 		i = -1;
602 	} else {
603 		for (i = 0; *cp != '\0'; cp++) {
604 			if (*cp < '0' || *cp > '9') {
605 				/* Bogus unit number. */
606 				return (NULL);
607 			}
608 			i = (i * 10) + (*cp - '0');
609 		}
610 	}
611 
612 	if (unitp != NULL)
613 		*unitp = i;
614 	return (ifc);
615 }
616 
617 /*
618  * Register a network interface cloner.
619  */
620 void
621 if_clone_attach(struct if_clone *ifc)
622 {
623 	int bytoff, bitoff;
624 	int err;
625 	int len, maxclone;
626 	int unit;
627 
628 	KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
629 	    ("%s: %s requested more units then allowed (%d > %d)",
630 	    __func__, ifc->ifc_name, ifc->ifc_minifs,
631 	    ifc->ifc_maxunit + 1));
632 	/*
633 	 * Compute bitmap size and allocate it.
634 	 */
635 	maxclone = ifc->ifc_maxunit + 1;
636 	len = maxclone >> 3;
637 	if ((len << 3) < maxclone)
638 		len++;
639 	ifc->ifc_units = kmalloc(len, M_CLONE, M_WAITOK | M_ZERO);
640 	ifc->ifc_bmlen = len;
641 
642 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
643 	if_cloners_count++;
644 
645 	for (unit = 0; unit < ifc->ifc_minifs; unit++) {
646 		err = (*ifc->ifc_create)(ifc, unit);
647 		KASSERT(err == 0,
648 		    ("%s: failed to create required interface %s%d",
649 		    __func__, ifc->ifc_name, unit));
650 
651 		/* Allocate the unit in the bitmap. */
652 		bytoff = unit >> 3;
653 		bitoff = unit - (bytoff << 3);
654 		ifc->ifc_units[bytoff] |= (1 << bitoff);
655 	}
656 }
657 
658 /*
659  * Unregister a network interface cloner.
660  */
661 void
662 if_clone_detach(struct if_clone *ifc)
663 {
664 
665 	LIST_REMOVE(ifc, ifc_list);
666 	kfree(ifc->ifc_units, M_CLONE);
667 	if_cloners_count--;
668 }
669 
670 /*
671  * Provide list of interface cloners to userspace.
672  */
673 int
674 if_clone_list(struct if_clonereq *ifcr)
675 {
676 	char outbuf[IFNAMSIZ], *dst;
677 	struct if_clone *ifc;
678 	int count, error = 0;
679 
680 	ifcr->ifcr_total = if_cloners_count;
681 	if ((dst = ifcr->ifcr_buffer) == NULL) {
682 		/* Just asking how many there are. */
683 		return (0);
684 	}
685 
686 	if (ifcr->ifcr_count < 0)
687 		return (EINVAL);
688 
689 	count = (if_cloners_count < ifcr->ifcr_count) ?
690 	    if_cloners_count : ifcr->ifcr_count;
691 
692 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
693 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
694 		strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
695 		error = copyout(outbuf, dst, IFNAMSIZ);
696 		if (error)
697 			break;
698 	}
699 
700 	return (error);
701 }
702 
703 /*
704  * Locate an interface based on a complete address.
705  */
706 struct ifaddr *
707 ifa_ifwithaddr(struct sockaddr *addr)
708 {
709 	struct ifnet *ifp;
710 	struct ifaddr *ifa;
711 
712 	TAILQ_FOREACH(ifp, &ifnet, if_link)
713 	    TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
714 		if (ifa->ifa_addr->sa_family != addr->sa_family)
715 			continue;
716 		if (sa_equal(addr, ifa->ifa_addr))
717 			return (ifa);
718 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
719 		    /* IPv6 doesn't have broadcast */
720 		    ifa->ifa_broadaddr->sa_len != 0 &&
721 		    sa_equal(ifa->ifa_broadaddr, addr))
722 			return (ifa);
723 	}
724 	return ((struct ifaddr *)NULL);
725 }
726 /*
727  * Locate the point to point interface with a given destination address.
728  */
729 struct ifaddr *
730 ifa_ifwithdstaddr(struct sockaddr *addr)
731 {
732 	struct ifnet *ifp;
733 	struct ifaddr *ifa;
734 
735 	TAILQ_FOREACH(ifp, &ifnet, if_link)
736 	    if (ifp->if_flags & IFF_POINTOPOINT)
737 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
738 			if (ifa->ifa_addr->sa_family != addr->sa_family)
739 				continue;
740 			if (ifa->ifa_dstaddr &&
741 			    sa_equal(addr, ifa->ifa_dstaddr))
742 				return (ifa);
743 	}
744 	return ((struct ifaddr *)NULL);
745 }
746 
747 /*
748  * Find an interface on a specific network.  If many, choice
749  * is most specific found.
750  */
751 struct ifaddr *
752 ifa_ifwithnet(struct sockaddr *addr)
753 {
754 	struct ifnet *ifp;
755 	struct ifaddr *ifa;
756 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
757 	u_int af = addr->sa_family;
758 	char *addr_data = addr->sa_data, *cplim;
759 
760 	/*
761 	 * AF_LINK addresses can be looked up directly by their index number,
762 	 * so do that if we can.
763 	 */
764 	if (af == AF_LINK) {
765 	    struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
766 
767 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
768 		return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
769 	}
770 
771 	/*
772 	 * Scan though each interface, looking for ones that have
773 	 * addresses in this address family.
774 	 */
775 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
776 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
777 			char *cp, *cp2, *cp3;
778 
779 			if (ifa->ifa_addr->sa_family != af)
780 next:				continue;
781 			if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
782 				/*
783 				 * This is a bit broken as it doesn't
784 				 * take into account that the remote end may
785 				 * be a single node in the network we are
786 				 * looking for.
787 				 * The trouble is that we don't know the
788 				 * netmask for the remote end.
789 				 */
790 				if (ifa->ifa_dstaddr != NULL &&
791 				    sa_equal(addr, ifa->ifa_dstaddr))
792 					return (ifa);
793 			} else {
794 				/*
795 				 * if we have a special address handler,
796 				 * then use it instead of the generic one.
797 				 */
798 				if (ifa->ifa_claim_addr) {
799 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
800 						return (ifa);
801 					} else {
802 						continue;
803 					}
804 				}
805 
806 				/*
807 				 * Scan all the bits in the ifa's address.
808 				 * If a bit dissagrees with what we are
809 				 * looking for, mask it with the netmask
810 				 * to see if it really matters.
811 				 * (A byte at a time)
812 				 */
813 				if (ifa->ifa_netmask == 0)
814 					continue;
815 				cp = addr_data;
816 				cp2 = ifa->ifa_addr->sa_data;
817 				cp3 = ifa->ifa_netmask->sa_data;
818 				cplim = ifa->ifa_netmask->sa_len +
819 					(char *)ifa->ifa_netmask;
820 				while (cp3 < cplim)
821 					if ((*cp++ ^ *cp2++) & *cp3++)
822 						goto next; /* next address! */
823 				/*
824 				 * If the netmask of what we just found
825 				 * is more specific than what we had before
826 				 * (if we had one) then remember the new one
827 				 * before continuing to search
828 				 * for an even better one.
829 				 */
830 				if (ifa_maybe == 0 ||
831 				    rn_refines((char *)ifa->ifa_netmask,
832 					       (char *)ifa_maybe->ifa_netmask))
833 					ifa_maybe = ifa;
834 			}
835 		}
836 	}
837 	return (ifa_maybe);
838 }
839 
840 /*
841  * Find an interface address specific to an interface best matching
842  * a given address.
843  */
844 struct ifaddr *
845 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
846 {
847 	struct ifaddr *ifa;
848 	char *cp, *cp2, *cp3;
849 	char *cplim;
850 	struct ifaddr *ifa_maybe = 0;
851 	u_int af = addr->sa_family;
852 
853 	if (af >= AF_MAX)
854 		return (0);
855 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
856 		if (ifa->ifa_addr->sa_family != af)
857 			continue;
858 		if (ifa_maybe == 0)
859 			ifa_maybe = ifa;
860 		if (ifa->ifa_netmask == NULL) {
861 			if (sa_equal(addr, ifa->ifa_addr) ||
862 			    (ifa->ifa_dstaddr != NULL &&
863 			     sa_equal(addr, ifa->ifa_dstaddr)))
864 				return (ifa);
865 			continue;
866 		}
867 		if (ifp->if_flags & IFF_POINTOPOINT) {
868 			if (sa_equal(addr, ifa->ifa_dstaddr))
869 				return (ifa);
870 		} else {
871 			cp = addr->sa_data;
872 			cp2 = ifa->ifa_addr->sa_data;
873 			cp3 = ifa->ifa_netmask->sa_data;
874 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
875 			for (; cp3 < cplim; cp3++)
876 				if ((*cp++ ^ *cp2++) & *cp3)
877 					break;
878 			if (cp3 == cplim)
879 				return (ifa);
880 		}
881 	}
882 	return (ifa_maybe);
883 }
884 
885 /*
886  * Default action when installing a route with a Link Level gateway.
887  * Lookup an appropriate real ifa to point to.
888  * This should be moved to /sys/net/link.c eventually.
889  */
890 static void
891 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
892 {
893 	struct ifaddr *ifa;
894 	struct sockaddr *dst;
895 	struct ifnet *ifp;
896 
897 	if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
898 	    (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
899 		return;
900 	ifa = ifaof_ifpforaddr(dst, ifp);
901 	if (ifa != NULL) {
902 		IFAFREE(rt->rt_ifa);
903 		IFAREF(ifa);
904 		rt->rt_ifa = ifa;
905 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
906 			ifa->ifa_rtrequest(cmd, rt, info);
907 	}
908 }
909 
910 /*
911  * Mark an interface down and notify protocols of
912  * the transition.
913  * NOTE: must be called at splnet or eqivalent.
914  */
915 void
916 if_unroute(struct ifnet *ifp, int flag, int fam)
917 {
918 	struct ifaddr *ifa;
919 
920 	ifp->if_flags &= ~flag;
921 	getmicrotime(&ifp->if_lastchange);
922 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
923 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
924 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
925 	ifq_purge(&ifp->if_snd);
926 	rt_ifmsg(ifp);
927 }
928 
929 /*
930  * Mark an interface up and notify protocols of
931  * the transition.
932  * NOTE: must be called at splnet or eqivalent.
933  */
934 void
935 if_route(struct ifnet *ifp, int flag, int fam)
936 {
937 	struct ifaddr *ifa;
938 
939 	ifp->if_flags |= flag;
940 	getmicrotime(&ifp->if_lastchange);
941 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
942 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
943 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
944 	rt_ifmsg(ifp);
945 #ifdef INET6
946 	in6_if_up(ifp);
947 #endif
948 }
949 
950 /*
951  * Mark an interface down and notify protocols of the transition.  An
952  * interface going down is also considered to be a synchronizing event.
953  * We must ensure that all packet processing related to the interface
954  * has completed before we return so e.g. the caller can free the ifnet
955  * structure that the mbufs may be referencing.
956  *
957  * NOTE: must be called at splnet or eqivalent.
958  */
959 void
960 if_down(struct ifnet *ifp)
961 {
962 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
963 	netmsg_service_sync();
964 }
965 
966 /*
967  * Mark an interface up and notify protocols of
968  * the transition.
969  * NOTE: must be called at splnet or eqivalent.
970  */
971 void
972 if_up(struct ifnet *ifp)
973 {
974 
975 	if_route(ifp, IFF_UP, AF_UNSPEC);
976 }
977 
978 /*
979  * Process a link state change.
980  * NOTE: must be called at splsoftnet or equivalent.
981  */
982 void
983 if_link_state_change(struct ifnet *ifp)
984 {
985 	rt_ifmsg(ifp);
986 }
987 
988 /*
989  * Handle interface watchdog timer routines.  Called
990  * from softclock, we decrement timers (if set) and
991  * call the appropriate interface routine on expiration.
992  */
993 static void
994 if_slowtimo(void *arg)
995 {
996 	struct ifnet *ifp;
997 
998 	crit_enter();
999 
1000 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1001 		if (ifp->if_timer == 0 || --ifp->if_timer)
1002 			continue;
1003 		if (ifp->if_watchdog) {
1004 			if (lwkt_serialize_try(ifp->if_serializer)) {
1005 				(*ifp->if_watchdog)(ifp);
1006 				lwkt_serialize_exit(ifp->if_serializer);
1007 			} else {
1008 				/* try again next timeout */
1009 				++ifp->if_timer;
1010 			}
1011 		}
1012 	}
1013 
1014 	crit_exit();
1015 
1016 	callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1017 }
1018 
1019 /*
1020  * Map interface name to
1021  * interface structure pointer.
1022  */
1023 struct ifnet *
1024 ifunit(const char *name)
1025 {
1026 	struct ifnet *ifp;
1027 
1028 	/*
1029 	 * Search all the interfaces for this name/number
1030 	 */
1031 
1032 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1033 		if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1034 			break;
1035 	}
1036 	return (ifp);
1037 }
1038 
1039 
1040 /*
1041  * Map interface name in a sockaddr_dl to
1042  * interface structure pointer.
1043  */
1044 struct ifnet *
1045 if_withname(struct sockaddr *sa)
1046 {
1047 	char ifname[IFNAMSIZ+1];
1048 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1049 
1050 	if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1051 	     (sdl->sdl_nlen > IFNAMSIZ) )
1052 		return NULL;
1053 
1054 	/*
1055 	 * ifunit wants a null-terminated name.  It may not be null-terminated
1056 	 * in the sockaddr.  We don't want to change the caller's sockaddr,
1057 	 * and there might not be room to put the trailing null anyway, so we
1058 	 * make a local copy that we know we can null terminate safely.
1059 	 */
1060 
1061 	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1062 	ifname[sdl->sdl_nlen] = '\0';
1063 	return ifunit(ifname);
1064 }
1065 
1066 
1067 /*
1068  * Interface ioctls.
1069  */
1070 int
1071 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1072 {
1073 	struct ifnet *ifp;
1074 	struct ifreq *ifr;
1075 	struct ifstat *ifs;
1076 	int error;
1077 	short oif_flags;
1078 	int new_flags;
1079 	size_t namelen, onamelen;
1080 	char new_name[IFNAMSIZ];
1081 	struct ifaddr *ifa;
1082 	struct sockaddr_dl *sdl;
1083 
1084 	switch (cmd) {
1085 
1086 	case SIOCGIFCONF:
1087 	case OSIOCGIFCONF:
1088 		return (ifconf(cmd, data, cred));
1089 	}
1090 	ifr = (struct ifreq *)data;
1091 
1092 	switch (cmd) {
1093 	case SIOCIFCREATE:
1094 	case SIOCIFDESTROY:
1095 		if ((error = suser_cred(cred, 0)) != 0)
1096 			return (error);
1097 		return ((cmd == SIOCIFCREATE) ?
1098 			if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1099 			if_clone_destroy(ifr->ifr_name));
1100 
1101 	case SIOCIFGCLONERS:
1102 		return (if_clone_list((struct if_clonereq *)data));
1103 	}
1104 
1105 	ifp = ifunit(ifr->ifr_name);
1106 	if (ifp == 0)
1107 		return (ENXIO);
1108 	switch (cmd) {
1109 
1110 	case SIOCGIFFLAGS:
1111 		ifr->ifr_flags = ifp->if_flags;
1112 		ifr->ifr_flagshigh = ifp->if_flags >> 16;
1113 		break;
1114 
1115 	case SIOCGIFCAP:
1116 		ifr->ifr_reqcap = ifp->if_capabilities;
1117 		ifr->ifr_curcap = ifp->if_capenable;
1118 		break;
1119 
1120 	case SIOCGIFMETRIC:
1121 		ifr->ifr_metric = ifp->if_metric;
1122 		break;
1123 
1124 	case SIOCGIFMTU:
1125 		ifr->ifr_mtu = ifp->if_mtu;
1126 		break;
1127 
1128 	case SIOCGIFPHYS:
1129 		ifr->ifr_phys = ifp->if_physical;
1130 		break;
1131 
1132 	case SIOCGIFPOLLCPU:
1133 #ifdef DEVICE_POLLING
1134 		ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1135 #else
1136 		ifr->ifr_pollcpu = -1;
1137 #endif
1138 		break;
1139 
1140 	case SIOCSIFPOLLCPU:
1141 #ifdef DEVICE_POLLING
1142 		if ((ifp->if_flags & IFF_POLLING) == 0)
1143 			ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1144 #endif
1145 		break;
1146 
1147 	case SIOCSIFFLAGS:
1148 		error = suser_cred(cred, 0);
1149 		if (error)
1150 			return (error);
1151 		new_flags = (ifr->ifr_flags & 0xffff) |
1152 		    (ifr->ifr_flagshigh << 16);
1153 		if (ifp->if_flags & IFF_SMART) {
1154 			/* Smart drivers twiddle their own routes */
1155 		} else if (ifp->if_flags & IFF_UP &&
1156 		    (new_flags & IFF_UP) == 0) {
1157 			crit_enter();
1158 			if_down(ifp);
1159 			crit_exit();
1160 		} else if (new_flags & IFF_UP &&
1161 		    (ifp->if_flags & IFF_UP) == 0) {
1162 			crit_enter();
1163 			if_up(ifp);
1164 			crit_exit();
1165 		}
1166 
1167 #ifdef DEVICE_POLLING
1168 		if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1169 			if (new_flags & IFF_POLLING) {
1170 				ether_poll_register(ifp);
1171 			} else {
1172 				ether_poll_deregister(ifp);
1173 			}
1174 		}
1175 #endif
1176 
1177 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1178 			(new_flags &~ IFF_CANTCHANGE);
1179 		if (new_flags & IFF_PPROMISC) {
1180 			/* Permanently promiscuous mode requested */
1181 			ifp->if_flags |= IFF_PROMISC;
1182 		} else if (ifp->if_pcount == 0) {
1183 			ifp->if_flags &= ~IFF_PROMISC;
1184 		}
1185 		if (ifp->if_ioctl) {
1186 			lwkt_serialize_enter(ifp->if_serializer);
1187 			ifp->if_ioctl(ifp, cmd, data, cred);
1188 			lwkt_serialize_exit(ifp->if_serializer);
1189 		}
1190 		getmicrotime(&ifp->if_lastchange);
1191 		break;
1192 
1193 	case SIOCSIFCAP:
1194 		error = suser_cred(cred, 0);
1195 		if (error)
1196 			return (error);
1197 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1198 			return (EINVAL);
1199 		lwkt_serialize_enter(ifp->if_serializer);
1200 		ifp->if_ioctl(ifp, cmd, data, cred);
1201 		lwkt_serialize_exit(ifp->if_serializer);
1202 		break;
1203 
1204 	case SIOCSIFNAME:
1205 		error = suser_cred(cred, 0);
1206 		if (error != 0)
1207 			return (error);
1208 		error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1209 		if (error != 0)
1210 			return (error);
1211 		if (new_name[0] == '\0')
1212 			return (EINVAL);
1213 		if (ifunit(new_name) != NULL)
1214 			return (EEXIST);
1215 
1216 		EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1217 
1218 		/* Announce the departure of the interface. */
1219 		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1220 
1221 		strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1222 		ifa = TAILQ_FIRST(&ifp->if_addrhead);
1223 		/* XXX IFA_LOCK(ifa); */
1224 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1225 		namelen = strlen(new_name);
1226 		onamelen = sdl->sdl_nlen;
1227 		/*
1228 		 * Move the address if needed.  This is safe because we
1229 		 * allocate space for a name of length IFNAMSIZ when we
1230 		 * create this in if_attach().
1231 		 */
1232 		if (namelen != onamelen) {
1233 			bcopy(sdl->sdl_data + onamelen,
1234 			    sdl->sdl_data + namelen, sdl->sdl_alen);
1235 		}
1236 		bcopy(new_name, sdl->sdl_data, namelen);
1237 		sdl->sdl_nlen = namelen;
1238 		sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1239 		bzero(sdl->sdl_data, onamelen);
1240 		while (namelen != 0)
1241 			sdl->sdl_data[--namelen] = 0xff;
1242 		/* XXX IFA_UNLOCK(ifa) */
1243 
1244 		EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1245 
1246 		/* Announce the return of the interface. */
1247 		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1248 		break;
1249 
1250 	case SIOCSIFMETRIC:
1251 		error = suser_cred(cred, 0);
1252 		if (error)
1253 			return (error);
1254 		ifp->if_metric = ifr->ifr_metric;
1255 		getmicrotime(&ifp->if_lastchange);
1256 		break;
1257 
1258 	case SIOCSIFPHYS:
1259 		error = suser_cred(cred, 0);
1260 		if (error)
1261 			return error;
1262 		if (!ifp->if_ioctl)
1263 		        return EOPNOTSUPP;
1264 		lwkt_serialize_enter(ifp->if_serializer);
1265 		error = ifp->if_ioctl(ifp, cmd, data, cred);
1266 		lwkt_serialize_exit(ifp->if_serializer);
1267 		if (error == 0)
1268 			getmicrotime(&ifp->if_lastchange);
1269 		return (error);
1270 
1271 	case SIOCSIFMTU:
1272 	{
1273 		u_long oldmtu = ifp->if_mtu;
1274 
1275 		error = suser_cred(cred, 0);
1276 		if (error)
1277 			return (error);
1278 		if (ifp->if_ioctl == NULL)
1279 			return (EOPNOTSUPP);
1280 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1281 			return (EINVAL);
1282 		lwkt_serialize_enter(ifp->if_serializer);
1283 		error = ifp->if_ioctl(ifp, cmd, data, cred);
1284 		lwkt_serialize_exit(ifp->if_serializer);
1285 		if (error == 0) {
1286 			getmicrotime(&ifp->if_lastchange);
1287 			rt_ifmsg(ifp);
1288 		}
1289 		/*
1290 		 * If the link MTU changed, do network layer specific procedure.
1291 		 */
1292 		if (ifp->if_mtu != oldmtu) {
1293 #ifdef INET6
1294 			nd6_setmtu(ifp);
1295 #endif
1296 		}
1297 		return (error);
1298 	}
1299 
1300 	case SIOCADDMULTI:
1301 	case SIOCDELMULTI:
1302 		error = suser_cred(cred, 0);
1303 		if (error)
1304 			return (error);
1305 
1306 		/* Don't allow group membership on non-multicast interfaces. */
1307 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
1308 			return EOPNOTSUPP;
1309 
1310 		/* Don't let users screw up protocols' entries. */
1311 		if (ifr->ifr_addr.sa_family != AF_LINK)
1312 			return EINVAL;
1313 
1314 		if (cmd == SIOCADDMULTI) {
1315 			struct ifmultiaddr *ifma;
1316 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1317 		} else {
1318 			error = if_delmulti(ifp, &ifr->ifr_addr);
1319 		}
1320 		if (error == 0)
1321 			getmicrotime(&ifp->if_lastchange);
1322 		return error;
1323 
1324 	case SIOCSIFPHYADDR:
1325 	case SIOCDIFPHYADDR:
1326 #ifdef INET6
1327 	case SIOCSIFPHYADDR_IN6:
1328 #endif
1329 	case SIOCSLIFPHYADDR:
1330         case SIOCSIFMEDIA:
1331 	case SIOCSIFGENERIC:
1332 		error = suser_cred(cred, 0);
1333 		if (error)
1334 			return (error);
1335 		if (ifp->if_ioctl == 0)
1336 			return (EOPNOTSUPP);
1337 		lwkt_serialize_enter(ifp->if_serializer);
1338 		error = ifp->if_ioctl(ifp, cmd, data, cred);
1339 		lwkt_serialize_exit(ifp->if_serializer);
1340 		if (error == 0)
1341 			getmicrotime(&ifp->if_lastchange);
1342 		return error;
1343 
1344 	case SIOCGIFSTATUS:
1345 		ifs = (struct ifstat *)data;
1346 		ifs->ascii[0] = '\0';
1347 
1348 	case SIOCGIFPSRCADDR:
1349 	case SIOCGIFPDSTADDR:
1350 	case SIOCGLIFPHYADDR:
1351 	case SIOCGIFMEDIA:
1352 	case SIOCGIFGENERIC:
1353 		if (ifp->if_ioctl == NULL)
1354 			return (EOPNOTSUPP);
1355 		lwkt_serialize_enter(ifp->if_serializer);
1356 		error = ifp->if_ioctl(ifp, cmd, data, cred);
1357 		lwkt_serialize_exit(ifp->if_serializer);
1358 		return (error);
1359 
1360 	case SIOCSIFLLADDR:
1361 		error = suser_cred(cred, 0);
1362 		if (error)
1363 			return (error);
1364 		return if_setlladdr(ifp,
1365 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1366 
1367 	default:
1368 		oif_flags = ifp->if_flags;
1369 		if (so->so_proto == 0)
1370 			return (EOPNOTSUPP);
1371 #ifndef COMPAT_43
1372 		error = so_pru_control(so, cmd, data, ifp);
1373 #else
1374 	    {
1375 		int ocmd = cmd;
1376 
1377 		switch (cmd) {
1378 
1379 		case SIOCSIFDSTADDR:
1380 		case SIOCSIFADDR:
1381 		case SIOCSIFBRDADDR:
1382 		case SIOCSIFNETMASK:
1383 #if BYTE_ORDER != BIG_ENDIAN
1384 			if (ifr->ifr_addr.sa_family == 0 &&
1385 			    ifr->ifr_addr.sa_len < 16) {
1386 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1387 				ifr->ifr_addr.sa_len = 16;
1388 			}
1389 #else
1390 			if (ifr->ifr_addr.sa_len == 0)
1391 				ifr->ifr_addr.sa_len = 16;
1392 #endif
1393 			break;
1394 
1395 		case OSIOCGIFADDR:
1396 			cmd = SIOCGIFADDR;
1397 			break;
1398 
1399 		case OSIOCGIFDSTADDR:
1400 			cmd = SIOCGIFDSTADDR;
1401 			break;
1402 
1403 		case OSIOCGIFBRDADDR:
1404 			cmd = SIOCGIFBRDADDR;
1405 			break;
1406 
1407 		case OSIOCGIFNETMASK:
1408 			cmd = SIOCGIFNETMASK;
1409 		}
1410 		error =  so_pru_control(so, cmd, data, ifp);
1411 		switch (ocmd) {
1412 
1413 		case OSIOCGIFADDR:
1414 		case OSIOCGIFDSTADDR:
1415 		case OSIOCGIFBRDADDR:
1416 		case OSIOCGIFNETMASK:
1417 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1418 
1419 		}
1420 	    }
1421 #endif /* COMPAT_43 */
1422 
1423 		if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1424 #ifdef INET6
1425 			DELAY(100);/* XXX: temporary workaround for fxp issue*/
1426 			if (ifp->if_flags & IFF_UP) {
1427 				crit_enter();
1428 				in6_if_up(ifp);
1429 				crit_exit();
1430 			}
1431 #endif
1432 		}
1433 		return (error);
1434 
1435 	}
1436 	return (0);
1437 }
1438 
1439 /*
1440  * Set/clear promiscuous mode on interface ifp based on the truth value
1441  * of pswitch.  The calls are reference counted so that only the first
1442  * "on" request actually has an effect, as does the final "off" request.
1443  * Results are undefined if the "off" and "on" requests are not matched.
1444  */
1445 int
1446 ifpromisc(struct ifnet *ifp, int pswitch)
1447 {
1448 	struct ifreq ifr;
1449 	int error;
1450 	int oldflags;
1451 
1452 	oldflags = ifp->if_flags;
1453 	if (ifp->if_flags & IFF_PPROMISC) {
1454 		/* Do nothing if device is in permanently promiscuous mode */
1455 		ifp->if_pcount += pswitch ? 1 : -1;
1456 		return (0);
1457 	}
1458 	if (pswitch) {
1459 		/*
1460 		 * If the device is not configured up, we cannot put it in
1461 		 * promiscuous mode.
1462 		 */
1463 		if ((ifp->if_flags & IFF_UP) == 0)
1464 			return (ENETDOWN);
1465 		if (ifp->if_pcount++ != 0)
1466 			return (0);
1467 		ifp->if_flags |= IFF_PROMISC;
1468 		log(LOG_INFO, "%s: promiscuous mode enabled\n",
1469 		    ifp->if_xname);
1470 	} else {
1471 		if (--ifp->if_pcount > 0)
1472 			return (0);
1473 		ifp->if_flags &= ~IFF_PROMISC;
1474 		log(LOG_INFO, "%s: promiscuous mode disabled\n",
1475 		    ifp->if_xname);
1476 	}
1477 	ifr.ifr_flags = ifp->if_flags;
1478 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
1479 	lwkt_serialize_enter(ifp->if_serializer);
1480 	error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1481 				 (struct ucred *)NULL);
1482 	lwkt_serialize_exit(ifp->if_serializer);
1483 	if (error == 0)
1484 		rt_ifmsg(ifp);
1485 	else
1486 		ifp->if_flags = oldflags;
1487 	return error;
1488 }
1489 
1490 /*
1491  * Return interface configuration
1492  * of system.  List may be used
1493  * in later ioctl's (above) to get
1494  * other information.
1495  */
1496 static int
1497 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1498 {
1499 	struct ifconf *ifc = (struct ifconf *)data;
1500 	struct ifnet *ifp;
1501 	struct ifaddr *ifa;
1502 	struct sockaddr *sa;
1503 	struct ifreq ifr, *ifrp;
1504 	int space = ifc->ifc_len, error = 0;
1505 
1506 	ifrp = ifc->ifc_req;
1507 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1508 		int addrs;
1509 
1510 		if (space <= sizeof ifr)
1511 			break;
1512 
1513 		/*
1514 		 * Zero the stack declared structure first to prevent
1515 		 * memory disclosure.
1516 		 */
1517 		bzero(&ifr, sizeof(ifr));
1518 		if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1519 		    >= sizeof(ifr.ifr_name)) {
1520 			error = ENAMETOOLONG;
1521 			break;
1522 		}
1523 
1524 		addrs = 0;
1525 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1526 			if (space <= sizeof ifr)
1527 				break;
1528 			sa = ifa->ifa_addr;
1529 			if (cred->cr_prison &&
1530 			    prison_if(cred, sa))
1531 				continue;
1532 			addrs++;
1533 #ifdef COMPAT_43
1534 			if (cmd == OSIOCGIFCONF) {
1535 				struct osockaddr *osa =
1536 					 (struct osockaddr *)&ifr.ifr_addr;
1537 				ifr.ifr_addr = *sa;
1538 				osa->sa_family = sa->sa_family;
1539 				error = copyout(&ifr, ifrp, sizeof ifr);
1540 				ifrp++;
1541 			} else
1542 #endif
1543 			if (sa->sa_len <= sizeof(*sa)) {
1544 				ifr.ifr_addr = *sa;
1545 				error = copyout(&ifr, ifrp, sizeof ifr);
1546 				ifrp++;
1547 			} else {
1548 				if (space < (sizeof ifr) + sa->sa_len -
1549 					    sizeof(*sa))
1550 					break;
1551 				space -= sa->sa_len - sizeof(*sa);
1552 				error = copyout(&ifr, ifrp,
1553 						sizeof ifr.ifr_name);
1554 				if (error == 0)
1555 					error = copyout(sa, &ifrp->ifr_addr,
1556 							sa->sa_len);
1557 				ifrp = (struct ifreq *)
1558 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1559 			}
1560 			if (error)
1561 				break;
1562 			space -= sizeof ifr;
1563 		}
1564 		if (error)
1565 			break;
1566 		if (!addrs) {
1567 			bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1568 			error = copyout(&ifr, ifrp, sizeof ifr);
1569 			if (error)
1570 				break;
1571 			space -= sizeof ifr;
1572 			ifrp++;
1573 		}
1574 	}
1575 	ifc->ifc_len -= space;
1576 	return (error);
1577 }
1578 
1579 /*
1580  * Just like if_promisc(), but for all-multicast-reception mode.
1581  */
1582 int
1583 if_allmulti(struct ifnet *ifp, int onswitch)
1584 {
1585 	int error = 0;
1586 	struct ifreq ifr;
1587 
1588 	crit_enter();
1589 
1590 	if (onswitch) {
1591 		if (ifp->if_amcount++ == 0) {
1592 			ifp->if_flags |= IFF_ALLMULTI;
1593 			ifr.ifr_flags = ifp->if_flags;
1594 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1595 			lwkt_serialize_enter(ifp->if_serializer);
1596 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1597 					      (struct ucred *)NULL);
1598 			lwkt_serialize_exit(ifp->if_serializer);
1599 		}
1600 	} else {
1601 		if (ifp->if_amcount > 1) {
1602 			ifp->if_amcount--;
1603 		} else {
1604 			ifp->if_amcount = 0;
1605 			ifp->if_flags &= ~IFF_ALLMULTI;
1606 			ifr.ifr_flags = ifp->if_flags;
1607 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1608 			lwkt_serialize_enter(ifp->if_serializer);
1609 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1610 					      (struct ucred *)NULL);
1611 			lwkt_serialize_exit(ifp->if_serializer);
1612 		}
1613 	}
1614 
1615 	crit_exit();
1616 
1617 	if (error == 0)
1618 		rt_ifmsg(ifp);
1619 	return error;
1620 }
1621 
1622 /*
1623  * Add a multicast listenership to the interface in question.
1624  * The link layer provides a routine which converts
1625  */
1626 int
1627 if_addmulti(
1628 	struct ifnet *ifp,	/* interface to manipulate */
1629 	struct sockaddr *sa,	/* address to add */
1630 	struct ifmultiaddr **retifma)
1631 {
1632 	struct sockaddr *llsa, *dupsa;
1633 	int error;
1634 	struct ifmultiaddr *ifma;
1635 
1636 	/*
1637 	 * If the matching multicast address already exists
1638 	 * then don't add a new one, just add a reference
1639 	 */
1640 	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1641 		if (sa_equal(sa, ifma->ifma_addr)) {
1642 			ifma->ifma_refcount++;
1643 			if (retifma)
1644 				*retifma = ifma;
1645 			return 0;
1646 		}
1647 	}
1648 
1649 	/*
1650 	 * Give the link layer a chance to accept/reject it, and also
1651 	 * find out which AF_LINK address this maps to, if it isn't one
1652 	 * already.
1653 	 */
1654 	if (ifp->if_resolvemulti) {
1655 		lwkt_serialize_enter(ifp->if_serializer);
1656 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
1657 		lwkt_serialize_exit(ifp->if_serializer);
1658 		if (error)
1659 			return error;
1660 	} else {
1661 		llsa = 0;
1662 	}
1663 
1664 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1665 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1666 	bcopy(sa, dupsa, sa->sa_len);
1667 
1668 	ifma->ifma_addr = dupsa;
1669 	ifma->ifma_lladdr = llsa;
1670 	ifma->ifma_ifp = ifp;
1671 	ifma->ifma_refcount = 1;
1672 	ifma->ifma_protospec = 0;
1673 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1674 
1675 	/*
1676 	 * Some network interfaces can scan the address list at
1677 	 * interrupt time; lock them out.
1678 	 */
1679 	crit_enter();
1680 	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1681 	crit_exit();
1682 	*retifma = ifma;
1683 
1684 	if (llsa != 0) {
1685 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1686 			if (sa_equal(ifma->ifma_addr, llsa))
1687 				break;
1688 		}
1689 		if (ifma) {
1690 			ifma->ifma_refcount++;
1691 		} else {
1692 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1693 			       M_IFMADDR, M_WAITOK);
1694 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1695 			       M_IFMADDR, M_WAITOK);
1696 			bcopy(llsa, dupsa, llsa->sa_len);
1697 			ifma->ifma_addr = dupsa;
1698 			ifma->ifma_ifp = ifp;
1699 			ifma->ifma_refcount = 1;
1700 			crit_enter();
1701 			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1702 			crit_exit();
1703 		}
1704 	}
1705 	/*
1706 	 * We are certain we have added something, so call down to the
1707 	 * interface to let them know about it.
1708 	 */
1709 	crit_enter();
1710 	lwkt_serialize_enter(ifp->if_serializer);
1711 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1712 	lwkt_serialize_exit(ifp->if_serializer);
1713 	crit_exit();
1714 
1715 	return 0;
1716 }
1717 
1718 /*
1719  * Remove a reference to a multicast address on this interface.  Yell
1720  * if the request does not match an existing membership.
1721  */
1722 int
1723 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1724 {
1725 	struct ifmultiaddr *ifma;
1726 
1727 	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1728 		if (sa_equal(sa, ifma->ifma_addr))
1729 			break;
1730 	if (ifma == 0)
1731 		return ENOENT;
1732 
1733 	if (ifma->ifma_refcount > 1) {
1734 		ifma->ifma_refcount--;
1735 		return 0;
1736 	}
1737 
1738 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
1739 	sa = ifma->ifma_lladdr;
1740 	crit_enter();
1741 	LIST_REMOVE(ifma, ifma_link);
1742 	/*
1743 	 * Make sure the interface driver is notified
1744 	 * in the case of a link layer mcast group being left.
1745 	 */
1746 	if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1747 		lwkt_serialize_enter(ifp->if_serializer);
1748 		ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1749 		lwkt_serialize_exit(ifp->if_serializer);
1750 	}
1751 	crit_exit();
1752 	kfree(ifma->ifma_addr, M_IFMADDR);
1753 	kfree(ifma, M_IFMADDR);
1754 	if (sa == 0)
1755 		return 0;
1756 
1757 	/*
1758 	 * Now look for the link-layer address which corresponds to
1759 	 * this network address.  It had been squirreled away in
1760 	 * ifma->ifma_lladdr for this purpose (so we don't have
1761 	 * to call ifp->if_resolvemulti() again), and we saved that
1762 	 * value in sa above.  If some nasty deleted the
1763 	 * link-layer address out from underneath us, we can deal because
1764 	 * the address we stored was is not the same as the one which was
1765 	 * in the record for the link-layer address.  (So we don't complain
1766 	 * in that case.)
1767 	 */
1768 	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1769 		if (sa_equal(sa, ifma->ifma_addr))
1770 			break;
1771 	if (ifma == 0)
1772 		return 0;
1773 
1774 	if (ifma->ifma_refcount > 1) {
1775 		ifma->ifma_refcount--;
1776 		return 0;
1777 	}
1778 
1779 	crit_enter();
1780 	lwkt_serialize_enter(ifp->if_serializer);
1781 	LIST_REMOVE(ifma, ifma_link);
1782 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1783 	lwkt_serialize_exit(ifp->if_serializer);
1784 	crit_exit();
1785 	kfree(ifma->ifma_addr, M_IFMADDR);
1786 	kfree(sa, M_IFMADDR);
1787 	kfree(ifma, M_IFMADDR);
1788 
1789 	return 0;
1790 }
1791 
1792 /*
1793  * Set the link layer address on an interface.
1794  *
1795  * At this time we only support certain types of interfaces,
1796  * and we don't allow the length of the address to change.
1797  */
1798 int
1799 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1800 {
1801 	struct sockaddr_dl *sdl;
1802 	struct ifaddr *ifa;
1803 	struct ifreq ifr;
1804 
1805 	sdl = IF_LLSOCKADDR(ifp);
1806 	if (sdl == NULL)
1807 		return (EINVAL);
1808 	if (len != sdl->sdl_alen)	/* don't allow length to change */
1809 		return (EINVAL);
1810 	switch (ifp->if_type) {
1811 	case IFT_ETHER:			/* these types use struct arpcom */
1812 	case IFT_XETHER:
1813 	case IFT_L2VLAN:
1814 		bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1815 		bcopy(lladdr, LLADDR(sdl), len);
1816 		break;
1817 	default:
1818 		return (ENODEV);
1819 	}
1820 	/*
1821 	 * If the interface is already up, we need
1822 	 * to re-init it in order to reprogram its
1823 	 * address filter.
1824 	 */
1825 	lwkt_serialize_enter(ifp->if_serializer);
1826 	if ((ifp->if_flags & IFF_UP) != 0) {
1827 		ifp->if_flags &= ~IFF_UP;
1828 		ifr.ifr_flags = ifp->if_flags;
1829 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
1830 		ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1831 			      (struct ucred *)NULL);
1832 		ifp->if_flags |= IFF_UP;
1833 		ifr.ifr_flags = ifp->if_flags;
1834 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
1835 		ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1836 				 (struct ucred *)NULL);
1837 #ifdef INET
1838 		/*
1839 		 * Also send gratuitous ARPs to notify other nodes about
1840 		 * the address change.
1841 		 */
1842 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1843 			if (ifa->ifa_addr != NULL &&
1844 			    ifa->ifa_addr->sa_family == AF_INET)
1845 				arp_ifinit(ifp, ifa);
1846 		}
1847 #endif
1848 	}
1849 	lwkt_serialize_exit(ifp->if_serializer);
1850 	return (0);
1851 }
1852 
1853 struct ifmultiaddr *
1854 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
1855 {
1856 	struct ifmultiaddr *ifma;
1857 
1858 	LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1859 		if (sa_equal(ifma->ifma_addr, sa))
1860 			break;
1861 
1862 	return ifma;
1863 }
1864 
1865 /*
1866  * This function locates the first real ethernet MAC from a network
1867  * card and loads it into node, returning 0 on success or ENOENT if
1868  * no suitable interfaces were found.  It is used by the uuid code to
1869  * generate a unique 6-byte number.
1870  */
1871 int
1872 if_getanyethermac(uint16_t *node, int minlen)
1873 {
1874 	struct ifnet *ifp;
1875 	struct sockaddr_dl *sdl;
1876 
1877 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1878 		if (ifp->if_type != IFT_ETHER)
1879 			continue;
1880 		sdl = IF_LLSOCKADDR(ifp);
1881 		if (sdl->sdl_alen < minlen)
1882 			continue;
1883 		bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
1884 		      minlen);
1885 		return(0);
1886 	}
1887 	return (ENOENT);
1888 }
1889 
1890 /*
1891  * The name argument must be a pointer to storage which will last as
1892  * long as the interface does.  For physical devices, the result of
1893  * device_get_name(dev) is a good choice and for pseudo-devices a
1894  * static string works well.
1895  */
1896 void
1897 if_initname(struct ifnet *ifp, const char *name, int unit)
1898 {
1899 	ifp->if_dname = name;
1900 	ifp->if_dunit = unit;
1901 	if (unit != IF_DUNIT_NONE)
1902 		ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1903 	else
1904 		strlcpy(ifp->if_xname, name, IFNAMSIZ);
1905 }
1906 
1907 int
1908 if_printf(struct ifnet *ifp, const char *fmt, ...)
1909 {
1910 	__va_list ap;
1911 	int retval;
1912 
1913 	retval = kprintf("%s: ", ifp->if_xname);
1914 	__va_start(ap, fmt);
1915 	retval += kvprintf(fmt, ap);
1916 	__va_end(ap);
1917 	return (retval);
1918 }
1919 
1920 void
1921 ifq_set_classic(struct ifaltq *ifq)
1922 {
1923 	ifq->altq_enqueue = ifq_classic_enqueue;
1924 	ifq->altq_dequeue = ifq_classic_dequeue;
1925 	ifq->altq_request = ifq_classic_request;
1926 }
1927 
1928 static int
1929 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
1930 		    struct altq_pktattr *pa __unused)
1931 {
1932 	crit_enter();
1933 	if (IF_QFULL(ifq)) {
1934 		m_freem(m);
1935 		crit_exit();
1936 		return(ENOBUFS);
1937 	} else {
1938 		IF_ENQUEUE(ifq, m);
1939 		crit_exit();
1940 		return(0);
1941 	}
1942 }
1943 
1944 static struct mbuf *
1945 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
1946 {
1947 	struct mbuf *m;
1948 
1949 	crit_enter();
1950 	switch (op) {
1951 	case ALTDQ_POLL:
1952 		IF_POLL(ifq, m);
1953 		break;
1954 	case ALTDQ_REMOVE:
1955 		IF_DEQUEUE(ifq, m);
1956 		break;
1957 	default:
1958 		panic("unsupported ALTQ dequeue op: %d", op);
1959 	}
1960 	crit_exit();
1961 	KKASSERT(mpolled == NULL || mpolled == m);
1962 	return(m);
1963 }
1964 
1965 static int
1966 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
1967 {
1968 	crit_enter();
1969 	switch (req) {
1970 	case ALTRQ_PURGE:
1971 		IF_DRAIN(ifq);
1972 		break;
1973 	default:
1974 		panic("unsupported ALTQ request: %d", req);
1975 	}
1976 	crit_exit();
1977 	return(0);
1978 }
1979