1 |
583 |
jeremybenn |
/**
|
2 |
|
|
* \addtogroup uip
|
3 |
|
|
* @{
|
4 |
|
|
*/
|
5 |
|
|
|
6 |
|
|
/**
|
7 |
|
|
* \file
|
8 |
|
|
* The uIP TCP/IP stack code.
|
9 |
|
|
* \author Adam Dunkels <adam@dunkels.com>
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
|
|
/*
|
13 |
|
|
* Copyright (c) 2001-2003, Adam Dunkels.
|
14 |
|
|
* All rights reserved.
|
15 |
|
|
*
|
16 |
|
|
* Redistribution and use in source and binary forms, with or without
|
17 |
|
|
* modification, are permitted provided that the following conditions
|
18 |
|
|
* are met:
|
19 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
20 |
|
|
* notice, this list of conditions and the following disclaimer.
|
21 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
22 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
23 |
|
|
* documentation and/or other materials provided with the distribution.
|
24 |
|
|
* 3. The name of the author may not be used to endorse or promote
|
25 |
|
|
* products derived from this software without specific prior
|
26 |
|
|
* written permission.
|
27 |
|
|
*
|
28 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
29 |
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
30 |
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
31 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
32 |
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
33 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
34 |
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
35 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
36 |
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
37 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
38 |
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
39 |
|
|
*
|
40 |
|
|
* This file is part of the uIP TCP/IP stack.
|
41 |
|
|
*
|
42 |
|
|
* $Id: uip.c 2 2011-07-17 20:13:17Z filepang@gmail.com $
|
43 |
|
|
*
|
44 |
|
|
*/
|
45 |
|
|
|
46 |
|
|
/*
|
47 |
|
|
This is a small implementation of the IP and TCP protocols (as well as
|
48 |
|
|
some basic ICMP stuff). The implementation couples the IP, TCP and the
|
49 |
|
|
application layers very tightly. To keep the size of the compiled code
|
50 |
|
|
down, this code also features heavy usage of the goto statement.
|
51 |
|
|
|
52 |
|
|
The principle is that we have a small buffer, called the uip_buf, in
|
53 |
|
|
which the device driver puts an incoming packet. The TCP/IP stack
|
54 |
|
|
parses the headers in the packet, and calls upon the application. If
|
55 |
|
|
the remote host has sent data to the application, this data is present
|
56 |
|
|
in the uip_buf and the application read the data from there. It is up
|
57 |
|
|
to the application to put this data into a byte stream if needed. The
|
58 |
|
|
application will not be fed with data that is out of sequence.
|
59 |
|
|
|
60 |
|
|
If the application whishes to send data to the peer, it should put its
|
61 |
|
|
data into the uip_buf, 40 bytes from the start of the buffer. The
|
62 |
|
|
TCP/IP stack will calculate the checksums, and fill in the necessary
|
63 |
|
|
header fields and finally send the packet back to the peer.
|
64 |
|
|
*/
|
65 |
|
|
|
66 |
|
|
#include "uip.h"
|
67 |
|
|
#include "uipopt.h"
|
68 |
|
|
#include "uip_arch.h"
|
69 |
|
|
|
70 |
|
|
/*-----------------------------------------------------------------------------------*/
|
71 |
|
|
/* Variable definitions. */
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
/* The IP address of this host. If it is defined to be fixed (by setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set here. Otherwise, the address */
|
75 |
|
|
#if UIP_FIXEDADDR > 0
|
76 |
|
|
const u16_t uip_hostaddr[2] =
|
77 |
|
|
{HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1),
|
78 |
|
|
HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)};
|
79 |
|
|
const u16_t uip_arp_draddr[2] =
|
80 |
|
|
{HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1),
|
81 |
|
|
HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)};
|
82 |
|
|
const u16_t uip_arp_netmask[2] =
|
83 |
|
|
{HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1),
|
84 |
|
|
HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)};
|
85 |
|
|
#else
|
86 |
|
|
u16_t uip_hostaddr[2];
|
87 |
|
|
u16_t uip_arp_draddr[2], uip_arp_netmask[2];
|
88 |
|
|
#endif /* UIP_FIXEDADDR */
|
89 |
|
|
|
90 |
|
|
u8_t uip_buf[UIP_BUFSIZE+2]; /* The packet buffer that contains
|
91 |
|
|
incoming packets. */
|
92 |
|
|
volatile u8_t *uip_appdata; /* The uip_appdata pointer points to
|
93 |
|
|
application data. */
|
94 |
|
|
volatile u8_t *uip_sappdata; /* The uip_appdata pointer points to the
|
95 |
|
|
application data which is to be sent. */
|
96 |
|
|
#if UIP_URGDATA > 0
|
97 |
|
|
volatile u8_t *uip_urgdata; /* The uip_urgdata pointer points to
|
98 |
|
|
urgent data (out-of-band data), if
|
99 |
|
|
present. */
|
100 |
|
|
volatile u8_t uip_urglen, uip_surglen;
|
101 |
|
|
#endif /* UIP_URGDATA > 0 */
|
102 |
|
|
|
103 |
|
|
volatile u16_t uip_len, uip_slen;
|
104 |
|
|
/* The uip_len is either 8 or 16 bits,
|
105 |
|
|
depending on the maximum packet
|
106 |
|
|
size. */
|
107 |
|
|
|
108 |
|
|
volatile u8_t uip_flags; /* The uip_flags variable is used for
|
109 |
|
|
communication between the TCP/IP stack
|
110 |
|
|
and the application program. */
|
111 |
|
|
struct uip_conn *uip_conn; /* uip_conn always points to the current
|
112 |
|
|
connection. */
|
113 |
|
|
|
114 |
|
|
struct uip_conn uip_conns[UIP_CONNS];
|
115 |
|
|
/* The uip_conns array holds all TCP
|
116 |
|
|
connections. */
|
117 |
|
|
u16_t uip_listenports[UIP_LISTENPORTS];
|
118 |
|
|
/* The uip_listenports list all currently
|
119 |
|
|
listning ports. */
|
120 |
|
|
#if UIP_UDP
|
121 |
|
|
struct uip_udp_conn *uip_udp_conn;
|
122 |
|
|
struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
|
123 |
|
|
#endif /* UIP_UDP */
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
static u16_t ipid; /* Ths ipid variable is an increasing
|
127 |
|
|
number that is used for the IP ID
|
128 |
|
|
field. */
|
129 |
|
|
|
130 |
|
|
static u8_t iss[4]; /* The iss variable is used for the TCP
|
131 |
|
|
initial sequence number. */
|
132 |
|
|
|
133 |
|
|
#if UIP_ACTIVE_OPEN
|
134 |
|
|
static u16_t lastport; /* Keeps track of the last port used for
|
135 |
|
|
a new connection. */
|
136 |
|
|
#endif /* UIP_ACTIVE_OPEN */
|
137 |
|
|
|
138 |
|
|
/* Temporary variables. */
|
139 |
|
|
volatile u8_t uip_acc32[4];
|
140 |
|
|
static u8_t c, opt;
|
141 |
|
|
static u16_t tmp16;
|
142 |
|
|
|
143 |
|
|
/* Structures and definitions. */
|
144 |
|
|
#define TCP_FIN 0x01
|
145 |
|
|
#define TCP_SYN 0x02
|
146 |
|
|
#define TCP_RST 0x04
|
147 |
|
|
#define TCP_PSH 0x08
|
148 |
|
|
#define TCP_ACK 0x10
|
149 |
|
|
#define TCP_URG 0x20
|
150 |
|
|
#define TCP_CTL 0x3f
|
151 |
|
|
|
152 |
|
|
#define ICMP_ECHO_REPLY 0
|
153 |
|
|
#define ICMP_ECHO 8
|
154 |
|
|
|
155 |
|
|
/* Macros. */
|
156 |
|
|
#define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
|
157 |
|
|
#define FBUF ((uip_tcpip_hdr *)&uip_reassbuf[0])
|
158 |
|
|
#define ICMPBUF ((uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
|
159 |
|
|
#define UDPBUF ((uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
|
160 |
|
|
|
161 |
|
|
#if UIP_STATISTICS == 1
|
162 |
|
|
struct uip_stats uip_stat;
|
163 |
|
|
#define UIP_STAT(s) s
|
164 |
|
|
#else
|
165 |
|
|
#define UIP_STAT(s)
|
166 |
|
|
#endif /* UIP_STATISTICS == 1 */
|
167 |
|
|
|
168 |
|
|
#if UIP_LOGGING == 1
|
169 |
|
|
#include <stdio.h>
|
170 |
|
|
void uip_log(char *msg);
|
171 |
|
|
#define UIP_LOG(m) uip_log(m)
|
172 |
|
|
#else
|
173 |
|
|
#define UIP_LOG(m)
|
174 |
|
|
#endif /* UIP_LOGGING == 1 */
|
175 |
|
|
|
176 |
|
|
/*-----------------------------------------------------------------------------------*/
|
177 |
|
|
void
|
178 |
|
|
uip_init(void)
|
179 |
|
|
{
|
180 |
|
|
for(c = 0; c < UIP_LISTENPORTS; ++c) {
|
181 |
|
|
uip_listenports[c] = 0;
|
182 |
|
|
}
|
183 |
|
|
for(c = 0; c < UIP_CONNS; ++c) {
|
184 |
|
|
uip_conns[c].tcpstateflags = CLOSED;
|
185 |
|
|
}
|
186 |
|
|
#if UIP_ACTIVE_OPEN
|
187 |
|
|
lastport = 1024;
|
188 |
|
|
#endif /* UIP_ACTIVE_OPEN */
|
189 |
|
|
|
190 |
|
|
#if UIP_UDP
|
191 |
|
|
for(c = 0; c < UIP_UDP_CONNS; ++c) {
|
192 |
|
|
uip_udp_conns[c].lport = 0;
|
193 |
|
|
}
|
194 |
|
|
#endif /* UIP_UDP */
|
195 |
|
|
|
196 |
|
|
|
197 |
|
|
/* IPv4 initialization. */
|
198 |
|
|
#if UIP_FIXEDADDR == 0
|
199 |
|
|
uip_hostaddr[0] = uip_hostaddr[1] = 0;
|
200 |
|
|
#endif /* UIP_FIXEDADDR */
|
201 |
|
|
|
202 |
|
|
}
|
203 |
|
|
/*-----------------------------------------------------------------------------------*/
|
204 |
|
|
#if UIP_ACTIVE_OPEN
|
205 |
|
|
struct uip_conn *
|
206 |
|
|
uip_connect(u16_t *ripaddr, u16_t rport)
|
207 |
|
|
{
|
208 |
|
|
register struct uip_conn *conn, *cconn;
|
209 |
|
|
|
210 |
|
|
/* Find an unused local port. */
|
211 |
|
|
again:
|
212 |
|
|
++lastport;
|
213 |
|
|
|
214 |
|
|
if(lastport >= 32000) {
|
215 |
|
|
lastport = 4096;
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
/* Check if this port is already in use, and if so try to find
|
219 |
|
|
another one. */
|
220 |
|
|
for(c = 0; c < UIP_CONNS; ++c) {
|
221 |
|
|
conn = &uip_conns[c];
|
222 |
|
|
if(conn->tcpstateflags != CLOSED &&
|
223 |
|
|
conn->lport == htons(lastport)) {
|
224 |
|
|
goto again;
|
225 |
|
|
}
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
|
229 |
|
|
conn = 0;
|
230 |
|
|
for(c = 0; c < UIP_CONNS; ++c) {
|
231 |
|
|
cconn = &uip_conns[c];
|
232 |
|
|
if(cconn->tcpstateflags == CLOSED) {
|
233 |
|
|
conn = cconn;
|
234 |
|
|
break;
|
235 |
|
|
}
|
236 |
|
|
if(cconn->tcpstateflags == TIME_WAIT) {
|
237 |
|
|
if(conn == 0 ||
|
238 |
|
|
cconn->timer > uip_conn->timer) {
|
239 |
|
|
conn = cconn;
|
240 |
|
|
}
|
241 |
|
|
}
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
if(conn == 0) {
|
245 |
|
|
return 0;
|
246 |
|
|
}
|
247 |
|
|
|
248 |
|
|
conn->tcpstateflags = SYN_SENT;
|
249 |
|
|
|
250 |
|
|
conn->snd_nxt[0] = iss[0];
|
251 |
|
|
conn->snd_nxt[1] = iss[1];
|
252 |
|
|
conn->snd_nxt[2] = iss[2];
|
253 |
|
|
conn->snd_nxt[3] = iss[3];
|
254 |
|
|
|
255 |
|
|
conn->initialmss = conn->mss = UIP_TCP_MSS;
|
256 |
|
|
|
257 |
|
|
conn->len = 1; /* TCP length of the SYN is one. */
|
258 |
|
|
conn->nrtx = 0;
|
259 |
|
|
conn->timer = 1; /* Send the SYN next time around. */
|
260 |
|
|
conn->rto = UIP_RTO;
|
261 |
|
|
conn->sa = 0;
|
262 |
|
|
conn->sv = 16;
|
263 |
|
|
conn->lport = htons(lastport);
|
264 |
|
|
conn->rport = rport;
|
265 |
|
|
conn->ripaddr[0] = ripaddr[0];
|
266 |
|
|
conn->ripaddr[1] = ripaddr[1];
|
267 |
|
|
|
268 |
|
|
return conn;
|
269 |
|
|
}
|
270 |
|
|
#endif /* UIP_ACTIVE_OPEN */
|
271 |
|
|
/*-----------------------------------------------------------------------------------*/
|
272 |
|
|
#if UIP_UDP
|
273 |
|
|
struct uip_udp_conn *
|
274 |
|
|
uip_udp_new(u16_t *ripaddr, u16_t rport)
|
275 |
|
|
{
|
276 |
|
|
register struct uip_udp_conn *conn;
|
277 |
|
|
|
278 |
|
|
/* Find an unused local port. */
|
279 |
|
|
again:
|
280 |
|
|
++lastport;
|
281 |
|
|
|
282 |
|
|
if(lastport >= 32000) {
|
283 |
|
|
lastport = 4096;
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
for(c = 0; c < UIP_UDP_CONNS; ++c) {
|
287 |
|
|
if(uip_udp_conns[c].lport == lastport) {
|
288 |
|
|
goto again;
|
289 |
|
|
}
|
290 |
|
|
}
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
conn = 0;
|
294 |
|
|
for(c = 0; c < UIP_UDP_CONNS; ++c) {
|
295 |
|
|
if(uip_udp_conns[c].lport == 0) {
|
296 |
|
|
conn = &uip_udp_conns[c];
|
297 |
|
|
break;
|
298 |
|
|
}
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
if(conn == 0) {
|
302 |
|
|
return 0;
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
conn->lport = HTONS(lastport);
|
306 |
|
|
conn->rport = HTONS(rport);
|
307 |
|
|
conn->ripaddr[0] = ripaddr[0];
|
308 |
|
|
conn->ripaddr[1] = ripaddr[1];
|
309 |
|
|
|
310 |
|
|
return conn;
|
311 |
|
|
}
|
312 |
|
|
#endif /* UIP_UDP */
|
313 |
|
|
/*-----------------------------------------------------------------------------------*/
|
314 |
|
|
void
|
315 |
|
|
uip_unlisten(u16_t port)
|
316 |
|
|
{
|
317 |
|
|
for(c = 0; c < UIP_LISTENPORTS; ++c) {
|
318 |
|
|
if(uip_listenports[c] == port) {
|
319 |
|
|
uip_listenports[c] = 0;
|
320 |
|
|
return;
|
321 |
|
|
}
|
322 |
|
|
}
|
323 |
|
|
}
|
324 |
|
|
/*-----------------------------------------------------------------------------------*/
|
325 |
|
|
void
|
326 |
|
|
uip_listen(u16_t port)
|
327 |
|
|
{
|
328 |
|
|
for(c = 0; c < UIP_LISTENPORTS; ++c) {
|
329 |
|
|
if(uip_listenports[c] == 0) {
|
330 |
|
|
uip_listenports[c] = port;
|
331 |
|
|
return;
|
332 |
|
|
}
|
333 |
|
|
}
|
334 |
|
|
}
|
335 |
|
|
/*-----------------------------------------------------------------------------------*/
|
336 |
|
|
/* XXX: IP fragment reassembly: not well-tested. */
|
337 |
|
|
|
338 |
|
|
#if UIP_REASSEMBLY
|
339 |
|
|
#define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
|
340 |
|
|
static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];
|
341 |
|
|
static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
|
342 |
|
|
static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
|
343 |
|
|
0x0f, 0x07, 0x03, 0x01};
|
344 |
|
|
static u16_t uip_reasslen;
|
345 |
|
|
static u8_t uip_reassflags;
|
346 |
|
|
#define UIP_REASS_FLAG_LASTFRAG 0x01
|
347 |
|
|
static u8_t uip_reasstmr;
|
348 |
|
|
|
349 |
|
|
#define IP_HLEN 20
|
350 |
|
|
#define IP_MF 0x20
|
351 |
|
|
|
352 |
|
|
static u8_t
|
353 |
|
|
uip_reass(void)
|
354 |
|
|
{
|
355 |
|
|
u16_t offset, len;
|
356 |
|
|
u16_t i;
|
357 |
|
|
|
358 |
|
|
/* If ip_reasstmr is zero, no packet is present in the buffer, so we
|
359 |
|
|
write the IP header of the fragment into the reassembly
|
360 |
|
|
buffer. The timer is updated with the maximum age. */
|
361 |
|
|
if(uip_reasstmr == 0) {
|
362 |
|
|
memcpy(uip_reassbuf, &BUF->vhl, IP_HLEN);
|
363 |
|
|
uip_reasstmr = UIP_REASS_MAXAGE;
|
364 |
|
|
uip_reassflags = 0;
|
365 |
|
|
/* Clear the bitmap. */
|
366 |
|
|
memset(uip_reassbitmap, sizeof(uip_reassbitmap), 0);
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
/* Check if the incoming fragment matches the one currently present
|
370 |
|
|
in the reasembly buffer. If so, we proceed with copying the
|
371 |
|
|
fragment into the buffer. */
|
372 |
|
|
if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&
|
373 |
|
|
BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
|
374 |
|
|
BUF->destipaddr[0] == FBUF->destipaddr[0] &&
|
375 |
|
|
BUF->destipaddr[1] == FBUF->destipaddr[1] &&
|
376 |
|
|
BUF->ipid[0] == FBUF->ipid[0] &&
|
377 |
|
|
BUF->ipid[1] == FBUF->ipid[1]) {
|
378 |
|
|
|
379 |
|
|
len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
|
380 |
|
|
offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
|
381 |
|
|
|
382 |
|
|
/* If the offset or the offset + fragment length overflows the
|
383 |
|
|
reassembly buffer, we discard the entire packet. */
|
384 |
|
|
if(offset > UIP_REASS_BUFSIZE ||
|
385 |
|
|
offset + len > UIP_REASS_BUFSIZE) {
|
386 |
|
|
uip_reasstmr = 0;
|
387 |
|
|
goto nullreturn;
|
388 |
|
|
}
|
389 |
|
|
|
390 |
|
|
/* Copy the fragment into the reassembly buffer, at the right
|
391 |
|
|
offset. */
|
392 |
|
|
memcpy(&uip_reassbuf[IP_HLEN + offset],
|
393 |
|
|
(char *)BUF + (int)((BUF->vhl & 0x0f) * 4),
|
394 |
|
|
len);
|
395 |
|
|
|
396 |
|
|
/* Update the bitmap. */
|
397 |
|
|
if(offset / (8 * 8) == (offset + len) / (8 * 8)) {
|
398 |
|
|
/* If the two endpoints are in the same byte, we only update
|
399 |
|
|
that byte. */
|
400 |
|
|
|
401 |
|
|
uip_reassbitmap[offset / (8 * 8)] |=
|
402 |
|
|
bitmap_bits[(offset / 8 ) & 7] &
|
403 |
|
|
~bitmap_bits[((offset + len) / 8 ) & 7];
|
404 |
|
|
} else {
|
405 |
|
|
/* If the two endpoints are in different bytes, we update the
|
406 |
|
|
bytes in the endpoints and fill the stuff inbetween with
|
407 |
|
|
0xff. */
|
408 |
|
|
uip_reassbitmap[offset / (8 * 8)] |=
|
409 |
|
|
bitmap_bits[(offset / 8 ) & 7];
|
410 |
|
|
for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
|
411 |
|
|
uip_reassbitmap[i] = 0xff;
|
412 |
|
|
}
|
413 |
|
|
uip_reassbitmap[(offset + len) / (8 * 8)] |=
|
414 |
|
|
~bitmap_bits[((offset + len) / 8 ) & 7];
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
/* If this fragment has the More Fragments flag set to zero, we
|
418 |
|
|
know that this is the last fragment, so we can calculate the
|
419 |
|
|
size of the entire packet. We also set the
|
420 |
|
|
IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
|
421 |
|
|
the final fragment. */
|
422 |
|
|
|
423 |
|
|
if((BUF->ipoffset[0] & IP_MF) == 0) {
|
424 |
|
|
uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
|
425 |
|
|
uip_reasslen = offset + len;
|
426 |
|
|
}
|
427 |
|
|
|
428 |
|
|
/* Finally, we check if we have a full packet in the buffer. We do
|
429 |
|
|
this by checking if we have the last fragment and if all bits
|
430 |
|
|
in the bitmap are set. */
|
431 |
|
|
if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
|
432 |
|
|
/* Check all bytes up to and including all but the last byte in
|
433 |
|
|
the bitmap. */
|
434 |
|
|
for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {
|
435 |
|
|
if(uip_reassbitmap[i] != 0xff) {
|
436 |
|
|
goto nullreturn;
|
437 |
|
|
}
|
438 |
|
|
}
|
439 |
|
|
/* Check the last byte in the bitmap. It should contain just the
|
440 |
|
|
right amount of bits. */
|
441 |
|
|
if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=
|
442 |
|
|
(u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {
|
443 |
|
|
goto nullreturn;
|
444 |
|
|
}
|
445 |
|
|
|
446 |
|
|
/* If we have come this far, we have a full packet in the
|
447 |
|
|
buffer, so we allocate a pbuf and copy the packet into it. We
|
448 |
|
|
also reset the timer. */
|
449 |
|
|
uip_reasstmr = 0;
|
450 |
|
|
memcpy(BUF, FBUF, uip_reasslen);
|
451 |
|
|
|
452 |
|
|
/* Pretend to be a "normal" (i.e., not fragmented) IP packet
|
453 |
|
|
from now on. */
|
454 |
|
|
BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
|
455 |
|
|
BUF->len[0] = uip_reasslen >> 8;
|
456 |
|
|
BUF->len[1] = uip_reasslen & 0xff;
|
457 |
|
|
BUF->ipchksum = 0;
|
458 |
|
|
BUF->ipchksum = ~(uip_ipchksum());
|
459 |
|
|
|
460 |
|
|
return uip_reasslen;
|
461 |
|
|
}
|
462 |
|
|
}
|
463 |
|
|
|
464 |
|
|
nullreturn:
|
465 |
|
|
return 0;
|
466 |
|
|
}
|
467 |
|
|
#endif /* UIP_REASSEMBL */
|
468 |
|
|
/*-----------------------------------------------------------------------------------*/
|
469 |
|
|
static void
|
470 |
|
|
uip_add_rcv_nxt(u16_t n)
|
471 |
|
|
{
|
472 |
|
|
uip_add32(uip_conn->rcv_nxt, n);
|
473 |
|
|
uip_conn->rcv_nxt[0] = uip_acc32[0];
|
474 |
|
|
uip_conn->rcv_nxt[1] = uip_acc32[1];
|
475 |
|
|
uip_conn->rcv_nxt[2] = uip_acc32[2];
|
476 |
|
|
uip_conn->rcv_nxt[3] = uip_acc32[3];
|
477 |
|
|
}
|
478 |
|
|
/*-----------------------------------------------------------------------------------*/
|
479 |
|
|
void
|
480 |
|
|
uip_process(u8_t flag)
|
481 |
|
|
{
|
482 |
|
|
register struct uip_conn *uip_connr = uip_conn;
|
483 |
|
|
|
484 |
|
|
uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
|
485 |
|
|
|
486 |
|
|
|
487 |
|
|
/* Check if we were invoked because of the perodic timer fireing. */
|
488 |
|
|
if(flag == UIP_TIMER) {
|
489 |
|
|
#if UIP_REASSEMBLY
|
490 |
|
|
if(uip_reasstmr != 0) {
|
491 |
|
|
--uip_reasstmr;
|
492 |
|
|
}
|
493 |
|
|
#endif /* UIP_REASSEMBLY */
|
494 |
|
|
/* Increase the initial sequence number. */
|
495 |
|
|
if(++iss[3] == 0) {
|
496 |
|
|
if(++iss[2] == 0) {
|
497 |
|
|
if(++iss[1] == 0) {
|
498 |
|
|
++iss[0];
|
499 |
|
|
}
|
500 |
|
|
}
|
501 |
|
|
}
|
502 |
|
|
uip_len = 0;
|
503 |
|
|
if(uip_connr->tcpstateflags == TIME_WAIT ||
|
504 |
|
|
uip_connr->tcpstateflags == FIN_WAIT_2) {
|
505 |
|
|
++(uip_connr->timer);
|
506 |
|
|
if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
|
507 |
|
|
uip_connr->tcpstateflags = CLOSED;
|
508 |
|
|
}
|
509 |
|
|
} else if(uip_connr->tcpstateflags != CLOSED) {
|
510 |
|
|
/* If the connection has outstanding data, we increase the
|
511 |
|
|
connection's timer and see if it has reached the RTO value
|
512 |
|
|
in which case we retransmit. */
|
513 |
|
|
if(uip_outstanding(uip_connr)) {
|
514 |
|
|
if(uip_connr->timer-- == 0) {
|
515 |
|
|
if(uip_connr->nrtx == UIP_MAXRTX ||
|
516 |
|
|
((uip_connr->tcpstateflags == SYN_SENT ||
|
517 |
|
|
uip_connr->tcpstateflags == SYN_RCVD) &&
|
518 |
|
|
uip_connr->nrtx == UIP_MAXSYNRTX)) {
|
519 |
|
|
uip_connr->tcpstateflags = CLOSED;
|
520 |
|
|
|
521 |
|
|
/* We call UIP_APPCALL() with uip_flags set to
|
522 |
|
|
UIP_TIMEDOUT to inform the application that the
|
523 |
|
|
connection has timed out. */
|
524 |
|
|
uip_flags = UIP_TIMEDOUT;
|
525 |
|
|
UIP_APPCALL();
|
526 |
|
|
|
527 |
|
|
/* We also send a reset packet to the remote host. */
|
528 |
|
|
BUF->flags = TCP_RST | TCP_ACK;
|
529 |
|
|
goto tcp_send_nodata;
|
530 |
|
|
}
|
531 |
|
|
|
532 |
|
|
/* Exponential backoff. */
|
533 |
|
|
uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
|
534 |
|
|
4:
|
535 |
|
|
uip_connr->nrtx);
|
536 |
|
|
++(uip_connr->nrtx);
|
537 |
|
|
|
538 |
|
|
/* Ok, so we need to retransmit. We do this differently
|
539 |
|
|
depending on which state we are in. In ESTABLISHED, we
|
540 |
|
|
call upon the application so that it may prepare the
|
541 |
|
|
data for the retransmit. In SYN_RCVD, we resend the
|
542 |
|
|
SYNACK that we sent earlier and in LAST_ACK we have to
|
543 |
|
|
retransmit our FINACK. */
|
544 |
|
|
UIP_STAT(++uip_stat.tcp.rexmit);
|
545 |
|
|
switch(uip_connr->tcpstateflags & TS_MASK) {
|
546 |
|
|
case SYN_RCVD:
|
547 |
|
|
/* In the SYN_RCVD state, we should retransmit our
|
548 |
|
|
SYNACK. */
|
549 |
|
|
goto tcp_send_synack;
|
550 |
|
|
|
551 |
|
|
#if UIP_ACTIVE_OPEN
|
552 |
|
|
case SYN_SENT:
|
553 |
|
|
/* In the SYN_SENT state, we retransmit out SYN. */
|
554 |
|
|
BUF->flags = 0;
|
555 |
|
|
goto tcp_send_syn;
|
556 |
|
|
#endif /* UIP_ACTIVE_OPEN */
|
557 |
|
|
|
558 |
|
|
case ESTABLISHED:
|
559 |
|
|
/* In the ESTABLISHED state, we call upon the application
|
560 |
|
|
to do the actual retransmit after which we jump into
|
561 |
|
|
the code for sending out the packet (the apprexmit
|
562 |
|
|
label). */
|
563 |
|
|
uip_len = 0;
|
564 |
|
|
uip_slen = 0;
|
565 |
|
|
uip_flags = UIP_REXMIT;
|
566 |
|
|
UIP_APPCALL();
|
567 |
|
|
goto apprexmit;
|
568 |
|
|
|
569 |
|
|
case FIN_WAIT_1:
|
570 |
|
|
case CLOSING:
|
571 |
|
|
case LAST_ACK:
|
572 |
|
|
/* In all these states we should retransmit a FINACK. */
|
573 |
|
|
goto tcp_send_finack;
|
574 |
|
|
|
575 |
|
|
}
|
576 |
|
|
}
|
577 |
|
|
} else if((uip_connr->tcpstateflags & TS_MASK) == ESTABLISHED) {
|
578 |
|
|
/* If there was no need for a retransmission, we poll the
|
579 |
|
|
application for new data. */
|
580 |
|
|
uip_len = 0;
|
581 |
|
|
uip_slen = 0;
|
582 |
|
|
uip_flags = UIP_POLL;
|
583 |
|
|
UIP_APPCALL();
|
584 |
|
|
goto appsend;
|
585 |
|
|
}
|
586 |
|
|
}
|
587 |
|
|
goto drop;
|
588 |
|
|
}
|
589 |
|
|
#if UIP_UDP
|
590 |
|
|
if(flag == UIP_UDP_TIMER) {
|
591 |
|
|
if(uip_udp_conn->lport != 0) {
|
592 |
|
|
uip_appdata = &uip_buf[UIP_LLH_LEN + 28];
|
593 |
|
|
uip_len = uip_slen = 0;
|
594 |
|
|
uip_flags = UIP_POLL;
|
595 |
|
|
UIP_UDP_APPCALL();
|
596 |
|
|
goto udp_send;
|
597 |
|
|
} else {
|
598 |
|
|
goto drop;
|
599 |
|
|
}
|
600 |
|
|
}
|
601 |
|
|
#endif
|
602 |
|
|
|
603 |
|
|
/* This is where the input processing starts. */
|
604 |
|
|
UIP_STAT(++uip_stat.ip.recv);
|
605 |
|
|
|
606 |
|
|
|
607 |
|
|
/* Start of IPv4 input header processing code. */
|
608 |
|
|
|
609 |
|
|
/* Check validity of the IP header. */
|
610 |
|
|
if(BUF->vhl != 0x45) { /* IP version and header length. */
|
611 |
|
|
UIP_STAT(++uip_stat.ip.drop);
|
612 |
|
|
UIP_STAT(++uip_stat.ip.vhlerr);
|
613 |
|
|
UIP_LOG("ip: invalid version or header length.");
|
614 |
|
|
goto drop;
|
615 |
|
|
}
|
616 |
|
|
|
617 |
|
|
/* Check the size of the packet. If the size reported to us in
|
618 |
|
|
uip_len doesn't match the size reported in the IP header, there
|
619 |
|
|
has been a transmission error and we drop the packet. */
|
620 |
|
|
|
621 |
|
|
if(BUF->len[0] != (uip_len >> 8)) { /* IP length, high byte. */
|
622 |
|
|
uip_len = (uip_len & 0xff) | (BUF->len[0] << 8);
|
623 |
|
|
}
|
624 |
|
|
if(BUF->len[1] != (uip_len & 0xff)) { /* IP length, low byte. */
|
625 |
|
|
uip_len = (uip_len & 0xff00) | BUF->len[1];
|
626 |
|
|
}
|
627 |
|
|
|
628 |
|
|
/* Check the fragment flag. */
|
629 |
|
|
if((BUF->ipoffset[0] & 0x3f) != 0 ||
|
630 |
|
|
BUF->ipoffset[1] != 0) {
|
631 |
|
|
#if UIP_REASSEMBLY
|
632 |
|
|
uip_len = uip_reass();
|
633 |
|
|
if(uip_len == 0) {
|
634 |
|
|
goto drop;
|
635 |
|
|
}
|
636 |
|
|
#else
|
637 |
|
|
UIP_STAT(++uip_stat.ip.drop);
|
638 |
|
|
UIP_STAT(++uip_stat.ip.fragerr);
|
639 |
|
|
UIP_LOG("ip: fragment dropped.");
|
640 |
|
|
goto drop;
|
641 |
|
|
#endif /* UIP_REASSEMBLY */
|
642 |
|
|
}
|
643 |
|
|
|
644 |
|
|
/* If we are configured to use ping IP address configuration and
|
645 |
|
|
hasn't been assigned an IP address yet, we accept all ICMP
|
646 |
|
|
packets. */
|
647 |
|
|
#if UIP_PINGADDRCONF
|
648 |
|
|
if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
|
649 |
|
|
if(BUF->proto == UIP_PROTO_ICMP) {
|
650 |
|
|
UIP_LOG("ip: possible ping config packet received.");
|
651 |
|
|
goto icmp_input;
|
652 |
|
|
} else {
|
653 |
|
|
UIP_LOG("ip: packet dropped since no address assigned.");
|
654 |
|
|
goto drop;
|
655 |
|
|
}
|
656 |
|
|
}
|
657 |
|
|
#endif /* UIP_PINGADDRCONF */
|
658 |
|
|
|
659 |
|
|
/* Check if the packet is destined for our IP address. */
|
660 |
|
|
if(BUF->destipaddr[0] != uip_hostaddr[0]) {
|
661 |
|
|
UIP_STAT(++uip_stat.ip.drop);
|
662 |
|
|
UIP_LOG("ip: packet not for us.");
|
663 |
|
|
goto drop;
|
664 |
|
|
}
|
665 |
|
|
if(BUF->destipaddr[1] != uip_hostaddr[1]) {
|
666 |
|
|
UIP_STAT(++uip_stat.ip.drop);
|
667 |
|
|
UIP_LOG("ip: packet not for us.");
|
668 |
|
|
goto drop;
|
669 |
|
|
}
|
670 |
|
|
|
671 |
|
|
#if 0
|
672 |
|
|
// IP checksum is wrong through Netgear DSL router
|
673 |
|
|
if (uip_ipchksum() != 0xffff) { /* Compute and check the IP header
|
674 |
|
|
checksum. */
|
675 |
|
|
UIP_STAT(++uip_stat.ip.drop);
|
676 |
|
|
UIP_STAT(++uip_stat.ip.chkerr);
|
677 |
|
|
UIP_LOG("ip: bad checksum.");
|
678 |
|
|
goto drop;
|
679 |
|
|
}
|
680 |
|
|
#endif
|
681 |
|
|
|
682 |
|
|
if(BUF->proto == UIP_PROTO_TCP) /* Check for TCP packet. If so, jump
|
683 |
|
|
to the tcp_input label. */
|
684 |
|
|
goto tcp_input;
|
685 |
|
|
|
686 |
|
|
#if UIP_UDP
|
687 |
|
|
if(BUF->proto == UIP_PROTO_UDP)
|
688 |
|
|
goto udp_input;
|
689 |
|
|
#endif /* UIP_UDP */
|
690 |
|
|
|
691 |
|
|
if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from
|
692 |
|
|
here. */
|
693 |
|
|
UIP_STAT(++uip_stat.ip.drop);
|
694 |
|
|
UIP_STAT(++uip_stat.ip.protoerr);
|
695 |
|
|
UIP_LOG("ip: neither tcp nor icmp.");
|
696 |
|
|
goto drop;
|
697 |
|
|
}
|
698 |
|
|
|
699 |
|
|
icmp_input:
|
700 |
|
|
UIP_STAT(++uip_stat.icmp.recv);
|
701 |
|
|
|
702 |
|
|
/* ICMP echo (i.e., ping) processing. This is simple, we only change
|
703 |
|
|
the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
|
704 |
|
|
checksum before we return the packet. */
|
705 |
|
|
if(ICMPBUF->type != ICMP_ECHO) {
|
706 |
|
|
UIP_STAT(++uip_stat.icmp.drop);
|
707 |
|
|
UIP_STAT(++uip_stat.icmp.typeerr);
|
708 |
|
|
UIP_LOG("icmp: not icmp echo.");
|
709 |
|
|
goto drop;
|
710 |
|
|
}
|
711 |
|
|
|
712 |
|
|
/* If we are configured to use ping IP address assignment, we use
|
713 |
|
|
the destination IP address of this ping packet and assign it to
|
714 |
|
|
ourself. */
|
715 |
|
|
#if UIP_PINGADDRCONF
|
716 |
|
|
if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
|
717 |
|
|
uip_hostaddr[0] = BUF->destipaddr[0];
|
718 |
|
|
uip_hostaddr[1] = BUF->destipaddr[1];
|
719 |
|
|
}
|
720 |
|
|
#endif /* UIP_PINGADDRCONF */
|
721 |
|
|
|
722 |
|
|
ICMPBUF->type = ICMP_ECHO_REPLY;
|
723 |
|
|
|
724 |
|
|
if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {
|
725 |
|
|
ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
|
726 |
|
|
} else {
|
727 |
|
|
ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);
|
728 |
|
|
}
|
729 |
|
|
|
730 |
|
|
/* Swap IP addresses. */
|
731 |
|
|
tmp16 = BUF->destipaddr[0];
|
732 |
|
|
BUF->destipaddr[0] = BUF->srcipaddr[0];
|
733 |
|
|
BUF->srcipaddr[0] = tmp16;
|
734 |
|
|
tmp16 = BUF->destipaddr[1];
|
735 |
|
|
BUF->destipaddr[1] = BUF->srcipaddr[1];
|
736 |
|
|
BUF->srcipaddr[1] = tmp16;
|
737 |
|
|
|
738 |
|
|
UIP_STAT(++uip_stat.icmp.sent);
|
739 |
|
|
goto send;
|
740 |
|
|
|
741 |
|
|
/* End of IPv4 input header processing code. */
|
742 |
|
|
|
743 |
|
|
|
744 |
|
|
#if UIP_UDP
|
745 |
|
|
/* UDP input processing. */
|
746 |
|
|
udp_input:
|
747 |
|
|
/* UDP processing is really just a hack. We don't do anything to the
|
748 |
|
|
UDP/IP headers, but let the UDP application do all the hard
|
749 |
|
|
work. If the application sets uip_slen, it has a packet to
|
750 |
|
|
send. */
|
751 |
|
|
#if UIP_UDP_CHECKSUMS
|
752 |
|
|
if(uip_udpchksum() != 0xffff) {
|
753 |
|
|
UIP_STAT(++uip_stat.udp.drop);
|
754 |
|
|
UIP_STAT(++uip_stat.udp.chkerr);
|
755 |
|
|
UIP_LOG("udp: bad checksum.");
|
756 |
|
|
goto drop;
|
757 |
|
|
}
|
758 |
|
|
#endif /* UIP_UDP_CHECKSUMS */
|
759 |
|
|
|
760 |
|
|
/* Demultiplex this UDP packet between the UDP "connections". */
|
761 |
|
|
for(uip_udp_conn = &uip_udp_conns[0];
|
762 |
|
|
uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
|
763 |
|
|
++uip_udp_conn) {
|
764 |
|
|
if(uip_udp_conn->lport != 0 &&
|
765 |
|
|
UDPBUF->destport == uip_udp_conn->lport &&
|
766 |
|
|
(uip_udp_conn->rport == 0 ||
|
767 |
|
|
UDPBUF->srcport == uip_udp_conn->rport) &&
|
768 |
|
|
BUF->srcipaddr[0] == uip_udp_conn->ripaddr[0] &&
|
769 |
|
|
BUF->srcipaddr[1] == uip_udp_conn->ripaddr[1]) {
|
770 |
|
|
goto udp_found;
|
771 |
|
|
}
|
772 |
|
|
}
|
773 |
|
|
goto drop;
|
774 |
|
|
|
775 |
|
|
udp_found:
|
776 |
|
|
uip_len = uip_len - 28;
|
777 |
|
|
uip_appdata = &uip_buf[UIP_LLH_LEN + 28];
|
778 |
|
|
uip_flags = UIP_NEWDATA;
|
779 |
|
|
uip_slen = 0;
|
780 |
|
|
UIP_UDP_APPCALL();
|
781 |
|
|
udp_send:
|
782 |
|
|
if(uip_slen == 0) {
|
783 |
|
|
goto drop;
|
784 |
|
|
}
|
785 |
|
|
uip_len = uip_slen + 28;
|
786 |
|
|
|
787 |
|
|
BUF->len[0] = (uip_len >> 8);
|
788 |
|
|
BUF->len[1] = (uip_len & 0xff);
|
789 |
|
|
|
790 |
|
|
BUF->proto = UIP_PROTO_UDP;
|
791 |
|
|
|
792 |
|
|
UDPBUF->udplen = HTONS(uip_slen + 8);
|
793 |
|
|
UDPBUF->udpchksum = 0;
|
794 |
|
|
#if UIP_UDP_CHECKSUMS
|
795 |
|
|
/* Calculate UDP checksum. */
|
796 |
|
|
UDPBUF->udpchksum = ~(uip_udpchksum());
|
797 |
|
|
if(UDPBUF->udpchksum == 0) {
|
798 |
|
|
UDPBUF->udpchksum = 0xffff;
|
799 |
|
|
}
|
800 |
|
|
#endif /* UIP_UDP_CHECKSUMS */
|
801 |
|
|
|
802 |
|
|
BUF->srcport = uip_udp_conn->lport;
|
803 |
|
|
BUF->destport = uip_udp_conn->rport;
|
804 |
|
|
|
805 |
|
|
BUF->srcipaddr[0] = uip_hostaddr[0];
|
806 |
|
|
BUF->srcipaddr[1] = uip_hostaddr[1];
|
807 |
|
|
BUF->destipaddr[0] = uip_udp_conn->ripaddr[0];
|
808 |
|
|
BUF->destipaddr[1] = uip_udp_conn->ripaddr[1];
|
809 |
|
|
|
810 |
|
|
uip_appdata = &uip_buf[UIP_LLH_LEN + 40];
|
811 |
|
|
goto ip_send_nolen;
|
812 |
|
|
#endif /* UIP_UDP */
|
813 |
|
|
|
814 |
|
|
/* TCP input processing. */
|
815 |
|
|
tcp_input:
|
816 |
|
|
UIP_STAT(++uip_stat.tcp.recv);
|
817 |
|
|
|
818 |
|
|
/* Start of TCP input header processing code. */
|
819 |
|
|
|
820 |
|
|
#if 1 // FIXME
|
821 |
|
|
if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
|
822 |
|
|
checksum. */
|
823 |
|
|
UIP_STAT(++uip_stat.tcp.drop);
|
824 |
|
|
UIP_STAT(++uip_stat.tcp.chkerr);
|
825 |
|
|
UIP_LOG("tcp: bad checksum.");
|
826 |
|
|
goto drop;
|
827 |
|
|
}
|
828 |
|
|
#endif
|
829 |
|
|
|
830 |
|
|
/* Demultiplex this segment. */
|
831 |
|
|
/* First check any active connections. */
|
832 |
|
|
for(uip_connr = &uip_conns[0]; uip_connr < &uip_conns[UIP_CONNS]; ++uip_connr) {
|
833 |
|
|
if(uip_connr->tcpstateflags != CLOSED &&
|
834 |
|
|
BUF->destport == uip_connr->lport &&
|
835 |
|
|
BUF->srcport == uip_connr->rport &&
|
836 |
|
|
BUF->srcipaddr[0] == uip_connr->ripaddr[0] &&
|
837 |
|
|
BUF->srcipaddr[1] == uip_connr->ripaddr[1]) {
|
838 |
|
|
goto found;
|
839 |
|
|
}
|
840 |
|
|
}
|
841 |
|
|
|
842 |
|
|
/* If we didn't find and active connection that expected the packet,
|
843 |
|
|
either this packet is an old duplicate, or this is a SYN packet
|
844 |
|
|
destined for a connection in LISTEN. If the SYN flag isn't set,
|
845 |
|
|
it is an old packet and we send a RST. */
|
846 |
|
|
if((BUF->flags & TCP_CTL) != TCP_SYN)
|
847 |
|
|
goto reset;
|
848 |
|
|
|
849 |
|
|
tmp16 = BUF->destport;
|
850 |
|
|
/* Next, check listening connections. */
|
851 |
|
|
for(c = 0; c < UIP_LISTENPORTS; ++c) {
|
852 |
|
|
if(tmp16 == uip_listenports[c])
|
853 |
|
|
goto found_listen;
|
854 |
|
|
}
|
855 |
|
|
|
856 |
|
|
/* No matching connection found, so we send a RST packet. */
|
857 |
|
|
UIP_STAT(++uip_stat.tcp.synrst);
|
858 |
|
|
reset:
|
859 |
|
|
|
860 |
|
|
/* We do not send resets in response to resets. */
|
861 |
|
|
if(BUF->flags & TCP_RST)
|
862 |
|
|
goto drop;
|
863 |
|
|
|
864 |
|
|
UIP_STAT(++uip_stat.tcp.rst);
|
865 |
|
|
|
866 |
|
|
BUF->flags = TCP_RST | TCP_ACK;
|
867 |
|
|
uip_len = 40;
|
868 |
|
|
BUF->tcpoffset = 5 << 4;
|
869 |
|
|
|
870 |
|
|
/* Flip the seqno and ackno fields in the TCP header. */
|
871 |
|
|
c = BUF->seqno[3];
|
872 |
|
|
BUF->seqno[3] = BUF->ackno[3];
|
873 |
|
|
BUF->ackno[3] = c;
|
874 |
|
|
|
875 |
|
|
c = BUF->seqno[2];
|
876 |
|
|
BUF->seqno[2] = BUF->ackno[2];
|
877 |
|
|
BUF->ackno[2] = c;
|
878 |
|
|
|
879 |
|
|
c = BUF->seqno[1];
|
880 |
|
|
BUF->seqno[1] = BUF->ackno[1];
|
881 |
|
|
BUF->ackno[1] = c;
|
882 |
|
|
|
883 |
|
|
c = BUF->seqno[0];
|
884 |
|
|
BUF->seqno[0] = BUF->ackno[0];
|
885 |
|
|
BUF->ackno[0] = c;
|
886 |
|
|
|
887 |
|
|
/* We also have to increase the sequence number we are
|
888 |
|
|
acknowledging. If the least significant byte overflowed, we need
|
889 |
|
|
to propagate the carry to the other bytes as well. */
|
890 |
|
|
if(++BUF->ackno[3] == 0) {
|
891 |
|
|
if(++BUF->ackno[2] == 0) {
|
892 |
|
|
if(++BUF->ackno[1] == 0) {
|
893 |
|
|
++BUF->ackno[0];
|
894 |
|
|
}
|
895 |
|
|
}
|
896 |
|
|
}
|
897 |
|
|
|
898 |
|
|
/* Swap port numbers. */
|
899 |
|
|
tmp16 = BUF->srcport;
|
900 |
|
|
BUF->srcport = BUF->destport;
|
901 |
|
|
BUF->destport = tmp16;
|
902 |
|
|
|
903 |
|
|
/* Swap IP addresses. */
|
904 |
|
|
tmp16 = BUF->destipaddr[0];
|
905 |
|
|
BUF->destipaddr[0] = BUF->srcipaddr[0];
|
906 |
|
|
BUF->srcipaddr[0] = tmp16;
|
907 |
|
|
tmp16 = BUF->destipaddr[1];
|
908 |
|
|
BUF->destipaddr[1] = BUF->srcipaddr[1];
|
909 |
|
|
BUF->srcipaddr[1] = tmp16;
|
910 |
|
|
|
911 |
|
|
|
912 |
|
|
/* And send out the RST packet! */
|
913 |
|
|
goto tcp_send_noconn;
|
914 |
|
|
|
915 |
|
|
/* This label will be jumped to if we matched the incoming packet
|
916 |
|
|
with a connection in LISTEN. In that case, we should create a new
|
917 |
|
|
connection and send a SYNACK in return. */
|
918 |
|
|
found_listen:
|
919 |
|
|
/* First we check if there are any connections avaliable. Unused
|
920 |
|
|
connections are kept in the same table as used connections, but
|
921 |
|
|
unused ones have the tcpstate set to CLOSED. Also, connections in
|
922 |
|
|
TIME_WAIT are kept track of and we'll use the oldest one if no
|
923 |
|
|
CLOSED connections are found. Thanks to Eddie C. Dost for a very
|
924 |
|
|
nice algorithm for the TIME_WAIT search. */
|
925 |
|
|
uip_connr = 0;
|
926 |
|
|
for(c = 0; c < UIP_CONNS; ++c) {
|
927 |
|
|
if(uip_conns[c].tcpstateflags == CLOSED) {
|
928 |
|
|
uip_connr = &uip_conns[c];
|
929 |
|
|
break;
|
930 |
|
|
}
|
931 |
|
|
if(uip_conns[c].tcpstateflags == TIME_WAIT) {
|
932 |
|
|
if(uip_connr == 0 ||
|
933 |
|
|
uip_conns[c].timer > uip_connr->timer) {
|
934 |
|
|
uip_connr = &uip_conns[c];
|
935 |
|
|
}
|
936 |
|
|
}
|
937 |
|
|
}
|
938 |
|
|
|
939 |
|
|
if(uip_connr == 0) {
|
940 |
|
|
/* All connections are used already, we drop packet and hope that
|
941 |
|
|
the remote end will retransmit the packet at a time when we
|
942 |
|
|
have more spare connections. */
|
943 |
|
|
UIP_STAT(++uip_stat.tcp.syndrop);
|
944 |
|
|
UIP_LOG("tcp: found no unused connections.");
|
945 |
|
|
goto drop;
|
946 |
|
|
}
|
947 |
|
|
uip_conn = uip_connr;
|
948 |
|
|
|
949 |
|
|
/* Fill in the necessary fields for the new connection. */
|
950 |
|
|
uip_connr->rto = uip_connr->timer = UIP_RTO;
|
951 |
|
|
uip_connr->sa = 0;
|
952 |
|
|
uip_connr->sv = 4;
|
953 |
|
|
uip_connr->nrtx = 0;
|
954 |
|
|
uip_connr->lport = BUF->destport;
|
955 |
|
|
uip_connr->rport = BUF->srcport;
|
956 |
|
|
uip_connr->ripaddr[0] = BUF->srcipaddr[0];
|
957 |
|
|
uip_connr->ripaddr[1] = BUF->srcipaddr[1];
|
958 |
|
|
uip_connr->tcpstateflags = SYN_RCVD;
|
959 |
|
|
|
960 |
|
|
uip_connr->snd_nxt[0] = iss[0];
|
961 |
|
|
uip_connr->snd_nxt[1] = iss[1];
|
962 |
|
|
uip_connr->snd_nxt[2] = iss[2];
|
963 |
|
|
uip_connr->snd_nxt[3] = iss[3];
|
964 |
|
|
uip_connr->len = 1;
|
965 |
|
|
|
966 |
|
|
/* rcv_nxt should be the seqno from the incoming packet + 1. */
|
967 |
|
|
uip_connr->rcv_nxt[3] = BUF->seqno[3];
|
968 |
|
|
uip_connr->rcv_nxt[2] = BUF->seqno[2];
|
969 |
|
|
uip_connr->rcv_nxt[1] = BUF->seqno[1];
|
970 |
|
|
uip_connr->rcv_nxt[0] = BUF->seqno[0];
|
971 |
|
|
uip_add_rcv_nxt(1);
|
972 |
|
|
|
973 |
|
|
/* Parse the TCP MSS option, if present. */
|
974 |
|
|
if((BUF->tcpoffset & 0xf0) > 0x50) {
|
975 |
|
|
for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
|
976 |
|
|
opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
|
977 |
|
|
if(opt == 0x00) {
|
978 |
|
|
/* End of options. */
|
979 |
|
|
break;
|
980 |
|
|
} else if(opt == 0x01) {
|
981 |
|
|
++c;
|
982 |
|
|
/* NOP option. */
|
983 |
|
|
} else if(opt == 0x02 &&
|
984 |
|
|
uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) {
|
985 |
|
|
/* An MSS option with the right option length. */
|
986 |
|
|
tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
|
987 |
|
|
(u16_t)uip_buf[40 + UIP_LLH_LEN + 3 + c];
|
988 |
|
|
uip_connr->initialmss = uip_connr->mss =
|
989 |
|
|
tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
|
990 |
|
|
|
991 |
|
|
/* And we are done processing options. */
|
992 |
|
|
break;
|
993 |
|
|
} else {
|
994 |
|
|
/* All other options have a length field, so that we easily
|
995 |
|
|
can skip past them. */
|
996 |
|
|
if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
|
997 |
|
|
/* If the length field is zero, the options are malformed
|
998 |
|
|
and we don't process them further. */
|
999 |
|
|
break;
|
1000 |
|
|
}
|
1001 |
|
|
c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
|
1002 |
|
|
}
|
1003 |
|
|
}
|
1004 |
|
|
}
|
1005 |
|
|
|
1006 |
|
|
/* Our response will be a SYNACK. */
|
1007 |
|
|
#if UIP_ACTIVE_OPEN
|
1008 |
|
|
tcp_send_synack:
|
1009 |
|
|
BUF->flags = TCP_ACK;
|
1010 |
|
|
|
1011 |
|
|
tcp_send_syn:
|
1012 |
|
|
BUF->flags |= TCP_SYN;
|
1013 |
|
|
#else /* UIP_ACTIVE_OPEN */
|
1014 |
|
|
tcp_send_synack:
|
1015 |
|
|
BUF->flags = TCP_SYN | TCP_ACK;
|
1016 |
|
|
#endif /* UIP_ACTIVE_OPEN */
|
1017 |
|
|
|
1018 |
|
|
/* We send out the TCP Maximum Segment Size option with our
|
1019 |
|
|
SYNACK. */
|
1020 |
|
|
BUF->optdata[0] = 2;
|
1021 |
|
|
BUF->optdata[1] = 4;
|
1022 |
|
|
BUF->optdata[2] = (UIP_TCP_MSS) / 256;
|
1023 |
|
|
BUF->optdata[3] = (UIP_TCP_MSS) & 255;
|
1024 |
|
|
uip_len = 44;
|
1025 |
|
|
BUF->tcpoffset = 6 << 4;
|
1026 |
|
|
goto tcp_send;
|
1027 |
|
|
|
1028 |
|
|
/* This label will be jumped to if we found an active connection. */
|
1029 |
|
|
found:
|
1030 |
|
|
uip_conn = uip_connr;
|
1031 |
|
|
uip_flags = 0;
|
1032 |
|
|
|
1033 |
|
|
/* We do a very naive form of TCP reset processing; we just accept
|
1034 |
|
|
any RST and kill our connection. We should in fact check if the
|
1035 |
|
|
sequence number of this reset is wihtin our advertised window
|
1036 |
|
|
before we accept the reset. */
|
1037 |
|
|
if(BUF->flags & TCP_RST) {
|
1038 |
|
|
uip_connr->tcpstateflags = CLOSED;
|
1039 |
|
|
UIP_LOG("tcp: got reset, aborting connection.");
|
1040 |
|
|
uip_flags = UIP_ABORT;
|
1041 |
|
|
UIP_APPCALL();
|
1042 |
|
|
goto drop;
|
1043 |
|
|
}
|
1044 |
|
|
/* Calculated the length of the data, if the application has sent
|
1045 |
|
|
any data to us. */
|
1046 |
|
|
c = (BUF->tcpoffset >> 4) << 2;
|
1047 |
|
|
/* uip_len will contain the length of the actual TCP data. This is
|
1048 |
|
|
calculated by subtracing the length of the TCP header (in
|
1049 |
|
|
c) and the length of the IP header (20 bytes). */
|
1050 |
|
|
uip_len = uip_len - c - 20;
|
1051 |
|
|
|
1052 |
|
|
/* First, check if the sequence number of the incoming packet is
|
1053 |
|
|
what we're expecting next. If not, we send out an ACK with the
|
1054 |
|
|
correct numbers in. */
|
1055 |
|
|
if(uip_len > 0 &&
|
1056 |
|
|
(BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
|
1057 |
|
|
BUF->seqno[1] != uip_connr->rcv_nxt[1] ||
|
1058 |
|
|
BUF->seqno[2] != uip_connr->rcv_nxt[2] ||
|
1059 |
|
|
BUF->seqno[3] != uip_connr->rcv_nxt[3])) {
|
1060 |
|
|
goto tcp_send_ack;
|
1061 |
|
|
}
|
1062 |
|
|
|
1063 |
|
|
/* Next, check if the incoming segment acknowledges any outstanding
|
1064 |
|
|
data. If so, we update the sequence number, reset the length of
|
1065 |
|
|
the outstanding data, calculate RTT estimations, and reset the
|
1066 |
|
|
retransmission timer. */
|
1067 |
|
|
if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {
|
1068 |
|
|
uip_add32(uip_connr->snd_nxt, uip_connr->len);
|
1069 |
|
|
if(BUF->ackno[0] == uip_acc32[0] &&
|
1070 |
|
|
BUF->ackno[1] == uip_acc32[1] &&
|
1071 |
|
|
BUF->ackno[2] == uip_acc32[2] &&
|
1072 |
|
|
BUF->ackno[3] == uip_acc32[3]) {
|
1073 |
|
|
/* Update sequence number. */
|
1074 |
|
|
uip_connr->snd_nxt[0] = uip_acc32[0];
|
1075 |
|
|
uip_connr->snd_nxt[1] = uip_acc32[1];
|
1076 |
|
|
uip_connr->snd_nxt[2] = uip_acc32[2];
|
1077 |
|
|
uip_connr->snd_nxt[3] = uip_acc32[3];
|
1078 |
|
|
|
1079 |
|
|
|
1080 |
|
|
/* Do RTT estimation, unless we have done retransmissions. */
|
1081 |
|
|
if(uip_connr->nrtx == 0) {
|
1082 |
|
|
signed char m;
|
1083 |
|
|
m = uip_connr->rto - uip_connr->timer;
|
1084 |
|
|
/* This is taken directly from VJs original code in his paper */
|
1085 |
|
|
m = m - (uip_connr->sa >> 3);
|
1086 |
|
|
uip_connr->sa += m;
|
1087 |
|
|
if(m < 0) {
|
1088 |
|
|
m = -m;
|
1089 |
|
|
}
|
1090 |
|
|
m = m - (uip_connr->sv >> 2);
|
1091 |
|
|
uip_connr->sv += m;
|
1092 |
|
|
uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;
|
1093 |
|
|
|
1094 |
|
|
}
|
1095 |
|
|
/* Set the acknowledged flag. */
|
1096 |
|
|
uip_flags = UIP_ACKDATA;
|
1097 |
|
|
/* Reset the retransmission timer. */
|
1098 |
|
|
uip_connr->timer = uip_connr->rto;
|
1099 |
|
|
}
|
1100 |
|
|
|
1101 |
|
|
}
|
1102 |
|
|
|
1103 |
|
|
/* Do different things depending on in what state the connection is. */
|
1104 |
|
|
switch(uip_connr->tcpstateflags & TS_MASK) {
|
1105 |
|
|
/* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
|
1106 |
|
|
implemented, since we force the application to close when the
|
1107 |
|
|
peer sends a FIN (hence the application goes directly from
|
1108 |
|
|
ESTABLISHED to LAST_ACK). */
|
1109 |
|
|
case SYN_RCVD:
|
1110 |
|
|
/* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
|
1111 |
|
|
we are waiting for an ACK that acknowledges the data we sent
|
1112 |
|
|
out the last time. Therefore, we want to have the UIP_ACKDATA
|
1113 |
|
|
flag set. If so, we enter the ESTABLISHED state. */
|
1114 |
|
|
if(uip_flags & UIP_ACKDATA) {
|
1115 |
|
|
uip_connr->tcpstateflags = ESTABLISHED;
|
1116 |
|
|
uip_flags = UIP_CONNECTED;
|
1117 |
|
|
uip_connr->len = 0;
|
1118 |
|
|
if(uip_len > 0) {
|
1119 |
|
|
uip_flags |= UIP_NEWDATA;
|
1120 |
|
|
uip_add_rcv_nxt(uip_len);
|
1121 |
|
|
}
|
1122 |
|
|
uip_slen = 0;
|
1123 |
|
|
UIP_APPCALL();
|
1124 |
|
|
goto appsend;
|
1125 |
|
|
}
|
1126 |
|
|
goto drop;
|
1127 |
|
|
#if UIP_ACTIVE_OPEN
|
1128 |
|
|
case SYN_SENT:
|
1129 |
|
|
/* In SYN_SENT, we wait for a SYNACK that is sent in response to
|
1130 |
|
|
our SYN. The rcv_nxt is set to sequence number in the SYNACK
|
1131 |
|
|
plus one, and we send an ACK. We move into the ESTABLISHED
|
1132 |
|
|
state. */
|
1133 |
|
|
if((uip_flags & UIP_ACKDATA) &&
|
1134 |
|
|
BUF->flags == (TCP_SYN | TCP_ACK)) {
|
1135 |
|
|
|
1136 |
|
|
/* Parse the TCP MSS option, if present. */
|
1137 |
|
|
if((BUF->tcpoffset & 0xf0) > 0x50) {
|
1138 |
|
|
for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
|
1139 |
|
|
opt = uip_buf[40 + UIP_LLH_LEN + c];
|
1140 |
|
|
if(opt == 0x00) {
|
1141 |
|
|
/* End of options. */
|
1142 |
|
|
break;
|
1143 |
|
|
} else if(opt == 0x01) {
|
1144 |
|
|
++c;
|
1145 |
|
|
/* NOP option. */
|
1146 |
|
|
} else if(opt == 0x02 &&
|
1147 |
|
|
uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) {
|
1148 |
|
|
/* An MSS option with the right option length. */
|
1149 |
|
|
tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
|
1150 |
|
|
uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];
|
1151 |
|
|
uip_connr->initialmss =
|
1152 |
|
|
uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
|
1153 |
|
|
|
1154 |
|
|
/* And we are done processing options. */
|
1155 |
|
|
break;
|
1156 |
|
|
} else {
|
1157 |
|
|
/* All other options have a length field, so that we easily
|
1158 |
|
|
can skip past them. */
|
1159 |
|
|
if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
|
1160 |
|
|
/* If the length field is zero, the options are malformed
|
1161 |
|
|
and we don't process them further. */
|
1162 |
|
|
break;
|
1163 |
|
|
}
|
1164 |
|
|
c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
|
1165 |
|
|
}
|
1166 |
|
|
}
|
1167 |
|
|
}
|
1168 |
|
|
uip_connr->tcpstateflags = ESTABLISHED;
|
1169 |
|
|
uip_connr->rcv_nxt[0] = BUF->seqno[0];
|
1170 |
|
|
uip_connr->rcv_nxt[1] = BUF->seqno[1];
|
1171 |
|
|
uip_connr->rcv_nxt[2] = BUF->seqno[2];
|
1172 |
|
|
uip_connr->rcv_nxt[3] = BUF->seqno[3];
|
1173 |
|
|
uip_add_rcv_nxt(1);
|
1174 |
|
|
uip_flags = UIP_CONNECTED | UIP_NEWDATA;
|
1175 |
|
|
uip_connr->len = 0;
|
1176 |
|
|
uip_len = 0;
|
1177 |
|
|
uip_slen = 0;
|
1178 |
|
|
UIP_APPCALL();
|
1179 |
|
|
goto appsend;
|
1180 |
|
|
}
|
1181 |
|
|
goto reset;
|
1182 |
|
|
#endif /* UIP_ACTIVE_OPEN */
|
1183 |
|
|
|
1184 |
|
|
case ESTABLISHED:
|
1185 |
|
|
/* In the ESTABLISHED state, we call upon the application to feed
|
1186 |
|
|
data into the uip_buf. If the UIP_ACKDATA flag is set, the
|
1187 |
|
|
application should put new data into the buffer, otherwise we are
|
1188 |
|
|
retransmitting an old segment, and the application should put that
|
1189 |
|
|
data into the buffer.
|
1190 |
|
|
|
1191 |
|
|
If the incoming packet is a FIN, we should close the connection on
|
1192 |
|
|
this side as well, and we send out a FIN and enter the LAST_ACK
|
1193 |
|
|
state. We require that there is no outstanding data; otherwise the
|
1194 |
|
|
sequence numbers will be screwed up. */
|
1195 |
|
|
|
1196 |
|
|
if(BUF->flags & TCP_FIN) {
|
1197 |
|
|
if(uip_outstanding(uip_connr)) {
|
1198 |
|
|
goto drop;
|
1199 |
|
|
}
|
1200 |
|
|
uip_add_rcv_nxt(1 + uip_len);
|
1201 |
|
|
uip_flags = UIP_CLOSE;
|
1202 |
|
|
if(uip_len > 0) {
|
1203 |
|
|
uip_flags |= UIP_NEWDATA;
|
1204 |
|
|
}
|
1205 |
|
|
UIP_APPCALL();
|
1206 |
|
|
uip_connr->len = 1;
|
1207 |
|
|
uip_connr->tcpstateflags = LAST_ACK;
|
1208 |
|
|
uip_connr->nrtx = 0;
|
1209 |
|
|
tcp_send_finack:
|
1210 |
|
|
BUF->flags = TCP_FIN | TCP_ACK;
|
1211 |
|
|
goto tcp_send_nodata;
|
1212 |
|
|
}
|
1213 |
|
|
|
1214 |
|
|
/* Check the URG flag. If this is set, the segment carries urgent
|
1215 |
|
|
data that we must pass to the application. */
|
1216 |
|
|
if(BUF->flags & TCP_URG) {
|
1217 |
|
|
#if UIP_URGDATA > 0
|
1218 |
|
|
uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];
|
1219 |
|
|
if(uip_urglen > uip_len) {
|
1220 |
|
|
/* There is more urgent data in the next segment to come. */
|
1221 |
|
|
uip_urglen = uip_len;
|
1222 |
|
|
}
|
1223 |
|
|
uip_add_rcv_nxt(uip_urglen);
|
1224 |
|
|
uip_len -= uip_urglen;
|
1225 |
|
|
uip_urgdata = uip_appdata;
|
1226 |
|
|
uip_appdata += uip_urglen;
|
1227 |
|
|
} else {
|
1228 |
|
|
uip_urglen = 0;
|
1229 |
|
|
#endif /* UIP_URGDATA > 0 */
|
1230 |
|
|
uip_appdata += (BUF->urgp[0] << 8) | BUF->urgp[1];
|
1231 |
|
|
uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];
|
1232 |
|
|
}
|
1233 |
|
|
|
1234 |
|
|
|
1235 |
|
|
/* If uip_len > 0 we have TCP data in the packet, and we flag this
|
1236 |
|
|
by setting the UIP_NEWDATA flag and update the sequence number
|
1237 |
|
|
we acknowledge. If the application has stopped the dataflow
|
1238 |
|
|
using uip_stop(), we must not accept any data packets from the
|
1239 |
|
|
remote host. */
|
1240 |
|
|
if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
|
1241 |
|
|
uip_flags |= UIP_NEWDATA;
|
1242 |
|
|
uip_add_rcv_nxt(uip_len);
|
1243 |
|
|
}
|
1244 |
|
|
|
1245 |
|
|
/* Check if the available buffer space advertised by the other end
|
1246 |
|
|
is smaller than the initial MSS for this connection. If so, we
|
1247 |
|
|
set the current MSS to the window size to ensure that the
|
1248 |
|
|
application does not send more data than the other end can
|
1249 |
|
|
handle.
|
1250 |
|
|
|
1251 |
|
|
If the remote host advertises a zero window, we set the MSS to
|
1252 |
|
|
the initial MSS so that the application will send an entire MSS
|
1253 |
|
|
of data. This data will not be acknowledged by the receiver,
|
1254 |
|
|
and the application will retransmit it. This is called the
|
1255 |
|
|
"persistent timer" and uses the retransmission mechanim.
|
1256 |
|
|
*/
|
1257 |
|
|
tmp16 = ((u16_t)BUF->wnd[0] << 8) + (u16_t)BUF->wnd[1];
|
1258 |
|
|
if(tmp16 > uip_connr->initialmss ||
|
1259 |
|
|
tmp16 == 0) {
|
1260 |
|
|
tmp16 = uip_connr->initialmss;
|
1261 |
|
|
}
|
1262 |
|
|
uip_connr->mss = tmp16;
|
1263 |
|
|
|
1264 |
|
|
/* If this packet constitutes an ACK for outstanding data (flagged
|
1265 |
|
|
by the UIP_ACKDATA flag, we should call the application since it
|
1266 |
|
|
might want to send more data. If the incoming packet had data
|
1267 |
|
|
from the peer (as flagged by the UIP_NEWDATA flag), the
|
1268 |
|
|
application must also be notified.
|
1269 |
|
|
|
1270 |
|
|
When the application is called, the global variable uip_len
|
1271 |
|
|
contains the length of the incoming data. The application can
|
1272 |
|
|
access the incoming data through the global pointer
|
1273 |
|
|
uip_appdata, which usually points 40 bytes into the uip_buf
|
1274 |
|
|
array.
|
1275 |
|
|
|
1276 |
|
|
If the application wishes to send any data, this data should be
|
1277 |
|
|
put into the uip_appdata and the length of the data should be
|
1278 |
|
|
put into uip_len. If the application don't have any data to
|
1279 |
|
|
send, uip_len must be set to 0. */
|
1280 |
|
|
if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {
|
1281 |
|
|
uip_slen = 0;
|
1282 |
|
|
UIP_APPCALL();
|
1283 |
|
|
|
1284 |
|
|
appsend:
|
1285 |
|
|
|
1286 |
|
|
if(uip_flags & UIP_ABORT) {
|
1287 |
|
|
uip_slen = 0;
|
1288 |
|
|
uip_connr->tcpstateflags = CLOSED;
|
1289 |
|
|
BUF->flags = TCP_RST | TCP_ACK;
|
1290 |
|
|
goto tcp_send_nodata;
|
1291 |
|
|
}
|
1292 |
|
|
|
1293 |
|
|
if(uip_flags & UIP_CLOSE) {
|
1294 |
|
|
uip_slen = 0;
|
1295 |
|
|
uip_connr->len = 1;
|
1296 |
|
|
uip_connr->tcpstateflags = FIN_WAIT_1;
|
1297 |
|
|
uip_connr->nrtx = 0;
|
1298 |
|
|
BUF->flags = TCP_FIN | TCP_ACK;
|
1299 |
|
|
goto tcp_send_nodata;
|
1300 |
|
|
}
|
1301 |
|
|
|
1302 |
|
|
/* If uip_slen > 0, the application has data to be sent. */
|
1303 |
|
|
if(uip_slen > 0) {
|
1304 |
|
|
|
1305 |
|
|
/* If the connection has acknowledged data, the contents of
|
1306 |
|
|
the ->len variable should be discarded. */
|
1307 |
|
|
if((uip_flags & UIP_ACKDATA) != 0) {
|
1308 |
|
|
uip_connr->len = 0;
|
1309 |
|
|
}
|
1310 |
|
|
|
1311 |
|
|
/* If the ->len variable is non-zero the connection has
|
1312 |
|
|
already data in transit and cannot send anymore right
|
1313 |
|
|
now. */
|
1314 |
|
|
if(uip_connr->len == 0) {
|
1315 |
|
|
|
1316 |
|
|
/* The application cannot send more than what is allowed by
|
1317 |
|
|
the mss (the minumum of the MSS and the available
|
1318 |
|
|
window). */
|
1319 |
|
|
if(uip_slen > uip_connr->mss) {
|
1320 |
|
|
uip_slen = uip_connr->mss;
|
1321 |
|
|
}
|
1322 |
|
|
|
1323 |
|
|
/* Remember how much data we send out now so that we know
|
1324 |
|
|
when everything has been acknowledged. */
|
1325 |
|
|
uip_connr->len = uip_slen;
|
1326 |
|
|
} else {
|
1327 |
|
|
|
1328 |
|
|
/* If the application already had unacknowledged data, we
|
1329 |
|
|
make sure that the application does not send (i.e.,
|
1330 |
|
|
retransmit) out more than it previously sent out. */
|
1331 |
|
|
uip_slen = uip_connr->len;
|
1332 |
|
|
}
|
1333 |
|
|
} else {
|
1334 |
|
|
uip_connr->len = 0;
|
1335 |
|
|
}
|
1336 |
|
|
uip_connr->nrtx = 0;
|
1337 |
|
|
apprexmit:
|
1338 |
|
|
uip_appdata = uip_sappdata;
|
1339 |
|
|
|
1340 |
|
|
/* If the application has data to be sent, or if the incoming
|
1341 |
|
|
packet had new data in it, we must send out a packet. */
|
1342 |
|
|
if(uip_slen > 0 && uip_connr->len > 0) {
|
1343 |
|
|
/* Add the length of the IP and TCP headers. */
|
1344 |
|
|
uip_len = uip_connr->len + UIP_TCPIP_HLEN;
|
1345 |
|
|
/* We always set the ACK flag in response packets. */
|
1346 |
|
|
BUF->flags = TCP_ACK | TCP_PSH;
|
1347 |
|
|
/* Send the packet. */
|
1348 |
|
|
goto tcp_send_noopts;
|
1349 |
|
|
}
|
1350 |
|
|
/* If there is no data to send, just send out a pure ACK if
|
1351 |
|
|
there is newdata. */
|
1352 |
|
|
if(uip_flags & UIP_NEWDATA) {
|
1353 |
|
|
uip_len = UIP_TCPIP_HLEN;
|
1354 |
|
|
BUF->flags = TCP_ACK;
|
1355 |
|
|
goto tcp_send_noopts;
|
1356 |
|
|
}
|
1357 |
|
|
}
|
1358 |
|
|
goto drop;
|
1359 |
|
|
case LAST_ACK:
|
1360 |
|
|
/* We can close this connection if the peer has acknowledged our
|
1361 |
|
|
FIN. This is indicated by the UIP_ACKDATA flag. */
|
1362 |
|
|
if(uip_flags & UIP_ACKDATA) {
|
1363 |
|
|
uip_connr->tcpstateflags = CLOSED;
|
1364 |
|
|
uip_flags = UIP_CLOSE;
|
1365 |
|
|
UIP_APPCALL();
|
1366 |
|
|
}
|
1367 |
|
|
break;
|
1368 |
|
|
|
1369 |
|
|
case FIN_WAIT_1:
|
1370 |
|
|
/* The application has closed the connection, but the remote host
|
1371 |
|
|
hasn't closed its end yet. Thus we do nothing but wait for a
|
1372 |
|
|
FIN from the other side. */
|
1373 |
|
|
if(uip_len > 0) {
|
1374 |
|
|
uip_add_rcv_nxt(uip_len);
|
1375 |
|
|
}
|
1376 |
|
|
if(BUF->flags & TCP_FIN) {
|
1377 |
|
|
if(uip_flags & UIP_ACKDATA) {
|
1378 |
|
|
uip_connr->tcpstateflags = TIME_WAIT;
|
1379 |
|
|
uip_connr->timer = 0;
|
1380 |
|
|
uip_connr->len = 0;
|
1381 |
|
|
} else {
|
1382 |
|
|
uip_connr->tcpstateflags = CLOSING;
|
1383 |
|
|
}
|
1384 |
|
|
uip_add_rcv_nxt(1);
|
1385 |
|
|
uip_flags = UIP_CLOSE;
|
1386 |
|
|
UIP_APPCALL();
|
1387 |
|
|
goto tcp_send_ack;
|
1388 |
|
|
} else if(uip_flags & UIP_ACKDATA) {
|
1389 |
|
|
uip_connr->tcpstateflags = FIN_WAIT_2;
|
1390 |
|
|
uip_connr->len = 0;
|
1391 |
|
|
goto drop;
|
1392 |
|
|
}
|
1393 |
|
|
if(uip_len > 0) {
|
1394 |
|
|
goto tcp_send_ack;
|
1395 |
|
|
}
|
1396 |
|
|
goto drop;
|
1397 |
|
|
|
1398 |
|
|
case FIN_WAIT_2:
|
1399 |
|
|
if(uip_len > 0) {
|
1400 |
|
|
uip_add_rcv_nxt(uip_len);
|
1401 |
|
|
}
|
1402 |
|
|
if(BUF->flags & TCP_FIN) {
|
1403 |
|
|
uip_connr->tcpstateflags = TIME_WAIT;
|
1404 |
|
|
uip_connr->timer = 0;
|
1405 |
|
|
uip_add_rcv_nxt(1);
|
1406 |
|
|
uip_flags = UIP_CLOSE;
|
1407 |
|
|
UIP_APPCALL();
|
1408 |
|
|
goto tcp_send_ack;
|
1409 |
|
|
}
|
1410 |
|
|
if(uip_len > 0) {
|
1411 |
|
|
goto tcp_send_ack;
|
1412 |
|
|
}
|
1413 |
|
|
goto drop;
|
1414 |
|
|
|
1415 |
|
|
case TIME_WAIT:
|
1416 |
|
|
goto tcp_send_ack;
|
1417 |
|
|
|
1418 |
|
|
case CLOSING:
|
1419 |
|
|
if(uip_flags & UIP_ACKDATA) {
|
1420 |
|
|
uip_connr->tcpstateflags = TIME_WAIT;
|
1421 |
|
|
uip_connr->timer = 0;
|
1422 |
|
|
}
|
1423 |
|
|
}
|
1424 |
|
|
goto drop;
|
1425 |
|
|
|
1426 |
|
|
|
1427 |
|
|
/* We jump here when we are ready to send the packet, and just want
|
1428 |
|
|
to set the appropriate TCP sequence numbers in the TCP header. */
|
1429 |
|
|
tcp_send_ack:
|
1430 |
|
|
BUF->flags = TCP_ACK;
|
1431 |
|
|
tcp_send_nodata:
|
1432 |
|
|
uip_len = 40;
|
1433 |
|
|
tcp_send_noopts:
|
1434 |
|
|
BUF->tcpoffset = 5 << 4;
|
1435 |
|
|
tcp_send:
|
1436 |
|
|
/* We're done with the input processing. We are now ready to send a
|
1437 |
|
|
reply. Our job is to fill in all the fields of the TCP and IP
|
1438 |
|
|
headers before calculating the checksum and finally send the
|
1439 |
|
|
packet. */
|
1440 |
|
|
BUF->ackno[0] = uip_connr->rcv_nxt[0];
|
1441 |
|
|
BUF->ackno[1] = uip_connr->rcv_nxt[1];
|
1442 |
|
|
BUF->ackno[2] = uip_connr->rcv_nxt[2];
|
1443 |
|
|
BUF->ackno[3] = uip_connr->rcv_nxt[3];
|
1444 |
|
|
|
1445 |
|
|
BUF->seqno[0] = uip_connr->snd_nxt[0];
|
1446 |
|
|
BUF->seqno[1] = uip_connr->snd_nxt[1];
|
1447 |
|
|
BUF->seqno[2] = uip_connr->snd_nxt[2];
|
1448 |
|
|
BUF->seqno[3] = uip_connr->snd_nxt[3];
|
1449 |
|
|
|
1450 |
|
|
BUF->proto = UIP_PROTO_TCP;
|
1451 |
|
|
|
1452 |
|
|
BUF->srcport = uip_connr->lport;
|
1453 |
|
|
BUF->destport = uip_connr->rport;
|
1454 |
|
|
|
1455 |
|
|
BUF->srcipaddr[0] = uip_hostaddr[0];
|
1456 |
|
|
BUF->srcipaddr[1] = uip_hostaddr[1];
|
1457 |
|
|
BUF->destipaddr[0] = uip_connr->ripaddr[0];
|
1458 |
|
|
BUF->destipaddr[1] = uip_connr->ripaddr[1];
|
1459 |
|
|
|
1460 |
|
|
|
1461 |
|
|
if(uip_connr->tcpstateflags & UIP_STOPPED) {
|
1462 |
|
|
/* If the connection has issued uip_stop(), we advertise a zero
|
1463 |
|
|
window so that the remote host will stop sending data. */
|
1464 |
|
|
BUF->wnd[0] = BUF->wnd[1] = 0;
|
1465 |
|
|
} else {
|
1466 |
|
|
BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);
|
1467 |
|
|
BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);
|
1468 |
|
|
}
|
1469 |
|
|
|
1470 |
|
|
tcp_send_noconn:
|
1471 |
|
|
|
1472 |
|
|
BUF->len[0] = (uip_len >> 8);
|
1473 |
|
|
BUF->len[1] = (uip_len & 0xff);
|
1474 |
|
|
|
1475 |
|
|
/* Calculate TCP checksum. */
|
1476 |
|
|
BUF->tcpchksum = 0;
|
1477 |
|
|
BUF->tcpchksum = ~(uip_tcpchksum());
|
1478 |
|
|
|
1479 |
|
|
ip_send_nolen:
|
1480 |
|
|
|
1481 |
|
|
BUF->vhl = 0x45;
|
1482 |
|
|
BUF->tos = 0;
|
1483 |
|
|
BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
|
1484 |
|
|
BUF->ttl = UIP_TTL;
|
1485 |
|
|
++ipid;
|
1486 |
|
|
BUF->ipid[0] = ipid >> 8;
|
1487 |
|
|
BUF->ipid[1] = ipid & 0xff;
|
1488 |
|
|
|
1489 |
|
|
/* Calculate IP checksum. */
|
1490 |
|
|
BUF->ipchksum = 0;
|
1491 |
|
|
BUF->ipchksum = ~(uip_ipchksum());
|
1492 |
|
|
|
1493 |
|
|
UIP_STAT(++uip_stat.tcp.sent);
|
1494 |
|
|
send:
|
1495 |
|
|
UIP_STAT(++uip_stat.ip.sent);
|
1496 |
|
|
/* Return and let the caller do the actual transmission. */
|
1497 |
|
|
return;
|
1498 |
|
|
drop:
|
1499 |
|
|
uip_len = 0;
|
1500 |
|
|
return;
|
1501 |
|
|
}
|
1502 |
|
|
/*-----------------------------------------------------------------------------------*/
|
1503 |
|
|
u16_t
|
1504 |
|
|
htons(u16_t val)
|
1505 |
|
|
{
|
1506 |
|
|
return HTONS(val);
|
1507 |
|
|
}
|
1508 |
|
|
/*-----------------------------------------------------------------------------------*/
|
1509 |
|
|
/** @} */
|