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