kernel: Remove the COMPAT_43 kernel option along with all related code.
[dragonfly.git] / sys / net / if.c
blob8b793feb6b43595b092a962039bf27c9d04d2831
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
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_inet6.h"
34 #include "opt_inet.h"
35 #include "opt_ifpoll.h"
37 #include <sys/param.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/priv.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/socketops.h>
47 #include <sys/kernel.h>
48 #include <sys/ktr.h>
49 #include <sys/mutex.h>
50 #include <sys/sockio.h>
51 #include <sys/syslog.h>
52 #include <sys/sysctl.h>
53 #include <sys/domain.h>
54 #include <sys/thread.h>
55 #include <sys/serialize.h>
56 #include <sys/bus.h>
58 #include <sys/thread2.h>
59 #include <sys/msgport2.h>
60 #include <sys/mutex2.h>
62 #include <net/if.h>
63 #include <net/if_arp.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/if_var.h>
67 #include <net/ifq_var.h>
68 #include <net/radix.h>
69 #include <net/route.h>
70 #include <net/if_clone.h>
71 #include <net/netisr2.h>
72 #include <net/netmsg2.h>
74 #include <machine/atomic.h>
75 #include <machine/stdarg.h>
76 #include <machine/smp.h>
78 #if defined(INET) || defined(INET6)
79 /*XXX*/
80 #include <netinet/in.h>
81 #include <netinet/in_var.h>
82 #include <netinet/if_ether.h>
83 #ifdef INET6
84 #include <netinet6/in6_var.h>
85 #include <netinet6/in6_ifattach.h>
86 #endif
87 #endif
89 struct netmsg_ifaddr {
90 struct netmsg_base base;
91 struct ifaddr *ifa;
92 struct ifnet *ifp;
93 int tail;
96 struct ifsubq_stage_head {
97 TAILQ_HEAD(, ifsubq_stage) stg_head;
98 } __cachealign;
101 * System initialization
103 static void if_attachdomain(void *);
104 static void if_attachdomain1(struct ifnet *);
105 static int ifconf(u_long, caddr_t, struct ucred *);
106 static void ifinit(void *);
107 static void ifnetinit(void *);
108 static void if_slowtimo(void *);
109 static void link_rtrequest(int, struct rtentry *);
110 static int if_rtdel(struct radix_node *, void *);
111 static void if_slowtimo_dispatch(netmsg_t);
113 /* Helper functions */
114 static void ifsq_watchdog_reset(struct ifsubq_watchdog *);
115 static int if_delmulti_serialized(struct ifnet *, struct sockaddr *);
116 static struct ifnet_array *ifnet_array_alloc(int);
117 static void ifnet_array_free(struct ifnet_array *);
118 static struct ifnet_array *ifnet_array_add(struct ifnet *,
119 const struct ifnet_array *);
120 static struct ifnet_array *ifnet_array_del(struct ifnet *,
121 const struct ifnet_array *);
123 #ifdef INET6
125 * XXX: declare here to avoid to include many inet6 related files..
126 * should be more generalized?
128 extern void nd6_setmtu(struct ifnet *);
129 #endif
131 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
132 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
134 static int ifsq_stage_cntmax = 4;
135 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
136 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
137 &ifsq_stage_cntmax, 0, "ifq staging packet count max");
139 static int if_stats_compat = 0;
140 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
141 &if_stats_compat, 0, "Compat the old ifnet stats");
143 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL);
144 /* Must be after netisr_init */
145 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL);
147 static if_com_alloc_t *if_com_alloc[256];
148 static if_com_free_t *if_com_free[256];
150 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
151 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
152 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
154 int ifqmaxlen = IFQ_MAXLEN;
155 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
157 static struct ifnet_array ifnet_array0;
158 static struct ifnet_array *ifnet_array = &ifnet_array0;
160 static struct callout if_slowtimo_timer;
161 static struct netmsg_base if_slowtimo_netmsg;
163 int if_index = 0;
164 struct ifnet **ifindex2ifnet = NULL;
165 static struct thread *ifnet_threads[MAXCPU];
166 static struct mtx ifnet_mtx = MTX_INITIALIZER("ifnet");
168 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
170 #ifdef notyet
171 #define IFQ_KTR_STRING "ifq=%p"
172 #define IFQ_KTR_ARGS struct ifaltq *ifq
173 #ifndef KTR_IFQ
174 #define KTR_IFQ KTR_ALL
175 #endif
176 KTR_INFO_MASTER(ifq);
177 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
178 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
179 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
181 #define IF_START_KTR_STRING "ifp=%p"
182 #define IF_START_KTR_ARGS struct ifnet *ifp
183 #ifndef KTR_IF_START
184 #define KTR_IF_START KTR_ALL
185 #endif
186 KTR_INFO_MASTER(if_start);
187 KTR_INFO(KTR_IF_START, if_start, run, 0,
188 IF_START_KTR_STRING, IF_START_KTR_ARGS);
189 KTR_INFO(KTR_IF_START, if_start, sched, 1,
190 IF_START_KTR_STRING, IF_START_KTR_ARGS);
191 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
192 IF_START_KTR_STRING, IF_START_KTR_ARGS);
193 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
194 IF_START_KTR_STRING, IF_START_KTR_ARGS);
195 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
196 IF_START_KTR_STRING, IF_START_KTR_ARGS);
197 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
198 #endif
200 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
203 * Network interface utility routines.
205 * Routines with ifa_ifwith* names take sockaddr *'s as
206 * parameters.
208 /* ARGSUSED*/
209 static void
210 ifinit(void *dummy)
212 struct ifnet *ifp;
214 callout_init_mp(&if_slowtimo_timer);
215 netmsg_init(&if_slowtimo_netmsg, NULL, &netisr_adone_rport,
216 MSGF_PRIORITY, if_slowtimo_dispatch);
218 /* XXX is this necessary? */
219 ifnet_lock();
220 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
221 if (ifp->if_snd.altq_maxlen == 0) {
222 if_printf(ifp, "XXX: driver didn't set altq_maxlen\n");
223 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
226 ifnet_unlock();
228 /* Start if_slowtimo */
229 lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg.lmsg);
232 static void
233 ifsq_ifstart_ipifunc(void *arg)
235 struct ifaltq_subque *ifsq = arg;
236 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
238 crit_enter();
239 if (lmsg->ms_flags & MSGF_DONE)
240 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg);
241 crit_exit();
244 static __inline void
245 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
247 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
248 TAILQ_REMOVE(&head->stg_head, stage, stg_link);
249 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
250 stage->stg_cnt = 0;
251 stage->stg_len = 0;
254 static __inline void
255 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
257 KKASSERT((stage->stg_flags &
258 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
259 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
260 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
264 * Schedule ifnet.if_start on the subqueue owner CPU
266 static void
267 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
269 int cpu;
271 if (!force && curthread->td_type == TD_TYPE_NETISR &&
272 ifsq_stage_cntmax > 0) {
273 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
275 stage->stg_cnt = 0;
276 stage->stg_len = 0;
277 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
278 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
279 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
280 return;
283 cpu = ifsq_get_cpuid(ifsq);
284 if (cpu != mycpuid)
285 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
286 else
287 ifsq_ifstart_ipifunc(ifsq);
291 * NOTE:
292 * This function will release ifnet.if_start subqueue interlock,
293 * if ifnet.if_start for the subqueue does not need to be scheduled
295 static __inline int
296 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
298 if (!running || ifsq_is_empty(ifsq)
299 #ifdef ALTQ
300 || ifsq->ifsq_altq->altq_tbr != NULL
301 #endif
303 ALTQ_SQ_LOCK(ifsq);
305 * ifnet.if_start subqueue interlock is released, if:
306 * 1) Hardware can not take any packets, due to
307 * o interface is marked down
308 * o hardware queue is full (ifsq_is_oactive)
309 * Under the second situation, hardware interrupt
310 * or polling(4) will call/schedule ifnet.if_start
311 * on the subqueue when hardware queue is ready
312 * 2) There is no packet in the subqueue.
313 * Further ifq_dispatch or ifq_handoff will call/
314 * schedule ifnet.if_start on the subqueue.
315 * 3) TBR is used and it does not allow further
316 * dequeueing.
317 * TBR callout will call ifnet.if_start on the
318 * subqueue.
320 if (!running || !ifsq_data_ready(ifsq)) {
321 ifsq_clr_started(ifsq);
322 ALTQ_SQ_UNLOCK(ifsq);
323 return 0;
325 ALTQ_SQ_UNLOCK(ifsq);
327 return 1;
330 static void
331 ifsq_ifstart_dispatch(netmsg_t msg)
333 struct lwkt_msg *lmsg = &msg->base.lmsg;
334 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
335 struct ifnet *ifp = ifsq_get_ifp(ifsq);
336 struct globaldata *gd = mycpu;
337 int running = 0, need_sched;
339 crit_enter_gd(gd);
341 lwkt_replymsg(lmsg, 0); /* reply ASAP */
343 if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) {
345 * We need to chase the subqueue owner CPU change.
347 ifsq_ifstart_schedule(ifsq, 1);
348 crit_exit_gd(gd);
349 return;
352 ifsq_serialize_hw(ifsq);
353 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
354 ifp->if_start(ifp, ifsq);
355 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
356 running = 1;
358 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
359 ifsq_deserialize_hw(ifsq);
361 if (need_sched) {
363 * More data need to be transmitted, ifnet.if_start is
364 * scheduled on the subqueue owner CPU, and we keep going.
365 * NOTE: ifnet.if_start subqueue interlock is not released.
367 ifsq_ifstart_schedule(ifsq, 0);
370 crit_exit_gd(gd);
373 /* Device driver ifnet.if_start helper function */
374 void
375 ifsq_devstart(struct ifaltq_subque *ifsq)
377 struct ifnet *ifp = ifsq_get_ifp(ifsq);
378 int running = 0;
380 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
382 ALTQ_SQ_LOCK(ifsq);
383 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
384 ALTQ_SQ_UNLOCK(ifsq);
385 return;
387 ifsq_set_started(ifsq);
388 ALTQ_SQ_UNLOCK(ifsq);
390 ifp->if_start(ifp, ifsq);
392 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
393 running = 1;
395 if (ifsq_ifstart_need_schedule(ifsq, running)) {
397 * More data need to be transmitted, ifnet.if_start is
398 * scheduled on ifnet's CPU, and we keep going.
399 * NOTE: ifnet.if_start interlock is not released.
401 ifsq_ifstart_schedule(ifsq, 0);
405 void
406 if_devstart(struct ifnet *ifp)
408 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
411 /* Device driver ifnet.if_start schedule helper function */
412 void
413 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
415 ifsq_ifstart_schedule(ifsq, 1);
418 void
419 if_devstart_sched(struct ifnet *ifp)
421 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
424 static void
425 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
427 lwkt_serialize_enter(ifp->if_serializer);
430 static void
431 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
433 lwkt_serialize_exit(ifp->if_serializer);
436 static int
437 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
439 return lwkt_serialize_try(ifp->if_serializer);
442 #ifdef INVARIANTS
443 static void
444 if_default_serialize_assert(struct ifnet *ifp,
445 enum ifnet_serialize slz __unused,
446 boolean_t serialized)
448 if (serialized)
449 ASSERT_SERIALIZED(ifp->if_serializer);
450 else
451 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
453 #endif
456 * Attach an interface to the list of "active" interfaces.
458 * The serializer is optional.
460 void
461 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
463 unsigned socksize;
464 int namelen, masklen;
465 struct sockaddr_dl *sdl, *sdl_addr;
466 struct ifaddr *ifa;
467 struct ifaltq *ifq;
468 struct ifnet **old_ifindex2ifnet = NULL;
469 struct ifnet_array *old_ifnet_array;
470 int i, q;
472 static int if_indexlim = 8;
474 if (ifp->if_serialize != NULL) {
475 KASSERT(ifp->if_deserialize != NULL &&
476 ifp->if_tryserialize != NULL &&
477 ifp->if_serialize_assert != NULL,
478 ("serialize functions are partially setup"));
481 * If the device supplies serialize functions,
482 * then clear if_serializer to catch any invalid
483 * usage of this field.
485 KASSERT(serializer == NULL,
486 ("both serialize functions and default serializer "
487 "are supplied"));
488 ifp->if_serializer = NULL;
489 } else {
490 KASSERT(ifp->if_deserialize == NULL &&
491 ifp->if_tryserialize == NULL &&
492 ifp->if_serialize_assert == NULL,
493 ("serialize functions are partially setup"));
494 ifp->if_serialize = if_default_serialize;
495 ifp->if_deserialize = if_default_deserialize;
496 ifp->if_tryserialize = if_default_tryserialize;
497 #ifdef INVARIANTS
498 ifp->if_serialize_assert = if_default_serialize_assert;
499 #endif
502 * The serializer can be passed in from the device,
503 * allowing the same serializer to be used for both
504 * the interrupt interlock and the device queue.
505 * If not specified, the netif structure will use an
506 * embedded serializer.
508 if (serializer == NULL) {
509 serializer = &ifp->if_default_serializer;
510 lwkt_serialize_init(serializer);
512 ifp->if_serializer = serializer;
516 * XXX -
517 * The old code would work if the interface passed a pre-existing
518 * chain of ifaddrs to this code. We don't trust our callers to
519 * properly initialize the tailq, however, so we no longer allow
520 * this unlikely case.
522 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
523 M_IFADDR, M_WAITOK | M_ZERO);
524 for (i = 0; i < ncpus; ++i)
525 TAILQ_INIT(&ifp->if_addrheads[i]);
527 TAILQ_INIT(&ifp->if_multiaddrs);
528 TAILQ_INIT(&ifp->if_groups);
529 getmicrotime(&ifp->if_lastchange);
532 * create a Link Level name for this device
534 namelen = strlen(ifp->if_xname);
535 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
536 socksize = masklen + ifp->if_addrlen;
537 if (socksize < sizeof(*sdl))
538 socksize = sizeof(*sdl);
539 socksize = RT_ROUNDUP(socksize);
540 ifa = ifa_create(sizeof(struct ifaddr) + 2 * socksize);
541 sdl = sdl_addr = (struct sockaddr_dl *)(ifa + 1);
542 sdl->sdl_len = socksize;
543 sdl->sdl_family = AF_LINK;
544 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
545 sdl->sdl_nlen = namelen;
546 sdl->sdl_type = ifp->if_type;
547 ifp->if_lladdr = ifa;
548 ifa->ifa_ifp = ifp;
549 ifa->ifa_rtrequest = link_rtrequest;
550 ifa->ifa_addr = (struct sockaddr *)sdl;
551 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
552 ifa->ifa_netmask = (struct sockaddr *)sdl;
553 sdl->sdl_len = masklen;
554 while (namelen != 0)
555 sdl->sdl_data[--namelen] = 0xff;
556 ifa_iflink(ifa, ifp, 0 /* Insert head */);
558 ifp->if_data_pcpu = kmalloc_cachealign(
559 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
561 if (ifp->if_mapsubq == NULL)
562 ifp->if_mapsubq = ifq_mapsubq_default;
564 ifq = &ifp->if_snd;
565 ifq->altq_type = 0;
566 ifq->altq_disc = NULL;
567 ifq->altq_flags &= ALTQF_CANTCHANGE;
568 ifq->altq_tbr = NULL;
569 ifq->altq_ifp = ifp;
571 if (ifq->altq_subq_cnt <= 0)
572 ifq->altq_subq_cnt = 1;
573 ifq->altq_subq = kmalloc_cachealign(
574 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
575 M_DEVBUF, M_WAITOK | M_ZERO);
577 if (ifq->altq_maxlen == 0) {
578 if_printf(ifp, "driver didn't set altq_maxlen\n");
579 ifq_set_maxlen(ifq, ifqmaxlen);
582 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
583 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
585 ALTQ_SQ_LOCK_INIT(ifsq);
586 ifsq->ifsq_index = q;
588 ifsq->ifsq_altq = ifq;
589 ifsq->ifsq_ifp = ifp;
591 ifsq->ifsq_maxlen = ifq->altq_maxlen;
592 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
593 ifsq->ifsq_prepended = NULL;
594 ifsq->ifsq_started = 0;
595 ifsq->ifsq_hw_oactive = 0;
596 ifsq_set_cpuid(ifsq, 0);
597 if (ifp->if_serializer != NULL)
598 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
600 ifsq->ifsq_stage =
601 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
602 M_DEVBUF, M_WAITOK | M_ZERO);
603 for (i = 0; i < ncpus; ++i)
604 ifsq->ifsq_stage[i].stg_subq = ifsq;
606 ifsq->ifsq_ifstart_nmsg =
607 kmalloc(ncpus * sizeof(struct netmsg_base),
608 M_LWKTMSG, M_WAITOK);
609 for (i = 0; i < ncpus; ++i) {
610 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
611 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
612 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
615 ifq_set_classic(ifq);
618 * Increase mbuf cluster/jcluster limits for the mbufs that
619 * could sit on the device queues for quite some time.
621 if (ifp->if_nmbclusters > 0)
622 mcl_inclimit(ifp->if_nmbclusters);
623 if (ifp->if_nmbjclusters > 0)
624 mjcl_inclimit(ifp->if_nmbjclusters);
627 * Install this ifp into ifindex2inet, ifnet queue and ifnet
628 * array after it is setup.
630 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
631 * by ifnet lock, so that non-netisr threads could get a
632 * consistent view.
634 ifnet_lock();
636 /* Don't update if_index until ifindex2ifnet is setup */
637 ifp->if_index = if_index + 1;
638 sdl_addr->sdl_index = ifp->if_index;
641 * Install this ifp into ifindex2ifnet
643 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
644 unsigned int n;
645 struct ifnet **q;
648 * Grow ifindex2ifnet
650 if_indexlim <<= 1;
651 n = if_indexlim * sizeof(*q);
652 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
653 if (ifindex2ifnet != NULL) {
654 bcopy(ifindex2ifnet, q, n/2);
655 /* Free old ifindex2ifnet after sync all netisrs */
656 old_ifindex2ifnet = ifindex2ifnet;
658 ifindex2ifnet = q;
660 ifindex2ifnet[ifp->if_index] = ifp;
662 * Update if_index after this ifp is installed into ifindex2ifnet,
663 * so that netisrs could get a consistent view of ifindex2ifnet.
665 cpu_sfence();
666 if_index = ifp->if_index;
669 * Install this ifp into ifnet array.
671 /* Free old ifnet array after sync all netisrs */
672 old_ifnet_array = ifnet_array;
673 ifnet_array = ifnet_array_add(ifp, old_ifnet_array);
676 * Install this ifp into ifnet queue.
678 TAILQ_INSERT_TAIL(&ifnetlist, ifp, if_link);
680 ifnet_unlock();
683 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
684 * are no longer accessed and we can free them safely later on.
686 netmsg_service_sync();
687 if (old_ifindex2ifnet != NULL)
688 kfree(old_ifindex2ifnet, M_IFADDR);
689 ifnet_array_free(old_ifnet_array);
691 if (!SLIST_EMPTY(&domains))
692 if_attachdomain1(ifp);
694 /* Announce the interface. */
695 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
696 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
697 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
700 static void
701 if_attachdomain(void *dummy)
703 struct ifnet *ifp;
705 ifnet_lock();
706 TAILQ_FOREACH(ifp, &ifnetlist, if_list)
707 if_attachdomain1(ifp);
708 ifnet_unlock();
710 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
711 if_attachdomain, NULL);
713 static void
714 if_attachdomain1(struct ifnet *ifp)
716 struct domain *dp;
718 crit_enter();
720 /* address family dependent data region */
721 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
722 SLIST_FOREACH(dp, &domains, dom_next)
723 if (dp->dom_ifattach)
724 ifp->if_afdata[dp->dom_family] =
725 (*dp->dom_ifattach)(ifp);
726 crit_exit();
730 * Purge all addresses whose type is _not_ AF_LINK
732 static void
733 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg)
735 struct lwkt_msg *lmsg = &nmsg->lmsg;
736 struct ifnet *ifp = lmsg->u.ms_resultp;
737 struct ifaddr_container *ifac, *next;
739 ASSERT_IN_NETISR(0);
742 * The ifaddr processing in the following loop will block,
743 * however, this function is called in netisr0, in which
744 * ifaddr list changes happen, so we don't care about the
745 * blockness of the ifaddr processing here.
747 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
748 ifa_link, next) {
749 struct ifaddr *ifa = ifac->ifa;
751 /* Ignore marker */
752 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
753 continue;
755 /* Leave link ifaddr as it is */
756 if (ifa->ifa_addr->sa_family == AF_LINK)
757 continue;
758 #ifdef INET
759 /* XXX: Ugly!! ad hoc just for INET */
760 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
761 struct ifaliasreq ifr;
762 #ifdef IFADDR_DEBUG_VERBOSE
763 int i;
765 kprintf("purge in4 addr %p: ", ifa);
766 for (i = 0; i < ncpus; ++i)
767 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
768 kprintf("\n");
769 #endif
771 bzero(&ifr, sizeof ifr);
772 ifr.ifra_addr = *ifa->ifa_addr;
773 if (ifa->ifa_dstaddr)
774 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
775 if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp,
776 NULL) == 0)
777 continue;
779 #endif /* INET */
780 #ifdef INET6
781 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
782 #ifdef IFADDR_DEBUG_VERBOSE
783 int i;
785 kprintf("purge in6 addr %p: ", ifa);
786 for (i = 0; i < ncpus; ++i)
787 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
788 kprintf("\n");
789 #endif
791 in6_purgeaddr(ifa);
792 /* ifp_addrhead is already updated */
793 continue;
795 #endif /* INET6 */
796 ifa_ifunlink(ifa, ifp);
797 ifa_destroy(ifa);
800 lwkt_replymsg(lmsg, 0);
803 void
804 if_purgeaddrs_nolink(struct ifnet *ifp)
806 struct netmsg_base nmsg;
807 struct lwkt_msg *lmsg = &nmsg.lmsg;
809 ASSERT_CANDOMSG_NETISR0(curthread);
811 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
812 if_purgeaddrs_nolink_dispatch);
813 lmsg->u.ms_resultp = ifp;
814 lwkt_domsg(netisr_cpuport(0), lmsg, 0);
817 static void
818 ifq_stage_detach_handler(netmsg_t nmsg)
820 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
821 int q;
823 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
824 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
825 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
827 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
828 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
830 lwkt_replymsg(&nmsg->lmsg, 0);
833 static void
834 ifq_stage_detach(struct ifaltq *ifq)
836 struct netmsg_base base;
837 int cpu;
839 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
840 ifq_stage_detach_handler);
841 base.lmsg.u.ms_resultp = ifq;
843 for (cpu = 0; cpu < ncpus; ++cpu)
844 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
847 struct netmsg_if_rtdel {
848 struct netmsg_base base;
849 struct ifnet *ifp;
852 static void
853 if_rtdel_dispatch(netmsg_t msg)
855 struct netmsg_if_rtdel *rmsg = (void *)msg;
856 int i, nextcpu, cpu;
858 cpu = mycpuid;
859 for (i = 1; i <= AF_MAX; i++) {
860 struct radix_node_head *rnh;
862 if ((rnh = rt_tables[cpu][i]) == NULL)
863 continue;
864 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
867 nextcpu = cpu + 1;
868 if (nextcpu < ncpus)
869 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
870 else
871 lwkt_replymsg(&rmsg->base.lmsg, 0);
875 * Detach an interface, removing it from the
876 * list of "active" interfaces.
878 void
879 if_detach(struct ifnet *ifp)
881 struct ifnet_array *old_ifnet_array;
882 struct netmsg_if_rtdel msg;
883 struct domain *dp;
884 int q;
886 /* Announce that the interface is gone. */
887 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
888 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
889 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
892 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
893 * array before it is whacked.
895 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
896 * by ifnet lock, so that non-netisr threads could get a
897 * consistent view.
899 ifnet_lock();
902 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
904 ifindex2ifnet[ifp->if_index] = NULL;
905 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
906 if_index--;
909 * Remove this ifp from ifnet queue.
911 TAILQ_REMOVE(&ifnetlist, ifp, if_link);
914 * Remove this ifp from ifnet array.
916 /* Free old ifnet array after sync all netisrs */
917 old_ifnet_array = ifnet_array;
918 ifnet_array = ifnet_array_del(ifp, old_ifnet_array);
920 ifnet_unlock();
923 * Sync all netisrs so that the old ifnet array is no longer
924 * accessed and we can free it safely later on.
926 netmsg_service_sync();
927 ifnet_array_free(old_ifnet_array);
930 * Remove routes and flush queues.
932 crit_enter();
933 #ifdef IFPOLL_ENABLE
934 if (ifp->if_flags & IFF_NPOLLING)
935 ifpoll_deregister(ifp);
936 #endif
937 if_down(ifp);
939 /* Decrease the mbuf clusters/jclusters limits increased by us */
940 if (ifp->if_nmbclusters > 0)
941 mcl_inclimit(-ifp->if_nmbclusters);
942 if (ifp->if_nmbjclusters > 0)
943 mjcl_inclimit(-ifp->if_nmbjclusters);
945 #ifdef ALTQ
946 if (ifq_is_enabled(&ifp->if_snd))
947 altq_disable(&ifp->if_snd);
948 if (ifq_is_attached(&ifp->if_snd))
949 altq_detach(&ifp->if_snd);
950 #endif
953 * Clean up all addresses.
955 ifp->if_lladdr = NULL;
957 if_purgeaddrs_nolink(ifp);
958 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
959 struct ifaddr *ifa;
961 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
962 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
963 ("non-link ifaddr is left on if_addrheads"));
965 ifa_ifunlink(ifa, ifp);
966 ifa_destroy(ifa);
967 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
968 ("there are still ifaddrs left on if_addrheads"));
971 #ifdef INET
973 * Remove all IPv4 kernel structures related to ifp.
975 in_ifdetach(ifp);
976 #endif
978 #ifdef INET6
980 * Remove all IPv6 kernel structs related to ifp. This should be done
981 * before removing routing entries below, since IPv6 interface direct
982 * routes are expected to be removed by the IPv6-specific kernel API.
983 * Otherwise, the kernel will detect some inconsistency and bark it.
985 in6_ifdetach(ifp);
986 #endif
989 * Delete all remaining routes using this interface
991 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
992 if_rtdel_dispatch);
993 msg.ifp = ifp;
994 rt_domsg_global(&msg.base);
996 SLIST_FOREACH(dp, &domains, dom_next)
997 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
998 (*dp->dom_ifdetach)(ifp,
999 ifp->if_afdata[dp->dom_family]);
1001 kfree(ifp->if_addrheads, M_IFADDR);
1003 lwkt_synchronize_ipiqs("if_detach");
1004 ifq_stage_detach(&ifp->if_snd);
1006 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
1007 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
1009 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
1010 kfree(ifsq->ifsq_stage, M_DEVBUF);
1012 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
1014 kfree(ifp->if_data_pcpu, M_DEVBUF);
1016 crit_exit();
1020 * Create interface group without members
1022 struct ifg_group *
1023 if_creategroup(const char *groupname)
1025 struct ifg_group *ifg = NULL;
1027 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
1028 M_TEMP, M_NOWAIT)) == NULL)
1029 return (NULL);
1031 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1032 ifg->ifg_refcnt = 0;
1033 ifg->ifg_carp_demoted = 0;
1034 TAILQ_INIT(&ifg->ifg_members);
1035 #if NPF > 0
1036 pfi_attach_ifgroup(ifg);
1037 #endif
1038 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
1040 return (ifg);
1044 * Add a group to an interface
1047 if_addgroup(struct ifnet *ifp, const char *groupname)
1049 struct ifg_list *ifgl;
1050 struct ifg_group *ifg = NULL;
1051 struct ifg_member *ifgm;
1053 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1054 groupname[strlen(groupname) - 1] <= '9')
1055 return (EINVAL);
1057 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1058 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1059 return (EEXIST);
1061 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
1062 return (ENOMEM);
1064 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1065 kfree(ifgl, M_TEMP);
1066 return (ENOMEM);
1069 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1070 if (!strcmp(ifg->ifg_group, groupname))
1071 break;
1073 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
1074 kfree(ifgl, M_TEMP);
1075 kfree(ifgm, M_TEMP);
1076 return (ENOMEM);
1079 ifg->ifg_refcnt++;
1080 ifgl->ifgl_group = ifg;
1081 ifgm->ifgm_ifp = ifp;
1083 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1084 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1086 #if NPF > 0
1087 pfi_group_change(groupname);
1088 #endif
1090 return (0);
1094 * Remove a group from an interface
1097 if_delgroup(struct ifnet *ifp, const char *groupname)
1099 struct ifg_list *ifgl;
1100 struct ifg_member *ifgm;
1102 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1103 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1104 break;
1105 if (ifgl == NULL)
1106 return (ENOENT);
1108 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1110 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1111 if (ifgm->ifgm_ifp == ifp)
1112 break;
1114 if (ifgm != NULL) {
1115 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1116 kfree(ifgm, M_TEMP);
1119 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1120 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1121 #if NPF > 0
1122 pfi_detach_ifgroup(ifgl->ifgl_group);
1123 #endif
1124 kfree(ifgl->ifgl_group, M_TEMP);
1127 kfree(ifgl, M_TEMP);
1129 #if NPF > 0
1130 pfi_group_change(groupname);
1131 #endif
1133 return (0);
1137 * Stores all groups from an interface in memory pointed
1138 * to by data
1141 if_getgroup(caddr_t data, struct ifnet *ifp)
1143 int len, error;
1144 struct ifg_list *ifgl;
1145 struct ifg_req ifgrq, *ifgp;
1146 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1148 if (ifgr->ifgr_len == 0) {
1149 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1150 ifgr->ifgr_len += sizeof(struct ifg_req);
1151 return (0);
1154 len = ifgr->ifgr_len;
1155 ifgp = ifgr->ifgr_groups;
1156 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1157 if (len < sizeof(ifgrq))
1158 return (EINVAL);
1159 bzero(&ifgrq, sizeof ifgrq);
1160 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1161 sizeof(ifgrq.ifgrq_group));
1162 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1163 sizeof(struct ifg_req))))
1164 return (error);
1165 len -= sizeof(ifgrq);
1166 ifgp++;
1169 return (0);
1173 * Stores all members of a group in memory pointed to by data
1176 if_getgroupmembers(caddr_t data)
1178 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1179 struct ifg_group *ifg;
1180 struct ifg_member *ifgm;
1181 struct ifg_req ifgrq, *ifgp;
1182 int len, error;
1184 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1185 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1186 break;
1187 if (ifg == NULL)
1188 return (ENOENT);
1190 if (ifgr->ifgr_len == 0) {
1191 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1192 ifgr->ifgr_len += sizeof(ifgrq);
1193 return (0);
1196 len = ifgr->ifgr_len;
1197 ifgp = ifgr->ifgr_groups;
1198 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1199 if (len < sizeof(ifgrq))
1200 return (EINVAL);
1201 bzero(&ifgrq, sizeof ifgrq);
1202 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1203 sizeof(ifgrq.ifgrq_member));
1204 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1205 sizeof(struct ifg_req))))
1206 return (error);
1207 len -= sizeof(ifgrq);
1208 ifgp++;
1211 return (0);
1215 * Delete Routes for a Network Interface
1217 * Called for each routing entry via the rnh->rnh_walktree() call above
1218 * to delete all route entries referencing a detaching network interface.
1220 * Arguments:
1221 * rn pointer to node in the routing table
1222 * arg argument passed to rnh->rnh_walktree() - detaching interface
1224 * Returns:
1225 * 0 successful
1226 * errno failed - reason indicated
1229 static int
1230 if_rtdel(struct radix_node *rn, void *arg)
1232 struct rtentry *rt = (struct rtentry *)rn;
1233 struct ifnet *ifp = arg;
1234 int err;
1236 if (rt->rt_ifp == ifp) {
1239 * Protect (sorta) against walktree recursion problems
1240 * with cloned routes
1242 if (!(rt->rt_flags & RTF_UP))
1243 return (0);
1245 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1246 rt_mask(rt), rt->rt_flags,
1247 NULL);
1248 if (err) {
1249 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1253 return (0);
1256 static __inline boolean_t
1257 ifa_prefer(const struct ifaddr *cur_ifa, const struct ifaddr *old_ifa)
1259 if (old_ifa == NULL)
1260 return TRUE;
1262 if ((old_ifa->ifa_ifp->if_flags & IFF_UP) == 0 &&
1263 (cur_ifa->ifa_ifp->if_flags & IFF_UP))
1264 return TRUE;
1265 if ((old_ifa->ifa_flags & IFA_ROUTE) == 0 &&
1266 (cur_ifa->ifa_flags & IFA_ROUTE))
1267 return TRUE;
1268 return FALSE;
1272 * Locate an interface based on a complete address.
1274 struct ifaddr *
1275 ifa_ifwithaddr(struct sockaddr *addr)
1277 const struct ifnet_array *arr;
1278 int i;
1280 arr = ifnet_array_get();
1281 for (i = 0; i < arr->ifnet_count; ++i) {
1282 struct ifnet *ifp = arr->ifnet_arr[i];
1283 struct ifaddr_container *ifac;
1285 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1286 struct ifaddr *ifa = ifac->ifa;
1288 if (ifa->ifa_addr->sa_family != addr->sa_family)
1289 continue;
1290 if (sa_equal(addr, ifa->ifa_addr))
1291 return (ifa);
1292 if ((ifp->if_flags & IFF_BROADCAST) &&
1293 ifa->ifa_broadaddr &&
1294 /* IPv6 doesn't have broadcast */
1295 ifa->ifa_broadaddr->sa_len != 0 &&
1296 sa_equal(ifa->ifa_broadaddr, addr))
1297 return (ifa);
1300 return (NULL);
1304 * Locate the point to point interface with a given destination address.
1306 struct ifaddr *
1307 ifa_ifwithdstaddr(struct sockaddr *addr)
1309 const struct ifnet_array *arr;
1310 int i;
1312 arr = ifnet_array_get();
1313 for (i = 0; i < arr->ifnet_count; ++i) {
1314 struct ifnet *ifp = arr->ifnet_arr[i];
1315 struct ifaddr_container *ifac;
1317 if (!(ifp->if_flags & IFF_POINTOPOINT))
1318 continue;
1320 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1321 struct ifaddr *ifa = ifac->ifa;
1323 if (ifa->ifa_addr->sa_family != addr->sa_family)
1324 continue;
1325 if (ifa->ifa_dstaddr &&
1326 sa_equal(addr, ifa->ifa_dstaddr))
1327 return (ifa);
1330 return (NULL);
1334 * Find an interface on a specific network. If many, choice
1335 * is most specific found.
1337 struct ifaddr *
1338 ifa_ifwithnet(struct sockaddr *addr)
1340 struct ifaddr *ifa_maybe = NULL;
1341 u_int af = addr->sa_family;
1342 char *addr_data = addr->sa_data, *cplim;
1343 const struct ifnet_array *arr;
1344 int i;
1347 * AF_LINK addresses can be looked up directly by their index number,
1348 * so do that if we can.
1350 if (af == AF_LINK) {
1351 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1353 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1354 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1358 * Scan though each interface, looking for ones that have
1359 * addresses in this address family.
1361 arr = ifnet_array_get();
1362 for (i = 0; i < arr->ifnet_count; ++i) {
1363 struct ifnet *ifp = arr->ifnet_arr[i];
1364 struct ifaddr_container *ifac;
1366 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1367 struct ifaddr *ifa = ifac->ifa;
1368 char *cp, *cp2, *cp3;
1370 if (ifa->ifa_addr->sa_family != af)
1371 next: continue;
1372 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1374 * This is a bit broken as it doesn't
1375 * take into account that the remote end may
1376 * be a single node in the network we are
1377 * looking for.
1378 * The trouble is that we don't know the
1379 * netmask for the remote end.
1381 if (ifa->ifa_dstaddr != NULL &&
1382 sa_equal(addr, ifa->ifa_dstaddr))
1383 return (ifa);
1384 } else {
1386 * if we have a special address handler,
1387 * then use it instead of the generic one.
1389 if (ifa->ifa_claim_addr) {
1390 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1391 return (ifa);
1392 } else {
1393 continue;
1398 * Scan all the bits in the ifa's address.
1399 * If a bit dissagrees with what we are
1400 * looking for, mask it with the netmask
1401 * to see if it really matters.
1402 * (A byte at a time)
1404 if (ifa->ifa_netmask == 0)
1405 continue;
1406 cp = addr_data;
1407 cp2 = ifa->ifa_addr->sa_data;
1408 cp3 = ifa->ifa_netmask->sa_data;
1409 cplim = ifa->ifa_netmask->sa_len +
1410 (char *)ifa->ifa_netmask;
1411 while (cp3 < cplim)
1412 if ((*cp++ ^ *cp2++) & *cp3++)
1413 goto next; /* next address! */
1415 * If the netmask of what we just found
1416 * is more specific than what we had before
1417 * (if we had one) then remember the new one
1418 * before continuing to search for an even
1419 * better one. If the netmasks are equal,
1420 * we prefer the this ifa based on the result
1421 * of ifa_prefer().
1423 if (ifa_maybe == NULL ||
1424 rn_refines((char *)ifa->ifa_netmask,
1425 (char *)ifa_maybe->ifa_netmask) ||
1426 (sa_equal(ifa_maybe->ifa_netmask,
1427 ifa->ifa_netmask) &&
1428 ifa_prefer(ifa, ifa_maybe)))
1429 ifa_maybe = ifa;
1433 return (ifa_maybe);
1437 * Find an interface address specific to an interface best matching
1438 * a given address.
1440 struct ifaddr *
1441 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1443 struct ifaddr_container *ifac;
1444 char *cp, *cp2, *cp3;
1445 char *cplim;
1446 struct ifaddr *ifa_maybe = NULL;
1447 u_int af = addr->sa_family;
1449 if (af >= AF_MAX)
1450 return (0);
1451 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1452 struct ifaddr *ifa = ifac->ifa;
1454 if (ifa->ifa_addr->sa_family != af)
1455 continue;
1456 if (ifa_maybe == NULL)
1457 ifa_maybe = ifa;
1458 if (ifa->ifa_netmask == NULL) {
1459 if (sa_equal(addr, ifa->ifa_addr) ||
1460 (ifa->ifa_dstaddr != NULL &&
1461 sa_equal(addr, ifa->ifa_dstaddr)))
1462 return (ifa);
1463 continue;
1465 if (ifp->if_flags & IFF_POINTOPOINT) {
1466 if (sa_equal(addr, ifa->ifa_dstaddr))
1467 return (ifa);
1468 } else {
1469 cp = addr->sa_data;
1470 cp2 = ifa->ifa_addr->sa_data;
1471 cp3 = ifa->ifa_netmask->sa_data;
1472 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1473 for (; cp3 < cplim; cp3++)
1474 if ((*cp++ ^ *cp2++) & *cp3)
1475 break;
1476 if (cp3 == cplim)
1477 return (ifa);
1480 return (ifa_maybe);
1484 * Default action when installing a route with a Link Level gateway.
1485 * Lookup an appropriate real ifa to point to.
1486 * This should be moved to /sys/net/link.c eventually.
1488 static void
1489 link_rtrequest(int cmd, struct rtentry *rt)
1491 struct ifaddr *ifa;
1492 struct sockaddr *dst;
1493 struct ifnet *ifp;
1495 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1496 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1497 return;
1498 ifa = ifaof_ifpforaddr(dst, ifp);
1499 if (ifa != NULL) {
1500 IFAFREE(rt->rt_ifa);
1501 IFAREF(ifa);
1502 rt->rt_ifa = ifa;
1503 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1504 ifa->ifa_rtrequest(cmd, rt);
1508 struct netmsg_ifroute {
1509 struct netmsg_base base;
1510 struct ifnet *ifp;
1511 int flag;
1512 int fam;
1516 * Mark an interface down and notify protocols of the transition.
1518 static void
1519 if_unroute_dispatch(netmsg_t nmsg)
1521 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1522 struct ifnet *ifp = msg->ifp;
1523 int flag = msg->flag, fam = msg->fam;
1524 struct ifaddr_container *ifac;
1526 ifp->if_flags &= ~flag;
1527 getmicrotime(&ifp->if_lastchange);
1529 * The ifaddr processing in the following loop will block,
1530 * however, this function is called in netisr0, in which
1531 * ifaddr list changes happen, so we don't care about the
1532 * blockness of the ifaddr processing here.
1534 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1535 struct ifaddr *ifa = ifac->ifa;
1537 /* Ignore marker */
1538 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1539 continue;
1541 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1542 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1544 ifq_purge_all(&ifp->if_snd);
1545 rt_ifmsg(ifp);
1547 lwkt_replymsg(&nmsg->lmsg, 0);
1550 void
1551 if_unroute(struct ifnet *ifp, int flag, int fam)
1553 struct netmsg_ifroute msg;
1555 ASSERT_CANDOMSG_NETISR0(curthread);
1557 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1558 if_unroute_dispatch);
1559 msg.ifp = ifp;
1560 msg.flag = flag;
1561 msg.fam = fam;
1562 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1566 * Mark an interface up and notify protocols of the transition.
1568 static void
1569 if_route_dispatch(netmsg_t nmsg)
1571 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1572 struct ifnet *ifp = msg->ifp;
1573 int flag = msg->flag, fam = msg->fam;
1574 struct ifaddr_container *ifac;
1576 ifq_purge_all(&ifp->if_snd);
1577 ifp->if_flags |= flag;
1578 getmicrotime(&ifp->if_lastchange);
1580 * The ifaddr processing in the following loop will block,
1581 * however, this function is called in netisr0, in which
1582 * ifaddr list changes happen, so we don't care about the
1583 * blockness of the ifaddr processing here.
1585 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1586 struct ifaddr *ifa = ifac->ifa;
1588 /* Ignore marker */
1589 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1590 continue;
1592 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1593 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1595 rt_ifmsg(ifp);
1596 #ifdef INET6
1597 in6_if_up(ifp);
1598 #endif
1600 lwkt_replymsg(&nmsg->lmsg, 0);
1603 void
1604 if_route(struct ifnet *ifp, int flag, int fam)
1606 struct netmsg_ifroute msg;
1608 ASSERT_CANDOMSG_NETISR0(curthread);
1610 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1611 if_route_dispatch);
1612 msg.ifp = ifp;
1613 msg.flag = flag;
1614 msg.fam = fam;
1615 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1619 * Mark an interface down and notify protocols of the transition. An
1620 * interface going down is also considered to be a synchronizing event.
1621 * We must ensure that all packet processing related to the interface
1622 * has completed before we return so e.g. the caller can free the ifnet
1623 * structure that the mbufs may be referencing.
1625 * NOTE: must be called at splnet or eqivalent.
1627 void
1628 if_down(struct ifnet *ifp)
1630 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1631 netmsg_service_sync();
1635 * Mark an interface up and notify protocols of
1636 * the transition.
1637 * NOTE: must be called at splnet or eqivalent.
1639 void
1640 if_up(struct ifnet *ifp)
1642 if_route(ifp, IFF_UP, AF_UNSPEC);
1646 * Process a link state change.
1647 * NOTE: must be called at splsoftnet or equivalent.
1649 void
1650 if_link_state_change(struct ifnet *ifp)
1652 int link_state = ifp->if_link_state;
1654 rt_ifmsg(ifp);
1655 devctl_notify("IFNET", ifp->if_xname,
1656 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1660 * Handle interface watchdog timer routines. Called
1661 * from softclock, we decrement timers (if set) and
1662 * call the appropriate interface routine on expiration.
1664 static void
1665 if_slowtimo_dispatch(netmsg_t nmsg)
1667 struct globaldata *gd = mycpu;
1668 const struct ifnet_array *arr;
1669 int i;
1671 ASSERT_IN_NETISR(0);
1673 crit_enter_gd(gd);
1674 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1675 crit_exit_gd(gd);
1677 arr = ifnet_array_get();
1678 for (i = 0; i < arr->ifnet_count; ++i) {
1679 struct ifnet *ifp = arr->ifnet_arr[i];
1681 crit_enter_gd(gd);
1683 if (if_stats_compat) {
1684 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1685 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1686 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1687 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1688 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1689 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1690 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1691 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1692 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1693 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1694 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1695 IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1698 if (ifp->if_timer == 0 || --ifp->if_timer) {
1699 crit_exit_gd(gd);
1700 continue;
1702 if (ifp->if_watchdog) {
1703 if (ifnet_tryserialize_all(ifp)) {
1704 (*ifp->if_watchdog)(ifp);
1705 ifnet_deserialize_all(ifp);
1706 } else {
1707 /* try again next timeout */
1708 ++ifp->if_timer;
1712 crit_exit_gd(gd);
1715 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1718 static void
1719 if_slowtimo(void *arg __unused)
1721 struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1723 KASSERT(mycpuid == 0, ("not on cpu0"));
1724 crit_enter();
1725 if (lmsg->ms_flags & MSGF_DONE)
1726 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1727 crit_exit();
1731 * Map interface name to
1732 * interface structure pointer.
1734 struct ifnet *
1735 ifunit(const char *name)
1737 struct ifnet *ifp;
1740 * Search all the interfaces for this name/number
1742 KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked"));
1744 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1745 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1746 break;
1748 return (ifp);
1751 struct ifnet *
1752 ifunit_netisr(const char *name)
1754 const struct ifnet_array *arr;
1755 int i;
1758 * Search all the interfaces for this name/number
1761 arr = ifnet_array_get();
1762 for (i = 0; i < arr->ifnet_count; ++i) {
1763 struct ifnet *ifp = arr->ifnet_arr[i];
1765 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1766 return ifp;
1768 return NULL;
1772 * Interface ioctls.
1775 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1777 struct ifnet *ifp;
1778 struct ifreq *ifr;
1779 struct ifstat *ifs;
1780 int error;
1781 short oif_flags;
1782 int new_flags;
1783 size_t namelen, onamelen;
1784 char new_name[IFNAMSIZ];
1785 struct ifaddr *ifa;
1786 struct sockaddr_dl *sdl;
1788 switch (cmd) {
1789 case SIOCGIFCONF:
1790 case OSIOCGIFCONF:
1791 return (ifconf(cmd, data, cred));
1792 default:
1793 break;
1796 ifr = (struct ifreq *)data;
1798 switch (cmd) {
1799 case SIOCIFCREATE:
1800 case SIOCIFCREATE2:
1801 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1802 return (error);
1803 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1804 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1805 case SIOCIFDESTROY:
1806 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1807 return (error);
1808 return (if_clone_destroy(ifr->ifr_name));
1809 case SIOCIFGCLONERS:
1810 return (if_clone_list((struct if_clonereq *)data));
1811 default:
1812 break;
1816 * Nominal ioctl through interface, lookup the ifp and obtain a
1817 * lock to serialize the ifconfig ioctl operation.
1819 ifnet_lock();
1821 ifp = ifunit(ifr->ifr_name);
1822 if (ifp == NULL) {
1823 ifnet_unlock();
1824 return (ENXIO);
1826 error = 0;
1828 switch (cmd) {
1829 case SIOCGIFINDEX:
1830 ifr->ifr_index = ifp->if_index;
1831 break;
1833 case SIOCGIFFLAGS:
1834 ifr->ifr_flags = ifp->if_flags;
1835 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1836 break;
1838 case SIOCGIFCAP:
1839 ifr->ifr_reqcap = ifp->if_capabilities;
1840 ifr->ifr_curcap = ifp->if_capenable;
1841 break;
1843 case SIOCGIFMETRIC:
1844 ifr->ifr_metric = ifp->if_metric;
1845 break;
1847 case SIOCGIFMTU:
1848 ifr->ifr_mtu = ifp->if_mtu;
1849 break;
1851 case SIOCGIFTSOLEN:
1852 ifr->ifr_tsolen = ifp->if_tsolen;
1853 break;
1855 case SIOCGIFDATA:
1856 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1857 sizeof(ifp->if_data));
1858 break;
1860 case SIOCGIFPHYS:
1861 ifr->ifr_phys = ifp->if_physical;
1862 break;
1864 case SIOCGIFPOLLCPU:
1865 ifr->ifr_pollcpu = -1;
1866 break;
1868 case SIOCSIFPOLLCPU:
1869 break;
1871 case SIOCSIFFLAGS:
1872 error = priv_check_cred(cred, PRIV_ROOT, 0);
1873 if (error)
1874 break;
1875 new_flags = (ifr->ifr_flags & 0xffff) |
1876 (ifr->ifr_flagshigh << 16);
1877 if (ifp->if_flags & IFF_SMART) {
1878 /* Smart drivers twiddle their own routes */
1879 } else if (ifp->if_flags & IFF_UP &&
1880 (new_flags & IFF_UP) == 0) {
1881 crit_enter();
1882 if_down(ifp);
1883 crit_exit();
1884 } else if (new_flags & IFF_UP &&
1885 (ifp->if_flags & IFF_UP) == 0) {
1886 crit_enter();
1887 if_up(ifp);
1888 crit_exit();
1891 #ifdef IFPOLL_ENABLE
1892 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1893 if (new_flags & IFF_NPOLLING)
1894 ifpoll_register(ifp);
1895 else
1896 ifpoll_deregister(ifp);
1898 #endif
1900 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1901 (new_flags &~ IFF_CANTCHANGE);
1902 if (new_flags & IFF_PPROMISC) {
1903 /* Permanently promiscuous mode requested */
1904 ifp->if_flags |= IFF_PROMISC;
1905 } else if (ifp->if_pcount == 0) {
1906 ifp->if_flags &= ~IFF_PROMISC;
1908 if (ifp->if_ioctl) {
1909 ifnet_serialize_all(ifp);
1910 ifp->if_ioctl(ifp, cmd, data, cred);
1911 ifnet_deserialize_all(ifp);
1913 getmicrotime(&ifp->if_lastchange);
1914 break;
1916 case SIOCSIFCAP:
1917 error = priv_check_cred(cred, PRIV_ROOT, 0);
1918 if (error)
1919 break;
1920 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1921 error = EINVAL;
1922 break;
1924 ifnet_serialize_all(ifp);
1925 ifp->if_ioctl(ifp, cmd, data, cred);
1926 ifnet_deserialize_all(ifp);
1927 break;
1929 case SIOCSIFNAME:
1930 error = priv_check_cred(cred, PRIV_ROOT, 0);
1931 if (error)
1932 break;
1933 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1934 if (error)
1935 break;
1936 if (new_name[0] == '\0') {
1937 error = EINVAL;
1938 break;
1940 if (ifunit(new_name) != NULL) {
1941 error = EEXIST;
1942 break;
1945 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1947 /* Announce the departure of the interface. */
1948 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1950 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1951 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1952 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1953 namelen = strlen(new_name);
1954 onamelen = sdl->sdl_nlen;
1956 * Move the address if needed. This is safe because we
1957 * allocate space for a name of length IFNAMSIZ when we
1958 * create this in if_attach().
1960 if (namelen != onamelen) {
1961 bcopy(sdl->sdl_data + onamelen,
1962 sdl->sdl_data + namelen, sdl->sdl_alen);
1964 bcopy(new_name, sdl->sdl_data, namelen);
1965 sdl->sdl_nlen = namelen;
1966 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1967 bzero(sdl->sdl_data, onamelen);
1968 while (namelen != 0)
1969 sdl->sdl_data[--namelen] = 0xff;
1971 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1973 /* Announce the return of the interface. */
1974 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1975 break;
1977 case SIOCSIFMETRIC:
1978 error = priv_check_cred(cred, PRIV_ROOT, 0);
1979 if (error)
1980 break;
1981 ifp->if_metric = ifr->ifr_metric;
1982 getmicrotime(&ifp->if_lastchange);
1983 break;
1985 case SIOCSIFPHYS:
1986 error = priv_check_cred(cred, PRIV_ROOT, 0);
1987 if (error)
1988 break;
1989 if (ifp->if_ioctl == NULL) {
1990 error = EOPNOTSUPP;
1991 break;
1993 ifnet_serialize_all(ifp);
1994 error = ifp->if_ioctl(ifp, cmd, data, cred);
1995 ifnet_deserialize_all(ifp);
1996 if (error == 0)
1997 getmicrotime(&ifp->if_lastchange);
1998 break;
2000 case SIOCSIFMTU:
2002 u_long oldmtu = ifp->if_mtu;
2004 error = priv_check_cred(cred, PRIV_ROOT, 0);
2005 if (error)
2006 break;
2007 if (ifp->if_ioctl == NULL) {
2008 error = EOPNOTSUPP;
2009 break;
2011 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2012 error = EINVAL;
2013 break;
2015 ifnet_serialize_all(ifp);
2016 error = ifp->if_ioctl(ifp, cmd, data, cred);
2017 ifnet_deserialize_all(ifp);
2018 if (error == 0) {
2019 getmicrotime(&ifp->if_lastchange);
2020 rt_ifmsg(ifp);
2023 * If the link MTU changed, do network layer specific procedure.
2025 if (ifp->if_mtu != oldmtu) {
2026 #ifdef INET6
2027 nd6_setmtu(ifp);
2028 #endif
2030 break;
2033 case SIOCSIFTSOLEN:
2034 error = priv_check_cred(cred, PRIV_ROOT, 0);
2035 if (error)
2036 break;
2038 /* XXX need driver supplied upper limit */
2039 if (ifr->ifr_tsolen <= 0) {
2040 error = EINVAL;
2041 break;
2043 ifp->if_tsolen = ifr->ifr_tsolen;
2044 break;
2046 case SIOCADDMULTI:
2047 case SIOCDELMULTI:
2048 error = priv_check_cred(cred, PRIV_ROOT, 0);
2049 if (error)
2050 break;
2052 /* Don't allow group membership on non-multicast interfaces. */
2053 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2054 error = EOPNOTSUPP;
2055 break;
2058 /* Don't let users screw up protocols' entries. */
2059 if (ifr->ifr_addr.sa_family != AF_LINK) {
2060 error = EINVAL;
2061 break;
2064 if (cmd == SIOCADDMULTI) {
2065 struct ifmultiaddr *ifma;
2066 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2067 } else {
2068 error = if_delmulti(ifp, &ifr->ifr_addr);
2070 if (error == 0)
2071 getmicrotime(&ifp->if_lastchange);
2072 break;
2074 case SIOCSIFPHYADDR:
2075 case SIOCDIFPHYADDR:
2076 #ifdef INET6
2077 case SIOCSIFPHYADDR_IN6:
2078 #endif
2079 case SIOCSLIFPHYADDR:
2080 case SIOCSIFMEDIA:
2081 case SIOCSIFGENERIC:
2082 error = priv_check_cred(cred, PRIV_ROOT, 0);
2083 if (error)
2084 break;
2085 if (ifp->if_ioctl == 0) {
2086 error = EOPNOTSUPP;
2087 break;
2089 ifnet_serialize_all(ifp);
2090 error = ifp->if_ioctl(ifp, cmd, data, cred);
2091 ifnet_deserialize_all(ifp);
2092 if (error == 0)
2093 getmicrotime(&ifp->if_lastchange);
2094 break;
2096 case SIOCGIFSTATUS:
2097 ifs = (struct ifstat *)data;
2098 ifs->ascii[0] = '\0';
2099 /* fall through */
2100 case SIOCGIFPSRCADDR:
2101 case SIOCGIFPDSTADDR:
2102 case SIOCGLIFPHYADDR:
2103 case SIOCGIFMEDIA:
2104 case SIOCGIFGENERIC:
2105 if (ifp->if_ioctl == NULL) {
2106 error = EOPNOTSUPP;
2107 break;
2109 ifnet_serialize_all(ifp);
2110 error = ifp->if_ioctl(ifp, cmd, data, cred);
2111 ifnet_deserialize_all(ifp);
2112 break;
2114 case SIOCSIFLLADDR:
2115 error = priv_check_cred(cred, PRIV_ROOT, 0);
2116 if (error)
2117 break;
2118 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
2119 ifr->ifr_addr.sa_len);
2120 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2121 break;
2123 default:
2124 oif_flags = ifp->if_flags;
2125 if (so->so_proto == 0) {
2126 error = EOPNOTSUPP;
2127 break;
2129 error = so_pru_control_direct(so, cmd, data, ifp);
2131 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2132 #ifdef INET6
2133 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2134 if (ifp->if_flags & IFF_UP) {
2135 crit_enter();
2136 in6_if_up(ifp);
2137 crit_exit();
2139 #endif
2141 break;
2144 ifnet_unlock();
2145 return (error);
2149 * Set/clear promiscuous mode on interface ifp based on the truth value
2150 * of pswitch. The calls are reference counted so that only the first
2151 * "on" request actually has an effect, as does the final "off" request.
2152 * Results are undefined if the "off" and "on" requests are not matched.
2155 ifpromisc(struct ifnet *ifp, int pswitch)
2157 struct ifreq ifr;
2158 int error;
2159 int oldflags;
2161 oldflags = ifp->if_flags;
2162 if (ifp->if_flags & IFF_PPROMISC) {
2163 /* Do nothing if device is in permanently promiscuous mode */
2164 ifp->if_pcount += pswitch ? 1 : -1;
2165 return (0);
2167 if (pswitch) {
2169 * If the device is not configured up, we cannot put it in
2170 * promiscuous mode.
2172 if ((ifp->if_flags & IFF_UP) == 0)
2173 return (ENETDOWN);
2174 if (ifp->if_pcount++ != 0)
2175 return (0);
2176 ifp->if_flags |= IFF_PROMISC;
2177 log(LOG_INFO, "%s: promiscuous mode enabled\n",
2178 ifp->if_xname);
2179 } else {
2180 if (--ifp->if_pcount > 0)
2181 return (0);
2182 ifp->if_flags &= ~IFF_PROMISC;
2183 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2184 ifp->if_xname);
2186 ifr.ifr_flags = ifp->if_flags;
2187 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2188 ifnet_serialize_all(ifp);
2189 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2190 ifnet_deserialize_all(ifp);
2191 if (error == 0)
2192 rt_ifmsg(ifp);
2193 else
2194 ifp->if_flags = oldflags;
2195 return error;
2199 * Return interface configuration
2200 * of system. List may be used
2201 * in later ioctl's (above) to get
2202 * other information.
2204 static int
2205 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2207 struct ifconf *ifc = (struct ifconf *)data;
2208 struct ifnet *ifp;
2209 struct sockaddr *sa;
2210 struct ifreq ifr, *ifrp;
2211 int space = ifc->ifc_len, error = 0;
2213 ifrp = ifc->ifc_req;
2215 ifnet_lock();
2216 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2217 struct ifaddr_container *ifac, *ifac_mark;
2218 struct ifaddr_marker mark;
2219 struct ifaddrhead *head;
2220 int addrs;
2222 if (space <= sizeof ifr)
2223 break;
2226 * Zero the stack declared structure first to prevent
2227 * memory disclosure.
2229 bzero(&ifr, sizeof(ifr));
2230 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2231 >= sizeof(ifr.ifr_name)) {
2232 error = ENAMETOOLONG;
2233 break;
2237 * Add a marker, since copyout() could block and during that
2238 * period the list could be changed. Inserting the marker to
2239 * the header of the list will not cause trouble for the code
2240 * assuming that the first element of the list is AF_LINK; the
2241 * marker will be moved to the next position w/o blocking.
2243 ifa_marker_init(&mark, ifp);
2244 ifac_mark = &mark.ifac;
2245 head = &ifp->if_addrheads[mycpuid];
2247 addrs = 0;
2248 TAILQ_INSERT_HEAD(head, ifac_mark, ifa_link);
2249 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
2250 struct ifaddr *ifa = ifac->ifa;
2252 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2253 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
2255 /* Ignore marker */
2256 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
2257 continue;
2259 if (space <= sizeof ifr)
2260 break;
2261 sa = ifa->ifa_addr;
2262 if (cred->cr_prison &&
2263 prison_if(cred, sa))
2264 continue;
2265 addrs++;
2267 * Keep a reference on this ifaddr, so that it will
2268 * not be destroyed when its address is copied to
2269 * the userland, which could block.
2271 IFAREF(ifa);
2272 if (sa->sa_len <= sizeof(*sa)) {
2273 ifr.ifr_addr = *sa;
2274 error = copyout(&ifr, ifrp, sizeof ifr);
2275 ifrp++;
2276 } else {
2277 if (space < (sizeof ifr) + sa->sa_len -
2278 sizeof(*sa)) {
2279 IFAFREE(ifa);
2280 break;
2282 space -= sa->sa_len - sizeof(*sa);
2283 error = copyout(&ifr, ifrp,
2284 sizeof ifr.ifr_name);
2285 if (error == 0)
2286 error = copyout(sa, &ifrp->ifr_addr,
2287 sa->sa_len);
2288 ifrp = (struct ifreq *)
2289 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2291 IFAFREE(ifa);
2292 if (error)
2293 break;
2294 space -= sizeof ifr;
2296 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2297 if (error)
2298 break;
2299 if (!addrs) {
2300 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2301 error = copyout(&ifr, ifrp, sizeof ifr);
2302 if (error)
2303 break;
2304 space -= sizeof ifr;
2305 ifrp++;
2308 ifnet_unlock();
2310 ifc->ifc_len -= space;
2311 return (error);
2315 * Just like if_promisc(), but for all-multicast-reception mode.
2318 if_allmulti(struct ifnet *ifp, int onswitch)
2320 int error = 0;
2321 struct ifreq ifr;
2323 crit_enter();
2325 if (onswitch) {
2326 if (ifp->if_amcount++ == 0) {
2327 ifp->if_flags |= IFF_ALLMULTI;
2328 ifr.ifr_flags = ifp->if_flags;
2329 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2330 ifnet_serialize_all(ifp);
2331 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2332 NULL);
2333 ifnet_deserialize_all(ifp);
2335 } else {
2336 if (ifp->if_amcount > 1) {
2337 ifp->if_amcount--;
2338 } else {
2339 ifp->if_amcount = 0;
2340 ifp->if_flags &= ~IFF_ALLMULTI;
2341 ifr.ifr_flags = ifp->if_flags;
2342 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2343 ifnet_serialize_all(ifp);
2344 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2345 NULL);
2346 ifnet_deserialize_all(ifp);
2350 crit_exit();
2352 if (error == 0)
2353 rt_ifmsg(ifp);
2354 return error;
2358 * Add a multicast listenership to the interface in question.
2359 * The link layer provides a routine which converts
2362 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2363 struct ifmultiaddr **retifma)
2365 struct sockaddr *llsa, *dupsa;
2366 int error;
2367 struct ifmultiaddr *ifma;
2369 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2372 * If the matching multicast address already exists
2373 * then don't add a new one, just add a reference
2375 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2376 if (sa_equal(sa, ifma->ifma_addr)) {
2377 ifma->ifma_refcount++;
2378 if (retifma)
2379 *retifma = ifma;
2380 return 0;
2385 * Give the link layer a chance to accept/reject it, and also
2386 * find out which AF_LINK address this maps to, if it isn't one
2387 * already.
2389 if (ifp->if_resolvemulti) {
2390 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2391 if (error)
2392 return error;
2393 } else {
2394 llsa = NULL;
2397 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2398 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_INTWAIT);
2399 bcopy(sa, dupsa, sa->sa_len);
2401 ifma->ifma_addr = dupsa;
2402 ifma->ifma_lladdr = llsa;
2403 ifma->ifma_ifp = ifp;
2404 ifma->ifma_refcount = 1;
2405 ifma->ifma_protospec = NULL;
2406 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2408 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2409 if (retifma)
2410 *retifma = ifma;
2412 if (llsa != NULL) {
2413 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2414 if (sa_equal(ifma->ifma_addr, llsa))
2415 break;
2417 if (ifma) {
2418 ifma->ifma_refcount++;
2419 } else {
2420 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2421 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_INTWAIT);
2422 bcopy(llsa, dupsa, llsa->sa_len);
2423 ifma->ifma_addr = dupsa;
2424 ifma->ifma_ifp = ifp;
2425 ifma->ifma_refcount = 1;
2426 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2430 * We are certain we have added something, so call down to the
2431 * interface to let them know about it.
2433 if (ifp->if_ioctl)
2434 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2436 return 0;
2440 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2441 struct ifmultiaddr **retifma)
2443 int error;
2445 ifnet_serialize_all(ifp);
2446 error = if_addmulti_serialized(ifp, sa, retifma);
2447 ifnet_deserialize_all(ifp);
2449 return error;
2453 * Remove a reference to a multicast address on this interface. Yell
2454 * if the request does not match an existing membership.
2456 static int
2457 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2459 struct ifmultiaddr *ifma;
2461 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2463 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2464 if (sa_equal(sa, ifma->ifma_addr))
2465 break;
2466 if (ifma == NULL)
2467 return ENOENT;
2469 if (ifma->ifma_refcount > 1) {
2470 ifma->ifma_refcount--;
2471 return 0;
2474 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2475 sa = ifma->ifma_lladdr;
2476 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2478 * Make sure the interface driver is notified
2479 * in the case of a link layer mcast group being left.
2481 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2482 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2483 kfree(ifma->ifma_addr, M_IFMADDR);
2484 kfree(ifma, M_IFMADDR);
2485 if (sa == NULL)
2486 return 0;
2489 * Now look for the link-layer address which corresponds to
2490 * this network address. It had been squirreled away in
2491 * ifma->ifma_lladdr for this purpose (so we don't have
2492 * to call ifp->if_resolvemulti() again), and we saved that
2493 * value in sa above. If some nasty deleted the
2494 * link-layer address out from underneath us, we can deal because
2495 * the address we stored was is not the same as the one which was
2496 * in the record for the link-layer address. (So we don't complain
2497 * in that case.)
2499 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2500 if (sa_equal(sa, ifma->ifma_addr))
2501 break;
2502 if (ifma == NULL)
2503 return 0;
2505 if (ifma->ifma_refcount > 1) {
2506 ifma->ifma_refcount--;
2507 return 0;
2510 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2511 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2512 kfree(ifma->ifma_addr, M_IFMADDR);
2513 kfree(sa, M_IFMADDR);
2514 kfree(ifma, M_IFMADDR);
2516 return 0;
2520 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2522 int error;
2524 ifnet_serialize_all(ifp);
2525 error = if_delmulti_serialized(ifp, sa);
2526 ifnet_deserialize_all(ifp);
2528 return error;
2532 * Delete all multicast group membership for an interface.
2533 * Should be used to quickly flush all multicast filters.
2535 void
2536 if_delallmulti_serialized(struct ifnet *ifp)
2538 struct ifmultiaddr *ifma, mark;
2539 struct sockaddr sa;
2541 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2543 bzero(&sa, sizeof(sa));
2544 sa.sa_family = AF_UNSPEC;
2545 sa.sa_len = sizeof(sa);
2547 bzero(&mark, sizeof(mark));
2548 mark.ifma_addr = &sa;
2550 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2551 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2552 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2553 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2554 ifma_link);
2556 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2557 continue;
2559 if_delmulti_serialized(ifp, ifma->ifma_addr);
2561 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2566 * Set the link layer address on an interface.
2568 * At this time we only support certain types of interfaces,
2569 * and we don't allow the length of the address to change.
2572 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2574 struct sockaddr_dl *sdl;
2575 struct ifreq ifr;
2577 sdl = IF_LLSOCKADDR(ifp);
2578 if (sdl == NULL)
2579 return (EINVAL);
2580 if (len != sdl->sdl_alen) /* don't allow length to change */
2581 return (EINVAL);
2582 switch (ifp->if_type) {
2583 case IFT_ETHER: /* these types use struct arpcom */
2584 case IFT_XETHER:
2585 case IFT_L2VLAN:
2586 case IFT_IEEE8023ADLAG:
2587 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2588 bcopy(lladdr, LLADDR(sdl), len);
2589 break;
2590 default:
2591 return (ENODEV);
2594 * If the interface is already up, we need
2595 * to re-init it in order to reprogram its
2596 * address filter.
2598 ifnet_serialize_all(ifp);
2599 if ((ifp->if_flags & IFF_UP) != 0) {
2600 #ifdef INET
2601 struct ifaddr_container *ifac;
2602 #endif
2604 ifp->if_flags &= ~IFF_UP;
2605 ifr.ifr_flags = ifp->if_flags;
2606 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2607 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2608 NULL);
2609 ifp->if_flags |= IFF_UP;
2610 ifr.ifr_flags = ifp->if_flags;
2611 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2612 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2613 NULL);
2614 #ifdef INET
2616 * Also send gratuitous ARPs to notify other nodes about
2617 * the address change.
2619 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2620 struct ifaddr *ifa = ifac->ifa;
2622 if (ifa->ifa_addr != NULL &&
2623 ifa->ifa_addr->sa_family == AF_INET)
2624 arp_gratuitous(ifp, ifa);
2626 #endif
2628 ifnet_deserialize_all(ifp);
2629 return (0);
2632 struct ifmultiaddr *
2633 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2635 struct ifmultiaddr *ifma;
2637 /* TODO: need ifnet_serialize_main */
2638 ifnet_serialize_all(ifp);
2639 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2640 if (sa_equal(ifma->ifma_addr, sa))
2641 break;
2642 ifnet_deserialize_all(ifp);
2644 return ifma;
2648 * This function locates the first real ethernet MAC from a network
2649 * card and loads it into node, returning 0 on success or ENOENT if
2650 * no suitable interfaces were found. It is used by the uuid code to
2651 * generate a unique 6-byte number.
2654 if_getanyethermac(uint16_t *node, int minlen)
2656 struct ifnet *ifp;
2657 struct sockaddr_dl *sdl;
2659 ifnet_lock();
2660 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2661 if (ifp->if_type != IFT_ETHER)
2662 continue;
2663 sdl = IF_LLSOCKADDR(ifp);
2664 if (sdl->sdl_alen < minlen)
2665 continue;
2666 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2667 minlen);
2668 ifnet_unlock();
2669 return(0);
2671 ifnet_unlock();
2672 return (ENOENT);
2676 * The name argument must be a pointer to storage which will last as
2677 * long as the interface does. For physical devices, the result of
2678 * device_get_name(dev) is a good choice and for pseudo-devices a
2679 * static string works well.
2681 void
2682 if_initname(struct ifnet *ifp, const char *name, int unit)
2684 ifp->if_dname = name;
2685 ifp->if_dunit = unit;
2686 if (unit != IF_DUNIT_NONE)
2687 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2688 else
2689 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2693 if_printf(struct ifnet *ifp, const char *fmt, ...)
2695 __va_list ap;
2696 int retval;
2698 retval = kprintf("%s: ", ifp->if_xname);
2699 __va_start(ap, fmt);
2700 retval += kvprintf(fmt, ap);
2701 __va_end(ap);
2702 return (retval);
2705 struct ifnet *
2706 if_alloc(uint8_t type)
2708 struct ifnet *ifp;
2709 size_t size;
2712 * XXX temporary hack until arpcom is setup in if_l2com
2714 if (type == IFT_ETHER)
2715 size = sizeof(struct arpcom);
2716 else
2717 size = sizeof(struct ifnet);
2719 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2721 ifp->if_type = type;
2723 if (if_com_alloc[type] != NULL) {
2724 ifp->if_l2com = if_com_alloc[type](type, ifp);
2725 if (ifp->if_l2com == NULL) {
2726 kfree(ifp, M_IFNET);
2727 return (NULL);
2730 return (ifp);
2733 void
2734 if_free(struct ifnet *ifp)
2736 kfree(ifp, M_IFNET);
2739 void
2740 ifq_set_classic(struct ifaltq *ifq)
2742 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2743 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2746 void
2747 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2748 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2750 int q;
2752 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2753 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2754 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2755 KASSERT(request != NULL, ("request is not specified"));
2757 ifq->altq_mapsubq = mapsubq;
2758 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2759 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2761 ifsq->ifsq_enqueue = enqueue;
2762 ifsq->ifsq_dequeue = dequeue;
2763 ifsq->ifsq_request = request;
2767 static void
2768 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2770 m->m_nextpkt = NULL;
2771 if (ifsq->ifsq_norm_tail == NULL)
2772 ifsq->ifsq_norm_head = m;
2773 else
2774 ifsq->ifsq_norm_tail->m_nextpkt = m;
2775 ifsq->ifsq_norm_tail = m;
2776 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2779 static void
2780 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2782 m->m_nextpkt = NULL;
2783 if (ifsq->ifsq_prio_tail == NULL)
2784 ifsq->ifsq_prio_head = m;
2785 else
2786 ifsq->ifsq_prio_tail->m_nextpkt = m;
2787 ifsq->ifsq_prio_tail = m;
2788 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2789 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2792 static struct mbuf *
2793 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2795 struct mbuf *m;
2797 m = ifsq->ifsq_norm_head;
2798 if (m != NULL) {
2799 if ((ifsq->ifsq_norm_head = m->m_nextpkt) == NULL)
2800 ifsq->ifsq_norm_tail = NULL;
2801 m->m_nextpkt = NULL;
2802 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2804 return m;
2807 static struct mbuf *
2808 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2810 struct mbuf *m;
2812 m = ifsq->ifsq_prio_head;
2813 if (m != NULL) {
2814 if ((ifsq->ifsq_prio_head = m->m_nextpkt) == NULL)
2815 ifsq->ifsq_prio_tail = NULL;
2816 m->m_nextpkt = NULL;
2817 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2818 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2820 return m;
2824 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2825 struct altq_pktattr *pa __unused)
2827 M_ASSERTPKTHDR(m);
2828 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2829 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
2830 if ((m->m_flags & M_PRIO) &&
2831 ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen / 2) &&
2832 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt / 2)) {
2833 struct mbuf *m_drop;
2836 * Perform drop-head on normal queue
2838 m_drop = ifsq_norm_dequeue(ifsq);
2839 if (m_drop != NULL) {
2840 m_freem(m_drop);
2841 ifsq_prio_enqueue(ifsq, m);
2842 return 0;
2844 /* XXX nothing could be dropped? */
2846 m_freem(m);
2847 return ENOBUFS;
2848 } else {
2849 if (m->m_flags & M_PRIO)
2850 ifsq_prio_enqueue(ifsq, m);
2851 else
2852 ifsq_norm_enqueue(ifsq, m);
2853 return 0;
2857 struct mbuf *
2858 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
2860 struct mbuf *m;
2862 switch (op) {
2863 case ALTDQ_POLL:
2864 m = ifsq->ifsq_prio_head;
2865 if (m == NULL)
2866 m = ifsq->ifsq_norm_head;
2867 break;
2869 case ALTDQ_REMOVE:
2870 m = ifsq_prio_dequeue(ifsq);
2871 if (m == NULL)
2872 m = ifsq_norm_dequeue(ifsq);
2873 break;
2875 default:
2876 panic("unsupported ALTQ dequeue op: %d", op);
2878 return m;
2882 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2884 switch (req) {
2885 case ALTRQ_PURGE:
2886 for (;;) {
2887 struct mbuf *m;
2889 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
2890 if (m == NULL)
2891 break;
2892 m_freem(m);
2894 break;
2896 default:
2897 panic("unsupported ALTQ request: %d", req);
2899 return 0;
2902 static void
2903 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2905 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2906 int running = 0, need_sched;
2909 * Try to do direct ifnet.if_start on the subqueue first, if there is
2910 * contention on the subqueue hardware serializer, ifnet.if_start on
2911 * the subqueue will be scheduled on the subqueue owner CPU.
2913 if (!ifsq_tryserialize_hw(ifsq)) {
2915 * Subqueue hardware serializer contention happened,
2916 * ifnet.if_start on the subqueue is scheduled on
2917 * the subqueue owner CPU, and we keep going.
2919 ifsq_ifstart_schedule(ifsq, 1);
2920 return;
2923 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2924 ifp->if_start(ifp, ifsq);
2925 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2926 running = 1;
2928 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2930 ifsq_deserialize_hw(ifsq);
2932 if (need_sched) {
2934 * More data need to be transmitted, ifnet.if_start on the
2935 * subqueue is scheduled on the subqueue owner CPU, and we
2936 * keep going.
2937 * NOTE: ifnet.if_start subqueue interlock is not released.
2939 ifsq_ifstart_schedule(ifsq, force_sched);
2944 * Subqeue packets staging mechanism:
2946 * The packets enqueued into the subqueue are staged to a certain amount
2947 * before the ifnet.if_start on the subqueue is called. In this way, the
2948 * driver could avoid writing to hardware registers upon every packet,
2949 * instead, hardware registers could be written when certain amount of
2950 * packets are put onto hardware TX ring. The measurement on several modern
2951 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2952 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2953 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2954 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2955 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2957 * Subqueue packets staging is performed for two entry points into drivers'
2958 * transmission function:
2959 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2960 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2962 * Subqueue packets staging will be stopped upon any of the following
2963 * conditions:
2964 * - If the count of packets enqueued on the current CPU is great than or
2965 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2966 * - If the total length of packets enqueued on the current CPU is great
2967 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2968 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2969 * is usually less than hardware's MTU.
2970 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2971 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2972 * released.
2973 * - The if_start_rollup(), which is registered as low priority netisr
2974 * rollup function, is called; probably because no more work is pending
2975 * for netisr.
2977 * NOTE:
2978 * Currently subqueue packet staging is only performed in netisr threads.
2981 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2983 struct ifaltq *ifq = &ifp->if_snd;
2984 struct ifaltq_subque *ifsq;
2985 int error, start = 0, len, mcast = 0, avoid_start = 0;
2986 struct ifsubq_stage_head *head = NULL;
2987 struct ifsubq_stage *stage = NULL;
2988 struct globaldata *gd = mycpu;
2989 struct thread *td = gd->gd_curthread;
2991 crit_enter_quick(td);
2993 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
2994 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
2996 len = m->m_pkthdr.len;
2997 if (m->m_flags & M_MCAST)
2998 mcast = 1;
3000 if (td->td_type == TD_TYPE_NETISR) {
3001 head = &ifsubq_stage_heads[mycpuid];
3002 stage = ifsq_get_stage(ifsq, mycpuid);
3004 stage->stg_cnt++;
3005 stage->stg_len += len;
3006 if (stage->stg_cnt < ifsq_stage_cntmax &&
3007 stage->stg_len < (ifp->if_mtu - max_protohdr))
3008 avoid_start = 1;
3011 ALTQ_SQ_LOCK(ifsq);
3012 error = ifsq_enqueue_locked(ifsq, m, pa);
3013 if (error) {
3014 IFNET_STAT_INC(ifp, oqdrops, 1);
3015 if (!ifsq_data_ready(ifsq)) {
3016 ALTQ_SQ_UNLOCK(ifsq);
3017 crit_exit_quick(td);
3018 return error;
3020 avoid_start = 0;
3022 if (!ifsq_is_started(ifsq)) {
3023 if (avoid_start) {
3024 ALTQ_SQ_UNLOCK(ifsq);
3026 KKASSERT(!error);
3027 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
3028 ifsq_stage_insert(head, stage);
3030 IFNET_STAT_INC(ifp, obytes, len);
3031 if (mcast)
3032 IFNET_STAT_INC(ifp, omcasts, 1);
3033 crit_exit_quick(td);
3034 return error;
3038 * Hold the subqueue interlock of ifnet.if_start
3040 ifsq_set_started(ifsq);
3041 start = 1;
3043 ALTQ_SQ_UNLOCK(ifsq);
3045 if (!error) {
3046 IFNET_STAT_INC(ifp, obytes, len);
3047 if (mcast)
3048 IFNET_STAT_INC(ifp, omcasts, 1);
3051 if (stage != NULL) {
3052 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
3053 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3054 if (!avoid_start) {
3055 ifsq_stage_remove(head, stage);
3056 ifsq_ifstart_schedule(ifsq, 1);
3058 crit_exit_quick(td);
3059 return error;
3062 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
3063 ifsq_stage_remove(head, stage);
3064 } else {
3065 stage->stg_cnt = 0;
3066 stage->stg_len = 0;
3070 if (!start) {
3071 crit_exit_quick(td);
3072 return error;
3075 ifsq_ifstart_try(ifsq, 0);
3077 crit_exit_quick(td);
3078 return error;
3081 void *
3082 ifa_create(int size)
3084 struct ifaddr *ifa;
3085 int i;
3087 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
3089 ifa = kmalloc(size, M_IFADDR, M_INTWAIT | M_ZERO);
3090 ifa->ifa_containers =
3091 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
3092 M_IFADDR, M_INTWAIT | M_ZERO);
3094 ifa->ifa_ncnt = ncpus;
3095 for (i = 0; i < ncpus; ++i) {
3096 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
3098 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
3099 ifac->ifa = ifa;
3100 ifac->ifa_refcnt = 1;
3102 #ifdef IFADDR_DEBUG
3103 kprintf("alloc ifa %p %d\n", ifa, size);
3104 #endif
3105 return ifa;
3108 void
3109 ifac_free(struct ifaddr_container *ifac, int cpu_id)
3111 struct ifaddr *ifa = ifac->ifa;
3113 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
3114 KKASSERT(ifac->ifa_refcnt == 0);
3115 KASSERT(ifac->ifa_listmask == 0,
3116 ("ifa is still on %#x lists", ifac->ifa_listmask));
3118 ifac->ifa_magic = IFA_CONTAINER_DEAD;
3120 #ifdef IFADDR_DEBUG_VERBOSE
3121 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
3122 #endif
3124 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
3125 ("invalid # of ifac, %d", ifa->ifa_ncnt));
3126 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
3127 #ifdef IFADDR_DEBUG
3128 kprintf("free ifa %p\n", ifa);
3129 #endif
3130 kfree(ifa->ifa_containers, M_IFADDR);
3131 kfree(ifa, M_IFADDR);
3135 static void
3136 ifa_iflink_dispatch(netmsg_t nmsg)
3138 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3139 struct ifaddr *ifa = msg->ifa;
3140 struct ifnet *ifp = msg->ifp;
3141 int cpu = mycpuid;
3142 struct ifaddr_container *ifac;
3144 crit_enter();
3146 ifac = &ifa->ifa_containers[cpu];
3147 ASSERT_IFAC_VALID(ifac);
3148 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
3149 ("ifaddr is on if_addrheads"));
3151 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
3152 if (msg->tail)
3153 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
3154 else
3155 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
3157 crit_exit();
3159 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
3162 void
3163 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
3165 struct netmsg_ifaddr msg;
3167 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3168 0, ifa_iflink_dispatch);
3169 msg.ifa = ifa;
3170 msg.ifp = ifp;
3171 msg.tail = tail;
3173 ifa_domsg(&msg.base.lmsg, 0);
3176 static void
3177 ifa_ifunlink_dispatch(netmsg_t nmsg)
3179 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3180 struct ifaddr *ifa = msg->ifa;
3181 struct ifnet *ifp = msg->ifp;
3182 int cpu = mycpuid;
3183 struct ifaddr_container *ifac;
3185 crit_enter();
3187 ifac = &ifa->ifa_containers[cpu];
3188 ASSERT_IFAC_VALID(ifac);
3189 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3190 ("ifaddr is not on if_addrhead"));
3192 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3193 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3195 crit_exit();
3197 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
3200 void
3201 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3203 struct netmsg_ifaddr msg;
3205 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3206 0, ifa_ifunlink_dispatch);
3207 msg.ifa = ifa;
3208 msg.ifp = ifp;
3210 ifa_domsg(&msg.base.lmsg, 0);
3213 static void
3214 ifa_destroy_dispatch(netmsg_t nmsg)
3216 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3218 IFAFREE(msg->ifa);
3219 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
3222 void
3223 ifa_destroy(struct ifaddr *ifa)
3225 struct netmsg_ifaddr msg;
3227 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3228 0, ifa_destroy_dispatch);
3229 msg.ifa = ifa;
3231 ifa_domsg(&msg.base.lmsg, 0);
3234 struct lwkt_port *
3235 ifnet_portfn(int cpu)
3237 return &ifnet_threads[cpu]->td_msgport;
3240 void
3241 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
3243 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
3245 if (next_cpu < ncpus)
3246 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
3247 else
3248 lwkt_replymsg(lmsg, 0);
3252 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
3254 KKASSERT(cpu < ncpus);
3255 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
3258 void
3259 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
3261 KKASSERT(cpu < ncpus);
3262 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
3266 * Generic netmsg service loop. Some protocols may roll their own but all
3267 * must do the basic command dispatch function call done here.
3269 static void
3270 ifnet_service_loop(void *arg __unused)
3272 netmsg_t msg;
3274 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
3275 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
3276 msg->base.nm_dispatch(msg);
3280 static void
3281 if_start_rollup(void)
3283 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3284 struct ifsubq_stage *stage;
3286 crit_enter();
3288 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3289 struct ifaltq_subque *ifsq = stage->stg_subq;
3290 int is_sched = 0;
3292 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3293 is_sched = 1;
3294 ifsq_stage_remove(head, stage);
3296 if (is_sched) {
3297 ifsq_ifstart_schedule(ifsq, 1);
3298 } else {
3299 int start = 0;
3301 ALTQ_SQ_LOCK(ifsq);
3302 if (!ifsq_is_started(ifsq)) {
3304 * Hold the subqueue interlock of
3305 * ifnet.if_start
3307 ifsq_set_started(ifsq);
3308 start = 1;
3310 ALTQ_SQ_UNLOCK(ifsq);
3312 if (start)
3313 ifsq_ifstart_try(ifsq, 1);
3315 KKASSERT((stage->stg_flags &
3316 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3319 crit_exit();
3322 static void
3323 ifnetinit(void *dummy __unused)
3325 int i;
3327 for (i = 0; i < ncpus; ++i) {
3328 struct thread **thr = &ifnet_threads[i];
3330 lwkt_create(ifnet_service_loop, NULL, thr, NULL,
3331 TDF_NOSTART|TDF_FORCE_SPINPORT|TDF_FIXEDCPU,
3332 i, "ifnet %d", i);
3333 netmsg_service_port_init(&(*thr)->td_msgport);
3334 lwkt_schedule(*thr);
3337 for (i = 0; i < ncpus; ++i)
3338 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3339 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3342 void
3343 if_register_com_alloc(u_char type,
3344 if_com_alloc_t *a, if_com_free_t *f)
3347 KASSERT(if_com_alloc[type] == NULL,
3348 ("if_register_com_alloc: %d already registered", type));
3349 KASSERT(if_com_free[type] == NULL,
3350 ("if_register_com_alloc: %d free already registered", type));
3352 if_com_alloc[type] = a;
3353 if_com_free[type] = f;
3356 void
3357 if_deregister_com_alloc(u_char type)
3360 KASSERT(if_com_alloc[type] != NULL,
3361 ("if_deregister_com_alloc: %d not registered", type));
3362 KASSERT(if_com_free[type] != NULL,
3363 ("if_deregister_com_alloc: %d free not registered", type));
3364 if_com_alloc[type] = NULL;
3365 if_com_free[type] = NULL;
3369 if_ring_count2(int cnt, int cnt_max)
3371 int shift = 0;
3373 KASSERT(cnt_max >= 1 && powerof2(cnt_max),
3374 ("invalid ring count max %d", cnt_max));
3376 if (cnt <= 0)
3377 cnt = cnt_max;
3378 if (cnt > ncpus2)
3379 cnt = ncpus2;
3380 if (cnt > cnt_max)
3381 cnt = cnt_max;
3383 while ((1 << (shift + 1)) <= cnt)
3384 ++shift;
3385 cnt = 1 << shift;
3387 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
3388 ("calculate cnt %d, ncpus2 %d, cnt max %d",
3389 cnt, ncpus2, cnt_max));
3390 return cnt;
3393 void
3394 ifq_set_maxlen(struct ifaltq *ifq, int len)
3396 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3400 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3402 return ALTQ_SUBQ_INDEX_DEFAULT;
3406 ifq_mapsubq_mask(struct ifaltq *ifq, int cpuid)
3408 return (cpuid & ifq->altq_subq_mask);
3411 static void
3412 ifsq_watchdog(void *arg)
3414 struct ifsubq_watchdog *wd = arg;
3415 struct ifnet *ifp;
3417 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3418 goto done;
3420 ifp = ifsq_get_ifp(wd->wd_subq);
3421 if (ifnet_tryserialize_all(ifp)) {
3422 wd->wd_watchdog(wd->wd_subq);
3423 ifnet_deserialize_all(ifp);
3424 } else {
3425 /* try again next timeout */
3426 wd->wd_timer = 1;
3428 done:
3429 ifsq_watchdog_reset(wd);
3432 static void
3433 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3435 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3436 ifsq_get_cpuid(wd->wd_subq));
3439 void
3440 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3441 ifsq_watchdog_t watchdog)
3443 callout_init_mp(&wd->wd_callout);
3444 wd->wd_timer = 0;
3445 wd->wd_subq = ifsq;
3446 wd->wd_watchdog = watchdog;
3449 void
3450 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3452 wd->wd_timer = 0;
3453 ifsq_watchdog_reset(wd);
3456 void
3457 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3459 wd->wd_timer = 0;
3460 callout_stop(&wd->wd_callout);
3463 void
3464 ifnet_lock(void)
3466 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3467 ("try holding ifnet lock in netisr"));
3468 mtx_lock(&ifnet_mtx);
3471 void
3472 ifnet_unlock(void)
3474 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3475 ("try holding ifnet lock in netisr"));
3476 mtx_unlock(&ifnet_mtx);
3479 static struct ifnet_array *
3480 ifnet_array_alloc(int count)
3482 struct ifnet_array *arr;
3484 arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]),
3485 M_IFNET, M_WAITOK);
3486 arr->ifnet_count = count;
3488 return arr;
3491 static void
3492 ifnet_array_free(struct ifnet_array *arr)
3494 if (arr == &ifnet_array0)
3495 return;
3496 kfree(arr, M_IFNET);
3499 static struct ifnet_array *
3500 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr)
3502 struct ifnet_array *arr;
3503 int count, i;
3505 KASSERT(old_arr->ifnet_count >= 0,
3506 ("invalid ifnet array count %d", old_arr->ifnet_count));
3507 count = old_arr->ifnet_count + 1;
3508 arr = ifnet_array_alloc(count);
3511 * Save the old ifnet array and append this ifp to the end of
3512 * the new ifnet array.
3514 for (i = 0; i < old_arr->ifnet_count; ++i) {
3515 KASSERT(old_arr->ifnet_arr[i] != ifp,
3516 ("%s is already in ifnet array", ifp->if_xname));
3517 arr->ifnet_arr[i] = old_arr->ifnet_arr[i];
3519 KASSERT(i == count - 1,
3520 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3521 ifp->if_xname, count - 1, i));
3522 arr->ifnet_arr[i] = ifp;
3524 return arr;
3527 static struct ifnet_array *
3528 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr)
3530 struct ifnet_array *arr;
3531 int count, i, idx, found = 0;
3533 KASSERT(old_arr->ifnet_count > 0,
3534 ("invalid ifnet array count %d", old_arr->ifnet_count));
3535 count = old_arr->ifnet_count - 1;
3536 arr = ifnet_array_alloc(count);
3539 * Save the old ifnet array, but skip this ifp.
3541 idx = 0;
3542 for (i = 0; i < old_arr->ifnet_count; ++i) {
3543 if (old_arr->ifnet_arr[i] == ifp) {
3544 KASSERT(!found,
3545 ("dup %s is in ifnet array", ifp->if_xname));
3546 found = 1;
3547 continue;
3549 KASSERT(idx < count,
3550 ("invalid ifnet array index %d, count %d", idx, count));
3551 arr->ifnet_arr[idx] = old_arr->ifnet_arr[i];
3552 ++idx;
3554 KASSERT(found, ("%s is not in ifnet array", ifp->if_xname));
3555 KASSERT(idx == count,
3556 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3557 ifp->if_xname, count, idx));
3559 return arr;
3562 const struct ifnet_array *
3563 ifnet_array_get(void)
3565 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3566 return ifnet_array;
3570 ifnet_array_isempty(void)
3572 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3573 if (ifnet_array->ifnet_count == 0)
3574 return 1;
3575 else
3576 return 0;
3579 void
3580 ifa_marker_init(struct ifaddr_marker *mark, struct ifnet *ifp)
3582 struct ifaddr *ifa;
3584 memset(mark, 0, sizeof(*mark));
3585 ifa = &mark->ifa;
3587 mark->ifac.ifa = ifa;
3589 ifa->ifa_addr = &mark->addr;
3590 ifa->ifa_dstaddr = &mark->dstaddr;
3591 ifa->ifa_netmask = &mark->netmask;
3592 ifa->ifa_ifp = ifp;