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