Annotation of src/sys/netinet/ip_input.c, Revision 1.82.2.1
1.82.2.1! proff 1: /* $NetBSD: ip_input.c,v 1.83 1999/04/07 02:31:05 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);
1.82.2.1! proff 423: if (ia != NULL) {
! 424: if (ia->ia_ifp->if_flags & IFF_UP)
! 425: goto ours;
! 426: }
1.57 tls 427: if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
428: for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
429: ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
430: if (ifa->ifa_addr->sa_family != AF_INET) continue;
431: ia = ifatoia(ifa);
1.35 mycroft 432: if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
433: in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
1.20 mycroft 434: /*
435: * Look for all-0's host part (old broadcast addr),
436: * either for subnet or net.
437: */
438: ip->ip_dst.s_addr == ia->ia_subnet ||
1.18 mycroft 439: ip->ip_dst.s_addr == ia->ia_net)
1.1 cgd 440: goto ours;
1.57 tls 441: /*
442: * An interface with IP address zero accepts
443: * all packets that arrive on that interface.
444: */
445: if (in_nullhost(ia->ia_addr.sin_addr))
446: goto ours;
1.1 cgd 447: }
448: }
1.18 mycroft 449: if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4 hpeyerl 450: struct in_multi *inm;
451: #ifdef MROUTING
452: extern struct socket *ip_mrouter;
1.10 brezak 453:
454: if (m->m_flags & M_EXT) {
455: if ((m = m_pullup(m, hlen)) == 0) {
456: ipstat.ips_toosmall++;
457: goto next;
458: }
459: ip = mtod(m, struct ip *);
460: }
1.4 hpeyerl 461:
462: if (ip_mrouter) {
463: /*
464: * If we are acting as a multicast router, all
465: * incoming multicast packets are passed to the
466: * kernel-level multicast forwarding function.
467: * The packet is returned (relatively) intact; if
468: * ip_mforward() returns a non-zero value, the packet
469: * must be discarded, else it may be accepted below.
470: *
471: * (The IP ident field is put in the same byte order
472: * as expected when ip_mforward() is called from
473: * ip_output().)
474: */
1.13 mycroft 475: if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
476: ipstat.ips_cantforward++;
1.4 hpeyerl 477: m_freem(m);
478: goto next;
479: }
480:
481: /*
482: * The process-level routing demon needs to receive
483: * all multicast IGMP packets, whether or not this
484: * host belongs to their destination groups.
485: */
486: if (ip->ip_p == IPPROTO_IGMP)
487: goto ours;
1.13 mycroft 488: ipstat.ips_forward++;
1.4 hpeyerl 489: }
490: #endif
491: /*
492: * See if we belong to the destination multicast group on the
493: * arrival interface.
494: */
495: IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
496: if (inm == NULL) {
1.13 mycroft 497: ipstat.ips_cantforward++;
1.4 hpeyerl 498: m_freem(m);
499: goto next;
500: }
501: goto ours;
502: }
1.19 mycroft 503: if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
1.35 mycroft 504: in_nullhost(ip->ip_dst))
1.1 cgd 505: goto ours;
506:
507: /*
508: * Not for us; forward if possible and desirable.
509: */
510: if (ipforwarding == 0) {
511: ipstat.ips_cantforward++;
512: m_freem(m);
513: } else
514: ip_forward(m, 0);
515: goto next;
516:
517: ours:
518: /*
519: * If offset or IP_MF are set, must reassemble.
520: * Otherwise, nothing need be done.
521: * (We could look in the reassembly queue to see
522: * if the packet was previously fragmented,
523: * but it's not worth the time; just let them time out.)
524: */
1.37 perry 525: if (ip->ip_off & ~(IP_DF|IP_RF)) {
1.1 cgd 526: /*
527: * Look for queue of fragments
528: * of this datagram.
529: */
1.75 thorpej 530: IPQ_LOCK();
1.25 cgd 531: for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
1.1 cgd 532: if (ip->ip_id == fp->ipq_id &&
1.35 mycroft 533: in_hosteq(ip->ip_src, fp->ipq_src) &&
534: in_hosteq(ip->ip_dst, fp->ipq_dst) &&
1.1 cgd 535: ip->ip_p == fp->ipq_p)
536: goto found;
537: fp = 0;
538: found:
539:
540: /*
541: * Adjust ip_len to not reflect header,
1.25 cgd 542: * set ipqe_mff if more fragments are expected,
1.1 cgd 543: * convert offset of this to bytes.
544: */
545: ip->ip_len -= hlen;
1.25 cgd 546: mff = (ip->ip_off & IP_MF) != 0;
547: if (mff) {
1.16 cgd 548: /*
549: * Make sure that fragments have a data length
550: * that's a non-zero multiple of 8 bytes.
551: */
1.17 cgd 552: if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
1.16 cgd 553: ipstat.ips_badfrags++;
1.75 thorpej 554: IPQ_UNLOCK();
1.16 cgd 555: goto bad;
556: }
557: }
1.1 cgd 558: ip->ip_off <<= 3;
559:
560: /*
561: * If datagram marked as having more fragments
562: * or if this is not the first fragment,
563: * attempt reassembly; if it succeeds, proceed.
564: */
1.25 cgd 565: if (mff || ip->ip_off) {
1.1 cgd 566: ipstat.ips_fragments++;
1.72 thorpej 567: ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
1.25 cgd 568: if (ipqe == NULL) {
569: ipstat.ips_rcvmemdrop++;
1.75 thorpej 570: IPQ_UNLOCK();
1.25 cgd 571: goto bad;
572: }
573: ipqe->ipqe_mff = mff;
1.50 thorpej 574: ipqe->ipqe_m = m;
1.25 cgd 575: ipqe->ipqe_ip = ip;
1.50 thorpej 576: m = ip_reass(ipqe, fp);
1.75 thorpej 577: if (m == 0) {
578: IPQ_UNLOCK();
1.1 cgd 579: goto next;
1.75 thorpej 580: }
1.13 mycroft 581: ipstat.ips_reassembled++;
1.50 thorpej 582: ip = mtod(m, struct ip *);
1.74 thorpej 583: hlen = ip->ip_hl << 2;
1.79 mycroft 584: ip->ip_len += hlen;
1.1 cgd 585: } else
586: if (fp)
587: ip_freef(fp);
1.75 thorpej 588: IPQ_UNLOCK();
1.79 mycroft 589: }
1.1 cgd 590:
591: /*
592: * Switch out to protocol's input routine.
593: */
1.82 aidan 594: #if IFA_STATS
595: ia->ia_ifa.ifa_data.ifad_inbytes += ip->ip_len;
596: #endif
1.1 cgd 597: ipstat.ips_delivered++;
598: (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
599: goto next;
600: bad:
601: m_freem(m);
602: goto next;
603: }
604:
605: /*
606: * Take incoming datagram fragment and try to
607: * reassemble it into whole datagram. If a chain for
608: * reassembly of this datagram already exists, then it
609: * is given as fp; otherwise have to make a chain.
610: */
1.50 thorpej 611: struct mbuf *
1.25 cgd 612: ip_reass(ipqe, fp)
613: register struct ipqent *ipqe;
1.1 cgd 614: register struct ipq *fp;
615: {
1.50 thorpej 616: register struct mbuf *m = ipqe->ipqe_m;
1.25 cgd 617: register struct ipqent *nq, *p, *q;
618: struct ip *ip;
1.1 cgd 619: struct mbuf *t;
1.25 cgd 620: int hlen = ipqe->ipqe_ip->ip_hl << 2;
1.1 cgd 621: int i, next;
622:
1.75 thorpej 623: IPQ_LOCK_CHECK();
624:
1.1 cgd 625: /*
626: * Presence of header sizes in mbufs
627: * would confuse code below.
628: */
629: m->m_data += hlen;
630: m->m_len -= hlen;
631:
632: /*
633: * If first fragment to arrive, create a reassembly queue.
634: */
635: if (fp == 0) {
1.50 thorpej 636: MALLOC(fp, struct ipq *, sizeof (struct ipq),
637: M_FTABLE, M_NOWAIT);
638: if (fp == NULL)
1.1 cgd 639: goto dropfrag;
1.25 cgd 640: LIST_INSERT_HEAD(&ipq, fp, ipq_q);
1.1 cgd 641: fp->ipq_ttl = IPFRAGTTL;
1.25 cgd 642: fp->ipq_p = ipqe->ipqe_ip->ip_p;
643: fp->ipq_id = ipqe->ipqe_ip->ip_id;
644: LIST_INIT(&fp->ipq_fragq);
645: fp->ipq_src = ipqe->ipqe_ip->ip_src;
646: fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
647: p = NULL;
1.1 cgd 648: goto insert;
649: }
650:
651: /*
652: * Find a segment which begins after this one does.
653: */
1.25 cgd 654: for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
655: p = q, q = q->ipqe_q.le_next)
656: if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
1.1 cgd 657: break;
658:
659: /*
660: * If there is a preceding segment, it may provide some of
661: * our data already. If so, drop the data from the incoming
662: * segment. If it provides all of our data, drop us.
663: */
1.25 cgd 664: if (p != NULL) {
665: i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
666: ipqe->ipqe_ip->ip_off;
1.1 cgd 667: if (i > 0) {
1.25 cgd 668: if (i >= ipqe->ipqe_ip->ip_len)
1.1 cgd 669: goto dropfrag;
1.50 thorpej 670: m_adj(ipqe->ipqe_m, i);
1.25 cgd 671: ipqe->ipqe_ip->ip_off += i;
672: ipqe->ipqe_ip->ip_len -= i;
1.1 cgd 673: }
674: }
675:
676: /*
677: * While we overlap succeeding segments trim them or,
678: * if they are completely covered, dequeue them.
679: */
1.25 cgd 680: for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
681: q->ipqe_ip->ip_off; q = nq) {
682: i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
683: q->ipqe_ip->ip_off;
684: if (i < q->ipqe_ip->ip_len) {
685: q->ipqe_ip->ip_len -= i;
686: q->ipqe_ip->ip_off += i;
1.50 thorpej 687: m_adj(q->ipqe_m, i);
1.1 cgd 688: break;
689: }
1.25 cgd 690: nq = q->ipqe_q.le_next;
1.50 thorpej 691: m_freem(q->ipqe_m);
1.25 cgd 692: LIST_REMOVE(q, ipqe_q);
1.72 thorpej 693: pool_put(&ipqent_pool, q);
1.1 cgd 694: }
695:
696: insert:
697: /*
698: * Stick new segment in its place;
699: * check for complete reassembly.
700: */
1.25 cgd 701: if (p == NULL) {
702: LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
703: } else {
704: LIST_INSERT_AFTER(p, ipqe, ipqe_q);
705: }
1.1 cgd 706: next = 0;
1.25 cgd 707: for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
708: p = q, q = q->ipqe_q.le_next) {
709: if (q->ipqe_ip->ip_off != next)
1.1 cgd 710: return (0);
1.25 cgd 711: next += q->ipqe_ip->ip_len;
1.1 cgd 712: }
1.25 cgd 713: if (p->ipqe_mff)
1.1 cgd 714: return (0);
715:
716: /*
1.41 thorpej 717: * Reassembly is complete. Check for a bogus message size and
718: * concatenate fragments.
1.1 cgd 719: */
1.25 cgd 720: q = fp->ipq_fragq.lh_first;
721: ip = q->ipqe_ip;
1.41 thorpej 722: if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
723: ipstat.ips_toolong++;
724: ip_freef(fp);
725: return (0);
726: }
1.50 thorpej 727: m = q->ipqe_m;
1.1 cgd 728: t = m->m_next;
729: m->m_next = 0;
730: m_cat(m, t);
1.25 cgd 731: nq = q->ipqe_q.le_next;
1.72 thorpej 732: pool_put(&ipqent_pool, q);
1.25 cgd 733: for (q = nq; q != NULL; q = nq) {
1.50 thorpej 734: t = q->ipqe_m;
1.25 cgd 735: nq = q->ipqe_q.le_next;
1.72 thorpej 736: pool_put(&ipqent_pool, q);
1.1 cgd 737: m_cat(m, t);
738: }
739:
740: /*
741: * Create header for new ip packet by
742: * modifying header of first packet;
743: * dequeue and discard fragment reassembly header.
744: * Make header visible.
745: */
746: ip->ip_len = next;
1.25 cgd 747: ip->ip_src = fp->ipq_src;
748: ip->ip_dst = fp->ipq_dst;
749: LIST_REMOVE(fp, ipq_q);
1.50 thorpej 750: FREE(fp, M_FTABLE);
1.1 cgd 751: m->m_len += (ip->ip_hl << 2);
752: m->m_data -= (ip->ip_hl << 2);
753: /* some debugging cruft by sklower, below, will go away soon */
754: if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
755: register int plen = 0;
1.50 thorpej 756: for (t = m; t; t = t->m_next)
757: plen += t->m_len;
758: m->m_pkthdr.len = plen;
1.1 cgd 759: }
1.50 thorpej 760: return (m);
1.1 cgd 761:
762: dropfrag:
763: ipstat.ips_fragdropped++;
764: m_freem(m);
1.72 thorpej 765: pool_put(&ipqent_pool, ipqe);
1.1 cgd 766: return (0);
767: }
768:
769: /*
770: * Free a fragment reassembly header and all
771: * associated datagrams.
772: */
1.8 mycroft 773: void
1.1 cgd 774: ip_freef(fp)
775: struct ipq *fp;
776: {
1.25 cgd 777: register struct ipqent *q, *p;
1.1 cgd 778:
1.75 thorpej 779: IPQ_LOCK_CHECK();
780:
1.25 cgd 781: for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
782: p = q->ipqe_q.le_next;
1.50 thorpej 783: m_freem(q->ipqe_m);
1.25 cgd 784: LIST_REMOVE(q, ipqe_q);
1.72 thorpej 785: pool_put(&ipqent_pool, q);
1.1 cgd 786: }
1.25 cgd 787: LIST_REMOVE(fp, ipq_q);
1.50 thorpej 788: FREE(fp, M_FTABLE);
1.1 cgd 789: }
790:
791: /*
792: * IP timer processing;
793: * if a timer expires on a reassembly
794: * queue, discard it.
795: */
1.8 mycroft 796: void
1.1 cgd 797: ip_slowtimo()
798: {
1.25 cgd 799: register struct ipq *fp, *nfp;
1.24 mycroft 800: int s = splsoftnet();
1.1 cgd 801:
1.75 thorpej 802: IPQ_LOCK();
1.25 cgd 803: for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
804: nfp = fp->ipq_q.le_next;
805: if (--fp->ipq_ttl == 0) {
1.1 cgd 806: ipstat.ips_fragtimeout++;
1.25 cgd 807: ip_freef(fp);
1.1 cgd 808: }
809: }
1.75 thorpej 810: IPQ_UNLOCK();
1.63 matt 811: #ifdef GATEWAY
812: ipflow_slowtimo();
813: #endif
1.1 cgd 814: splx(s);
815: }
816:
817: /*
818: * Drain off all datagram fragments.
819: */
1.8 mycroft 820: void
1.1 cgd 821: ip_drain()
822: {
823:
1.75 thorpej 824: /*
825: * We may be called from a device's interrupt context. If
826: * the ipq is already busy, just bail out now.
827: */
828: if (ipq_lock_try() == 0)
829: return;
830:
1.25 cgd 831: while (ipq.lh_first != NULL) {
1.1 cgd 832: ipstat.ips_fragdropped++;
1.25 cgd 833: ip_freef(ipq.lh_first);
1.1 cgd 834: }
1.75 thorpej 835:
836: IPQ_UNLOCK();
1.1 cgd 837: }
838:
839: /*
840: * Do option processing on a datagram,
841: * possibly discarding it if bad options are encountered,
842: * or forwarding it if source-routed.
843: * Returns 1 if packet has been forwarded/freed,
844: * 0 if the packet should be processed further.
845: */
1.8 mycroft 846: int
1.1 cgd 847: ip_dooptions(m)
848: struct mbuf *m;
849: {
850: register struct ip *ip = mtod(m, struct ip *);
851: register u_char *cp;
852: register struct ip_timestamp *ipt;
853: register struct in_ifaddr *ia;
854: int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1.13 mycroft 855: struct in_addr *sin, dst;
1.1 cgd 856: n_time ntime;
857:
1.13 mycroft 858: dst = ip->ip_dst;
1.1 cgd 859: cp = (u_char *)(ip + 1);
860: cnt = (ip->ip_hl << 2) - sizeof (struct ip);
861: for (; cnt > 0; cnt -= optlen, cp += optlen) {
862: opt = cp[IPOPT_OPTVAL];
863: if (opt == IPOPT_EOL)
864: break;
865: if (opt == IPOPT_NOP)
866: optlen = 1;
867: else {
868: optlen = cp[IPOPT_OLEN];
869: if (optlen <= 0 || optlen > cnt) {
870: code = &cp[IPOPT_OLEN] - (u_char *)ip;
871: goto bad;
872: }
873: }
874: switch (opt) {
875:
876: default:
877: break;
878:
879: /*
880: * Source routing with record.
881: * Find interface with current destination address.
882: * If none on this machine then drop if strictly routed,
883: * or do nothing if loosely routed.
884: * Record interface address and bring up next address
885: * component. If strictly routed make sure next
886: * address is on directly accessible net.
887: */
888: case IPOPT_LSRR:
889: case IPOPT_SSRR:
1.47 cjs 890: if (ip_allowsrcrt == 0) {
891: type = ICMP_UNREACH;
892: code = ICMP_UNREACH_NET_PROHIB;
893: goto bad;
894: }
1.1 cgd 895: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
896: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
897: goto bad;
898: }
899: ipaddr.sin_addr = ip->ip_dst;
1.19 mycroft 900: ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1.1 cgd 901: if (ia == 0) {
902: if (opt == IPOPT_SSRR) {
903: type = ICMP_UNREACH;
904: code = ICMP_UNREACH_SRCFAIL;
905: goto bad;
906: }
907: /*
908: * Loose routing, and not at next destination
909: * yet; nothing to do except forward.
910: */
911: break;
912: }
913: off--; /* 0 origin */
914: if (off > optlen - sizeof(struct in_addr)) {
915: /*
916: * End of source route. Should be for us.
917: */
918: save_rte(cp, ip->ip_src);
919: break;
920: }
921: /*
922: * locate outgoing interface
923: */
924: bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
925: sizeof(ipaddr.sin_addr));
926: if (opt == IPOPT_SSRR) {
927: #define INA struct in_ifaddr *
928: #define SA struct sockaddr *
1.29 mrg 929: ia = (INA)ifa_ifwithladdr((SA)&ipaddr);
1.1 cgd 930: } else
931: ia = ip_rtaddr(ipaddr.sin_addr);
932: if (ia == 0) {
933: type = ICMP_UNREACH;
934: code = ICMP_UNREACH_SRCFAIL;
935: goto bad;
936: }
937: ip->ip_dst = ipaddr.sin_addr;
1.20 mycroft 938: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 939: (caddr_t)(cp + off), sizeof(struct in_addr));
940: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13 mycroft 941: /*
942: * Let ip_intr's mcast routing check handle mcast pkts
943: */
1.18 mycroft 944: forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1 cgd 945: break;
946:
947: case IPOPT_RR:
948: if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
949: code = &cp[IPOPT_OFFSET] - (u_char *)ip;
950: goto bad;
951: }
952: /*
953: * If no space remains, ignore.
954: */
955: off--; /* 0 origin */
956: if (off > optlen - sizeof(struct in_addr))
957: break;
958: bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
959: sizeof(ipaddr.sin_addr));
960: /*
961: * locate outgoing interface; if we're the destination,
962: * use the incoming interface (should be same).
963: */
964: if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
965: (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
966: type = ICMP_UNREACH;
967: code = ICMP_UNREACH_HOST;
968: goto bad;
969: }
1.20 mycroft 970: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 971: (caddr_t)(cp + off), sizeof(struct in_addr));
972: cp[IPOPT_OFFSET] += sizeof(struct in_addr);
973: break;
974:
975: case IPOPT_TS:
976: code = cp - (u_char *)ip;
977: ipt = (struct ip_timestamp *)cp;
978: if (ipt->ipt_len < 5)
979: goto bad;
1.15 cgd 980: if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.1 cgd 981: if (++ipt->ipt_oflw == 0)
982: goto bad;
983: break;
984: }
985: sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
986: switch (ipt->ipt_flg) {
987:
988: case IPOPT_TS_TSONLY:
989: break;
990:
991: case IPOPT_TS_TSANDADDR:
1.66 thorpej 992: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.1 cgd 993: sizeof(struct in_addr) > ipt->ipt_len)
994: goto bad;
1.13 mycroft 995: ipaddr.sin_addr = dst;
996: ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
997: m->m_pkthdr.rcvif);
998: if (ia == 0)
999: continue;
1.20 mycroft 1000: bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1 cgd 1001: (caddr_t)sin, sizeof(struct in_addr));
1002: ipt->ipt_ptr += sizeof(struct in_addr);
1003: break;
1004:
1005: case IPOPT_TS_PRESPEC:
1.66 thorpej 1006: if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.1 cgd 1007: sizeof(struct in_addr) > ipt->ipt_len)
1008: goto bad;
1009: bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
1010: sizeof(struct in_addr));
1011: if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1012: continue;
1013: ipt->ipt_ptr += sizeof(struct in_addr);
1014: break;
1015:
1016: default:
1017: goto bad;
1018: }
1019: ntime = iptime();
1020: bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
1021: sizeof(n_time));
1022: ipt->ipt_ptr += sizeof(n_time);
1023: }
1024: }
1025: if (forward) {
1.26 thorpej 1026: if (ip_forwsrcrt == 0) {
1027: type = ICMP_UNREACH;
1028: code = ICMP_UNREACH_SRCFAIL;
1029: goto bad;
1030: }
1.1 cgd 1031: ip_forward(m, 1);
1032: return (1);
1.13 mycroft 1033: }
1034: return (0);
1.1 cgd 1035: bad:
1.13 mycroft 1036: icmp_error(m, type, code, 0, 0);
1037: ipstat.ips_badoptions++;
1.1 cgd 1038: return (1);
1039: }
1040:
1041: /*
1042: * Given address of next destination (final or next hop),
1043: * return internet address info of interface to be used to get there.
1044: */
1045: struct in_ifaddr *
1046: ip_rtaddr(dst)
1047: struct in_addr dst;
1048: {
1049: register struct sockaddr_in *sin;
1050:
1.19 mycroft 1051: sin = satosin(&ipforward_rt.ro_dst);
1.1 cgd 1052:
1.35 mycroft 1053: if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
1.1 cgd 1054: if (ipforward_rt.ro_rt) {
1055: RTFREE(ipforward_rt.ro_rt);
1056: ipforward_rt.ro_rt = 0;
1057: }
1058: sin->sin_family = AF_INET;
1059: sin->sin_len = sizeof(*sin);
1060: sin->sin_addr = dst;
1061:
1062: rtalloc(&ipforward_rt);
1063: }
1064: if (ipforward_rt.ro_rt == 0)
1065: return ((struct in_ifaddr *)0);
1.19 mycroft 1066: return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1.1 cgd 1067: }
1068:
1069: /*
1070: * Save incoming source route for use in replies,
1071: * to be picked up later by ip_srcroute if the receiver is interested.
1072: */
1.13 mycroft 1073: void
1.1 cgd 1074: save_rte(option, dst)
1075: u_char *option;
1076: struct in_addr dst;
1077: {
1078: unsigned olen;
1079:
1080: olen = option[IPOPT_OLEN];
1081: #ifdef DIAGNOSTIC
1082: if (ipprintfs)
1.39 christos 1083: printf("save_rte: olen %d\n", olen);
1.1 cgd 1084: #endif
1085: if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1086: return;
1087: bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
1088: ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1089: ip_srcrt.dst = dst;
1090: }
1091:
1092: /*
1093: * Retrieve incoming source route for use in replies,
1094: * in the same form used by setsockopt.
1095: * The first hop is placed before the options, will be removed later.
1096: */
1097: struct mbuf *
1098: ip_srcroute()
1099: {
1100: register struct in_addr *p, *q;
1101: register struct mbuf *m;
1102:
1103: if (ip_nhops == 0)
1104: return ((struct mbuf *)0);
1105: m = m_get(M_DONTWAIT, MT_SOOPTS);
1106: if (m == 0)
1107: return ((struct mbuf *)0);
1108:
1.13 mycroft 1109: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1 cgd 1110:
1111: /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1112: m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1113: OPTSIZ;
1114: #ifdef DIAGNOSTIC
1115: if (ipprintfs)
1.39 christos 1116: printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1.1 cgd 1117: #endif
1118:
1119: /*
1120: * First save first hop for return route
1121: */
1122: p = &ip_srcrt.route[ip_nhops - 1];
1123: *(mtod(m, struct in_addr *)) = *p--;
1124: #ifdef DIAGNOSTIC
1125: if (ipprintfs)
1.39 christos 1126: printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1.1 cgd 1127: #endif
1128:
1129: /*
1130: * Copy option fields and padding (nop) to mbuf.
1131: */
1132: ip_srcrt.nop = IPOPT_NOP;
1133: ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1134: bcopy((caddr_t)&ip_srcrt.nop,
1135: mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
1136: q = (struct in_addr *)(mtod(m, caddr_t) +
1137: sizeof(struct in_addr) + OPTSIZ);
1138: #undef OPTSIZ
1139: /*
1140: * Record return path as an IP source route,
1141: * reversing the path (pointers are now aligned).
1142: */
1143: while (p >= ip_srcrt.route) {
1144: #ifdef DIAGNOSTIC
1145: if (ipprintfs)
1.39 christos 1146: printf(" %x", ntohl(q->s_addr));
1.1 cgd 1147: #endif
1148: *q++ = *p--;
1149: }
1150: /*
1151: * Last hop goes to final destination.
1152: */
1153: *q = ip_srcrt.dst;
1154: #ifdef DIAGNOSTIC
1155: if (ipprintfs)
1.39 christos 1156: printf(" %x\n", ntohl(q->s_addr));
1.1 cgd 1157: #endif
1158: return (m);
1159: }
1160:
1161: /*
1162: * Strip out IP options, at higher
1163: * level protocol in the kernel.
1164: * Second argument is buffer to which options
1165: * will be moved, and return value is their length.
1166: * XXX should be deleted; last arg currently ignored.
1167: */
1.8 mycroft 1168: void
1.1 cgd 1169: ip_stripoptions(m, mopt)
1170: register struct mbuf *m;
1171: struct mbuf *mopt;
1172: {
1173: register int i;
1174: struct ip *ip = mtod(m, struct ip *);
1175: register caddr_t opts;
1176: int olen;
1177:
1.79 mycroft 1178: olen = (ip->ip_hl << 2) - sizeof (struct ip);
1.1 cgd 1179: opts = (caddr_t)(ip + 1);
1180: i = m->m_len - (sizeof (struct ip) + olen);
1181: bcopy(opts + olen, opts, (unsigned)i);
1182: m->m_len -= olen;
1183: if (m->m_flags & M_PKTHDR)
1184: m->m_pkthdr.len -= olen;
1.79 mycroft 1185: ip->ip_len -= olen;
1186: ip->ip_hl = sizeof (struct ip) >> 2;
1.1 cgd 1187: }
1188:
1.23 mycroft 1189: int inetctlerrmap[PRC_NCMDS] = {
1.1 cgd 1190: 0, 0, 0, 0,
1191: 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1192: EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1193: EMSGSIZE, EHOSTUNREACH, 0, 0,
1194: 0, 0, 0, 0,
1195: ENOPROTOOPT
1196: };
1197:
1198: /*
1199: * Forward a packet. If some error occurs return the sender
1200: * an icmp packet. Note we can't always generate a meaningful
1201: * icmp message because icmp doesn't have a large enough repertoire
1202: * of codes and types.
1203: *
1204: * If not forwarding, just drop the packet. This could be confusing
1205: * if ipforwarding was zero but some routing protocol was advancing
1206: * us as a gateway to somewhere. However, we must let the routing
1207: * protocol deal with that.
1208: *
1209: * The srcrt parameter indicates whether the packet is being forwarded
1210: * via a source route.
1211: */
1.13 mycroft 1212: void
1.1 cgd 1213: ip_forward(m, srcrt)
1214: struct mbuf *m;
1215: int srcrt;
1216: {
1217: register struct ip *ip = mtod(m, struct ip *);
1218: register struct sockaddr_in *sin;
1219: register struct rtentry *rt;
1.28 christos 1220: int error, type = 0, code = 0;
1.1 cgd 1221: struct mbuf *mcopy;
1.13 mycroft 1222: n_long dest;
1223: struct ifnet *destifp;
1.1 cgd 1224:
1.13 mycroft 1225: dest = 0;
1.1 cgd 1226: #ifdef DIAGNOSTIC
1227: if (ipprintfs)
1.70 thorpej 1228: printf("forward: src %2.2x dst %2.2x ttl %x\n",
1229: ntohl(ip->ip_src.s_addr),
1230: ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
1.1 cgd 1231: #endif
1232: if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
1233: ipstat.ips_cantforward++;
1234: m_freem(m);
1235: return;
1236: }
1237: if (ip->ip_ttl <= IPTTLDEC) {
1.13 mycroft 1238: icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1 cgd 1239: return;
1240: }
1241: ip->ip_ttl -= IPTTLDEC;
1242:
1.19 mycroft 1243: sin = satosin(&ipforward_rt.ro_dst);
1.1 cgd 1244: if ((rt = ipforward_rt.ro_rt) == 0 ||
1.35 mycroft 1245: !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1.1 cgd 1246: if (ipforward_rt.ro_rt) {
1247: RTFREE(ipforward_rt.ro_rt);
1248: ipforward_rt.ro_rt = 0;
1249: }
1250: sin->sin_family = AF_INET;
1.35 mycroft 1251: sin->sin_len = sizeof(struct sockaddr_in);
1.1 cgd 1252: sin->sin_addr = ip->ip_dst;
1253:
1254: rtalloc(&ipforward_rt);
1255: if (ipforward_rt.ro_rt == 0) {
1.13 mycroft 1256: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1.1 cgd 1257: return;
1258: }
1259: rt = ipforward_rt.ro_rt;
1260: }
1261:
1262: /*
1.34 mycroft 1263: * Save at most 68 bytes of the packet in case
1.1 cgd 1264: * we need to generate an ICMP message to the src.
1265: */
1.34 mycroft 1266: mcopy = m_copy(m, 0, imin((int)ip->ip_len, 68));
1.1 cgd 1267:
1268: /*
1269: * If forwarding packet using same interface that it came in on,
1270: * perhaps should send a redirect to sender to shortcut a hop.
1271: * Only send redirect if source is sending directly to us,
1272: * and if packet was not source routed (or has any options).
1273: * Also, don't send redirect if forwarding using a default route
1274: * or a route modified by a redirect.
1275: */
1276: if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1277: (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1.35 mycroft 1278: !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1.1 cgd 1279: ipsendredirects && !srcrt) {
1.19 mycroft 1280: if (rt->rt_ifa &&
1281: (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1282: ifatoia(rt->rt_ifa)->ia_subnet) {
1.77 thorpej 1283: if (rt->rt_flags & RTF_GATEWAY)
1284: dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1285: else
1286: dest = ip->ip_dst.s_addr;
1287: /*
1288: * Router requirements says to only send host
1289: * redirects.
1290: */
1291: type = ICMP_REDIRECT;
1292: code = ICMP_REDIRECT_HOST;
1.1 cgd 1293: #ifdef DIAGNOSTIC
1.77 thorpej 1294: if (ipprintfs)
1295: printf("redirect (%d) to %x\n", code,
1296: (u_int32_t)dest);
1.1 cgd 1297: #endif
1298: }
1299: }
1300:
1.27 thorpej 1301: error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1302: (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1.1 cgd 1303: if (error)
1304: ipstat.ips_cantforward++;
1305: else {
1306: ipstat.ips_forward++;
1307: if (type)
1308: ipstat.ips_redirectsent++;
1309: else {
1.63 matt 1310: if (mcopy) {
1311: #ifdef GATEWAY
1.64 thorpej 1312: if (mcopy->m_flags & M_CANFASTFWD)
1313: ipflow_create(&ipforward_rt, mcopy);
1.63 matt 1314: #endif
1.1 cgd 1315: m_freem(mcopy);
1.63 matt 1316: }
1.1 cgd 1317: return;
1318: }
1319: }
1320: if (mcopy == NULL)
1321: return;
1.13 mycroft 1322: destifp = NULL;
1323:
1.1 cgd 1324: switch (error) {
1325:
1326: case 0: /* forwarded, but need redirect */
1327: /* type, code set above */
1328: break;
1329:
1330: case ENETUNREACH: /* shouldn't happen, checked above */
1331: case EHOSTUNREACH:
1332: case ENETDOWN:
1333: case EHOSTDOWN:
1334: default:
1335: type = ICMP_UNREACH;
1336: code = ICMP_UNREACH_HOST;
1337: break;
1338:
1339: case EMSGSIZE:
1340: type = ICMP_UNREACH;
1341: code = ICMP_UNREACH_NEEDFRAG;
1.13 mycroft 1342: if (ipforward_rt.ro_rt)
1343: destifp = ipforward_rt.ro_rt->rt_ifp;
1.1 cgd 1344: ipstat.ips_cantfrag++;
1345: break;
1346:
1347: case ENOBUFS:
1348: type = ICMP_SOURCEQUENCH;
1349: code = 0;
1350: break;
1351: }
1.13 mycroft 1352: icmp_error(mcopy, type, code, dest, destifp);
1.44 thorpej 1353: }
1354:
1355: void
1356: ip_savecontrol(inp, mp, ip, m)
1357: register struct inpcb *inp;
1358: register struct mbuf **mp;
1359: register struct ip *ip;
1360: register struct mbuf *m;
1361: {
1362:
1363: if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1364: struct timeval tv;
1365:
1366: microtime(&tv);
1367: *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1368: SCM_TIMESTAMP, SOL_SOCKET);
1369: if (*mp)
1370: mp = &(*mp)->m_next;
1371: }
1372: if (inp->inp_flags & INP_RECVDSTADDR) {
1373: *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1374: sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1375: if (*mp)
1376: mp = &(*mp)->m_next;
1377: }
1378: #ifdef notyet
1379: /*
1380: * XXX
1381: * Moving these out of udp_input() made them even more broken
1382: * than they already were.
1383: * - fenner@parc.xerox.com
1384: */
1385: /* options were tossed already */
1386: if (inp->inp_flags & INP_RECVOPTS) {
1387: *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1388: sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1389: if (*mp)
1390: mp = &(*mp)->m_next;
1391: }
1392: /* ip_srcroute doesn't do what we want here, need to fix */
1393: if (inp->inp_flags & INP_RECVRETOPTS) {
1394: *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1395: sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1396: if (*mp)
1397: mp = &(*mp)->m_next;
1398: }
1399: #endif
1400: if (inp->inp_flags & INP_RECVIF) {
1401: struct sockaddr_dl sdl;
1402:
1403: sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1404: sdl.sdl_family = AF_LINK;
1405: sdl.sdl_index = m->m_pkthdr.rcvif ?
1406: m->m_pkthdr.rcvif->if_index : 0;
1407: sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1408: *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1409: IP_RECVIF, IPPROTO_IP);
1410: if (*mp)
1411: mp = &(*mp)->m_next;
1412: }
1.13 mycroft 1413: }
1414:
1415: int
1416: ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1417: int *name;
1418: u_int namelen;
1419: void *oldp;
1420: size_t *oldlenp;
1421: void *newp;
1422: size_t newlen;
1423: {
1.52 thorpej 1424: extern int subnetsarelocal;
1425:
1.54 lukem 1426: int error, old;
1427:
1.13 mycroft 1428: /* All sysctl names at this level are terminal. */
1429: if (namelen != 1)
1430: return (ENOTDIR);
1431:
1432: switch (name[0]) {
1433: case IPCTL_FORWARDING:
1434: return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
1435: case IPCTL_SENDREDIRECTS:
1436: return (sysctl_int(oldp, oldlenp, newp, newlen,
1437: &ipsendredirects));
1438: case IPCTL_DEFTTL:
1439: return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
1440: #ifdef notyet
1441: case IPCTL_DEFMTU:
1442: return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1443: #endif
1.26 thorpej 1444: case IPCTL_FORWSRCRT:
1.47 cjs 1445: /* Don't allow this to change in a secure environment. */
1.26 thorpej 1446: if (securelevel > 0)
1.46 cjs 1447: return (sysctl_rdint(oldp, oldlenp, newp,
1448: ip_forwsrcrt));
1449: else
1450: return (sysctl_int(oldp, oldlenp, newp, newlen,
1451: &ip_forwsrcrt));
1.27 thorpej 1452: case IPCTL_DIRECTEDBCAST:
1453: return (sysctl_int(oldp, oldlenp, newp, newlen,
1454: &ip_directedbcast));
1.47 cjs 1455: case IPCTL_ALLOWSRCRT:
1456: return (sysctl_int(oldp, oldlenp, newp, newlen,
1457: &ip_allowsrcrt));
1.52 thorpej 1458: case IPCTL_SUBNETSARELOCAL:
1459: return (sysctl_int(oldp, oldlenp, newp, newlen,
1460: &subnetsarelocal));
1.53 kml 1461: case IPCTL_MTUDISC:
1.60 kml 1462: error = sysctl_int(oldp, oldlenp, newp, newlen,
1463: &ip_mtudisc);
1464: if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
1465: ip_mtudisc_timeout_q =
1466: rt_timer_queue_create(ip_mtudisc_timeout);
1467: } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
1468: rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE);
1469: ip_mtudisc_timeout_q = NULL;
1470: }
1471: return error;
1.54 lukem 1472: case IPCTL_ANONPORTMIN:
1473: old = anonportmin;
1474: error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
1475: if (anonportmin >= anonportmax || anonportmin > 65535
1476: #ifndef IPNOPRIVPORTS
1477: || anonportmin < IPPORT_RESERVED
1478: #endif
1479: ) {
1480: anonportmin = old;
1481: return (EINVAL);
1482: }
1483: return (error);
1484: case IPCTL_ANONPORTMAX:
1485: old = anonportmax;
1486: error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
1487: if (anonportmin >= anonportmax || anonportmax > 65535
1488: #ifndef IPNOPRIVPORTS
1489: || anonportmax < IPPORT_RESERVED
1490: #endif
1491: ) {
1492: anonportmax = old;
1493: return (EINVAL);
1494: }
1.60 kml 1495: return (error);
1496: case IPCTL_MTUDISCTIMEOUT:
1497: error = sysctl_int(oldp, oldlenp, newp, newlen,
1498: &ip_mtudisc_timeout);
1499: if (ip_mtudisc_timeout_q != NULL)
1500: rt_timer_queue_change(ip_mtudisc_timeout_q,
1501: ip_mtudisc_timeout);
1.54 lukem 1502: return (error);
1.65 matt 1503: #ifdef GATEWAY
1504: case IPCTL_MAXFLOWS:
1.67 thorpej 1505: {
1506: int s;
1507:
1.65 matt 1508: error = sysctl_int(oldp, oldlenp, newp, newlen,
1509: &ip_maxflows);
1.67 thorpej 1510: s = splsoftnet();
1.65 matt 1511: ipflow_reap(0);
1.67 thorpej 1512: splx(s);
1.65 matt 1513: return (error);
1.67 thorpej 1514: }
1.65 matt 1515: #endif
1.13 mycroft 1516: default:
1517: return (EOPNOTSUPP);
1518: }
1519: /* NOTREACHED */
1.1 cgd 1520: }
CVSweb <webmaster@jp.NetBSD.org>