8 * The uIP TCP/IP stack code.
9 * \author Adam Dunkels <adam@dunkels.com>
13 * Copyright (c) 2001-2003, Adam Dunkels.
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. The name of the author may not be used to endorse or promote
25 * products derived from this software without specific prior
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
29 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
34 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * This file is part of the uIP TCP/IP stack.
47 This is a small implementation of the IP and TCP protocols (as well as
48 some basic ICMP stuff). The implementation couples the IP, TCP and the
49 application layers very tightly. To keep the size of the compiled code
50 down, this code also features heavy usage of the goto statement.
52 The principle is that we have a small buffer, called the uip_buf, in
53 which the device driver puts an incoming packet. The TCP/IP stack
54 parses the headers in the packet, and calls upon the application. If
55 the remote host has sent data to the application, this data is present
56 in the uip_buf and the application read the data from there. It is up
57 to the application to put this data into a byte stream if needed. The
58 application will not be fed with data that is out of sequence.
60 If the application whishes to send data to the peer, it should put its
61 data into the uip_buf, 40 bytes from the start of the buffer. The
62 TCP/IP stack will calculate the checksums, and fill in the necessary
63 header fields and finally send the packet back to the peer.
72 /*-----------------------------------------------------------------------------------*/
73 /* Variable definitions. */
76 /* The IP address of this host. If it is defined to be fixed (by setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set here. Otherwise, the address */
78 const u16_t uip_hostaddr[2] =
79 {HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1),
80 HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)};
81 const u16_t uip_arp_draddr[2] =
82 {HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1),
83 HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)};
84 const u16_t uip_arp_netmask[2] =
85 {HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1),
86 HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)};
88 u16_t uip_hostaddr[2];
89 u16_t uip_arp_draddr[2], uip_arp_netmask[2];
90 #endif /* UIP_FIXEDADDR */
92 u8_t uip_buf[UIP_BUFSIZE+2]; /* The packet buffer that contains
94 volatile u8_t *uip_appdata; /* The uip_appdata pointer points to
96 volatile u8_t *uip_sappdata; /* The uip_appdata pointer points to the
97 application data which is to be sent. */
99 volatile u8_t *uip_urgdata; /* The uip_urgdata pointer points to
100 urgent data (out-of-band data), if
102 volatile u8_t uip_urglen, uip_surglen;
103 #endif /* UIP_URGDATA > 0 */
105 volatile u16_t uip_len, uip_slen;
106 /* The uip_len is either 8 or 16 bits,
107 depending on the maximum packet
110 volatile u8_t uip_flags; /* The uip_flags variable is used for
111 communication between the TCP/IP stack
112 and the application program. */
113 struct uip_conn *uip_conn; /* uip_conn always points to the current
116 struct uip_conn uip_conns[UIP_CONNS];
117 /* The uip_conns array holds all TCP
119 u16_t uip_listenports[UIP_LISTENPORTS];
120 /* The uip_listenports list all currently
123 struct uip_udp_conn *uip_udp_conn;
124 struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
128 static u16_t ipid; /* Ths ipid variable is an increasing
129 number that is used for the IP ID
132 static u8_t iss[4]; /* The iss variable is used for the TCP
133 initial sequence number. */
136 static u16_t lastport = 1024; /* Keeps track of the last port used for
138 #endif /* UIP_ACTIVE_OPEN */
140 /* Temporary variables. */
141 volatile u8_t uip_acc32[4];
145 /* Structures and definitions. */
154 #define ICMP_ECHO_REPLY 0
158 #define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
159 #define FBUF ((uip_tcpip_hdr *)&uip_reassbuf[0])
160 #define ICMPBUF ((uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
161 #define UDPBUF ((uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
163 #if UIP_STATISTICS == 1
164 struct uip_stats uip_stat;
165 #define UIP_STAT(s) s
168 #endif /* UIP_STATISTICS == 1 */
172 void uip_log(char *msg);
173 #define UIP_LOG(m) uip_log(m)
176 #endif /* UIP_LOGGING == 1 */
178 /*-----------------------------------------------------------------------------------*/
183 for(c = 0; c < UIP_LISTENPORTS; ++c) {
184 uip_listenports[c] = 0;
186 for(c = 0; c < UIP_CONNS; ++c) {
187 uip_conns[c].tcpstateflags = CLOSED;
191 #endif /* UIP_ACTIVE_OPEN */
194 for(c = 0; c < UIP_UDP_CONNS; ++c) {
195 uip_udp_conns[c].lport = 0;
200 /* IPv4 initialization. */
201 #if UIP_FIXEDADDR == 0
202 uip_hostaddr[0] = uip_hostaddr[1] = 0;
203 #endif /* UIP_FIXEDADDR */
207 /*-----------------------------------------------------------------------------------*/
210 uip_connect(u16_t *ripaddr, u16_t rport)
212 register struct uip_conn *conn, *cconn;
214 /* Find an unused local port. */
218 if(lastport >= 32000) {
222 /* Check if this port is already in use, and if so try to find
224 for(c = 0; c < UIP_CONNS; ++c) {
225 conn = &uip_conns[c];
226 if(conn->tcpstateflags != CLOSED &&
227 conn->lport == htons(lastport)) {
234 for(c = 0; c < UIP_CONNS; ++c) {
235 cconn = &uip_conns[c];
236 if(cconn->tcpstateflags == CLOSED) {
240 if(cconn->tcpstateflags == TIME_WAIT) {
242 cconn->timer > uip_conn->timer) {
252 conn->tcpstateflags = SYN_SENT;
254 conn->snd_nxt[0] = iss[0];
255 conn->snd_nxt[1] = iss[1];
256 conn->snd_nxt[2] = iss[2];
257 conn->snd_nxt[3] = iss[3];
259 conn->initialmss = conn->mss = UIP_TCP_MSS;
261 conn->len = 1; /* TCP length of the SYN is one. */
263 conn->timer = 1; /* Send the SYN next time around. */
267 conn->lport = htons(lastport);
269 conn->ripaddr[0] = ripaddr[0];
270 conn->ripaddr[1] = ripaddr[1];
274 #endif /* UIP_ACTIVE_OPEN */
275 /*-----------------------------------------------------------------------------------*/
277 struct uip_udp_conn *
278 uip_udp_new(u16_t *ripaddr, u16_t rport)
280 register struct uip_udp_conn *conn;
282 /* Find an unused local port. */
286 if(lastport >= 32000) {
290 for(c = 0; c < UIP_UDP_CONNS; ++c) {
291 if(uip_udp_conns[c].lport == lastport) {
298 for(c = 0; c < UIP_UDP_CONNS; ++c) {
299 if(uip_udp_conns[c].lport == 0) {
300 conn = &uip_udp_conns[c];
309 conn->lport = HTONS(lastport);
310 conn->rport = HTONS(rport);
311 conn->ripaddr[0] = ripaddr[0];
312 conn->ripaddr[1] = ripaddr[1];
317 /*-----------------------------------------------------------------------------------*/
319 uip_unlisten(u16_t port)
321 for(c = 0; c < UIP_LISTENPORTS; ++c) {
322 if(uip_listenports[c] == port) {
323 uip_listenports[c] = 0;
328 /*-----------------------------------------------------------------------------------*/
330 uip_listen(u16_t port)
332 for(c = 0; c < UIP_LISTENPORTS; ++c) {
333 if(uip_listenports[c] == 0) {
334 uip_listenports[c] = port;
339 /*-----------------------------------------------------------------------------------*/
340 /* XXX: IP fragment reassembly: not well-tested. */
343 #define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
344 static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];
345 static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
346 static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
347 0x0f, 0x07, 0x03, 0x01};
348 static u16_t uip_reasslen;
349 static u8_t uip_reassflags;
350 #define UIP_REASS_FLAG_LASTFRAG 0x01
351 static u8_t uip_reasstmr;
362 /* If ip_reasstmr is zero, no packet is present in the buffer, so we
363 write the IP header of the fragment into the reassembly
364 buffer. The timer is updated with the maximum age. */
365 if(uip_reasstmr == 0) {
366 memcpy(uip_reassbuf, &BUF->vhl, IP_HLEN);
367 uip_reasstmr = UIP_REASS_MAXAGE;
369 /* Clear the bitmap. */
370 memset(uip_reassbitmap, sizeof(uip_reassbitmap), 0);
373 /* Check if the incoming fragment matches the one currently present
374 in the reasembly buffer. If so, we proceed with copying the
375 fragment into the buffer. */
376 if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&
377 BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
378 BUF->destipaddr[0] == FBUF->destipaddr[0] &&
379 BUF->destipaddr[1] == FBUF->destipaddr[1] &&
380 BUF->ipid[0] == FBUF->ipid[0] &&
381 BUF->ipid[1] == FBUF->ipid[1]) {
383 len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
384 offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
386 /* If the offset or the offset + fragment length overflows the
387 reassembly buffer, we discard the entire packet. */
388 if(offset > UIP_REASS_BUFSIZE ||
389 offset + len > UIP_REASS_BUFSIZE) {
394 /* Copy the fragment into the reassembly buffer, at the right
396 memcpy(&uip_reassbuf[IP_HLEN + offset],
397 (char *)BUF + (int)((BUF->vhl & 0x0f) * 4),
400 /* Update the bitmap. */
401 if(offset / (8 * 8) == (offset + len) / (8 * 8)) {
402 /* If the two endpoints are in the same byte, we only update
405 uip_reassbitmap[offset / (8 * 8)] |=
406 bitmap_bits[(offset / 8 ) & 7] &
407 ~bitmap_bits[((offset + len) / 8 ) & 7];
409 /* If the two endpoints are in different bytes, we update the
410 bytes in the endpoints and fill the stuff inbetween with
412 uip_reassbitmap[offset / (8 * 8)] |=
413 bitmap_bits[(offset / 8 ) & 7];
414 for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
415 uip_reassbitmap[i] = 0xff;
417 uip_reassbitmap[(offset + len) / (8 * 8)] |=
418 ~bitmap_bits[((offset + len) / 8 ) & 7];
421 /* If this fragment has the More Fragments flag set to zero, we
422 know that this is the last fragment, so we can calculate the
423 size of the entire packet. We also set the
424 IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
425 the final fragment. */
427 if((BUF->ipoffset[0] & IP_MF) == 0) {
428 uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
429 uip_reasslen = offset + len;
432 /* Finally, we check if we have a full packet in the buffer. We do
433 this by checking if we have the last fragment and if all bits
434 in the bitmap are set. */
435 if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
436 /* Check all bytes up to and including all but the last byte in
438 for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {
439 if(uip_reassbitmap[i] != 0xff) {
443 /* Check the last byte in the bitmap. It should contain just the
444 right amount of bits. */
445 if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=
446 (u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {
450 /* If we have come this far, we have a full packet in the
451 buffer, so we allocate a pbuf and copy the packet into it. We
452 also reset the timer. */
454 memcpy(BUF, FBUF, uip_reasslen);
456 /* Pretend to be a "normal" (i.e., not fragmented) IP packet
458 BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
459 BUF->len[0] = uip_reasslen >> 8;
460 BUF->len[1] = uip_reasslen & 0xff;
462 BUF->ipchksum = ~(uip_ipchksum());
471 #endif /* UIP_REASSEMBL */
472 /*-----------------------------------------------------------------------------------*/
474 uip_add_rcv_nxt(u16_t n)
476 uip_add32(uip_conn->rcv_nxt, n);
477 uip_conn->rcv_nxt[0] = uip_acc32[0];
478 uip_conn->rcv_nxt[1] = uip_acc32[1];
479 uip_conn->rcv_nxt[2] = uip_acc32[2];
480 uip_conn->rcv_nxt[3] = uip_acc32[3];
482 /*-----------------------------------------------------------------------------------*/
484 uip_process(u8_t flag)
486 register struct uip_conn *uip_connr = uip_conn;
488 uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
491 /* Check if we were invoked because of the perodic timer fireing. */
492 if(flag == UIP_TIMER) {
494 if(uip_reasstmr != 0) {
497 #endif /* UIP_REASSEMBLY */
498 /* Increase the initial sequence number. */
507 if(uip_connr->tcpstateflags == TIME_WAIT ||
508 uip_connr->tcpstateflags == FIN_WAIT_2) {
509 ++(uip_connr->timer);
510 if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
511 uip_connr->tcpstateflags = CLOSED;
513 } else if(uip_connr->tcpstateflags != CLOSED) {
514 /* If the connection has outstanding data, we increase the
515 connection's timer and see if it has reached the RTO value
516 in which case we retransmit. */
517 if(uip_outstanding(uip_connr)) {
518 if(uip_connr->timer-- == 0) {
519 if(uip_connr->nrtx == UIP_MAXRTX ||
520 ((uip_connr->tcpstateflags == SYN_SENT ||
521 uip_connr->tcpstateflags == SYN_RCVD) &&
522 uip_connr->nrtx == UIP_MAXSYNRTX)) {
523 uip_connr->tcpstateflags = CLOSED;
525 /* We call UIP_APPCALL() with uip_flags set to
526 UIP_TIMEDOUT to inform the application that the
527 connection has timed out. */
528 uip_flags = UIP_TIMEDOUT;
531 /* We also send a reset packet to the remote host. */
532 BUF->flags = TCP_RST | TCP_ACK;
533 goto tcp_send_nodata;
536 /* Exponential backoff. */
537 uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
542 /* Ok, so we need to retransmit. We do this differently
543 depending on which state we are in. In ESTABLISHED, we
544 call upon the application so that it may prepare the
545 data for the retransmit. In SYN_RCVD, we resend the
546 SYNACK that we sent earlier and in LAST_ACK we have to
547 retransmit our FINACK. */
548 UIP_STAT(++uip_stat.tcp.rexmit);
549 switch(uip_connr->tcpstateflags & TS_MASK) {
551 /* In the SYN_RCVD state, we should retransmit our
553 goto tcp_send_synack;
557 /* In the SYN_SENT state, we retransmit out SYN. */
560 #endif /* UIP_ACTIVE_OPEN */
563 /* In the ESTABLISHED state, we call upon the application
564 to do the actual retransmit after which we jump into
565 the code for sending out the packet (the apprexmit
569 uip_flags = UIP_REXMIT;
576 /* In all these states we should retransmit a FINACK. */
577 goto tcp_send_finack;
581 } else if((uip_connr->tcpstateflags & TS_MASK) == ESTABLISHED) {
582 /* If there was no need for a retransmission, we poll the
583 application for new data. */
586 uip_flags = UIP_POLL;
594 if(flag == UIP_UDP_TIMER) {
595 if(uip_udp_conn->lport != 0) {
596 uip_appdata = &uip_buf[UIP_LLH_LEN + 28];
597 uip_len = uip_slen = 0;
598 uip_flags = UIP_POLL;
607 /* This is where the input processing starts. */
608 UIP_STAT(++uip_stat.ip.recv);
611 /* Start of IPv4 input header processing code. */
613 /* Check validity of the IP header. */
614 if(BUF->vhl != 0x45) { /* IP version and header length. */
615 UIP_STAT(++uip_stat.ip.drop);
616 UIP_STAT(++uip_stat.ip.vhlerr);
617 UIP_LOG("ip: invalid version or header length.");
621 /* Check the size of the packet. If the size reported to us in
622 uip_len doesn't match the size reported in the IP header, there
623 has been a transmission error and we drop the packet. */
625 if(BUF->len[0] != (uip_len >> 8)) { /* IP length, high byte. */
626 uip_len = (uip_len & 0xff) | (BUF->len[0] << 8);
628 if(BUF->len[1] != (uip_len & 0xff)) { /* IP length, low byte. */
629 uip_len = (uip_len & 0xff00) | BUF->len[1];
632 /* Check the fragment flag. */
633 if((BUF->ipoffset[0] & 0x3f) != 0 ||
634 BUF->ipoffset[1] != 0) {
636 uip_len = uip_reass();
641 UIP_STAT(++uip_stat.ip.drop);
642 UIP_STAT(++uip_stat.ip.fragerr);
643 UIP_LOG("ip: fragment dropped.");
645 #endif /* UIP_REASSEMBLY */
648 /* If we are configured to use ping IP address configuration and
649 hasn't been assigned an IP address yet, we accept all ICMP
652 if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
653 if(BUF->proto == UIP_PROTO_ICMP) {
654 UIP_LOG("ip: possible ping config packet received.");
657 UIP_LOG("ip: packet dropped since no address assigned.");
661 #endif /* UIP_PINGADDRCONF */
663 /* Check if the packet is destined for our IP address. */
664 if(BUF->destipaddr[0] != uip_hostaddr[0]) {
665 UIP_STAT(++uip_stat.ip.drop);
666 UIP_LOG("ip: packet not for us.");
669 if(BUF->destipaddr[1] != uip_hostaddr[1]) {
670 UIP_STAT(++uip_stat.ip.drop);
671 UIP_LOG("ip: packet not for us.");
675 if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
677 UIP_STAT(++uip_stat.ip.drop);
678 UIP_STAT(++uip_stat.ip.chkerr);
679 UIP_LOG("ip: bad checksum.");
683 if(BUF->proto == UIP_PROTO_TCP) /* Check for TCP packet. If so, jump
684 to the tcp_input label. */
688 if(BUF->proto == UIP_PROTO_UDP)
692 if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from
694 UIP_STAT(++uip_stat.ip.drop);
695 UIP_STAT(++uip_stat.ip.protoerr);
696 UIP_LOG("ip: neither tcp nor icmp.");
701 UIP_STAT(++uip_stat.icmp.recv);
703 /* ICMP echo (i.e., ping) processing. This is simple, we only change
704 the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
705 checksum before we return the packet. */
706 if(ICMPBUF->type != ICMP_ECHO) {
707 UIP_STAT(++uip_stat.icmp.drop);
708 UIP_STAT(++uip_stat.icmp.typeerr);
709 UIP_LOG("icmp: not icmp echo.");
713 /* If we are configured to use ping IP address assignment, we use
714 the destination IP address of this ping packet and assign it to
717 if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
718 uip_hostaddr[0] = BUF->destipaddr[0];
719 uip_hostaddr[1] = BUF->destipaddr[1];
721 #endif /* UIP_PINGADDRCONF */
723 ICMPBUF->type = ICMP_ECHO_REPLY;
725 if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {
726 ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
728 ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);
731 /* Swap IP addresses. */
732 tmp16 = BUF->destipaddr[0];
733 BUF->destipaddr[0] = BUF->srcipaddr[0];
734 BUF->srcipaddr[0] = tmp16;
735 tmp16 = BUF->destipaddr[1];
736 BUF->destipaddr[1] = BUF->srcipaddr[1];
737 BUF->srcipaddr[1] = tmp16;
739 UIP_STAT(++uip_stat.icmp.sent);
742 /* End of IPv4 input header processing code. */
746 /* UDP input processing. */
748 /* UDP processing is really just a hack. We don't do anything to the
749 UDP/IP headers, but let the UDP application do all the hard
750 work. If the application sets uip_slen, it has a packet to
752 #if UIP_UDP_CHECKSUMS
753 if(uip_udpchksum() != 0xffff) {
754 UIP_STAT(++uip_stat.udp.drop);
755 UIP_STAT(++uip_stat.udp.chkerr);
756 UIP_LOG("udp: bad checksum.");
759 #endif /* UIP_UDP_CHECKSUMS */
761 /* Demultiplex this UDP packet between the UDP "connections". */
762 for(uip_udp_conn = &uip_udp_conns[0];
763 uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
765 if(uip_udp_conn->lport != 0 &&
766 UDPBUF->destport == uip_udp_conn->lport &&
767 (uip_udp_conn->rport == 0 ||
768 UDPBUF->srcport == uip_udp_conn->rport) &&
769 BUF->srcipaddr[0] == uip_udp_conn->ripaddr[0] &&
770 BUF->srcipaddr[1] == uip_udp_conn->ripaddr[1]) {
777 uip_len = uip_len - 28;
778 uip_appdata = &uip_buf[UIP_LLH_LEN + 28];
779 uip_flags = UIP_NEWDATA;
786 uip_len = uip_slen + 28;
788 BUF->len[0] = (uip_len >> 8);
789 BUF->len[1] = (uip_len & 0xff);
791 BUF->proto = UIP_PROTO_UDP;
793 UDPBUF->udplen = HTONS(uip_slen + 8);
794 UDPBUF->udpchksum = 0;
795 #if UIP_UDP_CHECKSUMS
796 /* Calculate UDP checksum. */
797 UDPBUF->udpchksum = ~(uip_udpchksum());
798 if(UDPBUF->udpchksum == 0) {
799 UDPBUF->udpchksum = 0xffff;
801 #endif /* UIP_UDP_CHECKSUMS */
803 BUF->srcport = uip_udp_conn->lport;
804 BUF->destport = uip_udp_conn->rport;
806 BUF->srcipaddr[0] = uip_hostaddr[0];
807 BUF->srcipaddr[1] = uip_hostaddr[1];
808 BUF->destipaddr[0] = uip_udp_conn->ripaddr[0];
809 BUF->destipaddr[1] = uip_udp_conn->ripaddr[1];
811 uip_appdata = &uip_buf[UIP_LLH_LEN + 40];
815 /* TCP input processing. */
817 UIP_STAT(++uip_stat.tcp.recv);
819 /* Start of TCP input header processing code. */
821 if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
823 UIP_STAT(++uip_stat.tcp.drop);
824 UIP_STAT(++uip_stat.tcp.chkerr);
825 UIP_LOG("tcp: bad checksum.");
829 /* Demultiplex this segment. */
830 /* First check any active connections. */
831 for(uip_connr = &uip_conns[0]; uip_connr < &uip_conns[UIP_CONNS]; ++uip_connr) {
832 if(uip_connr->tcpstateflags != CLOSED &&
833 BUF->destport == uip_connr->lport &&
834 BUF->srcport == uip_connr->rport &&
835 BUF->srcipaddr[0] == uip_connr->ripaddr[0] &&
836 BUF->srcipaddr[1] == uip_connr->ripaddr[1]) {
841 /* If we didn't find and active connection that expected the packet,
842 either this packet is an old duplicate, or this is a SYN packet
843 destined for a connection in LISTEN. If the SYN flag isn't set,
844 it is an old packet and we send a RST. */
845 if((BUF->flags & TCP_CTL) != TCP_SYN)
848 tmp16 = BUF->destport;
849 /* Next, check listening connections. */
850 for(c = 0; c < UIP_LISTENPORTS; ++c) {
851 if(tmp16 == uip_listenports[c])
855 /* No matching connection found, so we send a RST packet. */
856 UIP_STAT(++uip_stat.tcp.synrst);
859 /* We do not send resets in response to resets. */
860 if(BUF->flags & TCP_RST)
863 UIP_STAT(++uip_stat.tcp.rst);
865 BUF->flags = TCP_RST | TCP_ACK;
867 BUF->tcpoffset = 5 << 4;
869 /* Flip the seqno and ackno fields in the TCP header. */
871 BUF->seqno[3] = BUF->ackno[3];
875 BUF->seqno[2] = BUF->ackno[2];
879 BUF->seqno[1] = BUF->ackno[1];
883 BUF->seqno[0] = BUF->ackno[0];
886 /* We also have to increase the sequence number we are
887 acknowledging. If the least significant byte overflowed, we need
888 to propagate the carry to the other bytes as well. */
889 if(++BUF->ackno[3] == 0) {
890 if(++BUF->ackno[2] == 0) {
891 if(++BUF->ackno[1] == 0) {
897 /* Swap port numbers. */
898 tmp16 = BUF->srcport;
899 BUF->srcport = BUF->destport;
900 BUF->destport = tmp16;
902 /* Swap IP addresses. */
903 tmp16 = BUF->destipaddr[0];
904 BUF->destipaddr[0] = BUF->srcipaddr[0];
905 BUF->srcipaddr[0] = tmp16;
906 tmp16 = BUF->destipaddr[1];
907 BUF->destipaddr[1] = BUF->srcipaddr[1];
908 BUF->srcipaddr[1] = tmp16;
911 /* And send out the RST packet! */
912 goto tcp_send_noconn;
914 /* This label will be jumped to if we matched the incoming packet
915 with a connection in LISTEN. In that case, we should create a new
916 connection and send a SYNACK in return. */
918 /* First we check if there are any connections avaliable. Unused
919 connections are kept in the same table as used connections, but
920 unused ones have the tcpstate set to CLOSED. Also, connections in
921 TIME_WAIT are kept track of and we'll use the oldest one if no
922 CLOSED connections are found. Thanks to Eddie C. Dost for a very
923 nice algorithm for the TIME_WAIT search. */
925 for(c = 0; c < UIP_CONNS; ++c) {
926 if(uip_conns[c].tcpstateflags == CLOSED) {
927 uip_connr = &uip_conns[c];
930 if(uip_conns[c].tcpstateflags == TIME_WAIT) {
932 uip_conns[c].timer > uip_connr->timer) {
933 uip_connr = &uip_conns[c];
939 /* All connections are used already, we drop packet and hope that
940 the remote end will retransmit the packet at a time when we
941 have more spare connections. */
942 UIP_STAT(++uip_stat.tcp.syndrop);
943 UIP_LOG("tcp: found no unused connections.");
946 uip_conn = uip_connr;
948 /* Fill in the necessary fields for the new connection. */
949 uip_connr->rto = uip_connr->timer = UIP_RTO;
953 uip_connr->lport = BUF->destport;
954 uip_connr->rport = BUF->srcport;
955 uip_connr->ripaddr[0] = BUF->srcipaddr[0];
956 uip_connr->ripaddr[1] = BUF->srcipaddr[1];
957 uip_connr->tcpstateflags = SYN_RCVD;
959 uip_connr->snd_nxt[0] = iss[0];
960 uip_connr->snd_nxt[1] = iss[1];
961 uip_connr->snd_nxt[2] = iss[2];
962 uip_connr->snd_nxt[3] = iss[3];
965 /* rcv_nxt should be the seqno from the incoming packet + 1. */
966 uip_connr->rcv_nxt[3] = BUF->seqno[3];
967 uip_connr->rcv_nxt[2] = BUF->seqno[2];
968 uip_connr->rcv_nxt[1] = BUF->seqno[1];
969 uip_connr->rcv_nxt[0] = BUF->seqno[0];
972 /* Parse the TCP MSS option, if present. */
973 if((BUF->tcpoffset & 0xf0) > 0x50) {
974 for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
975 opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
977 /* End of options. */
979 } else if(opt == 0x01) {
982 } else if(opt == 0x02 &&
983 uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) {
984 /* An MSS option with the right option length. */
985 tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
986 (u16_t)uip_buf[40 + UIP_LLH_LEN + 3 + c];
987 uip_connr->initialmss = uip_connr->mss =
988 tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
990 /* And we are done processing options. */
993 /* All other options have a length field, so that we easily
994 can skip past them. */
995 if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
996 /* If the length field is zero, the options are malformed
997 and we don't process them further. */
1000 c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
1005 /* Our response will be a SYNACK. */
1008 BUF->flags = TCP_ACK;
1011 BUF->flags |= TCP_SYN;
1012 #else /* UIP_ACTIVE_OPEN */
1014 BUF->flags = TCP_SYN | TCP_ACK;
1015 #endif /* UIP_ACTIVE_OPEN */
1017 /* We send out the TCP Maximum Segment Size option with our
1019 BUF->optdata[0] = 2;
1020 BUF->optdata[1] = 4;
1021 BUF->optdata[2] = (UIP_TCP_MSS) / 256;
1022 BUF->optdata[3] = (UIP_TCP_MSS) & 255;
1024 BUF->tcpoffset = 6 << 4;
1027 /* This label will be jumped to if we found an active connection. */
1029 uip_conn = uip_connr;
1032 /* We do a very naive form of TCP reset processing; we just accept
1033 any RST and kill our connection. We should in fact check if the
1034 sequence number of this reset is wihtin our advertised window
1035 before we accept the reset. */
1036 if(BUF->flags & TCP_RST) {
1037 uip_connr->tcpstateflags = CLOSED;
1038 UIP_LOG("tcp: got reset, aborting connection.");
1039 uip_flags = UIP_ABORT;
1043 /* Calculated the length of the data, if the application has sent
1045 c = (BUF->tcpoffset >> 4) << 2;
1046 /* uip_len will contain the length of the actual TCP data. This is
1047 calculated by subtracing the length of the TCP header (in
1048 c) and the length of the IP header (20 bytes). */
1049 uip_len = uip_len - c - 20;
1051 /* First, check if the sequence number of the incoming packet is
1052 what we're expecting next. If not, we send out an ACK with the
1053 correct numbers in. */
1055 (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
1056 BUF->seqno[1] != uip_connr->rcv_nxt[1] ||
1057 BUF->seqno[2] != uip_connr->rcv_nxt[2] ||
1058 BUF->seqno[3] != uip_connr->rcv_nxt[3])) {
1062 /* Next, check if the incoming segment acknowledges any outstanding
1063 data. If so, we update the sequence number, reset the length of
1064 the outstanding data, calculate RTT estimations, and reset the
1065 retransmission timer. */
1066 if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {
1067 uip_add32(uip_connr->snd_nxt, uip_connr->len);
1068 if(BUF->ackno[0] == uip_acc32[0] &&
1069 BUF->ackno[1] == uip_acc32[1] &&
1070 BUF->ackno[2] == uip_acc32[2] &&
1071 BUF->ackno[3] == uip_acc32[3]) {
1072 /* Update sequence number. */
1073 uip_connr->snd_nxt[0] = uip_acc32[0];
1074 uip_connr->snd_nxt[1] = uip_acc32[1];
1075 uip_connr->snd_nxt[2] = uip_acc32[2];
1076 uip_connr->snd_nxt[3] = uip_acc32[3];
1079 /* Do RTT estimation, unless we have done retransmissions. */
1080 if(uip_connr->nrtx == 0) {
1082 m = uip_connr->rto - uip_connr->timer;
1083 /* This is taken directly from VJs original code in his paper */
1084 m = m - (uip_connr->sa >> 3);
1089 m = m - (uip_connr->sv >> 2);
1091 uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;
1094 /* Set the acknowledged flag. */
1095 uip_flags = UIP_ACKDATA;
1096 /* Reset the retransmission timer. */
1097 uip_connr->timer = uip_connr->rto;
1102 /* Do different things depending on in what state the connection is. */
1103 switch(uip_connr->tcpstateflags & TS_MASK) {
1104 /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
1105 implemented, since we force the application to close when the
1106 peer sends a FIN (hence the application goes directly from
1107 ESTABLISHED to LAST_ACK). */
1109 /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
1110 we are waiting for an ACK that acknowledges the data we sent
1111 out the last time. Therefore, we want to have the UIP_ACKDATA
1112 flag set. If so, we enter the ESTABLISHED state. */
1113 if(uip_flags & UIP_ACKDATA) {
1114 uip_connr->tcpstateflags = ESTABLISHED;
1115 uip_flags = UIP_CONNECTED;
1118 uip_flags |= UIP_NEWDATA;
1119 uip_add_rcv_nxt(uip_len);
1128 /* In SYN_SENT, we wait for a SYNACK that is sent in response to
1129 our SYN. The rcv_nxt is set to sequence number in the SYNACK
1130 plus one, and we send an ACK. We move into the ESTABLISHED
1132 if((uip_flags & UIP_ACKDATA) &&
1133 BUF->flags == (TCP_SYN | TCP_ACK)) {
1135 /* Parse the TCP MSS option, if present. */
1136 if((BUF->tcpoffset & 0xf0) > 0x50) {
1137 for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
1138 opt = uip_buf[40 + UIP_LLH_LEN + c];
1140 /* End of options. */
1142 } else if(opt == 0x01) {
1145 } else if(opt == 0x02 &&
1146 uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) {
1147 /* An MSS option with the right option length. */
1148 tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
1149 uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];
1150 uip_connr->initialmss =
1151 uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
1153 /* And we are done processing options. */
1156 /* All other options have a length field, so that we easily
1157 can skip past them. */
1158 if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
1159 /* If the length field is zero, the options are malformed
1160 and we don't process them further. */
1163 c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
1167 uip_connr->tcpstateflags = ESTABLISHED;
1168 uip_connr->rcv_nxt[0] = BUF->seqno[0];
1169 uip_connr->rcv_nxt[1] = BUF->seqno[1];
1170 uip_connr->rcv_nxt[2] = BUF->seqno[2];
1171 uip_connr->rcv_nxt[3] = BUF->seqno[3];
1173 uip_flags = UIP_CONNECTED | UIP_NEWDATA;
1181 #endif /* UIP_ACTIVE_OPEN */
1184 /* In the ESTABLISHED state, we call upon the application to feed
1185 data into the uip_buf. If the UIP_ACKDATA flag is set, the
1186 application should put new data into the buffer, otherwise we are
1187 retransmitting an old segment, and the application should put that
1188 data into the buffer.
1190 If the incoming packet is a FIN, we should close the connection on
1191 this side as well, and we send out a FIN and enter the LAST_ACK
1192 state. We require that there is no outstanding data; otherwise the
1193 sequence numbers will be screwed up. */
1195 if(BUF->flags & TCP_FIN) {
1196 if(uip_outstanding(uip_connr)) {
1199 uip_add_rcv_nxt(1 + uip_len);
1200 uip_flags = UIP_CLOSE;
1202 uip_flags |= UIP_NEWDATA;
1206 uip_connr->tcpstateflags = LAST_ACK;
1207 uip_connr->nrtx = 0;
1209 BUF->flags = TCP_FIN | TCP_ACK;
1210 goto tcp_send_nodata;
1213 /* Check the URG flag. If this is set, the segment carries urgent
1214 data that we must pass to the application. */
1215 if(BUF->flags & TCP_URG) {
1217 uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];
1218 if(uip_urglen > uip_len) {
1219 /* There is more urgent data in the next segment to come. */
1220 uip_urglen = uip_len;
1222 uip_add_rcv_nxt(uip_urglen);
1223 uip_len -= uip_urglen;
1224 uip_urgdata = uip_appdata;
1225 uip_appdata += uip_urglen;
1228 #endif /* UIP_URGDATA > 0 */
1229 uip_appdata += (BUF->urgp[0] << 8) | BUF->urgp[1];
1230 uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];
1234 /* If uip_len > 0 we have TCP data in the packet, and we flag this
1235 by setting the UIP_NEWDATA flag and update the sequence number
1236 we acknowledge. If the application has stopped the dataflow
1237 using uip_stop(), we must not accept any data packets from the
1239 if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
1240 uip_flags |= UIP_NEWDATA;
1241 uip_add_rcv_nxt(uip_len);
1244 /* Check if the available buffer space advertised by the other end
1245 is smaller than the initial MSS for this connection. If so, we
1246 set the current MSS to the window size to ensure that the
1247 application does not send more data than the other end can
1250 If the remote host advertises a zero window, we set the MSS to
1251 the initial MSS so that the application will send an entire MSS
1252 of data. This data will not be acknowledged by the receiver,
1253 and the application will retransmit it. This is called the
1254 "persistent timer" and uses the retransmission mechanim.
1256 tmp16 = ((u16_t)BUF->wnd[0] << 8) + (u16_t)BUF->wnd[1];
1257 if(tmp16 > uip_connr->initialmss ||
1259 tmp16 = uip_connr->initialmss;
1261 uip_connr->mss = tmp16;
1263 /* If this packet constitutes an ACK for outstanding data (flagged
1264 by the UIP_ACKDATA flag, we should call the application since it
1265 might want to send more data. If the incoming packet had data
1266 from the peer (as flagged by the UIP_NEWDATA flag), the
1267 application must also be notified.
1269 When the application is called, the global variable uip_len
1270 contains the length of the incoming data. The application can
1271 access the incoming data through the global pointer
1272 uip_appdata, which usually points 40 bytes into the uip_buf
1275 If the application wishes to send any data, this data should be
1276 put into the uip_appdata and the length of the data should be
1277 put into uip_len. If the application don't have any data to
1278 send, uip_len must be set to 0. */
1279 if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {
1285 if(uip_flags & UIP_ABORT) {
1287 uip_connr->tcpstateflags = CLOSED;
1288 BUF->flags = TCP_RST | TCP_ACK;
1289 goto tcp_send_nodata;
1292 if(uip_flags & UIP_CLOSE) {
1295 uip_connr->tcpstateflags = FIN_WAIT_1;
1296 uip_connr->nrtx = 0;
1297 BUF->flags = TCP_FIN | TCP_ACK;
1298 goto tcp_send_nodata;
1301 /* If uip_slen > 0, the application has data to be sent. */
1304 /* If the connection has acknowledged data, the contents of
1305 the ->len variable should be discarded. */
1306 if((uip_flags & UIP_ACKDATA) != 0) {
1310 /* If the ->len variable is non-zero the connection has
1311 already data in transit and cannot send anymore right
1313 if(uip_connr->len == 0) {
1315 /* The application cannot send more than what is allowed by
1316 the mss (the minumum of the MSS and the available
1318 if(uip_slen > uip_connr->mss) {
1319 uip_slen = uip_connr->mss;
1322 /* Remember how much data we send out now so that we know
1323 when everything has been acknowledged. */
1324 uip_connr->len = uip_slen;
1327 /* If the application already had unacknowledged data, we
1328 make sure that the application does not send (i.e.,
1329 retransmit) out more than it previously sent out. */
1330 uip_slen = uip_connr->len;
1335 uip_connr->nrtx = 0;
1337 uip_appdata = uip_sappdata;
1339 /* If the application has data to be sent, or if the incoming
1340 packet had new data in it, we must send out a packet. */
1341 if(uip_slen > 0 && uip_connr->len > 0) {
1342 /* Add the length of the IP and TCP headers. */
1343 uip_len = uip_connr->len + UIP_TCPIP_HLEN;
1344 /* We always set the ACK flag in response packets. */
1345 BUF->flags = TCP_ACK | TCP_PSH;
1346 /* Send the packet. */
1347 goto tcp_send_noopts;
1349 /* If there is no data to send, just send out a pure ACK if
1350 there is newdata. */
1351 if(uip_flags & UIP_NEWDATA) {
1352 uip_len = UIP_TCPIP_HLEN;
1353 BUF->flags = TCP_ACK;
1354 goto tcp_send_noopts;
1359 /* We can close this connection if the peer has acknowledged our
1360 FIN. This is indicated by the UIP_ACKDATA flag. */
1361 if(uip_flags & UIP_ACKDATA) {
1362 uip_connr->tcpstateflags = CLOSED;
1363 uip_flags = UIP_CLOSE;
1369 /* The application has closed the connection, but the remote host
1370 hasn't closed its end yet. Thus we do nothing but wait for a
1371 FIN from the other side. */
1373 uip_add_rcv_nxt(uip_len);
1375 if(BUF->flags & TCP_FIN) {
1376 if(uip_flags & UIP_ACKDATA) {
1377 uip_connr->tcpstateflags = TIME_WAIT;
1378 uip_connr->timer = 0;
1381 uip_connr->tcpstateflags = CLOSING;
1384 uip_flags = UIP_CLOSE;
1387 } else if(uip_flags & UIP_ACKDATA) {
1388 uip_connr->tcpstateflags = FIN_WAIT_2;
1399 uip_add_rcv_nxt(uip_len);
1401 if(BUF->flags & TCP_FIN) {
1402 uip_connr->tcpstateflags = TIME_WAIT;
1403 uip_connr->timer = 0;
1405 uip_flags = UIP_CLOSE;
1418 if(uip_flags & UIP_ACKDATA) {
1419 uip_connr->tcpstateflags = TIME_WAIT;
1420 uip_connr->timer = 0;
1426 /* We jump here when we are ready to send the packet, and just want
1427 to set the appropriate TCP sequence numbers in the TCP header. */
1429 BUF->flags = TCP_ACK;
1433 BUF->tcpoffset = 5 << 4;
1435 /* We're done with the input processing. We are now ready to send a
1436 reply. Our job is to fill in all the fields of the TCP and IP
1437 headers before calculating the checksum and finally send the
1439 BUF->ackno[0] = uip_connr->rcv_nxt[0];
1440 BUF->ackno[1] = uip_connr->rcv_nxt[1];
1441 BUF->ackno[2] = uip_connr->rcv_nxt[2];
1442 BUF->ackno[3] = uip_connr->rcv_nxt[3];
1444 BUF->seqno[0] = uip_connr->snd_nxt[0];
1445 BUF->seqno[1] = uip_connr->snd_nxt[1];
1446 BUF->seqno[2] = uip_connr->snd_nxt[2];
1447 BUF->seqno[3] = uip_connr->snd_nxt[3];
1449 BUF->proto = UIP_PROTO_TCP;
1451 BUF->srcport = uip_connr->lport;
1452 BUF->destport = uip_connr->rport;
1454 BUF->srcipaddr[0] = uip_hostaddr[0];
1455 BUF->srcipaddr[1] = uip_hostaddr[1];
1456 BUF->destipaddr[0] = uip_connr->ripaddr[0];
1457 BUF->destipaddr[1] = uip_connr->ripaddr[1];
1460 if(uip_connr->tcpstateflags & UIP_STOPPED) {
1461 /* If the connection has issued uip_stop(), we advertise a zero
1462 window so that the remote host will stop sending data. */
1463 BUF->wnd[0] = BUF->wnd[1] = 0;
1465 BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);
1466 BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);
1471 BUF->len[0] = (uip_len >> 8);
1472 BUF->len[1] = (uip_len & 0xff);
1474 /* Calculate TCP checksum. */
1476 BUF->tcpchksum = ~(uip_tcpchksum());
1482 BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
1485 BUF->ipid[0] = ipid >> 8;
1486 BUF->ipid[1] = ipid & 0xff;
1488 /* Calculate IP checksum. */
1490 BUF->ipchksum = ~(uip_ipchksum());
1492 UIP_STAT(++uip_stat.tcp.sent);
1494 UIP_STAT(++uip_stat.ip.sent);
1495 /* Return and let the caller do the actual transmission. */