2007-11-07 00:38:14 +01:00
|
|
|
/****************************************************************************
|
2014-06-18 18:50:09 +02:00
|
|
|
* net/udp/udp_callback.c
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01: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
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01: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.
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
|
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
#include <stdint.h>
|
2015-02-02 18:37:22 +01:00
|
|
|
#include <string.h>
|
2007-11-07 00:38:14 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2015-01-30 15:54:40 +01:00
|
|
|
#include <nuttx/net/netstats.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/udp.h>
|
2007-11-07 00:38:14 +01:00
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-06-25 02:55:01 +02:00
|
|
|
#include "udp/udp.h"
|
2007-11-07 00:38:14 +01:00
|
|
|
|
2015-01-30 13:08:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define IPv4BUF ((FAR struct ipv4_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
|
|
|
|
#define IPv6BUF ((FAR struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
|
|
|
|
|
|
|
|
#define UDPIPv4BUF ((FAR struct udp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv4_HDRLEN])
|
|
|
|
#define UDPIPv6BUF ((FAR struct udp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
|
|
|
|
|
2007-11-07 00:38:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 13:08:26 +01:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: udp_datahandler
|
2015-01-30 13:08:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the receipt of UDP data by adding the newly received packet to
|
|
|
|
* the UDP read-ahead buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-25 19:31:42 +02:00
|
|
|
static uint16_t udp_datahandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct udp_conn_s *conn,
|
2015-01-30 13:08:26 +01:00
|
|
|
FAR uint8_t *buffer, uint16_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
int ret;
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2019-03-19 16:49:54 +01:00
|
|
|
FAR struct sockaddr_in6 src_addr6 =
|
|
|
|
{
|
2019-06-03 17:23:19 +02:00
|
|
|
0
|
2019-03-19 16:49:54 +01:00
|
|
|
};
|
2015-01-30 13:08:26 +01:00
|
|
|
#endif
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2015-01-30 13:08:26 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2019-03-19 16:49:54 +01:00
|
|
|
FAR struct sockaddr_in src_addr4 =
|
|
|
|
{
|
2019-06-03 17:23:19 +02:00
|
|
|
0
|
2019-03-19 16:49:54 +01:00
|
|
|
};
|
2015-01-30 13:08:26 +01:00
|
|
|
#endif
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2015-01-30 13:08:26 +01:00
|
|
|
FAR void *src_addr;
|
|
|
|
uint8_t src_addr_size;
|
|
|
|
|
2021-07-05 10:01:48 +02:00
|
|
|
#if CONFIG_NET_RECV_BUFSIZE > 0
|
|
|
|
while (iob_get_queue_size(&conn->readahead) > conn->rcvbufs)
|
|
|
|
{
|
|
|
|
iob = iob_remove_queue(&conn->readahead);
|
|
|
|
iob_free_chain(iob, IOBUSER_NET_UDP_READAHEAD);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-30 13:08:26 +01:00
|
|
|
/* Allocate on I/O buffer to start the chain (throttling as necessary).
|
|
|
|
* We will not wait for an I/O buffer to become available in this context.
|
|
|
|
*/
|
|
|
|
|
2019-08-17 00:42:25 +02:00
|
|
|
iob = iob_tryalloc(true, IOBUSER_NET_UDP_READAHEAD);
|
2015-01-30 13:08:26 +01:00
|
|
|
if (iob == NULL)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to create new I/O buffer chain\n");
|
2015-01-30 13:08:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (IFF_IS_IPv6(dev->d_flags))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv6BUF;
|
|
|
|
FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
|
|
|
|
|
|
|
|
src_addr6.sin6_family = AF_INET6;
|
|
|
|
src_addr6.sin6_port = udp->srcport;
|
|
|
|
|
|
|
|
net_ipv6addr_copy(src_addr6.sin6_addr.s6_addr, ipv6->srcipaddr);
|
|
|
|
|
|
|
|
src_addr_size = sizeof(src_addr6);
|
|
|
|
src_addr = &src_addr6;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2015-02-16 21:33:58 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-02-17 14:37:44 +01:00
|
|
|
/* Hybrid dual-stack IPv6/IPv4 implementations recognize a special
|
|
|
|
* class of addresses, the IPv4-mapped IPv6 addresses.
|
|
|
|
*/
|
|
|
|
|
2016-02-26 13:45:37 +01:00
|
|
|
if (conn->domain == PF_INET6)
|
2015-02-16 21:33:58 +01:00
|
|
|
{
|
2015-05-27 19:39:44 +02:00
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv6BUF;
|
|
|
|
FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
|
2015-02-17 14:37:44 +01:00
|
|
|
in_addr_t ipv4addr;
|
2015-02-16 21:33:58 +01:00
|
|
|
|
2015-02-17 14:37:44 +01:00
|
|
|
/* Encode the IPv4 address as an IPv-mapped IPv6 address */
|
2015-02-16 21:33:58 +01:00
|
|
|
|
2015-02-17 14:37:44 +01:00
|
|
|
src_addr6.sin6_family = AF_INET6;
|
|
|
|
src_addr6.sin6_port = udp->srcport;
|
2015-02-16 21:33:58 +01:00
|
|
|
|
2015-05-27 19:39:44 +02:00
|
|
|
ipv4addr = net_ip4addr_conv32(ipv6->srcipaddr);
|
|
|
|
ip6_map_ipv4addr(ipv4addr, src_addr6.sin6_addr.s6_addr16);
|
2015-02-16 21:33:58 +01:00
|
|
|
|
|
|
|
src_addr_size = sizeof(src_addr6);
|
|
|
|
src_addr = &src_addr6;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2015-05-27 19:39:44 +02:00
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv4BUF;
|
|
|
|
FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
|
|
|
|
|
2015-02-16 21:33:58 +01:00
|
|
|
src_addr4.sin_family = AF_INET;
|
|
|
|
src_addr4.sin_port = udp->srcport;
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-02-16 21:33:58 +01:00
|
|
|
net_ipv4addr_copy(src_addr4.sin_addr.s_addr,
|
2015-01-30 13:08:26 +01:00
|
|
|
net_ip4addr_conv32(ipv4->srcipaddr));
|
2020-02-25 15:53:39 +01:00
|
|
|
memset(src_addr4.sin_zero, 0, sizeof(src_addr4.sin_zero));
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-02-16 21:33:58 +01:00
|
|
|
src_addr_size = sizeof(src_addr4);
|
|
|
|
src_addr = &src_addr4;
|
|
|
|
}
|
2015-01-30 13:08:26 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
/* Copy the src address info into the I/O buffer chain. We will not wait
|
|
|
|
* for an I/O buffer to become available in this context. It there is
|
|
|
|
* any failure to allocated, the entire I/O buffer chain will be discarded.
|
|
|
|
*/
|
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
ret = iob_trycopyin(iob, (FAR const uint8_t *)&src_addr_size,
|
2019-08-17 00:42:25 +02:00
|
|
|
sizeof(uint8_t), 0, true, IOBUSER_NET_UDP_READAHEAD);
|
2015-01-30 13:08:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* On a failure, iob_trycopyin return a negated error value but does
|
|
|
|
* not free any I/O buffers.
|
|
|
|
*/
|
|
|
|
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret);
|
2020-01-02 17:49:34 +01:00
|
|
|
iob_free_chain(iob, IOBUSER_NET_UDP_READAHEAD);
|
2015-01-30 13:08:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
ret = iob_trycopyin(iob, (FAR const uint8_t *)src_addr, src_addr_size,
|
2019-08-17 00:42:25 +02:00
|
|
|
sizeof(uint8_t), true, IOBUSER_NET_UDP_READAHEAD);
|
2015-01-30 13:08:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* On a failure, iob_trycopyin return a negated error value but does
|
|
|
|
* not free any I/O buffers.
|
|
|
|
*/
|
|
|
|
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret);
|
2020-01-02 17:49:34 +01:00
|
|
|
iob_free_chain(iob, IOBUSER_NET_UDP_READAHEAD);
|
2015-01-30 13:08:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-11 15:55:40 +02:00
|
|
|
if (buflen > 0)
|
2015-01-30 13:08:26 +01:00
|
|
|
{
|
2015-08-11 15:55:40 +02:00
|
|
|
/* Copy the new appdata into the I/O buffer chain */
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-08-11 15:55:40 +02:00
|
|
|
ret = iob_trycopyin(iob, buffer, buflen,
|
2019-08-17 00:42:25 +02:00
|
|
|
src_addr_size + sizeof(uint8_t), true,
|
|
|
|
IOBUSER_NET_UDP_READAHEAD);
|
2015-08-11 15:55:40 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* On a failure, iob_trycopyin return a negated error value but
|
|
|
|
* does not free any I/O buffers.
|
|
|
|
*/
|
|
|
|
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n",
|
|
|
|
ret);
|
2020-01-02 17:49:34 +01:00
|
|
|
iob_free_chain(iob, IOBUSER_NET_UDP_READAHEAD);
|
2015-08-11 15:55:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-30 13:08:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the new I/O buffer chain to the tail of the read-ahead queue */
|
|
|
|
|
|
|
|
ret = iob_tryadd_queue(iob, &conn->readahead);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
|
2020-01-02 17:49:34 +01:00
|
|
|
iob_free_chain(iob, IOBUSER_NET_UDP_READAHEAD);
|
2015-01-30 13:08:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
#ifdef CONFIG_NET_UDP_NOTIFIER
|
2018-09-09 17:21:39 +02:00
|
|
|
/* Provided notification(s) that additional UDP read-ahead data is
|
|
|
|
* available.
|
|
|
|
*/
|
|
|
|
|
2019-07-01 20:25:32 +02:00
|
|
|
udp_readahead_signal(conn);
|
2018-09-09 17:21:39 +02:00
|
|
|
#endif
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Buffered %d bytes\n", buflen);
|
2015-01-30 13:08:26 +01:00
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: net_dataevent
|
2015-01-30 13:08:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handling the network UDP_NEWDATA event.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint16_t
|
|
|
|
net_dataevent(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn,
|
|
|
|
uint16_t flags)
|
|
|
|
{
|
|
|
|
uint16_t ret;
|
2015-08-11 15:55:40 +02:00
|
|
|
uint8_t *buffer = dev->d_appdata;
|
|
|
|
int buflen = dev->d_len;
|
|
|
|
uint16_t recvlen;
|
2015-01-30 13:08:26 +01:00
|
|
|
|
|
|
|
ret = (flags & ~UDP_NEWDATA);
|
|
|
|
|
|
|
|
/* Is there new data? With non-zero length? (Certain connection events
|
|
|
|
* can have zero-length with UDP_NEWDATA set just to cause an ACK).
|
|
|
|
*/
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("No receive on connection\n");
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-08-11 15:55:40 +02:00
|
|
|
/* Save as the packet data as in the read-ahead buffer. NOTE that
|
|
|
|
* partial packets will not be buffered.
|
|
|
|
*/
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-08-11 15:55:40 +02:00
|
|
|
recvlen = udp_datahandler(dev, conn, buffer, buflen);
|
|
|
|
if (recvlen < buflen)
|
|
|
|
{
|
|
|
|
/* There is no handler to receive new data and there are no free
|
|
|
|
* read-ahead buffers to retain the data -- drop the packet.
|
|
|
|
*/
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Dropped %d bytes\n", dev->d_len);
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2015-08-11 15:55:40 +02:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.udp.drop++;
|
2015-01-30 13:08:26 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In any event, the new data has now been handled */
|
|
|
|
|
|
|
|
dev->d_len = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-11-07 00:38:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: udp_callback
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Inform the application holding the UDP socket of a change in state.
|
|
|
|
*
|
2013-09-06 17:18:07 +02:00
|
|
|
* Returned Value:
|
|
|
|
* OK if packet has been processed, otherwise ERROR.
|
|
|
|
*
|
2007-11-07 00:38:14 +01:00
|
|
|
* Assumptions:
|
2017-09-02 18:27:03 +02:00
|
|
|
* This function must be called with the network locked.
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
uint16_t udp_callback(FAR struct net_driver_s *dev,
|
2014-06-25 02:55:01 +02:00
|
|
|
FAR struct udp_conn_s *conn, uint16_t flags)
|
2007-11-07 00:38:14 +01:00
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("flags: %04x\n", flags);
|
2007-11-07 00:38:14 +01:00
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
2008-09-01 17:22:54 +02:00
|
|
|
if (conn)
|
2007-11-07 00:38:14 +01:00
|
|
|
{
|
|
|
|
/* Perform the callback */
|
|
|
|
|
2022-02-07 04:34:18 +01:00
|
|
|
flags = devif_conn_event(dev, conn, flags, conn->sconn.list);
|
2015-01-30 13:08:26 +01:00
|
|
|
|
|
|
|
if ((flags & UDP_NEWDATA) != 0)
|
|
|
|
{
|
|
|
|
/* Data was not handled.. dispose of it appropriately */
|
|
|
|
|
|
|
|
flags = net_dataevent(dev, conn, flags);
|
|
|
|
}
|
2007-11-07 00:38:14 +01:00
|
|
|
}
|
2013-09-06 17:18:07 +02:00
|
|
|
|
2013-10-11 18:48:00 +02:00
|
|
|
return flags;
|
2007-11-07 00:38:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_UDP */
|