Avoid leaving an obsolete pointer while the interface is detaching
[dragonfly.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: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $ 
35  * $DragonFly: src/sys/net/if.c,v 1.21 2004/09/13 23:45:57 drhodus Exp $
36  */
37
38 #include "opt_compat.h"
39 #include "opt_inet6.h"
40 #include "opt_inet.h"
41
42 #include <sys/param.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/socketops.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
53 #include <sys/sockio.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
56
57 #include <net/if.h>
58 #include <net/if_arp.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/if_var.h>
62 #include <net/radix.h>
63 #include <net/route.h>
64 #include <machine/stdarg.h>
65
66 #if defined(INET) || defined(INET6)
67 /*XXX*/
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <netinet/if_ether.h>
71 #ifdef INET6
72 #include <machine/clock.h> /* XXX: temporal workaround for fxp issue */
73 #include <netinet6/in6_var.h>
74 #include <netinet6/in6_ifattach.h>
75 #endif
76 #endif
77
78 #if defined(COMPAT_43)
79 #include <emulation/43bsd/43bsd_socket.h>
80 #endif /* COMPAT_43 */
81
82 /*
83  * System initialization
84  */
85
86 static int ifconf (u_long, caddr_t, struct thread *);
87 static void ifinit (void *);
88 static void if_qflush (struct ifqueue *);
89 static void if_slowtimo (void *);
90 static void link_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
91 static int  if_rtdel (struct radix_node *, void *);
92
93 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
94
95 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
96 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
97 MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
98
99 int     ifqmaxlen = IFQ_MAXLEN;
100 struct  ifnethead ifnet;        /* depend on static init XXX */
101
102 #ifdef INET6
103 /*
104  * XXX: declare here to avoid to include many inet6 related files..
105  * should be more generalized?
106  */
107 extern void     nd6_setmtu (struct ifnet *);
108 #endif
109
110 struct if_clone *if_clone_lookup (const char *, int *);
111 int if_clone_list (struct if_clonereq *);
112
113 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
114 int if_cloners_count;
115
116 /*
117  * Network interface utility routines.
118  *
119  * Routines with ifa_ifwith* names take sockaddr *'s as
120  * parameters.
121  */
122 /* ARGSUSED*/
123 void
124 ifinit(dummy)
125         void *dummy;
126 {
127         struct ifnet *ifp;
128         int s;
129
130         s = splimp();
131         TAILQ_FOREACH(ifp, &ifnet, if_link) {
132                 if (ifp->if_snd.ifq_maxlen == 0) {
133                         if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
134                         ifp->if_snd.ifq_maxlen = ifqmaxlen;
135                 }
136         }
137         splx(s);
138         if_slowtimo(0);
139 }
140
141 int if_index = 0;
142 struct ifaddr **ifnet_addrs;
143 struct ifnet **ifindex2ifnet = NULL;
144
145
146 /*
147  * Attach an interface to the
148  * list of "active" interfaces.
149  */
150 void
151 if_attach(ifp)
152         struct ifnet *ifp;
153 {
154         unsigned socksize, ifasize;
155         int namelen, masklen;
156         struct sockaddr_dl *sdl;
157         struct ifaddr *ifa;
158         static int if_indexlim = 8;
159         static int inited;
160
161         if (!inited) {
162                 TAILQ_INIT(&ifnet);
163                 inited = 1;
164         }
165
166         TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
167         ifp->if_index = ++if_index;
168         /*
169          * XXX -
170          * The old code would work if the interface passed a pre-existing
171          * chain of ifaddrs to this code.  We don't trust our callers to
172          * properly initialize the tailq, however, so we no longer allow
173          * this unlikely case.
174          */
175         TAILQ_INIT(&ifp->if_addrhead);
176         TAILQ_INIT(&ifp->if_prefixhead);
177         LIST_INIT(&ifp->if_multiaddrs);
178         getmicrotime(&ifp->if_lastchange);
179         if (ifnet_addrs == 0 || if_index >= if_indexlim) {
180                 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
181                 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
182                 bzero(q, n);
183                 if (ifnet_addrs) {
184                         bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
185                         free((caddr_t)ifnet_addrs, M_IFADDR);
186                 }
187                 ifnet_addrs = (struct ifaddr **)q;
188
189                 /* grow ifindex2ifnet */
190                 n = if_indexlim * sizeof(struct ifnet *);
191                 q = malloc(n, M_IFADDR, M_WAITOK);
192                 bzero(q, n);
193                 if (ifindex2ifnet) {
194                         bcopy((caddr_t)ifindex2ifnet, q, n/2);
195                         free((caddr_t)ifindex2ifnet, M_IFADDR);
196                 }
197                 ifindex2ifnet = (struct ifnet **)q;
198         }
199
200         ifindex2ifnet[if_index] = ifp;
201
202         /*
203          * create a Link Level name for this device
204          */
205         namelen = strlen(ifp->if_xname);
206 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
207         masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
208         socksize = masklen + ifp->if_addrlen;
209 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
210         if (socksize < sizeof(*sdl))
211                 socksize = sizeof(*sdl);
212         socksize = ROUNDUP(socksize);
213         ifasize = sizeof(*ifa) + 2 * socksize;
214         ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
215         if (ifa) {
216                 bzero((caddr_t)ifa, ifasize);
217                 sdl = (struct sockaddr_dl *)(ifa + 1);
218                 sdl->sdl_len = socksize;
219                 sdl->sdl_family = AF_LINK;
220                 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
221                 sdl->sdl_nlen = namelen;
222                 sdl->sdl_index = ifp->if_index;
223                 sdl->sdl_type = ifp->if_type;
224                 ifnet_addrs[if_index - 1] = ifa;
225                 ifa->ifa_ifp = ifp;
226                 ifa->ifa_rtrequest = link_rtrequest;
227                 ifa->ifa_addr = (struct sockaddr *)sdl;
228                 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
229                 ifa->ifa_netmask = (struct sockaddr *)sdl;
230                 sdl->sdl_len = masklen;
231                 while (namelen != 0)
232                         sdl->sdl_data[--namelen] = 0xff;
233                 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
234         }
235
236         EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
237
238         /* Announce the interface. */
239         rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
240 }
241
242 /*
243  * Detach an interface, removing it from the
244  * list of "active" interfaces.
245  */
246 void
247 if_detach(ifp)
248         struct ifnet *ifp;
249 {
250         struct ifaddr *ifa;
251         struct radix_node_head  *rnh;
252         int s;
253         int i;
254
255         EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
256
257         /*
258          * Remove routes and flush queues.
259          */
260         s = splnet();
261         if_down(ifp);
262
263         /*
264          * Remove address from ifnet_addrs[] and maybe decrement if_index.
265          * Clean up all addresses.
266          */
267         ifnet_addrs[ifp->if_index - 1] = 0;
268         while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
269                 if_index--;
270
271         for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
272              ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
273 #ifdef INET
274                 /* XXX: Ugly!! ad hoc just for INET */
275                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
276                         struct ifaliasreq ifr;
277
278                         bzero(&ifr, sizeof(ifr));
279                         ifr.ifra_addr = *ifa->ifa_addr;
280                         if (ifa->ifa_dstaddr)
281                                 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
282                         if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
283                             NULL) == 0)
284                                 continue;
285                 }
286 #endif /* INET */
287 #ifdef INET6
288                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
289                         in6_purgeaddr(ifa);
290                         /* ifp_addrhead is already updated */
291                         continue;
292                 }
293 #endif /* INET6 */
294                 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
295                 IFAFREE(ifa);
296         }
297
298 #ifdef INET6
299         /*
300          * Remove all IPv6 kernel structs related to ifp.  This should be done
301          * before removing routing entries below, since IPv6 interface direct
302          * routes are expected to be removed by the IPv6-specific kernel API.
303          * Otherwise, the kernel will detect some inconsistency and bark it.
304          */
305         in6_ifdetach(ifp);
306 #endif
307
308         /*
309          * Delete all remaining routes using this interface
310          * Unfortuneatly the only way to do this is to slog through
311          * the entire routing table looking for routes which point
312          * to this interface...oh well...
313          */
314         for (i = 1; i <= AF_MAX; i++) {
315                 if ((rnh = rt_tables[i]) == NULL)
316                         continue;
317                 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
318         }
319
320         /* Announce that the interface is gone. */
321         rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
322
323         ifindex2ifnet[ifp->if_index] = NULL;
324
325         TAILQ_REMOVE(&ifnet, ifp, if_link);
326         splx(s);
327 }
328
329 /*
330  * Delete Routes for a Network Interface
331  * 
332  * Called for each routing entry via the rnh->rnh_walktree() call above
333  * to delete all route entries referencing a detaching network interface.
334  *
335  * Arguments:
336  *      rn      pointer to node in the routing table
337  *      arg     argument passed to rnh->rnh_walktree() - detaching interface
338  *
339  * Returns:
340  *      0       successful
341  *      errno   failed - reason indicated
342  *
343  */
344 static int
345 if_rtdel(rn, arg)
346         struct radix_node       *rn;
347         void                    *arg;
348 {
349         struct rtentry  *rt = (struct rtentry *)rn;
350         struct ifnet    *ifp = arg;
351         int             err;
352
353         if (rt->rt_ifp == ifp) {
354
355                 /*
356                  * Protect (sorta) against walktree recursion problems
357                  * with cloned routes
358                  */
359                 if ((rt->rt_flags & RTF_UP) == 0)
360                         return (0);
361
362                 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
363                                 rt_mask(rt), rt->rt_flags,
364                                 (struct rtentry **) NULL);
365                 if (err) {
366                         log(LOG_WARNING, "if_rtdel: error %d\n", err);
367                 }
368         }
369
370         return (0);
371 }
372
373 /*
374  * Create a clone network interface.
375  */
376 int
377 if_clone_create(name, len)
378         char *name;
379         int len;
380 {
381         struct if_clone *ifc;
382         char *dp;
383         int wildcard, bytoff, bitoff;
384         int unit;
385         int err;
386
387         ifc = if_clone_lookup(name, &unit);
388         if (ifc == NULL)
389                 return (EINVAL);
390
391         if (ifunit(name) != NULL)
392                 return (EEXIST);
393
394         bytoff = bitoff = 0;
395         wildcard = (unit < 0);
396         /*
397          * Find a free unit if none was given.
398          */
399         if (wildcard) {
400                 while ((bytoff < ifc->ifc_bmlen)
401                     && (ifc->ifc_units[bytoff] == 0xff))
402                         bytoff++;
403                 if (bytoff >= ifc->ifc_bmlen)
404                         return (ENOSPC);
405                 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
406                         bitoff++;
407                 unit = (bytoff << 3) + bitoff;
408         }
409
410         if (unit > ifc->ifc_maxunit)
411                 return (ENXIO);
412
413         err = (*ifc->ifc_create)(ifc, unit);
414         if (err != 0)
415                 return (err);
416
417         if (!wildcard) {
418                 bytoff = unit >> 3;
419                 bitoff = unit - (bytoff << 3);
420         }
421
422         /*
423          * Allocate the unit in the bitmap.
424          */
425         KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
426             ("%s: bit is already set", __func__));
427         ifc->ifc_units[bytoff] |= (1 << bitoff);
428
429         /* In the wildcard case, we need to update the name. */
430         if (wildcard) {
431                 for (dp = name; *dp != '\0'; dp++);
432                 if (snprintf(dp, len - (dp-name), "%d", unit) >
433                     len - (dp-name) - 1) {
434                         /*
435                          * This can only be a programmer error and
436                          * there's no straightforward way to recover if
437                          * it happens.
438                          */
439                         panic("if_clone_create(): interface name too long");
440                 }
441
442         }
443
444         EVENTHANDLER_INVOKE(if_clone_event, ifc);
445
446         return (0);
447 }
448
449 /*
450  * Destroy a clone network interface.
451  */
452 int
453 if_clone_destroy(name)
454         const char *name;
455 {
456         struct if_clone *ifc;
457         struct ifnet *ifp;
458         int bytoff, bitoff;
459         int unit;
460
461         ifc = if_clone_lookup(name, &unit);
462         if (ifc == NULL)
463                 return (EINVAL);
464
465         if (unit < ifc->ifc_minifs)
466                 return (EINVAL);
467
468         ifp = ifunit(name);
469         if (ifp == NULL)
470                 return (ENXIO);
471
472         if (ifc->ifc_destroy == NULL)
473                 return (EOPNOTSUPP);
474
475         (*ifc->ifc_destroy)(ifp);
476
477         /*
478          * Compute offset in the bitmap and deallocate the unit.
479          */
480         bytoff = unit >> 3;
481         bitoff = unit - (bytoff << 3);
482         KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
483             ("%s: bit is already cleared", __func__));
484         ifc->ifc_units[bytoff] &= ~(1 << bitoff);
485         return (0);
486 }
487
488 /*
489  * Look up a network interface cloner.
490  */
491 struct if_clone *
492 if_clone_lookup(name, unitp)
493         const char *name;
494         int *unitp;
495 {
496         struct if_clone *ifc;
497         const char *cp;
498         int i;
499
500         for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
501                 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
502                         if (ifc->ifc_name[i] != *cp)
503                                 goto next_ifc;
504                 }
505                 goto found_name;
506  next_ifc:
507                 ifc = LIST_NEXT(ifc, ifc_list);
508         }
509
510         /* No match. */
511         return ((struct if_clone *)NULL);
512
513  found_name:
514         if (*cp == '\0') {
515                 i = -1;
516         } else {
517                 for (i = 0; *cp != '\0'; cp++) {
518                         if (*cp < '0' || *cp > '9') {
519                                 /* Bogus unit number. */
520                                 return (NULL);
521                         }
522                         i = (i * 10) + (*cp - '0');
523                 }
524         }
525
526         if (unitp != NULL)
527                 *unitp = i;
528         return (ifc);
529 }
530
531 /*
532  * Register a network interface cloner.
533  */
534 void
535 if_clone_attach(ifc)
536         struct if_clone *ifc;
537 {
538         int bytoff, bitoff;
539         int err;
540         int len, maxclone;
541         int unit;
542
543         KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
544             ("%s: %s requested more units then allowed (%d > %d)",
545             __func__, ifc->ifc_name, ifc->ifc_minifs,
546             ifc->ifc_maxunit + 1));
547         /*
548          * Compute bitmap size and allocate it.
549          */
550         maxclone = ifc->ifc_maxunit + 1;
551         len = maxclone >> 3;
552         if ((len << 3) < maxclone)
553                 len++;
554         ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
555         ifc->ifc_bmlen = len;
556
557         LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
558         if_cloners_count++;
559
560         for (unit = 0; unit < ifc->ifc_minifs; unit++) {
561                 err = (*ifc->ifc_create)(ifc, unit);
562                 KASSERT(err == 0,
563                     ("%s: failed to create required interface %s%d",
564                     __func__, ifc->ifc_name, unit));
565
566                 /* Allocate the unit in the bitmap. */
567                 bytoff = unit >> 3;
568                 bitoff = unit - (bytoff << 3);
569                 ifc->ifc_units[bytoff] |= (1 << bitoff);
570         }
571 }
572
573 /*
574  * Unregister a network interface cloner.
575  */
576 void
577 if_clone_detach(ifc)
578         struct if_clone *ifc;
579 {
580
581         LIST_REMOVE(ifc, ifc_list);
582         free(ifc->ifc_units, M_CLONE);
583         if_cloners_count--;
584 }
585
586 /*
587  * Provide list of interface cloners to userspace.
588  */
589 int
590 if_clone_list(ifcr)
591         struct if_clonereq *ifcr;
592 {
593         char outbuf[IFNAMSIZ], *dst;
594         struct if_clone *ifc;
595         int count, error = 0;
596
597         ifcr->ifcr_total = if_cloners_count;
598         if ((dst = ifcr->ifcr_buffer) == NULL) {
599                 /* Just asking how many there are. */
600                 return (0);
601         }
602
603         if (ifcr->ifcr_count < 0)
604                 return (EINVAL);
605
606         count = (if_cloners_count < ifcr->ifcr_count) ?
607             if_cloners_count : ifcr->ifcr_count;
608
609         for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
610              ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
611                 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
612                 error = copyout(outbuf, dst, IFNAMSIZ);
613                 if (error)
614                         break;
615         }
616
617         return (error);
618 }
619
620 /*
621  * Locate an interface based on a complete address.
622  */
623 /*ARGSUSED*/
624 struct ifaddr *
625 ifa_ifwithaddr(addr)
626         struct sockaddr *addr;
627 {
628         struct ifnet *ifp;
629         struct ifaddr *ifa;
630
631 #define equal(a1, a2) \
632   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
633         TAILQ_FOREACH(ifp, &ifnet, if_link)
634             TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
635                 if (ifa->ifa_addr->sa_family != addr->sa_family)
636                         continue;
637                 if (equal(addr, ifa->ifa_addr))
638                         return (ifa);
639                 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
640                     /* IP6 doesn't have broadcast */
641                     ifa->ifa_broadaddr->sa_len != 0 &&
642                     equal(ifa->ifa_broadaddr, addr))
643                         return (ifa);
644         }
645         return ((struct ifaddr *)0);
646 }
647 /*
648  * Locate the point to point interface with a given destination address.
649  */
650 /*ARGSUSED*/
651 struct ifaddr *
652 ifa_ifwithdstaddr(addr)
653         struct sockaddr *addr;
654 {
655         struct ifnet *ifp;
656         struct ifaddr *ifa;
657
658         TAILQ_FOREACH(ifp, &ifnet, if_link)
659             if (ifp->if_flags & IFF_POINTOPOINT)
660                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
661                         if (ifa->ifa_addr->sa_family != addr->sa_family)
662                                 continue;
663                         if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
664                                 return (ifa);
665         }
666         return ((struct ifaddr *)0);
667 }
668
669 /*
670  * Find an interface on a specific network.  If many, choice
671  * is most specific found.
672  */
673 struct ifaddr *
674 ifa_ifwithnet(addr)
675         struct sockaddr *addr;
676 {
677         struct ifnet *ifp;
678         struct ifaddr *ifa;
679         struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
680         u_int af = addr->sa_family;
681         char *addr_data = addr->sa_data, *cplim;
682
683         /*
684          * AF_LINK addresses can be looked up directly by their index number,
685          * so do that if we can.
686          */
687         if (af == AF_LINK) {
688             struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
689             if (sdl->sdl_index && sdl->sdl_index <= if_index)
690                 return (ifnet_addrs[sdl->sdl_index - 1]);
691         }
692
693         /*
694          * Scan though each interface, looking for ones that have
695          * addresses in this address family.
696          */
697         TAILQ_FOREACH(ifp, &ifnet, if_link) {
698                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
699                         char *cp, *cp2, *cp3;
700
701                         if (ifa->ifa_addr->sa_family != af)
702 next:                           continue;
703                         if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
704                                 /*
705                                  * This is a bit broken as it doesn't
706                                  * take into account that the remote end may
707                                  * be a single node in the network we are
708                                  * looking for.
709                                  * The trouble is that we don't know the
710                                  * netmask for the remote end.
711                                  */
712                                 if (ifa->ifa_dstaddr != 0
713                                     && equal(addr, ifa->ifa_dstaddr))
714                                         return (ifa);
715                         } else {
716                                 /*
717                                  * if we have a special address handler,
718                                  * then use it instead of the generic one.
719                                  */
720                                 if (ifa->ifa_claim_addr) {
721                                         if ((*ifa->ifa_claim_addr)(ifa, addr)) {
722                                                 return (ifa);
723                                         } else {
724                                                 continue;
725                                         }
726                                 }
727
728                                 /*
729                                  * Scan all the bits in the ifa's address.
730                                  * If a bit dissagrees with what we are
731                                  * looking for, mask it with the netmask
732                                  * to see if it really matters.
733                                  * (A byte at a time)
734                                  */
735                                 if (ifa->ifa_netmask == 0)
736                                         continue;
737                                 cp = addr_data;
738                                 cp2 = ifa->ifa_addr->sa_data;
739                                 cp3 = ifa->ifa_netmask->sa_data;
740                                 cplim = ifa->ifa_netmask->sa_len
741                                         + (char *)ifa->ifa_netmask;
742                                 while (cp3 < cplim)
743                                         if ((*cp++ ^ *cp2++) & *cp3++)
744                                                 goto next; /* next address! */
745                                 /*
746                                  * If the netmask of what we just found
747                                  * is more specific than what we had before
748                                  * (if we had one) then remember the new one
749                                  * before continuing to search
750                                  * for an even better one.
751                                  */
752                                 if (ifa_maybe == 0 ||
753                                     rn_refines((caddr_t)ifa->ifa_netmask,
754                                     (caddr_t)ifa_maybe->ifa_netmask))
755                                         ifa_maybe = ifa;
756                         }
757                 }
758         }
759         return (ifa_maybe);
760 }
761
762 /*
763  * Find an interface address specific to an interface best matching
764  * a given address.
765  */
766 struct ifaddr *
767 ifaof_ifpforaddr(addr, ifp)
768         struct sockaddr *addr;
769         struct ifnet *ifp;
770 {
771         struct ifaddr *ifa;
772         char *cp, *cp2, *cp3;
773         char *cplim;
774         struct ifaddr *ifa_maybe = 0;
775         u_int af = addr->sa_family;
776
777         if (af >= AF_MAX)
778                 return (0);
779         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
780                 if (ifa->ifa_addr->sa_family != af)
781                         continue;
782                 if (ifa_maybe == 0)
783                         ifa_maybe = ifa;
784                 if (ifa->ifa_netmask == 0) {
785                         if (equal(addr, ifa->ifa_addr) ||
786                             (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
787                                 return (ifa);
788                         continue;
789                 }
790                 if (ifp->if_flags & IFF_POINTOPOINT) {
791                         if (equal(addr, ifa->ifa_dstaddr))
792                                 return (ifa);
793                 } else {
794                         cp = addr->sa_data;
795                         cp2 = ifa->ifa_addr->sa_data;
796                         cp3 = ifa->ifa_netmask->sa_data;
797                         cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
798                         for (; cp3 < cplim; cp3++)
799                                 if ((*cp++ ^ *cp2++) & *cp3)
800                                         break;
801                         if (cp3 == cplim)
802                                 return (ifa);
803                 }
804         }
805         return (ifa_maybe);
806 }
807
808 #include <net/route.h>
809
810 /*
811  * Default action when installing a route with a Link Level gateway.
812  * Lookup an appropriate real ifa to point to.
813  * This should be moved to /sys/net/link.c eventually.
814  */
815 static void
816 link_rtrequest(cmd, rt, info)
817         int cmd;
818         struct rtentry *rt;
819         struct rt_addrinfo *info;
820 {
821         struct ifaddr *ifa;
822         struct sockaddr *dst;
823         struct ifnet *ifp;
824
825         if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
826             ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
827                 return;
828         ifa = ifaof_ifpforaddr(dst, ifp);
829         if (ifa) {
830                 IFAFREE(rt->rt_ifa);
831                 rt->rt_ifa = ifa;
832                 ifa->ifa_refcnt++;
833                 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
834                         ifa->ifa_rtrequest(cmd, rt, info);
835         }
836 }
837
838 /*
839  * Mark an interface down and notify protocols of
840  * the transition.
841  * NOTE: must be called at splnet or eqivalent.
842  */
843 void
844 if_unroute(ifp, flag, fam)
845         struct ifnet *ifp;
846         int flag, fam;
847 {
848         struct ifaddr *ifa;
849
850         ifp->if_flags &= ~flag;
851         getmicrotime(&ifp->if_lastchange);
852         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
853                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
854                         pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
855         if_qflush(&ifp->if_snd);
856         rt_ifmsg(ifp);
857 }
858
859 /*
860  * Mark an interface up and notify protocols of
861  * the transition.
862  * NOTE: must be called at splnet or eqivalent.
863  */
864 void
865 if_route(ifp, flag, fam)
866         struct ifnet *ifp;
867         int flag, fam;
868 {
869         struct ifaddr *ifa;
870
871         ifp->if_flags |= flag;
872         getmicrotime(&ifp->if_lastchange);
873         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
874                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
875                         pfctlinput(PRC_IFUP, ifa->ifa_addr);
876         rt_ifmsg(ifp);
877 #ifdef INET6
878         in6_if_up(ifp);
879 #endif
880 }
881
882 /*
883  * Mark an interface down and notify protocols of
884  * the transition.
885  * NOTE: must be called at splnet or eqivalent.
886  */
887 void
888 if_down(ifp)
889         struct ifnet *ifp;
890 {
891
892         if_unroute(ifp, IFF_UP, AF_UNSPEC);
893 }
894
895 /*
896  * Mark an interface up and notify protocols of
897  * the transition.
898  * NOTE: must be called at splnet or eqivalent.
899  */
900 void
901 if_up(ifp)
902         struct ifnet *ifp;
903 {
904
905         if_route(ifp, IFF_UP, AF_UNSPEC);
906 }
907
908 /*
909  * Flush an interface queue.
910  */
911 static void
912 if_qflush(ifq)
913         struct ifqueue *ifq;
914 {
915         struct mbuf *m, *n;
916
917         n = ifq->ifq_head;
918         while ((m = n) != 0) {
919                 n = m->m_nextpkt;
920                 m_freem(m);
921         }
922         ifq->ifq_head = 0;
923         ifq->ifq_tail = 0;
924         ifq->ifq_len = 0;
925 }
926
927 /*
928  * Handle interface watchdog timer routines.  Called
929  * from softclock, we decrement timers (if set) and
930  * call the appropriate interface routine on expiration.
931  */
932 static void
933 if_slowtimo(arg)
934         void *arg;
935 {
936         struct ifnet *ifp;
937         int s = splimp();
938
939         TAILQ_FOREACH(ifp, &ifnet, if_link) {
940                 if (ifp->if_timer == 0 || --ifp->if_timer)
941                         continue;
942                 if (ifp->if_watchdog)
943                         (*ifp->if_watchdog)(ifp);
944         }
945         splx(s);
946         timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
947 }
948
949 /*
950  * Map interface name to
951  * interface structure pointer.
952  */
953 struct ifnet *
954 ifunit(const char *name)
955 {
956         struct ifnet *ifp;
957
958         /*
959          * Search all the interfaces for this name/number
960          */
961
962         TAILQ_FOREACH(ifp, &ifnet, if_link) {
963                 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
964                         break;
965         }
966         return (ifp);
967 }
968
969
970 /*
971  * Map interface name in a sockaddr_dl to
972  * interface structure pointer.
973  */
974 struct ifnet *
975 if_withname(sa)
976         struct sockaddr *sa;
977 {
978         char ifname[IFNAMSIZ+1];
979         struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
980
981         if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
982              (sdl->sdl_nlen > IFNAMSIZ) )
983                 return NULL;
984
985         /*
986          * ifunit wants a null-terminated name.  It may not be null-terminated
987          * in the sockaddr.  We don't want to change the caller's sockaddr,
988          * and there might not be room to put the trailing null anyway, so we
989          * make a local copy that we know we can null terminate safely.
990          */
991
992         bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
993         ifname[sdl->sdl_nlen] = '\0';
994         return ifunit(ifname);
995 }
996
997
998 /*
999  * Interface ioctls.
1000  */
1001 int
1002 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1003 {
1004         struct ifnet *ifp;
1005         struct ifreq *ifr;
1006         struct ifstat *ifs;
1007         int error;
1008         short oif_flags;
1009         int new_flags;
1010         size_t namelen, onamelen;
1011         char new_name[IFNAMSIZ];
1012         struct ifaddr *ifa;
1013         struct sockaddr_dl *sdl;
1014
1015         switch (cmd) {
1016
1017         case SIOCGIFCONF:
1018         case OSIOCGIFCONF:
1019                 return (ifconf(cmd, data, td));
1020         }
1021         ifr = (struct ifreq *)data;
1022
1023         switch (cmd) {
1024         case SIOCIFCREATE:
1025         case SIOCIFDESTROY:
1026                 if ((error = suser(td)) != 0)
1027                         return (error);
1028                 return ((cmd == SIOCIFCREATE) ?
1029                         if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1030                         if_clone_destroy(ifr->ifr_name));
1031         
1032         case SIOCIFGCLONERS:
1033                 return (if_clone_list((struct if_clonereq *)data));
1034         }
1035
1036         ifp = ifunit(ifr->ifr_name);
1037         if (ifp == 0)
1038                 return (ENXIO);
1039         switch (cmd) {
1040
1041         case SIOCGIFFLAGS:
1042                 ifr->ifr_flags = ifp->if_flags;
1043                 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1044                 break;
1045
1046         case SIOCGIFCAP:
1047                 ifr->ifr_reqcap = ifp->if_capabilities;
1048                 ifr->ifr_curcap = ifp->if_capenable;
1049                 break;
1050
1051         case SIOCGIFMETRIC:
1052                 ifr->ifr_metric = ifp->if_metric;
1053                 break;
1054
1055         case SIOCGIFMTU:
1056                 ifr->ifr_mtu = ifp->if_mtu;
1057                 break;
1058
1059         case SIOCGIFPHYS:
1060                 ifr->ifr_phys = ifp->if_physical;
1061                 break;
1062
1063         case SIOCSIFFLAGS:
1064                 error = suser(td);
1065                 if (error)
1066                         return (error);
1067                 new_flags = (ifr->ifr_flags & 0xffff) |
1068                     (ifr->ifr_flagshigh << 16);
1069                 if (ifp->if_flags & IFF_SMART) {
1070                         /* Smart drivers twiddle their own routes */
1071                 } else if (ifp->if_flags & IFF_UP &&
1072                     (new_flags & IFF_UP) == 0) {
1073                         int s = splimp();
1074                         if_down(ifp);
1075                         splx(s);
1076                 } else if (new_flags & IFF_UP &&
1077                     (ifp->if_flags & IFF_UP) == 0) {
1078                         int s = splimp();
1079                         if_up(ifp);
1080                         splx(s);
1081                 }
1082                 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1083                         (new_flags &~ IFF_CANTCHANGE);
1084                 if (new_flags & IFF_PPROMISC) {
1085                         /* Permanently promiscuous mode requested */
1086                         ifp->if_flags |= IFF_PROMISC;
1087                 } else if (ifp->if_pcount == 0) {
1088                         ifp->if_flags &= ~IFF_PROMISC;
1089                 }
1090                 if (ifp->if_ioctl)
1091                         (void) (*ifp->if_ioctl)(ifp, cmd, data,
1092                                                 td->td_proc->p_ucred);
1093                 getmicrotime(&ifp->if_lastchange);
1094                 break;
1095
1096         case SIOCSIFCAP:
1097                 error = suser(td);
1098                 if (error)
1099                         return (error);
1100                 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1101                         return (EINVAL);
1102                 (void) (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1103                 break;
1104
1105         case SIOCSIFNAME:
1106                 error = suser(td);
1107                 if (error != 0)
1108                         return (error);
1109                 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1110                 if (error != 0)
1111                         return (error);
1112                 if (new_name[0] == '\0')
1113                         return (EINVAL);
1114                 if (ifunit(new_name) != NULL)
1115                         return (EEXIST);
1116
1117                 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1118                 
1119                 /* Announce the departure of the interface. */
1120                 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1121  
1122                 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1123                 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1124                 /* XXX IFA_LOCK(ifa); */
1125                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1126                 namelen = strlen(new_name);
1127                 onamelen = sdl->sdl_nlen;
1128                 /*
1129                  * Move the address if needed.  This is safe because we
1130                  * allocate space for a name of length IFNAMSIZ when we
1131                  * create this in if_attach().
1132                  */
1133                 if (namelen != onamelen) {
1134                         bcopy(sdl->sdl_data + onamelen,
1135                             sdl->sdl_data + namelen, sdl->sdl_alen);
1136                 }
1137                 bcopy(new_name, sdl->sdl_data, namelen);
1138                 sdl->sdl_nlen = namelen;
1139                 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1140                 bzero(sdl->sdl_data, onamelen);
1141                 while (namelen != 0)
1142                         sdl->sdl_data[--namelen] = 0xff;
1143                 /* XXX IFA_UNLOCK(ifa) */
1144
1145                 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1146  
1147                 /* Announce the return of the interface. */
1148                 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1149                 break;
1150
1151         case SIOCSIFMETRIC:
1152                 error = suser(td);
1153                 if (error)
1154                         return (error);
1155                 ifp->if_metric = ifr->ifr_metric;
1156                 getmicrotime(&ifp->if_lastchange);
1157                 break;
1158
1159         case SIOCSIFPHYS:
1160                 error = suser(td);
1161                 if (error)
1162                         return error;
1163                 if (!ifp->if_ioctl)
1164                         return EOPNOTSUPP;
1165                 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1166                 if (error == 0)
1167                         getmicrotime(&ifp->if_lastchange);
1168                 return(error);
1169
1170         case SIOCSIFMTU:
1171         {
1172                 u_long oldmtu = ifp->if_mtu;
1173
1174                 error = suser(td);
1175                 if (error)
1176                         return (error);
1177                 if (ifp->if_ioctl == NULL)
1178                         return (EOPNOTSUPP);
1179                 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1180                         return (EINVAL);
1181                 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1182                 if (error == 0) {
1183                         getmicrotime(&ifp->if_lastchange);
1184                         rt_ifmsg(ifp);
1185                 }
1186                 /*
1187                  * If the link MTU changed, do network layer specific procedure.
1188                  */
1189                 if (ifp->if_mtu != oldmtu) {
1190 #ifdef INET6
1191                         nd6_setmtu(ifp);
1192 #endif
1193                 }
1194                 return (error);
1195         }
1196
1197         case SIOCADDMULTI:
1198         case SIOCDELMULTI:
1199                 error = suser(td);
1200                 if (error)
1201                         return (error);
1202
1203                 /* Don't allow group membership on non-multicast interfaces. */
1204                 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1205                         return EOPNOTSUPP;
1206
1207                 /* Don't let users screw up protocols' entries. */
1208                 if (ifr->ifr_addr.sa_family != AF_LINK)
1209                         return EINVAL;
1210
1211                 if (cmd == SIOCADDMULTI) {
1212                         struct ifmultiaddr *ifma;
1213                         error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1214                 } else {
1215                         error = if_delmulti(ifp, &ifr->ifr_addr);
1216                 }
1217                 if (error == 0)
1218                         getmicrotime(&ifp->if_lastchange);
1219                 return error;
1220
1221         case SIOCSIFPHYADDR:
1222         case SIOCDIFPHYADDR:
1223 #ifdef INET6
1224         case SIOCSIFPHYADDR_IN6:
1225 #endif
1226         case SIOCSLIFPHYADDR:
1227         case SIOCSIFMEDIA:
1228         case SIOCSIFGENERIC:
1229                 error = suser(td);
1230                 if (error)
1231                         return (error);
1232                 if (ifp->if_ioctl == 0)
1233                         return (EOPNOTSUPP);
1234                 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
1235                 if (error == 0)
1236                         getmicrotime(&ifp->if_lastchange);
1237                 return error;
1238
1239         case SIOCGIFSTATUS:
1240                 ifs = (struct ifstat *)data;
1241                 ifs->ascii[0] = '\0';
1242                 
1243         case SIOCGIFPSRCADDR:
1244         case SIOCGIFPDSTADDR:
1245         case SIOCGLIFPHYADDR:
1246         case SIOCGIFMEDIA:
1247         case SIOCGIFGENERIC:
1248                 if (ifp->if_ioctl == 0)
1249                         return (EOPNOTSUPP);
1250                 return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
1251
1252         case SIOCSIFLLADDR:
1253                 error = suser(td);
1254                 if (error)
1255                         return (error);
1256                 return if_setlladdr(ifp,
1257                     ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1258
1259         default:
1260                 oif_flags = ifp->if_flags;
1261                 if (so->so_proto == 0)
1262                         return (EOPNOTSUPP);
1263 #ifndef COMPAT_43
1264                 error = so_pru_control(so, cmd, data, ifp, td);
1265 #else
1266             {
1267                 int ocmd = cmd;
1268
1269                 switch (cmd) {
1270
1271                 case SIOCSIFDSTADDR:
1272                 case SIOCSIFADDR:
1273                 case SIOCSIFBRDADDR:
1274                 case SIOCSIFNETMASK:
1275 #if BYTE_ORDER != BIG_ENDIAN
1276                         if (ifr->ifr_addr.sa_family == 0 &&
1277                             ifr->ifr_addr.sa_len < 16) {
1278                                 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1279                                 ifr->ifr_addr.sa_len = 16;
1280                         }
1281 #else
1282                         if (ifr->ifr_addr.sa_len == 0)
1283                                 ifr->ifr_addr.sa_len = 16;
1284 #endif
1285                         break;
1286
1287                 case OSIOCGIFADDR:
1288                         cmd = SIOCGIFADDR;
1289                         break;
1290
1291                 case OSIOCGIFDSTADDR:
1292                         cmd = SIOCGIFDSTADDR;
1293                         break;
1294
1295                 case OSIOCGIFBRDADDR:
1296                         cmd = SIOCGIFBRDADDR;
1297                         break;
1298
1299                 case OSIOCGIFNETMASK:
1300                         cmd = SIOCGIFNETMASK;
1301                 }
1302                 error =  so_pru_control(so, cmd, data, ifp, td);
1303                 switch (ocmd) {
1304
1305                 case OSIOCGIFADDR:
1306                 case OSIOCGIFDSTADDR:
1307                 case OSIOCGIFBRDADDR:
1308                 case OSIOCGIFNETMASK:
1309                         *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1310
1311                 }
1312             }
1313 #endif /* COMPAT_43 */
1314
1315                 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1316 #ifdef INET6
1317                         DELAY(100);/* XXX: temporary workaround for fxp issue*/
1318                         if (ifp->if_flags & IFF_UP) {
1319                                 int s = splimp();
1320                                 in6_if_up(ifp);
1321                                 splx(s);
1322                         }
1323 #endif
1324                 }
1325                 return (error);
1326
1327         }
1328         return (0);
1329 }
1330
1331 /*
1332  * Set/clear promiscuous mode on interface ifp based on the truth value
1333  * of pswitch.  The calls are reference counted so that only the first
1334  * "on" request actually has an effect, as does the final "off" request.
1335  * Results are undefined if the "off" and "on" requests are not matched.
1336  */
1337 int
1338 ifpromisc(ifp, pswitch)
1339         struct ifnet *ifp;
1340         int pswitch;
1341 {
1342         struct ifreq ifr;
1343         int error;
1344         int oldflags;
1345
1346         oldflags = ifp->if_flags;
1347         if (ifp->if_flags & IFF_PPROMISC) {
1348                 /* Do nothing if device is in permanently promiscuous mode */
1349                 ifp->if_pcount += pswitch ? 1 : -1;
1350                 return (0);
1351         }
1352         if (pswitch) {
1353                 /*
1354                  * If the device is not configured up, we cannot put it in
1355                  * promiscuous mode.
1356                  */
1357                 if ((ifp->if_flags & IFF_UP) == 0)
1358                         return (ENETDOWN);
1359                 if (ifp->if_pcount++ != 0)
1360                         return (0);
1361                 ifp->if_flags |= IFF_PROMISC;
1362                 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1363                     ifp->if_xname);
1364         } else {
1365                 if (--ifp->if_pcount > 0)
1366                         return (0);
1367                 ifp->if_flags &= ~IFF_PROMISC;
1368                 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1369                     ifp->if_xname);
1370         }
1371         ifr.ifr_flags = ifp->if_flags;
1372         ifr.ifr_flagshigh = ifp->if_flags >> 16;
1373         error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1374                                  (struct ucred *)NULL);
1375         if (error == 0)
1376                 rt_ifmsg(ifp);
1377         else
1378                 ifp->if_flags = oldflags;
1379         return error;
1380 }
1381
1382 /*
1383  * Return interface configuration
1384  * of system.  List may be used
1385  * in later ioctl's (above) to get
1386  * other information.
1387  */
1388 /*ARGSUSED*/
1389 static int
1390 ifconf(u_long cmd, caddr_t data, struct thread *td)
1391 {
1392         struct ifconf *ifc = (struct ifconf *)data;
1393         struct ifnet *ifp;
1394         struct ifaddr *ifa;
1395         struct sockaddr *sa;
1396         struct ifreq ifr, *ifrp;
1397         int space = ifc->ifc_len, error = 0;
1398
1399         ifrp = ifc->ifc_req;
1400         TAILQ_FOREACH(ifp, &ifnet, if_link) {
1401                 int addrs;
1402
1403                 if (space <= sizeof (ifr))
1404                         break;
1405                 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1406                     >= sizeof(ifr.ifr_name)) {
1407                         error = ENAMETOOLONG;
1408                         break;
1409                 }
1410
1411                 addrs = 0;
1412                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1413                         if (space <= sizeof(ifr))
1414                                 break;
1415                         sa = ifa->ifa_addr;
1416                         if (td->td_proc->p_ucred->cr_prison &&
1417                             prison_if(td, sa))
1418                                 continue;
1419                         addrs++;
1420 #ifdef COMPAT_43
1421                         if (cmd == OSIOCGIFCONF) {
1422                                 struct osockaddr *osa =
1423                                          (struct osockaddr *)&ifr.ifr_addr;
1424                                 ifr.ifr_addr = *sa;
1425                                 osa->sa_family = sa->sa_family;
1426                                 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1427                                                 sizeof (ifr));
1428                                 ifrp++;
1429                         } else
1430 #endif
1431                         if (sa->sa_len <= sizeof(*sa)) {
1432                                 ifr.ifr_addr = *sa;
1433                                 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1434                                                 sizeof (ifr));
1435                                 ifrp++;
1436                         } else {
1437                                 if (space < sizeof (ifr) + sa->sa_len -
1438                                             sizeof(*sa))
1439                                         break;
1440                                 space -= sa->sa_len - sizeof(*sa);
1441                                 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1442                                                 sizeof (ifr.ifr_name));
1443                                 if (error == 0)
1444                                     error = copyout((caddr_t)sa,
1445                                       (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1446                                 ifrp = (struct ifreq *)
1447                                         (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1448                         }
1449                         if (error)
1450                                 break;
1451                         space -= sizeof (ifr);
1452                 }
1453                 if (error)
1454                         break;
1455                 if (!addrs) {
1456                         bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1457                         error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1458                             sizeof (ifr));
1459                         if (error)
1460                                 break;
1461                         space -= sizeof (ifr);
1462                         ifrp++;
1463                 }
1464         }
1465         ifc->ifc_len -= space;
1466         return (error);
1467 }
1468
1469 /*
1470  * Just like if_promisc(), but for all-multicast-reception mode.
1471  */
1472 int
1473 if_allmulti(ifp, onswitch)
1474         struct ifnet *ifp;
1475         int onswitch;
1476 {
1477         int error = 0;
1478         int s = splimp();
1479         struct ifreq ifr;
1480
1481         if (onswitch) {
1482                 if (ifp->if_amcount++ == 0) {
1483                         ifp->if_flags |= IFF_ALLMULTI;
1484                         ifr.ifr_flags = ifp->if_flags;
1485                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
1486                         error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1487                                               (struct ucred *)NULL);
1488                 }
1489         } else {
1490                 if (ifp->if_amcount > 1) {
1491                         ifp->if_amcount--;
1492                 } else {
1493                         ifp->if_amcount = 0;
1494                         ifp->if_flags &= ~IFF_ALLMULTI;
1495                         ifr.ifr_flags = ifp->if_flags;
1496                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
1497                         error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1498                                               (struct ucred *)NULL);
1499                 }
1500         }
1501         splx(s);
1502
1503         if (error == 0)
1504                 rt_ifmsg(ifp);
1505         return error;
1506 }
1507
1508 /*
1509  * Add a multicast listenership to the interface in question.
1510  * The link layer provides a routine which converts
1511  */
1512 int
1513 if_addmulti(ifp, sa, retifma)
1514         struct ifnet *ifp;      /* interface to manipulate */
1515         struct sockaddr *sa;    /* address to add */
1516         struct ifmultiaddr **retifma;
1517 {
1518         struct sockaddr *llsa, *dupsa;
1519         int error, s;
1520         struct ifmultiaddr *ifma;
1521
1522         /*
1523          * If the matching multicast address already exists
1524          * then don't add a new one, just add a reference
1525          */
1526         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1527                 if (equal(sa, ifma->ifma_addr)) {
1528                         ifma->ifma_refcount++;
1529                         if (retifma)
1530                                 *retifma = ifma;
1531                         return 0;
1532                 }
1533         }
1534
1535         /*
1536          * Give the link layer a chance to accept/reject it, and also
1537          * find out which AF_LINK address this maps to, if it isn't one
1538          * already.
1539          */
1540         if (ifp->if_resolvemulti) {
1541                 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1542                 if (error) return error;
1543         } else {
1544                 llsa = 0;
1545         }
1546
1547         MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1548         MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1549         bcopy(sa, dupsa, sa->sa_len);
1550
1551         ifma->ifma_addr = dupsa;
1552         ifma->ifma_lladdr = llsa;
1553         ifma->ifma_ifp = ifp;
1554         ifma->ifma_refcount = 1;
1555         ifma->ifma_protospec = 0;
1556         rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1557
1558         /*
1559          * Some network interfaces can scan the address list at
1560          * interrupt time; lock them out.
1561          */
1562         s = splimp();
1563         LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1564         splx(s);
1565         *retifma = ifma;
1566
1567         if (llsa != 0) {
1568                 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1569                         if (equal(ifma->ifma_addr, llsa))
1570                                 break;
1571                 }
1572                 if (ifma) {
1573                         ifma->ifma_refcount++;
1574                 } else {
1575                         MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1576                                M_IFMADDR, M_WAITOK);
1577                         MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1578                                M_IFMADDR, M_WAITOK);
1579                         bcopy(llsa, dupsa, llsa->sa_len);
1580                         ifma->ifma_addr = dupsa;
1581                         ifma->ifma_ifp = ifp;
1582                         ifma->ifma_refcount = 1;
1583                         s = splimp();
1584                         LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1585                         splx(s);
1586                 }
1587         }
1588         /*
1589          * We are certain we have added something, so call down to the
1590          * interface to let them know about it.
1591          */
1592         s = splimp();
1593         ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1594         splx(s);
1595
1596         return 0;
1597 }
1598
1599 /*
1600  * Remove a reference to a multicast address on this interface.  Yell
1601  * if the request does not match an existing membership.
1602  */
1603 int
1604 if_delmulti(ifp, sa)
1605         struct ifnet *ifp;
1606         struct sockaddr *sa;
1607 {
1608         struct ifmultiaddr *ifma;
1609         int s;
1610
1611         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1612                 if (equal(sa, ifma->ifma_addr))
1613                         break;
1614         if (ifma == 0)
1615                 return ENOENT;
1616
1617         if (ifma->ifma_refcount > 1) {
1618                 ifma->ifma_refcount--;
1619                 return 0;
1620         }
1621
1622         rt_newmaddrmsg(RTM_DELMADDR, ifma);
1623         sa = ifma->ifma_lladdr;
1624         s = splimp();
1625         LIST_REMOVE(ifma, ifma_link);
1626         /*
1627          * Make sure the interface driver is notified
1628          * in the case of a link layer mcast group being left.
1629          */
1630         if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1631                 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1632         splx(s);
1633         free(ifma->ifma_addr, M_IFMADDR);
1634         free(ifma, M_IFMADDR);
1635         if (sa == 0)
1636                 return 0;
1637
1638         /*
1639          * Now look for the link-layer address which corresponds to
1640          * this network address.  It had been squirreled away in
1641          * ifma->ifma_lladdr for this purpose (so we don't have
1642          * to call ifp->if_resolvemulti() again), and we saved that
1643          * value in sa above.  If some nasty deleted the
1644          * link-layer address out from underneath us, we can deal because
1645          * the address we stored was is not the same as the one which was
1646          * in the record for the link-layer address.  (So we don't complain
1647          * in that case.)
1648          */
1649         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1650                 if (equal(sa, ifma->ifma_addr))
1651                         break;
1652         if (ifma == 0)
1653                 return 0;
1654
1655         if (ifma->ifma_refcount > 1) {
1656                 ifma->ifma_refcount--;
1657                 return 0;
1658         }
1659
1660         s = splimp();
1661         LIST_REMOVE(ifma, ifma_link);
1662         ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1663         splx(s);
1664         free(ifma->ifma_addr, M_IFMADDR);
1665         free(sa, M_IFMADDR);
1666         free(ifma, M_IFMADDR);
1667
1668         return 0;
1669 }
1670
1671 /*
1672  * Set the link layer address on an interface.
1673  *
1674  * At this time we only support certain types of interfaces,
1675  * and we don't allow the length of the address to change.
1676  */
1677 int
1678 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1679 {
1680         struct sockaddr_dl *sdl;
1681         struct ifaddr *ifa;
1682         struct ifreq ifr;
1683
1684         ifa = ifnet_addrs[ifp->if_index - 1];
1685         if (ifa == NULL)
1686                 return (EINVAL);
1687         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1688         if (sdl == NULL)
1689                 return (EINVAL);
1690         if (len != sdl->sdl_alen)       /* don't allow length to change */
1691                 return (EINVAL);
1692         switch (ifp->if_type) {
1693         case IFT_ETHER:                 /* these types use struct arpcom */
1694         case IFT_FDDI:
1695         case IFT_XETHER:
1696         case IFT_ISO88025:
1697         case IFT_L2VLAN:
1698                 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1699                 /* FALLTHROUGH */
1700         case IFT_ARCNET:
1701                 bcopy(lladdr, LLADDR(sdl), len);
1702                 break;
1703         default:
1704                 return (ENODEV);
1705         }
1706         /*
1707          * If the interface is already up, we need
1708          * to re-init it in order to reprogram its
1709          * address filter.
1710          */
1711         if ((ifp->if_flags & IFF_UP) != 0) {
1712                 ifp->if_flags &= ~IFF_UP;
1713                 ifr.ifr_flags = ifp->if_flags;
1714                 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1715                 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1716                                  (struct ucred *)NULL);
1717                 ifp->if_flags |= IFF_UP;
1718                 ifr.ifr_flags = ifp->if_flags;
1719                 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1720                 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1721                                  (struct ucred *)NULL);
1722 #ifdef INET
1723                 /*
1724                  * Also send gratuitous ARPs to notify other nodes about
1725                  * the address change.
1726                  */
1727                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1728                         if (ifa->ifa_addr != NULL &&
1729                             ifa->ifa_addr->sa_family == AF_INET)
1730                                 arp_ifinit(ifp, ifa);
1731                 }
1732 #endif
1733         }
1734         return (0);
1735 }
1736
1737 struct ifmultiaddr *
1738 ifmaof_ifpforaddr(sa, ifp)
1739         struct sockaddr *sa;
1740         struct ifnet *ifp;
1741 {
1742         struct ifmultiaddr *ifma;
1743
1744         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1745                 if (equal(ifma->ifma_addr, sa))
1746                         break;
1747
1748         return ifma;
1749 }
1750
1751 /*
1752  * The name argument must be a pointer to storage which will last as
1753  * long as the interface does.  For physical devices, the result of
1754  * device_get_name(dev) is a good choice and for pseudo-devices a
1755  * static string works well.
1756  */
1757 void
1758 if_initname(struct ifnet *ifp, const char *name, int unit)
1759 {
1760         ifp->if_dname = name;
1761         ifp->if_dunit = unit;
1762         if (unit != IF_DUNIT_NONE)
1763                 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1764         else
1765                 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1766 }
1767
1768 int
1769 if_printf(struct ifnet *ifp, const char *fmt, ...)
1770 {
1771         __va_list ap;
1772         int retval;
1773
1774         retval = printf("%s: ", ifp->if_xname);
1775         __va_start(ap, fmt);
1776         retval += vprintf(fmt, ap);
1777         __va_end(ap);
1778         return (retval);
1779 }
1780
1781 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1782 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");