if.c revision 71959
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: head/sys/net/if.c 71959 2001-02-03 11:46:35Z phk $
35 */
36
37#include "opt_compat.h"
38#include "opt_inet6.h"
39#include "opt_inet.h"
40
41#include <sys/param.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/systm.h>
45#include <sys/proc.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/protosw.h>
49#include <sys/kernel.h>
50#include <sys/sockio.h>
51#include <sys/syslog.h>
52#include <sys/sysctl.h>
53
54#include <net/if.h>
55#include <net/if_arp.h>
56#include <net/if_dl.h>
57#include <net/if_types.h>
58#include <net/radix.h>
59#include <net/route.h>
60
61#if defined(INET) || defined(INET6)
62/*XXX*/
63#include <netinet/in.h>
64#include <netinet/in_var.h>
65#ifdef INET6
66#include <netinet6/in6_var.h>
67#include <netinet6/in6_ifattach.h>
68#endif
69#endif
70
71/*
72 * System initialization
73 */
74
75static int ifconf __P((u_long, caddr_t));
76static void ifinit __P((void *));
77static void if_qflush __P((struct ifqueue *));
78static void if_slowtimo __P((void *));
79static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
80static int  if_rtdel __P((struct radix_node *, void *));
81
82SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
83
84MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
85MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
86
87int	ifqmaxlen = IFQ_MAXLEN;
88struct	ifnethead ifnet;	/* depend on static init XXX */
89
90#ifdef INET6
91/*
92 * XXX: declare here to avoid to include many inet6 related files..
93 * should be more generalized?
94 */
95extern void	nd6_setmtu __P((struct ifnet *));
96#endif
97
98/*
99 * Network interface utility routines.
100 *
101 * Routines with ifa_ifwith* names take sockaddr *'s as
102 * parameters.
103 */
104/* ARGSUSED*/
105void
106ifinit(dummy)
107	void *dummy;
108{
109	struct ifnet *ifp;
110	int s;
111
112	s = splimp();
113	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_link)) {
114		if (ifp->if_snd.ifq_maxlen == 0) {
115			printf("%s%d XXX: driver didn't set ifq_maxlen\n",
116			    ifp->if_name, ifp->if_unit);
117			ifp->if_snd.ifq_maxlen = ifqmaxlen;
118		}
119		/* XXX This is an access violation of the mutex internals. */
120		if (ifp->if_snd.ifq_mtx.mtx_description == NULL) {
121			printf("%s%d XXX: driver didn't initialize queue mtx\n",
122			    ifp->if_name, ifp->if_unit);
123			mtx_init(&ifp->if_snd.ifq_mtx, "unknown", MTX_DEF);
124		}
125	}
126	splx(s);
127	if_slowtimo(0);
128}
129
130int if_index = 0;
131struct ifaddr **ifnet_addrs;
132struct ifnet **ifindex2ifnet = NULL;
133
134
135/*
136 * Attach an interface to the
137 * list of "active" interfaces.
138 */
139void
140if_attach(ifp)
141	struct ifnet *ifp;
142{
143	unsigned socksize, ifasize;
144	int namelen, masklen;
145	char workbuf[64];
146	register struct sockaddr_dl *sdl;
147	register struct ifaddr *ifa;
148	static int if_indexlim = 8;
149	static int inited;
150
151	if (!inited) {
152		TAILQ_INIT(&ifnet);
153		inited = 1;
154	}
155
156	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
157	ifp->if_index = ++if_index;
158	/*
159	 * XXX -
160	 * The old code would work if the interface passed a pre-existing
161	 * chain of ifaddrs to this code.  We don't trust our callers to
162	 * properly initialize the tailq, however, so we no longer allow
163	 * this unlikely case.
164	 */
165	TAILQ_INIT(&ifp->if_addrhead);
166	TAILQ_INIT(&ifp->if_prefixhead);
167	LIST_INIT(&ifp->if_multiaddrs);
168	getmicrotime(&ifp->if_lastchange);
169	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
170		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
171		caddr_t q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
172		if (ifnet_addrs) {
173			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
174			free((caddr_t)ifnet_addrs, M_IFADDR);
175		}
176		ifnet_addrs = (struct ifaddr **)q;
177
178		/* grow ifindex2ifnet */
179		n = if_indexlim * sizeof(struct ifnet *);
180		q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
181		if (ifindex2ifnet) {
182			bcopy((caddr_t)ifindex2ifnet, q, n/2);
183			free((caddr_t)ifindex2ifnet, M_IFADDR);
184		}
185		ifindex2ifnet = (struct ifnet **)q;
186	}
187
188	ifindex2ifnet[if_index] = ifp;
189
190	mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, MTX_DEF);
191
192	/*
193	 * create a Link Level name for this device
194	 */
195	namelen = snprintf(workbuf, sizeof(workbuf),
196	    "%s%d", ifp->if_name, ifp->if_unit);
197#define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
198	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
199	socksize = masklen + ifp->if_addrlen;
200#define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
201	if (socksize < sizeof(*sdl))
202		socksize = sizeof(*sdl);
203	socksize = ROUNDUP(socksize);
204	ifasize = sizeof(*ifa) + 2 * socksize;
205	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
206	if (ifa) {
207		sdl = (struct sockaddr_dl *)(ifa + 1);
208		sdl->sdl_len = socksize;
209		sdl->sdl_family = AF_LINK;
210		bcopy(workbuf, sdl->sdl_data, namelen);
211		sdl->sdl_nlen = namelen;
212		sdl->sdl_index = ifp->if_index;
213		sdl->sdl_type = ifp->if_type;
214		ifnet_addrs[if_index - 1] = ifa;
215		ifa->ifa_ifp = ifp;
216		ifa->ifa_rtrequest = link_rtrequest;
217		ifa->ifa_addr = (struct sockaddr *)sdl;
218		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
219		ifa->ifa_netmask = (struct sockaddr *)sdl;
220		sdl->sdl_len = masklen;
221		while (namelen != 0)
222			sdl->sdl_data[--namelen] = 0xff;
223		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
224	}
225}
226
227/*
228 * Detach an interface, removing it from the
229 * list of "active" interfaces.
230 */
231void
232if_detach(ifp)
233	struct ifnet *ifp;
234{
235	struct ifaddr *ifa;
236	struct radix_node_head	*rnh;
237	int s;
238	int i;
239
240	/*
241	 * Remove routes and flush queues.
242	 */
243	s = splnet();
244	if_down(ifp);
245
246	/*
247	 * Remove address from ifnet_addrs[] and maybe decrement if_index.
248	 * Clean up all addresses.
249	 */
250	ifnet_addrs[ifp->if_index - 1] = 0;
251	while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
252		if_index--;
253
254	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
255	     ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
256#ifdef INET
257		/* XXX: Ugly!! ad hoc just for INET */
258		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
259			struct ifaliasreq ifr;
260
261			bzero(&ifr, sizeof(ifr));
262			ifr.ifra_addr = *ifa->ifa_addr;
263			if (ifa->ifa_dstaddr)
264				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
265			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
266			    NULL) == 0)
267				continue;
268		}
269#endif /* INET */
270#ifdef INET6
271		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
272			in6_purgeaddr(ifa, ifp);
273			/* ifp_addrhead is already updated */
274			continue;
275		}
276#endif /* INET6 */
277		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
278		IFAFREE(ifa);
279	}
280
281	/*
282	 * Delete all remaining routes using this interface
283	 * Unfortuneatly the only way to do this is to slog through
284	 * the entire routing table looking for routes which point
285	 * to this interface...oh well...
286	 */
287	for (i = 1; i <= AF_MAX; i++) {
288		if ((rnh = rt_tables[i]) == NULL)
289			continue;
290		(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
291	}
292
293#ifdef INET6
294	/* nuke all IPv6 kernel structs related to ifp */
295	in6_ifdetach(ifp);
296#endif
297
298	TAILQ_REMOVE(&ifnet, ifp, if_link);
299	mtx_destroy(&ifp->if_snd.ifq_mtx);
300	splx(s);
301}
302
303/*
304 * Delete Routes for a Network Interface
305 *
306 * Called for each routing entry via the rnh->rnh_walktree() call above
307 * to delete all route entries referencing a detaching network interface.
308 *
309 * Arguments:
310 *	rn	pointer to node in the routing table
311 *	arg	argument passed to rnh->rnh_walktree() - detaching interface
312 *
313 * Returns:
314 *	0	successful
315 *	errno	failed - reason indicated
316 *
317 */
318static int
319if_rtdel(rn, arg)
320	struct radix_node	*rn;
321	void			*arg;
322{
323	struct rtentry	*rt = (struct rtentry *)rn;
324	struct ifnet	*ifp = arg;
325	int		err;
326
327	if (rt->rt_ifp == ifp) {
328
329		/*
330		 * Protect (sorta) against walktree recursion problems
331		 * with cloned routes
332		 */
333		if ((rt->rt_flags & RTF_UP) == 0)
334			return (0);
335
336		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
337				rt_mask(rt), rt->rt_flags,
338				(struct rtentry **) NULL);
339		if (err) {
340			log(LOG_WARNING, "if_rtdel: error %d\n", err);
341		}
342	}
343
344	return (0);
345}
346
347/*
348 * Locate an interface based on a complete address.
349 */
350/*ARGSUSED*/
351struct ifaddr *
352ifa_ifwithaddr(addr)
353	register struct sockaddr *addr;
354{
355	register struct ifnet *ifp;
356	register struct ifaddr *ifa;
357
358#define	equal(a1, a2) \
359  (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
360	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_link))
361	    for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
362		 ifa = TAILQ_NEXT(ifa, ifa_link)) {
363		if (ifa->ifa_addr->sa_family != addr->sa_family)
364			continue;
365		if (equal(addr, ifa->ifa_addr))
366			return (ifa);
367		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
368		    /* IP6 doesn't have broadcast */
369		    ifa->ifa_broadaddr->sa_len != 0 &&
370		    equal(ifa->ifa_broadaddr, addr))
371			return (ifa);
372	}
373	return ((struct ifaddr *)0);
374}
375/*
376 * Locate the point to point interface with a given destination address.
377 */
378/*ARGSUSED*/
379struct ifaddr *
380ifa_ifwithdstaddr(addr)
381	register struct sockaddr *addr;
382{
383	register struct ifnet *ifp;
384	register struct ifaddr *ifa;
385
386	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_link))
387	    if (ifp->if_flags & IFF_POINTOPOINT)
388		for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
389		     ifa = TAILQ_NEXT(ifa, ifa_link)) {
390			if (ifa->ifa_addr->sa_family != addr->sa_family)
391				continue;
392			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
393				return (ifa);
394	}
395	return ((struct ifaddr *)0);
396}
397
398/*
399 * Find an interface on a specific network.  If many, choice
400 * is most specific found.
401 */
402struct ifaddr *
403ifa_ifwithnet(addr)
404	struct sockaddr *addr;
405{
406	register struct ifnet *ifp;
407	register struct ifaddr *ifa;
408	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
409	u_int af = addr->sa_family;
410	char *addr_data = addr->sa_data, *cplim;
411
412	/*
413	 * AF_LINK addresses can be looked up directly by their index number,
414	 * so do that if we can.
415	 */
416	if (af == AF_LINK) {
417	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
418	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
419		return (ifnet_addrs[sdl->sdl_index - 1]);
420	}
421
422	/*
423	 * Scan though each interface, looking for ones that have
424	 * addresses in this address family.
425	 */
426	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_link)) {
427		for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
428		     ifa = TAILQ_NEXT(ifa, ifa_link)) {
429			register char *cp, *cp2, *cp3;
430
431			if (ifa->ifa_addr->sa_family != af)
432next:				continue;
433			if (
434#ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
435			    addr->sa_family != AF_INET6 &&
436#endif
437			    ifp->if_flags & IFF_POINTOPOINT) {
438				/*
439				 * This is a bit broken as it doesn't
440				 * take into account that the remote end may
441				 * be a single node in the network we are
442				 * looking for.
443				 * The trouble is that we don't know the
444				 * netmask for the remote end.
445				 */
446				if (ifa->ifa_dstaddr != 0
447				    && equal(addr, ifa->ifa_dstaddr))
448 					return (ifa);
449			} else {
450				/*
451				 * if we have a special address handler,
452				 * then use it instead of the generic one.
453				 */
454	          		if (ifa->ifa_claim_addr) {
455					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
456						return (ifa);
457					} else {
458						continue;
459					}
460				}
461
462				/*
463				 * Scan all the bits in the ifa's address.
464				 * If a bit dissagrees with what we are
465				 * looking for, mask it with the netmask
466				 * to see if it really matters.
467				 * (A byte at a time)
468				 */
469				if (ifa->ifa_netmask == 0)
470					continue;
471				cp = addr_data;
472				cp2 = ifa->ifa_addr->sa_data;
473				cp3 = ifa->ifa_netmask->sa_data;
474				cplim = ifa->ifa_netmask->sa_len
475					+ (char *)ifa->ifa_netmask;
476				while (cp3 < cplim)
477					if ((*cp++ ^ *cp2++) & *cp3++)
478						goto next; /* next address! */
479				/*
480				 * If the netmask of what we just found
481				 * is more specific than what we had before
482				 * (if we had one) then remember the new one
483				 * before continuing to search
484				 * for an even better one.
485				 */
486				if (ifa_maybe == 0 ||
487				    rn_refines((caddr_t)ifa->ifa_netmask,
488				    (caddr_t)ifa_maybe->ifa_netmask))
489					ifa_maybe = ifa;
490			}
491		}
492	}
493	return (ifa_maybe);
494}
495
496/*
497 * Find an interface address specific to an interface best matching
498 * a given address.
499 */
500struct ifaddr *
501ifaof_ifpforaddr(addr, ifp)
502	struct sockaddr *addr;
503	register struct ifnet *ifp;
504{
505	register struct ifaddr *ifa;
506	register char *cp, *cp2, *cp3;
507	register char *cplim;
508	struct ifaddr *ifa_maybe = 0;
509	u_int af = addr->sa_family;
510
511	if (af >= AF_MAX)
512		return (0);
513	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
514	     ifa = TAILQ_NEXT(ifa, ifa_link)) {
515		if (ifa->ifa_addr->sa_family != af)
516			continue;
517		if (ifa_maybe == 0)
518			ifa_maybe = ifa;
519		if (ifa->ifa_netmask == 0) {
520			if (equal(addr, ifa->ifa_addr) ||
521			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
522				return (ifa);
523			continue;
524		}
525		if (ifp->if_flags & IFF_POINTOPOINT) {
526			if (equal(addr, ifa->ifa_dstaddr))
527				return (ifa);
528		} else {
529			cp = addr->sa_data;
530			cp2 = ifa->ifa_addr->sa_data;
531			cp3 = ifa->ifa_netmask->sa_data;
532			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
533			for (; cp3 < cplim; cp3++)
534				if ((*cp++ ^ *cp2++) & *cp3)
535					break;
536			if (cp3 == cplim)
537				return (ifa);
538		}
539	}
540	return (ifa_maybe);
541}
542
543#include <net/route.h>
544
545/*
546 * Default action when installing a route with a Link Level gateway.
547 * Lookup an appropriate real ifa to point to.
548 * This should be moved to /sys/net/link.c eventually.
549 */
550static void
551link_rtrequest(cmd, rt, sa)
552	int cmd;
553	register struct rtentry *rt;
554	struct sockaddr *sa;
555{
556	register struct ifaddr *ifa;
557	struct sockaddr *dst;
558	struct ifnet *ifp;
559
560	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
561	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
562		return;
563	ifa = ifaof_ifpforaddr(dst, ifp);
564	if (ifa) {
565		IFAFREE(rt->rt_ifa);
566		rt->rt_ifa = ifa;
567		ifa->ifa_refcnt++;
568		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
569			ifa->ifa_rtrequest(cmd, rt, sa);
570	}
571}
572
573/*
574 * Mark an interface down and notify protocols of
575 * the transition.
576 * NOTE: must be called at splnet or eqivalent.
577 */
578void
579if_unroute(ifp, flag, fam)
580	register struct ifnet *ifp;
581	int flag, fam;
582{
583	register struct ifaddr *ifa;
584
585	ifp->if_flags &= ~flag;
586	getmicrotime(&ifp->if_lastchange);
587	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
588		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
589			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
590	if_qflush(&ifp->if_snd);
591	rt_ifmsg(ifp);
592}
593
594/*
595 * Mark an interface up and notify protocols of
596 * the transition.
597 * NOTE: must be called at splnet or eqivalent.
598 */
599void
600if_route(ifp, flag, fam)
601	register struct ifnet *ifp;
602	int flag, fam;
603{
604	register struct ifaddr *ifa;
605
606	ifp->if_flags |= flag;
607	getmicrotime(&ifp->if_lastchange);
608	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
609		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
610			pfctlinput(PRC_IFUP, ifa->ifa_addr);
611	rt_ifmsg(ifp);
612#ifdef INET6
613	in6_if_up(ifp);
614#endif
615}
616
617/*
618 * Mark an interface down and notify protocols of
619 * the transition.
620 * NOTE: must be called at splnet or eqivalent.
621 */
622void
623if_down(ifp)
624	register struct ifnet *ifp;
625{
626
627	if_unroute(ifp, IFF_UP, AF_UNSPEC);
628}
629
630/*
631 * Mark an interface up and notify protocols of
632 * the transition.
633 * NOTE: must be called at splnet or eqivalent.
634 */
635void
636if_up(ifp)
637	register struct ifnet *ifp;
638{
639
640	if_route(ifp, IFF_UP, AF_UNSPEC);
641}
642
643/*
644 * Flush an interface queue.
645 */
646static void
647if_qflush(ifq)
648	register struct ifqueue *ifq;
649{
650	register struct mbuf *m, *n;
651
652	n = ifq->ifq_head;
653	while ((m = n) != 0) {
654		n = m->m_act;
655		m_freem(m);
656	}
657	ifq->ifq_head = 0;
658	ifq->ifq_tail = 0;
659	ifq->ifq_len = 0;
660}
661
662/*
663 * Handle interface watchdog timer routines.  Called
664 * from softclock, we decrement timers (if set) and
665 * call the appropriate interface routine on expiration.
666 */
667static void
668if_slowtimo(arg)
669	void *arg;
670{
671	register struct ifnet *ifp;
672	int s = splimp();
673
674	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_link)) {
675		if (ifp->if_timer == 0 || --ifp->if_timer)
676			continue;
677		if (ifp->if_watchdog)
678			(*ifp->if_watchdog)(ifp);
679	}
680	splx(s);
681	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
682}
683
684/*
685 * Map interface name to
686 * interface structure pointer.
687 */
688struct ifnet *
689ifunit(char *name)
690{
691	char namebuf[IFNAMSIZ + 1];
692	char *cp;
693	struct ifnet *ifp;
694	int unit;
695	unsigned len, m;
696	char c;
697
698	len = strlen(name);
699	if (len < 2 || len > IFNAMSIZ)
700		return NULL;
701	cp = name + len - 1;
702	c = *cp;
703	if (c < '0' || c > '9')
704		return NULL;		/* trailing garbage */
705	unit = 0;
706	m = 1;
707	do {
708		if (cp == name)
709			return NULL;	/* no interface name */
710		unit += (c - '0') * m;
711		if (unit > 1000000)
712			return NULL;	/* number is unreasonable */
713		m *= 10;
714		c = *--cp;
715	} while (c >= '0' && c <= '9');
716	len = cp - name + 1;
717	bcopy(name, namebuf, len);
718	namebuf[len] = '\0';
719	/*
720	 * Now search all the interfaces for this name/number
721	 */
722	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_link)) {
723		if (strcmp(ifp->if_name, namebuf))
724			continue;
725		if (unit == ifp->if_unit)
726			break;
727	}
728	return (ifp);
729}
730
731
732/*
733 * Map interface name in a sockaddr_dl to
734 * interface structure pointer.
735 */
736struct ifnet *
737if_withname(sa)
738	struct sockaddr *sa;
739{
740	char ifname[IFNAMSIZ+1];
741	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
742
743	if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
744	     (sdl->sdl_nlen > IFNAMSIZ) )
745		return NULL;
746
747	/*
748	 * ifunit wants a null-terminated name.  It may not be null-terminated
749	 * in the sockaddr.  We don't want to change the caller's sockaddr,
750	 * and there might not be room to put the trailing null anyway, so we
751	 * make a local copy that we know we can null terminate safely.
752	 */
753
754	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
755	ifname[sdl->sdl_nlen] = '\0';
756	return ifunit(ifname);
757}
758
759
760/*
761 * Interface ioctls.
762 */
763int
764ifioctl(so, cmd, data, p)
765	struct socket *so;
766	u_long cmd;
767	caddr_t data;
768	struct proc *p;
769{
770	register struct ifnet *ifp;
771	register struct ifreq *ifr;
772	struct ifstat *ifs;
773	int error;
774	short oif_flags;
775
776	switch (cmd) {
777
778	case SIOCGIFCONF:
779	case OSIOCGIFCONF:
780		return (ifconf(cmd, data));
781	}
782	ifr = (struct ifreq *)data;
783	ifp = ifunit(ifr->ifr_name);
784	if (ifp == 0)
785		return (ENXIO);
786	switch (cmd) {
787
788	case SIOCGIFFLAGS:
789		ifr->ifr_flags = ifp->if_flags;
790		break;
791
792	case SIOCGIFMETRIC:
793		ifr->ifr_metric = ifp->if_metric;
794		break;
795
796	case SIOCGIFMTU:
797		ifr->ifr_mtu = ifp->if_mtu;
798		break;
799
800	case SIOCGIFPHYS:
801		ifr->ifr_phys = ifp->if_physical;
802		break;
803
804	case SIOCSIFFLAGS:
805		error = suser(p);
806		if (error)
807			return (error);
808		ifr->ifr_prevflags = ifp->if_flags;
809		if (ifp->if_flags & IFF_SMART) {
810			/* Smart drivers twiddle their own routes */
811		} else if (ifp->if_flags & IFF_UP &&
812		    (ifr->ifr_flags & IFF_UP) == 0) {
813			int s = splimp();
814			if_down(ifp);
815			splx(s);
816		} else if (ifr->ifr_flags & IFF_UP &&
817		    (ifp->if_flags & IFF_UP) == 0) {
818			int s = splimp();
819			if_up(ifp);
820			splx(s);
821		}
822		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
823			(ifr->ifr_flags &~ IFF_CANTCHANGE);
824		if (ifp->if_ioctl)
825			(void) (*ifp->if_ioctl)(ifp, cmd, data);
826		getmicrotime(&ifp->if_lastchange);
827		break;
828
829	case SIOCSIFMETRIC:
830		error = suser(p);
831		if (error)
832			return (error);
833		ifp->if_metric = ifr->ifr_metric;
834		getmicrotime(&ifp->if_lastchange);
835		break;
836
837	case SIOCSIFPHYS:
838		error = suser(p);
839		if (error)
840			return error;
841		if (!ifp->if_ioctl)
842		        return EOPNOTSUPP;
843		error = (*ifp->if_ioctl)(ifp, cmd, data);
844		if (error == 0)
845			getmicrotime(&ifp->if_lastchange);
846		return(error);
847
848	case SIOCSIFMTU:
849	{
850		u_long oldmtu = ifp->if_mtu;
851
852		error = suser(p);
853		if (error)
854			return (error);
855		if (ifp->if_ioctl == NULL)
856			return (EOPNOTSUPP);
857		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
858			return (EINVAL);
859		error = (*ifp->if_ioctl)(ifp, cmd, data);
860		if (error == 0) {
861			getmicrotime(&ifp->if_lastchange);
862			rt_ifmsg(ifp);
863		}
864		/*
865		 * If the link MTU changed, do network layer specific procedure.
866		 */
867		if (ifp->if_mtu != oldmtu) {
868#ifdef INET6
869			nd6_setmtu(ifp);
870#endif
871		}
872		return (error);
873	}
874
875	case SIOCADDMULTI:
876	case SIOCDELMULTI:
877		error = suser(p);
878		if (error)
879			return (error);
880
881		/* Don't allow group membership on non-multicast interfaces. */
882		if ((ifp->if_flags & IFF_MULTICAST) == 0)
883			return EOPNOTSUPP;
884
885		/* Don't let users screw up protocols' entries. */
886		if (ifr->ifr_addr.sa_family != AF_LINK)
887			return EINVAL;
888
889		if (cmd == SIOCADDMULTI) {
890			struct ifmultiaddr *ifma;
891			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
892		} else {
893			error = if_delmulti(ifp, &ifr->ifr_addr);
894		}
895		if (error == 0)
896			getmicrotime(&ifp->if_lastchange);
897		return error;
898
899	case SIOCSIFPHYADDR:
900	case SIOCDIFPHYADDR:
901#ifdef INET6
902	case SIOCSIFPHYADDR_IN6:
903#endif
904        case SIOCSIFMEDIA:
905	case SIOCSIFGENERIC:
906		error = suser(p);
907		if (error)
908			return (error);
909		if (ifp->if_ioctl == 0)
910			return (EOPNOTSUPP);
911		error = (*ifp->if_ioctl)(ifp, cmd, data);
912		if (error == 0)
913			getmicrotime(&ifp->if_lastchange);
914		return error;
915
916	case SIOCGIFSTATUS:
917		ifs = (struct ifstat *)data;
918		ifs->ascii[0] = '\0';
919
920	case SIOCGIFMEDIA:
921	case SIOCGIFGENERIC:
922		if (ifp->if_ioctl == 0)
923			return (EOPNOTSUPP);
924		return ((*ifp->if_ioctl)(ifp, cmd, data));
925
926	case SIOCSIFLLADDR:
927		error = suser(p);
928		if (error)
929			return (error);
930		return if_setlladdr(ifp,
931		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
932
933	default:
934		oif_flags = ifp->if_flags;
935		if (so->so_proto == 0)
936			return (EOPNOTSUPP);
937#ifndef COMPAT_43
938		error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
939								 data,
940								 ifp, p));
941#else
942	    {
943		int ocmd = cmd;
944
945		switch (cmd) {
946
947		case SIOCSIFDSTADDR:
948		case SIOCSIFADDR:
949		case SIOCSIFBRDADDR:
950		case SIOCSIFNETMASK:
951#if BYTE_ORDER != BIG_ENDIAN
952			if (ifr->ifr_addr.sa_family == 0 &&
953			    ifr->ifr_addr.sa_len < 16) {
954				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
955				ifr->ifr_addr.sa_len = 16;
956			}
957#else
958			if (ifr->ifr_addr.sa_len == 0)
959				ifr->ifr_addr.sa_len = 16;
960#endif
961			break;
962
963		case OSIOCGIFADDR:
964			cmd = SIOCGIFADDR;
965			break;
966
967		case OSIOCGIFDSTADDR:
968			cmd = SIOCGIFDSTADDR;
969			break;
970
971		case OSIOCGIFBRDADDR:
972			cmd = SIOCGIFBRDADDR;
973			break;
974
975		case OSIOCGIFNETMASK:
976			cmd = SIOCGIFNETMASK;
977		}
978		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
979								   cmd,
980								   data,
981								   ifp, p));
982		switch (ocmd) {
983
984		case OSIOCGIFADDR:
985		case OSIOCGIFDSTADDR:
986		case OSIOCGIFBRDADDR:
987		case OSIOCGIFNETMASK:
988			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
989
990		}
991	    }
992#endif /* COMPAT_43 */
993
994		if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
995#ifdef INET6
996			DELAY(100);/* XXX: temporal workaround for fxp issue*/
997			if (ifp->if_flags & IFF_UP) {
998				int s = splimp();
999				in6_if_up(ifp);
1000				splx(s);
1001			}
1002#endif
1003		}
1004		return (error);
1005
1006	}
1007	return (0);
1008}
1009
1010/*
1011 * Set/clear promiscuous mode on interface ifp based on the truth value
1012 * of pswitch.  The calls are reference counted so that only the first
1013 * "on" request actually has an effect, as does the final "off" request.
1014 * Results are undefined if the "off" and "on" requests are not matched.
1015 */
1016int
1017ifpromisc(ifp, pswitch)
1018	struct ifnet *ifp;
1019	int pswitch;
1020{
1021	struct ifreq ifr;
1022	int error;
1023
1024	if (pswitch) {
1025		/*
1026		 * If the device is not configured up, we cannot put it in
1027		 * promiscuous mode.
1028		 */
1029		if ((ifp->if_flags & IFF_UP) == 0)
1030			return (ENETDOWN);
1031		if (ifp->if_pcount++ != 0)
1032			return (0);
1033		ifp->if_flags |= IFF_PROMISC;
1034		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1035		    ifp->if_name, ifp->if_unit);
1036	} else {
1037		if (--ifp->if_pcount > 0)
1038			return (0);
1039		ifp->if_flags &= ~IFF_PROMISC;
1040		log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1041		    ifp->if_name, ifp->if_unit);
1042	}
1043	ifr.ifr_flags = ifp->if_flags;
1044	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1045	if (error == 0)
1046		rt_ifmsg(ifp);
1047	return error;
1048}
1049
1050/*
1051 * Return interface configuration
1052 * of system.  List may be used
1053 * in later ioctl's (above) to get
1054 * other information.
1055 */
1056/*ARGSUSED*/
1057static int
1058ifconf(cmd, data)
1059	u_long cmd;
1060	caddr_t data;
1061{
1062	register struct ifconf *ifc = (struct ifconf *)data;
1063	register struct ifnet *ifp = TAILQ_FIRST(&ifnet);
1064	register struct ifaddr *ifa;
1065	struct ifreq ifr, *ifrp;
1066	int space = ifc->ifc_len, error = 0;
1067
1068	ifrp = ifc->ifc_req;
1069	for (; space > sizeof (ifr) && ifp; ifp = TAILQ_NEXT(ifp, if_link)) {
1070		char workbuf[64];
1071		int ifnlen, addrs;
1072
1073		ifnlen = snprintf(workbuf, sizeof(workbuf),
1074		    "%s%d", ifp->if_name, ifp->if_unit);
1075		if(ifnlen + 1 > sizeof ifr.ifr_name) {
1076			error = ENAMETOOLONG;
1077			break;
1078		} else {
1079			strcpy(ifr.ifr_name, workbuf);
1080		}
1081
1082		addrs = 0;
1083		ifa = TAILQ_FIRST(&ifp->if_addrhead);
1084		for ( ; space > sizeof (ifr) && ifa;
1085		    ifa = TAILQ_NEXT(ifa, ifa_link)) {
1086			register struct sockaddr *sa = ifa->ifa_addr;
1087			if (curproc->p_prison && prison_if(curproc, sa))
1088				continue;
1089			addrs++;
1090#ifdef COMPAT_43
1091			if (cmd == OSIOCGIFCONF) {
1092				struct osockaddr *osa =
1093					 (struct osockaddr *)&ifr.ifr_addr;
1094				ifr.ifr_addr = *sa;
1095				osa->sa_family = sa->sa_family;
1096				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1097						sizeof (ifr));
1098				ifrp++;
1099			} else
1100#endif
1101			if (sa->sa_len <= sizeof(*sa)) {
1102				ifr.ifr_addr = *sa;
1103				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1104						sizeof (ifr));
1105				ifrp++;
1106			} else {
1107				if (space < sizeof (ifr) + sa->sa_len -
1108					    sizeof(*sa))
1109					break;
1110				space -= sa->sa_len - sizeof(*sa);
1111				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1112						sizeof (ifr.ifr_name));
1113				if (error == 0)
1114				    error = copyout((caddr_t)sa,
1115				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1116				ifrp = (struct ifreq *)
1117					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1118			}
1119			if (error)
1120				break;
1121			space -= sizeof (ifr);
1122		}
1123		if (error)
1124			break;
1125		if (!addrs) {
1126			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1127			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1128			    sizeof (ifr));
1129			if (error)
1130				break;
1131			space -= sizeof (ifr);
1132			ifrp++;
1133		}
1134	}
1135	ifc->ifc_len -= space;
1136	return (error);
1137}
1138
1139/*
1140 * Just like if_promisc(), but for all-multicast-reception mode.
1141 */
1142int
1143if_allmulti(ifp, onswitch)
1144	struct ifnet *ifp;
1145	int onswitch;
1146{
1147	int error = 0;
1148	int s = splimp();
1149
1150	if (onswitch) {
1151		if (ifp->if_amcount++ == 0) {
1152			ifp->if_flags |= IFF_ALLMULTI;
1153			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1154		}
1155	} else {
1156		if (ifp->if_amcount > 1) {
1157			ifp->if_amcount--;
1158		} else {
1159			ifp->if_amcount = 0;
1160			ifp->if_flags &= ~IFF_ALLMULTI;
1161			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1162		}
1163	}
1164	splx(s);
1165
1166	if (error == 0)
1167		rt_ifmsg(ifp);
1168	return error;
1169}
1170
1171/*
1172 * Add a multicast listenership to the interface in question.
1173 * The link layer provides a routine which converts
1174 */
1175int
1176if_addmulti(ifp, sa, retifma)
1177	struct ifnet *ifp;	/* interface to manipulate */
1178	struct sockaddr *sa;	/* address to add */
1179	struct ifmultiaddr **retifma;
1180{
1181	struct sockaddr *llsa, *dupsa;
1182	int error, s;
1183	struct ifmultiaddr *ifma;
1184
1185	/*
1186	 * If the matching multicast address already exists
1187	 * then don't add a new one, just add a reference
1188	 */
1189	for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma;
1190	     ifma = LIST_NEXT(ifma, ifma_link)) {
1191		if (equal(sa, ifma->ifma_addr)) {
1192			ifma->ifma_refcount++;
1193			if (retifma)
1194				*retifma = ifma;
1195			return 0;
1196		}
1197	}
1198
1199	/*
1200	 * Give the link layer a chance to accept/reject it, and also
1201	 * find out which AF_LINK address this maps to, if it isn't one
1202	 * already.
1203	 */
1204	if (ifp->if_resolvemulti) {
1205		error = ifp->if_resolvemulti(ifp, &llsa, sa);
1206		if (error) return error;
1207	} else {
1208		llsa = 0;
1209	}
1210
1211	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1212	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1213	bcopy(sa, dupsa, sa->sa_len);
1214
1215	ifma->ifma_addr = dupsa;
1216	ifma->ifma_lladdr = llsa;
1217	ifma->ifma_ifp = ifp;
1218	ifma->ifma_refcount = 1;
1219	ifma->ifma_protospec = 0;
1220	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1221
1222	/*
1223	 * Some network interfaces can scan the address list at
1224	 * interrupt time; lock them out.
1225	 */
1226	s = splimp();
1227	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1228	splx(s);
1229	*retifma = ifma;
1230
1231	if (llsa != 0) {
1232		for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma;
1233		     ifma = LIST_NEXT(ifma, ifma_link)) {
1234			if (equal(ifma->ifma_addr, llsa))
1235				break;
1236		}
1237		if (ifma) {
1238			ifma->ifma_refcount++;
1239		} else {
1240			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1241			       M_IFMADDR, M_WAITOK);
1242			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1243			       M_IFMADDR, M_WAITOK);
1244			bcopy(llsa, dupsa, llsa->sa_len);
1245			ifma->ifma_addr = dupsa;
1246			ifma->ifma_ifp = ifp;
1247			ifma->ifma_refcount = 1;
1248			s = splimp();
1249			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1250			splx(s);
1251		}
1252	}
1253	/*
1254	 * We are certain we have added something, so call down to the
1255	 * interface to let them know about it.
1256	 */
1257	s = splimp();
1258	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1259	splx(s);
1260
1261	return 0;
1262}
1263
1264/*
1265 * Remove a reference to a multicast address on this interface.  Yell
1266 * if the request does not match an existing membership.
1267 */
1268int
1269if_delmulti(ifp, sa)
1270	struct ifnet *ifp;
1271	struct sockaddr *sa;
1272{
1273	struct ifmultiaddr *ifma;
1274	int s;
1275
1276	for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma;
1277	     ifma = LIST_NEXT(ifma, ifma_link))
1278		if (equal(sa, ifma->ifma_addr))
1279			break;
1280	if (ifma == 0)
1281		return ENOENT;
1282
1283	if (ifma->ifma_refcount > 1) {
1284		ifma->ifma_refcount--;
1285		return 0;
1286	}
1287
1288	rt_newmaddrmsg(RTM_DELMADDR, ifma);
1289	sa = ifma->ifma_lladdr;
1290	s = splimp();
1291	LIST_REMOVE(ifma, ifma_link);
1292	splx(s);
1293	free(ifma->ifma_addr, M_IFMADDR);
1294	free(ifma, M_IFMADDR);
1295	if (sa == 0)
1296		return 0;
1297
1298	/*
1299	 * Now look for the link-layer address which corresponds to
1300	 * this network address.  It had been squirreled away in
1301	 * ifma->ifma_lladdr for this purpose (so we don't have
1302	 * to call ifp->if_resolvemulti() again), and we saved that
1303	 * value in sa above.  If some nasty deleted the
1304	 * link-layer address out from underneath us, we can deal because
1305	 * the address we stored was is not the same as the one which was
1306	 * in the record for the link-layer address.  (So we don't complain
1307	 * in that case.)
1308	 */
1309	for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma;
1310	     ifma = LIST_NEXT(ifma, ifma_link))
1311		if (equal(sa, ifma->ifma_addr))
1312			break;
1313	if (ifma == 0)
1314		return 0;
1315
1316	if (ifma->ifma_refcount > 1) {
1317		ifma->ifma_refcount--;
1318		return 0;
1319	}
1320
1321	s = splimp();
1322	LIST_REMOVE(ifma, ifma_link);
1323	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1324	splx(s);
1325	free(ifma->ifma_addr, M_IFMADDR);
1326	free(sa, M_IFMADDR);
1327	free(ifma, M_IFMADDR);
1328
1329	return 0;
1330}
1331
1332/*
1333 * Set the link layer address on an interface.
1334 *
1335 * At this time we only support certain types of interfaces,
1336 * and we don't allow the length of the address to change.
1337 */
1338int
1339if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1340{
1341	struct sockaddr_dl *sdl;
1342	struct ifaddr *ifa;
1343
1344	ifa = ifnet_addrs[ifp->if_index - 1];
1345	if (ifa == NULL)
1346		return (EINVAL);
1347	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1348	if (sdl == NULL)
1349		return (EINVAL);
1350	if (len != sdl->sdl_alen)	/* don't allow length to change */
1351		return (EINVAL);
1352	switch (ifp->if_type) {
1353	case IFT_ETHER:			/* these types use struct arpcom */
1354	case IFT_FDDI:
1355	case IFT_XETHER:
1356	case IFT_ISO88025:
1357	case IFT_PROPVIRTUAL:		/* XXX waiting for IFT_8021_VLAN */
1358		bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1359		bcopy(lladdr, LLADDR(sdl), len);
1360		break;
1361	default:
1362		return (ENODEV);
1363	}
1364	/*
1365	 * If the interface is already up, we need
1366	 * to re-init it in order to reprogram its
1367	 * address filter.
1368	 */
1369	if ((ifp->if_flags & IFF_UP) != 0) {
1370		ifp->if_flags &= ~IFF_UP;
1371		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1372		ifp->if_flags |= IFF_UP;
1373		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1374	}
1375	return (0);
1376}
1377
1378struct ifmultiaddr *
1379ifmaof_ifpforaddr(sa, ifp)
1380	struct sockaddr *sa;
1381	struct ifnet *ifp;
1382{
1383	struct ifmultiaddr *ifma;
1384
1385	for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma;
1386	     ifma = LIST_NEXT(ifma, ifma_link))
1387		if (equal(ifma->ifma_addr, sa))
1388			break;
1389
1390	return ifma;
1391}
1392
1393SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1394SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1395