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