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