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