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