tcpout.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (C) 2001-2005 by egnite Software GmbH
  3. * Copyright (c) 1993 by Digital Equipment Corporation
  4. * Copyright (c) 1983, 1993 by The Regents of the University of California
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file net/tcpout.c
  38. * \brief TCP output functions.
  39. *
  40. * \verbatim
  41. * $Id: tcpout.c 4473 2012-08-20 15:12:45Z haraldkipp $
  42. * \endverbatim
  43. */
  44. #include <cfg/os.h>
  45. #include <string.h>
  46. #include <errno.h>
  47. #include <sys/heap.h>
  48. #include <sys/event.h>
  49. #include <sys/timer.h>
  50. #include <netinet/in.h>
  51. #include <netinet/ip.h>
  52. #include <netinet/icmp.h>
  53. #include <netinet/ip_icmp.h>
  54. #include <netinet/ipcsum.h>
  55. #include <sys/socket.h>
  56. #include <netinet/tcp.h>
  57. #include <sys/thread.h>
  58. #ifdef NUTDEBUG
  59. #include <net/netdebug.h>
  60. #endif
  61. /*!
  62. * \addtogroup xgTCP
  63. */
  64. /*!
  65. * \brief Initiate TCP segment transmission.
  66. *
  67. * Check the TCP socket status and send any segment waiting
  68. * for transmission.
  69. *
  70. * The function will not return until the data has been stored in the
  71. * network device hardware for transmission. If the device is not ready
  72. * for transmitting a new packet, the calling thread will be suspended
  73. * until the device becomes ready again.
  74. *
  75. * If the target host is connected through an Ethernet network and if
  76. * the hardware address of that host is currently unknown, an ARP
  77. * request is sent out and the function will block until a response
  78. * is received or an ARP timeout occurs.
  79. *
  80. * Segments containing data or SYN and FIN flags are added to a special
  81. * queue for unacknowledged segments and will be retransmitted by the
  82. * TCP timer thread, if not acknowledged by the remote within a specific
  83. * time. The state machine will remove these segments from the queue
  84. * as soon as they are acknowledged.
  85. *
  86. * \note This function is mainly used by the TCP state machine.
  87. * Applications typically do not call this function but
  88. * use NutTcpSend(), which is part of the TCP socket interface.
  89. *
  90. * \param sock Socket descriptor. This pointer must have been retrieved
  91. * by calling NutTcpCreateSocket().
  92. * \param data Pointer to TCP segment contents.
  93. * \param size TCP segment length.
  94. *
  95. * \return 0 on success, -1 otherwise. Returning 0 does not imply that
  96. * the data has been successfully delivered, because flow control
  97. * and retransmission is still handled in the background.
  98. */
  99. int NutTcpOutput(TCPSOCKET * sock, const uint8_t * data, uint16_t size)
  100. {
  101. NETBUF *nb;
  102. NETBUF *nb_clone = 0;
  103. TCPHDR *th;
  104. uint16_t csum;
  105. uint8_t hlen;
  106. /*
  107. * Check if anything to send at all.
  108. */
  109. if (size == 0
  110. && (sock->so_tx_flags & (SO_SYN | SO_FIN | SO_FORCE)) == 0)
  111. return 0;
  112. /*
  113. * Build TCP header. Add room for MAXSEG option if this is a
  114. * SYN segment.
  115. */
  116. hlen = sizeof(TCPHDR);
  117. if (sock->so_tx_flags & SO_SYN)
  118. hlen += 4;
  119. if ((nb = NutNetBufAlloc(0, NBAF_TRANSPORT, hlen)) == 0) {
  120. sock->so_last_error = ENOBUFS;
  121. return -1;
  122. }
  123. th = (TCPHDR *) nb->nb_tp.vp;
  124. th->th_sport = sock->so_local_port;
  125. th->th_dport = sock->so_remote_port;
  126. th->th_x2 = 0;
  127. th->th_off = hlen >> 2;
  128. sock->so_tx_flags &= ~SO_FORCE;
  129. /*
  130. * Process ACK flag.
  131. */
  132. th->th_seq = htonl(sock->so_tx_nxt);
  133. if (sock->so_tx_flags & SO_ACK) {
  134. th->th_flags = TH_ACK;
  135. sock->so_tx_flags &= ~SO_ACK;
  136. th->th_ack = htonl(sock->so_rx_nxt);
  137. } else {
  138. th->th_flags = 0;
  139. th->th_ack = 0;
  140. }
  141. /*
  142. * Any SYN is sent first. Add options too. We rely on the caller
  143. * not to send a SYN segment with data, because this may break
  144. * some old stacks.
  145. */
  146. if (sock->so_tx_flags & SO_SYN) {
  147. uint8_t *cp;
  148. uint16_t n_mss = htons(sock->so_mss);
  149. th->th_flags |= TH_SYN;
  150. sock->so_tx_flags &= ~SO_SYN;
  151. sock->so_tx_nxt++;
  152. cp = (uint8_t *) (th + 1);
  153. *cp++ = TCPOPT_MAXSEG;
  154. *cp++ = TCPOLEN_MAXSEG;
  155. *cp++ = *(uint8_t *)&n_mss;
  156. *cp = *((uint8_t *)(&n_mss) + 1);
  157. }
  158. /*
  159. * Next preference is sending data. Set PUSH flag.
  160. */
  161. else if (size) {
  162. if ((nb = NutNetBufAlloc(nb, NBAF_APPLICATION, size)) == 0) {
  163. sock->so_last_error = ENOBUFS;
  164. return -1;
  165. }
  166. memcpy(nb->nb_ap.vp, (void *)data, size);
  167. sock->so_tx_nxt += size;
  168. th->th_flags |= TH_PUSH;
  169. }
  170. /*
  171. * If all data sent, transmit any waiting FIN.
  172. */
  173. else if (sock->so_tx_flags & SO_FIN) {
  174. th->th_flags |= TH_FIN;
  175. sock->so_tx_flags &= ~SO_FIN;
  176. sock->so_tx_nxt++;
  177. //@@@printf ("[%04X]TcpOutput: sending FIN\n", (u_short) sock);
  178. }
  179. /*
  180. * We close our receiver window, if it is
  181. * below the maximum segment size.
  182. */
  183. if (sock->so_rx_win < sock->so_mss)
  184. th->th_win = 0;
  185. else
  186. th->th_win = htons(sock->so_rx_win);
  187. th->th_sum = 0;
  188. th->th_urp = 0;
  189. /*
  190. * Calculate TCP checksum.
  191. */
  192. csum =
  193. NutIpPseudoChkSumPartial(sock->so_local_addr, sock->so_remote_addr,
  194. IPPROTO_TCP,
  195. htons(nb->nb_tp.sz + nb->nb_ap.sz));
  196. csum = NutIpChkSumPartial(csum, th, nb->nb_tp.sz);
  197. th->th_sum = NutIpChkSum(csum, nb->nb_ap.vp, nb->nb_ap.sz);
  198. #ifdef NUTDEBUG
  199. if (__tcp_trf)
  200. NutDumpTcpHeader(__tcp_trs, "OUT", sock, nb);
  201. #endif
  202. /*
  203. * To avoid a race condition in tcp state machine, the segment is first
  204. * appended to the transmission que, and then sent to the network.
  205. */
  206. /*
  207. * Append the segment to our transmission queue.
  208. */
  209. //@@@printf ("[%04X]TcpOutput: size: %u, flags: %u\n", (u_short) sock, size, th->th_flags);
  210. if (size || ((th->th_flags & (TH_FIN | TH_SYN)))) {
  211. //@@@printf ("[%04X]TcpOutput: appending nb to queue\n", (u_short) sock);
  212. NETBUF *nbp;
  213. nb->nb_next = 0;
  214. if ((nbp = sock->so_tx_nbq) == 0) {
  215. sock->so_tx_nbq = nb;
  216. /*
  217. * First entry, so initialize our retransmission timer.
  218. * It is also set at various places in the state machine,
  219. * but here is the best central point to do it. We may
  220. * carefully check later, if we can remove some in the
  221. * state machine.
  222. */
  223. sock->so_retran_time = (uint16_t) NutGetMillis() | 1;
  224. }
  225. else {
  226. while (nbp->nb_next)
  227. nbp = nbp->nb_next;
  228. nbp->nb_next = nb;
  229. }
  230. if (sock->so_rtt_seq == 0)
  231. sock->so_rtt_seq = ntohl (th->th_seq);
  232. nb_clone = NutNetBufClonePart(nb, 0);
  233. if (nb_clone == NULL) {
  234. sock->so_last_error = ENOBUFS;
  235. return -1;
  236. }
  237. }
  238. else
  239. nb_clone = nb;
  240. /*
  241. * IP output might fail because of routing, ARP or network device
  242. * problems or because the system ran out of memory.
  243. */
  244. if (NutIpOutput(IPPROTO_TCP, sock->so_remote_addr, nb_clone))
  245. return -1;
  246. NutNetBufFree (nb_clone);
  247. return 0;
  248. }
  249. /*!
  250. * \brief Reject an incoming segment.
  251. *
  252. * Send RST in response to an incoming segment, which should
  253. * be rejected.
  254. *
  255. * The function avoids to send out a RST segment in response to
  256. * an incoming RST segment.
  257. *
  258. * \note This function is mainly used by the TCP state machine.
  259. * Applications typically do not call this function.
  260. *
  261. * \param nb Network buffer structure of the incoming segment.
  262. * Will be released within this function.
  263. *
  264. * \return 0 on success, -1 otherwise.
  265. */
  266. int NutTcpReject(NETBUF * nb)
  267. {
  268. uint16_t csum;
  269. IPHDR *ih = (IPHDR *) nb->nb_nw.vp;
  270. TCPHDR *th = (TCPHDR *) nb->nb_tp.vp;
  271. /*
  272. * Never send RST in response to RST.
  273. */
  274. if (th->th_flags & TH_RST) {
  275. NutNetBufFree(nb);
  276. return 0;
  277. }
  278. /*
  279. * Remove any application data and TCP header options.
  280. */
  281. nb->nb_ap.sz = 0;
  282. nb->nb_tp.sz = sizeof(TCPHDR);
  283. /*
  284. * Swap ports.
  285. */
  286. csum = th->th_sport;
  287. th->th_sport = th->th_dport;
  288. th->th_dport = csum;
  289. /*
  290. * If the incoming segment has an ACK field, the reset
  291. * takes its sequence number from the ACK field of the
  292. * segment, otherwise the reset has sequence number zero
  293. * and the ACK field is set to the sum of the sequence
  294. * number and segment length of the incoming segment.
  295. */
  296. if (th->th_flags & TH_ACK) {
  297. th->th_flags = TH_RST;
  298. th->th_seq = th->th_ack;
  299. th->th_ack = 0;
  300. } else {
  301. if (th->th_flags & TH_SYN)
  302. th->th_ack = htonl(ntohl(th->th_seq) + 1);
  303. else
  304. th->th_ack = th->th_seq;
  305. th->th_seq = 0;
  306. th->th_flags = TH_RST | TH_ACK;
  307. }
  308. th->th_x2 = 0;
  309. th->th_off = sizeof(TCPHDR) / 4;
  310. th->th_win = 0;
  311. th->th_urp = 0;
  312. /*
  313. * Calculate TCP checksum without application data.
  314. */
  315. th->th_sum = 0;
  316. csum =
  317. NutIpPseudoChkSumPartial(ih->ip_dst, ih->ip_src, IPPROTO_TCP,
  318. htons(nb->nb_tp.sz));
  319. th->th_sum = NutIpChkSum(csum, th, nb->nb_tp.sz);
  320. /*
  321. * Sent segment back to the source.
  322. */
  323. #ifdef NUTDEBUG
  324. if (__tcp_trf)
  325. NutDumpTcpHeader(__tcp_trs, "REJ", 0, nb);
  326. #endif
  327. if(NutIpOutput(IPPROTO_TCP, ih->ip_src, nb) == 0)
  328. NutNetBufFree(nb);
  329. return 0;
  330. }