incubator-nuttx/net/inet/ipv4_setsockopt.c

399 lines
12 KiB
C
Raw Normal View History

/****************************************************************************
* net/inet/ipv4_setsockopt.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/net/net.h>
#include <netinet/in.h>
#include "netdev/netdev.h"
#include "netfilter/iptables.h"
#include "igmp/igmp.h"
#include "inet/inet.h"
#include "socket/socket.h"
#include "udp/udp.h"
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_SOCKOPTS)
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ipv4_setsockopt
*
* Description:
* ipv4_setsockopt() sets the IPv4-protocol socket option specified by the
* 'option' argument to the value pointed to by the 'value' argument for
* the socket specified by the 'psock' argument.
*
* See <netinet/in.h> for the a complete list of values of IPv4 protocol
* socket options.
*
* Input Parameters:
* psock Socket structure of socket to operate on
* option identifies the option to set
* value Points to the argument value
* value_len The length of the argument value
*
* Returned Value:
* Returns zero (OK) on success. On failure, it returns a negated errno
* value to indicate the nature of the error. See psock_setcockopt() for
* the list of possible error values.
*
****************************************************************************/
int ipv4_setsockopt(FAR struct socket *psock, int option,
FAR const void *value, socklen_t value_len)
{
int ret;
ninfo("option: %d\n", option);
/* With IPv4, the multicast-related socket options are simply an
* alternative way to access IGMP. That IGMP functionality can also be
* accessed via IOCTL commands (see netdev/netdev_ioctl.c)
*
* REVISIT: Clone the logic from netdev_ioctl.c here.
*/
net_lock();
switch (option)
{
#ifdef CONFIG_NET_IGMP
case IP_MSFILTER: /* Access advanced, full-state filtering API */
{
#if 0 /* REVISIT: This is not a proper implementation of IP_MSGFILTER */
FAR const struct ip_msfilter *imsf;
FAR struct net_driver_s *dev;
imsf = (FAR const struct ip_msfilter *)value;
if (imsf == NULL || value_len < sizeof(struct ip_msfilter))
{
nerr("ERROR: Bad value or value_len\n");
ret = -EINVAL;
}
else
{
/* Get the device associated with the local interface address */
dev = netdev_findby_lipv4addr(imsf->imsf_interface.s_addr);
if (dev == NULL)
{
nwarn("WARNING: Could not find device\n");
ret = -ENODEV;
}
else if (imsf->imsf_fmode == MCAST_INCLUDE)
{
ret = igmp_joingroup(dev, &imsf->imsf_multiaddr);
}
else
{
DEBUGASSERT(imsf->imsf_fmode == MCAST_EXCLUDE);
ret = igmp_leavegroup(dev, &imsf->imsf_multiaddr);
}
}
#else
ret = -ENOSYS;
#endif
}
break;
#ifdef NET_UDP_HAVE_STACK
case IP_ADD_MEMBERSHIP: /* Join a multicast group */
case IP_DROP_MEMBERSHIP: /* Leave a multicast group */
{
FAR const struct ip_mreq *mrec;
FAR struct net_driver_s *dev;
/* REVISIT: This is not a proper implementation of IP_MSGFILTER */
mrec = (FAR const struct ip_mreq *)value;
if (mrec == NULL || value_len < sizeof(struct ip_mreq))
{
nerr("ERROR: Bad value or value_len\n");
ret = -EINVAL;
}
else
{
FAR struct udp_conn_s *conn = psock->s_conn;
/* Use the default network device is imr_interface is
* INADDRY_ANY.
*/
if (mrec->imr_interface.s_addr == INADDR_ANY)
{
dev = netdev_default();
}
else
{
/* Get the device associated with the local interface
* address
*/
dev = netdev_findby_lipv4addr(mrec->imr_interface.s_addr);
}
if (dev == NULL)
{
nwarn("WARNING: Could not find device\n");
ret = -ENODEV;
}
else if (option == IP_ADD_MEMBERSHIP)
{
if (conn->mreq.imr_multiaddr.s_addr != 0)
{
ret = -EADDRINUSE;
}
else
{
ret = igmp_joingroup(dev, &mrec->imr_multiaddr);
if (ret == OK)
{
conn->mreq.imr_multiaddr = mrec->imr_multiaddr;
conn->mreq.imr_ifindex = dev->d_ifindex;
}
}
}
else
{
ret = igmp_leavegroup(dev, &mrec->imr_multiaddr);
if (ret == OK)
{
conn->mreq.imr_multiaddr.s_addr = 0;
conn->mreq.imr_ifindex = 0;
}
}
}
}
break;
case IP_MULTICAST_TTL: /* Set/read the time-to-live value of
* outgoing multicast packets */
#endif
case IP_TTL: /* The IP TTL (time to live) of IP
* packets sent by the network stack */
{
FAR struct socket_conn_s *conn;
int ttl;
if (value == NULL || value_len == 0)
{
ret = -EINVAL;
break;
}
ttl = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
if (ttl < 0 || ttl > 255)
{
ret = -EINVAL;
}
else
{
conn = psock->s_conn;
conn->s_ttl = ttl;
ret = OK;
}
}
break;
case IP_MULTICAST_IF: /* Set local device for a multicast
* socket */
net:add IP_MULTICAST_IF & IPV6_MULTICAST_IF function implementation refer to https://man7.org/linux/man-pages/man7/ip.7.html IP_MULTICAST_IF (since Linux 1.2) Set the local device for a multicast socket. The argument for setsockopt(2) is an ip_mreqn or (since Linux 3.5) ip_mreq structure similar to IP_ADD_MEMBERSHIP, or an in_addr structure. (The kernel determines which structure is being passed based on the size passed in optlen.) For getsockopt(2), the argument is an in_addr structure. refer to https://man7.org/linux/man-pages/man7/ipv6.7.html IPV6_MULTICAST_IF Set the device for outgoing multicast packets on the socket. This is allowed only for SOCK_DGRAM and SOCK_RAW socket. The argument is a pointer to an interface index (see netdevice(7)) in an integer. testcase1: TEST_IMPL(udp_multicast_interface) { /* TODO(gengjiawen): Fix test on QEMU. */ RETURN_SKIP("Test does not currently work in QEMU"); int r; uv_udp_send_t req; uv_buf_t buf; struct sockaddr_in addr; struct sockaddr_in baddr; close_cb_called = 0; sv_send_cb_called = 0; ASSERT(0 == uv_ip4_addr("239.255.0.1", TEST_PORT, &addr)); r = uv_udp_init(uv_default_loop(), &server); ASSERT(r == 0); ASSERT(0 == uv_ip4_addr("0.0.0.0", 0, &baddr)); r = uv_udp_bind(&server, (const struct sockaddr*)&baddr, 0); ASSERT(r == 0); r = uv_udp_set_multicast_interface(&server, "0.0.0.0"); ASSERT(r == 0); /* server sends "PING" */ buf = uv_buf_init("PING", 4); r = uv_udp_send(&req, &server, &buf, 1, (const struct sockaddr*)&addr, sv_send_cb); ASSERT(r == 0); ASSERT(close_cb_called == 0); ASSERT(sv_send_cb_called == 0); /* run the loop till all events are processed */ uv_run(uv_default_loop(), UV_RUN_DEFAULT); ASSERT(sv_send_cb_called == 1); ASSERT(close_cb_called == 1); ASSERT(client.send_queue_size == 0); ASSERT(server.send_queue_size == 0); MAKE_VALGRIND_HAPPY(); return 0; } testcase2: TEST_IMPL(udp_multicast_interface6) { /* TODO(gengjiawen): Fix test on QEMU. */ RETURN_SKIP("Test does not currently work in QEMU"); int r; uv_udp_send_t req; uv_buf_t buf; struct sockaddr_in6 addr; struct sockaddr_in6 baddr; if (!can_ipv6()) RETURN_SKIP("IPv6 not supported"); close_cb_called = 0; sv_send_cb_called = 0; ASSERT(0 == uv_ip6_addr("::1", TEST_PORT, &addr)); r = uv_udp_init(uv_default_loop(), &server); ASSERT(r == 0); ASSERT(0 == uv_ip6_addr("::", 0, &baddr)); r = uv_udp_bind(&server, (const struct sockaddr*)&baddr, 0); ASSERT(r == 0); r = uv_udp_set_multicast_interface(&server, "::1%lo0"); r = uv_udp_set_multicast_interface(&server, NULL); ASSERT(r == 0); /* server sends "PING" */ buf = uv_buf_init("PING", 4); r = uv_udp_send(&req, &server, &buf, 1, (const struct sockaddr*)&addr, sv_send_cb); ASSERT(r == 0); ASSERT(close_cb_called == 0); ASSERT(sv_send_cb_called == 0); /* run the loop till all events are processed */ uv_run(uv_default_loop(), UV_RUN_DEFAULT); ASSERT(sv_send_cb_called == 1); ASSERT(close_cb_called == 1); MAKE_VALGRIND_HAPPY(); return 0; } Signed-off-by: wangchen <wangchen41@xiaomi.com>
2023-08-21 10:49:02 +08:00
#ifdef NET_UDP_HAVE_STACK
{
FAR struct udp_conn_s *conn;
FAR struct net_driver_s *dev;
struct ip_mreqn mreq;
conn = psock->s_conn;
if (value == NULL || value_len == 0)
{
ret = -EINVAL;
break;
}
if (value_len >= sizeof(struct ip_mreqn))
{
memcpy(&mreq, value, sizeof(mreq));
}
else
{
memset(&mreq, 0, sizeof(mreq));
if (value_len >= sizeof(struct ip_mreq))
{
memcpy(&mreq, value, sizeof(struct ip_mreq));
}
else if (value_len >= sizeof(struct in_addr))
{
memcpy(&mreq.imr_multiaddr,
value, sizeof(struct in_addr));
}
}
if (!mreq.imr_ifindex)
{
if (net_ipv4addr_cmp(mreq.imr_multiaddr.s_addr, INADDR_ANY))
{
conn->mreq.imr_interface.s_addr = 0;
conn->mreq.imr_ifindex = 0;
ret = OK;
break;
}
dev = netdev_findby_lipv4addr(mreq.imr_multiaddr.s_addr);
if (dev)
{
mreq.imr_ifindex = dev->d_ifindex;
}
}
else
{
dev = netdev_findbyindex(mreq.imr_ifindex);
}
if (!dev)
{
ret = -EADDRNOTAVAIL;
break;
}
#ifdef CONFIG_NET_BINDTODEVICE
if (conn->sconn.s_boundto &&
mreq.imr_ifindex != conn->sconn.s_boundto)
{
ret = -EINVAL;
break;
}
#endif
conn->mreq.imr_interface.s_addr = mreq.imr_multiaddr.s_addr;
conn->mreq.imr_ifindex = mreq.imr_ifindex;
ret = OK;
break;
}
#endif
/* The following IPv4 socket options are defined, but not implemented */
case IP_UNBLOCK_SOURCE: /* Unblock previously blocked multicast
* source */
case IP_BLOCK_SOURCE: /* Stop receiving multicast data from
* source */
case IP_ADD_SOURCE_MEMBERSHIP: /* Join a multicast group; allow receive
* only from source */
case IP_DROP_SOURCE_MEMBERSHIP: /* Leave a source-specific group. Stop
* receiving data from a given multicast
* group that come from a given source */
case IP_MULTICAST_ALL: /* Modify the delivery policy of
* multicast messages bound to
* INADDR_ANY */
/* #warning Missing logic */
nwarn("WARNING: Unimplemented IPv4 option: %d\n", option);
ret = -ENOSYS;
break;
case IP_MULTICAST_LOOP: /* Set/read boolean that determines
* whether sent multicast packets
* should be looped back to local
* sockets. */
#endif /* CONFIG_NET_IGMP */
case IP_PKTINFO:
{
FAR struct socket_conn_s *conn;
int enable;
if (value == NULL || value_len == 0)
{
ret = -EINVAL;
break;
}
enable = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
conn = psock->s_conn;
if (enable)
{
_SO_SETOPT(conn->s_options, option);
}
else
{
_SO_CLROPT(conn->s_options, option);
}
ret = OK;
}
break;
case IP_TOS:
{
FAR struct socket_conn_s *conn = psock->s_conn;
int tos;
tos = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
if (tos < 0 || tos > 0xff)
{
nerr("ERROR: invalid tos:%d\n", tos);
ret = -EINVAL;
}
else
{
conn->s_tos = tos;
ret = OK;
}
}
break;
#ifdef CONFIG_NET_IPTABLES
case IPT_SO_SET_REPLACE:
ret = ipt_setsockopt(psock, option, value, value_len);
break;
#endif
default:
nerr("ERROR: Unrecognized IPv4 option: %d\n", option);
ret = -ENOPROTOOPT;
break;
}
net_unlock();
return ret;
}
#endif /* CONFIG_NET_IPv4 */