net: Get rid of ifnet threads.
[dragonfly.git] / sys / net / if.c
bloba67a82a23a2d26e953fd07227fd97a45fe9e2f7c
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 mtx ifnet_mtx = MTX_INITIALIZER("ifnet");
167 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
169 #ifdef notyet
170 #define IFQ_KTR_STRING "ifq=%p"
171 #define IFQ_KTR_ARGS struct ifaltq *ifq
172 #ifndef KTR_IFQ
173 #define KTR_IFQ KTR_ALL
174 #endif
175 KTR_INFO_MASTER(ifq);
176 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
177 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
178 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
180 #define IF_START_KTR_STRING "ifp=%p"
181 #define IF_START_KTR_ARGS struct ifnet *ifp
182 #ifndef KTR_IF_START
183 #define KTR_IF_START KTR_ALL
184 #endif
185 KTR_INFO_MASTER(if_start);
186 KTR_INFO(KTR_IF_START, if_start, run, 0,
187 IF_START_KTR_STRING, IF_START_KTR_ARGS);
188 KTR_INFO(KTR_IF_START, if_start, sched, 1,
189 IF_START_KTR_STRING, IF_START_KTR_ARGS);
190 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
191 IF_START_KTR_STRING, IF_START_KTR_ARGS);
192 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
193 IF_START_KTR_STRING, IF_START_KTR_ARGS);
194 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
195 IF_START_KTR_STRING, IF_START_KTR_ARGS);
196 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
197 #endif
199 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
202 * Network interface utility routines.
204 * Routines with ifa_ifwith* names take sockaddr *'s as
205 * parameters.
207 /* ARGSUSED*/
208 static void
209 ifinit(void *dummy)
211 struct ifnet *ifp;
213 callout_init_mp(&if_slowtimo_timer);
214 netmsg_init(&if_slowtimo_netmsg, NULL, &netisr_adone_rport,
215 MSGF_PRIORITY, if_slowtimo_dispatch);
217 /* XXX is this necessary? */
218 ifnet_lock();
219 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
220 if (ifp->if_snd.altq_maxlen == 0) {
221 if_printf(ifp, "XXX: driver didn't set altq_maxlen\n");
222 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
225 ifnet_unlock();
227 /* Start if_slowtimo */
228 lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg.lmsg);
231 static void
232 ifsq_ifstart_ipifunc(void *arg)
234 struct ifaltq_subque *ifsq = arg;
235 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
237 crit_enter();
238 if (lmsg->ms_flags & MSGF_DONE)
239 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg);
240 crit_exit();
243 static __inline void
244 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
246 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
247 TAILQ_REMOVE(&head->stg_head, stage, stg_link);
248 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
249 stage->stg_cnt = 0;
250 stage->stg_len = 0;
253 static __inline void
254 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
256 KKASSERT((stage->stg_flags &
257 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
258 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
259 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
263 * Schedule ifnet.if_start on the subqueue owner CPU
265 static void
266 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
268 int cpu;
270 if (!force && curthread->td_type == TD_TYPE_NETISR &&
271 ifsq_stage_cntmax > 0) {
272 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
274 stage->stg_cnt = 0;
275 stage->stg_len = 0;
276 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
277 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
278 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
279 return;
282 cpu = ifsq_get_cpuid(ifsq);
283 if (cpu != mycpuid)
284 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
285 else
286 ifsq_ifstart_ipifunc(ifsq);
290 * NOTE:
291 * This function will release ifnet.if_start subqueue interlock,
292 * if ifnet.if_start for the subqueue does not need to be scheduled
294 static __inline int
295 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
297 if (!running || ifsq_is_empty(ifsq)
298 #ifdef ALTQ
299 || ifsq->ifsq_altq->altq_tbr != NULL
300 #endif
302 ALTQ_SQ_LOCK(ifsq);
304 * ifnet.if_start subqueue interlock is released, if:
305 * 1) Hardware can not take any packets, due to
306 * o interface is marked down
307 * o hardware queue is full (ifsq_is_oactive)
308 * Under the second situation, hardware interrupt
309 * or polling(4) will call/schedule ifnet.if_start
310 * on the subqueue when hardware queue is ready
311 * 2) There is no packet in the subqueue.
312 * Further ifq_dispatch or ifq_handoff will call/
313 * schedule ifnet.if_start on the subqueue.
314 * 3) TBR is used and it does not allow further
315 * dequeueing.
316 * TBR callout will call ifnet.if_start on the
317 * subqueue.
319 if (!running || !ifsq_data_ready(ifsq)) {
320 ifsq_clr_started(ifsq);
321 ALTQ_SQ_UNLOCK(ifsq);
322 return 0;
324 ALTQ_SQ_UNLOCK(ifsq);
326 return 1;
329 static void
330 ifsq_ifstart_dispatch(netmsg_t msg)
332 struct lwkt_msg *lmsg = &msg->base.lmsg;
333 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
334 struct ifnet *ifp = ifsq_get_ifp(ifsq);
335 struct globaldata *gd = mycpu;
336 int running = 0, need_sched;
338 crit_enter_gd(gd);
340 lwkt_replymsg(lmsg, 0); /* reply ASAP */
342 if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) {
344 * We need to chase the subqueue owner CPU change.
346 ifsq_ifstart_schedule(ifsq, 1);
347 crit_exit_gd(gd);
348 return;
351 ifsq_serialize_hw(ifsq);
352 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
353 ifp->if_start(ifp, ifsq);
354 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
355 running = 1;
357 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
358 ifsq_deserialize_hw(ifsq);
360 if (need_sched) {
362 * More data need to be transmitted, ifnet.if_start is
363 * scheduled on the subqueue owner CPU, and we keep going.
364 * NOTE: ifnet.if_start subqueue interlock is not released.
366 ifsq_ifstart_schedule(ifsq, 0);
369 crit_exit_gd(gd);
372 /* Device driver ifnet.if_start helper function */
373 void
374 ifsq_devstart(struct ifaltq_subque *ifsq)
376 struct ifnet *ifp = ifsq_get_ifp(ifsq);
377 int running = 0;
379 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
381 ALTQ_SQ_LOCK(ifsq);
382 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
383 ALTQ_SQ_UNLOCK(ifsq);
384 return;
386 ifsq_set_started(ifsq);
387 ALTQ_SQ_UNLOCK(ifsq);
389 ifp->if_start(ifp, ifsq);
391 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
392 running = 1;
394 if (ifsq_ifstart_need_schedule(ifsq, running)) {
396 * More data need to be transmitted, ifnet.if_start is
397 * scheduled on ifnet's CPU, and we keep going.
398 * NOTE: ifnet.if_start interlock is not released.
400 ifsq_ifstart_schedule(ifsq, 0);
404 void
405 if_devstart(struct ifnet *ifp)
407 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
410 /* Device driver ifnet.if_start schedule helper function */
411 void
412 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
414 ifsq_ifstart_schedule(ifsq, 1);
417 void
418 if_devstart_sched(struct ifnet *ifp)
420 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
423 static void
424 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
426 lwkt_serialize_enter(ifp->if_serializer);
429 static void
430 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
432 lwkt_serialize_exit(ifp->if_serializer);
435 static int
436 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
438 return lwkt_serialize_try(ifp->if_serializer);
441 #ifdef INVARIANTS
442 static void
443 if_default_serialize_assert(struct ifnet *ifp,
444 enum ifnet_serialize slz __unused,
445 boolean_t serialized)
447 if (serialized)
448 ASSERT_SERIALIZED(ifp->if_serializer);
449 else
450 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
452 #endif
455 * Attach an interface to the list of "active" interfaces.
457 * The serializer is optional.
459 void
460 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
462 unsigned socksize;
463 int namelen, masklen;
464 struct sockaddr_dl *sdl, *sdl_addr;
465 struct ifaddr *ifa;
466 struct ifaltq *ifq;
467 struct ifnet **old_ifindex2ifnet = NULL;
468 struct ifnet_array *old_ifnet_array;
469 int i, q;
471 static int if_indexlim = 8;
473 if (ifp->if_serialize != NULL) {
474 KASSERT(ifp->if_deserialize != NULL &&
475 ifp->if_tryserialize != NULL &&
476 ifp->if_serialize_assert != NULL,
477 ("serialize functions are partially setup"));
480 * If the device supplies serialize functions,
481 * then clear if_serializer to catch any invalid
482 * usage of this field.
484 KASSERT(serializer == NULL,
485 ("both serialize functions and default serializer "
486 "are supplied"));
487 ifp->if_serializer = NULL;
488 } else {
489 KASSERT(ifp->if_deserialize == NULL &&
490 ifp->if_tryserialize == NULL &&
491 ifp->if_serialize_assert == NULL,
492 ("serialize functions are partially setup"));
493 ifp->if_serialize = if_default_serialize;
494 ifp->if_deserialize = if_default_deserialize;
495 ifp->if_tryserialize = if_default_tryserialize;
496 #ifdef INVARIANTS
497 ifp->if_serialize_assert = if_default_serialize_assert;
498 #endif
501 * The serializer can be passed in from the device,
502 * allowing the same serializer to be used for both
503 * the interrupt interlock and the device queue.
504 * If not specified, the netif structure will use an
505 * embedded serializer.
507 if (serializer == NULL) {
508 serializer = &ifp->if_default_serializer;
509 lwkt_serialize_init(serializer);
511 ifp->if_serializer = serializer;
515 * XXX -
516 * The old code would work if the interface passed a pre-existing
517 * chain of ifaddrs to this code. We don't trust our callers to
518 * properly initialize the tailq, however, so we no longer allow
519 * this unlikely case.
521 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
522 M_IFADDR, M_WAITOK | M_ZERO);
523 for (i = 0; i < ncpus; ++i)
524 TAILQ_INIT(&ifp->if_addrheads[i]);
526 TAILQ_INIT(&ifp->if_multiaddrs);
527 TAILQ_INIT(&ifp->if_groups);
528 getmicrotime(&ifp->if_lastchange);
531 * create a Link Level name for this device
533 namelen = strlen(ifp->if_xname);
534 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
535 socksize = masklen + ifp->if_addrlen;
536 if (socksize < sizeof(*sdl))
537 socksize = sizeof(*sdl);
538 socksize = RT_ROUNDUP(socksize);
539 ifa = ifa_create(sizeof(struct ifaddr) + 2 * socksize);
540 sdl = sdl_addr = (struct sockaddr_dl *)(ifa + 1);
541 sdl->sdl_len = socksize;
542 sdl->sdl_family = AF_LINK;
543 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
544 sdl->sdl_nlen = namelen;
545 sdl->sdl_type = ifp->if_type;
546 ifp->if_lladdr = ifa;
547 ifa->ifa_ifp = ifp;
548 ifa->ifa_rtrequest = link_rtrequest;
549 ifa->ifa_addr = (struct sockaddr *)sdl;
550 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
551 ifa->ifa_netmask = (struct sockaddr *)sdl;
552 sdl->sdl_len = masklen;
553 while (namelen != 0)
554 sdl->sdl_data[--namelen] = 0xff;
555 ifa_iflink(ifa, ifp, 0 /* Insert head */);
557 ifp->if_data_pcpu = kmalloc_cachealign(
558 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
560 if (ifp->if_mapsubq == NULL)
561 ifp->if_mapsubq = ifq_mapsubq_default;
563 ifq = &ifp->if_snd;
564 ifq->altq_type = 0;
565 ifq->altq_disc = NULL;
566 ifq->altq_flags &= ALTQF_CANTCHANGE;
567 ifq->altq_tbr = NULL;
568 ifq->altq_ifp = ifp;
570 if (ifq->altq_subq_cnt <= 0)
571 ifq->altq_subq_cnt = 1;
572 ifq->altq_subq = kmalloc_cachealign(
573 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
574 M_DEVBUF, M_WAITOK | M_ZERO);
576 if (ifq->altq_maxlen == 0) {
577 if_printf(ifp, "driver didn't set altq_maxlen\n");
578 ifq_set_maxlen(ifq, ifqmaxlen);
581 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
582 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
584 ALTQ_SQ_LOCK_INIT(ifsq);
585 ifsq->ifsq_index = q;
587 ifsq->ifsq_altq = ifq;
588 ifsq->ifsq_ifp = ifp;
590 ifsq->ifsq_maxlen = ifq->altq_maxlen;
591 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
592 ifsq->ifsq_prepended = NULL;
593 ifsq->ifsq_started = 0;
594 ifsq->ifsq_hw_oactive = 0;
595 ifsq_set_cpuid(ifsq, 0);
596 if (ifp->if_serializer != NULL)
597 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
599 ifsq->ifsq_stage =
600 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
601 M_DEVBUF, M_WAITOK | M_ZERO);
602 for (i = 0; i < ncpus; ++i)
603 ifsq->ifsq_stage[i].stg_subq = ifsq;
605 ifsq->ifsq_ifstart_nmsg =
606 kmalloc(ncpus * sizeof(struct netmsg_base),
607 M_LWKTMSG, M_WAITOK);
608 for (i = 0; i < ncpus; ++i) {
609 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
610 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
611 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
614 ifq_set_classic(ifq);
617 * Increase mbuf cluster/jcluster limits for the mbufs that
618 * could sit on the device queues for quite some time.
620 if (ifp->if_nmbclusters > 0)
621 mcl_inclimit(ifp->if_nmbclusters);
622 if (ifp->if_nmbjclusters > 0)
623 mjcl_inclimit(ifp->if_nmbjclusters);
626 * Install this ifp into ifindex2inet, ifnet queue and ifnet
627 * array after it is setup.
629 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
630 * by ifnet lock, so that non-netisr threads could get a
631 * consistent view.
633 ifnet_lock();
635 /* Don't update if_index until ifindex2ifnet is setup */
636 ifp->if_index = if_index + 1;
637 sdl_addr->sdl_index = ifp->if_index;
640 * Install this ifp into ifindex2ifnet
642 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
643 unsigned int n;
644 struct ifnet **q;
647 * Grow ifindex2ifnet
649 if_indexlim <<= 1;
650 n = if_indexlim * sizeof(*q);
651 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
652 if (ifindex2ifnet != NULL) {
653 bcopy(ifindex2ifnet, q, n/2);
654 /* Free old ifindex2ifnet after sync all netisrs */
655 old_ifindex2ifnet = ifindex2ifnet;
657 ifindex2ifnet = q;
659 ifindex2ifnet[ifp->if_index] = ifp;
661 * Update if_index after this ifp is installed into ifindex2ifnet,
662 * so that netisrs could get a consistent view of ifindex2ifnet.
664 cpu_sfence();
665 if_index = ifp->if_index;
668 * Install this ifp into ifnet array.
670 /* Free old ifnet array after sync all netisrs */
671 old_ifnet_array = ifnet_array;
672 ifnet_array = ifnet_array_add(ifp, old_ifnet_array);
675 * Install this ifp into ifnet queue.
677 TAILQ_INSERT_TAIL(&ifnetlist, ifp, if_link);
679 ifnet_unlock();
682 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
683 * are no longer accessed and we can free them safely later on.
685 netmsg_service_sync();
686 if (old_ifindex2ifnet != NULL)
687 kfree(old_ifindex2ifnet, M_IFADDR);
688 ifnet_array_free(old_ifnet_array);
690 if (!SLIST_EMPTY(&domains))
691 if_attachdomain1(ifp);
693 /* Announce the interface. */
694 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
695 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
696 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
699 static void
700 if_attachdomain(void *dummy)
702 struct ifnet *ifp;
704 ifnet_lock();
705 TAILQ_FOREACH(ifp, &ifnetlist, if_list)
706 if_attachdomain1(ifp);
707 ifnet_unlock();
709 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
710 if_attachdomain, NULL);
712 static void
713 if_attachdomain1(struct ifnet *ifp)
715 struct domain *dp;
717 crit_enter();
719 /* address family dependent data region */
720 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
721 SLIST_FOREACH(dp, &domains, dom_next)
722 if (dp->dom_ifattach)
723 ifp->if_afdata[dp->dom_family] =
724 (*dp->dom_ifattach)(ifp);
725 crit_exit();
729 * Purge all addresses whose type is _not_ AF_LINK
731 static void
732 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg)
734 struct lwkt_msg *lmsg = &nmsg->lmsg;
735 struct ifnet *ifp = lmsg->u.ms_resultp;
736 struct ifaddr_container *ifac, *next;
738 ASSERT_IN_NETISR(0);
741 * The ifaddr processing in the following loop will block,
742 * however, this function is called in netisr0, in which
743 * ifaddr list changes happen, so we don't care about the
744 * blockness of the ifaddr processing here.
746 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
747 ifa_link, next) {
748 struct ifaddr *ifa = ifac->ifa;
750 /* Ignore marker */
751 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
752 continue;
754 /* Leave link ifaddr as it is */
755 if (ifa->ifa_addr->sa_family == AF_LINK)
756 continue;
757 #ifdef INET
758 /* XXX: Ugly!! ad hoc just for INET */
759 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
760 struct ifaliasreq ifr;
761 #ifdef IFADDR_DEBUG_VERBOSE
762 int i;
764 kprintf("purge in4 addr %p: ", ifa);
765 for (i = 0; i < ncpus; ++i)
766 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
767 kprintf("\n");
768 #endif
770 bzero(&ifr, sizeof ifr);
771 ifr.ifra_addr = *ifa->ifa_addr;
772 if (ifa->ifa_dstaddr)
773 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
774 if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp,
775 NULL) == 0)
776 continue;
778 #endif /* INET */
779 #ifdef INET6
780 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
781 #ifdef IFADDR_DEBUG_VERBOSE
782 int i;
784 kprintf("purge in6 addr %p: ", ifa);
785 for (i = 0; i < ncpus; ++i)
786 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
787 kprintf("\n");
788 #endif
790 in6_purgeaddr(ifa);
791 /* ifp_addrhead is already updated */
792 continue;
794 #endif /* INET6 */
795 ifa_ifunlink(ifa, ifp);
796 ifa_destroy(ifa);
799 lwkt_replymsg(lmsg, 0);
802 void
803 if_purgeaddrs_nolink(struct ifnet *ifp)
805 struct netmsg_base nmsg;
806 struct lwkt_msg *lmsg = &nmsg.lmsg;
808 ASSERT_CANDOMSG_NETISR0(curthread);
810 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
811 if_purgeaddrs_nolink_dispatch);
812 lmsg->u.ms_resultp = ifp;
813 lwkt_domsg(netisr_cpuport(0), lmsg, 0);
816 static void
817 ifq_stage_detach_handler(netmsg_t nmsg)
819 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
820 int q;
822 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
823 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
824 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
826 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
827 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
829 lwkt_replymsg(&nmsg->lmsg, 0);
832 static void
833 ifq_stage_detach(struct ifaltq *ifq)
835 struct netmsg_base base;
836 int cpu;
838 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
839 ifq_stage_detach_handler);
840 base.lmsg.u.ms_resultp = ifq;
842 for (cpu = 0; cpu < ncpus; ++cpu)
843 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
846 struct netmsg_if_rtdel {
847 struct netmsg_base base;
848 struct ifnet *ifp;
851 static void
852 if_rtdel_dispatch(netmsg_t msg)
854 struct netmsg_if_rtdel *rmsg = (void *)msg;
855 int i, nextcpu, cpu;
857 cpu = mycpuid;
858 for (i = 1; i <= AF_MAX; i++) {
859 struct radix_node_head *rnh;
861 if ((rnh = rt_tables[cpu][i]) == NULL)
862 continue;
863 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
866 nextcpu = cpu + 1;
867 if (nextcpu < ncpus)
868 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
869 else
870 lwkt_replymsg(&rmsg->base.lmsg, 0);
874 * Detach an interface, removing it from the
875 * list of "active" interfaces.
877 void
878 if_detach(struct ifnet *ifp)
880 struct ifnet_array *old_ifnet_array;
881 struct netmsg_if_rtdel msg;
882 struct domain *dp;
883 int q;
885 /* Announce that the interface is gone. */
886 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
887 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
888 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
891 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
892 * array before it is whacked.
894 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
895 * by ifnet lock, so that non-netisr threads could get a
896 * consistent view.
898 ifnet_lock();
901 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
903 ifindex2ifnet[ifp->if_index] = NULL;
904 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
905 if_index--;
908 * Remove this ifp from ifnet queue.
910 TAILQ_REMOVE(&ifnetlist, ifp, if_link);
913 * Remove this ifp from ifnet array.
915 /* Free old ifnet array after sync all netisrs */
916 old_ifnet_array = ifnet_array;
917 ifnet_array = ifnet_array_del(ifp, old_ifnet_array);
919 ifnet_unlock();
922 * Sync all netisrs so that the old ifnet array is no longer
923 * accessed and we can free it safely later on.
925 netmsg_service_sync();
926 ifnet_array_free(old_ifnet_array);
929 * Remove routes and flush queues.
931 crit_enter();
932 #ifdef IFPOLL_ENABLE
933 if (ifp->if_flags & IFF_NPOLLING)
934 ifpoll_deregister(ifp);
935 #endif
936 if_down(ifp);
938 /* Decrease the mbuf clusters/jclusters limits increased by us */
939 if (ifp->if_nmbclusters > 0)
940 mcl_inclimit(-ifp->if_nmbclusters);
941 if (ifp->if_nmbjclusters > 0)
942 mjcl_inclimit(-ifp->if_nmbjclusters);
944 #ifdef ALTQ
945 if (ifq_is_enabled(&ifp->if_snd))
946 altq_disable(&ifp->if_snd);
947 if (ifq_is_attached(&ifp->if_snd))
948 altq_detach(&ifp->if_snd);
949 #endif
952 * Clean up all addresses.
954 ifp->if_lladdr = NULL;
956 if_purgeaddrs_nolink(ifp);
957 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
958 struct ifaddr *ifa;
960 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
961 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
962 ("non-link ifaddr is left on if_addrheads"));
964 ifa_ifunlink(ifa, ifp);
965 ifa_destroy(ifa);
966 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
967 ("there are still ifaddrs left on if_addrheads"));
970 #ifdef INET
972 * Remove all IPv4 kernel structures related to ifp.
974 in_ifdetach(ifp);
975 #endif
977 #ifdef INET6
979 * Remove all IPv6 kernel structs related to ifp. This should be done
980 * before removing routing entries below, since IPv6 interface direct
981 * routes are expected to be removed by the IPv6-specific kernel API.
982 * Otherwise, the kernel will detect some inconsistency and bark it.
984 in6_ifdetach(ifp);
985 #endif
988 * Delete all remaining routes using this interface
990 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
991 if_rtdel_dispatch);
992 msg.ifp = ifp;
993 rt_domsg_global(&msg.base);
995 SLIST_FOREACH(dp, &domains, dom_next)
996 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
997 (*dp->dom_ifdetach)(ifp,
998 ifp->if_afdata[dp->dom_family]);
1000 kfree(ifp->if_addrheads, M_IFADDR);
1002 lwkt_synchronize_ipiqs("if_detach");
1003 ifq_stage_detach(&ifp->if_snd);
1005 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
1006 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
1008 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
1009 kfree(ifsq->ifsq_stage, M_DEVBUF);
1011 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
1013 kfree(ifp->if_data_pcpu, M_DEVBUF);
1015 crit_exit();
1019 * Create interface group without members
1021 struct ifg_group *
1022 if_creategroup(const char *groupname)
1024 struct ifg_group *ifg = NULL;
1026 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
1027 M_TEMP, M_NOWAIT)) == NULL)
1028 return (NULL);
1030 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1031 ifg->ifg_refcnt = 0;
1032 ifg->ifg_carp_demoted = 0;
1033 TAILQ_INIT(&ifg->ifg_members);
1034 #if NPF > 0
1035 pfi_attach_ifgroup(ifg);
1036 #endif
1037 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
1039 return (ifg);
1043 * Add a group to an interface
1046 if_addgroup(struct ifnet *ifp, const char *groupname)
1048 struct ifg_list *ifgl;
1049 struct ifg_group *ifg = NULL;
1050 struct ifg_member *ifgm;
1052 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1053 groupname[strlen(groupname) - 1] <= '9')
1054 return (EINVAL);
1056 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1057 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1058 return (EEXIST);
1060 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
1061 return (ENOMEM);
1063 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1064 kfree(ifgl, M_TEMP);
1065 return (ENOMEM);
1068 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1069 if (!strcmp(ifg->ifg_group, groupname))
1070 break;
1072 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
1073 kfree(ifgl, M_TEMP);
1074 kfree(ifgm, M_TEMP);
1075 return (ENOMEM);
1078 ifg->ifg_refcnt++;
1079 ifgl->ifgl_group = ifg;
1080 ifgm->ifgm_ifp = ifp;
1082 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1083 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1085 #if NPF > 0
1086 pfi_group_change(groupname);
1087 #endif
1089 return (0);
1093 * Remove a group from an interface
1096 if_delgroup(struct ifnet *ifp, const char *groupname)
1098 struct ifg_list *ifgl;
1099 struct ifg_member *ifgm;
1101 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1102 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1103 break;
1104 if (ifgl == NULL)
1105 return (ENOENT);
1107 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1109 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1110 if (ifgm->ifgm_ifp == ifp)
1111 break;
1113 if (ifgm != NULL) {
1114 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1115 kfree(ifgm, M_TEMP);
1118 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1119 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1120 #if NPF > 0
1121 pfi_detach_ifgroup(ifgl->ifgl_group);
1122 #endif
1123 kfree(ifgl->ifgl_group, M_TEMP);
1126 kfree(ifgl, M_TEMP);
1128 #if NPF > 0
1129 pfi_group_change(groupname);
1130 #endif
1132 return (0);
1136 * Stores all groups from an interface in memory pointed
1137 * to by data
1140 if_getgroup(caddr_t data, struct ifnet *ifp)
1142 int len, error;
1143 struct ifg_list *ifgl;
1144 struct ifg_req ifgrq, *ifgp;
1145 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1147 if (ifgr->ifgr_len == 0) {
1148 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1149 ifgr->ifgr_len += sizeof(struct ifg_req);
1150 return (0);
1153 len = ifgr->ifgr_len;
1154 ifgp = ifgr->ifgr_groups;
1155 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1156 if (len < sizeof(ifgrq))
1157 return (EINVAL);
1158 bzero(&ifgrq, sizeof ifgrq);
1159 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1160 sizeof(ifgrq.ifgrq_group));
1161 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1162 sizeof(struct ifg_req))))
1163 return (error);
1164 len -= sizeof(ifgrq);
1165 ifgp++;
1168 return (0);
1172 * Stores all members of a group in memory pointed to by data
1175 if_getgroupmembers(caddr_t data)
1177 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1178 struct ifg_group *ifg;
1179 struct ifg_member *ifgm;
1180 struct ifg_req ifgrq, *ifgp;
1181 int len, error;
1183 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1184 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1185 break;
1186 if (ifg == NULL)
1187 return (ENOENT);
1189 if (ifgr->ifgr_len == 0) {
1190 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1191 ifgr->ifgr_len += sizeof(ifgrq);
1192 return (0);
1195 len = ifgr->ifgr_len;
1196 ifgp = ifgr->ifgr_groups;
1197 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1198 if (len < sizeof(ifgrq))
1199 return (EINVAL);
1200 bzero(&ifgrq, sizeof ifgrq);
1201 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1202 sizeof(ifgrq.ifgrq_member));
1203 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1204 sizeof(struct ifg_req))))
1205 return (error);
1206 len -= sizeof(ifgrq);
1207 ifgp++;
1210 return (0);
1214 * Delete Routes for a Network Interface
1216 * Called for each routing entry via the rnh->rnh_walktree() call above
1217 * to delete all route entries referencing a detaching network interface.
1219 * Arguments:
1220 * rn pointer to node in the routing table
1221 * arg argument passed to rnh->rnh_walktree() - detaching interface
1223 * Returns:
1224 * 0 successful
1225 * errno failed - reason indicated
1228 static int
1229 if_rtdel(struct radix_node *rn, void *arg)
1231 struct rtentry *rt = (struct rtentry *)rn;
1232 struct ifnet *ifp = arg;
1233 int err;
1235 if (rt->rt_ifp == ifp) {
1238 * Protect (sorta) against walktree recursion problems
1239 * with cloned routes
1241 if (!(rt->rt_flags & RTF_UP))
1242 return (0);
1244 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1245 rt_mask(rt), rt->rt_flags,
1246 NULL);
1247 if (err) {
1248 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1252 return (0);
1255 static __inline boolean_t
1256 ifa_prefer(const struct ifaddr *cur_ifa, const struct ifaddr *old_ifa)
1258 if (old_ifa == NULL)
1259 return TRUE;
1261 if ((old_ifa->ifa_ifp->if_flags & IFF_UP) == 0 &&
1262 (cur_ifa->ifa_ifp->if_flags & IFF_UP))
1263 return TRUE;
1264 if ((old_ifa->ifa_flags & IFA_ROUTE) == 0 &&
1265 (cur_ifa->ifa_flags & IFA_ROUTE))
1266 return TRUE;
1267 return FALSE;
1271 * Locate an interface based on a complete address.
1273 struct ifaddr *
1274 ifa_ifwithaddr(struct sockaddr *addr)
1276 const struct ifnet_array *arr;
1277 int i;
1279 arr = ifnet_array_get();
1280 for (i = 0; i < arr->ifnet_count; ++i) {
1281 struct ifnet *ifp = arr->ifnet_arr[i];
1282 struct ifaddr_container *ifac;
1284 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1285 struct ifaddr *ifa = ifac->ifa;
1287 if (ifa->ifa_addr->sa_family != addr->sa_family)
1288 continue;
1289 if (sa_equal(addr, ifa->ifa_addr))
1290 return (ifa);
1291 if ((ifp->if_flags & IFF_BROADCAST) &&
1292 ifa->ifa_broadaddr &&
1293 /* IPv6 doesn't have broadcast */
1294 ifa->ifa_broadaddr->sa_len != 0 &&
1295 sa_equal(ifa->ifa_broadaddr, addr))
1296 return (ifa);
1299 return (NULL);
1303 * Locate the point to point interface with a given destination address.
1305 struct ifaddr *
1306 ifa_ifwithdstaddr(struct sockaddr *addr)
1308 const struct ifnet_array *arr;
1309 int i;
1311 arr = ifnet_array_get();
1312 for (i = 0; i < arr->ifnet_count; ++i) {
1313 struct ifnet *ifp = arr->ifnet_arr[i];
1314 struct ifaddr_container *ifac;
1316 if (!(ifp->if_flags & IFF_POINTOPOINT))
1317 continue;
1319 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1320 struct ifaddr *ifa = ifac->ifa;
1322 if (ifa->ifa_addr->sa_family != addr->sa_family)
1323 continue;
1324 if (ifa->ifa_dstaddr &&
1325 sa_equal(addr, ifa->ifa_dstaddr))
1326 return (ifa);
1329 return (NULL);
1333 * Find an interface on a specific network. If many, choice
1334 * is most specific found.
1336 struct ifaddr *
1337 ifa_ifwithnet(struct sockaddr *addr)
1339 struct ifaddr *ifa_maybe = NULL;
1340 u_int af = addr->sa_family;
1341 char *addr_data = addr->sa_data, *cplim;
1342 const struct ifnet_array *arr;
1343 int i;
1346 * AF_LINK addresses can be looked up directly by their index number,
1347 * so do that if we can.
1349 if (af == AF_LINK) {
1350 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1352 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1353 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1357 * Scan though each interface, looking for ones that have
1358 * addresses in this address family.
1360 arr = ifnet_array_get();
1361 for (i = 0; i < arr->ifnet_count; ++i) {
1362 struct ifnet *ifp = arr->ifnet_arr[i];
1363 struct ifaddr_container *ifac;
1365 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1366 struct ifaddr *ifa = ifac->ifa;
1367 char *cp, *cp2, *cp3;
1369 if (ifa->ifa_addr->sa_family != af)
1370 next: continue;
1371 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1373 * This is a bit broken as it doesn't
1374 * take into account that the remote end may
1375 * be a single node in the network we are
1376 * looking for.
1377 * The trouble is that we don't know the
1378 * netmask for the remote end.
1380 if (ifa->ifa_dstaddr != NULL &&
1381 sa_equal(addr, ifa->ifa_dstaddr))
1382 return (ifa);
1383 } else {
1385 * if we have a special address handler,
1386 * then use it instead of the generic one.
1388 if (ifa->ifa_claim_addr) {
1389 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1390 return (ifa);
1391 } else {
1392 continue;
1397 * Scan all the bits in the ifa's address.
1398 * If a bit dissagrees with what we are
1399 * looking for, mask it with the netmask
1400 * to see if it really matters.
1401 * (A byte at a time)
1403 if (ifa->ifa_netmask == 0)
1404 continue;
1405 cp = addr_data;
1406 cp2 = ifa->ifa_addr->sa_data;
1407 cp3 = ifa->ifa_netmask->sa_data;
1408 cplim = ifa->ifa_netmask->sa_len +
1409 (char *)ifa->ifa_netmask;
1410 while (cp3 < cplim)
1411 if ((*cp++ ^ *cp2++) & *cp3++)
1412 goto next; /* next address! */
1414 * If the netmask of what we just found
1415 * is more specific than what we had before
1416 * (if we had one) then remember the new one
1417 * before continuing to search for an even
1418 * better one. If the netmasks are equal,
1419 * we prefer the this ifa based on the result
1420 * of ifa_prefer().
1422 if (ifa_maybe == NULL ||
1423 rn_refines((char *)ifa->ifa_netmask,
1424 (char *)ifa_maybe->ifa_netmask) ||
1425 (sa_equal(ifa_maybe->ifa_netmask,
1426 ifa->ifa_netmask) &&
1427 ifa_prefer(ifa, ifa_maybe)))
1428 ifa_maybe = ifa;
1432 return (ifa_maybe);
1436 * Find an interface address specific to an interface best matching
1437 * a given address.
1439 struct ifaddr *
1440 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1442 struct ifaddr_container *ifac;
1443 char *cp, *cp2, *cp3;
1444 char *cplim;
1445 struct ifaddr *ifa_maybe = NULL;
1446 u_int af = addr->sa_family;
1448 if (af >= AF_MAX)
1449 return (0);
1450 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1451 struct ifaddr *ifa = ifac->ifa;
1453 if (ifa->ifa_addr->sa_family != af)
1454 continue;
1455 if (ifa_maybe == NULL)
1456 ifa_maybe = ifa;
1457 if (ifa->ifa_netmask == NULL) {
1458 if (sa_equal(addr, ifa->ifa_addr) ||
1459 (ifa->ifa_dstaddr != NULL &&
1460 sa_equal(addr, ifa->ifa_dstaddr)))
1461 return (ifa);
1462 continue;
1464 if (ifp->if_flags & IFF_POINTOPOINT) {
1465 if (sa_equal(addr, ifa->ifa_dstaddr))
1466 return (ifa);
1467 } else {
1468 cp = addr->sa_data;
1469 cp2 = ifa->ifa_addr->sa_data;
1470 cp3 = ifa->ifa_netmask->sa_data;
1471 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1472 for (; cp3 < cplim; cp3++)
1473 if ((*cp++ ^ *cp2++) & *cp3)
1474 break;
1475 if (cp3 == cplim)
1476 return (ifa);
1479 return (ifa_maybe);
1483 * Default action when installing a route with a Link Level gateway.
1484 * Lookup an appropriate real ifa to point to.
1485 * This should be moved to /sys/net/link.c eventually.
1487 static void
1488 link_rtrequest(int cmd, struct rtentry *rt)
1490 struct ifaddr *ifa;
1491 struct sockaddr *dst;
1492 struct ifnet *ifp;
1494 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1495 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1496 return;
1497 ifa = ifaof_ifpforaddr(dst, ifp);
1498 if (ifa != NULL) {
1499 IFAFREE(rt->rt_ifa);
1500 IFAREF(ifa);
1501 rt->rt_ifa = ifa;
1502 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1503 ifa->ifa_rtrequest(cmd, rt);
1507 struct netmsg_ifroute {
1508 struct netmsg_base base;
1509 struct ifnet *ifp;
1510 int flag;
1511 int fam;
1515 * Mark an interface down and notify protocols of the transition.
1517 static void
1518 if_unroute_dispatch(netmsg_t nmsg)
1520 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1521 struct ifnet *ifp = msg->ifp;
1522 int flag = msg->flag, fam = msg->fam;
1523 struct ifaddr_container *ifac;
1525 ifp->if_flags &= ~flag;
1526 getmicrotime(&ifp->if_lastchange);
1528 * The ifaddr processing in the following loop will block,
1529 * however, this function is called in netisr0, in which
1530 * ifaddr list changes happen, so we don't care about the
1531 * blockness of the ifaddr processing here.
1533 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1534 struct ifaddr *ifa = ifac->ifa;
1536 /* Ignore marker */
1537 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1538 continue;
1540 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1541 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1543 ifq_purge_all(&ifp->if_snd);
1544 rt_ifmsg(ifp);
1546 lwkt_replymsg(&nmsg->lmsg, 0);
1549 void
1550 if_unroute(struct ifnet *ifp, int flag, int fam)
1552 struct netmsg_ifroute msg;
1554 ASSERT_CANDOMSG_NETISR0(curthread);
1556 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1557 if_unroute_dispatch);
1558 msg.ifp = ifp;
1559 msg.flag = flag;
1560 msg.fam = fam;
1561 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1565 * Mark an interface up and notify protocols of the transition.
1567 static void
1568 if_route_dispatch(netmsg_t nmsg)
1570 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1571 struct ifnet *ifp = msg->ifp;
1572 int flag = msg->flag, fam = msg->fam;
1573 struct ifaddr_container *ifac;
1575 ifq_purge_all(&ifp->if_snd);
1576 ifp->if_flags |= flag;
1577 getmicrotime(&ifp->if_lastchange);
1579 * The ifaddr processing in the following loop will block,
1580 * however, this function is called in netisr0, in which
1581 * ifaddr list changes happen, so we don't care about the
1582 * blockness of the ifaddr processing here.
1584 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1585 struct ifaddr *ifa = ifac->ifa;
1587 /* Ignore marker */
1588 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1589 continue;
1591 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1592 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1594 rt_ifmsg(ifp);
1595 #ifdef INET6
1596 in6_if_up(ifp);
1597 #endif
1599 lwkt_replymsg(&nmsg->lmsg, 0);
1602 void
1603 if_route(struct ifnet *ifp, int flag, int fam)
1605 struct netmsg_ifroute msg;
1607 ASSERT_CANDOMSG_NETISR0(curthread);
1609 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1610 if_route_dispatch);
1611 msg.ifp = ifp;
1612 msg.flag = flag;
1613 msg.fam = fam;
1614 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1618 * Mark an interface down and notify protocols of the transition. An
1619 * interface going down is also considered to be a synchronizing event.
1620 * We must ensure that all packet processing related to the interface
1621 * has completed before we return so e.g. the caller can free the ifnet
1622 * structure that the mbufs may be referencing.
1624 * NOTE: must be called at splnet or eqivalent.
1626 void
1627 if_down(struct ifnet *ifp)
1629 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1630 netmsg_service_sync();
1634 * Mark an interface up and notify protocols of
1635 * the transition.
1636 * NOTE: must be called at splnet or eqivalent.
1638 void
1639 if_up(struct ifnet *ifp)
1641 if_route(ifp, IFF_UP, AF_UNSPEC);
1645 * Process a link state change.
1646 * NOTE: must be called at splsoftnet or equivalent.
1648 void
1649 if_link_state_change(struct ifnet *ifp)
1651 int link_state = ifp->if_link_state;
1653 rt_ifmsg(ifp);
1654 devctl_notify("IFNET", ifp->if_xname,
1655 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1659 * Handle interface watchdog timer routines. Called
1660 * from softclock, we decrement timers (if set) and
1661 * call the appropriate interface routine on expiration.
1663 static void
1664 if_slowtimo_dispatch(netmsg_t nmsg)
1666 struct globaldata *gd = mycpu;
1667 const struct ifnet_array *arr;
1668 int i;
1670 ASSERT_IN_NETISR(0);
1672 crit_enter_gd(gd);
1673 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1674 crit_exit_gd(gd);
1676 arr = ifnet_array_get();
1677 for (i = 0; i < arr->ifnet_count; ++i) {
1678 struct ifnet *ifp = arr->ifnet_arr[i];
1680 crit_enter_gd(gd);
1682 if (if_stats_compat) {
1683 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1684 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1685 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1686 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1687 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1688 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1689 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1690 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1691 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1692 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1693 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1694 IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1697 if (ifp->if_timer == 0 || --ifp->if_timer) {
1698 crit_exit_gd(gd);
1699 continue;
1701 if (ifp->if_watchdog) {
1702 if (ifnet_tryserialize_all(ifp)) {
1703 (*ifp->if_watchdog)(ifp);
1704 ifnet_deserialize_all(ifp);
1705 } else {
1706 /* try again next timeout */
1707 ++ifp->if_timer;
1711 crit_exit_gd(gd);
1714 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1717 static void
1718 if_slowtimo(void *arg __unused)
1720 struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1722 KASSERT(mycpuid == 0, ("not on cpu0"));
1723 crit_enter();
1724 if (lmsg->ms_flags & MSGF_DONE)
1725 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1726 crit_exit();
1730 * Map interface name to
1731 * interface structure pointer.
1733 struct ifnet *
1734 ifunit(const char *name)
1736 struct ifnet *ifp;
1739 * Search all the interfaces for this name/number
1741 KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked"));
1743 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1744 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1745 break;
1747 return (ifp);
1750 struct ifnet *
1751 ifunit_netisr(const char *name)
1753 const struct ifnet_array *arr;
1754 int i;
1757 * Search all the interfaces for this name/number
1760 arr = ifnet_array_get();
1761 for (i = 0; i < arr->ifnet_count; ++i) {
1762 struct ifnet *ifp = arr->ifnet_arr[i];
1764 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1765 return ifp;
1767 return NULL;
1771 * Interface ioctls.
1774 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1776 struct ifnet *ifp;
1777 struct ifreq *ifr;
1778 struct ifstat *ifs;
1779 int error, do_ifup = 0;
1780 short oif_flags;
1781 int new_flags;
1782 size_t namelen, onamelen;
1783 char new_name[IFNAMSIZ];
1784 struct ifaddr *ifa;
1785 struct sockaddr_dl *sdl;
1787 switch (cmd) {
1788 case SIOCGIFCONF:
1789 case OSIOCGIFCONF:
1790 return (ifconf(cmd, data, cred));
1791 default:
1792 break;
1795 ifr = (struct ifreq *)data;
1797 switch (cmd) {
1798 case SIOCIFCREATE:
1799 case SIOCIFCREATE2:
1800 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1801 return (error);
1802 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1803 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1804 case SIOCIFDESTROY:
1805 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1806 return (error);
1807 return (if_clone_destroy(ifr->ifr_name));
1808 case SIOCIFGCLONERS:
1809 return (if_clone_list((struct if_clonereq *)data));
1810 default:
1811 break;
1815 * Nominal ioctl through interface, lookup the ifp and obtain a
1816 * lock to serialize the ifconfig ioctl operation.
1818 ifnet_lock();
1820 ifp = ifunit(ifr->ifr_name);
1821 if (ifp == NULL) {
1822 ifnet_unlock();
1823 return (ENXIO);
1825 error = 0;
1827 switch (cmd) {
1828 case SIOCGIFINDEX:
1829 ifr->ifr_index = ifp->if_index;
1830 break;
1832 case SIOCGIFFLAGS:
1833 ifr->ifr_flags = ifp->if_flags;
1834 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1835 break;
1837 case SIOCGIFCAP:
1838 ifr->ifr_reqcap = ifp->if_capabilities;
1839 ifr->ifr_curcap = ifp->if_capenable;
1840 break;
1842 case SIOCGIFMETRIC:
1843 ifr->ifr_metric = ifp->if_metric;
1844 break;
1846 case SIOCGIFMTU:
1847 ifr->ifr_mtu = ifp->if_mtu;
1848 break;
1850 case SIOCGIFTSOLEN:
1851 ifr->ifr_tsolen = ifp->if_tsolen;
1852 break;
1854 case SIOCGIFDATA:
1855 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1856 sizeof(ifp->if_data));
1857 break;
1859 case SIOCGIFPHYS:
1860 ifr->ifr_phys = ifp->if_physical;
1861 break;
1863 case SIOCGIFPOLLCPU:
1864 ifr->ifr_pollcpu = -1;
1865 break;
1867 case SIOCSIFPOLLCPU:
1868 break;
1870 case SIOCSIFFLAGS:
1871 error = priv_check_cred(cred, PRIV_ROOT, 0);
1872 if (error)
1873 break;
1874 new_flags = (ifr->ifr_flags & 0xffff) |
1875 (ifr->ifr_flagshigh << 16);
1876 if (ifp->if_flags & IFF_SMART) {
1877 /* Smart drivers twiddle their own routes */
1878 } else if (ifp->if_flags & IFF_UP &&
1879 (new_flags & IFF_UP) == 0) {
1880 if_down(ifp);
1881 } else if (new_flags & IFF_UP &&
1882 (ifp->if_flags & IFF_UP) == 0) {
1883 do_ifup = 1;
1886 #ifdef IFPOLL_ENABLE
1887 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1888 if (new_flags & IFF_NPOLLING)
1889 ifpoll_register(ifp);
1890 else
1891 ifpoll_deregister(ifp);
1893 #endif
1895 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1896 (new_flags &~ IFF_CANTCHANGE);
1897 if (new_flags & IFF_PPROMISC) {
1898 /* Permanently promiscuous mode requested */
1899 ifp->if_flags |= IFF_PROMISC;
1900 } else if (ifp->if_pcount == 0) {
1901 ifp->if_flags &= ~IFF_PROMISC;
1903 if (ifp->if_ioctl) {
1904 ifnet_serialize_all(ifp);
1905 ifp->if_ioctl(ifp, cmd, data, cred);
1906 ifnet_deserialize_all(ifp);
1908 if (do_ifup)
1909 if_up(ifp);
1910 getmicrotime(&ifp->if_lastchange);
1911 break;
1913 case SIOCSIFCAP:
1914 error = priv_check_cred(cred, PRIV_ROOT, 0);
1915 if (error)
1916 break;
1917 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1918 error = EINVAL;
1919 break;
1921 ifnet_serialize_all(ifp);
1922 ifp->if_ioctl(ifp, cmd, data, cred);
1923 ifnet_deserialize_all(ifp);
1924 break;
1926 case SIOCSIFNAME:
1927 error = priv_check_cred(cred, PRIV_ROOT, 0);
1928 if (error)
1929 break;
1930 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1931 if (error)
1932 break;
1933 if (new_name[0] == '\0') {
1934 error = EINVAL;
1935 break;
1937 if (ifunit(new_name) != NULL) {
1938 error = EEXIST;
1939 break;
1942 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1944 /* Announce the departure of the interface. */
1945 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1947 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1948 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1949 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1950 namelen = strlen(new_name);
1951 onamelen = sdl->sdl_nlen;
1953 * Move the address if needed. This is safe because we
1954 * allocate space for a name of length IFNAMSIZ when we
1955 * create this in if_attach().
1957 if (namelen != onamelen) {
1958 bcopy(sdl->sdl_data + onamelen,
1959 sdl->sdl_data + namelen, sdl->sdl_alen);
1961 bcopy(new_name, sdl->sdl_data, namelen);
1962 sdl->sdl_nlen = namelen;
1963 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1964 bzero(sdl->sdl_data, onamelen);
1965 while (namelen != 0)
1966 sdl->sdl_data[--namelen] = 0xff;
1968 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1970 /* Announce the return of the interface. */
1971 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1972 break;
1974 case SIOCSIFMETRIC:
1975 error = priv_check_cred(cred, PRIV_ROOT, 0);
1976 if (error)
1977 break;
1978 ifp->if_metric = ifr->ifr_metric;
1979 getmicrotime(&ifp->if_lastchange);
1980 break;
1982 case SIOCSIFPHYS:
1983 error = priv_check_cred(cred, PRIV_ROOT, 0);
1984 if (error)
1985 break;
1986 if (ifp->if_ioctl == NULL) {
1987 error = EOPNOTSUPP;
1988 break;
1990 ifnet_serialize_all(ifp);
1991 error = ifp->if_ioctl(ifp, cmd, data, cred);
1992 ifnet_deserialize_all(ifp);
1993 if (error == 0)
1994 getmicrotime(&ifp->if_lastchange);
1995 break;
1997 case SIOCSIFMTU:
1999 u_long oldmtu = ifp->if_mtu;
2001 error = priv_check_cred(cred, PRIV_ROOT, 0);
2002 if (error)
2003 break;
2004 if (ifp->if_ioctl == NULL) {
2005 error = EOPNOTSUPP;
2006 break;
2008 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2009 error = EINVAL;
2010 break;
2012 ifnet_serialize_all(ifp);
2013 error = ifp->if_ioctl(ifp, cmd, data, cred);
2014 ifnet_deserialize_all(ifp);
2015 if (error == 0) {
2016 getmicrotime(&ifp->if_lastchange);
2017 rt_ifmsg(ifp);
2020 * If the link MTU changed, do network layer specific procedure.
2022 if (ifp->if_mtu != oldmtu) {
2023 #ifdef INET6
2024 nd6_setmtu(ifp);
2025 #endif
2027 break;
2030 case SIOCSIFTSOLEN:
2031 error = priv_check_cred(cred, PRIV_ROOT, 0);
2032 if (error)
2033 break;
2035 /* XXX need driver supplied upper limit */
2036 if (ifr->ifr_tsolen <= 0) {
2037 error = EINVAL;
2038 break;
2040 ifp->if_tsolen = ifr->ifr_tsolen;
2041 break;
2043 case SIOCADDMULTI:
2044 case SIOCDELMULTI:
2045 error = priv_check_cred(cred, PRIV_ROOT, 0);
2046 if (error)
2047 break;
2049 /* Don't allow group membership on non-multicast interfaces. */
2050 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2051 error = EOPNOTSUPP;
2052 break;
2055 /* Don't let users screw up protocols' entries. */
2056 if (ifr->ifr_addr.sa_family != AF_LINK) {
2057 error = EINVAL;
2058 break;
2061 if (cmd == SIOCADDMULTI) {
2062 struct ifmultiaddr *ifma;
2063 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2064 } else {
2065 error = if_delmulti(ifp, &ifr->ifr_addr);
2067 if (error == 0)
2068 getmicrotime(&ifp->if_lastchange);
2069 break;
2071 case SIOCSIFPHYADDR:
2072 case SIOCDIFPHYADDR:
2073 #ifdef INET6
2074 case SIOCSIFPHYADDR_IN6:
2075 #endif
2076 case SIOCSLIFPHYADDR:
2077 case SIOCSIFMEDIA:
2078 case SIOCSIFGENERIC:
2079 error = priv_check_cred(cred, PRIV_ROOT, 0);
2080 if (error)
2081 break;
2082 if (ifp->if_ioctl == 0) {
2083 error = EOPNOTSUPP;
2084 break;
2086 ifnet_serialize_all(ifp);
2087 error = ifp->if_ioctl(ifp, cmd, data, cred);
2088 ifnet_deserialize_all(ifp);
2089 if (error == 0)
2090 getmicrotime(&ifp->if_lastchange);
2091 break;
2093 case SIOCGIFSTATUS:
2094 ifs = (struct ifstat *)data;
2095 ifs->ascii[0] = '\0';
2096 /* fall through */
2097 case SIOCGIFPSRCADDR:
2098 case SIOCGIFPDSTADDR:
2099 case SIOCGLIFPHYADDR:
2100 case SIOCGIFMEDIA:
2101 case SIOCGIFGENERIC:
2102 if (ifp->if_ioctl == NULL) {
2103 error = EOPNOTSUPP;
2104 break;
2106 ifnet_serialize_all(ifp);
2107 error = ifp->if_ioctl(ifp, cmd, data, cred);
2108 ifnet_deserialize_all(ifp);
2109 break;
2111 case SIOCSIFLLADDR:
2112 error = priv_check_cred(cred, PRIV_ROOT, 0);
2113 if (error)
2114 break;
2115 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
2116 ifr->ifr_addr.sa_len);
2117 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2118 break;
2120 default:
2121 oif_flags = ifp->if_flags;
2122 if (so->so_proto == 0) {
2123 error = EOPNOTSUPP;
2124 break;
2126 error = so_pru_control_direct(so, cmd, data, ifp);
2128 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2129 #ifdef INET6
2130 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2131 if (ifp->if_flags & IFF_UP) {
2132 crit_enter();
2133 in6_if_up(ifp);
2134 crit_exit();
2136 #endif
2138 break;
2141 ifnet_unlock();
2142 return (error);
2146 * Set/clear promiscuous mode on interface ifp based on the truth value
2147 * of pswitch. The calls are reference counted so that only the first
2148 * "on" request actually has an effect, as does the final "off" request.
2149 * Results are undefined if the "off" and "on" requests are not matched.
2152 ifpromisc(struct ifnet *ifp, int pswitch)
2154 struct ifreq ifr;
2155 int error;
2156 int oldflags;
2158 oldflags = ifp->if_flags;
2159 if (ifp->if_flags & IFF_PPROMISC) {
2160 /* Do nothing if device is in permanently promiscuous mode */
2161 ifp->if_pcount += pswitch ? 1 : -1;
2162 return (0);
2164 if (pswitch) {
2166 * If the device is not configured up, we cannot put it in
2167 * promiscuous mode.
2169 if ((ifp->if_flags & IFF_UP) == 0)
2170 return (ENETDOWN);
2171 if (ifp->if_pcount++ != 0)
2172 return (0);
2173 ifp->if_flags |= IFF_PROMISC;
2174 log(LOG_INFO, "%s: promiscuous mode enabled\n",
2175 ifp->if_xname);
2176 } else {
2177 if (--ifp->if_pcount > 0)
2178 return (0);
2179 ifp->if_flags &= ~IFF_PROMISC;
2180 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2181 ifp->if_xname);
2183 ifr.ifr_flags = ifp->if_flags;
2184 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2185 ifnet_serialize_all(ifp);
2186 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2187 ifnet_deserialize_all(ifp);
2188 if (error == 0)
2189 rt_ifmsg(ifp);
2190 else
2191 ifp->if_flags = oldflags;
2192 return error;
2196 * Return interface configuration
2197 * of system. List may be used
2198 * in later ioctl's (above) to get
2199 * other information.
2201 static int
2202 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2204 struct ifconf *ifc = (struct ifconf *)data;
2205 struct ifnet *ifp;
2206 struct sockaddr *sa;
2207 struct ifreq ifr, *ifrp;
2208 int space = ifc->ifc_len, error = 0;
2210 ifrp = ifc->ifc_req;
2212 ifnet_lock();
2213 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2214 struct ifaddr_container *ifac, *ifac_mark;
2215 struct ifaddr_marker mark;
2216 struct ifaddrhead *head;
2217 int addrs;
2219 if (space <= sizeof ifr)
2220 break;
2223 * Zero the stack declared structure first to prevent
2224 * memory disclosure.
2226 bzero(&ifr, sizeof(ifr));
2227 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2228 >= sizeof(ifr.ifr_name)) {
2229 error = ENAMETOOLONG;
2230 break;
2234 * Add a marker, since copyout() could block and during that
2235 * period the list could be changed. Inserting the marker to
2236 * the header of the list will not cause trouble for the code
2237 * assuming that the first element of the list is AF_LINK; the
2238 * marker will be moved to the next position w/o blocking.
2240 ifa_marker_init(&mark, ifp);
2241 ifac_mark = &mark.ifac;
2242 head = &ifp->if_addrheads[mycpuid];
2244 addrs = 0;
2245 TAILQ_INSERT_HEAD(head, ifac_mark, ifa_link);
2246 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
2247 struct ifaddr *ifa = ifac->ifa;
2249 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2250 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
2252 /* Ignore marker */
2253 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
2254 continue;
2256 if (space <= sizeof ifr)
2257 break;
2258 sa = ifa->ifa_addr;
2259 if (cred->cr_prison &&
2260 prison_if(cred, sa))
2261 continue;
2262 addrs++;
2264 * Keep a reference on this ifaddr, so that it will
2265 * not be destroyed when its address is copied to
2266 * the userland, which could block.
2268 IFAREF(ifa);
2269 if (sa->sa_len <= sizeof(*sa)) {
2270 ifr.ifr_addr = *sa;
2271 error = copyout(&ifr, ifrp, sizeof ifr);
2272 ifrp++;
2273 } else {
2274 if (space < (sizeof ifr) + sa->sa_len -
2275 sizeof(*sa)) {
2276 IFAFREE(ifa);
2277 break;
2279 space -= sa->sa_len - sizeof(*sa);
2280 error = copyout(&ifr, ifrp,
2281 sizeof ifr.ifr_name);
2282 if (error == 0)
2283 error = copyout(sa, &ifrp->ifr_addr,
2284 sa->sa_len);
2285 ifrp = (struct ifreq *)
2286 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2288 IFAFREE(ifa);
2289 if (error)
2290 break;
2291 space -= sizeof ifr;
2293 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2294 if (error)
2295 break;
2296 if (!addrs) {
2297 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2298 error = copyout(&ifr, ifrp, sizeof ifr);
2299 if (error)
2300 break;
2301 space -= sizeof ifr;
2302 ifrp++;
2305 ifnet_unlock();
2307 ifc->ifc_len -= space;
2308 return (error);
2312 * Just like if_promisc(), but for all-multicast-reception mode.
2315 if_allmulti(struct ifnet *ifp, int onswitch)
2317 int error = 0;
2318 struct ifreq ifr;
2320 crit_enter();
2322 if (onswitch) {
2323 if (ifp->if_amcount++ == 0) {
2324 ifp->if_flags |= IFF_ALLMULTI;
2325 ifr.ifr_flags = ifp->if_flags;
2326 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2327 ifnet_serialize_all(ifp);
2328 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2329 NULL);
2330 ifnet_deserialize_all(ifp);
2332 } else {
2333 if (ifp->if_amcount > 1) {
2334 ifp->if_amcount--;
2335 } else {
2336 ifp->if_amcount = 0;
2337 ifp->if_flags &= ~IFF_ALLMULTI;
2338 ifr.ifr_flags = ifp->if_flags;
2339 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2340 ifnet_serialize_all(ifp);
2341 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2342 NULL);
2343 ifnet_deserialize_all(ifp);
2347 crit_exit();
2349 if (error == 0)
2350 rt_ifmsg(ifp);
2351 return error;
2355 * Add a multicast listenership to the interface in question.
2356 * The link layer provides a routine which converts
2359 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2360 struct ifmultiaddr **retifma)
2362 struct sockaddr *llsa, *dupsa;
2363 int error;
2364 struct ifmultiaddr *ifma;
2366 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2369 * If the matching multicast address already exists
2370 * then don't add a new one, just add a reference
2372 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2373 if (sa_equal(sa, ifma->ifma_addr)) {
2374 ifma->ifma_refcount++;
2375 if (retifma)
2376 *retifma = ifma;
2377 return 0;
2382 * Give the link layer a chance to accept/reject it, and also
2383 * find out which AF_LINK address this maps to, if it isn't one
2384 * already.
2386 if (ifp->if_resolvemulti) {
2387 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2388 if (error)
2389 return error;
2390 } else {
2391 llsa = NULL;
2394 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2395 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_INTWAIT);
2396 bcopy(sa, dupsa, sa->sa_len);
2398 ifma->ifma_addr = dupsa;
2399 ifma->ifma_lladdr = llsa;
2400 ifma->ifma_ifp = ifp;
2401 ifma->ifma_refcount = 1;
2402 ifma->ifma_protospec = NULL;
2403 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2405 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2406 if (retifma)
2407 *retifma = ifma;
2409 if (llsa != NULL) {
2410 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2411 if (sa_equal(ifma->ifma_addr, llsa))
2412 break;
2414 if (ifma) {
2415 ifma->ifma_refcount++;
2416 } else {
2417 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2418 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_INTWAIT);
2419 bcopy(llsa, dupsa, llsa->sa_len);
2420 ifma->ifma_addr = dupsa;
2421 ifma->ifma_ifp = ifp;
2422 ifma->ifma_refcount = 1;
2423 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2427 * We are certain we have added something, so call down to the
2428 * interface to let them know about it.
2430 if (ifp->if_ioctl)
2431 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2433 return 0;
2437 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2438 struct ifmultiaddr **retifma)
2440 int error;
2442 ifnet_serialize_all(ifp);
2443 error = if_addmulti_serialized(ifp, sa, retifma);
2444 ifnet_deserialize_all(ifp);
2446 return error;
2450 * Remove a reference to a multicast address on this interface. Yell
2451 * if the request does not match an existing membership.
2453 static int
2454 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2456 struct ifmultiaddr *ifma;
2458 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2460 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2461 if (sa_equal(sa, ifma->ifma_addr))
2462 break;
2463 if (ifma == NULL)
2464 return ENOENT;
2466 if (ifma->ifma_refcount > 1) {
2467 ifma->ifma_refcount--;
2468 return 0;
2471 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2472 sa = ifma->ifma_lladdr;
2473 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2475 * Make sure the interface driver is notified
2476 * in the case of a link layer mcast group being left.
2478 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2479 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2480 kfree(ifma->ifma_addr, M_IFMADDR);
2481 kfree(ifma, M_IFMADDR);
2482 if (sa == NULL)
2483 return 0;
2486 * Now look for the link-layer address which corresponds to
2487 * this network address. It had been squirreled away in
2488 * ifma->ifma_lladdr for this purpose (so we don't have
2489 * to call ifp->if_resolvemulti() again), and we saved that
2490 * value in sa above. If some nasty deleted the
2491 * link-layer address out from underneath us, we can deal because
2492 * the address we stored was is not the same as the one which was
2493 * in the record for the link-layer address. (So we don't complain
2494 * in that case.)
2496 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2497 if (sa_equal(sa, ifma->ifma_addr))
2498 break;
2499 if (ifma == NULL)
2500 return 0;
2502 if (ifma->ifma_refcount > 1) {
2503 ifma->ifma_refcount--;
2504 return 0;
2507 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2508 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2509 kfree(ifma->ifma_addr, M_IFMADDR);
2510 kfree(sa, M_IFMADDR);
2511 kfree(ifma, M_IFMADDR);
2513 return 0;
2517 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2519 int error;
2521 ifnet_serialize_all(ifp);
2522 error = if_delmulti_serialized(ifp, sa);
2523 ifnet_deserialize_all(ifp);
2525 return error;
2529 * Delete all multicast group membership for an interface.
2530 * Should be used to quickly flush all multicast filters.
2532 void
2533 if_delallmulti_serialized(struct ifnet *ifp)
2535 struct ifmultiaddr *ifma, mark;
2536 struct sockaddr sa;
2538 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2540 bzero(&sa, sizeof(sa));
2541 sa.sa_family = AF_UNSPEC;
2542 sa.sa_len = sizeof(sa);
2544 bzero(&mark, sizeof(mark));
2545 mark.ifma_addr = &sa;
2547 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2548 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2549 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2550 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2551 ifma_link);
2553 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2554 continue;
2556 if_delmulti_serialized(ifp, ifma->ifma_addr);
2558 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2563 * Set the link layer address on an interface.
2565 * At this time we only support certain types of interfaces,
2566 * and we don't allow the length of the address to change.
2569 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2571 struct sockaddr_dl *sdl;
2572 struct ifreq ifr;
2574 sdl = IF_LLSOCKADDR(ifp);
2575 if (sdl == NULL)
2576 return (EINVAL);
2577 if (len != sdl->sdl_alen) /* don't allow length to change */
2578 return (EINVAL);
2579 switch (ifp->if_type) {
2580 case IFT_ETHER: /* these types use struct arpcom */
2581 case IFT_XETHER:
2582 case IFT_L2VLAN:
2583 case IFT_IEEE8023ADLAG:
2584 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2585 bcopy(lladdr, LLADDR(sdl), len);
2586 break;
2587 default:
2588 return (ENODEV);
2591 * If the interface is already up, we need
2592 * to re-init it in order to reprogram its
2593 * address filter.
2595 ifnet_serialize_all(ifp);
2596 if ((ifp->if_flags & IFF_UP) != 0) {
2597 #ifdef INET
2598 struct ifaddr_container *ifac;
2599 #endif
2601 ifp->if_flags &= ~IFF_UP;
2602 ifr.ifr_flags = ifp->if_flags;
2603 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2604 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2605 NULL);
2606 ifp->if_flags |= IFF_UP;
2607 ifr.ifr_flags = ifp->if_flags;
2608 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2609 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2610 NULL);
2611 #ifdef INET
2613 * Also send gratuitous ARPs to notify other nodes about
2614 * the address change.
2616 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2617 struct ifaddr *ifa = ifac->ifa;
2619 if (ifa->ifa_addr != NULL &&
2620 ifa->ifa_addr->sa_family == AF_INET)
2621 arp_gratuitous(ifp, ifa);
2623 #endif
2625 ifnet_deserialize_all(ifp);
2626 return (0);
2629 struct ifmultiaddr *
2630 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2632 struct ifmultiaddr *ifma;
2634 /* TODO: need ifnet_serialize_main */
2635 ifnet_serialize_all(ifp);
2636 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2637 if (sa_equal(ifma->ifma_addr, sa))
2638 break;
2639 ifnet_deserialize_all(ifp);
2641 return ifma;
2645 * This function locates the first real ethernet MAC from a network
2646 * card and loads it into node, returning 0 on success or ENOENT if
2647 * no suitable interfaces were found. It is used by the uuid code to
2648 * generate a unique 6-byte number.
2651 if_getanyethermac(uint16_t *node, int minlen)
2653 struct ifnet *ifp;
2654 struct sockaddr_dl *sdl;
2656 ifnet_lock();
2657 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2658 if (ifp->if_type != IFT_ETHER)
2659 continue;
2660 sdl = IF_LLSOCKADDR(ifp);
2661 if (sdl->sdl_alen < minlen)
2662 continue;
2663 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2664 minlen);
2665 ifnet_unlock();
2666 return(0);
2668 ifnet_unlock();
2669 return (ENOENT);
2673 * The name argument must be a pointer to storage which will last as
2674 * long as the interface does. For physical devices, the result of
2675 * device_get_name(dev) is a good choice and for pseudo-devices a
2676 * static string works well.
2678 void
2679 if_initname(struct ifnet *ifp, const char *name, int unit)
2681 ifp->if_dname = name;
2682 ifp->if_dunit = unit;
2683 if (unit != IF_DUNIT_NONE)
2684 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2685 else
2686 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2690 if_printf(struct ifnet *ifp, const char *fmt, ...)
2692 __va_list ap;
2693 int retval;
2695 retval = kprintf("%s: ", ifp->if_xname);
2696 __va_start(ap, fmt);
2697 retval += kvprintf(fmt, ap);
2698 __va_end(ap);
2699 return (retval);
2702 struct ifnet *
2703 if_alloc(uint8_t type)
2705 struct ifnet *ifp;
2706 size_t size;
2709 * XXX temporary hack until arpcom is setup in if_l2com
2711 if (type == IFT_ETHER)
2712 size = sizeof(struct arpcom);
2713 else
2714 size = sizeof(struct ifnet);
2716 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2718 ifp->if_type = type;
2720 if (if_com_alloc[type] != NULL) {
2721 ifp->if_l2com = if_com_alloc[type](type, ifp);
2722 if (ifp->if_l2com == NULL) {
2723 kfree(ifp, M_IFNET);
2724 return (NULL);
2727 return (ifp);
2730 void
2731 if_free(struct ifnet *ifp)
2733 kfree(ifp, M_IFNET);
2736 void
2737 ifq_set_classic(struct ifaltq *ifq)
2739 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2740 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2743 void
2744 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2745 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2747 int q;
2749 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2750 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2751 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2752 KASSERT(request != NULL, ("request is not specified"));
2754 ifq->altq_mapsubq = mapsubq;
2755 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2756 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2758 ifsq->ifsq_enqueue = enqueue;
2759 ifsq->ifsq_dequeue = dequeue;
2760 ifsq->ifsq_request = request;
2764 static void
2765 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2768 classq_add(&ifsq->ifsq_norm, m);
2769 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2772 static void
2773 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2776 classq_add(&ifsq->ifsq_prio, m);
2777 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2778 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2781 static struct mbuf *
2782 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2784 struct mbuf *m;
2786 m = classq_get(&ifsq->ifsq_norm);
2787 if (m != NULL)
2788 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2789 return (m);
2792 static struct mbuf *
2793 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2795 struct mbuf *m;
2797 m = classq_get(&ifsq->ifsq_prio);
2798 if (m != NULL) {
2799 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2800 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2802 return (m);
2806 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2807 struct altq_pktattr *pa __unused)
2810 M_ASSERTPKTHDR(m);
2811 again:
2812 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2813 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
2814 struct mbuf *m_drop;
2816 if (m->m_flags & M_PRIO) {
2817 m_drop = NULL;
2818 if (ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen >> 1) &&
2819 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt >> 1)) {
2820 /* Try dropping some from normal queue. */
2821 m_drop = ifsq_norm_dequeue(ifsq);
2823 if (m_drop == NULL)
2824 m_drop = ifsq_prio_dequeue(ifsq);
2825 } else {
2826 m_drop = ifsq_norm_dequeue(ifsq);
2828 if (m_drop != NULL) {
2829 IFNET_STAT_INC(ifsq->ifsq_ifp, oqdrops, 1);
2830 m_freem(m_drop);
2831 goto again;
2834 * No old packets could be dropped!
2835 * NOTE: Caller increases oqdrops.
2837 m_freem(m);
2838 return (ENOBUFS);
2839 } else {
2840 if (m->m_flags & M_PRIO)
2841 ifsq_prio_enqueue(ifsq, m);
2842 else
2843 ifsq_norm_enqueue(ifsq, m);
2844 return (0);
2848 struct mbuf *
2849 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
2851 struct mbuf *m;
2853 switch (op) {
2854 case ALTDQ_POLL:
2855 m = classq_head(&ifsq->ifsq_prio);
2856 if (m == NULL)
2857 m = classq_head(&ifsq->ifsq_norm);
2858 break;
2860 case ALTDQ_REMOVE:
2861 m = ifsq_prio_dequeue(ifsq);
2862 if (m == NULL)
2863 m = ifsq_norm_dequeue(ifsq);
2864 break;
2866 default:
2867 panic("unsupported ALTQ dequeue op: %d", op);
2869 return m;
2873 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2875 switch (req) {
2876 case ALTRQ_PURGE:
2877 for (;;) {
2878 struct mbuf *m;
2880 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
2881 if (m == NULL)
2882 break;
2883 m_freem(m);
2885 break;
2887 default:
2888 panic("unsupported ALTQ request: %d", req);
2890 return 0;
2893 static void
2894 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2896 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2897 int running = 0, need_sched;
2900 * Try to do direct ifnet.if_start on the subqueue first, if there is
2901 * contention on the subqueue hardware serializer, ifnet.if_start on
2902 * the subqueue will be scheduled on the subqueue owner CPU.
2904 if (!ifsq_tryserialize_hw(ifsq)) {
2906 * Subqueue hardware serializer contention happened,
2907 * ifnet.if_start on the subqueue is scheduled on
2908 * the subqueue owner CPU, and we keep going.
2910 ifsq_ifstart_schedule(ifsq, 1);
2911 return;
2914 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2915 ifp->if_start(ifp, ifsq);
2916 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2917 running = 1;
2919 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2921 ifsq_deserialize_hw(ifsq);
2923 if (need_sched) {
2925 * More data need to be transmitted, ifnet.if_start on the
2926 * subqueue is scheduled on the subqueue owner CPU, and we
2927 * keep going.
2928 * NOTE: ifnet.if_start subqueue interlock is not released.
2930 ifsq_ifstart_schedule(ifsq, force_sched);
2935 * Subqeue packets staging mechanism:
2937 * The packets enqueued into the subqueue are staged to a certain amount
2938 * before the ifnet.if_start on the subqueue is called. In this way, the
2939 * driver could avoid writing to hardware registers upon every packet,
2940 * instead, hardware registers could be written when certain amount of
2941 * packets are put onto hardware TX ring. The measurement on several modern
2942 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2943 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2944 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2945 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2946 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2948 * Subqueue packets staging is performed for two entry points into drivers'
2949 * transmission function:
2950 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2951 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2953 * Subqueue packets staging will be stopped upon any of the following
2954 * conditions:
2955 * - If the count of packets enqueued on the current CPU is great than or
2956 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2957 * - If the total length of packets enqueued on the current CPU is great
2958 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2959 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2960 * is usually less than hardware's MTU.
2961 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2962 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2963 * released.
2964 * - The if_start_rollup(), which is registered as low priority netisr
2965 * rollup function, is called; probably because no more work is pending
2966 * for netisr.
2968 * NOTE:
2969 * Currently subqueue packet staging is only performed in netisr threads.
2972 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2974 struct ifaltq *ifq = &ifp->if_snd;
2975 struct ifaltq_subque *ifsq;
2976 int error, start = 0, len, mcast = 0, avoid_start = 0;
2977 struct ifsubq_stage_head *head = NULL;
2978 struct ifsubq_stage *stage = NULL;
2979 struct globaldata *gd = mycpu;
2980 struct thread *td = gd->gd_curthread;
2982 crit_enter_quick(td);
2984 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
2985 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
2987 len = m->m_pkthdr.len;
2988 if (m->m_flags & M_MCAST)
2989 mcast = 1;
2991 if (td->td_type == TD_TYPE_NETISR) {
2992 head = &ifsubq_stage_heads[mycpuid];
2993 stage = ifsq_get_stage(ifsq, mycpuid);
2995 stage->stg_cnt++;
2996 stage->stg_len += len;
2997 if (stage->stg_cnt < ifsq_stage_cntmax &&
2998 stage->stg_len < (ifp->if_mtu - max_protohdr))
2999 avoid_start = 1;
3002 ALTQ_SQ_LOCK(ifsq);
3003 error = ifsq_enqueue_locked(ifsq, m, pa);
3004 if (error) {
3005 IFNET_STAT_INC(ifp, oqdrops, 1);
3006 if (!ifsq_data_ready(ifsq)) {
3007 ALTQ_SQ_UNLOCK(ifsq);
3008 crit_exit_quick(td);
3009 return error;
3011 avoid_start = 0;
3013 if (!ifsq_is_started(ifsq)) {
3014 if (avoid_start) {
3015 ALTQ_SQ_UNLOCK(ifsq);
3017 KKASSERT(!error);
3018 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
3019 ifsq_stage_insert(head, stage);
3021 IFNET_STAT_INC(ifp, obytes, len);
3022 if (mcast)
3023 IFNET_STAT_INC(ifp, omcasts, 1);
3024 crit_exit_quick(td);
3025 return error;
3029 * Hold the subqueue interlock of ifnet.if_start
3031 ifsq_set_started(ifsq);
3032 start = 1;
3034 ALTQ_SQ_UNLOCK(ifsq);
3036 if (!error) {
3037 IFNET_STAT_INC(ifp, obytes, len);
3038 if (mcast)
3039 IFNET_STAT_INC(ifp, omcasts, 1);
3042 if (stage != NULL) {
3043 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
3044 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3045 if (!avoid_start) {
3046 ifsq_stage_remove(head, stage);
3047 ifsq_ifstart_schedule(ifsq, 1);
3049 crit_exit_quick(td);
3050 return error;
3053 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
3054 ifsq_stage_remove(head, stage);
3055 } else {
3056 stage->stg_cnt = 0;
3057 stage->stg_len = 0;
3061 if (!start) {
3062 crit_exit_quick(td);
3063 return error;
3066 ifsq_ifstart_try(ifsq, 0);
3068 crit_exit_quick(td);
3069 return error;
3072 void *
3073 ifa_create(int size)
3075 struct ifaddr *ifa;
3076 int i;
3078 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
3080 ifa = kmalloc(size, M_IFADDR, M_INTWAIT | M_ZERO);
3081 ifa->ifa_containers =
3082 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
3083 M_IFADDR, M_INTWAIT | M_ZERO);
3085 ifa->ifa_ncnt = ncpus;
3086 for (i = 0; i < ncpus; ++i) {
3087 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
3089 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
3090 ifac->ifa = ifa;
3091 ifac->ifa_refcnt = 1;
3093 #ifdef IFADDR_DEBUG
3094 kprintf("alloc ifa %p %d\n", ifa, size);
3095 #endif
3096 return ifa;
3099 void
3100 ifac_free(struct ifaddr_container *ifac, int cpu_id)
3102 struct ifaddr *ifa = ifac->ifa;
3104 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
3105 KKASSERT(ifac->ifa_refcnt == 0);
3106 KASSERT(ifac->ifa_listmask == 0,
3107 ("ifa is still on %#x lists", ifac->ifa_listmask));
3109 ifac->ifa_magic = IFA_CONTAINER_DEAD;
3111 #ifdef IFADDR_DEBUG_VERBOSE
3112 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
3113 #endif
3115 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
3116 ("invalid # of ifac, %d", ifa->ifa_ncnt));
3117 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
3118 #ifdef IFADDR_DEBUG
3119 kprintf("free ifa %p\n", ifa);
3120 #endif
3121 kfree(ifa->ifa_containers, M_IFADDR);
3122 kfree(ifa, M_IFADDR);
3126 static void
3127 ifa_iflink_dispatch(netmsg_t nmsg)
3129 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3130 struct ifaddr *ifa = msg->ifa;
3131 struct ifnet *ifp = msg->ifp;
3132 int cpu = mycpuid;
3133 struct ifaddr_container *ifac;
3135 crit_enter();
3137 ifac = &ifa->ifa_containers[cpu];
3138 ASSERT_IFAC_VALID(ifac);
3139 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
3140 ("ifaddr is on if_addrheads"));
3142 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
3143 if (msg->tail)
3144 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
3145 else
3146 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
3148 crit_exit();
3150 netisr_forwardmsg(&nmsg->base, cpu + 1);
3153 void
3154 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
3156 struct netmsg_ifaddr msg;
3158 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3159 0, ifa_iflink_dispatch);
3160 msg.ifa = ifa;
3161 msg.ifp = ifp;
3162 msg.tail = tail;
3164 netisr_domsg(&msg.base, 0);
3167 static void
3168 ifa_ifunlink_dispatch(netmsg_t nmsg)
3170 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3171 struct ifaddr *ifa = msg->ifa;
3172 struct ifnet *ifp = msg->ifp;
3173 int cpu = mycpuid;
3174 struct ifaddr_container *ifac;
3176 crit_enter();
3178 ifac = &ifa->ifa_containers[cpu];
3179 ASSERT_IFAC_VALID(ifac);
3180 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3181 ("ifaddr is not on if_addrhead"));
3183 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3184 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3186 crit_exit();
3188 netisr_forwardmsg(&nmsg->base, cpu + 1);
3191 void
3192 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3194 struct netmsg_ifaddr msg;
3196 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3197 0, ifa_ifunlink_dispatch);
3198 msg.ifa = ifa;
3199 msg.ifp = ifp;
3201 netisr_domsg(&msg.base, 0);
3204 static void
3205 ifa_destroy_dispatch(netmsg_t nmsg)
3207 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3209 IFAFREE(msg->ifa);
3210 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3213 void
3214 ifa_destroy(struct ifaddr *ifa)
3216 struct netmsg_ifaddr msg;
3218 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3219 0, ifa_destroy_dispatch);
3220 msg.ifa = ifa;
3222 netisr_domsg(&msg.base, 0);
3225 static void
3226 if_start_rollup(void)
3228 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3229 struct ifsubq_stage *stage;
3231 crit_enter();
3233 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3234 struct ifaltq_subque *ifsq = stage->stg_subq;
3235 int is_sched = 0;
3237 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3238 is_sched = 1;
3239 ifsq_stage_remove(head, stage);
3241 if (is_sched) {
3242 ifsq_ifstart_schedule(ifsq, 1);
3243 } else {
3244 int start = 0;
3246 ALTQ_SQ_LOCK(ifsq);
3247 if (!ifsq_is_started(ifsq)) {
3249 * Hold the subqueue interlock of
3250 * ifnet.if_start
3252 ifsq_set_started(ifsq);
3253 start = 1;
3255 ALTQ_SQ_UNLOCK(ifsq);
3257 if (start)
3258 ifsq_ifstart_try(ifsq, 1);
3260 KKASSERT((stage->stg_flags &
3261 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3264 crit_exit();
3267 static void
3268 ifnetinit(void *dummy __unused)
3270 int i;
3272 for (i = 0; i < ncpus; ++i)
3273 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3274 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3277 void
3278 if_register_com_alloc(u_char type,
3279 if_com_alloc_t *a, if_com_free_t *f)
3282 KASSERT(if_com_alloc[type] == NULL,
3283 ("if_register_com_alloc: %d already registered", type));
3284 KASSERT(if_com_free[type] == NULL,
3285 ("if_register_com_alloc: %d free already registered", type));
3287 if_com_alloc[type] = a;
3288 if_com_free[type] = f;
3291 void
3292 if_deregister_com_alloc(u_char type)
3295 KASSERT(if_com_alloc[type] != NULL,
3296 ("if_deregister_com_alloc: %d not registered", type));
3297 KASSERT(if_com_free[type] != NULL,
3298 ("if_deregister_com_alloc: %d free not registered", type));
3299 if_com_alloc[type] = NULL;
3300 if_com_free[type] = NULL;
3304 if_ring_count2(int cnt, int cnt_max)
3306 int shift = 0;
3308 KASSERT(cnt_max >= 1 && powerof2(cnt_max),
3309 ("invalid ring count max %d", cnt_max));
3311 if (cnt <= 0)
3312 cnt = cnt_max;
3313 if (cnt > ncpus2)
3314 cnt = ncpus2;
3315 if (cnt > cnt_max)
3316 cnt = cnt_max;
3318 while ((1 << (shift + 1)) <= cnt)
3319 ++shift;
3320 cnt = 1 << shift;
3322 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
3323 ("calculate cnt %d, ncpus2 %d, cnt max %d",
3324 cnt, ncpus2, cnt_max));
3325 return cnt;
3328 void
3329 ifq_set_maxlen(struct ifaltq *ifq, int len)
3331 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3335 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3337 return ALTQ_SUBQ_INDEX_DEFAULT;
3341 ifq_mapsubq_mask(struct ifaltq *ifq, int cpuid)
3343 return (cpuid & ifq->altq_subq_mask);
3346 static void
3347 ifsq_watchdog(void *arg)
3349 struct ifsubq_watchdog *wd = arg;
3350 struct ifnet *ifp;
3352 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3353 goto done;
3355 ifp = ifsq_get_ifp(wd->wd_subq);
3356 if (ifnet_tryserialize_all(ifp)) {
3357 wd->wd_watchdog(wd->wd_subq);
3358 ifnet_deserialize_all(ifp);
3359 } else {
3360 /* try again next timeout */
3361 wd->wd_timer = 1;
3363 done:
3364 ifsq_watchdog_reset(wd);
3367 static void
3368 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3370 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3371 ifsq_get_cpuid(wd->wd_subq));
3374 void
3375 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3376 ifsq_watchdog_t watchdog)
3378 callout_init_mp(&wd->wd_callout);
3379 wd->wd_timer = 0;
3380 wd->wd_subq = ifsq;
3381 wd->wd_watchdog = watchdog;
3384 void
3385 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3387 wd->wd_timer = 0;
3388 ifsq_watchdog_reset(wd);
3391 void
3392 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3394 wd->wd_timer = 0;
3395 callout_stop(&wd->wd_callout);
3398 void
3399 ifnet_lock(void)
3401 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3402 ("try holding ifnet lock in netisr"));
3403 mtx_lock(&ifnet_mtx);
3406 void
3407 ifnet_unlock(void)
3409 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3410 ("try holding ifnet lock in netisr"));
3411 mtx_unlock(&ifnet_mtx);
3414 static struct ifnet_array *
3415 ifnet_array_alloc(int count)
3417 struct ifnet_array *arr;
3419 arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]),
3420 M_IFNET, M_WAITOK);
3421 arr->ifnet_count = count;
3423 return arr;
3426 static void
3427 ifnet_array_free(struct ifnet_array *arr)
3429 if (arr == &ifnet_array0)
3430 return;
3431 kfree(arr, M_IFNET);
3434 static struct ifnet_array *
3435 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr)
3437 struct ifnet_array *arr;
3438 int count, i;
3440 KASSERT(old_arr->ifnet_count >= 0,
3441 ("invalid ifnet array count %d", old_arr->ifnet_count));
3442 count = old_arr->ifnet_count + 1;
3443 arr = ifnet_array_alloc(count);
3446 * Save the old ifnet array and append this ifp to the end of
3447 * the new ifnet array.
3449 for (i = 0; i < old_arr->ifnet_count; ++i) {
3450 KASSERT(old_arr->ifnet_arr[i] != ifp,
3451 ("%s is already in ifnet array", ifp->if_xname));
3452 arr->ifnet_arr[i] = old_arr->ifnet_arr[i];
3454 KASSERT(i == count - 1,
3455 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3456 ifp->if_xname, count - 1, i));
3457 arr->ifnet_arr[i] = ifp;
3459 return arr;
3462 static struct ifnet_array *
3463 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr)
3465 struct ifnet_array *arr;
3466 int count, i, idx, found = 0;
3468 KASSERT(old_arr->ifnet_count > 0,
3469 ("invalid ifnet array count %d", old_arr->ifnet_count));
3470 count = old_arr->ifnet_count - 1;
3471 arr = ifnet_array_alloc(count);
3474 * Save the old ifnet array, but skip this ifp.
3476 idx = 0;
3477 for (i = 0; i < old_arr->ifnet_count; ++i) {
3478 if (old_arr->ifnet_arr[i] == ifp) {
3479 KASSERT(!found,
3480 ("dup %s is in ifnet array", ifp->if_xname));
3481 found = 1;
3482 continue;
3484 KASSERT(idx < count,
3485 ("invalid ifnet array index %d, count %d", idx, count));
3486 arr->ifnet_arr[idx] = old_arr->ifnet_arr[i];
3487 ++idx;
3489 KASSERT(found, ("%s is not in ifnet array", ifp->if_xname));
3490 KASSERT(idx == count,
3491 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3492 ifp->if_xname, count, idx));
3494 return arr;
3497 const struct ifnet_array *
3498 ifnet_array_get(void)
3500 const struct ifnet_array *ret;
3502 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3503 ret = ifnet_array;
3504 /* Make sure 'ret' is really used. */
3505 cpu_ccfence();
3506 return (ret);
3510 ifnet_array_isempty(void)
3512 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3513 if (ifnet_array->ifnet_count == 0)
3514 return 1;
3515 else
3516 return 0;
3519 void
3520 ifa_marker_init(struct ifaddr_marker *mark, struct ifnet *ifp)
3522 struct ifaddr *ifa;
3524 memset(mark, 0, sizeof(*mark));
3525 ifa = &mark->ifa;
3527 mark->ifac.ifa = ifa;
3529 ifa->ifa_addr = &mark->addr;
3530 ifa->ifa_dstaddr = &mark->dstaddr;
3531 ifa->ifa_netmask = &mark->netmask;
3532 ifa->ifa_ifp = ifp;