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