Annotation of src/sys/netinet/ip_input.c, Revision 1.99
1.99 ! thorpej 1: /* $NetBSD: ip_input.c,v 1.98 2000/02/12 17:45:44 thorpej Exp $ */
1.89 itojun 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: */
1.76 thorpej 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: */
1.14 cgd 68:
1.1 cgd 69: /*
1.13 mycroft 70: * Copyright (c) 1982, 1986, 1988, 1993
71: * The Regents of the University of California. All rights reserved.
1.1 cgd 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. All advertising materials mentioning features or use of this software
82: * must display the following acknowledgement:
83: * This product includes software developed by the University of
84: * California, Berkeley and its contributors.
85: * 4. Neither the name of the University nor the names of its contributors
86: * may be used to endorse or promote products derived from this software
87: * without specific prior written permission.
88: *
89: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99: * SUCH DAMAGE.
100: *
1.14 cgd 101: * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
1.1 cgd 102: */
1.55 scottr 103:
1.62 matt 104: #include "opt_gateway.h"
1.69 mrg 105: #include "opt_pfil_hooks.h"
1.91 thorpej 106: #include "opt_ipsec.h"
1.55 scottr 107: #include "opt_mrouting.h"
1.1 cgd 108:
1.5 mycroft 109: #include <sys/param.h>
110: #include <sys/systm.h>
111: #include <sys/malloc.h>
112: #include <sys/mbuf.h>
113: #include <sys/domain.h>
114: #include <sys/protosw.h>
115: #include <sys/socket.h>
1.44 thorpej 116: #include <sys/socketvar.h>
1.5 mycroft 117: #include <sys/errno.h>
118: #include <sys/time.h>
119: #include <sys/kernel.h>
1.28 christos 120: #include <sys/proc.h>
1.72 thorpej 121: #include <sys/pool.h>
1.28 christos 122:
123: #include <vm/vm.h>
124: #include <sys/sysctl.h>
1.1 cgd 125:
1.5 mycroft 126: #include <net/if.h>
1.44 thorpej 127: #include <net/if_dl.h>
1.5 mycroft 128: #include <net/route.h>
1.45 mrg 129: #include <net/pfil.h>
1.1 cgd 130:
1.5 mycroft 131: #include <netinet/in.h>
132: #include <netinet/in_systm.h>
133: #include <netinet/ip.h>
134: #include <netinet/in_pcb.h>
135: #include <netinet/in_var.h>
136: #include <netinet/ip_var.h>
137: #include <netinet/ip_icmp.h>
1.89 itojun 138: /* just for gif_ttl */
139: #include <netinet/in_gif.h>
140: #include "gif.h"
141:
142: #ifdef IPSEC
143: #include <netinet6/ipsec.h>
144: #include <netkey/key.h>
145: #include <netkey/key_debug.h>
146: #endif
1.44 thorpej 147:
1.1 cgd 148: #ifndef IPFORWARDING
149: #ifdef GATEWAY
150: #define IPFORWARDING 1 /* forward IP packets not for us */
151: #else /* GATEWAY */
152: #define IPFORWARDING 0 /* don't forward IP packets not for us */
153: #endif /* GATEWAY */
154: #endif /* IPFORWARDING */
155: #ifndef IPSENDREDIRECTS
156: #define IPSENDREDIRECTS 1
157: #endif
1.26 thorpej 158: #ifndef IPFORWSRCRT
1.47 cjs 159: #define IPFORWSRCRT 1 /* forward source-routed packets */
160: #endif
161: #ifndef IPALLOWSRCRT
1.48 mrg 162: #define IPALLOWSRCRT 1 /* allow source-routed packets */
1.26 thorpej 163: #endif
1.53 kml 164: #ifndef IPMTUDISC
165: #define IPMTUDISC 0
166: #endif
1.60 kml 167: #ifndef IPMTUDISCTIMEOUT
1.61 kml 168: #define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */
1.60 kml 169: #endif
1.53 kml 170:
1.27 thorpej 171: /*
172: * Note: DIRECTED_BROADCAST is handled this way so that previous
173: * configuration using this option will Just Work.
174: */
175: #ifndef IPDIRECTEDBCAST
176: #ifdef DIRECTED_BROADCAST
177: #define IPDIRECTEDBCAST 1
178: #else
179: #define IPDIRECTEDBCAST 0
180: #endif /* DIRECTED_BROADCAST */
181: #endif /* IPDIRECTEDBCAST */
1.1 cgd 182: int ipforwarding = IPFORWARDING;
183: int ipsendredirects = IPSENDREDIRECTS;
1.13 mycroft 184: int ip_defttl = IPDEFTTL;
1.26 thorpej 185: int ip_forwsrcrt = IPFORWSRCRT;
1.27 thorpej 186: int ip_directedbcast = IPDIRECTEDBCAST;
1.47 cjs 187: int ip_allowsrcrt = IPALLOWSRCRT;
1.53 kml 188: int ip_mtudisc = IPMTUDISC;
1.60 kml 189: u_int ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
1.1 cgd 190: #ifdef DIAGNOSTIC
191: int ipprintfs = 0;
192: #endif
193:
1.60 kml 194: struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
195:
1.1 cgd 196: extern struct domain inetdomain;
197: extern struct protosw inetsw[];
198: u_char ip_protox[IPPROTO_MAX];
199: int ipqmaxlen = IFQ_MAXLEN;
1.22 mycroft 200: struct in_ifaddrhead in_ifaddr;
1.57 tls 201: struct in_ifaddrhashhead *in_ifaddrhashtbl;
1.13 mycroft 202: struct ifqueue ipintrq;
1.63 matt 203: struct ipstat ipstat;
204: u_int16_t ip_id;
205: int ip_defttl;
1.75 thorpej 206:
1.63 matt 207: struct ipqhead ipq;
1.75 thorpej 208: int ipq_locked;
209:
210: static __inline int ipq_lock_try __P((void));
211: static __inline void ipq_unlock __P((void));
212:
213: static __inline int
214: ipq_lock_try()
215: {
216: int s;
217:
218: s = splimp();
219: if (ipq_locked) {
220: splx(s);
221: return (0);
222: }
223: ipq_locked = 1;
224: splx(s);
225: return (1);
226: }
227:
228: static __inline void
229: ipq_unlock()
230: {
231: int s;
232:
233: s = splimp();
234: ipq_locked = 0;
235: splx(s);
236: }
237:
238: #ifdef DIAGNOSTIC
239: #define IPQ_LOCK() \
240: do { \
241: if (ipq_lock_try() == 0) { \
242: printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
243: panic("ipq_lock"); \
244: } \
245: } while (0)
246: #define IPQ_LOCK_CHECK() \
247: do { \
248: if (ipq_locked == 0) { \
249: printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
250: panic("ipq lock check"); \
251: } \
252: } while (0)
253: #else
254: #define IPQ_LOCK() (void) ipq_lock_try()
255: #define IPQ_LOCK_CHECK() /* nothing */
256: #endif
257:
258: #define IPQ_UNLOCK() ipq_unlock()
1.1 cgd 259:
1.72 thorpej 260: struct pool ipqent_pool;
261:
1.1 cgd 262: /*
263: * We need to save the IP options in case a protocol wants to respond
264: * to an incoming packet over the same route if the packet got here
265: * using IP source routing. This allows connection establishment and
266: * maintenance when the remote end is on a network that is not known
267: * to us.
268: */
269: int ip_nhops = 0;
270: static struct ip_srcrt {
271: struct in_addr dst; /* final destination */
272: char nop; /* one NOP to align */
273: char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
274: struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
275: } ip_srcrt;
276:
1.13 mycroft 277: static void save_rte __P((u_char *, struct in_addr));
1.35 mycroft 278:
1.1 cgd 279: /*
280: * IP initialization: fill in IP protocol switch table.
281: * All protocols not implemented in kernel go to raw IP protocol handler.
282: */
1.8 mycroft 283: void
1.1 cgd 284: ip_init()
285: {
286: register struct protosw *pr;
287: register int i;
288:
1.72 thorpej 289: pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl",
290: 0, NULL, NULL, M_IPQ);
291:
1.1 cgd 292: pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
293: if (pr == 0)
294: panic("ip_init");
295: for (i = 0; i < IPPROTO_MAX; i++)
296: ip_protox[i] = pr - inetsw;
297: for (pr = inetdomain.dom_protosw;
298: pr < inetdomain.dom_protoswNPROTOSW; pr++)
299: if (pr->pr_domain->dom_family == PF_INET &&
300: pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
301: ip_protox[pr->pr_protocol] = pr - inetsw;
1.25 cgd 302: LIST_INIT(&ipq);
1.1 cgd 303: ip_id = time.tv_sec & 0xffff;
304: ipintrq.ifq_maxlen = ipqmaxlen;
1.22 mycroft 305: TAILQ_INIT(&in_ifaddr);
1.57 tls 306: in_ifaddrhashtbl =
307: hashinit(IN_IFADDR_HASH_SIZE, M_IFADDR, M_WAITOK, &in_ifaddrhash);
1.60 kml 308: if (ip_mtudisc != 0)
309: ip_mtudisc_timeout_q =
310: rt_timer_queue_create(ip_mtudisc_timeout);
1.73 thorpej 311: #ifdef GATEWAY
312: ipflow_init();
313: #endif
1.1 cgd 314: }
315:
316: struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
317: struct route ipforward_rt;
318:
319: /*
1.89 itojun 320: * IP software interrupt routine
321: */
322: void
323: ipintr()
324: {
325: int s;
326: struct mbuf *m;
327:
328: while (1) {
329: s = splimp();
330: IF_DEQUEUE(&ipintrq, m);
331: splx(s);
332: if (m == 0)
333: return;
334: ip_input(m);
335: }
336: }
337:
338: /*
1.1 cgd 339: * Ip input routine. Checksum and byte swap header. If fragmented
340: * try to reassemble. Process options. Pass to next level.
341: */
1.8 mycroft 342: void
1.89 itojun 343: ip_input(struct mbuf *m)
1.1 cgd 344: {
1.33 mrg 345: register struct ip *ip = NULL;
1.1 cgd 346: register struct ipq *fp;
347: register struct in_ifaddr *ia;
1.57 tls 348: register struct ifaddr *ifa;
1.25 cgd 349: struct ipqent *ipqe;
1.89 itojun 350: int hlen = 0, mff, len;
1.36 mrg 351: #ifdef PFIL_HOOKS
1.33 mrg 352: struct packet_filter_hook *pfh;
353: struct mbuf *m0;
1.43 mrg 354: int rv;
1.36 mrg 355: #endif /* PFIL_HOOKS */
1.1 cgd 356:
357: #ifdef DIAGNOSTIC
358: if ((m->m_flags & M_PKTHDR) == 0)
359: panic("ipintr no HDR");
360: #endif
1.89 itojun 361: #ifdef IPSEC
362: /*
363: * should the inner packet be considered authentic?
364: * see comment in ah4_input().
365: */
366: if (m) {
367: m->m_flags &= ~M_AUTHIPHDR;
368: m->m_flags &= ~M_AUTHIPDGM;
369: }
370: #endif
1.1 cgd 371: /*
372: * If no IP addresses have been set yet but the interfaces
373: * are receiving, can't do anything with incoming packets yet.
374: */
1.22 mycroft 375: if (in_ifaddr.tqh_first == 0)
1.1 cgd 376: goto bad;
377: ipstat.ips_total++;
378: if (m->m_len < sizeof (struct ip) &&
379: (m = m_pullup(m, sizeof (struct ip))) == 0) {
380: ipstat.ips_toosmall++;
1.89 itojun 381: return;
1.1 cgd 382: }
383: ip = mtod(m, struct ip *);
1.13 mycroft 384: if (ip->ip_v != IPVERSION) {
385: ipstat.ips_badvers++;
386: goto bad;
387: }
1.1 cgd 388: hlen = ip->ip_hl << 2;
389: if (hlen < sizeof(struct ip)) { /* minimum header length */
390: ipstat.ips_badhlen++;
391: goto bad;
392: }
393: if (hlen > m->m_len) {
394: if ((m = m_pullup(m, hlen)) == 0) {
395: ipstat.ips_badhlen++;
1.89 itojun 396: return;
1.1 cgd 397: }
398: ip = mtod(m, struct ip *);
399: }
1.98 thorpej 400:
1.85 hwr 401: /*
1.99 ! thorpej 402: * RFC1122: packets with a multicast source address are
1.98 thorpej 403: * not allowed.
1.85 hwr 404: */
405: if (IN_MULTICAST(ip->ip_src.s_addr)) {
1.98 thorpej 406: /* XXX stat */
1.85 hwr 407: goto bad;
408: }
409:
1.78 mycroft 410: if (in_cksum(m, hlen) != 0) {
1.1 cgd 411: ipstat.ips_badsum++;
412: goto bad;
413: }
414:
415: /*
416: * Convert fields to host representation.
417: */
418: NTOHS(ip->ip_len);
419: NTOHS(ip->ip_off);
1.35 mycroft 420: len = ip->ip_len;
1.81 proff 421:
422: /*
423: * Check for additional length bogosity
424: */
1.84 proff 425: if (len < hlen) {
1.81 proff 426: ipstat.ips_badlen++;
427: goto bad;
428: }
1.1 cgd 429:
430: /*
431: * Check that the amount of data in the buffers
432: * is as at least much as the IP header would have us expect.
433: * Trim mbufs if longer than we expect.
434: * Drop packet if shorter than we expect.
435: */
1.35 mycroft 436: if (m->m_pkthdr.len < len) {
1.1 cgd 437: ipstat.ips_tooshort++;
438: goto bad;
439: }
1.35 mycroft 440: if (m->m_pkthdr.len > len) {
1.1 cgd 441: if (m->m_len == m->m_pkthdr.len) {
1.35 mycroft 442: m->m_len = len;
443: m->m_pkthdr.len = len;
1.1 cgd 444: } else
1.35 mycroft 445: m_adj(m, len - m->m_pkthdr.len);
1.1 cgd 446: }
447:
1.94 itojun 448: #ifdef IPSEC
449: /* ipflow (IP fast fowarding) is not compatible with IPsec. */
450: m->m_flags &= ~M_CANFASTFWD;
451: #else
1.64 thorpej 452: /*
453: * Assume that we can create a fast-forward IP flow entry
454: * based on this packet.
455: */
456: m->m_flags |= M_CANFASTFWD;
1.94 itojun 457: #endif
1.64 thorpej 458:
1.36 mrg 459: #ifdef PFIL_HOOKS
1.33 mrg 460: /*
1.64 thorpej 461: * Run through list of hooks for input packets. If there are any
462: * filters which require that additional packets in the flow are
463: * not fast-forwarded, they must clear the M_CANFASTFWD flag.
464: * Note that filters must _never_ set this flag, as another filter
465: * in the list may have previously cleared it.
1.33 mrg 466: */
467: m0 = m;
1.59 mrg 468: for (pfh = pfil_hook_get(PFIL_IN); pfh; pfh = pfh->pfil_link.tqe_next)
1.33 mrg 469: if (pfh->pfil_func) {
1.43 mrg 470: rv = pfh->pfil_func(ip, hlen, m->m_pkthdr.rcvif, 0, &m0);
471: if (rv)
1.89 itojun 472: return;
1.68 sommerfe 473: m = m0;
474: if (m == NULL)
1.89 itojun 475: return;
1.68 sommerfe 476: ip = mtod(m, struct ip *);
1.33 mrg 477: }
1.36 mrg 478: #endif /* PFIL_HOOKS */
1.33 mrg 479:
1.1 cgd 480: /*
481: * Process options and, if not destined for us,
482: * ship it on. ip_dooptions returns 1 when an
483: * error was detected (causing an icmp message
484: * to be sent and the original packet to be freed).
485: */
486: ip_nhops = 0; /* for source routed packets */
487: if (hlen > sizeof (struct ip) && ip_dooptions(m))
1.89 itojun 488: return;
1.1 cgd 489:
490: /*
491: * Check our list of addresses, to see if the packet is for us.
492: */
1.97 itojun 493: for (ia = IN_IFADDR_HASH(ip->ip_dst.s_addr).lh_first;
494: ia != NULL;
495: ia = ia->ia_hash.le_next) {
496: if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
497: if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
498: break;
499: else {
500: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST,
501: 0, m->m_pkthdr.rcvif);
502: return;
503: }
504: }
505: }
1.86 thorpej 506: if (ia != NULL)
507: goto ours;
1.57 tls 508: if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
509: for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
510: ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
511: if (ifa->ifa_addr->sa_family != AF_INET) continue;
512: ia = ifatoia(ifa);
1.35 mycroft 513: if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
514: in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
1.20 mycroft 515: /*
516: * Look for all-0's host part (old broadcast addr),
517: * either for subnet or net.
518: */
519: ip->ip_dst.s_addr == ia->ia_subnet ||
1.18 mycroft 520: ip->ip_dst.s_addr == ia->ia_net)
1.1 cgd 521: goto ours;
1.57 tls 522: /*
523: * An interface with IP address zero accepts
524: * all packets that arrive on that interface.
525: */
526: if (in_nullhost(ia->ia_addr.sin_addr))
527: goto ours;
1.1 cgd 528: }
529: }
1.18 mycroft 530: if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4 hpeyerl 531: struct in_multi *inm;
532: #ifdef MROUTING
533: extern struct socket *ip_mrouter;
1.10 brezak 534:
535: if (m->m_flags & M_EXT) {
536: if ((m = m_pullup(m, hlen)) == 0) {
537: ipstat.ips_toosmall++;
1.89 itojun 538: return;
1.10 brezak 539: }
540: ip = mtod(m, struct ip *);
541: }
1.4 hpeyerl 542:
543: if (ip_mrouter) {
544: /*
545: * If we are acting as a multicast router, all
546: * incoming multicast packets are passed to the
547: * kernel-level multicast forwarding function.
548: * The packet is returned (relatively) intact; if
549: * ip_mforward() returns a non-zero value, the packet
550: * must be discarded, else it may be accepted below.
551: *
552: * (The IP ident field is put in the same byte order
553: * as expected when ip_mforward() is called from
554: * ip_output().)
555: */
1.13 mycroft 556: if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
557: ipstat.ips_cantforward++;
1.4 hpeyerl 558: m_freem(m);
1.89 itojun 559: return;
1.4 hpeyerl 560: }
561:
562: /*
563: * The process-level routing demon needs to receive
564: * all multicast IGMP packets, whether or not this
565: * host belongs to their destination groups.
566: */
567: if (ip->ip_p == IPPROTO_IGMP)
568: goto ours;
1.13 mycroft 569: ipstat.ips_forward++;
1.4 hpeyerl 570: }
571: #endif
572: /*
573: * See if we belong to the destination multicast group on the
574: * arrival interface.
575: */
576: IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
577: if (inm == NULL) {
1.13 mycroft 578: ipstat.ips_cantforward++;
1.4 hpeyerl 579: m_freem(m);
1.89 itojun 580: return;
1.4 hpeyerl 581: }
582: goto ours;
583: }
1.19 mycroft 584: if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
1.35 mycroft 585: in_nullhost(ip->ip_dst))
1.1 cgd 586: goto ours;
587:
588: /*
589: * Not for us; forward if possible and desirable.
590: */
591: if (ipforwarding == 0) {
592: ipstat.ips_cantforward++;
593: m_freem(m);
594: } else
595: ip_forward(m, 0);
1.89 itojun 596: return;
1.1 cgd 597:
598: ours:
599: /*
600: * If offset or IP_MF are set, must reassemble.
601: * Otherwise, nothing need be done.
602: * (We could look in the reassembly queue to see
603: * if the packet was previously fragmented,
604: * but it's not worth the time; just let them time out.)
605: */
1.37 perry 606: if (ip->ip_off & ~(IP_DF|IP_RF)) {
1.1 cgd 607: /*
608: * Look for queue of fragments
609: * of this datagram.
610: */
1.75 thorpej 611: IPQ_LOCK();
1.25 cgd 612: for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
1.1 cgd 613: if (ip->ip_id == fp->ipq_id &&
1.35 mycroft 614: in_hosteq(ip->ip_src, fp->ipq_src) &&
615: in_hosteq(ip->ip_dst, fp->ipq_dst) &&
1.1 cgd 616: ip->ip_p == fp->ipq_p)
617: goto found;
618: fp = 0;
619: found:
620:
621: /*
622: * Adjust ip_len to not reflect header,
1.25 cgd 623: * set ipqe_mff if more fragments are expected,
1.1 cgd 624: * convert offset of this to bytes.
625: */
626: ip->ip_len -= hlen;
1.25 cgd 627: mff = (ip->ip_off & IP_MF) != 0;
628: if (mff) {
1.16 cgd 629: /*
630: * Make sure that fragments have a data length
631: * that's a non-zero multiple of 8 bytes.
632: */
1.17 cgd 633: if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
1.16 cgd 634: ipstat.ips_badfrags++;
1.75 thorpej 635: IPQ_UNLOCK();
1.16 cgd 636: goto bad;
637: }
638: }
1.1 cgd 639: ip->ip_off <<= 3;
640:
641: /*
642: * If datagram marked as having more fragments
643: * or if this is not the first fragment,
644: * attempt reassembly; if it succeeds, proceed.
645: */
1.25 cgd 646: if (mff || ip->ip_off) {
1.1 cgd 647: ipstat.ips_fragments++;
1.72 thorpej 648: ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
1.25 cgd 649: if (ipqe == NULL) {
650: ipstat.ips_rcvmemdrop++;
1.75 thorpej 651: IPQ_UNLOCK();
1.25 cgd 652: goto bad;
653: }
654: ipqe->ipqe_mff = mff;
1.50 thorpej 655: ipqe->ipqe_m = m;
1.25 cgd 656: ipqe->ipqe_ip = ip;
1.50 thorpej 657: m = ip_reass(ipqe, fp);
1.75 thorpej 658: if (m == 0) {
659: IPQ_UNLOCK();
1.89 itojun 660: return;
1.75 thorpej 661: }
1.13 mycroft 662: ipstat.ips_reassembled++;
1.50 thorpej 663: ip = mtod(m, struct ip *);
1.74 thorpej 664: hlen = ip->ip_hl << 2;
1.79 mycroft 665: ip->ip_len += hlen;
1.1 cgd 666: } else
667: if (fp)
668: ip_freef(fp);
1.75 thorpej 669: IPQ_UNLOCK();
1.79 mycroft 670: }
1.1 cgd 671:
672: /*
673: * Switch out to protocol's input routine.
674: */
1.82 aidan 675: #if IFA_STATS
676: ia->ia_ifa.ifa_data.ifad_inbytes += ip->ip_len;
677: #endif
1.1 cgd 678: ipstat.ips_delivered++;
1.89 itojun 679: {
680: int off = hlen, nh = ip->ip_p;
681:
682: (*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
683: return;
684: }
1.1 cgd 685: bad:
686: m_freem(m);
687: }
688:
689: /*
690: * Take incoming datagram fragment and try to
691: * reassemble it into whole datagram. If a chain for
692: * reassembly of this datagram already exists, then it
693: * is given as fp; otherwise have to make a chain.
694: */
1.50 thorpej 695: struct mbuf *
1.25 cgd 696: ip_reass(ipqe, fp)
697: register struct ipqent *ipqe;
1.1 cgd 698: register struct ipq *fp;
699: {
1.50 thorpej 700: register struct mbuf *m = ipqe->ipqe_m;
1.25 cgd 701: register struct ipqent *nq, *p, *q;
702: struct ip *ip;
1.1 cgd 703: struct mbuf *t;
1.25 cgd 704: int hlen = ipqe->ipqe_ip->ip_hl << 2;
1.1 cgd 705: int i, next;
706:
1.75 thorpej 707: IPQ_LOCK_CHECK();
708:
1.1 cgd 709: /*
710: * Presence of header sizes in mbufs
711: * would confuse code below.
712: */
713: m->m_data += hlen;
714: m->m_len -= hlen;
715:
716: /*
717: * If first fragment to arrive, create a reassembly queue.
718: */
719: if (fp == 0) {
1.50 thorpej 720: MALLOC(fp, struct ipq *, sizeof (struct ipq),
721: M_FTABLE, M_NOWAIT);
722: if (fp == NULL)
1.1 cgd 723: goto dropfrag;
1.25 cgd 724: LIST_INSERT_HEAD(&ipq, fp, ipq_q);
1.1 cgd 725: fp->ipq_ttl = IPFRAGTTL;
1.25 cgd 726: fp->ipq_p = ipqe->ipqe_ip->ip_p;
727: fp->ipq_id = ipqe->ipqe_ip->ip_id;
728: LIST_INIT(&fp->ipq_fragq);
729: fp->ipq_src = ipqe->ipqe_ip->ip_src;
730: fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
731: p = NULL;
1.1 cgd 732: goto insert;
733: }
734:
735: /*
736: * Find a segment which begins after this one does.
737: */
1.25 cgd 738: for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
739: p = q, q = q->ipqe_q.le_next)
740: if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
1.1 cgd 741: break;
742:
743: /*
744: * If there is a preceding segment, it may provide some of
745: * our data already. If so, drop the data from the incoming
746: * segment. If it provides all of our data, drop us.
747: */
1.25 cgd 748: if (p != NULL) {
749: i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
750: ipqe->ipqe_ip->ip_off;
1.1 cgd 751: if (i > 0) {
1.25 cgd 752: if (i >= ipqe->ipqe_ip->ip_len)
1.1 cgd 753: goto dropfrag;
1.50 thorpej 754: m_adj(ipqe->ipqe_m, i);
1.25 cgd 755: ipqe->ipqe_ip->ip_off += i;
756: ipqe->ipqe_ip->ip_len -= i;
1.1 cgd 757: }
758: }
759:
760: /*
761: * While we overlap succeeding segments trim them or,
762: * if they are completely covered, dequeue them.
763: */
1.25 cgd 764: for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
765: q->ipqe_ip->ip_off; q = nq) {
766: i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
767: q->ipqe_ip->ip_off;
768: if (i < q->ipqe_ip->ip_len) {
769: q->ipqe_ip->ip_len -= i;
770: q->ipqe_ip->ip_off += i;
1.50 thorpej 771: m_adj(q->ipqe_m, i);
1.1 cgd 772: break;
773: }
1.25 cgd 774: nq = q->ipqe_q.le_next;
1.50 thorpej 775: m_freem(q->ipqe_m);
1.25 cgd 776: LIST_REMOVE(q, ipqe_q);
1.72 thorpej 777: pool_put(&ipqent_pool, q);
1.1 cgd 778: }
779:
780: insert:
781: /*
782: * Stick new segment in its place;
783: * check for complete reassembly.
784: */
1.25 cgd 785: if (p == NULL) {
786: LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
787: } else {
788: LIST_INSERT_AFTER(p, ipqe, ipqe_q);
789: }
1.1 cgd 790: next = 0;
1.25 cgd 791: for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
792: p = q, q = q->ipqe_q.le_next) {
793: if (q->ipqe_ip->ip_off != next)
1.1 cgd 794: return (0);
1.25 cgd 795: next += q->ipqe_ip->ip_len;
1.1 cgd 796: }
1.25 cgd 797: if (p->ipqe_mff)
1.1 cgd 798: return (0);
799:
800: /*
1.41 thorpej 801: * Reassembly is complete. Check for a bogus message size and
802: * concatenate fragments.
1.1 cgd 803: */
1.25 cgd 804: q = fp->ipq_fragq.lh_first;
805: ip = q->ipqe_ip;
1.41 thorpej 806: if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
807: ipstat.ips_toolong++;
808: ip_freef(fp);
809: return (0);
810: }
1.50 thorpej 811: m = q->ipqe_m;
1.1 cgd 812: t = m->m_next;
813: m->m_next = 0;
814: m_cat(m, t);
1.25 cgd 815: nq = q->ipqe_q.le_next;
1.72 thorpej 816: pool_put(&ipqent_pool, q);
1.25 cgd 817: for (q = nq; q != NULL; q = nq) {
1.50 thorpej 818: t = q->ipqe_m;
1.25 cgd 819: nq = q->ipqe_q.le_next;
1.72 thorpej 820: pool_put(&ipqent_pool, q);
1.1 cgd 821: m_cat(m, t);
822: }
823:
824: /*
825: * Create header for new ip packet by
826: * modifying header of first packet;
827: * dequeue and discard fragment reassembly header.
828: * Make header visible.
829: */
830: ip->ip_len = next;
1.89 itojun 831: ip->ip_ttl = 0; /* xxx */
832: ip->ip_sum = 0;
1.25 cgd 833: ip->ip_src = fp->ipq_src;
834: ip->ip_dst = fp->ipq_dst;
835: LIST_REMOVE(fp, ipq_q);
1.50 thorpej 836: FREE(fp, M_FTABLE);
1.1 cgd 837: m->m_len += (ip->ip_hl << 2);
838: m->m_data -= (ip->ip_hl << 2);
839: /* some debugging cruft by sklower, below, will go away soon */
840: if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
841: register int plen = 0;
1.50 thorpej 842: for (t = m; t; t = t->m_next)
843: plen += t->m_len;
844: m->m_pkthdr.len = plen;
1.1 cgd 845: }
1.50 thorpej 846: return (m);
1.1 cgd 847:
848: dropfrag:
849: ipstat.ips_fragdropped++;
850: m_freem(m);
1.72 thorpej 851: pool_put(&ipqent_pool, ipqe);
1.1 cgd 852: return (0);
853: }
854:
855: /*
856: * Free a fragment reassembly header and all
857: * associated datagrams.
858: */
1.8 mycroft 859: void
1.1 cgd 860: ip_freef(fp)
861: struct ipq *fp;
862: {
1.25 cgd 863: register struct ipqent *q, *p;
1.1 cgd 864:
1.75 thorpej 865: IPQ_LOCK_CHECK();
866:
1.25 cgd 867: for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
868: p = q->ipqe_q.le_next;
1.50 thorpej 869: m_freem(q->ipqe_m);
1.25 cgd 870: LIST_REMOVE(q, ipqe_q);
1.72 thorpej 871: pool_put(&ipqent_pool, q);
1.1 cgd 872: }
1.25 cgd 873: LIST_REMOVE(fp, ipq_q);
1.50 thorpej 874: FREE(fp, M_FTABLE);
1.1 cgd 875: }
876:
877: /*
878: * IP timer processing;
879: * if a timer expires on a reassembly
880: * queue, discard it.
881: */
1.8 mycroft 882: void
1.1 cgd 883: ip_slowtimo()
884: {
1.25 cgd 885: register struct ipq *fp, *nfp;
1.24 mycroft 886: int s = splsoftnet();
1.1 cgd 887:
1.75 thorpej 888: IPQ_LOCK();
1.25 cgd 889: for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
890: nfp = fp->ipq_q.le_next;
891: if (--fp->ipq_ttl == 0) {
1.1 cgd 892: ipstat.ips_fragtimeout++;
1.25 cgd 893: ip_freef(fp);
1.1 cgd 894: }
895: }
1.75 thorpej 896: IPQ_UNLOCK();
1.63 matt 897: #ifdef GATEWAY
898: ipflow_slowtimo();
899: #endif
1.1 cgd 900: splx(s);
901: }
902:
903: /*
904: * Drain off all datagram fragments.
905: */
1.8 mycroft 906: void
1.1 cgd 907: ip_drain()
908: {
909:
1.75 thorpej 910: /*
911: * We may be called from a device's interrupt context. If
912: * the ipq is already busy, just bail out now.
913: */
914: if (ipq_lock_try() == 0)
915: return;
916:
1.25 cgd 917: while (ipq.lh_first != NULL) {
1.1 cgd 918: ipstat.ips_fragdropped++;
1.25 cgd 919: ip_freef(ipq.lh_first);
1.1 cgd 920: }
1.75 thorpej 921:
922: IPQ_UNLOCK();
1.1 cgd 923: }
924:
925: /*
926: * Do option processing on a datagram,
927: * possibly discarding it if bad options are encountered,
928: * or forwarding it if source-routed.
929: * Returns 1 if packet has been forwarded/freed,
930: * 0 if the packet should be processed further.
931: */
1.8 mycroft 932: int
1.1 cgd 933: ip_dooptions(m)
934: struct mbuf *m;
935: {
936: register struct ip *ip = mtod(m, struct ip *);
937: register u_char *cp;
938: register struct ip_timestamp *ipt;
939: register struct in_ifaddr *ia;
940: int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1.13 mycroft 941: struct in_addr *sin, dst;
1.1 cgd 942: n_time ntime;
943:
1.13 mycroft 944: dst = ip->ip_dst;
1.1 cgd 945: cp = (u_char *)(ip + 1);
946: cnt = (ip->ip_hl << 2) - sizeof (struct ip);
947: for (; cnt > 0; cnt -= optlen, cp += optlen) {
948: opt = cp[IPOPT_OPTVAL];
949: if (opt == IPOPT_EOL)
950: break;
951: if (opt == IPOPT_NOP)
952: optlen = 1;
953: else {
954: optlen = cp[IPOPT_OLEN];
955: if (optlen <= 0 || optlen > cnt) {
956: code = &cp[IPOPT_OLEN] - (u_char *)ip;
957: goto bad;
958: }
959: }
960: switch (opt) {
961:
962: default:
963: break;
964:
965: /*
966: * Source routing with record.
967: * Find interface with current destination address.
968: * If none on this machine then drop if strictly routed,
969: * or do nothing if loosely routed.
970: * Record interface address and bring up next address
971: * component. If strictly routed make sure next
972: * address is on directly accessible net.
973: */
974: case IPOPT_LSRR:
975: case IPOPT_SSRR:
1.47 cjs 976: if (ip_allowsrcrt == 0) {
977: type = ICMP_UNREACH;
978: code = ICMP_UNREACH_NET_PROHIB;
979: goto bad;
980: }
1.1 cgd 981: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
982: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
983: goto bad;
984: }
985: ipaddr.sin_addr = ip->ip_dst;
1.19 mycroft 986: ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1.1 cgd 987: if (ia == 0) {
988: if (opt == IPOPT_SSRR) {
989: type = ICMP_UNREACH;
990: code = ICMP_UNREACH_SRCFAIL;
991: goto bad;
992: }
993: /*
994: * Loose routing, and not at next destination
995: * yet; nothing to do except forward.
996: */
997: break;
998: }
999: off--; /* 0 origin */
1000: if (off > optlen - sizeof(struct in_addr)) {
1001: /*
1002: * End of source route. Should be for us.
1003: */
1004: save_rte(cp, ip->ip_src);
1005: break;
1006: }
1007: /*
1008: * locate outgoing interface
1009: */
1010: bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
1011: sizeof(ipaddr.sin_addr));
1.96 thorpej 1012: if (opt == IPOPT_SSRR)
1013: ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1014: else
1.1 cgd 1015: ia = ip_rtaddr(ipaddr.sin_addr);
1016: if (ia == 0) {
1017: type = ICMP_UNREACH;
1018: code = ICMP_UNREACH_SRCFAIL;
1019: goto bad;
1020: }
1021: ip->ip_dst = ipaddr.sin_addr;
1.20 mycroft 1022: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 1023: (caddr_t)(cp + off), sizeof(struct in_addr));
1024: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13 mycroft 1025: /*
1026: * Let ip_intr's mcast routing check handle mcast pkts
1027: */
1.18 mycroft 1028: forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1 cgd 1029: break;
1030:
1031: case IPOPT_RR:
1032: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1033: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1034: goto bad;
1035: }
1036: /*
1037: * If no space remains, ignore.
1038: */
1039: off--; /* 0 origin */
1040: if (off > optlen - sizeof(struct in_addr))
1041: break;
1042: bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
1043: sizeof(ipaddr.sin_addr));
1044: /*
1045: * locate outgoing interface; if we're the destination,
1046: * use the incoming interface (should be same).
1047: */
1.96 thorpej 1048: if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
1049: == NULL &&
1050: (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1.1 cgd 1051: type = ICMP_UNREACH;
1052: code = ICMP_UNREACH_HOST;
1053: goto bad;
1054: }
1.20 mycroft 1055: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 1056: (caddr_t)(cp + off), sizeof(struct in_addr));
1057: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1058: break;
1059:
1060: case IPOPT_TS:
1061: code = cp - (u_char *)ip;
1062: ipt = (struct ip_timestamp *)cp;
1063: if (ipt->ipt_len < 5)
1064: goto bad;
1.15 cgd 1065: if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.1 cgd 1066: if (++ipt->ipt_oflw == 0)
1067: goto bad;
1068: break;
1069: }
1070: sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1071: switch (ipt->ipt_flg) {
1072:
1073: case IPOPT_TS_TSONLY:
1074: break;
1075:
1076: case IPOPT_TS_TSANDADDR:
1.66 thorpej 1077: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.1 cgd 1078: sizeof(struct in_addr) > ipt->ipt_len)
1079: goto bad;
1.13 mycroft 1080: ipaddr.sin_addr = dst;
1.96 thorpej 1081: ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
1082: m->m_pkthdr.rcvif));
1.13 mycroft 1083: if (ia == 0)
1084: continue;
1.20 mycroft 1085: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 1086: (caddr_t)sin, sizeof(struct in_addr));
1087: ipt->ipt_ptr += sizeof(struct in_addr);
1088: break;
1089:
1090: case IPOPT_TS_PRESPEC:
1.66 thorpej 1091: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.1 cgd 1092: sizeof(struct in_addr) > ipt->ipt_len)
1093: goto bad;
1094: bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
1095: sizeof(struct in_addr));
1.96 thorpej 1096: if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
1097: == NULL)
1.1 cgd 1098: continue;
1099: ipt->ipt_ptr += sizeof(struct in_addr);
1100: break;
1101:
1102: default:
1103: goto bad;
1104: }
1105: ntime = iptime();
1106: bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
1107: sizeof(n_time));
1108: ipt->ipt_ptr += sizeof(n_time);
1109: }
1110: }
1111: if (forward) {
1.26 thorpej 1112: if (ip_forwsrcrt == 0) {
1113: type = ICMP_UNREACH;
1114: code = ICMP_UNREACH_SRCFAIL;
1115: goto bad;
1116: }
1.1 cgd 1117: ip_forward(m, 1);
1118: return (1);
1.13 mycroft 1119: }
1120: return (0);
1.1 cgd 1121: bad:
1.13 mycroft 1122: icmp_error(m, type, code, 0, 0);
1123: ipstat.ips_badoptions++;
1.1 cgd 1124: return (1);
1125: }
1126:
1127: /*
1128: * Given address of next destination (final or next hop),
1129: * return internet address info of interface to be used to get there.
1130: */
1131: struct in_ifaddr *
1132: ip_rtaddr(dst)
1133: struct in_addr dst;
1134: {
1135: register struct sockaddr_in *sin;
1136:
1.19 mycroft 1137: sin = satosin(&ipforward_rt.ro_dst);
1.1 cgd 1138:
1.35 mycroft 1139: if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
1.1 cgd 1140: if (ipforward_rt.ro_rt) {
1141: RTFREE(ipforward_rt.ro_rt);
1142: ipforward_rt.ro_rt = 0;
1143: }
1144: sin->sin_family = AF_INET;
1145: sin->sin_len = sizeof(*sin);
1146: sin->sin_addr = dst;
1147:
1148: rtalloc(&ipforward_rt);
1149: }
1150: if (ipforward_rt.ro_rt == 0)
1151: return ((struct in_ifaddr *)0);
1.19 mycroft 1152: return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1.1 cgd 1153: }
1154:
1155: /*
1156: * Save incoming source route for use in replies,
1157: * to be picked up later by ip_srcroute if the receiver is interested.
1158: */
1.13 mycroft 1159: void
1.1 cgd 1160: save_rte(option, dst)
1161: u_char *option;
1162: struct in_addr dst;
1163: {
1164: unsigned olen;
1165:
1166: olen = option[IPOPT_OLEN];
1167: #ifdef DIAGNOSTIC
1168: if (ipprintfs)
1.39 christos 1169: printf("save_rte: olen %d\n", olen);
1.89 itojun 1170: #endif /* 0 */
1.1 cgd 1171: if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1172: return;
1173: bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
1174: ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1175: ip_srcrt.dst = dst;
1176: }
1177:
1178: /*
1179: * Retrieve incoming source route for use in replies,
1180: * in the same form used by setsockopt.
1181: * The first hop is placed before the options, will be removed later.
1182: */
1183: struct mbuf *
1184: ip_srcroute()
1185: {
1186: register struct in_addr *p, *q;
1187: register struct mbuf *m;
1188:
1189: if (ip_nhops == 0)
1190: return ((struct mbuf *)0);
1191: m = m_get(M_DONTWAIT, MT_SOOPTS);
1192: if (m == 0)
1193: return ((struct mbuf *)0);
1194:
1.13 mycroft 1195: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1 cgd 1196:
1197: /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1198: m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1199: OPTSIZ;
1200: #ifdef DIAGNOSTIC
1201: if (ipprintfs)
1.39 christos 1202: printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1.1 cgd 1203: #endif
1204:
1205: /*
1206: * First save first hop for return route
1207: */
1208: p = &ip_srcrt.route[ip_nhops - 1];
1209: *(mtod(m, struct in_addr *)) = *p--;
1210: #ifdef DIAGNOSTIC
1211: if (ipprintfs)
1.39 christos 1212: printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1.1 cgd 1213: #endif
1214:
1215: /*
1216: * Copy option fields and padding (nop) to mbuf.
1217: */
1218: ip_srcrt.nop = IPOPT_NOP;
1219: ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1220: bcopy((caddr_t)&ip_srcrt.nop,
1221: mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
1222: q = (struct in_addr *)(mtod(m, caddr_t) +
1223: sizeof(struct in_addr) + OPTSIZ);
1224: #undef OPTSIZ
1225: /*
1226: * Record return path as an IP source route,
1227: * reversing the path (pointers are now aligned).
1228: */
1229: while (p >= ip_srcrt.route) {
1230: #ifdef DIAGNOSTIC
1231: if (ipprintfs)
1.39 christos 1232: printf(" %x", ntohl(q->s_addr));
1.1 cgd 1233: #endif
1234: *q++ = *p--;
1235: }
1236: /*
1237: * Last hop goes to final destination.
1238: */
1239: *q = ip_srcrt.dst;
1240: #ifdef DIAGNOSTIC
1241: if (ipprintfs)
1.39 christos 1242: printf(" %x\n", ntohl(q->s_addr));
1.1 cgd 1243: #endif
1244: return (m);
1245: }
1246:
1247: /*
1248: * Strip out IP options, at higher
1249: * level protocol in the kernel.
1250: * Second argument is buffer to which options
1251: * will be moved, and return value is their length.
1252: * XXX should be deleted; last arg currently ignored.
1253: */
1.8 mycroft 1254: void
1.1 cgd 1255: ip_stripoptions(m, mopt)
1256: register struct mbuf *m;
1257: struct mbuf *mopt;
1258: {
1259: register int i;
1260: struct ip *ip = mtod(m, struct ip *);
1261: register caddr_t opts;
1262: int olen;
1263:
1.79 mycroft 1264: olen = (ip->ip_hl << 2) - sizeof (struct ip);
1.1 cgd 1265: opts = (caddr_t)(ip + 1);
1266: i = m->m_len - (sizeof (struct ip) + olen);
1267: bcopy(opts + olen, opts, (unsigned)i);
1268: m->m_len -= olen;
1269: if (m->m_flags & M_PKTHDR)
1270: m->m_pkthdr.len -= olen;
1.79 mycroft 1271: ip->ip_len -= olen;
1272: ip->ip_hl = sizeof (struct ip) >> 2;
1.1 cgd 1273: }
1274:
1.23 mycroft 1275: int inetctlerrmap[PRC_NCMDS] = {
1.1 cgd 1276: 0, 0, 0, 0,
1277: 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1278: EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1279: EMSGSIZE, EHOSTUNREACH, 0, 0,
1280: 0, 0, 0, 0,
1281: ENOPROTOOPT
1282: };
1283:
1284: /*
1285: * Forward a packet. If some error occurs return the sender
1286: * an icmp packet. Note we can't always generate a meaningful
1287: * icmp message because icmp doesn't have a large enough repertoire
1288: * of codes and types.
1289: *
1290: * If not forwarding, just drop the packet. This could be confusing
1291: * if ipforwarding was zero but some routing protocol was advancing
1292: * us as a gateway to somewhere. However, we must let the routing
1293: * protocol deal with that.
1294: *
1295: * The srcrt parameter indicates whether the packet is being forwarded
1296: * via a source route.
1297: */
1.13 mycroft 1298: void
1.1 cgd 1299: ip_forward(m, srcrt)
1300: struct mbuf *m;
1301: int srcrt;
1302: {
1303: register struct ip *ip = mtod(m, struct ip *);
1304: register struct sockaddr_in *sin;
1305: register struct rtentry *rt;
1.28 christos 1306: int error, type = 0, code = 0;
1.1 cgd 1307: struct mbuf *mcopy;
1.13 mycroft 1308: n_long dest;
1309: struct ifnet *destifp;
1.89 itojun 1310: #ifdef IPSEC
1311: struct ifnet dummyifp;
1312: #endif
1.1 cgd 1313:
1.13 mycroft 1314: dest = 0;
1.1 cgd 1315: #ifdef DIAGNOSTIC
1316: if (ipprintfs)
1.70 thorpej 1317: printf("forward: src %2.2x dst %2.2x ttl %x\n",
1318: ntohl(ip->ip_src.s_addr),
1319: ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
1.1 cgd 1320: #endif
1.93 sommerfe 1321: if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1.1 cgd 1322: ipstat.ips_cantforward++;
1323: m_freem(m);
1324: return;
1325: }
1326: if (ip->ip_ttl <= IPTTLDEC) {
1.13 mycroft 1327: icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1 cgd 1328: return;
1329: }
1330: ip->ip_ttl -= IPTTLDEC;
1331:
1.19 mycroft 1332: sin = satosin(&ipforward_rt.ro_dst);
1.1 cgd 1333: if ((rt = ipforward_rt.ro_rt) == 0 ||
1.35 mycroft 1334: !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1.1 cgd 1335: if (ipforward_rt.ro_rt) {
1336: RTFREE(ipforward_rt.ro_rt);
1337: ipforward_rt.ro_rt = 0;
1338: }
1339: sin->sin_family = AF_INET;
1.35 mycroft 1340: sin->sin_len = sizeof(struct sockaddr_in);
1.1 cgd 1341: sin->sin_addr = ip->ip_dst;
1342:
1343: rtalloc(&ipforward_rt);
1344: if (ipforward_rt.ro_rt == 0) {
1.13 mycroft 1345: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1.1 cgd 1346: return;
1347: }
1348: rt = ipforward_rt.ro_rt;
1349: }
1350:
1351: /*
1.34 mycroft 1352: * Save at most 68 bytes of the packet in case
1.1 cgd 1353: * we need to generate an ICMP message to the src.
1354: */
1.34 mycroft 1355: mcopy = m_copy(m, 0, imin((int)ip->ip_len, 68));
1.1 cgd 1356:
1357: /*
1358: * If forwarding packet using same interface that it came in on,
1359: * perhaps should send a redirect to sender to shortcut a hop.
1360: * Only send redirect if source is sending directly to us,
1361: * and if packet was not source routed (or has any options).
1362: * Also, don't send redirect if forwarding using a default route
1363: * or a route modified by a redirect.
1364: */
1365: if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1366: (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1.35 mycroft 1367: !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1.1 cgd 1368: ipsendredirects && !srcrt) {
1.19 mycroft 1369: if (rt->rt_ifa &&
1370: (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1371: ifatoia(rt->rt_ifa)->ia_subnet) {
1.77 thorpej 1372: if (rt->rt_flags & RTF_GATEWAY)
1373: dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1374: else
1375: dest = ip->ip_dst.s_addr;
1376: /*
1377: * Router requirements says to only send host
1378: * redirects.
1379: */
1380: type = ICMP_REDIRECT;
1381: code = ICMP_REDIRECT_HOST;
1.1 cgd 1382: #ifdef DIAGNOSTIC
1.77 thorpej 1383: if (ipprintfs)
1384: printf("redirect (%d) to %x\n", code,
1385: (u_int32_t)dest);
1.1 cgd 1386: #endif
1387: }
1388: }
1389:
1.89 itojun 1390: #ifdef IPSEC
1391: m->m_pkthdr.rcvif = NULL;
1392: #endif /*IPSEC*/
1.27 thorpej 1393: error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1394: (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1.1 cgd 1395: if (error)
1396: ipstat.ips_cantforward++;
1397: else {
1398: ipstat.ips_forward++;
1399: if (type)
1400: ipstat.ips_redirectsent++;
1401: else {
1.63 matt 1402: if (mcopy) {
1403: #ifdef GATEWAY
1.64 thorpej 1404: if (mcopy->m_flags & M_CANFASTFWD)
1405: ipflow_create(&ipforward_rt, mcopy);
1.63 matt 1406: #endif
1.1 cgd 1407: m_freem(mcopy);
1.63 matt 1408: }
1.1 cgd 1409: return;
1410: }
1411: }
1412: if (mcopy == NULL)
1413: return;
1.13 mycroft 1414: destifp = NULL;
1415:
1.1 cgd 1416: switch (error) {
1417:
1418: case 0: /* forwarded, but need redirect */
1419: /* type, code set above */
1420: break;
1421:
1422: case ENETUNREACH: /* shouldn't happen, checked above */
1423: case EHOSTUNREACH:
1424: case ENETDOWN:
1425: case EHOSTDOWN:
1426: default:
1427: type = ICMP_UNREACH;
1428: code = ICMP_UNREACH_HOST;
1429: break;
1430:
1431: case EMSGSIZE:
1432: type = ICMP_UNREACH;
1433: code = ICMP_UNREACH_NEEDFRAG;
1.89 itojun 1434: #ifndef IPSEC
1.13 mycroft 1435: if (ipforward_rt.ro_rt)
1436: destifp = ipforward_rt.ro_rt->rt_ifp;
1.89 itojun 1437: #else
1438: /*
1439: * If the packet is routed over IPsec tunnel, tell the
1440: * originator the tunnel MTU.
1441: * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1442: * XXX quickhack!!!
1443: */
1444: if (ipforward_rt.ro_rt) {
1445: struct secpolicy *sp;
1446: int ipsecerror;
1.95 itojun 1447: size_t ipsechdr;
1.89 itojun 1448: struct route *ro;
1449:
1450: sp = ipsec4_getpolicybyaddr(mcopy,
1.95 itojun 1451: IPSEC_DIR_OUTBOUND,
1452: IP_FORWARDING,
1453: &ipsecerror);
1.89 itojun 1454:
1455: if (sp == NULL)
1456: destifp = ipforward_rt.ro_rt->rt_ifp;
1457: else {
1458: /* count IPsec header size */
1.95 itojun 1459: ipsechdr = ipsec4_hdrsiz(mcopy,
1460: IPSEC_DIR_OUTBOUND,
1461: NULL);
1.89 itojun 1462:
1463: /*
1464: * find the correct route for outer IPv4
1465: * header, compute tunnel MTU.
1466: *
1467: * XXX BUG ALERT
1468: * The "dummyifp" code relies upon the fact
1469: * that icmp_error() touches only ifp->if_mtu.
1470: */
1471: /*XXX*/
1472: destifp = NULL;
1473: if (sp->req != NULL
1.95 itojun 1474: && sp->req->sav != NULL
1475: && sp->req->sav->sah != NULL) {
1476: ro = &sp->req->sav->sah->sa_route;
1.89 itojun 1477: if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1478: dummyifp.if_mtu =
1479: ro->ro_rt->rt_ifp->if_mtu;
1480: dummyifp.if_mtu -= ipsechdr;
1481: destifp = &dummyifp;
1482: }
1483: }
1484:
1485: key_freesp(sp);
1486: }
1487: }
1488: #endif /*IPSEC*/
1.1 cgd 1489: ipstat.ips_cantfrag++;
1490: break;
1491:
1492: case ENOBUFS:
1493: type = ICMP_SOURCEQUENCH;
1494: code = 0;
1495: break;
1496: }
1.13 mycroft 1497: icmp_error(mcopy, type, code, dest, destifp);
1.44 thorpej 1498: }
1499:
1500: void
1501: ip_savecontrol(inp, mp, ip, m)
1502: register struct inpcb *inp;
1503: register struct mbuf **mp;
1504: register struct ip *ip;
1505: register struct mbuf *m;
1506: {
1507:
1508: if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1509: struct timeval tv;
1510:
1511: microtime(&tv);
1512: *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1513: SCM_TIMESTAMP, SOL_SOCKET);
1514: if (*mp)
1515: mp = &(*mp)->m_next;
1516: }
1517: if (inp->inp_flags & INP_RECVDSTADDR) {
1518: *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1519: sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1520: if (*mp)
1521: mp = &(*mp)->m_next;
1522: }
1523: #ifdef notyet
1524: /*
1525: * XXX
1526: * Moving these out of udp_input() made them even more broken
1527: * than they already were.
1528: * - fenner@parc.xerox.com
1529: */
1530: /* options were tossed already */
1531: if (inp->inp_flags & INP_RECVOPTS) {
1532: *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1533: sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1534: if (*mp)
1535: mp = &(*mp)->m_next;
1536: }
1537: /* ip_srcroute doesn't do what we want here, need to fix */
1538: if (inp->inp_flags & INP_RECVRETOPTS) {
1539: *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1540: sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1541: if (*mp)
1542: mp = &(*mp)->m_next;
1543: }
1544: #endif
1545: if (inp->inp_flags & INP_RECVIF) {
1546: struct sockaddr_dl sdl;
1547:
1548: sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1549: sdl.sdl_family = AF_LINK;
1550: sdl.sdl_index = m->m_pkthdr.rcvif ?
1551: m->m_pkthdr.rcvif->if_index : 0;
1552: sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1553: *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1554: IP_RECVIF, IPPROTO_IP);
1555: if (*mp)
1556: mp = &(*mp)->m_next;
1557: }
1.13 mycroft 1558: }
1559:
1560: int
1561: ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1562: int *name;
1563: u_int namelen;
1564: void *oldp;
1565: size_t *oldlenp;
1566: void *newp;
1567: size_t newlen;
1568: {
1.88 sommerfe 1569: extern int subnetsarelocal, hostzeroisbroadcast;
1.52 thorpej 1570:
1.54 lukem 1571: int error, old;
1572:
1.13 mycroft 1573: /* All sysctl names at this level are terminal. */
1574: if (namelen != 1)
1575: return (ENOTDIR);
1576:
1577: switch (name[0]) {
1578: case IPCTL_FORWARDING:
1579: return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
1580: case IPCTL_SENDREDIRECTS:
1581: return (sysctl_int(oldp, oldlenp, newp, newlen,
1582: &ipsendredirects));
1583: case IPCTL_DEFTTL:
1584: return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
1585: #ifdef notyet
1586: case IPCTL_DEFMTU:
1587: return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1588: #endif
1.26 thorpej 1589: case IPCTL_FORWSRCRT:
1.47 cjs 1590: /* Don't allow this to change in a secure environment. */
1.26 thorpej 1591: if (securelevel > 0)
1.46 cjs 1592: return (sysctl_rdint(oldp, oldlenp, newp,
1593: ip_forwsrcrt));
1594: else
1595: return (sysctl_int(oldp, oldlenp, newp, newlen,
1596: &ip_forwsrcrt));
1.27 thorpej 1597: case IPCTL_DIRECTEDBCAST:
1598: return (sysctl_int(oldp, oldlenp, newp, newlen,
1599: &ip_directedbcast));
1.47 cjs 1600: case IPCTL_ALLOWSRCRT:
1601: return (sysctl_int(oldp, oldlenp, newp, newlen,
1602: &ip_allowsrcrt));
1.52 thorpej 1603: case IPCTL_SUBNETSARELOCAL:
1604: return (sysctl_int(oldp, oldlenp, newp, newlen,
1605: &subnetsarelocal));
1.53 kml 1606: case IPCTL_MTUDISC:
1.60 kml 1607: error = sysctl_int(oldp, oldlenp, newp, newlen,
1608: &ip_mtudisc);
1609: if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
1610: ip_mtudisc_timeout_q =
1611: rt_timer_queue_create(ip_mtudisc_timeout);
1612: } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
1613: rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE);
1614: ip_mtudisc_timeout_q = NULL;
1615: }
1616: return error;
1.54 lukem 1617: case IPCTL_ANONPORTMIN:
1618: old = anonportmin;
1619: error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
1620: if (anonportmin >= anonportmax || anonportmin > 65535
1621: #ifndef IPNOPRIVPORTS
1622: || anonportmin < IPPORT_RESERVED
1623: #endif
1624: ) {
1625: anonportmin = old;
1626: return (EINVAL);
1627: }
1628: return (error);
1629: case IPCTL_ANONPORTMAX:
1630: old = anonportmax;
1631: error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
1632: if (anonportmin >= anonportmax || anonportmax > 65535
1633: #ifndef IPNOPRIVPORTS
1634: || anonportmax < IPPORT_RESERVED
1635: #endif
1636: ) {
1637: anonportmax = old;
1638: return (EINVAL);
1639: }
1.60 kml 1640: return (error);
1641: case IPCTL_MTUDISCTIMEOUT:
1642: error = sysctl_int(oldp, oldlenp, newp, newlen,
1643: &ip_mtudisc_timeout);
1644: if (ip_mtudisc_timeout_q != NULL)
1645: rt_timer_queue_change(ip_mtudisc_timeout_q,
1646: ip_mtudisc_timeout);
1.54 lukem 1647: return (error);
1.65 matt 1648: #ifdef GATEWAY
1649: case IPCTL_MAXFLOWS:
1.67 thorpej 1650: {
1651: int s;
1652:
1.65 matt 1653: error = sysctl_int(oldp, oldlenp, newp, newlen,
1654: &ip_maxflows);
1.67 thorpej 1655: s = splsoftnet();
1.65 matt 1656: ipflow_reap(0);
1.67 thorpej 1657: splx(s);
1.65 matt 1658: return (error);
1.67 thorpej 1659: }
1.89 itojun 1660: #endif
1.90 itojun 1661: case IPCTL_HOSTZEROBROADCAST:
1662: return (sysctl_int(oldp, oldlenp, newp, newlen,
1663: &hostzeroisbroadcast));
1.89 itojun 1664: #if NGIF > 0
1665: case IPCTL_GIF_TTL:
1666: return(sysctl_int(oldp, oldlenp, newp, newlen,
1.90 itojun 1667: &ip_gif_ttl));
1.65 matt 1668: #endif
1.88 sommerfe 1669:
1.13 mycroft 1670: default:
1671: return (EOPNOTSUPP);
1672: }
1673: /* NOTREACHED */
1.1 cgd 1674: }
CVSweb <webmaster@jp.NetBSD.org>