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

📄 tcp_minisocks.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
					 struct request_sock *req){	tp->ecn_flags = inet_rsk(req)->ecn_ok ? TCP_ECN_OK : 0;}/* This is not only more efficient than what we used to do, it eliminates * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM * * Actually, we could lots of memory writes here. tp of listening * socket contains all necessary default parameters. */struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req, struct sk_buff *skb){	struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);	if (newsk != NULL) {		const struct inet_request_sock *ireq = inet_rsk(req);		struct tcp_request_sock *treq = tcp_rsk(req);		struct inet_connection_sock *newicsk = inet_csk(newsk);		struct tcp_sock *newtp;		/* Now setup tcp_sock */		newtp = tcp_sk(newsk);		newtp->pred_flags = 0;		newtp->rcv_wup = newtp->copied_seq = newtp->rcv_nxt = treq->rcv_isn + 1;		newtp->snd_sml = newtp->snd_una = newtp->snd_nxt = treq->snt_isn + 1;		tcp_prequeue_init(newtp);		tcp_init_wl(newtp, treq->snt_isn, treq->rcv_isn);		newtp->srtt = 0;		newtp->mdev = TCP_TIMEOUT_INIT;		newicsk->icsk_rto = TCP_TIMEOUT_INIT;		newtp->packets_out = 0;		newtp->retrans_out = 0;		newtp->sacked_out = 0;		newtp->fackets_out = 0;		newtp->snd_ssthresh = 0x7fffffff;		/* So many TCP implementations out there (incorrectly) count the		 * initial SYN frame in their delayed-ACK and congestion control		 * algorithms that we must have the following bandaid to talk		 * efficiently to them.  -DaveM		 */		newtp->snd_cwnd = 2;		newtp->snd_cwnd_cnt = 0;		newtp->bytes_acked = 0;		newtp->frto_counter = 0;		newtp->frto_highmark = 0;		newicsk->icsk_ca_ops = &tcp_init_congestion_ops;		tcp_set_ca_state(newsk, TCP_CA_Open);		tcp_init_xmit_timers(newsk);		skb_queue_head_init(&newtp->out_of_order_queue);		newtp->write_seq = treq->snt_isn + 1;		newtp->pushed_seq = newtp->write_seq;		newtp->rx_opt.saw_tstamp = 0;		newtp->rx_opt.dsack = 0;		newtp->rx_opt.eff_sacks = 0;		newtp->rx_opt.num_sacks = 0;		newtp->urg_data = 0;		if (sock_flag(newsk, SOCK_KEEPOPEN))			inet_csk_reset_keepalive_timer(newsk,						       keepalive_time_when(newtp));		newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;		if ((newtp->rx_opt.sack_ok = ireq->sack_ok) != 0) {			if (sysctl_tcp_fack)				tcp_enable_fack(newtp);		}		newtp->window_clamp = req->window_clamp;		newtp->rcv_ssthresh = req->rcv_wnd;		newtp->rcv_wnd = req->rcv_wnd;		newtp->rx_opt.wscale_ok = ireq->wscale_ok;		if (newtp->rx_opt.wscale_ok) {			newtp->rx_opt.snd_wscale = ireq->snd_wscale;			newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;		} else {			newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;			newtp->window_clamp = min(newtp->window_clamp, 65535U);		}		newtp->snd_wnd = (ntohs(tcp_hdr(skb)->window) <<				  newtp->rx_opt.snd_wscale);		newtp->max_window = newtp->snd_wnd;		if (newtp->rx_opt.tstamp_ok) {			newtp->rx_opt.ts_recent = req->ts_recent;			newtp->rx_opt.ts_recent_stamp = get_seconds();			newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;		} else {			newtp->rx_opt.ts_recent_stamp = 0;			newtp->tcp_header_len = sizeof(struct tcphdr);		}#ifdef CONFIG_TCP_MD5SIG		newtp->md5sig_info = NULL;	/*XXX*/		if (newtp->af_specific->md5_lookup(sk, newsk))			newtp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;#endif		if (skb->len >= TCP_MIN_RCVMSS+newtp->tcp_header_len)			newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;		newtp->rx_opt.mss_clamp = req->mss;		TCP_ECN_openreq_child(newtp, req);		TCP_INC_STATS_BH(TCP_MIB_PASSIVEOPENS);	}	return newsk;}/* *	Process an incoming packet for SYN_RECV sockets represented *	as a request_sock. */struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,			   struct request_sock *req,			   struct request_sock **prev){	const struct tcphdr *th = tcp_hdr(skb);	__be32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);	int paws_reject = 0;	struct tcp_options_received tmp_opt;	struct sock *child;	tmp_opt.saw_tstamp = 0;	if (th->doff > (sizeof(struct tcphdr)>>2)) {		tcp_parse_options(skb, &tmp_opt, 0);		if (tmp_opt.saw_tstamp) {			tmp_opt.ts_recent = req->ts_recent;			/* We do not store true stamp, but it is not required,			 * it can be estimated (approximately)			 * from another data.			 */			tmp_opt.ts_recent_stamp = get_seconds() - ((TCP_TIMEOUT_INIT/HZ)<<req->retrans);			paws_reject = tcp_paws_check(&tmp_opt, th->rst);		}	}	/* Check for pure retransmitted SYN. */	if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&	    flg == TCP_FLAG_SYN &&	    !paws_reject) {		/*		 * RFC793 draws (Incorrectly! It was fixed in RFC1122)		 * this case on figure 6 and figure 8, but formal		 * protocol description says NOTHING.		 * To be more exact, it says that we should send ACK,		 * because this segment (at least, if it has no data)		 * is out of window.		 *		 *  CONCLUSION: RFC793 (even with RFC1122) DOES NOT		 *  describe SYN-RECV state. All the description		 *  is wrong, we cannot believe to it and should		 *  rely only on common sense and implementation		 *  experience.		 *		 * Enforce "SYN-ACK" according to figure 8, figure 6		 * of RFC793, fixed by RFC1122.		 */		req->rsk_ops->rtx_syn_ack(sk, req, NULL);		return NULL;	}	/* Further reproduces section "SEGMENT ARRIVES"	   for state SYN-RECEIVED of RFC793.	   It is broken, however, it does not work only	   when SYNs are crossed.	   You would think that SYN crossing is impossible here, since	   we should have a SYN_SENT socket (from connect()) on our end,	   but this is not true if the crossed SYNs were sent to both	   ends by a malicious third party.  We must defend against this,	   and to do that we first verify the ACK (as per RFC793, page	   36) and reset if it is invalid.  Is this a true full defense?	   To convince ourselves, let us consider a way in which the ACK	   test can still pass in this 'malicious crossed SYNs' case.	   Malicious sender sends identical SYNs (and thus identical sequence	   numbers) to both A and B:		A: gets SYN, seq=7		B: gets SYN, seq=7	   By our good fortune, both A and B select the same initial	   send sequence number of seven :-)		A: sends SYN|ACK, seq=7, ack_seq=8		B: sends SYN|ACK, seq=7, ack_seq=8	   So we are now A eating this SYN|ACK, ACK test passes.  So	   does sequence test, SYN is truncated, and thus we consider	   it a bare ACK.	   If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this	   bare ACK.  Otherwise, we create an established connection.  Both	   ends (listening sockets) accept the new incoming connection and try	   to talk to each other. 8-)	   Note: This case is both harmless, and rare.  Possibility is about the	   same as us discovering intelligent life on another plant tomorrow.	   But generally, we should (RFC lies!) to accept ACK	   from SYNACK both here and in tcp_rcv_state_process().	   tcp_rcv_state_process() does not, hence, we do not too.	   Note that the case is absolutely generic:	   we cannot optimize anything here without	   violating protocol. All the checks must be made	   before attempt to create socket.	 */	/* RFC793 page 36: "If the connection is in any non-synchronized state ...	 *                  and the incoming segment acknowledges something not yet	 *                  sent (the segment carries an unacceptable ACK) ...	 *                  a reset is sent."	 *	 * Invalid ACK: reset will be sent by listening socket	 */	if ((flg & TCP_FLAG_ACK) &&	    (TCP_SKB_CB(skb)->ack_seq != tcp_rsk(req)->snt_isn + 1))		return sk;	/* Also, it would be not so bad idea to check rcv_tsecr, which	 * is essentially ACK extension and too early or too late values	 * should cause reset in unsynchronized states.	 */	/* RFC793: "first check sequence number". */	if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,					  tcp_rsk(req)->rcv_isn + 1, tcp_rsk(req)->rcv_isn + 1 + req->rcv_wnd)) {		/* Out of window: send ACK and drop. */		if (!(flg & TCP_FLAG_RST))			req->rsk_ops->send_ack(skb, req);		if (paws_reject)			NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);		return NULL;	}	/* In sequence, PAWS is OK. */	if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_isn + 1))			req->ts_recent = tmp_opt.rcv_tsval;		if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {			/* Truncate SYN, it is out of window starting			   at tcp_rsk(req)->rcv_isn + 1. */			flg &= ~TCP_FLAG_SYN;		}		/* RFC793: "second check the RST bit" and		 *	   "fourth, check the SYN bit"		 */		if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN)) {			TCP_INC_STATS_BH(TCP_MIB_ATTEMPTFAILS);			goto embryonic_reset;		}		/* ACK sequence verified above, just make sure ACK is		 * set.  If ACK not set, just silently drop the packet.		 */		if (!(flg & TCP_FLAG_ACK))			return NULL;		/* If TCP_DEFER_ACCEPT is set, drop bare ACK. */		if (inet_csk(sk)->icsk_accept_queue.rskq_defer_accept &&		    TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {			inet_rsk(req)->acked = 1;			return NULL;		}		/* OK, ACK is valid, create big socket and		 * feed this segment to it. It will repeat all		 * the tests. THIS SEGMENT MUST MOVE SOCKET TO		 * ESTABLISHED STATE. If it will be dropped after		 * socket is created, wait for troubles.		 */		child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb,								 req, NULL);		if (child == NULL)			goto listen_overflow;#ifdef CONFIG_TCP_MD5SIG		else {			/* Copy over the MD5 key from the original socket */			struct tcp_md5sig_key *key;			struct tcp_sock *tp = tcp_sk(sk);			key = tp->af_specific->md5_lookup(sk, child);			if (key != NULL) {				/*				 * We're using one, so create a matching key on the				 * newsk structure. If we fail to get memory then we				 * end up not copying the key across. Shucks.				 */				char *newkey = kmemdup(key->key, key->keylen,						       GFP_ATOMIC);				if (newkey) {					if (!tcp_alloc_md5sig_pool())						BUG();					tp->af_specific->md5_add(child, child,								 newkey,								 key->keylen);				}			}		}#endif		inet_csk_reqsk_queue_unlink(sk, req, prev);		inet_csk_reqsk_queue_removed(sk, req);		inet_csk_reqsk_queue_add(sk, req, child);		return child;	listen_overflow:		if (!sysctl_tcp_abort_on_overflow) {			inet_rsk(req)->acked = 1;			return NULL;		}	embryonic_reset:		NET_INC_STATS_BH(LINUX_MIB_EMBRYONICRSTS);		if (!(flg & TCP_FLAG_RST))			req->rsk_ops->send_reset(sk, skb);		inet_csk_reqsk_queue_drop(sk, req, prev);		return NULL;}/* * Queue segment on the new socket if the new socket is active, * otherwise we just shortcircuit this and continue with * the new socket. */int tcp_child_process(struct sock *parent, struct sock *child,		      struct sk_buff *skb){	int ret = 0;	int state = child->sk_state;	if (!sock_owned_by_user(child)) {		ret = tcp_rcv_state_process(child, skb, tcp_hdr(skb),					    skb->len);		/* Wakeup parent, send SIGIO */		if (state == TCP_SYN_RECV && child->sk_state != state)			parent->sk_data_ready(parent, 0);	} else {		/* Alas, it is possible again, because we do lookup		 * in main socket hash table and lock on listening		 * socket does not protect us more.		 */		sk_add_backlog(child, skb);	}	bh_unlock_sock(child);	sock_put(child);	return ret;}EXPORT_SYMBOL(tcp_check_req);EXPORT_SYMBOL(tcp_child_process);EXPORT_SYMBOL(tcp_create_openreq_child);EXPORT_SYMBOL(tcp_timewait_state_process);

⌨️ 快捷键说明

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