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