xref: /csrg-svn/sys/netinet/tcp_subr.c (revision 5174)
1 /* tcp_subr.c 4.6 81/12/03 */
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/mbuf.h"
6 #include "../h/socket.h"
7 #include "../h/socketvar.h"
8 #include "../h/protosw.h"
9 #include "../net/in.h"
10 #include "../net/in_pcb.h"
11 #include "../net/in_systm.h"
12 #include "../net/if.h"
13 #include "../net/ip.h"
14 #include "../net/ip_var.h"
15 #include "../net/tcp.h"
16 #include "../net/tcp_fsm.h"
17 #include "../net/tcp_seq.h"
18 #include "../net/tcp_timer.h"
19 #include "../net/tcp_var.h"
20 #include "../net/tcpip.h"
21 #include "../errno.h"
22 
23 /*
24  * Tcp initialization
25  */
26 tcp_init()
27 {
28 
29 COUNT(TCP_INIT);
30 	tcp_iss = 1;		/* wrong */
31 	tcb.inp_next = tcb.inp_prev = &tcb;
32 	tcp_alpha = TCP_ALPHA;
33 	tcp_beta = TCP_BETA;
34 }
35 
36 /*
37  * Create template to be used to send tcp packets on a connection.
38  * Call after host entry created, allocates an mbuf and fills
39  * in a skeletal tcp/ip header, minimizing the amount of work
40  * necessary when the connection is used.
41  */
42 struct tcpiphdr *
43 tcp_template(tp)
44 	struct tcpcb *tp;
45 {
46 	register struct inpcb *inp = tp->t_inpcb;
47 	register struct mbuf *m;
48 	register struct tcpiphdr *n;
49 
50 COUNT(TCP_TEMPLATE);
51 	m = m_get(1);
52 	if (m == 0)
53 		return (0);
54 	m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
55 	m->m_len = sizeof (struct tcpiphdr);
56 	n = mtod(m, struct tcpiphdr *);
57 	n->ti_next = n->ti_prev = 0;
58 	n->ti_x1 = 0;
59 	n->ti_pr = IPPROTO_TCP;
60 	n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
61 	n->ti_src = inp->inp_laddr;
62 	n->ti_dst = inp->inp_faddr;
63 	n->ti_sport = inp->inp_lport;
64 	n->ti_dport = inp->inp_fport;
65 	n->ti_seq = 0;
66 	n->ti_ack = 0;
67 	n->ti_x2 = 0;
68 	n->ti_off = 5;
69 	n->ti_flags = 0;
70 	n->ti_win = 0;
71 	n->ti_sum = 0;
72 	n->ti_urp = 0;
73 	return (n);
74 }
75 
76 /*
77  * Send a single message to the TCP at address specified by
78  * the given TCP/IP header.  If flags==0, then we make a copy
79  * of the tcpiphdr at ti and send directly to the addressed host.
80  * This is used to force keep alive messages out using the TCP
81  * template for a connection tp->t_template.  If flags are given
82  * then we send a message back to the TCP which originated the
83  * segment ti, and discard the mbuf containing it and any other
84  * attached mbufs.
85  *
86  * In any case the ack and sequence number of the transmitted
87  * segment are as specified by the parameters.
88  */
89 tcp_respond(ti, ack, seq, flags)
90 	register struct tcpiphdr *ti;
91 	tcp_seq ack, seq;
92 	int flags;
93 {
94 	struct mbuf *m;
95 
96 COUNT(TCP_RESPOND);
97 	if (flags == 0) {
98 		m = m_get(0);
99 		if (m == 0)
100 			return;
101 		m->m_off = MMINOFF;
102 		m->m_len = sizeof (struct tcpiphdr);
103 		*mtod(m, struct tcpiphdr *) = *ti;
104 		ti = mtod(m, struct tcpiphdr *);
105 		flags = TH_ACK;
106 	} else {
107 		m_freem(m->m_next);
108 		m->m_next = 0;
109 		m->m_len = sizeof (struct tcpiphdr);
110 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
111 		xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
112 		xchg(ti->ti_dport, ti->ti_sport, u_short);
113 #undef xchg
114 	}
115 	ti->ti_next = ti->ti_prev = 0;
116 	ti->ti_x1 = 0;
117 	ti->ti_len = htons(sizeof (struct tcphdr));
118 	ti->ti_seq = htonl(seq);
119 	ti->ti_ack = htonl(ack);
120 	ti->ti_x2 = 0;
121 	ti->ti_off = sizeof (struct tcphdr) >> 2;
122 	ti->ti_flags = flags;
123 	ti->ti_win = ti->ti_urp = 0;
124 	ti->ti_sum = in_cksum(m, sizeof(struct tcpiphdr));
125 	((struct ip *)ti)->ip_len = sizeof(struct tcpiphdr);
126 	((struct ip *)ti)->ip_ttl = TCP_TTL;
127 	(void) ip_output(m, (struct mbuf *)0);
128 }
129 
130 /*
131  * Create a new TCP control block, making an
132  * empty reassembly queue and hooking it to the argument
133  * protocol control block.
134  */
135 struct tcpcb *
136 tcp_newtcpcb(inp)
137 	struct inpcb *inp;
138 {
139 	struct mbuf *m = m_getclr(0);
140 	register struct tcpcb *tp;
141 COUNT(TCP_NEWTCPCB);
142 
143 	if (m == 0)
144 		return (0);
145 	tp = mtod(m, struct tcpcb *);
146 	tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
147 	tp->t_maxseg = 1024;
148 	tp->t_inpcb = inp;
149 	inp->inp_ppcb = (caddr_t)tp;
150 	return (tp);
151 }
152 
153 /*
154  * Drop a TCP connection, reporting
155  * the specified error.  If connection is synchronized,
156  * then send a RST to peer.
157  */
158 tcp_drop(tp, errno)
159 	struct tcpcb *tp;
160 	int errno;
161 {
162 	struct socket *so = tp->t_inpcb->inp_socket;
163 
164 COUNT(TCP_DROP);
165 	if (TCPS_HAVERCVDSYN(tp->t_state) &&
166 	    TCPS_OURFINNOTACKED(tp->t_state)) {
167 		tp->t_state = TCPS_CLOSED;
168 		tcp_output(tp);
169 	}
170 	so->so_error = errno;
171 	tcp_close(tp);
172 	in_pcbdetach(tp->t_inpcb);
173 }
174 
175 /*
176  * Close a TCP control block:
177  *	discard all space held by the tcp
178  *	discard internet protocol block
179  *	wake up any sleepers
180  */
181 tcp_close(tp)
182 	register struct tcpcb *tp;
183 {
184 	register struct tcpiphdr *t;
185 	struct socket *so = tp->t_inpcb->inp_socket;
186 
187 COUNT(TCP_CLOSE);
188 	t = tp->seg_next;
189 	for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next)
190 		m_freem(dtom(t));
191 	if (tp->t_template)
192 		(void) m_free(dtom(tp->t_template));
193 	if (tp->t_tcpopt)
194 		(void) m_free(dtom(tp->t_tcpopt));
195 	if (tp->t_ipopt)
196 		(void) m_free(dtom(tp->t_ipopt));
197 	(void) m_free(dtom(tp));
198 	socantrcvmore(so);
199 	socantsendmore(so);
200 	in_pcbdisconnect(tp->t_inpcb);
201 }
202 
203 tcp_drain()
204 {
205 
206 COUNT(TCP_DRAIN);
207 }
208 
209 tcp_ctlinput(m)
210 	struct mbuf *m;
211 {
212 
213 COUNT(TCP_CTLINPUT);
214 	m_freem(m);
215 }
216