if: Defer if_start to netisr instead of ifnet for further processing
[dragonfly.git] / sys / net / if.c
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
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.
20  *
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.
32  *
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  */
36
37 #include "opt_compat.h"
38 #include "opt_inet6.h"
39 #include "opt_inet.h"
40 #include "opt_polling.h"
41 #include "opt_ifpoll.h"
42
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/mutex.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/serialize.h>
63 #include <sys/bus.h>
64
65 #include <sys/thread2.h>
66 #include <sys/msgport2.h>
67 #include <sys/mutex2.h>
68
69 #include <net/if.h>
70 #include <net/if_arp.h>
71 #include <net/if_dl.h>
72 #include <net/if_types.h>
73 #include <net/if_var.h>
74 #include <net/ifq_var.h>
75 #include <net/radix.h>
76 #include <net/route.h>
77 #include <net/if_clone.h>
78 #include <net/netisr.h>
79 #include <net/netmsg2.h>
80
81 #include <machine/atomic.h>
82 #include <machine/stdarg.h>
83 #include <machine/smp.h>
84
85 #if defined(INET) || defined(INET6)
86 /*XXX*/
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/if_ether.h>
90 #ifdef INET6
91 #include <netinet6/in6_var.h>
92 #include <netinet6/in6_ifattach.h>
93 #endif
94 #endif
95
96 #if defined(COMPAT_43)
97 #include <emulation/43bsd/43bsd_socket.h>
98 #endif /* COMPAT_43 */
99
100 struct netmsg_ifaddr {
101         struct netmsg_base base;
102         struct ifaddr   *ifa;
103         struct ifnet    *ifp;
104         int             tail;
105 };
106
107 /*
108  * System initialization
109  */
110 static void     if_attachdomain(void *);
111 static void     if_attachdomain1(struct ifnet *);
112 static int      ifconf(u_long, caddr_t, struct ucred *);
113 static void     ifinit(void *);
114 static void     ifnetinit(void *);
115 static void     if_slowtimo(void *);
116 static void     link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
117 static int      if_rtdel(struct radix_node *, void *);
118
119 #ifdef INET6
120 /*
121  * XXX: declare here to avoid to include many inet6 related files..
122  * should be more generalized?
123  */
124 extern void     nd6_setmtu(struct ifnet *);
125 #endif
126
127 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
128 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
129
130 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
131 /* Must be after netisr_init */
132 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
133
134 static  if_com_alloc_t *if_com_alloc[256];
135 static  if_com_free_t *if_com_free[256];
136
137 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
138 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
139 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
140
141 int                     ifqmaxlen = IFQ_MAXLEN;
142 struct ifnethead        ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
143
144 struct callout          if_slowtimo_timer;
145
146 int                     if_index = 0;
147 struct ifnet            **ifindex2ifnet = NULL;
148 static struct thread    ifnet_threads[MAXCPU];
149
150 #define IFQ_KTR_STRING          "ifq=%p"
151 #define IFQ_KTR_ARGS    struct ifaltq *ifq
152 #ifndef KTR_IFQ
153 #define KTR_IFQ                 KTR_ALL
154 #endif
155 KTR_INFO_MASTER(ifq);
156 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
157 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
158 #define logifq(name, arg)       KTR_LOG(ifq_ ## name, arg)
159
160 #define IF_START_KTR_STRING     "ifp=%p"
161 #define IF_START_KTR_ARGS       struct ifnet *ifp
162 #ifndef KTR_IF_START
163 #define KTR_IF_START            KTR_ALL
164 #endif
165 KTR_INFO_MASTER(if_start);
166 KTR_INFO(KTR_IF_START, if_start, run, 0,
167          IF_START_KTR_STRING, IF_START_KTR_ARGS);
168 KTR_INFO(KTR_IF_START, if_start, sched, 1,
169          IF_START_KTR_STRING, IF_START_KTR_ARGS);
170 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
171          IF_START_KTR_STRING, IF_START_KTR_ARGS);
172 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
173          IF_START_KTR_STRING, IF_START_KTR_ARGS);
174 #ifdef SMP
175 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
176          IF_START_KTR_STRING, IF_START_KTR_ARGS);
177 #endif
178 #define logifstart(name, arg)   KTR_LOG(if_start_ ## name, arg)
179
180 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
181
182 /*
183  * Network interface utility routines.
184  *
185  * Routines with ifa_ifwith* names take sockaddr *'s as
186  * parameters.
187  */
188 /* ARGSUSED*/
189 void
190 ifinit(void *dummy)
191 {
192         struct ifnet *ifp;
193
194         callout_init(&if_slowtimo_timer);
195
196         crit_enter();
197         TAILQ_FOREACH(ifp, &ifnet, if_link) {
198                 if (ifp->if_snd.ifq_maxlen == 0) {
199                         if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
200                         ifp->if_snd.ifq_maxlen = ifqmaxlen;
201                 }
202         }
203         crit_exit();
204
205         if_slowtimo(0);
206 }
207
208 static int
209 if_start_cpuid(struct ifnet *ifp)
210 {
211         return ifp->if_cpuid;
212 }
213
214 #ifdef DEVICE_POLLING
215 static int
216 if_start_cpuid_poll(struct ifnet *ifp)
217 {
218         int poll_cpuid = ifp->if_poll_cpuid;
219
220         if (poll_cpuid >= 0)
221                 return poll_cpuid;
222         else
223                 return ifp->if_cpuid;
224 }
225 #endif
226
227 #ifdef IFPOLL_ENABLE
228 static int
229 if_start_cpuid_npoll(struct ifnet *ifp)
230 {
231         int poll_cpuid = ifp->if_npoll_cpuid;
232
233         if (poll_cpuid >= 0)
234                 return poll_cpuid;
235         else
236                 return ifp->if_cpuid;
237 }
238 #endif
239
240 static void
241 if_start_ipifunc(void *arg)
242 {
243         struct ifnet *ifp = arg;
244         struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].lmsg;
245
246         crit_enter();
247         if (lmsg->ms_flags & MSGF_DONE)
248                 lwkt_sendmsg(netisr_portfn(mycpuid), lmsg);
249         crit_exit();
250 }
251
252 /*
253  * Schedule ifnet.if_start on ifnet's CPU
254  */
255 static void
256 if_start_schedule(struct ifnet *ifp)
257 {
258 #ifdef SMP
259         int cpu;
260
261         cpu = ifp->if_start_cpuid(ifp);
262         if (cpu != mycpuid)
263                 lwkt_send_ipiq(globaldata_find(cpu), if_start_ipifunc, ifp);
264         else
265 #endif
266         if_start_ipifunc(ifp);
267 }
268
269 /*
270  * NOTE:
271  * This function will release ifnet.if_start interlock,
272  * if ifnet.if_start does not need to be scheduled
273  */
274 static __inline int
275 if_start_need_schedule(struct ifaltq *ifq, int running)
276 {
277         if (!running || ifq_is_empty(ifq)
278 #ifdef ALTQ
279             || ifq->altq_tbr != NULL
280 #endif
281         ) {
282                 ALTQ_LOCK(ifq);
283                 /*
284                  * ifnet.if_start interlock is released, if:
285                  * 1) Hardware can not take any packets, due to
286                  *    o  interface is marked down
287                  *    o  hardware queue is full (IFF_OACTIVE)
288                  *    Under the second situation, hardware interrupt
289                  *    or polling(4) will call/schedule ifnet.if_start
290                  *    when hardware queue is ready
291                  * 2) There is not packet in the ifnet.if_snd.
292                  *    Further ifq_dispatch or ifq_handoff will call/
293                  *    schedule ifnet.if_start
294                  * 3) TBR is used and it does not allow further
295                  *    dequeueing.
296                  *    TBR callout will call ifnet.if_start
297                  */
298                 if (!running || !ifq_data_ready(ifq)) {
299                         ifq->altq_started = 0;
300                         ALTQ_UNLOCK(ifq);
301                         return 0;
302                 }
303                 ALTQ_UNLOCK(ifq);
304         }
305         return 1;
306 }
307
308 static void
309 if_start_dispatch(netmsg_t msg)
310 {
311         struct lwkt_msg *lmsg = &msg->base.lmsg;
312         struct ifnet *ifp = lmsg->u.ms_resultp;
313         struct ifaltq *ifq = &ifp->if_snd;
314         int running = 0;
315
316         crit_enter();
317         lwkt_replymsg(lmsg, 0); /* reply ASAP */
318         crit_exit();
319
320 #ifdef SMP
321         if (mycpuid != ifp->if_start_cpuid(ifp)) {
322                 /*
323                  * If the ifnet is still up, we need to
324                  * chase its CPU change.
325                  */
326                 if (ifp->if_flags & IFF_UP) {
327                         logifstart(chase_sched, ifp);
328                         if_start_schedule(ifp);
329                         return;
330                 } else {
331                         goto check;
332                 }
333         }
334 #endif
335
336         if (ifp->if_flags & IFF_UP) {
337                 ifnet_serialize_tx(ifp); /* XXX try? */
338                 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
339                         logifstart(run, ifp);
340                         ifp->if_start(ifp);
341                         if ((ifp->if_flags &
342                         (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
343                                 running = 1;
344                 }
345                 ifnet_deserialize_tx(ifp);
346         }
347 #ifdef SMP
348 check:
349 #endif
350         if (if_start_need_schedule(ifq, running)) {
351                 crit_enter();
352                 if (lmsg->ms_flags & MSGF_DONE) { /* XXX necessary? */
353                         logifstart(sched, ifp);
354                         lwkt_sendmsg(netisr_portfn(mycpuid), lmsg);
355                 }
356                 crit_exit();
357         }
358 }
359
360 /* Device driver ifnet.if_start helper function */
361 void
362 if_devstart(struct ifnet *ifp)
363 {
364         struct ifaltq *ifq = &ifp->if_snd;
365         int running = 0;
366
367         ASSERT_IFNET_SERIALIZED_TX(ifp);
368
369         ALTQ_LOCK(ifq);
370         if (ifq->altq_started || !ifq_data_ready(ifq)) {
371                 logifstart(avoid, ifp);
372                 ALTQ_UNLOCK(ifq);
373                 return;
374         }
375         ifq->altq_started = 1;
376         ALTQ_UNLOCK(ifq);
377
378         logifstart(run, ifp);
379         ifp->if_start(ifp);
380
381         if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
382                 running = 1;
383
384         if (if_start_need_schedule(ifq, running)) {
385                 /*
386                  * More data need to be transmitted, ifnet.if_start is
387                  * scheduled on ifnet's CPU, and we keep going.
388                  * NOTE: ifnet.if_start interlock is not released.
389                  */
390                 logifstart(sched, ifp);
391                 if_start_schedule(ifp);
392         }
393 }
394
395 static void
396 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
397 {
398         lwkt_serialize_enter(ifp->if_serializer);
399 }
400
401 static void
402 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
403 {
404         lwkt_serialize_exit(ifp->if_serializer);
405 }
406
407 static int
408 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
409 {
410         return lwkt_serialize_try(ifp->if_serializer);
411 }
412
413 #ifdef INVARIANTS
414 static void
415 if_default_serialize_assert(struct ifnet *ifp,
416                             enum ifnet_serialize slz __unused,
417                             boolean_t serialized)
418 {
419         if (serialized)
420                 ASSERT_SERIALIZED(ifp->if_serializer);
421         else
422                 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
423 }
424 #endif
425
426 /*
427  * Attach an interface to the list of "active" interfaces.
428  *
429  * The serializer is optional.  If non-NULL access to the interface
430  * may be MPSAFE.
431  */
432 void
433 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
434 {
435         unsigned socksize, ifasize;
436         int namelen, masklen;
437         struct sockaddr_dl *sdl;
438         struct ifaddr *ifa;
439         struct ifaltq *ifq;
440         int i;
441
442         static int if_indexlim = 8;
443
444         if (ifp->if_serialize != NULL) {
445                 KASSERT(ifp->if_deserialize != NULL &&
446                         ifp->if_tryserialize != NULL &&
447                         ifp->if_serialize_assert != NULL,
448                         ("serialize functions are partially setup"));
449
450                 /*
451                  * If the device supplies serialize functions,
452                  * then clear if_serializer to catch any invalid
453                  * usage of this field.
454                  */
455                 KASSERT(serializer == NULL,
456                         ("both serialize functions and default serializer "
457                          "are supplied"));
458                 ifp->if_serializer = NULL;
459         } else {
460                 KASSERT(ifp->if_deserialize == NULL &&
461                         ifp->if_tryserialize == NULL &&
462                         ifp->if_serialize_assert == NULL,
463                         ("serialize functions are partially setup"));
464                 ifp->if_serialize = if_default_serialize;
465                 ifp->if_deserialize = if_default_deserialize;
466                 ifp->if_tryserialize = if_default_tryserialize;
467 #ifdef INVARIANTS
468                 ifp->if_serialize_assert = if_default_serialize_assert;
469 #endif
470
471                 /*
472                  * The serializer can be passed in from the device,
473                  * allowing the same serializer to be used for both
474                  * the interrupt interlock and the device queue.
475                  * If not specified, the netif structure will use an
476                  * embedded serializer.
477                  */
478                 if (serializer == NULL) {
479                         serializer = &ifp->if_default_serializer;
480                         lwkt_serialize_init(serializer);
481                 }
482                 ifp->if_serializer = serializer;
483         }
484
485         ifp->if_start_cpuid = if_start_cpuid;
486         ifp->if_cpuid = 0;
487
488 #ifdef DEVICE_POLLING
489         /* Device is not in polling mode by default */
490         ifp->if_poll_cpuid = -1;
491         if (ifp->if_poll != NULL)
492                 ifp->if_start_cpuid = if_start_cpuid_poll;
493 #endif
494 #ifdef IFPOLL_ENABLE
495         /* Device is not in polling mode by default */
496         ifp->if_npoll_cpuid = -1;
497         if (ifp->if_npoll != NULL)
498                 ifp->if_start_cpuid = if_start_cpuid_npoll;
499 #endif
500
501         ifp->if_start_nmsg = kmalloc(ncpus * sizeof(*ifp->if_start_nmsg),
502                                      M_LWKTMSG, M_WAITOK);
503         for (i = 0; i < ncpus; ++i) {
504                 netmsg_init(&ifp->if_start_nmsg[i], NULL, &netisr_adone_rport,
505                             0, if_start_dispatch);
506                 ifp->if_start_nmsg[i].lmsg.u.ms_resultp = ifp;
507         }
508
509         mtx_init(&ifp->if_ioctl_mtx);
510         mtx_lock(&ifp->if_ioctl_mtx);
511
512         TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
513         ifp->if_index = ++if_index;
514
515         /*
516          * XXX -
517          * The old code would work if the interface passed a pre-existing
518          * chain of ifaddrs to this code.  We don't trust our callers to
519          * properly initialize the tailq, however, so we no longer allow
520          * this unlikely case.
521          */
522         ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
523                                     M_IFADDR, M_WAITOK | M_ZERO);
524         for (i = 0; i < ncpus; ++i)
525                 TAILQ_INIT(&ifp->if_addrheads[i]);
526
527         TAILQ_INIT(&ifp->if_prefixhead);
528         TAILQ_INIT(&ifp->if_multiaddrs);
529         TAILQ_INIT(&ifp->if_groups);
530         getmicrotime(&ifp->if_lastchange);
531         if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
532                 unsigned int n;
533                 struct ifnet **q;
534
535                 if_indexlim <<= 1;
536
537                 /* grow ifindex2ifnet */
538                 n = if_indexlim * sizeof(*q);
539                 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
540                 if (ifindex2ifnet) {
541                         bcopy(ifindex2ifnet, q, n/2);
542                         kfree(ifindex2ifnet, M_IFADDR);
543                 }
544                 ifindex2ifnet = q;
545         }
546
547         ifindex2ifnet[if_index] = ifp;
548
549         /*
550          * create a Link Level name for this device
551          */
552         namelen = strlen(ifp->if_xname);
553         masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
554         socksize = masklen + ifp->if_addrlen;
555 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
556         if (socksize < sizeof(*sdl))
557                 socksize = sizeof(*sdl);
558         socksize = ROUNDUP(socksize);
559 #undef ROUNDUP
560         ifasize = sizeof(struct ifaddr) + 2 * socksize;
561         ifa = ifa_create(ifasize, M_WAITOK);
562         sdl = (struct sockaddr_dl *)(ifa + 1);
563         sdl->sdl_len = socksize;
564         sdl->sdl_family = AF_LINK;
565         bcopy(ifp->if_xname, sdl->sdl_data, namelen);
566         sdl->sdl_nlen = namelen;
567         sdl->sdl_index = ifp->if_index;
568         sdl->sdl_type = ifp->if_type;
569         ifp->if_lladdr = ifa;
570         ifa->ifa_ifp = ifp;
571         ifa->ifa_rtrequest = link_rtrequest;
572         ifa->ifa_addr = (struct sockaddr *)sdl;
573         sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
574         ifa->ifa_netmask = (struct sockaddr *)sdl;
575         sdl->sdl_len = masklen;
576         while (namelen != 0)
577                 sdl->sdl_data[--namelen] = 0xff;
578         ifa_iflink(ifa, ifp, 0 /* Insert head */);
579
580         EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
581         devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
582
583         ifq = &ifp->if_snd;
584         ifq->altq_type = 0;
585         ifq->altq_disc = NULL;
586         ifq->altq_flags &= ALTQF_CANTCHANGE;
587         ifq->altq_tbr = NULL;
588         ifq->altq_ifp = ifp;
589         ifq->altq_started = 0;
590         ifq->altq_prepended = NULL;
591         ALTQ_LOCK_INIT(ifq);
592         ifq_set_classic(ifq);
593
594         if (!SLIST_EMPTY(&domains))
595                 if_attachdomain1(ifp);
596
597         /* Announce the interface. */
598         rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
599
600         mtx_unlock(&ifp->if_ioctl_mtx);
601 }
602
603 static void
604 if_attachdomain(void *dummy)
605 {
606         struct ifnet *ifp;
607
608         crit_enter();
609         TAILQ_FOREACH(ifp, &ifnet, if_list)
610                 if_attachdomain1(ifp);
611         crit_exit();
612 }
613 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
614         if_attachdomain, NULL);
615
616 static void
617 if_attachdomain1(struct ifnet *ifp)
618 {
619         struct domain *dp;
620
621         crit_enter();
622
623         /* address family dependent data region */
624         bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
625         SLIST_FOREACH(dp, &domains, dom_next)
626                 if (dp->dom_ifattach)
627                         ifp->if_afdata[dp->dom_family] =
628                                 (*dp->dom_ifattach)(ifp);
629         crit_exit();
630 }
631
632 /*
633  * Purge all addresses whose type is _not_ AF_LINK
634  */
635 void
636 if_purgeaddrs_nolink(struct ifnet *ifp)
637 {
638         struct ifaddr_container *ifac, *next;
639
640         TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
641                               ifa_link, next) {
642                 struct ifaddr *ifa = ifac->ifa;
643
644                 /* Leave link ifaddr as it is */
645                 if (ifa->ifa_addr->sa_family == AF_LINK)
646                         continue;
647 #ifdef INET
648                 /* XXX: Ugly!! ad hoc just for INET */
649                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
650                         struct ifaliasreq ifr;
651 #ifdef IFADDR_DEBUG_VERBOSE
652                         int i;
653
654                         kprintf("purge in4 addr %p: ", ifa);
655                         for (i = 0; i < ncpus; ++i)
656                                 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
657                         kprintf("\n");
658 #endif
659
660                         bzero(&ifr, sizeof ifr);
661                         ifr.ifra_addr = *ifa->ifa_addr;
662                         if (ifa->ifa_dstaddr)
663                                 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
664                         if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
665                                        NULL) == 0)
666                                 continue;
667                 }
668 #endif /* INET */
669 #ifdef INET6
670                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
671 #ifdef IFADDR_DEBUG_VERBOSE
672                         int i;
673
674                         kprintf("purge in6 addr %p: ", ifa);
675                         for (i = 0; i < ncpus; ++i)
676                                 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
677                         kprintf("\n");
678 #endif
679
680                         in6_purgeaddr(ifa);
681                         /* ifp_addrhead is already updated */
682                         continue;
683                 }
684 #endif /* INET6 */
685                 ifa_ifunlink(ifa, ifp);
686                 ifa_destroy(ifa);
687         }
688 }
689
690 /*
691  * Detach an interface, removing it from the
692  * list of "active" interfaces.
693  */
694 void
695 if_detach(struct ifnet *ifp)
696 {
697         struct radix_node_head  *rnh;
698         int i;
699         int cpu, origcpu;
700         struct domain *dp;
701
702         EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
703
704         /*
705          * Remove routes and flush queues.
706          */
707         crit_enter();
708 #ifdef DEVICE_POLLING
709         if (ifp->if_flags & IFF_POLLING)
710                 ether_poll_deregister(ifp);
711 #endif
712 #ifdef IFPOLL_ENABLE
713         if (ifp->if_flags & IFF_NPOLLING)
714                 ifpoll_deregister(ifp);
715 #endif
716         if_down(ifp);
717
718 #ifdef ALTQ
719         if (ifq_is_enabled(&ifp->if_snd))
720                 altq_disable(&ifp->if_snd);
721         if (ifq_is_attached(&ifp->if_snd))
722                 altq_detach(&ifp->if_snd);
723 #endif
724
725         /*
726          * Clean up all addresses.
727          */
728         ifp->if_lladdr = NULL;
729
730         if_purgeaddrs_nolink(ifp);
731         if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
732                 struct ifaddr *ifa;
733
734                 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
735                 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
736                         ("non-link ifaddr is left on if_addrheads"));
737
738                 ifa_ifunlink(ifa, ifp);
739                 ifa_destroy(ifa);
740                 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
741                         ("there are still ifaddrs left on if_addrheads"));
742         }
743
744 #ifdef INET
745         /*
746          * Remove all IPv4 kernel structures related to ifp.
747          */
748         in_ifdetach(ifp);
749 #endif
750
751 #ifdef INET6
752         /*
753          * Remove all IPv6 kernel structs related to ifp.  This should be done
754          * before removing routing entries below, since IPv6 interface direct
755          * routes are expected to be removed by the IPv6-specific kernel API.
756          * Otherwise, the kernel will detect some inconsistency and bark it.
757          */
758         in6_ifdetach(ifp);
759 #endif
760
761         /*
762          * Delete all remaining routes using this interface
763          * Unfortuneatly the only way to do this is to slog through
764          * the entire routing table looking for routes which point
765          * to this interface...oh well...
766          */
767         origcpu = mycpuid;
768         for (cpu = 0; cpu < ncpus; cpu++) {
769                 lwkt_migratecpu(cpu);
770                 for (i = 1; i <= AF_MAX; i++) {
771                         if ((rnh = rt_tables[cpu][i]) == NULL)
772                                 continue;
773                         rnh->rnh_walktree(rnh, if_rtdel, ifp);
774                 }
775         }
776         lwkt_migratecpu(origcpu);
777
778         /* Announce that the interface is gone. */
779         rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
780         devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
781
782         SLIST_FOREACH(dp, &domains, dom_next)
783                 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
784                         (*dp->dom_ifdetach)(ifp,
785                                 ifp->if_afdata[dp->dom_family]);
786
787         /*
788          * Remove interface from ifindex2ifp[] and maybe decrement if_index.
789          */
790         ifindex2ifnet[ifp->if_index] = NULL;
791         while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
792                 if_index--;
793
794         TAILQ_REMOVE(&ifnet, ifp, if_link);
795         kfree(ifp->if_addrheads, M_IFADDR);
796         kfree(ifp->if_start_nmsg, M_LWKTMSG);
797         crit_exit();
798 }
799
800 /*
801  * Create interface group without members
802  */
803 struct ifg_group *
804 if_creategroup(const char *groupname)
805 {
806         struct ifg_group        *ifg = NULL;
807
808         if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
809             M_TEMP, M_NOWAIT)) == NULL)
810                 return (NULL);
811
812         strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
813         ifg->ifg_refcnt = 0;
814         ifg->ifg_carp_demoted = 0;
815         TAILQ_INIT(&ifg->ifg_members);
816 #if NPF > 0
817         pfi_attach_ifgroup(ifg);
818 #endif
819         TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
820
821         return (ifg);
822 }
823
824 /*
825  * Add a group to an interface
826  */
827 int
828 if_addgroup(struct ifnet *ifp, const char *groupname)
829 {
830         struct ifg_list         *ifgl;
831         struct ifg_group        *ifg = NULL;
832         struct ifg_member       *ifgm;
833
834         if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
835             groupname[strlen(groupname) - 1] <= '9')
836                 return (EINVAL);
837
838         TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
839                 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
840                         return (EEXIST);
841
842         if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
843                 return (ENOMEM);
844
845         if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
846                 kfree(ifgl, M_TEMP);
847                 return (ENOMEM);
848         }
849
850         TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
851                 if (!strcmp(ifg->ifg_group, groupname))
852                         break;
853
854         if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
855                 kfree(ifgl, M_TEMP);
856                 kfree(ifgm, M_TEMP);
857                 return (ENOMEM);
858         }
859
860         ifg->ifg_refcnt++;
861         ifgl->ifgl_group = ifg;
862         ifgm->ifgm_ifp = ifp;
863
864         TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
865         TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
866
867 #if NPF > 0
868         pfi_group_change(groupname);
869 #endif
870
871         return (0);
872 }
873
874 /*
875  * Remove a group from an interface
876  */
877 int
878 if_delgroup(struct ifnet *ifp, const char *groupname)
879 {
880         struct ifg_list         *ifgl;
881         struct ifg_member       *ifgm;
882
883         TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
884                 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
885                         break;
886         if (ifgl == NULL)
887                 return (ENOENT);
888
889         TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
890
891         TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
892                 if (ifgm->ifgm_ifp == ifp)
893                         break;
894
895         if (ifgm != NULL) {
896                 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
897                 kfree(ifgm, M_TEMP);
898         }
899
900         if (--ifgl->ifgl_group->ifg_refcnt == 0) {
901                 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
902 #if NPF > 0
903                 pfi_detach_ifgroup(ifgl->ifgl_group);
904 #endif
905                 kfree(ifgl->ifgl_group, M_TEMP);
906         }
907
908         kfree(ifgl, M_TEMP);
909
910 #if NPF > 0
911         pfi_group_change(groupname);
912 #endif
913
914         return (0);
915 }
916
917 /*
918  * Stores all groups from an interface in memory pointed
919  * to by data
920  */
921 int
922 if_getgroup(caddr_t data, struct ifnet *ifp)
923 {
924         int                      len, error;
925         struct ifg_list         *ifgl;
926         struct ifg_req           ifgrq, *ifgp;
927         struct ifgroupreq       *ifgr = (struct ifgroupreq *)data;
928
929         if (ifgr->ifgr_len == 0) {
930                 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
931                         ifgr->ifgr_len += sizeof(struct ifg_req);
932                 return (0);
933         }
934
935         len = ifgr->ifgr_len;
936         ifgp = ifgr->ifgr_groups;
937         TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
938                 if (len < sizeof(ifgrq))
939                         return (EINVAL);
940                 bzero(&ifgrq, sizeof ifgrq);
941                 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
942                     sizeof(ifgrq.ifgrq_group));
943                 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
944                     sizeof(struct ifg_req))))
945                         return (error);
946                 len -= sizeof(ifgrq);
947                 ifgp++;
948         }
949
950         return (0);
951 }
952
953 /*
954  * Stores all members of a group in memory pointed to by data
955  */
956 int
957 if_getgroupmembers(caddr_t data)
958 {
959         struct ifgroupreq       *ifgr = (struct ifgroupreq *)data;
960         struct ifg_group        *ifg;
961         struct ifg_member       *ifgm;
962         struct ifg_req           ifgrq, *ifgp;
963         int                      len, error;
964
965         TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
966                 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
967                         break;
968         if (ifg == NULL)
969                 return (ENOENT);
970
971         if (ifgr->ifgr_len == 0) {
972                 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
973                         ifgr->ifgr_len += sizeof(ifgrq);
974                 return (0);
975         }
976
977         len = ifgr->ifgr_len;
978         ifgp = ifgr->ifgr_groups;
979         TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
980                 if (len < sizeof(ifgrq))
981                         return (EINVAL);
982                 bzero(&ifgrq, sizeof ifgrq);
983                 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
984                     sizeof(ifgrq.ifgrq_member));
985                 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
986                     sizeof(struct ifg_req))))
987                         return (error);
988                 len -= sizeof(ifgrq);
989                 ifgp++;
990         }
991
992         return (0);
993 }
994
995 /*
996  * Delete Routes for a Network Interface
997  *
998  * Called for each routing entry via the rnh->rnh_walktree() call above
999  * to delete all route entries referencing a detaching network interface.
1000  *
1001  * Arguments:
1002  *      rn      pointer to node in the routing table
1003  *      arg     argument passed to rnh->rnh_walktree() - detaching interface
1004  *
1005  * Returns:
1006  *      0       successful
1007  *      errno   failed - reason indicated
1008  *
1009  */
1010 static int
1011 if_rtdel(struct radix_node *rn, void *arg)
1012 {
1013         struct rtentry  *rt = (struct rtentry *)rn;
1014         struct ifnet    *ifp = arg;
1015         int             err;
1016
1017         if (rt->rt_ifp == ifp) {
1018
1019                 /*
1020                  * Protect (sorta) against walktree recursion problems
1021                  * with cloned routes
1022                  */
1023                 if (!(rt->rt_flags & RTF_UP))
1024                         return (0);
1025
1026                 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1027                                 rt_mask(rt), rt->rt_flags,
1028                                 NULL);
1029                 if (err) {
1030                         log(LOG_WARNING, "if_rtdel: error %d\n", err);
1031                 }
1032         }
1033
1034         return (0);
1035 }
1036
1037 /*
1038  * Locate an interface based on a complete address.
1039  */
1040 struct ifaddr *
1041 ifa_ifwithaddr(struct sockaddr *addr)
1042 {
1043         struct ifnet *ifp;
1044
1045         TAILQ_FOREACH(ifp, &ifnet, if_link) {
1046                 struct ifaddr_container *ifac;
1047
1048                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1049                         struct ifaddr *ifa = ifac->ifa;
1050
1051                         if (ifa->ifa_addr->sa_family != addr->sa_family)
1052                                 continue;
1053                         if (sa_equal(addr, ifa->ifa_addr))
1054                                 return (ifa);
1055                         if ((ifp->if_flags & IFF_BROADCAST) &&
1056                             ifa->ifa_broadaddr &&
1057                             /* IPv6 doesn't have broadcast */
1058                             ifa->ifa_broadaddr->sa_len != 0 &&
1059                             sa_equal(ifa->ifa_broadaddr, addr))
1060                                 return (ifa);
1061                 }
1062         }
1063         return (NULL);
1064 }
1065 /*
1066  * Locate the point to point interface with a given destination address.
1067  */
1068 struct ifaddr *
1069 ifa_ifwithdstaddr(struct sockaddr *addr)
1070 {
1071         struct ifnet *ifp;
1072
1073         TAILQ_FOREACH(ifp, &ifnet, if_link) {
1074                 struct ifaddr_container *ifac;
1075
1076                 if (!(ifp->if_flags & IFF_POINTOPOINT))
1077                         continue;
1078
1079                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1080                         struct ifaddr *ifa = ifac->ifa;
1081
1082                         if (ifa->ifa_addr->sa_family != addr->sa_family)
1083                                 continue;
1084                         if (ifa->ifa_dstaddr &&
1085                             sa_equal(addr, ifa->ifa_dstaddr))
1086                                 return (ifa);
1087                 }
1088         }
1089         return (NULL);
1090 }
1091
1092 /*
1093  * Find an interface on a specific network.  If many, choice
1094  * is most specific found.
1095  */
1096 struct ifaddr *
1097 ifa_ifwithnet(struct sockaddr *addr)
1098 {
1099         struct ifnet *ifp;
1100         struct ifaddr *ifa_maybe = NULL;
1101         u_int af = addr->sa_family;
1102         char *addr_data = addr->sa_data, *cplim;
1103
1104         /*
1105          * AF_LINK addresses can be looked up directly by their index number,
1106          * so do that if we can.
1107          */
1108         if (af == AF_LINK) {
1109                 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1110
1111                 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1112                         return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1113         }
1114
1115         /*
1116          * Scan though each interface, looking for ones that have
1117          * addresses in this address family.
1118          */
1119         TAILQ_FOREACH(ifp, &ifnet, if_link) {
1120                 struct ifaddr_container *ifac;
1121
1122                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1123                         struct ifaddr *ifa = ifac->ifa;
1124                         char *cp, *cp2, *cp3;
1125
1126                         if (ifa->ifa_addr->sa_family != af)
1127 next:                           continue;
1128                         if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1129                                 /*
1130                                  * This is a bit broken as it doesn't
1131                                  * take into account that the remote end may
1132                                  * be a single node in the network we are
1133                                  * looking for.
1134                                  * The trouble is that we don't know the
1135                                  * netmask for the remote end.
1136                                  */
1137                                 if (ifa->ifa_dstaddr != NULL &&
1138                                     sa_equal(addr, ifa->ifa_dstaddr))
1139                                         return (ifa);
1140                         } else {
1141                                 /*
1142                                  * if we have a special address handler,
1143                                  * then use it instead of the generic one.
1144                                  */
1145                                 if (ifa->ifa_claim_addr) {
1146                                         if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1147                                                 return (ifa);
1148                                         } else {
1149                                                 continue;
1150                                         }
1151                                 }
1152
1153                                 /*
1154                                  * Scan all the bits in the ifa's address.
1155                                  * If a bit dissagrees with what we are
1156                                  * looking for, mask it with the netmask
1157                                  * to see if it really matters.
1158                                  * (A byte at a time)
1159                                  */
1160                                 if (ifa->ifa_netmask == 0)
1161                                         continue;
1162                                 cp = addr_data;
1163                                 cp2 = ifa->ifa_addr->sa_data;
1164                                 cp3 = ifa->ifa_netmask->sa_data;
1165                                 cplim = ifa->ifa_netmask->sa_len +
1166                                         (char *)ifa->ifa_netmask;
1167                                 while (cp3 < cplim)
1168                                         if ((*cp++ ^ *cp2++) & *cp3++)
1169                                                 goto next; /* next address! */
1170                                 /*
1171                                  * If the netmask of what we just found
1172                                  * is more specific than what we had before
1173                                  * (if we had one) then remember the new one
1174                                  * before continuing to search
1175                                  * for an even better one.
1176                                  */
1177                                 if (ifa_maybe == NULL ||
1178                                     rn_refines((char *)ifa->ifa_netmask,
1179                                                (char *)ifa_maybe->ifa_netmask))
1180                                         ifa_maybe = ifa;
1181                         }
1182                 }
1183         }
1184         return (ifa_maybe);
1185 }
1186
1187 /*
1188  * Find an interface address specific to an interface best matching
1189  * a given address.
1190  */
1191 struct ifaddr *
1192 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1193 {
1194         struct ifaddr_container *ifac;
1195         char *cp, *cp2, *cp3;
1196         char *cplim;
1197         struct ifaddr *ifa_maybe = NULL;
1198         u_int af = addr->sa_family;
1199
1200         if (af >= AF_MAX)
1201                 return (0);
1202         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1203                 struct ifaddr *ifa = ifac->ifa;
1204
1205                 if (ifa->ifa_addr->sa_family != af)
1206                         continue;
1207                 if (ifa_maybe == NULL)
1208                         ifa_maybe = ifa;
1209                 if (ifa->ifa_netmask == NULL) {
1210                         if (sa_equal(addr, ifa->ifa_addr) ||
1211                             (ifa->ifa_dstaddr != NULL &&
1212                              sa_equal(addr, ifa->ifa_dstaddr)))
1213                                 return (ifa);
1214                         continue;
1215                 }
1216                 if (ifp->if_flags & IFF_POINTOPOINT) {
1217                         if (sa_equal(addr, ifa->ifa_dstaddr))
1218                                 return (ifa);
1219                 } else {
1220                         cp = addr->sa_data;
1221                         cp2 = ifa->ifa_addr->sa_data;
1222                         cp3 = ifa->ifa_netmask->sa_data;
1223                         cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1224                         for (; cp3 < cplim; cp3++)
1225                                 if ((*cp++ ^ *cp2++) & *cp3)
1226                                         break;
1227                         if (cp3 == cplim)
1228                                 return (ifa);
1229                 }
1230         }
1231         return (ifa_maybe);
1232 }
1233
1234 /*
1235  * Default action when installing a route with a Link Level gateway.
1236  * Lookup an appropriate real ifa to point to.
1237  * This should be moved to /sys/net/link.c eventually.
1238  */
1239 static void
1240 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1241 {
1242         struct ifaddr *ifa;
1243         struct sockaddr *dst;
1244         struct ifnet *ifp;
1245
1246         if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1247             (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1248                 return;
1249         ifa = ifaof_ifpforaddr(dst, ifp);
1250         if (ifa != NULL) {
1251                 IFAFREE(rt->rt_ifa);
1252                 IFAREF(ifa);
1253                 rt->rt_ifa = ifa;
1254                 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1255                         ifa->ifa_rtrequest(cmd, rt, info);
1256         }
1257 }
1258
1259 /*
1260  * Mark an interface down and notify protocols of
1261  * the transition.
1262  * NOTE: must be called at splnet or eqivalent.
1263  */
1264 void
1265 if_unroute(struct ifnet *ifp, int flag, int fam)
1266 {
1267         struct ifaddr_container *ifac;
1268
1269         ifp->if_flags &= ~flag;
1270         getmicrotime(&ifp->if_lastchange);
1271         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1272                 struct ifaddr *ifa = ifac->ifa;
1273
1274                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1275                         kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1276         }
1277         ifq_purge(&ifp->if_snd);
1278         rt_ifmsg(ifp);
1279 }
1280
1281 /*
1282  * Mark an interface up and notify protocols of
1283  * the transition.
1284  * NOTE: must be called at splnet or eqivalent.
1285  */
1286 void
1287 if_route(struct ifnet *ifp, int flag, int fam)
1288 {
1289         struct ifaddr_container *ifac;
1290
1291         ifq_purge(&ifp->if_snd);
1292         ifp->if_flags |= flag;
1293         getmicrotime(&ifp->if_lastchange);
1294         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1295                 struct ifaddr *ifa = ifac->ifa;
1296
1297                 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1298                         kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1299         }
1300         rt_ifmsg(ifp);
1301 #ifdef INET6
1302         in6_if_up(ifp);
1303 #endif
1304 }
1305
1306 /*
1307  * Mark an interface down and notify protocols of the transition.  An
1308  * interface going down is also considered to be a synchronizing event.
1309  * We must ensure that all packet processing related to the interface
1310  * has completed before we return so e.g. the caller can free the ifnet
1311  * structure that the mbufs may be referencing.
1312  *
1313  * NOTE: must be called at splnet or eqivalent.
1314  */
1315 void
1316 if_down(struct ifnet *ifp)
1317 {
1318         if_unroute(ifp, IFF_UP, AF_UNSPEC);
1319         netmsg_service_sync();
1320 }
1321
1322 /*
1323  * Mark an interface up and notify protocols of
1324  * the transition.
1325  * NOTE: must be called at splnet or eqivalent.
1326  */
1327 void
1328 if_up(struct ifnet *ifp)
1329 {
1330         if_route(ifp, IFF_UP, AF_UNSPEC);
1331 }
1332
1333 /*
1334  * Process a link state change.
1335  * NOTE: must be called at splsoftnet or equivalent.
1336  */
1337 void
1338 if_link_state_change(struct ifnet *ifp)
1339 {
1340         int link_state = ifp->if_link_state;
1341
1342         rt_ifmsg(ifp);
1343         devctl_notify("IFNET", ifp->if_xname,
1344             (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1345 }
1346
1347 /*
1348  * Handle interface watchdog timer routines.  Called
1349  * from softclock, we decrement timers (if set) and
1350  * call the appropriate interface routine on expiration.
1351  */
1352 static void
1353 if_slowtimo(void *arg)
1354 {
1355         struct ifnet *ifp;
1356
1357         crit_enter();
1358
1359         TAILQ_FOREACH(ifp, &ifnet, if_link) {
1360                 if (ifp->if_timer == 0 || --ifp->if_timer)
1361                         continue;
1362                 if (ifp->if_watchdog) {
1363                         if (ifnet_tryserialize_all(ifp)) {
1364                                 (*ifp->if_watchdog)(ifp);
1365                                 ifnet_deserialize_all(ifp);
1366                         } else {
1367                                 /* try again next timeout */
1368                                 ++ifp->if_timer;
1369                         }
1370                 }
1371         }
1372
1373         crit_exit();
1374
1375         callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1376 }
1377
1378 /*
1379  * Map interface name to
1380  * interface structure pointer.
1381  */
1382 struct ifnet *
1383 ifunit(const char *name)
1384 {
1385         struct ifnet *ifp;
1386
1387         /*
1388          * Search all the interfaces for this name/number
1389          */
1390
1391         TAILQ_FOREACH(ifp, &ifnet, if_link) {
1392                 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1393                         break;
1394         }
1395         return (ifp);
1396 }
1397
1398
1399 /*
1400  * Map interface name in a sockaddr_dl to
1401  * interface structure pointer.
1402  */
1403 struct ifnet *
1404 if_withname(struct sockaddr *sa)
1405 {
1406         char ifname[IFNAMSIZ+1];
1407         struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1408
1409         if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1410              (sdl->sdl_nlen > IFNAMSIZ) )
1411                 return NULL;
1412
1413         /*
1414          * ifunit wants a null-terminated name.  It may not be null-terminated
1415          * in the sockaddr.  We don't want to change the caller's sockaddr,
1416          * and there might not be room to put the trailing null anyway, so we
1417          * make a local copy that we know we can null terminate safely.
1418          */
1419
1420         bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1421         ifname[sdl->sdl_nlen] = '\0';
1422         return ifunit(ifname);
1423 }
1424
1425
1426 /*
1427  * Interface ioctls.
1428  */
1429 int
1430 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1431 {
1432         struct ifnet *ifp;
1433         struct ifreq *ifr;
1434         struct ifstat *ifs;
1435         int error;
1436         short oif_flags;
1437         int new_flags;
1438 #ifdef COMPAT_43
1439         int ocmd;
1440 #endif
1441         size_t namelen, onamelen;
1442         char new_name[IFNAMSIZ];
1443         struct ifaddr *ifa;
1444         struct sockaddr_dl *sdl;
1445
1446         switch (cmd) {
1447         case SIOCGIFCONF:
1448         case OSIOCGIFCONF:
1449                 return (ifconf(cmd, data, cred));
1450         default:
1451                 break;
1452         }
1453
1454         ifr = (struct ifreq *)data;
1455
1456         switch (cmd) {
1457         case SIOCIFCREATE:
1458         case SIOCIFCREATE2:
1459                 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1460                         return (error);
1461                 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1462                         cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1463         case SIOCIFDESTROY:
1464                 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1465                         return (error);
1466                 return (if_clone_destroy(ifr->ifr_name));
1467         case SIOCIFGCLONERS:
1468                 return (if_clone_list((struct if_clonereq *)data));
1469         default:
1470                 break;
1471         }
1472
1473         /*
1474          * Nominal ioctl through interface, lookup the ifp and obtain a
1475          * lock to serialize the ifconfig ioctl operation.
1476          */
1477         ifp = ifunit(ifr->ifr_name);
1478         if (ifp == NULL)
1479                 return (ENXIO);
1480         error = 0;
1481         mtx_lock(&ifp->if_ioctl_mtx);
1482
1483         switch (cmd) {
1484         case SIOCGIFINDEX:
1485                 ifr->ifr_index = ifp->if_index;
1486                 break;
1487
1488         case SIOCGIFFLAGS:
1489                 ifr->ifr_flags = ifp->if_flags;
1490                 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1491                 break;
1492
1493         case SIOCGIFCAP:
1494                 ifr->ifr_reqcap = ifp->if_capabilities;
1495                 ifr->ifr_curcap = ifp->if_capenable;
1496                 break;
1497
1498         case SIOCGIFMETRIC:
1499                 ifr->ifr_metric = ifp->if_metric;
1500                 break;
1501
1502         case SIOCGIFMTU:
1503                 ifr->ifr_mtu = ifp->if_mtu;
1504                 break;
1505
1506         case SIOCGIFDATA:
1507                 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1508                                 sizeof(ifp->if_data));
1509                 break;
1510
1511         case SIOCGIFPHYS:
1512                 ifr->ifr_phys = ifp->if_physical;
1513                 break;
1514
1515         case SIOCGIFPOLLCPU:
1516 #ifdef DEVICE_POLLING
1517                 ifr->ifr_pollcpu = ifp->if_poll_cpuid;
1518 #else
1519                 ifr->ifr_pollcpu = -1;
1520 #endif
1521                 break;
1522
1523         case SIOCSIFPOLLCPU:
1524 #ifdef DEVICE_POLLING
1525                 if ((ifp->if_flags & IFF_POLLING) == 0)
1526                         ether_pollcpu_register(ifp, ifr->ifr_pollcpu);
1527 #endif
1528                 break;
1529
1530         case SIOCSIFFLAGS:
1531                 error = priv_check_cred(cred, PRIV_ROOT, 0);
1532                 if (error)
1533                         break;
1534                 new_flags = (ifr->ifr_flags & 0xffff) |
1535                     (ifr->ifr_flagshigh << 16);
1536                 if (ifp->if_flags & IFF_SMART) {
1537                         /* Smart drivers twiddle their own routes */
1538                 } else if (ifp->if_flags & IFF_UP &&
1539                     (new_flags & IFF_UP) == 0) {
1540                         crit_enter();
1541                         if_down(ifp);
1542                         crit_exit();
1543                 } else if (new_flags & IFF_UP &&
1544                     (ifp->if_flags & IFF_UP) == 0) {
1545                         crit_enter();
1546                         if_up(ifp);
1547                         crit_exit();
1548                 }
1549
1550 #ifdef DEVICE_POLLING
1551                 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1552                         if (new_flags & IFF_POLLING) {
1553                                 ether_poll_register(ifp);
1554                         } else {
1555                                 ether_poll_deregister(ifp);
1556                         }
1557                 }
1558 #endif
1559 #ifdef IFPOLL_ENABLE
1560                 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1561                         if (new_flags & IFF_NPOLLING)
1562                                 ifpoll_register(ifp);
1563                         else
1564                                 ifpoll_deregister(ifp);
1565                 }
1566 #endif
1567
1568                 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1569                         (new_flags &~ IFF_CANTCHANGE);
1570                 if (new_flags & IFF_PPROMISC) {
1571                         /* Permanently promiscuous mode requested */
1572                         ifp->if_flags |= IFF_PROMISC;
1573                 } else if (ifp->if_pcount == 0) {
1574                         ifp->if_flags &= ~IFF_PROMISC;
1575                 }
1576                 if (ifp->if_ioctl) {
1577                         ifnet_serialize_all(ifp);
1578                         ifp->if_ioctl(ifp, cmd, data, cred);
1579                         ifnet_deserialize_all(ifp);
1580                 }
1581                 getmicrotime(&ifp->if_lastchange);
1582                 break;
1583
1584         case SIOCSIFCAP:
1585                 error = priv_check_cred(cred, PRIV_ROOT, 0);
1586                 if (error)
1587                         break;
1588                 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1589                         error = EINVAL;
1590                         break;
1591                 }
1592                 ifnet_serialize_all(ifp);
1593                 ifp->if_ioctl(ifp, cmd, data, cred);
1594                 ifnet_deserialize_all(ifp);
1595                 break;
1596
1597         case SIOCSIFNAME:
1598                 error = priv_check_cred(cred, PRIV_ROOT, 0);
1599                 if (error)
1600                         break;
1601                 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1602                 if (error)
1603                         break;
1604                 if (new_name[0] == '\0') {
1605                         error = EINVAL;
1606                         break;
1607                 }
1608                 if (ifunit(new_name) != NULL) {
1609                         error = EEXIST;
1610                         break;
1611                 }
1612
1613                 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1614
1615                 /* Announce the departure of the interface. */
1616                 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1617
1618                 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1619                 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1620                 /* XXX IFA_LOCK(ifa); */
1621                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1622                 namelen = strlen(new_name);
1623                 onamelen = sdl->sdl_nlen;
1624                 /*
1625                  * Move the address if needed.  This is safe because we
1626                  * allocate space for a name of length IFNAMSIZ when we
1627                  * create this in if_attach().
1628                  */
1629                 if (namelen != onamelen) {
1630                         bcopy(sdl->sdl_data + onamelen,
1631                             sdl->sdl_data + namelen, sdl->sdl_alen);
1632                 }
1633                 bcopy(new_name, sdl->sdl_data, namelen);
1634                 sdl->sdl_nlen = namelen;
1635                 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1636                 bzero(sdl->sdl_data, onamelen);
1637                 while (namelen != 0)
1638                         sdl->sdl_data[--namelen] = 0xff;
1639                 /* XXX IFA_UNLOCK(ifa) */
1640
1641                 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1642
1643                 /* Announce the return of the interface. */
1644                 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1645                 break;
1646
1647         case SIOCSIFMETRIC:
1648                 error = priv_check_cred(cred, PRIV_ROOT, 0);
1649                 if (error)
1650                         break;
1651                 ifp->if_metric = ifr->ifr_metric;
1652                 getmicrotime(&ifp->if_lastchange);
1653                 break;
1654
1655         case SIOCSIFPHYS:
1656                 error = priv_check_cred(cred, PRIV_ROOT, 0);
1657                 if (error)
1658                         break;
1659                 if (ifp->if_ioctl == NULL) {
1660                         error = EOPNOTSUPP;
1661                         break;
1662                 }
1663                 ifnet_serialize_all(ifp);
1664                 error = ifp->if_ioctl(ifp, cmd, data, cred);
1665                 ifnet_deserialize_all(ifp);
1666                 if (error == 0)
1667                         getmicrotime(&ifp->if_lastchange);
1668                 break;
1669
1670         case SIOCSIFMTU:
1671         {
1672                 u_long oldmtu = ifp->if_mtu;
1673
1674                 error = priv_check_cred(cred, PRIV_ROOT, 0);
1675                 if (error)
1676                         break;
1677                 if (ifp->if_ioctl == NULL) {
1678                         error = EOPNOTSUPP;
1679                         break;
1680                 }
1681                 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
1682                         error = EINVAL;
1683                         break;
1684                 }
1685                 ifnet_serialize_all(ifp);
1686                 error = ifp->if_ioctl(ifp, cmd, data, cred);
1687                 ifnet_deserialize_all(ifp);
1688                 if (error == 0) {
1689                         getmicrotime(&ifp->if_lastchange);
1690                         rt_ifmsg(ifp);
1691                 }
1692                 /*
1693                  * If the link MTU changed, do network layer specific procedure.
1694                  */
1695                 if (ifp->if_mtu != oldmtu) {
1696 #ifdef INET6
1697                         nd6_setmtu(ifp);
1698 #endif
1699                 }
1700                 break;
1701         }
1702
1703         case SIOCADDMULTI:
1704         case SIOCDELMULTI:
1705                 error = priv_check_cred(cred, PRIV_ROOT, 0);
1706                 if (error)
1707                         break;
1708
1709                 /* Don't allow group membership on non-multicast interfaces. */
1710                 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1711                         error = EOPNOTSUPP;
1712                         break;
1713                 }
1714
1715                 /* Don't let users screw up protocols' entries. */
1716                 if (ifr->ifr_addr.sa_family != AF_LINK) {
1717                         error = EINVAL;
1718                         break;
1719                 }
1720
1721                 if (cmd == SIOCADDMULTI) {
1722                         struct ifmultiaddr *ifma;
1723                         error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1724                 } else {
1725                         error = if_delmulti(ifp, &ifr->ifr_addr);
1726                 }
1727                 if (error == 0)
1728                         getmicrotime(&ifp->if_lastchange);
1729                 break;
1730
1731         case SIOCSIFPHYADDR:
1732         case SIOCDIFPHYADDR:
1733 #ifdef INET6
1734         case SIOCSIFPHYADDR_IN6:
1735 #endif
1736         case SIOCSLIFPHYADDR:
1737         case SIOCSIFMEDIA:
1738         case SIOCSIFGENERIC:
1739                 error = priv_check_cred(cred, PRIV_ROOT, 0);
1740                 if (error)
1741                         break;
1742                 if (ifp->if_ioctl == 0) {
1743                         error = EOPNOTSUPP;
1744                         break;
1745                 }
1746                 ifnet_serialize_all(ifp);
1747                 error = ifp->if_ioctl(ifp, cmd, data, cred);
1748                 ifnet_deserialize_all(ifp);
1749                 if (error == 0)
1750                         getmicrotime(&ifp->if_lastchange);
1751                 break;
1752
1753         case SIOCGIFSTATUS:
1754                 ifs = (struct ifstat *)data;
1755                 ifs->ascii[0] = '\0';
1756                 /* fall through */
1757         case SIOCGIFPSRCADDR:
1758         case SIOCGIFPDSTADDR:
1759         case SIOCGLIFPHYADDR:
1760         case SIOCGIFMEDIA:
1761         case SIOCGIFGENERIC:
1762                 if (ifp->if_ioctl == NULL) {
1763                         error = EOPNOTSUPP;
1764                         break;
1765                 }
1766                 ifnet_serialize_all(ifp);
1767                 error = ifp->if_ioctl(ifp, cmd, data, cred);
1768                 ifnet_deserialize_all(ifp);
1769                 break;
1770
1771         case SIOCSIFLLADDR:
1772                 error = priv_check_cred(cred, PRIV_ROOT, 0);
1773                 if (error)
1774                         break;
1775                 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
1776                                      ifr->ifr_addr.sa_len);
1777                 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1778                 break;
1779
1780         default:
1781                 oif_flags = ifp->if_flags;
1782                 if (so->so_proto == 0) {
1783                         error = EOPNOTSUPP;
1784                         break;
1785                 }
1786 #ifndef COMPAT_43
1787                 error = so_pru_control_direct(so, cmd, data, ifp);
1788 #else
1789                 ocmd = cmd;
1790
1791                 switch (cmd) {
1792                 case SIOCSIFDSTADDR:
1793                 case SIOCSIFADDR:
1794                 case SIOCSIFBRDADDR:
1795                 case SIOCSIFNETMASK:
1796 #if BYTE_ORDER != BIG_ENDIAN
1797                         if (ifr->ifr_addr.sa_family == 0 &&
1798                             ifr->ifr_addr.sa_len < 16) {
1799                                 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1800                                 ifr->ifr_addr.sa_len = 16;
1801                         }
1802 #else
1803                         if (ifr->ifr_addr.sa_len == 0)
1804                                 ifr->ifr_addr.sa_len = 16;
1805 #endif
1806                         break;
1807                 case OSIOCGIFADDR:
1808                         cmd = SIOCGIFADDR;
1809                         break;
1810                 case OSIOCGIFDSTADDR:
1811                         cmd = SIOCGIFDSTADDR;
1812                         break;
1813                 case OSIOCGIFBRDADDR:
1814                         cmd = SIOCGIFBRDADDR;
1815                         break;
1816                 case OSIOCGIFNETMASK:
1817                         cmd = SIOCGIFNETMASK;
1818                         break;
1819                 default:
1820                         break;
1821                 }
1822
1823                 error = so_pru_control_direct(so, cmd, data, ifp);
1824
1825                 switch (ocmd) {
1826                 case OSIOCGIFADDR:
1827                 case OSIOCGIFDSTADDR:
1828                 case OSIOCGIFBRDADDR:
1829                 case OSIOCGIFNETMASK:
1830                         *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1831                         break;
1832                 }
1833 #endif /* COMPAT_43 */
1834
1835                 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1836 #ifdef INET6
1837                         DELAY(100);/* XXX: temporary workaround for fxp issue*/
1838                         if (ifp->if_flags & IFF_UP) {
1839                                 crit_enter();
1840                                 in6_if_up(ifp);
1841                                 crit_exit();
1842                         }
1843 #endif
1844                 }
1845                 break;
1846         }
1847
1848         mtx_unlock(&ifp->if_ioctl_mtx);
1849         return (error);
1850 }
1851
1852 /*
1853  * Set/clear promiscuous mode on interface ifp based on the truth value
1854  * of pswitch.  The calls are reference counted so that only the first
1855  * "on" request actually has an effect, as does the final "off" request.
1856  * Results are undefined if the "off" and "on" requests are not matched.
1857  */
1858 int
1859 ifpromisc(struct ifnet *ifp, int pswitch)
1860 {
1861         struct ifreq ifr;
1862         int error;
1863         int oldflags;
1864
1865         oldflags = ifp->if_flags;
1866         if (ifp->if_flags & IFF_PPROMISC) {
1867                 /* Do nothing if device is in permanently promiscuous mode */
1868                 ifp->if_pcount += pswitch ? 1 : -1;
1869                 return (0);
1870         }
1871         if (pswitch) {
1872                 /*
1873                  * If the device is not configured up, we cannot put it in
1874                  * promiscuous mode.
1875                  */
1876                 if ((ifp->if_flags & IFF_UP) == 0)
1877                         return (ENETDOWN);
1878                 if (ifp->if_pcount++ != 0)
1879                         return (0);
1880                 ifp->if_flags |= IFF_PROMISC;
1881                 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1882                     ifp->if_xname);
1883         } else {
1884                 if (--ifp->if_pcount > 0)
1885                         return (0);
1886                 ifp->if_flags &= ~IFF_PROMISC;
1887                 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1888                     ifp->if_xname);
1889         }
1890         ifr.ifr_flags = ifp->if_flags;
1891         ifr.ifr_flagshigh = ifp->if_flags >> 16;
1892         ifnet_serialize_all(ifp);
1893         error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
1894         ifnet_deserialize_all(ifp);
1895         if (error == 0)
1896                 rt_ifmsg(ifp);
1897         else
1898                 ifp->if_flags = oldflags;
1899         return error;
1900 }
1901
1902 /*
1903  * Return interface configuration
1904  * of system.  List may be used
1905  * in later ioctl's (above) to get
1906  * other information.
1907  */
1908 static int
1909 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1910 {
1911         struct ifconf *ifc = (struct ifconf *)data;
1912         struct ifnet *ifp;
1913         struct sockaddr *sa;
1914         struct ifreq ifr, *ifrp;
1915         int space = ifc->ifc_len, error = 0;
1916
1917         ifrp = ifc->ifc_req;
1918         TAILQ_FOREACH(ifp, &ifnet, if_link) {
1919                 struct ifaddr_container *ifac;
1920                 int addrs;
1921
1922                 if (space <= sizeof ifr)
1923                         break;
1924
1925                 /*
1926                  * Zero the stack declared structure first to prevent
1927                  * memory disclosure.
1928                  */
1929                 bzero(&ifr, sizeof(ifr));
1930                 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1931                     >= sizeof(ifr.ifr_name)) {
1932                         error = ENAMETOOLONG;
1933                         break;
1934                 }
1935
1936                 addrs = 0;
1937                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1938                         struct ifaddr *ifa = ifac->ifa;
1939
1940                         if (space <= sizeof ifr)
1941                                 break;
1942                         sa = ifa->ifa_addr;
1943                         if (cred->cr_prison &&
1944                             prison_if(cred, sa))
1945                                 continue;
1946                         addrs++;
1947 #ifdef COMPAT_43
1948                         if (cmd == OSIOCGIFCONF) {
1949                                 struct osockaddr *osa =
1950                                          (struct osockaddr *)&ifr.ifr_addr;
1951                                 ifr.ifr_addr = *sa;
1952                                 osa->sa_family = sa->sa_family;
1953                                 error = copyout(&ifr, ifrp, sizeof ifr);
1954                                 ifrp++;
1955                         } else
1956 #endif
1957                         if (sa->sa_len <= sizeof(*sa)) {
1958                                 ifr.ifr_addr = *sa;
1959                                 error = copyout(&ifr, ifrp, sizeof ifr);
1960                                 ifrp++;
1961                         } else {
1962                                 if (space < (sizeof ifr) + sa->sa_len -
1963                                             sizeof(*sa))
1964                                         break;
1965                                 space -= sa->sa_len - sizeof(*sa);
1966                                 error = copyout(&ifr, ifrp,
1967                                                 sizeof ifr.ifr_name);
1968                                 if (error == 0)
1969                                         error = copyout(sa, &ifrp->ifr_addr,
1970                                                         sa->sa_len);
1971                                 ifrp = (struct ifreq *)
1972                                         (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1973                         }
1974                         if (error)
1975                                 break;
1976                         space -= sizeof ifr;
1977                 }
1978                 if (error)
1979                         break;
1980                 if (!addrs) {
1981                         bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1982                         error = copyout(&ifr, ifrp, sizeof ifr);
1983                         if (error)
1984                                 break;
1985                         space -= sizeof ifr;
1986                         ifrp++;
1987                 }
1988         }
1989         ifc->ifc_len -= space;
1990         return (error);
1991 }
1992
1993 /*
1994  * Just like if_promisc(), but for all-multicast-reception mode.
1995  */
1996 int
1997 if_allmulti(struct ifnet *ifp, int onswitch)
1998 {
1999         int error = 0;
2000         struct ifreq ifr;
2001
2002         crit_enter();
2003
2004         if (onswitch) {
2005                 if (ifp->if_amcount++ == 0) {
2006                         ifp->if_flags |= IFF_ALLMULTI;
2007                         ifr.ifr_flags = ifp->if_flags;
2008                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
2009                         ifnet_serialize_all(ifp);
2010                         error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2011                                               NULL);
2012                         ifnet_deserialize_all(ifp);
2013                 }
2014         } else {
2015                 if (ifp->if_amcount > 1) {
2016                         ifp->if_amcount--;
2017                 } else {
2018                         ifp->if_amcount = 0;
2019                         ifp->if_flags &= ~IFF_ALLMULTI;
2020                         ifr.ifr_flags = ifp->if_flags;
2021                         ifr.ifr_flagshigh = ifp->if_flags >> 16;
2022                         ifnet_serialize_all(ifp);
2023                         error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2024                                               NULL);
2025                         ifnet_deserialize_all(ifp);
2026                 }
2027         }
2028
2029         crit_exit();
2030
2031         if (error == 0)
2032                 rt_ifmsg(ifp);
2033         return error;
2034 }
2035
2036 /*
2037  * Add a multicast listenership to the interface in question.
2038  * The link layer provides a routine which converts
2039  */
2040 int
2041 if_addmulti(
2042         struct ifnet *ifp,      /* interface to manipulate */
2043         struct sockaddr *sa,    /* address to add */
2044         struct ifmultiaddr **retifma)
2045 {
2046         struct sockaddr *llsa, *dupsa;
2047         int error;
2048         struct ifmultiaddr *ifma;
2049
2050         /*
2051          * If the matching multicast address already exists
2052          * then don't add a new one, just add a reference
2053          */
2054         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2055                 if (sa_equal(sa, ifma->ifma_addr)) {
2056                         ifma->ifma_refcount++;
2057                         if (retifma)
2058                                 *retifma = ifma;
2059                         return 0;
2060                 }
2061         }
2062
2063         /*
2064          * Give the link layer a chance to accept/reject it, and also
2065          * find out which AF_LINK address this maps to, if it isn't one
2066          * already.
2067          */
2068         if (ifp->if_resolvemulti) {
2069                 ifnet_serialize_all(ifp);
2070                 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2071                 ifnet_deserialize_all(ifp);
2072                 if (error) 
2073                         return error;
2074         } else {
2075                 llsa = NULL;
2076         }
2077
2078         ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2079         dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_WAITOK);
2080         bcopy(sa, dupsa, sa->sa_len);
2081
2082         ifma->ifma_addr = dupsa;
2083         ifma->ifma_lladdr = llsa;
2084         ifma->ifma_ifp = ifp;
2085         ifma->ifma_refcount = 1;
2086         ifma->ifma_protospec = 0;
2087         rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2088
2089         /*
2090          * Some network interfaces can scan the address list at
2091          * interrupt time; lock them out.
2092          */
2093         crit_enter();
2094         TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2095         crit_exit();
2096         if (retifma)
2097                 *retifma = ifma;
2098
2099         if (llsa != NULL) {
2100                 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2101                         if (sa_equal(ifma->ifma_addr, llsa))
2102                                 break;
2103                 }
2104                 if (ifma) {
2105                         ifma->ifma_refcount++;
2106                 } else {
2107                         ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2108                         dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_WAITOK);
2109                         bcopy(llsa, dupsa, llsa->sa_len);
2110                         ifma->ifma_addr = dupsa;
2111                         ifma->ifma_ifp = ifp;
2112                         ifma->ifma_refcount = 1;
2113                         crit_enter();
2114                         TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2115                         crit_exit();
2116                 }
2117         }
2118         /*
2119          * We are certain we have added something, so call down to the
2120          * interface to let them know about it.
2121          */
2122         crit_enter();
2123         ifnet_serialize_all(ifp);
2124         if (ifp->if_ioctl)
2125                 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2126         ifnet_deserialize_all(ifp);
2127         crit_exit();
2128
2129         return 0;
2130 }
2131
2132 /*
2133  * Remove a reference to a multicast address on this interface.  Yell
2134  * if the request does not match an existing membership.
2135  */
2136 int
2137 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2138 {
2139         struct ifmultiaddr *ifma;
2140
2141         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2142                 if (sa_equal(sa, ifma->ifma_addr))
2143                         break;
2144         if (ifma == NULL)
2145                 return ENOENT;
2146
2147         if (ifma->ifma_refcount > 1) {
2148                 ifma->ifma_refcount--;
2149                 return 0;
2150         }
2151
2152         rt_newmaddrmsg(RTM_DELMADDR, ifma);
2153         sa = ifma->ifma_lladdr;
2154         crit_enter();
2155         TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2156         /*
2157          * Make sure the interface driver is notified
2158          * in the case of a link layer mcast group being left.
2159          */
2160         if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL) {
2161                 ifnet_serialize_all(ifp);
2162                 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2163                 ifnet_deserialize_all(ifp);
2164         }
2165         crit_exit();
2166         kfree(ifma->ifma_addr, M_IFMADDR);
2167         kfree(ifma, M_IFMADDR);
2168         if (sa == NULL)
2169                 return 0;
2170
2171         /*
2172          * Now look for the link-layer address which corresponds to
2173          * this network address.  It had been squirreled away in
2174          * ifma->ifma_lladdr for this purpose (so we don't have
2175          * to call ifp->if_resolvemulti() again), and we saved that
2176          * value in sa above.  If some nasty deleted the
2177          * link-layer address out from underneath us, we can deal because
2178          * the address we stored was is not the same as the one which was
2179          * in the record for the link-layer address.  (So we don't complain
2180          * in that case.)
2181          */
2182         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2183                 if (sa_equal(sa, ifma->ifma_addr))
2184                         break;
2185         if (ifma == NULL)
2186                 return 0;
2187
2188         if (ifma->ifma_refcount > 1) {
2189                 ifma->ifma_refcount--;
2190                 return 0;
2191         }
2192
2193         crit_enter();
2194         ifnet_serialize_all(ifp);
2195         TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2196         ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2197         ifnet_deserialize_all(ifp);
2198         crit_exit();
2199         kfree(ifma->ifma_addr, M_IFMADDR);
2200         kfree(sa, M_IFMADDR);
2201         kfree(ifma, M_IFMADDR);
2202
2203         return 0;
2204 }
2205
2206 /*
2207  * Delete all multicast group membership for an interface.
2208  * Should be used to quickly flush all multicast filters.
2209  */
2210 void
2211 if_delallmulti(struct ifnet *ifp)
2212 {
2213         struct ifmultiaddr *ifma;
2214         struct ifmultiaddr *next;
2215
2216         TAILQ_FOREACH_MUTABLE(ifma, &ifp->if_multiaddrs, ifma_link, next)
2217                 if_delmulti(ifp, ifma->ifma_addr);
2218 }
2219
2220
2221 /*
2222  * Set the link layer address on an interface.
2223  *
2224  * At this time we only support certain types of interfaces,
2225  * and we don't allow the length of the address to change.
2226  */
2227 int
2228 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2229 {
2230         struct sockaddr_dl *sdl;
2231         struct ifreq ifr;
2232
2233         sdl = IF_LLSOCKADDR(ifp);
2234         if (sdl == NULL)
2235                 return (EINVAL);
2236         if (len != sdl->sdl_alen)       /* don't allow length to change */
2237                 return (EINVAL);
2238         switch (ifp->if_type) {
2239         case IFT_ETHER:                 /* these types use struct arpcom */
2240         case IFT_XETHER:
2241         case IFT_L2VLAN:
2242                 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2243                 bcopy(lladdr, LLADDR(sdl), len);
2244                 break;
2245         default:
2246                 return (ENODEV);
2247         }
2248         /*
2249          * If the interface is already up, we need
2250          * to re-init it in order to reprogram its
2251          * address filter.
2252          */
2253         ifnet_serialize_all(ifp);
2254         if ((ifp->if_flags & IFF_UP) != 0) {
2255 #ifdef INET
2256                 struct ifaddr_container *ifac;
2257 #endif
2258
2259                 ifp->if_flags &= ~IFF_UP;
2260                 ifr.ifr_flags = ifp->if_flags;
2261                 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2262                 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2263                               NULL);
2264                 ifp->if_flags |= IFF_UP;
2265                 ifr.ifr_flags = ifp->if_flags;
2266                 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2267                 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2268                                  NULL);
2269 #ifdef INET
2270                 /*
2271                  * Also send gratuitous ARPs to notify other nodes about
2272                  * the address change.
2273                  */
2274                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2275                         struct ifaddr *ifa = ifac->ifa;
2276
2277                         if (ifa->ifa_addr != NULL &&
2278                             ifa->ifa_addr->sa_family == AF_INET)
2279                                 arp_gratuitous(ifp, ifa);
2280                 }
2281 #endif
2282         }
2283         ifnet_deserialize_all(ifp);
2284         return (0);
2285 }
2286
2287 struct ifmultiaddr *
2288 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2289 {
2290         struct ifmultiaddr *ifma;
2291
2292         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2293                 if (sa_equal(ifma->ifma_addr, sa))
2294                         break;
2295
2296         return ifma;
2297 }
2298
2299 /*
2300  * This function locates the first real ethernet MAC from a network
2301  * card and loads it into node, returning 0 on success or ENOENT if
2302  * no suitable interfaces were found.  It is used by the uuid code to
2303  * generate a unique 6-byte number.
2304  */
2305 int
2306 if_getanyethermac(uint16_t *node, int minlen)
2307 {
2308         struct ifnet *ifp;
2309         struct sockaddr_dl *sdl;
2310
2311         TAILQ_FOREACH(ifp, &ifnet, if_link) {
2312                 if (ifp->if_type != IFT_ETHER)
2313                         continue;
2314                 sdl = IF_LLSOCKADDR(ifp);
2315                 if (sdl->sdl_alen < minlen)
2316                         continue;
2317                 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2318                       minlen);
2319                 return(0);
2320         }
2321         return (ENOENT);
2322 }
2323
2324 /*
2325  * The name argument must be a pointer to storage which will last as
2326  * long as the interface does.  For physical devices, the result of
2327  * device_get_name(dev) is a good choice and for pseudo-devices a
2328  * static string works well.
2329  */
2330 void
2331 if_initname(struct ifnet *ifp, const char *name, int unit)
2332 {
2333         ifp->if_dname = name;
2334         ifp->if_dunit = unit;
2335         if (unit != IF_DUNIT_NONE)
2336                 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2337         else
2338                 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2339 }
2340
2341 int
2342 if_printf(struct ifnet *ifp, const char *fmt, ...)
2343 {
2344         __va_list ap;
2345         int retval;
2346
2347         retval = kprintf("%s: ", ifp->if_xname);
2348         __va_start(ap, fmt);
2349         retval += kvprintf(fmt, ap);
2350         __va_end(ap);
2351         return (retval);
2352 }
2353
2354 struct ifnet *
2355 if_alloc(uint8_t type)
2356 {
2357         struct ifnet *ifp;
2358         size_t size;
2359
2360         /*
2361          * XXX temporary hack until arpcom is setup in if_l2com
2362          */
2363         if (type == IFT_ETHER)
2364                 size = sizeof(struct arpcom);
2365         else
2366                 size = sizeof(struct ifnet);
2367
2368         ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2369
2370         ifp->if_type = type;
2371
2372         if (if_com_alloc[type] != NULL) {
2373                 ifp->if_l2com = if_com_alloc[type](type, ifp);
2374                 if (ifp->if_l2com == NULL) {
2375                         kfree(ifp, M_IFNET);
2376                         return (NULL);
2377                 }
2378         }
2379         return (ifp);
2380 }
2381
2382 void
2383 if_free(struct ifnet *ifp)
2384 {
2385         kfree(ifp, M_IFNET);
2386 }
2387
2388 void
2389 ifq_set_classic(struct ifaltq *ifq)
2390 {
2391         ifq->altq_enqueue = ifq_classic_enqueue;
2392         ifq->altq_dequeue = ifq_classic_dequeue;
2393         ifq->altq_request = ifq_classic_request;
2394 }
2395
2396 int
2397 ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
2398                     struct altq_pktattr *pa __unused)
2399 {
2400         logifq(enqueue, ifq);
2401         if (IF_QFULL(ifq)) {
2402                 m_freem(m);
2403                 return(ENOBUFS);
2404         } else {
2405                 IF_ENQUEUE(ifq, m);
2406                 return(0);
2407         }       
2408 }
2409
2410 struct mbuf *
2411 ifq_classic_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
2412 {
2413         struct mbuf *m;
2414
2415         switch (op) {
2416         case ALTDQ_POLL:
2417                 IF_POLL(ifq, m);
2418                 break;
2419         case ALTDQ_REMOVE:
2420                 logifq(dequeue, ifq);
2421                 IF_DEQUEUE(ifq, m);
2422                 break;
2423         default:
2424                 panic("unsupported ALTQ dequeue op: %d", op);
2425         }
2426         KKASSERT(mpolled == NULL || mpolled == m);
2427         return(m);
2428 }
2429
2430 int
2431 ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
2432 {
2433         switch (req) {
2434         case ALTRQ_PURGE:
2435                 IF_DRAIN(ifq);
2436                 break;
2437         default:
2438                 panic("unsupported ALTQ request: %d", req);
2439         }
2440         return(0);
2441 }
2442
2443 int
2444 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2445 {
2446         struct ifaltq *ifq = &ifp->if_snd;
2447         int running = 0, error, start = 0;
2448
2449         ASSERT_IFNET_NOT_SERIALIZED_TX(ifp);
2450
2451         ALTQ_LOCK(ifq);
2452         error = ifq_enqueue_locked(ifq, m, pa);
2453         if (error) {
2454                 ALTQ_UNLOCK(ifq);
2455                 return error;
2456         }
2457         if (!ifq->altq_started) {
2458                 /*
2459                  * Hold the interlock of ifnet.if_start
2460                  */
2461                 ifq->altq_started = 1;
2462                 start = 1;
2463         }
2464         ALTQ_UNLOCK(ifq);
2465
2466         ifp->if_obytes += m->m_pkthdr.len;
2467         if (m->m_flags & M_MCAST)
2468                 ifp->if_omcasts++;
2469
2470         if (!start) {
2471                 logifstart(avoid, ifp);
2472                 return 0;
2473         }
2474
2475         /*
2476          * Try to do direct ifnet.if_start first, if there is
2477          * contention on ifnet's serializer, ifnet.if_start will
2478          * be scheduled on ifnet's CPU.
2479          */
2480         if (!ifnet_tryserialize_tx(ifp)) {
2481                 /*
2482                  * ifnet serializer contention happened,
2483                  * ifnet.if_start is scheduled on ifnet's
2484                  * CPU, and we keep going.
2485                  */
2486                 logifstart(contend_sched, ifp);
2487                 if_start_schedule(ifp);
2488                 return 0;
2489         }
2490
2491         if ((ifp->if_flags & IFF_OACTIVE) == 0) {
2492                 logifstart(run, ifp);
2493                 ifp->if_start(ifp);
2494                 if ((ifp->if_flags &
2495                      (IFF_OACTIVE | IFF_RUNNING)) == IFF_RUNNING)
2496                         running = 1;
2497         }
2498
2499         ifnet_deserialize_tx(ifp);
2500
2501         if (if_start_need_schedule(ifq, running)) {
2502                 /*
2503                  * More data need to be transmitted, ifnet.if_start is
2504                  * scheduled on ifnet's CPU, and we keep going.
2505                  * NOTE: ifnet.if_start interlock is not released.
2506                  */
2507                 logifstart(sched, ifp);
2508                 if_start_schedule(ifp);
2509         }
2510         return 0;
2511 }
2512
2513 void *
2514 ifa_create(int size, int flags)
2515 {
2516         struct ifaddr *ifa;
2517         int i;
2518
2519         KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
2520
2521         ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2522         if (ifa == NULL)
2523                 return NULL;
2524
2525         ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
2526                                       M_IFADDR, M_WAITOK | M_ZERO);
2527         ifa->ifa_ncnt = ncpus;
2528         for (i = 0; i < ncpus; ++i) {
2529                 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2530
2531                 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2532                 ifac->ifa = ifa;
2533                 ifac->ifa_refcnt = 1;
2534         }
2535 #ifdef IFADDR_DEBUG
2536         kprintf("alloc ifa %p %d\n", ifa, size);
2537 #endif
2538         return ifa;
2539 }
2540
2541 void
2542 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2543 {
2544         struct ifaddr *ifa = ifac->ifa;
2545
2546         KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2547         KKASSERT(ifac->ifa_refcnt == 0);
2548         KASSERT(ifac->ifa_listmask == 0,
2549                 ("ifa is still on %#x lists", ifac->ifa_listmask));
2550
2551         ifac->ifa_magic = IFA_CONTAINER_DEAD;
2552
2553 #ifdef IFADDR_DEBUG_VERBOSE
2554         kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2555 #endif
2556
2557         KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2558                 ("invalid # of ifac, %d", ifa->ifa_ncnt));
2559         if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2560 #ifdef IFADDR_DEBUG
2561                 kprintf("free ifa %p\n", ifa);
2562 #endif
2563                 kfree(ifa->ifa_containers, M_IFADDR);
2564                 kfree(ifa, M_IFADDR);
2565         }
2566 }
2567
2568 static void
2569 ifa_iflink_dispatch(netmsg_t nmsg)
2570 {
2571         struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2572         struct ifaddr *ifa = msg->ifa;
2573         struct ifnet *ifp = msg->ifp;
2574         int cpu = mycpuid;
2575         struct ifaddr_container *ifac;
2576
2577         crit_enter();
2578
2579         ifac = &ifa->ifa_containers[cpu];
2580         ASSERT_IFAC_VALID(ifac);
2581         KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2582                 ("ifaddr is on if_addrheads"));
2583
2584         ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2585         if (msg->tail)
2586                 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2587         else
2588                 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2589
2590         crit_exit();
2591
2592         ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2593 }
2594
2595 void
2596 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2597 {
2598         struct netmsg_ifaddr msg;
2599
2600         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2601                     0, ifa_iflink_dispatch);
2602         msg.ifa = ifa;
2603         msg.ifp = ifp;
2604         msg.tail = tail;
2605
2606         ifa_domsg(&msg.base.lmsg, 0);
2607 }
2608
2609 static void
2610 ifa_ifunlink_dispatch(netmsg_t nmsg)
2611 {
2612         struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2613         struct ifaddr *ifa = msg->ifa;
2614         struct ifnet *ifp = msg->ifp;
2615         int cpu = mycpuid;
2616         struct ifaddr_container *ifac;
2617
2618         crit_enter();
2619
2620         ifac = &ifa->ifa_containers[cpu];
2621         ASSERT_IFAC_VALID(ifac);
2622         KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2623                 ("ifaddr is not on if_addrhead"));
2624
2625         TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2626         ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2627
2628         crit_exit();
2629
2630         ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2631 }
2632
2633 void
2634 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2635 {
2636         struct netmsg_ifaddr msg;
2637
2638         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2639                     0, ifa_ifunlink_dispatch);
2640         msg.ifa = ifa;
2641         msg.ifp = ifp;
2642
2643         ifa_domsg(&msg.base.lmsg, 0);
2644 }
2645
2646 static void
2647 ifa_destroy_dispatch(netmsg_t nmsg)
2648 {
2649         struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2650
2651         IFAFREE(msg->ifa);
2652         ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
2653 }
2654
2655 void
2656 ifa_destroy(struct ifaddr *ifa)
2657 {
2658         struct netmsg_ifaddr msg;
2659
2660         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2661                     0, ifa_destroy_dispatch);
2662         msg.ifa = ifa;
2663
2664         ifa_domsg(&msg.base.lmsg, 0);
2665 }
2666
2667 struct lwkt_port *
2668 ifnet_portfn(int cpu)
2669 {
2670         return &ifnet_threads[cpu].td_msgport;
2671 }
2672
2673 void
2674 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2675 {
2676         KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2677
2678         if (next_cpu < ncpus)
2679                 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2680         else
2681                 lwkt_replymsg(lmsg, 0);
2682 }
2683
2684 int
2685 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2686 {
2687         KKASSERT(cpu < ncpus);
2688         return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2689 }
2690
2691 void
2692 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2693 {
2694         KKASSERT(cpu < ncpus);
2695         lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2696 }
2697
2698 /*
2699  * Generic netmsg service loop.  Some protocols may roll their own but all
2700  * must do the basic command dispatch function call done here.
2701  */
2702 static void
2703 ifnet_service_loop(void *arg __unused)
2704 {
2705         netmsg_t msg;
2706
2707         while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
2708                 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
2709                 msg->base.nm_dispatch(msg);
2710         }
2711 }
2712
2713 static void
2714 ifnetinit(void *dummy __unused)
2715 {
2716         int i;
2717
2718         for (i = 0; i < ncpus; ++i) {
2719                 struct thread *thr = &ifnet_threads[i];
2720
2721                 lwkt_create(ifnet_service_loop, NULL, NULL,
2722                             thr, TDF_NOSTART|TDF_FORCE_SPINPORT,
2723                             i, "ifnet %d", i);
2724                 netmsg_service_port_init(&thr->td_msgport);
2725                 lwkt_schedule(thr);
2726         }
2727 }
2728
2729 struct ifnet *
2730 ifnet_byindex(unsigned short idx)
2731 {
2732         if (idx > if_index)
2733                 return NULL;
2734         return ifindex2ifnet[idx];
2735 }
2736
2737 struct ifaddr *
2738 ifaddr_byindex(unsigned short idx)
2739 {
2740         struct ifnet *ifp;
2741
2742         ifp = ifnet_byindex(idx);
2743         if (!ifp)
2744                 return NULL;
2745         return TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2746 }
2747
2748 void
2749 if_register_com_alloc(u_char type,
2750     if_com_alloc_t *a, if_com_free_t *f)
2751 {
2752
2753         KASSERT(if_com_alloc[type] == NULL,
2754             ("if_register_com_alloc: %d already registered", type));
2755         KASSERT(if_com_free[type] == NULL,
2756             ("if_register_com_alloc: %d free already registered", type));
2757
2758         if_com_alloc[type] = a;
2759         if_com_free[type] = f;
2760 }
2761
2762 void
2763 if_deregister_com_alloc(u_char type)
2764 {
2765
2766         KASSERT(if_com_alloc[type] != NULL,
2767             ("if_deregister_com_alloc: %d not registered", type));
2768         KASSERT(if_com_free[type] != NULL,
2769             ("if_deregister_com_alloc: %d free not registered", type));
2770         if_com_alloc[type] = NULL;
2771         if_com_free[type] = NULL;
2772 }
2773
2774 int
2775 if_ring_count2(int cnt, int cnt_max)
2776 {
2777         int shift = 0;
2778
2779         KASSERT(cnt_max >= 1 && powerof2(cnt_max),
2780             ("invalid ring count max %d", cnt_max));
2781
2782         if (cnt <= 0)
2783                 cnt = cnt_max;
2784         if (cnt > ncpus2)
2785                 cnt = ncpus2;
2786         if (cnt > cnt_max)
2787                 cnt = cnt_max;
2788
2789         while ((1 << (shift + 1)) <= cnt)
2790                 ++shift;
2791         cnt = 1 << shift;
2792
2793         KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
2794             ("calculate cnt %d, ncpus2 %d, cnt max %d",
2795              cnt, ncpus2, cnt_max));
2796         return cnt;
2797 }