Annotation of src/sys/netinet/ip_input.c, Revision 1.290
1.290 ! rmind 1: /* $NetBSD: ip_input.c,v 1.289 2010/07/19 14:09:45 rmind 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.290 ! rmind 94: __KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.289 2010/07/19 14:09:45 rmind Exp $");
1.55 scottr 95:
1.184 jonathan 96: #include "opt_inet.h"
1.278 christos 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/mbuf.h>
108: #include <sys/domain.h>
109: #include <sys/protosw.h>
110: #include <sys/socket.h>
1.44 thorpej 111: #include <sys/socketvar.h>
1.5 mycroft 112: #include <sys/errno.h>
113: #include <sys/time.h>
114: #include <sys/kernel.h>
1.72 thorpej 115: #include <sys/pool.h>
1.28 christos 116: #include <sys/sysctl.h>
1.230 elad 117: #include <sys/kauth.h>
1.1 cgd 118:
1.5 mycroft 119: #include <net/if.h>
1.44 thorpej 120: #include <net/if_dl.h>
1.5 mycroft 121: #include <net/route.h>
1.45 mrg 122: #include <net/pfil.h>
1.1 cgd 123:
1.5 mycroft 124: #include <netinet/in.h>
125: #include <netinet/in_systm.h>
126: #include <netinet/ip.h>
127: #include <netinet/in_pcb.h>
1.215 yamt 128: #include <netinet/in_proto.h>
1.5 mycroft 129: #include <netinet/in_var.h>
130: #include <netinet/ip_var.h>
1.266 thorpej 131: #include <netinet/ip_private.h>
1.5 mycroft 132: #include <netinet/ip_icmp.h>
1.89 itojun 133: /* just for gif_ttl */
134: #include <netinet/in_gif.h>
135: #include "gif.h"
1.144 martin 136: #include <net/if_gre.h>
137: #include "gre.h"
1.111 jdolecek 138:
139: #ifdef MROUTING
140: #include <netinet/ip_mroute.h>
141: #endif
1.89 itojun 142:
143: #ifdef IPSEC
144: #include <netinet6/ipsec.h>
1.267 thorpej 145: #include <netinet6/ipsec_private.h>
1.89 itojun 146: #include <netkey/key.h>
147: #endif
1.173 jonathan 148: #ifdef FAST_IPSEC
149: #include <netipsec/ipsec.h>
150: #include <netipsec/key.h>
151: #endif /* FAST_IPSEC*/
1.44 thorpej 152:
1.1 cgd 153: #ifndef IPFORWARDING
154: #ifdef GATEWAY
155: #define IPFORWARDING 1 /* forward IP packets not for us */
156: #else /* GATEWAY */
157: #define IPFORWARDING 0 /* don't forward IP packets not for us */
158: #endif /* GATEWAY */
159: #endif /* IPFORWARDING */
160: #ifndef IPSENDREDIRECTS
161: #define IPSENDREDIRECTS 1
162: #endif
1.26 thorpej 163: #ifndef IPFORWSRCRT
1.47 cjs 164: #define IPFORWSRCRT 1 /* forward source-routed packets */
165: #endif
166: #ifndef IPALLOWSRCRT
1.48 mrg 167: #define IPALLOWSRCRT 1 /* allow source-routed packets */
1.26 thorpej 168: #endif
1.53 kml 169: #ifndef IPMTUDISC
1.153 itojun 170: #define IPMTUDISC 1
1.53 kml 171: #endif
1.60 kml 172: #ifndef IPMTUDISCTIMEOUT
1.61 kml 173: #define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */
1.60 kml 174: #endif
1.53 kml 175:
1.278 christos 176: #ifdef COMPAT_50
177: #include <compat/sys/time.h>
178: #include <compat/sys/socket.h>
179: #endif
180:
1.27 thorpej 181: /*
182: * Note: DIRECTED_BROADCAST is handled this way so that previous
183: * configuration using this option will Just Work.
184: */
185: #ifndef IPDIRECTEDBCAST
186: #ifdef DIRECTED_BROADCAST
187: #define IPDIRECTEDBCAST 1
188: #else
189: #define IPDIRECTEDBCAST 0
190: #endif /* DIRECTED_BROADCAST */
191: #endif /* IPDIRECTEDBCAST */
1.1 cgd 192: int ipforwarding = IPFORWARDING;
193: int ipsendredirects = IPSENDREDIRECTS;
1.13 mycroft 194: int ip_defttl = IPDEFTTL;
1.26 thorpej 195: int ip_forwsrcrt = IPFORWSRCRT;
1.27 thorpej 196: int ip_directedbcast = IPDIRECTEDBCAST;
1.47 cjs 197: int ip_allowsrcrt = IPALLOWSRCRT;
1.53 kml 198: int ip_mtudisc = IPMTUDISC;
1.156 itojun 199: int ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
1.1 cgd 200: #ifdef DIAGNOSTIC
201: int ipprintfs = 0;
202: #endif
1.184 jonathan 203:
204: int ip_do_randomid = 0;
205:
1.165 christos 206: /*
207: * XXX - Setting ip_checkinterface mostly implements the receive side of
208: * the Strong ES model described in RFC 1122, but since the routing table
209: * and transmit implementation do not implement the Strong ES model,
210: * setting this to 1 results in an odd hybrid.
211: *
212: * XXX - ip_checkinterface currently must be disabled if you use ipnat
213: * to translate the destination address to another local interface.
214: *
215: * XXX - ip_checkinterface must be disabled if you add IP aliases
216: * to the loopback interface instead of the interface where the
217: * packets for those addresses are received.
218: */
219: int ip_checkinterface = 0;
220:
1.1 cgd 221:
1.60 kml 222: struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
223:
1.1 cgd 224: int ipqmaxlen = IFQ_MAXLEN;
1.150 matt 225: u_long in_ifaddrhash; /* size of hash table - 1 */
226: int in_ifaddrentries; /* total number of addrs */
1.212 perry 227: struct in_ifaddrhead in_ifaddrhead;
1.57 tls 228: struct in_ifaddrhashhead *in_ifaddrhashtbl;
1.166 matt 229: u_long in_multihash; /* size of hash table - 1 */
230: int in_multientries; /* total number of addrs */
231: struct in_multihashhead *in_multihashtbl;
1.13 mycroft 232: struct ifqueue ipintrq;
1.286 tls 233:
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.275 pooka 242: struct pool inmulti_pool;
1.72 thorpej 243:
1.135 thorpej 244: #ifdef INET_CSUM_COUNTERS
245: #include <sys/device.h>
246:
247: struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
248: NULL, "inet", "hwcsum bad");
249: struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
250: NULL, "inet", "hwcsum ok");
251: struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
252: NULL, "inet", "swcsum");
253:
254: #define INET_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
255:
1.201 matt 256: EVCNT_ATTACH_STATIC(ip_hwcsum_bad);
257: EVCNT_ATTACH_STATIC(ip_hwcsum_ok);
258: EVCNT_ATTACH_STATIC(ip_swcsum);
259:
1.135 thorpej 260: #else
261:
262: #define INET_CSUM_COUNTER_INCR(ev) /* nothing */
263:
264: #endif /* INET_CSUM_COUNTERS */
265:
1.1 cgd 266: /*
267: * We need to save the IP options in case a protocol wants to respond
268: * to an incoming packet over the same route if the packet got here
269: * using IP source routing. This allows connection establishment and
270: * maintenance when the remote end is on a network that is not known
271: * to us.
272: */
273: int ip_nhops = 0;
274: static struct ip_srcrt {
275: struct in_addr dst; /* final destination */
276: char nop; /* one NOP to align */
277: char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
278: struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
279: } ip_srcrt;
280:
1.210 perry 281: static void save_rte(u_char *, struct in_addr);
1.35 mycroft 282:
1.164 matt 283: #ifdef MBUFTRACE
1.234 dogcow 284: struct mowner ip_rx_mowner = MOWNER_INIT("internet", "rx");
285: struct mowner ip_tx_mowner = MOWNER_INIT("internet", "tx");
1.164 matt 286: #endif
287:
1.284 pooka 288: static void sysctl_net_inet_ip_setup(struct sysctllog **);
289:
1.1 cgd 290: /*
291: * IP initialization: fill in IP protocol switch table.
292: * All protocols not implemented in kernel go to raw IP protocol handler.
293: */
1.8 mycroft 294: void
1.211 perry 295: ip_init(void)
1.1 cgd 296: {
1.199 matt 297: const struct protosw *pr;
1.109 augustss 298: int i;
1.1 cgd 299:
1.284 pooka 300: sysctl_net_inet_ip_setup(NULL);
301:
1.275 pooka 302: pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
303: NULL, IPL_SOFTNET);
304:
1.1 cgd 305: pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
306: if (pr == 0)
307: panic("ip_init");
308: for (i = 0; i < IPPROTO_MAX; i++)
309: ip_protox[i] = pr - inetsw;
310: for (pr = inetdomain.dom_protosw;
311: pr < inetdomain.dom_protoswNPROTOSW; pr++)
312: if (pr->pr_domain->dom_family == PF_INET &&
313: pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
314: ip_protox[pr->pr_protocol] = pr - inetsw;
1.192 jonathan 315:
1.288 rmind 316: ip_reass_init();
1.190 jonathan 317:
1.262 matt 318: ip_initid();
1.227 kardel 319: ip_id = time_second & 0xfffff;
1.194 jonathan 320:
1.1 cgd 321: ipintrq.ifq_maxlen = ipqmaxlen;
1.194 jonathan 322:
1.181 jonathan 323: TAILQ_INIT(&in_ifaddrhead);
1.272 ad 324: in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
325: &in_ifaddrhash);
326: in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
327: &in_multihash);
1.160 itojun 328: ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout);
1.73 thorpej 329: #ifdef GATEWAY
1.248 liamjfoy 330: ipflow_init(ip_hashsize);
1.73 thorpej 331: #endif
1.121 thorpej 332:
333: #ifdef PFIL_HOOKS
334: /* Register our Packet Filter hook. */
1.126 thorpej 335: inet_pfil_hook.ph_type = PFIL_TYPE_AF;
336: inet_pfil_hook.ph_af = AF_INET;
1.121 thorpej 337: i = pfil_head_register(&inet_pfil_hook);
338: if (i != 0)
339: printf("ip_init: WARNING: unable to register pfil hook, "
340: "error %d\n", i);
341: #endif /* PFIL_HOOKS */
1.135 thorpej 342:
1.164 matt 343: #ifdef MBUFTRACE
344: MOWNER_ATTACH(&ip_tx_mowner);
345: MOWNER_ATTACH(&ip_rx_mowner);
346: #endif /* MBUFTRACE */
1.266 thorpej 347:
348: ipstat_percpu = percpu_alloc(sizeof(uint64_t) * IP_NSTATS);
1.1 cgd 349: }
350:
1.229 christos 351: struct sockaddr_in ipaddr = {
352: .sin_len = sizeof(ipaddr),
353: .sin_family = AF_INET,
354: };
1.1 cgd 355: struct route ipforward_rt;
356:
357: /*
1.89 itojun 358: * IP software interrupt routine
359: */
360: void
1.211 perry 361: ipintr(void)
1.89 itojun 362: {
363: int s;
364: struct mbuf *m;
1.286 tls 365: struct ifqueue lcl_intrq;
366:
367: memset(&lcl_intrq, 0, sizeof(lcl_intrq));
368: ipintrq.ifq_maxlen = ipqmaxlen;
1.89 itojun 369:
1.268 ad 370: mutex_enter(softnet_lock);
371: KERNEL_LOCK(1, NULL);
1.286 tls 372: if (!IF_IS_EMPTY(&ipintrq)) {
1.132 thorpej 373: s = splnet();
1.286 tls 374:
375: /* Take existing queue onto stack */
376: lcl_intrq = ipintrq;
377:
378: /* Zero out global queue, preserving maxlen and drops */
379: ipintrq.ifq_head = NULL;
380: ipintrq.ifq_tail = NULL;
381: ipintrq.ifq_len = 0;
382: ipintrq.ifq_maxlen = lcl_intrq.ifq_maxlen;
383: ipintrq.ifq_drops = lcl_intrq.ifq_drops;
384:
1.89 itojun 385: splx(s);
1.286 tls 386: }
387: KERNEL_UNLOCK_ONE(NULL);
388: while (!IF_IS_EMPTY(&lcl_intrq)) {
389: IF_DEQUEUE(&lcl_intrq, m);
1.268 ad 390: if (m == NULL)
391: break;
1.89 itojun 392: ip_input(m);
393: }
1.268 ad 394: mutex_exit(softnet_lock);
1.89 itojun 395: }
396:
397: /*
1.1 cgd 398: * Ip input routine. Checksum and byte swap header. If fragmented
399: * try to reassemble. Process options. Pass to next level.
400: */
1.8 mycroft 401: void
1.89 itojun 402: ip_input(struct mbuf *m)
1.1 cgd 403: {
1.109 augustss 404: struct ip *ip = NULL;
405: struct in_ifaddr *ia;
406: struct ifaddr *ifa;
1.288 rmind 407: int hlen = 0, len;
1.100 itojun 408: int downmatch;
1.165 christos 409: int checkif;
1.169 itojun 410: int srcrt = 0;
1.173 jonathan 411: #ifdef FAST_IPSEC
412: struct m_tag *mtag;
413: struct tdb_ident *tdbi;
414: struct secpolicy *sp;
1.289 rmind 415: int error, s;
1.173 jonathan 416: #endif /* FAST_IPSEC */
1.1 cgd 417:
1.164 matt 418: MCLAIM(m, &ip_rx_mowner);
1.289 rmind 419: KASSERT((m->m_flags & M_PKTHDR) != 0);
1.164 matt 420:
1.1 cgd 421: /*
422: * If no IP addresses have been set yet but the interfaces
423: * are receiving, can't do anything with incoming packets yet.
424: */
1.181 jonathan 425: if (TAILQ_FIRST(&in_ifaddrhead) == 0)
1.1 cgd 426: goto bad;
1.266 thorpej 427: IP_STATINC(IP_STAT_TOTAL);
1.154 thorpej 428: /*
429: * If the IP header is not aligned, slurp it up into a new
430: * mbuf with space for link headers, in the event we forward
431: * it. Otherwise, if it is aligned, make sure the entire
432: * base IP header is in the first mbuf of the chain.
433: */
1.244 christos 434: if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
1.154 thorpej 435: if ((m = m_copyup(m, sizeof(struct ip),
436: (max_linkhdr + 3) & ~3)) == NULL) {
437: /* XXXJRT new stat, please */
1.266 thorpej 438: IP_STATINC(IP_STAT_TOOSMALL);
1.154 thorpej 439: return;
440: }
441: } else if (__predict_false(m->m_len < sizeof (struct ip))) {
442: if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
1.266 thorpej 443: IP_STATINC(IP_STAT_TOOSMALL);
1.154 thorpej 444: return;
445: }
1.1 cgd 446: }
447: ip = mtod(m, struct ip *);
1.13 mycroft 448: if (ip->ip_v != IPVERSION) {
1.266 thorpej 449: IP_STATINC(IP_STAT_BADVERS);
1.13 mycroft 450: goto bad;
451: }
1.1 cgd 452: hlen = ip->ip_hl << 2;
453: if (hlen < sizeof(struct ip)) { /* minimum header length */
1.266 thorpej 454: IP_STATINC(IP_STAT_BADHLEN);
1.1 cgd 455: goto bad;
456: }
457: if (hlen > m->m_len) {
458: if ((m = m_pullup(m, hlen)) == 0) {
1.266 thorpej 459: IP_STATINC(IP_STAT_BADHLEN);
1.89 itojun 460: return;
1.1 cgd 461: }
462: ip = mtod(m, struct ip *);
463: }
1.98 thorpej 464:
1.85 hwr 465: /*
1.99 thorpej 466: * RFC1122: packets with a multicast source address are
1.98 thorpej 467: * not allowed.
1.85 hwr 468: */
469: if (IN_MULTICAST(ip->ip_src.s_addr)) {
1.266 thorpej 470: IP_STATINC(IP_STAT_BADADDR);
1.85 hwr 471: goto bad;
1.129 itojun 472: }
473:
474: /* 127/8 must not appear on wire - RFC1122 */
475: if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
476: (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
1.130 itojun 477: if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
1.266 thorpej 478: IP_STATINC(IP_STAT_BADADDR);
1.129 itojun 479: goto bad;
1.130 itojun 480: }
1.85 hwr 481: }
482:
1.135 thorpej 483: switch (m->m_pkthdr.csum_flags &
1.137 thorpej 484: ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
1.135 thorpej 485: M_CSUM_IPv4_BAD)) {
486: case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
487: INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad);
488: goto badcsum;
489:
490: case M_CSUM_IPv4:
491: /* Checksum was okay. */
492: INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok);
493: break;
494:
495: default:
1.206 thorpej 496: /*
497: * Must compute it ourselves. Maybe skip checksum on
498: * loopback interfaces.
499: */
500: if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
501: IFF_LOOPBACK) || ip_do_loopback_cksum)) {
502: INET_CSUM_COUNTER_INCR(&ip_swcsum);
503: if (in_cksum(m, hlen) != 0)
504: goto badcsum;
505: }
1.135 thorpej 506: break;
1.1 cgd 507: }
508:
1.121 thorpej 509: /* Retrieve the packet length. */
510: len = ntohs(ip->ip_len);
1.81 proff 511:
512: /*
513: * Check for additional length bogosity
514: */
1.84 proff 515: if (len < hlen) {
1.266 thorpej 516: IP_STATINC(IP_STAT_BADLEN);
1.81 proff 517: goto bad;
518: }
1.1 cgd 519:
520: /*
521: * Check that the amount of data in the buffers
522: * is as at least much as the IP header would have us expect.
523: * Trim mbufs if longer than we expect.
524: * Drop packet if shorter than we expect.
525: */
1.35 mycroft 526: if (m->m_pkthdr.len < len) {
1.266 thorpej 527: IP_STATINC(IP_STAT_TOOSHORT);
1.1 cgd 528: goto bad;
529: }
1.35 mycroft 530: if (m->m_pkthdr.len > len) {
1.1 cgd 531: if (m->m_len == m->m_pkthdr.len) {
1.35 mycroft 532: m->m_len = len;
533: m->m_pkthdr.len = len;
1.1 cgd 534: } else
1.35 mycroft 535: m_adj(m, len - m->m_pkthdr.len);
1.1 cgd 536: }
537:
1.193 scw 538: #if defined(IPSEC)
1.149 wiz 539: /* ipflow (IP fast forwarding) is not compatible with IPsec. */
1.94 itojun 540: m->m_flags &= ~M_CANFASTFWD;
541: #else
1.64 thorpej 542: /*
543: * Assume that we can create a fast-forward IP flow entry
544: * based on this packet.
545: */
546: m->m_flags |= M_CANFASTFWD;
1.94 itojun 547: #endif
1.64 thorpej 548:
1.36 mrg 549: #ifdef PFIL_HOOKS
1.33 mrg 550: /*
1.64 thorpej 551: * Run through list of hooks for input packets. If there are any
552: * filters which require that additional packets in the flow are
553: * not fast-forwarded, they must clear the M_CANFASTFWD flag.
554: * Note that filters must _never_ set this flag, as another filter
555: * in the list may have previously cleared it.
1.33 mrg 556: */
1.127 itojun 557: /*
558: * let ipfilter look at packet on the wire,
559: * not the decapsulated packet.
560: */
561: #ifdef IPSEC
1.136 itojun 562: if (!ipsec_getnhist(m))
1.186 scw 563: #elif defined(FAST_IPSEC)
564: if (!ipsec_indone(m))
1.127 itojun 565: #else
566: if (1)
567: #endif
568: {
1.169 itojun 569: struct in_addr odst;
570:
571: odst = ip->ip_dst;
1.127 itojun 572: if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
1.168 itojun 573: PFIL_IN) != 0)
574: return;
1.127 itojun 575: if (m == NULL)
576: return;
577: ip = mtod(m, struct ip *);
1.142 darrenr 578: hlen = ip->ip_hl << 2;
1.205 darrenr 579: /*
580: * XXX The setting of "srcrt" here is to prevent ip_forward()
581: * from generating ICMP redirects for packets that have
582: * been redirected by a hook back out on to the same LAN that
583: * they came from and is not an indication that the packet
584: * is being inffluenced by source routing options. This
585: * allows things like
586: * "rdr tlp0 0/0 port 80 -> 1.1.1.200 3128 tcp"
587: * where tlp0 is both on the 1.1.1.0/24 network and is the
588: * default route for hosts on 1.1.1.0/24. Of course this
589: * also requires a "map tlp0 ..." to complete the story.
590: * One might argue whether or not this kind of network config.
1.212 perry 591: * should be supported in this manner...
1.205 darrenr 592: */
1.169 itojun 593: srcrt = (odst.s_addr != ip->ip_dst.s_addr);
1.127 itojun 594: }
1.36 mrg 595: #endif /* PFIL_HOOKS */
1.123 thorpej 596:
597: #ifdef ALTQ
598: /* XXX Temporary until ALTQ is changed to use a pfil hook */
599: if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
600: /* packet dropped by traffic conditioner */
601: return;
602: }
603: #endif
1.121 thorpej 604:
605: /*
1.1 cgd 606: * Process options and, if not destined for us,
607: * ship it on. ip_dooptions returns 1 when an
608: * error was detected (causing an icmp message
609: * to be sent and the original packet to be freed).
610: */
611: ip_nhops = 0; /* for source routed packets */
612: if (hlen > sizeof (struct ip) && ip_dooptions(m))
1.89 itojun 613: return;
1.1 cgd 614:
615: /*
1.165 christos 616: * Enable a consistency check between the destination address
617: * and the arrival interface for a unicast packet (the RFC 1122
618: * strong ES model) if IP forwarding is disabled and the packet
619: * is not locally generated.
620: *
621: * XXX - Checking also should be disabled if the destination
622: * address is ipnat'ed to a different interface.
623: *
624: * XXX - Checking is incompatible with IP aliases added
625: * to the loopback interface instead of the interface where
626: * the packets are received.
627: *
628: * XXX - We need to add a per ifaddr flag for this so that
629: * we get finer grain control.
630: */
631: checkif = ip_checkinterface && (ipforwarding == 0) &&
632: (m->m_pkthdr.rcvif != NULL) &&
633: ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0);
634:
635: /*
1.1 cgd 636: * Check our list of addresses, to see if the packet is for us.
1.100 itojun 637: *
638: * Traditional 4.4BSD did not consult IFF_UP at all.
639: * The behavior here is to treat addresses on !IFF_UP interface
640: * as not mine.
1.1 cgd 641: */
1.100 itojun 642: downmatch = 0;
1.140 matt 643: LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
1.97 itojun 644: if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
1.165 christos 645: if (checkif && ia->ia_ifp != m->m_pkthdr.rcvif)
646: continue;
1.97 itojun 647: if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
648: break;
1.100 itojun 649: else
650: downmatch++;
1.97 itojun 651: }
652: }
1.86 thorpej 653: if (ia != NULL)
654: goto ours;
1.225 christos 655: if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
1.209 matt 656: IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
1.140 matt 657: if (ifa->ifa_addr->sa_family != AF_INET)
658: continue;
1.57 tls 659: ia = ifatoia(ifa);
1.35 mycroft 660: if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
661: in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
1.20 mycroft 662: /*
663: * Look for all-0's host part (old broadcast addr),
664: * either for subnet or net.
665: */
666: ip->ip_dst.s_addr == ia->ia_subnet ||
1.18 mycroft 667: ip->ip_dst.s_addr == ia->ia_net)
1.1 cgd 668: goto ours;
1.57 tls 669: /*
670: * An interface with IP address zero accepts
671: * all packets that arrive on that interface.
672: */
673: if (in_nullhost(ia->ia_addr.sin_addr))
674: goto ours;
1.1 cgd 675: }
676: }
1.18 mycroft 677: if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4 hpeyerl 678: struct in_multi *inm;
679: #ifdef MROUTING
680: extern struct socket *ip_mrouter;
1.10 brezak 681:
1.4 hpeyerl 682: if (ip_mrouter) {
683: /*
684: * If we are acting as a multicast router, all
685: * incoming multicast packets are passed to the
686: * kernel-level multicast forwarding function.
687: * The packet is returned (relatively) intact; if
688: * ip_mforward() returns a non-zero value, the packet
689: * must be discarded, else it may be accepted below.
690: *
691: * (The IP ident field is put in the same byte order
692: * as expected when ip_mforward() is called from
693: * ip_output().)
694: */
1.13 mycroft 695: if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
1.266 thorpej 696: IP_STATINC(IP_STAT_CANTFORWARD);
1.4 hpeyerl 697: m_freem(m);
1.89 itojun 698: return;
1.4 hpeyerl 699: }
700:
701: /*
702: * The process-level routing demon needs to receive
703: * all multicast IGMP packets, whether or not this
704: * host belongs to their destination groups.
705: */
706: if (ip->ip_p == IPPROTO_IGMP)
707: goto ours;
1.266 thorpej 708: IP_STATINC(IP_STAT_CANTFORWARD);
1.4 hpeyerl 709: }
710: #endif
711: /*
712: * See if we belong to the destination multicast group on the
713: * arrival interface.
714: */
715: IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
716: if (inm == NULL) {
1.266 thorpej 717: IP_STATINC(IP_STAT_CANTFORWARD);
1.4 hpeyerl 718: m_freem(m);
1.89 itojun 719: return;
1.4 hpeyerl 720: }
721: goto ours;
722: }
1.19 mycroft 723: if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
1.35 mycroft 724: in_nullhost(ip->ip_dst))
1.1 cgd 725: goto ours;
726:
727: /*
728: * Not for us; forward if possible and desirable.
729: */
730: if (ipforwarding == 0) {
1.266 thorpej 731: IP_STATINC(IP_STAT_CANTFORWARD);
1.1 cgd 732: m_freem(m);
1.100 itojun 733: } else {
734: /*
735: * If ip_dst matched any of my address on !IFF_UP interface,
736: * and there's no IFF_UP interface that matches ip_dst,
737: * send icmp unreach. Forwarding it will result in in-kernel
738: * forwarding loop till TTL goes to 0.
739: */
740: if (downmatch) {
741: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1.266 thorpej 742: IP_STATINC(IP_STAT_CANTFORWARD);
1.100 itojun 743: return;
744: }
1.145 itojun 745: #ifdef IPSEC
746: if (ipsec4_in_reject(m, NULL)) {
1.267 thorpej 747: IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.145 itojun 748: goto bad;
749: }
750: #endif
1.173 jonathan 751: #ifdef FAST_IPSEC
752: mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
753: s = splsoftnet();
754: if (mtag != NULL) {
755: tdbi = (struct tdb_ident *)(mtag + 1);
756: sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
757: } else {
758: sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
1.212 perry 759: IP_FORWARDING, &error);
1.173 jonathan 760: }
761: if (sp == NULL) { /* NB: can happen if error */
762: splx(s);
763: /*XXX error stat???*/
764: DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/
765: goto bad;
766: }
767:
768: /*
769: * Check security policy against packet attributes.
770: */
771: error = ipsec_in_reject(sp, m);
772: KEY_FREESP(&sp);
773: splx(s);
774: if (error) {
1.266 thorpej 775: IP_STATINC(IP_STAT_CANTFORWARD);
1.173 jonathan 776: goto bad;
1.193 scw 777: }
778:
779: /*
780: * Peek at the outbound SP for this packet to determine if
781: * it's a Fast Forward candidate.
782: */
783: mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
784: if (mtag != NULL)
785: m->m_flags &= ~M_CANFASTFWD;
786: else {
787: s = splsoftnet();
788: sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND,
789: (IP_FORWARDING |
790: (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
791: &error, NULL);
792: if (sp != NULL) {
793: m->m_flags &= ~M_CANFASTFWD;
794: KEY_FREESP(&sp);
795: }
796: splx(s);
1.173 jonathan 797: }
798: #endif /* FAST_IPSEC */
1.145 itojun 799:
1.169 itojun 800: ip_forward(m, srcrt);
1.100 itojun 801: }
1.89 itojun 802: return;
1.1 cgd 803:
804: ours:
805: /*
806: * If offset or IP_MF are set, must reassemble.
807: */
1.155 itojun 808: if (ip->ip_off & ~htons(IP_DF|IP_RF)) {
1.289 rmind 809: /*
810: * Pass to IP reassembly mechanism.
811: */
1.290 ! rmind 812: if (ip_reass_packet(&m, ip) != 0) {
1.289 rmind 813: /* Failed; invalid fragment(s) or packet. */
1.288 rmind 814: goto bad;
1.16 cgd 815: }
1.290 ! rmind 816: if (m == NULL) {
1.289 rmind 817: /* More fragments should come; silently return. */
818: return;
819: }
1.290 ! rmind 820: /*
! 821: * Reassembly is done, we have the final packet.
! 822: * Updated cached data in local variable(s).
! 823: */
1.289 rmind 824: ip = mtod(m, struct ip *);
825: hlen = ip->ip_hl << 2;
1.79 mycroft 826: }
1.128 itojun 827:
1.173 jonathan 828: #if defined(IPSEC)
1.128 itojun 829: /*
830: * enforce IPsec policy checking if we are seeing last header.
831: * note that we do not visit this with protocols with pcb layer
832: * code - like udp/tcp/raw ip.
833: */
834: if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
835: ipsec4_in_reject(m, NULL)) {
1.267 thorpej 836: IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.128 itojun 837: goto bad;
838: }
839: #endif
1.226 liamjfoy 840: #ifdef FAST_IPSEC
1.173 jonathan 841: /*
842: * enforce IPsec policy checking if we are seeing last header.
843: * note that we do not visit this with protocols with pcb layer
844: * code - like udp/tcp/raw ip.
845: */
846: if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
847: /*
848: * Check if the packet has already had IPsec processing
849: * done. If so, then just pass it along. This tag gets
850: * set during AH, ESP, etc. input handling, before the
851: * packet is returned to the ip input queue for delivery.
1.212 perry 852: */
1.173 jonathan 853: mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
854: s = splsoftnet();
855: if (mtag != NULL) {
856: tdbi = (struct tdb_ident *)(mtag + 1);
857: sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
858: } else {
859: sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
1.212 perry 860: IP_FORWARDING, &error);
1.173 jonathan 861: }
862: if (sp != NULL) {
863: /*
864: * Check security policy against packet attributes.
865: */
866: error = ipsec_in_reject(sp, m);
867: KEY_FREESP(&sp);
868: } else {
869: /* XXX error stat??? */
870: error = EINVAL;
871: DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
872: }
873: splx(s);
874: if (error)
875: goto bad;
876: }
877: #endif /* FAST_IPSEC */
1.1 cgd 878:
879: /*
880: * Switch out to protocol's input routine.
881: */
1.82 aidan 882: #if IFA_STATS
1.122 itojun 883: if (ia && ip)
1.155 itojun 884: ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len);
1.82 aidan 885: #endif
1.266 thorpej 886: IP_STATINC(IP_STAT_DELIVERED);
1.89 itojun 887: {
888: int off = hlen, nh = ip->ip_p;
889:
890: (*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
891: return;
892: }
1.1 cgd 893: bad:
894: m_freem(m);
1.135 thorpej 895: return;
896:
897: badcsum:
1.266 thorpej 898: IP_STATINC(IP_STAT_BADSUM);
1.135 thorpej 899: m_freem(m);
1.1 cgd 900: }
901:
902: /*
1.288 rmind 903: * IP timer processing.
1.1 cgd 904: */
1.8 mycroft 905: void
1.211 perry 906: ip_slowtimo(void)
1.1 cgd 907: {
1.268 ad 908:
909: mutex_enter(softnet_lock);
910: KERNEL_LOCK(1, NULL);
1.1 cgd 911:
1.288 rmind 912: ip_reass_slowtimo();
1.268 ad 913:
914: KERNEL_UNLOCK_ONE(NULL);
915: mutex_exit(softnet_lock);
1.1 cgd 916: }
917:
918: /*
1.288 rmind 919: * IP drain processing.
1.1 cgd 920: */
1.8 mycroft 921: void
1.211 perry 922: ip_drain(void)
1.1 cgd 923: {
924:
1.268 ad 925: KERNEL_LOCK(1, NULL);
1.288 rmind 926: ip_reass_drain();
1.268 ad 927: KERNEL_UNLOCK_ONE(NULL);
1.1 cgd 928: }
929:
930: /*
931: * Do option processing on a datagram,
932: * possibly discarding it if bad options are encountered,
933: * or forwarding it if source-routed.
934: * Returns 1 if packet has been forwarded/freed,
935: * 0 if the packet should be processed further.
936: */
1.8 mycroft 937: int
1.211 perry 938: ip_dooptions(struct mbuf *m)
1.1 cgd 939: {
1.109 augustss 940: struct ip *ip = mtod(m, struct ip *);
941: u_char *cp, *cp0;
942: struct ip_timestamp *ipt;
943: struct in_ifaddr *ia;
1.1 cgd 944: int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1.104 thorpej 945: struct in_addr dst;
1.1 cgd 946: n_time ntime;
947:
1.13 mycroft 948: dst = ip->ip_dst;
1.1 cgd 949: cp = (u_char *)(ip + 1);
950: cnt = (ip->ip_hl << 2) - sizeof (struct ip);
951: for (; cnt > 0; cnt -= optlen, cp += optlen) {
952: opt = cp[IPOPT_OPTVAL];
953: if (opt == IPOPT_EOL)
954: break;
955: if (opt == IPOPT_NOP)
956: optlen = 1;
957: else {
1.113 itojun 958: if (cnt < IPOPT_OLEN + sizeof(*cp)) {
959: code = &cp[IPOPT_OLEN] - (u_char *)ip;
960: goto bad;
961: }
1.1 cgd 962: optlen = cp[IPOPT_OLEN];
1.114 itojun 963: if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1.1 cgd 964: code = &cp[IPOPT_OLEN] - (u_char *)ip;
965: goto bad;
966: }
967: }
968: switch (opt) {
969:
970: default:
971: break;
972:
973: /*
974: * Source routing with record.
975: * Find interface with current destination address.
976: * If none on this machine then drop if strictly routed,
977: * or do nothing if loosely routed.
978: * Record interface address and bring up next address
979: * component. If strictly routed make sure next
980: * address is on directly accessible net.
981: */
982: case IPOPT_LSRR:
983: case IPOPT_SSRR:
1.47 cjs 984: if (ip_allowsrcrt == 0) {
985: type = ICMP_UNREACH;
986: code = ICMP_UNREACH_NET_PROHIB;
987: goto bad;
988: }
1.114 itojun 989: if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
990: code = &cp[IPOPT_OLEN] - (u_char *)ip;
991: goto bad;
992: }
1.1 cgd 993: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
994: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
995: goto bad;
996: }
997: ipaddr.sin_addr = ip->ip_dst;
1.19 mycroft 998: ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1.1 cgd 999: if (ia == 0) {
1000: if (opt == IPOPT_SSRR) {
1001: type = ICMP_UNREACH;
1002: code = ICMP_UNREACH_SRCFAIL;
1003: goto bad;
1004: }
1005: /*
1006: * Loose routing, and not at next destination
1007: * yet; nothing to do except forward.
1008: */
1009: break;
1010: }
1011: off--; /* 0 origin */
1.112 sommerfe 1012: if ((off + sizeof(struct in_addr)) > optlen) {
1.1 cgd 1013: /*
1014: * End of source route. Should be for us.
1015: */
1016: save_rte(cp, ip->ip_src);
1017: break;
1018: }
1019: /*
1020: * locate outgoing interface
1021: */
1.281 tsutsui 1022: memcpy((void *)&ipaddr.sin_addr, (void *)(cp + off),
1.1 cgd 1023: sizeof(ipaddr.sin_addr));
1.96 thorpej 1024: if (opt == IPOPT_SSRR)
1.196 itojun 1025: ia = ifatoia(ifa_ifwithladdr(sintosa(&ipaddr)));
1.96 thorpej 1026: else
1.1 cgd 1027: ia = ip_rtaddr(ipaddr.sin_addr);
1028: if (ia == 0) {
1029: type = ICMP_UNREACH;
1030: code = ICMP_UNREACH_SRCFAIL;
1031: goto bad;
1032: }
1033: ip->ip_dst = ipaddr.sin_addr;
1.244 christos 1034: bcopy((void *)&ia->ia_addr.sin_addr,
1035: (void *)(cp + off), sizeof(struct in_addr));
1.1 cgd 1036: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13 mycroft 1037: /*
1038: * Let ip_intr's mcast routing check handle mcast pkts
1039: */
1.18 mycroft 1040: forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1 cgd 1041: break;
1042:
1043: case IPOPT_RR:
1.114 itojun 1044: if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1045: code = &cp[IPOPT_OLEN] - (u_char *)ip;
1046: goto bad;
1047: }
1.1 cgd 1048: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1049: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1050: goto bad;
1051: }
1052: /*
1053: * If no space remains, ignore.
1054: */
1055: off--; /* 0 origin */
1.112 sommerfe 1056: if ((off + sizeof(struct in_addr)) > optlen)
1.1 cgd 1057: break;
1.281 tsutsui 1058: memcpy((void *)&ipaddr.sin_addr, (void *)(&ip->ip_dst),
1.1 cgd 1059: sizeof(ipaddr.sin_addr));
1060: /*
1061: * locate outgoing interface; if we're the destination,
1062: * use the incoming interface (should be same).
1063: */
1.96 thorpej 1064: if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
1065: == NULL &&
1066: (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1.1 cgd 1067: type = ICMP_UNREACH;
1068: code = ICMP_UNREACH_HOST;
1069: goto bad;
1070: }
1.244 christos 1071: bcopy((void *)&ia->ia_addr.sin_addr,
1072: (void *)(cp + off), sizeof(struct in_addr));
1.1 cgd 1073: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1074: break;
1075:
1076: case IPOPT_TS:
1077: code = cp - (u_char *)ip;
1078: ipt = (struct ip_timestamp *)cp;
1.114 itojun 1079: if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
1080: code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1.1 cgd 1081: goto bad;
1.114 itojun 1082: }
1083: if (ipt->ipt_ptr < 5) {
1084: code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
1085: goto bad;
1086: }
1.15 cgd 1087: if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.114 itojun 1088: if (++ipt->ipt_oflw == 0) {
1089: code = (u_char *)&ipt->ipt_ptr -
1090: (u_char *)ip;
1.1 cgd 1091: goto bad;
1.114 itojun 1092: }
1.1 cgd 1093: break;
1094: }
1.104 thorpej 1095: cp0 = (cp + ipt->ipt_ptr - 1);
1.1 cgd 1096: switch (ipt->ipt_flg) {
1097:
1098: case IPOPT_TS_TSONLY:
1099: break;
1100:
1101: case IPOPT_TS_TSANDADDR:
1.66 thorpej 1102: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.114 itojun 1103: sizeof(struct in_addr) > ipt->ipt_len) {
1104: code = (u_char *)&ipt->ipt_ptr -
1105: (u_char *)ip;
1.1 cgd 1106: goto bad;
1.114 itojun 1107: }
1.13 mycroft 1108: ipaddr.sin_addr = dst;
1.96 thorpej 1109: ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
1110: m->m_pkthdr.rcvif));
1.13 mycroft 1111: if (ia == 0)
1112: continue;
1.104 thorpej 1113: bcopy(&ia->ia_addr.sin_addr,
1114: cp0, sizeof(struct in_addr));
1.1 cgd 1115: ipt->ipt_ptr += sizeof(struct in_addr);
1116: break;
1117:
1118: case IPOPT_TS_PRESPEC:
1.66 thorpej 1119: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.114 itojun 1120: sizeof(struct in_addr) > ipt->ipt_len) {
1121: code = (u_char *)&ipt->ipt_ptr -
1122: (u_char *)ip;
1.1 cgd 1123: goto bad;
1.114 itojun 1124: }
1.281 tsutsui 1125: memcpy(&ipaddr.sin_addr, cp0,
1.1 cgd 1126: sizeof(struct in_addr));
1.96 thorpej 1127: if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
1128: == NULL)
1.1 cgd 1129: continue;
1130: ipt->ipt_ptr += sizeof(struct in_addr);
1131: break;
1132:
1133: default:
1.114 itojun 1134: /* XXX can't take &ipt->ipt_flg */
1135: code = (u_char *)&ipt->ipt_ptr -
1136: (u_char *)ip + 1;
1.1 cgd 1137: goto bad;
1138: }
1139: ntime = iptime();
1.107 thorpej 1140: cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
1.244 christos 1141: memmove((char *)cp + ipt->ipt_ptr - 1, cp0,
1.1 cgd 1142: sizeof(n_time));
1143: ipt->ipt_ptr += sizeof(n_time);
1144: }
1145: }
1146: if (forward) {
1.26 thorpej 1147: if (ip_forwsrcrt == 0) {
1148: type = ICMP_UNREACH;
1149: code = ICMP_UNREACH_SRCFAIL;
1150: goto bad;
1151: }
1.1 cgd 1152: ip_forward(m, 1);
1153: return (1);
1.13 mycroft 1154: }
1155: return (0);
1.1 cgd 1156: bad:
1.13 mycroft 1157: icmp_error(m, type, code, 0, 0);
1.266 thorpej 1158: IP_STATINC(IP_STAT_BADOPTIONS);
1.1 cgd 1159: return (1);
1160: }
1161:
1162: /*
1163: * Given address of next destination (final or next hop),
1164: * return internet address info of interface to be used to get there.
1165: */
1166: struct in_ifaddr *
1.211 perry 1167: ip_rtaddr(struct in_addr dst)
1.1 cgd 1168: {
1.249 dyoung 1169: struct rtentry *rt;
1170: union {
1171: struct sockaddr dst;
1172: struct sockaddr_in dst4;
1173: } u;
1174:
1175: sockaddr_in_init(&u.dst4, &dst, 0);
1176:
1177: if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL)
1178: return NULL;
1179:
1180: return ifatoia(rt->rt_ifa);
1.1 cgd 1181: }
1182:
1183: /*
1184: * Save incoming source route for use in replies,
1185: * to be picked up later by ip_srcroute if the receiver is interested.
1186: */
1.13 mycroft 1187: void
1.211 perry 1188: save_rte(u_char *option, struct in_addr dst)
1.1 cgd 1189: {
1190: unsigned olen;
1191:
1192: olen = option[IPOPT_OLEN];
1193: #ifdef DIAGNOSTIC
1194: if (ipprintfs)
1.39 christos 1195: printf("save_rte: olen %d\n", olen);
1.89 itojun 1196: #endif /* 0 */
1.1 cgd 1197: if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1198: return;
1.281 tsutsui 1199: memcpy((void *)ip_srcrt.srcopt, (void *)option, olen);
1.1 cgd 1200: ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1201: ip_srcrt.dst = dst;
1202: }
1203:
1204: /*
1205: * Retrieve incoming source route for use in replies,
1206: * in the same form used by setsockopt.
1207: * The first hop is placed before the options, will be removed later.
1208: */
1209: struct mbuf *
1.211 perry 1210: ip_srcroute(void)
1.1 cgd 1211: {
1.109 augustss 1212: struct in_addr *p, *q;
1213: struct mbuf *m;
1.1 cgd 1214:
1215: if (ip_nhops == 0)
1.237 dyoung 1216: return NULL;
1.1 cgd 1217: m = m_get(M_DONTWAIT, MT_SOOPTS);
1218: if (m == 0)
1.237 dyoung 1219: return NULL;
1.1 cgd 1220:
1.164 matt 1221: MCLAIM(m, &inetdomain.dom_mowner);
1.13 mycroft 1222: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1 cgd 1223:
1224: /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1225: m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1226: OPTSIZ;
1227: #ifdef DIAGNOSTIC
1228: if (ipprintfs)
1.39 christos 1229: printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1.1 cgd 1230: #endif
1231:
1232: /*
1233: * First save first hop for return route
1234: */
1235: p = &ip_srcrt.route[ip_nhops - 1];
1236: *(mtod(m, struct in_addr *)) = *p--;
1237: #ifdef DIAGNOSTIC
1238: if (ipprintfs)
1.39 christos 1239: printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1.1 cgd 1240: #endif
1241:
1242: /*
1243: * Copy option fields and padding (nop) to mbuf.
1244: */
1245: ip_srcrt.nop = IPOPT_NOP;
1246: ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1.244 christos 1247: memmove(mtod(m, char *) + sizeof(struct in_addr), &ip_srcrt.nop,
1248: OPTSIZ);
1249: q = (struct in_addr *)(mtod(m, char *) +
1.1 cgd 1250: sizeof(struct in_addr) + OPTSIZ);
1251: #undef OPTSIZ
1252: /*
1253: * Record return path as an IP source route,
1254: * reversing the path (pointers are now aligned).
1255: */
1256: while (p >= ip_srcrt.route) {
1257: #ifdef DIAGNOSTIC
1258: if (ipprintfs)
1.39 christos 1259: printf(" %x", ntohl(q->s_addr));
1.1 cgd 1260: #endif
1261: *q++ = *p--;
1262: }
1263: /*
1264: * Last hop goes to final destination.
1265: */
1266: *q = ip_srcrt.dst;
1267: #ifdef DIAGNOSTIC
1268: if (ipprintfs)
1.39 christos 1269: printf(" %x\n", ntohl(q->s_addr));
1.1 cgd 1270: #endif
1271: return (m);
1272: }
1273:
1.139 matt 1274: const int inetctlerrmap[PRC_NCMDS] = {
1.256 yamt 1275: [PRC_MSGSIZE] = EMSGSIZE,
1276: [PRC_HOSTDEAD] = EHOSTDOWN,
1277: [PRC_HOSTUNREACH] = EHOSTUNREACH,
1278: [PRC_UNREACH_NET] = EHOSTUNREACH,
1279: [PRC_UNREACH_HOST] = EHOSTUNREACH,
1280: [PRC_UNREACH_PROTOCOL] = ECONNREFUSED,
1281: [PRC_UNREACH_PORT] = ECONNREFUSED,
1282: [PRC_UNREACH_SRCFAIL] = EHOSTUNREACH,
1283: [PRC_PARAMPROB] = ENOPROTOOPT,
1.1 cgd 1284: };
1285:
1286: /*
1287: * Forward a packet. If some error occurs return the sender
1288: * an icmp packet. Note we can't always generate a meaningful
1289: * icmp message because icmp doesn't have a large enough repertoire
1290: * of codes and types.
1291: *
1292: * If not forwarding, just drop the packet. This could be confusing
1293: * if ipforwarding was zero but some routing protocol was advancing
1294: * us as a gateway to somewhere. However, we must let the routing
1295: * protocol deal with that.
1296: *
1297: * The srcrt parameter indicates whether the packet is being forwarded
1298: * via a source route.
1299: */
1.13 mycroft 1300: void
1.211 perry 1301: ip_forward(struct mbuf *m, int srcrt)
1.1 cgd 1302: {
1.109 augustss 1303: struct ip *ip = mtod(m, struct ip *);
1304: struct rtentry *rt;
1.220 christos 1305: int error, type = 0, code = 0, destmtu = 0;
1.1 cgd 1306: struct mbuf *mcopy;
1.13 mycroft 1307: n_long dest;
1.249 dyoung 1308: union {
1309: struct sockaddr dst;
1310: struct sockaddr_in dst4;
1311: } u;
1.164 matt 1312:
1313: /*
1314: * We are now in the output path.
1315: */
1316: MCLAIM(m, &ip_tx_mowner);
1.135 thorpej 1317:
1318: /*
1319: * Clear any in-bound checksum flags for this packet.
1320: */
1321: m->m_pkthdr.csum_flags = 0;
1.1 cgd 1322:
1.13 mycroft 1323: dest = 0;
1.1 cgd 1324: #ifdef DIAGNOSTIC
1.224 joerg 1325: if (ipprintfs) {
1326: printf("forward: src %s ", inet_ntoa(ip->ip_src));
1327: printf("dst %s ttl %x\n", inet_ntoa(ip->ip_dst), ip->ip_ttl);
1328: }
1.1 cgd 1329: #endif
1.93 sommerfe 1330: if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1.266 thorpej 1331: IP_STATINC(IP_STAT_CANTFORWARD);
1.1 cgd 1332: m_freem(m);
1333: return;
1334: }
1335: if (ip->ip_ttl <= IPTTLDEC) {
1.13 mycroft 1336: icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1 cgd 1337: return;
1338: }
1339:
1.249 dyoung 1340: sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
1341: if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL) {
1342: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, dest, 0);
1343: return;
1.1 cgd 1344: }
1345:
1346: /*
1.34 mycroft 1347: * Save at most 68 bytes of the packet in case
1.1 cgd 1348: * we need to generate an ICMP message to the src.
1.119 itojun 1349: * Pullup to avoid sharing mbuf cluster between m and mcopy.
1.1 cgd 1350: */
1.155 itojun 1351: mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT);
1.119 itojun 1352: if (mcopy)
1353: mcopy = m_pullup(mcopy, ip->ip_hl << 2);
1.1 cgd 1354:
1.221 christos 1355: ip->ip_ttl -= IPTTLDEC;
1356:
1.1 cgd 1357: /*
1358: * If forwarding packet using same interface that it came in on,
1359: * perhaps should send a redirect to sender to shortcut a hop.
1360: * Only send redirect if source is sending directly to us,
1361: * and if packet was not source routed (or has any options).
1362: * Also, don't send redirect if forwarding using a default route
1363: * or a route modified by a redirect.
1364: */
1365: if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1366: (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1.250 dyoung 1367: !in_nullhost(satocsin(rt_getkey(rt))->sin_addr) &&
1.1 cgd 1368: ipsendredirects && !srcrt) {
1.19 mycroft 1369: if (rt->rt_ifa &&
1370: (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1371: ifatoia(rt->rt_ifa)->ia_subnet) {
1.77 thorpej 1372: if (rt->rt_flags & RTF_GATEWAY)
1373: dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1374: else
1375: dest = ip->ip_dst.s_addr;
1376: /*
1377: * Router requirements says to only send host
1378: * redirects.
1379: */
1380: type = ICMP_REDIRECT;
1381: code = ICMP_REDIRECT_HOST;
1.1 cgd 1382: #ifdef DIAGNOSTIC
1.77 thorpej 1383: if (ipprintfs)
1384: printf("redirect (%d) to %x\n", code,
1385: (u_int32_t)dest);
1.1 cgd 1386: #endif
1387: }
1388: }
1389:
1.238 dyoung 1390: error = ip_output(m, NULL, &ipforward_rt,
1.173 jonathan 1391: (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
1.174 itojun 1392: (struct ip_moptions *)NULL, (struct socket *)NULL);
1.173 jonathan 1393:
1.1 cgd 1394: if (error)
1.266 thorpej 1395: IP_STATINC(IP_STAT_CANTFORWARD);
1.1 cgd 1396: else {
1.266 thorpej 1397: uint64_t *ips = IP_STAT_GETREF();
1398: ips[IP_STAT_FORWARD]++;
1399: if (type) {
1400: ips[IP_STAT_REDIRECTSENT]++;
1401: IP_STAT_PUTREF();
1402: } else {
1403: IP_STAT_PUTREF();
1.63 matt 1404: if (mcopy) {
1405: #ifdef GATEWAY
1.64 thorpej 1406: if (mcopy->m_flags & M_CANFASTFWD)
1407: ipflow_create(&ipforward_rt, mcopy);
1.63 matt 1408: #endif
1.1 cgd 1409: m_freem(mcopy);
1.63 matt 1410: }
1.1 cgd 1411: return;
1412: }
1413: }
1414: if (mcopy == NULL)
1415: return;
1.13 mycroft 1416:
1.1 cgd 1417: switch (error) {
1418:
1419: case 0: /* forwarded, but need redirect */
1420: /* type, code set above */
1421: break;
1422:
1423: case ENETUNREACH: /* shouldn't happen, checked above */
1424: case EHOSTUNREACH:
1425: case ENETDOWN:
1426: case EHOSTDOWN:
1427: default:
1428: type = ICMP_UNREACH;
1429: code = ICMP_UNREACH_HOST;
1430: break;
1431:
1432: case EMSGSIZE:
1433: type = ICMP_UNREACH;
1434: code = ICMP_UNREACH_NEEDFRAG;
1.263 cube 1435:
1.274 seanb 1436: if ((rt = rtcache_validate(&ipforward_rt)) != NULL)
1437: destmtu = rt->rt_ifp->if_mtu;
1.263 cube 1438:
1439: #if defined(IPSEC) || defined(FAST_IPSEC)
1.274 seanb 1440: {
1.263 cube 1441: /*
1442: * If the packet is routed over IPsec tunnel, tell the
1443: * originator the tunnel MTU.
1444: * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1445: * XXX quickhack!!!
1446: */
1447:
1.89 itojun 1448: struct secpolicy *sp;
1449: int ipsecerror;
1.95 itojun 1450: size_t ipsechdr;
1.89 itojun 1451: struct route *ro;
1452:
1453: sp = ipsec4_getpolicybyaddr(mcopy,
1.170 itojun 1454: IPSEC_DIR_OUTBOUND, IP_FORWARDING,
1455: &ipsecerror);
1.89 itojun 1456:
1.263 cube 1457: if (sp != NULL) {
1.89 itojun 1458: /* count IPsec header size */
1.95 itojun 1459: ipsechdr = ipsec4_hdrsiz(mcopy,
1.170 itojun 1460: IPSEC_DIR_OUTBOUND, NULL);
1.89 itojun 1461:
1462: /*
1463: * find the correct route for outer IPv4
1464: * header, compute tunnel MTU.
1465: */
1.220 christos 1466:
1.89 itojun 1467: if (sp->req != NULL
1.95 itojun 1468: && sp->req->sav != NULL
1469: && sp->req->sav->sah != NULL) {
1470: ro = &sp->req->sav->sah->sa_route;
1.274 seanb 1471: rt = rtcache_validate(ro);
1.257 dyoung 1472: if (rt && rt->rt_ifp) {
1.220 christos 1473: destmtu =
1.257 dyoung 1474: rt->rt_rmx.rmx_mtu ?
1475: rt->rt_rmx.rmx_mtu :
1476: rt->rt_ifp->if_mtu;
1.220 christos 1477: destmtu -= ipsechdr;
1.89 itojun 1478: }
1479: }
1480:
1.173 jonathan 1481: #ifdef IPSEC
1.89 itojun 1482: key_freesp(sp);
1.173 jonathan 1483: #else
1484: KEY_FREESP(&sp);
1485: #endif
1.89 itojun 1486: }
1.274 seanb 1487: }
1.263 cube 1488: #endif /*defined(IPSEC) || defined(FAST_IPSEC)*/
1.266 thorpej 1489: IP_STATINC(IP_STAT_CANTFRAG);
1.1 cgd 1490: break;
1491:
1492: case ENOBUFS:
1.143 itojun 1493: #if 1
1494: /*
1495: * a router should not generate ICMP_SOURCEQUENCH as
1496: * required in RFC1812 Requirements for IP Version 4 Routers.
1497: * source quench could be a big problem under DoS attacks,
1.149 wiz 1498: * or if the underlying interface is rate-limited.
1.143 itojun 1499: */
1500: if (mcopy)
1501: m_freem(mcopy);
1502: return;
1503: #else
1.1 cgd 1504: type = ICMP_SOURCEQUENCH;
1505: code = 0;
1506: break;
1.143 itojun 1507: #endif
1.1 cgd 1508: }
1.220 christos 1509: icmp_error(mcopy, type, code, dest, destmtu);
1.44 thorpej 1510: }
1511:
1512: void
1.211 perry 1513: ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1514: struct mbuf *m)
1.44 thorpej 1515: {
1516:
1.283 minskim 1517: if (inp->inp_socket->so_options & SO_TIMESTAMP
1.278 christos 1518: #ifdef SO_OTIMESTAMP
1.283 minskim 1519: || inp->inp_socket->so_options & SO_OTIMESTAMP
1.278 christos 1520: #endif
1521: ) {
1.44 thorpej 1522: struct timeval tv;
1523:
1524: microtime(&tv);
1.278 christos 1525: #ifdef SO_OTIMESTAMP
1526: if (inp->inp_socket->so_options & SO_OTIMESTAMP) {
1527: struct timeval50 tv50;
1528: timeval_to_timeval50(&tv, &tv50);
1529: *mp = sbcreatecontrol((void *) &tv50, sizeof(tv50),
1530: SCM_OTIMESTAMP, SOL_SOCKET);
1531: } else
1532: #endif
1.244 christos 1533: *mp = sbcreatecontrol((void *) &tv, sizeof(tv),
1.44 thorpej 1534: SCM_TIMESTAMP, SOL_SOCKET);
1535: if (*mp)
1536: mp = &(*mp)->m_next;
1537: }
1538: if (inp->inp_flags & INP_RECVDSTADDR) {
1.244 christos 1539: *mp = sbcreatecontrol((void *) &ip->ip_dst,
1.44 thorpej 1540: sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1541: if (*mp)
1542: mp = &(*mp)->m_next;
1543: }
1544: #ifdef notyet
1545: /*
1546: * XXX
1547: * Moving these out of udp_input() made them even more broken
1548: * than they already were.
1549: * - fenner@parc.xerox.com
1550: */
1551: /* options were tossed already */
1552: if (inp->inp_flags & INP_RECVOPTS) {
1.244 christos 1553: *mp = sbcreatecontrol((void *) opts_deleted_above,
1.44 thorpej 1554: sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1555: if (*mp)
1556: mp = &(*mp)->m_next;
1557: }
1558: /* ip_srcroute doesn't do what we want here, need to fix */
1559: if (inp->inp_flags & INP_RECVRETOPTS) {
1.244 christos 1560: *mp = sbcreatecontrol((void *) ip_srcroute(),
1.44 thorpej 1561: sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1562: if (*mp)
1563: mp = &(*mp)->m_next;
1564: }
1565: #endif
1566: if (inp->inp_flags & INP_RECVIF) {
1567: struct sockaddr_dl sdl;
1568:
1.252 dyoung 1569: sockaddr_dl_init(&sdl, sizeof(sdl),
1570: (m->m_pkthdr.rcvif != NULL)
1571: ? m->m_pkthdr.rcvif->if_index
1572: : 0,
1573: 0, NULL, 0, NULL, 0);
1.251 dyoung 1574: *mp = sbcreatecontrol(&sdl, sdl.sdl_len, IP_RECVIF, IPPROTO_IP);
1.44 thorpej 1575: if (*mp)
1576: mp = &(*mp)->m_next;
1577: }
1.282 minskim 1578: if (inp->inp_flags & INP_RECVTTL) {
1579: *mp = sbcreatecontrol((void *) &ip->ip_ttl,
1580: sizeof(uint8_t), IP_RECVTTL, IPPROTO_IP);
1581: if (*mp)
1582: mp = &(*mp)->m_next;
1583: }
1.13 mycroft 1584: }
1585:
1.189 atatat 1586: /*
1.228 elad 1587: * sysctl helper routine for net.inet.ip.forwsrcrt.
1588: */
1589: static int
1590: sysctl_net_inet_ip_forwsrcrt(SYSCTLFN_ARGS)
1591: {
1592: int error, tmp;
1593: struct sysctlnode node;
1594:
1595: node = *rnode;
1596: tmp = ip_forwsrcrt;
1597: node.sysctl_data = &tmp;
1598: error = sysctl_lookup(SYSCTLFN_CALL(&node));
1599: if (error || newp == NULL)
1600: return (error);
1601:
1.280 elad 1602: error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FORWSRCRT,
1603: 0, NULL, NULL, NULL);
1604: if (error)
1605: return (error);
1.228 elad 1606:
1607: ip_forwsrcrt = tmp;
1608:
1609: return (0);
1610: }
1611:
1612: /*
1.189 atatat 1613: * sysctl helper routine for net.inet.ip.mtudisctimeout. checks the
1614: * range of the new value and tweaks timers if it changes.
1615: */
1616: static int
1617: sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS)
1.13 mycroft 1618: {
1.189 atatat 1619: int error, tmp;
1620: struct sysctlnode node;
1621:
1622: node = *rnode;
1623: tmp = ip_mtudisc_timeout;
1624: node.sysctl_data = &tmp;
1625: error = sysctl_lookup(SYSCTLFN_CALL(&node));
1626: if (error || newp == NULL)
1627: return (error);
1628: if (tmp < 0)
1629: return (EINVAL);
1.52 thorpej 1630:
1.273 matt 1631: mutex_enter(softnet_lock);
1632:
1.189 atatat 1633: ip_mtudisc_timeout = tmp;
1634: rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout);
1635:
1.273 matt 1636: mutex_exit(softnet_lock);
1637:
1.189 atatat 1638: return (0);
1639: }
1.54 lukem 1640:
1.65 matt 1641: #ifdef GATEWAY
1.189 atatat 1642: /*
1.247 liamjfoy 1643: * sysctl helper routine for net.inet.ip.maxflows.
1.189 atatat 1644: */
1645: static int
1646: sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS)
1647: {
1.273 matt 1648: int error;
1.67 thorpej 1649:
1.273 matt 1650: error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1651: if (error || newp == NULL)
1652: return (error);
1653:
1654: mutex_enter(softnet_lock);
1655: KERNEL_LOCK(1, NULL);
1.212 perry 1656:
1.265 thorpej 1657: ipflow_prune();
1.273 matt 1658:
1659: KERNEL_UNLOCK_ONE(NULL);
1660: mutex_exit(softnet_lock);
1.144 martin 1661:
1.189 atatat 1662: return (0);
1663: }
1.248 liamjfoy 1664:
1665: static int
1666: sysctl_net_inet_ip_hashsize(SYSCTLFN_ARGS)
1.283 minskim 1667: {
1.248 liamjfoy 1668: int error, tmp;
1669: struct sysctlnode node;
1670:
1671: node = *rnode;
1672: tmp = ip_hashsize;
1673: node.sysctl_data = &tmp;
1674: error = sysctl_lookup(SYSCTLFN_CALL(&node));
1675: if (error || newp == NULL)
1676: return (error);
1677:
1678: if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
1679: /*
1680: * Can only fail due to malloc()
1681: */
1.273 matt 1682: mutex_enter(softnet_lock);
1683: KERNEL_LOCK(1, NULL);
1684:
1685: error = ipflow_invalidate_all(tmp);
1686:
1687: KERNEL_UNLOCK_ONE(NULL);
1688: mutex_exit(softnet_lock);
1689:
1.248 liamjfoy 1690: } else {
1691: /*
1692: * EINVAL if not a power of 2
1693: */
1.273 matt 1694: error = EINVAL;
1.283 minskim 1695: }
1.248 liamjfoy 1696:
1.273 matt 1697: return error;
1.248 liamjfoy 1698: }
1.189 atatat 1699: #endif /* GATEWAY */
1.117 tron 1700:
1.266 thorpej 1701: static int
1702: sysctl_net_inet_ip_stats(SYSCTLFN_ARGS)
1703: {
1704:
1.271 thorpej 1705: return (NETSTAT_SYSCTL(ipstat_percpu, IP_NSTATS));
1.266 thorpej 1706: }
1.131 itojun 1707:
1.284 pooka 1708: static void
1709: sysctl_net_inet_ip_setup(struct sysctllog **clog)
1.189 atatat 1710: {
1711: extern int subnetsarelocal, hostzeroisbroadcast;
1.180 jonathan 1712:
1.197 atatat 1713: sysctl_createv(clog, 0, NULL, NULL,
1714: CTLFLAG_PERMANENT,
1.189 atatat 1715: CTLTYPE_NODE, "net", NULL,
1716: NULL, 0, NULL, 0,
1717: CTL_NET, CTL_EOL);
1.197 atatat 1718: sysctl_createv(clog, 0, NULL, NULL,
1719: CTLFLAG_PERMANENT,
1.203 atatat 1720: CTLTYPE_NODE, "inet",
1721: SYSCTL_DESCR("PF_INET related settings"),
1.189 atatat 1722: NULL, 0, NULL, 0,
1723: CTL_NET, PF_INET, CTL_EOL);
1.197 atatat 1724: sysctl_createv(clog, 0, NULL, NULL,
1725: CTLFLAG_PERMANENT,
1.203 atatat 1726: CTLTYPE_NODE, "ip",
1727: SYSCTL_DESCR("IPv4 related settings"),
1.189 atatat 1728: NULL, 0, NULL, 0,
1729: CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
1.212 perry 1730:
1.197 atatat 1731: sysctl_createv(clog, 0, NULL, NULL,
1732: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1733: CTLTYPE_INT, "forwarding",
1734: SYSCTL_DESCR("Enable forwarding of INET datagrams"),
1.189 atatat 1735: NULL, 0, &ipforwarding, 0,
1736: CTL_NET, PF_INET, IPPROTO_IP,
1737: IPCTL_FORWARDING, CTL_EOL);
1.197 atatat 1738: sysctl_createv(clog, 0, NULL, NULL,
1739: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1740: CTLTYPE_INT, "redirect",
1741: SYSCTL_DESCR("Enable sending of ICMP redirect messages"),
1.189 atatat 1742: NULL, 0, &ipsendredirects, 0,
1743: CTL_NET, PF_INET, IPPROTO_IP,
1744: IPCTL_SENDREDIRECTS, CTL_EOL);
1.197 atatat 1745: sysctl_createv(clog, 0, NULL, NULL,
1746: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1747: CTLTYPE_INT, "ttl",
1748: SYSCTL_DESCR("Default TTL for an INET datagram"),
1.189 atatat 1749: NULL, 0, &ip_defttl, 0,
1750: CTL_NET, PF_INET, IPPROTO_IP,
1751: IPCTL_DEFTTL, CTL_EOL);
1752: #ifdef IPCTL_DEFMTU
1.197 atatat 1753: sysctl_createv(clog, 0, NULL, NULL,
1754: CTLFLAG_PERMANENT /* |CTLFLAG_READWRITE? */,
1.203 atatat 1755: CTLTYPE_INT, "mtu",
1756: SYSCTL_DESCR("Default MTA for an INET route"),
1.189 atatat 1757: NULL, 0, &ip_mtu, 0,
1758: CTL_NET, PF_INET, IPPROTO_IP,
1759: IPCTL_DEFMTU, CTL_EOL);
1760: #endif /* IPCTL_DEFMTU */
1.197 atatat 1761: sysctl_createv(clog, 0, NULL, NULL,
1.228 elad 1762: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1763: CTLTYPE_INT, "forwsrcrt",
1764: SYSCTL_DESCR("Enable forwarding of source-routed "
1765: "datagrams"),
1.228 elad 1766: sysctl_net_inet_ip_forwsrcrt, 0, &ip_forwsrcrt, 0,
1.189 atatat 1767: CTL_NET, PF_INET, IPPROTO_IP,
1768: IPCTL_FORWSRCRT, CTL_EOL);
1.197 atatat 1769: sysctl_createv(clog, 0, NULL, NULL,
1770: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1771: CTLTYPE_INT, "directed-broadcast",
1772: SYSCTL_DESCR("Enable forwarding of broadcast datagrams"),
1.189 atatat 1773: NULL, 0, &ip_directedbcast, 0,
1774: CTL_NET, PF_INET, IPPROTO_IP,
1775: IPCTL_DIRECTEDBCAST, CTL_EOL);
1.197 atatat 1776: sysctl_createv(clog, 0, NULL, NULL,
1777: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1778: CTLTYPE_INT, "allowsrcrt",
1779: SYSCTL_DESCR("Accept source-routed datagrams"),
1.189 atatat 1780: NULL, 0, &ip_allowsrcrt, 0,
1781: CTL_NET, PF_INET, IPPROTO_IP,
1782: IPCTL_ALLOWSRCRT, CTL_EOL);
1.197 atatat 1783: sysctl_createv(clog, 0, NULL, NULL,
1784: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1785: CTLTYPE_INT, "subnetsarelocal",
1786: SYSCTL_DESCR("Whether logical subnets are considered "
1787: "local"),
1.189 atatat 1788: NULL, 0, &subnetsarelocal, 0,
1789: CTL_NET, PF_INET, IPPROTO_IP,
1790: IPCTL_SUBNETSARELOCAL, CTL_EOL);
1.197 atatat 1791: sysctl_createv(clog, 0, NULL, NULL,
1792: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1793: CTLTYPE_INT, "mtudisc",
1794: SYSCTL_DESCR("Use RFC1191 Path MTU Discovery"),
1.189 atatat 1795: NULL, 0, &ip_mtudisc, 0,
1796: CTL_NET, PF_INET, IPPROTO_IP,
1797: IPCTL_MTUDISC, CTL_EOL);
1.197 atatat 1798: sysctl_createv(clog, 0, NULL, NULL,
1799: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1800: CTLTYPE_INT, "anonportmin",
1801: SYSCTL_DESCR("Lowest ephemeral port number to assign"),
1.189 atatat 1802: sysctl_net_inet_ip_ports, 0, &anonportmin, 0,
1803: CTL_NET, PF_INET, IPPROTO_IP,
1804: IPCTL_ANONPORTMIN, CTL_EOL);
1.197 atatat 1805: sysctl_createv(clog, 0, NULL, NULL,
1806: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1807: CTLTYPE_INT, "anonportmax",
1808: SYSCTL_DESCR("Highest ephemeral port number to assign"),
1.189 atatat 1809: sysctl_net_inet_ip_ports, 0, &anonportmax, 0,
1810: CTL_NET, PF_INET, IPPROTO_IP,
1811: IPCTL_ANONPORTMAX, CTL_EOL);
1.197 atatat 1812: sysctl_createv(clog, 0, NULL, NULL,
1813: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1814: CTLTYPE_INT, "mtudisctimeout",
1815: SYSCTL_DESCR("Lifetime of a Path MTU Discovered route"),
1.189 atatat 1816: sysctl_net_inet_ip_pmtudto, 0, &ip_mtudisc_timeout, 0,
1817: CTL_NET, PF_INET, IPPROTO_IP,
1818: IPCTL_MTUDISCTIMEOUT, CTL_EOL);
1819: #ifdef GATEWAY
1.197 atatat 1820: sysctl_createv(clog, 0, NULL, NULL,
1821: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1822: CTLTYPE_INT, "maxflows",
1823: SYSCTL_DESCR("Number of flows for fast forwarding"),
1.189 atatat 1824: sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0,
1825: CTL_NET, PF_INET, IPPROTO_IP,
1826: IPCTL_MAXFLOWS, CTL_EOL);
1.248 liamjfoy 1827: sysctl_createv(clog, 0, NULL, NULL,
1828: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1829: CTLTYPE_INT, "hashsize",
1830: SYSCTL_DESCR("Size of hash table for fast forwarding (IPv4)"),
1831: sysctl_net_inet_ip_hashsize, 0, &ip_hashsize, 0,
1832: CTL_NET, PF_INET, IPPROTO_IP,
1833: CTL_CREATE, CTL_EOL);
1.189 atatat 1834: #endif /* GATEWAY */
1.197 atatat 1835: sysctl_createv(clog, 0, NULL, NULL,
1836: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1837: CTLTYPE_INT, "hostzerobroadcast",
1838: SYSCTL_DESCR("All zeroes address is broadcast address"),
1.189 atatat 1839: NULL, 0, &hostzeroisbroadcast, 0,
1840: CTL_NET, PF_INET, IPPROTO_IP,
1841: IPCTL_HOSTZEROBROADCAST, CTL_EOL);
1842: #if NGIF > 0
1.197 atatat 1843: sysctl_createv(clog, 0, NULL, NULL,
1844: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1845: CTLTYPE_INT, "gifttl",
1846: SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
1.189 atatat 1847: NULL, 0, &ip_gif_ttl, 0,
1848: CTL_NET, PF_INET, IPPROTO_IP,
1849: IPCTL_GIF_TTL, CTL_EOL);
1850: #endif /* NGIF */
1851: #ifndef IPNOPRIVPORTS
1.197 atatat 1852: sysctl_createv(clog, 0, NULL, NULL,
1853: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1854: CTLTYPE_INT, "lowportmin",
1855: SYSCTL_DESCR("Lowest privileged ephemeral port number "
1856: "to assign"),
1.189 atatat 1857: sysctl_net_inet_ip_ports, 0, &lowportmin, 0,
1858: CTL_NET, PF_INET, IPPROTO_IP,
1859: IPCTL_LOWPORTMIN, CTL_EOL);
1.197 atatat 1860: sysctl_createv(clog, 0, NULL, NULL,
1861: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1862: CTLTYPE_INT, "lowportmax",
1863: SYSCTL_DESCR("Highest privileged ephemeral port number "
1864: "to assign"),
1.189 atatat 1865: sysctl_net_inet_ip_ports, 0, &lowportmax, 0,
1866: CTL_NET, PF_INET, IPPROTO_IP,
1867: IPCTL_LOWPORTMAX, CTL_EOL);
1868: #endif /* IPNOPRIVPORTS */
1869: #if NGRE > 0
1.197 atatat 1870: sysctl_createv(clog, 0, NULL, NULL,
1871: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1872: CTLTYPE_INT, "grettl",
1873: SYSCTL_DESCR("Default TTL for a gre tunnel datagram"),
1.189 atatat 1874: NULL, 0, &ip_gre_ttl, 0,
1875: CTL_NET, PF_INET, IPPROTO_IP,
1876: IPCTL_GRE_TTL, CTL_EOL);
1877: #endif /* NGRE */
1.197 atatat 1878: sysctl_createv(clog, 0, NULL, NULL,
1879: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1880: CTLTYPE_INT, "checkinterface",
1881: SYSCTL_DESCR("Enable receive side of Strong ES model "
1882: "from RFC1122"),
1.189 atatat 1883: NULL, 0, &ip_checkinterface, 0,
1884: CTL_NET, PF_INET, IPPROTO_IP,
1885: IPCTL_CHECKINTERFACE, CTL_EOL);
1.197 atatat 1886: sysctl_createv(clog, 0, NULL, NULL,
1887: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203 atatat 1888: CTLTYPE_INT, "random_id",
1889: SYSCTL_DESCR("Assign random ip_id values"),
1.189 atatat 1890: NULL, 0, &ip_do_randomid, 0,
1891: CTL_NET, PF_INET, IPPROTO_IP,
1892: IPCTL_RANDOMID, CTL_EOL);
1.206 thorpej 1893: sysctl_createv(clog, 0, NULL, NULL,
1894: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1895: CTLTYPE_INT, "do_loopback_cksum",
1896: SYSCTL_DESCR("Perform IP checksum on loopback"),
1897: NULL, 0, &ip_do_loopback_cksum, 0,
1898: CTL_NET, PF_INET, IPPROTO_IP,
1899: IPCTL_LOOPBACKCKSUM, CTL_EOL);
1.219 elad 1900: sysctl_createv(clog, 0, NULL, NULL,
1901: CTLFLAG_PERMANENT,
1902: CTLTYPE_STRUCT, "stats",
1903: SYSCTL_DESCR("IP statistics"),
1.266 thorpej 1904: sysctl_net_inet_ip_stats, 0, NULL, 0,
1.219 elad 1905: CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS,
1906: CTL_EOL);
1.1 cgd 1907: }
1.266 thorpej 1908:
1909: void
1910: ip_statinc(u_int stat)
1911: {
1912:
1913: KASSERT(stat < IP_NSTATS);
1914: IP_STATINC(stat);
1915: }
CVSweb <webmaster@jp.NetBSD.org>