in.c revision 1.58 1 /* $NetBSD: in.c,v 1.58 2000/03/21 11:23: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/if_types.h>
119 #include <net/route.h>
120 #include "gif.h"
121 #if NGIF > 0
122 #include <net/if_gif.h>
123 #endif
124
125 #include <net/if_ether.h>
126
127 #include <netinet/in_systm.h>
128 #include <netinet/in.h>
129 #include <netinet/in_var.h>
130 #include <netinet/if_inarp.h>
131 #include <netinet/ip_mroute.h>
132 #include <netinet/igmp_var.h>
133
134 #ifdef INET
135
136 static int in_mask2len __P((struct in_addr *));
137 static void in_len2mask __P((struct in_addr *, int));
138 static int in_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
139 struct ifnet *, struct proc *));
140
141 #ifndef SUBNETSARELOCAL
142 #define SUBNETSARELOCAL 1
143 #endif
144
145 #ifndef HOSTZEROBROADCAST
146 #define HOSTZEROBROADCAST 1
147 #endif
148
149 int subnetsarelocal = SUBNETSARELOCAL;
150 int hostzeroisbroadcast = HOSTZEROBROADCAST;
151
152 /*
153 * Return 1 if an internet address is for a ``local'' host
154 * (one to which we have a connection). If subnetsarelocal
155 * is true, this includes other subnets of the local net.
156 * Otherwise, it includes only the directly-connected (sub)nets.
157 */
158 int
159 in_localaddr(in)
160 struct in_addr in;
161 {
162 register struct in_ifaddr *ia;
163
164 if (subnetsarelocal) {
165 for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
166 if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
167 return (1);
168 } else {
169 for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
170 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
171 return (1);
172 }
173 return (0);
174 }
175
176 /*
177 * Determine whether an IP address is in a reserved set of addresses
178 * that may not be forwarded, or whether datagrams to that destination
179 * may be forwarded.
180 */
181 int
182 in_canforward(in)
183 struct in_addr in;
184 {
185 register u_int32_t net;
186
187 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
188 return (0);
189 if (IN_CLASSA(in.s_addr)) {
190 net = in.s_addr & IN_CLASSA_NET;
191 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
192 return (0);
193 }
194 return (1);
195 }
196
197 /*
198 * Trim a mask in a sockaddr
199 */
200 void
201 in_socktrim(ap)
202 struct sockaddr_in *ap;
203 {
204 register char *cplim = (char *) &ap->sin_addr;
205 register char *cp = (char *) (&ap->sin_addr + 1);
206
207 ap->sin_len = 0;
208 while (--cp >= cplim)
209 if (*cp) {
210 (ap)->sin_len = cp - (char *) (ap) + 1;
211 break;
212 }
213 }
214
215 /*
216 * Routine to take an Internet address and convert into a
217 * "dotted quad" representation for printing.
218 */
219 const char *
220 in_fmtaddr(addr)
221 struct in_addr addr;
222 {
223 static char buf[sizeof("123.456.789.123")];
224
225 addr.s_addr = ntohl(addr.s_addr);
226
227 sprintf(buf, "%d.%d.%d.%d",
228 (addr.s_addr >> 24) & 0xFF,
229 (addr.s_addr >> 16) & 0xFF,
230 (addr.s_addr >> 8) & 0xFF,
231 (addr.s_addr >> 0) & 0xFF);
232 return buf;
233 }
234
235 /*
236 * Maintain the "in_maxmtu" variable, which is the largest
237 * mtu for non-local interfaces with AF_INET addresses assigned
238 * to them that are up.
239 */
240 unsigned long in_maxmtu;
241
242 void
243 in_setmaxmtu()
244 {
245 register struct in_ifaddr *ia;
246 register struct ifnet *ifp;
247 unsigned long maxmtu = 0;
248
249 for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) {
250 if ((ifp = ia->ia_ifp) == 0)
251 continue;
252 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
253 continue;
254 if (ifp->if_mtu > maxmtu)
255 maxmtu = ifp->if_mtu;
256 }
257 if (maxmtu)
258 in_maxmtu = maxmtu;
259 }
260
261 static int
262 in_mask2len(mask)
263 struct in_addr *mask;
264 {
265 int x, y;
266 u_char *p;
267
268 p = (u_char *)mask;
269 for (x = 0; x < sizeof(*mask); x++) {
270 if (p[x] != 0xff)
271 break;
272 }
273 y = 0;
274 if (x < sizeof(*mask)) {
275 for (y = 0; y < 8; y++) {
276 if ((p[x] & (0x80 >> y)) == 0)
277 break;
278 }
279 }
280 return x * 8 + y;
281 }
282
283 static void
284 in_len2mask(mask, len)
285 struct in_addr *mask;
286 int len;
287 {
288 int i;
289 u_char *p;
290
291 p = (u_char *)mask;
292 bzero(mask, sizeof(*mask));
293 for (i = 0; i < len / 8; i++)
294 p[i] = 0xff;
295 if (len % 8)
296 p[i] = (0xff00 >> (len % 8)) & 0xff;
297 }
298
299 int in_interfaces; /* number of external internet interfaces */
300
301 /*
302 * Generic internet control operations (ioctl's).
303 * Ifp is 0 if not an interface-specific ioctl.
304 */
305 /* ARGSUSED */
306 int
307 in_control(so, cmd, data, ifp, p)
308 struct socket *so;
309 u_long cmd;
310 caddr_t data;
311 register struct ifnet *ifp;
312 struct proc *p;
313 {
314 register struct ifreq *ifr = (struct ifreq *)data;
315 register struct in_ifaddr *ia = 0;
316 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
317 struct sockaddr_in oldaddr;
318 int error, hostIsNew, maskIsNew;
319 int newifaddr;
320
321 #if NGIF > 0
322 if (ifp && ifp->if_type == IFT_GIF) {
323 switch (cmd) {
324 case SIOCSIFPHYADDR:
325 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
326 return(EPERM);
327 case SIOCGIFPSRCADDR:
328 case SIOCGIFPDSTADDR:
329 return gif_ioctl(ifp, cmd, data);
330 }
331 }
332 #endif
333
334 switch (cmd) {
335 case SIOCALIFADDR:
336 case SIOCDLIFADDR:
337 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
338 return(EPERM);
339 /*fall through*/
340 case SIOCGLIFADDR:
341 if (!ifp)
342 return EINVAL;
343 return in_lifaddr_ioctl(so, cmd, data, ifp, p);
344 }
345
346 /*
347 * Find address for this interface, if it exists.
348 */
349 if (ifp)
350 IFP_TO_IA(ifp, ia);
351
352 switch (cmd) {
353
354 case SIOCAIFADDR:
355 case SIOCDIFADDR:
356 case SIOCGIFALIAS:
357 if (ifra->ifra_addr.sin_family == AF_INET)
358 for (ia = IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr).lh_first;
359 ia != 0; ia = ia->ia_hash.le_next) {
360 if (ia->ia_ifp == ifp &&
361 in_hosteq(ia->ia_addr.sin_addr,
362 ifra->ifra_addr.sin_addr))
363 break;
364 }
365 if (cmd == SIOCDIFADDR) {
366 if (ia == 0)
367 return (EADDRNOTAVAIL);
368 #if 1 /*def COMPAT_43*/
369 if (ifra->ifra_addr.sin_family == AF_UNSPEC)
370 ifra->ifra_addr.sin_family = AF_INET;
371 #endif
372 }
373 /* FALLTHROUGH */
374 case SIOCSIFADDR:
375 case SIOCSIFDSTADDR:
376 if (ifra->ifra_addr.sin_family != AF_INET)
377 return (EAFNOSUPPORT);
378 /* FALLTHROUGH */
379 case SIOCSIFNETMASK:
380 if (ifp == 0)
381 panic("in_control");
382
383 if (cmd == SIOCGIFALIAS)
384 break;
385
386 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
387 return (EPERM);
388
389 if (ia == 0) {
390 MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
391 M_IFADDR, M_WAITOK);
392 if (ia == 0)
393 return (ENOBUFS);
394 bzero((caddr_t)ia, sizeof *ia);
395 TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list);
396 IFAREF(&ia->ia_ifa);
397 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
398 ifa_list);
399 IFAREF(&ia->ia_ifa);
400 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
401 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
402 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
403 ia->ia_sockmask.sin_len = 8;
404 if (ifp->if_flags & IFF_BROADCAST) {
405 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
406 ia->ia_broadaddr.sin_family = AF_INET;
407 }
408 ia->ia_ifp = ifp;
409 LIST_INIT(&ia->ia_multiaddrs);
410 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
411 in_interfaces++;
412
413 newifaddr = 1;
414 } else
415 newifaddr = 0;
416 break;
417
418 case SIOCSIFBRDADDR:
419 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
420 return (EPERM);
421 /* FALLTHROUGH */
422
423 case SIOCGIFADDR:
424 case SIOCGIFNETMASK:
425 case SIOCGIFDSTADDR:
426 case SIOCGIFBRDADDR:
427 if (ia == 0)
428 return (EADDRNOTAVAIL);
429 break;
430 }
431 switch (cmd) {
432
433 case SIOCGIFADDR:
434 *satosin(&ifr->ifr_addr) = ia->ia_addr;
435 break;
436
437 case SIOCGIFBRDADDR:
438 if ((ifp->if_flags & IFF_BROADCAST) == 0)
439 return (EINVAL);
440 *satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
441 break;
442
443 case SIOCGIFDSTADDR:
444 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
445 return (EINVAL);
446 *satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
447 break;
448
449 case SIOCGIFNETMASK:
450 *satosin(&ifr->ifr_addr) = ia->ia_sockmask;
451 break;
452
453 case SIOCSIFDSTADDR:
454 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
455 return (EINVAL);
456 oldaddr = ia->ia_dstaddr;
457 ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
458 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
459 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
460 ia->ia_dstaddr = oldaddr;
461 return (error);
462 }
463 if (ia->ia_flags & IFA_ROUTE) {
464 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
465 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
466 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
467 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
468 }
469 break;
470
471 case SIOCSIFBRDADDR:
472 if ((ifp->if_flags & IFF_BROADCAST) == 0)
473 return (EINVAL);
474 ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
475 break;
476
477 case SIOCSIFADDR:
478 error = in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1);
479 #if 0
480 /*
481 * the code chokes if we are to assign multiple addresses with
482 * the same address prefix (rtinit() will return EEXIST, which
483 * is not fatal actually). we will get memory leak if we
484 * don't do it.
485 * -> we may want to hide EEXIST from rtinit().
486 */
487 undo:
488 if (error && newifaddr) {
489 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
490 IFAFREE(&ia->ia_ifa);
491 TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
492 IFAFREE(&ia->ia_ifa);
493 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
494 in_interfaces--;
495 }
496 #endif
497 return error;
498
499 case SIOCSIFNETMASK:
500 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr =
501 ifra->ifra_addr.sin_addr.s_addr;
502 break;
503
504 case SIOCAIFADDR:
505 maskIsNew = 0;
506 hostIsNew = 1;
507 error = 0;
508 if (ia->ia_addr.sin_family == AF_INET) {
509 if (ifra->ifra_addr.sin_len == 0) {
510 ifra->ifra_addr = ia->ia_addr;
511 hostIsNew = 0;
512 } else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr))
513 hostIsNew = 0;
514 }
515 if (ifra->ifra_mask.sin_len) {
516 in_ifscrub(ifp, ia);
517 ia->ia_sockmask = ifra->ifra_mask;
518 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
519 maskIsNew = 1;
520 }
521 if ((ifp->if_flags & IFF_POINTOPOINT) &&
522 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
523 in_ifscrub(ifp, ia);
524 ia->ia_dstaddr = ifra->ifra_dstaddr;
525 maskIsNew = 1; /* We lie; but the effect's the same */
526 }
527 if (ifra->ifra_addr.sin_family == AF_INET &&
528 (hostIsNew || maskIsNew)) {
529 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
530 #if 0
531 if (error)
532 goto undo;
533 #endif
534 }
535 if ((ifp->if_flags & IFF_BROADCAST) &&
536 (ifra->ifra_broadaddr.sin_family == AF_INET))
537 ia->ia_broadaddr = ifra->ifra_broadaddr;
538 return (error);
539
540 case SIOCGIFALIAS:
541 ifra->ifra_mask = ia->ia_sockmask;
542 if ((ifp->if_flags & IFF_POINTOPOINT) &&
543 (ia->ia_dstaddr.sin_family == AF_INET))
544 ifra->ifra_dstaddr = ia->ia_dstaddr;
545 else if ((ifp->if_flags & IFF_BROADCAST) &&
546 (ia->ia_broadaddr.sin_family == AF_INET))
547 ifra->ifra_broadaddr = ia->ia_broadaddr;
548 else
549 bzero(&ifra->ifra_broadaddr,
550 sizeof(ifra->ifra_broadaddr));
551 return 0;
552
553 case SIOCDIFADDR:
554 in_purgeaddr(&ia->ia_ifa, ifp);
555 break;
556
557 #ifdef MROUTING
558 case SIOCGETVIFCNT:
559 case SIOCGETSGCNT:
560 return (mrt_ioctl(so, cmd, data));
561 #endif /* MROUTING */
562
563 default:
564 if (ifp == 0 || ifp->if_ioctl == 0)
565 return (EOPNOTSUPP);
566 error = (*ifp->if_ioctl)(ifp, cmd, data);
567 in_setmaxmtu();
568 return(error);
569 }
570 return (0);
571 }
572
573 void
574 in_purgeaddr(ifa, ifp)
575 struct ifaddr *ifa;
576 struct ifnet *ifp;
577 {
578 struct in_ifaddr *ia = (void *) ifa;
579
580 in_ifscrub(ifp, ia);
581 LIST_REMOVE(ia, ia_hash);
582 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
583 IFAFREE(&ia->ia_ifa);
584 TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
585 IFAFREE(&ia->ia_ifa);
586 in_setmaxmtu();
587 }
588
589 void
590 in_purgeif(ifp)
591 struct ifnet *ifp;
592 {
593 struct ifaddr *ifa, *nifa;
594
595 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
596 nifa = TAILQ_NEXT(ifa, ifa_list);
597 if (ifa->ifa_addr->sa_family != AF_INET)
598 continue;
599 in_purgeaddr(ifa, ifp);
600 }
601 }
602
603 /*
604 * SIOC[GAD]LIFADDR.
605 * SIOCGLIFADDR: get first address. (???)
606 * SIOCGLIFADDR with IFLR_PREFIX:
607 * get first address that matches the specified prefix.
608 * SIOCALIFADDR: add the specified address.
609 * SIOCALIFADDR with IFLR_PREFIX:
610 * EINVAL since we can't deduce hostid part of the address.
611 * SIOCDLIFADDR: delete the specified address.
612 * SIOCDLIFADDR with IFLR_PREFIX:
613 * delete the first address that matches the specified prefix.
614 * return values:
615 * EINVAL on invalid parameters
616 * EADDRNOTAVAIL on prefix match failed/specified address not found
617 * other values may be returned from in_ioctl()
618 */
619 static int
620 in_lifaddr_ioctl(so, cmd, data, ifp, p)
621 struct socket *so;
622 u_long cmd;
623 caddr_t data;
624 struct ifnet *ifp;
625 struct proc *p;
626 {
627 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
628 struct ifaddr *ifa;
629 struct sockaddr *sa;
630
631 /* sanity checks */
632 if (!data || !ifp) {
633 panic("invalid argument to in_lifaddr_ioctl");
634 /*NOTRECHED*/
635 }
636
637 switch (cmd) {
638 case SIOCGLIFADDR:
639 /* address must be specified on GET with IFLR_PREFIX */
640 if ((iflr->flags & IFLR_PREFIX) == 0)
641 break;
642 /*FALLTHROUGH*/
643 case SIOCALIFADDR:
644 case SIOCDLIFADDR:
645 /* address must be specified on ADD and DELETE */
646 sa = (struct sockaddr *)&iflr->addr;
647 if (sa->sa_family != AF_INET)
648 return EINVAL;
649 if (sa->sa_len != sizeof(struct sockaddr_in))
650 return EINVAL;
651 /* XXX need improvement */
652 sa = (struct sockaddr *)&iflr->dstaddr;
653 if (sa->sa_family
654 && sa->sa_family != AF_INET)
655 return EINVAL;
656 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in))
657 return EINVAL;
658 break;
659 default: /*shouldn't happen*/
660 #if 0
661 panic("invalid cmd to in_lifaddr_ioctl");
662 /*NOTREACHED*/
663 #else
664 return EOPNOTSUPP;
665 #endif
666 }
667 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
668 return EINVAL;
669
670 switch (cmd) {
671 case SIOCALIFADDR:
672 {
673 struct in_aliasreq ifra;
674
675 if (iflr->flags & IFLR_PREFIX)
676 return EINVAL;
677
678 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
679 bzero(&ifra, sizeof(ifra));
680 bcopy(iflr->iflr_name, ifra.ifra_name,
681 sizeof(ifra.ifra_name));
682
683 bcopy(&iflr->addr, &ifra.ifra_addr,
684 ((struct sockaddr *)&iflr->addr)->sa_len);
685
686 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
687 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
688 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
689 }
690
691 ifra.ifra_mask.sin_family = AF_INET;
692 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
693 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
694
695 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, p);
696 }
697 case SIOCGLIFADDR:
698 case SIOCDLIFADDR:
699 {
700 struct in_ifaddr *ia;
701 struct in_addr mask, candidate, match;
702 struct sockaddr_in *sin;
703 int cmp;
704
705 bzero(&mask, sizeof(mask));
706 if (iflr->flags & IFLR_PREFIX) {
707 /* lookup a prefix rather than address. */
708 in_len2mask(&mask, iflr->prefixlen);
709
710 sin = (struct sockaddr_in *)&iflr->addr;
711 match.s_addr = sin->sin_addr.s_addr;
712 match.s_addr &= mask.s_addr;
713
714 /* if you set extra bits, that's wrong */
715 if (match.s_addr != sin->sin_addr.s_addr)
716 return EINVAL;
717
718 cmp = 1;
719 } else {
720 if (cmd == SIOCGLIFADDR) {
721 /* on getting an address, take the 1st match */
722 cmp = 0; /*XXX*/
723 } else {
724 /* on deleting an address, do exact match */
725 in_len2mask(&mask, 32);
726 sin = (struct sockaddr_in *)&iflr->addr;
727 match.s_addr = sin->sin_addr.s_addr;
728
729 cmp = 1;
730 }
731 }
732
733 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
734 if (ifa->ifa_addr->sa_family != AF_INET6)
735 continue;
736 if (!cmp)
737 break;
738 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
739 candidate.s_addr &= mask.s_addr;
740 if (candidate.s_addr == match.s_addr)
741 break;
742 }
743 if (!ifa)
744 return EADDRNOTAVAIL;
745 ia = (struct in_ifaddr *)ifa;
746
747 if (cmd == SIOCGLIFADDR) {
748 /* fill in the if_laddrreq structure */
749 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
750
751 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
752 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
753 ia->ia_dstaddr.sin_len);
754 } else
755 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
756
757 iflr->prefixlen =
758 in_mask2len(&ia->ia_sockmask.sin_addr);
759
760 iflr->flags = 0; /*XXX*/
761
762 return 0;
763 } else {
764 struct in_aliasreq ifra;
765
766 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
767 bzero(&ifra, sizeof(ifra));
768 bcopy(iflr->iflr_name, ifra.ifra_name,
769 sizeof(ifra.ifra_name));
770
771 bcopy(&ia->ia_addr, &ifra.ifra_addr,
772 ia->ia_addr.sin_len);
773 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
774 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
775 ia->ia_dstaddr.sin_len);
776 }
777 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
778 ia->ia_sockmask.sin_len);
779
780 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
781 ifp, p);
782 }
783 }
784 }
785
786 return EOPNOTSUPP; /*just for safety*/
787 }
788
789 /*
790 * Delete any existing route for an interface.
791 */
792 void
793 in_ifscrub(ifp, ia)
794 register struct ifnet *ifp;
795 register struct in_ifaddr *ia;
796 {
797
798 if ((ia->ia_flags & IFA_ROUTE) == 0)
799 return;
800 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
801 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
802 else
803 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
804 ia->ia_flags &= ~IFA_ROUTE;
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 register struct ifnet *ifp;
814 register struct in_ifaddr *ia;
815 struct sockaddr_in *sin;
816 int scrub;
817 {
818 register u_int32_t i = sin->sin_addr.s_addr;
819 struct sockaddr_in oldaddr;
820 int s = splimp(), flags = RTF_UP, error;
821
822 /*
823 * Set up new addresses.
824 */
825 oldaddr = ia->ia_addr;
826 if (ia->ia_addr.sin_family == AF_INET)
827 LIST_REMOVE(ia, ia_hash);
828 ia->ia_addr = *sin;
829 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
830
831 /*
832 * Give the interface a chance to initialize
833 * if this is its first address,
834 * and to validate the address if necessary.
835 */
836 if (ifp->if_ioctl &&
837 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
838 goto bad;
839 splx(s);
840 if (scrub) {
841 ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
842 in_ifscrub(ifp, ia);
843 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
844 }
845
846 if (IN_CLASSA(i))
847 ia->ia_netmask = IN_CLASSA_NET;
848 else if (IN_CLASSB(i))
849 ia->ia_netmask = IN_CLASSB_NET;
850 else
851 ia->ia_netmask = IN_CLASSC_NET;
852 /*
853 * The subnet mask usually includes at least the standard network part,
854 * but may may be smaller in the case of supernetting.
855 * If it is set, we believe it.
856 */
857 if (ia->ia_subnetmask == 0) {
858 ia->ia_subnetmask = ia->ia_netmask;
859 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
860 } else
861 ia->ia_netmask &= ia->ia_subnetmask;
862
863 ia->ia_net = i & ia->ia_netmask;
864 ia->ia_subnet = i & ia->ia_subnetmask;
865 in_socktrim(&ia->ia_sockmask);
866 /* re-calculate the "in_maxmtu" value */
867 in_setmaxmtu();
868 /*
869 * Add route for the network.
870 */
871 ia->ia_ifa.ifa_metric = ifp->if_metric;
872 if (ifp->if_flags & IFF_BROADCAST) {
873 ia->ia_broadaddr.sin_addr.s_addr =
874 ia->ia_subnet | ~ia->ia_subnetmask;
875 ia->ia_netbroadcast.s_addr =
876 ia->ia_net | ~ia->ia_netmask;
877 } else if (ifp->if_flags & IFF_LOOPBACK) {
878 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
879 flags |= RTF_HOST;
880 } else if (ifp->if_flags & IFF_POINTOPOINT) {
881 if (ia->ia_dstaddr.sin_family != AF_INET)
882 return (0);
883 flags |= RTF_HOST;
884 }
885 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags);
886 if (!error)
887 ia->ia_flags |= IFA_ROUTE;
888 /*
889 * If the interface supports multicast, join the "all hosts"
890 * multicast group on that interface.
891 */
892 if (ifp->if_flags & IFF_MULTICAST) {
893 struct in_addr addr;
894
895 addr.s_addr = INADDR_ALLHOSTS_GROUP;
896 in_addmulti(&addr, ifp);
897 }
898 return (error);
899 bad:
900 splx(s);
901 LIST_REMOVE(ia, ia_hash);
902 ia->ia_addr = oldaddr;
903 if (ia->ia_addr.sin_family == AF_INET)
904 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
905 ia, ia_hash);
906 return (error);
907 }
908
909 /*
910 * Return 1 if the address might be a local broadcast address.
911 */
912 int
913 in_broadcast(in, ifp)
914 struct in_addr in;
915 struct ifnet *ifp;
916 {
917 register struct ifaddr *ifa;
918
919 if (in.s_addr == INADDR_BROADCAST ||
920 in_nullhost(in))
921 return 1;
922 if ((ifp->if_flags & IFF_BROADCAST) == 0)
923 return 0;
924 /*
925 * Look through the list of addresses for a match
926 * with a broadcast address.
927 */
928 #define ia (ifatoia(ifa))
929 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
930 if (ifa->ifa_addr->sa_family == AF_INET &&
931 (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
932 in_hosteq(in, ia->ia_netbroadcast) ||
933 (hostzeroisbroadcast &&
934 /*
935 * Check for old-style (host 0) broadcast.
936 */
937 (in.s_addr == ia->ia_subnet ||
938 in.s_addr == ia->ia_net))))
939 return 1;
940 return (0);
941 #undef ia
942 }
943
944 /*
945 * Add an address to the list of IP multicast addresses for a given interface.
946 */
947 struct in_multi *
948 in_addmulti(ap, ifp)
949 register struct in_addr *ap;
950 register struct ifnet *ifp;
951 {
952 register struct in_multi *inm;
953 struct ifreq ifr;
954 struct in_ifaddr *ia;
955 int s = splsoftnet();
956
957 /*
958 * See if address already in list.
959 */
960 IN_LOOKUP_MULTI(*ap, ifp, inm);
961 if (inm != NULL) {
962 /*
963 * Found it; just increment the reference count.
964 */
965 ++inm->inm_refcount;
966 } else {
967 /*
968 * New address; allocate a new multicast record
969 * and link it into the interface's multicast list.
970 */
971 inm = (struct in_multi *)malloc(sizeof(*inm),
972 M_IPMADDR, M_NOWAIT);
973 if (inm == NULL) {
974 splx(s);
975 return (NULL);
976 }
977 inm->inm_addr = *ap;
978 inm->inm_ifp = ifp;
979 inm->inm_refcount = 1;
980 IFP_TO_IA(ifp, ia);
981 if (ia == NULL) {
982 free(inm, M_IPMADDR);
983 splx(s);
984 return (NULL);
985 }
986 inm->inm_ia = ia;
987 LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
988 /*
989 * Ask the network driver to update its multicast reception
990 * filter appropriately for the new address.
991 */
992 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
993 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
994 satosin(&ifr.ifr_addr)->sin_addr = *ap;
995 if ((ifp->if_ioctl == NULL) ||
996 (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
997 LIST_REMOVE(inm, inm_list);
998 free(inm, M_IPMADDR);
999 splx(s);
1000 return (NULL);
1001 }
1002 /*
1003 * Let IGMP know that we have joined a new IP multicast group.
1004 */
1005 igmp_joingroup(inm);
1006 }
1007 splx(s);
1008 return (inm);
1009 }
1010
1011 /*
1012 * Delete a multicast address record.
1013 */
1014 void
1015 in_delmulti(inm)
1016 register struct in_multi *inm;
1017 {
1018 struct ifreq ifr;
1019 int s = splsoftnet();
1020
1021 if (--inm->inm_refcount == 0) {
1022 /*
1023 * No remaining claims to this record; let IGMP know that
1024 * we are leaving the multicast group.
1025 */
1026 igmp_leavegroup(inm);
1027 /*
1028 * Unlink from list.
1029 */
1030 LIST_REMOVE(inm, inm_list);
1031 /*
1032 * Notify the network driver to update its multicast reception
1033 * filter.
1034 */
1035 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
1036 satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
1037 (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
1038 (caddr_t)&ifr);
1039 free(inm, M_IPMADDR);
1040 }
1041 splx(s);
1042 }
1043 #endif
1044