2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.3 (Berkeley) 1/4/94
30 * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
36 #include "opt_ifpoll.h"
38 #include <sys/param.h>
39 #include <sys/malloc.h>
41 #include <sys/systm.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/socketops.h>
48 #include <sys/kernel.h>
50 #include <sys/mutex.h>
51 #include <sys/sockio.h>
52 #include <sys/syslog.h>
53 #include <sys/sysctl.h>
54 #include <sys/domain.h>
55 #include <sys/thread.h>
56 #include <sys/serialize.h>
59 #include <sys/thread2.h>
60 #include <sys/msgport2.h>
61 #include <sys/mutex2.h>
64 #include <net/if_arp.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/if_var.h>
68 #include <net/ifq_var.h>
69 #include <net/radix.h>
70 #include <net/route.h>
71 #include <net/if_clone.h>
72 #include <net/netisr2.h>
73 #include <net/netmsg2.h>
75 #include <machine/atomic.h>
76 #include <machine/stdarg.h>
77 #include <machine/smp.h>
79 #if defined(INET) || defined(INET6)
81 #include <netinet/in.h>
82 #include <netinet/in_var.h>
83 #include <netinet/if_ether.h>
85 #include <netinet6/in6_var.h>
86 #include <netinet6/in6_ifattach.h>
90 #if defined(COMPAT_43)
91 #include <emulation/43bsd/43bsd_socket.h>
92 #endif /* COMPAT_43 */
94 struct netmsg_ifaddr {
95 struct netmsg_base base;
101 struct ifsubq_stage_head {
102 TAILQ_HEAD(, ifsubq_stage) stg_head;
106 * System initialization
108 static void if_attachdomain(void *);
109 static void if_attachdomain1(struct ifnet *);
110 static int ifconf(u_long, caddr_t, struct ucred *);
111 static void ifinit(void *);
112 static void ifnetinit(void *);
113 static void if_slowtimo(void *);
114 static void link_rtrequest(int, struct rtentry *);
115 static int if_rtdel(struct radix_node *, void *);
116 static void if_slowtimo_dispatch(netmsg_t);
118 /* Helper functions */
119 static void ifsq_watchdog_reset(struct ifsubq_watchdog *);
120 static int if_delmulti_serialized(struct ifnet *, struct sockaddr *);
124 * XXX: declare here to avoid to include many inet6 related files..
125 * should be more generalized?
127 extern void nd6_setmtu(struct ifnet *);
130 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
131 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
133 static int ifsq_stage_cntmax = 4;
134 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
135 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
136 &ifsq_stage_cntmax, 0, "ifq staging packet count max");
138 static int if_stats_compat = 0;
139 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
140 &if_stats_compat, 0, "Compat the old ifnet stats");
142 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
143 /* Must be after netisr_init */
144 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
146 static if_com_alloc_t *if_com_alloc[256];
147 static if_com_free_t *if_com_free[256];
149 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
150 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
151 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
153 int ifqmaxlen = IFQ_MAXLEN;
154 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
156 static struct callout if_slowtimo_timer;
157 static struct netmsg_base if_slowtimo_netmsg;
160 struct ifnet **ifindex2ifnet = NULL;
161 static struct thread ifnet_threads[MAXCPU];
163 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
166 #define IFQ_KTR_STRING "ifq=%p"
167 #define IFQ_KTR_ARGS struct ifaltq *ifq
169 #define KTR_IFQ KTR_ALL
171 KTR_INFO_MASTER(ifq);
172 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
173 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
174 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
176 #define IF_START_KTR_STRING "ifp=%p"
177 #define IF_START_KTR_ARGS struct ifnet *ifp
179 #define KTR_IF_START KTR_ALL
181 KTR_INFO_MASTER(if_start);
182 KTR_INFO(KTR_IF_START, if_start, run, 0,
183 IF_START_KTR_STRING, IF_START_KTR_ARGS);
184 KTR_INFO(KTR_IF_START, if_start, sched, 1,
185 IF_START_KTR_STRING, IF_START_KTR_ARGS);
186 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
187 IF_START_KTR_STRING, IF_START_KTR_ARGS);
188 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
189 IF_START_KTR_STRING, IF_START_KTR_ARGS);
190 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
191 IF_START_KTR_STRING, IF_START_KTR_ARGS);
192 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
195 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
198 * Network interface utility routines.
200 * Routines with ifa_ifwith* names take sockaddr *'s as
209 callout_init_mp(&if_slowtimo_timer);
210 netmsg_init(&if_slowtimo_netmsg, NULL, &netisr_adone_rport,
211 MSGF_PRIORITY, if_slowtimo_dispatch);
214 TAILQ_FOREACH(ifp, &ifnet, if_link) {
215 if (ifp->if_snd.altq_maxlen == 0) {
216 if_printf(ifp, "XXX: driver didn't set altq_maxlen\n");
217 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
222 /* Start if_slowtimo */
223 lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg.lmsg);
227 ifsq_ifstart_ipifunc(void *arg)
229 struct ifaltq_subque *ifsq = arg;
230 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
233 if (lmsg->ms_flags & MSGF_DONE)
234 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg);
239 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
241 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
242 TAILQ_REMOVE(&head->stg_head, stage, stg_link);
243 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
249 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
251 KKASSERT((stage->stg_flags &
252 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
253 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
254 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
258 * Schedule ifnet.if_start on the subqueue owner CPU
261 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
265 if (!force && curthread->td_type == TD_TYPE_NETISR &&
266 ifsq_stage_cntmax > 0) {
267 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
271 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
272 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
273 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
277 cpu = ifsq_get_cpuid(ifsq);
279 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
281 ifsq_ifstart_ipifunc(ifsq);
286 * This function will release ifnet.if_start subqueue interlock,
287 * if ifnet.if_start for the subqueue does not need to be scheduled
290 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
292 if (!running || ifsq_is_empty(ifsq)
294 || ifsq->ifsq_altq->altq_tbr != NULL
299 * ifnet.if_start subqueue interlock is released, if:
300 * 1) Hardware can not take any packets, due to
301 * o interface is marked down
302 * o hardware queue is full (ifsq_is_oactive)
303 * Under the second situation, hardware interrupt
304 * or polling(4) will call/schedule ifnet.if_start
305 * on the subqueue when hardware queue is ready
306 * 2) There is no packet in the subqueue.
307 * Further ifq_dispatch or ifq_handoff will call/
308 * schedule ifnet.if_start on the subqueue.
309 * 3) TBR is used and it does not allow further
311 * TBR callout will call ifnet.if_start on the
314 if (!running || !ifsq_data_ready(ifsq)) {
315 ifsq_clr_started(ifsq);
316 ALTQ_SQ_UNLOCK(ifsq);
319 ALTQ_SQ_UNLOCK(ifsq);
325 ifsq_ifstart_dispatch(netmsg_t msg)
327 struct lwkt_msg *lmsg = &msg->base.lmsg;
328 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
329 struct ifnet *ifp = ifsq_get_ifp(ifsq);
330 struct globaldata *gd = mycpu;
331 int running = 0, need_sched;
335 lwkt_replymsg(lmsg, 0); /* reply ASAP */
337 if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) {
339 * We need to chase the subqueue owner CPU change.
341 ifsq_ifstart_schedule(ifsq, 1);
346 ifsq_serialize_hw(ifsq);
347 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
348 ifp->if_start(ifp, ifsq);
349 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
352 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
353 ifsq_deserialize_hw(ifsq);
357 * More data need to be transmitted, ifnet.if_start is
358 * scheduled on the subqueue owner CPU, and we keep going.
359 * NOTE: ifnet.if_start subqueue interlock is not released.
361 ifsq_ifstart_schedule(ifsq, 0);
367 /* Device driver ifnet.if_start helper function */
369 ifsq_devstart(struct ifaltq_subque *ifsq)
371 struct ifnet *ifp = ifsq_get_ifp(ifsq);
374 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
377 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
378 ALTQ_SQ_UNLOCK(ifsq);
381 ifsq_set_started(ifsq);
382 ALTQ_SQ_UNLOCK(ifsq);
384 ifp->if_start(ifp, ifsq);
386 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
389 if (ifsq_ifstart_need_schedule(ifsq, running)) {
391 * More data need to be transmitted, ifnet.if_start is
392 * scheduled on ifnet's CPU, and we keep going.
393 * NOTE: ifnet.if_start interlock is not released.
395 ifsq_ifstart_schedule(ifsq, 0);
400 if_devstart(struct ifnet *ifp)
402 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
405 /* Device driver ifnet.if_start schedule helper function */
407 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
409 ifsq_ifstart_schedule(ifsq, 1);
413 if_devstart_sched(struct ifnet *ifp)
415 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
419 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
421 lwkt_serialize_enter(ifp->if_serializer);
425 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
427 lwkt_serialize_exit(ifp->if_serializer);
431 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
433 return lwkt_serialize_try(ifp->if_serializer);
438 if_default_serialize_assert(struct ifnet *ifp,
439 enum ifnet_serialize slz __unused,
440 boolean_t serialized)
443 ASSERT_SERIALIZED(ifp->if_serializer);
445 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
450 * Attach an interface to the list of "active" interfaces.
452 * The serializer is optional.
455 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
457 unsigned socksize, ifasize;
458 int namelen, masklen;
459 struct sockaddr_dl *sdl;
464 static int if_indexlim = 8;
466 if (ifp->if_serialize != NULL) {
467 KASSERT(ifp->if_deserialize != NULL &&
468 ifp->if_tryserialize != NULL &&
469 ifp->if_serialize_assert != NULL,
470 ("serialize functions are partially setup"));
473 * If the device supplies serialize functions,
474 * then clear if_serializer to catch any invalid
475 * usage of this field.
477 KASSERT(serializer == NULL,
478 ("both serialize functions and default serializer "
480 ifp->if_serializer = NULL;
482 KASSERT(ifp->if_deserialize == NULL &&
483 ifp->if_tryserialize == NULL &&
484 ifp->if_serialize_assert == NULL,
485 ("serialize functions are partially setup"));
486 ifp->if_serialize = if_default_serialize;
487 ifp->if_deserialize = if_default_deserialize;
488 ifp->if_tryserialize = if_default_tryserialize;
490 ifp->if_serialize_assert = if_default_serialize_assert;
494 * The serializer can be passed in from the device,
495 * allowing the same serializer to be used for both
496 * the interrupt interlock and the device queue.
497 * If not specified, the netif structure will use an
498 * embedded serializer.
500 if (serializer == NULL) {
501 serializer = &ifp->if_default_serializer;
502 lwkt_serialize_init(serializer);
504 ifp->if_serializer = serializer;
507 mtx_init(&ifp->if_ioctl_mtx);
508 mtx_lock(&ifp->if_ioctl_mtx);
510 lwkt_gettoken(&ifnet_token); /* protect if_index and ifnet tailq */
511 ifp->if_index = ++if_index;
515 * The old code would work if the interface passed a pre-existing
516 * chain of ifaddrs to this code. We don't trust our callers to
517 * properly initialize the tailq, however, so we no longer allow
518 * this unlikely case.
520 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
521 M_IFADDR, M_WAITOK | M_ZERO);
522 for (i = 0; i < ncpus; ++i)
523 TAILQ_INIT(&ifp->if_addrheads[i]);
525 TAILQ_INIT(&ifp->if_multiaddrs);
526 TAILQ_INIT(&ifp->if_groups);
527 getmicrotime(&ifp->if_lastchange);
528 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
534 /* grow ifindex2ifnet */
535 n = if_indexlim * sizeof(*q);
536 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
538 bcopy(ifindex2ifnet, q, n/2);
539 kfree(ifindex2ifnet, M_IFADDR);
544 ifindex2ifnet[if_index] = ifp;
547 * create a Link Level name for this device
549 namelen = strlen(ifp->if_xname);
550 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
551 socksize = masklen + ifp->if_addrlen;
552 if (socksize < sizeof(*sdl))
553 socksize = sizeof(*sdl);
554 socksize = RT_ROUNDUP(socksize);
555 ifasize = sizeof(struct ifaddr) + 2 * socksize;
556 ifa = ifa_create(ifasize, M_WAITOK);
557 sdl = (struct sockaddr_dl *)(ifa + 1);
558 sdl->sdl_len = socksize;
559 sdl->sdl_family = AF_LINK;
560 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
561 sdl->sdl_nlen = namelen;
562 sdl->sdl_index = ifp->if_index;
563 sdl->sdl_type = ifp->if_type;
564 ifp->if_lladdr = ifa;
566 ifa->ifa_rtrequest = link_rtrequest;
567 ifa->ifa_addr = (struct sockaddr *)sdl;
568 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
569 ifa->ifa_netmask = (struct sockaddr *)sdl;
570 sdl->sdl_len = masklen;
572 sdl->sdl_data[--namelen] = 0xff;
573 ifa_iflink(ifa, ifp, 0 /* Insert head */);
575 ifp->if_data_pcpu = kmalloc_cachealign(
576 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
578 if (ifp->if_mapsubq == NULL)
579 ifp->if_mapsubq = ifq_mapsubq_default;
583 ifq->altq_disc = NULL;
584 ifq->altq_flags &= ALTQF_CANTCHANGE;
585 ifq->altq_tbr = NULL;
588 if (ifq->altq_subq_cnt <= 0)
589 ifq->altq_subq_cnt = 1;
590 ifq->altq_subq = kmalloc_cachealign(
591 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
592 M_DEVBUF, M_WAITOK | M_ZERO);
594 if (ifq->altq_maxlen == 0) {
595 if_printf(ifp, "driver didn't set altq_maxlen\n");
596 ifq_set_maxlen(ifq, ifqmaxlen);
599 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
600 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
602 ALTQ_SQ_LOCK_INIT(ifsq);
603 ifsq->ifsq_index = q;
605 ifsq->ifsq_altq = ifq;
606 ifsq->ifsq_ifp = ifp;
608 ifsq->ifsq_maxlen = ifq->altq_maxlen;
609 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
610 ifsq->ifsq_prepended = NULL;
611 ifsq->ifsq_started = 0;
612 ifsq->ifsq_hw_oactive = 0;
613 ifsq_set_cpuid(ifsq, 0);
614 if (ifp->if_serializer != NULL)
615 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
618 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
619 M_DEVBUF, M_WAITOK | M_ZERO);
620 for (i = 0; i < ncpus; ++i)
621 ifsq->ifsq_stage[i].stg_subq = ifsq;
623 ifsq->ifsq_ifstart_nmsg =
624 kmalloc(ncpus * sizeof(struct netmsg_base),
625 M_LWKTMSG, M_WAITOK);
626 for (i = 0; i < ncpus; ++i) {
627 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
628 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
629 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
632 ifq_set_classic(ifq);
634 if (!SLIST_EMPTY(&domains))
635 if_attachdomain1(ifp);
637 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
638 lwkt_reltoken(&ifnet_token);
640 /* Announce the interface. */
641 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
642 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
643 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
645 mtx_unlock(&ifp->if_ioctl_mtx);
649 if_attachdomain(void *dummy)
654 TAILQ_FOREACH(ifp, &ifnet, if_list)
655 if_attachdomain1(ifp);
658 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
659 if_attachdomain, NULL);
662 if_attachdomain1(struct ifnet *ifp)
668 /* address family dependent data region */
669 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
670 SLIST_FOREACH(dp, &domains, dom_next)
671 if (dp->dom_ifattach)
672 ifp->if_afdata[dp->dom_family] =
673 (*dp->dom_ifattach)(ifp);
678 * Purge all addresses whose type is _not_ AF_LINK
681 if_purgeaddrs_nolink(struct ifnet *ifp)
683 struct ifaddr_container *ifac, *next;
685 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
687 struct ifaddr *ifa = ifac->ifa;
689 /* Leave link ifaddr as it is */
690 if (ifa->ifa_addr->sa_family == AF_LINK)
693 /* XXX: Ugly!! ad hoc just for INET */
694 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
695 struct ifaliasreq ifr;
696 #ifdef IFADDR_DEBUG_VERBOSE
699 kprintf("purge in4 addr %p: ", ifa);
700 for (i = 0; i < ncpus; ++i)
701 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
705 bzero(&ifr, sizeof ifr);
706 ifr.ifra_addr = *ifa->ifa_addr;
707 if (ifa->ifa_dstaddr)
708 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
709 if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp,
715 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
716 #ifdef IFADDR_DEBUG_VERBOSE
719 kprintf("purge in6 addr %p: ", ifa);
720 for (i = 0; i < ncpus; ++i)
721 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
726 /* ifp_addrhead is already updated */
730 ifa_ifunlink(ifa, ifp);
736 ifq_stage_detach_handler(netmsg_t nmsg)
738 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
741 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
742 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
743 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
745 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
746 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
748 lwkt_replymsg(&nmsg->lmsg, 0);
752 ifq_stage_detach(struct ifaltq *ifq)
754 struct netmsg_base base;
757 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
758 ifq_stage_detach_handler);
759 base.lmsg.u.ms_resultp = ifq;
761 for (cpu = 0; cpu < ncpus; ++cpu)
762 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
765 struct netmsg_if_rtdel {
766 struct netmsg_base base;
771 if_rtdel_dispatch(netmsg_t msg)
773 struct netmsg_if_rtdel *rmsg = (void *)msg;
777 for (i = 1; i <= AF_MAX; i++) {
778 struct radix_node_head *rnh;
780 if ((rnh = rt_tables[cpu][i]) == NULL)
782 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
787 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
789 lwkt_replymsg(&rmsg->base.lmsg, 0);
793 * Detach an interface, removing it from the
794 * list of "active" interfaces.
797 if_detach(struct ifnet *ifp)
799 struct netmsg_if_rtdel msg;
803 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
806 * Remove routes and flush queues.
810 if (ifp->if_flags & IFF_NPOLLING)
811 ifpoll_deregister(ifp);
816 if (ifq_is_enabled(&ifp->if_snd))
817 altq_disable(&ifp->if_snd);
818 if (ifq_is_attached(&ifp->if_snd))
819 altq_detach(&ifp->if_snd);
823 * Clean up all addresses.
825 ifp->if_lladdr = NULL;
827 if_purgeaddrs_nolink(ifp);
828 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
831 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
832 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
833 ("non-link ifaddr is left on if_addrheads"));
835 ifa_ifunlink(ifa, ifp);
837 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
838 ("there are still ifaddrs left on if_addrheads"));
843 * Remove all IPv4 kernel structures related to ifp.
850 * Remove all IPv6 kernel structs related to ifp. This should be done
851 * before removing routing entries below, since IPv6 interface direct
852 * routes are expected to be removed by the IPv6-specific kernel API.
853 * Otherwise, the kernel will detect some inconsistency and bark it.
859 * Delete all remaining routes using this interface
861 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
864 rt_domsg_global(&msg.base);
866 /* Announce that the interface is gone. */
867 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
868 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
870 SLIST_FOREACH(dp, &domains, dom_next)
871 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
872 (*dp->dom_ifdetach)(ifp,
873 ifp->if_afdata[dp->dom_family]);
876 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
878 lwkt_gettoken(&ifnet_token);
879 ifindex2ifnet[ifp->if_index] = NULL;
880 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
882 TAILQ_REMOVE(&ifnet, ifp, if_link);
883 lwkt_reltoken(&ifnet_token);
885 kfree(ifp->if_addrheads, M_IFADDR);
887 lwkt_synchronize_ipiqs("if_detach");
888 ifq_stage_detach(&ifp->if_snd);
890 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
891 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
893 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
894 kfree(ifsq->ifsq_stage, M_DEVBUF);
896 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
898 kfree(ifp->if_data_pcpu, M_DEVBUF);
904 * Create interface group without members
907 if_creategroup(const char *groupname)
909 struct ifg_group *ifg = NULL;
911 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
912 M_TEMP, M_NOWAIT)) == NULL)
915 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
917 ifg->ifg_carp_demoted = 0;
918 TAILQ_INIT(&ifg->ifg_members);
920 pfi_attach_ifgroup(ifg);
922 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
928 * Add a group to an interface
931 if_addgroup(struct ifnet *ifp, const char *groupname)
933 struct ifg_list *ifgl;
934 struct ifg_group *ifg = NULL;
935 struct ifg_member *ifgm;
937 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
938 groupname[strlen(groupname) - 1] <= '9')
941 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
942 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
945 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
948 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
953 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
954 if (!strcmp(ifg->ifg_group, groupname))
957 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
964 ifgl->ifgl_group = ifg;
965 ifgm->ifgm_ifp = ifp;
967 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
968 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
971 pfi_group_change(groupname);
978 * Remove a group from an interface
981 if_delgroup(struct ifnet *ifp, const char *groupname)
983 struct ifg_list *ifgl;
984 struct ifg_member *ifgm;
986 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
987 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
992 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
994 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
995 if (ifgm->ifgm_ifp == ifp)
999 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1000 kfree(ifgm, M_TEMP);
1003 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1004 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1006 pfi_detach_ifgroup(ifgl->ifgl_group);
1008 kfree(ifgl->ifgl_group, M_TEMP);
1011 kfree(ifgl, M_TEMP);
1014 pfi_group_change(groupname);
1021 * Stores all groups from an interface in memory pointed
1025 if_getgroup(caddr_t data, struct ifnet *ifp)
1028 struct ifg_list *ifgl;
1029 struct ifg_req ifgrq, *ifgp;
1030 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1032 if (ifgr->ifgr_len == 0) {
1033 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1034 ifgr->ifgr_len += sizeof(struct ifg_req);
1038 len = ifgr->ifgr_len;
1039 ifgp = ifgr->ifgr_groups;
1040 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1041 if (len < sizeof(ifgrq))
1043 bzero(&ifgrq, sizeof ifgrq);
1044 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1045 sizeof(ifgrq.ifgrq_group));
1046 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1047 sizeof(struct ifg_req))))
1049 len -= sizeof(ifgrq);
1057 * Stores all members of a group in memory pointed to by data
1060 if_getgroupmembers(caddr_t data)
1062 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1063 struct ifg_group *ifg;
1064 struct ifg_member *ifgm;
1065 struct ifg_req ifgrq, *ifgp;
1068 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1069 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1074 if (ifgr->ifgr_len == 0) {
1075 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1076 ifgr->ifgr_len += sizeof(ifgrq);
1080 len = ifgr->ifgr_len;
1081 ifgp = ifgr->ifgr_groups;
1082 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1083 if (len < sizeof(ifgrq))
1085 bzero(&ifgrq, sizeof ifgrq);
1086 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1087 sizeof(ifgrq.ifgrq_member));
1088 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1089 sizeof(struct ifg_req))))
1091 len -= sizeof(ifgrq);
1099 * Delete Routes for a Network Interface
1101 * Called for each routing entry via the rnh->rnh_walktree() call above
1102 * to delete all route entries referencing a detaching network interface.
1105 * rn pointer to node in the routing table
1106 * arg argument passed to rnh->rnh_walktree() - detaching interface
1110 * errno failed - reason indicated
1114 if_rtdel(struct radix_node *rn, void *arg)
1116 struct rtentry *rt = (struct rtentry *)rn;
1117 struct ifnet *ifp = arg;
1120 if (rt->rt_ifp == ifp) {
1123 * Protect (sorta) against walktree recursion problems
1124 * with cloned routes
1126 if (!(rt->rt_flags & RTF_UP))
1129 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1130 rt_mask(rt), rt->rt_flags,
1133 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1141 * Locate an interface based on a complete address.
1144 ifa_ifwithaddr(struct sockaddr *addr)
1148 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1149 struct ifaddr_container *ifac;
1151 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1152 struct ifaddr *ifa = ifac->ifa;
1154 if (ifa->ifa_addr->sa_family != addr->sa_family)
1156 if (sa_equal(addr, ifa->ifa_addr))
1158 if ((ifp->if_flags & IFF_BROADCAST) &&
1159 ifa->ifa_broadaddr &&
1160 /* IPv6 doesn't have broadcast */
1161 ifa->ifa_broadaddr->sa_len != 0 &&
1162 sa_equal(ifa->ifa_broadaddr, addr))
1169 * Locate the point to point interface with a given destination address.
1172 ifa_ifwithdstaddr(struct sockaddr *addr)
1176 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1177 struct ifaddr_container *ifac;
1179 if (!(ifp->if_flags & IFF_POINTOPOINT))
1182 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1183 struct ifaddr *ifa = ifac->ifa;
1185 if (ifa->ifa_addr->sa_family != addr->sa_family)
1187 if (ifa->ifa_dstaddr &&
1188 sa_equal(addr, ifa->ifa_dstaddr))
1196 * Find an interface on a specific network. If many, choice
1197 * is most specific found.
1200 ifa_ifwithnet(struct sockaddr *addr)
1203 struct ifaddr *ifa_maybe = NULL;
1204 u_int af = addr->sa_family;
1205 char *addr_data = addr->sa_data, *cplim;
1208 * AF_LINK addresses can be looked up directly by their index number,
1209 * so do that if we can.
1211 if (af == AF_LINK) {
1212 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1214 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1215 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1219 * Scan though each interface, looking for ones that have
1220 * addresses in this address family.
1222 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1223 struct ifaddr_container *ifac;
1225 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1226 struct ifaddr *ifa = ifac->ifa;
1227 char *cp, *cp2, *cp3;
1229 if (ifa->ifa_addr->sa_family != af)
1231 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1233 * This is a bit broken as it doesn't
1234 * take into account that the remote end may
1235 * be a single node in the network we are
1237 * The trouble is that we don't know the
1238 * netmask for the remote end.
1240 if (ifa->ifa_dstaddr != NULL &&
1241 sa_equal(addr, ifa->ifa_dstaddr))
1245 * if we have a special address handler,
1246 * then use it instead of the generic one.
1248 if (ifa->ifa_claim_addr) {
1249 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1257 * Scan all the bits in the ifa's address.
1258 * If a bit dissagrees with what we are
1259 * looking for, mask it with the netmask
1260 * to see if it really matters.
1261 * (A byte at a time)
1263 if (ifa->ifa_netmask == 0)
1266 cp2 = ifa->ifa_addr->sa_data;
1267 cp3 = ifa->ifa_netmask->sa_data;
1268 cplim = ifa->ifa_netmask->sa_len +
1269 (char *)ifa->ifa_netmask;
1271 if ((*cp++ ^ *cp2++) & *cp3++)
1272 goto next; /* next address! */
1274 * If the netmask of what we just found
1275 * is more specific than what we had before
1276 * (if we had one) then remember the new one
1277 * before continuing to search
1278 * for an even better one.
1280 if (ifa_maybe == NULL ||
1281 rn_refines((char *)ifa->ifa_netmask,
1282 (char *)ifa_maybe->ifa_netmask))
1291 * Find an interface address specific to an interface best matching
1295 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1297 struct ifaddr_container *ifac;
1298 char *cp, *cp2, *cp3;
1300 struct ifaddr *ifa_maybe = NULL;
1301 u_int af = addr->sa_family;
1305 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1306 struct ifaddr *ifa = ifac->ifa;
1308 if (ifa->ifa_addr->sa_family != af)
1310 if (ifa_maybe == NULL)
1312 if (ifa->ifa_netmask == NULL) {
1313 if (sa_equal(addr, ifa->ifa_addr) ||
1314 (ifa->ifa_dstaddr != NULL &&
1315 sa_equal(addr, ifa->ifa_dstaddr)))
1319 if (ifp->if_flags & IFF_POINTOPOINT) {
1320 if (sa_equal(addr, ifa->ifa_dstaddr))
1324 cp2 = ifa->ifa_addr->sa_data;
1325 cp3 = ifa->ifa_netmask->sa_data;
1326 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1327 for (; cp3 < cplim; cp3++)
1328 if ((*cp++ ^ *cp2++) & *cp3)
1338 * Default action when installing a route with a Link Level gateway.
1339 * Lookup an appropriate real ifa to point to.
1340 * This should be moved to /sys/net/link.c eventually.
1343 link_rtrequest(int cmd, struct rtentry *rt)
1346 struct sockaddr *dst;
1349 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1350 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1352 ifa = ifaof_ifpforaddr(dst, ifp);
1354 IFAFREE(rt->rt_ifa);
1357 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1358 ifa->ifa_rtrequest(cmd, rt);
1363 * Mark an interface down and notify protocols of
1365 * NOTE: must be called at splnet or eqivalent.
1368 if_unroute(struct ifnet *ifp, int flag, int fam)
1370 struct ifaddr_container *ifac;
1372 ifp->if_flags &= ~flag;
1373 getmicrotime(&ifp->if_lastchange);
1374 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1375 struct ifaddr *ifa = ifac->ifa;
1377 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1378 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1380 ifq_purge_all(&ifp->if_snd);
1385 * Mark an interface up and notify protocols of
1387 * NOTE: must be called at splnet or eqivalent.
1390 if_route(struct ifnet *ifp, int flag, int fam)
1392 struct ifaddr_container *ifac;
1394 ifq_purge_all(&ifp->if_snd);
1395 ifp->if_flags |= flag;
1396 getmicrotime(&ifp->if_lastchange);
1397 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1398 struct ifaddr *ifa = ifac->ifa;
1400 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1401 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1410 * Mark an interface down and notify protocols of the transition. An
1411 * interface going down is also considered to be a synchronizing event.
1412 * We must ensure that all packet processing related to the interface
1413 * has completed before we return so e.g. the caller can free the ifnet
1414 * structure that the mbufs may be referencing.
1416 * NOTE: must be called at splnet or eqivalent.
1419 if_down(struct ifnet *ifp)
1421 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1422 netmsg_service_sync();
1426 * Mark an interface up and notify protocols of
1428 * NOTE: must be called at splnet or eqivalent.
1431 if_up(struct ifnet *ifp)
1433 if_route(ifp, IFF_UP, AF_UNSPEC);
1437 * Process a link state change.
1438 * NOTE: must be called at splsoftnet or equivalent.
1441 if_link_state_change(struct ifnet *ifp)
1443 int link_state = ifp->if_link_state;
1446 devctl_notify("IFNET", ifp->if_xname,
1447 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1451 * Handle interface watchdog timer routines. Called
1452 * from softclock, we decrement timers (if set) and
1453 * call the appropriate interface routine on expiration.
1456 if_slowtimo_dispatch(netmsg_t nmsg)
1458 struct globaldata *gd = mycpu;
1461 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1462 ("not in netisr0"));
1465 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1468 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1471 if (if_stats_compat) {
1472 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1473 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1474 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1475 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1476 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1477 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1478 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1479 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1480 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1481 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1482 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1485 if (ifp->if_timer == 0 || --ifp->if_timer) {
1489 if (ifp->if_watchdog) {
1490 if (ifnet_tryserialize_all(ifp)) {
1491 (*ifp->if_watchdog)(ifp);
1492 ifnet_deserialize_all(ifp);
1494 /* try again next timeout */
1502 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1506 if_slowtimo(void *arg __unused)
1508 struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1510 KASSERT(mycpuid == 0, ("not on cpu0"));
1512 if (lmsg->ms_flags & MSGF_DONE)
1513 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1518 * Map interface name to
1519 * interface structure pointer.
1522 ifunit(const char *name)
1527 * Search all the interfaces for this name/number
1530 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1531 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1539 * Map interface name in a sockaddr_dl to
1540 * interface structure pointer.
1543 if_withname(struct sockaddr *sa)
1545 char ifname[IFNAMSIZ+1];
1546 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1548 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1549 (sdl->sdl_nlen > IFNAMSIZ) )
1553 * ifunit wants a null-terminated name. It may not be null-terminated
1554 * in the sockaddr. We don't want to change the caller's sockaddr,
1555 * and there might not be room to put the trailing null anyway, so we
1556 * make a local copy that we know we can null terminate safely.
1559 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1560 ifname[sdl->sdl_nlen] = '\0';
1561 return ifunit(ifname);
1569 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1580 size_t namelen, onamelen;
1581 char new_name[IFNAMSIZ];
1583 struct sockaddr_dl *sdl;
1588 return (ifconf(cmd, data, cred));
1593 ifr = (struct ifreq *)data;
1598 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1600 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1601 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1603 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1605 return (if_clone_destroy(ifr->ifr_name));
1606 case SIOCIFGCLONERS:
1607 return (if_clone_list((struct if_clonereq *)data));
1613 * Nominal ioctl through interface, lookup the ifp and obtain a
1614 * lock to serialize the ifconfig ioctl operation.
1616 ifp = ifunit(ifr->ifr_name);
1620 mtx_lock(&ifp->if_ioctl_mtx);
1624 ifr->ifr_index = ifp->if_index;
1628 ifr->ifr_flags = ifp->if_flags;
1629 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1633 ifr->ifr_reqcap = ifp->if_capabilities;
1634 ifr->ifr_curcap = ifp->if_capenable;
1638 ifr->ifr_metric = ifp->if_metric;
1642 ifr->ifr_mtu = ifp->if_mtu;
1646 ifr->ifr_tsolen = ifp->if_tsolen;
1650 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1651 sizeof(ifp->if_data));
1655 ifr->ifr_phys = ifp->if_physical;
1658 case SIOCGIFPOLLCPU:
1659 ifr->ifr_pollcpu = -1;
1662 case SIOCSIFPOLLCPU:
1666 error = priv_check_cred(cred, PRIV_ROOT, 0);
1669 new_flags = (ifr->ifr_flags & 0xffff) |
1670 (ifr->ifr_flagshigh << 16);
1671 if (ifp->if_flags & IFF_SMART) {
1672 /* Smart drivers twiddle their own routes */
1673 } else if (ifp->if_flags & IFF_UP &&
1674 (new_flags & IFF_UP) == 0) {
1678 } else if (new_flags & IFF_UP &&
1679 (ifp->if_flags & IFF_UP) == 0) {
1685 #ifdef IFPOLL_ENABLE
1686 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1687 if (new_flags & IFF_NPOLLING)
1688 ifpoll_register(ifp);
1690 ifpoll_deregister(ifp);
1694 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1695 (new_flags &~ IFF_CANTCHANGE);
1696 if (new_flags & IFF_PPROMISC) {
1697 /* Permanently promiscuous mode requested */
1698 ifp->if_flags |= IFF_PROMISC;
1699 } else if (ifp->if_pcount == 0) {
1700 ifp->if_flags &= ~IFF_PROMISC;
1702 if (ifp->if_ioctl) {
1703 ifnet_serialize_all(ifp);
1704 ifp->if_ioctl(ifp, cmd, data, cred);
1705 ifnet_deserialize_all(ifp);
1707 getmicrotime(&ifp->if_lastchange);
1711 error = priv_check_cred(cred, PRIV_ROOT, 0);
1714 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1718 ifnet_serialize_all(ifp);
1719 ifp->if_ioctl(ifp, cmd, data, cred);
1720 ifnet_deserialize_all(ifp);
1724 error = priv_check_cred(cred, PRIV_ROOT, 0);
1727 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1730 if (new_name[0] == '\0') {
1734 if (ifunit(new_name) != NULL) {
1739 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1741 /* Announce the departure of the interface. */
1742 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1744 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1745 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1746 /* XXX IFA_LOCK(ifa); */
1747 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1748 namelen = strlen(new_name);
1749 onamelen = sdl->sdl_nlen;
1751 * Move the address if needed. This is safe because we
1752 * allocate space for a name of length IFNAMSIZ when we
1753 * create this in if_attach().
1755 if (namelen != onamelen) {
1756 bcopy(sdl->sdl_data + onamelen,
1757 sdl->sdl_data + namelen, sdl->sdl_alen);
1759 bcopy(new_name, sdl->sdl_data, namelen);
1760 sdl->sdl_nlen = namelen;
1761 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1762 bzero(sdl->sdl_data, onamelen);
1763 while (namelen != 0)
1764 sdl->sdl_data[--namelen] = 0xff;
1765 /* XXX IFA_UNLOCK(ifa) */
1767 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1769 /* Announce the return of the interface. */
1770 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1774 error = priv_check_cred(cred, PRIV_ROOT, 0);
1777 ifp->if_metric = ifr->ifr_metric;
1778 getmicrotime(&ifp->if_lastchange);
1782 error = priv_check_cred(cred, PRIV_ROOT, 0);
1785 if (ifp->if_ioctl == NULL) {
1789 ifnet_serialize_all(ifp);
1790 error = ifp->if_ioctl(ifp, cmd, data, cred);
1791 ifnet_deserialize_all(ifp);
1793 getmicrotime(&ifp->if_lastchange);
1798 u_long oldmtu = ifp->if_mtu;
1800 error = priv_check_cred(cred, PRIV_ROOT, 0);
1803 if (ifp->if_ioctl == NULL) {
1807 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
1811 ifnet_serialize_all(ifp);
1812 error = ifp->if_ioctl(ifp, cmd, data, cred);
1813 ifnet_deserialize_all(ifp);
1815 getmicrotime(&ifp->if_lastchange);
1819 * If the link MTU changed, do network layer specific procedure.
1821 if (ifp->if_mtu != oldmtu) {
1830 error = priv_check_cred(cred, PRIV_ROOT, 0);
1834 /* XXX need driver supplied upper limit */
1835 if (ifr->ifr_tsolen <= 0) {
1839 ifp->if_tsolen = ifr->ifr_tsolen;
1844 error = priv_check_cred(cred, PRIV_ROOT, 0);
1848 /* Don't allow group membership on non-multicast interfaces. */
1849 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1854 /* Don't let users screw up protocols' entries. */
1855 if (ifr->ifr_addr.sa_family != AF_LINK) {
1860 if (cmd == SIOCADDMULTI) {
1861 struct ifmultiaddr *ifma;
1862 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1864 error = if_delmulti(ifp, &ifr->ifr_addr);
1867 getmicrotime(&ifp->if_lastchange);
1870 case SIOCSIFPHYADDR:
1871 case SIOCDIFPHYADDR:
1873 case SIOCSIFPHYADDR_IN6:
1875 case SIOCSLIFPHYADDR:
1877 case SIOCSIFGENERIC:
1878 error = priv_check_cred(cred, PRIV_ROOT, 0);
1881 if (ifp->if_ioctl == 0) {
1885 ifnet_serialize_all(ifp);
1886 error = ifp->if_ioctl(ifp, cmd, data, cred);
1887 ifnet_deserialize_all(ifp);
1889 getmicrotime(&ifp->if_lastchange);
1893 ifs = (struct ifstat *)data;
1894 ifs->ascii[0] = '\0';
1896 case SIOCGIFPSRCADDR:
1897 case SIOCGIFPDSTADDR:
1898 case SIOCGLIFPHYADDR:
1900 case SIOCGIFGENERIC:
1901 if (ifp->if_ioctl == NULL) {
1905 ifnet_serialize_all(ifp);
1906 error = ifp->if_ioctl(ifp, cmd, data, cred);
1907 ifnet_deserialize_all(ifp);
1911 error = priv_check_cred(cred, PRIV_ROOT, 0);
1914 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
1915 ifr->ifr_addr.sa_len);
1916 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1920 oif_flags = ifp->if_flags;
1921 if (so->so_proto == 0) {
1926 error = so_pru_control_direct(so, cmd, data, ifp);
1931 case SIOCSIFDSTADDR:
1933 case SIOCSIFBRDADDR:
1934 case SIOCSIFNETMASK:
1935 #if BYTE_ORDER != BIG_ENDIAN
1936 if (ifr->ifr_addr.sa_family == 0 &&
1937 ifr->ifr_addr.sa_len < 16) {
1938 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1939 ifr->ifr_addr.sa_len = 16;
1942 if (ifr->ifr_addr.sa_len == 0)
1943 ifr->ifr_addr.sa_len = 16;
1949 case OSIOCGIFDSTADDR:
1950 cmd = SIOCGIFDSTADDR;
1952 case OSIOCGIFBRDADDR:
1953 cmd = SIOCGIFBRDADDR;
1955 case OSIOCGIFNETMASK:
1956 cmd = SIOCGIFNETMASK;
1962 error = so_pru_control_direct(so, cmd, data, ifp);
1966 case OSIOCGIFDSTADDR:
1967 case OSIOCGIFBRDADDR:
1968 case OSIOCGIFNETMASK:
1969 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1972 #endif /* COMPAT_43 */
1974 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1976 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1977 if (ifp->if_flags & IFF_UP) {
1987 mtx_unlock(&ifp->if_ioctl_mtx);
1992 * Set/clear promiscuous mode on interface ifp based on the truth value
1993 * of pswitch. The calls are reference counted so that only the first
1994 * "on" request actually has an effect, as does the final "off" request.
1995 * Results are undefined if the "off" and "on" requests are not matched.
1998 ifpromisc(struct ifnet *ifp, int pswitch)
2004 oldflags = ifp->if_flags;
2005 if (ifp->if_flags & IFF_PPROMISC) {
2006 /* Do nothing if device is in permanently promiscuous mode */
2007 ifp->if_pcount += pswitch ? 1 : -1;
2012 * If the device is not configured up, we cannot put it in
2015 if ((ifp->if_flags & IFF_UP) == 0)
2017 if (ifp->if_pcount++ != 0)
2019 ifp->if_flags |= IFF_PROMISC;
2020 log(LOG_INFO, "%s: promiscuous mode enabled\n",
2023 if (--ifp->if_pcount > 0)
2025 ifp->if_flags &= ~IFF_PROMISC;
2026 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2029 ifr.ifr_flags = ifp->if_flags;
2030 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2031 ifnet_serialize_all(ifp);
2032 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2033 ifnet_deserialize_all(ifp);
2037 ifp->if_flags = oldflags;
2042 * Return interface configuration
2043 * of system. List may be used
2044 * in later ioctl's (above) to get
2045 * other information.
2048 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2050 struct ifconf *ifc = (struct ifconf *)data;
2052 struct sockaddr *sa;
2053 struct ifreq ifr, *ifrp;
2054 int space = ifc->ifc_len, error = 0;
2056 ifrp = ifc->ifc_req;
2057 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2058 struct ifaddr_container *ifac;
2061 if (space <= sizeof ifr)
2065 * Zero the stack declared structure first to prevent
2066 * memory disclosure.
2068 bzero(&ifr, sizeof(ifr));
2069 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2070 >= sizeof(ifr.ifr_name)) {
2071 error = ENAMETOOLONG;
2076 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2077 struct ifaddr *ifa = ifac->ifa;
2079 if (space <= sizeof ifr)
2082 if (cred->cr_prison &&
2083 prison_if(cred, sa))
2087 if (cmd == OSIOCGIFCONF) {
2088 struct osockaddr *osa =
2089 (struct osockaddr *)&ifr.ifr_addr;
2091 osa->sa_family = sa->sa_family;
2092 error = copyout(&ifr, ifrp, sizeof ifr);
2096 if (sa->sa_len <= sizeof(*sa)) {
2098 error = copyout(&ifr, ifrp, sizeof ifr);
2101 if (space < (sizeof ifr) + sa->sa_len -
2104 space -= sa->sa_len - sizeof(*sa);
2105 error = copyout(&ifr, ifrp,
2106 sizeof ifr.ifr_name);
2108 error = copyout(sa, &ifrp->ifr_addr,
2110 ifrp = (struct ifreq *)
2111 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2115 space -= sizeof ifr;
2120 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2121 error = copyout(&ifr, ifrp, sizeof ifr);
2124 space -= sizeof ifr;
2128 ifc->ifc_len -= space;
2133 * Just like if_promisc(), but for all-multicast-reception mode.
2136 if_allmulti(struct ifnet *ifp, int onswitch)
2144 if (ifp->if_amcount++ == 0) {
2145 ifp->if_flags |= IFF_ALLMULTI;
2146 ifr.ifr_flags = ifp->if_flags;
2147 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2148 ifnet_serialize_all(ifp);
2149 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2151 ifnet_deserialize_all(ifp);
2154 if (ifp->if_amcount > 1) {
2157 ifp->if_amcount = 0;
2158 ifp->if_flags &= ~IFF_ALLMULTI;
2159 ifr.ifr_flags = ifp->if_flags;
2160 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2161 ifnet_serialize_all(ifp);
2162 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2164 ifnet_deserialize_all(ifp);
2176 * Add a multicast listenership to the interface in question.
2177 * The link layer provides a routine which converts
2180 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2181 struct ifmultiaddr **retifma)
2183 struct sockaddr *llsa, *dupsa;
2185 struct ifmultiaddr *ifma;
2187 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2190 * If the matching multicast address already exists
2191 * then don't add a new one, just add a reference
2193 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2194 if (sa_equal(sa, ifma->ifma_addr)) {
2195 ifma->ifma_refcount++;
2203 * Give the link layer a chance to accept/reject it, and also
2204 * find out which AF_LINK address this maps to, if it isn't one
2207 if (ifp->if_resolvemulti) {
2208 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2215 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2216 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_WAITOK);
2217 bcopy(sa, dupsa, sa->sa_len);
2219 ifma->ifma_addr = dupsa;
2220 ifma->ifma_lladdr = llsa;
2221 ifma->ifma_ifp = ifp;
2222 ifma->ifma_refcount = 1;
2223 ifma->ifma_protospec = NULL;
2224 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2226 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2231 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2232 if (sa_equal(ifma->ifma_addr, llsa))
2236 ifma->ifma_refcount++;
2238 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2239 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_WAITOK);
2240 bcopy(llsa, dupsa, llsa->sa_len);
2241 ifma->ifma_addr = dupsa;
2242 ifma->ifma_ifp = ifp;
2243 ifma->ifma_refcount = 1;
2244 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2248 * We are certain we have added something, so call down to the
2249 * interface to let them know about it.
2252 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2258 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2259 struct ifmultiaddr **retifma)
2263 ifnet_serialize_all(ifp);
2264 error = if_addmulti_serialized(ifp, sa, retifma);
2265 ifnet_deserialize_all(ifp);
2271 * Remove a reference to a multicast address on this interface. Yell
2272 * if the request does not match an existing membership.
2275 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2277 struct ifmultiaddr *ifma;
2279 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2281 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2282 if (sa_equal(sa, ifma->ifma_addr))
2287 if (ifma->ifma_refcount > 1) {
2288 ifma->ifma_refcount--;
2292 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2293 sa = ifma->ifma_lladdr;
2294 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2296 * Make sure the interface driver is notified
2297 * in the case of a link layer mcast group being left.
2299 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2300 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2301 kfree(ifma->ifma_addr, M_IFMADDR);
2302 kfree(ifma, M_IFMADDR);
2307 * Now look for the link-layer address which corresponds to
2308 * this network address. It had been squirreled away in
2309 * ifma->ifma_lladdr for this purpose (so we don't have
2310 * to call ifp->if_resolvemulti() again), and we saved that
2311 * value in sa above. If some nasty deleted the
2312 * link-layer address out from underneath us, we can deal because
2313 * the address we stored was is not the same as the one which was
2314 * in the record for the link-layer address. (So we don't complain
2317 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2318 if (sa_equal(sa, ifma->ifma_addr))
2323 if (ifma->ifma_refcount > 1) {
2324 ifma->ifma_refcount--;
2328 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2329 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2330 kfree(ifma->ifma_addr, M_IFMADDR);
2331 kfree(sa, M_IFMADDR);
2332 kfree(ifma, M_IFMADDR);
2338 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2342 ifnet_serialize_all(ifp);
2343 error = if_delmulti_serialized(ifp, sa);
2344 ifnet_deserialize_all(ifp);
2350 * Delete all multicast group membership for an interface.
2351 * Should be used to quickly flush all multicast filters.
2354 if_delallmulti_serialized(struct ifnet *ifp)
2356 struct ifmultiaddr *ifma, mark;
2359 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2361 bzero(&sa, sizeof(sa));
2362 sa.sa_family = AF_UNSPEC;
2363 sa.sa_len = sizeof(sa);
2365 bzero(&mark, sizeof(mark));
2366 mark.ifma_addr = &sa;
2368 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2369 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2370 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2371 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2374 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2377 if_delmulti_serialized(ifp, ifma->ifma_addr);
2379 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2384 * Set the link layer address on an interface.
2386 * At this time we only support certain types of interfaces,
2387 * and we don't allow the length of the address to change.
2390 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2392 struct sockaddr_dl *sdl;
2395 sdl = IF_LLSOCKADDR(ifp);
2398 if (len != sdl->sdl_alen) /* don't allow length to change */
2400 switch (ifp->if_type) {
2401 case IFT_ETHER: /* these types use struct arpcom */
2404 case IFT_IEEE8023ADLAG:
2405 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2406 bcopy(lladdr, LLADDR(sdl), len);
2412 * If the interface is already up, we need
2413 * to re-init it in order to reprogram its
2416 ifnet_serialize_all(ifp);
2417 if ((ifp->if_flags & IFF_UP) != 0) {
2419 struct ifaddr_container *ifac;
2422 ifp->if_flags &= ~IFF_UP;
2423 ifr.ifr_flags = ifp->if_flags;
2424 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2425 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2427 ifp->if_flags |= IFF_UP;
2428 ifr.ifr_flags = ifp->if_flags;
2429 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2430 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2434 * Also send gratuitous ARPs to notify other nodes about
2435 * the address change.
2437 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2438 struct ifaddr *ifa = ifac->ifa;
2440 if (ifa->ifa_addr != NULL &&
2441 ifa->ifa_addr->sa_family == AF_INET)
2442 arp_gratuitous(ifp, ifa);
2446 ifnet_deserialize_all(ifp);
2450 struct ifmultiaddr *
2451 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2453 struct ifmultiaddr *ifma;
2455 /* TODO: need ifnet_serialize_main */
2456 ifnet_serialize_all(ifp);
2457 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2458 if (sa_equal(ifma->ifma_addr, sa))
2460 ifnet_deserialize_all(ifp);
2466 * This function locates the first real ethernet MAC from a network
2467 * card and loads it into node, returning 0 on success or ENOENT if
2468 * no suitable interfaces were found. It is used by the uuid code to
2469 * generate a unique 6-byte number.
2472 if_getanyethermac(uint16_t *node, int minlen)
2475 struct sockaddr_dl *sdl;
2477 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2478 if (ifp->if_type != IFT_ETHER)
2480 sdl = IF_LLSOCKADDR(ifp);
2481 if (sdl->sdl_alen < minlen)
2483 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2491 * The name argument must be a pointer to storage which will last as
2492 * long as the interface does. For physical devices, the result of
2493 * device_get_name(dev) is a good choice and for pseudo-devices a
2494 * static string works well.
2497 if_initname(struct ifnet *ifp, const char *name, int unit)
2499 ifp->if_dname = name;
2500 ifp->if_dunit = unit;
2501 if (unit != IF_DUNIT_NONE)
2502 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2504 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2508 if_printf(struct ifnet *ifp, const char *fmt, ...)
2513 retval = kprintf("%s: ", ifp->if_xname);
2514 __va_start(ap, fmt);
2515 retval += kvprintf(fmt, ap);
2521 if_alloc(uint8_t type)
2527 * XXX temporary hack until arpcom is setup in if_l2com
2529 if (type == IFT_ETHER)
2530 size = sizeof(struct arpcom);
2532 size = sizeof(struct ifnet);
2534 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2536 ifp->if_type = type;
2538 if (if_com_alloc[type] != NULL) {
2539 ifp->if_l2com = if_com_alloc[type](type, ifp);
2540 if (ifp->if_l2com == NULL) {
2541 kfree(ifp, M_IFNET);
2549 if_free(struct ifnet *ifp)
2551 kfree(ifp, M_IFNET);
2555 ifq_set_classic(struct ifaltq *ifq)
2557 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2558 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2562 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2563 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2567 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2568 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2569 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2570 KASSERT(request != NULL, ("request is not specified"));
2572 ifq->altq_mapsubq = mapsubq;
2573 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2574 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2576 ifsq->ifsq_enqueue = enqueue;
2577 ifsq->ifsq_dequeue = dequeue;
2578 ifsq->ifsq_request = request;
2583 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2585 m->m_nextpkt = NULL;
2586 if (ifsq->ifsq_norm_tail == NULL)
2587 ifsq->ifsq_norm_head = m;
2589 ifsq->ifsq_norm_tail->m_nextpkt = m;
2590 ifsq->ifsq_norm_tail = m;
2591 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2595 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2597 m->m_nextpkt = NULL;
2598 if (ifsq->ifsq_prio_tail == NULL)
2599 ifsq->ifsq_prio_head = m;
2601 ifsq->ifsq_prio_tail->m_nextpkt = m;
2602 ifsq->ifsq_prio_tail = m;
2603 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2604 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2607 static struct mbuf *
2608 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2612 m = ifsq->ifsq_norm_head;
2614 if ((ifsq->ifsq_norm_head = m->m_nextpkt) == NULL)
2615 ifsq->ifsq_norm_tail = NULL;
2616 m->m_nextpkt = NULL;
2617 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2622 static struct mbuf *
2623 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2627 m = ifsq->ifsq_prio_head;
2629 if ((ifsq->ifsq_prio_head = m->m_nextpkt) == NULL)
2630 ifsq->ifsq_prio_tail = NULL;
2631 m->m_nextpkt = NULL;
2632 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2633 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2639 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2640 struct altq_pktattr *pa __unused)
2643 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2644 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
2645 if ((m->m_flags & M_PRIO) &&
2646 ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen / 2) &&
2647 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt / 2)) {
2648 struct mbuf *m_drop;
2651 * Perform drop-head on normal queue
2653 m_drop = ifsq_norm_dequeue(ifsq);
2654 if (m_drop != NULL) {
2656 ifsq_prio_enqueue(ifsq, m);
2659 /* XXX nothing could be dropped? */
2664 if (m->m_flags & M_PRIO)
2665 ifsq_prio_enqueue(ifsq, m);
2667 ifsq_norm_enqueue(ifsq, m);
2673 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
2679 m = ifsq->ifsq_prio_head;
2681 m = ifsq->ifsq_norm_head;
2685 m = ifsq_prio_dequeue(ifsq);
2687 m = ifsq_norm_dequeue(ifsq);
2691 panic("unsupported ALTQ dequeue op: %d", op);
2697 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2704 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
2712 panic("unsupported ALTQ request: %d", req);
2718 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2720 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2721 int running = 0, need_sched;
2724 * Try to do direct ifnet.if_start on the subqueue first, if there is
2725 * contention on the subqueue hardware serializer, ifnet.if_start on
2726 * the subqueue will be scheduled on the subqueue owner CPU.
2728 if (!ifsq_tryserialize_hw(ifsq)) {
2730 * Subqueue hardware serializer contention happened,
2731 * ifnet.if_start on the subqueue is scheduled on
2732 * the subqueue owner CPU, and we keep going.
2734 ifsq_ifstart_schedule(ifsq, 1);
2738 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2739 ifp->if_start(ifp, ifsq);
2740 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2743 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2745 ifsq_deserialize_hw(ifsq);
2749 * More data need to be transmitted, ifnet.if_start on the
2750 * subqueue is scheduled on the subqueue owner CPU, and we
2752 * NOTE: ifnet.if_start subqueue interlock is not released.
2754 ifsq_ifstart_schedule(ifsq, force_sched);
2759 * Subqeue packets staging mechanism:
2761 * The packets enqueued into the subqueue are staged to a certain amount
2762 * before the ifnet.if_start on the subqueue is called. In this way, the
2763 * driver could avoid writing to hardware registers upon every packet,
2764 * instead, hardware registers could be written when certain amount of
2765 * packets are put onto hardware TX ring. The measurement on several modern
2766 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2767 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2768 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2769 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2770 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2772 * Subqueue packets staging is performed for two entry points into drivers'
2773 * transmission function:
2774 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2775 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2777 * Subqueue packets staging will be stopped upon any of the following
2779 * - If the count of packets enqueued on the current CPU is great than or
2780 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2781 * - If the total length of packets enqueued on the current CPU is great
2782 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2783 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2784 * is usually less than hardware's MTU.
2785 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2786 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2788 * - The if_start_rollup(), which is registered as low priority netisr
2789 * rollup function, is called; probably because no more work is pending
2793 * Currently subqueue packet staging is only performed in netisr threads.
2796 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2798 struct ifaltq *ifq = &ifp->if_snd;
2799 struct ifaltq_subque *ifsq;
2800 int error, start = 0, len, mcast = 0, avoid_start = 0;
2801 struct ifsubq_stage_head *head = NULL;
2802 struct ifsubq_stage *stage = NULL;
2803 struct globaldata *gd = mycpu;
2804 struct thread *td = gd->gd_curthread;
2806 crit_enter_quick(td);
2808 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
2809 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
2811 len = m->m_pkthdr.len;
2812 if (m->m_flags & M_MCAST)
2815 if (td->td_type == TD_TYPE_NETISR) {
2816 head = &ifsubq_stage_heads[mycpuid];
2817 stage = ifsq_get_stage(ifsq, mycpuid);
2820 stage->stg_len += len;
2821 if (stage->stg_cnt < ifsq_stage_cntmax &&
2822 stage->stg_len < (ifp->if_mtu - max_protohdr))
2827 error = ifsq_enqueue_locked(ifsq, m, pa);
2829 if (!ifsq_data_ready(ifsq)) {
2830 ALTQ_SQ_UNLOCK(ifsq);
2831 crit_exit_quick(td);
2836 if (!ifsq_is_started(ifsq)) {
2838 ALTQ_SQ_UNLOCK(ifsq);
2841 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
2842 ifsq_stage_insert(head, stage);
2844 IFNET_STAT_INC(ifp, obytes, len);
2846 IFNET_STAT_INC(ifp, omcasts, 1);
2847 crit_exit_quick(td);
2852 * Hold the subqueue interlock of ifnet.if_start
2854 ifsq_set_started(ifsq);
2857 ALTQ_SQ_UNLOCK(ifsq);
2860 IFNET_STAT_INC(ifp, obytes, len);
2862 IFNET_STAT_INC(ifp, omcasts, 1);
2865 if (stage != NULL) {
2866 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
2867 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
2869 ifsq_stage_remove(head, stage);
2870 ifsq_ifstart_schedule(ifsq, 1);
2872 crit_exit_quick(td);
2876 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
2877 ifsq_stage_remove(head, stage);
2885 crit_exit_quick(td);
2889 ifsq_ifstart_try(ifsq, 0);
2891 crit_exit_quick(td);
2896 ifa_create(int size, int flags)
2901 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
2903 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2907 ifa->ifa_containers =
2908 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
2909 M_IFADDR, M_WAITOK | M_ZERO);
2910 ifa->ifa_ncnt = ncpus;
2911 for (i = 0; i < ncpus; ++i) {
2912 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2914 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2916 ifac->ifa_refcnt = 1;
2919 kprintf("alloc ifa %p %d\n", ifa, size);
2925 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2927 struct ifaddr *ifa = ifac->ifa;
2929 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2930 KKASSERT(ifac->ifa_refcnt == 0);
2931 KASSERT(ifac->ifa_listmask == 0,
2932 ("ifa is still on %#x lists", ifac->ifa_listmask));
2934 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2936 #ifdef IFADDR_DEBUG_VERBOSE
2937 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2940 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2941 ("invalid # of ifac, %d", ifa->ifa_ncnt));
2942 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2944 kprintf("free ifa %p\n", ifa);
2946 kfree(ifa->ifa_containers, M_IFADDR);
2947 kfree(ifa, M_IFADDR);
2952 ifa_iflink_dispatch(netmsg_t nmsg)
2954 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2955 struct ifaddr *ifa = msg->ifa;
2956 struct ifnet *ifp = msg->ifp;
2958 struct ifaddr_container *ifac;
2962 ifac = &ifa->ifa_containers[cpu];
2963 ASSERT_IFAC_VALID(ifac);
2964 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2965 ("ifaddr is on if_addrheads"));
2967 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2969 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2971 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2975 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2979 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2981 struct netmsg_ifaddr msg;
2983 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2984 0, ifa_iflink_dispatch);
2989 ifa_domsg(&msg.base.lmsg, 0);
2993 ifa_ifunlink_dispatch(netmsg_t nmsg)
2995 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2996 struct ifaddr *ifa = msg->ifa;
2997 struct ifnet *ifp = msg->ifp;
2999 struct ifaddr_container *ifac;
3003 ifac = &ifa->ifa_containers[cpu];
3004 ASSERT_IFAC_VALID(ifac);
3005 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3006 ("ifaddr is not on if_addrhead"));
3008 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3009 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3013 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
3017 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3019 struct netmsg_ifaddr msg;
3021 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3022 0, ifa_ifunlink_dispatch);
3026 ifa_domsg(&msg.base.lmsg, 0);
3030 ifa_destroy_dispatch(netmsg_t nmsg)
3032 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3035 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
3039 ifa_destroy(struct ifaddr *ifa)
3041 struct netmsg_ifaddr msg;
3043 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3044 0, ifa_destroy_dispatch);
3047 ifa_domsg(&msg.base.lmsg, 0);
3051 ifnet_portfn(int cpu)
3053 return &ifnet_threads[cpu].td_msgport;
3057 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
3059 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
3061 if (next_cpu < ncpus)
3062 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
3064 lwkt_replymsg(lmsg, 0);
3068 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
3070 KKASSERT(cpu < ncpus);
3071 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
3075 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
3077 KKASSERT(cpu < ncpus);
3078 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
3082 * Generic netmsg service loop. Some protocols may roll their own but all
3083 * must do the basic command dispatch function call done here.
3086 ifnet_service_loop(void *arg __unused)
3090 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
3091 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
3092 msg->base.nm_dispatch(msg);
3097 if_start_rollup(void)
3099 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3100 struct ifsubq_stage *stage;
3104 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3105 struct ifaltq_subque *ifsq = stage->stg_subq;
3108 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3110 ifsq_stage_remove(head, stage);
3113 ifsq_ifstart_schedule(ifsq, 1);
3118 if (!ifsq_is_started(ifsq)) {
3120 * Hold the subqueue interlock of
3123 ifsq_set_started(ifsq);
3126 ALTQ_SQ_UNLOCK(ifsq);
3129 ifsq_ifstart_try(ifsq, 1);
3131 KKASSERT((stage->stg_flags &
3132 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3139 ifnetinit(void *dummy __unused)
3143 for (i = 0; i < ncpus; ++i) {
3144 struct thread *thr = &ifnet_threads[i];
3146 lwkt_create(ifnet_service_loop, NULL, NULL,
3147 thr, TDF_NOSTART|TDF_FORCE_SPINPORT|TDF_FIXEDCPU,
3149 netmsg_service_port_init(&thr->td_msgport);
3153 for (i = 0; i < ncpus; ++i)
3154 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3155 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3159 if_register_com_alloc(u_char type,
3160 if_com_alloc_t *a, if_com_free_t *f)
3163 KASSERT(if_com_alloc[type] == NULL,
3164 ("if_register_com_alloc: %d already registered", type));
3165 KASSERT(if_com_free[type] == NULL,
3166 ("if_register_com_alloc: %d free already registered", type));
3168 if_com_alloc[type] = a;
3169 if_com_free[type] = f;
3173 if_deregister_com_alloc(u_char type)
3176 KASSERT(if_com_alloc[type] != NULL,
3177 ("if_deregister_com_alloc: %d not registered", type));
3178 KASSERT(if_com_free[type] != NULL,
3179 ("if_deregister_com_alloc: %d free not registered", type));
3180 if_com_alloc[type] = NULL;
3181 if_com_free[type] = NULL;
3185 if_ring_count2(int cnt, int cnt_max)
3189 KASSERT(cnt_max >= 1 && powerof2(cnt_max),
3190 ("invalid ring count max %d", cnt_max));
3199 while ((1 << (shift + 1)) <= cnt)
3203 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
3204 ("calculate cnt %d, ncpus2 %d, cnt max %d",
3205 cnt, ncpus2, cnt_max));
3210 ifq_set_maxlen(struct ifaltq *ifq, int len)
3212 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3216 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3218 return ALTQ_SUBQ_INDEX_DEFAULT;
3222 ifq_mapsubq_mask(struct ifaltq *ifq, int cpuid)
3224 return (cpuid & ifq->altq_subq_mask);
3228 ifsq_watchdog(void *arg)
3230 struct ifsubq_watchdog *wd = arg;
3233 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3236 ifp = ifsq_get_ifp(wd->wd_subq);
3237 if (ifnet_tryserialize_all(ifp)) {
3238 wd->wd_watchdog(wd->wd_subq);
3239 ifnet_deserialize_all(ifp);
3241 /* try again next timeout */
3245 ifsq_watchdog_reset(wd);
3249 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3251 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3252 ifsq_get_cpuid(wd->wd_subq));
3256 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3257 ifsq_watchdog_t watchdog)
3259 callout_init_mp(&wd->wd_callout);
3262 wd->wd_watchdog = watchdog;
3266 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3269 ifsq_watchdog_reset(wd);
3273 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3276 callout_stop(&wd->wd_callout);