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