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