sixlowpan_tcpsend.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /****************************************************************************
  2. * net/sixlowpan/sixlowpan_tcpsend.c
  3. *
  4. * Copyright (C) 2017-2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * 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
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <string.h>
  40. #include <assert.h>
  41. #include <errno.h>
  42. #include <debug.h>
  43. #include "nuttx/semaphore.h"
  44. #include "nuttx/net/netdev.h"
  45. #include "nuttx/net/radiodev.h"
  46. #include "nuttx/net/netstats.h"
  47. #include "netdev/netdev.h"
  48. #include "devif/devif.h"
  49. #include "socket/socket.h"
  50. #include "inet/inet.h"
  51. #include "icmpv6/icmpv6.h"
  52. #include "tcp/tcp.h"
  53. #include "utils/utils.h"
  54. #include "sixlowpan/sixlowpan_internal.h"
  55. #if defined(CONFIG_NET_6LOWPAN) && defined(CONFIG_NET_TCP)
  56. /****************************************************************************
  57. * Pre-processor Definitions
  58. ****************************************************************************/
  59. /* Buffer access helpers */
  60. #define TCPBUF(dev) ((FAR struct tcp_hdr_s *)(&(dev)->d_buf[IPv6_HDRLEN]))
  61. /****************************************************************************
  62. * Pre-processor Definitions
  63. ****************************************************************************/
  64. /* These are temporary stubs. Something like this would be needed to
  65. * monitor the health of a IPv6 neighbor.
  66. */
  67. #define neighbor_reachable(dev)
  68. #define neighbor_notreachable(dev)
  69. /****************************************************************************
  70. * Private Types
  71. ****************************************************************************/
  72. /* This is the state data provided to the send event handler. No actions
  73. * can be taken until the until we receive the TX poll, then we can call
  74. * sixlowpan_queue_frames() with this data strurcture.
  75. */
  76. struct sixlowpan_send_s
  77. {
  78. FAR struct socket *s_sock; /* Internal socket reference */
  79. FAR struct devif_callback_s *s_cb; /* Reference to callback instance */
  80. sem_t s_waitsem; /* Supports waiting for driver events */
  81. int s_result; /* The result of the transfer */
  82. uint16_t s_timeout; /* Send timeout in deciseconds */
  83. clock_t s_time; /* Last send time for determining timeout */
  84. FAR const struct netdev_varaddr_s *s_destmac; /* Destination MAC address */
  85. FAR const uint8_t *s_buf; /* Data to send */
  86. size_t s_buflen; /* Length of data in buf */
  87. ssize_t s_sent; /* The number of bytes sent */
  88. uint32_t s_isn; /* Initial sequence number */
  89. uint32_t s_acked; /* The number of bytes acked */
  90. };
  91. /****************************************************************************
  92. * Private Functions
  93. ****************************************************************************/
  94. /****************************************************************************
  95. * Name: sixlowpan_tcp_chksum
  96. *
  97. * Description:
  98. * Perform the checksum calcaultion over the IPv6, protocol headers, and
  99. * data payload as necessary.
  100. *
  101. * Input Parameters:
  102. * ipv6tcp - A reference to a structure containing the IPv6 and TCP headers.
  103. * buf - The beginning of the payload data
  104. * buflen - The length of the payload data.
  105. *
  106. * Returned Value:
  107. * The calculated checksum
  108. *
  109. ****************************************************************************/
  110. static uint16_t sixlowpan_tcp_chksum(FAR const struct ipv6tcp_hdr_s *ipv6tcp,
  111. FAR const uint8_t *buf, uint16_t buflen)
  112. {
  113. uint16_t upperlen;
  114. uint16_t tcplen;
  115. uint16_t sum;
  116. /* The length reported in the IPv6 header is the length of the payload
  117. * that follows the header.
  118. */
  119. upperlen = ((uint16_t)ipv6tcp->ipv6.len[0] << 8) + ipv6tcp->ipv6.len[1];
  120. /* Verify some minimal assumptions */
  121. if (upperlen > CONFIG_NET_6LOWPAN_PKTSIZE)
  122. {
  123. return 0;
  124. }
  125. /* The checksum is calculated starting with a pseudo-header of IPv6 header
  126. * fields according to the IPv6 standard, which consists of the source
  127. * and destination addresses, the packet length and the next header field.
  128. */
  129. sum = upperlen + ipv6tcp->ipv6.proto;
  130. /* Sum IP source and destination addresses. */
  131. sum = chksum(sum, (FAR uint8_t *)ipv6tcp->ipv6.srcipaddr,
  132. 2 * sizeof(net_ipv6addr_t));
  133. /* Sum the TCP header
  134. *
  135. * The TCP header length is encoded in the top 4 bits of the tcpoffset
  136. * field (in units of 32-bit words).
  137. */
  138. tcplen = ((uint16_t)ipv6tcp->tcp.tcpoffset >> 4) << 2;
  139. sum = chksum(sum, (FAR uint8_t *)&ipv6tcp->tcp, tcplen);
  140. /* Sum payload data. */
  141. sum = chksum(sum, buf, buflen);
  142. return (sum == 0) ? 0xffff : htons(sum);
  143. }
  144. /****************************************************************************
  145. * Name: sixlowpan_tcp_header
  146. *
  147. * Description:
  148. * sixlowpan_tcp_header() will construct the IPv6 and TCP headers
  149. *
  150. * Input Parameters:
  151. * conn - An instance of the TCP connection structure.
  152. * dev - The network device that will route the packet
  153. * buf - Data to send
  154. * bulen - Length of data to send
  155. * ipv6tcp - The location to save the IPv6 + TCP header
  156. *
  157. * Returned Value:
  158. * Zero (OK) is returned on success; a negated errno value is returned on
  159. * failure.
  160. *
  161. * Assumptions:
  162. * Called with the network locked.
  163. *
  164. ****************************************************************************/
  165. static int sixlowpan_tcp_header(FAR struct tcp_conn_s *conn,
  166. FAR struct net_driver_s *dev,
  167. FAR const void *buf, size_t buflen,
  168. FAR struct ipv6tcp_hdr_s *ipv6tcp)
  169. {
  170. uint16_t iplen;
  171. /* Initialize the IPv6/TCP headers */
  172. ipv6tcp->ipv6.vtc = 0x60;
  173. ipv6tcp->ipv6.tcf = 0x00;
  174. ipv6tcp->ipv6.flow = 0x00;
  175. ipv6tcp->ipv6.proto = IP_PROTO_TCP;
  176. ipv6tcp->ipv6.ttl = IP_TTL;
  177. /* The IPv6 header length field does not include the size of IPv6 IP
  178. * header.
  179. */
  180. iplen = buflen + TCP_HDRLEN;
  181. ipv6tcp->ipv6.len[0] = (iplen >> 8);
  182. ipv6tcp->ipv6.len[1] = (iplen & 0xff);
  183. /* Copy the source and destination addresses */
  184. net_ipv6addr_hdrcopy(ipv6tcp->ipv6.destipaddr, conn->u.ipv6.raddr);
  185. if (!net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr))
  186. {
  187. net_ipv6addr_hdrcopy(ipv6tcp->ipv6.srcipaddr, conn->u.ipv6.laddr);
  188. }
  189. else
  190. {
  191. net_ipv6addr_hdrcopy(ipv6tcp->ipv6.srcipaddr, dev->d_ipv6addr);
  192. }
  193. ninfo("IPv6 length: %d\n",
  194. ((int)ipv6tcp->ipv6.len[0] << 8) + ipv6tcp->ipv6.len[1]);
  195. #ifdef CONFIG_NET_STATISTICS
  196. g_netstats.ipv6.sent++;
  197. #endif
  198. /* Initialize the TCP header */
  199. ipv6tcp->tcp.srcport = conn->lport; /* Local port */
  200. ipv6tcp->tcp.destport = conn->rport; /* Connected remote port */
  201. ipv6tcp->tcp.tcpoffset = (TCP_HDRLEN / 4) << 4; /* No optdata */
  202. ipv6tcp->tcp.flags = TCP_ACK | TCP_PSH; /* No urgent data */
  203. ipv6tcp->tcp.urgp[0] = 0; /* No urgent data */
  204. ipv6tcp->tcp.urgp[1] = 0;
  205. /* Set the sequency number information */
  206. /* REVISIT: There is currently no wait for the data to be ACKed and,
  207. * hence, no mechanism to retransmit the packet.
  208. */
  209. memcpy(ipv6tcp->tcp.ackno, conn->rcvseq, 4); /* ACK number */
  210. memcpy(ipv6tcp->tcp.seqno, conn->sndseq, 4); /* Sequence number */
  211. /* Set the TCP window */
  212. if (conn->tcpstateflags & TCP_STOPPED)
  213. {
  214. /* If the connection has issued TCP_STOPPED, we advertise a zero
  215. * window so that the remote host will stop sending data.
  216. */
  217. ipv6tcp->tcp.wnd[0] = 0;
  218. ipv6tcp->tcp.wnd[1] = 0;
  219. }
  220. else
  221. {
  222. /* Update the TCP received window based on I/O buffer availability */
  223. uint16_t recvwndo = tcp_get_recvwindow(dev);
  224. /* Set the TCP Window */
  225. ipv6tcp->tcp.wnd[0] = recvwndo >> 8;
  226. ipv6tcp->tcp.wnd[1] = recvwndo & 0xff;
  227. }
  228. /* Calculate TCP checksum. */
  229. ipv6tcp->tcp.tcpchksum = 0;
  230. ipv6tcp->tcp.tcpchksum = ~sixlowpan_tcp_chksum(ipv6tcp, buf, buflen);
  231. ninfo("Outgoing TCP packet length: %d bytes\n", iplen + IPv6_HDRLEN);
  232. return OK;
  233. }
  234. /****************************************************************************
  235. * Name: send_timeout
  236. *
  237. * Description:
  238. * Check for send timeout.
  239. *
  240. * Input Parameters:
  241. * sinfo - Send state structure reference
  242. *
  243. * Returned Value:
  244. * TRUE:timeout FALSE:no timeout
  245. *
  246. * Assumptions:
  247. * The network is locked
  248. *
  249. ****************************************************************************/
  250. static inline bool send_timeout(FAR struct sixlowpan_send_s *sinfo)
  251. {
  252. /* Check for a timeout. Zero means none and, in that case, we will let
  253. * the send wait forever.
  254. */
  255. if (sinfo->s_timeout != 0)
  256. {
  257. /* Check if the configured timeout has elapsed */
  258. clock_t timeo_ticks = DSEC2TICK(sinfo->s_timeout);
  259. clock_t elapsed = clock_systimer() - sinfo->s_time;
  260. if (elapsed >= timeo_ticks)
  261. {
  262. return true;
  263. }
  264. }
  265. /* No timeout */
  266. return false;
  267. }
  268. /****************************************************************************
  269. * Name: tcp_send_eventhandler
  270. *
  271. * Description:
  272. * This function is called with the network locked to perform the actual
  273. * TCP send operation when polled by the lower, device interfacing layer.
  274. *
  275. * Input Parameters:
  276. * dev - The structure of the network driver that generated the event.
  277. * pvconn - The connection structure associated with the socket
  278. * pvpriv - The event handler's private data argument
  279. * flags - Set of events describing why the callback was invoked
  280. *
  281. * Returned Value:
  282. * None
  283. *
  284. * Assumptions:
  285. * The network is locked.
  286. *
  287. ****************************************************************************/
  288. static uint16_t tcp_send_eventhandler(FAR struct net_driver_s *dev,
  289. FAR void *pvconn,
  290. FAR void *pvpriv, uint16_t flags)
  291. {
  292. FAR struct sixlowpan_send_s *sinfo = (FAR struct sixlowpan_send_s *)pvpriv;
  293. FAR struct tcp_conn_s *conn = (FAR struct tcp_conn_s *)pvconn;
  294. struct ipv6tcp_hdr_s ipv6tcp;
  295. int ret;
  296. /* Verify that this is an IEEE802.15.4 network driver. */
  297. if (dev->d_lltype != NET_LL_IEEE802154 &&
  298. dev->d_lltype != NET_LL_PKTRADIO)
  299. {
  300. ninfo("Not a compatible network device\n");
  301. return flags;
  302. }
  303. /* The TCP socket is connected and, hence, should be bound to a device.
  304. * Make sure that the polling device is the one that we are bound to.
  305. */
  306. DEBUGASSERT(conn->dev != NULL);
  307. if (dev != conn->dev)
  308. {
  309. ninfo("Not the connected device\n");
  310. return flags;
  311. }
  312. /* Check if the IEEE802.15.4 network driver went down */
  313. if ((flags & NETDEV_DOWN) != 0)
  314. {
  315. nwarn("WARNING: Device is down\n");
  316. sinfo->s_result = -ENOTCONN;
  317. goto end_wait;
  318. }
  319. ninfo("flags: %04x acked: %u sent: %u\n",
  320. flags, sinfo->s_acked, sinfo->s_sent);
  321. /* If this packet contains an acknowledgement, then update the count of
  322. * acknowledged bytes.
  323. */
  324. if ((flags & TCP_ACKDATA) != 0)
  325. {
  326. FAR struct tcp_hdr_s *tcp = TCPBUF(dev);
  327. #ifdef CONFIG_NET_SOCKOPTS
  328. /* Update the timeout */
  329. sinfo->s_time = clock_systimer();
  330. #endif
  331. /* The current acknowledgement number number is the (relative) offset
  332. * of the of the next byte needed by the receiver. The s_isn is the
  333. * offset of the first byte to send to the receiver. The difference
  334. * is the number of bytes to be acknowledged.
  335. */
  336. sinfo->s_acked = tcp_getsequence(tcp->ackno) - sinfo->s_isn;
  337. ninfo("ACK: acked=%d sent=%d buflen=%d\n",
  338. sinfo->s_acked, sinfo->s_sent, sinfo->s_buflen);
  339. /* Have all of the bytes in the buffer been sent and acknowledged? */
  340. if (sinfo->s_acked >= sinfo->s_buflen)
  341. {
  342. /* Yes. Then sinfo->s_buflen should hold the number of bytes
  343. * actually sent.
  344. */
  345. sinfo->s_result = sinfo->s_sent;
  346. goto end_wait;
  347. }
  348. /* No.. fall through to send more data if necessary */
  349. }
  350. /* Check if we are being asked to retransmit data */
  351. else if ((flags & TCP_REXMIT) != 0)
  352. {
  353. /* Yes.. in this case, reset the number of bytes that have been sent
  354. * to the number of bytes that have been ACKed.
  355. */
  356. sinfo->s_sent = sinfo->s_acked;
  357. /* Fall through to re-send data from the last that was ACKed */
  358. }
  359. /* Check for a loss of connection */
  360. else if ((flags & TCP_DISCONN_EVENTS) != 0)
  361. {
  362. FAR struct socket *psock = sinfo->s_sock;
  363. nwarn("WARNING: Lost connection\n");
  364. /* We could get here recursively through the callback actions of
  365. * tcp_lost_connection(). So don't repeat that action if we have
  366. * already been disconnected.
  367. */
  368. DEBUGASSERT(psock != NULL);
  369. if (_SS_ISCONNECTED(psock->s_flags))
  370. {
  371. /* Report the disconnection event to all socket clones */
  372. tcp_lost_connection(psock, sinfo->s_cb, flags);
  373. }
  374. /* Report not connected to the sender */
  375. sinfo->s_result = -ENOTCONN;
  376. goto end_wait;
  377. }
  378. /* Check if the outgoing packet is available (it may have been claimed
  379. * by a sendto event handler serving a different thread).
  380. */
  381. #if 0 /* We can't really support multiple senders on the same TCP socket */
  382. else if (dev->d_sndlen > 0)
  383. {
  384. /* Another thread has beat us sending data, wait for the next poll */
  385. return flags;
  386. }
  387. #endif
  388. /* We get here if (1) not all of the data has been ACKed, (2) we have been
  389. * asked to retransmit data, (3) the connection is still healthy, and (4)
  390. * the outgoing packet is available for our use. In this case, we are
  391. * now free to send more data to receiver -- UNLESS the buffer contains
  392. * unprocessed incoming data. In that event, we will have to wait for the
  393. * next polling cycle.
  394. */
  395. if ((flags & WPAN_NEWDATA) == 0 && sinfo->s_sent < sinfo->s_buflen)
  396. {
  397. uint32_t seqno;
  398. uint16_t winleft;
  399. uint16_t sndlen;
  400. /* Get the amount of TCP payload data that we can send in the next
  401. * packet.
  402. */
  403. sndlen = sinfo->s_buflen - sinfo->s_sent;
  404. if (sndlen > conn->mss)
  405. {
  406. sndlen = conn->mss;
  407. }
  408. winleft = conn->winsize - sinfo->s_sent + sinfo->s_acked;
  409. if (sndlen > winleft)
  410. {
  411. sndlen = winleft;
  412. }
  413. ninfo("s_buflen=%u s_sent=%u mss=%u winsize=%u sndlen=%d\n",
  414. sinfo->s_buflen, sinfo->s_sent, conn->mss, conn->winsize, sndlen);
  415. if (sndlen > 0)
  416. {
  417. /* Set the sequence number for this packet. NOTE: The network updates
  418. * sndseq on receipt of ACK *before* this function is called. In that
  419. * case sndseq will point to the next unacknowledged byte (which might
  420. * have already been sent). We will overwrite the value of sndseq
  421. * here before the packet is sent.
  422. */
  423. seqno = sinfo->s_sent + sinfo->s_isn;
  424. ninfo("Sending: sndseq %08lx->%08x\n",
  425. (unsigned long)tcp_getsequence(conn->sndseq), seqno);
  426. tcp_setsequence(conn->sndseq, seqno);
  427. /* Create the IPv6 + TCP header */
  428. ret = sixlowpan_tcp_header(conn, dev, &sinfo->s_buf[sinfo->s_sent],
  429. sndlen, &ipv6tcp);
  430. if (ret < 0)
  431. {
  432. nerr("ERROR: sixlowpan_tcp_header failed: %d\n", ret);
  433. sinfo->s_result = ret;
  434. goto end_wait;
  435. }
  436. /* Transfer the frame list to the IEEE802.15.4 MAC device */
  437. ret = sixlowpan_queue_frames((FAR struct radio_driver_s *)dev,
  438. &ipv6tcp.ipv6,
  439. &sinfo->s_buf[sinfo->s_sent], sndlen,
  440. sinfo->s_destmac);
  441. if (ret < 0)
  442. {
  443. nerr("ERROR: sixlowpan_queue_frames failed: %d\n", ret);
  444. sinfo->s_result = ret;
  445. goto end_wait;
  446. }
  447. /* Increment the count of bytes sent, the number of unacked bytes,
  448. * and the total count of TCP packets sent.
  449. *
  450. * NOTE: tcp_appsend() normally increments conn->unacked based on
  451. * the value of dev->d_sndlen. However, dev->d_len is always
  452. * zero for 6LoWPAN since it does not send via the dev->d_buf
  453. * but, rather, uses a backdoor frame interface with the IEEE
  454. * 802.15.4 MAC.
  455. */
  456. sinfo->s_sent += sndlen;
  457. conn->unacked += sndlen;
  458. #ifdef CONFIG_NET_TCP_WRITE_BUFFERS
  459. /* For compability with buffered send logic */
  460. conn->sndseq_max = tcp_addsequence(conn->sndseq, conn->unacked);
  461. #endif
  462. #ifdef CONFIG_NET_STATISTICS
  463. g_netstats.tcp.sent++;
  464. #endif
  465. ninfo("Sent: acked=%d sent=%d buflen=%d unacked=%d\n",
  466. sinfo->s_acked, sinfo->s_sent, sinfo->s_buflen,
  467. conn->unacked);
  468. }
  469. }
  470. #ifdef CONFIG_NET_SOCKOPTS
  471. /* All data has been sent and we are just waiting for ACK or re-transmit
  472. * indications to complete the send. Check for a timeout.
  473. */
  474. if (send_timeout(sinfo))
  475. {
  476. /* Yes.. report the timeout */
  477. nwarn("WARNING: SEND timeout\n");
  478. sinfo->s_sent = -ETIMEDOUT;
  479. goto end_wait;
  480. }
  481. #endif /* CONFIG_NET_SOCKOPTS */
  482. /* Continue waiting */
  483. return flags;
  484. end_wait:
  485. /* Do not allow any further callbacks */
  486. sinfo->s_cb->flags = 0;
  487. sinfo->s_cb->priv = NULL;
  488. sinfo->s_cb->event = NULL;
  489. /* There are no outstanding, unacknowledged bytes */
  490. conn->unacked = 0;
  491. /* Wake up the waiting thread */
  492. nxsem_post(&sinfo->s_waitsem);
  493. return flags;
  494. }
  495. /****************************************************************************
  496. * Name: sixlowpan_send_packet
  497. *
  498. * Description:
  499. * Process an outgoing TCP packet. Takes an IP packet and formats it to
  500. * be sent on an 802.15.4 network using 6lowpan. Called from common TCP
  501. * send logic.
  502. *
  503. * The payload data is in the caller 'buf' and is of length 'buflen'.
  504. * Compressed headers will be added and if necessary the packet is
  505. * fragmented. The resulting packet/fragments are submitted to the MAC
  506. * via the network driver r_req_data method.
  507. *
  508. * Input Parameters:
  509. * psock - An instance of the internal socket structure.
  510. * dev - The IEEE802.15.4 MAC network driver interface.
  511. * conn - TCP connection structure
  512. * buf - Data to send
  513. * len - Length of data to send
  514. * destmac - The IEEE802.15.4 MAC address of the destination
  515. * timeout - Send timeout in deciseconds
  516. *
  517. * Returned Value:
  518. * Ok is returned on success; Othewise a negated errno value is returned.
  519. * This function is expected to fail if the driver is not an IEEE802.15.4
  520. * MAC network driver. In that case, the logic will fall back to normal
  521. * IPv4/IPv6 formatting.
  522. *
  523. * Assumptions:
  524. * Called with the network locked.
  525. *
  526. ****************************************************************************/
  527. static int sixlowpan_send_packet(FAR struct socket *psock,
  528. FAR struct net_driver_s *dev,
  529. FAR struct tcp_conn_s *conn,
  530. FAR const uint8_t *buf, size_t len,
  531. FAR const struct netdev_varaddr_s *destmac,
  532. uint16_t timeout)
  533. {
  534. struct sixlowpan_send_s sinfo;
  535. ninfo("len=%lu timeout=%u\n", (unsigned long)len, timeout);
  536. DEBUGASSERT(psock != NULL && dev != NULL && conn != NULL && buf != NULL &&
  537. destmac != NULL);
  538. memset(&sinfo, 0, sizeof(struct sixlowpan_send_s));
  539. net_lock();
  540. if (len > 0)
  541. {
  542. /* Allocate resources to receive a callback.
  543. *
  544. * The second parameter is NULL meaning that we can get only
  545. * device related events, no connect-related events.
  546. */
  547. sinfo.s_cb = tcp_callback_alloc(conn);
  548. if (sinfo.s_cb != NULL)
  549. {
  550. int ret;
  551. /* Initialize the send state structure */
  552. nxsem_init(&sinfo.s_waitsem, 0, 0);
  553. (void)nxsem_setprotocol(&sinfo.s_waitsem, SEM_PRIO_NONE);
  554. sinfo.s_sock = psock;
  555. sinfo.s_result = -EBUSY;
  556. sinfo.s_timeout = timeout;
  557. sinfo.s_time = clock_systimer();
  558. sinfo.s_destmac = destmac;
  559. sinfo.s_buf = buf;
  560. sinfo.s_buflen = len;
  561. /* Set up the initial sequence number */
  562. sinfo.s_isn = tcp_getsequence(conn->sndseq);
  563. /* Set up the callback in the connection */
  564. sinfo.s_cb->flags = (NETDEV_DOWN | TCP_ACKDATA | TCP_REXMIT |
  565. TCP_DISCONN_EVENTS | WPAN_POLL);
  566. sinfo.s_cb->priv = (FAR void *)&sinfo;
  567. sinfo.s_cb->event = tcp_send_eventhandler;
  568. /* There is no outstanding, unacknowledged data after this
  569. * initial sequence number.
  570. */
  571. conn->unacked = 0;
  572. /* Notify the IEEE802.15.4 MAC that we have data to send. */
  573. netdev_txnotify_dev(dev);
  574. /* Wait for the send to complete or an error to occur.
  575. * net_lockedwait will also terminate if a signal is received.
  576. */
  577. ninfo("Wait for send complete\n");
  578. ret = net_lockedwait(&sinfo.s_waitsem);
  579. if (ret < 0)
  580. {
  581. sinfo.s_result = ret;
  582. }
  583. /* Make sure that no further events are processed */
  584. tcp_callback_free(conn, sinfo.s_cb);
  585. }
  586. }
  587. nxsem_destroy(&sinfo.s_waitsem);
  588. net_unlock();
  589. return (sinfo.s_result < 0 ? sinfo.s_result : len);
  590. }
  591. /****************************************************************************
  592. * Public Functions
  593. ****************************************************************************/
  594. /****************************************************************************
  595. * Name: psock_6lowpan_tcp_send
  596. *
  597. * Description:
  598. * psock_6lowpan_tcp_send() call may be used only when the TCP socket is in a
  599. * connected state (so that the intended recipient is known).
  600. *
  601. * Input Parameters:
  602. * psock - An instance of the internal socket structure.
  603. * buf - Data to send
  604. * bulen - Length of data to send
  605. *
  606. * Returned Value:
  607. * On success, returns the number of characters sent. On error,
  608. * -1 is returned, and errno is set appropriately. Returned error numbers
  609. * must be consistent with definition of errors reported by send() or
  610. * sendto().
  611. *
  612. * Assumptions:
  613. * Called with the network locked.
  614. *
  615. ****************************************************************************/
  616. ssize_t psock_6lowpan_tcp_send(FAR struct socket *psock, FAR const void *buf,
  617. size_t buflen)
  618. {
  619. FAR struct tcp_conn_s *conn;
  620. FAR struct net_driver_s *dev;
  621. struct netdev_varaddr_s destmac;
  622. uint16_t timeout;
  623. int ret;
  624. ninfo("buflen %lu\n", (unsigned long)buflen);
  625. sixlowpan_dumpbuffer("Outgoing TCP payload", buf, buflen);
  626. DEBUGASSERT(psock != NULL && psock->s_crefs > 0);
  627. DEBUGASSERT(psock->s_type == SOCK_STREAM);
  628. /* Make sure that this is a valid socket */
  629. if (psock == NULL || psock->s_crefs <= 0)
  630. {
  631. nerr("ERROR: Invalid socket\n");
  632. return (ssize_t)-EBADF;
  633. }
  634. /* Make sure that this is a connected TCP socket */
  635. if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(psock->s_flags))
  636. {
  637. nerr("ERROR: Not connected\n");
  638. return (ssize_t)-ENOTCONN;
  639. }
  640. /* Get the underlying TCP connection structure */
  641. conn = (FAR struct tcp_conn_s *)psock->s_conn;
  642. DEBUGASSERT(conn != NULL);
  643. #ifdef CONFIG_NET_IPv4
  644. /* Ignore if not IPv6 domain */
  645. if (conn->domain != PF_INET6)
  646. {
  647. nwarn("WARNING: Not IPv6\n");
  648. return (ssize_t)-EPROTOTYPE;
  649. }
  650. #endif
  651. /* Route outgoing message to the correct device */
  652. dev = netdev_findby_ripv6addr(conn->u.ipv6.laddr, conn->u.ipv6.raddr);
  653. if (dev == NULL)
  654. {
  655. nwarn("WARNING: Not routable\n");
  656. return (ssize_t)-ENETUNREACH;
  657. }
  658. /* Some network devices support different link layer protocols.
  659. * Check if this device has the hooks to support 6LoWPAN.
  660. */
  661. if (dev->d_lltype != NET_LL_IEEE802154 &&
  662. dev->d_lltype != NET_LL_PKTRADIO)
  663. {
  664. nwarn("WARNING: Not a compatible network device\n");
  665. return (ssize_t)-ENONET;
  666. }
  667. #ifdef CONFIG_NET_ICMPv6_NEIGHBOR
  668. /* Make sure that the IP address mapping is in the Neighbor Table */
  669. ret = icmpv6_neighbor(conn->u.ipv6.raddr);
  670. if (ret < 0)
  671. {
  672. nerr("ERROR: Not reachable\n");
  673. return (ssize_t)-ENETUNREACH;
  674. }
  675. #endif
  676. /* Get the IEEE 802.15.4 MAC address of the next hop. */
  677. ret = sixlowpan_nexthopaddr((FAR struct radio_driver_s *)dev,
  678. conn->u.ipv6.raddr, &destmac);
  679. if (ret < 0)
  680. {
  681. nerr("ERROR: Failed to get dest MAC address: %d\n", ret);
  682. return (ssize_t)ret;
  683. }
  684. /* Set the socket state to sending */
  685. psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_SEND);
  686. /* Send the TCP packets, breaking down the potential large user buffer
  687. * into smaller packets that can be reassembled in the allocated MTU
  688. * packet buffer.
  689. */
  690. #ifdef CONFIG_NET_SOCKOPTS
  691. timeout = psock->s_sndtimeo;
  692. #else
  693. timeout = 0;
  694. #endif
  695. ret = sixlowpan_send_packet(psock, dev, conn, buf, buflen, &destmac,
  696. timeout);
  697. if (ret < 0)
  698. {
  699. nerr("ERROR: sixlowpan_send_packet() failed: %d\n", ret);
  700. psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_IDLE);
  701. return (ssize_t)ret;
  702. }
  703. /* Set the socket state to idle */
  704. psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_IDLE);
  705. return (ssize_t)buflen;
  706. }
  707. /****************************************************************************
  708. * Name: sixlowpan_tcp_send
  709. *
  710. * Description:
  711. * TCP output comes through three different mechansims. Either from:
  712. *
  713. * 1. TCP socket output. For the case of TCP output to an
  714. * IEEE802.15.4, the TCP output is caught in the socket
  715. * send()/sendto() logic and and redirected to psock_6lowpan_tcp_send().
  716. * 2. TCP output from the TCP state machine. That will occur
  717. * during TCP packet processing by the TCP state meachine.
  718. * 3. TCP output resulting from TX or timer polling
  719. *
  720. * Cases 2 and 3 will be handled here. Logic in ipv6_tcp_input(),
  721. * devif_poll(), and devif_timer() detect if (1) an attempt to return with
  722. * d_len > 0 and (2) that the device is an IEEE802.15.4 MAC network
  723. * driver. Under those conditions, this function will be called to create
  724. * the IEEE80215.4 frames.
  725. *
  726. * Input Parameters:
  727. * dev - The network device containing the packet to be sent.
  728. * fwddev - The network device used to send the data. This will be the
  729. * same device except for the IP forwarding case where packets
  730. * are sent across devices.
  731. * ipv6 - A pointer to the IPv6 header in dev->d_buf which lies AFTER
  732. * the L1 header. NOTE: dev->d_len must have been decremented
  733. * by the size of any preceding MAC header.
  734. *
  735. * Returned Value:
  736. * None
  737. *
  738. * Assumptions:
  739. * Called with the network locked.
  740. *
  741. ****************************************************************************/
  742. void sixlowpan_tcp_send(FAR struct net_driver_s *dev,
  743. FAR struct net_driver_s *fwddev,
  744. FAR struct ipv6_hdr_s *ipv6)
  745. {
  746. DEBUGASSERT(dev != NULL && dev->d_len > 0 && fwddev != NULL);
  747. /* Double check */
  748. ninfo("d_len %u\n", dev->d_len);
  749. sixlowpan_dumpbuffer("Outgoing TCP packet",
  750. (FAR const uint8_t *)ipv6, dev->d_len);
  751. if (dev != NULL && dev->d_len > 0 && fwddev != NULL)
  752. {
  753. FAR struct ipv6tcp_hdr_s *ipv6hdr;
  754. /* The IPv6 header followed by a TCP headers should lie at the
  755. * beginning of d_buf since there is no link layer protocol header
  756. * and the TCP state machine should only response with TCP packets.
  757. */
  758. ipv6hdr = (FAR struct ipv6tcp_hdr_s *)ipv6;
  759. /* The TCP data payload should follow the IPv6 header plus the
  760. * protocol header.
  761. */
  762. if (ipv6hdr->ipv6.proto != IP_PROTO_TCP)
  763. {
  764. nwarn("WARNING: Expected TCP protoype: %u vs %s\n",
  765. ipv6hdr->ipv6.proto, IP_PROTO_TCP);
  766. }
  767. else
  768. {
  769. struct netdev_varaddr_s destmac;
  770. FAR uint8_t *buf;
  771. uint16_t hdrlen;
  772. uint16_t buflen;
  773. int ret;
  774. /* Get the IEEE 802.15.4 MAC address of the next hop. */
  775. ret = sixlowpan_nexthopaddr((FAR struct radio_driver_s *)fwddev,
  776. ipv6hdr->ipv6.destipaddr, &destmac);
  777. if (ret < 0)
  778. {
  779. nerr("ERROR: Failed to get dest MAC address: %d\n", ret);
  780. goto drop;
  781. }
  782. /* Get the IPv6 + TCP combined header length. The size of the TCP
  783. * header is encoded in the top 4 bits of the tcpoffset field (in
  784. * units of 32-bit words).
  785. */
  786. hdrlen = IPv6_HDRLEN + (((uint16_t)ipv6hdr->tcp.tcpoffset >> 4) << 2);
  787. /* Drop the packet if the buffer length is less than this. */
  788. if (hdrlen > dev->d_len)
  789. {
  790. nwarn("WARNING: Dropping small TCP packet: %u < %u\n",
  791. buflen, hdrlen);
  792. }
  793. else
  794. {
  795. /* Convert the outgoing packet into a frame list. */
  796. buf = (FAR uint8_t *)ipv6 + hdrlen;
  797. buflen = dev->d_len - hdrlen;
  798. (void)sixlowpan_queue_frames(
  799. (FAR struct radio_driver_s *)fwddev,
  800. &ipv6hdr->ipv6, buf, buflen, &destmac);
  801. }
  802. }
  803. }
  804. drop:
  805. dev->d_len = 0;
  806. }
  807. #endif /* CONFIG_NET_6LOWPAN && CONFIG_NET_TCP */