inet: Merge in_control() and in_control_redispatch()
[dragonfly.git] / sys / netinet / in.c
1 /*
2  * Copyright (c) 1982, 1986, 1991, 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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)in.c        8.4 (Berkeley) 1/9/95
30  * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
31  */
32
33 #include "opt_bootp.h"
34 #include "opt_carp.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
39 #include <sys/malloc.h>
40 #include <sys/proc.h>
41 #include <sys/priv.h>
42 #include <sys/msgport.h>
43 #include <sys/socket.h>
44
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread2.h>
48
49 #include <net/if.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
52 #include <net/netmsg2.h>
53 #include <net/netisr2.h>
54
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/udp_var.h>
59
60 #include <netinet/igmp_var.h>
61
62 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
63
64 static int in_mask2len (struct in_addr *);
65 static void in_len2mask (struct in_addr *, int);
66 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
67         struct ifnet *, struct thread *);
68
69 static void     in_socktrim (struct sockaddr_in *);
70 static int      in_ifinit(struct ifnet *, struct in_ifaddr *,
71                     const struct sockaddr_in *, int);
72
73 static int      in_control_internal(u_long, caddr_t, struct ifnet *,
74                     struct thread *);
75
76 static int      in_addprefix(struct in_ifaddr *, int);
77 static void     in_scrubprefix(struct in_ifaddr *);
78
79 static int subnetsarelocal = 0;
80 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
81     &subnetsarelocal, 0,
82     "Count all internet addresses of subnets of the local net as local");
83
84 struct in_multihead in_multihead; /* XXX BSS initialization */
85
86 extern struct inpcbinfo ripcbinfo;
87
88 /*
89  * Return 1 if an internet address is for a ``local'' host
90  * (one to which we have a connection).  If subnetsarelocal
91  * is true, this includes other subnets of the local net.
92  * Otherwise, it includes only the directly-connected (sub)nets.
93  */
94 int
95 in_localaddr(struct in_addr in)
96 {
97         u_long i = ntohl(in.s_addr);
98         struct in_ifaddr_container *iac;
99         struct in_ifaddr *ia;
100
101         if (subnetsarelocal) {
102                 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
103                         ia = iac->ia;
104
105                         if ((i & ia->ia_netmask) == ia->ia_net)
106                                 return (1);
107                 }
108         } else {
109                 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
110                         ia = iac->ia;
111
112                         if ((i & ia->ia_subnetmask) == ia->ia_subnet)
113                                 return (1);
114                 }
115         }
116         return (0);
117 }
118
119 /*
120  * Determine whether an IP address is in a reserved set of addresses
121  * that may not be forwarded, or whether datagrams to that destination
122  * may be forwarded.
123  */
124 int
125 in_canforward(struct in_addr in)
126 {
127         u_long i = ntohl(in.s_addr);
128         u_long net;
129
130         if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
131                 return (0);
132         if (IN_CLASSA(i)) {
133                 net = i & IN_CLASSA_NET;
134                 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
135                         return (0);
136         }
137         return (1);
138 }
139
140 /*
141  * Trim a mask in a sockaddr
142  */
143 static void
144 in_socktrim(struct sockaddr_in *ap)
145 {
146     char *cplim = (char *) &ap->sin_addr;
147     char *cp = (char *) (&ap->sin_addr + 1);
148
149     ap->sin_len = 0;
150     while (--cp >= cplim)
151         if (*cp) {
152             (ap)->sin_len = cp - (char *) (ap) + 1;
153             break;
154         }
155 }
156
157 static int
158 in_mask2len(struct in_addr *mask)
159 {
160         int x, y;
161         u_char *p;
162
163         p = (u_char *)mask;
164         for (x = 0; x < sizeof *mask; x++) {
165                 if (p[x] != 0xff)
166                         break;
167         }
168         y = 0;
169         if (x < sizeof *mask) {
170                 for (y = 0; y < 8; y++) {
171                         if ((p[x] & (0x80 >> y)) == 0)
172                                 break;
173                 }
174         }
175         return x * 8 + y;
176 }
177
178 static void
179 in_len2mask(struct in_addr *mask, int len)
180 {
181         int i;
182         u_char *p;
183
184         p = (u_char *)mask;
185         bzero(mask, sizeof *mask);
186         for (i = 0; i < len / 8; i++)
187                 p[i] = 0xff;
188         if (len % 8)
189                 p[i] = (0xff00 >> (len % 8)) & 0xff;
190 }
191
192 static int in_interfaces;       /* number of external internet interfaces */
193
194 void
195 in_control_dispatch(netmsg_t msg)
196 {
197         int error;
198
199         error = in_control(msg->base.nm_so, msg->control.nm_cmd,
200             msg->control.nm_data, msg->control.nm_ifp, msg->control.nm_td);
201         lwkt_replymsg(&msg->lmsg, error);
202 }
203
204 static void
205 in_control_internal_dispatch(netmsg_t msg)
206 {
207         int error;
208
209         error = in_control_internal(msg->control.nm_cmd,
210                                     msg->control.nm_data,
211                                     msg->control.nm_ifp,
212                                     msg->control.nm_td);
213         lwkt_replymsg(&msg->lmsg, error);
214 }
215
216 /*
217  * Generic internet control operations (ioctl's).
218  * Ifp is 0 if not an interface-specific ioctl.
219  *
220  * NOTE! td might be NULL.
221  */
222 /* ARGSUSED */
223 int
224 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
225     struct thread *td)
226 {
227         struct netmsg_pru_control msg;
228         int error;
229
230         switch (cmd) {
231         case SIOCALIFADDR:
232         case SIOCDLIFADDR:
233                 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
234                         return error;
235                 /* FALLTHROUGH */
236         case SIOCGLIFADDR:
237                 if (!ifp)
238                         return EINVAL;
239                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
240         }
241
242         /*
243          * IFADDR alterations are serialized by netisr0
244          */
245         switch (cmd) {
246         case SIOCSIFDSTADDR:
247         case SIOCSIFBRDADDR:
248         case SIOCSIFADDR:
249         case SIOCSIFNETMASK:
250         case SIOCAIFADDR:
251         case SIOCDIFADDR:
252                 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
253                             0, in_control_internal_dispatch);
254                 msg.nm_cmd = cmd;
255                 msg.nm_data = data;
256                 msg.nm_ifp = ifp;
257                 msg.nm_td = td;
258                 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
259                 error = msg.base.lmsg.ms_error;
260                 break;
261
262         default:
263                 error = in_control_internal(cmd, data, ifp, td);
264                 break;
265         }
266         return error;
267 }
268
269 static void
270 in_ialink_dispatch(netmsg_t msg)
271 {
272         struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
273         struct ifaddr_container *ifac;
274         struct in_ifaddr_container *iac;
275         int cpu = mycpuid;
276
277         crit_enter();
278
279         ifac = &ia->ia_ifa.ifa_containers[cpu];
280         ASSERT_IFAC_VALID(ifac);
281         KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
282                 ("ia is on in_ifaddrheads"));
283
284         ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
285         iac = &ifac->ifa_proto_u.u_in_ifac;
286         TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
287
288         crit_exit();
289
290         ifa_forwardmsg(&msg->lmsg, cpu + 1);
291 }
292
293 static void
294 in_iaunlink_dispatch(netmsg_t msg)
295 {
296         struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
297         struct ifaddr_container *ifac;
298         struct in_ifaddr_container *iac;
299         int cpu = mycpuid;
300
301         crit_enter();
302
303         ifac = &ia->ia_ifa.ifa_containers[cpu];
304         ASSERT_IFAC_VALID(ifac);
305         KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
306                 ("ia is not on in_ifaddrheads"));
307
308         iac = &ifac->ifa_proto_u.u_in_ifac;
309         TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
310         ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
311
312         crit_exit();
313
314         ifa_forwardmsg(&msg->lmsg, cpu + 1);
315 }
316
317 static void
318 in_iahashins_dispatch(netmsg_t msg)
319 {
320         struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
321         struct ifaddr_container *ifac;
322         struct in_ifaddr_container *iac;
323         int cpu = mycpuid;
324
325         crit_enter();
326
327         ifac = &ia->ia_ifa.ifa_containers[cpu];
328         ASSERT_IFAC_VALID(ifac);
329         KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
330                 ("ia is on in_ifaddrhashtbls"));
331
332         ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
333         iac = &ifac->ifa_proto_u.u_in_ifac;
334         LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
335                          iac, ia_hash);
336
337         crit_exit();
338
339         ifa_forwardmsg(&msg->lmsg, cpu + 1);
340 }
341
342 static void
343 in_iahashrem_dispatch(netmsg_t msg)
344 {
345         struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
346         struct ifaddr_container *ifac;
347         struct in_ifaddr_container *iac;
348         int cpu = mycpuid;
349
350         crit_enter();
351
352         ifac = &ia->ia_ifa.ifa_containers[cpu];
353         ASSERT_IFAC_VALID(ifac);
354         KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
355                 ("ia is not on in_ifaddrhashtbls"));
356
357         iac = &ifac->ifa_proto_u.u_in_ifac;
358         LIST_REMOVE(iac, ia_hash);
359         ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
360
361         crit_exit();
362
363         ifa_forwardmsg(&msg->lmsg, cpu + 1);
364 }
365
366 static void
367 in_ialink(struct in_ifaddr *ia)
368 {
369         struct netmsg_base msg;
370
371         netmsg_init(&msg, NULL, &curthread->td_msgport,
372                     0, in_ialink_dispatch);
373         msg.lmsg.u.ms_resultp = ia;
374
375         ifa_domsg(&msg.lmsg, 0);
376 }
377
378 void
379 in_iaunlink(struct in_ifaddr *ia)
380 {
381         struct netmsg_base msg;
382
383         netmsg_init(&msg, NULL, &curthread->td_msgport,
384                     0, in_iaunlink_dispatch);
385         msg.lmsg.u.ms_resultp = ia;
386
387         ifa_domsg(&msg.lmsg, 0);
388 }
389
390 void
391 in_iahash_insert(struct in_ifaddr *ia)
392 {
393         struct netmsg_base msg;
394
395         netmsg_init(&msg, NULL, &curthread->td_msgport,
396                     0, in_iahashins_dispatch);
397         msg.lmsg.u.ms_resultp = ia;
398
399         ifa_domsg(&msg.lmsg, 0);
400 }
401
402 void
403 in_iahash_remove(struct in_ifaddr *ia)
404 {
405         struct netmsg_base msg;
406
407         netmsg_init(&msg, NULL, &curthread->td_msgport,
408                     0, in_iahashrem_dispatch);
409         msg.lmsg.u.ms_resultp = ia;
410
411         ifa_domsg(&msg.lmsg, 0);
412 }
413
414 static __inline struct in_ifaddr *
415 in_ianext(struct in_ifaddr *oia)
416 {
417         struct ifaddr_container *ifac;
418         struct in_ifaddr_container *iac;
419
420         ifac = &oia->ia_ifa.ifa_containers[mycpuid];
421         ASSERT_IFAC_VALID(ifac);
422         KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
423                 ("ia is not on in_ifaddrheads"));
424
425         iac = &ifac->ifa_proto_u.u_in_ifac;
426         iac = TAILQ_NEXT(iac, ia_link);
427         if (iac != NULL)
428                 return iac->ia;
429         else
430                 return NULL;
431 }
432
433 static int
434 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
435                     struct thread *td)
436 {
437         struct ifreq *ifr = (struct ifreq *)data;
438         struct in_ifaddr *ia = NULL;
439         struct in_addr dst;
440         struct in_aliasreq *ifra = (struct in_aliasreq *)data;
441         struct ifaddr_container *ifac;
442         struct in_ifaddr_container *iac;
443         struct sockaddr_in oldaddr;
444         int hostIsNew, iaIsNew, maskIsNew, ifpWasUp;
445         int error = 0;
446
447         iaIsNew = 0;
448         ifpWasUp = 0;
449
450         /*
451          * Find address for this interface, if it exists.
452          *
453          * If an alias address was specified, find that one instead of
454          * the first one on the interface, if possible
455          */
456         if (ifp) {
457                 struct in_ifaddr *iap;
458
459                 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
460                 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
461                         iap = iac->ia;
462                         if (iap->ia_ifp == ifp &&
463                             iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
464                                 ia = iap;
465                                 break;
466                         }
467                 }
468                 if (ia == NULL) {
469                         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
470                                       ifa_link) {
471                                 iap = ifatoia(ifac->ifa);
472                                 if (iap->ia_addr.sin_family == AF_INET) {
473                                         ia = iap;
474                                         break;
475                                 }
476                         }
477                 }
478
479                 if (ifp->if_flags & IFF_UP)
480                         ifpWasUp = 1;
481         }
482
483         switch (cmd) {
484         case SIOCAIFADDR:
485         case SIOCDIFADDR:
486                 if (ifp == NULL)
487                         return (EADDRNOTAVAIL);
488                 if (ifra->ifra_addr.sin_family == AF_INET) {
489                         while (ia != NULL) {
490                                 if (ia->ia_ifp == ifp  &&
491                                     ia->ia_addr.sin_addr.s_addr ==
492                                     ifra->ifra_addr.sin_addr.s_addr)
493                                         break;
494                                 ia = in_ianext(ia);
495                         }
496                         if ((ifp->if_flags & IFF_POINTOPOINT) &&
497                             cmd == SIOCAIFADDR &&
498                             ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
499                                 return EDESTADDRREQ;
500                         }
501                 }
502                 if (cmd == SIOCDIFADDR && ia == NULL)
503                         return (EADDRNOTAVAIL);
504                 /* FALLTHROUGH */
505         case SIOCSIFADDR:
506         case SIOCSIFNETMASK:
507         case SIOCSIFDSTADDR:
508                 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
509                         return error;
510
511                 if (ifp == NULL)
512                         return (EADDRNOTAVAIL);
513
514                 if (cmd == SIOCSIFDSTADDR &&
515                     (ifp->if_flags & IFF_POINTOPOINT) == 0)
516                         return (EINVAL);
517
518                 if (ia == NULL) {
519                         struct ifaddr *ifa;
520                         int i;
521
522                         ia = ifa_create(sizeof(*ia), M_WAITOK);
523                         ifa = &ia->ia_ifa;
524
525                         /*
526                          * Setup per-CPU information
527                          */
528                         for (i = 0; i < ncpus; ++i) {
529                                 ifac = &ifa->ifa_containers[i];
530                                 iac = &ifac->ifa_proto_u.u_in_ifac;
531                                 iac->ia = ia;
532                                 iac->ia_ifac = ifac;
533                         }
534
535                         /*
536                          * Protect from NETISR_IP traversing address list
537                          * while we're modifying it.
538                          */
539                         crit_enter();
540
541                         in_ialink(ia);
542                         ifa_iflink(ifa, ifp, 1);
543
544                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
545                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
546                         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
547                         ia->ia_sockmask.sin_len = 8;
548                         ia->ia_sockmask.sin_family = AF_INET;
549                         if (ifp->if_flags & IFF_BROADCAST) {
550                                 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
551                                 ia->ia_broadaddr.sin_family = AF_INET;
552                         }
553                         ia->ia_ifp = ifp;
554                         if (!(ifp->if_flags & IFF_LOOPBACK))
555                                 in_interfaces++;
556                         iaIsNew = 1;
557
558                         crit_exit();
559                 }
560                 break;
561
562         case SIOCSIFBRDADDR:
563                 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
564                         return error;
565                 /* FALLTHROUGH */
566
567         case SIOCGIFADDR:
568         case SIOCGIFNETMASK:
569         case SIOCGIFDSTADDR:
570         case SIOCGIFBRDADDR:
571                 if (ia == NULL)
572                         return (EADDRNOTAVAIL);
573                 break;
574         }
575
576         switch (cmd) {
577         case SIOCGIFADDR:
578                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
579                 return (0);
580
581         case SIOCGIFBRDADDR:
582                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
583                         return (EINVAL);
584                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
585                 return (0);
586
587         case SIOCGIFDSTADDR:
588                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
589                         return (EINVAL);
590                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
591                 return (0);
592
593         case SIOCGIFNETMASK:
594                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
595                 return (0);
596
597         case SIOCSIFDSTADDR:
598                 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
599
600                 oldaddr = ia->ia_dstaddr;
601                 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
602                 if (ifp->if_ioctl != NULL) {
603                         ifnet_serialize_all(ifp);
604                         error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
605                                               td->td_proc->p_ucred);
606                         ifnet_deserialize_all(ifp);
607                         if (error) {
608                                 ia->ia_dstaddr = oldaddr;
609                                 return (error);
610                         }
611                 }
612                 if (ia->ia_flags & IFA_ROUTE) {
613                         ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
614                         rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
615                         ia->ia_ifa.ifa_dstaddr =
616                                         (struct sockaddr *)&ia->ia_dstaddr;
617                         rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
618                 }
619                 return (0);
620
621         case SIOCSIFBRDADDR:
622                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
623                         return (EINVAL);
624                 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
625                 return (0);
626
627         case SIOCSIFADDR:
628                 error = in_ifinit(ifp, ia,
629                     (const struct sockaddr_in *)&ifr->ifr_addr, 1);
630                 if (error != 0 && iaIsNew)
631                         break;
632                 if (error == 0) {
633                         EVENTHANDLER_INVOKE(ifaddr_event, ifp,
634                         iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
635                         &ia->ia_ifa);
636                 }
637                 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
638                         /*
639                          * Interface is brought up by in_ifinit()
640                          * (via ifp->if_ioctl).  We act as if the
641                          * interface got IFF_UP flag turned on.
642                          */
643                         if_up(ifp);
644                 }
645                 return (0);
646
647         case SIOCSIFNETMASK:
648                 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
649                 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
650                 return (0);
651
652         case SIOCAIFADDR:
653                 maskIsNew = 0;
654                 hostIsNew = 1;
655                 error = 0;
656                 if (ia->ia_addr.sin_family == AF_INET) {
657                         if (ifra->ifra_addr.sin_len == 0) {
658                                 ifra->ifra_addr = ia->ia_addr;
659                                 hostIsNew = 0;
660                         } else if (ifra->ifra_addr.sin_addr.s_addr ==
661                                    ia->ia_addr.sin_addr.s_addr) {
662                                 hostIsNew = 0;
663                         }
664                 }
665                 if (ifra->ifra_mask.sin_len) {
666                         in_ifscrub(ifp, ia);
667                         ia->ia_sockmask = ifra->ifra_mask;
668                         ia->ia_sockmask.sin_family = AF_INET;
669                         ia->ia_subnetmask =
670                             ntohl(ia->ia_sockmask.sin_addr.s_addr);
671                         maskIsNew = 1;
672                 }
673                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
674                     ifra->ifra_dstaddr.sin_family == AF_INET) {
675                         in_ifscrub(ifp, ia);
676                         ia->ia_dstaddr = ifra->ifra_dstaddr;
677                         maskIsNew  = 1; /* We lie; but the effect's the same */
678                 }
679                 if (ifra->ifra_addr.sin_family == AF_INET &&
680                     (hostIsNew || maskIsNew))
681                         error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
682
683                 if (error != 0 && iaIsNew)
684                         break;
685
686                 if ((ifp->if_flags & IFF_BROADCAST) &&
687                     ifra->ifra_broadaddr.sin_family == AF_INET)
688                         ia->ia_broadaddr = ifra->ifra_broadaddr;
689                 if (error == 0) {
690                         EVENTHANDLER_INVOKE(ifaddr_event, ifp,
691                         iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
692                         &ia->ia_ifa);
693                 }
694                 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
695                         /* See the comment in SIOCSIFADDR */
696                         if_up(ifp);
697                 }
698                 return (error);
699
700         case SIOCDIFADDR:
701                 /*
702                  * in_ifscrub kills the interface route.
703                  */
704                 in_ifscrub(ifp, ia);
705                 /*
706                  * in_ifadown gets rid of all the rest of
707                  * the routes.  This is not quite the right
708                  * thing to do, but at least if we are running
709                  * a routing process they will come back.
710                  */
711                 in_ifadown(&ia->ia_ifa, 1);
712                 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
713                                     &ia->ia_ifa);
714                 error = 0;
715                 break;
716
717         default:
718                 if (ifp == NULL || ifp->if_ioctl == NULL)
719                         return (EOPNOTSUPP);
720                 ifnet_serialize_all(ifp);
721                 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
722                 ifnet_deserialize_all(ifp);
723                 return (error);
724         }
725
726         KKASSERT(cmd == SIOCDIFADDR ||
727                  ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
728
729         ifa_ifunlink(&ia->ia_ifa, ifp);
730         in_iaunlink(ia);
731
732         if (cmd == SIOCDIFADDR) {
733                 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
734                 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
735                         in_iahash_remove(ia);
736         }
737 #ifdef INVARIANTS
738         else {
739                 /*
740                  * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
741                  * already taken care of the deletion from hash table
742                  */
743                 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
744                 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
745                         ("SIOC%cIFADDR failed on new ia, "
746                          "but the new ia is still in hash table",
747                          cmd == SIOCSIFADDR ? 'S' : 'A'));
748         }
749 #endif
750
751         ifa_destroy(&ia->ia_ifa);
752
753         if ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) &&
754             !ifpWasUp && (ifp->if_flags & IFF_UP)) {
755                 /*
756                  * Though the address assignment failed, the
757                  * interface is brought up by in_ifinit()
758                  * (via ifp->if_ioctl).  With the hope that
759                  * the interface has some valid addresses, we
760                  * act as if IFF_UP flag was just set on the
761                  * interface.
762                  *
763                  * NOTE:
764                  * This could only be done after the failed
765                  * address is unlinked from the global address
766                  * list.
767                  */
768                 if_up(ifp);
769         }
770
771         return (error);
772 }
773
774 /*
775  * SIOC[GAD]LIFADDR.
776  *      SIOCGLIFADDR: get first address. (?!?)
777  *      SIOCGLIFADDR with IFLR_PREFIX:
778  *              get first address that matches the specified prefix.
779  *      SIOCALIFADDR: add the specified address.
780  *      SIOCALIFADDR with IFLR_PREFIX:
781  *              EINVAL since we can't deduce hostid part of the address.
782  *      SIOCDLIFADDR: delete the specified address.
783  *      SIOCDLIFADDR with IFLR_PREFIX:
784  *              delete the first address that matches the specified prefix.
785  * return values:
786  *      EINVAL on invalid parameters
787  *      EADDRNOTAVAIL on prefix match failed/specified address not found
788  *      other values may be returned from in_ioctl()
789  *
790  * NOTE! td might be NULL.
791  */
792 static int
793 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
794                  struct thread *td)
795 {
796         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
797
798         /* sanity checks */
799         if (!data || !ifp) {
800                 panic("invalid argument to in_lifaddr_ioctl");
801                 /*NOTRECHED*/
802         }
803
804         switch (cmd) {
805         case SIOCGLIFADDR:
806                 /* address must be specified on GET with IFLR_PREFIX */
807                 if ((iflr->flags & IFLR_PREFIX) == 0)
808                         break;
809                 /*FALLTHROUGH*/
810         case SIOCALIFADDR:
811         case SIOCDLIFADDR:
812                 /* address must be specified on ADD and DELETE */
813                 if (iflr->addr.ss_family != AF_INET)
814                         return EINVAL;
815                 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
816                         return EINVAL;
817                 /* XXX need improvement */
818                 if (iflr->dstaddr.ss_family
819                  && iflr->dstaddr.ss_family != AF_INET)
820                         return EINVAL;
821                 if (iflr->dstaddr.ss_family
822                  && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
823                         return EINVAL;
824                 break;
825         default: /*shouldn't happen*/
826                 return EOPNOTSUPP;
827         }
828         if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
829                 return EINVAL;
830
831         switch (cmd) {
832         case SIOCALIFADDR:
833             {
834                 struct in_aliasreq ifra;
835
836                 if (iflr->flags & IFLR_PREFIX)
837                         return EINVAL;
838
839                 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
840                 bzero(&ifra, sizeof ifra);
841                 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
842
843                 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
844
845                 if (iflr->dstaddr.ss_family) {  /*XXX*/
846                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
847                                 iflr->dstaddr.ss_len);
848                 }
849
850                 ifra.ifra_mask.sin_family = AF_INET;
851                 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
852                 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
853
854                 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
855             }
856         case SIOCGLIFADDR:
857         case SIOCDLIFADDR:
858             {
859                 struct ifaddr_container *ifac;
860                 struct in_ifaddr *ia;
861                 struct in_addr mask, candidate, match;
862                 struct sockaddr_in *sin;
863                 int cmp;
864
865                 bzero(&mask, sizeof mask);
866                 if (iflr->flags & IFLR_PREFIX) {
867                         /* lookup a prefix rather than address. */
868                         in_len2mask(&mask, iflr->prefixlen);
869
870                         sin = (struct sockaddr_in *)&iflr->addr;
871                         match.s_addr = sin->sin_addr.s_addr;
872                         match.s_addr &= mask.s_addr;
873
874                         /* if you set extra bits, that's wrong */
875                         if (match.s_addr != sin->sin_addr.s_addr)
876                                 return EINVAL;
877
878                         cmp = 1;
879                 } else {
880                         if (cmd == SIOCGLIFADDR) {
881                                 /* on getting an address, take the 1st match */
882                                 match.s_addr = 0; /* gcc4 warning */
883                                 cmp = 0;        /*XXX*/
884                         } else {
885                                 /* on deleting an address, do exact match */
886                                 in_len2mask(&mask, 32);
887                                 sin = (struct sockaddr_in *)&iflr->addr;
888                                 match.s_addr = sin->sin_addr.s_addr;
889
890                                 cmp = 1;
891                         }
892                 }
893
894                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
895                         struct ifaddr *ifa = ifac->ifa;
896
897                         if (ifa->ifa_addr->sa_family != AF_INET6)
898                                 continue;
899                         if (!cmp)
900                                 break;
901                         candidate.s_addr =
902                         ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
903                         candidate.s_addr &= mask.s_addr;
904                         if (candidate.s_addr == match.s_addr)
905                                 break;
906                 }
907                 if (ifac == NULL)
908                         return EADDRNOTAVAIL;
909                 ia = (struct in_ifaddr *)(ifac->ifa);
910
911                 if (cmd == SIOCGLIFADDR) {
912                         /* fill in the if_laddrreq structure */
913                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
914
915                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
916                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
917                                         ia->ia_dstaddr.sin_len);
918                         } else
919                                 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
920
921                         iflr->prefixlen =
922                                 in_mask2len(&ia->ia_sockmask.sin_addr);
923
924                         iflr->flags = 0;        /*XXX*/
925
926                         return 0;
927                 } else {
928                         struct in_aliasreq ifra;
929
930                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
931                         bzero(&ifra, sizeof ifra);
932                         bcopy(iflr->iflr_name, ifra.ifra_name,
933                                 sizeof ifra.ifra_name);
934
935                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
936                                 ia->ia_addr.sin_len);
937                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
938                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
939                                         ia->ia_dstaddr.sin_len);
940                         }
941                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
942                                 ia->ia_sockmask.sin_len);
943
944                         return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
945                                           ifp, td);
946                 }
947             }
948         }
949
950         return EOPNOTSUPP;      /*just for safety*/
951 }
952
953 /*
954  * Delete any existing route for an interface.
955  */
956 void
957 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
958 {
959         in_scrubprefix(ia);
960 }
961
962 /*
963  * Initialize an interface's internet address
964  * and routing table entry.
965  */
966 static int
967 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
968           const struct sockaddr_in *sin, int scrub)
969 {
970         u_long i = ntohl(sin->sin_addr.s_addr);
971         struct sockaddr_in oldaddr;
972         struct ifaddr_container *ifac;
973         int flags = RTF_UP, error = 0;
974         int was_hash = 0;
975
976         ifac = &ia->ia_ifa.ifa_containers[mycpuid];
977         oldaddr = ia->ia_addr;
978
979         if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
980                 was_hash = 1;
981                 in_iahash_remove(ia);
982         }
983
984         ia->ia_addr = *sin;
985         if (ia->ia_addr.sin_family == AF_INET)
986                 in_iahash_insert(ia);
987
988         /*
989          * Give the interface a chance to initialize
990          * if this is its first address,
991          * and to validate the address if necessary.
992          */
993         if (ifp->if_ioctl != NULL) {
994                 ifnet_serialize_all(ifp);
995                 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
996                 ifnet_deserialize_all(ifp);
997                 if (error)
998                         goto fail;
999         }
1000
1001         /*
1002          * Delete old route, if requested.
1003          */
1004         if (scrub) {
1005                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1006                 in_ifscrub(ifp, ia);
1007                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1008         }
1009
1010         /*
1011          * Calculate netmask/subnetmask.
1012          */
1013         if (IN_CLASSA(i))
1014                 ia->ia_netmask = IN_CLASSA_NET;
1015         else if (IN_CLASSB(i))
1016                 ia->ia_netmask = IN_CLASSB_NET;
1017         else
1018                 ia->ia_netmask = IN_CLASSC_NET;
1019         /*
1020          * The subnet mask usually includes at least the standard network part,
1021          * but may may be smaller in the case of supernetting.
1022          * If it is set, we believe it.
1023          */
1024         if (ia->ia_subnetmask == 0) {
1025                 ia->ia_subnetmask = ia->ia_netmask;
1026                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1027         } else {
1028                 ia->ia_netmask &= ia->ia_subnetmask;
1029         }
1030         ia->ia_net = i & ia->ia_netmask;
1031         ia->ia_subnet = i & ia->ia_subnetmask;
1032         in_socktrim(&ia->ia_sockmask);
1033
1034         /*
1035          * Add route for the network.
1036          */
1037         ia->ia_ifa.ifa_metric = ifp->if_metric;
1038         if (ifp->if_flags & IFF_BROADCAST) {
1039                 ia->ia_broadaddr.sin_addr.s_addr =
1040                         htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1041                 ia->ia_netbroadcast.s_addr =
1042                         htonl(ia->ia_net | ~ ia->ia_netmask);
1043         } else if (ifp->if_flags & IFF_LOOPBACK) {
1044                 ia->ia_dstaddr = ia->ia_addr;
1045                 flags |= RTF_HOST;
1046         } else if (ifp->if_flags & IFF_POINTOPOINT) {
1047                 if (ia->ia_dstaddr.sin_family != AF_INET)
1048                         return (0);
1049                 flags |= RTF_HOST;
1050         }
1051
1052         /*-
1053          * Don't add host routes for interface addresses of
1054          * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
1055          * possible to assign several such address pairs with consistent
1056          * results (no host route) and is required by BOOTP.
1057          *
1058          * XXX: This is ugly !  There should be a way for the caller to
1059          *      say that they don't want a host route.
1060          */
1061         if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1062             ia->ia_netmask != IN_CLASSA_NET ||
1063             ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1064                 error = in_addprefix(ia, flags);
1065                 if (error)
1066                         goto fail;
1067         }
1068
1069         /*
1070          * If the interface supports multicast, join the "all hosts"
1071          * multicast group on that interface.
1072          */
1073         if (ifp->if_flags & IFF_MULTICAST) {
1074                 struct in_addr addr;
1075
1076                 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1077                 in_addmulti(&addr, ifp);
1078         }
1079         return (0);
1080 fail:
1081         if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1082                 in_iahash_remove(ia);
1083
1084         ia->ia_addr = oldaddr;
1085         if (was_hash)
1086                 in_iahash_insert(ia);
1087         return (error);
1088 }
1089
1090 #define rtinitflags(x) \
1091         (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1092          ? RTF_HOST : 0)
1093
1094 /*
1095  * Add a route to prefix ("connected route" in cisco terminology).
1096  * Do nothing, if there are some interface addresses with the same
1097  * prefix already.  This function assumes that the 'target' parent
1098  * interface is UP.
1099  */
1100 static int
1101 in_addprefix(struct in_ifaddr *target, int flags)
1102 {
1103         struct in_ifaddr_container *iac;
1104         struct in_addr prefix, mask;
1105         int error;
1106
1107 #ifdef CARP
1108         /*
1109          * Don't add prefix routes for CARP interfaces.
1110          * Prefix routes creation is handled by CARP
1111          * interfaces themselves.
1112          */
1113         if (target->ia_ifp->if_type == IFT_CARP)
1114                 return 0;
1115 #endif
1116
1117         mask = target->ia_sockmask.sin_addr;
1118         if (flags & RTF_HOST) {
1119                 prefix = target->ia_dstaddr.sin_addr;
1120         } else {
1121                 prefix = target->ia_addr.sin_addr;
1122                 prefix.s_addr &= mask.s_addr;
1123         }
1124
1125         TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1126                 struct in_ifaddr *ia = iac->ia;
1127                 struct in_addr p;
1128
1129                 /* Don't test against self */
1130                 if (ia == target)
1131                         continue;
1132
1133                 /* The tested address does not own a route entry */
1134                 if ((ia->ia_flags & IFA_ROUTE) == 0)
1135                         continue;
1136
1137                 /* Prefix test */
1138                 if (rtinitflags(ia)) {
1139                         p = ia->ia_dstaddr.sin_addr;
1140                 } else {
1141                         p = ia->ia_addr.sin_addr;
1142                         p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1143                 }
1144                 if (prefix.s_addr != p.s_addr)
1145                         continue;
1146
1147                 /*
1148                  * If the to-be-added address and the curretly being
1149                  * tested address are not host addresses, we need to
1150                  * take subnetmask into consideration.
1151                  */
1152                 if (!(flags & RTF_HOST) && !rtinitflags(ia) &&
1153                     mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
1154                         continue;
1155
1156                 /*
1157                  * If we got a matching prefix route inserted by other
1158                  * interface address, we don't need to bother.
1159                  */
1160                 return 0;
1161         }
1162
1163         /*
1164          * No one seem to have prefix route; insert it.
1165          */
1166         error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1167         if (!error)
1168                 target->ia_flags |= IFA_ROUTE;
1169         return error;
1170 }
1171
1172 /*
1173  * Remove a route to prefix ("connected route" in cisco terminology).
1174  * Re-installs the route by using another interface address, if there's
1175  * one with the same prefix (otherwise we lose the route mistakenly).
1176  */
1177 static void
1178 in_scrubprefix(struct in_ifaddr *target)
1179 {
1180         struct in_ifaddr_container *iac;
1181         struct in_addr prefix, mask;
1182         int error;
1183
1184 #ifdef CARP
1185         /*
1186          * Don't scrub prefix routes for CARP interfaces.
1187          * Prefix routes deletion is handled by CARP
1188          * interfaces themselves.
1189          */
1190         if (target->ia_ifp->if_type == IFT_CARP)
1191                 return;
1192 #endif
1193
1194         if ((target->ia_flags & IFA_ROUTE) == 0)
1195                 return;
1196
1197         mask = target->ia_sockmask.sin_addr;
1198         if (rtinitflags(target)) {
1199                 prefix = target->ia_dstaddr.sin_addr;
1200         } else {
1201                 prefix = target->ia_addr.sin_addr;
1202                 prefix.s_addr &= mask.s_addr;
1203         }
1204
1205         TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1206                 struct in_ifaddr *ia = iac->ia;
1207                 struct in_addr p;
1208
1209                 /* Don't test against self */
1210                 if (ia == target)
1211                         continue;
1212
1213                 /* The tested address already owns a route entry */
1214                 if (ia->ia_flags & IFA_ROUTE)
1215                         continue;
1216
1217                 /*
1218                  * The prefix route of the tested address should
1219                  * never be installed if its parent interface is
1220                  * not UP yet.
1221                  */
1222                 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1223                         continue;
1224
1225 #ifdef CARP
1226                 /*
1227                  * Don't add prefix routes for CARP interfaces.
1228                  * Prefix routes creation is handled by CARP
1229                  * interfaces themselves.
1230                  */
1231                 if (ia->ia_ifp->if_type == IFT_CARP)
1232                         continue;
1233 #endif
1234
1235                 /* Prefix test */
1236                 if (rtinitflags(ia)) {
1237                         p = ia->ia_dstaddr.sin_addr;
1238                 } else {
1239                         p = ia->ia_addr.sin_addr;
1240                         p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1241                 }
1242                 if (prefix.s_addr != p.s_addr)
1243                         continue;
1244
1245                 /*
1246                  * We don't need to test subnetmask here, as what we do
1247                  * in in_addprefix(), since if the the tested address's
1248                  * parent interface is UP, the tested address should own
1249                  * a prefix route entry and we would never reach here.
1250                  */
1251
1252                 /*
1253                  * If we got a matching prefix route, move IFA_ROUTE to him
1254                  */
1255                 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1256                 target->ia_flags &= ~IFA_ROUTE;
1257
1258                 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
1259                 if (!error)
1260                         ia->ia_flags |= IFA_ROUTE;
1261                 return;
1262         }
1263
1264         /*
1265          * No candidates for this prefix route; just remove it.
1266          */
1267         rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1268         target->ia_flags &= ~IFA_ROUTE;
1269 }
1270
1271 #undef rtinitflags
1272
1273 /*
1274  * Return 1 if the address might be a local broadcast address.
1275  */
1276 int
1277 in_broadcast(struct in_addr in, struct ifnet *ifp)
1278 {
1279         struct ifaddr_container *ifac;
1280         u_long t;
1281
1282         if (in.s_addr == INADDR_BROADCAST ||
1283             in.s_addr == INADDR_ANY)
1284                 return 1;
1285         if (ifp == NULL || (ifp->if_flags & IFF_BROADCAST) == 0)
1286                 return 0;
1287         t = ntohl(in.s_addr);
1288         /*
1289          * Look through the list of addresses for a match
1290          * with a broadcast address.
1291          */
1292 #define ia ((struct in_ifaddr *)ifa)
1293         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1294                 struct ifaddr *ifa = ifac->ifa;
1295
1296                 if (ifa->ifa_addr->sa_family == AF_INET &&
1297                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1298                      in.s_addr == ia->ia_netbroadcast.s_addr ||
1299                      /*
1300                       * Check for old-style (host 0) broadcast.
1301                       */
1302                      t == ia->ia_subnet || t == ia->ia_net) &&
1303                      /*
1304                       * Check for an all one subnetmask. These
1305                       * only exist when an interface gets a secondary
1306                       * address.
1307                       */
1308                      ia->ia_subnetmask != (u_long)0xffffffff)
1309                             return 1;
1310         }
1311         return (0);
1312 #undef ia
1313 }
1314
1315 /*
1316  * Add an address to the list of IP multicast addresses for a given interface.
1317  */
1318 struct in_multi *
1319 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1320 {
1321         struct in_multi *inm;
1322         int error;
1323         struct sockaddr_in sin;
1324         struct ifmultiaddr *ifma;
1325
1326         KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1327             ("in_addmulti is not called in netisr0"));
1328
1329         /*
1330          * Call generic routine to add membership or increment
1331          * refcount.  It wants addresses in the form of a sockaddr,
1332          * so we build one here (being careful to zero the unused bytes).
1333          */
1334         bzero(&sin, sizeof sin);
1335         sin.sin_family = AF_INET;
1336         sin.sin_len = sizeof sin;
1337         sin.sin_addr = *ap;
1338         error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1339         if (error)
1340                 return NULL;
1341
1342         /*
1343          * If ifma->ifma_protospec is null, then if_addmulti() created
1344          * a new record.  Otherwise, we are done.
1345          */
1346         if (ifma->ifma_protospec != NULL)
1347                 return ifma->ifma_protospec;
1348
1349         inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
1350         inm->inm_addr = *ap;
1351         inm->inm_ifp = ifp;
1352         inm->inm_ifma = ifma;
1353         ifma->ifma_protospec = inm;
1354         LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1355
1356         /*
1357          * Let IGMP know that we have joined a new IP multicast group.
1358          */
1359         igmp_joingroup(inm);
1360         return inm;
1361 }
1362
1363 /*
1364  * Delete a multicast address record.
1365  */
1366 void
1367 in_delmulti(struct in_multi *inm)
1368 {
1369         struct ifmultiaddr *ifma;
1370         struct in_multi my_inm;
1371
1372         KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1373             ("in_delmulti is not called in netisr0"));
1374
1375         ifma = inm->inm_ifma;
1376         my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1377         if (ifma->ifma_refcount == 1) {
1378                 /*
1379                  * No remaining claims to this record; let IGMP know that
1380                  * we are leaving the multicast group.
1381                  * But do it after the if_delmulti() which might reset
1382                  * the interface and nuke the packet.
1383                  */
1384                 my_inm = *inm ;
1385                 ifma->ifma_protospec = NULL;
1386                 LIST_REMOVE(inm, inm_link);
1387                 kfree(inm, M_IPMADDR);
1388         }
1389         /* XXX - should be separate API for when we have an ifma? */
1390         if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1391         if (my_inm.inm_ifp != NULL)
1392                 igmp_leavegroup(&my_inm);
1393 }
1394
1395 static void
1396 in_ifdetach_dispatch(netmsg_t nmsg)
1397 {
1398         struct lwkt_msg *lmsg = &nmsg->lmsg;
1399         struct ifnet *ifp = lmsg->u.ms_resultp;
1400         int cpu;
1401
1402         in_pcbpurgeif0(&ripcbinfo, ifp);
1403         for (cpu = 0; cpu < ncpus2; ++cpu)
1404                 in_pcbpurgeif0(&udbinfo[cpu], ifp);
1405
1406         lwkt_replymsg(lmsg, 0);
1407 }
1408
1409 void
1410 in_ifdetach(struct ifnet *ifp)
1411 {
1412         struct netmsg_base nmsg;
1413         struct lwkt_msg *lmsg = &nmsg.lmsg;
1414
1415         netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
1416             in_ifdetach_dispatch);
1417         lmsg->u.ms_resultp = ifp;
1418
1419         lwkt_domsg(netisr_cpuport(0), lmsg, 0);
1420 }