ipv6_setsockopt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /****************************************************************************
  2. * net/inet/ipv6_setsockopt.c
  3. *
  4. * Copyright (C) 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <errno.h>
  41. #include <debug.h>
  42. #include <netinet/in.h>
  43. #include <nuttx/net/net.h>
  44. #include "mld/mld.h"
  45. #include "inet/inet.h"
  46. #ifdef CONFIG_NET_IPv6
  47. /****************************************************************************
  48. * Public Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: ipv6_setsockopt
  52. *
  53. * Description:
  54. * ipv6_setsockopt() sets the IPv6-protocol socket option specified by the
  55. * 'option' argument to the value pointed to by the 'value' argument for
  56. * the socket specified by the 'psock' argument.
  57. *
  58. * See <netinet/in.h> for the a complete list of values of IPv6 protocol
  59. * socket options.
  60. *
  61. * Input Parameters:
  62. * psock Socket structure of socket to operate on
  63. * option identifies the option to set
  64. * value Points to the argument value
  65. * value_len The length of the argument value
  66. *
  67. * Returned Value:
  68. * Returns zero (OK) on success. On failure, it returns a negated errno
  69. * value to indicate the nature of the error. See psock_setcockopt() for
  70. * the list of possible error values.
  71. *
  72. ****************************************************************************/
  73. int ipv6_setsockopt(FAR struct socket *psock, int option,
  74. FAR const void *value, socklen_t value_len)
  75. {
  76. #ifdef CONFIG_NET_MLD
  77. int ret;
  78. ninfo("option: %d\n", option);
  79. /* Handle MLD-related socket options */
  80. net_lock();
  81. switch (option)
  82. {
  83. case IPV6_JOIN_GROUP: /* Join a multicast group */
  84. {
  85. FAR const struct ipv6_mreq *mrec ;
  86. mrec = (FAR const struct ipv6_mreq *)value;
  87. if (mrec == NULL)
  88. {
  89. ret = -EINVAL;
  90. }
  91. else
  92. {
  93. ret = mld_joingroup(mrec);
  94. }
  95. }
  96. break;
  97. case IPV6_LEAVE_GROUP: /* Quit a multicast group */
  98. {
  99. FAR const struct ipv6_mreq *mrec ;
  100. mrec = (FAR const struct ipv6_mreq *)value;
  101. if (mrec == NULL)
  102. {
  103. ret = -EINVAL;
  104. }
  105. else
  106. {
  107. ret = mld_leavegroup(mrec);
  108. }
  109. }
  110. break;
  111. /* The following IPv6 socket options are defined, but not implemented */
  112. case IPV6_MULTICAST_HOPS: /* Multicast hop limit */
  113. case IPV6_MULTICAST_IF: /* Interface to use for outgoing multicast
  114. * packets */
  115. case IPV6_MULTICAST_LOOP: /* Multicast packets are delivered back to
  116. * the local application */
  117. case IPV6_UNICAST_HOPS: /* Unicast hop limit */
  118. case IPV6_V6ONLY: /* Restrict AF_INET6 socket to IPv6
  119. * communications only */
  120. nwarn("WARNING: Unimplemented IPv6 option: %d\n", option);
  121. ret = -ENOSYS;
  122. break;
  123. default:
  124. nerr("ERROR: Unrecognized IPv6 option: %d\n", option);
  125. ret = -ENOPROTOOPT;
  126. break;
  127. }
  128. net_unlock();
  129. return ret;
  130. #else
  131. return -ENOPROTOOPT;
  132. #endif
  133. }
  134. #endif /* CONFIG_NET_IPv6 */