1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 1980, 1986, 1993 |
---|
5 | * The Regents of the University of California. All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * 3. Neither the name of the University nor the names of its contributors |
---|
16 | * may be used to endorse or promote products derived from this software |
---|
17 | * without specific prior written permission. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
29 | * SUCH DAMAGE. |
---|
30 | * |
---|
31 | * @(#)if.c 8.5 (Berkeley) 1/9/95 |
---|
32 | * $FreeBSD$ |
---|
33 | */ |
---|
34 | |
---|
35 | #include <rtems/bsd/local/opt_compat.h> |
---|
36 | #include <rtems/bsd/local/opt_inet6.h> |
---|
37 | #include <rtems/bsd/local/opt_inet.h> |
---|
38 | |
---|
39 | #include <sys/param.h> |
---|
40 | #include <sys/types.h> |
---|
41 | #include <sys/conf.h> |
---|
42 | #include <sys/malloc.h> |
---|
43 | #include <sys/sbuf.h> |
---|
44 | #include <sys/bus.h> |
---|
45 | #include <sys/mbuf.h> |
---|
46 | #include <sys/systm.h> |
---|
47 | #include <sys/priv.h> |
---|
48 | #include <sys/proc.h> |
---|
49 | #include <sys/socket.h> |
---|
50 | #include <sys/socketvar.h> |
---|
51 | #include <sys/protosw.h> |
---|
52 | #include <sys/kernel.h> |
---|
53 | #include <sys/lock.h> |
---|
54 | #include <sys/refcount.h> |
---|
55 | #include <sys/module.h> |
---|
56 | #include <sys/rwlock.h> |
---|
57 | #include <sys/sockio.h> |
---|
58 | #include <sys/syslog.h> |
---|
59 | #include <sys/sysctl.h> |
---|
60 | #include <sys/taskqueue.h> |
---|
61 | #include <sys/domain.h> |
---|
62 | #include <sys/jail.h> |
---|
63 | #include <sys/priv.h> |
---|
64 | |
---|
65 | #include <machine/stdarg.h> |
---|
66 | #include <vm/uma.h> |
---|
67 | |
---|
68 | #include <net/bpf.h> |
---|
69 | #include <net/ethernet.h> |
---|
70 | #include <net/if.h> |
---|
71 | #include <net/if_arp.h> |
---|
72 | #include <net/if_clone.h> |
---|
73 | #include <net/if_dl.h> |
---|
74 | #include <net/if_types.h> |
---|
75 | #include <net/if_var.h> |
---|
76 | #include <net/if_media.h> |
---|
77 | #include <net/if_vlan_var.h> |
---|
78 | #include <net/radix.h> |
---|
79 | #include <net/route.h> |
---|
80 | #include <net/vnet.h> |
---|
81 | |
---|
82 | #if defined(INET) || defined(INET6) |
---|
83 | #include <net/ethernet.h> |
---|
84 | #include <netinet/in.h> |
---|
85 | #include <netinet/in_var.h> |
---|
86 | #include <netinet/ip.h> |
---|
87 | #include <netinet/ip_carp.h> |
---|
88 | #ifdef INET |
---|
89 | #include <netinet/if_ether.h> |
---|
90 | #endif /* INET */ |
---|
91 | #ifdef INET6 |
---|
92 | #include <netinet6/in6_var.h> |
---|
93 | #include <netinet6/in6_ifattach.h> |
---|
94 | #endif /* INET6 */ |
---|
95 | #endif /* INET || INET6 */ |
---|
96 | |
---|
97 | #include <security/mac/mac_framework.h> |
---|
98 | |
---|
99 | #ifdef COMPAT_FREEBSD32 |
---|
100 | #include <sys/mount.h> |
---|
101 | #include <compat/freebsd32/freebsd32.h> |
---|
102 | #endif |
---|
103 | |
---|
104 | SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); |
---|
105 | SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); |
---|
106 | |
---|
107 | SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN, |
---|
108 | &ifqmaxlen, 0, "max send queue size"); |
---|
109 | |
---|
110 | /* Log link state change events */ |
---|
111 | static int log_link_state_change = 1; |
---|
112 | |
---|
113 | SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, |
---|
114 | &log_link_state_change, 0, |
---|
115 | "log interface link state change events"); |
---|
116 | |
---|
117 | /* Log promiscuous mode change events */ |
---|
118 | static int log_promisc_mode_change = 1; |
---|
119 | |
---|
120 | SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN, |
---|
121 | &log_promisc_mode_change, 1, |
---|
122 | "log promiscuous mode change events"); |
---|
123 | |
---|
124 | /* Interface description */ |
---|
125 | static unsigned int ifdescr_maxlen = 1024; |
---|
126 | SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW, |
---|
127 | &ifdescr_maxlen, 0, |
---|
128 | "administrative maximum length for interface description"); |
---|
129 | |
---|
130 | static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions"); |
---|
131 | |
---|
132 | /* global sx for non-critical path ifdescr */ |
---|
133 | static struct sx ifdescr_sx; |
---|
134 | SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr"); |
---|
135 | |
---|
136 | void (*bridge_linkstate_p)(struct ifnet *ifp); |
---|
137 | void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); |
---|
138 | void (*lagg_linkstate_p)(struct ifnet *ifp, int state); |
---|
139 | /* These are external hooks for CARP. */ |
---|
140 | void (*carp_linkstate_p)(struct ifnet *ifp); |
---|
141 | void (*carp_demote_adj_p)(int, char *); |
---|
142 | int (*carp_master_p)(struct ifaddr *); |
---|
143 | #if defined(INET) || defined(INET6) |
---|
144 | int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost); |
---|
145 | int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m, |
---|
146 | const struct sockaddr *sa); |
---|
147 | int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *); |
---|
148 | int (*carp_attach_p)(struct ifaddr *, int); |
---|
149 | void (*carp_detach_p)(struct ifaddr *, bool); |
---|
150 | #endif |
---|
151 | #ifdef INET |
---|
152 | int (*carp_iamatch_p)(struct ifaddr *, uint8_t **); |
---|
153 | #endif |
---|
154 | #ifdef INET6 |
---|
155 | struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6); |
---|
156 | caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m, |
---|
157 | const struct in6_addr *taddr); |
---|
158 | #endif |
---|
159 | |
---|
160 | struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; |
---|
161 | |
---|
162 | /* |
---|
163 | * XXX: Style; these should be sorted alphabetically, and unprototyped |
---|
164 | * static functions should be prototyped. Currently they are sorted by |
---|
165 | * declaration order. |
---|
166 | */ |
---|
167 | static void if_attachdomain(void *); |
---|
168 | static void if_attachdomain1(struct ifnet *); |
---|
169 | static int ifconf(u_long, caddr_t); |
---|
170 | static void if_freemulti(struct ifmultiaddr *); |
---|
171 | static void if_grow(void); |
---|
172 | static void if_input_default(struct ifnet *, struct mbuf *); |
---|
173 | static int if_requestencap_default(struct ifnet *, struct if_encap_req *); |
---|
174 | static void if_route(struct ifnet *, int flag, int fam); |
---|
175 | static int if_setflag(struct ifnet *, int, int, int *, int); |
---|
176 | static int if_transmit(struct ifnet *ifp, struct mbuf *m); |
---|
177 | static void if_unroute(struct ifnet *, int flag, int fam); |
---|
178 | static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); |
---|
179 | static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); |
---|
180 | static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); |
---|
181 | static void do_link_state_change(void *, int); |
---|
182 | static int if_getgroup(struct ifgroupreq *, struct ifnet *); |
---|
183 | static int if_getgroupmembers(struct ifgroupreq *); |
---|
184 | static void if_delgroups(struct ifnet *); |
---|
185 | static void if_attach_internal(struct ifnet *, int, struct if_clone *); |
---|
186 | static int if_detach_internal(struct ifnet *, int, struct if_clone **); |
---|
187 | #ifdef VIMAGE |
---|
188 | static void if_vmove(struct ifnet *, struct vnet *); |
---|
189 | #endif |
---|
190 | |
---|
191 | #ifdef INET6 |
---|
192 | /* |
---|
193 | * XXX: declare here to avoid to include many inet6 related files.. |
---|
194 | * should be more generalized? |
---|
195 | */ |
---|
196 | extern void nd6_setmtu(struct ifnet *); |
---|
197 | #endif |
---|
198 | |
---|
199 | /* ipsec helper hooks */ |
---|
200 | VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]); |
---|
201 | VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]); |
---|
202 | |
---|
203 | VNET_DEFINE(int, if_index); |
---|
204 | int ifqmaxlen = IFQ_MAXLEN; |
---|
205 | VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */ |
---|
206 | VNET_DEFINE(struct ifgrouphead, ifg_head); |
---|
207 | |
---|
208 | static VNET_DEFINE(int, if_indexlim) = 8; |
---|
209 | |
---|
210 | /* Table of ifnet by index. */ |
---|
211 | VNET_DEFINE(struct ifnet **, ifindex_table); |
---|
212 | |
---|
213 | #define V_if_indexlim VNET(if_indexlim) |
---|
214 | #define V_ifindex_table VNET(ifindex_table) |
---|
215 | |
---|
216 | /* |
---|
217 | * The global network interface list (V_ifnet) and related state (such as |
---|
218 | * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and |
---|
219 | * an rwlock. Either may be acquired shared to stablize the list, but both |
---|
220 | * must be acquired writable to modify the list. This model allows us to |
---|
221 | * both stablize the interface list during interrupt thread processing, but |
---|
222 | * also to stablize it over long-running ioctls, without introducing priority |
---|
223 | * inversions and deadlocks. |
---|
224 | */ |
---|
225 | struct rwlock ifnet_rwlock; |
---|
226 | RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE); |
---|
227 | struct sx ifnet_sxlock; |
---|
228 | SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE); |
---|
229 | |
---|
230 | /* |
---|
231 | * The allocation of network interfaces is a rather non-atomic affair; we |
---|
232 | * need to select an index before we are ready to expose the interface for |
---|
233 | * use, so will use this pointer value to indicate reservation. |
---|
234 | */ |
---|
235 | #define IFNET_HOLD (void *)(uintptr_t)(-1) |
---|
236 | |
---|
237 | static if_com_alloc_t *if_com_alloc[256]; |
---|
238 | static if_com_free_t *if_com_free[256]; |
---|
239 | |
---|
240 | static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); |
---|
241 | MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); |
---|
242 | MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); |
---|
243 | |
---|
244 | struct ifnet * |
---|
245 | ifnet_byindex_locked(u_short idx) |
---|
246 | { |
---|
247 | |
---|
248 | if (idx > V_if_index) |
---|
249 | return (NULL); |
---|
250 | if (V_ifindex_table[idx] == IFNET_HOLD) |
---|
251 | return (NULL); |
---|
252 | return (V_ifindex_table[idx]); |
---|
253 | } |
---|
254 | |
---|
255 | struct ifnet * |
---|
256 | ifnet_byindex(u_short idx) |
---|
257 | { |
---|
258 | struct ifnet *ifp; |
---|
259 | |
---|
260 | IFNET_RLOCK_NOSLEEP(); |
---|
261 | ifp = ifnet_byindex_locked(idx); |
---|
262 | IFNET_RUNLOCK_NOSLEEP(); |
---|
263 | return (ifp); |
---|
264 | } |
---|
265 | |
---|
266 | struct ifnet * |
---|
267 | ifnet_byindex_ref(u_short idx) |
---|
268 | { |
---|
269 | struct ifnet *ifp; |
---|
270 | |
---|
271 | IFNET_RLOCK_NOSLEEP(); |
---|
272 | ifp = ifnet_byindex_locked(idx); |
---|
273 | if (ifp == NULL || (ifp->if_flags & IFF_DYING)) { |
---|
274 | IFNET_RUNLOCK_NOSLEEP(); |
---|
275 | return (NULL); |
---|
276 | } |
---|
277 | if_ref(ifp); |
---|
278 | IFNET_RUNLOCK_NOSLEEP(); |
---|
279 | return (ifp); |
---|
280 | } |
---|
281 | |
---|
282 | /* |
---|
283 | * Allocate an ifindex array entry; return 0 on success or an error on |
---|
284 | * failure. |
---|
285 | */ |
---|
286 | static u_short |
---|
287 | ifindex_alloc(void) |
---|
288 | { |
---|
289 | u_short idx; |
---|
290 | |
---|
291 | IFNET_WLOCK_ASSERT(); |
---|
292 | retry: |
---|
293 | /* |
---|
294 | * Try to find an empty slot below V_if_index. If we fail, take the |
---|
295 | * next slot. |
---|
296 | */ |
---|
297 | for (idx = 1; idx <= V_if_index; idx++) { |
---|
298 | if (V_ifindex_table[idx] == NULL) |
---|
299 | break; |
---|
300 | } |
---|
301 | |
---|
302 | /* Catch if_index overflow. */ |
---|
303 | if (idx >= V_if_indexlim) { |
---|
304 | if_grow(); |
---|
305 | goto retry; |
---|
306 | } |
---|
307 | if (idx > V_if_index) |
---|
308 | V_if_index = idx; |
---|
309 | return (idx); |
---|
310 | } |
---|
311 | |
---|
312 | static void |
---|
313 | ifindex_free_locked(u_short idx) |
---|
314 | { |
---|
315 | |
---|
316 | IFNET_WLOCK_ASSERT(); |
---|
317 | |
---|
318 | V_ifindex_table[idx] = NULL; |
---|
319 | while (V_if_index > 0 && |
---|
320 | V_ifindex_table[V_if_index] == NULL) |
---|
321 | V_if_index--; |
---|
322 | } |
---|
323 | |
---|
324 | static void |
---|
325 | ifindex_free(u_short idx) |
---|
326 | { |
---|
327 | |
---|
328 | IFNET_WLOCK(); |
---|
329 | ifindex_free_locked(idx); |
---|
330 | IFNET_WUNLOCK(); |
---|
331 | } |
---|
332 | |
---|
333 | static void |
---|
334 | ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp) |
---|
335 | { |
---|
336 | |
---|
337 | IFNET_WLOCK_ASSERT(); |
---|
338 | |
---|
339 | V_ifindex_table[idx] = ifp; |
---|
340 | } |
---|
341 | |
---|
342 | static void |
---|
343 | ifnet_setbyindex(u_short idx, struct ifnet *ifp) |
---|
344 | { |
---|
345 | |
---|
346 | IFNET_WLOCK(); |
---|
347 | ifnet_setbyindex_locked(idx, ifp); |
---|
348 | IFNET_WUNLOCK(); |
---|
349 | } |
---|
350 | |
---|
351 | struct ifaddr * |
---|
352 | ifaddr_byindex(u_short idx) |
---|
353 | { |
---|
354 | struct ifnet *ifp; |
---|
355 | struct ifaddr *ifa = NULL; |
---|
356 | |
---|
357 | IFNET_RLOCK_NOSLEEP(); |
---|
358 | ifp = ifnet_byindex_locked(idx); |
---|
359 | if (ifp != NULL && (ifa = ifp->if_addr) != NULL) |
---|
360 | ifa_ref(ifa); |
---|
361 | IFNET_RUNLOCK_NOSLEEP(); |
---|
362 | return (ifa); |
---|
363 | } |
---|
364 | |
---|
365 | /* |
---|
366 | * Network interface utility routines. |
---|
367 | * |
---|
368 | * Routines with ifa_ifwith* names take sockaddr *'s as |
---|
369 | * parameters. |
---|
370 | */ |
---|
371 | |
---|
372 | static void |
---|
373 | vnet_if_init(const void *unused __unused) |
---|
374 | { |
---|
375 | |
---|
376 | TAILQ_INIT(&V_ifnet); |
---|
377 | TAILQ_INIT(&V_ifg_head); |
---|
378 | IFNET_WLOCK(); |
---|
379 | if_grow(); /* create initial table */ |
---|
380 | IFNET_WUNLOCK(); |
---|
381 | vnet_if_clone_init(); |
---|
382 | } |
---|
383 | VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init, |
---|
384 | NULL); |
---|
385 | |
---|
386 | #ifdef VIMAGE |
---|
387 | static void |
---|
388 | vnet_if_uninit(const void *unused __unused) |
---|
389 | { |
---|
390 | |
---|
391 | VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p " |
---|
392 | "not empty", __func__, __LINE__, &V_ifnet)); |
---|
393 | VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p " |
---|
394 | "not empty", __func__, __LINE__, &V_ifg_head)); |
---|
395 | |
---|
396 | free((caddr_t)V_ifindex_table, M_IFNET); |
---|
397 | } |
---|
398 | VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, |
---|
399 | vnet_if_uninit, NULL); |
---|
400 | |
---|
401 | static void |
---|
402 | vnet_if_return(const void *unused __unused) |
---|
403 | { |
---|
404 | struct ifnet *ifp, *nifp; |
---|
405 | |
---|
406 | /* Return all inherited interfaces to their parent vnets. */ |
---|
407 | TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) { |
---|
408 | if (ifp->if_home_vnet != ifp->if_vnet) |
---|
409 | if_vmove(ifp, ifp->if_home_vnet); |
---|
410 | } |
---|
411 | } |
---|
412 | VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY, |
---|
413 | vnet_if_return, NULL); |
---|
414 | #endif |
---|
415 | |
---|
416 | static void |
---|
417 | if_grow(void) |
---|
418 | { |
---|
419 | int oldlim; |
---|
420 | u_int n; |
---|
421 | struct ifnet **e; |
---|
422 | |
---|
423 | IFNET_WLOCK_ASSERT(); |
---|
424 | oldlim = V_if_indexlim; |
---|
425 | IFNET_WUNLOCK(); |
---|
426 | n = (oldlim << 1) * sizeof(*e); |
---|
427 | e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); |
---|
428 | IFNET_WLOCK(); |
---|
429 | if (V_if_indexlim != oldlim) { |
---|
430 | free(e, M_IFNET); |
---|
431 | return; |
---|
432 | } |
---|
433 | if (V_ifindex_table != NULL) { |
---|
434 | memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); |
---|
435 | free((caddr_t)V_ifindex_table, M_IFNET); |
---|
436 | } |
---|
437 | V_if_indexlim <<= 1; |
---|
438 | V_ifindex_table = e; |
---|
439 | } |
---|
440 | |
---|
441 | /* |
---|
442 | * Allocate a struct ifnet and an index for an interface. A layer 2 |
---|
443 | * common structure will also be allocated if an allocation routine is |
---|
444 | * registered for the passed type. |
---|
445 | */ |
---|
446 | struct ifnet * |
---|
447 | if_alloc(u_char type) |
---|
448 | { |
---|
449 | struct ifnet *ifp; |
---|
450 | u_short idx; |
---|
451 | |
---|
452 | ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); |
---|
453 | IFNET_WLOCK(); |
---|
454 | idx = ifindex_alloc(); |
---|
455 | ifnet_setbyindex_locked(idx, IFNET_HOLD); |
---|
456 | IFNET_WUNLOCK(); |
---|
457 | ifp->if_index = idx; |
---|
458 | ifp->if_type = type; |
---|
459 | ifp->if_alloctype = type; |
---|
460 | #ifdef VIMAGE |
---|
461 | ifp->if_vnet = curvnet; |
---|
462 | #endif |
---|
463 | if (if_com_alloc[type] != NULL) { |
---|
464 | ifp->if_l2com = if_com_alloc[type](type, ifp); |
---|
465 | if (ifp->if_l2com == NULL) { |
---|
466 | free(ifp, M_IFNET); |
---|
467 | ifindex_free(idx); |
---|
468 | return (NULL); |
---|
469 | } |
---|
470 | } |
---|
471 | |
---|
472 | IF_ADDR_LOCK_INIT(ifp); |
---|
473 | TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); |
---|
474 | ifp->if_afdata_initialized = 0; |
---|
475 | IF_AFDATA_LOCK_INIT(ifp); |
---|
476 | TAILQ_INIT(&ifp->if_addrhead); |
---|
477 | TAILQ_INIT(&ifp->if_multiaddrs); |
---|
478 | TAILQ_INIT(&ifp->if_groups); |
---|
479 | #ifdef MAC |
---|
480 | mac_ifnet_init(ifp); |
---|
481 | #endif |
---|
482 | ifq_init(&ifp->if_snd, ifp); |
---|
483 | |
---|
484 | refcount_init(&ifp->if_refcount, 1); /* Index reference. */ |
---|
485 | for (int i = 0; i < IFCOUNTERS; i++) |
---|
486 | ifp->if_counters[i] = counter_u64_alloc(M_WAITOK); |
---|
487 | ifp->if_get_counter = if_get_counter_default; |
---|
488 | ifnet_setbyindex(ifp->if_index, ifp); |
---|
489 | return (ifp); |
---|
490 | } |
---|
491 | |
---|
492 | /* |
---|
493 | * Do the actual work of freeing a struct ifnet, and layer 2 common |
---|
494 | * structure. This call is made when the last reference to an |
---|
495 | * interface is released. |
---|
496 | */ |
---|
497 | static void |
---|
498 | if_free_internal(struct ifnet *ifp) |
---|
499 | { |
---|
500 | |
---|
501 | KASSERT((ifp->if_flags & IFF_DYING), |
---|
502 | ("if_free_internal: interface not dying")); |
---|
503 | |
---|
504 | if (if_com_free[ifp->if_alloctype] != NULL) |
---|
505 | if_com_free[ifp->if_alloctype](ifp->if_l2com, |
---|
506 | ifp->if_alloctype); |
---|
507 | |
---|
508 | #ifdef MAC |
---|
509 | mac_ifnet_destroy(ifp); |
---|
510 | #endif /* MAC */ |
---|
511 | if (ifp->if_description != NULL) |
---|
512 | free(ifp->if_description, M_IFDESCR); |
---|
513 | IF_AFDATA_DESTROY(ifp); |
---|
514 | IF_ADDR_LOCK_DESTROY(ifp); |
---|
515 | ifq_delete(&ifp->if_snd); |
---|
516 | |
---|
517 | for (int i = 0; i < IFCOUNTERS; i++) |
---|
518 | counter_u64_free(ifp->if_counters[i]); |
---|
519 | |
---|
520 | free(ifp, M_IFNET); |
---|
521 | } |
---|
522 | |
---|
523 | /* |
---|
524 | * Deregister an interface and free the associated storage. |
---|
525 | */ |
---|
526 | void |
---|
527 | if_free(struct ifnet *ifp) |
---|
528 | { |
---|
529 | |
---|
530 | ifp->if_flags |= IFF_DYING; /* XXX: Locking */ |
---|
531 | |
---|
532 | CURVNET_SET_QUIET(ifp->if_vnet); |
---|
533 | IFNET_WLOCK(); |
---|
534 | KASSERT(ifp == ifnet_byindex_locked(ifp->if_index), |
---|
535 | ("%s: freeing unallocated ifnet", ifp->if_xname)); |
---|
536 | |
---|
537 | ifindex_free_locked(ifp->if_index); |
---|
538 | IFNET_WUNLOCK(); |
---|
539 | |
---|
540 | if (refcount_release(&ifp->if_refcount)) |
---|
541 | if_free_internal(ifp); |
---|
542 | CURVNET_RESTORE(); |
---|
543 | } |
---|
544 | |
---|
545 | /* |
---|
546 | * Interfaces to keep an ifnet type-stable despite the possibility of the |
---|
547 | * driver calling if_free(). If there are additional references, we defer |
---|
548 | * freeing the underlying data structure. |
---|
549 | */ |
---|
550 | void |
---|
551 | if_ref(struct ifnet *ifp) |
---|
552 | { |
---|
553 | |
---|
554 | /* We don't assert the ifnet list lock here, but arguably should. */ |
---|
555 | refcount_acquire(&ifp->if_refcount); |
---|
556 | } |
---|
557 | |
---|
558 | void |
---|
559 | if_rele(struct ifnet *ifp) |
---|
560 | { |
---|
561 | |
---|
562 | if (!refcount_release(&ifp->if_refcount)) |
---|
563 | return; |
---|
564 | if_free_internal(ifp); |
---|
565 | } |
---|
566 | |
---|
567 | void |
---|
568 | ifq_init(struct ifaltq *ifq, struct ifnet *ifp) |
---|
569 | { |
---|
570 | |
---|
571 | mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); |
---|
572 | |
---|
573 | if (ifq->ifq_maxlen == 0) |
---|
574 | ifq->ifq_maxlen = ifqmaxlen; |
---|
575 | |
---|
576 | ifq->altq_type = 0; |
---|
577 | ifq->altq_disc = NULL; |
---|
578 | ifq->altq_flags &= ALTQF_CANTCHANGE; |
---|
579 | ifq->altq_tbr = NULL; |
---|
580 | ifq->altq_ifp = ifp; |
---|
581 | } |
---|
582 | |
---|
583 | void |
---|
584 | ifq_delete(struct ifaltq *ifq) |
---|
585 | { |
---|
586 | mtx_destroy(&ifq->ifq_mtx); |
---|
587 | } |
---|
588 | |
---|
589 | /* |
---|
590 | * Perform generic interface initialization tasks and attach the interface |
---|
591 | * to the list of "active" interfaces. If vmove flag is set on entry |
---|
592 | * to if_attach_internal(), perform only a limited subset of initialization |
---|
593 | * tasks, given that we are moving from one vnet to another an ifnet which |
---|
594 | * has already been fully initialized. |
---|
595 | * |
---|
596 | * Note that if_detach_internal() removes group membership unconditionally |
---|
597 | * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL. |
---|
598 | * Thus, when if_vmove() is applied to a cloned interface, group membership |
---|
599 | * is lost while a cloned one always joins a group whose name is |
---|
600 | * ifc->ifc_name. To recover this after if_detach_internal() and |
---|
601 | * if_attach_internal(), the cloner should be specified to |
---|
602 | * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal() |
---|
603 | * attempts to join a group whose name is ifc->ifc_name. |
---|
604 | * |
---|
605 | * XXX: |
---|
606 | * - The decision to return void and thus require this function to |
---|
607 | * succeed is questionable. |
---|
608 | * - We should probably do more sanity checking. For instance we don't |
---|
609 | * do anything to insure if_xname is unique or non-empty. |
---|
610 | */ |
---|
611 | void |
---|
612 | if_attach(struct ifnet *ifp) |
---|
613 | { |
---|
614 | |
---|
615 | if_attach_internal(ifp, 0, NULL); |
---|
616 | } |
---|
617 | |
---|
618 | /* |
---|
619 | * Compute the least common TSO limit. |
---|
620 | */ |
---|
621 | void |
---|
622 | if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax) |
---|
623 | { |
---|
624 | /* |
---|
625 | * 1) If there is no limit currently, take the limit from |
---|
626 | * the network adapter. |
---|
627 | * |
---|
628 | * 2) If the network adapter has a limit below the current |
---|
629 | * limit, apply it. |
---|
630 | */ |
---|
631 | if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 && |
---|
632 | ifp->if_hw_tsomax < pmax->tsomaxbytes)) { |
---|
633 | pmax->tsomaxbytes = ifp->if_hw_tsomax; |
---|
634 | } |
---|
635 | if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 && |
---|
636 | ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) { |
---|
637 | pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount; |
---|
638 | } |
---|
639 | if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 && |
---|
640 | ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) { |
---|
641 | pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize; |
---|
642 | } |
---|
643 | } |
---|
644 | |
---|
645 | /* |
---|
646 | * Update TSO limit of a network adapter. |
---|
647 | * |
---|
648 | * Returns zero if no change. Else non-zero. |
---|
649 | */ |
---|
650 | int |
---|
651 | if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax) |
---|
652 | { |
---|
653 | int retval = 0; |
---|
654 | if (ifp->if_hw_tsomax != pmax->tsomaxbytes) { |
---|
655 | ifp->if_hw_tsomax = pmax->tsomaxbytes; |
---|
656 | retval++; |
---|
657 | } |
---|
658 | if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) { |
---|
659 | ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize; |
---|
660 | retval++; |
---|
661 | } |
---|
662 | if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) { |
---|
663 | ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount; |
---|
664 | retval++; |
---|
665 | } |
---|
666 | return (retval); |
---|
667 | } |
---|
668 | |
---|
669 | static void |
---|
670 | if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc) |
---|
671 | { |
---|
672 | unsigned socksize, ifasize; |
---|
673 | int namelen, masklen; |
---|
674 | struct sockaddr_dl *sdl; |
---|
675 | struct ifaddr *ifa; |
---|
676 | |
---|
677 | if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) |
---|
678 | panic ("%s: BUG: if_attach called without if_alloc'd input()\n", |
---|
679 | ifp->if_xname); |
---|
680 | |
---|
681 | #ifdef VIMAGE |
---|
682 | ifp->if_vnet = curvnet; |
---|
683 | if (ifp->if_home_vnet == NULL) |
---|
684 | ifp->if_home_vnet = curvnet; |
---|
685 | #endif |
---|
686 | |
---|
687 | if_addgroup(ifp, IFG_ALL); |
---|
688 | |
---|
689 | /* Restore group membership for cloned interfaces. */ |
---|
690 | if (vmove && ifc != NULL) |
---|
691 | if_clone_addgroup(ifp, ifc); |
---|
692 | |
---|
693 | getmicrotime(&ifp->if_lastchange); |
---|
694 | ifp->if_epoch = time_uptime; |
---|
695 | |
---|
696 | KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) || |
---|
697 | (ifp->if_transmit != NULL && ifp->if_qflush != NULL), |
---|
698 | ("transmit and qflush must both either be set or both be NULL")); |
---|
699 | if (ifp->if_transmit == NULL) { |
---|
700 | ifp->if_transmit = if_transmit; |
---|
701 | ifp->if_qflush = if_qflush; |
---|
702 | } |
---|
703 | if (ifp->if_input == NULL) |
---|
704 | ifp->if_input = if_input_default; |
---|
705 | |
---|
706 | if (ifp->if_requestencap == NULL) |
---|
707 | ifp->if_requestencap = if_requestencap_default; |
---|
708 | |
---|
709 | if (!vmove) { |
---|
710 | #ifdef MAC |
---|
711 | mac_ifnet_create(ifp); |
---|
712 | #endif |
---|
713 | |
---|
714 | /* |
---|
715 | * Create a Link Level name for this device. |
---|
716 | */ |
---|
717 | namelen = strlen(ifp->if_xname); |
---|
718 | /* |
---|
719 | * Always save enough space for any possiable name so we |
---|
720 | * can do a rename in place later. |
---|
721 | */ |
---|
722 | masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; |
---|
723 | socksize = masklen + ifp->if_addrlen; |
---|
724 | if (socksize < sizeof(*sdl)) |
---|
725 | socksize = sizeof(*sdl); |
---|
726 | socksize = roundup2(socksize, sizeof(long)); |
---|
727 | ifasize = sizeof(*ifa) + 2 * socksize; |
---|
728 | ifa = ifa_alloc(ifasize, M_WAITOK); |
---|
729 | sdl = (struct sockaddr_dl *)(ifa + 1); |
---|
730 | sdl->sdl_len = socksize; |
---|
731 | sdl->sdl_family = AF_LINK; |
---|
732 | bcopy(ifp->if_xname, sdl->sdl_data, namelen); |
---|
733 | sdl->sdl_nlen = namelen; |
---|
734 | sdl->sdl_index = ifp->if_index; |
---|
735 | sdl->sdl_type = ifp->if_type; |
---|
736 | ifp->if_addr = ifa; |
---|
737 | ifa->ifa_ifp = ifp; |
---|
738 | ifa->ifa_rtrequest = link_rtrequest; |
---|
739 | ifa->ifa_addr = (struct sockaddr *)sdl; |
---|
740 | sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); |
---|
741 | ifa->ifa_netmask = (struct sockaddr *)sdl; |
---|
742 | sdl->sdl_len = masklen; |
---|
743 | while (namelen != 0) |
---|
744 | sdl->sdl_data[--namelen] = 0xff; |
---|
745 | TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); |
---|
746 | /* Reliably crash if used uninitialized. */ |
---|
747 | ifp->if_broadcastaddr = NULL; |
---|
748 | |
---|
749 | #if defined(INET) || defined(INET6) |
---|
750 | /* Use defaults for TSO, if nothing is set */ |
---|
751 | if (ifp->if_hw_tsomax == 0 && |
---|
752 | ifp->if_hw_tsomaxsegcount == 0 && |
---|
753 | ifp->if_hw_tsomaxsegsize == 0) { |
---|
754 | /* |
---|
755 | * The TSO defaults needs to be such that an |
---|
756 | * NFS mbuf list of 35 mbufs totalling just |
---|
757 | * below 64K works and that a chain of mbufs |
---|
758 | * can be defragged into at most 32 segments: |
---|
759 | */ |
---|
760 | ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) - |
---|
761 | (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN)); |
---|
762 | ifp->if_hw_tsomaxsegcount = 35; |
---|
763 | ifp->if_hw_tsomaxsegsize = 2048; /* 2K */ |
---|
764 | |
---|
765 | /* XXX some drivers set IFCAP_TSO after ethernet attach */ |
---|
766 | if (ifp->if_capabilities & IFCAP_TSO) { |
---|
767 | if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n", |
---|
768 | ifp->if_hw_tsomax, |
---|
769 | ifp->if_hw_tsomaxsegcount, |
---|
770 | ifp->if_hw_tsomaxsegsize); |
---|
771 | } |
---|
772 | } |
---|
773 | #endif |
---|
774 | } |
---|
775 | #ifdef VIMAGE |
---|
776 | else { |
---|
777 | /* |
---|
778 | * Update the interface index in the link layer address |
---|
779 | * of the interface. |
---|
780 | */ |
---|
781 | for (ifa = ifp->if_addr; ifa != NULL; |
---|
782 | ifa = TAILQ_NEXT(ifa, ifa_link)) { |
---|
783 | if (ifa->ifa_addr->sa_family == AF_LINK) { |
---|
784 | sdl = (struct sockaddr_dl *)ifa->ifa_addr; |
---|
785 | sdl->sdl_index = ifp->if_index; |
---|
786 | } |
---|
787 | } |
---|
788 | } |
---|
789 | #endif |
---|
790 | |
---|
791 | IFNET_WLOCK(); |
---|
792 | TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); |
---|
793 | #ifdef VIMAGE |
---|
794 | curvnet->vnet_ifcnt++; |
---|
795 | #endif |
---|
796 | IFNET_WUNLOCK(); |
---|
797 | |
---|
798 | if (domain_init_status >= 2) |
---|
799 | if_attachdomain1(ifp); |
---|
800 | |
---|
801 | EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); |
---|
802 | if (IS_DEFAULT_VNET(curvnet)) |
---|
803 | devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); |
---|
804 | |
---|
805 | /* Announce the interface. */ |
---|
806 | rt_ifannouncemsg(ifp, IFAN_ARRIVAL); |
---|
807 | } |
---|
808 | |
---|
809 | static void |
---|
810 | if_attachdomain(void *dummy) |
---|
811 | { |
---|
812 | struct ifnet *ifp; |
---|
813 | |
---|
814 | TAILQ_FOREACH(ifp, &V_ifnet, if_link) |
---|
815 | if_attachdomain1(ifp); |
---|
816 | } |
---|
817 | SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, |
---|
818 | if_attachdomain, NULL); |
---|
819 | |
---|
820 | static void |
---|
821 | if_attachdomain1(struct ifnet *ifp) |
---|
822 | { |
---|
823 | struct domain *dp; |
---|
824 | |
---|
825 | /* |
---|
826 | * Since dp->dom_ifattach calls malloc() with M_WAITOK, we |
---|
827 | * cannot lock ifp->if_afdata initialization, entirely. |
---|
828 | */ |
---|
829 | IF_AFDATA_LOCK(ifp); |
---|
830 | if (ifp->if_afdata_initialized >= domain_init_status) { |
---|
831 | IF_AFDATA_UNLOCK(ifp); |
---|
832 | log(LOG_WARNING, "%s called more than once on %s\n", |
---|
833 | __func__, ifp->if_xname); |
---|
834 | return; |
---|
835 | } |
---|
836 | ifp->if_afdata_initialized = domain_init_status; |
---|
837 | IF_AFDATA_UNLOCK(ifp); |
---|
838 | |
---|
839 | /* address family dependent data region */ |
---|
840 | bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); |
---|
841 | for (dp = domains; dp; dp = dp->dom_next) { |
---|
842 | if (dp->dom_ifattach) |
---|
843 | ifp->if_afdata[dp->dom_family] = |
---|
844 | (*dp->dom_ifattach)(ifp); |
---|
845 | } |
---|
846 | } |
---|
847 | |
---|
848 | /* |
---|
849 | * Remove any unicast or broadcast network addresses from an interface. |
---|
850 | */ |
---|
851 | void |
---|
852 | if_purgeaddrs(struct ifnet *ifp) |
---|
853 | { |
---|
854 | struct ifaddr *ifa, *next; |
---|
855 | |
---|
856 | /* XXX cannot hold IF_ADDR_WLOCK over called functions. */ |
---|
857 | TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { |
---|
858 | if (ifa->ifa_addr->sa_family == AF_LINK) |
---|
859 | continue; |
---|
860 | #ifdef INET |
---|
861 | /* XXX: Ugly!! ad hoc just for INET */ |
---|
862 | if (ifa->ifa_addr->sa_family == AF_INET) { |
---|
863 | struct ifaliasreq ifr; |
---|
864 | |
---|
865 | bzero(&ifr, sizeof(ifr)); |
---|
866 | ifr.ifra_addr = *ifa->ifa_addr; |
---|
867 | if (ifa->ifa_dstaddr) |
---|
868 | ifr.ifra_broadaddr = *ifa->ifa_dstaddr; |
---|
869 | if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, |
---|
870 | NULL) == 0) |
---|
871 | continue; |
---|
872 | } |
---|
873 | #endif /* INET */ |
---|
874 | #ifdef INET6 |
---|
875 | if (ifa->ifa_addr->sa_family == AF_INET6) { |
---|
876 | in6_purgeaddr(ifa); |
---|
877 | /* ifp_addrhead is already updated */ |
---|
878 | continue; |
---|
879 | } |
---|
880 | #endif /* INET6 */ |
---|
881 | IF_ADDR_WLOCK(ifp); |
---|
882 | TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); |
---|
883 | IF_ADDR_WUNLOCK(ifp); |
---|
884 | ifa_free(ifa); |
---|
885 | } |
---|
886 | } |
---|
887 | |
---|
888 | /* |
---|
889 | * Remove any multicast network addresses from an interface when an ifnet |
---|
890 | * is going away. |
---|
891 | */ |
---|
892 | static void |
---|
893 | if_purgemaddrs(struct ifnet *ifp) |
---|
894 | { |
---|
895 | struct ifmultiaddr *ifma; |
---|
896 | struct ifmultiaddr *next; |
---|
897 | |
---|
898 | IF_ADDR_WLOCK(ifp); |
---|
899 | TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) |
---|
900 | if_delmulti_locked(ifp, ifma, 1); |
---|
901 | IF_ADDR_WUNLOCK(ifp); |
---|
902 | } |
---|
903 | |
---|
904 | /* |
---|
905 | * Detach an interface, removing it from the list of "active" interfaces. |
---|
906 | * If vmove flag is set on entry to if_detach_internal(), perform only a |
---|
907 | * limited subset of cleanup tasks, given that we are moving an ifnet from |
---|
908 | * one vnet to another, where it must be fully operational. |
---|
909 | * |
---|
910 | * XXXRW: There are some significant questions about event ordering, and |
---|
911 | * how to prevent things from starting to use the interface during detach. |
---|
912 | */ |
---|
913 | void |
---|
914 | if_detach(struct ifnet *ifp) |
---|
915 | { |
---|
916 | |
---|
917 | CURVNET_SET_QUIET(ifp->if_vnet); |
---|
918 | if_detach_internal(ifp, 0, NULL); |
---|
919 | CURVNET_RESTORE(); |
---|
920 | } |
---|
921 | |
---|
922 | /* |
---|
923 | * The vmove flag, if set, indicates that we are called from a callpath |
---|
924 | * that is moving an interface to a different vnet instance. |
---|
925 | * |
---|
926 | * The shutdown flag, if set, indicates that we are called in the |
---|
927 | * process of shutting down a vnet instance. Currently only the |
---|
928 | * vnet_if_return SYSUNINIT function sets it. Note: we can be called |
---|
929 | * on a vnet instance shutdown without this flag being set, e.g., when |
---|
930 | * the cloned interfaces are destoyed as first thing of teardown. |
---|
931 | */ |
---|
932 | static int |
---|
933 | if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp) |
---|
934 | { |
---|
935 | struct ifaddr *ifa; |
---|
936 | int i; |
---|
937 | struct domain *dp; |
---|
938 | struct ifnet *iter; |
---|
939 | int found = 0; |
---|
940 | #ifdef VIMAGE |
---|
941 | int shutdown; |
---|
942 | |
---|
943 | shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET && |
---|
944 | ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; |
---|
945 | #endif |
---|
946 | IFNET_WLOCK(); |
---|
947 | TAILQ_FOREACH(iter, &V_ifnet, if_link) |
---|
948 | if (iter == ifp) { |
---|
949 | TAILQ_REMOVE(&V_ifnet, ifp, if_link); |
---|
950 | found = 1; |
---|
951 | break; |
---|
952 | } |
---|
953 | IFNET_WUNLOCK(); |
---|
954 | if (!found) { |
---|
955 | /* |
---|
956 | * While we would want to panic here, we cannot |
---|
957 | * guarantee that the interface is indeed still on |
---|
958 | * the list given we don't hold locks all the way. |
---|
959 | */ |
---|
960 | return (ENOENT); |
---|
961 | #if 0 |
---|
962 | if (vmove) |
---|
963 | panic("%s: ifp=%p not on the ifnet tailq %p", |
---|
964 | __func__, ifp, &V_ifnet); |
---|
965 | else |
---|
966 | return; /* XXX this should panic as well? */ |
---|
967 | #endif |
---|
968 | } |
---|
969 | |
---|
970 | /* |
---|
971 | * At this point we know the interface still was on the ifnet list |
---|
972 | * and we removed it so we are in a stable state. |
---|
973 | */ |
---|
974 | #ifdef VIMAGE |
---|
975 | curvnet->vnet_ifcnt--; |
---|
976 | #endif |
---|
977 | |
---|
978 | /* |
---|
979 | * In any case (destroy or vmove) detach us from the groups |
---|
980 | * and remove/wait for pending events on the taskq. |
---|
981 | * XXX-BZ in theory an interface could still enqueue a taskq change? |
---|
982 | */ |
---|
983 | if_delgroups(ifp); |
---|
984 | |
---|
985 | taskqueue_drain(taskqueue_swi, &ifp->if_linktask); |
---|
986 | |
---|
987 | /* |
---|
988 | * Check if this is a cloned interface or not. Must do even if |
---|
989 | * shutting down as a if_vmove_reclaim() would move the ifp and |
---|
990 | * the if_clone_addgroup() will have a corrupted string overwise |
---|
991 | * from a gibberish pointer. |
---|
992 | */ |
---|
993 | if (vmove && ifcp != NULL) |
---|
994 | *ifcp = if_clone_findifc(ifp); |
---|
995 | |
---|
996 | if_down(ifp); |
---|
997 | |
---|
998 | #ifdef VIMAGE |
---|
999 | /* |
---|
1000 | * On VNET shutdown abort here as the stack teardown will do all |
---|
1001 | * the work top-down for us. |
---|
1002 | */ |
---|
1003 | if (shutdown) { |
---|
1004 | /* |
---|
1005 | * In case of a vmove we are done here without error. |
---|
1006 | * If we would signal an error it would lead to the same |
---|
1007 | * abort as if we did not find the ifnet anymore. |
---|
1008 | * if_detach() calls us in void context and does not care |
---|
1009 | * about an early abort notification, so life is splendid :) |
---|
1010 | */ |
---|
1011 | goto finish_vnet_shutdown; |
---|
1012 | } |
---|
1013 | #endif |
---|
1014 | |
---|
1015 | /* |
---|
1016 | * At this point we are not tearing down a VNET and are either |
---|
1017 | * going to destroy or vmove the interface and have to cleanup |
---|
1018 | * accordingly. |
---|
1019 | */ |
---|
1020 | |
---|
1021 | /* |
---|
1022 | * Remove routes and flush queues. |
---|
1023 | */ |
---|
1024 | #ifdef ALTQ |
---|
1025 | if (ALTQ_IS_ENABLED(&ifp->if_snd)) |
---|
1026 | altq_disable(&ifp->if_snd); |
---|
1027 | if (ALTQ_IS_ATTACHED(&ifp->if_snd)) |
---|
1028 | altq_detach(&ifp->if_snd); |
---|
1029 | #endif |
---|
1030 | |
---|
1031 | if_purgeaddrs(ifp); |
---|
1032 | |
---|
1033 | #ifdef INET |
---|
1034 | in_ifdetach(ifp); |
---|
1035 | #endif |
---|
1036 | |
---|
1037 | #ifdef INET6 |
---|
1038 | /* |
---|
1039 | * Remove all IPv6 kernel structs related to ifp. This should be done |
---|
1040 | * before removing routing entries below, since IPv6 interface direct |
---|
1041 | * routes are expected to be removed by the IPv6-specific kernel API. |
---|
1042 | * Otherwise, the kernel will detect some inconsistency and bark it. |
---|
1043 | */ |
---|
1044 | in6_ifdetach(ifp); |
---|
1045 | #endif |
---|
1046 | if_purgemaddrs(ifp); |
---|
1047 | |
---|
1048 | /* Announce that the interface is gone. */ |
---|
1049 | rt_ifannouncemsg(ifp, IFAN_DEPARTURE); |
---|
1050 | EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); |
---|
1051 | if (IS_DEFAULT_VNET(curvnet)) |
---|
1052 | devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); |
---|
1053 | |
---|
1054 | if (!vmove) { |
---|
1055 | /* |
---|
1056 | * Prevent further calls into the device driver via ifnet. |
---|
1057 | */ |
---|
1058 | if_dead(ifp); |
---|
1059 | |
---|
1060 | /* |
---|
1061 | * Remove link ifaddr pointer and maybe decrement if_index. |
---|
1062 | * Clean up all addresses. |
---|
1063 | */ |
---|
1064 | ifp->if_addr = NULL; |
---|
1065 | |
---|
1066 | /* We can now free link ifaddr. */ |
---|
1067 | IF_ADDR_WLOCK(ifp); |
---|
1068 | if (!TAILQ_EMPTY(&ifp->if_addrhead)) { |
---|
1069 | ifa = TAILQ_FIRST(&ifp->if_addrhead); |
---|
1070 | TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); |
---|
1071 | IF_ADDR_WUNLOCK(ifp); |
---|
1072 | ifa_free(ifa); |
---|
1073 | } else |
---|
1074 | IF_ADDR_WUNLOCK(ifp); |
---|
1075 | } |
---|
1076 | |
---|
1077 | rt_flushifroutes(ifp); |
---|
1078 | |
---|
1079 | #ifdef VIMAGE |
---|
1080 | finish_vnet_shutdown: |
---|
1081 | #endif |
---|
1082 | /* |
---|
1083 | * We cannot hold the lock over dom_ifdetach calls as they might |
---|
1084 | * sleep, for example trying to drain a callout, thus open up the |
---|
1085 | * theoretical race with re-attaching. |
---|
1086 | */ |
---|
1087 | IF_AFDATA_LOCK(ifp); |
---|
1088 | i = ifp->if_afdata_initialized; |
---|
1089 | ifp->if_afdata_initialized = 0; |
---|
1090 | IF_AFDATA_UNLOCK(ifp); |
---|
1091 | for (dp = domains; i > 0 && dp; dp = dp->dom_next) { |
---|
1092 | if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) { |
---|
1093 | (*dp->dom_ifdetach)(ifp, |
---|
1094 | ifp->if_afdata[dp->dom_family]); |
---|
1095 | ifp->if_afdata[dp->dom_family] = NULL; |
---|
1096 | } |
---|
1097 | } |
---|
1098 | |
---|
1099 | return (0); |
---|
1100 | } |
---|
1101 | |
---|
1102 | #ifdef VIMAGE |
---|
1103 | /* |
---|
1104 | * if_vmove() performs a limited version of if_detach() in current |
---|
1105 | * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg. |
---|
1106 | * An attempt is made to shrink if_index in current vnet, find an |
---|
1107 | * unused if_index in target vnet and calls if_grow() if necessary, |
---|
1108 | * and finally find an unused if_xname for the target vnet. |
---|
1109 | */ |
---|
1110 | static void |
---|
1111 | if_vmove(struct ifnet *ifp, struct vnet *new_vnet) |
---|
1112 | { |
---|
1113 | struct if_clone *ifc; |
---|
1114 | u_int bif_dlt, bif_hdrlen; |
---|
1115 | int rc; |
---|
1116 | |
---|
1117 | /* |
---|
1118 | * if_detach_internal() will call the eventhandler to notify |
---|
1119 | * interface departure. That will detach if_bpf. We need to |
---|
1120 | * safe the dlt and hdrlen so we can re-attach it later. |
---|
1121 | */ |
---|
1122 | bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen); |
---|
1123 | |
---|
1124 | /* |
---|
1125 | * Detach from current vnet, but preserve LLADDR info, do not |
---|
1126 | * mark as dead etc. so that the ifnet can be reattached later. |
---|
1127 | * If we cannot find it, we lost the race to someone else. |
---|
1128 | */ |
---|
1129 | rc = if_detach_internal(ifp, 1, &ifc); |
---|
1130 | if (rc != 0) |
---|
1131 | return; |
---|
1132 | |
---|
1133 | /* |
---|
1134 | * Unlink the ifnet from ifindex_table[] in current vnet, and shrink |
---|
1135 | * the if_index for that vnet if possible. |
---|
1136 | * |
---|
1137 | * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized, |
---|
1138 | * or we'd lock on one vnet and unlock on another. |
---|
1139 | */ |
---|
1140 | IFNET_WLOCK(); |
---|
1141 | ifindex_free_locked(ifp->if_index); |
---|
1142 | IFNET_WUNLOCK(); |
---|
1143 | |
---|
1144 | /* |
---|
1145 | * Perform interface-specific reassignment tasks, if provided by |
---|
1146 | * the driver. |
---|
1147 | */ |
---|
1148 | if (ifp->if_reassign != NULL) |
---|
1149 | ifp->if_reassign(ifp, new_vnet, NULL); |
---|
1150 | |
---|
1151 | /* |
---|
1152 | * Switch to the context of the target vnet. |
---|
1153 | */ |
---|
1154 | CURVNET_SET_QUIET(new_vnet); |
---|
1155 | |
---|
1156 | IFNET_WLOCK(); |
---|
1157 | ifp->if_index = ifindex_alloc(); |
---|
1158 | ifnet_setbyindex_locked(ifp->if_index, ifp); |
---|
1159 | IFNET_WUNLOCK(); |
---|
1160 | |
---|
1161 | if_attach_internal(ifp, 1, ifc); |
---|
1162 | |
---|
1163 | if (ifp->if_bpf == NULL) |
---|
1164 | bpfattach(ifp, bif_dlt, bif_hdrlen); |
---|
1165 | |
---|
1166 | CURVNET_RESTORE(); |
---|
1167 | } |
---|
1168 | |
---|
1169 | /* |
---|
1170 | * Move an ifnet to or from another child prison/vnet, specified by the jail id. |
---|
1171 | */ |
---|
1172 | static int |
---|
1173 | if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid) |
---|
1174 | { |
---|
1175 | struct prison *pr; |
---|
1176 | struct ifnet *difp; |
---|
1177 | int shutdown; |
---|
1178 | |
---|
1179 | /* Try to find the prison within our visibility. */ |
---|
1180 | sx_slock(&allprison_lock); |
---|
1181 | pr = prison_find_child(td->td_ucred->cr_prison, jid); |
---|
1182 | sx_sunlock(&allprison_lock); |
---|
1183 | if (pr == NULL) |
---|
1184 | return (ENXIO); |
---|
1185 | prison_hold_locked(pr); |
---|
1186 | mtx_unlock(&pr->pr_mtx); |
---|
1187 | |
---|
1188 | /* Do not try to move the iface from and to the same prison. */ |
---|
1189 | if (pr->pr_vnet == ifp->if_vnet) { |
---|
1190 | prison_free(pr); |
---|
1191 | return (EEXIST); |
---|
1192 | } |
---|
1193 | |
---|
1194 | /* Make sure the named iface does not exists in the dst. prison/vnet. */ |
---|
1195 | /* XXX Lock interfaces to avoid races. */ |
---|
1196 | CURVNET_SET_QUIET(pr->pr_vnet); |
---|
1197 | difp = ifunit(ifname); |
---|
1198 | if (difp != NULL) { |
---|
1199 | CURVNET_RESTORE(); |
---|
1200 | prison_free(pr); |
---|
1201 | return (EEXIST); |
---|
1202 | } |
---|
1203 | |
---|
1204 | /* Make sure the VNET is stable. */ |
---|
1205 | shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET && |
---|
1206 | ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; |
---|
1207 | if (shutdown) { |
---|
1208 | CURVNET_RESTORE(); |
---|
1209 | prison_free(pr); |
---|
1210 | return (EBUSY); |
---|
1211 | } |
---|
1212 | CURVNET_RESTORE(); |
---|
1213 | |
---|
1214 | /* Move the interface into the child jail/vnet. */ |
---|
1215 | if_vmove(ifp, pr->pr_vnet); |
---|
1216 | |
---|
1217 | /* Report the new if_xname back to the userland. */ |
---|
1218 | sprintf(ifname, "%s", ifp->if_xname); |
---|
1219 | |
---|
1220 | prison_free(pr); |
---|
1221 | return (0); |
---|
1222 | } |
---|
1223 | |
---|
1224 | static int |
---|
1225 | if_vmove_reclaim(struct thread *td, char *ifname, int jid) |
---|
1226 | { |
---|
1227 | struct prison *pr; |
---|
1228 | struct vnet *vnet_dst; |
---|
1229 | struct ifnet *ifp; |
---|
1230 | int shutdown; |
---|
1231 | |
---|
1232 | /* Try to find the prison within our visibility. */ |
---|
1233 | sx_slock(&allprison_lock); |
---|
1234 | pr = prison_find_child(td->td_ucred->cr_prison, jid); |
---|
1235 | sx_sunlock(&allprison_lock); |
---|
1236 | if (pr == NULL) |
---|
1237 | return (ENXIO); |
---|
1238 | prison_hold_locked(pr); |
---|
1239 | mtx_unlock(&pr->pr_mtx); |
---|
1240 | |
---|
1241 | /* Make sure the named iface exists in the source prison/vnet. */ |
---|
1242 | CURVNET_SET(pr->pr_vnet); |
---|
1243 | ifp = ifunit(ifname); /* XXX Lock to avoid races. */ |
---|
1244 | if (ifp == NULL) { |
---|
1245 | CURVNET_RESTORE(); |
---|
1246 | prison_free(pr); |
---|
1247 | return (ENXIO); |
---|
1248 | } |
---|
1249 | |
---|
1250 | /* Do not try to move the iface from and to the same prison. */ |
---|
1251 | vnet_dst = TD_TO_VNET(td); |
---|
1252 | if (vnet_dst == ifp->if_vnet) { |
---|
1253 | CURVNET_RESTORE(); |
---|
1254 | prison_free(pr); |
---|
1255 | return (EEXIST); |
---|
1256 | } |
---|
1257 | |
---|
1258 | /* Make sure the VNET is stable. */ |
---|
1259 | shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET && |
---|
1260 | ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; |
---|
1261 | if (shutdown) { |
---|
1262 | CURVNET_RESTORE(); |
---|
1263 | prison_free(pr); |
---|
1264 | return (EBUSY); |
---|
1265 | } |
---|
1266 | |
---|
1267 | /* Get interface back from child jail/vnet. */ |
---|
1268 | if_vmove(ifp, vnet_dst); |
---|
1269 | CURVNET_RESTORE(); |
---|
1270 | |
---|
1271 | /* Report the new if_xname back to the userland. */ |
---|
1272 | sprintf(ifname, "%s", ifp->if_xname); |
---|
1273 | |
---|
1274 | prison_free(pr); |
---|
1275 | return (0); |
---|
1276 | } |
---|
1277 | #endif /* VIMAGE */ |
---|
1278 | |
---|
1279 | /* |
---|
1280 | * Add a group to an interface |
---|
1281 | */ |
---|
1282 | int |
---|
1283 | if_addgroup(struct ifnet *ifp, const char *groupname) |
---|
1284 | { |
---|
1285 | struct ifg_list *ifgl; |
---|
1286 | struct ifg_group *ifg = NULL; |
---|
1287 | struct ifg_member *ifgm; |
---|
1288 | int new = 0; |
---|
1289 | |
---|
1290 | if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && |
---|
1291 | groupname[strlen(groupname) - 1] <= '9') |
---|
1292 | return (EINVAL); |
---|
1293 | |
---|
1294 | IFNET_WLOCK(); |
---|
1295 | TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) |
---|
1296 | if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { |
---|
1297 | IFNET_WUNLOCK(); |
---|
1298 | return (EEXIST); |
---|
1299 | } |
---|
1300 | |
---|
1301 | if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, |
---|
1302 | M_NOWAIT)) == NULL) { |
---|
1303 | IFNET_WUNLOCK(); |
---|
1304 | return (ENOMEM); |
---|
1305 | } |
---|
1306 | |
---|
1307 | if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), |
---|
1308 | M_TEMP, M_NOWAIT)) == NULL) { |
---|
1309 | free(ifgl, M_TEMP); |
---|
1310 | IFNET_WUNLOCK(); |
---|
1311 | return (ENOMEM); |
---|
1312 | } |
---|
1313 | |
---|
1314 | TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) |
---|
1315 | if (!strcmp(ifg->ifg_group, groupname)) |
---|
1316 | break; |
---|
1317 | |
---|
1318 | if (ifg == NULL) { |
---|
1319 | if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), |
---|
1320 | M_TEMP, M_NOWAIT)) == NULL) { |
---|
1321 | free(ifgl, M_TEMP); |
---|
1322 | free(ifgm, M_TEMP); |
---|
1323 | IFNET_WUNLOCK(); |
---|
1324 | return (ENOMEM); |
---|
1325 | } |
---|
1326 | strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); |
---|
1327 | ifg->ifg_refcnt = 0; |
---|
1328 | TAILQ_INIT(&ifg->ifg_members); |
---|
1329 | TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); |
---|
1330 | new = 1; |
---|
1331 | } |
---|
1332 | |
---|
1333 | ifg->ifg_refcnt++; |
---|
1334 | ifgl->ifgl_group = ifg; |
---|
1335 | ifgm->ifgm_ifp = ifp; |
---|
1336 | |
---|
1337 | IF_ADDR_WLOCK(ifp); |
---|
1338 | TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); |
---|
1339 | TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); |
---|
1340 | IF_ADDR_WUNLOCK(ifp); |
---|
1341 | |
---|
1342 | IFNET_WUNLOCK(); |
---|
1343 | |
---|
1344 | if (new) |
---|
1345 | EVENTHANDLER_INVOKE(group_attach_event, ifg); |
---|
1346 | EVENTHANDLER_INVOKE(group_change_event, groupname); |
---|
1347 | |
---|
1348 | return (0); |
---|
1349 | } |
---|
1350 | |
---|
1351 | /* |
---|
1352 | * Remove a group from an interface |
---|
1353 | */ |
---|
1354 | int |
---|
1355 | if_delgroup(struct ifnet *ifp, const char *groupname) |
---|
1356 | { |
---|
1357 | struct ifg_list *ifgl; |
---|
1358 | struct ifg_member *ifgm; |
---|
1359 | |
---|
1360 | IFNET_WLOCK(); |
---|
1361 | TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) |
---|
1362 | if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) |
---|
1363 | break; |
---|
1364 | if (ifgl == NULL) { |
---|
1365 | IFNET_WUNLOCK(); |
---|
1366 | return (ENOENT); |
---|
1367 | } |
---|
1368 | |
---|
1369 | IF_ADDR_WLOCK(ifp); |
---|
1370 | TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); |
---|
1371 | IF_ADDR_WUNLOCK(ifp); |
---|
1372 | |
---|
1373 | TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) |
---|
1374 | if (ifgm->ifgm_ifp == ifp) |
---|
1375 | break; |
---|
1376 | |
---|
1377 | if (ifgm != NULL) { |
---|
1378 | TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); |
---|
1379 | free(ifgm, M_TEMP); |
---|
1380 | } |
---|
1381 | |
---|
1382 | if (--ifgl->ifgl_group->ifg_refcnt == 0) { |
---|
1383 | TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); |
---|
1384 | IFNET_WUNLOCK(); |
---|
1385 | EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); |
---|
1386 | free(ifgl->ifgl_group, M_TEMP); |
---|
1387 | } else |
---|
1388 | IFNET_WUNLOCK(); |
---|
1389 | |
---|
1390 | free(ifgl, M_TEMP); |
---|
1391 | |
---|
1392 | EVENTHANDLER_INVOKE(group_change_event, groupname); |
---|
1393 | |
---|
1394 | return (0); |
---|
1395 | } |
---|
1396 | |
---|
1397 | /* |
---|
1398 | * Remove an interface from all groups |
---|
1399 | */ |
---|
1400 | static void |
---|
1401 | if_delgroups(struct ifnet *ifp) |
---|
1402 | { |
---|
1403 | struct ifg_list *ifgl; |
---|
1404 | struct ifg_member *ifgm; |
---|
1405 | char groupname[IFNAMSIZ]; |
---|
1406 | |
---|
1407 | IFNET_WLOCK(); |
---|
1408 | while (!TAILQ_EMPTY(&ifp->if_groups)) { |
---|
1409 | ifgl = TAILQ_FIRST(&ifp->if_groups); |
---|
1410 | |
---|
1411 | strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ); |
---|
1412 | |
---|
1413 | IF_ADDR_WLOCK(ifp); |
---|
1414 | TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); |
---|
1415 | IF_ADDR_WUNLOCK(ifp); |
---|
1416 | |
---|
1417 | TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) |
---|
1418 | if (ifgm->ifgm_ifp == ifp) |
---|
1419 | break; |
---|
1420 | |
---|
1421 | if (ifgm != NULL) { |
---|
1422 | TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, |
---|
1423 | ifgm_next); |
---|
1424 | free(ifgm, M_TEMP); |
---|
1425 | } |
---|
1426 | |
---|
1427 | if (--ifgl->ifgl_group->ifg_refcnt == 0) { |
---|
1428 | TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); |
---|
1429 | IFNET_WUNLOCK(); |
---|
1430 | EVENTHANDLER_INVOKE(group_detach_event, |
---|
1431 | ifgl->ifgl_group); |
---|
1432 | free(ifgl->ifgl_group, M_TEMP); |
---|
1433 | } else |
---|
1434 | IFNET_WUNLOCK(); |
---|
1435 | |
---|
1436 | free(ifgl, M_TEMP); |
---|
1437 | |
---|
1438 | EVENTHANDLER_INVOKE(group_change_event, groupname); |
---|
1439 | |
---|
1440 | IFNET_WLOCK(); |
---|
1441 | } |
---|
1442 | IFNET_WUNLOCK(); |
---|
1443 | } |
---|
1444 | |
---|
1445 | /* |
---|
1446 | * Stores all groups from an interface in memory pointed |
---|
1447 | * to by data |
---|
1448 | */ |
---|
1449 | static int |
---|
1450 | if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) |
---|
1451 | { |
---|
1452 | int len, error; |
---|
1453 | struct ifg_list *ifgl; |
---|
1454 | struct ifg_req ifgrq, *ifgp; |
---|
1455 | struct ifgroupreq *ifgr = data; |
---|
1456 | |
---|
1457 | if (ifgr->ifgr_len == 0) { |
---|
1458 | IF_ADDR_RLOCK(ifp); |
---|
1459 | TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) |
---|
1460 | ifgr->ifgr_len += sizeof(struct ifg_req); |
---|
1461 | IF_ADDR_RUNLOCK(ifp); |
---|
1462 | return (0); |
---|
1463 | } |
---|
1464 | |
---|
1465 | len = ifgr->ifgr_len; |
---|
1466 | ifgp = ifgr->ifgr_groups; |
---|
1467 | /* XXX: wire */ |
---|
1468 | IF_ADDR_RLOCK(ifp); |
---|
1469 | TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { |
---|
1470 | if (len < sizeof(ifgrq)) { |
---|
1471 | IF_ADDR_RUNLOCK(ifp); |
---|
1472 | return (EINVAL); |
---|
1473 | } |
---|
1474 | bzero(&ifgrq, sizeof ifgrq); |
---|
1475 | strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, |
---|
1476 | sizeof(ifgrq.ifgrq_group)); |
---|
1477 | if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { |
---|
1478 | IF_ADDR_RUNLOCK(ifp); |
---|
1479 | return (error); |
---|
1480 | } |
---|
1481 | len -= sizeof(ifgrq); |
---|
1482 | ifgp++; |
---|
1483 | } |
---|
1484 | IF_ADDR_RUNLOCK(ifp); |
---|
1485 | |
---|
1486 | return (0); |
---|
1487 | } |
---|
1488 | |
---|
1489 | /* |
---|
1490 | * Stores all members of a group in memory pointed to by data |
---|
1491 | */ |
---|
1492 | static int |
---|
1493 | if_getgroupmembers(struct ifgroupreq *data) |
---|
1494 | { |
---|
1495 | struct ifgroupreq *ifgr = data; |
---|
1496 | struct ifg_group *ifg; |
---|
1497 | struct ifg_member *ifgm; |
---|
1498 | struct ifg_req ifgrq, *ifgp; |
---|
1499 | int len, error; |
---|
1500 | |
---|
1501 | IFNET_RLOCK(); |
---|
1502 | TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) |
---|
1503 | if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) |
---|
1504 | break; |
---|
1505 | if (ifg == NULL) { |
---|
1506 | IFNET_RUNLOCK(); |
---|
1507 | return (ENOENT); |
---|
1508 | } |
---|
1509 | |
---|
1510 | if (ifgr->ifgr_len == 0) { |
---|
1511 | TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) |
---|
1512 | ifgr->ifgr_len += sizeof(ifgrq); |
---|
1513 | IFNET_RUNLOCK(); |
---|
1514 | return (0); |
---|
1515 | } |
---|
1516 | |
---|
1517 | len = ifgr->ifgr_len; |
---|
1518 | ifgp = ifgr->ifgr_groups; |
---|
1519 | TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { |
---|
1520 | if (len < sizeof(ifgrq)) { |
---|
1521 | IFNET_RUNLOCK(); |
---|
1522 | return (EINVAL); |
---|
1523 | } |
---|
1524 | bzero(&ifgrq, sizeof ifgrq); |
---|
1525 | strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, |
---|
1526 | sizeof(ifgrq.ifgrq_member)); |
---|
1527 | if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { |
---|
1528 | IFNET_RUNLOCK(); |
---|
1529 | return (error); |
---|
1530 | } |
---|
1531 | len -= sizeof(ifgrq); |
---|
1532 | ifgp++; |
---|
1533 | } |
---|
1534 | IFNET_RUNLOCK(); |
---|
1535 | |
---|
1536 | return (0); |
---|
1537 | } |
---|
1538 | |
---|
1539 | /* |
---|
1540 | * Return counter values from counter(9)s stored in ifnet. |
---|
1541 | */ |
---|
1542 | uint64_t |
---|
1543 | if_get_counter_default(struct ifnet *ifp, ift_counter cnt) |
---|
1544 | { |
---|
1545 | |
---|
1546 | KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); |
---|
1547 | |
---|
1548 | return (counter_u64_fetch(ifp->if_counters[cnt])); |
---|
1549 | } |
---|
1550 | |
---|
1551 | /* |
---|
1552 | * Increase an ifnet counter. Usually used for counters shared |
---|
1553 | * between the stack and a driver, but function supports them all. |
---|
1554 | */ |
---|
1555 | void |
---|
1556 | if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc) |
---|
1557 | { |
---|
1558 | |
---|
1559 | KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); |
---|
1560 | |
---|
1561 | counter_u64_add(ifp->if_counters[cnt], inc); |
---|
1562 | } |
---|
1563 | |
---|
1564 | /* |
---|
1565 | * Copy data from ifnet to userland API structure if_data. |
---|
1566 | */ |
---|
1567 | void |
---|
1568 | if_data_copy(struct ifnet *ifp, struct if_data *ifd) |
---|
1569 | { |
---|
1570 | |
---|
1571 | ifd->ifi_type = ifp->if_type; |
---|
1572 | ifd->ifi_physical = 0; |
---|
1573 | ifd->ifi_addrlen = ifp->if_addrlen; |
---|
1574 | ifd->ifi_hdrlen = ifp->if_hdrlen; |
---|
1575 | ifd->ifi_link_state = ifp->if_link_state; |
---|
1576 | ifd->ifi_vhid = 0; |
---|
1577 | ifd->ifi_datalen = sizeof(struct if_data); |
---|
1578 | ifd->ifi_mtu = ifp->if_mtu; |
---|
1579 | ifd->ifi_metric = ifp->if_metric; |
---|
1580 | ifd->ifi_baudrate = ifp->if_baudrate; |
---|
1581 | ifd->ifi_hwassist = ifp->if_hwassist; |
---|
1582 | ifd->ifi_epoch = ifp->if_epoch; |
---|
1583 | ifd->ifi_lastchange = ifp->if_lastchange; |
---|
1584 | |
---|
1585 | ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS); |
---|
1586 | ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS); |
---|
1587 | ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS); |
---|
1588 | ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS); |
---|
1589 | ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS); |
---|
1590 | ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES); |
---|
1591 | ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES); |
---|
1592 | ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS); |
---|
1593 | ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS); |
---|
1594 | ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS); |
---|
1595 | ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS); |
---|
1596 | ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO); |
---|
1597 | } |
---|
1598 | |
---|
1599 | /* |
---|
1600 | * Wrapper functions for struct ifnet address list locking macros. These are |
---|
1601 | * used by kernel modules to avoid encoding programming interface or binary |
---|
1602 | * interface assumptions that may be violated when kernel-internal locking |
---|
1603 | * approaches change. |
---|
1604 | */ |
---|
1605 | void |
---|
1606 | if_addr_rlock(struct ifnet *ifp) |
---|
1607 | { |
---|
1608 | |
---|
1609 | IF_ADDR_RLOCK(ifp); |
---|
1610 | } |
---|
1611 | |
---|
1612 | void |
---|
1613 | if_addr_runlock(struct ifnet *ifp) |
---|
1614 | { |
---|
1615 | |
---|
1616 | IF_ADDR_RUNLOCK(ifp); |
---|
1617 | } |
---|
1618 | |
---|
1619 | void |
---|
1620 | if_maddr_rlock(if_t ifp) |
---|
1621 | { |
---|
1622 | |
---|
1623 | IF_ADDR_RLOCK((struct ifnet *)ifp); |
---|
1624 | } |
---|
1625 | |
---|
1626 | void |
---|
1627 | if_maddr_runlock(if_t ifp) |
---|
1628 | { |
---|
1629 | |
---|
1630 | IF_ADDR_RUNLOCK((struct ifnet *)ifp); |
---|
1631 | } |
---|
1632 | |
---|
1633 | /* |
---|
1634 | * Initialization, destruction and refcounting functions for ifaddrs. |
---|
1635 | */ |
---|
1636 | struct ifaddr * |
---|
1637 | ifa_alloc(size_t size, int flags) |
---|
1638 | { |
---|
1639 | struct ifaddr *ifa; |
---|
1640 | |
---|
1641 | KASSERT(size >= sizeof(struct ifaddr), |
---|
1642 | ("%s: invalid size %zu", __func__, size)); |
---|
1643 | |
---|
1644 | ifa = malloc(size, M_IFADDR, M_ZERO | flags); |
---|
1645 | if (ifa == NULL) |
---|
1646 | return (NULL); |
---|
1647 | |
---|
1648 | if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL) |
---|
1649 | goto fail; |
---|
1650 | if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL) |
---|
1651 | goto fail; |
---|
1652 | if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL) |
---|
1653 | goto fail; |
---|
1654 | if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL) |
---|
1655 | goto fail; |
---|
1656 | |
---|
1657 | refcount_init(&ifa->ifa_refcnt, 1); |
---|
1658 | |
---|
1659 | return (ifa); |
---|
1660 | |
---|
1661 | fail: |
---|
1662 | /* free(NULL) is okay */ |
---|
1663 | counter_u64_free(ifa->ifa_opackets); |
---|
1664 | counter_u64_free(ifa->ifa_ipackets); |
---|
1665 | counter_u64_free(ifa->ifa_obytes); |
---|
1666 | counter_u64_free(ifa->ifa_ibytes); |
---|
1667 | free(ifa, M_IFADDR); |
---|
1668 | |
---|
1669 | return (NULL); |
---|
1670 | } |
---|
1671 | |
---|
1672 | void |
---|
1673 | ifa_ref(struct ifaddr *ifa) |
---|
1674 | { |
---|
1675 | |
---|
1676 | refcount_acquire(&ifa->ifa_refcnt); |
---|
1677 | } |
---|
1678 | |
---|
1679 | void |
---|
1680 | ifa_free(struct ifaddr *ifa) |
---|
1681 | { |
---|
1682 | |
---|
1683 | if (refcount_release(&ifa->ifa_refcnt)) { |
---|
1684 | counter_u64_free(ifa->ifa_opackets); |
---|
1685 | counter_u64_free(ifa->ifa_ipackets); |
---|
1686 | counter_u64_free(ifa->ifa_obytes); |
---|
1687 | counter_u64_free(ifa->ifa_ibytes); |
---|
1688 | free(ifa, M_IFADDR); |
---|
1689 | } |
---|
1690 | } |
---|
1691 | |
---|
1692 | static int |
---|
1693 | ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa, |
---|
1694 | struct sockaddr *ia) |
---|
1695 | { |
---|
1696 | int error; |
---|
1697 | struct rt_addrinfo info; |
---|
1698 | struct sockaddr_dl null_sdl; |
---|
1699 | struct ifnet *ifp; |
---|
1700 | |
---|
1701 | ifp = ifa->ifa_ifp; |
---|
1702 | |
---|
1703 | bzero(&info, sizeof(info)); |
---|
1704 | if (cmd != RTM_DELETE) |
---|
1705 | info.rti_ifp = V_loif; |
---|
1706 | info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; |
---|
1707 | info.rti_info[RTAX_DST] = ia; |
---|
1708 | info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; |
---|
1709 | link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type); |
---|
1710 | |
---|
1711 | error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib); |
---|
1712 | |
---|
1713 | if (error != 0) |
---|
1714 | log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n", |
---|
1715 | __func__, otype, if_name(ifp), error); |
---|
1716 | |
---|
1717 | return (error); |
---|
1718 | } |
---|
1719 | |
---|
1720 | int |
---|
1721 | ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) |
---|
1722 | { |
---|
1723 | |
---|
1724 | return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia)); |
---|
1725 | } |
---|
1726 | |
---|
1727 | int |
---|
1728 | ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) |
---|
1729 | { |
---|
1730 | |
---|
1731 | return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia)); |
---|
1732 | } |
---|
1733 | |
---|
1734 | int |
---|
1735 | ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) |
---|
1736 | { |
---|
1737 | |
---|
1738 | return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia)); |
---|
1739 | } |
---|
1740 | |
---|
1741 | /* |
---|
1742 | * XXX: Because sockaddr_dl has deeper structure than the sockaddr |
---|
1743 | * structs used to represent other address families, it is necessary |
---|
1744 | * to perform a different comparison. |
---|
1745 | */ |
---|
1746 | |
---|
1747 | #define sa_dl_equal(a1, a2) \ |
---|
1748 | ((((const struct sockaddr_dl *)(a1))->sdl_len == \ |
---|
1749 | ((const struct sockaddr_dl *)(a2))->sdl_len) && \ |
---|
1750 | (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \ |
---|
1751 | CLLADDR((const struct sockaddr_dl *)(a2)), \ |
---|
1752 | ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0)) |
---|
1753 | |
---|
1754 | /* |
---|
1755 | * Locate an interface based on a complete address. |
---|
1756 | */ |
---|
1757 | /*ARGSUSED*/ |
---|
1758 | static struct ifaddr * |
---|
1759 | ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref) |
---|
1760 | { |
---|
1761 | struct ifnet *ifp; |
---|
1762 | struct ifaddr *ifa; |
---|
1763 | |
---|
1764 | IFNET_RLOCK_NOSLEEP(); |
---|
1765 | TAILQ_FOREACH(ifp, &V_ifnet, if_link) { |
---|
1766 | IF_ADDR_RLOCK(ifp); |
---|
1767 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { |
---|
1768 | if (ifa->ifa_addr->sa_family != addr->sa_family) |
---|
1769 | continue; |
---|
1770 | if (sa_equal(addr, ifa->ifa_addr)) { |
---|
1771 | if (getref) |
---|
1772 | ifa_ref(ifa); |
---|
1773 | IF_ADDR_RUNLOCK(ifp); |
---|
1774 | goto done; |
---|
1775 | } |
---|
1776 | /* IP6 doesn't have broadcast */ |
---|
1777 | if ((ifp->if_flags & IFF_BROADCAST) && |
---|
1778 | ifa->ifa_broadaddr && |
---|
1779 | ifa->ifa_broadaddr->sa_len != 0 && |
---|
1780 | sa_equal(ifa->ifa_broadaddr, addr)) { |
---|
1781 | if (getref) |
---|
1782 | ifa_ref(ifa); |
---|
1783 | IF_ADDR_RUNLOCK(ifp); |
---|
1784 | goto done; |
---|
1785 | } |
---|
1786 | } |
---|
1787 | IF_ADDR_RUNLOCK(ifp); |
---|
1788 | } |
---|
1789 | ifa = NULL; |
---|
1790 | done: |
---|
1791 | IFNET_RUNLOCK_NOSLEEP(); |
---|
1792 | return (ifa); |
---|
1793 | } |
---|
1794 | |
---|
1795 | struct ifaddr * |
---|
1796 | ifa_ifwithaddr(const struct sockaddr *addr) |
---|
1797 | { |
---|
1798 | |
---|
1799 | return (ifa_ifwithaddr_internal(addr, 1)); |
---|
1800 | } |
---|
1801 | |
---|
1802 | int |
---|
1803 | ifa_ifwithaddr_check(const struct sockaddr *addr) |
---|
1804 | { |
---|
1805 | |
---|
1806 | return (ifa_ifwithaddr_internal(addr, 0) != NULL); |
---|
1807 | } |
---|
1808 | |
---|
1809 | /* |
---|
1810 | * Locate an interface based on the broadcast address. |
---|
1811 | */ |
---|
1812 | /* ARGSUSED */ |
---|
1813 | struct ifaddr * |
---|
1814 | ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum) |
---|
1815 | { |
---|
1816 | struct ifnet *ifp; |
---|
1817 | struct ifaddr *ifa; |
---|
1818 | |
---|
1819 | IFNET_RLOCK_NOSLEEP(); |
---|
1820 | TAILQ_FOREACH(ifp, &V_ifnet, if_link) { |
---|
1821 | if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) |
---|
1822 | continue; |
---|
1823 | IF_ADDR_RLOCK(ifp); |
---|
1824 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { |
---|
1825 | if (ifa->ifa_addr->sa_family != addr->sa_family) |
---|
1826 | continue; |
---|
1827 | if ((ifp->if_flags & IFF_BROADCAST) && |
---|
1828 | ifa->ifa_broadaddr && |
---|
1829 | ifa->ifa_broadaddr->sa_len != 0 && |
---|
1830 | sa_equal(ifa->ifa_broadaddr, addr)) { |
---|
1831 | ifa_ref(ifa); |
---|
1832 | IF_ADDR_RUNLOCK(ifp); |
---|
1833 | goto done; |
---|
1834 | } |
---|
1835 | } |
---|
1836 | IF_ADDR_RUNLOCK(ifp); |
---|
1837 | } |
---|
1838 | ifa = NULL; |
---|
1839 | done: |
---|
1840 | IFNET_RUNLOCK_NOSLEEP(); |
---|
1841 | return (ifa); |
---|
1842 | } |
---|
1843 | |
---|
1844 | /* |
---|
1845 | * Locate the point to point interface with a given destination address. |
---|
1846 | */ |
---|
1847 | /*ARGSUSED*/ |
---|
1848 | struct ifaddr * |
---|
1849 | ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum) |
---|
1850 | { |
---|
1851 | struct ifnet *ifp; |
---|
1852 | struct ifaddr *ifa; |
---|
1853 | |
---|
1854 | IFNET_RLOCK_NOSLEEP(); |
---|
1855 | TAILQ_FOREACH(ifp, &V_ifnet, if_link) { |
---|
1856 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) |
---|
1857 | continue; |
---|
1858 | if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) |
---|
1859 | continue; |
---|
1860 | IF_ADDR_RLOCK(ifp); |
---|
1861 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { |
---|
1862 | if (ifa->ifa_addr->sa_family != addr->sa_family) |
---|
1863 | continue; |
---|
1864 | if (ifa->ifa_dstaddr != NULL && |
---|
1865 | sa_equal(addr, ifa->ifa_dstaddr)) { |
---|
1866 | ifa_ref(ifa); |
---|
1867 | IF_ADDR_RUNLOCK(ifp); |
---|
1868 | goto done; |
---|
1869 | } |
---|
1870 | } |
---|
1871 | IF_ADDR_RUNLOCK(ifp); |
---|
1872 | } |
---|
1873 | ifa = NULL; |
---|
1874 | done: |
---|
1875 | IFNET_RUNLOCK_NOSLEEP(); |
---|
1876 | return (ifa); |
---|
1877 | } |
---|
1878 | |
---|
1879 | /* |
---|
1880 | * Find an interface on a specific network. If many, choice |
---|
1881 | * is most specific found. |
---|
1882 | */ |
---|
1883 | struct ifaddr * |
---|
1884 | ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum) |
---|
1885 | { |
---|
1886 | struct ifnet *ifp; |
---|
1887 | struct ifaddr *ifa; |
---|
1888 | struct ifaddr *ifa_maybe = NULL; |
---|
1889 | u_int af = addr->sa_family; |
---|
1890 | const char *addr_data = addr->sa_data, *cplim; |
---|
1891 | |
---|
1892 | /* |
---|
1893 | * AF_LINK addresses can be looked up directly by their index number, |
---|
1894 | * so do that if we can. |
---|
1895 | */ |
---|
1896 | if (af == AF_LINK) { |
---|
1897 | const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr; |
---|
1898 | if (sdl->sdl_index && sdl->sdl_index <= V_if_index) |
---|
1899 | return (ifaddr_byindex(sdl->sdl_index)); |
---|
1900 | } |
---|
1901 | |
---|
1902 | /* |
---|
1903 | * Scan though each interface, looking for ones that have addresses |
---|
1904 | * in this address family and the requested fib. Maintain a reference |
---|
1905 | * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that |
---|
1906 | * kept it stable when we move onto the next interface. |
---|
1907 | */ |
---|
1908 | IFNET_RLOCK_NOSLEEP(); |
---|
1909 | TAILQ_FOREACH(ifp, &V_ifnet, if_link) { |
---|
1910 | if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) |
---|
1911 | continue; |
---|
1912 | IF_ADDR_RLOCK(ifp); |
---|
1913 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { |
---|
1914 | const char *cp, *cp2, *cp3; |
---|
1915 | |
---|
1916 | if (ifa->ifa_addr->sa_family != af) |
---|
1917 | next: continue; |
---|
1918 | if (af == AF_INET && |
---|
1919 | ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) { |
---|
1920 | /* |
---|
1921 | * This is a bit broken as it doesn't |
---|
1922 | * take into account that the remote end may |
---|
1923 | * be a single node in the network we are |
---|
1924 | * looking for. |
---|
1925 | * The trouble is that we don't know the |
---|
1926 | * netmask for the remote end. |
---|
1927 | */ |
---|
1928 | if (ifa->ifa_dstaddr != NULL && |
---|
1929 | sa_equal(addr, ifa->ifa_dstaddr)) { |
---|
1930 | ifa_ref(ifa); |
---|
1931 | IF_ADDR_RUNLOCK(ifp); |
---|
1932 | goto done; |
---|
1933 | } |
---|
1934 | } else { |
---|
1935 | /* |
---|
1936 | * Scan all the bits in the ifa's address. |
---|
1937 | * If a bit dissagrees with what we are |
---|
1938 | * looking for, mask it with the netmask |
---|
1939 | * to see if it really matters. |
---|
1940 | * (A byte at a time) |
---|
1941 | */ |
---|
1942 | if (ifa->ifa_netmask == 0) |
---|
1943 | continue; |
---|
1944 | cp = addr_data; |
---|
1945 | cp2 = ifa->ifa_addr->sa_data; |
---|
1946 | cp3 = ifa->ifa_netmask->sa_data; |
---|
1947 | cplim = ifa->ifa_netmask->sa_len |
---|
1948 | + (char *)ifa->ifa_netmask; |
---|
1949 | while (cp3 < cplim) |
---|
1950 | if ((*cp++ ^ *cp2++) & *cp3++) |
---|
1951 | goto next; /* next address! */ |
---|
1952 | /* |
---|
1953 | * If the netmask of what we just found |
---|
1954 | * is more specific than what we had before |
---|
1955 | * (if we had one), or if the virtual status |
---|
1956 | * of new prefix is better than of the old one, |
---|
1957 | * then remember the new one before continuing |
---|
1958 | * to search for an even better one. |
---|
1959 | */ |
---|
1960 | if (ifa_maybe == NULL || |
---|
1961 | ifa_preferred(ifa_maybe, ifa) || |
---|
1962 | rn_refines((caddr_t)ifa->ifa_netmask, |
---|
1963 | (caddr_t)ifa_maybe->ifa_netmask)) { |
---|
1964 | if (ifa_maybe != NULL) |
---|
1965 | ifa_free(ifa_maybe); |
---|
1966 | ifa_maybe = ifa; |
---|
1967 | ifa_ref(ifa_maybe); |
---|
1968 | } |
---|
1969 | } |
---|
1970 | } |
---|
1971 | IF_ADDR_RUNLOCK(ifp); |
---|
1972 | } |
---|
1973 | ifa = ifa_maybe; |
---|
1974 | ifa_maybe = NULL; |
---|
1975 | done: |
---|
1976 | IFNET_RUNLOCK_NOSLEEP(); |
---|
1977 | if (ifa_maybe != NULL) |
---|
1978 | ifa_free(ifa_maybe); |
---|
1979 | return (ifa); |
---|
1980 | } |
---|
1981 | |
---|
1982 | /* |
---|
1983 | * Find an interface address specific to an interface best matching |
---|
1984 | * a given address. |
---|
1985 | */ |
---|
1986 | struct ifaddr * |
---|
1987 | ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp) |
---|
1988 | { |
---|
1989 | struct ifaddr *ifa; |
---|
1990 | const char *cp, *cp2, *cp3; |
---|
1991 | char *cplim; |
---|
1992 | struct ifaddr *ifa_maybe = NULL; |
---|
1993 | u_int af = addr->sa_family; |
---|
1994 | |
---|
1995 | if (af >= AF_MAX) |
---|
1996 | return (NULL); |
---|
1997 | IF_ADDR_RLOCK(ifp); |
---|
1998 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { |
---|
1999 | if (ifa->ifa_addr->sa_family != af) |
---|
2000 | continue; |
---|
2001 | if (ifa_maybe == NULL) |
---|
2002 | ifa_maybe = ifa; |
---|
2003 | if (ifa->ifa_netmask == 0) { |
---|
2004 | if (sa_equal(addr, ifa->ifa_addr) || |
---|
2005 | (ifa->ifa_dstaddr && |
---|
2006 | sa_equal(addr, ifa->ifa_dstaddr))) |
---|
2007 | goto done; |
---|
2008 | continue; |
---|
2009 | } |
---|
2010 | if (ifp->if_flags & IFF_POINTOPOINT) { |
---|
2011 | if (sa_equal(addr, ifa->ifa_dstaddr)) |
---|
2012 | goto done; |
---|
2013 | } else { |
---|
2014 | cp = addr->sa_data; |
---|
2015 | cp2 = ifa->ifa_addr->sa_data; |
---|
2016 | cp3 = ifa->ifa_netmask->sa_data; |
---|
2017 | cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; |
---|
2018 | for (; cp3 < cplim; cp3++) |
---|
2019 | if ((*cp++ ^ *cp2++) & *cp3) |
---|
2020 | break; |
---|
2021 | if (cp3 == cplim) |
---|
2022 | goto done; |
---|
2023 | } |
---|
2024 | } |
---|
2025 | ifa = ifa_maybe; |
---|
2026 | done: |
---|
2027 | if (ifa != NULL) |
---|
2028 | ifa_ref(ifa); |
---|
2029 | IF_ADDR_RUNLOCK(ifp); |
---|
2030 | return (ifa); |
---|
2031 | } |
---|
2032 | |
---|
2033 | /* |
---|
2034 | * See whether new ifa is better than current one: |
---|
2035 | * 1) A non-virtual one is preferred over virtual. |
---|
2036 | * 2) A virtual in master state preferred over any other state. |
---|
2037 | * |
---|
2038 | * Used in several address selecting functions. |
---|
2039 | */ |
---|
2040 | int |
---|
2041 | ifa_preferred(struct ifaddr *cur, struct ifaddr *next) |
---|
2042 | { |
---|
2043 | |
---|
2044 | return (cur->ifa_carp && (!next->ifa_carp || |
---|
2045 | ((*carp_master_p)(next) && !(*carp_master_p)(cur)))); |
---|
2046 | } |
---|
2047 | |
---|
2048 | #include <net/if_llatbl.h> |
---|
2049 | |
---|
2050 | /* |
---|
2051 | * Default action when installing a route with a Link Level gateway. |
---|
2052 | * Lookup an appropriate real ifa to point to. |
---|
2053 | * This should be moved to /sys/net/link.c eventually. |
---|
2054 | */ |
---|
2055 | static void |
---|
2056 | link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) |
---|
2057 | { |
---|
2058 | struct ifaddr *ifa, *oifa; |
---|
2059 | struct sockaddr *dst; |
---|
2060 | struct ifnet *ifp; |
---|
2061 | |
---|
2062 | if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) || |
---|
2063 | ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL)) |
---|
2064 | return; |
---|
2065 | ifa = ifaof_ifpforaddr(dst, ifp); |
---|
2066 | if (ifa) { |
---|
2067 | oifa = rt->rt_ifa; |
---|
2068 | rt->rt_ifa = ifa; |
---|
2069 | ifa_free(oifa); |
---|
2070 | if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) |
---|
2071 | ifa->ifa_rtrequest(cmd, rt, info); |
---|
2072 | } |
---|
2073 | } |
---|
2074 | |
---|
2075 | struct sockaddr_dl * |
---|
2076 | link_alloc_sdl(size_t size, int flags) |
---|
2077 | { |
---|
2078 | |
---|
2079 | return (malloc(size, M_TEMP, flags)); |
---|
2080 | } |
---|
2081 | |
---|
2082 | void |
---|
2083 | link_free_sdl(struct sockaddr *sa) |
---|
2084 | { |
---|
2085 | free(sa, M_TEMP); |
---|
2086 | } |
---|
2087 | |
---|
2088 | /* |
---|
2089 | * Fills in given sdl with interface basic info. |
---|
2090 | * Returns pointer to filled sdl. |
---|
2091 | */ |
---|
2092 | struct sockaddr_dl * |
---|
2093 | link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype) |
---|
2094 | { |
---|
2095 | struct sockaddr_dl *sdl; |
---|
2096 | |
---|
2097 | sdl = (struct sockaddr_dl *)paddr; |
---|
2098 | memset(sdl, 0, sizeof(struct sockaddr_dl)); |
---|
2099 | sdl->sdl_len = sizeof(struct sockaddr_dl); |
---|
2100 | sdl->sdl_family = AF_LINK; |
---|
2101 | sdl->sdl_index = ifp->if_index; |
---|
2102 | sdl->sdl_type = iftype; |
---|
2103 | |
---|
2104 | return (sdl); |
---|
2105 | } |
---|
2106 | |
---|
2107 | /* |
---|
2108 | * Mark an interface down and notify protocols of |
---|
2109 | * the transition. |
---|
2110 | */ |
---|
2111 | static void |
---|
2112 | if_unroute(struct ifnet *ifp, int flag, int fam) |
---|
2113 | { |
---|
2114 | struct ifaddr *ifa; |
---|
2115 | |
---|
2116 | KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); |
---|
2117 | |
---|
2118 | ifp->if_flags &= ~flag; |
---|
2119 | getmicrotime(&ifp->if_lastchange); |
---|
2120 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) |
---|
2121 | if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) |
---|
2122 | pfctlinput(PRC_IFDOWN, ifa->ifa_addr); |
---|
2123 | ifp->if_qflush(ifp); |
---|
2124 | |
---|
2125 | if (ifp->if_carp) |
---|
2126 | (*carp_linkstate_p)(ifp); |
---|
2127 | rt_ifmsg(ifp); |
---|
2128 | } |
---|
2129 | |
---|
2130 | /* |
---|
2131 | * Mark an interface up and notify protocols of |
---|
2132 | * the transition. |
---|
2133 | */ |
---|
2134 | static void |
---|
2135 | if_route(struct ifnet *ifp, int flag, int fam) |
---|
2136 | { |
---|
2137 | struct ifaddr *ifa; |
---|
2138 | |
---|
2139 | KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); |
---|
2140 | |
---|
2141 | ifp->if_flags |= flag; |
---|
2142 | getmicrotime(&ifp->if_lastchange); |
---|
2143 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) |
---|
2144 | if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) |
---|
2145 | pfctlinput(PRC_IFUP, ifa->ifa_addr); |
---|
2146 | if (ifp->if_carp) |
---|
2147 | (*carp_linkstate_p)(ifp); |
---|
2148 | rt_ifmsg(ifp); |
---|
2149 | #ifdef INET6 |
---|
2150 | in6_if_up(ifp); |
---|
2151 | #endif |
---|
2152 | } |
---|
2153 | |
---|
2154 | void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */ |
---|
2155 | void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ |
---|
2156 | struct ifnet *(*vlan_trunkdev_p)(struct ifnet *); |
---|
2157 | struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t); |
---|
2158 | int (*vlan_tag_p)(struct ifnet *, uint16_t *); |
---|
2159 | int (*vlan_setcookie_p)(struct ifnet *, void *); |
---|
2160 | void *(*vlan_cookie_p)(struct ifnet *); |
---|
2161 | |
---|
2162 | /* |
---|
2163 | * Handle a change in the interface link state. To avoid LORs |
---|
2164 | * between driver lock and upper layer locks, as well as possible |
---|
2165 | * recursions, we post event to taskqueue, and all job |
---|
2166 | * is done in static do_link_state_change(). |
---|
2167 | */ |
---|
2168 | void |
---|
2169 | if_link_state_change(struct ifnet *ifp, int link_state) |
---|
2170 | { |
---|
2171 | /* Return if state hasn't changed. */ |
---|
2172 | if (ifp->if_link_state == link_state) |
---|
2173 | return; |
---|
2174 | |
---|
2175 | ifp->if_link_state = link_state; |
---|
2176 | |
---|
2177 | taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); |
---|
2178 | } |
---|
2179 | |
---|
2180 | static void |
---|
2181 | do_link_state_change(void *arg, int pending) |
---|
2182 | { |
---|
2183 | struct ifnet *ifp = (struct ifnet *)arg; |
---|
2184 | int link_state = ifp->if_link_state; |
---|
2185 | CURVNET_SET(ifp->if_vnet); |
---|
2186 | |
---|
2187 | /* Notify that the link state has changed. */ |
---|
2188 | rt_ifmsg(ifp); |
---|
2189 | if (ifp->if_vlantrunk != NULL) |
---|
2190 | (*vlan_link_state_p)(ifp); |
---|
2191 | |
---|
2192 | if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && |
---|
2193 | ifp->if_l2com != NULL) |
---|
2194 | (*ng_ether_link_state_p)(ifp, link_state); |
---|
2195 | if (ifp->if_carp) |
---|
2196 | (*carp_linkstate_p)(ifp); |
---|
2197 | if (ifp->if_bridge) |
---|
2198 | (*bridge_linkstate_p)(ifp); |
---|
2199 | if (ifp->if_lagg) |
---|
2200 | (*lagg_linkstate_p)(ifp, link_state); |
---|
2201 | |
---|
2202 | if (IS_DEFAULT_VNET(curvnet)) |
---|
2203 | devctl_notify("IFNET", ifp->if_xname, |
---|
2204 | (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", |
---|
2205 | NULL); |
---|
2206 | if (pending > 1) |
---|
2207 | if_printf(ifp, "%d link states coalesced\n", pending); |
---|
2208 | if (log_link_state_change) |
---|
2209 | log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, |
---|
2210 | (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); |
---|
2211 | EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state); |
---|
2212 | CURVNET_RESTORE(); |
---|
2213 | } |
---|
2214 | |
---|
2215 | /* |
---|
2216 | * Mark an interface down and notify protocols of |
---|
2217 | * the transition. |
---|
2218 | */ |
---|
2219 | void |
---|
2220 | if_down(struct ifnet *ifp) |
---|
2221 | { |
---|
2222 | |
---|
2223 | EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN); |
---|
2224 | if_unroute(ifp, IFF_UP, AF_UNSPEC); |
---|
2225 | } |
---|
2226 | |
---|
2227 | /* |
---|
2228 | * Mark an interface up and notify protocols of |
---|
2229 | * the transition. |
---|
2230 | */ |
---|
2231 | void |
---|
2232 | if_up(struct ifnet *ifp) |
---|
2233 | { |
---|
2234 | |
---|
2235 | if_route(ifp, IFF_UP, AF_UNSPEC); |
---|
2236 | EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP); |
---|
2237 | } |
---|
2238 | |
---|
2239 | /* |
---|
2240 | * Flush an interface queue. |
---|
2241 | */ |
---|
2242 | void |
---|
2243 | if_qflush(struct ifnet *ifp) |
---|
2244 | { |
---|
2245 | struct mbuf *m, *n; |
---|
2246 | struct ifaltq *ifq; |
---|
2247 | |
---|
2248 | ifq = &ifp->if_snd; |
---|
2249 | IFQ_LOCK(ifq); |
---|
2250 | #ifdef ALTQ |
---|
2251 | if (ALTQ_IS_ENABLED(ifq)) |
---|
2252 | ALTQ_PURGE(ifq); |
---|
2253 | #endif |
---|
2254 | n = ifq->ifq_head; |
---|
2255 | while ((m = n) != NULL) { |
---|
2256 | n = m->m_nextpkt; |
---|
2257 | m_freem(m); |
---|
2258 | } |
---|
2259 | ifq->ifq_head = 0; |
---|
2260 | ifq->ifq_tail = 0; |
---|
2261 | ifq->ifq_len = 0; |
---|
2262 | IFQ_UNLOCK(ifq); |
---|
2263 | } |
---|
2264 | |
---|
2265 | /* |
---|
2266 | * Map interface name to interface structure pointer, with or without |
---|
2267 | * returning a reference. |
---|
2268 | */ |
---|
2269 | struct ifnet * |
---|
2270 | ifunit_ref(const char *name) |
---|
2271 | { |
---|
2272 | struct ifnet *ifp; |
---|
2273 | |
---|
2274 | IFNET_RLOCK_NOSLEEP(); |
---|
2275 | TAILQ_FOREACH(ifp, &V_ifnet, if_link) { |
---|
2276 | if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 && |
---|
2277 | !(ifp->if_flags & IFF_DYING)) |
---|
2278 | break; |
---|
2279 | } |
---|
2280 | if (ifp != NULL) |
---|
2281 | if_ref(ifp); |
---|
2282 | IFNET_RUNLOCK_NOSLEEP(); |
---|
2283 | return (ifp); |
---|
2284 | } |
---|
2285 | |
---|
2286 | struct ifnet * |
---|
2287 | ifunit(const char *name) |
---|
2288 | { |
---|
2289 | struct ifnet *ifp; |
---|
2290 | |
---|
2291 | IFNET_RLOCK_NOSLEEP(); |
---|
2292 | TAILQ_FOREACH(ifp, &V_ifnet, if_link) { |
---|
2293 | if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) |
---|
2294 | break; |
---|
2295 | } |
---|
2296 | IFNET_RUNLOCK_NOSLEEP(); |
---|
2297 | return (ifp); |
---|
2298 | } |
---|
2299 | |
---|
2300 | /* |
---|
2301 | * Hardware specific interface ioctls. |
---|
2302 | */ |
---|
2303 | static int |
---|
2304 | ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) |
---|
2305 | { |
---|
2306 | struct ifreq *ifr; |
---|
2307 | int error = 0, do_ifup = 0; |
---|
2308 | int new_flags, temp_flags; |
---|
2309 | size_t namelen, onamelen; |
---|
2310 | size_t descrlen; |
---|
2311 | char *descrbuf, *odescrbuf; |
---|
2312 | char new_name[IFNAMSIZ]; |
---|
2313 | struct ifaddr *ifa; |
---|
2314 | struct sockaddr_dl *sdl; |
---|
2315 | |
---|
2316 | ifr = (struct ifreq *)data; |
---|
2317 | switch (cmd) { |
---|
2318 | case SIOCGIFINDEX: |
---|
2319 | ifr->ifr_index = ifp->if_index; |
---|
2320 | break; |
---|
2321 | |
---|
2322 | case SIOCGIFFLAGS: |
---|
2323 | temp_flags = ifp->if_flags | ifp->if_drv_flags; |
---|
2324 | ifr->ifr_flags = temp_flags & 0xffff; |
---|
2325 | ifr->ifr_flagshigh = temp_flags >> 16; |
---|
2326 | break; |
---|
2327 | |
---|
2328 | case SIOCGIFCAP: |
---|
2329 | ifr->ifr_reqcap = ifp->if_capabilities; |
---|
2330 | ifr->ifr_curcap = ifp->if_capenable; |
---|
2331 | break; |
---|
2332 | |
---|
2333 | #ifdef MAC |
---|
2334 | case SIOCGIFMAC: |
---|
2335 | error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); |
---|
2336 | break; |
---|
2337 | #endif |
---|
2338 | |
---|
2339 | case SIOCGIFMETRIC: |
---|
2340 | ifr->ifr_metric = ifp->if_metric; |
---|
2341 | break; |
---|
2342 | |
---|
2343 | case SIOCGIFMTU: |
---|
2344 | ifr->ifr_mtu = ifp->if_mtu; |
---|
2345 | break; |
---|
2346 | |
---|
2347 | case SIOCGIFPHYS: |
---|
2348 | /* XXXGL: did this ever worked? */ |
---|
2349 | ifr->ifr_phys = 0; |
---|
2350 | break; |
---|
2351 | |
---|
2352 | case SIOCGIFDESCR: |
---|
2353 | error = 0; |
---|
2354 | sx_slock(&ifdescr_sx); |
---|
2355 | if (ifp->if_description == NULL) |
---|
2356 | error = ENOMSG; |
---|
2357 | else { |
---|
2358 | /* space for terminating nul */ |
---|
2359 | descrlen = strlen(ifp->if_description) + 1; |
---|
2360 | if (ifr->ifr_buffer.length < descrlen) |
---|
2361 | ifr->ifr_buffer.buffer = NULL; |
---|
2362 | else |
---|
2363 | error = copyout(ifp->if_description, |
---|
2364 | ifr->ifr_buffer.buffer, descrlen); |
---|
2365 | ifr->ifr_buffer.length = descrlen; |
---|
2366 | } |
---|
2367 | sx_sunlock(&ifdescr_sx); |
---|
2368 | break; |
---|
2369 | |
---|
2370 | case SIOCSIFDESCR: |
---|
2371 | error = priv_check(td, PRIV_NET_SETIFDESCR); |
---|
2372 | if (error) |
---|
2373 | return (error); |
---|
2374 | |
---|
2375 | /* |
---|
2376 | * Copy only (length-1) bytes to make sure that |
---|
2377 | * if_description is always nul terminated. The |
---|
2378 | * length parameter is supposed to count the |
---|
2379 | * terminating nul in. |
---|
2380 | */ |
---|
2381 | if (ifr->ifr_buffer.length > ifdescr_maxlen) |
---|
2382 | return (ENAMETOOLONG); |
---|
2383 | else if (ifr->ifr_buffer.length == 0) |
---|
2384 | descrbuf = NULL; |
---|
2385 | else { |
---|
2386 | descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR, |
---|
2387 | M_WAITOK | M_ZERO); |
---|
2388 | error = copyin(ifr->ifr_buffer.buffer, descrbuf, |
---|
2389 | ifr->ifr_buffer.length - 1); |
---|
2390 | if (error) { |
---|
2391 | free(descrbuf, M_IFDESCR); |
---|
2392 | break; |
---|
2393 | } |
---|
2394 | } |
---|
2395 | |
---|
2396 | sx_xlock(&ifdescr_sx); |
---|
2397 | odescrbuf = ifp->if_description; |
---|
2398 | ifp->if_description = descrbuf; |
---|
2399 | sx_xunlock(&ifdescr_sx); |
---|
2400 | |
---|
2401 | getmicrotime(&ifp->if_lastchange); |
---|
2402 | free(odescrbuf, M_IFDESCR); |
---|
2403 | break; |
---|
2404 | |
---|
2405 | case SIOCGIFFIB: |
---|
2406 | ifr->ifr_fib = ifp->if_fib; |
---|
2407 | break; |
---|
2408 | |
---|
2409 | case SIOCSIFFIB: |
---|
2410 | error = priv_check(td, PRIV_NET_SETIFFIB); |
---|
2411 | if (error) |
---|
2412 | return (error); |
---|
2413 | if (ifr->ifr_fib >= rt_numfibs) |
---|
2414 | return (EINVAL); |
---|
2415 | |
---|
2416 | ifp->if_fib = ifr->ifr_fib; |
---|
2417 | break; |
---|
2418 | |
---|
2419 | case SIOCSIFFLAGS: |
---|
2420 | error = priv_check(td, PRIV_NET_SETIFFLAGS); |
---|
2421 | if (error) |
---|
2422 | return (error); |
---|
2423 | /* |
---|
2424 | * Currently, no driver owned flags pass the IFF_CANTCHANGE |
---|
2425 | * check, so we don't need special handling here yet. |
---|
2426 | */ |
---|
2427 | new_flags = (ifr->ifr_flags & 0xffff) | |
---|
2428 | (ifr->ifr_flagshigh << 16); |
---|
2429 | if (ifp->if_flags & IFF_UP && |
---|
2430 | (new_flags & IFF_UP) == 0) { |
---|
2431 | if_down(ifp); |
---|
2432 | } else if (new_flags & IFF_UP && |
---|
2433 | (ifp->if_flags & IFF_UP) == 0) { |
---|
2434 | do_ifup = 1; |
---|
2435 | } |
---|
2436 | /* See if permanently promiscuous mode bit is about to flip */ |
---|
2437 | if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { |
---|
2438 | if (new_flags & IFF_PPROMISC) |
---|
2439 | ifp->if_flags |= IFF_PROMISC; |
---|
2440 | else if (ifp->if_pcount == 0) |
---|
2441 | ifp->if_flags &= ~IFF_PROMISC; |
---|
2442 | if (log_promisc_mode_change) |
---|
2443 | log(LOG_INFO, "%s: permanently promiscuous mode %s\n", |
---|
2444 | ifp->if_xname, |
---|
2445 | ((new_flags & IFF_PPROMISC) ? |
---|
2446 | "enabled" : "disabled")); |
---|
2447 | } |
---|
2448 | ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | |
---|
2449 | (new_flags &~ IFF_CANTCHANGE); |
---|
2450 | if (ifp->if_ioctl) { |
---|
2451 | (void) (*ifp->if_ioctl)(ifp, cmd, data); |
---|
2452 | } |
---|
2453 | if (do_ifup) |
---|
2454 | if_up(ifp); |
---|
2455 | getmicrotime(&ifp->if_lastchange); |
---|
2456 | break; |
---|
2457 | |
---|
2458 | case SIOCSIFCAP: |
---|
2459 | error = priv_check(td, PRIV_NET_SETIFCAP); |
---|
2460 | if (error) |
---|
2461 | return (error); |
---|
2462 | if (ifp->if_ioctl == NULL) |
---|
2463 | return (EOPNOTSUPP); |
---|
2464 | if (ifr->ifr_reqcap & ~ifp->if_capabilities) |
---|
2465 | return (EINVAL); |
---|
2466 | error = (*ifp->if_ioctl)(ifp, cmd, data); |
---|
2467 | if (error == 0) |
---|
2468 | getmicrotime(&ifp->if_lastchange); |
---|
2469 | break; |
---|
2470 | |
---|
2471 | #ifdef MAC |
---|
2472 | case SIOCSIFMAC: |
---|
2473 | error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); |
---|
2474 | break; |
---|
2475 | #endif |
---|
2476 | |
---|
2477 | case SIOCSIFNAME: |
---|
2478 | error = priv_check(td, PRIV_NET_SETIFNAME); |
---|
2479 | if (error) |
---|
2480 | return (error); |
---|
2481 | error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); |
---|
2482 | if (error != 0) |
---|
2483 | return (error); |
---|
2484 | if (new_name[0] == '\0') |
---|
2485 | return (EINVAL); |
---|
2486 | if (new_name[IFNAMSIZ-1] != '\0') { |
---|
2487 | new_name[IFNAMSIZ-1] = '\0'; |
---|
2488 | if (strlen(new_name) == IFNAMSIZ-1) |
---|
2489 | return (EINVAL); |
---|
2490 | } |
---|
2491 | if (ifunit(new_name) != NULL) |
---|
2492 | return (EEXIST); |
---|
2493 | |
---|
2494 | /* |
---|
2495 | * XXX: Locking. Nothing else seems to lock if_flags, |
---|
2496 | * and there are numerous other races with the |
---|
2497 | * ifunit() checks not being atomic with namespace |
---|
2498 | * changes (renames, vmoves, if_attach, etc). |
---|
2499 | */ |
---|
2500 | ifp->if_flags |= IFF_RENAMING; |
---|
2501 | |
---|
2502 | /* Announce the departure of the interface. */ |
---|
2503 | rt_ifannouncemsg(ifp, IFAN_DEPARTURE); |
---|
2504 | EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); |
---|
2505 | |
---|
2506 | log(LOG_INFO, "%s: changing name to '%s'\n", |
---|
2507 | ifp->if_xname, new_name); |
---|
2508 | |
---|
2509 | IF_ADDR_WLOCK(ifp); |
---|
2510 | strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); |
---|
2511 | ifa = ifp->if_addr; |
---|
2512 | sdl = (struct sockaddr_dl *)ifa->ifa_addr; |
---|
2513 | namelen = strlen(new_name); |
---|
2514 | onamelen = sdl->sdl_nlen; |
---|
2515 | /* |
---|
2516 | * Move the address if needed. This is safe because we |
---|
2517 | * allocate space for a name of length IFNAMSIZ when we |
---|
2518 | * create this in if_attach(). |
---|
2519 | */ |
---|
2520 | if (namelen != onamelen) { |
---|
2521 | bcopy(sdl->sdl_data + onamelen, |
---|
2522 | sdl->sdl_data + namelen, sdl->sdl_alen); |
---|
2523 | } |
---|
2524 | bcopy(new_name, sdl->sdl_data, namelen); |
---|
2525 | sdl->sdl_nlen = namelen; |
---|
2526 | sdl = (struct sockaddr_dl *)ifa->ifa_netmask; |
---|
2527 | bzero(sdl->sdl_data, onamelen); |
---|
2528 | while (namelen != 0) |
---|
2529 | sdl->sdl_data[--namelen] = 0xff; |
---|
2530 | IF_ADDR_WUNLOCK(ifp); |
---|
2531 | |
---|
2532 | EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); |
---|
2533 | /* Announce the return of the interface. */ |
---|
2534 | rt_ifannouncemsg(ifp, IFAN_ARRIVAL); |
---|
2535 | |
---|
2536 | ifp->if_flags &= ~IFF_RENAMING; |
---|
2537 | break; |
---|
2538 | |
---|
2539 | #ifdef VIMAGE |
---|
2540 | case SIOCSIFVNET: |
---|
2541 | error = priv_check(td, PRIV_NET_SETIFVNET); |
---|
2542 | if (error) |
---|
2543 | return (error); |
---|
2544 | error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid); |
---|
2545 | break; |
---|
2546 | #endif |
---|
2547 | |
---|
2548 | case SIOCSIFMETRIC: |
---|
2549 | error = priv_check(td, PRIV_NET_SETIFMETRIC); |
---|
2550 | if (error) |
---|
2551 | return (error); |
---|
2552 | ifp->if_metric = ifr->ifr_metric; |
---|
2553 | getmicrotime(&ifp->if_lastchange); |
---|
2554 | break; |
---|
2555 | |
---|
2556 | case SIOCSIFPHYS: |
---|
2557 | error = priv_check(td, PRIV_NET_SETIFPHYS); |
---|
2558 | if (error) |
---|
2559 | return (error); |
---|
2560 | if (ifp->if_ioctl == NULL) |
---|
2561 | return (EOPNOTSUPP); |
---|
2562 | error = (*ifp->if_ioctl)(ifp, cmd, data); |
---|
2563 | if (error == 0) |
---|
2564 | getmicrotime(&ifp->if_lastchange); |
---|
2565 | break; |
---|
2566 | |
---|
2567 | case SIOCSIFMTU: |
---|
2568 | { |
---|
2569 | u_long oldmtu = ifp->if_mtu; |
---|
2570 | |
---|
2571 | error = priv_check(td, PRIV_NET_SETIFMTU); |
---|
2572 | if (error) |
---|
2573 | return (error); |
---|
2574 | if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) |
---|
2575 | return (EINVAL); |
---|
2576 | if (ifp->if_ioctl == NULL) |
---|
2577 | return (EOPNOTSUPP); |
---|
2578 | error = (*ifp->if_ioctl)(ifp, cmd, data); |
---|
2579 | if (error == 0) { |
---|
2580 | getmicrotime(&ifp->if_lastchange); |
---|
2581 | rt_ifmsg(ifp); |
---|
2582 | } |
---|
2583 | /* |
---|
2584 | * If the link MTU changed, do network layer specific procedure. |
---|
2585 | */ |
---|
2586 | if (ifp->if_mtu != oldmtu) { |
---|
2587 | #ifdef INET6 |
---|
2588 | nd6_setmtu(ifp); |
---|
2589 | #endif |
---|
2590 | rt_updatemtu(ifp); |
---|
2591 | } |
---|
2592 | break; |
---|
2593 | } |
---|
2594 | |
---|
2595 | case SIOCADDMULTI: |
---|
2596 | case SIOCDELMULTI: |
---|
2597 | if (cmd == SIOCADDMULTI) |
---|
2598 | error = priv_check(td, PRIV_NET_ADDMULTI); |
---|
2599 | else |
---|
2600 | error = priv_check(td, PRIV_NET_DELMULTI); |
---|
2601 | if (error) |
---|
2602 | return (error); |
---|
2603 | |
---|
2604 | /* Don't allow group membership on non-multicast interfaces. */ |
---|
2605 | if ((ifp->if_flags & IFF_MULTICAST) == 0) |
---|
2606 | return (EOPNOTSUPP); |
---|
2607 | |
---|
2608 | /* Don't let users screw up protocols' entries. */ |
---|
2609 | if (ifr->ifr_addr.sa_family != AF_LINK) |
---|
2610 | return (EINVAL); |
---|
2611 | |
---|
2612 | if (cmd == SIOCADDMULTI) { |
---|
2613 | struct ifmultiaddr *ifma; |
---|
2614 | |
---|
2615 | /* |
---|
2616 | * Userland is only permitted to join groups once |
---|
2617 | * via the if_addmulti() KPI, because it cannot hold |
---|
2618 | * struct ifmultiaddr * between calls. It may also |
---|
2619 | * lose a race while we check if the membership |
---|
2620 | * already exists. |
---|
2621 | */ |
---|
2622 | IF_ADDR_RLOCK(ifp); |
---|
2623 | ifma = if_findmulti(ifp, &ifr->ifr_addr); |
---|
2624 | IF_ADDR_RUNLOCK(ifp); |
---|
2625 | if (ifma != NULL) |
---|
2626 | error = EADDRINUSE; |
---|
2627 | else |
---|
2628 | error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); |
---|
2629 | } else { |
---|
2630 | error = if_delmulti(ifp, &ifr->ifr_addr); |
---|
2631 | } |
---|
2632 | if (error == 0) |
---|
2633 | getmicrotime(&ifp->if_lastchange); |
---|
2634 | break; |
---|
2635 | |
---|
2636 | case SIOCSIFPHYADDR: |
---|
2637 | case SIOCDIFPHYADDR: |
---|
2638 | #ifdef INET6 |
---|
2639 | case SIOCSIFPHYADDR_IN6: |
---|
2640 | #endif |
---|
2641 | case SIOCSIFMEDIA: |
---|
2642 | case SIOCSIFGENERIC: |
---|
2643 | error = priv_check(td, PRIV_NET_HWIOCTL); |
---|
2644 | if (error) |
---|
2645 | return (error); |
---|
2646 | if (ifp->if_ioctl == NULL) |
---|
2647 | return (EOPNOTSUPP); |
---|
2648 | error = (*ifp->if_ioctl)(ifp, cmd, data); |
---|
2649 | if (error == 0) |
---|
2650 | getmicrotime(&ifp->if_lastchange); |
---|
2651 | break; |
---|
2652 | |
---|
2653 | case SIOCGIFSTATUS: |
---|
2654 | case SIOCGIFPSRCADDR: |
---|
2655 | case SIOCGIFPDSTADDR: |
---|
2656 | case SIOCGIFMEDIA: |
---|
2657 | case SIOCGIFXMEDIA: |
---|
2658 | case SIOCGIFGENERIC: |
---|
2659 | if (ifp->if_ioctl == NULL) |
---|
2660 | return (EOPNOTSUPP); |
---|
2661 | error = (*ifp->if_ioctl)(ifp, cmd, data); |
---|
2662 | break; |
---|
2663 | |
---|
2664 | case SIOCSIFLLADDR: |
---|
2665 | error = priv_check(td, PRIV_NET_SETLLADDR); |
---|
2666 | if (error) |
---|
2667 | return (error); |
---|
2668 | error = if_setlladdr(ifp, |
---|
2669 | ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); |
---|
2670 | break; |
---|
2671 | |
---|
2672 | case SIOCAIFGROUP: |
---|
2673 | { |
---|
2674 | struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; |
---|
2675 | |
---|
2676 | error = priv_check(td, PRIV_NET_ADDIFGROUP); |
---|
2677 | if (error) |
---|
2678 | return (error); |
---|
2679 | if ((error = if_addgroup(ifp, ifgr->ifgr_group))) |
---|
2680 | return (error); |
---|
2681 | break; |
---|
2682 | } |
---|
2683 | |
---|
2684 | case SIOCGIFGROUP: |
---|
2685 | if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) |
---|
2686 | return (error); |
---|
2687 | break; |
---|
2688 | |
---|
2689 | case SIOCDIFGROUP: |
---|
2690 | { |
---|
2691 | struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; |
---|
2692 | |
---|
2693 | error = priv_check(td, PRIV_NET_DELIFGROUP); |
---|
2694 | if (error) |
---|
2695 | return (error); |
---|
2696 | if ((error = if_delgroup(ifp, ifgr->ifgr_group))) |
---|
2697 | return (error); |
---|
2698 | break; |
---|
2699 | } |
---|
2700 | |
---|
2701 | #ifdef __rtems__ |
---|
2702 | case RTEMS_SIOSIFINPUT: |
---|
2703 | if (ifp->if_input_arg == NULL) { |
---|
2704 | struct rtems_ifinputreq *ifipfr; |
---|
2705 | |
---|
2706 | ifipfr = (struct rtems_ifinputreq *)data; |
---|
2707 | ifipfr->old_if_input = ifp->if_input; |
---|
2708 | ifp->if_input_arg = ifipfr->arg; |
---|
2709 | (*ifipfr->init)(ifp, ifipfr->arg); |
---|
2710 | ifp->if_input = ifipfr->new_if_input; |
---|
2711 | error = 0; |
---|
2712 | } else { |
---|
2713 | return (EEXIST); |
---|
2714 | } |
---|
2715 | break; |
---|
2716 | #endif /* __rtems__ */ |
---|
2717 | default: |
---|
2718 | error = ENOIOCTL; |
---|
2719 | break; |
---|
2720 | } |
---|
2721 | return (error); |
---|
2722 | } |
---|
2723 | |
---|
2724 | #ifdef COMPAT_FREEBSD32 |
---|
2725 | struct ifconf32 { |
---|
2726 | int32_t ifc_len; |
---|
2727 | union { |
---|
2728 | uint32_t ifcu_buf; |
---|
2729 | uint32_t ifcu_req; |
---|
2730 | } ifc_ifcu; |
---|
2731 | }; |
---|
2732 | #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32) |
---|
2733 | #endif |
---|
2734 | |
---|
2735 | /* |
---|
2736 | * Interface ioctls. |
---|
2737 | */ |
---|
2738 | int |
---|
2739 | ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) |
---|
2740 | { |
---|
2741 | struct ifnet *ifp; |
---|
2742 | struct ifreq *ifr; |
---|
2743 | int error; |
---|
2744 | int oif_flags; |
---|
2745 | #ifdef VIMAGE |
---|
2746 | int shutdown; |
---|
2747 | #endif |
---|
2748 | |
---|
2749 | CURVNET_SET(so->so_vnet); |
---|
2750 | #ifdef VIMAGE |
---|
2751 | /* Make sure the VNET is stable. */ |
---|
2752 | shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET && |
---|
2753 | so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0; |
---|
2754 | if (shutdown) { |
---|
2755 | CURVNET_RESTORE(); |
---|
2756 | return (EBUSY); |
---|
2757 | } |
---|
2758 | #endif |
---|
2759 | |
---|
2760 | |
---|
2761 | switch (cmd) { |
---|
2762 | case SIOCGIFCONF: |
---|
2763 | error = ifconf(cmd, data); |
---|
2764 | CURVNET_RESTORE(); |
---|
2765 | return (error); |
---|
2766 | |
---|
2767 | #ifdef COMPAT_FREEBSD32 |
---|
2768 | case SIOCGIFCONF32: |
---|
2769 | { |
---|
2770 | struct ifconf32 *ifc32; |
---|
2771 | struct ifconf ifc; |
---|
2772 | |
---|
2773 | ifc32 = (struct ifconf32 *)data; |
---|
2774 | ifc.ifc_len = ifc32->ifc_len; |
---|
2775 | ifc.ifc_buf = PTRIN(ifc32->ifc_buf); |
---|
2776 | |
---|
2777 | error = ifconf(SIOCGIFCONF, (void *)&ifc); |
---|
2778 | CURVNET_RESTORE(); |
---|
2779 | if (error == 0) |
---|
2780 | ifc32->ifc_len = ifc.ifc_len; |
---|
2781 | return (error); |
---|
2782 | } |
---|
2783 | #endif |
---|
2784 | } |
---|
2785 | ifr = (struct ifreq *)data; |
---|
2786 | |
---|
2787 | switch (cmd) { |
---|
2788 | #ifdef VIMAGE |
---|
2789 | case SIOCSIFRVNET: |
---|
2790 | error = priv_check(td, PRIV_NET_SETIFVNET); |
---|
2791 | if (error == 0) |
---|
2792 | error = if_vmove_reclaim(td, ifr->ifr_name, |
---|
2793 | ifr->ifr_jid); |
---|
2794 | CURVNET_RESTORE(); |
---|
2795 | return (error); |
---|
2796 | #endif |
---|
2797 | case SIOCIFCREATE: |
---|
2798 | case SIOCIFCREATE2: |
---|
2799 | error = priv_check(td, PRIV_NET_IFCREATE); |
---|
2800 | if (error == 0) |
---|
2801 | error = if_clone_create(ifr->ifr_name, |
---|
2802 | sizeof(ifr->ifr_name), |
---|
2803 | cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL); |
---|
2804 | CURVNET_RESTORE(); |
---|
2805 | return (error); |
---|
2806 | case SIOCIFDESTROY: |
---|
2807 | error = priv_check(td, PRIV_NET_IFDESTROY); |
---|
2808 | if (error == 0) |
---|
2809 | error = if_clone_destroy(ifr->ifr_name); |
---|
2810 | CURVNET_RESTORE(); |
---|
2811 | return (error); |
---|
2812 | |
---|
2813 | case SIOCIFGCLONERS: |
---|
2814 | error = if_clone_list((struct if_clonereq *)data); |
---|
2815 | CURVNET_RESTORE(); |
---|
2816 | return (error); |
---|
2817 | case SIOCGIFGMEMB: |
---|
2818 | error = if_getgroupmembers((struct ifgroupreq *)data); |
---|
2819 | CURVNET_RESTORE(); |
---|
2820 | return (error); |
---|
2821 | #if defined(INET) || defined(INET6) |
---|
2822 | case SIOCSVH: |
---|
2823 | case SIOCGVH: |
---|
2824 | if (carp_ioctl_p == NULL) |
---|
2825 | error = EPROTONOSUPPORT; |
---|
2826 | else |
---|
2827 | error = (*carp_ioctl_p)(ifr, cmd, td); |
---|
2828 | CURVNET_RESTORE(); |
---|
2829 | return (error); |
---|
2830 | #endif |
---|
2831 | } |
---|
2832 | |
---|
2833 | ifp = ifunit_ref(ifr->ifr_name); |
---|
2834 | if (ifp == NULL) { |
---|
2835 | CURVNET_RESTORE(); |
---|
2836 | return (ENXIO); |
---|
2837 | } |
---|
2838 | |
---|
2839 | error = ifhwioctl(cmd, ifp, data, td); |
---|
2840 | if (error != ENOIOCTL) { |
---|
2841 | if_rele(ifp); |
---|
2842 | CURVNET_RESTORE(); |
---|
2843 | return (error); |
---|
2844 | } |
---|
2845 | |
---|
2846 | oif_flags = ifp->if_flags; |
---|
2847 | if (so->so_proto == NULL) { |
---|
2848 | if_rele(ifp); |
---|
2849 | CURVNET_RESTORE(); |
---|
2850 | return (EOPNOTSUPP); |
---|
2851 | } |
---|
2852 | |
---|
2853 | /* |
---|
2854 | * Pass the request on to the socket control method, and if the |
---|
2855 | * latter returns EOPNOTSUPP, directly to the interface. |
---|
2856 | * |
---|
2857 | * Make an exception for the legacy SIOCSIF* requests. Drivers |
---|
2858 | * trust SIOCSIFADDR et al to come from an already privileged |
---|
2859 | * layer, and do not perform any credentials checks or input |
---|
2860 | * validation. |
---|
2861 | */ |
---|
2862 | error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, |
---|
2863 | ifp, td)); |
---|
2864 | if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL && |
---|
2865 | cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR && |
---|
2866 | cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK) |
---|
2867 | error = (*ifp->if_ioctl)(ifp, cmd, data); |
---|
2868 | |
---|
2869 | if ((oif_flags ^ ifp->if_flags) & IFF_UP) { |
---|
2870 | #ifdef INET6 |
---|
2871 | if (ifp->if_flags & IFF_UP) |
---|
2872 | in6_if_up(ifp); |
---|
2873 | #endif |
---|
2874 | } |
---|
2875 | if_rele(ifp); |
---|
2876 | CURVNET_RESTORE(); |
---|
2877 | return (error); |
---|
2878 | } |
---|
2879 | |
---|
2880 | /* |
---|
2881 | * The code common to handling reference counted flags, |
---|
2882 | * e.g., in ifpromisc() and if_allmulti(). |
---|
2883 | * The "pflag" argument can specify a permanent mode flag to check, |
---|
2884 | * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. |
---|
2885 | * |
---|
2886 | * Only to be used on stack-owned flags, not driver-owned flags. |
---|
2887 | */ |
---|
2888 | static int |
---|
2889 | if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) |
---|
2890 | { |
---|
2891 | struct ifreq ifr; |
---|
2892 | int error; |
---|
2893 | int oldflags, oldcount; |
---|
2894 | |
---|
2895 | /* Sanity checks to catch programming errors */ |
---|
2896 | KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, |
---|
2897 | ("%s: setting driver-owned flag %d", __func__, flag)); |
---|
2898 | |
---|
2899 | if (onswitch) |
---|
2900 | KASSERT(*refcount >= 0, |
---|
2901 | ("%s: increment negative refcount %d for flag %d", |
---|
2902 | __func__, *refcount, flag)); |
---|
2903 | else |
---|
2904 | KASSERT(*refcount > 0, |
---|
2905 | ("%s: decrement non-positive refcount %d for flag %d", |
---|
2906 | __func__, *refcount, flag)); |
---|
2907 | |
---|
2908 | /* In case this mode is permanent, just touch refcount */ |
---|
2909 | if (ifp->if_flags & pflag) { |
---|
2910 | *refcount += onswitch ? 1 : -1; |
---|
2911 | return (0); |
---|
2912 | } |
---|
2913 | |
---|
2914 | /* Save ifnet parameters for if_ioctl() may fail */ |
---|
2915 | oldcount = *refcount; |
---|
2916 | oldflags = ifp->if_flags; |
---|
2917 | |
---|
2918 | /* |
---|
2919 | * See if we aren't the only and touching refcount is enough. |
---|
2920 | * Actually toggle interface flag if we are the first or last. |
---|
2921 | */ |
---|
2922 | if (onswitch) { |
---|
2923 | if ((*refcount)++) |
---|
2924 | return (0); |
---|
2925 | ifp->if_flags |= flag; |
---|
2926 | } else { |
---|
2927 | if (--(*refcount)) |
---|
2928 | return (0); |
---|
2929 | ifp->if_flags &= ~flag; |
---|
2930 | } |
---|
2931 | |
---|
2932 | /* Call down the driver since we've changed interface flags */ |
---|
2933 | if (ifp->if_ioctl == NULL) { |
---|
2934 | error = EOPNOTSUPP; |
---|
2935 | goto recover; |
---|
2936 | } |
---|
2937 | ifr.ifr_flags = ifp->if_flags & 0xffff; |
---|
2938 | ifr.ifr_flagshigh = ifp->if_flags >> 16; |
---|
2939 | error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); |
---|
2940 | if (error) |
---|
2941 | goto recover; |
---|
2942 | /* Notify userland that interface flags have changed */ |
---|
2943 | rt_ifmsg(ifp); |
---|
2944 | return (0); |
---|
2945 | |
---|
2946 | recover: |
---|
2947 | /* Recover after driver error */ |
---|
2948 | *refcount = oldcount; |
---|
2949 | ifp->if_flags = oldflags; |
---|
2950 | return (error); |
---|
2951 | } |
---|
2952 | |
---|
2953 | /* |
---|
2954 | * Set/clear promiscuous mode on interface ifp based on the truth value |
---|
2955 | * of pswitch. The calls are reference counted so that only the first |
---|
2956 | * "on" request actually has an effect, as does the final "off" request. |
---|
2957 | * Results are undefined if the "off" and "on" requests are not matched. |
---|
2958 | */ |
---|
2959 | int |
---|
2960 | ifpromisc(struct ifnet *ifp, int pswitch) |
---|
2961 | { |
---|
2962 | int error; |
---|
2963 | int oldflags = ifp->if_flags; |
---|
2964 | |
---|
2965 | error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, |
---|
2966 | &ifp->if_pcount, pswitch); |
---|
2967 | /* If promiscuous mode status has changed, log a message */ |
---|
2968 | if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) && |
---|
2969 | log_promisc_mode_change) |
---|
2970 | log(LOG_INFO, "%s: promiscuous mode %s\n", |
---|
2971 | ifp->if_xname, |
---|
2972 | (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); |
---|
2973 | return (error); |
---|
2974 | } |
---|
2975 | |
---|
2976 | /* |
---|
2977 | * Return interface configuration |
---|
2978 | * of system. List may be used |
---|
2979 | * in later ioctl's (above) to get |
---|
2980 | * other information. |
---|
2981 | */ |
---|
2982 | /*ARGSUSED*/ |
---|
2983 | static int |
---|
2984 | ifconf(u_long cmd, caddr_t data) |
---|
2985 | { |
---|
2986 | struct ifconf *ifc = (struct ifconf *)data; |
---|
2987 | struct ifnet *ifp; |
---|
2988 | struct ifaddr *ifa; |
---|
2989 | struct ifreq ifr; |
---|
2990 | struct sbuf *sb; |
---|
2991 | int error, full = 0, valid_len, max_len; |
---|
2992 | |
---|
2993 | /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ |
---|
2994 | max_len = MAXPHYS - 1; |
---|
2995 | |
---|
2996 | /* Prevent hostile input from being able to crash the system */ |
---|
2997 | if (ifc->ifc_len <= 0) |
---|
2998 | return (EINVAL); |
---|
2999 | |
---|
3000 | again: |
---|
3001 | if (ifc->ifc_len <= max_len) { |
---|
3002 | max_len = ifc->ifc_len; |
---|
3003 | full = 1; |
---|
3004 | } |
---|
3005 | sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); |
---|
3006 | max_len = 0; |
---|
3007 | valid_len = 0; |
---|
3008 | |
---|
3009 | IFNET_RLOCK(); |
---|
3010 | TAILQ_FOREACH(ifp, &V_ifnet, if_link) { |
---|
3011 | int addrs; |
---|
3012 | |
---|
3013 | /* |
---|
3014 | * Zero the ifr_name buffer to make sure we don't |
---|
3015 | * disclose the contents of the stack. |
---|
3016 | */ |
---|
3017 | memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); |
---|
3018 | |
---|
3019 | if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) |
---|
3020 | >= sizeof(ifr.ifr_name)) { |
---|
3021 | sbuf_delete(sb); |
---|
3022 | IFNET_RUNLOCK(); |
---|
3023 | return (ENAMETOOLONG); |
---|
3024 | } |
---|
3025 | |
---|
3026 | addrs = 0; |
---|
3027 | IF_ADDR_RLOCK(ifp); |
---|
3028 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { |
---|
3029 | struct sockaddr *sa = ifa->ifa_addr; |
---|
3030 | |
---|
3031 | if (prison_if(curthread->td_ucred, sa) != 0) |
---|
3032 | continue; |
---|
3033 | addrs++; |
---|
3034 | if (sa->sa_len <= sizeof(*sa)) { |
---|
3035 | ifr.ifr_addr = *sa; |
---|
3036 | sbuf_bcat(sb, &ifr, sizeof(ifr)); |
---|
3037 | max_len += sizeof(ifr); |
---|
3038 | } else { |
---|
3039 | sbuf_bcat(sb, &ifr, |
---|
3040 | offsetof(struct ifreq, ifr_addr)); |
---|
3041 | max_len += offsetof(struct ifreq, ifr_addr); |
---|
3042 | sbuf_bcat(sb, sa, sa->sa_len); |
---|
3043 | max_len += sa->sa_len; |
---|
3044 | } |
---|
3045 | |
---|
3046 | if (sbuf_error(sb) == 0) |
---|
3047 | valid_len = sbuf_len(sb); |
---|
3048 | } |
---|
3049 | IF_ADDR_RUNLOCK(ifp); |
---|
3050 | if (addrs == 0) { |
---|
3051 | bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); |
---|
3052 | sbuf_bcat(sb, &ifr, sizeof(ifr)); |
---|
3053 | max_len += sizeof(ifr); |
---|
3054 | |
---|
3055 | if (sbuf_error(sb) == 0) |
---|
3056 | valid_len = sbuf_len(sb); |
---|
3057 | } |
---|
3058 | } |
---|
3059 | IFNET_RUNLOCK(); |
---|
3060 | |
---|
3061 | /* |
---|
3062 | * If we didn't allocate enough space (uncommon), try again. If |
---|
3063 | * we have already allocated as much space as we are allowed, |
---|
3064 | * return what we've got. |
---|
3065 | */ |
---|
3066 | if (valid_len != max_len && !full) { |
---|
3067 | sbuf_delete(sb); |
---|
3068 | goto again; |
---|
3069 | } |
---|
3070 | |
---|
3071 | ifc->ifc_len = valid_len; |
---|
3072 | sbuf_finish(sb); |
---|
3073 | error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); |
---|
3074 | sbuf_delete(sb); |
---|
3075 | return (error); |
---|
3076 | } |
---|
3077 | |
---|
3078 | /* |
---|
3079 | * Just like ifpromisc(), but for all-multicast-reception mode. |
---|
3080 | */ |
---|
3081 | int |
---|
3082 | if_allmulti(struct ifnet *ifp, int onswitch) |
---|
3083 | { |
---|
3084 | |
---|
3085 | return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); |
---|
3086 | } |
---|
3087 | |
---|
3088 | struct ifmultiaddr * |
---|
3089 | if_findmulti(struct ifnet *ifp, const struct sockaddr *sa) |
---|
3090 | { |
---|
3091 | struct ifmultiaddr *ifma; |
---|
3092 | |
---|
3093 | IF_ADDR_LOCK_ASSERT(ifp); |
---|
3094 | |
---|
3095 | TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { |
---|
3096 | if (sa->sa_family == AF_LINK) { |
---|
3097 | if (sa_dl_equal(ifma->ifma_addr, sa)) |
---|
3098 | break; |
---|
3099 | } else { |
---|
3100 | if (sa_equal(ifma->ifma_addr, sa)) |
---|
3101 | break; |
---|
3102 | } |
---|
3103 | } |
---|
3104 | |
---|
3105 | return ifma; |
---|
3106 | } |
---|
3107 | |
---|
3108 | /* |
---|
3109 | * Allocate a new ifmultiaddr and initialize based on passed arguments. We |
---|
3110 | * make copies of passed sockaddrs. The ifmultiaddr will not be added to |
---|
3111 | * the ifnet multicast address list here, so the caller must do that and |
---|
3112 | * other setup work (such as notifying the device driver). The reference |
---|
3113 | * count is initialized to 1. |
---|
3114 | */ |
---|
3115 | static struct ifmultiaddr * |
---|
3116 | if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, |
---|
3117 | int mflags) |
---|
3118 | { |
---|
3119 | struct ifmultiaddr *ifma; |
---|
3120 | struct sockaddr *dupsa; |
---|
3121 | |
---|
3122 | ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | |
---|
3123 | M_ZERO); |
---|
3124 | if (ifma == NULL) |
---|
3125 | return (NULL); |
---|
3126 | |
---|
3127 | dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); |
---|
3128 | if (dupsa == NULL) { |
---|
3129 | free(ifma, M_IFMADDR); |
---|
3130 | return (NULL); |
---|
3131 | } |
---|
3132 | bcopy(sa, dupsa, sa->sa_len); |
---|
3133 | ifma->ifma_addr = dupsa; |
---|
3134 | |
---|
3135 | ifma->ifma_ifp = ifp; |
---|
3136 | ifma->ifma_refcount = 1; |
---|
3137 | ifma->ifma_protospec = NULL; |
---|
3138 | |
---|
3139 | if (llsa == NULL) { |
---|
3140 | ifma->ifma_lladdr = NULL; |
---|
3141 | return (ifma); |
---|
3142 | } |
---|
3143 | |
---|
3144 | dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); |
---|
3145 | if (dupsa == NULL) { |
---|
3146 | free(ifma->ifma_addr, M_IFMADDR); |
---|
3147 | free(ifma, M_IFMADDR); |
---|
3148 | return (NULL); |
---|
3149 | } |
---|
3150 | bcopy(llsa, dupsa, llsa->sa_len); |
---|
3151 | ifma->ifma_lladdr = dupsa; |
---|
3152 | |
---|
3153 | return (ifma); |
---|
3154 | } |
---|
3155 | |
---|
3156 | /* |
---|
3157 | * if_freemulti: free ifmultiaddr structure and possibly attached related |
---|
3158 | * addresses. The caller is responsible for implementing reference |
---|
3159 | * counting, notifying the driver, handling routing messages, and releasing |
---|
3160 | * any dependent link layer state. |
---|
3161 | */ |
---|
3162 | static void |
---|
3163 | if_freemulti(struct ifmultiaddr *ifma) |
---|
3164 | { |
---|
3165 | |
---|
3166 | KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", |
---|
3167 | ifma->ifma_refcount)); |
---|
3168 | |
---|
3169 | if (ifma->ifma_lladdr != NULL) |
---|
3170 | free(ifma->ifma_lladdr, M_IFMADDR); |
---|
3171 | free(ifma->ifma_addr, M_IFMADDR); |
---|
3172 | free(ifma, M_IFMADDR); |
---|
3173 | } |
---|
3174 | |
---|
3175 | /* |
---|
3176 | * Register an additional multicast address with a network interface. |
---|
3177 | * |
---|
3178 | * - If the address is already present, bump the reference count on the |
---|
3179 | * address and return. |
---|
3180 | * - If the address is not link-layer, look up a link layer address. |
---|
3181 | * - Allocate address structures for one or both addresses, and attach to the |
---|
3182 | * multicast address list on the interface. If automatically adding a link |
---|
3183 | * layer address, the protocol address will own a reference to the link |
---|
3184 | * layer address, to be freed when it is freed. |
---|
3185 | * - Notify the network device driver of an addition to the multicast address |
---|
3186 | * list. |
---|
3187 | * |
---|
3188 | * 'sa' points to caller-owned memory with the desired multicast address. |
---|
3189 | * |
---|
3190 | * 'retifma' will be used to return a pointer to the resulting multicast |
---|
3191 | * address reference, if desired. |
---|
3192 | */ |
---|
3193 | int |
---|
3194 | if_addmulti(struct ifnet *ifp, struct sockaddr *sa, |
---|
3195 | struct ifmultiaddr **retifma) |
---|
3196 | { |
---|
3197 | struct ifmultiaddr *ifma, *ll_ifma; |
---|
3198 | struct sockaddr *llsa; |
---|
3199 | struct sockaddr_dl sdl; |
---|
3200 | int error; |
---|
3201 | |
---|
3202 | /* |
---|
3203 | * If the address is already present, return a new reference to it; |
---|
3204 | * otherwise, allocate storage and set up a new address. |
---|
3205 | */ |
---|
3206 | IF_ADDR_WLOCK(ifp); |
---|
3207 | ifma = if_findmulti(ifp, sa); |
---|
3208 | if (ifma != NULL) { |
---|
3209 | ifma->ifma_refcount++; |
---|
3210 | if (retifma != NULL) |
---|
3211 | *retifma = ifma; |
---|
3212 | IF_ADDR_WUNLOCK(ifp); |
---|
3213 | return (0); |
---|
3214 | } |
---|
3215 | |
---|
3216 | /* |
---|
3217 | * The address isn't already present; resolve the protocol address |
---|
3218 | * into a link layer address, and then look that up, bump its |
---|
3219 | * refcount or allocate an ifma for that also. |
---|
3220 | * Most link layer resolving functions returns address data which |
---|
3221 | * fits inside default sockaddr_dl structure. However callback |
---|
3222 | * can allocate another sockaddr structure, in that case we need to |
---|
3223 | * free it later. |
---|
3224 | */ |
---|
3225 | llsa = NULL; |
---|
3226 | ll_ifma = NULL; |
---|
3227 | if (ifp->if_resolvemulti != NULL) { |
---|
3228 | /* Provide called function with buffer size information */ |
---|
3229 | sdl.sdl_len = sizeof(sdl); |
---|
3230 | llsa = (struct sockaddr *)&sdl; |
---|
3231 | error = ifp->if_resolvemulti(ifp, &llsa, sa); |
---|
3232 | if (error) |
---|
3233 | goto unlock_out; |
---|
3234 | } |
---|
3235 | |
---|
3236 | /* |
---|
3237 | * Allocate the new address. Don't hook it up yet, as we may also |
---|
3238 | * need to allocate a link layer multicast address. |
---|
3239 | */ |
---|
3240 | ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); |
---|
3241 | if (ifma == NULL) { |
---|
3242 | error = ENOMEM; |
---|
3243 | goto free_llsa_out; |
---|
3244 | } |
---|
3245 | |
---|
3246 | /* |
---|
3247 | * If a link layer address is found, we'll need to see if it's |
---|
3248 | * already present in the address list, or allocate is as well. |
---|
3249 | * When this block finishes, the link layer address will be on the |
---|
3250 | * list. |
---|
3251 | */ |
---|
3252 | if (llsa != NULL) { |
---|
3253 | ll_ifma = if_findmulti(ifp, llsa); |
---|
3254 | if (ll_ifma == NULL) { |
---|
3255 | ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); |
---|
3256 | if (ll_ifma == NULL) { |
---|
3257 | --ifma->ifma_refcount; |
---|
3258 | if_freemulti(ifma); |
---|
3259 | error = ENOMEM; |
---|
3260 | goto free_llsa_out; |
---|
3261 | } |
---|
3262 | TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, |
---|
3263 | ifma_link); |
---|
3264 | } else |
---|
3265 | ll_ifma->ifma_refcount++; |
---|
3266 | ifma->ifma_llifma = ll_ifma; |
---|
3267 | } |
---|
3268 | |
---|
3269 | /* |
---|
3270 | * We now have a new multicast address, ifma, and possibly a new or |
---|
3271 | * referenced link layer address. Add the primary address to the |
---|
3272 | * ifnet address list. |
---|
3273 | */ |
---|
3274 | TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); |
---|
3275 | |
---|
3276 | if (retifma != NULL) |
---|
3277 | *retifma = ifma; |
---|
3278 | |
---|
3279 | /* |
---|
3280 | * Must generate the message while holding the lock so that 'ifma' |
---|
3281 | * pointer is still valid. |
---|
3282 | */ |
---|
3283 | rt_newmaddrmsg(RTM_NEWMADDR, ifma); |
---|
3284 | IF_ADDR_WUNLOCK(ifp); |
---|
3285 | |
---|
3286 | /* |
---|
3287 | * We are certain we have added something, so call down to the |
---|
3288 | * interface to let them know about it. |
---|
3289 | */ |
---|
3290 | if (ifp->if_ioctl != NULL) { |
---|
3291 | (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); |
---|
3292 | } |
---|
3293 | |
---|
3294 | if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) |
---|
3295 | link_free_sdl(llsa); |
---|
3296 | |
---|
3297 | return (0); |
---|
3298 | |
---|
3299 | free_llsa_out: |
---|
3300 | if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) |
---|
3301 | link_free_sdl(llsa); |
---|
3302 | |
---|
3303 | unlock_out: |
---|
3304 | IF_ADDR_WUNLOCK(ifp); |
---|
3305 | return (error); |
---|
3306 | } |
---|
3307 | |
---|
3308 | /* |
---|
3309 | * Delete a multicast group membership by network-layer group address. |
---|
3310 | * |
---|
3311 | * Returns ENOENT if the entry could not be found. If ifp no longer |
---|
3312 | * exists, results are undefined. This entry point should only be used |
---|
3313 | * from subsystems which do appropriate locking to hold ifp for the |
---|
3314 | * duration of the call. |
---|
3315 | * Network-layer protocol domains must use if_delmulti_ifma(). |
---|
3316 | */ |
---|
3317 | int |
---|
3318 | if_delmulti(struct ifnet *ifp, struct sockaddr *sa) |
---|
3319 | { |
---|
3320 | struct ifmultiaddr *ifma; |
---|
3321 | int lastref; |
---|
3322 | #ifdef INVARIANTS |
---|
3323 | struct ifnet *oifp; |
---|
3324 | |
---|
3325 | IFNET_RLOCK_NOSLEEP(); |
---|
3326 | TAILQ_FOREACH(oifp, &V_ifnet, if_link) |
---|
3327 | if (ifp == oifp) |
---|
3328 | break; |
---|
3329 | if (ifp != oifp) |
---|
3330 | ifp = NULL; |
---|
3331 | IFNET_RUNLOCK_NOSLEEP(); |
---|
3332 | |
---|
3333 | KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); |
---|
3334 | #endif |
---|
3335 | if (ifp == NULL) |
---|
3336 | return (ENOENT); |
---|
3337 | |
---|
3338 | IF_ADDR_WLOCK(ifp); |
---|
3339 | lastref = 0; |
---|
3340 | ifma = if_findmulti(ifp, sa); |
---|
3341 | if (ifma != NULL) |
---|
3342 | lastref = if_delmulti_locked(ifp, ifma, 0); |
---|
3343 | IF_ADDR_WUNLOCK(ifp); |
---|
3344 | |
---|
3345 | if (ifma == NULL) |
---|
3346 | return (ENOENT); |
---|
3347 | |
---|
3348 | if (lastref && ifp->if_ioctl != NULL) { |
---|
3349 | (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); |
---|
3350 | } |
---|
3351 | |
---|
3352 | return (0); |
---|
3353 | } |
---|
3354 | |
---|
3355 | /* |
---|
3356 | * Delete all multicast group membership for an interface. |
---|
3357 | * Should be used to quickly flush all multicast filters. |
---|
3358 | */ |
---|
3359 | void |
---|
3360 | if_delallmulti(struct ifnet *ifp) |
---|
3361 | { |
---|
3362 | struct ifmultiaddr *ifma; |
---|
3363 | struct ifmultiaddr *next; |
---|
3364 | |
---|
3365 | IF_ADDR_WLOCK(ifp); |
---|
3366 | TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) |
---|
3367 | if_delmulti_locked(ifp, ifma, 0); |
---|
3368 | IF_ADDR_WUNLOCK(ifp); |
---|
3369 | } |
---|
3370 | |
---|
3371 | /* |
---|
3372 | * Delete a multicast group membership by group membership pointer. |
---|
3373 | * Network-layer protocol domains must use this routine. |
---|
3374 | * |
---|
3375 | * It is safe to call this routine if the ifp disappeared. |
---|
3376 | */ |
---|
3377 | void |
---|
3378 | if_delmulti_ifma(struct ifmultiaddr *ifma) |
---|
3379 | { |
---|
3380 | struct ifnet *ifp; |
---|
3381 | int lastref; |
---|
3382 | |
---|
3383 | ifp = ifma->ifma_ifp; |
---|
3384 | #ifdef DIAGNOSTIC |
---|
3385 | if (ifp == NULL) { |
---|
3386 | printf("%s: ifma_ifp seems to be detached\n", __func__); |
---|
3387 | } else { |
---|
3388 | struct ifnet *oifp; |
---|
3389 | |
---|
3390 | IFNET_RLOCK_NOSLEEP(); |
---|
3391 | TAILQ_FOREACH(oifp, &V_ifnet, if_link) |
---|
3392 | if (ifp == oifp) |
---|
3393 | break; |
---|
3394 | if (ifp != oifp) { |
---|
3395 | printf("%s: ifnet %p disappeared\n", __func__, ifp); |
---|
3396 | ifp = NULL; |
---|
3397 | } |
---|
3398 | IFNET_RUNLOCK_NOSLEEP(); |
---|
3399 | } |
---|
3400 | #endif |
---|
3401 | /* |
---|
3402 | * If and only if the ifnet instance exists: Acquire the address lock. |
---|
3403 | */ |
---|
3404 | if (ifp != NULL) |
---|
3405 | IF_ADDR_WLOCK(ifp); |
---|
3406 | |
---|
3407 | lastref = if_delmulti_locked(ifp, ifma, 0); |
---|
3408 | |
---|
3409 | if (ifp != NULL) { |
---|
3410 | /* |
---|
3411 | * If and only if the ifnet instance exists: |
---|
3412 | * Release the address lock. |
---|
3413 | * If the group was left: update the hardware hash filter. |
---|
3414 | */ |
---|
3415 | IF_ADDR_WUNLOCK(ifp); |
---|
3416 | if (lastref && ifp->if_ioctl != NULL) { |
---|
3417 | (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); |
---|
3418 | } |
---|
3419 | } |
---|
3420 | } |
---|
3421 | |
---|
3422 | /* |
---|
3423 | * Perform deletion of network-layer and/or link-layer multicast address. |
---|
3424 | * |
---|
3425 | * Return 0 if the reference count was decremented. |
---|
3426 | * Return 1 if the final reference was released, indicating that the |
---|
3427 | * hardware hash filter should be reprogrammed. |
---|
3428 | */ |
---|
3429 | static int |
---|
3430 | if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) |
---|
3431 | { |
---|
3432 | struct ifmultiaddr *ll_ifma; |
---|
3433 | |
---|
3434 | if (ifp != NULL && ifma->ifma_ifp != NULL) { |
---|
3435 | KASSERT(ifma->ifma_ifp == ifp, |
---|
3436 | ("%s: inconsistent ifp %p", __func__, ifp)); |
---|
3437 | IF_ADDR_WLOCK_ASSERT(ifp); |
---|
3438 | } |
---|
3439 | |
---|
3440 | ifp = ifma->ifma_ifp; |
---|
3441 | |
---|
3442 | /* |
---|
3443 | * If the ifnet is detaching, null out references to ifnet, |
---|
3444 | * so that upper protocol layers will notice, and not attempt |
---|
3445 | * to obtain locks for an ifnet which no longer exists. The |
---|
3446 | * routing socket announcement must happen before the ifnet |
---|
3447 | * instance is detached from the system. |
---|
3448 | */ |
---|
3449 | if (detaching) { |
---|
3450 | #ifdef DIAGNOSTIC |
---|
3451 | printf("%s: detaching ifnet instance %p\n", __func__, ifp); |
---|
3452 | #endif |
---|
3453 | /* |
---|
3454 | * ifp may already be nulled out if we are being reentered |
---|
3455 | * to delete the ll_ifma. |
---|
3456 | */ |
---|
3457 | if (ifp != NULL) { |
---|
3458 | rt_newmaddrmsg(RTM_DELMADDR, ifma); |
---|
3459 | ifma->ifma_ifp = NULL; |
---|
3460 | } |
---|
3461 | } |
---|
3462 | |
---|
3463 | if (--ifma->ifma_refcount > 0) |
---|
3464 | return 0; |
---|
3465 | |
---|
3466 | /* |
---|
3467 | * If this ifma is a network-layer ifma, a link-layer ifma may |
---|
3468 | * have been associated with it. Release it first if so. |
---|
3469 | */ |
---|
3470 | ll_ifma = ifma->ifma_llifma; |
---|
3471 | if (ll_ifma != NULL) { |
---|
3472 | KASSERT(ifma->ifma_lladdr != NULL, |
---|
3473 | ("%s: llifma w/o lladdr", __func__)); |
---|
3474 | if (detaching) |
---|
3475 | ll_ifma->ifma_ifp = NULL; /* XXX */ |
---|
3476 | if (--ll_ifma->ifma_refcount == 0) { |
---|
3477 | if (ifp != NULL) { |
---|
3478 | TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, |
---|
3479 | ifma_link); |
---|
3480 | } |
---|
3481 | if_freemulti(ll_ifma); |
---|
3482 | } |
---|
3483 | } |
---|
3484 | |
---|
3485 | if (ifp != NULL) |
---|
3486 | TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); |
---|
3487 | |
---|
3488 | if_freemulti(ifma); |
---|
3489 | |
---|
3490 | /* |
---|
3491 | * The last reference to this instance of struct ifmultiaddr |
---|
3492 | * was released; the hardware should be notified of this change. |
---|
3493 | */ |
---|
3494 | return 1; |
---|
3495 | } |
---|
3496 | |
---|
3497 | /* |
---|
3498 | * Set the link layer address on an interface. |
---|
3499 | * |
---|
3500 | * At this time we only support certain types of interfaces, |
---|
3501 | * and we don't allow the length of the address to change. |
---|
3502 | * |
---|
3503 | * Set noinline to be dtrace-friendly |
---|
3504 | */ |
---|
3505 | __noinline int |
---|
3506 | if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) |
---|
3507 | { |
---|
3508 | struct sockaddr_dl *sdl; |
---|
3509 | struct ifaddr *ifa; |
---|
3510 | struct ifreq ifr; |
---|
3511 | |
---|
3512 | IF_ADDR_RLOCK(ifp); |
---|
3513 | ifa = ifp->if_addr; |
---|
3514 | if (ifa == NULL) { |
---|
3515 | IF_ADDR_RUNLOCK(ifp); |
---|
3516 | return (EINVAL); |
---|
3517 | } |
---|
3518 | ifa_ref(ifa); |
---|
3519 | IF_ADDR_RUNLOCK(ifp); |
---|
3520 | sdl = (struct sockaddr_dl *)ifa->ifa_addr; |
---|
3521 | if (sdl == NULL) { |
---|
3522 | ifa_free(ifa); |
---|
3523 | return (EINVAL); |
---|
3524 | } |
---|
3525 | if (len != sdl->sdl_alen) { /* don't allow length to change */ |
---|
3526 | ifa_free(ifa); |
---|
3527 | return (EINVAL); |
---|
3528 | } |
---|
3529 | switch (ifp->if_type) { |
---|
3530 | case IFT_ETHER: |
---|
3531 | case IFT_FDDI: |
---|
3532 | case IFT_XETHER: |
---|
3533 | case IFT_ISO88025: |
---|
3534 | case IFT_L2VLAN: |
---|
3535 | case IFT_BRIDGE: |
---|
3536 | case IFT_ARCNET: |
---|
3537 | case IFT_IEEE8023ADLAG: |
---|
3538 | bcopy(lladdr, LLADDR(sdl), len); |
---|
3539 | ifa_free(ifa); |
---|
3540 | break; |
---|
3541 | default: |
---|
3542 | ifa_free(ifa); |
---|
3543 | return (ENODEV); |
---|
3544 | } |
---|
3545 | |
---|
3546 | /* |
---|
3547 | * If the interface is already up, we need |
---|
3548 | * to re-init it in order to reprogram its |
---|
3549 | * address filter. |
---|
3550 | */ |
---|
3551 | if ((ifp->if_flags & IFF_UP) != 0) { |
---|
3552 | if (ifp->if_ioctl) { |
---|
3553 | ifp->if_flags &= ~IFF_UP; |
---|
3554 | ifr.ifr_flags = ifp->if_flags & 0xffff; |
---|
3555 | ifr.ifr_flagshigh = ifp->if_flags >> 16; |
---|
3556 | (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); |
---|
3557 | ifp->if_flags |= IFF_UP; |
---|
3558 | ifr.ifr_flags = ifp->if_flags & 0xffff; |
---|
3559 | ifr.ifr_flagshigh = ifp->if_flags >> 16; |
---|
3560 | (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); |
---|
3561 | } |
---|
3562 | } |
---|
3563 | EVENTHANDLER_INVOKE(iflladdr_event, ifp); |
---|
3564 | return (0); |
---|
3565 | } |
---|
3566 | |
---|
3567 | /* |
---|
3568 | * Compat function for handling basic encapsulation requests. |
---|
3569 | * Not converted stacks (FDDI, IB, ..) supports traditional |
---|
3570 | * output model: ARP (and other similar L2 protocols) are handled |
---|
3571 | * inside output routine, arpresolve/nd6_resolve() returns MAC |
---|
3572 | * address instead of full prepend. |
---|
3573 | * |
---|
3574 | * This function creates calculated header==MAC for IPv4/IPv6 and |
---|
3575 | * returns EAFNOSUPPORT (which is then handled in ARP code) for other |
---|
3576 | * address families. |
---|
3577 | */ |
---|
3578 | static int |
---|
3579 | if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req) |
---|
3580 | { |
---|
3581 | |
---|
3582 | if (req->rtype != IFENCAP_LL) |
---|
3583 | return (EOPNOTSUPP); |
---|
3584 | |
---|
3585 | if (req->bufsize < req->lladdr_len) |
---|
3586 | return (ENOMEM); |
---|
3587 | |
---|
3588 | switch (req->family) { |
---|
3589 | case AF_INET: |
---|
3590 | case AF_INET6: |
---|
3591 | break; |
---|
3592 | default: |
---|
3593 | return (EAFNOSUPPORT); |
---|
3594 | } |
---|
3595 | |
---|
3596 | /* Copy lladdr to storage as is */ |
---|
3597 | memmove(req->buf, req->lladdr, req->lladdr_len); |
---|
3598 | req->bufsize = req->lladdr_len; |
---|
3599 | req->lladdr_off = 0; |
---|
3600 | |
---|
3601 | return (0); |
---|
3602 | } |
---|
3603 | |
---|
3604 | /* |
---|
3605 | * The name argument must be a pointer to storage which will last as |
---|
3606 | * long as the interface does. For physical devices, the result of |
---|
3607 | * device_get_name(dev) is a good choice and for pseudo-devices a |
---|
3608 | * static string works well. |
---|
3609 | */ |
---|
3610 | void |
---|
3611 | if_initname(struct ifnet *ifp, const char *name, int unit) |
---|
3612 | { |
---|
3613 | ifp->if_dname = name; |
---|
3614 | ifp->if_dunit = unit; |
---|
3615 | if (unit != IF_DUNIT_NONE) |
---|
3616 | snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); |
---|
3617 | else |
---|
3618 | strlcpy(ifp->if_xname, name, IFNAMSIZ); |
---|
3619 | } |
---|
3620 | |
---|
3621 | int |
---|
3622 | if_printf(struct ifnet *ifp, const char * fmt, ...) |
---|
3623 | { |
---|
3624 | va_list ap; |
---|
3625 | int retval; |
---|
3626 | |
---|
3627 | retval = printf("%s: ", ifp->if_xname); |
---|
3628 | va_start(ap, fmt); |
---|
3629 | retval += vprintf(fmt, ap); |
---|
3630 | va_end(ap); |
---|
3631 | return (retval); |
---|
3632 | } |
---|
3633 | |
---|
3634 | void |
---|
3635 | if_start(struct ifnet *ifp) |
---|
3636 | { |
---|
3637 | |
---|
3638 | (*(ifp)->if_start)(ifp); |
---|
3639 | } |
---|
3640 | |
---|
3641 | /* |
---|
3642 | * Backwards compatibility interface for drivers |
---|
3643 | * that have not implemented it |
---|
3644 | */ |
---|
3645 | static int |
---|
3646 | if_transmit(struct ifnet *ifp, struct mbuf *m) |
---|
3647 | { |
---|
3648 | int error; |
---|
3649 | |
---|
3650 | IFQ_HANDOFF(ifp, m, error); |
---|
3651 | return (error); |
---|
3652 | } |
---|
3653 | |
---|
3654 | static void |
---|
3655 | if_input_default(struct ifnet *ifp __unused, struct mbuf *m) |
---|
3656 | { |
---|
3657 | |
---|
3658 | m_freem(m); |
---|
3659 | } |
---|
3660 | |
---|
3661 | int |
---|
3662 | if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) |
---|
3663 | { |
---|
3664 | int active = 0; |
---|
3665 | |
---|
3666 | IF_LOCK(ifq); |
---|
3667 | if (_IF_QFULL(ifq)) { |
---|
3668 | IF_UNLOCK(ifq); |
---|
3669 | if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); |
---|
3670 | m_freem(m); |
---|
3671 | return (0); |
---|
3672 | } |
---|
3673 | if (ifp != NULL) { |
---|
3674 | if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust); |
---|
3675 | if (m->m_flags & (M_BCAST|M_MCAST)) |
---|
3676 | if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); |
---|
3677 | active = ifp->if_drv_flags & IFF_DRV_OACTIVE; |
---|
3678 | } |
---|
3679 | _IF_ENQUEUE(ifq, m); |
---|
3680 | IF_UNLOCK(ifq); |
---|
3681 | if (ifp != NULL && !active) |
---|
3682 | (*(ifp)->if_start)(ifp); |
---|
3683 | return (1); |
---|
3684 | } |
---|
3685 | |
---|
3686 | void |
---|
3687 | if_register_com_alloc(u_char type, |
---|
3688 | if_com_alloc_t *a, if_com_free_t *f) |
---|
3689 | { |
---|
3690 | |
---|
3691 | KASSERT(if_com_alloc[type] == NULL, |
---|
3692 | ("if_register_com_alloc: %d already registered", type)); |
---|
3693 | KASSERT(if_com_free[type] == NULL, |
---|
3694 | ("if_register_com_alloc: %d free already registered", type)); |
---|
3695 | |
---|
3696 | if_com_alloc[type] = a; |
---|
3697 | if_com_free[type] = f; |
---|
3698 | } |
---|
3699 | |
---|
3700 | void |
---|
3701 | if_deregister_com_alloc(u_char type) |
---|
3702 | { |
---|
3703 | |
---|
3704 | KASSERT(if_com_alloc[type] != NULL, |
---|
3705 | ("if_deregister_com_alloc: %d not registered", type)); |
---|
3706 | KASSERT(if_com_free[type] != NULL, |
---|
3707 | ("if_deregister_com_alloc: %d free not registered", type)); |
---|
3708 | if_com_alloc[type] = NULL; |
---|
3709 | if_com_free[type] = NULL; |
---|
3710 | } |
---|
3711 | |
---|
3712 | /* API for driver access to network stack owned ifnet.*/ |
---|
3713 | uint64_t |
---|
3714 | if_setbaudrate(struct ifnet *ifp, uint64_t baudrate) |
---|
3715 | { |
---|
3716 | uint64_t oldbrate; |
---|
3717 | |
---|
3718 | oldbrate = ifp->if_baudrate; |
---|
3719 | ifp->if_baudrate = baudrate; |
---|
3720 | return (oldbrate); |
---|
3721 | } |
---|
3722 | |
---|
3723 | uint64_t |
---|
3724 | if_getbaudrate(if_t ifp) |
---|
3725 | { |
---|
3726 | |
---|
3727 | return (((struct ifnet *)ifp)->if_baudrate); |
---|
3728 | } |
---|
3729 | |
---|
3730 | int |
---|
3731 | if_setcapabilities(if_t ifp, int capabilities) |
---|
3732 | { |
---|
3733 | ((struct ifnet *)ifp)->if_capabilities = capabilities; |
---|
3734 | return (0); |
---|
3735 | } |
---|
3736 | |
---|
3737 | int |
---|
3738 | if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit) |
---|
3739 | { |
---|
3740 | ((struct ifnet *)ifp)->if_capabilities |= setbit; |
---|
3741 | ((struct ifnet *)ifp)->if_capabilities &= ~clearbit; |
---|
3742 | |
---|
3743 | return (0); |
---|
3744 | } |
---|
3745 | |
---|
3746 | int |
---|
3747 | if_getcapabilities(if_t ifp) |
---|
3748 | { |
---|
3749 | return ((struct ifnet *)ifp)->if_capabilities; |
---|
3750 | } |
---|
3751 | |
---|
3752 | int |
---|
3753 | if_setcapenable(if_t ifp, int capabilities) |
---|
3754 | { |
---|
3755 | ((struct ifnet *)ifp)->if_capenable = capabilities; |
---|
3756 | return (0); |
---|
3757 | } |
---|
3758 | |
---|
3759 | int |
---|
3760 | if_setcapenablebit(if_t ifp, int setcap, int clearcap) |
---|
3761 | { |
---|
3762 | if(setcap) |
---|
3763 | ((struct ifnet *)ifp)->if_capenable |= setcap; |
---|
3764 | if(clearcap) |
---|
3765 | ((struct ifnet *)ifp)->if_capenable &= ~clearcap; |
---|
3766 | |
---|
3767 | return (0); |
---|
3768 | } |
---|
3769 | |
---|
3770 | const char * |
---|
3771 | if_getdname(if_t ifp) |
---|
3772 | { |
---|
3773 | return ((struct ifnet *)ifp)->if_dname; |
---|
3774 | } |
---|
3775 | |
---|
3776 | int |
---|
3777 | if_togglecapenable(if_t ifp, int togglecap) |
---|
3778 | { |
---|
3779 | ((struct ifnet *)ifp)->if_capenable ^= togglecap; |
---|
3780 | return (0); |
---|
3781 | } |
---|
3782 | |
---|
3783 | int |
---|
3784 | if_getcapenable(if_t ifp) |
---|
3785 | { |
---|
3786 | return ((struct ifnet *)ifp)->if_capenable; |
---|
3787 | } |
---|
3788 | |
---|
3789 | /* |
---|
3790 | * This is largely undesirable because it ties ifnet to a device, but does |
---|
3791 | * provide flexiblity for an embedded product vendor. Should be used with |
---|
3792 | * the understanding that it violates the interface boundaries, and should be |
---|
3793 | * a last resort only. |
---|
3794 | */ |
---|
3795 | int |
---|
3796 | if_setdev(if_t ifp, void *dev) |
---|
3797 | { |
---|
3798 | return (0); |
---|
3799 | } |
---|
3800 | |
---|
3801 | int |
---|
3802 | if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags) |
---|
3803 | { |
---|
3804 | ((struct ifnet *)ifp)->if_drv_flags |= set_flags; |
---|
3805 | ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags; |
---|
3806 | |
---|
3807 | return (0); |
---|
3808 | } |
---|
3809 | |
---|
3810 | int |
---|
3811 | if_getdrvflags(if_t ifp) |
---|
3812 | { |
---|
3813 | return ((struct ifnet *)ifp)->if_drv_flags; |
---|
3814 | } |
---|
3815 | |
---|
3816 | int |
---|
3817 | if_setdrvflags(if_t ifp, int flags) |
---|
3818 | { |
---|
3819 | ((struct ifnet *)ifp)->if_drv_flags = flags; |
---|
3820 | return (0); |
---|
3821 | } |
---|
3822 | |
---|
3823 | |
---|
3824 | int |
---|
3825 | if_setflags(if_t ifp, int flags) |
---|
3826 | { |
---|
3827 | ((struct ifnet *)ifp)->if_flags = flags; |
---|
3828 | return (0); |
---|
3829 | } |
---|
3830 | |
---|
3831 | int |
---|
3832 | if_setflagbits(if_t ifp, int set, int clear) |
---|
3833 | { |
---|
3834 | ((struct ifnet *)ifp)->if_flags |= set; |
---|
3835 | ((struct ifnet *)ifp)->if_flags &= ~clear; |
---|
3836 | |
---|
3837 | return (0); |
---|
3838 | } |
---|
3839 | |
---|
3840 | int |
---|
3841 | if_getflags(if_t ifp) |
---|
3842 | { |
---|
3843 | return ((struct ifnet *)ifp)->if_flags; |
---|
3844 | } |
---|
3845 | |
---|
3846 | int |
---|
3847 | if_clearhwassist(if_t ifp) |
---|
3848 | { |
---|
3849 | ((struct ifnet *)ifp)->if_hwassist = 0; |
---|
3850 | return (0); |
---|
3851 | } |
---|
3852 | |
---|
3853 | int |
---|
3854 | if_sethwassistbits(if_t ifp, int toset, int toclear) |
---|
3855 | { |
---|
3856 | ((struct ifnet *)ifp)->if_hwassist |= toset; |
---|
3857 | ((struct ifnet *)ifp)->if_hwassist &= ~toclear; |
---|
3858 | |
---|
3859 | return (0); |
---|
3860 | } |
---|
3861 | |
---|
3862 | int |
---|
3863 | if_sethwassist(if_t ifp, int hwassist_bit) |
---|
3864 | { |
---|
3865 | ((struct ifnet *)ifp)->if_hwassist = hwassist_bit; |
---|
3866 | return (0); |
---|
3867 | } |
---|
3868 | |
---|
3869 | int |
---|
3870 | if_gethwassist(if_t ifp) |
---|
3871 | { |
---|
3872 | return ((struct ifnet *)ifp)->if_hwassist; |
---|
3873 | } |
---|
3874 | |
---|
3875 | int |
---|
3876 | if_setmtu(if_t ifp, int mtu) |
---|
3877 | { |
---|
3878 | ((struct ifnet *)ifp)->if_mtu = mtu; |
---|
3879 | return (0); |
---|
3880 | } |
---|
3881 | |
---|
3882 | int |
---|
3883 | if_getmtu(if_t ifp) |
---|
3884 | { |
---|
3885 | return ((struct ifnet *)ifp)->if_mtu; |
---|
3886 | } |
---|
3887 | |
---|
3888 | int |
---|
3889 | if_getmtu_family(if_t ifp, int family) |
---|
3890 | { |
---|
3891 | struct domain *dp; |
---|
3892 | |
---|
3893 | for (dp = domains; dp; dp = dp->dom_next) { |
---|
3894 | if (dp->dom_family == family && dp->dom_ifmtu != NULL) |
---|
3895 | return (dp->dom_ifmtu((struct ifnet *)ifp)); |
---|
3896 | } |
---|
3897 | |
---|
3898 | return (((struct ifnet *)ifp)->if_mtu); |
---|
3899 | } |
---|
3900 | |
---|
3901 | int |
---|
3902 | if_setsoftc(if_t ifp, void *softc) |
---|
3903 | { |
---|
3904 | ((struct ifnet *)ifp)->if_softc = softc; |
---|
3905 | return (0); |
---|
3906 | } |
---|
3907 | |
---|
3908 | void * |
---|
3909 | if_getsoftc(if_t ifp) |
---|
3910 | { |
---|
3911 | return ((struct ifnet *)ifp)->if_softc; |
---|
3912 | } |
---|
3913 | |
---|
3914 | void |
---|
3915 | if_setrcvif(struct mbuf *m, if_t ifp) |
---|
3916 | { |
---|
3917 | m->m_pkthdr.rcvif = (struct ifnet *)ifp; |
---|
3918 | } |
---|
3919 | |
---|
3920 | void |
---|
3921 | if_setvtag(struct mbuf *m, uint16_t tag) |
---|
3922 | { |
---|
3923 | m->m_pkthdr.ether_vtag = tag; |
---|
3924 | } |
---|
3925 | |
---|
3926 | uint16_t |
---|
3927 | if_getvtag(struct mbuf *m) |
---|
3928 | { |
---|
3929 | |
---|
3930 | return (m->m_pkthdr.ether_vtag); |
---|
3931 | } |
---|
3932 | |
---|
3933 | int |
---|
3934 | if_sendq_empty(if_t ifp) |
---|
3935 | { |
---|
3936 | return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd); |
---|
3937 | } |
---|
3938 | |
---|
3939 | struct ifaddr * |
---|
3940 | if_getifaddr(if_t ifp) |
---|
3941 | { |
---|
3942 | return ((struct ifnet *)ifp)->if_addr; |
---|
3943 | } |
---|
3944 | |
---|
3945 | int |
---|
3946 | if_getamcount(if_t ifp) |
---|
3947 | { |
---|
3948 | return ((struct ifnet *)ifp)->if_amcount; |
---|
3949 | } |
---|
3950 | |
---|
3951 | |
---|
3952 | int |
---|
3953 | if_setsendqready(if_t ifp) |
---|
3954 | { |
---|
3955 | IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd); |
---|
3956 | return (0); |
---|
3957 | } |
---|
3958 | |
---|
3959 | int |
---|
3960 | if_setsendqlen(if_t ifp, int tx_desc_count) |
---|
3961 | { |
---|
3962 | IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count); |
---|
3963 | ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count; |
---|
3964 | |
---|
3965 | return (0); |
---|
3966 | } |
---|
3967 | |
---|
3968 | int |
---|
3969 | if_vlantrunkinuse(if_t ifp) |
---|
3970 | { |
---|
3971 | return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0; |
---|
3972 | } |
---|
3973 | |
---|
3974 | int |
---|
3975 | if_input(if_t ifp, struct mbuf* sendmp) |
---|
3976 | { |
---|
3977 | (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp); |
---|
3978 | return (0); |
---|
3979 | |
---|
3980 | } |
---|
3981 | |
---|
3982 | /* XXX */ |
---|
3983 | #ifndef ETH_ADDR_LEN |
---|
3984 | #define ETH_ADDR_LEN 6 |
---|
3985 | #endif |
---|
3986 | |
---|
3987 | int |
---|
3988 | if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max) |
---|
3989 | { |
---|
3990 | struct ifmultiaddr *ifma; |
---|
3991 | uint8_t *lmta = (uint8_t *)mta; |
---|
3992 | int mcnt = 0; |
---|
3993 | |
---|
3994 | TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) { |
---|
3995 | if (ifma->ifma_addr->sa_family != AF_LINK) |
---|
3996 | continue; |
---|
3997 | |
---|
3998 | if (mcnt == max) |
---|
3999 | break; |
---|
4000 | |
---|
4001 | bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), |
---|
4002 | &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN); |
---|
4003 | mcnt++; |
---|
4004 | } |
---|
4005 | *cnt = mcnt; |
---|
4006 | |
---|
4007 | return (0); |
---|
4008 | } |
---|
4009 | |
---|
4010 | int |
---|
4011 | if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max) |
---|
4012 | { |
---|
4013 | int error; |
---|
4014 | |
---|
4015 | if_maddr_rlock(ifp); |
---|
4016 | error = if_setupmultiaddr(ifp, mta, cnt, max); |
---|
4017 | if_maddr_runlock(ifp); |
---|
4018 | return (error); |
---|
4019 | } |
---|
4020 | |
---|
4021 | int |
---|
4022 | if_multiaddr_count(if_t ifp, int max) |
---|
4023 | { |
---|
4024 | struct ifmultiaddr *ifma; |
---|
4025 | int count; |
---|
4026 | |
---|
4027 | count = 0; |
---|
4028 | if_maddr_rlock(ifp); |
---|
4029 | TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) { |
---|
4030 | if (ifma->ifma_addr->sa_family != AF_LINK) |
---|
4031 | continue; |
---|
4032 | count++; |
---|
4033 | if (count == max) |
---|
4034 | break; |
---|
4035 | } |
---|
4036 | if_maddr_runlock(ifp); |
---|
4037 | return (count); |
---|
4038 | } |
---|
4039 | |
---|
4040 | int |
---|
4041 | if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg) |
---|
4042 | { |
---|
4043 | struct ifmultiaddr *ifma; |
---|
4044 | int cnt = 0; |
---|
4045 | |
---|
4046 | if_maddr_rlock(ifp); |
---|
4047 | TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) |
---|
4048 | cnt += filter(arg, ifma, cnt); |
---|
4049 | if_maddr_runlock(ifp); |
---|
4050 | return (cnt); |
---|
4051 | } |
---|
4052 | |
---|
4053 | struct mbuf * |
---|
4054 | if_dequeue(if_t ifp) |
---|
4055 | { |
---|
4056 | struct mbuf *m; |
---|
4057 | IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m); |
---|
4058 | |
---|
4059 | return (m); |
---|
4060 | } |
---|
4061 | |
---|
4062 | int |
---|
4063 | if_sendq_prepend(if_t ifp, struct mbuf *m) |
---|
4064 | { |
---|
4065 | IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m); |
---|
4066 | return (0); |
---|
4067 | } |
---|
4068 | |
---|
4069 | int |
---|
4070 | if_setifheaderlen(if_t ifp, int len) |
---|
4071 | { |
---|
4072 | ((struct ifnet *)ifp)->if_hdrlen = len; |
---|
4073 | return (0); |
---|
4074 | } |
---|
4075 | |
---|
4076 | caddr_t |
---|
4077 | if_getlladdr(if_t ifp) |
---|
4078 | { |
---|
4079 | return (IF_LLADDR((struct ifnet *)ifp)); |
---|
4080 | } |
---|
4081 | |
---|
4082 | void * |
---|
4083 | if_gethandle(u_char type) |
---|
4084 | { |
---|
4085 | return (if_alloc(type)); |
---|
4086 | } |
---|
4087 | |
---|
4088 | void |
---|
4089 | if_bpfmtap(if_t ifh, struct mbuf *m) |
---|
4090 | { |
---|
4091 | struct ifnet *ifp = (struct ifnet *)ifh; |
---|
4092 | |
---|
4093 | BPF_MTAP(ifp, m); |
---|
4094 | } |
---|
4095 | |
---|
4096 | void |
---|
4097 | if_etherbpfmtap(if_t ifh, struct mbuf *m) |
---|
4098 | { |
---|
4099 | struct ifnet *ifp = (struct ifnet *)ifh; |
---|
4100 | |
---|
4101 | ETHER_BPF_MTAP(ifp, m); |
---|
4102 | } |
---|
4103 | |
---|
4104 | void |
---|
4105 | if_vlancap(if_t ifh) |
---|
4106 | { |
---|
4107 | struct ifnet *ifp = (struct ifnet *)ifh; |
---|
4108 | VLAN_CAPABILITIES(ifp); |
---|
4109 | } |
---|
4110 | |
---|
4111 | int |
---|
4112 | if_sethwtsomax(if_t ifp, u_int if_hw_tsomax) |
---|
4113 | { |
---|
4114 | |
---|
4115 | ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax; |
---|
4116 | return (0); |
---|
4117 | } |
---|
4118 | |
---|
4119 | int |
---|
4120 | if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount) |
---|
4121 | { |
---|
4122 | |
---|
4123 | ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount; |
---|
4124 | return (0); |
---|
4125 | } |
---|
4126 | |
---|
4127 | int |
---|
4128 | if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize) |
---|
4129 | { |
---|
4130 | |
---|
4131 | ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize; |
---|
4132 | return (0); |
---|
4133 | } |
---|
4134 | |
---|
4135 | u_int |
---|
4136 | if_gethwtsomax(if_t ifp) |
---|
4137 | { |
---|
4138 | |
---|
4139 | return (((struct ifnet *)ifp)->if_hw_tsomax); |
---|
4140 | } |
---|
4141 | |
---|
4142 | u_int |
---|
4143 | if_gethwtsomaxsegcount(if_t ifp) |
---|
4144 | { |
---|
4145 | |
---|
4146 | return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount); |
---|
4147 | } |
---|
4148 | |
---|
4149 | u_int |
---|
4150 | if_gethwtsomaxsegsize(if_t ifp) |
---|
4151 | { |
---|
4152 | |
---|
4153 | return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize); |
---|
4154 | } |
---|
4155 | |
---|
4156 | void |
---|
4157 | if_setinitfn(if_t ifp, void (*init_fn)(void *)) |
---|
4158 | { |
---|
4159 | ((struct ifnet *)ifp)->if_init = init_fn; |
---|
4160 | } |
---|
4161 | |
---|
4162 | void |
---|
4163 | if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t)) |
---|
4164 | { |
---|
4165 | ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn; |
---|
4166 | } |
---|
4167 | |
---|
4168 | void |
---|
4169 | if_setstartfn(if_t ifp, void (*start_fn)(if_t)) |
---|
4170 | { |
---|
4171 | ((struct ifnet *)ifp)->if_start = (void *)start_fn; |
---|
4172 | } |
---|
4173 | |
---|
4174 | void |
---|
4175 | if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn) |
---|
4176 | { |
---|
4177 | ((struct ifnet *)ifp)->if_transmit = start_fn; |
---|
4178 | } |
---|
4179 | |
---|
4180 | void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn) |
---|
4181 | { |
---|
4182 | ((struct ifnet *)ifp)->if_qflush = flush_fn; |
---|
4183 | |
---|
4184 | } |
---|
4185 | |
---|
4186 | void |
---|
4187 | if_setgetcounterfn(if_t ifp, if_get_counter_t fn) |
---|
4188 | { |
---|
4189 | |
---|
4190 | ifp->if_get_counter = fn; |
---|
4191 | } |
---|
4192 | |
---|
4193 | /* Revisit these - These are inline functions originally. */ |
---|
4194 | int |
---|
4195 | drbr_inuse_drv(if_t ifh, struct buf_ring *br) |
---|
4196 | { |
---|
4197 | return drbr_inuse(ifh, br); |
---|
4198 | } |
---|
4199 | |
---|
4200 | struct mbuf* |
---|
4201 | drbr_dequeue_drv(if_t ifh, struct buf_ring *br) |
---|
4202 | { |
---|
4203 | return drbr_dequeue(ifh, br); |
---|
4204 | } |
---|
4205 | |
---|
4206 | int |
---|
4207 | drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br) |
---|
4208 | { |
---|
4209 | return drbr_needs_enqueue(ifh, br); |
---|
4210 | } |
---|
4211 | |
---|
4212 | int |
---|
4213 | drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m) |
---|
4214 | { |
---|
4215 | return drbr_enqueue(ifh, br, m); |
---|
4216 | |
---|
4217 | } |
---|