• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)tcp_subr.c	8.2 (Berkeley) 5/24/95
30  */
31 
32 #include <errno.h>
33 #include <stddef.h>
34 #include <string.h>
35 
36 #include "../tcplp.h"
37 #include "ip.h"
38 #include "ip6.h"
39 #include "tcp.h"
40 #include "tcp_fsm.h"
41 #include "tcp_var.h"
42 #include "tcp_seq.h"
43 #include "tcp_timer.h"
44 #include "sys/queue.h"
45 #include "../lib/bitmap.h"
46 #include "../lib/cbuf.h"
47 #include "cc.h"
48 
49 #include "tcp_const.h"
50 
51 /*
52  * samkumar: This is rewritten to have the host network stack to generate the
53  * ISN with appropriate randomness.
54  */
tcp_new_isn(struct tcpcb * tp)55 tcp_seq tcp_new_isn(struct tcpcb* tp) {
56 	return (uint32_t) tcplp_sys_generate_isn();
57 }
58 
59 /*
60  * samkumar: There used to be a function, void tcp_init(void), that would
61  * initialize global state for TCP, including a hash table to store TCBs,
62  * allocating memory zones for sockets, and setting global configurable state.
63  * None of that is needed for TCPlp: TCB allocation and matching is done by
64  * the host system and global configurable state is removed with hardcoded
65  * values in order to save memory, for example. Thus, I've removed the function
66  * entirely.
67  */
68 
69 /*
70  * A subroutine which makes it easy to track TCP state changes with DTrace.
71  * This function shouldn't be called for t_state initializations that don't
72  * correspond to actual TCP state transitions.
73  */
74 void
tcp_state_change(struct tcpcb * tp,int newstate)75 tcp_state_change(struct tcpcb *tp, int newstate)
76 {
77 #if 0
78 #if defined(KDTRACE_HOOKS)
79 	int pstate = tp->t_state;
80 #endif
81 #endif
82 	tcplp_sys_log("Socket %p: %s --> %s", tp, tcpstates[tp->t_state], tcpstates[newstate]);
83 	tp->t_state = newstate;
84 
85 	// samkumar: may need to do other actions too, so call into the host
86 	tcplp_sys_on_state_change(tp, newstate);
87 #if 0
88 	TCP_PROBE6(state__change, NULL, tp, NULL, tp, NULL, pstate);
89 #endif
90 }
91 
92  /* samkumar: Based on tcp_newtcb in tcp_subr.c, and tcp_usr_attach in tcp_usrreq.c. */
initialize_tcb(struct tcpcb * tp)93 __attribute__((used)) void initialize_tcb(struct tcpcb* tp) {
94 	uint32_t ticks = tcplp_sys_get_ticks();
95 
96 	/* samkumar: Clear all fields starting laddr; rest are initialized by the host. */
97 	memset(((uint8_t*) tp) + offsetof(struct tcpcb, laddr), 0x00, sizeof(struct tcpcb) - offsetof(struct tcpcb, laddr));
98 	tp->reass_fin_index = -1;
99 
100 	/*
101 	 * samkumar: Only New Reno congestion control is implemented at the moment,
102 	 * so there's no need to record the congestion control algorithm used for
103 	 * each TCB.
104 	 */
105 	// CC_ALGO(tp) = CC_DEFAULT();
106 	// tp->ccv->type = IPPROTO_TCP;
107 	tp->ccv->ccvc.tcp = tp;
108 
109 	/*
110 	 * samkumar: The original code used to choose a different constant
111 	 * depending on whether it's an IPv4 or IPv6 connection. In TCPlp, we
112 	 * unconditionally choose the IPv6 branch.
113 	 */
114 	tp->t_maxseg = tp->t_maxopd =
115 //#ifdef INET6
116 		/*isipv6 ? */V_tcp_v6mssdflt /*:*/
117 //#endif /* INET6 */
118 		/*V_tcp_mssdflt*/;
119 
120 	if (V_tcp_do_rfc1323)
121 		tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
122 	if (V_tcp_do_sack)
123 		tp->t_flags |= TF_SACK_PERMIT;
124 	TAILQ_INIT(&tp->snd_holes);
125 
126 	/*
127 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
128 	 * rtt estimate.  Set rttvar so that srtt + 4 * rttvar gives
129 	 * reasonable initial retransmit time.
130 	 */
131 	tp->t_srtt = TCPTV_SRTTBASE;
132 	tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
133 	tp->t_rttmin = TCPTV_MIN < 1 ? 1 : TCPTV_MIN; /* samkumar: used to be tcp_rexmit_min, which was set in tcp_init */
134 	tp->t_rxtcur = TCPTV_RTOBASE;
135 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
136 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
137 	tp->t_rcvtime = ticks;
138 
139 	/* samkumar: Taken from tcp_usr_attach in tcp_usrreq.c. */
140 	tp->t_state = TCP6S_CLOSED;
141 
142 	/* samkumar: Added to initialize the per-TCP sackhole pool. */
143 	tcp_sack_init(tp);
144 }
145 
146 
147 /*
148  * samkumar: Most of this function was no longer needed. It did things like
149  * reference-counting for the TCB, updating host cache stats for better
150  * starting values of, e.g., ssthresh, for new connections, freeing resources
151  * for TCP offloading, etc. There's no host cache in TCPlp and the host system
152  * is responsible for managing TCB memory, so much of this isn't needed. I just
153  * kept (and adpated) the few parts of the function that appeared to be needed
154  * for TCPlp.
155  */
156 void
tcp_discardcb(struct tcpcb * tp)157 tcp_discardcb(struct tcpcb *tp)
158 {
159 	tcp_cancel_timers(tp);
160 
161 	/* Allow the CC algorithm to clean up after itself. */
162 	if (CC_ALGO(tp)->cb_destroy != NULL)
163 		CC_ALGO(tp)->cb_destroy(tp->ccv);
164 
165 	tcp_free_sackholes(tp);
166 }
167 
168 
169  /*
170  * Attempt to close a TCP control block, marking it as dropped, and freeing
171  * the socket if we hold the only reference.
172  */
173 /*
174  * samkumar: Most of this function has to do with dropping the reference to
175  * the inpcb, freeing resources at the socket layer and marking it as
176  * disconnected, and miscellaneous cleanup. I've rewritten this to do what is
177  * needed for TCP.
178  */
179 struct tcpcb *
tcp_close(struct tcpcb * tp)180 tcp_close(struct tcpcb *tp)
181 {
182 	tcp_state_change(tp, TCP6S_CLOSED); // for the print statement
183 	tcp_discardcb(tp);
184 	// Don't reset the TCB by calling initialize_tcb, since that overwrites the buffer contents.
185 	return tp;
186 }
187 
188 /*
189  * Create template to be used to send tcp packets on a connection.
190  * Allocates an mbuf and fills in a skeletal tcp/ip header.  The only
191  * use for this function is in keepalives, which use tcp_respond.
192  */
193 /* samkumar: I changed the signature of this function. Instead of allocating
194  * the struct tcptemp using malloc, populating it, and then returning it, I
195  * have the caller allocate it. This function merely populates it now.
196  */
197 void
tcpip_maketemplate(struct tcpcb * tp,struct tcptemp * t)198 tcpip_maketemplate(struct tcpcb* tp, struct tcptemp* t)
199 {
200 	tcpip_fillheaders(tp, (void *)&t->tt_ipgen, (void *)&t->tt_t);
201 }
202 
203 /*
204  * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb.
205  * tcp_template used to store this data in mbufs, but we now recopy it out
206  * of the tcpcb each time to conserve mbufs.
207  */
208 /*
209  * samkumar: This has a different signature from the original function in
210  * tcp_subr.c. In particular, IP header information is filled into an
211  * otMessageInfo rather than into a struct representing the on-wire header
212  * format. Additionally, I have changed it to always assume IPv6; I removed the
213  * code for IPv4.
214  */
215 void
tcpip_fillheaders(struct tcpcb * tp,otMessageInfo * ip_ptr,void * tcp_ptr)216 tcpip_fillheaders(struct tcpcb* tp, otMessageInfo* ip_ptr, void *tcp_ptr)
217 {
218 	struct tcphdr *th = (struct tcphdr *)tcp_ptr;
219 
220 	/* Fill in the IP header */
221 
222     /* samkumar: The old IPv6 code, for reference. */
223 	// ip6 = (struct ip6_hdr *)ip_ptr;
224 	// ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
225 	//     (inp->inp_flow & IPV6_FLOWINFO_MASK);
226 	// ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
227 	//     (IPV6_VERSION & IPV6_VERSION_MASK);
228 	// ip6->ip6_nxt = IPPROTO_TCP;
229 	// ip6->ip6_plen = htons(sizeof(struct tcphdr));
230 	// ip6->ip6_src = inp->in6p_laddr;
231 	// ip6->ip6_dst = inp->in6p_faddr;
232 
233 	memset(ip_ptr, 0x00, sizeof(otMessageInfo));
234 	memcpy(&ip_ptr->mSockAddr, &tp->laddr, sizeof(ip_ptr->mSockAddr));
235 	memcpy(&ip_ptr->mPeerAddr, &tp->faddr, sizeof(ip_ptr->mPeerAddr));
236 
237 	/* Fill in the TCP header */
238 	/* samkumar: I kept the old code commented out, for reference. */
239 	//th->th_sport = inp->inp_lport;
240 	//th->th_dport = inp->inp_fport;
241 	th->th_sport = tp->lport;
242 	th->th_dport = tp->fport;
243 	th->th_seq = 0;
244 	th->th_ack = 0;
245 	// th->th_x2 = 0;
246 	// th->th_off = 5;
247 	th->th_off_x2 = (5 << TH_OFF_SHIFT);
248 	th->th_flags = 0;
249 	th->th_win = 0;
250 	th->th_urp = 0;
251 	th->th_sum = 0;		/* in_pseudo() is called later for ipv4 */
252 }
253 
254 /*
255  * Send a single message to the TCP at address specified by
256  * the given TCP/IP header.  If m == NULL, then we make a copy
257  * of the tcpiphdr at th and send directly to the addressed host.
258  * This is used to force keep alive messages out using the TCP
259  * template for a connection.  If flags are given then we send
260  * a message back to the TCP which originated the segment th,
261  * and discard the mbuf containing it and any other attached mbufs.
262  *
263  * In any case the ack and sequence number of the transmitted
264  * segment are as specified by the parameters.
265  *
266  * NOTE: If m != NULL, then th must point to *inside* the mbuf.
267  */
268 /* samkumar: Original signature was
269 void
270 tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m,
271 	tcp_seq ack, tcp_seq seq, int flags)
272 */
273 /*
274  * samkumar: Essentially all of the code had to be discarded/rewritten since I
275  * have to send out packets by allocating buffers from the host system,
276  * populating them, and passing them back to the host system to send out. I
277  * simplified the code by only using the logic that was fully necessary,
278  * eliminating the code for IPv4 packets and keeping only the code for IPv6
279  * packets. I also removed all of the mbuf logic, instead using the logic for
280  * using the host system's buffering (in particular, the code to reuse the
281  * provided mbuf is no longer there).
282  */
283 void
tcp_respond(struct tcpcb * tp,otInstance * instance,struct ip6_hdr * ip6gen,struct tcphdr * thgen,tcp_seq ack,tcp_seq seq,int flags)284 tcp_respond(struct tcpcb *tp, otInstance* instance, struct ip6_hdr* ip6gen, struct tcphdr *thgen,
285 	tcp_seq ack, tcp_seq seq, int flags)
286 {
287 	otMessage* message = tcplp_sys_new_message(instance);
288 	if (message == NULL) {
289 		return;
290 	}
291 	if (otMessageSetLength(message, sizeof(struct tcphdr)) != OT_ERROR_NONE) {
292 		tcplp_sys_free_message(instance, message);
293 		return;
294 	}
295 
296 	struct tcphdr th;
297 	struct tcphdr* nth = &th;
298 	otMessageInfo ip6info;
299 	int win = 0;
300 	if (tp != NULL) {
301 		if (!(flags & TH_RST)) {
302 			win = cbuf_free_space(&tp->recvbuf);
303 			if (win > (long)TCP_MAXWIN << tp->rcv_scale)
304 				win = (long)TCP_MAXWIN << tp->rcv_scale;
305 		}
306 	}
307 	memset(&ip6info, 0x00, sizeof(otMessageInfo));
308 	memcpy(&ip6info.mSockAddr, &ip6gen->ip6_dst, sizeof(ip6info.mSockAddr));
309 	memcpy(&ip6info.mPeerAddr, &ip6gen->ip6_src, sizeof(ip6info.mPeerAddr));
310 	nth->th_sport = thgen->th_dport;
311 	nth->th_dport = thgen->th_sport;
312 	nth->th_seq = htonl(seq);
313 	nth->th_ack = htonl(ack);
314 	/* samkumar: original code for setting th_x2 and th_off, for reference. */
315 	// nth->th_x2 = 0;
316 	// nth->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
317 	nth->th_off_x2 = (sizeof(struct tcphdr) >> 2) << TH_OFF_SHIFT;
318 	nth->th_flags = flags;
319 	if (tp != NULL)
320 		nth->th_win = htons((uint16_t) (win >> tp->rcv_scale));
321 	else
322 		nth->th_win = htons((uint16_t)win);
323 	nth->th_urp = 0;
324 	nth->th_sum = 0;
325 
326 	otMessageWrite(message, 0, &th, sizeof(struct tcphdr));
327 
328 	tcplp_sys_send_message(instance, message, &ip6info);
329 }
330 
331 /*
332  * Drop a TCP connection, reporting
333  * the specified error.  If connection is synchronized,
334  * then send a RST to peer.
335  */
336 /*
337  * samkumar: I changed the parameter "errno" to "errnum" since it caused
338  * problems during compilation. I also the code for asserting locks,
339  * incermenting stats, and managing the sockets layer.
340  */
341 struct tcpcb *
tcp_drop(struct tcpcb * tp,int errnum)342 tcp_drop(struct tcpcb *tp, int errnum)
343 {
344 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
345 		tcp_state_change(tp, TCPS_CLOSED);
346 		(void) tcp_output(tp);
347 	}
348 	if (errnum == ETIMEDOUT && tp->t_softerror)
349 		errnum = tp->t_softerror;
350 	tp = tcp_close(tp);
351 	tcplp_sys_connection_lost(tp, errnum);
352 	return tp;
353 }
354 
355 /*
356  * Look-up the routing entry to the peer of this inpcb.  If no route
357  * is found and it cannot be allocated, then return 0.  This routine
358  * is called by TCP routines that access the rmx structure and by
359  * tcp_mss_update to get the peer/interface MTU.
360  */
361 /*
362  * samkumar: In TCPlp, we don't bother with keeping track of the MTU for each
363  * route. The MSS we choose for the 6LoWPAN/802.15.4 network is probably the
364  * bottleneck, so we just use that. (I also removed the struct in_conninfo *
365  * that was formerly the first argument).
366  */
367 uint64_t
tcp_maxmtu6(struct tcpcb * tp,struct tcp_ifcap * cap)368 tcp_maxmtu6(struct tcpcb* tp, struct tcp_ifcap *cap)
369 {
370 	uint64_t maxmtu = 0;
371 
372 	KASSERT (tp != NULL, ("tcp_maxmtu6 with NULL tcpcb pointer"));
373 	if (!IN6_IS_ADDR_UNSPECIFIED(&tp->faddr)) {
374 		maxmtu = FRAMES_PER_SEG * FRAMECAP_6LOWPAN;
375 	}
376 
377 	return (maxmtu);
378 }
379