2017-03-30 02:07:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/sixlowpan/sixlowpan_tcpsend.c
|
|
|
|
*
|
2018-07-01 04:50:07 +02:00
|
|
|
* Copyright (C) 2017-2018 Gregory Nutt. All rights reserved.
|
2017-03-30 02:07:52 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2017-04-02 21:27:53 +02:00
|
|
|
#include <string.h>
|
2017-03-30 02:07:52 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2020-02-01 08:17:32 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/radiodev.h>
|
|
|
|
#include <nuttx/net/netstats.h>
|
2017-03-30 02:07:52 +02:00
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
2017-06-25 00:00:41 +02:00
|
|
|
#include "devif/devif.h"
|
2017-03-30 02:07:52 +02:00
|
|
|
#include "socket/socket.h"
|
2017-08-06 22:48:19 +02:00
|
|
|
#include "inet/inet.h"
|
2017-07-02 19:04:57 +02:00
|
|
|
#include "icmpv6/icmpv6.h"
|
2017-03-30 02:07:52 +02:00
|
|
|
#include "tcp/tcp.h"
|
2017-04-02 22:30:01 +02:00
|
|
|
#include "utils/utils.h"
|
2017-03-30 02:07:52 +02:00
|
|
|
#include "sixlowpan/sixlowpan_internal.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_6LOWPAN) && defined(CONFIG_NET_TCP)
|
|
|
|
|
2017-04-04 20:31:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Buffer access helpers */
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
#define TCPBUF(dev) ((FAR struct tcp_hdr_s *)(&(dev)->d_buf[IPv6_HDRLEN]))
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* These are temporary stubs. Something like this would be needed to
|
|
|
|
* monitor the health of a IPv6 neighbor.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define neighbor_reachable(dev)
|
|
|
|
#define neighbor_notreachable(dev)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-06-23 22:09:06 +02:00
|
|
|
/* This is the state data provided to the send event handler. No actions
|
2017-06-25 00:00:41 +02:00
|
|
|
* can be taken until the until we receive the TX poll, then we can call
|
|
|
|
* sixlowpan_queue_frames() with this data strurcture.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct sixlowpan_send_s
|
|
|
|
{
|
|
|
|
FAR struct socket *s_sock; /* Internal socket reference */
|
|
|
|
FAR struct devif_callback_s *s_cb; /* Reference to callback instance */
|
|
|
|
sem_t s_waitsem; /* Supports waiting for driver events */
|
|
|
|
int s_result; /* The result of the transfer */
|
2017-07-31 03:10:51 +02:00
|
|
|
FAR const struct netdev_varaddr_s *s_destmac; /* Destination MAC address */
|
2017-06-25 00:00:41 +02:00
|
|
|
FAR const uint8_t *s_buf; /* Data to send */
|
|
|
|
size_t s_buflen; /* Length of data in buf */
|
|
|
|
ssize_t s_sent; /* The number of bytes sent */
|
|
|
|
uint32_t s_isn; /* Initial sequence number */
|
|
|
|
uint32_t s_acked; /* The number of bytes acked */
|
|
|
|
};
|
2017-04-04 20:31:22 +02:00
|
|
|
|
2017-04-02 22:30:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sixlowpan_tcp_chksum
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform the checksum calcaultion over the IPv6, protocol headers, and
|
|
|
|
* data payload as necessary.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ipv6tcp - A reference to a structure containing the IPv6 and TCP headers.
|
|
|
|
* buf - The beginning of the payload data
|
|
|
|
* buflen - The length of the payload data.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The calculated checksum
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
static uint16_t sixlowpan_tcp_chksum(FAR const struct ipv6tcp_hdr_s *ipv6tcp,
|
2017-04-02 22:30:01 +02:00
|
|
|
FAR const uint8_t *buf, uint16_t buflen)
|
|
|
|
{
|
|
|
|
uint16_t upperlen;
|
2017-04-08 01:04:57 +02:00
|
|
|
uint16_t tcplen;
|
2017-04-02 22:30:01 +02:00
|
|
|
uint16_t sum;
|
|
|
|
|
|
|
|
/* The length reported in the IPv6 header is the length of the payload
|
|
|
|
* that follows the header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
upperlen = ((uint16_t)ipv6tcp->ipv6.len[0] << 8) + ipv6tcp->ipv6.len[1];
|
|
|
|
|
|
|
|
/* Verify some minimal assumptions */
|
|
|
|
|
2018-07-04 22:10:40 +02:00
|
|
|
if (upperlen > CONFIG_NET_6LOWPAN_PKTSIZE)
|
2017-04-02 22:30:01 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The checksum is calculated starting with a pseudo-header of IPv6 header
|
|
|
|
* fields according to the IPv6 standard, which consists of the source
|
|
|
|
* and destination addresses, the packet length and the next header field.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sum = upperlen + ipv6tcp->ipv6.proto;
|
|
|
|
|
|
|
|
/* Sum IP source and destination addresses. */
|
|
|
|
|
|
|
|
sum = chksum(sum, (FAR uint8_t *)ipv6tcp->ipv6.srcipaddr,
|
|
|
|
2 * sizeof(net_ipv6addr_t));
|
|
|
|
|
2017-04-08 01:04:57 +02:00
|
|
|
/* Sum the TCP header
|
|
|
|
*
|
|
|
|
* The TCP header length is encoded in the top 4 bits of the tcpoffset
|
|
|
|
* field (in units of 32-bit words).
|
|
|
|
*/
|
2017-04-02 22:30:01 +02:00
|
|
|
|
2017-04-08 01:04:57 +02:00
|
|
|
tcplen = ((uint16_t)ipv6tcp->tcp.tcpoffset >> 4) << 2;
|
|
|
|
sum = chksum(sum, (FAR uint8_t *)&ipv6tcp->tcp, tcplen);
|
2017-04-02 22:30:01 +02:00
|
|
|
|
|
|
|
/* Sum payload data. */
|
|
|
|
|
|
|
|
sum = chksum(sum, buf, buflen);
|
|
|
|
return (sum == 0) ? 0xffff : htons(sum);
|
|
|
|
}
|
|
|
|
|
2017-06-24 17:48:41 +02:00
|
|
|
/****************************************************************************
|
2017-06-25 00:00:41 +02:00
|
|
|
* Name: sixlowpan_tcp_header
|
2017-06-24 17:48:41 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-06-25 00:00:41 +02:00
|
|
|
* sixlowpan_tcp_header() will construct the IPv6 and TCP headers
|
2017-06-24 17:48:41 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2017-06-24 17:48:41 +02:00
|
|
|
* conn - An instance of the TCP connection structure.
|
|
|
|
* dev - The network device that will route the packet
|
|
|
|
* buf - Data to send
|
|
|
|
* bulen - Length of data to send
|
2017-06-25 00:00:41 +02:00
|
|
|
* ipv6tcp - The location to save the IPv6 + TCP header
|
2017-06-24 17:48:41 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-06-25 00:00:41 +02:00
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* failure.
|
2017-06-24 17:48:41 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
static int sixlowpan_tcp_header(FAR struct tcp_conn_s *conn,
|
|
|
|
FAR struct net_driver_s *dev,
|
|
|
|
FAR const void *buf, size_t buflen,
|
|
|
|
FAR struct ipv6tcp_hdr_s *ipv6tcp)
|
2017-06-24 17:48:41 +02:00
|
|
|
{
|
|
|
|
uint16_t iplen;
|
2017-06-24 19:57:59 +02:00
|
|
|
|
2017-06-24 17:48:41 +02:00
|
|
|
/* Initialize the IPv6/TCP headers */
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
ipv6tcp->ipv6.vtc = 0x60;
|
|
|
|
ipv6tcp->ipv6.tcf = 0x00;
|
|
|
|
ipv6tcp->ipv6.flow = 0x00;
|
|
|
|
ipv6tcp->ipv6.proto = IP_PROTO_TCP;
|
|
|
|
ipv6tcp->ipv6.ttl = IP_TTL;
|
2017-06-24 17:48:41 +02:00
|
|
|
|
|
|
|
/* The IPv6 header length field does not include the size of IPv6 IP
|
|
|
|
* header.
|
|
|
|
*/
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
iplen = buflen + TCP_HDRLEN;
|
|
|
|
ipv6tcp->ipv6.len[0] = (iplen >> 8);
|
|
|
|
ipv6tcp->ipv6.len[1] = (iplen & 0xff);
|
2017-06-24 17:48:41 +02:00
|
|
|
|
|
|
|
/* Copy the source and destination addresses */
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
net_ipv6addr_hdrcopy(ipv6tcp->ipv6.destipaddr, conn->u.ipv6.raddr);
|
2018-06-23 20:53:27 +02:00
|
|
|
if (!net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr))
|
2017-06-24 19:57:59 +02:00
|
|
|
{
|
2017-06-25 00:00:41 +02:00
|
|
|
net_ipv6addr_hdrcopy(ipv6tcp->ipv6.srcipaddr, conn->u.ipv6.laddr);
|
2017-06-24 19:57:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-25 00:00:41 +02:00
|
|
|
net_ipv6addr_hdrcopy(ipv6tcp->ipv6.srcipaddr, dev->d_ipv6addr);
|
2017-06-24 19:57:59 +02:00
|
|
|
}
|
2017-06-24 17:48:41 +02:00
|
|
|
|
|
|
|
ninfo("IPv6 length: %d\n",
|
2017-06-25 00:00:41 +02:00
|
|
|
((int)ipv6tcp->ipv6.len[0] << 8) + ipv6tcp->ipv6.len[1]);
|
2017-06-24 17:48:41 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.ipv6.sent++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialize the TCP header */
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
ipv6tcp->tcp.srcport = conn->lport; /* Local port */
|
|
|
|
ipv6tcp->tcp.destport = conn->rport; /* Connected remote port */
|
2017-06-24 17:48:41 +02:00
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
ipv6tcp->tcp.tcpoffset = (TCP_HDRLEN / 4) << 4; /* No optdata */
|
2018-05-17 14:45:46 +02:00
|
|
|
ipv6tcp->tcp.flags = TCP_ACK | TCP_PSH; /* No urgent data */
|
2017-06-25 00:00:41 +02:00
|
|
|
ipv6tcp->tcp.urgp[0] = 0; /* No urgent data */
|
|
|
|
ipv6tcp->tcp.urgp[1] = 0;
|
2017-06-24 17:48:41 +02:00
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
/* Set the sequency number information.
|
|
|
|
*
|
|
|
|
* REVISIT: There is currently no wait for the data to be ACKed and,
|
2017-06-24 19:57:59 +02:00
|
|
|
* hence, no mechanism to retransmit the packet.
|
|
|
|
*/
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
memcpy(ipv6tcp->tcp.ackno, conn->rcvseq, 4); /* ACK number */
|
|
|
|
memcpy(ipv6tcp->tcp.seqno, conn->sndseq, 4); /* Sequence number */
|
2017-06-24 19:57:59 +02:00
|
|
|
|
|
|
|
/* Set the TCP window */
|
2017-06-24 17:48:41 +02:00
|
|
|
|
|
|
|
if (conn->tcpstateflags & TCP_STOPPED)
|
|
|
|
{
|
|
|
|
/* If the connection has issued TCP_STOPPED, we advertise a zero
|
|
|
|
* window so that the remote host will stop sending data.
|
|
|
|
*/
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
ipv6tcp->tcp.wnd[0] = 0;
|
|
|
|
ipv6tcp->tcp.wnd[1] = 0;
|
2017-06-24 17:48:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-01 04:50:07 +02:00
|
|
|
/* Update the TCP received window based on I/O buffer availability */
|
|
|
|
|
2018-07-01 18:05:15 +02:00
|
|
|
uint16_t recvwndo = tcp_get_recvwindow(dev);
|
2018-07-01 04:50:07 +02:00
|
|
|
|
|
|
|
/* Set the TCP Window */
|
|
|
|
|
2018-07-01 15:59:33 +02:00
|
|
|
ipv6tcp->tcp.wnd[0] = recvwndo >> 8;
|
|
|
|
ipv6tcp->tcp.wnd[1] = recvwndo & 0xff;
|
2017-06-24 17:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate TCP checksum. */
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
ipv6tcp->tcp.tcpchksum = 0;
|
|
|
|
ipv6tcp->tcp.tcpchksum = ~sixlowpan_tcp_chksum(ipv6tcp, buf, buflen);
|
2017-06-24 17:48:41 +02:00
|
|
|
|
|
|
|
ninfo("Outgoing TCP packet length: %d bytes\n", iplen + IPv6_HDRLEN);
|
2017-06-25 00:00:41 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2017-06-24 17:48:41 +02:00
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
/****************************************************************************
|
2017-08-29 22:08:04 +02:00
|
|
|
* Name: tcp_send_eventhandler
|
2017-06-25 00:00:41 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-06-23 22:09:06 +02:00
|
|
|
* This function is called with the network locked to perform the actual
|
2017-06-25 00:00:41 +02:00
|
|
|
* TCP send operation when polled by the lower, device interfacing layer.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2018-06-23 22:09:06 +02:00
|
|
|
* dev - The structure of the network driver that generated the event.
|
2017-06-25 00:00:41 +02:00
|
|
|
* pvconn - The connection structure associated with the socket
|
2018-06-23 22:09:06 +02:00
|
|
|
* pvpriv - The event handler's private data argument
|
2017-06-25 00:00:41 +02:00
|
|
|
* flags - Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-29 22:08:04 +02:00
|
|
|
static uint16_t tcp_send_eventhandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *pvconn,
|
|
|
|
FAR void *pvpriv, uint16_t flags)
|
2017-06-25 00:00:41 +02:00
|
|
|
{
|
|
|
|
FAR struct sixlowpan_send_s *sinfo = (FAR struct sixlowpan_send_s *)pvpriv;
|
|
|
|
FAR struct tcp_conn_s *conn = (FAR struct tcp_conn_s *)pvconn;
|
|
|
|
struct ipv6tcp_hdr_s ipv6tcp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Verify that this is an IEEE802.15.4 network driver. */
|
|
|
|
|
2017-07-29 21:42:26 +02:00
|
|
|
if (dev->d_lltype != NET_LL_IEEE802154 &&
|
|
|
|
dev->d_lltype != NET_LL_PKTRADIO)
|
2017-06-25 00:00:41 +02:00
|
|
|
{
|
2017-07-29 21:42:26 +02:00
|
|
|
ninfo("Not a compatible network device\n");
|
2017-06-25 00:00:41 +02:00
|
|
|
return flags;
|
|
|
|
}
|
2017-06-24 17:48:41 +02:00
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
/* The TCP socket is connected and, hence, should be bound to a device.
|
|
|
|
* Make sure that the polling device is the one that we are bound to.
|
2017-06-24 17:48:41 +02:00
|
|
|
*/
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
DEBUGASSERT(conn->dev != NULL);
|
|
|
|
if (dev != conn->dev)
|
|
|
|
{
|
2017-08-08 22:24:12 +02:00
|
|
|
ninfo("Not the connected device\n");
|
2017-06-25 00:00:41 +02:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the IEEE802.15.4 network driver went down */
|
2017-06-24 17:48:41 +02:00
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
if ((flags & NETDEV_DOWN) != 0)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Device is down\n");
|
|
|
|
sinfo->s_result = -ENOTCONN;
|
|
|
|
goto end_wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
ninfo("flags: %04x acked: %u sent: %u\n",
|
|
|
|
flags, sinfo->s_acked, sinfo->s_sent);
|
|
|
|
|
|
|
|
/* If this packet contains an acknowledgement, then update the count of
|
|
|
|
* acknowledged bytes.
|
2017-06-24 17:48:41 +02:00
|
|
|
*/
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
if ((flags & TCP_ACKDATA) != 0)
|
|
|
|
{
|
|
|
|
FAR struct tcp_hdr_s *tcp = TCPBUF(dev);
|
|
|
|
|
|
|
|
/* The current acknowledgement number number is the (relative) offset
|
|
|
|
* of the of the next byte needed by the receiver. The s_isn is the
|
|
|
|
* offset of the first byte to send to the receiver. The difference
|
|
|
|
* is the number of bytes to be acknowledged.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sinfo->s_acked = tcp_getsequence(tcp->ackno) - sinfo->s_isn;
|
|
|
|
ninfo("ACK: acked=%d sent=%d buflen=%d\n",
|
|
|
|
sinfo->s_acked, sinfo->s_sent, sinfo->s_buflen);
|
|
|
|
|
|
|
|
/* Have all of the bytes in the buffer been sent and acknowledged? */
|
|
|
|
|
|
|
|
if (sinfo->s_acked >= sinfo->s_buflen)
|
|
|
|
{
|
|
|
|
/* Yes. Then sinfo->s_buflen should hold the number of bytes
|
|
|
|
* actually sent.
|
|
|
|
*/
|
|
|
|
|
2017-07-01 00:07:51 +02:00
|
|
|
sinfo->s_result = sinfo->s_sent;
|
2017-06-25 00:00:41 +02:00
|
|
|
goto end_wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. fall through to send more data if necessary */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we are being asked to retransmit data */
|
|
|
|
|
|
|
|
else if ((flags & TCP_REXMIT) != 0)
|
2017-06-24 17:48:41 +02:00
|
|
|
{
|
2017-06-25 00:00:41 +02:00
|
|
|
/* Yes.. in this case, reset the number of bytes that have been sent
|
|
|
|
* to the number of bytes that have been ACKed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sinfo->s_sent = sinfo->s_acked;
|
|
|
|
|
|
|
|
/* Fall through to re-send data from the last that was ACKed */
|
2017-06-24 17:48:41 +02:00
|
|
|
}
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
/* Check for a loss of connection */
|
|
|
|
|
|
|
|
else if ((flags & TCP_DISCONN_EVENTS) != 0)
|
|
|
|
{
|
2017-10-20 00:45:00 +02:00
|
|
|
FAR struct socket *psock = sinfo->s_sock;
|
2017-06-25 00:00:41 +02:00
|
|
|
|
2017-10-20 00:45:00 +02:00
|
|
|
nwarn("WARNING: Lost connection\n");
|
|
|
|
|
|
|
|
/* We could get here recursively through the callback actions of
|
|
|
|
* tcp_lost_connection(). So don't repeat that action if we have
|
|
|
|
* already been disconnected.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(psock != NULL);
|
|
|
|
if (_SS_ISCONNECTED(psock->s_flags))
|
2019-07-03 02:02:23 +02:00
|
|
|
{
|
|
|
|
/* Report the disconnection event to all socket clones */
|
2017-10-20 00:45:00 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
tcp_lost_connection(psock, sinfo->s_cb, flags);
|
|
|
|
}
|
2017-10-20 00:45:00 +02:00
|
|
|
|
|
|
|
/* Report not connected to the sender */
|
2017-08-29 20:27:58 +02:00
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
sinfo->s_result = -ENOTCONN;
|
|
|
|
goto end_wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the outgoing packet is available (it may have been claimed
|
2018-06-23 22:09:06 +02:00
|
|
|
* by a sendto event handler serving a different thread).
|
2017-06-25 00:00:41 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0 /* We can't really support multiple senders on the same TCP socket */
|
|
|
|
else if (dev->d_sndlen > 0)
|
|
|
|
{
|
|
|
|
/* Another thread has beat us sending data, wait for the next poll */
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We get here if (1) not all of the data has been ACKed, (2) we have been
|
|
|
|
* asked to retransmit data, (3) the connection is still healthy, and (4)
|
|
|
|
* the outgoing packet is available for our use. In this case, we are
|
|
|
|
* now free to send more data to receiver -- UNLESS the buffer contains
|
|
|
|
* unprocessed incoming data. In that event, we will have to wait for the
|
|
|
|
* next polling cycle.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((flags & WPAN_NEWDATA) == 0 && sinfo->s_sent < sinfo->s_buflen)
|
|
|
|
{
|
|
|
|
uint32_t seqno;
|
|
|
|
uint16_t winleft;
|
2017-06-25 02:29:07 +02:00
|
|
|
uint16_t sndlen;
|
2017-06-25 00:00:41 +02:00
|
|
|
|
2017-06-25 02:29:07 +02:00
|
|
|
/* Get the amount of TCP payload data that we can send in the next
|
|
|
|
* packet.
|
|
|
|
*/
|
2017-06-25 00:00:41 +02:00
|
|
|
|
2017-06-25 02:29:07 +02:00
|
|
|
sndlen = sinfo->s_buflen - sinfo->s_sent;
|
2017-06-25 00:00:41 +02:00
|
|
|
if (sndlen > conn->mss)
|
|
|
|
{
|
|
|
|
sndlen = conn->mss;
|
|
|
|
}
|
|
|
|
|
|
|
|
winleft = conn->winsize - sinfo->s_sent + sinfo->s_acked;
|
|
|
|
if (sndlen > winleft)
|
|
|
|
{
|
|
|
|
sndlen = winleft;
|
|
|
|
}
|
|
|
|
|
2017-06-25 02:29:07 +02:00
|
|
|
ninfo("s_buflen=%u s_sent=%u mss=%u winsize=%u sndlen=%d\n",
|
|
|
|
sinfo->s_buflen, sinfo->s_sent, conn->mss, conn->winsize, sndlen);
|
2017-06-25 00:00:41 +02:00
|
|
|
|
|
|
|
if (sndlen > 0)
|
|
|
|
{
|
|
|
|
/* Set the sequence number for this packet. NOTE: The network updates
|
|
|
|
* sndseq on receipt of ACK *before* this function is called. In that
|
|
|
|
* case sndseq will point to the next unacknowledged byte (which might
|
|
|
|
* have already been sent). We will overwrite the value of sndseq
|
|
|
|
* here before the packet is sent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
seqno = sinfo->s_sent + sinfo->s_isn;
|
2017-06-25 02:29:07 +02:00
|
|
|
ninfo("Sending: sndseq %08lx->%08x\n",
|
|
|
|
(unsigned long)tcp_getsequence(conn->sndseq), seqno);
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
tcp_setsequence(conn->sndseq, seqno);
|
|
|
|
|
|
|
|
/* Create the IPv6 + TCP header */
|
|
|
|
|
|
|
|
ret = sixlowpan_tcp_header(conn, dev, &sinfo->s_buf[sinfo->s_sent],
|
|
|
|
sndlen, &ipv6tcp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: sixlowpan_tcp_header failed: %d\n", ret);
|
|
|
|
sinfo->s_result = ret;
|
|
|
|
goto end_wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer the frame list to the IEEE802.15.4 MAC device */
|
|
|
|
|
2017-08-19 16:48:52 +02:00
|
|
|
ret = sixlowpan_queue_frames((FAR struct radio_driver_s *)dev,
|
2017-06-25 00:00:41 +02:00
|
|
|
&ipv6tcp.ipv6,
|
|
|
|
&sinfo->s_buf[sinfo->s_sent], sndlen,
|
|
|
|
sinfo->s_destmac);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: sixlowpan_queue_frames failed: %d\n", ret);
|
|
|
|
sinfo->s_result = ret;
|
|
|
|
goto end_wait;
|
|
|
|
}
|
|
|
|
|
2017-06-25 21:00:42 +02:00
|
|
|
/* Increment the count of bytes sent, the number of unacked bytes,
|
|
|
|
* and the total count of TCP packets sent.
|
|
|
|
*
|
2019-12-08 20:13:51 +01:00
|
|
|
* NOTE: tcp_appsend() normally increments conn->tx_unacked based on
|
2017-06-25 21:00:42 +02:00
|
|
|
* the value of dev->d_sndlen. However, dev->d_len is always
|
2017-06-28 23:39:21 +02:00
|
|
|
* zero for 6LoWPAN since it does not send via the dev->d_buf
|
2017-06-25 21:00:42 +02:00
|
|
|
* but, rather, uses a backdoor frame interface with the IEEE
|
|
|
|
* 802.15.4 MAC.
|
|
|
|
*/
|
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
sinfo->s_sent += sndlen;
|
|
|
|
conn->tx_unacked += sndlen;
|
2017-06-25 21:00:42 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
2020-02-22 19:31:14 +01:00
|
|
|
/* For compatibility with buffered send logic */
|
2017-06-25 21:00:42 +02:00
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
conn->sndseq_max = tcp_addsequence(conn->sndseq, conn->tx_unacked);
|
2017-06-25 21:00:42 +02:00
|
|
|
#endif
|
2017-06-25 00:00:41 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.tcp.sent++;
|
|
|
|
#endif
|
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
ninfo("Sent: acked=%d sent=%d buflen=%d tx_unacked=%d\n",
|
2017-06-25 21:00:42 +02:00
|
|
|
sinfo->s_acked, sinfo->s_sent, sinfo->s_buflen,
|
2019-12-08 20:13:51 +01:00
|
|
|
conn->tx_unacked);
|
2017-06-25 00:00:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Continue waiting */
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
|
|
|
|
end_wait:
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
/* Do not allow any further callbacks */
|
|
|
|
|
|
|
|
sinfo->s_cb->flags = 0;
|
|
|
|
sinfo->s_cb->priv = NULL;
|
|
|
|
sinfo->s_cb->event = NULL;
|
|
|
|
|
|
|
|
/* There are no outstanding, unacknowledged bytes */
|
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
conn->tx_unacked = 0;
|
2017-06-25 00:00:41 +02:00
|
|
|
|
|
|
|
/* Wake up the waiting thread */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&sinfo->s_waitsem);
|
2017-06-25 00:00:41 +02:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sixlowpan_send_packet
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Process an outgoing TCP packet. Takes an IP packet and formats it to
|
|
|
|
* be sent on an 802.15.4 network using 6lowpan. Called from common TCP
|
|
|
|
* send logic.
|
|
|
|
*
|
|
|
|
* The payload data is in the caller 'buf' and is of length 'buflen'.
|
|
|
|
* Compressed headers will be added and if necessary the packet is
|
|
|
|
* fragmented. The resulting packet/fragments are submitted to the MAC
|
2017-07-29 21:42:26 +02:00
|
|
|
* via the network driver r_req_data method.
|
2017-06-25 00:00:41 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - An instance of the internal socket structure.
|
|
|
|
* dev - The IEEE802.15.4 MAC network driver interface.
|
|
|
|
* conn - TCP connection structure
|
|
|
|
* buf - Data to send
|
|
|
|
* len - Length of data to send
|
|
|
|
* destmac - The IEEE802.15.4 MAC address of the destination
|
2020-01-04 11:37:46 +01:00
|
|
|
* timeout - Send timeout in milliseconds
|
2017-06-25 00:00:41 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Ok is returned on success; Othewise a negated errno value is returned.
|
|
|
|
* This function is expected to fail if the driver is not an IEEE802.15.4
|
|
|
|
* MAC network driver. In that case, the logic will fall back to normal
|
|
|
|
* IPv4/IPv6 formatting.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int sixlowpan_send_packet(FAR struct socket *psock,
|
|
|
|
FAR struct net_driver_s *dev,
|
|
|
|
FAR struct tcp_conn_s *conn,
|
|
|
|
FAR const uint8_t *buf, size_t len,
|
2017-07-31 03:10:51 +02:00
|
|
|
FAR const struct netdev_varaddr_s *destmac,
|
2017-06-25 00:00:41 +02:00
|
|
|
uint16_t timeout)
|
|
|
|
{
|
|
|
|
struct sixlowpan_send_s sinfo;
|
|
|
|
|
|
|
|
ninfo("len=%lu timeout=%u\n", (unsigned long)len, timeout);
|
|
|
|
DEBUGASSERT(psock != NULL && dev != NULL && conn != NULL && buf != NULL &&
|
|
|
|
destmac != NULL);
|
|
|
|
|
|
|
|
memset(&sinfo, 0, sizeof(struct sixlowpan_send_s));
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
/* Allocate resources to receive a callback.
|
|
|
|
*
|
|
|
|
* The second parameter is NULL meaning that we can get only
|
|
|
|
* device related events, no connect-related events.
|
|
|
|
*/
|
|
|
|
|
2017-06-25 21:00:42 +02:00
|
|
|
sinfo.s_cb = tcp_callback_alloc(conn);
|
2017-06-25 00:00:41 +02:00
|
|
|
if (sinfo.s_cb != NULL)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize the send state structure */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&sinfo.s_waitsem, 0, 0);
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_setprotocol(&sinfo.s_waitsem, SEM_PRIO_NONE);
|
2017-06-25 00:00:41 +02:00
|
|
|
|
|
|
|
sinfo.s_sock = psock;
|
|
|
|
sinfo.s_result = -EBUSY;
|
|
|
|
sinfo.s_destmac = destmac;
|
|
|
|
sinfo.s_buf = buf;
|
|
|
|
sinfo.s_buflen = len;
|
|
|
|
|
|
|
|
/* Set up the initial sequence number */
|
|
|
|
|
|
|
|
sinfo.s_isn = tcp_getsequence(conn->sndseq);
|
|
|
|
|
|
|
|
/* Set up the callback in the connection */
|
|
|
|
|
2017-06-25 21:00:42 +02:00
|
|
|
sinfo.s_cb->flags = (NETDEV_DOWN | TCP_ACKDATA | TCP_REXMIT |
|
|
|
|
TCP_DISCONN_EVENTS | WPAN_POLL);
|
2017-06-25 00:00:41 +02:00
|
|
|
sinfo.s_cb->priv = (FAR void *)&sinfo;
|
2017-08-29 22:08:04 +02:00
|
|
|
sinfo.s_cb->event = tcp_send_eventhandler;
|
2017-06-25 00:00:41 +02:00
|
|
|
|
|
|
|
/* There is no outstanding, unacknowledged data after this
|
|
|
|
* initial sequence number.
|
|
|
|
*/
|
|
|
|
|
2019-12-08 20:13:51 +01:00
|
|
|
conn->tx_unacked = 0;
|
2017-06-25 00:00:41 +02:00
|
|
|
|
|
|
|
/* Notify the IEEE802.15.4 MAC that we have data to send. */
|
|
|
|
|
|
|
|
netdev_txnotify_dev(dev);
|
|
|
|
|
2018-06-23 22:09:06 +02:00
|
|
|
/* Wait for the send to complete or an error to occur.
|
2020-01-04 11:37:46 +01:00
|
|
|
* net_timedwait will also terminate if a signal is received.
|
2017-06-25 00:00:41 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
ninfo("Wait for send complete\n");
|
|
|
|
|
2020-01-04 11:37:46 +01:00
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
uint32_t acked = sinfo.s_acked;
|
|
|
|
|
|
|
|
ret = net_timedwait(&sinfo.s_waitsem, timeout);
|
|
|
|
if (ret != -ETIMEDOUT || acked == sinfo.s_acked)
|
|
|
|
{
|
|
|
|
break; /* Timeout without any progress */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-09-04 14:55:28 +02:00
|
|
|
sinfo.s_result = ret;
|
2017-06-25 00:00:41 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 22:09:06 +02:00
|
|
|
/* Make sure that no further events are processed */
|
2017-06-25 00:00:41 +02:00
|
|
|
|
2017-06-25 21:00:42 +02:00
|
|
|
tcp_callback_free(conn, sinfo.s_cb);
|
2017-06-25 00:00:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&sinfo.s_waitsem);
|
2017-06-25 00:00:41 +02:00
|
|
|
net_unlock();
|
|
|
|
|
|
|
|
return (sinfo.s_result < 0 ? sinfo.s_result : len);
|
2017-06-24 17:48:41 +02:00
|
|
|
}
|
|
|
|
|
2017-03-30 02:07:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:23:40 +02:00
|
|
|
* Name: psock_6lowpan_tcp_send
|
2017-03-30 02:07:52 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* psock_6lowpan_tcp_send() call may be used only when the TCP socket is in a
|
|
|
|
* connected state (so that the intended recipient is known).
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2017-03-30 02:07:52 +02:00
|
|
|
* psock - An instance of the internal socket structure.
|
|
|
|
* buf - Data to send
|
2017-04-02 21:27:53 +02:00
|
|
|
* bulen - Length of data to send
|
2017-03-30 02:07:52 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters sent. On error,
|
|
|
|
* -1 is returned, and errno is set appropriately. Returned error numbers
|
|
|
|
* must be consistent with definition of errors reported by send() or
|
|
|
|
* sendto().
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t psock_6lowpan_tcp_send(FAR struct socket *psock, FAR const void *buf,
|
2017-04-02 21:27:53 +02:00
|
|
|
size_t buflen)
|
2017-03-30 02:07:52 +02:00
|
|
|
{
|
|
|
|
FAR struct tcp_conn_s *conn;
|
|
|
|
FAR struct net_driver_s *dev;
|
2017-07-31 03:10:51 +02:00
|
|
|
struct netdev_varaddr_s destmac;
|
2017-03-30 02:07:52 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-04-03 20:01:04 +02:00
|
|
|
ninfo("buflen %lu\n", (unsigned long)buflen);
|
2017-04-04 20:31:22 +02:00
|
|
|
sixlowpan_dumpbuffer("Outgoing TCP payload", buf, buflen);
|
2017-04-03 20:01:04 +02:00
|
|
|
|
2017-03-30 02:07:52 +02:00
|
|
|
DEBUGASSERT(psock != NULL && psock->s_crefs > 0);
|
|
|
|
DEBUGASSERT(psock->s_type == SOCK_STREAM);
|
|
|
|
|
|
|
|
/* Make sure that this is a valid socket */
|
|
|
|
|
2017-06-24 17:48:41 +02:00
|
|
|
if (psock == NULL || psock->s_crefs <= 0)
|
2017-03-30 02:07:52 +02:00
|
|
|
{
|
|
|
|
nerr("ERROR: Invalid socket\n");
|
|
|
|
return (ssize_t)-EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that this is a connected TCP socket */
|
|
|
|
|
|
|
|
if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(psock->s_flags))
|
|
|
|
{
|
|
|
|
nerr("ERROR: Not connected\n");
|
|
|
|
return (ssize_t)-ENOTCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the underlying TCP connection structure */
|
|
|
|
|
|
|
|
conn = (FAR struct tcp_conn_s *)psock->s_conn;
|
|
|
|
DEBUGASSERT(conn != NULL);
|
|
|
|
|
2017-04-02 20:01:04 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2017-03-30 02:07:52 +02:00
|
|
|
/* Ignore if not IPv6 domain */
|
|
|
|
|
|
|
|
if (conn->domain != PF_INET6)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Not IPv6\n");
|
|
|
|
return (ssize_t)-EPROTOTYPE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Route outgoing message to the correct device */
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
dev = netdev_findby_ripv6addr(conn->u.ipv6.laddr, conn->u.ipv6.raddr);
|
2017-03-31 20:06:21 +02:00
|
|
|
if (dev == NULL)
|
2017-03-30 02:07:52 +02:00
|
|
|
{
|
2017-07-29 21:42:26 +02:00
|
|
|
nwarn("WARNING: Not routable\n");
|
2017-03-30 02:07:52 +02:00
|
|
|
return (ssize_t)-ENETUNREACH;
|
|
|
|
}
|
2017-07-29 21:42:26 +02:00
|
|
|
|
|
|
|
/* Some network devices support different link layer protocols.
|
|
|
|
* Check if this device has the hooks to support 6LoWPAN.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->d_lltype != NET_LL_IEEE802154 &&
|
|
|
|
dev->d_lltype != NET_LL_PKTRADIO)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Not a compatible network device\n");
|
|
|
|
return (ssize_t)-ENONET;
|
|
|
|
}
|
|
|
|
|
2017-03-30 02:07:52 +02:00
|
|
|
#ifdef CONFIG_NET_ICMPv6_NEIGHBOR
|
|
|
|
/* Make sure that the IP address mapping is in the Neighbor Table */
|
|
|
|
|
|
|
|
ret = icmpv6_neighbor(conn->u.ipv6.raddr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Not reachable\n");
|
|
|
|
return (ssize_t)-ENETUNREACH;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-22 19:10:39 +01:00
|
|
|
/* Get the IEEE 802.15.4 MAC address of the next hop. */
|
2017-06-25 00:00:41 +02:00
|
|
|
|
2017-11-22 19:10:39 +01:00
|
|
|
ret = sixlowpan_nexthopaddr((FAR struct radio_driver_s *)dev,
|
|
|
|
conn->u.ipv6.raddr, &destmac);
|
2017-06-30 17:32:17 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-07-31 17:31:24 +02:00
|
|
|
nerr("ERROR: Failed to get dest MAC address: %d\n", ret);
|
2017-06-30 17:32:17 +02:00
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
2017-06-25 00:00:41 +02:00
|
|
|
|
2017-06-24 17:48:41 +02:00
|
|
|
/* Send the TCP packets, breaking down the potential large user buffer
|
|
|
|
* into smaller packets that can be reassembled in the allocated MTU
|
|
|
|
* packet buffer.
|
2017-04-02 21:27:53 +02:00
|
|
|
*/
|
|
|
|
|
2017-06-25 00:00:41 +02:00
|
|
|
ret = sixlowpan_send_packet(psock, dev, conn, buf, buflen, &destmac,
|
2020-01-04 11:37:46 +01:00
|
|
|
_SO_TIMEOUT(psock->s_sndtimeo));
|
2017-06-25 00:00:41 +02:00
|
|
|
if (ret < 0)
|
2017-04-02 21:27:53 +02:00
|
|
|
{
|
2017-06-25 00:00:41 +02:00
|
|
|
nerr("ERROR: sixlowpan_send_packet() failed: %d\n", ret);
|
2017-06-30 17:32:17 +02:00
|
|
|
return (ssize_t)ret;
|
2017-03-30 02:07:52 +02:00
|
|
|
}
|
|
|
|
|
2017-06-24 17:48:41 +02:00
|
|
|
return (ssize_t)buflen;
|
2017-03-30 02:07:52 +02:00
|
|
|
}
|
|
|
|
|
2017-04-02 20:01:04 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:23:40 +02:00
|
|
|
* Name: sixlowpan_tcp_send
|
2017-04-02 20:01:04 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-04-03 20:01:04 +02:00
|
|
|
* TCP output comes through three different mechansims. Either from:
|
2017-04-02 20:01:04 +02:00
|
|
|
*
|
|
|
|
* 1. TCP socket output. For the case of TCP output to an
|
2017-06-28 23:39:21 +02:00
|
|
|
* IEEE802.15.4, the TCP output is caught in the socket
|
2017-04-02 20:01:04 +02:00
|
|
|
* send()/sendto() logic and and redirected to psock_6lowpan_tcp_send().
|
|
|
|
* 2. TCP output from the TCP state machine. That will occur
|
2017-04-03 20:01:04 +02:00
|
|
|
* during TCP packet processing by the TCP state meachine.
|
|
|
|
* 3. TCP output resulting from TX or timer polling
|
|
|
|
*
|
|
|
|
* Cases 2 and 3 will be handled here. Logic in ipv6_tcp_input(),
|
|
|
|
* devif_poll(), and devif_timer() detect if (1) an attempt to return with
|
|
|
|
* d_len > 0 and (2) that the device is an IEEE802.15.4 MAC network
|
|
|
|
* driver. Under those conditions, this function will be called to create
|
|
|
|
* the IEEE80215.4 frames.
|
2017-04-02 20:01:04 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2017-06-28 23:39:21 +02:00
|
|
|
* dev - The network device containing the packet to be sent.
|
|
|
|
* fwddev - The network device used to send the data. This will be the
|
|
|
|
* same device except for the IP forwarding case where packets
|
|
|
|
* are sent across devices.
|
|
|
|
* ipv6 - A pointer to the IPv6 header in dev->d_buf which lies AFTER
|
|
|
|
* the L1 header. NOTE: dev->d_len must have been decremented
|
|
|
|
* by the size of any preceding MAC header.
|
2017-04-02 20:01:04 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-06-28 23:39:21 +02:00
|
|
|
void sixlowpan_tcp_send(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct net_driver_s *fwddev,
|
|
|
|
FAR struct ipv6_hdr_s *ipv6)
|
2017-04-02 20:01:04 +02:00
|
|
|
{
|
2017-06-28 23:39:21 +02:00
|
|
|
DEBUGASSERT(dev != NULL && dev->d_len > 0 && fwddev != NULL);
|
2017-04-02 20:01:04 +02:00
|
|
|
|
|
|
|
/* Double check */
|
|
|
|
|
2017-04-03 20:01:04 +02:00
|
|
|
ninfo("d_len %u\n", dev->d_len);
|
2017-04-04 20:31:22 +02:00
|
|
|
sixlowpan_dumpbuffer("Outgoing TCP packet",
|
2017-06-28 23:39:21 +02:00
|
|
|
(FAR const uint8_t *)ipv6, dev->d_len);
|
2017-04-03 20:01:04 +02:00
|
|
|
|
2017-11-01 21:15:21 +01:00
|
|
|
if (dev != NULL && dev->d_len > 0 && fwddev != NULL)
|
2017-04-02 20:01:04 +02:00
|
|
|
{
|
2017-04-08 01:04:57 +02:00
|
|
|
FAR struct ipv6tcp_hdr_s *ipv6hdr;
|
2017-04-02 20:01:04 +02:00
|
|
|
|
|
|
|
/* The IPv6 header followed by a TCP headers should lie at the
|
|
|
|
* beginning of d_buf since there is no link layer protocol header
|
|
|
|
* and the TCP state machine should only response with TCP packets.
|
|
|
|
*/
|
|
|
|
|
2017-06-28 23:39:21 +02:00
|
|
|
ipv6hdr = (FAR struct ipv6tcp_hdr_s *)ipv6;
|
2017-04-02 20:01:04 +02:00
|
|
|
|
|
|
|
/* The TCP data payload should follow the IPv6 header plus the
|
|
|
|
* protocol header.
|
|
|
|
*/
|
|
|
|
|
2017-04-08 01:04:57 +02:00
|
|
|
if (ipv6hdr->ipv6.proto != IP_PROTO_TCP)
|
2017-04-02 20:01:04 +02:00
|
|
|
{
|
2017-04-08 01:04:57 +02:00
|
|
|
nwarn("WARNING: Expected TCP protoype: %u vs %s\n",
|
|
|
|
ipv6hdr->ipv6.proto, IP_PROTO_TCP);
|
2017-04-02 20:01:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-31 03:10:51 +02:00
|
|
|
struct netdev_varaddr_s destmac;
|
2017-04-07 23:27:53 +02:00
|
|
|
FAR uint8_t *buf;
|
2017-04-08 01:04:57 +02:00
|
|
|
uint16_t hdrlen;
|
|
|
|
uint16_t buflen;
|
2017-06-30 17:32:17 +02:00
|
|
|
int ret;
|
2017-04-04 17:08:48 +02:00
|
|
|
|
2017-11-22 19:10:39 +01:00
|
|
|
/* Get the IEEE 802.15.4 MAC address of the next hop. */
|
2017-04-04 17:08:48 +02:00
|
|
|
|
2017-11-22 19:10:39 +01:00
|
|
|
ret = sixlowpan_nexthopaddr((FAR struct radio_driver_s *)fwddev,
|
|
|
|
ipv6hdr->ipv6.destipaddr, &destmac);
|
2017-06-30 17:32:17 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-07-31 17:31:24 +02:00
|
|
|
nerr("ERROR: Failed to get dest MAC address: %d\n", ret);
|
2017-06-30 17:32:17 +02:00
|
|
|
goto drop;
|
|
|
|
}
|
2017-04-08 01:04:57 +02:00
|
|
|
|
|
|
|
/* Get the IPv6 + TCP combined header length. The size of the TCP
|
|
|
|
* header is encoded in the top 4 bits of the tcpoffset field (in
|
|
|
|
* units of 32-bit words).
|
|
|
|
*/
|
|
|
|
|
|
|
|
hdrlen = IPv6_HDRLEN + (((uint16_t)ipv6hdr->tcp.tcpoffset >> 4) << 2);
|
|
|
|
|
|
|
|
/* Drop the packet if the buffer length is less than this. */
|
2017-04-04 17:08:48 +02:00
|
|
|
|
2017-04-08 01:04:57 +02:00
|
|
|
if (hdrlen > dev->d_len)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Dropping small TCP packet: %u < %u\n",
|
|
|
|
buflen, hdrlen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Convert the outgoing packet into a frame list. */
|
2017-04-04 17:08:48 +02:00
|
|
|
|
2017-06-28 23:39:21 +02:00
|
|
|
buf = (FAR uint8_t *)ipv6 + hdrlen;
|
2017-04-08 01:04:57 +02:00
|
|
|
buflen = dev->d_len - hdrlen;
|
2017-04-07 23:27:53 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
sixlowpan_queue_frames(
|
2017-08-19 16:48:52 +02:00
|
|
|
(FAR struct radio_driver_s *)fwddev,
|
2017-06-28 23:39:21 +02:00
|
|
|
&ipv6hdr->ipv6, buf, buflen, &destmac);
|
2017-04-08 01:04:57 +02:00
|
|
|
}
|
2017-04-02 20:01:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 17:32:17 +02:00
|
|
|
drop:
|
2017-04-02 20:01:04 +02:00
|
|
|
dev->d_len = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-30 02:07:52 +02:00
|
|
|
#endif /* CONFIG_NET_6LOWPAN && CONFIG_NET_TCP */
|