in.c revision 1.204 1 /* $NetBSD: in.c,v 1.204 2017/06/22 09:23:10 ozaki-r Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 */
61
62 /*
63 * Copyright (c) 1982, 1986, 1991, 1993
64 * The Regents of the University of California. All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
77 *
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * SUCH DAMAGE.
89 *
90 * @(#)in.c 8.4 (Berkeley) 1/9/95
91 */
92
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.204 2017/06/22 09:23:10 ozaki-r Exp $");
95
96 #include "arp.h"
97
98 #ifdef _KERNEL_OPT
99 #include "opt_inet.h"
100 #include "opt_inet_conf.h"
101 #include "opt_mrouting.h"
102 #include "opt_net_mpsafe.h"
103 #endif
104
105 #include <sys/param.h>
106 #include <sys/ioctl.h>
107 #include <sys/errno.h>
108 #include <sys/kernel.h>
109 #include <sys/malloc.h>
110 #include <sys/socket.h>
111 #include <sys/socketvar.h>
112 #include <sys/sysctl.h>
113 #include <sys/systm.h>
114 #include <sys/proc.h>
115 #include <sys/syslog.h>
116 #include <sys/kauth.h>
117 #include <sys/kmem.h>
118
119 #include <sys/cprng.h>
120
121 #include <net/if.h>
122 #include <net/route.h>
123 #include <net/pfil.h>
124
125 #include <net/if_arp.h>
126 #include <net/if_ether.h>
127 #include <net/if_types.h>
128 #include <net/if_llatbl.h>
129 #include <net/if_dl.h>
130
131 #include <netinet/in_systm.h>
132 #include <netinet/in.h>
133 #include <netinet/in_var.h>
134 #include <netinet/ip.h>
135 #include <netinet/ip_var.h>
136 #include <netinet/in_ifattach.h>
137 #include <netinet/in_pcb.h>
138 #include <netinet/in_selsrc.h>
139 #include <netinet/if_inarp.h>
140 #include <netinet/ip_mroute.h>
141 #include <netinet/igmp_var.h>
142
143 #ifdef IPSELSRC
144 #include <netinet/in_selsrc.h>
145 #endif
146
147 static u_int in_mask2len(struct in_addr *);
148 static void in_len2mask(struct in_addr *, u_int);
149 static int in_lifaddr_ioctl(struct socket *, u_long, void *,
150 struct ifnet *);
151
152 static void in_addrhash_insert_locked(struct in_ifaddr *);
153 static void in_addrhash_remove_locked(struct in_ifaddr *);
154
155 static int in_addprefix(struct in_ifaddr *, int);
156 static void in_scrubaddr(struct in_ifaddr *);
157 static int in_scrubprefix(struct in_ifaddr *);
158 static void in_sysctl_init(struct sysctllog **);
159
160 #ifndef SUBNETSARELOCAL
161 #define SUBNETSARELOCAL 1
162 #endif
163
164 #ifndef HOSTZEROBROADCAST
165 #define HOSTZEROBROADCAST 0
166 #endif
167
168 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */
169 #ifndef IN_MULTI_HASH_SIZE
170 #define IN_MULTI_HASH_SIZE 509
171 #endif
172
173 static int subnetsarelocal = SUBNETSARELOCAL;
174 static int hostzeroisbroadcast = HOSTZEROBROADCAST;
175
176 /*
177 * This list is used to keep track of in_multi chains which belong to
178 * deleted interface addresses. We use in_ifaddr so that a chain head
179 * won't be deallocated until all multicast address record are deleted.
180 */
181
182 LIST_HEAD(in_multihashhead, in_multi); /* Type of the hash head */
183
184 static struct pool inmulti_pool;
185 static u_int in_multientries;
186 static struct in_multihashhead *in_multihashtbl;
187 static u_long in_multihash;
188 static krwlock_t in_multilock;
189
190 #define IN_MULTI_HASH(x, ifp) \
191 (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE])
192
193 /* XXX DEPRECATED. Keep them to avoid breaking kvm(3) users. */
194 struct in_ifaddrhashhead * in_ifaddrhashtbl;
195 u_long in_ifaddrhash;
196 struct in_ifaddrhead in_ifaddrhead;
197 static kmutex_t in_ifaddr_lock;
198
199 pserialize_t in_ifaddrhash_psz;
200 struct pslist_head * in_ifaddrhashtbl_pslist;
201 u_long in_ifaddrhash_pslist;
202 struct pslist_head in_ifaddrhead_pslist;
203
204 void
205 in_init(void)
206 {
207 pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
208 NULL, IPL_SOFTNET);
209 TAILQ_INIT(&in_ifaddrhead);
210 PSLIST_INIT(&in_ifaddrhead_pslist);
211
212 in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
213 &in_ifaddrhash);
214
215 in_ifaddrhash_psz = pserialize_create();
216 in_ifaddrhashtbl_pslist = hashinit(IN_IFADDR_HASH_SIZE, HASH_PSLIST,
217 true, &in_ifaddrhash_pslist);
218 mutex_init(&in_ifaddr_lock, MUTEX_DEFAULT, IPL_NONE);
219
220 in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
221 &in_multihash);
222 rw_init(&in_multilock);
223
224 in_sysctl_init(NULL);
225 }
226
227 /*
228 * Return 1 if an internet address is for a ``local'' host
229 * (one to which we have a connection). If subnetsarelocal
230 * is true, this includes other subnets of the local net.
231 * Otherwise, it includes only the directly-connected (sub)nets.
232 */
233 int
234 in_localaddr(struct in_addr in)
235 {
236 struct in_ifaddr *ia;
237 int localaddr = 0;
238 int s = pserialize_read_enter();
239
240 if (subnetsarelocal) {
241 IN_ADDRLIST_READER_FOREACH(ia) {
242 if ((in.s_addr & ia->ia_netmask) == ia->ia_net) {
243 localaddr = 1;
244 break;
245 }
246 }
247 } else {
248 IN_ADDRLIST_READER_FOREACH(ia) {
249 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) {
250 localaddr = 1;
251 break;
252 }
253 }
254 }
255 pserialize_read_exit(s);
256
257 return localaddr;
258 }
259
260 /*
261 * Determine whether an IP address is in a reserved set of addresses
262 * that may not be forwarded, or whether datagrams to that destination
263 * may be forwarded.
264 */
265 int
266 in_canforward(struct in_addr in)
267 {
268 u_int32_t net;
269
270 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
271 return (0);
272 if (IN_CLASSA(in.s_addr)) {
273 net = in.s_addr & IN_CLASSA_NET;
274 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
275 return (0);
276 }
277 return (1);
278 }
279
280 /*
281 * Trim a mask in a sockaddr
282 */
283 void
284 in_socktrim(struct sockaddr_in *ap)
285 {
286 char *cplim = (char *) &ap->sin_addr;
287 char *cp = (char *) (&ap->sin_addr + 1);
288
289 ap->sin_len = 0;
290 while (--cp >= cplim)
291 if (*cp) {
292 (ap)->sin_len = cp - (char *) (ap) + 1;
293 break;
294 }
295 }
296
297 /*
298 * Maintain the "in_maxmtu" variable, which is the largest
299 * mtu for non-local interfaces with AF_INET addresses assigned
300 * to them that are up.
301 */
302 unsigned long in_maxmtu;
303
304 void
305 in_setmaxmtu(void)
306 {
307 struct in_ifaddr *ia;
308 struct ifnet *ifp;
309 unsigned long maxmtu = 0;
310 int s = pserialize_read_enter();
311
312 IN_ADDRLIST_READER_FOREACH(ia) {
313 if ((ifp = ia->ia_ifp) == 0)
314 continue;
315 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
316 continue;
317 if (ifp->if_mtu > maxmtu)
318 maxmtu = ifp->if_mtu;
319 }
320 if (maxmtu)
321 in_maxmtu = maxmtu;
322 pserialize_read_exit(s);
323 }
324
325 static u_int
326 in_mask2len(struct in_addr *mask)
327 {
328 u_int x, y;
329 u_char *p;
330
331 p = (u_char *)mask;
332 for (x = 0; x < sizeof(*mask); x++) {
333 if (p[x] != 0xff)
334 break;
335 }
336 y = 0;
337 if (x < sizeof(*mask)) {
338 for (y = 0; y < NBBY; y++) {
339 if ((p[x] & (0x80 >> y)) == 0)
340 break;
341 }
342 }
343 return x * NBBY + y;
344 }
345
346 static void
347 in_len2mask(struct in_addr *mask, u_int len)
348 {
349 u_int i;
350 u_char *p;
351
352 p = (u_char *)mask;
353 memset(mask, 0, sizeof(*mask));
354 for (i = 0; i < len / NBBY; i++)
355 p[i] = 0xff;
356 if (len % NBBY)
357 p[i] = (0xff00 >> (len % NBBY)) & 0xff;
358 }
359
360 /*
361 * Generic internet control operations (ioctl's).
362 * Ifp is 0 if not an interface-specific ioctl.
363 */
364 /* ARGSUSED */
365 static int
366 in_control0(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
367 {
368 struct ifreq *ifr = (struct ifreq *)data;
369 struct in_ifaddr *ia = NULL;
370 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
371 struct sockaddr_in oldaddr, *new_dstaddr;
372 int error, hostIsNew, maskIsNew;
373 int newifaddr = 0;
374 bool run_hook = false;
375 bool need_reinsert = false;
376 struct psref psref;
377 int bound;
378
379 switch (cmd) {
380 case SIOCALIFADDR:
381 case SIOCDLIFADDR:
382 case SIOCGLIFADDR:
383 if (ifp == NULL)
384 return EINVAL;
385 return in_lifaddr_ioctl(so, cmd, data, ifp);
386 case SIOCGIFADDRPREF:
387 case SIOCSIFADDRPREF:
388 if (ifp == NULL)
389 return EINVAL;
390 return ifaddrpref_ioctl(so, cmd, data, ifp);
391 }
392
393 bound = curlwp_bind();
394 /*
395 * Find address for this interface, if it exists.
396 */
397 if (ifp != NULL)
398 ia = in_get_ia_from_ifp_psref(ifp, &psref);
399
400 hostIsNew = 1; /* moved here to appease gcc */
401 switch (cmd) {
402 case SIOCAIFADDR:
403 case SIOCDIFADDR:
404 case SIOCGIFALIAS:
405 case SIOCGIFAFLAG_IN:
406 if (ifra->ifra_addr.sin_family == AF_INET) {
407 int s;
408
409 if (ia != NULL)
410 ia4_release(ia, &psref);
411 s = pserialize_read_enter();
412 IN_ADDRHASH_READER_FOREACH(ia,
413 ifra->ifra_addr.sin_addr.s_addr) {
414 if (ia->ia_ifp == ifp &&
415 in_hosteq(ia->ia_addr.sin_addr,
416 ifra->ifra_addr.sin_addr))
417 break;
418 }
419 if (ia != NULL)
420 ia4_acquire(ia, &psref);
421 pserialize_read_exit(s);
422 }
423 if ((cmd == SIOCDIFADDR ||
424 cmd == SIOCGIFALIAS ||
425 cmd == SIOCGIFAFLAG_IN) &&
426 ia == NULL) {
427 error = EADDRNOTAVAIL;
428 goto out;
429 }
430
431 if (cmd == SIOCDIFADDR &&
432 ifra->ifra_addr.sin_family == AF_UNSPEC) {
433 ifra->ifra_addr.sin_family = AF_INET;
434 }
435 /* FALLTHROUGH */
436 case SIOCSIFADDR:
437 if (ia == NULL || ia->ia_addr.sin_family != AF_INET)
438 ;
439 else if (ifra->ifra_addr.sin_len == 0) {
440 ifra->ifra_addr = ia->ia_addr;
441 hostIsNew = 0;
442 } else if (in_hosteq(ia->ia_addr.sin_addr,
443 ifra->ifra_addr.sin_addr))
444 hostIsNew = 0;
445 /* FALLTHROUGH */
446 case SIOCSIFDSTADDR:
447 if (ifra->ifra_addr.sin_family != AF_INET) {
448 error = EAFNOSUPPORT;
449 goto out;
450 }
451 /* FALLTHROUGH */
452 case SIOCSIFNETMASK:
453 if (ifp == NULL)
454 panic("in_control");
455
456 if (cmd == SIOCGIFALIAS || cmd == SIOCGIFAFLAG_IN)
457 break;
458
459 if (ia == NULL &&
460 (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR)) {
461 error = EADDRNOTAVAIL;
462 goto out;
463 }
464
465 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
466 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
467 NULL) != 0) {
468 error = EPERM;
469 goto out;
470 }
471
472 if (ia == NULL) {
473 ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
474 if (ia == NULL) {
475 error = ENOBUFS;
476 goto out;
477 }
478 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
479 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
480 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
481 #ifdef IPSELSRC
482 ia->ia_ifa.ifa_getifa = in_getifa;
483 #else /* IPSELSRC */
484 ia->ia_ifa.ifa_getifa = NULL;
485 #endif /* IPSELSRC */
486 ia->ia_sockmask.sin_len = 8;
487 ia->ia_sockmask.sin_family = AF_INET;
488 if (ifp->if_flags & IFF_BROADCAST) {
489 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
490 ia->ia_broadaddr.sin_family = AF_INET;
491 }
492 ia->ia_ifp = ifp;
493 ia->ia_idsalt = cprng_fast32() % 65535;
494 LIST_INIT(&ia->ia_multiaddrs);
495 IN_ADDRHASH_ENTRY_INIT(ia);
496 IN_ADDRLIST_ENTRY_INIT(ia);
497 ifa_psref_init(&ia->ia_ifa);
498 /*
499 * We need a reference to make ia survive over in_ifinit
500 * that does ifaref and ifafree.
501 */
502 ifaref(&ia->ia_ifa);
503
504 newifaddr = 1;
505 }
506 break;
507
508 case SIOCSIFBRDADDR:
509 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
510 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
511 NULL) != 0) {
512 error = EPERM;
513 goto out;
514 }
515 /* FALLTHROUGH */
516
517 case SIOCGIFADDR:
518 case SIOCGIFNETMASK:
519 case SIOCGIFDSTADDR:
520 case SIOCGIFBRDADDR:
521 if (ia == NULL) {
522 error = EADDRNOTAVAIL;
523 goto out;
524 }
525 break;
526 }
527 error = 0;
528 switch (cmd) {
529
530 case SIOCGIFADDR:
531 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
532 break;
533
534 case SIOCGIFBRDADDR:
535 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
536 error = EINVAL;
537 goto out;
538 }
539 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
540 break;
541
542 case SIOCGIFDSTADDR:
543 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
544 error = EINVAL;
545 goto out;
546 }
547 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
548 break;
549
550 case SIOCGIFNETMASK:
551 /*
552 * We keep the number of trailing zero bytes the sin_len field
553 * of ia_sockmask, so we fix this before we pass it back to
554 * userland.
555 */
556 oldaddr = ia->ia_sockmask;
557 oldaddr.sin_len = sizeof(struct sockaddr_in);
558 ifreq_setaddr(cmd, ifr, (const void *)&oldaddr);
559 break;
560
561 case SIOCSIFDSTADDR:
562 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
563 error = EINVAL;
564 goto out;
565 }
566 oldaddr = ia->ia_dstaddr;
567 ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
568 if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) {
569 ia->ia_dstaddr = oldaddr;
570 goto out;
571 }
572 if (ia->ia_flags & IFA_ROUTE) {
573 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
574 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
575 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
576 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
577 }
578 break;
579
580 case SIOCSIFBRDADDR:
581 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
582 error = EINVAL;
583 goto out;
584 }
585 ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
586 break;
587
588 case SIOCSIFADDR:
589 if (!newifaddr) {
590 in_addrhash_remove(ia);
591 need_reinsert = true;
592 }
593 error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
594 NULL, 1);
595
596 run_hook = true;
597 break;
598
599 case SIOCSIFNETMASK:
600 in_scrubprefix(ia);
601 ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
602 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
603 if (!newifaddr) {
604 in_addrhash_remove(ia);
605 need_reinsert = true;
606 }
607 error = in_ifinit(ifp, ia, NULL, NULL, 0);
608 break;
609
610 case SIOCAIFADDR:
611 maskIsNew = 0;
612 if (ifra->ifra_mask.sin_len) {
613 in_scrubprefix(ia);
614 ia->ia_sockmask = ifra->ifra_mask;
615 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
616 maskIsNew = 1;
617 }
618 if ((ifp->if_flags & IFF_POINTOPOINT) &&
619 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
620 new_dstaddr = &ifra->ifra_dstaddr;
621 maskIsNew = 1; /* We lie; but the effect's the same */
622 } else
623 new_dstaddr = NULL;
624 if (ifra->ifra_addr.sin_family == AF_INET &&
625 (hostIsNew || maskIsNew)) {
626 if (!newifaddr) {
627 in_addrhash_remove(ia);
628 need_reinsert = true;
629 }
630 error = in_ifinit(ifp, ia, &ifra->ifra_addr,
631 new_dstaddr, 0);
632 }
633 if ((ifp->if_flags & IFF_BROADCAST) &&
634 (ifra->ifra_broadaddr.sin_family == AF_INET))
635 ia->ia_broadaddr = ifra->ifra_broadaddr;
636 run_hook = true;
637 break;
638
639 case SIOCGIFALIAS:
640 ifra->ifra_mask = ia->ia_sockmask;
641 if ((ifp->if_flags & IFF_POINTOPOINT) &&
642 (ia->ia_dstaddr.sin_family == AF_INET))
643 ifra->ifra_dstaddr = ia->ia_dstaddr;
644 else if ((ifp->if_flags & IFF_BROADCAST) &&
645 (ia->ia_broadaddr.sin_family == AF_INET))
646 ifra->ifra_broadaddr = ia->ia_broadaddr;
647 else
648 memset(&ifra->ifra_broadaddr, 0,
649 sizeof(ifra->ifra_broadaddr));
650 break;
651
652 case SIOCGIFAFLAG_IN:
653 ifr->ifr_addrflags = ia->ia4_flags;
654 break;
655
656 case SIOCDIFADDR:
657 ia4_release(ia, &psref);
658 ifaref(&ia->ia_ifa);
659 in_purgeaddr(&ia->ia_ifa);
660 pfil_run_addrhooks(if_pfil, cmd, &ia->ia_ifa);
661 ifafree(&ia->ia_ifa);
662 ia = NULL;
663 break;
664
665 #ifdef MROUTING
666 case SIOCGETVIFCNT:
667 case SIOCGETSGCNT:
668 error = mrt_ioctl(so, cmd, data);
669 break;
670 #endif /* MROUTING */
671
672 default:
673 error = ENOTTY;
674 goto out;
675 }
676
677 /*
678 * XXX insert regardless of error to make in_purgeaddr below work.
679 * Need to improve.
680 */
681 if (newifaddr) {
682 ifaref(&ia->ia_ifa);
683 ifa_insert(ifp, &ia->ia_ifa);
684
685 mutex_enter(&in_ifaddr_lock);
686 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
687 IN_ADDRLIST_WRITER_INSERT_TAIL(ia);
688 in_addrhash_insert_locked(ia);
689 /* Release a reference that is held just after creation. */
690 ifafree(&ia->ia_ifa);
691 mutex_exit(&in_ifaddr_lock);
692 } else if (need_reinsert) {
693 in_addrhash_insert(ia);
694 }
695
696 if (error == 0) {
697 if (run_hook)
698 pfil_run_addrhooks(if_pfil, cmd, &ia->ia_ifa);
699 } else if (newifaddr) {
700 KASSERT(ia != NULL);
701 in_purgeaddr(&ia->ia_ifa);
702 ia = NULL;
703 }
704
705 out:
706 if (!newifaddr && ia != NULL)
707 ia4_release(ia, &psref);
708 curlwp_bindx(bound);
709 return error;
710 }
711
712 int
713 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
714 {
715 int error;
716
717 #ifndef NET_MPSAFE
718 mutex_enter(softnet_lock);
719 #endif
720 error = in_control0(so, cmd, data, ifp);
721 #ifndef NET_MPSAFE
722 mutex_exit(softnet_lock);
723 #endif
724
725 return error;
726 }
727
728 /* Add ownaddr as loopback rtentry. */
729 static void
730 in_ifaddlocal(struct ifaddr *ifa)
731 {
732 struct in_ifaddr *ia;
733
734 ia = (struct in_ifaddr *)ifa;
735 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY ||
736 (ia->ia_ifp->if_flags & IFF_POINTOPOINT &&
737 in_hosteq(ia->ia_dstaddr.sin_addr, ia->ia_addr.sin_addr)))
738 {
739 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
740 return;
741 }
742
743 rt_ifa_addlocal(ifa);
744 }
745
746 /* Remove loopback entry of ownaddr */
747 static void
748 in_ifremlocal(struct ifaddr *ifa)
749 {
750 struct in_ifaddr *ia, *p;
751 struct ifaddr *alt_ifa = NULL;
752 int ia_count = 0;
753 int s;
754 struct psref psref;
755 int bound = curlwp_bind();
756
757 ia = (struct in_ifaddr *)ifa;
758 /* Delete the entry if exactly one ifaddr matches the
759 * address, ifa->ifa_addr. */
760 s = pserialize_read_enter();
761 IN_ADDRLIST_READER_FOREACH(p) {
762 if (!in_hosteq(p->ia_addr.sin_addr, ia->ia_addr.sin_addr))
763 continue;
764 if (p->ia_ifp != ia->ia_ifp)
765 alt_ifa = &p->ia_ifa;
766 if (++ia_count > 1 && alt_ifa != NULL)
767 break;
768 }
769 if (alt_ifa != NULL && ia_count > 1)
770 ifa_acquire(alt_ifa, &psref);
771 pserialize_read_exit(s);
772
773 if (ia_count == 0)
774 goto out;
775
776 rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa);
777 if (alt_ifa != NULL && ia_count > 1)
778 ifa_release(alt_ifa, &psref);
779 out:
780 curlwp_bindx(bound);
781 }
782
783 static void
784 in_scrubaddr(struct in_ifaddr *ia)
785 {
786
787 /* stop DAD processing */
788 if (ia->ia_dad_stop != NULL)
789 ia->ia_dad_stop(&ia->ia_ifa);
790
791 in_scrubprefix(ia);
792 in_ifremlocal(&ia->ia_ifa);
793
794 mutex_enter(&in_ifaddr_lock);
795 if (ia->ia_allhosts != NULL) {
796 in_delmulti(ia->ia_allhosts);
797 ia->ia_allhosts = NULL;
798 }
799 mutex_exit(&in_ifaddr_lock);
800 }
801
802 /*
803 * Depends on it isn't called in concurrent. It should be guaranteed
804 * by ifa->ifa_ifp's ioctl lock. The possible callers are in_control
805 * and if_purgeaddrs; the former is called iva ifa->ifa_ifp's ioctl
806 * and the latter is called via ifa->ifa_ifp's if_detach. The functions
807 * never be executed in concurrent.
808 */
809 void
810 in_purgeaddr(struct ifaddr *ifa)
811 {
812 struct in_ifaddr *ia = (void *) ifa;
813 struct ifnet *ifp = ifa->ifa_ifp;
814
815 KASSERT(!ifa_held(ifa));
816
817 ifa->ifa_flags |= IFA_DESTROYING;
818 in_scrubaddr(ia);
819
820 mutex_enter(&in_ifaddr_lock);
821 in_addrhash_remove_locked(ia);
822 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
823 IN_ADDRLIST_WRITER_REMOVE(ia);
824 ifa_remove(ifp, &ia->ia_ifa);
825 #ifdef NET_MPSAFE
826 pserialize_perform(in_ifaddrhash_psz);
827 #endif
828 mutex_exit(&in_ifaddr_lock);
829 IN_ADDRHASH_ENTRY_DESTROY(ia);
830 IN_ADDRLIST_ENTRY_DESTROY(ia);
831 ifafree(&ia->ia_ifa);
832 in_setmaxmtu();
833 }
834
835 static void
836 in_addrhash_insert_locked(struct in_ifaddr *ia)
837 {
838
839 KASSERT(mutex_owned(&in_ifaddr_lock));
840
841 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia,
842 ia_hash);
843 IN_ADDRHASH_ENTRY_INIT(ia);
844 IN_ADDRHASH_WRITER_INSERT_HEAD(ia);
845 }
846
847 void
848 in_addrhash_insert(struct in_ifaddr *ia)
849 {
850
851 mutex_enter(&in_ifaddr_lock);
852 in_addrhash_insert_locked(ia);
853 mutex_exit(&in_ifaddr_lock);
854 }
855
856 static void
857 in_addrhash_remove_locked(struct in_ifaddr *ia)
858 {
859
860 KASSERT(mutex_owned(&in_ifaddr_lock));
861
862 LIST_REMOVE(ia, ia_hash);
863 IN_ADDRHASH_WRITER_REMOVE(ia);
864 }
865
866 void
867 in_addrhash_remove(struct in_ifaddr *ia)
868 {
869
870 mutex_enter(&in_ifaddr_lock);
871 in_addrhash_remove_locked(ia);
872 #ifdef NET_MPSAFE
873 pserialize_perform(in_ifaddrhash_psz);
874 #endif
875 mutex_exit(&in_ifaddr_lock);
876 IN_ADDRHASH_ENTRY_DESTROY(ia);
877 }
878
879 void
880 in_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */
881 {
882 if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
883 igmp_purgeif(ifp); /* manipulates pools */
884 #ifdef MROUTING
885 ip_mrouter_detach(ifp);
886 #endif
887 }
888
889 /*
890 * SIOC[GAD]LIFADDR.
891 * SIOCGLIFADDR: get first address. (???)
892 * SIOCGLIFADDR with IFLR_PREFIX:
893 * get first address that matches the specified prefix.
894 * SIOCALIFADDR: add the specified address.
895 * SIOCALIFADDR with IFLR_PREFIX:
896 * EINVAL since we can't deduce hostid part of the address.
897 * SIOCDLIFADDR: delete the specified address.
898 * SIOCDLIFADDR with IFLR_PREFIX:
899 * delete the first address that matches the specified prefix.
900 * return values:
901 * EINVAL on invalid parameters
902 * EADDRNOTAVAIL on prefix match failed/specified address not found
903 * other values may be returned from in_ioctl()
904 */
905 static int
906 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
907 struct ifnet *ifp)
908 {
909 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
910 struct ifaddr *ifa;
911 struct sockaddr *sa;
912
913 /* sanity checks */
914 if (data == NULL || ifp == NULL) {
915 panic("invalid argument to in_lifaddr_ioctl");
916 /*NOTRECHED*/
917 }
918
919 switch (cmd) {
920 case SIOCGLIFADDR:
921 /* address must be specified on GET with IFLR_PREFIX */
922 if ((iflr->flags & IFLR_PREFIX) == 0)
923 break;
924 /*FALLTHROUGH*/
925 case SIOCALIFADDR:
926 case SIOCDLIFADDR:
927 /* address must be specified on ADD and DELETE */
928 sa = (struct sockaddr *)&iflr->addr;
929 if (sa->sa_family != AF_INET)
930 return EINVAL;
931 if (sa->sa_len != sizeof(struct sockaddr_in))
932 return EINVAL;
933 /* XXX need improvement */
934 sa = (struct sockaddr *)&iflr->dstaddr;
935 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
936 return EINVAL;
937 if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
938 return EINVAL;
939 break;
940 default: /*shouldn't happen*/
941 #if 0
942 panic("invalid cmd to in_lifaddr_ioctl");
943 /*NOTREACHED*/
944 #else
945 return EOPNOTSUPP;
946 #endif
947 }
948 if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
949 return EINVAL;
950
951 switch (cmd) {
952 case SIOCALIFADDR:
953 {
954 struct in_aliasreq ifra;
955
956 if (iflr->flags & IFLR_PREFIX)
957 return EINVAL;
958
959 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
960 memset(&ifra, 0, sizeof(ifra));
961 memcpy(ifra.ifra_name, iflr->iflr_name,
962 sizeof(ifra.ifra_name));
963
964 memcpy(&ifra.ifra_addr, &iflr->addr,
965 ((struct sockaddr *)&iflr->addr)->sa_len);
966
967 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
968 memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
969 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
970 }
971
972 ifra.ifra_mask.sin_family = AF_INET;
973 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
974 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
975
976 return in_control(so, SIOCAIFADDR, &ifra, ifp);
977 }
978 case SIOCGLIFADDR:
979 case SIOCDLIFADDR:
980 {
981 struct in_ifaddr *ia;
982 struct in_addr mask, candidate, match;
983 struct sockaddr_in *sin;
984 int cmp, s;
985
986 memset(&mask, 0, sizeof(mask));
987 memset(&match, 0, sizeof(match)); /* XXX gcc */
988 if (iflr->flags & IFLR_PREFIX) {
989 /* lookup a prefix rather than address. */
990 in_len2mask(&mask, iflr->prefixlen);
991
992 sin = (struct sockaddr_in *)&iflr->addr;
993 match.s_addr = sin->sin_addr.s_addr;
994 match.s_addr &= mask.s_addr;
995
996 /* if you set extra bits, that's wrong */
997 if (match.s_addr != sin->sin_addr.s_addr)
998 return EINVAL;
999
1000 cmp = 1;
1001 } else {
1002 if (cmd == SIOCGLIFADDR) {
1003 /* on getting an address, take the 1st match */
1004 cmp = 0; /*XXX*/
1005 } else {
1006 /* on deleting an address, do exact match */
1007 in_len2mask(&mask, 32);
1008 sin = (struct sockaddr_in *)&iflr->addr;
1009 match.s_addr = sin->sin_addr.s_addr;
1010
1011 cmp = 1;
1012 }
1013 }
1014
1015 s = pserialize_read_enter();
1016 IFADDR_READER_FOREACH(ifa, ifp) {
1017 if (ifa->ifa_addr->sa_family != AF_INET)
1018 continue;
1019 if (cmp == 0)
1020 break;
1021 candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
1022 candidate.s_addr &= mask.s_addr;
1023 if (candidate.s_addr == match.s_addr)
1024 break;
1025 }
1026 if (ifa == NULL) {
1027 pserialize_read_exit(s);
1028 return EADDRNOTAVAIL;
1029 }
1030 ia = (struct in_ifaddr *)ifa;
1031
1032 if (cmd == SIOCGLIFADDR) {
1033 /* fill in the if_laddrreq structure */
1034 memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
1035
1036 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1037 memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
1038 ia->ia_dstaddr.sin_len);
1039 } else
1040 memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
1041
1042 iflr->prefixlen =
1043 in_mask2len(&ia->ia_sockmask.sin_addr);
1044
1045 iflr->flags = 0; /*XXX*/
1046 pserialize_read_exit(s);
1047
1048 return 0;
1049 } else {
1050 struct in_aliasreq ifra;
1051
1052 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
1053 memset(&ifra, 0, sizeof(ifra));
1054 memcpy(ifra.ifra_name, iflr->iflr_name,
1055 sizeof(ifra.ifra_name));
1056
1057 memcpy(&ifra.ifra_addr, &ia->ia_addr,
1058 ia->ia_addr.sin_len);
1059 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1060 memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
1061 ia->ia_dstaddr.sin_len);
1062 }
1063 memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
1064 ia->ia_sockmask.sin_len);
1065 pserialize_read_exit(s);
1066
1067 return in_control(so, SIOCDIFADDR, &ifra, ifp);
1068 }
1069 }
1070 }
1071
1072 return EOPNOTSUPP; /*just for safety*/
1073 }
1074
1075 /*
1076 * Initialize an interface's internet address
1077 * and routing table entry.
1078 */
1079 int
1080 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
1081 const struct sockaddr_in *sin, const struct sockaddr_in *dst, int scrub)
1082 {
1083 u_int32_t i;
1084 struct sockaddr_in oldaddr, olddst;
1085 int s, oldflags, flags = RTF_UP, error, hostIsNew;
1086
1087 if (sin == NULL)
1088 sin = &ia->ia_addr;
1089 if (dst == NULL)
1090 dst = &ia->ia_dstaddr;
1091
1092 /*
1093 * Set up new addresses.
1094 */
1095 oldaddr = ia->ia_addr;
1096 olddst = ia->ia_dstaddr;
1097 oldflags = ia->ia4_flags;
1098 ia->ia_addr = *sin;
1099 ia->ia_dstaddr = *dst;
1100 hostIsNew = oldaddr.sin_family != AF_INET ||
1101 !in_hosteq(ia->ia_addr.sin_addr, oldaddr.sin_addr);
1102 if (!scrub)
1103 scrub = oldaddr.sin_family != ia->ia_dstaddr.sin_family ||
1104 !in_hosteq(ia->ia_dstaddr.sin_addr, olddst.sin_addr);
1105
1106 /*
1107 * Configure address flags.
1108 * We need to do this early because they maybe adjusted
1109 * by if_addr_init depending on the address.
1110 */
1111 if (ia->ia4_flags & IN_IFF_DUPLICATED) {
1112 ia->ia4_flags &= ~IN_IFF_DUPLICATED;
1113 hostIsNew = 1;
1114 }
1115 if (ifp->if_link_state == LINK_STATE_DOWN) {
1116 ia->ia4_flags |= IN_IFF_DETACHED;
1117 ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1118 } else if (hostIsNew && if_do_dad(ifp))
1119 ia->ia4_flags |= IN_IFF_TRYTENTATIVE;
1120
1121 /*
1122 * Give the interface a chance to initialize
1123 * if this is its first address,
1124 * and to validate the address if necessary.
1125 */
1126 s = splsoftnet();
1127 error = if_addr_init(ifp, &ia->ia_ifa, true);
1128 splx(s);
1129 /* Now clear the try tentative flag, it's job is done. */
1130 ia->ia4_flags &= ~IN_IFF_TRYTENTATIVE;
1131 if (error != 0) {
1132 ia->ia_addr = oldaddr;
1133 ia->ia_dstaddr = olddst;
1134 ia->ia4_flags = oldflags;
1135 return error;
1136 }
1137
1138 if (scrub || hostIsNew) {
1139 int newflags = ia->ia4_flags;
1140
1141 ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
1142 ia->ia_ifa.ifa_dstaddr = sintosa(&olddst);
1143 ia->ia4_flags = oldflags;
1144 if (hostIsNew)
1145 in_scrubaddr(ia);
1146 else if (scrub)
1147 in_scrubprefix(ia);
1148 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
1149 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
1150 ia->ia4_flags = newflags;
1151 }
1152
1153 i = ia->ia_addr.sin_addr.s_addr;
1154 if (ifp->if_flags & IFF_POINTOPOINT)
1155 ia->ia_netmask = INADDR_BROADCAST; /* default to /32 */
1156 else if (IN_CLASSA(i))
1157 ia->ia_netmask = IN_CLASSA_NET;
1158 else if (IN_CLASSB(i))
1159 ia->ia_netmask = IN_CLASSB_NET;
1160 else
1161 ia->ia_netmask = IN_CLASSC_NET;
1162 /*
1163 * The subnet mask usually includes at least the standard network part,
1164 * but may may be smaller in the case of supernetting.
1165 * If it is set, we believe it.
1166 */
1167 if (ia->ia_subnetmask == 0) {
1168 ia->ia_subnetmask = ia->ia_netmask;
1169 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
1170 } else
1171 ia->ia_netmask &= ia->ia_subnetmask;
1172
1173 ia->ia_net = i & ia->ia_netmask;
1174 ia->ia_subnet = i & ia->ia_subnetmask;
1175 in_socktrim(&ia->ia_sockmask);
1176
1177 /* re-calculate the "in_maxmtu" value */
1178 in_setmaxmtu();
1179
1180 ia->ia_ifa.ifa_metric = ifp->if_metric;
1181 if (ifp->if_flags & IFF_BROADCAST) {
1182 ia->ia_broadaddr.sin_addr.s_addr =
1183 ia->ia_subnet | ~ia->ia_subnetmask;
1184 ia->ia_netbroadcast.s_addr =
1185 ia->ia_net | ~ia->ia_netmask;
1186 } else if (ifp->if_flags & IFF_LOOPBACK) {
1187 ia->ia_dstaddr = ia->ia_addr;
1188 flags |= RTF_HOST;
1189 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1190 if (ia->ia_dstaddr.sin_family != AF_INET)
1191 return (0);
1192 flags |= RTF_HOST;
1193 }
1194
1195 /* Add the local route to the address */
1196 in_ifaddlocal(&ia->ia_ifa);
1197
1198 /* Add the prefix route for the address */
1199 error = in_addprefix(ia, flags);
1200
1201 /*
1202 * If the interface supports multicast, join the "all hosts"
1203 * multicast group on that interface.
1204 */
1205 mutex_enter(&in_ifaddr_lock);
1206 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
1207 struct in_addr addr;
1208
1209 addr.s_addr = INADDR_ALLHOSTS_GROUP;
1210 ia->ia_allhosts = in_addmulti(&addr, ifp);
1211 }
1212 mutex_exit(&in_ifaddr_lock);
1213
1214 if (hostIsNew &&
1215 ia->ia4_flags & IN_IFF_TENTATIVE &&
1216 if_do_dad(ifp))
1217 ia->ia_dad_start((struct ifaddr *)ia);
1218
1219 return error;
1220 }
1221
1222 #define rtinitflags(x) \
1223 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
1224 ? RTF_HOST : 0)
1225
1226 /*
1227 * add a route to prefix ("connected route" in cisco terminology).
1228 * does nothing if there's some interface address with the same prefix already.
1229 */
1230 static int
1231 in_addprefix(struct in_ifaddr *target, int flags)
1232 {
1233 struct in_ifaddr *ia;
1234 struct in_addr prefix, mask, p;
1235 int error;
1236 int s;
1237
1238 if ((flags & RTF_HOST) != 0)
1239 prefix = target->ia_dstaddr.sin_addr;
1240 else {
1241 prefix = target->ia_addr.sin_addr;
1242 mask = target->ia_sockmask.sin_addr;
1243 prefix.s_addr &= mask.s_addr;
1244 }
1245
1246 s = pserialize_read_enter();
1247 IN_ADDRLIST_READER_FOREACH(ia) {
1248 if (rtinitflags(ia))
1249 p = ia->ia_dstaddr.sin_addr;
1250 else {
1251 p = ia->ia_addr.sin_addr;
1252 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1253 }
1254
1255 if (prefix.s_addr != p.s_addr)
1256 continue;
1257
1258 /*
1259 * if we got a matching prefix route inserted by other
1260 * interface address, we don't need to bother
1261 *
1262 * XXX RADIX_MPATH implications here? -dyoung
1263 */
1264 if (ia->ia_flags & IFA_ROUTE) {
1265 pserialize_read_exit(s);
1266 return 0;
1267 }
1268 }
1269 pserialize_read_exit(s);
1270
1271 /*
1272 * noone seem to have prefix route. insert it.
1273 */
1274 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1275 if (error == 0)
1276 target->ia_flags |= IFA_ROUTE;
1277 else if (error == EEXIST) {
1278 /*
1279 * the fact the route already exists is not an error.
1280 */
1281 error = 0;
1282 }
1283 return error;
1284 }
1285
1286 /*
1287 * remove a route to prefix ("connected route" in cisco terminology).
1288 * re-installs the route by using another interface address, if there's one
1289 * with the same prefix (otherwise we lose the route mistakenly).
1290 */
1291 static int
1292 in_scrubprefix(struct in_ifaddr *target)
1293 {
1294 struct in_ifaddr *ia;
1295 struct in_addr prefix, mask, p;
1296 int error;
1297 int s;
1298
1299 /* If we don't have IFA_ROUTE we have nothing to do */
1300 if ((target->ia_flags & IFA_ROUTE) == 0)
1301 return 0;
1302
1303 if (rtinitflags(target))
1304 prefix = target->ia_dstaddr.sin_addr;
1305 else {
1306 prefix = target->ia_addr.sin_addr;
1307 mask = target->ia_sockmask.sin_addr;
1308 prefix.s_addr &= mask.s_addr;
1309 }
1310
1311 s = pserialize_read_enter();
1312 IN_ADDRLIST_READER_FOREACH(ia) {
1313 if (rtinitflags(ia))
1314 p = ia->ia_dstaddr.sin_addr;
1315 else {
1316 p = ia->ia_addr.sin_addr;
1317 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1318 }
1319
1320 if (prefix.s_addr != p.s_addr)
1321 continue;
1322
1323 /*
1324 * if we got a matching prefix route, move IFA_ROUTE to him
1325 */
1326 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1327 struct psref psref;
1328 int bound = curlwp_bind();
1329
1330 ia4_acquire(ia, &psref);
1331 pserialize_read_exit(s);
1332
1333 rtinit(&target->ia_ifa, RTM_DELETE,
1334 rtinitflags(target));
1335 target->ia_flags &= ~IFA_ROUTE;
1336
1337 error = rtinit(&ia->ia_ifa, RTM_ADD,
1338 rtinitflags(ia) | RTF_UP);
1339 if (error == 0)
1340 ia->ia_flags |= IFA_ROUTE;
1341
1342 ia4_release(ia, &psref);
1343 curlwp_bindx(bound);
1344
1345 return error;
1346 }
1347 }
1348 pserialize_read_exit(s);
1349
1350 /*
1351 * noone seem to have prefix route. remove it.
1352 */
1353 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1354 target->ia_flags &= ~IFA_ROUTE;
1355 return 0;
1356 }
1357
1358 #undef rtinitflags
1359
1360 /*
1361 * Return 1 if the address might be a local broadcast address.
1362 */
1363 int
1364 in_broadcast(struct in_addr in, struct ifnet *ifp)
1365 {
1366 struct ifaddr *ifa;
1367 int s;
1368
1369 KASSERT(ifp != NULL);
1370
1371 if (in.s_addr == INADDR_BROADCAST ||
1372 in_nullhost(in))
1373 return 1;
1374 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1375 return 0;
1376 /*
1377 * Look through the list of addresses for a match
1378 * with a broadcast address.
1379 */
1380 #define ia (ifatoia(ifa))
1381 s = pserialize_read_enter();
1382 IFADDR_READER_FOREACH(ifa, ifp) {
1383 if (ifa->ifa_addr->sa_family == AF_INET &&
1384 !in_hosteq(in, ia->ia_addr.sin_addr) &&
1385 (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1386 in_hosteq(in, ia->ia_netbroadcast) ||
1387 (hostzeroisbroadcast &&
1388 /*
1389 * Check for old-style (host 0) broadcast.
1390 */
1391 (in.s_addr == ia->ia_subnet ||
1392 in.s_addr == ia->ia_net)))) {
1393 pserialize_read_exit(s);
1394 return 1;
1395 }
1396 }
1397 pserialize_read_exit(s);
1398 return (0);
1399 #undef ia
1400 }
1401
1402 /*
1403 * perform DAD when interface becomes IFF_UP.
1404 */
1405 void
1406 in_if_link_up(struct ifnet *ifp)
1407 {
1408 struct ifaddr *ifa;
1409 struct in_ifaddr *ia;
1410 int s, bound;
1411
1412 /* Ensure it's sane to run DAD */
1413 if (ifp->if_link_state == LINK_STATE_DOWN)
1414 return;
1415 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
1416 return;
1417
1418 bound = curlwp_bind();
1419 s = pserialize_read_enter();
1420 IFADDR_READER_FOREACH(ifa, ifp) {
1421 struct psref psref;
1422
1423 if (ifa->ifa_addr->sa_family != AF_INET)
1424 continue;
1425 ifa_acquire(ifa, &psref);
1426 pserialize_read_exit(s);
1427
1428 ia = (struct in_ifaddr *)ifa;
1429
1430 /* If detached then mark as tentative */
1431 if (ia->ia4_flags & IN_IFF_DETACHED) {
1432 ia->ia4_flags &= ~IN_IFF_DETACHED;
1433 if (if_do_dad(ifp) && ia->ia_dad_start != NULL)
1434 ia->ia4_flags |= IN_IFF_TENTATIVE;
1435 else if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0)
1436 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1437 }
1438
1439 if (ia->ia4_flags & IN_IFF_TENTATIVE) {
1440 /* Clear the duplicated flag as we're starting DAD. */
1441 ia->ia4_flags &= ~IN_IFF_DUPLICATED;
1442 ia->ia_dad_start(ifa);
1443 }
1444
1445 s = pserialize_read_enter();
1446 ifa_release(ifa, &psref);
1447 }
1448 pserialize_read_exit(s);
1449 curlwp_bindx(bound);
1450 }
1451
1452 void
1453 in_if_up(struct ifnet *ifp)
1454 {
1455
1456 /* interface may not support link state, so bring it up also */
1457 in_if_link_up(ifp);
1458 }
1459
1460 /*
1461 * Mark all addresses as detached.
1462 */
1463 void
1464 in_if_link_down(struct ifnet *ifp)
1465 {
1466 struct ifaddr *ifa;
1467 struct in_ifaddr *ia;
1468 int s, bound;
1469
1470 bound = curlwp_bind();
1471 s = pserialize_read_enter();
1472 IFADDR_READER_FOREACH(ifa, ifp) {
1473 struct psref psref;
1474
1475 if (ifa->ifa_addr->sa_family != AF_INET)
1476 continue;
1477 ifa_acquire(ifa, &psref);
1478 pserialize_read_exit(s);
1479
1480 ia = (struct in_ifaddr *)ifa;
1481
1482 /* Stop DAD processing */
1483 if (ia->ia_dad_stop != NULL)
1484 ia->ia_dad_stop(ifa);
1485
1486 /*
1487 * Mark the address as detached.
1488 */
1489 if (!(ia->ia4_flags & IN_IFF_DETACHED)) {
1490 ia->ia4_flags |= IN_IFF_DETACHED;
1491 ia->ia4_flags &=
1492 ~(IN_IFF_TENTATIVE | IN_IFF_DUPLICATED);
1493 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1494 }
1495
1496 s = pserialize_read_enter();
1497 ifa_release(ifa, &psref);
1498 }
1499 pserialize_read_exit(s);
1500 curlwp_bindx(bound);
1501 }
1502
1503 void
1504 in_if_down(struct ifnet *ifp)
1505 {
1506
1507 in_if_link_down(ifp);
1508 }
1509
1510 void
1511 in_if_link_state_change(struct ifnet *ifp, int link_state)
1512 {
1513
1514 switch (link_state) {
1515 case LINK_STATE_DOWN:
1516 in_if_link_down(ifp);
1517 break;
1518 case LINK_STATE_UP:
1519 in_if_link_up(ifp);
1520 break;
1521 }
1522 }
1523
1524 /*
1525 * in_lookup_multi: look up the in_multi record for a given IP
1526 * multicast address on a given interface. If no matching record is
1527 * found, return NULL.
1528 */
1529 struct in_multi *
1530 in_lookup_multi(struct in_addr addr, ifnet_t *ifp)
1531 {
1532 struct in_multi *inm;
1533
1534 KASSERT(rw_lock_held(&in_multilock));
1535
1536 LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) {
1537 if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp)
1538 break;
1539 }
1540 return inm;
1541 }
1542
1543 /*
1544 * in_multi_group: check whether the address belongs to an IP multicast
1545 * group we are joined on this interface. Returns true or false.
1546 */
1547 bool
1548 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags)
1549 {
1550 bool ingroup;
1551
1552 if (__predict_true(flags & IP_IGMP_MCAST) == 0) {
1553 rw_enter(&in_multilock, RW_READER);
1554 ingroup = in_lookup_multi(addr, ifp) != NULL;
1555 rw_exit(&in_multilock);
1556 } else {
1557 /* XXX Recursive call from ip_output(). */
1558 KASSERT(rw_lock_held(&in_multilock));
1559 ingroup = in_lookup_multi(addr, ifp) != NULL;
1560 }
1561 return ingroup;
1562 }
1563
1564 /*
1565 * Add an address to the list of IP multicast addresses for a given interface.
1566 */
1567 struct in_multi *
1568 in_addmulti(struct in_addr *ap, ifnet_t *ifp)
1569 {
1570 struct sockaddr_in sin;
1571 struct in_multi *inm;
1572
1573 /*
1574 * See if address already in list.
1575 */
1576 rw_enter(&in_multilock, RW_WRITER);
1577 inm = in_lookup_multi(*ap, ifp);
1578 if (inm != NULL) {
1579 /*
1580 * Found it; just increment the reference count.
1581 */
1582 inm->inm_refcount++;
1583 rw_exit(&in_multilock);
1584 return inm;
1585 }
1586
1587 /*
1588 * New address; allocate a new multicast record.
1589 */
1590 inm = pool_get(&inmulti_pool, PR_NOWAIT);
1591 if (inm == NULL) {
1592 rw_exit(&in_multilock);
1593 return NULL;
1594 }
1595 inm->inm_addr = *ap;
1596 inm->inm_ifp = ifp;
1597 inm->inm_refcount = 1;
1598
1599 /*
1600 * Ask the network driver to update its multicast reception
1601 * filter appropriately for the new address.
1602 */
1603 sockaddr_in_init(&sin, ap, 0);
1604 if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
1605 rw_exit(&in_multilock);
1606 pool_put(&inmulti_pool, inm);
1607 return NULL;
1608 }
1609
1610 /*
1611 * Let IGMP know that we have joined a new IP multicast group.
1612 */
1613 if (igmp_joingroup(inm) != 0) {
1614 rw_exit(&in_multilock);
1615 pool_put(&inmulti_pool, inm);
1616 return NULL;
1617 }
1618 LIST_INSERT_HEAD(
1619 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1620 inm, inm_list);
1621 in_multientries++;
1622 rw_exit(&in_multilock);
1623
1624 return inm;
1625 }
1626
1627 /*
1628 * Delete a multicast address record.
1629 */
1630 void
1631 in_delmulti(struct in_multi *inm)
1632 {
1633 struct sockaddr_in sin;
1634
1635 rw_enter(&in_multilock, RW_WRITER);
1636 if (--inm->inm_refcount > 0) {
1637 rw_exit(&in_multilock);
1638 return;
1639 }
1640
1641 /*
1642 * No remaining claims to this record; let IGMP know that
1643 * we are leaving the multicast group.
1644 */
1645 igmp_leavegroup(inm);
1646
1647 /*
1648 * Notify the network driver to update its multicast reception
1649 * filter.
1650 */
1651 sockaddr_in_init(&sin, &inm->inm_addr, 0);
1652 if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
1653
1654 /*
1655 * Unlink from list.
1656 */
1657 LIST_REMOVE(inm, inm_list);
1658 in_multientries--;
1659 rw_exit(&in_multilock);
1660
1661 pool_put(&inmulti_pool, inm);
1662 }
1663
1664 /*
1665 * in_next_multi: step through all of the in_multi records, one at a time.
1666 * The current position is remembered in "step", which the caller must
1667 * provide. in_first_multi(), below, must be called to initialize "step"
1668 * and get the first record. Both macros return a NULL "inm" when there
1669 * are no remaining records.
1670 */
1671 struct in_multi *
1672 in_next_multi(struct in_multistep *step)
1673 {
1674 struct in_multi *inm;
1675
1676 KASSERT(rw_lock_held(&in_multilock));
1677
1678 while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) {
1679 step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]);
1680 }
1681 if ((inm = step->i_inm) != NULL) {
1682 step->i_inm = LIST_NEXT(inm, inm_list);
1683 }
1684 return inm;
1685 }
1686
1687 struct in_multi *
1688 in_first_multi(struct in_multistep *step)
1689 {
1690 KASSERT(rw_lock_held(&in_multilock));
1691
1692 step->i_n = 0;
1693 step->i_inm = LIST_FIRST(&in_multihashtbl[0]);
1694 return in_next_multi(step);
1695 }
1696
1697 void
1698 in_multi_lock(int op)
1699 {
1700 rw_enter(&in_multilock, op);
1701 }
1702
1703 void
1704 in_multi_unlock(void)
1705 {
1706 rw_exit(&in_multilock);
1707 }
1708
1709 int
1710 in_multi_lock_held(void)
1711 {
1712 return rw_lock_held(&in_multilock);
1713 }
1714
1715 struct in_ifaddr *
1716 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
1717 int soopts, struct ip_moptions *mopts, int *errorp, struct psref *psref)
1718 {
1719 struct rtentry *rt = NULL;
1720 struct in_ifaddr *ia = NULL;
1721
1722 KASSERT(ISSET(curlwp->l_pflag, LP_BOUND));
1723 /*
1724 * If route is known or can be allocated now, take the
1725 * source address from the interface. Otherwise, punt.
1726 */
1727 if ((soopts & SO_DONTROUTE) != 0)
1728 rtcache_free(ro);
1729 else {
1730 union {
1731 struct sockaddr dst;
1732 struct sockaddr_in dst4;
1733 } u;
1734
1735 sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
1736 rt = rtcache_lookup(ro, &u.dst);
1737 }
1738 /*
1739 * If we found a route, use the address
1740 * corresponding to the outgoing interface
1741 * unless it is the loopback (in case a route
1742 * to our address on another net goes to loopback).
1743 *
1744 * XXX Is this still true? Do we care?
1745 */
1746 if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
1747 int s;
1748 struct ifaddr *ifa;
1749 /*
1750 * Just in case. May not need to do this workaround.
1751 * Revisit when working on rtentry MP-ification.
1752 */
1753 s = pserialize_read_enter();
1754 IFADDR_READER_FOREACH(ifa, rt->rt_ifp) {
1755 if (ifa == rt->rt_ifa)
1756 break;
1757 }
1758 if (ifa != NULL)
1759 ifa_acquire(ifa, psref);
1760 pserialize_read_exit(s);
1761
1762 ia = ifatoia(ifa);
1763 }
1764 if (ia == NULL) {
1765 u_int16_t fport = sin->sin_port;
1766 struct ifaddr *ifa;
1767 int s;
1768
1769 sin->sin_port = 0;
1770 ifa = ifa_ifwithladdr_psref(sintosa(sin), psref);
1771 sin->sin_port = fport;
1772 if (ifa == NULL) {
1773 /* Find 1st non-loopback AF_INET address */
1774 s = pserialize_read_enter();
1775 IN_ADDRLIST_READER_FOREACH(ia) {
1776 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
1777 break;
1778 }
1779 if (ia != NULL)
1780 ia4_acquire(ia, psref);
1781 pserialize_read_exit(s);
1782 } else {
1783 /* ia is already referenced by psref */
1784 ia = ifatoia(ifa);
1785 }
1786 if (ia == NULL) {
1787 *errorp = EADDRNOTAVAIL;
1788 goto out;
1789 }
1790 }
1791 /*
1792 * If the destination address is multicast and an outgoing
1793 * interface has been set as a multicast option, use the
1794 * address of that interface as our source address.
1795 */
1796 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
1797 struct ip_moptions *imo;
1798
1799 imo = mopts;
1800 if (imo->imo_multicast_if_index != 0) {
1801 struct ifnet *ifp;
1802 int s;
1803
1804 if (ia != NULL)
1805 ia4_release(ia, psref);
1806 s = pserialize_read_enter();
1807 ifp = if_byindex(imo->imo_multicast_if_index);
1808 if (ifp != NULL) {
1809 /* XXX */
1810 ia = in_get_ia_from_ifp_psref(ifp, psref);
1811 } else
1812 ia = NULL;
1813 if (ia == NULL || ia->ia4_flags & IN_IFF_NOTREADY) {
1814 pserialize_read_exit(s);
1815 if (ia != NULL)
1816 ia4_release(ia, psref);
1817 *errorp = EADDRNOTAVAIL;
1818 ia = NULL;
1819 goto out;
1820 }
1821 pserialize_read_exit(s);
1822 }
1823 }
1824 if (ia->ia_ifa.ifa_getifa != NULL) {
1825 ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
1826 sintosa(sin)));
1827 if (ia == NULL) {
1828 *errorp = EADDRNOTAVAIL;
1829 goto out;
1830 }
1831 /* FIXME NOMPSAFE */
1832 ia4_acquire(ia, psref);
1833 }
1834 #ifdef GETIFA_DEBUG
1835 else
1836 printf("%s: missing ifa_getifa\n", __func__);
1837 #endif
1838 out:
1839 rtcache_unref(rt, ro);
1840 return ia;
1841 }
1842
1843 #if NARP > 0
1844
1845 struct in_llentry {
1846 struct llentry base;
1847 };
1848
1849 #define IN_LLTBL_DEFAULT_HSIZE 32
1850 #define IN_LLTBL_HASH(k, h) \
1851 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1852
1853 /*
1854 * Do actual deallocation of @lle.
1855 * Called by LLE_FREE_LOCKED when number of references
1856 * drops to zero.
1857 */
1858 static void
1859 in_lltable_destroy_lle(struct llentry *lle)
1860 {
1861
1862 LLE_WUNLOCK(lle);
1863 LLE_LOCK_DESTROY(lle);
1864 kmem_intr_free(lle, sizeof(*lle));
1865 }
1866
1867 static struct llentry *
1868 in_lltable_new(struct in_addr addr4, u_int flags)
1869 {
1870 struct in_llentry *lle;
1871
1872 lle = kmem_intr_zalloc(sizeof(*lle), KM_NOSLEEP);
1873 if (lle == NULL) /* NB: caller generates msg */
1874 return NULL;
1875
1876 /*
1877 * For IPv4 this will trigger "arpresolve" to generate
1878 * an ARP request.
1879 */
1880 lle->base.la_expire = time_uptime; /* mark expired */
1881 lle->base.r_l3addr.addr4 = addr4;
1882 lle->base.lle_refcnt = 1;
1883 lle->base.lle_free = in_lltable_destroy_lle;
1884 LLE_LOCK_INIT(&lle->base);
1885 callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
1886
1887 return (&lle->base);
1888 }
1889
1890 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1891 (((ntohl((d).s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1892
1893 static int
1894 in_lltable_match_prefix(const struct sockaddr *prefix,
1895 const struct sockaddr *mask, u_int flags, struct llentry *lle)
1896 {
1897 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1898 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1899 struct in_addr lle_addr;
1900
1901 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1902
1903 /*
1904 * (flags & LLE_STATIC) means deleting all entries
1905 * including static ARP entries.
1906 */
1907 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, pfx, msk) &&
1908 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)))
1909 return (1);
1910
1911 return (0);
1912 }
1913
1914 static void
1915 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1916 {
1917 struct ifnet *ifp __diagused;
1918 size_t pkts_dropped;
1919
1920 LLE_WLOCK_ASSERT(lle);
1921 KASSERT(llt != NULL);
1922
1923 /* Unlink entry from table if not already */
1924 if ((lle->la_flags & LLE_LINKED) != 0) {
1925 ifp = llt->llt_ifp;
1926 IF_AFDATA_WLOCK_ASSERT(ifp);
1927 lltable_unlink_entry(llt, lle);
1928 }
1929
1930 /* cancel timer */
1931 if (callout_halt(&lle->lle_timer, &lle->lle_lock))
1932 LLE_REMREF(lle);
1933
1934 /* Drop hold queue */
1935 pkts_dropped = llentry_free(lle);
1936 arp_stat_add(ARP_STAT_DFRDROPPED, (uint64_t)pkts_dropped);
1937 }
1938
1939 static int
1940 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1941 {
1942 struct rtentry *rt;
1943 int error = EINVAL;
1944
1945 KASSERTMSG(l3addr->sa_family == AF_INET,
1946 "sin_family %d", l3addr->sa_family);
1947
1948 rt = rtalloc1(l3addr, 0);
1949 if (rt == NULL)
1950 return error;
1951
1952 /*
1953 * If the gateway for an existing host route matches the target L3
1954 * address, which is a special route inserted by some implementation
1955 * such as MANET, and the interface is of the correct type, then
1956 * allow for ARP to proceed.
1957 */
1958 if (rt->rt_flags & RTF_GATEWAY) {
1959 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
1960 rt->rt_ifp->if_type != IFT_ETHER ||
1961 #ifdef __FreeBSD__
1962 (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1963 #else
1964 (rt->rt_ifp->if_flags & IFF_NOARP) != 0 ||
1965 #endif
1966 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
1967 sizeof(in_addr_t)) != 0) {
1968 goto error;
1969 }
1970 }
1971
1972 /*
1973 * Make sure that at least the destination address is covered
1974 * by the route. This is for handling the case where 2 or more
1975 * interfaces have the same prefix. An incoming packet arrives
1976 * on one interface and the corresponding outgoing packet leaves
1977 * another interface.
1978 */
1979 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
1980 const char *sa, *mask, *addr, *lim;
1981 int len;
1982
1983 mask = (const char *)rt_mask(rt);
1984 /*
1985 * Just being extra cautious to avoid some custom
1986 * code getting into trouble.
1987 */
1988 if (mask == NULL)
1989 goto error;
1990
1991 sa = (const char *)rt_getkey(rt);
1992 addr = (const char *)l3addr;
1993 len = ((const struct sockaddr_in *)l3addr)->sin_len;
1994 lim = addr + len;
1995
1996 for ( ; addr < lim; sa++, mask++, addr++) {
1997 if ((*sa ^ *addr) & *mask) {
1998 #ifdef DIAGNOSTIC
1999 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
2000 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
2001 #endif
2002 goto error;
2003 }
2004 }
2005 }
2006
2007 error = 0;
2008 error:
2009 rt_unref(rt);
2010 return error;
2011 }
2012
2013 static inline uint32_t
2014 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
2015 {
2016
2017 return (IN_LLTBL_HASH(dst.s_addr, hsize));
2018 }
2019
2020 static uint32_t
2021 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
2022 {
2023
2024 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
2025 }
2026
2027 static void
2028 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2029 {
2030 struct sockaddr_in *sin;
2031
2032 sin = (struct sockaddr_in *)sa;
2033 memset(sin, 0, sizeof(*sin));
2034 sin->sin_family = AF_INET;
2035 sin->sin_len = sizeof(*sin);
2036 sin->sin_addr = lle->r_l3addr.addr4;
2037 }
2038
2039 static inline struct llentry *
2040 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
2041 {
2042 struct llentry *lle;
2043 struct llentries *lleh;
2044 u_int hashidx;
2045
2046 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
2047 lleh = &llt->lle_head[hashidx];
2048 LIST_FOREACH(lle, lleh, lle_next) {
2049 if (lle->la_flags & LLE_DELETED)
2050 continue;
2051 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
2052 break;
2053 }
2054
2055 return (lle);
2056 }
2057
2058 static int
2059 in_lltable_delete(struct lltable *llt, u_int flags,
2060 const struct sockaddr *l3addr)
2061 {
2062 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
2063 struct ifnet *ifp __diagused = llt->llt_ifp;
2064 struct llentry *lle;
2065
2066 IF_AFDATA_WLOCK_ASSERT(ifp);
2067 KASSERTMSG(l3addr->sa_family == AF_INET,
2068 "sin_family %d", l3addr->sa_family);
2069
2070 lle = in_lltable_find_dst(llt, sin->sin_addr);
2071 if (lle == NULL) {
2072 #ifdef DEBUG
2073 char buf[64];
2074 sockaddr_format(l3addr, buf, sizeof(buf));
2075 log(LOG_INFO, "%s: cache for %s is not found\n",
2076 __func__, buf);
2077 #endif
2078 return (ENOENT);
2079 }
2080
2081 LLE_WLOCK(lle);
2082 lle->la_flags |= LLE_DELETED;
2083 #ifdef DEBUG
2084 {
2085 char buf[64];
2086 sockaddr_format(l3addr, buf, sizeof(buf));
2087 log(LOG_INFO, "%s: cache for %s (%p) is deleted\n",
2088 __func__, buf, lle);
2089 }
2090 #endif
2091 if ((lle->la_flags & (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
2092 llentry_free(lle);
2093 else
2094 LLE_WUNLOCK(lle);
2095
2096 return (0);
2097 }
2098
2099 static struct llentry *
2100 in_lltable_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
2101 {
2102 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
2103 struct ifnet *ifp = llt->llt_ifp;
2104 struct llentry *lle;
2105
2106 IF_AFDATA_WLOCK_ASSERT(ifp);
2107 KASSERTMSG(l3addr->sa_family == AF_INET,
2108 "sin_family %d", l3addr->sa_family);
2109
2110 lle = in_lltable_find_dst(llt, sin->sin_addr);
2111
2112 if (lle != NULL) {
2113 LLE_WLOCK(lle);
2114 return (lle);
2115 }
2116
2117 /* no existing record, we need to create new one */
2118
2119 /*
2120 * A route that covers the given address must have
2121 * been installed 1st because we are doing a resolution,
2122 * verify this.
2123 */
2124 if (!(flags & LLE_IFADDR) &&
2125 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
2126 return (NULL);
2127
2128 lle = in_lltable_new(sin->sin_addr, flags);
2129 if (lle == NULL) {
2130 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2131 return (NULL);
2132 }
2133 lle->la_flags = flags;
2134 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2135 memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
2136 lle->la_flags |= (LLE_VALID | LLE_STATIC);
2137 }
2138
2139 lltable_link_entry(llt, lle);
2140 LLE_WLOCK(lle);
2141
2142 return (lle);
2143 }
2144
2145 /*
2146 * Return NULL if not found or marked for deletion.
2147 * If found return lle read locked.
2148 */
2149 static struct llentry *
2150 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
2151 {
2152 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
2153 struct llentry *lle;
2154
2155 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
2156 KASSERTMSG(l3addr->sa_family == AF_INET,
2157 "sin_family %d", l3addr->sa_family);
2158
2159 lle = in_lltable_find_dst(llt, sin->sin_addr);
2160
2161 if (lle == NULL)
2162 return NULL;
2163
2164 if (flags & LLE_EXCLUSIVE)
2165 LLE_WLOCK(lle);
2166 else
2167 LLE_RLOCK(lle);
2168
2169 return lle;
2170 }
2171
2172 static int
2173 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2174 struct rt_walkarg *w)
2175 {
2176 struct sockaddr_in sin;
2177
2178 LLTABLE_LOCK_ASSERT();
2179
2180 /* skip deleted entries */
2181 if (lle->la_flags & LLE_DELETED)
2182 return 0;
2183
2184 sockaddr_in_init(&sin, &lle->r_l3addr.addr4, 0);
2185
2186 return lltable_dump_entry(llt, lle, w, sintosa(&sin));
2187 }
2188
2189 #endif /* NARP > 0 */
2190
2191 static int
2192 in_multicast_sysctl(SYSCTLFN_ARGS)
2193 {
2194 struct ifnet *ifp;
2195 struct ifaddr *ifa;
2196 struct in_ifaddr *ifa4;
2197 struct in_multi *inm;
2198 uint32_t tmp;
2199 int error;
2200 size_t written;
2201 struct psref psref;
2202 int bound;
2203
2204 if (namelen != 1)
2205 return EINVAL;
2206
2207 bound = curlwp_bind();
2208 ifp = if_get_byindex(name[0], &psref);
2209 if (ifp == NULL) {
2210 curlwp_bindx(bound);
2211 return ENODEV;
2212 }
2213
2214 if (oldp == NULL) {
2215 *oldlenp = 0;
2216 IFADDR_FOREACH(ifa, ifp) {
2217 if (ifa->ifa_addr->sa_family != AF_INET)
2218 continue;
2219 ifa4 = (void *)ifa;
2220 LIST_FOREACH(inm, &ifa4->ia_multiaddrs, inm_list) {
2221 *oldlenp += 2 * sizeof(struct in_addr) +
2222 sizeof(uint32_t);
2223 }
2224 }
2225 if_put(ifp, &psref);
2226 curlwp_bindx(bound);
2227 return 0;
2228 }
2229
2230 error = 0;
2231 written = 0;
2232 IFADDR_FOREACH(ifa, ifp) {
2233 if (ifa->ifa_addr->sa_family != AF_INET)
2234 continue;
2235 ifa4 = (void *)ifa;
2236 LIST_FOREACH(inm, &ifa4->ia_multiaddrs, inm_list) {
2237 if (written + 2 * sizeof(struct in_addr) +
2238 sizeof(uint32_t) > *oldlenp)
2239 goto done;
2240 error = sysctl_copyout(l, &ifa4->ia_addr.sin_addr,
2241 oldp, sizeof(struct in_addr));
2242 if (error)
2243 goto done;
2244 oldp = (char *)oldp + sizeof(struct in_addr);
2245 written += sizeof(struct in_addr);
2246 error = sysctl_copyout(l, &inm->inm_addr,
2247 oldp, sizeof(struct in_addr));
2248 if (error)
2249 goto done;
2250 oldp = (char *)oldp + sizeof(struct in_addr);
2251 written += sizeof(struct in_addr);
2252 tmp = inm->inm_refcount;
2253 error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
2254 if (error)
2255 goto done;
2256 oldp = (char *)oldp + sizeof(tmp);
2257 written += sizeof(tmp);
2258 }
2259 }
2260 done:
2261 if_put(ifp, &psref);
2262 curlwp_bindx(bound);
2263 *oldlenp = written;
2264 return error;
2265 }
2266
2267 static void
2268 in_sysctl_init(struct sysctllog **clog)
2269 {
2270 sysctl_createv(clog, 0, NULL, NULL,
2271 CTLFLAG_PERMANENT,
2272 CTLTYPE_NODE, "inet",
2273 SYSCTL_DESCR("PF_INET related settings"),
2274 NULL, 0, NULL, 0,
2275 CTL_NET, PF_INET, CTL_EOL);
2276 sysctl_createv(clog, 0, NULL, NULL,
2277 CTLFLAG_PERMANENT,
2278 CTLTYPE_NODE, "multicast",
2279 SYSCTL_DESCR("Multicast information"),
2280 in_multicast_sysctl, 0, NULL, 0,
2281 CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
2282 sysctl_createv(clog, 0, NULL, NULL,
2283 CTLFLAG_PERMANENT,
2284 CTLTYPE_NODE, "ip",
2285 SYSCTL_DESCR("IPv4 related settings"),
2286 NULL, 0, NULL, 0,
2287 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
2288
2289 sysctl_createv(clog, 0, NULL, NULL,
2290 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2291 CTLTYPE_INT, "subnetsarelocal",
2292 SYSCTL_DESCR("Whether logical subnets are considered "
2293 "local"),
2294 NULL, 0, &subnetsarelocal, 0,
2295 CTL_NET, PF_INET, IPPROTO_IP,
2296 IPCTL_SUBNETSARELOCAL, CTL_EOL);
2297 sysctl_createv(clog, 0, NULL, NULL,
2298 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2299 CTLTYPE_INT, "hostzerobroadcast",
2300 SYSCTL_DESCR("All zeroes address is broadcast address"),
2301 NULL, 0, &hostzeroisbroadcast, 0,
2302 CTL_NET, PF_INET, IPPROTO_IP,
2303 IPCTL_HOSTZEROBROADCAST, CTL_EOL);
2304 }
2305
2306 #if NARP > 0
2307
2308 static struct lltable *
2309 in_lltattach(struct ifnet *ifp)
2310 {
2311 struct lltable *llt;
2312
2313 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
2314 llt->llt_af = AF_INET;
2315 llt->llt_ifp = ifp;
2316
2317 llt->llt_lookup = in_lltable_lookup;
2318 llt->llt_create = in_lltable_create;
2319 llt->llt_delete = in_lltable_delete;
2320 llt->llt_dump_entry = in_lltable_dump_entry;
2321 llt->llt_hash = in_lltable_hash;
2322 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
2323 llt->llt_free_entry = in_lltable_free_entry;
2324 llt->llt_match_prefix = in_lltable_match_prefix;
2325 lltable_link(llt);
2326
2327 return (llt);
2328 }
2329
2330 #endif /* NARP > 0 */
2331
2332 void *
2333 in_domifattach(struct ifnet *ifp)
2334 {
2335 struct in_ifinfo *ii;
2336
2337 ii = kmem_zalloc(sizeof(struct in_ifinfo), KM_SLEEP);
2338
2339 #if NARP > 0
2340 ii->ii_llt = in_lltattach(ifp);
2341 #endif
2342
2343 #ifdef IPSELSRC
2344 ii->ii_selsrc = in_selsrc_domifattach(ifp);
2345 KASSERT(ii->ii_selsrc != NULL);
2346 #endif
2347
2348 return ii;
2349 }
2350
2351 void
2352 in_domifdetach(struct ifnet *ifp, void *aux)
2353 {
2354 struct in_ifinfo *ii = aux;
2355
2356 #ifdef IPSELSRC
2357 in_selsrc_domifdetach(ifp, ii->ii_selsrc);
2358 #endif
2359 #if NARP > 0
2360 lltable_free(ii->ii_llt);
2361 #endif
2362 kmem_free(ii, sizeof(struct in_ifinfo));
2363 }
2364