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