Contiki 3.x
uip.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2003, Adam Dunkels.
3  * 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  * 3. The name of the author may not be used to endorse or promote
14  * products derived from this software without specific prior
15  * written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the uIP TCP/IP stack.
30  *
31  *
32  */
33 
34 /**
35  * \file
36  * The uIP TCP/IP stack code.
37  * \author Adam Dunkels <adam@dunkels.com>
38  */
39 
40 /**
41  * \addtogroup uip
42  * @{
43  */
44 
45 #define DEBUG_PRINTF(...) /*printf(__VA_ARGS__)*/
46 
47 /*
48  * uIP is a small implementation of the IP, UDP and TCP protocols (as
49  * well as some basic ICMP stuff). The implementation couples the IP,
50  * UDP, TCP and the application layers very tightly. To keep the size
51  * of the compiled code down, this code frequently uses the goto
52  * statement. While it would be possible to break the uip_process()
53  * function into many smaller functions, this would increase the code
54  * size because of the overhead of parameter passing and the fact that
55  * the optimier would not be as efficient.
56  *
57  * The principle is that we have a small buffer, called the uip_buf,
58  * in which the device driver puts an incoming packet. The TCP/IP
59  * stack parses the headers in the packet, and calls the
60  * application. If the remote host has sent data to the application,
61  * this data is present in the uip_buf and the application read the
62  * data from there. It is up to the application to put this data into
63  * a byte stream if needed. The application will not be fed with data
64  * that is out of sequence.
65  *
66  * If the application whishes to send data to the peer, it should put
67  * its data into the uip_buf. The uip_appdata pointer points to the
68  * first available byte. The TCP/IP stack will calculate the
69  * checksums, and fill in the necessary header fields and finally send
70  * the packet back to the peer.
71 */
72 
73 #include "net/ip/uip.h"
74 #include "net/ip/uipopt.h"
75 #include "net/ipv4/uip_arp.h"
76 #include "net/ip/uip_arch.h"
77 
78 #include "net/ipv4/uip-neighbor.h"
79 
80 #include <string.h>
81 #include "sys/cc.h"
82 
83 /*---------------------------------------------------------------------------*/
84 /* Variable definitions. */
85 
86 
87 /* The IP address of this host. If it is defined to be fixed (by
88  setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set
89  here. Otherwise, the address */
90 #if UIP_FIXEDADDR > 0
91 const uip_ipaddr_t uip_hostaddr =
92  { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3 };
93 const uip_ipaddr_t uip_draddr =
94  { UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3 };
95 const uip_ipaddr_t uip_netmask =
96  { UIP_NETMASK0, UIP_NETMASK1, UIP_NETMASK2, UIP_NETMASK3 };
97 #else
98 uip_ipaddr_t uip_hostaddr, uip_draddr, uip_netmask;
99 #endif /* UIP_FIXEDADDR */
100 
101 const uip_ipaddr_t uip_broadcast_addr =
102 #if NETSTACK_CONF_WITH_IPV6
103  { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
104  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
105 #else /* NETSTACK_CONF_WITH_IPV6 */
106  { { 0xff, 0xff, 0xff, 0xff } };
107 #endif /* NETSTACK_CONF_WITH_IPV6 */
108 const uip_ipaddr_t uip_all_zeroes_addr = { { 0x0, /* rest is 0 */ } };
109 
110 #if UIP_FIXEDETHADDR
111 const uip_lladdr_t uip_lladdr = {{UIP_ETHADDR0,
112  UIP_ETHADDR1,
113  UIP_ETHADDR2,
114  UIP_ETHADDR3,
115  UIP_ETHADDR4,
116  UIP_ETHADDR5}};
117 #else
118 uip_lladdr_t uip_lladdr = {{0,0,0,0,0,0}};
119 #endif
120 
121 /* The packet buffer that contains incoming packets. */
123 
124 void *uip_appdata; /* The uip_appdata pointer points to
125  application data. */
126 void *uip_sappdata; /* The uip_appdata pointer points to
127  the application data which is to
128  be sent. */
129 #if UIP_URGDATA > 0
130 void *uip_urgdata; /* The uip_urgdata pointer points to
131  urgent data (out-of-band data), if
132  present. */
133 uint16_t uip_urglen, uip_surglen;
134 #endif /* UIP_URGDATA > 0 */
135 
136 uint16_t uip_len, uip_slen;
137  /* The uip_len is either 8 or 16 bits,
138  depending on the maximum packet
139  size. */
140 
141 uint8_t uip_flags; /* The uip_flags variable is used for
142  communication between the TCP/IP stack
143  and the application program. */
144 struct uip_conn *uip_conn; /* uip_conn always points to the current
145  connection. */
146 
147 struct uip_conn uip_conns[UIP_CONNS];
148  /* The uip_conns array holds all TCP
149  connections. */
150 uint16_t uip_listenports[UIP_LISTENPORTS];
151  /* The uip_listenports list all currently
152  listning ports. */
153 #if UIP_UDP
155 struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
156 #endif /* UIP_UDP */
157 
158 static uint16_t ipid; /* Ths ipid variable is an increasing
159  number that is used for the IP ID
160  field. */
161 
162 void uip_setipid(uint16_t id) { ipid = id; }
163 
164 static uint8_t iss[4]; /* The iss variable is used for the TCP
165  initial sequence number. */
166 
167 #if UIP_ACTIVE_OPEN || UIP_UDP
168 static uint16_t lastport; /* Keeps track of the last port used for
169  a new connection. */
170 #endif /* UIP_ACTIVE_OPEN || UIP_UDP */
171 
172 /* Temporary variables. */
173 uint8_t uip_acc32[4];
174 static uint8_t c, opt;
175 static uint16_t tmp16;
176 
177 /* Structures and definitions. */
178 #define TCP_FIN 0x01
179 #define TCP_SYN 0x02
180 #define TCP_RST 0x04
181 #define TCP_PSH 0x08
182 #define TCP_ACK 0x10
183 #define TCP_URG 0x20
184 #define TCP_CTL 0x3f
185 
186 #define TCP_OPT_END 0 /* End of TCP options list */
187 #define TCP_OPT_NOOP 1 /* "No-operation" TCP option */
188 #define TCP_OPT_MSS 2 /* Maximum segment size TCP option */
189 
190 #define TCP_OPT_MSS_LEN 4 /* Length of TCP MSS option. */
191 
192 #define ICMP_ECHO_REPLY 0
193 #define ICMP_ECHO 8
194 
195 #define ICMP_DEST_UNREACHABLE 3
196 #define ICMP_PORT_UNREACHABLE 3
197 
198 #define ICMP6_ECHO_REPLY 129
199 #define ICMP6_ECHO 128
200 #define ICMP6_NEIGHBOR_SOLICITATION 135
201 #define ICMP6_NEIGHBOR_ADVERTISEMENT 136
202 
203 #define ICMP6_FLAG_S (1 << 6)
204 
205 #define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1
206 #define ICMP6_OPTION_TARGET_LINK_ADDRESS 2
207 
208 
209 /* Macros. */
210 #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
211 #define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0])
212 #define ICMPBUF ((struct uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
213 #define UDPBUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
214 
215 
216 #if UIP_STATISTICS == 1
217 struct uip_stats uip_stat;
218 #define UIP_STAT(s) s
219 #else
220 #define UIP_STAT(s)
221 #endif /* UIP_STATISTICS == 1 */
222 
223 #if UIP_LOGGING == 1
224 #include <stdio.h>
225 void uip_log(char *msg);
226 #define UIP_LOG(m) uip_log(m)
227 #else
228 #define UIP_LOG(m)
229 #endif /* UIP_LOGGING == 1 */
230 
231 #if ! UIP_ARCH_ADD32
232 void
233 uip_add32(uint8_t *op32, uint16_t op16)
234 {
235  uip_acc32[3] = op32[3] + (op16 & 0xff);
236  uip_acc32[2] = op32[2] + (op16 >> 8);
237  uip_acc32[1] = op32[1];
238  uip_acc32[0] = op32[0];
239 
240  if(uip_acc32[2] < (op16 >> 8)) {
241  ++uip_acc32[1];
242  if(uip_acc32[1] == 0) {
243  ++uip_acc32[0];
244  }
245  }
246 
247 
248  if(uip_acc32[3] < (op16 & 0xff)) {
249  ++uip_acc32[2];
250  if(uip_acc32[2] == 0) {
251  ++uip_acc32[1];
252  if(uip_acc32[1] == 0) {
253  ++uip_acc32[0];
254  }
255  }
256  }
257 }
258 
259 #endif /* UIP_ARCH_ADD32 */
260 
261 #if ! UIP_ARCH_CHKSUM
262 /*---------------------------------------------------------------------------*/
263 static uint16_t
264 chksum(uint16_t sum, const uint8_t *data, uint16_t len)
265 {
266  uint16_t t;
267  const uint8_t *dataptr;
268  const uint8_t *last_byte;
269 
270  dataptr = data;
271  last_byte = data + len - 1;
272 
273  while(dataptr < last_byte) { /* At least two more bytes */
274  t = (dataptr[0] << 8) + dataptr[1];
275  sum += t;
276  if(sum < t) {
277  sum++; /* carry */
278  }
279  dataptr += 2;
280  }
281 
282  if(dataptr == last_byte) {
283  t = (dataptr[0] << 8) + 0;
284  sum += t;
285  if(sum < t) {
286  sum++; /* carry */
287  }
288  }
289 
290  /* Return sum in host byte order. */
291  return sum;
292 }
293 /*---------------------------------------------------------------------------*/
294 uint16_t
295 uip_chksum(uint16_t *data, uint16_t len)
296 {
297  return uip_htons(chksum(0, (uint8_t *)data, len));
298 }
299 /*---------------------------------------------------------------------------*/
300 #ifndef UIP_ARCH_IPCHKSUM
301 uint16_t
303 {
304  uint16_t sum;
305 
306  sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
307  DEBUG_PRINTF("uip_ipchksum: sum 0x%04x\n", sum);
308  return (sum == 0) ? 0xffff : uip_htons(sum);
309 }
310 #endif
311 /*---------------------------------------------------------------------------*/
312 static uint16_t
313 upper_layer_chksum(uint8_t proto)
314 {
315  uint16_t upper_layer_len;
316  uint16_t sum;
317 
318 #if NETSTACK_CONF_WITH_IPV6
319  upper_layer_len = (((uint16_t)(BUF->len[0]) << 8) + BUF->len[1]);
320 #else /* NETSTACK_CONF_WITH_IPV6 */
321  upper_layer_len = (((uint16_t)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN;
322 #endif /* NETSTACK_CONF_WITH_IPV6 */
323 
324  /* First sum pseudoheader. */
325 
326  /* IP protocol and length fields. This addition cannot carry. */
327  sum = upper_layer_len + proto;
328  /* Sum IP source and destination addresses. */
329  sum = chksum(sum, (uint8_t *)&BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t));
330 
331  /* Sum TCP header and data. */
332  sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN],
333  upper_layer_len);
334 
335  return (sum == 0) ? 0xffff : uip_htons(sum);
336 }
337 /*---------------------------------------------------------------------------*/
338 #if NETSTACK_CONF_WITH_IPV6
339 uint16_t
341 {
342  return upper_layer_chksum(UIP_PROTO_ICMP6);
343 
344 }
345 #endif /* NETSTACK_CONF_WITH_IPV6 */
346 /*---------------------------------------------------------------------------*/
347 uint16_t
349 {
350  return upper_layer_chksum(UIP_PROTO_TCP);
351 }
352 /*---------------------------------------------------------------------------*/
353 #if UIP_UDP_CHECKSUMS
354 uint16_t
355 uip_udpchksum(void)
356 {
357  return upper_layer_chksum(UIP_PROTO_UDP);
358 }
359 #endif /* UIP_UDP_CHECKSUMS */
360 #endif /* UIP_ARCH_CHKSUM */
361 /*---------------------------------------------------------------------------*/
362 void
363 uip_init(void)
364 {
365  for(c = 0; c < UIP_LISTENPORTS; ++c) {
366  uip_listenports[c] = 0;
367  }
368  for(c = 0; c < UIP_CONNS; ++c) {
369  uip_conns[c].tcpstateflags = UIP_CLOSED;
370  }
371 #if UIP_ACTIVE_OPEN || UIP_UDP
372  lastport = 1024;
373 #endif /* UIP_ACTIVE_OPEN || UIP_UDP */
374 
375 #if UIP_UDP
376  for(c = 0; c < UIP_UDP_CONNS; ++c) {
377  uip_udp_conns[c].lport = 0;
378  }
379 #endif /* UIP_UDP */
380 
381 
382  /* IPv4 initialization. */
383 #if UIP_FIXEDADDR == 0
384  /* uip_hostaddr[0] = uip_hostaddr[1] = 0;*/
385 #endif /* UIP_FIXEDADDR */
386 
387 }
388 /*---------------------------------------------------------------------------*/
389 #if UIP_ACTIVE_OPEN
390 struct uip_conn *
391 uip_connect(const uip_ipaddr_t *ripaddr, uint16_t rport)
392 {
393  register struct uip_conn *conn, *cconn;
394 
395  /* Find an unused local port. */
396  again:
397  ++lastport;
398 
399  if(lastport >= 32000) {
400  lastport = 4096;
401  }
402 
403  /* Check if this port is already in use, and if so try to find
404  another one. */
405  for(c = 0; c < UIP_CONNS; ++c) {
406  conn = &uip_conns[c];
407  if(conn->tcpstateflags != UIP_CLOSED &&
408  conn->lport == uip_htons(lastport)) {
409  goto again;
410  }
411  }
412 
413  conn = 0;
414  for(c = 0; c < UIP_CONNS; ++c) {
415  cconn = &uip_conns[c];
416  if(cconn->tcpstateflags == UIP_CLOSED) {
417  conn = cconn;
418  break;
419  }
420  if(cconn->tcpstateflags == UIP_TIME_WAIT) {
421  if(conn == 0 ||
422  cconn->timer > conn->timer) {
423  conn = cconn;
424  }
425  }
426  }
427 
428  if(conn == 0) {
429  return 0;
430  }
431 
432  conn->tcpstateflags = UIP_SYN_SENT;
433 
434  conn->snd_nxt[0] = iss[0];
435  conn->snd_nxt[1] = iss[1];
436  conn->snd_nxt[2] = iss[2];
437  conn->snd_nxt[3] = iss[3];
438 
439  conn->rcv_nxt[0] = 0;
440  conn->rcv_nxt[1] = 0;
441  conn->rcv_nxt[2] = 0;
442  conn->rcv_nxt[3] = 0;
443 
444  conn->initialmss = conn->mss = UIP_TCP_MSS;
445 
446  conn->len = 1; /* TCP length of the SYN is one. */
447  conn->nrtx = 0;
448  conn->timer = 1; /* Send the SYN next time around. */
449  conn->rto = UIP_RTO;
450  conn->sa = 0;
451  conn->sv = 16; /* Initial value of the RTT variance. */
452  conn->lport = uip_htons(lastport);
453  conn->rport = rport;
454  uip_ipaddr_copy(&conn->ripaddr, ripaddr);
455 
456  return conn;
457 }
458 #endif /* UIP_ACTIVE_OPEN */
459 /*---------------------------------------------------------------------------*/
460 #if UIP_UDP
461 struct uip_udp_conn *
462 uip_udp_new(const uip_ipaddr_t *ripaddr, uint16_t rport)
463 {
464  register struct uip_udp_conn *conn;
465 
466  /* Find an unused local port. */
467  again:
468  ++lastport;
469 
470  if(lastport >= 32000) {
471  lastport = 4096;
472  }
473 
474  for(c = 0; c < UIP_UDP_CONNS; ++c) {
475  if(uip_udp_conns[c].lport == uip_htons(lastport)) {
476  goto again;
477  }
478  }
479 
480 
481  conn = 0;
482  for(c = 0; c < UIP_UDP_CONNS; ++c) {
483  if(uip_udp_conns[c].lport == 0) {
484  conn = &uip_udp_conns[c];
485  break;
486  }
487  }
488 
489  if(conn == 0) {
490  return 0;
491  }
492 
493  conn->lport = UIP_HTONS(lastport);
494  conn->rport = rport;
495  if(ripaddr == NULL) {
496  memset(&conn->ripaddr, 0, sizeof(uip_ipaddr_t));
497  } else {
498  uip_ipaddr_copy(&conn->ripaddr, ripaddr);
499  }
500  conn->ttl = UIP_TTL;
501 
502  return conn;
503 }
504 #endif /* UIP_UDP */
505 /*---------------------------------------------------------------------------*/
506 void
507 uip_unlisten(uint16_t port)
508 {
509  for(c = 0; c < UIP_LISTENPORTS; ++c) {
510  if(uip_listenports[c] == port) {
511  uip_listenports[c] = 0;
512  return;
513  }
514  }
515 }
516 /*---------------------------------------------------------------------------*/
517 void
518 uip_listen(uint16_t port)
519 {
520  for(c = 0; c < UIP_LISTENPORTS; ++c) {
521  if(uip_listenports[c] == 0) {
522  uip_listenports[c] = port;
523  return;
524  }
525  }
526 }
527 /*---------------------------------------------------------------------------*/
528 /* XXX: IP fragment reassembly: not well-tested. */
529 
530 #if UIP_REASSEMBLY && !NETSTACK_CONF_WITH_IPV6
531 #define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
532 static uint8_t uip_reassbuf[UIP_REASS_BUFSIZE];
533 static uint8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
534 static const uint8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
535  0x0f, 0x07, 0x03, 0x01};
536 static uint16_t uip_reasslen;
537 static uint8_t uip_reassflags;
538 #define UIP_REASS_FLAG_LASTFRAG 0x01
539 static uint8_t uip_reasstmr;
540 
541 #define IP_MF 0x20
542 
543 static uint8_t
544 uip_reass(void)
545 {
546  uint16_t offset, len;
547  uint16_t i;
548 
549  /* If ip_reasstmr is zero, no packet is present in the buffer, so we
550  write the IP header of the fragment into the reassembly
551  buffer. The timer is updated with the maximum age. */
552  if(uip_reasstmr == 0) {
553  memcpy(uip_reassbuf, &BUF->vhl, UIP_IPH_LEN);
554  uip_reasstmr = UIP_REASS_MAXAGE;
555  uip_reassflags = 0;
556  /* Clear the bitmap. */
557  memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap));
558  }
559 
560  /* Check if the incoming fragment matches the one currently present
561  in the reasembly buffer. If so, we proceed with copying the
562  fragment into the buffer. */
563  if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&
564  BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
565  BUF->destipaddr[0] == FBUF->destipaddr[0] &&
566  BUF->destipaddr[1] == FBUF->destipaddr[1] &&
567  BUF->ipid[0] == FBUF->ipid[0] &&
568  BUF->ipid[1] == FBUF->ipid[1]) {
569 
570  len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
571  offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
572 
573  /* If the offset or the offset + fragment length overflows the
574  reassembly buffer, we discard the entire packet. */
575  if(offset > UIP_REASS_BUFSIZE ||
576  offset + len > UIP_REASS_BUFSIZE) {
577  uip_reasstmr = 0;
578  goto nullreturn;
579  }
580 
581  /* Copy the fragment into the reassembly buffer, at the right
582  offset. */
583  memcpy(&uip_reassbuf[UIP_IPH_LEN + offset],
584  (char *)BUF + (int)((BUF->vhl & 0x0f) * 4),
585  len);
586 
587  /* Update the bitmap. */
588  if(offset / (8 * 8) == (offset + len) / (8 * 8)) {
589  /* If the two endpoints are in the same byte, we only update
590  that byte. */
591 
592  uip_reassbitmap[offset / (8 * 8)] |=
593  bitmap_bits[(offset / 8 ) & 7] &
594  ~bitmap_bits[((offset + len) / 8 ) & 7];
595  } else {
596  /* If the two endpoints are in different bytes, we update the
597  bytes in the endpoints and fill the stuff inbetween with
598  0xff. */
599  uip_reassbitmap[offset / (8 * 8)] |=
600  bitmap_bits[(offset / 8 ) & 7];
601  for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
602  uip_reassbitmap[i] = 0xff;
603  }
604  uip_reassbitmap[(offset + len) / (8 * 8)] |=
605  ~bitmap_bits[((offset + len) / 8 ) & 7];
606  }
607 
608  /* If this fragment has the More Fragments flag set to zero, we
609  know that this is the last fragment, so we can calculate the
610  size of the entire packet. We also set the
611  IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
612  the final fragment. */
613 
614  if((BUF->ipoffset[0] & IP_MF) == 0) {
615  uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
616  uip_reasslen = offset + len;
617  }
618 
619  /* Finally, we check if we have a full packet in the buffer. We do
620  this by checking if we have the last fragment and if all bits
621  in the bitmap are set. */
622  if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
623  /* Check all bytes up to and including all but the last byte in
624  the bitmap. */
625  for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {
626  if(uip_reassbitmap[i] != 0xff) {
627  goto nullreturn;
628  }
629  }
630  /* Check the last byte in the bitmap. It should contain just the
631  right amount of bits. */
632  if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=
633  (uint8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {
634  goto nullreturn;
635  }
636 
637  /* If we have come this far, we have a full packet in the
638  buffer, so we allocate a pbuf and copy the packet into it. We
639  also reset the timer. */
640  uip_reasstmr = 0;
641  memcpy(BUF, FBUF, uip_reasslen);
642 
643  /* Pretend to be a "normal" (i.e., not fragmented) IP packet
644  from now on. */
645  BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
646  BUF->len[0] = uip_reasslen >> 8;
647  BUF->len[1] = uip_reasslen & 0xff;
648  BUF->ipchksum = 0;
649  BUF->ipchksum = ~(uip_ipchksum());
650 
651  return uip_reasslen;
652  }
653  }
654 
655  nullreturn:
656  return 0;
657 }
658 #endif /* UIP_REASSEMBLY */
659 /*---------------------------------------------------------------------------*/
660 static void
661 uip_add_rcv_nxt(uint16_t n)
662 {
663  uip_add32(uip_conn->rcv_nxt, n);
664  uip_conn->rcv_nxt[0] = uip_acc32[0];
665  uip_conn->rcv_nxt[1] = uip_acc32[1];
666  uip_conn->rcv_nxt[2] = uip_acc32[2];
667  uip_conn->rcv_nxt[3] = uip_acc32[3];
668 }
669 /*---------------------------------------------------------------------------*/
670 void
671 uip_process(uint8_t flag)
672 {
673  register struct uip_conn *uip_connr = uip_conn;
674 
675 #if UIP_UDP
676  if(flag == UIP_UDP_SEND_CONN) {
677  goto udp_send;
678  }
679 #endif /* UIP_UDP */
680 
681  uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];
682 
683  /* Check if we were invoked because of a poll request for a
684  particular connection. */
685  if(flag == UIP_POLL_REQUEST) {
686  if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED &&
687  !uip_outstanding(uip_connr)) {
688  uip_flags = UIP_POLL;
689  UIP_APPCALL();
690  goto appsend;
691 #if UIP_ACTIVE_OPEN && UIP_TCP
692  } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) {
693  /* In the SYN_SENT state, we retransmit out SYN. */
694  BUF->flags = 0;
695  goto tcp_send_syn;
696 #endif /* UIP_ACTIVE_OPEN */
697  }
698  goto drop;
699 
700  /* Check if we were invoked because of the periodic timer firing. */
701  } else if(flag == UIP_TIMER) {
702 #if UIP_REASSEMBLY
703  if(uip_reasstmr != 0) {
704  --uip_reasstmr;
705  }
706 #endif /* UIP_REASSEMBLY */
707  /* Increase the initial sequence number. */
708  if(++iss[3] == 0) {
709  if(++iss[2] == 0) {
710  if(++iss[1] == 0) {
711  ++iss[0];
712  }
713  }
714  }
715 
716  /* Reset the length variables. */
717  uip_clear_buf();
718  uip_slen = 0;
719 
720 #if UIP_TCP
721  /* Check if the connection is in a state in which we simply wait
722  for the connection to time out. If so, we increase the
723  connection's timer and remove the connection if it times
724  out. */
725  if(uip_connr->tcpstateflags == UIP_TIME_WAIT ||
726  uip_connr->tcpstateflags == UIP_FIN_WAIT_2) {
727  ++(uip_connr->timer);
728  if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
729  uip_connr->tcpstateflags = UIP_CLOSED;
730  }
731  } else if(uip_connr->tcpstateflags != UIP_CLOSED) {
732  /* If the connection has outstanding data, we increase the
733  connection's timer and see if it has reached the RTO value
734  in which case we retransmit. */
735 
736  if(uip_outstanding(uip_connr)) {
737  if(uip_connr->timer-- == 0) {
738  if(uip_connr->nrtx == UIP_MAXRTX ||
739  ((uip_connr->tcpstateflags == UIP_SYN_SENT ||
740  uip_connr->tcpstateflags == UIP_SYN_RCVD) &&
741  uip_connr->nrtx == UIP_MAXSYNRTX)) {
742  uip_connr->tcpstateflags = UIP_CLOSED;
743 
744  /* We call UIP_APPCALL() with uip_flags set to
745  UIP_TIMEDOUT to inform the application that the
746  connection has timed out. */
747  uip_flags = UIP_TIMEDOUT;
748  UIP_APPCALL();
749 
750  /* We also send a reset packet to the remote host. */
751  BUF->flags = TCP_RST | TCP_ACK;
752  goto tcp_send_nodata;
753  }
754 
755  /* Exponential backoff. */
756  uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
757  4:
758  uip_connr->nrtx);
759  ++(uip_connr->nrtx);
760 
761  /* Ok, so we need to retransmit. We do this differently
762  depending on which state we are in. In ESTABLISHED, we
763  call upon the application so that it may prepare the
764  data for the retransmit. In SYN_RCVD, we resend the
765  SYNACK that we sent earlier and in LAST_ACK we have to
766  retransmit our FINACK. */
767  UIP_STAT(++uip_stat.tcp.rexmit);
768  switch(uip_connr->tcpstateflags & UIP_TS_MASK) {
769  case UIP_SYN_RCVD:
770  /* In the SYN_RCVD state, we should retransmit our
771  SYNACK. */
772  goto tcp_send_synack;
773 
774 #if UIP_ACTIVE_OPEN
775  case UIP_SYN_SENT:
776  /* In the SYN_SENT state, we retransmit out SYN. */
777  BUF->flags = 0;
778  goto tcp_send_syn;
779 #endif /* UIP_ACTIVE_OPEN */
780 
781  case UIP_ESTABLISHED:
782  /* In the ESTABLISHED state, we call upon the application
783  to do the actual retransmit after which we jump into
784  the code for sending out the packet (the apprexmit
785  label). */
786  uip_flags = UIP_REXMIT;
787  UIP_APPCALL();
788  goto apprexmit;
789 
790  case UIP_FIN_WAIT_1:
791  case UIP_CLOSING:
792  case UIP_LAST_ACK:
793  /* In all these states we should retransmit a FINACK. */
794  goto tcp_send_finack;
795 
796  }
797  }
798  } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) {
799  /* If there was no need for a retransmission, we poll the
800  application for new data. */
801  uip_flags = UIP_POLL;
802  UIP_APPCALL();
803  goto appsend;
804  }
805  }
806 #endif
807  goto drop;
808  }
809 #if UIP_UDP
810  if(flag == UIP_UDP_TIMER) {
811  if(uip_udp_conn->lport != 0) {
812  uip_conn = NULL;
813  uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
814  uip_len = uip_slen = 0;
815  uip_flags = UIP_POLL;
816  UIP_UDP_APPCALL();
817  goto udp_send;
818  } else {
819  goto drop;
820  }
821  }
822 #endif
823 
824  /* This is where the input processing starts. */
825  UIP_STAT(++uip_stat.ip.recv);
826 
827  /* Start of IP input header processing code. */
828 
829 #if NETSTACK_CONF_WITH_IPV6
830  /* Check validity of the IP header. */
831  if((BUF->vtc & 0xf0) != 0x60) { /* IP version and header length. */
832  UIP_STAT(++uip_stat.ip.drop);
833  UIP_STAT(++uip_stat.ip.vhlerr);
834  UIP_LOG("ipv6: invalid version.");
835  goto drop;
836  }
837 #else /* NETSTACK_CONF_WITH_IPV6 */
838  /* Check validity of the IP header. */
839  if(BUF->vhl != 0x45) { /* IP version and header length. */
840  UIP_STAT(++uip_stat.ip.drop);
841  UIP_STAT(++uip_stat.ip.vhlerr);
842  UIP_LOG("ip: invalid version or header length.");
843  goto drop;
844  }
845 #endif /* NETSTACK_CONF_WITH_IPV6 */
846 
847  /* Check the size of the packet. If the size reported to us in
848  uip_len is smaller the size reported in the IP header, we assume
849  that the packet has been corrupted in transit. If the size of
850  uip_len is larger than the size reported in the IP packet header,
851  the packet has been padded and we set uip_len to the correct
852  value. */
853 
854  if((BUF->len[0] << 8) + BUF->len[1] <= uip_len) {
855  uip_len = (BUF->len[0] << 8) + BUF->len[1];
856 #if NETSTACK_CONF_WITH_IPV6
857  uip_len += 40; /* The length reported in the IPv6 header is the
858  length of the payload that follows the
859  header. However, uIP uses the uip_len variable
860  for holding the size of the entire packet,
861  including the IP header. For IPv4 this is not a
862  problem as the length field in the IPv4 header
863  contains the length of the entire packet. But
864  for IPv6 we need to add the size of the IPv6
865  header (40 bytes). */
866 #endif /* NETSTACK_CONF_WITH_IPV6 */
867  } else {
868  UIP_LOG("ip: packet shorter than reported in IP header.");
869  goto drop;
870  }
871 
872 #if !NETSTACK_CONF_WITH_IPV6
873  /* Check the fragment flag. */
874  if((BUF->ipoffset[0] & 0x3f) != 0 ||
875  BUF->ipoffset[1] != 0) {
876 #if UIP_REASSEMBLY
877  uip_len = uip_reass();
878  if(uip_len == 0) {
879  goto drop;
880  }
881 #else /* UIP_REASSEMBLY */
882  UIP_STAT(++uip_stat.ip.drop);
883  UIP_STAT(++uip_stat.ip.fragerr);
884  UIP_LOG("ip: fragment dropped.");
885  goto drop;
886 #endif /* UIP_REASSEMBLY */
887  }
888 #endif /* NETSTACK_CONF_WITH_IPV6 */
889 
890  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) {
891  /* If we are configured to use ping IP address configuration and
892  haven't been assigned an IP address yet, we accept all ICMP
893  packets. */
894 #if UIP_PINGADDRCONF && !NETSTACK_CONF_WITH_IPV6
895  if(BUF->proto == UIP_PROTO_ICMP) {
896  UIP_LOG("ip: possible ping config packet received.");
897  goto icmp_input;
898  } else {
899  UIP_LOG("ip: packet dropped since no address assigned.");
900  goto drop;
901  }
902 #endif /* UIP_PINGADDRCONF */
903 
904  } else {
905  /* If IP broadcast support is configured, we check for a broadcast
906  UDP packet, which may be destined to us. */
907 #if UIP_BROADCAST
908  DEBUG_PRINTF("UDP IP checksum 0x%04x\n", uip_ipchksum());
909  if(BUF->proto == UIP_PROTO_UDP &&
910  (uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr) ||
911  (BUF->destipaddr.u8[0] & 224) == 224)) { /* XXX this is a
912  hack to be able
913  to receive UDP
914  multicast
915  packets. We check
916  for the bit
917  pattern of the
918  multicast
919  prefix. */
920  goto udp_input;
921  }
922 #endif /* UIP_BROADCAST */
923 
924  /* Check if the packet is destined for our IP address. */
925 #if !NETSTACK_CONF_WITH_IPV6
926  if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) {
927  UIP_STAT(++uip_stat.ip.drop);
928  goto drop;
929  }
930 #else /* NETSTACK_CONF_WITH_IPV6 */
931  /* For IPv6, packet reception is a little trickier as we need to
932  make sure that we listen to certain multicast addresses (all
933  hosts multicast address, and the solicited-node multicast
934  address) as well. However, we will cheat here and accept all
935  multicast packets that are sent to the ff02::/16 addresses. */
936  if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr) &&
937  BUF->destipaddr.u16[0] != UIP_HTONS(0xff02)) {
938  UIP_STAT(++uip_stat.ip.drop);
939  goto drop;
940  }
941 #endif /* NETSTACK_CONF_WITH_IPV6 */
942  }
943 
944 #if !NETSTACK_CONF_WITH_IPV6
945  if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
946  checksum. */
947  UIP_STAT(++uip_stat.ip.drop);
948  UIP_STAT(++uip_stat.ip.chkerr);
949  UIP_LOG("ip: bad checksum.");
950  goto drop;
951  }
952 #endif /* NETSTACK_CONF_WITH_IPV6 */
953 
954 #if UIP_TCP
955  if(BUF->proto == UIP_PROTO_TCP) { /* Check for TCP packet. If so,
956  proceed with TCP input
957  processing. */
958  goto tcp_input;
959  }
960 #endif
961 
962 #if UIP_UDP
963  if(BUF->proto == UIP_PROTO_UDP) {
964  goto udp_input;
965  }
966 #endif /* UIP_UDP */
967 
968 #if !NETSTACK_CONF_WITH_IPV6
969  /* ICMPv4 processing code follows. */
970  if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from
971  here. */
972  UIP_STAT(++uip_stat.ip.drop);
973  UIP_STAT(++uip_stat.ip.protoerr);
974  UIP_LOG("ip: neither tcp nor icmp.");
975  goto drop;
976  }
977 
978 #if UIP_PINGADDRCONF
979  icmp_input:
980 #endif /* UIP_PINGADDRCONF */
981  UIP_STAT(++uip_stat.icmp.recv);
982 
983  /* ICMP echo (i.e., ping) processing. This is simple, we only change
984  the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
985  checksum before we return the packet. */
986  if(ICMPBUF->type != ICMP_ECHO) {
987  UIP_STAT(++uip_stat.icmp.drop);
988  UIP_STAT(++uip_stat.icmp.typeerr);
989  UIP_LOG("icmp: not icmp echo.");
990  goto drop;
991  }
992 
993  /* If we are configured to use ping IP address assignment, we use
994  the destination IP address of this ping packet and assign it to
995  ourself. */
996 #if UIP_PINGADDRCONF
997  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) {
998  uip_hostaddr = BUF->destipaddr;
999  }
1000 #endif /* UIP_PINGADDRCONF */
1001 
1002  ICMPBUF->type = ICMP_ECHO_REPLY;
1003 
1004  if(ICMPBUF->icmpchksum >= UIP_HTONS(0xffff - (ICMP_ECHO << 8))) {
1005  ICMPBUF->icmpchksum += UIP_HTONS(ICMP_ECHO << 8) + 1;
1006  } else {
1007  ICMPBUF->icmpchksum += UIP_HTONS(ICMP_ECHO << 8);
1008  }
1009 
1010  /* Swap IP addresses. */
1011  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
1012  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
1013 
1014  UIP_STAT(++uip_stat.icmp.sent);
1015  BUF->ttl = UIP_TTL;
1016  goto ip_send_nolen;
1017 
1018  /* End of IPv4 input header processing code. */
1019 #else /* !NETSTACK_CONF_WITH_IPV6 */
1020 
1021  /* This is IPv6 ICMPv6 processing code. */
1022  DEBUG_PRINTF("icmp6_input: length %d\n", uip_len);
1023 
1024  if(BUF->proto != UIP_PROTO_ICMP6) { /* We only allow ICMPv6 packets from
1025  here. */
1026  UIP_STAT(++uip_stat.ip.drop);
1027  UIP_STAT(++uip_stat.ip.protoerr);
1028  UIP_LOG("ip: neither tcp nor icmp6.");
1029  goto drop;
1030  }
1031 
1032  UIP_STAT(++uip_stat.icmp.recv);
1033 
1034  /* If we get a neighbor solicitation for our address we should send
1035  a neighbor advertisement message back. */
1036  if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) {
1037  if(uip_ipaddr_cmp(&ICMPBUF->icmp6data, &uip_hostaddr)) {
1038 
1039  if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) {
1040  /* Save the sender's address in our neighbor list. */
1041  uip_neighbor_add(&ICMPBUF->srcipaddr, &(ICMPBUF->options[2]));
1042  }
1043 
1044  /* We should now send a neighbor advertisement back to where the
1045  neighbor solicication came from. */
1046  ICMPBUF->type = ICMP6_NEIGHBOR_ADVERTISEMENT;
1047  ICMPBUF->flags = ICMP6_FLAG_S; /* Solicited flag. */
1048 
1049  ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0;
1050 
1051  uip_ipaddr_copy(&ICMPBUF->destipaddr, &ICMPBUF->srcipaddr);
1052  uip_ipaddr_copy(&ICMPBUF->srcipaddr, &uip_hostaddr);
1053  ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS;
1054  ICMPBUF->options[1] = 1; /* Options length, 1 = 8 bytes. */
1055  memcpy(&(ICMPBUF->options[2]), &uip_lladdr, sizeof(uip_lladdr));
1056  ICMPBUF->icmpchksum = 0;
1057  ICMPBUF->icmpchksum = ~uip_icmp6chksum();
1058 
1059  goto send;
1060 
1061  }
1062  goto drop;
1063  } else if(ICMPBUF->type == ICMP6_ECHO) {
1064  /* ICMP echo (i.e., ping) processing. This is simple, we only
1065  change the ICMP type from ECHO to ECHO_REPLY and update the
1066  ICMP checksum before we return the packet. */
1067 
1068  ICMPBUF->type = ICMP6_ECHO_REPLY;
1069 
1070  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
1071  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
1072  ICMPBUF->icmpchksum = 0;
1073  ICMPBUF->icmpchksum = ~uip_icmp6chksum();
1074 
1075  UIP_STAT(++uip_stat.icmp.sent);
1076  goto send;
1077  } else {
1078  DEBUG_PRINTF("Unknown icmp6 message type %d\n", ICMPBUF->type);
1079  UIP_STAT(++uip_stat.icmp.drop);
1080  UIP_STAT(++uip_stat.icmp.typeerr);
1081  UIP_LOG("icmp: unknown ICMP message.");
1082  goto drop;
1083  }
1084 
1085  /* End of IPv6 ICMP processing. */
1086 
1087 #endif /* !NETSTACK_CONF_WITH_IPV6 */
1088 
1089 #if UIP_UDP
1090  /* UDP input processing. */
1091  udp_input:
1092  /* UDP processing is really just a hack. We don't do anything to the
1093  UDP/IP headers, but let the UDP application do all the hard
1094  work. If the application sets uip_slen, it has a packet to
1095  send. */
1096 #if UIP_UDP_CHECKSUMS
1097  uip_len = uip_len - UIP_IPUDPH_LEN;
1098  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
1099  if(UDPBUF->udpchksum != 0 && uip_udpchksum() != 0xffff) {
1100  UIP_STAT(++uip_stat.udp.drop);
1101  UIP_STAT(++uip_stat.udp.chkerr);
1102  UIP_LOG("udp: bad checksum.");
1103  goto drop;
1104  }
1105 #else /* UIP_UDP_CHECKSUMS */
1106  uip_len = uip_len - UIP_IPUDPH_LEN;
1107 #endif /* UIP_UDP_CHECKSUMS */
1108 
1109  /* Make sure that the UDP destination port number is not zero. */
1110  if(UDPBUF->destport == 0) {
1111  UIP_LOG("udp: zero port.");
1112  goto drop;
1113  }
1114 
1115  /* Demultiplex this UDP packet between the UDP "connections". */
1116  for(uip_udp_conn = &uip_udp_conns[0];
1117  uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
1118  ++uip_udp_conn) {
1119  /* If the local UDP port is non-zero, the connection is considered
1120  to be used. If so, the local port number is checked against the
1121  destination port number in the received packet. If the two port
1122  numbers match, the remote port number is checked if the
1123  connection is bound to a remote port. Finally, if the
1124  connection is bound to a remote IP address, the source IP
1125  address of the packet is checked. */
1126  if(uip_udp_conn->lport != 0 &&
1127  UDPBUF->destport == uip_udp_conn->lport &&
1128  (uip_udp_conn->rport == 0 ||
1129  UDPBUF->srcport == uip_udp_conn->rport) &&
1130  (uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_all_zeroes_addr) ||
1131  uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_broadcast_addr) ||
1132  uip_ipaddr_cmp(&BUF->srcipaddr, &uip_udp_conn->ripaddr))) {
1133  goto udp_found;
1134  }
1135  }
1136  UIP_LOG("udp: no matching connection found");
1137  UIP_STAT(++uip_stat.udp.drop);
1138 #if UIP_CONF_ICMP_DEST_UNREACH && !NETSTACK_CONF_WITH_IPV6
1139  /* Copy fields from packet header into payload of this ICMP packet. */
1140  memcpy(&(ICMPBUF->payload[0]), ICMPBUF, UIP_IPH_LEN + 8);
1141 
1142  /* Set the ICMP type and code. */
1143  ICMPBUF->type = ICMP_DEST_UNREACHABLE;
1144  ICMPBUF->icode = ICMP_PORT_UNREACHABLE;
1145 
1146  /* Calculate the ICMP checksum. */
1147  ICMPBUF->icmpchksum = 0;
1148  ICMPBUF->icmpchksum = ~uip_chksum((uint16_t *)&(ICMPBUF->type), 36);
1149 
1150  /* Set the IP destination address to be the source address of the
1151  original packet. */
1152  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
1153 
1154  /* Set our IP address as the source address. */
1155  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
1156 
1157  /* The size of the ICMP destination unreachable packet is 36 + the
1158  size of the IP header (20) = 56. */
1159  uip_len = 36 + UIP_IPH_LEN;
1160  ICMPBUF->len[0] = 0;
1161  ICMPBUF->len[1] = (uint8_t)uip_len;
1162  ICMPBUF->ttl = UIP_TTL;
1163  ICMPBUF->proto = UIP_PROTO_ICMP;
1164 
1165  goto ip_send_nolen;
1166 #else /* UIP_CONF_ICMP_DEST_UNREACH */
1167  goto drop;
1168 #endif /* UIP_CONF_ICMP_DEST_UNREACH */
1169 
1170  udp_found:
1171  UIP_STAT(++uip_stat.udp.recv);
1172  uip_conn = NULL;
1173  uip_flags = UIP_NEWDATA;
1174  uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
1175  uip_slen = 0;
1176  UIP_UDP_APPCALL();
1177 
1178  udp_send:
1179  if(uip_slen == 0) {
1180  goto drop;
1181  }
1182  uip_len = uip_slen + UIP_IPUDPH_LEN;
1183 
1184 #if NETSTACK_CONF_WITH_IPV6
1185  /* For IPv6, the IP length field does not include the IPv6 IP header
1186  length. */
1187  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
1188  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
1189 #else /* NETSTACK_CONF_WITH_IPV6 */
1190  BUF->len[0] = (uip_len >> 8);
1191  BUF->len[1] = (uip_len & 0xff);
1192 #endif /* NETSTACK_CONF_WITH_IPV6 */
1193 
1194  BUF->ttl = uip_udp_conn->ttl;
1195  BUF->proto = UIP_PROTO_UDP;
1196 
1197  UDPBUF->udplen = UIP_HTONS(uip_slen + UIP_UDPH_LEN);
1198  UDPBUF->udpchksum = 0;
1199 
1200  BUF->srcport = uip_udp_conn->lport;
1201  BUF->destport = uip_udp_conn->rport;
1202 
1203  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
1204  uip_ipaddr_copy(&BUF->destipaddr, &uip_udp_conn->ripaddr);
1205 
1206  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];
1207 
1208 #if UIP_UDP_CHECKSUMS
1209  /* Calculate UDP checksum. */
1210  UDPBUF->udpchksum = ~(uip_udpchksum());
1211  if(UDPBUF->udpchksum == 0) {
1212  UDPBUF->udpchksum = 0xffff;
1213  }
1214 #endif /* UIP_UDP_CHECKSUMS */
1215 
1216  UIP_STAT(++uip_stat.udp.sent);
1217  goto ip_send_nolen;
1218 #endif /* UIP_UDP */
1219 
1220  /* TCP input processing. */
1221 #if UIP_TCP
1222  tcp_input:
1223  UIP_STAT(++uip_stat.tcp.recv);
1224 
1225  /* Start of TCP input header processing code. */
1226 
1227  if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
1228  checksum. */
1229  UIP_STAT(++uip_stat.tcp.drop);
1230  UIP_STAT(++uip_stat.tcp.chkerr);
1231  UIP_LOG("tcp: bad checksum.");
1232  goto drop;
1233  }
1234 
1235  /* Make sure that the TCP port number is not zero. */
1236  if(BUF->destport == 0 || BUF->srcport == 0) {
1237  UIP_LOG("tcp: zero port.");
1238  goto drop;
1239  }
1240 
1241  /* Demultiplex this segment. */
1242  /* First check any active connections. */
1243  for(uip_connr = &uip_conns[0]; uip_connr <= &uip_conns[UIP_CONNS - 1];
1244  ++uip_connr) {
1245  if(uip_connr->tcpstateflags != UIP_CLOSED &&
1246  BUF->destport == uip_connr->lport &&
1247  BUF->srcport == uip_connr->rport &&
1248  uip_ipaddr_cmp(&BUF->srcipaddr, &uip_connr->ripaddr)) {
1249  goto found;
1250  }
1251  }
1252 
1253  /* If we didn't find an active connection that expected the packet,
1254  either this packet is an old duplicate, or this is a SYN packet
1255  destined for a connection in LISTEN. If the SYN flag isn't set,
1256  it is an old packet and we send a RST. */
1257  if((BUF->flags & TCP_CTL) != TCP_SYN) {
1258  goto reset;
1259  }
1260 
1261  tmp16 = BUF->destport;
1262  /* Next, check listening connections. */
1263  for(c = 0; c < UIP_LISTENPORTS; ++c) {
1264  if(tmp16 == uip_listenports[c]) {
1265  goto found_listen;
1266  }
1267  }
1268 
1269  /* No matching connection found, so we send a RST packet. */
1270  UIP_STAT(++uip_stat.tcp.synrst);
1271 
1272  reset:
1273  /* We do not send resets in response to resets. */
1274  if(BUF->flags & TCP_RST) {
1275  goto drop;
1276  }
1277 
1278  UIP_STAT(++uip_stat.tcp.rst);
1279 
1280  BUF->flags = TCP_RST | TCP_ACK;
1281  uip_len = UIP_IPTCPH_LEN;
1282  BUF->tcpoffset = 5 << 4;
1283 
1284  /* Flip the seqno and ackno fields in the TCP header. */
1285  c = BUF->seqno[3];
1286  BUF->seqno[3] = BUF->ackno[3];
1287  BUF->ackno[3] = c;
1288 
1289  c = BUF->seqno[2];
1290  BUF->seqno[2] = BUF->ackno[2];
1291  BUF->ackno[2] = c;
1292 
1293  c = BUF->seqno[1];
1294  BUF->seqno[1] = BUF->ackno[1];
1295  BUF->ackno[1] = c;
1296 
1297  c = BUF->seqno[0];
1298  BUF->seqno[0] = BUF->ackno[0];
1299  BUF->ackno[0] = c;
1300 
1301  /* We also have to increase the sequence number we are
1302  acknowledging. If the least significant byte overflowed, we need
1303  to propagate the carry to the other bytes as well. */
1304  if(++BUF->ackno[3] == 0) {
1305  if(++BUF->ackno[2] == 0) {
1306  if(++BUF->ackno[1] == 0) {
1307  ++BUF->ackno[0];
1308  }
1309  }
1310  }
1311 
1312  /* Swap port numbers. */
1313  tmp16 = BUF->srcport;
1314  BUF->srcport = BUF->destport;
1315  BUF->destport = tmp16;
1316 
1317  /* Swap IP addresses. */
1318  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
1319  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
1320 
1321  /* And send out the RST packet! */
1322  goto tcp_send_noconn;
1323 
1324  /* This label will be jumped to if we matched the incoming packet
1325  with a connection in LISTEN. In that case, we should create a new
1326  connection and send a SYNACK in return. */
1327  found_listen:
1328  /* First we check if there are any connections avaliable. Unused
1329  connections are kept in the same table as used connections, but
1330  unused ones have the tcpstate set to CLOSED. Also, connections in
1331  TIME_WAIT are kept track of and we'll use the oldest one if no
1332  CLOSED connections are found. Thanks to Eddie C. Dost for a very
1333  nice algorithm for the TIME_WAIT search. */
1334  uip_connr = 0;
1335  for(c = 0; c < UIP_CONNS; ++c) {
1336  if(uip_conns[c].tcpstateflags == UIP_CLOSED) {
1337  uip_connr = &uip_conns[c];
1338  break;
1339  }
1340  if(uip_conns[c].tcpstateflags == UIP_TIME_WAIT) {
1341  if(uip_connr == 0 ||
1342  uip_conns[c].timer > uip_connr->timer) {
1343  uip_connr = &uip_conns[c];
1344  }
1345  }
1346  }
1347 
1348  if(uip_connr == 0) {
1349  /* All connections are used already, we drop packet and hope that
1350  the remote end will retransmit the packet at a time when we
1351  have more spare connections. */
1352  UIP_STAT(++uip_stat.tcp.syndrop);
1353  UIP_LOG("tcp: found no unused connections.");
1354  goto drop;
1355  }
1356  uip_conn = uip_connr;
1357 
1358  /* Fill in the necessary fields for the new connection. */
1359  uip_connr->rto = uip_connr->timer = UIP_RTO;
1360  uip_connr->sa = 0;
1361  uip_connr->sv = 4;
1362  uip_connr->nrtx = 0;
1363  uip_connr->lport = BUF->destport;
1364  uip_connr->rport = BUF->srcport;
1365  uip_ipaddr_copy(&uip_connr->ripaddr, &BUF->srcipaddr);
1366  uip_connr->tcpstateflags = UIP_SYN_RCVD;
1367 
1368  uip_connr->snd_nxt[0] = iss[0];
1369  uip_connr->snd_nxt[1] = iss[1];
1370  uip_connr->snd_nxt[2] = iss[2];
1371  uip_connr->snd_nxt[3] = iss[3];
1372  uip_connr->len = 1;
1373 
1374  /* rcv_nxt should be the seqno from the incoming packet + 1. */
1375  uip_connr->rcv_nxt[0] = BUF->seqno[0];
1376  uip_connr->rcv_nxt[1] = BUF->seqno[1];
1377  uip_connr->rcv_nxt[2] = BUF->seqno[2];
1378  uip_connr->rcv_nxt[3] = BUF->seqno[3];
1379  uip_add_rcv_nxt(1);
1380 
1381  /* Parse the TCP MSS option, if present. */
1382  if((BUF->tcpoffset & 0xf0) > 0x50) {
1383  for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
1384  opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
1385  if(opt == TCP_OPT_END) {
1386  /* End of options. */
1387  break;
1388  } else if(opt == TCP_OPT_NOOP) {
1389  ++c;
1390  /* NOP option. */
1391  } else if(opt == TCP_OPT_MSS &&
1392  uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
1393  /* An MSS option with the right option length. */
1394  tmp16 = ((uint16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
1395  (uint16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c];
1396  uip_connr->initialmss = uip_connr->mss =
1397  tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
1398 
1399  /* And we are done processing options. */
1400  break;
1401  } else {
1402  /* All other options have a length field, so that we easily
1403  can skip past them. */
1404  if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
1405  /* If the length field is zero, the options are malformed
1406  and we don't process them further. */
1407  break;
1408  }
1409  c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
1410  }
1411  }
1412  }
1413 
1414  /* Our response will be a SYNACK. */
1415 #if UIP_ACTIVE_OPEN
1416  tcp_send_synack:
1417  BUF->flags = TCP_ACK;
1418 
1419  tcp_send_syn:
1420  BUF->flags |= TCP_SYN;
1421 #else /* UIP_ACTIVE_OPEN */
1422  tcp_send_synack:
1423  BUF->flags = TCP_SYN | TCP_ACK;
1424 #endif /* UIP_ACTIVE_OPEN */
1425 
1426  /* We send out the TCP Maximum Segment Size option with our
1427  SYNACK. */
1428  BUF->optdata[0] = TCP_OPT_MSS;
1429  BUF->optdata[1] = TCP_OPT_MSS_LEN;
1430  BUF->optdata[2] = (UIP_TCP_MSS) / 256;
1431  BUF->optdata[3] = (UIP_TCP_MSS) & 255;
1432  uip_len = UIP_IPTCPH_LEN + TCP_OPT_MSS_LEN;
1433  BUF->tcpoffset = ((UIP_TCPH_LEN + TCP_OPT_MSS_LEN) / 4) << 4;
1434  goto tcp_send;
1435 
1436  /* This label will be jumped to if we found an active connection. */
1437  found:
1438  uip_conn = uip_connr;
1439  uip_flags = 0;
1440  /* We do a very naive form of TCP reset processing; we just accept
1441  any RST and kill our connection. We should in fact check if the
1442  sequence number of this reset is within our advertised window
1443  before we accept the reset. */
1444  if(BUF->flags & TCP_RST) {
1445  uip_connr->tcpstateflags = UIP_CLOSED;
1446  UIP_LOG("tcp: got reset, aborting connection.");
1447  uip_flags = UIP_ABORT;
1448  UIP_APPCALL();
1449  goto drop;
1450  }
1451  /* Calculate the length of the data, if the application has sent
1452  any data to us. */
1453  c = (BUF->tcpoffset >> 4) << 2;
1454  /* uip_len will contain the length of the actual TCP data. This is
1455  calculated by subtracing the length of the TCP header (in
1456  c) and the length of the IP header (20 bytes). */
1457  uip_len = uip_len - c - UIP_IPH_LEN;
1458 
1459  /* First, check if the sequence number of the incoming packet is
1460  what we're expecting next. If not, we send out an ACK with the
1461  correct numbers in, unless we are in the SYN_RCVD state and
1462  receive a SYN, in which case we should retransmit our SYNACK
1463  (which is done futher down). */
1464  if(!((((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&
1465  ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK))) ||
1466  (((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_RCVD) &&
1467  ((BUF->flags & TCP_CTL) == TCP_SYN)))) {
1468  if((uip_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) &&
1469  (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
1470  BUF->seqno[1] != uip_connr->rcv_nxt[1] ||
1471  BUF->seqno[2] != uip_connr->rcv_nxt[2] ||
1472  BUF->seqno[3] != uip_connr->rcv_nxt[3])) {
1473  goto tcp_send_ack;
1474  }
1475  }
1476 
1477  /* Next, check if the incoming segment acknowledges any outstanding
1478  data. If so, we update the sequence number, reset the length of
1479  the outstanding data, calculate RTT estimations, and reset the
1480  retransmission timer. */
1481  if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {
1482  uip_add32(uip_connr->snd_nxt, uip_connr->len);
1483 
1484  if(BUF->ackno[0] == uip_acc32[0] &&
1485  BUF->ackno[1] == uip_acc32[1] &&
1486  BUF->ackno[2] == uip_acc32[2] &&
1487  BUF->ackno[3] == uip_acc32[3]) {
1488  /* Update sequence number. */
1489  uip_connr->snd_nxt[0] = uip_acc32[0];
1490  uip_connr->snd_nxt[1] = uip_acc32[1];
1491  uip_connr->snd_nxt[2] = uip_acc32[2];
1492  uip_connr->snd_nxt[3] = uip_acc32[3];
1493 
1494  /* Do RTT estimation, unless we have done retransmissions. */
1495  if(uip_connr->nrtx == 0) {
1496  signed char m;
1497  m = uip_connr->rto - uip_connr->timer;
1498  /* This is taken directly from VJs original code in his paper */
1499  m = m - (uip_connr->sa >> 3);
1500  uip_connr->sa += m;
1501  if(m < 0) {
1502  m = -m;
1503  }
1504  m = m - (uip_connr->sv >> 2);
1505  uip_connr->sv += m;
1506  uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;
1507 
1508  }
1509  /* Set the acknowledged flag. */
1510  uip_flags = UIP_ACKDATA;
1511  /* Reset the retransmission timer. */
1512  uip_connr->timer = uip_connr->rto;
1513 
1514  /* Reset length of outstanding data. */
1515  uip_connr->len = 0;
1516  }
1517 
1518  }
1519 
1520  /* Do different things depending on in what state the connection is. */
1521  switch(uip_connr->tcpstateflags & UIP_TS_MASK) {
1522  /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
1523  implemented, since we force the application to close when the
1524  peer sends a FIN (hence the application goes directly from
1525  ESTABLISHED to LAST_ACK). */
1526  case UIP_SYN_RCVD:
1527  /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
1528  we are waiting for an ACK that acknowledges the data we sent
1529  out the last time. Therefore, we want to have the UIP_ACKDATA
1530  flag set. If so, we enter the ESTABLISHED state. */
1531  if(uip_flags & UIP_ACKDATA) {
1532  uip_connr->tcpstateflags = UIP_ESTABLISHED;
1533  uip_flags = UIP_CONNECTED;
1534  uip_connr->len = 0;
1535  if(uip_len > 0) {
1536  uip_flags |= UIP_NEWDATA;
1537  uip_add_rcv_nxt(uip_len);
1538  }
1539  uip_slen = 0;
1540  UIP_APPCALL();
1541  goto appsend;
1542  }
1543  /* We need to retransmit the SYNACK */
1544  if((BUF->flags & TCP_CTL) == TCP_SYN) {
1545  goto tcp_send_synack;
1546  }
1547  goto drop;
1548 #if UIP_ACTIVE_OPEN
1549  case UIP_SYN_SENT:
1550  /* In SYN_SENT, we wait for a SYNACK that is sent in response to
1551  our SYN. The rcv_nxt is set to sequence number in the SYNACK
1552  plus one, and we send an ACK. We move into the ESTABLISHED
1553  state. */
1554  if((uip_flags & UIP_ACKDATA) &&
1555  (BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)) {
1556 
1557  /* Parse the TCP MSS option, if present. */
1558  if((BUF->tcpoffset & 0xf0) > 0x50) {
1559  for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
1560  opt = uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + c];
1561  if(opt == TCP_OPT_END) {
1562  /* End of options. */
1563  break;
1564  } else if(opt == TCP_OPT_NOOP) {
1565  ++c;
1566  /* NOP option. */
1567  } else if(opt == TCP_OPT_MSS &&
1568  uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
1569  /* An MSS option with the right option length. */
1570  tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
1571  uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];
1572  uip_connr->initialmss =
1573  uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
1574 
1575  /* And we are done processing options. */
1576  break;
1577  } else {
1578  /* All other options have a length field, so that we easily
1579  can skip past them. */
1580  if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
1581  /* If the length field is zero, the options are malformed
1582  and we don't process them further. */
1583  break;
1584  }
1585  c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
1586  }
1587  }
1588  }
1589  uip_connr->tcpstateflags = UIP_ESTABLISHED;
1590  uip_connr->rcv_nxt[0] = BUF->seqno[0];
1591  uip_connr->rcv_nxt[1] = BUF->seqno[1];
1592  uip_connr->rcv_nxt[2] = BUF->seqno[2];
1593  uip_connr->rcv_nxt[3] = BUF->seqno[3];
1594  uip_add_rcv_nxt(1);
1595  uip_flags = UIP_CONNECTED | UIP_NEWDATA;
1596  uip_connr->len = 0;
1597  uip_clear_buf();
1598  uip_slen = 0;
1599  UIP_APPCALL();
1600  goto appsend;
1601  }
1602  /* Inform the application that the connection failed */
1603  uip_flags = UIP_ABORT;
1604  UIP_APPCALL();
1605  /* The connection is closed after we send the RST */
1606  uip_conn->tcpstateflags = UIP_CLOSED;
1607  goto reset;
1608 #endif /* UIP_ACTIVE_OPEN */
1609 
1610  case UIP_ESTABLISHED:
1611  /* In the ESTABLISHED state, we call upon the application to feed
1612  data into the uip_buf. If the UIP_ACKDATA flag is set, the
1613  application should put new data into the buffer, otherwise we are
1614  retransmitting an old segment, and the application should put that
1615  data into the buffer.
1616 
1617  If the incoming packet is a FIN, we should close the connection on
1618  this side as well, and we send out a FIN and enter the LAST_ACK
1619  state. We require that there is no outstanding data; otherwise the
1620  sequence numbers will be screwed up. */
1621 
1622  if(BUF->flags & TCP_FIN && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
1623  if(uip_outstanding(uip_connr)) {
1624  goto drop;
1625  }
1626  uip_add_rcv_nxt(1 + uip_len);
1627  uip_flags |= UIP_CLOSE;
1628  if(uip_len > 0) {
1629  uip_flags |= UIP_NEWDATA;
1630  }
1631  UIP_APPCALL();
1632  uip_connr->len = 1;
1633  uip_connr->tcpstateflags = UIP_LAST_ACK;
1634  uip_connr->nrtx = 0;
1635  tcp_send_finack:
1636  BUF->flags = TCP_FIN | TCP_ACK;
1637  goto tcp_send_nodata;
1638  }
1639 
1640  /* Check the URG flag. If this is set, the segment carries urgent
1641  data that we must pass to the application. */
1642  if((BUF->flags & TCP_URG) != 0) {
1643 #if UIP_URGDATA > 0
1644  uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];
1645  if(uip_urglen > uip_len) {
1646  /* There is more urgent data in the next segment to come. */
1647  uip_urglen = uip_len;
1648  }
1649  uip_add_rcv_nxt(uip_urglen);
1650  uip_len -= uip_urglen;
1651  uip_urgdata = uip_appdata;
1652  uip_appdata += uip_urglen;
1653  } else {
1654  uip_urglen = 0;
1655 #else /* UIP_URGDATA > 0 */
1656  uip_appdata = ((char *)uip_appdata) + ((BUF->urgp[0] << 8) | BUF->urgp[1]);
1657  uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];
1658 #endif /* UIP_URGDATA > 0 */
1659  }
1660 
1661  /* If uip_len > 0 we have TCP data in the packet, and we flag this
1662  by setting the UIP_NEWDATA flag and update the sequence number
1663  we acknowledge. If the application has stopped the dataflow
1664  using uip_stop(), we must not accept any data packets from the
1665  remote host. */
1666  if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
1667  uip_flags |= UIP_NEWDATA;
1668  uip_add_rcv_nxt(uip_len);
1669  }
1670 
1671  /* Check if the available buffer space advertised by the other end
1672  is smaller than the initial MSS for this connection. If so, we
1673  set the current MSS to the window size to ensure that the
1674  application does not send more data than the other end can
1675  handle.
1676 
1677  If the remote host advertises a zero window, we set the MSS to
1678  the initial MSS so that the application will send an entire MSS
1679  of data. This data will not be acknowledged by the receiver,
1680  and the application will retransmit it. This is called the
1681  "persistent timer" and uses the retransmission mechanim.
1682  */
1683  tmp16 = ((uint16_t)BUF->wnd[0] << 8) + (uint16_t)BUF->wnd[1];
1684  if(tmp16 > uip_connr->initialmss ||
1685  tmp16 == 0) {
1686  tmp16 = uip_connr->initialmss;
1687  }
1688  uip_connr->mss = tmp16;
1689 
1690  /* If this packet constitutes an ACK for outstanding data (flagged
1691  by the UIP_ACKDATA flag, we should call the application since it
1692  might want to send more data. If the incoming packet had data
1693  from the peer (as flagged by the UIP_NEWDATA flag), the
1694  application must also be notified.
1695 
1696  When the application is called, the global variable uip_len
1697  contains the length of the incoming data. The application can
1698  access the incoming data through the global pointer
1699  uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN
1700  bytes into the uip_buf array.
1701 
1702  If the application wishes to send any data, this data should be
1703  put into the uip_appdata and the length of the data should be
1704  put into uip_len. If the application don't have any data to
1705  send, uip_len must be set to 0. */
1706  if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {
1707  uip_slen = 0;
1708  UIP_APPCALL();
1709 
1710  appsend:
1711 
1712  if(uip_flags & UIP_ABORT) {
1713  uip_slen = 0;
1714  uip_connr->tcpstateflags = UIP_CLOSED;
1715  BUF->flags = TCP_RST | TCP_ACK;
1716  goto tcp_send_nodata;
1717  }
1718 
1719  if(uip_flags & UIP_CLOSE) {
1720  uip_slen = 0;
1721  uip_connr->len = 1;
1722  uip_connr->tcpstateflags = UIP_FIN_WAIT_1;
1723  uip_connr->nrtx = 0;
1724  BUF->flags = TCP_FIN | TCP_ACK;
1725  goto tcp_send_nodata;
1726  }
1727 
1728  /* If uip_slen > 0, the application has data to be sent. */
1729  if(uip_slen > 0) {
1730 
1731  /* If the connection has acknowledged data, the contents of
1732  the ->len variable should be discarded. */
1733  if((uip_flags & UIP_ACKDATA) != 0) {
1734  uip_connr->len = 0;
1735  }
1736 
1737  /* If the ->len variable is non-zero the connection has
1738  already data in transit and cannot send anymore right
1739  now. */
1740  if(uip_connr->len == 0) {
1741 
1742  /* The application cannot send more than what is allowed by
1743  the mss (the minumum of the MSS and the available
1744  window). */
1745  if(uip_slen > uip_connr->mss) {
1746  uip_slen = uip_connr->mss;
1747  }
1748 
1749  /* Remember how much data we send out now so that we know
1750  when everything has been acknowledged. */
1751  uip_connr->len = uip_slen;
1752  } else {
1753 
1754  /* If the application already had unacknowledged data, we
1755  make sure that the application does not send (i.e.,
1756  retransmit) out more than it previously sent out. */
1757  uip_slen = uip_connr->len;
1758  }
1759  }
1760  uip_connr->nrtx = 0;
1761  apprexmit:
1762  uip_appdata = uip_sappdata;
1763 
1764  /* If the application has data to be sent, or if the incoming
1765  packet had new data in it, we must send out a packet. */
1766  if(uip_slen > 0 && uip_connr->len > 0) {
1767  /* Add the length of the IP and TCP headers. */
1768  uip_len = uip_connr->len + UIP_TCPIP_HLEN;
1769  /* We always set the ACK flag in response packets. */
1770  BUF->flags = TCP_ACK | TCP_PSH;
1771  /* Send the packet. */
1772  goto tcp_send_noopts;
1773  }
1774  /* If there is no data to send, just send out a pure ACK if
1775  there is newdata. */
1776  if(uip_flags & UIP_NEWDATA) {
1777  uip_len = UIP_TCPIP_HLEN;
1778  BUF->flags = TCP_ACK;
1779  goto tcp_send_noopts;
1780  }
1781  }
1782  goto drop;
1783  case UIP_LAST_ACK:
1784  /* We can close this connection if the peer has acknowledged our
1785  FIN. This is indicated by the UIP_ACKDATA flag. */
1786  if(uip_flags & UIP_ACKDATA) {
1787  uip_connr->tcpstateflags = UIP_CLOSED;
1788  uip_flags = UIP_CLOSE;
1789  UIP_APPCALL();
1790  }
1791  break;
1792 
1793  case UIP_FIN_WAIT_1:
1794  /* The application has closed the connection, but the remote host
1795  hasn't closed its end yet. Thus we do nothing but wait for a
1796  FIN from the other side. */
1797  if(uip_len > 0) {
1798  uip_add_rcv_nxt(uip_len);
1799  }
1800  if(BUF->flags & TCP_FIN) {
1801  if(uip_flags & UIP_ACKDATA) {
1802  uip_connr->tcpstateflags = UIP_TIME_WAIT;
1803  uip_connr->timer = 0;
1804  uip_connr->len = 0;
1805  } else {
1806  uip_connr->tcpstateflags = UIP_CLOSING;
1807  }
1808  uip_add_rcv_nxt(1);
1809  uip_flags = UIP_CLOSE;
1810  UIP_APPCALL();
1811  goto tcp_send_ack;
1812  } else if(uip_flags & UIP_ACKDATA) {
1813  uip_connr->tcpstateflags = UIP_FIN_WAIT_2;
1814  uip_connr->len = 0;
1815  goto drop;
1816  }
1817  if(uip_len > 0) {
1818  goto tcp_send_ack;
1819  }
1820  goto drop;
1821 
1822  case UIP_FIN_WAIT_2:
1823  if(uip_len > 0) {
1824  uip_add_rcv_nxt(uip_len);
1825  }
1826  if(BUF->flags & TCP_FIN) {
1827  uip_connr->tcpstateflags = UIP_TIME_WAIT;
1828  uip_connr->timer = 0;
1829  uip_add_rcv_nxt(1);
1830  uip_flags = UIP_CLOSE;
1831  UIP_APPCALL();
1832  goto tcp_send_ack;
1833  }
1834  if(uip_len > 0) {
1835  goto tcp_send_ack;
1836  }
1837  goto drop;
1838 
1839  case UIP_TIME_WAIT:
1840  goto tcp_send_ack;
1841 
1842  case UIP_CLOSING:
1843  if(uip_flags & UIP_ACKDATA) {
1844  uip_connr->tcpstateflags = UIP_TIME_WAIT;
1845  uip_connr->timer = 0;
1846  }
1847  }
1848  goto drop;
1849 
1850  /* We jump here when we are ready to send the packet, and just want
1851  to set the appropriate TCP sequence numbers in the TCP header. */
1852  tcp_send_ack:
1853  BUF->flags = TCP_ACK;
1854 
1855  tcp_send_nodata:
1856  uip_len = UIP_IPTCPH_LEN;
1857 
1858  tcp_send_noopts:
1859  BUF->tcpoffset = (UIP_TCPH_LEN / 4) << 4;
1860 
1861  /* We're done with the input processing. We are now ready to send a
1862  reply. Our job is to fill in all the fields of the TCP and IP
1863  headers before calculating the checksum and finally send the
1864  packet. */
1865  tcp_send:
1866  BUF->ackno[0] = uip_connr->rcv_nxt[0];
1867  BUF->ackno[1] = uip_connr->rcv_nxt[1];
1868  BUF->ackno[2] = uip_connr->rcv_nxt[2];
1869  BUF->ackno[3] = uip_connr->rcv_nxt[3];
1870 
1871  BUF->seqno[0] = uip_connr->snd_nxt[0];
1872  BUF->seqno[1] = uip_connr->snd_nxt[1];
1873  BUF->seqno[2] = uip_connr->snd_nxt[2];
1874  BUF->seqno[3] = uip_connr->snd_nxt[3];
1875 
1876  BUF->srcport = uip_connr->lport;
1877  BUF->destport = uip_connr->rport;
1878 
1879  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
1880  uip_ipaddr_copy(&BUF->destipaddr, &uip_connr->ripaddr);
1881 
1882  if(uip_connr->tcpstateflags & UIP_STOPPED) {
1883  /* If the connection has issued uip_stop(), we advertise a zero
1884  window so that the remote host will stop sending data. */
1885  BUF->wnd[0] = BUF->wnd[1] = 0;
1886  } else {
1887  BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);
1888  BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);
1889  }
1890 
1891  tcp_send_noconn:
1892  BUF->proto = UIP_PROTO_TCP;
1893 
1894  BUF->ttl = UIP_TTL;
1895 #if NETSTACK_CONF_WITH_IPV6
1896  /* For IPv6, the IP length field does not include the IPv6 IP header
1897  length. */
1898  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
1899  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
1900 #else /* NETSTACK_CONF_WITH_IPV6 */
1901  BUF->len[0] = (uip_len >> 8);
1902  BUF->len[1] = (uip_len & 0xff);
1903 #endif /* NETSTACK_CONF_WITH_IPV6 */
1904 
1905  BUF->urgp[0] = BUF->urgp[1] = 0;
1906 
1907  /* Calculate TCP checksum. */
1908  BUF->tcpchksum = 0;
1909  BUF->tcpchksum = ~(uip_tcpchksum());
1910 #endif
1911 
1912  ip_send_nolen:
1913 #if NETSTACK_CONF_WITH_IPV6
1914  BUF->vtc = 0x60;
1915  BUF->tcflow = 0x00;
1916  BUF->flow = 0x00;
1917 #else /* NETSTACK_CONF_WITH_IPV6 */
1918  BUF->vhl = 0x45;
1919  BUF->tos = 0;
1920  BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
1921  ++ipid;
1922  BUF->ipid[0] = ipid >> 8;
1923  BUF->ipid[1] = ipid & 0xff;
1924  /* Calculate IP checksum. */
1925  BUF->ipchksum = 0;
1926  BUF->ipchksum = ~(uip_ipchksum());
1927  DEBUG_PRINTF("uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum());
1928 #endif /* NETSTACK_CONF_WITH_IPV6 */
1929  UIP_STAT(++uip_stat.tcp.sent);
1930 #if NETSTACK_CONF_WITH_IPV6
1931  send:
1932 #endif /* NETSTACK_CONF_WITH_IPV6 */
1933  DEBUG_PRINTF("Sending packet with length %d (%d)\n", uip_len,
1934  (BUF->len[0] << 8) | BUF->len[1]);
1935 
1936  UIP_STAT(++uip_stat.ip.sent);
1937  /* Return and let the caller do the actual transmission. */
1938  uip_flags = 0;
1939  return;
1940 
1941  drop:
1942  uip_clear_buf();
1943  uip_flags = 0;
1944  return;
1945 }
1946 /*---------------------------------------------------------------------------*/
1947 uint16_t
1948 uip_htons(uint16_t val)
1949 {
1950  return UIP_HTONS(val);
1951 }
1952 
1953 uint32_t
1954 uip_htonl(uint32_t val)
1955 {
1956  return UIP_HTONL(val);
1957 }
1958 /*---------------------------------------------------------------------------*/
1959 void
1960 uip_send(const void *data, int len)
1961 {
1962  int copylen;
1963  copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN -
1964  (int)((char *)uip_sappdata - (char *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN]));
1965  if(copylen > 0) {
1966  uip_slen = copylen;
1967  if(data != uip_sappdata) {
1968  memcpy(uip_sappdata, (data), uip_slen);
1969  }
1970  }
1971 }
1972 /*---------------------------------------------------------------------------*/
1973 /** @}*/
void uip_log(char *msg)
Print out a uIP log message.
Definition: uip-log.c:3
uint16_t uip_ipchksum(void)
Calculate the IP header checksum of the packet header in uip_buf.
Definition: uip.c:302
#define ICMP6_ECHO_REPLY
Echo reply.
Definition: uip-icmp6.h:58
struct uip_conn * uip_conn
Pointer to the current TCP connection.
Definition: uip.c:144
#define UIP_REASS_MAXAGE
The maximum time an IP fragment should wait in the reassembly buffer before it is dropped...
Definition: uipopt.h:253
uint16_t initialmss
Initial maximum segment size for the connection.
Definition: uip.h:1365
#define UIP_MAXRTX
The maximum number of times a segment should be retransmitted before the connection should be aborted...
Definition: uipopt.h:462
uint8_t sa
Retransmission time-out calculation state variable.
Definition: uip.h:1366
void * uip_appdata
Pointer to the application data in the packet buffer.
Definition: uip.c:124
uint8_t tcpstateflags
TCP state and flags.
Definition: uip.h:1369
The structure holding the TCP/IP statistics that are gathered if UIP_STATISTICS is set to 1...
Definition: uip.h:1456
uip_ipaddr_t ripaddr
The IP address of the remote peer.
Definition: uip.h:1406
Representation of a uIP TCP connection.
Definition: uip.h:1353
struct uip_udp_conn * uip_udp_conn
The current UDP connection.
Definition: uip.c:154
Configuration options for uIP.
#define UIP_TTL
The IP TTL (time to live) of IP packets sent by uIP.
Definition: uipopt.h:245
void uip_listen(uint16_t port)
Start listening to the specified port.
Definition: uip.c:518
#define UIP_CONNS
The maximum number of simultaneously open TCP connections.
Definition: uipopt.h:419
uint8_t uip_acc32[4]
4-byte array used for the 32-bit sequence number calculations.
Definition: uip.c:173
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1239
void uip_setipid(uint16_t id)
uIP initialization function.
Definition: uip.c:162
uint16_t uip_chksum(uint16_t *data, uint16_t len)
Calculate the Internet checksum over a buffer.
Definition: uip.c:295
Default definitions of C compiler quirk work-arounds.
Representation of a uIP UDP connection.
Definition: uip.h:1405
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:160
A timer.
Definition: timer.h:86
uint16_t uip_icmp6chksum(void)
Calculate the ICMP checksum of the packet in uip_buf.
Definition: uip.c:340
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition: uip.h:523
void uip_add32(uint8_t *op32, uint16_t op16)
Carry out a 32-bit addition.
Definition: uip.c:233
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
uint8_t rcv_nxt[4]
The sequence number that we expect to receive next.
Definition: uip.h:1360
Declarations of architecture specific functions.
uint8_t snd_nxt[4]
The sequence number that was last sent by us.
Definition: uip.h:1362
uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip.c:1948
uip_ipaddr_t ripaddr
The IP address of the remote host.
Definition: uip.h:1354
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip.c:136
#define UIP_STAT(s)
The uIP TCP/IP statistics.
Definition: uip.h:1448
uint8_t ttl
Default time-to-live.
Definition: uip.h:1409
#define UIP_MAXSYNRTX
The maximum number of times a SYN segment should be retransmitted before a connection request should ...
Definition: uipopt.h:471
struct uip_udp_conn * uip_udp_new(const uip_ipaddr_t *ripaddr, uint16_t rport)
Set up a new UDP connection.
Definition: uip.c:462
#define NULL
The null pointer.
802.3 address
Definition: uip.h:129
uint16_t rport
The local remote TCP port, in network byte order.
Definition: uip.h:1357
uint8_t nrtx
The number of retransmissions for the last segment sent.
Definition: uip.h:1371
uint16_t lport
The local TCP port, in network byte order.
Definition: uip.h:1356
uint16_t uip_tcpchksum(void)
Calculate the TCP checksum of the packet in uip_buf and uip_appdata.
Definition: uip.c:348
uint8_t rto
Retransmission time-out.
Definition: uip.h:1368
#define UIP_APPCALL
The name of the application function that uIP should call in response to TCP/IP events.
Definition: smtp.h:53
#define UIP_UDP_CONNS
The maximum amount of concurrent UDP connections.
Definition: uipopt.h:365
Header file for the uIP TCP/IP stack.
uint16_t rport
The remote port number in network byte order.
Definition: uip.h:1408
uint8_t timer
The retransmission timer.
Definition: uip.h:1370
#define UIP_RECEIVE_WINDOW
The size of the advertised receiver's window.
Definition: uipopt.h:498
struct uip_conn * uip_connect(const uip_ipaddr_t *ripaddr, uint16_t port)
Connect to a remote host using TCP.
uint16_t uip_udpchksum(void)
Calculate the UDP checksum of the packet in uip_buf and uip_appdata.
void uip_unlisten(uint16_t port)
Stop listening to the specified port.
Definition: uip.c:507
uint16_t len
Length of the data that was previously sent.
Definition: uip.h:1363
void uip_send(const void *data, int len)
Send data on the current connection.
Definition: uip.c:1960
uint16_t lport
The local port number in network byte order.
Definition: uip.h:1407
#define UIP_TIME_WAIT_TIMEOUT
How long a connection should stay in the TIME_WAIT state.
Definition: uipopt.h:509
uint8_t sv
Retransmission time-out calculation state variable.
Definition: uip.h:1367
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118
#define UIP_TCP_MSS
The TCP maximum segment size.
Definition: uipopt.h:485
uint16_t mss
Current maximum segment size for the connection.
Definition: uip.h:1364
#define UIP_LISTENPORTS
The maximum number of simultaneously listening TCP ports.
Definition: uipopt.h:433
The uIP packet buffer.
Definition: uip.h:515
Header file for database of link-local neighbors, used by IPv6 code and to be used by future ...
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition: uipopt.h:173
CCIF uip_buf_t uip_aligned_buf
Packet buffer for incoming and outgoing packets.
Definition: uip.c:122
Macros and definitions for the ARP module.
void uip_init(void)
uIP initialization function.
Definition: uip.c:363
#define UIP_RTO
The initial retransmission timeout counted in timer pulses.
Definition: uipopt.h:454
void uip_process(uint8_t flag)
process the options within a hop by hop or destination option header
Definition: uip.c:671