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