2017-03-30 02:07:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/sixlowpan/sixlowpan_udpsend.c
|
|
|
|
*
|
2020-04-12 18:46:40 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-03-30 02:07:52 +02:00
|
|
|
*
|
2020-04-12 18:46:40 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-03-30 02:07:52 +02:00
|
|
|
*
|
2020-04-12 18:46:40 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-03-30 02:07:52 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <string.h>
|
2017-03-30 02:07:52 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include "nuttx/net/netdev.h"
|
2017-08-20 15:10:48 +02:00
|
|
|
#include "nuttx/net/radiodev.h"
|
2017-04-02 21:27:53 +02:00
|
|
|
#include "nuttx/net/netstats.h"
|
2017-03-30 02:07:52 +02:00
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
|
|
|
#include "socket/socket.h"
|
2017-08-07 19:50:50 +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 "udp/udp.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_UDP)
|
|
|
|
|
2017-04-02 22:30:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sixlowpan_udp_chksum
|
|
|
|
*
|
|
|
|
* Description:
|
2020-04-12 19:18:02 +02:00
|
|
|
* Perform the checksum calculation over the IPv6, protocol headers, and
|
2017-04-02 22:30:01 +02:00
|
|
|
* data payload as necessary.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-04-12 19:18:02 +02:00
|
|
|
* ipv6udp - A reference to a structure containing the IPv6 and UDP
|
|
|
|
* headers.
|
2017-04-02 22:30:01 +02:00
|
|
|
* buf - The beginning of the payload data
|
|
|
|
* buflen - The length of the payload data.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The calculated checksum
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_UDP_CHECKSUMS
|
2017-06-17 00:47:27 +02:00
|
|
|
static uint16_t sixlowpan_udp_chksum(FAR const struct ipv6udp_hdr_s *ipv6udp,
|
2017-04-02 22:30:01 +02:00
|
|
|
FAR const uint8_t *buf, uint16_t buflen)
|
|
|
|
{
|
|
|
|
uint16_t upperlen;
|
|
|
|
uint16_t sum;
|
|
|
|
|
|
|
|
/* The length reported in the IPv6 header is the length of the payload
|
|
|
|
* that follows the header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
upperlen = ((uint16_t)ipv6udp->ipv6.len[0] << 8) + ipv6udp->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 + ipv6udp->ipv6.proto;
|
|
|
|
|
|
|
|
/* Sum IP source and destination addresses. */
|
|
|
|
|
|
|
|
sum = chksum(sum, (FAR uint8_t *)ipv6udp->ipv6.srcipaddr,
|
|
|
|
2 * sizeof(net_ipv6addr_t));
|
|
|
|
|
|
|
|
/* Sum the UDP header */
|
|
|
|
|
|
|
|
sum = chksum(sum, (FAR uint8_t *)&ipv6udp->udp, UDP_HDRLEN);
|
|
|
|
|
|
|
|
/* Sum payload data. */
|
|
|
|
|
|
|
|
sum = chksum(sum, buf, buflen);
|
2022-01-18 08:38:00 +01:00
|
|
|
return (sum == 0) ? 0xffff : HTONS(sum);
|
2017-04-02 22:30:01 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-30 02:07:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:23:40 +02:00
|
|
|
* Name: psock_6lowpan_udp_sendto
|
2017-03-30 02:07:52 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-04-02 19:15:46 +02:00
|
|
|
* If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
|
|
|
|
* socket, the parameters to and 'tolen' are ignored (and the error EISCONN
|
|
|
|
* may be returned when they are not NULL and 0), and the error ENOTCONN is
|
|
|
|
* returned when the socket was not actually connected.
|
2017-03-30 02:07:52 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2017-04-02 19:15:46 +02:00
|
|
|
* psock A pointer to a NuttX-specific, internal socket structure
|
|
|
|
* buf Data to send
|
2017-04-02 21:27:53 +02:00
|
|
|
* buflen Length of data to send
|
2017-04-02 19:15:46 +02:00
|
|
|
* flags Send flags
|
|
|
|
* to Address of recipient
|
|
|
|
* tolen The length of the address structure
|
2017-03-30 02:07:52 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters sent. On error,
|
2017-04-02 19:15:46 +02:00
|
|
|
* -1 is returned, and errno is set appropriately. Returned error
|
|
|
|
* number must be consistent with definition of errors reported by
|
2017-03-30 02:07:52 +02:00
|
|
|
* sendto().
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
ssize_t psock_6lowpan_udp_sendto(FAR struct socket *psock,
|
|
|
|
FAR const void *buf,
|
2017-04-02 21:27:53 +02:00
|
|
|
size_t buflen, int flags,
|
2017-04-02 19:15:46 +02:00
|
|
|
FAR const struct sockaddr *to,
|
|
|
|
socklen_t tolen)
|
2017-03-30 02:07:52 +02:00
|
|
|
{
|
2017-04-02 19:15:46 +02:00
|
|
|
FAR struct sockaddr_in6 *to6 = (FAR struct sockaddr_in6 *)to;
|
2017-03-30 02:07:52 +02:00
|
|
|
FAR struct udp_conn_s *conn;
|
|
|
|
FAR struct net_driver_s *dev;
|
|
|
|
struct ipv6udp_hdr_s ipv6udp;
|
2017-07-31 03:10:51 +02:00
|
|
|
struct netdev_varaddr_s destmac;
|
2017-04-02 21:27:53 +02:00
|
|
|
uint16_t iplen;
|
2017-03-30 02:07:52 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-04-04 20:31:22 +02:00
|
|
|
ninfo("buflen %lu\n", (unsigned long)buflen);
|
|
|
|
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(to != NULL);
|
2017-03-30 02:07:52 +02:00
|
|
|
DEBUGASSERT(psock->s_type == SOCK_DGRAM);
|
|
|
|
|
2017-04-04 20:31:22 +02:00
|
|
|
sixlowpan_dumpbuffer("Outgoing UDP payload", buf, buflen);
|
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
if (psock == NULL || to == NULL)
|
|
|
|
{
|
|
|
|
return (ssize_t)-EINVAL;
|
|
|
|
}
|
2017-03-30 02:07:52 +02:00
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
/* Make sure that this is a datagram valid socket */
|
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
if (psock->s_conn == NULL || psock->s_type != SOCK_DGRAM)
|
2017-03-30 02:07:52 +02:00
|
|
|
{
|
|
|
|
nerr("ERROR: Invalid socket\n");
|
|
|
|
return (ssize_t)-EBADF;
|
|
|
|
}
|
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
/* Make sure that the destination address is valid */
|
2017-03-30 02:07:52 +02:00
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
if (to6->sin6_family != AF_INET6 || tolen < sizeof(struct sockaddr_in6))
|
2017-03-30 02:07:52 +02:00
|
|
|
{
|
2017-04-06 19:57:43 +02:00
|
|
|
nerr("ERROR: Invalid destination address: sin6_family=%u tolen = %u\n",
|
|
|
|
to6->sin6_family, tolen);
|
|
|
|
return (ssize_t)-EPROTOTYPE;
|
2017-03-30 02:07:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the underlying UDP "connection" structure */
|
|
|
|
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2017-03-30 02:07:52 +02:00
|
|
|
DEBUGASSERT(conn != NULL);
|
|
|
|
|
|
|
|
/* Route outgoing message to the correct device */
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
dev = netdev_findby_ripv6addr(conn->u.ipv6.laddr,
|
|
|
|
to6->sin6_addr.in6_u.u6_addr16);
|
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 */
|
|
|
|
|
2023-08-10 05:43:27 +02:00
|
|
|
ret = icmpv6_neighbor(dev, to6->sin6_addr.in6_u.u6_addr16);
|
2017-03-30 02:07:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Not reachable\n");
|
|
|
|
return (ssize_t)-ENETUNREACH;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialize the IPv6/UDP headers */
|
2017-04-02 21:27:53 +02:00
|
|
|
|
|
|
|
ipv6udp.ipv6.vtc = 0x60;
|
|
|
|
ipv6udp.ipv6.tcf = 0x00;
|
|
|
|
ipv6udp.ipv6.flow = 0x00;
|
|
|
|
ipv6udp.ipv6.proto = IP_PROTO_UDP;
|
2024-05-29 13:35:34 +02:00
|
|
|
ipv6udp.ipv6.ttl = conn->sconn.s_ttl;
|
2017-04-02 21:27:53 +02:00
|
|
|
|
|
|
|
/* The IPv6 header length field does not include the size of IPv6 IP
|
|
|
|
* header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
iplen = buflen + UDP_HDRLEN;
|
|
|
|
ipv6udp.ipv6.len[0] = (iplen >> 8);
|
|
|
|
ipv6udp.ipv6.len[1] = (iplen & 0xff);
|
|
|
|
|
|
|
|
/* Copy the source and destination addresses */
|
|
|
|
|
2020-04-12 19:18:02 +02:00
|
|
|
net_ipv6addr_hdrcopy(ipv6udp.ipv6.destipaddr,
|
|
|
|
to6->sin6_addr.in6_u.u6_addr16);
|
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
|
|
|
{
|
|
|
|
net_ipv6addr_hdrcopy(ipv6udp.ipv6.srcipaddr, conn->u.ipv6.laddr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-25 11:37:11 +02:00
|
|
|
net_ipv6addr_hdrcopy(ipv6udp.ipv6.srcipaddr,
|
|
|
|
netdev_ipv6_srcaddr(dev, ipv6udp.ipv6.destipaddr));
|
2017-06-24 19:57:59 +02:00
|
|
|
}
|
2017-04-02 21:27:53 +02:00
|
|
|
|
2017-04-03 17:15:00 +02:00
|
|
|
ninfo("IPv6 length: %d\n",
|
|
|
|
((int)ipv6udp.ipv6.len[0] << 8) + ipv6udp.ipv6.len[1]);
|
2017-04-02 21:27:53 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.ipv6.sent++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialize the UDP header */
|
|
|
|
|
|
|
|
ipv6udp.udp.srcport = conn->lport;
|
|
|
|
ipv6udp.udp.destport = to6->sin6_port;
|
2022-01-18 08:38:00 +01:00
|
|
|
ipv6udp.udp.udplen = HTONS(iplen);
|
2017-04-02 21:27:53 +02:00
|
|
|
ipv6udp.udp.udpchksum = 0;
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_UDP_CHECKSUMS
|
2017-06-17 00:47:27 +02:00
|
|
|
ipv6udp.udp.udpchksum = ~sixlowpan_udp_chksum(&ipv6udp, buf, buflen);
|
2017-04-02 21:27:53 +02:00
|
|
|
if (ipv6udp.udp.udpchksum == 0)
|
|
|
|
{
|
|
|
|
ipv6udp.udp.udpchksum = 0xffff;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_UDP_CHECKSUMS */
|
|
|
|
|
|
|
|
ninfo("Outgoing UDP packet length: %d\n", iplen + IPv6_HDRLEN);
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.udp.sent++;
|
|
|
|
#endif
|
2017-03-30 02:07:52 +02:00
|
|
|
|
2017-11-22 19:10:39 +01:00
|
|
|
/* Get the IEEE 802.15.4 MAC address of the next hop. */
|
2017-04-01 21:42:00 +02:00
|
|
|
|
2017-11-22 19:10:39 +01:00
|
|
|
ret = sixlowpan_nexthopaddr((FAR struct radio_driver_s *)dev,
|
|
|
|
to6->sin6_addr.in6_u.u6_addr16, &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-19 00:00:08 +02:00
|
|
|
/* If routable, then call sixlowpan_send() to format and send the 6LoWPAN
|
2017-03-30 02:07:52 +02:00
|
|
|
* packet.
|
|
|
|
*/
|
|
|
|
|
net/devif/devif_callback.c: corrected the connection event list to work as FIFO instead of LIFO.
In case of enabled packet forwarding mode, packets were forwarded in a reverse order
because of LIFO behavior of the connection event list.
The issue exposed only during high network traffic. Thus the event list started to grow
that resulted in changing the order of packets inside of groups of several packets
like the following: 3, 2, 1, 6, 5, 4, 8, 7 etc.
Remarks concerning the connection event list implementation:
* Now the queue (list) is FIFO as it should be.
* The list is singly linked.
* The list has a head pointer (inside of outer net_driver_s structure),
and a tail pointer is added into outer net_driver_s structure.
* The list item is devif_callback_s structure.
It still has two pointers to two different list chains (*nxtconn and *nxtdev).
* As before the first argument (*dev) of the list functions can be NULL,
while the other argument (*list) is effective (not NULL).
* An extra (*tail) argument is added to devif_callback_alloc()
and devif_conn_callback_free() functions.
* devif_callback_alloc() time complexity is O(1) (i.e. O(n) to fill the whole list).
* devif_callback_free() time complexity is O(n) (i.e. O(n^2) to empty the whole list).
* devif_conn_event() time complexity is O(n).
2021-08-29 22:57:26 +02:00
|
|
|
ret = sixlowpan_send(dev,
|
2022-02-08 06:18:01 +01:00
|
|
|
&conn->sconn.list,
|
|
|
|
&conn->sconn.list_tail,
|
2017-04-06 19:57:43 +02:00
|
|
|
(FAR const struct ipv6_hdr_s *)&ipv6udp,
|
2020-04-12 19:18:02 +02:00
|
|
|
buf, buflen, &destmac,
|
2022-02-07 07:36:54 +01:00
|
|
|
_SO_TIMEOUT(conn->sconn.s_sndtimeo));
|
2017-03-30 02:07:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: sixlowpan_send() 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-04-02 19:15:46 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:23:40 +02:00
|
|
|
* Name: psock_6lowpan_udp_send
|
2017-04-02 19:15:46 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* psock_6lowpan_udp_send() call may be used with connectionlesss UDP
|
|
|
|
* sockets.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2017-04-02 21:27:53 +02:00
|
|
|
* psock - An instance of the internal socket structure.
|
|
|
|
* buf - Data to send
|
|
|
|
* buflen - Length of data to send
|
2017-04-02 19:15:46 +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().
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t psock_6lowpan_udp_send(FAR struct socket *psock, FAR const void *buf,
|
2017-04-02 21:27:53 +02:00
|
|
|
size_t buflen)
|
2017-04-02 19:15:46 +02:00
|
|
|
{
|
|
|
|
FAR struct udp_conn_s *conn;
|
|
|
|
struct sockaddr_in6 to;
|
|
|
|
|
2017-04-04 20:31:22 +02:00
|
|
|
ninfo("buflen %lu\n", (unsigned long)buflen);
|
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
DEBUGASSERT(psock->s_type == SOCK_DGRAM);
|
|
|
|
|
2017-04-04 20:31:22 +02:00
|
|
|
sixlowpan_dumpbuffer("Outgoing UDP payload", buf, buflen);
|
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
/* Make sure that this is a valid socket */
|
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
if (psock == NULL || psock->s_conn == NULL)
|
2017-04-02 19:15:46 +02:00
|
|
|
{
|
|
|
|
nerr("ERROR: Invalid socket\n");
|
|
|
|
return (ssize_t)-EBADF;
|
|
|
|
}
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
/* Get the underlying UDP "connection" structure */
|
|
|
|
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2022-02-08 06:18:01 +01:00
|
|
|
|
2017-04-02 19:15:46 +02:00
|
|
|
/* Was the UDP socket connected via connect()? */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (psock->s_type != SOCK_DGRAM || !_SS_ISCONNECTED(conn->sconn.s_flags))
|
2017-04-02 19:15:46 +02:00
|
|
|
{
|
|
|
|
/* No, then it is not legal to call send() with this socket. */
|
|
|
|
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore if not IPv6 domain */
|
|
|
|
|
|
|
|
if (conn->domain != PF_INET6)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Not IPv6\n");
|
|
|
|
return (ssize_t)-EPROTOTYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the 'to' address */
|
|
|
|
|
|
|
|
to.sin6_family = AF_INET6;
|
|
|
|
to.sin6_port = conn->rport; /* Already network order */
|
|
|
|
memcpy(to.sin6_addr.in6_u.u6_addr16, conn->u.ipv6.raddr, 16);
|
|
|
|
|
2017-04-02 21:27:53 +02:00
|
|
|
return psock_6lowpan_udp_sendto(psock, buf, buflen, 0,
|
2017-04-02 19:15:46 +02:00
|
|
|
(FAR const struct sockaddr *)&to,
|
|
|
|
sizeof(struct sockaddr_in6));
|
|
|
|
}
|
|
|
|
|
2017-06-28 23:39:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: sixlowpan_udp_send
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handles forwarding a UDP packet via 6LoWPAN. This is currently only
|
|
|
|
* used by the IPv6 forwarding logic.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2020-02-23 09:50:23 +01:00
|
|
|
* dev - An instance of network device state structure
|
2017-06-28 23:39:21 +02:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPFORWARD
|
|
|
|
void sixlowpan_udp_send(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct net_driver_s *fwddev,
|
|
|
|
FAR struct ipv6_hdr_s *ipv6)
|
|
|
|
{
|
|
|
|
FAR struct ipv6udp_hdr_s *ipv6udp = (FAR struct ipv6udp_hdr_s *)ipv6;
|
|
|
|
|
|
|
|
/* Double check */
|
|
|
|
|
2017-11-01 21:15:21 +01:00
|
|
|
DEBUGASSERT(dev != NULL && dev->d_len > 0 && fwddev != NULL);
|
2017-06-28 23:39:21 +02:00
|
|
|
|
|
|
|
ninfo("d_len %u\n", dev->d_len);
|
|
|
|
|
2017-11-01 21:15:21 +01:00
|
|
|
if (dev != NULL && dev->d_len > 0 && fwddev != NULL)
|
2017-06-28 23:39:21 +02:00
|
|
|
{
|
|
|
|
sixlowpan_dumpbuffer("Outgoing UDP packet",
|
|
|
|
(FAR const uint8_t *)ipv6udp, dev->d_len);
|
|
|
|
|
|
|
|
/* The UDP data payload should follow the IPv6 header plus the
|
|
|
|
* protocol header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ipv6udp->ipv6.proto != IP_PROTO_UDP)
|
|
|
|
{
|
2020-12-01 08:07:56 +01:00
|
|
|
nwarn("WARNING: Expected UDP prototype: %u vs %u\n",
|
2017-06-28 23:39:21 +02:00
|
|
|
ipv6udp->ipv6.proto, IP_PROTO_UDP);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-31 03:10:51 +02:00
|
|
|
struct netdev_varaddr_s destmac;
|
2017-06-28 23:39:21 +02:00
|
|
|
FAR uint8_t *buf;
|
|
|
|
uint16_t hdrlen;
|
|
|
|
uint16_t buflen;
|
2017-06-30 18:13:31 +02:00
|
|
|
int ret;
|
2017-06-28 23:39:21 +02:00
|
|
|
|
2017-11-22 19:10:39 +01:00
|
|
|
/* Get the IEEE 802.15.4 MAC address of the next hop. */
|
2017-06-28 23:39:21 +02:00
|
|
|
|
2017-11-22 19:10:39 +01:00
|
|
|
ret = sixlowpan_nexthopaddr((FAR struct radio_driver_s *)fwddev,
|
|
|
|
ipv6udp->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-06-28 23:39:21 +02:00
|
|
|
|
2017-06-29 16:16:46 +02:00
|
|
|
/* Get the IPv6 + UDP combined header length. */
|
2017-06-28 23:39:21 +02:00
|
|
|
|
2017-06-29 16:16:46 +02:00
|
|
|
hdrlen = IPv6_HDRLEN + UDP_HDRLEN;
|
2017-06-28 23:39:21 +02:00
|
|
|
|
|
|
|
/* Drop the packet if the buffer length is less than this. */
|
|
|
|
|
|
|
|
if (hdrlen > dev->d_len)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: Dropping small UDP packet: %u < %u\n",
|
2022-11-19 06:19:26 +01:00
|
|
|
dev->d_len, hdrlen);
|
2017-06-28 23:39:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Convert the outgoing packet into a frame list. */
|
|
|
|
|
|
|
|
buf = (FAR uint8_t *)ipv6 + hdrlen;
|
|
|
|
buflen = dev->d_len - hdrlen;
|
|
|
|
|
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
|
|
|
&ipv6udp->ipv6, buf, buflen, &destmac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 17:32:17 +02:00
|
|
|
drop:
|
2017-06-28 23:39:21 +02:00
|
|
|
dev->d_len = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-30 02:07:52 +02:00
|
|
|
#endif /* CONFIG_NET_6LOWPAN && CONFIG_NET_UDP */
|