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