tcp_input.c
来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 2,232 行 · 第 1/5 页
C
2,232 行
/* * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 * $Id: tcp_input.c,v 1.82.2.1 1999/04/20 19:09:15 obrien Exp $ */#include "opt_ipfw.h" /* for ipfw_fwd */#include "opt_tcpdebug.h"#include <sys/param.h>#include <sys/systm.h>#include <sys/kernel.h>#include <sys/sysctl.h>#include <sys/malloc.h>#include <sys/mbuf.h>#include <sys/proc.h> /* for proc0 declaration */#include <sys/protosw.h>#include <sys/socket.h>#include <sys/socketvar.h>#include <sys/syslog.h>#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */#include <net/if.h>#include <net/route.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/ip.h>#include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */#include <netinet/in_pcb.h>#include <netinet/ip_var.h>#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */#include <netinet/tcp.h>#include <netinet/tcp_fsm.h>#include <netinet/tcp_seq.h>#include <netinet/tcp_timer.h>#include <netinet/tcp_var.h>#include <netinet/tcpip.h>#ifdef TCPDEBUG#include <netinet/tcp_debug.h>static struct tcpiphdr tcp_saveti;#endifstatic int tcprexmtthresh = 3;tcp_seq tcp_iss;tcp_cc tcp_ccgen;struct tcpstat tcpstat;SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD, &tcpstat , tcpstat, "");static int log_in_vain = 0;SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, &log_in_vain, 0, "");int tcp_delack_enabled = 1;SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, &tcp_delack_enabled, 0, "");u_long tcp_now;struct inpcbhead tcb;struct inpcbinfo tcbinfo;static void tcp_dooptions __P((struct tcpcb *, u_char *, int, struct tcpiphdr *, struct tcpopt *));static void tcp_pulloutofband __P((struct socket *, struct tcpiphdr *, struct mbuf *));static int tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *));static void tcp_xmit_timer __P((struct tcpcb *, int));/* * Insert segment ti into reassembly queue of tcp with * control block tp. Return TH_FIN if reassembly now includes * a segment with FIN. The macro form does the common case inline * (segment is the next to be received on an established connection, * and the queue is empty), avoiding linkage into and removal * from the queue and repetition of various conversions. * Set DELACK for segments received in order, but ack immediately * when segments are out of order (so fast retransmit can work). */#define TCP_REASS(tp, ti, m, so, flags) { \ if ((ti)->ti_seq == (tp)->rcv_nxt && \ (tp)->t_segq == NULL && \ (tp)->t_state == TCPS_ESTABLISHED) { \ if (tcp_delack_enabled) \ tp->t_flags |= TF_DELACK; \ else \ tp->t_flags |= TF_ACKNOW; \ (tp)->rcv_nxt += (ti)->ti_len; \ flags = (ti)->ti_flags & TH_FIN; \ tcpstat.tcps_rcvpack++;\ tcpstat.tcps_rcvbyte += (ti)->ti_len;\ sbappend(&(so)->so_rcv, (m)); \ sorwakeup(so); \ } else { \ (flags) = tcp_reass((tp), (ti), (m)); \ tp->t_flags |= TF_ACKNOW; \ } \}static inttcp_reass(tp, ti, m) register struct tcpcb *tp; register struct tcpiphdr *ti; struct mbuf *m;{ struct mbuf *q; struct mbuf *p; struct mbuf *nq; struct socket *so = tp->t_inpcb->inp_socket; int flags;#define GETTCP(m) ((struct tcpiphdr *)m->m_pkthdr.header) /* * Call with ti==0 after become established to * force pre-ESTABLISHED data up to user socket. */ if (ti == 0) goto present; m->m_pkthdr.header = ti; /* * Find a segment which begins after this one does. */ for (q = tp->t_segq, p = NULL; q; p = q, q = q->m_nextpkt) if (SEQ_GT(GETTCP(q)->ti_seq, ti->ti_seq)) break; /* * If there is a preceding segment, it may provide some of * our data already. If so, drop the data from the incoming * segment. If it provides all of our data, drop us. */ if (p != NULL) { register int i; /* conversion to int (in i) handles seq wraparound */ i = GETTCP(p)->ti_seq + GETTCP(p)->ti_len - ti->ti_seq; if (i > 0) { if (i >= ti->ti_len) { tcpstat.tcps_rcvduppack++; tcpstat.tcps_rcvdupbyte += ti->ti_len; m_freem(m); /* * Try to present any queued data * at the left window edge to the user. * This is needed after the 3-WHS * completes. */ goto present; /* ??? */ } m_adj(m, i); ti->ti_len -= i; ti->ti_seq += i; } } tcpstat.tcps_rcvoopack++; tcpstat.tcps_rcvoobyte += ti->ti_len; /* * While we overlap succeeding segments trim them or, * if they are completely covered, dequeue them. */ while (q) { register int i = (ti->ti_seq + ti->ti_len) - GETTCP(q)->ti_seq; if (i <= 0) break; if (i < GETTCP(q)->ti_len) { GETTCP(q)->ti_seq += i; GETTCP(q)->ti_len -= i; m_adj(q, i); break; } nq = q->m_nextpkt; if (p) p->m_nextpkt = nq; else tp->t_segq = nq; m_freem(q); q = nq; } if (p == NULL) { m->m_nextpkt = tp->t_segq; tp->t_segq = m; } else { m->m_nextpkt = p->m_nextpkt; p->m_nextpkt = m; }present: /* * Present data to user, advancing rcv_nxt through * completed sequence space. */ if (!TCPS_HAVEESTABLISHED(tp->t_state)) return (0); q = tp->t_segq; if (!q || GETTCP(q)->ti_seq != tp->rcv_nxt) return (0); do { tp->rcv_nxt += GETTCP(q)->ti_len; flags = GETTCP(q)->ti_flags & TH_FIN; nq = q->m_nextpkt; tp->t_segq = nq; q->m_nextpkt = NULL; if (so->so_state & SS_CANTRCVMORE) m_freem(q); else sbappend(&so->so_rcv, q); q = nq; } while (q && GETTCP(q)->ti_seq == tp->rcv_nxt); sorwakeup(so); return (flags);#undef GETTCP}/* * TCP input routine, follows pages 65-76 of the * protocol specification dated September, 1981 very closely. */voidtcp_input(m, iphlen) register struct mbuf *m; int iphlen;{ register struct tcpiphdr *ti; register struct inpcb *inp; u_char *optp = NULL; int optlen = 0; int len, tlen, off; register struct tcpcb *tp = 0; register int tiflags; struct socket *so = 0; int todrop, acked, ourfinisacked, needoutput = 0; struct in_addr laddr; int dropsocket = 0; int iss = 0; u_long tiwin; struct tcpopt to; /* options in this segment */ struct rmxp_tao *taop; /* pointer to our TAO cache entry */ struct rmxp_tao tao_noncached; /* in case there's no cached entry */#ifdef TCPDEBUG short ostate = 0;#endif bzero((char *)&to, sizeof(to)); tcpstat.tcps_rcvtotal++; /* * Get IP and TCP header together in first mbuf. * Note: IP leaves IP header in first mbuf. */ ti = mtod(m, struct tcpiphdr *); if (iphlen > sizeof (struct ip)) ip_stripoptions(m, (struct mbuf *)0); if (m->m_len < sizeof (struct tcpiphdr)) { if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { tcpstat.tcps_rcvshort++; return; } ti = mtod(m, struct tcpiphdr *); } /* * Checksum extended TCP header and data. */ tlen = ((struct ip *)ti)->ip_len; len = sizeof (struct ip) + tlen; bzero(ti->ti_x1, sizeof(ti->ti_x1)); ti->ti_len = (u_short)tlen; HTONS(ti->ti_len); ti->ti_sum = in_cksum(m, len); if (ti->ti_sum) { tcpstat.tcps_rcvbadsum++; goto drop; } /* * Check that TCP offset makes sense, * pull out TCP options and adjust length. XXX */ off = ti->ti_off << 2; if (off < sizeof (struct tcphdr) || off > tlen) { tcpstat.tcps_rcvbadoff++; goto drop; } tlen -= off; ti->ti_len = tlen; if (off > sizeof (struct tcphdr)) { if (m->m_len < sizeof(struct ip) + off) { if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { tcpstat.tcps_rcvshort++; return; } ti = mtod(m, struct tcpiphdr *); } optlen = off - sizeof (struct tcphdr); optp = mtod(m, u_char *) + sizeof (struct tcpiphdr); } tiflags = ti->ti_flags; /* * Convert TCP protocol specific fields to host format. */ NTOHL(ti->ti_seq); NTOHL(ti->ti_ack); NTOHS(ti->ti_win); NTOHS(ti->ti_urp); /* * Drop TCP, IP headers and TCP options. */ m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); /* * Locate pcb for segment. */findpcb:#ifdef IPFIREWALL_FORWARD if (ip_fw_fwd_addr != NULL) { /* * Diverted. Pretend to be the destination. * already got one like this? */ inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 0); if (!inp) { /* * No, then it's new. Try find the ambushing socket */ if (!ip_fw_fwd_addr->sin_port) { inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport, ip_fw_fwd_addr->sin_addr, ti->ti_dport, 1); } else { inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport, ip_fw_fwd_addr->sin_addr, ntohs(ip_fw_fwd_addr->sin_port), 1); } } ip_fw_fwd_addr = NULL; } else#endif /* IPFIREWALL_FORWARD */ inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 1); /* * If the state is CLOSED (i.e., TCB does not exist) then * all data in the incoming segment is discarded. * If the TCB exists but is in CLOSED state, it is embryonic, * but should either do a listen or a connect soon. */ if (inp == NULL) { if (log_in_vain && tiflags & TH_SYN) { char buf[4*sizeof "123"]; strcpy(buf, inet_ntoa(ti->ti_dst)); log(LOG_INFO, "Connection attempt to TCP %s:%d from %s:%d\n", buf, ntohs(ti->ti_dport), inet_ntoa(ti->ti_src), ntohs(ti->ti_sport)); }#ifdef ICMP_BANDLIM if (badport_bandlim(1) < 0) goto drop;#endif goto dropwithreset; } tp = intotcpcb(inp); if (tp == 0) goto dropwithreset; if (tp->t_state == TCPS_CLOSED) goto drop; /* Unscale the window into a 32-bit value. */ if ((tiflags & TH_SYN) == 0) tiwin = ti->ti_win << tp->snd_scale; else tiwin = ti->ti_win; so = inp->inp_socket; if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {#ifdef TCPDEBUG if (so->so_options & SO_DEBUG) { ostate = tp->t_state; tcp_saveti = *ti; }#endif if (so->so_options & SO_ACCEPTCONN) { register struct tcpcb *tp0 = tp; struct socket *so2; if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { /* * Note: dropwithreset makes sure we don't * send a RST in response to a RST. */ if (tiflags & TH_ACK) { tcpstat.tcps_badsyn++; goto dropwithreset; } goto drop; } so2 = sonewconn(so, 0); if (so2 == 0) { tcpstat.tcps_listendrop++; so2 = sodropablereq(so); if (so2) { tcp_drop(sototcpcb(so2), ETIMEDOUT);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?