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