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 *);
117 /* Helper functions */
118 static void ifsq_watchdog_reset(struct ifsubq_watchdog *);
119 static int if_delmulti_serialized(struct ifnet *, struct sockaddr *);
123 * XXX: declare here to avoid to include many inet6 related files..
124 * should be more generalized?
126 extern void nd6_setmtu(struct ifnet *);
129 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
130 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
132 static int ifsq_stage_cntmax = 4;
133 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
134 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
135 &ifsq_stage_cntmax, 0, "ifq staging packet count max");
137 static int if_stats_compat = 0;
138 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
139 &if_stats_compat, 0, "Compat the old ifnet stats");
141 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
142 /* Must be after netisr_init */
143 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
145 static if_com_alloc_t *if_com_alloc[256];
146 static if_com_free_t *if_com_free[256];
148 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
149 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
150 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
152 int ifqmaxlen = IFQ_MAXLEN;
153 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
155 struct callout if_slowtimo_timer;
158 struct ifnet **ifindex2ifnet = NULL;
159 static struct thread ifnet_threads[MAXCPU];
161 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
164 #define IFQ_KTR_STRING "ifq=%p"
165 #define IFQ_KTR_ARGS struct ifaltq *ifq
167 #define KTR_IFQ KTR_ALL
169 KTR_INFO_MASTER(ifq);
170 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
171 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
172 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
174 #define IF_START_KTR_STRING "ifp=%p"
175 #define IF_START_KTR_ARGS struct ifnet *ifp
177 #define KTR_IF_START KTR_ALL
179 KTR_INFO_MASTER(if_start);
180 KTR_INFO(KTR_IF_START, if_start, run, 0,
181 IF_START_KTR_STRING, IF_START_KTR_ARGS);
182 KTR_INFO(KTR_IF_START, if_start, sched, 1,
183 IF_START_KTR_STRING, IF_START_KTR_ARGS);
184 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
185 IF_START_KTR_STRING, IF_START_KTR_ARGS);
186 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
187 IF_START_KTR_STRING, IF_START_KTR_ARGS);
188 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
189 IF_START_KTR_STRING, IF_START_KTR_ARGS);
190 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
193 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
196 * Network interface utility routines.
198 * Routines with ifa_ifwith* names take sockaddr *'s as
207 callout_init(&if_slowtimo_timer);
210 TAILQ_FOREACH(ifp, &ifnet, if_link) {
211 if (ifp->if_snd.altq_maxlen == 0) {
212 if_printf(ifp, "XXX: driver didn't set altq_maxlen\n");
213 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
222 ifsq_ifstart_ipifunc(void *arg)
224 struct ifaltq_subque *ifsq = arg;
225 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
228 if (lmsg->ms_flags & MSGF_DONE)
229 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg);
234 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
236 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
237 TAILQ_REMOVE(&head->stg_head, stage, stg_link);
238 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
244 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
246 KKASSERT((stage->stg_flags &
247 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
248 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
249 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
253 * Schedule ifnet.if_start on the subqueue owner CPU
256 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
260 if (!force && curthread->td_type == TD_TYPE_NETISR &&
261 ifsq_stage_cntmax > 0) {
262 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
266 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
267 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
268 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
272 cpu = ifsq_get_cpuid(ifsq);
274 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
276 ifsq_ifstart_ipifunc(ifsq);
281 * This function will release ifnet.if_start subqueue interlock,
282 * if ifnet.if_start for the subqueue does not need to be scheduled
285 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
287 if (!running || ifsq_is_empty(ifsq)
289 || ifsq->ifsq_altq->altq_tbr != NULL
294 * ifnet.if_start subqueue interlock is released, if:
295 * 1) Hardware can not take any packets, due to
296 * o interface is marked down
297 * o hardware queue is full (ifsq_is_oactive)
298 * Under the second situation, hardware interrupt
299 * or polling(4) will call/schedule ifnet.if_start
300 * on the subqueue when hardware queue is ready
301 * 2) There is no packet in the subqueue.
302 * Further ifq_dispatch or ifq_handoff will call/
303 * schedule ifnet.if_start on the subqueue.
304 * 3) TBR is used and it does not allow further
306 * TBR callout will call ifnet.if_start on the
309 if (!running || !ifsq_data_ready(ifsq)) {
310 ifsq_clr_started(ifsq);
311 ALTQ_SQ_UNLOCK(ifsq);
314 ALTQ_SQ_UNLOCK(ifsq);
320 ifsq_ifstart_dispatch(netmsg_t msg)
322 struct lwkt_msg *lmsg = &msg->base.lmsg;
323 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
324 struct ifnet *ifp = ifsq_get_ifp(ifsq);
325 struct globaldata *gd = mycpu;
326 int running = 0, need_sched;
330 lwkt_replymsg(lmsg, 0); /* reply ASAP */
332 if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) {
334 * We need to chase the subqueue owner CPU change.
336 ifsq_ifstart_schedule(ifsq, 1);
341 ifsq_serialize_hw(ifsq);
342 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
343 ifp->if_start(ifp, ifsq);
344 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
347 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
348 ifsq_deserialize_hw(ifsq);
352 * More data need to be transmitted, ifnet.if_start is
353 * scheduled on the subqueue owner CPU, and we keep going.
354 * NOTE: ifnet.if_start subqueue interlock is not released.
356 ifsq_ifstart_schedule(ifsq, 0);
362 /* Device driver ifnet.if_start helper function */
364 ifsq_devstart(struct ifaltq_subque *ifsq)
366 struct ifnet *ifp = ifsq_get_ifp(ifsq);
369 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
372 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
373 ALTQ_SQ_UNLOCK(ifsq);
376 ifsq_set_started(ifsq);
377 ALTQ_SQ_UNLOCK(ifsq);
379 ifp->if_start(ifp, ifsq);
381 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
384 if (ifsq_ifstart_need_schedule(ifsq, running)) {
386 * More data need to be transmitted, ifnet.if_start is
387 * scheduled on ifnet's CPU, and we keep going.
388 * NOTE: ifnet.if_start interlock is not released.
390 ifsq_ifstart_schedule(ifsq, 0);
395 if_devstart(struct ifnet *ifp)
397 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
400 /* Device driver ifnet.if_start schedule helper function */
402 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
404 ifsq_ifstart_schedule(ifsq, 1);
408 if_devstart_sched(struct ifnet *ifp)
410 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
414 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
416 lwkt_serialize_enter(ifp->if_serializer);
420 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
422 lwkt_serialize_exit(ifp->if_serializer);
426 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
428 return lwkt_serialize_try(ifp->if_serializer);
433 if_default_serialize_assert(struct ifnet *ifp,
434 enum ifnet_serialize slz __unused,
435 boolean_t serialized)
438 ASSERT_SERIALIZED(ifp->if_serializer);
440 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
445 * Attach an interface to the list of "active" interfaces.
447 * The serializer is optional.
450 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
452 unsigned socksize, ifasize;
453 int namelen, masklen;
454 struct sockaddr_dl *sdl;
459 static int if_indexlim = 8;
461 if (ifp->if_serialize != NULL) {
462 KASSERT(ifp->if_deserialize != NULL &&
463 ifp->if_tryserialize != NULL &&
464 ifp->if_serialize_assert != NULL,
465 ("serialize functions are partially setup"));
468 * If the device supplies serialize functions,
469 * then clear if_serializer to catch any invalid
470 * usage of this field.
472 KASSERT(serializer == NULL,
473 ("both serialize functions and default serializer "
475 ifp->if_serializer = NULL;
477 KASSERT(ifp->if_deserialize == NULL &&
478 ifp->if_tryserialize == NULL &&
479 ifp->if_serialize_assert == NULL,
480 ("serialize functions are partially setup"));
481 ifp->if_serialize = if_default_serialize;
482 ifp->if_deserialize = if_default_deserialize;
483 ifp->if_tryserialize = if_default_tryserialize;
485 ifp->if_serialize_assert = if_default_serialize_assert;
489 * The serializer can be passed in from the device,
490 * allowing the same serializer to be used for both
491 * the interrupt interlock and the device queue.
492 * If not specified, the netif structure will use an
493 * embedded serializer.
495 if (serializer == NULL) {
496 serializer = &ifp->if_default_serializer;
497 lwkt_serialize_init(serializer);
499 ifp->if_serializer = serializer;
502 mtx_init(&ifp->if_ioctl_mtx);
503 mtx_lock(&ifp->if_ioctl_mtx);
505 lwkt_gettoken(&ifnet_token); /* protect if_index and ifnet tailq */
506 ifp->if_index = ++if_index;
510 * The old code would work if the interface passed a pre-existing
511 * chain of ifaddrs to this code. We don't trust our callers to
512 * properly initialize the tailq, however, so we no longer allow
513 * this unlikely case.
515 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
516 M_IFADDR, M_WAITOK | M_ZERO);
517 for (i = 0; i < ncpus; ++i)
518 TAILQ_INIT(&ifp->if_addrheads[i]);
520 TAILQ_INIT(&ifp->if_prefixhead);
521 TAILQ_INIT(&ifp->if_multiaddrs);
522 TAILQ_INIT(&ifp->if_groups);
523 getmicrotime(&ifp->if_lastchange);
524 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
530 /* grow ifindex2ifnet */
531 n = if_indexlim * sizeof(*q);
532 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
534 bcopy(ifindex2ifnet, q, n/2);
535 kfree(ifindex2ifnet, M_IFADDR);
540 ifindex2ifnet[if_index] = ifp;
543 * create a Link Level name for this device
545 namelen = strlen(ifp->if_xname);
546 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
547 socksize = masklen + ifp->if_addrlen;
548 if (socksize < sizeof(*sdl))
549 socksize = sizeof(*sdl);
550 socksize = RT_ROUNDUP(socksize);
551 ifasize = sizeof(struct ifaddr) + 2 * socksize;
552 ifa = ifa_create(ifasize, M_WAITOK);
553 sdl = (struct sockaddr_dl *)(ifa + 1);
554 sdl->sdl_len = socksize;
555 sdl->sdl_family = AF_LINK;
556 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
557 sdl->sdl_nlen = namelen;
558 sdl->sdl_index = ifp->if_index;
559 sdl->sdl_type = ifp->if_type;
560 ifp->if_lladdr = ifa;
562 ifa->ifa_rtrequest = link_rtrequest;
563 ifa->ifa_addr = (struct sockaddr *)sdl;
564 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
565 ifa->ifa_netmask = (struct sockaddr *)sdl;
566 sdl->sdl_len = masklen;
568 sdl->sdl_data[--namelen] = 0xff;
569 ifa_iflink(ifa, ifp, 0 /* Insert head */);
571 ifp->if_data_pcpu = kmalloc_cachealign(
572 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
574 if (ifp->if_mapsubq == NULL)
575 ifp->if_mapsubq = ifq_mapsubq_default;
579 ifq->altq_disc = NULL;
580 ifq->altq_flags &= ALTQF_CANTCHANGE;
581 ifq->altq_tbr = NULL;
584 if (ifq->altq_subq_cnt <= 0)
585 ifq->altq_subq_cnt = 1;
586 ifq->altq_subq = kmalloc_cachealign(
587 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
588 M_DEVBUF, M_WAITOK | M_ZERO);
590 if (ifq->altq_maxlen == 0) {
591 if_printf(ifp, "driver didn't set altq_maxlen\n");
592 ifq_set_maxlen(ifq, ifqmaxlen);
595 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
596 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
598 ALTQ_SQ_LOCK_INIT(ifsq);
599 ifsq->ifsq_index = q;
601 ifsq->ifsq_altq = ifq;
602 ifsq->ifsq_ifp = ifp;
604 ifsq->ifsq_maxlen = ifq->altq_maxlen;
605 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
606 ifsq->ifsq_prepended = NULL;
607 ifsq->ifsq_started = 0;
608 ifsq->ifsq_hw_oactive = 0;
609 ifsq_set_cpuid(ifsq, 0);
610 if (ifp->if_serializer != NULL)
611 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
614 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
615 M_DEVBUF, M_WAITOK | M_ZERO);
616 for (i = 0; i < ncpus; ++i)
617 ifsq->ifsq_stage[i].stg_subq = ifsq;
619 ifsq->ifsq_ifstart_nmsg =
620 kmalloc(ncpus * sizeof(struct netmsg_base),
621 M_LWKTMSG, M_WAITOK);
622 for (i = 0; i < ncpus; ++i) {
623 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
624 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
625 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
628 ifq_set_classic(ifq);
630 if (!SLIST_EMPTY(&domains))
631 if_attachdomain1(ifp);
633 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
634 lwkt_reltoken(&ifnet_token);
636 /* Announce the interface. */
637 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
638 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
639 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
641 mtx_unlock(&ifp->if_ioctl_mtx);
645 if_attachdomain(void *dummy)
650 TAILQ_FOREACH(ifp, &ifnet, if_list)
651 if_attachdomain1(ifp);
654 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
655 if_attachdomain, NULL);
658 if_attachdomain1(struct ifnet *ifp)
664 /* address family dependent data region */
665 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
666 SLIST_FOREACH(dp, &domains, dom_next)
667 if (dp->dom_ifattach)
668 ifp->if_afdata[dp->dom_family] =
669 (*dp->dom_ifattach)(ifp);
674 * Purge all addresses whose type is _not_ AF_LINK
677 if_purgeaddrs_nolink(struct ifnet *ifp)
679 struct ifaddr_container *ifac, *next;
681 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
683 struct ifaddr *ifa = ifac->ifa;
685 /* Leave link ifaddr as it is */
686 if (ifa->ifa_addr->sa_family == AF_LINK)
689 /* XXX: Ugly!! ad hoc just for INET */
690 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
691 struct ifaliasreq ifr;
692 #ifdef IFADDR_DEBUG_VERBOSE
695 kprintf("purge in4 addr %p: ", ifa);
696 for (i = 0; i < ncpus; ++i)
697 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
701 bzero(&ifr, sizeof ifr);
702 ifr.ifra_addr = *ifa->ifa_addr;
703 if (ifa->ifa_dstaddr)
704 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
705 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
711 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
712 #ifdef IFADDR_DEBUG_VERBOSE
715 kprintf("purge in6 addr %p: ", ifa);
716 for (i = 0; i < ncpus; ++i)
717 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
722 /* ifp_addrhead is already updated */
726 ifa_ifunlink(ifa, ifp);
732 ifq_stage_detach_handler(netmsg_t nmsg)
734 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
737 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
738 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
739 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
741 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
742 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
744 lwkt_replymsg(&nmsg->lmsg, 0);
748 ifq_stage_detach(struct ifaltq *ifq)
750 struct netmsg_base base;
753 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
754 ifq_stage_detach_handler);
755 base.lmsg.u.ms_resultp = ifq;
757 for (cpu = 0; cpu < ncpus; ++cpu)
758 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
761 struct netmsg_if_rtdel {
762 struct netmsg_base base;
767 if_rtdel_dispatch(netmsg_t msg)
769 struct netmsg_if_rtdel *rmsg = (void *)msg;
773 for (i = 1; i <= AF_MAX; i++) {
774 struct radix_node_head *rnh;
776 if ((rnh = rt_tables[cpu][i]) == NULL)
778 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
783 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
785 lwkt_replymsg(&rmsg->base.lmsg, 0);
789 * Detach an interface, removing it from the
790 * list of "active" interfaces.
793 if_detach(struct ifnet *ifp)
795 struct netmsg_if_rtdel msg;
799 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
802 * Remove routes and flush queues.
806 if (ifp->if_flags & IFF_NPOLLING)
807 ifpoll_deregister(ifp);
812 if (ifq_is_enabled(&ifp->if_snd))
813 altq_disable(&ifp->if_snd);
814 if (ifq_is_attached(&ifp->if_snd))
815 altq_detach(&ifp->if_snd);
819 * Clean up all addresses.
821 ifp->if_lladdr = NULL;
823 if_purgeaddrs_nolink(ifp);
824 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
827 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
828 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
829 ("non-link ifaddr is left on if_addrheads"));
831 ifa_ifunlink(ifa, ifp);
833 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
834 ("there are still ifaddrs left on if_addrheads"));
839 * Remove all IPv4 kernel structures related to ifp.
846 * Remove all IPv6 kernel structs related to ifp. This should be done
847 * before removing routing entries below, since IPv6 interface direct
848 * routes are expected to be removed by the IPv6-specific kernel API.
849 * Otherwise, the kernel will detect some inconsistency and bark it.
855 * Delete all remaining routes using this interface
857 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
860 rt_domsg_global(&msg.base);
862 /* Announce that the interface is gone. */
863 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
864 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
866 SLIST_FOREACH(dp, &domains, dom_next)
867 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
868 (*dp->dom_ifdetach)(ifp,
869 ifp->if_afdata[dp->dom_family]);
872 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
874 lwkt_gettoken(&ifnet_token);
875 ifindex2ifnet[ifp->if_index] = NULL;
876 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
878 TAILQ_REMOVE(&ifnet, ifp, if_link);
879 lwkt_reltoken(&ifnet_token);
881 kfree(ifp->if_addrheads, M_IFADDR);
883 lwkt_synchronize_ipiqs("if_detach");
884 ifq_stage_detach(&ifp->if_snd);
886 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
887 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
889 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
890 kfree(ifsq->ifsq_stage, M_DEVBUF);
892 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
894 kfree(ifp->if_data_pcpu, M_DEVBUF);
900 * Create interface group without members
903 if_creategroup(const char *groupname)
905 struct ifg_group *ifg = NULL;
907 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
908 M_TEMP, M_NOWAIT)) == NULL)
911 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
913 ifg->ifg_carp_demoted = 0;
914 TAILQ_INIT(&ifg->ifg_members);
916 pfi_attach_ifgroup(ifg);
918 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
924 * Add a group to an interface
927 if_addgroup(struct ifnet *ifp, const char *groupname)
929 struct ifg_list *ifgl;
930 struct ifg_group *ifg = NULL;
931 struct ifg_member *ifgm;
933 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
934 groupname[strlen(groupname) - 1] <= '9')
937 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
938 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
941 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
944 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
949 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
950 if (!strcmp(ifg->ifg_group, groupname))
953 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
960 ifgl->ifgl_group = ifg;
961 ifgm->ifgm_ifp = ifp;
963 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
964 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
967 pfi_group_change(groupname);
974 * Remove a group from an interface
977 if_delgroup(struct ifnet *ifp, const char *groupname)
979 struct ifg_list *ifgl;
980 struct ifg_member *ifgm;
982 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
983 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
988 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
990 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
991 if (ifgm->ifgm_ifp == ifp)
995 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
999 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1000 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1002 pfi_detach_ifgroup(ifgl->ifgl_group);
1004 kfree(ifgl->ifgl_group, M_TEMP);
1007 kfree(ifgl, M_TEMP);
1010 pfi_group_change(groupname);
1017 * Stores all groups from an interface in memory pointed
1021 if_getgroup(caddr_t data, struct ifnet *ifp)
1024 struct ifg_list *ifgl;
1025 struct ifg_req ifgrq, *ifgp;
1026 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1028 if (ifgr->ifgr_len == 0) {
1029 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1030 ifgr->ifgr_len += sizeof(struct ifg_req);
1034 len = ifgr->ifgr_len;
1035 ifgp = ifgr->ifgr_groups;
1036 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1037 if (len < sizeof(ifgrq))
1039 bzero(&ifgrq, sizeof ifgrq);
1040 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1041 sizeof(ifgrq.ifgrq_group));
1042 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1043 sizeof(struct ifg_req))))
1045 len -= sizeof(ifgrq);
1053 * Stores all members of a group in memory pointed to by data
1056 if_getgroupmembers(caddr_t data)
1058 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1059 struct ifg_group *ifg;
1060 struct ifg_member *ifgm;
1061 struct ifg_req ifgrq, *ifgp;
1064 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1065 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1070 if (ifgr->ifgr_len == 0) {
1071 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1072 ifgr->ifgr_len += sizeof(ifgrq);
1076 len = ifgr->ifgr_len;
1077 ifgp = ifgr->ifgr_groups;
1078 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1079 if (len < sizeof(ifgrq))
1081 bzero(&ifgrq, sizeof ifgrq);
1082 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1083 sizeof(ifgrq.ifgrq_member));
1084 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1085 sizeof(struct ifg_req))))
1087 len -= sizeof(ifgrq);
1095 * Delete Routes for a Network Interface
1097 * Called for each routing entry via the rnh->rnh_walktree() call above
1098 * to delete all route entries referencing a detaching network interface.
1101 * rn pointer to node in the routing table
1102 * arg argument passed to rnh->rnh_walktree() - detaching interface
1106 * errno failed - reason indicated
1110 if_rtdel(struct radix_node *rn, void *arg)
1112 struct rtentry *rt = (struct rtentry *)rn;
1113 struct ifnet *ifp = arg;
1116 if (rt->rt_ifp == ifp) {
1119 * Protect (sorta) against walktree recursion problems
1120 * with cloned routes
1122 if (!(rt->rt_flags & RTF_UP))
1125 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1126 rt_mask(rt), rt->rt_flags,
1129 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1137 * Locate an interface based on a complete address.
1140 ifa_ifwithaddr(struct sockaddr *addr)
1144 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1145 struct ifaddr_container *ifac;
1147 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1148 struct ifaddr *ifa = ifac->ifa;
1150 if (ifa->ifa_addr->sa_family != addr->sa_family)
1152 if (sa_equal(addr, ifa->ifa_addr))
1154 if ((ifp->if_flags & IFF_BROADCAST) &&
1155 ifa->ifa_broadaddr &&
1156 /* IPv6 doesn't have broadcast */
1157 ifa->ifa_broadaddr->sa_len != 0 &&
1158 sa_equal(ifa->ifa_broadaddr, addr))
1165 * Locate the point to point interface with a given destination address.
1168 ifa_ifwithdstaddr(struct sockaddr *addr)
1172 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1173 struct ifaddr_container *ifac;
1175 if (!(ifp->if_flags & IFF_POINTOPOINT))
1178 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1179 struct ifaddr *ifa = ifac->ifa;
1181 if (ifa->ifa_addr->sa_family != addr->sa_family)
1183 if (ifa->ifa_dstaddr &&
1184 sa_equal(addr, ifa->ifa_dstaddr))
1192 * Find an interface on a specific network. If many, choice
1193 * is most specific found.
1196 ifa_ifwithnet(struct sockaddr *addr)
1199 struct ifaddr *ifa_maybe = NULL;
1200 u_int af = addr->sa_family;
1201 char *addr_data = addr->sa_data, *cplim;
1204 * AF_LINK addresses can be looked up directly by their index number,
1205 * so do that if we can.
1207 if (af == AF_LINK) {
1208 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1210 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1211 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1215 * Scan though each interface, looking for ones that have
1216 * addresses in this address family.
1218 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1219 struct ifaddr_container *ifac;
1221 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1222 struct ifaddr *ifa = ifac->ifa;
1223 char *cp, *cp2, *cp3;
1225 if (ifa->ifa_addr->sa_family != af)
1227 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1229 * This is a bit broken as it doesn't
1230 * take into account that the remote end may
1231 * be a single node in the network we are
1233 * The trouble is that we don't know the
1234 * netmask for the remote end.
1236 if (ifa->ifa_dstaddr != NULL &&
1237 sa_equal(addr, ifa->ifa_dstaddr))
1241 * if we have a special address handler,
1242 * then use it instead of the generic one.
1244 if (ifa->ifa_claim_addr) {
1245 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1253 * Scan all the bits in the ifa's address.
1254 * If a bit dissagrees with what we are
1255 * looking for, mask it with the netmask
1256 * to see if it really matters.
1257 * (A byte at a time)
1259 if (ifa->ifa_netmask == 0)
1262 cp2 = ifa->ifa_addr->sa_data;
1263 cp3 = ifa->ifa_netmask->sa_data;
1264 cplim = ifa->ifa_netmask->sa_len +
1265 (char *)ifa->ifa_netmask;
1267 if ((*cp++ ^ *cp2++) & *cp3++)
1268 goto next; /* next address! */
1270 * If the netmask of what we just found
1271 * is more specific than what we had before
1272 * (if we had one) then remember the new one
1273 * before continuing to search
1274 * for an even better one.
1276 if (ifa_maybe == NULL ||
1277 rn_refines((char *)ifa->ifa_netmask,
1278 (char *)ifa_maybe->ifa_netmask))
1287 * Find an interface address specific to an interface best matching
1291 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1293 struct ifaddr_container *ifac;
1294 char *cp, *cp2, *cp3;
1296 struct ifaddr *ifa_maybe = NULL;
1297 u_int af = addr->sa_family;
1301 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1302 struct ifaddr *ifa = ifac->ifa;
1304 if (ifa->ifa_addr->sa_family != af)
1306 if (ifa_maybe == NULL)
1308 if (ifa->ifa_netmask == NULL) {
1309 if (sa_equal(addr, ifa->ifa_addr) ||
1310 (ifa->ifa_dstaddr != NULL &&
1311 sa_equal(addr, ifa->ifa_dstaddr)))
1315 if (ifp->if_flags & IFF_POINTOPOINT) {
1316 if (sa_equal(addr, ifa->ifa_dstaddr))
1320 cp2 = ifa->ifa_addr->sa_data;
1321 cp3 = ifa->ifa_netmask->sa_data;
1322 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1323 for (; cp3 < cplim; cp3++)
1324 if ((*cp++ ^ *cp2++) & *cp3)
1334 * Default action when installing a route with a Link Level gateway.
1335 * Lookup an appropriate real ifa to point to.
1336 * This should be moved to /sys/net/link.c eventually.
1339 link_rtrequest(int cmd, struct rtentry *rt)
1342 struct sockaddr *dst;
1345 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1346 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1348 ifa = ifaof_ifpforaddr(dst, ifp);
1350 IFAFREE(rt->rt_ifa);
1353 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1354 ifa->ifa_rtrequest(cmd, rt);
1359 * Mark an interface down and notify protocols of
1361 * NOTE: must be called at splnet or eqivalent.
1364 if_unroute(struct ifnet *ifp, int flag, int fam)
1366 struct ifaddr_container *ifac;
1368 ifp->if_flags &= ~flag;
1369 getmicrotime(&ifp->if_lastchange);
1370 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1371 struct ifaddr *ifa = ifac->ifa;
1373 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1374 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1376 ifq_purge_all(&ifp->if_snd);
1381 * Mark an interface up and notify protocols of
1383 * NOTE: must be called at splnet or eqivalent.
1386 if_route(struct ifnet *ifp, int flag, int fam)
1388 struct ifaddr_container *ifac;
1390 ifq_purge_all(&ifp->if_snd);
1391 ifp->if_flags |= flag;
1392 getmicrotime(&ifp->if_lastchange);
1393 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1394 struct ifaddr *ifa = ifac->ifa;
1396 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1397 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1406 * Mark an interface down and notify protocols of the transition. An
1407 * interface going down is also considered to be a synchronizing event.
1408 * We must ensure that all packet processing related to the interface
1409 * has completed before we return so e.g. the caller can free the ifnet
1410 * structure that the mbufs may be referencing.
1412 * NOTE: must be called at splnet or eqivalent.
1415 if_down(struct ifnet *ifp)
1417 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1418 netmsg_service_sync();
1422 * Mark an interface up and notify protocols of
1424 * NOTE: must be called at splnet or eqivalent.
1427 if_up(struct ifnet *ifp)
1429 if_route(ifp, IFF_UP, AF_UNSPEC);
1433 * Process a link state change.
1434 * NOTE: must be called at splsoftnet or equivalent.
1437 if_link_state_change(struct ifnet *ifp)
1439 int link_state = ifp->if_link_state;
1442 devctl_notify("IFNET", ifp->if_xname,
1443 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1447 * Handle interface watchdog timer routines. Called
1448 * from softclock, we decrement timers (if set) and
1449 * call the appropriate interface routine on expiration.
1452 if_slowtimo(void *arg)
1458 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1459 if (if_stats_compat) {
1460 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1461 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1462 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1463 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1464 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1465 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1466 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1467 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1468 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1469 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1470 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1473 if (ifp->if_timer == 0 || --ifp->if_timer)
1475 if (ifp->if_watchdog) {
1476 if (ifnet_tryserialize_all(ifp)) {
1477 (*ifp->if_watchdog)(ifp);
1478 ifnet_deserialize_all(ifp);
1480 /* try again next timeout */
1488 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1492 * Map interface name to
1493 * interface structure pointer.
1496 ifunit(const char *name)
1501 * Search all the interfaces for this name/number
1504 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1505 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1513 * Map interface name in a sockaddr_dl to
1514 * interface structure pointer.
1517 if_withname(struct sockaddr *sa)
1519 char ifname[IFNAMSIZ+1];
1520 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1522 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1523 (sdl->sdl_nlen > IFNAMSIZ) )
1527 * ifunit wants a null-terminated name. It may not be null-terminated
1528 * in the sockaddr. We don't want to change the caller's sockaddr,
1529 * and there might not be room to put the trailing null anyway, so we
1530 * make a local copy that we know we can null terminate safely.
1533 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1534 ifname[sdl->sdl_nlen] = '\0';
1535 return ifunit(ifname);
1543 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1554 size_t namelen, onamelen;
1555 char new_name[IFNAMSIZ];
1557 struct sockaddr_dl *sdl;
1562 return (ifconf(cmd, data, cred));
1567 ifr = (struct ifreq *)data;
1572 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1574 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1575 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1577 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1579 return (if_clone_destroy(ifr->ifr_name));
1580 case SIOCIFGCLONERS:
1581 return (if_clone_list((struct if_clonereq *)data));
1587 * Nominal ioctl through interface, lookup the ifp and obtain a
1588 * lock to serialize the ifconfig ioctl operation.
1590 ifp = ifunit(ifr->ifr_name);
1594 mtx_lock(&ifp->if_ioctl_mtx);
1598 ifr->ifr_index = ifp->if_index;
1602 ifr->ifr_flags = ifp->if_flags;
1603 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1607 ifr->ifr_reqcap = ifp->if_capabilities;
1608 ifr->ifr_curcap = ifp->if_capenable;
1612 ifr->ifr_metric = ifp->if_metric;
1616 ifr->ifr_mtu = ifp->if_mtu;
1620 ifr->ifr_tsolen = ifp->if_tsolen;
1624 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1625 sizeof(ifp->if_data));
1629 ifr->ifr_phys = ifp->if_physical;
1632 case SIOCGIFPOLLCPU:
1633 ifr->ifr_pollcpu = -1;
1636 case SIOCSIFPOLLCPU:
1640 error = priv_check_cred(cred, PRIV_ROOT, 0);
1643 new_flags = (ifr->ifr_flags & 0xffff) |
1644 (ifr->ifr_flagshigh << 16);
1645 if (ifp->if_flags & IFF_SMART) {
1646 /* Smart drivers twiddle their own routes */
1647 } else if (ifp->if_flags & IFF_UP &&
1648 (new_flags & IFF_UP) == 0) {
1652 } else if (new_flags & IFF_UP &&
1653 (ifp->if_flags & IFF_UP) == 0) {
1659 #ifdef IFPOLL_ENABLE
1660 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1661 if (new_flags & IFF_NPOLLING)
1662 ifpoll_register(ifp);
1664 ifpoll_deregister(ifp);
1668 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1669 (new_flags &~ IFF_CANTCHANGE);
1670 if (new_flags & IFF_PPROMISC) {
1671 /* Permanently promiscuous mode requested */
1672 ifp->if_flags |= IFF_PROMISC;
1673 } else if (ifp->if_pcount == 0) {
1674 ifp->if_flags &= ~IFF_PROMISC;
1676 if (ifp->if_ioctl) {
1677 ifnet_serialize_all(ifp);
1678 ifp->if_ioctl(ifp, cmd, data, cred);
1679 ifnet_deserialize_all(ifp);
1681 getmicrotime(&ifp->if_lastchange);
1685 error = priv_check_cred(cred, PRIV_ROOT, 0);
1688 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1692 ifnet_serialize_all(ifp);
1693 ifp->if_ioctl(ifp, cmd, data, cred);
1694 ifnet_deserialize_all(ifp);
1698 error = priv_check_cred(cred, PRIV_ROOT, 0);
1701 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1704 if (new_name[0] == '\0') {
1708 if (ifunit(new_name) != NULL) {
1713 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1715 /* Announce the departure of the interface. */
1716 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1718 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1719 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1720 /* XXX IFA_LOCK(ifa); */
1721 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1722 namelen = strlen(new_name);
1723 onamelen = sdl->sdl_nlen;
1725 * Move the address if needed. This is safe because we
1726 * allocate space for a name of length IFNAMSIZ when we
1727 * create this in if_attach().
1729 if (namelen != onamelen) {
1730 bcopy(sdl->sdl_data + onamelen,
1731 sdl->sdl_data + namelen, sdl->sdl_alen);
1733 bcopy(new_name, sdl->sdl_data, namelen);
1734 sdl->sdl_nlen = namelen;
1735 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1736 bzero(sdl->sdl_data, onamelen);
1737 while (namelen != 0)
1738 sdl->sdl_data[--namelen] = 0xff;
1739 /* XXX IFA_UNLOCK(ifa) */
1741 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1743 /* Announce the return of the interface. */
1744 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1748 error = priv_check_cred(cred, PRIV_ROOT, 0);
1751 ifp->if_metric = ifr->ifr_metric;
1752 getmicrotime(&ifp->if_lastchange);
1756 error = priv_check_cred(cred, PRIV_ROOT, 0);
1759 if (ifp->if_ioctl == NULL) {
1763 ifnet_serialize_all(ifp);
1764 error = ifp->if_ioctl(ifp, cmd, data, cred);
1765 ifnet_deserialize_all(ifp);
1767 getmicrotime(&ifp->if_lastchange);
1772 u_long oldmtu = ifp->if_mtu;
1774 error = priv_check_cred(cred, PRIV_ROOT, 0);
1777 if (ifp->if_ioctl == NULL) {
1781 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
1785 ifnet_serialize_all(ifp);
1786 error = ifp->if_ioctl(ifp, cmd, data, cred);
1787 ifnet_deserialize_all(ifp);
1789 getmicrotime(&ifp->if_lastchange);
1793 * If the link MTU changed, do network layer specific procedure.
1795 if (ifp->if_mtu != oldmtu) {
1804 error = priv_check_cred(cred, PRIV_ROOT, 0);
1808 /* XXX need driver supplied upper limit */
1809 if (ifr->ifr_tsolen <= 0) {
1813 ifp->if_tsolen = ifr->ifr_tsolen;
1818 error = priv_check_cred(cred, PRIV_ROOT, 0);
1822 /* Don't allow group membership on non-multicast interfaces. */
1823 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1828 /* Don't let users screw up protocols' entries. */
1829 if (ifr->ifr_addr.sa_family != AF_LINK) {
1834 if (cmd == SIOCADDMULTI) {
1835 struct ifmultiaddr *ifma;
1836 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1838 error = if_delmulti(ifp, &ifr->ifr_addr);
1841 getmicrotime(&ifp->if_lastchange);
1844 case SIOCSIFPHYADDR:
1845 case SIOCDIFPHYADDR:
1847 case SIOCSIFPHYADDR_IN6:
1849 case SIOCSLIFPHYADDR:
1851 case SIOCSIFGENERIC:
1852 error = priv_check_cred(cred, PRIV_ROOT, 0);
1855 if (ifp->if_ioctl == 0) {
1859 ifnet_serialize_all(ifp);
1860 error = ifp->if_ioctl(ifp, cmd, data, cred);
1861 ifnet_deserialize_all(ifp);
1863 getmicrotime(&ifp->if_lastchange);
1867 ifs = (struct ifstat *)data;
1868 ifs->ascii[0] = '\0';
1870 case SIOCGIFPSRCADDR:
1871 case SIOCGIFPDSTADDR:
1872 case SIOCGLIFPHYADDR:
1874 case SIOCGIFGENERIC:
1875 if (ifp->if_ioctl == NULL) {
1879 ifnet_serialize_all(ifp);
1880 error = ifp->if_ioctl(ifp, cmd, data, cred);
1881 ifnet_deserialize_all(ifp);
1885 error = priv_check_cred(cred, PRIV_ROOT, 0);
1888 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
1889 ifr->ifr_addr.sa_len);
1890 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1894 oif_flags = ifp->if_flags;
1895 if (so->so_proto == 0) {
1900 error = so_pru_control_direct(so, cmd, data, ifp);
1905 case SIOCSIFDSTADDR:
1907 case SIOCSIFBRDADDR:
1908 case SIOCSIFNETMASK:
1909 #if BYTE_ORDER != BIG_ENDIAN
1910 if (ifr->ifr_addr.sa_family == 0 &&
1911 ifr->ifr_addr.sa_len < 16) {
1912 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1913 ifr->ifr_addr.sa_len = 16;
1916 if (ifr->ifr_addr.sa_len == 0)
1917 ifr->ifr_addr.sa_len = 16;
1923 case OSIOCGIFDSTADDR:
1924 cmd = SIOCGIFDSTADDR;
1926 case OSIOCGIFBRDADDR:
1927 cmd = SIOCGIFBRDADDR;
1929 case OSIOCGIFNETMASK:
1930 cmd = SIOCGIFNETMASK;
1936 error = so_pru_control_direct(so, cmd, data, ifp);
1940 case OSIOCGIFDSTADDR:
1941 case OSIOCGIFBRDADDR:
1942 case OSIOCGIFNETMASK:
1943 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1946 #endif /* COMPAT_43 */
1948 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1950 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1951 if (ifp->if_flags & IFF_UP) {
1961 mtx_unlock(&ifp->if_ioctl_mtx);
1966 * Set/clear promiscuous mode on interface ifp based on the truth value
1967 * of pswitch. The calls are reference counted so that only the first
1968 * "on" request actually has an effect, as does the final "off" request.
1969 * Results are undefined if the "off" and "on" requests are not matched.
1972 ifpromisc(struct ifnet *ifp, int pswitch)
1978 oldflags = ifp->if_flags;
1979 if (ifp->if_flags & IFF_PPROMISC) {
1980 /* Do nothing if device is in permanently promiscuous mode */
1981 ifp->if_pcount += pswitch ? 1 : -1;
1986 * If the device is not configured up, we cannot put it in
1989 if ((ifp->if_flags & IFF_UP) == 0)
1991 if (ifp->if_pcount++ != 0)
1993 ifp->if_flags |= IFF_PROMISC;
1994 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1997 if (--ifp->if_pcount > 0)
1999 ifp->if_flags &= ~IFF_PROMISC;
2000 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2003 ifr.ifr_flags = ifp->if_flags;
2004 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2005 ifnet_serialize_all(ifp);
2006 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2007 ifnet_deserialize_all(ifp);
2011 ifp->if_flags = oldflags;
2016 * Return interface configuration
2017 * of system. List may be used
2018 * in later ioctl's (above) to get
2019 * other information.
2022 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2024 struct ifconf *ifc = (struct ifconf *)data;
2026 struct sockaddr *sa;
2027 struct ifreq ifr, *ifrp;
2028 int space = ifc->ifc_len, error = 0;
2030 ifrp = ifc->ifc_req;
2031 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2032 struct ifaddr_container *ifac;
2035 if (space <= sizeof ifr)
2039 * Zero the stack declared structure first to prevent
2040 * memory disclosure.
2042 bzero(&ifr, sizeof(ifr));
2043 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2044 >= sizeof(ifr.ifr_name)) {
2045 error = ENAMETOOLONG;
2050 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2051 struct ifaddr *ifa = ifac->ifa;
2053 if (space <= sizeof ifr)
2056 if (cred->cr_prison &&
2057 prison_if(cred, sa))
2061 if (cmd == OSIOCGIFCONF) {
2062 struct osockaddr *osa =
2063 (struct osockaddr *)&ifr.ifr_addr;
2065 osa->sa_family = sa->sa_family;
2066 error = copyout(&ifr, ifrp, sizeof ifr);
2070 if (sa->sa_len <= sizeof(*sa)) {
2072 error = copyout(&ifr, ifrp, sizeof ifr);
2075 if (space < (sizeof ifr) + sa->sa_len -
2078 space -= sa->sa_len - sizeof(*sa);
2079 error = copyout(&ifr, ifrp,
2080 sizeof ifr.ifr_name);
2082 error = copyout(sa, &ifrp->ifr_addr,
2084 ifrp = (struct ifreq *)
2085 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2089 space -= sizeof ifr;
2094 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2095 error = copyout(&ifr, ifrp, sizeof ifr);
2098 space -= sizeof ifr;
2102 ifc->ifc_len -= space;
2107 * Just like if_promisc(), but for all-multicast-reception mode.
2110 if_allmulti(struct ifnet *ifp, int onswitch)
2118 if (ifp->if_amcount++ == 0) {
2119 ifp->if_flags |= IFF_ALLMULTI;
2120 ifr.ifr_flags = ifp->if_flags;
2121 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2122 ifnet_serialize_all(ifp);
2123 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2125 ifnet_deserialize_all(ifp);
2128 if (ifp->if_amcount > 1) {
2131 ifp->if_amcount = 0;
2132 ifp->if_flags &= ~IFF_ALLMULTI;
2133 ifr.ifr_flags = ifp->if_flags;
2134 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2135 ifnet_serialize_all(ifp);
2136 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2138 ifnet_deserialize_all(ifp);
2150 * Add a multicast listenership to the interface in question.
2151 * The link layer provides a routine which converts
2154 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2155 struct ifmultiaddr **retifma)
2157 struct sockaddr *llsa, *dupsa;
2159 struct ifmultiaddr *ifma;
2161 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2164 * If the matching multicast address already exists
2165 * then don't add a new one, just add a reference
2167 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2168 if (sa_equal(sa, ifma->ifma_addr)) {
2169 ifma->ifma_refcount++;
2177 * Give the link layer a chance to accept/reject it, and also
2178 * find out which AF_LINK address this maps to, if it isn't one
2181 if (ifp->if_resolvemulti) {
2182 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2189 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2190 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_WAITOK);
2191 bcopy(sa, dupsa, sa->sa_len);
2193 ifma->ifma_addr = dupsa;
2194 ifma->ifma_lladdr = llsa;
2195 ifma->ifma_ifp = ifp;
2196 ifma->ifma_refcount = 1;
2197 ifma->ifma_protospec = NULL;
2198 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2200 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2205 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2206 if (sa_equal(ifma->ifma_addr, llsa))
2210 ifma->ifma_refcount++;
2212 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2213 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_WAITOK);
2214 bcopy(llsa, dupsa, llsa->sa_len);
2215 ifma->ifma_addr = dupsa;
2216 ifma->ifma_ifp = ifp;
2217 ifma->ifma_refcount = 1;
2218 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2222 * We are certain we have added something, so call down to the
2223 * interface to let them know about it.
2226 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2232 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2233 struct ifmultiaddr **retifma)
2237 ifnet_serialize_all(ifp);
2238 error = if_addmulti_serialized(ifp, sa, retifma);
2239 ifnet_deserialize_all(ifp);
2245 * Remove a reference to a multicast address on this interface. Yell
2246 * if the request does not match an existing membership.
2249 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2251 struct ifmultiaddr *ifma;
2253 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2255 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2256 if (sa_equal(sa, ifma->ifma_addr))
2261 if (ifma->ifma_refcount > 1) {
2262 ifma->ifma_refcount--;
2266 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2267 sa = ifma->ifma_lladdr;
2268 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2270 * Make sure the interface driver is notified
2271 * in the case of a link layer mcast group being left.
2273 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2274 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2275 kfree(ifma->ifma_addr, M_IFMADDR);
2276 kfree(ifma, M_IFMADDR);
2281 * Now look for the link-layer address which corresponds to
2282 * this network address. It had been squirreled away in
2283 * ifma->ifma_lladdr for this purpose (so we don't have
2284 * to call ifp->if_resolvemulti() again), and we saved that
2285 * value in sa above. If some nasty deleted the
2286 * link-layer address out from underneath us, we can deal because
2287 * the address we stored was is not the same as the one which was
2288 * in the record for the link-layer address. (So we don't complain
2291 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2292 if (sa_equal(sa, ifma->ifma_addr))
2297 if (ifma->ifma_refcount > 1) {
2298 ifma->ifma_refcount--;
2302 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2303 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2304 kfree(ifma->ifma_addr, M_IFMADDR);
2305 kfree(sa, M_IFMADDR);
2306 kfree(ifma, M_IFMADDR);
2312 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2316 ifnet_serialize_all(ifp);
2317 error = if_delmulti_serialized(ifp, sa);
2318 ifnet_deserialize_all(ifp);
2324 * Delete all multicast group membership for an interface.
2325 * Should be used to quickly flush all multicast filters.
2328 if_delallmulti_serialized(struct ifnet *ifp)
2330 struct ifmultiaddr *ifma, mark;
2333 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2335 bzero(&sa, sizeof(sa));
2336 sa.sa_family = AF_UNSPEC;
2337 sa.sa_len = sizeof(sa);
2339 bzero(&mark, sizeof(mark));
2340 mark.ifma_addr = &sa;
2342 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2343 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2344 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2345 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2348 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2351 if_delmulti_serialized(ifp, ifma->ifma_addr);
2353 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2358 * Set the link layer address on an interface.
2360 * At this time we only support certain types of interfaces,
2361 * and we don't allow the length of the address to change.
2364 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2366 struct sockaddr_dl *sdl;
2369 sdl = IF_LLSOCKADDR(ifp);
2372 if (len != sdl->sdl_alen) /* don't allow length to change */
2374 switch (ifp->if_type) {
2375 case IFT_ETHER: /* these types use struct arpcom */
2378 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2379 bcopy(lladdr, LLADDR(sdl), len);
2385 * If the interface is already up, we need
2386 * to re-init it in order to reprogram its
2389 ifnet_serialize_all(ifp);
2390 if ((ifp->if_flags & IFF_UP) != 0) {
2392 struct ifaddr_container *ifac;
2395 ifp->if_flags &= ~IFF_UP;
2396 ifr.ifr_flags = ifp->if_flags;
2397 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2398 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2400 ifp->if_flags |= IFF_UP;
2401 ifr.ifr_flags = ifp->if_flags;
2402 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2403 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2407 * Also send gratuitous ARPs to notify other nodes about
2408 * the address change.
2410 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2411 struct ifaddr *ifa = ifac->ifa;
2413 if (ifa->ifa_addr != NULL &&
2414 ifa->ifa_addr->sa_family == AF_INET)
2415 arp_gratuitous(ifp, ifa);
2419 ifnet_deserialize_all(ifp);
2423 struct ifmultiaddr *
2424 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2426 struct ifmultiaddr *ifma;
2428 /* TODO: need ifnet_serialize_main */
2429 ifnet_serialize_all(ifp);
2430 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2431 if (sa_equal(ifma->ifma_addr, sa))
2433 ifnet_deserialize_all(ifp);
2439 * This function locates the first real ethernet MAC from a network
2440 * card and loads it into node, returning 0 on success or ENOENT if
2441 * no suitable interfaces were found. It is used by the uuid code to
2442 * generate a unique 6-byte number.
2445 if_getanyethermac(uint16_t *node, int minlen)
2448 struct sockaddr_dl *sdl;
2450 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2451 if (ifp->if_type != IFT_ETHER)
2453 sdl = IF_LLSOCKADDR(ifp);
2454 if (sdl->sdl_alen < minlen)
2456 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2464 * The name argument must be a pointer to storage which will last as
2465 * long as the interface does. For physical devices, the result of
2466 * device_get_name(dev) is a good choice and for pseudo-devices a
2467 * static string works well.
2470 if_initname(struct ifnet *ifp, const char *name, int unit)
2472 ifp->if_dname = name;
2473 ifp->if_dunit = unit;
2474 if (unit != IF_DUNIT_NONE)
2475 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2477 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2481 if_printf(struct ifnet *ifp, const char *fmt, ...)
2486 retval = kprintf("%s: ", ifp->if_xname);
2487 __va_start(ap, fmt);
2488 retval += kvprintf(fmt, ap);
2494 if_alloc(uint8_t type)
2500 * XXX temporary hack until arpcom is setup in if_l2com
2502 if (type == IFT_ETHER)
2503 size = sizeof(struct arpcom);
2505 size = sizeof(struct ifnet);
2507 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2509 ifp->if_type = type;
2511 if (if_com_alloc[type] != NULL) {
2512 ifp->if_l2com = if_com_alloc[type](type, ifp);
2513 if (ifp->if_l2com == NULL) {
2514 kfree(ifp, M_IFNET);
2522 if_free(struct ifnet *ifp)
2524 kfree(ifp, M_IFNET);
2528 ifq_set_classic(struct ifaltq *ifq)
2530 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2531 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2535 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2536 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2540 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2541 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2542 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2543 KASSERT(request != NULL, ("request is not specified"));
2545 ifq->altq_mapsubq = mapsubq;
2546 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2547 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2549 ifsq->ifsq_enqueue = enqueue;
2550 ifsq->ifsq_dequeue = dequeue;
2551 ifsq->ifsq_request = request;
2556 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2558 m->m_nextpkt = NULL;
2559 if (ifsq->ifsq_norm_tail == NULL)
2560 ifsq->ifsq_norm_head = m;
2562 ifsq->ifsq_norm_tail->m_nextpkt = m;
2563 ifsq->ifsq_norm_tail = m;
2564 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2568 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2570 m->m_nextpkt = NULL;
2571 if (ifsq->ifsq_prio_tail == NULL)
2572 ifsq->ifsq_prio_head = m;
2574 ifsq->ifsq_prio_tail->m_nextpkt = m;
2575 ifsq->ifsq_prio_tail = m;
2576 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2577 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2580 static struct mbuf *
2581 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2585 m = ifsq->ifsq_norm_head;
2587 if ((ifsq->ifsq_norm_head = m->m_nextpkt) == NULL)
2588 ifsq->ifsq_norm_tail = NULL;
2589 m->m_nextpkt = NULL;
2590 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2595 static struct mbuf *
2596 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2600 m = ifsq->ifsq_prio_head;
2602 if ((ifsq->ifsq_prio_head = m->m_nextpkt) == NULL)
2603 ifsq->ifsq_prio_tail = NULL;
2604 m->m_nextpkt = NULL;
2605 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2606 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2612 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2613 struct altq_pktattr *pa __unused)
2616 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2617 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
2618 if ((m->m_flags & M_PRIO) &&
2619 ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen / 2) &&
2620 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt / 2)) {
2621 struct mbuf *m_drop;
2624 * Perform drop-head on normal queue
2626 m_drop = ifsq_norm_dequeue(ifsq);
2627 if (m_drop != NULL) {
2629 ifsq_prio_enqueue(ifsq, m);
2632 /* XXX nothing could be dropped? */
2637 if (m->m_flags & M_PRIO)
2638 ifsq_prio_enqueue(ifsq, m);
2640 ifsq_norm_enqueue(ifsq, m);
2646 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
2652 m = ifsq->ifsq_prio_head;
2654 m = ifsq->ifsq_norm_head;
2658 m = ifsq_prio_dequeue(ifsq);
2660 m = ifsq_norm_dequeue(ifsq);
2664 panic("unsupported ALTQ dequeue op: %d", op);
2670 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2677 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
2685 panic("unsupported ALTQ request: %d", req);
2691 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2693 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2694 int running = 0, need_sched;
2697 * Try to do direct ifnet.if_start on the subqueue first, if there is
2698 * contention on the subqueue hardware serializer, ifnet.if_start on
2699 * the subqueue will be scheduled on the subqueue owner CPU.
2701 if (!ifsq_tryserialize_hw(ifsq)) {
2703 * Subqueue hardware serializer contention happened,
2704 * ifnet.if_start on the subqueue is scheduled on
2705 * the subqueue owner CPU, and we keep going.
2707 ifsq_ifstart_schedule(ifsq, 1);
2711 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2712 ifp->if_start(ifp, ifsq);
2713 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2716 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2718 ifsq_deserialize_hw(ifsq);
2722 * More data need to be transmitted, ifnet.if_start on the
2723 * subqueue is scheduled on the subqueue owner CPU, and we
2725 * NOTE: ifnet.if_start subqueue interlock is not released.
2727 ifsq_ifstart_schedule(ifsq, force_sched);
2732 * Subqeue packets staging mechanism:
2734 * The packets enqueued into the subqueue are staged to a certain amount
2735 * before the ifnet.if_start on the subqueue is called. In this way, the
2736 * driver could avoid writing to hardware registers upon every packet,
2737 * instead, hardware registers could be written when certain amount of
2738 * packets are put onto hardware TX ring. The measurement on several modern
2739 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2740 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2741 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2742 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2743 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2745 * Subqueue packets staging is performed for two entry points into drivers'
2746 * transmission function:
2747 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2748 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2750 * Subqueue packets staging will be stopped upon any of the following
2752 * - If the count of packets enqueued on the current CPU is great than or
2753 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2754 * - If the total length of packets enqueued on the current CPU is great
2755 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2756 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2757 * is usually less than hardware's MTU.
2758 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2759 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2761 * - The if_start_rollup(), which is registered as low priority netisr
2762 * rollup function, is called; probably because no more work is pending
2766 * Currently subqueue packet staging is only performed in netisr threads.
2769 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2771 struct ifaltq *ifq = &ifp->if_snd;
2772 struct ifaltq_subque *ifsq;
2773 int error, start = 0, len, mcast = 0, avoid_start = 0;
2774 struct ifsubq_stage_head *head = NULL;
2775 struct ifsubq_stage *stage = NULL;
2776 struct globaldata *gd = mycpu;
2777 struct thread *td = gd->gd_curthread;
2779 crit_enter_quick(td);
2781 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
2782 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
2784 len = m->m_pkthdr.len;
2785 if (m->m_flags & M_MCAST)
2788 if (td->td_type == TD_TYPE_NETISR) {
2789 head = &ifsubq_stage_heads[mycpuid];
2790 stage = ifsq_get_stage(ifsq, mycpuid);
2793 stage->stg_len += len;
2794 if (stage->stg_cnt < ifsq_stage_cntmax &&
2795 stage->stg_len < (ifp->if_mtu - max_protohdr))
2800 error = ifsq_enqueue_locked(ifsq, m, pa);
2802 if (!ifsq_data_ready(ifsq)) {
2803 ALTQ_SQ_UNLOCK(ifsq);
2804 crit_exit_quick(td);
2809 if (!ifsq_is_started(ifsq)) {
2811 ALTQ_SQ_UNLOCK(ifsq);
2814 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
2815 ifsq_stage_insert(head, stage);
2817 IFNET_STAT_INC(ifp, obytes, len);
2819 IFNET_STAT_INC(ifp, omcasts, 1);
2820 crit_exit_quick(td);
2825 * Hold the subqueue interlock of ifnet.if_start
2827 ifsq_set_started(ifsq);
2830 ALTQ_SQ_UNLOCK(ifsq);
2833 IFNET_STAT_INC(ifp, obytes, len);
2835 IFNET_STAT_INC(ifp, omcasts, 1);
2838 if (stage != NULL) {
2839 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
2840 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
2842 ifsq_stage_remove(head, stage);
2843 ifsq_ifstart_schedule(ifsq, 1);
2845 crit_exit_quick(td);
2849 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
2850 ifsq_stage_remove(head, stage);
2858 crit_exit_quick(td);
2862 ifsq_ifstart_try(ifsq, 0);
2864 crit_exit_quick(td);
2869 ifa_create(int size, int flags)
2874 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
2876 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2880 ifa->ifa_containers =
2881 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
2882 M_IFADDR, M_WAITOK | M_ZERO);
2883 ifa->ifa_ncnt = ncpus;
2884 for (i = 0; i < ncpus; ++i) {
2885 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2887 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2889 ifac->ifa_refcnt = 1;
2892 kprintf("alloc ifa %p %d\n", ifa, size);
2898 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2900 struct ifaddr *ifa = ifac->ifa;
2902 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2903 KKASSERT(ifac->ifa_refcnt == 0);
2904 KASSERT(ifac->ifa_listmask == 0,
2905 ("ifa is still on %#x lists", ifac->ifa_listmask));
2907 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2909 #ifdef IFADDR_DEBUG_VERBOSE
2910 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2913 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2914 ("invalid # of ifac, %d", ifa->ifa_ncnt));
2915 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2917 kprintf("free ifa %p\n", ifa);
2919 kfree(ifa->ifa_containers, M_IFADDR);
2920 kfree(ifa, M_IFADDR);
2925 ifa_iflink_dispatch(netmsg_t nmsg)
2927 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2928 struct ifaddr *ifa = msg->ifa;
2929 struct ifnet *ifp = msg->ifp;
2931 struct ifaddr_container *ifac;
2935 ifac = &ifa->ifa_containers[cpu];
2936 ASSERT_IFAC_VALID(ifac);
2937 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2938 ("ifaddr is on if_addrheads"));
2940 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2942 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2944 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2948 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2952 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2954 struct netmsg_ifaddr msg;
2956 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2957 0, ifa_iflink_dispatch);
2962 ifa_domsg(&msg.base.lmsg, 0);
2966 ifa_ifunlink_dispatch(netmsg_t nmsg)
2968 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2969 struct ifaddr *ifa = msg->ifa;
2970 struct ifnet *ifp = msg->ifp;
2972 struct ifaddr_container *ifac;
2976 ifac = &ifa->ifa_containers[cpu];
2977 ASSERT_IFAC_VALID(ifac);
2978 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2979 ("ifaddr is not on if_addrhead"));
2981 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2982 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2986 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2990 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2992 struct netmsg_ifaddr msg;
2994 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2995 0, ifa_ifunlink_dispatch);
2999 ifa_domsg(&msg.base.lmsg, 0);
3003 ifa_destroy_dispatch(netmsg_t nmsg)
3005 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3008 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
3012 ifa_destroy(struct ifaddr *ifa)
3014 struct netmsg_ifaddr msg;
3016 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3017 0, ifa_destroy_dispatch);
3020 ifa_domsg(&msg.base.lmsg, 0);
3024 ifnet_portfn(int cpu)
3026 return &ifnet_threads[cpu].td_msgport;
3030 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
3032 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
3034 if (next_cpu < ncpus)
3035 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
3037 lwkt_replymsg(lmsg, 0);
3041 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
3043 KKASSERT(cpu < ncpus);
3044 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
3048 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
3050 KKASSERT(cpu < ncpus);
3051 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
3055 * Generic netmsg service loop. Some protocols may roll their own but all
3056 * must do the basic command dispatch function call done here.
3059 ifnet_service_loop(void *arg __unused)
3063 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
3064 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
3065 msg->base.nm_dispatch(msg);
3070 if_start_rollup(void)
3072 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3073 struct ifsubq_stage *stage;
3077 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3078 struct ifaltq_subque *ifsq = stage->stg_subq;
3081 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3083 ifsq_stage_remove(head, stage);
3086 ifsq_ifstart_schedule(ifsq, 1);
3091 if (!ifsq_is_started(ifsq)) {
3093 * Hold the subqueue interlock of
3096 ifsq_set_started(ifsq);
3099 ALTQ_SQ_UNLOCK(ifsq);
3102 ifsq_ifstart_try(ifsq, 1);
3104 KKASSERT((stage->stg_flags &
3105 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3112 ifnetinit(void *dummy __unused)
3116 for (i = 0; i < ncpus; ++i) {
3117 struct thread *thr = &ifnet_threads[i];
3119 lwkt_create(ifnet_service_loop, NULL, NULL,
3120 thr, TDF_NOSTART|TDF_FORCE_SPINPORT|TDF_FIXEDCPU,
3122 netmsg_service_port_init(&thr->td_msgport);
3126 for (i = 0; i < ncpus; ++i)
3127 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3128 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3132 ifnet_byindex(unsigned short idx)
3136 return ifindex2ifnet[idx];
3140 ifaddr_byindex(unsigned short idx)
3144 ifp = ifnet_byindex(idx);
3147 return TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
3151 if_register_com_alloc(u_char type,
3152 if_com_alloc_t *a, if_com_free_t *f)
3155 KASSERT(if_com_alloc[type] == NULL,
3156 ("if_register_com_alloc: %d already registered", type));
3157 KASSERT(if_com_free[type] == NULL,
3158 ("if_register_com_alloc: %d free already registered", type));
3160 if_com_alloc[type] = a;
3161 if_com_free[type] = f;
3165 if_deregister_com_alloc(u_char type)
3168 KASSERT(if_com_alloc[type] != NULL,
3169 ("if_deregister_com_alloc: %d not registered", type));
3170 KASSERT(if_com_free[type] != NULL,
3171 ("if_deregister_com_alloc: %d free not registered", type));
3172 if_com_alloc[type] = NULL;
3173 if_com_free[type] = NULL;
3177 if_ring_count2(int cnt, int cnt_max)
3181 KASSERT(cnt_max >= 1 && powerof2(cnt_max),
3182 ("invalid ring count max %d", cnt_max));
3191 while ((1 << (shift + 1)) <= cnt)
3195 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
3196 ("calculate cnt %d, ncpus2 %d, cnt max %d",
3197 cnt, ncpus2, cnt_max));
3202 ifq_set_maxlen(struct ifaltq *ifq, int len)
3204 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3208 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3210 return ALTQ_SUBQ_INDEX_DEFAULT;
3214 ifq_mapsubq_mask(struct ifaltq *ifq, int cpuid)
3216 return (cpuid & ifq->altq_subq_mask);
3220 ifsq_watchdog(void *arg)
3222 struct ifsubq_watchdog *wd = arg;
3225 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3228 ifp = ifsq_get_ifp(wd->wd_subq);
3229 if (ifnet_tryserialize_all(ifp)) {
3230 wd->wd_watchdog(wd->wd_subq);
3231 ifnet_deserialize_all(ifp);
3233 /* try again next timeout */
3237 ifsq_watchdog_reset(wd);
3241 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3243 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3244 ifsq_get_cpuid(wd->wd_subq));
3248 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3249 ifsq_watchdog_t watchdog)
3251 callout_init_mp(&wd->wd_callout);
3254 wd->wd_watchdog = watchdog;
3258 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3261 ifsq_watchdog_reset(wd);
3265 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3268 callout_stop(&wd->wd_callout);