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