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