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