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