]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/net/if.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)if.c        8.5 (Berkeley) 1/9/95
30  * $FreeBSD$
31  */
32
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
35 #include "opt_inet.h"
36 #include "opt_carp.h"
37
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/conf.h>
41 #include <sys/malloc.h>
42 #include <sys/sbuf.h>
43 #include <sys/bus.h>
44 #include <sys/mbuf.h>
45 #include <sys/systm.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/kernel.h>
52 #include <sys/lock.h>
53 #include <sys/refcount.h>
54 #include <sys/module.h>
55 #include <sys/rwlock.h>
56 #include <sys/sockio.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 #include <sys/taskqueue.h>
60 #include <sys/domain.h>
61 #include <sys/jail.h>
62 #include <machine/stdarg.h>
63 #include <vm/uma.h>
64
65 #include <net/if.h>
66 #include <net/if_arp.h>
67 #include <net/if_clone.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/if_var.h>
71 #include <net/radix.h>
72 #include <net/route.h>
73 #include <net/vnet.h>
74
75 #if defined(INET) || defined(INET6)
76 /*XXX*/
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
79 #ifdef INET6
80 #include <netinet6/in6_var.h>
81 #include <netinet6/in6_ifattach.h>
82 #endif
83 #endif
84 #ifdef INET
85 #include <netinet/if_ether.h>
86 #endif
87 #if defined(INET) || defined(INET6)
88 #ifdef DEV_CARP
89 #include <netinet/ip_carp.h>
90 #endif
91 #endif
92
93 #include <security/mac/mac_framework.h>
94
95 struct ifindex_entry {
96         struct  ifnet *ife_ifnet;
97 };
98
99 static int slowtimo_started;
100
101 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
102 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
103
104 /* Log link state change events */
105 static int log_link_state_change = 1;
106
107 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
108         &log_link_state_change, 0,
109         "log interface link state change events");
110
111 void    (*bstp_linkstate_p)(struct ifnet *ifp, int state);
112 void    (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
113 void    (*lagg_linkstate_p)(struct ifnet *ifp, int state);
114
115 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
116
117 /*
118  * XXX: Style; these should be sorted alphabetically, and unprototyped
119  * static functions should be prototyped. Currently they are sorted by
120  * declaration order.
121  */
122 static void     if_attachdomain(void *);
123 static void     if_attachdomain1(struct ifnet *);
124 static int      ifconf(u_long, caddr_t);
125 static void     if_freemulti(struct ifmultiaddr *);
126 static void     if_init(void *);
127 static void     if_grow(void);
128 static void     if_check(void *);
129 static void     if_route(struct ifnet *, int flag, int fam);
130 static int      if_setflag(struct ifnet *, int, int, int *, int);
131 static void     if_slowtimo(void *);
132 static int      if_transmit(struct ifnet *ifp, struct mbuf *m);
133 static void     if_unroute(struct ifnet *, int flag, int fam);
134 static void     link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
135 static int      if_rtdel(struct radix_node *, void *);
136 static int      ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
137 static int      if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
138 static void     do_link_state_change(void *, int);
139 static int      if_getgroup(struct ifgroupreq *, struct ifnet *);
140 static int      if_getgroupmembers(struct ifgroupreq *);
141 static void     if_delgroups(struct ifnet *);
142 static void     if_attach_internal(struct ifnet *, int);
143 static void     if_detach_internal(struct ifnet *, int);
144
145 #ifdef INET6
146 /*
147  * XXX: declare here to avoid to include many inet6 related files..
148  * should be more generalized?
149  */
150 extern void     nd6_setmtu(struct ifnet *);
151 #endif
152
153 VNET_DEFINE(struct ifnethead, ifnet);   /* depend on static init XXX */
154 VNET_DEFINE(struct ifgrouphead, ifg_head);
155 VNET_DEFINE(int, if_index);
156 static VNET_DEFINE(int, if_indexlim) = 8;
157
158 /* Table of ifnet by index. */
159 static VNET_DEFINE(struct ifindex_entry *, ifindex_table);
160
161 #define V_if_indexlim           VNET(if_indexlim)
162 #define V_ifindex_table         VNET(ifindex_table)
163
164 int     ifqmaxlen = IFQ_MAXLEN;
165
166 /*
167  * The global network interface list (V_ifnet) and related state (such as
168  * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
169  * an rwlock.  Either may be acquired shared to stablize the list, but both
170  * must be acquired writable to modify the list.  This model allows us to
171  * both stablize the interface list during interrupt thread processing, but
172  * also to stablize it over long-running ioctls, without introducing priority
173  * inversions and deadlocks.
174  */
175 struct rwlock ifnet_rwlock;
176 struct sx ifnet_sxlock;
177
178 /*
179  * The allocation of network interfaces is a rather non-atomic affair; we
180  * need to select an index before we are ready to expose the interface for
181  * use, so will use this pointer value to indicate reservation.
182  */
183 #define IFNET_HOLD      (void *)(uintptr_t)(-1)
184
185 static  if_com_alloc_t *if_com_alloc[256];
186 static  if_com_free_t *if_com_free[256];
187
188 /*
189  * System initialization
190  */
191 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL);
192
193 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
194 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
195 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
196
197 struct ifnet *
198 ifnet_byindex_locked(u_short idx)
199 {
200
201         if (idx > V_if_index)
202                 return (NULL);
203         if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
204                 return (NULL);
205         return (V_ifindex_table[idx].ife_ifnet);
206 }
207
208 struct ifnet *
209 ifnet_byindex(u_short idx)
210 {
211         struct ifnet *ifp;
212
213         IFNET_RLOCK_NOSLEEP();
214         ifp = ifnet_byindex_locked(idx);
215         IFNET_RUNLOCK_NOSLEEP();
216         return (ifp);
217 }
218
219 struct ifnet *
220 ifnet_byindex_ref(u_short idx)
221 {
222         struct ifnet *ifp;
223
224         IFNET_RLOCK_NOSLEEP();
225         ifp = ifnet_byindex_locked(idx);
226         if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
227                 IFNET_RUNLOCK_NOSLEEP();
228                 return (NULL);
229         }
230         if_ref(ifp);
231         IFNET_RUNLOCK_NOSLEEP();
232         return (ifp);
233 }
234
235 /*
236  * Allocate an ifindex array entry; return 0 on success or an error on
237  * failure.
238  */
239 static int
240 ifindex_alloc_locked(u_short *idxp)
241 {
242         u_short idx;
243
244         IFNET_WLOCK_ASSERT();
245
246         /*
247          * Try to find an empty slot below V_if_index.  If we fail, take the
248          * next slot.
249          */
250         for (idx = 1; idx <= V_if_index; idx++) {
251                 if (V_ifindex_table[idx].ife_ifnet == NULL)
252                         break;
253         }
254
255         /* Catch if_index overflow. */
256         if (idx < 1)
257                 return (ENOSPC);
258         if (idx > V_if_index)
259                 V_if_index = idx;
260         if (V_if_index >= V_if_indexlim)
261                 if_grow();
262         *idxp = idx;
263         return (0);
264 }
265
266 static void
267 ifindex_free_locked(u_short idx)
268 {
269
270         IFNET_WLOCK_ASSERT();
271
272         V_ifindex_table[idx].ife_ifnet = NULL;
273         while (V_if_index > 0 &&
274             V_ifindex_table[V_if_index].ife_ifnet == NULL)
275                 V_if_index--;
276 }
277
278 static void
279 ifindex_free(u_short idx)
280 {
281
282         IFNET_WLOCK();
283         ifindex_free_locked(idx);
284         IFNET_WUNLOCK();
285 }
286
287 static void
288 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
289 {
290
291         IFNET_WLOCK_ASSERT();
292
293         V_ifindex_table[idx].ife_ifnet = ifp;
294 }
295
296 static void
297 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
298 {
299
300         IFNET_WLOCK();
301         ifnet_setbyindex_locked(idx, ifp);
302         IFNET_WUNLOCK();
303 }
304
305 struct ifaddr *
306 ifaddr_byindex(u_short idx)
307 {
308         struct ifaddr *ifa;
309
310         IFNET_RLOCK_NOSLEEP();
311         ifa = ifnet_byindex_locked(idx)->if_addr;
312         if (ifa != NULL)
313                 ifa_ref(ifa);
314         IFNET_RUNLOCK_NOSLEEP();
315         return (ifa);
316 }
317
318 /*
319  * Network interface utility routines.
320  *
321  * Routines with ifa_ifwith* names take sockaddr *'s as
322  * parameters.
323  */
324
325 static void
326 vnet_if_init(const void *unused __unused)
327 {
328
329         TAILQ_INIT(&V_ifnet);
330         TAILQ_INIT(&V_ifg_head);
331         if_grow();                              /* create initial table */
332         vnet_if_clone_init();
333 }
334 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_FIRST, vnet_if_init,
335     NULL);
336
337 /* ARGSUSED*/
338 static void
339 if_init(void *dummy __unused)
340 {
341
342         IFNET_LOCK_INIT();
343         if_clone_init();
344 }
345 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init, NULL);
346
347
348 #ifdef VIMAGE
349 static void
350 vnet_if_uninit(const void *unused __unused)
351 {
352
353         VNET_ASSERT(TAILQ_EMPTY(&V_ifnet));
354         VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head));
355
356         free((caddr_t)V_ifindex_table, M_IFNET);
357 }
358 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
359     vnet_if_uninit, NULL);
360 #endif
361
362 static void
363 if_grow(void)
364 {
365         u_int n;
366         struct ifindex_entry *e;
367
368         V_if_indexlim <<= 1;
369         n = V_if_indexlim * sizeof(*e);
370         e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
371         if (V_ifindex_table != NULL) {
372                 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
373                 free((caddr_t)V_ifindex_table, M_IFNET);
374         }
375         V_ifindex_table = e;
376 }
377
378 static void
379 if_check(void *dummy __unused)
380 {
381
382         /*
383          * If at least one interface added during boot uses
384          * if_watchdog then start the timer.
385          */
386         if (slowtimo_started)
387                 if_slowtimo(0);
388 }
389
390 /*
391  * Allocate a struct ifnet and an index for an interface.  A layer 2
392  * common structure will also be allocated if an allocation routine is
393  * registered for the passed type.
394  */
395 struct ifnet *
396 if_alloc(u_char type)
397 {
398         struct ifnet *ifp;
399         u_short idx;
400
401         ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
402         IFNET_WLOCK();
403         if (ifindex_alloc_locked(&idx) != 0) {
404                 IFNET_WUNLOCK();
405                 free(ifp, M_IFNET);
406                 return (NULL);
407         }
408         ifnet_setbyindex_locked(idx, IFNET_HOLD);
409         IFNET_WUNLOCK();
410         ifp->if_index = idx;
411         ifp->if_type = type;
412         ifp->if_alloctype = type;
413         if (if_com_alloc[type] != NULL) {
414                 ifp->if_l2com = if_com_alloc[type](type, ifp);
415                 if (ifp->if_l2com == NULL) {
416                         free(ifp, M_IFNET);
417                         ifindex_free(idx);
418                         return (NULL);
419                 }
420         }
421
422         IF_ADDR_LOCK_INIT(ifp);
423         TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
424         ifp->if_afdata_initialized = 0;
425         IF_AFDATA_LOCK_INIT(ifp);
426         TAILQ_INIT(&ifp->if_addrhead);
427         TAILQ_INIT(&ifp->if_prefixhead);
428         TAILQ_INIT(&ifp->if_multiaddrs);
429         TAILQ_INIT(&ifp->if_groups);
430 #ifdef MAC
431         mac_ifnet_init(ifp);
432 #endif
433         ifq_init(&ifp->if_snd, ifp);
434
435         refcount_init(&ifp->if_refcount, 1);    /* Index reference. */
436         ifnet_setbyindex(ifp->if_index, ifp);
437         return (ifp);
438 }
439
440 /*
441  * Do the actual work of freeing a struct ifnet, associated index, and layer
442  * 2 common structure.  This call is made when the last reference to an
443  * interface is released.
444  */
445 static void
446 if_free_internal(struct ifnet *ifp)
447 {
448
449         KASSERT((ifp->if_flags & IFF_DYING),
450             ("if_free_internal: interface not dying"));
451
452         IFNET_WLOCK();
453         KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
454             ("%s: freeing unallocated ifnet", ifp->if_xname));
455
456         ifindex_free_locked(ifp->if_index);
457         IFNET_WUNLOCK();
458
459         if (if_com_free[ifp->if_alloctype] != NULL)
460                 if_com_free[ifp->if_alloctype](ifp->if_l2com,
461                     ifp->if_alloctype);
462
463 #ifdef MAC
464         mac_ifnet_destroy(ifp);
465 #endif /* MAC */
466         IF_AFDATA_DESTROY(ifp);
467         IF_ADDR_LOCK_DESTROY(ifp);
468         ifq_delete(&ifp->if_snd);
469         free(ifp, M_IFNET);
470 }
471
472 /*
473  * This version should only be called by intefaces that switch their type
474  * after calling if_alloc().  if_free_type() will go away again now that we
475  * have if_alloctype to cache the original allocation type.  For now, assert
476  * that they match, since we require that in practice.
477  */
478 void
479 if_free_type(struct ifnet *ifp, u_char type)
480 {
481
482         KASSERT(ifp->if_alloctype == type,
483             ("if_free_type: type (%d) != alloctype (%d)", type,
484             ifp->if_alloctype));
485
486         ifp->if_flags |= IFF_DYING;                     /* XXX: Locking */
487         if (!refcount_release(&ifp->if_refcount))
488                 return;
489         if_free_internal(ifp);
490 }
491
492 /*
493  * This is the normal version of if_free(), used by device drivers to free a
494  * detached network interface.  The contents of if_free_type() will move into
495  * here when if_free_type() goes away.
496  */
497 void
498 if_free(struct ifnet *ifp)
499 {
500
501         if_free_type(ifp, ifp->if_alloctype);
502 }
503
504 /*
505  * Interfaces to keep an ifnet type-stable despite the possibility of the
506  * driver calling if_free().  If there are additional references, we defer
507  * freeing the underlying data structure.
508  */
509 void
510 if_ref(struct ifnet *ifp)
511 {
512
513         /* We don't assert the ifnet list lock here, but arguably should. */
514         refcount_acquire(&ifp->if_refcount);
515 }
516
517 void
518 if_rele(struct ifnet *ifp)
519 {
520
521         if (!refcount_release(&ifp->if_refcount))
522                 return;
523         if_free_internal(ifp);
524 }
525
526 void
527 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
528 {
529         
530         mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
531
532         if (ifq->ifq_maxlen == 0) 
533                 ifq->ifq_maxlen = ifqmaxlen;
534
535         ifq->altq_type = 0;
536         ifq->altq_disc = NULL;
537         ifq->altq_flags &= ALTQF_CANTCHANGE;
538         ifq->altq_tbr  = NULL;
539         ifq->altq_ifp  = ifp;
540 }
541
542 void
543 ifq_delete(struct ifaltq *ifq)
544 {
545         mtx_destroy(&ifq->ifq_mtx);
546 }
547
548 /*
549  * Perform generic interface initalization tasks and attach the interface
550  * to the list of "active" interfaces.  If vmove flag is set on entry
551  * to if_attach_internal(), perform only a limited subset of initialization
552  * tasks, given that we are moving from one vnet to another an ifnet which
553  * has already been fully initialized.
554  *
555  * XXX:
556  *  - The decision to return void and thus require this function to
557  *    succeed is questionable.
558  *  - We should probably do more sanity checking.  For instance we don't
559  *    do anything to insure if_xname is unique or non-empty.
560  */
561 void
562 if_attach(struct ifnet *ifp)
563 {
564
565         if_attach_internal(ifp, 0);
566 }
567
568 static void
569 if_attach_internal(struct ifnet *ifp, int vmove)
570 {
571         unsigned socksize, ifasize;
572         int namelen, masklen;
573         struct sockaddr_dl *sdl;
574         struct ifaddr *ifa;
575
576         if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
577                 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
578                     ifp->if_xname);
579
580 #ifdef VIMAGE
581         ifp->if_vnet = curvnet;
582         if (ifp->if_home_vnet == NULL)
583                 ifp->if_home_vnet = curvnet;
584 #endif
585
586         if_addgroup(ifp, IFG_ALL);
587
588         getmicrotime(&ifp->if_lastchange);
589         ifp->if_data.ifi_epoch = time_uptime;
590         ifp->if_data.ifi_datalen = sizeof(struct if_data);
591
592         KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
593             (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
594             ("transmit and qflush must both either be set or both be NULL"));
595         if (ifp->if_transmit == NULL) {
596                 ifp->if_transmit = if_transmit;
597                 ifp->if_qflush = if_qflush;
598         }
599         
600         if (!vmove) {
601 #ifdef MAC
602                 mac_ifnet_create(ifp);
603 #endif
604
605                 /*
606                  * Create a Link Level name for this device.
607                  */
608                 namelen = strlen(ifp->if_xname);
609                 /*
610                  * Always save enough space for any possiable name so we
611                  * can do a rename in place later.
612                  */
613                 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
614                 socksize = masklen + ifp->if_addrlen;
615                 if (socksize < sizeof(*sdl))
616                         socksize = sizeof(*sdl);
617                 socksize = roundup2(socksize, sizeof(long));
618                 ifasize = sizeof(*ifa) + 2 * socksize;
619                 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
620                 ifa_init(ifa);
621                 sdl = (struct sockaddr_dl *)(ifa + 1);
622                 sdl->sdl_len = socksize;
623                 sdl->sdl_family = AF_LINK;
624                 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
625                 sdl->sdl_nlen = namelen;
626                 sdl->sdl_index = ifp->if_index;
627                 sdl->sdl_type = ifp->if_type;
628                 ifp->if_addr = ifa;
629                 ifa->ifa_ifp = ifp;
630                 ifa->ifa_rtrequest = link_rtrequest;
631                 ifa->ifa_addr = (struct sockaddr *)sdl;
632                 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
633                 ifa->ifa_netmask = (struct sockaddr *)sdl;
634                 sdl->sdl_len = masklen;
635                 while (namelen != 0)
636                         sdl->sdl_data[--namelen] = 0xff;
637                 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
638                 /* Reliably crash if used uninitialized. */
639                 ifp->if_broadcastaddr = NULL;
640         }
641 #ifdef VIMAGE
642         else {
643                 /*
644                  * Update the interface index in the link layer address
645                  * of the interface.
646                  */
647                 for (ifa = ifp->if_addr; ifa != NULL;
648                     ifa = TAILQ_NEXT(ifa, ifa_link)) {
649                         if (ifa->ifa_addr->sa_family == AF_LINK) {
650                                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
651                                 sdl->sdl_index = ifp->if_index;
652                         }
653                 }
654         }
655 #endif
656
657         IFNET_WLOCK();
658         TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
659 #ifdef VIMAGE
660         curvnet->vnet_ifcnt++;
661 #endif
662         IFNET_WUNLOCK();
663
664         if (domain_init_status >= 2)
665                 if_attachdomain1(ifp);
666
667         EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
668         if (IS_DEFAULT_VNET(curvnet))
669                 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
670
671         /* Announce the interface. */
672         rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
673
674         if (!vmove && ifp->if_watchdog != NULL) {
675                 if_printf(ifp,
676                     "WARNING: using obsoleted if_watchdog interface\n");
677
678                 /*
679                  * Note that we need if_slowtimo().  If this happens after
680                  * boot, then call if_slowtimo() directly.
681                  */
682                 if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold)
683                         if_slowtimo(0);
684         }
685 }
686
687 static void
688 if_attachdomain(void *dummy)
689 {
690         struct ifnet *ifp;
691         int s;
692
693         s = splnet();
694         TAILQ_FOREACH(ifp, &V_ifnet, if_link)
695                 if_attachdomain1(ifp);
696         splx(s);
697 }
698 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
699     if_attachdomain, NULL);
700
701 static void
702 if_attachdomain1(struct ifnet *ifp)
703 {
704         struct domain *dp;
705         int s;
706
707         s = splnet();
708
709         /*
710          * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
711          * cannot lock ifp->if_afdata initialization, entirely.
712          */
713         if (IF_AFDATA_TRYLOCK(ifp) == 0) {
714                 splx(s);
715                 return;
716         }
717         if (ifp->if_afdata_initialized >= domain_init_status) {
718                 IF_AFDATA_UNLOCK(ifp);
719                 splx(s);
720                 printf("if_attachdomain called more than once on %s\n",
721                     ifp->if_xname);
722                 return;
723         }
724         ifp->if_afdata_initialized = domain_init_status;
725         IF_AFDATA_UNLOCK(ifp);
726
727         /* address family dependent data region */
728         bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
729         for (dp = domains; dp; dp = dp->dom_next) {
730                 if (dp->dom_ifattach)
731                         ifp->if_afdata[dp->dom_family] =
732                             (*dp->dom_ifattach)(ifp);
733         }
734
735         splx(s);
736 }
737
738 /*
739  * Remove any unicast or broadcast network addresses from an interface.
740  */
741 void
742 if_purgeaddrs(struct ifnet *ifp)
743 {
744         struct ifaddr *ifa, *next;
745
746         TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
747                 if (ifa->ifa_addr->sa_family == AF_LINK)
748                         continue;
749 #ifdef INET
750                 /* XXX: Ugly!! ad hoc just for INET */
751                 if (ifa->ifa_addr->sa_family == AF_INET) {
752                         struct ifaliasreq ifr;
753
754                         bzero(&ifr, sizeof(ifr));
755                         ifr.ifra_addr = *ifa->ifa_addr;
756                         if (ifa->ifa_dstaddr)
757                                 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
758                         if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
759                             NULL) == 0)
760                                 continue;
761                 }
762 #endif /* INET */
763 #ifdef INET6
764                 if (ifa->ifa_addr->sa_family == AF_INET6) {
765                         in6_purgeaddr(ifa);
766                         /* ifp_addrhead is already updated */
767                         continue;
768                 }
769 #endif /* INET6 */
770                 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
771                 ifa_free(ifa);
772         }
773 }
774
775 /*
776  * Remove any multicast network addresses from an interface.
777  */
778 void
779 if_purgemaddrs(struct ifnet *ifp)
780 {
781         struct ifmultiaddr *ifma;
782         struct ifmultiaddr *next;
783
784         IF_ADDR_LOCK(ifp);
785         TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
786                 if_delmulti_locked(ifp, ifma, 1);
787         IF_ADDR_UNLOCK(ifp);
788 }
789
790 /*
791  * Detach an interface, removing it from the list of "active" interfaces.
792  * If vmove flag is set on entry to if_detach_internal(), perform only a
793  * limited subset of cleanup tasks, given that we are moving an ifnet from
794  * one vnet to another, where it must be fully operational.
795  *
796  * XXXRW: There are some significant questions about event ordering, and
797  * how to prevent things from starting to use the interface during detach.
798  */
799 void
800 if_detach(struct ifnet *ifp)
801 {
802
803         if_detach_internal(ifp, 0);
804 }
805
806 static void
807 if_detach_internal(struct ifnet *ifp, int vmove)
808 {
809         struct ifaddr *ifa;
810         struct radix_node_head  *rnh;
811         int i, j;
812         struct domain *dp;
813         struct ifnet *iter;
814         int found = 0;
815
816         IFNET_WLOCK();
817         TAILQ_FOREACH(iter, &V_ifnet, if_link)
818                 if (iter == ifp) {
819                         TAILQ_REMOVE(&V_ifnet, ifp, if_link);
820                         found = 1;
821                         break;
822                 }
823 #ifdef VIMAGE
824         if (found)
825                 curvnet->vnet_ifcnt--;
826 #endif
827         IFNET_WUNLOCK();
828         if (!found) {
829                 if (vmove)
830                         panic("interface not in it's own ifnet list");
831                 else
832                         return; /* XXX this should panic as well? */
833         }
834
835         /*
836          * Remove/wait for pending events.
837          */
838         taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
839
840         /*
841          * Remove routes and flush queues.
842          */
843         if_down(ifp);
844 #ifdef ALTQ
845         if (ALTQ_IS_ENABLED(&ifp->if_snd))
846                 altq_disable(&ifp->if_snd);
847         if (ALTQ_IS_ATTACHED(&ifp->if_snd))
848                 altq_detach(&ifp->if_snd);
849 #endif
850
851         if_purgeaddrs(ifp);
852
853 #ifdef INET
854         in_ifdetach(ifp);
855 #endif
856
857 #ifdef INET6
858         /*
859          * Remove all IPv6 kernel structs related to ifp.  This should be done
860          * before removing routing entries below, since IPv6 interface direct
861          * routes are expected to be removed by the IPv6-specific kernel API.
862          * Otherwise, the kernel will detect some inconsistency and bark it.
863          */
864         in6_ifdetach(ifp);
865 #endif
866         if_purgemaddrs(ifp);
867
868         if (!vmove) {
869                 /*
870                  * Prevent further calls into the device driver via ifnet.
871                  */
872                 if_dead(ifp);
873
874                 /*
875                  * Remove link ifaddr pointer and maybe decrement if_index.
876                  * Clean up all addresses.
877                  */
878                 ifp->if_addr = NULL;
879
880                 /* We can now free link ifaddr. */
881                 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
882                         ifa = TAILQ_FIRST(&ifp->if_addrhead);
883                         TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
884                         ifa_free(ifa);
885                 }
886         }
887
888         /*
889          * Delete all remaining routes using this interface
890          * Unfortuneatly the only way to do this is to slog through
891          * the entire routing table looking for routes which point
892          * to this interface...oh well...
893          */
894         for (i = 1; i <= AF_MAX; i++) {
895                 for (j = 0; j < rt_numfibs; j++) {
896                         rnh = rt_tables_get_rnh(j, i);
897                         if (rnh == NULL)
898                                 continue;
899                         RADIX_NODE_HEAD_LOCK(rnh);
900                         (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
901                         RADIX_NODE_HEAD_UNLOCK(rnh);
902                 }
903         }
904
905         /* Announce that the interface is gone. */
906         rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
907         EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
908         if (IS_DEFAULT_VNET(curvnet))
909                 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
910         if_delgroups(ifp);
911
912         IF_AFDATA_LOCK(ifp);
913         for (dp = domains; dp; dp = dp->dom_next) {
914                 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
915                         (*dp->dom_ifdetach)(ifp,
916                             ifp->if_afdata[dp->dom_family]);
917         }
918         ifp->if_afdata_initialized = 0;
919         IF_AFDATA_UNLOCK(ifp);
920 }
921
922 #ifdef VIMAGE
923 /*
924  * if_vmove() performs a limited version of if_detach() in current
925  * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
926  * An attempt is made to shrink if_index in current vnet, find an
927  * unused if_index in target vnet and calls if_grow() if necessary,
928  * and finally find an unused if_xname for the target vnet.
929  */
930 void
931 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
932 {
933         u_short idx;
934
935         /*
936          * Detach from current vnet, but preserve LLADDR info, do not
937          * mark as dead etc. so that the ifnet can be reattached later.
938          */
939         if_detach_internal(ifp, 1);
940
941         /*
942          * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
943          * the if_index for that vnet if possible.
944          *
945          * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
946          * or we'd lock on one vnet and unlock on another.
947          */
948         IFNET_WLOCK();
949         ifindex_free_locked(ifp->if_index);
950
951         /*
952          * Switch to the context of the target vnet.
953          */
954         CURVNET_SET_QUIET(new_vnet);
955
956         if (ifindex_alloc_locked(&idx) != 0) {
957                 IFNET_WUNLOCK();
958                 panic("if_index overflow");
959         }
960         ifp->if_index = idx;
961         ifnet_setbyindex_locked(ifp->if_index, ifp);
962         IFNET_WUNLOCK();
963
964         if_attach_internal(ifp, 1);
965
966         CURVNET_RESTORE();
967 }
968
969 /*
970  * Move an ifnet to or from another child prison/vnet, specified by the jail id.
971  */
972 static int
973 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
974 {
975         struct prison *pr;
976         struct ifnet *difp;
977
978         /* Try to find the prison within our visibility. */
979         sx_slock(&allprison_lock);
980         pr = prison_find_child(td->td_ucred->cr_prison, jid);
981         sx_sunlock(&allprison_lock);
982         if (pr == NULL)
983                 return (ENXIO);
984         prison_hold_locked(pr);
985         mtx_unlock(&pr->pr_mtx);
986
987         /* Do not try to move the iface from and to the same prison. */
988         if (pr->pr_vnet == ifp->if_vnet) {
989                 prison_free(pr);
990                 return (EEXIST);
991         }
992
993         /* Make sure the named iface does not exists in the dst. prison/vnet. */
994         /* XXX Lock interfaces to avoid races. */
995         CURVNET_SET_QUIET(pr->pr_vnet);
996         difp = ifunit(ifname);
997         CURVNET_RESTORE();
998         if (difp != NULL) {
999                 prison_free(pr);
1000                 return (EEXIST);
1001         }
1002
1003         /* Move the interface into the child jail/vnet. */
1004         if_vmove(ifp, pr->pr_vnet);
1005
1006         /* Report the new if_xname back to the userland. */
1007         sprintf(ifname, "%s", ifp->if_xname);
1008
1009         prison_free(pr);
1010         return (0);
1011 }
1012
1013 static int
1014 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1015 {
1016         struct prison *pr;
1017         struct vnet *vnet_dst;
1018         struct ifnet *ifp;
1019
1020         /* Try to find the prison within our visibility. */
1021         sx_slock(&allprison_lock);
1022         pr = prison_find_child(td->td_ucred->cr_prison, jid);
1023         sx_sunlock(&allprison_lock);
1024         if (pr == NULL)
1025                 return (ENXIO);
1026         prison_hold_locked(pr);
1027         mtx_unlock(&pr->pr_mtx);
1028
1029         /* Make sure the named iface exists in the source prison/vnet. */
1030         CURVNET_SET(pr->pr_vnet);
1031         ifp = ifunit(ifname);           /* XXX Lock to avoid races. */
1032         if (ifp == NULL) {
1033                 CURVNET_RESTORE();
1034                 prison_free(pr);
1035                 return (ENXIO);
1036         }
1037
1038         /* Do not try to move the iface from and to the same prison. */
1039         vnet_dst = TD_TO_VNET(td);
1040         if (vnet_dst == ifp->if_vnet) {
1041                 CURVNET_RESTORE();
1042                 prison_free(pr);
1043                 return (EEXIST);
1044         }
1045
1046         /* Get interface back from child jail/vnet. */
1047         if_vmove(ifp, vnet_dst);
1048         CURVNET_RESTORE();
1049
1050         /* Report the new if_xname back to the userland. */
1051         sprintf(ifname, "%s", ifp->if_xname);
1052
1053         prison_free(pr);
1054         return (0);
1055 }
1056 #endif /* VIMAGE */
1057
1058 /*
1059  * Add a group to an interface
1060  */
1061 int
1062 if_addgroup(struct ifnet *ifp, const char *groupname)
1063 {
1064         struct ifg_list         *ifgl;
1065         struct ifg_group        *ifg = NULL;
1066         struct ifg_member       *ifgm;
1067
1068         if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1069             groupname[strlen(groupname) - 1] <= '9')
1070                 return (EINVAL);
1071
1072         IFNET_WLOCK();
1073         TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1074                 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1075                         IFNET_WUNLOCK();
1076                         return (EEXIST);
1077                 }
1078
1079         if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1080             M_NOWAIT)) == NULL) {
1081                 IFNET_WUNLOCK();
1082                 return (ENOMEM);
1083         }
1084
1085         if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1086             M_TEMP, M_NOWAIT)) == NULL) {
1087                 free(ifgl, M_TEMP);
1088                 IFNET_WUNLOCK();
1089                 return (ENOMEM);
1090         }
1091
1092         TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1093                 if (!strcmp(ifg->ifg_group, groupname))
1094                         break;
1095
1096         if (ifg == NULL) {
1097                 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1098                     M_TEMP, M_NOWAIT)) == NULL) {
1099                         free(ifgl, M_TEMP);
1100                         free(ifgm, M_TEMP);
1101                         IFNET_WUNLOCK();
1102                         return (ENOMEM);
1103                 }
1104                 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1105                 ifg->ifg_refcnt = 0;
1106                 TAILQ_INIT(&ifg->ifg_members);
1107                 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1108                 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1109         }
1110
1111         ifg->ifg_refcnt++;
1112         ifgl->ifgl_group = ifg;
1113         ifgm->ifgm_ifp = ifp;
1114
1115         IF_ADDR_LOCK(ifp);
1116         TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1117         TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1118         IF_ADDR_UNLOCK(ifp);
1119
1120         IFNET_WUNLOCK();
1121
1122         EVENTHANDLER_INVOKE(group_change_event, groupname);
1123
1124         return (0);
1125 }
1126
1127 /*
1128  * Remove a group from an interface
1129  */
1130 int
1131 if_delgroup(struct ifnet *ifp, const char *groupname)
1132 {
1133         struct ifg_list         *ifgl;
1134         struct ifg_member       *ifgm;
1135
1136         IFNET_WLOCK();
1137         TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1138                 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1139                         break;
1140         if (ifgl == NULL) {
1141                 IFNET_WUNLOCK();
1142                 return (ENOENT);
1143         }
1144
1145         IF_ADDR_LOCK(ifp);
1146         TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1147         IF_ADDR_UNLOCK(ifp);
1148
1149         TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1150                 if (ifgm->ifgm_ifp == ifp)
1151                         break;
1152
1153         if (ifgm != NULL) {
1154                 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1155                 free(ifgm, M_TEMP);
1156         }
1157
1158         if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1159                 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1160                 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1161                 free(ifgl->ifgl_group, M_TEMP);
1162         }
1163         IFNET_WUNLOCK();
1164
1165         free(ifgl, M_TEMP);
1166
1167         EVENTHANDLER_INVOKE(group_change_event, groupname);
1168
1169         return (0);
1170 }
1171
1172 /*
1173  * Remove an interface from all groups
1174  */
1175 static void
1176 if_delgroups(struct ifnet *ifp)
1177 {
1178         struct ifg_list         *ifgl;
1179         struct ifg_member       *ifgm;
1180         char groupname[IFNAMSIZ];
1181
1182         IFNET_WLOCK();
1183         while (!TAILQ_EMPTY(&ifp->if_groups)) {
1184                 ifgl = TAILQ_FIRST(&ifp->if_groups);
1185
1186                 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1187
1188                 IF_ADDR_LOCK(ifp);
1189                 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1190                 IF_ADDR_UNLOCK(ifp);
1191
1192                 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1193                         if (ifgm->ifgm_ifp == ifp)
1194                                 break;
1195
1196                 if (ifgm != NULL) {
1197                         TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1198                             ifgm_next);
1199                         free(ifgm, M_TEMP);
1200                 }
1201
1202                 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1203                         TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1204                         EVENTHANDLER_INVOKE(group_detach_event,
1205                             ifgl->ifgl_group);
1206                         free(ifgl->ifgl_group, M_TEMP);
1207                 }
1208                 IFNET_WUNLOCK();
1209
1210                 free(ifgl, M_TEMP);
1211
1212                 EVENTHANDLER_INVOKE(group_change_event, groupname);
1213
1214                 IFNET_WLOCK();
1215         }
1216         IFNET_WUNLOCK();
1217 }
1218
1219 /*
1220  * Stores all groups from an interface in memory pointed
1221  * to by data
1222  */
1223 static int
1224 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1225 {
1226         int                      len, error;
1227         struct ifg_list         *ifgl;
1228         struct ifg_req           ifgrq, *ifgp;
1229         struct ifgroupreq       *ifgr = data;
1230
1231         if (ifgr->ifgr_len == 0) {
1232                 IF_ADDR_LOCK(ifp);
1233                 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1234                         ifgr->ifgr_len += sizeof(struct ifg_req);
1235                 IF_ADDR_UNLOCK(ifp);
1236                 return (0);
1237         }
1238
1239         len = ifgr->ifgr_len;
1240         ifgp = ifgr->ifgr_groups;
1241         /* XXX: wire */
1242         IF_ADDR_LOCK(ifp);
1243         TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1244                 if (len < sizeof(ifgrq)) {
1245                         IF_ADDR_UNLOCK(ifp);
1246                         return (EINVAL);
1247                 }
1248                 bzero(&ifgrq, sizeof ifgrq);
1249                 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1250                     sizeof(ifgrq.ifgrq_group));
1251                 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1252                         IF_ADDR_UNLOCK(ifp);
1253                         return (error);
1254                 }
1255                 len -= sizeof(ifgrq);
1256                 ifgp++;
1257         }
1258         IF_ADDR_UNLOCK(ifp);
1259
1260         return (0);
1261 }
1262
1263 /*
1264  * Stores all members of a group in memory pointed to by data
1265  */
1266 static int
1267 if_getgroupmembers(struct ifgroupreq *data)
1268 {
1269         struct ifgroupreq       *ifgr = data;
1270         struct ifg_group        *ifg;
1271         struct ifg_member       *ifgm;
1272         struct ifg_req           ifgrq, *ifgp;
1273         int                      len, error;
1274
1275         IFNET_RLOCK();
1276         TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1277                 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1278                         break;
1279         if (ifg == NULL) {
1280                 IFNET_RUNLOCK();
1281                 return (ENOENT);
1282         }
1283
1284         if (ifgr->ifgr_len == 0) {
1285                 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1286                         ifgr->ifgr_len += sizeof(ifgrq);
1287                 IFNET_RUNLOCK();
1288                 return (0);
1289         }
1290
1291         len = ifgr->ifgr_len;
1292         ifgp = ifgr->ifgr_groups;
1293         TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1294                 if (len < sizeof(ifgrq)) {
1295                         IFNET_RUNLOCK();
1296                         return (EINVAL);
1297                 }
1298                 bzero(&ifgrq, sizeof ifgrq);
1299                 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1300                     sizeof(ifgrq.ifgrq_member));
1301                 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1302                         IFNET_RUNLOCK();
1303                         return (error);
1304                 }
1305                 len -= sizeof(ifgrq);
1306                 ifgp++;
1307         }
1308         IFNET_RUNLOCK();
1309
1310         return (0);
1311 }
1312
1313 /*
1314  * Delete Routes for a Network Interface
1315  *
1316  * Called for each routing entry via the rnh->rnh_walktree() call above
1317  * to delete all route entries referencing a detaching network interface.
1318  *
1319  * Arguments:
1320  *      rn      pointer to node in the routing table
1321  *      arg     argument passed to rnh->rnh_walktree() - detaching interface
1322  *
1323  * Returns:
1324  *      0       successful
1325  *      errno   failed - reason indicated
1326  *
1327  */
1328 static int
1329 if_rtdel(struct radix_node *rn, void *arg)
1330 {
1331         struct rtentry  *rt = (struct rtentry *)rn;
1332         struct ifnet    *ifp = arg;
1333         int             err;
1334
1335         if (rt->rt_ifp == ifp) {
1336
1337                 /*
1338                  * Protect (sorta) against walktree recursion problems
1339                  * with cloned routes
1340                  */
1341                 if ((rt->rt_flags & RTF_UP) == 0)
1342                         return (0);
1343
1344                 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1345                                 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1346                                 (struct rtentry **) NULL, rt->rt_fibnum);
1347                 if (err) {
1348                         log(LOG_WARNING, "if_rtdel: error %d\n", err);
1349                 }
1350         }
1351
1352         return (0);
1353 }
1354
1355 /*
1356  * Wrapper functions for struct ifnet address list locking macros.  These are
1357  * used by kernel modules to avoid encoding programming interface or binary
1358  * interface assumptions that may be violated when kernel-internal locking
1359  * approaches change.
1360  */
1361 void
1362 if_addr_rlock(struct ifnet *ifp)
1363 {
1364
1365         IF_ADDR_LOCK(ifp);
1366 }
1367
1368 void
1369 if_addr_runlock(struct ifnet *ifp)
1370 {
1371
1372         IF_ADDR_UNLOCK(ifp);
1373 }
1374
1375 void
1376 if_maddr_rlock(struct ifnet *ifp)
1377 {
1378
1379         IF_ADDR_LOCK(ifp);
1380 }
1381
1382 void
1383 if_maddr_runlock(struct ifnet *ifp)
1384 {
1385
1386         IF_ADDR_UNLOCK(ifp);
1387 }
1388
1389 /*
1390  * Reference count functions for ifaddrs.
1391  */
1392 void
1393 ifa_init(struct ifaddr *ifa)
1394 {
1395
1396         mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1397         refcount_init(&ifa->ifa_refcnt, 1);
1398 }
1399
1400 void
1401 ifa_ref(struct ifaddr *ifa)
1402 {
1403
1404         refcount_acquire(&ifa->ifa_refcnt);
1405 }
1406
1407 void
1408 ifa_free(struct ifaddr *ifa)
1409 {
1410
1411         if (refcount_release(&ifa->ifa_refcnt)) {
1412                 mtx_destroy(&ifa->ifa_mtx);
1413                 free(ifa, M_IFADDR);
1414         }
1415 }
1416
1417 int
1418 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1419 {
1420         int error = 0;
1421         struct rtentry *rt = NULL;
1422         struct rt_addrinfo info;
1423         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1424
1425         bzero(&info, sizeof(info));
1426         info.rti_ifp = V_loif;
1427         info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1428         info.rti_info[RTAX_DST] = ia;
1429         info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1430         error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1431
1432         if (error == 0 && rt != NULL) {
1433                 RT_LOCK(rt);
1434                 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type  =
1435                         ifa->ifa_ifp->if_type;
1436                 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1437                         ifa->ifa_ifp->if_index;
1438                 RT_REMREF(rt);
1439                 RT_UNLOCK(rt);
1440         } else if (error != 0)
1441                 log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n");
1442
1443         return (error);
1444 }
1445
1446 int
1447 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1448 {
1449         int error = 0;
1450         struct rt_addrinfo info;
1451         struct sockaddr_dl null_sdl;
1452
1453         bzero(&null_sdl, sizeof(null_sdl));
1454         null_sdl.sdl_len = sizeof(null_sdl);
1455         null_sdl.sdl_family = AF_LINK;
1456         null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1457         null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1458         bzero(&info, sizeof(info));
1459         info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1460         info.rti_info[RTAX_DST] = ia;
1461         info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1462         error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1463
1464         if (error != 0)
1465                 log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n");
1466
1467         return (error);
1468 }
1469
1470 /*
1471  * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1472  * structs used to represent other address families, it is necessary
1473  * to perform a different comparison.
1474  */
1475
1476 #define sa_equal(a1, a2)        \
1477         (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1478
1479 #define sa_dl_equal(a1, a2)     \
1480         ((((struct sockaddr_dl *)(a1))->sdl_len ==                      \
1481          ((struct sockaddr_dl *)(a2))->sdl_len) &&                      \
1482          (bcmp(LLADDR((struct sockaddr_dl *)(a1)),                      \
1483                LLADDR((struct sockaddr_dl *)(a2)),                      \
1484                ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1485
1486 /*
1487  * Locate an interface based on a complete address.
1488  */
1489 /*ARGSUSED*/
1490 static struct ifaddr *
1491 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1492 {
1493         struct ifnet *ifp;
1494         struct ifaddr *ifa;
1495
1496         IFNET_RLOCK_NOSLEEP();
1497         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1498                 IF_ADDR_LOCK(ifp);
1499                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1500                         if (ifa->ifa_addr->sa_family != addr->sa_family)
1501                                 continue;
1502                         if (sa_equal(addr, ifa->ifa_addr)) {
1503                                 if (getref)
1504                                         ifa_ref(ifa);
1505                                 IF_ADDR_UNLOCK(ifp);
1506                                 goto done;
1507                         }
1508                         /* IP6 doesn't have broadcast */
1509                         if ((ifp->if_flags & IFF_BROADCAST) &&
1510                             ifa->ifa_broadaddr &&
1511                             ifa->ifa_broadaddr->sa_len != 0 &&
1512                             sa_equal(ifa->ifa_broadaddr, addr)) {
1513                                 if (getref)
1514                                         ifa_ref(ifa);
1515                                 IF_ADDR_UNLOCK(ifp);
1516                                 goto done;
1517                         }
1518                 }
1519                 IF_ADDR_UNLOCK(ifp);
1520         }
1521         ifa = NULL;
1522 done:
1523         IFNET_RUNLOCK_NOSLEEP();
1524         return (ifa);
1525 }
1526
1527 struct ifaddr *
1528 ifa_ifwithaddr(struct sockaddr *addr)
1529 {
1530
1531         return (ifa_ifwithaddr_internal(addr, 1));
1532 }
1533
1534 int
1535 ifa_ifwithaddr_check(struct sockaddr *addr)
1536 {
1537
1538         return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1539 }
1540
1541 /*
1542  * Locate an interface based on the broadcast address.
1543  */
1544 /* ARGSUSED */
1545 struct ifaddr *
1546 ifa_ifwithbroadaddr(struct sockaddr *addr)
1547 {
1548         struct ifnet *ifp;
1549         struct ifaddr *ifa;
1550
1551         IFNET_RLOCK_NOSLEEP();
1552         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1553                 IF_ADDR_LOCK(ifp);
1554                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1555                         if (ifa->ifa_addr->sa_family != addr->sa_family)
1556                                 continue;
1557                         if ((ifp->if_flags & IFF_BROADCAST) &&
1558                             ifa->ifa_broadaddr &&
1559                             ifa->ifa_broadaddr->sa_len != 0 &&
1560                             sa_equal(ifa->ifa_broadaddr, addr)) {
1561                                 ifa_ref(ifa);
1562                                 IF_ADDR_UNLOCK(ifp);
1563                                 goto done;
1564                         }
1565                 }
1566                 IF_ADDR_UNLOCK(ifp);
1567         }
1568         ifa = NULL;
1569 done:
1570         IFNET_RUNLOCK_NOSLEEP();
1571         return (ifa);
1572 }
1573
1574 /*
1575  * Locate the point to point interface with a given destination address.
1576  */
1577 /*ARGSUSED*/
1578 struct ifaddr *
1579 ifa_ifwithdstaddr(struct sockaddr *addr)
1580 {
1581         struct ifnet *ifp;
1582         struct ifaddr *ifa;
1583
1584         IFNET_RLOCK_NOSLEEP();
1585         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1586                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1587                         continue;
1588                 IF_ADDR_LOCK(ifp);
1589                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1590                         if (ifa->ifa_addr->sa_family != addr->sa_family)
1591                                 continue;
1592                         if (ifa->ifa_dstaddr != NULL &&
1593                             sa_equal(addr, ifa->ifa_dstaddr)) {
1594                                 ifa_ref(ifa);
1595                                 IF_ADDR_UNLOCK(ifp);
1596                                 goto done;
1597                         }
1598                 }
1599                 IF_ADDR_UNLOCK(ifp);
1600         }
1601         ifa = NULL;
1602 done:
1603         IFNET_RUNLOCK_NOSLEEP();
1604         return (ifa);
1605 }
1606
1607 /*
1608  * Find an interface on a specific network.  If many, choice
1609  * is most specific found.
1610  */
1611 struct ifaddr *
1612 ifa_ifwithnet(struct sockaddr *addr)
1613 {
1614         struct ifnet *ifp;
1615         struct ifaddr *ifa;
1616         struct ifaddr *ifa_maybe = NULL;
1617         u_int af = addr->sa_family;
1618         char *addr_data = addr->sa_data, *cplim;
1619
1620         /*
1621          * AF_LINK addresses can be looked up directly by their index number,
1622          * so do that if we can.
1623          */
1624         if (af == AF_LINK) {
1625             struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1626             if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1627                 return (ifaddr_byindex(sdl->sdl_index));
1628         }
1629
1630         /*
1631          * Scan though each interface, looking for ones that have addresses
1632          * in this address family.  Maintain a reference on ifa_maybe once
1633          * we find one, as we release the IF_ADDR_LOCK() that kept it stable
1634          * when we move onto the next interface.
1635          */
1636         IFNET_RLOCK_NOSLEEP();
1637         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1638                 IF_ADDR_LOCK(ifp);
1639                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1640                         char *cp, *cp2, *cp3;
1641
1642                         if (ifa->ifa_addr->sa_family != af)
1643 next:                           continue;
1644                         if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1645                                 /*
1646                                  * This is a bit broken as it doesn't
1647                                  * take into account that the remote end may
1648                                  * be a single node in the network we are
1649                                  * looking for.
1650                                  * The trouble is that we don't know the
1651                                  * netmask for the remote end.
1652                                  */
1653                                 if (ifa->ifa_dstaddr != NULL &&
1654                                     sa_equal(addr, ifa->ifa_dstaddr)) {
1655                                         ifa_ref(ifa);
1656                                         IF_ADDR_UNLOCK(ifp);
1657                                         goto done;
1658                                 }
1659                         } else {
1660                                 /*
1661                                  * if we have a special address handler,
1662                                  * then use it instead of the generic one.
1663                                  */
1664                                 if (ifa->ifa_claim_addr) {
1665                                         if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1666                                                 ifa_ref(ifa);
1667                                                 IF_ADDR_UNLOCK(ifp);
1668                                                 goto done;
1669                                         }
1670                                         continue;
1671                                 }
1672
1673                                 /*
1674                                  * Scan all the bits in the ifa's address.
1675                                  * If a bit dissagrees with what we are
1676                                  * looking for, mask it with the netmask
1677                                  * to see if it really matters.
1678                                  * (A byte at a time)
1679                                  */
1680                                 if (ifa->ifa_netmask == 0)
1681                                         continue;
1682                                 cp = addr_data;
1683                                 cp2 = ifa->ifa_addr->sa_data;
1684                                 cp3 = ifa->ifa_netmask->sa_data;
1685                                 cplim = ifa->ifa_netmask->sa_len
1686                                         + (char *)ifa->ifa_netmask;
1687                                 while (cp3 < cplim)
1688                                         if ((*cp++ ^ *cp2++) & *cp3++)
1689                                                 goto next; /* next address! */
1690                                 /*
1691                                  * If the netmask of what we just found
1692                                  * is more specific than what we had before
1693                                  * (if we had one) then remember the new one
1694                                  * before continuing to search
1695                                  * for an even better one.
1696                                  */
1697                                 if (ifa_maybe == NULL ||
1698                                     rn_refines((caddr_t)ifa->ifa_netmask,
1699                                     (caddr_t)ifa_maybe->ifa_netmask)) {
1700                                         if (ifa_maybe != NULL)
1701                                                 ifa_free(ifa_maybe);
1702                                         ifa_maybe = ifa;
1703                                         ifa_ref(ifa_maybe);
1704                                 }
1705                         }
1706                 }
1707                 IF_ADDR_UNLOCK(ifp);
1708         }
1709         ifa = ifa_maybe;
1710         ifa_maybe = NULL;
1711 done:
1712         IFNET_RUNLOCK_NOSLEEP();
1713         if (ifa_maybe != NULL)
1714                 ifa_free(ifa_maybe);
1715         return (ifa);
1716 }
1717
1718 /*
1719  * Find an interface address specific to an interface best matching
1720  * a given address.
1721  */
1722 struct ifaddr *
1723 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1724 {
1725         struct ifaddr *ifa;
1726         char *cp, *cp2, *cp3;
1727         char *cplim;
1728         struct ifaddr *ifa_maybe = NULL;
1729         u_int af = addr->sa_family;
1730
1731         if (af >= AF_MAX)
1732                 return (0);
1733         IF_ADDR_LOCK(ifp);
1734         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1735                 if (ifa->ifa_addr->sa_family != af)
1736                         continue;
1737                 if (ifa_maybe == NULL)
1738                         ifa_maybe = ifa;
1739                 if (ifa->ifa_netmask == 0) {
1740                         if (sa_equal(addr, ifa->ifa_addr) ||
1741                             (ifa->ifa_dstaddr &&
1742                             sa_equal(addr, ifa->ifa_dstaddr)))
1743                                 goto done;
1744                         continue;
1745                 }
1746                 if (ifp->if_flags & IFF_POINTOPOINT) {
1747                         if (sa_equal(addr, ifa->ifa_dstaddr))
1748                                 goto done;
1749                 } else {
1750                         cp = addr->sa_data;
1751                         cp2 = ifa->ifa_addr->sa_data;
1752                         cp3 = ifa->ifa_netmask->sa_data;
1753                         cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1754                         for (; cp3 < cplim; cp3++)
1755                                 if ((*cp++ ^ *cp2++) & *cp3)
1756                                         break;
1757                         if (cp3 == cplim)
1758                                 goto done;
1759                 }
1760         }
1761         ifa = ifa_maybe;
1762 done:
1763         if (ifa != NULL)
1764                 ifa_ref(ifa);
1765         IF_ADDR_UNLOCK(ifp);
1766         return (ifa);
1767 }
1768
1769 #include <net/if_llatbl.h>
1770
1771 /*
1772  * Default action when installing a route with a Link Level gateway.
1773  * Lookup an appropriate real ifa to point to.
1774  * This should be moved to /sys/net/link.c eventually.
1775  */
1776 static void
1777 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1778 {
1779         struct ifaddr *ifa, *oifa;
1780         struct sockaddr *dst;
1781         struct ifnet *ifp;
1782
1783         RT_LOCK_ASSERT(rt);
1784
1785         if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1786             ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1787                 return;
1788         ifa = ifaof_ifpforaddr(dst, ifp);
1789         if (ifa) {
1790                 oifa = rt->rt_ifa;
1791                 rt->rt_ifa = ifa;
1792                 ifa_free(oifa);
1793                 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1794                         ifa->ifa_rtrequest(cmd, rt, info);
1795         }
1796 }
1797
1798 /*
1799  * Mark an interface down and notify protocols of
1800  * the transition.
1801  * NOTE: must be called at splnet or eqivalent.
1802  */
1803 static void
1804 if_unroute(struct ifnet *ifp, int flag, int fam)
1805 {
1806         struct ifaddr *ifa;
1807
1808         KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1809
1810         ifp->if_flags &= ~flag;
1811         getmicrotime(&ifp->if_lastchange);
1812         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1813                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1814                         pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1815         ifp->if_qflush(ifp);
1816
1817 #if defined(INET) || defined(INET6)
1818 #ifdef DEV_CARP
1819         if (ifp->if_carp)
1820                 carp_carpdev_state(ifp->if_carp);
1821 #endif
1822 #endif
1823         rt_ifmsg(ifp);
1824 }
1825
1826 /*
1827  * Mark an interface up and notify protocols of
1828  * the transition.
1829  * NOTE: must be called at splnet or eqivalent.
1830  */
1831 static void
1832 if_route(struct ifnet *ifp, int flag, int fam)
1833 {
1834         struct ifaddr *ifa;
1835
1836         KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1837
1838         ifp->if_flags |= flag;
1839         getmicrotime(&ifp->if_lastchange);
1840         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1841                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1842                         pfctlinput(PRC_IFUP, ifa->ifa_addr);
1843 #if defined(INET) || defined(INET6)
1844 #ifdef DEV_CARP
1845         if (ifp->if_carp)
1846                 carp_carpdev_state(ifp->if_carp);
1847 #endif
1848 #endif
1849         rt_ifmsg(ifp);
1850 #ifdef INET6
1851         in6_if_up(ifp);
1852 #endif
1853 }
1854
1855 void    (*vlan_link_state_p)(struct ifnet *, int);      /* XXX: private from if_vlan */
1856 void    (*vlan_trunk_cap_p)(struct ifnet *);            /* XXX: private from if_vlan */
1857
1858 /*
1859  * Handle a change in the interface link state. To avoid LORs
1860  * between driver lock and upper layer locks, as well as possible
1861  * recursions, we post event to taskqueue, and all job
1862  * is done in static do_link_state_change().
1863  */
1864 void
1865 if_link_state_change(struct ifnet *ifp, int link_state)
1866 {
1867         /* Return if state hasn't changed. */
1868         if (ifp->if_link_state == link_state)
1869                 return;
1870
1871         ifp->if_link_state = link_state;
1872
1873         taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1874 }
1875
1876 static void
1877 do_link_state_change(void *arg, int pending)
1878 {
1879         struct ifnet *ifp = (struct ifnet *)arg;
1880         int link_state = ifp->if_link_state;
1881         int link;
1882         CURVNET_SET(ifp->if_vnet);
1883
1884         /* Notify that the link state has changed. */
1885         rt_ifmsg(ifp);
1886         if (link_state == LINK_STATE_UP)
1887                 link = NOTE_LINKUP;
1888         else if (link_state == LINK_STATE_DOWN)
1889                 link = NOTE_LINKDOWN;
1890         else
1891                 link = NOTE_LINKINV;
1892         if (ifp->if_vlantrunk != NULL)
1893                 (*vlan_link_state_p)(ifp, link);
1894
1895         if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1896             IFP2AC(ifp)->ac_netgraph != NULL)
1897                 (*ng_ether_link_state_p)(ifp, link_state);
1898 #if defined(INET) || defined(INET6)
1899 #ifdef DEV_CARP
1900         if (ifp->if_carp)
1901                 carp_carpdev_state(ifp->if_carp);
1902 #endif
1903 #endif
1904         if (ifp->if_bridge) {
1905                 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1906                 (*bstp_linkstate_p)(ifp, link_state);
1907         }
1908         if (ifp->if_lagg) {
1909                 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1910                 (*lagg_linkstate_p)(ifp, link_state);
1911         }
1912
1913         if (IS_DEFAULT_VNET(curvnet))
1914                 devctl_notify("IFNET", ifp->if_xname,
1915                     (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1916                     NULL);
1917         if (pending > 1)
1918                 if_printf(ifp, "%d link states coalesced\n", pending);
1919         if (log_link_state_change)
1920                 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1921                     (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1922         CURVNET_RESTORE();
1923 }
1924
1925 /*
1926  * Mark an interface down and notify protocols of
1927  * the transition.
1928  * NOTE: must be called at splnet or eqivalent.
1929  */
1930 void
1931 if_down(struct ifnet *ifp)
1932 {
1933
1934         if_unroute(ifp, IFF_UP, AF_UNSPEC);
1935 }
1936
1937 /*
1938  * Mark an interface up and notify protocols of
1939  * the transition.
1940  * NOTE: must be called at splnet or eqivalent.
1941  */
1942 void
1943 if_up(struct ifnet *ifp)
1944 {
1945
1946         if_route(ifp, IFF_UP, AF_UNSPEC);
1947 }
1948
1949 /*
1950  * Flush an interface queue.
1951  */
1952 void
1953 if_qflush(struct ifnet *ifp)
1954 {
1955         struct mbuf *m, *n;
1956         struct ifaltq *ifq;
1957         
1958         ifq = &ifp->if_snd;
1959         IFQ_LOCK(ifq);
1960 #ifdef ALTQ
1961         if (ALTQ_IS_ENABLED(ifq))
1962                 ALTQ_PURGE(ifq);
1963 #endif
1964         n = ifq->ifq_head;
1965         while ((m = n) != 0) {
1966                 n = m->m_act;
1967                 m_freem(m);
1968         }
1969         ifq->ifq_head = 0;
1970         ifq->ifq_tail = 0;
1971         ifq->ifq_len = 0;
1972         IFQ_UNLOCK(ifq);
1973 }
1974
1975 /*
1976  * Handle interface watchdog timer routines.  Called
1977  * from softclock, we decrement timers (if set) and
1978  * call the appropriate interface routine on expiration.
1979  *
1980  * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1981  * holding Giant.
1982  */
1983 static void
1984 if_slowtimo(void *arg)
1985 {
1986         VNET_ITERATOR_DECL(vnet_iter);
1987         struct ifnet *ifp;
1988         int s = splimp();
1989
1990         VNET_LIST_RLOCK_NOSLEEP();
1991         IFNET_RLOCK_NOSLEEP();
1992         VNET_FOREACH(vnet_iter) {
1993                 CURVNET_SET(vnet_iter);
1994                 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1995                         if (ifp->if_timer == 0 || --ifp->if_timer)
1996                                 continue;
1997                         if (ifp->if_watchdog)
1998                                 (*ifp->if_watchdog)(ifp);
1999                 }
2000                 CURVNET_RESTORE();
2001         }
2002         IFNET_RUNLOCK_NOSLEEP();
2003         VNET_LIST_RUNLOCK_NOSLEEP();
2004         splx(s);
2005         timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
2006 }
2007
2008 /*
2009  * Map interface name to interface structure pointer, with or without
2010  * returning a reference.
2011  */
2012 struct ifnet *
2013 ifunit_ref(const char *name)
2014 {
2015         struct ifnet *ifp;
2016
2017         IFNET_RLOCK_NOSLEEP();
2018         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2019                 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2020                     !(ifp->if_flags & IFF_DYING))
2021                         break;
2022         }
2023         if (ifp != NULL)
2024                 if_ref(ifp);
2025         IFNET_RUNLOCK_NOSLEEP();
2026         return (ifp);
2027 }
2028
2029 struct ifnet *
2030 ifunit(const char *name)
2031 {
2032         struct ifnet *ifp;
2033
2034         IFNET_RLOCK_NOSLEEP();
2035         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2036                 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2037                         break;
2038         }
2039         IFNET_RUNLOCK_NOSLEEP();
2040         return (ifp);
2041 }
2042
2043 /*
2044  * Hardware specific interface ioctls.
2045  */
2046 static int
2047 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2048 {
2049         struct ifreq *ifr;
2050         struct ifstat *ifs;
2051         int error = 0;
2052         int new_flags, temp_flags;
2053         size_t namelen, onamelen;
2054         char new_name[IFNAMSIZ];
2055         struct ifaddr *ifa;
2056         struct sockaddr_dl *sdl;
2057
2058         ifr = (struct ifreq *)data;
2059         switch (cmd) {
2060         case SIOCGIFINDEX:
2061                 ifr->ifr_index = ifp->if_index;
2062                 break;
2063
2064         case SIOCGIFFLAGS:
2065                 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2066                 ifr->ifr_flags = temp_flags & 0xffff;
2067                 ifr->ifr_flagshigh = temp_flags >> 16;
2068                 break;
2069
2070         case SIOCGIFCAP:
2071                 ifr->ifr_reqcap = ifp->if_capabilities;
2072                 ifr->ifr_curcap = ifp->if_capenable;
2073                 break;
2074
2075 #ifdef MAC
2076         case SIOCGIFMAC:
2077                 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2078                 break;
2079 #endif
2080
2081         case SIOCGIFMETRIC:
2082                 ifr->ifr_metric = ifp->if_metric;
2083                 break;
2084
2085         case SIOCGIFMTU:
2086                 ifr->ifr_mtu = ifp->if_mtu;
2087                 break;
2088
2089         case SIOCGIFPHYS:
2090                 ifr->ifr_phys = ifp->if_physical;
2091                 break;
2092
2093         case SIOCSIFFLAGS:
2094                 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2095                 if (error)
2096                         return (error);
2097                 /*
2098                  * Currently, no driver owned flags pass the IFF_CANTCHANGE
2099                  * check, so we don't need special handling here yet.
2100                  */
2101                 new_flags = (ifr->ifr_flags & 0xffff) |
2102                     (ifr->ifr_flagshigh << 16);
2103                 if (ifp->if_flags & IFF_SMART) {
2104                         /* Smart drivers twiddle their own routes */
2105                 } else if (ifp->if_flags & IFF_UP &&
2106                     (new_flags & IFF_UP) == 0) {
2107                         int s = splimp();
2108                         if_down(ifp);
2109                         splx(s);
2110                 } else if (new_flags & IFF_UP &&
2111                     (ifp->if_flags & IFF_UP) == 0) {
2112                         int s = splimp();
2113                         if_up(ifp);
2114                         splx(s);
2115                 }
2116                 /* See if permanently promiscuous mode bit is about to flip */
2117                 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2118                         if (new_flags & IFF_PPROMISC)
2119                                 ifp->if_flags |= IFF_PROMISC;
2120                         else if (ifp->if_pcount == 0)
2121                                 ifp->if_flags &= ~IFF_PROMISC;
2122                         log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2123                             ifp->if_xname,
2124                             (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2125                 }
2126                 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2127                         (new_flags &~ IFF_CANTCHANGE);
2128                 if (ifp->if_ioctl) {
2129                         (void) (*ifp->if_ioctl)(ifp, cmd, data);
2130                 }
2131                 getmicrotime(&ifp->if_lastchange);
2132                 break;
2133
2134         case SIOCSIFCAP:
2135                 error = priv_check(td, PRIV_NET_SETIFCAP);
2136                 if (error)
2137                         return (error);
2138                 if (ifp->if_ioctl == NULL)
2139                         return (EOPNOTSUPP);
2140                 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2141                         return (EINVAL);
2142                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2143                 if (error == 0)
2144                         getmicrotime(&ifp->if_lastchange);
2145                 break;
2146
2147 #ifdef MAC
2148         case SIOCSIFMAC:
2149                 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2150                 break;
2151 #endif
2152
2153         case SIOCSIFNAME:
2154                 error = priv_check(td, PRIV_NET_SETIFNAME);
2155                 if (error)
2156                         return (error);
2157                 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2158                 if (error != 0)
2159                         return (error);
2160                 if (new_name[0] == '\0')
2161                         return (EINVAL);
2162                 if (ifunit(new_name) != NULL)
2163                         return (EEXIST);
2164                 
2165                 /* Announce the departure of the interface. */
2166                 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2167                 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2168
2169                 log(LOG_INFO, "%s: changing name to '%s'\n",
2170                     ifp->if_xname, new_name);
2171
2172                 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2173                 ifa = ifp->if_addr;
2174                 IFA_LOCK(ifa);
2175                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2176                 namelen = strlen(new_name);
2177                 onamelen = sdl->sdl_nlen;
2178                 /*
2179                  * Move the address if needed.  This is safe because we
2180                  * allocate space for a name of length IFNAMSIZ when we
2181                  * create this in if_attach().
2182                  */
2183                 if (namelen != onamelen) {
2184                         bcopy(sdl->sdl_data + onamelen,
2185                             sdl->sdl_data + namelen, sdl->sdl_alen);
2186                 }
2187                 bcopy(new_name, sdl->sdl_data, namelen);
2188                 sdl->sdl_nlen = namelen;
2189                 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2190                 bzero(sdl->sdl_data, onamelen);
2191                 while (namelen != 0)
2192                         sdl->sdl_data[--namelen] = 0xff;
2193                 IFA_UNLOCK(ifa);
2194
2195                 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2196                 /* Announce the return of the interface. */
2197                 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2198                 break;
2199
2200 #ifdef VIMAGE
2201         case SIOCSIFVNET:
2202                 error = priv_check(td, PRIV_NET_SETIFVNET);
2203                 if (error)
2204                         return (error);
2205                 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2206                 break;
2207 #endif
2208
2209         case SIOCSIFMETRIC:
2210                 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2211                 if (error)
2212                         return (error);
2213                 ifp->if_metric = ifr->ifr_metric;
2214                 getmicrotime(&ifp->if_lastchange);
2215                 break;
2216
2217         case SIOCSIFPHYS:
2218                 error = priv_check(td, PRIV_NET_SETIFPHYS);
2219                 if (error)
2220                         return (error);
2221                 if (ifp->if_ioctl == NULL)
2222                         return (EOPNOTSUPP);
2223                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2224                 if (error == 0)
2225                         getmicrotime(&ifp->if_lastchange);
2226                 break;
2227
2228         case SIOCSIFMTU:
2229         {
2230                 u_long oldmtu = ifp->if_mtu;
2231
2232                 error = priv_check(td, PRIV_NET_SETIFMTU);
2233                 if (error)
2234                         return (error);
2235                 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2236                         return (EINVAL);
2237                 if (ifp->if_ioctl == NULL)
2238                         return (EOPNOTSUPP);
2239                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2240                 if (error == 0) {
2241                         getmicrotime(&ifp->if_lastchange);
2242                         rt_ifmsg(ifp);
2243                 }
2244                 /*
2245                  * If the link MTU changed, do network layer specific procedure.
2246                  */
2247                 if (ifp->if_mtu != oldmtu) {
2248 #ifdef INET6
2249                         nd6_setmtu(ifp);
2250 #endif
2251                 }
2252                 break;
2253         }
2254
2255         case SIOCADDMULTI:
2256         case SIOCDELMULTI:
2257                 if (cmd == SIOCADDMULTI)
2258                         error = priv_check(td, PRIV_NET_ADDMULTI);
2259                 else
2260                         error = priv_check(td, PRIV_NET_DELMULTI);
2261                 if (error)
2262                         return (error);
2263
2264                 /* Don't allow group membership on non-multicast interfaces. */
2265                 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2266                         return (EOPNOTSUPP);
2267
2268                 /* Don't let users screw up protocols' entries. */
2269                 if (ifr->ifr_addr.sa_family != AF_LINK)
2270                         return (EINVAL);
2271
2272                 if (cmd == SIOCADDMULTI) {
2273                         struct ifmultiaddr *ifma;
2274
2275                         /*
2276                          * Userland is only permitted to join groups once
2277                          * via the if_addmulti() KPI, because it cannot hold
2278                          * struct ifmultiaddr * between calls. It may also
2279                          * lose a race while we check if the membership
2280                          * already exists.
2281                          */
2282                         IF_ADDR_LOCK(ifp);
2283                         ifma = if_findmulti(ifp, &ifr->ifr_addr);
2284                         IF_ADDR_UNLOCK(ifp);
2285                         if (ifma != NULL)
2286                                 error = EADDRINUSE;
2287                         else
2288                                 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2289                 } else {
2290                         error = if_delmulti(ifp, &ifr->ifr_addr);
2291                 }
2292                 if (error == 0)
2293                         getmicrotime(&ifp->if_lastchange);
2294                 break;
2295
2296         case SIOCSIFPHYADDR:
2297         case SIOCDIFPHYADDR:
2298 #ifdef INET6
2299         case SIOCSIFPHYADDR_IN6:
2300 #endif
2301         case SIOCSLIFPHYADDR:
2302         case SIOCSIFMEDIA:
2303         case SIOCSIFGENERIC:
2304                 error = priv_check(td, PRIV_NET_HWIOCTL);
2305                 if (error)
2306                         return (error);
2307                 if (ifp->if_ioctl == NULL)
2308                         return (EOPNOTSUPP);
2309                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2310                 if (error == 0)
2311                         getmicrotime(&ifp->if_lastchange);
2312                 break;
2313
2314         case SIOCGIFSTATUS:
2315                 ifs = (struct ifstat *)data;
2316                 ifs->ascii[0] = '\0';
2317
2318         case SIOCGIFPSRCADDR:
2319         case SIOCGIFPDSTADDR:
2320         case SIOCGLIFPHYADDR:
2321         case SIOCGIFMEDIA:
2322         case SIOCGIFGENERIC:
2323                 if (ifp->if_ioctl == NULL)
2324                         return (EOPNOTSUPP);
2325                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2326                 break;
2327
2328         case SIOCSIFLLADDR:
2329                 error = priv_check(td, PRIV_NET_SETLLADDR);
2330                 if (error)
2331                         return (error);
2332                 error = if_setlladdr(ifp,
2333                     ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2334                 break;
2335
2336         case SIOCAIFGROUP:
2337         {
2338                 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2339
2340                 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2341                 if (error)
2342                         return (error);
2343                 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2344                         return (error);
2345                 break;
2346         }
2347
2348         case SIOCGIFGROUP:
2349                 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2350                         return (error);
2351                 break;
2352
2353         case SIOCDIFGROUP:
2354         {
2355                 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2356
2357                 error = priv_check(td, PRIV_NET_DELIFGROUP);
2358                 if (error)
2359                         return (error);
2360                 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2361                         return (error);
2362                 break;
2363         }
2364
2365         default:
2366                 error = ENOIOCTL;
2367                 break;
2368         }
2369         return (error);
2370 }
2371
2372 /*
2373  * Interface ioctls.
2374  */
2375 int
2376 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2377 {
2378         struct ifnet *ifp;
2379         struct ifreq *ifr;
2380         int error;
2381         int oif_flags;
2382
2383         switch (cmd) {
2384         case SIOCGIFCONF:
2385         case OSIOCGIFCONF:
2386 #ifdef __amd64__
2387         case SIOCGIFCONF32:
2388 #endif
2389                 return (ifconf(cmd, data));
2390         }
2391         ifr = (struct ifreq *)data;
2392
2393         switch (cmd) {
2394 #ifdef VIMAGE
2395         case SIOCSIFRVNET:
2396                 error = priv_check(td, PRIV_NET_SETIFVNET);
2397                 if (error)
2398                         return (error);
2399                 return (if_vmove_reclaim(td, ifr->ifr_name, ifr->ifr_jid));
2400 #endif
2401         case SIOCIFCREATE:
2402         case SIOCIFCREATE2:
2403                 error = priv_check(td, PRIV_NET_IFCREATE);
2404                 if (error)
2405                         return (error);
2406                 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2407                         cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2408         case SIOCIFDESTROY:
2409                 error = priv_check(td, PRIV_NET_IFDESTROY);
2410                 if (error)
2411                         return (error);
2412                 return if_clone_destroy(ifr->ifr_name);
2413
2414         case SIOCIFGCLONERS:
2415                 return (if_clone_list((struct if_clonereq *)data));
2416         case SIOCGIFGMEMB:
2417                 return (if_getgroupmembers((struct ifgroupreq *)data));
2418         }
2419
2420         ifp = ifunit_ref(ifr->ifr_name);
2421         if (ifp == NULL)
2422                 return (ENXIO);
2423
2424         error = ifhwioctl(cmd, ifp, data, td);
2425         if (error != ENOIOCTL) {
2426                 if_rele(ifp);
2427                 return (error);
2428         }
2429
2430         oif_flags = ifp->if_flags;
2431         if (so->so_proto == NULL) {
2432                 if_rele(ifp);
2433                 return (EOPNOTSUPP);
2434         }
2435 #ifndef COMPAT_43
2436         error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2437                                                                  data,
2438                                                                  ifp, td));
2439         if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2440                 error = (*ifp->if_ioctl)(ifp, cmd, data);
2441 #else
2442         {
2443                 u_long ocmd = cmd;
2444
2445                 switch (cmd) {
2446
2447                 case SIOCSIFDSTADDR:
2448                 case SIOCSIFADDR:
2449                 case SIOCSIFBRDADDR:
2450                 case SIOCSIFNETMASK:
2451 #if BYTE_ORDER != BIG_ENDIAN
2452                         if (ifr->ifr_addr.sa_family == 0 &&
2453                             ifr->ifr_addr.sa_len < 16) {
2454                                 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2455                                 ifr->ifr_addr.sa_len = 16;
2456                         }
2457 #else
2458                         if (ifr->ifr_addr.sa_len == 0)
2459                                 ifr->ifr_addr.sa_len = 16;
2460 #endif
2461                         break;
2462
2463                 case OSIOCGIFADDR:
2464                         cmd = SIOCGIFADDR;
2465                         break;
2466
2467                 case OSIOCGIFDSTADDR:
2468                         cmd = SIOCGIFDSTADDR;
2469                         break;
2470
2471                 case OSIOCGIFBRDADDR:
2472                         cmd = SIOCGIFBRDADDR;
2473                         break;
2474
2475                 case OSIOCGIFNETMASK:
2476                         cmd = SIOCGIFNETMASK;
2477                 }
2478                 error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
2479                                                                    cmd,
2480                                                                    data,
2481                                                                    ifp, td));
2482                 if (error == EOPNOTSUPP && ifp != NULL &&
2483                     ifp->if_ioctl != NULL)
2484                         error = (*ifp->if_ioctl)(ifp, cmd, data);
2485                 switch (ocmd) {
2486
2487                 case OSIOCGIFADDR:
2488                 case OSIOCGIFDSTADDR:
2489                 case OSIOCGIFBRDADDR:
2490                 case OSIOCGIFNETMASK:
2491                         *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2492
2493                 }
2494         }
2495 #endif /* COMPAT_43 */
2496
2497         if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2498 #ifdef INET6
2499                 if (ifp->if_flags & IFF_UP) {
2500                         int s = splimp();
2501                         in6_if_up(ifp);
2502                         splx(s);
2503                 }
2504 #endif
2505         }
2506         if_rele(ifp);
2507         return (error);
2508 }
2509
2510 /*
2511  * The code common to handling reference counted flags,
2512  * e.g., in ifpromisc() and if_allmulti().
2513  * The "pflag" argument can specify a permanent mode flag to check,
2514  * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2515  *
2516  * Only to be used on stack-owned flags, not driver-owned flags.
2517  */
2518 static int
2519 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2520 {
2521         struct ifreq ifr;
2522         int error;
2523         int oldflags, oldcount;
2524
2525         /* Sanity checks to catch programming errors */
2526         KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2527             ("%s: setting driver-owned flag %d", __func__, flag));
2528
2529         if (onswitch)
2530                 KASSERT(*refcount >= 0,
2531                     ("%s: increment negative refcount %d for flag %d",
2532                     __func__, *refcount, flag));
2533         else
2534                 KASSERT(*refcount > 0,
2535                     ("%s: decrement non-positive refcount %d for flag %d",
2536                     __func__, *refcount, flag));
2537
2538         /* In case this mode is permanent, just touch refcount */
2539         if (ifp->if_flags & pflag) {
2540                 *refcount += onswitch ? 1 : -1;
2541                 return (0);
2542         }
2543
2544         /* Save ifnet parameters for if_ioctl() may fail */
2545         oldcount = *refcount;
2546         oldflags = ifp->if_flags;
2547         
2548         /*
2549          * See if we aren't the only and touching refcount is enough.
2550          * Actually toggle interface flag if we are the first or last.
2551          */
2552         if (onswitch) {
2553                 if ((*refcount)++)
2554                         return (0);
2555                 ifp->if_flags |= flag;
2556         } else {
2557                 if (--(*refcount))
2558                         return (0);
2559                 ifp->if_flags &= ~flag;
2560         }
2561
2562         /* Call down the driver since we've changed interface flags */
2563         if (ifp->if_ioctl == NULL) {
2564                 error = EOPNOTSUPP;
2565                 goto recover;
2566         }
2567         ifr.ifr_flags = ifp->if_flags & 0xffff;
2568         ifr.ifr_flagshigh = ifp->if_flags >> 16;
2569         error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2570         if (error)
2571                 goto recover;
2572         /* Notify userland that interface flags have changed */
2573         rt_ifmsg(ifp);
2574         return (0);
2575
2576 recover:
2577         /* Recover after driver error */
2578         *refcount = oldcount;
2579         ifp->if_flags = oldflags;
2580         return (error);
2581 }
2582
2583 /*
2584  * Set/clear promiscuous mode on interface ifp based on the truth value
2585  * of pswitch.  The calls are reference counted so that only the first
2586  * "on" request actually has an effect, as does the final "off" request.
2587  * Results are undefined if the "off" and "on" requests are not matched.
2588  */
2589 int
2590 ifpromisc(struct ifnet *ifp, int pswitch)
2591 {
2592         int error;
2593         int oldflags = ifp->if_flags;
2594
2595         error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2596                            &ifp->if_pcount, pswitch);
2597         /* If promiscuous mode status has changed, log a message */
2598         if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2599                 log(LOG_INFO, "%s: promiscuous mode %s\n",
2600                     ifp->if_xname,
2601                     (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2602         return (error);
2603 }
2604
2605 /*
2606  * Return interface configuration
2607  * of system.  List may be used
2608  * in later ioctl's (above) to get
2609  * other information.
2610  */
2611 /*ARGSUSED*/
2612 static int
2613 ifconf(u_long cmd, caddr_t data)
2614 {
2615         struct ifconf *ifc = (struct ifconf *)data;
2616 #ifdef __amd64__
2617         struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2618         struct ifconf ifc_swab;
2619 #endif
2620         struct ifnet *ifp;
2621         struct ifaddr *ifa;
2622         struct ifreq ifr;
2623         struct sbuf *sb;
2624         int error, full = 0, valid_len, max_len;
2625
2626 #ifdef __amd64__
2627         if (cmd == SIOCGIFCONF32) {
2628                 ifc_swab.ifc_len = ifc32->ifc_len;
2629                 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2630                 ifc = &ifc_swab;
2631         }
2632 #endif
2633         /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2634         max_len = MAXPHYS - 1;
2635
2636         /* Prevent hostile input from being able to crash the system */
2637         if (ifc->ifc_len <= 0)
2638                 return (EINVAL);
2639
2640 again:
2641         if (ifc->ifc_len <= max_len) {
2642                 max_len = ifc->ifc_len;
2643                 full = 1;
2644         }
2645         sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2646         max_len = 0;
2647         valid_len = 0;
2648
2649         IFNET_RLOCK();
2650         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2651                 int addrs;
2652
2653                 /*
2654                  * Zero the ifr_name buffer to make sure we don't
2655                  * disclose the contents of the stack.
2656                  */
2657                 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2658
2659                 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2660                     >= sizeof(ifr.ifr_name)) {
2661                         sbuf_delete(sb);
2662                         IFNET_RUNLOCK();
2663                         return (ENAMETOOLONG);
2664                 }
2665
2666                 addrs = 0;
2667                 IF_ADDR_LOCK(ifp);
2668                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2669                         struct sockaddr *sa = ifa->ifa_addr;
2670
2671                         if (prison_if(curthread->td_ucred, sa) != 0)
2672                                 continue;
2673                         addrs++;
2674 #ifdef COMPAT_43
2675                         if (cmd == OSIOCGIFCONF) {
2676                                 struct osockaddr *osa =
2677                                          (struct osockaddr *)&ifr.ifr_addr;
2678                                 ifr.ifr_addr = *sa;
2679                                 osa->sa_family = sa->sa_family;
2680                                 sbuf_bcat(sb, &ifr, sizeof(ifr));
2681                                 max_len += sizeof(ifr);
2682                         } else
2683 #endif
2684                         if (sa->sa_len <= sizeof(*sa)) {
2685                                 ifr.ifr_addr = *sa;
2686                                 sbuf_bcat(sb, &ifr, sizeof(ifr));
2687                                 max_len += sizeof(ifr);
2688                         } else {
2689                                 sbuf_bcat(sb, &ifr,
2690                                     offsetof(struct ifreq, ifr_addr));
2691                                 max_len += offsetof(struct ifreq, ifr_addr);
2692                                 sbuf_bcat(sb, sa, sa->sa_len);
2693                                 max_len += sa->sa_len;
2694                         }
2695
2696                         if (!sbuf_overflowed(sb))
2697                                 valid_len = sbuf_len(sb);
2698                 }
2699                 IF_ADDR_UNLOCK(ifp);
2700                 if (addrs == 0) {
2701                         bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2702                         sbuf_bcat(sb, &ifr, sizeof(ifr));
2703                         max_len += sizeof(ifr);
2704
2705                         if (!sbuf_overflowed(sb))
2706                                 valid_len = sbuf_len(sb);
2707                 }
2708         }
2709         IFNET_RUNLOCK();
2710
2711         /*
2712          * If we didn't allocate enough space (uncommon), try again.  If
2713          * we have already allocated as much space as we are allowed,
2714          * return what we've got.
2715          */
2716         if (valid_len != max_len && !full) {
2717                 sbuf_delete(sb);
2718                 goto again;
2719         }
2720
2721         ifc->ifc_len = valid_len;
2722 #ifdef __amd64__
2723         if (cmd == SIOCGIFCONF32)
2724                 ifc32->ifc_len = valid_len;
2725 #endif
2726         sbuf_finish(sb);
2727         error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2728         sbuf_delete(sb);
2729         return (error);
2730 }
2731
2732 /*
2733  * Just like ifpromisc(), but for all-multicast-reception mode.
2734  */
2735 int
2736 if_allmulti(struct ifnet *ifp, int onswitch)
2737 {
2738
2739         return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2740 }
2741
2742 struct ifmultiaddr *
2743 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2744 {
2745         struct ifmultiaddr *ifma;
2746
2747         IF_ADDR_LOCK_ASSERT(ifp);
2748
2749         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2750                 if (sa->sa_family == AF_LINK) {
2751                         if (sa_dl_equal(ifma->ifma_addr, sa))
2752                                 break;
2753                 } else {
2754                         if (sa_equal(ifma->ifma_addr, sa))
2755                                 break;
2756                 }
2757         }
2758
2759         return ifma;
2760 }
2761
2762 /*
2763  * Allocate a new ifmultiaddr and initialize based on passed arguments.  We
2764  * make copies of passed sockaddrs.  The ifmultiaddr will not be added to
2765  * the ifnet multicast address list here, so the caller must do that and
2766  * other setup work (such as notifying the device driver).  The reference
2767  * count is initialized to 1.
2768  */
2769 static struct ifmultiaddr *
2770 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2771     int mflags)
2772 {
2773         struct ifmultiaddr *ifma;
2774         struct sockaddr *dupsa;
2775
2776         ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2777             M_ZERO);
2778         if (ifma == NULL)
2779                 return (NULL);
2780
2781         dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2782         if (dupsa == NULL) {
2783                 free(ifma, M_IFMADDR);
2784                 return (NULL);
2785         }
2786         bcopy(sa, dupsa, sa->sa_len);
2787         ifma->ifma_addr = dupsa;
2788
2789         ifma->ifma_ifp = ifp;
2790         ifma->ifma_refcount = 1;
2791         ifma->ifma_protospec = NULL;
2792
2793         if (llsa == NULL) {
2794                 ifma->ifma_lladdr = NULL;
2795                 return (ifma);
2796         }
2797
2798         dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2799         if (dupsa == NULL) {
2800                 free(ifma->ifma_addr, M_IFMADDR);
2801                 free(ifma, M_IFMADDR);
2802                 return (NULL);
2803         }
2804         bcopy(llsa, dupsa, llsa->sa_len);
2805         ifma->ifma_lladdr = dupsa;
2806
2807         return (ifma);
2808 }
2809
2810 /*
2811  * if_freemulti: free ifmultiaddr structure and possibly attached related
2812  * addresses.  The caller is responsible for implementing reference
2813  * counting, notifying the driver, handling routing messages, and releasing
2814  * any dependent link layer state.
2815  */
2816 static void
2817 if_freemulti(struct ifmultiaddr *ifma)
2818 {
2819
2820         KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2821             ifma->ifma_refcount));
2822         KASSERT(ifma->ifma_protospec == NULL,
2823             ("if_freemulti: protospec not NULL"));
2824
2825         if (ifma->ifma_lladdr != NULL)
2826                 free(ifma->ifma_lladdr, M_IFMADDR);
2827         free(ifma->ifma_addr, M_IFMADDR);
2828         free(ifma, M_IFMADDR);
2829 }
2830
2831 /*
2832  * Register an additional multicast address with a network interface.
2833  *
2834  * - If the address is already present, bump the reference count on the
2835  *   address and return.
2836  * - If the address is not link-layer, look up a link layer address.
2837  * - Allocate address structures for one or both addresses, and attach to the
2838  *   multicast address list on the interface.  If automatically adding a link
2839  *   layer address, the protocol address will own a reference to the link
2840  *   layer address, to be freed when it is freed.
2841  * - Notify the network device driver of an addition to the multicast address
2842  *   list.
2843  *
2844  * 'sa' points to caller-owned memory with the desired multicast address.
2845  *
2846  * 'retifma' will be used to return a pointer to the resulting multicast
2847  * address reference, if desired.
2848  */
2849 int
2850 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2851     struct ifmultiaddr **retifma)
2852 {
2853         struct ifmultiaddr *ifma, *ll_ifma;
2854         struct sockaddr *llsa;
2855         int error;
2856
2857         /*
2858          * If the address is already present, return a new reference to it;
2859          * otherwise, allocate storage and set up a new address.
2860          */
2861         IF_ADDR_LOCK(ifp);
2862         ifma = if_findmulti(ifp, sa);
2863         if (ifma != NULL) {
2864                 ifma->ifma_refcount++;
2865                 if (retifma != NULL)
2866                         *retifma = ifma;
2867                 IF_ADDR_UNLOCK(ifp);
2868                 return (0);
2869         }
2870
2871         /*
2872          * The address isn't already present; resolve the protocol address
2873          * into a link layer address, and then look that up, bump its
2874          * refcount or allocate an ifma for that also.  If 'llsa' was
2875          * returned, we will need to free it later.
2876          */
2877         llsa = NULL;
2878         ll_ifma = NULL;
2879         if (ifp->if_resolvemulti != NULL) {
2880                 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2881                 if (error)
2882                         goto unlock_out;
2883         }
2884
2885         /*
2886          * Allocate the new address.  Don't hook it up yet, as we may also
2887          * need to allocate a link layer multicast address.
2888          */
2889         ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2890         if (ifma == NULL) {
2891                 error = ENOMEM;
2892                 goto free_llsa_out;
2893         }
2894
2895         /*
2896          * If a link layer address is found, we'll need to see if it's
2897          * already present in the address list, or allocate is as well.
2898          * When this block finishes, the link layer address will be on the
2899          * list.
2900          */
2901         if (llsa != NULL) {
2902                 ll_ifma = if_findmulti(ifp, llsa);
2903                 if (ll_ifma == NULL) {
2904                         ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2905                         if (ll_ifma == NULL) {
2906                                 --ifma->ifma_refcount;
2907                                 if_freemulti(ifma);
2908                                 error = ENOMEM;
2909                                 goto free_llsa_out;
2910                         }
2911                         TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2912                             ifma_link);
2913                 } else
2914                         ll_ifma->ifma_refcount++;
2915                 ifma->ifma_llifma = ll_ifma;
2916         }
2917
2918         /*
2919          * We now have a new multicast address, ifma, and possibly a new or
2920          * referenced link layer address.  Add the primary address to the
2921          * ifnet address list.
2922          */
2923         TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2924
2925         if (retifma != NULL)
2926                 *retifma = ifma;
2927
2928         /*
2929          * Must generate the message while holding the lock so that 'ifma'
2930          * pointer is still valid.
2931          */
2932         rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2933         IF_ADDR_UNLOCK(ifp);
2934
2935         /*
2936          * We are certain we have added something, so call down to the
2937          * interface to let them know about it.
2938          */
2939         if (ifp->if_ioctl != NULL) {
2940                 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2941         }
2942
2943         if (llsa != NULL)
2944                 free(llsa, M_IFMADDR);
2945
2946         return (0);
2947
2948 free_llsa_out:
2949         if (llsa != NULL)
2950                 free(llsa, M_IFMADDR);
2951
2952 unlock_out:
2953         IF_ADDR_UNLOCK(ifp);
2954         return (error);
2955 }
2956
2957 /*
2958  * Delete a multicast group membership by network-layer group address.
2959  *
2960  * Returns ENOENT if the entry could not be found. If ifp no longer
2961  * exists, results are undefined. This entry point should only be used
2962  * from subsystems which do appropriate locking to hold ifp for the
2963  * duration of the call.
2964  * Network-layer protocol domains must use if_delmulti_ifma().
2965  */
2966 int
2967 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2968 {
2969         struct ifmultiaddr *ifma;
2970         int lastref;
2971 #ifdef INVARIANTS
2972         struct ifnet *oifp;
2973
2974         IFNET_RLOCK_NOSLEEP();
2975         TAILQ_FOREACH(oifp, &V_ifnet, if_link)
2976                 if (ifp == oifp)
2977                         break;
2978         if (ifp != oifp)
2979                 ifp = NULL;
2980         IFNET_RUNLOCK_NOSLEEP();
2981
2982         KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
2983 #endif
2984         if (ifp == NULL)
2985                 return (ENOENT);
2986
2987         IF_ADDR_LOCK(ifp);
2988         lastref = 0;
2989         ifma = if_findmulti(ifp, sa);
2990         if (ifma != NULL)
2991                 lastref = if_delmulti_locked(ifp, ifma, 0);
2992         IF_ADDR_UNLOCK(ifp);
2993
2994         if (ifma == NULL)
2995                 return (ENOENT);
2996
2997         if (lastref && ifp->if_ioctl != NULL) {
2998                 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2999         }
3000
3001         return (0);
3002 }
3003
3004 /*
3005  * Delete a multicast group membership by group membership pointer.
3006  * Network-layer protocol domains must use this routine.
3007  *
3008  * It is safe to call this routine if the ifp disappeared.
3009  */
3010 void
3011 if_delmulti_ifma(struct ifmultiaddr *ifma)
3012 {
3013         struct ifnet *ifp;
3014         int lastref;
3015
3016         ifp = ifma->ifma_ifp;
3017 #ifdef DIAGNOSTIC
3018         if (ifp == NULL) {
3019                 printf("%s: ifma_ifp seems to be detached\n", __func__);
3020         } else {
3021                 struct ifnet *oifp;
3022
3023                 IFNET_RLOCK_NOSLEEP();
3024                 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3025                         if (ifp == oifp)
3026                                 break;
3027                 if (ifp != oifp) {
3028                         printf("%s: ifnet %p disappeared\n", __func__, ifp);
3029                         ifp = NULL;
3030                 }
3031                 IFNET_RUNLOCK_NOSLEEP();
3032         }
3033 #endif
3034         /*
3035          * If and only if the ifnet instance exists: Acquire the address lock.
3036          */
3037         if (ifp != NULL)
3038                 IF_ADDR_LOCK(ifp);
3039
3040         lastref = if_delmulti_locked(ifp, ifma, 0);
3041
3042         if (ifp != NULL) {
3043                 /*
3044                  * If and only if the ifnet instance exists:
3045                  *  Release the address lock.
3046                  *  If the group was left: update the hardware hash filter.
3047                  */
3048                 IF_ADDR_UNLOCK(ifp);
3049                 if (lastref && ifp->if_ioctl != NULL) {
3050                         (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3051                 }
3052         }
3053 }
3054
3055 /*
3056  * Perform deletion of network-layer and/or link-layer multicast address.
3057  *
3058  * Return 0 if the reference count was decremented.
3059  * Return 1 if the final reference was released, indicating that the
3060  * hardware hash filter should be reprogrammed.
3061  */
3062 static int
3063 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3064 {
3065         struct ifmultiaddr *ll_ifma;
3066
3067         if (ifp != NULL && ifma->ifma_ifp != NULL) {
3068                 KASSERT(ifma->ifma_ifp == ifp,
3069                     ("%s: inconsistent ifp %p", __func__, ifp));
3070                 IF_ADDR_LOCK_ASSERT(ifp);
3071         }
3072
3073         ifp = ifma->ifma_ifp;
3074
3075         /*
3076          * If the ifnet is detaching, null out references to ifnet,
3077          * so that upper protocol layers will notice, and not attempt
3078          * to obtain locks for an ifnet which no longer exists. The
3079          * routing socket announcement must happen before the ifnet
3080          * instance is detached from the system.
3081          */
3082         if (detaching) {
3083 #ifdef DIAGNOSTIC
3084                 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3085 #endif
3086                 /*
3087                  * ifp may already be nulled out if we are being reentered
3088                  * to delete the ll_ifma.
3089                  */
3090                 if (ifp != NULL) {
3091                         rt_newmaddrmsg(RTM_DELMADDR, ifma);
3092                         ifma->ifma_ifp = NULL;
3093                 }
3094         }
3095
3096         if (--ifma->ifma_refcount > 0)
3097                 return 0;
3098
3099         /*
3100          * If this ifma is a network-layer ifma, a link-layer ifma may
3101          * have been associated with it. Release it first if so.
3102          */
3103         ll_ifma = ifma->ifma_llifma;
3104         if (ll_ifma != NULL) {
3105                 KASSERT(ifma->ifma_lladdr != NULL,
3106                     ("%s: llifma w/o lladdr", __func__));
3107                 if (detaching)
3108                         ll_ifma->ifma_ifp = NULL;       /* XXX */
3109                 if (--ll_ifma->ifma_refcount == 0) {
3110                         if (ifp != NULL) {
3111                                 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3112                                     ifma_link);
3113                         }
3114                         if_freemulti(ll_ifma);
3115                 }
3116         }
3117
3118         if (ifp != NULL)
3119                 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3120
3121         if_freemulti(ifma);
3122
3123         /*
3124          * The last reference to this instance of struct ifmultiaddr
3125          * was released; the hardware should be notified of this change.
3126          */
3127         return 1;
3128 }
3129
3130 /*
3131  * Set the link layer address on an interface.
3132  *
3133  * At this time we only support certain types of interfaces,
3134  * and we don't allow the length of the address to change.
3135  */
3136 int
3137 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3138 {
3139         struct sockaddr_dl *sdl;
3140         struct ifaddr *ifa;
3141         struct ifreq ifr;
3142
3143         IF_ADDR_LOCK(ifp);
3144         ifa = ifp->if_addr;
3145         if (ifa == NULL) {
3146                 IF_ADDR_UNLOCK(ifp);
3147                 return (EINVAL);
3148         }
3149         ifa_ref(ifa);
3150         IF_ADDR_UNLOCK(ifp);
3151         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3152         if (sdl == NULL) {
3153                 ifa_free(ifa);
3154                 return (EINVAL);
3155         }
3156         if (len != sdl->sdl_alen) {     /* don't allow length to change */
3157                 ifa_free(ifa);
3158                 return (EINVAL);
3159         }
3160         switch (ifp->if_type) {
3161         case IFT_ETHER:
3162         case IFT_FDDI:
3163         case IFT_XETHER:
3164         case IFT_ISO88025:
3165         case IFT_L2VLAN:
3166         case IFT_BRIDGE:
3167         case IFT_ARCNET:
3168         case IFT_IEEE8023ADLAG:
3169         case IFT_IEEE80211:
3170                 bcopy(lladdr, LLADDR(sdl), len);
3171                 ifa_free(ifa);
3172                 break;
3173         default:
3174                 ifa_free(ifa);
3175                 return (ENODEV);
3176         }
3177
3178         /*
3179          * If the interface is already up, we need
3180          * to re-init it in order to reprogram its
3181          * address filter.
3182          */
3183         if ((ifp->if_flags & IFF_UP) != 0) {
3184                 if (ifp->if_ioctl) {
3185                         ifp->if_flags &= ~IFF_UP;
3186                         ifr.ifr_flags = ifp->if_flags & 0xffff;
3187                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
3188                         (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3189                         ifp->if_flags |= IFF_UP;
3190                         ifr.ifr_flags = ifp->if_flags & 0xffff;
3191                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
3192                         (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3193                 }
3194 #ifdef INET
3195                 /*
3196                  * Also send gratuitous ARPs to notify other nodes about
3197                  * the address change.
3198                  */
3199                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3200                         if (ifa->ifa_addr->sa_family == AF_INET)
3201                                 arp_ifinit(ifp, ifa);
3202                 }
3203 #endif
3204         }
3205         return (0);
3206 }
3207
3208 /*
3209  * The name argument must be a pointer to storage which will last as
3210  * long as the interface does.  For physical devices, the result of
3211  * device_get_name(dev) is a good choice and for pseudo-devices a
3212  * static string works well.
3213  */
3214 void
3215 if_initname(struct ifnet *ifp, const char *name, int unit)
3216 {
3217         ifp->if_dname = name;
3218         ifp->if_dunit = unit;
3219         if (unit != IF_DUNIT_NONE)
3220                 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3221         else
3222                 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3223 }
3224
3225 int
3226 if_printf(struct ifnet *ifp, const char * fmt, ...)
3227 {
3228         va_list ap;
3229         int retval;
3230
3231         retval = printf("%s: ", ifp->if_xname);
3232         va_start(ap, fmt);
3233         retval += vprintf(fmt, ap);
3234         va_end(ap);
3235         return (retval);
3236 }
3237
3238 void
3239 if_start(struct ifnet *ifp)
3240 {
3241
3242         (*(ifp)->if_start)(ifp);
3243 }
3244
3245 /*
3246  * Backwards compatibility interface for drivers 
3247  * that have not implemented it
3248  */
3249 static int
3250 if_transmit(struct ifnet *ifp, struct mbuf *m)
3251 {
3252         int error;
3253
3254         IFQ_HANDOFF(ifp, m, error);
3255         return (error);
3256 }
3257
3258 int
3259 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3260 {
3261         int active = 0;
3262
3263         IF_LOCK(ifq);
3264         if (_IF_QFULL(ifq)) {
3265                 _IF_DROP(ifq);
3266                 IF_UNLOCK(ifq);
3267                 m_freem(m);
3268                 return (0);
3269         }
3270         if (ifp != NULL) {
3271                 ifp->if_obytes += m->m_pkthdr.len + adjust;
3272                 if (m->m_flags & (M_BCAST|M_MCAST))
3273                         ifp->if_omcasts++;
3274                 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3275         }
3276         _IF_ENQUEUE(ifq, m);
3277         IF_UNLOCK(ifq);
3278         if (ifp != NULL && !active)
3279                 (*(ifp)->if_start)(ifp);
3280         return (1);
3281 }
3282
3283 void
3284 if_register_com_alloc(u_char type,
3285     if_com_alloc_t *a, if_com_free_t *f)
3286 {
3287         
3288         KASSERT(if_com_alloc[type] == NULL,
3289             ("if_register_com_alloc: %d already registered", type));
3290         KASSERT(if_com_free[type] == NULL,
3291             ("if_register_com_alloc: %d free already registered", type));
3292
3293         if_com_alloc[type] = a;
3294         if_com_free[type] = f;
3295 }
3296
3297 void
3298 if_deregister_com_alloc(u_char type)
3299 {
3300         
3301         KASSERT(if_com_alloc[type] != NULL,
3302             ("if_deregister_com_alloc: %d not registered", type));
3303         KASSERT(if_com_free[type] != NULL,
3304             ("if_deregister_com_alloc: %d free not registered", type));
3305         if_com_alloc[type] = NULL;
3306         if_com_free[type] = NULL;
3307 }