source: rtems-libbsd/freebsd/sys/net/if.c @ 2404264

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 2404264 was 2404264, checked in by Sebastian Huber <sebastian.huber@…>, on 10/30/13 at 12:23:59

Revert superfluous changes

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