⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp.c

📁 GNU Hurd 源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * INET		An implementation of the TCP/IP protocol suite for the LINUX *		operating system.  INET is implemented using the  BSD Socket *		interface as the means of communication with the user level. * *		Implementation of the Transmission Control Protocol(TCP). * * Version:	$Id: tcp.c,v 1.140.2.5 1999/09/23 19:21:16 davem Exp $ * * Authors:	Ross Biro, <bir7@leland.Stanford.Edu> *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> *		Mark Evans, <evansmp@uhura.aston.ac.uk> *		Corey Minyard <wf-rch!minyard@relay.EU.net> *		Florian La Roche, <flla@stud.uni-sb.de> *		Charles Hedrick, <hedrick@klinzhai.rutgers.edu> *		Linus Torvalds, <torvalds@cs.helsinki.fi> *		Alan Cox, <gw4pts@gw4pts.ampr.org> *		Matthew Dillon, <dillon@apollo.west.oic.com> *		Arnt Gulbrandsen, <agulbra@nvg.unit.no> *		Jorge Cwik, <jorge@laser.satlink.net> * * Fixes: *		Alan Cox	:	Numerous verify_area() calls *		Alan Cox	:	Set the ACK bit on a reset *		Alan Cox	:	Stopped it crashing if it closed while *					sk->inuse=1 and was trying to connect *					(tcp_err()). *		Alan Cox	:	All icmp error handling was broken *					pointers passed where wrong and the *					socket was looked up backwards. Nobody *					tested any icmp error code obviously. *		Alan Cox	:	tcp_err() now handled properly. It *					wakes people on errors. poll *					behaves and the icmp error race *					has gone by moving it into sock.c *		Alan Cox	:	tcp_send_reset() fixed to work for *					everything not just packets for *					unknown sockets. *		Alan Cox	:	tcp option processing. *		Alan Cox	:	Reset tweaked (still not 100%) [Had *					syn rule wrong] *		Herp Rosmanith  :	More reset fixes *		Alan Cox	:	No longer acks invalid rst frames. *					Acking any kind of RST is right out. *		Alan Cox	:	Sets an ignore me flag on an rst *					receive otherwise odd bits of prattle *					escape still *		Alan Cox	:	Fixed another acking RST frame bug. *					Should stop LAN workplace lockups. *		Alan Cox	: 	Some tidyups using the new skb list *					facilities *		Alan Cox	:	sk->keepopen now seems to work *		Alan Cox	:	Pulls options out correctly on accepts *		Alan Cox	:	Fixed assorted sk->rqueue->next errors *		Alan Cox	:	PSH doesn't end a TCP read. Switched a *					bit to skb ops. *		Alan Cox	:	Tidied tcp_data to avoid a potential *					nasty. *		Alan Cox	:	Added some better commenting, as the *					tcp is hard to follow *		Alan Cox	:	Removed incorrect check for 20 * psh *	Michael O'Reilly	:	ack < copied bug fix. *	Johannes Stille		:	Misc tcp fixes (not all in yet). *		Alan Cox	:	FIN with no memory -> CRASH *		Alan Cox	:	Added socket option proto entries. *					Also added awareness of them to accept. *		Alan Cox	:	Added TCP options (SOL_TCP) *		Alan Cox	:	Switched wakeup calls to callbacks, *					so the kernel can layer network *					sockets. *		Alan Cox	:	Use ip_tos/ip_ttl settings. *		Alan Cox	:	Handle FIN (more) properly (we hope). *		Alan Cox	:	RST frames sent on unsynchronised *					state ack error. *		Alan Cox	:	Put in missing check for SYN bit. *		Alan Cox	:	Added tcp_select_window() aka NET2E *					window non shrink trick. *		Alan Cox	:	Added a couple of small NET2E timer *					fixes *		Charles Hedrick :	TCP fixes *		Toomas Tamm	:	TCP window fixes *		Alan Cox	:	Small URG fix to rlogin ^C ack fight *		Charles Hedrick	:	Rewrote most of it to actually work *		Linus		:	Rewrote tcp_read() and URG handling *					completely *		Gerhard Koerting:	Fixed some missing timer handling *		Matthew Dillon  :	Reworked TCP machine states as per RFC *		Gerhard Koerting:	PC/TCP workarounds *		Adam Caldwell	:	Assorted timer/timing errors *		Matthew Dillon	:	Fixed another RST bug *		Alan Cox	:	Move to kernel side addressing changes. *		Alan Cox	:	Beginning work on TCP fastpathing *					(not yet usable) *		Arnt Gulbrandsen:	Turbocharged tcp_check() routine. *		Alan Cox	:	TCP fast path debugging *		Alan Cox	:	Window clamping *		Michael Riepe	:	Bug in tcp_check() *		Matt Dillon	:	More TCP improvements and RST bug fixes *		Matt Dillon	:	Yet more small nasties remove from the *					TCP code (Be very nice to this man if *					tcp finally works 100%) 8) *		Alan Cox	:	BSD accept semantics. *		Alan Cox	:	Reset on closedown bug. *	Peter De Schrijver	:	ENOTCONN check missing in tcp_sendto(). *		Michael Pall	:	Handle poll() after URG properly in *					all cases. *		Michael Pall	:	Undo the last fix in tcp_read_urg() *					(multi URG PUSH broke rlogin). *		Michael Pall	:	Fix the multi URG PUSH problem in *					tcp_readable(), poll() after URG *					works now. *		Michael Pall	:	recv(...,MSG_OOB) never blocks in the *					BSD api. *		Alan Cox	:	Changed the semantics of sk->socket to *					fix a race and a signal problem with *					accept() and async I/O. *		Alan Cox	:	Relaxed the rules on tcp_sendto(). *		Yury Shevchuk	:	Really fixed accept() blocking problem. *		Craig I. Hagan  :	Allow for BSD compatible TIME_WAIT for *					clients/servers which listen in on *					fixed ports. *		Alan Cox	:	Cleaned the above up and shrank it to *					a sensible code size. *		Alan Cox	:	Self connect lockup fix. *		Alan Cox	:	No connect to multicast. *		Ross Biro	:	Close unaccepted children on master *					socket close. *		Alan Cox	:	Reset tracing code. *		Alan Cox	:	Spurious resets on shutdown. *		Alan Cox	:	Giant 15 minute/60 second timer error *		Alan Cox	:	Small whoops in polling before an *					accept. *		Alan Cox	:	Kept the state trace facility since *					it's handy for debugging. *		Alan Cox	:	More reset handler fixes. *		Alan Cox	:	Started rewriting the code based on *					the RFC's for other useful protocol *					references see: Comer, KA9Q NOS, and *					for a reference on the difference *					between specifications and how BSD *					works see the 4.4lite source. *		A.N.Kuznetsov	:	Don't time wait on completion of tidy *					close. *		Linus Torvalds	:	Fin/Shutdown & copied_seq changes. *		Linus Torvalds	:	Fixed BSD port reuse to work first syn *		Alan Cox	:	Reimplemented timers as per the RFC *					and using multiple timers for sanity. *		Alan Cox	:	Small bug fixes, and a lot of new *					comments. *		Alan Cox	:	Fixed dual reader crash by locking *					the buffers (much like datagram.c) *		Alan Cox	:	Fixed stuck sockets in probe. A probe *					now gets fed up of retrying without *					(even a no space) answer. *		Alan Cox	:	Extracted closing code better *		Alan Cox	:	Fixed the closing state machine to *					resemble the RFC. *		Alan Cox	:	More 'per spec' fixes. *		Jorge Cwik	:	Even faster checksumming. *		Alan Cox	:	tcp_data() doesn't ack illegal PSH *					only frames. At least one pc tcp stack *					generates them. *		Alan Cox	:	Cache last socket. *		Alan Cox	:	Per route irtt. *		Matt Day	:	poll()->select() match BSD precisely on error *		Alan Cox	:	New buffers *		Marc Tamsky	:	Various sk->prot->retransmits and *					sk->retransmits misupdating fixed. *					Fixed tcp_write_timeout: stuck close, *					and TCP syn retries gets used now. *		Mark Yarvis	:	In tcp_read_wakeup(), don't send an *					ack if state is TCP_CLOSED. *		Alan Cox	:	Look up device on a retransmit - routes may *					change. Doesn't yet cope with MSS shrink right *					but its a start! *		Marc Tamsky	:	Closing in closing fixes. *		Mike Shaver	:	RFC1122 verifications. *		Alan Cox	:	rcv_saddr errors. *		Alan Cox	:	Block double connect(). *		Alan Cox	:	Small hooks for enSKIP. *		Alexey Kuznetsov:	Path MTU discovery. *		Alan Cox	:	Support soft errors. *		Alan Cox	:	Fix MTU discovery pathological case *					when the remote claims no mtu! *		Marc Tamsky	:	TCP_CLOSE fix. *		Colin (G3TNE)	:	Send a reset on syn ack replies in *					window but wrong (fixes NT lpd problems) *		Pedro Roque	:	Better TCP window handling, delayed ack. *		Joerg Reuter	:	No modification of locked buffers in *					tcp_do_retransmit() *		Eric Schenk	:	Changed receiver side silly window *					avoidance algorithm to BSD style *					algorithm. This doubles throughput *					against machines running Solaris, *					and seems to result in general *					improvement. *	Stefan Magdalinski	:	adjusted tcp_readable() to fix FIONREAD *	Willy Konynenberg	:	Transparent proxying support. *	Mike McLagan		:	Routing by source *		Keith Owens	:	Do proper merging with partial SKB's in *					tcp_do_sendmsg to avoid burstiness. *		Eric Schenk	:	Fix fast close down bug with *					shutdown() followed by close(). *		Andi Kleen :	Make poll agree with SIGIO * *		This program is free software; you can redistribute it and/or *		modify it under the terms of the GNU General Public License *		as published by the Free Software Foundation; either version *		2 of the License, or(at your option) any later version. * * Description of States: * *	TCP_SYN_SENT		sent a connection request, waiting for ack * *	TCP_SYN_RECV		received a connection request, sent ack, *				waiting for final ack in three-way handshake. * *	TCP_ESTABLISHED		connection established * *	TCP_FIN_WAIT1		our side has shutdown, waiting to complete *				transmission of remaining buffered data * *	TCP_FIN_WAIT2		all buffered data sent, waiting for remote *				to shutdown * *	TCP_CLOSING		both sides have shutdown but we still have *				data we have to finish sending * *	TCP_TIME_WAIT		timeout to catch resent junk before entering *				closed, can only be entered from FIN_WAIT2 *				or CLOSING.  Required because the other end *				may not have gotten our last ACK causing it *				to retransmit the data packet (which we ignore) * *	TCP_CLOSE_WAIT		remote side has shutdown and is waiting for *				us to finish writing our data and to shutdown *				(we have to close() to move on to LAST_ACK) * *	TCP_LAST_ACK		out side has shutdown after remote has *				shutdown.  There may still be data in our *				buffer that we have to finish sending * *	TCP_CLOSE		socket is finished *//* * RFC1122 status: * NOTE: I'm not going to be doing comments in the code for this one except * for violations and the like.  tcp.c is just too big... If I say something * "does?" or "doesn't?", it means I'm not sure, and will have to hash it out * with Alan. -- MS 950903 * [Note: Most of the TCP code has been rewriten/redesigned since this *  RFC1122 check. It is probably not correct anymore. It should be redone *  before 2.2. -AK] * * Use of PSH (4.2.2.2) *   MAY aggregate data sent without the PSH flag. (does) *   MAY queue data received without the PSH flag. (does) *   SHOULD collapse successive PSH flags when it packetizes data. (doesn't) *   MAY implement PSH on send calls. (doesn't, thus:) *     MUST NOT buffer data indefinitely (doesn't [1 second]) *     MUST set PSH on last segment (does) *   MAY pass received PSH to application layer (doesn't) *   SHOULD send maximum-sized segment whenever possible. (almost always does) * * Window Size (4.2.2.3, 4.2.2.16) *   MUST treat window size as an unsigned number (does) *   SHOULD treat window size as a 32-bit number (does not) *   MUST NOT shrink window once it is offered (does not normally) * * Urgent Pointer (4.2.2.4) * **MUST point urgent pointer to last byte of urgent data (not right *     after). (doesn't, to be like BSD. That's configurable, but defaults *	to off) *   MUST inform application layer asynchronously of incoming urgent *     data. (does) *   MUST provide application with means of determining the amount of *     urgent data pending. (does) * **MUST support urgent data sequence of arbitrary length. (doesn't, but *   it's sort of tricky to fix, as urg_ptr is a 16-bit quantity) *	[Follows BSD 1 byte of urgent data] * * TCP Options (4.2.2.5) *   MUST be able to receive TCP options in any segment. (does) *   MUST ignore unsupported options (does) * * Maximum Segment Size Option (4.2.2.6) *   MUST implement both sending and receiving MSS. (does, but currently *	only uses the smaller of both of them) *   SHOULD send an MSS with every SYN where receive MSS != 536 (MAY send *     it always). (does, even when MSS == 536, which is legal) *   MUST assume MSS == 536 if no MSS received at connection setup (does) *   MUST calculate "effective send MSS" correctly: *     min(physical_MTU, remote_MSS+20) - sizeof(tcphdr) - sizeof(ipopts) *     (does - but allows operator override) * * TCP Checksum (4.2.2.7) *   MUST generate and check TCP checksum. (does) * * Initial Sequence Number Selection (4.2.2.8) *   MUST use the RFC 793 clock selection mechanism.  (doesn't, but it's *     OK: RFC 793 specifies a 250KHz clock, while we use 1MHz, which is *     necessary for 10Mbps networks - and harder than BSD to spoof! *     With syncookies we don't) * * Simultaneous Open Attempts (4.2.2.10) *   MUST support simultaneous open attempts (does) * * Recovery from Old Duplicate SYN (4.2.2.11) *   MUST keep track of active vs. passive open (does) * * RST segment (4.2.2.12) *   SHOULD allow an RST segment to contain data (does, but doesn't do *     anything with it, which is standard) * * Closing a Connection (4.2.2.13) *   MUST inform application of whether connection was closed by RST or *     normal close. (does) *   MAY allow "half-duplex" close (treat connection as closed for the *     local app, even before handshake is done). (does) *   MUST linger in TIME_WAIT for 2 * MSL (does) * * Retransmission Timeout (4.2.2.15) *   MUST implement Jacobson's slow start and congestion avoidance *     stuff. (does) * * Probing Zero Windows (4.2.2.17) *   MUST support probing of zero windows. (does) *   MAY keep offered window closed indefinitely. (does) *   MUST allow remote window to stay closed indefinitely. (does) * * Passive Open Calls (4.2.2.18) *   MUST NOT let new passive open affect other connections. (doesn't) *   MUST support passive opens (LISTENs) concurrently. (does) * * Time to Live (4.2.2.19) *   MUST make TCP TTL configurable. (does - IP_TTL option) * * Event Processing (4.2.2.20) *   SHOULD queue out-of-order segments. (does) *   MUST aggregate ACK segments whenever possible. (does but badly) * * Retransmission Timeout Calculation (4.2.3.1) *   MUST implement Karn's algorithm and Jacobson's algorithm for RTO *     calculation. (does, or at least explains them in the comments 8*b) *  SHOULD initialize RTO to 0 and RTT to 3. (does) * * When to Send an ACK Segment (4.2.3.2) *   SHOULD implement delayed ACK. (does) *   MUST keep ACK delay < 0.5 sec. (does) * * When to Send a Window Update (4.2.3.3) *   MUST implement receiver-side SWS. (does) * * When to Send Data (4.2.3.4) *   MUST implement sender-side SWS. (does) *   SHOULD implement Nagle algorithm. (does) * * TCP Connection Failures (4.2.3.5) *  MUST handle excessive retransmissions "properly" (see the RFC). (does) *   SHOULD inform application layer of soft errors. (does) * * TCP Keep-Alives (4.2.3.6) *   MAY provide keep-alives. (does) *   MUST make keep-alives configurable on a per-connection basis. (does) *   MUST default to no keep-alives. (does) *   MUST make keep-alive interval configurable. (does) *   MUST make default keep-alive interval > 2 hours. (does) *   MUST NOT interpret failure to ACK keep-alive packet as dead *     connection. (doesn't) *   SHOULD send keep-alive with no data. (does) * * TCP Multihoming (4.2.3.7) *   MUST get source address from IP layer before sending first *     SYN. (does) *   MUST use same local address for all segments of a connection. (does) * * IP Options (4.2.3.8) *   MUST ignore unsupported IP options. (does) *   MAY support Time Stamp and Record Route. (does) *   MUST allow application to specify a source route. (does) *   MUST allow received Source Route option to set route for all future *     segments on this connection. (does not (security issues)) * * ICMP messages (4.2.3.9) *   MUST act on ICMP errors. (does) *   MUST slow transmission upon receipt of a Source Quench. (doesn't anymore *   because that is deprecated now by the IETF, can be turned on) *   MUST NOT abort connection upon receipt of soft Destination *     Unreachables (0, 1, 5), Time Exceededs and Parameter *     Problems. (doesn't) *   SHOULD report soft Destination Unreachables etc. to the *     application. (does, except during SYN_RECV and may drop messages *     in some rare cases before accept() - ICMP is unreliable) *   SHOULD abort connection upon receipt of hard Destination Unreachable *     messages (2, 3, 4). (does, but see above) * * Remote Address Validation (4.2.3.10) *   MUST reject as an error OPEN for invalid remote IP address. (does) *   MUST ignore SYN with invalid source address. (does) *   MUST silently discard incoming SYN for broadcast/multicast *     address. (does) * * Asynchronous Reports (4.2.4.1) * MUST provide mechanism for reporting soft errors to application *     layer. (does) * * Type of Service (4.2.4.2) *   MUST allow application layer to set Type of Service. (does IP_TOS) * * (Whew. -- MS 950903) * (Updated by AK, but not complete yet.) **/#include <linux/types.h>#include <linux/fcntl.h>#include <linux/poll.h>#include <linux/init.h>#include <net/icmp.h>#include <net/tcp.h>#include <asm/uaccess.h>int sysctl_tcp_fin_timeout = TCP_FIN_TIMEOUT;struct tcp_mib	tcp_statistics;kmem_cache_t *tcp_openreq_cachep;kmem_cache_t *tcp_bucket_cachep;kmem_cache_t *tcp_timewait_cachep;/* *	Find someone to 'accept'. Must be called with *	the socket locked or with interrupts disabled */static struct open_request *tcp_find_established(struct tcp_opt *tp,						 struct open_request **prevp){	struct open_request *req = tp->syn_wait_queue;	struct open_request *prev = (struct open_request *)&tp->syn_wait_queue;	while(req) {		if (req->sk &&		    ((1 << req->sk->state) &		     ~(TCPF_SYN_SENT|TCPF_SYN_RECV)))			break;		prev = req;		req = req->dl_next;	}	*prevp = prev;	return req;}/* *	Walk down the receive queue counting readable data. * *	Must be called with the socket lock held. */static int tcp_readable(struct sock *sk){	unsigned long counted;	unsigned long amount;	struct sk_buff *skb;	int sum;	SOCK_DEBUG(sk, "tcp_readable: %p - ",sk);	skb = skb_peek(&sk->receive_queue);	if (skb == NULL) {		SOCK_DEBUG(sk, "empty\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -