Contiki 2.5
uip6.c
Go to the documentation of this file.
1 /**
2  * \addtogroup uip6
3  * @{
4  */
5 
6 /**
7  * \file
8  * The uIP TCP/IPv6 stack code.
9  *
10  * \author Adam Dunkels <adam@sics.se>
11  * \author Julien Abeille <jabeille@cisco.com> (IPv6 related code)
12  * \author Mathilde Durvy <mdurvy@cisco.com> (IPv6 related code)
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  * $Id: uip6.c,v 1.25 2011/01/04 22:11:37 joxe Exp $
45  *
46  */
47 
48 /*
49  * uIP is a small implementation of the IP, UDP and TCP protocols (as
50  * well as some basic ICMP stuff). The implementation couples the IP,
51  * UDP, TCP and the application layers very tightly. To keep the size
52  * of the compiled code down, this code frequently uses the goto
53  * statement. While it would be possible to break the uip_process()
54  * function into many smaller functions, this would increase the code
55  * size because of the overhead of parameter passing and the fact that
56  * the optimier would not be as efficient.
57  *
58  * The principle is that we have a small buffer, called the uip_buf,
59  * in which the device driver puts an incoming packet. The TCP/IP
60  * stack parses the headers in the packet, and calls the
61  * application. If the remote host has sent data to the application,
62  * this data is present in the uip_buf and the application read the
63  * data from there. It is up to the application to put this data into
64  * a byte stream if needed. The application will not be fed with data
65  * that is out of sequence.
66  *
67  * If the application whishes to send data to the peer, it should put
68  * its data into the uip_buf. The uip_appdata pointer points to the
69  * first available byte. The TCP/IP stack will calculate the
70  * checksums, and fill in the necessary header fields and finally send
71  * the packet back to the peer.
72  */
73 
74 #include "net/uip.h"
75 #include "net/uipopt.h"
76 #include "net/uip-icmp6.h"
77 #include "net/uip-nd6.h"
78 #include "net/uip-ds6.h"
79 
80 #include <string.h>
81 
82 /*---------------------------------------------------------------------------*/
83 /* For Debug, logging, statistics */
84 /*---------------------------------------------------------------------------*/
85 
86 #define DEBUG 0
87 #if DEBUG
88 #include <stdio.h>
89 #define PRINTF(...) printf(__VA_ARGS__)
90 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
91 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",lladdr->addr[0], lladdr->addr[1], lladdr->addr[2], lladdr->addr[3],lladdr->addr[4], lladdr->addr[5])
92 #else
93 #define PRINTF(...)
94 #define PRINT6ADDR(addr)
95 #endif
96 
97 #if UIP_CONF_IPV6_RPL
98 void uip_rpl_input(void);
99 #endif /* UIP_CONF_IPV6_RPL */
100 
101 #if UIP_LOGGING == 1
102 #include <stdio.h>
103 void uip_log(char *msg);
104 #define UIP_LOG(m) uip_log(m)
105 #else
106 #define UIP_LOG(m)
107 #endif /* UIP_LOGGING == 1 */
108 
109 #if UIP_STATISTICS == 1
110 struct uip_stats uip_stat;
111 #endif /* UIP_STATISTICS == 1 */
112 
113 
114 /*---------------------------------------------------------------------------*/
115 /** @{ \name Layer 2 variables */
116 /*---------------------------------------------------------------------------*/
117 /** Host L2 address */
118 #if UIP_CONF_LL_802154
120 #else /*UIP_CONF_LL_802154*/
121 uip_lladdr_t uip_lladdr = {{0x00,0x06,0x98,0x00,0x02,0x32}};
122 #endif /*UIP_CONF_LL_802154*/
123 /** @} */
124 
125 /*---------------------------------------------------------------------------*/
126 /** @{ \name Layer 3 variables */
127 /*---------------------------------------------------------------------------*/
128 /**
129  * \brief Type of the next header in IPv6 header or extension headers
130  *
131  * Can be the next header field in the IPv6 header or in an extension header.
132  * When doing fragment reassembly, we must change the value of the next header
133  * field in the header before the fragmentation header, hence we need a pointer
134  * to this field.
135  */
137 /** \brief bitmap we use to record which IPv6 headers we have already seen */
138 u8_t uip_ext_bitmap = 0;
139 /**
140  * \brief length of the extension headers read. updated each time we process
141  * a header
142  */
143 u8_t uip_ext_len = 0;
144 /** \brief length of the header options read */
146 /** @} */
147 
148 /*---------------------------------------------------------------------------*/
149 /* Buffers */
150 /*---------------------------------------------------------------------------*/
151 /** \name Buffer defines
152  * @{
153  */
154 #define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0])
155 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
156 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
157 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
158 #define UIP_TCP_BUF ((struct uip_tcp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
159 #define UIP_EXT_BUF ((struct uip_ext_hdr *)&uip_buf[uip_l2_l3_hdr_len])
160 #define UIP_ROUTING_BUF ((struct uip_routing_hdr *)&uip_buf[uip_l2_l3_hdr_len])
161 #define UIP_FRAG_BUF ((struct uip_frag_hdr *)&uip_buf[uip_l2_l3_hdr_len])
162 #define UIP_HBHO_BUF ((struct uip_hbho_hdr *)&uip_buf[uip_l2_l3_hdr_len])
163 #define UIP_DESTO_BUF ((struct uip_desto_hdr *)&uip_buf[uip_l2_l3_hdr_len])
164 #define UIP_EXT_HDR_OPT_BUF ((struct uip_ext_hdr_opt *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset])
165 #define UIP_EXT_HDR_OPT_PADN_BUF ((struct uip_ext_hdr_opt_padn *)&uip_buf[uip_l2_l3_hdr_len + uip_ext_opt_offset])
166 #define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len])
167 /** @} */
168 /** \name Buffer variables
169  * @{
170  */
171 /** Packet buffer for incoming and outgoing packets */
172 #ifndef UIP_CONF_EXTERNAL_BUFFER
173 uip_buf_t uip_aligned_buf;
174 #endif /* UIP_CONF_EXTERNAL_BUFFER */
175 
176 /* The uip_appdata pointer points to application data. */
178 /* The uip_appdata pointer points to the application data which is to be sent*/
179 void *uip_sappdata;
180 
181 #if UIP_URGDATA > 0
182 /* The uip_urgdata pointer points to urgent data (out-of-band data), if present */
183 void *uip_urgdata;
184 u16_t uip_urglen, uip_surglen;
185 #endif /* UIP_URGDATA > 0 */
186 
187 /* The uip_len is either 8 or 16 bits, depending on the maximum packet size.*/
188 u16_t uip_len, uip_slen;
189 /** @} */
190 
191 /*---------------------------------------------------------------------------*/
192 /** @{ \name General variables */
193 /*---------------------------------------------------------------------------*/
194 
195 /* The uip_flags variable is used for communication between the TCP/IP stack
196 and the application program. */
197 u8_t uip_flags;
198 
199 /* uip_conn always points to the current connection (set to NULL for UDP). */
201 
202 /* Temporary variables. */
203 #if (UIP_TCP || UIP_UDP)
204 static u8_t c;
205 #endif
206 
207 #if UIP_ACTIVE_OPEN || UIP_UDP
208 /* Keeps track of the last port used for a new connection. */
209 static u16_t lastport;
210 #endif /* UIP_ACTIVE_OPEN || UIP_UDP */
211 /** @} */
212 
213 /*---------------------------------------------------------------------------*/
214 /* TCP */
215 /*---------------------------------------------------------------------------*/
216 /** \name TCP defines
217  *@{
218  */
219 /* Structures and definitions. */
220 #define TCP_FIN 0x01
221 #define TCP_SYN 0x02
222 #define TCP_RST 0x04
223 #define TCP_PSH 0x08
224 #define TCP_ACK 0x10
225 #define TCP_URG 0x20
226 #define TCP_CTL 0x3f
227 
228 #define TCP_OPT_END 0 /* End of TCP options list */
229 #define TCP_OPT_NOOP 1 /* "No-operation" TCP option */
230 #define TCP_OPT_MSS 2 /* Maximum segment size TCP option */
231 
232 #define TCP_OPT_MSS_LEN 4 /* Length of TCP MSS option. */
233 /** @} */
234 /** \name TCP variables
235  *@{
236  */
237 #if UIP_TCP
238 /* The uip_conns array holds all TCP connections. */
239 struct uip_conn uip_conns[UIP_CONNS];
240 
241 /* The uip_listenports list all currently listning ports. */
242 u16_t uip_listenports[UIP_LISTENPORTS];
243 
244 /* The iss variable is used for the TCP initial sequence number. */
245 static u8_t iss[4];
246 
247 /* Temporary variables. */
248 u8_t uip_acc32[4];
249 static u8_t opt;
250 static u16_t tmp16;
251 #endif /* UIP_TCP */
252 /** @} */
253 
254 /*---------------------------------------------------------------------------*/
255 /** @{ \name UDP variables */
256 /*---------------------------------------------------------------------------*/
257 #if UIP_UDP
259 struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
260 #endif /* UIP_UDP */
261 /** @} */
262 
263 /*---------------------------------------------------------------------------*/
264 /** @{ \name ICMPv6 variables */
265 /*---------------------------------------------------------------------------*/
266 #if UIP_CONF_ICMP6
267 /** single possible icmpv6 "connection" */
268 struct uip_icmp6_conn uip_icmp6_conns;
269 #endif /*UIP_CONF_ICMP6*/
270 
271 /*---------------------------------------------------------------------------*/
272 /* Functions */
273 /*---------------------------------------------------------------------------*/
274 #if (!UIP_ARCH_ADD32 && UIP_TCP)
275 void
276 uip_add32(u8_t *op32, u16_t op16)
277 {
278  uip_acc32[3] = op32[3] + (op16 & 0xff);
279  uip_acc32[2] = op32[2] + (op16 >> 8);
280  uip_acc32[1] = op32[1];
281  uip_acc32[0] = op32[0];
282 
283  if(uip_acc32[2] < (op16 >> 8)) {
284  ++uip_acc32[1];
285  if(uip_acc32[1] == 0) {
286  ++uip_acc32[0];
287  }
288  }
289 
290 
291  if(uip_acc32[3] < (op16 & 0xff)) {
292  ++uip_acc32[2];
293  if(uip_acc32[2] == 0) {
294  ++uip_acc32[1];
295  if(uip_acc32[1] == 0) {
296  ++uip_acc32[0];
297  }
298  }
299  }
300 }
301 
302 #endif /* UIP_ARCH_ADD32 && UIP_TCP */
303 
304 #if ! UIP_ARCH_CHKSUM
305 /*---------------------------------------------------------------------------*/
306 static u16_t
307 chksum(u16_t sum, const u8_t *data, u16_t len)
308 {
309  u16_t t;
310  const u8_t *dataptr;
311  const u8_t *last_byte;
312 
313  dataptr = data;
314  last_byte = data + len - 1;
315 
316  while(dataptr < last_byte) { /* At least two more bytes */
317  t = (dataptr[0] << 8) + dataptr[1];
318  sum += t;
319  if(sum < t) {
320  sum++; /* carry */
321  }
322  dataptr += 2;
323  }
324 
325  if(dataptr == last_byte) {
326  t = (dataptr[0] << 8) + 0;
327  sum += t;
328  if(sum < t) {
329  sum++; /* carry */
330  }
331  }
332 
333  /* Return sum in host byte order. */
334  return sum;
335 }
336 /*---------------------------------------------------------------------------*/
337 u16_t
338 uip_chksum(u16_t *data, u16_t len)
339 {
340  return uip_htons(chksum(0, (u8_t *)data, len));
341 }
342 /*---------------------------------------------------------------------------*/
343 #ifndef UIP_ARCH_IPCHKSUM
344 u16_t
346 {
347  u16_t sum;
348 
349  sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
350  PRINTF("uip_ipchksum: sum 0x%04x\n", sum);
351  return (sum == 0) ? 0xffff : uip_htons(sum);
352 }
353 #endif
354 /*---------------------------------------------------------------------------*/
355 static u16_t
356 upper_layer_chksum(u8_t proto)
357 {
358 /* gcc 4.4.0 - 4.6.1 (maybe 4.3...) with -Os on 8 bit CPUS incorrectly compiles:
359  * int bar (int);
360  * int foo (unsigned char a, unsigned char b) {
361  * int len = (a << 8) + b; //len becomes 0xff00&<random>+b
362  * return len + bar (len);
363  * }
364  * upper_layer_len triggers this bug unless it is declared volatile.
365  * See https://sourceforge.net/apps/mantisbt/contiki/view.php?id=3
366  */
367  volatile u16_t upper_layer_len;
368  u16_t sum;
369 
370  upper_layer_len = (((u16_t)(UIP_IP_BUF->len[0]) << 8) + UIP_IP_BUF->len[1] - uip_ext_len) ;
371 
372  /* First sum pseudoheader. */
373  /* IP protocol and length fields. This addition cannot carry. */
374  sum = upper_layer_len + proto;
375  /* Sum IP source and destination addresses. */
376  sum = chksum(sum, (u8_t *)&UIP_IP_BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t));
377 
378  /* Sum TCP header and data. */
379  sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN + uip_ext_len],
380  upper_layer_len);
381 
382  return (sum == 0) ? 0xffff : uip_htons(sum);
383 }
384 /*---------------------------------------------------------------------------*/
385 u16_t
387 {
388  return upper_layer_chksum(UIP_PROTO_ICMP6);
389 
390 }
391 /*---------------------------------------------------------------------------*/
392 #if UIP_TCP
393 u16_t
394 uip_tcpchksum(void)
395 {
396  return upper_layer_chksum(UIP_PROTO_TCP);
397 }
398 #endif /* UIP_TCP */
399 /*---------------------------------------------------------------------------*/
400 #if UIP_UDP && UIP_UDP_CHECKSUMS
401 u16_t
402 uip_udpchksum(void)
403 {
404  return upper_layer_chksum(UIP_PROTO_UDP);
405 }
406 #endif /* UIP_UDP && UIP_UDP_CHECKSUMS */
407 #endif /* UIP_ARCH_CHKSUM */
408 /*---------------------------------------------------------------------------*/
409 void
410 uip_init(void)
411 {
412 
413  uip_ds6_init();
414 
415 #if UIP_TCP
416  for(c = 0; c < UIP_LISTENPORTS; ++c) {
417  uip_listenports[c] = 0;
418  }
419  for(c = 0; c < UIP_CONNS; ++c) {
420  uip_conns[c].tcpstateflags = UIP_CLOSED;
421  }
422 #endif /* UIP_TCP */
423 
424 #if UIP_ACTIVE_OPEN || UIP_UDP
425  lastport = 1024;
426 #endif /* UIP_ACTIVE_OPEN || UIP_UDP */
427 
428 #if UIP_UDP
429  for(c = 0; c < UIP_UDP_CONNS; ++c) {
430  uip_udp_conns[c].lport = 0;
431  }
432 #endif /* UIP_UDP */
433 }
434 /*---------------------------------------------------------------------------*/
435 #if UIP_TCP && UIP_ACTIVE_OPEN
436 struct uip_conn *
438 {
439  register struct uip_conn *conn, *cconn;
440 
441  /* Find an unused local port. */
442  again:
443  ++lastport;
444 
445  if(lastport >= 32000) {
446  lastport = 4096;
447  }
448 
449  /* Check if this port is already in use, and if so try to find
450  another one. */
451  for(c = 0; c < UIP_CONNS; ++c) {
452  conn = &uip_conns[c];
453  if(conn->tcpstateflags != UIP_CLOSED &&
454  conn->lport == uip_htons(lastport)) {
455  goto again;
456  }
457  }
458 
459  conn = 0;
460  for(c = 0; c < UIP_CONNS; ++c) {
461  cconn = &uip_conns[c];
462  if(cconn->tcpstateflags == UIP_CLOSED) {
463  conn = cconn;
464  break;
465  }
466  if(cconn->tcpstateflags == UIP_TIME_WAIT) {
467  if(conn == 0 ||
468  cconn->timer > conn->timer) {
469  conn = cconn;
470  }
471  }
472  }
473 
474  if(conn == 0) {
475  return 0;
476  }
477 
478  conn->tcpstateflags = UIP_SYN_SENT;
479 
480  conn->snd_nxt[0] = iss[0];
481  conn->snd_nxt[1] = iss[1];
482  conn->snd_nxt[2] = iss[2];
483  conn->snd_nxt[3] = iss[3];
484 
485  conn->rcv_nxt[0] = 0;
486  conn->rcv_nxt[1] = 0;
487  conn->rcv_nxt[2] = 0;
488  conn->rcv_nxt[3] = 0;
489 
490  conn->initialmss = conn->mss = UIP_TCP_MSS;
491 
492  conn->len = 1; /* TCP length of the SYN is one. */
493  conn->nrtx = 0;
494  conn->timer = 1; /* Send the SYN next time around. */
495  conn->rto = UIP_RTO;
496  conn->sa = 0;
497  conn->sv = 16; /* Initial value of the RTT variance. */
498  conn->lport = uip_htons(lastport);
499  conn->rport = rport;
500  uip_ipaddr_copy(&conn->ripaddr, ripaddr);
501 
502  return conn;
503 }
504 #endif /* UIP_TCP && UIP_ACTIVE_OPEN */
505 /*---------------------------------------------------------------------------*/
506 #if UIP_UDP
507 struct uip_udp_conn *
508 uip_udp_new(const uip_ipaddr_t *ripaddr, u16_t rport)
509 {
510  register struct uip_udp_conn *conn;
511 
512  /* Find an unused local port. */
513  again:
514  ++lastport;
515 
516  if(lastport >= 32000) {
517  lastport = 4096;
518  }
519 
520  for(c = 0; c < UIP_UDP_CONNS; ++c) {
521  if(uip_udp_conns[c].lport == uip_htons(lastport)) {
522  goto again;
523  }
524  }
525 
526  conn = 0;
527  for(c = 0; c < UIP_UDP_CONNS; ++c) {
528  if(uip_udp_conns[c].lport == 0) {
529  conn = &uip_udp_conns[c];
530  break;
531  }
532  }
533 
534  if(conn == 0) {
535  return 0;
536  }
537 
538  conn->lport = UIP_HTONS(lastport);
539  conn->rport = rport;
540  if(ripaddr == NULL) {
541  memset(&conn->ripaddr, 0, sizeof(uip_ipaddr_t));
542  } else {
543  uip_ipaddr_copy(&conn->ripaddr, ripaddr);
544  }
545  conn->ttl = uip_ds6_if.cur_hop_limit;
546 
547  return conn;
548 }
549 #endif /* UIP_UDP */
550 /*---------------------------------------------------------------------------*/
551 #if UIP_TCP
552 void
553 uip_unlisten(u16_t port)
554 {
555  for(c = 0; c < UIP_LISTENPORTS; ++c) {
556  if(uip_listenports[c] == port) {
557  uip_listenports[c] = 0;
558  return;
559  }
560  }
561 }
562 /*---------------------------------------------------------------------------*/
563 void
564 uip_listen(u16_t port)
565 {
566  for(c = 0; c < UIP_LISTENPORTS; ++c) {
567  if(uip_listenports[c] == 0) {
568  uip_listenports[c] = port;
569  return;
570  }
571  }
572 }
573 #endif
574 /*---------------------------------------------------------------------------*/
575 
576 #if UIP_CONF_IPV6_REASSEMBLY
577 #define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
578 
579 static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];
580 
581 static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
582 /*the first byte of an IP fragment is aligned on an 8-byte boundary */
583 
584 static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
585  0x0f, 0x07, 0x03, 0x01};
586 static u16_t uip_reasslen;
587 static u8_t uip_reassflags;
588 
589 #define UIP_REASS_FLAG_LASTFRAG 0x01
590 #define UIP_REASS_FLAG_FIRSTFRAG 0x02
591 #define UIP_REASS_FLAG_ERROR_MSG 0x04
592 
593 
594 /*
595  * See RFC 2460 for a description of fragmentation in IPv6
596  * A typical Ipv6 fragment
597  * +------------------+--------+--------------+
598  * | Unfragmentable |Fragment| first |
599  * | Part | Header | fragment |
600  * +------------------+--------+--------------+
601  */
602 
603 
604 struct etimer uip_reass_timer; /* timer for reassembly */
605 u8_t uip_reass_on; /* equal to 1 if we are currently reassembling a packet */
606 
607 static u32_t uip_id; /* For every packet that is to be fragmented, the source
608  node generates an Identification value that is present
609  in all the fragments */
610 #define IP_MF 0x0001
611 
612 static u16_t
613 uip_reass(void)
614 {
615  u16_t offset=0;
616  u16_t len;
617  u16_t i;
618 
619  /* If ip_reasstmr is zero, no packet is present in the buffer */
620  /* We first write the unfragmentable part of IP header into the reassembly
621  buffer. The reset the other reassembly variables. */
622  if(uip_reass_on == 0) {
623  PRINTF("Starting reassembly\n");
624  memcpy(FBUF, UIP_IP_BUF, uip_ext_len + UIP_IPH_LEN);
625  /* temporary in case we do not receive the fragment with offset 0 first */
626  etimer_set(&uip_reass_timer, UIP_REASS_MAXAGE*CLOCK_SECOND);
627  uip_reass_on = 1;
628  uip_reassflags = 0;
629  uip_id = UIP_FRAG_BUF->id;
630  /* Clear the bitmap. */
631  memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap));
632  }
633  /*
634  * Check if the incoming fragment matches the one currently present
635  * in the reasembly buffer. If so, we proceed with copying the fragment
636  * into the buffer.
637  */
638  if(uip_ipaddr_cmp(&FBUF->srcipaddr, &UIP_IP_BUF->srcipaddr) &&
639  uip_ipaddr_cmp(&FBUF->destipaddr, &UIP_IP_BUF->destipaddr) &&
640  UIP_FRAG_BUF->id == uip_id) {
641  len = uip_len - uip_ext_len - UIP_IPH_LEN - UIP_FRAGH_LEN;
642  offset = (uip_ntohs(UIP_FRAG_BUF->offsetresmore) & 0xfff8);
643  /* in byte, originaly in multiple of 8 bytes*/
644  PRINTF("len %d\n", len);
645  PRINTF("offset %d\n", offset);
646  if(offset == 0){
647  uip_reassflags |= UIP_REASS_FLAG_FIRSTFRAG;
648  /*
649  * The Next Header field of the last header of the Unfragmentable
650  * Part is obtained from the Next Header field of the first
651  * fragment's Fragment header.
652  */
653  *uip_next_hdr = UIP_FRAG_BUF->next;
654  memcpy(FBUF, UIP_IP_BUF, uip_ext_len + UIP_IPH_LEN);
655  PRINTF("src ");
656  PRINT6ADDR(&FBUF->srcipaddr);
657  PRINTF("dest ");
658  PRINT6ADDR(&FBUF->destipaddr);
659  PRINTF("next %d\n", UIP_IP_BUF->proto);
660 
661  }
662 
663  /* If the offset or the offset + fragment length overflows the
664  reassembly buffer, we discard the entire packet. */
665  if(offset > UIP_REASS_BUFSIZE ||
666  offset + len > UIP_REASS_BUFSIZE) {
667  uip_reass_on = 0;
668  etimer_stop(&uip_reass_timer);
669  return 0;
670  }
671 
672  /* If this fragment has the More Fragments flag set to zero, it is the
673  last fragment*/
674  if((uip_ntohs(UIP_FRAG_BUF->offsetresmore) & IP_MF) == 0) {
675  uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
676  /*calculate the size of the entire packet*/
677  uip_reasslen = offset + len;
678  PRINTF("LAST FRAGMENT reasslen %d\n", uip_reasslen);
679  } else {
680  /* If len is not a multiple of 8 octets and the M flag of that fragment
681  is 1, then that fragment must be discarded and an ICMP Parameter
682  Problem, Code 0, message should be sent to the source of the fragment,
683  pointing to the Payload Length field of the fragment packet. */
684  if(len % 8 != 0){
686  uip_reassflags |= UIP_REASS_FLAG_ERROR_MSG;
687  /* not clear if we should interrupt reassembly, but it seems so from
688  the conformance tests */
689  uip_reass_on = 0;
690  etimer_stop(&uip_reass_timer);
691  return uip_len;
692  }
693  }
694 
695  /* Copy the fragment into the reassembly buffer, at the right
696  offset. */
697  memcpy((uint8_t *)FBUF + UIP_IPH_LEN + uip_ext_len + offset,
698  (uint8_t *)UIP_FRAG_BUF + UIP_FRAGH_LEN, len);
699 
700  /* Update the bitmap. */
701  if(offset >> 6 == (offset + len) >> 6) {
702  uip_reassbitmap[offset >> 6] |=
703  bitmap_bits[(offset >> 3) & 7] &
704  ~bitmap_bits[((offset + len) >> 3) & 7];
705  } else {
706  /* If the two endpoints are in different bytes, we update the
707  bytes in the endpoints and fill the stuff inbetween with
708  0xff. */
709  uip_reassbitmap[offset >> 6] |= bitmap_bits[(offset >> 3) & 7];
710 
711  for(i = (1 + (offset >> 6)); i < ((offset + len) >> 6); ++i) {
712  uip_reassbitmap[i] = 0xff;
713  }
714  uip_reassbitmap[(offset + len) >> 6] |=
715  ~bitmap_bits[((offset + len) >> 3) & 7];
716  }
717 
718  /* Finally, we check if we have a full packet in the buffer. We do
719  this by checking if we have the last fragment and if all bits
720  in the bitmap are set. */
721 
722  if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
723  /* Check all bytes up to and including all but the last byte in
724  the bitmap. */
725  for(i = 0; i < (uip_reasslen >> 6); ++i) {
726  if(uip_reassbitmap[i] != 0xff) {
727  return 0;
728  }
729  }
730  /* Check the last byte in the bitmap. It should contain just the
731  right amount of bits. */
732  if(uip_reassbitmap[uip_reasslen >> 6] !=
733  (u8_t)~bitmap_bits[(uip_reasslen >> 3) & 7]) {
734  return 0;
735  }
736 
737  /* If we have come this far, we have a full packet in the
738  buffer, so we copy it to uip_buf. We also reset the timer. */
739  uip_reass_on = 0;
740  etimer_stop(&uip_reass_timer);
741 
742  uip_reasslen += UIP_IPH_LEN + uip_ext_len;
743  memcpy(UIP_IP_BUF, FBUF, uip_reasslen);
744  UIP_IP_BUF->len[0] = ((uip_reasslen - UIP_IPH_LEN) >> 8);
745  UIP_IP_BUF->len[1] = ((uip_reasslen - UIP_IPH_LEN) & 0xff);
746  PRINTF("REASSEMBLED PAQUET %d (%d)\n", uip_reasslen,
747  (UIP_IP_BUF->len[0] << 8) | UIP_IP_BUF->len[1]);
748 
749  return uip_reasslen;
750 
751  }
752  } else {
753  PRINTF("Already reassembling another paquet\n");
754  }
755  return 0;
756 }
757 
758 void
759 uip_reass_over(void)
760 {
761  /* to late, we abandon the reassembly of the packet */
762 
763  uip_reass_on = 0;
764  etimer_stop(&uip_reass_timer);
765 
766  if(uip_reassflags & UIP_REASS_FLAG_FIRSTFRAG){
767  PRINTF("FRAG INTERRUPTED TOO LATE\n");
768  /* If the first fragment has been received, an ICMP Time Exceeded
769  -- Fragment Reassembly Time Exceeded message should be sent to the
770  source of that fragment. */
771  /** \note
772  * We don't have a complete packet to put in the error message.
773  * We could include the first fragment but since its not mandated by
774  * any RFC, we decided not to include it as it reduces the size of
775  * the packet.
776  */
777  uip_len = 0;
778  uip_ext_len = 0;
779  memcpy(UIP_IP_BUF, FBUF, UIP_IPH_LEN); /* copy the header for src
780  and dest address*/
782 
783  UIP_STAT(++uip_stat.ip.sent);
784  uip_flags = 0;
785  }
786 }
787 
788 #endif /* UIP_CONF_IPV6_REASSEMBLY */
789 
790 /*---------------------------------------------------------------------------*/
791 #if UIP_TCP
792 static void
793 uip_add_rcv_nxt(u16_t n)
794 {
795  uip_add32(uip_conn->rcv_nxt, n);
796  uip_conn->rcv_nxt[0] = uip_acc32[0];
797  uip_conn->rcv_nxt[1] = uip_acc32[1];
798  uip_conn->rcv_nxt[2] = uip_acc32[2];
799  uip_conn->rcv_nxt[3] = uip_acc32[3];
800 }
801 #endif
802 /*---------------------------------------------------------------------------*/
803 
804 /**
805  * \brief Process the options in Destination and Hop By Hop extension headers
806  */
807 static u8_t
808 ext_hdr_options_process(void)
809 {
810  /*
811  * Length field in the extension header: length of the header in units of
812  * 8 bytes, excluding the first 8 bytes
813  * length field in an option : the length of data in the option
814  */
815  uip_ext_opt_offset = 2;
816  while(uip_ext_opt_offset < ((UIP_EXT_BUF->len << 3) + 8)) {
817  switch(UIP_EXT_HDR_OPT_BUF->type) {
818  /*
819  * for now we do not support any options except padding ones
820  * PAD1 does not make sense as the header must be 8bytes aligned,
821  * hence we can only have
822  */
823  case UIP_EXT_HDR_OPT_PAD1:
824  PRINTF("Processing PAD1 option\n");
825  uip_ext_opt_offset += 1;
826  break;
827  case UIP_EXT_HDR_OPT_PADN:
828  PRINTF("Processing PADN option\n");
829  uip_ext_opt_offset += UIP_EXT_HDR_OPT_PADN_BUF->opt_len + 2;
830  break;
831  default:
832  /*
833  * check the two highest order bits of the option
834  * - 00 skip over this option and continue processing the header.
835  * - 01 discard the packet.
836  * - 10 discard the packet and, regardless of whether or not the
837  * packet's Destination Address was a multicast address, send an
838  * ICMP Parameter Problem, Code 2, message to the packet's
839  * Source Address, pointing to the unrecognized Option Type.
840  * - 11 discard the packet and, only if the packet's Destination
841  * Address was not a multicast address, send an ICMP Parameter
842  * Problem, Code 2, message to the packet's Source Address,
843  * pointing to the unrecognized Option Type.
844  */
845  PRINTF("MSB %x\n", UIP_EXT_HDR_OPT_BUF->type);
846  switch(UIP_EXT_HDR_OPT_BUF->type & 0xC0) {
847  case 0:
848  break;
849  case 0x40:
850  return 1;
851  case 0xC0:
852  if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
853  return 1;
854  }
855  case 0x80:
857  (u32_t)UIP_IPH_LEN + uip_ext_len + uip_ext_opt_offset);
858  return 2;
859  }
860  /* in the cases were we did not discard, update ext_opt* */
861  uip_ext_opt_offset += UIP_EXT_HDR_OPT_BUF->len + 2;
862  break;
863  }
864  }
865  return 0;
866 }
867 
868 
869 /*---------------------------------------------------------------------------*/
870 void
871 uip_process(u8_t flag)
872 {
873 #if UIP_TCP
874  register struct uip_conn *uip_connr = uip_conn;
875 #endif /* UIP_TCP */
876 #if UIP_UDP
877  if(flag == UIP_UDP_SEND_CONN) {
878  goto udp_send;
879  }
880 #endif /* UIP_UDP */
881  uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];
882 
883  /* Check if we were invoked because of a poll request for a
884  particular connection. */
885  if(flag == UIP_POLL_REQUEST) {
886 #if UIP_TCP
887  if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED &&
888  !uip_outstanding(uip_connr)) {
889  uip_flags = UIP_POLL;
890  UIP_APPCALL();
891  goto appsend;
892 #if UIP_ACTIVE_OPEN
893  } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) {
894  /* In the SYN_SENT state, we retransmit out SYN. */
895  UIP_TCP_BUF->flags = 0;
896  goto tcp_send_syn;
897 #endif /* UIP_ACTIVE_OPEN */
898  }
899  goto drop;
900 #endif /* UIP_TCP */
901  /* Check if we were invoked because of the perodic timer fireing. */
902  } else if(flag == UIP_TIMER) {
903  /* Reset the length variables. */
904 #if UIP_TCP
905  uip_len = 0;
906  uip_slen = 0;
907 
908  /* Increase the initial sequence number. */
909  if(++iss[3] == 0) {
910  if(++iss[2] == 0) {
911  if(++iss[1] == 0) {
912  ++iss[0];
913  }
914  }
915  }
916 
917  /*
918  * Check if the connection is in a state in which we simply wait
919  * for the connection to time out. If so, we increase the
920  * connection's timer and remove the connection if it times
921  * out.
922  */
923  if(uip_connr->tcpstateflags == UIP_TIME_WAIT ||
924  uip_connr->tcpstateflags == UIP_FIN_WAIT_2) {
925  ++(uip_connr->timer);
926  if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
927  uip_connr->tcpstateflags = UIP_CLOSED;
928  }
929  } else if(uip_connr->tcpstateflags != UIP_CLOSED) {
930  /*
931  * If the connection has outstanding data, we increase the
932  * connection's timer and see if it has reached the RTO value
933  * in which case we retransmit.
934  */
935  if(uip_outstanding(uip_connr)) {
936  if(uip_connr->timer-- == 0) {
937  if(uip_connr->nrtx == UIP_MAXRTX ||
938  ((uip_connr->tcpstateflags == UIP_SYN_SENT ||
939  uip_connr->tcpstateflags == UIP_SYN_RCVD) &&
940  uip_connr->nrtx == UIP_MAXSYNRTX)) {
941  uip_connr->tcpstateflags = UIP_CLOSED;
942 
943  /*
944  * We call UIP_APPCALL() with uip_flags set to
945  * UIP_TIMEDOUT to inform the application that the
946  * connection has timed out.
947  */
948  uip_flags = UIP_TIMEDOUT;
949  UIP_APPCALL();
950 
951  /* We also send a reset packet to the remote host. */
952  UIP_TCP_BUF->flags = TCP_RST | TCP_ACK;
953  goto tcp_send_nodata;
954  }
955 
956  /* Exponential backoff. */
957  uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
958  4:
959  uip_connr->nrtx);
960  ++(uip_connr->nrtx);
961 
962  /*
963  * Ok, so we need to retransmit. We do this differently
964  * depending on which state we are in. In ESTABLISHED, we
965  * call upon the application so that it may prepare the
966  * data for the retransmit. In SYN_RCVD, we resend the
967  * SYNACK that we sent earlier and in LAST_ACK we have to
968  * retransmit our FINACK.
969  */
970  UIP_STAT(++uip_stat.tcp.rexmit);
971  switch(uip_connr->tcpstateflags & UIP_TS_MASK) {
972  case UIP_SYN_RCVD:
973  /* In the SYN_RCVD state, we should retransmit our SYNACK. */
974  goto tcp_send_synack;
975 
976 #if UIP_ACTIVE_OPEN
977  case UIP_SYN_SENT:
978  /* In the SYN_SENT state, we retransmit out SYN. */
979  UIP_TCP_BUF->flags = 0;
980  goto tcp_send_syn;
981 #endif /* UIP_ACTIVE_OPEN */
982 
983  case UIP_ESTABLISHED:
984  /*
985  * In the ESTABLISHED state, we call upon the application
986  * to do the actual retransmit after which we jump into
987  * the code for sending out the packet (the apprexmit
988  * label).
989  */
990  uip_flags = UIP_REXMIT;
991  UIP_APPCALL();
992  goto apprexmit;
993 
994  case UIP_FIN_WAIT_1:
995  case UIP_CLOSING:
996  case UIP_LAST_ACK:
997  /* In all these states we should retransmit a FINACK. */
998  goto tcp_send_finack;
999  }
1000  }
1001  } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) {
1002  /*
1003  * If there was no need for a retransmission, we poll the
1004  * application for new data.
1005  */
1006  uip_flags = UIP_POLL;
1007  UIP_APPCALL();
1008  goto appsend;
1009  }
1010  }
1011  goto drop;
1012 #endif /* UIP_TCP */
1013  }
1014 #if UIP_UDP
1015  if(flag == UIP_UDP_TIMER) {
1016  if(uip_udp_conn->lport != 0) {
1017  uip_conn = NULL;
1018  uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
1019  uip_len = uip_slen = 0;
1020  uip_flags = UIP_POLL;
1021  UIP_UDP_APPCALL();
1022  goto udp_send;
1023  } else {
1024  goto drop;
1025  }
1026  }
1027 #endif /* UIP_UDP */
1028 
1029 
1030  /* This is where the input processing starts. */
1031  UIP_STAT(++uip_stat.ip.recv);
1032 
1033  /* Start of IP input header processing code. */
1034 
1035  /* Check validity of the IP header. */
1036  if((UIP_IP_BUF->vtc & 0xf0) != 0x60) { /* IP version and header length. */
1037  UIP_STAT(++uip_stat.ip.drop);
1038  UIP_STAT(++uip_stat.ip.vhlerr);
1039  UIP_LOG("ipv6: invalid version.");
1040  goto drop;
1041  }
1042  /*
1043  * Check the size of the packet. If the size reported to us in
1044  * uip_len is smaller the size reported in the IP header, we assume
1045  * that the packet has been corrupted in transit. If the size of
1046  * uip_len is larger than the size reported in the IP packet header,
1047  * the packet has been padded and we set uip_len to the correct
1048  * value..
1049  */
1050 
1051  if((UIP_IP_BUF->len[0] << 8) + UIP_IP_BUF->len[1] <= uip_len) {
1052  uip_len = (UIP_IP_BUF->len[0] << 8) + UIP_IP_BUF->len[1] + UIP_IPH_LEN;
1053  /*
1054  * The length reported in the IPv6 header is the
1055  * length of the payload that follows the
1056  * header. However, uIP uses the uip_len variable
1057  * for holding the size of the entire packet,
1058  * including the IP header. For IPv4 this is not a
1059  * problem as the length field in the IPv4 header
1060  * contains the length of the entire packet. But
1061  * for IPv6 we need to add the size of the IPv6
1062  * header (40 bytes).
1063  */
1064  } else {
1065  UIP_LOG("ip: packet shorter than reported in IP header.");
1066  goto drop;
1067  }
1068 
1069  PRINTF("IPv6 packet received from ");
1070  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
1071  PRINTF(" to ");
1072  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
1073  PRINTF("\n");
1074 
1075  if(uip_is_addr_mcast(&UIP_IP_BUF->srcipaddr)){
1076  UIP_STAT(++uip_stat.ip.drop);
1077  PRINTF("Dropping packet, src is mcast\n");
1078  goto drop;
1079  }
1080 
1081 #if UIP_CONF_ROUTER
1082  /* TBD Some Parameter problem messages */
1083  if(!uip_ds6_is_my_addr(&UIP_IP_BUF->destipaddr) &&
1084  !uip_ds6_is_my_maddr(&UIP_IP_BUF->destipaddr)) {
1085  if(!uip_is_addr_mcast(&UIP_IP_BUF->destipaddr) &&
1086  !uip_is_addr_link_local(&UIP_IP_BUF->destipaddr) &&
1087  !uip_is_addr_link_local(&UIP_IP_BUF->srcipaddr) &&
1088  !uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr) &&
1089  !uip_is_addr_loopback(&UIP_IP_BUF->destipaddr)) {
1090 
1091 
1092  /* Check MTU */
1093  if(uip_len > UIP_LINK_MTU) {
1095  UIP_STAT(++uip_stat.ip.drop);
1096  goto send;
1097  }
1098  /* Check Hop Limit */
1099  if(UIP_IP_BUF->ttl <= 1) {
1102  UIP_STAT(++uip_stat.ip.drop);
1103  goto send;
1104  }
1105  UIP_IP_BUF->ttl = UIP_IP_BUF->ttl - 1;
1106  PRINTF("Forwarding packet to ");
1107  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
1108  PRINTF("\n");
1109  UIP_STAT(++uip_stat.ip.forwarded);
1110  goto send;
1111  } else {
1112  if((uip_is_addr_link_local(&UIP_IP_BUF->srcipaddr)) &&
1113  (!uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) &&
1114  (!uip_is_addr_loopback(&UIP_IP_BUF->destipaddr)) &&
1115  (!uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) &&
1116  (!uip_ds6_is_addr_onlink((&UIP_IP_BUF->destipaddr)))) {
1117  PRINTF("LL source address with off link destination, dropping\n");
1120  goto send;
1121  }
1122  PRINTF("Dropping packet, not for me and link local or multicast\n");
1123  UIP_STAT(++uip_stat.ip.drop);
1124  goto drop;
1125  }
1126  }
1127 #else /* UIP_CONF_ROUTER */
1128  if(!uip_ds6_is_my_addr(&UIP_IP_BUF->destipaddr) &&
1129  !uip_ds6_is_my_maddr(&UIP_IP_BUF->destipaddr) &&
1130  !uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
1131  PRINTF("Dropping packet, not for me\n");
1132  UIP_STAT(++uip_stat.ip.drop);
1133  goto drop;
1134  }
1135 #endif /* UIP_CONF_ROUTER */
1136 
1137  /*
1138  * Next header field processing. In IPv6, we can have extension headers,
1139  * they are processed here
1140  */
1141  uip_next_hdr = &UIP_IP_BUF->proto;
1142  uip_ext_len = 0;
1143  uip_ext_bitmap = 0;
1144  while(1) {
1145  switch(*uip_next_hdr){
1146 #if UIP_TCP
1147  case UIP_PROTO_TCP:
1148  /* TCP, for both IPv4 and IPv6 */
1149  goto tcp_input;
1150 #endif /* UIP_TCP */
1151 #if UIP_UDP
1152  case UIP_PROTO_UDP:
1153  /* UDP, for both IPv4 and IPv6 */
1154  goto udp_input;
1155 #endif /* UIP_UDP */
1156  case UIP_PROTO_ICMP6:
1157  /* ICMPv6 */
1158  goto icmp6_input;
1159  case UIP_PROTO_HBHO:
1160  PRINTF("Processing hbh header\n");
1161  /* Hop by hop option header */
1162 #if UIP_CONF_IPV6_CHECKS
1163  /* Hop by hop option header. If we saw one HBH already, drop */
1164  if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_HBHO) {
1165  goto bad_hdr;
1166  } else {
1167  uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_HBHO;
1168  }
1169 #endif /*UIP_CONF_IPV6_CHECKS*/
1170  switch(ext_hdr_options_process()) {
1171  case 0:
1172  /*continue*/
1173  uip_next_hdr = &UIP_EXT_BUF->next;
1174  uip_ext_len += (UIP_EXT_BUF->len << 3) + 8;
1175  break;
1176  case 1:
1177  /*silently discard*/
1178  goto drop;
1179  case 2:
1180  /* send icmp error message (created in ext_hdr_options_process)
1181  * and discard*/
1182  goto send;
1183  }
1184  break;
1185  case UIP_PROTO_DESTO:
1186 #if UIP_CONF_IPV6_CHECKS
1187  /* Destination option header. if we saw two already, drop */
1188  PRINTF("Processing desto header\n");
1189  if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_DESTO1) {
1190  if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_DESTO2) {
1191  goto bad_hdr;
1192  } else{
1193  uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO2;
1194  }
1195  } else {
1196  uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_DESTO1;
1197  }
1198 #endif /*UIP_CONF_IPV6_CHECKS*/
1199  switch(ext_hdr_options_process()) {
1200  case 0:
1201  /*continue*/
1202  uip_next_hdr = &UIP_EXT_BUF->next;
1203  uip_ext_len += (UIP_EXT_BUF->len << 3) + 8;
1204  break;
1205  case 1:
1206  /*silently discard*/
1207  goto drop;
1208  case 2:
1209  /* send icmp error message (created in ext_hdr_options_process)
1210  * and discard*/
1211  goto send;
1212  }
1213  break;
1214  case UIP_PROTO_ROUTING:
1215 #if UIP_CONF_IPV6_CHECKS
1216  /* Routing header. If we saw one already, drop */
1217  if(uip_ext_bitmap & UIP_EXT_HDR_BITMAP_ROUTING) {
1218  goto bad_hdr;
1219  } else {
1220  uip_ext_bitmap |= UIP_EXT_HDR_BITMAP_ROUTING;
1221  }
1222 #endif /*UIP_CONF_IPV6_CHECKS*/
1223  /*
1224  * Routing Header length field is in units of 8 bytes, excluding
1225  * As per RFC2460 section 4.4, if routing type is unrecognized:
1226  * if segments left = 0, ignore the header
1227  * if segments left > 0, discard packet and send icmp error pointing
1228  * to the routing type
1229  */
1230 
1231  PRINTF("Processing Routing header\n");
1232  if(UIP_ROUTING_BUF->seg_left > 0) {
1234  UIP_STAT(++uip_stat.ip.drop);
1235  UIP_LOG("ip6: unrecognized routing type");
1236  goto send;
1237  }
1238  uip_next_hdr = &UIP_EXT_BUF->next;
1239  uip_ext_len += (UIP_EXT_BUF->len << 3) + 8;
1240  break;
1241  case UIP_PROTO_FRAG:
1242  /* Fragmentation header:call the reassembly function, then leave */
1243 #if UIP_CONF_IPV6_REASSEMBLY
1244  PRINTF("Processing frag header\n");
1245  uip_len = uip_reass();
1246  if(uip_len == 0) {
1247  goto drop;
1248  }
1249  if(uip_reassflags & UIP_REASS_FLAG_ERROR_MSG){
1250  /* we are not done with reassembly, this is an error message */
1251  goto send;
1252  }
1253  /*packet is reassembled, reset the next hdr to the beginning
1254  of the IP header and restart the parsing of the reassembled pkt*/
1255  PRINTF("Processing reassembled packet\n");
1256  uip_ext_len = 0;
1257  uip_ext_bitmap = 0;
1258  uip_next_hdr = &UIP_IP_BUF->proto;
1259  break;
1260 #else /* UIP_CONF_IPV6_REASSEMBLY */
1261  UIP_STAT(++uip_stat.ip.drop);
1262  UIP_STAT(++uip_stat.ip.fragerr);
1263  UIP_LOG("ip: fragment dropped.");
1264  goto drop;
1265 #endif /* UIP_CONF_IPV6_REASSEMBLY */
1266  case UIP_PROTO_NONE:
1267  goto drop;
1268  default:
1269  goto bad_hdr;
1270  }
1271  }
1272  bad_hdr:
1273  /*
1274  * RFC 2460 send error message parameterr problem, code unrecognized
1275  * next header, pointing to the next header field
1276  */
1278  UIP_STAT(++uip_stat.ip.drop);
1279  UIP_STAT(++uip_stat.ip.protoerr);
1280  UIP_LOG("ip6: unrecognized header");
1281  goto send;
1282  /* End of headers processing */
1283 
1284  icmp6_input:
1285  /* This is IPv6 ICMPv6 processing code. */
1286  PRINTF("icmp6_input: length %d\n", uip_len);
1287 
1288 #if UIP_CONF_IPV6_CHECKS
1289  /* Compute and check the ICMP header checksum */
1290  if(uip_icmp6chksum() != 0xffff) {
1291  UIP_STAT(++uip_stat.icmp.drop);
1292  UIP_STAT(++uip_stat.icmp.chkerr);
1293  UIP_LOG("icmpv6: bad checksum.");
1294  goto drop;
1295  }
1296 #endif /*UIP_CONF_IPV6_CHECKS*/
1297 
1298  UIP_STAT(++uip_stat.icmp.recv);
1299  /*
1300  * Here we process incoming ICMPv6 packets
1301  * For echo request, we send echo reply
1302  * For ND pkts, we call the appropriate function in uip-nd6.c
1303  * We do not treat Error messages for now
1304  * If no pkt is to be sent as an answer to the incoming one, we
1305  * "goto drop". Else we just break; then at the after the "switch"
1306  * we "goto send"
1307  */
1308 #if UIP_CONF_ICMP6
1309  UIP_ICMP6_APPCALL(UIP_ICMP_BUF->type);
1310 #endif /*UIP_CONF_ICMP6*/
1311 
1312  switch(UIP_ICMP_BUF->type) {
1313  case ICMP6_NS:
1314  uip_nd6_ns_input();
1315  break;
1316  case ICMP6_NA:
1317  uip_nd6_na_input();
1318  break;
1319  case ICMP6_RS:
1320 #if UIP_CONF_ROUTER && UIP_ND6_SEND_RA
1321  uip_nd6_rs_input();
1322 #else /* UIP_CONF_ROUTER && UIP_ND6_SEND_RA */
1323  UIP_STAT(++uip_stat.icmp.drop);
1324  uip_len = 0;
1325 #endif /* UIP_CONF_ROUTER && UIP_ND6_SEND_RA */
1326  break;
1327  case ICMP6_RA:
1328 #if UIP_CONF_ROUTER
1329  UIP_STAT(++uip_stat.icmp.drop);
1330  uip_len = 0;
1331 #else /* UIP_CONF_ROUTER */
1332  uip_nd6_ra_input();
1333 #endif /* UIP_CONF_ROUTER */
1334  break;
1335 #if UIP_CONF_IPV6_RPL
1336  case ICMP6_RPL:
1337  uip_rpl_input();
1338  break;
1339 #endif /* UIP_CONF_IPV6_RPL */
1340  case ICMP6_ECHO_REQUEST:
1342  break;
1343  case ICMP6_ECHO_REPLY:
1344  /** \note We don't implement any application callback for now */
1345  PRINTF("Received an icmp6 echo reply\n");
1346  UIP_STAT(++uip_stat.icmp.recv);
1347  uip_len = 0;
1348  break;
1349  default:
1350  PRINTF("Unknown icmp6 message type %d\n", UIP_ICMP_BUF->type);
1351  UIP_STAT(++uip_stat.icmp.drop);
1352  UIP_STAT(++uip_stat.icmp.typeerr);
1353  UIP_LOG("icmp6: unknown ICMP message.");
1354  uip_len = 0;
1355  break;
1356  }
1357 
1358  if(uip_len > 0) {
1359  goto send;
1360  } else {
1361  goto drop;
1362  }
1363  /* End of IPv6 ICMP processing. */
1364 
1365 
1366 #if UIP_UDP
1367  /* UDP input processing. */
1368  udp_input:
1369  PRINTF("Receiving UDP packet\n");
1370  UIP_STAT(++uip_stat.udp.recv);
1371 
1372  /* UDP processing is really just a hack. We don't do anything to the
1373  UDP/IP headers, but let the UDP application do all the hard
1374  work. If the application sets uip_slen, it has a packet to
1375  send. */
1376 #if UIP_UDP_CHECKSUMS
1377  uip_len = uip_len - UIP_IPUDPH_LEN;
1378  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
1379  if(UIP_UDP_BUF->udpchksum != 0 && uip_udpchksum() != 0xffff) {
1380  UIP_STAT(++uip_stat.udp.drop);
1381  UIP_STAT(++uip_stat.udp.chkerr);
1382  PRINTF("udp: bad checksum 0x%04x 0x%04x\n", UIP_UDP_BUF->udpchksum,
1383  uip_udpchksum());
1384  goto drop;
1385  }
1386 #else /* UIP_UDP_CHECKSUMS */
1387  uip_len = uip_len - UIP_IPUDPH_LEN;
1388 #endif /* UIP_UDP_CHECKSUMS */
1389 
1390  /* Make sure that the UDP destination port number is not zero. */
1391  if(UIP_UDP_BUF->destport == 0) {
1392  PRINTF("udp: zero port.\n");
1393  goto drop;
1394  }
1395 
1396  /* Demultiplex this UDP packet between the UDP "connections". */
1397  for(uip_udp_conn = &uip_udp_conns[0];
1398  uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
1399  ++uip_udp_conn) {
1400  /* If the local UDP port is non-zero, the connection is considered
1401  to be used. If so, the local port number is checked against the
1402  destination port number in the received packet. If the two port
1403  numbers match, the remote port number is checked if the
1404  connection is bound to a remote port. Finally, if the
1405  connection is bound to a remote IP address, the source IP
1406  address of the packet is checked. */
1407  if(uip_udp_conn->lport != 0 &&
1408  UIP_UDP_BUF->destport == uip_udp_conn->lport &&
1409  (uip_udp_conn->rport == 0 ||
1410  UIP_UDP_BUF->srcport == uip_udp_conn->rport) &&
1411  (uip_is_addr_unspecified(&uip_udp_conn->ripaddr) ||
1412  uip_ipaddr_cmp(&UIP_IP_BUF->srcipaddr, &uip_udp_conn->ripaddr))) {
1413  goto udp_found;
1414  }
1415  }
1416  PRINTF("udp: no matching connection found\n");
1417 
1418 #if UIP_UDP_SEND_UNREACH_NOPORT
1420  UIP_STAT(++uip_stat.ip.drop);
1421  goto send;
1422 #else
1423  goto drop;
1424 #endif
1425 
1426  udp_found:
1427  PRINTF("In udp_found\n");
1428 
1429  uip_conn = NULL;
1430  uip_flags = UIP_NEWDATA;
1431  uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
1432  uip_slen = 0;
1433  UIP_UDP_APPCALL();
1434 
1435  udp_send:
1436  PRINTF("In udp_send\n");
1437 
1438  if(uip_slen == 0) {
1439  goto drop;
1440  }
1441  uip_len = uip_slen + UIP_IPUDPH_LEN;
1442 
1443  /* For IPv6, the IP length field does not include the IPv6 IP header
1444  length. */
1445  UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
1446  UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
1447 
1448  UIP_IP_BUF->ttl = uip_udp_conn->ttl;
1449  UIP_IP_BUF->proto = UIP_PROTO_UDP;
1450 
1451  UIP_UDP_BUF->udplen = UIP_HTONS(uip_slen + UIP_UDPH_LEN);
1452  UIP_UDP_BUF->udpchksum = 0;
1453 
1454  UIP_UDP_BUF->srcport = uip_udp_conn->lport;
1455  UIP_UDP_BUF->destport = uip_udp_conn->rport;
1456 
1457  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &uip_udp_conn->ripaddr);
1458  uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
1459 
1460  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];
1461 
1462 #if UIP_UDP_CHECKSUMS
1463  /* Calculate UDP checksum. */
1464  UIP_UDP_BUF->udpchksum = ~(uip_udpchksum());
1465  if(UIP_UDP_BUF->udpchksum == 0) {
1466  UIP_UDP_BUF->udpchksum = 0xffff;
1467  }
1468 #endif /* UIP_UDP_CHECKSUMS */
1469  UIP_STAT(++uip_stat.udp.sent);
1470  goto ip_send_nolen;
1471 #endif /* UIP_UDP */
1472 
1473 #if UIP_TCP
1474  /* TCP input processing. */
1475  tcp_input:
1476 
1477  UIP_STAT(++uip_stat.tcp.recv);
1478  PRINTF("Receiving TCP packet\n");
1479  /* Start of TCP input header processing code. */
1480 
1481  if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
1482  checksum. */
1483  UIP_STAT(++uip_stat.tcp.drop);
1484  UIP_STAT(++uip_stat.tcp.chkerr);
1485  UIP_LOG("tcp: bad checksum.");
1486  goto drop;
1487  }
1488 
1489  /* Make sure that the TCP port number is not zero. */
1490  if(UIP_TCP_BUF->destport == 0 || UIP_TCP_BUF->srcport == 0) {
1491  UIP_LOG("tcp: zero port.");
1492  goto drop;
1493  }
1494 
1495  /* Demultiplex this segment. */
1496  /* First check any active connections. */
1497  for(uip_connr = &uip_conns[0]; uip_connr <= &uip_conns[UIP_CONNS - 1];
1498  ++uip_connr) {
1499  if(uip_connr->tcpstateflags != UIP_CLOSED &&
1500  UIP_TCP_BUF->destport == uip_connr->lport &&
1501  UIP_TCP_BUF->srcport == uip_connr->rport &&
1502  uip_ipaddr_cmp(&UIP_IP_BUF->srcipaddr, &uip_connr->ripaddr)) {
1503  goto found;
1504  }
1505  }
1506 
1507  /* If we didn't find and active connection that expected the packet,
1508  either this packet is an old duplicate, or this is a SYN packet
1509  destined for a connection in LISTEN. If the SYN flag isn't set,
1510  it is an old packet and we send a RST. */
1511  if((UIP_TCP_BUF->flags & TCP_CTL) != TCP_SYN) {
1512  goto reset;
1513  }
1514 
1515  tmp16 = UIP_TCP_BUF->destport;
1516  /* Next, check listening connections. */
1517  for(c = 0; c < UIP_LISTENPORTS; ++c) {
1518  if(tmp16 == uip_listenports[c]) {
1519  goto found_listen;
1520  }
1521  }
1522 
1523  /* No matching connection found, so we send a RST packet. */
1524  UIP_STAT(++uip_stat.tcp.synrst);
1525 
1526  reset:
1527  PRINTF("In reset\n");
1528  /* We do not send resets in response to resets. */
1529  if(UIP_TCP_BUF->flags & TCP_RST) {
1530  goto drop;
1531  }
1532 
1533  UIP_STAT(++uip_stat.tcp.rst);
1534 
1535  UIP_TCP_BUF->flags = TCP_RST | TCP_ACK;
1536  uip_len = UIP_IPTCPH_LEN;
1537  UIP_TCP_BUF->tcpoffset = 5 << 4;
1538 
1539  /* Flip the seqno and ackno fields in the TCP header. */
1540  c = UIP_TCP_BUF->seqno[3];
1541  UIP_TCP_BUF->seqno[3] = UIP_TCP_BUF->ackno[3];
1542  UIP_TCP_BUF->ackno[3] = c;
1543 
1544  c = UIP_TCP_BUF->seqno[2];
1545  UIP_TCP_BUF->seqno[2] = UIP_TCP_BUF->ackno[2];
1546  UIP_TCP_BUF->ackno[2] = c;
1547 
1548  c = UIP_TCP_BUF->seqno[1];
1549  UIP_TCP_BUF->seqno[1] = UIP_TCP_BUF->ackno[1];
1550  UIP_TCP_BUF->ackno[1] = c;
1551 
1552  c = UIP_TCP_BUF->seqno[0];
1553  UIP_TCP_BUF->seqno[0] = UIP_TCP_BUF->ackno[0];
1554  UIP_TCP_BUF->ackno[0] = c;
1555 
1556  /* We also have to increase the sequence number we are
1557  acknowledging. If the least significant byte overflowed, we need
1558  to propagate the carry to the other bytes as well. */
1559  if(++UIP_TCP_BUF->ackno[3] == 0) {
1560  if(++UIP_TCP_BUF->ackno[2] == 0) {
1561  if(++UIP_TCP_BUF->ackno[1] == 0) {
1562  ++UIP_TCP_BUF->ackno[0];
1563  }
1564  }
1565  }
1566 
1567  /* Swap port numbers. */
1568  tmp16 = UIP_TCP_BUF->srcport;
1569  UIP_TCP_BUF->srcport = UIP_TCP_BUF->destport;
1570  UIP_TCP_BUF->destport = tmp16;
1571 
1572  /* Swap IP addresses. */
1573  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
1574  uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
1575  /* And send out the RST packet! */
1576  goto tcp_send_noconn;
1577 
1578  /* This label will be jumped to if we matched the incoming packet
1579  with a connection in LISTEN. In that case, we should create a new
1580  connection and send a SYNACK in return. */
1581  found_listen:
1582  PRINTF("In found listen\n");
1583  /* First we check if there are any connections avaliable. Unused
1584  connections are kept in the same table as used connections, but
1585  unused ones have the tcpstate set to CLOSED. Also, connections in
1586  TIME_WAIT are kept track of and we'll use the oldest one if no
1587  CLOSED connections are found. Thanks to Eddie C. Dost for a very
1588  nice algorithm for the TIME_WAIT search. */
1589  uip_connr = 0;
1590  for(c = 0; c < UIP_CONNS; ++c) {
1591  if(uip_conns[c].tcpstateflags == UIP_CLOSED) {
1592  uip_connr = &uip_conns[c];
1593  break;
1594  }
1595  if(uip_conns[c].tcpstateflags == UIP_TIME_WAIT) {
1596  if(uip_connr == 0 ||
1597  uip_conns[c].timer > uip_connr->timer) {
1598  uip_connr = &uip_conns[c];
1599  }
1600  }
1601  }
1602 
1603  if(uip_connr == 0) {
1604  /* All connections are used already, we drop packet and hope that
1605  the remote end will retransmit the packet at a time when we
1606  have more spare connections. */
1607  UIP_STAT(++uip_stat.tcp.syndrop);
1608  UIP_LOG("tcp: found no unused connections.");
1609  goto drop;
1610  }
1611  uip_conn = uip_connr;
1612 
1613  /* Fill in the necessary fields for the new connection. */
1614  uip_connr->rto = uip_connr->timer = UIP_RTO;
1615  uip_connr->sa = 0;
1616  uip_connr->sv = 4;
1617  uip_connr->nrtx = 0;
1618  uip_connr->lport = UIP_TCP_BUF->destport;
1619  uip_connr->rport = UIP_TCP_BUF->srcport;
1620  uip_ipaddr_copy(&uip_connr->ripaddr, &UIP_IP_BUF->srcipaddr);
1621  uip_connr->tcpstateflags = UIP_SYN_RCVD;
1622 
1623  uip_connr->snd_nxt[0] = iss[0];
1624  uip_connr->snd_nxt[1] = iss[1];
1625  uip_connr->snd_nxt[2] = iss[2];
1626  uip_connr->snd_nxt[3] = iss[3];
1627  uip_connr->len = 1;
1628 
1629  /* rcv_nxt should be the seqno from the incoming packet + 1. */
1630  uip_connr->rcv_nxt[3] = UIP_TCP_BUF->seqno[3];
1631  uip_connr->rcv_nxt[2] = UIP_TCP_BUF->seqno[2];
1632  uip_connr->rcv_nxt[1] = UIP_TCP_BUF->seqno[1];
1633  uip_connr->rcv_nxt[0] = UIP_TCP_BUF->seqno[0];
1634  uip_add_rcv_nxt(1);
1635 
1636  /* Parse the TCP MSS option, if present. */
1637  if((UIP_TCP_BUF->tcpoffset & 0xf0) > 0x50) {
1638  for(c = 0; c < ((UIP_TCP_BUF->tcpoffset >> 4) - 5) << 2 ;) {
1639  opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
1640  if(opt == TCP_OPT_END) {
1641  /* End of options. */
1642  break;
1643  } else if(opt == TCP_OPT_NOOP) {
1644  ++c;
1645  /* NOP option. */
1646  } else if(opt == TCP_OPT_MSS &&
1647  uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
1648  /* An MSS option with the right option length. */
1649  tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
1650  (u16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c];
1651  uip_connr->initialmss = uip_connr->mss =
1652  tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
1653 
1654  /* And we are done processing options. */
1655  break;
1656  } else {
1657  /* All other options have a length field, so that we easily
1658  can skip past them. */
1659  if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
1660  /* If the length field is zero, the options are malformed
1661  and we don't process them further. */
1662  break;
1663  }
1664  c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
1665  }
1666  }
1667  }
1668 
1669  /* Our response will be a SYNACK. */
1670 #if UIP_ACTIVE_OPEN
1671  tcp_send_synack:
1672  UIP_TCP_BUF->flags = TCP_ACK;
1673 
1674  tcp_send_syn:
1675  UIP_TCP_BUF->flags |= TCP_SYN;
1676 #else /* UIP_ACTIVE_OPEN */
1677  tcp_send_synack:
1678  UIP_TCP_BUF->flags = TCP_SYN | TCP_ACK;
1679 #endif /* UIP_ACTIVE_OPEN */
1680 
1681  /* We send out the TCP Maximum Segment Size option with our
1682  SYNACK. */
1683  UIP_TCP_BUF->optdata[0] = TCP_OPT_MSS;
1684  UIP_TCP_BUF->optdata[1] = TCP_OPT_MSS_LEN;
1685  UIP_TCP_BUF->optdata[2] = (UIP_TCP_MSS) / 256;
1686  UIP_TCP_BUF->optdata[3] = (UIP_TCP_MSS) & 255;
1687  uip_len = UIP_IPTCPH_LEN + TCP_OPT_MSS_LEN;
1688  UIP_TCP_BUF->tcpoffset = ((UIP_TCPH_LEN + TCP_OPT_MSS_LEN) / 4) << 4;
1689  goto tcp_send;
1690 
1691  /* This label will be jumped to if we found an active connection. */
1692  found:
1693  PRINTF("In found\n");
1694  uip_conn = uip_connr;
1695  uip_flags = 0;
1696  /* We do a very naive form of TCP reset processing; we just accept
1697  any RST and kill our connection. We should in fact check if the
1698  sequence number of this reset is wihtin our advertised window
1699  before we accept the reset. */
1700  if(UIP_TCP_BUF->flags & TCP_RST) {
1701  uip_connr->tcpstateflags = UIP_CLOSED;
1702  UIP_LOG("tcp: got reset, aborting connection.");
1703  uip_flags = UIP_ABORT;
1704  UIP_APPCALL();
1705  goto drop;
1706  }
1707  /* Calculate the length of the data, if the application has sent
1708  any data to us. */
1709  c = (UIP_TCP_BUF->tcpoffset >> 4) << 2;
1710  /* uip_len will contain the length of the actual TCP data. This is
1711  calculated by subtracing the length of the TCP header (in
1712  c) and the length of the IP header (20 bytes). */
1713  uip_len = uip_len - c - UIP_IPH_LEN;
1714 
1715  /* First, check if the sequence number of the incoming packet is
1716  what we're expecting next. If not, we send out an ACK with the
1717  correct numbers in, unless we are in the SYN_RCVD state and
1718  receive a SYN, in which case we should retransmit our SYNACK
1719  (which is done futher down). */
1720  if(!((((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&
1721  ((UIP_TCP_BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK))) ||
1722  (((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_RCVD) &&
1723  ((UIP_TCP_BUF->flags & TCP_CTL) == TCP_SYN)))) {
1724  if((uip_len > 0 || ((UIP_TCP_BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) &&
1725  (UIP_TCP_BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
1726  UIP_TCP_BUF->seqno[1] != uip_connr->rcv_nxt[1] ||
1727  UIP_TCP_BUF->seqno[2] != uip_connr->rcv_nxt[2] ||
1728  UIP_TCP_BUF->seqno[3] != uip_connr->rcv_nxt[3])) {
1729 
1730  if(UIP_TCP_BUF->flags & TCP_SYN) {
1731  goto tcp_send_synack;
1732  }
1733  goto tcp_send_ack;
1734  }
1735  }
1736 
1737  /* Next, check if the incoming segment acknowledges any outstanding
1738  data. If so, we update the sequence number, reset the length of
1739  the outstanding data, calculate RTT estimations, and reset the
1740  retransmission timer. */
1741  if((UIP_TCP_BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {
1742  uip_add32(uip_connr->snd_nxt, uip_connr->len);
1743 
1744  if(UIP_TCP_BUF->ackno[0] == uip_acc32[0] &&
1745  UIP_TCP_BUF->ackno[1] == uip_acc32[1] &&
1746  UIP_TCP_BUF->ackno[2] == uip_acc32[2] &&
1747  UIP_TCP_BUF->ackno[3] == uip_acc32[3]) {
1748  /* Update sequence number. */
1749  uip_connr->snd_nxt[0] = uip_acc32[0];
1750  uip_connr->snd_nxt[1] = uip_acc32[1];
1751  uip_connr->snd_nxt[2] = uip_acc32[2];
1752  uip_connr->snd_nxt[3] = uip_acc32[3];
1753 
1754  /* Do RTT estimation, unless we have done retransmissions. */
1755  if(uip_connr->nrtx == 0) {
1756  signed char m;
1757  m = uip_connr->rto - uip_connr->timer;
1758  /* This is taken directly from VJs original code in his paper */
1759  m = m - (uip_connr->sa >> 3);
1760  uip_connr->sa += m;
1761  if(m < 0) {
1762  m = -m;
1763  }
1764  m = m - (uip_connr->sv >> 2);
1765  uip_connr->sv += m;
1766  uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;
1767 
1768  }
1769  /* Set the acknowledged flag. */
1770  uip_flags = UIP_ACKDATA;
1771  /* Reset the retransmission timer. */
1772  uip_connr->timer = uip_connr->rto;
1773 
1774  /* Reset length of outstanding data. */
1775  uip_connr->len = 0;
1776  }
1777 
1778  }
1779 
1780  /* Do different things depending on in what state the connection is. */
1781  switch(uip_connr->tcpstateflags & UIP_TS_MASK) {
1782  /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
1783  implemented, since we force the application to close when the
1784  peer sends a FIN (hence the application goes directly from
1785  ESTABLISHED to LAST_ACK). */
1786  case UIP_SYN_RCVD:
1787  /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
1788  we are waiting for an ACK that acknowledges the data we sent
1789  out the last time. Therefore, we want to have the UIP_ACKDATA
1790  flag set. If so, we enter the ESTABLISHED state. */
1791  if(uip_flags & UIP_ACKDATA) {
1792  uip_connr->tcpstateflags = UIP_ESTABLISHED;
1793  uip_flags = UIP_CONNECTED;
1794  uip_connr->len = 0;
1795  if(uip_len > 0) {
1796  uip_flags |= UIP_NEWDATA;
1797  uip_add_rcv_nxt(uip_len);
1798  }
1799  uip_slen = 0;
1800  UIP_APPCALL();
1801  goto appsend;
1802  }
1803  /* We need to retransmit the SYNACK */
1804  if((UIP_TCP_BUF->flags & TCP_CTL) == TCP_SYN) {
1805  goto tcp_send_synack;
1806  }
1807  goto drop;
1808 #if UIP_ACTIVE_OPEN
1809  case UIP_SYN_SENT:
1810  /* In SYN_SENT, we wait for a SYNACK that is sent in response to
1811  our SYN. The rcv_nxt is set to sequence number in the SYNACK
1812  plus one, and we send an ACK. We move into the ESTABLISHED
1813  state. */
1814  if((uip_flags & UIP_ACKDATA) &&
1815  (UIP_TCP_BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)) {
1816 
1817  /* Parse the TCP MSS option, if present. */
1818  if((UIP_TCP_BUF->tcpoffset & 0xf0) > 0x50) {
1819  for(c = 0; c < ((UIP_TCP_BUF->tcpoffset >> 4) - 5) << 2 ;) {
1820  opt = uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + c];
1821  if(opt == TCP_OPT_END) {
1822  /* End of options. */
1823  break;
1824  } else if(opt == TCP_OPT_NOOP) {
1825  ++c;
1826  /* NOP option. */
1827  } else if(opt == TCP_OPT_MSS &&
1828  uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
1829  /* An MSS option with the right option length. */
1830  tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
1831  uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];
1832  uip_connr->initialmss =
1833  uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
1834 
1835  /* And we are done processing options. */
1836  break;
1837  } else {
1838  /* All other options have a length field, so that we easily
1839  can skip past them. */
1840  if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
1841  /* If the length field is zero, the options are malformed
1842  and we don't process them further. */
1843  break;
1844  }
1845  c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
1846  }
1847  }
1848  }
1849  uip_connr->tcpstateflags = UIP_ESTABLISHED;
1850  uip_connr->rcv_nxt[0] = UIP_TCP_BUF->seqno[0];
1851  uip_connr->rcv_nxt[1] = UIP_TCP_BUF->seqno[1];
1852  uip_connr->rcv_nxt[2] = UIP_TCP_BUF->seqno[2];
1853  uip_connr->rcv_nxt[3] = UIP_TCP_BUF->seqno[3];
1854  uip_add_rcv_nxt(1);
1855  uip_flags = UIP_CONNECTED | UIP_NEWDATA;
1856  uip_connr->len = 0;
1857  uip_len = 0;
1858  uip_slen = 0;
1859  UIP_APPCALL();
1860  goto appsend;
1861  }
1862  /* Inform the application that the connection failed */
1863  uip_flags = UIP_ABORT;
1864  UIP_APPCALL();
1865  /* The connection is closed after we send the RST */
1866  uip_conn->tcpstateflags = UIP_CLOSED;
1867  goto reset;
1868 #endif /* UIP_ACTIVE_OPEN */
1869 
1870  case UIP_ESTABLISHED:
1871  /* In the ESTABLISHED state, we call upon the application to feed
1872  data into the uip_buf. If the UIP_ACKDATA flag is set, the
1873  application should put new data into the buffer, otherwise we are
1874  retransmitting an old segment, and the application should put that
1875  data into the buffer.
1876 
1877  If the incoming packet is a FIN, we should close the connection on
1878  this side as well, and we send out a FIN and enter the LAST_ACK
1879  state. We require that there is no outstanding data; otherwise the
1880  sequence numbers will be screwed up. */
1881 
1882  if(UIP_TCP_BUF->flags & TCP_FIN && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
1883  if(uip_outstanding(uip_connr)) {
1884  goto drop;
1885  }
1886  uip_add_rcv_nxt(1 + uip_len);
1887  uip_flags |= UIP_CLOSE;
1888  if(uip_len > 0) {
1889  uip_flags |= UIP_NEWDATA;
1890  }
1891  UIP_APPCALL();
1892  uip_connr->len = 1;
1893  uip_connr->tcpstateflags = UIP_LAST_ACK;
1894  uip_connr->nrtx = 0;
1895  tcp_send_finack:
1896  UIP_TCP_BUF->flags = TCP_FIN | TCP_ACK;
1897  goto tcp_send_nodata;
1898  }
1899 
1900  /* Check the URG flag. If this is set, the segment carries urgent
1901  data that we must pass to the application. */
1902  if((UIP_TCP_BUF->flags & TCP_URG) != 0) {
1903 #if UIP_URGDATA > 0
1904  uip_urglen = (UIP_TCP_BUF->urgp[0] << 8) | UIP_TCP_BUF->urgp[1];
1905  if(uip_urglen > uip_len) {
1906  /* There is more urgent data in the next segment to come. */
1907  uip_urglen = uip_len;
1908  }
1909  uip_add_rcv_nxt(uip_urglen);
1910  uip_len -= uip_urglen;
1911  uip_urgdata = uip_appdata;
1912  uip_appdata += uip_urglen;
1913  } else {
1914  uip_urglen = 0;
1915 #else /* UIP_URGDATA > 0 */
1916  uip_appdata = ((char *)uip_appdata) + ((UIP_TCP_BUF->urgp[0] << 8) | UIP_TCP_BUF->urgp[1]);
1917  uip_len -= (UIP_TCP_BUF->urgp[0] << 8) | UIP_TCP_BUF->urgp[1];
1918 #endif /* UIP_URGDATA > 0 */
1919  }
1920 
1921  /* If uip_len > 0 we have TCP data in the packet, and we flag this
1922  by setting the UIP_NEWDATA flag and update the sequence number
1923  we acknowledge. If the application has stopped the dataflow
1924  using uip_stop(), we must not accept any data packets from the
1925  remote host. */
1926  if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
1927  uip_flags |= UIP_NEWDATA;
1928  uip_add_rcv_nxt(uip_len);
1929  }
1930 
1931  /* Check if the available buffer space advertised by the other end
1932  is smaller than the initial MSS for this connection. If so, we
1933  set the current MSS to the window size to ensure that the
1934  application does not send more data than the other end can
1935  handle.
1936 
1937  If the remote host advertises a zero window, we set the MSS to
1938  the initial MSS so that the application will send an entire MSS
1939  of data. This data will not be acknowledged by the receiver,
1940  and the application will retransmit it. This is called the
1941  "persistent timer" and uses the retransmission mechanim.
1942  */
1943  tmp16 = ((u16_t)UIP_TCP_BUF->wnd[0] << 8) + (u16_t)UIP_TCP_BUF->wnd[1];
1944  if(tmp16 > uip_connr->initialmss ||
1945  tmp16 == 0) {
1946  tmp16 = uip_connr->initialmss;
1947  }
1948  uip_connr->mss = tmp16;
1949 
1950  /* If this packet constitutes an ACK for outstanding data (flagged
1951  by the UIP_ACKDATA flag, we should call the application since it
1952  might want to send more data. If the incoming packet had data
1953  from the peer (as flagged by the UIP_NEWDATA flag), the
1954  application must also be notified.
1955 
1956  When the application is called, the global variable uip_len
1957  contains the length of the incoming data. The application can
1958  access the incoming data through the global pointer
1959  uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN
1960  bytes into the uip_buf array.
1961 
1962  If the application wishes to send any data, this data should be
1963  put into the uip_appdata and the length of the data should be
1964  put into uip_len. If the application don't have any data to
1965  send, uip_len must be set to 0. */
1966  if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {
1967  uip_slen = 0;
1968  UIP_APPCALL();
1969 
1970  appsend:
1971 
1972  if(uip_flags & UIP_ABORT) {
1973  uip_slen = 0;
1974  uip_connr->tcpstateflags = UIP_CLOSED;
1975  UIP_TCP_BUF->flags = TCP_RST | TCP_ACK;
1976  goto tcp_send_nodata;
1977  }
1978 
1979  if(uip_flags & UIP_CLOSE) {
1980  uip_slen = 0;
1981  uip_connr->len = 1;
1982  uip_connr->tcpstateflags = UIP_FIN_WAIT_1;
1983  uip_connr->nrtx = 0;
1984  UIP_TCP_BUF->flags = TCP_FIN | TCP_ACK;
1985  goto tcp_send_nodata;
1986  }
1987 
1988  /* If uip_slen > 0, the application has data to be sent. */
1989  if(uip_slen > 0) {
1990 
1991  /* If the connection has acknowledged data, the contents of
1992  the ->len variable should be discarded. */
1993  if((uip_flags & UIP_ACKDATA) != 0) {
1994  uip_connr->len = 0;
1995  }
1996 
1997  /* If the ->len variable is non-zero the connection has
1998  already data in transit and cannot send anymore right
1999  now. */
2000  if(uip_connr->len == 0) {
2001 
2002  /* The application cannot send more than what is allowed by
2003  the mss (the minumum of the MSS and the available
2004  window). */
2005  if(uip_slen > uip_connr->mss) {
2006  uip_slen = uip_connr->mss;
2007  }
2008 
2009  /* Remember how much data we send out now so that we know
2010  when everything has been acknowledged. */
2011  uip_connr->len = uip_slen;
2012  } else {
2013 
2014  /* If the application already had unacknowledged data, we
2015  make sure that the application does not send (i.e.,
2016  retransmit) out more than it previously sent out. */
2017  uip_slen = uip_connr->len;
2018  }
2019  }
2020  uip_connr->nrtx = 0;
2021  apprexmit:
2022  uip_appdata = uip_sappdata;
2023 
2024  /* If the application has data to be sent, or if the incoming
2025  packet had new data in it, we must send out a packet. */
2026  if(uip_slen > 0 && uip_connr->len > 0) {
2027  /* Add the length of the IP and TCP headers. */
2028  uip_len = uip_connr->len + UIP_TCPIP_HLEN;
2029  /* We always set the ACK flag in response packets. */
2030  UIP_TCP_BUF->flags = TCP_ACK | TCP_PSH;
2031  /* Send the packet. */
2032  goto tcp_send_noopts;
2033  }
2034  /* If there is no data to send, just send out a pure ACK if
2035  there is newdata. */
2036  if(uip_flags & UIP_NEWDATA) {
2037  uip_len = UIP_TCPIP_HLEN;
2038  UIP_TCP_BUF->flags = TCP_ACK;
2039  goto tcp_send_noopts;
2040  }
2041  }
2042  goto drop;
2043  case UIP_LAST_ACK:
2044  /* We can close this connection if the peer has acknowledged our
2045  FIN. This is indicated by the UIP_ACKDATA flag. */
2046  if(uip_flags & UIP_ACKDATA) {
2047  uip_connr->tcpstateflags = UIP_CLOSED;
2048  uip_flags = UIP_CLOSE;
2049  UIP_APPCALL();
2050  }
2051  break;
2052 
2053  case UIP_FIN_WAIT_1:
2054  /* The application has closed the connection, but the remote host
2055  hasn't closed its end yet. Thus we do nothing but wait for a
2056  FIN from the other side. */
2057  if(uip_len > 0) {
2058  uip_add_rcv_nxt(uip_len);
2059  }
2060  if(UIP_TCP_BUF->flags & TCP_FIN) {
2061  if(uip_flags & UIP_ACKDATA) {
2062  uip_connr->tcpstateflags = UIP_TIME_WAIT;
2063  uip_connr->timer = 0;
2064  uip_connr->len = 0;
2065  } else {
2066  uip_connr->tcpstateflags = UIP_CLOSING;
2067  }
2068  uip_add_rcv_nxt(1);
2069  uip_flags = UIP_CLOSE;
2070  UIP_APPCALL();
2071  goto tcp_send_ack;
2072  } else if(uip_flags & UIP_ACKDATA) {
2073  uip_connr->tcpstateflags = UIP_FIN_WAIT_2;
2074  uip_connr->len = 0;
2075  goto drop;
2076  }
2077  if(uip_len > 0) {
2078  goto tcp_send_ack;
2079  }
2080  goto drop;
2081 
2082  case UIP_FIN_WAIT_2:
2083  if(uip_len > 0) {
2084  uip_add_rcv_nxt(uip_len);
2085  }
2086  if(UIP_TCP_BUF->flags & TCP_FIN) {
2087  uip_connr->tcpstateflags = UIP_TIME_WAIT;
2088  uip_connr->timer = 0;
2089  uip_add_rcv_nxt(1);
2090  uip_flags = UIP_CLOSE;
2091  UIP_APPCALL();
2092  goto tcp_send_ack;
2093  }
2094  if(uip_len > 0) {
2095  goto tcp_send_ack;
2096  }
2097  goto drop;
2098 
2099  case UIP_TIME_WAIT:
2100  goto tcp_send_ack;
2101 
2102  case UIP_CLOSING:
2103  if(uip_flags & UIP_ACKDATA) {
2104  uip_connr->tcpstateflags = UIP_TIME_WAIT;
2105  uip_connr->timer = 0;
2106  }
2107  }
2108  goto drop;
2109 
2110  /* We jump here when we are ready to send the packet, and just want
2111  to set the appropriate TCP sequence numbers in the TCP header. */
2112  tcp_send_ack:
2113  UIP_TCP_BUF->flags = TCP_ACK;
2114 
2115  tcp_send_nodata:
2116  uip_len = UIP_IPTCPH_LEN;
2117 
2118  tcp_send_noopts:
2119  UIP_TCP_BUF->tcpoffset = (UIP_TCPH_LEN / 4) << 4;
2120 
2121  /* We're done with the input processing. We are now ready to send a
2122  reply. Our job is to fill in all the fields of the TCP and IP
2123  headers before calculating the checksum and finally send the
2124  packet. */
2125  tcp_send:
2126  PRINTF("In tcp_send\n");
2127 
2128  UIP_TCP_BUF->ackno[0] = uip_connr->rcv_nxt[0];
2129  UIP_TCP_BUF->ackno[1] = uip_connr->rcv_nxt[1];
2130  UIP_TCP_BUF->ackno[2] = uip_connr->rcv_nxt[2];
2131  UIP_TCP_BUF->ackno[3] = uip_connr->rcv_nxt[3];
2132 
2133  UIP_TCP_BUF->seqno[0] = uip_connr->snd_nxt[0];
2134  UIP_TCP_BUF->seqno[1] = uip_connr->snd_nxt[1];
2135  UIP_TCP_BUF->seqno[2] = uip_connr->snd_nxt[2];
2136  UIP_TCP_BUF->seqno[3] = uip_connr->snd_nxt[3];
2137 
2138  UIP_IP_BUF->proto = UIP_PROTO_TCP;
2139 
2140  UIP_TCP_BUF->srcport = uip_connr->lport;
2141  UIP_TCP_BUF->destport = uip_connr->rport;
2142 
2143 
2144  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &uip_connr->ripaddr);
2145  uip_ds6_select_src(&UIP_IP_BUF->srcipaddr,&UIP_IP_BUF->destipaddr);
2146  PRINTF("Sending TCP packet to");
2147  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
2148  PRINTF("from");
2149  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
2150  PRINTF("\n");
2151 
2152  if(uip_connr->tcpstateflags & UIP_STOPPED) {
2153  /* If the connection has issued uip_stop(), we advertise a zero
2154  window so that the remote host will stop sending data. */
2155  UIP_TCP_BUF->wnd[0] = UIP_TCP_BUF->wnd[1] = 0;
2156  } else {
2157  UIP_TCP_BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);
2158  UIP_TCP_BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);
2159  }
2160 
2161  tcp_send_noconn:
2162  UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit;
2163  UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
2164  UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
2165 
2166  UIP_TCP_BUF->urgp[0] = UIP_TCP_BUF->urgp[1] = 0;
2167 
2168  /* Calculate TCP checksum. */
2169  UIP_TCP_BUF->tcpchksum = 0;
2170  UIP_TCP_BUF->tcpchksum = ~(uip_tcpchksum());
2171  UIP_STAT(++uip_stat.tcp.sent);
2172 
2173 #endif /* UIP_TCP */
2174 #if UIP_UDP
2175  ip_send_nolen:
2176 #endif
2177  UIP_IP_BUF->vtc = 0x60;
2178  UIP_IP_BUF->tcflow = 0x00;
2179  UIP_IP_BUF->flow = 0x00;
2180  send:
2181  PRINTF("Sending packet with length %d (%d)\n", uip_len,
2182  (UIP_IP_BUF->len[0] << 8) | UIP_IP_BUF->len[1]);
2183 
2184  UIP_STAT(++uip_stat.ip.sent);
2185  /* Return and let the caller do the actual transmission. */
2186  uip_flags = 0;
2187  return;
2188 
2189  drop:
2190  uip_len = 0;
2191  uip_ext_len = 0;
2192  uip_ext_bitmap = 0;
2193  uip_flags = 0;
2194  return;
2195 }
2196 /*---------------------------------------------------------------------------*/
2197 u16_t
2198 uip_htons(u16_t val)
2199 {
2200  return UIP_HTONS(val);
2201 }
2202 
2203 u32_t
2204 uip_htonl(u32_t val)
2205 {
2206  return UIP_HTONL(val);
2207 }
2208 /*---------------------------------------------------------------------------*/
2209 void
2210 uip_send(const void *data, int len)
2211 {
2212  int copylen;
2213 #define MIN(a,b) ((a) < (b)? (a): (b))
2214  copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN -
2215  (int)((char *)uip_sappdata - (char *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN]));
2216  if(copylen > 0) {
2217  uip_slen = copylen;
2218  if(data != uip_sappdata) {
2219  memcpy(uip_sappdata, (data), uip_slen);
2220  }
2221  }
2222 }
2223 /*---------------------------------------------------------------------------*/
2224 /** @} */