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