1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
5 * Copyright (c) 1980, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include "opt_bpf.h"
34 #include "opt_inet6.h"
35 #include "opt_inet.h"
36 #include "opt_ddb.h"
37
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/conf.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/domainset.h>
44 #include <sys/sbuf.h>
45 #include <sys/bus.h>
46 #include <sys/epoch.h>
47 #include <sys/mbuf.h>
48 #include <sys/systm.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/refcount.h>
57 #include <sys/module.h>
58 #include <sys/nv.h>
59 #include <sys/rwlock.h>
60 #include <sys/sockio.h>
61 #include <sys/stdarg.h>
62 #include <sys/syslog.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/taskqueue.h>
66 #include <sys/domain.h>
67 #include <sys/jail.h>
68 #include <sys/priv.h>
69
70 #ifdef DDB
71 #include <ddb/ddb.h>
72 #endif
73
74 #include <vm/uma.h>
75
76 #include <net/bpf.h>
77 #include <net/if.h>
78 #include <net/if_arp.h>
79 #include <net/if_clone.h>
80 #include <net/if_dl.h>
81 #include <net/if_strings.h>
82 #include <net/if_types.h>
83 #include <net/if_var.h>
84 #include <net/if_media.h>
85 #include <net/if_mib.h>
86 #include <net/if_private.h>
87 #include <net/if_vlan_var.h>
88 #include <net/radix.h>
89 #include <net/route.h>
90 #include <net/route/route_ctl.h>
91 #include <net/vnet.h>
92
93 #if defined(INET) || defined(INET6)
94 #include <net/ethernet.h>
95 #include <netinet/in.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip.h>
98 #include <netinet/ip_carp.h>
99 #ifdef INET
100 #include <net/debugnet.h>
101 #include <netinet/if_ether.h>
102 #endif /* INET */
103 #ifdef INET6
104 #include <netinet6/in6_var.h>
105 #endif /* INET6 */
106 #endif /* INET || INET6 */
107
108 #include <security/mac/mac_framework.h>
109
110 /*
111 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
112 * and ifr_ifru when it is used in SIOCGIFCONF.
113 */
114 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
115 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
116
117 __read_mostly epoch_t net_epoch_preempt;
118 #ifdef COMPAT_FREEBSD32
119 #include <sys/mount.h>
120 #include <compat/freebsd32/freebsd32.h>
121
122 struct ifreq_buffer32 {
123 uint32_t length; /* (size_t) */
124 uint32_t buffer; /* (void *) */
125 };
126
127 /*
128 * Interface request structure used for socket
129 * ioctl's. All interface ioctl's must have parameter
130 * definitions which begin with ifr_name. The
131 * remainder may be interface specific.
132 */
133 struct ifreq32 {
134 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
135 union {
136 struct sockaddr ifru_addr;
137 struct sockaddr ifru_dstaddr;
138 struct sockaddr ifru_broadaddr;
139 struct ifreq_buffer32 ifru_buffer;
140 short ifru_flags[2];
141 short ifru_index;
142 int ifru_jid;
143 int ifru_metric;
144 int ifru_mtu;
145 int ifru_phys;
146 int ifru_media;
147 uint32_t ifru_data;
148 int ifru_cap[2];
149 u_int ifru_fib;
150 u_char ifru_vlan_pcp;
151 } ifr_ifru;
152 };
153 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
154 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
155 __offsetof(struct ifreq32, ifr_ifru));
156
157 struct ifconf32 {
158 int32_t ifc_len;
159 union {
160 uint32_t ifcu_buf;
161 uint32_t ifcu_req;
162 } ifc_ifcu;
163 };
164 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
165
166 struct ifdrv32 {
167 char ifd_name[IFNAMSIZ];
168 uint32_t ifd_cmd;
169 uint32_t ifd_len;
170 uint32_t ifd_data;
171 };
172 #define SIOCSDRVSPEC32 _IOC_NEWTYPE(SIOCSDRVSPEC, struct ifdrv32)
173 #define SIOCGDRVSPEC32 _IOC_NEWTYPE(SIOCGDRVSPEC, struct ifdrv32)
174
175 struct ifgroupreq32 {
176 char ifgr_name[IFNAMSIZ];
177 u_int ifgr_len;
178 union {
179 char ifgru_group[IFNAMSIZ];
180 uint32_t ifgru_groups;
181 } ifgr_ifgru;
182 };
183 #define SIOCAIFGROUP32 _IOC_NEWTYPE(SIOCAIFGROUP, struct ifgroupreq32)
184 #define SIOCGIFGROUP32 _IOC_NEWTYPE(SIOCGIFGROUP, struct ifgroupreq32)
185 #define SIOCDIFGROUP32 _IOC_NEWTYPE(SIOCDIFGROUP, struct ifgroupreq32)
186 #define SIOCGIFGMEMB32 _IOC_NEWTYPE(SIOCGIFGMEMB, struct ifgroupreq32)
187
188 struct ifmediareq32 {
189 char ifm_name[IFNAMSIZ];
190 int ifm_current;
191 int ifm_mask;
192 int ifm_status;
193 int ifm_active;
194 int ifm_count;
195 uint32_t ifm_ulist; /* (int *) */
196 };
197 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
198 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
199 #endif /* COMPAT_FREEBSD32 */
200
201 union ifreq_union {
202 struct ifreq ifr;
203 #ifdef COMPAT_FREEBSD32
204 struct ifreq32 ifr32;
205 #endif
206 };
207
208 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
209 "Link layers");
210 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
211 "Generic link-management");
212
213 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
214 &ifqmaxlen, 0, "max send queue size");
215
216 /* Log link state change events */
217 static int log_link_state_change = 1;
218
219 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
220 &log_link_state_change, 0,
221 "log interface link state change events");
222
223 /* Log promiscuous mode change events */
224 static int log_promisc_mode_change = 1;
225
226 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
227 &log_promisc_mode_change, 1,
228 "log promiscuous mode change events");
229
230 /* Interface description */
231 static unsigned int ifdescr_maxlen = 1024;
232 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
233 &ifdescr_maxlen, 0,
234 "administrative maximum length for interface description");
235
236 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
237
238 /* global sx for non-critical path ifdescr */
239 static struct sx ifdescr_sx;
240 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
241
242 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
243 /* These are external hooks for CARP. */
244 void (*carp_linkstate_p)(struct ifnet *ifp);
245 void (*carp_demote_adj_p)(int, char *);
246 int (*carp_master_p)(struct ifaddr *);
247 #if defined(INET) || defined(INET6)
248 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
249 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
250 const struct sockaddr *sa);
251 int (*carp_attach_p)(struct ifaddr *, int);
252 void (*carp_detach_p)(struct ifaddr *, bool);
253 #endif
254 #ifdef INET
255 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
256 #endif
257 #ifdef INET6
258 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
259 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
260 const struct in6_addr *taddr);
261 #endif
262
263 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
264
265 /*
266 * XXX: Style; these should be sorted alphabetically, and unprototyped
267 * static functions should be prototyped. Currently they are sorted by
268 * declaration order.
269 */
270 static int ifconf(u_long, caddr_t);
271 static void if_input_default(struct ifnet *, struct mbuf *);
272 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
273 static int if_setflag(struct ifnet *, int, int, int *, int);
274 static int if_transmit_default(struct ifnet *ifp, struct mbuf *m);
275 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
276 static void do_link_state_change(void *, int);
277 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
278 static int if_getgroupmembers(struct ifgroupreq *);
279 static void if_delgroups(struct ifnet *);
280 static void if_attach_internal(struct ifnet *, bool);
281 static void if_detach_internal(struct ifnet *, bool);
282 static void if_siocaddmulti(void *, int);
283 static void if_link_ifnet(struct ifnet *);
284 static bool if_unlink_ifnet(struct ifnet *, bool);
285 #ifdef VIMAGE
286 static void if_vmove(struct ifnet *, struct vnet *);
287 #endif
288
289 #ifdef INET6
290 /*
291 * XXX: declare here to avoid to include many inet6 related files..
292 * should be more generalized?
293 */
294 extern void nd6_setmtu(struct ifnet *);
295 #endif
296
297 /* ipsec helper hooks */
298 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
299 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
300
301 int ifqmaxlen = IFQ_MAXLEN;
302 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
303 VNET_DEFINE(struct ifgrouphead, ifg_head);
304
305 /* Table of ifnet by index. */
306 static int if_index;
307 static int if_indexlim = 8;
308 static struct ifindex_entry {
309 struct ifnet *ife_ifnet;
310 uint16_t ife_gencnt;
311 } *ifindex_table;
312
313 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
314 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
315 "Variables global to all interfaces");
316 static int
sysctl_ifcount(SYSCTL_HANDLER_ARGS)317 sysctl_ifcount(SYSCTL_HANDLER_ARGS)
318 {
319 int rv = 0;
320
321 IFNET_RLOCK();
322 for (int i = 1; i <= if_index; i++)
323 if (ifindex_table[i].ife_ifnet != NULL &&
324 ifindex_table[i].ife_ifnet->if_vnet == curvnet)
325 rv = i;
326 IFNET_RUNLOCK();
327
328 return (sysctl_handle_int(oidp, &rv, 0, req));
329 }
330 SYSCTL_PROC(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
331 CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RD, NULL, 0, sysctl_ifcount, "I",
332 "Maximum known interface index");
333
334 /*
335 * The global network interface list (V_ifnet) and related state (such as
336 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
337 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
338 */
339 struct sx ifnet_sxlock;
340 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
341
342 struct sx ifnet_detach_sxlock;
343 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
344 SX_RECURSE);
345
346 static if_com_alloc_t *if_com_alloc[256];
347 static if_com_free_t *if_com_free[256];
348
349 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
350 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
351 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
352
353 struct ifnet *
ifnet_byindex(u_int idx)354 ifnet_byindex(u_int idx)
355 {
356 struct ifnet *ifp;
357
358 NET_EPOCH_ASSERT();
359
360 if (__predict_false(idx > if_index))
361 return (NULL);
362
363 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
364
365 if (curvnet != NULL && ifp != NULL && ifp->if_vnet != curvnet)
366 ifp = NULL;
367
368 return (ifp);
369 }
370
371 struct ifnet *
ifnet_byindex_ref(u_int idx)372 ifnet_byindex_ref(u_int idx)
373 {
374 struct ifnet *ifp;
375
376 ifp = ifnet_byindex(idx);
377 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
378 return (NULL);
379 if (!if_try_ref(ifp))
380 return (NULL);
381 return (ifp);
382 }
383
384 struct ifnet *
ifnet_byindexgen(uint16_t idx,uint16_t gen)385 ifnet_byindexgen(uint16_t idx, uint16_t gen)
386 {
387 struct ifnet *ifp;
388
389 NET_EPOCH_ASSERT();
390
391 if (__predict_false(idx > if_index))
392 return (NULL);
393
394 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
395
396 if (ifindex_table[idx].ife_gencnt == gen)
397 return (ifp);
398 else
399 return (NULL);
400 }
401
402 /*
403 * Network interface utility routines.
404 *
405 * Routines with ifa_ifwith* names take sockaddr *'s as
406 * parameters.
407 */
408
409 static void
if_init_idxtable(void * arg __unused)410 if_init_idxtable(void *arg __unused)
411 {
412
413 ifindex_table = malloc(if_indexlim * sizeof(*ifindex_table),
414 M_IFNET, M_WAITOK | M_ZERO);
415 }
416 SYSINIT(if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init_idxtable, NULL);
417
418 static void
vnet_if_init(const void * unused __unused)419 vnet_if_init(const void *unused __unused)
420 {
421
422 CK_STAILQ_INIT(&V_ifnet);
423 CK_STAILQ_INIT(&V_ifg_head);
424 }
425 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
426 NULL);
427
428 static void
if_link_ifnet(struct ifnet * ifp)429 if_link_ifnet(struct ifnet *ifp)
430 {
431
432 IFNET_WLOCK();
433 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
434 #ifdef VIMAGE
435 curvnet->vnet_ifcnt++;
436 #endif
437 IFNET_WUNLOCK();
438 }
439
440 static bool
if_unlink_ifnet(struct ifnet * ifp,bool vmove)441 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
442 {
443 struct ifnet *iter;
444 int found = 0;
445
446 IFNET_WLOCK();
447 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
448 if (iter == ifp) {
449 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
450 #ifdef VIMAGE
451 curvnet->vnet_ifcnt--;
452 #endif
453 if (!vmove)
454 ifp->if_flags |= IFF_DYING;
455 found = 1;
456 break;
457 }
458 IFNET_WUNLOCK();
459
460 return (found);
461 }
462
463 #ifdef VIMAGE
464 static void
vnet_if_return(const void * unused __unused)465 vnet_if_return(const void *unused __unused)
466 {
467 struct ifnet *ifp, *nifp;
468 struct ifnet **pending;
469 int found __diagused;
470 int i;
471
472 i = 0;
473
474 /*
475 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
476 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
477 * if_detach_internal(), which waits for NET_EPOCH callbacks to
478 * complete. We can't do that from within NET_EPOCH.
479 *
480 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
481 * read/write lock. We cannot hold the lock as we call if_vmove()
482 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
483 * ctx lock.
484 */
485 IFNET_WLOCK();
486
487 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
488 M_IFNET, M_WAITOK | M_ZERO);
489
490 /* Return all inherited interfaces to their parent vnets. */
491 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
492 if (ifp->if_home_vnet != ifp->if_vnet) {
493 found = if_unlink_ifnet(ifp, true);
494 MPASS(found);
495
496 pending[i++] = ifp;
497 }
498 }
499 IFNET_WUNLOCK();
500
501 for (int j = 0; j < i; j++) {
502 sx_xlock(&ifnet_detach_sxlock);
503 if_vmove(pending[j], pending[j]->if_home_vnet);
504 sx_xunlock(&ifnet_detach_sxlock);
505 }
506
507 free(pending, M_IFNET);
508 }
509 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
510 vnet_if_return, NULL);
511 #endif
512
513 /*
514 * Allocate a struct ifnet and an index for an interface. A layer 2
515 * common structure will also be allocated if an allocation routine is
516 * registered for the passed type.
517 */
518 static struct ifnet *
if_alloc_domain(u_char type,int numa_domain)519 if_alloc_domain(u_char type, int numa_domain)
520 {
521 struct ifnet *ifp;
522 u_short idx;
523
524 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
525 if (numa_domain == IF_NODOM)
526 ifp = malloc(sizeof(struct ifnet), M_IFNET,
527 M_WAITOK | M_ZERO);
528 else
529 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
530 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
531 ifp->if_type = type;
532 ifp->if_alloctype = type;
533 ifp->if_numa_domain = numa_domain;
534 #ifdef VIMAGE
535 ifp->if_vnet = curvnet;
536 #endif
537 if (if_com_alloc[type] != NULL) {
538 ifp->if_l2com = if_com_alloc[type](type, ifp);
539 KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
540 type));
541 }
542
543 IF_ADDR_LOCK_INIT(ifp);
544 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
545 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
546 CK_STAILQ_INIT(&ifp->if_addrhead);
547 CK_STAILQ_INIT(&ifp->if_multiaddrs);
548 CK_STAILQ_INIT(&ifp->if_groups);
549 #ifdef MAC
550 mac_ifnet_init(ifp);
551 #endif
552 ifq_init(&ifp->if_snd, ifp);
553
554 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
555 for (int i = 0; i < IFCOUNTERS; i++)
556 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
557 ifp->if_get_counter = if_get_counter_default;
558 ifp->if_pcp = IFNET_PCP_NONE;
559
560 /* Allocate an ifindex array entry. */
561 IFNET_WLOCK();
562 /*
563 * Try to find an empty slot below if_index. If we fail, take the
564 * next slot.
565 */
566 for (idx = 1; idx <= if_index; idx++) {
567 if (ifindex_table[idx].ife_ifnet == NULL)
568 break;
569 }
570
571 /* Catch if_index overflow. */
572 if (idx >= if_indexlim) {
573 struct ifindex_entry *new, *old;
574 int newlim;
575
576 newlim = if_indexlim * 2;
577 new = malloc(newlim * sizeof(*new), M_IFNET, M_WAITOK | M_ZERO);
578 memcpy(new, ifindex_table, if_indexlim * sizeof(*new));
579 old = ifindex_table;
580 ck_pr_store_ptr(&ifindex_table, new);
581 if_indexlim = newlim;
582 NET_EPOCH_WAIT();
583 free(old, M_IFNET);
584 }
585 if (idx > if_index)
586 if_index = idx;
587
588 ifp->if_index = idx;
589 ifp->if_idxgen = ifindex_table[idx].ife_gencnt;
590 ck_pr_store_ptr(&ifindex_table[idx].ife_ifnet, ifp);
591 IFNET_WUNLOCK();
592
593 return (ifp);
594 }
595
596 struct ifnet *
if_alloc_dev(u_char type,device_t dev)597 if_alloc_dev(u_char type, device_t dev)
598 {
599 int numa_domain;
600
601 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
602 return (if_alloc_domain(type, IF_NODOM));
603 return (if_alloc_domain(type, numa_domain));
604 }
605
606 struct ifnet *
if_alloc(u_char type)607 if_alloc(u_char type)
608 {
609
610 return (if_alloc_domain(type, IF_NODOM));
611 }
612 /*
613 * Do the actual work of freeing a struct ifnet, and layer 2 common
614 * structure. This call is made when the network epoch guarantees
615 * us that nobody holds a pointer to the interface.
616 */
617 static void
if_free_deferred(epoch_context_t ctx)618 if_free_deferred(epoch_context_t ctx)
619 {
620 struct ifnet *ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
621
622 KASSERT((ifp->if_flags & IFF_DYING),
623 ("%s: interface not dying", __func__));
624
625 if (if_com_free[ifp->if_alloctype] != NULL)
626 if_com_free[ifp->if_alloctype](ifp->if_l2com,
627 ifp->if_alloctype);
628
629 #ifdef MAC
630 mac_ifnet_destroy(ifp);
631 #endif /* MAC */
632 IF_ADDR_LOCK_DESTROY(ifp);
633 ifq_delete(&ifp->if_snd);
634
635 for (int i = 0; i < IFCOUNTERS; i++)
636 counter_u64_free(ifp->if_counters[i]);
637
638 if_freedescr(ifp->if_description);
639 free(ifp->if_hw_addr, M_IFADDR);
640 free(ifp, M_IFNET);
641 }
642
643 /*
644 * Deregister an interface and free the associated storage.
645 */
646 void
if_free(struct ifnet * ifp)647 if_free(struct ifnet *ifp)
648 {
649
650 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
651
652 /*
653 * XXXGL: An interface index is really an alias to ifp pointer.
654 * Why would we clear the alias now, and not in the deferred
655 * context? Indeed there is nothing wrong with some network
656 * thread obtaining ifp via ifnet_byindex() inside the network
657 * epoch and then dereferencing ifp while we perform if_free(),
658 * and after if_free() finished, too.
659 *
660 * This early index freeing was important back when ifindex was
661 * virtualized and interface would outlive the vnet.
662 */
663 IFNET_WLOCK();
664 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
665 ck_pr_store_ptr(&ifindex_table[ifp->if_index].ife_ifnet, NULL);
666 ifindex_table[ifp->if_index].ife_gencnt++;
667 while (if_index > 0 && ifindex_table[if_index].ife_ifnet == NULL)
668 if_index--;
669 IFNET_WUNLOCK();
670
671 if (refcount_release(&ifp->if_refcount))
672 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
673 }
674
675 /*
676 * Interfaces to keep an ifnet type-stable despite the possibility of the
677 * driver calling if_free(). If there are additional references, we defer
678 * freeing the underlying data structure.
679 */
680 void
if_ref(struct ifnet * ifp)681 if_ref(struct ifnet *ifp)
682 {
683 u_int old __diagused;
684
685 /* We don't assert the ifnet list lock here, but arguably should. */
686 old = refcount_acquire(&ifp->if_refcount);
687 KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
688 }
689
690 bool
if_try_ref(struct ifnet * ifp)691 if_try_ref(struct ifnet *ifp)
692 {
693 NET_EPOCH_ASSERT();
694 return (refcount_acquire_if_not_zero(&ifp->if_refcount));
695 }
696
697 void
if_rele(struct ifnet * ifp)698 if_rele(struct ifnet *ifp)
699 {
700
701 if (!refcount_release(&ifp->if_refcount))
702 return;
703 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
704 }
705
706 void
ifq_init(struct ifaltq * ifq,struct ifnet * ifp)707 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
708 {
709
710 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
711
712 if (ifq->ifq_maxlen == 0)
713 ifq->ifq_maxlen = ifqmaxlen;
714
715 ifq->altq_type = 0;
716 ifq->altq_disc = NULL;
717 ifq->altq_flags &= ALTQF_CANTCHANGE;
718 ifq->altq_tbr = NULL;
719 ifq->altq_ifp = ifp;
720 }
721
722 void
ifq_delete(struct ifaltq * ifq)723 ifq_delete(struct ifaltq *ifq)
724 {
725 mtx_destroy(&ifq->ifq_mtx);
726 }
727
728 /*
729 * Perform generic interface initialization tasks and attach the interface
730 * to the list of "active" interfaces. If vmove flag is set on entry
731 * to if_attach_internal(), perform only a limited subset of initialization
732 * tasks, given that we are moving from one vnet to another an ifnet which
733 * has already been fully initialized.
734 *
735 * Note that if_detach_internal() removes group membership unconditionally
736 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
737 * Thus, when if_vmove() is applied to a cloned interface, group membership
738 * is lost while a cloned one always joins a group whose name is
739 * ifc->ifc_name. To recover this after if_detach_internal() and
740 * if_attach_internal(), the cloner should be specified to
741 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
742 * attempts to join a group whose name is ifc->ifc_name.
743 *
744 * XXX:
745 * - The decision to return void and thus require this function to
746 * succeed is questionable.
747 * - We should probably do more sanity checking. For instance we don't
748 * do anything to insure if_xname is unique or non-empty.
749 */
750 void
if_attach(struct ifnet * ifp)751 if_attach(struct ifnet *ifp)
752 {
753
754 if_attach_internal(ifp, false);
755 }
756
757 /*
758 * Compute the least common TSO limit.
759 */
760 void
if_hw_tsomax_common(if_t ifp,struct ifnet_hw_tsomax * pmax)761 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
762 {
763 /*
764 * 1) If there is no limit currently, take the limit from
765 * the network adapter.
766 *
767 * 2) If the network adapter has a limit below the current
768 * limit, apply it.
769 */
770 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
771 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
772 pmax->tsomaxbytes = ifp->if_hw_tsomax;
773 }
774 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
775 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
776 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
777 }
778 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
779 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
780 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
781 }
782 }
783
784 /*
785 * Update TSO limit of a network adapter.
786 *
787 * Returns zero if no change. Else non-zero.
788 */
789 int
if_hw_tsomax_update(if_t ifp,struct ifnet_hw_tsomax * pmax)790 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
791 {
792 int retval = 0;
793 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
794 ifp->if_hw_tsomax = pmax->tsomaxbytes;
795 retval++;
796 }
797 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
798 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
799 retval++;
800 }
801 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
802 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
803 retval++;
804 }
805 return (retval);
806 }
807
808 static void
if_attach_internal(struct ifnet * ifp,bool vmove)809 if_attach_internal(struct ifnet *ifp, bool vmove)
810 {
811 unsigned socksize, ifasize;
812 int namelen, masklen;
813 struct sockaddr_dl *sdl;
814 struct ifaddr *ifa;
815
816 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
817
818 #ifdef VIMAGE
819 CURVNET_ASSERT_SET();
820 ifp->if_vnet = curvnet;
821 if (ifp->if_home_vnet == NULL)
822 ifp->if_home_vnet = curvnet;
823 #endif
824
825 if_addgroup(ifp, IFG_ALL);
826
827 #ifdef VIMAGE
828 /* Restore group membership for cloned interface. */
829 if (vmove)
830 if_clone_restoregroup(ifp);
831 #endif
832
833 getmicrotime(&ifp->if_lastchange);
834 ifp->if_epoch = time_uptime;
835
836 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
837 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
838 ("transmit and qflush must both either be set or both be NULL"));
839 if (ifp->if_transmit == NULL) {
840 ifp->if_transmit = if_transmit_default;
841 ifp->if_qflush = if_qflush;
842 }
843 if (ifp->if_input == NULL)
844 ifp->if_input = if_input_default;
845
846 if (ifp->if_requestencap == NULL)
847 ifp->if_requestencap = if_requestencap_default;
848
849 if (!vmove) {
850 #ifdef MAC
851 mac_ifnet_create(ifp);
852 #endif
853
854 /*
855 * Create a Link Level name for this device.
856 */
857 namelen = strlen(ifp->if_xname);
858 /*
859 * Always save enough space for any possible name so we
860 * can do a rename in place later.
861 */
862 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
863 socksize = masklen + ifp->if_addrlen;
864 if (socksize < sizeof(*sdl))
865 socksize = sizeof(*sdl);
866 socksize = roundup2(socksize, sizeof(long));
867 ifasize = sizeof(*ifa) + 2 * socksize;
868 ifa = ifa_alloc(ifasize, M_WAITOK);
869 sdl = (struct sockaddr_dl *)(ifa + 1);
870 sdl->sdl_len = socksize;
871 sdl->sdl_family = AF_LINK;
872 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
873 sdl->sdl_nlen = namelen;
874 sdl->sdl_index = ifp->if_index;
875 sdl->sdl_type = ifp->if_type;
876 ifp->if_addr = ifa;
877 ifa->ifa_ifp = ifp;
878 ifa->ifa_addr = (struct sockaddr *)sdl;
879 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
880 ifa->ifa_netmask = (struct sockaddr *)sdl;
881 sdl->sdl_len = masklen;
882 while (namelen != 0)
883 sdl->sdl_data[--namelen] = 0xff;
884 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
885 /* Reliably crash if used uninitialized. */
886 ifp->if_broadcastaddr = NULL;
887
888 if (ifp->if_type == IFT_ETHER) {
889 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
890 M_WAITOK | M_ZERO);
891 }
892
893 #if defined(INET) || defined(INET6)
894 /* Use defaults for TSO, if nothing is set */
895 if (ifp->if_hw_tsomax == 0 &&
896 ifp->if_hw_tsomaxsegcount == 0 &&
897 ifp->if_hw_tsomaxsegsize == 0) {
898 /*
899 * The TSO defaults needs to be such that an
900 * NFS mbuf list of 35 mbufs totalling just
901 * below 64K works and that a chain of mbufs
902 * can be defragged into at most 32 segments:
903 */
904 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
905 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
906 ifp->if_hw_tsomaxsegcount = 35;
907 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
908
909 /* XXX some drivers set IFCAP_TSO after ethernet attach */
910 if (ifp->if_capabilities & IFCAP_TSO) {
911 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
912 ifp->if_hw_tsomax,
913 ifp->if_hw_tsomaxsegcount,
914 ifp->if_hw_tsomaxsegsize);
915 }
916 }
917 #endif
918 }
919
920 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
921 if_link_ifnet(ifp);
922 EVENTHANDLER_INVOKE(ifnet_attached_event, ifp);
923 if (IS_DEFAULT_VNET(curvnet))
924 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
925 }
926
927 static void
if_epochalloc(void * dummy __unused)928 if_epochalloc(void *dummy __unused)
929 {
930
931 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
932 }
933 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
934
935 /*
936 * Remove any unicast or broadcast network addresses from an interface.
937 */
938 void
if_purgeaddrs(struct ifnet * ifp)939 if_purgeaddrs(struct ifnet *ifp)
940 {
941 struct ifaddr *ifa;
942
943 #ifdef INET6
944 /*
945 * Need to leave multicast addresses of proxy NDP llentries
946 * before in6_purgeifaddr() because the llentries are keys
947 * for in6_multi objects of proxy NDP entries.
948 * in6_purgeifaddr()s clean up llentries including proxy NDPs
949 * then we would lose the keys if they are called earlier.
950 */
951 in6_purge_proxy_ndp(ifp);
952 #endif
953 while (1) {
954 struct epoch_tracker et;
955
956 NET_EPOCH_ENTER(et);
957 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
958 if (ifa->ifa_addr->sa_family != AF_LINK)
959 break;
960 }
961 NET_EPOCH_EXIT(et);
962
963 if (ifa == NULL)
964 break;
965 #ifdef INET
966 /* XXX: Ugly!! ad hoc just for INET */
967 if (ifa->ifa_addr->sa_family == AF_INET) {
968 struct ifreq ifr;
969
970 bzero(&ifr, sizeof(ifr));
971 ifr.ifr_addr = *ifa->ifa_addr;
972 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
973 NULL) == 0)
974 continue;
975 }
976 #endif /* INET */
977 #ifdef INET6
978 if (ifa->ifa_addr->sa_family == AF_INET6) {
979 in6_purgeifaddr((struct in6_ifaddr *)ifa);
980 /* ifp_addrhead is already updated */
981 continue;
982 }
983 #endif /* INET6 */
984 IF_ADDR_WLOCK(ifp);
985 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
986 IF_ADDR_WUNLOCK(ifp);
987 ifa_free(ifa);
988 }
989 }
990
991 /*
992 * Remove any multicast network addresses from an interface when an ifnet
993 * is going away.
994 */
995 static void
if_purgemaddrs(struct ifnet * ifp)996 if_purgemaddrs(struct ifnet *ifp)
997 {
998 struct ifmultiaddr *ifma;
999
1000 IF_ADDR_WLOCK(ifp);
1001 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1002 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1003 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1004 if_delmulti_locked(ifp, ifma, 1);
1005 }
1006 IF_ADDR_WUNLOCK(ifp);
1007 }
1008
1009 /*
1010 * Detach an interface, removing it from the list of "active" interfaces.
1011 * If vmove flag is set on entry to if_detach_internal(), perform only a
1012 * limited subset of cleanup tasks, given that we are moving an ifnet from
1013 * one vnet to another, where it must be fully operational.
1014 *
1015 * XXXRW: There are some significant questions about event ordering, and
1016 * how to prevent things from starting to use the interface during detach.
1017 */
1018 void
if_detach(struct ifnet * ifp)1019 if_detach(struct ifnet *ifp)
1020 {
1021 bool found;
1022
1023 CURVNET_SET_QUIET(ifp->if_vnet);
1024 found = if_unlink_ifnet(ifp, false);
1025 if (found) {
1026 sx_xlock(&ifnet_detach_sxlock);
1027 if_detach_internal(ifp, false);
1028 sx_xunlock(&ifnet_detach_sxlock);
1029 }
1030 CURVNET_RESTORE();
1031 }
1032
1033 /*
1034 * The vmove flag, if set, indicates that we are called from a callpath
1035 * that is moving an interface to a different vnet instance.
1036 *
1037 * The shutdown flag, if set, indicates that we are called in the
1038 * process of shutting down a vnet instance. Currently only the
1039 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1040 * on a vnet instance shutdown without this flag being set, e.g., when
1041 * the cloned interfaces are destoyed as first thing of teardown.
1042 */
1043 static void
if_detach_internal(struct ifnet * ifp,bool vmove)1044 if_detach_internal(struct ifnet *ifp, bool vmove)
1045 {
1046 struct ifaddr *ifa;
1047 #ifdef VIMAGE
1048 bool shutdown;
1049
1050 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1051 #endif
1052
1053 sx_assert(&ifnet_detach_sxlock, SX_XLOCKED);
1054
1055 /*
1056 * At this point we know the interface still was on the ifnet list
1057 * and we removed it so we are in a stable state.
1058 */
1059 NET_EPOCH_WAIT();
1060
1061 /*
1062 * Ensure all pending EPOCH(9) callbacks have been executed. This
1063 * fixes issues about late destruction of multicast options
1064 * which lead to leave group calls, which in turn access the
1065 * belonging ifnet structure:
1066 */
1067 NET_EPOCH_DRAIN_CALLBACKS();
1068
1069 /*
1070 * In any case (destroy or vmove) detach us from the groups
1071 * and remove/wait for pending events on the taskq.
1072 * XXX-BZ in theory an interface could still enqueue a taskq change?
1073 */
1074 if_delgroups(ifp);
1075
1076 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1077 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1078
1079 if_down(ifp);
1080
1081 #ifdef VIMAGE
1082 /*
1083 * On VNET shutdown abort here as the stack teardown will do all
1084 * the work top-down for us.
1085 */
1086 if (shutdown) {
1087 /* Give interface users the chance to clean up. */
1088 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1089
1090 /*
1091 * In case of a vmove we are done here without error.
1092 * If we would signal an error it would lead to the same
1093 * abort as if we did not find the ifnet anymore.
1094 * if_detach() calls us in void context and does not care
1095 * about an early abort notification, so life is splendid :)
1096 */
1097 return;
1098 }
1099 #endif
1100
1101 /*
1102 * At this point we are not tearing down a VNET and are either
1103 * going to destroy or vmove the interface and have to cleanup
1104 * accordingly.
1105 */
1106
1107 /*
1108 * Remove routes and flush queues.
1109 */
1110 #ifdef ALTQ
1111 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1112 altq_disable(&ifp->if_snd);
1113 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1114 altq_detach(&ifp->if_snd);
1115 #endif
1116
1117 rt_flushifroutes(ifp);
1118
1119 if_purgeaddrs(ifp);
1120 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1121 if_purgemaddrs(ifp);
1122 if (IS_DEFAULT_VNET(curvnet))
1123 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1124
1125 if (!vmove) {
1126 /*
1127 * Prevent further calls into the device driver via ifnet.
1128 */
1129 if_dead(ifp);
1130
1131 /*
1132 * Clean up all addresses.
1133 */
1134 IF_ADDR_WLOCK(ifp);
1135 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1136 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1137 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1138 IF_ADDR_WUNLOCK(ifp);
1139 ifa_free(ifa);
1140 } else
1141 IF_ADDR_WUNLOCK(ifp);
1142 }
1143 }
1144
1145 #ifdef VIMAGE
1146 /*
1147 * if_vmove() performs a limited version of if_detach() in current
1148 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1149 */
1150 static void
if_vmove(struct ifnet * ifp,struct vnet * new_vnet)1151 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1152 {
1153 #ifdef DEV_BPF
1154 /*
1155 * Detach BPF file descriptors from its interface.
1156 */
1157 bpf_ifdetach(ifp);
1158 #endif
1159
1160 /*
1161 * Detach from current vnet, but preserve LLADDR info, do not
1162 * mark as dead etc. so that the ifnet can be reattached later.
1163 */
1164 if_detach_internal(ifp, true);
1165
1166 /*
1167 * Perform interface-specific reassignment tasks, if provided by
1168 * the driver.
1169 */
1170 if (ifp->if_reassign != NULL)
1171 ifp->if_reassign(ifp, new_vnet, NULL);
1172
1173 /*
1174 * Switch to the context of the target vnet.
1175 */
1176 CURVNET_SET_QUIET(new_vnet);
1177 if_attach_internal(ifp, true);
1178 bpf_vmove(ifp->if_bpf);
1179 CURVNET_RESTORE();
1180 }
1181
1182 /*
1183 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1184 */
1185 static int
if_vmove_loan(struct thread * td,struct ifnet * ifp,char * ifname,int jid)1186 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1187 {
1188 struct prison *pr;
1189 struct ifnet *difp;
1190 bool found;
1191
1192 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
1193
1194 /* Try to find the prison within our visibility. */
1195 sx_slock(&allprison_lock);
1196 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1197 sx_sunlock(&allprison_lock);
1198 if (pr == NULL)
1199 return (ENXIO);
1200 prison_hold_locked(pr);
1201 mtx_unlock(&pr->pr_mtx);
1202
1203 /* Do not try to move the iface from and to the same prison. */
1204 if (pr->pr_vnet == ifp->if_vnet) {
1205 prison_free(pr);
1206 return (EEXIST);
1207 }
1208
1209 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1210 /* XXX Lock interfaces to avoid races. */
1211 CURVNET_SET_QUIET(pr->pr_vnet);
1212 difp = ifunit(ifname);
1213 CURVNET_RESTORE();
1214 if (difp != NULL) {
1215 prison_free(pr);
1216 return (EEXIST);
1217 }
1218 sx_xlock(&ifnet_detach_sxlock);
1219
1220 found = if_unlink_ifnet(ifp, true);
1221 if (! found) {
1222 sx_xunlock(&ifnet_detach_sxlock);
1223 prison_free(pr);
1224 return (ENODEV);
1225 }
1226
1227 /* Move the interface into the child jail/vnet. */
1228 if_vmove(ifp, pr->pr_vnet);
1229
1230 /* Report the new if_xname back to the userland. */
1231 sprintf(ifname, "%s", ifp->if_xname);
1232
1233 sx_xunlock(&ifnet_detach_sxlock);
1234
1235 prison_free(pr);
1236 return (0);
1237 }
1238
1239 static int
if_vmove_reclaim(struct thread * td,char * ifname,int jid)1240 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1241 {
1242 struct prison *pr;
1243 struct vnet *vnet_dst;
1244 struct ifnet *ifp;
1245 int found __diagused;
1246
1247 /* Try to find the prison within our visibility. */
1248 sx_slock(&allprison_lock);
1249 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1250 sx_sunlock(&allprison_lock);
1251 if (pr == NULL)
1252 return (ENXIO);
1253 prison_hold_locked(pr);
1254 mtx_unlock(&pr->pr_mtx);
1255
1256 /* Make sure the named iface exists in the source prison/vnet. */
1257 CURVNET_SET(pr->pr_vnet);
1258 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1259 if (ifp == NULL) {
1260 CURVNET_RESTORE();
1261 prison_free(pr);
1262 return (ENXIO);
1263 }
1264
1265 /* Do not try to move the iface from and to the same prison. */
1266 vnet_dst = TD_TO_VNET(td);
1267 if (vnet_dst == ifp->if_vnet) {
1268 CURVNET_RESTORE();
1269 prison_free(pr);
1270 return (EEXIST);
1271 }
1272
1273 /* Get interface back from child jail/vnet. */
1274 found = if_unlink_ifnet(ifp, true);
1275 MPASS(found);
1276 sx_xlock(&ifnet_detach_sxlock);
1277 if_vmove(ifp, vnet_dst);
1278 sx_xunlock(&ifnet_detach_sxlock);
1279 CURVNET_RESTORE();
1280
1281 /* Report the new if_xname back to the userland. */
1282 sprintf(ifname, "%s", ifp->if_xname);
1283
1284 prison_free(pr);
1285 return (0);
1286 }
1287 #endif /* VIMAGE */
1288
1289 /*
1290 * Add a group to an interface
1291 */
1292 int
if_addgroup(struct ifnet * ifp,const char * groupname)1293 if_addgroup(struct ifnet *ifp, const char *groupname)
1294 {
1295 struct ifg_list *ifgl;
1296 struct ifg_group *ifg = NULL;
1297 struct ifg_member *ifgm;
1298 int new = 0;
1299
1300 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1301 groupname[strlen(groupname) - 1] <= '9')
1302 return (EINVAL);
1303
1304 IFNET_WLOCK();
1305 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1306 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1307 IFNET_WUNLOCK();
1308 return (EEXIST);
1309 }
1310
1311 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1312 IFNET_WUNLOCK();
1313 return (ENOMEM);
1314 }
1315
1316 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1317 free(ifgl, M_TEMP);
1318 IFNET_WUNLOCK();
1319 return (ENOMEM);
1320 }
1321
1322 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1323 if (!strcmp(ifg->ifg_group, groupname))
1324 break;
1325
1326 if (ifg == NULL) {
1327 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1328 free(ifgl, M_TEMP);
1329 free(ifgm, M_TEMP);
1330 IFNET_WUNLOCK();
1331 return (ENOMEM);
1332 }
1333 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1334 ifg->ifg_refcnt = 0;
1335 CK_STAILQ_INIT(&ifg->ifg_members);
1336 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1337 new = 1;
1338 }
1339
1340 ifg->ifg_refcnt++;
1341 ifgl->ifgl_group = ifg;
1342 ifgm->ifgm_ifp = ifp;
1343
1344 IF_ADDR_WLOCK(ifp);
1345 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1346 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1347 IF_ADDR_WUNLOCK(ifp);
1348
1349 IFNET_WUNLOCK();
1350
1351 if (new)
1352 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1353 EVENTHANDLER_INVOKE(group_change_event, groupname);
1354
1355 return (0);
1356 }
1357
1358 /*
1359 * Helper function to remove a group out of an interface. Expects the global
1360 * ifnet lock to be write-locked, and drops it before returning.
1361 */
1362 static void
_if_delgroup_locked(struct ifnet * ifp,struct ifg_list * ifgl,const char * groupname)1363 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1364 const char *groupname)
1365 {
1366 struct ifg_member *ifgm;
1367 bool freeifgl;
1368
1369 IFNET_WLOCK_ASSERT();
1370
1371 IF_ADDR_WLOCK(ifp);
1372 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1373 IF_ADDR_WUNLOCK(ifp);
1374
1375 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1376 if (ifgm->ifgm_ifp == ifp) {
1377 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1378 ifg_member, ifgm_next);
1379 break;
1380 }
1381 }
1382
1383 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1384 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1385 ifg_next);
1386 freeifgl = true;
1387 } else {
1388 freeifgl = false;
1389 }
1390 IFNET_WUNLOCK();
1391
1392 NET_EPOCH_WAIT();
1393 EVENTHANDLER_INVOKE(group_change_event, groupname);
1394 if (freeifgl) {
1395 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1396 free(ifgl->ifgl_group, M_TEMP);
1397 }
1398 free(ifgm, M_TEMP);
1399 free(ifgl, M_TEMP);
1400 }
1401
1402 /*
1403 * Remove a group from an interface
1404 */
1405 int
if_delgroup(struct ifnet * ifp,const char * groupname)1406 if_delgroup(struct ifnet *ifp, const char *groupname)
1407 {
1408 struct ifg_list *ifgl;
1409
1410 IFNET_WLOCK();
1411 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1412 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1413 break;
1414 if (ifgl == NULL) {
1415 IFNET_WUNLOCK();
1416 return (ENOENT);
1417 }
1418
1419 _if_delgroup_locked(ifp, ifgl, groupname);
1420
1421 return (0);
1422 }
1423
1424 /*
1425 * Remove an interface from all groups
1426 */
1427 static void
if_delgroups(struct ifnet * ifp)1428 if_delgroups(struct ifnet *ifp)
1429 {
1430 struct ifg_list *ifgl;
1431 char groupname[IFNAMSIZ];
1432
1433 IFNET_WLOCK();
1434 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1435 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1436 _if_delgroup_locked(ifp, ifgl, groupname);
1437 IFNET_WLOCK();
1438 }
1439 IFNET_WUNLOCK();
1440 }
1441
1442 /*
1443 * Stores all groups from an interface in memory pointed to by ifgr.
1444 */
1445 static int
if_getgroup(struct ifgroupreq * ifgr,struct ifnet * ifp)1446 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1447 {
1448 int len, error;
1449 struct ifg_list *ifgl;
1450 struct ifg_req ifgrq, *ifgp;
1451
1452 NET_EPOCH_ASSERT();
1453
1454 if (ifgr->ifgr_len == 0) {
1455 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1456 ifgr->ifgr_len += sizeof(struct ifg_req);
1457 return (0);
1458 }
1459
1460 len = ifgr->ifgr_len;
1461 ifgp = ifgr->ifgr_groups;
1462 /* XXX: wire */
1463 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1464 if (len < sizeof(ifgrq))
1465 return (EINVAL);
1466 bzero(&ifgrq, sizeof ifgrq);
1467 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1468 sizeof(ifgrq.ifgrq_group));
1469 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1470 return (error);
1471 len -= sizeof(ifgrq);
1472 ifgp++;
1473 }
1474
1475 return (0);
1476 }
1477
1478 /*
1479 * Stores all members of a group in memory pointed to by igfr
1480 */
1481 static int
if_getgroupmembers(struct ifgroupreq * ifgr)1482 if_getgroupmembers(struct ifgroupreq *ifgr)
1483 {
1484 struct ifg_group *ifg;
1485 struct ifg_member *ifgm;
1486 struct ifg_req ifgrq, *ifgp;
1487 int len, error;
1488
1489 IFNET_RLOCK();
1490 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1491 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1492 break;
1493 if (ifg == NULL) {
1494 IFNET_RUNLOCK();
1495 return (ENOENT);
1496 }
1497
1498 if (ifgr->ifgr_len == 0) {
1499 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1500 ifgr->ifgr_len += sizeof(ifgrq);
1501 IFNET_RUNLOCK();
1502 return (0);
1503 }
1504
1505 len = ifgr->ifgr_len;
1506 ifgp = ifgr->ifgr_groups;
1507 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1508 if (len < sizeof(ifgrq)) {
1509 IFNET_RUNLOCK();
1510 return (EINVAL);
1511 }
1512 bzero(&ifgrq, sizeof ifgrq);
1513 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1514 sizeof(ifgrq.ifgrq_member));
1515 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1516 IFNET_RUNLOCK();
1517 return (error);
1518 }
1519 len -= sizeof(ifgrq);
1520 ifgp++;
1521 }
1522 IFNET_RUNLOCK();
1523
1524 return (0);
1525 }
1526
1527 /*
1528 * Return counter values from counter(9)s stored in ifnet.
1529 */
1530 uint64_t
if_get_counter_default(struct ifnet * ifp,ift_counter cnt)1531 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1532 {
1533
1534 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1535
1536 return (counter_u64_fetch(ifp->if_counters[cnt]));
1537 }
1538
1539 /*
1540 * Increase an ifnet counter. Usually used for counters shared
1541 * between the stack and a driver, but function supports them all.
1542 */
1543 void
if_inc_counter(struct ifnet * ifp,ift_counter cnt,int64_t inc)1544 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1545 {
1546
1547 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1548
1549 counter_u64_add(ifp->if_counters[cnt], inc);
1550 }
1551
1552 /*
1553 * Copy data from ifnet to userland API structure if_data.
1554 */
1555 void
if_data_copy(struct ifnet * ifp,struct if_data * ifd)1556 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1557 {
1558
1559 ifd->ifi_type = ifp->if_type;
1560 ifd->ifi_physical = 0;
1561 ifd->ifi_addrlen = ifp->if_addrlen;
1562 ifd->ifi_hdrlen = ifp->if_hdrlen;
1563 ifd->ifi_link_state = ifp->if_link_state;
1564 ifd->ifi_vhid = 0;
1565 ifd->ifi_datalen = sizeof(struct if_data);
1566 ifd->ifi_mtu = ifp->if_mtu;
1567 ifd->ifi_metric = ifp->if_metric;
1568 ifd->ifi_baudrate = ifp->if_baudrate;
1569 ifd->ifi_hwassist = ifp->if_hwassist;
1570 ifd->ifi_epoch = ifp->if_epoch;
1571 ifd->ifi_lastchange = ifp->if_lastchange;
1572
1573 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1574 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1575 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1576 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1577 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1578 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1579 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1580 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1581 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1582 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1583 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1584 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1585 }
1586
1587 /*
1588 * Initialization, destruction and refcounting functions for ifaddrs.
1589 */
1590 struct ifaddr *
ifa_alloc(size_t size,int flags)1591 ifa_alloc(size_t size, int flags)
1592 {
1593 struct ifaddr *ifa;
1594
1595 KASSERT(size >= sizeof(struct ifaddr),
1596 ("%s: invalid size %zu", __func__, size));
1597
1598 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1599 if (ifa == NULL)
1600 return (NULL);
1601
1602 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1603 goto fail;
1604 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1605 goto fail;
1606 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1607 goto fail;
1608 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1609 goto fail;
1610
1611 refcount_init(&ifa->ifa_refcnt, 1);
1612
1613 return (ifa);
1614
1615 fail:
1616 /* free(NULL) is okay */
1617 counter_u64_free(ifa->ifa_opackets);
1618 counter_u64_free(ifa->ifa_ipackets);
1619 counter_u64_free(ifa->ifa_obytes);
1620 counter_u64_free(ifa->ifa_ibytes);
1621 free(ifa, M_IFADDR);
1622
1623 return (NULL);
1624 }
1625
1626 void
ifa_ref(struct ifaddr * ifa)1627 ifa_ref(struct ifaddr *ifa)
1628 {
1629 u_int old __diagused;
1630
1631 old = refcount_acquire(&ifa->ifa_refcnt);
1632 KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1633 }
1634
1635 int
ifa_try_ref(struct ifaddr * ifa)1636 ifa_try_ref(struct ifaddr *ifa)
1637 {
1638
1639 NET_EPOCH_ASSERT();
1640 return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1641 }
1642
1643 static void
ifa_destroy(epoch_context_t ctx)1644 ifa_destroy(epoch_context_t ctx)
1645 {
1646 struct ifaddr *ifa;
1647
1648 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1649 counter_u64_free(ifa->ifa_opackets);
1650 counter_u64_free(ifa->ifa_ipackets);
1651 counter_u64_free(ifa->ifa_obytes);
1652 counter_u64_free(ifa->ifa_ibytes);
1653 free(ifa, M_IFADDR);
1654 }
1655
1656 void
ifa_free(struct ifaddr * ifa)1657 ifa_free(struct ifaddr *ifa)
1658 {
1659
1660 if (refcount_release(&ifa->ifa_refcnt))
1661 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1662 }
1663
1664 /*
1665 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1666 * structs used to represent other address families, it is necessary
1667 * to perform a different comparison.
1668 */
1669 static bool
sa_dl_equal(const struct sockaddr * a,const struct sockaddr * b)1670 sa_dl_equal(const struct sockaddr *a, const struct sockaddr *b)
1671 {
1672 const struct sockaddr_dl *sdl1 = (const struct sockaddr_dl *)a;
1673 const struct sockaddr_dl *sdl2 = (const struct sockaddr_dl *)b;
1674
1675 return (sdl1->sdl_len == sdl2->sdl_len &&
1676 bcmp(sdl1->sdl_data + sdl1->sdl_nlen,
1677 sdl2->sdl_data + sdl2->sdl_nlen, sdl1->sdl_alen) == 0);
1678 }
1679
1680 /*
1681 * Locate an interface based on a complete address.
1682 */
1683 /*ARGSUSED*/
1684 struct ifaddr *
ifa_ifwithaddr(const struct sockaddr * addr)1685 ifa_ifwithaddr(const struct sockaddr *addr)
1686 {
1687 struct ifnet *ifp;
1688 struct ifaddr *ifa;
1689
1690 NET_EPOCH_ASSERT();
1691
1692 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1693 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1694 if (ifa->ifa_addr->sa_family != addr->sa_family)
1695 continue;
1696 if (sa_equal(addr, ifa->ifa_addr)) {
1697 goto done;
1698 }
1699 /* IP6 doesn't have broadcast */
1700 if ((ifp->if_flags & IFF_BROADCAST) &&
1701 ifa->ifa_broadaddr &&
1702 ifa->ifa_broadaddr->sa_len != 0 &&
1703 sa_equal(ifa->ifa_broadaddr, addr)) {
1704 goto done;
1705 }
1706 }
1707 }
1708 ifa = NULL;
1709 done:
1710 return (ifa);
1711 }
1712
1713 int
ifa_ifwithaddr_check(const struct sockaddr * addr)1714 ifa_ifwithaddr_check(const struct sockaddr *addr)
1715 {
1716 struct epoch_tracker et;
1717 int rc;
1718
1719 NET_EPOCH_ENTER(et);
1720 rc = (ifa_ifwithaddr(addr) != NULL);
1721 NET_EPOCH_EXIT(et);
1722 return (rc);
1723 }
1724
1725 /*
1726 * Locate an interface based on the broadcast address.
1727 */
1728 /* ARGSUSED */
1729 struct ifaddr *
ifa_ifwithbroadaddr(const struct sockaddr * addr,int fibnum)1730 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1731 {
1732 struct ifnet *ifp;
1733 struct ifaddr *ifa;
1734
1735 NET_EPOCH_ASSERT();
1736 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1737 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1738 continue;
1739 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1740 if (ifa->ifa_addr->sa_family != addr->sa_family)
1741 continue;
1742 if ((ifp->if_flags & IFF_BROADCAST) &&
1743 ifa->ifa_broadaddr &&
1744 ifa->ifa_broadaddr->sa_len != 0 &&
1745 sa_equal(ifa->ifa_broadaddr, addr)) {
1746 goto done;
1747 }
1748 }
1749 }
1750 ifa = NULL;
1751 done:
1752 return (ifa);
1753 }
1754
1755 /*
1756 * Locate the point to point interface with a given destination address.
1757 */
1758 /*ARGSUSED*/
1759 struct ifaddr *
ifa_ifwithdstaddr(const struct sockaddr * addr,int fibnum)1760 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1761 {
1762 struct ifnet *ifp;
1763 struct ifaddr *ifa;
1764
1765 NET_EPOCH_ASSERT();
1766 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1767 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1768 continue;
1769 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1770 continue;
1771 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1772 if (ifa->ifa_addr->sa_family != addr->sa_family)
1773 continue;
1774 if (ifa->ifa_dstaddr != NULL &&
1775 sa_equal(addr, ifa->ifa_dstaddr)) {
1776 goto done;
1777 }
1778 }
1779 }
1780 ifa = NULL;
1781 done:
1782 return (ifa);
1783 }
1784
1785 /*
1786 * Find an interface on a specific network. If many, choice
1787 * is most specific found.
1788 */
1789 struct ifaddr *
ifa_ifwithnet(const struct sockaddr * addr,int ignore_ptp,int fibnum)1790 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1791 {
1792 struct ifnet *ifp;
1793 struct ifaddr *ifa;
1794 struct ifaddr *ifa_maybe = NULL;
1795 u_int af = addr->sa_family;
1796 const char *addr_data = addr->sa_data, *cplim;
1797
1798 NET_EPOCH_ASSERT();
1799 /*
1800 * AF_LINK addresses can be looked up directly by their index number,
1801 * so do that if we can.
1802 */
1803 if (af == AF_LINK) {
1804 ifp = ifnet_byindex(
1805 ((const struct sockaddr_dl *)addr)->sdl_index);
1806 return (ifp ? ifp->if_addr : NULL);
1807 }
1808
1809 /*
1810 * Scan though each interface, looking for ones that have addresses
1811 * in this address family and the requested fib.
1812 */
1813 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1814 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1815 continue;
1816 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1817 const char *cp, *cp2, *cp3;
1818
1819 if (ifa->ifa_addr->sa_family != af)
1820 next: continue;
1821 if (af == AF_INET &&
1822 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1823 /*
1824 * This is a bit broken as it doesn't
1825 * take into account that the remote end may
1826 * be a single node in the network we are
1827 * looking for.
1828 * The trouble is that we don't know the
1829 * netmask for the remote end.
1830 */
1831 if (ifa->ifa_dstaddr != NULL &&
1832 sa_equal(addr, ifa->ifa_dstaddr)) {
1833 goto done;
1834 }
1835 } else {
1836 /*
1837 * Scan all the bits in the ifa's address.
1838 * If a bit dissagrees with what we are
1839 * looking for, mask it with the netmask
1840 * to see if it really matters.
1841 * (A byte at a time)
1842 */
1843 if (ifa->ifa_netmask == 0)
1844 continue;
1845 cp = addr_data;
1846 cp2 = ifa->ifa_addr->sa_data;
1847 cp3 = ifa->ifa_netmask->sa_data;
1848 cplim = ifa->ifa_netmask->sa_len
1849 + (char *)ifa->ifa_netmask;
1850 while (cp3 < cplim)
1851 if ((*cp++ ^ *cp2++) & *cp3++)
1852 goto next; /* next address! */
1853 /*
1854 * If the netmask of what we just found
1855 * is more specific than what we had before
1856 * (if we had one), or if the virtual status
1857 * of new prefix is better than of the old one,
1858 * then remember the new one before continuing
1859 * to search for an even better one.
1860 */
1861 if (ifa_maybe == NULL ||
1862 ifa_preferred(ifa_maybe, ifa) ||
1863 rn_refines((caddr_t)ifa->ifa_netmask,
1864 (caddr_t)ifa_maybe->ifa_netmask)) {
1865 ifa_maybe = ifa;
1866 }
1867 }
1868 }
1869 }
1870 ifa = ifa_maybe;
1871 ifa_maybe = NULL;
1872 done:
1873 return (ifa);
1874 }
1875
1876 /*
1877 * Find an interface address specific to an interface best matching
1878 * a given address.
1879 */
1880 struct ifaddr *
ifaof_ifpforaddr(const struct sockaddr * addr,struct ifnet * ifp)1881 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1882 {
1883 struct ifaddr *ifa;
1884 const char *cp, *cp2, *cp3;
1885 char *cplim;
1886 struct ifaddr *ifa_maybe = NULL;
1887 u_int af = addr->sa_family;
1888
1889 if (af >= AF_MAX)
1890 return (NULL);
1891
1892 NET_EPOCH_ASSERT();
1893 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1894 if (ifa->ifa_addr->sa_family != af)
1895 continue;
1896 if (ifa_maybe == NULL)
1897 ifa_maybe = ifa;
1898 if (ifa->ifa_netmask == 0) {
1899 if (sa_equal(addr, ifa->ifa_addr) ||
1900 (ifa->ifa_dstaddr &&
1901 sa_equal(addr, ifa->ifa_dstaddr)))
1902 goto done;
1903 continue;
1904 }
1905 if (ifp->if_flags & IFF_POINTOPOINT) {
1906 if (ifa->ifa_dstaddr && sa_equal(addr, ifa->ifa_dstaddr))
1907 goto done;
1908 } else {
1909 cp = addr->sa_data;
1910 cp2 = ifa->ifa_addr->sa_data;
1911 cp3 = ifa->ifa_netmask->sa_data;
1912 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1913 for (; cp3 < cplim; cp3++)
1914 if ((*cp++ ^ *cp2++) & *cp3)
1915 break;
1916 if (cp3 == cplim)
1917 goto done;
1918 }
1919 }
1920 ifa = ifa_maybe;
1921 done:
1922 return (ifa);
1923 }
1924
1925 /*
1926 * See whether new ifa is better than current one:
1927 * 1) A non-virtual one is preferred over virtual.
1928 * 2) A virtual in master state preferred over any other state.
1929 *
1930 * Used in several address selecting functions.
1931 */
1932 int
ifa_preferred(struct ifaddr * cur,struct ifaddr * next)1933 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1934 {
1935
1936 return (cur->ifa_carp && (!next->ifa_carp ||
1937 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
1938 }
1939
1940 struct sockaddr_dl *
link_alloc_sdl(size_t size,int flags)1941 link_alloc_sdl(size_t size, int flags)
1942 {
1943
1944 return (malloc(size, M_TEMP, flags));
1945 }
1946
1947 void
link_free_sdl(struct sockaddr * sa)1948 link_free_sdl(struct sockaddr *sa)
1949 {
1950 free(sa, M_TEMP);
1951 }
1952
1953 /*
1954 * Fills in given sdl with interface basic info.
1955 * Returns pointer to filled sdl.
1956 */
1957 struct sockaddr_dl *
link_init_sdl(struct ifnet * ifp,struct sockaddr * paddr,u_char iftype)1958 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
1959 {
1960 struct sockaddr_dl *sdl;
1961
1962 sdl = (struct sockaddr_dl *)paddr;
1963 memset(sdl, 0, sizeof(struct sockaddr_dl));
1964 sdl->sdl_len = sizeof(struct sockaddr_dl);
1965 sdl->sdl_family = AF_LINK;
1966 sdl->sdl_index = ifp->if_index;
1967 sdl->sdl_type = iftype;
1968
1969 return (sdl);
1970 }
1971
1972 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1973 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1974 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
1975 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
1976 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
1977 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
1978 int (*vlan_setcookie_p)(struct ifnet *, void *);
1979 void *(*vlan_cookie_p)(struct ifnet *);
1980 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
1981
1982 /*
1983 * Handle a change in the interface link state. To avoid LORs
1984 * between driver lock and upper layer locks, as well as possible
1985 * recursions, we post event to taskqueue, and all job
1986 * is done in static do_link_state_change().
1987 */
1988 void
if_link_state_change(struct ifnet * ifp,int link_state)1989 if_link_state_change(struct ifnet *ifp, int link_state)
1990 {
1991 /* Return if state hasn't changed. */
1992 if (ifp->if_link_state == link_state)
1993 return;
1994
1995 ifp->if_link_state = link_state;
1996
1997 /* XXXGL: reference ifp? */
1998 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1999 }
2000
2001 static void
do_link_state_change(void * arg,int pending)2002 do_link_state_change(void *arg, int pending)
2003 {
2004 struct ifnet *ifp;
2005 int link_state;
2006
2007 ifp = arg;
2008 link_state = ifp->if_link_state;
2009
2010 CURVNET_SET(ifp->if_vnet);
2011 rt_ifmsg(ifp, 0);
2012 if (ifp->if_vlantrunk != NULL)
2013 (*vlan_link_state_p)(ifp);
2014 if (ifp->if_carp)
2015 (*carp_linkstate_p)(ifp);
2016 if (ifp->if_bridge)
2017 ifp->if_bridge_linkstate(ifp);
2018 if (ifp->if_lagg)
2019 (*lagg_linkstate_p)(ifp, link_state);
2020
2021 if (IS_DEFAULT_VNET(curvnet))
2022 devctl_notify("IFNET", ifp->if_xname,
2023 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2024 NULL);
2025 if (pending > 1)
2026 if_printf(ifp, "%d link states coalesced\n", pending);
2027 if (log_link_state_change)
2028 if_printf(ifp, "link state changed to %s\n",
2029 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2030 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2031 CURVNET_RESTORE();
2032 }
2033
2034 /*
2035 * Mark an interface down and notify protocols of
2036 * the transition.
2037 */
2038 void
if_down(struct ifnet * ifp)2039 if_down(struct ifnet *ifp)
2040 {
2041
2042 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2043
2044 ifp->if_flags &= ~IFF_UP;
2045 getmicrotime(&ifp->if_lastchange);
2046 ifp->if_qflush(ifp);
2047
2048 if (ifp->if_carp)
2049 (*carp_linkstate_p)(ifp);
2050 rt_ifmsg(ifp, IFF_UP);
2051 }
2052
2053 /*
2054 * Mark an interface up and notify protocols of
2055 * the transition.
2056 */
2057 void
if_up(struct ifnet * ifp)2058 if_up(struct ifnet *ifp)
2059 {
2060
2061 ifp->if_flags |= IFF_UP;
2062 getmicrotime(&ifp->if_lastchange);
2063 if (ifp->if_carp)
2064 (*carp_linkstate_p)(ifp);
2065 rt_ifmsg(ifp, IFF_UP);
2066 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2067 }
2068
2069 /*
2070 * Flush an interface queue.
2071 */
2072 void
if_qflush(struct ifnet * ifp)2073 if_qflush(struct ifnet *ifp)
2074 {
2075 struct mbuf *m, *n;
2076 struct ifaltq *ifq;
2077
2078 ifq = &ifp->if_snd;
2079 IFQ_LOCK(ifq);
2080 #ifdef ALTQ
2081 if (ALTQ_IS_ENABLED(ifq))
2082 ALTQ_PURGE(ifq);
2083 #endif
2084 n = ifq->ifq_head;
2085 while ((m = n) != NULL) {
2086 n = m->m_nextpkt;
2087 m_freem(m);
2088 }
2089 ifq->ifq_head = 0;
2090 ifq->ifq_tail = 0;
2091 ifq->ifq_len = 0;
2092 IFQ_UNLOCK(ifq);
2093 }
2094
2095 /*
2096 * Map interface name to interface structure pointer, with or without
2097 * returning a reference.
2098 */
2099 struct ifnet *
ifunit_ref(const char * name)2100 ifunit_ref(const char *name)
2101 {
2102 struct epoch_tracker et;
2103 struct ifnet *ifp;
2104
2105 NET_EPOCH_ENTER(et);
2106 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2107 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2108 !(ifp->if_flags & IFF_DYING))
2109 break;
2110 }
2111 if (ifp != NULL) {
2112 if_ref(ifp);
2113 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
2114 }
2115
2116 NET_EPOCH_EXIT(et);
2117 return (ifp);
2118 }
2119
2120 struct ifnet *
ifunit(const char * name)2121 ifunit(const char *name)
2122 {
2123 struct epoch_tracker et;
2124 struct ifnet *ifp;
2125
2126 NET_EPOCH_ENTER(et);
2127 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2128 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2129 break;
2130 }
2131 NET_EPOCH_EXIT(et);
2132 return (ifp);
2133 }
2134
2135 void *
ifr_buffer_get_buffer(void * data)2136 ifr_buffer_get_buffer(void *data)
2137 {
2138 union ifreq_union *ifrup;
2139
2140 ifrup = data;
2141 #ifdef COMPAT_FREEBSD32
2142 if (SV_CURPROC_FLAG(SV_ILP32))
2143 return ((void *)(uintptr_t)
2144 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2145 #endif
2146 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2147 }
2148
2149 static void
ifr_buffer_set_buffer_null(void * data)2150 ifr_buffer_set_buffer_null(void *data)
2151 {
2152 union ifreq_union *ifrup;
2153
2154 ifrup = data;
2155 #ifdef COMPAT_FREEBSD32
2156 if (SV_CURPROC_FLAG(SV_ILP32))
2157 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2158 else
2159 #endif
2160 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2161 }
2162
2163 size_t
ifr_buffer_get_length(void * data)2164 ifr_buffer_get_length(void *data)
2165 {
2166 union ifreq_union *ifrup;
2167
2168 ifrup = data;
2169 #ifdef COMPAT_FREEBSD32
2170 if (SV_CURPROC_FLAG(SV_ILP32))
2171 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2172 #endif
2173 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2174 }
2175
2176 static void
ifr_buffer_set_length(void * data,size_t len)2177 ifr_buffer_set_length(void *data, size_t len)
2178 {
2179 union ifreq_union *ifrup;
2180
2181 ifrup = data;
2182 #ifdef COMPAT_FREEBSD32
2183 if (SV_CURPROC_FLAG(SV_ILP32))
2184 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2185 else
2186 #endif
2187 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2188 }
2189
2190 void *
ifr_data_get_ptr(void * ifrp)2191 ifr_data_get_ptr(void *ifrp)
2192 {
2193 union ifreq_union *ifrup;
2194
2195 ifrup = ifrp;
2196 #ifdef COMPAT_FREEBSD32
2197 if (SV_CURPROC_FLAG(SV_ILP32))
2198 return ((void *)(uintptr_t)
2199 ifrup->ifr32.ifr_ifru.ifru_data);
2200 #endif
2201 return (ifrup->ifr.ifr_ifru.ifru_data);
2202 }
2203
2204 struct ifcap_nv_bit_name {
2205 uint64_t cap_bit;
2206 const char *cap_name;
2207 };
2208 #define CAPNV(x) {.cap_bit = IFCAP_##x, \
2209 .cap_name = __CONCAT(IFCAP_, __CONCAT(x, _NAME)) }
2210 const struct ifcap_nv_bit_name ifcap_nv_bit_names[] = {
2211 CAPNV(RXCSUM),
2212 CAPNV(TXCSUM),
2213 CAPNV(NETCONS),
2214 CAPNV(VLAN_MTU),
2215 CAPNV(VLAN_HWTAGGING),
2216 CAPNV(JUMBO_MTU),
2217 CAPNV(POLLING),
2218 CAPNV(VLAN_HWCSUM),
2219 CAPNV(TSO4),
2220 CAPNV(TSO6),
2221 CAPNV(LRO),
2222 CAPNV(WOL_UCAST),
2223 CAPNV(WOL_MCAST),
2224 CAPNV(WOL_MAGIC),
2225 CAPNV(TOE4),
2226 CAPNV(TOE6),
2227 CAPNV(VLAN_HWFILTER),
2228 CAPNV(VLAN_HWTSO),
2229 CAPNV(LINKSTATE),
2230 CAPNV(NETMAP),
2231 CAPNV(RXCSUM_IPV6),
2232 CAPNV(TXCSUM_IPV6),
2233 CAPNV(HWSTATS),
2234 CAPNV(TXRTLMT),
2235 CAPNV(HWRXTSTMP),
2236 CAPNV(MEXTPG),
2237 CAPNV(TXTLS4),
2238 CAPNV(TXTLS6),
2239 CAPNV(VXLAN_HWCSUM),
2240 CAPNV(VXLAN_HWTSO),
2241 CAPNV(TXTLS_RTLMT),
2242 {0, NULL}
2243 };
2244 #define CAP2NV(x) {.cap_bit = IFCAP2_BIT(IFCAP2_##x), \
2245 .cap_name = __CONCAT(IFCAP2_, __CONCAT(x, _NAME)) }
2246 const struct ifcap_nv_bit_name ifcap2_nv_bit_names[] = {
2247 CAP2NV(RXTLS4),
2248 CAP2NV(RXTLS6),
2249 CAP2NV(IPSEC_OFFLOAD),
2250 {0, NULL}
2251 };
2252 #undef CAPNV
2253 #undef CAP2NV
2254
2255 int
if_capnv_to_capint(const nvlist_t * nv,int * old_cap,const struct ifcap_nv_bit_name * nn,bool all)2256 if_capnv_to_capint(const nvlist_t *nv, int *old_cap,
2257 const struct ifcap_nv_bit_name *nn, bool all)
2258 {
2259 int i, res;
2260
2261 res = 0;
2262 for (i = 0; nn[i].cap_name != NULL; i++) {
2263 if (nvlist_exists_bool(nv, nn[i].cap_name)) {
2264 if (all || nvlist_get_bool(nv, nn[i].cap_name))
2265 res |= nn[i].cap_bit;
2266 } else {
2267 res |= *old_cap & nn[i].cap_bit;
2268 }
2269 }
2270 return (res);
2271 }
2272
2273 void
if_capint_to_capnv(nvlist_t * nv,const struct ifcap_nv_bit_name * nn,int ifr_cap,int ifr_req)2274 if_capint_to_capnv(nvlist_t *nv, const struct ifcap_nv_bit_name *nn,
2275 int ifr_cap, int ifr_req)
2276 {
2277 int i;
2278
2279 for (i = 0; nn[i].cap_name != NULL; i++) {
2280 if ((nn[i].cap_bit & ifr_cap) != 0) {
2281 nvlist_add_bool(nv, nn[i].cap_name,
2282 (nn[i].cap_bit & ifr_req) != 0);
2283 }
2284 }
2285 }
2286
2287 /*
2288 * Hardware specific interface ioctls.
2289 */
2290 int
ifhwioctl(u_long cmd,struct ifnet * ifp,caddr_t data,struct thread * td)2291 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2292 {
2293 struct ifreq *ifr;
2294 int error = 0, do_ifup = 0;
2295 int new_flags, temp_flags;
2296 size_t descrlen, nvbuflen;
2297 char *descrbuf;
2298 char new_name[IFNAMSIZ];
2299 void *buf;
2300 nvlist_t *nvcap;
2301 struct siocsifcapnv_driver_data drv_ioctl_data;
2302
2303 ifr = (struct ifreq *)data;
2304 switch (cmd) {
2305 case SIOCGIFINDEX:
2306 ifr->ifr_index = ifp->if_index;
2307 break;
2308
2309 case SIOCGIFFLAGS:
2310 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2311 ifr->ifr_flags = temp_flags & 0xffff;
2312 ifr->ifr_flagshigh = temp_flags >> 16;
2313 break;
2314
2315 case SIOCGIFCAP:
2316 ifr->ifr_reqcap = ifp->if_capabilities;
2317 ifr->ifr_curcap = ifp->if_capenable;
2318 break;
2319
2320 case SIOCGIFCAPNV:
2321 if ((ifp->if_capabilities & IFCAP_NV) == 0) {
2322 error = EINVAL;
2323 break;
2324 }
2325 buf = NULL;
2326 nvcap = nvlist_create(0);
2327 for (;;) {
2328 if_capint_to_capnv(nvcap, ifcap_nv_bit_names,
2329 ifp->if_capabilities, ifp->if_capenable);
2330 if_capint_to_capnv(nvcap, ifcap2_nv_bit_names,
2331 ifp->if_capabilities2, ifp->if_capenable2);
2332 error = (*ifp->if_ioctl)(ifp, SIOCGIFCAPNV,
2333 __DECONST(caddr_t, nvcap));
2334 if (error != 0) {
2335 if_printf(ifp,
2336 "SIOCGIFCAPNV driver mistake: nvlist error %d\n",
2337 error);
2338 break;
2339 }
2340 buf = nvlist_pack(nvcap, &nvbuflen);
2341 if (buf == NULL) {
2342 error = nvlist_error(nvcap);
2343 if (error == 0)
2344 error = EDOOFUS;
2345 break;
2346 }
2347 if (nvbuflen > ifr->ifr_cap_nv.buf_length) {
2348 ifr->ifr_cap_nv.length = nvbuflen;
2349 ifr->ifr_cap_nv.buffer = NULL;
2350 error = EFBIG;
2351 break;
2352 }
2353 ifr->ifr_cap_nv.length = nvbuflen;
2354 error = copyout(buf, ifr->ifr_cap_nv.buffer, nvbuflen);
2355 break;
2356 }
2357 free(buf, M_NVLIST);
2358 nvlist_destroy(nvcap);
2359 break;
2360
2361 case SIOCGIFDATA:
2362 {
2363 struct if_data ifd;
2364
2365 /* Ensure uninitialised padding is not leaked. */
2366 memset(&ifd, 0, sizeof(ifd));
2367
2368 if_data_copy(ifp, &ifd);
2369 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2370 break;
2371 }
2372
2373 #ifdef MAC
2374 case SIOCGIFMAC:
2375 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2376 break;
2377 #endif
2378
2379 case SIOCGIFMETRIC:
2380 ifr->ifr_metric = ifp->if_metric;
2381 break;
2382
2383 case SIOCGIFMTU:
2384 ifr->ifr_mtu = ifp->if_mtu;
2385 break;
2386
2387 case SIOCGIFPHYS:
2388 /* XXXGL: did this ever worked? */
2389 ifr->ifr_phys = 0;
2390 break;
2391
2392 case SIOCGIFDESCR:
2393 error = 0;
2394 sx_slock(&ifdescr_sx);
2395 if (ifp->if_description == NULL)
2396 error = ENOMSG;
2397 else {
2398 /* space for terminating nul */
2399 descrlen = strlen(ifp->if_description) + 1;
2400 if (ifr_buffer_get_length(ifr) < descrlen)
2401 ifr_buffer_set_buffer_null(ifr);
2402 else
2403 error = copyout(ifp->if_description,
2404 ifr_buffer_get_buffer(ifr), descrlen);
2405 ifr_buffer_set_length(ifr, descrlen);
2406 }
2407 sx_sunlock(&ifdescr_sx);
2408 break;
2409
2410 case SIOCSIFDESCR:
2411 error = priv_check(td, PRIV_NET_SETIFDESCR);
2412 if (error)
2413 return (error);
2414
2415 /*
2416 * Copy only (length-1) bytes to make sure that
2417 * if_description is always nul terminated. The
2418 * length parameter is supposed to count the
2419 * terminating nul in.
2420 */
2421 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2422 return (ENAMETOOLONG);
2423 else if (ifr_buffer_get_length(ifr) == 0)
2424 descrbuf = NULL;
2425 else {
2426 descrbuf = if_allocdescr(ifr_buffer_get_length(ifr), M_WAITOK);
2427 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2428 ifr_buffer_get_length(ifr) - 1);
2429 if (error) {
2430 if_freedescr(descrbuf);
2431 break;
2432 }
2433 }
2434
2435 if_setdescr(ifp, descrbuf);
2436 getmicrotime(&ifp->if_lastchange);
2437 break;
2438
2439 case SIOCGIFFIB:
2440 ifr->ifr_fib = ifp->if_fib;
2441 break;
2442
2443 case SIOCSIFFIB:
2444 error = priv_check(td, PRIV_NET_SETIFFIB);
2445 if (error)
2446 return (error);
2447 if (ifr->ifr_fib >= rt_numfibs)
2448 return (EINVAL);
2449
2450 ifp->if_fib = ifr->ifr_fib;
2451 break;
2452
2453 case SIOCSIFFLAGS:
2454 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2455 if (error)
2456 return (error);
2457 /*
2458 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2459 * check, so we don't need special handling here yet.
2460 */
2461 new_flags = (ifr->ifr_flags & 0xffff) |
2462 (ifr->ifr_flagshigh << 16);
2463 if (ifp->if_flags & IFF_UP &&
2464 (new_flags & IFF_UP) == 0) {
2465 if_down(ifp);
2466 } else if (new_flags & IFF_UP &&
2467 (ifp->if_flags & IFF_UP) == 0) {
2468 do_ifup = 1;
2469 }
2470
2471 /*
2472 * See if the promiscuous mode or allmulti bits are about to
2473 * flip. They require special handling because in-kernel
2474 * consumers may indepdently toggle them.
2475 */
2476 if_setppromisc(ifp, new_flags & IFF_PPROMISC);
2477 if ((ifp->if_flags ^ new_flags) & IFF_PALLMULTI) {
2478 if (new_flags & IFF_PALLMULTI)
2479 ifp->if_flags |= IFF_ALLMULTI;
2480 else if (ifp->if_amcount == 0)
2481 ifp->if_flags &= ~IFF_ALLMULTI;
2482 }
2483 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2484 (new_flags &~ IFF_CANTCHANGE);
2485 if (ifp->if_ioctl) {
2486 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2487 }
2488 if (do_ifup)
2489 if_up(ifp);
2490 getmicrotime(&ifp->if_lastchange);
2491 break;
2492
2493 case SIOCSIFCAP:
2494 error = priv_check(td, PRIV_NET_SETIFCAP);
2495 if (error != 0)
2496 return (error);
2497 if (ifp->if_ioctl == NULL)
2498 return (EOPNOTSUPP);
2499 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2500 return (EINVAL);
2501 error = (*ifp->if_ioctl)(ifp, cmd, data);
2502 if (error == 0)
2503 getmicrotime(&ifp->if_lastchange);
2504 break;
2505
2506 case SIOCSIFCAPNV:
2507 error = priv_check(td, PRIV_NET_SETIFCAP);
2508 if (error != 0)
2509 return (error);
2510 if (ifp->if_ioctl == NULL)
2511 return (EOPNOTSUPP);
2512 if ((ifp->if_capabilities & IFCAP_NV) == 0)
2513 return (EINVAL);
2514 if (ifr->ifr_cap_nv.length > IFR_CAP_NV_MAXBUFSIZE)
2515 return (EINVAL);
2516 nvcap = NULL;
2517 buf = malloc(ifr->ifr_cap_nv.length, M_TEMP, M_WAITOK);
2518 for (;;) {
2519 error = copyin(ifr->ifr_cap_nv.buffer, buf,
2520 ifr->ifr_cap_nv.length);
2521 if (error != 0)
2522 break;
2523 nvcap = nvlist_unpack(buf, ifr->ifr_cap_nv.length, 0);
2524 if (nvcap == NULL) {
2525 error = EINVAL;
2526 break;
2527 }
2528 drv_ioctl_data.reqcap = if_capnv_to_capint(nvcap,
2529 &ifp->if_capenable, ifcap_nv_bit_names, false);
2530 if ((drv_ioctl_data.reqcap &
2531 ~ifp->if_capabilities) != 0) {
2532 error = EINVAL;
2533 break;
2534 }
2535 drv_ioctl_data.reqcap2 = if_capnv_to_capint(nvcap,
2536 &ifp->if_capenable2, ifcap2_nv_bit_names, false);
2537 if ((drv_ioctl_data.reqcap2 &
2538 ~ifp->if_capabilities2) != 0) {
2539 error = EINVAL;
2540 break;
2541 }
2542 drv_ioctl_data.nvcap = nvcap;
2543 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAPNV,
2544 (caddr_t)&drv_ioctl_data);
2545 break;
2546 }
2547 nvlist_destroy(nvcap);
2548 free(buf, M_TEMP);
2549 if (error == 0)
2550 getmicrotime(&ifp->if_lastchange);
2551 break;
2552
2553 #ifdef MAC
2554 case SIOCSIFMAC:
2555 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2556 break;
2557 #endif
2558
2559 case SIOCSIFNAME:
2560 error = priv_check(td, PRIV_NET_SETIFNAME);
2561 if (error)
2562 return (error);
2563 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2564 NULL);
2565 if (error != 0)
2566 return (error);
2567 error = if_rename(ifp, new_name);
2568 break;
2569
2570 #ifdef VIMAGE
2571 case SIOCSIFVNET:
2572 error = priv_check(td, PRIV_NET_SETIFVNET);
2573 if (error)
2574 return (error);
2575 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2576 break;
2577 #endif
2578
2579 case SIOCSIFMETRIC:
2580 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2581 if (error)
2582 return (error);
2583 ifp->if_metric = ifr->ifr_metric;
2584 getmicrotime(&ifp->if_lastchange);
2585 break;
2586
2587 case SIOCSIFPHYS:
2588 error = priv_check(td, PRIV_NET_SETIFPHYS);
2589 if (error)
2590 return (error);
2591 if (ifp->if_ioctl == NULL)
2592 return (EOPNOTSUPP);
2593 error = (*ifp->if_ioctl)(ifp, cmd, data);
2594 if (error == 0)
2595 getmicrotime(&ifp->if_lastchange);
2596 break;
2597
2598 case SIOCSIFMTU:
2599 {
2600 u_long oldmtu = ifp->if_mtu;
2601
2602 error = priv_check(td, PRIV_NET_SETIFMTU);
2603 if (error)
2604 return (error);
2605 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2606 return (EINVAL);
2607 if (ifp->if_ioctl == NULL)
2608 return (EOPNOTSUPP);
2609 /* Disallow MTU changes on bridge member interfaces. */
2610 if (ifp->if_bridge)
2611 return (EOPNOTSUPP);
2612 error = (*ifp->if_ioctl)(ifp, cmd, data);
2613 if (error == 0) {
2614 getmicrotime(&ifp->if_lastchange);
2615 rt_ifmsg(ifp, 0);
2616 #ifdef INET
2617 DEBUGNET_NOTIFY_MTU(ifp);
2618 #endif
2619 }
2620 /*
2621 * If the link MTU changed, do network layer specific procedure.
2622 */
2623 if (ifp->if_mtu != oldmtu)
2624 if_notifymtu(ifp);
2625 break;
2626 }
2627
2628 case SIOCADDMULTI:
2629 case SIOCDELMULTI:
2630 if (cmd == SIOCADDMULTI)
2631 error = priv_check(td, PRIV_NET_ADDMULTI);
2632 else
2633 error = priv_check(td, PRIV_NET_DELMULTI);
2634 if (error)
2635 return (error);
2636
2637 /* Don't allow group membership on non-multicast interfaces. */
2638 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2639 return (EOPNOTSUPP);
2640
2641 /* Don't let users screw up protocols' entries. */
2642 if (ifr->ifr_addr.sa_family != AF_LINK)
2643 return (EINVAL);
2644
2645 if (cmd == SIOCADDMULTI) {
2646 struct epoch_tracker et;
2647 struct ifmultiaddr *ifma;
2648
2649 /*
2650 * Userland is only permitted to join groups once
2651 * via the if_addmulti() KPI, because it cannot hold
2652 * struct ifmultiaddr * between calls. It may also
2653 * lose a race while we check if the membership
2654 * already exists.
2655 */
2656 NET_EPOCH_ENTER(et);
2657 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2658 NET_EPOCH_EXIT(et);
2659 if (ifma != NULL)
2660 error = EADDRINUSE;
2661 else
2662 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2663 } else {
2664 error = if_delmulti(ifp, &ifr->ifr_addr);
2665 }
2666 if (error == 0)
2667 getmicrotime(&ifp->if_lastchange);
2668 break;
2669
2670 case SIOCSIFPHYADDR:
2671 case SIOCDIFPHYADDR:
2672 #ifdef INET6
2673 case SIOCSIFPHYADDR_IN6:
2674 #endif
2675 case SIOCSIFMEDIA:
2676 case SIOCSIFGENERIC:
2677 error = priv_check(td, PRIV_NET_HWIOCTL);
2678 if (error)
2679 return (error);
2680 if (ifp->if_ioctl == NULL)
2681 return (EOPNOTSUPP);
2682 error = (*ifp->if_ioctl)(ifp, cmd, data);
2683 if (error == 0)
2684 getmicrotime(&ifp->if_lastchange);
2685 break;
2686
2687 case SIOCGIFSTATUS:
2688 case SIOCGIFPSRCADDR:
2689 case SIOCGIFPDSTADDR:
2690 case SIOCGIFMEDIA:
2691 case SIOCGIFXMEDIA:
2692 case SIOCGIFGENERIC:
2693 case SIOCGIFRSSKEY:
2694 case SIOCGIFRSSHASH:
2695 case SIOCGIFDOWNREASON:
2696 if (ifp->if_ioctl == NULL)
2697 return (EOPNOTSUPP);
2698 error = (*ifp->if_ioctl)(ifp, cmd, data);
2699 break;
2700
2701 case SIOCSIFLLADDR:
2702 error = priv_check(td, PRIV_NET_SETLLADDR);
2703 if (error)
2704 return (error);
2705 error = if_setlladdr(ifp,
2706 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2707 break;
2708
2709 case SIOCGHWADDR:
2710 error = if_gethwaddr(ifp, ifr);
2711 break;
2712
2713 case SIOCAIFGROUP:
2714 {
2715 const char *groupname;
2716
2717 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2718 if (error)
2719 return (error);
2720 groupname = ((struct ifgroupreq *)data)->ifgr_group;
2721 if (strnlen(groupname, IFNAMSIZ) == IFNAMSIZ)
2722 return (EINVAL);
2723 error = if_addgroup(ifp, groupname);
2724 if (error != 0)
2725 return (error);
2726 break;
2727 }
2728 case SIOCGIFGROUP:
2729 {
2730 struct epoch_tracker et;
2731
2732 NET_EPOCH_ENTER(et);
2733 error = if_getgroup((struct ifgroupreq *)data, ifp);
2734 NET_EPOCH_EXIT(et);
2735 break;
2736 }
2737
2738 case SIOCDIFGROUP:
2739 {
2740 const char *groupname;
2741
2742 error = priv_check(td, PRIV_NET_DELIFGROUP);
2743 if (error)
2744 return (error);
2745 groupname = ((struct ifgroupreq *)data)->ifgr_group;
2746 if (strnlen(groupname, IFNAMSIZ) == IFNAMSIZ)
2747 return (EINVAL);
2748 error = if_delgroup(ifp, groupname);
2749 if (error != 0)
2750 return (error);
2751 break;
2752 }
2753 default:
2754 error = ENOIOCTL;
2755 break;
2756 }
2757 return (error);
2758 }
2759
2760 /*
2761 * Interface ioctls.
2762 */
2763 int
ifioctl(struct socket * so,u_long cmd,caddr_t data,struct thread * td)2764 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2765 {
2766 #ifdef COMPAT_FREEBSD32
2767 union {
2768 struct ifconf ifc;
2769 struct ifdrv ifd;
2770 struct ifgroupreq ifgr;
2771 struct ifmediareq ifmr;
2772 } thunk;
2773 u_long saved_cmd;
2774 struct ifconf32 *ifc32;
2775 struct ifdrv32 *ifd32;
2776 struct ifgroupreq32 *ifgr32;
2777 struct ifmediareq32 *ifmr32;
2778 #endif
2779 struct ifnet *ifp;
2780 struct ifreq *ifr;
2781 int error;
2782 int oif_flags;
2783 #ifdef VIMAGE
2784 bool shutdown;
2785 #endif
2786
2787 CURVNET_SET(so->so_vnet);
2788 #ifdef VIMAGE
2789 /* Make sure the VNET is stable. */
2790 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2791 if (shutdown) {
2792 CURVNET_RESTORE();
2793 return (EBUSY);
2794 }
2795 #endif
2796
2797 #ifdef COMPAT_FREEBSD32
2798 saved_cmd = cmd;
2799 switch (cmd) {
2800 case SIOCGIFCONF32:
2801 ifc32 = (struct ifconf32 *)data;
2802 thunk.ifc.ifc_len = ifc32->ifc_len;
2803 thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2804 data = (caddr_t)&thunk.ifc;
2805 cmd = SIOCGIFCONF;
2806 break;
2807 case SIOCGDRVSPEC32:
2808 case SIOCSDRVSPEC32:
2809 ifd32 = (struct ifdrv32 *)data;
2810 memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
2811 sizeof(thunk.ifd.ifd_name));
2812 thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
2813 thunk.ifd.ifd_len = ifd32->ifd_len;
2814 thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
2815 data = (caddr_t)&thunk.ifd;
2816 cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
2817 break;
2818 case SIOCAIFGROUP32:
2819 case SIOCGIFGROUP32:
2820 case SIOCDIFGROUP32:
2821 case SIOCGIFGMEMB32:
2822 ifgr32 = (struct ifgroupreq32 *)data;
2823 memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name,
2824 sizeof(thunk.ifgr.ifgr_name));
2825 thunk.ifgr.ifgr_len = ifgr32->ifgr_len;
2826 switch (cmd) {
2827 case SIOCAIFGROUP32:
2828 case SIOCDIFGROUP32:
2829 memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group,
2830 sizeof(thunk.ifgr.ifgr_group));
2831 break;
2832 case SIOCGIFGROUP32:
2833 case SIOCGIFGMEMB32:
2834 thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups);
2835 break;
2836 }
2837 data = (caddr_t)&thunk.ifgr;
2838 cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq);
2839 break;
2840 case SIOCGIFMEDIA32:
2841 case SIOCGIFXMEDIA32:
2842 ifmr32 = (struct ifmediareq32 *)data;
2843 memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
2844 sizeof(thunk.ifmr.ifm_name));
2845 thunk.ifmr.ifm_current = ifmr32->ifm_current;
2846 thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
2847 thunk.ifmr.ifm_status = ifmr32->ifm_status;
2848 thunk.ifmr.ifm_active = ifmr32->ifm_active;
2849 thunk.ifmr.ifm_count = ifmr32->ifm_count;
2850 thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
2851 data = (caddr_t)&thunk.ifmr;
2852 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2853 break;
2854 }
2855 #endif
2856
2857 switch (cmd) {
2858 case SIOCGIFCONF:
2859 error = ifconf(cmd, data);
2860 goto out_noref;
2861 }
2862
2863 ifr = (struct ifreq *)data;
2864 switch (cmd) {
2865 #ifdef VIMAGE
2866 case SIOCSIFRVNET:
2867 error = priv_check(td, PRIV_NET_SETIFVNET);
2868 if (error == 0)
2869 error = if_vmove_reclaim(td, ifr->ifr_name,
2870 ifr->ifr_jid);
2871 goto out_noref;
2872 #endif
2873 case SIOCIFCREATE:
2874 case SIOCIFCREATE2:
2875 error = priv_check(td, PRIV_NET_IFCREATE);
2876 if (error == 0)
2877 error = if_clone_create(ifr->ifr_name,
2878 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
2879 ifr_data_get_ptr(ifr) : NULL);
2880 goto out_noref;
2881 case SIOCIFDESTROY:
2882 error = priv_check(td, PRIV_NET_IFDESTROY);
2883
2884 if (error == 0) {
2885 sx_xlock(&ifnet_detach_sxlock);
2886 error = if_clone_destroy(ifr->ifr_name);
2887 sx_xunlock(&ifnet_detach_sxlock);
2888 }
2889 goto out_noref;
2890
2891 case SIOCIFGCLONERS:
2892 error = if_clone_list((struct if_clonereq *)data);
2893 goto out_noref;
2894
2895 case SIOCGIFGMEMB:
2896 {
2897 struct ifgroupreq *req;
2898
2899 req = (struct ifgroupreq *)data;
2900 if (strnlen(req->ifgr_name, IFNAMSIZ) == IFNAMSIZ) {
2901 error = EINVAL;
2902 goto out_noref;
2903 }
2904 error = if_getgroupmembers(req);
2905 goto out_noref;
2906 }
2907 }
2908
2909 ifp = ifunit_ref(ifr->ifr_name);
2910 if (ifp == NULL) {
2911 error = ENXIO;
2912 goto out_noref;
2913 }
2914
2915 error = ifhwioctl(cmd, ifp, data, td);
2916 if (error != ENOIOCTL)
2917 goto out_ref;
2918
2919 oif_flags = ifp->if_flags;
2920 if (so->so_proto == NULL) {
2921 error = EOPNOTSUPP;
2922 goto out_ref;
2923 }
2924
2925 /*
2926 * Pass the request on to the socket control method, and if the
2927 * latter returns EOPNOTSUPP, directly to the interface.
2928 *
2929 * Make an exception for the legacy SIOCSIF* requests. Drivers
2930 * trust SIOCSIFADDR et al to come from an already privileged
2931 * layer, and do not perform any credentials checks or input
2932 * validation.
2933 */
2934 error = so->so_proto->pr_control(so, cmd, data, ifp, td);
2935 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2936 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2937 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2938 error = (*ifp->if_ioctl)(ifp, cmd, data);
2939
2940 if (!(oif_flags & IFF_UP) && (ifp->if_flags & IFF_UP))
2941 if_up(ifp);
2942 out_ref:
2943 if_rele(ifp);
2944 out_noref:
2945 CURVNET_RESTORE();
2946 #ifdef COMPAT_FREEBSD32
2947 if (error != 0)
2948 return (error);
2949 switch (saved_cmd) {
2950 case SIOCGIFCONF32:
2951 ifc32->ifc_len = thunk.ifc.ifc_len;
2952 break;
2953 case SIOCGDRVSPEC32:
2954 /*
2955 * SIOCGDRVSPEC is IOWR, but nothing actually touches
2956 * the struct so just assert that ifd_len (the only
2957 * field it might make sense to update) hasn't
2958 * changed.
2959 */
2960 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
2961 ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
2962 thunk.ifd.ifd_len));
2963 break;
2964 case SIOCGIFGROUP32:
2965 case SIOCGIFGMEMB32:
2966 ifgr32->ifgr_len = thunk.ifgr.ifgr_len;
2967 break;
2968 case SIOCGIFMEDIA32:
2969 case SIOCGIFXMEDIA32:
2970 ifmr32->ifm_current = thunk.ifmr.ifm_current;
2971 ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
2972 ifmr32->ifm_status = thunk.ifmr.ifm_status;
2973 ifmr32->ifm_active = thunk.ifmr.ifm_active;
2974 ifmr32->ifm_count = thunk.ifmr.ifm_count;
2975 break;
2976 }
2977 #endif
2978 return (error);
2979 }
2980
2981 int
if_rename(struct ifnet * ifp,char * new_name)2982 if_rename(struct ifnet *ifp, char *new_name)
2983 {
2984 struct ifaddr *ifa;
2985 struct sockaddr_dl *sdl;
2986 size_t namelen, onamelen;
2987 char old_name[IFNAMSIZ];
2988 char strbuf[IFNAMSIZ + 8];
2989
2990 if (new_name[0] == '\0')
2991 return (EINVAL);
2992 if (strcmp(new_name, ifp->if_xname) == 0)
2993 return (0);
2994 if (ifunit(new_name) != NULL)
2995 return (EEXIST);
2996
2997 if_printf(ifp, "changing name to '%s'\n", new_name);
2998
2999 IF_ADDR_WLOCK(ifp);
3000 strlcpy(old_name, ifp->if_xname, sizeof(old_name));
3001 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
3002 ifa = ifp->if_addr;
3003 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3004 namelen = strlen(new_name);
3005 onamelen = sdl->sdl_nlen;
3006 /*
3007 * Move the address if needed. This is safe because we
3008 * allocate space for a name of length IFNAMSIZ when we
3009 * create this in if_attach().
3010 */
3011 if (namelen != onamelen) {
3012 bcopy(sdl->sdl_data + onamelen,
3013 sdl->sdl_data + namelen, sdl->sdl_alen);
3014 }
3015 bcopy(new_name, sdl->sdl_data, namelen);
3016 sdl->sdl_nlen = namelen;
3017 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
3018 bzero(sdl->sdl_data, onamelen);
3019 while (namelen != 0)
3020 sdl->sdl_data[--namelen] = 0xff;
3021 IF_ADDR_WUNLOCK(ifp);
3022
3023 EVENTHANDLER_INVOKE(ifnet_rename_event, ifp, old_name);
3024
3025 snprintf(strbuf, sizeof(strbuf), "name=%s", new_name);
3026 devctl_notify("IFNET", old_name, "RENAME", strbuf);
3027
3028 return (0);
3029 }
3030
3031 /*
3032 * The code common to handling reference counted flags,
3033 * e.g., in ifpromisc() and if_allmulti().
3034 * The "pflag" argument can specify a permanent mode flag to check,
3035 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3036 *
3037 * Only to be used on stack-owned flags, not driver-owned flags.
3038 */
3039 static int
if_setflag(struct ifnet * ifp,int flag,int pflag,int * refcount,int onswitch)3040 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3041 {
3042 struct ifreq ifr;
3043 int error;
3044 int oldflags, oldcount;
3045
3046 /* Sanity checks to catch programming errors */
3047 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3048 ("%s: setting driver-owned flag %d", __func__, flag));
3049
3050 if (onswitch)
3051 KASSERT(*refcount >= 0,
3052 ("%s: increment negative refcount %d for flag %d",
3053 __func__, *refcount, flag));
3054 else
3055 KASSERT(*refcount > 0,
3056 ("%s: decrement non-positive refcount %d for flag %d",
3057 __func__, *refcount, flag));
3058
3059 /* In case this mode is permanent, just touch refcount */
3060 if (ifp->if_flags & pflag) {
3061 *refcount += onswitch ? 1 : -1;
3062 return (0);
3063 }
3064
3065 /* Save ifnet parameters for if_ioctl() may fail */
3066 oldcount = *refcount;
3067 oldflags = ifp->if_flags;
3068
3069 /*
3070 * See if we aren't the only and touching refcount is enough.
3071 * Actually toggle interface flag if we are the first or last.
3072 */
3073 if (onswitch) {
3074 if ((*refcount)++)
3075 return (0);
3076 ifp->if_flags |= flag;
3077 } else {
3078 if (--(*refcount))
3079 return (0);
3080 ifp->if_flags &= ~flag;
3081 }
3082
3083 /* Call down the driver since we've changed interface flags */
3084 if (ifp->if_ioctl == NULL) {
3085 error = EOPNOTSUPP;
3086 goto recover;
3087 }
3088 ifr.ifr_flags = ifp->if_flags & 0xffff;
3089 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3090 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3091 if (error)
3092 goto recover;
3093 /* Notify userland that interface flags have changed */
3094 rt_ifmsg(ifp, flag);
3095 return (0);
3096
3097 recover:
3098 /* Recover after driver error */
3099 *refcount = oldcount;
3100 ifp->if_flags = oldflags;
3101 return (error);
3102 }
3103
3104 /*
3105 * Set/clear promiscuous mode on interface ifp based on the truth value
3106 * of pswitch. The calls are reference counted so that only the first
3107 * "on" request actually has an effect, as does the final "off" request.
3108 * Results are undefined if the "off" and "on" requests are not matched.
3109 */
3110 int
ifpromisc(struct ifnet * ifp,int pswitch)3111 ifpromisc(struct ifnet *ifp, int pswitch)
3112 {
3113 int error;
3114 int oldflags = ifp->if_flags;
3115
3116 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3117 &ifp->if_pcount, pswitch);
3118 /* If promiscuous mode status has changed, log a message */
3119 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3120 log_promisc_mode_change)
3121 if_printf(ifp, "promiscuous mode %s\n",
3122 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3123 return (error);
3124 }
3125
3126 /*
3127 * Return interface configuration
3128 * of system. List may be used
3129 * in later ioctl's (above) to get
3130 * other information.
3131 */
3132 /*ARGSUSED*/
3133 static int
ifconf(u_long cmd,caddr_t data)3134 ifconf(u_long cmd, caddr_t data)
3135 {
3136 struct ifconf *ifc = (struct ifconf *)data;
3137 struct ifnet *ifp;
3138 struct ifaddr *ifa;
3139 struct ifreq ifr;
3140 struct sbuf *sb;
3141 int error, full = 0, valid_len, max_len;
3142
3143 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3144 max_len = maxphys - 1;
3145
3146 /* Prevent hostile input from being able to crash the system */
3147 if (ifc->ifc_len <= 0)
3148 return (EINVAL);
3149
3150 again:
3151 if (ifc->ifc_len <= max_len) {
3152 max_len = ifc->ifc_len;
3153 full = 1;
3154 }
3155 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3156 max_len = 0;
3157 valid_len = 0;
3158
3159 IFNET_RLOCK();
3160 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3161 struct epoch_tracker et;
3162 int addrs;
3163
3164 /*
3165 * Zero the ifr to make sure we don't disclose the contents
3166 * of the stack.
3167 */
3168 memset(&ifr, 0, sizeof(ifr));
3169
3170 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3171 >= sizeof(ifr.ifr_name)) {
3172 sbuf_delete(sb);
3173 IFNET_RUNLOCK();
3174 return (ENAMETOOLONG);
3175 }
3176
3177 addrs = 0;
3178 NET_EPOCH_ENTER(et);
3179 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3180 struct sockaddr *sa = ifa->ifa_addr;
3181
3182 if (prison_if(curthread->td_ucred, sa) != 0)
3183 continue;
3184 addrs++;
3185 if (sa->sa_len <= sizeof(*sa)) {
3186 if (sa->sa_len < sizeof(*sa)) {
3187 memset(&ifr.ifr_ifru.ifru_addr, 0,
3188 sizeof(ifr.ifr_ifru.ifru_addr));
3189 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3190 sa->sa_len);
3191 } else
3192 ifr.ifr_ifru.ifru_addr = *sa;
3193 sbuf_bcat(sb, &ifr, sizeof(ifr));
3194 max_len += sizeof(ifr);
3195 } else {
3196 sbuf_bcat(sb, &ifr,
3197 offsetof(struct ifreq, ifr_addr));
3198 max_len += offsetof(struct ifreq, ifr_addr);
3199 sbuf_bcat(sb, sa, sa->sa_len);
3200 max_len += sa->sa_len;
3201 }
3202
3203 if (sbuf_error(sb) == 0)
3204 valid_len = sbuf_len(sb);
3205 }
3206 NET_EPOCH_EXIT(et);
3207 if (addrs == 0) {
3208 sbuf_bcat(sb, &ifr, sizeof(ifr));
3209 max_len += sizeof(ifr);
3210
3211 if (sbuf_error(sb) == 0)
3212 valid_len = sbuf_len(sb);
3213 }
3214 }
3215 IFNET_RUNLOCK();
3216
3217 /*
3218 * If we didn't allocate enough space (uncommon), try again. If
3219 * we have already allocated as much space as we are allowed,
3220 * return what we've got.
3221 */
3222 if (valid_len != max_len && !full) {
3223 sbuf_delete(sb);
3224 goto again;
3225 }
3226
3227 ifc->ifc_len = valid_len;
3228 sbuf_finish(sb);
3229 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3230 sbuf_delete(sb);
3231 return (error);
3232 }
3233
3234 /*
3235 * Just like ifpromisc(), but for all-multicast-reception mode.
3236 */
3237 int
if_allmulti(struct ifnet * ifp,int onswitch)3238 if_allmulti(struct ifnet *ifp, int onswitch)
3239 {
3240
3241 return (if_setflag(ifp, IFF_ALLMULTI, IFF_PALLMULTI, &ifp->if_amcount,
3242 onswitch));
3243 }
3244
3245 struct ifmultiaddr *
if_findmulti(struct ifnet * ifp,const struct sockaddr * sa)3246 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3247 {
3248 struct ifmultiaddr *ifma;
3249
3250 IF_ADDR_LOCK_ASSERT(ifp);
3251
3252 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3253 if (sa->sa_family == AF_LINK) {
3254 if (sa_dl_equal(ifma->ifma_addr, sa))
3255 break;
3256 } else {
3257 if (sa_equal(ifma->ifma_addr, sa))
3258 break;
3259 }
3260 }
3261
3262 return ifma;
3263 }
3264
3265 /*
3266 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3267 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3268 * the ifnet multicast address list here, so the caller must do that and
3269 * other setup work (such as notifying the device driver). The reference
3270 * count is initialized to 1.
3271 */
3272 static struct ifmultiaddr *
if_allocmulti(struct ifnet * ifp,struct sockaddr * sa,struct sockaddr * llsa,int mflags)3273 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3274 int mflags)
3275 {
3276 struct ifmultiaddr *ifma;
3277 struct sockaddr *dupsa;
3278
3279 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3280 M_ZERO);
3281 if (ifma == NULL)
3282 return (NULL);
3283
3284 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3285 if (dupsa == NULL) {
3286 free(ifma, M_IFMADDR);
3287 return (NULL);
3288 }
3289 bcopy(sa, dupsa, sa->sa_len);
3290 ifma->ifma_addr = dupsa;
3291
3292 ifma->ifma_ifp = ifp;
3293 ifma->ifma_refcount = 1;
3294 ifma->ifma_protospec = NULL;
3295
3296 if (llsa == NULL) {
3297 ifma->ifma_lladdr = NULL;
3298 return (ifma);
3299 }
3300
3301 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3302 if (dupsa == NULL) {
3303 free(ifma->ifma_addr, M_IFMADDR);
3304 free(ifma, M_IFMADDR);
3305 return (NULL);
3306 }
3307 bcopy(llsa, dupsa, llsa->sa_len);
3308 ifma->ifma_lladdr = dupsa;
3309
3310 return (ifma);
3311 }
3312
3313 /*
3314 * if_freemulti: free ifmultiaddr structure and possibly attached related
3315 * addresses. The caller is responsible for implementing reference
3316 * counting, notifying the driver, handling routing messages, and releasing
3317 * any dependent link layer state.
3318 */
3319 #ifdef MCAST_VERBOSE
3320 extern void kdb_backtrace(void);
3321 #endif
3322 static void
if_freemulti_internal(struct ifmultiaddr * ifma)3323 if_freemulti_internal(struct ifmultiaddr *ifma)
3324 {
3325
3326 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3327 ifma->ifma_refcount));
3328
3329 if (ifma->ifma_lladdr != NULL)
3330 free(ifma->ifma_lladdr, M_IFMADDR);
3331 #ifdef MCAST_VERBOSE
3332 kdb_backtrace();
3333 printf("%s freeing ifma: %p\n", __func__, ifma);
3334 #endif
3335 free(ifma->ifma_addr, M_IFMADDR);
3336 free(ifma, M_IFMADDR);
3337 }
3338
3339 static void
if_destroymulti(epoch_context_t ctx)3340 if_destroymulti(epoch_context_t ctx)
3341 {
3342 struct ifmultiaddr *ifma;
3343
3344 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3345 if_freemulti_internal(ifma);
3346 }
3347
3348 void
if_freemulti(struct ifmultiaddr * ifma)3349 if_freemulti(struct ifmultiaddr *ifma)
3350 {
3351 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3352 ifma->ifma_refcount));
3353
3354 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3355 }
3356
3357 /*
3358 * Register an additional multicast address with a network interface.
3359 *
3360 * - If the address is already present, bump the reference count on the
3361 * address and return.
3362 * - If the address is not link-layer, look up a link layer address.
3363 * - Allocate address structures for one or both addresses, and attach to the
3364 * multicast address list on the interface. If automatically adding a link
3365 * layer address, the protocol address will own a reference to the link
3366 * layer address, to be freed when it is freed.
3367 * - Notify the network device driver of an addition to the multicast address
3368 * list.
3369 *
3370 * 'sa' points to caller-owned memory with the desired multicast address.
3371 *
3372 * 'retifma' will be used to return a pointer to the resulting multicast
3373 * address reference, if desired.
3374 */
3375 int
if_addmulti(struct ifnet * ifp,struct sockaddr * sa,struct ifmultiaddr ** retifma)3376 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3377 struct ifmultiaddr **retifma)
3378 {
3379 struct ifmultiaddr *ifma, *ll_ifma;
3380 struct sockaddr *llsa;
3381 struct sockaddr_dl sdl;
3382 int error;
3383
3384 #ifdef INET
3385 IN_MULTI_LIST_UNLOCK_ASSERT();
3386 #endif
3387 #ifdef INET6
3388 IN6_MULTI_LIST_UNLOCK_ASSERT();
3389 #endif
3390 /*
3391 * If the address is already present, return a new reference to it;
3392 * otherwise, allocate storage and set up a new address.
3393 */
3394 IF_ADDR_WLOCK(ifp);
3395 ifma = if_findmulti(ifp, sa);
3396 if (ifma != NULL) {
3397 ifma->ifma_refcount++;
3398 if (retifma != NULL)
3399 *retifma = ifma;
3400 IF_ADDR_WUNLOCK(ifp);
3401 return (0);
3402 }
3403
3404 /*
3405 * The address isn't already present; resolve the protocol address
3406 * into a link layer address, and then look that up, bump its
3407 * refcount or allocate an ifma for that also.
3408 * Most link layer resolving functions returns address data which
3409 * fits inside default sockaddr_dl structure. However callback
3410 * can allocate another sockaddr structure, in that case we need to
3411 * free it later.
3412 */
3413 llsa = NULL;
3414 ll_ifma = NULL;
3415 if (ifp->if_resolvemulti != NULL) {
3416 /* Provide called function with buffer size information */
3417 sdl.sdl_len = sizeof(sdl);
3418 llsa = (struct sockaddr *)&sdl;
3419 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3420 if (error)
3421 goto unlock_out;
3422 }
3423
3424 /*
3425 * Allocate the new address. Don't hook it up yet, as we may also
3426 * need to allocate a link layer multicast address.
3427 */
3428 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3429 if (ifma == NULL) {
3430 error = ENOMEM;
3431 goto free_llsa_out;
3432 }
3433
3434 /*
3435 * If a link layer address is found, we'll need to see if it's
3436 * already present in the address list, or allocate is as well.
3437 * When this block finishes, the link layer address will be on the
3438 * list.
3439 */
3440 if (llsa != NULL) {
3441 ll_ifma = if_findmulti(ifp, llsa);
3442 if (ll_ifma == NULL) {
3443 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3444 if (ll_ifma == NULL) {
3445 --ifma->ifma_refcount;
3446 if_freemulti(ifma);
3447 error = ENOMEM;
3448 goto free_llsa_out;
3449 }
3450 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3451 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3452 ifma_link);
3453 } else
3454 ll_ifma->ifma_refcount++;
3455 ifma->ifma_llifma = ll_ifma;
3456 }
3457
3458 /*
3459 * We now have a new multicast address, ifma, and possibly a new or
3460 * referenced link layer address. Add the primary address to the
3461 * ifnet address list.
3462 */
3463 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3464 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3465
3466 if (retifma != NULL)
3467 *retifma = ifma;
3468
3469 /*
3470 * Must generate the message while holding the lock so that 'ifma'
3471 * pointer is still valid.
3472 */
3473 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3474 IF_ADDR_WUNLOCK(ifp);
3475
3476 /*
3477 * We are certain we have added something, so call down to the
3478 * interface to let them know about it.
3479 */
3480 if (ifp->if_ioctl != NULL) {
3481 if (THREAD_CAN_SLEEP())
3482 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3483 else
3484 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3485 }
3486
3487 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3488 link_free_sdl(llsa);
3489
3490 return (0);
3491
3492 free_llsa_out:
3493 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3494 link_free_sdl(llsa);
3495
3496 unlock_out:
3497 IF_ADDR_WUNLOCK(ifp);
3498 return (error);
3499 }
3500
3501 static void
if_siocaddmulti(void * arg,int pending)3502 if_siocaddmulti(void *arg, int pending)
3503 {
3504 struct ifnet *ifp;
3505
3506 ifp = arg;
3507 #ifdef DIAGNOSTIC
3508 if (pending > 1)
3509 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3510 #endif
3511 CURVNET_SET(ifp->if_vnet);
3512 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3513 CURVNET_RESTORE();
3514 }
3515
3516 /*
3517 * Delete a multicast group membership by network-layer group address.
3518 *
3519 * Returns ENOENT if the entry could not be found. If ifp no longer
3520 * exists, results are undefined. This entry point should only be used
3521 * from subsystems which do appropriate locking to hold ifp for the
3522 * duration of the call.
3523 * Network-layer protocol domains must use if_delmulti_ifma().
3524 */
3525 int
if_delmulti(struct ifnet * ifp,struct sockaddr * sa)3526 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3527 {
3528 struct ifmultiaddr *ifma;
3529 int lastref;
3530
3531 KASSERT(ifp, ("%s: NULL ifp", __func__));
3532
3533 IF_ADDR_WLOCK(ifp);
3534 lastref = 0;
3535 ifma = if_findmulti(ifp, sa);
3536 if (ifma != NULL)
3537 lastref = if_delmulti_locked(ifp, ifma, 0);
3538 IF_ADDR_WUNLOCK(ifp);
3539
3540 if (ifma == NULL)
3541 return (ENOENT);
3542
3543 if (lastref && ifp->if_ioctl != NULL) {
3544 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3545 }
3546
3547 return (0);
3548 }
3549
3550 /*
3551 * Delete all multicast group membership for an interface.
3552 * Should be used to quickly flush all multicast filters.
3553 */
3554 void
if_delallmulti(struct ifnet * ifp)3555 if_delallmulti(struct ifnet *ifp)
3556 {
3557 struct ifmultiaddr *ifma;
3558 struct ifmultiaddr *next;
3559
3560 IF_ADDR_WLOCK(ifp);
3561 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3562 if_delmulti_locked(ifp, ifma, 0);
3563 IF_ADDR_WUNLOCK(ifp);
3564 }
3565
3566 void
if_delmulti_ifma(struct ifmultiaddr * ifma)3567 if_delmulti_ifma(struct ifmultiaddr *ifma)
3568 {
3569 if_delmulti_ifma_flags(ifma, 0);
3570 }
3571
3572 /*
3573 * Delete a multicast group membership by group membership pointer.
3574 * Network-layer protocol domains must use this routine.
3575 *
3576 * It is safe to call this routine if the ifp disappeared.
3577 */
3578 void
if_delmulti_ifma_flags(struct ifmultiaddr * ifma,int flags)3579 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3580 {
3581 struct ifnet *ifp;
3582 int lastref;
3583 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3584 #ifdef INET
3585 IN_MULTI_LIST_UNLOCK_ASSERT();
3586 #endif
3587 ifp = ifma->ifma_ifp;
3588 #ifdef DIAGNOSTIC
3589 if (ifp == NULL) {
3590 printf("%s: ifma_ifp seems to be detached\n", __func__);
3591 } else {
3592 struct epoch_tracker et;
3593 struct ifnet *oifp;
3594
3595 NET_EPOCH_ENTER(et);
3596 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3597 if (ifp == oifp)
3598 break;
3599 NET_EPOCH_EXIT(et);
3600 if (ifp != oifp)
3601 ifp = NULL;
3602 }
3603 #endif
3604 /*
3605 * If and only if the ifnet instance exists: Acquire the address lock.
3606 */
3607 if (ifp != NULL)
3608 IF_ADDR_WLOCK(ifp);
3609
3610 lastref = if_delmulti_locked(ifp, ifma, flags);
3611
3612 if (ifp != NULL) {
3613 /*
3614 * If and only if the ifnet instance exists:
3615 * Release the address lock.
3616 * If the group was left: update the hardware hash filter.
3617 */
3618 IF_ADDR_WUNLOCK(ifp);
3619 if (lastref && ifp->if_ioctl != NULL) {
3620 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3621 }
3622 }
3623 }
3624
3625 /*
3626 * Perform deletion of network-layer and/or link-layer multicast address.
3627 *
3628 * Return 0 if the reference count was decremented.
3629 * Return 1 if the final reference was released, indicating that the
3630 * hardware hash filter should be reprogrammed.
3631 */
3632 static int
if_delmulti_locked(struct ifnet * ifp,struct ifmultiaddr * ifma,int detaching)3633 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3634 {
3635 struct ifmultiaddr *ll_ifma;
3636
3637 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3638 KASSERT(ifma->ifma_ifp == ifp,
3639 ("%s: inconsistent ifp %p", __func__, ifp));
3640 IF_ADDR_WLOCK_ASSERT(ifp);
3641 }
3642
3643 ifp = ifma->ifma_ifp;
3644 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3645
3646 /*
3647 * If the ifnet is detaching, null out references to ifnet,
3648 * so that upper protocol layers will notice, and not attempt
3649 * to obtain locks for an ifnet which no longer exists. The
3650 * routing socket announcement must happen before the ifnet
3651 * instance is detached from the system.
3652 */
3653 if (detaching) {
3654 #ifdef DIAGNOSTIC
3655 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3656 #endif
3657 /*
3658 * ifp may already be nulled out if we are being reentered
3659 * to delete the ll_ifma.
3660 */
3661 if (ifp != NULL) {
3662 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3663 ifma->ifma_ifp = NULL;
3664 }
3665 }
3666
3667 if (--ifma->ifma_refcount > 0)
3668 return 0;
3669
3670 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3671 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3672 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3673 }
3674 /*
3675 * If this ifma is a network-layer ifma, a link-layer ifma may
3676 * have been associated with it. Release it first if so.
3677 */
3678 ll_ifma = ifma->ifma_llifma;
3679 if (ll_ifma != NULL) {
3680 KASSERT(ifma->ifma_lladdr != NULL,
3681 ("%s: llifma w/o lladdr", __func__));
3682 if (detaching)
3683 ll_ifma->ifma_ifp = NULL; /* XXX */
3684 if (--ll_ifma->ifma_refcount == 0) {
3685 if (ifp != NULL) {
3686 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3687 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3688 ifma_link);
3689 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3690 }
3691 }
3692 if_freemulti(ll_ifma);
3693 }
3694 }
3695 #ifdef INVARIANTS
3696 if (ifp) {
3697 struct ifmultiaddr *ifmatmp;
3698
3699 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3700 MPASS(ifma != ifmatmp);
3701 }
3702 #endif
3703 if_freemulti(ifma);
3704 /*
3705 * The last reference to this instance of struct ifmultiaddr
3706 * was released; the hardware should be notified of this change.
3707 */
3708 return 1;
3709 }
3710
3711 /*
3712 * Set the link layer address on an interface.
3713 *
3714 * At this time we only support certain types of interfaces,
3715 * and we don't allow the length of the address to change.
3716 *
3717 * Set noinline to be dtrace-friendly
3718 */
3719 __noinline int
if_setlladdr(struct ifnet * ifp,const u_char * lladdr,int len)3720 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3721 {
3722 struct sockaddr_dl *sdl;
3723 struct ifaddr *ifa;
3724 struct ifreq ifr;
3725
3726 ifa = ifp->if_addr;
3727 if (ifa == NULL)
3728 return (EINVAL);
3729
3730 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3731 if (sdl == NULL)
3732 return (EINVAL);
3733
3734 if (len != sdl->sdl_alen) /* don't allow length to change */
3735 return (EINVAL);
3736
3737 switch (ifp->if_type) {
3738 case IFT_ETHER:
3739 case IFT_XETHER:
3740 case IFT_L2VLAN:
3741 case IFT_BRIDGE:
3742 case IFT_IEEE8023ADLAG:
3743 bcopy(lladdr, LLADDR(sdl), len);
3744 break;
3745 default:
3746 return (ENODEV);
3747 }
3748
3749 /*
3750 * If the interface is already up, we need
3751 * to re-init it in order to reprogram its
3752 * address filter.
3753 */
3754 if ((ifp->if_flags & IFF_UP) != 0) {
3755 if (ifp->if_ioctl) {
3756 ifp->if_flags &= ~IFF_UP;
3757 ifr.ifr_flags = ifp->if_flags & 0xffff;
3758 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3759 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3760 ifp->if_flags |= IFF_UP;
3761 ifr.ifr_flags = ifp->if_flags & 0xffff;
3762 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3763 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3764 }
3765 }
3766 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3767
3768 return (0);
3769 }
3770
3771 /*
3772 * Compat function for handling basic encapsulation requests.
3773 * Not converted stacks (FDDI, IB, ..) supports traditional
3774 * output model: ARP (and other similar L2 protocols) are handled
3775 * inside output routine, arpresolve/nd6_resolve() returns MAC
3776 * address instead of full prepend.
3777 *
3778 * This function creates calculated header==MAC for IPv4/IPv6 and
3779 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3780 * address families.
3781 */
3782 static int
if_requestencap_default(struct ifnet * ifp,struct if_encap_req * req)3783 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3784 {
3785 if (req->rtype != IFENCAP_LL)
3786 return (EOPNOTSUPP);
3787
3788 if (req->bufsize < req->lladdr_len)
3789 return (ENOMEM);
3790
3791 switch (req->family) {
3792 case AF_INET:
3793 case AF_INET6:
3794 break;
3795 default:
3796 return (EAFNOSUPPORT);
3797 }
3798
3799 /* Copy lladdr to storage as is */
3800 memmove(req->buf, req->lladdr, req->lladdr_len);
3801 req->bufsize = req->lladdr_len;
3802 req->lladdr_off = 0;
3803
3804 return (0);
3805 }
3806
3807 /*
3808 * Tunnel interfaces can nest, also they may cause infinite recursion
3809 * calls when misconfigured. We'll prevent this by detecting loops.
3810 * High nesting level may cause stack exhaustion. We'll prevent this
3811 * by introducing upper limit.
3812 *
3813 * Return 0, if tunnel nesting count is equal or less than limit.
3814 */
3815 int
if_tunnel_check_nesting(struct ifnet * ifp,struct mbuf * m,uint32_t cookie,int limit)3816 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3817 int limit)
3818 {
3819 struct m_tag *mtag;
3820 int count;
3821
3822 count = 1;
3823 mtag = NULL;
3824 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3825 if (*(struct ifnet **)(mtag + 1) == ifp) {
3826 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3827 return (EIO);
3828 }
3829 count++;
3830 }
3831 if (count > limit) {
3832 log(LOG_NOTICE,
3833 "%s: if_output recursively called too many times(%d)\n",
3834 if_name(ifp), count);
3835 return (EIO);
3836 }
3837 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3838 if (mtag == NULL)
3839 return (ENOMEM);
3840 *(struct ifnet **)(mtag + 1) = ifp;
3841 m_tag_prepend(m, mtag);
3842 return (0);
3843 }
3844
3845 /*
3846 * Get the link layer address that was read from the hardware at attach.
3847 *
3848 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3849 * their component interfaces as IFT_IEEE8023ADLAG.
3850 */
3851 int
if_gethwaddr(struct ifnet * ifp,struct ifreq * ifr)3852 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3853 {
3854 if (ifp->if_hw_addr == NULL)
3855 return (ENODEV);
3856
3857 switch (ifp->if_type) {
3858 case IFT_ETHER:
3859 case IFT_IEEE8023ADLAG:
3860 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3861 return (0);
3862 default:
3863 return (ENODEV);
3864 }
3865 }
3866
3867 /*
3868 * The name argument must be a pointer to storage which will last as
3869 * long as the interface does. For physical devices, the result of
3870 * device_get_name(dev) is a good choice and for pseudo-devices a
3871 * static string works well.
3872 */
3873 void
if_initname(struct ifnet * ifp,const char * name,int unit)3874 if_initname(struct ifnet *ifp, const char *name, int unit)
3875 {
3876 ifp->if_dname = name;
3877 ifp->if_dunit = unit;
3878 if (unit != IF_DUNIT_NONE)
3879 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3880 else
3881 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3882 }
3883
3884 static int
if_vlog(struct ifnet * ifp,int pri,const char * fmt,va_list ap)3885 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
3886 {
3887 char if_fmt[256];
3888
3889 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3890 vlog(pri, if_fmt, ap);
3891 return (0);
3892 }
3893
3894
3895 int
if_printf(struct ifnet * ifp,const char * fmt,...)3896 if_printf(struct ifnet *ifp, const char *fmt, ...)
3897 {
3898 va_list ap;
3899
3900 va_start(ap, fmt);
3901 if_vlog(ifp, LOG_INFO, fmt, ap);
3902 va_end(ap);
3903 return (0);
3904 }
3905
3906 int
if_log(struct ifnet * ifp,int pri,const char * fmt,...)3907 if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
3908 {
3909 va_list ap;
3910
3911 va_start(ap, fmt);
3912 if_vlog(ifp, pri, fmt, ap);
3913 va_end(ap);
3914 return (0);
3915 }
3916
3917 void
if_start(struct ifnet * ifp)3918 if_start(struct ifnet *ifp)
3919 {
3920
3921 (*(ifp)->if_start)(ifp);
3922 }
3923
3924 /*
3925 * Backwards compatibility interface for drivers
3926 * that have not implemented it
3927 */
3928 static int
if_transmit_default(struct ifnet * ifp,struct mbuf * m)3929 if_transmit_default(struct ifnet *ifp, struct mbuf *m)
3930 {
3931 int error;
3932
3933 IFQ_HANDOFF(ifp, m, error);
3934 return (error);
3935 }
3936
3937 static void
if_input_default(struct ifnet * ifp __unused,struct mbuf * m)3938 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3939 {
3940 m_freem(m);
3941 }
3942
3943 int
if_handoff(struct ifqueue * ifq,struct mbuf * m,struct ifnet * ifp,int adjust)3944 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3945 {
3946 int active = 0;
3947
3948 IF_LOCK(ifq);
3949 if (_IF_QFULL(ifq)) {
3950 IF_UNLOCK(ifq);
3951 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3952 m_freem(m);
3953 return (0);
3954 }
3955 if (ifp != NULL) {
3956 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3957 if (m->m_flags & (M_BCAST|M_MCAST))
3958 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3959 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3960 }
3961 _IF_ENQUEUE(ifq, m);
3962 IF_UNLOCK(ifq);
3963 if (ifp != NULL && !active)
3964 (*(ifp)->if_start)(ifp);
3965 return (1);
3966 }
3967
3968 void
if_register_com_alloc(u_char type,if_com_alloc_t * a,if_com_free_t * f)3969 if_register_com_alloc(u_char type,
3970 if_com_alloc_t *a, if_com_free_t *f)
3971 {
3972
3973 KASSERT(if_com_alloc[type] == NULL,
3974 ("if_register_com_alloc: %d already registered", type));
3975 KASSERT(if_com_free[type] == NULL,
3976 ("if_register_com_alloc: %d free already registered", type));
3977
3978 if_com_alloc[type] = a;
3979 if_com_free[type] = f;
3980 }
3981
3982 void
if_deregister_com_alloc(u_char type)3983 if_deregister_com_alloc(u_char type)
3984 {
3985
3986 KASSERT(if_com_alloc[type] != NULL,
3987 ("if_deregister_com_alloc: %d not registered", type));
3988 KASSERT(if_com_free[type] != NULL,
3989 ("if_deregister_com_alloc: %d free not registered", type));
3990
3991 /*
3992 * Ensure all pending EPOCH(9) callbacks have been executed. This
3993 * fixes issues about late invocation of if_destroy(), which leads
3994 * to memory leak from if_com_alloc[type] allocated if_l2com.
3995 */
3996 NET_EPOCH_DRAIN_CALLBACKS();
3997
3998 if_com_alloc[type] = NULL;
3999 if_com_free[type] = NULL;
4000 }
4001
4002 /* API for driver access to network stack owned ifnet.*/
4003 uint64_t
if_setbaudrate(struct ifnet * ifp,uint64_t baudrate)4004 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4005 {
4006 uint64_t oldbrate;
4007
4008 oldbrate = ifp->if_baudrate;
4009 ifp->if_baudrate = baudrate;
4010 return (oldbrate);
4011 }
4012
4013 uint64_t
if_getbaudrate(const if_t ifp)4014 if_getbaudrate(const if_t ifp)
4015 {
4016 return (ifp->if_baudrate);
4017 }
4018
4019 int
if_setcapabilities(if_t ifp,int capabilities)4020 if_setcapabilities(if_t ifp, int capabilities)
4021 {
4022 ifp->if_capabilities = capabilities;
4023 return (0);
4024 }
4025
4026 int
if_setcapabilitiesbit(if_t ifp,int setbit,int clearbit)4027 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4028 {
4029 ifp->if_capabilities &= ~clearbit;
4030 ifp->if_capabilities |= setbit;
4031 return (0);
4032 }
4033
4034 int
if_getcapabilities(const if_t ifp)4035 if_getcapabilities(const if_t ifp)
4036 {
4037 return (ifp->if_capabilities);
4038 }
4039
4040 int
if_setcapenable(if_t ifp,int capabilities)4041 if_setcapenable(if_t ifp, int capabilities)
4042 {
4043 ifp->if_capenable = capabilities;
4044 return (0);
4045 }
4046
4047 int
if_setcapenablebit(if_t ifp,int setcap,int clearcap)4048 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4049 {
4050 ifp->if_capenable &= ~clearcap;
4051 ifp->if_capenable |= setcap;
4052 return (0);
4053 }
4054
4055 int
if_setcapabilities2(if_t ifp,int capabilities)4056 if_setcapabilities2(if_t ifp, int capabilities)
4057 {
4058 ifp->if_capabilities2 = capabilities;
4059 return (0);
4060 }
4061
4062 int
if_setcapabilities2bit(if_t ifp,int setbit,int clearbit)4063 if_setcapabilities2bit(if_t ifp, int setbit, int clearbit)
4064 {
4065 ifp->if_capabilities2 &= ~clearbit;
4066 ifp->if_capabilities2 |= setbit;
4067 return (0);
4068 }
4069
4070 int
if_getcapabilities2(const if_t ifp)4071 if_getcapabilities2(const if_t ifp)
4072 {
4073 return (ifp->if_capabilities2);
4074 }
4075
4076 int
if_setcapenable2(if_t ifp,int capabilities2)4077 if_setcapenable2(if_t ifp, int capabilities2)
4078 {
4079 ifp->if_capenable2 = capabilities2;
4080 return (0);
4081 }
4082
4083 int
if_setcapenable2bit(if_t ifp,int setcap,int clearcap)4084 if_setcapenable2bit(if_t ifp, int setcap, int clearcap)
4085 {
4086 ifp->if_capenable2 &= ~clearcap;
4087 ifp->if_capenable2 |= setcap;
4088 return (0);
4089 }
4090
4091 const char *
if_getdname(const if_t ifp)4092 if_getdname(const if_t ifp)
4093 {
4094 return (ifp->if_dname);
4095 }
4096
4097 void
if_setdname(if_t ifp,const char * dname)4098 if_setdname(if_t ifp, const char *dname)
4099 {
4100 ifp->if_dname = dname;
4101 }
4102
4103 const char *
if_name(if_t ifp)4104 if_name(if_t ifp)
4105 {
4106 return (ifp->if_xname);
4107 }
4108
4109 int
if_setname(if_t ifp,const char * name)4110 if_setname(if_t ifp, const char *name)
4111 {
4112 if (strlen(name) > sizeof(ifp->if_xname) - 1)
4113 return (ENAMETOOLONG);
4114 strcpy(ifp->if_xname, name);
4115
4116 return (0);
4117 }
4118
4119 int
if_togglecapenable(if_t ifp,int togglecap)4120 if_togglecapenable(if_t ifp, int togglecap)
4121 {
4122 ifp->if_capenable ^= togglecap;
4123 return (0);
4124 }
4125
4126 int
if_getcapenable(const if_t ifp)4127 if_getcapenable(const if_t ifp)
4128 {
4129 return (ifp->if_capenable);
4130 }
4131
4132 int
if_togglecapenable2(if_t ifp,int togglecap)4133 if_togglecapenable2(if_t ifp, int togglecap)
4134 {
4135 ifp->if_capenable2 ^= togglecap;
4136 return (0);
4137 }
4138
4139 int
if_getcapenable2(const if_t ifp)4140 if_getcapenable2(const if_t ifp)
4141 {
4142 return (ifp->if_capenable2);
4143 }
4144
4145 int
if_getdunit(const if_t ifp)4146 if_getdunit(const if_t ifp)
4147 {
4148 return (ifp->if_dunit);
4149 }
4150
4151 int
if_getindex(const if_t ifp)4152 if_getindex(const if_t ifp)
4153 {
4154 return (ifp->if_index);
4155 }
4156
4157 int
if_getidxgen(const if_t ifp)4158 if_getidxgen(const if_t ifp)
4159 {
4160 return (ifp->if_idxgen);
4161 }
4162
4163 const char *
if_getdescr(if_t ifp)4164 if_getdescr(if_t ifp)
4165 {
4166 return (ifp->if_description);
4167 }
4168
4169 void
if_setdescr(if_t ifp,char * descrbuf)4170 if_setdescr(if_t ifp, char *descrbuf)
4171 {
4172 sx_xlock(&ifdescr_sx);
4173 char *odescrbuf = ifp->if_description;
4174 ifp->if_description = descrbuf;
4175 sx_xunlock(&ifdescr_sx);
4176
4177 if_freedescr(odescrbuf);
4178 }
4179
4180 char *
if_allocdescr(size_t sz,int malloc_flag)4181 if_allocdescr(size_t sz, int malloc_flag)
4182 {
4183 malloc_flag &= (M_WAITOK | M_NOWAIT);
4184 return (malloc(sz, M_IFDESCR, M_ZERO | malloc_flag));
4185 }
4186
4187 void
if_freedescr(char * descrbuf)4188 if_freedescr(char *descrbuf)
4189 {
4190 free(descrbuf, M_IFDESCR);
4191 }
4192
4193 int
if_getalloctype(const if_t ifp)4194 if_getalloctype(const if_t ifp)
4195 {
4196 return (ifp->if_alloctype);
4197 }
4198
4199 void
if_setlastchange(if_t ifp)4200 if_setlastchange(if_t ifp)
4201 {
4202 getmicrotime(&ifp->if_lastchange);
4203 }
4204
4205 /*
4206 * This is largely undesirable because it ties ifnet to a device, but does
4207 * provide flexiblity for an embedded product vendor. Should be used with
4208 * the understanding that it violates the interface boundaries, and should be
4209 * a last resort only.
4210 */
4211 int
if_setdev(if_t ifp,void * dev)4212 if_setdev(if_t ifp, void *dev)
4213 {
4214 return (0);
4215 }
4216
4217 int
if_setdrvflagbits(if_t ifp,int set_flags,int clear_flags)4218 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4219 {
4220 ifp->if_drv_flags &= ~clear_flags;
4221 ifp->if_drv_flags |= set_flags;
4222
4223 return (0);
4224 }
4225
4226 int
if_getdrvflags(const if_t ifp)4227 if_getdrvflags(const if_t ifp)
4228 {
4229 return (ifp->if_drv_flags);
4230 }
4231
4232 int
if_setdrvflags(if_t ifp,int flags)4233 if_setdrvflags(if_t ifp, int flags)
4234 {
4235 ifp->if_drv_flags = flags;
4236 return (0);
4237 }
4238
4239 int
if_setflags(if_t ifp,int flags)4240 if_setflags(if_t ifp, int flags)
4241 {
4242 ifp->if_flags = flags;
4243 return (0);
4244 }
4245
4246 int
if_setflagbits(if_t ifp,int set,int clear)4247 if_setflagbits(if_t ifp, int set, int clear)
4248 {
4249 ifp->if_flags &= ~clear;
4250 ifp->if_flags |= set;
4251 return (0);
4252 }
4253
4254 int
if_getflags(const if_t ifp)4255 if_getflags(const if_t ifp)
4256 {
4257 return (ifp->if_flags);
4258 }
4259
4260 int
if_clearhwassist(if_t ifp)4261 if_clearhwassist(if_t ifp)
4262 {
4263 ifp->if_hwassist = 0;
4264 return (0);
4265 }
4266
4267 int
if_sethwassistbits(if_t ifp,int toset,int toclear)4268 if_sethwassistbits(if_t ifp, int toset, int toclear)
4269 {
4270 ifp->if_hwassist &= ~toclear;
4271 ifp->if_hwassist |= toset;
4272
4273 return (0);
4274 }
4275
4276 int
if_sethwassist(if_t ifp,int hwassist_bit)4277 if_sethwassist(if_t ifp, int hwassist_bit)
4278 {
4279 ifp->if_hwassist = hwassist_bit;
4280 return (0);
4281 }
4282
4283 int
if_gethwassist(const if_t ifp)4284 if_gethwassist(const if_t ifp)
4285 {
4286 return (ifp->if_hwassist);
4287 }
4288
4289 int
if_togglehwassist(if_t ifp,int toggle_bits)4290 if_togglehwassist(if_t ifp, int toggle_bits)
4291 {
4292 ifp->if_hwassist ^= toggle_bits;
4293 return (0);
4294 }
4295
4296 int
if_setmtu(if_t ifp,int mtu)4297 if_setmtu(if_t ifp, int mtu)
4298 {
4299 ifp->if_mtu = mtu;
4300 return (0);
4301 }
4302
4303 void
if_notifymtu(if_t ifp)4304 if_notifymtu(if_t ifp)
4305 {
4306 #ifdef INET6
4307 nd6_setmtu(ifp);
4308 #endif
4309 rt_updatemtu(ifp);
4310 }
4311
4312 int
if_getmtu(const if_t ifp)4313 if_getmtu(const if_t ifp)
4314 {
4315 return (ifp->if_mtu);
4316 }
4317
4318 void
if_setppromisc(if_t ifp,bool ppromisc)4319 if_setppromisc(if_t ifp, bool ppromisc)
4320 {
4321 int new_flags;
4322
4323 if (ppromisc)
4324 new_flags = ifp->if_flags | IFF_PPROMISC;
4325 else
4326 new_flags = ifp->if_flags & ~IFF_PPROMISC;
4327 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
4328 if (new_flags & IFF_PPROMISC)
4329 new_flags |= IFF_PROMISC;
4330 /*
4331 * Only unset IFF_PROMISC if there are no more consumers of
4332 * promiscuity, i.e. the ifp->if_pcount refcount is 0.
4333 */
4334 else if (ifp->if_pcount == 0)
4335 new_flags &= ~IFF_PROMISC;
4336 if (log_promisc_mode_change)
4337 if_printf(ifp, "permanently promiscuous mode %s\n",
4338 ((new_flags & IFF_PPROMISC) ?
4339 "enabled" : "disabled"));
4340 }
4341 ifp->if_flags = new_flags;
4342 }
4343
4344 /*
4345 * Methods for drivers to access interface unicast and multicast
4346 * link level addresses. Driver shall not know 'struct ifaddr' neither
4347 * 'struct ifmultiaddr'.
4348 */
4349 u_int
if_lladdr_count(if_t ifp)4350 if_lladdr_count(if_t ifp)
4351 {
4352 struct epoch_tracker et;
4353 struct ifaddr *ifa;
4354 u_int count;
4355
4356 count = 0;
4357 NET_EPOCH_ENTER(et);
4358 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4359 if (ifa->ifa_addr->sa_family == AF_LINK)
4360 count++;
4361 NET_EPOCH_EXIT(et);
4362
4363 return (count);
4364 }
4365
4366 int
if_foreach(if_foreach_cb_t cb,void * cb_arg)4367 if_foreach(if_foreach_cb_t cb, void *cb_arg)
4368 {
4369 if_t ifp;
4370 int error;
4371
4372 NET_EPOCH_ASSERT();
4373 MPASS(cb);
4374
4375 error = 0;
4376 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4377 error = cb(ifp, cb_arg);
4378 if (error != 0)
4379 break;
4380 }
4381
4382 return (error);
4383 }
4384
4385 /*
4386 * Iterates over the list of interfaces, permitting callback function @cb to sleep.
4387 * Stops iteration if @cb returns non-zero error code.
4388 * Returns the last error code from @cb.
4389 * @match_cb: optional match callback limiting the iteration to only matched interfaces
4390 * @match_arg: argument to pass to @match_cb
4391 * @cb: iteration callback
4392 * @cb_arg: argument to pass to @cb
4393 */
4394 int
if_foreach_sleep(if_foreach_match_t match_cb,void * match_arg,if_foreach_cb_t cb,void * cb_arg)4395 if_foreach_sleep(if_foreach_match_t match_cb, void *match_arg, if_foreach_cb_t cb,
4396 void *cb_arg)
4397 {
4398 int match_count = 0, array_size = 16; /* 128 bytes for malloc */
4399 struct ifnet **match_array = NULL;
4400 int error = 0;
4401
4402 MPASS(cb);
4403
4404 while (true) {
4405 struct ifnet **new_array;
4406 int new_size = array_size;
4407 struct epoch_tracker et;
4408 struct ifnet *ifp;
4409
4410 while (new_size < match_count)
4411 new_size *= 2;
4412 new_array = malloc(new_size * sizeof(void *), M_TEMP, M_WAITOK);
4413 if (match_array != NULL)
4414 memcpy(new_array, match_array, array_size * sizeof(void *));
4415 free(match_array, M_TEMP);
4416 match_array = new_array;
4417 array_size = new_size;
4418
4419 match_count = 0;
4420 NET_EPOCH_ENTER(et);
4421 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4422 if (match_cb != NULL && !match_cb(ifp, match_arg))
4423 continue;
4424 if (match_count < array_size) {
4425 if (if_try_ref(ifp))
4426 match_array[match_count++] = ifp;
4427 } else
4428 match_count++;
4429 }
4430 NET_EPOCH_EXIT(et);
4431
4432 if (match_count > array_size) {
4433 for (int i = 0; i < array_size; i++)
4434 if_rele(match_array[i]);
4435 continue;
4436 } else {
4437 for (int i = 0; i < match_count; i++) {
4438 if (error == 0)
4439 error = cb(match_array[i], cb_arg);
4440 if_rele(match_array[i]);
4441 }
4442 free(match_array, M_TEMP);
4443 break;
4444 }
4445 }
4446
4447 return (error);
4448 }
4449
4450
4451 /*
4452 * Uses just 1 pointer of the 4 available in the public struct.
4453 */
4454 if_t
if_iter_start(struct if_iter * iter)4455 if_iter_start(struct if_iter *iter)
4456 {
4457 if_t ifp;
4458
4459 NET_EPOCH_ASSERT();
4460
4461 bzero(iter, sizeof(*iter));
4462 ifp = CK_STAILQ_FIRST(&V_ifnet);
4463 if (ifp != NULL)
4464 iter->context[0] = CK_STAILQ_NEXT(ifp, if_link);
4465 else
4466 iter->context[0] = NULL;
4467 return (ifp);
4468 }
4469
4470 if_t
if_iter_next(struct if_iter * iter)4471 if_iter_next(struct if_iter *iter)
4472 {
4473 if_t cur_ifp = iter->context[0];
4474
4475 if (cur_ifp != NULL)
4476 iter->context[0] = CK_STAILQ_NEXT(cur_ifp, if_link);
4477 return (cur_ifp);
4478 }
4479
4480 void
if_iter_finish(struct if_iter * iter)4481 if_iter_finish(struct if_iter *iter)
4482 {
4483 /* Nothing to do here for now. */
4484 }
4485
4486 u_int
if_foreach_lladdr(if_t ifp,iflladdr_cb_t cb,void * cb_arg)4487 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4488 {
4489 struct epoch_tracker et;
4490 struct ifaddr *ifa;
4491 u_int count;
4492
4493 MPASS(cb);
4494
4495 count = 0;
4496 NET_EPOCH_ENTER(et);
4497 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4498 if (ifa->ifa_addr->sa_family != AF_LINK)
4499 continue;
4500 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4501 count);
4502 }
4503 NET_EPOCH_EXIT(et);
4504
4505 return (count);
4506 }
4507
4508 u_int
if_llmaddr_count(if_t ifp)4509 if_llmaddr_count(if_t ifp)
4510 {
4511 struct epoch_tracker et;
4512 struct ifmultiaddr *ifma;
4513 int count;
4514
4515 count = 0;
4516 NET_EPOCH_ENTER(et);
4517 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4518 if (ifma->ifma_addr->sa_family == AF_LINK)
4519 count++;
4520 NET_EPOCH_EXIT(et);
4521
4522 return (count);
4523 }
4524
4525 bool
if_maddr_empty(if_t ifp)4526 if_maddr_empty(if_t ifp)
4527 {
4528
4529 return (CK_STAILQ_EMPTY(&ifp->if_multiaddrs));
4530 }
4531
4532 u_int
if_foreach_llmaddr(if_t ifp,iflladdr_cb_t cb,void * cb_arg)4533 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4534 {
4535 struct epoch_tracker et;
4536 struct ifmultiaddr *ifma;
4537 u_int count;
4538
4539 MPASS(cb);
4540
4541 count = 0;
4542 NET_EPOCH_ENTER(et);
4543 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4544 if (ifma->ifma_addr->sa_family != AF_LINK)
4545 continue;
4546 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4547 count);
4548 }
4549 NET_EPOCH_EXIT(et);
4550
4551 return (count);
4552 }
4553
4554 u_int
if_foreach_addr_type(if_t ifp,int type,if_addr_cb_t cb,void * cb_arg)4555 if_foreach_addr_type(if_t ifp, int type, if_addr_cb_t cb, void *cb_arg)
4556 {
4557 struct epoch_tracker et;
4558 struct ifaddr *ifa;
4559 u_int count;
4560
4561 MPASS(cb);
4562
4563 count = 0;
4564 NET_EPOCH_ENTER(et);
4565 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4566 if (ifa->ifa_addr->sa_family != type)
4567 continue;
4568 count += (*cb)(cb_arg, ifa, count);
4569 }
4570 NET_EPOCH_EXIT(et);
4571
4572 return (count);
4573 }
4574
4575 struct ifaddr *
ifa_iter_start(if_t ifp,struct ifa_iter * iter)4576 ifa_iter_start(if_t ifp, struct ifa_iter *iter)
4577 {
4578 struct ifaddr *ifa;
4579
4580 NET_EPOCH_ASSERT();
4581
4582 bzero(iter, sizeof(*iter));
4583 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
4584 if (ifa != NULL)
4585 iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
4586 else
4587 iter->context[0] = NULL;
4588 return (ifa);
4589 }
4590
4591 struct ifaddr *
ifa_iter_next(struct ifa_iter * iter)4592 ifa_iter_next(struct ifa_iter *iter)
4593 {
4594 struct ifaddr *ifa = iter->context[0];
4595
4596 if (ifa != NULL)
4597 iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
4598 return (ifa);
4599 }
4600
4601 void
ifa_iter_finish(struct ifa_iter * iter)4602 ifa_iter_finish(struct ifa_iter *iter)
4603 {
4604 /* Nothing to do here for now. */
4605 }
4606
4607 int
if_setsoftc(if_t ifp,void * softc)4608 if_setsoftc(if_t ifp, void *softc)
4609 {
4610 ifp->if_softc = softc;
4611 return (0);
4612 }
4613
4614 void *
if_getsoftc(const if_t ifp)4615 if_getsoftc(const if_t ifp)
4616 {
4617 return (ifp->if_softc);
4618 }
4619
4620 void
if_setrcvif(struct mbuf * m,if_t ifp)4621 if_setrcvif(struct mbuf *m, if_t ifp)
4622 {
4623
4624 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4625 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4626 }
4627
4628 void
if_setvtag(struct mbuf * m,uint16_t tag)4629 if_setvtag(struct mbuf *m, uint16_t tag)
4630 {
4631 m->m_pkthdr.ether_vtag = tag;
4632 }
4633
4634 uint16_t
if_getvtag(struct mbuf * m)4635 if_getvtag(struct mbuf *m)
4636 {
4637 return (m->m_pkthdr.ether_vtag);
4638 }
4639
4640 int
if_sendq_empty(if_t ifp)4641 if_sendq_empty(if_t ifp)
4642 {
4643 return (IFQ_DRV_IS_EMPTY(&ifp->if_snd));
4644 }
4645
4646 struct ifaddr *
if_getifaddr(const if_t ifp)4647 if_getifaddr(const if_t ifp)
4648 {
4649 return (ifp->if_addr);
4650 }
4651
4652 int
if_setsendqready(if_t ifp)4653 if_setsendqready(if_t ifp)
4654 {
4655 IFQ_SET_READY(&ifp->if_snd);
4656 return (0);
4657 }
4658
4659 int
if_setsendqlen(if_t ifp,int tx_desc_count)4660 if_setsendqlen(if_t ifp, int tx_desc_count)
4661 {
4662 IFQ_SET_MAXLEN(&ifp->if_snd, tx_desc_count);
4663 ifp->if_snd.ifq_drv_maxlen = tx_desc_count;
4664 return (0);
4665 }
4666
4667 void
if_setnetmapadapter(if_t ifp,struct netmap_adapter * na)4668 if_setnetmapadapter(if_t ifp, struct netmap_adapter *na)
4669 {
4670 ifp->if_netmap = na;
4671 }
4672
4673 struct netmap_adapter *
if_getnetmapadapter(if_t ifp)4674 if_getnetmapadapter(if_t ifp)
4675 {
4676 return (ifp->if_netmap);
4677 }
4678
4679 int
if_vlantrunkinuse(if_t ifp)4680 if_vlantrunkinuse(if_t ifp)
4681 {
4682 return (ifp->if_vlantrunk != NULL);
4683 }
4684
4685 void
if_init(if_t ifp,void * ctx)4686 if_init(if_t ifp, void *ctx)
4687 {
4688 (*ifp->if_init)(ctx);
4689 }
4690
4691 void
if_input(if_t ifp,struct mbuf * sendmp)4692 if_input(if_t ifp, struct mbuf* sendmp)
4693 {
4694 (*ifp->if_input)(ifp, sendmp);
4695 }
4696
4697 int
if_transmit(if_t ifp,struct mbuf * m)4698 if_transmit(if_t ifp, struct mbuf *m)
4699 {
4700 return ((*ifp->if_transmit)(ifp, m));
4701 }
4702
4703 int
if_resolvemulti(if_t ifp,struct sockaddr ** srcs,struct sockaddr * dst)4704 if_resolvemulti(if_t ifp, struct sockaddr **srcs, struct sockaddr *dst)
4705 {
4706 if (ifp->if_resolvemulti == NULL)
4707 return (EOPNOTSUPP);
4708
4709 return (ifp->if_resolvemulti(ifp, srcs, dst));
4710 }
4711
4712 int
if_ioctl(if_t ifp,u_long cmd,void * data)4713 if_ioctl(if_t ifp, u_long cmd, void *data)
4714 {
4715 if (ifp->if_ioctl == NULL)
4716 return (EOPNOTSUPP);
4717
4718 return (ifp->if_ioctl(ifp, cmd, data));
4719 }
4720
4721 struct mbuf *
if_dequeue(if_t ifp)4722 if_dequeue(if_t ifp)
4723 {
4724 struct mbuf *m;
4725
4726 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
4727 return (m);
4728 }
4729
4730 int
if_sendq_prepend(if_t ifp,struct mbuf * m)4731 if_sendq_prepend(if_t ifp, struct mbuf *m)
4732 {
4733 IFQ_DRV_PREPEND(&ifp->if_snd, m);
4734 return (0);
4735 }
4736
4737 int
if_setifheaderlen(if_t ifp,int len)4738 if_setifheaderlen(if_t ifp, int len)
4739 {
4740 ifp->if_hdrlen = len;
4741 return (0);
4742 }
4743
4744 char *
if_getlladdr(const if_t ifp)4745 if_getlladdr(const if_t ifp)
4746 {
4747 return (IF_LLADDR(ifp));
4748 }
4749
4750 void *
if_gethandle(u_char type)4751 if_gethandle(u_char type)
4752 {
4753 return (if_alloc(type));
4754 }
4755
4756 void
if_vlancap(if_t ifp)4757 if_vlancap(if_t ifp)
4758 {
4759 VLAN_CAPABILITIES(ifp);
4760 }
4761
4762 int
if_sethwtsomax(if_t ifp,u_int if_hw_tsomax)4763 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4764 {
4765 ifp->if_hw_tsomax = if_hw_tsomax;
4766 return (0);
4767 }
4768
4769 int
if_sethwtsomaxsegcount(if_t ifp,u_int if_hw_tsomaxsegcount)4770 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4771 {
4772 ifp->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4773 return (0);
4774 }
4775
4776 int
if_sethwtsomaxsegsize(if_t ifp,u_int if_hw_tsomaxsegsize)4777 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4778 {
4779 ifp->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4780 return (0);
4781 }
4782
4783 u_int
if_gethwtsomax(const if_t ifp)4784 if_gethwtsomax(const if_t ifp)
4785 {
4786 return (ifp->if_hw_tsomax);
4787 }
4788
4789 u_int
if_gethwtsomaxsegcount(const if_t ifp)4790 if_gethwtsomaxsegcount(const if_t ifp)
4791 {
4792 return (ifp->if_hw_tsomaxsegcount);
4793 }
4794
4795 u_int
if_gethwtsomaxsegsize(const if_t ifp)4796 if_gethwtsomaxsegsize(const if_t ifp)
4797 {
4798 return (ifp->if_hw_tsomaxsegsize);
4799 }
4800
4801 void
if_setinitfn(if_t ifp,if_init_fn_t init_fn)4802 if_setinitfn(if_t ifp, if_init_fn_t init_fn)
4803 {
4804 ifp->if_init = init_fn;
4805 }
4806
4807 void
if_setinputfn(if_t ifp,if_input_fn_t input_fn)4808 if_setinputfn(if_t ifp, if_input_fn_t input_fn)
4809 {
4810 ifp->if_input = input_fn;
4811 }
4812
4813 if_input_fn_t
if_getinputfn(if_t ifp)4814 if_getinputfn(if_t ifp)
4815 {
4816 return (ifp->if_input);
4817 }
4818
4819 void
if_setioctlfn(if_t ifp,if_ioctl_fn_t ioctl_fn)4820 if_setioctlfn(if_t ifp, if_ioctl_fn_t ioctl_fn)
4821 {
4822 ifp->if_ioctl = ioctl_fn;
4823 }
4824
4825 void
if_setoutputfn(if_t ifp,if_output_fn_t output_fn)4826 if_setoutputfn(if_t ifp, if_output_fn_t output_fn)
4827 {
4828 ifp->if_output = output_fn;
4829 }
4830
4831 void
if_setstartfn(if_t ifp,if_start_fn_t start_fn)4832 if_setstartfn(if_t ifp, if_start_fn_t start_fn)
4833 {
4834 ifp->if_start = start_fn;
4835 }
4836
4837 if_start_fn_t
if_getstartfn(if_t ifp)4838 if_getstartfn(if_t ifp)
4839 {
4840 return (ifp->if_start);
4841 }
4842
4843 void
if_settransmitfn(if_t ifp,if_transmit_fn_t start_fn)4844 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4845 {
4846 ifp->if_transmit = start_fn;
4847 }
4848
4849 if_transmit_fn_t
if_gettransmitfn(if_t ifp)4850 if_gettransmitfn(if_t ifp)
4851 {
4852 return (ifp->if_transmit);
4853 }
4854
4855 void
if_setqflushfn(if_t ifp,if_qflush_fn_t flush_fn)4856 if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4857 {
4858 ifp->if_qflush = flush_fn;
4859 }
4860
4861 void
if_setsndtagallocfn(if_t ifp,if_snd_tag_alloc_t alloc_fn)4862 if_setsndtagallocfn(if_t ifp, if_snd_tag_alloc_t alloc_fn)
4863 {
4864 ifp->if_snd_tag_alloc = alloc_fn;
4865 }
4866
4867 int
if_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** mstp)4868 if_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
4869 struct m_snd_tag **mstp)
4870 {
4871 if (ifp->if_snd_tag_alloc == NULL)
4872 return (EOPNOTSUPP);
4873 return (ifp->if_snd_tag_alloc(ifp, params, mstp));
4874 }
4875
4876 void
if_setgetcounterfn(if_t ifp,if_get_counter_t fn)4877 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4878 {
4879 ifp->if_get_counter = fn;
4880 }
4881
4882 void
if_setreassignfn(if_t ifp,if_reassign_fn_t fn)4883 if_setreassignfn(if_t ifp, if_reassign_fn_t fn)
4884 {
4885 ifp->if_reassign = fn;
4886 }
4887
4888 void
if_setratelimitqueryfn(if_t ifp,if_ratelimit_query_t fn)4889 if_setratelimitqueryfn(if_t ifp, if_ratelimit_query_t fn)
4890 {
4891 ifp->if_ratelimit_query = fn;
4892 }
4893
4894 void
if_setdebugnet_methods(if_t ifp,struct debugnet_methods * m)4895 if_setdebugnet_methods(if_t ifp, struct debugnet_methods *m)
4896 {
4897 ifp->if_debugnet_methods = m;
4898 }
4899
4900 struct label *
if_getmaclabel(if_t ifp)4901 if_getmaclabel(if_t ifp)
4902 {
4903 return (ifp->if_label);
4904 }
4905
4906 void
if_setmaclabel(if_t ifp,struct label * label)4907 if_setmaclabel(if_t ifp, struct label *label)
4908 {
4909 ifp->if_label = label;
4910 }
4911
4912 int
if_gettype(if_t ifp)4913 if_gettype(if_t ifp)
4914 {
4915 return (ifp->if_type);
4916 }
4917
4918 void *
if_getllsoftc(if_t ifp)4919 if_getllsoftc(if_t ifp)
4920 {
4921 return (ifp->if_llsoftc);
4922 }
4923
4924 void
if_setllsoftc(if_t ifp,void * llsoftc)4925 if_setllsoftc(if_t ifp, void *llsoftc)
4926 {
4927 ifp->if_llsoftc = llsoftc;
4928 };
4929
4930 int
if_getlinkstate(if_t ifp)4931 if_getlinkstate(if_t ifp)
4932 {
4933 return (ifp->if_link_state);
4934 }
4935
4936 const uint8_t *
if_getbroadcastaddr(if_t ifp)4937 if_getbroadcastaddr(if_t ifp)
4938 {
4939 return (ifp->if_broadcastaddr);
4940 }
4941
4942 void
if_setbroadcastaddr(if_t ifp,const uint8_t * addr)4943 if_setbroadcastaddr(if_t ifp, const uint8_t *addr)
4944 {
4945 ifp->if_broadcastaddr = addr;
4946 }
4947
4948 int
if_getnumadomain(if_t ifp)4949 if_getnumadomain(if_t ifp)
4950 {
4951 return (ifp->if_numa_domain);
4952 }
4953
4954 uint64_t
if_getcounter(if_t ifp,ift_counter counter)4955 if_getcounter(if_t ifp, ift_counter counter)
4956 {
4957 return (ifp->if_get_counter(ifp, counter));
4958 }
4959
4960 bool
if_altq_is_enabled(if_t ifp)4961 if_altq_is_enabled(if_t ifp)
4962 {
4963 return (ALTQ_IS_ENABLED(&ifp->if_snd));
4964 }
4965
4966 struct vnet *
if_getvnet(if_t ifp)4967 if_getvnet(if_t ifp)
4968 {
4969 return (ifp->if_vnet);
4970 }
4971
4972 struct in_ifinfo *
if_getinet(if_t ifp)4973 if_getinet(if_t ifp)
4974 {
4975 return (ifp->if_inet);
4976 }
4977
4978 struct in6_ifextra *
if_getinet6(if_t ifp)4979 if_getinet6(if_t ifp)
4980 {
4981 return (ifp->if_inet6);
4982 }
4983
4984 u_int
if_getfib(if_t ifp)4985 if_getfib(if_t ifp)
4986 {
4987 return (ifp->if_fib);
4988 }
4989
4990 uint8_t
if_getaddrlen(if_t ifp)4991 if_getaddrlen(if_t ifp)
4992 {
4993 return (ifp->if_addrlen);
4994 }
4995
4996 struct bpf_if *
if_getbpf(if_t ifp)4997 if_getbpf(if_t ifp)
4998 {
4999 return (ifp->if_bpf);
5000 }
5001
5002 struct ifvlantrunk *
if_getvlantrunk(if_t ifp)5003 if_getvlantrunk(if_t ifp)
5004 {
5005 return (ifp->if_vlantrunk);
5006 }
5007
5008 uint8_t
if_getpcp(if_t ifp)5009 if_getpcp(if_t ifp)
5010 {
5011 return (ifp->if_pcp);
5012 }
5013
5014 void *
if_getl2com(if_t ifp)5015 if_getl2com(if_t ifp)
5016 {
5017 return (ifp->if_l2com);
5018 }
5019
5020 void
if_setipsec_accel_methods(if_t ifp,const struct if_ipsec_accel_methods * m)5021 if_setipsec_accel_methods(if_t ifp, const struct if_ipsec_accel_methods *m)
5022 {
5023 ifp->if_ipsec_accel_m = m;
5024 }
5025
5026 #ifdef DDB
5027 static void
if_show_ifnet(struct ifnet * ifp)5028 if_show_ifnet(struct ifnet *ifp)
5029 {
5030 if (ifp == NULL)
5031 return;
5032 db_printf("%s:\n", ifp->if_xname);
5033 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
5034 IF_DB_PRINTF("%s", if_dname);
5035 IF_DB_PRINTF("%d", if_dunit);
5036 IF_DB_PRINTF("%s", if_description);
5037 IF_DB_PRINTF("%u", if_index);
5038 IF_DB_PRINTF("%d", if_idxgen);
5039 IF_DB_PRINTF("%u", if_refcount);
5040 IF_DB_PRINTF("%p", if_softc);
5041 IF_DB_PRINTF("%p", if_l2com);
5042 IF_DB_PRINTF("%p", if_llsoftc);
5043 IF_DB_PRINTF("%d", if_amcount);
5044 IF_DB_PRINTF("%p", if_addr);
5045 IF_DB_PRINTF("%p", if_broadcastaddr);
5046 IF_DB_PRINTF("%u", if_fib);
5047 IF_DB_PRINTF("%p", if_vnet);
5048 IF_DB_PRINTF("%p", if_home_vnet);
5049 IF_DB_PRINTF("%p", if_vlantrunk);
5050 IF_DB_PRINTF("%p", if_bpf);
5051 IF_DB_PRINTF("%u", if_pcount);
5052 IF_DB_PRINTF("%p", if_bridge);
5053 IF_DB_PRINTF("%p", if_lagg);
5054 IF_DB_PRINTF("%p", if_pf_kif);
5055 IF_DB_PRINTF("%p", if_carp);
5056 IF_DB_PRINTF("%p", if_label);
5057 IF_DB_PRINTF("%p", if_netmap);
5058 IF_DB_PRINTF("0x%08x", if_flags);
5059 IF_DB_PRINTF("0x%08x", if_drv_flags);
5060 IF_DB_PRINTF("0x%08x", if_capabilities);
5061 IF_DB_PRINTF("0x%08x", if_capenable);
5062 IF_DB_PRINTF("%p", if_snd.ifq_head);
5063 IF_DB_PRINTF("%p", if_snd.ifq_tail);
5064 IF_DB_PRINTF("%d", if_snd.ifq_len);
5065 IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
5066 IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
5067 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
5068 IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
5069 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
5070 IF_DB_PRINTF("%d", if_snd.altq_type);
5071 IF_DB_PRINTF("%x", if_snd.altq_flags);
5072 #undef IF_DB_PRINTF
5073 }
5074
DB_SHOW_COMMAND(ifnet,db_show_ifnet)5075 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
5076 {
5077 if (!have_addr) {
5078 db_printf("usage: show ifnet <struct ifnet *>\n");
5079 return;
5080 }
5081
5082 if_show_ifnet((struct ifnet *)addr);
5083 }
5084
DB_SHOW_ALL_COMMAND(ifnets,db_show_all_ifnets)5085 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
5086 {
5087 struct ifnet *ifp;
5088 u_short idx;
5089
5090 for (idx = 1; idx <= if_index; idx++) {
5091 ifp = ifindex_table[idx].ife_ifnet;
5092 if (ifp == NULL)
5093 continue;
5094 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);
5095 if (db_pager_quit)
5096 break;
5097 }
5098 }
5099 #endif /* DDB */
5100