in.c revision 1.113 1 /* $NetBSD: in.c,v 1.113 2006/10/25 12:48:44 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.113 2006/10/25 12:48:44 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 lwp *);
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 lwp *l)
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 (l == NULL)
325 return (EPERM);
326 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
327 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
328 NULL) != 0)
329 return (EPERM);
330 /*fall through*/
331 case SIOCGLIFADDR:
332 if (!ifp)
333 return EINVAL;
334 return in_lifaddr_ioctl(so, cmd, data, ifp, l);
335 }
336
337 /*
338 * Find address for this interface, if it exists.
339 */
340 if (ifp)
341 IFP_TO_IA(ifp, ia);
342
343 switch (cmd) {
344
345 case SIOCAIFADDR:
346 case SIOCDIFADDR:
347 case SIOCGIFALIAS:
348 if (ifra->ifra_addr.sin_family == AF_INET)
349 LIST_FOREACH(ia,
350 &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
351 ia_hash) {
352 if (ia->ia_ifp == ifp &&
353 in_hosteq(ia->ia_addr.sin_addr,
354 ifra->ifra_addr.sin_addr))
355 break;
356 }
357 if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL)
358 return (EADDRNOTAVAIL);
359
360 #if 1 /*def COMPAT_43*/
361 if (cmd == SIOCDIFADDR &&
362 ifra->ifra_addr.sin_family == AF_UNSPEC) {
363 ifra->ifra_addr.sin_family = AF_INET;
364 }
365 #endif
366 /* FALLTHROUGH */
367 case SIOCSIFADDR:
368 case SIOCSIFDSTADDR:
369 if (ifra->ifra_addr.sin_family != AF_INET)
370 return (EAFNOSUPPORT);
371 /* FALLTHROUGH */
372 case SIOCSIFNETMASK:
373 if (ifp == 0)
374 panic("in_control");
375
376 if (cmd == SIOCGIFALIAS)
377 break;
378
379 if (ia == NULL &&
380 (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
381 return (EADDRNOTAVAIL);
382
383 if (l == NULL)
384 return (EPERM);
385 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
386 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
387 NULL) != 0)
388 return (EPERM);
389
390 if (ia == 0) {
391 MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
392 M_IFADDR, M_WAITOK);
393 if (ia == 0)
394 return (ENOBUFS);
395 bzero((caddr_t)ia, sizeof *ia);
396 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
397 IFAREF(&ia->ia_ifa);
398 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
399 ifa_list);
400 IFAREF(&ia->ia_ifa);
401 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
402 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
403 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
404 ia->ia_sockmask.sin_len = 8;
405 if (ifp->if_flags & IFF_BROADCAST) {
406 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
407 ia->ia_broadaddr.sin_family = AF_INET;
408 }
409 ia->ia_ifp = ifp;
410 LIST_INIT(&ia->ia_multiaddrs);
411 newifaddr = 1;
412 }
413 break;
414
415 case SIOCSIFBRDADDR:
416 if (l == NULL)
417 return (EPERM);
418 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
419 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
420 NULL) != 0)
421 return (EPERM);
422 /* FALLTHROUGH */
423
424 case SIOCGIFADDR:
425 case SIOCGIFNETMASK:
426 case SIOCGIFDSTADDR:
427 case SIOCGIFBRDADDR:
428 if (ia == 0)
429 return (EADDRNOTAVAIL);
430 break;
431 }
432 error = 0;
433 switch (cmd) {
434
435 case SIOCGIFADDR:
436 *satosin(&ifr->ifr_addr) = ia->ia_addr;
437 break;
438
439 case SIOCGIFBRDADDR:
440 if ((ifp->if_flags & IFF_BROADCAST) == 0)
441 return (EINVAL);
442 *satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
443 break;
444
445 case SIOCGIFDSTADDR:
446 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
447 return (EINVAL);
448 *satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
449 break;
450
451 case SIOCGIFNETMASK:
452 *satosin(&ifr->ifr_addr) = ia->ia_sockmask;
453 break;
454
455 case SIOCSIFDSTADDR:
456 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
457 return (EINVAL);
458 oldaddr = ia->ia_dstaddr;
459 ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
460 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
461 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
462 ia->ia_dstaddr = oldaddr;
463 return (error);
464 }
465 if (ia->ia_flags & IFA_ROUTE) {
466 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
467 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
468 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
469 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
470 }
471 break;
472
473 case SIOCSIFBRDADDR:
474 if ((ifp->if_flags & IFF_BROADCAST) == 0)
475 return (EINVAL);
476 ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
477 break;
478
479 case SIOCSIFADDR:
480 error = in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1);
481 #ifdef PFIL_HOOKS
482 if (!error)
483 (void)pfil_run_hooks(&if_pfil,
484 (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
485 #endif
486 break;
487
488 case SIOCSIFNETMASK:
489 in_ifscrub(ifp, ia);
490 ia->ia_sockmask = *satosin(&ifr->ifr_addr);
491 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
492 error = in_ifinit(ifp, ia, NULL, 0);
493 break;
494
495 case SIOCAIFADDR:
496 maskIsNew = 0;
497 hostIsNew = 1;
498 if (ia->ia_addr.sin_family == AF_INET) {
499 if (ifra->ifra_addr.sin_len == 0) {
500 ifra->ifra_addr = ia->ia_addr;
501 hostIsNew = 0;
502 } else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr))
503 hostIsNew = 0;
504 }
505 if (ifra->ifra_mask.sin_len) {
506 in_ifscrub(ifp, ia);
507 ia->ia_sockmask = ifra->ifra_mask;
508 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
509 maskIsNew = 1;
510 }
511 if ((ifp->if_flags & IFF_POINTOPOINT) &&
512 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
513 in_ifscrub(ifp, ia);
514 ia->ia_dstaddr = ifra->ifra_dstaddr;
515 maskIsNew = 1; /* We lie; but the effect's the same */
516 }
517 if (ifra->ifra_addr.sin_family == AF_INET &&
518 (hostIsNew || maskIsNew)) {
519 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
520 }
521 if ((ifp->if_flags & IFF_BROADCAST) &&
522 (ifra->ifra_broadaddr.sin_family == AF_INET))
523 ia->ia_broadaddr = ifra->ifra_broadaddr;
524 #ifdef PFIL_HOOKS
525 if (!error)
526 (void)pfil_run_hooks(&if_pfil,
527 (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
528 #endif
529 break;
530
531 case SIOCGIFALIAS:
532 ifra->ifra_mask = ia->ia_sockmask;
533 if ((ifp->if_flags & IFF_POINTOPOINT) &&
534 (ia->ia_dstaddr.sin_family == AF_INET))
535 ifra->ifra_dstaddr = ia->ia_dstaddr;
536 else if ((ifp->if_flags & IFF_BROADCAST) &&
537 (ia->ia_broadaddr.sin_family == AF_INET))
538 ifra->ifra_broadaddr = ia->ia_broadaddr;
539 else
540 bzero(&ifra->ifra_broadaddr,
541 sizeof(ifra->ifra_broadaddr));
542 break;
543
544 case SIOCDIFADDR:
545 in_purgeaddr(&ia->ia_ifa, ifp);
546 #ifdef PFIL_HOOKS
547 (void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR,
548 ifp, PFIL_IFADDR);
549 #endif
550 break;
551
552 #ifdef MROUTING
553 case SIOCGETVIFCNT:
554 case SIOCGETSGCNT:
555 error = mrt_ioctl(so, cmd, data);
556 break;
557 #endif /* MROUTING */
558
559 default:
560 if (ifp == 0 || ifp->if_ioctl == 0)
561 return (EOPNOTSUPP);
562 error = (*ifp->if_ioctl)(ifp, cmd, data);
563 in_setmaxmtu();
564 break;
565 }
566
567 if (error && newifaddr) {
568 KASSERT(ia != NULL);
569 in_purgeaddr(&ia->ia_ifa, ifp);
570 }
571
572 return error;
573 }
574
575 void
576 in_purgeaddr(struct ifaddr *ifa, 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_ifaddrhead, ia, ia_list);
585 if (ia->ia_allhosts != NULL)
586 in_delmulti(ia->ia_allhosts);
587 IFAFREE(&ia->ia_ifa);
588 in_setmaxmtu();
589 }
590
591 void
592 in_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */
593 {
594 struct ifaddr *ifa, *nifa;
595
596 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
597 nifa = TAILQ_NEXT(ifa, ifa_list);
598 if (ifa->ifa_addr->sa_family != AF_INET)
599 continue;
600 in_purgeaddr(ifa, ifp);
601 }
602
603 igmp_purgeif(ifp); /* manipulates pools */
604 #ifdef MROUTING
605 ip_mrouter_detach(ifp);
606 #endif
607 }
608
609 /*
610 * SIOC[GAD]LIFADDR.
611 * SIOCGLIFADDR: get first address. (???)
612 * SIOCGLIFADDR with IFLR_PREFIX:
613 * get first address that matches the specified prefix.
614 * SIOCALIFADDR: add the specified address.
615 * SIOCALIFADDR with IFLR_PREFIX:
616 * EINVAL since we can't deduce hostid part of the address.
617 * SIOCDLIFADDR: delete the specified address.
618 * SIOCDLIFADDR with IFLR_PREFIX:
619 * delete the first address that matches the specified prefix.
620 * return values:
621 * EINVAL on invalid parameters
622 * EADDRNOTAVAIL on prefix match failed/specified address not found
623 * other values may be returned from in_ioctl()
624 */
625 static int
626 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
627 struct ifnet *ifp, struct lwp *l)
628 {
629 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
630 struct ifaddr *ifa;
631 struct sockaddr *sa;
632
633 /* sanity checks */
634 if (!data || !ifp) {
635 panic("invalid argument to in_lifaddr_ioctl");
636 /*NOTRECHED*/
637 }
638
639 switch (cmd) {
640 case SIOCGLIFADDR:
641 /* address must be specified on GET with IFLR_PREFIX */
642 if ((iflr->flags & IFLR_PREFIX) == 0)
643 break;
644 /*FALLTHROUGH*/
645 case SIOCALIFADDR:
646 case SIOCDLIFADDR:
647 /* address must be specified on ADD and DELETE */
648 sa = (struct sockaddr *)&iflr->addr;
649 if (sa->sa_family != AF_INET)
650 return EINVAL;
651 if (sa->sa_len != sizeof(struct sockaddr_in))
652 return EINVAL;
653 /* XXX need improvement */
654 sa = (struct sockaddr *)&iflr->dstaddr;
655 if (sa->sa_family
656 && sa->sa_family != AF_INET)
657 return EINVAL;
658 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in))
659 return EINVAL;
660 break;
661 default: /*shouldn't happen*/
662 #if 0
663 panic("invalid cmd to in_lifaddr_ioctl");
664 /*NOTREACHED*/
665 #else
666 return EOPNOTSUPP;
667 #endif
668 }
669 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
670 return EINVAL;
671
672 switch (cmd) {
673 case SIOCALIFADDR:
674 {
675 struct in_aliasreq ifra;
676
677 if (iflr->flags & IFLR_PREFIX)
678 return EINVAL;
679
680 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
681 bzero(&ifra, sizeof(ifra));
682 bcopy(iflr->iflr_name, ifra.ifra_name,
683 sizeof(ifra.ifra_name));
684
685 bcopy(&iflr->addr, &ifra.ifra_addr,
686 ((struct sockaddr *)&iflr->addr)->sa_len);
687
688 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
689 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
690 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
691 }
692
693 ifra.ifra_mask.sin_family = AF_INET;
694 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
695 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
696
697 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, l);
698 }
699 case SIOCGLIFADDR:
700 case SIOCDLIFADDR:
701 {
702 struct in_ifaddr *ia;
703 struct in_addr mask, candidate, match;
704 struct sockaddr_in *sin;
705 int cmp;
706
707 bzero(&mask, sizeof(mask));
708 bzero(&match, sizeof(match)); /* XXX gcc */
709 if (iflr->flags & IFLR_PREFIX) {
710 /* lookup a prefix rather than address. */
711 in_len2mask(&mask, iflr->prefixlen);
712
713 sin = (struct sockaddr_in *)&iflr->addr;
714 match.s_addr = sin->sin_addr.s_addr;
715 match.s_addr &= mask.s_addr;
716
717 /* if you set extra bits, that's wrong */
718 if (match.s_addr != sin->sin_addr.s_addr)
719 return EINVAL;
720
721 cmp = 1;
722 } else {
723 if (cmd == SIOCGLIFADDR) {
724 /* on getting an address, take the 1st match */
725 cmp = 0; /*XXX*/
726 } else {
727 /* on deleting an address, do exact match */
728 in_len2mask(&mask, 32);
729 sin = (struct sockaddr_in *)&iflr->addr;
730 match.s_addr = sin->sin_addr.s_addr;
731
732 cmp = 1;
733 }
734 }
735
736 IFADDR_FOREACH(ifa, ifp) {
737 if (ifa->ifa_addr->sa_family != AF_INET)
738 continue;
739 if (!cmp)
740 break;
741 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
742 candidate.s_addr &= mask.s_addr;
743 if (candidate.s_addr == match.s_addr)
744 break;
745 }
746 if (!ifa)
747 return EADDRNOTAVAIL;
748 ia = (struct in_ifaddr *)ifa;
749
750 if (cmd == SIOCGLIFADDR) {
751 /* fill in the if_laddrreq structure */
752 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
753
754 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
755 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
756 ia->ia_dstaddr.sin_len);
757 } else
758 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
759
760 iflr->prefixlen =
761 in_mask2len(&ia->ia_sockmask.sin_addr);
762
763 iflr->flags = 0; /*XXX*/
764
765 return 0;
766 } else {
767 struct in_aliasreq ifra;
768
769 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
770 bzero(&ifra, sizeof(ifra));
771 bcopy(iflr->iflr_name, ifra.ifra_name,
772 sizeof(ifra.ifra_name));
773
774 bcopy(&ia->ia_addr, &ifra.ifra_addr,
775 ia->ia_addr.sin_len);
776 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
777 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
778 ia->ia_dstaddr.sin_len);
779 }
780 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
781 ia->ia_sockmask.sin_len);
782
783 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
784 ifp, l);
785 }
786 }
787 }
788
789 return EOPNOTSUPP; /*just for safety*/
790 }
791
792 /*
793 * Delete any existing route for an interface.
794 */
795 void
796 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
797 {
798
799 in_scrubprefix(ia);
800 }
801
802 /*
803 * Initialize an interface's internet address
804 * and routing table entry.
805 */
806 int
807 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
808 struct sockaddr_in *sin, int scrub)
809 {
810 u_int32_t i;
811 struct sockaddr_in oldaddr;
812 int s = splnet(), flags = RTF_UP, error;
813
814 if (!sin)
815 sin = &ia->ia_addr;
816
817 /*
818 * Set up new addresses.
819 */
820 oldaddr = ia->ia_addr;
821 if (ia->ia_addr.sin_family == AF_INET)
822 LIST_REMOVE(ia, ia_hash);
823 ia->ia_addr = *sin;
824 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
825
826 /*
827 * Give the interface a chance to initialize
828 * if this is its first address,
829 * and to validate the address if necessary.
830 */
831 if (ifp->if_ioctl &&
832 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
833 goto bad;
834 splx(s);
835 if (scrub) {
836 ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
837 in_ifscrub(ifp, ia);
838 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
839 }
840
841 i = ia->ia_addr.sin_addr.s_addr;
842 if (IN_CLASSA(i))
843 ia->ia_netmask = IN_CLASSA_NET;
844 else if (IN_CLASSB(i))
845 ia->ia_netmask = IN_CLASSB_NET;
846 else
847 ia->ia_netmask = IN_CLASSC_NET;
848 /*
849 * The subnet mask usually includes at least the standard network part,
850 * but may may be smaller in the case of supernetting.
851 * If it is set, we believe it.
852 */
853 if (ia->ia_subnetmask == 0) {
854 ia->ia_subnetmask = ia->ia_netmask;
855 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
856 } else
857 ia->ia_netmask &= ia->ia_subnetmask;
858
859 ia->ia_net = i & ia->ia_netmask;
860 ia->ia_subnet = i & ia->ia_subnetmask;
861 in_socktrim(&ia->ia_sockmask);
862 /* re-calculate the "in_maxmtu" value */
863 in_setmaxmtu();
864 /*
865 * Add route for the network.
866 */
867 ia->ia_ifa.ifa_metric = ifp->if_metric;
868 if (ifp->if_flags & IFF_BROADCAST) {
869 ia->ia_broadaddr.sin_addr.s_addr =
870 ia->ia_subnet | ~ia->ia_subnetmask;
871 ia->ia_netbroadcast.s_addr =
872 ia->ia_net | ~ia->ia_netmask;
873 } else if (ifp->if_flags & IFF_LOOPBACK) {
874 ia->ia_dstaddr = ia->ia_addr;
875 flags |= RTF_HOST;
876 } else if (ifp->if_flags & IFF_POINTOPOINT) {
877 if (ia->ia_dstaddr.sin_family != AF_INET)
878 return (0);
879 flags |= RTF_HOST;
880 }
881 error = in_addprefix(ia, flags);
882 /*
883 * If the interface supports multicast, join the "all hosts"
884 * multicast group on that interface.
885 */
886 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
887 struct in_addr addr;
888
889 addr.s_addr = INADDR_ALLHOSTS_GROUP;
890 ia->ia_allhosts = in_addmulti(&addr, ifp);
891 }
892 return (error);
893 bad:
894 splx(s);
895 LIST_REMOVE(ia, ia_hash);
896 ia->ia_addr = oldaddr;
897 if (ia->ia_addr.sin_family == AF_INET)
898 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
899 ia, ia_hash);
900 return (error);
901 }
902
903 #define rtinitflags(x) \
904 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
905 ? RTF_HOST : 0)
906
907 /*
908 * add a route to prefix ("connected route" in cisco terminology).
909 * does nothing if there's some interface address with the same prefix already.
910 */
911 static int
912 in_addprefix(struct in_ifaddr *target, int flags)
913 {
914 struct in_ifaddr *ia;
915 struct in_addr prefix, mask, p;
916 int error;
917
918 if ((flags & RTF_HOST) != 0)
919 prefix = target->ia_dstaddr.sin_addr;
920 else {
921 prefix = target->ia_addr.sin_addr;
922 mask = target->ia_sockmask.sin_addr;
923 prefix.s_addr &= mask.s_addr;
924 }
925
926 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
927 if (rtinitflags(ia))
928 p = ia->ia_dstaddr.sin_addr;
929 else {
930 p = ia->ia_addr.sin_addr;
931 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
932 }
933
934 if (prefix.s_addr != p.s_addr)
935 continue;
936
937 /*
938 * if we got a matching prefix route inserted by other
939 * interface address, we don't need to bother
940 */
941 if (ia->ia_flags & IFA_ROUTE)
942 return 0;
943 }
944
945 /*
946 * noone seem to have prefix route. insert it.
947 */
948 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
949 if (!error)
950 target->ia_flags |= IFA_ROUTE;
951 return error;
952 }
953
954 /*
955 * remove a route to prefix ("connected route" in cisco terminology).
956 * re-installs the route by using another interface address, if there's one
957 * with the same prefix (otherwise we lose the route mistakenly).
958 */
959 static int
960 in_scrubprefix(struct in_ifaddr *target)
961 {
962 struct in_ifaddr *ia;
963 struct in_addr prefix, mask, p;
964 int error;
965
966 if ((target->ia_flags & IFA_ROUTE) == 0)
967 return 0;
968
969 if (rtinitflags(target))
970 prefix = target->ia_dstaddr.sin_addr;
971 else {
972 prefix = target->ia_addr.sin_addr;
973 mask = target->ia_sockmask.sin_addr;
974 prefix.s_addr &= mask.s_addr;
975 }
976
977 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
978 if (rtinitflags(ia))
979 p = ia->ia_dstaddr.sin_addr;
980 else {
981 p = ia->ia_addr.sin_addr;
982 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
983 }
984
985 if (prefix.s_addr != p.s_addr)
986 continue;
987
988 /*
989 * if we got a matching prefix route, move IFA_ROUTE to him
990 */
991 if ((ia->ia_flags & IFA_ROUTE) == 0) {
992 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
993 rtinitflags(target));
994 target->ia_flags &= ~IFA_ROUTE;
995
996 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
997 rtinitflags(ia) | RTF_UP);
998 if (error == 0)
999 ia->ia_flags |= IFA_ROUTE;
1000 return error;
1001 }
1002 }
1003
1004 /*
1005 * noone seem to have prefix route. remove it.
1006 */
1007 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1008 target->ia_flags &= ~IFA_ROUTE;
1009 return 0;
1010 }
1011
1012 #undef rtinitflags
1013
1014 /*
1015 * Return 1 if the address might be a local broadcast address.
1016 */
1017 int
1018 in_broadcast(struct in_addr in, struct ifnet *ifp)
1019 {
1020 struct ifaddr *ifa;
1021
1022 if (in.s_addr == INADDR_BROADCAST ||
1023 in_nullhost(in))
1024 return 1;
1025 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1026 return 0;
1027 /*
1028 * Look through the list of addresses for a match
1029 * with a broadcast address.
1030 */
1031 #define ia (ifatoia(ifa))
1032 IFADDR_FOREACH(ifa, ifp)
1033 if (ifa->ifa_addr->sa_family == AF_INET &&
1034 !in_hosteq(in, ia->ia_addr.sin_addr) &&
1035 (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1036 in_hosteq(in, ia->ia_netbroadcast) ||
1037 (hostzeroisbroadcast &&
1038 /*
1039 * Check for old-style (host 0) broadcast.
1040 */
1041 (in.s_addr == ia->ia_subnet ||
1042 in.s_addr == ia->ia_net))))
1043 return 1;
1044 return (0);
1045 #undef ia
1046 }
1047
1048 /*
1049 * Add an address to the list of IP multicast addresses for a given interface.
1050 */
1051 struct in_multi *
1052 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1053 {
1054 struct in_multi *inm;
1055 struct ifreq ifr;
1056 int s = splsoftnet();
1057
1058 /*
1059 * See if address already in list.
1060 */
1061 IN_LOOKUP_MULTI(*ap, ifp, inm);
1062 if (inm != NULL) {
1063 /*
1064 * Found it; just increment the reference count.
1065 */
1066 ++inm->inm_refcount;
1067 } else {
1068 /*
1069 * New address; allocate a new multicast record
1070 * and link it into the interface's multicast list.
1071 */
1072 inm = pool_get(&inmulti_pool, PR_NOWAIT);
1073 if (inm == NULL) {
1074 splx(s);
1075 return (NULL);
1076 }
1077 inm->inm_addr = *ap;
1078 inm->inm_ifp = ifp;
1079 inm->inm_refcount = 1;
1080 LIST_INSERT_HEAD(
1081 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1082 inm, inm_list);
1083 /*
1084 * Ask the network driver to update its multicast reception
1085 * filter appropriately for the new address.
1086 */
1087 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
1088 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
1089 satosin(&ifr.ifr_addr)->sin_addr = *ap;
1090 if ((ifp->if_ioctl == NULL) ||
1091 (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
1092 LIST_REMOVE(inm, inm_list);
1093 pool_put(&inmulti_pool, inm);
1094 splx(s);
1095 return (NULL);
1096 }
1097 /*
1098 * Let IGMP know that we have joined a new IP multicast group.
1099 */
1100 if (igmp_joingroup(inm) != 0) {
1101 LIST_REMOVE(inm, inm_list);
1102 pool_put(&inmulti_pool, inm);
1103 splx(s);
1104 return (NULL);
1105 }
1106 in_multientries++;
1107 }
1108 splx(s);
1109 return (inm);
1110 }
1111
1112 /*
1113 * Delete a multicast address record.
1114 */
1115 void
1116 in_delmulti(struct in_multi *inm)
1117 {
1118 struct ifreq ifr;
1119 int s = splsoftnet();
1120
1121 if (--inm->inm_refcount == 0) {
1122 /*
1123 * No remaining claims to this record; let IGMP know that
1124 * we are leaving the multicast group.
1125 */
1126 igmp_leavegroup(inm);
1127 /*
1128 * Unlink from list.
1129 */
1130 LIST_REMOVE(inm, inm_list);
1131 in_multientries--;
1132 /*
1133 * Notify the network driver to update its multicast reception
1134 * filter.
1135 */
1136 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
1137 satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
1138 (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
1139 (caddr_t)&ifr);
1140 pool_put(&inmulti_pool, inm);
1141 }
1142 splx(s);
1143 }
1144 #endif
1145