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