2007-11-07 07:38:14 +08:00
|
|
|
/****************************************************************************
|
2014-06-19 00:50:09 +08:00
|
|
|
* net/udp/udp_callback.c
|
2007-11-07 07:38:14 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08: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 07:38:14 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-11-07 07:38:14 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08: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 07:38:14 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
|
|
|
|
|
2009-12-15 22:53:45 +08:00
|
|
|
#include <stdint.h>
|
2015-02-03 01:37:22 +08:00
|
|
|
#include <string.h>
|
2007-11-07 07:38:14 +08:00
|
|
|
#include <debug.h>
|
2023-11-17 15:44:49 +08:00
|
|
|
#include <sys/time.h>
|
2007-11-07 07:38:14 +08:00
|
|
|
|
2014-06-24 22:53:28 +08:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2014-06-24 23:28:44 +08:00
|
|
|
#include <nuttx/net/netdev.h>
|
2015-01-30 22:54:40 +08:00
|
|
|
#include <nuttx/net/netstats.h>
|
2014-06-26 23:32:39 +08:00
|
|
|
#include <nuttx/net/udp.h>
|
2007-11-07 07:38:14 +08:00
|
|
|
|
2014-06-29 08:07:02 +08:00
|
|
|
#include "devif/devif.h"
|
2014-06-25 08:55:01 +08:00
|
|
|
#include "udp/udp.h"
|
2023-07-04 13:24:51 +08:00
|
|
|
#include "utils/utils.h"
|
2007-11-07 07:38:14 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 20:08:26 +08:00
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: udp_datahandler
|
2015-01-30 20:08:26 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the receipt of UDP data by adding the newly received packet to
|
|
|
|
* the UDP read-ahead buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-26 01:31:42 +08:00
|
|
|
static uint16_t udp_datahandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct udp_conn_s *conn,
|
2015-01-30 20:08:26 +08:00
|
|
|
FAR uint8_t *buffer, uint16_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
int ret;
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2023-08-01 02:41:07 +08:00
|
|
|
struct sockaddr_in6 src_addr6 =
|
2019-03-19 23:49:54 +08:00
|
|
|
{
|
2019-06-03 23:23:19 +08:00
|
|
|
0
|
2019-03-19 23:49:54 +08:00
|
|
|
};
|
2015-01-30 20:08:26 +08:00
|
|
|
#endif
|
2019-10-26 01:31:42 +08:00
|
|
|
|
2015-01-30 20:08:26 +08:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2023-08-01 02:41:07 +08:00
|
|
|
struct sockaddr_in src_addr4 =
|
2019-03-19 23:49:54 +08:00
|
|
|
{
|
2019-06-03 23:23:19 +08:00
|
|
|
0
|
2019-03-19 23:49:54 +08:00
|
|
|
};
|
2015-01-30 20:08:26 +08:00
|
|
|
#endif
|
2019-10-26 01:31:42 +08:00
|
|
|
|
2015-01-30 20:08:26 +08:00
|
|
|
uint8_t src_addr_size;
|
2023-07-04 13:24:51 +08:00
|
|
|
FAR void *src_addr;
|
|
|
|
int offset;
|
2015-01-30 20:08:26 +08:00
|
|
|
|
2021-07-05 16:01:48 +08:00
|
|
|
#if CONFIG_NET_RECV_BUFSIZE > 0
|
2023-07-04 13:24:51 +08:00
|
|
|
if (conn->readahead && conn->readahead->io_pktlen > conn->rcvbufs)
|
2021-07-05 16:01:48 +08:00
|
|
|
{
|
2023-06-08 10:50:53 +08:00
|
|
|
netdev_iob_release(dev);
|
2023-02-22 19:10:45 +08:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.udp.drop++;
|
|
|
|
#endif
|
2023-06-08 10:50:53 +08:00
|
|
|
return 0;
|
2021-07-05 16:01:48 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-23 19:39:40 +08:00
|
|
|
iob = dev->d_iob;
|
2015-01-30 20:08:26 +08: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-17 04:33:58 +08:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-02-17 21:37:44 +08:00
|
|
|
/* Hybrid dual-stack IPv6/IPv4 implementations recognize a special
|
|
|
|
* class of addresses, the IPv4-mapped IPv6 addresses.
|
|
|
|
*/
|
|
|
|
|
2016-02-26 20:45:37 +08:00
|
|
|
if (conn->domain == PF_INET6)
|
2015-02-17 04:33:58 +08:00
|
|
|
{
|
2023-08-18 17:54:07 +08:00
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv4BUF;
|
|
|
|
FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
|
2015-02-17 21:37:44 +08:00
|
|
|
in_addr_t ipv4addr;
|
2015-02-17 04:33:58 +08:00
|
|
|
|
2023-08-18 17:54:07 +08:00
|
|
|
/* Encode the IPv4 address as an IPv4-mapped IPv6 address */
|
2015-02-17 04:33:58 +08:00
|
|
|
|
2015-02-17 21:37:44 +08:00
|
|
|
src_addr6.sin6_family = AF_INET6;
|
|
|
|
src_addr6.sin6_port = udp->srcport;
|
2015-02-17 04:33:58 +08:00
|
|
|
|
2023-08-18 17:54:07 +08:00
|
|
|
ipv4addr = net_ip4addr_conv32(ipv4->srcipaddr);
|
2015-05-28 01:39:44 +08:00
|
|
|
ip6_map_ipv4addr(ipv4addr, src_addr6.sin6_addr.s6_addr16);
|
2015-02-17 04:33:58 +08:00
|
|
|
|
|
|
|
src_addr_size = sizeof(src_addr6);
|
|
|
|
src_addr = &src_addr6;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2015-05-28 01:39:44 +08:00
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv4BUF;
|
|
|
|
FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
|
|
|
|
|
2015-02-17 04:33:58 +08:00
|
|
|
src_addr4.sin_family = AF_INET;
|
|
|
|
src_addr4.sin_port = udp->srcport;
|
2015-01-30 20:08:26 +08:00
|
|
|
|
2015-02-17 04:33:58 +08:00
|
|
|
net_ipv4addr_copy(src_addr4.sin_addr.s_addr,
|
2015-01-30 20:08:26 +08:00
|
|
|
net_ip4addr_conv32(ipv4->srcipaddr));
|
2020-02-25 22:53:39 +08:00
|
|
|
memset(src_addr4.sin_zero, 0, sizeof(src_addr4.sin_zero));
|
2015-01-30 20:08:26 +08:00
|
|
|
|
2015-02-17 04:33:58 +08:00
|
|
|
src_addr_size = sizeof(src_addr4);
|
|
|
|
src_addr = &src_addr4;
|
|
|
|
}
|
2015-01-30 20:08:26 +08:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
2023-07-04 13:24:51 +08:00
|
|
|
/* Copy the meta info into the I/O buffer chain, just before data.
|
2023-11-17 15:44:49 +08:00
|
|
|
* Layout: |datalen|ifindex|src_addr_size|src_addr|[timestamp]|data|
|
2023-07-04 13:24:51 +08:00
|
|
|
*/
|
2015-01-30 20:08:26 +08:00
|
|
|
|
2023-07-04 13:24:51 +08:00
|
|
|
offset = (dev->d_appdata - iob->io_data) - iob->io_offset;
|
2022-08-24 14:55:56 +08:00
|
|
|
|
2023-11-17 15:44:49 +08: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 13:24:51 +08:00
|
|
|
offset -= src_addr_size;
|
|
|
|
ret = iob_trycopyin(iob, src_addr, src_addr_size, offset, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2015-01-30 20:08:26 +08:00
|
|
|
|
2023-07-04 13:24:51 +08: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 20:08:26 +08:00
|
|
|
|
2023-07-04 13:24:51 +08: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 20:08:26 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-07-04 13:24:51 +08:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-23 19:39:40 +08:00
|
|
|
|
2023-07-04 13:24:51 +08: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 20:08:26 +08:00
|
|
|
}
|
2023-07-04 13:24:51 +08:00
|
|
|
|
2023-08-18 17:54:07 +08:00
|
|
|
/* Reset new offset to point at start point. */
|
2023-07-04 13:24:51 +08:00
|
|
|
|
2023-08-18 17:54:07 +08:00
|
|
|
DEBUGASSERT(iob->io_offset + offset >= 0);
|
|
|
|
iob_reserve(iob, iob->io_offset + offset);
|
2023-07-04 13:24:51 +08:00
|
|
|
|
|
|
|
/* Concat the iob to readahead */
|
|
|
|
|
|
|
|
net_iob_concat(&conn->readahead, &iob);
|
|
|
|
|
2019-12-31 23:26:14 +08:00
|
|
|
#ifdef CONFIG_NET_UDP_NOTIFIER
|
2023-07-04 13:24:51 +08:00
|
|
|
ninfo("Buffered %d bytes\n", buflen);
|
2022-11-23 19:39:40 +08:00
|
|
|
|
2023-07-04 13:24:51 +08:00
|
|
|
/* Provided notification(s) that additional UDP read-ahead data is
|
|
|
|
* available.
|
|
|
|
*/
|
2018-09-09 23:21:39 +08:00
|
|
|
|
2023-07-04 13:24:51 +08:00
|
|
|
udp_readahead_signal(conn);
|
2018-09-09 23:21:39 +08:00
|
|
|
#endif
|
|
|
|
|
2022-11-23 19:39:40 +08:00
|
|
|
netdev_iob_clear(dev);
|
2015-01-30 20:08:26 +08:00
|
|
|
return buflen;
|
2023-07-04 13:24:51 +08:00
|
|
|
|
|
|
|
errout:
|
|
|
|
nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
|
|
|
|
|
|
|
|
netdev_iob_release(dev);
|
|
|
|
return 0;
|
2015-01-30 20:08:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: net_dataevent
|
2015-01-30 20:08:26 +08: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 21:55:40 +08:00
|
|
|
uint8_t *buffer = dev->d_appdata;
|
|
|
|
int buflen = dev->d_len;
|
|
|
|
uint16_t recvlen;
|
2015-01-30 20:08:26 +08: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-21 01:59:15 +08:00
|
|
|
ninfo("No receive on connection\n");
|
2015-01-30 20:08:26 +08:00
|
|
|
|
2015-08-11 21:55:40 +08:00
|
|
|
/* Save as the packet data as in the read-ahead buffer. NOTE that
|
|
|
|
* partial packets will not be buffered.
|
|
|
|
*/
|
2015-01-30 20:08:26 +08:00
|
|
|
|
2015-08-11 21:55:40 +08: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 20:08:26 +08:00
|
|
|
|
2016-06-21 01:59:15 +08:00
|
|
|
ninfo("Dropped %d bytes\n", dev->d_len);
|
2015-01-30 20:08:26 +08:00
|
|
|
|
2015-08-11 21:55:40 +08:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
g_netstats.udp.drop++;
|
2015-01-30 20:08:26 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In any event, the new data has now been handled */
|
|
|
|
|
|
|
|
dev->d_len = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-11-07 07:38:14 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: udp_callback
|
2007-11-07 07:38:14 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Inform the application holding the UDP socket of a change in state.
|
|
|
|
*
|
2013-09-06 23:18:07 +08:00
|
|
|
* Returned Value:
|
|
|
|
* OK if packet has been processed, otherwise ERROR.
|
|
|
|
*
|
2007-11-07 07:38:14 +08:00
|
|
|
* Assumptions:
|
2017-09-03 00:27:03 +08:00
|
|
|
* This function must be called with the network locked.
|
2007-11-07 07:38:14 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 06:48:12 +08:00
|
|
|
uint16_t udp_callback(FAR struct net_driver_s *dev,
|
2014-06-25 08:55:01 +08:00
|
|
|
FAR struct udp_conn_s *conn, uint16_t flags)
|
2007-11-07 07:38:14 +08:00
|
|
|
{
|
2016-06-21 01:59:15 +08:00
|
|
|
ninfo("flags: %04x\n", flags);
|
2007-11-07 07:38:14 +08:00
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
2008-09-01 23:22:54 +08:00
|
|
|
if (conn)
|
2007-11-07 07:38:14 +08:00
|
|
|
{
|
|
|
|
/* Perform the callback */
|
|
|
|
|
2022-08-25 21:17:57 +08:00
|
|
|
flags = devif_conn_event(dev, flags, conn->sconn.list);
|
2015-01-30 20:08:26 +08:00
|
|
|
|
|
|
|
if ((flags & UDP_NEWDATA) != 0)
|
|
|
|
{
|
|
|
|
/* Data was not handled.. dispose of it appropriately */
|
|
|
|
|
|
|
|
flags = net_dataevent(dev, conn, flags);
|
|
|
|
}
|
2007-11-07 07:38:14 +08:00
|
|
|
}
|
2013-09-06 23:18:07 +08:00
|
|
|
|
2013-10-12 00:48:00 +08:00
|
|
|
return flags;
|
2007-11-07 07:38:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_UDP */
|