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