]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/netinet/in.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / sys / netinet / in.c
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (C) 2001 WIDE Project.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      @(#)in.c        8.4 (Berkeley) 1/9/95
31  * $FreeBSD$
32  */
33
34 #include "opt_carp.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43
44 #include <net/if.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
47
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/in_pcb.h>
51
52 #include <netinet/igmp_var.h>
53
54 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
55
56 static int in_mask2len(struct in_addr *);
57 static void in_len2mask(struct in_addr *, int);
58 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
59         struct ifnet *, struct thread *);
60
61 static int      in_addprefix(struct in_ifaddr *, int);
62 static void     in_delmulti_locked(register struct in_multi *, int);
63 static int      in_scrubprefix(struct in_ifaddr *);
64 static void     in_socktrim(struct sockaddr_in *);
65 static int      in_ifinit(struct ifnet *,
66             struct in_ifaddr *, struct sockaddr_in *, int);
67
68 static int subnetsarelocal = 0;
69 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
70         &subnetsarelocal, 0, "Treat all subnets as directly connected");
71 static int sameprefixcarponly = 0;
72 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
73         &sameprefixcarponly, 0,
74         "Refuse to create same prefixes on different interfaces");
75
76 /*
77  * The IPv4 multicast list (in_multihead and associated structures) are
78  * protected by the global in_multi_mtx.  See in_var.h for more details.  For
79  * now, in_multi_mtx is marked as recursible due to IGMP's calling back into
80  * ip_output() to send IGMP packets while holding the lock; this probably is
81  * not quite desirable.
82  */
83 struct in_multihead in_multihead; /* XXX BSS initialization */
84 struct mtx in_multi_mtx;
85 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE);
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(in)
98         struct in_addr in;
99 {
100         register u_long i = ntohl(in.s_addr);
101         register struct in_ifaddr *ia;
102
103         if (subnetsarelocal) {
104                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
105                         if ((i & ia->ia_netmask) == ia->ia_net)
106                                 return (1);
107         } else {
108                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
109                         if ((i & ia->ia_subnetmask) == ia->ia_subnet)
110                                 return (1);
111         }
112         return (0);
113 }
114
115 /*
116  * Return 1 if an internet address is for the local host and configured
117  * on one of its interfaces.
118  */
119 int
120 in_localip(in)
121         struct in_addr in;
122 {
123         struct in_ifaddr *ia;
124
125         LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
126                 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
127                         return 1;
128         }
129         return 0;
130 }
131
132 /*
133  * Determine whether an IP address is in a reserved set of addresses
134  * that may not be forwarded, or whether datagrams to that destination
135  * may be forwarded.
136  */
137 int
138 in_canforward(in)
139         struct in_addr in;
140 {
141         register u_long i = ntohl(in.s_addr);
142         register u_long net;
143
144         if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
145                 return (0);
146         if (IN_CLASSA(i)) {
147                 net = i & IN_CLASSA_NET;
148                 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
149                         return (0);
150         }
151         return (1);
152 }
153
154 /*
155  * Trim a mask in a sockaddr
156  */
157 static void
158 in_socktrim(ap)
159 struct sockaddr_in *ap;
160 {
161     register char *cplim = (char *) &ap->sin_addr;
162     register char *cp = (char *) (&ap->sin_addr + 1);
163
164     ap->sin_len = 0;
165     while (--cp >= cplim)
166         if (*cp) {
167             (ap)->sin_len = cp - (char *) (ap) + 1;
168             break;
169         }
170 }
171
172 static int
173 in_mask2len(mask)
174         struct in_addr *mask;
175 {
176         int x, y;
177         u_char *p;
178
179         p = (u_char *)mask;
180         for (x = 0; x < sizeof(*mask); x++) {
181                 if (p[x] != 0xff)
182                         break;
183         }
184         y = 0;
185         if (x < sizeof(*mask)) {
186                 for (y = 0; y < 8; y++) {
187                         if ((p[x] & (0x80 >> y)) == 0)
188                                 break;
189                 }
190         }
191         return x * 8 + y;
192 }
193
194 static void
195 in_len2mask(mask, len)
196         struct in_addr *mask;
197         int len;
198 {
199         int i;
200         u_char *p;
201
202         p = (u_char *)mask;
203         bzero(mask, sizeof(*mask));
204         for (i = 0; i < len / 8; i++)
205                 p[i] = 0xff;
206         if (len % 8)
207                 p[i] = (0xff00 >> (len % 8)) & 0xff;
208 }
209
210 /*
211  * Generic internet control operations (ioctl's).
212  * Ifp is 0 if not an interface-specific ioctl.
213  */
214 /* ARGSUSED */
215 int
216 in_control(so, cmd, data, ifp, td)
217         struct socket *so;
218         u_long cmd;
219         caddr_t data;
220         register struct ifnet *ifp;
221         struct thread *td;
222 {
223         register struct ifreq *ifr = (struct ifreq *)data;
224         register struct in_ifaddr *ia = 0, *iap;
225         register struct ifaddr *ifa;
226         struct in_addr dst;
227         struct in_ifaddr *oia;
228         struct in_aliasreq *ifra = (struct in_aliasreq *)data;
229         struct sockaddr_in oldaddr;
230         int error, hostIsNew, iaIsNew, maskIsNew, s;
231
232         iaIsNew = 0;
233
234         switch (cmd) {
235         case SIOCALIFADDR:
236         case SIOCDLIFADDR:
237                 if (td && (error = suser(td)) != 0)
238                         return error;
239                 /*fall through*/
240         case SIOCGLIFADDR:
241                 if (!ifp)
242                         return EINVAL;
243                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
244         }
245
246         /*
247          * Find address for this interface, if it exists.
248          *
249          * If an alias address was specified, find that one instead of
250          * the first one on the interface, if possible.
251          */
252         if (ifp) {
253                 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
254                 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
255                         if (iap->ia_ifp == ifp &&
256                             iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
257                                 ia = iap;
258                                 break;
259                         }
260                 if (ia == NULL)
261                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
262                                 iap = ifatoia(ifa);
263                                 if (iap->ia_addr.sin_family == AF_INET) {
264                                         ia = iap;
265                                         break;
266                                 }
267                         }
268         }
269
270         switch (cmd) {
271
272         case SIOCAIFADDR:
273         case SIOCDIFADDR:
274                 if (ifp == 0)
275                         return (EADDRNOTAVAIL);
276                 if (ifra->ifra_addr.sin_family == AF_INET) {
277                         for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
278                                 if (ia->ia_ifp == ifp  &&
279                                     ia->ia_addr.sin_addr.s_addr ==
280                                     ifra->ifra_addr.sin_addr.s_addr)
281                                         break;
282                         }
283                         if ((ifp->if_flags & IFF_POINTOPOINT)
284                             && (cmd == SIOCAIFADDR)
285                             && (ifra->ifra_dstaddr.sin_addr.s_addr
286                                 == INADDR_ANY)) {
287                                 return EDESTADDRREQ;
288                         }
289                 }
290                 if (cmd == SIOCDIFADDR && ia == 0)
291                         return (EADDRNOTAVAIL);
292                 /* FALLTHROUGH */
293         case SIOCSIFADDR:
294         case SIOCSIFNETMASK:
295         case SIOCSIFDSTADDR:
296                 if (td && (error = suser(td)) != 0)
297                         return error;
298
299                 if (ifp == 0)
300                         return (EADDRNOTAVAIL);
301                 if (ia == (struct in_ifaddr *)0) {
302                         ia = (struct in_ifaddr *)
303                                 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
304                         if (ia == (struct in_ifaddr *)NULL)
305                                 return (ENOBUFS);
306                         /*
307                          * Protect from ipintr() traversing address list
308                          * while we're modifying it.
309                          */
310                         s = splnet();
311                         ifa = &ia->ia_ifa;
312                         IFA_LOCK_INIT(ifa);
313                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
314                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
315                         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
316                         ifa->ifa_refcnt = 1;
317                         TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
318
319                         ia->ia_sockmask.sin_len = 8;
320                         ia->ia_sockmask.sin_family = AF_INET;
321                         if (ifp->if_flags & IFF_BROADCAST) {
322                                 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
323                                 ia->ia_broadaddr.sin_family = AF_INET;
324                         }
325                         ia->ia_ifp = ifp;
326
327                         TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
328                         splx(s);
329                         iaIsNew = 1;
330                 }
331                 break;
332
333         case SIOCSIFBRDADDR:
334                 if (td && (error = suser(td)) != 0)
335                         return error;
336                 /* FALLTHROUGH */
337
338         case SIOCGIFADDR:
339         case SIOCGIFNETMASK:
340         case SIOCGIFDSTADDR:
341         case SIOCGIFBRDADDR:
342                 if (ia == (struct in_ifaddr *)0)
343                         return (EADDRNOTAVAIL);
344                 break;
345         }
346         switch (cmd) {
347
348         case SIOCGIFADDR:
349                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
350                 return (0);
351
352         case SIOCGIFBRDADDR:
353                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
354                         return (EINVAL);
355                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
356                 return (0);
357
358         case SIOCGIFDSTADDR:
359                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
360                         return (EINVAL);
361                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
362                 return (0);
363
364         case SIOCGIFNETMASK:
365                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
366                 return (0);
367
368         case SIOCSIFDSTADDR:
369                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
370                         return (EINVAL);
371                 oldaddr = ia->ia_dstaddr;
372                 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
373                 if (ifp->if_ioctl) {
374                         IFF_LOCKGIANT(ifp);
375                         error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
376                             (caddr_t)ia);
377                         IFF_UNLOCKGIANT(ifp);
378                         if (error) {
379                                 ia->ia_dstaddr = oldaddr;
380                                 return (error);
381                         }
382                 }
383                 if (ia->ia_flags & IFA_ROUTE) {
384                         ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
385                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
386                         ia->ia_ifa.ifa_dstaddr =
387                                         (struct sockaddr *)&ia->ia_dstaddr;
388                         rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
389                 }
390                 return (0);
391
392         case SIOCSIFBRDADDR:
393                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
394                         return (EINVAL);
395                 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
396                 return (0);
397
398         case SIOCSIFADDR:
399                 error = in_ifinit(ifp, ia,
400                     (struct sockaddr_in *) &ifr->ifr_addr, 1);
401                 if (error != 0 && iaIsNew)
402                         break;
403                 if (error == 0)
404                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
405                 return (0);
406
407         case SIOCSIFNETMASK:
408                 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
409                 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
410                 return (0);
411
412         case SIOCAIFADDR:
413                 maskIsNew = 0;
414                 hostIsNew = 1;
415                 error = 0;
416                 if (ia->ia_addr.sin_family == AF_INET) {
417                         if (ifra->ifra_addr.sin_len == 0) {
418                                 ifra->ifra_addr = ia->ia_addr;
419                                 hostIsNew = 0;
420                         } else if (ifra->ifra_addr.sin_addr.s_addr ==
421                                                ia->ia_addr.sin_addr.s_addr)
422                                 hostIsNew = 0;
423                 }
424                 if (ifra->ifra_mask.sin_len) {
425                         in_ifscrub(ifp, ia);
426                         ia->ia_sockmask = ifra->ifra_mask;
427                         ia->ia_sockmask.sin_family = AF_INET;
428                         ia->ia_subnetmask =
429                              ntohl(ia->ia_sockmask.sin_addr.s_addr);
430                         maskIsNew = 1;
431                 }
432                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
433                     (ifra->ifra_dstaddr.sin_family == AF_INET)) {
434                         in_ifscrub(ifp, ia);
435                         ia->ia_dstaddr = ifra->ifra_dstaddr;
436                         maskIsNew  = 1; /* We lie; but the effect's the same */
437                 }
438                 if (ifra->ifra_addr.sin_family == AF_INET &&
439                     (hostIsNew || maskIsNew))
440                         error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
441                 if (error != 0 && iaIsNew)
442                         break;
443
444                 if ((ifp->if_flags & IFF_BROADCAST) &&
445                     (ifra->ifra_broadaddr.sin_family == AF_INET))
446                         ia->ia_broadaddr = ifra->ifra_broadaddr;
447                 if (error == 0)
448                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
449                 return (error);
450
451         case SIOCDIFADDR:
452                 /*
453                  * in_ifscrub kills the interface route.
454                  */
455                 in_ifscrub(ifp, ia);
456                 /*
457                  * in_ifadown gets rid of all the rest of
458                  * the routes.  This is not quite the right
459                  * thing to do, but at least if we are running
460                  * a routing process they will come back.
461                  */
462                 in_ifadown(&ia->ia_ifa, 1);
463                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
464                 error = 0;
465                 break;
466
467         default:
468                 if (ifp == 0 || ifp->if_ioctl == 0)
469                         return (EOPNOTSUPP);
470                 IFF_LOCKGIANT(ifp);
471                 error = (*ifp->if_ioctl)(ifp, cmd, data);
472                 IFF_UNLOCKGIANT(ifp);
473                 return (error);
474         }
475
476         /*
477          * Protect from ipintr() traversing address list while we're modifying
478          * it.
479          */
480         s = splnet();
481         TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
482         TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
483         if (ia->ia_addr.sin_family == AF_INET)
484                 LIST_REMOVE(ia, ia_hash);
485         IFAFREE(&ia->ia_ifa);
486         splx(s);
487
488         return (error);
489 }
490
491 /*
492  * SIOC[GAD]LIFADDR.
493  *      SIOCGLIFADDR: get first address. (?!?)
494  *      SIOCGLIFADDR with IFLR_PREFIX:
495  *              get first address that matches the specified prefix.
496  *      SIOCALIFADDR: add the specified address.
497  *      SIOCALIFADDR with IFLR_PREFIX:
498  *              EINVAL since we can't deduce hostid part of the address.
499  *      SIOCDLIFADDR: delete the specified address.
500  *      SIOCDLIFADDR with IFLR_PREFIX:
501  *              delete the first address that matches the specified prefix.
502  * return values:
503  *      EINVAL on invalid parameters
504  *      EADDRNOTAVAIL on prefix match failed/specified address not found
505  *      other values may be returned from in_ioctl()
506  */
507 static int
508 in_lifaddr_ioctl(so, cmd, data, ifp, td)
509         struct socket *so;
510         u_long cmd;
511         caddr_t data;
512         struct ifnet *ifp;
513         struct thread *td;
514 {
515         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
516         struct ifaddr *ifa;
517
518         /* sanity checks */
519         if (!data || !ifp) {
520                 panic("invalid argument to in_lifaddr_ioctl");
521                 /*NOTRECHED*/
522         }
523
524         switch (cmd) {
525         case SIOCGLIFADDR:
526                 /* address must be specified on GET with IFLR_PREFIX */
527                 if ((iflr->flags & IFLR_PREFIX) == 0)
528                         break;
529                 /*FALLTHROUGH*/
530         case SIOCALIFADDR:
531         case SIOCDLIFADDR:
532                 /* address must be specified on ADD and DELETE */
533                 if (iflr->addr.ss_family != AF_INET)
534                         return EINVAL;
535                 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
536                         return EINVAL;
537                 /* XXX need improvement */
538                 if (iflr->dstaddr.ss_family
539                  && iflr->dstaddr.ss_family != AF_INET)
540                         return EINVAL;
541                 if (iflr->dstaddr.ss_family
542                  && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
543                         return EINVAL;
544                 break;
545         default: /*shouldn't happen*/
546                 return EOPNOTSUPP;
547         }
548         if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
549                 return EINVAL;
550
551         switch (cmd) {
552         case SIOCALIFADDR:
553             {
554                 struct in_aliasreq ifra;
555
556                 if (iflr->flags & IFLR_PREFIX)
557                         return EINVAL;
558
559                 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
560                 bzero(&ifra, sizeof(ifra));
561                 bcopy(iflr->iflr_name, ifra.ifra_name,
562                         sizeof(ifra.ifra_name));
563
564                 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
565
566                 if (iflr->dstaddr.ss_family) {  /*XXX*/
567                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
568                                 iflr->dstaddr.ss_len);
569                 }
570
571                 ifra.ifra_mask.sin_family = AF_INET;
572                 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
573                 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
574
575                 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
576             }
577         case SIOCGLIFADDR:
578         case SIOCDLIFADDR:
579             {
580                 struct in_ifaddr *ia;
581                 struct in_addr mask, candidate, match;
582                 struct sockaddr_in *sin;
583                 int cmp;
584
585                 bzero(&mask, sizeof(mask));
586                 if (iflr->flags & IFLR_PREFIX) {
587                         /* lookup a prefix rather than address. */
588                         in_len2mask(&mask, iflr->prefixlen);
589
590                         sin = (struct sockaddr_in *)&iflr->addr;
591                         match.s_addr = sin->sin_addr.s_addr;
592                         match.s_addr &= mask.s_addr;
593
594                         /* if you set extra bits, that's wrong */
595                         if (match.s_addr != sin->sin_addr.s_addr)
596                                 return EINVAL;
597
598                         cmp = 1;
599                 } else {
600                         if (cmd == SIOCGLIFADDR) {
601                                 /* on getting an address, take the 1st match */
602                                 cmp = 0;        /*XXX*/
603                         } else {
604                                 /* on deleting an address, do exact match */
605                                 in_len2mask(&mask, 32);
606                                 sin = (struct sockaddr_in *)&iflr->addr;
607                                 match.s_addr = sin->sin_addr.s_addr;
608
609                                 cmp = 1;
610                         }
611                 }
612
613                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
614                         if (ifa->ifa_addr->sa_family != AF_INET6)
615                                 continue;
616                         if (!cmp)
617                                 break;
618                         candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
619                         candidate.s_addr &= mask.s_addr;
620                         if (candidate.s_addr == match.s_addr)
621                                 break;
622                 }
623                 if (!ifa)
624                         return EADDRNOTAVAIL;
625                 ia = (struct in_ifaddr *)ifa;
626
627                 if (cmd == SIOCGLIFADDR) {
628                         /* fill in the if_laddrreq structure */
629                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
630
631                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
632                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
633                                         ia->ia_dstaddr.sin_len);
634                         } else
635                                 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
636
637                         iflr->prefixlen =
638                                 in_mask2len(&ia->ia_sockmask.sin_addr);
639
640                         iflr->flags = 0;        /*XXX*/
641
642                         return 0;
643                 } else {
644                         struct in_aliasreq ifra;
645
646                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
647                         bzero(&ifra, sizeof(ifra));
648                         bcopy(iflr->iflr_name, ifra.ifra_name,
649                                 sizeof(ifra.ifra_name));
650
651                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
652                                 ia->ia_addr.sin_len);
653                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
654                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
655                                         ia->ia_dstaddr.sin_len);
656                         }
657                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
658                                 ia->ia_sockmask.sin_len);
659
660                         return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
661                                           ifp, td);
662                 }
663             }
664         }
665
666         return EOPNOTSUPP;      /*just for safety*/
667 }
668
669 /*
670  * Delete any existing route for an interface.
671  */
672 void
673 in_ifscrub(ifp, ia)
674         register struct ifnet *ifp;
675         register struct in_ifaddr *ia;
676 {
677         in_scrubprefix(ia);
678 }
679
680 /*
681  * Initialize an interface's internet address
682  * and routing table entry.
683  */
684 static int
685 in_ifinit(ifp, ia, sin, scrub)
686         register struct ifnet *ifp;
687         register struct in_ifaddr *ia;
688         struct sockaddr_in *sin;
689         int scrub;
690 {
691         register u_long i = ntohl(sin->sin_addr.s_addr);
692         struct sockaddr_in oldaddr;
693         int s = splimp(), flags = RTF_UP, error = 0;
694
695         oldaddr = ia->ia_addr;
696         if (oldaddr.sin_family == AF_INET)
697                 LIST_REMOVE(ia, ia_hash);
698         ia->ia_addr = *sin;
699         if (ia->ia_addr.sin_family == AF_INET)
700                 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
701                     ia, ia_hash);
702         /*
703          * Give the interface a chance to initialize
704          * if this is its first address,
705          * and to validate the address if necessary.
706          */
707         if (ifp->if_ioctl) {
708                 IFF_LOCKGIANT(ifp);
709                 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
710                 IFF_UNLOCKGIANT(ifp);
711                 if (error) {
712                         splx(s);
713                         /* LIST_REMOVE(ia, ia_hash) is done in in_control */
714                         ia->ia_addr = oldaddr;
715                         if (ia->ia_addr.sin_family == AF_INET)
716                                 LIST_INSERT_HEAD(INADDR_HASH(
717                                     ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
718                         else 
719                                 /* 
720                                  * If oldaddr family is not AF_INET (e.g. 
721                                  * interface has been just created) in_control 
722                                  * does not call LIST_REMOVE, and we end up 
723                                  * with bogus ia entries in hash
724                                  */
725                                 LIST_REMOVE(ia, ia_hash);
726                         return (error);
727                 }
728         }
729         splx(s);
730         if (scrub) {
731                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
732                 in_ifscrub(ifp, ia);
733                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
734         }
735         if (IN_CLASSA(i))
736                 ia->ia_netmask = IN_CLASSA_NET;
737         else if (IN_CLASSB(i))
738                 ia->ia_netmask = IN_CLASSB_NET;
739         else
740                 ia->ia_netmask = IN_CLASSC_NET;
741         /*
742          * The subnet mask usually includes at least the standard network part,
743          * but may may be smaller in the case of supernetting.
744          * If it is set, we believe it.
745          */
746         if (ia->ia_subnetmask == 0) {
747                 ia->ia_subnetmask = ia->ia_netmask;
748                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
749         } else
750                 ia->ia_netmask &= ia->ia_subnetmask;
751         ia->ia_net = i & ia->ia_netmask;
752         ia->ia_subnet = i & ia->ia_subnetmask;
753         in_socktrim(&ia->ia_sockmask);
754 #ifdef DEV_CARP
755         /*
756          * XXX: carp(4) does not have interface route
757          */
758         if (ifp->if_type == IFT_CARP)
759                 return (0);
760 #endif
761         /*
762          * Add route for the network.
763          */
764         ia->ia_ifa.ifa_metric = ifp->if_metric;
765         if (ifp->if_flags & IFF_BROADCAST) {
766                 ia->ia_broadaddr.sin_addr.s_addr =
767                         htonl(ia->ia_subnet | ~ia->ia_subnetmask);
768                 ia->ia_netbroadcast.s_addr =
769                         htonl(ia->ia_net | ~ ia->ia_netmask);
770         } else if (ifp->if_flags & IFF_LOOPBACK) {
771                 ia->ia_dstaddr = ia->ia_addr;
772                 flags |= RTF_HOST;
773         } else if (ifp->if_flags & IFF_POINTOPOINT) {
774                 if (ia->ia_dstaddr.sin_family != AF_INET)
775                         return (0);
776                 flags |= RTF_HOST;
777         }
778         if ((error = in_addprefix(ia, flags)) != 0)
779                 return (error);
780
781         /*
782          * If the interface supports multicast, join the "all hosts"
783          * multicast group on that interface.
784          */
785         if (ifp->if_flags & IFF_MULTICAST) {
786                 struct in_addr addr;
787
788                 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
789                 in_addmulti(&addr, ifp);
790         }
791         return (error);
792 }
793
794 #define rtinitflags(x) \
795         ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
796             ? RTF_HOST : 0)
797 /*
798  * Check if we have a route for the given prefix already or add a one
799  * accordingly.
800  */
801 static int
802 in_addprefix(target, flags)
803         struct in_ifaddr *target;
804         int flags;
805 {
806         struct in_ifaddr *ia;
807         struct in_addr prefix, mask, p, m;
808         int error;
809
810         if ((flags & RTF_HOST) != 0)
811                 prefix = target->ia_dstaddr.sin_addr;
812         else {
813                 prefix = target->ia_addr.sin_addr;
814                 mask = target->ia_sockmask.sin_addr;
815                 prefix.s_addr &= mask.s_addr;
816         }
817
818         TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
819                 if (rtinitflags(ia)) {
820                         p = ia->ia_addr.sin_addr;
821
822                         if (prefix.s_addr != p.s_addr)
823                                 continue;
824                 } else {
825                         p = ia->ia_addr.sin_addr;
826                         m = ia->ia_sockmask.sin_addr;
827                         p.s_addr &= m.s_addr;
828
829                         if (prefix.s_addr != p.s_addr ||
830                             mask.s_addr != m.s_addr)
831                                 continue;
832                 }
833
834                 /*
835                  * If we got a matching prefix route inserted by other
836                  * interface address, we are done here.
837                  */
838                 if (ia->ia_flags & IFA_ROUTE) {
839                         if (sameprefixcarponly &&
840                             target->ia_ifp->if_type != IFT_CARP &&
841                             ia->ia_ifp->if_type != IFT_CARP)
842                                 return (EEXIST);
843                         else
844                                 return (0);
845                 }
846         }
847
848         /*
849          * No-one seem to have this prefix route, so we try to insert it.
850          */
851         error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
852         if (!error)
853                 target->ia_flags |= IFA_ROUTE;
854         return error;
855 }
856
857 /*
858  * If there is no other address in the system that can serve a route to the
859  * same prefix, remove the route.  Hand over the route to the new address
860  * otherwise.
861  */
862 static int
863 in_scrubprefix(target)
864         struct in_ifaddr *target;
865 {
866         struct in_ifaddr *ia;
867         struct in_addr prefix, mask, p;
868         int error;
869
870         if ((target->ia_flags & IFA_ROUTE) == 0)
871                 return 0;
872
873         if (rtinitflags(target))
874                 prefix = target->ia_dstaddr.sin_addr;
875         else {
876                 prefix = target->ia_addr.sin_addr;
877                 mask = target->ia_sockmask.sin_addr;
878                 prefix.s_addr &= mask.s_addr;
879         }
880
881         TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
882                 if (rtinitflags(ia))
883                         p = ia->ia_dstaddr.sin_addr;
884                 else {
885                         p = ia->ia_addr.sin_addr;
886                         p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
887                 }
888
889                 if (prefix.s_addr != p.s_addr)
890                         continue;
891
892                 /*
893                  * If we got a matching prefix address, move IFA_ROUTE and
894                  * the route itself to it.  Make sure that routing daemons
895                  * get a heads-up.
896                  *
897                  * XXX: a special case for carp(4) interface
898                  */
899                 if ((ia->ia_flags & IFA_ROUTE) == 0
900 #ifdef DEV_CARP
901                     && (ia->ia_ifp->if_type != IFT_CARP)
902 #endif
903                                                         ) {
904                         rtinit(&(target->ia_ifa), (int)RTM_DELETE,
905                             rtinitflags(target));
906                         target->ia_flags &= ~IFA_ROUTE;
907
908                         error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
909                             rtinitflags(ia) | RTF_UP);
910                         if (error == 0)
911                                 ia->ia_flags |= IFA_ROUTE;
912                         return error;
913                 }
914         }
915
916         /*
917          * As no-one seem to have this prefix, we can remove the route.
918          */
919         rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
920         target->ia_flags &= ~IFA_ROUTE;
921         return 0;
922 }
923
924 #undef rtinitflags
925
926 /*
927  * Return 1 if the address might be a local broadcast address.
928  */
929 int
930 in_broadcast(in, ifp)
931         struct in_addr in;
932         struct ifnet *ifp;
933 {
934         register struct ifaddr *ifa;
935         u_long t;
936
937         if (in.s_addr == INADDR_BROADCAST ||
938             in.s_addr == INADDR_ANY)
939                 return 1;
940         if ((ifp->if_flags & IFF_BROADCAST) == 0)
941                 return 0;
942         t = ntohl(in.s_addr);
943         /*
944          * Look through the list of addresses for a match
945          * with a broadcast address.
946          */
947 #define ia ((struct in_ifaddr *)ifa)
948         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
949                 if (ifa->ifa_addr->sa_family == AF_INET &&
950                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
951                      in.s_addr == ia->ia_netbroadcast.s_addr ||
952                      /*
953                       * Check for old-style (host 0) broadcast.
954                       */
955                      t == ia->ia_subnet || t == ia->ia_net) &&
956                      /*
957                       * Check for an all one subnetmask. These
958                       * only exist when an interface gets a secondary
959                       * address.
960                       */
961                      ia->ia_subnetmask != (u_long)0xffffffff)
962                             return 1;
963         return (0);
964 #undef ia
965 }
966 /*
967  * Add an address to the list of IP multicast addresses for a given interface.
968  */
969 struct in_multi *
970 in_addmulti(ap, ifp)
971         register struct in_addr *ap;
972         register struct ifnet *ifp;
973 {
974         register struct in_multi *inm;
975         int error;
976         struct sockaddr_in sin;
977         struct ifmultiaddr *ifma;
978
979         IFF_LOCKGIANT(ifp);
980         IN_MULTI_LOCK();
981         /*
982          * Call generic routine to add membership or increment
983          * refcount.  It wants addresses in the form of a sockaddr,
984          * so we build one here (being careful to zero the unused bytes).
985          */
986         bzero(&sin, sizeof sin);
987         sin.sin_family = AF_INET;
988         sin.sin_len = sizeof sin;
989         sin.sin_addr = *ap;
990         error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
991         if (error) {
992                 IN_MULTI_UNLOCK();
993                 IFF_UNLOCKGIANT(ifp);
994                 return 0;
995         }
996
997         /*
998          * If ifma->ifma_protospec is null, then if_addmulti() created
999          * a new record.  Otherwise, we are done.
1000          */
1001         if (ifma->ifma_protospec != NULL) {
1002                 IN_MULTI_UNLOCK();
1003                 IFF_UNLOCKGIANT(ifp);
1004                 return ifma->ifma_protospec;
1005         }
1006
1007         inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
1008             M_NOWAIT | M_ZERO);
1009         if (inm == NULL) {
1010                 if_delmulti_ent(ifma);
1011                 IN_MULTI_UNLOCK();
1012                 IFF_UNLOCKGIANT(ifp);
1013                 return (NULL);
1014         }
1015
1016         inm->inm_addr = *ap;
1017         inm->inm_ifp = ifp;
1018         inm->inm_ifma = ifma;
1019         ifma->ifma_protospec = inm;
1020         LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1021
1022         /*
1023          * Let IGMP know that we have joined a new IP multicast group.
1024          */
1025         igmp_joingroup(inm);
1026         IN_MULTI_UNLOCK();
1027         IFF_UNLOCKGIANT(ifp);
1028         return (inm);
1029 }
1030
1031 static void
1032 in_delmulti_locked(inm, all)
1033         register struct in_multi *inm;
1034         int all;
1035 {
1036         struct ifmultiaddr *ifma;
1037         struct in_multi my_inm;
1038
1039         ifma = inm->inm_ifma;
1040         my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1041         if (all)
1042                 while (ifma->ifma_refcount > 1)
1043                         if_delmulti_ent(ifma);
1044         if (ifma->ifma_refcount == 1) {
1045                 /*
1046                  * No remaining claims to this record; let IGMP know that
1047                  * we are leaving the multicast group.
1048                  * But do it after the if_delmulti() which might reset
1049                  * the interface and nuke the packet.
1050                  */
1051                 my_inm = *inm ;
1052                 ifma->ifma_protospec = NULL;
1053                 LIST_REMOVE(inm, inm_link);
1054                 free(inm, M_IPMADDR);
1055         }
1056         if_delmulti_ent(ifma);
1057         if (my_inm.inm_ifp != NULL)
1058                 igmp_leavegroup(&my_inm);
1059 }
1060
1061 /*
1062  * Delete a multicast address record.
1063  */
1064 void
1065 in_delmulti(inm)
1066         register struct in_multi *inm;
1067 {
1068         struct ifnet *ifp;
1069
1070         ifp = inm->inm_ifp;
1071         IFF_LOCKGIANT(ifp);
1072         IN_MULTI_LOCK();
1073         in_delmulti_locked(inm, 0);
1074         IN_MULTI_UNLOCK();
1075         IFF_UNLOCKGIANT(ifp);
1076 }
1077
1078 /*
1079  * Delete all multicast address records associated with the ifp.
1080  */
1081 void
1082 in_delmulti_ifp(ifp)
1083         register struct ifnet *ifp;
1084 {
1085         struct in_multi *inm;
1086         struct in_multi *oinm;
1087
1088         IFF_LOCKGIANT(ifp);
1089         IN_MULTI_LOCK();
1090         LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
1091                 if (inm->inm_ifp == ifp)
1092                         in_delmulti_locked(inm, 1);
1093         }
1094         IN_MULTI_UNLOCK();
1095         IFF_UNLOCKGIANT(ifp);
1096 }
1097
1098 /*
1099  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1100  */
1101 void
1102 in_ifdetach(ifp)
1103         struct ifnet *ifp;
1104 {
1105
1106         in_pcbpurgeif0(&ripcbinfo, ifp);
1107         in_pcbpurgeif0(&udbinfo, ifp);
1108         in_delmulti_ifp(ifp);
1109 }