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