uip.c 59 KB

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