xref: /freebsd/sys/net/if.c (revision 230f8c40e55e3462e90151e30f61bd0fdd4dcda3)
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$
35  */
36 
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/mbuf.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/protosw.h>
45 #include <sys/kernel.h>
46 #include <sys/ioctl.h>
47 #include <sys/errno.h>
48 #include <sys/syslog.h>
49 #include <sys/sysctl.h>
50 
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/radix.h>
55 
56 /*
57  * System initialization
58  */
59 
60 static int ifconf __P((int, 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 
69 int	ifqmaxlen = IFQ_MAXLEN;
70 struct	ifnethead ifnet;	/* depend on static init XXX */
71 
72 /*
73  * Network interface utility routines.
74  *
75  * Routines with ifa_ifwith* names take sockaddr *'s as
76  * parameters.
77  *
78  * This routine assumes that it will be called at splimp() or higher.
79  */
80 /* ARGSUSED*/
81 void
82 ifinit(dummy)
83 	void *dummy;
84 {
85 	register struct ifnet *ifp;
86 
87 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
88 		if (ifp->if_snd.ifq_maxlen == 0)
89 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
90 	if_slowtimo(0);
91 }
92 
93 int if_index = 0;
94 struct ifaddr **ifnet_addrs;
95 
96 
97 /*
98  * Attach an interface to the
99  * list of "active" interfaces.
100  */
101 void
102 if_attach(ifp)
103 	struct ifnet *ifp;
104 {
105 	unsigned socksize, ifasize;
106 	int namelen, masklen;
107 	char workbuf[64];
108 	register struct sockaddr_dl *sdl;
109 	register struct ifaddr *ifa;
110 	static int if_indexlim = 8;
111 	static int inited;
112 
113 	if (!inited) {
114 		TAILQ_INIT(&ifnet);
115 		inited = 1;
116 	}
117 
118 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
119 	ifp->if_index = ++if_index;
120 	/*
121 	 * XXX -
122 	 * The old code would work if the interface passed a pre-existing
123 	 * chain of ifaddrs to this code.  We don't trust our callers to
124 	 * properly initialize the tailq, however, so we no longer allow
125 	 * this unlikely case.
126 	 */
127 	TAILQ_INIT(&ifp->if_addrhead);
128 	LIST_INIT(&ifp->if_multiaddrs);
129 	microtime(&ifp->if_lastchange);
130 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
131 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
132 		struct ifaddr **q = (struct ifaddr **)
133 					malloc(n, M_IFADDR, M_WAITOK);
134 		bzero((caddr_t)q, n);
135 		if (ifnet_addrs) {
136 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
137 			free((caddr_t)ifnet_addrs, M_IFADDR);
138 		}
139 		ifnet_addrs = q;
140 	}
141 	/*
142 	 * create a Link Level name for this device
143 	 */
144 	namelen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
145 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
146 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
147 	socksize = masklen + ifp->if_addrlen;
148 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
149 	socksize = ROUNDUP(socksize);
150 	if (socksize < sizeof(*sdl))
151 		socksize = sizeof(*sdl);
152 	ifasize = sizeof(*ifa) + 2 * socksize;
153 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
154 	if (ifa) {
155 		bzero((caddr_t)ifa, ifasize);
156 		sdl = (struct sockaddr_dl *)(ifa + 1);
157 		sdl->sdl_len = socksize;
158 		sdl->sdl_family = AF_LINK;
159 		bcopy(workbuf, sdl->sdl_data, namelen);
160 		sdl->sdl_nlen = namelen;
161 		sdl->sdl_index = ifp->if_index;
162 		sdl->sdl_type = ifp->if_type;
163 		ifnet_addrs[if_index - 1] = ifa;
164 		ifa->ifa_ifp = ifp;
165 		ifa->ifa_rtrequest = link_rtrequest;
166 		ifa->ifa_addr = (struct sockaddr *)sdl;
167 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
168 		ifa->ifa_netmask = (struct sockaddr *)sdl;
169 		sdl->sdl_len = masklen;
170 		while (namelen != 0)
171 			sdl->sdl_data[--namelen] = 0xff;
172 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
173 	}
174 }
175 /*
176  * Locate an interface based on a complete address.
177  */
178 /*ARGSUSED*/
179 struct ifaddr *
180 ifa_ifwithaddr(addr)
181 	register struct sockaddr *addr;
182 {
183 	register struct ifnet *ifp;
184 	register struct ifaddr *ifa;
185 
186 #define	equal(a1, a2) \
187   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
188 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
189 	    for (ifa = ifp->if_addrhead.tqh_first; ifa;
190 		 ifa = ifa->ifa_link.tqe_next) {
191 		if (ifa->ifa_addr->sa_family != addr->sa_family)
192 			continue;
193 		if (equal(addr, ifa->ifa_addr))
194 			return (ifa);
195 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
196 		    equal(ifa->ifa_broadaddr, addr))
197 			return (ifa);
198 	}
199 	return ((struct ifaddr *)0);
200 }
201 /*
202  * Locate the point to point interface with a given destination address.
203  */
204 /*ARGSUSED*/
205 struct ifaddr *
206 ifa_ifwithdstaddr(addr)
207 	register struct sockaddr *addr;
208 {
209 	register struct ifnet *ifp;
210 	register struct ifaddr *ifa;
211 
212 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
213 	    if (ifp->if_flags & IFF_POINTOPOINT)
214 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
215 		     ifa = ifa->ifa_link.tqe_next) {
216 			if (ifa->ifa_addr->sa_family != addr->sa_family)
217 				continue;
218 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
219 				return (ifa);
220 	}
221 	return ((struct ifaddr *)0);
222 }
223 
224 /*
225  * Find an interface on a specific network.  If many, choice
226  * is most specific found.
227  */
228 struct ifaddr *
229 ifa_ifwithnet(addr)
230 	struct sockaddr *addr;
231 {
232 	register struct ifnet *ifp;
233 	register struct ifaddr *ifa;
234 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
235 	u_int af = addr->sa_family;
236 	char *addr_data = addr->sa_data, *cplim;
237 
238 	if (af == AF_LINK) {
239 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
240 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
241 		return (ifnet_addrs[sdl->sdl_index - 1]);
242 	}
243 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
244 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
245 		     ifa = ifa->ifa_link.tqe_next) {
246 			register char *cp, *cp2, *cp3;
247 
248 			if (ifa->ifa_addr->sa_family != af)
249 				next: continue;
250 			if (ifp->if_flags & IFF_POINTOPOINT) {
251 				if (ifa->ifa_dstaddr != 0
252 				    && equal(addr, ifa->ifa_dstaddr))
253  					return (ifa);
254 			} else {
255 				if (ifa->ifa_netmask == 0)
256 					continue;
257 				cp = addr_data;
258 				cp2 = ifa->ifa_addr->sa_data;
259 				cp3 = ifa->ifa_netmask->sa_data;
260 				cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
261 				while (cp3 < cplim)
262 					if ((*cp++ ^ *cp2++) & *cp3++)
263 						goto next;
264 				if (ifa_maybe == 0 ||
265 				    rn_refines((caddr_t)ifa->ifa_netmask,
266 				    (caddr_t)ifa_maybe->ifa_netmask))
267 					ifa_maybe = ifa;
268 			}
269 		}
270 	}
271 	return (ifa_maybe);
272 }
273 
274 /*
275  * Find an interface address specific to an interface best matching
276  * a given address.
277  */
278 struct ifaddr *
279 ifaof_ifpforaddr(addr, ifp)
280 	struct sockaddr *addr;
281 	register struct ifnet *ifp;
282 {
283 	register struct ifaddr *ifa;
284 	register char *cp, *cp2, *cp3;
285 	register char *cplim;
286 	struct ifaddr *ifa_maybe = 0;
287 	u_int af = addr->sa_family;
288 
289 	if (af >= AF_MAX)
290 		return (0);
291 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
292 	     ifa = ifa->ifa_link.tqe_next) {
293 		if (ifa->ifa_addr->sa_family != af)
294 			continue;
295 		if (ifa_maybe == 0)
296 			ifa_maybe = ifa;
297 		if (ifa->ifa_netmask == 0) {
298 			if (equal(addr, ifa->ifa_addr) ||
299 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
300 				return (ifa);
301 			continue;
302 		}
303 		if (ifp->if_flags & IFF_POINTOPOINT) {
304 			if (equal(addr, ifa->ifa_dstaddr))
305 				return (ifa);
306 		} else {
307 			cp = addr->sa_data;
308 			cp2 = ifa->ifa_addr->sa_data;
309 			cp3 = ifa->ifa_netmask->sa_data;
310 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
311 			for (; cp3 < cplim; cp3++)
312 				if ((*cp++ ^ *cp2++) & *cp3)
313 					break;
314 			if (cp3 == cplim)
315 				return (ifa);
316 		}
317 	}
318 	return (ifa_maybe);
319 }
320 
321 #include <net/route.h>
322 
323 /*
324  * Default action when installing a route with a Link Level gateway.
325  * Lookup an appropriate real ifa to point to.
326  * This should be moved to /sys/net/link.c eventually.
327  */
328 static void
329 link_rtrequest(cmd, rt, sa)
330 	int cmd;
331 	register struct rtentry *rt;
332 	struct sockaddr *sa;
333 {
334 	register struct ifaddr *ifa;
335 	struct sockaddr *dst;
336 	struct ifnet *ifp;
337 
338 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
339 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
340 		return;
341 	ifa = ifaof_ifpforaddr(dst, ifp);
342 	if (ifa) {
343 		IFAFREE(rt->rt_ifa);
344 		rt->rt_ifa = ifa;
345 		ifa->ifa_refcnt++;
346 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
347 			ifa->ifa_rtrequest(cmd, rt, sa);
348 	}
349 }
350 
351 /*
352  * Mark an interface down and notify protocols of
353  * the transition.
354  * NOTE: must be called at splnet or eqivalent.
355  */
356 void
357 if_down(ifp)
358 	register struct ifnet *ifp;
359 {
360 	register struct ifaddr *ifa;
361 
362 	ifp->if_flags &= ~IFF_UP;
363 	microtime(&ifp->if_lastchange);
364 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
365 	     ifa = ifa->ifa_link.tqe_next)
366 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
367 	if_qflush(&ifp->if_snd);
368 	rt_ifmsg(ifp);
369 }
370 
371 /*
372  * Mark an interface up and notify protocols of
373  * the transition.
374  * NOTE: must be called at splnet or eqivalent.
375  */
376 void
377 if_up(ifp)
378 	register struct ifnet *ifp;
379 {
380 	register struct ifaddr *ifa;
381 
382 	ifp->if_flags |= IFF_UP;
383 	microtime(&ifp->if_lastchange);
384 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
385 	     ifa = ifa->ifa_link.tqe_next)
386 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
387 	rt_ifmsg(ifp);
388 }
389 
390 /*
391  * Flush an interface queue.
392  */
393 static void
394 if_qflush(ifq)
395 	register struct ifqueue *ifq;
396 {
397 	register struct mbuf *m, *n;
398 
399 	n = ifq->ifq_head;
400 	while ((m = n) != 0) {
401 		n = m->m_act;
402 		m_freem(m);
403 	}
404 	ifq->ifq_head = 0;
405 	ifq->ifq_tail = 0;
406 	ifq->ifq_len = 0;
407 }
408 
409 /*
410  * Handle interface watchdog timer routines.  Called
411  * from softclock, we decrement timers (if set) and
412  * call the appropriate interface routine on expiration.
413  */
414 static void
415 if_slowtimo(arg)
416 	void *arg;
417 {
418 	register struct ifnet *ifp;
419 	int s = splimp();
420 
421 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
422 		if (ifp->if_timer == 0 || --ifp->if_timer)
423 			continue;
424 		if (ifp->if_watchdog)
425 			(*ifp->if_watchdog)(ifp);
426 	}
427 	splx(s);
428 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
429 }
430 
431 /*
432  * Map interface name to
433  * interface structure pointer.
434  */
435 struct ifnet *
436 ifunit(name)
437 	register char *name;
438 {
439 	register char *cp;
440 	register struct ifnet *ifp;
441 	int unit;
442 	unsigned len;
443 	char *ep, c;
444 
445 	for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
446 		if (*cp >= '0' && *cp <= '9')
447 			break;
448 	if (*cp == '\0' || cp == name + IFNAMSIZ)
449 		return ((struct ifnet *)0);
450 	/*
451 	 * Save first char of unit, and pointer to it,
452 	 * so we can put a null there to avoid matching
453 	 * initial substrings of interface names.
454 	 */
455 	len = cp - name + 1;
456 	c = *cp;
457 	ep = cp;
458 	for (unit = 0; *cp >= '0' && *cp <= '9'; )
459 		unit = unit * 10 + *cp++ - '0';
460 	if (*cp != '\0')
461 		return 0;	/* no trailing garbage allowed */
462 	*ep = 0;
463 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
464 		if (bcmp(ifp->if_name, name, len))
465 			continue;
466 		if (unit == ifp->if_unit)
467 			break;
468 	}
469 	*ep = c;
470 	return (ifp);
471 }
472 
473 /*
474  * Interface ioctls.
475  */
476 int
477 ifioctl(so, cmd, data, p)
478 	struct socket *so;
479 	int cmd;
480 	caddr_t data;
481 	struct proc *p;
482 {
483 	register struct ifnet *ifp;
484 	register struct ifreq *ifr;
485 	int error;
486 
487 	switch (cmd) {
488 
489 	case SIOCGIFCONF:
490 	case OSIOCGIFCONF:
491 		return (ifconf(cmd, data));
492 	}
493 	ifr = (struct ifreq *)data;
494 	ifp = ifunit(ifr->ifr_name);
495 	if (ifp == 0)
496 		return (ENXIO);
497 	switch (cmd) {
498 
499 	case SIOCGIFFLAGS:
500 		ifr->ifr_flags = ifp->if_flags;
501 		break;
502 
503 	case SIOCGIFMETRIC:
504 		ifr->ifr_metric = ifp->if_metric;
505 		break;
506 
507 	case SIOCGIFMTU:
508 		ifr->ifr_mtu = ifp->if_mtu;
509 		break;
510 
511 	case SIOCGIFPHYS:
512 		ifr->ifr_phys = ifp->if_physical;
513 		break;
514 
515 	case SIOCSIFFLAGS:
516 		error = suser(p->p_ucred, &p->p_acflag);
517 		if (error)
518 			return (error);
519 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
520 			int s = splimp();
521 			if_down(ifp);
522 			splx(s);
523 		}
524 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
525 			int s = splimp();
526 			if_up(ifp);
527 			splx(s);
528 		}
529 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
530 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
531 		if (ifp->if_ioctl)
532 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
533 		microtime(&ifp->if_lastchange);
534 		break;
535 
536 	case SIOCSIFMETRIC:
537 		error = suser(p->p_ucred, &p->p_acflag);
538 		if (error)
539 			return (error);
540 		ifp->if_metric = ifr->ifr_metric;
541 		microtime(&ifp->if_lastchange);
542 		break;
543 
544 	case SIOCSIFPHYS:
545 		error = suser(p->p_ucred, &p->p_acflag);
546 		if (error)
547 		        return error;
548 		if (!ifp->if_ioctl)
549 		        return EOPNOTSUPP;
550 		error = (*ifp->if_ioctl)(ifp, cmd, data);
551 		if (error == 0)
552 			microtime(&ifp->if_lastchange);
553 		return(error);
554 
555 	case SIOCSIFMTU:
556 		error = suser(p->p_ucred, &p->p_acflag);
557 		if (error)
558 			return (error);
559 		if (ifp->if_ioctl == NULL)
560 			return (EOPNOTSUPP);
561 		/*
562 		 * 72 was chosen below because it is the size of a TCP/IP
563 		 * header (40) + the minimum mss (32).
564 		 */
565 		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
566 			return (EINVAL);
567 		error = (*ifp->if_ioctl)(ifp, cmd, data);
568 		if (error == 0)
569 			microtime(&ifp->if_lastchange);
570 		return(error);
571 
572 	case SIOCADDMULTI:
573 	case SIOCDELMULTI:
574 		error = suser(p->p_ucred, &p->p_acflag);
575 		if (error)
576 			return (error);
577 
578 		/* Don't allow group membership on non-multicast interfaces. */
579 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
580 			return EOPNOTSUPP;
581 
582 		/* Don't let users screw up protocols' entries. */
583 		if (ifr->ifr_addr.sa_family != AF_LINK)
584 			return EINVAL;
585 
586 		if (cmd == SIOCADDMULTI) {
587 			struct ifmultiaddr *ifma;
588 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
589 		} else {
590 			error = if_delmulti(ifp, &ifr->ifr_addr);
591 		}
592 		if (error == 0)
593 			microtime(&ifp->if_lastchange);
594 		return error;
595 
596 	default:
597 		if (so->so_proto == 0)
598 			return (EOPNOTSUPP);
599 #ifndef COMPAT_43
600 		return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
601 								 data,
602 								 ifp));
603 #else
604 	    {
605 		int ocmd = cmd;
606 
607 		switch (cmd) {
608 
609 		case SIOCSIFDSTADDR:
610 		case SIOCSIFADDR:
611 		case SIOCSIFBRDADDR:
612 		case SIOCSIFNETMASK:
613 #if BYTE_ORDER != BIG_ENDIAN
614 			if (ifr->ifr_addr.sa_family == 0 &&
615 			    ifr->ifr_addr.sa_len < 16) {
616 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
617 				ifr->ifr_addr.sa_len = 16;
618 			}
619 #else
620 			if (ifr->ifr_addr.sa_len == 0)
621 				ifr->ifr_addr.sa_len = 16;
622 #endif
623 			break;
624 
625 		case OSIOCGIFADDR:
626 			cmd = SIOCGIFADDR;
627 			break;
628 
629 		case OSIOCGIFDSTADDR:
630 			cmd = SIOCGIFDSTADDR;
631 			break;
632 
633 		case OSIOCGIFBRDADDR:
634 			cmd = SIOCGIFBRDADDR;
635 			break;
636 
637 		case OSIOCGIFNETMASK:
638 			cmd = SIOCGIFNETMASK;
639 		}
640 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
641 								   cmd,
642 								   data,
643 								   ifp));
644 		switch (ocmd) {
645 
646 		case OSIOCGIFADDR:
647 		case OSIOCGIFDSTADDR:
648 		case OSIOCGIFBRDADDR:
649 		case OSIOCGIFNETMASK:
650 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
651 		}
652 		return (error);
653 
654 	    }
655 #endif
656 	}
657 	return (0);
658 }
659 
660 /*
661  * Set/clear promiscuous mode on interface ifp based on the truth value
662  * of pswitch.  The calls are reference counted so that only the first
663  * "on" request actually has an effect, as does the final "off" request.
664  * Results are undefined if the "off" and "on" requests are not matched.
665  */
666 int
667 ifpromisc(ifp, pswitch)
668 	struct ifnet *ifp;
669 	int pswitch;
670 {
671 	struct ifreq ifr;
672 	int error;
673 
674 	if (pswitch) {
675 		/*
676 		 * If the device is not configured up, we cannot put it in
677 		 * promiscuous mode.
678 		 */
679 		if ((ifp->if_flags & IFF_UP) == 0)
680 			return (ENETDOWN);
681 		if (ifp->if_pcount++ != 0)
682 			return (0);
683 		ifp->if_flags |= IFF_PROMISC;
684 		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
685 		    ifp->if_name, ifp->if_unit);
686 	} else {
687 		if (--ifp->if_pcount > 0)
688 			return (0);
689 		ifp->if_flags &= ~IFF_PROMISC;
690 	}
691 	ifr.ifr_flags = ifp->if_flags;
692 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
693 	if (error == 0)
694 		rt_ifmsg(ifp);
695 	return error;
696 }
697 
698 /*
699  * Return interface configuration
700  * of system.  List may be used
701  * in later ioctl's (above) to get
702  * other information.
703  */
704 /*ARGSUSED*/
705 static int
706 ifconf(cmd, data)
707 	int cmd;
708 	caddr_t data;
709 {
710 	register struct ifconf *ifc = (struct ifconf *)data;
711 	register struct ifnet *ifp = ifnet.tqh_first;
712 	register struct ifaddr *ifa;
713 	struct ifreq ifr, *ifrp;
714 	int space = ifc->ifc_len, error = 0;
715 
716 	ifrp = ifc->ifc_req;
717 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
718 		char workbuf[64];
719 		int ifnlen;
720 
721 		ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
722 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
723 			error = ENAMETOOLONG;
724 		} else {
725 			strcpy(ifr.ifr_name, workbuf);
726 		}
727 
728 		if ((ifa = ifp->if_addrhead.tqh_first) == 0) {
729 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
730 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
731 			    sizeof (ifr));
732 			if (error)
733 				break;
734 			space -= sizeof (ifr), ifrp++;
735 		} else
736 		    for ( ; space > sizeof (ifr) && ifa;
737 			 ifa = ifa->ifa_link.tqe_next) {
738 			register struct sockaddr *sa = ifa->ifa_addr;
739 #ifdef COMPAT_43
740 			if (cmd == OSIOCGIFCONF) {
741 				struct osockaddr *osa =
742 					 (struct osockaddr *)&ifr.ifr_addr;
743 				ifr.ifr_addr = *sa;
744 				osa->sa_family = sa->sa_family;
745 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
746 						sizeof (ifr));
747 				ifrp++;
748 			} else
749 #endif
750 			if (sa->sa_len <= sizeof(*sa)) {
751 				ifr.ifr_addr = *sa;
752 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
753 						sizeof (ifr));
754 				ifrp++;
755 			} else {
756 				space -= sa->sa_len - sizeof(*sa);
757 				if (space < sizeof (ifr))
758 					break;
759 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
760 						sizeof (ifr.ifr_name));
761 				if (error == 0)
762 				    error = copyout((caddr_t)sa,
763 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
764 				ifrp = (struct ifreq *)
765 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
766 			}
767 			if (error)
768 				break;
769 			space -= sizeof (ifr);
770 		}
771 	}
772 	ifc->ifc_len -= space;
773 	return (error);
774 }
775 
776 /*
777  * Just like if_promisc(), but for all-multicast-reception mode.
778  */
779 int
780 if_allmulti(ifp, onswitch)
781 	struct ifnet *ifp;
782 	int onswitch;
783 {
784 	int error = 0;
785 	int s = splimp();
786 
787 	if (onswitch) {
788 		if (ifp->if_amcount++ == 0) {
789 			ifp->if_flags |= IFF_ALLMULTI;
790 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
791 		}
792 	} else {
793 		if (ifp->if_amcount > 1) {
794 			ifp->if_amcount--;
795 		} else {
796 			ifp->if_amcount = 0;
797 			ifp->if_flags &= ~IFF_ALLMULTI;
798 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
799 		}
800 	}
801 	splx(s);
802 
803 	if (error == 0)
804 		rt_ifmsg(ifp);
805 	return error;
806 }
807 
808 /*
809  * Add a multicast listenership to the interface in question.
810  * The link layer provides a routine which converts
811  */
812 int
813 if_addmulti(ifp, sa, retifma)
814 	struct ifnet *ifp;	/* interface to manipulate */
815 	struct sockaddr *sa;	/* address to add */
816 	struct ifmultiaddr **retifma;
817 {
818 	struct sockaddr *llsa, *dupsa;
819 	int error, s;
820 	struct ifmultiaddr *ifma;
821 
822 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
823 	     ifma = ifma->ifma_link.le_next) {
824 		if (equal(sa, ifma->ifma_addr))
825 			break;
826 	}
827 
828 	if (ifma) {
829 		ifma->ifma_refcount++;
830 		if (retifma) *retifma = ifma;
831 		return 0;
832 	}
833 
834 	/*
835 	 * Give the link layer a chance to accept/reject it, and also
836 	 * find out which AF_LINK address this maps to, if it isn't one
837 	 * already.
838 	 */
839 	if (ifp->if_resolvemulti) {
840 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
841 		if (error) return error;
842 	} else {
843 		llsa = 0;
844 	}
845 
846 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
847 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
848 	bcopy(sa, dupsa, sa->sa_len);
849 
850 	ifma->ifma_addr = dupsa;
851 	ifma->ifma_lladdr = llsa;
852 	ifma->ifma_ifp = ifp;
853 	ifma->ifma_refcount = 1;
854 	ifma->ifma_protospec = 0;
855 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
856 
857 	/*
858 	 * Some network interfaces can scan the address list at
859 	 * interrupt time; lock them out.
860 	 */
861 	s = splimp();
862 	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
863 	splx(s);
864 	*retifma = ifma;
865 
866 	if (llsa != 0) {
867 		for (ifma = ifp->if_multiaddrs.lh_first; ifma;
868 		     ifma = ifma->ifma_link.le_next) {
869 			if (equal(ifma->ifma_addr, llsa))
870 				break;
871 		}
872 		if (ifma) {
873 			ifma->ifma_refcount++;
874 		} else {
875 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
876 			       M_IFMADDR, M_WAITOK);
877 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
878 			       M_IFMADDR, M_WAITOK);
879 			bcopy(llsa, dupsa, llsa->sa_len);
880 			ifma->ifma_addr = dupsa;
881 			ifma->ifma_ifp = ifp;
882 			ifma->ifma_refcount = 1;
883 		}
884 		s = splimp();
885 		LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
886 		splx(s);
887 	}
888 	/*
889 	 * We are certain we have added something, so call down to the
890 	 * interface to let them know about it.
891 	 */
892 	s = splimp();
893 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
894 	splx(s);
895 
896 	return 0;
897 }
898 
899 /*
900  * Remove a reference to a multicast address on this interface.  Yell
901  * if the request does not match an existing membership.
902  */
903 int
904 if_delmulti(ifp, sa)
905 	struct ifnet *ifp;
906 	struct sockaddr *sa;
907 {
908 	struct ifmultiaddr *ifma;
909 	int s;
910 
911 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
912 	     ifma = ifma->ifma_link.le_next)
913 		if (equal(sa, ifma->ifma_addr))
914 			break;
915 	if (ifma == 0)
916 		return ENOENT;
917 
918 	if (ifma->ifma_refcount > 1) {
919 		ifma->ifma_refcount--;
920 		return 0;
921 	}
922 
923 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
924 	sa = ifma->ifma_lladdr;
925 	s = splimp();
926 	LIST_REMOVE(ifma, ifma_link);
927 	splx(s);
928 	free(ifma->ifma_addr, M_IFMADDR);
929 	free(ifma, M_IFMADDR);
930 	if (sa == 0)
931 		return 0;
932 
933 	/*
934 	 * Now look for the link-layer address which corresponds to
935 	 * this network address.  It had been squirreled away in
936 	 * ifma->ifma_lladdr for this purpose (so we don't have
937 	 * to call ifp->if_resolvemulti() again), and we saved that
938 	 * value in sa above.  If some nasty deleted the
939 	 * link-layer address out from underneath us, we can deal because
940 	 * the address we stored was is not the same as the one which was
941 	 * in the record for the link-layer address.  (So we don't complain
942 	 * in that case.)
943 	 */
944 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
945 	     ifma = ifma->ifma_link.le_next)
946 		if (equal(sa, ifma->ifma_addr))
947 			break;
948 	if (ifma == 0)
949 		return 0;
950 
951 	if (ifma->ifma_refcount > 1) {
952 		ifma->ifma_refcount--;
953 		return 0;
954 	}
955 
956 	s = splimp();
957 	LIST_REMOVE(ifma, ifma_link);
958 	splx(s);
959 	free(ifma->ifma_addr, M_IFMADDR);
960 	free(sa, M_IFMADDR);
961 	free(ifma, M_IFMADDR);
962 
963 	return 0;
964 }
965 
966 struct ifmultiaddr *
967 ifmaof_ifpforaddr(sa, ifp)
968 	struct sockaddr *sa;
969 	struct ifnet *ifp;
970 {
971 	struct ifmultiaddr *ifma;
972 
973 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
974 	     ifma = ifma->ifma_link.le_next)
975 		if (equal(ifma->ifma_addr, sa))
976 			break;
977 
978 	return ifma;
979 }
980 
981 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
982 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
983