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