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