Annotation of src/sys/netinet/ip_input.c, Revision 1.268.2.5
1.268.2.5! yamt 1: /* $NetBSD: ip_input.c,v 1.268.2.4 2009/08/19 18:48:24 yamt 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: *
49: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59: * POSSIBILITY OF SUCH DAMAGE.
60: */
1.14 cgd 61:
1.1 cgd 62: /*
1.13 mycroft 63: * Copyright (c) 1982, 1986, 1988, 1993
64: * The Regents of the University of California. All rights reserved.
1.1 cgd 65: *
66: * Redistribution and use in source and binary forms, with or without
67: * modification, are permitted provided that the following conditions
68: * are met:
69: * 1. Redistributions of source code must retain the above copyright
70: * notice, this list of conditions and the following disclaimer.
71: * 2. Redistributions in binary form must reproduce the above copyright
72: * notice, this list of conditions and the following disclaimer in the
73: * documentation and/or other materials provided with the distribution.
1.172 agc 74: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 75: * may be used to endorse or promote products derived from this software
76: * without specific prior written permission.
77: *
78: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88: * SUCH DAMAGE.
89: *
1.14 cgd 90: * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
1.1 cgd 91: */
1.141 lukem 92:
93: #include <sys/cdefs.h>
1.268.2.5! yamt 94: __KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.268.2.4 2009/08/19 18:48:24 yamt Exp $");
1.55 scottr 95:
1.184 jonathan 96: #include "opt_inet.h"
1.268.2.2 yamt 97: #include "opt_compat_netbsd.h"
1.62 matt 98: #include "opt_gateway.h"
1.69 mrg 99: #include "opt_pfil_hooks.h"
1.91 thorpej 100: #include "opt_ipsec.h"
1.55 scottr 101: #include "opt_mrouting.h"
1.167 martin 102: #include "opt_mbuftrace.h"
1.135 thorpej 103: #include "opt_inet_csum.h"
1.1 cgd 104:
1.5 mycroft 105: #include <sys/param.h>
106: #include <sys/systm.h>
107: #include <sys/malloc.h>
108: #include <sys/mbuf.h>
109: #include <sys/domain.h>
110: #include <sys/protosw.h>
111: #include <sys/socket.h>
1.44 thorpej 112: #include <sys/socketvar.h>
1.5 mycroft 113: #include <sys/errno.h>
114: #include <sys/time.h>
115: #include <sys/kernel.h>
1.72 thorpej 116: #include <sys/pool.h>
1.28 christos 117: #include <sys/sysctl.h>
1.230 elad 118: #include <sys/kauth.h>
1.1 cgd 119:
1.5 mycroft 120: #include <net/if.h>
1.44 thorpej 121: #include <net/if_dl.h>
1.5 mycroft 122: #include <net/route.h>
1.45 mrg 123: #include <net/pfil.h>
1.1 cgd 124:
1.5 mycroft 125: #include <netinet/in.h>
126: #include <netinet/in_systm.h>
127: #include <netinet/ip.h>
128: #include <netinet/in_pcb.h>
1.215 yamt 129: #include <netinet/in_proto.h>
1.5 mycroft 130: #include <netinet/in_var.h>
131: #include <netinet/ip_var.h>
1.266 thorpej 132: #include <netinet/ip_private.h>
1.5 mycroft 133: #include <netinet/ip_icmp.h>
1.89 itojun 134: /* just for gif_ttl */
135: #include <netinet/in_gif.h>
136: #include "gif.h"
1.144 martin 137: #include <net/if_gre.h>
138: #include "gre.h"
1.111 jdolecek 139:
140: #ifdef MROUTING
141: #include <netinet/ip_mroute.h>
142: #endif
1.89 itojun 143:
144: #ifdef IPSEC
145: #include <netinet6/ipsec.h>
1.267 thorpej 146: #include <netinet6/ipsec_private.h>
1.89 itojun 147: #include <netkey/key.h>
148: #endif
1.173 jonathan 149: #ifdef FAST_IPSEC
150: #include <netipsec/ipsec.h>
151: #include <netipsec/key.h>
152: #endif /* FAST_IPSEC*/
1.44 thorpej 153:
1.1 cgd 154: #ifndef IPFORWARDING
155: #ifdef GATEWAY
156: #define IPFORWARDING 1 /* forward IP packets not for us */
157: #else /* GATEWAY */
158: #define IPFORWARDING 0 /* don't forward IP packets not for us */
159: #endif /* GATEWAY */
160: #endif /* IPFORWARDING */
161: #ifndef IPSENDREDIRECTS
162: #define IPSENDREDIRECTS 1
163: #endif
1.26 thorpej 164: #ifndef IPFORWSRCRT
1.47 cjs 165: #define IPFORWSRCRT 1 /* forward source-routed packets */
166: #endif
167: #ifndef IPALLOWSRCRT
1.48 mrg 168: #define IPALLOWSRCRT 1 /* allow source-routed packets */
1.26 thorpej 169: #endif
1.53 kml 170: #ifndef IPMTUDISC
1.153 itojun 171: #define IPMTUDISC 1
1.53 kml 172: #endif
1.60 kml 173: #ifndef IPMTUDISCTIMEOUT
1.61 kml 174: #define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */
1.60 kml 175: #endif
1.53 kml 176:
1.268.2.2 yamt 177: #ifdef COMPAT_50
178: #include <compat/sys/time.h>
179: #include <compat/sys/socket.h>
180: #endif
181:
1.27 thorpej 182: /*
183: * Note: DIRECTED_BROADCAST is handled this way so that previous
184: * configuration using this option will Just Work.
185: */
186: #ifndef IPDIRECTEDBCAST
187: #ifdef DIRECTED_BROADCAST
188: #define IPDIRECTEDBCAST 1
189: #else
190: #define IPDIRECTEDBCAST 0
191: #endif /* DIRECTED_BROADCAST */
192: #endif /* IPDIRECTEDBCAST */
1.1 cgd 193: int ipforwarding = IPFORWARDING;
194: int ipsendredirects = IPSENDREDIRECTS;
1.13 mycroft 195: int ip_defttl = IPDEFTTL;
1.26 thorpej 196: int ip_forwsrcrt = IPFORWSRCRT;
1.27 thorpej 197: int ip_directedbcast = IPDIRECTEDBCAST;
1.47 cjs 198: int ip_allowsrcrt = IPALLOWSRCRT;
1.53 kml 199: int ip_mtudisc = IPMTUDISC;
1.156 itojun 200: int ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
1.1 cgd 201: #ifdef DIAGNOSTIC
202: int ipprintfs = 0;
203: #endif
1.184 jonathan 204:
205: int ip_do_randomid = 0;
206:
1.165 christos 207: /*
208: * XXX - Setting ip_checkinterface mostly implements the receive side of
209: * the Strong ES model described in RFC 1122, but since the routing table
210: * and transmit implementation do not implement the Strong ES model,
211: * setting this to 1 results in an odd hybrid.
212: *
213: * XXX - ip_checkinterface currently must be disabled if you use ipnat
214: * to translate the destination address to another local interface.
215: *
216: * XXX - ip_checkinterface must be disabled if you add IP aliases
217: * to the loopback interface instead of the interface where the
218: * packets for those addresses are received.
219: */
220: int ip_checkinterface = 0;
221:
1.1 cgd 222:
1.60 kml 223: struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
224:
1.1 cgd 225: int ipqmaxlen = IFQ_MAXLEN;
1.150 matt 226: u_long in_ifaddrhash; /* size of hash table - 1 */
227: int in_ifaddrentries; /* total number of addrs */
1.212 perry 228: struct in_ifaddrhead in_ifaddrhead;
1.57 tls 229: struct in_ifaddrhashhead *in_ifaddrhashtbl;
1.166 matt 230: u_long in_multihash; /* size of hash table - 1 */
231: int in_multientries; /* total number of addrs */
232: struct in_multihashhead *in_multihashtbl;
1.13 mycroft 233: struct ifqueue ipintrq;
1.183 jonathan 234: uint16_t ip_id;
1.75 thorpej 235:
1.266 thorpej 236: percpu_t *ipstat_percpu;
237:
1.121 thorpej 238: #ifdef PFIL_HOOKS
239: struct pfil_head inet_pfil_hook;
240: #endif
241:
1.194 jonathan 242: /*
243: * Cached copy of nmbclusters. If nbclusters is different,
244: * recalculate IP parameters derived from nmbclusters.
245: */
246: static int ip_nmbclusters; /* copy of nmbclusters */
1.210 perry 247: static void ip_nmbclusters_changed(void); /* recalc limits */
1.194 jonathan 248:
1.195 thorpej 249: #define CHECK_NMBCLUSTER_PARAMS() \
250: do { \
251: if (__predict_false(ip_nmbclusters != nmbclusters)) \
252: ip_nmbclusters_changed(); \
253: } while (/*CONSTCOND*/0)
1.194 jonathan 254:
1.190 jonathan 255: /* IP datagram reassembly queues (hashed) */
256: #define IPREASS_NHASH_LOG2 6
257: #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
258: #define IPREASS_HMASK (IPREASS_NHASH - 1)
259: #define IPREASS_HASH(x,y) \
260: (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
261: struct ipqhead ipq[IPREASS_NHASH];
1.75 thorpej 262: int ipq_locked;
1.212 perry 263: static int ip_nfragpackets; /* packets in reass queue */
1.194 jonathan 264: static int ip_nfrags; /* total fragments in reass queues */
265:
266: int ip_maxfragpackets = 200; /* limit on packets. XXX sysctl */
267: int ip_maxfrags; /* limit on fragments. XXX sysctl */
268:
269:
270: /*
271: * Additive-Increase/Multiplicative-Decrease (AIMD) strategy for
272: * IP reassembly queue buffer managment.
1.212 perry 273: *
1.194 jonathan 274: * We keep a count of total IP fragments (NB: not fragmented packets!)
275: * awaiting reassembly (ip_nfrags) and a limit (ip_maxfrags) on fragments.
276: * If ip_nfrags exceeds ip_maxfrags the limit, we drop half the
277: * total fragments in reassembly queues.This AIMD policy avoids
278: * repeatedly deleting single packets under heavy fragmentation load
279: * (e.g., from lossy NFS peers).
280: */
1.212 perry 281: static u_int ip_reass_ttl_decr(u_int ticks);
1.210 perry 282: static void ip_reass_drophalf(void);
1.194 jonathan 283:
1.75 thorpej 284:
1.223 perry 285: static inline int ipq_lock_try(void);
286: static inline void ipq_unlock(void);
1.75 thorpej 287:
1.223 perry 288: static inline int
1.211 perry 289: ipq_lock_try(void)
1.75 thorpej 290: {
291: int s;
292:
1.132 thorpej 293: /*
1.149 wiz 294: * Use splvm() -- we're blocking things that would cause
1.132 thorpej 295: * mbuf allocation.
296: */
297: s = splvm();
1.75 thorpej 298: if (ipq_locked) {
299: splx(s);
300: return (0);
301: }
302: ipq_locked = 1;
303: splx(s);
304: return (1);
305: }
306:
1.223 perry 307: static inline void
1.211 perry 308: ipq_unlock(void)
1.75 thorpej 309: {
310: int s;
311:
1.132 thorpej 312: s = splvm();
1.75 thorpej 313: ipq_locked = 0;
314: splx(s);
315: }
316:
317: #ifdef DIAGNOSTIC
318: #define IPQ_LOCK() \
319: do { \
320: if (ipq_lock_try() == 0) { \
321: printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
322: panic("ipq_lock"); \
323: } \
1.159 perry 324: } while (/*CONSTCOND*/ 0)
1.75 thorpej 325: #define IPQ_LOCK_CHECK() \
326: do { \
327: if (ipq_locked == 0) { \
328: printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
329: panic("ipq lock check"); \
330: } \
1.159 perry 331: } while (/*CONSTCOND*/ 0)
1.75 thorpej 332: #else
333: #define IPQ_LOCK() (void) ipq_lock_try()
334: #define IPQ_LOCK_CHECK() /* nothing */
335: #endif
336:
337: #define IPQ_UNLOCK() ipq_unlock()
1.1 cgd 338:
1.268.2.2 yamt 339: struct pool inmulti_pool;
340: struct pool ipqent_pool;
1.72 thorpej 341:
1.135 thorpej 342: #ifdef INET_CSUM_COUNTERS
343: #include <sys/device.h>
344:
345: struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
346: NULL, "inet", "hwcsum bad");
347: struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
348: NULL, "inet", "hwcsum ok");
349: struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
350: NULL, "inet", "swcsum");
351:
352: #define INET_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
353:
1.201 matt 354: EVCNT_ATTACH_STATIC(ip_hwcsum_bad);
355: EVCNT_ATTACH_STATIC(ip_hwcsum_ok);
356: EVCNT_ATTACH_STATIC(ip_swcsum);
357:
1.135 thorpej 358: #else
359:
360: #define INET_CSUM_COUNTER_INCR(ev) /* nothing */
361:
362: #endif /* INET_CSUM_COUNTERS */
363:
1.1 cgd 364: /*
365: * We need to save the IP options in case a protocol wants to respond
366: * to an incoming packet over the same route if the packet got here
367: * using IP source routing. This allows connection establishment and
368: * maintenance when the remote end is on a network that is not known
369: * to us.
370: */
371: int ip_nhops = 0;
372: static struct ip_srcrt {
373: struct in_addr dst; /* final destination */
374: char nop; /* one NOP to align */
375: char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
376: struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
377: } ip_srcrt;
378:
1.210 perry 379: static void save_rte(u_char *, struct in_addr);
1.35 mycroft 380:
1.164 matt 381: #ifdef MBUFTRACE
1.234 dogcow 382: struct mowner ip_rx_mowner = MOWNER_INIT("internet", "rx");
383: struct mowner ip_tx_mowner = MOWNER_INIT("internet", "tx");
1.164 matt 384: #endif
385:
1.268.2.5! yamt 386: static void sysctl_net_inet_ip_setup(struct sysctllog **);
! 387:
1.1 cgd 388: /*
1.194 jonathan 389: * Compute IP limits derived from the value of nmbclusters.
390: */
391: static void
392: ip_nmbclusters_changed(void)
393: {
394: ip_maxfrags = nmbclusters / 4;
395: ip_nmbclusters = nmbclusters;
396: }
397:
398: /*
1.1 cgd 399: * IP initialization: fill in IP protocol switch table.
400: * All protocols not implemented in kernel go to raw IP protocol handler.
401: */
1.8 mycroft 402: void
1.211 perry 403: ip_init(void)
1.1 cgd 404: {
1.199 matt 405: const struct protosw *pr;
1.109 augustss 406: int i;
1.1 cgd 407:
1.268.2.5! yamt 408: sysctl_net_inet_ip_setup(NULL);
! 409:
1.268.2.2 yamt 410: pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
411: NULL, IPL_SOFTNET);
412: pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl",
413: NULL, IPL_VM);
414:
1.1 cgd 415: pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
416: if (pr == 0)
417: panic("ip_init");
418: for (i = 0; i < IPPROTO_MAX; i++)
419: ip_protox[i] = pr - inetsw;
420: for (pr = inetdomain.dom_protosw;
421: pr < inetdomain.dom_protoswNPROTOSW; pr++)
422: if (pr->pr_domain->dom_family == PF_INET &&
423: pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
424: ip_protox[pr->pr_protocol] = pr - inetsw;
1.192 jonathan 425:
1.190 jonathan 426: for (i = 0; i < IPREASS_NHASH; i++)
427: LIST_INIT(&ipq[i]);
428:
1.262 matt 429: ip_initid();
1.227 kardel 430: ip_id = time_second & 0xfffff;
1.194 jonathan 431:
1.1 cgd 432: ipintrq.ifq_maxlen = ipqmaxlen;
1.194 jonathan 433: ip_nmbclusters_changed();
434:
1.181 jonathan 435: TAILQ_INIT(&in_ifaddrhead);
1.268.2.1 yamt 436: in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
437: &in_ifaddrhash);
438: in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
439: &in_multihash);
1.160 itojun 440: ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout);
1.73 thorpej 441: #ifdef GATEWAY
1.248 liamjfoy 442: ipflow_init(ip_hashsize);
1.73 thorpej 443: #endif
1.121 thorpej 444:
445: #ifdef PFIL_HOOKS
446: /* Register our Packet Filter hook. */
1.126 thorpej 447: inet_pfil_hook.ph_type = PFIL_TYPE_AF;
448: inet_pfil_hook.ph_af = AF_INET;
1.121 thorpej 449: i = pfil_head_register(&inet_pfil_hook);
450: if (i != 0)
451: printf("ip_init: WARNING: unable to register pfil hook, "
452: "error %d\n", i);
453: #endif /* PFIL_HOOKS */
1.135 thorpej 454:
1.164 matt 455: #ifdef MBUFTRACE
456: MOWNER_ATTACH(&ip_tx_mowner);
457: MOWNER_ATTACH(&ip_rx_mowner);
458: #endif /* MBUFTRACE */
1.266 thorpej 459:
460: ipstat_percpu = percpu_alloc(sizeof(uint64_t) * IP_NSTATS);
1.1 cgd 461: }
462:
1.229 christos 463: struct sockaddr_in ipaddr = {
464: .sin_len = sizeof(ipaddr),
465: .sin_family = AF_INET,
466: };
1.1 cgd 467: struct route ipforward_rt;
468:
469: /*
1.89 itojun 470: * IP software interrupt routine
471: */
472: void
1.211 perry 473: ipintr(void)
1.89 itojun 474: {
475: int s;
476: struct mbuf *m;
477:
1.268 ad 478: mutex_enter(softnet_lock);
479: KERNEL_LOCK(1, NULL);
1.241 ad 480: while (!IF_IS_EMPTY(&ipintrq)) {
1.132 thorpej 481: s = splnet();
1.89 itojun 482: IF_DEQUEUE(&ipintrq, m);
483: splx(s);
1.268 ad 484: if (m == NULL)
485: break;
1.89 itojun 486: ip_input(m);
487: }
1.268 ad 488: KERNEL_UNLOCK_ONE(NULL);
489: mutex_exit(softnet_lock);
1.89 itojun 490: }
491:
492: /*
1.1 cgd 493: * Ip input routine. Checksum and byte swap header. If fragmented
494: * try to reassemble. Process options. Pass to next level.
495: */
1.8 mycroft 496: void
1.89 itojun 497: ip_input(struct mbuf *m)
1.1 cgd 498: {
1.109 augustss 499: struct ip *ip = NULL;
500: struct ipq *fp;
501: struct in_ifaddr *ia;
502: struct ifaddr *ifa;
1.25 cgd 503: struct ipqent *ipqe;
1.89 itojun 504: int hlen = 0, mff, len;
1.100 itojun 505: int downmatch;
1.165 christos 506: int checkif;
1.169 itojun 507: int srcrt = 0;
1.233 tls 508: int s;
1.190 jonathan 509: u_int hash;
1.173 jonathan 510: #ifdef FAST_IPSEC
511: struct m_tag *mtag;
512: struct tdb_ident *tdbi;
513: struct secpolicy *sp;
1.233 tls 514: int error;
1.173 jonathan 515: #endif /* FAST_IPSEC */
1.1 cgd 516:
1.164 matt 517: MCLAIM(m, &ip_rx_mowner);
1.1 cgd 518: #ifdef DIAGNOSTIC
519: if ((m->m_flags & M_PKTHDR) == 0)
520: panic("ipintr no HDR");
1.89 itojun 521: #endif
1.164 matt 522:
1.1 cgd 523: /*
524: * If no IP addresses have been set yet but the interfaces
525: * are receiving, can't do anything with incoming packets yet.
526: */
1.181 jonathan 527: if (TAILQ_FIRST(&in_ifaddrhead) == 0)
1.1 cgd 528: goto bad;
1.266 thorpej 529: IP_STATINC(IP_STAT_TOTAL);
1.154 thorpej 530: /*
531: * If the IP header is not aligned, slurp it up into a new
532: * mbuf with space for link headers, in the event we forward
533: * it. Otherwise, if it is aligned, make sure the entire
534: * base IP header is in the first mbuf of the chain.
535: */
1.244 christos 536: if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
1.154 thorpej 537: if ((m = m_copyup(m, sizeof(struct ip),
538: (max_linkhdr + 3) & ~3)) == NULL) {
539: /* XXXJRT new stat, please */
1.266 thorpej 540: IP_STATINC(IP_STAT_TOOSMALL);
1.154 thorpej 541: return;
542: }
543: } else if (__predict_false(m->m_len < sizeof (struct ip))) {
544: if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
1.266 thorpej 545: IP_STATINC(IP_STAT_TOOSMALL);
1.154 thorpej 546: return;
547: }
1.1 cgd 548: }
549: ip = mtod(m, struct ip *);
1.13 mycroft 550: if (ip->ip_v != IPVERSION) {
1.266 thorpej 551: IP_STATINC(IP_STAT_BADVERS);
1.13 mycroft 552: goto bad;
553: }
1.1 cgd 554: hlen = ip->ip_hl << 2;
555: if (hlen < sizeof(struct ip)) { /* minimum header length */
1.266 thorpej 556: IP_STATINC(IP_STAT_BADHLEN);
1.1 cgd 557: goto bad;
558: }
559: if (hlen > m->m_len) {
560: if ((m = m_pullup(m, hlen)) == 0) {
1.266 thorpej 561: IP_STATINC(IP_STAT_BADHLEN);
1.89 itojun 562: return;
1.1 cgd 563: }
564: ip = mtod(m, struct ip *);
565: }
1.98 thorpej 566:
1.85 hwr 567: /*
1.99 thorpej 568: * RFC1122: packets with a multicast source address are
1.98 thorpej 569: * not allowed.
1.85 hwr 570: */
571: if (IN_MULTICAST(ip->ip_src.s_addr)) {
1.266 thorpej 572: IP_STATINC(IP_STAT_BADADDR);
1.85 hwr 573: goto bad;
1.129 itojun 574: }
575:
576: /* 127/8 must not appear on wire - RFC1122 */
577: if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
578: (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
1.130 itojun 579: if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
1.266 thorpej 580: IP_STATINC(IP_STAT_BADADDR);
1.129 itojun 581: goto bad;
1.130 itojun 582: }
1.85 hwr 583: }
584:
1.135 thorpej 585: switch (m->m_pkthdr.csum_flags &
1.137 thorpej 586: ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
1.135 thorpej 587: M_CSUM_IPv4_BAD)) {
588: case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
589: INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad);
590: goto badcsum;
591:
592: case M_CSUM_IPv4:
593: /* Checksum was okay. */
594: INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok);
595: break;
596:
597: default:
1.206 thorpej 598: /*
599: * Must compute it ourselves. Maybe skip checksum on
600: * loopback interfaces.
601: */
602: if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
603: IFF_LOOPBACK) || ip_do_loopback_cksum)) {
604: INET_CSUM_COUNTER_INCR(&ip_swcsum);
605: if (in_cksum(m, hlen) != 0)
606: goto badcsum;
607: }
1.135 thorpej 608: break;
1.1 cgd 609: }
610:
1.121 thorpej 611: /* Retrieve the packet length. */
612: len = ntohs(ip->ip_len);
1.81 proff 613:
614: /*
615: * Check for additional length bogosity
616: */
1.84 proff 617: if (len < hlen) {
1.266 thorpej 618: IP_STATINC(IP_STAT_BADLEN);
1.81 proff 619: goto bad;
620: }
1.1 cgd 621:
622: /*
623: * Check that the amount of data in the buffers
624: * is as at least much as the IP header would have us expect.
625: * Trim mbufs if longer than we expect.
626: * Drop packet if shorter than we expect.
627: */
1.35 mycroft 628: if (m->m_pkthdr.len < len) {
1.266 thorpej 629: IP_STATINC(IP_STAT_TOOSHORT);
1.1 cgd 630: goto bad;
631: }
1.35 mycroft 632: if (m->m_pkthdr.len > len) {
1.1 cgd 633: if (m->m_len == m->m_pkthdr.len) {
1.35 mycroft 634: m->m_len = len;
635: m->m_pkthdr.len = len;
1.1 cgd 636: } else
1.35 mycroft 637: m_adj(m, len - m->m_pkthdr.len);
1.1 cgd 638: }
639:
1.193 scw 640: #if defined(IPSEC)
1.149 wiz 641: /* ipflow (IP fast forwarding) is not compatible with IPsec. */
1.94 itojun 642: m->m_flags &= ~M_CANFASTFWD;
643: #else
1.64 thorpej 644: /*
645: * Assume that we can create a fast-forward IP flow entry
646: * based on this packet.
647: */
648: m->m_flags |= M_CANFASTFWD;
1.94 itojun 649: #endif
1.64 thorpej 650:
1.36 mrg 651: #ifdef PFIL_HOOKS
1.33 mrg 652: /*
1.64 thorpej 653: * Run through list of hooks for input packets. If there are any
654: * filters which require that additional packets in the flow are
655: * not fast-forwarded, they must clear the M_CANFASTFWD flag.
656: * Note that filters must _never_ set this flag, as another filter
657: * in the list may have previously cleared it.
1.33 mrg 658: */
1.127 itojun 659: /*
660: * let ipfilter look at packet on the wire,
661: * not the decapsulated packet.
662: */
663: #ifdef IPSEC
1.136 itojun 664: if (!ipsec_getnhist(m))
1.186 scw 665: #elif defined(FAST_IPSEC)
666: if (!ipsec_indone(m))
1.127 itojun 667: #else
668: if (1)
669: #endif
670: {
1.169 itojun 671: struct in_addr odst;
672:
673: odst = ip->ip_dst;
1.127 itojun 674: if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
1.168 itojun 675: PFIL_IN) != 0)
676: return;
1.127 itojun 677: if (m == NULL)
678: return;
679: ip = mtod(m, struct ip *);
1.142 darrenr 680: hlen = ip->ip_hl << 2;
1.205 darrenr 681: /*
682: * XXX The setting of "srcrt" here is to prevent ip_forward()
683: * from generating ICMP redirects for packets that have
684: * been redirected by a hook back out on to the same LAN that
685: * they came from and is not an indication that the packet
686: * is being inffluenced by source routing options. This
687: * allows things like
688: * "rdr tlp0 0/0 port 80 -> 1.1.1.200 3128 tcp"
689: * where tlp0 is both on the 1.1.1.0/24 network and is the
690: * default route for hosts on 1.1.1.0/24. Of course this
691: * also requires a "map tlp0 ..." to complete the story.
692: * One might argue whether or not this kind of network config.
1.212 perry 693: * should be supported in this manner...
1.205 darrenr 694: */
1.169 itojun 695: srcrt = (odst.s_addr != ip->ip_dst.s_addr);
1.127 itojun 696: }
1.36 mrg 697: #endif /* PFIL_HOOKS */
1.123 thorpej 698:
699: #ifdef ALTQ
700: /* XXX Temporary until ALTQ is changed to use a pfil hook */
701: if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
702: /* packet dropped by traffic conditioner */
703: return;
704: }
705: #endif
1.121 thorpej 706:
707: /*
1.1 cgd 708: * Process options and, if not destined for us,
709: * ship it on. ip_dooptions returns 1 when an
710: * error was detected (causing an icmp message
711: * to be sent and the original packet to be freed).
712: */
713: ip_nhops = 0; /* for source routed packets */
714: if (hlen > sizeof (struct ip) && ip_dooptions(m))
1.89 itojun 715: return;
1.1 cgd 716:
717: /*
1.165 christos 718: * Enable a consistency check between the destination address
719: * and the arrival interface for a unicast packet (the RFC 1122
720: * strong ES model) if IP forwarding is disabled and the packet
721: * is not locally generated.
722: *
723: * XXX - Checking also should be disabled if the destination
724: * address is ipnat'ed to a different interface.
725: *
726: * XXX - Checking is incompatible with IP aliases added
727: * to the loopback interface instead of the interface where
728: * the packets are received.
729: *
730: * XXX - We need to add a per ifaddr flag for this so that
731: * we get finer grain control.
732: */
733: checkif = ip_checkinterface && (ipforwarding == 0) &&
734: (m->m_pkthdr.rcvif != NULL) &&
735: ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0);
736:
737: /*
1.1 cgd 738: * Check our list of addresses, to see if the packet is for us.
1.100 itojun 739: *
740: * Traditional 4.4BSD did not consult IFF_UP at all.
741: * The behavior here is to treat addresses on !IFF_UP interface
742: * as not mine.
1.1 cgd 743: */
1.100 itojun 744: downmatch = 0;
1.140 matt 745: LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
1.97 itojun 746: if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
1.165 christos 747: if (checkif && ia->ia_ifp != m->m_pkthdr.rcvif)
748: continue;
1.97 itojun 749: if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
750: break;
1.100 itojun 751: else
752: downmatch++;
1.97 itojun 753: }
754: }
1.86 thorpej 755: if (ia != NULL)
756: goto ours;
1.225 christos 757: if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
1.209 matt 758: IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
1.140 matt 759: if (ifa->ifa_addr->sa_family != AF_INET)
760: continue;
1.57 tls 761: ia = ifatoia(ifa);
1.35 mycroft 762: if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
763: in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
1.20 mycroft 764: /*
765: * Look for all-0's host part (old broadcast addr),
766: * either for subnet or net.
767: */
768: ip->ip_dst.s_addr == ia->ia_subnet ||
1.18 mycroft 769: ip->ip_dst.s_addr == ia->ia_net)
1.1 cgd 770: goto ours;
1.57 tls 771: /*
772: * An interface with IP address zero accepts
773: * all packets that arrive on that interface.
774: */
775: if (in_nullhost(ia->ia_addr.sin_addr))
776: goto ours;
1.1 cgd 777: }
778: }
1.18 mycroft 779: if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4 hpeyerl 780: struct in_multi *inm;
781: #ifdef MROUTING
782: extern struct socket *ip_mrouter;
1.10 brezak 783:
1.4 hpeyerl 784: if (ip_mrouter) {
785: /*
786: * If we are acting as a multicast router, all
787: * incoming multicast packets are passed to the
788: * kernel-level multicast forwarding function.
789: * The packet is returned (relatively) intact; if
790: * ip_mforward() returns a non-zero value, the packet
791: * must be discarded, else it may be accepted below.
792: *
793: * (The IP ident field is put in the same byte order
794: * as expected when ip_mforward() is called from
795: * ip_output().)
796: */
1.13 mycroft 797: if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
1.266 thorpej 798: IP_STATINC(IP_STAT_CANTFORWARD);
1.4 hpeyerl 799: m_freem(m);
1.89 itojun 800: return;
1.4 hpeyerl 801: }
802:
803: /*
804: * The process-level routing demon needs to receive
805: * all multicast IGMP packets, whether or not this
806: * host belongs to their destination groups.
807: */
808: if (ip->ip_p == IPPROTO_IGMP)
809: goto ours;
1.266 thorpej 810: IP_STATINC(IP_STAT_CANTFORWARD);
1.4 hpeyerl 811: }
812: #endif
813: /*
814: * See if we belong to the destination multicast group on the
815: * arrival interface.
816: */
817: IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
818: if (inm == NULL) {
1.266 thorpej 819: IP_STATINC(IP_STAT_CANTFORWARD);
1.4 hpeyerl 820: m_freem(m);
1.89 itojun 821: return;
1.4 hpeyerl 822: }
823: goto ours;
824: }
1.19 mycroft 825: if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
1.35 mycroft 826: in_nullhost(ip->ip_dst))
1.1 cgd 827: goto ours;
828:
829: /*
830: * Not for us; forward if possible and desirable.
831: */
832: if (ipforwarding == 0) {
1.266 thorpej 833: IP_STATINC(IP_STAT_CANTFORWARD);
1.1 cgd 834: m_freem(m);
1.100 itojun 835: } else {
836: /*
837: * If ip_dst matched any of my address on !IFF_UP interface,
838: * and there's no IFF_UP interface that matches ip_dst,
839: * send icmp unreach. Forwarding it will result in in-kernel
840: * forwarding loop till TTL goes to 0.
841: */
842: if (downmatch) {
843: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1.266 thorpej 844: IP_STATINC(IP_STAT_CANTFORWARD);
1.100 itojun 845: return;
846: }
1.145 itojun 847: #ifdef IPSEC
848: if (ipsec4_in_reject(m, NULL)) {
1.267 thorpej 849: IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.145 itojun 850: goto bad;
851: }
852: #endif
1.173 jonathan 853: #ifdef FAST_IPSEC
854: mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
855: s = splsoftnet();
856: if (mtag != NULL) {
857: tdbi = (struct tdb_ident *)(mtag + 1);
858: sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
859: } else {
860: sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
1.212 perry 861: IP_FORWARDING, &error);
1.173 jonathan 862: }
863: if (sp == NULL) { /* NB: can happen if error */
864: splx(s);
865: /*XXX error stat???*/
866: DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/
867: goto bad;
868: }
869:
870: /*
871: * Check security policy against packet attributes.
872: */
873: error = ipsec_in_reject(sp, m);
874: KEY_FREESP(&sp);
875: splx(s);
876: if (error) {
1.266 thorpej 877: IP_STATINC(IP_STAT_CANTFORWARD);
1.173 jonathan 878: goto bad;
1.193 scw 879: }
880:
881: /*
882: * Peek at the outbound SP for this packet to determine if
883: * it's a Fast Forward candidate.
884: */
885: mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
886: if (mtag != NULL)
887: m->m_flags &= ~M_CANFASTFWD;
888: else {
889: s = splsoftnet();
890: sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND,
891: (IP_FORWARDING |
892: (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
893: &error, NULL);
894: if (sp != NULL) {
895: m->m_flags &= ~M_CANFASTFWD;
896: KEY_FREESP(&sp);
897: }
898: splx(s);
1.173 jonathan 899: }
900: #endif /* FAST_IPSEC */
1.145 itojun 901:
1.169 itojun 902: ip_forward(m, srcrt);
1.100 itojun 903: }
1.89 itojun 904: return;
1.1 cgd 905:
906: ours:
907: /*
908: * If offset or IP_MF are set, must reassemble.
909: * Otherwise, nothing need be done.
910: * (We could look in the reassembly queue to see
911: * if the packet was previously fragmented,
912: * but it's not worth the time; just let them time out.)
913: */
1.155 itojun 914: if (ip->ip_off & ~htons(IP_DF|IP_RF)) {
1.258 matt 915: uint16_t off;
916: /*
917: * Prevent TCP blind data attacks by not allowing non-initial
918: * fragments to start at less than 68 bytes (minimal fragment
1.259 matt 919: * size) and making sure the first fragment is at least 68
920: * bytes.
1.258 matt 921: */
1.260 matt 922: off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
1.259 matt 923: if ((off > 0 ? off + hlen : len) < IP_MINFRAGSIZE - 1) {
1.266 thorpej 924: IP_STATINC(IP_STAT_BADFRAGS);
1.258 matt 925: goto bad;
926: }
1.1 cgd 927: /*
928: * Look for queue of fragments
929: * of this datagram.
930: */
1.75 thorpej 931: IPQ_LOCK();
1.190 jonathan 932: hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
1.250 dyoung 933: LIST_FOREACH(fp, &ipq[hash], ipq_q) {
1.1 cgd 934: if (ip->ip_id == fp->ipq_id &&
1.35 mycroft 935: in_hosteq(ip->ip_src, fp->ipq_src) &&
936: in_hosteq(ip->ip_dst, fp->ipq_dst) &&
1.260 matt 937: ip->ip_p == fp->ipq_p) {
938: /*
939: * Make sure the TOS is matches previous
940: * fragments.
941: */
942: if (ip->ip_tos != fp->ipq_tos) {
1.266 thorpej 943: IP_STATINC(IP_STAT_BADFRAGS);
1.268.2.2 yamt 944: IPQ_UNLOCK();
1.260 matt 945: goto bad;
946: }
1.1 cgd 947: goto found;
1.260 matt 948: }
1.190 jonathan 949: }
1.1 cgd 950: fp = 0;
951: found:
952:
953: /*
954: * Adjust ip_len to not reflect header,
1.25 cgd 955: * set ipqe_mff if more fragments are expected,
1.1 cgd 956: * convert offset of this to bytes.
957: */
1.155 itojun 958: ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
959: mff = (ip->ip_off & htons(IP_MF)) != 0;
1.25 cgd 960: if (mff) {
1.16 cgd 961: /*
962: * Make sure that fragments have a data length
963: * that's a non-zero multiple of 8 bytes.
964: */
1.155 itojun 965: if (ntohs(ip->ip_len) == 0 ||
966: (ntohs(ip->ip_len) & 0x7) != 0) {
1.266 thorpej 967: IP_STATINC(IP_STAT_BADFRAGS);
1.75 thorpej 968: IPQ_UNLOCK();
1.16 cgd 969: goto bad;
970: }
971: }
1.155 itojun 972: ip->ip_off = htons((ntohs(ip->ip_off) & IP_OFFMASK) << 3);
1.1 cgd 973:
974: /*
975: * If datagram marked as having more fragments
976: * or if this is not the first fragment,
977: * attempt reassembly; if it succeeds, proceed.
978: */
1.155 itojun 979: if (mff || ip->ip_off != htons(0)) {
1.266 thorpej 980: IP_STATINC(IP_STAT_FRAGMENTS);
1.233 tls 981: s = splvm();
1.72 thorpej 982: ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
1.233 tls 983: splx(s);
1.25 cgd 984: if (ipqe == NULL) {
1.266 thorpej 985: IP_STATINC(IP_STAT_RCVMEMDROP);
1.75 thorpej 986: IPQ_UNLOCK();
1.25 cgd 987: goto bad;
988: }
989: ipqe->ipqe_mff = mff;
1.50 thorpej 990: ipqe->ipqe_m = m;
1.25 cgd 991: ipqe->ipqe_ip = ip;
1.190 jonathan 992: m = ip_reass(ipqe, fp, &ipq[hash]);
1.75 thorpej 993: if (m == 0) {
994: IPQ_UNLOCK();
1.89 itojun 995: return;
1.75 thorpej 996: }
1.266 thorpej 997: IP_STATINC(IP_STAT_REASSEMBLED);
1.50 thorpej 998: ip = mtod(m, struct ip *);
1.74 thorpej 999: hlen = ip->ip_hl << 2;
1.155 itojun 1000: ip->ip_len = htons(ntohs(ip->ip_len) + hlen);
1.1 cgd 1001: } else
1002: if (fp)
1003: ip_freef(fp);
1.75 thorpej 1004: IPQ_UNLOCK();
1.79 mycroft 1005: }
1.128 itojun 1006:
1.173 jonathan 1007: #if defined(IPSEC)
1.128 itojun 1008: /*
1009: * enforce IPsec policy checking if we are seeing last header.
1010: * note that we do not visit this with protocols with pcb layer
1011: * code - like udp/tcp/raw ip.
1012: */
1013: if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
1014: ipsec4_in_reject(m, NULL)) {
1.267 thorpej 1015: IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.128 itojun 1016: goto bad;
1017: }
1018: #endif
1.226 liamjfoy 1019: #ifdef FAST_IPSEC
1.173 jonathan 1020: /*
1021: * enforce IPsec policy checking if we are seeing last header.
1022: * note that we do not visit this with protocols with pcb layer
1023: * code - like udp/tcp/raw ip.
1024: */
1025: if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
1026: /*
1027: * Check if the packet has already had IPsec processing
1028: * done. If so, then just pass it along. This tag gets
1029: * set during AH, ESP, etc. input handling, before the
1030: * packet is returned to the ip input queue for delivery.
1.212 perry 1031: */
1.173 jonathan 1032: mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
1033: s = splsoftnet();
1034: if (mtag != NULL) {
1035: tdbi = (struct tdb_ident *)(mtag + 1);
1036: sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
1037: } else {
1038: sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
1.212 perry 1039: IP_FORWARDING, &error);
1.173 jonathan 1040: }
1041: if (sp != NULL) {
1042: /*
1043: * Check security policy against packet attributes.
1044: */
1045: error = ipsec_in_reject(sp, m);
1046: KEY_FREESP(&sp);
1047: } else {
1048: /* XXX error stat??? */
1049: error = EINVAL;
1050: DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
1051: }
1052: splx(s);
1053: if (error)
1054: goto bad;
1055: }
1056: #endif /* FAST_IPSEC */
1.1 cgd 1057:
1058: /*
1059: * Switch out to protocol's input routine.
1060: */
1.82 aidan 1061: #if IFA_STATS
1.122 itojun 1062: if (ia && ip)
1.155 itojun 1063: ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len);
1.82 aidan 1064: #endif
1.266 thorpej 1065: IP_STATINC(IP_STAT_DELIVERED);
1.89 itojun 1066: {
1067: int off = hlen, nh = ip->ip_p;
1068:
1069: (*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
1070: return;
1071: }
1.1 cgd 1072: bad:
1073: m_freem(m);
1.135 thorpej 1074: return;
1075:
1076: badcsum:
1.266 thorpej 1077: IP_STATINC(IP_STAT_BADSUM);
1.135 thorpej 1078: m_freem(m);
1.1 cgd 1079: }
1080:
1081: /*
1082: * Take incoming datagram fragment and try to
1083: * reassemble it into whole datagram. If a chain for
1084: * reassembly of this datagram already exists, then it
1085: * is given as fp; otherwise have to make a chain.
1086: */
1.50 thorpej 1087: struct mbuf *
1.211 perry 1088: ip_reass(struct ipqent *ipqe, struct ipq *fp, struct ipqhead *ipqhead)
1.1 cgd 1089: {
1.109 augustss 1090: struct mbuf *m = ipqe->ipqe_m;
1091: struct ipqent *nq, *p, *q;
1.25 cgd 1092: struct ip *ip;
1.1 cgd 1093: struct mbuf *t;
1.25 cgd 1094: int hlen = ipqe->ipqe_ip->ip_hl << 2;
1.233 tls 1095: int i, next, s;
1.1 cgd 1096:
1.75 thorpej 1097: IPQ_LOCK_CHECK();
1098:
1.1 cgd 1099: /*
1100: * Presence of header sizes in mbufs
1101: * would confuse code below.
1102: */
1103: m->m_data += hlen;
1104: m->m_len -= hlen;
1105:
1.194 jonathan 1106: #ifdef notyet
1107: /* make sure fragment limit is up-to-date */
1108: CHECK_NMBCLUSTER_PARAMS();
1109:
1110: /* If we have too many fragments, drop the older half. */
1111: if (ip_nfrags >= ip_maxfrags)
1112: ip_reass_drophalf(void);
1113: #endif
1114:
1.1 cgd 1115: /*
1.192 jonathan 1116: * We are about to add a fragment; increment frag count.
1117: */
1118: ip_nfrags++;
1.212 perry 1119:
1.192 jonathan 1120: /*
1.1 cgd 1121: * If first fragment to arrive, create a reassembly queue.
1122: */
1123: if (fp == 0) {
1.131 itojun 1124: /*
1125: * Enforce upper bound on number of fragmented packets
1126: * for which we attempt reassembly;
1127: * If maxfrag is 0, never accept fragments.
1128: * If maxfrag is -1, accept all fragments without limitation.
1129: */
1130: if (ip_maxfragpackets < 0)
1131: ;
1132: else if (ip_nfragpackets >= ip_maxfragpackets)
1133: goto dropfrag;
1134: ip_nfragpackets++;
1.268.2.2 yamt 1135: fp = malloc(sizeof (struct ipq), M_FTABLE, M_NOWAIT);
1.50 thorpej 1136: if (fp == NULL)
1.1 cgd 1137: goto dropfrag;
1.190 jonathan 1138: LIST_INSERT_HEAD(ipqhead, fp, ipq_q);
1.192 jonathan 1139: fp->ipq_nfrags = 1;
1.1 cgd 1140: fp->ipq_ttl = IPFRAGTTL;
1.25 cgd 1141: fp->ipq_p = ipqe->ipqe_ip->ip_p;
1142: fp->ipq_id = ipqe->ipqe_ip->ip_id;
1.260 matt 1143: fp->ipq_tos = ipqe->ipqe_ip->ip_tos;
1.148 matt 1144: TAILQ_INIT(&fp->ipq_fragq);
1.25 cgd 1145: fp->ipq_src = ipqe->ipqe_ip->ip_src;
1146: fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
1147: p = NULL;
1.1 cgd 1148: goto insert;
1.192 jonathan 1149: } else {
1150: fp->ipq_nfrags++;
1.1 cgd 1151: }
1152:
1153: /*
1154: * Find a segment which begins after this one does.
1155: */
1.148 matt 1156: for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
1157: p = q, q = TAILQ_NEXT(q, ipqe_q))
1.155 itojun 1158: if (ntohs(q->ipqe_ip->ip_off) > ntohs(ipqe->ipqe_ip->ip_off))
1.1 cgd 1159: break;
1160:
1161: /*
1162: * If there is a preceding segment, it may provide some of
1163: * our data already. If so, drop the data from the incoming
1164: * segment. If it provides all of our data, drop us.
1165: */
1.25 cgd 1166: if (p != NULL) {
1.155 itojun 1167: i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) -
1168: ntohs(ipqe->ipqe_ip->ip_off);
1.1 cgd 1169: if (i > 0) {
1.155 itojun 1170: if (i >= ntohs(ipqe->ipqe_ip->ip_len))
1.1 cgd 1171: goto dropfrag;
1.50 thorpej 1172: m_adj(ipqe->ipqe_m, i);
1.155 itojun 1173: ipqe->ipqe_ip->ip_off =
1174: htons(ntohs(ipqe->ipqe_ip->ip_off) + i);
1175: ipqe->ipqe_ip->ip_len =
1176: htons(ntohs(ipqe->ipqe_ip->ip_len) - i);
1.1 cgd 1177: }
1178: }
1179:
1180: /*
1181: * While we overlap succeeding segments trim them or,
1182: * if they are completely covered, dequeue them.
1183: */
1.155 itojun 1184: for (; q != NULL &&
1185: ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len) >
1186: ntohs(q->ipqe_ip->ip_off); q = nq) {
1187: i = (ntohs(ipqe->ipqe_ip->ip_off) +
1188: ntohs(ipqe->ipqe_ip->ip_len)) - ntohs(q->ipqe_ip->ip_off);
1189: if (i < ntohs(q->ipqe_ip->ip_len)) {
1190: q->ipqe_ip->ip_len =
1191: htons(ntohs(q->ipqe_ip->ip_len) - i);
1192: q->ipqe_ip->ip_off =
1193: htons(ntohs(q->ipqe_ip->ip_off) + i);
1.50 thorpej 1194: m_adj(q->ipqe_m, i);
1.1 cgd 1195: break;
1196: }
1.148 matt 1197: nq = TAILQ_NEXT(q, ipqe_q);
1.50 thorpej 1198: m_freem(q->ipqe_m);
1.148 matt 1199: TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
1.233 tls 1200: s = splvm();
1.72 thorpej 1201: pool_put(&ipqent_pool, q);
1.233 tls 1202: splx(s);
1.192 jonathan 1203: fp->ipq_nfrags--;
1204: ip_nfrags--;
1.1 cgd 1205: }
1206:
1207: insert:
1208: /*
1209: * Stick new segment in its place;
1210: * check for complete reassembly.
1211: */
1.25 cgd 1212: if (p == NULL) {
1.148 matt 1213: TAILQ_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
1.25 cgd 1214: } else {
1.148 matt 1215: TAILQ_INSERT_AFTER(&fp->ipq_fragq, p, ipqe, ipqe_q);
1.25 cgd 1216: }
1.1 cgd 1217: next = 0;
1.148 matt 1218: for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
1219: p = q, q = TAILQ_NEXT(q, ipqe_q)) {
1.155 itojun 1220: if (ntohs(q->ipqe_ip->ip_off) != next)
1.1 cgd 1221: return (0);
1.155 itojun 1222: next += ntohs(q->ipqe_ip->ip_len);
1.1 cgd 1223: }
1.25 cgd 1224: if (p->ipqe_mff)
1.1 cgd 1225: return (0);
1226:
1227: /*
1.41 thorpej 1228: * Reassembly is complete. Check for a bogus message size and
1229: * concatenate fragments.
1.1 cgd 1230: */
1.148 matt 1231: q = TAILQ_FIRST(&fp->ipq_fragq);
1.25 cgd 1232: ip = q->ipqe_ip;
1.41 thorpej 1233: if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
1.266 thorpej 1234: IP_STATINC(IP_STAT_TOOLONG);
1.41 thorpej 1235: ip_freef(fp);
1236: return (0);
1237: }
1.50 thorpej 1238: m = q->ipqe_m;
1.1 cgd 1239: t = m->m_next;
1240: m->m_next = 0;
1241: m_cat(m, t);
1.148 matt 1242: nq = TAILQ_NEXT(q, ipqe_q);
1.233 tls 1243: s = splvm();
1.72 thorpej 1244: pool_put(&ipqent_pool, q);
1.233 tls 1245: splx(s);
1.25 cgd 1246: for (q = nq; q != NULL; q = nq) {
1.50 thorpej 1247: t = q->ipqe_m;
1.148 matt 1248: nq = TAILQ_NEXT(q, ipqe_q);
1.233 tls 1249: s = splvm();
1.72 thorpej 1250: pool_put(&ipqent_pool, q);
1.233 tls 1251: splx(s);
1.1 cgd 1252: m_cat(m, t);
1253: }
1.192 jonathan 1254: ip_nfrags -= fp->ipq_nfrags;
1.1 cgd 1255:
1256: /*
1257: * Create header for new ip packet by
1258: * modifying header of first packet;
1259: * dequeue and discard fragment reassembly header.
1260: * Make header visible.
1261: */
1.155 itojun 1262: ip->ip_len = htons(next);
1.25 cgd 1263: ip->ip_src = fp->ipq_src;
1264: ip->ip_dst = fp->ipq_dst;
1265: LIST_REMOVE(fp, ipq_q);
1.268.2.2 yamt 1266: free(fp, M_FTABLE);
1.131 itojun 1267: ip_nfragpackets--;
1.1 cgd 1268: m->m_len += (ip->ip_hl << 2);
1269: m->m_data -= (ip->ip_hl << 2);
1270: /* some debugging cruft by sklower, below, will go away soon */
1271: if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1.109 augustss 1272: int plen = 0;
1.50 thorpej 1273: for (t = m; t; t = t->m_next)
1274: plen += t->m_len;
1275: m->m_pkthdr.len = plen;
1.213 yamt 1276: m->m_pkthdr.csum_flags = 0;
1.1 cgd 1277: }
1.50 thorpej 1278: return (m);
1.1 cgd 1279:
1280: dropfrag:
1.192 jonathan 1281: if (fp != 0)
1282: fp->ipq_nfrags--;
1283: ip_nfrags--;
1.266 thorpej 1284: IP_STATINC(IP_STAT_FRAGDROPPED);
1.1 cgd 1285: m_freem(m);
1.233 tls 1286: s = splvm();
1.72 thorpej 1287: pool_put(&ipqent_pool, ipqe);
1.233 tls 1288: splx(s);
1.1 cgd 1289: return (0);
1290: }
1291:
1292: /*
1293: * Free a fragment reassembly header and all
1294: * associated datagrams.
1295: */
1.8 mycroft 1296: void
1.211 perry 1297: ip_freef(struct ipq *fp)
1.1 cgd 1298: {
1.109 augustss 1299: struct ipqent *q, *p;
1.192 jonathan 1300: u_int nfrags = 0;
1.233 tls 1301: int s;
1.1 cgd 1302:
1.75 thorpej 1303: IPQ_LOCK_CHECK();
1304:
1.148 matt 1305: for (q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; q = p) {
1306: p = TAILQ_NEXT(q, ipqe_q);
1.50 thorpej 1307: m_freem(q->ipqe_m);
1.192 jonathan 1308: nfrags++;
1.148 matt 1309: TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
1.233 tls 1310: s = splvm();
1.72 thorpej 1311: pool_put(&ipqent_pool, q);
1.233 tls 1312: splx(s);
1.1 cgd 1313: }
1.192 jonathan 1314:
1315: if (nfrags != fp->ipq_nfrags)
1316: printf("ip_freef: nfrags %d != %d\n", fp->ipq_nfrags, nfrags);
1317: ip_nfrags -= nfrags;
1.25 cgd 1318: LIST_REMOVE(fp, ipq_q);
1.268.2.2 yamt 1319: free(fp, M_FTABLE);
1.131 itojun 1320: ip_nfragpackets--;
1.1 cgd 1321: }
1322:
1323: /*
1.194 jonathan 1324: * IP reassembly TTL machinery for multiplicative drop.
1325: */
1326: static u_int fragttl_histo[(IPFRAGTTL+1)];
1327:
1328:
1329: /*
1330: * Decrement TTL of all reasembly queue entries by `ticks'.
1331: * Count number of distinct fragments (as opposed to partial, fragmented
1332: * datagrams) in the reassembly queue. While we traverse the entire
1333: * reassembly queue, compute and return the median TTL over all fragments.
1334: */
1335: static u_int
1336: ip_reass_ttl_decr(u_int ticks)
1337: {
1.198 matt 1338: u_int nfrags, median, dropfraction, keepfraction;
1.194 jonathan 1339: struct ipq *fp, *nfp;
1.198 matt 1340: int i;
1.212 perry 1341:
1.194 jonathan 1342: nfrags = 0;
1343: memset(fragttl_histo, 0, sizeof fragttl_histo);
1.212 perry 1344:
1.194 jonathan 1345: for (i = 0; i < IPREASS_NHASH; i++) {
1346: for (fp = LIST_FIRST(&ipq[i]); fp != NULL; fp = nfp) {
1347: fp->ipq_ttl = ((fp->ipq_ttl <= ticks) ?
1348: 0 : fp->ipq_ttl - ticks);
1349: nfp = LIST_NEXT(fp, ipq_q);
1350: if (fp->ipq_ttl == 0) {
1.266 thorpej 1351: IP_STATINC(IP_STAT_FRAGTIMEOUT);
1.194 jonathan 1352: ip_freef(fp);
1353: } else {
1354: nfrags += fp->ipq_nfrags;
1355: fragttl_histo[fp->ipq_ttl] += fp->ipq_nfrags;
1356: }
1357: }
1358: }
1359:
1360: KASSERT(ip_nfrags == nfrags);
1361:
1362: /* Find median (or other drop fraction) in histogram. */
1363: dropfraction = (ip_nfrags / 2);
1364: keepfraction = ip_nfrags - dropfraction;
1365: for (i = IPFRAGTTL, median = 0; i >= 0; i--) {
1366: median += fragttl_histo[i];
1367: if (median >= keepfraction)
1368: break;
1369: }
1370:
1371: /* Return TTL of median (or other fraction). */
1372: return (u_int)i;
1373: }
1374:
1375: void
1376: ip_reass_drophalf(void)
1377: {
1378:
1379: u_int median_ticks;
1380: /*
1381: * Compute median TTL of all fragments, and count frags
1382: * with that TTL or lower (roughly half of all fragments).
1383: */
1384: median_ticks = ip_reass_ttl_decr(0);
1385:
1386: /* Drop half. */
1387: median_ticks = ip_reass_ttl_decr(median_ticks);
1388:
1389: }
1390:
1391: /*
1.1 cgd 1392: * IP timer processing;
1393: * if a timer expires on a reassembly
1394: * queue, discard it.
1395: */
1.8 mycroft 1396: void
1.211 perry 1397: ip_slowtimo(void)
1.1 cgd 1398: {
1.191 jonathan 1399: static u_int dropscanidx = 0;
1400: u_int i;
1.194 jonathan 1401: u_int median_ttl;
1.268 ad 1402:
1403: mutex_enter(softnet_lock);
1404: KERNEL_LOCK(1, NULL);
1.1 cgd 1405:
1.75 thorpej 1406: IPQ_LOCK();
1.194 jonathan 1407:
1408: /* Age TTL of all fragments by 1 tick .*/
1409: median_ttl = ip_reass_ttl_decr(1);
1410:
1411: /* make sure fragment limit is up-to-date */
1412: CHECK_NMBCLUSTER_PARAMS();
1413:
1414: /* If we have too many fragments, drop the older half. */
1415: if (ip_nfrags > ip_maxfrags)
1416: ip_reass_ttl_decr(median_ttl);
1417:
1.131 itojun 1418: /*
1.194 jonathan 1419: * If we are over the maximum number of fragmented packets
1.131 itojun 1420: * (due to the limit being lowered), drain off
1.190 jonathan 1421: * enough to get down to the new limit. Start draining
1422: * from the reassembly hashqueue most recently drained.
1.131 itojun 1423: */
1424: if (ip_maxfragpackets < 0)
1425: ;
1426: else {
1.190 jonathan 1427: int wrapped = 0;
1428:
1429: i = dropscanidx;
1430: while (ip_nfragpackets > ip_maxfragpackets && wrapped == 0) {
1431: while (LIST_FIRST(&ipq[i]) != NULL)
1432: ip_freef(LIST_FIRST(&ipq[i]));
1433: if (++i >= IPREASS_NHASH) {
1434: i = 0;
1435: }
1436: /*
1437: * Dont scan forever even if fragment counters are
1438: * wrong: stop after scanning entire reassembly queue.
1439: */
1440: if (i == dropscanidx)
1441: wrapped = 1;
1442: }
1443: dropscanidx = i;
1.131 itojun 1444: }
1.75 thorpej 1445: IPQ_UNLOCK();
1.268 ad 1446:
1447: KERNEL_UNLOCK_ONE(NULL);
1448: mutex_exit(softnet_lock);
1.1 cgd 1449: }
1450:
1451: /*
1.268.2.1 yamt 1452: * Drain off all datagram fragments. Don't acquire softnet_lock as
1453: * can be called from hardware interrupt context.
1.1 cgd 1454: */
1.8 mycroft 1455: void
1.211 perry 1456: ip_drain(void)
1.1 cgd 1457: {
1458:
1.268 ad 1459: KERNEL_LOCK(1, NULL);
1460:
1.75 thorpej 1461: /*
1462: * We may be called from a device's interrupt context. If
1463: * the ipq is already busy, just bail out now.
1464: */
1.268 ad 1465: if (ipq_lock_try() != 0) {
1466: /*
1467: * Drop half the total fragments now. If more mbufs are
1468: * needed, we will be called again soon.
1469: */
1470: ip_reass_drophalf();
1471: IPQ_UNLOCK();
1472: }
1.75 thorpej 1473:
1.268 ad 1474: KERNEL_UNLOCK_ONE(NULL);
1.1 cgd 1475: }
1476:
1477: /*
1478: * Do option processing on a datagram,
1479: * possibly discarding it if bad options are encountered,
1480: * or forwarding it if source-routed.
1481: * Returns 1 if packet has been forwarded/freed,
1482: * 0 if the packet should be processed further.
1483: */
1.8 mycroft 1484: int
1.211 perry 1485: ip_dooptions(struct mbuf *m)
1.1 cgd 1486: {
1.109 augustss 1487: struct ip *ip = mtod(m, struct ip *);
1488: u_char *cp, *cp0;
1489: struct ip_timestamp *ipt;
1490: struct in_ifaddr *ia;
1.1 cgd 1491: int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1.104 thorpej 1492: struct in_addr dst;
1.1 cgd 1493: n_time ntime;
1494:
1.13 mycroft 1495: dst = ip->ip_dst;
1.1 cgd 1496: cp = (u_char *)(ip + 1);
1497: cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1498: for (; cnt > 0; cnt -= optlen, cp += optlen) {
1499: opt = cp[IPOPT_OPTVAL];
1500: if (opt == IPOPT_EOL)
1501: break;
1502: if (opt == IPOPT_NOP)
1503: optlen = 1;
1504: else {
1.113 itojun 1505: if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1506: code = &cp[IPOPT_OLEN] - (u_char *)ip;
1507: goto bad;
1508: }
1.1 cgd 1509: optlen = cp[IPOPT_OLEN];
1.114 itojun 1510: if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1.1 cgd 1511: code = &cp[IPOPT_OLEN] - (u_char *)ip;
1512: goto bad;
1513: }
1514: }
1515: switch (opt) {
1516:
1517: default:
1518: break;
1519:
1520: /*
1521: * Source routing with record.
1522: * Find interface with current destination address.
1523: * If none on this machine then drop if strictly routed,
1524: * or do nothing if loosely routed.
1525: * Record interface address and bring up next address
1526: * component. If strictly routed make sure next
1527: * address is on directly accessible net.
1528: */
1529: case IPOPT_LSRR:
1530: case IPOPT_SSRR:
1.47 cjs 1531: if (ip_allowsrcrt == 0) {
1532: type = ICMP_UNREACH;
1533: code = ICMP_UNREACH_NET_PROHIB;
1534: goto bad;
1535: }
1.114 itojun 1536: if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1537: code = &cp[IPOPT_OLEN] - (u_char *)ip;
1538: goto bad;
1539: }
1.1 cgd 1540: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1541: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1542: goto bad;
1543: }
1544: ipaddr.sin_addr = ip->ip_dst;
1.19 mycroft 1545: ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1.1 cgd 1546: if (ia == 0) {
1547: if (opt == IPOPT_SSRR) {
1548: type = ICMP_UNREACH;
1549: code = ICMP_UNREACH_SRCFAIL;
1550: goto bad;
1551: }
1552: /*
1553: * Loose routing, and not at next destination
1554: * yet; nothing to do except forward.
1555: */
1556: break;
1557: }
1558: off--; /* 0 origin */
1.112 sommerfe 1559: if ((off + sizeof(struct in_addr)) > optlen) {
1.1 cgd 1560: /*
1561: * End of source route. Should be for us.
1562: */
1563: save_rte(cp, ip->ip_src);
1564: break;
1565: }
1566: /*
1567: * locate outgoing interface
1568: */
1.268.2.2 yamt 1569: memcpy((void *)&ipaddr.sin_addr, (void *)(cp + off),
1.1 cgd 1570: sizeof(ipaddr.sin_addr));
1.96 thorpej 1571: if (opt == IPOPT_SSRR)
1.196 itojun 1572: ia = ifatoia(ifa_ifwithladdr(sintosa(&ipaddr)));
1.96 thorpej 1573: else
1.1 cgd 1574: ia = ip_rtaddr(ipaddr.sin_addr);
1575: if (ia == 0) {
1576: type = ICMP_UNREACH;
1577: code = ICMP_UNREACH_SRCFAIL;
1578: goto bad;
1579: }
1580: ip->ip_dst = ipaddr.sin_addr;
1.244 christos 1581: bcopy((void *)&ia->ia_addr.sin_addr,
1582: (void *)(cp + off), sizeof(struct in_addr));
1.1 cgd 1583: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13 mycroft 1584: /*
1585: * Let ip_intr's mcast routing check handle mcast pkts
1586: */
1.18 mycroft 1587: forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1 cgd 1588: break;
1589:
1590: case IPOPT_RR:
1.114 itojun 1591: if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1592: code = &cp[IPOPT_OLEN] - (u_char *)ip;
1593: goto bad;
1594: }
1.1 cgd 1595: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1596: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1597: goto bad;
1598: }
1599: /*
1600: * If no space remains, ignore.
1601: */
1602: off--; /* 0 origin */
1.112 sommerfe 1603: if ((off + sizeof(struct in_addr)) > optlen)
1.1 cgd 1604: break;
1.268.2.2 yamt 1605: memcpy((void *)&ipaddr.sin_addr, (void *)(&ip->ip_dst),
1.1 cgd 1606: sizeof(ipaddr.sin_addr));
1607: /*
1608: * locate outgoing interface; if we're the destination,
1609: * use the incoming interface (should be same).
1610: */
1.96 thorpej 1611: if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
1612: == NULL &&
1613: (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1.1 cgd 1614: type = ICMP_UNREACH;
1615: code = ICMP_UNREACH_HOST;
1616: goto bad;
1617: }
1.244 christos 1618: bcopy((void *)&ia->ia_addr.sin_addr,
1619: (void *)(cp + off), sizeof(struct in_addr));
1.1 cgd 1620: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1621: break;
1622:
1623: case IPOPT_TS:
1624: code = cp - (u_char *)ip;
1625: ipt = (struct ip_timestamp *)cp;
1.114 itojun 1626: if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
1627: code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1.1 cgd 1628: goto bad;
1.114 itojun 1629: }
1630: if (ipt->ipt_ptr < 5) {
1631: code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
1632: goto bad;
1633: }
1.15 cgd 1634: if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.114 itojun 1635: if (++ipt->ipt_oflw == 0) {
1636: code = (u_char *)&ipt->ipt_ptr -
1637: (u_char *)ip;
1.1 cgd 1638: goto bad;
1.114 itojun 1639: }
1.1 cgd 1640: break;
1641: }
1.104 thorpej 1642: cp0 = (cp + ipt->ipt_ptr - 1);
1.1 cgd 1643: switch (ipt->ipt_flg) {
1644:
1645: case IPOPT_TS_TSONLY:
1646: break;
1647:
1648: case IPOPT_TS_TSANDADDR:
1.66 thorpej 1649: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.114 itojun 1650: sizeof(struct in_addr) > ipt->ipt_len) {
1651: code = (u_char *)&ipt->ipt_ptr -
1652: (u_char *)ip;
1.1 cgd 1653: goto bad;
1.114 itojun 1654: }
1.13 mycroft 1655: ipaddr.sin_addr = dst;
1.96 thorpej 1656: ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
1657: m->m_pkthdr.rcvif));
1.13 mycroft 1658: if (ia == 0)
1659: continue;
1.104 thorpej 1660: bcopy(&ia->ia_addr.sin_addr,
1661: cp0, sizeof(struct in_addr));
1.1 cgd 1662: ipt->ipt_ptr += sizeof(struct in_addr);
1663: break;
1664:
1665: case IPOPT_TS_PRESPEC:
1.66 thorpej 1666: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.114 itojun 1667: sizeof(struct in_addr) > ipt->ipt_len) {
1668: code = (u_char *)&ipt->ipt_ptr -
1669: (u_char *)ip;
1.1 cgd 1670: goto bad;
1.114 itojun 1671: }
1.268.2.2 yamt 1672: memcpy(&ipaddr.sin_addr, cp0,
1.1 cgd 1673: sizeof(struct in_addr));
1.96 thorpej 1674: if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
1675: == NULL)
1.1 cgd 1676: continue;
1677: ipt->ipt_ptr += sizeof(struct in_addr);
1678: break;
1679:
1680: default:
1.114 itojun 1681: /* XXX can't take &ipt->ipt_flg */
1682: code = (u_char *)&ipt->ipt_ptr -
1683: (u_char *)ip + 1;
1.1 cgd 1684: goto bad;
1685: }
1686: ntime = iptime();
1.107 thorpej 1687: cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
1.244 christos 1688: memmove((char *)cp + ipt->ipt_ptr - 1, cp0,
1.1 cgd 1689: sizeof(n_time));
1690: ipt->ipt_ptr += sizeof(n_time);
1691: }
1692: }
1693: if (forward) {
1.26 thorpej 1694: if (ip_forwsrcrt == 0) {
1695: type = ICMP_UNREACH;
1696: code = ICMP_UNREACH_SRCFAIL;
1697: goto bad;
1698: }
1.1 cgd 1699: ip_forward(m, 1);
1700: return (1);
1.13 mycroft 1701: }
1702: return (0);
1.1 cgd 1703: bad:
1.13 mycroft 1704: icmp_error(m, type, code, 0, 0);
1.266 thorpej 1705: IP_STATINC(IP_STAT_BADOPTIONS);
1.1 cgd 1706: return (1);
1707: }
1708:
1709: /*
1710: * Given address of next destination (final or next hop),
1711: * return internet address info of interface to be used to get there.
1712: */
1713: struct in_ifaddr *
1.211 perry 1714: ip_rtaddr(struct in_addr dst)
1.1 cgd 1715: {
1.249 dyoung 1716: struct rtentry *rt;
1717: union {
1718: struct sockaddr dst;
1719: struct sockaddr_in dst4;
1720: } u;
1721:
1722: sockaddr_in_init(&u.dst4, &dst, 0);
1723:
1724: if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL)
1725: return NULL;
1726:
1727: return ifatoia(rt->rt_ifa);
1.1 cgd 1728: }
1729:
1730: /*
1731: * Save incoming source route for use in replies,
1732: * to be picked up later by ip_srcroute if the receiver is interested.
1733: */
1.13 mycroft 1734: void
1.211 perry 1735: save_rte(u_char *option, struct in_addr dst)
1.1 cgd 1736: {
1737: unsigned olen;
1738:
1739: olen = option[IPOPT_OLEN];
1740: #ifdef DIAGNOSTIC
1741: if (ipprintfs)
1.39 christos 1742: printf("save_rte: olen %d\n", olen);
1.89 itojun 1743: #endif /* 0 */
1.1 cgd 1744: if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1745: return;
1.268.2.2 yamt 1746: memcpy((void *)ip_srcrt.srcopt, (void *)option, olen);
1.1 cgd 1747: ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1748: ip_srcrt.dst = dst;
1749: }
1750:
1751: /*
1752: * Retrieve incoming source route for use in replies,
1753: * in the same form used by setsockopt.
1754: * The first hop is placed before the options, will be removed later.
1755: */
1756: struct mbuf *
1.211 perry 1757: ip_srcroute(void)
1.1 cgd 1758: {
1.109 augustss 1759: struct in_addr *p, *q;
1760: struct mbuf *m;
1.1 cgd 1761:
1762: if (ip_nhops == 0)
1.237 dyoung 1763: return NULL;
1.1 cgd 1764: m = m_get(M_DONTWAIT, MT_SOOPTS);
1765: if (m == 0)
1.237 dyoung 1766: return NULL;
1.1 cgd 1767:
1.164 matt 1768: MCLAIM(m, &inetdomain.dom_mowner);
1.13 mycroft 1769: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1 cgd 1770:
1771: /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1772: m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1773: OPTSIZ;
1774: #ifdef DIAGNOSTIC
1775: if (ipprintfs)
1.39 christos 1776: printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1.1 cgd 1777: #endif
1778:
1779: /*
1780: * First save first hop for return route
1781: */
1782: p = &ip_srcrt.route[ip_nhops - 1];
1783: *(mtod(m, struct in_addr *)) = *p--;
1784: #ifdef DIAGNOSTIC
1785: if (ipprintfs)
1.39 christos 1786: printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1.1 cgd 1787: #endif
1788:
1789: /*
1790: * Copy option fields and padding (nop) to mbuf.
1791: */
1792: ip_srcrt.nop = IPOPT_NOP;
1793: ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1.244 christos 1794: memmove(mtod(m, char *) + sizeof(struct in_addr), &ip_srcrt.nop,
1795: OPTSIZ);
1796: q = (struct in_addr *)(mtod(m, char *) +
1.1 cgd 1797: sizeof(struct in_addr) + OPTSIZ);
1798: #undef OPTSIZ
1799: /*
1800: * Record return path as an IP source route,
1801: * reversing the path (pointers are now aligned).
1802: */
1803: while (p >= ip_srcrt.route) {
1804: #ifdef DIAGNOSTIC
1805: if (ipprintfs)
1.39 christos 1806: printf(" %x", ntohl(q->s_addr));
1.1 cgd 1807: #endif
1808: *q++ = *p--;
1809: }
1810: /*
1811: * Last hop goes to final destination.
1812: */
1813: *q = ip_srcrt.dst;
1814: #ifdef DIAGNOSTIC
1815: if (ipprintfs)
1.39 christos 1816: printf(" %x\n", ntohl(q->s_addr));
1.1 cgd 1817: #endif
1818: return (m);
1819: }
1820:
1.139 matt 1821: const int inetctlerrmap[PRC_NCMDS] = {
1.256 yamt 1822: [PRC_MSGSIZE] = EMSGSIZE,
1823: [PRC_HOSTDEAD] = EHOSTDOWN,
1824: [PRC_HOSTUNREACH] = EHOSTUNREACH,
1825: [PRC_UNREACH_NET] = EHOSTUNREACH,
1826: [PRC_UNREACH_HOST] = EHOSTUNREACH,
1827: [PRC_UNREACH_PROTOCOL] = ECONNREFUSED,
1828: [PRC_UNREACH_PORT] = ECONNREFUSED,
1829: [PRC_UNREACH_SRCFAIL] = EHOSTUNREACH,
1830: [PRC_PARAMPROB] = ENOPROTOOPT,
1.1 cgd 1831: };
1832:
1833: /*
1834: * Forward a packet. If some error occurs return the sender
1835: * an icmp packet. Note we can't always generate a meaningful
1836: * icmp message because icmp doesn't have a large enough repertoire
1837: * of codes and types.
1838: *
1839: * If not forwarding, just drop the packet. This could be confusing
1840: * if ipforwarding was zero but some routing protocol was advancing
1841: * us as a gateway to somewhere. However, we must let the routing
1842: * protocol deal with that.
1843: *
1844: * The srcrt parameter indicates whether the packet is being forwarded
1845: * via a source route.
1846: */
1.13 mycroft 1847: void
1.211 perry 1848: ip_forward(struct mbuf *m, int srcrt)
1.1 cgd 1849: {
1.109 augustss 1850: struct ip *ip = mtod(m, struct ip *);
1851: struct rtentry *rt;
1.220 christos 1852: int error, type = 0, code = 0, destmtu = 0;
1.1 cgd 1853: struct mbuf *mcopy;
1.13 mycroft 1854: n_long dest;
1.249 dyoung 1855: union {
1856: struct sockaddr dst;
1857: struct sockaddr_in dst4;
1858: } u;
1.164 matt 1859:
1860: /*
1861: * We are now in the output path.
1862: */
1863: MCLAIM(m, &ip_tx_mowner);
1.135 thorpej 1864:
1865: /*
1866: * Clear any in-bound checksum flags for this packet.
1867: */
1868: m->m_pkthdr.csum_flags = 0;
1.1 cgd 1869:
1.13 mycroft 1870: dest = 0;
1.1 cgd 1871: #ifdef DIAGNOSTIC
1.224 joerg 1872: if (ipprintfs) {
1873: printf("forward: src %s ", inet_ntoa(ip->ip_src));
1874: printf("dst %s ttl %x\n", inet_ntoa(ip->ip_dst), ip->ip_ttl);
1875: }
1.1 cgd 1876: #endif
1.93 sommerfe 1877: if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1.266 thorpej 1878: IP_STATINC(IP_STAT_CANTFORWARD);
1.1 cgd 1879: m_freem(m);
1880: return;
1881: }
1882: if (ip->ip_ttl <= IPTTLDEC) {
1.13 mycroft 1883: icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1 cgd 1884: return;
1885: }
1886:
1.249 dyoung 1887: sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
1888: if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL) {
1889: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, dest, 0);
1890: return;
1.1 cgd 1891: }
1892:
1893: /*
1.34 mycroft 1894: * Save at most 68 bytes of the packet in case
1.1 cgd 1895: * we need to generate an ICMP message to the src.
1.119 itojun 1896: * Pullup to avoid sharing mbuf cluster between m and mcopy.
1.1 cgd 1897: */
1.155 itojun 1898: mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT);
1.119 itojun 1899: if (mcopy)
1900: mcopy = m_pullup(mcopy, ip->ip_hl << 2);
1.1 cgd 1901:
1.221 christos 1902: ip->ip_ttl -= IPTTLDEC;
1903:
1.1 cgd 1904: /*
1905: * If forwarding packet using same interface that it came in on,
1906: * perhaps should send a redirect to sender to shortcut a hop.
1907: * Only send redirect if source is sending directly to us,
1908: * and if packet was not source routed (or has any options).
1909: * Also, don't send redirect if forwarding using a default route
1910: * or a route modified by a redirect.
1911: */
1912: if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1913: (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1.250 dyoung 1914: !in_nullhost(satocsin(rt_getkey(rt))->sin_addr) &&
1.1 cgd 1915: ipsendredirects && !srcrt) {
1.19 mycroft 1916: if (rt->rt_ifa &&
1917: (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1918: ifatoia(rt->rt_ifa)->ia_subnet) {
1.77 thorpej 1919: if (rt->rt_flags & RTF_GATEWAY)
1920: dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1921: else
1922: dest = ip->ip_dst.s_addr;
1923: /*
1924: * Router requirements says to only send host
1925: * redirects.
1926: */
1927: type = ICMP_REDIRECT;
1928: code = ICMP_REDIRECT_HOST;
1.1 cgd 1929: #ifdef DIAGNOSTIC
1.77 thorpej 1930: if (ipprintfs)
1931: printf("redirect (%d) to %x\n", code,
1932: (u_int32_t)dest);
1.1 cgd 1933: #endif
1934: }
1935: }
1936:
1.238 dyoung 1937: error = ip_output(m, NULL, &ipforward_rt,
1.173 jonathan 1938: (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
1.174 itojun 1939: (struct ip_moptions *)NULL, (struct socket *)NULL);
1.173 jonathan 1940:
1.1 cgd 1941: if (error)
1.266 thorpej 1942: IP_STATINC(IP_STAT_CANTFORWARD);
1.1 cgd 1943: else {
1.266 thorpej 1944: uint64_t *ips = IP_STAT_GETREF();
1945: ips[IP_STAT_FORWARD]++;
1946: if (type) {
1947: ips[IP_STAT_REDIRECTSENT]++;
1948: IP_STAT_PUTREF();
1949: } else {
1950: IP_STAT_PUTREF();
1.63 matt 1951: if (mcopy) {
1952: #ifdef GATEWAY
1.64 thorpej 1953: if (mcopy->m_flags & M_CANFASTFWD)
1954: ipflow_create(&ipforward_rt, mcopy);
1.63 matt 1955: #endif
1.1 cgd 1956: m_freem(mcopy);
1.63 matt 1957: }
1.1 cgd 1958: return;
1959: }
1960: }
1961: if (mcopy == NULL)
1962: return;
1.13 mycroft 1963:
1.1 cgd 1964: switch (error) {
1965:
1966: case 0: /* forwarded, but need redirect */
1967: /* type, code set above */
1968: break;
1969:
1970: case ENETUNREACH: /* shouldn't happen, checked above */
1971: case EHOSTUNREACH:
1972: case ENETDOWN:
1973: case EHOSTDOWN:
1974: default:
1975: type = ICMP_UNREACH;
1976: code = ICMP_UNREACH_HOST;
1977: break;
1978:
1979: case EMSGSIZE:
1980: type = ICMP_UNREACH;
1981: code = ICMP_UNREACH_NEEDFRAG;
1.263 cube 1982:
1.268.2.2 yamt 1983: if ((rt = rtcache_validate(&ipforward_rt)) != NULL)
1984: destmtu = rt->rt_ifp->if_mtu;
1.263 cube 1985:
1986: #if defined(IPSEC) || defined(FAST_IPSEC)
1.268.2.2 yamt 1987: {
1.263 cube 1988: /*
1989: * If the packet is routed over IPsec tunnel, tell the
1990: * originator the tunnel MTU.
1991: * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1992: * XXX quickhack!!!
1993: */
1994:
1.89 itojun 1995: struct secpolicy *sp;
1996: int ipsecerror;
1.95 itojun 1997: size_t ipsechdr;
1.89 itojun 1998: struct route *ro;
1999:
2000: sp = ipsec4_getpolicybyaddr(mcopy,
1.170 itojun 2001: IPSEC_DIR_OUTBOUND, IP_FORWARDING,
2002: &ipsecerror);
1.89 itojun 2003:
1.263 cube 2004: if (sp != NULL) {
1.89 itojun 2005: /* count IPsec header size */
1.95 itojun 2006: ipsechdr = ipsec4_hdrsiz(mcopy,
1.170 itojun 2007: IPSEC_DIR_OUTBOUND, NULL);
1.89 itojun 2008:
2009: /*
2010: * find the correct route for outer IPv4
2011: * header, compute tunnel MTU.
2012: */
1.220 christos 2013:
1.89 itojun 2014: if (sp->req != NULL
1.95 itojun 2015: && sp->req->sav != NULL
2016: && sp->req->sav->sah != NULL) {
2017: ro = &sp->req->sav->sah->sa_route;
1.268.2.2 yamt 2018: rt = rtcache_validate(ro);
1.257 dyoung 2019: if (rt && rt->rt_ifp) {
1.220 christos 2020: destmtu =
1.257 dyoung 2021: rt->rt_rmx.rmx_mtu ?
2022: rt->rt_rmx.rmx_mtu :
2023: rt->rt_ifp->if_mtu;
1.220 christos 2024: destmtu -= ipsechdr;
1.89 itojun 2025: }
2026: }
2027:
1.173 jonathan 2028: #ifdef IPSEC
1.89 itojun 2029: key_freesp(sp);
1.173 jonathan 2030: #else
2031: KEY_FREESP(&sp);
2032: #endif
1.89 itojun 2033: }
2034: }
1.268.2.2 yamt 2035: #endif /*defined(IPSEC) || defined(FAST_IPSEC)*/
1.266 thorpej 2036: IP_STATINC(IP_STAT_CANTFRAG);
1.1 cgd 2037: break;
2038:
2039: case ENOBUFS:
1.143 itojun 2040: #if 1
2041: /*
2042: * a router should not generate ICMP_SOURCEQUENCH as
2043: * required in RFC1812 Requirements for IP Version 4 Routers.
2044: * source quench could be a big problem under DoS attacks,
1.149 wiz 2045: * or if the underlying interface is rate-limited.
1.143 itojun 2046: */
2047: if (mcopy)
2048: m_freem(mcopy);
2049: return;
2050: #else
1.1 cgd 2051: type = ICMP_SOURCEQUENCH;
2052: code = 0;
2053: break;
1.143 itojun 2054: #endif
1.1 cgd 2055: }
1.220 christos 2056: icmp_error(mcopy, type, code, dest, destmtu);
1.44 thorpej 2057: }
2058:
2059: void
1.211 perry 2060: ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
2061: struct mbuf *m)
1.44 thorpej 2062: {
2063:
1.268.2.4 yamt 2064: if (inp->inp_socket->so_options & SO_TIMESTAMP
1.268.2.2 yamt 2065: #ifdef SO_OTIMESTAMP
1.268.2.4 yamt 2066: || inp->inp_socket->so_options & SO_OTIMESTAMP
1.268.2.2 yamt 2067: #endif
2068: ) {
1.44 thorpej 2069: struct timeval tv;
2070:
2071: microtime(&tv);
1.268.2.2 yamt 2072: #ifdef SO_OTIMESTAMP
2073: if (inp->inp_socket->so_options & SO_OTIMESTAMP) {
2074: struct timeval50 tv50;
2075: timeval_to_timeval50(&tv, &tv50);
2076: *mp = sbcreatecontrol((void *) &tv50, sizeof(tv50),
2077: SCM_OTIMESTAMP, SOL_SOCKET);
2078: } else
2079: #endif
1.244 christos 2080: *mp = sbcreatecontrol((void *) &tv, sizeof(tv),
1.44 thorpej 2081: SCM_TIMESTAMP, SOL_SOCKET);
2082: if (*mp)
2083: mp = &(*mp)->m_next;
2084: }
2085: if (inp->inp_flags & INP_RECVDSTADDR) {
1.244 christos 2086: *mp = sbcreatecontrol((void *) &ip->ip_dst,
1.44 thorpej 2087: sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
2088: if (*mp)
2089: mp = &(*mp)->m_next;
2090: }
2091: #ifdef notyet
2092: /*
2093: * XXX
2094: * Moving these out of udp_input() made them even more broken
2095: * than they already were.
2096: * - fenner@parc.xerox.com
2097: */
2098: /* options were tossed already */
2099: if (inp->inp_flags & INP_RECVOPTS) {
1.244 christos 2100: *mp = sbcreatecontrol((void *) opts_deleted_above,
1.44 thorpej 2101: sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
2102: if (*mp)
2103: mp = &(*mp)->m_next;
2104: }
2105: /* ip_srcroute doesn't do what we want here, need to fix */
2106: if (inp->inp_flags & INP_RECVRETOPTS) {
1.244 christos 2107: *mp = sbcreatecontrol((void *) ip_srcroute(),
1.44 thorpej 2108: sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
2109: if (*mp)
2110: mp = &(*mp)->m_next;
2111: }
2112: #endif
2113: if (inp->inp_flags & INP_RECVIF) {
2114: struct sockaddr_dl sdl;
2115:
1.252 dyoung 2116: sockaddr_dl_init(&sdl, sizeof(sdl),
2117: (m->m_pkthdr.rcvif != NULL)
2118: ? m->m_pkthdr.rcvif->if_index
2119: : 0,
2120: 0, NULL, 0, NULL, 0);
1.251 dyoung 2121: *mp = sbcreatecontrol(&sdl, sdl.sdl_len, IP_RECVIF, IPPROTO_IP);
1.44 thorpej 2122: if (*mp)
2123: mp = &(*mp)->m_next;
2124: }
1.268.2.3 yamt 2125: if (inp->inp_flags & INP_RECVTTL) {
2126: *mp = sbcreatecontrol((void *) &ip->ip_ttl,
2127: sizeof(uint8_t), IP_RECVTTL, IPPROTO_IP);
2128: if (*mp)
2129: mp = &(*mp)->m_next;
2130: }
1.13 mycroft 2131: }
2132:
1.189 atatat 2133: /*
1.228 elad 2134: * sysctl helper routine for net.inet.ip.forwsrcrt.
2135: */
2136: static int
2137: sysctl_net_inet_ip_forwsrcrt(SYSCTLFN_ARGS)
2138: {
2139: int error, tmp;
2140: struct sysctlnode node;
2141:
2142: node = *rnode;
2143: tmp = ip_forwsrcrt;
2144: node.sysctl_data = &tmp;
2145: error = sysctl_lookup(SYSCTLFN_CALL(&node));
2146: if (error || newp == NULL)
2147: return (error);
2148:
1.268.2.2 yamt 2149: error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FORWSRCRT,
2150: 0, NULL, NULL, NULL);
2151: if (error)
2152: return (error);
1.228 elad 2153:
2154: ip_forwsrcrt = tmp;
2155:
2156: return (0);
2157: }
2158:
2159: /*
1.189 atatat 2160: * sysctl helper routine for net.inet.ip.mtudisctimeout. checks the
2161: * range of the new value and tweaks timers if it changes.
2162: */
2163: static int
2164: sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS)
1.13 mycroft 2165: {
1.189 atatat 2166: int error, tmp;
2167: struct sysctlnode node;
2168:
2169: node = *rnode;
2170: tmp = ip_mtudisc_timeout;
2171: node.sysctl_data = &tmp;
2172: error = sysctl_lookup(SYSCTLFN_CALL(&node));
2173: if (error || newp == NULL)
2174: return (error);
2175: if (tmp < 0)
2176: return (EINVAL);
1.52 thorpej 2177:
1.268.2.2 yamt 2178: mutex_enter(softnet_lock);
2179:
1.189 atatat 2180: ip_mtudisc_timeout = tmp;
2181: rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout);
2182:
1.268.2.2 yamt 2183: mutex_exit(softnet_lock);
2184:
1.189 atatat 2185: return (0);
2186: }
1.54 lukem 2187:
1.65 matt 2188: #ifdef GATEWAY
1.189 atatat 2189: /*
1.247 liamjfoy 2190: * sysctl helper routine for net.inet.ip.maxflows.
1.189 atatat 2191: */
2192: static int
2193: sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS)
2194: {
1.268.2.2 yamt 2195: int error;
2196:
2197: error = sysctl_lookup(SYSCTLFN_CALL(rnode));
2198: if (error || newp == NULL)
2199: return (error);
1.67 thorpej 2200:
1.268.2.2 yamt 2201: mutex_enter(softnet_lock);
2202: KERNEL_LOCK(1, NULL);
1.212 perry 2203:
1.265 thorpej 2204: ipflow_prune();
1.268.2.2 yamt 2205:
2206: KERNEL_UNLOCK_ONE(NULL);
2207: mutex_exit(softnet_lock);
1.144 martin 2208:
1.189 atatat 2209: return (0);
2210: }
1.248 liamjfoy 2211:
2212: static int
2213: sysctl_net_inet_ip_hashsize(SYSCTLFN_ARGS)
1.268.2.4 yamt 2214: {
1.248 liamjfoy 2215: int error, tmp;
2216: struct sysctlnode node;
2217:
2218: node = *rnode;
2219: tmp = ip_hashsize;
2220: node.sysctl_data = &tmp;
2221: error = sysctl_lookup(SYSCTLFN_CALL(&node));
2222: if (error || newp == NULL)
2223: return (error);
2224:
2225: if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
2226: /*
2227: * Can only fail due to malloc()
2228: */
1.268.2.2 yamt 2229: mutex_enter(softnet_lock);
2230: KERNEL_LOCK(1, NULL);
2231:
2232: error = ipflow_invalidate_all(tmp);
2233:
2234: KERNEL_UNLOCK_ONE(NULL);
2235: mutex_exit(softnet_lock);
2236:
1.248 liamjfoy 2237: } else {
2238: /*
2239: * EINVAL if not a power of 2
2240: */
1.268.2.2 yamt 2241: error = EINVAL;
1.268.2.4 yamt 2242: }
1.248 liamjfoy 2243:
1.268.2.2 yamt 2244: return error;
1.248 liamjfoy 2245: }
1.189 atatat 2246: #endif /* GATEWAY */
1.117 tron 2247:
1.266 thorpej 2248: static int
2249: sysctl_net_inet_ip_stats(SYSCTLFN_ARGS)
2250: {
2251:
1.268.2.1 yamt 2252: return (NETSTAT_SYSCTL(ipstat_percpu, IP_NSTATS));
1.266 thorpej 2253: }
1.131 itojun 2254:
1.268.2.5! yamt 2255: static void
! 2256: sysctl_net_inet_ip_setup(struct sysctllog **clog)
1.189 atatat 2257: {
2258: extern int subnetsarelocal, hostzeroisbroadcast;
1.180 jonathan 2259:
1.197 atatat 2260: sysctl_createv(clog, 0, NULL, NULL,
2261: CTLFLAG_PERMANENT,
1.189 atatat 2262: CTLTYPE_NODE, "net", NULL,
2263: NULL, 0, NULL, 0,
2264: CTL_NET, CTL_EOL);
1.197 atatat 2265: sysctl_createv(clog, 0, NULL, NULL,
2266: CTLFLAG_PERMANENT,
1.203 atatat 2267: CTLTYPE_NODE, "inet",
2268: SYSCTL_DESCR("PF_INET related settings"),
1.189 atatat 2269: NULL, 0, NULL, 0,
2270: CTL_NET, PF_INET, CTL_EOL);
1.197 atatat 2271: sysctl_createv(clog, 0, NULL, NULL,
2272: CTLFLAG_PERMANENT,
1.203 atatat 2273: CTLTYPE_NODE, "ip",
2274: SYSCTL_DESCR("IPv4 related settings"),
1.189 atatat 2275: NULL, 0, NULL, 0,
2276: CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
1.212 perry 2277:
1.197 atatat 2278: sysctl_createv(clog, 0, NULL, NULL,
2279: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2280: CTLTYPE_INT, "forwarding",
2281: SYSCTL_DESCR("Enable forwarding of INET datagrams"),
1.189 atatat 2282: NULL, 0, &ipforwarding, 0,
2283: CTL_NET, PF_INET, IPPROTO_IP,
2284: IPCTL_FORWARDING, CTL_EOL);
1.197 atatat 2285: sysctl_createv(clog, 0, NULL, NULL,
2286: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2287: CTLTYPE_INT, "redirect",
2288: SYSCTL_DESCR("Enable sending of ICMP redirect messages"),
1.189 atatat 2289: NULL, 0, &ipsendredirects, 0,
2290: CTL_NET, PF_INET, IPPROTO_IP,
2291: IPCTL_SENDREDIRECTS, CTL_EOL);
1.197 atatat 2292: sysctl_createv(clog, 0, NULL, NULL,
2293: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2294: CTLTYPE_INT, "ttl",
2295: SYSCTL_DESCR("Default TTL for an INET datagram"),
1.189 atatat 2296: NULL, 0, &ip_defttl, 0,
2297: CTL_NET, PF_INET, IPPROTO_IP,
2298: IPCTL_DEFTTL, CTL_EOL);
2299: #ifdef IPCTL_DEFMTU
1.197 atatat 2300: sysctl_createv(clog, 0, NULL, NULL,
2301: CTLFLAG_PERMANENT /* |CTLFLAG_READWRITE? */,
1.203 atatat 2302: CTLTYPE_INT, "mtu",
2303: SYSCTL_DESCR("Default MTA for an INET route"),
1.189 atatat 2304: NULL, 0, &ip_mtu, 0,
2305: CTL_NET, PF_INET, IPPROTO_IP,
2306: IPCTL_DEFMTU, CTL_EOL);
2307: #endif /* IPCTL_DEFMTU */
1.197 atatat 2308: sysctl_createv(clog, 0, NULL, NULL,
1.228 elad 2309: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2310: CTLTYPE_INT, "forwsrcrt",
2311: SYSCTL_DESCR("Enable forwarding of source-routed "
2312: "datagrams"),
1.228 elad 2313: sysctl_net_inet_ip_forwsrcrt, 0, &ip_forwsrcrt, 0,
1.189 atatat 2314: CTL_NET, PF_INET, IPPROTO_IP,
2315: IPCTL_FORWSRCRT, CTL_EOL);
1.197 atatat 2316: sysctl_createv(clog, 0, NULL, NULL,
2317: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2318: CTLTYPE_INT, "directed-broadcast",
2319: SYSCTL_DESCR("Enable forwarding of broadcast datagrams"),
1.189 atatat 2320: NULL, 0, &ip_directedbcast, 0,
2321: CTL_NET, PF_INET, IPPROTO_IP,
2322: IPCTL_DIRECTEDBCAST, CTL_EOL);
1.197 atatat 2323: sysctl_createv(clog, 0, NULL, NULL,
2324: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2325: CTLTYPE_INT, "allowsrcrt",
2326: SYSCTL_DESCR("Accept source-routed datagrams"),
1.189 atatat 2327: NULL, 0, &ip_allowsrcrt, 0,
2328: CTL_NET, PF_INET, IPPROTO_IP,
2329: IPCTL_ALLOWSRCRT, CTL_EOL);
1.197 atatat 2330: sysctl_createv(clog, 0, NULL, NULL,
2331: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2332: CTLTYPE_INT, "subnetsarelocal",
2333: SYSCTL_DESCR("Whether logical subnets are considered "
2334: "local"),
1.189 atatat 2335: NULL, 0, &subnetsarelocal, 0,
2336: CTL_NET, PF_INET, IPPROTO_IP,
2337: IPCTL_SUBNETSARELOCAL, CTL_EOL);
1.197 atatat 2338: sysctl_createv(clog, 0, NULL, NULL,
2339: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2340: CTLTYPE_INT, "mtudisc",
2341: SYSCTL_DESCR("Use RFC1191 Path MTU Discovery"),
1.189 atatat 2342: NULL, 0, &ip_mtudisc, 0,
2343: CTL_NET, PF_INET, IPPROTO_IP,
2344: IPCTL_MTUDISC, CTL_EOL);
1.197 atatat 2345: sysctl_createv(clog, 0, NULL, NULL,
2346: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2347: CTLTYPE_INT, "anonportmin",
2348: SYSCTL_DESCR("Lowest ephemeral port number to assign"),
1.189 atatat 2349: sysctl_net_inet_ip_ports, 0, &anonportmin, 0,
2350: CTL_NET, PF_INET, IPPROTO_IP,
2351: IPCTL_ANONPORTMIN, CTL_EOL);
1.197 atatat 2352: sysctl_createv(clog, 0, NULL, NULL,
2353: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2354: CTLTYPE_INT, "anonportmax",
2355: SYSCTL_DESCR("Highest ephemeral port number to assign"),
1.189 atatat 2356: sysctl_net_inet_ip_ports, 0, &anonportmax, 0,
2357: CTL_NET, PF_INET, IPPROTO_IP,
2358: IPCTL_ANONPORTMAX, CTL_EOL);
1.197 atatat 2359: sysctl_createv(clog, 0, NULL, NULL,
2360: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2361: CTLTYPE_INT, "mtudisctimeout",
2362: SYSCTL_DESCR("Lifetime of a Path MTU Discovered route"),
1.189 atatat 2363: sysctl_net_inet_ip_pmtudto, 0, &ip_mtudisc_timeout, 0,
2364: CTL_NET, PF_INET, IPPROTO_IP,
2365: IPCTL_MTUDISCTIMEOUT, CTL_EOL);
2366: #ifdef GATEWAY
1.197 atatat 2367: sysctl_createv(clog, 0, NULL, NULL,
2368: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2369: CTLTYPE_INT, "maxflows",
2370: SYSCTL_DESCR("Number of flows for fast forwarding"),
1.189 atatat 2371: sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0,
2372: CTL_NET, PF_INET, IPPROTO_IP,
2373: IPCTL_MAXFLOWS, CTL_EOL);
1.248 liamjfoy 2374: sysctl_createv(clog, 0, NULL, NULL,
2375: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2376: CTLTYPE_INT, "hashsize",
2377: SYSCTL_DESCR("Size of hash table for fast forwarding (IPv4)"),
2378: sysctl_net_inet_ip_hashsize, 0, &ip_hashsize, 0,
2379: CTL_NET, PF_INET, IPPROTO_IP,
2380: CTL_CREATE, CTL_EOL);
1.189 atatat 2381: #endif /* GATEWAY */
1.197 atatat 2382: sysctl_createv(clog, 0, NULL, NULL,
2383: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2384: CTLTYPE_INT, "hostzerobroadcast",
2385: SYSCTL_DESCR("All zeroes address is broadcast address"),
1.189 atatat 2386: NULL, 0, &hostzeroisbroadcast, 0,
2387: CTL_NET, PF_INET, IPPROTO_IP,
2388: IPCTL_HOSTZEROBROADCAST, CTL_EOL);
2389: #if NGIF > 0
1.197 atatat 2390: sysctl_createv(clog, 0, NULL, NULL,
2391: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2392: CTLTYPE_INT, "gifttl",
2393: SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
1.189 atatat 2394: NULL, 0, &ip_gif_ttl, 0,
2395: CTL_NET, PF_INET, IPPROTO_IP,
2396: IPCTL_GIF_TTL, CTL_EOL);
2397: #endif /* NGIF */
2398: #ifndef IPNOPRIVPORTS
1.197 atatat 2399: sysctl_createv(clog, 0, NULL, NULL,
2400: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2401: CTLTYPE_INT, "lowportmin",
2402: SYSCTL_DESCR("Lowest privileged ephemeral port number "
2403: "to assign"),
1.189 atatat 2404: sysctl_net_inet_ip_ports, 0, &lowportmin, 0,
2405: CTL_NET, PF_INET, IPPROTO_IP,
2406: IPCTL_LOWPORTMIN, CTL_EOL);
1.197 atatat 2407: sysctl_createv(clog, 0, NULL, NULL,
2408: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2409: CTLTYPE_INT, "lowportmax",
2410: SYSCTL_DESCR("Highest privileged ephemeral port number "
2411: "to assign"),
1.189 atatat 2412: sysctl_net_inet_ip_ports, 0, &lowportmax, 0,
2413: CTL_NET, PF_INET, IPPROTO_IP,
2414: IPCTL_LOWPORTMAX, CTL_EOL);
2415: #endif /* IPNOPRIVPORTS */
1.197 atatat 2416: sysctl_createv(clog, 0, NULL, NULL,
2417: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2418: CTLTYPE_INT, "maxfragpackets",
2419: SYSCTL_DESCR("Maximum number of fragments to retain for "
2420: "possible reassembly"),
1.189 atatat 2421: NULL, 0, &ip_maxfragpackets, 0,
2422: CTL_NET, PF_INET, IPPROTO_IP,
2423: IPCTL_MAXFRAGPACKETS, CTL_EOL);
2424: #if NGRE > 0
1.197 atatat 2425: sysctl_createv(clog, 0, NULL, NULL,
2426: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2427: CTLTYPE_INT, "grettl",
2428: SYSCTL_DESCR("Default TTL for a gre tunnel datagram"),
1.189 atatat 2429: NULL, 0, &ip_gre_ttl, 0,
2430: CTL_NET, PF_INET, IPPROTO_IP,
2431: IPCTL_GRE_TTL, CTL_EOL);
2432: #endif /* NGRE */
1.197 atatat 2433: sysctl_createv(clog, 0, NULL, NULL,
2434: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2435: CTLTYPE_INT, "checkinterface",
2436: SYSCTL_DESCR("Enable receive side of Strong ES model "
2437: "from RFC1122"),
1.189 atatat 2438: NULL, 0, &ip_checkinterface, 0,
2439: CTL_NET, PF_INET, IPPROTO_IP,
2440: IPCTL_CHECKINTERFACE, CTL_EOL);
1.197 atatat 2441: sysctl_createv(clog, 0, NULL, NULL,
2442: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 2443: CTLTYPE_INT, "random_id",
2444: SYSCTL_DESCR("Assign random ip_id values"),
1.189 atatat 2445: NULL, 0, &ip_do_randomid, 0,
2446: CTL_NET, PF_INET, IPPROTO_IP,
2447: IPCTL_RANDOMID, CTL_EOL);
1.206 thorpej 2448: sysctl_createv(clog, 0, NULL, NULL,
2449: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2450: CTLTYPE_INT, "do_loopback_cksum",
2451: SYSCTL_DESCR("Perform IP checksum on loopback"),
2452: NULL, 0, &ip_do_loopback_cksum, 0,
2453: CTL_NET, PF_INET, IPPROTO_IP,
2454: IPCTL_LOOPBACKCKSUM, CTL_EOL);
1.219 elad 2455: sysctl_createv(clog, 0, NULL, NULL,
2456: CTLFLAG_PERMANENT,
2457: CTLTYPE_STRUCT, "stats",
2458: SYSCTL_DESCR("IP statistics"),
1.266 thorpej 2459: sysctl_net_inet_ip_stats, 0, NULL, 0,
1.219 elad 2460: CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS,
2461: CTL_EOL);
1.1 cgd 2462: }
1.266 thorpej 2463:
2464: void
2465: ip_statinc(u_int stat)
2466: {
2467:
2468: KASSERT(stat < IP_NSTATS);
2469: IP_STATINC(stat);
2470: }
CVSweb <webmaster@jp.NetBSD.org>