in.c revision 1.246 1 /* $NetBSD: in.c,v 1.246 2022/11/19 08:00:51 yamt 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.246 2022/11/19 08:00:51 yamt 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 /*
1198 * The interface which does not have IPv4 address is not required
1199 * to scrub old address. So, skip scrub such cases.
1200 */
1201 if (oldaddr.sin_family == AF_INET && (scrub || hostIsNew)) {
1202 int newflags = ia->ia4_flags;
1203
1204 ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
1205 ia->ia_ifa.ifa_dstaddr = sintosa(&olddst);
1206 ia->ia4_flags = oldflags;
1207 if (hostIsNew)
1208 in_scrubaddr(ia);
1209 else if (scrub)
1210 in_scrubprefix(ia);
1211 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
1212 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
1213 ia->ia4_flags = newflags;
1214 }
1215
1216 i = ia->ia_addr.sin_addr.s_addr;
1217 if (ifp->if_flags & IFF_POINTOPOINT)
1218 ia->ia_netmask = INADDR_BROADCAST; /* default to /32 */
1219 else if (IN_CLASSA(i))
1220 ia->ia_netmask = IN_CLASSA_NET;
1221 else if (IN_CLASSB(i))
1222 ia->ia_netmask = IN_CLASSB_NET;
1223 else
1224 ia->ia_netmask = IN_CLASSC_NET;
1225 /*
1226 * The subnet mask usually includes at least the standard network part,
1227 * but may may be smaller in the case of supernetting.
1228 * If it is set, we believe it.
1229 */
1230 if (ia->ia_subnetmask == 0) {
1231 ia->ia_subnetmask = ia->ia_netmask;
1232 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
1233 } else
1234 ia->ia_netmask &= ia->ia_subnetmask;
1235
1236 ia->ia_net = i & ia->ia_netmask;
1237 ia->ia_subnet = i & ia->ia_subnetmask;
1238 in_socktrim(&ia->ia_sockmask);
1239
1240 /* re-calculate the "in_maxmtu" value */
1241 in_setmaxmtu();
1242
1243 ia->ia_ifa.ifa_metric = ifp->if_metric;
1244 if (ifp->if_flags & IFF_BROADCAST) {
1245 if (ia->ia_subnetmask == IN_RFC3021_MASK) {
1246 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
1247 ia->ia_netbroadcast.s_addr = INADDR_BROADCAST;
1248 } else {
1249 ia->ia_broadaddr.sin_addr.s_addr =
1250 ia->ia_subnet | ~ia->ia_subnetmask;
1251 ia->ia_netbroadcast.s_addr =
1252 ia->ia_net | ~ia->ia_netmask;
1253 }
1254 } else if (ifp->if_flags & IFF_LOOPBACK) {
1255 ia->ia_dstaddr = ia->ia_addr;
1256 flags |= RTF_HOST;
1257 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1258 if (ia->ia_dstaddr.sin_family != AF_INET)
1259 return (0);
1260 flags |= RTF_HOST;
1261 }
1262
1263 /* Add the local route to the address */
1264 in_ifaddlocal(&ia->ia_ifa);
1265
1266 /* Add the prefix route for the address */
1267 error = in_addprefix(ia, flags);
1268
1269 /*
1270 * If the interface supports multicast, join the "all hosts"
1271 * multicast group on that interface.
1272 */
1273 mutex_enter(&in_ifaddr_lock);
1274 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
1275 struct in_addr addr;
1276
1277 addr.s_addr = INADDR_ALLHOSTS_GROUP;
1278 ia->ia_allhosts = in_addmulti(&addr, ifp);
1279 }
1280 mutex_exit(&in_ifaddr_lock);
1281
1282 if (hostIsNew &&
1283 ia->ia4_flags & IN_IFF_TENTATIVE &&
1284 if_do_dad(ifp))
1285 ia->ia_dad_start((struct ifaddr *)ia);
1286
1287 return error;
1288 }
1289
1290 #define rtinitflags(x) \
1291 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
1292 ? RTF_HOST : 0)
1293
1294 /*
1295 * add a route to prefix ("connected route" in cisco terminology).
1296 * does nothing if there's some interface address with the same prefix already.
1297 */
1298 static int
1299 in_addprefix(struct in_ifaddr *target, int flags)
1300 {
1301 struct in_ifaddr *ia;
1302 struct in_addr prefix, mask, p;
1303 int error;
1304 int s;
1305
1306 if ((flags & RTF_HOST) != 0)
1307 prefix = target->ia_dstaddr.sin_addr;
1308 else {
1309 prefix = target->ia_addr.sin_addr;
1310 mask = target->ia_sockmask.sin_addr;
1311 prefix.s_addr &= mask.s_addr;
1312 }
1313
1314 s = pserialize_read_enter();
1315 IN_ADDRLIST_READER_FOREACH(ia) {
1316 if (rtinitflags(ia))
1317 p = ia->ia_dstaddr.sin_addr;
1318 else {
1319 p = ia->ia_addr.sin_addr;
1320 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1321 }
1322
1323 if (prefix.s_addr != p.s_addr)
1324 continue;
1325
1326 /*
1327 * if we got a matching prefix route inserted by other
1328 * interface address, we don't need to bother
1329 *
1330 * XXX RADIX_MPATH implications here? -dyoung
1331 */
1332 if (ia->ia_flags & IFA_ROUTE) {
1333 pserialize_read_exit(s);
1334 return 0;
1335 }
1336 }
1337 pserialize_read_exit(s);
1338
1339 /*
1340 * noone seem to have prefix route. insert it.
1341 */
1342 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1343 if (error == 0)
1344 target->ia_flags |= IFA_ROUTE;
1345 else if (error == EEXIST) {
1346 /*
1347 * the fact the route already exists is not an error.
1348 */
1349 error = 0;
1350 }
1351 return error;
1352 }
1353
1354 static int
1355 in_rt_ifa_matcher(struct rtentry *rt, void *v)
1356 {
1357 struct ifaddr *ifa = v;
1358
1359 if (rt->rt_ifa == ifa)
1360 return 1;
1361 else
1362 return 0;
1363 }
1364
1365 /*
1366 * remove a route to prefix ("connected route" in cisco terminology).
1367 * re-installs the route by using another interface address, if there's one
1368 * with the same prefix (otherwise we lose the route mistakenly).
1369 */
1370 static int
1371 in_scrubprefix(struct in_ifaddr *target)
1372 {
1373 struct in_ifaddr *ia;
1374 struct in_addr prefix, mask, p;
1375 int error;
1376 int s;
1377
1378 /* If we don't have IFA_ROUTE we have nothing to do */
1379 if ((target->ia_flags & IFA_ROUTE) == 0)
1380 return 0;
1381
1382 if (rtinitflags(target))
1383 prefix = target->ia_dstaddr.sin_addr;
1384 else {
1385 prefix = target->ia_addr.sin_addr;
1386 mask = target->ia_sockmask.sin_addr;
1387 prefix.s_addr &= mask.s_addr;
1388 }
1389
1390 s = pserialize_read_enter();
1391 IN_ADDRLIST_READER_FOREACH(ia) {
1392 if (rtinitflags(ia))
1393 p = ia->ia_dstaddr.sin_addr;
1394 else {
1395 p = ia->ia_addr.sin_addr;
1396 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1397 }
1398
1399 if (prefix.s_addr != p.s_addr)
1400 continue;
1401
1402 /*
1403 * if we got a matching prefix route, move IFA_ROUTE to him
1404 */
1405 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1406 struct psref psref;
1407 int bound = curlwp_bind();
1408
1409 ia4_acquire(ia, &psref);
1410 pserialize_read_exit(s);
1411
1412 rtinit(&target->ia_ifa, RTM_DELETE,
1413 rtinitflags(target));
1414 target->ia_flags &= ~IFA_ROUTE;
1415
1416 error = rtinit(&ia->ia_ifa, RTM_ADD,
1417 rtinitflags(ia) | RTF_UP);
1418 if (error == 0)
1419 ia->ia_flags |= IFA_ROUTE;
1420
1421 if (!ISSET(target->ia_ifa.ifa_flags, IFA_DESTROYING))
1422 goto skip;
1423 /*
1424 * Replace rt_ifa of routes that have the removing address
1425 * with the new address.
1426 */
1427 rt_replace_ifa_matched_entries(AF_INET,
1428 in_rt_ifa_matcher, &target->ia_ifa, &ia->ia_ifa);
1429
1430 skip:
1431 ia4_release(ia, &psref);
1432 curlwp_bindx(bound);
1433
1434 return error;
1435 }
1436 }
1437 pserialize_read_exit(s);
1438
1439 /*
1440 * noone seem to have prefix route. remove it.
1441 */
1442 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1443 target->ia_flags &= ~IFA_ROUTE;
1444
1445 if (ISSET(target->ia_ifa.ifa_flags, IFA_DESTROYING)) {
1446 /* Remove routes that have the removing address as rt_ifa. */
1447 rt_delete_matched_entries(AF_INET, in_rt_ifa_matcher,
1448 &target->ia_ifa, true);
1449 }
1450
1451 return 0;
1452 }
1453
1454 #undef rtinitflags
1455
1456 /*
1457 * Return 1 if the address might be a local broadcast address.
1458 */
1459 int
1460 in_broadcast(struct in_addr in, struct ifnet *ifp)
1461 {
1462 struct ifaddr *ifa;
1463 int s;
1464
1465 KASSERT(ifp != NULL);
1466
1467 if (in.s_addr == INADDR_BROADCAST ||
1468 in_nullhost(in))
1469 return 1;
1470 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1471 return 0;
1472 /*
1473 * Look through the list of addresses for a match
1474 * with a broadcast address.
1475 */
1476 #define ia (ifatoia(ifa))
1477 s = pserialize_read_enter();
1478 IFADDR_READER_FOREACH(ifa, ifp) {
1479 if (ifa->ifa_addr->sa_family == AF_INET &&
1480 !in_hosteq(in, ia->ia_addr.sin_addr) &&
1481 (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1482 in_hosteq(in, ia->ia_netbroadcast) ||
1483 (hostzeroisbroadcast &&
1484 /*
1485 * Check for old-style (host 0) broadcast, but
1486 * taking into account that RFC 3021 obsoletes it.
1487 */
1488 ia->ia_subnetmask != IN_RFC3021_MASK &&
1489 (in.s_addr == ia->ia_subnet ||
1490 in.s_addr == ia->ia_net)))) {
1491 pserialize_read_exit(s);
1492 return 1;
1493 }
1494 }
1495 pserialize_read_exit(s);
1496 return (0);
1497 #undef ia
1498 }
1499
1500 /*
1501 * perform DAD when interface becomes IFF_UP.
1502 */
1503 void
1504 in_if_link_up(struct ifnet *ifp)
1505 {
1506 struct ifaddr *ifa;
1507 struct in_ifaddr *ia;
1508 int s, bound;
1509
1510 /* Ensure it's sane to run DAD */
1511 if (ifp->if_link_state == LINK_STATE_DOWN)
1512 return;
1513 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
1514 return;
1515
1516 bound = curlwp_bind();
1517 s = pserialize_read_enter();
1518 IFADDR_READER_FOREACH(ifa, ifp) {
1519 struct psref psref;
1520
1521 if (ifa->ifa_addr->sa_family != AF_INET)
1522 continue;
1523 ifa_acquire(ifa, &psref);
1524 pserialize_read_exit(s);
1525
1526 ia = (struct in_ifaddr *)ifa;
1527
1528 /* If detached then mark as tentative */
1529 if (ia->ia4_flags & IN_IFF_DETACHED) {
1530 ia->ia4_flags &= ~IN_IFF_DETACHED;
1531 if (ip_dad_enabled() && if_do_dad(ifp) &&
1532 ia->ia_dad_start != NULL)
1533 ia->ia4_flags |= IN_IFF_TENTATIVE;
1534 else if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0)
1535 rt_addrmsg(RTM_NEWADDR, ifa);
1536 }
1537
1538 if (ia->ia4_flags & IN_IFF_TENTATIVE) {
1539 /* Clear the duplicated flag as we're starting DAD. */
1540 ia->ia4_flags &= ~IN_IFF_DUPLICATED;
1541 ia->ia_dad_start(ifa);
1542 }
1543
1544 s = pserialize_read_enter();
1545 ifa_release(ifa, &psref);
1546 }
1547 pserialize_read_exit(s);
1548 curlwp_bindx(bound);
1549 }
1550
1551 void
1552 in_if_up(struct ifnet *ifp)
1553 {
1554
1555 /* interface may not support link state, so bring it up also */
1556 in_if_link_up(ifp);
1557 }
1558
1559 /*
1560 * Mark all addresses as detached.
1561 */
1562 void
1563 in_if_link_down(struct ifnet *ifp)
1564 {
1565 struct ifaddr *ifa;
1566 struct in_ifaddr *ia;
1567 int s, bound;
1568
1569 bound = curlwp_bind();
1570 s = pserialize_read_enter();
1571 IFADDR_READER_FOREACH(ifa, ifp) {
1572 struct psref psref;
1573
1574 if (ifa->ifa_addr->sa_family != AF_INET)
1575 continue;
1576 ifa_acquire(ifa, &psref);
1577 pserialize_read_exit(s);
1578
1579 ia = (struct in_ifaddr *)ifa;
1580
1581 /* Stop DAD processing */
1582 if (ia->ia_dad_stop != NULL)
1583 ia->ia_dad_stop(ifa);
1584
1585 /*
1586 * Mark the address as detached.
1587 */
1588 if (!(ia->ia4_flags & IN_IFF_DETACHED)) {
1589 ia->ia4_flags |= IN_IFF_DETACHED;
1590 ia->ia4_flags &=
1591 ~(IN_IFF_TENTATIVE | IN_IFF_DUPLICATED);
1592 rt_addrmsg(RTM_NEWADDR, ifa);
1593 }
1594
1595 s = pserialize_read_enter();
1596 ifa_release(ifa, &psref);
1597 }
1598 pserialize_read_exit(s);
1599 curlwp_bindx(bound);
1600 }
1601
1602 void
1603 in_if_down(struct ifnet *ifp)
1604 {
1605
1606 in_if_link_down(ifp);
1607 #if NARP > 0
1608 lltable_purge_entries(LLTABLE(ifp));
1609 #endif
1610 }
1611
1612 void
1613 in_if_link_state_change(struct ifnet *ifp, int link_state)
1614 {
1615
1616 /*
1617 * Treat LINK_STATE_UNKNOWN as UP.
1618 * LINK_STATE_UNKNOWN transitions to LINK_STATE_DOWN when
1619 * if_link_state_change() transitions to LINK_STATE_UP.
1620 */
1621 if (link_state == LINK_STATE_DOWN)
1622 in_if_link_down(ifp);
1623 else
1624 in_if_link_up(ifp);
1625 }
1626
1627 /*
1628 * in_lookup_multi: look up the in_multi record for a given IP
1629 * multicast address on a given interface. If no matching record is
1630 * found, return NULL.
1631 */
1632 struct in_multi *
1633 in_lookup_multi(struct in_addr addr, ifnet_t *ifp)
1634 {
1635 struct in_multi *inm;
1636
1637 KASSERT(rw_lock_held(&in_multilock));
1638
1639 LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) {
1640 if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp)
1641 break;
1642 }
1643 return inm;
1644 }
1645
1646 /*
1647 * in_multi_group: check whether the address belongs to an IP multicast
1648 * group we are joined on this interface. Returns true or false.
1649 */
1650 bool
1651 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags)
1652 {
1653 bool ingroup;
1654
1655 if (__predict_true(flags & IP_IGMP_MCAST) == 0) {
1656 rw_enter(&in_multilock, RW_READER);
1657 ingroup = in_lookup_multi(addr, ifp) != NULL;
1658 rw_exit(&in_multilock);
1659 } else {
1660 /* XXX Recursive call from ip_output(). */
1661 KASSERT(rw_lock_held(&in_multilock));
1662 ingroup = in_lookup_multi(addr, ifp) != NULL;
1663 }
1664 return ingroup;
1665 }
1666
1667 /*
1668 * Add an address to the list of IP multicast addresses for a given interface.
1669 */
1670 struct in_multi *
1671 in_addmulti(struct in_addr *ap, ifnet_t *ifp)
1672 {
1673 struct sockaddr_in sin;
1674 struct in_multi *inm;
1675
1676 /*
1677 * See if address already in list.
1678 */
1679 rw_enter(&in_multilock, RW_WRITER);
1680 inm = in_lookup_multi(*ap, ifp);
1681 if (inm != NULL) {
1682 /*
1683 * Found it; just increment the reference count.
1684 */
1685 inm->inm_refcount++;
1686 rw_exit(&in_multilock);
1687 return inm;
1688 }
1689
1690 /*
1691 * New address; allocate a new multicast record.
1692 */
1693 inm = pool_get(&inmulti_pool, PR_NOWAIT);
1694 if (inm == NULL) {
1695 rw_exit(&in_multilock);
1696 return NULL;
1697 }
1698 inm->inm_addr = *ap;
1699 inm->inm_ifp = ifp;
1700 inm->inm_refcount = 1;
1701
1702 /*
1703 * Ask the network driver to update its multicast reception
1704 * filter appropriately for the new address.
1705 */
1706 sockaddr_in_init(&sin, ap, 0);
1707 if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
1708 rw_exit(&in_multilock);
1709 pool_put(&inmulti_pool, inm);
1710 return NULL;
1711 }
1712
1713 /*
1714 * Let IGMP know that we have joined a new IP multicast group.
1715 */
1716 if (igmp_joingroup(inm) != 0) {
1717 rw_exit(&in_multilock);
1718 pool_put(&inmulti_pool, inm);
1719 return NULL;
1720 }
1721 LIST_INSERT_HEAD(
1722 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1723 inm, inm_list);
1724 in_multientries++;
1725 rw_exit(&in_multilock);
1726
1727 return inm;
1728 }
1729
1730 /*
1731 * Delete a multicast address record.
1732 */
1733 void
1734 in_delmulti(struct in_multi *inm)
1735 {
1736 struct sockaddr_in sin;
1737
1738 rw_enter(&in_multilock, RW_WRITER);
1739 if (--inm->inm_refcount > 0) {
1740 rw_exit(&in_multilock);
1741 return;
1742 }
1743
1744 /*
1745 * No remaining claims to this record; let IGMP know that
1746 * we are leaving the multicast group.
1747 */
1748 igmp_leavegroup(inm);
1749
1750 /*
1751 * Notify the network driver to update its multicast reception
1752 * filter.
1753 */
1754 sockaddr_in_init(&sin, &inm->inm_addr, 0);
1755 if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
1756
1757 /*
1758 * Unlink from list.
1759 */
1760 LIST_REMOVE(inm, inm_list);
1761 in_multientries--;
1762 rw_exit(&in_multilock);
1763
1764 pool_put(&inmulti_pool, inm);
1765 }
1766
1767 /*
1768 * in_next_multi: step through all of the in_multi records, one at a time.
1769 * The current position is remembered in "step", which the caller must
1770 * provide. in_first_multi(), below, must be called to initialize "step"
1771 * and get the first record. Both macros return a NULL "inm" when there
1772 * are no remaining records.
1773 */
1774 struct in_multi *
1775 in_next_multi(struct in_multistep *step)
1776 {
1777 struct in_multi *inm;
1778
1779 KASSERT(rw_lock_held(&in_multilock));
1780
1781 while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) {
1782 step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]);
1783 }
1784 if ((inm = step->i_inm) != NULL) {
1785 step->i_inm = LIST_NEXT(inm, inm_list);
1786 }
1787 return inm;
1788 }
1789
1790 struct in_multi *
1791 in_first_multi(struct in_multistep *step)
1792 {
1793 KASSERT(rw_lock_held(&in_multilock));
1794
1795 step->i_n = 0;
1796 step->i_inm = LIST_FIRST(&in_multihashtbl[0]);
1797 return in_next_multi(step);
1798 }
1799
1800 void
1801 in_multi_lock(int op)
1802 {
1803 rw_enter(&in_multilock, op);
1804 }
1805
1806 void
1807 in_multi_unlock(void)
1808 {
1809 rw_exit(&in_multilock);
1810 }
1811
1812 int
1813 in_multi_lock_held(void)
1814 {
1815 return rw_lock_held(&in_multilock);
1816 }
1817
1818 struct in_ifaddr *
1819 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
1820 int soopts, struct ip_moptions *mopts, int *errorp, struct psref *psref)
1821 {
1822 struct rtentry *rt = NULL;
1823 struct in_ifaddr *ia = NULL;
1824
1825 KASSERT(ISSET(curlwp->l_pflag, LP_BOUND));
1826 /*
1827 * If route is known or can be allocated now, take the
1828 * source address from the interface. Otherwise, punt.
1829 */
1830 if ((soopts & SO_DONTROUTE) != 0)
1831 rtcache_free(ro);
1832 else {
1833 union {
1834 struct sockaddr dst;
1835 struct sockaddr_in dst4;
1836 } u;
1837
1838 sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
1839 rt = rtcache_lookup(ro, &u.dst);
1840 }
1841 /*
1842 * If we found a route, use the address
1843 * corresponding to the outgoing interface
1844 * unless it is the loopback (in case a route
1845 * to our address on another net goes to loopback).
1846 *
1847 * XXX Is this still true? Do we care?
1848 */
1849 if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
1850 int s;
1851 struct ifaddr *ifa;
1852 /*
1853 * Just in case. May not need to do this workaround.
1854 * Revisit when working on rtentry MP-ification.
1855 */
1856 s = pserialize_read_enter();
1857 IFADDR_READER_FOREACH(ifa, rt->rt_ifp) {
1858 if (ifa == rt->rt_ifa)
1859 break;
1860 }
1861 if (ifa != NULL)
1862 ifa_acquire(ifa, psref);
1863 pserialize_read_exit(s);
1864
1865 ia = ifatoia(ifa);
1866 }
1867 if (ia == NULL) {
1868 in_port_t fport = sin->sin_port;
1869 struct ifaddr *ifa;
1870 int s;
1871
1872 sin->sin_port = 0;
1873 ifa = ifa_ifwithladdr_psref(sintosa(sin), psref);
1874 sin->sin_port = fport;
1875 if (ifa == NULL) {
1876 /* Find 1st non-loopback AF_INET address */
1877 s = pserialize_read_enter();
1878 IN_ADDRLIST_READER_FOREACH(ia) {
1879 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
1880 break;
1881 }
1882 if (ia != NULL)
1883 ia4_acquire(ia, psref);
1884 pserialize_read_exit(s);
1885 } else {
1886 /* ia is already referenced by psref */
1887 ia = ifatoia(ifa);
1888 }
1889 if (ia == NULL) {
1890 *errorp = EADDRNOTAVAIL;
1891 goto out;
1892 }
1893 }
1894 /*
1895 * If the destination address is multicast and an outgoing
1896 * interface has been set as a multicast option, use the
1897 * address of that interface as our source address.
1898 */
1899 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
1900 struct ip_moptions *imo;
1901
1902 imo = mopts;
1903 if (imo->imo_multicast_if_index != 0) {
1904 struct ifnet *ifp;
1905 int s;
1906
1907 if (ia != NULL)
1908 ia4_release(ia, psref);
1909 s = pserialize_read_enter();
1910 ifp = if_byindex(imo->imo_multicast_if_index);
1911 if (ifp != NULL) {
1912 /* XXX */
1913 ia = in_get_ia_from_ifp_psref(ifp, psref);
1914 } else
1915 ia = NULL;
1916 if (ia == NULL || ia->ia4_flags & IN_IFF_NOTREADY) {
1917 pserialize_read_exit(s);
1918 if (ia != NULL)
1919 ia4_release(ia, psref);
1920 *errorp = EADDRNOTAVAIL;
1921 ia = NULL;
1922 goto out;
1923 }
1924 pserialize_read_exit(s);
1925 }
1926 }
1927 if (ia->ia_ifa.ifa_getifa != NULL) {
1928 ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
1929 sintosa(sin)));
1930 if (ia == NULL) {
1931 *errorp = EADDRNOTAVAIL;
1932 goto out;
1933 }
1934 /* FIXME NOMPSAFE */
1935 ia4_acquire(ia, psref);
1936 }
1937 #ifdef GETIFA_DEBUG
1938 else
1939 printf("%s: missing ifa_getifa\n", __func__);
1940 #endif
1941 out:
1942 rtcache_unref(rt, ro);
1943 return ia;
1944 }
1945
1946 int
1947 in_tunnel_validate(const struct ip *ip, struct in_addr src, struct in_addr dst)
1948 {
1949 struct in_ifaddr *ia4;
1950 int s;
1951
1952 /* check for address match */
1953 if (src.s_addr != ip->ip_dst.s_addr ||
1954 dst.s_addr != ip->ip_src.s_addr)
1955 return 0;
1956
1957 /* martian filters on outer source - NOT done in ip_input! */
1958 if (IN_MULTICAST(ip->ip_src.s_addr))
1959 return 0;
1960 switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
1961 case 0:
1962 case 127:
1963 case 255:
1964 return 0;
1965 }
1966 /* reject packets with broadcast on source */
1967 s = pserialize_read_enter();
1968 IN_ADDRLIST_READER_FOREACH(ia4) {
1969 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
1970 continue;
1971 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
1972 pserialize_read_exit(s);
1973 return 0;
1974 }
1975 }
1976 pserialize_read_exit(s);
1977
1978 /* NOTE: packet may dropped by uRPF */
1979
1980 /* return valid bytes length */
1981 return sizeof(src) + sizeof(dst);
1982 }
1983
1984 #if NARP > 0
1985
1986 #define IN_LLTBL_DEFAULT_HSIZE 32
1987 #define IN_LLTBL_HASH(k, h) \
1988 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1989
1990 /*
1991 * Do actual deallocation of @lle.
1992 * Called by LLE_FREE_LOCKED when number of references
1993 * drops to zero.
1994 */
1995 static void
1996 in_lltable_destroy_lle(struct llentry *lle)
1997 {
1998
1999 KASSERTMSG(lle->la_numheld == 0, "la_numheld=%d", lle->la_numheld);
2000
2001 LLE_WUNLOCK(lle);
2002 LLE_LOCK_DESTROY(lle);
2003 llentry_pool_put(lle);
2004 }
2005
2006 static struct llentry *
2007 in_lltable_new(struct in_addr addr4, u_int flags)
2008 {
2009 struct llentry *lle;
2010
2011 lle = llentry_pool_get(PR_NOWAIT);
2012 if (lle == NULL) /* NB: caller generates msg */
2013 return NULL;
2014
2015 lle->r_l3addr.addr4 = addr4;
2016 lle->lle_refcnt = 1;
2017 lle->lle_free = in_lltable_destroy_lle;
2018 LLE_LOCK_INIT(lle);
2019 callout_init(&lle->la_timer, CALLOUT_MPSAFE);
2020
2021 return lle;
2022 }
2023
2024 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
2025 (((ntohl((d).s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
2026
2027 static int
2028 in_lltable_match_prefix(const struct sockaddr *prefix,
2029 const struct sockaddr *mask, u_int flags, struct llentry *lle)
2030 {
2031 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
2032 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
2033 struct in_addr lle_addr;
2034
2035 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
2036
2037 /*
2038 * (flags & LLE_STATIC) means deleting all entries
2039 * including static ARP entries.
2040 */
2041 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, pfx, msk) &&
2042 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)))
2043 return (1);
2044
2045 return (0);
2046 }
2047
2048 static void
2049 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
2050 {
2051 size_t pkts_dropped;
2052
2053 LLE_WLOCK_ASSERT(lle);
2054 KASSERT(llt != NULL);
2055
2056 pkts_dropped = llentry_free(lle);
2057 arp_stat_add(ARP_STAT_DFRDROPPED, (uint64_t)pkts_dropped);
2058 }
2059
2060 static int
2061 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr,
2062 const struct rtentry *rt)
2063 {
2064 int error = EINVAL;
2065
2066 if (rt == NULL)
2067 return error;
2068
2069 /*
2070 * If the gateway for an existing host route matches the target L3
2071 * address, which is a special route inserted by some implementation
2072 * such as MANET, and the interface is of the correct type, then
2073 * allow for ARP to proceed.
2074 */
2075 if (rt->rt_flags & RTF_GATEWAY) {
2076 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
2077 rt->rt_ifp->if_type != IFT_ETHER ||
2078 (rt->rt_ifp->if_flags & IFF_NOARP) != 0 ||
2079 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
2080 sizeof(in_addr_t)) != 0) {
2081 goto error;
2082 }
2083 }
2084
2085 /*
2086 * Make sure that at least the destination address is covered
2087 * by the route. This is for handling the case where 2 or more
2088 * interfaces have the same prefix. An incoming packet arrives
2089 * on one interface and the corresponding outgoing packet leaves
2090 * another interface.
2091 */
2092 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
2093 const char *sa, *mask, *addr, *lim;
2094 int len;
2095
2096 mask = (const char *)rt_mask(rt);
2097 /*
2098 * Just being extra cautious to avoid some custom
2099 * code getting into trouble.
2100 */
2101 if (mask == NULL)
2102 goto error;
2103
2104 sa = (const char *)rt_getkey(rt);
2105 addr = (const char *)l3addr;
2106 len = ((const struct sockaddr_in *)l3addr)->sin_len;
2107 lim = addr + len;
2108
2109 for ( ; addr < lim; sa++, mask++, addr++) {
2110 if ((*sa ^ *addr) & *mask) {
2111 #ifdef DIAGNOSTIC
2112 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
2113 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
2114 #endif
2115 goto error;
2116 }
2117 }
2118 }
2119
2120 error = 0;
2121 error:
2122 return error;
2123 }
2124
2125 static inline uint32_t
2126 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
2127 {
2128
2129 return (IN_LLTBL_HASH(dst.s_addr, hsize));
2130 }
2131
2132 static uint32_t
2133 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
2134 {
2135
2136 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
2137 }
2138
2139 static void
2140 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2141 {
2142 struct sockaddr_in *sin;
2143
2144 sin = (struct sockaddr_in *)sa;
2145 memset(sin, 0, sizeof(*sin));
2146 sin->sin_family = AF_INET;
2147 sin->sin_len = sizeof(*sin);
2148 sin->sin_addr = lle->r_l3addr.addr4;
2149 }
2150
2151 static inline struct llentry *
2152 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
2153 {
2154 struct llentry *lle;
2155 struct llentries *lleh;
2156 u_int hashidx;
2157
2158 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
2159 lleh = &llt->lle_head[hashidx];
2160 LIST_FOREACH(lle, lleh, lle_next) {
2161 if (lle->la_flags & LLE_DELETED)
2162 continue;
2163 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
2164 break;
2165 }
2166
2167 return (lle);
2168 }
2169
2170 static int
2171 in_lltable_delete(struct lltable *llt, u_int flags,
2172 const struct sockaddr *l3addr)
2173 {
2174 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
2175 struct ifnet *ifp __diagused = llt->llt_ifp;
2176 struct llentry *lle;
2177
2178 IF_AFDATA_WLOCK_ASSERT(ifp);
2179 KASSERTMSG(l3addr->sa_family == AF_INET,
2180 "sin_family %d", l3addr->sa_family);
2181
2182 lle = in_lltable_find_dst(llt, sin->sin_addr);
2183 if (lle == NULL) {
2184 #ifdef LLTABLE_DEBUG
2185 char buf[64];
2186 sockaddr_format(l3addr, buf, sizeof(buf));
2187 log(LOG_INFO, "%s: cache for %s is not found\n",
2188 __func__, buf);
2189 #endif
2190 return (ENOENT);
2191 }
2192
2193 LLE_WLOCK(lle);
2194 #ifdef LLTABLE_DEBUG
2195 {
2196 char buf[64];
2197 sockaddr_format(l3addr, buf, sizeof(buf));
2198 log(LOG_INFO, "%s: cache for %s (%p) is deleted\n",
2199 __func__, buf, lle);
2200 }
2201 #endif
2202 llentry_free(lle);
2203
2204 return (0);
2205 }
2206
2207 static struct llentry *
2208 in_lltable_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr,
2209 const struct rtentry *rt)
2210 {
2211 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
2212 struct ifnet *ifp = llt->llt_ifp;
2213 struct llentry *lle;
2214
2215 IF_AFDATA_WLOCK_ASSERT(ifp);
2216 KASSERTMSG(l3addr->sa_family == AF_INET,
2217 "sin_family %d", l3addr->sa_family);
2218
2219 lle = in_lltable_find_dst(llt, sin->sin_addr);
2220
2221 if (lle != NULL) {
2222 LLE_WLOCK(lle);
2223 return (lle);
2224 }
2225
2226 /* no existing record, we need to create new one */
2227
2228 /*
2229 * A route that covers the given address must have
2230 * been installed 1st because we are doing a resolution,
2231 * verify this.
2232 */
2233 if (!(flags & LLE_IFADDR) &&
2234 in_lltable_rtcheck(ifp, flags, l3addr, rt) != 0)
2235 return (NULL);
2236
2237 lle = in_lltable_new(sin->sin_addr, flags);
2238 if (lle == NULL) {
2239 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2240 return (NULL);
2241 }
2242 lle->la_flags = flags;
2243 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2244 memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
2245 lle->la_flags |= (LLE_VALID | LLE_STATIC);
2246 }
2247
2248 lltable_link_entry(llt, lle);
2249 LLE_WLOCK(lle);
2250
2251 return (lle);
2252 }
2253
2254 /*
2255 * Return NULL if not found or marked for deletion.
2256 * If found return lle read locked.
2257 */
2258 static struct llentry *
2259 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
2260 {
2261 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
2262 struct llentry *lle;
2263
2264 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
2265 KASSERTMSG(l3addr->sa_family == AF_INET,
2266 "sin_family %d", l3addr->sa_family);
2267
2268 lle = in_lltable_find_dst(llt, sin->sin_addr);
2269
2270 if (lle == NULL)
2271 return NULL;
2272
2273 if (flags & LLE_EXCLUSIVE)
2274 LLE_WLOCK(lle);
2275 else
2276 LLE_RLOCK(lle);
2277
2278 return lle;
2279 }
2280
2281 static int
2282 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2283 struct rt_walkarg *w)
2284 {
2285 struct sockaddr_in sin;
2286
2287 LLTABLE_LOCK_ASSERT();
2288
2289 /* skip deleted entries */
2290 if (lle->la_flags & LLE_DELETED)
2291 return 0;
2292
2293 sockaddr_in_init(&sin, &lle->r_l3addr.addr4, 0);
2294
2295 return lltable_dump_entry(llt, lle, w, sintosa(&sin));
2296 }
2297
2298 #endif /* NARP > 0 */
2299
2300 static int
2301 in_multicast_sysctl(SYSCTLFN_ARGS)
2302 {
2303 struct ifnet *ifp;
2304 struct ifaddr *ifa;
2305 struct in_ifaddr *ifa4;
2306 struct in_multi *inm;
2307 uint32_t tmp;
2308 int error;
2309 size_t written;
2310 struct psref psref;
2311 int bound;
2312
2313 if (namelen != 1)
2314 return EINVAL;
2315
2316 bound = curlwp_bind();
2317 ifp = if_get_byindex(name[0], &psref);
2318 if (ifp == NULL) {
2319 curlwp_bindx(bound);
2320 return ENODEV;
2321 }
2322
2323 if (oldp == NULL) {
2324 *oldlenp = 0;
2325 IFADDR_FOREACH(ifa, ifp) {
2326 if (ifa->ifa_addr->sa_family != AF_INET)
2327 continue;
2328 ifa4 = (void *)ifa;
2329 LIST_FOREACH(inm, &ifa4->ia_multiaddrs, inm_list) {
2330 *oldlenp += 2 * sizeof(struct in_addr) +
2331 sizeof(uint32_t);
2332 }
2333 }
2334 if_put(ifp, &psref);
2335 curlwp_bindx(bound);
2336 return 0;
2337 }
2338
2339 error = 0;
2340 written = 0;
2341 IFADDR_FOREACH(ifa, ifp) {
2342 if (ifa->ifa_addr->sa_family != AF_INET)
2343 continue;
2344 ifa4 = (void *)ifa;
2345 LIST_FOREACH(inm, &ifa4->ia_multiaddrs, inm_list) {
2346 if (written + 2 * sizeof(struct in_addr) +
2347 sizeof(uint32_t) > *oldlenp)
2348 goto done;
2349 error = sysctl_copyout(l, &ifa4->ia_addr.sin_addr,
2350 oldp, sizeof(struct in_addr));
2351 if (error)
2352 goto done;
2353 oldp = (char *)oldp + sizeof(struct in_addr);
2354 written += sizeof(struct in_addr);
2355 error = sysctl_copyout(l, &inm->inm_addr,
2356 oldp, sizeof(struct in_addr));
2357 if (error)
2358 goto done;
2359 oldp = (char *)oldp + sizeof(struct in_addr);
2360 written += sizeof(struct in_addr);
2361 tmp = inm->inm_refcount;
2362 error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
2363 if (error)
2364 goto done;
2365 oldp = (char *)oldp + sizeof(tmp);
2366 written += sizeof(tmp);
2367 }
2368 }
2369 done:
2370 if_put(ifp, &psref);
2371 curlwp_bindx(bound);
2372 *oldlenp = written;
2373 return error;
2374 }
2375
2376 static void
2377 in_sysctl_init(struct sysctllog **clog)
2378 {
2379 sysctl_createv(clog, 0, NULL, NULL,
2380 CTLFLAG_PERMANENT,
2381 CTLTYPE_NODE, "inet",
2382 SYSCTL_DESCR("PF_INET related settings"),
2383 NULL, 0, NULL, 0,
2384 CTL_NET, PF_INET, CTL_EOL);
2385 sysctl_createv(clog, 0, NULL, NULL,
2386 CTLFLAG_PERMANENT,
2387 CTLTYPE_NODE, "multicast",
2388 SYSCTL_DESCR("Multicast information"),
2389 in_multicast_sysctl, 0, NULL, 0,
2390 CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
2391 sysctl_createv(clog, 0, NULL, NULL,
2392 CTLFLAG_PERMANENT,
2393 CTLTYPE_NODE, "ip",
2394 SYSCTL_DESCR("IPv4 related settings"),
2395 NULL, 0, NULL, 0,
2396 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
2397
2398 sysctl_createv(clog, 0, NULL, NULL,
2399 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2400 CTLTYPE_INT, "subnetsarelocal",
2401 SYSCTL_DESCR("Whether logical subnets are considered "
2402 "local"),
2403 NULL, 0, &subnetsarelocal, 0,
2404 CTL_NET, PF_INET, IPPROTO_IP,
2405 IPCTL_SUBNETSARELOCAL, CTL_EOL);
2406 sysctl_createv(clog, 0, NULL, NULL,
2407 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2408 CTLTYPE_INT, "hostzerobroadcast",
2409 SYSCTL_DESCR("All zeroes address is broadcast address"),
2410 NULL, 0, &hostzeroisbroadcast, 0,
2411 CTL_NET, PF_INET, IPPROTO_IP,
2412 IPCTL_HOSTZEROBROADCAST, CTL_EOL);
2413 }
2414
2415 #if NARP > 0
2416
2417 static struct lltable *
2418 in_lltattach(struct ifnet *ifp, struct in_ifinfo *ii)
2419 {
2420 struct lltable *llt;
2421
2422 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
2423 llt->llt_af = AF_INET;
2424 llt->llt_ifp = ifp;
2425
2426 llt->llt_lookup = in_lltable_lookup;
2427 llt->llt_create = in_lltable_create;
2428 llt->llt_delete = in_lltable_delete;
2429 llt->llt_dump_entry = in_lltable_dump_entry;
2430 llt->llt_hash = in_lltable_hash;
2431 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
2432 llt->llt_free_entry = in_lltable_free_entry;
2433 llt->llt_match_prefix = in_lltable_match_prefix;
2434 #ifdef MBUFTRACE
2435 struct mowner *mowner = &ii->ii_mowner;
2436 mowner_init_owner(mowner, ifp->if_xname, "arp");
2437 MOWNER_ATTACH(mowner);
2438 llt->llt_mowner = mowner;
2439 #endif
2440 lltable_link(llt);
2441
2442 return (llt);
2443 }
2444
2445 #endif /* NARP > 0 */
2446
2447 void *
2448 in_domifattach(struct ifnet *ifp)
2449 {
2450 struct in_ifinfo *ii;
2451
2452 ii = kmem_zalloc(sizeof(struct in_ifinfo), KM_SLEEP);
2453
2454 #if NARP > 0
2455 ii->ii_llt = in_lltattach(ifp, ii);
2456 #endif
2457
2458 #ifdef IPSELSRC
2459 ii->ii_selsrc = in_selsrc_domifattach(ifp);
2460 KASSERT(ii->ii_selsrc != NULL);
2461 #endif
2462
2463 return ii;
2464 }
2465
2466 void
2467 in_domifdetach(struct ifnet *ifp, void *aux)
2468 {
2469 struct in_ifinfo *ii = aux;
2470
2471 #ifdef IPSELSRC
2472 in_selsrc_domifdetach(ifp, ii->ii_selsrc);
2473 #endif
2474 #if NARP > 0
2475 lltable_free(ii->ii_llt);
2476 #ifdef MBUFTRACE
2477 MOWNER_DETACH(&ii->ii_mowner);
2478 #endif
2479 #endif
2480 kmem_free(ii, sizeof(struct in_ifinfo));
2481 }
2482