xref: /freebsd/sys/net/if.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if.c	8.3 (Berkeley) 1/4/94
34  *	$Id: if.c,v 1.72 1999/06/06 09:28:01 phk Exp $
35  */
36 
37 #include "opt_compat.h"
38 
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/protosw.h>
47 #include <sys/kernel.h>
48 #include <sys/sockio.h>
49 #include <sys/syslog.h>
50 #include <sys/sysctl.h>
51 
52 #include <net/if.h>
53 #include <net/if_dl.h>
54 #include <net/radix.h>
55 
56 /*
57  * System initialization
58  */
59 
60 static int ifconf __P((u_long, caddr_t));
61 static void ifinit __P((void *));
62 static void if_qflush __P((struct ifqueue *));
63 static void if_slowtimo __P((void *));
64 static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
65 
66 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
67 
68 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
69 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
70 
71 int	ifqmaxlen = IFQ_MAXLEN;
72 struct	ifnethead ifnet;	/* depend on static init XXX */
73 
74 /*
75  * Network interface utility routines.
76  *
77  * Routines with ifa_ifwith* names take sockaddr *'s as
78  * parameters.
79  */
80 /* ARGSUSED*/
81 void
82 ifinit(dummy)
83 	void *dummy;
84 {
85 	struct ifnet *ifp;
86 	int s;
87 
88 	s = splimp();
89 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
90 		if (ifp->if_snd.ifq_maxlen == 0) {
91 			printf("%s%d XXX: driver didn't set ifq_maxlen\n",
92 			    ifp->if_name, ifp->if_unit);
93 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
94 		}
95 	splx(s);
96 	if_slowtimo(0);
97 }
98 
99 int if_index = 0;
100 struct ifaddr **ifnet_addrs;
101 
102 
103 /*
104  * Attach an interface to the
105  * list of "active" interfaces.
106  */
107 void
108 if_attach(ifp)
109 	struct ifnet *ifp;
110 {
111 	unsigned socksize, ifasize;
112 	int namelen, masklen;
113 	char workbuf[64];
114 	register struct sockaddr_dl *sdl;
115 	register struct ifaddr *ifa;
116 	static int if_indexlim = 8;
117 	static int inited;
118 
119 	if (!inited) {
120 		TAILQ_INIT(&ifnet);
121 		inited = 1;
122 	}
123 
124 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
125 	ifp->if_index = ++if_index;
126 	/*
127 	 * XXX -
128 	 * The old code would work if the interface passed a pre-existing
129 	 * chain of ifaddrs to this code.  We don't trust our callers to
130 	 * properly initialize the tailq, however, so we no longer allow
131 	 * this unlikely case.
132 	 */
133 	TAILQ_INIT(&ifp->if_addrhead);
134 	LIST_INIT(&ifp->if_multiaddrs);
135 	getmicrotime(&ifp->if_lastchange);
136 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
137 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
138 		struct ifaddr **q = (struct ifaddr **)
139 					malloc(n, M_IFADDR, M_WAITOK);
140 		bzero((caddr_t)q, n);
141 		if (ifnet_addrs) {
142 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
143 			free((caddr_t)ifnet_addrs, M_IFADDR);
144 		}
145 		ifnet_addrs = q;
146 	}
147 	/*
148 	 * create a Link Level name for this device
149 	 */
150 	namelen = snprintf(workbuf, sizeof(workbuf),
151 	    "%s%d", ifp->if_name, ifp->if_unit);
152 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
153 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
154 	socksize = masklen + ifp->if_addrlen;
155 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
156 	if (socksize < sizeof(*sdl))
157 		socksize = sizeof(*sdl);
158 	socksize = ROUNDUP(socksize);
159 	ifasize = sizeof(*ifa) + 2 * socksize;
160 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
161 	if (ifa) {
162 		bzero((caddr_t)ifa, ifasize);
163 		sdl = (struct sockaddr_dl *)(ifa + 1);
164 		sdl->sdl_len = socksize;
165 		sdl->sdl_family = AF_LINK;
166 		bcopy(workbuf, sdl->sdl_data, namelen);
167 		sdl->sdl_nlen = namelen;
168 		sdl->sdl_index = ifp->if_index;
169 		sdl->sdl_type = ifp->if_type;
170 		ifnet_addrs[if_index - 1] = ifa;
171 		ifa->ifa_ifp = ifp;
172 		ifa->ifa_rtrequest = link_rtrequest;
173 		ifa->ifa_addr = (struct sockaddr *)sdl;
174 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
175 		ifa->ifa_netmask = (struct sockaddr *)sdl;
176 		sdl->sdl_len = masklen;
177 		while (namelen != 0)
178 			sdl->sdl_data[--namelen] = 0xff;
179 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
180 	}
181 }
182 
183 /*
184  * Detach an interface, removing it from the
185  * list of "active" interfaces.
186  */
187 void
188 if_detach(ifp)
189 	struct ifnet *ifp;
190 {
191 	struct ifaddr *ifa;
192 
193 	/*
194 	 * Remove routes and flush queues.
195 	 */
196 	if_down(ifp);
197 
198 	/*
199 	 * Remove address from ifnet_addrs[] and maybe decrement if_index.
200 	 * Clean up all addresses.
201 	 */
202 	ifnet_addrs[ifp->if_index] = 0;
203 	while (ifnet_addrs[if_index] == 0)
204 		if_index--;
205 
206 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
207 	     ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
208 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
209 		IFAFREE(ifa);
210 	}
211 
212 	TAILQ_REMOVE(&ifnet, ifp, if_link);
213 }
214 
215 /*
216  * Locate an interface based on a complete address.
217  */
218 /*ARGSUSED*/
219 struct ifaddr *
220 ifa_ifwithaddr(addr)
221 	register struct sockaddr *addr;
222 {
223 	register struct ifnet *ifp;
224 	register struct ifaddr *ifa;
225 
226 #define	equal(a1, a2) \
227   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
228 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
229 	    for (ifa = ifp->if_addrhead.tqh_first; ifa;
230 		 ifa = ifa->ifa_link.tqe_next) {
231 		if (ifa->ifa_addr->sa_family != addr->sa_family)
232 			continue;
233 		if (equal(addr, ifa->ifa_addr))
234 			return (ifa);
235 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
236 		    equal(ifa->ifa_broadaddr, addr))
237 			return (ifa);
238 	}
239 	return ((struct ifaddr *)0);
240 }
241 /*
242  * Locate the point to point interface with a given destination address.
243  */
244 /*ARGSUSED*/
245 struct ifaddr *
246 ifa_ifwithdstaddr(addr)
247 	register struct sockaddr *addr;
248 {
249 	register struct ifnet *ifp;
250 	register struct ifaddr *ifa;
251 
252 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
253 	    if (ifp->if_flags & IFF_POINTOPOINT)
254 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
255 		     ifa = ifa->ifa_link.tqe_next) {
256 			if (ifa->ifa_addr->sa_family != addr->sa_family)
257 				continue;
258 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
259 				return (ifa);
260 	}
261 	return ((struct ifaddr *)0);
262 }
263 
264 /*
265  * Find an interface on a specific network.  If many, choice
266  * is most specific found.
267  */
268 struct ifaddr *
269 ifa_ifwithnet(addr)
270 	struct sockaddr *addr;
271 {
272 	register struct ifnet *ifp;
273 	register struct ifaddr *ifa;
274 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
275 	u_int af = addr->sa_family;
276 	char *addr_data = addr->sa_data, *cplim;
277 
278 	/*
279 	 * AF_LINK addresses can be looked up directly by their index number,
280 	 * so do that if we can.
281 	 */
282 	if (af == AF_LINK) {
283 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
284 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
285 		return (ifnet_addrs[sdl->sdl_index - 1]);
286 	}
287 
288 	/*
289 	 * Scan though each interface, looking for ones that have
290 	 * addresses in this address family.
291 	 */
292 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
293 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
294 		     ifa = ifa->ifa_link.tqe_next) {
295 			register char *cp, *cp2, *cp3;
296 
297 			if (ifa->ifa_addr->sa_family != af)
298 next:				continue;
299 			if (ifp->if_flags & IFF_POINTOPOINT) {
300 				/*
301 				 * This is a bit broken as it doesn't
302 				 * take into account that the remote end may
303 				 * be a single node in the network we are
304 				 * looking for.
305 				 * The trouble is that we don't know the
306 				 * netmask for the remote end.
307 				 */
308 				if (ifa->ifa_dstaddr != 0
309 				    && equal(addr, ifa->ifa_dstaddr))
310  					return (ifa);
311 			} else {
312 				/*
313 				 * if we have a special address handler,
314 				 * then use it instead of the generic one.
315 				 */
316 	          		if (ifa->ifa_claim_addr) {
317 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
318 						return (ifa);
319 					} else {
320 						continue;
321 					}
322 				}
323 
324 				/*
325 				 * Scan all the bits in the ifa's address.
326 				 * If a bit dissagrees with what we are
327 				 * looking for, mask it with the netmask
328 				 * to see if it really matters.
329 				 * (A byte at a time)
330 				 */
331 				if (ifa->ifa_netmask == 0)
332 					continue;
333 				cp = addr_data;
334 				cp2 = ifa->ifa_addr->sa_data;
335 				cp3 = ifa->ifa_netmask->sa_data;
336 				cplim = ifa->ifa_netmask->sa_len
337 					+ (char *)ifa->ifa_netmask;
338 				while (cp3 < cplim)
339 					if ((*cp++ ^ *cp2++) & *cp3++)
340 						goto next; /* next address! */
341 				/*
342 				 * If the netmask of what we just found
343 				 * is more specific than what we had before
344 				 * (if we had one) then remember the new one
345 				 * before continuing to search
346 				 * for an even better one.
347 				 */
348 				if (ifa_maybe == 0 ||
349 				    rn_refines((caddr_t)ifa->ifa_netmask,
350 				    (caddr_t)ifa_maybe->ifa_netmask))
351 					ifa_maybe = ifa;
352 			}
353 		}
354 	}
355 	return (ifa_maybe);
356 }
357 
358 /*
359  * Find an interface address specific to an interface best matching
360  * a given address.
361  */
362 struct ifaddr *
363 ifaof_ifpforaddr(addr, ifp)
364 	struct sockaddr *addr;
365 	register struct ifnet *ifp;
366 {
367 	register struct ifaddr *ifa;
368 	register char *cp, *cp2, *cp3;
369 	register char *cplim;
370 	struct ifaddr *ifa_maybe = 0;
371 	u_int af = addr->sa_family;
372 
373 	if (af >= AF_MAX)
374 		return (0);
375 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
376 	     ifa = ifa->ifa_link.tqe_next) {
377 		if (ifa->ifa_addr->sa_family != af)
378 			continue;
379 		if (ifa_maybe == 0)
380 			ifa_maybe = ifa;
381 		if (ifa->ifa_netmask == 0) {
382 			if (equal(addr, ifa->ifa_addr) ||
383 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
384 				return (ifa);
385 			continue;
386 		}
387 		if (ifp->if_flags & IFF_POINTOPOINT) {
388 			if (equal(addr, ifa->ifa_dstaddr))
389 				return (ifa);
390 		} else {
391 			cp = addr->sa_data;
392 			cp2 = ifa->ifa_addr->sa_data;
393 			cp3 = ifa->ifa_netmask->sa_data;
394 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
395 			for (; cp3 < cplim; cp3++)
396 				if ((*cp++ ^ *cp2++) & *cp3)
397 					break;
398 			if (cp3 == cplim)
399 				return (ifa);
400 		}
401 	}
402 	return (ifa_maybe);
403 }
404 
405 #include <net/route.h>
406 
407 /*
408  * Default action when installing a route with a Link Level gateway.
409  * Lookup an appropriate real ifa to point to.
410  * This should be moved to /sys/net/link.c eventually.
411  */
412 static void
413 link_rtrequest(cmd, rt, sa)
414 	int cmd;
415 	register struct rtentry *rt;
416 	struct sockaddr *sa;
417 {
418 	register struct ifaddr *ifa;
419 	struct sockaddr *dst;
420 	struct ifnet *ifp;
421 
422 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
423 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
424 		return;
425 	ifa = ifaof_ifpforaddr(dst, ifp);
426 	if (ifa) {
427 		IFAFREE(rt->rt_ifa);
428 		rt->rt_ifa = ifa;
429 		ifa->ifa_refcnt++;
430 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
431 			ifa->ifa_rtrequest(cmd, rt, sa);
432 	}
433 }
434 
435 /*
436  * Mark an interface down and notify protocols of
437  * the transition.
438  * NOTE: must be called at splnet or eqivalent.
439  */
440 void
441 if_unroute(ifp, flag, fam)
442 	register struct ifnet *ifp;
443 	int flag, fam;
444 {
445 	register struct ifaddr *ifa;
446 
447 	ifp->if_flags &= ~flag;
448 	getmicrotime(&ifp->if_lastchange);
449 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
450 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
451 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
452 	if_qflush(&ifp->if_snd);
453 	rt_ifmsg(ifp);
454 }
455 
456 /*
457  * Mark an interface up and notify protocols of
458  * the transition.
459  * NOTE: must be called at splnet or eqivalent.
460  */
461 void
462 if_route(ifp, flag, fam)
463 	register struct ifnet *ifp;
464 	int flag, fam;
465 {
466 	register struct ifaddr *ifa;
467 
468 	ifp->if_flags |= flag;
469 	getmicrotime(&ifp->if_lastchange);
470 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
471 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
472 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
473 	rt_ifmsg(ifp);
474 }
475 
476 /*
477  * Mark an interface down and notify protocols of
478  * the transition.
479  * NOTE: must be called at splnet or eqivalent.
480  */
481 void
482 if_down(ifp)
483 	register struct ifnet *ifp;
484 {
485 
486 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
487 }
488 
489 /*
490  * Mark an interface up and notify protocols of
491  * the transition.
492  * NOTE: must be called at splnet or eqivalent.
493  */
494 void
495 if_up(ifp)
496 	register struct ifnet *ifp;
497 {
498 
499 	if_route(ifp, IFF_UP, AF_UNSPEC);
500 }
501 
502 /*
503  * Flush an interface queue.
504  */
505 static void
506 if_qflush(ifq)
507 	register struct ifqueue *ifq;
508 {
509 	register struct mbuf *m, *n;
510 
511 	n = ifq->ifq_head;
512 	while ((m = n) != 0) {
513 		n = m->m_act;
514 		m_freem(m);
515 	}
516 	ifq->ifq_head = 0;
517 	ifq->ifq_tail = 0;
518 	ifq->ifq_len = 0;
519 }
520 
521 /*
522  * Handle interface watchdog timer routines.  Called
523  * from softclock, we decrement timers (if set) and
524  * call the appropriate interface routine on expiration.
525  */
526 static void
527 if_slowtimo(arg)
528 	void *arg;
529 {
530 	register struct ifnet *ifp;
531 	int s = splimp();
532 
533 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
534 		if (ifp->if_timer == 0 || --ifp->if_timer)
535 			continue;
536 		if (ifp->if_watchdog)
537 			(*ifp->if_watchdog)(ifp);
538 	}
539 	splx(s);
540 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
541 }
542 
543 /*
544  * Map interface name to
545  * interface structure pointer.
546  */
547 struct ifnet *
548 ifunit(name)
549 	register char *name;
550 {
551 	char namebuf[IFNAMSIZ + 1];
552 	register char *cp, *cp2;
553 	char *end;
554 	register struct ifnet *ifp;
555 	int unit;
556 	unsigned len;
557 	register char c = '\0';
558 
559 	/*
560 	 * Look for a non numeric part
561 	 */
562 	end = name + IFNAMSIZ;
563 	cp2 = namebuf;
564 	cp = name;
565 	while ((cp < end) && (c = *cp)) {
566 		if (c >= '0' && c <= '9')
567 			break;
568 		*cp2++ = c;
569 		cp++;
570 	}
571 	if ((cp == end) || (c == '\0') || (cp == name))
572 		return ((struct ifnet *)0);
573 	*cp2 = '\0';
574 	/*
575 	 * check we have a legal number (limit to 7 digits?)
576 	 */
577 	len = cp - name + 1;
578 	for (unit = 0;
579 	    ((c = *cp) >= '0') && (c <= '9') && (unit < 1000000); cp++ )
580 		unit = (unit * 10) + (c - '0');
581 	if (*cp != '\0')
582 		return 0;	/* no trailing garbage allowed */
583 	/*
584 	 * Now search all the interfaces for this name/number
585 	 */
586 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
587 		if (bcmp(ifp->if_name, namebuf, len))
588 			continue;
589 		if (unit == ifp->if_unit)
590 			break;
591 	}
592 	return (ifp);
593 }
594 
595 /*
596  * Interface ioctls.
597  */
598 int
599 ifioctl(so, cmd, data, p)
600 	struct socket *so;
601 	u_long cmd;
602 	caddr_t data;
603 	struct proc *p;
604 {
605 	register struct ifnet *ifp;
606 	register struct ifreq *ifr;
607 	struct ifstat *ifs;
608 	int error;
609 
610 	switch (cmd) {
611 
612 	case SIOCGIFCONF:
613 	case OSIOCGIFCONF:
614 		return (ifconf(cmd, data));
615 	}
616 	ifr = (struct ifreq *)data;
617 	ifp = ifunit(ifr->ifr_name);
618 	if (ifp == 0)
619 		return (ENXIO);
620 	switch (cmd) {
621 
622 	case SIOCGIFFLAGS:
623 		ifr->ifr_flags = ifp->if_flags;
624 		break;
625 
626 	case SIOCGIFMETRIC:
627 		ifr->ifr_metric = ifp->if_metric;
628 		break;
629 
630 	case SIOCGIFMTU:
631 		ifr->ifr_mtu = ifp->if_mtu;
632 		break;
633 
634 	case SIOCGIFPHYS:
635 		ifr->ifr_phys = ifp->if_physical;
636 		break;
637 
638 	case SIOCSIFFLAGS:
639 		error = suser(p);
640 		if (error)
641 			return (error);
642 		ifr->ifr_prevflags = ifp->if_flags;
643 		if (ifp->if_flags & IFF_SMART) {
644 			/* Smart drivers twiddle their own routes */
645 		} else if (ifp->if_flags & IFF_UP &&
646 		    (ifr->ifr_flags & IFF_UP) == 0) {
647 			int s = splimp();
648 			if_down(ifp);
649 			splx(s);
650 		} else if (ifr->ifr_flags & IFF_UP &&
651 		    (ifp->if_flags & IFF_UP) == 0) {
652 			int s = splimp();
653 			if_up(ifp);
654 			splx(s);
655 		}
656 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
657 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
658 		if (ifp->if_ioctl)
659 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
660 		getmicrotime(&ifp->if_lastchange);
661 		break;
662 
663 	case SIOCSIFMETRIC:
664 		error = suser(p);
665 		if (error)
666 			return (error);
667 		ifp->if_metric = ifr->ifr_metric;
668 		getmicrotime(&ifp->if_lastchange);
669 		break;
670 
671 	case SIOCSIFPHYS:
672 		error = suser(p);
673 		if (error)
674 			return error;
675 		if (!ifp->if_ioctl)
676 		        return EOPNOTSUPP;
677 		error = (*ifp->if_ioctl)(ifp, cmd, data);
678 		if (error == 0)
679 			getmicrotime(&ifp->if_lastchange);
680 		return(error);
681 
682 	case SIOCSIFMTU:
683 		error = suser(p);
684 		if (error)
685 			return (error);
686 		if (ifp->if_ioctl == NULL)
687 			return (EOPNOTSUPP);
688 		/*
689 		 * 72 was chosen below because it is the size of a TCP/IP
690 		 * header (40) + the minimum mss (32).
691 		 */
692 		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
693 			return (EINVAL);
694 		error = (*ifp->if_ioctl)(ifp, cmd, data);
695 		if (error == 0)
696 			getmicrotime(&ifp->if_lastchange);
697 		return(error);
698 
699 	case SIOCADDMULTI:
700 	case SIOCDELMULTI:
701 		error = suser(p);
702 		if (error)
703 			return (error);
704 
705 		/* Don't allow group membership on non-multicast interfaces. */
706 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
707 			return EOPNOTSUPP;
708 
709 		/* Don't let users screw up protocols' entries. */
710 		if (ifr->ifr_addr.sa_family != AF_LINK)
711 			return EINVAL;
712 
713 		if (cmd == SIOCADDMULTI) {
714 			struct ifmultiaddr *ifma;
715 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
716 		} else {
717 			error = if_delmulti(ifp, &ifr->ifr_addr);
718 		}
719 		if (error == 0)
720 			getmicrotime(&ifp->if_lastchange);
721 		return error;
722 
723         case SIOCSIFMEDIA:
724 	case SIOCSIFGENERIC:
725 		error = suser(p);
726 		if (error)
727 			return (error);
728 		if (ifp->if_ioctl == 0)
729 			return (EOPNOTSUPP);
730 		error = (*ifp->if_ioctl)(ifp, cmd, data);
731 		if (error == 0)
732 			getmicrotime(&ifp->if_lastchange);
733 		return error;
734 
735 	case SIOCGIFSTATUS:
736 		ifs = (struct ifstat *)data;
737 		ifs->ascii[0] = '\0';
738 
739 	case SIOCGIFMEDIA:
740 	case SIOCGIFGENERIC:
741 		if (ifp->if_ioctl == 0)
742 			return (EOPNOTSUPP);
743 		return ((*ifp->if_ioctl)(ifp, cmd, data));
744 
745 	default:
746 		if (so->so_proto == 0)
747 			return (EOPNOTSUPP);
748 #ifndef COMPAT_43
749 		return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
750 								 data,
751 								 ifp, p));
752 #else
753 	    {
754 		int ocmd = cmd;
755 
756 		switch (cmd) {
757 
758 		case SIOCSIFDSTADDR:
759 		case SIOCSIFADDR:
760 		case SIOCSIFBRDADDR:
761 		case SIOCSIFNETMASK:
762 #if BYTE_ORDER != BIG_ENDIAN
763 			if (ifr->ifr_addr.sa_family == 0 &&
764 			    ifr->ifr_addr.sa_len < 16) {
765 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
766 				ifr->ifr_addr.sa_len = 16;
767 			}
768 #else
769 			if (ifr->ifr_addr.sa_len == 0)
770 				ifr->ifr_addr.sa_len = 16;
771 #endif
772 			break;
773 
774 		case OSIOCGIFADDR:
775 			cmd = SIOCGIFADDR;
776 			break;
777 
778 		case OSIOCGIFDSTADDR:
779 			cmd = SIOCGIFDSTADDR;
780 			break;
781 
782 		case OSIOCGIFBRDADDR:
783 			cmd = SIOCGIFBRDADDR;
784 			break;
785 
786 		case OSIOCGIFNETMASK:
787 			cmd = SIOCGIFNETMASK;
788 		}
789 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
790 								   cmd,
791 								   data,
792 								   ifp, p));
793 		switch (ocmd) {
794 
795 		case OSIOCGIFADDR:
796 		case OSIOCGIFDSTADDR:
797 		case OSIOCGIFBRDADDR:
798 		case OSIOCGIFNETMASK:
799 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
800 		}
801 		return (error);
802 
803 	    }
804 #endif
805 	}
806 	return (0);
807 }
808 
809 /*
810  * Set/clear promiscuous mode on interface ifp based on the truth value
811  * of pswitch.  The calls are reference counted so that only the first
812  * "on" request actually has an effect, as does the final "off" request.
813  * Results are undefined if the "off" and "on" requests are not matched.
814  */
815 int
816 ifpromisc(ifp, pswitch)
817 	struct ifnet *ifp;
818 	int pswitch;
819 {
820 	struct ifreq ifr;
821 	int error;
822 
823 	if (pswitch) {
824 		/*
825 		 * If the device is not configured up, we cannot put it in
826 		 * promiscuous mode.
827 		 */
828 		if ((ifp->if_flags & IFF_UP) == 0)
829 			return (ENETDOWN);
830 		if (ifp->if_pcount++ != 0)
831 			return (0);
832 		ifp->if_flags |= IFF_PROMISC;
833 		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
834 		    ifp->if_name, ifp->if_unit);
835 	} else {
836 		if (--ifp->if_pcount > 0)
837 			return (0);
838 		ifp->if_flags &= ~IFF_PROMISC;
839 	}
840 	ifr.ifr_flags = ifp->if_flags;
841 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
842 	if (error == 0)
843 		rt_ifmsg(ifp);
844 	return error;
845 }
846 
847 /*
848  * Return interface configuration
849  * of system.  List may be used
850  * in later ioctl's (above) to get
851  * other information.
852  */
853 /*ARGSUSED*/
854 static int
855 ifconf(cmd, data)
856 	u_long cmd;
857 	caddr_t data;
858 {
859 	register struct ifconf *ifc = (struct ifconf *)data;
860 	register struct ifnet *ifp = ifnet.tqh_first;
861 	register struct ifaddr *ifa;
862 	struct ifreq ifr, *ifrp;
863 	int space = ifc->ifc_len, error = 0;
864 
865 	ifrp = ifc->ifc_req;
866 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
867 		char workbuf[64];
868 		int ifnlen, addrs;
869 
870 		ifnlen = snprintf(workbuf, sizeof(workbuf),
871 		    "%s%d", ifp->if_name, ifp->if_unit);
872 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
873 			error = ENAMETOOLONG;
874 		} else {
875 			strcpy(ifr.ifr_name, workbuf);
876 		}
877 
878 		addrs = 0;
879 		ifa = ifp->if_addrhead.tqh_first;
880 		for ( ; space > sizeof (ifr) && ifa;
881 		    ifa = ifa->ifa_link.tqe_next) {
882 			register struct sockaddr *sa = ifa->ifa_addr;
883 			if (curproc->p_prison && prison_if(curproc, sa))
884 				continue;
885 			addrs++;
886 #ifdef COMPAT_43
887 			if (cmd == OSIOCGIFCONF) {
888 				struct osockaddr *osa =
889 					 (struct osockaddr *)&ifr.ifr_addr;
890 				ifr.ifr_addr = *sa;
891 				osa->sa_family = sa->sa_family;
892 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
893 						sizeof (ifr));
894 				ifrp++;
895 			} else
896 #endif
897 			if (sa->sa_len <= sizeof(*sa)) {
898 				ifr.ifr_addr = *sa;
899 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
900 						sizeof (ifr));
901 				ifrp++;
902 			} else {
903 				space -= sa->sa_len - sizeof(*sa);
904 				if (space < sizeof (ifr))
905 					break;
906 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
907 						sizeof (ifr.ifr_name));
908 				if (error == 0)
909 				    error = copyout((caddr_t)sa,
910 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
911 				ifrp = (struct ifreq *)
912 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
913 			}
914 			if (error)
915 				break;
916 			space -= sizeof (ifr);
917 		}
918 		if (!addrs) {
919 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
920 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
921 			    sizeof (ifr));
922 			if (error)
923 				break;
924 			space -= sizeof (ifr), ifrp++;
925 		}
926 	}
927 	ifc->ifc_len -= space;
928 	return (error);
929 }
930 
931 /*
932  * Just like if_promisc(), but for all-multicast-reception mode.
933  */
934 int
935 if_allmulti(ifp, onswitch)
936 	struct ifnet *ifp;
937 	int onswitch;
938 {
939 	int error = 0;
940 	int s = splimp();
941 
942 	if (onswitch) {
943 		if (ifp->if_amcount++ == 0) {
944 			ifp->if_flags |= IFF_ALLMULTI;
945 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
946 		}
947 	} else {
948 		if (ifp->if_amcount > 1) {
949 			ifp->if_amcount--;
950 		} else {
951 			ifp->if_amcount = 0;
952 			ifp->if_flags &= ~IFF_ALLMULTI;
953 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
954 		}
955 	}
956 	splx(s);
957 
958 	if (error == 0)
959 		rt_ifmsg(ifp);
960 	return error;
961 }
962 
963 /*
964  * Add a multicast listenership to the interface in question.
965  * The link layer provides a routine which converts
966  */
967 int
968 if_addmulti(ifp, sa, retifma)
969 	struct ifnet *ifp;	/* interface to manipulate */
970 	struct sockaddr *sa;	/* address to add */
971 	struct ifmultiaddr **retifma;
972 {
973 	struct sockaddr *llsa, *dupsa;
974 	int error, s;
975 	struct ifmultiaddr *ifma;
976 
977 	/*
978 	 * If the matching multicast address already exists
979 	 * then don't add a new one, just add a reference
980 	 */
981 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
982 	     ifma = ifma->ifma_link.le_next) {
983 		if (equal(sa, ifma->ifma_addr)) {
984 			ifma->ifma_refcount++;
985 			if (retifma)
986 				*retifma = ifma;
987 			return 0;
988 		}
989 	}
990 
991 	/*
992 	 * Give the link layer a chance to accept/reject it, and also
993 	 * find out which AF_LINK address this maps to, if it isn't one
994 	 * already.
995 	 */
996 	if (ifp->if_resolvemulti) {
997 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
998 		if (error) return error;
999 	} else {
1000 		llsa = 0;
1001 	}
1002 
1003 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1004 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1005 	bcopy(sa, dupsa, sa->sa_len);
1006 
1007 	ifma->ifma_addr = dupsa;
1008 	ifma->ifma_lladdr = llsa;
1009 	ifma->ifma_ifp = ifp;
1010 	ifma->ifma_refcount = 1;
1011 	ifma->ifma_protospec = 0;
1012 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1013 
1014 	/*
1015 	 * Some network interfaces can scan the address list at
1016 	 * interrupt time; lock them out.
1017 	 */
1018 	s = splimp();
1019 	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1020 	splx(s);
1021 	*retifma = ifma;
1022 
1023 	if (llsa != 0) {
1024 		for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1025 		     ifma = ifma->ifma_link.le_next) {
1026 			if (equal(ifma->ifma_addr, llsa))
1027 				break;
1028 		}
1029 		if (ifma) {
1030 			ifma->ifma_refcount++;
1031 		} else {
1032 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1033 			       M_IFMADDR, M_WAITOK);
1034 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1035 			       M_IFMADDR, M_WAITOK);
1036 			bcopy(llsa, dupsa, llsa->sa_len);
1037 			ifma->ifma_addr = dupsa;
1038 			ifma->ifma_ifp = ifp;
1039 			ifma->ifma_refcount = 1;
1040 			s = splimp();
1041 			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1042 			splx(s);
1043 		}
1044 	}
1045 	/*
1046 	 * We are certain we have added something, so call down to the
1047 	 * interface to let them know about it.
1048 	 */
1049 	s = splimp();
1050 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1051 	splx(s);
1052 
1053 	return 0;
1054 }
1055 
1056 /*
1057  * Remove a reference to a multicast address on this interface.  Yell
1058  * if the request does not match an existing membership.
1059  */
1060 int
1061 if_delmulti(ifp, sa)
1062 	struct ifnet *ifp;
1063 	struct sockaddr *sa;
1064 {
1065 	struct ifmultiaddr *ifma;
1066 	int s;
1067 
1068 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1069 	     ifma = ifma->ifma_link.le_next)
1070 		if (equal(sa, ifma->ifma_addr))
1071 			break;
1072 	if (ifma == 0)
1073 		return ENOENT;
1074 
1075 	if (ifma->ifma_refcount > 1) {
1076 		ifma->ifma_refcount--;
1077 		return 0;
1078 	}
1079 
1080 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
1081 	sa = ifma->ifma_lladdr;
1082 	s = splimp();
1083 	LIST_REMOVE(ifma, ifma_link);
1084 	splx(s);
1085 	free(ifma->ifma_addr, M_IFMADDR);
1086 	free(ifma, M_IFMADDR);
1087 	if (sa == 0)
1088 		return 0;
1089 
1090 	/*
1091 	 * Now look for the link-layer address which corresponds to
1092 	 * this network address.  It had been squirreled away in
1093 	 * ifma->ifma_lladdr for this purpose (so we don't have
1094 	 * to call ifp->if_resolvemulti() again), and we saved that
1095 	 * value in sa above.  If some nasty deleted the
1096 	 * link-layer address out from underneath us, we can deal because
1097 	 * the address we stored was is not the same as the one which was
1098 	 * in the record for the link-layer address.  (So we don't complain
1099 	 * in that case.)
1100 	 */
1101 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1102 	     ifma = ifma->ifma_link.le_next)
1103 		if (equal(sa, ifma->ifma_addr))
1104 			break;
1105 	if (ifma == 0)
1106 		return 0;
1107 
1108 	if (ifma->ifma_refcount > 1) {
1109 		ifma->ifma_refcount--;
1110 		return 0;
1111 	}
1112 
1113 	s = splimp();
1114 	LIST_REMOVE(ifma, ifma_link);
1115 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1116 	splx(s);
1117 	free(ifma->ifma_addr, M_IFMADDR);
1118 	free(sa, M_IFMADDR);
1119 	free(ifma, M_IFMADDR);
1120 
1121 	return 0;
1122 }
1123 
1124 struct ifmultiaddr *
1125 ifmaof_ifpforaddr(sa, ifp)
1126 	struct sockaddr *sa;
1127 	struct ifnet *ifp;
1128 {
1129 	struct ifmultiaddr *ifma;
1130 
1131 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1132 	     ifma = ifma->ifma_link.le_next)
1133 		if (equal(ifma->ifma_addr, sa))
1134 			break;
1135 
1136 	return ifma;
1137 }
1138 
1139 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1140 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1141