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