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