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