xref: /freebsd/sys/net/if.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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  * $FreeBSD$
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 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
689 			return (EINVAL);
690 		error = (*ifp->if_ioctl)(ifp, cmd, data);
691 		if (error == 0)
692 			getmicrotime(&ifp->if_lastchange);
693 		return(error);
694 
695 	case SIOCADDMULTI:
696 	case SIOCDELMULTI:
697 		error = suser(p);
698 		if (error)
699 			return (error);
700 
701 		/* Don't allow group membership on non-multicast interfaces. */
702 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
703 			return EOPNOTSUPP;
704 
705 		/* Don't let users screw up protocols' entries. */
706 		if (ifr->ifr_addr.sa_family != AF_LINK)
707 			return EINVAL;
708 
709 		if (cmd == SIOCADDMULTI) {
710 			struct ifmultiaddr *ifma;
711 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
712 		} else {
713 			error = if_delmulti(ifp, &ifr->ifr_addr);
714 		}
715 		if (error == 0)
716 			getmicrotime(&ifp->if_lastchange);
717 		return error;
718 
719         case SIOCSIFMEDIA:
720 	case SIOCSIFGENERIC:
721 		error = suser(p);
722 		if (error)
723 			return (error);
724 		if (ifp->if_ioctl == 0)
725 			return (EOPNOTSUPP);
726 		error = (*ifp->if_ioctl)(ifp, cmd, data);
727 		if (error == 0)
728 			getmicrotime(&ifp->if_lastchange);
729 		return error;
730 
731 	case SIOCGIFSTATUS:
732 		ifs = (struct ifstat *)data;
733 		ifs->ascii[0] = '\0';
734 
735 	case SIOCGIFMEDIA:
736 	case SIOCGIFGENERIC:
737 		if (ifp->if_ioctl == 0)
738 			return (EOPNOTSUPP);
739 		return ((*ifp->if_ioctl)(ifp, cmd, data));
740 
741 	default:
742 		if (so->so_proto == 0)
743 			return (EOPNOTSUPP);
744 #ifndef COMPAT_43
745 		return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
746 								 data,
747 								 ifp, p));
748 #else
749 	    {
750 		int ocmd = cmd;
751 
752 		switch (cmd) {
753 
754 		case SIOCSIFDSTADDR:
755 		case SIOCSIFADDR:
756 		case SIOCSIFBRDADDR:
757 		case SIOCSIFNETMASK:
758 #if BYTE_ORDER != BIG_ENDIAN
759 			if (ifr->ifr_addr.sa_family == 0 &&
760 			    ifr->ifr_addr.sa_len < 16) {
761 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
762 				ifr->ifr_addr.sa_len = 16;
763 			}
764 #else
765 			if (ifr->ifr_addr.sa_len == 0)
766 				ifr->ifr_addr.sa_len = 16;
767 #endif
768 			break;
769 
770 		case OSIOCGIFADDR:
771 			cmd = SIOCGIFADDR;
772 			break;
773 
774 		case OSIOCGIFDSTADDR:
775 			cmd = SIOCGIFDSTADDR;
776 			break;
777 
778 		case OSIOCGIFBRDADDR:
779 			cmd = SIOCGIFBRDADDR;
780 			break;
781 
782 		case OSIOCGIFNETMASK:
783 			cmd = SIOCGIFNETMASK;
784 		}
785 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
786 								   cmd,
787 								   data,
788 								   ifp, p));
789 		switch (ocmd) {
790 
791 		case OSIOCGIFADDR:
792 		case OSIOCGIFDSTADDR:
793 		case OSIOCGIFBRDADDR:
794 		case OSIOCGIFNETMASK:
795 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
796 		}
797 		return (error);
798 
799 	    }
800 #endif
801 	}
802 	return (0);
803 }
804 
805 /*
806  * Set/clear promiscuous mode on interface ifp based on the truth value
807  * of pswitch.  The calls are reference counted so that only the first
808  * "on" request actually has an effect, as does the final "off" request.
809  * Results are undefined if the "off" and "on" requests are not matched.
810  */
811 int
812 ifpromisc(ifp, pswitch)
813 	struct ifnet *ifp;
814 	int pswitch;
815 {
816 	struct ifreq ifr;
817 	int error;
818 
819 	if (pswitch) {
820 		/*
821 		 * If the device is not configured up, we cannot put it in
822 		 * promiscuous mode.
823 		 */
824 		if ((ifp->if_flags & IFF_UP) == 0)
825 			return (ENETDOWN);
826 		if (ifp->if_pcount++ != 0)
827 			return (0);
828 		ifp->if_flags |= IFF_PROMISC;
829 		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
830 		    ifp->if_name, ifp->if_unit);
831 	} else {
832 		if (--ifp->if_pcount > 0)
833 			return (0);
834 		ifp->if_flags &= ~IFF_PROMISC;
835 		log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
836 		    ifp->if_name, ifp->if_unit);
837 	}
838 	ifr.ifr_flags = ifp->if_flags;
839 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
840 	if (error == 0)
841 		rt_ifmsg(ifp);
842 	return error;
843 }
844 
845 /*
846  * Return interface configuration
847  * of system.  List may be used
848  * in later ioctl's (above) to get
849  * other information.
850  */
851 /*ARGSUSED*/
852 static int
853 ifconf(cmd, data)
854 	u_long cmd;
855 	caddr_t data;
856 {
857 	register struct ifconf *ifc = (struct ifconf *)data;
858 	register struct ifnet *ifp = ifnet.tqh_first;
859 	register struct ifaddr *ifa;
860 	struct ifreq ifr, *ifrp;
861 	int space = ifc->ifc_len, error = 0;
862 
863 	ifrp = ifc->ifc_req;
864 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
865 		char workbuf[64];
866 		int ifnlen, addrs;
867 
868 		ifnlen = snprintf(workbuf, sizeof(workbuf),
869 		    "%s%d", ifp->if_name, ifp->if_unit);
870 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
871 			error = ENAMETOOLONG;
872 		} else {
873 			strcpy(ifr.ifr_name, workbuf);
874 		}
875 
876 		addrs = 0;
877 		ifa = ifp->if_addrhead.tqh_first;
878 		for ( ; space > sizeof (ifr) && ifa;
879 		    ifa = ifa->ifa_link.tqe_next) {
880 			register struct sockaddr *sa = ifa->ifa_addr;
881 			if (curproc->p_prison && prison_if(curproc, sa))
882 				continue;
883 			addrs++;
884 #ifdef COMPAT_43
885 			if (cmd == OSIOCGIFCONF) {
886 				struct osockaddr *osa =
887 					 (struct osockaddr *)&ifr.ifr_addr;
888 				ifr.ifr_addr = *sa;
889 				osa->sa_family = sa->sa_family;
890 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
891 						sizeof (ifr));
892 				ifrp++;
893 			} else
894 #endif
895 			if (sa->sa_len <= sizeof(*sa)) {
896 				ifr.ifr_addr = *sa;
897 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
898 						sizeof (ifr));
899 				ifrp++;
900 			} else {
901 				space -= sa->sa_len - sizeof(*sa);
902 				if (space < sizeof (ifr))
903 					break;
904 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
905 						sizeof (ifr.ifr_name));
906 				if (error == 0)
907 				    error = copyout((caddr_t)sa,
908 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
909 				ifrp = (struct ifreq *)
910 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
911 			}
912 			if (error)
913 				break;
914 			space -= sizeof (ifr);
915 		}
916 		if (!addrs) {
917 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
918 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
919 			    sizeof (ifr));
920 			if (error)
921 				break;
922 			space -= sizeof (ifr), ifrp++;
923 		}
924 	}
925 	ifc->ifc_len -= space;
926 	return (error);
927 }
928 
929 /*
930  * Just like if_promisc(), but for all-multicast-reception mode.
931  */
932 int
933 if_allmulti(ifp, onswitch)
934 	struct ifnet *ifp;
935 	int onswitch;
936 {
937 	int error = 0;
938 	int s = splimp();
939 
940 	if (onswitch) {
941 		if (ifp->if_amcount++ == 0) {
942 			ifp->if_flags |= IFF_ALLMULTI;
943 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
944 		}
945 	} else {
946 		if (ifp->if_amcount > 1) {
947 			ifp->if_amcount--;
948 		} else {
949 			ifp->if_amcount = 0;
950 			ifp->if_flags &= ~IFF_ALLMULTI;
951 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
952 		}
953 	}
954 	splx(s);
955 
956 	if (error == 0)
957 		rt_ifmsg(ifp);
958 	return error;
959 }
960 
961 /*
962  * Add a multicast listenership to the interface in question.
963  * The link layer provides a routine which converts
964  */
965 int
966 if_addmulti(ifp, sa, retifma)
967 	struct ifnet *ifp;	/* interface to manipulate */
968 	struct sockaddr *sa;	/* address to add */
969 	struct ifmultiaddr **retifma;
970 {
971 	struct sockaddr *llsa, *dupsa;
972 	int error, s;
973 	struct ifmultiaddr *ifma;
974 
975 	/*
976 	 * If the matching multicast address already exists
977 	 * then don't add a new one, just add a reference
978 	 */
979 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
980 	     ifma = ifma->ifma_link.le_next) {
981 		if (equal(sa, ifma->ifma_addr)) {
982 			ifma->ifma_refcount++;
983 			if (retifma)
984 				*retifma = ifma;
985 			return 0;
986 		}
987 	}
988 
989 	/*
990 	 * Give the link layer a chance to accept/reject it, and also
991 	 * find out which AF_LINK address this maps to, if it isn't one
992 	 * already.
993 	 */
994 	if (ifp->if_resolvemulti) {
995 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
996 		if (error) return error;
997 	} else {
998 		llsa = 0;
999 	}
1000 
1001 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1002 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1003 	bcopy(sa, dupsa, sa->sa_len);
1004 
1005 	ifma->ifma_addr = dupsa;
1006 	ifma->ifma_lladdr = llsa;
1007 	ifma->ifma_ifp = ifp;
1008 	ifma->ifma_refcount = 1;
1009 	ifma->ifma_protospec = 0;
1010 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1011 
1012 	/*
1013 	 * Some network interfaces can scan the address list at
1014 	 * interrupt time; lock them out.
1015 	 */
1016 	s = splimp();
1017 	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1018 	splx(s);
1019 	*retifma = ifma;
1020 
1021 	if (llsa != 0) {
1022 		for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1023 		     ifma = ifma->ifma_link.le_next) {
1024 			if (equal(ifma->ifma_addr, llsa))
1025 				break;
1026 		}
1027 		if (ifma) {
1028 			ifma->ifma_refcount++;
1029 		} else {
1030 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1031 			       M_IFMADDR, M_WAITOK);
1032 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1033 			       M_IFMADDR, M_WAITOK);
1034 			bcopy(llsa, dupsa, llsa->sa_len);
1035 			ifma->ifma_addr = dupsa;
1036 			ifma->ifma_ifp = ifp;
1037 			ifma->ifma_refcount = 1;
1038 			s = splimp();
1039 			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1040 			splx(s);
1041 		}
1042 	}
1043 	/*
1044 	 * We are certain we have added something, so call down to the
1045 	 * interface to let them know about it.
1046 	 */
1047 	s = splimp();
1048 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1049 	splx(s);
1050 
1051 	return 0;
1052 }
1053 
1054 /*
1055  * Remove a reference to a multicast address on this interface.  Yell
1056  * if the request does not match an existing membership.
1057  */
1058 int
1059 if_delmulti(ifp, sa)
1060 	struct ifnet *ifp;
1061 	struct sockaddr *sa;
1062 {
1063 	struct ifmultiaddr *ifma;
1064 	int s;
1065 
1066 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1067 	     ifma = ifma->ifma_link.le_next)
1068 		if (equal(sa, ifma->ifma_addr))
1069 			break;
1070 	if (ifma == 0)
1071 		return ENOENT;
1072 
1073 	if (ifma->ifma_refcount > 1) {
1074 		ifma->ifma_refcount--;
1075 		return 0;
1076 	}
1077 
1078 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
1079 	sa = ifma->ifma_lladdr;
1080 	s = splimp();
1081 	LIST_REMOVE(ifma, ifma_link);
1082 	splx(s);
1083 	free(ifma->ifma_addr, M_IFMADDR);
1084 	free(ifma, M_IFMADDR);
1085 	if (sa == 0)
1086 		return 0;
1087 
1088 	/*
1089 	 * Now look for the link-layer address which corresponds to
1090 	 * this network address.  It had been squirreled away in
1091 	 * ifma->ifma_lladdr for this purpose (so we don't have
1092 	 * to call ifp->if_resolvemulti() again), and we saved that
1093 	 * value in sa above.  If some nasty deleted the
1094 	 * link-layer address out from underneath us, we can deal because
1095 	 * the address we stored was is not the same as the one which was
1096 	 * in the record for the link-layer address.  (So we don't complain
1097 	 * in that case.)
1098 	 */
1099 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1100 	     ifma = ifma->ifma_link.le_next)
1101 		if (equal(sa, ifma->ifma_addr))
1102 			break;
1103 	if (ifma == 0)
1104 		return 0;
1105 
1106 	if (ifma->ifma_refcount > 1) {
1107 		ifma->ifma_refcount--;
1108 		return 0;
1109 	}
1110 
1111 	s = splimp();
1112 	LIST_REMOVE(ifma, ifma_link);
1113 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1114 	splx(s);
1115 	free(ifma->ifma_addr, M_IFMADDR);
1116 	free(sa, M_IFMADDR);
1117 	free(ifma, M_IFMADDR);
1118 
1119 	return 0;
1120 }
1121 
1122 struct ifmultiaddr *
1123 ifmaof_ifpforaddr(sa, ifp)
1124 	struct sockaddr *sa;
1125 	struct ifnet *ifp;
1126 {
1127 	struct ifmultiaddr *ifma;
1128 
1129 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1130 	     ifma = ifma->ifma_link.le_next)
1131 		if (equal(ifma->ifma_addr, sa))
1132 			break;
1133 
1134 	return ifma;
1135 }
1136 
1137 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1138 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1139