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