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