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>
|
2023-11-17 08:44:49 +01:00
|
|
|
#include <sys/time.h>
|
2007-11-07 00:38:14 +01:00
|
|
|
|
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"
|
2023-07-04 07:24:51 +02:00
|
|
|
#include "utils/utils.h"
|
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
|
2023-07-31 20:41:07 +02:00
|
|
|
struct sockaddr_in6 src_addr6 =
|
2019-03-19 16:49:54 +01:00
|
|
|
{
|
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
|
2023-07-31 20:41:07 +02:00
|
|
|
struct sockaddr_in src_addr4 =
|
2019-03-19 16:49:54 +01:00
|
|
|
{
|
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
|
|
|
uint8_t src_addr_size;
|
2023-07-04 07:24:51 +02:00
|
|
|
FAR void *src_addr;
|
|
|
|
int offset;
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2021-07-05 10:01:48 +02:00
|
|
|
#if CONFIG_NET_RECV_BUFSIZE > 0
|
2023-07-04 07:24:51 +02:00
|
|
|
if (conn->readahead && conn->readahead->io_pktlen > conn->rcvbufs)
|
2021-07-05 10:01:48 +02:00
|
|
|
{
|
2023-06-08 04:50:53 +02:00
|
|
|
netdev_iob_release(dev);
|
2023-02-22 12:10:45 +01:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.udp.drop++;
|
|
|
|
#endif
|
2023-06-08 04:50:53 +02:00
|
|
|
return 0;
|
2021-07-05 10:01:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
iob = dev->d_iob;
|
2015-01-30 13:08:26 +01:00
|
|
|
|
|
|
|
#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
|
|
|
{
|
2023-08-18 11:54:07 +02:00
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv4BUF;
|
|
|
|
FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
|
2015-02-17 14:37:44 +01:00
|
|
|
in_addr_t ipv4addr;
|
2015-02-16 21:33:58 +01:00
|
|
|
|
2023-08-18 11:54:07 +02:00
|
|
|
/* Encode the IPv4 address as an IPv4-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
|
|
|
|
2023-08-18 11:54:07 +02:00
|
|
|
ipv4addr = net_ip4addr_conv32(ipv4->srcipaddr);
|
2015-05-27 19:39:44 +02:00
|
|
|
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 */
|
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
/* Copy the meta info into the I/O buffer chain, just before data.
|
2023-11-17 08:44:49 +01:00
|
|
|
* Layout: |datalen|ifindex|src_addr_size|src_addr|[timestamp]|data|
|
2023-07-04 07:24:51 +02:00
|
|
|
*/
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
offset = (dev->d_appdata - iob->io_data) - iob->io_offset;
|
2022-08-24 08:55:56 +02:00
|
|
|
|
2023-11-17 08:44:49 +01:00
|
|
|
#ifdef CONFIG_NET_TIMESTAMP
|
|
|
|
/* Store timestamp while packet is being queued.
|
|
|
|
* This is done unconditionally to avoid race condition when SO_TIMESTAMP
|
|
|
|
* gets enabled after packet is received but before it is read.
|
|
|
|
*/
|
|
|
|
|
|
|
|
offset -= sizeof(struct timespec);
|
|
|
|
ret = iob_trycopyin(iob, (FAR const uint8_t *)&dev->d_rxtime,
|
|
|
|
sizeof(struct timespec), offset, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
offset -= src_addr_size;
|
|
|
|
ret = iob_trycopyin(iob, src_addr, src_addr_size, offset, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
offset -= sizeof(src_addr_size);
|
|
|
|
ret = iob_trycopyin(iob, &src_addr_size, sizeof(src_addr_size),
|
|
|
|
offset, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2015-01-30 13:08:26 +01:00
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
#ifdef CONFIG_NETDEV_IFINDEX
|
|
|
|
offset -= sizeof(dev->d_ifindex);
|
|
|
|
ret = iob_trycopyin(iob, &dev->d_ifindex, sizeof(dev->d_ifindex),
|
|
|
|
offset, true);
|
2015-01-30 13:08:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-07-04 07:24:51 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-23 12:39:40 +01:00
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
offset -= sizeof(buflen);
|
|
|
|
ret = iob_trycopyin(iob, (FAR const uint8_t *)&buflen, sizeof(buflen),
|
|
|
|
offset, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
2015-01-30 13:08:26 +01:00
|
|
|
}
|
2023-07-04 07:24:51 +02:00
|
|
|
|
2023-08-18 11:54:07 +02:00
|
|
|
/* Reset new offset to point at start point. */
|
2023-07-04 07:24:51 +02:00
|
|
|
|
2023-08-18 11:54:07 +02:00
|
|
|
DEBUGASSERT(iob->io_offset + offset >= 0);
|
|
|
|
iob_reserve(iob, iob->io_offset + offset);
|
2023-07-04 07:24:51 +02:00
|
|
|
|
|
|
|
/* Concat the iob to readahead */
|
|
|
|
|
|
|
|
net_iob_concat(&conn->readahead, &iob);
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
#ifdef CONFIG_NET_UDP_NOTIFIER
|
2023-07-04 07:24:51 +02:00
|
|
|
ninfo("Buffered %d bytes\n", buflen);
|
2022-11-23 12:39:40 +01:00
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
/* Provided notification(s) that additional UDP read-ahead data is
|
|
|
|
* available.
|
|
|
|
*/
|
2018-09-09 17:21:39 +02:00
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
udp_readahead_signal(conn);
|
2018-09-09 17:21:39 +02:00
|
|
|
#endif
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
netdev_iob_clear(dev);
|
2015-01-30 13:08:26 +01:00
|
|
|
return buflen;
|
2023-07-04 07:24:51 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
|
|
|
|
|
|
|
|
netdev_iob_release(dev);
|
|
|
|
return 0;
|
2015-01-30 13:08:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
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-08-25 15:17:57 +02:00
|
|
|
flags = devif_conn_event(dev, 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 */
|