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