in.c revision 1.21 1 /* $NetBSD: in.c,v 1.21 1995/06/04 04:35:29 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)in.c 8.2 (Berkeley) 11/15/93
36 */
37
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/errno.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44
45 #include <net/if.h>
46 #include <net/route.h>
47
48 #include <netinet/in_systm.h>
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/if_ether.h>
52 #include <netinet/ip_mroute.h>
53
54 #include "ether.h"
55
56 #ifdef INET
57
58 #ifndef SUBNETSARELOCAL
59 #define SUBNETSARELOCAL 1
60 #endif
61 int subnetsarelocal = SUBNETSARELOCAL;
62 /*
63 * Return 1 if an internet address is for a ``local'' host
64 * (one to which we have a connection). If subnetsarelocal
65 * is true, this includes other subnets of the local net.
66 * Otherwise, it includes only the directly-connected (sub)nets.
67 */
68 int
69 in_localaddr(in)
70 struct in_addr in;
71 {
72 register struct in_ifaddr *ia;
73
74 if (subnetsarelocal) {
75 for (ia = in_ifaddr; ia; ia = ia->ia_next)
76 if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
77 return (1);
78 } else {
79 for (ia = in_ifaddr; ia; ia = ia->ia_next)
80 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
81 return (1);
82 }
83 return (0);
84 }
85
86 /*
87 * Determine whether an IP address is in a reserved set of addresses
88 * that may not be forwarded, or whether datagrams to that destination
89 * may be forwarded.
90 */
91 int
92 in_canforward(in)
93 struct in_addr in;
94 {
95 register u_int32_t net;
96
97 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
98 return (0);
99 if (IN_CLASSA(in.s_addr)) {
100 net = in.s_addr & IN_CLASSA_NET;
101 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
102 return (0);
103 }
104 return (1);
105 }
106
107 /*
108 * Trim a mask in a sockaddr
109 */
110 void
111 in_socktrim(ap)
112 struct sockaddr_in *ap;
113 {
114 register char *cplim = (char *) &ap->sin_addr;
115 register char *cp = (char *) (&ap->sin_addr + 1);
116
117 ap->sin_len = 0;
118 while (--cp >= cplim)
119 if (*cp) {
120 (ap)->sin_len = cp - (char *) (ap) + 1;
121 break;
122 }
123 }
124
125 int in_interfaces; /* number of external internet interfaces */
126 #define satosin(sa) ((struct sockaddr_in *)(sa))
127 #define sintosa(sin) ((struct sockaddr *)(sin))
128
129 /*
130 * Generic internet control operations (ioctl's).
131 * Ifp is 0 if not an interface-specific ioctl.
132 */
133 /* ARGSUSED */
134 int
135 in_control(so, cmd, data, ifp)
136 struct socket *so;
137 u_long cmd;
138 caddr_t data;
139 register struct ifnet *ifp;
140 {
141 register struct ifreq *ifr = (struct ifreq *)data;
142 register struct in_ifaddr *ia = 0;
143 register struct ifaddr *ifa;
144 struct in_ifaddr *oia;
145 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
146 struct sockaddr_in oldaddr;
147 int error, hostIsNew, maskIsNew;
148
149 /*
150 * Find address for this interface, if it exists.
151 */
152 if (ifp)
153 for (ia = in_ifaddr; ia; ia = ia->ia_next)
154 if (ia->ia_ifp == ifp)
155 break;
156
157 switch (cmd) {
158
159 case SIOCAIFADDR:
160 case SIOCDIFADDR:
161 if (ifra->ifra_addr.sin_family == AF_INET)
162 for (oia = ia; ia; ia = ia->ia_next) {
163 if (ia->ia_ifp == ifp &&
164 ia->ia_addr.sin_addr.s_addr ==
165 ifra->ifra_addr.sin_addr.s_addr)
166 break;
167 }
168 if (cmd == SIOCDIFADDR && ia == 0)
169 return (EADDRNOTAVAIL);
170 /* FALLTHROUGH */
171 case SIOCSIFADDR:
172 case SIOCSIFNETMASK:
173 case SIOCSIFDSTADDR:
174 if ((so->so_state & SS_PRIV) == 0)
175 return (EPERM);
176
177 if (ifp == 0)
178 panic("in_control");
179 if (ia == (struct in_ifaddr *)0) {
180 oia = (struct in_ifaddr *)
181 malloc(sizeof *oia, M_IFADDR, M_WAITOK);
182 if (oia == (struct in_ifaddr *)NULL)
183 return (ENOBUFS);
184 bzero((caddr_t)oia, sizeof *oia);
185 if (ia = in_ifaddr) {
186 for ( ; ia->ia_next; ia = ia->ia_next)
187 continue;
188 ia->ia_next = oia;
189 } else
190 in_ifaddr = oia;
191 ia = oia;
192 if (ifa = ifp->if_addrlist) {
193 for ( ; ifa->ifa_next; ifa = ifa->ifa_next)
194 continue;
195 ifa->ifa_next = (struct ifaddr *) ia;
196 } else
197 ifp->if_addrlist = (struct ifaddr *) ia;
198 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
199 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
200 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
201 ia->ia_sockmask.sin_len = 8;
202 if (ifp->if_flags & IFF_BROADCAST) {
203 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
204 ia->ia_broadaddr.sin_family = AF_INET;
205 }
206 ia->ia_ifp = ifp;
207 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
208 in_interfaces++;
209 }
210 break;
211
212 case SIOCSIFBRDADDR:
213 if ((so->so_state & SS_PRIV) == 0)
214 return (EPERM);
215 /* FALLTHROUGH */
216
217 case SIOCGIFADDR:
218 case SIOCGIFNETMASK:
219 case SIOCGIFDSTADDR:
220 case SIOCGIFBRDADDR:
221 if (ia == (struct in_ifaddr *)0)
222 return (EADDRNOTAVAIL);
223 break;
224 }
225 switch (cmd) {
226
227 case SIOCGIFADDR:
228 *satosin(&ifr->ifr_addr) = ia->ia_addr;
229 break;
230
231 case SIOCGIFBRDADDR:
232 if ((ifp->if_flags & IFF_BROADCAST) == 0)
233 return (EINVAL);
234 *satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
235 break;
236
237 case SIOCGIFDSTADDR:
238 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
239 return (EINVAL);
240 *satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
241 break;
242
243 case SIOCGIFNETMASK:
244 *satosin(&ifr->ifr_addr) = ia->ia_sockmask;
245 break;
246
247 case SIOCSIFDSTADDR:
248 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
249 return (EINVAL);
250 oldaddr = ia->ia_dstaddr;
251 ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
252 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
253 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
254 ia->ia_dstaddr = oldaddr;
255 return (error);
256 }
257 if (ia->ia_flags & IFA_ROUTE) {
258 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
259 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
260 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
261 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
262 }
263 break;
264
265 case SIOCSIFBRDADDR:
266 if ((ifp->if_flags & IFF_BROADCAST) == 0)
267 return (EINVAL);
268 ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
269 break;
270
271 case SIOCSIFADDR:
272 return (in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1));
273
274 case SIOCSIFNETMASK:
275 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr =
276 ifra->ifra_addr.sin_addr.s_addr;
277 break;
278
279 case SIOCAIFADDR:
280 maskIsNew = 0;
281 hostIsNew = 1;
282 error = 0;
283 if (ia->ia_addr.sin_family == AF_INET) {
284 if (ifra->ifra_addr.sin_len == 0) {
285 ifra->ifra_addr = ia->ia_addr;
286 hostIsNew = 0;
287 } else if (ifra->ifra_addr.sin_addr.s_addr ==
288 ia->ia_addr.sin_addr.s_addr)
289 hostIsNew = 0;
290 }
291 if (ifra->ifra_mask.sin_len) {
292 in_ifscrub(ifp, ia);
293 ia->ia_sockmask = ifra->ifra_mask;
294 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
295 maskIsNew = 1;
296 }
297 if ((ifp->if_flags & IFF_POINTOPOINT) &&
298 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
299 in_ifscrub(ifp, ia);
300 ia->ia_dstaddr = ifra->ifra_dstaddr;
301 maskIsNew = 1; /* We lie; but the effect's the same */
302 }
303 if (ifra->ifra_addr.sin_family == AF_INET &&
304 (hostIsNew || maskIsNew))
305 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
306 if ((ifp->if_flags & IFF_BROADCAST) &&
307 (ifra->ifra_broadaddr.sin_family == AF_INET))
308 ia->ia_broadaddr = ifra->ifra_broadaddr;
309 return (error);
310
311 case SIOCDIFADDR:
312 in_ifscrub(ifp, ia);
313 if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia)
314 ifp->if_addrlist = ifa->ifa_next;
315 else {
316 while (ifa->ifa_next &&
317 (ifa->ifa_next != (struct ifaddr *)ia))
318 ifa = ifa->ifa_next;
319 if (ifa->ifa_next)
320 ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next;
321 else
322 printf("Couldn't unlink inifaddr from ifp\n");
323 }
324 oia = ia;
325 if (oia == (ia = in_ifaddr))
326 in_ifaddr = ia->ia_next;
327 else {
328 while (ia->ia_next && (ia->ia_next != oia))
329 ia = ia->ia_next;
330 if (ia->ia_next)
331 ia->ia_next = oia->ia_next;
332 else
333 printf("Didn't unlink inifadr from list\n");
334 }
335 IFAFREE((&oia->ia_ifa));
336 break;
337
338 #ifdef MROUTING
339 case SIOCGETVIFCNT:
340 case SIOCGETSGCNT:
341 return (mrt_ioctl(cmd, data));
342 #endif /* MROUTING */
343
344 default:
345 if (ifp == 0 || ifp->if_ioctl == 0)
346 return (EOPNOTSUPP);
347 return ((*ifp->if_ioctl)(ifp, cmd, data));
348 }
349 return (0);
350 }
351
352 /*
353 * Delete any existing route for an interface.
354 */
355 void
356 in_ifscrub(ifp, ia)
357 register struct ifnet *ifp;
358 register struct in_ifaddr *ia;
359 {
360
361 if ((ia->ia_flags & IFA_ROUTE) == 0)
362 return;
363 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
364 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
365 else
366 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
367 ia->ia_flags &= ~IFA_ROUTE;
368 }
369
370 /*
371 * Initialize an interface's internet address
372 * and routing table entry.
373 */
374 int
375 in_ifinit(ifp, ia, sin, scrub)
376 register struct ifnet *ifp;
377 register struct in_ifaddr *ia;
378 struct sockaddr_in *sin;
379 int scrub;
380 {
381 register u_int32_t i = sin->sin_addr.s_addr;
382 struct sockaddr_in oldaddr;
383 int s = splimp(), flags = RTF_UP, error, ether_output();
384
385 oldaddr = ia->ia_addr;
386 ia->ia_addr = *sin;
387 /*
388 * Give the interface a chance to initialize
389 * if this is its first address,
390 * and to validate the address if necessary.
391 */
392 if (ifp->if_ioctl &&
393 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
394 splx(s);
395 ia->ia_addr = oldaddr;
396 return (error);
397 }
398 splx(s);
399 if (scrub) {
400 ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
401 in_ifscrub(ifp, ia);
402 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
403 }
404 if (IN_CLASSA(i))
405 ia->ia_netmask = IN_CLASSA_NET;
406 else if (IN_CLASSB(i))
407 ia->ia_netmask = IN_CLASSB_NET;
408 else
409 ia->ia_netmask = IN_CLASSC_NET;
410 /*
411 * The subnet mask usually includes at least the standard network part,
412 * but may may be smaller in the case of supernetting.
413 * If it is set, we believe it.
414 */
415 if (ia->ia_subnetmask == 0) {
416 ia->ia_subnetmask = ia->ia_netmask;
417 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
418 } else
419 ia->ia_netmask &= ia->ia_subnetmask;
420 ia->ia_net = i & ia->ia_netmask;
421 ia->ia_subnet = i & ia->ia_subnetmask;
422 in_socktrim(&ia->ia_sockmask);
423 /*
424 * Add route for the network.
425 */
426 ia->ia_ifa.ifa_metric = ifp->if_metric;
427 if (ifp->if_flags & IFF_BROADCAST) {
428 ia->ia_broadaddr.sin_addr.s_addr =
429 ia->ia_subnet | ~ia->ia_subnetmask;
430 ia->ia_netbroadcast.s_addr =
431 ia->ia_net | ~ia->ia_netmask;
432 } else if (ifp->if_flags & IFF_LOOPBACK) {
433 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
434 flags |= RTF_HOST;
435 } else if (ifp->if_flags & IFF_POINTOPOINT) {
436 if (ia->ia_dstaddr.sin_family != AF_INET)
437 return (0);
438 flags |= RTF_HOST;
439 }
440 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
441 ia->ia_flags |= IFA_ROUTE;
442 /*
443 * If the interface supports multicast, join the "all hosts"
444 * multicast group on that interface.
445 */
446 if (ifp->if_flags & IFF_MULTICAST) {
447 struct in_addr addr;
448
449 addr.s_addr = INADDR_ALLHOSTS_GROUP;
450 in_addmulti(&addr, ifp);
451 }
452 return (error);
453 }
454
455
456 /*
457 * Return 1 if the address might be a local broadcast address.
458 */
459 int
460 in_broadcast(in, ifp)
461 struct in_addr in;
462 struct ifnet *ifp;
463 {
464 register struct ifaddr *ifa;
465
466 if (in.s_addr == INADDR_BROADCAST ||
467 in.s_addr == INADDR_ANY)
468 return 1;
469 if ((ifp->if_flags & IFF_BROADCAST) == 0)
470 return 0;
471 /*
472 * Look through the list of addresses for a match
473 * with a broadcast address.
474 */
475 #define ia ((struct in_ifaddr *)ifa)
476 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
477 if (ifa->ifa_addr->sa_family == AF_INET &&
478 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
479 in.s_addr == ia->ia_netbroadcast.s_addr ||
480 /*
481 * Check for old-style (host 0) broadcast.
482 */
483 in.s_addr == ia->ia_subnet ||
484 in.s_addr == ia->ia_net))
485 return 1;
486 return (0);
487 #undef ia
488 }
489
490 /*
491 * Add an address to the list of IP multicast addresses for a given interface.
492 */
493 struct in_multi *
494 in_addmulti(ap, ifp)
495 register struct in_addr *ap;
496 register struct ifnet *ifp;
497 {
498 register struct in_multi *inm;
499 struct ifreq ifr;
500 struct in_ifaddr *ia;
501 int s = splnet();
502
503 /*
504 * See if address already in list.
505 */
506 IN_LOOKUP_MULTI(*ap, ifp, inm);
507 if (inm != NULL) {
508 /*
509 * Found it; just increment the reference count.
510 */
511 ++inm->inm_refcount;
512 }
513 else {
514 /*
515 * New address; allocate a new multicast record
516 * and link it into the interface's multicast list.
517 */
518 inm = (struct in_multi *)malloc(sizeof(*inm),
519 M_IPMADDR, M_NOWAIT);
520 if (inm == NULL) {
521 splx(s);
522 return (NULL);
523 }
524 inm->inm_addr = *ap;
525 inm->inm_ifp = ifp;
526 inm->inm_refcount = 1;
527 IFP_TO_IA(ifp, ia);
528 if (ia == NULL) {
529 free(inm, M_IPMADDR);
530 splx(s);
531 return (NULL);
532 }
533 inm->inm_ia = ia;
534 inm->inm_next = ia->ia_multiaddrs;
535 ia->ia_multiaddrs = inm;
536 /*
537 * Ask the network driver to update its multicast reception
538 * filter appropriately for the new address.
539 */
540 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
541 satosin(&ifr.ifr_addr)->sin_addr = *ap;
542 if ((ifp->if_ioctl == NULL) ||
543 (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
544 ia->ia_multiaddrs = inm->inm_next;
545 free(inm, M_IPMADDR);
546 splx(s);
547 return (NULL);
548 }
549 /*
550 * Let IGMP know that we have joined a new IP multicast group.
551 */
552 igmp_joingroup(inm);
553 }
554 splx(s);
555 return (inm);
556 }
557
558 /*
559 * Delete a multicast address record.
560 */
561 int
562 in_delmulti(inm)
563 register struct in_multi *inm;
564 {
565 register struct in_multi **p;
566 struct ifreq ifr;
567 int s = splnet();
568
569 if (--inm->inm_refcount == 0) {
570 /*
571 * No remaining claims to this record; let IGMP know that
572 * we are leaving the multicast group.
573 */
574 igmp_leavegroup(inm);
575 /*
576 * Unlink from list.
577 */
578 for (p = &inm->inm_ia->ia_multiaddrs;
579 *p != inm;
580 p = &(*p)->inm_next)
581 continue;
582 *p = (*p)->inm_next;
583 /*
584 * Notify the network driver to update its multicast reception
585 * filter.
586 */
587 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
588 satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
589 (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
590 (caddr_t)&ifr);
591 free(inm, M_IPMADDR);
592 }
593 splx(s);
594 }
595
596 #endif
597