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