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