選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

uip.c 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. /**
  2. * \addtogroup uip
  3. * @{
  4. */
  5. /**
  6. * \file
  7. * The uIP TCP/IP stack code.
  8. * \author Adam Dunkels <adam@dunkels.com>
  9. */
  10. /*
  11. * Copyright (c) 2001-2003, Adam Dunkels.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. The name of the author may not be used to endorse or promote
  23. * products derived from this software without specific prior
  24. * written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  27. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  30. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  34. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * This file is part of the uIP TCP/IP stack.
  39. *
  40. * $Id$
  41. *
  42. */
  43. /*
  44. This is a small implementation of the IP and TCP protocols (as well as
  45. some basic ICMP stuff). The implementation couples the IP, TCP and the
  46. application layers very tightly. To keep the size of the compiled code
  47. down, this code also features heavy usage of the goto statement.
  48. The principle is that we have a small buffer, called the uip_buf, in
  49. which the device driver puts an incoming packet. The TCP/IP stack
  50. parses the headers in the packet, and calls upon the application. If
  51. the remote host has sent data to the application, this data is present
  52. in the uip_buf and the application read the data from there. It is up
  53. to the application to put this data into a byte stream if needed. The
  54. application will not be fed with data that is out of sequence.
  55. If the application whishes to send data to the peer, it should put its
  56. data into the uip_buf, 40 bytes from the start of the buffer. The
  57. TCP/IP stack will calculate the checksums, and fill in the necessary
  58. header fields and finally send the packet back to the peer.
  59. */
  60. #include <stdint.h>
  61. #include <byteswap.h>
  62. #include "uip.h"
  63. #include "uipopt.h"
  64. #include "uip_arch.h"
  65. /*-----------------------------------------------------------------------------------*/
  66. /* Variable definitions. */
  67. /* The IP address of this host. If it is defined to be fixed (by setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set here. Otherwise, the address */
  68. #if UIP_FIXEDADDR > 0
  69. const u16_t uip_hostaddr[2] =
  70. {HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1),
  71. HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)};
  72. const u16_t uip_arp_draddr[2] =
  73. {HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1),
  74. HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)};
  75. const u16_t uip_arp_netmask[2] =
  76. {HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1),
  77. HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)};
  78. #else
  79. u16_t uip_hostaddr[2];
  80. u16_t uip_arp_draddr[2], uip_arp_netmask[2];
  81. #endif /* UIP_FIXEDADDR */
  82. u8_t uip_buf[UIP_BUFSIZE+2]; /* The packet buffer that contains
  83. incoming packets. */
  84. volatile u8_t *uip_appdata; /* The uip_appdata pointer points to
  85. application data. */
  86. volatile u8_t *uip_sappdata; /* The uip_appdata pointer points to the
  87. application data which is to be sent. */
  88. #if UIP_URGDATA > 0
  89. volatile u8_t *uip_urgdata; /* The uip_urgdata pointer points to
  90. urgent data (out-of-band data), if
  91. present. */
  92. volatile u8_t uip_urglen, uip_surglen;
  93. #endif /* UIP_URGDATA > 0 */
  94. volatile u16_t uip_len, uip_slen;
  95. /* The uip_len is either 8 or 16 bits,
  96. depending on the maximum packet
  97. size. */
  98. volatile u8_t uip_flags; /* The uip_flags variable is used for
  99. communication between the TCP/IP stack
  100. and the application program. */
  101. struct uip_conn *uip_conn; /* uip_conn always points to the current
  102. connection. */
  103. struct uip_conn uip_conns[UIP_CONNS];
  104. /* The uip_conns array holds all TCP
  105. connections. */
  106. u16_t uip_listenports[UIP_LISTENPORTS];
  107. /* The uip_listenports list all currently
  108. listning ports. */
  109. #if UIP_UDP
  110. struct uip_udp_conn *uip_udp_conn;
  111. struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
  112. #endif /* UIP_UDP */
  113. static u16_t ipid; /* Ths ipid variable is an increasing
  114. number that is used for the IP ID
  115. field. */
  116. static u8_t iss[4]; /* The iss variable is used for the TCP
  117. initial sequence number. */
  118. #if UIP_ACTIVE_OPEN
  119. static u16_t lastport = 1024; /* Keeps track of the last port used for
  120. a new connection. */
  121. #endif /* UIP_ACTIVE_OPEN */
  122. /* Temporary variables. */
  123. volatile u8_t uip_acc32[4];
  124. static u8_t c, opt;
  125. static u16_t tmp16;
  126. /* Structures and definitions. */
  127. #define TCP_FIN 0x01
  128. #define TCP_SYN 0x02
  129. #define TCP_RST 0x04
  130. #define TCP_PSH 0x08
  131. #define TCP_ACK 0x10
  132. #define TCP_URG 0x20
  133. #define TCP_CTL 0x3f
  134. #define ICMP_ECHO_REPLY 0
  135. #define ICMP_ECHO 8
  136. /* Macros. */
  137. #define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
  138. #define FBUF ((uip_tcpip_hdr *)&uip_reassbuf[0])
  139. #define ICMPBUF ((uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
  140. #define UDPBUF ((uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
  141. #if UIP_STATISTICS == 1
  142. struct uip_stats uip_stat;
  143. #define UIP_STAT(s) s
  144. #else
  145. #define UIP_STAT(s)
  146. #endif /* UIP_STATISTICS == 1 */
  147. #if UIP_LOGGING == 1
  148. void uip_log(char *msg);
  149. #define UIP_LOG(m) uip_log(m)
  150. #else
  151. #define UIP_LOG(m)
  152. #endif /* UIP_LOGGING == 1 */
  153. /*-----------------------------------------------------------------------------------*/
  154. #if 0
  155. void
  156. uip_init(void)
  157. {
  158. for(c = 0; c < UIP_LISTENPORTS; ++c) {
  159. uip_listenports[c] = 0;
  160. }
  161. for(c = 0; c < UIP_CONNS; ++c) {
  162. uip_conns[c].tcpstateflags = CLOSED;
  163. }
  164. #if UIP_ACTIVE_OPEN
  165. lastport = 1024;
  166. #endif /* UIP_ACTIVE_OPEN */
  167. #if UIP_UDP
  168. for(c = 0; c < UIP_UDP_CONNS; ++c) {
  169. uip_udp_conns[c].lport = 0;
  170. }
  171. #endif /* UIP_UDP */
  172. /* IPv4 initialization. */
  173. #if UIP_FIXEDADDR == 0
  174. uip_hostaddr[0] = uip_hostaddr[1] = 0;
  175. #endif /* UIP_FIXEDADDR */
  176. }
  177. #endif
  178. /*-----------------------------------------------------------------------------------*/
  179. #if UIP_ACTIVE_OPEN
  180. struct uip_conn *
  181. uip_connect(u16_t *ripaddr, u16_t rport)
  182. {
  183. register struct uip_conn *conn, *cconn;
  184. /* Find an unused local port. */
  185. again:
  186. ++lastport;
  187. if(lastport >= 32000) {
  188. lastport = 4096;
  189. }
  190. /* Check if this port is already in use, and if so try to find
  191. another one. */
  192. for(c = 0; c < UIP_CONNS; ++c) {
  193. conn = &uip_conns[c];
  194. if(conn->tcpstateflags != CLOSED &&
  195. conn->lport == htons(lastport)) {
  196. goto again;
  197. }
  198. }
  199. conn = 0;
  200. for(c = 0; c < UIP_CONNS; ++c) {
  201. cconn = &uip_conns[c];
  202. if(cconn->tcpstateflags == CLOSED) {
  203. conn = cconn;
  204. break;
  205. }
  206. if(cconn->tcpstateflags == TIME_WAIT) {
  207. if(conn == 0 ||
  208. cconn->timer > uip_conn->timer) {
  209. conn = cconn;
  210. }
  211. }
  212. }
  213. if(conn == 0) {
  214. return 0;
  215. }
  216. conn->tcpstateflags = SYN_SENT;
  217. conn->snd_nxt[0] = iss[0];
  218. conn->snd_nxt[1] = iss[1];
  219. conn->snd_nxt[2] = iss[2];
  220. conn->snd_nxt[3] = iss[3];
  221. conn->initialmss = conn->mss = UIP_TCP_MSS;
  222. conn->len = 1; /* TCP length of the SYN is one. */
  223. conn->nrtx = 0;
  224. conn->timer = 1; /* Send the SYN next time around. */
  225. conn->rto = UIP_RTO;
  226. conn->sa = 0;
  227. conn->sv = 16;
  228. conn->lport = htons(lastport);
  229. conn->rport = rport;
  230. conn->ripaddr[0] = ripaddr[0];
  231. conn->ripaddr[1] = ripaddr[1];
  232. return conn;
  233. }
  234. #endif /* UIP_ACTIVE_OPEN */
  235. /*-----------------------------------------------------------------------------------*/
  236. #if UIP_UDP
  237. struct uip_udp_conn *
  238. uip_udp_new(u16_t *ripaddr, u16_t rport)
  239. {
  240. register struct uip_udp_conn *conn;
  241. /* Find an unused local port. */
  242. again:
  243. ++lastport;
  244. if(lastport >= 32000) {
  245. lastport = 4096;
  246. }
  247. for(c = 0; c < UIP_UDP_CONNS; ++c) {
  248. if(uip_udp_conns[c].lport == lastport) {
  249. goto again;
  250. }
  251. }
  252. conn = 0;
  253. for(c = 0; c < UIP_UDP_CONNS; ++c) {
  254. if(uip_udp_conns[c].lport == 0) {
  255. conn = &uip_udp_conns[c];
  256. break;
  257. }
  258. }
  259. if(conn == 0) {
  260. return 0;
  261. }
  262. conn->lport = HTONS(lastport);
  263. conn->rport = HTONS(rport);
  264. conn->ripaddr[0] = ripaddr[0];
  265. conn->ripaddr[1] = ripaddr[1];
  266. return conn;
  267. }
  268. #endif /* UIP_UDP */
  269. /*-----------------------------------------------------------------------------------*/
  270. void
  271. uip_unlisten(u16_t port)
  272. {
  273. for(c = 0; c < UIP_LISTENPORTS; ++c) {
  274. if(uip_listenports[c] == port) {
  275. uip_listenports[c] = 0;
  276. return;
  277. }
  278. }
  279. }
  280. /*-----------------------------------------------------------------------------------*/
  281. void
  282. uip_listen(u16_t port)
  283. {
  284. for(c = 0; c < UIP_LISTENPORTS; ++c) {
  285. if(uip_listenports[c] == 0) {
  286. uip_listenports[c] = port;
  287. return;
  288. }
  289. }
  290. }
  291. /*-----------------------------------------------------------------------------------*/
  292. /* XXX: IP fragment reassembly: not well-tested. */
  293. #if UIP_REASSEMBLY
  294. #define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
  295. static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];
  296. static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
  297. static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
  298. 0x0f, 0x07, 0x03, 0x01};
  299. static u16_t uip_reasslen;
  300. static u8_t uip_reassflags;
  301. #define UIP_REASS_FLAG_LASTFRAG 0x01
  302. static u8_t uip_reasstmr;
  303. #define IP_HLEN 20
  304. #define IP_MF 0x20
  305. static u8_t
  306. uip_reass(void)
  307. {
  308. u16_t offset, len;
  309. u16_t i;
  310. /* If ip_reasstmr is zero, no packet is present in the buffer, so we
  311. write the IP header of the fragment into the reassembly
  312. buffer. The timer is updated with the maximum age. */
  313. if(uip_reasstmr == 0) {
  314. memcpy(uip_reassbuf, &BUF->vhl, IP_HLEN);
  315. uip_reasstmr = UIP_REASS_MAXAGE;
  316. uip_reassflags = 0;
  317. /* Clear the bitmap. */
  318. memset(uip_reassbitmap, sizeof(uip_reassbitmap), 0);
  319. }
  320. /* Check if the incoming fragment matches the one currently present
  321. in the reasembly buffer. If so, we proceed with copying the
  322. fragment into the buffer. */
  323. if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&
  324. BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
  325. BUF->destipaddr[0] == FBUF->destipaddr[0] &&
  326. BUF->destipaddr[1] == FBUF->destipaddr[1] &&
  327. BUF->ipid[0] == FBUF->ipid[0] &&
  328. BUF->ipid[1] == FBUF->ipid[1]) {
  329. len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
  330. offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
  331. /* If the offset or the offset + fragment length overflows the
  332. reassembly buffer, we discard the entire packet. */
  333. if(offset > UIP_REASS_BUFSIZE ||
  334. offset + len > UIP_REASS_BUFSIZE) {
  335. uip_reasstmr = 0;
  336. goto nullreturn;
  337. }
  338. /* Copy the fragment into the reassembly buffer, at the right
  339. offset. */
  340. memcpy(&uip_reassbuf[IP_HLEN + offset],
  341. (char *)BUF + (int)((BUF->vhl & 0x0f) * 4),
  342. len);
  343. /* Update the bitmap. */
  344. if(offset / (8 * 8) == (offset + len) / (8 * 8)) {
  345. /* If the two endpoints are in the same byte, we only update
  346. that byte. */
  347. uip_reassbitmap[offset / (8 * 8)] |=
  348. bitmap_bits[(offset / 8 ) & 7] &
  349. ~bitmap_bits[((offset + len) / 8 ) & 7];
  350. } else {
  351. /* If the two endpoints are in different bytes, we update the
  352. bytes in the endpoints and fill the stuff inbetween with
  353. 0xff. */
  354. uip_reassbitmap[offset / (8 * 8)] |=
  355. bitmap_bits[(offset / 8 ) & 7];
  356. for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
  357. uip_reassbitmap[i] = 0xff;
  358. }
  359. uip_reassbitmap[(offset + len) / (8 * 8)] |=
  360. ~bitmap_bits[((offset + len) / 8 ) & 7];
  361. }
  362. /* If this fragment has the More Fragments flag set to zero, we
  363. know that this is the last fragment, so we can calculate the
  364. size of the entire packet. We also set the
  365. IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
  366. the final fragment. */
  367. if((BUF->ipoffset[0] & IP_MF) == 0) {
  368. uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
  369. uip_reasslen = offset + len;
  370. }
  371. /* Finally, we check if we have a full packet in the buffer. We do
  372. this by checking if we have the last fragment and if all bits
  373. in the bitmap are set. */
  374. if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
  375. /* Check all bytes up to and including all but the last byte in
  376. the bitmap. */
  377. for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {
  378. if(uip_reassbitmap[i] != 0xff) {
  379. goto nullreturn;
  380. }
  381. }
  382. /* Check the last byte in the bitmap. It should contain just the
  383. right amount of bits. */
  384. if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=
  385. (u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {
  386. goto nullreturn;
  387. }
  388. /* If we have come this far, we have a full packet in the
  389. buffer, so we allocate a pbuf and copy the packet into it. We
  390. also reset the timer. */
  391. uip_reasstmr = 0;
  392. memcpy(BUF, FBUF, uip_reasslen);
  393. /* Pretend to be a "normal" (i.e., not fragmented) IP packet
  394. from now on. */
  395. BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
  396. BUF->len[0] = uip_reasslen >> 8;
  397. BUF->len[1] = uip_reasslen & 0xff;
  398. BUF->ipchksum = 0;
  399. BUF->ipchksum = ~(uip_ipchksum());
  400. return uip_reasslen;
  401. }
  402. }
  403. nullreturn:
  404. return 0;
  405. }
  406. #endif /* UIP_REASSEMBL */
  407. /*-----------------------------------------------------------------------------------*/
  408. static void
  409. uip_add_rcv_nxt(u16_t n)
  410. {
  411. uip_add32(uip_conn->rcv_nxt, n);
  412. uip_conn->rcv_nxt[0] = uip_acc32[0];
  413. uip_conn->rcv_nxt[1] = uip_acc32[1];
  414. uip_conn->rcv_nxt[2] = uip_acc32[2];
  415. uip_conn->rcv_nxt[3] = uip_acc32[3];
  416. }
  417. /*-----------------------------------------------------------------------------------*/
  418. void
  419. uip_process(u8_t flag)
  420. {
  421. register struct uip_conn *uip_connr = uip_conn;
  422. uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
  423. /* Check if we were invoked because of the perodic timer fireing. */
  424. if(flag == UIP_TIMER) {
  425. #if UIP_REASSEMBLY
  426. if(uip_reasstmr != 0) {
  427. --uip_reasstmr;
  428. }
  429. #endif /* UIP_REASSEMBLY */
  430. /* Increase the initial sequence number. */
  431. if(++iss[3] == 0) {
  432. if(++iss[2] == 0) {
  433. if(++iss[1] == 0) {
  434. ++iss[0];
  435. }
  436. }
  437. }
  438. uip_len = 0;
  439. if(uip_connr->tcpstateflags == TIME_WAIT ||
  440. uip_connr->tcpstateflags == FIN_WAIT_2) {
  441. ++(uip_connr->timer);
  442. if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
  443. uip_connr->tcpstateflags = CLOSED;
  444. }
  445. } else if(uip_connr->tcpstateflags != CLOSED) {
  446. /* If the connection has outstanding data, we increase the
  447. connection's timer and see if it has reached the RTO value
  448. in which case we retransmit. */
  449. if(uip_outstanding(uip_connr)) {
  450. if(uip_connr->timer-- == 0) {
  451. if(uip_connr->nrtx == UIP_MAXRTX ||
  452. ((uip_connr->tcpstateflags == SYN_SENT ||
  453. uip_connr->tcpstateflags == SYN_RCVD) &&
  454. uip_connr->nrtx == UIP_MAXSYNRTX)) {
  455. uip_connr->tcpstateflags = CLOSED;
  456. /* We call UIP_APPCALL() with uip_flags set to
  457. UIP_TIMEDOUT to inform the application that the
  458. connection has timed out. */
  459. uip_flags = UIP_TIMEDOUT;
  460. UIP_APPCALL();
  461. /* We also send a reset packet to the remote host. */
  462. BUF->flags = TCP_RST | TCP_ACK;
  463. goto tcp_send_nodata;
  464. }
  465. /* Exponential backoff. */
  466. uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
  467. 4:
  468. uip_connr->nrtx);
  469. ++(uip_connr->nrtx);
  470. /* Ok, so we need to retransmit. We do this differently
  471. depending on which state we are in. In ESTABLISHED, we
  472. call upon the application so that it may prepare the
  473. data for the retransmit. In SYN_RCVD, we resend the
  474. SYNACK that we sent earlier and in LAST_ACK we have to
  475. retransmit our FINACK. */
  476. UIP_STAT(++uip_stat.tcp.rexmit);
  477. switch(uip_connr->tcpstateflags & TS_MASK) {
  478. case SYN_RCVD:
  479. /* In the SYN_RCVD state, we should retransmit our
  480. SYNACK. */
  481. goto tcp_send_synack;
  482. #if UIP_ACTIVE_OPEN
  483. case SYN_SENT:
  484. /* In the SYN_SENT state, we retransmit out SYN. */
  485. BUF->flags = 0;
  486. goto tcp_send_syn;
  487. #endif /* UIP_ACTIVE_OPEN */
  488. case ESTABLISHED:
  489. /* In the ESTABLISHED state, we call upon the application
  490. to do the actual retransmit after which we jump into
  491. the code for sending out the packet (the apprexmit
  492. label). */
  493. uip_len = 0;
  494. uip_slen = 0;
  495. uip_flags = UIP_REXMIT;
  496. UIP_APPCALL();
  497. goto apprexmit;
  498. case FIN_WAIT_1:
  499. case CLOSING:
  500. case LAST_ACK:
  501. /* In all these states we should retransmit a FINACK. */
  502. goto tcp_send_finack;
  503. }
  504. }
  505. } else if((uip_connr->tcpstateflags & TS_MASK) == ESTABLISHED) {
  506. /* If there was no need for a retransmission, we poll the
  507. application for new data. */
  508. uip_len = 0;
  509. uip_slen = 0;
  510. uip_flags = UIP_POLL;
  511. UIP_APPCALL();
  512. goto appsend;
  513. }
  514. }
  515. goto drop;
  516. }
  517. #if UIP_UDP
  518. if(flag == UIP_UDP_TIMER) {
  519. if(uip_udp_conn->lport != 0) {
  520. uip_appdata = &uip_buf[UIP_LLH_LEN + 28];
  521. uip_len = uip_slen = 0;
  522. uip_flags = UIP_POLL;
  523. UIP_UDP_APPCALL();
  524. goto udp_send;
  525. } else {
  526. goto drop;
  527. }
  528. }
  529. #endif
  530. /* This is where the input processing starts. */
  531. UIP_STAT(++uip_stat.ip.recv);
  532. /* Start of IPv4 input header processing code. */
  533. /* Check validity of the IP header. */
  534. if(BUF->vhl != 0x45) { /* IP version and header length. */
  535. UIP_STAT(++uip_stat.ip.drop);
  536. UIP_STAT(++uip_stat.ip.vhlerr);
  537. UIP_LOG("ip: invalid version or header length.");
  538. goto drop;
  539. }
  540. /* Check the size of the packet. If the size reported to us in
  541. uip_len doesn't match the size reported in the IP header, there
  542. has been a transmission error and we drop the packet. */
  543. if(BUF->len[0] != (uip_len >> 8)) { /* IP length, high byte. */
  544. uip_len = (uip_len & 0xff) | (BUF->len[0] << 8);
  545. }
  546. if(BUF->len[1] != (uip_len & 0xff)) { /* IP length, low byte. */
  547. uip_len = (uip_len & 0xff00) | BUF->len[1];
  548. }
  549. /* Check the fragment flag. */
  550. if((BUF->ipoffset[0] & 0x3f) != 0 ||
  551. BUF->ipoffset[1] != 0) {
  552. #if UIP_REASSEMBLY
  553. uip_len = uip_reass();
  554. if(uip_len == 0) {
  555. goto drop;
  556. }
  557. #else
  558. UIP_STAT(++uip_stat.ip.drop);
  559. UIP_STAT(++uip_stat.ip.fragerr);
  560. UIP_LOG("ip: fragment dropped.");
  561. goto drop;
  562. #endif /* UIP_REASSEMBLY */
  563. }
  564. /* If we are configured to use ping IP address configuration and
  565. hasn't been assigned an IP address yet, we accept all ICMP
  566. packets. */
  567. #if UIP_PINGADDRCONF
  568. if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
  569. if(BUF->proto == UIP_PROTO_ICMP) {
  570. UIP_LOG("ip: possible ping config packet received.");
  571. goto icmp_input;
  572. } else {
  573. UIP_LOG("ip: packet dropped since no address assigned.");
  574. goto drop;
  575. }
  576. }
  577. #endif /* UIP_PINGADDRCONF */
  578. /* Check if the packet is destined for our IP address. */
  579. if(BUF->destipaddr[0] != uip_hostaddr[0]) {
  580. UIP_STAT(++uip_stat.ip.drop);
  581. UIP_LOG("ip: packet not for us.");
  582. goto drop;
  583. }
  584. if(BUF->destipaddr[1] != uip_hostaddr[1]) {
  585. UIP_STAT(++uip_stat.ip.drop);
  586. UIP_LOG("ip: packet not for us.");
  587. goto drop;
  588. }
  589. if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
  590. checksum. */
  591. UIP_STAT(++uip_stat.ip.drop);
  592. UIP_STAT(++uip_stat.ip.chkerr);
  593. UIP_LOG("ip: bad checksum.");
  594. goto drop;
  595. }
  596. if(BUF->proto == UIP_PROTO_TCP) /* Check for TCP packet. If so, jump
  597. to the tcp_input label. */
  598. goto tcp_input;
  599. #if UIP_UDP
  600. if(BUF->proto == UIP_PROTO_UDP)
  601. goto udp_input;
  602. #endif /* UIP_UDP */
  603. if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from
  604. here. */
  605. UIP_STAT(++uip_stat.ip.drop);
  606. UIP_STAT(++uip_stat.ip.protoerr);
  607. UIP_LOG("ip: neither tcp nor icmp.");
  608. goto drop;
  609. }
  610. icmp_input:
  611. UIP_STAT(++uip_stat.icmp.recv);
  612. /* ICMP echo (i.e., ping) processing. This is simple, we only change
  613. the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
  614. checksum before we return the packet. */
  615. if(ICMPBUF->type != ICMP_ECHO) {
  616. UIP_STAT(++uip_stat.icmp.drop);
  617. UIP_STAT(++uip_stat.icmp.typeerr);
  618. UIP_LOG("icmp: not icmp echo.");
  619. goto drop;
  620. }
  621. /* If we are configured to use ping IP address assignment, we use
  622. the destination IP address of this ping packet and assign it to
  623. ourself. */
  624. #if UIP_PINGADDRCONF
  625. if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
  626. uip_hostaddr[0] = BUF->destipaddr[0];
  627. uip_hostaddr[1] = BUF->destipaddr[1];
  628. }
  629. #endif /* UIP_PINGADDRCONF */
  630. ICMPBUF->type = ICMP_ECHO_REPLY;
  631. if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {
  632. ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
  633. } else {
  634. ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);
  635. }
  636. /* Swap IP addresses. */
  637. tmp16 = BUF->destipaddr[0];
  638. BUF->destipaddr[0] = BUF->srcipaddr[0];
  639. BUF->srcipaddr[0] = tmp16;
  640. tmp16 = BUF->destipaddr[1];
  641. BUF->destipaddr[1] = BUF->srcipaddr[1];
  642. BUF->srcipaddr[1] = tmp16;
  643. UIP_STAT(++uip_stat.icmp.sent);
  644. goto send;
  645. /* End of IPv4 input header processing code. */
  646. #if UIP_UDP
  647. /* UDP input processing. */
  648. udp_input:
  649. /* UDP processing is really just a hack. We don't do anything to the
  650. UDP/IP headers, but let the UDP application do all the hard
  651. work. If the application sets uip_slen, it has a packet to
  652. send. */
  653. #if UIP_UDP_CHECKSUMS
  654. if(uip_udpchksum() != 0xffff) {
  655. UIP_STAT(++uip_stat.udp.drop);
  656. UIP_STAT(++uip_stat.udp.chkerr);
  657. UIP_LOG("udp: bad checksum.");
  658. goto drop;
  659. }
  660. #endif /* UIP_UDP_CHECKSUMS */
  661. /* Demultiplex this UDP packet between the UDP "connections". */
  662. for(uip_udp_conn = &uip_udp_conns[0];
  663. uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
  664. ++uip_udp_conn) {
  665. if(uip_udp_conn->lport != 0 &&
  666. UDPBUF->destport == uip_udp_conn->lport &&
  667. (uip_udp_conn->rport == 0 ||
  668. UDPBUF->srcport == uip_udp_conn->rport) &&
  669. BUF->srcipaddr[0] == uip_udp_conn->ripaddr[0] &&
  670. BUF->srcipaddr[1] == uip_udp_conn->ripaddr[1]) {
  671. goto udp_found;
  672. }
  673. }
  674. goto drop;
  675. udp_found:
  676. uip_len = uip_len - 28;
  677. uip_appdata = &uip_buf[UIP_LLH_LEN + 28];
  678. uip_flags = UIP_NEWDATA;
  679. uip_slen = 0;
  680. UIP_UDP_APPCALL();
  681. udp_send:
  682. if(uip_slen == 0) {
  683. goto drop;
  684. }
  685. uip_len = uip_slen + 28;
  686. BUF->len[0] = (uip_len >> 8);
  687. BUF->len[1] = (uip_len & 0xff);
  688. BUF->proto = UIP_PROTO_UDP;
  689. UDPBUF->udplen = HTONS(uip_slen + 8);
  690. UDPBUF->udpchksum = 0;
  691. #if UIP_UDP_CHECKSUMS
  692. /* Calculate UDP checksum. */
  693. UDPBUF->udpchksum = ~(uip_udpchksum());
  694. if(UDPBUF->udpchksum == 0) {
  695. UDPBUF->udpchksum = 0xffff;
  696. }
  697. #endif /* UIP_UDP_CHECKSUMS */
  698. BUF->srcport = uip_udp_conn->lport;
  699. BUF->destport = uip_udp_conn->rport;
  700. BUF->srcipaddr[0] = uip_hostaddr[0];
  701. BUF->srcipaddr[1] = uip_hostaddr[1];
  702. BUF->destipaddr[0] = uip_udp_conn->ripaddr[0];
  703. BUF->destipaddr[1] = uip_udp_conn->ripaddr[1];
  704. uip_appdata = &uip_buf[UIP_LLH_LEN + 40];
  705. goto ip_send_nolen;
  706. #endif /* UIP_UDP */
  707. /* TCP input processing. */
  708. tcp_input:
  709. UIP_STAT(++uip_stat.tcp.recv);
  710. /* Start of TCP input header processing code. */
  711. if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
  712. checksum. */
  713. UIP_STAT(++uip_stat.tcp.drop);
  714. UIP_STAT(++uip_stat.tcp.chkerr);
  715. UIP_LOG("tcp: bad checksum.");
  716. goto drop;
  717. }
  718. /* Demultiplex this segment. */
  719. /* First check any active connections. */
  720. for(uip_connr = &uip_conns[0]; uip_connr < &uip_conns[UIP_CONNS]; ++uip_connr) {
  721. if(uip_connr->tcpstateflags != CLOSED &&
  722. BUF->destport == uip_connr->lport &&
  723. BUF->srcport == uip_connr->rport &&
  724. BUF->srcipaddr[0] == uip_connr->ripaddr[0] &&
  725. BUF->srcipaddr[1] == uip_connr->ripaddr[1]) {
  726. goto found;
  727. }
  728. }
  729. /* If we didn't find and active connection that expected the packet,
  730. either this packet is an old duplicate, or this is a SYN packet
  731. destined for a connection in LISTEN. If the SYN flag isn't set,
  732. it is an old packet and we send a RST. */
  733. if((BUF->flags & TCP_CTL) != TCP_SYN)
  734. goto reset;
  735. tmp16 = BUF->destport;
  736. /* Next, check listening connections. */
  737. for(c = 0; c < UIP_LISTENPORTS; ++c) {
  738. if(tmp16 == uip_listenports[c])
  739. goto found_listen;
  740. }
  741. /* No matching connection found, so we send a RST packet. */
  742. UIP_STAT(++uip_stat.tcp.synrst);
  743. reset:
  744. /* We do not send resets in response to resets. */
  745. if(BUF->flags & TCP_RST)
  746. goto drop;
  747. UIP_STAT(++uip_stat.tcp.rst);
  748. BUF->flags = TCP_RST | TCP_ACK;
  749. uip_len = 40;
  750. BUF->tcpoffset = 5 << 4;
  751. /* Flip the seqno and ackno fields in the TCP header. */
  752. c = BUF->seqno[3];
  753. BUF->seqno[3] = BUF->ackno[3];
  754. BUF->ackno[3] = c;
  755. c = BUF->seqno[2];
  756. BUF->seqno[2] = BUF->ackno[2];
  757. BUF->ackno[2] = c;
  758. c = BUF->seqno[1];
  759. BUF->seqno[1] = BUF->ackno[1];
  760. BUF->ackno[1] = c;
  761. c = BUF->seqno[0];
  762. BUF->seqno[0] = BUF->ackno[0];
  763. BUF->ackno[0] = c;
  764. /* We also have to increase the sequence number we are
  765. acknowledging. If the least significant byte overflowed, we need
  766. to propagate the carry to the other bytes as well. */
  767. if(++BUF->ackno[3] == 0) {
  768. if(++BUF->ackno[2] == 0) {
  769. if(++BUF->ackno[1] == 0) {
  770. ++BUF->ackno[0];
  771. }
  772. }
  773. }
  774. /* Swap port numbers. */
  775. tmp16 = BUF->srcport;
  776. BUF->srcport = BUF->destport;
  777. BUF->destport = tmp16;
  778. /* Swap IP addresses. */
  779. tmp16 = BUF->destipaddr[0];
  780. BUF->destipaddr[0] = BUF->srcipaddr[0];
  781. BUF->srcipaddr[0] = tmp16;
  782. tmp16 = BUF->destipaddr[1];
  783. BUF->destipaddr[1] = BUF->srcipaddr[1];
  784. BUF->srcipaddr[1] = tmp16;
  785. /* And send out the RST packet! */
  786. goto tcp_send_noconn;
  787. /* This label will be jumped to if we matched the incoming packet
  788. with a connection in LISTEN. In that case, we should create a new
  789. connection and send a SYNACK in return. */
  790. found_listen:
  791. /* First we check if there are any connections avaliable. Unused
  792. connections are kept in the same table as used connections, but
  793. unused ones have the tcpstate set to CLOSED. Also, connections in
  794. TIME_WAIT are kept track of and we'll use the oldest one if no
  795. CLOSED connections are found. Thanks to Eddie C. Dost for a very
  796. nice algorithm for the TIME_WAIT search. */
  797. uip_connr = 0;
  798. for(c = 0; c < UIP_CONNS; ++c) {
  799. if(uip_conns[c].tcpstateflags == CLOSED) {
  800. uip_connr = &uip_conns[c];
  801. break;
  802. }
  803. if(uip_conns[c].tcpstateflags == TIME_WAIT) {
  804. if(uip_connr == 0 ||
  805. uip_conns[c].timer > uip_connr->timer) {
  806. uip_connr = &uip_conns[c];
  807. }
  808. }
  809. }
  810. if(uip_connr == 0) {
  811. /* All connections are used already, we drop packet and hope that
  812. the remote end will retransmit the packet at a time when we
  813. have more spare connections. */
  814. UIP_STAT(++uip_stat.tcp.syndrop);
  815. UIP_LOG("tcp: found no unused connections.");
  816. goto drop;
  817. }
  818. uip_conn = uip_connr;
  819. /* Fill in the necessary fields for the new connection. */
  820. uip_connr->rto = uip_connr->timer = UIP_RTO;
  821. uip_connr->sa = 0;
  822. uip_connr->sv = 4;
  823. uip_connr->nrtx = 0;
  824. uip_connr->lport = BUF->destport;
  825. uip_connr->rport = BUF->srcport;
  826. uip_connr->ripaddr[0] = BUF->srcipaddr[0];
  827. uip_connr->ripaddr[1] = BUF->srcipaddr[1];
  828. uip_connr->tcpstateflags = SYN_RCVD;
  829. uip_connr->snd_nxt[0] = iss[0];
  830. uip_connr->snd_nxt[1] = iss[1];
  831. uip_connr->snd_nxt[2] = iss[2];
  832. uip_connr->snd_nxt[3] = iss[3];
  833. uip_connr->len = 1;
  834. /* rcv_nxt should be the seqno from the incoming packet + 1. */
  835. uip_connr->rcv_nxt[3] = BUF->seqno[3];
  836. uip_connr->rcv_nxt[2] = BUF->seqno[2];
  837. uip_connr->rcv_nxt[1] = BUF->seqno[1];
  838. uip_connr->rcv_nxt[0] = BUF->seqno[0];
  839. uip_add_rcv_nxt(1);
  840. /* Parse the TCP MSS option, if present. */
  841. if((BUF->tcpoffset & 0xf0) > 0x50) {
  842. for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
  843. opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
  844. if(opt == 0x00) {
  845. /* End of options. */
  846. break;
  847. } else if(opt == 0x01) {
  848. ++c;
  849. /* NOP option. */
  850. } else if(opt == 0x02 &&
  851. uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) {
  852. /* An MSS option with the right option length. */
  853. tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
  854. (u16_t)uip_buf[40 + UIP_LLH_LEN + 3 + c];
  855. uip_connr->initialmss = uip_connr->mss =
  856. tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
  857. /* And we are done processing options. */
  858. break;
  859. } else {
  860. /* All other options have a length field, so that we easily
  861. can skip past them. */
  862. if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
  863. /* If the length field is zero, the options are malformed
  864. and we don't process them further. */
  865. break;
  866. }
  867. c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
  868. }
  869. }
  870. }
  871. /* Our response will be a SYNACK. */
  872. #if UIP_ACTIVE_OPEN
  873. tcp_send_synack:
  874. BUF->flags = TCP_ACK;
  875. tcp_send_syn:
  876. BUF->flags |= TCP_SYN;
  877. #else /* UIP_ACTIVE_OPEN */
  878. tcp_send_synack:
  879. BUF->flags = TCP_SYN | TCP_ACK;
  880. #endif /* UIP_ACTIVE_OPEN */
  881. /* We send out the TCP Maximum Segment Size option with our
  882. SYNACK. */
  883. BUF->optdata[0] = 2;
  884. BUF->optdata[1] = 4;
  885. BUF->optdata[2] = (UIP_TCP_MSS) / 256;
  886. BUF->optdata[3] = (UIP_TCP_MSS) & 255;
  887. uip_len = 44;
  888. BUF->tcpoffset = 6 << 4;
  889. goto tcp_send;
  890. /* This label will be jumped to if we found an active connection. */
  891. found:
  892. uip_conn = uip_connr;
  893. uip_flags = 0;
  894. /* We do a very naive form of TCP reset processing; we just accept
  895. any RST and kill our connection. We should in fact check if the
  896. sequence number of this reset is wihtin our advertised window
  897. before we accept the reset. */
  898. if(BUF->flags & TCP_RST) {
  899. uip_connr->tcpstateflags = CLOSED;
  900. UIP_LOG("tcp: got reset, aborting connection.");
  901. uip_flags = UIP_ABORT;
  902. UIP_APPCALL();
  903. goto drop;
  904. }
  905. /* Calculated the length of the data, if the application has sent
  906. any data to us. */
  907. c = (BUF->tcpoffset >> 4) << 2;
  908. /* uip_len will contain the length of the actual TCP data. This is
  909. calculated by subtracing the length of the TCP header (in
  910. c) and the length of the IP header (20 bytes). */
  911. uip_len = uip_len - c - 20;
  912. /* First, check if the sequence number of the incoming packet is
  913. what we're expecting next. If not, we send out an ACK with the
  914. correct numbers in. */
  915. if(uip_len > 0 &&
  916. (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
  917. BUF->seqno[1] != uip_connr->rcv_nxt[1] ||
  918. BUF->seqno[2] != uip_connr->rcv_nxt[2] ||
  919. BUF->seqno[3] != uip_connr->rcv_nxt[3])) {
  920. goto tcp_send_ack;
  921. }
  922. /* Next, check if the incoming segment acknowledges any outstanding
  923. data. If so, we update the sequence number, reset the length of
  924. the outstanding data, calculate RTT estimations, and reset the
  925. retransmission timer. */
  926. if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {
  927. uip_add32(uip_connr->snd_nxt, uip_connr->len);
  928. if(BUF->ackno[0] == uip_acc32[0] &&
  929. BUF->ackno[1] == uip_acc32[1] &&
  930. BUF->ackno[2] == uip_acc32[2] &&
  931. BUF->ackno[3] == uip_acc32[3]) {
  932. /* Update sequence number. */
  933. uip_connr->snd_nxt[0] = uip_acc32[0];
  934. uip_connr->snd_nxt[1] = uip_acc32[1];
  935. uip_connr->snd_nxt[2] = uip_acc32[2];
  936. uip_connr->snd_nxt[3] = uip_acc32[3];
  937. /* Do RTT estimation, unless we have done retransmissions. */
  938. if(uip_connr->nrtx == 0) {
  939. signed char m;
  940. m = uip_connr->rto - uip_connr->timer;
  941. /* This is taken directly from VJs original code in his paper */
  942. m = m - (uip_connr->sa >> 3);
  943. uip_connr->sa += m;
  944. if(m < 0) {
  945. m = -m;
  946. }
  947. m = m - (uip_connr->sv >> 2);
  948. uip_connr->sv += m;
  949. uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;
  950. }
  951. /* Set the acknowledged flag. */
  952. uip_flags = UIP_ACKDATA;
  953. /* Reset the retransmission timer. */
  954. uip_connr->timer = uip_connr->rto;
  955. }
  956. }
  957. /* Do different things depending on in what state the connection is. */
  958. switch(uip_connr->tcpstateflags & TS_MASK) {
  959. /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not
  960. implemented, since we force the application to close when the
  961. peer sends a FIN (hence the application goes directly from
  962. ESTABLISHED to LAST_ACK). */
  963. case SYN_RCVD:
  964. /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and
  965. we are waiting for an ACK that acknowledges the data we sent
  966. out the last time. Therefore, we want to have the UIP_ACKDATA
  967. flag set. If so, we enter the ESTABLISHED state. */
  968. if(uip_flags & UIP_ACKDATA) {
  969. uip_connr->tcpstateflags = ESTABLISHED;
  970. uip_flags = UIP_CONNECTED;
  971. uip_connr->len = 0;
  972. if(uip_len > 0) {
  973. uip_flags |= UIP_NEWDATA;
  974. uip_add_rcv_nxt(uip_len);
  975. }
  976. uip_slen = 0;
  977. UIP_APPCALL();
  978. goto appsend;
  979. }
  980. goto drop;
  981. #if UIP_ACTIVE_OPEN
  982. case SYN_SENT:
  983. /* In SYN_SENT, we wait for a SYNACK that is sent in response to
  984. our SYN. The rcv_nxt is set to sequence number in the SYNACK
  985. plus one, and we send an ACK. We move into the ESTABLISHED
  986. state. */
  987. if((uip_flags & UIP_ACKDATA) &&
  988. BUF->flags == (TCP_SYN | TCP_ACK)) {
  989. /* Parse the TCP MSS option, if present. */
  990. if((BUF->tcpoffset & 0xf0) > 0x50) {
  991. for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
  992. opt = uip_buf[40 + UIP_LLH_LEN + c];
  993. if(opt == 0x00) {
  994. /* End of options. */
  995. break;
  996. } else if(opt == 0x01) {
  997. ++c;
  998. /* NOP option. */
  999. } else if(opt == 0x02 &&
  1000. uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) {
  1001. /* An MSS option with the right option length. */
  1002. tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
  1003. uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];
  1004. uip_connr->initialmss =
  1005. uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
  1006. /* And we are done processing options. */
  1007. break;
  1008. } else {
  1009. /* All other options have a length field, so that we easily
  1010. can skip past them. */
  1011. if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
  1012. /* If the length field is zero, the options are malformed
  1013. and we don't process them further. */
  1014. break;
  1015. }
  1016. c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
  1017. }
  1018. }
  1019. }
  1020. uip_connr->tcpstateflags = ESTABLISHED;
  1021. uip_connr->rcv_nxt[0] = BUF->seqno[0];
  1022. uip_connr->rcv_nxt[1] = BUF->seqno[1];
  1023. uip_connr->rcv_nxt[2] = BUF->seqno[2];
  1024. uip_connr->rcv_nxt[3] = BUF->seqno[3];
  1025. uip_add_rcv_nxt(1);
  1026. uip_flags = UIP_CONNECTED | UIP_NEWDATA;
  1027. uip_connr->len = 0;
  1028. uip_len = 0;
  1029. uip_slen = 0;
  1030. UIP_APPCALL();
  1031. goto appsend;
  1032. }
  1033. goto reset;
  1034. #endif /* UIP_ACTIVE_OPEN */
  1035. case ESTABLISHED:
  1036. /* In the ESTABLISHED state, we call upon the application to feed
  1037. data into the uip_buf. If the UIP_ACKDATA flag is set, the
  1038. application should put new data into the buffer, otherwise we are
  1039. retransmitting an old segment, and the application should put that
  1040. data into the buffer.
  1041. If the incoming packet is a FIN, we should close the connection on
  1042. this side as well, and we send out a FIN and enter the LAST_ACK
  1043. state. We require that there is no outstanding data; otherwise the
  1044. sequence numbers will be screwed up. */
  1045. if(BUF->flags & TCP_FIN) {
  1046. if(uip_outstanding(uip_connr)) {
  1047. goto drop;
  1048. }
  1049. uip_add_rcv_nxt(1 + uip_len);
  1050. uip_flags = UIP_CLOSE;
  1051. if(uip_len > 0) {
  1052. uip_flags |= UIP_NEWDATA;
  1053. }
  1054. UIP_APPCALL();
  1055. uip_connr->len = 1;
  1056. uip_connr->tcpstateflags = LAST_ACK;
  1057. uip_connr->nrtx = 0;
  1058. tcp_send_finack:
  1059. BUF->flags = TCP_FIN | TCP_ACK;
  1060. goto tcp_send_nodata;
  1061. }
  1062. /* Check the URG flag. If this is set, the segment carries urgent
  1063. data that we must pass to the application. */
  1064. if(BUF->flags & TCP_URG) {
  1065. #if UIP_URGDATA > 0
  1066. uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];
  1067. if(uip_urglen > uip_len) {
  1068. /* There is more urgent data in the next segment to come. */
  1069. uip_urglen = uip_len;
  1070. }
  1071. uip_add_rcv_nxt(uip_urglen);
  1072. uip_len -= uip_urglen;
  1073. uip_urgdata = uip_appdata;
  1074. uip_appdata += uip_urglen;
  1075. } else {
  1076. uip_urglen = 0;
  1077. #endif /* UIP_URGDATA > 0 */
  1078. uip_appdata += (BUF->urgp[0] << 8) | BUF->urgp[1];
  1079. uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];
  1080. }
  1081. /* If uip_len > 0 we have TCP data in the packet, and we flag this
  1082. by setting the UIP_NEWDATA flag and update the sequence number
  1083. we acknowledge. If the application has stopped the dataflow
  1084. using uip_stop(), we must not accept any data packets from the
  1085. remote host. */
  1086. if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
  1087. uip_flags |= UIP_NEWDATA;
  1088. uip_add_rcv_nxt(uip_len);
  1089. }
  1090. /* Check if the available buffer space advertised by the other end
  1091. is smaller than the initial MSS for this connection. If so, we
  1092. set the current MSS to the window size to ensure that the
  1093. application does not send more data than the other end can
  1094. handle.
  1095. If the remote host advertises a zero window, we set the MSS to
  1096. the initial MSS so that the application will send an entire MSS
  1097. of data. This data will not be acknowledged by the receiver,
  1098. and the application will retransmit it. This is called the
  1099. "persistent timer" and uses the retransmission mechanim.
  1100. */
  1101. tmp16 = ((u16_t)BUF->wnd[0] << 8) + (u16_t)BUF->wnd[1];
  1102. if(tmp16 > uip_connr->initialmss ||
  1103. tmp16 == 0) {
  1104. tmp16 = uip_connr->initialmss;
  1105. }
  1106. uip_connr->mss = tmp16;
  1107. /* If this packet constitutes an ACK for outstanding data (flagged
  1108. by the UIP_ACKDATA flag, we should call the application since it
  1109. might want to send more data. If the incoming packet had data
  1110. from the peer (as flagged by the UIP_NEWDATA flag), the
  1111. application must also be notified.
  1112. When the application is called, the global variable uip_len
  1113. contains the length of the incoming data. The application can
  1114. access the incoming data through the global pointer
  1115. uip_appdata, which usually points 40 bytes into the uip_buf
  1116. array.
  1117. If the application wishes to send any data, this data should be
  1118. put into the uip_appdata and the length of the data should be
  1119. put into uip_len. If the application don't have any data to
  1120. send, uip_len must be set to 0. */
  1121. if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {
  1122. uip_slen = 0;
  1123. UIP_APPCALL();
  1124. appsend:
  1125. if(uip_flags & UIP_ABORT) {
  1126. uip_slen = 0;
  1127. uip_connr->tcpstateflags = CLOSED;
  1128. BUF->flags = TCP_RST | TCP_ACK;
  1129. goto tcp_send_nodata;
  1130. }
  1131. if(uip_flags & UIP_CLOSE) {
  1132. uip_slen = 0;
  1133. uip_connr->len = 1;
  1134. uip_connr->tcpstateflags = FIN_WAIT_1;
  1135. uip_connr->nrtx = 0;
  1136. BUF->flags = TCP_FIN | TCP_ACK;
  1137. goto tcp_send_nodata;
  1138. }
  1139. /* If uip_slen > 0, the application has data to be sent. */
  1140. if(uip_slen > 0) {
  1141. /* If the connection has acknowledged data, the contents of
  1142. the ->len variable should be discarded. */
  1143. if((uip_flags & UIP_ACKDATA) != 0) {
  1144. uip_connr->len = 0;
  1145. }
  1146. /* If the ->len variable is non-zero the connection has
  1147. already data in transit and cannot send anymore right
  1148. now. */
  1149. if(uip_connr->len == 0) {
  1150. /* The application cannot send more than what is allowed by
  1151. the mss (the minumum of the MSS and the available
  1152. window). */
  1153. if(uip_slen > uip_connr->mss) {
  1154. uip_slen = uip_connr->mss;
  1155. }
  1156. /* Remember how much data we send out now so that we know
  1157. when everything has been acknowledged. */
  1158. uip_connr->len = uip_slen;
  1159. } else {
  1160. /* If the application already had unacknowledged data, we
  1161. make sure that the application does not send (i.e.,
  1162. retransmit) out more than it previously sent out. */
  1163. uip_slen = uip_connr->len;
  1164. }
  1165. } else {
  1166. uip_connr->len = 0;
  1167. }
  1168. uip_connr->nrtx = 0;
  1169. apprexmit:
  1170. uip_appdata = uip_sappdata;
  1171. /* If the application has data to be sent, or if the incoming
  1172. packet had new data in it, we must send out a packet. */
  1173. if(uip_slen > 0 && uip_connr->len > 0) {
  1174. /* Add the length of the IP and TCP headers. */
  1175. uip_len = uip_connr->len + UIP_TCPIP_HLEN;
  1176. /* We always set the ACK flag in response packets. */
  1177. BUF->flags = TCP_ACK | TCP_PSH;
  1178. /* Send the packet. */
  1179. goto tcp_send_noopts;
  1180. }
  1181. /* If there is no data to send, just send out a pure ACK if
  1182. there is newdata. */
  1183. if(uip_flags & UIP_NEWDATA) {
  1184. uip_len = UIP_TCPIP_HLEN;
  1185. BUF->flags = TCP_ACK;
  1186. goto tcp_send_noopts;
  1187. }
  1188. }
  1189. goto drop;
  1190. case LAST_ACK:
  1191. /* We can close this connection if the peer has acknowledged our
  1192. FIN. This is indicated by the UIP_ACKDATA flag. */
  1193. if(uip_flags & UIP_ACKDATA) {
  1194. uip_connr->tcpstateflags = CLOSED;
  1195. uip_flags = UIP_CLOSE;
  1196. UIP_APPCALL();
  1197. }
  1198. break;
  1199. case FIN_WAIT_1:
  1200. /* The application has closed the connection, but the remote host
  1201. hasn't closed its end yet. Thus we do nothing but wait for a
  1202. FIN from the other side. */
  1203. if(uip_len > 0) {
  1204. uip_add_rcv_nxt(uip_len);
  1205. }
  1206. if(BUF->flags & TCP_FIN) {
  1207. if(uip_flags & UIP_ACKDATA) {
  1208. uip_connr->tcpstateflags = TIME_WAIT;
  1209. uip_connr->timer = 0;
  1210. uip_connr->len = 0;
  1211. } else {
  1212. uip_connr->tcpstateflags = CLOSING;
  1213. }
  1214. uip_add_rcv_nxt(1);
  1215. uip_flags = UIP_CLOSE;
  1216. UIP_APPCALL();
  1217. goto tcp_send_ack;
  1218. } else if(uip_flags & UIP_ACKDATA) {
  1219. uip_connr->tcpstateflags = FIN_WAIT_2;
  1220. uip_connr->len = 0;
  1221. goto drop;
  1222. }
  1223. if(uip_len > 0) {
  1224. goto tcp_send_ack;
  1225. }
  1226. goto drop;
  1227. case FIN_WAIT_2:
  1228. if(uip_len > 0) {
  1229. uip_add_rcv_nxt(uip_len);
  1230. }
  1231. if(BUF->flags & TCP_FIN) {
  1232. uip_connr->tcpstateflags = TIME_WAIT;
  1233. uip_connr->timer = 0;
  1234. uip_add_rcv_nxt(1);
  1235. uip_flags = UIP_CLOSE;
  1236. UIP_APPCALL();
  1237. goto tcp_send_ack;
  1238. }
  1239. if(uip_len > 0) {
  1240. goto tcp_send_ack;
  1241. }
  1242. goto drop;
  1243. case TIME_WAIT:
  1244. goto tcp_send_ack;
  1245. case CLOSING:
  1246. if(uip_flags & UIP_ACKDATA) {
  1247. uip_connr->tcpstateflags = TIME_WAIT;
  1248. uip_connr->timer = 0;
  1249. }
  1250. }
  1251. goto drop;
  1252. /* We jump here when we are ready to send the packet, and just want
  1253. to set the appropriate TCP sequence numbers in the TCP header. */
  1254. tcp_send_ack:
  1255. BUF->flags = TCP_ACK;
  1256. tcp_send_nodata:
  1257. uip_len = 40;
  1258. tcp_send_noopts:
  1259. BUF->tcpoffset = 5 << 4;
  1260. tcp_send:
  1261. /* We're done with the input processing. We are now ready to send a
  1262. reply. Our job is to fill in all the fields of the TCP and IP
  1263. headers before calculating the checksum and finally send the
  1264. packet. */
  1265. BUF->ackno[0] = uip_connr->rcv_nxt[0];
  1266. BUF->ackno[1] = uip_connr->rcv_nxt[1];
  1267. BUF->ackno[2] = uip_connr->rcv_nxt[2];
  1268. BUF->ackno[3] = uip_connr->rcv_nxt[3];
  1269. BUF->seqno[0] = uip_connr->snd_nxt[0];
  1270. BUF->seqno[1] = uip_connr->snd_nxt[1];
  1271. BUF->seqno[2] = uip_connr->snd_nxt[2];
  1272. BUF->seqno[3] = uip_connr->snd_nxt[3];
  1273. BUF->proto = UIP_PROTO_TCP;
  1274. BUF->srcport = uip_connr->lport;
  1275. BUF->destport = uip_connr->rport;
  1276. BUF->srcipaddr[0] = uip_hostaddr[0];
  1277. BUF->srcipaddr[1] = uip_hostaddr[1];
  1278. BUF->destipaddr[0] = uip_connr->ripaddr[0];
  1279. BUF->destipaddr[1] = uip_connr->ripaddr[1];
  1280. if(uip_connr->tcpstateflags & UIP_STOPPED) {
  1281. /* If the connection has issued uip_stop(), we advertise a zero
  1282. window so that the remote host will stop sending data. */
  1283. BUF->wnd[0] = BUF->wnd[1] = 0;
  1284. } else {
  1285. BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);
  1286. BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);
  1287. }
  1288. tcp_send_noconn:
  1289. BUF->len[0] = (uip_len >> 8);
  1290. BUF->len[1] = (uip_len & 0xff);
  1291. /* Calculate TCP checksum. */
  1292. BUF->tcpchksum = 0;
  1293. BUF->tcpchksum = ~(uip_tcpchksum());
  1294. ip_send_nolen:
  1295. BUF->vhl = 0x45;
  1296. BUF->tos = 0;
  1297. BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
  1298. BUF->ttl = UIP_TTL;
  1299. ++ipid;
  1300. BUF->ipid[0] = ipid >> 8;
  1301. BUF->ipid[1] = ipid & 0xff;
  1302. /* Calculate IP checksum. */
  1303. BUF->ipchksum = 0;
  1304. BUF->ipchksum = ~(uip_ipchksum());
  1305. UIP_STAT(++uip_stat.tcp.sent);
  1306. send:
  1307. UIP_STAT(++uip_stat.ip.sent);
  1308. /* Return and let the caller do the actual transmission. */
  1309. return;
  1310. drop:
  1311. uip_len = 0;
  1312. return;
  1313. }
  1314. /** @} */