Annotation of src/sys/netinet/ip_input.c, Revision 1.164
1.164 ! matt 1: /* $NetBSD: ip_input.c,v 1.163 2002/11/12 02:10:13 itojun Exp $ */
1.89 itojun 2:
3: /*
4: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5: * All rights reserved.
1.152 itojun 6: *
1.89 itojun 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.
1.152 itojun 18: *
1.89 itojun 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.141 lukem 103:
104: #include <sys/cdefs.h>
1.164 ! matt 105: __KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.163 2002/11/12 02:10:13 itojun Exp $");
1.55 scottr 106:
1.62 matt 107: #include "opt_gateway.h"
1.69 mrg 108: #include "opt_pfil_hooks.h"
1.91 thorpej 109: #include "opt_ipsec.h"
1.55 scottr 110: #include "opt_mrouting.h"
1.135 thorpej 111: #include "opt_inet_csum.h"
1.1 cgd 112:
1.5 mycroft 113: #include <sys/param.h>
114: #include <sys/systm.h>
115: #include <sys/malloc.h>
116: #include <sys/mbuf.h>
117: #include <sys/domain.h>
118: #include <sys/protosw.h>
119: #include <sys/socket.h>
1.44 thorpej 120: #include <sys/socketvar.h>
1.5 mycroft 121: #include <sys/errno.h>
122: #include <sys/time.h>
123: #include <sys/kernel.h>
1.72 thorpej 124: #include <sys/pool.h>
1.28 christos 125: #include <sys/sysctl.h>
1.1 cgd 126:
1.5 mycroft 127: #include <net/if.h>
1.44 thorpej 128: #include <net/if_dl.h>
1.5 mycroft 129: #include <net/route.h>
1.45 mrg 130: #include <net/pfil.h>
1.1 cgd 131:
1.5 mycroft 132: #include <netinet/in.h>
133: #include <netinet/in_systm.h>
134: #include <netinet/ip.h>
135: #include <netinet/in_pcb.h>
136: #include <netinet/in_var.h>
137: #include <netinet/ip_var.h>
138: #include <netinet/ip_icmp.h>
1.89 itojun 139: /* just for gif_ttl */
140: #include <netinet/in_gif.h>
141: #include "gif.h"
1.144 martin 142: #include <net/if_gre.h>
143: #include "gre.h"
1.111 jdolecek 144:
145: #ifdef MROUTING
146: #include <netinet/ip_mroute.h>
147: #endif
1.89 itojun 148:
149: #ifdef IPSEC
150: #include <netinet6/ipsec.h>
151: #include <netkey/key.h>
152: #endif
1.44 thorpej 153:
1.1 cgd 154: #ifndef IPFORWARDING
155: #ifdef GATEWAY
156: #define IPFORWARDING 1 /* forward IP packets not for us */
157: #else /* GATEWAY */
158: #define IPFORWARDING 0 /* don't forward IP packets not for us */
159: #endif /* GATEWAY */
160: #endif /* IPFORWARDING */
161: #ifndef IPSENDREDIRECTS
162: #define IPSENDREDIRECTS 1
163: #endif
1.26 thorpej 164: #ifndef IPFORWSRCRT
1.47 cjs 165: #define IPFORWSRCRT 1 /* forward source-routed packets */
166: #endif
167: #ifndef IPALLOWSRCRT
1.48 mrg 168: #define IPALLOWSRCRT 1 /* allow source-routed packets */
1.26 thorpej 169: #endif
1.53 kml 170: #ifndef IPMTUDISC
1.153 itojun 171: #define IPMTUDISC 1
1.53 kml 172: #endif
1.60 kml 173: #ifndef IPMTUDISCTIMEOUT
1.61 kml 174: #define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */
1.60 kml 175: #endif
1.53 kml 176:
1.27 thorpej 177: /*
178: * Note: DIRECTED_BROADCAST is handled this way so that previous
179: * configuration using this option will Just Work.
180: */
181: #ifndef IPDIRECTEDBCAST
182: #ifdef DIRECTED_BROADCAST
183: #define IPDIRECTEDBCAST 1
184: #else
185: #define IPDIRECTEDBCAST 0
186: #endif /* DIRECTED_BROADCAST */
187: #endif /* IPDIRECTEDBCAST */
1.1 cgd 188: int ipforwarding = IPFORWARDING;
189: int ipsendredirects = IPSENDREDIRECTS;
1.13 mycroft 190: int ip_defttl = IPDEFTTL;
1.26 thorpej 191: int ip_forwsrcrt = IPFORWSRCRT;
1.27 thorpej 192: int ip_directedbcast = IPDIRECTEDBCAST;
1.47 cjs 193: int ip_allowsrcrt = IPALLOWSRCRT;
1.53 kml 194: int ip_mtudisc = IPMTUDISC;
1.156 itojun 195: int ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
1.1 cgd 196: #ifdef DIAGNOSTIC
197: int ipprintfs = 0;
198: #endif
199:
1.60 kml 200: struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
201:
1.1 cgd 202: extern struct domain inetdomain;
203: int ipqmaxlen = IFQ_MAXLEN;
1.150 matt 204: u_long in_ifaddrhash; /* size of hash table - 1 */
205: int in_ifaddrentries; /* total number of addrs */
1.22 mycroft 206: struct in_ifaddrhead in_ifaddr;
1.57 tls 207: struct in_ifaddrhashhead *in_ifaddrhashtbl;
1.13 mycroft 208: struct ifqueue ipintrq;
1.63 matt 209: struct ipstat ipstat;
210: u_int16_t ip_id;
1.75 thorpej 211:
1.121 thorpej 212: #ifdef PFIL_HOOKS
213: struct pfil_head inet_pfil_hook;
214: #endif
215:
1.63 matt 216: struct ipqhead ipq;
1.75 thorpej 217: int ipq_locked;
1.131 itojun 218: int ip_nfragpackets = 0;
1.133 itojun 219: int ip_maxfragpackets = 200;
1.75 thorpej 220:
221: static __inline int ipq_lock_try __P((void));
222: static __inline void ipq_unlock __P((void));
223:
224: static __inline int
225: ipq_lock_try()
226: {
227: int s;
228:
1.132 thorpej 229: /*
1.149 wiz 230: * Use splvm() -- we're blocking things that would cause
1.132 thorpej 231: * mbuf allocation.
232: */
233: s = splvm();
1.75 thorpej 234: if (ipq_locked) {
235: splx(s);
236: return (0);
237: }
238: ipq_locked = 1;
239: splx(s);
240: return (1);
241: }
242:
243: static __inline void
244: ipq_unlock()
245: {
246: int s;
247:
1.132 thorpej 248: s = splvm();
1.75 thorpej 249: ipq_locked = 0;
250: splx(s);
251: }
252:
253: #ifdef DIAGNOSTIC
254: #define IPQ_LOCK() \
255: do { \
256: if (ipq_lock_try() == 0) { \
257: printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
258: panic("ipq_lock"); \
259: } \
1.159 perry 260: } while (/*CONSTCOND*/ 0)
1.75 thorpej 261: #define IPQ_LOCK_CHECK() \
262: do { \
263: if (ipq_locked == 0) { \
264: printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
265: panic("ipq lock check"); \
266: } \
1.159 perry 267: } while (/*CONSTCOND*/ 0)
1.75 thorpej 268: #else
269: #define IPQ_LOCK() (void) ipq_lock_try()
270: #define IPQ_LOCK_CHECK() /* nothing */
271: #endif
272:
273: #define IPQ_UNLOCK() ipq_unlock()
1.1 cgd 274:
1.72 thorpej 275: struct pool ipqent_pool;
276:
1.135 thorpej 277: #ifdef INET_CSUM_COUNTERS
278: #include <sys/device.h>
279:
280: struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
281: NULL, "inet", "hwcsum bad");
282: struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
283: NULL, "inet", "hwcsum ok");
284: struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
285: NULL, "inet", "swcsum");
286:
287: #define INET_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
288:
289: #else
290:
291: #define INET_CSUM_COUNTER_INCR(ev) /* nothing */
292:
293: #endif /* INET_CSUM_COUNTERS */
294:
1.1 cgd 295: /*
296: * We need to save the IP options in case a protocol wants to respond
297: * to an incoming packet over the same route if the packet got here
298: * using IP source routing. This allows connection establishment and
299: * maintenance when the remote end is on a network that is not known
300: * to us.
301: */
302: int ip_nhops = 0;
303: static struct ip_srcrt {
304: struct in_addr dst; /* final destination */
305: char nop; /* one NOP to align */
306: char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
307: struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
308: } ip_srcrt;
309:
1.13 mycroft 310: static void save_rte __P((u_char *, struct in_addr));
1.35 mycroft 311:
1.164 ! matt 312: #ifdef MBUFTRACE
! 313: struct mowner ip_rx_mowner = { "internet", "rx" };
! 314: struct mowner ip_tx_mowner = { "internet", "tx" };
! 315: #endif
! 316:
1.1 cgd 317: /*
318: * IP initialization: fill in IP protocol switch table.
319: * All protocols not implemented in kernel go to raw IP protocol handler.
320: */
1.8 mycroft 321: void
1.1 cgd 322: ip_init()
323: {
1.109 augustss 324: struct protosw *pr;
325: int i;
1.1 cgd 326:
1.72 thorpej 327: pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl",
1.146 thorpej 328: NULL);
1.72 thorpej 329:
1.1 cgd 330: pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
331: if (pr == 0)
332: panic("ip_init");
333: for (i = 0; i < IPPROTO_MAX; i++)
334: ip_protox[i] = pr - inetsw;
335: for (pr = inetdomain.dom_protosw;
336: pr < inetdomain.dom_protoswNPROTOSW; pr++)
337: if (pr->pr_domain->dom_family == PF_INET &&
338: pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
339: ip_protox[pr->pr_protocol] = pr - inetsw;
1.25 cgd 340: LIST_INIT(&ipq);
1.1 cgd 341: ip_id = time.tv_sec & 0xffff;
342: ipintrq.ifq_maxlen = ipqmaxlen;
1.22 mycroft 343: TAILQ_INIT(&in_ifaddr);
1.120 ad 344: in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IFADDR,
345: M_WAITOK, &in_ifaddrhash);
1.160 itojun 346: ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout);
1.73 thorpej 347: #ifdef GATEWAY
348: ipflow_init();
349: #endif
1.121 thorpej 350:
351: #ifdef PFIL_HOOKS
352: /* Register our Packet Filter hook. */
1.126 thorpej 353: inet_pfil_hook.ph_type = PFIL_TYPE_AF;
354: inet_pfil_hook.ph_af = AF_INET;
1.121 thorpej 355: i = pfil_head_register(&inet_pfil_hook);
356: if (i != 0)
357: printf("ip_init: WARNING: unable to register pfil hook, "
358: "error %d\n", i);
359: #endif /* PFIL_HOOKS */
1.135 thorpej 360:
361: #ifdef INET_CSUM_COUNTERS
362: evcnt_attach_static(&ip_hwcsum_bad);
363: evcnt_attach_static(&ip_hwcsum_ok);
364: evcnt_attach_static(&ip_swcsum);
365: #endif /* INET_CSUM_COUNTERS */
1.164 ! matt 366:
! 367: #ifdef MBUFTRACE
! 368: MOWNER_ATTACH(&ip_tx_mowner);
! 369: MOWNER_ATTACH(&ip_rx_mowner);
! 370: #endif /* MBUFTRACE */
1.1 cgd 371: }
372:
373: struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
374: struct route ipforward_rt;
375:
376: /*
1.89 itojun 377: * IP software interrupt routine
378: */
379: void
380: ipintr()
381: {
382: int s;
383: struct mbuf *m;
384:
385: while (1) {
1.132 thorpej 386: s = splnet();
1.89 itojun 387: IF_DEQUEUE(&ipintrq, m);
388: splx(s);
389: if (m == 0)
390: return;
1.164 ! matt 391: MCLAIM(m, &ip_rx_mowner);
1.89 itojun 392: ip_input(m);
393: }
394: }
395:
396: /*
1.1 cgd 397: * Ip input routine. Checksum and byte swap header. If fragmented
398: * try to reassemble. Process options. Pass to next level.
399: */
1.8 mycroft 400: void
1.89 itojun 401: ip_input(struct mbuf *m)
1.1 cgd 402: {
1.109 augustss 403: struct ip *ip = NULL;
404: struct ipq *fp;
405: struct in_ifaddr *ia;
406: struct ifaddr *ifa;
1.25 cgd 407: struct ipqent *ipqe;
1.89 itojun 408: int hlen = 0, mff, len;
1.100 itojun 409: int downmatch;
1.1 cgd 410:
1.164 ! matt 411: MCLAIM(m, &ip_rx_mowner);
1.1 cgd 412: #ifdef DIAGNOSTIC
413: if ((m->m_flags & M_PKTHDR) == 0)
414: panic("ipintr no HDR");
415: #endif
1.89 itojun 416: #ifdef IPSEC
417: /*
418: * should the inner packet be considered authentic?
419: * see comment in ah4_input().
420: */
421: if (m) {
422: m->m_flags &= ~M_AUTHIPHDR;
423: m->m_flags &= ~M_AUTHIPDGM;
424: }
425: #endif
1.164 ! matt 426:
1.1 cgd 427: /*
428: * If no IP addresses have been set yet but the interfaces
429: * are receiving, can't do anything with incoming packets yet.
430: */
1.140 matt 431: if (TAILQ_FIRST(&in_ifaddr) == 0)
1.1 cgd 432: goto bad;
433: ipstat.ips_total++;
1.154 thorpej 434: /*
435: * If the IP header is not aligned, slurp it up into a new
436: * mbuf with space for link headers, in the event we forward
437: * it. Otherwise, if it is aligned, make sure the entire
438: * base IP header is in the first mbuf of the chain.
439: */
440: if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
441: if ((m = m_copyup(m, sizeof(struct ip),
442: (max_linkhdr + 3) & ~3)) == NULL) {
443: /* XXXJRT new stat, please */
444: ipstat.ips_toosmall++;
445: return;
446: }
447: } else if (__predict_false(m->m_len < sizeof (struct ip))) {
448: if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
449: ipstat.ips_toosmall++;
450: return;
451: }
1.1 cgd 452: }
453: ip = mtod(m, struct ip *);
1.13 mycroft 454: if (ip->ip_v != IPVERSION) {
455: ipstat.ips_badvers++;
456: goto bad;
457: }
1.1 cgd 458: hlen = ip->ip_hl << 2;
459: if (hlen < sizeof(struct ip)) { /* minimum header length */
460: ipstat.ips_badhlen++;
461: goto bad;
462: }
463: if (hlen > m->m_len) {
464: if ((m = m_pullup(m, hlen)) == 0) {
465: ipstat.ips_badhlen++;
1.89 itojun 466: return;
1.1 cgd 467: }
468: ip = mtod(m, struct ip *);
469: }
1.98 thorpej 470:
1.85 hwr 471: /*
1.99 thorpej 472: * RFC1122: packets with a multicast source address are
1.98 thorpej 473: * not allowed.
1.85 hwr 474: */
475: if (IN_MULTICAST(ip->ip_src.s_addr)) {
1.130 itojun 476: ipstat.ips_badaddr++;
1.85 hwr 477: goto bad;
1.129 itojun 478: }
479:
480: /* 127/8 must not appear on wire - RFC1122 */
481: if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
482: (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
1.130 itojun 483: if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
484: ipstat.ips_badaddr++;
1.129 itojun 485: goto bad;
1.130 itojun 486: }
1.85 hwr 487: }
488:
1.135 thorpej 489: switch (m->m_pkthdr.csum_flags &
1.137 thorpej 490: ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
1.135 thorpej 491: M_CSUM_IPv4_BAD)) {
492: case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
493: INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad);
494: goto badcsum;
495:
496: case M_CSUM_IPv4:
497: /* Checksum was okay. */
498: INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok);
499: break;
500:
501: default:
502: /* Must compute it ourselves. */
503: INET_CSUM_COUNTER_INCR(&ip_swcsum);
504: if (in_cksum(m, hlen) != 0)
505: goto bad;
506: break;
1.1 cgd 507: }
508:
1.121 thorpej 509: /* Retrieve the packet length. */
510: len = ntohs(ip->ip_len);
1.81 proff 511:
512: /*
513: * Check for additional length bogosity
514: */
1.84 proff 515: if (len < hlen) {
1.81 proff 516: ipstat.ips_badlen++;
517: goto bad;
518: }
1.1 cgd 519:
520: /*
521: * Check that the amount of data in the buffers
522: * is as at least much as the IP header would have us expect.
523: * Trim mbufs if longer than we expect.
524: * Drop packet if shorter than we expect.
525: */
1.35 mycroft 526: if (m->m_pkthdr.len < len) {
1.1 cgd 527: ipstat.ips_tooshort++;
528: goto bad;
529: }
1.35 mycroft 530: if (m->m_pkthdr.len > len) {
1.1 cgd 531: if (m->m_len == m->m_pkthdr.len) {
1.35 mycroft 532: m->m_len = len;
533: m->m_pkthdr.len = len;
1.1 cgd 534: } else
1.35 mycroft 535: m_adj(m, len - m->m_pkthdr.len);
1.1 cgd 536: }
537:
1.94 itojun 538: #ifdef IPSEC
1.149 wiz 539: /* ipflow (IP fast forwarding) is not compatible with IPsec. */
1.94 itojun 540: m->m_flags &= ~M_CANFASTFWD;
541: #else
1.64 thorpej 542: /*
543: * Assume that we can create a fast-forward IP flow entry
544: * based on this packet.
545: */
546: m->m_flags |= M_CANFASTFWD;
1.94 itojun 547: #endif
1.64 thorpej 548:
1.36 mrg 549: #ifdef PFIL_HOOKS
1.33 mrg 550: /*
1.64 thorpej 551: * Run through list of hooks for input packets. If there are any
552: * filters which require that additional packets in the flow are
553: * not fast-forwarded, they must clear the M_CANFASTFWD flag.
554: * Note that filters must _never_ set this flag, as another filter
555: * in the list may have previously cleared it.
1.33 mrg 556: */
1.127 itojun 557: /*
558: * let ipfilter look at packet on the wire,
559: * not the decapsulated packet.
560: */
561: #ifdef IPSEC
1.136 itojun 562: if (!ipsec_getnhist(m))
1.127 itojun 563: #else
564: if (1)
565: #endif
566: {
567: if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
568: PFIL_IN) != 0)
1.121 thorpej 569: return;
1.127 itojun 570: if (m == NULL)
571: return;
572: ip = mtod(m, struct ip *);
1.142 darrenr 573: hlen = ip->ip_hl << 2;
1.127 itojun 574: }
1.36 mrg 575: #endif /* PFIL_HOOKS */
1.123 thorpej 576:
577: #ifdef ALTQ
578: /* XXX Temporary until ALTQ is changed to use a pfil hook */
579: if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
580: /* packet dropped by traffic conditioner */
581: return;
582: }
583: #endif
1.121 thorpej 584:
585: /*
1.1 cgd 586: * Process options and, if not destined for us,
587: * ship it on. ip_dooptions returns 1 when an
588: * error was detected (causing an icmp message
589: * to be sent and the original packet to be freed).
590: */
591: ip_nhops = 0; /* for source routed packets */
592: if (hlen > sizeof (struct ip) && ip_dooptions(m))
1.89 itojun 593: return;
1.1 cgd 594:
595: /*
596: * Check our list of addresses, to see if the packet is for us.
1.100 itojun 597: *
598: * Traditional 4.4BSD did not consult IFF_UP at all.
599: * The behavior here is to treat addresses on !IFF_UP interface
600: * as not mine.
1.1 cgd 601: */
1.100 itojun 602: downmatch = 0;
1.140 matt 603: LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
1.97 itojun 604: if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
605: if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
606: break;
1.100 itojun 607: else
608: downmatch++;
1.97 itojun 609: }
610: }
1.86 thorpej 611: if (ia != NULL)
612: goto ours;
1.57 tls 613: if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
1.140 matt 614: TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) {
615: if (ifa->ifa_addr->sa_family != AF_INET)
616: continue;
1.57 tls 617: ia = ifatoia(ifa);
1.35 mycroft 618: if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
619: in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
1.20 mycroft 620: /*
621: * Look for all-0's host part (old broadcast addr),
622: * either for subnet or net.
623: */
624: ip->ip_dst.s_addr == ia->ia_subnet ||
1.18 mycroft 625: ip->ip_dst.s_addr == ia->ia_net)
1.1 cgd 626: goto ours;
1.57 tls 627: /*
628: * An interface with IP address zero accepts
629: * all packets that arrive on that interface.
630: */
631: if (in_nullhost(ia->ia_addr.sin_addr))
632: goto ours;
1.1 cgd 633: }
634: }
1.18 mycroft 635: if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4 hpeyerl 636: struct in_multi *inm;
637: #ifdef MROUTING
638: extern struct socket *ip_mrouter;
1.10 brezak 639:
1.147 matt 640: if (M_READONLY(m)) {
1.10 brezak 641: if ((m = m_pullup(m, hlen)) == 0) {
642: ipstat.ips_toosmall++;
1.89 itojun 643: return;
1.10 brezak 644: }
645: ip = mtod(m, struct ip *);
646: }
1.4 hpeyerl 647:
648: if (ip_mrouter) {
649: /*
650: * If we are acting as a multicast router, all
651: * incoming multicast packets are passed to the
652: * kernel-level multicast forwarding function.
653: * The packet is returned (relatively) intact; if
654: * ip_mforward() returns a non-zero value, the packet
655: * must be discarded, else it may be accepted below.
656: *
657: * (The IP ident field is put in the same byte order
658: * as expected when ip_mforward() is called from
659: * ip_output().)
660: */
1.13 mycroft 661: if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
662: ipstat.ips_cantforward++;
1.4 hpeyerl 663: m_freem(m);
1.89 itojun 664: return;
1.4 hpeyerl 665: }
666:
667: /*
668: * The process-level routing demon needs to receive
669: * all multicast IGMP packets, whether or not this
670: * host belongs to their destination groups.
671: */
672: if (ip->ip_p == IPPROTO_IGMP)
673: goto ours;
1.13 mycroft 674: ipstat.ips_forward++;
1.4 hpeyerl 675: }
676: #endif
677: /*
678: * See if we belong to the destination multicast group on the
679: * arrival interface.
680: */
681: IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
682: if (inm == NULL) {
1.13 mycroft 683: ipstat.ips_cantforward++;
1.4 hpeyerl 684: m_freem(m);
1.89 itojun 685: return;
1.4 hpeyerl 686: }
687: goto ours;
688: }
1.19 mycroft 689: if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
1.35 mycroft 690: in_nullhost(ip->ip_dst))
1.1 cgd 691: goto ours;
692:
693: /*
694: * Not for us; forward if possible and desirable.
695: */
696: if (ipforwarding == 0) {
697: ipstat.ips_cantforward++;
698: m_freem(m);
1.100 itojun 699: } else {
700: /*
701: * If ip_dst matched any of my address on !IFF_UP interface,
702: * and there's no IFF_UP interface that matches ip_dst,
703: * send icmp unreach. Forwarding it will result in in-kernel
704: * forwarding loop till TTL goes to 0.
705: */
706: if (downmatch) {
707: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
708: ipstat.ips_cantforward++;
709: return;
710: }
1.145 itojun 711: #ifdef IPSEC
712: if (ipsec4_in_reject(m, NULL)) {
713: ipsecstat.in_polvio++;
714: goto bad;
715: }
716: #endif
717:
1.1 cgd 718: ip_forward(m, 0);
1.100 itojun 719: }
1.89 itojun 720: return;
1.1 cgd 721:
722: ours:
723: /*
724: * If offset or IP_MF are set, must reassemble.
725: * Otherwise, nothing need be done.
726: * (We could look in the reassembly queue to see
727: * if the packet was previously fragmented,
728: * but it's not worth the time; just let them time out.)
729: */
1.155 itojun 730: if (ip->ip_off & ~htons(IP_DF|IP_RF)) {
731: if (M_READONLY(m)) {
732: if ((m = m_pullup(m, hlen)) == NULL) {
733: ipstat.ips_toosmall++;
734: goto bad;
735: }
736: ip = mtod(m, struct ip *);
737: }
738:
1.1 cgd 739: /*
740: * Look for queue of fragments
741: * of this datagram.
742: */
1.75 thorpej 743: IPQ_LOCK();
1.140 matt 744: LIST_FOREACH(fp, &ipq, ipq_q)
1.1 cgd 745: if (ip->ip_id == fp->ipq_id &&
1.35 mycroft 746: in_hosteq(ip->ip_src, fp->ipq_src) &&
747: in_hosteq(ip->ip_dst, fp->ipq_dst) &&
1.1 cgd 748: ip->ip_p == fp->ipq_p)
749: goto found;
750: fp = 0;
751: found:
752:
753: /*
754: * Adjust ip_len to not reflect header,
1.25 cgd 755: * set ipqe_mff if more fragments are expected,
1.1 cgd 756: * convert offset of this to bytes.
757: */
1.155 itojun 758: ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
759: mff = (ip->ip_off & htons(IP_MF)) != 0;
1.25 cgd 760: if (mff) {
1.16 cgd 761: /*
762: * Make sure that fragments have a data length
763: * that's a non-zero multiple of 8 bytes.
764: */
1.155 itojun 765: if (ntohs(ip->ip_len) == 0 ||
766: (ntohs(ip->ip_len) & 0x7) != 0) {
1.16 cgd 767: ipstat.ips_badfrags++;
1.75 thorpej 768: IPQ_UNLOCK();
1.16 cgd 769: goto bad;
770: }
771: }
1.155 itojun 772: ip->ip_off = htons((ntohs(ip->ip_off) & IP_OFFMASK) << 3);
1.1 cgd 773:
774: /*
775: * If datagram marked as having more fragments
776: * or if this is not the first fragment,
777: * attempt reassembly; if it succeeds, proceed.
778: */
1.155 itojun 779: if (mff || ip->ip_off != htons(0)) {
1.1 cgd 780: ipstat.ips_fragments++;
1.72 thorpej 781: ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
1.25 cgd 782: if (ipqe == NULL) {
783: ipstat.ips_rcvmemdrop++;
1.75 thorpej 784: IPQ_UNLOCK();
1.25 cgd 785: goto bad;
786: }
787: ipqe->ipqe_mff = mff;
1.50 thorpej 788: ipqe->ipqe_m = m;
1.25 cgd 789: ipqe->ipqe_ip = ip;
1.50 thorpej 790: m = ip_reass(ipqe, fp);
1.75 thorpej 791: if (m == 0) {
792: IPQ_UNLOCK();
1.89 itojun 793: return;
1.75 thorpej 794: }
1.13 mycroft 795: ipstat.ips_reassembled++;
1.50 thorpej 796: ip = mtod(m, struct ip *);
1.74 thorpej 797: hlen = ip->ip_hl << 2;
1.155 itojun 798: ip->ip_len = htons(ntohs(ip->ip_len) + hlen);
1.1 cgd 799: } else
800: if (fp)
801: ip_freef(fp);
1.75 thorpej 802: IPQ_UNLOCK();
1.79 mycroft 803: }
1.128 itojun 804:
805: #ifdef IPSEC
806: /*
807: * enforce IPsec policy checking if we are seeing last header.
808: * note that we do not visit this with protocols with pcb layer
809: * code - like udp/tcp/raw ip.
810: */
811: if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
812: ipsec4_in_reject(m, NULL)) {
813: ipsecstat.in_polvio++;
814: goto bad;
815: }
816: #endif
1.1 cgd 817:
818: /*
819: * Switch out to protocol's input routine.
820: */
1.82 aidan 821: #if IFA_STATS
1.122 itojun 822: if (ia && ip)
1.155 itojun 823: ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len);
1.82 aidan 824: #endif
1.1 cgd 825: ipstat.ips_delivered++;
1.89 itojun 826: {
827: int off = hlen, nh = ip->ip_p;
828:
829: (*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
830: return;
831: }
1.1 cgd 832: bad:
833: m_freem(m);
1.135 thorpej 834: return;
835:
836: badcsum:
837: ipstat.ips_badsum++;
838: m_freem(m);
1.1 cgd 839: }
840:
841: /*
842: * Take incoming datagram fragment and try to
843: * reassemble it into whole datagram. If a chain for
844: * reassembly of this datagram already exists, then it
845: * is given as fp; otherwise have to make a chain.
846: */
1.50 thorpej 847: struct mbuf *
1.25 cgd 848: ip_reass(ipqe, fp)
1.109 augustss 849: struct ipqent *ipqe;
850: struct ipq *fp;
1.1 cgd 851: {
1.109 augustss 852: struct mbuf *m = ipqe->ipqe_m;
853: struct ipqent *nq, *p, *q;
1.25 cgd 854: struct ip *ip;
1.1 cgd 855: struct mbuf *t;
1.25 cgd 856: int hlen = ipqe->ipqe_ip->ip_hl << 2;
1.1 cgd 857: int i, next;
858:
1.75 thorpej 859: IPQ_LOCK_CHECK();
860:
1.1 cgd 861: /*
862: * Presence of header sizes in mbufs
863: * would confuse code below.
864: */
865: m->m_data += hlen;
866: m->m_len -= hlen;
867:
868: /*
869: * If first fragment to arrive, create a reassembly queue.
870: */
871: if (fp == 0) {
1.131 itojun 872: /*
873: * Enforce upper bound on number of fragmented packets
874: * for which we attempt reassembly;
875: * If maxfrag is 0, never accept fragments.
876: * If maxfrag is -1, accept all fragments without limitation.
877: */
878: if (ip_maxfragpackets < 0)
879: ;
880: else if (ip_nfragpackets >= ip_maxfragpackets)
881: goto dropfrag;
882: ip_nfragpackets++;
1.50 thorpej 883: MALLOC(fp, struct ipq *, sizeof (struct ipq),
884: M_FTABLE, M_NOWAIT);
885: if (fp == NULL)
1.1 cgd 886: goto dropfrag;
1.25 cgd 887: LIST_INSERT_HEAD(&ipq, fp, ipq_q);
1.1 cgd 888: fp->ipq_ttl = IPFRAGTTL;
1.25 cgd 889: fp->ipq_p = ipqe->ipqe_ip->ip_p;
890: fp->ipq_id = ipqe->ipqe_ip->ip_id;
1.148 matt 891: TAILQ_INIT(&fp->ipq_fragq);
1.25 cgd 892: fp->ipq_src = ipqe->ipqe_ip->ip_src;
893: fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
894: p = NULL;
1.1 cgd 895: goto insert;
896: }
897:
898: /*
899: * Find a segment which begins after this one does.
900: */
1.148 matt 901: for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
902: p = q, q = TAILQ_NEXT(q, ipqe_q))
1.155 itojun 903: if (ntohs(q->ipqe_ip->ip_off) > ntohs(ipqe->ipqe_ip->ip_off))
1.1 cgd 904: break;
905:
906: /*
907: * If there is a preceding segment, it may provide some of
908: * our data already. If so, drop the data from the incoming
909: * segment. If it provides all of our data, drop us.
910: */
1.25 cgd 911: if (p != NULL) {
1.155 itojun 912: i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) -
913: ntohs(ipqe->ipqe_ip->ip_off);
1.1 cgd 914: if (i > 0) {
1.155 itojun 915: if (i >= ntohs(ipqe->ipqe_ip->ip_len))
1.1 cgd 916: goto dropfrag;
1.50 thorpej 917: m_adj(ipqe->ipqe_m, i);
1.155 itojun 918: ipqe->ipqe_ip->ip_off =
919: htons(ntohs(ipqe->ipqe_ip->ip_off) + i);
920: ipqe->ipqe_ip->ip_len =
921: htons(ntohs(ipqe->ipqe_ip->ip_len) - i);
1.1 cgd 922: }
923: }
924:
925: /*
926: * While we overlap succeeding segments trim them or,
927: * if they are completely covered, dequeue them.
928: */
1.155 itojun 929: for (; q != NULL &&
930: ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len) >
931: ntohs(q->ipqe_ip->ip_off); q = nq) {
932: i = (ntohs(ipqe->ipqe_ip->ip_off) +
933: ntohs(ipqe->ipqe_ip->ip_len)) - ntohs(q->ipqe_ip->ip_off);
934: if (i < ntohs(q->ipqe_ip->ip_len)) {
935: q->ipqe_ip->ip_len =
936: htons(ntohs(q->ipqe_ip->ip_len) - i);
937: q->ipqe_ip->ip_off =
938: htons(ntohs(q->ipqe_ip->ip_off) + i);
1.50 thorpej 939: m_adj(q->ipqe_m, i);
1.1 cgd 940: break;
941: }
1.148 matt 942: nq = TAILQ_NEXT(q, ipqe_q);
1.50 thorpej 943: m_freem(q->ipqe_m);
1.148 matt 944: TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
1.72 thorpej 945: pool_put(&ipqent_pool, q);
1.1 cgd 946: }
947:
948: insert:
949: /*
950: * Stick new segment in its place;
951: * check for complete reassembly.
952: */
1.25 cgd 953: if (p == NULL) {
1.148 matt 954: TAILQ_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
1.25 cgd 955: } else {
1.148 matt 956: TAILQ_INSERT_AFTER(&fp->ipq_fragq, p, ipqe, ipqe_q);
1.25 cgd 957: }
1.1 cgd 958: next = 0;
1.148 matt 959: for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
960: p = q, q = TAILQ_NEXT(q, ipqe_q)) {
1.155 itojun 961: if (ntohs(q->ipqe_ip->ip_off) != next)
1.1 cgd 962: return (0);
1.155 itojun 963: next += ntohs(q->ipqe_ip->ip_len);
1.1 cgd 964: }
1.25 cgd 965: if (p->ipqe_mff)
1.1 cgd 966: return (0);
967:
968: /*
1.41 thorpej 969: * Reassembly is complete. Check for a bogus message size and
970: * concatenate fragments.
1.1 cgd 971: */
1.148 matt 972: q = TAILQ_FIRST(&fp->ipq_fragq);
1.25 cgd 973: ip = q->ipqe_ip;
1.41 thorpej 974: if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
975: ipstat.ips_toolong++;
976: ip_freef(fp);
977: return (0);
978: }
1.50 thorpej 979: m = q->ipqe_m;
1.1 cgd 980: t = m->m_next;
981: m->m_next = 0;
982: m_cat(m, t);
1.148 matt 983: nq = TAILQ_NEXT(q, ipqe_q);
1.72 thorpej 984: pool_put(&ipqent_pool, q);
1.25 cgd 985: for (q = nq; q != NULL; q = nq) {
1.50 thorpej 986: t = q->ipqe_m;
1.148 matt 987: nq = TAILQ_NEXT(q, ipqe_q);
1.72 thorpej 988: pool_put(&ipqent_pool, q);
1.1 cgd 989: m_cat(m, t);
990: }
991:
992: /*
993: * Create header for new ip packet by
994: * modifying header of first packet;
995: * dequeue and discard fragment reassembly header.
996: * Make header visible.
997: */
1.155 itojun 998: ip->ip_len = htons(next);
1.25 cgd 999: ip->ip_src = fp->ipq_src;
1000: ip->ip_dst = fp->ipq_dst;
1001: LIST_REMOVE(fp, ipq_q);
1.50 thorpej 1002: FREE(fp, M_FTABLE);
1.131 itojun 1003: ip_nfragpackets--;
1.1 cgd 1004: m->m_len += (ip->ip_hl << 2);
1005: m->m_data -= (ip->ip_hl << 2);
1006: /* some debugging cruft by sklower, below, will go away soon */
1007: if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1.109 augustss 1008: int plen = 0;
1.50 thorpej 1009: for (t = m; t; t = t->m_next)
1010: plen += t->m_len;
1011: m->m_pkthdr.len = plen;
1.1 cgd 1012: }
1.50 thorpej 1013: return (m);
1.1 cgd 1014:
1015: dropfrag:
1016: ipstat.ips_fragdropped++;
1017: m_freem(m);
1.72 thorpej 1018: pool_put(&ipqent_pool, ipqe);
1.1 cgd 1019: return (0);
1020: }
1021:
1022: /*
1023: * Free a fragment reassembly header and all
1024: * associated datagrams.
1025: */
1.8 mycroft 1026: void
1.1 cgd 1027: ip_freef(fp)
1028: struct ipq *fp;
1029: {
1.109 augustss 1030: struct ipqent *q, *p;
1.1 cgd 1031:
1.75 thorpej 1032: IPQ_LOCK_CHECK();
1033:
1.148 matt 1034: for (q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; q = p) {
1035: p = TAILQ_NEXT(q, ipqe_q);
1.50 thorpej 1036: m_freem(q->ipqe_m);
1.148 matt 1037: TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
1.72 thorpej 1038: pool_put(&ipqent_pool, q);
1.1 cgd 1039: }
1.25 cgd 1040: LIST_REMOVE(fp, ipq_q);
1.50 thorpej 1041: FREE(fp, M_FTABLE);
1.131 itojun 1042: ip_nfragpackets--;
1.1 cgd 1043: }
1044:
1045: /*
1046: * IP timer processing;
1047: * if a timer expires on a reassembly
1048: * queue, discard it.
1049: */
1.8 mycroft 1050: void
1.1 cgd 1051: ip_slowtimo()
1052: {
1.109 augustss 1053: struct ipq *fp, *nfp;
1.24 mycroft 1054: int s = splsoftnet();
1.1 cgd 1055:
1.75 thorpej 1056: IPQ_LOCK();
1.140 matt 1057: for (fp = LIST_FIRST(&ipq); fp != NULL; fp = nfp) {
1058: nfp = LIST_NEXT(fp, ipq_q);
1.25 cgd 1059: if (--fp->ipq_ttl == 0) {
1.1 cgd 1060: ipstat.ips_fragtimeout++;
1.25 cgd 1061: ip_freef(fp);
1.1 cgd 1062: }
1063: }
1.131 itojun 1064: /*
1065: * If we are over the maximum number of fragments
1066: * (due to the limit being lowered), drain off
1067: * enough to get down to the new limit.
1068: */
1069: if (ip_maxfragpackets < 0)
1070: ;
1071: else {
1.140 matt 1072: while (ip_nfragpackets > ip_maxfragpackets && LIST_FIRST(&ipq))
1073: ip_freef(LIST_FIRST(&ipq));
1.131 itojun 1074: }
1.75 thorpej 1075: IPQ_UNLOCK();
1.63 matt 1076: #ifdef GATEWAY
1077: ipflow_slowtimo();
1078: #endif
1.1 cgd 1079: splx(s);
1080: }
1081:
1082: /*
1083: * Drain off all datagram fragments.
1084: */
1.8 mycroft 1085: void
1.1 cgd 1086: ip_drain()
1087: {
1088:
1.75 thorpej 1089: /*
1090: * We may be called from a device's interrupt context. If
1091: * the ipq is already busy, just bail out now.
1092: */
1093: if (ipq_lock_try() == 0)
1094: return;
1095:
1.140 matt 1096: while (LIST_FIRST(&ipq) != NULL) {
1.1 cgd 1097: ipstat.ips_fragdropped++;
1.140 matt 1098: ip_freef(LIST_FIRST(&ipq));
1.1 cgd 1099: }
1.75 thorpej 1100:
1101: IPQ_UNLOCK();
1.1 cgd 1102: }
1103:
1104: /*
1105: * Do option processing on a datagram,
1106: * possibly discarding it if bad options are encountered,
1107: * or forwarding it if source-routed.
1108: * Returns 1 if packet has been forwarded/freed,
1109: * 0 if the packet should be processed further.
1110: */
1.8 mycroft 1111: int
1.1 cgd 1112: ip_dooptions(m)
1113: struct mbuf *m;
1114: {
1.109 augustss 1115: struct ip *ip = mtod(m, struct ip *);
1116: u_char *cp, *cp0;
1117: struct ip_timestamp *ipt;
1118: struct in_ifaddr *ia;
1.1 cgd 1119: int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1.104 thorpej 1120: struct in_addr dst;
1.1 cgd 1121: n_time ntime;
1122:
1.13 mycroft 1123: dst = ip->ip_dst;
1.1 cgd 1124: cp = (u_char *)(ip + 1);
1125: cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1126: for (; cnt > 0; cnt -= optlen, cp += optlen) {
1127: opt = cp[IPOPT_OPTVAL];
1128: if (opt == IPOPT_EOL)
1129: break;
1130: if (opt == IPOPT_NOP)
1131: optlen = 1;
1132: else {
1.113 itojun 1133: if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1134: code = &cp[IPOPT_OLEN] - (u_char *)ip;
1135: goto bad;
1136: }
1.1 cgd 1137: optlen = cp[IPOPT_OLEN];
1.114 itojun 1138: if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1.1 cgd 1139: code = &cp[IPOPT_OLEN] - (u_char *)ip;
1140: goto bad;
1141: }
1142: }
1143: switch (opt) {
1144:
1145: default:
1146: break;
1147:
1148: /*
1149: * Source routing with record.
1150: * Find interface with current destination address.
1151: * If none on this machine then drop if strictly routed,
1152: * or do nothing if loosely routed.
1153: * Record interface address and bring up next address
1154: * component. If strictly routed make sure next
1155: * address is on directly accessible net.
1156: */
1157: case IPOPT_LSRR:
1158: case IPOPT_SSRR:
1.47 cjs 1159: if (ip_allowsrcrt == 0) {
1160: type = ICMP_UNREACH;
1161: code = ICMP_UNREACH_NET_PROHIB;
1162: goto bad;
1163: }
1.114 itojun 1164: if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1165: code = &cp[IPOPT_OLEN] - (u_char *)ip;
1166: goto bad;
1167: }
1.1 cgd 1168: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1169: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1170: goto bad;
1171: }
1172: ipaddr.sin_addr = ip->ip_dst;
1.19 mycroft 1173: ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1.1 cgd 1174: if (ia == 0) {
1175: if (opt == IPOPT_SSRR) {
1176: type = ICMP_UNREACH;
1177: code = ICMP_UNREACH_SRCFAIL;
1178: goto bad;
1179: }
1180: /*
1181: * Loose routing, and not at next destination
1182: * yet; nothing to do except forward.
1183: */
1184: break;
1185: }
1186: off--; /* 0 origin */
1.112 sommerfe 1187: if ((off + sizeof(struct in_addr)) > optlen) {
1.1 cgd 1188: /*
1189: * End of source route. Should be for us.
1190: */
1191: save_rte(cp, ip->ip_src);
1192: break;
1193: }
1194: /*
1195: * locate outgoing interface
1196: */
1197: bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
1198: sizeof(ipaddr.sin_addr));
1.96 thorpej 1199: if (opt == IPOPT_SSRR)
1200: ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1201: else
1.1 cgd 1202: ia = ip_rtaddr(ipaddr.sin_addr);
1203: if (ia == 0) {
1204: type = ICMP_UNREACH;
1205: code = ICMP_UNREACH_SRCFAIL;
1206: goto bad;
1207: }
1208: ip->ip_dst = ipaddr.sin_addr;
1.20 mycroft 1209: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 1210: (caddr_t)(cp + off), sizeof(struct in_addr));
1211: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13 mycroft 1212: /*
1213: * Let ip_intr's mcast routing check handle mcast pkts
1214: */
1.18 mycroft 1215: forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1 cgd 1216: break;
1217:
1218: case IPOPT_RR:
1.114 itojun 1219: if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1220: code = &cp[IPOPT_OLEN] - (u_char *)ip;
1221: goto bad;
1222: }
1.1 cgd 1223: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1224: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1225: goto bad;
1226: }
1227: /*
1228: * If no space remains, ignore.
1229: */
1230: off--; /* 0 origin */
1.112 sommerfe 1231: if ((off + sizeof(struct in_addr)) > optlen)
1.1 cgd 1232: break;
1233: bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
1234: sizeof(ipaddr.sin_addr));
1235: /*
1236: * locate outgoing interface; if we're the destination,
1237: * use the incoming interface (should be same).
1238: */
1.96 thorpej 1239: if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
1240: == NULL &&
1241: (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1.1 cgd 1242: type = ICMP_UNREACH;
1243: code = ICMP_UNREACH_HOST;
1244: goto bad;
1245: }
1.20 mycroft 1246: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 1247: (caddr_t)(cp + off), sizeof(struct in_addr));
1248: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1249: break;
1250:
1251: case IPOPT_TS:
1252: code = cp - (u_char *)ip;
1253: ipt = (struct ip_timestamp *)cp;
1.114 itojun 1254: if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
1255: code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1.1 cgd 1256: goto bad;
1.114 itojun 1257: }
1258: if (ipt->ipt_ptr < 5) {
1259: code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
1260: goto bad;
1261: }
1.15 cgd 1262: if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.114 itojun 1263: if (++ipt->ipt_oflw == 0) {
1264: code = (u_char *)&ipt->ipt_ptr -
1265: (u_char *)ip;
1.1 cgd 1266: goto bad;
1.114 itojun 1267: }
1.1 cgd 1268: break;
1269: }
1.104 thorpej 1270: cp0 = (cp + ipt->ipt_ptr - 1);
1.1 cgd 1271: switch (ipt->ipt_flg) {
1272:
1273: case IPOPT_TS_TSONLY:
1274: break;
1275:
1276: case IPOPT_TS_TSANDADDR:
1.66 thorpej 1277: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.114 itojun 1278: sizeof(struct in_addr) > ipt->ipt_len) {
1279: code = (u_char *)&ipt->ipt_ptr -
1280: (u_char *)ip;
1.1 cgd 1281: goto bad;
1.114 itojun 1282: }
1.13 mycroft 1283: ipaddr.sin_addr = dst;
1.96 thorpej 1284: ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
1285: m->m_pkthdr.rcvif));
1.13 mycroft 1286: if (ia == 0)
1287: continue;
1.104 thorpej 1288: bcopy(&ia->ia_addr.sin_addr,
1289: cp0, sizeof(struct in_addr));
1.1 cgd 1290: ipt->ipt_ptr += sizeof(struct in_addr);
1291: break;
1292:
1293: case IPOPT_TS_PRESPEC:
1.66 thorpej 1294: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.114 itojun 1295: sizeof(struct in_addr) > ipt->ipt_len) {
1296: code = (u_char *)&ipt->ipt_ptr -
1297: (u_char *)ip;
1.1 cgd 1298: goto bad;
1.114 itojun 1299: }
1.104 thorpej 1300: bcopy(cp0, &ipaddr.sin_addr,
1.1 cgd 1301: sizeof(struct in_addr));
1.96 thorpej 1302: if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
1303: == NULL)
1.1 cgd 1304: continue;
1305: ipt->ipt_ptr += sizeof(struct in_addr);
1306: break;
1307:
1308: default:
1.114 itojun 1309: /* XXX can't take &ipt->ipt_flg */
1310: code = (u_char *)&ipt->ipt_ptr -
1311: (u_char *)ip + 1;
1.1 cgd 1312: goto bad;
1313: }
1314: ntime = iptime();
1.107 thorpej 1315: cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
1316: bcopy(cp0, (caddr_t)cp + ipt->ipt_ptr - 1,
1.1 cgd 1317: sizeof(n_time));
1318: ipt->ipt_ptr += sizeof(n_time);
1319: }
1320: }
1321: if (forward) {
1.26 thorpej 1322: if (ip_forwsrcrt == 0) {
1323: type = ICMP_UNREACH;
1324: code = ICMP_UNREACH_SRCFAIL;
1325: goto bad;
1326: }
1.1 cgd 1327: ip_forward(m, 1);
1328: return (1);
1.13 mycroft 1329: }
1330: return (0);
1.1 cgd 1331: bad:
1.13 mycroft 1332: icmp_error(m, type, code, 0, 0);
1333: ipstat.ips_badoptions++;
1.1 cgd 1334: return (1);
1335: }
1336:
1337: /*
1338: * Given address of next destination (final or next hop),
1339: * return internet address info of interface to be used to get there.
1340: */
1341: struct in_ifaddr *
1342: ip_rtaddr(dst)
1343: struct in_addr dst;
1344: {
1.109 augustss 1345: struct sockaddr_in *sin;
1.1 cgd 1346:
1.19 mycroft 1347: sin = satosin(&ipforward_rt.ro_dst);
1.1 cgd 1348:
1.35 mycroft 1349: if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
1.1 cgd 1350: if (ipforward_rt.ro_rt) {
1351: RTFREE(ipforward_rt.ro_rt);
1352: ipforward_rt.ro_rt = 0;
1353: }
1354: sin->sin_family = AF_INET;
1355: sin->sin_len = sizeof(*sin);
1356: sin->sin_addr = dst;
1357:
1358: rtalloc(&ipforward_rt);
1359: }
1360: if (ipforward_rt.ro_rt == 0)
1361: return ((struct in_ifaddr *)0);
1.19 mycroft 1362: return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1.1 cgd 1363: }
1364:
1365: /*
1366: * Save incoming source route for use in replies,
1367: * to be picked up later by ip_srcroute if the receiver is interested.
1368: */
1.13 mycroft 1369: void
1.1 cgd 1370: save_rte(option, dst)
1371: u_char *option;
1372: struct in_addr dst;
1373: {
1374: unsigned olen;
1375:
1376: olen = option[IPOPT_OLEN];
1377: #ifdef DIAGNOSTIC
1378: if (ipprintfs)
1.39 christos 1379: printf("save_rte: olen %d\n", olen);
1.89 itojun 1380: #endif /* 0 */
1.1 cgd 1381: if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1382: return;
1383: bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
1384: ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1385: ip_srcrt.dst = dst;
1386: }
1387:
1388: /*
1389: * Retrieve incoming source route for use in replies,
1390: * in the same form used by setsockopt.
1391: * The first hop is placed before the options, will be removed later.
1392: */
1393: struct mbuf *
1394: ip_srcroute()
1395: {
1.109 augustss 1396: struct in_addr *p, *q;
1397: struct mbuf *m;
1.1 cgd 1398:
1399: if (ip_nhops == 0)
1400: return ((struct mbuf *)0);
1401: m = m_get(M_DONTWAIT, MT_SOOPTS);
1402: if (m == 0)
1403: return ((struct mbuf *)0);
1404:
1.164 ! matt 1405: MCLAIM(m, &inetdomain.dom_mowner);
1.13 mycroft 1406: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1 cgd 1407:
1408: /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1409: m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1410: OPTSIZ;
1411: #ifdef DIAGNOSTIC
1412: if (ipprintfs)
1.39 christos 1413: printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1.1 cgd 1414: #endif
1415:
1416: /*
1417: * First save first hop for return route
1418: */
1419: p = &ip_srcrt.route[ip_nhops - 1];
1420: *(mtod(m, struct in_addr *)) = *p--;
1421: #ifdef DIAGNOSTIC
1422: if (ipprintfs)
1.39 christos 1423: printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1.1 cgd 1424: #endif
1425:
1426: /*
1427: * Copy option fields and padding (nop) to mbuf.
1428: */
1429: ip_srcrt.nop = IPOPT_NOP;
1430: ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1431: bcopy((caddr_t)&ip_srcrt.nop,
1432: mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
1433: q = (struct in_addr *)(mtod(m, caddr_t) +
1434: sizeof(struct in_addr) + OPTSIZ);
1435: #undef OPTSIZ
1436: /*
1437: * Record return path as an IP source route,
1438: * reversing the path (pointers are now aligned).
1439: */
1440: while (p >= ip_srcrt.route) {
1441: #ifdef DIAGNOSTIC
1442: if (ipprintfs)
1.39 christos 1443: printf(" %x", ntohl(q->s_addr));
1.1 cgd 1444: #endif
1445: *q++ = *p--;
1446: }
1447: /*
1448: * Last hop goes to final destination.
1449: */
1450: *q = ip_srcrt.dst;
1451: #ifdef DIAGNOSTIC
1452: if (ipprintfs)
1.39 christos 1453: printf(" %x\n", ntohl(q->s_addr));
1.1 cgd 1454: #endif
1455: return (m);
1456: }
1457:
1458: /*
1459: * Strip out IP options, at higher
1460: * level protocol in the kernel.
1461: * Second argument is buffer to which options
1462: * will be moved, and return value is their length.
1463: * XXX should be deleted; last arg currently ignored.
1464: */
1.8 mycroft 1465: void
1.1 cgd 1466: ip_stripoptions(m, mopt)
1.109 augustss 1467: struct mbuf *m;
1.1 cgd 1468: struct mbuf *mopt;
1469: {
1.109 augustss 1470: int i;
1.1 cgd 1471: struct ip *ip = mtod(m, struct ip *);
1.109 augustss 1472: caddr_t opts;
1.1 cgd 1473: int olen;
1474:
1.79 mycroft 1475: olen = (ip->ip_hl << 2) - sizeof (struct ip);
1.1 cgd 1476: opts = (caddr_t)(ip + 1);
1477: i = m->m_len - (sizeof (struct ip) + olen);
1478: bcopy(opts + olen, opts, (unsigned)i);
1479: m->m_len -= olen;
1480: if (m->m_flags & M_PKTHDR)
1481: m->m_pkthdr.len -= olen;
1.155 itojun 1482: ip->ip_len = htons(ntohs(ip->ip_len) - olen);
1.79 mycroft 1483: ip->ip_hl = sizeof (struct ip) >> 2;
1.1 cgd 1484: }
1485:
1.139 matt 1486: const int inetctlerrmap[PRC_NCMDS] = {
1.1 cgd 1487: 0, 0, 0, 0,
1488: 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1489: EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1490: EMSGSIZE, EHOSTUNREACH, 0, 0,
1491: 0, 0, 0, 0,
1492: ENOPROTOOPT
1493: };
1494:
1495: /*
1496: * Forward a packet. If some error occurs return the sender
1497: * an icmp packet. Note we can't always generate a meaningful
1498: * icmp message because icmp doesn't have a large enough repertoire
1499: * of codes and types.
1500: *
1501: * If not forwarding, just drop the packet. This could be confusing
1502: * if ipforwarding was zero but some routing protocol was advancing
1503: * us as a gateway to somewhere. However, we must let the routing
1504: * protocol deal with that.
1505: *
1506: * The srcrt parameter indicates whether the packet is being forwarded
1507: * via a source route.
1508: */
1.13 mycroft 1509: void
1.1 cgd 1510: ip_forward(m, srcrt)
1511: struct mbuf *m;
1512: int srcrt;
1513: {
1.109 augustss 1514: struct ip *ip = mtod(m, struct ip *);
1515: struct sockaddr_in *sin;
1516: struct rtentry *rt;
1.28 christos 1517: int error, type = 0, code = 0;
1.1 cgd 1518: struct mbuf *mcopy;
1.13 mycroft 1519: n_long dest;
1520: struct ifnet *destifp;
1.89 itojun 1521: #ifdef IPSEC
1522: struct ifnet dummyifp;
1523: #endif
1.164 ! matt 1524:
! 1525: /*
! 1526: * We are now in the output path.
! 1527: */
! 1528: MCLAIM(m, &ip_tx_mowner);
1.135 thorpej 1529:
1530: /*
1531: * Clear any in-bound checksum flags for this packet.
1532: */
1533: m->m_pkthdr.csum_flags = 0;
1.1 cgd 1534:
1.13 mycroft 1535: dest = 0;
1.1 cgd 1536: #ifdef DIAGNOSTIC
1537: if (ipprintfs)
1.70 thorpej 1538: printf("forward: src %2.2x dst %2.2x ttl %x\n",
1539: ntohl(ip->ip_src.s_addr),
1540: ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
1.1 cgd 1541: #endif
1.93 sommerfe 1542: if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1.1 cgd 1543: ipstat.ips_cantforward++;
1544: m_freem(m);
1545: return;
1546: }
1547: if (ip->ip_ttl <= IPTTLDEC) {
1.13 mycroft 1548: icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1 cgd 1549: return;
1550: }
1551: ip->ip_ttl -= IPTTLDEC;
1552:
1.19 mycroft 1553: sin = satosin(&ipforward_rt.ro_dst);
1.1 cgd 1554: if ((rt = ipforward_rt.ro_rt) == 0 ||
1.35 mycroft 1555: !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1.1 cgd 1556: if (ipforward_rt.ro_rt) {
1557: RTFREE(ipforward_rt.ro_rt);
1558: ipforward_rt.ro_rt = 0;
1559: }
1560: sin->sin_family = AF_INET;
1.35 mycroft 1561: sin->sin_len = sizeof(struct sockaddr_in);
1.1 cgd 1562: sin->sin_addr = ip->ip_dst;
1563:
1564: rtalloc(&ipforward_rt);
1565: if (ipforward_rt.ro_rt == 0) {
1.13 mycroft 1566: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1.1 cgd 1567: return;
1568: }
1569: rt = ipforward_rt.ro_rt;
1570: }
1571:
1572: /*
1.34 mycroft 1573: * Save at most 68 bytes of the packet in case
1.1 cgd 1574: * we need to generate an ICMP message to the src.
1.119 itojun 1575: * Pullup to avoid sharing mbuf cluster between m and mcopy.
1.1 cgd 1576: */
1.155 itojun 1577: mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT);
1.119 itojun 1578: if (mcopy)
1579: mcopy = m_pullup(mcopy, ip->ip_hl << 2);
1.1 cgd 1580:
1581: /*
1582: * If forwarding packet using same interface that it came in on,
1583: * perhaps should send a redirect to sender to shortcut a hop.
1584: * Only send redirect if source is sending directly to us,
1585: * and if packet was not source routed (or has any options).
1586: * Also, don't send redirect if forwarding using a default route
1587: * or a route modified by a redirect.
1588: */
1589: if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1590: (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1.35 mycroft 1591: !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1.1 cgd 1592: ipsendredirects && !srcrt) {
1.19 mycroft 1593: if (rt->rt_ifa &&
1594: (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1595: ifatoia(rt->rt_ifa)->ia_subnet) {
1.77 thorpej 1596: if (rt->rt_flags & RTF_GATEWAY)
1597: dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1598: else
1599: dest = ip->ip_dst.s_addr;
1600: /*
1601: * Router requirements says to only send host
1602: * redirects.
1603: */
1604: type = ICMP_REDIRECT;
1605: code = ICMP_REDIRECT_HOST;
1.1 cgd 1606: #ifdef DIAGNOSTIC
1.77 thorpej 1607: if (ipprintfs)
1608: printf("redirect (%d) to %x\n", code,
1609: (u_int32_t)dest);
1.1 cgd 1610: #endif
1611: }
1612: }
1613:
1.89 itojun 1614: #ifdef IPSEC
1.134 lukem 1615: /* Don't lookup socket in forwarding case */
1.127 itojun 1616: (void)ipsec_setsocket(m, NULL);
1.103 itojun 1617: #endif
1.27 thorpej 1618: error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1619: (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1.1 cgd 1620: if (error)
1621: ipstat.ips_cantforward++;
1622: else {
1623: ipstat.ips_forward++;
1624: if (type)
1625: ipstat.ips_redirectsent++;
1626: else {
1.63 matt 1627: if (mcopy) {
1628: #ifdef GATEWAY
1.64 thorpej 1629: if (mcopy->m_flags & M_CANFASTFWD)
1630: ipflow_create(&ipforward_rt, mcopy);
1.63 matt 1631: #endif
1.1 cgd 1632: m_freem(mcopy);
1.63 matt 1633: }
1.1 cgd 1634: return;
1635: }
1636: }
1637: if (mcopy == NULL)
1638: return;
1.13 mycroft 1639: destifp = NULL;
1640:
1.1 cgd 1641: switch (error) {
1642:
1643: case 0: /* forwarded, but need redirect */
1644: /* type, code set above */
1645: break;
1646:
1647: case ENETUNREACH: /* shouldn't happen, checked above */
1648: case EHOSTUNREACH:
1649: case ENETDOWN:
1650: case EHOSTDOWN:
1651: default:
1652: type = ICMP_UNREACH;
1653: code = ICMP_UNREACH_HOST;
1654: break;
1655:
1656: case EMSGSIZE:
1657: type = ICMP_UNREACH;
1658: code = ICMP_UNREACH_NEEDFRAG;
1.89 itojun 1659: #ifndef IPSEC
1.13 mycroft 1660: if (ipforward_rt.ro_rt)
1661: destifp = ipforward_rt.ro_rt->rt_ifp;
1.89 itojun 1662: #else
1663: /*
1664: * If the packet is routed over IPsec tunnel, tell the
1665: * originator the tunnel MTU.
1666: * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1667: * XXX quickhack!!!
1668: */
1669: if (ipforward_rt.ro_rt) {
1670: struct secpolicy *sp;
1671: int ipsecerror;
1.95 itojun 1672: size_t ipsechdr;
1.89 itojun 1673: struct route *ro;
1674:
1675: sp = ipsec4_getpolicybyaddr(mcopy,
1.95 itojun 1676: IPSEC_DIR_OUTBOUND,
1677: IP_FORWARDING,
1678: &ipsecerror);
1.89 itojun 1679:
1680: if (sp == NULL)
1681: destifp = ipforward_rt.ro_rt->rt_ifp;
1682: else {
1683: /* count IPsec header size */
1.95 itojun 1684: ipsechdr = ipsec4_hdrsiz(mcopy,
1685: IPSEC_DIR_OUTBOUND,
1686: NULL);
1.89 itojun 1687:
1688: /*
1689: * find the correct route for outer IPv4
1690: * header, compute tunnel MTU.
1691: *
1692: * XXX BUG ALERT
1693: * The "dummyifp" code relies upon the fact
1694: * that icmp_error() touches only ifp->if_mtu.
1695: */
1696: /*XXX*/
1697: destifp = NULL;
1698: if (sp->req != NULL
1.95 itojun 1699: && sp->req->sav != NULL
1700: && sp->req->sav->sah != NULL) {
1701: ro = &sp->req->sav->sah->sa_route;
1.89 itojun 1702: if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1703: dummyifp.if_mtu =
1.151 itojun 1704: ro->ro_rt->rt_rmx.rmx_mtu ?
1705: ro->ro_rt->rt_rmx.rmx_mtu :
1.89 itojun 1706: ro->ro_rt->rt_ifp->if_mtu;
1707: dummyifp.if_mtu -= ipsechdr;
1708: destifp = &dummyifp;
1709: }
1710: }
1711:
1712: key_freesp(sp);
1713: }
1714: }
1715: #endif /*IPSEC*/
1.1 cgd 1716: ipstat.ips_cantfrag++;
1717: break;
1718:
1719: case ENOBUFS:
1.143 itojun 1720: #if 1
1721: /*
1722: * a router should not generate ICMP_SOURCEQUENCH as
1723: * required in RFC1812 Requirements for IP Version 4 Routers.
1724: * source quench could be a big problem under DoS attacks,
1.149 wiz 1725: * or if the underlying interface is rate-limited.
1.143 itojun 1726: */
1727: if (mcopy)
1728: m_freem(mcopy);
1729: return;
1730: #else
1.1 cgd 1731: type = ICMP_SOURCEQUENCH;
1732: code = 0;
1733: break;
1.143 itojun 1734: #endif
1.1 cgd 1735: }
1.13 mycroft 1736: icmp_error(mcopy, type, code, dest, destifp);
1.44 thorpej 1737: }
1738:
1739: void
1740: ip_savecontrol(inp, mp, ip, m)
1.109 augustss 1741: struct inpcb *inp;
1742: struct mbuf **mp;
1743: struct ip *ip;
1744: struct mbuf *m;
1.44 thorpej 1745: {
1746:
1747: if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1748: struct timeval tv;
1749:
1750: microtime(&tv);
1751: *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1752: SCM_TIMESTAMP, SOL_SOCKET);
1753: if (*mp)
1754: mp = &(*mp)->m_next;
1755: }
1756: if (inp->inp_flags & INP_RECVDSTADDR) {
1757: *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1758: sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1759: if (*mp)
1760: mp = &(*mp)->m_next;
1761: }
1762: #ifdef notyet
1763: /*
1764: * XXX
1765: * Moving these out of udp_input() made them even more broken
1766: * than they already were.
1767: * - fenner@parc.xerox.com
1768: */
1769: /* options were tossed already */
1770: if (inp->inp_flags & INP_RECVOPTS) {
1771: *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1772: sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1773: if (*mp)
1774: mp = &(*mp)->m_next;
1775: }
1776: /* ip_srcroute doesn't do what we want here, need to fix */
1777: if (inp->inp_flags & INP_RECVRETOPTS) {
1778: *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1779: sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1780: if (*mp)
1781: mp = &(*mp)->m_next;
1782: }
1783: #endif
1784: if (inp->inp_flags & INP_RECVIF) {
1785: struct sockaddr_dl sdl;
1786:
1787: sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1788: sdl.sdl_family = AF_LINK;
1789: sdl.sdl_index = m->m_pkthdr.rcvif ?
1790: m->m_pkthdr.rcvif->if_index : 0;
1791: sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1792: *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1793: IP_RECVIF, IPPROTO_IP);
1794: if (*mp)
1795: mp = &(*mp)->m_next;
1796: }
1.13 mycroft 1797: }
1798:
1799: int
1800: ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1801: int *name;
1802: u_int namelen;
1803: void *oldp;
1804: size_t *oldlenp;
1805: void *newp;
1806: size_t newlen;
1807: {
1.88 sommerfe 1808: extern int subnetsarelocal, hostzeroisbroadcast;
1.52 thorpej 1809:
1.54 lukem 1810: int error, old;
1811:
1.13 mycroft 1812: /* All sysctl names at this level are terminal. */
1813: if (namelen != 1)
1814: return (ENOTDIR);
1815:
1816: switch (name[0]) {
1817: case IPCTL_FORWARDING:
1818: return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
1819: case IPCTL_SENDREDIRECTS:
1820: return (sysctl_int(oldp, oldlenp, newp, newlen,
1821: &ipsendredirects));
1822: case IPCTL_DEFTTL:
1823: return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
1824: #ifdef notyet
1825: case IPCTL_DEFMTU:
1826: return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1827: #endif
1.26 thorpej 1828: case IPCTL_FORWSRCRT:
1.47 cjs 1829: /* Don't allow this to change in a secure environment. */
1.26 thorpej 1830: if (securelevel > 0)
1.46 cjs 1831: return (sysctl_rdint(oldp, oldlenp, newp,
1832: ip_forwsrcrt));
1833: else
1834: return (sysctl_int(oldp, oldlenp, newp, newlen,
1835: &ip_forwsrcrt));
1.27 thorpej 1836: case IPCTL_DIRECTEDBCAST:
1837: return (sysctl_int(oldp, oldlenp, newp, newlen,
1838: &ip_directedbcast));
1.47 cjs 1839: case IPCTL_ALLOWSRCRT:
1840: return (sysctl_int(oldp, oldlenp, newp, newlen,
1841: &ip_allowsrcrt));
1.52 thorpej 1842: case IPCTL_SUBNETSARELOCAL:
1843: return (sysctl_int(oldp, oldlenp, newp, newlen,
1844: &subnetsarelocal));
1.53 kml 1845: case IPCTL_MTUDISC:
1.60 kml 1846: error = sysctl_int(oldp, oldlenp, newp, newlen,
1847: &ip_mtudisc);
1.163 itojun 1848: if (error == 0 && ip_mtudisc == 0)
1849: rt_timer_queue_remove_all(ip_mtudisc_timeout_q, TRUE);
1.60 kml 1850: return error;
1.54 lukem 1851: case IPCTL_ANONPORTMIN:
1852: old = anonportmin;
1853: error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
1.118 itojun 1854: if (anonportmin >= anonportmax || anonportmin < 0
1855: || anonportmin > 65535
1.54 lukem 1856: #ifndef IPNOPRIVPORTS
1857: || anonportmin < IPPORT_RESERVED
1858: #endif
1859: ) {
1860: anonportmin = old;
1861: return (EINVAL);
1862: }
1863: return (error);
1864: case IPCTL_ANONPORTMAX:
1865: old = anonportmax;
1866: error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
1.118 itojun 1867: if (anonportmin >= anonportmax || anonportmax < 0
1868: || anonportmax > 65535
1.54 lukem 1869: #ifndef IPNOPRIVPORTS
1870: || anonportmax < IPPORT_RESERVED
1871: #endif
1872: ) {
1873: anonportmax = old;
1874: return (EINVAL);
1875: }
1.60 kml 1876: return (error);
1877: case IPCTL_MTUDISCTIMEOUT:
1.158 itojun 1878: old = ip_mtudisc_timeout;
1.60 kml 1879: error = sysctl_int(oldp, oldlenp, newp, newlen,
1880: &ip_mtudisc_timeout);
1.158 itojun 1881: if (ip_mtudisc_timeout < 0) {
1882: ip_mtudisc_timeout = old;
1.156 itojun 1883: return (EINVAL);
1.158 itojun 1884: }
1.163 itojun 1885: if (error == 0)
1.162 itojun 1886: rt_timer_queue_change(ip_mtudisc_timeout_q,
1887: ip_mtudisc_timeout);
1.54 lukem 1888: return (error);
1.65 matt 1889: #ifdef GATEWAY
1890: case IPCTL_MAXFLOWS:
1.67 thorpej 1891: {
1892: int s;
1893:
1.65 matt 1894: error = sysctl_int(oldp, oldlenp, newp, newlen,
1895: &ip_maxflows);
1.67 thorpej 1896: s = splsoftnet();
1.65 matt 1897: ipflow_reap(0);
1.67 thorpej 1898: splx(s);
1.65 matt 1899: return (error);
1.67 thorpej 1900: }
1.89 itojun 1901: #endif
1.90 itojun 1902: case IPCTL_HOSTZEROBROADCAST:
1903: return (sysctl_int(oldp, oldlenp, newp, newlen,
1904: &hostzeroisbroadcast));
1.89 itojun 1905: #if NGIF > 0
1906: case IPCTL_GIF_TTL:
1.157 itojun 1907: return (sysctl_int(oldp, oldlenp, newp, newlen,
1.90 itojun 1908: &ip_gif_ttl));
1.144 martin 1909: #endif
1910:
1911: #if NGRE > 0
1912: case IPCTL_GRE_TTL:
1.157 itojun 1913: return (sysctl_int(oldp, oldlenp, newp, newlen,
1.144 martin 1914: &ip_gre_ttl));
1.117 tron 1915: #endif
1916:
1917: #ifndef IPNOPRIVPORTS
1918: case IPCTL_LOWPORTMIN:
1919: old = lowportmin;
1920: error = sysctl_int(oldp, oldlenp, newp, newlen, &lowportmin);
1921: if (lowportmin >= lowportmax
1922: || lowportmin > IPPORT_RESERVEDMAX
1923: || lowportmin < IPPORT_RESERVEDMIN
1924: ) {
1925: lowportmin = old;
1926: return (EINVAL);
1927: }
1928: return (error);
1929: case IPCTL_LOWPORTMAX:
1930: old = lowportmax;
1931: error = sysctl_int(oldp, oldlenp, newp, newlen, &lowportmax);
1932: if (lowportmin >= lowportmax
1933: || lowportmax > IPPORT_RESERVEDMAX
1934: || lowportmax < IPPORT_RESERVEDMIN
1935: ) {
1936: lowportmax = old;
1937: return (EINVAL);
1938: }
1939: return (error);
1.65 matt 1940: #endif
1.131 itojun 1941:
1942: case IPCTL_MAXFRAGPACKETS:
1943: return (sysctl_int(oldp, oldlenp, newp, newlen,
1944: &ip_maxfragpackets));
1.88 sommerfe 1945:
1.13 mycroft 1946: default:
1947: return (EOPNOTSUPP);
1948: }
1949: /* NOTREACHED */
1.1 cgd 1950: }
CVSweb <webmaster@jp.NetBSD.org>