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