]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/netinet/in.c
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.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_mpath.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/jail.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_arp.h>
53 #include <net/if_dl.h>
54 #include <net/if_llatbl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
57 #include <net/vnet.h>
58
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/igmp_var.h>
64 #include <netinet/udp.h>
65 #include <netinet/udp_var.h>
66
67 static int in_mask2len(struct in_addr *);
68 static void in_len2mask(struct in_addr *, int);
69 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
70         struct ifnet *, struct thread *);
71
72 static int      in_addprefix(struct in_ifaddr *, int);
73 static int      in_scrubprefix(struct in_ifaddr *, u_int);
74 static void     in_socktrim(struct sockaddr_in *);
75 static int      in_ifinit(struct ifnet *,
76             struct in_ifaddr *, struct sockaddr_in *, int);
77 static void     in_purgemaddrs(struct ifnet *);
78
79 static VNET_DEFINE(int, subnetsarelocal);
80 #define V_subnetsarelocal               VNET(subnetsarelocal)
81 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
82         &VNET_NAME(subnetsarelocal), 0,
83         "Treat all subnets as directly connected");
84 static VNET_DEFINE(int, sameprefixcarponly);
85 #define V_sameprefixcarponly            VNET(sameprefixcarponly)
86 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
87         &VNET_NAME(sameprefixcarponly), 0,
88         "Refuse to create same prefixes on different interfaces");
89
90 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
91 #define V_ripcbinfo                     VNET(ripcbinfo)
92
93 VNET_DECLARE(struct arpstat, arpstat);  /* ARP statistics, see if_arp.h */
94 #define V_arpstat               VNET(arpstat)
95
96 /*
97  * Return 1 if an internet address is for a ``local'' host
98  * (one to which we have a connection).  If subnetsarelocal
99  * is true, this includes other subnets of the local net.
100  * Otherwise, it includes only the directly-connected (sub)nets.
101  */
102 int
103 in_localaddr(struct in_addr in)
104 {
105         register u_long i = ntohl(in.s_addr);
106         register struct in_ifaddr *ia;
107
108         IN_IFADDR_RLOCK();
109         if (V_subnetsarelocal) {
110                 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
111                         if ((i & ia->ia_netmask) == ia->ia_net) {
112                                 IN_IFADDR_RUNLOCK();
113                                 return (1);
114                         }
115                 }
116         } else {
117                 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
118                         if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
119                                 IN_IFADDR_RUNLOCK();
120                                 return (1);
121                         }
122                 }
123         }
124         IN_IFADDR_RUNLOCK();
125         return (0);
126 }
127
128 /*
129  * Return 1 if an internet address is for the local host and configured
130  * on one of its interfaces.
131  */
132 int
133 in_localip(struct in_addr in)
134 {
135         struct in_ifaddr *ia;
136
137         IN_IFADDR_RLOCK();
138         LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
139                 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
140                         IN_IFADDR_RUNLOCK();
141                         return (1);
142                 }
143         }
144         IN_IFADDR_RUNLOCK();
145         return (0);
146 }
147
148 /*
149  * Determine whether an IP address is in a reserved set of addresses
150  * that may not be forwarded, or whether datagrams to that destination
151  * may be forwarded.
152  */
153 int
154 in_canforward(struct in_addr in)
155 {
156         register u_long i = ntohl(in.s_addr);
157         register u_long net;
158
159         if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
160                 return (0);
161         if (IN_CLASSA(i)) {
162                 net = i & IN_CLASSA_NET;
163                 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
164                         return (0);
165         }
166         return (1);
167 }
168
169 /*
170  * Trim a mask in a sockaddr
171  */
172 static void
173 in_socktrim(struct sockaddr_in *ap)
174 {
175     register char *cplim = (char *) &ap->sin_addr;
176     register char *cp = (char *) (&ap->sin_addr + 1);
177
178     ap->sin_len = 0;
179     while (--cp >= cplim)
180         if (*cp) {
181             (ap)->sin_len = cp - (char *) (ap) + 1;
182             break;
183         }
184 }
185
186 static int
187 in_mask2len(mask)
188         struct in_addr *mask;
189 {
190         int x, y;
191         u_char *p;
192
193         p = (u_char *)mask;
194         for (x = 0; x < sizeof(*mask); x++) {
195                 if (p[x] != 0xff)
196                         break;
197         }
198         y = 0;
199         if (x < sizeof(*mask)) {
200                 for (y = 0; y < 8; y++) {
201                         if ((p[x] & (0x80 >> y)) == 0)
202                                 break;
203                 }
204         }
205         return (x * 8 + y);
206 }
207
208 static void
209 in_len2mask(struct in_addr *mask, int len)
210 {
211         int i;
212         u_char *p;
213
214         p = (u_char *)mask;
215         bzero(mask, sizeof(*mask));
216         for (i = 0; i < len / 8; i++)
217                 p[i] = 0xff;
218         if (len % 8)
219                 p[i] = (0xff00 >> (len % 8)) & 0xff;
220 }
221
222 /*
223  * Generic internet control operations (ioctl's).
224  *
225  * ifp is NULL if not an interface-specific ioctl.
226  */
227 /* ARGSUSED */
228 int
229 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
230     struct thread *td)
231 {
232         register struct ifreq *ifr = (struct ifreq *)data;
233         register struct in_ifaddr *ia, *iap;
234         register struct ifaddr *ifa;
235         struct in_addr allhosts_addr;
236         struct in_addr dst;
237         struct in_ifinfo *ii;
238         struct in_aliasreq *ifra = (struct in_aliasreq *)data;
239         struct sockaddr_in oldaddr;
240         int error, hostIsNew, iaIsNew, maskIsNew;
241         int iaIsFirst;
242
243         ia = NULL;
244         iaIsFirst = 0;
245         iaIsNew = 0;
246         allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
247
248         /*
249          * Filter out ioctls we implement directly; forward the rest on to
250          * in_lifaddr_ioctl() and ifp->if_ioctl().
251          */
252         switch (cmd) {
253         case SIOCAIFADDR:
254         case SIOCDIFADDR:
255         case SIOCGIFADDR:
256         case SIOCGIFBRDADDR:
257         case SIOCGIFDSTADDR:
258         case SIOCGIFNETMASK:
259         case SIOCSIFADDR:
260         case SIOCSIFBRDADDR:
261         case SIOCSIFDSTADDR:
262         case SIOCSIFNETMASK:
263                 break;
264
265         case SIOCALIFADDR:
266                 if (td != NULL) {
267                         error = priv_check(td, PRIV_NET_ADDIFADDR);
268                         if (error)
269                                 return (error);
270                 }
271                 if (ifp == NULL)
272                         return (EINVAL);
273                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
274
275         case SIOCDLIFADDR:
276                 if (td != NULL) {
277                         error = priv_check(td, PRIV_NET_DELIFADDR);
278                         if (error)
279                                 return (error);
280                 }
281                 if (ifp == NULL)
282                         return (EINVAL);
283                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
284
285         case SIOCGLIFADDR:
286                 if (ifp == NULL)
287                         return (EINVAL);
288                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
289
290         default:
291                 if (ifp == NULL || ifp->if_ioctl == NULL)
292                         return (EOPNOTSUPP);
293                 return ((*ifp->if_ioctl)(ifp, cmd, data));
294         }
295
296         if (ifp == NULL)
297                 return (EADDRNOTAVAIL);
298
299         /*
300          * Security checks before we get involved in any work.
301          */
302         switch (cmd) {
303         case SIOCAIFADDR:
304         case SIOCSIFADDR:
305         case SIOCSIFBRDADDR:
306         case SIOCSIFNETMASK:
307         case SIOCSIFDSTADDR:
308                 if (td != NULL) {
309                         error = priv_check(td, PRIV_NET_ADDIFADDR);
310                         if (error)
311                                 return (error);
312                 }
313                 break;
314
315         case SIOCDIFADDR:
316                 if (td != NULL) {
317                         error = priv_check(td, PRIV_NET_DELIFADDR);
318                         if (error)
319                                 return (error);
320                 }
321                 break;
322         }
323
324         /*
325          * Find address for this interface, if it exists.
326          *
327          * If an alias address was specified, find that one instead of the
328          * first one on the interface, if possible.
329          */
330         dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
331         IN_IFADDR_RLOCK();
332         LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
333                 if (iap->ia_ifp == ifp &&
334                     iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
335                         if (td == NULL || prison_check_ip4(td->td_ucred,
336                             &dst) == 0)
337                                 ia = iap;
338                         break;
339                 }
340         }
341         if (ia != NULL)
342                 ifa_ref(&ia->ia_ifa);
343         IN_IFADDR_RUNLOCK();
344         if (ia == NULL) {
345                 IF_ADDR_LOCK(ifp);
346                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
347                         iap = ifatoia(ifa);
348                         if (iap->ia_addr.sin_family == AF_INET) {
349                                 if (td != NULL &&
350                                     prison_check_ip4(td->td_ucred,
351                                     &iap->ia_addr.sin_addr) != 0)
352                                         continue;
353                                 ia = iap;
354                                 break;
355                         }
356                 }
357                 if (ia != NULL)
358                         ifa_ref(&ia->ia_ifa);
359                 IF_ADDR_UNLOCK(ifp);
360         }
361         if (ia == NULL)
362                 iaIsFirst = 1;
363
364         error = 0;
365         switch (cmd) {
366         case SIOCAIFADDR:
367         case SIOCDIFADDR:
368                 if (ifra->ifra_addr.sin_family == AF_INET) {
369                         struct in_ifaddr *oia;
370
371                         IN_IFADDR_RLOCK();
372                         for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
373                                 if (ia->ia_ifp == ifp  &&
374                                     ia->ia_addr.sin_addr.s_addr ==
375                                     ifra->ifra_addr.sin_addr.s_addr)
376                                         break;
377                         }
378                         if (ia != NULL && ia != oia)
379                                 ifa_ref(&ia->ia_ifa);
380                         if (oia != NULL && ia != oia)
381                                 ifa_free(&oia->ia_ifa);
382                         IN_IFADDR_RUNLOCK();
383                         if ((ifp->if_flags & IFF_POINTOPOINT)
384                             && (cmd == SIOCAIFADDR)
385                             && (ifra->ifra_dstaddr.sin_addr.s_addr
386                                 == INADDR_ANY)) {
387                                 error = EDESTADDRREQ;
388                                 goto out;
389                         }
390                 }
391                 if (cmd == SIOCDIFADDR && ia == NULL) {
392                         error = EADDRNOTAVAIL;
393                         goto out;
394                 }
395                 /* FALLTHROUGH */
396         case SIOCSIFADDR:
397         case SIOCSIFNETMASK:
398         case SIOCSIFDSTADDR:
399                 if (ia == NULL) {
400                         ia = (struct in_ifaddr *)
401                                 malloc(sizeof *ia, M_IFADDR, M_NOWAIT |
402                                     M_ZERO);
403                         if (ia == NULL) {
404                                 error = ENOBUFS;
405                                 goto out;
406                         }
407
408                         ifa = &ia->ia_ifa;
409                         ifa_init(ifa);
410                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
411                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
412                         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
413
414                         ia->ia_sockmask.sin_len = 8;
415                         ia->ia_sockmask.sin_family = AF_INET;
416                         if (ifp->if_flags & IFF_BROADCAST) {
417                                 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
418                                 ia->ia_broadaddr.sin_family = AF_INET;
419                         }
420                         ia->ia_ifp = ifp;
421
422                         ifa_ref(ifa);                   /* if_addrhead */
423                         IF_ADDR_LOCK(ifp);
424                         TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
425                         IF_ADDR_UNLOCK(ifp);
426                         ifa_ref(ifa);                   /* in_ifaddrhead */
427                         IN_IFADDR_WLOCK();
428                         TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
429                         IN_IFADDR_WUNLOCK();
430                         iaIsNew = 1;
431                 }
432                 break;
433
434         case SIOCSIFBRDADDR:
435         case SIOCGIFADDR:
436         case SIOCGIFNETMASK:
437         case SIOCGIFDSTADDR:
438         case SIOCGIFBRDADDR:
439                 if (ia == NULL) {
440                         error = EADDRNOTAVAIL;
441                         goto out;
442                 }
443                 break;
444         }
445
446         /*
447          * Most paths in this switch return directly or via out.  Only paths
448          * that remove the address break in order to hit common removal code.
449          */
450         switch (cmd) {
451         case SIOCGIFADDR:
452                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
453                 goto out;
454
455         case SIOCGIFBRDADDR:
456                 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
457                         error = EINVAL;
458                         goto out;
459                 }
460                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
461                 goto out;
462
463         case SIOCGIFDSTADDR:
464                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
465                         error = EINVAL;
466                         goto out;
467                 }
468                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
469                 goto out;
470
471         case SIOCGIFNETMASK:
472                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
473                 goto out;
474
475         case SIOCSIFDSTADDR:
476                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
477                         error = EINVAL;
478                         goto out;
479                 }
480                 oldaddr = ia->ia_dstaddr;
481                 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
482                 if (ifp->if_ioctl != NULL) {
483                         error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
484                             (caddr_t)ia);
485                         if (error) {
486                                 ia->ia_dstaddr = oldaddr;
487                                 goto out;
488                         }
489                 }
490                 if (ia->ia_flags & IFA_ROUTE) {
491                         ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
492                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
493                         ia->ia_ifa.ifa_dstaddr =
494                                         (struct sockaddr *)&ia->ia_dstaddr;
495                         rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
496                 }
497                 goto out;
498
499         case SIOCSIFBRDADDR:
500                 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
501                         error = EINVAL;
502                         goto out;
503                 }
504                 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
505                 goto out;
506
507         case SIOCSIFADDR:
508                 error = in_ifinit(ifp, ia,
509                     (struct sockaddr_in *) &ifr->ifr_addr, 1);
510                 if (error != 0 && iaIsNew)
511                         break;
512                 if (error == 0) {
513                         ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
514                         if (iaIsFirst &&
515                             (ifp->if_flags & IFF_MULTICAST) != 0) {
516                                 error = in_joingroup(ifp, &allhosts_addr,
517                                     NULL, &ii->ii_allhosts);
518                         }
519                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
520                 }
521                 error = 0;
522                 goto out;
523
524         case SIOCSIFNETMASK:
525                 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
526                 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
527                 goto out;
528
529         case SIOCAIFADDR:
530                 maskIsNew = 0;
531                 hostIsNew = 1;
532                 error = 0;
533                 if (ia->ia_addr.sin_family == AF_INET) {
534                         if (ifra->ifra_addr.sin_len == 0) {
535                                 ifra->ifra_addr = ia->ia_addr;
536                                 hostIsNew = 0;
537                         } else if (ifra->ifra_addr.sin_addr.s_addr ==
538                                                ia->ia_addr.sin_addr.s_addr)
539                                 hostIsNew = 0;
540                 }
541                 if (ifra->ifra_mask.sin_len) {
542                         /* 
543                          * QL: XXX
544                          * Need to scrub the prefix here in case
545                          * the issued command is SIOCAIFADDR with
546                          * the same address, but with a different
547                          * prefix length. And if the prefix length
548                          * is the same as before, then the call is 
549                          * un-necessarily executed here.
550                          */
551                         in_ifscrub(ifp, ia, LLE_STATIC);
552                         ia->ia_sockmask = ifra->ifra_mask;
553                         ia->ia_sockmask.sin_family = AF_INET;
554                         ia->ia_subnetmask =
555                              ntohl(ia->ia_sockmask.sin_addr.s_addr);
556                         maskIsNew = 1;
557                 }
558                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
559                     (ifra->ifra_dstaddr.sin_family == AF_INET)) {
560                         in_ifscrub(ifp, ia, LLE_STATIC);
561                         ia->ia_dstaddr = ifra->ifra_dstaddr;
562                         maskIsNew  = 1; /* We lie; but the effect's the same */
563                 }
564                 if (ifra->ifra_addr.sin_family == AF_INET &&
565                     (hostIsNew || maskIsNew))
566                         error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
567                 if (error != 0 && iaIsNew)
568                         break;
569
570                 if ((ifp->if_flags & IFF_BROADCAST) &&
571                     (ifra->ifra_broadaddr.sin_family == AF_INET))
572                         ia->ia_broadaddr = ifra->ifra_broadaddr;
573                 if (error == 0) {
574                         ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
575                         if (iaIsFirst &&
576                             (ifp->if_flags & IFF_MULTICAST) != 0) {
577                                 error = in_joingroup(ifp, &allhosts_addr,
578                                     NULL, &ii->ii_allhosts);
579                         }
580                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
581                 }
582                 goto out;
583
584         case SIOCDIFADDR:
585                 /*
586                  * in_ifscrub kills the interface route.
587                  */
588                 in_ifscrub(ifp, ia, LLE_STATIC);
589
590                 /*
591                  * in_ifadown gets rid of all the rest of
592                  * the routes.  This is not quite the right
593                  * thing to do, but at least if we are running
594                  * a routing process they will come back.
595                  */
596                 in_ifadown(&ia->ia_ifa, 1);
597                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
598                 error = 0;
599                 break;
600
601         default:
602                 panic("in_control: unsupported ioctl");
603         }
604
605         IF_ADDR_LOCK(ifp);
606         /* Re-check that ia is still part of the list. */
607         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
608                 if (ifa == &ia->ia_ifa)
609                         break;
610         }
611         if (ifa == NULL) {
612                 /*
613                  * If we lost the race with another thread, there is no need to
614                  * try it again for the next loop as there is no other exit
615                  * path between here and out.
616                  */
617                 IF_ADDR_UNLOCK(ifp);
618                 error = EADDRNOTAVAIL;
619                 goto out;
620         }
621         TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
622         IF_ADDR_UNLOCK(ifp);
623         ifa_free(&ia->ia_ifa);                          /* if_addrhead */
624
625         IN_IFADDR_WLOCK();
626         TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
627         if (ia->ia_addr.sin_family == AF_INET) {
628                 struct in_ifaddr *if_ia;
629
630                 LIST_REMOVE(ia, ia_hash);
631                 IN_IFADDR_WUNLOCK();
632                 /*
633                  * If this is the last IPv4 address configured on this
634                  * interface, leave the all-hosts group.
635                  * No state-change report need be transmitted.
636                  */
637                 if_ia = NULL;
638                 IFP_TO_IA(ifp, if_ia);
639                 if (if_ia == NULL) {
640                         ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
641                         IN_MULTI_LOCK();
642                         if (ii->ii_allhosts) {
643                                 (void)in_leavegroup_locked(ii->ii_allhosts,
644                                     NULL);
645                                 ii->ii_allhosts = NULL;
646                         }
647                         IN_MULTI_UNLOCK();
648                 } else
649                         ifa_free(&if_ia->ia_ifa);
650         } else
651                 IN_IFADDR_WUNLOCK();
652         ifa_free(&ia->ia_ifa);                          /* in_ifaddrhead */
653 out:
654         if (ia != NULL)
655                 ifa_free(&ia->ia_ifa);
656         return (error);
657 }
658
659 /*
660  * SIOC[GAD]LIFADDR.
661  *      SIOCGLIFADDR: get first address. (?!?)
662  *      SIOCGLIFADDR with IFLR_PREFIX:
663  *              get first address that matches the specified prefix.
664  *      SIOCALIFADDR: add the specified address.
665  *      SIOCALIFADDR with IFLR_PREFIX:
666  *              EINVAL since we can't deduce hostid part of the address.
667  *      SIOCDLIFADDR: delete the specified address.
668  *      SIOCDLIFADDR with IFLR_PREFIX:
669  *              delete the first address that matches the specified prefix.
670  * return values:
671  *      EINVAL on invalid parameters
672  *      EADDRNOTAVAIL on prefix match failed/specified address not found
673  *      other values may be returned from in_ioctl()
674  */
675 static int
676 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
677     struct ifnet *ifp, struct thread *td)
678 {
679         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
680         struct ifaddr *ifa;
681
682         /* sanity checks */
683         if (data == NULL || ifp == NULL) {
684                 panic("invalid argument to in_lifaddr_ioctl");
685                 /*NOTRECHED*/
686         }
687
688         switch (cmd) {
689         case SIOCGLIFADDR:
690                 /* address must be specified on GET with IFLR_PREFIX */
691                 if ((iflr->flags & IFLR_PREFIX) == 0)
692                         break;
693                 /*FALLTHROUGH*/
694         case SIOCALIFADDR:
695         case SIOCDLIFADDR:
696                 /* address must be specified on ADD and DELETE */
697                 if (iflr->addr.ss_family != AF_INET)
698                         return (EINVAL);
699                 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
700                         return (EINVAL);
701                 /* XXX need improvement */
702                 if (iflr->dstaddr.ss_family
703                  && iflr->dstaddr.ss_family != AF_INET)
704                         return (EINVAL);
705                 if (iflr->dstaddr.ss_family
706                  && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
707                         return (EINVAL);
708                 break;
709         default: /*shouldn't happen*/
710                 return (EOPNOTSUPP);
711         }
712         if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
713                 return (EINVAL);
714
715         switch (cmd) {
716         case SIOCALIFADDR:
717             {
718                 struct in_aliasreq ifra;
719
720                 if (iflr->flags & IFLR_PREFIX)
721                         return (EINVAL);
722
723                 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
724                 bzero(&ifra, sizeof(ifra));
725                 bcopy(iflr->iflr_name, ifra.ifra_name,
726                         sizeof(ifra.ifra_name));
727
728                 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
729
730                 if (iflr->dstaddr.ss_family) {  /*XXX*/
731                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
732                                 iflr->dstaddr.ss_len);
733                 }
734
735                 ifra.ifra_mask.sin_family = AF_INET;
736                 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
737                 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
738
739                 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
740             }
741         case SIOCGLIFADDR:
742         case SIOCDLIFADDR:
743             {
744                 struct in_ifaddr *ia;
745                 struct in_addr mask, candidate, match;
746                 struct sockaddr_in *sin;
747
748                 bzero(&mask, sizeof(mask));
749                 bzero(&match, sizeof(match));
750                 if (iflr->flags & IFLR_PREFIX) {
751                         /* lookup a prefix rather than address. */
752                         in_len2mask(&mask, iflr->prefixlen);
753
754                         sin = (struct sockaddr_in *)&iflr->addr;
755                         match.s_addr = sin->sin_addr.s_addr;
756                         match.s_addr &= mask.s_addr;
757
758                         /* if you set extra bits, that's wrong */
759                         if (match.s_addr != sin->sin_addr.s_addr)
760                                 return (EINVAL);
761
762                 } else {
763                         /* on getting an address, take the 1st match */
764                         /* on deleting an address, do exact match */
765                         if (cmd != SIOCGLIFADDR) {
766                                 in_len2mask(&mask, 32);
767                                 sin = (struct sockaddr_in *)&iflr->addr;
768                                 match.s_addr = sin->sin_addr.s_addr;
769                         }
770                 }
771
772                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
773                         if (ifa->ifa_addr->sa_family != AF_INET6)
774                                 continue;
775                         if (match.s_addr == 0)
776                                 break;
777                         candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
778                         candidate.s_addr &= mask.s_addr;
779                         if (candidate.s_addr == match.s_addr)
780                                 break;
781                 }
782                 if (ifa == NULL)
783                         return (EADDRNOTAVAIL);
784                 ia = (struct in_ifaddr *)ifa;
785
786                 if (cmd == SIOCGLIFADDR) {
787                         /* fill in the if_laddrreq structure */
788                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
789
790                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
791                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
792                                         ia->ia_dstaddr.sin_len);
793                         } else
794                                 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
795
796                         iflr->prefixlen =
797                                 in_mask2len(&ia->ia_sockmask.sin_addr);
798
799                         iflr->flags = 0;        /*XXX*/
800
801                         return (0);
802                 } else {
803                         struct in_aliasreq ifra;
804
805                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
806                         bzero(&ifra, sizeof(ifra));
807                         bcopy(iflr->iflr_name, ifra.ifra_name,
808                                 sizeof(ifra.ifra_name));
809
810                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
811                                 ia->ia_addr.sin_len);
812                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
813                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
814                                         ia->ia_dstaddr.sin_len);
815                         }
816                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
817                                 ia->ia_sockmask.sin_len);
818
819                         return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
820                             ifp, td));
821                 }
822             }
823         }
824
825         return (EOPNOTSUPP);    /*just for safety*/
826 }
827
828 /*
829  * Delete any existing route for an interface.
830  */
831 void
832 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia, u_int flags)
833 {
834
835         in_scrubprefix(ia, flags);
836 }
837
838 /*
839  * Initialize an interface's internet address
840  * and routing table entry.
841  */
842 static int
843 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
844     int scrub)
845 {
846         register u_long i = ntohl(sin->sin_addr.s_addr);
847         struct sockaddr_in oldaddr;
848         int s = splimp(), flags = RTF_UP, error = 0;
849
850         oldaddr = ia->ia_addr;
851         if (oldaddr.sin_family == AF_INET)
852                 LIST_REMOVE(ia, ia_hash);
853         ia->ia_addr = *sin;
854         if (ia->ia_addr.sin_family == AF_INET) {
855                 IN_IFADDR_WLOCK();
856                 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
857                     ia, ia_hash);
858                 IN_IFADDR_WUNLOCK();
859         }
860         /*
861          * Give the interface a chance to initialize
862          * if this is its first address,
863          * and to validate the address if necessary.
864          */
865         if (ifp->if_ioctl != NULL) {
866                 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
867                 if (error) {
868                         splx(s);
869                         /* LIST_REMOVE(ia, ia_hash) is done in in_control */
870                         ia->ia_addr = oldaddr;
871                         IN_IFADDR_WLOCK();
872                         if (ia->ia_addr.sin_family == AF_INET)
873                                 LIST_INSERT_HEAD(INADDR_HASH(
874                                     ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
875                         else 
876                                 /* 
877                                  * If oldaddr family is not AF_INET (e.g. 
878                                  * interface has been just created) in_control 
879                                  * does not call LIST_REMOVE, and we end up 
880                                  * with bogus ia entries in hash
881                                  */
882                                 LIST_REMOVE(ia, ia_hash);
883                         IN_IFADDR_WUNLOCK();
884                         return (error);
885                 }
886         }
887         splx(s);
888         if (scrub) {
889                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
890                 in_ifscrub(ifp, ia, LLE_STATIC);
891                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
892         }
893         if (IN_CLASSA(i))
894                 ia->ia_netmask = IN_CLASSA_NET;
895         else if (IN_CLASSB(i))
896                 ia->ia_netmask = IN_CLASSB_NET;
897         else
898                 ia->ia_netmask = IN_CLASSC_NET;
899         /*
900          * The subnet mask usually includes at least the standard network part,
901          * but may may be smaller in the case of supernetting.
902          * If it is set, we believe it.
903          */
904         if (ia->ia_subnetmask == 0) {
905                 ia->ia_subnetmask = ia->ia_netmask;
906                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
907         } else
908                 ia->ia_netmask &= ia->ia_subnetmask;
909         ia->ia_net = i & ia->ia_netmask;
910         ia->ia_subnet = i & ia->ia_subnetmask;
911         in_socktrim(&ia->ia_sockmask);
912         /*
913          * XXX: carp(4) does not have interface route
914          */
915         if (ifp->if_type == IFT_CARP)
916                 return (0);
917         /*
918          * Add route for the network.
919          */
920         ia->ia_ifa.ifa_metric = ifp->if_metric;
921         if (ifp->if_flags & IFF_BROADCAST) {
922                 ia->ia_broadaddr.sin_addr.s_addr =
923                         htonl(ia->ia_subnet | ~ia->ia_subnetmask);
924                 ia->ia_netbroadcast.s_addr =
925                         htonl(ia->ia_net | ~ ia->ia_netmask);
926         } else if (ifp->if_flags & IFF_LOOPBACK) {
927                 ia->ia_dstaddr = ia->ia_addr;
928                 flags |= RTF_HOST;
929         } else if (ifp->if_flags & IFF_POINTOPOINT) {
930                 if (ia->ia_dstaddr.sin_family != AF_INET)
931                         return (0);
932                 flags |= RTF_HOST;
933         }
934         if ((error = in_addprefix(ia, flags)) != 0)
935                 return (error);
936
937         if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
938                 return (0);
939
940         if (ifp->if_flags & IFF_POINTOPOINT) {
941                 if (ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
942                         return (0);
943         }
944
945
946         /*
947          * add a loopback route to self
948          */
949         if (V_useloopback && !(ifp->if_flags & IFF_LOOPBACK)) {
950                 struct route ia_ro;
951
952                 bzero(&ia_ro, sizeof(ia_ro));
953                 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr;
954                 rtalloc_ign_fib(&ia_ro, 0, 0);
955                 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
956                     (ia_ro.ro_rt->rt_ifp == V_loif)) {
957                         RT_LOCK(ia_ro.ro_rt);
958                         RT_ADDREF(ia_ro.ro_rt);
959                         RTFREE_LOCKED(ia_ro.ro_rt);
960                 } else
961                         error = ifa_add_loopback_route((struct ifaddr *)ia, 
962                                        (struct sockaddr *)&ia->ia_addr);
963                 if (error == 0)
964                         ia->ia_flags |= IFA_RTSELF;
965                 if (ia_ro.ro_rt != NULL)
966                         RTFREE(ia_ro.ro_rt);
967         }
968
969         return (error);
970 }
971
972 #define rtinitflags(x) \
973         ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
974             ? RTF_HOST : 0)
975
976 /*
977  * Generate a routing message when inserting or deleting 
978  * an interface address alias.
979  */
980 static void in_addralias_rtmsg(int cmd, struct in_addr *prefix, 
981     struct in_ifaddr *target)
982 {
983         struct route pfx_ro;
984         struct sockaddr_in *pfx_addr;
985         struct rtentry msg_rt;
986
987         /* QL: XXX
988          * This is a bit questionable because there is no
989          * additional route entry added/deleted for an address
990          * alias. Therefore this route report is inaccurate.
991          */
992         bzero(&pfx_ro, sizeof(pfx_ro));
993         pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst);
994         pfx_addr->sin_len = sizeof(*pfx_addr);
995         pfx_addr->sin_family = AF_INET;
996         pfx_addr->sin_addr = *prefix;
997         rtalloc_ign_fib(&pfx_ro, 0, 0);
998         if (pfx_ro.ro_rt != NULL) {
999                 msg_rt = *pfx_ro.ro_rt;
1000
1001                 /* QL: XXX
1002                  * Point the gateway to the new interface
1003                  * address as if a new prefix route entry has 
1004                  * been added through the new address alias. 
1005                  * All other parts of the rtentry is accurate, 
1006                  * e.g., rt_key, rt_mask, rt_ifp etc.
1007                  */
1008                 msg_rt.rt_gateway = 
1009                         (struct sockaddr *)&target->ia_addr;
1010                 rt_newaddrmsg(cmd, 
1011                               (struct ifaddr *)target,
1012                               0, &msg_rt);
1013                 RTFREE(pfx_ro.ro_rt);
1014         }
1015         return;
1016 }
1017
1018 /*
1019  * Check if we have a route for the given prefix already or add one accordingly.
1020  */
1021 static int
1022 in_addprefix(struct in_ifaddr *target, int flags)
1023 {
1024         struct in_ifaddr *ia;
1025         struct in_addr prefix, mask, p, m;
1026         int error;
1027
1028         if ((flags & RTF_HOST) != 0) {
1029                 prefix = target->ia_dstaddr.sin_addr;
1030                 mask.s_addr = 0;
1031         } else {
1032                 prefix = target->ia_addr.sin_addr;
1033                 mask = target->ia_sockmask.sin_addr;
1034                 prefix.s_addr &= mask.s_addr;
1035         }
1036
1037         IN_IFADDR_RLOCK();
1038         TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1039                 if (rtinitflags(ia)) {
1040                         p = ia->ia_dstaddr.sin_addr;
1041
1042                         if (prefix.s_addr != p.s_addr)
1043                                 continue;
1044                 } else {
1045                         p = ia->ia_addr.sin_addr;
1046                         m = ia->ia_sockmask.sin_addr;
1047                         p.s_addr &= m.s_addr;
1048
1049                         if (prefix.s_addr != p.s_addr ||
1050                             mask.s_addr != m.s_addr)
1051                                 continue;
1052                 }
1053
1054                 /*
1055                  * If we got a matching prefix route inserted by other
1056                  * interface address, we are done here.
1057                  */
1058                 if (ia->ia_flags & IFA_ROUTE) {
1059 #ifdef RADIX_MPATH
1060                         if (ia->ia_addr.sin_addr.s_addr == 
1061                             target->ia_addr.sin_addr.s_addr) {
1062                                 IN_IFADDR_RUNLOCK();
1063                                 return (EEXIST);
1064                         } else
1065                                 break;
1066 #endif
1067                         if (V_sameprefixcarponly &&
1068                             target->ia_ifp->if_type != IFT_CARP &&
1069                             ia->ia_ifp->if_type != IFT_CARP) {
1070                                 IN_IFADDR_RUNLOCK();
1071                                 return (EEXIST);
1072                         } else {
1073                                 in_addralias_rtmsg(RTM_ADD, &prefix, target);
1074                                 IN_IFADDR_RUNLOCK();
1075                                 return (0);
1076                         }
1077                 }
1078         }
1079         IN_IFADDR_RUNLOCK();
1080
1081         /*
1082          * No-one seem to have this prefix route, so we try to insert it.
1083          */
1084         error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
1085         if (!error)
1086                 target->ia_flags |= IFA_ROUTE;
1087         return (error);
1088 }
1089
1090 extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
1091
1092 /*
1093  * If there is no other address in the system that can serve a route to the
1094  * same prefix, remove the route.  Hand over the route to the new address
1095  * otherwise.
1096  */
1097 static int
1098 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1099 {
1100         struct in_ifaddr *ia;
1101         struct in_addr prefix, mask, p;
1102         int error = 0;
1103         struct sockaddr_in prefix0, mask0;
1104
1105         /*
1106          * Remove the loopback route to the interface address.
1107          * The "useloopback" setting is not consulted because if the
1108          * user configures an interface address, turns off this
1109          * setting, and then tries to delete that interface address,
1110          * checking the current setting of "useloopback" would leave
1111          * that interface address loopback route untouched, which
1112          * would be wrong. Therefore the interface address loopback route
1113          * deletion is unconditional.
1114          */
1115         if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
1116             !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
1117             (target->ia_flags & IFA_RTSELF)) {
1118                 struct route ia_ro;
1119                 int freeit = 0;
1120
1121                 bzero(&ia_ro, sizeof(ia_ro));
1122                 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr;
1123                 rtalloc_ign_fib(&ia_ro, 0, 0);
1124                 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
1125                     (ia_ro.ro_rt->rt_ifp == V_loif)) {
1126                         RT_LOCK(ia_ro.ro_rt);
1127                         if (ia_ro.ro_rt->rt_refcnt <= 1)
1128                                 freeit = 1;
1129                         else
1130                                 RT_REMREF(ia_ro.ro_rt);
1131                         RTFREE_LOCKED(ia_ro.ro_rt);
1132                 }
1133                 if (freeit && (flags & LLE_STATIC)) {
1134                         error = ifa_del_loopback_route((struct ifaddr *)target,
1135                                        (struct sockaddr *)&target->ia_addr);
1136                         if (error == 0)
1137                                 target->ia_flags &= ~IFA_RTSELF;
1138                 }
1139                 if (flags & LLE_STATIC)
1140                         /* remove arp cache */
1141                         arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
1142         }
1143
1144         if (rtinitflags(target))
1145                 prefix = target->ia_dstaddr.sin_addr;
1146         else {
1147                 prefix = target->ia_addr.sin_addr;
1148                 mask = target->ia_sockmask.sin_addr;
1149                 prefix.s_addr &= mask.s_addr;
1150         }
1151
1152         if ((target->ia_flags & IFA_ROUTE) == 0) {
1153                 in_addralias_rtmsg(RTM_DELETE, &prefix, target);
1154                 return (0);
1155         }
1156
1157         IN_IFADDR_RLOCK();
1158         TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1159                 if (rtinitflags(ia))
1160                         p = ia->ia_dstaddr.sin_addr;
1161                 else {
1162                         p = ia->ia_addr.sin_addr;
1163                         p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1164                 }
1165
1166                 if ((prefix.s_addr != p.s_addr) ||
1167                     !(ia->ia_ifp->if_flags & IFF_UP))
1168                         continue;
1169
1170                 /*
1171                  * If we got a matching prefix address, move IFA_ROUTE and
1172                  * the route itself to it.  Make sure that routing daemons
1173                  * get a heads-up.
1174                  *
1175                  * XXX: a special case for carp(4) interface - this should
1176                  *      be more generally specified as an interface that
1177                  *      doesn't support such action.
1178                  */
1179                 if ((ia->ia_flags & IFA_ROUTE) == 0
1180                     && (ia->ia_ifp->if_type != IFT_CARP)) {
1181                         ifa_ref(&ia->ia_ifa);
1182                         IN_IFADDR_RUNLOCK();
1183                         error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
1184                             rtinitflags(target));
1185                         if (error == 0)
1186                                 target->ia_flags &= ~IFA_ROUTE;
1187                         else
1188                                 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1189                                         error);
1190                         error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
1191                             rtinitflags(ia) | RTF_UP);
1192                         if (error == 0)
1193                                 ia->ia_flags |= IFA_ROUTE;
1194                         else
1195                                 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1196                                         error);
1197                         ifa_free(&ia->ia_ifa);
1198                         return (error);
1199                 }
1200         }
1201         IN_IFADDR_RUNLOCK();
1202
1203         /*
1204          * remove all L2 entries on the given prefix
1205          */
1206         bzero(&prefix0, sizeof(prefix0));
1207         prefix0.sin_len = sizeof(prefix0);
1208         prefix0.sin_family = AF_INET;
1209         prefix0.sin_addr.s_addr = target->ia_subnet;
1210         bzero(&mask0, sizeof(mask0));
1211         mask0.sin_len = sizeof(mask0);
1212         mask0.sin_family = AF_INET;
1213         mask0.sin_addr.s_addr = target->ia_subnetmask;
1214         lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0, 
1215                             (struct sockaddr *)&mask0, flags);
1216
1217         /*
1218          * As no-one seem to have this prefix, we can remove the route.
1219          */
1220         error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1221         if (error == 0)
1222                 target->ia_flags &= ~IFA_ROUTE;
1223         else
1224                 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1225         return (error);
1226 }
1227
1228 #undef rtinitflags
1229
1230 /*
1231  * Return 1 if the address might be a local broadcast address.
1232  */
1233 int
1234 in_broadcast(struct in_addr in, struct ifnet *ifp)
1235 {
1236         register struct ifaddr *ifa;
1237         u_long t;
1238
1239         if (in.s_addr == INADDR_BROADCAST ||
1240             in.s_addr == INADDR_ANY)
1241                 return (1);
1242         if ((ifp->if_flags & IFF_BROADCAST) == 0)
1243                 return (0);
1244         t = ntohl(in.s_addr);
1245         /*
1246          * Look through the list of addresses for a match
1247          * with a broadcast address.
1248          */
1249 #define ia ((struct in_ifaddr *)ifa)
1250         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1251                 if (ifa->ifa_addr->sa_family == AF_INET &&
1252                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1253                      in.s_addr == ia->ia_netbroadcast.s_addr ||
1254                      /*
1255                       * Check for old-style (host 0) broadcast.
1256                       */
1257                      t == ia->ia_subnet || t == ia->ia_net) &&
1258                      /*
1259                       * Check for an all one subnetmask. These
1260                       * only exist when an interface gets a secondary
1261                       * address.
1262                       */
1263                      ia->ia_subnetmask != (u_long)0xffffffff)
1264                             return (1);
1265         return (0);
1266 #undef ia
1267 }
1268
1269 /*
1270  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1271  */
1272 void
1273 in_ifdetach(struct ifnet *ifp)
1274 {
1275
1276         in_pcbpurgeif0(&V_ripcbinfo, ifp);
1277         in_pcbpurgeif0(&V_udbinfo, ifp);
1278         in_purgemaddrs(ifp);
1279 }
1280
1281 /*
1282  * Delete all IPv4 multicast address records, and associated link-layer
1283  * multicast address records, associated with ifp.
1284  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1285  * XXX This should not race with ifma_protospec being set during
1286  * a new allocation, if it does, we have bigger problems.
1287  */
1288 static void
1289 in_purgemaddrs(struct ifnet *ifp)
1290 {
1291         LIST_HEAD(,in_multi) purgeinms;
1292         struct in_multi         *inm, *tinm;
1293         struct ifmultiaddr      *ifma;
1294
1295         LIST_INIT(&purgeinms);
1296         IN_MULTI_LOCK();
1297
1298         /*
1299          * Extract list of in_multi associated with the detaching ifp
1300          * which the PF_INET layer is about to release.
1301          * We need to do this as IF_ADDR_LOCK() may be re-acquired
1302          * by code further down.
1303          */
1304         IF_ADDR_LOCK(ifp);
1305         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1306                 if (ifma->ifma_addr->sa_family != AF_INET ||
1307                     ifma->ifma_protospec == NULL)
1308                         continue;
1309 #if 0
1310                 KASSERT(ifma->ifma_protospec != NULL,
1311                     ("%s: ifma_protospec is NULL", __func__));
1312 #endif
1313                 inm = (struct in_multi *)ifma->ifma_protospec;
1314                 LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1315         }
1316         IF_ADDR_UNLOCK(ifp);
1317
1318         LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1319                 LIST_REMOVE(inm, inm_link);
1320                 inm_release_locked(inm);
1321         }
1322         igmp_ifdetach(ifp);
1323
1324         IN_MULTI_UNLOCK();
1325 }
1326
1327 #include <net/if_dl.h>
1328 #include <netinet/if_ether.h>
1329
1330 struct in_llentry {
1331         struct llentry          base;
1332         struct sockaddr_in      l3_addr4;
1333 };
1334
1335 static struct llentry *
1336 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
1337 {
1338         struct in_llentry *lle;
1339
1340         lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
1341         if (lle == NULL)                /* NB: caller generates msg */
1342                 return NULL;
1343
1344         callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
1345         /*
1346          * For IPv4 this will trigger "arpresolve" to generate
1347          * an ARP request.
1348          */
1349         lle->base.la_expire = time_uptime; /* mark expired */
1350         lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
1351         lle->base.lle_refcnt = 1;
1352         LLE_LOCK_INIT(&lle->base);
1353         return &lle->base;
1354 }
1355
1356 /*
1357  * Deletes an address from the address table.
1358  * This function is called by the timer functions
1359  * such as arptimer() and nd6_llinfo_timer(), and
1360  * the caller does the locking.
1361  */
1362 static void
1363 in_lltable_free(struct lltable *llt, struct llentry *lle)
1364 {
1365         LLE_WUNLOCK(lle);
1366         LLE_LOCK_DESTROY(lle);
1367         free(lle, M_LLTABLE);
1368 }
1369
1370
1371 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)       (                       \
1372             (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1373
1374 static void
1375 in_lltable_prefix_free(struct lltable *llt, 
1376                        const struct sockaddr *prefix,
1377                        const struct sockaddr *mask,
1378                        u_int flags)
1379 {
1380         const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1381         const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1382         struct llentry *lle, *next;
1383         register int i;
1384         size_t pkts_dropped;
1385
1386         for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
1387                 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1388
1389                         /* 
1390                          * (flags & LLE_STATIC) means deleting all entries
1391                          * including static ARP entries
1392                          */
1393                         if (IN_ARE_MASKED_ADDR_EQUAL((struct sockaddr_in *)L3_ADDR(lle), 
1394                                                      pfx, msk) &&
1395                             ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) {
1396                                 int canceled;
1397
1398                                 canceled = callout_drain(&lle->la_timer);
1399                                 LLE_WLOCK(lle);
1400                                 if (canceled)
1401                                         LLE_REMREF(lle);
1402                                 pkts_dropped = llentry_free(lle);
1403                                 ARPSTAT_ADD(dropped, pkts_dropped);
1404                         }
1405                 }
1406         }
1407 }
1408
1409
1410 static int
1411 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1412 {
1413         struct rtentry *rt;
1414
1415         KASSERT(l3addr->sa_family == AF_INET,
1416             ("sin_family %d", l3addr->sa_family));
1417
1418         /* XXX rtalloc1 should take a const param */
1419         rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1420
1421         /*
1422          * If the gateway for an existing host route matches the target L3
1423          * address, allow for ARP to proceed.
1424          */
1425         if (rt != NULL && (rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) &&
1426             rt->rt_gateway->sa_family == AF_INET &&
1427             memcmp(rt->rt_gateway->sa_data, l3addr->sa_data, 4) == 0) {
1428                 RTFREE_LOCKED(rt);
1429                 return (0);
1430         }
1431
1432         if (rt == NULL || (!(flags & LLE_PUB) &&
1433                            ((rt->rt_flags & RTF_GATEWAY) || 
1434                             (rt->rt_ifp != ifp)))) {
1435 #ifdef DIAGNOSTIC
1436                 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1437                     inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1438 #endif
1439                 if (rt != NULL)
1440                         RTFREE_LOCKED(rt);
1441                 return (EINVAL);
1442         }
1443         RTFREE_LOCKED(rt);
1444         return 0;
1445 }
1446
1447 /*
1448  * Return NULL if not found or marked for deletion.
1449  * If found return lle read locked.
1450  */
1451 static struct llentry *
1452 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1453 {
1454         const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1455         struct ifnet *ifp = llt->llt_ifp;
1456         struct llentry *lle;
1457         struct llentries *lleh;
1458         u_int hashkey;
1459
1460         IF_AFDATA_LOCK_ASSERT(ifp);
1461         KASSERT(l3addr->sa_family == AF_INET,
1462             ("sin_family %d", l3addr->sa_family));
1463
1464         hashkey = sin->sin_addr.s_addr;
1465         lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1466         LIST_FOREACH(lle, lleh, lle_next) {
1467                 struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle);
1468                 if (lle->la_flags & LLE_DELETED)
1469                         continue;
1470                 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1471                         break;
1472         }
1473         if (lle == NULL) {
1474 #ifdef DIAGNOSTIC
1475                 if (flags & LLE_DELETE)
1476                         log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);        
1477 #endif
1478                 if (!(flags & LLE_CREATE))
1479                         return (NULL);
1480                 /*
1481                  * A route that covers the given address must have
1482                  * been installed 1st because we are doing a resolution,
1483                  * verify this.
1484                  */
1485                 if (!(flags & LLE_IFADDR) &&
1486                     in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1487                         goto done;
1488
1489                 lle = in_lltable_new(l3addr, flags);
1490                 if (lle == NULL) {
1491                         log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1492                         goto done;
1493                 }
1494                 lle->la_flags = flags & ~LLE_CREATE;
1495                 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1496                         bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1497                         lle->la_flags |= (LLE_VALID | LLE_STATIC);
1498                 }
1499
1500                 lle->lle_tbl  = llt;
1501                 lle->lle_head = lleh;
1502                 LIST_INSERT_HEAD(lleh, lle, lle_next);
1503         } else if (flags & LLE_DELETE) {
1504                 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1505                         LLE_WLOCK(lle);
1506                         lle->la_flags = LLE_DELETED;
1507                         EVENTHANDLER_INVOKE(arp_update_event, lle);
1508                         LLE_WUNLOCK(lle);
1509 #ifdef DIAGNOSTIC
1510                         log(LOG_INFO, "ifaddr cache = %p  is deleted\n", lle);  
1511 #endif
1512                 }
1513                 lle = (void *)-1;
1514                 
1515         }
1516         if (LLE_IS_VALID(lle)) {
1517                 if (flags & LLE_EXCLUSIVE)
1518                         LLE_WLOCK(lle);
1519                 else
1520                         LLE_RLOCK(lle);
1521         }
1522 done:
1523         return (lle);
1524 }
1525
1526 static int
1527 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1528 {
1529 #define SIN(lle)        ((struct sockaddr_in *) L3_ADDR(lle))
1530         struct ifnet *ifp = llt->llt_ifp;
1531         struct llentry *lle;
1532         /* XXX stack use */
1533         struct {
1534                 struct rt_msghdr        rtm;
1535                 struct sockaddr_inarp   sin;
1536                 struct sockaddr_dl      sdl;
1537         } arpc;
1538         int error, i;
1539
1540         LLTABLE_LOCK_ASSERT();
1541
1542         error = 0;
1543         for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1544                 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1545                         struct sockaddr_dl *sdl;
1546                         
1547                         /* skip deleted entries */
1548                         if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1549                                 continue;
1550                         /* Skip if jailed and not a valid IP of the prison. */
1551                         if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1552                                 continue;
1553                         /*
1554                          * produce a msg made of:
1555                          *  struct rt_msghdr;
1556                          *  struct sockaddr_inarp; (IPv4)
1557                          *  struct sockaddr_dl;
1558                          */
1559                         bzero(&arpc, sizeof(arpc));
1560                         arpc.rtm.rtm_msglen = sizeof(arpc);
1561                         arpc.rtm.rtm_version = RTM_VERSION;
1562                         arpc.rtm.rtm_type = RTM_GET;
1563                         arpc.rtm.rtm_flags = RTF_UP;
1564                         arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1565                         arpc.sin.sin_family = AF_INET;
1566                         arpc.sin.sin_len = sizeof(arpc.sin);
1567                         arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1568
1569                         /* publish */
1570                         if (lle->la_flags & LLE_PUB) {
1571                                 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1572                                 /* proxy only */
1573                                 if (lle->la_flags & LLE_PROXY)
1574                                         arpc.sin.sin_other = SIN_PROXY;
1575                         }
1576
1577                         sdl = &arpc.sdl;
1578                         sdl->sdl_family = AF_LINK;
1579                         sdl->sdl_len = sizeof(*sdl);
1580                         sdl->sdl_index = ifp->if_index;
1581                         sdl->sdl_type = ifp->if_type;
1582                         if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1583                                 sdl->sdl_alen = ifp->if_addrlen;
1584                                 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1585                         } else {
1586                                 sdl->sdl_alen = 0;
1587                                 bzero(LLADDR(sdl), ifp->if_addrlen);
1588                         }
1589
1590                         arpc.rtm.rtm_rmx.rmx_expire =
1591                             lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1592                         arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1593                         if (lle->la_flags & LLE_STATIC)
1594                                 arpc.rtm.rtm_flags |= RTF_STATIC;
1595                         arpc.rtm.rtm_index = ifp->if_index;
1596                         error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1597                         if (error)
1598                                 break;
1599                 }
1600         }
1601         return error;
1602 #undef SIN
1603 }
1604
1605 void *
1606 in_domifattach(struct ifnet *ifp)
1607 {
1608         struct in_ifinfo *ii;
1609         struct lltable *llt;
1610
1611         ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1612
1613         llt = lltable_init(ifp, AF_INET);
1614         if (llt != NULL) {
1615                 llt->llt_free = in_lltable_free;
1616                 llt->llt_prefix_free = in_lltable_prefix_free;
1617                 llt->llt_lookup = in_lltable_lookup;
1618                 llt->llt_dump = in_lltable_dump;
1619         }
1620         ii->ii_llt = llt;
1621
1622         ii->ii_igmp = igmp_domifattach(ifp);
1623
1624         return ii;
1625 }
1626
1627 void
1628 in_domifdetach(struct ifnet *ifp, void *aux)
1629 {
1630         struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1631
1632         igmp_domifdetach(ifp);
1633         lltable_free(ii->ii_llt);
1634         free(ii, M_IFADDR);
1635 }