1 #define DEBUG_PRINTF( ... ) /*printf(__VA_ARGS__)*/
4 * \defgroup uip The uIP TCP/IP stack
7 * uIP is an implementation of the TCP/IP protocol stack intended for
8 * small 8-bit and 16-bit microcontrollers.
10 * uIP provides the necessary protocols for Internet communication,
11 * with a very small code footprint and RAM requirements - the uIP
12 * code size is on the order of a few kilobytes and RAM usage is on
13 * the order of a few hundred bytes.
18 * The uIP TCP/IP stack code.
19 * \author Adam Dunkels <adam@dunkels.com>
23 * Copyright (c) 2001-2003, Adam Dunkels.
24 * All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. The name of the author may not be used to endorse or promote
35 * products derived from this software without specific prior
38 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
39 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
40 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
42 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
44 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
46 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
47 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
48 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 * This file is part of the uIP TCP/IP stack.
52 * $Id: uip.c,v 1.65 2006/06/11 21:46:39 adam Exp $
57 * uIP is a small implementation of the IP, UDP and TCP protocols (as
58 * well as some basic ICMP stuff). The implementation couples the IP,
59 * UDP, TCP and the application layers very tightly. To keep the size
60 * of the compiled code down, this code frequently uses the goto
61 * statement. While it would be possible to break the uip_process()
62 * function into many smaller functions, this would increase the code
63 * size because of the overhead of parameter passing and the fact that
64 * the optimier would not be as efficient.
66 * The principle is that we have a small buffer, called the uip_buf,
67 * in which the device driver puts an incoming packet. The TCP/IP
68 * stack parses the headers in the packet, and calls the
69 * application. If the remote host has sent data to the application,
70 * this data is present in the uip_buf and the application read the
71 * data from there. It is up to the application to put this data into
72 * a byte stream if needed. The application will not be fed with data
73 * that is out of sequence.
75 * If the application whishes to send data to the peer, it should put
76 * its data into the uip_buf. The uip_appdata pointer points to the
77 * first available byte. The TCP/IP stack will calculate the
78 * checksums, and fill in the necessary header fields and finally send
79 * the packet back to the peer.
88 #include "uip-neighbor.h"
89 #endif /* UIP_CONF_IPV6 */
93 /*---------------------------------------------------------------------------*/
95 /* Variable definitions. */
97 /* The IP address of this host. If it is defined to be fixed (by
98 setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set
99 here. Otherwise, the address */
100 #if UIP_FIXEDADDR > 0
101 const uip_ipaddr_t uip_hostaddr
= { HTONS( (UIP_IPADDR0
<< 8) | UIP_IPADDR1
), HTONS( (UIP_IPADDR2
<< 8) | UIP_IPADDR3
) };
102 const uip_ipaddr_t uip_draddr
= { HTONS( (UIP_DRIPADDR0
<< 8) | UIP_DRIPADDR1
), HTONS( (UIP_DRIPADDR2
<< 8) | UIP_DRIPADDR3
) };
103 const uip_ipaddr_t uip_netmask
= { HTONS( (UIP_NETMASK0
<< 8) | UIP_NETMASK1
), HTONS( (UIP_NETMASK2
<< 8) | UIP_NETMASK3
) };
105 uip_ipaddr_t uip_hostaddr
, uip_draddr
, uip_netmask
;
106 #endif /* UIP_FIXEDADDR */
108 static const uip_ipaddr_t all_ones_addr
=
110 { 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff };
111 #else /* UIP_CONF_IPV6 */
115 #endif /* UIP_CONF_IPV6 */
116 static const uip_ipaddr_t all_zeroes_addr
=
118 { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 };
119 #else /* UIP_CONF_IPV6 */
123 #endif /* UIP_CONF_IPV6 */
126 const struct uip_eth_addr uip_ethaddr
= { { UIP_ETHADDR0
, UIP_ETHADDR1
, UIP_ETHADDR2
, UIP_ETHADDR3
, UIP_ETHADDR4
, UIP_ETHADDR5
} };
128 struct uip_eth_addr uip_ethaddr
= { { 0, 0, 0, 0, 0, 0 } };
130 #ifndef UIP_CONF_EXTERNAL_BUFFER
132 #pragma data_alignment = 4
133 u8_t uip_buf
[UIP_BUFSIZE
+ 2]; /* The packet buffer that contains incoming packets. */
135 u8_t uip_buf
[UIP_BUFSIZE
+ 2] ALIGN_STRUCT_END
; /* The packet buffer that contains incoming packets. */
137 #endif /* UIP_CONF_EXTERNAL_BUFFER */
139 void *uip_appdata
; /* The uip_appdata pointer points to
141 void *uip_sappdata
; /* The uip_appdata pointer points to
142 the application data which is to
145 void *uip_urgdata
; /* The uip_urgdata pointer points to
146 urgent data (out-of-band data), if
148 u16_t uip_urglen
, uip_surglen
;
149 #endif /* UIP_URGDATA > 0 */
151 u16_t uip_len
, uip_slen
;
153 /* The uip_len is either 8 or 16 bits,
154 depending on the maximum packet
156 u8_t uip_flags
; /* The uip_flags variable is used for
157 communication between the TCP/IP stack
158 and the application program. */
159 struct uip_conn
*uip_conn
; /* uip_conn always points to the current
162 struct uip_conn uip_conns
[UIP_CONNS
];
164 /* The uip_conns array holds all TCP
166 u16_t uip_listenports
[UIP_LISTENPORTS
];
168 /* The uip_listenports list all currently
171 struct uip_udp_conn
*uip_udp_conn
;
172 struct uip_udp_conn uip_udp_conns
[UIP_UDP_CONNS
];
175 static u16_t ipid
; /* Ths ipid variable is an increasing
176 number that is used for the IP ID
179 void uip_setipid( u16_t id
)
184 static u8_t iss
[4]; /* The iss variable is used for the TCP
185 initial sequence number. */
188 static u16_t lastport
; /* Keeps track of the last port used for
190 #endif /* UIP_ACTIVE_OPEN */
192 /* Temporary variables. */
197 /* Structures and definitions. */
206 #define TCP_OPT_END 0 /* End of TCP options list */
207 #define TCP_OPT_NOOP 1 /* "No-operation" TCP option */
208 #define TCP_OPT_MSS 2 /* Maximum segment size TCP option */
210 #define TCP_OPT_MSS_LEN 4 /* Length of TCP MSS option. */
212 #define ICMP_ECHO_REPLY 0
215 #define ICMP6_ECHO_REPLY 129
216 #define ICMP6_ECHO 128
217 #define ICMP6_NEIGHBOR_SOLICITATION 135
218 #define ICMP6_NEIGHBOR_ADVERTISEMENT 136
220 #define ICMP6_FLAG_S ( 1 << 6 )
221 #define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1
222 #define ICMP6_OPTION_TARGET_LINK_ADDRESS 2
225 #define BUF ( ( struct uip_tcpip_hdr * ) &uip_buf[UIP_LLH_LEN] )
226 #define FBUF ( ( struct uip_tcpip_hdr * ) &uip_reassbuf[0] )
227 #define ICMPBUF ( ( struct uip_icmpip_hdr * ) &uip_buf[UIP_LLH_LEN] )
228 #define UDPBUF ( ( struct uip_udpip_hdr * ) &uip_buf[UIP_LLH_LEN] )
229 #if UIP_STATISTICS == 1
230 struct uip_stats uip_stat
;
231 #define UIP_STAT( s ) s
233 #define UIP_STAT( s )
234 #endif /* UIP_STATISTICS == 1 */
238 void uip_log( char *msg
);
239 #define UIP_LOG( m ) uip_log( m )
242 #endif /* UIP_LOGGING == 1 */
245 void uip_add32( u8_t
*op32
, u16_t op16
)
247 uip_acc32
[3] = op32
[3] + ( op16
& 0xff );
248 uip_acc32
[2] = op32
[2] + ( op16
>> 8 );
249 uip_acc32
[1] = op32
[1];
250 uip_acc32
[0] = op32
[0];
252 if( uip_acc32
[2] < (op16
>> 8) )
255 if( uip_acc32
[1] == 0 )
261 if( uip_acc32
[3] < (op16
& 0xff) )
264 if( uip_acc32
[2] == 0 )
267 if( uip_acc32
[1] == 0 )
275 #endif /* UIP_ARCH_ADD32 */
279 /*---------------------------------------------------------------------------*/
280 static u16_t
chksum( u16_t sum
, const u8_t
*data
, u16_t len
)
284 const u8_t
*last_byte
;
287 last_byte
= data
+ len
- 1;
289 while( dataptr
< last_byte
)
290 { /* At least two more bytes */
291 t
= ( dataptr
[0] << 8 ) + dataptr
[1];
301 if( dataptr
== last_byte
)
303 t
= ( dataptr
[0] << 8 ) + 0;
311 /* Return sum in host byte order. */
315 /*---------------------------------------------------------------------------*/
316 u16_t
uip_chksum( u16_t
*data
, u16_t len
)
318 return htons( chksum(0, ( u8_t
* ) data
, len
) );
321 /*---------------------------------------------------------------------------*/
322 #ifndef UIP_ARCH_IPCHKSUM
323 u16_t
uip_ipchksum( void )
327 sum
= chksum( 0, &uip_buf
[UIP_LLH_LEN
], UIP_IPH_LEN
);
328 DEBUG_PRINTF( "uip_ipchksum: sum 0x%04x\n", sum
);
329 return( sum
== 0 ) ? 0xffff : htons( sum
);
334 /*---------------------------------------------------------------------------*/
335 static u16_t
upper_layer_chksum( u8_t proto
)
337 u16_t upper_layer_len
;
341 upper_layer_len
= ( ((u16_t
) (BUF
->len
[0]) << 8) + BUF
->len
[1] );
342 #else /* UIP_CONF_IPV6 */
343 upper_layer_len
= ( ((u16_t
) (BUF
->len
[0]) << 8) + BUF
->len
[1] ) - UIP_IPH_LEN
;
344 #endif /* UIP_CONF_IPV6 */
346 /* First sum pseudoheader. */
348 /* IP protocol and length fields. This addition cannot carry. */
349 sum
= upper_layer_len
+ proto
;
351 /* Sum IP source and destination addresses. */
352 sum
= chksum( sum
, ( u8_t
* ) &BUF
->srcipaddr
[0], 2 * sizeof(uip_ipaddr_t
) );
354 /* Sum TCP header and data. */
355 sum
= chksum( sum
, &uip_buf
[UIP_IPH_LEN
+ UIP_LLH_LEN
], upper_layer_len
);
357 return( sum
== 0 ) ? 0xffff : htons( sum
);
360 /*---------------------------------------------------------------------------*/
362 u16_t
uip_icmp6chksum( void )
364 return upper_layer_chksum( UIP_PROTO_ICMP6
);
367 #endif /* UIP_CONF_IPV6 */
369 /*---------------------------------------------------------------------------*/
370 u16_t
uip_tcpchksum( void )
372 return upper_layer_chksum( UIP_PROTO_TCP
);
375 /*---------------------------------------------------------------------------*/
376 #if UIP_UDP_CHECKSUMS
377 u16_t
uip_udpchksum( void )
379 return upper_layer_chksum( UIP_PROTO_UDP
);
382 #endif /* UIP_UDP_CHECKSUMS */
383 #endif /* UIP_ARCH_CHKSUM */
385 /*---------------------------------------------------------------------------*/
386 void uip_init( void )
388 for( c
= 0; c
< UIP_LISTENPORTS
; ++c
)
390 uip_listenports
[c
] = 0;
393 for( c
= 0; c
< UIP_CONNS
; ++c
)
395 uip_conns
[c
].tcpstateflags
= UIP_CLOSED
;
400 #endif /* UIP_ACTIVE_OPEN */
403 for( c
= 0; c
< UIP_UDP_CONNS
; ++c
)
405 uip_udp_conns
[c
].lport
= 0;
410 /* IPv4 initialization. */
411 #if UIP_FIXEDADDR == 0
413 /* uip_hostaddr[0] = uip_hostaddr[1] = 0;*/
414 #endif /* UIP_FIXEDADDR */
417 /*---------------------------------------------------------------------------*/
419 struct uip_conn
*uip_connect( uip_ipaddr_t
*ripaddr
, u16_t rport
)
421 register struct uip_conn
*conn
, *cconn
;
423 /* Find an unused local port. */
427 if( lastport
>= 32000 )
432 /* Check if this port is already in use, and if so try to find
434 for( c
= 0; c
< UIP_CONNS
; ++c
)
436 conn
= &uip_conns
[c
];
437 if( conn
->tcpstateflags
!= UIP_CLOSED
&& conn
->lport
== htons(lastport
) )
444 for( c
= 0; c
< UIP_CONNS
; ++c
)
446 cconn
= &uip_conns
[c
];
447 if( cconn
->tcpstateflags
== UIP_CLOSED
)
453 if( cconn
->tcpstateflags
== UIP_TIME_WAIT
)
455 if( conn
== 0 || cconn
->timer
> conn
->timer
)
467 conn
->tcpstateflags
= UIP_SYN_SENT
;
469 conn
->snd_nxt
[0] = iss
[0];
470 conn
->snd_nxt
[1] = iss
[1];
471 conn
->snd_nxt
[2] = iss
[2];
472 conn
->snd_nxt
[3] = iss
[3];
474 conn
->initialmss
= conn
->mss
= UIP_TCP_MSS
;
476 conn
->len
= 1; /* TCP length of the SYN is one. */
478 conn
->timer
= 1; /* Send the SYN next time around. */
481 conn
->sv
= 16; /* Initial value of the RTT variance. */
482 conn
->lport
= htons( lastport
);
484 uip_ipaddr_copy( &conn
->ripaddr
, ripaddr
);
489 #endif /* UIP_ACTIVE_OPEN */
491 /*---------------------------------------------------------------------------*/
493 struct uip_udp_conn
*uip_udp_new( uip_ipaddr_t
*ripaddr
, u16_t rport
)
495 register struct uip_udp_conn
*conn
;
497 /* Find an unused local port. */
501 if( lastport
>= 32000 )
506 for( c
= 0; c
< UIP_UDP_CONNS
; ++c
)
508 if( uip_udp_conns
[c
].lport
== htons(lastport
) )
515 for( c
= 0; c
< UIP_UDP_CONNS
; ++c
)
517 if( uip_udp_conns
[c
].lport
== 0 )
519 conn
= &uip_udp_conns
[c
];
529 conn
->lport
= HTONS( lastport
);
531 if( ripaddr
== NULL
)
533 memset( conn
->ripaddr
, 0, sizeof(uip_ipaddr_t
) );
537 uip_ipaddr_copy( &conn
->ripaddr
, ripaddr
);
547 /*---------------------------------------------------------------------------*/
548 void uip_unlisten( u16_t port
)
550 for( c
= 0; c
< UIP_LISTENPORTS
; ++c
)
552 if( uip_listenports
[c
] == port
)
554 uip_listenports
[c
] = 0;
560 /*---------------------------------------------------------------------------*/
561 void uip_listen( u16_t port
)
563 for( c
= 0; c
< UIP_LISTENPORTS
; ++c
)
565 if( uip_listenports
[c
] == 0 )
567 uip_listenports
[c
] = port
;
573 /*---------------------------------------------------------------------------*/
575 /* XXX: IP fragment reassembly: not well-tested. */
576 #if UIP_REASSEMBLY && !UIP_CONF_IPV6
577 #define UIP_REASS_BUFSIZE ( UIP_BUFSIZE - UIP_LLH_LEN )
578 static u8_t uip_reassbuf
[UIP_REASS_BUFSIZE
];
579 static u8_t uip_reassbitmap
[UIP_REASS_BUFSIZE
/ ( 8 * 8 )];
580 static const u8_t bitmap_bits
[8] = { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
581 static u16_t uip_reasslen
;
582 static u8_t uip_reassflags
;
583 #define UIP_REASS_FLAG_LASTFRAG 0x01
584 static u8_t uip_reasstmr
;
588 static u8_t
uip_reass( void )
593 /* If ip_reasstmr is zero, no packet is present in the buffer, so we
594 write the IP header of the fragment into the reassembly
595 buffer. The timer is updated with the maximum age. */
596 if( uip_reasstmr
== 0 )
598 memcpy( uip_reassbuf
, &BUF
->vhl
, UIP_IPH_LEN
);
599 uip_reasstmr
= UIP_REASS_MAXAGE
;
602 /* Clear the bitmap. */
603 memset( uip_reassbitmap
, 0, sizeof(uip_reassbitmap
) );
606 /* Check if the incoming fragment matches the one currently present
607 in the reasembly buffer. If so, we proceed with copying the
608 fragment into the buffer. */
611 BUF
->srcipaddr
[0] == FBUF
->srcipaddr
[0] &&
612 BUF
->srcipaddr
[1] == FBUF
->srcipaddr
[1] &&
613 BUF
->destipaddr
[0] == FBUF
->destipaddr
[0] &&
614 BUF
->destipaddr
[1] == FBUF
->destipaddr
[1] &&
615 BUF
->ipid
[0] == FBUF
->ipid
[0] &&
616 BUF
->ipid
[1] == FBUF
->ipid
[1]
619 len
= ( BUF
->len
[0] << 8 ) + BUF
->len
[1] - ( BUF
->vhl
& 0x0f ) * 4;
620 offset
= ( ((BUF
->ipoffset
[0] & 0x3f) << 8) + BUF
->ipoffset
[1] ) * 8;
622 /* If the offset or the offset + fragment length overflows the
623 reassembly buffer, we discard the entire packet. */
624 if( offset
> UIP_REASS_BUFSIZE
|| offset
+ len
> UIP_REASS_BUFSIZE
)
630 /* Copy the fragment into the reassembly buffer, at the right
632 memcpy( &uip_reassbuf
[UIP_IPH_LEN
+ offset
], ( char * ) BUF
+ ( int ) ((BUF
->vhl
& 0x0f) * 4), len
);
634 /* Update the bitmap. */
635 if( offset
/ (8 * 8) == (offset
+ len
) / (8 * 8) )
637 /* If the two endpoints are in the same byte, we only update
639 uip_reassbitmap
[offset
/ ( 8 * 8 )] |= bitmap_bits
[( offset
/ 8 ) & 7] &~bitmap_bits
[( (offset
+ len
) / 8 ) & 7];
643 /* If the two endpoints are in different bytes, we update the
644 bytes in the endpoints and fill the stuff inbetween with
646 uip_reassbitmap
[offset
/ ( 8 * 8 )] |= bitmap_bits
[( offset
/ 8 ) & 7];
647 for( i
= 1 + offset
/ (8 * 8); i
< (offset
+ len
) / (8 * 8); ++i
)
649 uip_reassbitmap
[i
] = 0xff;
652 uip_reassbitmap
[( offset
+ len
) / ( 8 * 8 )] |= ~bitmap_bits
[( (offset
+ len
) / 8 ) & 7];
655 /* If this fragment has the More Fragments flag set to zero, we
656 know that this is the last fragment, so we can calculate the
657 size of the entire packet. We also set the
658 IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
659 the final fragment. */
660 if( (BUF
->ipoffset
[0] & IP_MF
) == 0 )
662 uip_reassflags
|= UIP_REASS_FLAG_LASTFRAG
;
663 uip_reasslen
= offset
+ len
;
666 /* Finally, we check if we have a full packet in the buffer. We do
667 this by checking if we have the last fragment and if all bits
668 in the bitmap are set. */
669 if( uip_reassflags
& UIP_REASS_FLAG_LASTFRAG
)
671 /* Check all bytes up to and including all but the last byte in
673 for( i
= 0; i
< uip_reasslen
/ (8 * 8) - 1; ++i
)
675 if( uip_reassbitmap
[i
] != 0xff )
681 /* Check the last byte in the bitmap. It should contain just the
682 right amount of bits. */
683 if( uip_reassbitmap
[uip_reasslen
/ (8 * 8)] != (u8_t
)~bitmap_bits
[uip_reasslen
/ 8 & 7] )
688 /* If we have come this far, we have a full packet in the
689 buffer, so we allocate a pbuf and copy the packet into it. We
690 also reset the timer. */
692 memcpy( BUF
, FBUF
, uip_reasslen
);
694 /* Pretend to be a "normal" (i.e., not fragmented) IP packet
696 BUF
->ipoffset
[0] = BUF
->ipoffset
[1] = 0;
697 BUF
->len
[0] = uip_reasslen
>> 8;
698 BUF
->len
[1] = uip_reasslen
& 0xff;
700 BUF
->ipchksum
= ~( uip_ipchksum() );
710 #endif /* UIP_REASSEMBLY */
712 /*---------------------------------------------------------------------------*/
713 static void uip_add_rcv_nxt( u16_t n
)
715 uip_add32( uip_conn
->rcv_nxt
, n
);
716 uip_conn
->rcv_nxt
[0] = uip_acc32
[0];
717 uip_conn
->rcv_nxt
[1] = uip_acc32
[1];
718 uip_conn
->rcv_nxt
[2] = uip_acc32
[2];
719 uip_conn
->rcv_nxt
[3] = uip_acc32
[3];
722 /*---------------------------------------------------------------------------*/
723 void uip_process( u8_t flag
)
725 register struct uip_conn
*uip_connr
= uip_conn
;
728 if( flag
== UIP_UDP_SEND_CONN
)
735 uip_sappdata
= uip_appdata
= &uip_buf
[UIP_IPTCPH_LEN
+ UIP_LLH_LEN
];
737 /* Check if we were invoked because of a poll request for a
738 particular connection. */
739 if( flag
== UIP_POLL_REQUEST
)
741 if( (uip_connr
->tcpstateflags
& UIP_TS_MASK
) == UIP_ESTABLISHED
&& !uip_outstanding(uip_connr
) )
743 uip_flags
= UIP_POLL
;
750 /* Check if we were invoked because of the perodic timer fireing. */
752 else if( flag
== UIP_TIMER
)
755 if( uip_reasstmr
!= 0 )
760 #endif /* UIP_REASSEMBLY */
762 /* Increase the initial sequence number. */
774 /* Reset the length variables. */
778 /* Check if the connection is in a state in which we simply wait
779 for the connection to time out. If so, we increase the
780 connection's timer and remove the connection if it times
782 if( uip_connr
->tcpstateflags
== UIP_TIME_WAIT
|| uip_connr
->tcpstateflags
== UIP_FIN_WAIT_2
)
784 ++( uip_connr
->timer
);
785 if( uip_connr
->timer
== UIP_TIME_WAIT_TIMEOUT
)
787 uip_connr
->tcpstateflags
= UIP_CLOSED
;
790 else if( uip_connr
->tcpstateflags
!= UIP_CLOSED
)
792 /* If the connection has outstanding data, we increase the
793 connection's timer and see if it has reached the RTO value
794 in which case we retransmit. */
795 if( uip_outstanding(uip_connr
) )
797 uip_connr
->timer
= uip_connr
->timer
- 1;
798 if( uip_connr
->timer
== 0 )
802 uip_connr
->nrtx
== UIP_MAXRTX
||
804 (uip_connr
->tcpstateflags
== UIP_SYN_SENT
|| uip_connr
->tcpstateflags
== UIP_SYN_RCVD
) &&
805 uip_connr
->nrtx
== UIP_MAXSYNRTX
809 uip_connr
->tcpstateflags
= UIP_CLOSED
;
811 /* We call UIP_APPCALL() with uip_flags set to
812 UIP_TIMEDOUT to inform the application that the
813 connection has timed out. */
814 uip_flags
= UIP_TIMEDOUT
;
817 /* We also send a reset packet to the remote host. */
818 BUF
->flags
= TCP_RST
| TCP_ACK
;
819 goto tcp_send_nodata
;
822 /* Exponential backoff. */
823 uip_connr
->timer
= UIP_RTO
<< ( uip_connr
->nrtx
> 4 ? 4 : uip_connr
->nrtx
);
824 ++( uip_connr
->nrtx
);
826 /* Ok, so we need to retransmit. We do this differently
827 depending on which state we are in. In ESTABLISHED, we
828 call upon the application so that it may prepare the
829 data for the retransmit. In SYN_RCVD, we resend the
830 SYNACK that we sent earlier and in LAST_ACK we have to
831 retransmit our FINACK. */
832 UIP_STAT( ++uip_stat
.tcp
.rexmit
);
833 switch( uip_connr
->tcpstateflags
& UIP_TS_MASK
)
836 /* In the SYN_RCVD state, we should retransmit our
838 goto tcp_send_synack
;
843 /* In the SYN_SENT state, we retransmit out SYN. */
846 #endif /* UIP_ACTIVE_OPEN */
848 case UIP_ESTABLISHED
:
849 /* In the ESTABLISHED state, we call upon the application
850 to do the actual retransmit after which we jump into
851 the code for sending out the packet (the apprexmit
853 uip_flags
= UIP_REXMIT
;
860 /* In all these states we should retransmit a FINACK. */
861 goto tcp_send_finack
;
865 else if( (uip_connr
->tcpstateflags
& UIP_TS_MASK
) == UIP_ESTABLISHED
)
867 /* If there was no need for a retransmission, we poll the
868 application for new data. */
869 uip_flags
= UIP_POLL
;
879 if( flag
== UIP_UDP_TIMER
)
881 if( uip_udp_conn
->lport
!= 0 )
884 uip_sappdata
= uip_appdata
= &uip_buf
[UIP_LLH_LEN
+ UIP_IPUDPH_LEN
];
885 uip_len
= uip_slen
= 0;
886 uip_flags
= UIP_POLL
;
898 /* This is where the input processing starts. */
899 UIP_STAT( ++uip_stat
.ip
.recv
);
901 /* Start of IP input header processing code. */
904 /* Check validity of the IP header. */
905 if( (BUF
->vtc
& 0xf0) != 0x60 )
906 { /* IP version and header length. */
907 UIP_STAT( ++uip_stat
.ip
.drop
);
908 UIP_STAT( ++uip_stat
.ip
.vhlerr
);
909 UIP_LOG( "ipv6: invalid version." );
913 #else /* UIP_CONF_IPV6 */
915 /* Check validity of the IP header. */
916 if( BUF
->vhl
!= 0x45 )
917 { /* IP version and header length. */
918 UIP_STAT( ++uip_stat
.ip
.drop
);
919 UIP_STAT( ++uip_stat
.ip
.vhlerr
);
920 UIP_LOG( "ip: invalid version or header length." );
924 #endif /* UIP_CONF_IPV6 */
926 /* Check the size of the packet. If the size reported to us in
927 uip_len is smaller the size reported in the IP header, we assume
928 that the packet has been corrupted in transit. If the size of
929 uip_len is larger than the size reported in the IP packet header,
930 the packet has been padded and we set uip_len to the correct
932 if( (BUF
->len
[0] << 8) + BUF
->len
[1] <= uip_len
)
934 uip_len
= ( BUF
->len
[0] << 8 ) + BUF
->len
[1];
936 uip_len
+= 40; /* The length reported in the IPv6 header is the
937 length of the payload that follows the
938 header. However, uIP uses the uip_len variable
939 for holding the size of the entire packet,
940 including the IP header. For IPv4 this is not a
941 problem as the length field in the IPv4 header
942 contains the length of the entire packet. But
943 for IPv6 we need to add the size of the IPv6
944 header (40 bytes). */
945 #endif /* UIP_CONF_IPV6 */
949 UIP_LOG( "ip: packet shorter than reported in IP header." );
955 /* Check the fragment flag. */
956 if( (BUF
->ipoffset
[0] & 0x3f) != 0 || BUF
->ipoffset
[1] != 0 )
959 uip_len
= uip_reass();
965 #else /* UIP_REASSEMBLY */
966 UIP_STAT( ++uip_stat
.ip
.drop
);
967 UIP_STAT( ++uip_stat
.ip
.fragerr
);
968 UIP_LOG( "ip: fragment dropped." );
970 #endif /* UIP_REASSEMBLY */
973 #endif /* UIP_CONF_IPV6 */
975 if( uip_ipaddr_cmp(uip_hostaddr
, all_zeroes_addr
) )
977 /* If we are configured to use ping IP address configuration and
978 hasn't been assigned an IP address yet, we accept all ICMP
980 #if UIP_PINGADDRCONF && !UIP_CONF_IPV6
981 if( BUF
->proto
== UIP_PROTO_ICMP
)
983 UIP_LOG( "ip: possible ping config packet received." );
988 UIP_LOG( "ip: packet dropped since no address assigned." );
992 #endif /* UIP_PINGADDRCONF */
996 /* If IP broadcast support is configured, we check for a broadcast
997 UDP packet, which may be destined to us. */
999 DEBUG_PRINTF( "UDP IP checksum 0x%04x\n", uip_ipchksum() );
1000 if( BUF
->proto
== UIP_PROTO_UDP
&& uip_ipaddr_cmp(BUF
->destipaddr
, all_ones_addr
) /*&&
1001 uip_ipchksum() == 0xffff*/ )
1006 #endif /* UIP_BROADCAST */
1008 /* Check if the packet is destined for our IP address. */
1010 if( !uip_ipaddr_cmp(BUF
->destipaddr
, uip_hostaddr
) )
1012 UIP_STAT( ++uip_stat
.ip
.drop
);
1016 #else /* UIP_CONF_IPV6 */
1018 /* For IPv6, packet reception is a little trickier as we need to
1019 make sure that we listen to certain multicast addresses (all
1020 hosts multicast address, and the solicited-node multicast
1021 address) as well. However, we will cheat here and accept all
1022 multicast packets that are sent to the ff02::/16 addresses. */
1023 if( !uip_ipaddr_cmp(BUF
->destipaddr
, uip_hostaddr
) && BUF
->destipaddr
[0] != HTONS(0xff02) )
1025 UIP_STAT( ++uip_stat
.ip
.drop
);
1029 #endif /* UIP_CONF_IPV6 */
1033 if( uip_ipchksum() != 0xffff )
1034 { /* Compute and check the IP header
1036 UIP_STAT( ++uip_stat
.ip
.drop
);
1037 UIP_STAT( ++uip_stat
.ip
.chkerr
);
1038 UIP_LOG( "ip: bad checksum." );
1042 #endif /* UIP_CONF_IPV6 */
1044 if( BUF
->proto
== UIP_PROTO_TCP
)
1045 { /* Check for TCP packet. If so,
1046 proceed with TCP input
1052 if( BUF
->proto
== UIP_PROTO_UDP
)
1057 #endif /* UIP_UDP */
1061 /* ICMPv4 processing code follows. */
1062 if( BUF
->proto
!= UIP_PROTO_ICMP
)
1063 { /* We only allow ICMP packets from
1065 UIP_STAT( ++uip_stat
.ip
.drop
);
1066 UIP_STAT( ++uip_stat
.ip
.protoerr
);
1067 UIP_LOG( "ip: neither tcp nor icmp." );
1071 #if UIP_PINGADDRCONF
1073 #endif /* UIP_PINGADDRCONF */
1074 UIP_STAT( ++uip_stat
.icmp
.recv
);
1076 /* ICMP echo (i.e., ping) processing. This is simple, we only change
1077 the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
1078 checksum before we return the packet. */
1079 if( ICMPBUF
->type
!= ICMP_ECHO
)
1081 UIP_STAT( ++uip_stat
.icmp
.drop
);
1082 UIP_STAT( ++uip_stat
.icmp
.typeerr
);
1083 UIP_LOG( "icmp: not icmp echo." );
1087 /* If we are configured to use ping IP address assignment, we use
1088 the destination IP address of this ping packet and assign it to
1090 #if UIP_PINGADDRCONF
1091 if( (uip_hostaddr
[0] | uip_hostaddr
[1]) == 0 )
1093 uip_hostaddr
[0] = BUF
->destipaddr
[0];
1094 uip_hostaddr
[1] = BUF
->destipaddr
[1];
1097 #endif /* UIP_PINGADDRCONF */
1099 ICMPBUF
->type
= ICMP_ECHO_REPLY
;
1101 if( ICMPBUF
->icmpchksum
>= HTONS(0xffff - (ICMP_ECHO
<< 8)) )
1103 ICMPBUF
->icmpchksum
+= HTONS( ICMP_ECHO
<< 8 ) + 1;
1107 ICMPBUF
->icmpchksum
+= HTONS( ICMP_ECHO
<< 8 );
1110 /* Swap IP addresses. */
1111 uip_ipaddr_copy( BUF
->destipaddr
, BUF
->srcipaddr
);
1112 uip_ipaddr_copy( BUF
->srcipaddr
, uip_hostaddr
);
1114 UIP_STAT( ++uip_stat
.icmp
.sent
);
1117 /* End of IPv4 input header processing code. */
1118 #else /* !UIP_CONF_IPV6 */
1120 /* This is IPv6 ICMPv6 processing code. */
1121 DEBUG_PRINTF( "icmp6_input: length %d\n", uip_len
);
1123 if( BUF
->proto
!= UIP_PROTO_ICMP6
)
1124 { /* We only allow ICMPv6 packets from
1126 UIP_STAT( ++uip_stat
.ip
.drop
);
1127 UIP_STAT( ++uip_stat
.ip
.protoerr
);
1128 UIP_LOG( "ip: neither tcp nor icmp6." );
1132 UIP_STAT( ++uip_stat
.icmp
.recv
);
1134 /* If we get a neighbor solicitation for our address we should send
1135 a neighbor advertisement message back. */
1136 if( ICMPBUF
->type
== ICMP6_NEIGHBOR_SOLICITATION
)
1138 if( uip_ipaddr_cmp(ICMPBUF
->icmp6data
, uip_hostaddr
) )
1140 if( ICMPBUF
->options
[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS
)
1142 /* Save the sender's address in our neighbor list. */
1143 uip_neighbor_add( ICMPBUF
->srcipaddr
, &(ICMPBUF
->options
[2]) );
1146 /* We should now send a neighbor advertisement back to where the
1147 neighbor solicication came from. */
1148 ICMPBUF
->type
= ICMP6_NEIGHBOR_ADVERTISEMENT
;
1149 ICMPBUF
->flags
= ICMP6_FLAG_S
; /* Solicited flag. */
1151 ICMPBUF
->reserved1
= ICMPBUF
->reserved2
= ICMPBUF
->reserved3
= 0;
1153 uip_ipaddr_copy( ICMPBUF
->destipaddr
, ICMPBUF
->srcipaddr
);
1154 uip_ipaddr_copy( ICMPBUF
->srcipaddr
, uip_hostaddr
);
1155 ICMPBUF
->options
[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS
;
1156 ICMPBUF
->options
[1] = 1; /* Options length, 1 = 8 bytes. */
1157 memcpy( &(ICMPBUF
->options
[2]), &uip_ethaddr
, sizeof(uip_ethaddr
) );
1158 ICMPBUF
->icmpchksum
= 0;
1159 ICMPBUF
->icmpchksum
= ~uip_icmp6chksum();
1165 else if( ICMPBUF
->type
== ICMP6_ECHO
)
1167 /* ICMP echo (i.e., ping) processing. This is simple, we only
1168 change the ICMP type from ECHO to ECHO_REPLY and update the
1169 ICMP checksum before we return the packet. */
1170 ICMPBUF
->type
= ICMP6_ECHO_REPLY
;
1172 uip_ipaddr_copy( BUF
->destipaddr
, BUF
->srcipaddr
);
1173 uip_ipaddr_copy( BUF
->srcipaddr
, uip_hostaddr
);
1174 ICMPBUF
->icmpchksum
= 0;
1175 ICMPBUF
->icmpchksum
= ~uip_icmp6chksum();
1177 UIP_STAT( ++uip_stat
.icmp
.sent
);
1182 DEBUG_PRINTF( "Unknown icmp6 message type %d\n", ICMPBUF
->type
);
1183 UIP_STAT( ++uip_stat
.icmp
.drop
);
1184 UIP_STAT( ++uip_stat
.icmp
.typeerr
);
1185 UIP_LOG( "icmp: unknown ICMP message." );
1189 /* End of IPv6 ICMP processing. */
1190 #endif /* !UIP_CONF_IPV6 */
1194 /* UDP input processing. */
1196 /* UDP processing is really just a hack. We don't do anything to the
1197 UDP/IP headers, but let the UDP application do all the hard
1198 work. If the application sets uip_slen, it has a packet to
1200 #if UIP_UDP_CHECKSUMS
1201 uip_len
= uip_len
- UIP_IPUDPH_LEN
;
1202 uip_appdata
= &uip_buf
[UIP_LLH_LEN
+ UIP_IPUDPH_LEN
];
1203 if( UDPBUF
->udpchksum
!= 0 && uip_udpchksum() != 0xffff )
1205 UIP_STAT( ++uip_stat
.udp
.drop
);
1206 UIP_STAT( ++uip_stat
.udp
.chkerr
);
1207 UIP_LOG( "udp: bad checksum." );
1211 #else /* UIP_UDP_CHECKSUMS */
1212 uip_len
= uip_len
- UIP_IPUDPH_LEN
;
1213 #endif /* UIP_UDP_CHECKSUMS */
1215 /* Demultiplex this UDP packet between the UDP "connections". */
1216 for( uip_udp_conn
= &uip_udp_conns
[0]; uip_udp_conn
< &uip_udp_conns
[UIP_UDP_CONNS
]; ++uip_udp_conn
)
1218 /* If the local UDP port is non-zero, the connection is considered
1219 to be used. If so, the local port number is checked against the
1220 destination port number in the received packet. If the two port
1221 numbers match, the remote port number is checked if the
1222 connection is bound to a remote port. Finally, if the
1223 connection is bound to a remote IP address, the source IP
1224 address of the packet is checked. */
1227 uip_udp_conn
->lport
!= 0 &&
1228 UDPBUF
->destport
== uip_udp_conn
->lport
&&
1229 (uip_udp_conn
->rport
== 0 || UDPBUF
->srcport
== uip_udp_conn
->rport
) &&
1231 uip_ipaddr_cmp(uip_udp_conn
->ripaddr
, all_zeroes_addr
) ||
1232 uip_ipaddr_cmp(uip_udp_conn
->ripaddr
, all_ones_addr
) ||
1233 uip_ipaddr_cmp(BUF
->srcipaddr
, uip_udp_conn
->ripaddr
)
1241 UIP_LOG( "udp: no matching connection found" );
1245 UIP_STAT( ++uip_stat
.udp
.recv
);
1247 uip_flags
= UIP_NEWDATA
;
1248 uip_sappdata
= uip_appdata
= &uip_buf
[UIP_LLH_LEN
+ UIP_IPUDPH_LEN
];
1257 uip_len
= uip_slen
+ UIP_IPUDPH_LEN
;
1261 /* For IPv6, the IP length field does not include the IPv6 IP header
1263 BUF
->len
[0] = ( (uip_len
- UIP_IPH_LEN
) >> 8 );
1264 BUF
->len
[1] = ( (uip_len
- UIP_IPH_LEN
) & 0xff );
1265 #else /* UIP_CONF_IPV6 */
1266 BUF
->len
[0] = ( uip_len
>> 8 );
1267 BUF
->len
[1] = ( uip_len
& 0xff );
1268 #endif /* UIP_CONF_IPV6 */
1270 BUF
->ttl
= uip_udp_conn
->ttl
;
1271 BUF
->proto
= UIP_PROTO_UDP
;
1273 UDPBUF
->udplen
= HTONS( uip_slen
+ UIP_UDPH_LEN
);
1274 UDPBUF
->udpchksum
= 0;
1276 BUF
->srcport
= uip_udp_conn
->lport
;
1277 BUF
->destport
= uip_udp_conn
->rport
;
1279 uip_ipaddr_copy( BUF
->srcipaddr
, uip_hostaddr
);
1280 uip_ipaddr_copy( BUF
->destipaddr
, uip_udp_conn
->ripaddr
);
1282 uip_appdata
= &uip_buf
[UIP_LLH_LEN
+ UIP_IPTCPH_LEN
];
1284 #if UIP_UDP_CHECKSUMS
1286 /* Calculate UDP checksum. */
1287 UDPBUF
->udpchksum
= ~( uip_udpchksum() );
1288 if( UDPBUF
->udpchksum
== 0 )
1290 UDPBUF
->udpchksum
= 0xffff;
1293 #endif /* UIP_UDP_CHECKSUMS */
1294 UIP_STAT( ++uip_stat
.udp
.sent
);
1296 #endif /* UIP_UDP */
1298 /* TCP input processing. */
1299 tcp_input
: UIP_STAT( ++uip_stat
.tcp
.recv
);
1301 /* Start of TCP input header processing code. */
1302 if( uip_tcpchksum() != 0xffff )
1303 { /* Compute and check the TCP
1305 UIP_STAT( ++uip_stat
.tcp
.drop
);
1306 UIP_STAT( ++uip_stat
.tcp
.chkerr
);
1307 UIP_LOG( "tcp: bad checksum." );
1311 /* Demultiplex this segment. */
1313 /* First check any active connections. */
1314 for( uip_connr
= &uip_conns
[0]; uip_connr
<= &uip_conns
[UIP_CONNS
- 1]; ++uip_connr
)
1318 uip_connr
->tcpstateflags
!= UIP_CLOSED
&&
1319 BUF
->destport
== uip_connr
->lport
&&
1320 BUF
->srcport
== uip_connr
->rport
&&
1321 uip_ipaddr_cmp(BUF
->srcipaddr
, uip_connr
->ripaddr
)
1328 /* If we didn't find and active connection that expected the packet,
1329 either this packet is an old duplicate, or this is a SYN packet
1330 destined for a connection in LISTEN. If the SYN flag isn't set,
1331 it is an old packet and we send a RST. */
1332 if( (BUF
->flags
& TCP_CTL
) != TCP_SYN
)
1337 tmp16
= BUF
->destport
;
1339 /* Next, check listening connections. */
1340 for( c
= 0; c
< UIP_LISTENPORTS
; ++c
)
1342 if( tmp16
== uip_listenports
[c
] )
1348 /* No matching connection found, so we send a RST packet. */
1349 UIP_STAT( ++uip_stat
.tcp
.synrst
);
1351 /* We do not send resets in response to resets. */
1352 if( BUF
->flags
& TCP_RST
)
1357 UIP_STAT( ++uip_stat
.tcp
.rst
);
1359 BUF
->flags
= TCP_RST
| TCP_ACK
;
1360 uip_len
= UIP_IPTCPH_LEN
;
1361 BUF
->tcpoffset
= 5 << 4;
1363 /* Flip the seqno and ackno fields in the TCP header. */
1365 BUF
->seqno
[3] = BUF
->ackno
[3];
1369 BUF
->seqno
[2] = BUF
->ackno
[2];
1373 BUF
->seqno
[1] = BUF
->ackno
[1];
1377 BUF
->seqno
[0] = BUF
->ackno
[0];
1380 /* We also have to increase the sequence number we are
1381 acknowledging. If the least significant byte overflowed, we need
1382 to propagate the carry to the other bytes as well. */
1383 if( ++BUF
->ackno
[3] == 0 )
1385 if( ++BUF
->ackno
[2] == 0 )
1387 if( ++BUF
->ackno
[1] == 0 )
1394 /* Swap port numbers. */
1395 tmp16
= BUF
->srcport
;
1396 BUF
->srcport
= BUF
->destport
;
1397 BUF
->destport
= tmp16
;
1399 /* Swap IP addresses. */
1400 uip_ipaddr_copy( BUF
->destipaddr
, BUF
->srcipaddr
);
1401 uip_ipaddr_copy( BUF
->srcipaddr
, uip_hostaddr
);
1403 /* And send out the RST packet! */
1404 goto tcp_send_noconn
;
1406 /* This label will be jumped to if we matched the incoming packet
1407 with a connection in LISTEN. In that case, we should create a new
1408 connection and send a SYNACK in return. */
1410 /* First we check if there are any connections avaliable. Unused
1411 connections are kept in the same table as used connections, but
1412 unused ones have the tcpstate set to CLOSED. Also, connections in
1413 TIME_WAIT are kept track of and we'll use the oldest one if no
1414 CLOSED connections are found. Thanks to Eddie C. Dost for a very
1415 nice algorithm for the TIME_WAIT search. */
1417 for( c
= 0; c
< UIP_CONNS
; ++c
)
1419 if( uip_conns
[c
].tcpstateflags
== UIP_CLOSED
)
1421 uip_connr
= &uip_conns
[c
];
1425 if( uip_conns
[c
].tcpstateflags
== UIP_TIME_WAIT
)
1427 if( uip_connr
== 0 || uip_conns
[c
].timer
> uip_connr
->timer
)
1429 uip_connr
= &uip_conns
[c
];
1434 if( uip_connr
== 0 )
1436 /* All connections are used already, we drop packet and hope that
1437 the remote end will retransmit the packet at a time when we
1438 have more spare connections. */
1439 UIP_STAT( ++uip_stat
.tcp
.syndrop
);
1440 UIP_LOG( "tcp: found no unused connections." );
1444 uip_conn
= uip_connr
;
1446 /* Fill in the necessary fields for the new connection. */
1447 uip_connr
->rto
= uip_connr
->timer
= UIP_RTO
;
1450 uip_connr
->nrtx
= 0;
1451 uip_connr
->lport
= BUF
->destport
;
1452 uip_connr
->rport
= BUF
->srcport
;
1453 uip_ipaddr_copy( uip_connr
->ripaddr
, BUF
->srcipaddr
);
1454 uip_connr
->tcpstateflags
= UIP_SYN_RCVD
;
1456 uip_connr
->snd_nxt
[0] = iss
[0];
1457 uip_connr
->snd_nxt
[1] = iss
[1];
1458 uip_connr
->snd_nxt
[2] = iss
[2];
1459 uip_connr
->snd_nxt
[3] = iss
[3];
1462 /* rcv_nxt should be the seqno from the incoming packet + 1. */
1463 uip_connr
->rcv_nxt
[3] = BUF
->seqno
[3];
1464 uip_connr
->rcv_nxt
[2] = BUF
->seqno
[2];
1465 uip_connr
->rcv_nxt
[1] = BUF
->seqno
[1];
1466 uip_connr
->rcv_nxt
[0] = BUF
->seqno
[0];
1467 uip_add_rcv_nxt( 1 );
1469 /* Parse the TCP MSS option, if present. */
1470 if( (BUF
->tcpoffset
& 0xf0) > 0x50 )
1472 for( c
= 0; c
< ((BUF
->tcpoffset
>> 4) - 5) << 2; )
1474 opt
= uip_buf
[UIP_TCPIP_HLEN
+ UIP_LLH_LEN
+ c
];
1475 if( opt
== TCP_OPT_END
)
1477 /* End of options. */
1480 else if( opt
== TCP_OPT_NOOP
)
1486 else if( opt
== TCP_OPT_MSS
&& uip_buf
[UIP_TCPIP_HLEN
+ UIP_LLH_LEN
+ 1 + c
] == TCP_OPT_MSS_LEN
)
1488 /* An MSS option with the right option length. */
1489 tmp16
= ( (u16_t
) uip_buf
[UIP_TCPIP_HLEN
+ UIP_LLH_LEN
+ 2 + c
] << 8 ) | ( u16_t
) uip_buf
[UIP_IPTCPH_LEN
+ UIP_LLH_LEN
+ 3 + c
];
1490 uip_connr
->initialmss
= uip_connr
->mss
= tmp16
> UIP_TCP_MSS
? UIP_TCP_MSS
: tmp16
;
1492 /* And we are done processing options. */
1497 /* All other options have a length field, so that we easily
1498 can skip past them. */
1499 if( uip_buf
[UIP_TCPIP_HLEN
+ UIP_LLH_LEN
+ 1 + c
] == 0 )
1501 /* If the length field is zero, the options are malformed
1502 and we don't process them further. */
1506 c
+= uip_buf
[UIP_TCPIP_HLEN
+ UIP_LLH_LEN
+ 1 + c
];
1511 /* Our response will be a SYNACK. */
1513 tcp_send_synack
: BUF
->flags
= TCP_ACK
;
1516 BUF
->flags
|= TCP_SYN
;
1517 #else /* UIP_ACTIVE_OPEN */
1518 tcp_send_synack
: BUF
->flags
= TCP_SYN
| TCP_ACK
;
1519 #endif /* UIP_ACTIVE_OPEN */
1521 /* We send out the TCP Maximum Segment Size option with our
1523 BUF
->optdata
[0] = TCP_OPT_MSS
;
1524 BUF
->optdata
[1] = TCP_OPT_MSS_LEN
;
1525 BUF
->optdata
[2] = ( UIP_TCP_MSS
) / 256;
1526 BUF
->optdata
[3] = ( UIP_TCP_MSS
) & 255;
1527 uip_len
= UIP_IPTCPH_LEN
+ TCP_OPT_MSS_LEN
;
1528 BUF
->tcpoffset
= ( (UIP_TCPH_LEN
+ TCP_OPT_MSS_LEN
) / 4 ) << 4;
1531 /* This label will be jumped to if we found an active connection. */
1533 uip_conn
= uip_connr
;
1536 /* We do a very naive form of TCP reset processing; we just accept
1537 any RST and kill our connection. We should in fact check if the
1538 sequence number of this reset is wihtin our advertised window
1539 before we accept the reset. */
1540 if( BUF
->flags
& TCP_RST
)
1542 uip_connr
->tcpstateflags
= UIP_CLOSED
;
1543 UIP_LOG( "tcp: got reset, aborting connection." );
1544 uip_flags
= UIP_ABORT
;
1549 /* Calculated the length of the data, if the application has sent
1551 c
= ( BUF
->tcpoffset
>> 4 ) << 2;
1553 /* uip_len will contain the length of the actual TCP data. This is
1554 calculated by subtracing the length of the TCP header (in
1555 c) and the length of the IP header (20 bytes). */
1556 uip_len
= uip_len
- c
- UIP_IPH_LEN
;
1558 /* First, check if the sequence number of the incoming packet is
1559 what we're expecting next. If not, we send out an ACK with the
1560 correct numbers in. */
1561 if( !(((uip_connr
->tcpstateflags
& UIP_TS_MASK
) == UIP_SYN_SENT
) && ((BUF
->flags
& TCP_CTL
) == (TCP_SYN
| TCP_ACK
))) )
1565 (uip_len
> 0 || ((BUF
->flags
& (TCP_SYN
| TCP_FIN
)) != 0)) &&
1567 BUF
->seqno
[0] != uip_connr
->rcv_nxt
[0] ||
1568 BUF
->seqno
[1] != uip_connr
->rcv_nxt
[1] ||
1569 BUF
->seqno
[2] != uip_connr
->rcv_nxt
[2] ||
1570 BUF
->seqno
[3] != uip_connr
->rcv_nxt
[3]
1578 /* Next, check if the incoming segment acknowledges any outstanding
1579 data. If so, we update the sequence number, reset the length of
1580 the outstanding data, calculate RTT estimations, and reset the
1581 retransmission timer. */
1582 if( (BUF
->flags
& TCP_ACK
) && uip_outstanding(uip_connr
) )
1584 uip_add32( uip_connr
->snd_nxt
, uip_connr
->len
);
1588 BUF
->ackno
[0] == uip_acc32
[0] &&
1589 BUF
->ackno
[1] == uip_acc32
[1] &&
1590 BUF
->ackno
[2] == uip_acc32
[2] &&
1591 BUF
->ackno
[3] == uip_acc32
[3]
1594 /* Update sequence number. */
1595 uip_connr
->snd_nxt
[0] = uip_acc32
[0];
1596 uip_connr
->snd_nxt
[1] = uip_acc32
[1];
1597 uip_connr
->snd_nxt
[2] = uip_acc32
[2];
1598 uip_connr
->snd_nxt
[3] = uip_acc32
[3];
1600 /* Do RTT estimation, unless we have done retransmissions. */
1601 if( uip_connr
->nrtx
== 0 )
1604 m
= uip_connr
->rto
- uip_connr
->timer
;
1606 /* This is taken directly from VJs original code in his paper */
1607 m
= m
- ( uip_connr
->sa
>> 3 );
1614 m
= m
- ( uip_connr
->sv
>> 2 );
1616 uip_connr
->rto
= ( uip_connr
->sa
>> 3 ) + uip_connr
->sv
;
1619 /* Set the acknowledged flag. */
1620 uip_flags
= UIP_ACKDATA
;
1622 /* Reset the retransmission timer. */
1623 uip_connr
->timer
= uip_connr
->rto
;
1625 /* Reset length of outstanding data. */
1630 /* Do different things depending on in what state the connection is. */
1631 switch( uip_connr
->tcpstateflags
& UIP_TS_MASK
)
1633 /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
1634 implemented, since we force the application to close when the
1635 peer sends a FIN (hence the application goes directly from
1636 ESTABLISHED to LAST_ACK). */
1638 /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
1639 we are waiting for an ACK that acknowledges the data we sent
1640 out the last time. Therefore, we want to have the UIP_ACKDATA
1641 flag set. If so, we enter the ESTABLISHED state. */
1642 if( uip_flags
& UIP_ACKDATA
)
1644 uip_connr
->tcpstateflags
= UIP_ESTABLISHED
;
1645 uip_flags
= UIP_CONNECTED
;
1649 uip_flags
|= UIP_NEWDATA
;
1650 uip_add_rcv_nxt( uip_len
);
1662 /* In SYN_SENT, we wait for a SYNACK that is sent in response to
1663 our SYN. The rcv_nxt is set to sequence number in the SYNACK
1664 plus one, and we send an ACK. We move into the ESTABLISHED
1666 if( (uip_flags
& UIP_ACKDATA
) && (BUF
->flags
& TCP_CTL
) == (TCP_SYN
| TCP_ACK
) )
1668 /* Parse the TCP MSS option, if present. */
1669 if( (BUF
->tcpoffset
& 0xf0) > 0x50 )
1671 for( c
= 0; c
< ((BUF
->tcpoffset
>> 4) - 5) << 2; )
1673 opt
= uip_buf
[UIP_IPTCPH_LEN
+ UIP_LLH_LEN
+ c
];
1674 if( opt
== TCP_OPT_END
)
1676 /* End of options. */
1679 else if( opt
== TCP_OPT_NOOP
)
1685 else if( opt
== TCP_OPT_MSS
&& uip_buf
[UIP_TCPIP_HLEN
+ UIP_LLH_LEN
+ 1 + c
] == TCP_OPT_MSS_LEN
)
1687 /* An MSS option with the right option length. */
1688 tmp16
= ( uip_buf
[UIP_TCPIP_HLEN
+ UIP_LLH_LEN
+ 2 + c
] << 8 ) | uip_buf
[UIP_TCPIP_HLEN
+ UIP_LLH_LEN
+ 3 + c
];
1689 uip_connr
->initialmss
= uip_connr
->mss
= tmp16
> UIP_TCP_MSS
? UIP_TCP_MSS
: tmp16
;
1691 /* And we are done processing options. */
1696 /* All other options have a length field, so that we easily
1697 can skip past them. */
1698 if( uip_buf
[UIP_TCPIP_HLEN
+ UIP_LLH_LEN
+ 1 + c
] == 0 )
1700 /* If the length field is zero, the options are malformed
1701 and we don't process them further. */
1705 c
+= uip_buf
[UIP_TCPIP_HLEN
+ UIP_LLH_LEN
+ 1 + c
];
1710 uip_connr
->tcpstateflags
= UIP_ESTABLISHED
;
1711 uip_connr
->rcv_nxt
[0] = BUF
->seqno
[0];
1712 uip_connr
->rcv_nxt
[1] = BUF
->seqno
[1];
1713 uip_connr
->rcv_nxt
[2] = BUF
->seqno
[2];
1714 uip_connr
->rcv_nxt
[3] = BUF
->seqno
[3];
1715 uip_add_rcv_nxt( 1 );
1716 uip_flags
= UIP_CONNECTED
| UIP_NEWDATA
;
1724 /* Inform the application that the connection failed */
1725 uip_flags
= UIP_ABORT
;
1728 /* The connection is closed after we send the RST */
1729 uip_conn
->tcpstateflags
= UIP_CLOSED
;
1731 #endif /* UIP_ACTIVE_OPEN */
1733 case UIP_ESTABLISHED
:
1734 /* In the ESTABLISHED state, we call upon the application to feed
1735 data into the uip_buf. If the UIP_ACKDATA flag is set, the
1736 application should put new data into the buffer, otherwise we are
1737 retransmitting an old segment, and the application should put that
1738 data into the buffer.
1740 If the incoming packet is a FIN, we should close the connection on
1741 this side as well, and we send out a FIN and enter the LAST_ACK
1742 state. We require that there is no outstanding data; otherwise the
1743 sequence numbers will be screwed up. */
1744 if( BUF
->flags
& TCP_FIN
&& !(uip_connr
->tcpstateflags
& UIP_STOPPED
) )
1746 if( uip_outstanding(uip_connr
) )
1751 uip_add_rcv_nxt( 1 + uip_len
);
1752 uip_flags
|= UIP_CLOSE
;
1755 uip_flags
|= UIP_NEWDATA
;
1760 uip_connr
->tcpstateflags
= UIP_LAST_ACK
;
1761 uip_connr
->nrtx
= 0;
1763 BUF
->flags
= TCP_FIN
| TCP_ACK
;
1764 goto tcp_send_nodata
;
1767 /* Check the URG flag. If this is set, the segment carries urgent
1768 data that we must pass to the application. */
1769 if( (BUF
->flags
& TCP_URG
) != 0 )
1772 uip_urglen
= ( BUF
->urgp
[0] << 8 ) | BUF
->urgp
[1];
1773 if( uip_urglen
> uip_len
)
1775 /* There is more urgent data in the next segment to come. */
1776 uip_urglen
= uip_len
;
1779 uip_add_rcv_nxt( uip_urglen
);
1780 uip_len
-= uip_urglen
;
1781 uip_urgdata
= uip_appdata
;
1782 uip_appdata
+= uip_urglen
;
1787 #else /* UIP_URGDATA > 0 */
1788 uip_appdata
= ( ( char * ) uip_appdata
) + ( (BUF
->urgp
[0] << 8) | BUF
->urgp
[1] );
1789 uip_len
-= ( BUF
->urgp
[0] << 8 ) | BUF
->urgp
[1];
1790 #endif /* UIP_URGDATA > 0 */
1793 /* If uip_len > 0 we have TCP data in the packet, and we flag this
1794 by setting the UIP_NEWDATA flag and update the sequence number
1795 we acknowledge. If the application has stopped the dataflow
1796 using uip_stop(), we must not accept any data packets from the
1798 if( uip_len
> 0 && !(uip_connr
->tcpstateflags
& UIP_STOPPED
) )
1800 uip_flags
|= UIP_NEWDATA
;
1801 uip_add_rcv_nxt( uip_len
);
1804 /* Check if the available buffer space advertised by the other end
1805 is smaller than the initial MSS for this connection. If so, we
1806 set the current MSS to the window size to ensure that the
1807 application does not send more data than the other end can
1810 If the remote host advertises a zero window, we set the MSS to
1811 the initial MSS so that the application will send an entire MSS
1812 of data. This data will not be acknowledged by the receiver,
1813 and the application will retransmit it. This is called the
1814 "persistent timer" and uses the retransmission mechanim.
1816 tmp16
= ( (u16_t
) BUF
->wnd
[0] << 8 ) + ( u16_t
) BUF
->wnd
[1];
1817 if( tmp16
> (uip_connr
->initialmss
* FRAME_MULTIPLE
) || tmp16
== 0 )
1819 tmp16
= uip_connr
->initialmss
* FRAME_MULTIPLE
;
1822 uip_connr
->mss
= tmp16
;
1824 /* If this packet constitutes an ACK for outstanding data (flagged
1825 by the UIP_ACKDATA flag, we should call the application since it
1826 might want to send more data. If the incoming packet had data
1827 from the peer (as flagged by the UIP_NEWDATA flag), the
1828 application must also be notified.
1830 When the application is called, the global variable uip_len
1831 contains the length of the incoming data. The application can
1832 access the incoming data through the global pointer
1833 uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN
1834 bytes into the uip_buf array.
1836 If the application wishes to send any data, this data should be
1837 put into the uip_appdata and the length of the data should be
1838 put into uip_len. If the application don't have any data to
1839 send, uip_len must be set to 0. */
1840 if( uip_flags
& (UIP_NEWDATA
| UIP_ACKDATA
) )
1846 if( uip_flags
& UIP_ABORT
)
1849 uip_connr
->tcpstateflags
= UIP_CLOSED
;
1850 BUF
->flags
= TCP_RST
| TCP_ACK
;
1851 goto tcp_send_nodata
;
1854 if( uip_flags
& UIP_CLOSE
)
1858 uip_connr
->tcpstateflags
= UIP_FIN_WAIT_1
;
1859 uip_connr
->nrtx
= 0;
1860 BUF
->flags
= TCP_FIN
| TCP_ACK
;
1861 goto tcp_send_nodata
;
1864 /* If uip_slen > 0, the application has data to be sent. */
1867 /* If the connection has acknowledged data, the contents of
1868 the ->len variable should be discarded. */
1869 if( (uip_flags
& UIP_ACKDATA
) != 0 )
1874 /* If the ->len variable is non-zero the connection has
1875 already data in transit and cannot send anymore right
1877 if( uip_connr
->len
== 0 )
1879 /* The application cannot send more than what is allowed by
1880 the mss (the minumum of the MSS and the available
1882 if( uip_slen
> uip_connr
->mss
)
1884 uip_slen
= uip_connr
->mss
;
1887 /* Remember how much data we send out now so that we know
1888 when everything has been acknowledged. */
1889 uip_connr
->len
= uip_slen
;
1893 /* If the application already had unacknowledged data, we
1894 make sure that the application does not send (i.e.,
1895 retransmit) out more than it previously sent out. */
1896 uip_slen
= uip_connr
->len
;
1900 uip_connr
->nrtx
= 0;
1902 uip_appdata
= uip_sappdata
;
1904 /* If the application has data to be sent, or if the incoming
1905 packet had new data in it, we must send out a packet. */
1906 if( uip_slen
> 0 && uip_connr
->len
> 0 )
1908 /* Add the length of the IP and TCP headers. */
1909 uip_len
= uip_connr
->len
+ UIP_TCPIP_HLEN
;
1911 /* We always set the ACK flag in response packets. */
1912 BUF
->flags
= TCP_ACK
| TCP_PSH
;
1914 /* Send the packet. */
1915 goto tcp_send_noopts
;
1918 /* If there is no data to send, just send out a pure ACK if
1919 there is newdata. */
1920 if( uip_flags
& UIP_NEWDATA
)
1922 uip_len
= UIP_TCPIP_HLEN
;
1923 BUF
->flags
= TCP_ACK
;
1924 goto tcp_send_noopts
;
1931 /* We can close this connection if the peer has acknowledged our
1932 FIN. This is indicated by the UIP_ACKDATA flag. */
1933 if( uip_flags
& UIP_ACKDATA
)
1935 uip_connr
->tcpstateflags
= UIP_CLOSED
;
1936 uip_flags
= UIP_CLOSE
;
1942 case UIP_FIN_WAIT_1
:
1943 /* The application has closed the connection, but the remote host
1944 hasn't closed its end yet. Thus we do nothing but wait for a
1945 FIN from the other side. */
1948 uip_add_rcv_nxt( uip_len
);
1951 if( BUF
->flags
& TCP_FIN
)
1953 if( uip_flags
& UIP_ACKDATA
)
1955 uip_connr
->tcpstateflags
= UIP_TIME_WAIT
;
1956 uip_connr
->timer
= 0;
1961 uip_connr
->tcpstateflags
= UIP_CLOSING
;
1964 uip_add_rcv_nxt( 1 );
1965 uip_flags
= UIP_CLOSE
;
1969 else if( uip_flags
& UIP_ACKDATA
)
1971 uip_connr
->tcpstateflags
= UIP_FIN_WAIT_2
;
1983 case UIP_FIN_WAIT_2
:
1986 uip_add_rcv_nxt( uip_len
);
1989 if( BUF
->flags
& TCP_FIN
)
1991 uip_connr
->tcpstateflags
= UIP_TIME_WAIT
;
1992 uip_connr
->timer
= 0;
1993 uip_add_rcv_nxt( 1 );
1994 uip_flags
= UIP_CLOSE
;
2010 if( uip_flags
& UIP_ACKDATA
)
2012 uip_connr
->tcpstateflags
= UIP_TIME_WAIT
;
2013 uip_connr
->timer
= 0;
2019 /* We jump here when we are ready to send the packet, and just want
2020 to set the appropriate TCP sequence numbers in the TCP header. */
2022 BUF
->flags
= TCP_ACK
;
2024 uip_len
= UIP_IPTCPH_LEN
;
2026 BUF
->tcpoffset
= ( UIP_TCPH_LEN
/ 4 ) << 4;
2028 /* We're done with the input processing. We are now ready to send a
2029 reply. Our job is to fill in all the fields of the TCP and IP
2030 headers before calculating the checksum and finally send the
2032 BUF
->ackno
[0] = uip_connr
->rcv_nxt
[0];
2033 BUF
->ackno
[1] = uip_connr
->rcv_nxt
[1];
2034 BUF
->ackno
[2] = uip_connr
->rcv_nxt
[2];
2035 BUF
->ackno
[3] = uip_connr
->rcv_nxt
[3];
2037 BUF
->seqno
[0] = uip_connr
->snd_nxt
[0];
2038 BUF
->seqno
[1] = uip_connr
->snd_nxt
[1];
2039 BUF
->seqno
[2] = uip_connr
->snd_nxt
[2];
2040 BUF
->seqno
[3] = uip_connr
->snd_nxt
[3];
2042 BUF
->proto
= UIP_PROTO_TCP
;
2044 BUF
->srcport
= uip_connr
->lport
;
2045 BUF
->destport
= uip_connr
->rport
;
2047 uip_ipaddr_copy( BUF
->srcipaddr
, uip_hostaddr
);
2048 uip_ipaddr_copy( BUF
->destipaddr
, uip_connr
->ripaddr
);
2050 if( uip_connr
->tcpstateflags
& UIP_STOPPED
)
2052 /* If the connection has issued uip_stop(), we advertise a zero
2053 window so that the remote host will stop sending data. */
2054 BUF
->wnd
[0] = BUF
->wnd
[1] = 0;
2058 BUF
->wnd
[0] = ( (UIP_RECEIVE_WINDOW
) >> 8 );
2059 BUF
->wnd
[1] = ( (UIP_RECEIVE_WINDOW
) & 0xff );
2066 /* For IPv6, the IP length field does not include the IPv6 IP header
2068 BUF
->len
[0] = ( (uip_len
- UIP_IPH_LEN
) >> 8 );
2069 BUF
->len
[1] = ( (uip_len
- UIP_IPH_LEN
) & 0xff );
2070 #else /* UIP_CONF_IPV6 */
2071 BUF
->len
[0] = ( uip_len
>> 8 );
2072 BUF
->len
[1] = ( uip_len
& 0xff );
2073 #endif /* UIP_CONF_IPV6 */
2075 BUF
->urgp
[0] = BUF
->urgp
[1] = 0;
2077 /* Calculate TCP checksum. */
2079 BUF
->tcpchksum
= ~( uip_tcpchksum() );
2088 #else /* UIP_CONF_IPV6 */
2091 BUF
->ipoffset
[0] = BUF
->ipoffset
[1] = 0;
2093 BUF
->ipid
[0] = ipid
>> 8;
2094 BUF
->ipid
[1] = ipid
& 0xff;
2096 /* Calculate IP checksum. */
2098 BUF
->ipchksum
= ~( uip_ipchksum() );
2099 DEBUG_PRINTF( "uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum() );
2100 #endif /* UIP_CONF_IPV6 */
2102 UIP_STAT( ++uip_stat
.tcp
.sent
);
2104 DEBUG_PRINTF( "Sending packet with length %d (%d)\n", uip_len
, (BUF
->len
[0] << 8) | BUF
->len
[1] );
2106 UIP_STAT( ++uip_stat
.ip
.sent
);
2108 /* Return and let the caller do the actual transmission. */
2117 /*---------------------------------------------------------------------------*/
2118 u16_t
htons( u16_t val
)
2120 return HTONS( val
);
2123 /*---------------------------------------------------------------------------*/
2124 void uip_send( const void *data
, int len
)
2129 if( data
!= uip_sappdata
)
2131 memcpy( uip_sappdata
, (data
), uip_slen
);
2136 /*---------------------------------------------------------------------------*/
2137 int uip_fast_send( int xARP
)
2140 #if NOT_YET_COMPLETE
2142 u16_t tcplen
, len1
= 0, uiAccumulatedLen
= 0, len_previous
= 0, split_len
;
2144 extern int uip_low_level_output( unsigned char *pcBuf
, int ilen
);
2146 if( xARP
== pdTRUE
)
2148 if( BUF
->proto
== UIP_PROTO_TCP
&& uip_slen
> 1 )
2150 tcplen
= uip_len
- UIP_TCPIP_HLEN
;
2152 if( tcplen
> UIP_TCP_MSS
)
2154 split_len
= UIP_TCP_MSS
;
2158 split_len
= tcplen
/ 2;
2163 uiAccumulatedLen
+= len1
;
2165 if( tcplen
> split_len
)
2168 tcplen
-= split_len
;
2176 uip_len
= len1
+ UIP_TCPIP_HLEN
;
2177 BUF
->len
[0] = uip_len
>> 8;
2178 BUF
->len
[1] = uip_len
& 0xff;
2183 /* Create the first packet. This is done by altering the length
2184 field of the IP header and updating the checksums. */
2188 /* Now, create the second packet. To do this, it is not enough to
2189 just alter the length field, but we must also update the TCP
2190 sequence number and point the uip_appdata to a new place in
2191 memory. This place is determined by the length of the first
2194 /* uip_appdata += len1;*/
2195 memcpy( uip_appdata
, ( u8_t
* ) uip_appdata
+ uiAccumulatedLen
, len1
);
2196 uip_add32( BUF
->seqno
, len_previous
);
2197 BUF
->seqno
[0] = uip_acc32
[0];
2198 BUF
->seqno
[1] = uip_acc32
[1];
2199 BUF
->seqno
[2] = uip_acc32
[2];
2200 BUF
->seqno
[3] = uip_acc32
[3];
2203 /* Recalculate the TCP checksum. */
2205 BUF
->tcpchksum
= ~( uip_tcpchksum() );
2207 /* Recalculate the IP checksum. */
2209 BUF
->ipchksum
= ~( uip_ipchksum() );
2211 /* Transmit the packet. */
2213 uip_low_level_output( uip_buf
, uip_len
);
2215 len_previous
= len1
;
2221 uip_low_level_output( uip_buf
, uip_len
);
2226 uip_low_level_output( uip_buf
, uip_len
);