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