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