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