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