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