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