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