]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in.c
This commit was generated by cvs2svn to compensate for changes in r171169,
[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
609                 bzero(&mask, sizeof(mask));
610                 bzero(&match, sizeof(match));
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                 } else {
624                         /* on getting an address, take the 1st match */
625                         /* on deleting an address, do exact match */
626                         if (cmd != SIOCGLIFADDR) {
627                                 in_len2mask(&mask, 32);
628                                 sin = (struct sockaddr_in *)&iflr->addr;
629                                 match.s_addr = sin->sin_addr.s_addr;
630                         }
631                 }
632
633                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
634                         if (ifa->ifa_addr->sa_family != AF_INET6)
635                                 continue;
636                         if (match.s_addr == 0)
637                                 break;
638                         candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
639                         candidate.s_addr &= mask.s_addr;
640                         if (candidate.s_addr == match.s_addr)
641                                 break;
642                 }
643                 if (!ifa)
644                         return EADDRNOTAVAIL;
645                 ia = (struct in_ifaddr *)ifa;
646
647                 if (cmd == SIOCGLIFADDR) {
648                         /* fill in the if_laddrreq structure */
649                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
650
651                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
652                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
653                                         ia->ia_dstaddr.sin_len);
654                         } else
655                                 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
656
657                         iflr->prefixlen =
658                                 in_mask2len(&ia->ia_sockmask.sin_addr);
659
660                         iflr->flags = 0;        /*XXX*/
661
662                         return 0;
663                 } else {
664                         struct in_aliasreq ifra;
665
666                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
667                         bzero(&ifra, sizeof(ifra));
668                         bcopy(iflr->iflr_name, ifra.ifra_name,
669                                 sizeof(ifra.ifra_name));
670
671                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
672                                 ia->ia_addr.sin_len);
673                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
674                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
675                                         ia->ia_dstaddr.sin_len);
676                         }
677                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
678                                 ia->ia_sockmask.sin_len);
679
680                         return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
681                                           ifp, td);
682                 }
683             }
684         }
685
686         return EOPNOTSUPP;      /*just for safety*/
687 }
688
689 /*
690  * Delete any existing route for an interface.
691  */
692 void
693 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
694 {
695
696         in_scrubprefix(ia);
697 }
698
699 /*
700  * Initialize an interface's internet address
701  * and routing table entry.
702  */
703 static int
704 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
705     int scrub)
706 {
707         register u_long i = ntohl(sin->sin_addr.s_addr);
708         struct sockaddr_in oldaddr;
709         int s = splimp(), flags = RTF_UP, error = 0;
710
711         oldaddr = ia->ia_addr;
712         if (oldaddr.sin_family == AF_INET)
713                 LIST_REMOVE(ia, ia_hash);
714         ia->ia_addr = *sin;
715         if (ia->ia_addr.sin_family == AF_INET)
716                 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
717                     ia, ia_hash);
718         /*
719          * Give the interface a chance to initialize
720          * if this is its first address,
721          * and to validate the address if necessary.
722          */
723         if (ifp->if_ioctl) {
724                 IFF_LOCKGIANT(ifp);
725                 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
726                 IFF_UNLOCKGIANT(ifp);
727                 if (error) {
728                         splx(s);
729                         /* LIST_REMOVE(ia, ia_hash) is done in in_control */
730                         ia->ia_addr = oldaddr;
731                         if (ia->ia_addr.sin_family == AF_INET)
732                                 LIST_INSERT_HEAD(INADDR_HASH(
733                                     ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
734                         return (error);
735                 }
736         }
737         splx(s);
738         if (scrub) {
739                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
740                 in_ifscrub(ifp, ia);
741                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
742         }
743         if (IN_CLASSA(i))
744                 ia->ia_netmask = IN_CLASSA_NET;
745         else if (IN_CLASSB(i))
746                 ia->ia_netmask = IN_CLASSB_NET;
747         else
748                 ia->ia_netmask = IN_CLASSC_NET;
749         /*
750          * The subnet mask usually includes at least the standard network part,
751          * but may may be smaller in the case of supernetting.
752          * If it is set, we believe it.
753          */
754         if (ia->ia_subnetmask == 0) {
755                 ia->ia_subnetmask = ia->ia_netmask;
756                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
757         } else
758                 ia->ia_netmask &= ia->ia_subnetmask;
759         ia->ia_net = i & ia->ia_netmask;
760         ia->ia_subnet = i & ia->ia_subnetmask;
761         in_socktrim(&ia->ia_sockmask);
762 #ifdef DEV_CARP
763         /*
764          * XXX: carp(4) does not have interface route
765          */
766         if (ifp->if_type == IFT_CARP)
767                 return (0);
768 #endif
769         /*
770          * Add route for the network.
771          */
772         ia->ia_ifa.ifa_metric = ifp->if_metric;
773         if (ifp->if_flags & IFF_BROADCAST) {
774                 ia->ia_broadaddr.sin_addr.s_addr =
775                         htonl(ia->ia_subnet | ~ia->ia_subnetmask);
776                 ia->ia_netbroadcast.s_addr =
777                         htonl(ia->ia_net | ~ ia->ia_netmask);
778         } else if (ifp->if_flags & IFF_LOOPBACK) {
779                 ia->ia_dstaddr = ia->ia_addr;
780                 flags |= RTF_HOST;
781         } else if (ifp->if_flags & IFF_POINTOPOINT) {
782                 if (ia->ia_dstaddr.sin_family != AF_INET)
783                         return (0);
784                 flags |= RTF_HOST;
785         }
786         if ((error = in_addprefix(ia, flags)) != 0)
787                 return (error);
788
789         return (error);
790 }
791
792 #define rtinitflags(x) \
793         ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
794             ? RTF_HOST : 0)
795 /*
796  * Check if we have a route for the given prefix already or add one accordingly.
797  */
798 static int
799 in_addprefix(struct in_ifaddr *target, int flags)
800 {
801         struct in_ifaddr *ia;
802         struct in_addr prefix, mask, p, m;
803         int error;
804
805         if ((flags & RTF_HOST) != 0) {
806                 prefix = target->ia_dstaddr.sin_addr;
807                 mask.s_addr = 0;
808         } else {
809                 prefix = target->ia_addr.sin_addr;
810                 mask = target->ia_sockmask.sin_addr;
811                 prefix.s_addr &= mask.s_addr;
812         }
813
814         TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
815                 if (rtinitflags(ia)) {
816                         p = ia->ia_addr.sin_addr;
817
818                         if (prefix.s_addr != p.s_addr)
819                                 continue;
820                 } else {
821                         p = ia->ia_addr.sin_addr;
822                         m = ia->ia_sockmask.sin_addr;
823                         p.s_addr &= m.s_addr;
824
825                         if (prefix.s_addr != p.s_addr ||
826                             mask.s_addr != m.s_addr)
827                                 continue;
828                 }
829
830                 /*
831                  * If we got a matching prefix route inserted by other
832                  * interface address, we are done here.
833                  */
834                 if (ia->ia_flags & IFA_ROUTE) {
835                         if (sameprefixcarponly &&
836                             target->ia_ifp->if_type != IFT_CARP &&
837                             ia->ia_ifp->if_type != IFT_CARP)
838                                 return (EEXIST);
839                         else
840                                 return (0);
841                 }
842         }
843
844         /*
845          * No-one seem to have this prefix route, so we try to insert it.
846          */
847         error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
848         if (!error)
849                 target->ia_flags |= IFA_ROUTE;
850         return error;
851 }
852
853 /*
854  * If there is no other address in the system that can serve a route to the
855  * same prefix, remove the route.  Hand over the route to the new address
856  * otherwise.
857  */
858 static int
859 in_scrubprefix(struct in_ifaddr *target)
860 {
861         struct in_ifaddr *ia;
862         struct in_addr prefix, mask, p;
863         int error;
864
865         if ((target->ia_flags & IFA_ROUTE) == 0)
866                 return 0;
867
868         if (rtinitflags(target))
869                 prefix = target->ia_dstaddr.sin_addr;
870         else {
871                 prefix = target->ia_addr.sin_addr;
872                 mask = target->ia_sockmask.sin_addr;
873                 prefix.s_addr &= mask.s_addr;
874         }
875
876         TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
877                 if (rtinitflags(ia))
878                         p = ia->ia_dstaddr.sin_addr;
879                 else {
880                         p = ia->ia_addr.sin_addr;
881                         p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
882                 }
883
884                 if (prefix.s_addr != p.s_addr)
885                         continue;
886
887                 /*
888                  * If we got a matching prefix address, move IFA_ROUTE and
889                  * the route itself to it.  Make sure that routing daemons
890                  * get a heads-up.
891                  *
892                  * XXX: a special case for carp(4) interface
893                  */
894                 if ((ia->ia_flags & IFA_ROUTE) == 0
895 #ifdef DEV_CARP
896                     && (ia->ia_ifp->if_type != IFT_CARP)
897 #endif
898                                                         ) {
899                         rtinit(&(target->ia_ifa), (int)RTM_DELETE,
900                             rtinitflags(target));
901                         target->ia_flags &= ~IFA_ROUTE;
902
903                         error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
904                             rtinitflags(ia) | RTF_UP);
905                         if (error == 0)
906                                 ia->ia_flags |= IFA_ROUTE;
907                         return error;
908                 }
909         }
910
911         /*
912          * As no-one seem to have this prefix, we can remove the route.
913          */
914         rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
915         target->ia_flags &= ~IFA_ROUTE;
916         return 0;
917 }
918
919 #undef rtinitflags
920
921 /*
922  * Return 1 if the address might be a local broadcast address.
923  */
924 int
925 in_broadcast(struct in_addr in, struct ifnet *ifp)
926 {
927         register struct ifaddr *ifa;
928         u_long t;
929
930         if (in.s_addr == INADDR_BROADCAST ||
931             in.s_addr == INADDR_ANY)
932                 return 1;
933         if ((ifp->if_flags & IFF_BROADCAST) == 0)
934                 return 0;
935         t = ntohl(in.s_addr);
936         /*
937          * Look through the list of addresses for a match
938          * with a broadcast address.
939          */
940 #define ia ((struct in_ifaddr *)ifa)
941         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
942                 if (ifa->ifa_addr->sa_family == AF_INET &&
943                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
944                      in.s_addr == ia->ia_netbroadcast.s_addr ||
945                      /*
946                       * Check for old-style (host 0) broadcast.
947                       */
948                      t == ia->ia_subnet || t == ia->ia_net) &&
949                      /*
950                       * Check for an all one subnetmask. These
951                       * only exist when an interface gets a secondary
952                       * address.
953                       */
954                      ia->ia_subnetmask != (u_long)0xffffffff)
955                             return 1;
956         return (0);
957 #undef ia
958 }
959
960 /*
961  * Delete all IPv4 multicast address records, and associated link-layer
962  * multicast address records, associated with ifp.
963  */
964 static void
965 in_purgemaddrs(struct ifnet *ifp)
966 {
967         struct in_multi *inm;
968         struct in_multi *oinm;
969
970 #ifdef DIAGNOSTIC
971         printf("%s: purging ifp %p\n", __func__, ifp);
972 #endif
973         IFF_LOCKGIANT(ifp);
974         IN_MULTI_LOCK();
975         LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
976                 if (inm->inm_ifp == ifp)
977                         in_delmulti_locked(inm);
978         }
979         IN_MULTI_UNLOCK();
980         IFF_UNLOCKGIANT(ifp);
981 }
982
983 /*
984  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
985  */
986 void
987 in_ifdetach(struct ifnet *ifp)
988 {
989
990         in_pcbpurgeif0(&ripcbinfo, ifp);
991         in_pcbpurgeif0(&udbinfo, ifp);
992         in_purgemaddrs(ifp);
993 }