1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #include <sys/types.h>
  27 #include <sys/stream.h>
  28 #define _SUN_TPI_VERSION 2
  29 #include <sys/tihdr.h>
  30 #include <sys/socket.h>
  31 #include <sys/xti_xtiopt.h>
  32 #include <sys/xti_inet.h>
  33 
  34 #include <inet/common.h>
  35 #include <netinet/ip6.h>
  36 #include <inet/ip.h>
  37 #include <inet/udp_impl.h>
  38 /*
  39  * MK_XXX Following 2 includes temporary to import ip6_rthdr_t
  40  *        definition. May not be needed if we fix ip6_dg_snd_attrs_t
  41  *        to do all extension headers in identical manner.
  42  */
  43 #include <net/if.h>
  44 #include <inet/ip6.h>
  45 
  46 #include <netinet/in.h>
  47 #include <netinet/udp.h>
  48 #include <inet/optcom.h>
  49 
  50 /*
  51  * Table of all known options handled on a UDP protocol stack.
  52  *
  53  * Note: This table contains options processed by both UDP and IP levels
  54  *       and is the superset of options that can be performed on a UDP over IP
  55  *       stack.
  56  */
  57 opdes_t udp_opt_arr[] = {
  58 
  59 { SO_DEBUG,     SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
  60 { SO_DONTROUTE, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
  61 { SO_USELOOPBACK, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
  62         },
  63 { SO_BROADCAST, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
  64 { SO_REUSEADDR, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
  65 { SO_TYPE,      SOL_SOCKET, OA_R, OA_R, OP_NP, 0, sizeof (int), 0 },
  66 { SO_SNDBUF,    SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
  67 { SO_RCVBUF,    SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
  68 { SO_SNDTIMEO,  SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0,
  69         sizeof (struct timeval), 0 },
  70 { SO_RCVTIMEO,  SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0,
  71         sizeof (struct timeval), 0 },
  72 { SO_DGRAM_ERRIND, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
  73         0 },
  74 { SO_RECVUCRED, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
  75         },
  76 { SO_ALLZONES, SOL_SOCKET, OA_R, OA_RW, OP_CONFIG, 0, sizeof (int),
  77         0 },
  78 { SO_VRRP, SOL_SOCKET, OA_RW, OA_RW, OP_CONFIG, 0, sizeof (int), 0 },
  79 { SO_TIMESTAMP, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
  80         },
  81 { SO_ANON_MLP, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
  82     0 },
  83 { SO_MAC_EXEMPT, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
  84     0 },
  85 { SO_MAC_IMPLICIT, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int),
  86     0 },
  87 /*
  88  * The maximum size reported here depends on the maximum value for
  89  * ucredsize; unfortunately, we can't add ucredsize here so we need
  90  * to estimate here.  Before it was 512 or 384 + NGROUPS_UMAX * sizeof (gid_t);
  91  * as we're changing NGROUPS_UMAX we now codify this here using NGROUPS_UMAX.
  92  */
  93 { SCM_UCRED, SOL_SOCKET, OA_W, OA_W, OP_NP, OP_VARLEN|OP_NODEFAULT,
  94     384 + NGROUPS_UMAX * sizeof (gid_t), 0 },
  95 { SO_EXCLBIND, SOL_SOCKET, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
  96 { SO_DOMAIN,    SOL_SOCKET, OA_R, OA_R, OP_NP, 0, sizeof (int), 0 },
  97 { SO_PROTOTYPE, SOL_SOCKET, OA_R, OA_R, OP_NP, 0, sizeof (int), 0 },
  98 
  99 { IP_OPTIONS,   IPPROTO_IP, OA_RW, OA_RW, OP_NP,
 100         (OP_VARLEN|OP_NODEFAULT),
 101         IP_MAX_OPT_LENGTH + IP_ADDR_LEN, -1 /* not initialized */ },
 102 { T_IP_OPTIONS, IPPROTO_IP, OA_RW, OA_RW, OP_NP,
 103         (OP_VARLEN|OP_NODEFAULT),
 104         IP_MAX_OPT_LENGTH + IP_ADDR_LEN, -1 /* not initialized */ },
 105 
 106 { IP_TOS,       IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
 107 { T_IP_TOS,     IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
 108 { IP_TTL,       IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
 109 { IP_RECVOPTS,  IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
 110 { IP_RECVDSTADDR, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
 111         },
 112 { IP_RECVIF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
 113 { IP_RECVSLLA, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
 114 { IP_RECVTTL,   IPPROTO_IP,  OA_RW, OA_RW, OP_NP, 0, sizeof (int),
 115         0 },
 116 { IP_MULTICAST_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0,
 117         sizeof (struct in_addr),        0 /* INADDR_ANY */ },
 118 
 119 { IP_MULTICAST_LOOP, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_DEF_FN,
 120         sizeof (uchar_t), -1 /* not initialized */},
 121 
 122 { IP_MULTICAST_TTL, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_DEF_FN,
 123         sizeof (uchar_t), -1 /* not initialized */ },
 124 
 125 { IP_ADD_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
 126         sizeof (struct ip_mreq), -1 /* not initialized */ },
 127 
 128 { IP_DROP_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
 129         sizeof (struct ip_mreq), -1 /* not initialized */ },
 130 
 131 { IP_BLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
 132         sizeof (struct ip_mreq_source), -1 /* not initialized */ },
 133 
 134 { IP_UNBLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP, OP_NODEFAULT,
 135         sizeof (struct ip_mreq_source), -1 /* not initialized */ },
 136 
 137 { IP_ADD_SOURCE_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP,
 138         OP_NODEFAULT, sizeof (struct ip_mreq_source), -1 },
 139 
 140 { IP_DROP_SOURCE_MEMBERSHIP, IPPROTO_IP, OA_X, OA_X, OP_NP,
 141         OP_NODEFAULT, sizeof (struct ip_mreq_source), -1 },
 142 
 143 { IP_SEC_OPT, IPPROTO_IP, OA_RW, OA_RW, OP_NP, OP_NODEFAULT,
 144         sizeof (ipsec_req_t), -1 /* not initialized */ },
 145 
 146 { IP_BOUND_IF, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0,
 147         sizeof (int),   0 /* no ifindex */ },
 148 
 149 { IP_DHCPINIT_IF, IPPROTO_IP, OA_R, OA_RW, OP_CONFIG, 0,
 150         sizeof (int), 0 },
 151 
 152 { IP_UNSPEC_SRC, IPPROTO_IP, OA_R, OA_RW, OP_RAW, 0,
 153         sizeof (int), 0 },
 154 
 155 { IP_BROADCAST_TTL, IPPROTO_IP, OA_R, OA_RW, OP_RAW, 0, sizeof (uchar_t),
 156         0 /* disabled */ },
 157 
 158 { IP_PKTINFO, IPPROTO_IP, OA_RW, OA_RW, OP_NP,
 159         (OP_NODEFAULT|OP_VARLEN),
 160         sizeof (struct in_pktinfo), -1 /* not initialized */ },
 161 { IP_NEXTHOP, IPPROTO_IP, OA_R, OA_RW, OP_CONFIG, 0,
 162         sizeof (in_addr_t),     -1 /* not initialized  */ },
 163 
 164 { IP_DONTFRAG, IPPROTO_IP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0 },
 165 
 166 { MCAST_JOIN_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
 167         OP_NODEFAULT, sizeof (struct group_req),
 168         -1 /* not initialized */ },
 169 { MCAST_LEAVE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
 170         OP_NODEFAULT, sizeof (struct group_req),
 171         -1 /* not initialized */ },
 172 { MCAST_BLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP,
 173         OP_NODEFAULT, sizeof (struct group_source_req),
 174         -1 /* not initialized */ },
 175 { MCAST_UNBLOCK_SOURCE, IPPROTO_IP, OA_X, OA_X, OP_NP,
 176         OP_NODEFAULT, sizeof (struct group_source_req),
 177         -1 /* not initialized */ },
 178 { MCAST_JOIN_SOURCE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
 179         OP_NODEFAULT, sizeof (struct group_source_req),
 180         -1 /* not initialized */ },
 181 { MCAST_LEAVE_SOURCE_GROUP, IPPROTO_IP, OA_X, OA_X, OP_NP,
 182         OP_NODEFAULT, sizeof (struct group_source_req),
 183         -1 /* not initialized */ },
 184 
 185 { IPV6_MULTICAST_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 186         sizeof (int), 0 },
 187 
 188 { IPV6_MULTICAST_HOPS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 189         OP_DEF_FN, sizeof (int), -1 /* not initialized */ },
 190 
 191 { IPV6_MULTICAST_LOOP, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 192         OP_DEF_FN, sizeof (int), -1 /* not initialized */},
 193 
 194 { IPV6_JOIN_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP, OP_NODEFAULT,
 195         sizeof (struct ipv6_mreq), -1 /* not initialized */ },
 196 
 197 { IPV6_LEAVE_GROUP,     IPPROTO_IPV6, OA_X, OA_X, OP_NP,
 198         OP_NODEFAULT,
 199         sizeof (struct ipv6_mreq), -1 /* not initialized */ },
 200 
 201 { IPV6_UNICAST_HOPS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_DEF_FN,
 202         sizeof (int), -1 /* not initialized */ },
 203 
 204 { IPV6_BOUND_IF, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 205         sizeof (int),   0 /* no ifindex */ },
 206 
 207 { IPV6_UNSPEC_SRC, IPPROTO_IPV6, OA_R, OA_RW, OP_RAW, 0,
 208         sizeof (int), 0 },
 209 
 210 { IPV6_PKTINFO, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 211         (OP_NODEFAULT|OP_VARLEN),
 212         sizeof (struct in6_pktinfo), -1 /* not initialized */ },
 213 { IPV6_HOPLIMIT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 214         OP_NODEFAULT,
 215         sizeof (int), -1 /* not initialized */ },
 216 { IPV6_NEXTHOP, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 217         (OP_NODEFAULT|OP_VARLEN),
 218         sizeof (sin6_t), -1 /* not initialized */ },
 219 { IPV6_HOPOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 220         (OP_VARLEN|OP_NODEFAULT),
 221         MAX_EHDR_LEN, -1 /* not initialized */ },
 222 { IPV6_DSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 223         (OP_VARLEN|OP_NODEFAULT),
 224         MAX_EHDR_LEN, -1 /* not initialized */ },
 225 { IPV6_RTHDRDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 226         (OP_VARLEN|OP_NODEFAULT),
 227         MAX_EHDR_LEN, -1 /* not initialized */ },
 228 { IPV6_RTHDR, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 229         (OP_VARLEN|OP_NODEFAULT),
 230         MAX_EHDR_LEN, -1 /* not initialized */ },
 231 { IPV6_TCLASS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 232         OP_NODEFAULT,
 233         sizeof (int), -1 /* not initialized */ },
 234 { IPV6_PATHMTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 235         OP_NODEFAULT,
 236         sizeof (struct ip6_mtuinfo), -1 },
 237 { IPV6_DONTFRAG, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 238         sizeof (int), 0 },
 239 { IPV6_USE_MIN_MTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 240         sizeof (int), 0 },
 241 { IPV6_V6ONLY, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 242         sizeof (int), 0 },
 243 
 244 { IPV6_RECVPKTINFO, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 245         sizeof (int), 0 },
 246 { IPV6_RECVHOPLIMIT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 247         sizeof (int), 0 },
 248 { IPV6_RECVHOPOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 249         sizeof (int), 0 },
 250 { _OLD_IPV6_RECVDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 251         sizeof (int), 0 },
 252 { IPV6_RECVDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 253         sizeof (int), 0 },
 254 { IPV6_RECVRTHDR, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 255         sizeof (int), 0 },
 256 { IPV6_RECVRTHDRDSTOPTS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 257         sizeof (int), 0 },
 258 { IPV6_RECVPATHMTU, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP,
 259         0, sizeof (int), 0 },
 260 { IPV6_RECVTCLASS, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 261         sizeof (int), 0 },
 262 
 263 { IPV6_SEC_OPT, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, OP_NODEFAULT,
 264         sizeof (ipsec_req_t), -1 /* not initialized */ },
 265 { IPV6_SRC_PREFERENCES, IPPROTO_IPV6, OA_RW, OA_RW, OP_NP, 0,
 266         sizeof (uint32_t), IPV6_PREFER_SRC_DEFAULT },
 267 
 268 { MCAST_JOIN_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
 269         OP_NODEFAULT, sizeof (struct group_req),
 270         -1 /* not initialized */ },
 271 { MCAST_LEAVE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
 272         OP_NODEFAULT, sizeof (struct group_req),
 273         -1 /* not initialized */ },
 274 { MCAST_BLOCK_SOURCE, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
 275         OP_NODEFAULT, sizeof (struct group_source_req),
 276         -1 /* not initialized */ },
 277 { MCAST_UNBLOCK_SOURCE, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
 278         OP_NODEFAULT, sizeof (struct group_source_req),
 279         -1 /* not initialized */ },
 280 { MCAST_JOIN_SOURCE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
 281         OP_NODEFAULT, sizeof (struct group_source_req),
 282         -1 /* not initialized */ },
 283 { MCAST_LEAVE_SOURCE_GROUP, IPPROTO_IPV6, OA_X, OA_X, OP_NP,
 284         OP_NODEFAULT, sizeof (struct group_source_req),
 285         -1 /* not initialized */ },
 286 
 287 { UDP_ANONPRIVBIND, IPPROTO_UDP, OA_R, OA_RW, OP_PRIVPORT, 0,
 288         sizeof (int), 0 },
 289 { UDP_EXCLBIND, IPPROTO_UDP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
 290         },
 291 { UDP_RCVHDR, IPPROTO_UDP, OA_RW, OA_RW, OP_NP, 0, sizeof (int), 0
 292         },
 293 { UDP_NAT_T_ENDPOINT, IPPROTO_UDP, OA_RW, OA_RW, OP_PRIVPORT, 0, sizeof (int),
 294         0 },
 295 };
 296 
 297 /*
 298  * Table of all supported levels
 299  * Note: Some levels (e.g. XTI_GENERIC) may be valid but may not have
 300  * any supported options so we need this info separately.
 301  *
 302  * This is needed only for topmost tpi providers and is used only by
 303  * XTI interfaces.
 304  */
 305 optlevel_t      udp_valid_levels_arr[] = {
 306         XTI_GENERIC,
 307         SOL_SOCKET,
 308         IPPROTO_UDP,
 309         IPPROTO_IP,
 310         IPPROTO_IPV6
 311 };
 312 
 313 #define UDP_VALID_LEVELS_CNT    A_CNT(udp_valid_levels_arr)
 314 #define UDP_OPT_ARR_CNT         A_CNT(udp_opt_arr)
 315 
 316 uint_t udp_max_optsize; /* initialized when UDP driver is loaded */
 317 
 318 /*
 319  * Initialize option database object for UDP
 320  *
 321  * This object represents database of options to search passed to
 322  * {sock,tpi}optcom_req() interface routine to take care of option
 323  * management and associated methods.
 324  */
 325 
 326 optdb_obj_t udp_opt_obj = {
 327         udp_opt_default,        /* UDP default value function pointer */
 328         udp_tpi_opt_get,        /* UDP get function pointer */
 329         udp_tpi_opt_set,        /* UDP set function pointer */
 330         UDP_OPT_ARR_CNT,        /* UDP option database count of entries */
 331         udp_opt_arr,            /* UDP option database */
 332         UDP_VALID_LEVELS_CNT,   /* UDP valid level count of entries */
 333         udp_valid_levels_arr    /* UDP valid level array */
 334 };