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