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