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