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