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