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