2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if.c 8.3 (Berkeley) 1/4/94
34 * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
35 * $DragonFly: src/sys/net/if.c,v 1.84 2008/11/15 11:58:16 sephe Exp $
38 #include "opt_compat.h"
39 #include "opt_inet6.h"
41 #include "opt_polling.h"
42 #include "opt_ifpoll.h"
44 #include <sys/param.h>
45 #include <sys/malloc.h>
47 #include <sys/systm.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/socketops.h>
54 #include <sys/protosw.h>
55 #include <sys/kernel.h>
57 #include <sys/sockio.h>
58 #include <sys/syslog.h>
59 #include <sys/sysctl.h>
60 #include <sys/domain.h>
61 #include <sys/thread.h>
62 #include <sys/thread2.h>
63 #include <sys/serialize.h>
64 #include <sys/msgport2.h>
68 #include <net/if_arp.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/if_var.h>
72 #include <net/ifq_var.h>
73 #include <net/radix.h>
74 #include <net/route.h>
75 #include <net/if_clone.h>
76 #include <net/netisr.h>
77 #include <net/netmsg2.h>
79 #include <machine/atomic.h>
80 #include <machine/stdarg.h>
81 #include <machine/smp.h>
83 #if defined(INET) || defined(INET6)
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/if_ether.h>
89 #include <netinet6/in6_var.h>
90 #include <netinet6/in6_ifattach.h>
94 #if defined(COMPAT_43)
95 #include <emulation/43bsd/43bsd_socket.h>
96 #endif /* COMPAT_43 */
98 struct netmsg_ifaddr
{
106 * System initialization
108 static void if_attachdomain(void *);
109 static void if_attachdomain1(struct ifnet
*);
110 static int ifconf(u_long
, caddr_t
, struct ucred
*);
111 static void ifinit(void *);
112 static void ifnetinit(void *);
113 static void if_slowtimo(void *);
114 static void link_rtrequest(int, struct rtentry
*, struct rt_addrinfo
*);
115 static int if_rtdel(struct radix_node
*, void *);
119 * XXX: declare here to avoid to include many inet6 related files..
120 * should be more generalized?
122 extern void nd6_setmtu(struct ifnet
*);
125 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
, 0, "Link layers");
126 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
, 0, "Generic link-management");
128 SYSINIT(interfaces
, SI_SUB_PROTO_IF
, SI_ORDER_FIRST
, ifinit
, NULL
)
129 /* Must be after netisr_init */
130 SYSINIT(ifnet
, SI_SUB_PRE_DRIVERS
, SI_ORDER_SECOND
, ifnetinit
, NULL
)
132 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
133 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
135 int ifqmaxlen
= IFQ_MAXLEN
;
136 struct ifnethead ifnet
= TAILQ_HEAD_INITIALIZER(ifnet
);
138 /* In ifq_dispatch(), try to do direct ifnet.if_start first */
139 static int ifq_dispatch_schedonly
= 0;
140 SYSCTL_INT(_net_link_generic
, OID_AUTO
, ifq_dispatch_schedonly
, CTLFLAG_RW
,
141 &ifq_dispatch_schedonly
, 0, "");
143 /* In ifq_dispatch(), schedule ifnet.if_start without checking ifnet.if_snd */
144 static int ifq_dispatch_schednochk
= 0;
145 SYSCTL_INT(_net_link_generic
, OID_AUTO
, ifq_dispatch_schednochk
, CTLFLAG_RW
,
146 &ifq_dispatch_schednochk
, 0, "");
148 /* In if_devstart(), try to do direct ifnet.if_start first */
149 static int if_devstart_schedonly
= 0;
150 SYSCTL_INT(_net_link_generic
, OID_AUTO
, if_devstart_schedonly
, CTLFLAG_RW
,
151 &if_devstart_schedonly
, 0, "");
153 /* In if_devstart(), schedule ifnet.if_start without checking ifnet.if_snd */
154 static int if_devstart_schednochk
= 0;
155 SYSCTL_INT(_net_link_generic
, OID_AUTO
, if_devstart_schednochk
, CTLFLAG_RW
,
156 &if_devstart_schednochk
, 0, "");
159 /* Schedule ifnet.if_start on the current CPU */
160 static int if_start_oncpu_sched
= 0;
161 SYSCTL_INT(_net_link_generic
, OID_AUTO
, if_start_oncpu_sched
, CTLFLAG_RW
,
162 &if_start_oncpu_sched
, 0, "");
165 struct callout if_slowtimo_timer
;
168 struct ifnet
**ifindex2ifnet
= NULL
;
169 static struct thread ifnet_threads
[MAXCPU
];
170 static int ifnet_mpsafe_thread
= NETMSG_SERVICE_MPSAFE
;
172 #define IFQ_KTR_STRING "ifq=%p"
173 #define IFQ_KTR_ARG_SIZE (sizeof(void *))
175 #define KTR_IFQ KTR_ALL
177 KTR_INFO_MASTER(ifq
);
178 KTR_INFO(KTR_IFQ
, ifq
, enqueue
, 0, IFQ_KTR_STRING
, IFQ_KTR_ARG_SIZE
);
179 KTR_INFO(KTR_IFQ
, ifq
, dequeue
, 1, IFQ_KTR_STRING
, IFQ_KTR_ARG_SIZE
);
180 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
182 #define IF_START_KTR_STRING "ifp=%p"
183 #define IF_START_KTR_ARG_SIZE (sizeof(void *))
185 #define KTR_IF_START KTR_ALL
187 KTR_INFO_MASTER(if_start
);
188 KTR_INFO(KTR_IF_START
, if_start
, run
, 0,
189 IF_START_KTR_STRING
, IF_START_KTR_ARG_SIZE
);
190 KTR_INFO(KTR_IF_START
, if_start
, sched
, 1,
191 IF_START_KTR_STRING
, IF_START_KTR_ARG_SIZE
);
192 KTR_INFO(KTR_IF_START
, if_start
, avoid
, 2,
193 IF_START_KTR_STRING
, IF_START_KTR_ARG_SIZE
);
194 KTR_INFO(KTR_IF_START
, if_start
, contend_sched
, 3,
195 IF_START_KTR_STRING
, IF_START_KTR_ARG_SIZE
);
197 KTR_INFO(KTR_IF_START
, if_start
, chase_sched
, 4,
198 IF_START_KTR_STRING
, IF_START_KTR_ARG_SIZE
);
200 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
203 * Network interface utility routines.
205 * Routines with ifa_ifwith* names take sockaddr *'s as
214 callout_init(&if_slowtimo_timer
);
217 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
218 if (ifp
->if_snd
.ifq_maxlen
== 0) {
219 if_printf(ifp
, "XXX: driver didn't set ifq_maxlen\n");
220 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
229 if_start_cpuid(struct ifnet
*ifp
)
231 return ifp
->if_cpuid
;
234 #ifdef DEVICE_POLLING
236 if_start_cpuid_poll(struct ifnet
*ifp
)
238 int poll_cpuid
= ifp
->if_poll_cpuid
;
243 return ifp
->if_cpuid
;
248 if_start_ipifunc(void *arg
)
250 struct ifnet
*ifp
= arg
;
251 struct lwkt_msg
*lmsg
= &ifp
->if_start_nmsg
[mycpuid
].nm_lmsg
;
254 if (lmsg
->ms_flags
& MSGF_DONE
)
255 lwkt_sendmsg(ifnet_portfn(mycpuid
), lmsg
);
260 * Schedule ifnet.if_start on ifnet's CPU
263 if_start_schedule(struct ifnet
*ifp
)
268 if (if_start_oncpu_sched
)
271 cpu
= ifp
->if_start_cpuid(ifp
);
274 lwkt_send_ipiq(globaldata_find(cpu
), if_start_ipifunc
, ifp
);
277 if_start_ipifunc(ifp
);
282 * This function will release ifnet.if_start interlock,
283 * if ifnet.if_start does not need to be scheduled
286 if_start_need_schedule(struct ifaltq
*ifq
, int running
)
288 if (!running
|| ifq_is_empty(ifq
)
290 || ifq
->altq_tbr
!= NULL
295 * ifnet.if_start interlock is released, if:
296 * 1) Hardware can not take any packets, due to
297 * o interface is marked down
298 * o hardware queue is full (IFF_OACTIVE)
299 * Under the second situation, hardware interrupt
300 * or polling(4) will call/schedule ifnet.if_start
301 * when hardware queue is ready
302 * 2) There is not packet in the ifnet.if_snd.
303 * Further ifq_dispatch or ifq_handoff will call/
304 * schedule ifnet.if_start
305 * 3) TBR is used and it does not allow further
307 * TBR callout will call ifnet.if_start
309 if (!running
|| !ifq_data_ready(ifq
)) {
310 ifq
->altq_started
= 0;
320 if_start_dispatch(struct netmsg
*nmsg
)
322 struct lwkt_msg
*lmsg
= &nmsg
->nm_lmsg
;
323 struct ifnet
*ifp
= lmsg
->u
.ms_resultp
;
324 struct ifaltq
*ifq
= &ifp
->if_snd
;
328 lwkt_replymsg(lmsg
, 0); /* reply ASAP */
332 if (!if_start_oncpu_sched
&& mycpuid
!= ifp
->if_start_cpuid(ifp
)) {
334 * If the ifnet is still up, we need to
335 * chase its CPU change.
337 if (ifp
->if_flags
& IFF_UP
) {
338 logifstart(chase_sched
, ifp
);
339 if_start_schedule(ifp
);
347 if (ifp
->if_flags
& IFF_UP
) {
348 ifnet_serialize_tx(ifp
); /* XXX try? */
349 if ((ifp
->if_flags
& IFF_OACTIVE
) == 0) {
350 logifstart(run
, ifp
);
353 (IFF_OACTIVE
| IFF_RUNNING
)) == IFF_RUNNING
)
356 ifnet_deserialize_tx(ifp
);
361 if (if_start_need_schedule(ifq
, running
)) {
363 if (lmsg
->ms_flags
& MSGF_DONE
) { /* XXX necessary? */
364 logifstart(sched
, ifp
);
365 lwkt_sendmsg(ifnet_portfn(mycpuid
), lmsg
);
371 /* Device driver ifnet.if_start helper function */
373 if_devstart(struct ifnet
*ifp
)
375 struct ifaltq
*ifq
= &ifp
->if_snd
;
378 ASSERT_IFNET_SERIALIZED_TX(ifp
);
381 if (ifq
->altq_started
|| !ifq_data_ready(ifq
)) {
382 logifstart(avoid
, ifp
);
386 ifq
->altq_started
= 1;
389 if (if_devstart_schedonly
) {
391 * Always schedule ifnet.if_start on ifnet's CPU,
392 * short circuit the rest of this function.
394 logifstart(sched
, ifp
);
395 if_start_schedule(ifp
);
399 logifstart(run
, ifp
);
402 if ((ifp
->if_flags
& (IFF_OACTIVE
| IFF_RUNNING
)) == IFF_RUNNING
)
405 if (if_devstart_schednochk
|| if_start_need_schedule(ifq
, running
)) {
407 * More data need to be transmitted, ifnet.if_start is
408 * scheduled on ifnet's CPU, and we keep going.
409 * NOTE: ifnet.if_start interlock is not released.
411 logifstart(sched
, ifp
);
412 if_start_schedule(ifp
);
417 if_default_serialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
419 lwkt_serialize_enter(ifp
->if_serializer
);
423 if_default_deserialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
425 lwkt_serialize_exit(ifp
->if_serializer
);
429 if_default_tryserialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
431 return lwkt_serialize_try(ifp
->if_serializer
);
436 if_default_serialize_assert(struct ifnet
*ifp
,
437 enum ifnet_serialize slz __unused
,
438 boolean_t serialized
)
441 ASSERT_SERIALIZED(ifp
->if_serializer
);
443 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
448 * Attach an interface to the list of "active" interfaces.
450 * The serializer is optional. If non-NULL access to the interface
454 if_attach(struct ifnet
*ifp
, lwkt_serialize_t serializer
)
456 unsigned socksize
, ifasize
;
457 int namelen
, masklen
;
458 struct sockaddr_dl
*sdl
;
463 static int if_indexlim
= 8;
465 if (ifp
->if_serialize
!= NULL
) {
466 KASSERT(ifp
->if_deserialize
!= NULL
&&
467 ifp
->if_tryserialize
!= NULL
&&
468 ifp
->if_serialize_assert
!= NULL
,
469 ("serialize functions are partially setup\n"));
472 * If the device supplies serialize functions,
473 * then clear if_serializer to catch any invalid
474 * usage of this field.
476 KASSERT(serializer
== NULL
,
477 ("both serialize functions and default serializer "
479 ifp
->if_serializer
= NULL
;
481 KASSERT(ifp
->if_deserialize
== NULL
&&
482 ifp
->if_tryserialize
== NULL
&&
483 ifp
->if_serialize_assert
== NULL
,
484 ("serialize functions are partially setup\n"));
485 ifp
->if_serialize
= if_default_serialize
;
486 ifp
->if_deserialize
= if_default_deserialize
;
487 ifp
->if_tryserialize
= if_default_tryserialize
;
489 ifp
->if_serialize_assert
= if_default_serialize_assert
;
493 * The serializer can be passed in from the device,
494 * allowing the same serializer to be used for both
495 * the interrupt interlock and the device queue.
496 * If not specified, the netif structure will use an
497 * embedded serializer.
499 if (serializer
== NULL
) {
500 serializer
= &ifp
->if_default_serializer
;
501 lwkt_serialize_init(serializer
);
503 ifp
->if_serializer
= serializer
;
506 ifp
->if_start_cpuid
= if_start_cpuid
;
509 #ifdef DEVICE_POLLING
510 /* Device is not in polling mode by default */
511 ifp
->if_poll_cpuid
= -1;
512 if (ifp
->if_poll
!= NULL
)
513 ifp
->if_start_cpuid
= if_start_cpuid_poll
;
516 ifp
->if_start_nmsg
= kmalloc(ncpus
* sizeof(struct netmsg
),
517 M_LWKTMSG
, M_WAITOK
);
518 for (i
= 0; i
< ncpus
; ++i
) {
519 netmsg_init(&ifp
->if_start_nmsg
[i
], NULL
, &netisr_adone_rport
,
520 0, if_start_dispatch
);
521 ifp
->if_start_nmsg
[i
].nm_lmsg
.u
.ms_resultp
= ifp
;
524 TAILQ_INSERT_TAIL(&ifnet
, ifp
, if_link
);
525 ifp
->if_index
= ++if_index
;
529 * The old code would work if the interface passed a pre-existing
530 * chain of ifaddrs to this code. We don't trust our callers to
531 * properly initialize the tailq, however, so we no longer allow
532 * this unlikely case.
534 ifp
->if_addrheads
= kmalloc(ncpus
* sizeof(struct ifaddrhead
),
535 M_IFADDR
, M_WAITOK
| M_ZERO
);
536 for (i
= 0; i
< ncpus
; ++i
)
537 TAILQ_INIT(&ifp
->if_addrheads
[i
]);
539 TAILQ_INIT(&ifp
->if_prefixhead
);
540 LIST_INIT(&ifp
->if_multiaddrs
);
541 getmicrotime(&ifp
->if_lastchange
);
542 if (ifindex2ifnet
== NULL
|| if_index
>= if_indexlim
) {
548 /* grow ifindex2ifnet */
549 n
= if_indexlim
* sizeof(*q
);
550 q
= kmalloc(n
, M_IFADDR
, M_WAITOK
| M_ZERO
);
552 bcopy(ifindex2ifnet
, q
, n
/2);
553 kfree(ifindex2ifnet
, M_IFADDR
);
558 ifindex2ifnet
[if_index
] = ifp
;
561 * create a Link Level name for this device
563 namelen
= strlen(ifp
->if_xname
);
564 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
565 masklen
= _offsetof(struct sockaddr_dl
, sdl_data
[0]) + namelen
;
566 socksize
= masklen
+ ifp
->if_addrlen
;
567 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
568 if (socksize
< sizeof(*sdl
))
569 socksize
= sizeof(*sdl
);
570 socksize
= ROUNDUP(socksize
);
571 ifasize
= sizeof(struct ifaddr
) + 2 * socksize
;
572 ifa
= ifa_create(ifasize
, M_WAITOK
);
573 sdl
= (struct sockaddr_dl
*)(ifa
+ 1);
574 sdl
->sdl_len
= socksize
;
575 sdl
->sdl_family
= AF_LINK
;
576 bcopy(ifp
->if_xname
, sdl
->sdl_data
, namelen
);
577 sdl
->sdl_nlen
= namelen
;
578 sdl
->sdl_index
= ifp
->if_index
;
579 sdl
->sdl_type
= ifp
->if_type
;
580 ifp
->if_lladdr
= ifa
;
582 ifa
->ifa_rtrequest
= link_rtrequest
;
583 ifa
->ifa_addr
= (struct sockaddr
*)sdl
;
584 sdl
= (struct sockaddr_dl
*)(socksize
+ (caddr_t
)sdl
);
585 ifa
->ifa_netmask
= (struct sockaddr
*)sdl
;
586 sdl
->sdl_len
= masklen
;
588 sdl
->sdl_data
[--namelen
] = 0xff;
589 ifa_iflink(ifa
, ifp
, 0 /* Insert head */);
591 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
592 devctl_notify("IFNET", ifp
->if_xname
, "ATTACH", NULL
);
596 ifq
->altq_disc
= NULL
;
597 ifq
->altq_flags
&= ALTQF_CANTCHANGE
;
598 ifq
->altq_tbr
= NULL
;
600 ifq
->altq_started
= 0;
601 ifq
->altq_prepended
= NULL
;
603 ifq_set_classic(ifq
);
605 if (!SLIST_EMPTY(&domains
))
606 if_attachdomain1(ifp
);
608 /* Announce the interface. */
609 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
613 if_attachdomain(void *dummy
)
618 TAILQ_FOREACH(ifp
, &ifnet
, if_list
)
619 if_attachdomain1(ifp
);
622 SYSINIT(domainifattach
, SI_SUB_PROTO_IFATTACHDOMAIN
, SI_ORDER_FIRST
,
623 if_attachdomain
, NULL
);
626 if_attachdomain1(struct ifnet
*ifp
)
632 /* address family dependent data region */
633 bzero(ifp
->if_afdata
, sizeof(ifp
->if_afdata
));
634 SLIST_FOREACH(dp
, &domains
, dom_next
)
635 if (dp
->dom_ifattach
)
636 ifp
->if_afdata
[dp
->dom_family
] =
637 (*dp
->dom_ifattach
)(ifp
);
642 * Purge all addresses whose type is _not_ AF_LINK
645 if_purgeaddrs_nolink(struct ifnet
*ifp
)
647 struct ifaddr_container
*ifac
, *next
;
649 TAILQ_FOREACH_MUTABLE(ifac
, &ifp
->if_addrheads
[mycpuid
],
651 struct ifaddr
*ifa
= ifac
->ifa
;
653 /* Leave link ifaddr as it is */
654 if (ifa
->ifa_addr
->sa_family
== AF_LINK
)
657 /* XXX: Ugly!! ad hoc just for INET */
658 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
) {
659 struct ifaliasreq ifr
;
660 #ifdef IFADDR_DEBUG_VERBOSE
663 kprintf("purge in4 addr %p: ", ifa
);
664 for (i
= 0; i
< ncpus
; ++i
)
665 kprintf("%d ", ifa
->ifa_containers
[i
].ifa_refcnt
);
669 bzero(&ifr
, sizeof ifr
);
670 ifr
.ifra_addr
= *ifa
->ifa_addr
;
671 if (ifa
->ifa_dstaddr
)
672 ifr
.ifra_broadaddr
= *ifa
->ifa_dstaddr
;
673 if (in_control(NULL
, SIOCDIFADDR
, (caddr_t
)&ifr
, ifp
,
679 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET6
) {
680 #ifdef IFADDR_DEBUG_VERBOSE
683 kprintf("purge in6 addr %p: ", ifa
);
684 for (i
= 0; i
< ncpus
; ++i
)
685 kprintf("%d ", ifa
->ifa_containers
[i
].ifa_refcnt
);
690 /* ifp_addrhead is already updated */
694 ifa_ifunlink(ifa
, ifp
);
700 * Detach an interface, removing it from the
701 * list of "active" interfaces.
704 if_detach(struct ifnet
*ifp
)
706 struct radix_node_head
*rnh
;
711 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
714 * Remove routes and flush queues.
717 #ifdef DEVICE_POLLING
718 if (ifp
->if_flags
& IFF_POLLING
)
719 ether_poll_deregister(ifp
);
722 if (ifp
->if_flags
& IFF_NPOLLING
)
723 ifpoll_deregister(ifp
);
727 if (ifq_is_enabled(&ifp
->if_snd
))
728 altq_disable(&ifp
->if_snd
);
729 if (ifq_is_attached(&ifp
->if_snd
))
730 altq_detach(&ifp
->if_snd
);
733 * Clean up all addresses.
735 ifp
->if_lladdr
= NULL
;
737 if_purgeaddrs_nolink(ifp
);
738 if (!TAILQ_EMPTY(&ifp
->if_addrheads
[mycpuid
])) {
741 ifa
= TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
;
742 KASSERT(ifa
->ifa_addr
->sa_family
== AF_LINK
,
743 ("non-link ifaddr is left on if_addrheads"));
745 ifa_ifunlink(ifa
, ifp
);
747 KASSERT(TAILQ_EMPTY(&ifp
->if_addrheads
[mycpuid
]),
748 ("there are still ifaddrs left on if_addrheads"));
753 * Remove all IPv4 kernel structures related to ifp.
760 * Remove all IPv6 kernel structs related to ifp. This should be done
761 * before removing routing entries below, since IPv6 interface direct
762 * routes are expected to be removed by the IPv6-specific kernel API.
763 * Otherwise, the kernel will detect some inconsistency and bark it.
769 * Delete all remaining routes using this interface
770 * Unfortuneatly the only way to do this is to slog through
771 * the entire routing table looking for routes which point
772 * to this interface...oh well...
775 for (cpu
= 0; cpu
< ncpus2
; cpu
++) {
776 lwkt_migratecpu(cpu
);
777 for (i
= 1; i
<= AF_MAX
; i
++) {
778 if ((rnh
= rt_tables
[cpu
][i
]) == NULL
)
780 rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);
783 lwkt_migratecpu(origcpu
);
785 /* Announce that the interface is gone. */
786 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
787 devctl_notify("IFNET", ifp
->if_xname
, "DETACH", NULL
);
789 SLIST_FOREACH(dp
, &domains
, dom_next
)
790 if (dp
->dom_ifdetach
&& ifp
->if_afdata
[dp
->dom_family
])
791 (*dp
->dom_ifdetach
)(ifp
,
792 ifp
->if_afdata
[dp
->dom_family
]);
795 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
797 ifindex2ifnet
[ifp
->if_index
] = NULL
;
798 while (if_index
> 0 && ifindex2ifnet
[if_index
] == NULL
)
801 TAILQ_REMOVE(&ifnet
, ifp
, if_link
);
802 kfree(ifp
->if_addrheads
, M_IFADDR
);
803 kfree(ifp
->if_start_nmsg
, M_LWKTMSG
);
808 * Delete Routes for a Network Interface
810 * Called for each routing entry via the rnh->rnh_walktree() call above
811 * to delete all route entries referencing a detaching network interface.
814 * rn pointer to node in the routing table
815 * arg argument passed to rnh->rnh_walktree() - detaching interface
819 * errno failed - reason indicated
823 if_rtdel(struct radix_node
*rn
, void *arg
)
825 struct rtentry
*rt
= (struct rtentry
*)rn
;
826 struct ifnet
*ifp
= arg
;
829 if (rt
->rt_ifp
== ifp
) {
832 * Protect (sorta) against walktree recursion problems
835 if (!(rt
->rt_flags
& RTF_UP
))
838 err
= rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
839 rt_mask(rt
), rt
->rt_flags
,
842 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
850 * Locate an interface based on a complete address.
853 ifa_ifwithaddr(struct sockaddr
*addr
)
857 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
858 struct ifaddr_container
*ifac
;
860 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
861 struct ifaddr
*ifa
= ifac
->ifa
;
863 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
865 if (sa_equal(addr
, ifa
->ifa_addr
))
867 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
868 ifa
->ifa_broadaddr
&&
869 /* IPv6 doesn't have broadcast */
870 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
871 sa_equal(ifa
->ifa_broadaddr
, addr
))
878 * Locate the point to point interface with a given destination address.
881 ifa_ifwithdstaddr(struct sockaddr
*addr
)
885 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
886 struct ifaddr_container
*ifac
;
888 if (!(ifp
->if_flags
& IFF_POINTOPOINT
))
891 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
892 struct ifaddr
*ifa
= ifac
->ifa
;
894 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
896 if (ifa
->ifa_dstaddr
&&
897 sa_equal(addr
, ifa
->ifa_dstaddr
))
905 * Find an interface on a specific network. If many, choice
906 * is most specific found.
909 ifa_ifwithnet(struct sockaddr
*addr
)
912 struct ifaddr
*ifa_maybe
= NULL
;
913 u_int af
= addr
->sa_family
;
914 char *addr_data
= addr
->sa_data
, *cplim
;
917 * AF_LINK addresses can be looked up directly by their index number,
918 * so do that if we can.
921 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)addr
;
923 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
)
924 return (ifindex2ifnet
[sdl
->sdl_index
]->if_lladdr
);
928 * Scan though each interface, looking for ones that have
929 * addresses in this address family.
931 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
932 struct ifaddr_container
*ifac
;
934 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
935 struct ifaddr
*ifa
= ifac
->ifa
;
936 char *cp
, *cp2
, *cp3
;
938 if (ifa
->ifa_addr
->sa_family
!= af
)
940 if (af
== AF_INET
&& ifp
->if_flags
& IFF_POINTOPOINT
) {
942 * This is a bit broken as it doesn't
943 * take into account that the remote end may
944 * be a single node in the network we are
946 * The trouble is that we don't know the
947 * netmask for the remote end.
949 if (ifa
->ifa_dstaddr
!= NULL
&&
950 sa_equal(addr
, ifa
->ifa_dstaddr
))
954 * if we have a special address handler,
955 * then use it instead of the generic one.
957 if (ifa
->ifa_claim_addr
) {
958 if ((*ifa
->ifa_claim_addr
)(ifa
, addr
)) {
966 * Scan all the bits in the ifa's address.
967 * If a bit dissagrees with what we are
968 * looking for, mask it with the netmask
969 * to see if it really matters.
972 if (ifa
->ifa_netmask
== 0)
975 cp2
= ifa
->ifa_addr
->sa_data
;
976 cp3
= ifa
->ifa_netmask
->sa_data
;
977 cplim
= ifa
->ifa_netmask
->sa_len
+
978 (char *)ifa
->ifa_netmask
;
980 if ((*cp
++ ^ *cp2
++) & *cp3
++)
981 goto next
; /* next address! */
983 * If the netmask of what we just found
984 * is more specific than what we had before
985 * (if we had one) then remember the new one
986 * before continuing to search
987 * for an even better one.
989 if (ifa_maybe
== 0 ||
990 rn_refines((char *)ifa
->ifa_netmask
,
991 (char *)ifa_maybe
->ifa_netmask
))
1000 * Find an interface address specific to an interface best matching
1004 ifaof_ifpforaddr(struct sockaddr
*addr
, struct ifnet
*ifp
)
1006 struct ifaddr_container
*ifac
;
1007 char *cp
, *cp2
, *cp3
;
1009 struct ifaddr
*ifa_maybe
= 0;
1010 u_int af
= addr
->sa_family
;
1014 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1015 struct ifaddr
*ifa
= ifac
->ifa
;
1017 if (ifa
->ifa_addr
->sa_family
!= af
)
1021 if (ifa
->ifa_netmask
== NULL
) {
1022 if (sa_equal(addr
, ifa
->ifa_addr
) ||
1023 (ifa
->ifa_dstaddr
!= NULL
&&
1024 sa_equal(addr
, ifa
->ifa_dstaddr
)))
1028 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1029 if (sa_equal(addr
, ifa
->ifa_dstaddr
))
1033 cp2
= ifa
->ifa_addr
->sa_data
;
1034 cp3
= ifa
->ifa_netmask
->sa_data
;
1035 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
1036 for (; cp3
< cplim
; cp3
++)
1037 if ((*cp
++ ^ *cp2
++) & *cp3
)
1047 * Default action when installing a route with a Link Level gateway.
1048 * Lookup an appropriate real ifa to point to.
1049 * This should be moved to /sys/net/link.c eventually.
1052 link_rtrequest(int cmd
, struct rtentry
*rt
, struct rt_addrinfo
*info
)
1055 struct sockaddr
*dst
;
1058 if (cmd
!= RTM_ADD
|| (ifa
= rt
->rt_ifa
) == NULL
||
1059 (ifp
= ifa
->ifa_ifp
) == NULL
|| (dst
= rt_key(rt
)) == NULL
)
1061 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1063 IFAFREE(rt
->rt_ifa
);
1066 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
1067 ifa
->ifa_rtrequest(cmd
, rt
, info
);
1072 * Mark an interface down and notify protocols of
1074 * NOTE: must be called at splnet or eqivalent.
1077 if_unroute(struct ifnet
*ifp
, int flag
, int fam
)
1079 struct ifaddr_container
*ifac
;
1081 ifp
->if_flags
&= ~flag
;
1082 getmicrotime(&ifp
->if_lastchange
);
1083 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1084 struct ifaddr
*ifa
= ifac
->ifa
;
1086 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
1087 kpfctlinput(PRC_IFDOWN
, ifa
->ifa_addr
);
1089 ifq_purge(&ifp
->if_snd
);
1094 * Mark an interface up and notify protocols of
1096 * NOTE: must be called at splnet or eqivalent.
1099 if_route(struct ifnet
*ifp
, int flag
, int fam
)
1101 struct ifaddr_container
*ifac
;
1103 ifq_purge(&ifp
->if_snd
);
1104 ifp
->if_flags
|= flag
;
1105 getmicrotime(&ifp
->if_lastchange
);
1106 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1107 struct ifaddr
*ifa
= ifac
->ifa
;
1109 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
1110 kpfctlinput(PRC_IFUP
, ifa
->ifa_addr
);
1119 * Mark an interface down and notify protocols of the transition. An
1120 * interface going down is also considered to be a synchronizing event.
1121 * We must ensure that all packet processing related to the interface
1122 * has completed before we return so e.g. the caller can free the ifnet
1123 * structure that the mbufs may be referencing.
1125 * NOTE: must be called at splnet or eqivalent.
1128 if_down(struct ifnet
*ifp
)
1130 if_unroute(ifp
, IFF_UP
, AF_UNSPEC
);
1131 netmsg_service_sync();
1135 * Mark an interface up and notify protocols of
1137 * NOTE: must be called at splnet or eqivalent.
1140 if_up(struct ifnet
*ifp
)
1142 if_route(ifp
, IFF_UP
, AF_UNSPEC
);
1146 * Process a link state change.
1147 * NOTE: must be called at splsoftnet or equivalent.
1150 if_link_state_change(struct ifnet
*ifp
)
1152 int link_state
= ifp
->if_link_state
;
1155 devctl_notify("IFNET", ifp
->if_xname
,
1156 (link_state
== LINK_STATE_UP
) ? "LINK_UP" : "LINK_DOWN", NULL
);
1160 * Handle interface watchdog timer routines. Called
1161 * from softclock, we decrement timers (if set) and
1162 * call the appropriate interface routine on expiration.
1165 if_slowtimo(void *arg
)
1171 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
1172 if (ifp
->if_timer
== 0 || --ifp
->if_timer
)
1174 if (ifp
->if_watchdog
) {
1175 if (ifnet_tryserialize_all(ifp
)) {
1176 (*ifp
->if_watchdog
)(ifp
);
1177 ifnet_deserialize_all(ifp
);
1179 /* try again next timeout */
1187 callout_reset(&if_slowtimo_timer
, hz
/ IFNET_SLOWHZ
, if_slowtimo
, NULL
);
1191 * Map interface name to
1192 * interface structure pointer.
1195 ifunit(const char *name
)
1200 * Search all the interfaces for this name/number
1203 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
1204 if (strncmp(ifp
->if_xname
, name
, IFNAMSIZ
) == 0)
1212 * Map interface name in a sockaddr_dl to
1213 * interface structure pointer.
1216 if_withname(struct sockaddr
*sa
)
1218 char ifname
[IFNAMSIZ
+1];
1219 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
1221 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
1222 (sdl
->sdl_nlen
> IFNAMSIZ
) )
1226 * ifunit wants a null-terminated name. It may not be null-terminated
1227 * in the sockaddr. We don't want to change the caller's sockaddr,
1228 * and there might not be room to put the trailing null anyway, so we
1229 * make a local copy that we know we can null terminate safely.
1232 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
1233 ifname
[sdl
->sdl_nlen
] = '\0';
1234 return ifunit(ifname
);
1242 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct ucred
*cred
)
1250 size_t namelen
, onamelen
;
1251 char new_name
[IFNAMSIZ
];
1253 struct sockaddr_dl
*sdl
;
1259 return (ifconf(cmd
, data
, cred
));
1261 ifr
= (struct ifreq
*)data
;
1266 if ((error
= priv_check_cred(cred
, PRIV_ROOT
, 0)) != 0)
1268 return ((cmd
== SIOCIFCREATE
) ?
1269 if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
)) :
1270 if_clone_destroy(ifr
->ifr_name
));
1272 case SIOCIFGCLONERS
:
1273 return (if_clone_list((struct if_clonereq
*)data
));
1276 ifp
= ifunit(ifr
->ifr_name
);
1282 ifr
->ifr_index
= ifp
->if_index
;
1286 ifr
->ifr_flags
= ifp
->if_flags
;
1287 ifr
->ifr_flagshigh
= ifp
->if_flags
>> 16;
1291 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
1292 ifr
->ifr_curcap
= ifp
->if_capenable
;
1296 ifr
->ifr_metric
= ifp
->if_metric
;
1300 ifr
->ifr_mtu
= ifp
->if_mtu
;
1304 ifr
->ifr_phys
= ifp
->if_physical
;
1307 case SIOCGIFPOLLCPU
:
1308 #ifdef DEVICE_POLLING
1309 ifr
->ifr_pollcpu
= ifp
->if_poll_cpuid
;
1311 ifr
->ifr_pollcpu
= -1;
1315 case SIOCSIFPOLLCPU
:
1316 #ifdef DEVICE_POLLING
1317 if ((ifp
->if_flags
& IFF_POLLING
) == 0)
1318 ether_pollcpu_register(ifp
, ifr
->ifr_pollcpu
);
1323 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1326 new_flags
= (ifr
->ifr_flags
& 0xffff) |
1327 (ifr
->ifr_flagshigh
<< 16);
1328 if (ifp
->if_flags
& IFF_SMART
) {
1329 /* Smart drivers twiddle their own routes */
1330 } else if (ifp
->if_flags
& IFF_UP
&&
1331 (new_flags
& IFF_UP
) == 0) {
1335 } else if (new_flags
& IFF_UP
&&
1336 (ifp
->if_flags
& IFF_UP
) == 0) {
1342 #ifdef DEVICE_POLLING
1343 if ((new_flags
^ ifp
->if_flags
) & IFF_POLLING
) {
1344 if (new_flags
& IFF_POLLING
) {
1345 ether_poll_register(ifp
);
1347 ether_poll_deregister(ifp
);
1351 #ifdef IFPOLL_ENABLE
1352 if ((new_flags
^ ifp
->if_flags
) & IFF_NPOLLING
) {
1353 if (new_flags
& IFF_NPOLLING
)
1354 ifpoll_register(ifp
);
1356 ifpoll_deregister(ifp
);
1360 ifp
->if_flags
= (ifp
->if_flags
& IFF_CANTCHANGE
) |
1361 (new_flags
&~ IFF_CANTCHANGE
);
1362 if (new_flags
& IFF_PPROMISC
) {
1363 /* Permanently promiscuous mode requested */
1364 ifp
->if_flags
|= IFF_PROMISC
;
1365 } else if (ifp
->if_pcount
== 0) {
1366 ifp
->if_flags
&= ~IFF_PROMISC
;
1368 if (ifp
->if_ioctl
) {
1369 ifnet_serialize_all(ifp
);
1370 ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1371 ifnet_deserialize_all(ifp
);
1373 getmicrotime(&ifp
->if_lastchange
);
1377 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1380 if (ifr
->ifr_reqcap
& ~ifp
->if_capabilities
)
1382 ifnet_serialize_all(ifp
);
1383 ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1384 ifnet_deserialize_all(ifp
);
1388 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1391 error
= copyinstr(ifr
->ifr_data
, new_name
, IFNAMSIZ
, NULL
);
1394 if (new_name
[0] == '\0')
1396 if (ifunit(new_name
) != NULL
)
1399 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
1401 /* Announce the departure of the interface. */
1402 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
1404 strlcpy(ifp
->if_xname
, new_name
, sizeof(ifp
->if_xname
));
1405 ifa
= TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
;
1406 /* XXX IFA_LOCK(ifa); */
1407 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1408 namelen
= strlen(new_name
);
1409 onamelen
= sdl
->sdl_nlen
;
1411 * Move the address if needed. This is safe because we
1412 * allocate space for a name of length IFNAMSIZ when we
1413 * create this in if_attach().
1415 if (namelen
!= onamelen
) {
1416 bcopy(sdl
->sdl_data
+ onamelen
,
1417 sdl
->sdl_data
+ namelen
, sdl
->sdl_alen
);
1419 bcopy(new_name
, sdl
->sdl_data
, namelen
);
1420 sdl
->sdl_nlen
= namelen
;
1421 sdl
= (struct sockaddr_dl
*)ifa
->ifa_netmask
;
1422 bzero(sdl
->sdl_data
, onamelen
);
1423 while (namelen
!= 0)
1424 sdl
->sdl_data
[--namelen
] = 0xff;
1425 /* XXX IFA_UNLOCK(ifa) */
1427 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
1429 /* Announce the return of the interface. */
1430 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
1434 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1437 ifp
->if_metric
= ifr
->ifr_metric
;
1438 getmicrotime(&ifp
->if_lastchange
);
1442 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1447 ifnet_serialize_all(ifp
);
1448 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1449 ifnet_deserialize_all(ifp
);
1451 getmicrotime(&ifp
->if_lastchange
);
1456 u_long oldmtu
= ifp
->if_mtu
;
1458 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1461 if (ifp
->if_ioctl
== NULL
)
1462 return (EOPNOTSUPP
);
1463 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
1465 ifnet_serialize_all(ifp
);
1466 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1467 ifnet_deserialize_all(ifp
);
1469 getmicrotime(&ifp
->if_lastchange
);
1473 * If the link MTU changed, do network layer specific procedure.
1475 if (ifp
->if_mtu
!= oldmtu
) {
1485 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1489 /* Don't allow group membership on non-multicast interfaces. */
1490 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
1493 /* Don't let users screw up protocols' entries. */
1494 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
1497 if (cmd
== SIOCADDMULTI
) {
1498 struct ifmultiaddr
*ifma
;
1499 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, &ifma
);
1501 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
1504 getmicrotime(&ifp
->if_lastchange
);
1507 case SIOCSIFPHYADDR
:
1508 case SIOCDIFPHYADDR
:
1510 case SIOCSIFPHYADDR_IN6
:
1512 case SIOCSLIFPHYADDR
:
1514 case SIOCSIFGENERIC
:
1515 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1518 if (ifp
->if_ioctl
== 0)
1519 return (EOPNOTSUPP
);
1520 ifnet_serialize_all(ifp
);
1521 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1522 ifnet_deserialize_all(ifp
);
1524 getmicrotime(&ifp
->if_lastchange
);
1528 ifs
= (struct ifstat
*)data
;
1529 ifs
->ascii
[0] = '\0';
1531 case SIOCGIFPSRCADDR
:
1532 case SIOCGIFPDSTADDR
:
1533 case SIOCGLIFPHYADDR
:
1535 case SIOCGIFGENERIC
:
1536 if (ifp
->if_ioctl
== NULL
)
1537 return (EOPNOTSUPP
);
1538 ifnet_serialize_all(ifp
);
1539 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1540 ifnet_deserialize_all(ifp
);
1544 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1547 return if_setlladdr(ifp
,
1548 ifr
->ifr_addr
.sa_data
, ifr
->ifr_addr
.sa_len
);
1551 oif_flags
= ifp
->if_flags
;
1552 if (so
->so_proto
== 0)
1553 return (EOPNOTSUPP
);
1555 error
= so_pru_control(so
, cmd
, data
, ifp
);
1562 case SIOCSIFDSTADDR
:
1564 case SIOCSIFBRDADDR
:
1565 case SIOCSIFNETMASK
:
1566 #if BYTE_ORDER != BIG_ENDIAN
1567 if (ifr
->ifr_addr
.sa_family
== 0 &&
1568 ifr
->ifr_addr
.sa_len
< 16) {
1569 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1570 ifr
->ifr_addr
.sa_len
= 16;
1573 if (ifr
->ifr_addr
.sa_len
== 0)
1574 ifr
->ifr_addr
.sa_len
= 16;
1582 case OSIOCGIFDSTADDR
:
1583 cmd
= SIOCGIFDSTADDR
;
1586 case OSIOCGIFBRDADDR
:
1587 cmd
= SIOCGIFBRDADDR
;
1590 case OSIOCGIFNETMASK
:
1591 cmd
= SIOCGIFNETMASK
;
1593 error
= so_pru_control(so
, cmd
, data
, ifp
);
1597 case OSIOCGIFDSTADDR
:
1598 case OSIOCGIFBRDADDR
:
1599 case OSIOCGIFNETMASK
:
1600 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1604 #endif /* COMPAT_43 */
1606 if ((oif_flags
^ ifp
->if_flags
) & IFF_UP
) {
1608 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1609 if (ifp
->if_flags
& IFF_UP
) {
1623 * Set/clear promiscuous mode on interface ifp based on the truth value
1624 * of pswitch. The calls are reference counted so that only the first
1625 * "on" request actually has an effect, as does the final "off" request.
1626 * Results are undefined if the "off" and "on" requests are not matched.
1629 ifpromisc(struct ifnet
*ifp
, int pswitch
)
1635 oldflags
= ifp
->if_flags
;
1636 if (ifp
->if_flags
& IFF_PPROMISC
) {
1637 /* Do nothing if device is in permanently promiscuous mode */
1638 ifp
->if_pcount
+= pswitch
? 1 : -1;
1643 * If the device is not configured up, we cannot put it in
1646 if ((ifp
->if_flags
& IFF_UP
) == 0)
1648 if (ifp
->if_pcount
++ != 0)
1650 ifp
->if_flags
|= IFF_PROMISC
;
1651 log(LOG_INFO
, "%s: promiscuous mode enabled\n",
1654 if (--ifp
->if_pcount
> 0)
1656 ifp
->if_flags
&= ~IFF_PROMISC
;
1657 log(LOG_INFO
, "%s: promiscuous mode disabled\n",
1660 ifr
.ifr_flags
= ifp
->if_flags
;
1661 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
1662 ifnet_serialize_all(ifp
);
1663 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, NULL
);
1664 ifnet_deserialize_all(ifp
);
1668 ifp
->if_flags
= oldflags
;
1673 * Return interface configuration
1674 * of system. List may be used
1675 * in later ioctl's (above) to get
1676 * other information.
1679 ifconf(u_long cmd
, caddr_t data
, struct ucred
*cred
)
1681 struct ifconf
*ifc
= (struct ifconf
*)data
;
1683 struct sockaddr
*sa
;
1684 struct ifreq ifr
, *ifrp
;
1685 int space
= ifc
->ifc_len
, error
= 0;
1687 ifrp
= ifc
->ifc_req
;
1688 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
1689 struct ifaddr_container
*ifac
;
1692 if (space
<= sizeof ifr
)
1696 * Zero the stack declared structure first to prevent
1697 * memory disclosure.
1699 bzero(&ifr
, sizeof(ifr
));
1700 if (strlcpy(ifr
.ifr_name
, ifp
->if_xname
, sizeof(ifr
.ifr_name
))
1701 >= sizeof(ifr
.ifr_name
)) {
1702 error
= ENAMETOOLONG
;
1707 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1708 struct ifaddr
*ifa
= ifac
->ifa
;
1710 if (space
<= sizeof ifr
)
1713 if (cred
->cr_prison
&&
1714 prison_if(cred
, sa
))
1718 if (cmd
== OSIOCGIFCONF
) {
1719 struct osockaddr
*osa
=
1720 (struct osockaddr
*)&ifr
.ifr_addr
;
1722 osa
->sa_family
= sa
->sa_family
;
1723 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
1727 if (sa
->sa_len
<= sizeof(*sa
)) {
1729 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
1732 if (space
< (sizeof ifr
) + sa
->sa_len
-
1735 space
-= sa
->sa_len
- sizeof(*sa
);
1736 error
= copyout(&ifr
, ifrp
,
1737 sizeof ifr
.ifr_name
);
1739 error
= copyout(sa
, &ifrp
->ifr_addr
,
1741 ifrp
= (struct ifreq
*)
1742 (sa
->sa_len
+ (caddr_t
)&ifrp
->ifr_addr
);
1746 space
-= sizeof ifr
;
1751 bzero(&ifr
.ifr_addr
, sizeof ifr
.ifr_addr
);
1752 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
1755 space
-= sizeof ifr
;
1759 ifc
->ifc_len
-= space
;
1764 * Just like if_promisc(), but for all-multicast-reception mode.
1767 if_allmulti(struct ifnet
*ifp
, int onswitch
)
1775 if (ifp
->if_amcount
++ == 0) {
1776 ifp
->if_flags
|= IFF_ALLMULTI
;
1777 ifr
.ifr_flags
= ifp
->if_flags
;
1778 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
1779 ifnet_serialize_all(ifp
);
1780 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
1782 ifnet_deserialize_all(ifp
);
1785 if (ifp
->if_amcount
> 1) {
1788 ifp
->if_amcount
= 0;
1789 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1790 ifr
.ifr_flags
= ifp
->if_flags
;
1791 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
1792 ifnet_serialize_all(ifp
);
1793 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
1795 ifnet_deserialize_all(ifp
);
1807 * Add a multicast listenership to the interface in question.
1808 * The link layer provides a routine which converts
1812 struct ifnet
*ifp
, /* interface to manipulate */
1813 struct sockaddr
*sa
, /* address to add */
1814 struct ifmultiaddr
**retifma
)
1816 struct sockaddr
*llsa
, *dupsa
;
1818 struct ifmultiaddr
*ifma
;
1821 * If the matching multicast address already exists
1822 * then don't add a new one, just add a reference
1824 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
1825 if (sa_equal(sa
, ifma
->ifma_addr
)) {
1826 ifma
->ifma_refcount
++;
1834 * Give the link layer a chance to accept/reject it, and also
1835 * find out which AF_LINK address this maps to, if it isn't one
1838 if (ifp
->if_resolvemulti
) {
1839 ifnet_serialize_all(ifp
);
1840 error
= ifp
->if_resolvemulti(ifp
, &llsa
, sa
);
1841 ifnet_deserialize_all(ifp
);
1848 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1849 MALLOC(dupsa
, struct sockaddr
*, sa
->sa_len
, M_IFMADDR
, M_WAITOK
);
1850 bcopy(sa
, dupsa
, sa
->sa_len
);
1852 ifma
->ifma_addr
= dupsa
;
1853 ifma
->ifma_lladdr
= llsa
;
1854 ifma
->ifma_ifp
= ifp
;
1855 ifma
->ifma_refcount
= 1;
1856 ifma
->ifma_protospec
= 0;
1857 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1860 * Some network interfaces can scan the address list at
1861 * interrupt time; lock them out.
1864 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1869 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
1870 if (sa_equal(ifma
->ifma_addr
, llsa
))
1874 ifma
->ifma_refcount
++;
1876 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
,
1877 M_IFMADDR
, M_WAITOK
);
1878 MALLOC(dupsa
, struct sockaddr
*, llsa
->sa_len
,
1879 M_IFMADDR
, M_WAITOK
);
1880 bcopy(llsa
, dupsa
, llsa
->sa_len
);
1881 ifma
->ifma_addr
= dupsa
;
1882 ifma
->ifma_ifp
= ifp
;
1883 ifma
->ifma_refcount
= 1;
1885 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1890 * We are certain we have added something, so call down to the
1891 * interface to let them know about it.
1894 ifnet_serialize_all(ifp
);
1895 ifp
->if_ioctl(ifp
, SIOCADDMULTI
, 0, NULL
);
1896 ifnet_deserialize_all(ifp
);
1903 * Remove a reference to a multicast address on this interface. Yell
1904 * if the request does not match an existing membership.
1907 if_delmulti(struct ifnet
*ifp
, struct sockaddr
*sa
)
1909 struct ifmultiaddr
*ifma
;
1911 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
1912 if (sa_equal(sa
, ifma
->ifma_addr
))
1917 if (ifma
->ifma_refcount
> 1) {
1918 ifma
->ifma_refcount
--;
1922 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
1923 sa
= ifma
->ifma_lladdr
;
1925 LIST_REMOVE(ifma
, ifma_link
);
1927 * Make sure the interface driver is notified
1928 * in the case of a link layer mcast group being left.
1930 if (ifma
->ifma_addr
->sa_family
== AF_LINK
&& sa
== 0) {
1931 ifnet_serialize_all(ifp
);
1932 ifp
->if_ioctl(ifp
, SIOCDELMULTI
, 0, NULL
);
1933 ifnet_deserialize_all(ifp
);
1936 kfree(ifma
->ifma_addr
, M_IFMADDR
);
1937 kfree(ifma
, M_IFMADDR
);
1942 * Now look for the link-layer address which corresponds to
1943 * this network address. It had been squirreled away in
1944 * ifma->ifma_lladdr for this purpose (so we don't have
1945 * to call ifp->if_resolvemulti() again), and we saved that
1946 * value in sa above. If some nasty deleted the
1947 * link-layer address out from underneath us, we can deal because
1948 * the address we stored was is not the same as the one which was
1949 * in the record for the link-layer address. (So we don't complain
1952 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
1953 if (sa_equal(sa
, ifma
->ifma_addr
))
1958 if (ifma
->ifma_refcount
> 1) {
1959 ifma
->ifma_refcount
--;
1964 ifnet_serialize_all(ifp
);
1965 LIST_REMOVE(ifma
, ifma_link
);
1966 ifp
->if_ioctl(ifp
, SIOCDELMULTI
, 0, NULL
);
1967 ifnet_deserialize_all(ifp
);
1969 kfree(ifma
->ifma_addr
, M_IFMADDR
);
1970 kfree(sa
, M_IFMADDR
);
1971 kfree(ifma
, M_IFMADDR
);
1977 * Set the link layer address on an interface.
1979 * At this time we only support certain types of interfaces,
1980 * and we don't allow the length of the address to change.
1983 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
1985 struct sockaddr_dl
*sdl
;
1988 sdl
= IF_LLSOCKADDR(ifp
);
1991 if (len
!= sdl
->sdl_alen
) /* don't allow length to change */
1993 switch (ifp
->if_type
) {
1994 case IFT_ETHER
: /* these types use struct arpcom */
1997 bcopy(lladdr
, ((struct arpcom
*)ifp
->if_softc
)->ac_enaddr
, len
);
1998 bcopy(lladdr
, LLADDR(sdl
), len
);
2004 * If the interface is already up, we need
2005 * to re-init it in order to reprogram its
2008 ifnet_serialize_all(ifp
);
2009 if ((ifp
->if_flags
& IFF_UP
) != 0) {
2010 struct ifaddr_container
*ifac
;
2012 ifp
->if_flags
&= ~IFF_UP
;
2013 ifr
.ifr_flags
= ifp
->if_flags
;
2014 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2015 ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2017 ifp
->if_flags
|= IFF_UP
;
2018 ifr
.ifr_flags
= ifp
->if_flags
;
2019 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2020 ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2024 * Also send gratuitous ARPs to notify other nodes about
2025 * the address change.
2027 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
2028 struct ifaddr
*ifa
= ifac
->ifa
;
2030 if (ifa
->ifa_addr
!= NULL
&&
2031 ifa
->ifa_addr
->sa_family
== AF_INET
)
2032 arp_ifinit(ifp
, ifa
);
2036 ifnet_deserialize_all(ifp
);
2040 struct ifmultiaddr
*
2041 ifmaof_ifpforaddr(struct sockaddr
*sa
, struct ifnet
*ifp
)
2043 struct ifmultiaddr
*ifma
;
2045 LIST_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
2046 if (sa_equal(ifma
->ifma_addr
, sa
))
2053 * This function locates the first real ethernet MAC from a network
2054 * card and loads it into node, returning 0 on success or ENOENT if
2055 * no suitable interfaces were found. It is used by the uuid code to
2056 * generate a unique 6-byte number.
2059 if_getanyethermac(uint16_t *node
, int minlen
)
2062 struct sockaddr_dl
*sdl
;
2064 TAILQ_FOREACH(ifp
, &ifnet
, if_link
) {
2065 if (ifp
->if_type
!= IFT_ETHER
)
2067 sdl
= IF_LLSOCKADDR(ifp
);
2068 if (sdl
->sdl_alen
< minlen
)
2070 bcopy(((struct arpcom
*)ifp
->if_softc
)->ac_enaddr
, node
,
2078 * The name argument must be a pointer to storage which will last as
2079 * long as the interface does. For physical devices, the result of
2080 * device_get_name(dev) is a good choice and for pseudo-devices a
2081 * static string works well.
2084 if_initname(struct ifnet
*ifp
, const char *name
, int unit
)
2086 ifp
->if_dname
= name
;
2087 ifp
->if_dunit
= unit
;
2088 if (unit
!= IF_DUNIT_NONE
)
2089 ksnprintf(ifp
->if_xname
, IFNAMSIZ
, "%s%d", name
, unit
);
2091 strlcpy(ifp
->if_xname
, name
, IFNAMSIZ
);
2095 if_printf(struct ifnet
*ifp
, const char *fmt
, ...)
2100 retval
= kprintf("%s: ", ifp
->if_xname
);
2101 __va_start(ap
, fmt
);
2102 retval
+= kvprintf(fmt
, ap
);
2108 ifq_set_classic(struct ifaltq
*ifq
)
2110 ifq
->altq_enqueue
= ifq_classic_enqueue
;
2111 ifq
->altq_dequeue
= ifq_classic_dequeue
;
2112 ifq
->altq_request
= ifq_classic_request
;
2116 ifq_classic_enqueue(struct ifaltq
*ifq
, struct mbuf
*m
,
2117 struct altq_pktattr
*pa __unused
)
2119 logifq(enqueue
, ifq
);
2120 if (IF_QFULL(ifq
)) {
2130 ifq_classic_dequeue(struct ifaltq
*ifq
, struct mbuf
*mpolled
, int op
)
2139 logifq(dequeue
, ifq
);
2143 panic("unsupported ALTQ dequeue op: %d", op
);
2145 KKASSERT(mpolled
== NULL
|| mpolled
== m
);
2150 ifq_classic_request(struct ifaltq
*ifq
, int req
, void *arg
)
2157 panic("unsupported ALTQ request: %d", req
);
2163 ifq_dispatch(struct ifnet
*ifp
, struct mbuf
*m
, struct altq_pktattr
*pa
)
2165 struct ifaltq
*ifq
= &ifp
->if_snd
;
2166 int running
= 0, error
, start
= 0;
2168 ASSERT_IFNET_NOT_SERIALIZED_TX(ifp
);
2171 error
= ifq_enqueue_locked(ifq
, m
, pa
);
2176 if (!ifq
->altq_started
) {
2178 * Hold the interlock of ifnet.if_start
2180 ifq
->altq_started
= 1;
2185 ifp
->if_obytes
+= m
->m_pkthdr
.len
;
2186 if (m
->m_flags
& M_MCAST
)
2190 logifstart(avoid
, ifp
);
2194 if (ifq_dispatch_schedonly
) {
2196 * Always schedule ifnet.if_start on ifnet's CPU,
2197 * short circuit the rest of this function.
2199 logifstart(sched
, ifp
);
2200 if_start_schedule(ifp
);
2205 * Try to do direct ifnet.if_start first, if there is
2206 * contention on ifnet's serializer, ifnet.if_start will
2207 * be scheduled on ifnet's CPU.
2209 if (!ifnet_tryserialize_tx(ifp
)) {
2211 * ifnet serializer contention happened,
2212 * ifnet.if_start is scheduled on ifnet's
2213 * CPU, and we keep going.
2215 logifstart(contend_sched
, ifp
);
2216 if_start_schedule(ifp
);
2220 if ((ifp
->if_flags
& IFF_OACTIVE
) == 0) {
2221 logifstart(run
, ifp
);
2223 if ((ifp
->if_flags
&
2224 (IFF_OACTIVE
| IFF_RUNNING
)) == IFF_RUNNING
)
2228 ifnet_deserialize_tx(ifp
);
2230 if (ifq_dispatch_schednochk
|| if_start_need_schedule(ifq
, running
)) {
2232 * More data need to be transmitted, ifnet.if_start is
2233 * scheduled on ifnet's CPU, and we keep going.
2234 * NOTE: ifnet.if_start interlock is not released.
2236 logifstart(sched
, ifp
);
2237 if_start_schedule(ifp
);
2243 ifa_create(int size
, int flags
)
2248 KASSERT(size
>= sizeof(*ifa
), ("ifaddr size too small\n"));
2250 ifa
= kmalloc(size
, M_IFADDR
, flags
| M_ZERO
);
2254 ifa
->ifa_containers
= kmalloc(ncpus
* sizeof(struct ifaddr_container
),
2255 M_IFADDR
, M_WAITOK
| M_ZERO
);
2256 ifa
->ifa_ncnt
= ncpus
;
2257 for (i
= 0; i
< ncpus
; ++i
) {
2258 struct ifaddr_container
*ifac
= &ifa
->ifa_containers
[i
];
2260 ifac
->ifa_magic
= IFA_CONTAINER_MAGIC
;
2262 ifac
->ifa_refcnt
= 1;
2265 kprintf("alloc ifa %p %d\n", ifa
, size
);
2271 ifac_free(struct ifaddr_container
*ifac
, int cpu_id
)
2273 struct ifaddr
*ifa
= ifac
->ifa
;
2275 KKASSERT(ifac
->ifa_magic
== IFA_CONTAINER_MAGIC
);
2276 KKASSERT(ifac
->ifa_refcnt
== 0);
2277 KASSERT(ifac
->ifa_listmask
== 0,
2278 ("ifa is still on %#x lists\n", ifac
->ifa_listmask
));
2280 ifac
->ifa_magic
= IFA_CONTAINER_DEAD
;
2282 #ifdef IFADDR_DEBUG_VERBOSE
2283 kprintf("try free ifa %p cpu_id %d\n", ifac
->ifa
, cpu_id
);
2286 KASSERT(ifa
->ifa_ncnt
> 0 && ifa
->ifa_ncnt
<= ncpus
,
2287 ("invalid # of ifac, %d\n", ifa
->ifa_ncnt
));
2288 if (atomic_fetchadd_int(&ifa
->ifa_ncnt
, -1) == 1) {
2290 kprintf("free ifa %p\n", ifa
);
2292 kfree(ifa
->ifa_containers
, M_IFADDR
);
2293 kfree(ifa
, M_IFADDR
);
2298 ifa_iflink_dispatch(struct netmsg
*nmsg
)
2300 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
2301 struct ifaddr
*ifa
= msg
->ifa
;
2302 struct ifnet
*ifp
= msg
->ifp
;
2304 struct ifaddr_container
*ifac
;
2308 ifac
= &ifa
->ifa_containers
[cpu
];
2309 ASSERT_IFAC_VALID(ifac
);
2310 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IFADDRHEAD
) == 0,
2311 ("ifaddr is on if_addrheads\n"));
2313 ifac
->ifa_listmask
|= IFA_LIST_IFADDRHEAD
;
2315 TAILQ_INSERT_TAIL(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
2317 TAILQ_INSERT_HEAD(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
2321 ifa_forwardmsg(&nmsg
->nm_lmsg
, cpu
+ 1);
2325 ifa_iflink(struct ifaddr
*ifa
, struct ifnet
*ifp
, int tail
)
2327 struct netmsg_ifaddr msg
;
2329 netmsg_init(&msg
.netmsg
, NULL
, &curthread
->td_msgport
,
2330 0, ifa_iflink_dispatch
);
2335 ifa_domsg(&msg
.netmsg
.nm_lmsg
, 0);
2339 ifa_ifunlink_dispatch(struct netmsg
*nmsg
)
2341 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
2342 struct ifaddr
*ifa
= msg
->ifa
;
2343 struct ifnet
*ifp
= msg
->ifp
;
2345 struct ifaddr_container
*ifac
;
2349 ifac
= &ifa
->ifa_containers
[cpu
];
2350 ASSERT_IFAC_VALID(ifac
);
2351 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IFADDRHEAD
,
2352 ("ifaddr is not on if_addrhead\n"));
2354 TAILQ_REMOVE(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
2355 ifac
->ifa_listmask
&= ~IFA_LIST_IFADDRHEAD
;
2359 ifa_forwardmsg(&nmsg
->nm_lmsg
, cpu
+ 1);
2363 ifa_ifunlink(struct ifaddr
*ifa
, struct ifnet
*ifp
)
2365 struct netmsg_ifaddr msg
;
2367 netmsg_init(&msg
.netmsg
, NULL
, &curthread
->td_msgport
,
2368 0, ifa_ifunlink_dispatch
);
2372 ifa_domsg(&msg
.netmsg
.nm_lmsg
, 0);
2376 ifa_destroy_dispatch(struct netmsg
*nmsg
)
2378 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
2381 ifa_forwardmsg(&nmsg
->nm_lmsg
, mycpuid
+ 1);
2385 ifa_destroy(struct ifaddr
*ifa
)
2387 struct netmsg_ifaddr msg
;
2389 netmsg_init(&msg
.netmsg
, NULL
, &curthread
->td_msgport
,
2390 0, ifa_destroy_dispatch
);
2393 ifa_domsg(&msg
.netmsg
.nm_lmsg
, 0);
2397 ifnet_portfn(int cpu
)
2399 return &ifnet_threads
[cpu
].td_msgport
;
2403 ifnet_forwardmsg(struct lwkt_msg
*lmsg
, int next_cpu
)
2405 KKASSERT(next_cpu
> mycpuid
&& next_cpu
<= ncpus
);
2407 if (next_cpu
< ncpus
)
2408 lwkt_forwardmsg(ifnet_portfn(next_cpu
), lmsg
);
2410 lwkt_replymsg(lmsg
, 0);
2414 ifnet_domsg(struct lwkt_msg
*lmsg
, int cpu
)
2416 KKASSERT(cpu
< ncpus
);
2417 return lwkt_domsg(ifnet_portfn(cpu
), lmsg
, 0);
2421 ifnet_sendmsg(struct lwkt_msg
*lmsg
, int cpu
)
2423 KKASSERT(cpu
< ncpus
);
2424 lwkt_sendmsg(ifnet_portfn(cpu
), lmsg
);
2428 ifnetinit(void *dummy __unused
)
2432 for (i
= 0; i
< ncpus
; ++i
) {
2433 struct thread
*thr
= &ifnet_threads
[i
];
2435 lwkt_create(netmsg_service_loop
, &ifnet_mpsafe_thread
, NULL
,
2436 thr
, TDF_NETWORK
| TDF_MPSAFE
, i
, "ifnet %d", i
);
2437 netmsg_service_port_init(&thr
->td_msgport
);