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