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