nuttx/net/inet/ipv6_setsockopt.c

228 lines
6.6 KiB
C
Raw Normal View History

/****************************************************************************
* net/inet/ipv6_setsockopt.c
*
* 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 <errno.h>
#include <debug.h>
#include <netinet/in.h>
#include <nuttx/net/net.h>
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
#include "netdev/netdev.h"
#include "mld/mld.h"
#include "inet/inet.h"
#include "socket/socket.h"
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
#include "udp/udp.h"
#if defined(CONFIG_NET_IPv6) && defined(CONFIG_NET_SOCKOPTS)
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ipv6_setsockopt
*
* Description:
* ipv6_setsockopt() sets the IPv6-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 IPv6 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 ipv6_setsockopt(FAR struct socket *psock, int option,
FAR const void *value, socklen_t value_len)
{
int ret;
ninfo("option: %d\n", option);
if (value == NULL || value_len == 0)
{
return -EINVAL;
}
net_lock();
switch (option)
{
#ifdef CONFIG_NET_MLD
/* Handle MLD-related socket options */
case IPV6_JOIN_GROUP: /* Join a multicast group */
ret = mld_joingroup(value);
break;
case IPV6_LEAVE_GROUP: /* Quit a multicast group */
ret = mld_leavegroup(value);
break;
case IPV6_MULTICAST_HOPS: /* Multicast hop limit */
{
FAR struct socket_conn_s *conn = psock->s_conn;
conn->ttl = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
ret = OK;
}
break;
case IPV6_MULTICAST_IF: /* Interface to use for outgoing multicast
* packets */
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 net_driver_s *dev;
FAR struct udp_conn_s *conn = psock->s_conn;
int ifindex = *(FAR int *)value;
if (ifindex > 0)
{
dev = netdev_findbyindex(ifindex);
if (dev == NULL)
{
ret = -ENODEV;
break;
}
#ifdef CONFIG_NET_BINDTODEVICE
if (conn->sconn.s_boundto &&
ifindex != conn->sconn.s_boundto)
{
ret = -EINVAL;
break;
}
#endif
}
conn->mreq.imr_ifindex = ifindex;
ret = OK;
break;
}
#endif
/* The following IPv6 socket options are defined, but not implemented */
case IPV6_MULTICAST_LOOP: /* Multicast packets are delivered back to
* the local application */
#endif
case IPV6_V6ONLY: /* Restrict AF_INET6 socket to IPv6
* communications only */
nwarn("WARNING: Unimplemented IPv6 option: %d\n", option);
ret = -ENOSYS;
break;
case IPV6_UNICAST_HOPS: /* Unicast hop limit */
{
FAR struct socket_conn_s *conn = psock->s_conn;
conn->ttl = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
ret = OK;
}
break;
case IPV6_RECVPKTINFO:
case IPV6_RECVHOPLIMIT:
{
FAR struct socket_conn_s *conn = psock->s_conn;
int enable = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
if (enable)
{
_SO_SETOPT(conn->s_options, option);
}
else
{
_SO_CLROPT(conn->s_options, option);
}
ret = OK;
}
break;
case IPV6_TCLASS:
{
FAR struct socket_conn_s *conn = psock->s_conn;
int tclass = (value_len >= sizeof(int)) ?
*(FAR int *)value : (int)*(FAR unsigned char *)value;
/* According to RFC3542 6.5, the interpretation of the integer
* traffic class value is:
* x < -1: return an error of EINVAL
* x == -1: use kernel default
* 0 <= x <= 255: use x
* x >= 256: return an error of EINVAL
*/
if (tclass < -1 || tclass > 0xff)
{
nerr("ERROR: invalid tclass:%d\n", tclass);
ret = -EINVAL;
}
else
{
if (tclass == -1)
{
/* Default value is 0 */
tclass = 0;
}
conn->s_tclass = tclass;
ret = OK;
}
}
break;
default:
nerr("ERROR: Unrecognized IPv6 option: %d\n", option);
ret = -ENOPROTOOPT;
break;
}
net_unlock();
return ret;
}
#endif /* CONFIG_NET_IPv6 */