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