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