Improve the customizeiso step to allow restarts
[dragonfly.git] / sys / net / if.c
blob09f752d34feef5d02ba450a3fc02b4b19ed86e44
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. 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
31 * SUCH DAMAGE.
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"
40 #include "opt_inet.h"
41 #include "opt_polling.h"
43 #include <sys/param.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/priv.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/socketops.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
55 #include <sys/ktr.h>
56 #include <sys/sockio.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 #include <sys/domain.h>
60 #include <sys/thread.h>
61 #include <sys/thread2.h>
62 #include <sys/serialize.h>
63 #include <sys/msgport2.h>
64 #include <sys/bus.h>
66 #include <net/if.h>
67 #include <net/if_arp.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/if_var.h>
71 #include <net/ifq_var.h>
72 #include <net/radix.h>
73 #include <net/route.h>
74 #include <net/if_clone.h>
75 #include <net/netisr.h>
76 #include <net/netmsg2.h>
78 #include <machine/atomic.h>
79 #include <machine/stdarg.h>
80 #include <machine/smp.h>
82 #if defined(INET) || defined(INET6)
83 /*XXX*/
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/if_ether.h>
87 #ifdef INET6
88 #include <netinet6/in6_var.h>
89 #include <netinet6/in6_ifattach.h>
90 #endif
91 #endif
93 #if defined(COMPAT_43)
94 #include <emulation/43bsd/43bsd_socket.h>
95 #endif /* COMPAT_43 */
97 struct netmsg_ifaddr {
98 struct netmsg netmsg;
99 struct ifaddr *ifa;
100 struct ifnet *ifp;
101 int tail;
105 * System initialization
107 static void if_attachdomain(void *);
108 static void if_attachdomain1(struct ifnet *);
109 static int ifconf(u_long, caddr_t, struct ucred *);
110 static void ifinit(void *);
111 static void ifnetinit(void *);
112 static void if_slowtimo(void *);
113 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
114 static int if_rtdel(struct radix_node *, void *);
116 #ifdef INET6
118 * XXX: declare here to avoid to include many inet6 related files..
119 * should be more generalized?
121 extern void nd6_setmtu(struct ifnet *);
122 #endif
124 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
125 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
127 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
128 /* Must be after netisr_init */
129 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
131 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
132 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
134 int ifqmaxlen = IFQ_MAXLEN;
135 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
137 /* In ifq_dispatch(), try to do direct ifnet.if_start first */
138 static int ifq_dispatch_schedonly = 0;
139 SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schedonly, CTLFLAG_RW,
140 &ifq_dispatch_schedonly, 0, "");
142 /* In ifq_dispatch(), schedule ifnet.if_start without checking ifnet.if_snd */
143 static int ifq_dispatch_schednochk = 0;
144 SYSCTL_INT(_net_link_generic, OID_AUTO, ifq_dispatch_schednochk, CTLFLAG_RW,
145 &ifq_dispatch_schednochk, 0, "");
147 /* In if_devstart(), try to do direct ifnet.if_start first */
148 static int if_devstart_schedonly = 0;
149 SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schedonly, CTLFLAG_RW,
150 &if_devstart_schedonly, 0, "");
152 /* In if_devstart(), schedule ifnet.if_start without checking ifnet.if_snd */
153 static int if_devstart_schednochk = 0;
154 SYSCTL_INT(_net_link_generic, OID_AUTO, if_devstart_schednochk, CTLFLAG_RW,
155 &if_devstart_schednochk, 0, "");
157 #ifdef SMP
158 /* Schedule ifnet.if_start on the current CPU */
159 static int if_start_oncpu_sched = 0;
160 SYSCTL_INT(_net_link_generic, OID_AUTO, if_start_oncpu_sched, CTLFLAG_RW,
161 &if_start_oncpu_sched, 0, "");
162 #endif
164 struct callout if_slowtimo_timer;
166 int if_index = 0;
167 struct ifnet **ifindex2ifnet = NULL;
168 static struct thread ifnet_threads[MAXCPU];
169 static int ifnet_mpsafe_thread = NETMSG_SERVICE_MPSAFE;
171 #define IFQ_KTR_STRING "ifq=%p"
172 #define IFQ_KTR_ARG_SIZE (sizeof(void *))
173 #ifndef KTR_IFQ
174 #define KTR_IFQ KTR_ALL
175 #endif
176 KTR_INFO_MASTER(ifq);
177 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
178 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARG_SIZE);
179 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
181 #define IF_START_KTR_STRING "ifp=%p"
182 #define IF_START_KTR_ARG_SIZE (sizeof(void *))
183 #ifndef KTR_IF_START
184 #define KTR_IF_START KTR_ALL
185 #endif
186 KTR_INFO_MASTER(if_start);
187 KTR_INFO(KTR_IF_START, if_start, run, 0,
188 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
189 KTR_INFO(KTR_IF_START, if_start, sched, 1,
190 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
191 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
192 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
193 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
194 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
195 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
196 IF_START_KTR_STRING, IF_START_KTR_ARG_SIZE);
197 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
200 * Network interface utility routines.
202 * Routines with ifa_ifwith* names take sockaddr *'s as
203 * parameters.
205 /* ARGSUSED*/
206 void
207 ifinit(void *dummy)
209 struct ifnet *ifp;
211 callout_init(&if_slowtimo_timer);
213 crit_enter();
214 TAILQ_FOREACH(ifp, &ifnet, if_link) {
215 if (ifp->if_snd.ifq_maxlen == 0) {
216 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
217 ifp->if_snd.ifq_maxlen = ifqmaxlen;
220 crit_exit();
222 if_slowtimo(0);
225 static int
226 if_start_cpuid(struct ifnet *ifp)
228 return ifp->if_cpuid;
231 #ifdef DEVICE_POLLING
232 static int
233 if_start_cpuid_poll(struct ifnet *ifp)
235 int poll_cpuid = ifp->if_poll_cpuid;
237 if (poll_cpuid >= 0)
238 return poll_cpuid;
239 else
240 return ifp->if_cpuid;
242 #endif
244 static void
245 if_start_ipifunc(void *arg)
247 struct ifnet *ifp = arg;
248 struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].nm_lmsg;
250 crit_enter();
251 if (lmsg->ms_flags & MSGF_DONE)
252 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
253 crit_exit();
257 * Schedule ifnet.if_start on ifnet's CPU
259 static void
260 if_start_schedule(struct ifnet *ifp)
262 #ifdef SMP
263 int cpu;
265 if (if_start_oncpu_sched)
266 cpu = mycpuid;
267 else
268 cpu = ifp->if_start_cpuid(ifp);
270 if (cpu != mycpuid)
271 lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp);
272 else
273 #endif
274 if_start_ipifunc(ifp);
278 * NOTE:
279 * This function will release ifnet.if_start interlock,
280 * if ifnet.if_start does not need to be scheduled
282 static __inline int
283 if_start_need_schedule(struct ifaltq *ifq, int running)
285 if (!running || ifq_is_empty(ifq)
286 #ifdef ALTQ
287 || ifq->altq_tbr != NULL
288 #endif
290 ALTQ_LOCK(ifq);
292 * ifnet.if_start interlock is released, if:
293 * 1) Hardware can not take any packets, due to
294 * o interface is marked down
295 * o hardware queue is full (IFF_OACTIVE)
296 * Under the second situation, hardware interrupt
297 * or polling(4) will call/schedule ifnet.if_start
298 * when hardware queue is ready
299 * 2) There is not packet in the ifnet.if_snd.
300 * Further ifq_dispatch or ifq_handoff will call/
301 * schedule ifnet.if_start
302 * 3) TBR is used and it does not allow further
303 * dequeueing.
304 * TBR callout will call ifnet.if_start
306 if (!running || !ifq_data_ready(ifq)) {
307 ifq->altq_started = 0;
308 ALTQ_UNLOCK(ifq);
309 return 0;
311 ALTQ_UNLOCK(ifq);
313 return 1;
316 static void
317 if_start_dispatch(struct netmsg *nmsg)
319 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
320 struct ifnet *ifp = lmsg->u.ms_resultp;
321 struct ifaltq *ifq = &ifp->if_snd;
322 int running = 0;
324 crit_enter();
325 lwkt_replymsg(lmsg, 0); /* reply ASAP */
326 crit_exit();
328 #ifdef SMP
329 if (!if_start_oncpu_sched && mycpuid != ifp->if_start_cpuid(ifp)) {
331 * If the ifnet is still up, we need to
332 * chase its CPU change.
334 if (ifp->if_flags & IFF_UP) {
335 logifstart(chase_sched, ifp);
336 if_start_schedule(ifp);
337 return;
338 } else {
339 goto check;
342 #endif
344 if (ifp->if_flags & IFF_UP) {
345 lwkt_serialize_enter(ifp->if_serializer); /* XXX try? */
346 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
347 logifstart(run, ifp);
348 ifp->if_start(ifp);
349 if ((ifp->if_flags &
350 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
351 running = 1;
353 lwkt_serialize_exit(ifp->if_serializer);
355 #ifdef SMP
356 check:
357 #endif
358 if (if_start_need_schedule(ifq, running)) {
359 crit_enter();
360 if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */
361 logifstart(sched, ifp);
362 lwkt_sendmsg(ifnet_portfn(mycpuid), lmsg);
364 crit_exit();
368 /* Device driver ifnet.if_start helper function */
369 void
370 if_devstart(struct ifnet *ifp)
372 struct ifaltq *ifq = &ifp->if_snd;
373 int running = 0;
375 ASSERT_SERIALIZED(ifp->if_serializer);
377 ALTQ_LOCK(ifq);
378 if (ifq->altq_started || !ifq_data_ready(ifq)) {
379 logifstart(avoid, ifp);
380 ALTQ_UNLOCK(ifq);
381 return;
383 ifq->altq_started = 1;
384 ALTQ_UNLOCK(ifq);
386 if (if_devstart_schedonly) {
388 * Always schedule ifnet.if_start on ifnet's CPU,
389 * short circuit the rest of this function.
391 logifstart(sched, ifp);
392 if_start_schedule(ifp);
393 return;
396 logifstart(run, ifp);
397 ifp->if_start(ifp);
399 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
400 running = 1;
402 if (if_devstart_schednochk || if_start_need_schedule(ifq, running)) {
404 * More data need to be transmitted, ifnet.if_start is
405 * scheduled on ifnet's CPU, and we keep going.
406 * NOTE: ifnet.if_start interlock is not released.
408 logifstart(sched, ifp);
409 if_start_schedule(ifp);
414 * Attach an interface to the list of "active" interfaces.
416 * The serializer is optional. If non-NULL access to the interface
417 * may be MPSAFE.
419 void
420 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
422 unsigned socksize, ifasize;
423 int namelen, masklen;
424 struct sockaddr_dl *sdl;
425 struct ifaddr *ifa;
426 struct ifaltq *ifq;
427 int i;
429 static int if_indexlim = 8;
432 * The serializer can be passed in from the device, allowing the
433 * same serializer to be used for both the interrupt interlock and
434 * the device queue. If not specified, the netif structure will
435 * use an embedded serializer.
437 if (serializer == NULL) {
438 serializer = &ifp->if_default_serializer;
439 lwkt_serialize_init(serializer);
441 ifp->if_serializer = serializer;
443 ifp->if_start_cpuid = if_start_cpuid;
444 ifp->if_cpuid = 0;
446 #ifdef DEVICE_POLLING
447 /* Device is not in polling mode by default */
448 ifp->if_poll_cpuid = -1;
449 if (ifp->if_poll != NULL)
450 ifp->if_start_cpuid = if_start_cpuid_poll;
451 #endif
453 ifp->if_start_nmsg = kmalloc(ncpus * sizeof(struct netmsg),
454 M_LWKTMSG, M_WAITOK);
455 for (i = 0; i < ncpus; ++i) {
456 netmsg_init(&ifp->if_start_nmsg[i], &netisr_adone_rport, 0,
457 if_start_dispatch);
458 ifp->if_start_nmsg[i].nm_lmsg.u.ms_resultp = ifp;
461 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
462 ifp->if_index = ++if_index;
465 * XXX -
466 * The old code would work if the interface passed a pre-existing
467 * chain of ifaddrs to this code. We don't trust our callers to
468 * properly initialize the tailq, however, so we no longer allow
469 * this unlikely case.
471 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
472 M_IFADDR, M_WAITOK | M_ZERO);
473 for (i = 0; i < ncpus; ++i)
474 TAILQ_INIT(&ifp->if_addrheads[i]);
476 TAILQ_INIT(&ifp->if_prefixhead);
477 LIST_INIT(&ifp->if_multiaddrs);
478 getmicrotime(&ifp->if_lastchange);
479 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
480 unsigned int n;
481 struct ifnet **q;
483 if_indexlim <<= 1;
485 /* grow ifindex2ifnet */
486 n = if_indexlim * sizeof(*q);
487 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
488 if (ifindex2ifnet) {
489 bcopy(ifindex2ifnet, q, n/2);
490 kfree(ifindex2ifnet, M_IFADDR);
492 ifindex2ifnet = q;
495 ifindex2ifnet[if_index] = ifp;
498 * create a Link Level name for this device
500 namelen = strlen(ifp->if_xname);
501 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
502 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
503 socksize = masklen + ifp->if_addrlen;
504 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
505 if (socksize < sizeof(*sdl))
506 socksize = sizeof(*sdl);
507 socksize = ROUNDUP(socksize);
508 ifasize = sizeof(struct ifaddr) + 2 * socksize;
509 ifa = ifa_create(ifasize, M_WAITOK);
510 sdl = (struct sockaddr_dl *)(ifa + 1);
511 sdl->sdl_len = socksize;
512 sdl->sdl_family = AF_LINK;
513 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
514 sdl->sdl_nlen = namelen;
515 sdl->sdl_index = ifp->if_index;
516 sdl->sdl_type = ifp->if_type;
517 ifp->if_lladdr = ifa;
518 ifa->ifa_ifp = ifp;
519 ifa->ifa_rtrequest = link_rtrequest;
520 ifa->ifa_addr = (struct sockaddr *)sdl;
521 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
522 ifa->ifa_netmask = (struct sockaddr *)sdl;
523 sdl->sdl_len = masklen;
524 while (namelen != 0)
525 sdl->sdl_data[--namelen] = 0xff;
526 ifa_iflink(ifa, ifp, 0 /* Insert head */);
528 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
529 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
531 ifq = &ifp->if_snd;
532 ifq->altq_type = 0;
533 ifq->altq_disc = NULL;
534 ifq->altq_flags &= ALTQF_CANTCHANGE;
535 ifq->altq_tbr = NULL;
536 ifq->altq_ifp = ifp;
537 ifq->altq_started = 0;
538 ifq->altq_prepended = NULL;
539 ALTQ_LOCK_INIT(ifq);
540 ifq_set_classic(ifq);
542 if (!SLIST_EMPTY(&domains))
543 if_attachdomain1(ifp);
545 /* Announce the interface. */
546 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
549 static void
550 if_attachdomain(void *dummy)
552 struct ifnet *ifp;
554 crit_enter();
555 TAILQ_FOREACH(ifp, &ifnet, if_list)
556 if_attachdomain1(ifp);
557 crit_exit();
559 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
560 if_attachdomain, NULL);
562 static void
563 if_attachdomain1(struct ifnet *ifp)
565 struct domain *dp;
567 crit_enter();
569 /* address family dependent data region */
570 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
571 SLIST_FOREACH(dp, &domains, dom_next)
572 if (dp->dom_ifattach)
573 ifp->if_afdata[dp->dom_family] =
574 (*dp->dom_ifattach)(ifp);
575 crit_exit();
579 * Purge all addresses whose type is _not_ AF_LINK
581 void
582 if_purgeaddrs_nolink(struct ifnet *ifp)
584 struct ifaddr_container *ifac, *next;
586 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
587 ifa_link, next) {
588 struct ifaddr *ifa = ifac->ifa;
590 /* Leave link ifaddr as it is */
591 if (ifa->ifa_addr->sa_family == AF_LINK)
592 continue;
593 #ifdef INET
594 /* XXX: Ugly!! ad hoc just for INET */
595 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
596 struct ifaliasreq ifr;
597 #ifdef IFADDR_DEBUG_VERBOSE
598 int i;
600 kprintf("purge in4 addr %p: ", ifa);
601 for (i = 0; i < ncpus; ++i)
602 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
603 kprintf("\n");
604 #endif
606 bzero(&ifr, sizeof ifr);
607 ifr.ifra_addr = *ifa->ifa_addr;
608 if (ifa->ifa_dstaddr)
609 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
610 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
611 NULL) == 0)
612 continue;
614 #endif /* INET */
615 #ifdef INET6
616 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
617 #ifdef IFADDR_DEBUG_VERBOSE
618 int i;
620 kprintf("purge in6 addr %p: ", ifa);
621 for (i = 0; i < ncpus; ++i)
622 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
623 kprintf("\n");
624 #endif
626 in6_purgeaddr(ifa);
627 /* ifp_addrhead is already updated */
628 continue;
630 #endif /* INET6 */
631 ifa_ifunlink(ifa, ifp);
632 ifa_destroy(ifa);
637 * Detach an interface, removing it from the
638 * list of "active" interfaces.
640 void
641 if_detach(struct ifnet *ifp)
643 struct radix_node_head *rnh;
644 int i;
645 int cpu, origcpu;
646 struct domain *dp;
648 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
651 * Remove routes and flush queues.
653 crit_enter();
654 #ifdef DEVICE_POLLING
655 if (ifp->if_flags & IFF_POLLING)
656 ether_poll_deregister(ifp);
657 #endif
658 if_down(ifp);
660 if (ifq_is_enabled(&ifp->if_snd))
661 altq_disable(&ifp->if_snd);
662 if (ifq_is_attached(&ifp->if_snd))
663 altq_detach(&ifp->if_snd);
666 * Clean up all addresses.
668 ifp->if_lladdr = NULL;
670 if_purgeaddrs_nolink(ifp);
671 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
672 struct ifaddr *ifa;
674 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
675 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
676 ("non-link ifaddr is left on if_addrheads"));
678 ifa_ifunlink(ifa, ifp);
679 ifa_destroy(ifa);
680 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
681 ("there are still ifaddrs left on if_addrheads"));
684 #ifdef INET
686 * Remove all IPv4 kernel structures related to ifp.
688 in_ifdetach(ifp);
689 #endif
691 #ifdef INET6
693 * Remove all IPv6 kernel structs related to ifp. This should be done
694 * before removing routing entries below, since IPv6 interface direct
695 * routes are expected to be removed by the IPv6-specific kernel API.
696 * Otherwise, the kernel will detect some inconsistency and bark it.
698 in6_ifdetach(ifp);
699 #endif
702 * Delete all remaining routes using this interface
703 * Unfortuneatly the only way to do this is to slog through
704 * the entire routing table looking for routes which point
705 * to this interface...oh well...
707 origcpu = mycpuid;
708 for (cpu = 0; cpu < ncpus2; cpu++) {
709 lwkt_migratecpu(cpu);
710 for (i = 1; i <= AF_MAX; i++) {
711 if ((rnh = rt_tables[cpu][i]) == NULL)
712 continue;
713 rnh->rnh_walktree(rnh, if_rtdel, ifp);
716 lwkt_migratecpu(origcpu);
718 /* Announce that the interface is gone. */
719 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
720 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
722 SLIST_FOREACH(dp, &domains, dom_next)
723 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
724 (*dp->dom_ifdetach)(ifp,
725 ifp->if_afdata[dp->dom_family]);
728 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
730 ifindex2ifnet[ifp->if_index] = NULL;
731 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
732 if_index--;
734 TAILQ_REMOVE(&ifnet, ifp, if_link);
735 kfree(ifp->if_addrheads, M_IFADDR);
736 kfree(ifp->if_start_nmsg, M_LWKTMSG);
737 crit_exit();
741 * Delete Routes for a Network Interface
743 * Called for each routing entry via the rnh->rnh_walktree() call above
744 * to delete all route entries referencing a detaching network interface.
746 * Arguments:
747 * rn pointer to node in the routing table
748 * arg argument passed to rnh->rnh_walktree() - detaching interface
750 * Returns:
751 * 0 successful
752 * errno failed - reason indicated
755 static int
756 if_rtdel(struct radix_node *rn, void *arg)
758 struct rtentry *rt = (struct rtentry *)rn;
759 struct ifnet *ifp = arg;
760 int err;
762 if (rt->rt_ifp == ifp) {
765 * Protect (sorta) against walktree recursion problems
766 * with cloned routes
768 if (!(rt->rt_flags & RTF_UP))
769 return (0);
771 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
772 rt_mask(rt), rt->rt_flags,
773 (struct rtentry **) NULL);
774 if (err) {
775 log(LOG_WARNING, "if_rtdel: error %d\n", err);
779 return (0);
783 * Locate an interface based on a complete address.
785 struct ifaddr *
786 ifa_ifwithaddr(struct sockaddr *addr)
788 struct ifnet *ifp;
790 TAILQ_FOREACH(ifp, &ifnet, if_link) {
791 struct ifaddr_container *ifac;
793 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
794 struct ifaddr *ifa = ifac->ifa;
796 if (ifa->ifa_addr->sa_family != addr->sa_family)
797 continue;
798 if (sa_equal(addr, ifa->ifa_addr))
799 return (ifa);
800 if ((ifp->if_flags & IFF_BROADCAST) &&
801 ifa->ifa_broadaddr &&
802 /* IPv6 doesn't have broadcast */
803 ifa->ifa_broadaddr->sa_len != 0 &&
804 sa_equal(ifa->ifa_broadaddr, addr))
805 return (ifa);
808 return (NULL);
811 * Locate the point to point interface with a given destination address.
813 struct ifaddr *
814 ifa_ifwithdstaddr(struct sockaddr *addr)
816 struct ifnet *ifp;
818 TAILQ_FOREACH(ifp, &ifnet, if_link) {
819 struct ifaddr_container *ifac;
821 if (!(ifp->if_flags & IFF_POINTOPOINT))
822 continue;
824 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
825 struct ifaddr *ifa = ifac->ifa;
827 if (ifa->ifa_addr->sa_family != addr->sa_family)
828 continue;
829 if (ifa->ifa_dstaddr &&
830 sa_equal(addr, ifa->ifa_dstaddr))
831 return (ifa);
834 return (NULL);
838 * Find an interface on a specific network. If many, choice
839 * is most specific found.
841 struct ifaddr *
842 ifa_ifwithnet(struct sockaddr *addr)
844 struct ifnet *ifp;
845 struct ifaddr *ifa_maybe = NULL;
846 u_int af = addr->sa_family;
847 char *addr_data = addr->sa_data, *cplim;
850 * AF_LINK addresses can be looked up directly by their index number,
851 * so do that if we can.
853 if (af == AF_LINK) {
854 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
856 if (sdl->sdl_index && sdl->sdl_index <= if_index)
857 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
861 * Scan though each interface, looking for ones that have
862 * addresses in this address family.
864 TAILQ_FOREACH(ifp, &ifnet, if_link) {
865 struct ifaddr_container *ifac;
867 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
868 struct ifaddr *ifa = ifac->ifa;
869 char *cp, *cp2, *cp3;
871 if (ifa->ifa_addr->sa_family != af)
872 next: continue;
873 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
875 * This is a bit broken as it doesn't
876 * take into account that the remote end may
877 * be a single node in the network we are
878 * looking for.
879 * The trouble is that we don't know the
880 * netmask for the remote end.
882 if (ifa->ifa_dstaddr != NULL &&
883 sa_equal(addr, ifa->ifa_dstaddr))
884 return (ifa);
885 } else {
887 * if we have a special address handler,
888 * then use it instead of the generic one.
890 if (ifa->ifa_claim_addr) {
891 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
892 return (ifa);
893 } else {
894 continue;
899 * Scan all the bits in the ifa's address.
900 * If a bit dissagrees with what we are
901 * looking for, mask it with the netmask
902 * to see if it really matters.
903 * (A byte at a time)
905 if (ifa->ifa_netmask == 0)
906 continue;
907 cp = addr_data;
908 cp2 = ifa->ifa_addr->sa_data;
909 cp3 = ifa->ifa_netmask->sa_data;
910 cplim = ifa->ifa_netmask->sa_len +
911 (char *)ifa->ifa_netmask;
912 while (cp3 < cplim)
913 if ((*cp++ ^ *cp2++) & *cp3++)
914 goto next; /* next address! */
916 * If the netmask of what we just found
917 * is more specific than what we had before
918 * (if we had one) then remember the new one
919 * before continuing to search
920 * for an even better one.
922 if (ifa_maybe == 0 ||
923 rn_refines((char *)ifa->ifa_netmask,
924 (char *)ifa_maybe->ifa_netmask))
925 ifa_maybe = ifa;
929 return (ifa_maybe);
933 * Find an interface address specific to an interface best matching
934 * a given address.
936 struct ifaddr *
937 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
939 struct ifaddr_container *ifac;
940 char *cp, *cp2, *cp3;
941 char *cplim;
942 struct ifaddr *ifa_maybe = 0;
943 u_int af = addr->sa_family;
945 if (af >= AF_MAX)
946 return (0);
947 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
948 struct ifaddr *ifa = ifac->ifa;
950 if (ifa->ifa_addr->sa_family != af)
951 continue;
952 if (ifa_maybe == 0)
953 ifa_maybe = ifa;
954 if (ifa->ifa_netmask == NULL) {
955 if (sa_equal(addr, ifa->ifa_addr) ||
956 (ifa->ifa_dstaddr != NULL &&
957 sa_equal(addr, ifa->ifa_dstaddr)))
958 return (ifa);
959 continue;
961 if (ifp->if_flags & IFF_POINTOPOINT) {
962 if (sa_equal(addr, ifa->ifa_dstaddr))
963 return (ifa);
964 } else {
965 cp = addr->sa_data;
966 cp2 = ifa->ifa_addr->sa_data;
967 cp3 = ifa->ifa_netmask->sa_data;
968 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
969 for (; cp3 < cplim; cp3++)
970 if ((*cp++ ^ *cp2++) & *cp3)
971 break;
972 if (cp3 == cplim)
973 return (ifa);
976 return (ifa_maybe);
980 * Default action when installing a route with a Link Level gateway.
981 * Lookup an appropriate real ifa to point to.
982 * This should be moved to /sys/net/link.c eventually.
984 static void
985 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
987 struct ifaddr *ifa;
988 struct sockaddr *dst;
989 struct ifnet *ifp;
991 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
992 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
993 return;
994 ifa = ifaof_ifpforaddr(dst, ifp);
995 if (ifa != NULL) {
996 IFAFREE(rt->rt_ifa);
997 IFAREF(ifa);
998 rt->rt_ifa = ifa;
999 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1000 ifa->ifa_rtrequest(cmd, rt, info);
1005 * Mark an interface down and notify protocols of
1006 * the transition.
1007 * NOTE: must be called at splnet or eqivalent.
1009 void
1010 if_unroute(struct ifnet *ifp, int flag, int fam)
1012 struct ifaddr_container *ifac;
1014 ifp->if_flags &= ~flag;
1015 getmicrotime(&ifp->if_lastchange);
1016 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1017 struct ifaddr *ifa = ifac->ifa;
1019 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1020 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1022 ifq_purge(&ifp->if_snd);
1023 rt_ifmsg(ifp);
1027 * Mark an interface up and notify protocols of
1028 * the transition.
1029 * NOTE: must be called at splnet or eqivalent.
1031 void
1032 if_route(struct ifnet *ifp, int flag, int fam)
1034 struct ifaddr_container *ifac;
1036 ifq_purge(&ifp->if_snd);
1037 ifp->if_flags |= flag;
1038 getmicrotime(&ifp->if_lastchange);
1039 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1040 struct ifaddr *ifa = ifac->ifa;
1042 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1043 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1045 rt_ifmsg(ifp);
1046 #ifdef INET6
1047 in6_if_up(ifp);
1048 #endif
1052 * Mark an interface down and notify protocols of the transition. An
1053 * interface going down is also considered to be a synchronizing event.
1054 * We must ensure that all packet processing related to the interface
1055 * has completed before we return so e.g. the caller can free the ifnet
1056 * structure that the mbufs may be referencing.
1058 * NOTE: must be called at splnet or eqivalent.
1060 void
1061 if_down(struct ifnet *ifp)
1063 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1064 netmsg_service_sync();
1068 * Mark an interface up and notify protocols of
1069 * the transition.
1070 * NOTE: must be called at splnet or eqivalent.
1072 void
1073 if_up(struct ifnet *ifp)
1075 if_route(ifp, IFF_UP, AF_UNSPEC);
1079 * Process a link state change.
1080 * NOTE: must be called at splsoftnet or equivalent.
1082 void
1083 if_link_state_change(struct ifnet *ifp)
1085 int link_state = ifp->if_link_state;
1087 rt_ifmsg(ifp);
1088 devctl_notify("IFNET", ifp->if_xname,
1089 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1093 * Handle interface watchdog timer routines. Called
1094 * from softclock, we decrement timers (if set) and
1095 * call the appropriate interface routine on expiration.
1097 static void
1098 if_slowtimo(void *arg)
1100 struct ifnet *ifp;
1102 crit_enter();
1104 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1105 if (ifp->if_timer == 0 || --ifp->if_timer)
1106 continue;
1107 if (ifp->if_watchdog) {
1108 if (lwkt_serialize_try(ifp->if_serializer)) {
1109 (*ifp->if_watchdog)(ifp);
1110 lwkt_serialize_exit(ifp->if_serializer);
1111 } else {
1112 /* try again next timeout */
1113 ++ifp->if_timer;
1118 crit_exit();
1120 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1124 * Map interface name to
1125 * interface structure pointer.
1127 struct ifnet *
1128 ifunit(const char *name)
1130 struct ifnet *ifp;
1133 * Search all the interfaces for this name/number
1136 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1137 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1138 break;
1140 return (ifp);
1145 * Map interface name in a sockaddr_dl to
1146 * interface structure pointer.
1148 struct ifnet *
1149 if_withname(struct sockaddr *sa)
1151 char ifname[IFNAMSIZ+1];
1152 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1154 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1155 (sdl->sdl_nlen > IFNAMSIZ) )
1156 return NULL;
1159 * ifunit wants a null-terminated name. It may not be null-terminated
1160 * in the sockaddr. We don't want to change the caller's sockaddr,
1161 * and there might not be room to put the trailing null anyway, so we
1162 * make a local copy that we know we can null terminate safely.
1165 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1166 ifname[sdl->sdl_nlen] = '\0';
1167 return ifunit(ifname);
1172 * Interface ioctls.
1175 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1177 struct ifnet *ifp;
1178 struct ifreq *ifr;
1179 struct ifstat *ifs;
1180 int error;
1181 short oif_flags;
1182 int new_flags;
1183 size_t namelen, onamelen;
1184 char new_name[IFNAMSIZ];
1185 struct ifaddr *ifa;
1186 struct sockaddr_dl *sdl;
1188 switch (cmd) {
1190 case SIOCGIFCONF:
1191 case OSIOCGIFCONF:
1192 return (ifconf(cmd, data, cred));
1194 ifr = (struct ifreq *)data;
1196 switch (cmd) {
1197 case SIOCIFCREATE:
1198 case SIOCIFDESTROY:
1199 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1200 return (error);
1201 return ((cmd == SIOCIFCREATE) ?
1202 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1203 if_clone_destroy(ifr->ifr_name));
1205 case SIOCIFGCLONERS:
1206 return (if_clone_list((struct if_clonereq *)data));
1209 ifp = ifunit(ifr->ifr_name);
1210 if (ifp == 0)
1211 return (ENXIO);
1212 switch (cmd) {
1214 case SIOCGIFFLAGS:
1215 ifr->ifr_flags = ifp->if_flags;
1216 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1217 break;
1219 case SIOCGIFCAP:
1220 ifr->ifr_reqcap = ifp->if_capabilities;
1221 ifr->ifr_curcap = ifp->if_capenable;
1222 break;
1224 case SIOCGIFMETRIC:
1225 ifr->ifr_metric = ifp->if_metric;
1226 break;
1228 case SIOCGIFMTU:
1229 ifr->ifr_mtu = ifp->if_mtu;
1230 break;
1232 case SIOCGIFPHYS:
1233 ifr->ifr_phys = ifp->if_physical;
1234 break;
1236 case SIOCGIFPOLLCPU:
1237 #ifdef DEVICE_POLLING
1238 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1239 #else
1240 ifr->ifr_pollcpu = -1;
1241 #endif
1242 break;
1244 case SIOCSIFPOLLCPU:
1245 #ifdef DEVICE_POLLING
1246 if ((ifp->if_flags & IFF_POLLING) == 0)
1247 ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1248 #endif
1249 break;
1251 case SIOCSIFFLAGS:
1252 error = priv_check_cred(cred, PRIV_ROOT, 0);
1253 if (error)
1254 return (error);
1255 new_flags = (ifr->ifr_flags & 0xffff) |
1256 (ifr->ifr_flagshigh << 16);
1257 if (ifp->if_flags & IFF_SMART) {
1258 /* Smart drivers twiddle their own routes */
1259 } else if (ifp->if_flags & IFF_UP &&
1260 (new_flags & IFF_UP) == 0) {
1261 crit_enter();
1262 if_down(ifp);
1263 crit_exit();
1264 } else if (new_flags & IFF_UP &&
1265 (ifp->if_flags & IFF_UP) == 0) {
1266 crit_enter();
1267 if_up(ifp);
1268 crit_exit();
1271 #ifdef DEVICE_POLLING
1272 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1273 if (new_flags & IFF_POLLING) {
1274 ether_poll_register(ifp);
1275 } else {
1276 ether_poll_deregister(ifp);
1279 #endif
1281 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1282 (new_flags &~ IFF_CANTCHANGE);
1283 if (new_flags & IFF_PPROMISC) {
1284 /* Permanently promiscuous mode requested */
1285 ifp->if_flags |= IFF_PROMISC;
1286 } else if (ifp->if_pcount == 0) {
1287 ifp->if_flags &= ~IFF_PROMISC;
1289 if (ifp->if_ioctl) {
1290 lwkt_serialize_enter(ifp->if_serializer);
1291 ifp->if_ioctl(ifp, cmd, data, cred);
1292 lwkt_serialize_exit(ifp->if_serializer);
1294 getmicrotime(&ifp->if_lastchange);
1295 break;
1297 case SIOCSIFCAP:
1298 error = priv_check_cred(cred, PRIV_ROOT, 0);
1299 if (error)
1300 return (error);
1301 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1302 return (EINVAL);
1303 lwkt_serialize_enter(ifp->if_serializer);
1304 ifp->if_ioctl(ifp, cmd, data, cred);
1305 lwkt_serialize_exit(ifp->if_serializer);
1306 break;
1308 case SIOCSIFNAME:
1309 error = priv_check_cred(cred, PRIV_ROOT, 0);
1310 if (error != 0)
1311 return (error);
1312 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1313 if (error != 0)
1314 return (error);
1315 if (new_name[0] == '\0')
1316 return (EINVAL);
1317 if (ifunit(new_name) != NULL)
1318 return (EEXIST);
1320 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1322 /* Announce the departure of the interface. */
1323 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1325 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1326 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1327 /* XXX IFA_LOCK(ifa); */
1328 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1329 namelen = strlen(new_name);
1330 onamelen = sdl->sdl_nlen;
1332 * Move the address if needed. This is safe because we
1333 * allocate space for a name of length IFNAMSIZ when we
1334 * create this in if_attach().
1336 if (namelen != onamelen) {
1337 bcopy(sdl->sdl_data + onamelen,
1338 sdl->sdl_data + namelen, sdl->sdl_alen);
1340 bcopy(new_name, sdl->sdl_data, namelen);
1341 sdl->sdl_nlen = namelen;
1342 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1343 bzero(sdl->sdl_data, onamelen);
1344 while (namelen != 0)
1345 sdl->sdl_data[--namelen] = 0xff;
1346 /* XXX IFA_UNLOCK(ifa) */
1348 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1350 /* Announce the return of the interface. */
1351 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1352 break;
1354 case SIOCSIFMETRIC:
1355 error = priv_check_cred(cred, PRIV_ROOT, 0);
1356 if (error)
1357 return (error);
1358 ifp->if_metric = ifr->ifr_metric;
1359 getmicrotime(&ifp->if_lastchange);
1360 break;
1362 case SIOCSIFPHYS:
1363 error = priv_check_cred(cred, PRIV_ROOT, 0);
1364 if (error)
1365 return error;
1366 if (!ifp->if_ioctl)
1367 return EOPNOTSUPP;
1368 lwkt_serialize_enter(ifp->if_serializer);
1369 error = ifp->if_ioctl(ifp, cmd, data, cred);
1370 lwkt_serialize_exit(ifp->if_serializer);
1371 if (error == 0)
1372 getmicrotime(&ifp->if_lastchange);
1373 return (error);
1375 case SIOCSIFMTU:
1377 u_long oldmtu = ifp->if_mtu;
1379 error = priv_check_cred(cred, PRIV_ROOT, 0);
1380 if (error)
1381 return (error);
1382 if (ifp->if_ioctl == NULL)
1383 return (EOPNOTSUPP);
1384 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1385 return (EINVAL);
1386 lwkt_serialize_enter(ifp->if_serializer);
1387 error = ifp->if_ioctl(ifp, cmd, data, cred);
1388 lwkt_serialize_exit(ifp->if_serializer);
1389 if (error == 0) {
1390 getmicrotime(&ifp->if_lastchange);
1391 rt_ifmsg(ifp);
1394 * If the link MTU changed, do network layer specific procedure.
1396 if (ifp->if_mtu != oldmtu) {
1397 #ifdef INET6
1398 nd6_setmtu(ifp);
1399 #endif
1401 return (error);
1404 case SIOCADDMULTI:
1405 case SIOCDELMULTI:
1406 error = priv_check_cred(cred, PRIV_ROOT, 0);
1407 if (error)
1408 return (error);
1410 /* Don't allow group membership on non-multicast interfaces. */
1411 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1412 return EOPNOTSUPP;
1414 /* Don't let users screw up protocols' entries. */
1415 if (ifr->ifr_addr.sa_family != AF_LINK)
1416 return EINVAL;
1418 if (cmd == SIOCADDMULTI) {
1419 struct ifmultiaddr *ifma;
1420 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1421 } else {
1422 error = if_delmulti(ifp, &ifr->ifr_addr);
1424 if (error == 0)
1425 getmicrotime(&ifp->if_lastchange);
1426 return error;
1428 case SIOCSIFPHYADDR:
1429 case SIOCDIFPHYADDR:
1430 #ifdef INET6
1431 case SIOCSIFPHYADDR_IN6:
1432 #endif
1433 case SIOCSLIFPHYADDR:
1434 case SIOCSIFMEDIA:
1435 case SIOCSIFGENERIC:
1436 error = priv_check_cred(cred, PRIV_ROOT, 0);
1437 if (error)
1438 return (error);
1439 if (ifp->if_ioctl == 0)
1440 return (EOPNOTSUPP);
1441 lwkt_serialize_enter(ifp->if_serializer);
1442 error = ifp->if_ioctl(ifp, cmd, data, cred);
1443 lwkt_serialize_exit(ifp->if_serializer);
1444 if (error == 0)
1445 getmicrotime(&ifp->if_lastchange);
1446 return error;
1448 case SIOCGIFSTATUS:
1449 ifs = (struct ifstat *)data;
1450 ifs->ascii[0] = '\0';
1452 case SIOCGIFPSRCADDR:
1453 case SIOCGIFPDSTADDR:
1454 case SIOCGLIFPHYADDR:
1455 case SIOCGIFMEDIA:
1456 case SIOCGIFGENERIC:
1457 if (ifp->if_ioctl == NULL)
1458 return (EOPNOTSUPP);
1459 lwkt_serialize_enter(ifp->if_serializer);
1460 error = ifp->if_ioctl(ifp, cmd, data, cred);
1461 lwkt_serialize_exit(ifp->if_serializer);
1462 return (error);
1464 case SIOCSIFLLADDR:
1465 error = priv_check_cred(cred, PRIV_ROOT, 0);
1466 if (error)
1467 return (error);
1468 return if_setlladdr(ifp,
1469 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1471 default:
1472 oif_flags = ifp->if_flags;
1473 if (so->so_proto == 0)
1474 return (EOPNOTSUPP);
1475 #ifndef COMPAT_43
1476 error = so_pru_control(so, cmd, data, ifp);
1477 #else
1479 int ocmd = cmd;
1481 switch (cmd) {
1483 case SIOCSIFDSTADDR:
1484 case SIOCSIFADDR:
1485 case SIOCSIFBRDADDR:
1486 case SIOCSIFNETMASK:
1487 #if BYTE_ORDER != BIG_ENDIAN
1488 if (ifr->ifr_addr.sa_family == 0 &&
1489 ifr->ifr_addr.sa_len < 16) {
1490 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1491 ifr->ifr_addr.sa_len = 16;
1493 #else
1494 if (ifr->ifr_addr.sa_len == 0)
1495 ifr->ifr_addr.sa_len = 16;
1496 #endif
1497 break;
1499 case OSIOCGIFADDR:
1500 cmd = SIOCGIFADDR;
1501 break;
1503 case OSIOCGIFDSTADDR:
1504 cmd = SIOCGIFDSTADDR;
1505 break;
1507 case OSIOCGIFBRDADDR:
1508 cmd = SIOCGIFBRDADDR;
1509 break;
1511 case OSIOCGIFNETMASK:
1512 cmd = SIOCGIFNETMASK;
1514 error = so_pru_control(so, cmd, data, ifp);
1515 switch (ocmd) {
1517 case OSIOCGIFADDR:
1518 case OSIOCGIFDSTADDR:
1519 case OSIOCGIFBRDADDR:
1520 case OSIOCGIFNETMASK:
1521 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1525 #endif /* COMPAT_43 */
1527 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1528 #ifdef INET6
1529 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1530 if (ifp->if_flags & IFF_UP) {
1531 crit_enter();
1532 in6_if_up(ifp);
1533 crit_exit();
1535 #endif
1537 return (error);
1540 return (0);
1544 * Set/clear promiscuous mode on interface ifp based on the truth value
1545 * of pswitch. The calls are reference counted so that only the first
1546 * "on" request actually has an effect, as does the final "off" request.
1547 * Results are undefined if the "off" and "on" requests are not matched.
1550 ifpromisc(struct ifnet *ifp, int pswitch)
1552 struct ifreq ifr;
1553 int error;
1554 int oldflags;
1556 oldflags = ifp->if_flags;
1557 if (ifp->if_flags & IFF_PPROMISC) {
1558 /* Do nothing if device is in permanently promiscuous mode */
1559 ifp->if_pcount += pswitch ? 1 : -1;
1560 return (0);
1562 if (pswitch) {
1564 * If the device is not configured up, we cannot put it in
1565 * promiscuous mode.
1567 if ((ifp->if_flags & IFF_UP) == 0)
1568 return (ENETDOWN);
1569 if (ifp->if_pcount++ != 0)
1570 return (0);
1571 ifp->if_flags |= IFF_PROMISC;
1572 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1573 ifp->if_xname);
1574 } else {
1575 if (--ifp->if_pcount > 0)
1576 return (0);
1577 ifp->if_flags &= ~IFF_PROMISC;
1578 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1579 ifp->if_xname);
1581 ifr.ifr_flags = ifp->if_flags;
1582 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1583 lwkt_serialize_enter(ifp->if_serializer);
1584 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1585 (struct ucred *)NULL);
1586 lwkt_serialize_exit(ifp->if_serializer);
1587 if (error == 0)
1588 rt_ifmsg(ifp);
1589 else
1590 ifp->if_flags = oldflags;
1591 return error;
1595 * Return interface configuration
1596 * of system. List may be used
1597 * in later ioctl's (above) to get
1598 * other information.
1600 static int
1601 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1603 struct ifconf *ifc = (struct ifconf *)data;
1604 struct ifnet *ifp;
1605 struct sockaddr *sa;
1606 struct ifreq ifr, *ifrp;
1607 int space = ifc->ifc_len, error = 0;
1609 ifrp = ifc->ifc_req;
1610 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1611 struct ifaddr_container *ifac;
1612 int addrs;
1614 if (space <= sizeof ifr)
1615 break;
1618 * Zero the stack declared structure first to prevent
1619 * memory disclosure.
1621 bzero(&ifr, sizeof(ifr));
1622 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1623 >= sizeof(ifr.ifr_name)) {
1624 error = ENAMETOOLONG;
1625 break;
1628 addrs = 0;
1629 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1630 struct ifaddr *ifa = ifac->ifa;
1632 if (space <= sizeof ifr)
1633 break;
1634 sa = ifa->ifa_addr;
1635 if (cred->cr_prison &&
1636 prison_if(cred, sa))
1637 continue;
1638 addrs++;
1639 #ifdef COMPAT_43
1640 if (cmd == OSIOCGIFCONF) {
1641 struct osockaddr *osa =
1642 (struct osockaddr *)&ifr.ifr_addr;
1643 ifr.ifr_addr = *sa;
1644 osa->sa_family = sa->sa_family;
1645 error = copyout(&ifr, ifrp, sizeof ifr);
1646 ifrp++;
1647 } else
1648 #endif
1649 if (sa->sa_len <= sizeof(*sa)) {
1650 ifr.ifr_addr = *sa;
1651 error = copyout(&ifr, ifrp, sizeof ifr);
1652 ifrp++;
1653 } else {
1654 if (space < (sizeof ifr) + sa->sa_len -
1655 sizeof(*sa))
1656 break;
1657 space -= sa->sa_len - sizeof(*sa);
1658 error = copyout(&ifr, ifrp,
1659 sizeof ifr.ifr_name);
1660 if (error == 0)
1661 error = copyout(sa, &ifrp->ifr_addr,
1662 sa->sa_len);
1663 ifrp = (struct ifreq *)
1664 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1666 if (error)
1667 break;
1668 space -= sizeof ifr;
1670 if (error)
1671 break;
1672 if (!addrs) {
1673 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1674 error = copyout(&ifr, ifrp, sizeof ifr);
1675 if (error)
1676 break;
1677 space -= sizeof ifr;
1678 ifrp++;
1681 ifc->ifc_len -= space;
1682 return (error);
1686 * Just like if_promisc(), but for all-multicast-reception mode.
1689 if_allmulti(struct ifnet *ifp, int onswitch)
1691 int error = 0;
1692 struct ifreq ifr;
1694 crit_enter();
1696 if (onswitch) {
1697 if (ifp->if_amcount++ == 0) {
1698 ifp->if_flags |= IFF_ALLMULTI;
1699 ifr.ifr_flags = ifp->if_flags;
1700 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1701 lwkt_serialize_enter(ifp->if_serializer);
1702 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1703 (struct ucred *)NULL);
1704 lwkt_serialize_exit(ifp->if_serializer);
1706 } else {
1707 if (ifp->if_amcount > 1) {
1708 ifp->if_amcount--;
1709 } else {
1710 ifp->if_amcount = 0;
1711 ifp->if_flags &= ~IFF_ALLMULTI;
1712 ifr.ifr_flags = ifp->if_flags;
1713 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1714 lwkt_serialize_enter(ifp->if_serializer);
1715 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1716 (struct ucred *)NULL);
1717 lwkt_serialize_exit(ifp->if_serializer);
1721 crit_exit();
1723 if (error == 0)
1724 rt_ifmsg(ifp);
1725 return error;
1729 * Add a multicast listenership to the interface in question.
1730 * The link layer provides a routine which converts
1733 if_addmulti(
1734 struct ifnet *ifp, /* interface to manipulate */
1735 struct sockaddr *sa, /* address to add */
1736 struct ifmultiaddr **retifma)
1738 struct sockaddr *llsa, *dupsa;
1739 int error;
1740 struct ifmultiaddr *ifma;
1743 * If the matching multicast address already exists
1744 * then don't add a new one, just add a reference
1746 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1747 if (sa_equal(sa, ifma->ifma_addr)) {
1748 ifma->ifma_refcount++;
1749 if (retifma)
1750 *retifma = ifma;
1751 return 0;
1756 * Give the link layer a chance to accept/reject it, and also
1757 * find out which AF_LINK address this maps to, if it isn't one
1758 * already.
1760 if (ifp->if_resolvemulti) {
1761 lwkt_serialize_enter(ifp->if_serializer);
1762 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1763 lwkt_serialize_exit(ifp->if_serializer);
1764 if (error)
1765 return error;
1766 } else {
1767 llsa = 0;
1770 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1771 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1772 bcopy(sa, dupsa, sa->sa_len);
1774 ifma->ifma_addr = dupsa;
1775 ifma->ifma_lladdr = llsa;
1776 ifma->ifma_ifp = ifp;
1777 ifma->ifma_refcount = 1;
1778 ifma->ifma_protospec = 0;
1779 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1782 * Some network interfaces can scan the address list at
1783 * interrupt time; lock them out.
1785 crit_enter();
1786 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1787 crit_exit();
1788 *retifma = ifma;
1790 if (llsa != 0) {
1791 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1792 if (sa_equal(ifma->ifma_addr, llsa))
1793 break;
1795 if (ifma) {
1796 ifma->ifma_refcount++;
1797 } else {
1798 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1799 M_IFMADDR, M_WAITOK);
1800 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1801 M_IFMADDR, M_WAITOK);
1802 bcopy(llsa, dupsa, llsa->sa_len);
1803 ifma->ifma_addr = dupsa;
1804 ifma->ifma_ifp = ifp;
1805 ifma->ifma_refcount = 1;
1806 crit_enter();
1807 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1808 crit_exit();
1812 * We are certain we have added something, so call down to the
1813 * interface to let them know about it.
1815 crit_enter();
1816 lwkt_serialize_enter(ifp->if_serializer);
1817 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
1818 lwkt_serialize_exit(ifp->if_serializer);
1819 crit_exit();
1821 return 0;
1825 * Remove a reference to a multicast address on this interface. Yell
1826 * if the request does not match an existing membership.
1829 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1831 struct ifmultiaddr *ifma;
1833 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1834 if (sa_equal(sa, ifma->ifma_addr))
1835 break;
1836 if (ifma == 0)
1837 return ENOENT;
1839 if (ifma->ifma_refcount > 1) {
1840 ifma->ifma_refcount--;
1841 return 0;
1844 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1845 sa = ifma->ifma_lladdr;
1846 crit_enter();
1847 LIST_REMOVE(ifma, ifma_link);
1849 * Make sure the interface driver is notified
1850 * in the case of a link layer mcast group being left.
1852 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1853 lwkt_serialize_enter(ifp->if_serializer);
1854 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1855 lwkt_serialize_exit(ifp->if_serializer);
1857 crit_exit();
1858 kfree(ifma->ifma_addr, M_IFMADDR);
1859 kfree(ifma, M_IFMADDR);
1860 if (sa == 0)
1861 return 0;
1864 * Now look for the link-layer address which corresponds to
1865 * this network address. It had been squirreled away in
1866 * ifma->ifma_lladdr for this purpose (so we don't have
1867 * to call ifp->if_resolvemulti() again), and we saved that
1868 * value in sa above. If some nasty deleted the
1869 * link-layer address out from underneath us, we can deal because
1870 * the address we stored was is not the same as the one which was
1871 * in the record for the link-layer address. (So we don't complain
1872 * in that case.)
1874 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1875 if (sa_equal(sa, ifma->ifma_addr))
1876 break;
1877 if (ifma == 0)
1878 return 0;
1880 if (ifma->ifma_refcount > 1) {
1881 ifma->ifma_refcount--;
1882 return 0;
1885 crit_enter();
1886 lwkt_serialize_enter(ifp->if_serializer);
1887 LIST_REMOVE(ifma, ifma_link);
1888 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
1889 lwkt_serialize_exit(ifp->if_serializer);
1890 crit_exit();
1891 kfree(ifma->ifma_addr, M_IFMADDR);
1892 kfree(sa, M_IFMADDR);
1893 kfree(ifma, M_IFMADDR);
1895 return 0;
1899 * Set the link layer address on an interface.
1901 * At this time we only support certain types of interfaces,
1902 * and we don't allow the length of the address to change.
1905 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1907 struct sockaddr_dl *sdl;
1908 struct ifreq ifr;
1910 sdl = IF_LLSOCKADDR(ifp);
1911 if (sdl == NULL)
1912 return (EINVAL);
1913 if (len != sdl->sdl_alen) /* don't allow length to change */
1914 return (EINVAL);
1915 switch (ifp->if_type) {
1916 case IFT_ETHER: /* these types use struct arpcom */
1917 case IFT_XETHER:
1918 case IFT_L2VLAN:
1919 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1920 bcopy(lladdr, LLADDR(sdl), len);
1921 break;
1922 default:
1923 return (ENODEV);
1926 * If the interface is already up, we need
1927 * to re-init it in order to reprogram its
1928 * address filter.
1930 lwkt_serialize_enter(ifp->if_serializer);
1931 if ((ifp->if_flags & IFF_UP) != 0) {
1932 struct ifaddr_container *ifac;
1934 ifp->if_flags &= ~IFF_UP;
1935 ifr.ifr_flags = ifp->if_flags;
1936 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1937 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1938 (struct ucred *)NULL);
1939 ifp->if_flags |= IFF_UP;
1940 ifr.ifr_flags = ifp->if_flags;
1941 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1942 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1943 (struct ucred *)NULL);
1944 #ifdef INET
1946 * Also send gratuitous ARPs to notify other nodes about
1947 * the address change.
1949 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1950 struct ifaddr *ifa = ifac->ifa;
1952 if (ifa->ifa_addr != NULL &&
1953 ifa->ifa_addr->sa_family == AF_INET)
1954 arp_ifinit(ifp, ifa);
1956 #endif
1958 lwkt_serialize_exit(ifp->if_serializer);
1959 return (0);
1962 struct ifmultiaddr *
1963 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
1965 struct ifmultiaddr *ifma;
1967 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1968 if (sa_equal(ifma->ifma_addr, sa))
1969 break;
1971 return ifma;
1975 * This function locates the first real ethernet MAC from a network
1976 * card and loads it into node, returning 0 on success or ENOENT if
1977 * no suitable interfaces were found. It is used by the uuid code to
1978 * generate a unique 6-byte number.
1981 if_getanyethermac(uint16_t *node, int minlen)
1983 struct ifnet *ifp;
1984 struct sockaddr_dl *sdl;
1986 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1987 if (ifp->if_type != IFT_ETHER)
1988 continue;
1989 sdl = IF_LLSOCKADDR(ifp);
1990 if (sdl->sdl_alen < minlen)
1991 continue;
1992 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
1993 minlen);
1994 return(0);
1996 return (ENOENT);
2000 * The name argument must be a pointer to storage which will last as
2001 * long as the interface does. For physical devices, the result of
2002 * device_get_name(dev) is a good choice and for pseudo-devices a
2003 * static string works well.
2005 void
2006 if_initname(struct ifnet *ifp, const char *name, int unit)
2008 ifp->if_dname = name;
2009 ifp->if_dunit = unit;
2010 if (unit != IF_DUNIT_NONE)
2011 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2012 else
2013 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2017 if_printf(struct ifnet *ifp, const char *fmt, ...)
2019 __va_list ap;
2020 int retval;
2022 retval = kprintf("%s: ", ifp->if_xname);
2023 __va_start(ap, fmt);
2024 retval += kvprintf(fmt, ap);
2025 __va_end(ap);
2026 return (retval);
2029 void
2030 ifq_set_classic(struct ifaltq *ifq)
2032 ifq->altq_enqueue = ifq_classic_enqueue;
2033 ifq->altq_dequeue = ifq_classic_dequeue;
2034 ifq->altq_request = ifq_classic_request;
2038 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
2039 struct altq_pktattr *pa __unused)
2041 logifq(enqueue, ifq);
2042 if (IF_QFULL(ifq)) {
2043 m_freem(m);
2044 return(ENOBUFS);
2045 } else {
2046 IF_ENQUEUE(ifq, m);
2047 return(0);
2051 struct mbuf *
2052 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
2054 struct mbuf *m;
2056 switch (op) {
2057 case ALTDQ_POLL:
2058 IF_POLL(ifq, m);
2059 break;
2060 case ALTDQ_REMOVE:
2061 logifq(dequeue, ifq);
2062 IF_DEQUEUE(ifq, m);
2063 break;
2064 default:
2065 panic("unsupported ALTQ dequeue op: %d", op);
2067 KKASSERT(mpolled == NULL || mpolled == m);
2068 return(m);
2072 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
2074 switch (req) {
2075 case ALTRQ_PURGE:
2076 IF_DRAIN(ifq);
2077 break;
2078 default:
2079 panic("unsupported ALTQ request: %d", req);
2081 return(0);
2085 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2087 struct ifaltq *ifq = &ifp->if_snd;
2088 int running = 0, error, start = 0;
2090 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
2092 ALTQ_LOCK(ifq);
2093 error = ifq_enqueue_locked(ifq, m, pa);
2094 if (error) {
2095 ALTQ_UNLOCK(ifq);
2096 return error;
2098 if (!ifq->altq_started) {
2100 * Hold the interlock of ifnet.if_start
2102 ifq->altq_started = 1;
2103 start = 1;
2105 ALTQ_UNLOCK(ifq);
2107 ifp->if_obytes += m->m_pkthdr.len;
2108 if (m->m_flags & M_MCAST)
2109 ifp->if_omcasts++;
2111 if (!start) {
2112 logifstart(avoid, ifp);
2113 return 0;
2116 if (ifq_dispatch_schedonly) {
2118 * Always schedule ifnet.if_start on ifnet's CPU,
2119 * short circuit the rest of this function.
2121 logifstart(sched, ifp);
2122 if_start_schedule(ifp);
2123 return 0;
2127 * Try to do direct ifnet.if_start first, if there is
2128 * contention on ifnet's serializer, ifnet.if_start will
2129 * be scheduled on ifnet's CPU.
2131 if (!lwkt_serialize_try(ifp->if_serializer)) {
2133 * ifnet serializer contention happened,
2134 * ifnet.if_start is scheduled on ifnet's
2135 * CPU, and we keep going.
2137 logifstart(contend_sched, ifp);
2138 if_start_schedule(ifp);
2139 return 0;
2142 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
2143 logifstart(run, ifp);
2144 ifp->if_start(ifp);
2145 if ((ifp->if_flags &
2146 (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
2147 running = 1;
2150 lwkt_serialize_exit(ifp->if_serializer);
2152 if (ifq_dispatch_schednochk || if_start_need_schedule(ifq, running)) {
2154 * More data need to be transmitted, ifnet.if_start is
2155 * scheduled on ifnet's CPU, and we keep going.
2156 * NOTE: ifnet.if_start interlock is not released.
2158 logifstart(sched, ifp);
2159 if_start_schedule(ifp);
2161 return 0;
2164 void *
2165 ifa_create(int size, int flags)
2167 struct ifaddr *ifa;
2168 int i;
2170 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small\n"));
2172 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2173 if (ifa == NULL)
2174 return NULL;
2176 ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
2177 M_IFADDR, M_WAITOK | M_ZERO);
2178 ifa->ifa_ncnt = ncpus;
2179 for (i = 0; i < ncpus; ++i) {
2180 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2182 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2183 ifac->ifa = ifa;
2184 ifac->ifa_refcnt = 1;
2186 #ifdef IFADDR_DEBUG
2187 kprintf("alloc ifa %p %d\n", ifa, size);
2188 #endif
2189 return ifa;
2192 void
2193 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2195 struct ifaddr *ifa = ifac->ifa;
2197 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2198 KKASSERT(ifac->ifa_refcnt == 0);
2199 KASSERT(ifac->ifa_listmask == 0,
2200 ("ifa is still on %#x lists\n", ifac->ifa_listmask));
2202 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2204 #ifdef IFADDR_DEBUG_VERBOSE
2205 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2206 #endif
2208 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2209 ("invalid # of ifac, %d\n", ifa->ifa_ncnt));
2210 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2211 #ifdef IFADDR_DEBUG
2212 kprintf("free ifa %p\n", ifa);
2213 #endif
2214 kfree(ifa->ifa_containers, M_IFADDR);
2215 kfree(ifa, M_IFADDR);
2219 static void
2220 ifa_iflink_dispatch(struct netmsg *nmsg)
2222 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2223 struct ifaddr *ifa = msg->ifa;
2224 struct ifnet *ifp = msg->ifp;
2225 int cpu = mycpuid;
2226 struct ifaddr_container *ifac;
2228 crit_enter();
2230 ifac = &ifa->ifa_containers[cpu];
2231 ASSERT_IFAC_VALID(ifac);
2232 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2233 ("ifaddr is on if_addrheads\n"));
2235 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2236 if (msg->tail)
2237 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2238 else
2239 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2241 crit_exit();
2243 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
2246 void
2247 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2249 struct netmsg_ifaddr msg;
2251 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2252 ifa_iflink_dispatch);
2253 msg.ifa = ifa;
2254 msg.ifp = ifp;
2255 msg.tail = tail;
2257 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2260 static void
2261 ifa_ifunlink_dispatch(struct netmsg *nmsg)
2263 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2264 struct ifaddr *ifa = msg->ifa;
2265 struct ifnet *ifp = msg->ifp;
2266 int cpu = mycpuid;
2267 struct ifaddr_container *ifac;
2269 crit_enter();
2271 ifac = &ifa->ifa_containers[cpu];
2272 ASSERT_IFAC_VALID(ifac);
2273 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2274 ("ifaddr is not on if_addrhead\n"));
2276 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2277 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2279 crit_exit();
2281 ifa_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
2284 void
2285 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2287 struct netmsg_ifaddr msg;
2289 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2290 ifa_ifunlink_dispatch);
2291 msg.ifa = ifa;
2292 msg.ifp = ifp;
2294 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2297 static void
2298 ifa_destroy_dispatch(struct netmsg *nmsg)
2300 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2302 IFAFREE(msg->ifa);
2303 ifa_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2306 void
2307 ifa_destroy(struct ifaddr *ifa)
2309 struct netmsg_ifaddr msg;
2311 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
2312 ifa_destroy_dispatch);
2313 msg.ifa = ifa;
2315 ifa_domsg(&msg.netmsg.nm_lmsg, 0);
2318 struct lwkt_port *
2319 ifnet_portfn(int cpu)
2321 return &ifnet_threads[cpu].td_msgport;
2324 void
2325 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2327 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2329 if (next_cpu < ncpus)
2330 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2331 else
2332 lwkt_replymsg(lmsg, 0);
2336 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2338 KKASSERT(cpu < ncpus);
2339 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2342 void
2343 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2345 KKASSERT(cpu < ncpus);
2346 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2349 static void
2350 ifnetinit(void *dummy __unused)
2352 int i;
2354 for (i = 0; i < ncpus; ++i) {
2355 struct thread *thr = &ifnet_threads[i];
2357 lwkt_create(netmsg_service_loop, &ifnet_mpsafe_thread, NULL,
2358 thr, TDF_NETWORK | TDF_MPSAFE, i, "ifnet %d", i);
2359 netmsg_service_port_init(&thr->td_msgport);