in.c revision 1.128 1 /* $NetBSD: in.c,v 1.128 2008/11/07 00:20:18 dyoung 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.128 2008/11/07 00:20:18 dyoung Exp $");
95
96 #include "opt_inet.h"
97 #include "opt_inet_conf.h"
98 #include "opt_mrouting.h"
99 #include "opt_pfil_hooks.h"
100
101 #include <sys/param.h>
102 #include <sys/ioctl.h>
103 #include <sys/errno.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 <net/if.h>
114 #include <net/route.h>
115
116 #include <net/if_ether.h>
117
118 #include <netinet/in_systm.h>
119 #include <netinet/in.h>
120 #include <netinet/in_var.h>
121 #include <netinet/ip.h>
122 #include <netinet/ip_var.h>
123 #include <netinet/in_ifattach.h>
124 #include <netinet/in_pcb.h>
125 #include <netinet/if_inarp.h>
126 #include <netinet/ip_mroute.h>
127 #include <netinet/igmp_var.h>
128
129 #ifdef IPSELSRC
130 #include <netinet/in_selsrc.h>
131 #endif
132
133 #ifdef PFIL_HOOKS
134 #include <net/pfil.h>
135 #endif
136
137 static u_int in_mask2len(struct in_addr *);
138 static void in_len2mask(struct in_addr *, u_int);
139 static int in_lifaddr_ioctl(struct socket *, u_long, void *,
140 struct ifnet *, struct lwp *);
141
142 static int in_ifaddrpref_ioctl(struct socket *, u_long, void *,
143 struct ifnet *);
144 static int in_addprefix(struct in_ifaddr *, int);
145 static int in_scrubprefix(struct in_ifaddr *);
146
147 #ifndef SUBNETSARELOCAL
148 #define SUBNETSARELOCAL 1
149 #endif
150
151 #ifndef HOSTZEROBROADCAST
152 #define HOSTZEROBROADCAST 1
153 #endif
154
155 int subnetsarelocal = SUBNETSARELOCAL;
156 int hostzeroisbroadcast = HOSTZEROBROADCAST;
157
158 /*
159 * This list is used to keep track of in_multi chains which belong to
160 * deleted interface addresses. We use in_ifaddr so that a chain head
161 * won't be deallocated until all multicast address record are deleted.
162 */
163 static TAILQ_HEAD(, in_ifaddr) in_mk = TAILQ_HEAD_INITIALIZER(in_mk);
164
165 /*
166 * Return 1 if an internet address is for a ``local'' host
167 * (one to which we have a connection). If subnetsarelocal
168 * is true, this includes other subnets of the local net.
169 * Otherwise, it includes only the directly-connected (sub)nets.
170 */
171 int
172 in_localaddr(struct in_addr in)
173 {
174 struct in_ifaddr *ia;
175
176 if (subnetsarelocal) {
177 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
178 if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
179 return (1);
180 } else {
181 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
182 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
183 return (1);
184 }
185 return (0);
186 }
187
188 /*
189 * Determine whether an IP address is in a reserved set of addresses
190 * that may not be forwarded, or whether datagrams to that destination
191 * may be forwarded.
192 */
193 int
194 in_canforward(struct in_addr in)
195 {
196 u_int32_t net;
197
198 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
199 return (0);
200 if (IN_CLASSA(in.s_addr)) {
201 net = in.s_addr & IN_CLASSA_NET;
202 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
203 return (0);
204 }
205 return (1);
206 }
207
208 /*
209 * Trim a mask in a sockaddr
210 */
211 void
212 in_socktrim(struct sockaddr_in *ap)
213 {
214 char *cplim = (char *) &ap->sin_addr;
215 char *cp = (char *) (&ap->sin_addr + 1);
216
217 ap->sin_len = 0;
218 while (--cp >= cplim)
219 if (*cp) {
220 (ap)->sin_len = cp - (char *) (ap) + 1;
221 break;
222 }
223 }
224
225 /*
226 * Routine to take an Internet address and convert into a
227 * "dotted quad" representation for printing.
228 */
229 const char *
230 in_fmtaddr(struct in_addr addr)
231 {
232 static char buf[sizeof("123.456.789.123")];
233
234 addr.s_addr = ntohl(addr.s_addr);
235
236 snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
237 (addr.s_addr >> 24) & 0xFF,
238 (addr.s_addr >> 16) & 0xFF,
239 (addr.s_addr >> 8) & 0xFF,
240 (addr.s_addr >> 0) & 0xFF);
241 return buf;
242 }
243
244 /*
245 * Maintain the "in_maxmtu" variable, which is the largest
246 * mtu for non-local interfaces with AF_INET addresses assigned
247 * to them that are up.
248 */
249 unsigned long in_maxmtu;
250
251 void
252 in_setmaxmtu(void)
253 {
254 struct in_ifaddr *ia;
255 struct ifnet *ifp;
256 unsigned long maxmtu = 0;
257
258 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
259 if ((ifp = ia->ia_ifp) == 0)
260 continue;
261 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
262 continue;
263 if (ifp->if_mtu > maxmtu)
264 maxmtu = ifp->if_mtu;
265 }
266 if (maxmtu)
267 in_maxmtu = maxmtu;
268 }
269
270 static u_int
271 in_mask2len(struct in_addr *mask)
272 {
273 u_int x, y;
274 u_char *p;
275
276 p = (u_char *)mask;
277 for (x = 0; x < sizeof(*mask); x++) {
278 if (p[x] != 0xff)
279 break;
280 }
281 y = 0;
282 if (x < sizeof(*mask)) {
283 for (y = 0; y < NBBY; y++) {
284 if ((p[x] & (0x80 >> y)) == 0)
285 break;
286 }
287 }
288 return x * NBBY + y;
289 }
290
291 static void
292 in_len2mask(struct in_addr *mask, u_int len)
293 {
294 u_int i;
295 u_char *p;
296
297 p = (u_char *)mask;
298 bzero(mask, sizeof(*mask));
299 for (i = 0; i < len / NBBY; i++)
300 p[i] = 0xff;
301 if (len % NBBY)
302 p[i] = (0xff00 >> (len % NBBY)) & 0xff;
303 }
304
305 /*
306 * Generic internet control operations (ioctl's).
307 * Ifp is 0 if not an interface-specific ioctl.
308 */
309 /* ARGSUSED */
310 int
311 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
312 struct lwp *l)
313 {
314 struct ifreq *ifr = (struct ifreq *)data;
315 struct in_ifaddr *ia = 0;
316 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
317 struct sockaddr_in oldaddr;
318 int error, hostIsNew, maskIsNew;
319 int newifaddr = 0;
320
321 switch (cmd) {
322 case SIOCALIFADDR:
323 case SIOCDLIFADDR:
324 case SIOCSIFADDRPREF:
325 if (l == NULL)
326 return (EPERM);
327 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
328 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
329 NULL) != 0)
330 return (EPERM);
331 /*FALLTHROUGH*/
332 case SIOCGIFADDRPREF:
333 case SIOCGLIFADDR:
334 if (ifp == NULL)
335 return EINVAL;
336 if (cmd == SIOCGIFADDRPREF || cmd == SIOCSIFADDRPREF)
337 return in_ifaddrpref_ioctl(so, cmd, data, ifp);
338 else
339 return in_lifaddr_ioctl(so, cmd, data, ifp, l);
340 }
341
342 /*
343 * Find address for this interface, if it exists.
344 */
345 if (ifp != NULL)
346 IFP_TO_IA(ifp, ia);
347
348 switch (cmd) {
349
350 case SIOCAIFADDR:
351 case SIOCDIFADDR:
352 case SIOCGIFALIAS:
353 if (ifra->ifra_addr.sin_family == AF_INET)
354 LIST_FOREACH(ia,
355 &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
356 ia_hash) {
357 if (ia->ia_ifp == ifp &&
358 in_hosteq(ia->ia_addr.sin_addr,
359 ifra->ifra_addr.sin_addr))
360 break;
361 }
362 if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL)
363 return (EADDRNOTAVAIL);
364
365 #if 1 /*def COMPAT_43*/
366 if (cmd == SIOCDIFADDR &&
367 ifra->ifra_addr.sin_family == AF_UNSPEC) {
368 ifra->ifra_addr.sin_family = AF_INET;
369 }
370 #endif
371 /* FALLTHROUGH */
372 case SIOCSIFADDR:
373 case SIOCSIFDSTADDR:
374 if (ifra->ifra_addr.sin_family != AF_INET)
375 return (EAFNOSUPPORT);
376 /* FALLTHROUGH */
377 case SIOCSIFNETMASK:
378 if (ifp == NULL)
379 panic("in_control");
380
381 if (cmd == SIOCGIFALIAS)
382 break;
383
384 if (ia == NULL &&
385 (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
386 return (EADDRNOTAVAIL);
387
388 if (l == NULL)
389 return (EPERM);
390 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
391 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
392 NULL) != 0)
393 return (EPERM);
394
395 if (ia == 0) {
396 MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
397 M_IFADDR, M_WAITOK);
398 if (ia == 0)
399 return (ENOBUFS);
400 bzero((void *)ia, sizeof *ia);
401 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
402 IFAREF(&ia->ia_ifa);
403 ifa_insert(ifp, &ia->ia_ifa);
404 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
405 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
406 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
407 #ifdef IPSELSRC
408 ia->ia_ifa.ifa_getifa = in_getifa;
409 #else /* IPSELSRC */
410 ia->ia_ifa.ifa_getifa = NULL;
411 #endif /* IPSELSRC */
412 ia->ia_sockmask.sin_len = 8;
413 if (ifp->if_flags & IFF_BROADCAST) {
414 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
415 ia->ia_broadaddr.sin_family = AF_INET;
416 }
417 ia->ia_ifp = ifp;
418 ia->ia_idsalt = arc4random() % 65535;
419 LIST_INIT(&ia->ia_multiaddrs);
420 newifaddr = 1;
421 }
422 break;
423
424 case SIOCSIFBRDADDR:
425 if (l == NULL)
426 return (EPERM);
427 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
428 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
429 NULL) != 0)
430 return (EPERM);
431 /* FALLTHROUGH */
432
433 case SIOCGIFADDR:
434 case SIOCGIFNETMASK:
435 case SIOCGIFDSTADDR:
436 case SIOCGIFBRDADDR:
437 if (ia == 0)
438 return (EADDRNOTAVAIL);
439 break;
440 }
441 error = 0;
442 switch (cmd) {
443
444 case SIOCGIFADDR:
445 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
446 break;
447
448 case SIOCGIFBRDADDR:
449 if ((ifp->if_flags & IFF_BROADCAST) == 0)
450 return (EINVAL);
451 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
452 break;
453
454 case SIOCGIFDSTADDR:
455 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
456 return (EINVAL);
457 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
458 break;
459
460 case SIOCGIFNETMASK:
461 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_sockmask));
462 break;
463
464 case SIOCSIFDSTADDR:
465 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
466 return (EINVAL);
467 oldaddr = ia->ia_dstaddr;
468 ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
469 if ((error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ia)) != 0) {
470 ia->ia_dstaddr = oldaddr;
471 return error;
472 }
473 if (ia->ia_flags & IFA_ROUTE) {
474 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
475 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
476 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
477 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
478 }
479 break;
480
481 case SIOCSIFBRDADDR:
482 if ((ifp->if_flags & IFF_BROADCAST) == 0)
483 return EINVAL;
484 ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
485 break;
486
487 case SIOCSIFADDR:
488 error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
489 1);
490 #ifdef PFIL_HOOKS
491 if (error == 0)
492 (void)pfil_run_hooks(&if_pfil,
493 (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
494 #endif
495 break;
496
497 case SIOCSIFNETMASK:
498 in_ifscrub(ifp, ia);
499 ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
500 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
501 error = in_ifinit(ifp, ia, NULL, 0);
502 break;
503
504 case SIOCAIFADDR:
505 maskIsNew = 0;
506 hostIsNew = 1;
507 if (ia->ia_addr.sin_family != AF_INET)
508 ;
509 else if (ifra->ifra_addr.sin_len == 0) {
510 ifra->ifra_addr = ia->ia_addr;
511 hostIsNew = 0;
512 } else if (in_hosteq(ia->ia_addr.sin_addr,
513 ifra->ifra_addr.sin_addr))
514 hostIsNew = 0;
515 if (ifra->ifra_mask.sin_len) {
516 in_ifscrub(ifp, ia);
517 ia->ia_sockmask = ifra->ifra_mask;
518 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
519 maskIsNew = 1;
520 }
521 if ((ifp->if_flags & IFF_POINTOPOINT) &&
522 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
523 in_ifscrub(ifp, ia);
524 ia->ia_dstaddr = ifra->ifra_dstaddr;
525 maskIsNew = 1; /* We lie; but the effect's the same */
526 }
527 if (ifra->ifra_addr.sin_family == AF_INET &&
528 (hostIsNew || maskIsNew)) {
529 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
530 }
531 if ((ifp->if_flags & IFF_BROADCAST) &&
532 (ifra->ifra_broadaddr.sin_family == AF_INET))
533 ia->ia_broadaddr = ifra->ifra_broadaddr;
534 #ifdef PFIL_HOOKS
535 if (error == 0)
536 (void)pfil_run_hooks(&if_pfil,
537 (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
538 #endif
539 break;
540
541 case SIOCGIFALIAS:
542 ifra->ifra_mask = ia->ia_sockmask;
543 if ((ifp->if_flags & IFF_POINTOPOINT) &&
544 (ia->ia_dstaddr.sin_family == AF_INET))
545 ifra->ifra_dstaddr = ia->ia_dstaddr;
546 else if ((ifp->if_flags & IFF_BROADCAST) &&
547 (ia->ia_broadaddr.sin_family == AF_INET))
548 ifra->ifra_broadaddr = ia->ia_broadaddr;
549 else
550 memset(&ifra->ifra_broadaddr, 0,
551 sizeof(ifra->ifra_broadaddr));
552 break;
553
554 case SIOCDIFADDR:
555 in_purgeaddr(&ia->ia_ifa);
556 #ifdef PFIL_HOOKS
557 (void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR,
558 ifp, PFIL_IFADDR);
559 #endif
560 break;
561
562 #ifdef MROUTING
563 case SIOCGETVIFCNT:
564 case SIOCGETSGCNT:
565 error = mrt_ioctl(so, cmd, data);
566 break;
567 #endif /* MROUTING */
568
569 default:
570 return ENOTTY;
571 }
572
573 if (error != 0 && newifaddr) {
574 KASSERT(ia != NULL);
575 in_purgeaddr(&ia->ia_ifa);
576 }
577
578 return error;
579 }
580
581 void
582 in_purgeaddr(struct ifaddr *ifa)
583 {
584 struct ifnet *ifp = ifa->ifa_ifp;
585 struct in_ifaddr *ia = (void *) ifa;
586
587 in_ifscrub(ifp, ia);
588 LIST_REMOVE(ia, ia_hash);
589 ifa_remove(ifp, &ia->ia_ifa);
590 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
591 if (ia->ia_allhosts != NULL)
592 in_delmulti(ia->ia_allhosts);
593 IFAFREE(&ia->ia_ifa);
594 in_setmaxmtu();
595 }
596
597 void
598 in_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */
599 {
600 if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
601 igmp_purgeif(ifp); /* manipulates pools */
602 #ifdef MROUTING
603 ip_mrouter_detach(ifp);
604 #endif
605 }
606
607 /*
608 * SIOC[GAD]LIFADDR.
609 * SIOCGLIFADDR: get first address. (???)
610 * SIOCGLIFADDR with IFLR_PREFIX:
611 * get first address that matches the specified prefix.
612 * SIOCALIFADDR: add the specified address.
613 * SIOCALIFADDR with IFLR_PREFIX:
614 * EINVAL since we can't deduce hostid part of the address.
615 * SIOCDLIFADDR: delete the specified address.
616 * SIOCDLIFADDR with IFLR_PREFIX:
617 * delete the first address that matches the specified prefix.
618 * return values:
619 * EINVAL on invalid parameters
620 * EADDRNOTAVAIL on prefix match failed/specified address not found
621 * other values may be returned from in_ioctl()
622 */
623 static int
624 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
625 struct ifnet *ifp, struct lwp *l)
626 {
627 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
628 struct ifaddr *ifa;
629 struct sockaddr *sa;
630
631 /* sanity checks */
632 if (data == NULL || ifp == NULL) {
633 panic("invalid argument to in_lifaddr_ioctl");
634 /*NOTRECHED*/
635 }
636
637 switch (cmd) {
638 case SIOCGLIFADDR:
639 /* address must be specified on GET with IFLR_PREFIX */
640 if ((iflr->flags & IFLR_PREFIX) == 0)
641 break;
642 /*FALLTHROUGH*/
643 case SIOCALIFADDR:
644 case SIOCDLIFADDR:
645 /* address must be specified on ADD and DELETE */
646 sa = (struct sockaddr *)&iflr->addr;
647 if (sa->sa_family != AF_INET)
648 return EINVAL;
649 if (sa->sa_len != sizeof(struct sockaddr_in))
650 return EINVAL;
651 /* XXX need improvement */
652 sa = (struct sockaddr *)&iflr->dstaddr;
653 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
654 return EINVAL;
655 if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
656 return EINVAL;
657 break;
658 default: /*shouldn't happen*/
659 #if 0
660 panic("invalid cmd to in_lifaddr_ioctl");
661 /*NOTREACHED*/
662 #else
663 return EOPNOTSUPP;
664 #endif
665 }
666 if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
667 return EINVAL;
668
669 switch (cmd) {
670 case SIOCALIFADDR:
671 {
672 struct in_aliasreq ifra;
673
674 if (iflr->flags & IFLR_PREFIX)
675 return EINVAL;
676
677 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
678 bzero(&ifra, sizeof(ifra));
679 bcopy(iflr->iflr_name, ifra.ifra_name,
680 sizeof(ifra.ifra_name));
681
682 bcopy(&iflr->addr, &ifra.ifra_addr,
683 ((struct sockaddr *)&iflr->addr)->sa_len);
684
685 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
686 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
687 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
688 }
689
690 ifra.ifra_mask.sin_family = AF_INET;
691 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
692 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
693
694 return in_control(so, SIOCAIFADDR, (void *)&ifra, ifp, l);
695 }
696 case SIOCGLIFADDR:
697 case SIOCDLIFADDR:
698 {
699 struct in_ifaddr *ia;
700 struct in_addr mask, candidate, match;
701 struct sockaddr_in *sin;
702 int cmp;
703
704 bzero(&mask, sizeof(mask));
705 bzero(&match, sizeof(match)); /* XXX gcc */
706 if (iflr->flags & IFLR_PREFIX) {
707 /* lookup a prefix rather than address. */
708 in_len2mask(&mask, iflr->prefixlen);
709
710 sin = (struct sockaddr_in *)&iflr->addr;
711 match.s_addr = sin->sin_addr.s_addr;
712 match.s_addr &= mask.s_addr;
713
714 /* if you set extra bits, that's wrong */
715 if (match.s_addr != sin->sin_addr.s_addr)
716 return EINVAL;
717
718 cmp = 1;
719 } else {
720 if (cmd == SIOCGLIFADDR) {
721 /* on getting an address, take the 1st match */
722 cmp = 0; /*XXX*/
723 } else {
724 /* on deleting an address, do exact match */
725 in_len2mask(&mask, 32);
726 sin = (struct sockaddr_in *)&iflr->addr;
727 match.s_addr = sin->sin_addr.s_addr;
728
729 cmp = 1;
730 }
731 }
732
733 IFADDR_FOREACH(ifa, ifp) {
734 if (ifa->ifa_addr->sa_family != AF_INET)
735 continue;
736 if (cmp == 0)
737 break;
738 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
739 candidate.s_addr &= mask.s_addr;
740 if (candidate.s_addr == match.s_addr)
741 break;
742 }
743 if (ifa == NULL)
744 return EADDRNOTAVAIL;
745 ia = (struct in_ifaddr *)ifa;
746
747 if (cmd == SIOCGLIFADDR) {
748 /* fill in the if_laddrreq structure */
749 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
750
751 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
752 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
753 ia->ia_dstaddr.sin_len);
754 } else
755 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
756
757 iflr->prefixlen =
758 in_mask2len(&ia->ia_sockmask.sin_addr);
759
760 iflr->flags = 0; /*XXX*/
761
762 return 0;
763 } else {
764 struct in_aliasreq ifra;
765
766 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
767 bzero(&ifra, sizeof(ifra));
768 bcopy(iflr->iflr_name, ifra.ifra_name,
769 sizeof(ifra.ifra_name));
770
771 bcopy(&ia->ia_addr, &ifra.ifra_addr,
772 ia->ia_addr.sin_len);
773 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
774 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
775 ia->ia_dstaddr.sin_len);
776 }
777 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
778 ia->ia_sockmask.sin_len);
779
780 return in_control(so, SIOCDIFADDR, (void *)&ifra,
781 ifp, l);
782 }
783 }
784 }
785
786 return EOPNOTSUPP; /*just for safety*/
787 }
788
789 static int
790 in_ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data,
791 struct ifnet *ifp)
792 {
793 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
794 struct ifaddr *ifa;
795 struct sockaddr *sa;
796 struct in_ifaddr *ia = NULL; /* appease gcc -Wuninitialized */
797 struct in_addr match;
798 struct sockaddr_in *sin;
799
800 /* sanity checks */
801 if (data == NULL || ifp == NULL) {
802 panic("invalid argument to %s", __func__);
803 /*NOTREACHED*/
804 }
805
806 /* address must be specified on ADD and DELETE */
807 sa = (struct sockaddr *)&ifap->ifap_addr;
808 if (sa->sa_family != AF_INET)
809 return EINVAL;
810 if (sa->sa_len != sizeof(struct sockaddr_in))
811 return EINVAL;
812
813 switch (cmd) {
814 case SIOCSIFADDRPREF:
815 case SIOCGIFADDRPREF:
816 break;
817 default:
818 return EOPNOTSUPP;
819 }
820
821 sin = (struct sockaddr_in *)&ifap->ifap_addr;
822 match.s_addr = sin->sin_addr.s_addr;
823
824 IFADDR_FOREACH(ifa, ifp) {
825 ia = (struct in_ifaddr *)ifa;
826 if (ia->ia_addr.sin_family != AF_INET)
827 continue;
828 if (ia->ia_addr.sin_addr.s_addr == match.s_addr)
829 break;
830 }
831 if (ifa == NULL)
832 return EADDRNOTAVAIL;
833
834 switch (cmd) {
835 case SIOCSIFADDRPREF:
836 ifa->ifa_preference = ifap->ifap_preference;
837 return 0;
838 case SIOCGIFADDRPREF:
839 /* fill in the if_laddrreq structure */
840 (void)memcpy(&ifap->ifap_addr, &ia->ia_addr,
841 ia->ia_addr.sin_len);
842 ifap->ifap_preference = ifa->ifa_preference;
843 return 0;
844 default:
845 return EOPNOTSUPP;
846 }
847 }
848
849 /*
850 * Delete any existing route for an interface.
851 */
852 void
853 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
854 {
855
856 in_scrubprefix(ia);
857 }
858
859 /*
860 * Initialize an interface's internet address
861 * and routing table entry.
862 */
863 int
864 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
865 const struct sockaddr_in *sin, int scrub)
866 {
867 u_int32_t i;
868 struct sockaddr_in oldaddr;
869 int s = splnet(), flags = RTF_UP, error;
870
871 if (sin == NULL)
872 sin = &ia->ia_addr;
873
874 /*
875 * Set up new addresses.
876 */
877 oldaddr = ia->ia_addr;
878 if (ia->ia_addr.sin_family == AF_INET)
879 LIST_REMOVE(ia, ia_hash);
880 ia->ia_addr = *sin;
881 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
882
883 /*
884 * Give the interface a chance to initialize
885 * if this is its first address,
886 * and to validate the address if necessary.
887 */
888 if ((error = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ia)) != 0)
889 goto bad;
890 splx(s);
891 if (scrub) {
892 ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
893 in_ifscrub(ifp, ia);
894 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
895 }
896
897 i = ia->ia_addr.sin_addr.s_addr;
898 if (IN_CLASSA(i))
899 ia->ia_netmask = IN_CLASSA_NET;
900 else if (IN_CLASSB(i))
901 ia->ia_netmask = IN_CLASSB_NET;
902 else
903 ia->ia_netmask = IN_CLASSC_NET;
904 /*
905 * The subnet mask usually includes at least the standard network part,
906 * but may may be smaller in the case of supernetting.
907 * If it is set, we believe it.
908 */
909 if (ia->ia_subnetmask == 0) {
910 ia->ia_subnetmask = ia->ia_netmask;
911 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
912 } else
913 ia->ia_netmask &= ia->ia_subnetmask;
914
915 ia->ia_net = i & ia->ia_netmask;
916 ia->ia_subnet = i & ia->ia_subnetmask;
917 in_socktrim(&ia->ia_sockmask);
918 /* re-calculate the "in_maxmtu" value */
919 in_setmaxmtu();
920 /*
921 * Add route for the network.
922 */
923 ia->ia_ifa.ifa_metric = ifp->if_metric;
924 if (ifp->if_flags & IFF_BROADCAST) {
925 ia->ia_broadaddr.sin_addr.s_addr =
926 ia->ia_subnet | ~ia->ia_subnetmask;
927 ia->ia_netbroadcast.s_addr =
928 ia->ia_net | ~ia->ia_netmask;
929 } else if (ifp->if_flags & IFF_LOOPBACK) {
930 ia->ia_dstaddr = ia->ia_addr;
931 flags |= RTF_HOST;
932 } else if (ifp->if_flags & IFF_POINTOPOINT) {
933 if (ia->ia_dstaddr.sin_family != AF_INET)
934 return (0);
935 flags |= RTF_HOST;
936 }
937 error = in_addprefix(ia, flags);
938 /*
939 * If the interface supports multicast, join the "all hosts"
940 * multicast group on that interface.
941 */
942 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
943 struct in_addr addr;
944
945 addr.s_addr = INADDR_ALLHOSTS_GROUP;
946 ia->ia_allhosts = in_addmulti(&addr, ifp);
947 }
948 return (error);
949 bad:
950 splx(s);
951 LIST_REMOVE(ia, ia_hash);
952 ia->ia_addr = oldaddr;
953 if (ia->ia_addr.sin_family == AF_INET)
954 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
955 ia, ia_hash);
956 return (error);
957 }
958
959 #define rtinitflags(x) \
960 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
961 ? RTF_HOST : 0)
962
963 /*
964 * add a route to prefix ("connected route" in cisco terminology).
965 * does nothing if there's some interface address with the same prefix already.
966 */
967 static int
968 in_addprefix(struct in_ifaddr *target, int flags)
969 {
970 struct in_ifaddr *ia;
971 struct in_addr prefix, mask, p;
972 int error;
973
974 if ((flags & RTF_HOST) != 0)
975 prefix = target->ia_dstaddr.sin_addr;
976 else {
977 prefix = target->ia_addr.sin_addr;
978 mask = target->ia_sockmask.sin_addr;
979 prefix.s_addr &= mask.s_addr;
980 }
981
982 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
983 if (rtinitflags(ia))
984 p = ia->ia_dstaddr.sin_addr;
985 else {
986 p = ia->ia_addr.sin_addr;
987 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
988 }
989
990 if (prefix.s_addr != p.s_addr)
991 continue;
992
993 /*
994 * if we got a matching prefix route inserted by other
995 * interface address, we don't need to bother
996 *
997 * XXX RADIX_MPATH implications here? -dyoung
998 */
999 if (ia->ia_flags & IFA_ROUTE)
1000 return 0;
1001 }
1002
1003 /*
1004 * noone seem to have prefix route. insert it.
1005 */
1006 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1007 if (error == 0)
1008 target->ia_flags |= IFA_ROUTE;
1009 return error;
1010 }
1011
1012 /*
1013 * remove a route to prefix ("connected route" in cisco terminology).
1014 * re-installs the route by using another interface address, if there's one
1015 * with the same prefix (otherwise we lose the route mistakenly).
1016 */
1017 static int
1018 in_scrubprefix(struct in_ifaddr *target)
1019 {
1020 struct in_ifaddr *ia;
1021 struct in_addr prefix, mask, p;
1022 int error;
1023
1024 if ((target->ia_flags & IFA_ROUTE) == 0)
1025 return 0;
1026
1027 if (rtinitflags(target))
1028 prefix = target->ia_dstaddr.sin_addr;
1029 else {
1030 prefix = target->ia_addr.sin_addr;
1031 mask = target->ia_sockmask.sin_addr;
1032 prefix.s_addr &= mask.s_addr;
1033 }
1034
1035 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1036 if (rtinitflags(ia))
1037 p = ia->ia_dstaddr.sin_addr;
1038 else {
1039 p = ia->ia_addr.sin_addr;
1040 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1041 }
1042
1043 if (prefix.s_addr != p.s_addr)
1044 continue;
1045
1046 /*
1047 * if we got a matching prefix route, move IFA_ROUTE to him
1048 */
1049 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1050 rtinit(&target->ia_ifa, RTM_DELETE,
1051 rtinitflags(target));
1052 target->ia_flags &= ~IFA_ROUTE;
1053
1054 error = rtinit(&ia->ia_ifa, RTM_ADD,
1055 rtinitflags(ia) | RTF_UP);
1056 if (error == 0)
1057 ia->ia_flags |= IFA_ROUTE;
1058 return error;
1059 }
1060 }
1061
1062 /*
1063 * noone seem to have prefix route. remove it.
1064 */
1065 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1066 target->ia_flags &= ~IFA_ROUTE;
1067 return 0;
1068 }
1069
1070 #undef rtinitflags
1071
1072 /*
1073 * Return 1 if the address might be a local broadcast address.
1074 */
1075 int
1076 in_broadcast(struct in_addr in, struct ifnet *ifp)
1077 {
1078 struct ifaddr *ifa;
1079
1080 if (in.s_addr == INADDR_BROADCAST ||
1081 in_nullhost(in))
1082 return 1;
1083 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1084 return 0;
1085 /*
1086 * Look through the list of addresses for a match
1087 * with a broadcast address.
1088 */
1089 #define ia (ifatoia(ifa))
1090 IFADDR_FOREACH(ifa, ifp)
1091 if (ifa->ifa_addr->sa_family == AF_INET &&
1092 !in_hosteq(in, ia->ia_addr.sin_addr) &&
1093 (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1094 in_hosteq(in, ia->ia_netbroadcast) ||
1095 (hostzeroisbroadcast &&
1096 /*
1097 * Check for old-style (host 0) broadcast.
1098 */
1099 (in.s_addr == ia->ia_subnet ||
1100 in.s_addr == ia->ia_net))))
1101 return 1;
1102 return (0);
1103 #undef ia
1104 }
1105
1106 /*
1107 * Add an address to the list of IP multicast addresses for a given interface.
1108 */
1109 struct in_multi *
1110 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1111 {
1112 struct sockaddr_in sin;
1113 struct in_multi *inm;
1114 struct ifreq ifr;
1115 int s = splsoftnet();
1116
1117 /*
1118 * See if address already in list.
1119 */
1120 IN_LOOKUP_MULTI(*ap, ifp, inm);
1121 if (inm != NULL) {
1122 /*
1123 * Found it; just increment the reference count.
1124 */
1125 ++inm->inm_refcount;
1126 } else {
1127 /*
1128 * New address; allocate a new multicast record
1129 * and link it into the interface's multicast list.
1130 */
1131 inm = pool_get(&inmulti_pool, PR_NOWAIT);
1132 if (inm == NULL) {
1133 splx(s);
1134 return (NULL);
1135 }
1136 inm->inm_addr = *ap;
1137 inm->inm_ifp = ifp;
1138 inm->inm_refcount = 1;
1139 LIST_INSERT_HEAD(
1140 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1141 inm, inm_list);
1142 /*
1143 * Ask the network driver to update its multicast reception
1144 * filter appropriately for the new address.
1145 */
1146 sockaddr_in_init(&sin, ap, 0);
1147 ifreq_setaddr(SIOCADDMULTI, &ifr, sintosa(&sin));
1148 if ((*ifp->if_ioctl)(ifp, SIOCADDMULTI, &ifr) != 0) {
1149 LIST_REMOVE(inm, inm_list);
1150 pool_put(&inmulti_pool, inm);
1151 splx(s);
1152 return (NULL);
1153 }
1154 /*
1155 * Let IGMP know that we have joined a new IP multicast group.
1156 */
1157 if (igmp_joingroup(inm) != 0) {
1158 LIST_REMOVE(inm, inm_list);
1159 pool_put(&inmulti_pool, inm);
1160 splx(s);
1161 return (NULL);
1162 }
1163 in_multientries++;
1164 }
1165 splx(s);
1166 return (inm);
1167 }
1168
1169 /*
1170 * Delete a multicast address record.
1171 */
1172 void
1173 in_delmulti(struct in_multi *inm)
1174 {
1175 struct sockaddr_in sin;
1176 struct ifreq ifr;
1177 int s = splsoftnet();
1178
1179 if (--inm->inm_refcount == 0) {
1180 /*
1181 * No remaining claims to this record; let IGMP know that
1182 * we are leaving the multicast group.
1183 */
1184 igmp_leavegroup(inm);
1185 /*
1186 * Unlink from list.
1187 */
1188 LIST_REMOVE(inm, inm_list);
1189 in_multientries--;
1190 /*
1191 * Notify the network driver to update its multicast reception
1192 * filter.
1193 */
1194 sockaddr_in_init(&sin, &inm->inm_addr, 0);
1195 ifreq_setaddr(SIOCDELMULTI, &ifr, sintosa(&sin));
1196 (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI, &ifr);
1197 pool_put(&inmulti_pool, inm);
1198 }
1199 splx(s);
1200 }
1201