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