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