if.c revision 46155
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.69 1999/04/27 11:16:56 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
60static int ifconf __P((u_long, caddr_t));
61static void ifinit __P((void *));
62static void if_qflush __P((struct ifqueue *));
63static void if_slowtimo __P((void *));
64static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
65
66SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
67
68MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
69MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
70
71int	ifqmaxlen = IFQ_MAXLEN;
72struct	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*/
81void
82ifinit(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
99int if_index = 0;
100struct ifaddr **ifnet_addrs;
101
102
103/*
104 * Attach an interface to the
105 * list of "active" interfaces.
106 */
107void
108if_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 */
187void
188if_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*/
219struct ifaddr *
220ifa_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*/
245struct ifaddr *
246ifa_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 */
268struct ifaddr *
269ifa_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)
298next:				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 */
362struct ifaddr *
363ifaof_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 */
412static void
413link_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 */
440void
441if_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 */
461void
462if_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 */
481void
482if_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 */
494void
495if_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 */
505static void
506if_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 */
526static void
527if_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 */
547struct ifnet *
548ifunit(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 */
598int
599ifioctl(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	int error;
608
609	switch (cmd) {
610
611	case SIOCGIFCONF:
612	case OSIOCGIFCONF:
613		return (ifconf(cmd, data));
614	}
615	ifr = (struct ifreq *)data;
616	ifp = ifunit(ifr->ifr_name);
617	if (ifp == 0)
618		return (ENXIO);
619	switch (cmd) {
620
621	case SIOCGIFFLAGS:
622		ifr->ifr_flags = ifp->if_flags;
623		break;
624
625	case SIOCGIFMETRIC:
626		ifr->ifr_metric = ifp->if_metric;
627		break;
628
629	case SIOCGIFMTU:
630		ifr->ifr_mtu = ifp->if_mtu;
631		break;
632
633	case SIOCGIFPHYS:
634		ifr->ifr_phys = ifp->if_physical;
635		break;
636
637	case SIOCSIFFLAGS:
638		error = suser(p);
639		if (error)
640			return (error);
641		ifr->ifr_prevflags = ifp->if_flags;
642		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
643			int s = splimp();
644			if_down(ifp);
645			splx(s);
646		}
647		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
648			int s = splimp();
649			if_up(ifp);
650			splx(s);
651		}
652		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
653			(ifr->ifr_flags &~ IFF_CANTCHANGE);
654		if (ifp->if_ioctl)
655			(void) (*ifp->if_ioctl)(ifp, cmd, data);
656		getmicrotime(&ifp->if_lastchange);
657		break;
658
659	case SIOCSIFMETRIC:
660		error = suser(p);
661		if (error)
662			return (error);
663		ifp->if_metric = ifr->ifr_metric;
664		getmicrotime(&ifp->if_lastchange);
665		break;
666
667	case SIOCSIFPHYS:
668		error = suser(p);
669		if (error)
670			return error;
671		if (!ifp->if_ioctl)
672		        return EOPNOTSUPP;
673		error = (*ifp->if_ioctl)(ifp, cmd, data);
674		if (error == 0)
675			getmicrotime(&ifp->if_lastchange);
676		return(error);
677
678	case SIOCSIFMTU:
679		error = suser(p);
680		if (error)
681			return (error);
682		if (ifp->if_ioctl == NULL)
683			return (EOPNOTSUPP);
684		/*
685		 * 72 was chosen below because it is the size of a TCP/IP
686		 * header (40) + the minimum mss (32).
687		 */
688		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
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 SIOCGIFMEDIA:
732	case SIOCGIFGENERIC:
733		if (ifp->if_ioctl == 0)
734			return (EOPNOTSUPP);
735		return ((*ifp->if_ioctl)(ifp, cmd, data));
736
737	default:
738		if (so->so_proto == 0)
739			return (EOPNOTSUPP);
740#ifndef COMPAT_43
741		return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
742								 data,
743								 ifp, p));
744#else
745	    {
746		int ocmd = cmd;
747
748		switch (cmd) {
749
750		case SIOCSIFDSTADDR:
751		case SIOCSIFADDR:
752		case SIOCSIFBRDADDR:
753		case SIOCSIFNETMASK:
754#if BYTE_ORDER != BIG_ENDIAN
755			if (ifr->ifr_addr.sa_family == 0 &&
756			    ifr->ifr_addr.sa_len < 16) {
757				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
758				ifr->ifr_addr.sa_len = 16;
759			}
760#else
761			if (ifr->ifr_addr.sa_len == 0)
762				ifr->ifr_addr.sa_len = 16;
763#endif
764			break;
765
766		case OSIOCGIFADDR:
767			cmd = SIOCGIFADDR;
768			break;
769
770		case OSIOCGIFDSTADDR:
771			cmd = SIOCGIFDSTADDR;
772			break;
773
774		case OSIOCGIFBRDADDR:
775			cmd = SIOCGIFBRDADDR;
776			break;
777
778		case OSIOCGIFNETMASK:
779			cmd = SIOCGIFNETMASK;
780		}
781		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
782								   cmd,
783								   data,
784								   ifp, p));
785		switch (ocmd) {
786
787		case OSIOCGIFADDR:
788		case OSIOCGIFDSTADDR:
789		case OSIOCGIFBRDADDR:
790		case OSIOCGIFNETMASK:
791			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
792		}
793		return (error);
794
795	    }
796#endif
797	}
798	return (0);
799}
800
801/*
802 * Set/clear promiscuous mode on interface ifp based on the truth value
803 * of pswitch.  The calls are reference counted so that only the first
804 * "on" request actually has an effect, as does the final "off" request.
805 * Results are undefined if the "off" and "on" requests are not matched.
806 */
807int
808ifpromisc(ifp, pswitch)
809	struct ifnet *ifp;
810	int pswitch;
811{
812	struct ifreq ifr;
813	int error;
814
815	if (pswitch) {
816		/*
817		 * If the device is not configured up, we cannot put it in
818		 * promiscuous mode.
819		 */
820		if ((ifp->if_flags & IFF_UP) == 0)
821			return (ENETDOWN);
822		if (ifp->if_pcount++ != 0)
823			return (0);
824		ifp->if_flags |= IFF_PROMISC;
825		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
826		    ifp->if_name, ifp->if_unit);
827	} else {
828		if (--ifp->if_pcount > 0)
829			return (0);
830		ifp->if_flags &= ~IFF_PROMISC;
831	}
832	ifr.ifr_flags = ifp->if_flags;
833	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
834	if (error == 0)
835		rt_ifmsg(ifp);
836	return error;
837}
838
839/*
840 * Return interface configuration
841 * of system.  List may be used
842 * in later ioctl's (above) to get
843 * other information.
844 */
845/*ARGSUSED*/
846static int
847ifconf(cmd, data)
848	u_long cmd;
849	caddr_t data;
850{
851	register struct ifconf *ifc = (struct ifconf *)data;
852	register struct ifnet *ifp = ifnet.tqh_first;
853	register struct ifaddr *ifa;
854	struct ifreq ifr, *ifrp;
855	int space = ifc->ifc_len, error = 0;
856
857	ifrp = ifc->ifc_req;
858	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
859		char workbuf[64];
860		int ifnlen, addrs;
861
862		ifnlen = snprintf(workbuf, sizeof(workbuf),
863		    "%s%d", ifp->if_name, ifp->if_unit);
864		if(ifnlen + 1 > sizeof ifr.ifr_name) {
865			error = ENAMETOOLONG;
866		} else {
867			strcpy(ifr.ifr_name, workbuf);
868		}
869
870		addrs = 0;
871		ifa = ifp->if_addrhead.tqh_first;
872		for ( ; space > sizeof (ifr) && ifa;
873		    ifa = ifa->ifa_link.tqe_next) {
874			register struct sockaddr *sa = ifa->ifa_addr;
875			if (curproc->p_prison && prison_if(curproc, sa))
876				continue;
877			addrs++;
878#ifdef COMPAT_43
879			if (cmd == OSIOCGIFCONF) {
880				struct osockaddr *osa =
881					 (struct osockaddr *)&ifr.ifr_addr;
882				ifr.ifr_addr = *sa;
883				osa->sa_family = sa->sa_family;
884				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
885						sizeof (ifr));
886				ifrp++;
887			} else
888#endif
889			if (sa->sa_len <= sizeof(*sa)) {
890				ifr.ifr_addr = *sa;
891				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
892						sizeof (ifr));
893				ifrp++;
894			} else {
895				space -= sa->sa_len - sizeof(*sa);
896				if (space < sizeof (ifr))
897					break;
898				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
899						sizeof (ifr.ifr_name));
900				if (error == 0)
901				    error = copyout((caddr_t)sa,
902				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
903				ifrp = (struct ifreq *)
904					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
905			}
906			if (error)
907				break;
908			space -= sizeof (ifr);
909		}
910		if (!addrs) {
911			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
912			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
913			    sizeof (ifr));
914			if (error)
915				break;
916			space -= sizeof (ifr), ifrp++;
917		}
918	}
919	ifc->ifc_len -= space;
920	return (error);
921}
922
923/*
924 * Just like if_promisc(), but for all-multicast-reception mode.
925 */
926int
927if_allmulti(ifp, onswitch)
928	struct ifnet *ifp;
929	int onswitch;
930{
931	int error = 0;
932	int s = splimp();
933
934	if (onswitch) {
935		if (ifp->if_amcount++ == 0) {
936			ifp->if_flags |= IFF_ALLMULTI;
937			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
938		}
939	} else {
940		if (ifp->if_amcount > 1) {
941			ifp->if_amcount--;
942		} else {
943			ifp->if_amcount = 0;
944			ifp->if_flags &= ~IFF_ALLMULTI;
945			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
946		}
947	}
948	splx(s);
949
950	if (error == 0)
951		rt_ifmsg(ifp);
952	return error;
953}
954
955/*
956 * Add a multicast listenership to the interface in question.
957 * The link layer provides a routine which converts
958 */
959int
960if_addmulti(ifp, sa, retifma)
961	struct ifnet *ifp;	/* interface to manipulate */
962	struct sockaddr *sa;	/* address to add */
963	struct ifmultiaddr **retifma;
964{
965	struct sockaddr *llsa, *dupsa;
966	int error, s;
967	struct ifmultiaddr *ifma;
968
969	/*
970	 * If the matching multicast address already exists
971	 * then don't add a new one, just add a reference
972	 */
973	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
974	     ifma = ifma->ifma_link.le_next) {
975		if (equal(sa, ifma->ifma_addr)) {
976			ifma->ifma_refcount++;
977			if (retifma)
978				*retifma = ifma;
979			return 0;
980		}
981	}
982
983	/*
984	 * Give the link layer a chance to accept/reject it, and also
985	 * find out which AF_LINK address this maps to, if it isn't one
986	 * already.
987	 */
988	if (ifp->if_resolvemulti) {
989		error = ifp->if_resolvemulti(ifp, &llsa, sa);
990		if (error) return error;
991	} else {
992		llsa = 0;
993	}
994
995	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
996	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
997	bcopy(sa, dupsa, sa->sa_len);
998
999	ifma->ifma_addr = dupsa;
1000	ifma->ifma_lladdr = llsa;
1001	ifma->ifma_ifp = ifp;
1002	ifma->ifma_refcount = 1;
1003	ifma->ifma_protospec = 0;
1004	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1005
1006	/*
1007	 * Some network interfaces can scan the address list at
1008	 * interrupt time; lock them out.
1009	 */
1010	s = splimp();
1011	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1012	splx(s);
1013	*retifma = ifma;
1014
1015	if (llsa != 0) {
1016		for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1017		     ifma = ifma->ifma_link.le_next) {
1018			if (equal(ifma->ifma_addr, llsa))
1019				break;
1020		}
1021		if (ifma) {
1022			ifma->ifma_refcount++;
1023		} else {
1024			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1025			       M_IFMADDR, M_WAITOK);
1026			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1027			       M_IFMADDR, M_WAITOK);
1028			bcopy(llsa, dupsa, llsa->sa_len);
1029			ifma->ifma_addr = dupsa;
1030			ifma->ifma_ifp = ifp;
1031			ifma->ifma_refcount = 1;
1032			s = splimp();
1033			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1034			splx(s);
1035		}
1036	}
1037	/*
1038	 * We are certain we have added something, so call down to the
1039	 * interface to let them know about it.
1040	 */
1041	s = splimp();
1042	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1043	splx(s);
1044
1045	return 0;
1046}
1047
1048/*
1049 * Remove a reference to a multicast address on this interface.  Yell
1050 * if the request does not match an existing membership.
1051 */
1052int
1053if_delmulti(ifp, sa)
1054	struct ifnet *ifp;
1055	struct sockaddr *sa;
1056{
1057	struct ifmultiaddr *ifma;
1058	int s;
1059
1060	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1061	     ifma = ifma->ifma_link.le_next)
1062		if (equal(sa, ifma->ifma_addr))
1063			break;
1064	if (ifma == 0)
1065		return ENOENT;
1066
1067	if (ifma->ifma_refcount > 1) {
1068		ifma->ifma_refcount--;
1069		return 0;
1070	}
1071
1072	rt_newmaddrmsg(RTM_DELMADDR, ifma);
1073	sa = ifma->ifma_lladdr;
1074	s = splimp();
1075	LIST_REMOVE(ifma, ifma_link);
1076	splx(s);
1077	free(ifma->ifma_addr, M_IFMADDR);
1078	free(ifma, M_IFMADDR);
1079	if (sa == 0)
1080		return 0;
1081
1082	/*
1083	 * Now look for the link-layer address which corresponds to
1084	 * this network address.  It had been squirreled away in
1085	 * ifma->ifma_lladdr for this purpose (so we don't have
1086	 * to call ifp->if_resolvemulti() again), and we saved that
1087	 * value in sa above.  If some nasty deleted the
1088	 * link-layer address out from underneath us, we can deal because
1089	 * the address we stored was is not the same as the one which was
1090	 * in the record for the link-layer address.  (So we don't complain
1091	 * in that case.)
1092	 */
1093	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1094	     ifma = ifma->ifma_link.le_next)
1095		if (equal(sa, ifma->ifma_addr))
1096			break;
1097	if (ifma == 0)
1098		return 0;
1099
1100	if (ifma->ifma_refcount > 1) {
1101		ifma->ifma_refcount--;
1102		return 0;
1103	}
1104
1105	s = splimp();
1106	LIST_REMOVE(ifma, ifma_link);
1107	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1108	splx(s);
1109	free(ifma->ifma_addr, M_IFMADDR);
1110	free(sa, M_IFMADDR);
1111	free(ifma, M_IFMADDR);
1112
1113	return 0;
1114}
1115
1116struct ifmultiaddr *
1117ifmaof_ifpforaddr(sa, ifp)
1118	struct sockaddr *sa;
1119	struct ifnet *ifp;
1120{
1121	struct ifmultiaddr *ifma;
1122
1123	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1124	     ifma = ifma->ifma_link.le_next)
1125		if (equal(ifma->ifma_addr, sa))
1126			break;
1127
1128	return ifma;
1129}
1130
1131SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1132SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1133