2018-10-31 22:59:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/inet/ipv6_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>
|
|
|
|
#include <errno.h>
|
2018-11-01 00:15:23 +01:00
|
|
|
#include <debug.h>
|
2018-10-31 22:59:05 +01:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
2018-11-02 19:06:57 +01:00
|
|
|
#include "mld/mld.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"
|
2018-10-31 22:59:05 +01:00
|
|
|
|
2023-04-21 11:42:57 +02:00
|
|
|
#if defined(CONFIG_NET_IPv6) && defined(CONFIG_NET_SOCKOPTS)
|
2018-10-31 22:59:05 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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)
|
|
|
|
{
|
2018-11-01 00:15:23 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ninfo("option: %d\n", option);
|
|
|
|
|
2023-05-12 09:44:27 +02:00
|
|
|
if (value == NULL || value_len == 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-11-03 23:47:08 +01:00
|
|
|
net_lock();
|
2018-10-31 22:59:05 +01:00
|
|
|
switch (option)
|
|
|
|
{
|
2022-12-21 05:40:42 +01:00
|
|
|
#ifdef CONFIG_NET_MLD
|
|
|
|
|
|
|
|
/* Handle MLD-related socket options */
|
|
|
|
|
2018-10-31 22:59:05 +01:00
|
|
|
case IPV6_JOIN_GROUP: /* Join a multicast group */
|
2018-11-02 19:06:57 +01:00
|
|
|
{
|
|
|
|
FAR const struct ipv6_mreq *mrec ;
|
|
|
|
|
|
|
|
mrec = (FAR const struct ipv6_mreq *)value;
|
2023-05-12 09:44:27 +02:00
|
|
|
ret = mld_joingroup(mrec);
|
2018-11-02 19:06:57 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-10-31 22:59:05 +01:00
|
|
|
case IPV6_LEAVE_GROUP: /* Quit a multicast group */
|
2018-11-02 19:06:57 +01:00
|
|
|
{
|
|
|
|
FAR const struct ipv6_mreq *mrec ;
|
|
|
|
|
|
|
|
mrec = (FAR const struct ipv6_mreq *)value;
|
2023-05-12 09:44:27 +02:00
|
|
|
ret = mld_leavegroup(mrec);
|
2018-11-02 19:06:57 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-05-12 09:28:24 +02:00
|
|
|
case IPV6_MULTICAST_HOPS: /* Multicast hop limit */
|
|
|
|
{
|
|
|
|
FAR struct socket_conn_s *conn;
|
|
|
|
|
|
|
|
conn = psock->s_conn;
|
2023-05-12 09:44:27 +02:00
|
|
|
conn->ttl = (value_len >= sizeof(int)) ?
|
|
|
|
*(FAR int *)value : (int)*(FAR unsigned char *)value;
|
2023-05-12 09:28:24 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-12-21 05:40:42 +01:00
|
|
|
/* The following IPv6 socket options are defined, but not implemented */
|
|
|
|
|
|
|
|
case IPV6_MULTICAST_IF: /* Interface to use for outgoing multicast
|
|
|
|
* packets */
|
|
|
|
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;
|
|
|
|
|
2023-05-12 09:28:24 +02:00
|
|
|
case IPV6_UNICAST_HOPS: /* Unicast hop limit */
|
|
|
|
{
|
|
|
|
FAR struct socket_conn_s *conn;
|
|
|
|
|
|
|
|
conn = psock->s_conn;
|
2023-05-12 09:44:27 +02:00
|
|
|
conn->ttl = (value_len >= sizeof(int)) ?
|
|
|
|
*(FAR int *)value : (int)*(FAR unsigned char *)value;
|
2023-05-12 09:28:24 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
case IPV6_RECVPKTINFO:
|
2023-04-14 12:11:12 +02:00
|
|
|
case IPV6_RECVHOPLIMIT:
|
2022-08-24 08:55:56 +02:00
|
|
|
{
|
2023-05-12 09:44:27 +02:00
|
|
|
FAR struct socket_conn_s *conn = psock->s_conn;
|
|
|
|
int enable = (value_len >= sizeof(int)) ?
|
|
|
|
*(FAR int *)value : (int)*(FAR unsigned char *)value;
|
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;
|
|
|
|
|
2022-12-21 05:40:42 +01:00
|
|
|
case IPV6_TCLASS:
|
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct socket_conn_s *conn = psock->s_conn;
|
2023-05-12 09:44:27 +02:00
|
|
|
int tclass = (value_len >= sizeof(int)) ?
|
|
|
|
*(FAR int *)value : (int)*(FAR unsigned char *)value;
|
2022-12-21 05:40:42 +01:00
|
|
|
|
|
|
|
/* 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 */
|
2018-11-02 19:06:57 +01:00
|
|
|
|
2022-12-21 05:40:42 +01:00
|
|
|
tclass = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->s_tclass = tclass;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 00:15:23 +01:00
|
|
|
break;
|
|
|
|
|
2018-10-31 22:59:05 +01:00
|
|
|
default:
|
2018-11-01 00:15:23 +01:00
|
|
|
nerr("ERROR: Unrecognized IPv6 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_IPv6 */
|