2018-10-31 22:59:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/inet/ipv4_setsockopt.c
|
|
|
|
*
|
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
|
2018-10-31 22:59:05 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-10-31 22:59:05 +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.
|
2018-10-31 22:59:05 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2018-11-01 00:15:23 +01:00
|
|
|
#include <assert.h>
|
2018-10-31 22:59:05 +01:00
|
|
|
#include <errno.h>
|
2018-11-01 00:15:23 +01:00
|
|
|
#include <debug.h>
|
2018-10-31 22:59:05 +01:00
|
|
|
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2018-11-01 00:15:23 +01:00
|
|
|
#include "netdev/netdev.h"
|
2022-12-13 09:08:24 +01:00
|
|
|
#include "netfilter/iptables.h"
|
2018-11-01 00:15:23 +01:00
|
|
|
#include "igmp/igmp.h"
|
2018-10-31 22:59:05 +01:00
|
|
|
#include "inet/inet.h"
|
2023-04-21 11:42:57 +02:00
|
|
|
#include "socket/socket.h"
|
2023-06-30 11:24:07 +02:00
|
|
|
#include "udp/udp.h"
|
2018-10-31 22:59:05 +01:00
|
|
|
|
2023-04-21 11:42:57 +02:00
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_SOCKOPTS)
|
2018-10-31 22:59:05 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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)
|
|
|
|
{
|
2018-11-01 00:15:23 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ninfo("option: %d\n", option);
|
|
|
|
|
2020-11-27 04:19:33 +01:00
|
|
|
/* 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)
|
2018-10-31 22:59:05 +01:00
|
|
|
*
|
|
|
|
* REVISIT: Clone the logic from netdev_ioctl.c here.
|
|
|
|
*/
|
|
|
|
|
2018-11-03 23:47:08 +01:00
|
|
|
net_lock();
|
2018-10-31 22:59:05 +01:00
|
|
|
switch (option)
|
|
|
|
{
|
2022-11-23 10:08:48 +01:00
|
|
|
#ifdef CONFIG_NET_IGMP
|
2018-11-01 00:15:23 +01:00
|
|
|
case IP_MSFILTER: /* Access advanced, full-state filtering API */
|
|
|
|
{
|
2018-11-02 19:06:57 +01:00
|
|
|
#if 0 /* REVISIT: This is not a proper implementation of IP_MSGFILTER */
|
2018-11-01 00:15:23 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-11-27 04:19:33 +01:00
|
|
|
nwarn("WARNING: Could not find device\n");
|
2018-11-01 00:15:23 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-11-02 19:06:57 +01:00
|
|
|
#else
|
|
|
|
ret = -ENOSYS;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2020-11-27 04:19:33 +01:00
|
|
|
/* Use the default network device is imr_interface is
|
|
|
|
* INADDRY_ANY.
|
|
|
|
*/
|
2018-11-02 19:06:57 +01:00
|
|
|
|
|
|
|
if (mrec->imr_interface.s_addr == INADDR_ANY)
|
|
|
|
{
|
|
|
|
dev = netdev_default();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-27 04:19:33 +01:00
|
|
|
/* Get the device associated with the local interface
|
|
|
|
* address
|
|
|
|
*/
|
2018-11-02 19:06:57 +01:00
|
|
|
|
|
|
|
dev = netdev_findby_lipv4addr(mrec->imr_interface.s_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev == NULL)
|
|
|
|
{
|
2020-11-27 04:19:33 +01:00
|
|
|
nwarn("WARNING: Could not find device\n");
|
2018-11-02 19:06:57 +01:00
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else if (option == IP_ADD_MEMBERSHIP)
|
|
|
|
{
|
|
|
|
ret = igmp_joingroup(dev, &mrec->imr_multiaddr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = igmp_leavegroup(dev, &mrec->imr_multiaddr);
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 00:15:23 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-03-06 17:55:34 +01:00
|
|
|
#ifdef NET_UDP_HAVE_STACK
|
2020-12-03 06:53:50 +01:00
|
|
|
case IP_MULTICAST_TTL: /* Set/read the time-to-live value of
|
|
|
|
* outgoing multicast packets */
|
2023-07-24 11:43:09 +02:00
|
|
|
#endif
|
|
|
|
case IP_TTL: /* The IP TTL (time to live) of IP
|
|
|
|
* packets sent by the network stack */
|
2020-12-03 06:53:50 +01:00
|
|
|
{
|
2023-05-12 08:25:33 +02:00
|
|
|
FAR struct socket_conn_s *conn;
|
2023-06-30 11:24:07 +02:00
|
|
|
int ttl;
|
2020-12-05 11:21:18 +01:00
|
|
|
|
2023-05-12 08:25:33 +02:00
|
|
|
if (value == NULL || value_len == 0)
|
2020-12-05 11:21:18 +01:00
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ttl = (value_len >= sizeof(int)) ?
|
|
|
|
*(FAR int *)value : (int)*(FAR unsigned char *)value;
|
|
|
|
|
2023-08-03 04:34:02 +02:00
|
|
|
if (ttl < 0 || ttl > 255)
|
2020-12-03 06:53:50 +01:00
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2020-12-05 11:21:18 +01:00
|
|
|
conn->ttl = ttl;
|
|
|
|
ret = OK;
|
2020-12-03 06:53:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-11-23 10:08:48 +01:00
|
|
|
|
|
|
|
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 04:49:02 +02: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 */
|
|
|
|
|
2022-11-23 10:08:48 +01:00
|
|
|
case IP_MULTICAST_LOOP: /* Set/read boolean that determines
|
|
|
|
* whether sent multicast packets
|
|
|
|
* should be looped back to local
|
|
|
|
* sockets. */
|
|
|
|
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 */
|
2023-05-01 15:55:35 +02:00
|
|
|
|
|
|
|
/* #warning Missing logic */
|
|
|
|
|
2022-11-23 10:08:48 +01:00
|
|
|
nwarn("WARNING: Unimplemented IPv4 option: %d\n", option);
|
|
|
|
ret = -ENOSYS;
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_NET_IGMP */
|
2020-12-03 06:53:50 +01:00
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
case IP_PKTINFO:
|
|
|
|
{
|
2023-04-21 11:42:57 +02:00
|
|
|
FAR struct socket_conn_s *conn;
|
2022-08-24 08:55:56 +02:00
|
|
|
int enable;
|
|
|
|
|
2023-04-21 11:42:57 +02:00
|
|
|
if (value == NULL || value_len == 0)
|
2022-08-24 08:55:56 +02:00
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
enable = (value_len >= sizeof(int)) ?
|
|
|
|
*(FAR int *)value : (int)*(FAR unsigned char *)value;
|
2023-04-21 11:42:57 +02:00
|
|
|
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2022-08-24 08:55:56 +02:00
|
|
|
if (enable)
|
|
|
|
{
|
2023-04-21 11:42:57 +02:00
|
|
|
_SO_SETOPT(conn->s_options, option);
|
2022-08-24 08:55:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-21 11:42:57 +02:00
|
|
|
_SO_CLROPT(conn->s_options, option);
|
2022-08-24 08:55:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
2023-04-21 11:42:57 +02:00
|
|
|
|
2022-12-21 05:40:42 +01:00
|
|
|
case IP_TOS:
|
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct socket_conn_s *conn = psock->s_conn;
|
2022-12-21 05:40:42 +01:00
|
|
|
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;
|
2022-08-24 08:55:56 +02:00
|
|
|
|
2022-12-13 09:08:24 +01:00
|
|
|
#ifdef CONFIG_NET_IPTABLES
|
|
|
|
case IPT_SO_SET_REPLACE:
|
|
|
|
ret = ipt_setsockopt(psock, option, value, value_len);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2018-10-31 22:59:05 +01:00
|
|
|
default:
|
2018-11-01 00:15:23 +01:00
|
|
|
nerr("ERROR: Unrecognized IPv4 option: %d\n", option);
|
|
|
|
ret = -ENOPROTOOPT;
|
2018-10-31 22:59:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-03 23:47:08 +01:00
|
|
|
net_unlock();
|
2018-11-01 00:15:23 +01:00
|
|
|
return ret;
|
2018-10-31 22:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|