tcp_input()'s DELAY_ACK() code checks to see if the delayed ack timer is
[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.11 2004/06/07 02:36:22 dillon Exp $
36  */
37
38 #include "opt_bootp.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/socket.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47
48 #include <net/if.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
51
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/in_pcb.h>
55
56 #include <netinet/igmp_var.h>
57
58 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
59
60 static int in_mask2len (struct in_addr *);
61 static void in_len2mask (struct in_addr *, int);
62 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
63         struct ifnet *, struct thread *);
64
65 static void     in_socktrim (struct sockaddr_in *);
66 static int      in_ifinit (struct ifnet *,
67             struct in_ifaddr *, struct sockaddr_in *, int);
68
69 static int subnetsarelocal = 0;
70 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW, 
71         &subnetsarelocal, 0, "");
72
73 struct in_multihead in_multihead; /* XXX BSS initialization */
74
75 extern struct inpcbinfo ripcbinfo;
76 extern struct inpcbinfo udbinfo;
77
78 /*
79  * Return 1 if an internet address is for a ``local'' host
80  * (one to which we have a connection).  If subnetsarelocal
81  * is true, this includes other subnets of the local net.
82  * Otherwise, it includes only the directly-connected (sub)nets.
83  */
84 int
85 in_localaddr(in)
86         struct in_addr in;
87 {
88         u_long i = ntohl(in.s_addr);
89         struct in_ifaddr *ia;
90
91         if (subnetsarelocal) {
92                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
93                         if ((i & ia->ia_netmask) == ia->ia_net)
94                                 return (1);
95         } else {
96                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
97                         if ((i & ia->ia_subnetmask) == ia->ia_subnet)
98                                 return (1);
99         }
100         return (0);
101 }
102
103 /*
104  * Determine whether an IP address is in a reserved set of addresses
105  * that may not be forwarded, or whether datagrams to that destination
106  * may be forwarded.
107  */
108 int
109 in_canforward(in)
110         struct in_addr in;
111 {
112         u_long i = ntohl(in.s_addr);
113         u_long net;
114
115         if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
116                 return (0);
117         if (IN_CLASSA(i)) {
118                 net = i & IN_CLASSA_NET;
119                 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
120                         return (0);
121         }
122         return (1);
123 }
124
125 /*
126  * Trim a mask in a sockaddr
127  */
128 static void
129 in_socktrim(ap)
130 struct sockaddr_in *ap;
131 {
132     char *cplim = (char *) &ap->sin_addr;
133     char *cp = (char *) (&ap->sin_addr + 1);
134
135     ap->sin_len = 0;
136     while (--cp >= cplim)
137         if (*cp) {
138             (ap)->sin_len = cp - (char *) (ap) + 1;
139             break;
140         }
141 }
142
143 static int
144 in_mask2len(mask)
145         struct in_addr *mask;
146 {
147         int x, y;
148         u_char *p;
149
150         p = (u_char *)mask;
151         for (x = 0; x < sizeof(*mask); x++) {
152                 if (p[x] != 0xff)
153                         break;
154         }
155         y = 0;
156         if (x < sizeof(*mask)) {
157                 for (y = 0; y < 8; y++) {
158                         if ((p[x] & (0x80 >> y)) == 0)
159                                 break;
160                 }
161         }
162         return x * 8 + y;
163 }
164
165 static void
166 in_len2mask(mask, len)
167         struct in_addr *mask;
168         int len;
169 {
170         int i;
171         u_char *p;
172
173         p = (u_char *)mask;
174         bzero(mask, sizeof(*mask));
175         for (i = 0; i < len / 8; i++)
176                 p[i] = 0xff;
177         if (len % 8)
178                 p[i] = (0xff00 >> (len % 8)) & 0xff;
179 }
180
181 static int in_interfaces;       /* number of external internet interfaces */
182
183 /*
184  * Generic internet control operations (ioctl's).
185  * Ifp is 0 if not an interface-specific ioctl.
186  *
187  * NOTE! td might be NULL.
188  */
189 /* ARGSUSED */
190 int
191 in_control(so, cmd, data, ifp, td)
192         struct socket *so;
193         u_long cmd;
194         caddr_t data;
195         struct ifnet *ifp;
196         struct thread *td;
197 {
198         struct ifreq *ifr = (struct ifreq *)data;
199         struct in_ifaddr *ia = 0, *iap;
200         struct ifaddr *ifa;
201         struct in_addr dst;
202         struct in_ifaddr *oia;
203         struct in_aliasreq *ifra = (struct in_aliasreq *)data;
204         struct sockaddr_in oldaddr;
205         int hostIsNew, iaIsNew, maskIsNew, s;
206         int error = 0;
207
208         iaIsNew = 0;
209
210         switch (cmd) {
211         case SIOCALIFADDR:
212         case SIOCDLIFADDR:
213                 if (td && (error = suser(td)) != 0)
214                         return error;
215                 /*fall through*/
216         case SIOCGLIFADDR:
217                 if (!ifp)
218                         return EINVAL;
219                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
220         }
221
222         /*
223          * Find address for this interface, if it exists.
224          *
225          * If an alias address was specified, find that one instead of
226          * the first one on the interface, if possible
227          */
228         if (ifp) {
229                 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
230                 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
231                         if (iap->ia_ifp == ifp &&
232                             iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
233                                 ia = iap;
234                                 break;
235                         }
236                 if (ia == NULL)
237                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
238                                 iap = ifatoia(ifa);
239                                 if (iap->ia_addr.sin_family == AF_INET) {
240                                         ia = iap;
241                                         break;
242                                 }
243                         }
244         }
245
246         switch (cmd) {
247
248         case SIOCAIFADDR:
249         case SIOCDIFADDR:
250                 if (ifp == 0)
251                         return (EADDRNOTAVAIL);
252                 if (ifra->ifra_addr.sin_family == AF_INET) {
253                         for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
254                                 if (ia->ia_ifp == ifp  &&
255                                     ia->ia_addr.sin_addr.s_addr ==
256                                     ifra->ifra_addr.sin_addr.s_addr)
257                                         break;
258                         }
259                         if ((ifp->if_flags & IFF_POINTOPOINT)
260                             && (cmd == SIOCAIFADDR)
261                             && (ifra->ifra_dstaddr.sin_addr.s_addr
262                                 == INADDR_ANY)) {
263                                 return EDESTADDRREQ;
264                         }
265                 }
266                 if (cmd == SIOCDIFADDR && ia == 0)
267                         return (EADDRNOTAVAIL);
268                 /* FALLTHROUGH */
269         case SIOCSIFADDR:
270         case SIOCSIFNETMASK:
271         case SIOCSIFDSTADDR:
272                 if (td && (error = suser(td)) != 0)
273                         return error;
274
275                 if (ifp == 0)
276                         return (EADDRNOTAVAIL);
277                 if (ia == (struct in_ifaddr *)0) {
278                         ia = (struct in_ifaddr *)
279                                 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
280                         if (ia == (struct in_ifaddr *)NULL)
281                                 return (ENOBUFS);
282                         bzero((caddr_t)ia, sizeof *ia);
283                         /*
284                          * Protect from ipintr() traversing address list
285                          * while we're modifying it.
286                          */
287                         s = splnet();
288                         
289                         TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
290                         ifa = &ia->ia_ifa;
291                         TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
292
293                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
294                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
295                         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
296                         ia->ia_sockmask.sin_len = 8;
297                         ia->ia_sockmask.sin_family = AF_INET;
298                         if (ifp->if_flags & IFF_BROADCAST) {
299                                 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
300                                 ia->ia_broadaddr.sin_family = AF_INET;
301                         }
302                         ia->ia_ifp = ifp;
303                         if (!(ifp->if_flags & IFF_LOOPBACK))
304                                 in_interfaces++;
305                         iaIsNew = 1;
306                         splx(s);
307                 }
308                 break;
309
310         case SIOCSIFBRDADDR:
311                 if (td && (error = suser(td)) != 0)
312                         return error;
313                 /* FALLTHROUGH */
314
315         case SIOCGIFADDR:
316         case SIOCGIFNETMASK:
317         case SIOCGIFDSTADDR:
318         case SIOCGIFBRDADDR:
319                 if (ia == (struct in_ifaddr *)0)
320                         return (EADDRNOTAVAIL);
321                 break;
322         }
323         switch (cmd) {
324
325         case SIOCGIFADDR:
326                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
327                 return (0);
328
329         case SIOCGIFBRDADDR:
330                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
331                         return (EINVAL);
332                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
333                 return (0);
334
335         case SIOCGIFDSTADDR:
336                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
337                         return (EINVAL);
338                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
339                 return (0);
340
341         case SIOCGIFNETMASK:
342                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
343                 return (0);
344
345         case SIOCSIFDSTADDR:
346                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
347                         return (EINVAL);
348                 oldaddr = ia->ia_dstaddr;
349                 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
350                 if (ifp->if_ioctl &&
351                     (error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
352                                               td->td_proc->p_ucred))) {
353                         ia->ia_dstaddr = oldaddr;
354                         return (error);
355                 }
356                 if (ia->ia_flags & IFA_ROUTE) {
357                         ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
358                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
359                         ia->ia_ifa.ifa_dstaddr =
360                                         (struct sockaddr *)&ia->ia_dstaddr;
361                         rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
362                 }
363                 return (0);
364
365         case SIOCSIFBRDADDR:
366                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
367                         return (EINVAL);
368                 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
369                 return (0);
370
371         case SIOCSIFADDR:
372                 error = in_ifinit(ifp, ia,
373                     (struct sockaddr_in *) &ifr->ifr_addr, 1);
374                 if (error != 0 && iaIsNew)
375                         break;
376                 return (0);
377
378         case SIOCSIFNETMASK:
379                 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
380                 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
381                 return (0);
382
383         case SIOCAIFADDR:
384                 maskIsNew = 0;
385                 hostIsNew = 1;
386                 error = 0;
387                 if (ia->ia_addr.sin_family == AF_INET) {
388                         if (ifra->ifra_addr.sin_len == 0) {
389                                 ifra->ifra_addr = ia->ia_addr;
390                                 hostIsNew = 0;
391                         } else if (ifra->ifra_addr.sin_addr.s_addr ==
392                                                ia->ia_addr.sin_addr.s_addr)
393                                 hostIsNew = 0;
394                 }
395                 if (ifra->ifra_mask.sin_len) {
396                         in_ifscrub(ifp, ia);
397                         ia->ia_sockmask = ifra->ifra_mask;
398                         ia->ia_sockmask.sin_family = AF_INET;
399                         ia->ia_subnetmask =
400                              ntohl(ia->ia_sockmask.sin_addr.s_addr);
401                         maskIsNew = 1;
402                 }
403                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
404                     (ifra->ifra_dstaddr.sin_family == AF_INET)) {
405                         in_ifscrub(ifp, ia);
406                         ia->ia_dstaddr = ifra->ifra_dstaddr;
407                         maskIsNew  = 1; /* We lie; but the effect's the same */
408                 }
409                 if (ifra->ifra_addr.sin_family == AF_INET &&
410                     (hostIsNew || maskIsNew))
411                         error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
412
413                 if (error != 0 && iaIsNew)
414                         break;
415
416                 if ((ifp->if_flags & IFF_BROADCAST) &&
417                     (ifra->ifra_broadaddr.sin_family == AF_INET))
418                         ia->ia_broadaddr = ifra->ifra_broadaddr;
419                 return (error);
420
421         case SIOCDIFADDR:
422                 /*
423                  * in_ifscrub kills the interface route.
424                  */
425                 in_ifscrub(ifp, ia);
426                 /*
427                  * in_ifadown gets rid of all the rest of
428                  * the routes.  This is not quite the right
429                  * thing to do, but at least if we are running
430                  * a routing process they will come back.
431                  */
432                 in_ifadown(&ia->ia_ifa, 1);
433                 /*
434                  * XXX horrible hack to detect that we are being called
435                  * from if_detach()
436                  */
437                 if (!ifnet_addrs[ifp->if_index - 1]) {
438                         in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
439                         in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
440                 }
441                 error = 0;
442                 break;
443
444         default:
445                 if (ifp == NULL || ifp->if_ioctl == NULL)
446                         return (EOPNOTSUPP);
447                 return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
448         }
449
450         /*
451          * Protect from ipintr() traversing address list while we're modifying
452          * it.
453          */
454         s = splnet();
455         TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
456         TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
457         LIST_REMOVE(ia, ia_hash);
458         IFAFREE(&ia->ia_ifa);
459         splx(s);
460
461         return (error);
462 }
463
464 /*
465  * SIOC[GAD]LIFADDR.
466  *      SIOCGLIFADDR: get first address. (?!?)
467  *      SIOCGLIFADDR with IFLR_PREFIX:
468  *              get first address that matches the specified prefix.
469  *      SIOCALIFADDR: add the specified address.
470  *      SIOCALIFADDR with IFLR_PREFIX:
471  *              EINVAL since we can't deduce hostid part of the address.
472  *      SIOCDLIFADDR: delete the specified address.
473  *      SIOCDLIFADDR with IFLR_PREFIX:
474  *              delete the first address that matches the specified prefix.
475  * return values:
476  *      EINVAL on invalid parameters
477  *      EADDRNOTAVAIL on prefix match failed/specified address not found
478  *      other values may be returned from in_ioctl()
479  *
480  * NOTE! td might be NULL.
481  */
482 static int
483 in_lifaddr_ioctl(so, cmd, data, ifp, td)
484         struct socket *so;
485         u_long cmd;
486         caddr_t data;
487         struct ifnet *ifp;
488         struct thread *td;
489 {
490         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
491         struct ifaddr *ifa;
492
493         /* sanity checks */
494         if (!data || !ifp) {
495                 panic("invalid argument to in_lifaddr_ioctl");
496                 /*NOTRECHED*/
497         }
498
499         switch (cmd) {
500         case SIOCGLIFADDR:
501                 /* address must be specified on GET with IFLR_PREFIX */
502                 if ((iflr->flags & IFLR_PREFIX) == 0)
503                         break;
504                 /*FALLTHROUGH*/
505         case SIOCALIFADDR:
506         case SIOCDLIFADDR:
507                 /* address must be specified on ADD and DELETE */
508                 if (iflr->addr.ss_family != AF_INET)
509                         return EINVAL;
510                 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
511                         return EINVAL;
512                 /* XXX need improvement */
513                 if (iflr->dstaddr.ss_family
514                  && iflr->dstaddr.ss_family != AF_INET)
515                         return EINVAL;
516                 if (iflr->dstaddr.ss_family
517                  && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
518                         return EINVAL;
519                 break;
520         default: /*shouldn't happen*/
521                 return EOPNOTSUPP;
522         }
523         if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
524                 return EINVAL;
525
526         switch (cmd) {
527         case SIOCALIFADDR:
528             {
529                 struct in_aliasreq ifra;
530
531                 if (iflr->flags & IFLR_PREFIX)
532                         return EINVAL;
533
534                 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
535                 bzero(&ifra, sizeof(ifra));
536                 bcopy(iflr->iflr_name, ifra.ifra_name,
537                         sizeof(ifra.ifra_name));
538
539                 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
540
541                 if (iflr->dstaddr.ss_family) {  /*XXX*/
542                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
543                                 iflr->dstaddr.ss_len);
544                 }
545
546                 ifra.ifra_mask.sin_family = AF_INET;
547                 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
548                 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
549
550                 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
551             }
552         case SIOCGLIFADDR:
553         case SIOCDLIFADDR:
554             {
555                 struct in_ifaddr *ia;
556                 struct in_addr mask, candidate, match;
557                 struct sockaddr_in *sin;
558                 int cmp;
559
560                 bzero(&mask, sizeof(mask));
561                 if (iflr->flags & IFLR_PREFIX) {
562                         /* lookup a prefix rather than address. */
563                         in_len2mask(&mask, iflr->prefixlen);
564
565                         sin = (struct sockaddr_in *)&iflr->addr;
566                         match.s_addr = sin->sin_addr.s_addr;
567                         match.s_addr &= mask.s_addr;
568
569                         /* if you set extra bits, that's wrong */
570                         if (match.s_addr != sin->sin_addr.s_addr)
571                                 return EINVAL;
572
573                         cmp = 1;
574                 } else {
575                         if (cmd == SIOCGLIFADDR) {
576                                 /* on getting an address, take the 1st match */
577                                 cmp = 0;        /*XXX*/
578                         } else {
579                                 /* on deleting an address, do exact match */
580                                 in_len2mask(&mask, 32);
581                                 sin = (struct sockaddr_in *)&iflr->addr;
582                                 match.s_addr = sin->sin_addr.s_addr;
583
584                                 cmp = 1;
585                         }
586                 }
587
588                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
589                         if (ifa->ifa_addr->sa_family != AF_INET6)
590                                 continue;
591                         if (!cmp)
592                                 break;
593                         candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
594                         candidate.s_addr &= mask.s_addr;
595                         if (candidate.s_addr == match.s_addr)
596                                 break;
597                 }
598                 if (!ifa)
599                         return EADDRNOTAVAIL;
600                 ia = (struct in_ifaddr *)ifa;
601
602                 if (cmd == SIOCGLIFADDR) {
603                         /* fill in the if_laddrreq structure */
604                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
605
606                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
607                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
608                                         ia->ia_dstaddr.sin_len);
609                         } else
610                                 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
611
612                         iflr->prefixlen =
613                                 in_mask2len(&ia->ia_sockmask.sin_addr);
614
615                         iflr->flags = 0;        /*XXX*/
616
617                         return 0;
618                 } else {
619                         struct in_aliasreq ifra;
620
621                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
622                         bzero(&ifra, sizeof(ifra));
623                         bcopy(iflr->iflr_name, ifra.ifra_name,
624                                 sizeof(ifra.ifra_name));
625
626                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
627                                 ia->ia_addr.sin_len);
628                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
629                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
630                                         ia->ia_dstaddr.sin_len);
631                         }
632                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
633                                 ia->ia_sockmask.sin_len);
634
635                         return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
636                                           ifp, td);
637                 }
638             }
639         }
640
641         return EOPNOTSUPP;      /*just for safety*/
642 }
643
644 /*
645  * Delete any existing route for an interface.
646  */
647 void
648 in_ifscrub(ifp, ia)
649         struct ifnet *ifp;
650         struct in_ifaddr *ia;
651 {
652
653         if ((ia->ia_flags & IFA_ROUTE) == 0)
654                 return;
655         if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
656                 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
657         else
658                 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
659         ia->ia_flags &= ~IFA_ROUTE;
660 }
661
662 /*
663  * Initialize an interface's internet address
664  * and routing table entry.
665  */
666 static int
667 in_ifinit(ifp, ia, sin, scrub)
668         struct ifnet *ifp;
669         struct in_ifaddr *ia;
670         struct sockaddr_in *sin;
671         int scrub;
672 {
673         u_long i = ntohl(sin->sin_addr.s_addr);
674         struct sockaddr_in oldaddr;
675         int s = splimp(), flags = RTF_UP, error = 0;
676
677         oldaddr = ia->ia_addr;
678         if (oldaddr.sin_family == AF_INET)
679                 LIST_REMOVE(ia, ia_hash);
680         ia->ia_addr = *sin;
681         if (ia->ia_addr.sin_family == AF_INET)
682                 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
683                     ia, ia_hash);
684         /*
685          * Give the interface a chance to initialize
686          * if this is its first address,
687          * and to validate the address if necessary.
688          */
689         if (ifp->if_ioctl &&
690             (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia,
691                                       (struct ucred *)NULL))) {
692                 splx(s);
693                 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
694                 ia->ia_addr = oldaddr;
695                 if (ia->ia_addr.sin_family == AF_INET)
696                         LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
697                             ia, ia_hash);
698                 return (error);
699         }
700         splx(s);
701         if (scrub) {
702                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
703                 in_ifscrub(ifp, ia);
704                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
705         }
706         if (IN_CLASSA(i))
707                 ia->ia_netmask = IN_CLASSA_NET;
708         else if (IN_CLASSB(i))
709                 ia->ia_netmask = IN_CLASSB_NET;
710         else
711                 ia->ia_netmask = IN_CLASSC_NET;
712         /*
713          * The subnet mask usually includes at least the standard network part,
714          * but may may be smaller in the case of supernetting.
715          * If it is set, we believe it.
716          */
717         if (ia->ia_subnetmask == 0) {
718                 ia->ia_subnetmask = ia->ia_netmask;
719                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
720         } else
721                 ia->ia_netmask &= ia->ia_subnetmask;
722         ia->ia_net = i & ia->ia_netmask;
723         ia->ia_subnet = i & ia->ia_subnetmask;
724         in_socktrim(&ia->ia_sockmask);
725         /*
726          * Add route for the network.
727          */
728         ia->ia_ifa.ifa_metric = ifp->if_metric;
729         if (ifp->if_flags & IFF_BROADCAST) {
730                 ia->ia_broadaddr.sin_addr.s_addr =
731                         htonl(ia->ia_subnet | ~ia->ia_subnetmask);
732                 ia->ia_netbroadcast.s_addr =
733                         htonl(ia->ia_net | ~ ia->ia_netmask);
734         } else if (ifp->if_flags & IFF_LOOPBACK) {
735                 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
736                 flags |= RTF_HOST;
737         } else if (ifp->if_flags & IFF_POINTOPOINT) {
738                 if (ia->ia_dstaddr.sin_family != AF_INET)
739                         return (0);
740                 flags |= RTF_HOST;
741         }
742
743         /*-
744          * Don't add host routes for interface addresses of
745          * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
746          * possible to assign several such address pairs with consistent
747          * results (no host route) and is required by BOOTP.
748          *
749          * XXX: This is ugly !  There should be a way for the caller to
750          *      say that they don't want a host route.
751          */
752         if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
753             ia->ia_netmask != IN_CLASSA_NET ||
754             ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
755                 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
756                         ia->ia_addr = oldaddr;
757                         return (error);
758                 }
759                 ia->ia_flags |= IFA_ROUTE;
760         }
761
762         /*
763          * If the interface supports multicast, join the "all hosts"
764          * multicast group on that interface.
765          */
766         if (ifp->if_flags & IFF_MULTICAST) {
767                 struct in_addr addr;
768
769                 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
770                 in_addmulti(&addr, ifp);
771         }
772         return (error);
773 }
774
775
776 /*
777  * Return 1 if the address might be a local broadcast address.
778  */
779 int
780 in_broadcast(in, ifp)
781         struct in_addr in;
782         struct ifnet *ifp;
783 {
784         struct ifaddr *ifa;
785         u_long t;
786
787         if (in.s_addr == INADDR_BROADCAST ||
788             in.s_addr == INADDR_ANY)
789                 return 1;
790         if ((ifp->if_flags & IFF_BROADCAST) == 0)
791                 return 0;
792         t = ntohl(in.s_addr);
793         /*
794          * Look through the list of addresses for a match
795          * with a broadcast address.
796          */
797 #define ia ((struct in_ifaddr *)ifa)
798         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
799                 if (ifa->ifa_addr->sa_family == AF_INET &&
800                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
801                      in.s_addr == ia->ia_netbroadcast.s_addr ||
802                      /*
803                       * Check for old-style (host 0) broadcast.
804                       */
805                      t == ia->ia_subnet || t == ia->ia_net) &&
806                      /*
807                       * Check for an all one subnetmask. These
808                       * only exist when an interface gets a secondary
809                       * address.
810                       */
811                      ia->ia_subnetmask != (u_long)0xffffffff)
812                             return 1;
813         return (0);
814 #undef ia
815 }
816 /*
817  * Add an address to the list of IP multicast addresses for a given interface.
818  */
819 struct in_multi *
820 in_addmulti(ap, ifp)
821         struct in_addr *ap;
822         struct ifnet *ifp;
823 {
824         struct in_multi *inm;
825         int error;
826         struct sockaddr_in sin;
827         struct ifmultiaddr *ifma;
828         int s = splnet();
829
830         /*
831          * Call generic routine to add membership or increment
832          * refcount.  It wants addresses in the form of a sockaddr,
833          * so we build one here (being careful to zero the unused bytes).
834          */
835         bzero(&sin, sizeof sin);
836         sin.sin_family = AF_INET;
837         sin.sin_len = sizeof sin;
838         sin.sin_addr = *ap;
839         error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
840         if (error) {
841                 splx(s);
842                 return 0;
843         }
844
845         /*
846          * If ifma->ifma_protospec is null, then if_addmulti() created
847          * a new record.  Otherwise, we are done.
848          */
849         if (ifma->ifma_protospec != 0) {
850                 splx(s);
851                 return ifma->ifma_protospec;
852         }
853
854         /* XXX - if_addmulti uses M_WAITOK.  Can this really be called
855            at interrupt time?  If so, need to fix if_addmulti. XXX */
856         inm = malloc(sizeof(*inm), M_IPMADDR, M_WAITOK | M_ZERO);
857         inm->inm_addr = *ap;
858         inm->inm_ifp = ifp;
859         inm->inm_ifma = ifma;
860         ifma->ifma_protospec = inm;
861         LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
862
863         /*
864          * Let IGMP know that we have joined a new IP multicast group.
865          */
866         igmp_joingroup(inm);
867         splx(s);
868         return (inm);
869 }
870
871 /*
872  * Delete a multicast address record.
873  */
874 void
875 in_delmulti(inm)
876         struct in_multi *inm;
877 {
878         struct ifmultiaddr *ifma = inm->inm_ifma;
879         struct in_multi my_inm;
880         int s = splnet();
881
882         my_inm.inm_ifp = NULL ; /* don't send the leave msg */
883         if (ifma->ifma_refcount == 1) {
884                 /*
885                  * No remaining claims to this record; let IGMP know that
886                  * we are leaving the multicast group.
887                  * But do it after the if_delmulti() which might reset
888                  * the interface and nuke the packet.
889                  */
890                 my_inm = *inm ;
891                 ifma->ifma_protospec = 0;
892                 LIST_REMOVE(inm, inm_link);
893                 free(inm, M_IPMADDR);
894         }
895         /* XXX - should be separate API for when we have an ifma? */
896         if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
897         if (my_inm.inm_ifp != NULL)
898                 igmp_leavegroup(&my_inm);
899         splx(s);
900 }