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