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