net: Reorder sysinit a bit.
[dragonfly.git] / sys / net / if.c
blob649ff65ce22f1e6d5dc38fa0cb69366685770999
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 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, ifnetinit, NULL);
146 static if_com_alloc_t *if_com_alloc[256];
147 static if_com_free_t *if_com_free[256];
149 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
150 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
151 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
153 int ifqmaxlen = IFQ_MAXLEN;
154 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
156 static struct ifnet_array ifnet_array0;
157 static struct ifnet_array *ifnet_array = &ifnet_array0;
159 static struct callout if_slowtimo_timer;
160 static struct netmsg_base if_slowtimo_netmsg;
162 int if_index = 0;
163 struct ifnet **ifindex2ifnet = NULL;
164 static struct mtx ifnet_mtx = MTX_INITIALIZER("ifnet");
166 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
168 #ifdef notyet
169 #define IFQ_KTR_STRING "ifq=%p"
170 #define IFQ_KTR_ARGS struct ifaltq *ifq
171 #ifndef KTR_IFQ
172 #define KTR_IFQ KTR_ALL
173 #endif
174 KTR_INFO_MASTER(ifq);
175 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
176 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
177 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
179 #define IF_START_KTR_STRING "ifp=%p"
180 #define IF_START_KTR_ARGS struct ifnet *ifp
181 #ifndef KTR_IF_START
182 #define KTR_IF_START KTR_ALL
183 #endif
184 KTR_INFO_MASTER(if_start);
185 KTR_INFO(KTR_IF_START, if_start, run, 0,
186 IF_START_KTR_STRING, IF_START_KTR_ARGS);
187 KTR_INFO(KTR_IF_START, if_start, sched, 1,
188 IF_START_KTR_STRING, IF_START_KTR_ARGS);
189 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
190 IF_START_KTR_STRING, IF_START_KTR_ARGS);
191 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
192 IF_START_KTR_STRING, IF_START_KTR_ARGS);
193 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
194 IF_START_KTR_STRING, IF_START_KTR_ARGS);
195 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
196 #endif
198 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
201 * Network interface utility routines.
203 * Routines with ifa_ifwith* names take sockaddr *'s as
204 * parameters.
206 /* ARGSUSED*/
207 static void
208 ifinit(void *dummy)
210 struct ifnet *ifp;
212 callout_init_mp(&if_slowtimo_timer);
213 netmsg_init(&if_slowtimo_netmsg, NULL, &netisr_adone_rport,
214 MSGF_PRIORITY, if_slowtimo_dispatch);
216 /* XXX is this necessary? */
217 ifnet_lock();
218 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
219 if (ifp->if_snd.altq_maxlen == 0) {
220 if_printf(ifp, "XXX: driver didn't set altq_maxlen\n");
221 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
224 ifnet_unlock();
226 /* Start if_slowtimo */
227 lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg.lmsg);
230 static void
231 ifsq_ifstart_ipifunc(void *arg)
233 struct ifaltq_subque *ifsq = arg;
234 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
236 crit_enter();
237 if (lmsg->ms_flags & MSGF_DONE)
238 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg);
239 crit_exit();
242 static __inline void
243 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
245 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
246 TAILQ_REMOVE(&head->stg_head, stage, stg_link);
247 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
248 stage->stg_cnt = 0;
249 stage->stg_len = 0;
252 static __inline void
253 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
255 KKASSERT((stage->stg_flags &
256 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
257 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
258 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
262 * Schedule ifnet.if_start on the subqueue owner CPU
264 static void
265 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
267 int cpu;
269 if (!force && curthread->td_type == TD_TYPE_NETISR &&
270 ifsq_stage_cntmax > 0) {
271 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
273 stage->stg_cnt = 0;
274 stage->stg_len = 0;
275 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
276 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
277 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
278 return;
281 cpu = ifsq_get_cpuid(ifsq);
282 if (cpu != mycpuid)
283 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
284 else
285 ifsq_ifstart_ipifunc(ifsq);
289 * NOTE:
290 * This function will release ifnet.if_start subqueue interlock,
291 * if ifnet.if_start for the subqueue does not need to be scheduled
293 static __inline int
294 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
296 if (!running || ifsq_is_empty(ifsq)
297 #ifdef ALTQ
298 || ifsq->ifsq_altq->altq_tbr != NULL
299 #endif
301 ALTQ_SQ_LOCK(ifsq);
303 * ifnet.if_start subqueue interlock is released, if:
304 * 1) Hardware can not take any packets, due to
305 * o interface is marked down
306 * o hardware queue is full (ifsq_is_oactive)
307 * Under the second situation, hardware interrupt
308 * or polling(4) will call/schedule ifnet.if_start
309 * on the subqueue when hardware queue is ready
310 * 2) There is no packet in the subqueue.
311 * Further ifq_dispatch or ifq_handoff will call/
312 * schedule ifnet.if_start on the subqueue.
313 * 3) TBR is used and it does not allow further
314 * dequeueing.
315 * TBR callout will call ifnet.if_start on the
316 * subqueue.
318 if (!running || !ifsq_data_ready(ifsq)) {
319 ifsq_clr_started(ifsq);
320 ALTQ_SQ_UNLOCK(ifsq);
321 return 0;
323 ALTQ_SQ_UNLOCK(ifsq);
325 return 1;
328 static void
329 ifsq_ifstart_dispatch(netmsg_t msg)
331 struct lwkt_msg *lmsg = &msg->base.lmsg;
332 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
333 struct ifnet *ifp = ifsq_get_ifp(ifsq);
334 struct globaldata *gd = mycpu;
335 int running = 0, need_sched;
337 crit_enter_gd(gd);
339 lwkt_replymsg(lmsg, 0); /* reply ASAP */
341 if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) {
343 * We need to chase the subqueue owner CPU change.
345 ifsq_ifstart_schedule(ifsq, 1);
346 crit_exit_gd(gd);
347 return;
350 ifsq_serialize_hw(ifsq);
351 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
352 ifp->if_start(ifp, ifsq);
353 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
354 running = 1;
356 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
357 ifsq_deserialize_hw(ifsq);
359 if (need_sched) {
361 * More data need to be transmitted, ifnet.if_start is
362 * scheduled on the subqueue owner CPU, and we keep going.
363 * NOTE: ifnet.if_start subqueue interlock is not released.
365 ifsq_ifstart_schedule(ifsq, 0);
368 crit_exit_gd(gd);
371 /* Device driver ifnet.if_start helper function */
372 void
373 ifsq_devstart(struct ifaltq_subque *ifsq)
375 struct ifnet *ifp = ifsq_get_ifp(ifsq);
376 int running = 0;
378 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
380 ALTQ_SQ_LOCK(ifsq);
381 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
382 ALTQ_SQ_UNLOCK(ifsq);
383 return;
385 ifsq_set_started(ifsq);
386 ALTQ_SQ_UNLOCK(ifsq);
388 ifp->if_start(ifp, ifsq);
390 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
391 running = 1;
393 if (ifsq_ifstart_need_schedule(ifsq, running)) {
395 * More data need to be transmitted, ifnet.if_start is
396 * scheduled on ifnet's CPU, and we keep going.
397 * NOTE: ifnet.if_start interlock is not released.
399 ifsq_ifstart_schedule(ifsq, 0);
403 void
404 if_devstart(struct ifnet *ifp)
406 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
409 /* Device driver ifnet.if_start schedule helper function */
410 void
411 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
413 ifsq_ifstart_schedule(ifsq, 1);
416 void
417 if_devstart_sched(struct ifnet *ifp)
419 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
422 static void
423 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
425 lwkt_serialize_enter(ifp->if_serializer);
428 static void
429 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
431 lwkt_serialize_exit(ifp->if_serializer);
434 static int
435 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
437 return lwkt_serialize_try(ifp->if_serializer);
440 #ifdef INVARIANTS
441 static void
442 if_default_serialize_assert(struct ifnet *ifp,
443 enum ifnet_serialize slz __unused,
444 boolean_t serialized)
446 if (serialized)
447 ASSERT_SERIALIZED(ifp->if_serializer);
448 else
449 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
451 #endif
454 * Attach an interface to the list of "active" interfaces.
456 * The serializer is optional.
458 void
459 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
461 unsigned socksize;
462 int namelen, masklen;
463 struct sockaddr_dl *sdl, *sdl_addr;
464 struct ifaddr *ifa;
465 struct ifaltq *ifq;
466 struct ifnet **old_ifindex2ifnet = NULL;
467 struct ifnet_array *old_ifnet_array;
468 int i, q;
470 static int if_indexlim = 8;
472 if (ifp->if_serialize != NULL) {
473 KASSERT(ifp->if_deserialize != NULL &&
474 ifp->if_tryserialize != NULL &&
475 ifp->if_serialize_assert != NULL,
476 ("serialize functions are partially setup"));
479 * If the device supplies serialize functions,
480 * then clear if_serializer to catch any invalid
481 * usage of this field.
483 KASSERT(serializer == NULL,
484 ("both serialize functions and default serializer "
485 "are supplied"));
486 ifp->if_serializer = NULL;
487 } else {
488 KASSERT(ifp->if_deserialize == NULL &&
489 ifp->if_tryserialize == NULL &&
490 ifp->if_serialize_assert == NULL,
491 ("serialize functions are partially setup"));
492 ifp->if_serialize = if_default_serialize;
493 ifp->if_deserialize = if_default_deserialize;
494 ifp->if_tryserialize = if_default_tryserialize;
495 #ifdef INVARIANTS
496 ifp->if_serialize_assert = if_default_serialize_assert;
497 #endif
500 * The serializer can be passed in from the device,
501 * allowing the same serializer to be used for both
502 * the interrupt interlock and the device queue.
503 * If not specified, the netif structure will use an
504 * embedded serializer.
506 if (serializer == NULL) {
507 serializer = &ifp->if_default_serializer;
508 lwkt_serialize_init(serializer);
510 ifp->if_serializer = serializer;
514 * XXX -
515 * The old code would work if the interface passed a pre-existing
516 * chain of ifaddrs to this code. We don't trust our callers to
517 * properly initialize the tailq, however, so we no longer allow
518 * this unlikely case.
520 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
521 M_IFADDR, M_WAITOK | M_ZERO);
522 for (i = 0; i < ncpus; ++i)
523 TAILQ_INIT(&ifp->if_addrheads[i]);
525 TAILQ_INIT(&ifp->if_multiaddrs);
526 TAILQ_INIT(&ifp->if_groups);
527 getmicrotime(&ifp->if_lastchange);
530 * create a Link Level name for this device
532 namelen = strlen(ifp->if_xname);
533 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
534 socksize = masklen + ifp->if_addrlen;
535 if (socksize < sizeof(*sdl))
536 socksize = sizeof(*sdl);
537 socksize = RT_ROUNDUP(socksize);
538 ifa = ifa_create(sizeof(struct ifaddr) + 2 * socksize);
539 sdl = sdl_addr = (struct sockaddr_dl *)(ifa + 1);
540 sdl->sdl_len = socksize;
541 sdl->sdl_family = AF_LINK;
542 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
543 sdl->sdl_nlen = namelen;
544 sdl->sdl_type = ifp->if_type;
545 ifp->if_lladdr = ifa;
546 ifa->ifa_ifp = ifp;
547 ifa->ifa_rtrequest = link_rtrequest;
548 ifa->ifa_addr = (struct sockaddr *)sdl;
549 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
550 ifa->ifa_netmask = (struct sockaddr *)sdl;
551 sdl->sdl_len = masklen;
552 while (namelen != 0)
553 sdl->sdl_data[--namelen] = 0xff;
554 ifa_iflink(ifa, ifp, 0 /* Insert head */);
556 ifp->if_data_pcpu = kmalloc_cachealign(
557 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
559 if (ifp->if_mapsubq == NULL)
560 ifp->if_mapsubq = ifq_mapsubq_default;
562 ifq = &ifp->if_snd;
563 ifq->altq_type = 0;
564 ifq->altq_disc = NULL;
565 ifq->altq_flags &= ALTQF_CANTCHANGE;
566 ifq->altq_tbr = NULL;
567 ifq->altq_ifp = ifp;
569 if (ifq->altq_subq_cnt <= 0)
570 ifq->altq_subq_cnt = 1;
571 ifq->altq_subq = kmalloc_cachealign(
572 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
573 M_DEVBUF, M_WAITOK | M_ZERO);
575 if (ifq->altq_maxlen == 0) {
576 if_printf(ifp, "driver didn't set altq_maxlen\n");
577 ifq_set_maxlen(ifq, ifqmaxlen);
580 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
581 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
583 ALTQ_SQ_LOCK_INIT(ifsq);
584 ifsq->ifsq_index = q;
586 ifsq->ifsq_altq = ifq;
587 ifsq->ifsq_ifp = ifp;
589 ifsq->ifsq_maxlen = ifq->altq_maxlen;
590 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
591 ifsq->ifsq_prepended = NULL;
592 ifsq->ifsq_started = 0;
593 ifsq->ifsq_hw_oactive = 0;
594 ifsq_set_cpuid(ifsq, 0);
595 if (ifp->if_serializer != NULL)
596 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
598 ifsq->ifsq_stage =
599 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
600 M_DEVBUF, M_WAITOK | M_ZERO);
601 for (i = 0; i < ncpus; ++i)
602 ifsq->ifsq_stage[i].stg_subq = ifsq;
604 ifsq->ifsq_ifstart_nmsg =
605 kmalloc(ncpus * sizeof(struct netmsg_base),
606 M_LWKTMSG, M_WAITOK);
607 for (i = 0; i < ncpus; ++i) {
608 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
609 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
610 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
613 ifq_set_classic(ifq);
616 * Increase mbuf cluster/jcluster limits for the mbufs that
617 * could sit on the device queues for quite some time.
619 if (ifp->if_nmbclusters > 0)
620 mcl_inclimit(ifp->if_nmbclusters);
621 if (ifp->if_nmbjclusters > 0)
622 mjcl_inclimit(ifp->if_nmbjclusters);
625 * Install this ifp into ifindex2inet, ifnet queue and ifnet
626 * array after it is setup.
628 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
629 * by ifnet lock, so that non-netisr threads could get a
630 * consistent view.
632 ifnet_lock();
634 /* Don't update if_index until ifindex2ifnet is setup */
635 ifp->if_index = if_index + 1;
636 sdl_addr->sdl_index = ifp->if_index;
639 * Install this ifp into ifindex2ifnet
641 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
642 unsigned int n;
643 struct ifnet **q;
646 * Grow ifindex2ifnet
648 if_indexlim <<= 1;
649 n = if_indexlim * sizeof(*q);
650 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
651 if (ifindex2ifnet != NULL) {
652 bcopy(ifindex2ifnet, q, n/2);
653 /* Free old ifindex2ifnet after sync all netisrs */
654 old_ifindex2ifnet = ifindex2ifnet;
656 ifindex2ifnet = q;
658 ifindex2ifnet[ifp->if_index] = ifp;
660 * Update if_index after this ifp is installed into ifindex2ifnet,
661 * so that netisrs could get a consistent view of ifindex2ifnet.
663 cpu_sfence();
664 if_index = ifp->if_index;
667 * Install this ifp into ifnet array.
669 /* Free old ifnet array after sync all netisrs */
670 old_ifnet_array = ifnet_array;
671 ifnet_array = ifnet_array_add(ifp, old_ifnet_array);
674 * Install this ifp into ifnet queue.
676 TAILQ_INSERT_TAIL(&ifnetlist, ifp, if_link);
678 ifnet_unlock();
681 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
682 * are no longer accessed and we can free them safely later on.
684 netmsg_service_sync();
685 if (old_ifindex2ifnet != NULL)
686 kfree(old_ifindex2ifnet, M_IFADDR);
687 ifnet_array_free(old_ifnet_array);
689 if (!SLIST_EMPTY(&domains))
690 if_attachdomain1(ifp);
692 /* Announce the interface. */
693 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
694 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
695 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
698 static void
699 if_attachdomain(void *dummy)
701 struct ifnet *ifp;
703 ifnet_lock();
704 TAILQ_FOREACH(ifp, &ifnetlist, if_list)
705 if_attachdomain1(ifp);
706 ifnet_unlock();
708 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
709 if_attachdomain, NULL);
711 static void
712 if_attachdomain1(struct ifnet *ifp)
714 struct domain *dp;
716 crit_enter();
718 /* address family dependent data region */
719 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
720 SLIST_FOREACH(dp, &domains, dom_next)
721 if (dp->dom_ifattach)
722 ifp->if_afdata[dp->dom_family] =
723 (*dp->dom_ifattach)(ifp);
724 crit_exit();
728 * Purge all addresses whose type is _not_ AF_LINK
730 static void
731 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg)
733 struct lwkt_msg *lmsg = &nmsg->lmsg;
734 struct ifnet *ifp = lmsg->u.ms_resultp;
735 struct ifaddr_container *ifac, *next;
737 ASSERT_IN_NETISR(0);
740 * The ifaddr processing in the following loop will block,
741 * however, this function is called in netisr0, in which
742 * ifaddr list changes happen, so we don't care about the
743 * blockness of the ifaddr processing here.
745 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
746 ifa_link, next) {
747 struct ifaddr *ifa = ifac->ifa;
749 /* Ignore marker */
750 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
751 continue;
753 /* Leave link ifaddr as it is */
754 if (ifa->ifa_addr->sa_family == AF_LINK)
755 continue;
756 #ifdef INET
757 /* XXX: Ugly!! ad hoc just for INET */
758 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
759 struct ifaliasreq ifr;
760 #ifdef IFADDR_DEBUG_VERBOSE
761 int i;
763 kprintf("purge in4 addr %p: ", ifa);
764 for (i = 0; i < ncpus; ++i)
765 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
766 kprintf("\n");
767 #endif
769 bzero(&ifr, sizeof ifr);
770 ifr.ifra_addr = *ifa->ifa_addr;
771 if (ifa->ifa_dstaddr)
772 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
773 if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp,
774 NULL) == 0)
775 continue;
777 #endif /* INET */
778 #ifdef INET6
779 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
780 #ifdef IFADDR_DEBUG_VERBOSE
781 int i;
783 kprintf("purge in6 addr %p: ", ifa);
784 for (i = 0; i < ncpus; ++i)
785 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
786 kprintf("\n");
787 #endif
789 in6_purgeaddr(ifa);
790 /* ifp_addrhead is already updated */
791 continue;
793 #endif /* INET6 */
794 ifa_ifunlink(ifa, ifp);
795 ifa_destroy(ifa);
798 lwkt_replymsg(lmsg, 0);
801 void
802 if_purgeaddrs_nolink(struct ifnet *ifp)
804 struct netmsg_base nmsg;
805 struct lwkt_msg *lmsg = &nmsg.lmsg;
807 ASSERT_CANDOMSG_NETISR0(curthread);
809 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
810 if_purgeaddrs_nolink_dispatch);
811 lmsg->u.ms_resultp = ifp;
812 lwkt_domsg(netisr_cpuport(0), lmsg, 0);
815 static void
816 ifq_stage_detach_handler(netmsg_t nmsg)
818 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
819 int q;
821 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
822 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
823 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
825 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
826 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
828 lwkt_replymsg(&nmsg->lmsg, 0);
831 static void
832 ifq_stage_detach(struct ifaltq *ifq)
834 struct netmsg_base base;
835 int cpu;
837 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
838 ifq_stage_detach_handler);
839 base.lmsg.u.ms_resultp = ifq;
841 for (cpu = 0; cpu < ncpus; ++cpu)
842 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
845 struct netmsg_if_rtdel {
846 struct netmsg_base base;
847 struct ifnet *ifp;
850 static void
851 if_rtdel_dispatch(netmsg_t msg)
853 struct netmsg_if_rtdel *rmsg = (void *)msg;
854 int i, nextcpu, cpu;
856 cpu = mycpuid;
857 for (i = 1; i <= AF_MAX; i++) {
858 struct radix_node_head *rnh;
860 if ((rnh = rt_tables[cpu][i]) == NULL)
861 continue;
862 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
865 nextcpu = cpu + 1;
866 if (nextcpu < ncpus)
867 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
868 else
869 lwkt_replymsg(&rmsg->base.lmsg, 0);
873 * Detach an interface, removing it from the
874 * list of "active" interfaces.
876 void
877 if_detach(struct ifnet *ifp)
879 struct ifnet_array *old_ifnet_array;
880 struct netmsg_if_rtdel msg;
881 struct domain *dp;
882 int q;
884 /* Announce that the interface is gone. */
885 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
886 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
887 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
890 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
891 * array before it is whacked.
893 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
894 * by ifnet lock, so that non-netisr threads could get a
895 * consistent view.
897 ifnet_lock();
900 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
902 ifindex2ifnet[ifp->if_index] = NULL;
903 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
904 if_index--;
907 * Remove this ifp from ifnet queue.
909 TAILQ_REMOVE(&ifnetlist, ifp, if_link);
912 * Remove this ifp from ifnet array.
914 /* Free old ifnet array after sync all netisrs */
915 old_ifnet_array = ifnet_array;
916 ifnet_array = ifnet_array_del(ifp, old_ifnet_array);
918 ifnet_unlock();
921 * Sync all netisrs so that the old ifnet array is no longer
922 * accessed and we can free it safely later on.
924 netmsg_service_sync();
925 ifnet_array_free(old_ifnet_array);
928 * Remove routes and flush queues.
930 crit_enter();
931 #ifdef IFPOLL_ENABLE
932 if (ifp->if_flags & IFF_NPOLLING)
933 ifpoll_deregister(ifp);
934 #endif
935 if_down(ifp);
937 /* Decrease the mbuf clusters/jclusters limits increased by us */
938 if (ifp->if_nmbclusters > 0)
939 mcl_inclimit(-ifp->if_nmbclusters);
940 if (ifp->if_nmbjclusters > 0)
941 mjcl_inclimit(-ifp->if_nmbjclusters);
943 #ifdef ALTQ
944 if (ifq_is_enabled(&ifp->if_snd))
945 altq_disable(&ifp->if_snd);
946 if (ifq_is_attached(&ifp->if_snd))
947 altq_detach(&ifp->if_snd);
948 #endif
951 * Clean up all addresses.
953 ifp->if_lladdr = NULL;
955 if_purgeaddrs_nolink(ifp);
956 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
957 struct ifaddr *ifa;
959 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
960 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
961 ("non-link ifaddr is left on if_addrheads"));
963 ifa_ifunlink(ifa, ifp);
964 ifa_destroy(ifa);
965 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
966 ("there are still ifaddrs left on if_addrheads"));
969 #ifdef INET
971 * Remove all IPv4 kernel structures related to ifp.
973 in_ifdetach(ifp);
974 #endif
976 #ifdef INET6
978 * Remove all IPv6 kernel structs related to ifp. This should be done
979 * before removing routing entries below, since IPv6 interface direct
980 * routes are expected to be removed by the IPv6-specific kernel API.
981 * Otherwise, the kernel will detect some inconsistency and bark it.
983 in6_ifdetach(ifp);
984 #endif
987 * Delete all remaining routes using this interface
989 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
990 if_rtdel_dispatch);
991 msg.ifp = ifp;
992 rt_domsg_global(&msg.base);
994 SLIST_FOREACH(dp, &domains, dom_next)
995 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
996 (*dp->dom_ifdetach)(ifp,
997 ifp->if_afdata[dp->dom_family]);
999 kfree(ifp->if_addrheads, M_IFADDR);
1001 lwkt_synchronize_ipiqs("if_detach");
1002 ifq_stage_detach(&ifp->if_snd);
1004 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
1005 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
1007 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
1008 kfree(ifsq->ifsq_stage, M_DEVBUF);
1010 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
1012 kfree(ifp->if_data_pcpu, M_DEVBUF);
1014 crit_exit();
1018 * Create interface group without members
1020 struct ifg_group *
1021 if_creategroup(const char *groupname)
1023 struct ifg_group *ifg = NULL;
1025 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
1026 M_TEMP, M_NOWAIT)) == NULL)
1027 return (NULL);
1029 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1030 ifg->ifg_refcnt = 0;
1031 ifg->ifg_carp_demoted = 0;
1032 TAILQ_INIT(&ifg->ifg_members);
1033 #if NPF > 0
1034 pfi_attach_ifgroup(ifg);
1035 #endif
1036 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
1038 return (ifg);
1042 * Add a group to an interface
1045 if_addgroup(struct ifnet *ifp, const char *groupname)
1047 struct ifg_list *ifgl;
1048 struct ifg_group *ifg = NULL;
1049 struct ifg_member *ifgm;
1051 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1052 groupname[strlen(groupname) - 1] <= '9')
1053 return (EINVAL);
1055 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1056 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1057 return (EEXIST);
1059 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
1060 return (ENOMEM);
1062 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1063 kfree(ifgl, M_TEMP);
1064 return (ENOMEM);
1067 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1068 if (!strcmp(ifg->ifg_group, groupname))
1069 break;
1071 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
1072 kfree(ifgl, M_TEMP);
1073 kfree(ifgm, M_TEMP);
1074 return (ENOMEM);
1077 ifg->ifg_refcnt++;
1078 ifgl->ifgl_group = ifg;
1079 ifgm->ifgm_ifp = ifp;
1081 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1082 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1084 #if NPF > 0
1085 pfi_group_change(groupname);
1086 #endif
1088 return (0);
1092 * Remove a group from an interface
1095 if_delgroup(struct ifnet *ifp, const char *groupname)
1097 struct ifg_list *ifgl;
1098 struct ifg_member *ifgm;
1100 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1101 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1102 break;
1103 if (ifgl == NULL)
1104 return (ENOENT);
1106 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1108 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1109 if (ifgm->ifgm_ifp == ifp)
1110 break;
1112 if (ifgm != NULL) {
1113 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1114 kfree(ifgm, M_TEMP);
1117 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1118 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1119 #if NPF > 0
1120 pfi_detach_ifgroup(ifgl->ifgl_group);
1121 #endif
1122 kfree(ifgl->ifgl_group, M_TEMP);
1125 kfree(ifgl, M_TEMP);
1127 #if NPF > 0
1128 pfi_group_change(groupname);
1129 #endif
1131 return (0);
1135 * Stores all groups from an interface in memory pointed
1136 * to by data
1139 if_getgroup(caddr_t data, struct ifnet *ifp)
1141 int len, error;
1142 struct ifg_list *ifgl;
1143 struct ifg_req ifgrq, *ifgp;
1144 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1146 if (ifgr->ifgr_len == 0) {
1147 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1148 ifgr->ifgr_len += sizeof(struct ifg_req);
1149 return (0);
1152 len = ifgr->ifgr_len;
1153 ifgp = ifgr->ifgr_groups;
1154 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1155 if (len < sizeof(ifgrq))
1156 return (EINVAL);
1157 bzero(&ifgrq, sizeof ifgrq);
1158 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1159 sizeof(ifgrq.ifgrq_group));
1160 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1161 sizeof(struct ifg_req))))
1162 return (error);
1163 len -= sizeof(ifgrq);
1164 ifgp++;
1167 return (0);
1171 * Stores all members of a group in memory pointed to by data
1174 if_getgroupmembers(caddr_t data)
1176 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1177 struct ifg_group *ifg;
1178 struct ifg_member *ifgm;
1179 struct ifg_req ifgrq, *ifgp;
1180 int len, error;
1182 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1183 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1184 break;
1185 if (ifg == NULL)
1186 return (ENOENT);
1188 if (ifgr->ifgr_len == 0) {
1189 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1190 ifgr->ifgr_len += sizeof(ifgrq);
1191 return (0);
1194 len = ifgr->ifgr_len;
1195 ifgp = ifgr->ifgr_groups;
1196 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1197 if (len < sizeof(ifgrq))
1198 return (EINVAL);
1199 bzero(&ifgrq, sizeof ifgrq);
1200 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1201 sizeof(ifgrq.ifgrq_member));
1202 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1203 sizeof(struct ifg_req))))
1204 return (error);
1205 len -= sizeof(ifgrq);
1206 ifgp++;
1209 return (0);
1213 * Delete Routes for a Network Interface
1215 * Called for each routing entry via the rnh->rnh_walktree() call above
1216 * to delete all route entries referencing a detaching network interface.
1218 * Arguments:
1219 * rn pointer to node in the routing table
1220 * arg argument passed to rnh->rnh_walktree() - detaching interface
1222 * Returns:
1223 * 0 successful
1224 * errno failed - reason indicated
1227 static int
1228 if_rtdel(struct radix_node *rn, void *arg)
1230 struct rtentry *rt = (struct rtentry *)rn;
1231 struct ifnet *ifp = arg;
1232 int err;
1234 if (rt->rt_ifp == ifp) {
1237 * Protect (sorta) against walktree recursion problems
1238 * with cloned routes
1240 if (!(rt->rt_flags & RTF_UP))
1241 return (0);
1243 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1244 rt_mask(rt), rt->rt_flags,
1245 NULL);
1246 if (err) {
1247 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1251 return (0);
1254 static __inline boolean_t
1255 ifa_prefer(const struct ifaddr *cur_ifa, const struct ifaddr *old_ifa)
1257 if (old_ifa == NULL)
1258 return TRUE;
1260 if ((old_ifa->ifa_ifp->if_flags & IFF_UP) == 0 &&
1261 (cur_ifa->ifa_ifp->if_flags & IFF_UP))
1262 return TRUE;
1263 if ((old_ifa->ifa_flags & IFA_ROUTE) == 0 &&
1264 (cur_ifa->ifa_flags & IFA_ROUTE))
1265 return TRUE;
1266 return FALSE;
1270 * Locate an interface based on a complete address.
1272 struct ifaddr *
1273 ifa_ifwithaddr(struct sockaddr *addr)
1275 const struct ifnet_array *arr;
1276 int i;
1278 arr = ifnet_array_get();
1279 for (i = 0; i < arr->ifnet_count; ++i) {
1280 struct ifnet *ifp = arr->ifnet_arr[i];
1281 struct ifaddr_container *ifac;
1283 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1284 struct ifaddr *ifa = ifac->ifa;
1286 if (ifa->ifa_addr->sa_family != addr->sa_family)
1287 continue;
1288 if (sa_equal(addr, ifa->ifa_addr))
1289 return (ifa);
1290 if ((ifp->if_flags & IFF_BROADCAST) &&
1291 ifa->ifa_broadaddr &&
1292 /* IPv6 doesn't have broadcast */
1293 ifa->ifa_broadaddr->sa_len != 0 &&
1294 sa_equal(ifa->ifa_broadaddr, addr))
1295 return (ifa);
1298 return (NULL);
1302 * Locate the point to point interface with a given destination address.
1304 struct ifaddr *
1305 ifa_ifwithdstaddr(struct sockaddr *addr)
1307 const struct ifnet_array *arr;
1308 int i;
1310 arr = ifnet_array_get();
1311 for (i = 0; i < arr->ifnet_count; ++i) {
1312 struct ifnet *ifp = arr->ifnet_arr[i];
1313 struct ifaddr_container *ifac;
1315 if (!(ifp->if_flags & IFF_POINTOPOINT))
1316 continue;
1318 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1319 struct ifaddr *ifa = ifac->ifa;
1321 if (ifa->ifa_addr->sa_family != addr->sa_family)
1322 continue;
1323 if (ifa->ifa_dstaddr &&
1324 sa_equal(addr, ifa->ifa_dstaddr))
1325 return (ifa);
1328 return (NULL);
1332 * Find an interface on a specific network. If many, choice
1333 * is most specific found.
1335 struct ifaddr *
1336 ifa_ifwithnet(struct sockaddr *addr)
1338 struct ifaddr *ifa_maybe = NULL;
1339 u_int af = addr->sa_family;
1340 char *addr_data = addr->sa_data, *cplim;
1341 const struct ifnet_array *arr;
1342 int i;
1345 * AF_LINK addresses can be looked up directly by their index number,
1346 * so do that if we can.
1348 if (af == AF_LINK) {
1349 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1351 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1352 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1356 * Scan though each interface, looking for ones that have
1357 * addresses in this address family.
1359 arr = ifnet_array_get();
1360 for (i = 0; i < arr->ifnet_count; ++i) {
1361 struct ifnet *ifp = arr->ifnet_arr[i];
1362 struct ifaddr_container *ifac;
1364 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1365 struct ifaddr *ifa = ifac->ifa;
1366 char *cp, *cp2, *cp3;
1368 if (ifa->ifa_addr->sa_family != af)
1369 next: continue;
1370 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1372 * This is a bit broken as it doesn't
1373 * take into account that the remote end may
1374 * be a single node in the network we are
1375 * looking for.
1376 * The trouble is that we don't know the
1377 * netmask for the remote end.
1379 if (ifa->ifa_dstaddr != NULL &&
1380 sa_equal(addr, ifa->ifa_dstaddr))
1381 return (ifa);
1382 } else {
1384 * if we have a special address handler,
1385 * then use it instead of the generic one.
1387 if (ifa->ifa_claim_addr) {
1388 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1389 return (ifa);
1390 } else {
1391 continue;
1396 * Scan all the bits in the ifa's address.
1397 * If a bit dissagrees with what we are
1398 * looking for, mask it with the netmask
1399 * to see if it really matters.
1400 * (A byte at a time)
1402 if (ifa->ifa_netmask == 0)
1403 continue;
1404 cp = addr_data;
1405 cp2 = ifa->ifa_addr->sa_data;
1406 cp3 = ifa->ifa_netmask->sa_data;
1407 cplim = ifa->ifa_netmask->sa_len +
1408 (char *)ifa->ifa_netmask;
1409 while (cp3 < cplim)
1410 if ((*cp++ ^ *cp2++) & *cp3++)
1411 goto next; /* next address! */
1413 * If the netmask of what we just found
1414 * is more specific than what we had before
1415 * (if we had one) then remember the new one
1416 * before continuing to search for an even
1417 * better one. If the netmasks are equal,
1418 * we prefer the this ifa based on the result
1419 * of ifa_prefer().
1421 if (ifa_maybe == NULL ||
1422 rn_refines((char *)ifa->ifa_netmask,
1423 (char *)ifa_maybe->ifa_netmask) ||
1424 (sa_equal(ifa_maybe->ifa_netmask,
1425 ifa->ifa_netmask) &&
1426 ifa_prefer(ifa, ifa_maybe)))
1427 ifa_maybe = ifa;
1431 return (ifa_maybe);
1435 * Find an interface address specific to an interface best matching
1436 * a given address.
1438 struct ifaddr *
1439 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1441 struct ifaddr_container *ifac;
1442 char *cp, *cp2, *cp3;
1443 char *cplim;
1444 struct ifaddr *ifa_maybe = NULL;
1445 u_int af = addr->sa_family;
1447 if (af >= AF_MAX)
1448 return (0);
1449 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1450 struct ifaddr *ifa = ifac->ifa;
1452 if (ifa->ifa_addr->sa_family != af)
1453 continue;
1454 if (ifa_maybe == NULL)
1455 ifa_maybe = ifa;
1456 if (ifa->ifa_netmask == NULL) {
1457 if (sa_equal(addr, ifa->ifa_addr) ||
1458 (ifa->ifa_dstaddr != NULL &&
1459 sa_equal(addr, ifa->ifa_dstaddr)))
1460 return (ifa);
1461 continue;
1463 if (ifp->if_flags & IFF_POINTOPOINT) {
1464 if (sa_equal(addr, ifa->ifa_dstaddr))
1465 return (ifa);
1466 } else {
1467 cp = addr->sa_data;
1468 cp2 = ifa->ifa_addr->sa_data;
1469 cp3 = ifa->ifa_netmask->sa_data;
1470 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1471 for (; cp3 < cplim; cp3++)
1472 if ((*cp++ ^ *cp2++) & *cp3)
1473 break;
1474 if (cp3 == cplim)
1475 return (ifa);
1478 return (ifa_maybe);
1482 * Default action when installing a route with a Link Level gateway.
1483 * Lookup an appropriate real ifa to point to.
1484 * This should be moved to /sys/net/link.c eventually.
1486 static void
1487 link_rtrequest(int cmd, struct rtentry *rt)
1489 struct ifaddr *ifa;
1490 struct sockaddr *dst;
1491 struct ifnet *ifp;
1493 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1494 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1495 return;
1496 ifa = ifaof_ifpforaddr(dst, ifp);
1497 if (ifa != NULL) {
1498 IFAFREE(rt->rt_ifa);
1499 IFAREF(ifa);
1500 rt->rt_ifa = ifa;
1501 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1502 ifa->ifa_rtrequest(cmd, rt);
1506 struct netmsg_ifroute {
1507 struct netmsg_base base;
1508 struct ifnet *ifp;
1509 int flag;
1510 int fam;
1514 * Mark an interface down and notify protocols of the transition.
1516 static void
1517 if_unroute_dispatch(netmsg_t nmsg)
1519 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1520 struct ifnet *ifp = msg->ifp;
1521 int flag = msg->flag, fam = msg->fam;
1522 struct ifaddr_container *ifac;
1524 ifp->if_flags &= ~flag;
1525 getmicrotime(&ifp->if_lastchange);
1527 * The ifaddr processing in the following loop will block,
1528 * however, this function is called in netisr0, in which
1529 * ifaddr list changes happen, so we don't care about the
1530 * blockness of the ifaddr processing here.
1532 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1533 struct ifaddr *ifa = ifac->ifa;
1535 /* Ignore marker */
1536 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1537 continue;
1539 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1540 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1542 ifq_purge_all(&ifp->if_snd);
1543 rt_ifmsg(ifp);
1545 lwkt_replymsg(&nmsg->lmsg, 0);
1548 void
1549 if_unroute(struct ifnet *ifp, int flag, int fam)
1551 struct netmsg_ifroute msg;
1553 ASSERT_CANDOMSG_NETISR0(curthread);
1555 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1556 if_unroute_dispatch);
1557 msg.ifp = ifp;
1558 msg.flag = flag;
1559 msg.fam = fam;
1560 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1564 * Mark an interface up and notify protocols of the transition.
1566 static void
1567 if_route_dispatch(netmsg_t nmsg)
1569 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1570 struct ifnet *ifp = msg->ifp;
1571 int flag = msg->flag, fam = msg->fam;
1572 struct ifaddr_container *ifac;
1574 ifq_purge_all(&ifp->if_snd);
1575 ifp->if_flags |= flag;
1576 getmicrotime(&ifp->if_lastchange);
1578 * The ifaddr processing in the following loop will block,
1579 * however, this function is called in netisr0, in which
1580 * ifaddr list changes happen, so we don't care about the
1581 * blockness of the ifaddr processing here.
1583 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1584 struct ifaddr *ifa = ifac->ifa;
1586 /* Ignore marker */
1587 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1588 continue;
1590 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1591 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1593 rt_ifmsg(ifp);
1594 #ifdef INET6
1595 in6_if_up(ifp);
1596 #endif
1598 lwkt_replymsg(&nmsg->lmsg, 0);
1601 void
1602 if_route(struct ifnet *ifp, int flag, int fam)
1604 struct netmsg_ifroute msg;
1606 ASSERT_CANDOMSG_NETISR0(curthread);
1608 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1609 if_route_dispatch);
1610 msg.ifp = ifp;
1611 msg.flag = flag;
1612 msg.fam = fam;
1613 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1617 * Mark an interface down and notify protocols of the transition. An
1618 * interface going down is also considered to be a synchronizing event.
1619 * We must ensure that all packet processing related to the interface
1620 * has completed before we return so e.g. the caller can free the ifnet
1621 * structure that the mbufs may be referencing.
1623 * NOTE: must be called at splnet or eqivalent.
1625 void
1626 if_down(struct ifnet *ifp)
1628 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1629 netmsg_service_sync();
1633 * Mark an interface up and notify protocols of
1634 * the transition.
1635 * NOTE: must be called at splnet or eqivalent.
1637 void
1638 if_up(struct ifnet *ifp)
1640 if_route(ifp, IFF_UP, AF_UNSPEC);
1644 * Process a link state change.
1645 * NOTE: must be called at splsoftnet or equivalent.
1647 void
1648 if_link_state_change(struct ifnet *ifp)
1650 int link_state = ifp->if_link_state;
1652 rt_ifmsg(ifp);
1653 devctl_notify("IFNET", ifp->if_xname,
1654 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1658 * Handle interface watchdog timer routines. Called
1659 * from softclock, we decrement timers (if set) and
1660 * call the appropriate interface routine on expiration.
1662 static void
1663 if_slowtimo_dispatch(netmsg_t nmsg)
1665 struct globaldata *gd = mycpu;
1666 const struct ifnet_array *arr;
1667 int i;
1669 ASSERT_IN_NETISR(0);
1671 crit_enter_gd(gd);
1672 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1673 crit_exit_gd(gd);
1675 arr = ifnet_array_get();
1676 for (i = 0; i < arr->ifnet_count; ++i) {
1677 struct ifnet *ifp = arr->ifnet_arr[i];
1679 crit_enter_gd(gd);
1681 if (if_stats_compat) {
1682 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1683 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1684 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1685 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1686 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1687 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1688 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1689 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1690 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1691 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1692 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1693 IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1696 if (ifp->if_timer == 0 || --ifp->if_timer) {
1697 crit_exit_gd(gd);
1698 continue;
1700 if (ifp->if_watchdog) {
1701 if (ifnet_tryserialize_all(ifp)) {
1702 (*ifp->if_watchdog)(ifp);
1703 ifnet_deserialize_all(ifp);
1704 } else {
1705 /* try again next timeout */
1706 ++ifp->if_timer;
1710 crit_exit_gd(gd);
1713 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1716 static void
1717 if_slowtimo(void *arg __unused)
1719 struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1721 KASSERT(mycpuid == 0, ("not on cpu0"));
1722 crit_enter();
1723 if (lmsg->ms_flags & MSGF_DONE)
1724 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1725 crit_exit();
1729 * Map interface name to
1730 * interface structure pointer.
1732 struct ifnet *
1733 ifunit(const char *name)
1735 struct ifnet *ifp;
1738 * Search all the interfaces for this name/number
1740 KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked"));
1742 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1743 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1744 break;
1746 return (ifp);
1749 struct ifnet *
1750 ifunit_netisr(const char *name)
1752 const struct ifnet_array *arr;
1753 int i;
1756 * Search all the interfaces for this name/number
1759 arr = ifnet_array_get();
1760 for (i = 0; i < arr->ifnet_count; ++i) {
1761 struct ifnet *ifp = arr->ifnet_arr[i];
1763 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1764 return ifp;
1766 return NULL;
1770 * Interface ioctls.
1773 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1775 struct ifnet *ifp;
1776 struct ifreq *ifr;
1777 struct ifstat *ifs;
1778 int error, do_ifup = 0;
1779 short oif_flags;
1780 int new_flags;
1781 size_t namelen, onamelen;
1782 char new_name[IFNAMSIZ];
1783 struct ifaddr *ifa;
1784 struct sockaddr_dl *sdl;
1786 switch (cmd) {
1787 case SIOCGIFCONF:
1788 case OSIOCGIFCONF:
1789 return (ifconf(cmd, data, cred));
1790 default:
1791 break;
1794 ifr = (struct ifreq *)data;
1796 switch (cmd) {
1797 case SIOCIFCREATE:
1798 case SIOCIFCREATE2:
1799 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1800 return (error);
1801 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1802 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1803 case SIOCIFDESTROY:
1804 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1805 return (error);
1806 return (if_clone_destroy(ifr->ifr_name));
1807 case SIOCIFGCLONERS:
1808 return (if_clone_list((struct if_clonereq *)data));
1809 default:
1810 break;
1814 * Nominal ioctl through interface, lookup the ifp and obtain a
1815 * lock to serialize the ifconfig ioctl operation.
1817 ifnet_lock();
1819 ifp = ifunit(ifr->ifr_name);
1820 if (ifp == NULL) {
1821 ifnet_unlock();
1822 return (ENXIO);
1824 error = 0;
1826 switch (cmd) {
1827 case SIOCGIFINDEX:
1828 ifr->ifr_index = ifp->if_index;
1829 break;
1831 case SIOCGIFFLAGS:
1832 ifr->ifr_flags = ifp->if_flags;
1833 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1834 break;
1836 case SIOCGIFCAP:
1837 ifr->ifr_reqcap = ifp->if_capabilities;
1838 ifr->ifr_curcap = ifp->if_capenable;
1839 break;
1841 case SIOCGIFMETRIC:
1842 ifr->ifr_metric = ifp->if_metric;
1843 break;
1845 case SIOCGIFMTU:
1846 ifr->ifr_mtu = ifp->if_mtu;
1847 break;
1849 case SIOCGIFTSOLEN:
1850 ifr->ifr_tsolen = ifp->if_tsolen;
1851 break;
1853 case SIOCGIFDATA:
1854 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1855 sizeof(ifp->if_data));
1856 break;
1858 case SIOCGIFPHYS:
1859 ifr->ifr_phys = ifp->if_physical;
1860 break;
1862 case SIOCGIFPOLLCPU:
1863 ifr->ifr_pollcpu = -1;
1864 break;
1866 case SIOCSIFPOLLCPU:
1867 break;
1869 case SIOCSIFFLAGS:
1870 error = priv_check_cred(cred, PRIV_ROOT, 0);
1871 if (error)
1872 break;
1873 new_flags = (ifr->ifr_flags & 0xffff) |
1874 (ifr->ifr_flagshigh << 16);
1875 if (ifp->if_flags & IFF_SMART) {
1876 /* Smart drivers twiddle their own routes */
1877 } else if (ifp->if_flags & IFF_UP &&
1878 (new_flags & IFF_UP) == 0) {
1879 if_down(ifp);
1880 } else if (new_flags & IFF_UP &&
1881 (ifp->if_flags & IFF_UP) == 0) {
1882 do_ifup = 1;
1885 #ifdef IFPOLL_ENABLE
1886 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1887 if (new_flags & IFF_NPOLLING)
1888 ifpoll_register(ifp);
1889 else
1890 ifpoll_deregister(ifp);
1892 #endif
1894 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1895 (new_flags &~ IFF_CANTCHANGE);
1896 if (new_flags & IFF_PPROMISC) {
1897 /* Permanently promiscuous mode requested */
1898 ifp->if_flags |= IFF_PROMISC;
1899 } else if (ifp->if_pcount == 0) {
1900 ifp->if_flags &= ~IFF_PROMISC;
1902 if (ifp->if_ioctl) {
1903 ifnet_serialize_all(ifp);
1904 ifp->if_ioctl(ifp, cmd, data, cred);
1905 ifnet_deserialize_all(ifp);
1907 if (do_ifup)
1908 if_up(ifp);
1909 getmicrotime(&ifp->if_lastchange);
1910 break;
1912 case SIOCSIFCAP:
1913 error = priv_check_cred(cred, PRIV_ROOT, 0);
1914 if (error)
1915 break;
1916 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1917 error = EINVAL;
1918 break;
1920 ifnet_serialize_all(ifp);
1921 ifp->if_ioctl(ifp, cmd, data, cred);
1922 ifnet_deserialize_all(ifp);
1923 break;
1925 case SIOCSIFNAME:
1926 error = priv_check_cred(cred, PRIV_ROOT, 0);
1927 if (error)
1928 break;
1929 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1930 if (error)
1931 break;
1932 if (new_name[0] == '\0') {
1933 error = EINVAL;
1934 break;
1936 if (ifunit(new_name) != NULL) {
1937 error = EEXIST;
1938 break;
1941 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1943 /* Announce the departure of the interface. */
1944 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1946 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1947 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1948 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1949 namelen = strlen(new_name);
1950 onamelen = sdl->sdl_nlen;
1952 * Move the address if needed. This is safe because we
1953 * allocate space for a name of length IFNAMSIZ when we
1954 * create this in if_attach().
1956 if (namelen != onamelen) {
1957 bcopy(sdl->sdl_data + onamelen,
1958 sdl->sdl_data + namelen, sdl->sdl_alen);
1960 bcopy(new_name, sdl->sdl_data, namelen);
1961 sdl->sdl_nlen = namelen;
1962 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1963 bzero(sdl->sdl_data, onamelen);
1964 while (namelen != 0)
1965 sdl->sdl_data[--namelen] = 0xff;
1967 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1969 /* Announce the return of the interface. */
1970 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1971 break;
1973 case SIOCSIFMETRIC:
1974 error = priv_check_cred(cred, PRIV_ROOT, 0);
1975 if (error)
1976 break;
1977 ifp->if_metric = ifr->ifr_metric;
1978 getmicrotime(&ifp->if_lastchange);
1979 break;
1981 case SIOCSIFPHYS:
1982 error = priv_check_cred(cred, PRIV_ROOT, 0);
1983 if (error)
1984 break;
1985 if (ifp->if_ioctl == NULL) {
1986 error = EOPNOTSUPP;
1987 break;
1989 ifnet_serialize_all(ifp);
1990 error = ifp->if_ioctl(ifp, cmd, data, cred);
1991 ifnet_deserialize_all(ifp);
1992 if (error == 0)
1993 getmicrotime(&ifp->if_lastchange);
1994 break;
1996 case SIOCSIFMTU:
1998 u_long oldmtu = ifp->if_mtu;
2000 error = priv_check_cred(cred, PRIV_ROOT, 0);
2001 if (error)
2002 break;
2003 if (ifp->if_ioctl == NULL) {
2004 error = EOPNOTSUPP;
2005 break;
2007 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2008 error = EINVAL;
2009 break;
2011 ifnet_serialize_all(ifp);
2012 error = ifp->if_ioctl(ifp, cmd, data, cred);
2013 ifnet_deserialize_all(ifp);
2014 if (error == 0) {
2015 getmicrotime(&ifp->if_lastchange);
2016 rt_ifmsg(ifp);
2019 * If the link MTU changed, do network layer specific procedure.
2021 if (ifp->if_mtu != oldmtu) {
2022 #ifdef INET6
2023 nd6_setmtu(ifp);
2024 #endif
2026 break;
2029 case SIOCSIFTSOLEN:
2030 error = priv_check_cred(cred, PRIV_ROOT, 0);
2031 if (error)
2032 break;
2034 /* XXX need driver supplied upper limit */
2035 if (ifr->ifr_tsolen <= 0) {
2036 error = EINVAL;
2037 break;
2039 ifp->if_tsolen = ifr->ifr_tsolen;
2040 break;
2042 case SIOCADDMULTI:
2043 case SIOCDELMULTI:
2044 error = priv_check_cred(cred, PRIV_ROOT, 0);
2045 if (error)
2046 break;
2048 /* Don't allow group membership on non-multicast interfaces. */
2049 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2050 error = EOPNOTSUPP;
2051 break;
2054 /* Don't let users screw up protocols' entries. */
2055 if (ifr->ifr_addr.sa_family != AF_LINK) {
2056 error = EINVAL;
2057 break;
2060 if (cmd == SIOCADDMULTI) {
2061 struct ifmultiaddr *ifma;
2062 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2063 } else {
2064 error = if_delmulti(ifp, &ifr->ifr_addr);
2066 if (error == 0)
2067 getmicrotime(&ifp->if_lastchange);
2068 break;
2070 case SIOCSIFPHYADDR:
2071 case SIOCDIFPHYADDR:
2072 #ifdef INET6
2073 case SIOCSIFPHYADDR_IN6:
2074 #endif
2075 case SIOCSLIFPHYADDR:
2076 case SIOCSIFMEDIA:
2077 case SIOCSIFGENERIC:
2078 error = priv_check_cred(cred, PRIV_ROOT, 0);
2079 if (error)
2080 break;
2081 if (ifp->if_ioctl == 0) {
2082 error = EOPNOTSUPP;
2083 break;
2085 ifnet_serialize_all(ifp);
2086 error = ifp->if_ioctl(ifp, cmd, data, cred);
2087 ifnet_deserialize_all(ifp);
2088 if (error == 0)
2089 getmicrotime(&ifp->if_lastchange);
2090 break;
2092 case SIOCGIFSTATUS:
2093 ifs = (struct ifstat *)data;
2094 ifs->ascii[0] = '\0';
2095 /* fall through */
2096 case SIOCGIFPSRCADDR:
2097 case SIOCGIFPDSTADDR:
2098 case SIOCGLIFPHYADDR:
2099 case SIOCGIFMEDIA:
2100 case SIOCGIFGENERIC:
2101 if (ifp->if_ioctl == NULL) {
2102 error = EOPNOTSUPP;
2103 break;
2105 ifnet_serialize_all(ifp);
2106 error = ifp->if_ioctl(ifp, cmd, data, cred);
2107 ifnet_deserialize_all(ifp);
2108 break;
2110 case SIOCSIFLLADDR:
2111 error = priv_check_cred(cred, PRIV_ROOT, 0);
2112 if (error)
2113 break;
2114 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
2115 ifr->ifr_addr.sa_len);
2116 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2117 break;
2119 default:
2120 oif_flags = ifp->if_flags;
2121 if (so->so_proto == 0) {
2122 error = EOPNOTSUPP;
2123 break;
2125 error = so_pru_control_direct(so, cmd, data, ifp);
2127 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2128 #ifdef INET6
2129 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2130 if (ifp->if_flags & IFF_UP) {
2131 crit_enter();
2132 in6_if_up(ifp);
2133 crit_exit();
2135 #endif
2137 break;
2140 ifnet_unlock();
2141 return (error);
2145 * Set/clear promiscuous mode on interface ifp based on the truth value
2146 * of pswitch. The calls are reference counted so that only the first
2147 * "on" request actually has an effect, as does the final "off" request.
2148 * Results are undefined if the "off" and "on" requests are not matched.
2151 ifpromisc(struct ifnet *ifp, int pswitch)
2153 struct ifreq ifr;
2154 int error;
2155 int oldflags;
2157 oldflags = ifp->if_flags;
2158 if (ifp->if_flags & IFF_PPROMISC) {
2159 /* Do nothing if device is in permanently promiscuous mode */
2160 ifp->if_pcount += pswitch ? 1 : -1;
2161 return (0);
2163 if (pswitch) {
2165 * If the device is not configured up, we cannot put it in
2166 * promiscuous mode.
2168 if ((ifp->if_flags & IFF_UP) == 0)
2169 return (ENETDOWN);
2170 if (ifp->if_pcount++ != 0)
2171 return (0);
2172 ifp->if_flags |= IFF_PROMISC;
2173 log(LOG_INFO, "%s: promiscuous mode enabled\n",
2174 ifp->if_xname);
2175 } else {
2176 if (--ifp->if_pcount > 0)
2177 return (0);
2178 ifp->if_flags &= ~IFF_PROMISC;
2179 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2180 ifp->if_xname);
2182 ifr.ifr_flags = ifp->if_flags;
2183 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2184 ifnet_serialize_all(ifp);
2185 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2186 ifnet_deserialize_all(ifp);
2187 if (error == 0)
2188 rt_ifmsg(ifp);
2189 else
2190 ifp->if_flags = oldflags;
2191 return error;
2195 * Return interface configuration
2196 * of system. List may be used
2197 * in later ioctl's (above) to get
2198 * other information.
2200 static int
2201 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2203 struct ifconf *ifc = (struct ifconf *)data;
2204 struct ifnet *ifp;
2205 struct sockaddr *sa;
2206 struct ifreq ifr, *ifrp;
2207 int space = ifc->ifc_len, error = 0;
2209 ifrp = ifc->ifc_req;
2211 ifnet_lock();
2212 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2213 struct ifaddr_container *ifac, *ifac_mark;
2214 struct ifaddr_marker mark;
2215 struct ifaddrhead *head;
2216 int addrs;
2218 if (space <= sizeof ifr)
2219 break;
2222 * Zero the stack declared structure first to prevent
2223 * memory disclosure.
2225 bzero(&ifr, sizeof(ifr));
2226 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2227 >= sizeof(ifr.ifr_name)) {
2228 error = ENAMETOOLONG;
2229 break;
2233 * Add a marker, since copyout() could block and during that
2234 * period the list could be changed. Inserting the marker to
2235 * the header of the list will not cause trouble for the code
2236 * assuming that the first element of the list is AF_LINK; the
2237 * marker will be moved to the next position w/o blocking.
2239 ifa_marker_init(&mark, ifp);
2240 ifac_mark = &mark.ifac;
2241 head = &ifp->if_addrheads[mycpuid];
2243 addrs = 0;
2244 TAILQ_INSERT_HEAD(head, ifac_mark, ifa_link);
2245 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
2246 struct ifaddr *ifa = ifac->ifa;
2248 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2249 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
2251 /* Ignore marker */
2252 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
2253 continue;
2255 if (space <= sizeof ifr)
2256 break;
2257 sa = ifa->ifa_addr;
2258 if (cred->cr_prison &&
2259 prison_if(cred, sa))
2260 continue;
2261 addrs++;
2263 * Keep a reference on this ifaddr, so that it will
2264 * not be destroyed when its address is copied to
2265 * the userland, which could block.
2267 IFAREF(ifa);
2268 if (sa->sa_len <= sizeof(*sa)) {
2269 ifr.ifr_addr = *sa;
2270 error = copyout(&ifr, ifrp, sizeof ifr);
2271 ifrp++;
2272 } else {
2273 if (space < (sizeof ifr) + sa->sa_len -
2274 sizeof(*sa)) {
2275 IFAFREE(ifa);
2276 break;
2278 space -= sa->sa_len - sizeof(*sa);
2279 error = copyout(&ifr, ifrp,
2280 sizeof ifr.ifr_name);
2281 if (error == 0)
2282 error = copyout(sa, &ifrp->ifr_addr,
2283 sa->sa_len);
2284 ifrp = (struct ifreq *)
2285 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2287 IFAFREE(ifa);
2288 if (error)
2289 break;
2290 space -= sizeof ifr;
2292 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2293 if (error)
2294 break;
2295 if (!addrs) {
2296 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2297 error = copyout(&ifr, ifrp, sizeof ifr);
2298 if (error)
2299 break;
2300 space -= sizeof ifr;
2301 ifrp++;
2304 ifnet_unlock();
2306 ifc->ifc_len -= space;
2307 return (error);
2311 * Just like if_promisc(), but for all-multicast-reception mode.
2314 if_allmulti(struct ifnet *ifp, int onswitch)
2316 int error = 0;
2317 struct ifreq ifr;
2319 crit_enter();
2321 if (onswitch) {
2322 if (ifp->if_amcount++ == 0) {
2323 ifp->if_flags |= IFF_ALLMULTI;
2324 ifr.ifr_flags = ifp->if_flags;
2325 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2326 ifnet_serialize_all(ifp);
2327 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2328 NULL);
2329 ifnet_deserialize_all(ifp);
2331 } else {
2332 if (ifp->if_amcount > 1) {
2333 ifp->if_amcount--;
2334 } else {
2335 ifp->if_amcount = 0;
2336 ifp->if_flags &= ~IFF_ALLMULTI;
2337 ifr.ifr_flags = ifp->if_flags;
2338 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2339 ifnet_serialize_all(ifp);
2340 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2341 NULL);
2342 ifnet_deserialize_all(ifp);
2346 crit_exit();
2348 if (error == 0)
2349 rt_ifmsg(ifp);
2350 return error;
2354 * Add a multicast listenership to the interface in question.
2355 * The link layer provides a routine which converts
2358 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2359 struct ifmultiaddr **retifma)
2361 struct sockaddr *llsa, *dupsa;
2362 int error;
2363 struct ifmultiaddr *ifma;
2365 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2368 * If the matching multicast address already exists
2369 * then don't add a new one, just add a reference
2371 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2372 if (sa_equal(sa, ifma->ifma_addr)) {
2373 ifma->ifma_refcount++;
2374 if (retifma)
2375 *retifma = ifma;
2376 return 0;
2381 * Give the link layer a chance to accept/reject it, and also
2382 * find out which AF_LINK address this maps to, if it isn't one
2383 * already.
2385 if (ifp->if_resolvemulti) {
2386 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2387 if (error)
2388 return error;
2389 } else {
2390 llsa = NULL;
2393 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2394 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_INTWAIT);
2395 bcopy(sa, dupsa, sa->sa_len);
2397 ifma->ifma_addr = dupsa;
2398 ifma->ifma_lladdr = llsa;
2399 ifma->ifma_ifp = ifp;
2400 ifma->ifma_refcount = 1;
2401 ifma->ifma_protospec = NULL;
2402 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2404 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2405 if (retifma)
2406 *retifma = ifma;
2408 if (llsa != NULL) {
2409 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2410 if (sa_equal(ifma->ifma_addr, llsa))
2411 break;
2413 if (ifma) {
2414 ifma->ifma_refcount++;
2415 } else {
2416 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2417 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_INTWAIT);
2418 bcopy(llsa, dupsa, llsa->sa_len);
2419 ifma->ifma_addr = dupsa;
2420 ifma->ifma_ifp = ifp;
2421 ifma->ifma_refcount = 1;
2422 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2426 * We are certain we have added something, so call down to the
2427 * interface to let them know about it.
2429 if (ifp->if_ioctl)
2430 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2432 return 0;
2436 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2437 struct ifmultiaddr **retifma)
2439 int error;
2441 ifnet_serialize_all(ifp);
2442 error = if_addmulti_serialized(ifp, sa, retifma);
2443 ifnet_deserialize_all(ifp);
2445 return error;
2449 * Remove a reference to a multicast address on this interface. Yell
2450 * if the request does not match an existing membership.
2452 static int
2453 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2455 struct ifmultiaddr *ifma;
2457 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2459 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2460 if (sa_equal(sa, ifma->ifma_addr))
2461 break;
2462 if (ifma == NULL)
2463 return ENOENT;
2465 if (ifma->ifma_refcount > 1) {
2466 ifma->ifma_refcount--;
2467 return 0;
2470 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2471 sa = ifma->ifma_lladdr;
2472 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2474 * Make sure the interface driver is notified
2475 * in the case of a link layer mcast group being left.
2477 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2478 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2479 kfree(ifma->ifma_addr, M_IFMADDR);
2480 kfree(ifma, M_IFMADDR);
2481 if (sa == NULL)
2482 return 0;
2485 * Now look for the link-layer address which corresponds to
2486 * this network address. It had been squirreled away in
2487 * ifma->ifma_lladdr for this purpose (so we don't have
2488 * to call ifp->if_resolvemulti() again), and we saved that
2489 * value in sa above. If some nasty deleted the
2490 * link-layer address out from underneath us, we can deal because
2491 * the address we stored was is not the same as the one which was
2492 * in the record for the link-layer address. (So we don't complain
2493 * in that case.)
2495 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2496 if (sa_equal(sa, ifma->ifma_addr))
2497 break;
2498 if (ifma == NULL)
2499 return 0;
2501 if (ifma->ifma_refcount > 1) {
2502 ifma->ifma_refcount--;
2503 return 0;
2506 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2507 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2508 kfree(ifma->ifma_addr, M_IFMADDR);
2509 kfree(sa, M_IFMADDR);
2510 kfree(ifma, M_IFMADDR);
2512 return 0;
2516 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2518 int error;
2520 ifnet_serialize_all(ifp);
2521 error = if_delmulti_serialized(ifp, sa);
2522 ifnet_deserialize_all(ifp);
2524 return error;
2528 * Delete all multicast group membership for an interface.
2529 * Should be used to quickly flush all multicast filters.
2531 void
2532 if_delallmulti_serialized(struct ifnet *ifp)
2534 struct ifmultiaddr *ifma, mark;
2535 struct sockaddr sa;
2537 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2539 bzero(&sa, sizeof(sa));
2540 sa.sa_family = AF_UNSPEC;
2541 sa.sa_len = sizeof(sa);
2543 bzero(&mark, sizeof(mark));
2544 mark.ifma_addr = &sa;
2546 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2547 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2548 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2549 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2550 ifma_link);
2552 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2553 continue;
2555 if_delmulti_serialized(ifp, ifma->ifma_addr);
2557 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2562 * Set the link layer address on an interface.
2564 * At this time we only support certain types of interfaces,
2565 * and we don't allow the length of the address to change.
2568 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2570 struct sockaddr_dl *sdl;
2571 struct ifreq ifr;
2573 sdl = IF_LLSOCKADDR(ifp);
2574 if (sdl == NULL)
2575 return (EINVAL);
2576 if (len != sdl->sdl_alen) /* don't allow length to change */
2577 return (EINVAL);
2578 switch (ifp->if_type) {
2579 case IFT_ETHER: /* these types use struct arpcom */
2580 case IFT_XETHER:
2581 case IFT_L2VLAN:
2582 case IFT_IEEE8023ADLAG:
2583 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2584 bcopy(lladdr, LLADDR(sdl), len);
2585 break;
2586 default:
2587 return (ENODEV);
2590 * If the interface is already up, we need
2591 * to re-init it in order to reprogram its
2592 * address filter.
2594 ifnet_serialize_all(ifp);
2595 if ((ifp->if_flags & IFF_UP) != 0) {
2596 #ifdef INET
2597 struct ifaddr_container *ifac;
2598 #endif
2600 ifp->if_flags &= ~IFF_UP;
2601 ifr.ifr_flags = ifp->if_flags;
2602 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2603 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2604 NULL);
2605 ifp->if_flags |= IFF_UP;
2606 ifr.ifr_flags = ifp->if_flags;
2607 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2608 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2609 NULL);
2610 #ifdef INET
2612 * Also send gratuitous ARPs to notify other nodes about
2613 * the address change.
2615 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2616 struct ifaddr *ifa = ifac->ifa;
2618 if (ifa->ifa_addr != NULL &&
2619 ifa->ifa_addr->sa_family == AF_INET)
2620 arp_gratuitous(ifp, ifa);
2622 #endif
2624 ifnet_deserialize_all(ifp);
2625 return (0);
2628 struct ifmultiaddr *
2629 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2631 struct ifmultiaddr *ifma;
2633 /* TODO: need ifnet_serialize_main */
2634 ifnet_serialize_all(ifp);
2635 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2636 if (sa_equal(ifma->ifma_addr, sa))
2637 break;
2638 ifnet_deserialize_all(ifp);
2640 return ifma;
2644 * This function locates the first real ethernet MAC from a network
2645 * card and loads it into node, returning 0 on success or ENOENT if
2646 * no suitable interfaces were found. It is used by the uuid code to
2647 * generate a unique 6-byte number.
2650 if_getanyethermac(uint16_t *node, int minlen)
2652 struct ifnet *ifp;
2653 struct sockaddr_dl *sdl;
2655 ifnet_lock();
2656 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2657 if (ifp->if_type != IFT_ETHER)
2658 continue;
2659 sdl = IF_LLSOCKADDR(ifp);
2660 if (sdl->sdl_alen < minlen)
2661 continue;
2662 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2663 minlen);
2664 ifnet_unlock();
2665 return(0);
2667 ifnet_unlock();
2668 return (ENOENT);
2672 * The name argument must be a pointer to storage which will last as
2673 * long as the interface does. For physical devices, the result of
2674 * device_get_name(dev) is a good choice and for pseudo-devices a
2675 * static string works well.
2677 void
2678 if_initname(struct ifnet *ifp, const char *name, int unit)
2680 ifp->if_dname = name;
2681 ifp->if_dunit = unit;
2682 if (unit != IF_DUNIT_NONE)
2683 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2684 else
2685 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2689 if_printf(struct ifnet *ifp, const char *fmt, ...)
2691 __va_list ap;
2692 int retval;
2694 retval = kprintf("%s: ", ifp->if_xname);
2695 __va_start(ap, fmt);
2696 retval += kvprintf(fmt, ap);
2697 __va_end(ap);
2698 return (retval);
2701 struct ifnet *
2702 if_alloc(uint8_t type)
2704 struct ifnet *ifp;
2705 size_t size;
2708 * XXX temporary hack until arpcom is setup in if_l2com
2710 if (type == IFT_ETHER)
2711 size = sizeof(struct arpcom);
2712 else
2713 size = sizeof(struct ifnet);
2715 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2717 ifp->if_type = type;
2719 if (if_com_alloc[type] != NULL) {
2720 ifp->if_l2com = if_com_alloc[type](type, ifp);
2721 if (ifp->if_l2com == NULL) {
2722 kfree(ifp, M_IFNET);
2723 return (NULL);
2726 return (ifp);
2729 void
2730 if_free(struct ifnet *ifp)
2732 kfree(ifp, M_IFNET);
2735 void
2736 ifq_set_classic(struct ifaltq *ifq)
2738 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2739 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2742 void
2743 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2744 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2746 int q;
2748 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2749 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2750 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2751 KASSERT(request != NULL, ("request is not specified"));
2753 ifq->altq_mapsubq = mapsubq;
2754 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2755 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2757 ifsq->ifsq_enqueue = enqueue;
2758 ifsq->ifsq_dequeue = dequeue;
2759 ifsq->ifsq_request = request;
2763 static void
2764 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2767 classq_add(&ifsq->ifsq_norm, m);
2768 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2771 static void
2772 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2775 classq_add(&ifsq->ifsq_prio, m);
2776 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2777 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2780 static struct mbuf *
2781 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2783 struct mbuf *m;
2785 m = classq_get(&ifsq->ifsq_norm);
2786 if (m != NULL)
2787 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2788 return (m);
2791 static struct mbuf *
2792 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2794 struct mbuf *m;
2796 m = classq_get(&ifsq->ifsq_prio);
2797 if (m != NULL) {
2798 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2799 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2801 return (m);
2805 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2806 struct altq_pktattr *pa __unused)
2809 M_ASSERTPKTHDR(m);
2810 again:
2811 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2812 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
2813 struct mbuf *m_drop;
2815 if (m->m_flags & M_PRIO) {
2816 m_drop = NULL;
2817 if (ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen >> 1) &&
2818 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt >> 1)) {
2819 /* Try dropping some from normal queue. */
2820 m_drop = ifsq_norm_dequeue(ifsq);
2822 if (m_drop == NULL)
2823 m_drop = ifsq_prio_dequeue(ifsq);
2824 } else {
2825 m_drop = ifsq_norm_dequeue(ifsq);
2827 if (m_drop != NULL) {
2828 IFNET_STAT_INC(ifsq->ifsq_ifp, oqdrops, 1);
2829 m_freem(m_drop);
2830 goto again;
2833 * No old packets could be dropped!
2834 * NOTE: Caller increases oqdrops.
2836 m_freem(m);
2837 return (ENOBUFS);
2838 } else {
2839 if (m->m_flags & M_PRIO)
2840 ifsq_prio_enqueue(ifsq, m);
2841 else
2842 ifsq_norm_enqueue(ifsq, m);
2843 return (0);
2847 struct mbuf *
2848 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
2850 struct mbuf *m;
2852 switch (op) {
2853 case ALTDQ_POLL:
2854 m = classq_head(&ifsq->ifsq_prio);
2855 if (m == NULL)
2856 m = classq_head(&ifsq->ifsq_norm);
2857 break;
2859 case ALTDQ_REMOVE:
2860 m = ifsq_prio_dequeue(ifsq);
2861 if (m == NULL)
2862 m = ifsq_norm_dequeue(ifsq);
2863 break;
2865 default:
2866 panic("unsupported ALTQ dequeue op: %d", op);
2868 return m;
2872 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2874 switch (req) {
2875 case ALTRQ_PURGE:
2876 for (;;) {
2877 struct mbuf *m;
2879 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
2880 if (m == NULL)
2881 break;
2882 m_freem(m);
2884 break;
2886 default:
2887 panic("unsupported ALTQ request: %d", req);
2889 return 0;
2892 static void
2893 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2895 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2896 int running = 0, need_sched;
2899 * Try to do direct ifnet.if_start on the subqueue first, if there is
2900 * contention on the subqueue hardware serializer, ifnet.if_start on
2901 * the subqueue will be scheduled on the subqueue owner CPU.
2903 if (!ifsq_tryserialize_hw(ifsq)) {
2905 * Subqueue hardware serializer contention happened,
2906 * ifnet.if_start on the subqueue is scheduled on
2907 * the subqueue owner CPU, and we keep going.
2909 ifsq_ifstart_schedule(ifsq, 1);
2910 return;
2913 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2914 ifp->if_start(ifp, ifsq);
2915 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2916 running = 1;
2918 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2920 ifsq_deserialize_hw(ifsq);
2922 if (need_sched) {
2924 * More data need to be transmitted, ifnet.if_start on the
2925 * subqueue is scheduled on the subqueue owner CPU, and we
2926 * keep going.
2927 * NOTE: ifnet.if_start subqueue interlock is not released.
2929 ifsq_ifstart_schedule(ifsq, force_sched);
2934 * Subqeue packets staging mechanism:
2936 * The packets enqueued into the subqueue are staged to a certain amount
2937 * before the ifnet.if_start on the subqueue is called. In this way, the
2938 * driver could avoid writing to hardware registers upon every packet,
2939 * instead, hardware registers could be written when certain amount of
2940 * packets are put onto hardware TX ring. The measurement on several modern
2941 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2942 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2943 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2944 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2945 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2947 * Subqueue packets staging is performed for two entry points into drivers'
2948 * transmission function:
2949 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2950 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2952 * Subqueue packets staging will be stopped upon any of the following
2953 * conditions:
2954 * - If the count of packets enqueued on the current CPU is great than or
2955 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2956 * - If the total length of packets enqueued on the current CPU is great
2957 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2958 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2959 * is usually less than hardware's MTU.
2960 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2961 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2962 * released.
2963 * - The if_start_rollup(), which is registered as low priority netisr
2964 * rollup function, is called; probably because no more work is pending
2965 * for netisr.
2967 * NOTE:
2968 * Currently subqueue packet staging is only performed in netisr threads.
2971 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2973 struct ifaltq *ifq = &ifp->if_snd;
2974 struct ifaltq_subque *ifsq;
2975 int error, start = 0, len, mcast = 0, avoid_start = 0;
2976 struct ifsubq_stage_head *head = NULL;
2977 struct ifsubq_stage *stage = NULL;
2978 struct globaldata *gd = mycpu;
2979 struct thread *td = gd->gd_curthread;
2981 crit_enter_quick(td);
2983 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
2984 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
2986 len = m->m_pkthdr.len;
2987 if (m->m_flags & M_MCAST)
2988 mcast = 1;
2990 if (td->td_type == TD_TYPE_NETISR) {
2991 head = &ifsubq_stage_heads[mycpuid];
2992 stage = ifsq_get_stage(ifsq, mycpuid);
2994 stage->stg_cnt++;
2995 stage->stg_len += len;
2996 if (stage->stg_cnt < ifsq_stage_cntmax &&
2997 stage->stg_len < (ifp->if_mtu - max_protohdr))
2998 avoid_start = 1;
3001 ALTQ_SQ_LOCK(ifsq);
3002 error = ifsq_enqueue_locked(ifsq, m, pa);
3003 if (error) {
3004 IFNET_STAT_INC(ifp, oqdrops, 1);
3005 if (!ifsq_data_ready(ifsq)) {
3006 ALTQ_SQ_UNLOCK(ifsq);
3007 crit_exit_quick(td);
3008 return error;
3010 avoid_start = 0;
3012 if (!ifsq_is_started(ifsq)) {
3013 if (avoid_start) {
3014 ALTQ_SQ_UNLOCK(ifsq);
3016 KKASSERT(!error);
3017 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
3018 ifsq_stage_insert(head, stage);
3020 IFNET_STAT_INC(ifp, obytes, len);
3021 if (mcast)
3022 IFNET_STAT_INC(ifp, omcasts, 1);
3023 crit_exit_quick(td);
3024 return error;
3028 * Hold the subqueue interlock of ifnet.if_start
3030 ifsq_set_started(ifsq);
3031 start = 1;
3033 ALTQ_SQ_UNLOCK(ifsq);
3035 if (!error) {
3036 IFNET_STAT_INC(ifp, obytes, len);
3037 if (mcast)
3038 IFNET_STAT_INC(ifp, omcasts, 1);
3041 if (stage != NULL) {
3042 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
3043 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3044 if (!avoid_start) {
3045 ifsq_stage_remove(head, stage);
3046 ifsq_ifstart_schedule(ifsq, 1);
3048 crit_exit_quick(td);
3049 return error;
3052 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
3053 ifsq_stage_remove(head, stage);
3054 } else {
3055 stage->stg_cnt = 0;
3056 stage->stg_len = 0;
3060 if (!start) {
3061 crit_exit_quick(td);
3062 return error;
3065 ifsq_ifstart_try(ifsq, 0);
3067 crit_exit_quick(td);
3068 return error;
3071 void *
3072 ifa_create(int size)
3074 struct ifaddr *ifa;
3075 int i;
3077 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
3079 ifa = kmalloc(size, M_IFADDR, M_INTWAIT | M_ZERO);
3080 ifa->ifa_containers =
3081 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
3082 M_IFADDR, M_INTWAIT | M_ZERO);
3084 ifa->ifa_ncnt = ncpus;
3085 for (i = 0; i < ncpus; ++i) {
3086 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
3088 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
3089 ifac->ifa = ifa;
3090 ifac->ifa_refcnt = 1;
3092 #ifdef IFADDR_DEBUG
3093 kprintf("alloc ifa %p %d\n", ifa, size);
3094 #endif
3095 return ifa;
3098 void
3099 ifac_free(struct ifaddr_container *ifac, int cpu_id)
3101 struct ifaddr *ifa = ifac->ifa;
3103 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
3104 KKASSERT(ifac->ifa_refcnt == 0);
3105 KASSERT(ifac->ifa_listmask == 0,
3106 ("ifa is still on %#x lists", ifac->ifa_listmask));
3108 ifac->ifa_magic = IFA_CONTAINER_DEAD;
3110 #ifdef IFADDR_DEBUG_VERBOSE
3111 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
3112 #endif
3114 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
3115 ("invalid # of ifac, %d", ifa->ifa_ncnt));
3116 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
3117 #ifdef IFADDR_DEBUG
3118 kprintf("free ifa %p\n", ifa);
3119 #endif
3120 kfree(ifa->ifa_containers, M_IFADDR);
3121 kfree(ifa, M_IFADDR);
3125 static void
3126 ifa_iflink_dispatch(netmsg_t nmsg)
3128 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3129 struct ifaddr *ifa = msg->ifa;
3130 struct ifnet *ifp = msg->ifp;
3131 int cpu = mycpuid;
3132 struct ifaddr_container *ifac;
3134 crit_enter();
3136 ifac = &ifa->ifa_containers[cpu];
3137 ASSERT_IFAC_VALID(ifac);
3138 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
3139 ("ifaddr is on if_addrheads"));
3141 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
3142 if (msg->tail)
3143 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
3144 else
3145 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
3147 crit_exit();
3149 netisr_forwardmsg(&nmsg->base, cpu + 1);
3152 void
3153 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
3155 struct netmsg_ifaddr msg;
3157 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3158 0, ifa_iflink_dispatch);
3159 msg.ifa = ifa;
3160 msg.ifp = ifp;
3161 msg.tail = tail;
3163 netisr_domsg(&msg.base, 0);
3166 static void
3167 ifa_ifunlink_dispatch(netmsg_t nmsg)
3169 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3170 struct ifaddr *ifa = msg->ifa;
3171 struct ifnet *ifp = msg->ifp;
3172 int cpu = mycpuid;
3173 struct ifaddr_container *ifac;
3175 crit_enter();
3177 ifac = &ifa->ifa_containers[cpu];
3178 ASSERT_IFAC_VALID(ifac);
3179 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3180 ("ifaddr is not on if_addrhead"));
3182 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3183 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3185 crit_exit();
3187 netisr_forwardmsg(&nmsg->base, cpu + 1);
3190 void
3191 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3193 struct netmsg_ifaddr msg;
3195 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3196 0, ifa_ifunlink_dispatch);
3197 msg.ifa = ifa;
3198 msg.ifp = ifp;
3200 netisr_domsg(&msg.base, 0);
3203 static void
3204 ifa_destroy_dispatch(netmsg_t nmsg)
3206 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3208 IFAFREE(msg->ifa);
3209 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3212 void
3213 ifa_destroy(struct ifaddr *ifa)
3215 struct netmsg_ifaddr msg;
3217 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3218 0, ifa_destroy_dispatch);
3219 msg.ifa = ifa;
3221 netisr_domsg(&msg.base, 0);
3224 static void
3225 if_start_rollup(void)
3227 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3228 struct ifsubq_stage *stage;
3230 crit_enter();
3232 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3233 struct ifaltq_subque *ifsq = stage->stg_subq;
3234 int is_sched = 0;
3236 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3237 is_sched = 1;
3238 ifsq_stage_remove(head, stage);
3240 if (is_sched) {
3241 ifsq_ifstart_schedule(ifsq, 1);
3242 } else {
3243 int start = 0;
3245 ALTQ_SQ_LOCK(ifsq);
3246 if (!ifsq_is_started(ifsq)) {
3248 * Hold the subqueue interlock of
3249 * ifnet.if_start
3251 ifsq_set_started(ifsq);
3252 start = 1;
3254 ALTQ_SQ_UNLOCK(ifsq);
3256 if (start)
3257 ifsq_ifstart_try(ifsq, 1);
3259 KKASSERT((stage->stg_flags &
3260 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3263 crit_exit();
3266 static void
3267 ifnetinit(void *dummy __unused)
3269 int i;
3271 for (i = 0; i < ncpus; ++i)
3272 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3273 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3276 void
3277 if_register_com_alloc(u_char type,
3278 if_com_alloc_t *a, if_com_free_t *f)
3281 KASSERT(if_com_alloc[type] == NULL,
3282 ("if_register_com_alloc: %d already registered", type));
3283 KASSERT(if_com_free[type] == NULL,
3284 ("if_register_com_alloc: %d free already registered", type));
3286 if_com_alloc[type] = a;
3287 if_com_free[type] = f;
3290 void
3291 if_deregister_com_alloc(u_char type)
3294 KASSERT(if_com_alloc[type] != NULL,
3295 ("if_deregister_com_alloc: %d not registered", type));
3296 KASSERT(if_com_free[type] != NULL,
3297 ("if_deregister_com_alloc: %d free not registered", type));
3298 if_com_alloc[type] = NULL;
3299 if_com_free[type] = NULL;
3303 if_ring_count2(int cnt, int cnt_max)
3305 int shift = 0;
3307 KASSERT(cnt_max >= 1 && powerof2(cnt_max),
3308 ("invalid ring count max %d", cnt_max));
3310 if (cnt <= 0)
3311 cnt = cnt_max;
3312 if (cnt > ncpus2)
3313 cnt = ncpus2;
3314 if (cnt > cnt_max)
3315 cnt = cnt_max;
3317 while ((1 << (shift + 1)) <= cnt)
3318 ++shift;
3319 cnt = 1 << shift;
3321 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
3322 ("calculate cnt %d, ncpus2 %d, cnt max %d",
3323 cnt, ncpus2, cnt_max));
3324 return cnt;
3327 void
3328 ifq_set_maxlen(struct ifaltq *ifq, int len)
3330 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3334 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3336 return ALTQ_SUBQ_INDEX_DEFAULT;
3340 ifq_mapsubq_mask(struct ifaltq *ifq, int cpuid)
3342 return (cpuid & ifq->altq_subq_mask);
3345 static void
3346 ifsq_watchdog(void *arg)
3348 struct ifsubq_watchdog *wd = arg;
3349 struct ifnet *ifp;
3351 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3352 goto done;
3354 ifp = ifsq_get_ifp(wd->wd_subq);
3355 if (ifnet_tryserialize_all(ifp)) {
3356 wd->wd_watchdog(wd->wd_subq);
3357 ifnet_deserialize_all(ifp);
3358 } else {
3359 /* try again next timeout */
3360 wd->wd_timer = 1;
3362 done:
3363 ifsq_watchdog_reset(wd);
3366 static void
3367 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3369 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3370 ifsq_get_cpuid(wd->wd_subq));
3373 void
3374 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3375 ifsq_watchdog_t watchdog)
3377 callout_init_mp(&wd->wd_callout);
3378 wd->wd_timer = 0;
3379 wd->wd_subq = ifsq;
3380 wd->wd_watchdog = watchdog;
3383 void
3384 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3386 wd->wd_timer = 0;
3387 ifsq_watchdog_reset(wd);
3390 void
3391 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3393 wd->wd_timer = 0;
3394 callout_stop(&wd->wd_callout);
3397 void
3398 ifnet_lock(void)
3400 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3401 ("try holding ifnet lock in netisr"));
3402 mtx_lock(&ifnet_mtx);
3405 void
3406 ifnet_unlock(void)
3408 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3409 ("try holding ifnet lock in netisr"));
3410 mtx_unlock(&ifnet_mtx);
3413 static struct ifnet_array *
3414 ifnet_array_alloc(int count)
3416 struct ifnet_array *arr;
3418 arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]),
3419 M_IFNET, M_WAITOK);
3420 arr->ifnet_count = count;
3422 return arr;
3425 static void
3426 ifnet_array_free(struct ifnet_array *arr)
3428 if (arr == &ifnet_array0)
3429 return;
3430 kfree(arr, M_IFNET);
3433 static struct ifnet_array *
3434 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr)
3436 struct ifnet_array *arr;
3437 int count, i;
3439 KASSERT(old_arr->ifnet_count >= 0,
3440 ("invalid ifnet array count %d", old_arr->ifnet_count));
3441 count = old_arr->ifnet_count + 1;
3442 arr = ifnet_array_alloc(count);
3445 * Save the old ifnet array and append this ifp to the end of
3446 * the new ifnet array.
3448 for (i = 0; i < old_arr->ifnet_count; ++i) {
3449 KASSERT(old_arr->ifnet_arr[i] != ifp,
3450 ("%s is already in ifnet array", ifp->if_xname));
3451 arr->ifnet_arr[i] = old_arr->ifnet_arr[i];
3453 KASSERT(i == count - 1,
3454 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3455 ifp->if_xname, count - 1, i));
3456 arr->ifnet_arr[i] = ifp;
3458 return arr;
3461 static struct ifnet_array *
3462 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr)
3464 struct ifnet_array *arr;
3465 int count, i, idx, found = 0;
3467 KASSERT(old_arr->ifnet_count > 0,
3468 ("invalid ifnet array count %d", old_arr->ifnet_count));
3469 count = old_arr->ifnet_count - 1;
3470 arr = ifnet_array_alloc(count);
3473 * Save the old ifnet array, but skip this ifp.
3475 idx = 0;
3476 for (i = 0; i < old_arr->ifnet_count; ++i) {
3477 if (old_arr->ifnet_arr[i] == ifp) {
3478 KASSERT(!found,
3479 ("dup %s is in ifnet array", ifp->if_xname));
3480 found = 1;
3481 continue;
3483 KASSERT(idx < count,
3484 ("invalid ifnet array index %d, count %d", idx, count));
3485 arr->ifnet_arr[idx] = old_arr->ifnet_arr[i];
3486 ++idx;
3488 KASSERT(found, ("%s is not in ifnet array", ifp->if_xname));
3489 KASSERT(idx == count,
3490 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3491 ifp->if_xname, count, idx));
3493 return arr;
3496 const struct ifnet_array *
3497 ifnet_array_get(void)
3499 const struct ifnet_array *ret;
3501 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3502 ret = ifnet_array;
3503 /* Make sure 'ret' is really used. */
3504 cpu_ccfence();
3505 return (ret);
3509 ifnet_array_isempty(void)
3511 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3512 if (ifnet_array->ifnet_count == 0)
3513 return 1;
3514 else
3515 return 0;
3518 void
3519 ifa_marker_init(struct ifaddr_marker *mark, struct ifnet *ifp)
3521 struct ifaddr *ifa;
3523 memset(mark, 0, sizeof(*mark));
3524 ifa = &mark->ifa;
3526 mark->ifac.ifa = ifa;
3528 ifa->ifa_addr = &mark->addr;
3529 ifa->ifa_dstaddr = &mark->dstaddr;
3530 ifa->ifa_netmask = &mark->netmask;
3531 ifa->ifa_ifp = ifp;