in.c revision 1.152 1 /* $NetBSD: in.c,v 1.152 2015/05/02 14:41:32 roy Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 */
61
62 /*
63 * Copyright (c) 1982, 1986, 1991, 1993
64 * The Regents of the University of California. All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
77 *
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * SUCH DAMAGE.
89 *
90 * @(#)in.c 8.4 (Berkeley) 1/9/95
91 */
92
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.152 2015/05/02 14:41:32 roy Exp $");
95
96 #include "opt_inet.h"
97 #include "opt_inet_conf.h"
98 #include "opt_mrouting.h"
99
100 #include <sys/param.h>
101 #include <sys/ioctl.h>
102 #include <sys/errno.h>
103 #include <sys/kernel.h>
104 #include <sys/malloc.h>
105 #include <sys/socket.h>
106 #include <sys/socketvar.h>
107 #include <sys/sysctl.h>
108 #include <sys/systm.h>
109 #include <sys/proc.h>
110 #include <sys/syslog.h>
111 #include <sys/kauth.h>
112
113 #include <sys/cprng.h>
114
115 #include <net/if.h>
116 #include <net/route.h>
117 #include <net/pfil.h>
118
119 #include <net/if_ether.h>
120
121 #include <netinet/in_systm.h>
122 #include <netinet/in.h>
123 #include <netinet/in_var.h>
124 #include <netinet/ip.h>
125 #include <netinet/ip_var.h>
126 #include <netinet/in_ifattach.h>
127 #include <netinet/in_pcb.h>
128 #include <netinet/if_inarp.h>
129 #include <netinet/ip_mroute.h>
130 #include <netinet/igmp_var.h>
131
132 #ifdef IPSELSRC
133 #include <netinet/in_selsrc.h>
134 #endif
135
136 static u_int in_mask2len(struct in_addr *);
137 static void in_len2mask(struct in_addr *, u_int);
138 static int in_lifaddr_ioctl(struct socket *, u_long, void *,
139 struct ifnet *);
140
141 static int in_addprefix(struct in_ifaddr *, int);
142 static int in_scrubprefix(struct in_ifaddr *);
143 static void in_sysctl_init(struct sysctllog **);
144
145 #ifndef SUBNETSARELOCAL
146 #define SUBNETSARELOCAL 1
147 #endif
148
149 #ifndef HOSTZEROBROADCAST
150 #define HOSTZEROBROADCAST 1
151 #endif
152
153 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */
154 #ifndef IN_MULTI_HASH_SIZE
155 #define IN_MULTI_HASH_SIZE 509
156 #endif
157
158 static int subnetsarelocal = SUBNETSARELOCAL;
159 static int hostzeroisbroadcast = HOSTZEROBROADCAST;
160
161 /*
162 * This list is used to keep track of in_multi chains which belong to
163 * deleted interface addresses. We use in_ifaddr so that a chain head
164 * won't be deallocated until all multicast address record are deleted.
165 */
166
167 LIST_HEAD(in_multihashhead, in_multi); /* Type of the hash head */
168
169 static struct pool inmulti_pool;
170 static u_int in_multientries;
171 static struct in_multihashhead *in_multihashtbl;
172 static u_long in_multihash;
173 static krwlock_t in_multilock;
174
175 #define IN_MULTI_HASH(x, ifp) \
176 (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE])
177
178 struct in_ifaddrhashhead * in_ifaddrhashtbl;
179 u_long in_ifaddrhash;
180 struct in_ifaddrhead in_ifaddrhead;
181
182 void
183 in_init(void)
184 {
185 pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
186 NULL, IPL_SOFTNET);
187 TAILQ_INIT(&in_ifaddrhead);
188
189 in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
190 &in_ifaddrhash);
191 in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
192 &in_multihash);
193 rw_init(&in_multilock);
194
195 in_sysctl_init(NULL);
196 }
197
198 /*
199 * Return 1 if an internet address is for a ``local'' host
200 * (one to which we have a connection). If subnetsarelocal
201 * is true, this includes other subnets of the local net.
202 * Otherwise, it includes only the directly-connected (sub)nets.
203 */
204 int
205 in_localaddr(struct in_addr in)
206 {
207 struct in_ifaddr *ia;
208
209 if (subnetsarelocal) {
210 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
211 if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
212 return (1);
213 } else {
214 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
215 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
216 return (1);
217 }
218 return (0);
219 }
220
221 /*
222 * Determine whether an IP address is in a reserved set of addresses
223 * that may not be forwarded, or whether datagrams to that destination
224 * may be forwarded.
225 */
226 int
227 in_canforward(struct in_addr in)
228 {
229 u_int32_t net;
230
231 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
232 return (0);
233 if (IN_CLASSA(in.s_addr)) {
234 net = in.s_addr & IN_CLASSA_NET;
235 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
236 return (0);
237 }
238 return (1);
239 }
240
241 /*
242 * Trim a mask in a sockaddr
243 */
244 void
245 in_socktrim(struct sockaddr_in *ap)
246 {
247 char *cplim = (char *) &ap->sin_addr;
248 char *cp = (char *) (&ap->sin_addr + 1);
249
250 ap->sin_len = 0;
251 while (--cp >= cplim)
252 if (*cp) {
253 (ap)->sin_len = cp - (char *) (ap) + 1;
254 break;
255 }
256 }
257
258 /*
259 * Routine to take an Internet address and convert into a
260 * "dotted quad" representation for printing.
261 */
262 const char *
263 in_fmtaddr(struct in_addr addr)
264 {
265 static char buf[sizeof("123.456.789.123")];
266
267 addr.s_addr = ntohl(addr.s_addr);
268
269 snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
270 (addr.s_addr >> 24) & 0xFF,
271 (addr.s_addr >> 16) & 0xFF,
272 (addr.s_addr >> 8) & 0xFF,
273 (addr.s_addr >> 0) & 0xFF);
274 return buf;
275 }
276
277 /*
278 * Maintain the "in_maxmtu" variable, which is the largest
279 * mtu for non-local interfaces with AF_INET addresses assigned
280 * to them that are up.
281 */
282 unsigned long in_maxmtu;
283
284 void
285 in_setmaxmtu(void)
286 {
287 struct in_ifaddr *ia;
288 struct ifnet *ifp;
289 unsigned long maxmtu = 0;
290
291 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
292 if ((ifp = ia->ia_ifp) == 0)
293 continue;
294 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
295 continue;
296 if (ifp->if_mtu > maxmtu)
297 maxmtu = ifp->if_mtu;
298 }
299 if (maxmtu)
300 in_maxmtu = maxmtu;
301 }
302
303 static u_int
304 in_mask2len(struct in_addr *mask)
305 {
306 u_int x, y;
307 u_char *p;
308
309 p = (u_char *)mask;
310 for (x = 0; x < sizeof(*mask); x++) {
311 if (p[x] != 0xff)
312 break;
313 }
314 y = 0;
315 if (x < sizeof(*mask)) {
316 for (y = 0; y < NBBY; y++) {
317 if ((p[x] & (0x80 >> y)) == 0)
318 break;
319 }
320 }
321 return x * NBBY + y;
322 }
323
324 static void
325 in_len2mask(struct in_addr *mask, u_int len)
326 {
327 u_int i;
328 u_char *p;
329
330 p = (u_char *)mask;
331 memset(mask, 0, sizeof(*mask));
332 for (i = 0; i < len / NBBY; i++)
333 p[i] = 0xff;
334 if (len % NBBY)
335 p[i] = (0xff00 >> (len % NBBY)) & 0xff;
336 }
337
338 /*
339 * Generic internet control operations (ioctl's).
340 * Ifp is 0 if not an interface-specific ioctl.
341 */
342 /* ARGSUSED */
343 int
344 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
345 {
346 struct ifreq *ifr = (struct ifreq *)data;
347 struct in_ifaddr *ia = NULL;
348 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
349 struct sockaddr_in oldaddr;
350 int error, hostIsNew, maskIsNew;
351 int newifaddr = 0;
352
353 switch (cmd) {
354 case SIOCALIFADDR:
355 case SIOCDLIFADDR:
356 case SIOCGLIFADDR:
357 if (ifp == NULL)
358 return EINVAL;
359 return in_lifaddr_ioctl(so, cmd, data, ifp);
360 case SIOCGIFADDRPREF:
361 case SIOCSIFADDRPREF:
362 if (ifp == NULL)
363 return EINVAL;
364 return ifaddrpref_ioctl(so, cmd, data, ifp);
365 }
366
367 /*
368 * Find address for this interface, if it exists.
369 */
370 if (ifp != NULL)
371 IFP_TO_IA(ifp, ia);
372
373 switch (cmd) {
374 case SIOCAIFADDR:
375 case SIOCDIFADDR:
376 case SIOCGIFALIAS:
377 case SIOCGIFAFLAG_IN:
378 if (ifra->ifra_addr.sin_family == AF_INET)
379 LIST_FOREACH(ia,
380 &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
381 ia_hash) {
382 if (ia->ia_ifp == ifp &&
383 in_hosteq(ia->ia_addr.sin_addr,
384 ifra->ifra_addr.sin_addr))
385 break;
386 }
387 if ((cmd == SIOCDIFADDR ||
388 cmd == SIOCGIFALIAS ||
389 cmd == SIOCGIFAFLAG_IN) &&
390 ia == NULL)
391 return (EADDRNOTAVAIL);
392
393 if (cmd == SIOCDIFADDR &&
394 ifra->ifra_addr.sin_family == AF_UNSPEC) {
395 ifra->ifra_addr.sin_family = AF_INET;
396 }
397 /* FALLTHROUGH */
398 case SIOCSIFADDR:
399 hostIsNew = 1;
400 if (ia == NULL || ia->ia_addr.sin_family != AF_INET)
401 ;
402 else if (ifra->ifra_addr.sin_len == 0) {
403 ifra->ifra_addr = ia->ia_addr;
404 hostIsNew = 0;
405 } else if (in_hosteq(ia->ia_addr.sin_addr,
406 ifra->ifra_addr.sin_addr))
407 hostIsNew = 0;
408 /* FALLTHROUGH */
409 case SIOCSIFDSTADDR:
410 if (ifra->ifra_addr.sin_family != AF_INET)
411 return (EAFNOSUPPORT);
412 /* FALLTHROUGH */
413 case SIOCSIFNETMASK:
414 if (ifp == NULL)
415 panic("in_control");
416
417 if (cmd == SIOCGIFALIAS || cmd == SIOCGIFAFLAG_IN)
418 break;
419
420 if (ia == NULL &&
421 (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
422 return (EADDRNOTAVAIL);
423
424 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
425 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
426 NULL) != 0)
427 return (EPERM);
428
429 if (ia == NULL) {
430 ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
431 if (ia == NULL)
432 return (ENOBUFS);
433 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
434 ifaref(&ia->ia_ifa);
435 ifa_insert(ifp, &ia->ia_ifa);
436 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
437 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
438 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
439 #ifdef IPSELSRC
440 ia->ia_ifa.ifa_getifa = in_getifa;
441 #else /* IPSELSRC */
442 ia->ia_ifa.ifa_getifa = NULL;
443 #endif /* IPSELSRC */
444 ia->ia_sockmask.sin_len = 8;
445 ia->ia_sockmask.sin_family = AF_INET;
446 if (ifp->if_flags & IFF_BROADCAST) {
447 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
448 ia->ia_broadaddr.sin_family = AF_INET;
449 }
450 ia->ia_ifp = ifp;
451 ia->ia_idsalt = cprng_fast32() % 65535;
452 LIST_INIT(&ia->ia_multiaddrs);
453 newifaddr = 1;
454 }
455 break;
456
457 case SIOCSIFBRDADDR:
458 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
459 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
460 NULL) != 0)
461 return (EPERM);
462 /* FALLTHROUGH */
463
464 case SIOCGIFADDR:
465 case SIOCGIFNETMASK:
466 case SIOCGIFDSTADDR:
467 case SIOCGIFBRDADDR:
468 if (ia == NULL)
469 return (EADDRNOTAVAIL);
470 break;
471 }
472 error = 0;
473 switch (cmd) {
474
475 case SIOCGIFADDR:
476 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
477 break;
478
479 case SIOCGIFBRDADDR:
480 if ((ifp->if_flags & IFF_BROADCAST) == 0)
481 return (EINVAL);
482 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
483 break;
484
485 case SIOCGIFDSTADDR:
486 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
487 return (EINVAL);
488 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
489 break;
490
491 case SIOCGIFNETMASK:
492 /*
493 * We keep the number of trailing zero bytes the sin_len field
494 * of ia_sockmask, so we fix this before we pass it back to
495 * userland.
496 */
497 oldaddr = ia->ia_sockmask;
498 oldaddr.sin_len = sizeof(struct sockaddr_in);
499 ifreq_setaddr(cmd, ifr, (const void *)&oldaddr);
500 break;
501
502 case SIOCSIFDSTADDR:
503 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
504 return (EINVAL);
505 oldaddr = ia->ia_dstaddr;
506 ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
507 if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) {
508 ia->ia_dstaddr = oldaddr;
509 return error;
510 }
511 if (ia->ia_flags & IFA_ROUTE) {
512 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
513 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
514 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
515 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
516 }
517 break;
518
519 case SIOCSIFBRDADDR:
520 if ((ifp->if_flags & IFF_BROADCAST) == 0)
521 return EINVAL;
522 ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
523 break;
524
525 case SIOCSIFADDR:
526 error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
527 1, hostIsNew);
528 if (error == 0) {
529 (void)pfil_run_hooks(if_pfil,
530 (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
531 }
532 break;
533
534 case SIOCSIFNETMASK:
535 in_ifscrub(ifp, ia);
536 ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
537 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
538 error = in_ifinit(ifp, ia, NULL, 0, 0);
539 break;
540
541 case SIOCAIFADDR:
542 maskIsNew = 0;
543 if (ifra->ifra_mask.sin_len) {
544 /* Only scrub if we control the prefix route,
545 * otherwise userland gets a bogus message */
546 if ((ia->ia_flags & IFA_ROUTE))
547 in_ifscrub(ifp, ia);
548 ia->ia_sockmask = ifra->ifra_mask;
549 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
550 maskIsNew = 1;
551 }
552 if ((ifp->if_flags & IFF_POINTOPOINT) &&
553 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
554 /* Only scrub if we control the prefix route,
555 * otherwise userland gets a bogus message */
556 if ((ia->ia_flags & IFA_ROUTE))
557 in_ifscrub(ifp, ia);
558 ia->ia_dstaddr = ifra->ifra_dstaddr;
559 maskIsNew = 1; /* We lie; but the effect's the same */
560 }
561 if (ifra->ifra_addr.sin_family == AF_INET &&
562 (hostIsNew || maskIsNew)) {
563 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0,
564 hostIsNew);
565 }
566 if ((ifp->if_flags & IFF_BROADCAST) &&
567 (ifra->ifra_broadaddr.sin_family == AF_INET))
568 ia->ia_broadaddr = ifra->ifra_broadaddr;
569 if (error == 0)
570 (void)pfil_run_hooks(if_pfil,
571 (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
572 break;
573
574 case SIOCGIFALIAS:
575 ifra->ifra_mask = ia->ia_sockmask;
576 if ((ifp->if_flags & IFF_POINTOPOINT) &&
577 (ia->ia_dstaddr.sin_family == AF_INET))
578 ifra->ifra_dstaddr = ia->ia_dstaddr;
579 else if ((ifp->if_flags & IFF_BROADCAST) &&
580 (ia->ia_broadaddr.sin_family == AF_INET))
581 ifra->ifra_broadaddr = ia->ia_broadaddr;
582 else
583 memset(&ifra->ifra_broadaddr, 0,
584 sizeof(ifra->ifra_broadaddr));
585 break;
586
587 case SIOCGIFAFLAG_IN:
588 ifr->ifr_addrflags = ia->ia4_flags;
589 break;
590
591 case SIOCDIFADDR:
592 in_purgeaddr(&ia->ia_ifa);
593 (void)pfil_run_hooks(if_pfil, (struct mbuf **)SIOCDIFADDR,
594 ifp, PFIL_IFADDR);
595 break;
596
597 #ifdef MROUTING
598 case SIOCGETVIFCNT:
599 case SIOCGETSGCNT:
600 error = mrt_ioctl(so, cmd, data);
601 break;
602 #endif /* MROUTING */
603
604 default:
605 return ENOTTY;
606 }
607
608 if (error != 0 && newifaddr) {
609 KASSERT(ia != NULL);
610 in_purgeaddr(&ia->ia_ifa);
611 }
612
613 return error;
614 }
615
616 /* Add ownaddr as loopback rtentry. */
617 static void
618 in_ifaddlocal(struct ifaddr *ifa)
619 {
620 struct in_ifaddr *ia;
621
622 ia = (struct in_ifaddr *)ifa;
623 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY ||
624 (ia->ia_ifp->if_flags & IFF_POINTOPOINT &&
625 in_hosteq(ia->ia_dstaddr.sin_addr, ia->ia_addr.sin_addr)))
626 {
627 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
628 return;
629 }
630
631 rt_ifa_addlocal(ifa);
632 }
633
634 /* Rempve loopback entry of ownaddr */
635 static void
636 in_ifremlocal(struct ifaddr *ifa)
637 {
638 struct in_ifaddr *ia, *p;
639 struct ifaddr *alt_ifa = NULL;
640 int ia_count = 0;
641
642 ia = (struct in_ifaddr *)ifa;
643 /* Delete the entry if exactly one ifaddr matches the
644 * address, ifa->ifa_addr. */
645 TAILQ_FOREACH(p, &in_ifaddrhead, ia_list) {
646 if (!in_hosteq(p->ia_addr.sin_addr, ia->ia_addr.sin_addr))
647 continue;
648 if (p->ia_ifp != ia->ia_ifp)
649 alt_ifa = &p->ia_ifa;
650 if (++ia_count > 1 && alt_ifa != NULL)
651 break;
652 }
653
654 if (ia_count == 0)
655 return;
656
657 rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa);
658 }
659
660 void
661 in_purgeaddr(struct ifaddr *ifa)
662 {
663 struct ifnet *ifp = ifa->ifa_ifp;
664 struct in_ifaddr *ia = (void *) ifa;
665
666 /* stop DAD processing */
667 arp_dad_stop(ifa);
668
669 in_ifscrub(ifp, ia);
670 in_ifremlocal(ifa);
671 LIST_REMOVE(ia, ia_hash);
672 ifa_remove(ifp, &ia->ia_ifa);
673 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
674 if (ia->ia_allhosts != NULL)
675 in_delmulti(ia->ia_allhosts);
676 ifafree(&ia->ia_ifa);
677 in_setmaxmtu();
678 }
679
680 void
681 in_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */
682 {
683 if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
684 igmp_purgeif(ifp); /* manipulates pools */
685 #ifdef MROUTING
686 ip_mrouter_detach(ifp);
687 #endif
688 }
689
690 /*
691 * SIOC[GAD]LIFADDR.
692 * SIOCGLIFADDR: get first address. (???)
693 * SIOCGLIFADDR with IFLR_PREFIX:
694 * get first address that matches the specified prefix.
695 * SIOCALIFADDR: add the specified address.
696 * SIOCALIFADDR with IFLR_PREFIX:
697 * EINVAL since we can't deduce hostid part of the address.
698 * SIOCDLIFADDR: delete the specified address.
699 * SIOCDLIFADDR with IFLR_PREFIX:
700 * delete the first address that matches the specified prefix.
701 * return values:
702 * EINVAL on invalid parameters
703 * EADDRNOTAVAIL on prefix match failed/specified address not found
704 * other values may be returned from in_ioctl()
705 */
706 static int
707 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
708 struct ifnet *ifp)
709 {
710 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
711 struct ifaddr *ifa;
712 struct sockaddr *sa;
713
714 /* sanity checks */
715 if (data == NULL || ifp == NULL) {
716 panic("invalid argument to in_lifaddr_ioctl");
717 /*NOTRECHED*/
718 }
719
720 switch (cmd) {
721 case SIOCGLIFADDR:
722 /* address must be specified on GET with IFLR_PREFIX */
723 if ((iflr->flags & IFLR_PREFIX) == 0)
724 break;
725 /*FALLTHROUGH*/
726 case SIOCALIFADDR:
727 case SIOCDLIFADDR:
728 /* address must be specified on ADD and DELETE */
729 sa = (struct sockaddr *)&iflr->addr;
730 if (sa->sa_family != AF_INET)
731 return EINVAL;
732 if (sa->sa_len != sizeof(struct sockaddr_in))
733 return EINVAL;
734 /* XXX need improvement */
735 sa = (struct sockaddr *)&iflr->dstaddr;
736 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
737 return EINVAL;
738 if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
739 return EINVAL;
740 break;
741 default: /*shouldn't happen*/
742 #if 0
743 panic("invalid cmd to in_lifaddr_ioctl");
744 /*NOTREACHED*/
745 #else
746 return EOPNOTSUPP;
747 #endif
748 }
749 if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
750 return EINVAL;
751
752 switch (cmd) {
753 case SIOCALIFADDR:
754 {
755 struct in_aliasreq ifra;
756
757 if (iflr->flags & IFLR_PREFIX)
758 return EINVAL;
759
760 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
761 memset(&ifra, 0, sizeof(ifra));
762 memcpy(ifra.ifra_name, iflr->iflr_name,
763 sizeof(ifra.ifra_name));
764
765 memcpy(&ifra.ifra_addr, &iflr->addr,
766 ((struct sockaddr *)&iflr->addr)->sa_len);
767
768 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
769 memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
770 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
771 }
772
773 ifra.ifra_mask.sin_family = AF_INET;
774 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
775 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
776
777 return in_control(so, SIOCAIFADDR, &ifra, ifp);
778 }
779 case SIOCGLIFADDR:
780 case SIOCDLIFADDR:
781 {
782 struct in_ifaddr *ia;
783 struct in_addr mask, candidate, match;
784 struct sockaddr_in *sin;
785 int cmp;
786
787 memset(&mask, 0, sizeof(mask));
788 memset(&match, 0, sizeof(match)); /* XXX gcc */
789 if (iflr->flags & IFLR_PREFIX) {
790 /* lookup a prefix rather than address. */
791 in_len2mask(&mask, iflr->prefixlen);
792
793 sin = (struct sockaddr_in *)&iflr->addr;
794 match.s_addr = sin->sin_addr.s_addr;
795 match.s_addr &= mask.s_addr;
796
797 /* if you set extra bits, that's wrong */
798 if (match.s_addr != sin->sin_addr.s_addr)
799 return EINVAL;
800
801 cmp = 1;
802 } else {
803 if (cmd == SIOCGLIFADDR) {
804 /* on getting an address, take the 1st match */
805 cmp = 0; /*XXX*/
806 } else {
807 /* on deleting an address, do exact match */
808 in_len2mask(&mask, 32);
809 sin = (struct sockaddr_in *)&iflr->addr;
810 match.s_addr = sin->sin_addr.s_addr;
811
812 cmp = 1;
813 }
814 }
815
816 IFADDR_FOREACH(ifa, ifp) {
817 if (ifa->ifa_addr->sa_family != AF_INET)
818 continue;
819 if (cmp == 0)
820 break;
821 candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
822 candidate.s_addr &= mask.s_addr;
823 if (candidate.s_addr == match.s_addr)
824 break;
825 }
826 if (ifa == NULL)
827 return EADDRNOTAVAIL;
828 ia = (struct in_ifaddr *)ifa;
829
830 if (cmd == SIOCGLIFADDR) {
831 /* fill in the if_laddrreq structure */
832 memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
833
834 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
835 memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
836 ia->ia_dstaddr.sin_len);
837 } else
838 memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
839
840 iflr->prefixlen =
841 in_mask2len(&ia->ia_sockmask.sin_addr);
842
843 iflr->flags = 0; /*XXX*/
844
845 return 0;
846 } else {
847 struct in_aliasreq ifra;
848
849 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
850 memset(&ifra, 0, sizeof(ifra));
851 memcpy(ifra.ifra_name, iflr->iflr_name,
852 sizeof(ifra.ifra_name));
853
854 memcpy(&ifra.ifra_addr, &ia->ia_addr,
855 ia->ia_addr.sin_len);
856 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
857 memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
858 ia->ia_dstaddr.sin_len);
859 }
860 memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
861 ia->ia_sockmask.sin_len);
862
863 return in_control(so, SIOCDIFADDR, &ifra, ifp);
864 }
865 }
866 }
867
868 return EOPNOTSUPP; /*just for safety*/
869 }
870
871 /*
872 * Delete any existing route for an interface.
873 */
874 void
875 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
876 {
877
878 in_scrubprefix(ia);
879 }
880
881 /*
882 * Initialize an interface's internet address
883 * and routing table entry.
884 */
885 int
886 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
887 const struct sockaddr_in *sin, int scrub, int hostIsNew)
888 {
889 u_int32_t i;
890 struct sockaddr_in oldaddr;
891 int s = splnet(), flags = RTF_UP, error;
892
893 if (sin == NULL)
894 sin = &ia->ia_addr;
895
896 /*
897 * Set up new addresses.
898 */
899 oldaddr = ia->ia_addr;
900 if (ia->ia_addr.sin_family == AF_INET)
901 LIST_REMOVE(ia, ia_hash);
902 ia->ia_addr = *sin;
903 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
904
905 /* Set IN_IFF flags early for arp_ifinit() */
906 if (hostIsNew && if_do_dad(ifp) && !in_nullhost(ia->ia_addr.sin_addr)) {
907 if (ifp->if_link_state == LINK_STATE_DOWN)
908 ia->ia4_flags |= IN_IFF_DETACHED;
909 else
910 ia->ia4_flags |= IN_IFF_TENTATIVE;
911 }
912
913 /*
914 * Give the interface a chance to initialize
915 * if this is its first address,
916 * and to validate the address if necessary.
917 */
918 if ((error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0)
919 goto bad;
920 splx(s);
921 if (scrub) {
922 ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
923 in_ifscrub(ifp, ia);
924 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
925 }
926
927 /* Add the local route to the address */
928 in_ifaddlocal(&ia->ia_ifa);
929
930 i = ia->ia_addr.sin_addr.s_addr;
931 if (IN_CLASSA(i))
932 ia->ia_netmask = IN_CLASSA_NET;
933 else if (IN_CLASSB(i))
934 ia->ia_netmask = IN_CLASSB_NET;
935 else
936 ia->ia_netmask = IN_CLASSC_NET;
937 /*
938 * The subnet mask usually includes at least the standard network part,
939 * but may may be smaller in the case of supernetting.
940 * If it is set, we believe it.
941 */
942 if (ia->ia_subnetmask == 0) {
943 ia->ia_subnetmask = ia->ia_netmask;
944 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
945 } else
946 ia->ia_netmask &= ia->ia_subnetmask;
947
948 ia->ia_net = i & ia->ia_netmask;
949 ia->ia_subnet = i & ia->ia_subnetmask;
950 in_socktrim(&ia->ia_sockmask);
951 /* re-calculate the "in_maxmtu" value */
952 in_setmaxmtu();
953 /*
954 * Add route for the network.
955 */
956 ia->ia_ifa.ifa_metric = ifp->if_metric;
957 if (ifp->if_flags & IFF_BROADCAST) {
958 ia->ia_broadaddr.sin_addr.s_addr =
959 ia->ia_subnet | ~ia->ia_subnetmask;
960 ia->ia_netbroadcast.s_addr =
961 ia->ia_net | ~ia->ia_netmask;
962 } else if (ifp->if_flags & IFF_LOOPBACK) {
963 ia->ia_dstaddr = ia->ia_addr;
964 flags |= RTF_HOST;
965 } else if (ifp->if_flags & IFF_POINTOPOINT) {
966 if (ia->ia_dstaddr.sin_family != AF_INET)
967 return (0);
968 flags |= RTF_HOST;
969 }
970 error = in_addprefix(ia, flags);
971 /*
972 * If the interface supports multicast, join the "all hosts"
973 * multicast group on that interface.
974 */
975 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
976 struct in_addr addr;
977
978 addr.s_addr = INADDR_ALLHOSTS_GROUP;
979 ia->ia_allhosts = in_addmulti(&addr, ifp);
980 }
981
982 if (hostIsNew && if_do_dad(ifp) &&
983 !in_nullhost(ia->ia_addr.sin_addr) &&
984 ia->ia4_flags & IN_IFF_TENTATIVE)
985 arp_dad_start((struct ifaddr *)ia);
986
987 return (error);
988 bad:
989 splx(s);
990 LIST_REMOVE(ia, ia_hash);
991 ia->ia_addr = oldaddr;
992 if (ia->ia_addr.sin_family == AF_INET)
993 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
994 ia, ia_hash);
995 return (error);
996 }
997
998 #define rtinitflags(x) \
999 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
1000 ? RTF_HOST : 0)
1001
1002 /*
1003 * add a route to prefix ("connected route" in cisco terminology).
1004 * does nothing if there's some interface address with the same prefix already.
1005 */
1006 static int
1007 in_addprefix(struct in_ifaddr *target, int flags)
1008 {
1009 struct in_ifaddr *ia;
1010 struct in_addr prefix, mask, p;
1011 int error;
1012
1013 if ((flags & RTF_HOST) != 0)
1014 prefix = target->ia_dstaddr.sin_addr;
1015 else {
1016 prefix = target->ia_addr.sin_addr;
1017 mask = target->ia_sockmask.sin_addr;
1018 prefix.s_addr &= mask.s_addr;
1019 }
1020
1021 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1022 if (rtinitflags(ia))
1023 p = ia->ia_dstaddr.sin_addr;
1024 else {
1025 p = ia->ia_addr.sin_addr;
1026 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1027 }
1028
1029 if (prefix.s_addr != p.s_addr)
1030 continue;
1031
1032 /*
1033 * if we got a matching prefix route inserted by other
1034 * interface address, we don't need to bother
1035 *
1036 * XXX RADIX_MPATH implications here? -dyoung
1037 */
1038 if (ia->ia_flags & IFA_ROUTE)
1039 return 0;
1040 }
1041
1042 /*
1043 * noone seem to have prefix route. insert it.
1044 */
1045 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1046 if (error == 0)
1047 target->ia_flags |= IFA_ROUTE;
1048 else if (error == EEXIST) {
1049 /*
1050 * the fact the route already exists is not an error.
1051 */
1052 error = 0;
1053 }
1054 return error;
1055 }
1056
1057 /*
1058 * remove a route to prefix ("connected route" in cisco terminology).
1059 * re-installs the route by using another interface address, if there's one
1060 * with the same prefix (otherwise we lose the route mistakenly).
1061 */
1062 static int
1063 in_scrubprefix(struct in_ifaddr *target)
1064 {
1065 struct in_ifaddr *ia;
1066 struct in_addr prefix, mask, p;
1067 int error;
1068
1069 /* If we don't have IFA_ROUTE we should still inform userland */
1070 if ((target->ia_flags & IFA_ROUTE) == 0)
1071 return 0;
1072
1073 if (rtinitflags(target))
1074 prefix = target->ia_dstaddr.sin_addr;
1075 else {
1076 prefix = target->ia_addr.sin_addr;
1077 mask = target->ia_sockmask.sin_addr;
1078 prefix.s_addr &= mask.s_addr;
1079 }
1080
1081 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1082 if (rtinitflags(ia))
1083 p = ia->ia_dstaddr.sin_addr;
1084 else {
1085 p = ia->ia_addr.sin_addr;
1086 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1087 }
1088
1089 if (prefix.s_addr != p.s_addr)
1090 continue;
1091
1092 /*
1093 * if we got a matching prefix route, move IFA_ROUTE to him
1094 */
1095 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1096 rtinit(&target->ia_ifa, RTM_DELETE,
1097 rtinitflags(target));
1098 target->ia_flags &= ~IFA_ROUTE;
1099
1100 error = rtinit(&ia->ia_ifa, RTM_ADD,
1101 rtinitflags(ia) | RTF_UP);
1102 if (error == 0)
1103 ia->ia_flags |= IFA_ROUTE;
1104 return error;
1105 }
1106 }
1107
1108 /*
1109 * noone seem to have prefix route. remove it.
1110 */
1111 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1112 target->ia_flags &= ~IFA_ROUTE;
1113 return 0;
1114 }
1115
1116 #undef rtinitflags
1117
1118 /*
1119 * Return 1 if the address might be a local broadcast address.
1120 */
1121 int
1122 in_broadcast(struct in_addr in, struct ifnet *ifp)
1123 {
1124 struct ifaddr *ifa;
1125
1126 if (in.s_addr == INADDR_BROADCAST ||
1127 in_nullhost(in))
1128 return 1;
1129 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1130 return 0;
1131 /*
1132 * Look through the list of addresses for a match
1133 * with a broadcast address.
1134 */
1135 #define ia (ifatoia(ifa))
1136 IFADDR_FOREACH(ifa, ifp)
1137 if (ifa->ifa_addr->sa_family == AF_INET &&
1138 !in_hosteq(in, ia->ia_addr.sin_addr) &&
1139 (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1140 in_hosteq(in, ia->ia_netbroadcast) ||
1141 (hostzeroisbroadcast &&
1142 /*
1143 * Check for old-style (host 0) broadcast.
1144 */
1145 (in.s_addr == ia->ia_subnet ||
1146 in.s_addr == ia->ia_net))))
1147 return 1;
1148 return (0);
1149 #undef ia
1150 }
1151
1152 /*
1153 * perform DAD when interface becomes IFF_UP.
1154 */
1155 void
1156 in_if_link_up(struct ifnet *ifp)
1157 {
1158 struct ifaddr *ifa;
1159 struct in_ifaddr *ia;
1160
1161 /* Ensure it's sane to run DAD */
1162 if (ifp->if_link_state == LINK_STATE_DOWN)
1163 return;
1164 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
1165 return;
1166
1167 IFADDR_FOREACH(ifa, ifp) {
1168 if (ifa->ifa_addr->sa_family != AF_INET)
1169 continue;
1170 ia = (struct in_ifaddr *)ifa;
1171
1172 /* If detached then mark as tentative */
1173 if (ia->ia4_flags & IN_IFF_DETACHED) {
1174 ia->ia4_flags &= ~IN_IFF_DETACHED;
1175 if (if_do_dad(ifp))
1176 ia->ia4_flags |= IN_IFF_TENTATIVE;
1177 else if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0)
1178 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1179 }
1180
1181 if (ia->ia4_flags & IN_IFF_TENTATIVE) {
1182 /* Clear the duplicated flag as we're starting DAD. */
1183 ia->ia4_flags &= ~IN_IFF_DUPLICATED;
1184 arp_dad_start(ifa);
1185 }
1186 }
1187 }
1188
1189 void
1190 in_if_up(struct ifnet *ifp)
1191 {
1192
1193 /* interface may not support link state, so bring it up also */
1194 in_if_link_up(ifp);
1195 }
1196
1197 /*
1198 * Mark all addresses as detached.
1199 */
1200 void
1201 in_if_link_down(struct ifnet *ifp)
1202 {
1203 struct ifaddr *ifa;
1204 struct in_ifaddr *ia;
1205
1206 IFADDR_FOREACH(ifa, ifp) {
1207 if (ifa->ifa_addr->sa_family != AF_INET)
1208 continue;
1209 ia = (struct in_ifaddr *)ifa;
1210
1211 /* Stop DAD processing */
1212 arp_dad_stop(ifa);
1213
1214 /*
1215 * Mark the address as detached.
1216 */
1217 if (!(ia->ia4_flags & IN_IFF_DETACHED)) {
1218 ia->ia4_flags |= IN_IFF_DETACHED;
1219 ia->ia4_flags &=
1220 ~(IN_IFF_TENTATIVE | IN_IFF_DUPLICATED);
1221 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1222 }
1223 }
1224 }
1225
1226 void
1227 in_if_down(struct ifnet *ifp)
1228 {
1229
1230 in_if_link_down(ifp);
1231 }
1232
1233 void
1234 in_if_link_state_change(struct ifnet *ifp, int link_state)
1235 {
1236
1237 switch (link_state) {
1238 case LINK_STATE_DOWN:
1239 in_if_link_down(ifp);
1240 break;
1241 case LINK_STATE_UP:
1242 in_if_link_up(ifp);
1243 break;
1244 }
1245 }
1246
1247 /*
1248 * in_lookup_multi: look up the in_multi record for a given IP
1249 * multicast address on a given interface. If no matching record is
1250 * found, return NULL.
1251 */
1252 struct in_multi *
1253 in_lookup_multi(struct in_addr addr, ifnet_t *ifp)
1254 {
1255 struct in_multi *inm;
1256
1257 KASSERT(rw_lock_held(&in_multilock));
1258
1259 LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) {
1260 if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp)
1261 break;
1262 }
1263 return inm;
1264 }
1265
1266 /*
1267 * in_multi_group: check whether the address belongs to an IP multicast
1268 * group we are joined on this interface. Returns true or false.
1269 */
1270 bool
1271 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags)
1272 {
1273 bool ingroup;
1274
1275 if (__predict_true(flags & IP_IGMP_MCAST) == 0) {
1276 rw_enter(&in_multilock, RW_READER);
1277 ingroup = in_lookup_multi(addr, ifp) != NULL;
1278 rw_exit(&in_multilock);
1279 } else {
1280 /* XXX Recursive call from ip_output(). */
1281 KASSERT(rw_lock_held(&in_multilock));
1282 ingroup = in_lookup_multi(addr, ifp) != NULL;
1283 }
1284 return ingroup;
1285 }
1286
1287 /*
1288 * Add an address to the list of IP multicast addresses for a given interface.
1289 */
1290 struct in_multi *
1291 in_addmulti(struct in_addr *ap, ifnet_t *ifp)
1292 {
1293 struct sockaddr_in sin;
1294 struct in_multi *inm;
1295
1296 /*
1297 * See if address already in list.
1298 */
1299 rw_enter(&in_multilock, RW_WRITER);
1300 inm = in_lookup_multi(*ap, ifp);
1301 if (inm != NULL) {
1302 /*
1303 * Found it; just increment the reference count.
1304 */
1305 inm->inm_refcount++;
1306 rw_exit(&in_multilock);
1307 return inm;
1308 }
1309
1310 /*
1311 * New address; allocate a new multicast record.
1312 */
1313 inm = pool_get(&inmulti_pool, PR_NOWAIT);
1314 if (inm == NULL) {
1315 rw_exit(&in_multilock);
1316 return NULL;
1317 }
1318 inm->inm_addr = *ap;
1319 inm->inm_ifp = ifp;
1320 inm->inm_refcount = 1;
1321
1322 /*
1323 * Ask the network driver to update its multicast reception
1324 * filter appropriately for the new address.
1325 */
1326 sockaddr_in_init(&sin, ap, 0);
1327 if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
1328 rw_exit(&in_multilock);
1329 pool_put(&inmulti_pool, inm);
1330 return NULL;
1331 }
1332
1333 /*
1334 * Let IGMP know that we have joined a new IP multicast group.
1335 */
1336 if (igmp_joingroup(inm) != 0) {
1337 rw_exit(&in_multilock);
1338 pool_put(&inmulti_pool, inm);
1339 return NULL;
1340 }
1341 LIST_INSERT_HEAD(
1342 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1343 inm, inm_list);
1344 in_multientries++;
1345 rw_exit(&in_multilock);
1346
1347 return inm;
1348 }
1349
1350 /*
1351 * Delete a multicast address record.
1352 */
1353 void
1354 in_delmulti(struct in_multi *inm)
1355 {
1356 struct sockaddr_in sin;
1357
1358 rw_enter(&in_multilock, RW_WRITER);
1359 if (--inm->inm_refcount > 0) {
1360 rw_exit(&in_multilock);
1361 return;
1362 }
1363
1364 /*
1365 * No remaining claims to this record; let IGMP know that
1366 * we are leaving the multicast group.
1367 */
1368 igmp_leavegroup(inm);
1369
1370 /*
1371 * Notify the network driver to update its multicast reception
1372 * filter.
1373 */
1374 sockaddr_in_init(&sin, &inm->inm_addr, 0);
1375 if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
1376
1377 /*
1378 * Unlink from list.
1379 */
1380 LIST_REMOVE(inm, inm_list);
1381 in_multientries--;
1382 rw_exit(&in_multilock);
1383
1384 pool_put(&inmulti_pool, inm);
1385 }
1386
1387 /*
1388 * in_next_multi: step through all of the in_multi records, one at a time.
1389 * The current position is remembered in "step", which the caller must
1390 * provide. in_first_multi(), below, must be called to initialize "step"
1391 * and get the first record. Both macros return a NULL "inm" when there
1392 * are no remaining records.
1393 */
1394 struct in_multi *
1395 in_next_multi(struct in_multistep *step)
1396 {
1397 struct in_multi *inm;
1398
1399 KASSERT(rw_lock_held(&in_multilock));
1400
1401 while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) {
1402 step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]);
1403 }
1404 if ((inm = step->i_inm) != NULL) {
1405 step->i_inm = LIST_NEXT(inm, inm_list);
1406 }
1407 return inm;
1408 }
1409
1410 struct in_multi *
1411 in_first_multi(struct in_multistep *step)
1412 {
1413 KASSERT(rw_lock_held(&in_multilock));
1414
1415 step->i_n = 0;
1416 step->i_inm = LIST_FIRST(&in_multihashtbl[0]);
1417 return in_next_multi(step);
1418 }
1419
1420 void
1421 in_multi_lock(int op)
1422 {
1423 rw_enter(&in_multilock, op);
1424 }
1425
1426 void
1427 in_multi_unlock(void)
1428 {
1429 rw_exit(&in_multilock);
1430 }
1431
1432 int
1433 in_multi_lock_held(void)
1434 {
1435 return rw_lock_held(&in_multilock);
1436 }
1437
1438 struct sockaddr_in *
1439 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
1440 int soopts, struct ip_moptions *mopts, int *errorp)
1441 {
1442 struct rtentry *rt = NULL;
1443 struct in_ifaddr *ia = NULL;
1444
1445 /*
1446 * If route is known or can be allocated now, take the
1447 * source address from the interface. Otherwise, punt.
1448 */
1449 if ((soopts & SO_DONTROUTE) != 0)
1450 rtcache_free(ro);
1451 else {
1452 union {
1453 struct sockaddr dst;
1454 struct sockaddr_in dst4;
1455 } u;
1456
1457 sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
1458 rt = rtcache_lookup(ro, &u.dst);
1459 }
1460 /*
1461 * If we found a route, use the address
1462 * corresponding to the outgoing interface
1463 * unless it is the loopback (in case a route
1464 * to our address on another net goes to loopback).
1465 *
1466 * XXX Is this still true? Do we care?
1467 */
1468 if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
1469 ia = ifatoia(rt->rt_ifa);
1470 if (ia == NULL) {
1471 u_int16_t fport = sin->sin_port;
1472
1473 sin->sin_port = 0;
1474 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
1475 sin->sin_port = fport;
1476 if (ia == NULL) {
1477 /* Find 1st non-loopback AF_INET address */
1478 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1479 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
1480 break;
1481 }
1482 }
1483 if (ia == NULL) {
1484 *errorp = EADDRNOTAVAIL;
1485 return NULL;
1486 }
1487 }
1488 /*
1489 * If the destination address is multicast and an outgoing
1490 * interface has been set as a multicast option, use the
1491 * address of that interface as our source address.
1492 */
1493 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
1494 struct ip_moptions *imo;
1495 struct ifnet *ifp;
1496
1497 imo = mopts;
1498 if (imo->imo_multicast_ifp != NULL) {
1499 ifp = imo->imo_multicast_ifp;
1500 IFP_TO_IA(ifp, ia); /* XXX */
1501 if (ia == 0 || ia->ia4_flags & IN_IFF_NOTREADY) {
1502 *errorp = EADDRNOTAVAIL;
1503 return NULL;
1504 }
1505 }
1506 }
1507 if (ia->ia_ifa.ifa_getifa != NULL) {
1508 ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
1509 sintosa(sin)));
1510 if (ia == NULL) {
1511 *errorp = EADDRNOTAVAIL;
1512 return NULL;
1513 }
1514 }
1515 #ifdef GETIFA_DEBUG
1516 else
1517 printf("%s: missing ifa_getifa\n", __func__);
1518 #endif
1519 return satosin(&ia->ia_addr);
1520 }
1521
1522 static void
1523 in_sysctl_init(struct sysctllog **clog)
1524 {
1525 sysctl_createv(clog, 0, NULL, NULL,
1526 CTLFLAG_PERMANENT,
1527 CTLTYPE_NODE, "inet",
1528 SYSCTL_DESCR("PF_INET related settings"),
1529 NULL, 0, NULL, 0,
1530 CTL_NET, PF_INET, CTL_EOL);
1531 sysctl_createv(clog, 0, NULL, NULL,
1532 CTLFLAG_PERMANENT,
1533 CTLTYPE_NODE, "ip",
1534 SYSCTL_DESCR("IPv4 related settings"),
1535 NULL, 0, NULL, 0,
1536 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
1537
1538 sysctl_createv(clog, 0, NULL, NULL,
1539 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1540 CTLTYPE_INT, "subnetsarelocal",
1541 SYSCTL_DESCR("Whether logical subnets are considered "
1542 "local"),
1543 NULL, 0, &subnetsarelocal, 0,
1544 CTL_NET, PF_INET, IPPROTO_IP,
1545 IPCTL_SUBNETSARELOCAL, CTL_EOL);
1546 sysctl_createv(clog, 0, NULL, NULL,
1547 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1548 CTLTYPE_INT, "hostzerobroadcast",
1549 SYSCTL_DESCR("All zeroes address is broadcast address"),
1550 NULL, 0, &hostzeroisbroadcast, 0,
1551 CTL_NET, PF_INET, IPPROTO_IP,
1552 IPCTL_HOSTZEROBROADCAST, CTL_EOL);
1553 }
1554