Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uip.c 58KB

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