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 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* Copyright (c) 1990 Mentat Inc. */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 const char udp_version[] = "%Z%%M% %I% %E% SMI"; 30 31 #include <sys/types.h> 32 #include <sys/stream.h> 33 #include <sys/dlpi.h> 34 #include <sys/pattr.h> 35 #include <sys/stropts.h> 36 #include <sys/strlog.h> 37 #include <sys/strsun.h> 38 #include <sys/time.h> 39 #define _SUN_TPI_VERSION 2 40 #include <sys/tihdr.h> 41 #include <sys/timod.h> 42 #include <sys/ddi.h> 43 #include <sys/sunddi.h> 44 #include <sys/strsubr.h> 45 #include <sys/suntpi.h> 46 #include <sys/xti_inet.h> 47 #include <sys/cmn_err.h> 48 #include <sys/kmem.h> 49 #include <sys/policy.h> 50 #include <sys/ucred.h> 51 #include <sys/zone.h> 52 53 #include <sys/socket.h> 54 #include <sys/sockio.h> 55 #include <sys/vtrace.h> 56 #include <sys/debug.h> 57 #include <sys/isa_defs.h> 58 #include <sys/random.h> 59 #include <netinet/in.h> 60 #include <netinet/ip6.h> 61 #include <netinet/icmp6.h> 62 #include <netinet/udp.h> 63 #include <net/if.h> 64 #include <net/route.h> 65 66 #include <inet/common.h> 67 #include <inet/ip.h> 68 #include <inet/ip_impl.h> 69 #include <inet/ip6.h> 70 #include <inet/ip_ire.h> 71 #include <inet/ip_if.h> 72 #include <inet/ip_multi.h> 73 #include <inet/mi.h> 74 #include <inet/mib2.h> 75 #include <inet/nd.h> 76 #include <inet/optcom.h> 77 #include <inet/snmpcom.h> 78 #include <inet/kstatcom.h> 79 #include <inet/udp_impl.h> 80 #include <inet/ipclassifier.h> 81 #include <inet/ipsec_impl.h> 82 #include <inet/ipp_common.h> 83 84 /* 85 * The ipsec_info.h header file is here since it has the definition for the 86 * M_CTL message types used by IP to convey information to the ULP. The 87 * ipsec_info.h needs the pfkeyv2.h, hence the latter's presence. 88 */ 89 #include <net/pfkeyv2.h> 90 #include <inet/ipsec_info.h> 91 92 #include <sys/tsol/label.h> 93 #include <sys/tsol/tnet.h> 94 #include <rpc/pmap_prot.h> 95 96 /* 97 * Synchronization notes: 98 * 99 * UDP uses a combination of its internal perimeter, a global lock and 100 * a set of bind hash locks to protect its data structures. Please see 101 * the note above udp_mode_assertions for details about the internal 102 * perimeter. 103 * 104 * When a UDP endpoint is bound to a local port, it is inserted into 105 * a bind hash list. The list consists of an array of udp_fanout_t buckets. 106 * The size of the array is controlled by the udp_bind_fanout_size variable. 107 * This variable can be changed in /etc/system if the default value is 108 * not large enough. Each bind hash bucket is protected by a per bucket 109 * lock. It protects the udp_bind_hash and udp_ptpbhn fields in the udp_t 110 * structure. An UDP endpoint is removed from the bind hash list only 111 * when it is being unbound or being closed. The per bucket lock also 112 * protects a UDP endpoint's state changes. 113 * 114 * Plumbing notes: 115 * 116 * Both udp and ip are merged, but the streams plumbing is kept unchanged 117 * in that udp is always pushed atop /dev/ip. This is done to preserve 118 * backwards compatibility for certain applications which rely on such 119 * plumbing geometry to do things such as issuing I_POP on the stream 120 * in order to obtain direct access to /dev/ip, etc. 121 * 122 * All UDP processings happen in the /dev/ip instance; the udp module 123 * instance does not possess any state about the endpoint, and merely 124 * acts as a dummy module whose presence is to keep the streams plumbing 125 * appearance unchanged. At open time /dev/ip allocates a conn_t that 126 * happens to embed a udp_t. This stays dormant until the time udp is 127 * pushed, which indicates to /dev/ip that it must convert itself from 128 * an IP to a UDP endpoint. 129 * 130 * We only allow for the following plumbing cases: 131 * 132 * Normal: 133 * /dev/ip is first opened and later udp is pushed directly on top. 134 * This is the default action that happens when a udp socket or 135 * /dev/udp is opened. The conn_t created by /dev/ip instance is 136 * now shared and is marked with IPCL_UDP. 137 * 138 * SNMP-only: 139 * udp is pushed on top of a module other than /dev/ip. When this 140 * happens it will support only SNMP semantics. A new conn_t is 141 * allocated and marked with IPCL_UDPMOD. 142 * 143 * The above cases imply that we don't support any intermediate module to 144 * reside in between /dev/ip and udp -- in fact, we never supported such 145 * scenario in the past as the inter-layer communication semantics have 146 * always been private. Also note that the normal case allows for SNMP 147 * requests to be processed in addition to the rest of UDP operations. 148 * 149 * The normal case plumbing is depicted by the following diagram: 150 * 151 * +---------------+---------------+ 152 * | | | udp 153 * | udp_wq | udp_rq | 154 * | | UDP_RD | 155 * | | | 156 * +---------------+---------------+ 157 * | ^ 158 * v | 159 * +---------------+---------------+ 160 * | | | /dev/ip 161 * | ip_wq | ip_rq | conn_t 162 * | UDP_WR | | 163 * | | | 164 * +---------------+---------------+ 165 * 166 * Messages arriving at udp_wq from above will end up in ip_wq before 167 * it gets processed, i.e. udp write entry points will advance udp_wq 168 * and use its q_next value as ip_wq in order to use the conn_t that 169 * is stored in its q_ptr. Likewise, messages generated by ip to the 170 * module above udp will appear as if they are originated from udp_rq, 171 * i.e. putnext() calls to the module above udp is done using the 172 * udp_rq instead of ip_rq in order to avoid udp_rput() which does 173 * nothing more than calling putnext(). 174 * 175 * The above implies the following rule of thumb: 176 * 177 * 1. udp_t is obtained from conn_t, which is created by the /dev/ip 178 * instance and is stored in q_ptr of both ip_wq and ip_rq. There 179 * is no direct reference to conn_t from either udp_wq or udp_rq. 180 * 181 * 2. Write-side entry points of udp can obtain the conn_t via the 182 * Q_TO_CONN() macro, using the queue value obtain from UDP_WR(). 183 * 184 * 3. While in /dev/ip context, putnext() to the module above udp can 185 * be done by supplying the queue value obtained from UDP_RD(). 186 * 187 */ 188 189 static queue_t *UDP_WR(queue_t *); 190 static queue_t *UDP_RD(queue_t *); 191 192 udp_stat_t udp_statistics = { 193 { "udp_ip_send", KSTAT_DATA_UINT64 }, 194 { "udp_ip_ire_send", KSTAT_DATA_UINT64 }, 195 { "udp_ire_null", KSTAT_DATA_UINT64 }, 196 { "udp_drain", KSTAT_DATA_UINT64 }, 197 { "udp_sock_fallback", KSTAT_DATA_UINT64 }, 198 { "udp_rrw_busy", KSTAT_DATA_UINT64 }, 199 { "udp_rrw_msgcnt", KSTAT_DATA_UINT64 }, 200 { "udp_out_sw_cksum", KSTAT_DATA_UINT64 }, 201 { "udp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 202 { "udp_out_opt", KSTAT_DATA_UINT64 }, 203 { "udp_out_err_notconn", KSTAT_DATA_UINT64 }, 204 { "udp_out_err_output", KSTAT_DATA_UINT64 }, 205 { "udp_out_err_tudr", KSTAT_DATA_UINT64 }, 206 { "udp_in_pktinfo", KSTAT_DATA_UINT64 }, 207 { "udp_in_recvdstaddr", KSTAT_DATA_UINT64 }, 208 { "udp_in_recvopts", KSTAT_DATA_UINT64 }, 209 { "udp_in_recvif", KSTAT_DATA_UINT64 }, 210 { "udp_in_recvslla", KSTAT_DATA_UINT64 }, 211 { "udp_in_recvucred", KSTAT_DATA_UINT64 }, 212 { "udp_in_recvttl", KSTAT_DATA_UINT64 }, 213 { "udp_in_recvhopopts", KSTAT_DATA_UINT64 }, 214 { "udp_in_recvhoplimit", KSTAT_DATA_UINT64 }, 215 { "udp_in_recvdstopts", KSTAT_DATA_UINT64 }, 216 { "udp_in_recvrtdstopts", KSTAT_DATA_UINT64 }, 217 { "udp_in_recvrthdr", KSTAT_DATA_UINT64 }, 218 { "udp_in_recvpktinfo", KSTAT_DATA_UINT64 }, 219 { "udp_in_recvtclass", KSTAT_DATA_UINT64 }, 220 { "udp_in_timestamp", KSTAT_DATA_UINT64 }, 221 #ifdef DEBUG 222 { "udp_data_conn", KSTAT_DATA_UINT64 }, 223 { "udp_data_notconn", KSTAT_DATA_UINT64 }, 224 #endif 225 }; 226 227 static kstat_t *udp_ksp; 228 struct kmem_cache *udp_cache; 229 230 /* 231 * Bind hash list size and hash function. It has to be a power of 2 for 232 * hashing. 233 */ 234 #define UDP_BIND_FANOUT_SIZE 512 235 #define UDP_BIND_HASH(lport) \ 236 ((ntohs((uint16_t)lport)) & (udp_bind_fanout_size - 1)) 237 238 /* UDP bind fanout hash structure. */ 239 typedef struct udp_fanout_s { 240 udp_t *uf_udp; 241 kmutex_t uf_lock; 242 #if defined(_LP64) || defined(_I32LPx) 243 char uf_pad[48]; 244 #else 245 char uf_pad[56]; 246 #endif 247 } udp_fanout_t; 248 249 uint_t udp_bind_fanout_size = UDP_BIND_FANOUT_SIZE; 250 /* udp_fanout_t *udp_bind_fanout. */ 251 static udp_fanout_t *udp_bind_fanout; 252 253 /* 254 * This controls the rate some ndd info report functions can be used 255 * by non-privileged users. It stores the last time such info is 256 * requested. When those report functions are called again, this 257 * is checked with the current time and compare with the ndd param 258 * udp_ndd_get_info_interval. 259 */ 260 static clock_t udp_last_ndd_get_info_time; 261 #define NDD_TOO_QUICK_MSG \ 262 "ndd get info rate too high for non-privileged users, try again " \ 263 "later.\n" 264 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 265 266 /* Option processing attrs */ 267 typedef struct udpattrs_s { 268 ip6_pkt_t *udpattr_ipp; 269 mblk_t *udpattr_mb; 270 boolean_t udpattr_credset; 271 } udpattrs_t; 272 273 static void udp_addr_req(queue_t *q, mblk_t *mp); 274 static void udp_bind(queue_t *q, mblk_t *mp); 275 static void udp_bind_hash_insert(udp_fanout_t *uf, udp_t *udp); 276 static void udp_bind_hash_remove(udp_t *udp, boolean_t caller_holds_lock); 277 static int udp_build_hdrs(queue_t *q, udp_t *udp); 278 static void udp_capability_req(queue_t *q, mblk_t *mp); 279 static int udp_close(queue_t *q); 280 static void udp_connect(queue_t *q, mblk_t *mp); 281 static void udp_disconnect(queue_t *q, mblk_t *mp); 282 static void udp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, 283 int sys_error); 284 static void udp_err_ack_prim(queue_t *q, mblk_t *mp, int primitive, 285 t_scalar_t tlierr, int unixerr); 286 static int udp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 287 cred_t *cr); 288 static int udp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 289 char *value, caddr_t cp, cred_t *cr); 290 static int udp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 291 char *value, caddr_t cp, cred_t *cr); 292 static void udp_icmp_error(queue_t *q, mblk_t *mp); 293 static void udp_icmp_error_ipv6(queue_t *q, mblk_t *mp); 294 static void udp_info_req(queue_t *q, mblk_t *mp); 295 static mblk_t *udp_ip_bind_mp(udp_t *udp, t_scalar_t bind_prim, 296 t_scalar_t addr_length); 297 static int udp_open(queue_t *q, dev_t *devp, int flag, int sflag, 298 cred_t *credp); 299 static int udp_unitdata_opt_process(queue_t *q, mblk_t *mp, 300 int *errorp, udpattrs_t *udpattrs); 301 static boolean_t udp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name); 302 static int udp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 303 static boolean_t udp_param_register(udpparam_t *udppa, int cnt); 304 static int udp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 305 cred_t *cr); 306 static void udp_report_item(mblk_t *mp, udp_t *udp); 307 static void udp_rput(queue_t *q, mblk_t *mp); 308 static void udp_rput_other(queue_t *, mblk_t *); 309 static int udp_rinfop(queue_t *q, infod_t *dp); 310 static int udp_rrw(queue_t *q, struiod_t *dp); 311 static void udp_rput_bind_ack(queue_t *q, mblk_t *mp); 312 static int udp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 313 cred_t *cr); 314 static void udp_send_data(udp_t *udp, queue_t *q, mblk_t *mp, ipha_t *ipha); 315 static void udp_ud_err(queue_t *q, mblk_t *mp, uchar_t *destaddr, 316 t_scalar_t destlen, t_scalar_t err); 317 static void udp_unbind(queue_t *q, mblk_t *mp); 318 static in_port_t udp_update_next_port(udp_t *udp, in_port_t port, 319 boolean_t random); 320 static void udp_wput(queue_t *q, mblk_t *mp); 321 static mblk_t *udp_output_v4(conn_t *, mblk_t *mp, ipaddr_t v4dst, 322 uint16_t port, uint_t srcid, int *error); 323 static mblk_t *udp_output_v6(conn_t *connp, mblk_t *mp, sin6_t *sin6, 324 int *error); 325 static void udp_wput_other(queue_t *q, mblk_t *mp); 326 static void udp_wput_iocdata(queue_t *q, mblk_t *mp); 327 static void udp_output(conn_t *connp, mblk_t *mp, struct sockaddr *addr, 328 socklen_t addrlen); 329 static size_t udp_set_rcv_hiwat(udp_t *udp, size_t size); 330 331 static void udp_kstat_init(void); 332 static void udp_kstat_fini(void); 333 static int udp_kstat_update(kstat_t *kp, int rw); 334 static void udp_input_wrapper(void *arg, mblk_t *mp, void *arg2); 335 static void udp_rput_other_wrapper(void *arg, mblk_t *mp, void *arg2); 336 static void udp_wput_other_wrapper(void *arg, mblk_t *mp, void *arg2); 337 static void udp_resume_bind_cb(void *arg, mblk_t *mp, void *arg2); 338 339 static void udp_rcv_enqueue(queue_t *q, udp_t *udp, mblk_t *mp, 340 uint_t pkt_len); 341 static void udp_rcv_drain(queue_t *q, udp_t *udp, boolean_t closing); 342 static void udp_enter(conn_t *, mblk_t *, sqproc_t, uint8_t); 343 static void udp_exit(conn_t *); 344 static void udp_become_writer(conn_t *, mblk_t *, sqproc_t, uint8_t); 345 #ifdef DEBUG 346 static void udp_mode_assertions(udp_t *, int); 347 #endif /* DEBUG */ 348 349 major_t UDP6_MAJ; 350 #define UDP6 "udp6" 351 352 #define UDP_RECV_HIWATER (56 * 1024) 353 #define UDP_RECV_LOWATER 128 354 #define UDP_XMIT_HIWATER (56 * 1024) 355 #define UDP_XMIT_LOWATER 1024 356 357 static struct module_info udp_info = { 358 UDP_MOD_ID, UDP_MOD_NAME, 1, INFPSZ, UDP_RECV_HIWATER, UDP_RECV_LOWATER 359 }; 360 361 static struct qinit udp_rinit = { 362 (pfi_t)udp_rput, NULL, udp_open, udp_close, NULL, 363 &udp_info, NULL, udp_rrw, udp_rinfop, STRUIOT_STANDARD 364 }; 365 366 static struct qinit udp_winit = { 367 (pfi_t)udp_wput, NULL, NULL, NULL, NULL, 368 &udp_info, NULL, NULL, NULL, STRUIOT_NONE 369 }; 370 371 static struct qinit winit = { 372 (pfi_t)putnext, NULL, NULL, NULL, NULL, 373 &udp_info, NULL, NULL, NULL, STRUIOT_NONE 374 }; 375 376 /* Support for just SNMP if UDP is not pushed directly over device IP */ 377 struct qinit udp_snmp_rinit = { 378 (pfi_t)putnext, NULL, udp_open, ip_snmpmod_close, NULL, 379 &udp_info, NULL, NULL, NULL, STRUIOT_NONE 380 }; 381 382 struct qinit udp_snmp_winit = { 383 (pfi_t)ip_snmpmod_wput, NULL, udp_open, ip_snmpmod_close, NULL, 384 &udp_info, NULL, NULL, NULL, STRUIOT_NONE 385 }; 386 387 struct streamtab udpinfo = { 388 &udp_rinit, &winit 389 }; 390 391 static sin_t sin_null; /* Zero address for quick clears */ 392 static sin6_t sin6_null; /* Zero address for quick clears */ 393 394 /* Hint not protected by any lock */ 395 static in_port_t udp_g_next_port_to_try; 396 397 /* 398 * Extra privileged ports. In host byte order. 399 */ 400 #define UDP_NUM_EPRIV_PORTS 64 401 static int udp_g_num_epriv_ports = UDP_NUM_EPRIV_PORTS; 402 static in_port_t udp_g_epriv_ports[UDP_NUM_EPRIV_PORTS] = { 2049, 4045 }; 403 404 /* Only modified during _init and _fini thus no locking is needed. */ 405 static IDP udp_g_nd; /* Points to table of UDP ND variables. */ 406 407 /* MIB-2 stuff for SNMP */ 408 static mib2_udp_t udp_mib; /* SNMP fixed size info */ 409 static kstat_t *udp_mibkp; /* kstat exporting udp_mib data */ 410 411 #define UDP_MAXPACKET_IPV4 (IP_MAXPACKET - UDPH_SIZE - IP_SIMPLE_HDR_LENGTH) 412 413 /* Default structure copied into T_INFO_ACK messages */ 414 static struct T_info_ack udp_g_t_info_ack_ipv4 = { 415 T_INFO_ACK, 416 UDP_MAXPACKET_IPV4, /* TSDU_size. Excl. headers */ 417 T_INVALID, /* ETSU_size. udp does not support expedited data. */ 418 T_INVALID, /* CDATA_size. udp does not support connect data. */ 419 T_INVALID, /* DDATA_size. udp does not support disconnect data. */ 420 sizeof (sin_t), /* ADDR_size. */ 421 0, /* OPT_size - not initialized here */ 422 UDP_MAXPACKET_IPV4, /* TIDU_size. Excl. headers */ 423 T_CLTS, /* SERV_type. udp supports connection-less. */ 424 TS_UNBND, /* CURRENT_state. This is set from udp_state. */ 425 (XPG4_1|SENDZERO) /* PROVIDER_flag */ 426 }; 427 428 #define UDP_MAXPACKET_IPV6 (IP_MAXPACKET - UDPH_SIZE - IPV6_HDR_LEN) 429 430 static struct T_info_ack udp_g_t_info_ack_ipv6 = { 431 T_INFO_ACK, 432 UDP_MAXPACKET_IPV6, /* TSDU_size. Excl. headers */ 433 T_INVALID, /* ETSU_size. udp does not support expedited data. */ 434 T_INVALID, /* CDATA_size. udp does not support connect data. */ 435 T_INVALID, /* DDATA_size. udp does not support disconnect data. */ 436 sizeof (sin6_t), /* ADDR_size. */ 437 0, /* OPT_size - not initialized here */ 438 UDP_MAXPACKET_IPV6, /* TIDU_size. Excl. headers */ 439 T_CLTS, /* SERV_type. udp supports connection-less. */ 440 TS_UNBND, /* CURRENT_state. This is set from udp_state. */ 441 (XPG4_1|SENDZERO) /* PROVIDER_flag */ 442 }; 443 444 /* largest UDP port number */ 445 #define UDP_MAX_PORT 65535 446 447 /* 448 * Table of ND variables supported by udp. These are loaded into udp_g_nd 449 * in udp_open. 450 * All of these are alterable, within the min/max values given, at run time. 451 */ 452 /* BEGIN CSTYLED */ 453 udpparam_t udp_param_arr[] = { 454 /*min max value name */ 455 { 0L, 256, 32, "udp_wroff_extra" }, 456 { 1L, 255, 255, "udp_ipv4_ttl" }, 457 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "udp_ipv6_hoplimit"}, 458 { 1024, (32 * 1024), 1024, "udp_smallest_nonpriv_port" }, 459 { 0, 1, 1, "udp_do_checksum" }, 460 { 1024, UDP_MAX_PORT, (32 * 1024), "udp_smallest_anon_port" }, 461 { 1024, UDP_MAX_PORT, UDP_MAX_PORT, "udp_largest_anon_port" }, 462 { UDP_XMIT_LOWATER, (1<<30), UDP_XMIT_HIWATER, "udp_xmit_hiwat"}, 463 { 0, (1<<30), UDP_XMIT_LOWATER, "udp_xmit_lowat"}, 464 { UDP_RECV_LOWATER, (1<<30), UDP_RECV_HIWATER, "udp_recv_hiwat"}, 465 { 65536, (1<<30), 2*1024*1024, "udp_max_buf"}, 466 { 100, 60000, 1000, "udp_ndd_get_info_interval"}, 467 }; 468 /* END CSTYLED */ 469 470 /* 471 * The smallest anonymous port in the privileged port range which UDP 472 * looks for free port. Use in the option UDP_ANONPRIVBIND. 473 */ 474 static in_port_t udp_min_anonpriv_port = 512; 475 476 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 477 uint32_t udp_random_anon_port = 1; 478 479 /* 480 * Hook functions to enable cluster networking. 481 * On non-clustered systems these vectors must always be NULL 482 */ 483 484 void (*cl_inet_bind)(uchar_t protocol, sa_family_t addr_family, 485 uint8_t *laddrp, in_port_t lport) = NULL; 486 void (*cl_inet_unbind)(uint8_t protocol, sa_family_t addr_family, 487 uint8_t *laddrp, in_port_t lport) = NULL; 488 489 typedef union T_primitives *t_primp_t; 490 491 #define UDP_ENQUEUE_MP(udp, mp, proc, tag) { \ 492 ASSERT((mp)->b_prev == NULL && (mp)->b_queue == NULL); \ 493 ASSERT(MUTEX_HELD(&(udp)->udp_connp->conn_lock)); \ 494 (mp)->b_queue = (queue_t *)((uintptr_t)tag); \ 495 (mp)->b_prev = (mblk_t *)proc; \ 496 if ((udp)->udp_mphead == NULL) \ 497 (udp)->udp_mphead = (mp); \ 498 else \ 499 (udp)->udp_mptail->b_next = (mp); \ 500 (udp)->udp_mptail = (mp); \ 501 (udp)->udp_mpcount++; \ 502 } 503 504 #define UDP_READERS_INCREF(udp) { \ 505 ASSERT(MUTEX_HELD(&(udp)->udp_connp->conn_lock)); \ 506 (udp)->udp_reader_count++; \ 507 } 508 509 #define UDP_READERS_DECREF(udp) { \ 510 ASSERT(MUTEX_HELD(&(udp)->udp_connp->conn_lock)); \ 511 (udp)->udp_reader_count--; \ 512 if ((udp)->udp_reader_count == 0) \ 513 cv_broadcast(&(udp)->udp_connp->conn_cv); \ 514 } 515 516 #define UDP_SQUEUE_DECREF(udp) { \ 517 ASSERT(MUTEX_HELD(&(udp)->udp_connp->conn_lock)); \ 518 (udp)->udp_squeue_count--; \ 519 if ((udp)->udp_squeue_count == 0) \ 520 cv_broadcast(&(udp)->udp_connp->conn_cv); \ 521 } 522 523 /* 524 * Notes on UDP endpoint synchronization: 525 * 526 * UDP needs exclusive operation on a per endpoint basis, when executing 527 * functions that modify the endpoint state. udp_rput_other() deals with 528 * packets with IP options, and processing these packets end up having 529 * to update the endpoint's option related state. udp_wput_other() deals 530 * with control operations from the top, e.g. connect() that needs to 531 * update the endpoint state. These could be synchronized using locks, 532 * but the current version uses squeues for this purpose. squeues may 533 * give performance improvement for certain cases such as connected UDP 534 * sockets; thus the framework allows for using squeues. 535 * 536 * The perimeter routines are described as follows: 537 * 538 * udp_enter(): 539 * Enter the UDP endpoint perimeter. 540 * 541 * udp_become_writer(): 542 * Become exclusive on the UDP endpoint. Specifies a function 543 * that will be called exclusively either immediately or later 544 * when the perimeter is available exclusively. 545 * 546 * udp_exit(): 547 * Exit the UDP perimeter. 548 * 549 * Entering UDP from the top or from the bottom must be done using 550 * udp_enter(). No lock must be held while attempting to enter the UDP 551 * perimeter. When finished, udp_exit() must be called to get out of 552 * the perimeter. 553 * 554 * UDP operates in either MT_HOT mode or in SQUEUE mode. In MT_HOT mode, 555 * multiple threads may enter a UDP endpoint concurrently. This is used 556 * for sending and/or receiving normal data. Control operations and other 557 * special cases call udp_become_writer() to become exclusive on a per 558 * endpoint basis and this results in transitioning to SQUEUE mode. squeue 559 * by definition serializes access to the conn_t. When there are no more 560 * pending messages on the squeue for the UDP connection, the endpoint 561 * reverts to MT_HOT mode. During the interregnum when not all MT threads 562 * of an endpoint have finished, messages are queued in the UDP endpoint 563 * and the UDP is in UDP_MT_QUEUED mode or UDP_QUEUED_SQUEUE mode. 564 * 565 * These modes have the following analogs: 566 * 567 * UDP_MT_HOT/udp_reader_count==0 none 568 * UDP_MT_HOT/udp_reader_count>0 RW_READ_LOCK 569 * UDP_MT_QUEUED RW_WRITE_WANTED 570 * UDP_SQUEUE or UDP_QUEUED_SQUEUE RW_WRITE_LOCKED 571 * 572 * Stable modes: UDP_MT_HOT, UDP_SQUEUE 573 * Transient modes: UDP_MT_QUEUED, UDP_QUEUED_SQUEUE 574 * 575 * While in stable modes, UDP keeps track of the number of threads 576 * operating on the endpoint. The udp_reader_count variable represents 577 * the number of threads entering the endpoint as readers while it is 578 * in UDP_MT_HOT mode. Transitioning to UDP_SQUEUE happens when there 579 * is only a single reader, i.e. when this counter drops to 1. Likewise, 580 * udp_squeue_count represents the number of threads operating on the 581 * endpoint's squeue while it is in UDP_SQUEUE mode. The mode transition 582 * to UDP_MT_HOT happens after the last thread exits the endpoint, i.e. 583 * when this counter drops to 0. 584 * 585 * The default mode is set to UDP_MT_HOT and UDP alternates between 586 * UDP_MT_HOT and UDP_SQUEUE as shown in the state transition below. 587 * 588 * Mode transition: 589 * ---------------------------------------------------------------- 590 * old mode Event New mode 591 * ---------------------------------------------------------------- 592 * UDP_MT_HOT Call to udp_become_writer() UDP_SQUEUE 593 * and udp_reader_count == 1 594 * 595 * UDP_MT_HOT Call to udp_become_writer() UDP_MT_QUEUED 596 * and udp_reader_count > 1 597 * 598 * UDP_MT_QUEUED udp_reader_count drops to zero UDP_QUEUED_SQUEUE 599 * 600 * UDP_QUEUED_SQUEUE All messages enqueued on the UDP_SQUEUE 601 * internal UDP queue successfully 602 * moved to squeue AND udp_squeue_count != 0 603 * 604 * UDP_QUEUED_SQUEUE All messages enqueued on the UDP_MT_HOT 605 * internal UDP queue successfully 606 * moved to squeue AND udp_squeue_count 607 * drops to zero 608 * 609 * UDP_SQUEUE udp_squeue_count drops to zero UDP_MT_HOT 610 * ---------------------------------------------------------------- 611 */ 612 613 static queue_t * 614 UDP_WR(queue_t *q) 615 { 616 ASSERT(q->q_ptr == NULL && _OTHERQ(q)->q_ptr == NULL); 617 ASSERT(WR(q)->q_next != NULL && WR(q)->q_next->q_ptr != NULL); 618 ASSERT(IPCL_IS_UDP(Q_TO_CONN(WR(q)->q_next))); 619 620 return (_WR(q)->q_next); 621 } 622 623 static queue_t * 624 UDP_RD(queue_t *q) 625 { 626 ASSERT(q->q_ptr != NULL && _OTHERQ(q)->q_ptr != NULL); 627 ASSERT(IPCL_IS_UDP(Q_TO_CONN(q))); 628 ASSERT(RD(q)->q_next != NULL && RD(q)->q_next->q_ptr == NULL); 629 630 return (_RD(q)->q_next); 631 } 632 633 #ifdef DEBUG 634 #define UDP_MODE_ASSERTIONS(udp, caller) udp_mode_assertions(udp, caller) 635 #else 636 #define UDP_MODE_ASSERTIONS(udp, caller) 637 #endif 638 639 /* Invariants */ 640 #ifdef DEBUG 641 642 uint32_t udp_count[4]; 643 644 /* Context of udp_mode_assertions */ 645 #define UDP_ENTER 1 646 #define UDP_BECOME_WRITER 2 647 #define UDP_EXIT 3 648 649 static void 650 udp_mode_assertions(udp_t *udp, int caller) 651 { 652 ASSERT(MUTEX_HELD(&udp->udp_connp->conn_lock)); 653 654 switch (udp->udp_mode) { 655 case UDP_MT_HOT: 656 /* 657 * Messages have not yet been enqueued on the internal queue, 658 * otherwise we would have switched to UDP_MT_QUEUED. Likewise 659 * by definition, there can't be any messages enqueued on the 660 * squeue. The UDP could be quiescent, so udp_reader_count 661 * could be zero at entry. 662 */ 663 ASSERT(udp->udp_mphead == NULL && udp->udp_mpcount == 0 && 664 udp->udp_squeue_count == 0); 665 ASSERT(caller == UDP_ENTER || udp->udp_reader_count != 0); 666 udp_count[0]++; 667 break; 668 669 case UDP_MT_QUEUED: 670 /* 671 * The last MT thread to exit the udp perimeter empties the 672 * internal queue and then switches the UDP to 673 * UDP_QUEUED_SQUEUE mode. Since we are still in UDP_MT_QUEUED 674 * mode, it means there must be at least 1 MT thread still in 675 * the perimeter and at least 1 message on the internal queue. 676 */ 677 ASSERT(udp->udp_reader_count >= 1 && udp->udp_mphead != NULL && 678 udp->udp_mpcount != 0 && udp->udp_squeue_count == 0); 679 udp_count[1]++; 680 break; 681 682 case UDP_QUEUED_SQUEUE: 683 /* 684 * The switch has happened from MT to SQUEUE. So there can't 685 * any MT threads. Messages could still pile up on the internal 686 * queue until the transition is complete and we move to 687 * UDP_SQUEUE mode. We can't assert on nonzero udp_squeue_count 688 * since the squeue could drain any time. 689 */ 690 ASSERT(udp->udp_reader_count == 0); 691 udp_count[2]++; 692 break; 693 694 case UDP_SQUEUE: 695 /* 696 * The transition is complete. Thre can't be any messages on 697 * the internal queue. The udp could be quiescent or the squeue 698 * could drain any time, so we can't assert on nonzero 699 * udp_squeue_count during entry. Nor can we assert that 700 * udp_reader_count is zero, since, a reader thread could have 701 * directly become writer in line by calling udp_become_writer 702 * without going through the queued states. 703 */ 704 ASSERT(udp->udp_mphead == NULL && udp->udp_mpcount == 0); 705 ASSERT(caller == UDP_ENTER || udp->udp_squeue_count != 0); 706 udp_count[3]++; 707 break; 708 } 709 } 710 #endif 711 712 #define _UDP_ENTER(connp, mp, proc, tag) { \ 713 udp_t *_udp = (connp)->conn_udp; \ 714 \ 715 mutex_enter(&(connp)->conn_lock); \ 716 if ((connp)->conn_state_flags & CONN_CLOSING) { \ 717 mutex_exit(&(connp)->conn_lock); \ 718 freemsg(mp); \ 719 } else { \ 720 UDP_MODE_ASSERTIONS(_udp, UDP_ENTER); \ 721 \ 722 switch (_udp->udp_mode) { \ 723 case UDP_MT_HOT: \ 724 /* We can execute as reader right away. */ \ 725 UDP_READERS_INCREF(_udp); \ 726 mutex_exit(&(connp)->conn_lock); \ 727 (*(proc))(connp, mp, (connp)->conn_sqp); \ 728 break; \ 729 \ 730 case UDP_SQUEUE: \ 731 /* \ 732 * We are in squeue mode, send the \ 733 * packet to the squeue \ 734 */ \ 735 _udp->udp_squeue_count++; \ 736 CONN_INC_REF_LOCKED(connp); \ 737 mutex_exit(&(connp)->conn_lock); \ 738 squeue_enter((connp)->conn_sqp, mp, proc, \ 739 connp, tag); \ 740 break; \ 741 \ 742 case UDP_MT_QUEUED: \ 743 case UDP_QUEUED_SQUEUE: \ 744 /* \ 745 * Some messages may have been enqueued \ 746 * ahead of us. Enqueue the new message \ 747 * at the tail of the internal queue to \ 748 * preserve message ordering. \ 749 */ \ 750 UDP_ENQUEUE_MP(_udp, mp, proc, tag); \ 751 mutex_exit(&(connp)->conn_lock); \ 752 break; \ 753 } \ 754 } \ 755 } 756 757 static void 758 udp_enter(conn_t *connp, mblk_t *mp, sqproc_t proc, uint8_t tag) 759 { 760 _UDP_ENTER(connp, mp, proc, tag); 761 } 762 763 static void 764 udp_become_writer(conn_t *connp, mblk_t *mp, sqproc_t proc, uint8_t tag) 765 { 766 udp_t *udp; 767 768 udp = connp->conn_udp; 769 770 mutex_enter(&connp->conn_lock); 771 772 UDP_MODE_ASSERTIONS(udp, UDP_BECOME_WRITER); 773 774 switch (udp->udp_mode) { 775 case UDP_MT_HOT: 776 if (udp->udp_reader_count == 1) { 777 /* 778 * We are the only MT thread. Switch to squeue mode 779 * immediately. 780 */ 781 udp->udp_mode = UDP_SQUEUE; 782 udp->udp_squeue_count = 1; 783 CONN_INC_REF_LOCKED(connp); 784 mutex_exit(&connp->conn_lock); 785 squeue_enter(connp->conn_sqp, mp, proc, connp, tag); 786 return; 787 } 788 /* FALLTHRU */ 789 790 case UDP_MT_QUEUED: 791 /* Enqueue the packet internally in UDP */ 792 udp->udp_mode = UDP_MT_QUEUED; 793 UDP_ENQUEUE_MP(udp, mp, proc, tag); 794 mutex_exit(&connp->conn_lock); 795 return; 796 797 case UDP_SQUEUE: 798 case UDP_QUEUED_SQUEUE: 799 /* 800 * We are already exclusive. i.e. we are already 801 * writer. Simply call the desired function. 802 */ 803 udp->udp_squeue_count++; 804 mutex_exit(&connp->conn_lock); 805 (*proc)(connp, mp, connp->conn_sqp); 806 return; 807 } 808 } 809 810 /* 811 * Transition from MT mode to SQUEUE mode, when the last MT thread 812 * is exiting the UDP perimeter. Move all messages from the internal 813 * udp queue to the squeue. A better way would be to move all the 814 * messages in one shot, this needs more support from the squeue framework 815 */ 816 static void 817 udp_switch_to_squeue(udp_t *udp) 818 { 819 mblk_t *mp; 820 mblk_t *mp_next; 821 sqproc_t proc; 822 uint8_t tag; 823 conn_t *connp = udp->udp_connp; 824 825 ASSERT(MUTEX_HELD(&connp->conn_lock)); 826 ASSERT(udp->udp_mode == UDP_MT_QUEUED); 827 while (udp->udp_mphead != NULL) { 828 mp = udp->udp_mphead; 829 udp->udp_mphead = NULL; 830 udp->udp_mptail = NULL; 831 udp->udp_mpcount = 0; 832 udp->udp_mode = UDP_QUEUED_SQUEUE; 833 mutex_exit(&connp->conn_lock); 834 /* 835 * It is best not to hold any locks across the calls 836 * to squeue functions. Since we drop the lock we 837 * need to go back and check the udp_mphead once again 838 * after the squeue_fill and hence the while loop at 839 * the top of this function 840 */ 841 for (; mp != NULL; mp = mp_next) { 842 mp_next = mp->b_next; 843 proc = (sqproc_t)mp->b_prev; 844 tag = (uint8_t)((uintptr_t)mp->b_queue); 845 mp->b_next = NULL; 846 mp->b_prev = NULL; 847 mp->b_queue = NULL; 848 CONN_INC_REF(connp); 849 udp->udp_squeue_count++; 850 squeue_fill(connp->conn_sqp, mp, proc, connp, 851 tag); 852 } 853 mutex_enter(&connp->conn_lock); 854 } 855 /* 856 * udp_squeue_count of zero implies that the squeue has drained 857 * even before we arrived here (i.e. after the squeue_fill above) 858 */ 859 udp->udp_mode = (udp->udp_squeue_count != 0) ? 860 UDP_SQUEUE : UDP_MT_HOT; 861 } 862 863 #define _UDP_EXIT(connp) { \ 864 udp_t *_udp = (connp)->conn_udp; \ 865 \ 866 mutex_enter(&(connp)->conn_lock); \ 867 UDP_MODE_ASSERTIONS(_udp, UDP_EXIT); \ 868 \ 869 switch (_udp->udp_mode) { \ 870 case UDP_MT_HOT: \ 871 UDP_READERS_DECREF(_udp); \ 872 mutex_exit(&(connp)->conn_lock); \ 873 break; \ 874 \ 875 case UDP_SQUEUE: \ 876 UDP_SQUEUE_DECREF(_udp); \ 877 if (_udp->udp_squeue_count == 0) \ 878 _udp->udp_mode = UDP_MT_HOT; \ 879 mutex_exit(&(connp)->conn_lock); \ 880 break; \ 881 \ 882 case UDP_MT_QUEUED: \ 883 /* \ 884 * If this is the last MT thread, we need to \ 885 * switch to squeue mode \ 886 */ \ 887 UDP_READERS_DECREF(_udp); \ 888 if (_udp->udp_reader_count == 0) \ 889 udp_switch_to_squeue(_udp); \ 890 mutex_exit(&(connp)->conn_lock); \ 891 break; \ 892 \ 893 case UDP_QUEUED_SQUEUE: \ 894 UDP_SQUEUE_DECREF(_udp); \ 895 /* \ 896 * Even if the udp_squeue_count drops to zero, we \ 897 * don't want to change udp_mode to UDP_MT_HOT here. \ 898 * The thread in udp_switch_to_squeue will take care \ 899 * of the transition to UDP_MT_HOT, after emptying \ 900 * any more new messages that have been enqueued in \ 901 * udp_mphead. \ 902 */ \ 903 mutex_exit(&(connp)->conn_lock); \ 904 break; \ 905 } \ 906 } 907 908 static void 909 udp_exit(conn_t *connp) 910 { 911 _UDP_EXIT(connp); 912 } 913 914 /* 915 * Return the next anonymous port in the privileged port range for 916 * bind checking. 917 * 918 * Trusted Extension (TX) notes: TX allows administrator to mark or 919 * reserve ports as Multilevel ports (MLP). MLP has special function 920 * on TX systems. Once a port is made MLP, it's not available as 921 * ordinary port. This creates "holes" in the port name space. It 922 * may be necessary to skip the "holes" find a suitable anon port. 923 */ 924 static in_port_t 925 udp_get_next_priv_port(udp_t *udp) 926 { 927 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 928 in_port_t nextport; 929 boolean_t restart = B_FALSE; 930 931 retry: 932 if (next_priv_port < udp_min_anonpriv_port || 933 next_priv_port >= IPPORT_RESERVED) { 934 next_priv_port = IPPORT_RESERVED - 1; 935 if (restart) 936 return (0); 937 restart = B_TRUE; 938 } 939 940 if (is_system_labeled() && 941 (nextport = tsol_next_port(crgetzone(udp->udp_connp->conn_cred), 942 next_priv_port, IPPROTO_UDP, B_FALSE)) != 0) { 943 next_priv_port = nextport; 944 goto retry; 945 } 946 947 return (next_priv_port--); 948 } 949 950 /* UDP bind hash report triggered via the Named Dispatch mechanism. */ 951 /* ARGSUSED */ 952 static int 953 udp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 954 { 955 udp_fanout_t *udpf; 956 int i; 957 zoneid_t zoneid; 958 conn_t *connp; 959 udp_t *udp; 960 961 connp = Q_TO_CONN(q); 962 udp = connp->conn_udp; 963 964 /* Refer to comments in udp_status_report(). */ 965 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 966 if (ddi_get_lbolt() - udp_last_ndd_get_info_time < 967 drv_usectohz(udp_ndd_get_info_interval * 1000)) { 968 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 969 return (0); 970 } 971 } 972 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 973 /* The following may work even if we cannot get a large buf. */ 974 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 975 return (0); 976 } 977 978 (void) mi_mpprintf(mp, 979 "UDP " MI_COL_HDRPAD_STR 980 /* 12345678[89ABCDEF] */ 981 " zone lport src addr dest addr port state"); 982 /* 1234 12345 xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx 12345 UNBOUND */ 983 984 zoneid = connp->conn_zoneid; 985 986 for (i = 0; i < udp_bind_fanout_size; i++) { 987 udpf = &udp_bind_fanout[i]; 988 mutex_enter(&udpf->uf_lock); 989 990 /* Print the hash index. */ 991 udp = udpf->uf_udp; 992 if (zoneid != GLOBAL_ZONEID) { 993 /* skip to first entry in this zone; might be none */ 994 while (udp != NULL && 995 udp->udp_connp->conn_zoneid != zoneid) 996 udp = udp->udp_bind_hash; 997 } 998 if (udp != NULL) { 999 uint_t print_len, buf_len; 1000 1001 buf_len = mp->b_cont->b_datap->db_lim - 1002 mp->b_cont->b_wptr; 1003 print_len = snprintf((char *)mp->b_cont->b_wptr, 1004 buf_len, "%d\n", i); 1005 if (print_len < buf_len) { 1006 mp->b_cont->b_wptr += print_len; 1007 } else { 1008 mp->b_cont->b_wptr += buf_len; 1009 } 1010 for (; udp != NULL; udp = udp->udp_bind_hash) { 1011 if (zoneid == GLOBAL_ZONEID || 1012 zoneid == udp->udp_connp->conn_zoneid) 1013 udp_report_item(mp->b_cont, udp); 1014 } 1015 } 1016 mutex_exit(&udpf->uf_lock); 1017 } 1018 udp_last_ndd_get_info_time = ddi_get_lbolt(); 1019 return (0); 1020 } 1021 1022 /* 1023 * Hash list removal routine for udp_t structures. 1024 */ 1025 static void 1026 udp_bind_hash_remove(udp_t *udp, boolean_t caller_holds_lock) 1027 { 1028 udp_t *udpnext; 1029 kmutex_t *lockp; 1030 1031 if (udp->udp_ptpbhn == NULL) 1032 return; 1033 1034 /* 1035 * Extract the lock pointer in case there are concurrent 1036 * hash_remove's for this instance. 1037 */ 1038 ASSERT(udp->udp_port != 0); 1039 if (!caller_holds_lock) { 1040 lockp = &udp_bind_fanout[UDP_BIND_HASH(udp->udp_port)].uf_lock; 1041 ASSERT(lockp != NULL); 1042 mutex_enter(lockp); 1043 } 1044 if (udp->udp_ptpbhn != NULL) { 1045 udpnext = udp->udp_bind_hash; 1046 if (udpnext != NULL) { 1047 udpnext->udp_ptpbhn = udp->udp_ptpbhn; 1048 udp->udp_bind_hash = NULL; 1049 } 1050 *udp->udp_ptpbhn = udpnext; 1051 udp->udp_ptpbhn = NULL; 1052 } 1053 if (!caller_holds_lock) { 1054 mutex_exit(lockp); 1055 } 1056 } 1057 1058 static void 1059 udp_bind_hash_insert(udp_fanout_t *uf, udp_t *udp) 1060 { 1061 udp_t **udpp; 1062 udp_t *udpnext; 1063 1064 ASSERT(MUTEX_HELD(&uf->uf_lock)); 1065 if (udp->udp_ptpbhn != NULL) { 1066 udp_bind_hash_remove(udp, B_TRUE); 1067 } 1068 udpp = &uf->uf_udp; 1069 udpnext = udpp[0]; 1070 if (udpnext != NULL) { 1071 /* 1072 * If the new udp bound to the INADDR_ANY address 1073 * and the first one in the list is not bound to 1074 * INADDR_ANY we skip all entries until we find the 1075 * first one bound to INADDR_ANY. 1076 * This makes sure that applications binding to a 1077 * specific address get preference over those binding to 1078 * INADDR_ANY. 1079 */ 1080 if (V6_OR_V4_INADDR_ANY(udp->udp_bound_v6src) && 1081 !V6_OR_V4_INADDR_ANY(udpnext->udp_bound_v6src)) { 1082 while ((udpnext = udpp[0]) != NULL && 1083 !V6_OR_V4_INADDR_ANY( 1084 udpnext->udp_bound_v6src)) { 1085 udpp = &(udpnext->udp_bind_hash); 1086 } 1087 if (udpnext != NULL) 1088 udpnext->udp_ptpbhn = &udp->udp_bind_hash; 1089 } else { 1090 udpnext->udp_ptpbhn = &udp->udp_bind_hash; 1091 } 1092 } 1093 udp->udp_bind_hash = udpnext; 1094 udp->udp_ptpbhn = udpp; 1095 udpp[0] = udp; 1096 } 1097 1098 /* 1099 * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message 1100 * passed to udp_wput. 1101 * It associates a port number and local address with the stream. 1102 * The O_T_BIND_REQ/T_BIND_REQ is passed downstream to ip with the UDP 1103 * protocol type (IPPROTO_UDP) placed in the message following the address. 1104 * A T_BIND_ACK message is passed upstream when ip acknowledges the request. 1105 * (Called as writer.) 1106 * 1107 * Note that UDP over IPv4 and IPv6 sockets can use the same port number 1108 * without setting SO_REUSEADDR. This is needed so that they 1109 * can be viewed as two independent transport protocols. 1110 * However, anonymouns ports are allocated from the same range to avoid 1111 * duplicating the udp_g_next_port_to_try. 1112 */ 1113 static void 1114 udp_bind(queue_t *q, mblk_t *mp) 1115 { 1116 sin_t *sin; 1117 sin6_t *sin6; 1118 mblk_t *mp1; 1119 in_port_t port; /* Host byte order */ 1120 in_port_t requested_port; /* Host byte order */ 1121 struct T_bind_req *tbr; 1122 int count; 1123 in6_addr_t v6src; 1124 boolean_t bind_to_req_port_only; 1125 int loopmax; 1126 udp_fanout_t *udpf; 1127 in_port_t lport; /* Network byte order */ 1128 zoneid_t zoneid; 1129 conn_t *connp; 1130 udp_t *udp; 1131 boolean_t is_inaddr_any; 1132 mlp_type_t addrtype, mlptype; 1133 1134 connp = Q_TO_CONN(q); 1135 udp = connp->conn_udp; 1136 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 1137 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 1138 "udp_bind: bad req, len %u", 1139 (uint_t)(mp->b_wptr - mp->b_rptr)); 1140 udp_err_ack(q, mp, TPROTO, 0); 1141 return; 1142 } 1143 1144 if (udp->udp_state != TS_UNBND) { 1145 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 1146 "udp_bind: bad state, %u", udp->udp_state); 1147 udp_err_ack(q, mp, TOUTSTATE, 0); 1148 return; 1149 } 1150 /* 1151 * Reallocate the message to make sure we have enough room for an 1152 * address and the protocol type. 1153 */ 1154 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 1155 if (!mp1) { 1156 udp_err_ack(q, mp, TSYSERR, ENOMEM); 1157 return; 1158 } 1159 1160 mp = mp1; 1161 tbr = (struct T_bind_req *)mp->b_rptr; 1162 switch (tbr->ADDR_length) { 1163 case 0: /* Request for a generic port */ 1164 tbr->ADDR_offset = sizeof (struct T_bind_req); 1165 if (udp->udp_family == AF_INET) { 1166 tbr->ADDR_length = sizeof (sin_t); 1167 sin = (sin_t *)&tbr[1]; 1168 *sin = sin_null; 1169 sin->sin_family = AF_INET; 1170 mp->b_wptr = (uchar_t *)&sin[1]; 1171 } else { 1172 ASSERT(udp->udp_family == AF_INET6); 1173 tbr->ADDR_length = sizeof (sin6_t); 1174 sin6 = (sin6_t *)&tbr[1]; 1175 *sin6 = sin6_null; 1176 sin6->sin6_family = AF_INET6; 1177 mp->b_wptr = (uchar_t *)&sin6[1]; 1178 } 1179 port = 0; 1180 break; 1181 1182 case sizeof (sin_t): /* Complete IPv4 address */ 1183 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 1184 sizeof (sin_t)); 1185 if (sin == NULL || !OK_32PTR((char *)sin)) { 1186 udp_err_ack(q, mp, TSYSERR, EINVAL); 1187 return; 1188 } 1189 if (udp->udp_family != AF_INET || 1190 sin->sin_family != AF_INET) { 1191 udp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); 1192 return; 1193 } 1194 port = ntohs(sin->sin_port); 1195 break; 1196 1197 case sizeof (sin6_t): /* complete IPv6 address */ 1198 sin6 = (sin6_t *)mi_offset_param(mp, tbr->ADDR_offset, 1199 sizeof (sin6_t)); 1200 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 1201 udp_err_ack(q, mp, TSYSERR, EINVAL); 1202 return; 1203 } 1204 if (udp->udp_family != AF_INET6 || 1205 sin6->sin6_family != AF_INET6) { 1206 udp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); 1207 return; 1208 } 1209 port = ntohs(sin6->sin6_port); 1210 break; 1211 1212 default: /* Invalid request */ 1213 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 1214 "udp_bind: bad ADDR_length length %u", tbr->ADDR_length); 1215 udp_err_ack(q, mp, TBADADDR, 0); 1216 return; 1217 } 1218 1219 requested_port = port; 1220 1221 if (requested_port == 0 || tbr->PRIM_type == O_T_BIND_REQ) 1222 bind_to_req_port_only = B_FALSE; 1223 else /* T_BIND_REQ and requested_port != 0 */ 1224 bind_to_req_port_only = B_TRUE; 1225 1226 if (requested_port == 0) { 1227 /* 1228 * If the application passed in zero for the port number, it 1229 * doesn't care which port number we bind to. Get one in the 1230 * valid range. 1231 */ 1232 if (udp->udp_anon_priv_bind) { 1233 port = udp_get_next_priv_port(udp); 1234 } else { 1235 port = udp_update_next_port(udp, 1236 udp_g_next_port_to_try, B_TRUE); 1237 } 1238 } else { 1239 /* 1240 * If the port is in the well-known privileged range, 1241 * make sure the caller was privileged. 1242 */ 1243 int i; 1244 boolean_t priv = B_FALSE; 1245 1246 if (port < udp_smallest_nonpriv_port) { 1247 priv = B_TRUE; 1248 } else { 1249 for (i = 0; i < udp_g_num_epriv_ports; i++) { 1250 if (port == udp_g_epriv_ports[i]) { 1251 priv = B_TRUE; 1252 break; 1253 } 1254 } 1255 } 1256 1257 if (priv) { 1258 cred_t *cr = DB_CREDDEF(mp, connp->conn_cred); 1259 1260 if (secpolicy_net_privaddr(cr, port) != 0) { 1261 udp_err_ack(q, mp, TACCES, 0); 1262 return; 1263 } 1264 } 1265 } 1266 1267 if (port == 0) { 1268 udp_err_ack(q, mp, TNOADDR, 0); 1269 return; 1270 } 1271 1272 /* 1273 * Copy the source address into our udp structure. This address 1274 * may still be zero; if so, IP will fill in the correct address 1275 * each time an outbound packet is passed to it. 1276 */ 1277 if (udp->udp_family == AF_INET) { 1278 ASSERT(sin != NULL); 1279 ASSERT(udp->udp_ipversion == IPV4_VERSION); 1280 udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + UDPH_SIZE + 1281 udp->udp_ip_snd_options_len; 1282 IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &v6src); 1283 } else { 1284 ASSERT(sin6 != NULL); 1285 v6src = sin6->sin6_addr; 1286 if (IN6_IS_ADDR_V4MAPPED(&v6src)) { 1287 udp->udp_ipversion = IPV4_VERSION; 1288 udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + 1289 UDPH_SIZE + udp->udp_ip_snd_options_len; 1290 } else { 1291 udp->udp_ipversion = IPV6_VERSION; 1292 udp->udp_max_hdr_len = udp->udp_sticky_hdrs_len; 1293 } 1294 } 1295 1296 /* 1297 * If udp_reuseaddr is not set, then we have to make sure that 1298 * the IP address and port number the application requested 1299 * (or we selected for the application) is not being used by 1300 * another stream. If another stream is already using the 1301 * requested IP address and port, the behavior depends on 1302 * "bind_to_req_port_only". If set the bind fails; otherwise we 1303 * search for any an unused port to bind to the the stream. 1304 * 1305 * As per the BSD semantics, as modified by the Deering multicast 1306 * changes, if udp_reuseaddr is set, then we allow multiple binds 1307 * to the same port independent of the local IP address. 1308 * 1309 * This is slightly different than in SunOS 4.X which did not 1310 * support IP multicast. Note that the change implemented by the 1311 * Deering multicast code effects all binds - not only binding 1312 * to IP multicast addresses. 1313 * 1314 * Note that when binding to port zero we ignore SO_REUSEADDR in 1315 * order to guarantee a unique port. 1316 */ 1317 1318 count = 0; 1319 if (udp->udp_anon_priv_bind) { 1320 /* loopmax = (IPPORT_RESERVED-1) - udp_min_anonpriv_port + 1 */ 1321 loopmax = IPPORT_RESERVED - udp_min_anonpriv_port; 1322 } else { 1323 loopmax = udp_largest_anon_port - udp_smallest_anon_port + 1; 1324 } 1325 1326 is_inaddr_any = V6_OR_V4_INADDR_ANY(v6src); 1327 zoneid = connp->conn_zoneid; 1328 1329 for (;;) { 1330 udp_t *udp1; 1331 boolean_t found_exclbind = B_FALSE; 1332 1333 /* 1334 * Walk through the list of udp streams bound to 1335 * requested port with the same IP address. 1336 */ 1337 lport = htons(port); 1338 udpf = &udp_bind_fanout[UDP_BIND_HASH(lport)]; 1339 mutex_enter(&udpf->uf_lock); 1340 for (udp1 = udpf->uf_udp; udp1 != NULL; 1341 udp1 = udp1->udp_bind_hash) { 1342 if (lport != udp1->udp_port) 1343 continue; 1344 1345 /* 1346 * On a labeled system, we must treat bindings to ports 1347 * on shared IP addresses by sockets with MAC exemption 1348 * privilege as being in all zones, as there's 1349 * otherwise no way to identify the right receiver. 1350 */ 1351 if (zoneid != udp1->udp_connp->conn_zoneid && 1352 !udp->udp_mac_exempt && !udp1->udp_mac_exempt) 1353 continue; 1354 1355 /* 1356 * If UDP_EXCLBIND is set for either the bound or 1357 * binding endpoint, the semantics of bind 1358 * is changed according to the following chart. 1359 * 1360 * spec = specified address (v4 or v6) 1361 * unspec = unspecified address (v4 or v6) 1362 * A = specified addresses are different for endpoints 1363 * 1364 * bound bind to allowed? 1365 * ------------------------------------- 1366 * unspec unspec no 1367 * unspec spec no 1368 * spec unspec no 1369 * spec spec yes if A 1370 * 1371 * For labeled systems, SO_MAC_EXEMPT behaves the same 1372 * as UDP_EXCLBIND, except that zoneid is ignored. 1373 */ 1374 if (udp1->udp_exclbind || udp->udp_exclbind || 1375 udp1->udp_mac_exempt || udp->udp_mac_exempt) { 1376 if (V6_OR_V4_INADDR_ANY( 1377 udp1->udp_bound_v6src) || 1378 is_inaddr_any || 1379 IN6_ARE_ADDR_EQUAL(&udp1->udp_bound_v6src, 1380 &v6src)) { 1381 found_exclbind = B_TRUE; 1382 break; 1383 } 1384 continue; 1385 } 1386 1387 /* 1388 * Check ipversion to allow IPv4 and IPv6 sockets to 1389 * have disjoint port number spaces. 1390 */ 1391 if (udp->udp_ipversion != udp1->udp_ipversion) { 1392 1393 /* 1394 * On the first time through the loop, if the 1395 * the user intentionally specified a 1396 * particular port number, then ignore any 1397 * bindings of the other protocol that may 1398 * conflict. This allows the user to bind IPv6 1399 * alone and get both v4 and v6, or bind both 1400 * both and get each seperately. On subsequent 1401 * times through the loop, we're checking a 1402 * port that we chose (not the user) and thus 1403 * we do not allow casual duplicate bindings. 1404 */ 1405 if (count == 0 && requested_port != 0) 1406 continue; 1407 } 1408 1409 /* 1410 * No difference depending on SO_REUSEADDR. 1411 * 1412 * If existing port is bound to a 1413 * non-wildcard IP address and 1414 * the requesting stream is bound to 1415 * a distinct different IP addresses 1416 * (non-wildcard, also), keep going. 1417 */ 1418 if (!is_inaddr_any && 1419 !V6_OR_V4_INADDR_ANY(udp1->udp_bound_v6src) && 1420 !IN6_ARE_ADDR_EQUAL(&udp1->udp_bound_v6src, 1421 &v6src)) { 1422 continue; 1423 } 1424 break; 1425 } 1426 1427 if (!found_exclbind && 1428 (udp->udp_reuseaddr && requested_port != 0)) { 1429 break; 1430 } 1431 1432 if (udp1 == NULL) { 1433 /* 1434 * No other stream has this IP address 1435 * and port number. We can use it. 1436 */ 1437 break; 1438 } 1439 mutex_exit(&udpf->uf_lock); 1440 if (bind_to_req_port_only) { 1441 /* 1442 * We get here only when requested port 1443 * is bound (and only first of the for() 1444 * loop iteration). 1445 * 1446 * The semantics of this bind request 1447 * require it to fail so we return from 1448 * the routine (and exit the loop). 1449 * 1450 */ 1451 udp_err_ack(q, mp, TADDRBUSY, 0); 1452 return; 1453 } 1454 1455 if (udp->udp_anon_priv_bind) { 1456 port = udp_get_next_priv_port(udp); 1457 } else { 1458 if ((count == 0) && (requested_port != 0)) { 1459 /* 1460 * If the application wants us to find 1461 * a port, get one to start with. Set 1462 * requested_port to 0, so that we will 1463 * update udp_g_next_port_to_try below. 1464 */ 1465 port = udp_update_next_port(udp, 1466 udp_g_next_port_to_try, B_TRUE); 1467 requested_port = 0; 1468 } else { 1469 port = udp_update_next_port(udp, port + 1, 1470 B_FALSE); 1471 } 1472 } 1473 1474 if (port == 0 || ++count >= loopmax) { 1475 /* 1476 * We've tried every possible port number and 1477 * there are none available, so send an error 1478 * to the user. 1479 */ 1480 udp_err_ack(q, mp, TNOADDR, 0); 1481 return; 1482 } 1483 } 1484 1485 /* 1486 * Copy the source address into our udp structure. This address 1487 * may still be zero; if so, ip will fill in the correct address 1488 * each time an outbound packet is passed to it. 1489 * If we are binding to a broadcast or multicast address udp_rput 1490 * will clear the source address when it receives the T_BIND_ACK. 1491 */ 1492 udp->udp_v6src = udp->udp_bound_v6src = v6src; 1493 udp->udp_port = lport; 1494 /* 1495 * Now reset the the next anonymous port if the application requested 1496 * an anonymous port, or we handed out the next anonymous port. 1497 */ 1498 if ((requested_port == 0) && (!udp->udp_anon_priv_bind)) { 1499 udp_g_next_port_to_try = port + 1; 1500 } 1501 1502 /* Initialize the O_T_BIND_REQ/T_BIND_REQ for ip. */ 1503 if (udp->udp_family == AF_INET) { 1504 sin->sin_port = udp->udp_port; 1505 } else { 1506 int error; 1507 1508 sin6->sin6_port = udp->udp_port; 1509 /* Rebuild the header template */ 1510 error = udp_build_hdrs(q, udp); 1511 if (error != 0) { 1512 mutex_exit(&udpf->uf_lock); 1513 udp_err_ack(q, mp, TSYSERR, error); 1514 return; 1515 } 1516 } 1517 udp->udp_state = TS_IDLE; 1518 udp_bind_hash_insert(udpf, udp); 1519 mutex_exit(&udpf->uf_lock); 1520 1521 if (cl_inet_bind) { 1522 /* 1523 * Running in cluster mode - register bind information 1524 */ 1525 if (udp->udp_ipversion == IPV4_VERSION) { 1526 (*cl_inet_bind)(IPPROTO_UDP, AF_INET, 1527 (uint8_t *)(&V4_PART_OF_V6(udp->udp_v6src)), 1528 (in_port_t)udp->udp_port); 1529 } else { 1530 (*cl_inet_bind)(IPPROTO_UDP, AF_INET6, 1531 (uint8_t *)&(udp->udp_v6src), 1532 (in_port_t)udp->udp_port); 1533 } 1534 1535 } 1536 1537 connp->conn_anon_port = (is_system_labeled() && requested_port == 0); 1538 if (is_system_labeled() && (!connp->conn_anon_port || 1539 connp->conn_anon_mlp)) { 1540 uint16_t mlpport; 1541 cred_t *cr = connp->conn_cred; 1542 zone_t *zone; 1543 1544 connp->conn_mlp_type = udp->udp_recvucred ? mlptBoth : 1545 mlptSingle; 1546 addrtype = tsol_mlp_addr_type(zoneid, IPV6_VERSION, &v6src); 1547 if (addrtype == mlptSingle) { 1548 udp_err_ack(q, mp, TNOADDR, 0); 1549 connp->conn_anon_port = B_FALSE; 1550 connp->conn_mlp_type = mlptSingle; 1551 return; 1552 } 1553 mlpport = connp->conn_anon_port ? PMAPPORT : port; 1554 zone = crgetzone(cr); 1555 mlptype = tsol_mlp_port_type(zone, IPPROTO_UDP, mlpport, 1556 addrtype); 1557 if (mlptype != mlptSingle && 1558 (connp->conn_mlp_type == mlptSingle || 1559 secpolicy_net_bindmlp(cr) != 0)) { 1560 if (udp->udp_debug) { 1561 (void) strlog(UDP_MOD_ID, 0, 1, 1562 SL_ERROR|SL_TRACE, 1563 "udp_bind: no priv for multilevel port %d", 1564 mlpport); 1565 } 1566 udp_err_ack(q, mp, TACCES, 0); 1567 connp->conn_anon_port = B_FALSE; 1568 connp->conn_mlp_type = mlptSingle; 1569 return; 1570 } 1571 1572 /* 1573 * If we're specifically binding a shared IP address and the 1574 * port is MLP on shared addresses, then check to see if this 1575 * zone actually owns the MLP. Reject if not. 1576 */ 1577 if (mlptype == mlptShared && addrtype == mlptShared) { 1578 zoneid_t mlpzone; 1579 1580 mlpzone = tsol_mlp_findzone(IPPROTO_UDP, 1581 htons(mlpport)); 1582 if (connp->conn_zoneid != mlpzone) { 1583 if (udp->udp_debug) { 1584 (void) strlog(UDP_MOD_ID, 0, 1, 1585 SL_ERROR|SL_TRACE, 1586 "udp_bind: attempt to bind port " 1587 "%d on shared addr in zone %d " 1588 "(should be %d)", 1589 mlpport, connp->conn_zoneid, 1590 mlpzone); 1591 } 1592 udp_err_ack(q, mp, TACCES, 0); 1593 connp->conn_anon_port = B_FALSE; 1594 connp->conn_mlp_type = mlptSingle; 1595 return; 1596 } 1597 } 1598 if (connp->conn_anon_port) { 1599 int error; 1600 1601 error = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 1602 port, B_TRUE); 1603 if (error != 0) { 1604 if (udp->udp_debug) { 1605 (void) strlog(UDP_MOD_ID, 0, 1, 1606 SL_ERROR|SL_TRACE, 1607 "udp_bind: cannot establish anon " 1608 "MLP for port %d", port); 1609 } 1610 udp_err_ack(q, mp, TACCES, 0); 1611 connp->conn_anon_port = B_FALSE; 1612 connp->conn_mlp_type = mlptSingle; 1613 return; 1614 } 1615 } 1616 connp->conn_mlp_type = mlptype; 1617 } 1618 1619 /* Pass the protocol number in the message following the address. */ 1620 *mp->b_wptr++ = IPPROTO_UDP; 1621 if (!V6_OR_V4_INADDR_ANY(udp->udp_v6src)) { 1622 /* 1623 * Append a request for an IRE if udp_v6src not 1624 * zero (IPv4 - INADDR_ANY, or IPv6 - all-zeroes address). 1625 */ 1626 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 1627 if (!mp->b_cont) { 1628 udp_err_ack(q, mp, TSYSERR, ENOMEM); 1629 return; 1630 } 1631 mp->b_cont->b_wptr += sizeof (ire_t); 1632 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 1633 } 1634 if (udp->udp_family == AF_INET6) 1635 mp = ip_bind_v6(q, mp, connp, NULL); 1636 else 1637 mp = ip_bind_v4(q, mp, connp); 1638 1639 if (mp != NULL) 1640 udp_rput_other(_RD(q), mp); 1641 else 1642 CONN_INC_REF(connp); 1643 } 1644 1645 1646 void 1647 udp_resume_bind(conn_t *connp, mblk_t *mp) 1648 { 1649 udp_enter(connp, mp, udp_resume_bind_cb, SQTAG_BIND_RETRY); 1650 } 1651 1652 /* 1653 * This is called from ip_wput_nondata to resume a deferred UDP bind. 1654 */ 1655 /* ARGSUSED */ 1656 static void 1657 udp_resume_bind_cb(void *arg, mblk_t *mp, void *arg2) 1658 { 1659 conn_t *connp = arg; 1660 1661 ASSERT(connp != NULL && IPCL_IS_UDP(connp)); 1662 1663 udp_rput_other(connp->conn_rq, mp); 1664 1665 CONN_OPER_PENDING_DONE(connp); 1666 udp_exit(connp); 1667 } 1668 1669 /* 1670 * This routine handles each T_CONN_REQ message passed to udp. It 1671 * associates a default destination address with the stream. 1672 * 1673 * This routine sends down a T_BIND_REQ to IP with the following mblks: 1674 * T_BIND_REQ - specifying local and remote address/port 1675 * IRE_DB_REQ_TYPE - to get an IRE back containing ire_type and src 1676 * T_OK_ACK - for the T_CONN_REQ 1677 * T_CONN_CON - to keep the TPI user happy 1678 * 1679 * The connect completes in udp_rput. 1680 * When a T_BIND_ACK is received information is extracted from the IRE 1681 * and the two appended messages are sent to the TPI user. 1682 * Should udp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert 1683 * it to an error ack for the appropriate primitive. 1684 */ 1685 static void 1686 udp_connect(queue_t *q, mblk_t *mp) 1687 { 1688 sin6_t *sin6; 1689 sin_t *sin; 1690 struct T_conn_req *tcr; 1691 in6_addr_t v6dst; 1692 ipaddr_t v4dst; 1693 uint16_t dstport; 1694 uint32_t flowinfo; 1695 mblk_t *mp1, *mp2; 1696 udp_fanout_t *udpf; 1697 udp_t *udp, *udp1; 1698 1699 udp = Q_TO_UDP(q); 1700 1701 tcr = (struct T_conn_req *)mp->b_rptr; 1702 1703 /* A bit of sanity checking */ 1704 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_req)) { 1705 udp_err_ack(q, mp, TPROTO, 0); 1706 return; 1707 } 1708 /* 1709 * This UDP must have bound to a port already before doing 1710 * a connect. 1711 */ 1712 if (udp->udp_state == TS_UNBND) { 1713 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 1714 "udp_connect: bad state, %u", udp->udp_state); 1715 udp_err_ack(q, mp, TOUTSTATE, 0); 1716 return; 1717 } 1718 ASSERT(udp->udp_port != 0 && udp->udp_ptpbhn != NULL); 1719 1720 udpf = &udp_bind_fanout[UDP_BIND_HASH(udp->udp_port)]; 1721 1722 if (udp->udp_state == TS_DATA_XFER) { 1723 /* Already connected - clear out state */ 1724 mutex_enter(&udpf->uf_lock); 1725 udp->udp_v6src = udp->udp_bound_v6src; 1726 udp->udp_state = TS_IDLE; 1727 mutex_exit(&udpf->uf_lock); 1728 } 1729 1730 if (tcr->OPT_length != 0) { 1731 udp_err_ack(q, mp, TBADOPT, 0); 1732 return; 1733 } 1734 1735 /* 1736 * Determine packet type based on type of address passed in 1737 * the request should contain an IPv4 or IPv6 address. 1738 * Make sure that address family matches the type of 1739 * family of the the address passed down 1740 */ 1741 switch (tcr->DEST_length) { 1742 default: 1743 udp_err_ack(q, mp, TBADADDR, 0); 1744 return; 1745 1746 case sizeof (sin_t): 1747 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 1748 sizeof (sin_t)); 1749 if (sin == NULL || !OK_32PTR((char *)sin)) { 1750 udp_err_ack(q, mp, TSYSERR, EINVAL); 1751 return; 1752 } 1753 if (udp->udp_family != AF_INET || 1754 sin->sin_family != AF_INET) { 1755 udp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); 1756 return; 1757 } 1758 v4dst = sin->sin_addr.s_addr; 1759 dstport = sin->sin_port; 1760 IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst); 1761 ASSERT(udp->udp_ipversion == IPV4_VERSION); 1762 udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + UDPH_SIZE + 1763 udp->udp_ip_snd_options_len; 1764 break; 1765 1766 case sizeof (sin6_t): 1767 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 1768 sizeof (sin6_t)); 1769 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 1770 udp_err_ack(q, mp, TSYSERR, EINVAL); 1771 return; 1772 } 1773 if (udp->udp_family != AF_INET6 || 1774 sin6->sin6_family != AF_INET6) { 1775 udp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); 1776 return; 1777 } 1778 v6dst = sin6->sin6_addr; 1779 if (IN6_IS_ADDR_V4MAPPED(&v6dst)) { 1780 IN6_V4MAPPED_TO_IPADDR(&v6dst, v4dst); 1781 udp->udp_ipversion = IPV4_VERSION; 1782 udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + 1783 UDPH_SIZE + udp->udp_ip_snd_options_len; 1784 flowinfo = 0; 1785 } else { 1786 udp->udp_ipversion = IPV6_VERSION; 1787 udp->udp_max_hdr_len = udp->udp_sticky_hdrs_len; 1788 flowinfo = sin6->sin6_flowinfo; 1789 } 1790 dstport = sin6->sin6_port; 1791 break; 1792 } 1793 if (dstport == 0) { 1794 udp_err_ack(q, mp, TBADADDR, 0); 1795 return; 1796 } 1797 1798 /* 1799 * Create a default IP header with no IP options. 1800 */ 1801 udp->udp_dstport = dstport; 1802 if (udp->udp_ipversion == IPV4_VERSION) { 1803 /* 1804 * Interpret a zero destination to mean loopback. 1805 * Update the T_CONN_REQ (sin/sin6) since it is used to 1806 * generate the T_CONN_CON. 1807 */ 1808 if (v4dst == INADDR_ANY) { 1809 v4dst = htonl(INADDR_LOOPBACK); 1810 IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst); 1811 if (udp->udp_family == AF_INET) { 1812 sin->sin_addr.s_addr = v4dst; 1813 } else { 1814 sin6->sin6_addr = v6dst; 1815 } 1816 } 1817 udp->udp_v6dst = v6dst; 1818 udp->udp_flowinfo = 0; 1819 1820 /* 1821 * If the destination address is multicast and 1822 * an outgoing multicast interface has been set, 1823 * use the address of that interface as our 1824 * source address if no source address has been set. 1825 */ 1826 if (V4_PART_OF_V6(udp->udp_v6src) == INADDR_ANY && 1827 CLASSD(v4dst) && 1828 udp->udp_multicast_if_addr != INADDR_ANY) { 1829 IN6_IPADDR_TO_V4MAPPED(udp->udp_multicast_if_addr, 1830 &udp->udp_v6src); 1831 } 1832 } else { 1833 ASSERT(udp->udp_ipversion == IPV6_VERSION); 1834 /* 1835 * Interpret a zero destination to mean loopback. 1836 * Update the T_CONN_REQ (sin/sin6) since it is used to 1837 * generate the T_CONN_CON. 1838 */ 1839 if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) { 1840 v6dst = ipv6_loopback; 1841 sin6->sin6_addr = v6dst; 1842 } 1843 udp->udp_v6dst = v6dst; 1844 udp->udp_flowinfo = flowinfo; 1845 /* 1846 * If the destination address is multicast and 1847 * an outgoing multicast interface has been set, 1848 * then the ip bind logic will pick the correct source 1849 * address (i.e. matching the outgoing multicast interface). 1850 */ 1851 } 1852 1853 /* 1854 * Verify that the src/port/dst/port is unique for all 1855 * connections in TS_DATA_XFER 1856 */ 1857 mutex_enter(&udpf->uf_lock); 1858 for (udp1 = udpf->uf_udp; udp1 != NULL; udp1 = udp1->udp_bind_hash) { 1859 if (udp1->udp_state != TS_DATA_XFER) 1860 continue; 1861 if (udp->udp_port != udp1->udp_port || 1862 udp->udp_ipversion != udp1->udp_ipversion || 1863 dstport != udp1->udp_dstport || 1864 !IN6_ARE_ADDR_EQUAL(&udp->udp_v6src, &udp1->udp_v6src) || 1865 !IN6_ARE_ADDR_EQUAL(&v6dst, &udp1->udp_v6dst)) 1866 continue; 1867 mutex_exit(&udpf->uf_lock); 1868 udp_err_ack(q, mp, TBADADDR, 0); 1869 return; 1870 } 1871 udp->udp_state = TS_DATA_XFER; 1872 mutex_exit(&udpf->uf_lock); 1873 1874 /* 1875 * Send down bind to IP to verify that there is a route 1876 * and to determine the source address. 1877 * This will come back as T_BIND_ACK with an IRE_DB_TYPE in rput. 1878 */ 1879 if (udp->udp_family == AF_INET) 1880 mp1 = udp_ip_bind_mp(udp, O_T_BIND_REQ, sizeof (ipa_conn_t)); 1881 else 1882 mp1 = udp_ip_bind_mp(udp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 1883 if (mp1 == NULL) { 1884 udp_err_ack(q, mp, TSYSERR, ENOMEM); 1885 bind_failed: 1886 mutex_enter(&udpf->uf_lock); 1887 udp->udp_state = TS_IDLE; 1888 mutex_exit(&udpf->uf_lock); 1889 return; 1890 } 1891 1892 /* 1893 * We also have to send a connection confirmation to 1894 * keep TLI happy. Prepare it for udp_rput. 1895 */ 1896 if (udp->udp_family == AF_INET) 1897 mp2 = mi_tpi_conn_con(NULL, (char *)sin, 1898 sizeof (*sin), NULL, 0); 1899 else 1900 mp2 = mi_tpi_conn_con(NULL, (char *)sin6, 1901 sizeof (*sin6), NULL, 0); 1902 if (mp2 == NULL) { 1903 freemsg(mp1); 1904 udp_err_ack(q, mp, TSYSERR, ENOMEM); 1905 goto bind_failed; 1906 } 1907 1908 mp = mi_tpi_ok_ack_alloc(mp); 1909 if (mp == NULL) { 1910 /* Unable to reuse the T_CONN_REQ for the ack. */ 1911 freemsg(mp2); 1912 udp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM); 1913 goto bind_failed; 1914 } 1915 1916 /* Hang onto the T_OK_ACK and T_CONN_CON for later. */ 1917 linkb(mp1, mp); 1918 linkb(mp1, mp2); 1919 1920 mblk_setcred(mp1, udp->udp_connp->conn_cred); 1921 if (udp->udp_family == AF_INET) 1922 mp1 = ip_bind_v4(q, mp1, udp->udp_connp); 1923 else 1924 mp1 = ip_bind_v6(q, mp1, udp->udp_connp, NULL); 1925 1926 if (mp1 != NULL) 1927 udp_rput_other(_RD(q), mp1); 1928 else 1929 CONN_INC_REF(udp->udp_connp); 1930 } 1931 1932 static int 1933 udp_close(queue_t *q) 1934 { 1935 conn_t *connp = Q_TO_CONN(UDP_WR(q)); 1936 udp_t *udp; 1937 queue_t *ip_rq = RD(UDP_WR(q)); 1938 1939 ASSERT(connp != NULL && IPCL_IS_UDP(connp)); 1940 udp = connp->conn_udp; 1941 1942 ip_quiesce_conn(connp); 1943 /* 1944 * Disable read-side synchronous stream 1945 * interface and drain any queued data. 1946 */ 1947 udp_rcv_drain(q, udp, B_TRUE); 1948 ASSERT(!udp->udp_direct_sockfs); 1949 1950 qprocsoff(q); 1951 1952 /* restore IP module's high and low water marks to default values */ 1953 ip_rq->q_hiwat = ip_rq->q_qinfo->qi_minfo->mi_hiwat; 1954 WR(ip_rq)->q_hiwat = WR(ip_rq)->q_qinfo->qi_minfo->mi_hiwat; 1955 WR(ip_rq)->q_lowat = WR(ip_rq)->q_qinfo->qi_minfo->mi_lowat; 1956 1957 ASSERT(udp->udp_rcv_cnt == 0); 1958 ASSERT(udp->udp_rcv_msgcnt == 0); 1959 ASSERT(udp->udp_rcv_list_head == NULL); 1960 ASSERT(udp->udp_rcv_list_tail == NULL); 1961 1962 /* connp is now single threaded. */ 1963 udp_close_free(connp); 1964 /* 1965 * Restore connp as an IP endpoint. We don't need 1966 * any locks since we are now single threaded 1967 */ 1968 connp->conn_flags &= ~IPCL_UDP; 1969 connp->conn_state_flags &= 1970 ~(CONN_CLOSING | CONN_CONDEMNED | CONN_QUIESCED); 1971 connp->conn_ulp_labeled = B_FALSE; 1972 return (0); 1973 } 1974 1975 /* 1976 * Called in the close path from IP (ip_quiesce_conn) to quiesce the conn 1977 */ 1978 void 1979 udp_quiesce_conn(conn_t *connp) 1980 { 1981 udp_t *udp = connp->conn_udp; 1982 1983 if (cl_inet_unbind != NULL && udp->udp_state == TS_IDLE) { 1984 /* 1985 * Running in cluster mode - register unbind information 1986 */ 1987 if (udp->udp_ipversion == IPV4_VERSION) { 1988 (*cl_inet_unbind)(IPPROTO_UDP, AF_INET, 1989 (uint8_t *)(&(V4_PART_OF_V6(udp->udp_v6src))), 1990 (in_port_t)udp->udp_port); 1991 } else { 1992 (*cl_inet_unbind)(IPPROTO_UDP, AF_INET6, 1993 (uint8_t *)(&(udp->udp_v6src)), 1994 (in_port_t)udp->udp_port); 1995 } 1996 } 1997 1998 udp_bind_hash_remove(udp, B_FALSE); 1999 2000 mutex_enter(&connp->conn_lock); 2001 while (udp->udp_reader_count != 0 || udp->udp_squeue_count != 0 || 2002 udp->udp_mode != UDP_MT_HOT) { 2003 cv_wait(&connp->conn_cv, &connp->conn_lock); 2004 } 2005 mutex_exit(&connp->conn_lock); 2006 } 2007 2008 void 2009 udp_close_free(conn_t *connp) 2010 { 2011 udp_t *udp = connp->conn_udp; 2012 2013 /* If there are any options associated with the stream, free them. */ 2014 if (udp->udp_ip_snd_options) { 2015 mi_free((char *)udp->udp_ip_snd_options); 2016 udp->udp_ip_snd_options = NULL; 2017 } 2018 2019 if (udp->udp_ip_rcv_options) { 2020 mi_free((char *)udp->udp_ip_rcv_options); 2021 udp->udp_ip_rcv_options = NULL; 2022 } 2023 2024 /* Free memory associated with sticky options */ 2025 if (udp->udp_sticky_hdrs_len != 0) { 2026 kmem_free(udp->udp_sticky_hdrs, 2027 udp->udp_sticky_hdrs_len); 2028 udp->udp_sticky_hdrs = NULL; 2029 udp->udp_sticky_hdrs_len = 0; 2030 } 2031 2032 ip6_pkt_free(&udp->udp_sticky_ipp); 2033 2034 udp->udp_connp = NULL; 2035 connp->conn_udp = NULL; 2036 kmem_cache_free(udp_cache, udp); 2037 } 2038 2039 /* 2040 * This routine handles each T_DISCON_REQ message passed to udp 2041 * as an indicating that UDP is no longer connected. This results 2042 * in sending a T_BIND_REQ to IP to restore the binding to just 2043 * the local address/port. 2044 * 2045 * This routine sends down a T_BIND_REQ to IP with the following mblks: 2046 * T_BIND_REQ - specifying just the local address/port 2047 * T_OK_ACK - for the T_DISCON_REQ 2048 * 2049 * The disconnect completes in udp_rput. 2050 * When a T_BIND_ACK is received the appended T_OK_ACK is sent to the TPI user. 2051 * Should udp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert 2052 * it to an error ack for the appropriate primitive. 2053 */ 2054 static void 2055 udp_disconnect(queue_t *q, mblk_t *mp) 2056 { 2057 udp_t *udp = Q_TO_UDP(q); 2058 mblk_t *mp1; 2059 udp_fanout_t *udpf; 2060 2061 if (udp->udp_state != TS_DATA_XFER) { 2062 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 2063 "udp_disconnect: bad state, %u", udp->udp_state); 2064 udp_err_ack(q, mp, TOUTSTATE, 0); 2065 return; 2066 } 2067 udpf = &udp_bind_fanout[UDP_BIND_HASH(udp->udp_port)]; 2068 mutex_enter(&udpf->uf_lock); 2069 udp->udp_v6src = udp->udp_bound_v6src; 2070 udp->udp_state = TS_IDLE; 2071 mutex_exit(&udpf->uf_lock); 2072 2073 /* 2074 * Send down bind to IP to remove the full binding and revert 2075 * to the local address binding. 2076 */ 2077 if (udp->udp_family == AF_INET) 2078 mp1 = udp_ip_bind_mp(udp, O_T_BIND_REQ, sizeof (sin_t)); 2079 else 2080 mp1 = udp_ip_bind_mp(udp, O_T_BIND_REQ, sizeof (sin6_t)); 2081 if (mp1 == NULL) { 2082 udp_err_ack(q, mp, TSYSERR, ENOMEM); 2083 return; 2084 } 2085 mp = mi_tpi_ok_ack_alloc(mp); 2086 if (mp == NULL) { 2087 /* Unable to reuse the T_DISCON_REQ for the ack. */ 2088 udp_err_ack_prim(q, mp1, T_DISCON_REQ, TSYSERR, ENOMEM); 2089 return; 2090 } 2091 2092 if (udp->udp_family == AF_INET6) { 2093 int error; 2094 2095 /* Rebuild the header template */ 2096 error = udp_build_hdrs(q, udp); 2097 if (error != 0) { 2098 udp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, error); 2099 freemsg(mp1); 2100 return; 2101 } 2102 } 2103 mutex_enter(&udpf->uf_lock); 2104 udp->udp_discon_pending = 1; 2105 mutex_exit(&udpf->uf_lock); 2106 2107 /* Append the T_OK_ACK to the T_BIND_REQ for udp_rput */ 2108 linkb(mp1, mp); 2109 2110 if (udp->udp_family == AF_INET6) 2111 mp1 = ip_bind_v6(q, mp1, udp->udp_connp, NULL); 2112 else 2113 mp1 = ip_bind_v4(q, mp1, udp->udp_connp); 2114 2115 if (mp1 != NULL) 2116 udp_rput_other(_RD(q), mp1); 2117 else 2118 CONN_INC_REF(udp->udp_connp); 2119 } 2120 2121 /* This routine creates a T_ERROR_ACK message and passes it upstream. */ 2122 static void 2123 udp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error) 2124 { 2125 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 2126 putnext(UDP_RD(q), mp); 2127 } 2128 2129 /* Shorthand to generate and send TPI error acks to our client */ 2130 static void 2131 udp_err_ack_prim(queue_t *q, mblk_t *mp, int primitive, t_scalar_t t_error, 2132 int sys_error) 2133 { 2134 struct T_error_ack *teackp; 2135 2136 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 2137 M_PCPROTO, T_ERROR_ACK)) != NULL) { 2138 teackp = (struct T_error_ack *)mp->b_rptr; 2139 teackp->ERROR_prim = primitive; 2140 teackp->TLI_error = t_error; 2141 teackp->UNIX_error = sys_error; 2142 putnext(UDP_RD(q), mp); 2143 } 2144 } 2145 2146 /*ARGSUSED*/ 2147 static int 2148 udp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 2149 { 2150 int i; 2151 2152 for (i = 0; i < udp_g_num_epriv_ports; i++) { 2153 if (udp_g_epriv_ports[i] != 0) 2154 (void) mi_mpprintf(mp, "%d ", udp_g_epriv_ports[i]); 2155 } 2156 return (0); 2157 } 2158 2159 /* ARGSUSED */ 2160 static int 2161 udp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 2162 cred_t *cr) 2163 { 2164 long new_value; 2165 int i; 2166 2167 /* 2168 * Fail the request if the new value does not lie within the 2169 * port number limits. 2170 */ 2171 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 2172 new_value <= 0 || new_value >= 65536) { 2173 return (EINVAL); 2174 } 2175 2176 /* Check if the value is already in the list */ 2177 for (i = 0; i < udp_g_num_epriv_ports; i++) { 2178 if (new_value == udp_g_epriv_ports[i]) { 2179 return (EEXIST); 2180 } 2181 } 2182 /* Find an empty slot */ 2183 for (i = 0; i < udp_g_num_epriv_ports; i++) { 2184 if (udp_g_epriv_ports[i] == 0) 2185 break; 2186 } 2187 if (i == udp_g_num_epriv_ports) { 2188 return (EOVERFLOW); 2189 } 2190 2191 /* Set the new value */ 2192 udp_g_epriv_ports[i] = (in_port_t)new_value; 2193 return (0); 2194 } 2195 2196 /* ARGSUSED */ 2197 static int 2198 udp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 2199 cred_t *cr) 2200 { 2201 long new_value; 2202 int i; 2203 2204 /* 2205 * Fail the request if the new value does not lie within the 2206 * port number limits. 2207 */ 2208 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 2209 new_value <= 0 || new_value >= 65536) { 2210 return (EINVAL); 2211 } 2212 2213 /* Check that the value is already in the list */ 2214 for (i = 0; i < udp_g_num_epriv_ports; i++) { 2215 if (udp_g_epriv_ports[i] == new_value) 2216 break; 2217 } 2218 if (i == udp_g_num_epriv_ports) { 2219 return (ESRCH); 2220 } 2221 2222 /* Clear the value */ 2223 udp_g_epriv_ports[i] = 0; 2224 return (0); 2225 } 2226 2227 /* At minimum we need 4 bytes of UDP header */ 2228 #define ICMP_MIN_UDP_HDR 4 2229 2230 /* 2231 * udp_icmp_error is called by udp_rput to process ICMP msgs. passed up by IP. 2232 * Generates the appropriate T_UDERROR_IND for permanent (non-transient) errors. 2233 * Assumes that IP has pulled up everything up to and including the ICMP header. 2234 * An M_CTL could potentially come here from some other module (i.e. if UDP 2235 * is pushed on some module other than IP). Thus, if we find that the M_CTL 2236 * does not have enough ICMP information , following STREAMS conventions, 2237 * we send it upstream assuming it is an M_CTL we don't understand. 2238 */ 2239 static void 2240 udp_icmp_error(queue_t *q, mblk_t *mp) 2241 { 2242 icmph_t *icmph; 2243 ipha_t *ipha; 2244 int iph_hdr_length; 2245 udpha_t *udpha; 2246 sin_t sin; 2247 sin6_t sin6; 2248 mblk_t *mp1; 2249 int error = 0; 2250 size_t mp_size = MBLKL(mp); 2251 udp_t *udp = Q_TO_UDP(q); 2252 2253 /* 2254 * Assume IP provides aligned packets - otherwise toss 2255 */ 2256 if (!OK_32PTR(mp->b_rptr)) { 2257 freemsg(mp); 2258 return; 2259 } 2260 2261 /* 2262 * Verify that we have a complete IP header and the application has 2263 * asked for errors. If not, send it upstream. 2264 */ 2265 if (!udp->udp_dgram_errind || mp_size < sizeof (ipha_t)) { 2266 noticmpv4: 2267 putnext(UDP_RD(q), mp); 2268 return; 2269 } 2270 2271 ipha = (ipha_t *)mp->b_rptr; 2272 /* 2273 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 2274 * upstream. ICMPv6 is handled in udp_icmp_error_ipv6. 2275 */ 2276 switch (IPH_HDR_VERSION(ipha)) { 2277 case IPV6_VERSION: 2278 udp_icmp_error_ipv6(q, mp); 2279 return; 2280 case IPV4_VERSION: 2281 break; 2282 default: 2283 goto noticmpv4; 2284 } 2285 2286 /* Skip past the outer IP and ICMP headers */ 2287 iph_hdr_length = IPH_HDR_LENGTH(ipha); 2288 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2289 /* 2290 * If we don't have the correct outer IP header length or if the ULP 2291 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 2292 * send the packet upstream. 2293 */ 2294 if (iph_hdr_length < sizeof (ipha_t) || 2295 ipha->ipha_protocol != IPPROTO_ICMP || 2296 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 2297 goto noticmpv4; 2298 } 2299 ipha = (ipha_t *)&icmph[1]; 2300 2301 /* Skip past the inner IP and find the ULP header */ 2302 iph_hdr_length = IPH_HDR_LENGTH(ipha); 2303 udpha = (udpha_t *)((char *)ipha + iph_hdr_length); 2304 /* 2305 * If we don't have the correct inner IP header length or if the ULP 2306 * is not IPPROTO_UDP or if we don't have at least ICMP_MIN_UDP_HDR 2307 * bytes of UDP header, send it upstream. 2308 */ 2309 if (iph_hdr_length < sizeof (ipha_t) || 2310 ipha->ipha_protocol != IPPROTO_UDP || 2311 (uchar_t *)udpha + ICMP_MIN_UDP_HDR > mp->b_wptr) { 2312 goto noticmpv4; 2313 } 2314 2315 switch (icmph->icmph_type) { 2316 case ICMP_DEST_UNREACHABLE: 2317 switch (icmph->icmph_code) { 2318 case ICMP_FRAGMENTATION_NEEDED: 2319 /* 2320 * IP has already adjusted the path MTU. 2321 * XXX Somehow pass MTU indication to application? 2322 */ 2323 break; 2324 case ICMP_PORT_UNREACHABLE: 2325 case ICMP_PROTOCOL_UNREACHABLE: 2326 error = ECONNREFUSED; 2327 break; 2328 default: 2329 /* Transient errors */ 2330 break; 2331 } 2332 break; 2333 default: 2334 /* Transient errors */ 2335 break; 2336 } 2337 if (error == 0) { 2338 freemsg(mp); 2339 return; 2340 } 2341 2342 switch (udp->udp_family) { 2343 case AF_INET: 2344 sin = sin_null; 2345 sin.sin_family = AF_INET; 2346 sin.sin_addr.s_addr = ipha->ipha_dst; 2347 sin.sin_port = udpha->uha_dst_port; 2348 mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0, 2349 error); 2350 break; 2351 case AF_INET6: 2352 sin6 = sin6_null; 2353 sin6.sin6_family = AF_INET6; 2354 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr); 2355 sin6.sin6_port = udpha->uha_dst_port; 2356 2357 mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), 2358 NULL, 0, error); 2359 break; 2360 } 2361 if (mp1) 2362 putnext(UDP_RD(q), mp1); 2363 freemsg(mp); 2364 } 2365 2366 /* 2367 * udp_icmp_error_ipv6 is called by udp_icmp_error to process ICMP for IPv6. 2368 * Generates the appropriate T_UDERROR_IND for permanent (non-transient) errors. 2369 * Assumes that IP has pulled up all the extension headers as well as the 2370 * ICMPv6 header. 2371 * An M_CTL could potentially come here from some other module (i.e. if UDP 2372 * is pushed on some module other than IP). Thus, if we find that the M_CTL 2373 * does not have enough ICMP information , following STREAMS conventions, 2374 * we send it upstream assuming it is an M_CTL we don't understand. The reason 2375 * it might get here is if the non-ICMP M_CTL accidently has 6 in the version 2376 * field (when cast to ipha_t in udp_icmp_error). 2377 */ 2378 static void 2379 udp_icmp_error_ipv6(queue_t *q, mblk_t *mp) 2380 { 2381 icmp6_t *icmp6; 2382 ip6_t *ip6h, *outer_ip6h; 2383 uint16_t hdr_length; 2384 uint8_t *nexthdrp; 2385 udpha_t *udpha; 2386 sin6_t sin6; 2387 mblk_t *mp1; 2388 int error = 0; 2389 size_t mp_size = MBLKL(mp); 2390 udp_t *udp = Q_TO_UDP(q); 2391 2392 /* 2393 * Verify that we have a complete IP header. If not, send it upstream. 2394 */ 2395 if (mp_size < sizeof (ip6_t)) { 2396 noticmpv6: 2397 putnext(UDP_RD(q), mp); 2398 return; 2399 } 2400 2401 outer_ip6h = (ip6_t *)mp->b_rptr; 2402 /* 2403 * Verify this is an ICMPV6 packet, else send it upstream 2404 */ 2405 if (outer_ip6h->ip6_nxt == IPPROTO_ICMPV6) { 2406 hdr_length = IPV6_HDR_LEN; 2407 } else if (!ip_hdr_length_nexthdr_v6(mp, outer_ip6h, &hdr_length, 2408 &nexthdrp) || 2409 *nexthdrp != IPPROTO_ICMPV6) { 2410 goto noticmpv6; 2411 } 2412 icmp6 = (icmp6_t *)&mp->b_rptr[hdr_length]; 2413 ip6h = (ip6_t *)&icmp6[1]; 2414 /* 2415 * Verify we have a complete ICMP and inner IP header. 2416 */ 2417 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 2418 goto noticmpv6; 2419 2420 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp)) 2421 goto noticmpv6; 2422 udpha = (udpha_t *)((char *)ip6h + hdr_length); 2423 /* 2424 * Validate inner header. If the ULP is not IPPROTO_UDP or if we don't 2425 * have at least ICMP_MIN_UDP_HDR bytes of UDP header send the 2426 * packet upstream. 2427 */ 2428 if ((*nexthdrp != IPPROTO_UDP) || 2429 ((uchar_t *)udpha + ICMP_MIN_UDP_HDR) > mp->b_wptr) { 2430 goto noticmpv6; 2431 } 2432 2433 switch (icmp6->icmp6_type) { 2434 case ICMP6_DST_UNREACH: 2435 switch (icmp6->icmp6_code) { 2436 case ICMP6_DST_UNREACH_NOPORT: 2437 error = ECONNREFUSED; 2438 break; 2439 case ICMP6_DST_UNREACH_ADMIN: 2440 case ICMP6_DST_UNREACH_NOROUTE: 2441 case ICMP6_DST_UNREACH_BEYONDSCOPE: 2442 case ICMP6_DST_UNREACH_ADDR: 2443 /* Transient errors */ 2444 break; 2445 default: 2446 break; 2447 } 2448 break; 2449 case ICMP6_PACKET_TOO_BIG: { 2450 struct T_unitdata_ind *tudi; 2451 struct T_opthdr *toh; 2452 size_t udi_size; 2453 mblk_t *newmp; 2454 t_scalar_t opt_length = sizeof (struct T_opthdr) + 2455 sizeof (struct ip6_mtuinfo); 2456 sin6_t *sin6; 2457 struct ip6_mtuinfo *mtuinfo; 2458 2459 /* 2460 * If the application has requested to receive path mtu 2461 * information, send up an empty message containing an 2462 * IPV6_PATHMTU ancillary data item. 2463 */ 2464 if (!udp->udp_ipv6_recvpathmtu) 2465 break; 2466 2467 udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) + 2468 opt_length; 2469 if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) { 2470 BUMP_MIB(&udp_mib, udpInErrors); 2471 break; 2472 } 2473 2474 /* 2475 * newmp->b_cont is left to NULL on purpose. This is an 2476 * empty message containing only ancillary data. 2477 */ 2478 newmp->b_datap->db_type = M_PROTO; 2479 tudi = (struct T_unitdata_ind *)newmp->b_rptr; 2480 newmp->b_wptr = (uchar_t *)tudi + udi_size; 2481 tudi->PRIM_type = T_UNITDATA_IND; 2482 tudi->SRC_length = sizeof (sin6_t); 2483 tudi->SRC_offset = sizeof (struct T_unitdata_ind); 2484 tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t); 2485 tudi->OPT_length = opt_length; 2486 2487 sin6 = (sin6_t *)&tudi[1]; 2488 bzero(sin6, sizeof (sin6_t)); 2489 sin6->sin6_family = AF_INET6; 2490 sin6->sin6_addr = udp->udp_v6dst; 2491 2492 toh = (struct T_opthdr *)&sin6[1]; 2493 toh->level = IPPROTO_IPV6; 2494 toh->name = IPV6_PATHMTU; 2495 toh->len = opt_length; 2496 toh->status = 0; 2497 2498 mtuinfo = (struct ip6_mtuinfo *)&toh[1]; 2499 bzero(mtuinfo, sizeof (struct ip6_mtuinfo)); 2500 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 2501 mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst; 2502 mtuinfo->ip6m_mtu = icmp6->icmp6_mtu; 2503 /* 2504 * We've consumed everything we need from the original 2505 * message. Free it, then send our empty message. 2506 */ 2507 freemsg(mp); 2508 putnext(UDP_RD(q), newmp); 2509 return; 2510 } 2511 case ICMP6_TIME_EXCEEDED: 2512 /* Transient errors */ 2513 break; 2514 case ICMP6_PARAM_PROB: 2515 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 2516 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 2517 (uchar_t *)ip6h + icmp6->icmp6_pptr == 2518 (uchar_t *)nexthdrp) { 2519 error = ECONNREFUSED; 2520 break; 2521 } 2522 break; 2523 } 2524 if (error == 0) { 2525 freemsg(mp); 2526 return; 2527 } 2528 2529 sin6 = sin6_null; 2530 sin6.sin6_family = AF_INET6; 2531 sin6.sin6_addr = ip6h->ip6_dst; 2532 sin6.sin6_port = udpha->uha_dst_port; 2533 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 2534 2535 mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), NULL, 0, 2536 error); 2537 if (mp1) 2538 putnext(UDP_RD(q), mp1); 2539 freemsg(mp); 2540 } 2541 2542 /* 2543 * This routine responds to T_ADDR_REQ messages. It is called by udp_wput. 2544 * The local address is filled in if endpoint is bound. The remote address 2545 * is filled in if remote address has been precified ("connected endpoint") 2546 * (The concept of connected CLTS sockets is alien to published TPI 2547 * but we support it anyway). 2548 */ 2549 static void 2550 udp_addr_req(queue_t *q, mblk_t *mp) 2551 { 2552 sin_t *sin; 2553 sin6_t *sin6; 2554 mblk_t *ackmp; 2555 struct T_addr_ack *taa; 2556 udp_t *udp = Q_TO_UDP(q); 2557 2558 /* Make it large enough for worst case */ 2559 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 2560 2 * sizeof (sin6_t), 1); 2561 if (ackmp == NULL) { 2562 udp_err_ack(q, mp, TSYSERR, ENOMEM); 2563 return; 2564 } 2565 taa = (struct T_addr_ack *)ackmp->b_rptr; 2566 2567 bzero(taa, sizeof (struct T_addr_ack)); 2568 ackmp->b_wptr = (uchar_t *)&taa[1]; 2569 2570 taa->PRIM_type = T_ADDR_ACK; 2571 ackmp->b_datap->db_type = M_PCPROTO; 2572 /* 2573 * Note: Following code assumes 32 bit alignment of basic 2574 * data structures like sin_t and struct T_addr_ack. 2575 */ 2576 if (udp->udp_state != TS_UNBND) { 2577 /* 2578 * Fill in local address first 2579 */ 2580 taa->LOCADDR_offset = sizeof (*taa); 2581 if (udp->udp_family == AF_INET) { 2582 taa->LOCADDR_length = sizeof (sin_t); 2583 sin = (sin_t *)&taa[1]; 2584 /* Fill zeroes and then initialize non-zero fields */ 2585 *sin = sin_null; 2586 sin->sin_family = AF_INET; 2587 if (!IN6_IS_ADDR_V4MAPPED_ANY(&udp->udp_v6src) && 2588 !IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src)) { 2589 IN6_V4MAPPED_TO_IPADDR(&udp->udp_v6src, 2590 sin->sin_addr.s_addr); 2591 } else { 2592 /* 2593 * INADDR_ANY 2594 * udp_v6src is not set, we might be bound to 2595 * broadcast/multicast. Use udp_bound_v6src as 2596 * local address instead (that could 2597 * also still be INADDR_ANY) 2598 */ 2599 IN6_V4MAPPED_TO_IPADDR(&udp->udp_bound_v6src, 2600 sin->sin_addr.s_addr); 2601 } 2602 sin->sin_port = udp->udp_port; 2603 ackmp->b_wptr = (uchar_t *)&sin[1]; 2604 if (udp->udp_state == TS_DATA_XFER) { 2605 /* 2606 * connected, fill remote address too 2607 */ 2608 taa->REMADDR_length = sizeof (sin_t); 2609 /* assumed 32-bit alignment */ 2610 taa->REMADDR_offset = taa->LOCADDR_offset + 2611 taa->LOCADDR_length; 2612 2613 sin = (sin_t *)(ackmp->b_rptr + 2614 taa->REMADDR_offset); 2615 /* initialize */ 2616 *sin = sin_null; 2617 sin->sin_family = AF_INET; 2618 sin->sin_addr.s_addr = 2619 V4_PART_OF_V6(udp->udp_v6dst); 2620 sin->sin_port = udp->udp_dstport; 2621 ackmp->b_wptr = (uchar_t *)&sin[1]; 2622 } 2623 } else { 2624 taa->LOCADDR_length = sizeof (sin6_t); 2625 sin6 = (sin6_t *)&taa[1]; 2626 /* Fill zeroes and then initialize non-zero fields */ 2627 *sin6 = sin6_null; 2628 sin6->sin6_family = AF_INET6; 2629 if (!IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src)) { 2630 sin6->sin6_addr = udp->udp_v6src; 2631 } else { 2632 /* 2633 * UNSPECIFIED 2634 * udp_v6src is not set, we might be bound to 2635 * broadcast/multicast. Use udp_bound_v6src as 2636 * local address instead (that could 2637 * also still be UNSPECIFIED) 2638 */ 2639 sin6->sin6_addr = 2640 udp->udp_bound_v6src; 2641 } 2642 sin6->sin6_port = udp->udp_port; 2643 ackmp->b_wptr = (uchar_t *)&sin6[1]; 2644 if (udp->udp_state == TS_DATA_XFER) { 2645 /* 2646 * connected, fill remote address too 2647 */ 2648 taa->REMADDR_length = sizeof (sin6_t); 2649 /* assumed 32-bit alignment */ 2650 taa->REMADDR_offset = taa->LOCADDR_offset + 2651 taa->LOCADDR_length; 2652 2653 sin6 = (sin6_t *)(ackmp->b_rptr + 2654 taa->REMADDR_offset); 2655 /* initialize */ 2656 *sin6 = sin6_null; 2657 sin6->sin6_family = AF_INET6; 2658 sin6->sin6_addr = udp->udp_v6dst; 2659 sin6->sin6_port = udp->udp_dstport; 2660 ackmp->b_wptr = (uchar_t *)&sin6[1]; 2661 } 2662 ackmp->b_wptr = (uchar_t *)&sin6[1]; 2663 } 2664 } 2665 ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim); 2666 putnext(UDP_RD(q), ackmp); 2667 } 2668 2669 static void 2670 udp_copy_info(struct T_info_ack *tap, udp_t *udp) 2671 { 2672 if (udp->udp_family == AF_INET) { 2673 *tap = udp_g_t_info_ack_ipv4; 2674 } else { 2675 *tap = udp_g_t_info_ack_ipv6; 2676 } 2677 tap->CURRENT_state = udp->udp_state; 2678 tap->OPT_size = udp_max_optsize; 2679 } 2680 2681 /* 2682 * This routine responds to T_CAPABILITY_REQ messages. It is called by 2683 * udp_wput. Much of the T_CAPABILITY_ACK information is copied from 2684 * udp_g_t_info_ack. The current state of the stream is copied from 2685 * udp_state. 2686 */ 2687 static void 2688 udp_capability_req(queue_t *q, mblk_t *mp) 2689 { 2690 t_uscalar_t cap_bits1; 2691 struct T_capability_ack *tcap; 2692 udp_t *udp = Q_TO_UDP(q); 2693 2694 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 2695 2696 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 2697 mp->b_datap->db_type, T_CAPABILITY_ACK); 2698 if (!mp) 2699 return; 2700 2701 tcap = (struct T_capability_ack *)mp->b_rptr; 2702 tcap->CAP_bits1 = 0; 2703 2704 if (cap_bits1 & TC1_INFO) { 2705 udp_copy_info(&tcap->INFO_ack, udp); 2706 tcap->CAP_bits1 |= TC1_INFO; 2707 } 2708 2709 putnext(UDP_RD(q), mp); 2710 } 2711 2712 /* 2713 * This routine responds to T_INFO_REQ messages. It is called by udp_wput. 2714 * Most of the T_INFO_ACK information is copied from udp_g_t_info_ack. 2715 * The current state of the stream is copied from udp_state. 2716 */ 2717 static void 2718 udp_info_req(queue_t *q, mblk_t *mp) 2719 { 2720 udp_t *udp = Q_TO_UDP(q); 2721 2722 /* Create a T_INFO_ACK message. */ 2723 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 2724 T_INFO_ACK); 2725 if (!mp) 2726 return; 2727 udp_copy_info((struct T_info_ack *)mp->b_rptr, udp); 2728 putnext(UDP_RD(q), mp); 2729 } 2730 2731 /* 2732 * IP recognizes seven kinds of bind requests: 2733 * 2734 * - A zero-length address binds only to the protocol number. 2735 * 2736 * - A 4-byte address is treated as a request to 2737 * validate that the address is a valid local IPv4 2738 * address, appropriate for an application to bind to. 2739 * IP does the verification, but does not make any note 2740 * of the address at this time. 2741 * 2742 * - A 16-byte address contains is treated as a request 2743 * to validate a local IPv6 address, as the 4-byte 2744 * address case above. 2745 * 2746 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 2747 * use it for the inbound fanout of packets. 2748 * 2749 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 2750 * use it for the inbound fanout of packets. 2751 * 2752 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 2753 * information consisting of local and remote addresses 2754 * and ports. In this case, the addresses are both 2755 * validated as appropriate for this operation, and, if 2756 * so, the information is retained for use in the 2757 * inbound fanout. 2758 * 2759 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 2760 * fanout information, like the 12-byte case above. 2761 * 2762 * IP will also fill in the IRE request mblk with information 2763 * regarding our peer. In all cases, we notify IP of our protocol 2764 * type by appending a single protocol byte to the bind request. 2765 */ 2766 static mblk_t * 2767 udp_ip_bind_mp(udp_t *udp, t_scalar_t bind_prim, t_scalar_t addr_length) 2768 { 2769 char *cp; 2770 mblk_t *mp; 2771 struct T_bind_req *tbr; 2772 ipa_conn_t *ac; 2773 ipa6_conn_t *ac6; 2774 sin_t *sin; 2775 sin6_t *sin6; 2776 2777 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 2778 2779 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 2780 if (!mp) 2781 return (mp); 2782 mp->b_datap->db_type = M_PROTO; 2783 tbr = (struct T_bind_req *)mp->b_rptr; 2784 tbr->PRIM_type = bind_prim; 2785 tbr->ADDR_offset = sizeof (*tbr); 2786 tbr->CONIND_number = 0; 2787 tbr->ADDR_length = addr_length; 2788 cp = (char *)&tbr[1]; 2789 switch (addr_length) { 2790 case sizeof (ipa_conn_t): 2791 ASSERT(udp->udp_family == AF_INET); 2792 /* Append a request for an IRE */ 2793 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 2794 if (!mp->b_cont) { 2795 freemsg(mp); 2796 return (NULL); 2797 } 2798 mp->b_cont->b_wptr += sizeof (ire_t); 2799 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 2800 2801 /* cp known to be 32 bit aligned */ 2802 ac = (ipa_conn_t *)cp; 2803 ac->ac_laddr = V4_PART_OF_V6(udp->udp_v6src); 2804 ac->ac_faddr = V4_PART_OF_V6(udp->udp_v6dst); 2805 ac->ac_fport = udp->udp_dstport; 2806 ac->ac_lport = udp->udp_port; 2807 break; 2808 2809 case sizeof (ipa6_conn_t): 2810 ASSERT(udp->udp_family == AF_INET6); 2811 /* Append a request for an IRE */ 2812 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 2813 if (!mp->b_cont) { 2814 freemsg(mp); 2815 return (NULL); 2816 } 2817 mp->b_cont->b_wptr += sizeof (ire_t); 2818 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 2819 2820 /* cp known to be 32 bit aligned */ 2821 ac6 = (ipa6_conn_t *)cp; 2822 ac6->ac6_laddr = udp->udp_v6src; 2823 ac6->ac6_faddr = udp->udp_v6dst; 2824 ac6->ac6_fport = udp->udp_dstport; 2825 ac6->ac6_lport = udp->udp_port; 2826 break; 2827 2828 case sizeof (sin_t): 2829 ASSERT(udp->udp_family == AF_INET); 2830 /* Append a request for an IRE */ 2831 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 2832 if (!mp->b_cont) { 2833 freemsg(mp); 2834 return (NULL); 2835 } 2836 mp->b_cont->b_wptr += sizeof (ire_t); 2837 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 2838 2839 sin = (sin_t *)cp; 2840 *sin = sin_null; 2841 sin->sin_family = AF_INET; 2842 sin->sin_addr.s_addr = V4_PART_OF_V6(udp->udp_bound_v6src); 2843 sin->sin_port = udp->udp_port; 2844 break; 2845 2846 case sizeof (sin6_t): 2847 ASSERT(udp->udp_family == AF_INET6); 2848 /* Append a request for an IRE */ 2849 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 2850 if (!mp->b_cont) { 2851 freemsg(mp); 2852 return (NULL); 2853 } 2854 mp->b_cont->b_wptr += sizeof (ire_t); 2855 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 2856 2857 sin6 = (sin6_t *)cp; 2858 *sin6 = sin6_null; 2859 sin6->sin6_family = AF_INET6; 2860 sin6->sin6_addr = udp->udp_bound_v6src; 2861 sin6->sin6_port = udp->udp_port; 2862 break; 2863 } 2864 /* Add protocol number to end */ 2865 cp[addr_length] = (char)IPPROTO_UDP; 2866 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 2867 return (mp); 2868 } 2869 2870 /* 2871 * This is the open routine for udp. It allocates a udp_t structure for 2872 * the stream and, on the first open of the module, creates an ND table. 2873 */ 2874 /* ARGSUSED */ 2875 static int 2876 udp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 2877 { 2878 int err; 2879 udp_t *udp; 2880 conn_t *connp; 2881 zoneid_t zoneid = getzoneid(); 2882 queue_t *ip_wq; 2883 char *name; 2884 2885 TRACE_1(TR_FAC_UDP, TR_UDP_OPEN, "udp_open: q %p", q); 2886 2887 /* If the stream is already open, return immediately. */ 2888 if (q->q_ptr != NULL) 2889 return (0); 2890 2891 /* If this is not a push of udp as a module, fail. */ 2892 if (sflag != MODOPEN) 2893 return (EINVAL); 2894 2895 q->q_hiwat = udp_recv_hiwat; 2896 WR(q)->q_hiwat = udp_xmit_hiwat; 2897 WR(q)->q_lowat = udp_xmit_lowat; 2898 2899 /* Insert ourselves in the stream since we're about to walk q_next */ 2900 qprocson(q); 2901 2902 udp = kmem_cache_alloc(udp_cache, KM_SLEEP); 2903 bzero(udp, sizeof (*udp)); 2904 2905 /* 2906 * UDP is supported only as a module and it has to be pushed directly 2907 * above the device instance of IP. If UDP is pushed anywhere else 2908 * on a stream, it will support just T_SVR4_OPTMGMT_REQ for the 2909 * sake of MIB browsers and fail everything else. 2910 */ 2911 ip_wq = WR(q)->q_next; 2912 if (ip_wq->q_next != NULL || 2913 (name = ip_wq->q_qinfo->qi_minfo->mi_idname) == NULL || 2914 strcmp(name, IP_MOD_NAME) != 0 || 2915 ip_wq->q_qinfo->qi_minfo->mi_idnum != IP_MOD_ID) { 2916 /* Support just SNMP for MIB browsers */ 2917 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP); 2918 connp->conn_rq = q; 2919 connp->conn_wq = WR(q); 2920 connp->conn_flags |= IPCL_UDPMOD; 2921 connp->conn_cred = credp; 2922 connp->conn_zoneid = zoneid; 2923 connp->conn_udp = udp; 2924 udp->udp_connp = connp; 2925 q->q_ptr = WR(q)->q_ptr = connp; 2926 crhold(credp); 2927 q->q_qinfo = &udp_snmp_rinit; 2928 WR(q)->q_qinfo = &udp_snmp_winit; 2929 return (0); 2930 } 2931 2932 /* 2933 * Initialize the udp_t structure for this stream. 2934 */ 2935 q = RD(ip_wq); 2936 connp = Q_TO_CONN(q); 2937 mutex_enter(&connp->conn_lock); 2938 connp->conn_proto = IPPROTO_UDP; 2939 connp->conn_flags |= IPCL_UDP; 2940 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 2941 connp->conn_udp = udp; 2942 2943 /* Set the initial state of the stream and the privilege status. */ 2944 udp->udp_connp = connp; 2945 udp->udp_state = TS_UNBND; 2946 udp->udp_mode = UDP_MT_HOT; 2947 if (getmajor(*devp) == (major_t)UDP6_MAJ) { 2948 udp->udp_family = AF_INET6; 2949 udp->udp_ipversion = IPV6_VERSION; 2950 udp->udp_max_hdr_len = IPV6_HDR_LEN + UDPH_SIZE; 2951 udp->udp_ttl = udp_ipv6_hoplimit; 2952 connp->conn_af_isv6 = B_TRUE; 2953 connp->conn_flags |= IPCL_ISV6; 2954 } else { 2955 udp->udp_family = AF_INET; 2956 udp->udp_ipversion = IPV4_VERSION; 2957 udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + UDPH_SIZE; 2958 udp->udp_ttl = udp_ipv4_ttl; 2959 connp->conn_af_isv6 = B_FALSE; 2960 connp->conn_flags &= ~IPCL_ISV6; 2961 } 2962 2963 udp->udp_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 2964 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 2965 connp->conn_zoneid = zoneid; 2966 2967 /* 2968 * If the caller has the process-wide flag set, then default to MAC 2969 * exempt mode. This allows read-down to unlabeled hosts. 2970 */ 2971 if (getpflags(NET_MAC_AWARE, credp) != 0) 2972 udp->udp_mac_exempt = B_TRUE; 2973 2974 if (connp->conn_flags & IPCL_SOCKET) { 2975 udp->udp_issocket = B_TRUE; 2976 udp->udp_direct_sockfs = B_TRUE; 2977 } 2978 2979 connp->conn_ulp_labeled = is_system_labeled(); 2980 2981 mutex_exit(&connp->conn_lock); 2982 2983 /* 2984 * The transmit hiwat/lowat is only looked at on IP's queue. 2985 * Store in q_hiwat in order to return on SO_SNDBUF/SO_RCVBUF 2986 * getsockopts. 2987 */ 2988 q->q_hiwat = udp_recv_hiwat; 2989 WR(q)->q_hiwat = udp_xmit_hiwat; 2990 WR(q)->q_lowat = udp_xmit_lowat; 2991 2992 if (udp->udp_family == AF_INET6) { 2993 /* Build initial header template for transmit */ 2994 if ((err = udp_build_hdrs(q, udp)) != 0) { 2995 error: 2996 qprocsoff(UDP_RD(q)); 2997 udp->udp_connp = NULL; 2998 connp->conn_udp = NULL; 2999 kmem_cache_free(udp_cache, udp); 3000 return (err); 3001 } 3002 } 3003 3004 /* Set the Stream head write offset and high watermark. */ 3005 (void) mi_set_sth_wroff(UDP_RD(q), 3006 udp->udp_max_hdr_len + udp_wroff_extra); 3007 (void) mi_set_sth_hiwat(UDP_RD(q), udp_set_rcv_hiwat(udp, q->q_hiwat)); 3008 3009 WR(UDP_RD(q))->q_qinfo = &udp_winit; 3010 3011 return (0); 3012 } 3013 3014 /* 3015 * Which UDP options OK to set through T_UNITDATA_REQ... 3016 */ 3017 /* ARGSUSED */ 3018 static boolean_t 3019 udp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name) 3020 { 3021 return (B_TRUE); 3022 } 3023 3024 /* 3025 * This routine gets default values of certain options whose default 3026 * values are maintained by protcol specific code 3027 */ 3028 /* ARGSUSED */ 3029 int 3030 udp_opt_default(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr) 3031 { 3032 int *i1 = (int *)ptr; 3033 3034 switch (level) { 3035 case IPPROTO_IP: 3036 switch (name) { 3037 case IP_MULTICAST_TTL: 3038 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 3039 return (sizeof (uchar_t)); 3040 case IP_MULTICAST_LOOP: 3041 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 3042 return (sizeof (uchar_t)); 3043 } 3044 break; 3045 case IPPROTO_IPV6: 3046 switch (name) { 3047 case IPV6_MULTICAST_HOPS: 3048 *i1 = IP_DEFAULT_MULTICAST_TTL; 3049 return (sizeof (int)); 3050 case IPV6_MULTICAST_LOOP: 3051 *i1 = IP_DEFAULT_MULTICAST_LOOP; 3052 return (sizeof (int)); 3053 case IPV6_UNICAST_HOPS: 3054 *i1 = udp_ipv6_hoplimit; 3055 return (sizeof (int)); 3056 } 3057 break; 3058 } 3059 return (-1); 3060 } 3061 3062 /* 3063 * This routine retrieves the current status of socket options 3064 * and expects the caller to pass in the queue pointer of the 3065 * upper instance. It returns the size of the option retrieved. 3066 */ 3067 int 3068 udp_opt_get(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr) 3069 { 3070 int *i1 = (int *)ptr; 3071 conn_t *connp; 3072 udp_t *udp; 3073 ip6_pkt_t *ipp; 3074 int len; 3075 3076 q = UDP_WR(q); 3077 connp = Q_TO_CONN(q); 3078 udp = connp->conn_udp; 3079 ipp = &udp->udp_sticky_ipp; 3080 3081 switch (level) { 3082 case SOL_SOCKET: 3083 switch (name) { 3084 case SO_DEBUG: 3085 *i1 = udp->udp_debug; 3086 break; /* goto sizeof (int) option return */ 3087 case SO_REUSEADDR: 3088 *i1 = udp->udp_reuseaddr; 3089 break; /* goto sizeof (int) option return */ 3090 case SO_TYPE: 3091 *i1 = SOCK_DGRAM; 3092 break; /* goto sizeof (int) option return */ 3093 3094 /* 3095 * The following three items are available here, 3096 * but are only meaningful to IP. 3097 */ 3098 case SO_DONTROUTE: 3099 *i1 = udp->udp_dontroute; 3100 break; /* goto sizeof (int) option return */ 3101 case SO_USELOOPBACK: 3102 *i1 = udp->udp_useloopback; 3103 break; /* goto sizeof (int) option return */ 3104 case SO_BROADCAST: 3105 *i1 = udp->udp_broadcast; 3106 break; /* goto sizeof (int) option return */ 3107 3108 case SO_SNDBUF: 3109 *i1 = q->q_hiwat; 3110 break; /* goto sizeof (int) option return */ 3111 case SO_RCVBUF: 3112 *i1 = RD(q)->q_hiwat; 3113 break; /* goto sizeof (int) option return */ 3114 case SO_DGRAM_ERRIND: 3115 *i1 = udp->udp_dgram_errind; 3116 break; /* goto sizeof (int) option return */ 3117 case SO_RECVUCRED: 3118 *i1 = udp->udp_recvucred; 3119 break; /* goto sizeof (int) option return */ 3120 case SO_TIMESTAMP: 3121 *i1 = udp->udp_timestamp; 3122 break; /* goto sizeof (int) option return */ 3123 case SO_ANON_MLP: 3124 *i1 = udp->udp_anon_mlp; 3125 break; /* goto sizeof (int) option return */ 3126 case SO_MAC_EXEMPT: 3127 *i1 = udp->udp_mac_exempt; 3128 break; /* goto sizeof (int) option return */ 3129 case SO_ALLZONES: 3130 *i1 = connp->conn_allzones; 3131 break; /* goto sizeof (int) option return */ 3132 default: 3133 return (-1); 3134 } 3135 break; 3136 case IPPROTO_IP: 3137 if (udp->udp_family != AF_INET) 3138 return (-1); 3139 switch (name) { 3140 case IP_OPTIONS: 3141 case T_IP_OPTIONS: 3142 len = udp->udp_ip_rcv_options_len - udp->udp_label_len; 3143 if (len > 0) { 3144 bcopy(udp->udp_ip_rcv_options + 3145 udp->udp_label_len, ptr, len); 3146 } 3147 return (len); 3148 case IP_TOS: 3149 case T_IP_TOS: 3150 *i1 = (int)udp->udp_type_of_service; 3151 break; /* goto sizeof (int) option return */ 3152 case IP_TTL: 3153 *i1 = (int)udp->udp_ttl; 3154 break; /* goto sizeof (int) option return */ 3155 case IP_NEXTHOP: 3156 /* Handled at IP level */ 3157 return (-EINVAL); 3158 case IP_MULTICAST_IF: 3159 /* 0 address if not set */ 3160 *(ipaddr_t *)ptr = udp->udp_multicast_if_addr; 3161 return (sizeof (ipaddr_t)); 3162 case IP_MULTICAST_TTL: 3163 *(uchar_t *)ptr = udp->udp_multicast_ttl; 3164 return (sizeof (uchar_t)); 3165 case IP_MULTICAST_LOOP: 3166 *ptr = connp->conn_multicast_loop; 3167 return (sizeof (uint8_t)); 3168 case IP_RECVOPTS: 3169 *i1 = udp->udp_recvopts; 3170 break; /* goto sizeof (int) option return */ 3171 case IP_RECVDSTADDR: 3172 *i1 = udp->udp_recvdstaddr; 3173 break; /* goto sizeof (int) option return */ 3174 case IP_RECVIF: 3175 *i1 = udp->udp_recvif; 3176 break; /* goto sizeof (int) option return */ 3177 case IP_RECVSLLA: 3178 *i1 = udp->udp_recvslla; 3179 break; /* goto sizeof (int) option return */ 3180 case IP_RECVTTL: 3181 *i1 = udp->udp_recvttl; 3182 break; /* goto sizeof (int) option return */ 3183 case IP_ADD_MEMBERSHIP: 3184 case IP_DROP_MEMBERSHIP: 3185 case IP_BLOCK_SOURCE: 3186 case IP_UNBLOCK_SOURCE: 3187 case IP_ADD_SOURCE_MEMBERSHIP: 3188 case IP_DROP_SOURCE_MEMBERSHIP: 3189 case MCAST_JOIN_GROUP: 3190 case MCAST_LEAVE_GROUP: 3191 case MCAST_BLOCK_SOURCE: 3192 case MCAST_UNBLOCK_SOURCE: 3193 case MCAST_JOIN_SOURCE_GROUP: 3194 case MCAST_LEAVE_SOURCE_GROUP: 3195 case IP_DONTFAILOVER_IF: 3196 /* cannot "get" the value for these */ 3197 return (-1); 3198 case IP_BOUND_IF: 3199 /* Zero if not set */ 3200 *i1 = udp->udp_bound_if; 3201 break; /* goto sizeof (int) option return */ 3202 case IP_UNSPEC_SRC: 3203 *i1 = udp->udp_unspec_source; 3204 break; /* goto sizeof (int) option return */ 3205 case IP_XMIT_IF: 3206 *i1 = udp->udp_xmit_if; 3207 break; /* goto sizeof (int) option return */ 3208 default: 3209 return (-1); 3210 } 3211 break; 3212 case IPPROTO_IPV6: 3213 if (udp->udp_family != AF_INET6) 3214 return (-1); 3215 switch (name) { 3216 case IPV6_UNICAST_HOPS: 3217 *i1 = (unsigned int)udp->udp_ttl; 3218 break; /* goto sizeof (int) option return */ 3219 case IPV6_MULTICAST_IF: 3220 /* 0 index if not set */ 3221 *i1 = udp->udp_multicast_if_index; 3222 break; /* goto sizeof (int) option return */ 3223 case IPV6_MULTICAST_HOPS: 3224 *i1 = udp->udp_multicast_ttl; 3225 break; /* goto sizeof (int) option return */ 3226 case IPV6_MULTICAST_LOOP: 3227 *i1 = connp->conn_multicast_loop; 3228 break; /* goto sizeof (int) option return */ 3229 case IPV6_JOIN_GROUP: 3230 case IPV6_LEAVE_GROUP: 3231 case MCAST_JOIN_GROUP: 3232 case MCAST_LEAVE_GROUP: 3233 case MCAST_BLOCK_SOURCE: 3234 case MCAST_UNBLOCK_SOURCE: 3235 case MCAST_JOIN_SOURCE_GROUP: 3236 case MCAST_LEAVE_SOURCE_GROUP: 3237 /* cannot "get" the value for these */ 3238 return (-1); 3239 case IPV6_BOUND_IF: 3240 /* Zero if not set */ 3241 *i1 = udp->udp_bound_if; 3242 break; /* goto sizeof (int) option return */ 3243 case IPV6_UNSPEC_SRC: 3244 *i1 = udp->udp_unspec_source; 3245 break; /* goto sizeof (int) option return */ 3246 case IPV6_RECVPKTINFO: 3247 *i1 = udp->udp_ipv6_recvpktinfo; 3248 break; /* goto sizeof (int) option return */ 3249 case IPV6_RECVTCLASS: 3250 *i1 = udp->udp_ipv6_recvtclass; 3251 break; /* goto sizeof (int) option return */ 3252 case IPV6_RECVPATHMTU: 3253 *i1 = udp->udp_ipv6_recvpathmtu; 3254 break; /* goto sizeof (int) option return */ 3255 case IPV6_RECVHOPLIMIT: 3256 *i1 = udp->udp_ipv6_recvhoplimit; 3257 break; /* goto sizeof (int) option return */ 3258 case IPV6_RECVHOPOPTS: 3259 *i1 = udp->udp_ipv6_recvhopopts; 3260 break; /* goto sizeof (int) option return */ 3261 case IPV6_RECVDSTOPTS: 3262 *i1 = udp->udp_ipv6_recvdstopts; 3263 break; /* goto sizeof (int) option return */ 3264 case _OLD_IPV6_RECVDSTOPTS: 3265 *i1 = udp->udp_old_ipv6_recvdstopts; 3266 break; /* goto sizeof (int) option return */ 3267 case IPV6_RECVRTHDRDSTOPTS: 3268 *i1 = udp->udp_ipv6_recvrthdrdstopts; 3269 break; /* goto sizeof (int) option return */ 3270 case IPV6_RECVRTHDR: 3271 *i1 = udp->udp_ipv6_recvrthdr; 3272 break; /* goto sizeof (int) option return */ 3273 case IPV6_PKTINFO: { 3274 /* XXX assumes that caller has room for max size! */ 3275 struct in6_pktinfo *pkti; 3276 3277 pkti = (struct in6_pktinfo *)ptr; 3278 if (ipp->ipp_fields & IPPF_IFINDEX) 3279 pkti->ipi6_ifindex = ipp->ipp_ifindex; 3280 else 3281 pkti->ipi6_ifindex = 0; 3282 if (ipp->ipp_fields & IPPF_ADDR) 3283 pkti->ipi6_addr = ipp->ipp_addr; 3284 else 3285 pkti->ipi6_addr = ipv6_all_zeros; 3286 return (sizeof (struct in6_pktinfo)); 3287 } 3288 case IPV6_TCLASS: 3289 if (ipp->ipp_fields & IPPF_TCLASS) 3290 *i1 = ipp->ipp_tclass; 3291 else 3292 *i1 = IPV6_FLOW_TCLASS( 3293 IPV6_DEFAULT_VERS_AND_FLOW); 3294 break; /* goto sizeof (int) option return */ 3295 case IPV6_NEXTHOP: { 3296 sin6_t *sin6 = (sin6_t *)ptr; 3297 3298 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 3299 return (0); 3300 *sin6 = sin6_null; 3301 sin6->sin6_family = AF_INET6; 3302 sin6->sin6_addr = ipp->ipp_nexthop; 3303 return (sizeof (sin6_t)); 3304 } 3305 case IPV6_HOPOPTS: 3306 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 3307 return (0); 3308 if (ipp->ipp_hopoptslen <= udp->udp_label_len_v6) 3309 return (0); 3310 /* 3311 * The cipso/label option is added by kernel. 3312 * User is not usually aware of this option. 3313 * We copy out the hbh opt after the label option. 3314 */ 3315 bcopy((char *)ipp->ipp_hopopts + udp->udp_label_len_v6, 3316 ptr, ipp->ipp_hopoptslen - udp->udp_label_len_v6); 3317 if (udp->udp_label_len_v6 > 0) { 3318 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 3319 ptr[1] = (ipp->ipp_hopoptslen - 3320 udp->udp_label_len_v6 + 7) / 8 - 1; 3321 } 3322 return (ipp->ipp_hopoptslen - udp->udp_label_len_v6); 3323 case IPV6_RTHDRDSTOPTS: 3324 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 3325 return (0); 3326 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 3327 return (ipp->ipp_rtdstoptslen); 3328 case IPV6_RTHDR: 3329 if (!(ipp->ipp_fields & IPPF_RTHDR)) 3330 return (0); 3331 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 3332 return (ipp->ipp_rthdrlen); 3333 case IPV6_DSTOPTS: 3334 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 3335 return (0); 3336 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 3337 return (ipp->ipp_dstoptslen); 3338 case IPV6_PATHMTU: 3339 return (ip_fill_mtuinfo(&udp->udp_v6dst, 3340 udp->udp_dstport, (struct ip6_mtuinfo *)ptr)); 3341 default: 3342 return (-1); 3343 } 3344 break; 3345 case IPPROTO_UDP: 3346 switch (name) { 3347 case UDP_ANONPRIVBIND: 3348 *i1 = udp->udp_anon_priv_bind; 3349 break; 3350 case UDP_EXCLBIND: 3351 *i1 = udp->udp_exclbind ? UDP_EXCLBIND : 0; 3352 break; 3353 case UDP_RCVHDR: 3354 *i1 = udp->udp_rcvhdr ? 1 : 0; 3355 break; 3356 default: 3357 return (-1); 3358 } 3359 break; 3360 default: 3361 return (-1); 3362 } 3363 return (sizeof (int)); 3364 } 3365 3366 /* 3367 * This routine sets socket options; it expects the caller 3368 * to pass in the queue pointer of the upper instance. 3369 */ 3370 /* ARGSUSED */ 3371 int 3372 udp_opt_set(queue_t *q, uint_t optset_context, int level, 3373 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 3374 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 3375 { 3376 udpattrs_t *attrs = thisdg_attrs; 3377 int *i1 = (int *)invalp; 3378 boolean_t onoff = (*i1 == 0) ? 0 : 1; 3379 boolean_t checkonly; 3380 int error; 3381 conn_t *connp; 3382 udp_t *udp; 3383 uint_t newlen; 3384 3385 q = UDP_WR(q); 3386 connp = Q_TO_CONN(q); 3387 udp = connp->conn_udp; 3388 3389 switch (optset_context) { 3390 case SETFN_OPTCOM_CHECKONLY: 3391 checkonly = B_TRUE; 3392 /* 3393 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 3394 * inlen != 0 implies value supplied and 3395 * we have to "pretend" to set it. 3396 * inlen == 0 implies that there is no 3397 * value part in T_CHECK request and just validation 3398 * done elsewhere should be enough, we just return here. 3399 */ 3400 if (inlen == 0) { 3401 *outlenp = 0; 3402 return (0); 3403 } 3404 break; 3405 case SETFN_OPTCOM_NEGOTIATE: 3406 checkonly = B_FALSE; 3407 break; 3408 case SETFN_UD_NEGOTIATE: 3409 case SETFN_CONN_NEGOTIATE: 3410 checkonly = B_FALSE; 3411 /* 3412 * Negotiating local and "association-related" options 3413 * through T_UNITDATA_REQ. 3414 * 3415 * Following routine can filter out ones we do not 3416 * want to be "set" this way. 3417 */ 3418 if (!udp_opt_allow_udr_set(level, name)) { 3419 *outlenp = 0; 3420 return (EINVAL); 3421 } 3422 break; 3423 default: 3424 /* 3425 * We should never get here 3426 */ 3427 *outlenp = 0; 3428 return (EINVAL); 3429 } 3430 3431 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 3432 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 3433 3434 /* 3435 * For fixed length options, no sanity check 3436 * of passed in length is done. It is assumed *_optcom_req() 3437 * routines do the right thing. 3438 */ 3439 3440 switch (level) { 3441 case SOL_SOCKET: 3442 switch (name) { 3443 case SO_REUSEADDR: 3444 if (!checkonly) 3445 udp->udp_reuseaddr = onoff; 3446 break; 3447 case SO_DEBUG: 3448 if (!checkonly) 3449 udp->udp_debug = onoff; 3450 break; 3451 /* 3452 * The following three items are available here, 3453 * but are only meaningful to IP. 3454 */ 3455 case SO_DONTROUTE: 3456 if (!checkonly) 3457 udp->udp_dontroute = onoff; 3458 break; 3459 case SO_USELOOPBACK: 3460 if (!checkonly) 3461 udp->udp_useloopback = onoff; 3462 break; 3463 case SO_BROADCAST: 3464 if (!checkonly) 3465 udp->udp_broadcast = onoff; 3466 break; 3467 3468 case SO_SNDBUF: 3469 if (*i1 > udp_max_buf) { 3470 *outlenp = 0; 3471 return (ENOBUFS); 3472 } 3473 if (!checkonly) { 3474 q->q_hiwat = *i1; 3475 WR(UDP_RD(q))->q_hiwat = *i1; 3476 } 3477 break; 3478 case SO_RCVBUF: 3479 if (*i1 > udp_max_buf) { 3480 *outlenp = 0; 3481 return (ENOBUFS); 3482 } 3483 if (!checkonly) { 3484 RD(q)->q_hiwat = *i1; 3485 UDP_RD(q)->q_hiwat = *i1; 3486 (void) mi_set_sth_hiwat(UDP_RD(q), 3487 udp_set_rcv_hiwat(udp, *i1)); 3488 } 3489 break; 3490 case SO_DGRAM_ERRIND: 3491 if (!checkonly) 3492 udp->udp_dgram_errind = onoff; 3493 break; 3494 case SO_RECVUCRED: 3495 if (!checkonly) 3496 udp->udp_recvucred = onoff; 3497 break; 3498 case SO_ALLZONES: 3499 /* 3500 * "soft" error (negative) 3501 * option not handled at this level 3502 * Do not modify *outlenp. 3503 */ 3504 return (-EINVAL); 3505 case SO_TIMESTAMP: 3506 if (!checkonly) 3507 udp->udp_timestamp = onoff; 3508 break; 3509 case SO_ANON_MLP: 3510 if (!checkonly) 3511 udp->udp_anon_mlp = onoff; 3512 break; 3513 case SO_MAC_EXEMPT: 3514 if (secpolicy_net_mac_aware(cr) != 0 || 3515 udp->udp_state != TS_UNBND) 3516 return (EACCES); 3517 if (!checkonly) 3518 udp->udp_mac_exempt = onoff; 3519 break; 3520 case SCM_UCRED: { 3521 struct ucred_s *ucr; 3522 cred_t *cr, *newcr; 3523 ts_label_t *tsl; 3524 3525 /* 3526 * Only sockets that have proper privileges and are 3527 * bound to MLPs will have any other value here, so 3528 * this implicitly tests for privilege to set label. 3529 */ 3530 if (connp->conn_mlp_type == mlptSingle) 3531 break; 3532 ucr = (struct ucred_s *)invalp; 3533 if (inlen != ucredsize || 3534 ucr->uc_labeloff < sizeof (*ucr) || 3535 ucr->uc_labeloff + sizeof (bslabel_t) > inlen) 3536 return (EINVAL); 3537 if (!checkonly) { 3538 mblk_t *mb; 3539 3540 if (attrs == NULL || 3541 (mb = attrs->udpattr_mb) == NULL) 3542 return (EINVAL); 3543 if ((cr = DB_CRED(mb)) == NULL) 3544 cr = udp->udp_connp->conn_cred; 3545 ASSERT(cr != NULL); 3546 if ((tsl = crgetlabel(cr)) == NULL) 3547 return (EINVAL); 3548 newcr = copycred_from_bslabel(cr, UCLABEL(ucr), 3549 tsl->tsl_doi, KM_NOSLEEP); 3550 if (newcr == NULL) 3551 return (ENOSR); 3552 mblk_setcred(mb, newcr); 3553 attrs->udpattr_credset = B_TRUE; 3554 crfree(newcr); 3555 } 3556 break; 3557 } 3558 default: 3559 *outlenp = 0; 3560 return (EINVAL); 3561 } 3562 break; 3563 case IPPROTO_IP: 3564 if (udp->udp_family != AF_INET) { 3565 *outlenp = 0; 3566 return (ENOPROTOOPT); 3567 } 3568 switch (name) { 3569 case IP_OPTIONS: 3570 case T_IP_OPTIONS: 3571 /* Save options for use by IP. */ 3572 newlen = inlen + udp->udp_label_len; 3573 if ((inlen & 0x3) || newlen > IP_MAX_OPT_LENGTH) { 3574 *outlenp = 0; 3575 return (EINVAL); 3576 } 3577 if (checkonly) 3578 break; 3579 3580 if (!tsol_option_set(&udp->udp_ip_snd_options, 3581 &udp->udp_ip_snd_options_len, 3582 udp->udp_label_len, invalp, inlen)) { 3583 *outlenp = 0; 3584 return (ENOMEM); 3585 } 3586 3587 udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + 3588 UDPH_SIZE + udp->udp_ip_snd_options_len; 3589 (void) mi_set_sth_wroff(RD(q), udp->udp_max_hdr_len + 3590 udp_wroff_extra); 3591 break; 3592 3593 case IP_TTL: 3594 if (!checkonly) { 3595 udp->udp_ttl = (uchar_t)*i1; 3596 } 3597 break; 3598 case IP_TOS: 3599 case T_IP_TOS: 3600 if (!checkonly) { 3601 udp->udp_type_of_service = (uchar_t)*i1; 3602 } 3603 break; 3604 case IP_MULTICAST_IF: { 3605 /* 3606 * TODO should check OPTMGMT reply and undo this if 3607 * there is an error. 3608 */ 3609 struct in_addr *inap = (struct in_addr *)invalp; 3610 if (!checkonly) { 3611 udp->udp_multicast_if_addr = 3612 inap->s_addr; 3613 } 3614 break; 3615 } 3616 case IP_MULTICAST_TTL: 3617 if (!checkonly) 3618 udp->udp_multicast_ttl = *invalp; 3619 break; 3620 case IP_MULTICAST_LOOP: 3621 if (!checkonly) 3622 connp->conn_multicast_loop = *invalp; 3623 break; 3624 case IP_RECVOPTS: 3625 if (!checkonly) 3626 udp->udp_recvopts = onoff; 3627 break; 3628 case IP_RECVDSTADDR: 3629 if (!checkonly) 3630 udp->udp_recvdstaddr = onoff; 3631 break; 3632 case IP_RECVIF: 3633 if (!checkonly) 3634 udp->udp_recvif = onoff; 3635 break; 3636 case IP_RECVSLLA: 3637 if (!checkonly) 3638 udp->udp_recvslla = onoff; 3639 break; 3640 case IP_RECVTTL: 3641 if (!checkonly) 3642 udp->udp_recvttl = onoff; 3643 break; 3644 case IP_ADD_MEMBERSHIP: 3645 case IP_DROP_MEMBERSHIP: 3646 case IP_BLOCK_SOURCE: 3647 case IP_UNBLOCK_SOURCE: 3648 case IP_ADD_SOURCE_MEMBERSHIP: 3649 case IP_DROP_SOURCE_MEMBERSHIP: 3650 case MCAST_JOIN_GROUP: 3651 case MCAST_LEAVE_GROUP: 3652 case MCAST_BLOCK_SOURCE: 3653 case MCAST_UNBLOCK_SOURCE: 3654 case MCAST_JOIN_SOURCE_GROUP: 3655 case MCAST_LEAVE_SOURCE_GROUP: 3656 case IP_SEC_OPT: 3657 case IP_NEXTHOP: 3658 /* 3659 * "soft" error (negative) 3660 * option not handled at this level 3661 * Do not modify *outlenp. 3662 */ 3663 return (-EINVAL); 3664 case IP_BOUND_IF: 3665 if (!checkonly) 3666 udp->udp_bound_if = *i1; 3667 break; 3668 case IP_UNSPEC_SRC: 3669 if (!checkonly) 3670 udp->udp_unspec_source = onoff; 3671 break; 3672 case IP_XMIT_IF: 3673 if (!checkonly) 3674 udp->udp_xmit_if = *i1; 3675 break; 3676 default: 3677 *outlenp = 0; 3678 return (EINVAL); 3679 } 3680 break; 3681 case IPPROTO_IPV6: { 3682 ip6_pkt_t *ipp; 3683 boolean_t sticky; 3684 3685 if (udp->udp_family != AF_INET6) { 3686 *outlenp = 0; 3687 return (ENOPROTOOPT); 3688 } 3689 /* 3690 * Deal with both sticky options and ancillary data 3691 */ 3692 sticky = B_FALSE; 3693 if (attrs == NULL || (ipp = attrs->udpattr_ipp) == NULL) { 3694 /* sticky options, or none */ 3695 ipp = &udp->udp_sticky_ipp; 3696 sticky = B_TRUE; 3697 } 3698 3699 switch (name) { 3700 case IPV6_MULTICAST_IF: 3701 if (!checkonly) 3702 udp->udp_multicast_if_index = *i1; 3703 break; 3704 case IPV6_UNICAST_HOPS: 3705 /* -1 means use default */ 3706 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 3707 *outlenp = 0; 3708 return (EINVAL); 3709 } 3710 if (!checkonly) { 3711 if (*i1 == -1) { 3712 udp->udp_ttl = ipp->ipp_unicast_hops = 3713 udp_ipv6_hoplimit; 3714 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 3715 /* Pass modified value to IP. */ 3716 *i1 = udp->udp_ttl; 3717 } else { 3718 udp->udp_ttl = ipp->ipp_unicast_hops = 3719 (uint8_t)*i1; 3720 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 3721 } 3722 /* Rebuild the header template */ 3723 error = udp_build_hdrs(q, udp); 3724 if (error != 0) { 3725 *outlenp = 0; 3726 return (error); 3727 } 3728 } 3729 break; 3730 case IPV6_MULTICAST_HOPS: 3731 /* -1 means use default */ 3732 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 3733 *outlenp = 0; 3734 return (EINVAL); 3735 } 3736 if (!checkonly) { 3737 if (*i1 == -1) { 3738 udp->udp_multicast_ttl = 3739 ipp->ipp_multicast_hops = 3740 IP_DEFAULT_MULTICAST_TTL; 3741 ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS; 3742 /* Pass modified value to IP. */ 3743 *i1 = udp->udp_multicast_ttl; 3744 } else { 3745 udp->udp_multicast_ttl = 3746 ipp->ipp_multicast_hops = 3747 (uint8_t)*i1; 3748 ipp->ipp_fields |= IPPF_MULTICAST_HOPS; 3749 } 3750 } 3751 break; 3752 case IPV6_MULTICAST_LOOP: 3753 if (*i1 != 0 && *i1 != 1) { 3754 *outlenp = 0; 3755 return (EINVAL); 3756 } 3757 if (!checkonly) 3758 connp->conn_multicast_loop = *i1; 3759 break; 3760 case IPV6_JOIN_GROUP: 3761 case IPV6_LEAVE_GROUP: 3762 case MCAST_JOIN_GROUP: 3763 case MCAST_LEAVE_GROUP: 3764 case MCAST_BLOCK_SOURCE: 3765 case MCAST_UNBLOCK_SOURCE: 3766 case MCAST_JOIN_SOURCE_GROUP: 3767 case MCAST_LEAVE_SOURCE_GROUP: 3768 /* 3769 * "soft" error (negative) 3770 * option not handled at this level 3771 * Note: Do not modify *outlenp 3772 */ 3773 return (-EINVAL); 3774 case IPV6_BOUND_IF: 3775 if (!checkonly) 3776 udp->udp_bound_if = *i1; 3777 break; 3778 case IPV6_UNSPEC_SRC: 3779 if (!checkonly) 3780 udp->udp_unspec_source = onoff; 3781 break; 3782 /* 3783 * Set boolean switches for ancillary data delivery 3784 */ 3785 case IPV6_RECVPKTINFO: 3786 if (!checkonly) 3787 udp->udp_ipv6_recvpktinfo = onoff; 3788 break; 3789 case IPV6_RECVTCLASS: 3790 if (!checkonly) { 3791 udp->udp_ipv6_recvtclass = onoff; 3792 } 3793 break; 3794 case IPV6_RECVPATHMTU: 3795 if (!checkonly) { 3796 udp->udp_ipv6_recvpathmtu = onoff; 3797 } 3798 break; 3799 case IPV6_RECVHOPLIMIT: 3800 if (!checkonly) 3801 udp->udp_ipv6_recvhoplimit = onoff; 3802 break; 3803 case IPV6_RECVHOPOPTS: 3804 if (!checkonly) 3805 udp->udp_ipv6_recvhopopts = onoff; 3806 break; 3807 case IPV6_RECVDSTOPTS: 3808 if (!checkonly) 3809 udp->udp_ipv6_recvdstopts = onoff; 3810 break; 3811 case _OLD_IPV6_RECVDSTOPTS: 3812 if (!checkonly) 3813 udp->udp_old_ipv6_recvdstopts = onoff; 3814 break; 3815 case IPV6_RECVRTHDRDSTOPTS: 3816 if (!checkonly) 3817 udp->udp_ipv6_recvrthdrdstopts = onoff; 3818 break; 3819 case IPV6_RECVRTHDR: 3820 if (!checkonly) 3821 udp->udp_ipv6_recvrthdr = onoff; 3822 break; 3823 /* 3824 * Set sticky options or ancillary data. 3825 * If sticky options, (re)build any extension headers 3826 * that might be needed as a result. 3827 */ 3828 case IPV6_PKTINFO: 3829 /* 3830 * The source address and ifindex are verified 3831 * in ip_opt_set(). For ancillary data the 3832 * source address is checked in ip_wput_v6. 3833 */ 3834 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 3835 return (EINVAL); 3836 if (checkonly) 3837 break; 3838 3839 if (inlen == 0) { 3840 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 3841 ipp->ipp_sticky_ignored |= 3842 (IPPF_IFINDEX|IPPF_ADDR); 3843 } else { 3844 struct in6_pktinfo *pkti; 3845 3846 pkti = (struct in6_pktinfo *)invalp; 3847 ipp->ipp_ifindex = pkti->ipi6_ifindex; 3848 ipp->ipp_addr = pkti->ipi6_addr; 3849 if (ipp->ipp_ifindex != 0) 3850 ipp->ipp_fields |= IPPF_IFINDEX; 3851 else 3852 ipp->ipp_fields &= ~IPPF_IFINDEX; 3853 if (!IN6_IS_ADDR_UNSPECIFIED( 3854 &ipp->ipp_addr)) 3855 ipp->ipp_fields |= IPPF_ADDR; 3856 else 3857 ipp->ipp_fields &= ~IPPF_ADDR; 3858 } 3859 if (sticky) { 3860 error = udp_build_hdrs(q, udp); 3861 if (error != 0) 3862 return (error); 3863 } 3864 break; 3865 case IPV6_HOPLIMIT: 3866 if (sticky) 3867 return (EINVAL); 3868 if (inlen != 0 && inlen != sizeof (int)) 3869 return (EINVAL); 3870 if (checkonly) 3871 break; 3872 3873 if (inlen == 0) { 3874 ipp->ipp_fields &= ~IPPF_HOPLIMIT; 3875 ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT; 3876 } else { 3877 if (*i1 > 255 || *i1 < -1) 3878 return (EINVAL); 3879 if (*i1 == -1) 3880 ipp->ipp_hoplimit = udp_ipv6_hoplimit; 3881 else 3882 ipp->ipp_hoplimit = *i1; 3883 ipp->ipp_fields |= IPPF_HOPLIMIT; 3884 } 3885 break; 3886 case IPV6_TCLASS: 3887 if (inlen != 0 && inlen != sizeof (int)) 3888 return (EINVAL); 3889 if (checkonly) 3890 break; 3891 3892 if (inlen == 0) { 3893 ipp->ipp_fields &= ~IPPF_TCLASS; 3894 ipp->ipp_sticky_ignored |= IPPF_TCLASS; 3895 } else { 3896 if (*i1 > 255 || *i1 < -1) 3897 return (EINVAL); 3898 if (*i1 == -1) 3899 ipp->ipp_tclass = 0; 3900 else 3901 ipp->ipp_tclass = *i1; 3902 ipp->ipp_fields |= IPPF_TCLASS; 3903 } 3904 if (sticky) { 3905 error = udp_build_hdrs(q, udp); 3906 if (error != 0) 3907 return (error); 3908 } 3909 break; 3910 case IPV6_NEXTHOP: 3911 /* 3912 * IP will verify that the nexthop is reachable 3913 * and fail for sticky options. 3914 */ 3915 if (inlen != 0 && inlen != sizeof (sin6_t)) 3916 return (EINVAL); 3917 if (checkonly) 3918 break; 3919 3920 if (inlen == 0) { 3921 ipp->ipp_fields &= ~IPPF_NEXTHOP; 3922 ipp->ipp_sticky_ignored |= IPPF_NEXTHOP; 3923 } else { 3924 sin6_t *sin6 = (sin6_t *)invalp; 3925 3926 if (sin6->sin6_family != AF_INET6) 3927 return (EAFNOSUPPORT); 3928 if (IN6_IS_ADDR_V4MAPPED( 3929 &sin6->sin6_addr)) 3930 return (EADDRNOTAVAIL); 3931 ipp->ipp_nexthop = sin6->sin6_addr; 3932 if (!IN6_IS_ADDR_UNSPECIFIED( 3933 &ipp->ipp_nexthop)) 3934 ipp->ipp_fields |= IPPF_NEXTHOP; 3935 else 3936 ipp->ipp_fields &= ~IPPF_NEXTHOP; 3937 } 3938 if (sticky) { 3939 error = udp_build_hdrs(q, udp); 3940 if (error != 0) 3941 return (error); 3942 } 3943 break; 3944 case IPV6_HOPOPTS: { 3945 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 3946 /* 3947 * Sanity checks - minimum size, size a multiple of 3948 * eight bytes, and matching size passed in. 3949 */ 3950 if (inlen != 0 && 3951 inlen != (8 * (hopts->ip6h_len + 1))) 3952 return (EINVAL); 3953 3954 if (checkonly) 3955 break; 3956 3957 error = optcom_pkt_set(invalp, inlen, sticky, 3958 (uchar_t **)&ipp->ipp_hopopts, 3959 &ipp->ipp_hopoptslen, 3960 sticky ? udp->udp_label_len_v6 : 0); 3961 if (error != 0) 3962 return (error); 3963 if (ipp->ipp_hopoptslen == 0) { 3964 ipp->ipp_fields &= ~IPPF_HOPOPTS; 3965 ipp->ipp_sticky_ignored |= IPPF_HOPOPTS; 3966 } else { 3967 ipp->ipp_fields |= IPPF_HOPOPTS; 3968 } 3969 if (sticky) { 3970 error = udp_build_hdrs(q, udp); 3971 if (error != 0) 3972 return (error); 3973 } 3974 break; 3975 } 3976 case IPV6_RTHDRDSTOPTS: { 3977 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 3978 3979 /* 3980 * Sanity checks - minimum size, size a multiple of 3981 * eight bytes, and matching size passed in. 3982 */ 3983 if (inlen != 0 && 3984 inlen != (8 * (dopts->ip6d_len + 1))) 3985 return (EINVAL); 3986 3987 if (checkonly) 3988 break; 3989 3990 if (inlen == 0) { 3991 if (sticky && 3992 (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) { 3993 kmem_free(ipp->ipp_rtdstopts, 3994 ipp->ipp_rtdstoptslen); 3995 ipp->ipp_rtdstopts = NULL; 3996 ipp->ipp_rtdstoptslen = 0; 3997 } 3998 3999 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 4000 ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS; 4001 } else { 4002 error = optcom_pkt_set(invalp, inlen, sticky, 4003 (uchar_t **)&ipp->ipp_rtdstopts, 4004 &ipp->ipp_rtdstoptslen, 0); 4005 if (error != 0) 4006 return (error); 4007 ipp->ipp_fields |= IPPF_RTDSTOPTS; 4008 } 4009 if (sticky) { 4010 error = udp_build_hdrs(q, udp); 4011 if (error != 0) 4012 return (error); 4013 } 4014 break; 4015 } 4016 case IPV6_DSTOPTS: { 4017 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 4018 4019 /* 4020 * Sanity checks - minimum size, size a multiple of 4021 * eight bytes, and matching size passed in. 4022 */ 4023 if (inlen != 0 && 4024 inlen != (8 * (dopts->ip6d_len + 1))) 4025 return (EINVAL); 4026 4027 if (checkonly) 4028 break; 4029 4030 if (inlen == 0) { 4031 if (sticky && 4032 (ipp->ipp_fields & IPPF_DSTOPTS) != 0) { 4033 kmem_free(ipp->ipp_dstopts, 4034 ipp->ipp_dstoptslen); 4035 ipp->ipp_dstopts = NULL; 4036 ipp->ipp_dstoptslen = 0; 4037 } 4038 ipp->ipp_fields &= ~IPPF_DSTOPTS; 4039 ipp->ipp_sticky_ignored |= IPPF_DSTOPTS; 4040 } else { 4041 error = optcom_pkt_set(invalp, inlen, sticky, 4042 (uchar_t **)&ipp->ipp_dstopts, 4043 &ipp->ipp_dstoptslen, 0); 4044 if (error != 0) 4045 return (error); 4046 ipp->ipp_fields |= IPPF_DSTOPTS; 4047 } 4048 if (sticky) { 4049 error = udp_build_hdrs(q, udp); 4050 if (error != 0) 4051 return (error); 4052 } 4053 break; 4054 } 4055 case IPV6_RTHDR: { 4056 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 4057 4058 /* 4059 * Sanity checks - minimum size, size a multiple of 4060 * eight bytes, and matching size passed in. 4061 */ 4062 if (inlen != 0 && 4063 inlen != (8 * (rt->ip6r_len + 1))) 4064 return (EINVAL); 4065 4066 if (checkonly) 4067 break; 4068 4069 if (inlen == 0) { 4070 if (sticky && 4071 (ipp->ipp_fields & IPPF_RTHDR) != 0) { 4072 kmem_free(ipp->ipp_rthdr, 4073 ipp->ipp_rthdrlen); 4074 ipp->ipp_rthdr = NULL; 4075 ipp->ipp_rthdrlen = 0; 4076 } 4077 ipp->ipp_fields &= ~IPPF_RTHDR; 4078 ipp->ipp_sticky_ignored |= IPPF_RTHDR; 4079 } else { 4080 error = optcom_pkt_set(invalp, inlen, sticky, 4081 (uchar_t **)&ipp->ipp_rthdr, 4082 &ipp->ipp_rthdrlen, 0); 4083 if (error != 0) 4084 return (error); 4085 ipp->ipp_fields |= IPPF_RTHDR; 4086 } 4087 if (sticky) { 4088 error = udp_build_hdrs(q, udp); 4089 if (error != 0) 4090 return (error); 4091 } 4092 break; 4093 } 4094 4095 case IPV6_DONTFRAG: 4096 if (checkonly) 4097 break; 4098 4099 if (onoff) { 4100 ipp->ipp_fields |= IPPF_DONTFRAG; 4101 } else { 4102 ipp->ipp_fields &= ~IPPF_DONTFRAG; 4103 } 4104 break; 4105 4106 case IPV6_USE_MIN_MTU: 4107 if (inlen != sizeof (int)) 4108 return (EINVAL); 4109 4110 if (*i1 < -1 || *i1 > 1) 4111 return (EINVAL); 4112 4113 if (checkonly) 4114 break; 4115 4116 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 4117 ipp->ipp_use_min_mtu = *i1; 4118 break; 4119 4120 case IPV6_BOUND_PIF: 4121 case IPV6_SEC_OPT: 4122 case IPV6_DONTFAILOVER_IF: 4123 case IPV6_SRC_PREFERENCES: 4124 case IPV6_V6ONLY: 4125 /* Handled at the IP level */ 4126 return (-EINVAL); 4127 default: 4128 *outlenp = 0; 4129 return (EINVAL); 4130 } 4131 break; 4132 } /* end IPPROTO_IPV6 */ 4133 case IPPROTO_UDP: 4134 switch (name) { 4135 case UDP_ANONPRIVBIND: 4136 if ((error = secpolicy_net_privaddr(cr, 0)) != 0) { 4137 *outlenp = 0; 4138 return (error); 4139 } 4140 if (!checkonly) { 4141 udp->udp_anon_priv_bind = onoff; 4142 } 4143 break; 4144 case UDP_EXCLBIND: 4145 if (!checkonly) 4146 udp->udp_exclbind = onoff; 4147 break; 4148 case UDP_RCVHDR: 4149 if (!checkonly) 4150 udp->udp_rcvhdr = onoff; 4151 break; 4152 default: 4153 *outlenp = 0; 4154 return (EINVAL); 4155 } 4156 break; 4157 default: 4158 *outlenp = 0; 4159 return (EINVAL); 4160 } 4161 /* 4162 * Common case of OK return with outval same as inval. 4163 */ 4164 if (invalp != outvalp) { 4165 /* don't trust bcopy for identical src/dst */ 4166 (void) bcopy(invalp, outvalp, inlen); 4167 } 4168 *outlenp = inlen; 4169 return (0); 4170 } 4171 4172 /* 4173 * Update udp_sticky_hdrs based on udp_sticky_ipp, udp_v6src, and udp_ttl. 4174 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 4175 * headers, and the udp header. 4176 * Returns failure if can't allocate memory. 4177 */ 4178 static int 4179 udp_build_hdrs(queue_t *q, udp_t *udp) 4180 { 4181 uchar_t *hdrs; 4182 uint_t hdrs_len; 4183 ip6_t *ip6h; 4184 ip6i_t *ip6i; 4185 udpha_t *udpha; 4186 ip6_pkt_t *ipp = &udp->udp_sticky_ipp; 4187 4188 hdrs_len = ip_total_hdrs_len_v6(ipp) + UDPH_SIZE; 4189 ASSERT(hdrs_len != 0); 4190 if (hdrs_len != udp->udp_sticky_hdrs_len) { 4191 /* Need to reallocate */ 4192 hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP); 4193 if (hdrs == NULL) 4194 return (ENOMEM); 4195 4196 if (udp->udp_sticky_hdrs_len != 0) { 4197 kmem_free(udp->udp_sticky_hdrs, 4198 udp->udp_sticky_hdrs_len); 4199 } 4200 udp->udp_sticky_hdrs = hdrs; 4201 udp->udp_sticky_hdrs_len = hdrs_len; 4202 } 4203 ip_build_hdrs_v6(udp->udp_sticky_hdrs, 4204 udp->udp_sticky_hdrs_len - UDPH_SIZE, ipp, IPPROTO_UDP); 4205 4206 /* Set header fields not in ipp */ 4207 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 4208 ip6i = (ip6i_t *)udp->udp_sticky_hdrs; 4209 ip6h = (ip6_t *)&ip6i[1]; 4210 } else { 4211 ip6h = (ip6_t *)udp->udp_sticky_hdrs; 4212 } 4213 4214 if (!(ipp->ipp_fields & IPPF_ADDR)) 4215 ip6h->ip6_src = udp->udp_v6src; 4216 4217 udpha = (udpha_t *)(udp->udp_sticky_hdrs + hdrs_len - UDPH_SIZE); 4218 udpha->uha_src_port = udp->udp_port; 4219 4220 /* Try to get everything in a single mblk */ 4221 if (hdrs_len > udp->udp_max_hdr_len) { 4222 udp->udp_max_hdr_len = hdrs_len; 4223 (void) mi_set_sth_wroff(RD(q), udp->udp_max_hdr_len + 4224 udp_wroff_extra); 4225 } 4226 return (0); 4227 } 4228 4229 /* 4230 * This routine retrieves the value of an ND variable in a udpparam_t 4231 * structure. It is called through nd_getset when a user reads the 4232 * variable. 4233 */ 4234 /* ARGSUSED */ 4235 static int 4236 udp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 4237 { 4238 udpparam_t *udppa = (udpparam_t *)cp; 4239 4240 (void) mi_mpprintf(mp, "%d", udppa->udp_param_value); 4241 return (0); 4242 } 4243 4244 /* 4245 * Walk through the param array specified registering each element with the 4246 * named dispatch (ND) handler. 4247 */ 4248 static boolean_t 4249 udp_param_register(udpparam_t *udppa, int cnt) 4250 { 4251 for (; cnt-- > 0; udppa++) { 4252 if (udppa->udp_param_name && udppa->udp_param_name[0]) { 4253 if (!nd_load(&udp_g_nd, udppa->udp_param_name, 4254 udp_param_get, udp_param_set, 4255 (caddr_t)udppa)) { 4256 nd_free(&udp_g_nd); 4257 return (B_FALSE); 4258 } 4259 } 4260 } 4261 if (!nd_load(&udp_g_nd, "udp_extra_priv_ports", 4262 udp_extra_priv_ports_get, NULL, NULL)) { 4263 nd_free(&udp_g_nd); 4264 return (B_FALSE); 4265 } 4266 if (!nd_load(&udp_g_nd, "udp_extra_priv_ports_add", 4267 NULL, udp_extra_priv_ports_add, NULL)) { 4268 nd_free(&udp_g_nd); 4269 return (B_FALSE); 4270 } 4271 if (!nd_load(&udp_g_nd, "udp_extra_priv_ports_del", 4272 NULL, udp_extra_priv_ports_del, NULL)) { 4273 nd_free(&udp_g_nd); 4274 return (B_FALSE); 4275 } 4276 if (!nd_load(&udp_g_nd, "udp_status", udp_status_report, NULL, 4277 NULL)) { 4278 nd_free(&udp_g_nd); 4279 return (B_FALSE); 4280 } 4281 if (!nd_load(&udp_g_nd, "udp_bind_hash", udp_bind_hash_report, NULL, 4282 NULL)) { 4283 nd_free(&udp_g_nd); 4284 return (B_FALSE); 4285 } 4286 return (B_TRUE); 4287 } 4288 4289 /* This routine sets an ND variable in a udpparam_t structure. */ 4290 /* ARGSUSED */ 4291 static int 4292 udp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 4293 { 4294 long new_value; 4295 udpparam_t *udppa = (udpparam_t *)cp; 4296 4297 /* 4298 * Fail the request if the new value does not lie within the 4299 * required bounds. 4300 */ 4301 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 4302 new_value < udppa->udp_param_min || 4303 new_value > udppa->udp_param_max) { 4304 return (EINVAL); 4305 } 4306 4307 /* Set the new value */ 4308 udppa->udp_param_value = new_value; 4309 return (0); 4310 } 4311 4312 /* 4313 * Copy hop-by-hop option from ipp->ipp_hopopts to the buffer provided (with 4314 * T_opthdr) and return the number of bytes copied. 'dbuf' may be NULL to 4315 * just count the length needed for allocation. If 'dbuf' is non-NULL, 4316 * then it's assumed to be allocated to be large enough. 4317 * 4318 * Returns zero if trimming of the security option causes all options to go 4319 * away. 4320 */ 4321 static size_t 4322 copy_hop_opts(const ip6_pkt_t *ipp, uchar_t *dbuf) 4323 { 4324 struct T_opthdr *toh; 4325 size_t hol = ipp->ipp_hopoptslen; 4326 ip6_hbh_t *dstopt = NULL; 4327 const ip6_hbh_t *srcopt = ipp->ipp_hopopts; 4328 size_t tlen, olen, plen; 4329 boolean_t deleting; 4330 const struct ip6_opt *sopt, *lastpad; 4331 struct ip6_opt *dopt; 4332 4333 if ((toh = (struct T_opthdr *)dbuf) != NULL) { 4334 toh->level = IPPROTO_IPV6; 4335 toh->name = IPV6_HOPOPTS; 4336 toh->status = 0; 4337 dstopt = (ip6_hbh_t *)(toh + 1); 4338 } 4339 4340 /* 4341 * If labeling is enabled, then skip the label option 4342 * but get other options if there are any. 4343 */ 4344 if (is_system_labeled()) { 4345 dopt = NULL; 4346 if (dstopt != NULL) { 4347 /* will fill in ip6h_len later */ 4348 dstopt->ip6h_nxt = srcopt->ip6h_nxt; 4349 dopt = (struct ip6_opt *)(dstopt + 1); 4350 } 4351 sopt = (const struct ip6_opt *)(srcopt + 1); 4352 hol -= sizeof (*srcopt); 4353 tlen = sizeof (*dstopt); 4354 lastpad = NULL; 4355 deleting = B_FALSE; 4356 /* 4357 * This loop finds the first (lastpad pointer) of any number of 4358 * pads that preceeds the security option, then treats the 4359 * security option as though it were a pad, and then finds the 4360 * next non-pad option (or end of list). 4361 * 4362 * It then treats the entire block as one big pad. To preserve 4363 * alignment of any options that follow, or just the end of the 4364 * list, it computes a minimal new padding size that keeps the 4365 * same alignment for the next option. 4366 * 4367 * If it encounters just a sequence of pads with no security 4368 * option, those are copied as-is rather than collapsed. 4369 * 4370 * Note that to handle the end of list case, the code makes one 4371 * loop with 'hol' set to zero. 4372 */ 4373 for (;;) { 4374 if (hol > 0) { 4375 if (sopt->ip6o_type == IP6OPT_PAD1) { 4376 if (lastpad == NULL) 4377 lastpad = sopt; 4378 sopt = (const struct ip6_opt *) 4379 &sopt->ip6o_len; 4380 hol--; 4381 continue; 4382 } 4383 olen = sopt->ip6o_len + sizeof (*sopt); 4384 if (olen > hol) 4385 olen = hol; 4386 if (sopt->ip6o_type == IP6OPT_PADN || 4387 sopt->ip6o_type == ip6opt_ls) { 4388 if (sopt->ip6o_type == ip6opt_ls) 4389 deleting = B_TRUE; 4390 if (lastpad == NULL) 4391 lastpad = sopt; 4392 sopt = (const struct ip6_opt *) 4393 ((const char *)sopt + olen); 4394 hol -= olen; 4395 continue; 4396 } 4397 } else { 4398 /* if nothing was copied at all, then delete */ 4399 if (tlen == sizeof (*dstopt)) 4400 return (0); 4401 /* last pass; pick up any trailing padding */ 4402 olen = 0; 4403 } 4404 if (deleting) { 4405 /* 4406 * compute aligning effect of deleted material 4407 * to reproduce with pad. 4408 */ 4409 plen = ((const char *)sopt - 4410 (const char *)lastpad) & 7; 4411 tlen += plen; 4412 if (dopt != NULL) { 4413 if (plen == 1) { 4414 dopt->ip6o_type = IP6OPT_PAD1; 4415 } else if (plen > 1) { 4416 plen -= sizeof (*dopt); 4417 dopt->ip6o_type = IP6OPT_PADN; 4418 dopt->ip6o_len = plen; 4419 if (plen > 0) 4420 bzero(dopt + 1, plen); 4421 } 4422 dopt = (struct ip6_opt *) 4423 ((char *)dopt + plen); 4424 } 4425 deleting = B_FALSE; 4426 lastpad = NULL; 4427 } 4428 /* if there's uncopied padding, then copy that now */ 4429 if (lastpad != NULL) { 4430 olen += (const char *)sopt - 4431 (const char *)lastpad; 4432 sopt = lastpad; 4433 lastpad = NULL; 4434 } 4435 if (dopt != NULL && olen > 0) { 4436 bcopy(sopt, dopt, olen); 4437 dopt = (struct ip6_opt *)((char *)dopt + olen); 4438 } 4439 if (hol == 0) 4440 break; 4441 tlen += olen; 4442 sopt = (const struct ip6_opt *) 4443 ((const char *)sopt + olen); 4444 hol -= olen; 4445 } 4446 /* go back and patch up the length value, rounded upward */ 4447 if (dstopt != NULL) 4448 dstopt->ip6h_len = (tlen - 1) >> 3; 4449 } else { 4450 tlen = hol; 4451 if (dstopt != NULL) 4452 bcopy(srcopt, dstopt, hol); 4453 } 4454 4455 tlen += sizeof (*toh); 4456 if (toh != NULL) 4457 toh->len = tlen; 4458 4459 return (tlen); 4460 } 4461 4462 static void 4463 udp_input(conn_t *connp, mblk_t *mp) 4464 { 4465 struct T_unitdata_ind *tudi; 4466 uchar_t *rptr; /* Pointer to IP header */ 4467 int hdr_length; /* Length of IP+UDP headers */ 4468 int udi_size; /* Size of T_unitdata_ind */ 4469 int mp_len; 4470 udp_t *udp; 4471 udpha_t *udpha; 4472 int ipversion; 4473 ip6_pkt_t ipp; 4474 ip6_t *ip6h; 4475 ip6i_t *ip6i; 4476 mblk_t *mp1; 4477 mblk_t *options_mp = NULL; 4478 in_pktinfo_t *pinfo = NULL; 4479 cred_t *cr = NULL; 4480 queue_t *q = connp->conn_rq; 4481 pid_t cpid; 4482 cred_t *rcr = connp->conn_cred; 4483 4484 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_START, 4485 "udp_rput_start: q %p mp %p", q, mp); 4486 4487 udp = connp->conn_udp; 4488 rptr = mp->b_rptr; 4489 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_CTL); 4490 ASSERT(OK_32PTR(rptr)); 4491 4492 /* 4493 * IP should have prepended the options data in an M_CTL 4494 * Check M_CTL "type" to make sure are not here bcos of 4495 * a valid ICMP message 4496 */ 4497 if (DB_TYPE(mp) == M_CTL) { 4498 if (MBLKL(mp) == sizeof (in_pktinfo_t) && 4499 ((in_pktinfo_t *)mp->b_rptr)->in_pkt_ulp_type == 4500 IN_PKTINFO) { 4501 /* 4502 * IP_RECVIF or IP_RECVSLLA information has been 4503 * appended to the packet by IP. We need to 4504 * extract the mblk and adjust the rptr 4505 */ 4506 pinfo = (in_pktinfo_t *)mp->b_rptr; 4507 options_mp = mp; 4508 mp = mp->b_cont; 4509 rptr = mp->b_rptr; 4510 UDP_STAT(udp_in_pktinfo); 4511 } else { 4512 /* 4513 * ICMP messages. 4514 */ 4515 udp_icmp_error(q, mp); 4516 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END, 4517 "udp_rput_end: q %p (%S)", q, "m_ctl"); 4518 return; 4519 } 4520 } 4521 4522 mp_len = msgdsize(mp); 4523 /* 4524 * This is the inbound data path. 4525 * First, we check to make sure the IP version number is correct, 4526 * and then pull the IP and UDP headers into the first mblk. 4527 * Assume IP provides aligned packets - otherwise toss. 4528 * Also, check if we have a complete IP header. 4529 */ 4530 4531 /* Initialize regardless if ipversion is IPv4 or IPv6 */ 4532 ipp.ipp_fields = 0; 4533 4534 ipversion = IPH_HDR_VERSION(rptr); 4535 switch (ipversion) { 4536 case IPV4_VERSION: 4537 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 4538 ASSERT(((ipha_t *)rptr)->ipha_protocol == IPPROTO_UDP); 4539 hdr_length = IPH_HDR_LENGTH(rptr) + UDPH_SIZE; 4540 if ((hdr_length > IP_SIMPLE_HDR_LENGTH + UDPH_SIZE) || 4541 (udp->udp_ip_rcv_options_len)) { 4542 /* 4543 * Handle IPv4 packets with options outside of the 4544 * main data path. Not needed for AF_INET6 sockets 4545 * since they don't support a getsockopt of IP_OPTIONS. 4546 */ 4547 if (udp->udp_family == AF_INET6) 4548 break; 4549 /* 4550 * UDP length check performed for IPv4 packets with 4551 * options to check whether UDP length specified in 4552 * the header is the same as the physical length of 4553 * the packet. 4554 */ 4555 udpha = (udpha_t *)(rptr + (hdr_length - UDPH_SIZE)); 4556 if (mp_len != (ntohs(udpha->uha_length) + 4557 hdr_length - UDPH_SIZE)) { 4558 goto tossit; 4559 } 4560 /* 4561 * Handle the case where the packet has IP options 4562 * and the IP_RECVSLLA & IP_RECVIF are set 4563 */ 4564 if (pinfo != NULL) 4565 mp = options_mp; 4566 udp_become_writer(connp, mp, udp_rput_other_wrapper, 4567 SQTAG_UDP_INPUT); 4568 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END, 4569 "udp_rput_end: q %p (%S)", q, "end"); 4570 return; 4571 } 4572 4573 /* Handle IPV6_RECVHOPLIMIT. */ 4574 if ((udp->udp_family == AF_INET6) && (pinfo != NULL) && 4575 udp->udp_ipv6_recvpktinfo) { 4576 if (pinfo->in_pkt_flags & IPF_RECVIF) { 4577 ipp.ipp_fields |= IPPF_IFINDEX; 4578 ipp.ipp_ifindex = pinfo->in_pkt_ifindex; 4579 } 4580 } 4581 break; 4582 case IPV6_VERSION: 4583 /* 4584 * IPv6 packets can only be received by applications 4585 * that are prepared to receive IPv6 addresses. 4586 * The IP fanout must ensure this. 4587 */ 4588 ASSERT(udp->udp_family == AF_INET6); 4589 4590 ip6h = (ip6_t *)rptr; 4591 ASSERT((uchar_t *)&ip6h[1] <= mp->b_wptr); 4592 4593 if (ip6h->ip6_nxt != IPPROTO_UDP) { 4594 uint8_t nexthdrp; 4595 /* Look for ifindex information */ 4596 if (ip6h->ip6_nxt == IPPROTO_RAW) { 4597 ip6i = (ip6i_t *)ip6h; 4598 if ((uchar_t *)&ip6i[1] > mp->b_wptr) 4599 goto tossit; 4600 4601 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 4602 ASSERT(ip6i->ip6i_ifindex != 0); 4603 ipp.ipp_fields |= IPPF_IFINDEX; 4604 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 4605 } 4606 rptr = (uchar_t *)&ip6i[1]; 4607 mp->b_rptr = rptr; 4608 if (rptr == mp->b_wptr) { 4609 mp1 = mp->b_cont; 4610 freeb(mp); 4611 mp = mp1; 4612 rptr = mp->b_rptr; 4613 } 4614 if (MBLKL(mp) < (IPV6_HDR_LEN + UDPH_SIZE)) 4615 goto tossit; 4616 ip6h = (ip6_t *)rptr; 4617 mp_len = msgdsize(mp); 4618 } 4619 /* 4620 * Find any potentially interesting extension headers 4621 * as well as the length of the IPv6 + extension 4622 * headers. 4623 */ 4624 hdr_length = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp) + 4625 UDPH_SIZE; 4626 ASSERT(nexthdrp == IPPROTO_UDP); 4627 } else { 4628 hdr_length = IPV6_HDR_LEN + UDPH_SIZE; 4629 ip6i = NULL; 4630 } 4631 break; 4632 default: 4633 ASSERT(0); 4634 } 4635 4636 /* 4637 * IP inspected the UDP header thus all of it must be in the mblk. 4638 * UDP length check is performed for IPv6 packets and IPv4 packets 4639 * without options to check if the size of the packet as specified 4640 * by the header is the same as the physical size of the packet. 4641 */ 4642 udpha = (udpha_t *)(rptr + (hdr_length - UDPH_SIZE)); 4643 if ((MBLKL(mp) < hdr_length) || 4644 (mp_len != (ntohs(udpha->uha_length) + hdr_length - UDPH_SIZE))) { 4645 goto tossit; 4646 } 4647 4648 /* Walk past the headers. */ 4649 if (!udp->udp_rcvhdr) { 4650 mp->b_rptr = rptr + hdr_length; 4651 mp_len -= hdr_length; 4652 } 4653 4654 /* 4655 * This is the inbound data path. Packets are passed upstream as 4656 * T_UNITDATA_IND messages with full IP headers still attached. 4657 */ 4658 if (udp->udp_family == AF_INET) { 4659 sin_t *sin; 4660 4661 ASSERT(IPH_HDR_VERSION((ipha_t *)rptr) == IPV4_VERSION); 4662 4663 /* 4664 * Normally only send up the address. 4665 * If IP_RECVDSTADDR is set we include the destination IP 4666 * address as an option. With IP_RECVOPTS we include all 4667 * the IP options. Only ip_rput_other() handles packets 4668 * that contain IP options. 4669 */ 4670 udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin_t); 4671 if (udp->udp_recvdstaddr) { 4672 udi_size += sizeof (struct T_opthdr) + 4673 sizeof (struct in_addr); 4674 UDP_STAT(udp_in_recvdstaddr); 4675 } 4676 4677 /* 4678 * If the IP_RECVSLLA or the IP_RECVIF is set then allocate 4679 * space accordingly 4680 */ 4681 if (udp->udp_recvif && (pinfo != NULL) && 4682 (pinfo->in_pkt_flags & IPF_RECVIF)) { 4683 udi_size += sizeof (struct T_opthdr) + sizeof (uint_t); 4684 UDP_STAT(udp_in_recvif); 4685 } 4686 4687 if (udp->udp_recvslla && (pinfo != NULL) && 4688 (pinfo->in_pkt_flags & IPF_RECVSLLA)) { 4689 udi_size += sizeof (struct T_opthdr) + 4690 sizeof (struct sockaddr_dl); 4691 UDP_STAT(udp_in_recvslla); 4692 } 4693 4694 if (udp->udp_recvucred && (cr = DB_CRED(mp)) != NULL) { 4695 udi_size += sizeof (struct T_opthdr) + ucredsize; 4696 cpid = DB_CPID(mp); 4697 UDP_STAT(udp_in_recvucred); 4698 } 4699 /* 4700 * If IP_RECVTTL is set allocate the appropriate sized buffer 4701 */ 4702 if (udp->udp_recvttl) { 4703 udi_size += sizeof (struct T_opthdr) + sizeof (uint8_t); 4704 UDP_STAT(udp_in_recvttl); 4705 } 4706 /* 4707 * If SO_TIMESTAMP is set allocate the appropriate sized 4708 * buffer. Since gethrestime() expects a pointer aligned 4709 * argument, we allocate space necessary for extra 4710 * alignment (even though it might not be used). 4711 */ 4712 if (udp->udp_timestamp) { 4713 udi_size += sizeof (struct T_opthdr) + 4714 sizeof (timestruc_t) + _POINTER_ALIGNMENT; 4715 UDP_STAT(udp_in_timestamp); 4716 } 4717 ASSERT(IPH_HDR_LENGTH((ipha_t *)rptr) == IP_SIMPLE_HDR_LENGTH); 4718 4719 /* Allocate a message block for the T_UNITDATA_IND structure. */ 4720 mp1 = allocb(udi_size, BPRI_MED); 4721 if (mp1 == NULL) { 4722 freemsg(mp); 4723 if (options_mp != NULL) 4724 freeb(options_mp); 4725 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END, 4726 "udp_rput_end: q %p (%S)", q, "allocbfail"); 4727 BUMP_MIB(&udp_mib, udpInErrors); 4728 return; 4729 } 4730 mp1->b_cont = mp; 4731 mp = mp1; 4732 mp->b_datap->db_type = M_PROTO; 4733 tudi = (struct T_unitdata_ind *)mp->b_rptr; 4734 mp->b_wptr = (uchar_t *)tudi + udi_size; 4735 tudi->PRIM_type = T_UNITDATA_IND; 4736 tudi->SRC_length = sizeof (sin_t); 4737 tudi->SRC_offset = sizeof (struct T_unitdata_ind); 4738 tudi->OPT_offset = sizeof (struct T_unitdata_ind) + 4739 sizeof (sin_t); 4740 udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t)); 4741 tudi->OPT_length = udi_size; 4742 sin = (sin_t *)&tudi[1]; 4743 sin->sin_addr.s_addr = ((ipha_t *)rptr)->ipha_src; 4744 sin->sin_port = udpha->uha_src_port; 4745 sin->sin_family = udp->udp_family; 4746 *(uint32_t *)&sin->sin_zero[0] = 0; 4747 *(uint32_t *)&sin->sin_zero[4] = 0; 4748 4749 /* 4750 * Add options if IP_RECVDSTADDR, IP_RECVIF, IP_RECVSLLA or 4751 * IP_RECVTTL has been set. 4752 */ 4753 if (udi_size != 0) { 4754 /* 4755 * Copy in destination address before options to avoid 4756 * any padding issues. 4757 */ 4758 char *dstopt; 4759 4760 dstopt = (char *)&sin[1]; 4761 if (udp->udp_recvdstaddr) { 4762 struct T_opthdr *toh; 4763 ipaddr_t *dstptr; 4764 4765 toh = (struct T_opthdr *)dstopt; 4766 toh->level = IPPROTO_IP; 4767 toh->name = IP_RECVDSTADDR; 4768 toh->len = sizeof (struct T_opthdr) + 4769 sizeof (ipaddr_t); 4770 toh->status = 0; 4771 dstopt += sizeof (struct T_opthdr); 4772 dstptr = (ipaddr_t *)dstopt; 4773 *dstptr = ((ipha_t *)rptr)->ipha_dst; 4774 dstopt += sizeof (ipaddr_t); 4775 udi_size -= toh->len; 4776 } 4777 4778 if (udp->udp_recvslla && (pinfo != NULL) && 4779 (pinfo->in_pkt_flags & IPF_RECVSLLA)) { 4780 4781 struct T_opthdr *toh; 4782 struct sockaddr_dl *dstptr; 4783 4784 toh = (struct T_opthdr *)dstopt; 4785 toh->level = IPPROTO_IP; 4786 toh->name = IP_RECVSLLA; 4787 toh->len = sizeof (struct T_opthdr) + 4788 sizeof (struct sockaddr_dl); 4789 toh->status = 0; 4790 dstopt += sizeof (struct T_opthdr); 4791 dstptr = (struct sockaddr_dl *)dstopt; 4792 bcopy(&pinfo->in_pkt_slla, dstptr, 4793 sizeof (struct sockaddr_dl)); 4794 dstopt += sizeof (struct sockaddr_dl); 4795 udi_size -= toh->len; 4796 } 4797 4798 if (udp->udp_recvif && (pinfo != NULL) && 4799 (pinfo->in_pkt_flags & IPF_RECVIF)) { 4800 4801 struct T_opthdr *toh; 4802 uint_t *dstptr; 4803 4804 toh = (struct T_opthdr *)dstopt; 4805 toh->level = IPPROTO_IP; 4806 toh->name = IP_RECVIF; 4807 toh->len = sizeof (struct T_opthdr) + 4808 sizeof (uint_t); 4809 toh->status = 0; 4810 dstopt += sizeof (struct T_opthdr); 4811 dstptr = (uint_t *)dstopt; 4812 *dstptr = pinfo->in_pkt_ifindex; 4813 dstopt += sizeof (uint_t); 4814 udi_size -= toh->len; 4815 } 4816 4817 if (cr != NULL) { 4818 struct T_opthdr *toh; 4819 4820 toh = (struct T_opthdr *)dstopt; 4821 toh->level = SOL_SOCKET; 4822 toh->name = SCM_UCRED; 4823 toh->len = sizeof (struct T_opthdr) + ucredsize; 4824 toh->status = 0; 4825 (void) cred2ucred(cr, cpid, &toh[1], rcr); 4826 dstopt += toh->len; 4827 udi_size -= toh->len; 4828 } 4829 4830 if (udp->udp_recvttl) { 4831 struct T_opthdr *toh; 4832 uint8_t *dstptr; 4833 4834 toh = (struct T_opthdr *)dstopt; 4835 toh->level = IPPROTO_IP; 4836 toh->name = IP_RECVTTL; 4837 toh->len = sizeof (struct T_opthdr) + 4838 sizeof (uint8_t); 4839 toh->status = 0; 4840 dstopt += sizeof (struct T_opthdr); 4841 dstptr = (uint8_t *)dstopt; 4842 *dstptr = ((ipha_t *)rptr)->ipha_ttl; 4843 dstopt += sizeof (uint8_t); 4844 udi_size -= toh->len; 4845 } 4846 if (udp->udp_timestamp) { 4847 struct T_opthdr *toh; 4848 4849 toh = (struct T_opthdr *)dstopt; 4850 toh->level = SOL_SOCKET; 4851 toh->name = SCM_TIMESTAMP; 4852 toh->len = sizeof (struct T_opthdr) + 4853 sizeof (timestruc_t) + _POINTER_ALIGNMENT; 4854 toh->status = 0; 4855 dstopt += sizeof (struct T_opthdr); 4856 /* Align for gethrestime() */ 4857 dstopt = (char *)P2ROUNDUP((intptr_t)dstopt, 4858 sizeof (intptr_t)); 4859 gethrestime((timestruc_t *)dstopt); 4860 dstopt += sizeof (timestruc_t); 4861 udi_size -= toh->len; 4862 } 4863 4864 /* Consumed all of allocated space */ 4865 ASSERT(udi_size == 0); 4866 } 4867 } else { 4868 sin6_t *sin6; 4869 4870 /* 4871 * Handle both IPv4 and IPv6 packets for IPv6 sockets. 4872 * 4873 * Normally we only send up the address. If receiving of any 4874 * optional receive side information is enabled, we also send 4875 * that up as options. 4876 * [ Only udp_rput_other() handles packets that contain IP 4877 * options so code to account for does not appear immediately 4878 * below but elsewhere ] 4879 */ 4880 udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t); 4881 4882 if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS| 4883 IPPF_RTHDR|IPPF_IFINDEX)) { 4884 if (udp->udp_ipv6_recvhopopts && 4885 (ipp.ipp_fields & IPPF_HOPOPTS)) { 4886 size_t hlen; 4887 4888 UDP_STAT(udp_in_recvhopopts); 4889 hlen = copy_hop_opts(&ipp, NULL); 4890 if (hlen == 0) 4891 ipp.ipp_fields &= ~IPPF_HOPOPTS; 4892 udi_size += hlen; 4893 } 4894 if ((udp->udp_ipv6_recvdstopts || 4895 udp->udp_old_ipv6_recvdstopts) && 4896 (ipp.ipp_fields & IPPF_DSTOPTS)) { 4897 udi_size += sizeof (struct T_opthdr) + 4898 ipp.ipp_dstoptslen; 4899 UDP_STAT(udp_in_recvdstopts); 4900 } 4901 if (((udp->udp_ipv6_recvdstopts && 4902 udp->udp_ipv6_recvrthdr && 4903 (ipp.ipp_fields & IPPF_RTHDR)) || 4904 udp->udp_ipv6_recvrthdrdstopts) && 4905 (ipp.ipp_fields & IPPF_RTDSTOPTS)) { 4906 udi_size += sizeof (struct T_opthdr) + 4907 ipp.ipp_rtdstoptslen; 4908 UDP_STAT(udp_in_recvrtdstopts); 4909 } 4910 if (udp->udp_ipv6_recvrthdr && 4911 (ipp.ipp_fields & IPPF_RTHDR)) { 4912 udi_size += sizeof (struct T_opthdr) + 4913 ipp.ipp_rthdrlen; 4914 UDP_STAT(udp_in_recvrthdr); 4915 } 4916 if (udp->udp_ipv6_recvpktinfo && 4917 (ipp.ipp_fields & IPPF_IFINDEX)) { 4918 udi_size += sizeof (struct T_opthdr) + 4919 sizeof (struct in6_pktinfo); 4920 UDP_STAT(udp_in_recvpktinfo); 4921 } 4922 4923 } 4924 if (udp->udp_recvucred && (cr = DB_CRED(mp)) != NULL) { 4925 udi_size += sizeof (struct T_opthdr) + ucredsize; 4926 cpid = DB_CPID(mp); 4927 UDP_STAT(udp_in_recvucred); 4928 } 4929 4930 if (udp->udp_ipv6_recvhoplimit) { 4931 udi_size += sizeof (struct T_opthdr) + sizeof (int); 4932 UDP_STAT(udp_in_recvhoplimit); 4933 } 4934 4935 if (udp->udp_ipv6_recvtclass) { 4936 udi_size += sizeof (struct T_opthdr) + sizeof (int); 4937 UDP_STAT(udp_in_recvtclass); 4938 } 4939 4940 mp1 = allocb(udi_size, BPRI_MED); 4941 if (mp1 == NULL) { 4942 freemsg(mp); 4943 if (options_mp != NULL) 4944 freeb(options_mp); 4945 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END, 4946 "udp_rput_end: q %p (%S)", q, "allocbfail"); 4947 BUMP_MIB(&udp_mib, udpInErrors); 4948 return; 4949 } 4950 mp1->b_cont = mp; 4951 mp = mp1; 4952 mp->b_datap->db_type = M_PROTO; 4953 tudi = (struct T_unitdata_ind *)mp->b_rptr; 4954 mp->b_wptr = (uchar_t *)tudi + udi_size; 4955 tudi->PRIM_type = T_UNITDATA_IND; 4956 tudi->SRC_length = sizeof (sin6_t); 4957 tudi->SRC_offset = sizeof (struct T_unitdata_ind); 4958 tudi->OPT_offset = sizeof (struct T_unitdata_ind) + 4959 sizeof (sin6_t); 4960 udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t)); 4961 tudi->OPT_length = udi_size; 4962 sin6 = (sin6_t *)&tudi[1]; 4963 if (ipversion == IPV4_VERSION) { 4964 in6_addr_t v6dst; 4965 4966 IN6_IPADDR_TO_V4MAPPED(((ipha_t *)rptr)->ipha_src, 4967 &sin6->sin6_addr); 4968 IN6_IPADDR_TO_V4MAPPED(((ipha_t *)rptr)->ipha_dst, 4969 &v6dst); 4970 sin6->sin6_flowinfo = 0; 4971 sin6->sin6_scope_id = 0; 4972 sin6->__sin6_src_id = ip_srcid_find_addr(&v6dst, 4973 connp->conn_zoneid); 4974 } else { 4975 sin6->sin6_addr = ip6h->ip6_src; 4976 /* No sin6_flowinfo per API */ 4977 sin6->sin6_flowinfo = 0; 4978 /* For link-scope source pass up scope id */ 4979 if ((ipp.ipp_fields & IPPF_IFINDEX) && 4980 IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 4981 sin6->sin6_scope_id = ipp.ipp_ifindex; 4982 else 4983 sin6->sin6_scope_id = 0; 4984 sin6->__sin6_src_id = ip_srcid_find_addr( 4985 &ip6h->ip6_dst, connp->conn_zoneid); 4986 } 4987 sin6->sin6_port = udpha->uha_src_port; 4988 sin6->sin6_family = udp->udp_family; 4989 4990 if (udi_size != 0) { 4991 uchar_t *dstopt; 4992 4993 dstopt = (uchar_t *)&sin6[1]; 4994 if (udp->udp_ipv6_recvpktinfo && 4995 (ipp.ipp_fields & IPPF_IFINDEX)) { 4996 struct T_opthdr *toh; 4997 struct in6_pktinfo *pkti; 4998 4999 toh = (struct T_opthdr *)dstopt; 5000 toh->level = IPPROTO_IPV6; 5001 toh->name = IPV6_PKTINFO; 5002 toh->len = sizeof (struct T_opthdr) + 5003 sizeof (*pkti); 5004 toh->status = 0; 5005 dstopt += sizeof (struct T_opthdr); 5006 pkti = (struct in6_pktinfo *)dstopt; 5007 if (ipversion == IPV6_VERSION) 5008 pkti->ipi6_addr = ip6h->ip6_dst; 5009 else 5010 IN6_IPADDR_TO_V4MAPPED( 5011 ((ipha_t *)rptr)->ipha_dst, 5012 &pkti->ipi6_addr); 5013 pkti->ipi6_ifindex = ipp.ipp_ifindex; 5014 dstopt += sizeof (*pkti); 5015 udi_size -= toh->len; 5016 } 5017 if (udp->udp_ipv6_recvhoplimit) { 5018 struct T_opthdr *toh; 5019 5020 toh = (struct T_opthdr *)dstopt; 5021 toh->level = IPPROTO_IPV6; 5022 toh->name = IPV6_HOPLIMIT; 5023 toh->len = sizeof (struct T_opthdr) + 5024 sizeof (uint_t); 5025 toh->status = 0; 5026 dstopt += sizeof (struct T_opthdr); 5027 if (ipversion == IPV6_VERSION) 5028 *(uint_t *)dstopt = ip6h->ip6_hops; 5029 else 5030 *(uint_t *)dstopt = 5031 ((ipha_t *)rptr)->ipha_ttl; 5032 dstopt += sizeof (uint_t); 5033 udi_size -= toh->len; 5034 } 5035 if (udp->udp_ipv6_recvtclass) { 5036 struct T_opthdr *toh; 5037 5038 toh = (struct T_opthdr *)dstopt; 5039 toh->level = IPPROTO_IPV6; 5040 toh->name = IPV6_TCLASS; 5041 toh->len = sizeof (struct T_opthdr) + 5042 sizeof (uint_t); 5043 toh->status = 0; 5044 dstopt += sizeof (struct T_opthdr); 5045 if (ipversion == IPV6_VERSION) { 5046 *(uint_t *)dstopt = 5047 IPV6_FLOW_TCLASS(ip6h->ip6_flow); 5048 } else { 5049 ipha_t *ipha = (ipha_t *)rptr; 5050 *(uint_t *)dstopt = 5051 ipha->ipha_type_of_service; 5052 } 5053 dstopt += sizeof (uint_t); 5054 udi_size -= toh->len; 5055 } 5056 if (udp->udp_ipv6_recvhopopts && 5057 (ipp.ipp_fields & IPPF_HOPOPTS)) { 5058 size_t hlen; 5059 5060 hlen = copy_hop_opts(&ipp, dstopt); 5061 dstopt += hlen; 5062 udi_size -= hlen; 5063 } 5064 if (udp->udp_ipv6_recvdstopts && 5065 udp->udp_ipv6_recvrthdr && 5066 (ipp.ipp_fields & IPPF_RTHDR) && 5067 (ipp.ipp_fields & IPPF_RTDSTOPTS)) { 5068 struct T_opthdr *toh; 5069 5070 toh = (struct T_opthdr *)dstopt; 5071 toh->level = IPPROTO_IPV6; 5072 toh->name = IPV6_DSTOPTS; 5073 toh->len = sizeof (struct T_opthdr) + 5074 ipp.ipp_rtdstoptslen; 5075 toh->status = 0; 5076 dstopt += sizeof (struct T_opthdr); 5077 bcopy(ipp.ipp_rtdstopts, dstopt, 5078 ipp.ipp_rtdstoptslen); 5079 dstopt += ipp.ipp_rtdstoptslen; 5080 udi_size -= toh->len; 5081 } 5082 if (udp->udp_ipv6_recvrthdr && 5083 (ipp.ipp_fields & IPPF_RTHDR)) { 5084 struct T_opthdr *toh; 5085 5086 toh = (struct T_opthdr *)dstopt; 5087 toh->level = IPPROTO_IPV6; 5088 toh->name = IPV6_RTHDR; 5089 toh->len = sizeof (struct T_opthdr) + 5090 ipp.ipp_rthdrlen; 5091 toh->status = 0; 5092 dstopt += sizeof (struct T_opthdr); 5093 bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen); 5094 dstopt += ipp.ipp_rthdrlen; 5095 udi_size -= toh->len; 5096 } 5097 if (udp->udp_ipv6_recvdstopts && 5098 (ipp.ipp_fields & IPPF_DSTOPTS)) { 5099 struct T_opthdr *toh; 5100 5101 toh = (struct T_opthdr *)dstopt; 5102 toh->level = IPPROTO_IPV6; 5103 toh->name = IPV6_DSTOPTS; 5104 toh->len = sizeof (struct T_opthdr) + 5105 ipp.ipp_dstoptslen; 5106 toh->status = 0; 5107 dstopt += sizeof (struct T_opthdr); 5108 bcopy(ipp.ipp_dstopts, dstopt, 5109 ipp.ipp_dstoptslen); 5110 dstopt += ipp.ipp_dstoptslen; 5111 udi_size -= toh->len; 5112 } 5113 5114 if (cr != NULL) { 5115 struct T_opthdr *toh; 5116 5117 toh = (struct T_opthdr *)dstopt; 5118 toh->level = SOL_SOCKET; 5119 toh->name = SCM_UCRED; 5120 toh->len = sizeof (struct T_opthdr) + ucredsize; 5121 toh->status = 0; 5122 (void) cred2ucred(cr, cpid, &toh[1], rcr); 5123 dstopt += toh->len; 5124 udi_size -= toh->len; 5125 } 5126 /* Consumed all of allocated space */ 5127 ASSERT(udi_size == 0); 5128 } 5129 #undef sin6 5130 /* No IP_RECVDSTADDR for IPv6. */ 5131 } 5132 5133 BUMP_MIB(&udp_mib, udpInDatagrams); 5134 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END, 5135 "udp_rput_end: q %p (%S)", q, "end"); 5136 if (options_mp != NULL) 5137 freeb(options_mp); 5138 5139 if (udp->udp_direct_sockfs) { 5140 /* 5141 * There is nothing above us except for the stream head; 5142 * use the read-side synchronous stream interface in 5143 * order to reduce the time spent in interrupt thread. 5144 */ 5145 ASSERT(udp->udp_issocket); 5146 udp_rcv_enqueue(UDP_RD(q), udp, mp, mp_len); 5147 } else { 5148 /* 5149 * Use regular STREAMS interface to pass data upstream 5150 * if this is not a socket endpoint, or if we have 5151 * switched over to the slow mode due to sockmod being 5152 * popped or a module being pushed on top of us. 5153 */ 5154 putnext(UDP_RD(q), mp); 5155 } 5156 return; 5157 5158 tossit: 5159 freemsg(mp); 5160 if (options_mp != NULL) 5161 freeb(options_mp); 5162 BUMP_MIB(&udp_mib, udpInErrors); 5163 } 5164 5165 void 5166 udp_conn_recv(conn_t *connp, mblk_t *mp) 5167 { 5168 _UDP_ENTER(connp, mp, udp_input_wrapper, SQTAG_UDP_FANOUT); 5169 } 5170 5171 /* ARGSUSED */ 5172 static void 5173 udp_input_wrapper(void *arg, mblk_t *mp, void *arg2) 5174 { 5175 udp_input((conn_t *)arg, mp); 5176 _UDP_EXIT((conn_t *)arg); 5177 } 5178 5179 /* 5180 * Process non-M_DATA messages as well as M_DATA messages that requires 5181 * modifications to udp_ip_rcv_options i.e. IPv4 packets with IP options. 5182 */ 5183 static void 5184 udp_rput_other(queue_t *q, mblk_t *mp) 5185 { 5186 struct T_unitdata_ind *tudi; 5187 mblk_t *mp1; 5188 uchar_t *rptr; 5189 uchar_t *new_rptr; 5190 int hdr_length; 5191 int udi_size; /* Size of T_unitdata_ind */ 5192 int opt_len; /* Length of IP options */ 5193 sin_t *sin; 5194 struct T_error_ack *tea; 5195 mblk_t *options_mp = NULL; 5196 in_pktinfo_t *pinfo; 5197 boolean_t recv_on = B_FALSE; 5198 cred_t *cr = NULL; 5199 udp_t *udp = Q_TO_UDP(q); 5200 pid_t cpid; 5201 cred_t *rcr = udp->udp_connp->conn_cred; 5202 5203 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_START, 5204 "udp_rput_other: q %p mp %p", q, mp); 5205 5206 ASSERT(OK_32PTR(mp->b_rptr)); 5207 rptr = mp->b_rptr; 5208 5209 switch (mp->b_datap->db_type) { 5210 case M_CTL: 5211 /* 5212 * We are here only if IP_RECVSLLA and/or IP_RECVIF are set 5213 */ 5214 recv_on = B_TRUE; 5215 options_mp = mp; 5216 pinfo = (in_pktinfo_t *)options_mp->b_rptr; 5217 5218 /* 5219 * The actual data is in mp->b_cont 5220 */ 5221 mp = mp->b_cont; 5222 ASSERT(OK_32PTR(mp->b_rptr)); 5223 rptr = mp->b_rptr; 5224 break; 5225 case M_DATA: 5226 /* 5227 * M_DATA messages contain IPv4 datagrams. They are handled 5228 * after this switch. 5229 */ 5230 break; 5231 case M_PROTO: 5232 case M_PCPROTO: 5233 /* M_PROTO messages contain some type of TPI message. */ 5234 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 5235 if (mp->b_wptr - rptr < sizeof (t_scalar_t)) { 5236 freemsg(mp); 5237 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END, 5238 "udp_rput_other_end: q %p (%S)", q, "protoshort"); 5239 return; 5240 } 5241 tea = (struct T_error_ack *)rptr; 5242 5243 switch (tea->PRIM_type) { 5244 case T_ERROR_ACK: 5245 switch (tea->ERROR_prim) { 5246 case O_T_BIND_REQ: 5247 case T_BIND_REQ: { 5248 /* 5249 * If our O_T_BIND_REQ/T_BIND_REQ fails, 5250 * clear out the associated port and source 5251 * address before passing the message 5252 * upstream. If this was caused by a T_CONN_REQ 5253 * revert back to bound state. 5254 */ 5255 udp_fanout_t *udpf; 5256 5257 udpf = &udp_bind_fanout[ 5258 UDP_BIND_HASH(udp->udp_port)]; 5259 mutex_enter(&udpf->uf_lock); 5260 if (udp->udp_state == TS_DATA_XFER) { 5261 /* Connect failed */ 5262 tea->ERROR_prim = T_CONN_REQ; 5263 /* Revert back to the bound source */ 5264 udp->udp_v6src = udp->udp_bound_v6src; 5265 udp->udp_state = TS_IDLE; 5266 mutex_exit(&udpf->uf_lock); 5267 if (udp->udp_family == AF_INET6) 5268 (void) udp_build_hdrs(q, udp); 5269 break; 5270 } 5271 5272 if (udp->udp_discon_pending) { 5273 tea->ERROR_prim = T_DISCON_REQ; 5274 udp->udp_discon_pending = 0; 5275 } 5276 V6_SET_ZERO(udp->udp_v6src); 5277 V6_SET_ZERO(udp->udp_bound_v6src); 5278 udp->udp_state = TS_UNBND; 5279 udp_bind_hash_remove(udp, B_TRUE); 5280 udp->udp_port = 0; 5281 mutex_exit(&udpf->uf_lock); 5282 if (udp->udp_family == AF_INET6) 5283 (void) udp_build_hdrs(q, udp); 5284 break; 5285 } 5286 default: 5287 break; 5288 } 5289 break; 5290 case T_BIND_ACK: 5291 udp_rput_bind_ack(q, mp); 5292 return; 5293 5294 case T_OPTMGMT_ACK: 5295 case T_OK_ACK: 5296 break; 5297 default: 5298 freemsg(mp); 5299 return; 5300 } 5301 putnext(UDP_RD(q), mp); 5302 return; 5303 } 5304 5305 /* 5306 * This is the inbound data path. 5307 * First, we make sure the data contains both IP and UDP headers. 5308 * 5309 * This handle IPv4 packets for only AF_INET sockets. 5310 * AF_INET6 sockets can never access udp_ip_rcv_options thus there 5311 * is no need saving the options. 5312 */ 5313 ASSERT(IPH_HDR_VERSION((ipha_t *)rptr) == IPV4_VERSION); 5314 hdr_length = IPH_HDR_LENGTH(rptr) + UDPH_SIZE; 5315 if (mp->b_wptr - rptr < hdr_length) { 5316 if (!pullupmsg(mp, hdr_length)) { 5317 freemsg(mp); 5318 if (options_mp != NULL) 5319 freeb(options_mp); 5320 BUMP_MIB(&udp_mib, udpInErrors); 5321 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END, 5322 "udp_rput_other_end: q %p (%S)", q, "hdrshort"); 5323 BUMP_MIB(&udp_mib, udpInErrors); 5324 return; 5325 } 5326 rptr = mp->b_rptr; 5327 } 5328 /* Walk past the headers. */ 5329 new_rptr = rptr + hdr_length; 5330 if (!udp->udp_rcvhdr) 5331 mp->b_rptr = new_rptr; 5332 5333 /* Save the options if any */ 5334 opt_len = hdr_length - (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE); 5335 if (opt_len > 0) { 5336 if (opt_len > udp->udp_ip_rcv_options_len) { 5337 if (udp->udp_ip_rcv_options_len) 5338 mi_free((char *)udp->udp_ip_rcv_options); 5339 udp->udp_ip_rcv_options_len = 0; 5340 udp->udp_ip_rcv_options = 5341 (uchar_t *)mi_alloc(opt_len, BPRI_HI); 5342 if (udp->udp_ip_rcv_options) 5343 udp->udp_ip_rcv_options_len = opt_len; 5344 } 5345 if (udp->udp_ip_rcv_options_len) { 5346 bcopy(rptr + IP_SIMPLE_HDR_LENGTH, 5347 udp->udp_ip_rcv_options, opt_len); 5348 /* Adjust length if we are resusing the space */ 5349 udp->udp_ip_rcv_options_len = opt_len; 5350 } 5351 } else if (udp->udp_ip_rcv_options_len) { 5352 mi_free((char *)udp->udp_ip_rcv_options); 5353 udp->udp_ip_rcv_options = NULL; 5354 udp->udp_ip_rcv_options_len = 0; 5355 } 5356 5357 /* 5358 * Normally only send up the address. 5359 * If IP_RECVDSTADDR is set we include the destination IP 5360 * address as an option. With IP_RECVOPTS we include all 5361 * the IP options. 5362 */ 5363 udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin_t); 5364 if (udp->udp_recvdstaddr) { 5365 udi_size += sizeof (struct T_opthdr) + sizeof (struct in_addr); 5366 UDP_STAT(udp_in_recvdstaddr); 5367 } 5368 if (udp->udp_recvopts && opt_len > 0) { 5369 udi_size += sizeof (struct T_opthdr) + opt_len; 5370 UDP_STAT(udp_in_recvopts); 5371 } 5372 5373 /* 5374 * If the IP_RECVSLLA or the IP_RECVIF is set then allocate 5375 * space accordingly 5376 */ 5377 if (udp->udp_recvif && recv_on && 5378 (pinfo->in_pkt_flags & IPF_RECVIF)) { 5379 udi_size += sizeof (struct T_opthdr) + sizeof (uint_t); 5380 UDP_STAT(udp_in_recvif); 5381 } 5382 5383 if (udp->udp_recvslla && recv_on && 5384 (pinfo->in_pkt_flags & IPF_RECVSLLA)) { 5385 udi_size += sizeof (struct T_opthdr) + 5386 sizeof (struct sockaddr_dl); 5387 UDP_STAT(udp_in_recvslla); 5388 } 5389 5390 if (udp->udp_recvucred && (cr = DB_CRED(mp)) != NULL) { 5391 udi_size += sizeof (struct T_opthdr) + ucredsize; 5392 cpid = DB_CPID(mp); 5393 UDP_STAT(udp_in_recvucred); 5394 } 5395 /* 5396 * If IP_RECVTTL is set allocate the appropriate sized buffer 5397 */ 5398 if (udp->udp_recvttl) { 5399 udi_size += sizeof (struct T_opthdr) + sizeof (uint8_t); 5400 UDP_STAT(udp_in_recvttl); 5401 } 5402 5403 /* Allocate a message block for the T_UNITDATA_IND structure. */ 5404 mp1 = allocb(udi_size, BPRI_MED); 5405 if (mp1 == NULL) { 5406 freemsg(mp); 5407 if (options_mp != NULL) 5408 freeb(options_mp); 5409 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END, 5410 "udp_rput_other_end: q %p (%S)", q, "allocbfail"); 5411 BUMP_MIB(&udp_mib, udpInErrors); 5412 return; 5413 } 5414 mp1->b_cont = mp; 5415 mp = mp1; 5416 mp->b_datap->db_type = M_PROTO; 5417 tudi = (struct T_unitdata_ind *)mp->b_rptr; 5418 mp->b_wptr = (uchar_t *)tudi + udi_size; 5419 tudi->PRIM_type = T_UNITDATA_IND; 5420 tudi->SRC_length = sizeof (sin_t); 5421 tudi->SRC_offset = sizeof (struct T_unitdata_ind); 5422 tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin_t); 5423 udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t)); 5424 tudi->OPT_length = udi_size; 5425 5426 sin = (sin_t *)&tudi[1]; 5427 sin->sin_addr.s_addr = ((ipha_t *)rptr)->ipha_src; 5428 sin->sin_port = ((in_port_t *) 5429 new_rptr)[-(UDPH_SIZE/sizeof (in_port_t))]; 5430 sin->sin_family = AF_INET; 5431 *(uint32_t *)&sin->sin_zero[0] = 0; 5432 *(uint32_t *)&sin->sin_zero[4] = 0; 5433 5434 /* 5435 * Add options if IP_RECVDSTADDR, IP_RECVIF, IP_RECVSLLA or 5436 * IP_RECVTTL has been set. 5437 */ 5438 if (udi_size != 0) { 5439 /* 5440 * Copy in destination address before options to avoid any 5441 * padding issues. 5442 */ 5443 char *dstopt; 5444 5445 dstopt = (char *)&sin[1]; 5446 if (udp->udp_recvdstaddr) { 5447 struct T_opthdr *toh; 5448 ipaddr_t *dstptr; 5449 5450 toh = (struct T_opthdr *)dstopt; 5451 toh->level = IPPROTO_IP; 5452 toh->name = IP_RECVDSTADDR; 5453 toh->len = sizeof (struct T_opthdr) + sizeof (ipaddr_t); 5454 toh->status = 0; 5455 dstopt += sizeof (struct T_opthdr); 5456 dstptr = (ipaddr_t *)dstopt; 5457 *dstptr = (((ipaddr_t *)rptr)[4]); 5458 dstopt += sizeof (ipaddr_t); 5459 udi_size -= toh->len; 5460 } 5461 if (udp->udp_recvopts && udi_size != 0) { 5462 struct T_opthdr *toh; 5463 5464 toh = (struct T_opthdr *)dstopt; 5465 toh->level = IPPROTO_IP; 5466 toh->name = IP_RECVOPTS; 5467 toh->len = sizeof (struct T_opthdr) + opt_len; 5468 toh->status = 0; 5469 dstopt += sizeof (struct T_opthdr); 5470 bcopy(rptr + IP_SIMPLE_HDR_LENGTH, dstopt, opt_len); 5471 dstopt += opt_len; 5472 udi_size -= toh->len; 5473 } 5474 5475 if (udp->udp_recvslla && recv_on && 5476 (pinfo->in_pkt_flags & IPF_RECVSLLA)) { 5477 5478 struct T_opthdr *toh; 5479 struct sockaddr_dl *dstptr; 5480 5481 toh = (struct T_opthdr *)dstopt; 5482 toh->level = IPPROTO_IP; 5483 toh->name = IP_RECVSLLA; 5484 toh->len = sizeof (struct T_opthdr) + 5485 sizeof (struct sockaddr_dl); 5486 toh->status = 0; 5487 dstopt += sizeof (struct T_opthdr); 5488 dstptr = (struct sockaddr_dl *)dstopt; 5489 bcopy(&pinfo->in_pkt_slla, dstptr, 5490 sizeof (struct sockaddr_dl)); 5491 dstopt += sizeof (struct sockaddr_dl); 5492 udi_size -= toh->len; 5493 } 5494 5495 if (udp->udp_recvif && recv_on && 5496 (pinfo->in_pkt_flags & IPF_RECVIF)) { 5497 5498 struct T_opthdr *toh; 5499 uint_t *dstptr; 5500 5501 toh = (struct T_opthdr *)dstopt; 5502 toh->level = IPPROTO_IP; 5503 toh->name = IP_RECVIF; 5504 toh->len = sizeof (struct T_opthdr) + 5505 sizeof (uint_t); 5506 toh->status = 0; 5507 dstopt += sizeof (struct T_opthdr); 5508 dstptr = (uint_t *)dstopt; 5509 *dstptr = pinfo->in_pkt_ifindex; 5510 dstopt += sizeof (uint_t); 5511 udi_size -= toh->len; 5512 } 5513 5514 if (cr != NULL) { 5515 struct T_opthdr *toh; 5516 5517 toh = (struct T_opthdr *)dstopt; 5518 toh->level = SOL_SOCKET; 5519 toh->name = SCM_UCRED; 5520 toh->len = sizeof (struct T_opthdr) + ucredsize; 5521 toh->status = 0; 5522 (void) cred2ucred(cr, cpid, &toh[1], rcr); 5523 dstopt += toh->len; 5524 udi_size -= toh->len; 5525 } 5526 5527 if (udp->udp_recvttl) { 5528 struct T_opthdr *toh; 5529 uint8_t *dstptr; 5530 5531 toh = (struct T_opthdr *)dstopt; 5532 toh->level = IPPROTO_IP; 5533 toh->name = IP_RECVTTL; 5534 toh->len = sizeof (struct T_opthdr) + 5535 sizeof (uint8_t); 5536 toh->status = 0; 5537 dstopt += sizeof (struct T_opthdr); 5538 dstptr = (uint8_t *)dstopt; 5539 *dstptr = ((ipha_t *)rptr)->ipha_ttl; 5540 dstopt += sizeof (uint8_t); 5541 udi_size -= toh->len; 5542 } 5543 5544 ASSERT(udi_size == 0); /* "Consumed" all of allocated space */ 5545 } 5546 BUMP_MIB(&udp_mib, udpInDatagrams); 5547 TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END, 5548 "udp_rput_other_end: q %p (%S)", q, "end"); 5549 if (options_mp != NULL) 5550 freeb(options_mp); 5551 5552 if (udp->udp_direct_sockfs) { 5553 /* 5554 * There is nothing above us except for the stream head; 5555 * use the read-side synchronous stream interface in 5556 * order to reduce the time spent in interrupt thread. 5557 */ 5558 ASSERT(udp->udp_issocket); 5559 udp_rcv_enqueue(UDP_RD(q), udp, mp, msgdsize(mp)); 5560 } else { 5561 /* 5562 * Use regular STREAMS interface to pass data upstream 5563 * if this is not a socket endpoint, or if we have 5564 * switched over to the slow mode due to sockmod being 5565 * popped or a module being pushed on top of us. 5566 */ 5567 putnext(UDP_RD(q), mp); 5568 } 5569 } 5570 5571 /* ARGSUSED */ 5572 static void 5573 udp_rput_other_wrapper(void *arg, mblk_t *mp, void *arg2) 5574 { 5575 conn_t *connp = arg; 5576 5577 udp_rput_other(connp->conn_rq, mp); 5578 udp_exit(connp); 5579 } 5580 5581 /* 5582 * Process a T_BIND_ACK 5583 */ 5584 static void 5585 udp_rput_bind_ack(queue_t *q, mblk_t *mp) 5586 { 5587 udp_t *udp = Q_TO_UDP(q); 5588 mblk_t *mp1; 5589 ire_t *ire; 5590 struct T_bind_ack *tba; 5591 uchar_t *addrp; 5592 ipa_conn_t *ac; 5593 ipa6_conn_t *ac6; 5594 5595 if (udp->udp_discon_pending) 5596 udp->udp_discon_pending = 0; 5597 5598 /* 5599 * If a broadcast/multicast address was bound set 5600 * the source address to 0. 5601 * This ensures no datagrams with broadcast address 5602 * as source address are emitted (which would violate 5603 * RFC1122 - Hosts requirements) 5604 * 5605 * Note that when connecting the returned IRE is 5606 * for the destination address and we only perform 5607 * the broadcast check for the source address (it 5608 * is OK to connect to a broadcast/multicast address.) 5609 */ 5610 mp1 = mp->b_cont; 5611 if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) { 5612 ire = (ire_t *)mp1->b_rptr; 5613 5614 /* 5615 * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast 5616 * local address. 5617 */ 5618 if (ire->ire_type == IRE_BROADCAST && 5619 udp->udp_state != TS_DATA_XFER) { 5620 /* This was just a local bind to a broadcast addr */ 5621 V6_SET_ZERO(udp->udp_v6src); 5622 if (udp->udp_family == AF_INET6) 5623 (void) udp_build_hdrs(q, udp); 5624 } else if (V6_OR_V4_INADDR_ANY(udp->udp_v6src)) { 5625 /* 5626 * Local address not yet set - pick it from the 5627 * T_bind_ack 5628 */ 5629 tba = (struct T_bind_ack *)mp->b_rptr; 5630 addrp = &mp->b_rptr[tba->ADDR_offset]; 5631 switch (udp->udp_family) { 5632 case AF_INET: 5633 if (tba->ADDR_length == sizeof (ipa_conn_t)) { 5634 ac = (ipa_conn_t *)addrp; 5635 } else { 5636 ASSERT(tba->ADDR_length == 5637 sizeof (ipa_conn_x_t)); 5638 ac = &((ipa_conn_x_t *)addrp)->acx_conn; 5639 } 5640 IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr, 5641 &udp->udp_v6src); 5642 break; 5643 case AF_INET6: 5644 if (tba->ADDR_length == sizeof (ipa6_conn_t)) { 5645 ac6 = (ipa6_conn_t *)addrp; 5646 } else { 5647 ASSERT(tba->ADDR_length == 5648 sizeof (ipa6_conn_x_t)); 5649 ac6 = &((ipa6_conn_x_t *) 5650 addrp)->ac6x_conn; 5651 } 5652 udp->udp_v6src = ac6->ac6_laddr; 5653 (void) udp_build_hdrs(q, udp); 5654 break; 5655 } 5656 } 5657 mp1 = mp1->b_cont; 5658 } 5659 /* 5660 * Look for one or more appended ACK message added by 5661 * udp_connect or udp_disconnect. 5662 * If none found just send up the T_BIND_ACK. 5663 * udp_connect has appended a T_OK_ACK and a T_CONN_CON. 5664 * udp_disconnect has appended a T_OK_ACK. 5665 */ 5666 if (mp1 != NULL) { 5667 if (mp->b_cont == mp1) 5668 mp->b_cont = NULL; 5669 else { 5670 ASSERT(mp->b_cont->b_cont == mp1); 5671 mp->b_cont->b_cont = NULL; 5672 } 5673 freemsg(mp); 5674 mp = mp1; 5675 while (mp != NULL) { 5676 mp1 = mp->b_cont; 5677 mp->b_cont = NULL; 5678 putnext(UDP_RD(q), mp); 5679 mp = mp1; 5680 } 5681 return; 5682 } 5683 freemsg(mp->b_cont); 5684 mp->b_cont = NULL; 5685 putnext(UDP_RD(q), mp); 5686 } 5687 5688 /* 5689 * return SNMP stuff in buffer in mpdata 5690 */ 5691 int 5692 udp_snmp_get(queue_t *q, mblk_t *mpctl) 5693 { 5694 mblk_t *mpdata; 5695 mblk_t *mp_conn_ctl; 5696 mblk_t *mp_attr_ctl; 5697 mblk_t *mp6_conn_ctl; 5698 mblk_t *mp6_attr_ctl; 5699 mblk_t *mp_conn_tail; 5700 mblk_t *mp_attr_tail; 5701 mblk_t *mp6_conn_tail; 5702 mblk_t *mp6_attr_tail; 5703 struct opthdr *optp; 5704 mib2_udpEntry_t ude; 5705 mib2_udp6Entry_t ude6; 5706 mib2_transportMLPEntry_t mlp; 5707 int state; 5708 zoneid_t zoneid; 5709 int i; 5710 connf_t *connfp; 5711 conn_t *connp = Q_TO_CONN(q); 5712 udp_t *udp = connp->conn_udp; 5713 int v4_conn_idx; 5714 int v6_conn_idx; 5715 boolean_t needattr; 5716 5717 mp_conn_ctl = mp_attr_ctl = mp6_conn_ctl = NULL; 5718 if (mpctl == NULL || 5719 (mpdata = mpctl->b_cont) == NULL || 5720 (mp_conn_ctl = copymsg(mpctl)) == NULL || 5721 (mp_attr_ctl = copymsg(mpctl)) == NULL || 5722 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 5723 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 5724 freemsg(mp_conn_ctl); 5725 freemsg(mp_attr_ctl); 5726 freemsg(mp6_conn_ctl); 5727 return (0); 5728 } 5729 5730 zoneid = connp->conn_zoneid; 5731 5732 /* fixed length structure for IPv4 and IPv6 counters */ 5733 SET_MIB(udp_mib.udpEntrySize, sizeof (mib2_udpEntry_t)); 5734 SET_MIB(udp_mib.udp6EntrySize, sizeof (mib2_udp6Entry_t)); 5735 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 5736 optp->level = MIB2_UDP; 5737 optp->name = 0; 5738 (void) snmp_append_data(mpdata, (char *)&udp_mib, sizeof (udp_mib)); 5739 optp->len = msgdsize(mpdata); 5740 qreply(q, mpctl); 5741 5742 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 5743 v4_conn_idx = v6_conn_idx = 0; 5744 5745 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 5746 connfp = &ipcl_globalhash_fanout[i]; 5747 connp = NULL; 5748 5749 while ((connp = ipcl_get_next_conn(connfp, connp, 5750 IPCL_UDP))) { 5751 udp = connp->conn_udp; 5752 if (zoneid != connp->conn_zoneid) 5753 continue; 5754 5755 /* 5756 * Note that the port numbers are sent in 5757 * host byte order 5758 */ 5759 5760 if (udp->udp_state == TS_UNBND) 5761 state = MIB2_UDP_unbound; 5762 else if (udp->udp_state == TS_IDLE) 5763 state = MIB2_UDP_idle; 5764 else if (udp->udp_state == TS_DATA_XFER) 5765 state = MIB2_UDP_connected; 5766 else 5767 state = MIB2_UDP_unknown; 5768 5769 needattr = B_FALSE; 5770 bzero(&mlp, sizeof (mlp)); 5771 if (connp->conn_mlp_type != mlptSingle) { 5772 if (connp->conn_mlp_type == mlptShared || 5773 connp->conn_mlp_type == mlptBoth) 5774 mlp.tme_flags |= MIB2_TMEF_SHARED; 5775 if (connp->conn_mlp_type == mlptPrivate || 5776 connp->conn_mlp_type == mlptBoth) 5777 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 5778 needattr = B_TRUE; 5779 } 5780 5781 /* 5782 * Create an IPv4 table entry for IPv4 entries and also 5783 * any IPv6 entries which are bound to in6addr_any 5784 * (i.e. anything a IPv4 peer could connect/send to). 5785 */ 5786 if (udp->udp_ipversion == IPV4_VERSION || 5787 (udp->udp_state <= TS_IDLE && 5788 IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src))) { 5789 ude.udpEntryInfo.ue_state = state; 5790 /* 5791 * If in6addr_any this will set it to 5792 * INADDR_ANY 5793 */ 5794 ude.udpLocalAddress = 5795 V4_PART_OF_V6(udp->udp_v6src); 5796 ude.udpLocalPort = ntohs(udp->udp_port); 5797 if (udp->udp_state == TS_DATA_XFER) { 5798 /* 5799 * Can potentially get here for 5800 * v6 socket if another process 5801 * (say, ping) has just done a 5802 * sendto(), changing the state 5803 * from the TS_IDLE above to 5804 * TS_DATA_XFER by the time we hit 5805 * this part of the code. 5806 */ 5807 ude.udpEntryInfo.ue_RemoteAddress = 5808 V4_PART_OF_V6(udp->udp_v6dst); 5809 ude.udpEntryInfo.ue_RemotePort = 5810 ntohs(udp->udp_dstport); 5811 } else { 5812 ude.udpEntryInfo.ue_RemoteAddress = 0; 5813 ude.udpEntryInfo.ue_RemotePort = 0; 5814 } 5815 (void) snmp_append_data2(mp_conn_ctl->b_cont, 5816 &mp_conn_tail, (char *)&ude, sizeof (ude)); 5817 mlp.tme_connidx = v4_conn_idx++; 5818 if (needattr) 5819 (void) snmp_append_data2( 5820 mp_attr_ctl->b_cont, &mp_attr_tail, 5821 (char *)&mlp, sizeof (mlp)); 5822 } 5823 if (udp->udp_ipversion == IPV6_VERSION) { 5824 ude6.udp6EntryInfo.ue_state = state; 5825 ude6.udp6LocalAddress = udp->udp_v6src; 5826 ude6.udp6LocalPort = ntohs(udp->udp_port); 5827 ude6.udp6IfIndex = udp->udp_bound_if; 5828 if (udp->udp_state == TS_DATA_XFER) { 5829 ude6.udp6EntryInfo.ue_RemoteAddress = 5830 udp->udp_v6dst; 5831 ude6.udp6EntryInfo.ue_RemotePort = 5832 ntohs(udp->udp_dstport); 5833 } else { 5834 ude6.udp6EntryInfo.ue_RemoteAddress = 5835 sin6_null.sin6_addr; 5836 ude6.udp6EntryInfo.ue_RemotePort = 0; 5837 } 5838 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 5839 &mp6_conn_tail, (char *)&ude6, 5840 sizeof (ude6)); 5841 mlp.tme_connidx = v6_conn_idx++; 5842 if (needattr) 5843 (void) snmp_append_data2( 5844 mp6_attr_ctl->b_cont, 5845 &mp6_attr_tail, (char *)&mlp, 5846 sizeof (mlp)); 5847 } 5848 } 5849 } 5850 5851 /* IPv4 UDP endpoints */ 5852 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 5853 sizeof (struct T_optmgmt_ack)]; 5854 optp->level = MIB2_UDP; 5855 optp->name = MIB2_UDP_ENTRY; 5856 optp->len = msgdsize(mp_conn_ctl->b_cont); 5857 qreply(q, mp_conn_ctl); 5858 5859 /* table of MLP attributes... */ 5860 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 5861 sizeof (struct T_optmgmt_ack)]; 5862 optp->level = MIB2_UDP; 5863 optp->name = EXPER_XPORT_MLP; 5864 optp->len = msgdsize(mp_attr_ctl->b_cont); 5865 if (optp->len == 0) 5866 freemsg(mp_attr_ctl); 5867 else 5868 qreply(q, mp_attr_ctl); 5869 5870 /* IPv6 UDP endpoints */ 5871 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 5872 sizeof (struct T_optmgmt_ack)]; 5873 optp->level = MIB2_UDP6; 5874 optp->name = MIB2_UDP6_ENTRY; 5875 optp->len = msgdsize(mp6_conn_ctl->b_cont); 5876 qreply(q, mp6_conn_ctl); 5877 5878 /* table of MLP attributes... */ 5879 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 5880 sizeof (struct T_optmgmt_ack)]; 5881 optp->level = MIB2_UDP6; 5882 optp->name = EXPER_XPORT_MLP; 5883 optp->len = msgdsize(mp6_attr_ctl->b_cont); 5884 if (optp->len == 0) 5885 freemsg(mp6_attr_ctl); 5886 else 5887 qreply(q, mp6_attr_ctl); 5888 5889 return (1); 5890 } 5891 5892 /* 5893 * Return 0 if invalid set request, 1 otherwise, including non-udp requests. 5894 * NOTE: Per MIB-II, UDP has no writable data. 5895 * TODO: If this ever actually tries to set anything, it needs to be 5896 * to do the appropriate locking. 5897 */ 5898 /* ARGSUSED */ 5899 int 5900 udp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name, 5901 uchar_t *ptr, int len) 5902 { 5903 switch (level) { 5904 case MIB2_UDP: 5905 return (0); 5906 default: 5907 return (1); 5908 } 5909 } 5910 5911 static void 5912 udp_report_item(mblk_t *mp, udp_t *udp) 5913 { 5914 char *state; 5915 char addrbuf1[INET6_ADDRSTRLEN]; 5916 char addrbuf2[INET6_ADDRSTRLEN]; 5917 uint_t print_len, buf_len; 5918 5919 buf_len = mp->b_datap->db_lim - mp->b_wptr; 5920 ASSERT(buf_len >= 0); 5921 if (buf_len == 0) 5922 return; 5923 5924 if (udp->udp_state == TS_UNBND) 5925 state = "UNBOUND"; 5926 else if (udp->udp_state == TS_IDLE) 5927 state = "IDLE"; 5928 else if (udp->udp_state == TS_DATA_XFER) 5929 state = "CONNECTED"; 5930 else 5931 state = "UnkState"; 5932 print_len = snprintf((char *)mp->b_wptr, buf_len, 5933 MI_COL_PTRFMT_STR "%4d %5u %s %s %5u %s\n", 5934 (void *)udp, udp->udp_connp->conn_zoneid, ntohs(udp->udp_port), 5935 inet_ntop(AF_INET6, &udp->udp_v6src, 5936 addrbuf1, sizeof (addrbuf1)), 5937 inet_ntop(AF_INET6, &udp->udp_v6dst, 5938 addrbuf2, sizeof (addrbuf2)), 5939 ntohs(udp->udp_dstport), state); 5940 if (print_len < buf_len) { 5941 mp->b_wptr += print_len; 5942 } else { 5943 mp->b_wptr += buf_len; 5944 } 5945 } 5946 5947 /* Report for ndd "udp_status" */ 5948 /* ARGSUSED */ 5949 static int 5950 udp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 5951 { 5952 zoneid_t zoneid; 5953 connf_t *connfp; 5954 conn_t *connp = Q_TO_CONN(q); 5955 udp_t *udp = connp->conn_udp; 5956 int i; 5957 5958 /* 5959 * Because of the ndd constraint, at most we can have 64K buffer 5960 * to put in all UDP info. So to be more efficient, just 5961 * allocate a 64K buffer here, assuming we need that large buffer. 5962 * This may be a problem as any user can read udp_status. Therefore 5963 * we limit the rate of doing this using udp_ndd_get_info_interval. 5964 * This should be OK as normal users should not do this too often. 5965 */ 5966 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 5967 if (ddi_get_lbolt() - udp_last_ndd_get_info_time < 5968 drv_usectohz(udp_ndd_get_info_interval * 1000)) { 5969 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 5970 return (0); 5971 } 5972 } 5973 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 5974 /* The following may work even if we cannot get a large buf. */ 5975 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 5976 return (0); 5977 } 5978 (void) mi_mpprintf(mp, 5979 "UDP " MI_COL_HDRPAD_STR 5980 /* 12345678[89ABCDEF] */ 5981 " zone lport src addr dest addr port state"); 5982 /* 1234 12345 xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx 12345 UNBOUND */ 5983 5984 zoneid = connp->conn_zoneid; 5985 5986 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 5987 connfp = &ipcl_globalhash_fanout[i]; 5988 connp = NULL; 5989 5990 while ((connp = ipcl_get_next_conn(connfp, connp, 5991 IPCL_UDP))) { 5992 udp = connp->conn_udp; 5993 if (zoneid != GLOBAL_ZONEID && 5994 zoneid != connp->conn_zoneid) 5995 continue; 5996 5997 udp_report_item(mp->b_cont, udp); 5998 } 5999 } 6000 udp_last_ndd_get_info_time = ddi_get_lbolt(); 6001 return (0); 6002 } 6003 6004 /* 6005 * This routine creates a T_UDERROR_IND message and passes it upstream. 6006 * The address and options are copied from the T_UNITDATA_REQ message 6007 * passed in mp. This message is freed. 6008 */ 6009 static void 6010 udp_ud_err(queue_t *q, mblk_t *mp, uchar_t *destaddr, t_scalar_t destlen, 6011 t_scalar_t err) 6012 { 6013 struct T_unitdata_req *tudr; 6014 mblk_t *mp1; 6015 uchar_t *optaddr; 6016 t_scalar_t optlen; 6017 6018 if (DB_TYPE(mp) == M_DATA) { 6019 ASSERT(destaddr != NULL && destlen != 0); 6020 optaddr = NULL; 6021 optlen = 0; 6022 } else { 6023 if ((mp->b_wptr < mp->b_rptr) || 6024 (MBLKL(mp)) < sizeof (struct T_unitdata_req)) { 6025 goto done; 6026 } 6027 tudr = (struct T_unitdata_req *)mp->b_rptr; 6028 destaddr = mp->b_rptr + tudr->DEST_offset; 6029 if (destaddr < mp->b_rptr || destaddr >= mp->b_wptr || 6030 destaddr + tudr->DEST_length < mp->b_rptr || 6031 destaddr + tudr->DEST_length > mp->b_wptr) { 6032 goto done; 6033 } 6034 optaddr = mp->b_rptr + tudr->OPT_offset; 6035 if (optaddr < mp->b_rptr || optaddr >= mp->b_wptr || 6036 optaddr + tudr->OPT_length < mp->b_rptr || 6037 optaddr + tudr->OPT_length > mp->b_wptr) { 6038 goto done; 6039 } 6040 destlen = tudr->DEST_length; 6041 optlen = tudr->OPT_length; 6042 } 6043 6044 mp1 = mi_tpi_uderror_ind((char *)destaddr, destlen, 6045 (char *)optaddr, optlen, err); 6046 if (mp1 != NULL) 6047 putnext(UDP_RD(q), mp1); 6048 6049 done: 6050 freemsg(mp); 6051 } 6052 6053 /* 6054 * This routine removes a port number association from a stream. It 6055 * is called by udp_wput to handle T_UNBIND_REQ messages. 6056 */ 6057 static void 6058 udp_unbind(queue_t *q, mblk_t *mp) 6059 { 6060 udp_t *udp = Q_TO_UDP(q); 6061 6062 /* If a bind has not been done, we can't unbind. */ 6063 if (udp->udp_state == TS_UNBND) { 6064 udp_err_ack(q, mp, TOUTSTATE, 0); 6065 return; 6066 } 6067 if (cl_inet_unbind != NULL) { 6068 /* 6069 * Running in cluster mode - register unbind information 6070 */ 6071 if (udp->udp_ipversion == IPV4_VERSION) { 6072 (*cl_inet_unbind)(IPPROTO_UDP, AF_INET, 6073 (uint8_t *)(&V4_PART_OF_V6(udp->udp_v6src)), 6074 (in_port_t)udp->udp_port); 6075 } else { 6076 (*cl_inet_unbind)(IPPROTO_UDP, AF_INET6, 6077 (uint8_t *)&(udp->udp_v6src), 6078 (in_port_t)udp->udp_port); 6079 } 6080 } 6081 6082 udp_bind_hash_remove(udp, B_FALSE); 6083 V6_SET_ZERO(udp->udp_v6src); 6084 V6_SET_ZERO(udp->udp_bound_v6src); 6085 udp->udp_port = 0; 6086 udp->udp_state = TS_UNBND; 6087 6088 if (udp->udp_family == AF_INET6) { 6089 int error; 6090 6091 /* Rebuild the header template */ 6092 error = udp_build_hdrs(q, udp); 6093 if (error != 0) { 6094 udp_err_ack(q, mp, TSYSERR, error); 6095 return; 6096 } 6097 } 6098 /* 6099 * Pass the unbind to IP; T_UNBIND_REQ is larger than T_OK_ACK 6100 * and therefore ip_unbind must never return NULL. 6101 */ 6102 mp = ip_unbind(q, mp); 6103 ASSERT(mp != NULL); 6104 putnext(UDP_RD(q), mp); 6105 } 6106 6107 /* 6108 * Don't let port fall into the privileged range. 6109 * Since the extra privileged ports can be arbitrary we also 6110 * ensure that we exclude those from consideration. 6111 * udp_g_epriv_ports is not sorted thus we loop over it until 6112 * there are no changes. 6113 */ 6114 static in_port_t 6115 udp_update_next_port(udp_t *udp, in_port_t port, boolean_t random) 6116 { 6117 int i; 6118 in_port_t nextport; 6119 boolean_t restart = B_FALSE; 6120 6121 if (random && udp_random_anon_port != 0) { 6122 (void) random_get_pseudo_bytes((uint8_t *)&port, 6123 sizeof (in_port_t)); 6124 /* 6125 * Unless changed by a sys admin, the smallest anon port 6126 * is 32768 and the largest anon port is 65535. It is 6127 * very likely (50%) for the random port to be smaller 6128 * than the smallest anon port. When that happens, 6129 * add port % (anon port range) to the smallest anon 6130 * port to get the random port. It should fall into the 6131 * valid anon port range. 6132 */ 6133 if (port < udp_smallest_anon_port) { 6134 port = udp_smallest_anon_port + 6135 port % (udp_largest_anon_port - 6136 udp_smallest_anon_port); 6137 } 6138 } 6139 6140 retry: 6141 if (port < udp_smallest_anon_port) 6142 port = udp_smallest_anon_port; 6143 6144 if (port > udp_largest_anon_port) { 6145 port = udp_smallest_anon_port; 6146 if (restart) 6147 return (0); 6148 restart = B_TRUE; 6149 } 6150 6151 if (port < udp_smallest_nonpriv_port) 6152 port = udp_smallest_nonpriv_port; 6153 6154 for (i = 0; i < udp_g_num_epriv_ports; i++) { 6155 if (port == udp_g_epriv_ports[i]) { 6156 port++; 6157 /* 6158 * Make sure that the port is in the 6159 * valid range. 6160 */ 6161 goto retry; 6162 } 6163 } 6164 6165 if (is_system_labeled() && 6166 (nextport = tsol_next_port(crgetzone(udp->udp_connp->conn_cred), 6167 port, IPPROTO_UDP, B_TRUE)) != 0) { 6168 port = nextport; 6169 goto retry; 6170 } 6171 6172 return (port); 6173 } 6174 6175 static int 6176 udp_update_label(queue_t *wq, mblk_t *mp, ipaddr_t dst) 6177 { 6178 int err; 6179 uchar_t opt_storage[IP_MAX_OPT_LENGTH]; 6180 udp_t *udp = Q_TO_UDP(wq); 6181 6182 err = tsol_compute_label(DB_CREDDEF(mp, udp->udp_connp->conn_cred), dst, 6183 opt_storage, udp->udp_mac_exempt); 6184 if (err == 0) { 6185 err = tsol_update_options(&udp->udp_ip_snd_options, 6186 &udp->udp_ip_snd_options_len, &udp->udp_label_len, 6187 opt_storage); 6188 } 6189 if (err != 0) { 6190 DTRACE_PROBE4( 6191 tx__ip__log__info__updatelabel__udp, 6192 char *, "queue(1) failed to update options(2) on mp(3)", 6193 queue_t *, wq, char *, opt_storage, mblk_t *, mp); 6194 } else { 6195 IN6_IPADDR_TO_V4MAPPED(dst, &udp->udp_v6lastdst); 6196 } 6197 return (err); 6198 } 6199 6200 static mblk_t * 6201 udp_output_v4(conn_t *connp, mblk_t *mp, ipaddr_t v4dst, uint16_t port, 6202 uint_t srcid, int *error) 6203 { 6204 udp_t *udp = connp->conn_udp; 6205 queue_t *q = connp->conn_wq; 6206 mblk_t *mp1 = mp; 6207 mblk_t *mp2; 6208 ipha_t *ipha; 6209 int ip_hdr_length; 6210 uint32_t ip_len; 6211 udpha_t *udpha; 6212 udpattrs_t attrs; 6213 6214 *error = 0; 6215 6216 if (v4dst == INADDR_ANY) 6217 v4dst = htonl(INADDR_LOOPBACK); 6218 6219 /* 6220 * If options passed in, feed it for verification and handling 6221 */ 6222 attrs.udpattr_credset = B_FALSE; 6223 if (DB_TYPE(mp) != M_DATA) { 6224 mp1 = mp->b_cont; 6225 if (((struct T_unitdata_req *)mp->b_rptr)->OPT_length != 0) { 6226 attrs.udpattr_ipp = NULL; 6227 attrs.udpattr_mb = mp; 6228 if (udp_unitdata_opt_process(q, mp, error, &attrs) < 0) 6229 goto done; 6230 /* 6231 * Note: success in processing options. 6232 * mp option buffer represented by 6233 * OPT_length/offset now potentially modified 6234 * and contain option setting results 6235 */ 6236 ASSERT(*error == 0); 6237 } 6238 } 6239 6240 /* mp1 points to the M_DATA mblk carrying the packet */ 6241 ASSERT(mp1 != NULL && DB_TYPE(mp1) == M_DATA); 6242 6243 /* Check if our saved options are valid; update if not */ 6244 if (is_system_labeled()) { 6245 /* Using UDP MLP requires SCM_UCRED from user */ 6246 if (connp->conn_mlp_type != mlptSingle && 6247 !attrs.udpattr_credset) { 6248 DTRACE_PROBE4( 6249 tx__ip__log__info__output__udp, 6250 char *, "MLP mp(1) lacks SCM_UCRED attr(2) on q(3)", 6251 mblk_t *, mp1, udpattrs_t *, &attrs, queue_t *, q); 6252 *error = ECONNREFUSED; 6253 goto done; 6254 } 6255 if ((!IN6_IS_ADDR_V4MAPPED(&udp->udp_v6lastdst) || 6256 V4_PART_OF_V6(udp->udp_v6lastdst) != v4dst) && 6257 (*error = udp_update_label(q, mp, v4dst)) != 0) 6258 goto done; 6259 } 6260 6261 /* Add an IP header */ 6262 ip_hdr_length = IP_SIMPLE_HDR_LENGTH + UDPH_SIZE + 6263 udp->udp_ip_snd_options_len; 6264 ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length]; 6265 if (DB_REF(mp1) != 1 || (uchar_t *)ipha < DB_BASE(mp1) || 6266 !OK_32PTR(ipha)) { 6267 mp2 = allocb(ip_hdr_length + udp_wroff_extra, BPRI_LO); 6268 if (mp2 == NULL) { 6269 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6270 "udp_wput_end: q %p (%S)", q, "allocbfail2"); 6271 *error = ENOMEM; 6272 goto done; 6273 } 6274 mp2->b_wptr = DB_LIM(mp2); 6275 mp2->b_cont = mp1; 6276 mp1 = mp2; 6277 if (DB_TYPE(mp) != M_DATA) 6278 mp->b_cont = mp1; 6279 else 6280 mp = mp1; 6281 6282 ipha = (ipha_t *)(mp1->b_wptr - ip_hdr_length); 6283 } 6284 ip_hdr_length -= UDPH_SIZE; 6285 #ifdef _BIG_ENDIAN 6286 /* Set version, header length, and tos */ 6287 *(uint16_t *)&ipha->ipha_version_and_hdr_length = 6288 ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) | 6289 udp->udp_type_of_service); 6290 /* Set ttl and protocol */ 6291 *(uint16_t *)&ipha->ipha_ttl = (udp->udp_ttl << 8) | IPPROTO_UDP; 6292 #else 6293 /* Set version, header length, and tos */ 6294 *(uint16_t *)&ipha->ipha_version_and_hdr_length = 6295 ((udp->udp_type_of_service << 8) | 6296 ((IP_VERSION << 4) | (ip_hdr_length>>2))); 6297 /* Set ttl and protocol */ 6298 *(uint16_t *)&ipha->ipha_ttl = (IPPROTO_UDP << 8) | udp->udp_ttl; 6299 #endif 6300 /* 6301 * Copy our address into the packet. If this is zero, 6302 * first look at __sin6_src_id for a hint. If we leave the source 6303 * as INADDR_ANY then ip will fill in the real source address. 6304 */ 6305 IN6_V4MAPPED_TO_IPADDR(&udp->udp_v6src, ipha->ipha_src); 6306 if (srcid != 0 && ipha->ipha_src == INADDR_ANY) { 6307 in6_addr_t v6src; 6308 6309 ip_srcid_find_id(srcid, &v6src, connp->conn_zoneid); 6310 IN6_V4MAPPED_TO_IPADDR(&v6src, ipha->ipha_src); 6311 } 6312 6313 ipha->ipha_fragment_offset_and_flags = 0; 6314 ipha->ipha_ident = 0; 6315 6316 mp1->b_rptr = (uchar_t *)ipha; 6317 6318 ASSERT((uintptr_t)(mp1->b_wptr - (uchar_t *)ipha) <= 6319 (uintptr_t)UINT_MAX); 6320 6321 /* Determine length of packet */ 6322 ip_len = (uint32_t)(mp1->b_wptr - (uchar_t *)ipha); 6323 if ((mp2 = mp1->b_cont) != NULL) { 6324 do { 6325 ASSERT((uintptr_t)MBLKL(mp2) <= (uintptr_t)UINT_MAX); 6326 ip_len += (uint32_t)MBLKL(mp2); 6327 } while ((mp2 = mp2->b_cont) != NULL); 6328 } 6329 /* 6330 * If the size of the packet is greater than the maximum allowed by 6331 * ip, return an error. Passing this down could cause panics because 6332 * the size will have wrapped and be inconsistent with the msg size. 6333 */ 6334 if (ip_len > IP_MAXPACKET) { 6335 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6336 "udp_wput_end: q %p (%S)", q, "IP length exceeded"); 6337 *error = EMSGSIZE; 6338 goto done; 6339 } 6340 ipha->ipha_length = htons((uint16_t)ip_len); 6341 ip_len -= ip_hdr_length; 6342 ip_len = htons((uint16_t)ip_len); 6343 udpha = (udpha_t *)(((uchar_t *)ipha) + ip_hdr_length); 6344 6345 /* 6346 * Copy in the destination address 6347 */ 6348 ipha->ipha_dst = v4dst; 6349 6350 /* 6351 * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic. 6352 */ 6353 if (CLASSD(v4dst)) 6354 ipha->ipha_ttl = udp->udp_multicast_ttl; 6355 6356 udpha->uha_dst_port = port; 6357 udpha->uha_src_port = udp->udp_port; 6358 6359 if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) { 6360 uint32_t cksum; 6361 6362 bcopy(udp->udp_ip_snd_options, &ipha[1], 6363 udp->udp_ip_snd_options_len); 6364 /* 6365 * Massage source route putting first source route in ipha_dst. 6366 * Ignore the destination in T_unitdata_req. 6367 * Create a checksum adjustment for a source route, if any. 6368 */ 6369 cksum = ip_massage_options(ipha); 6370 cksum = (cksum & 0xFFFF) + (cksum >> 16); 6371 cksum -= ((ipha->ipha_dst >> 16) & 0xFFFF) + 6372 (ipha->ipha_dst & 0xFFFF); 6373 if ((int)cksum < 0) 6374 cksum--; 6375 cksum = (cksum & 0xFFFF) + (cksum >> 16); 6376 /* 6377 * IP does the checksum if uha_checksum is non-zero, 6378 * We make it easy for IP to include our pseudo header 6379 * by putting our length in uha_checksum. 6380 */ 6381 cksum += ip_len; 6382 cksum = (cksum & 0xFFFF) + (cksum >> 16); 6383 /* There might be a carry. */ 6384 cksum = (cksum & 0xFFFF) + (cksum >> 16); 6385 #ifdef _LITTLE_ENDIAN 6386 if (udp_do_checksum) 6387 ip_len = (cksum << 16) | ip_len; 6388 #else 6389 if (udp_do_checksum) 6390 ip_len = (ip_len << 16) | cksum; 6391 else 6392 ip_len <<= 16; 6393 #endif 6394 } else { 6395 /* 6396 * IP does the checksum if uha_checksum is non-zero, 6397 * We make it easy for IP to include our pseudo header 6398 * by putting our length in uha_checksum. 6399 */ 6400 if (udp_do_checksum) 6401 ip_len |= (ip_len << 16); 6402 #ifndef _LITTLE_ENDIAN 6403 else 6404 ip_len <<= 16; 6405 #endif 6406 } 6407 /* Set UDP length and checksum */ 6408 *((uint32_t *)&udpha->uha_length) = ip_len; 6409 if (DB_CRED(mp) != NULL) 6410 mblk_setcred(mp1, DB_CRED(mp)); 6411 6412 if (DB_TYPE(mp) != M_DATA) { 6413 ASSERT(mp != mp1); 6414 freeb(mp); 6415 } 6416 6417 /* mp has been consumed and we'll return success */ 6418 ASSERT(*error == 0); 6419 mp = NULL; 6420 6421 /* We're done. Pass the packet to ip. */ 6422 BUMP_MIB(&udp_mib, udpOutDatagrams); 6423 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6424 "udp_wput_end: q %p (%S)", q, "end"); 6425 6426 if ((connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 6427 CONN_OUTBOUND_POLICY_PRESENT(connp) || 6428 connp->conn_dontroute || connp->conn_xmit_if_ill != NULL || 6429 connp->conn_nofailover_ill != NULL || 6430 connp->conn_outgoing_ill != NULL || 6431 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 6432 IPP_ENABLED(IPP_LOCAL_OUT) || ip_g_mrouter != NULL) { 6433 UDP_STAT(udp_ip_send); 6434 ip_output(connp, mp1, connp->conn_wq, IP_WPUT); 6435 } else { 6436 udp_send_data(udp, connp->conn_wq, mp1, ipha); 6437 } 6438 6439 done: 6440 if (*error != 0) { 6441 ASSERT(mp != NULL); 6442 BUMP_MIB(&udp_mib, udpOutErrors); 6443 } 6444 return (mp); 6445 } 6446 6447 static void 6448 udp_send_data(udp_t *udp, queue_t *q, mblk_t *mp, ipha_t *ipha) 6449 { 6450 conn_t *connp = udp->udp_connp; 6451 ipaddr_t src, dst; 6452 ill_t *ill; 6453 ire_t *ire; 6454 ipif_t *ipif = NULL; 6455 mblk_t *ire_fp_mp; 6456 uint_t ire_fp_mp_len; 6457 uint16_t *up; 6458 uint32_t cksum, hcksum_txflags; 6459 queue_t *dev_q; 6460 boolean_t retry_caching; 6461 6462 dst = ipha->ipha_dst; 6463 src = ipha->ipha_src; 6464 ASSERT(ipha->ipha_ident == 0); 6465 6466 if (CLASSD(dst)) { 6467 int err; 6468 6469 ipif = conn_get_held_ipif(connp, 6470 &connp->conn_multicast_ipif, &err); 6471 6472 if (ipif == NULL || ipif->ipif_isv6 || 6473 (ipif->ipif_ill->ill_phyint->phyint_flags & 6474 PHYI_LOOPBACK)) { 6475 if (ipif != NULL) 6476 ipif_refrele(ipif); 6477 UDP_STAT(udp_ip_send); 6478 ip_output(connp, mp, q, IP_WPUT); 6479 return; 6480 } 6481 } 6482 6483 retry_caching = B_FALSE; 6484 mutex_enter(&connp->conn_lock); 6485 ire = connp->conn_ire_cache; 6486 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 6487 6488 if (ire == NULL || ire->ire_addr != dst || 6489 (ire->ire_marks & IRE_MARK_CONDEMNED)) { 6490 retry_caching = B_TRUE; 6491 } else if (CLASSD(dst) && (ire->ire_type & IRE_CACHE)) { 6492 ill_t *stq_ill = (ill_t *)ire->ire_stq->q_ptr; 6493 6494 ASSERT(ipif != NULL); 6495 if (stq_ill != ipif->ipif_ill && (stq_ill->ill_group == NULL || 6496 stq_ill->ill_group != ipif->ipif_ill->ill_group)) 6497 retry_caching = B_TRUE; 6498 } 6499 6500 if (!retry_caching) { 6501 ASSERT(ire != NULL); 6502 IRE_REFHOLD(ire); 6503 mutex_exit(&connp->conn_lock); 6504 } else { 6505 boolean_t cached = B_FALSE; 6506 6507 connp->conn_ire_cache = NULL; 6508 mutex_exit(&connp->conn_lock); 6509 6510 /* Release the old ire */ 6511 if (ire != NULL) { 6512 IRE_REFRELE_NOTR(ire); 6513 ire = NULL; 6514 } 6515 6516 if (CLASSD(dst)) { 6517 ASSERT(ipif != NULL); 6518 ire = ire_ctable_lookup(dst, 0, 0, ipif, 6519 connp->conn_zoneid, MBLK_GETLABEL(mp), 6520 MATCH_IRE_ILL_GROUP); 6521 } else { 6522 ASSERT(ipif == NULL); 6523 ire = ire_cache_lookup(dst, connp->conn_zoneid, 6524 MBLK_GETLABEL(mp)); 6525 } 6526 6527 if (ire == NULL) { 6528 if (ipif != NULL) 6529 ipif_refrele(ipif); 6530 UDP_STAT(udp_ire_null); 6531 ip_output(connp, mp, q, IP_WPUT); 6532 return; 6533 } 6534 IRE_REFHOLD_NOTR(ire); 6535 6536 mutex_enter(&connp->conn_lock); 6537 if (!(connp->conn_state_flags & CONN_CLOSING) && 6538 connp->conn_ire_cache == NULL) { 6539 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 6540 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 6541 connp->conn_ire_cache = ire; 6542 cached = B_TRUE; 6543 } 6544 rw_exit(&ire->ire_bucket->irb_lock); 6545 } 6546 mutex_exit(&connp->conn_lock); 6547 6548 /* 6549 * We can continue to use the ire but since it was not 6550 * cached, we should drop the extra reference. 6551 */ 6552 if (!cached) 6553 IRE_REFRELE_NOTR(ire); 6554 } 6555 ASSERT(ire != NULL && ire->ire_ipversion == IPV4_VERSION); 6556 ASSERT(!CLASSD(dst) || ipif != NULL); 6557 6558 if ((ire->ire_type & (IRE_BROADCAST|IRE_LOCAL|IRE_LOOPBACK)) || 6559 (ire->ire_flags & RTF_MULTIRT) || ire->ire_stq == NULL || 6560 ire->ire_max_frag < ntohs(ipha->ipha_length) || 6561 (ire_fp_mp = ire->ire_fp_mp) == NULL || 6562 (connp->conn_nexthop_set) || 6563 (ire_fp_mp_len = MBLKL(ire_fp_mp)) > MBLKHEAD(mp)) { 6564 if (ipif != NULL) 6565 ipif_refrele(ipif); 6566 UDP_STAT(udp_ip_ire_send); 6567 IRE_REFRELE(ire); 6568 ip_output(connp, mp, q, IP_WPUT); 6569 return; 6570 } 6571 6572 BUMP_MIB(&ip_mib, ipOutRequests); 6573 6574 ill = ire_to_ill(ire); 6575 ASSERT(ill != NULL); 6576 6577 dev_q = ire->ire_stq->q_next; 6578 ASSERT(dev_q != NULL); 6579 /* 6580 * If the service thread is already running, or if the driver 6581 * queue is currently flow-controlled, queue this packet. 6582 */ 6583 if ((q->q_first != NULL || connp->conn_draining) || 6584 ((dev_q->q_next || dev_q->q_first) && !canput(dev_q))) { 6585 if (ip_output_queue) { 6586 (void) putq(q, mp); 6587 } else { 6588 BUMP_MIB(&ip_mib, ipOutDiscards); 6589 freemsg(mp); 6590 } 6591 if (ipif != NULL) 6592 ipif_refrele(ipif); 6593 IRE_REFRELE(ire); 6594 return; 6595 } 6596 6597 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 6598 #ifndef _BIG_ENDIAN 6599 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 6600 #endif 6601 6602 if (src == INADDR_ANY && !connp->conn_unspec_src) { 6603 if (CLASSD(dst) && !(ire->ire_flags & RTF_SETSRC)) 6604 src = ipha->ipha_src = ipif->ipif_src_addr; 6605 else 6606 src = ipha->ipha_src = ire->ire_src_addr; 6607 } 6608 6609 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 6610 ASSERT(ill->ill_hcksum_capab != NULL); 6611 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 6612 } else { 6613 hcksum_txflags = 0; 6614 } 6615 6616 /* pseudo-header checksum (do it in parts for IP header checksum) */ 6617 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 6618 6619 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 6620 up = IPH_UDPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 6621 if (*up != 0) { 6622 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, 6623 mp, ipha, up, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH, 6624 ntohs(ipha->ipha_length), cksum); 6625 6626 /* Software checksum? */ 6627 if (DB_CKSUMFLAGS(mp) == 0) { 6628 UDP_STAT(udp_out_sw_cksum); 6629 UDP_STAT_UPDATE(udp_out_sw_cksum_bytes, 6630 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 6631 } 6632 } 6633 6634 ipha->ipha_fragment_offset_and_flags |= 6635 (uint32_t)htons(ire->ire_frag_flag); 6636 6637 /* Calculate IP header checksum if hardware isn't capable */ 6638 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 6639 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 6640 ((uint16_t *)ipha)[4]); 6641 } 6642 6643 if (CLASSD(dst)) { 6644 ilm_t *ilm; 6645 6646 ILM_WALKER_HOLD(ill); 6647 ilm = ilm_lookup_ill(ill, dst, ALL_ZONES); 6648 ILM_WALKER_RELE(ill); 6649 if (ilm != NULL) { 6650 ip_multicast_loopback(q, ill, mp, 6651 connp->conn_multicast_loop ? 0 : 6652 IP_FF_NO_MCAST_LOOP, connp->conn_zoneid); 6653 } 6654 6655 /* If multicast TTL is 0 then we are done */ 6656 if (ipha->ipha_ttl == 0) { 6657 if (ipif != NULL) 6658 ipif_refrele(ipif); 6659 freemsg(mp); 6660 IRE_REFRELE(ire); 6661 return; 6662 } 6663 } 6664 6665 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 6666 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 6667 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 6668 6669 UPDATE_OB_PKT_COUNT(ire); 6670 ire->ire_last_used_time = lbolt; 6671 6672 if (ILL_DLS_CAPABLE(ill)) { 6673 /* 6674 * Send the packet directly to DLD, where it may be queued 6675 * depending on the availability of transmit resources at 6676 * the media layer. 6677 */ 6678 IP_DLS_ILL_TX(ill, mp); 6679 } else { 6680 putnext(ire->ire_stq, mp); 6681 } 6682 6683 if (ipif != NULL) 6684 ipif_refrele(ipif); 6685 IRE_REFRELE(ire); 6686 } 6687 6688 static boolean_t 6689 udp_update_label_v6(queue_t *wq, mblk_t *mp, in6_addr_t *dst) 6690 { 6691 udp_t *udp = Q_TO_UDP(wq); 6692 int err; 6693 uchar_t opt_storage[TSOL_MAX_IPV6_OPTION]; 6694 6695 err = tsol_compute_label_v6(DB_CREDDEF(mp, udp->udp_connp->conn_cred), 6696 dst, opt_storage, udp->udp_mac_exempt); 6697 if (err == 0) { 6698 err = tsol_update_sticky(&udp->udp_sticky_ipp, 6699 &udp->udp_label_len_v6, opt_storage); 6700 } 6701 if (err != 0) { 6702 DTRACE_PROBE4( 6703 tx__ip__log__drop__updatelabel__udp6, 6704 char *, "queue(1) failed to update options(2) on mp(3)", 6705 queue_t *, wq, char *, opt_storage, mblk_t *, mp); 6706 } else { 6707 udp->udp_v6lastdst = *dst; 6708 } 6709 return (err); 6710 } 6711 6712 /* 6713 * This routine handles all messages passed downstream. It either 6714 * consumes the message or passes it downstream; it never queues a 6715 * a message. 6716 */ 6717 static void 6718 udp_output(conn_t *connp, mblk_t *mp, struct sockaddr *addr, socklen_t addrlen) 6719 { 6720 sin6_t *sin6; 6721 sin_t *sin; 6722 ipaddr_t v4dst; 6723 uint16_t port; 6724 uint_t srcid; 6725 queue_t *q = connp->conn_wq; 6726 udp_t *udp = connp->conn_udp; 6727 int error = 0; 6728 struct sockaddr_storage ss; 6729 6730 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_START, 6731 "udp_wput_start: connp %p mp %p", connp, mp); 6732 6733 /* 6734 * We directly handle several cases here: T_UNITDATA_REQ message 6735 * coming down as M_PROTO/M_PCPROTO and M_DATA messages for both 6736 * connected and non-connected socket. The latter carries the 6737 * address structure along when this routine gets called. 6738 */ 6739 switch (DB_TYPE(mp)) { 6740 case M_DATA: 6741 if (!udp->udp_direct_sockfs || udp->udp_state != TS_DATA_XFER) { 6742 if (!udp->udp_direct_sockfs || 6743 addr == NULL || addrlen == 0) { 6744 /* Not connected; address is required */ 6745 BUMP_MIB(&udp_mib, udpOutErrors); 6746 UDP_STAT(udp_out_err_notconn); 6747 freemsg(mp); 6748 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6749 "udp_wput_end: connp %p (%S)", connp, 6750 "not-connected; address required"); 6751 return; 6752 } 6753 ASSERT(udp->udp_issocket); 6754 UDP_DBGSTAT(udp_data_notconn); 6755 /* Not connected; do some more checks below */ 6756 break; 6757 } 6758 /* M_DATA for connected socket */ 6759 UDP_DBGSTAT(udp_data_conn); 6760 IN6_V4MAPPED_TO_IPADDR(&udp->udp_v6dst, v4dst); 6761 6762 /* Initialize addr and addrlen as if they're passed in */ 6763 if (udp->udp_family == AF_INET) { 6764 sin = (sin_t *)&ss; 6765 sin->sin_family = AF_INET; 6766 sin->sin_port = udp->udp_dstport; 6767 sin->sin_addr.s_addr = v4dst; 6768 addr = (struct sockaddr *)sin; 6769 addrlen = sizeof (*sin); 6770 } else { 6771 sin6 = (sin6_t *)&ss; 6772 sin6->sin6_family = AF_INET6; 6773 sin6->sin6_port = udp->udp_dstport; 6774 sin6->sin6_flowinfo = udp->udp_flowinfo; 6775 sin6->sin6_addr = udp->udp_v6dst; 6776 sin6->sin6_scope_id = 0; 6777 sin6->__sin6_src_id = 0; 6778 addr = (struct sockaddr *)sin6; 6779 addrlen = sizeof (*sin6); 6780 } 6781 6782 if (udp->udp_family == AF_INET || 6783 IN6_IS_ADDR_V4MAPPED(&udp->udp_v6dst)) { 6784 /* 6785 * Handle both AF_INET and AF_INET6; the latter 6786 * for IPV4 mapped destination addresses. Note 6787 * here that both addr and addrlen point to the 6788 * corresponding struct depending on the address 6789 * family of the socket. 6790 */ 6791 mp = udp_output_v4(connp, mp, v4dst, 6792 udp->udp_dstport, 0, &error); 6793 } else { 6794 mp = udp_output_v6(connp, mp, sin6, &error); 6795 } 6796 if (error != 0) { 6797 ASSERT(addr != NULL && addrlen != 0); 6798 goto ud_error; 6799 } 6800 return; 6801 case M_PROTO: 6802 case M_PCPROTO: { 6803 struct T_unitdata_req *tudr; 6804 6805 ASSERT((uintptr_t)MBLKL(mp) <= (uintptr_t)INT_MAX); 6806 tudr = (struct T_unitdata_req *)mp->b_rptr; 6807 6808 /* Handle valid T_UNITDATA_REQ here */ 6809 if (MBLKL(mp) >= sizeof (*tudr) && 6810 ((t_primp_t)mp->b_rptr)->type == T_UNITDATA_REQ) { 6811 if (mp->b_cont == NULL) { 6812 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6813 "udp_wput_end: q %p (%S)", q, "badaddr"); 6814 error = EPROTO; 6815 goto ud_error; 6816 } 6817 6818 if (!MBLKIN(mp, 0, tudr->DEST_offset + 6819 tudr->DEST_length)) { 6820 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6821 "udp_wput_end: q %p (%S)", q, "badaddr"); 6822 error = EADDRNOTAVAIL; 6823 goto ud_error; 6824 } 6825 /* 6826 * If a port has not been bound to the stream, fail. 6827 * This is not a problem when sockfs is directly 6828 * above us, because it will ensure that the socket 6829 * is first bound before allowing data to be sent. 6830 */ 6831 if (udp->udp_state == TS_UNBND) { 6832 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6833 "udp_wput_end: q %p (%S)", q, "outstate"); 6834 error = EPROTO; 6835 goto ud_error; 6836 } 6837 addr = (struct sockaddr *) 6838 &mp->b_rptr[tudr->DEST_offset]; 6839 addrlen = tudr->DEST_length; 6840 if (tudr->OPT_length != 0) 6841 UDP_STAT(udp_out_opt); 6842 break; 6843 } 6844 /* FALLTHRU */ 6845 } 6846 default: 6847 udp_become_writer(connp, mp, udp_wput_other_wrapper, 6848 SQTAG_UDP_OUTPUT); 6849 return; 6850 } 6851 ASSERT(addr != NULL); 6852 6853 switch (udp->udp_family) { 6854 case AF_INET6: 6855 sin6 = (sin6_t *)addr; 6856 if (!OK_32PTR((char *)sin6) || addrlen != sizeof (sin6_t) || 6857 sin6->sin6_family != AF_INET6) { 6858 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6859 "udp_wput_end: q %p (%S)", q, "badaddr"); 6860 error = EADDRNOTAVAIL; 6861 goto ud_error; 6862 } 6863 6864 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6865 /* 6866 * Destination is a non-IPv4-compatible IPv6 address. 6867 * Send out an IPv6 format packet. 6868 */ 6869 mp = udp_output_v6(connp, mp, sin6, &error); 6870 if (error != 0) 6871 goto ud_error; 6872 6873 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6874 "udp_wput_end: q %p (%S)", q, "udp_output_v6"); 6875 return; 6876 } 6877 /* 6878 * If the local address is not zero or a mapped address 6879 * return an error. It would be possible to send an IPv4 6880 * packet but the response would never make it back to the 6881 * application since it is bound to a non-mapped address. 6882 */ 6883 if (!IN6_IS_ADDR_V4MAPPED(&udp->udp_v6src) && 6884 !IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src)) { 6885 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6886 "udp_wput_end: q %p (%S)", q, "badaddr"); 6887 error = EADDRNOTAVAIL; 6888 goto ud_error; 6889 } 6890 /* Send IPv4 packet without modifying udp_ipversion */ 6891 /* Extract port and ipaddr */ 6892 port = sin6->sin6_port; 6893 IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr, v4dst); 6894 srcid = sin6->__sin6_src_id; 6895 break; 6896 6897 case AF_INET: 6898 sin = (sin_t *)addr; 6899 if (!OK_32PTR((char *)sin) || addrlen != sizeof (sin_t) || 6900 sin->sin_family != AF_INET) { 6901 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END, 6902 "udp_wput_end: q %p (%S)", q, "badaddr"); 6903 error = EADDRNOTAVAIL; 6904 goto ud_error; 6905 } 6906 /* Extract port and ipaddr */ 6907 port = sin->sin_port; 6908 v4dst = sin->sin_addr.s_addr; 6909 srcid = 0; 6910 break; 6911 } 6912 6913 mp = udp_output_v4(connp, mp, v4dst, port, srcid, &error); 6914 if (error != 0) { 6915 ud_error: 6916 UDP_STAT(udp_out_err_output); 6917 ASSERT(mp != NULL); 6918 /* mp is freed by the following routine */ 6919 udp_ud_err(q, mp, (uchar_t *)addr, (t_scalar_t)addrlen, 6920 (t_scalar_t)error); 6921 } 6922 } 6923 6924 /* ARGSUSED */ 6925 static void 6926 udp_output_wrapper(void *arg, mblk_t *mp, void *arg2) 6927 { 6928 udp_output((conn_t *)arg, mp, NULL, 0); 6929 _UDP_EXIT((conn_t *)arg); 6930 } 6931 6932 static void 6933 udp_wput(queue_t *q, mblk_t *mp) 6934 { 6935 _UDP_ENTER(Q_TO_CONN(UDP_WR(q)), mp, udp_output_wrapper, 6936 SQTAG_UDP_WPUT); 6937 } 6938 6939 /* 6940 * Allocate and prepare a T_UNITDATA_REQ message. 6941 */ 6942 static mblk_t * 6943 udp_tudr_alloc(struct sockaddr *addr, socklen_t addrlen) 6944 { 6945 struct T_unitdata_req *tudr; 6946 mblk_t *mp; 6947 6948 mp = allocb(sizeof (*tudr) + addrlen, BPRI_MED); 6949 if (mp != NULL) { 6950 mp->b_wptr += sizeof (*tudr) + addrlen; 6951 DB_TYPE(mp) = M_PROTO; 6952 6953 tudr = (struct T_unitdata_req *)mp->b_rptr; 6954 tudr->PRIM_type = T_UNITDATA_REQ; 6955 tudr->DEST_length = addrlen; 6956 tudr->DEST_offset = (t_scalar_t)sizeof (*tudr); 6957 tudr->OPT_length = 0; 6958 tudr->OPT_offset = 0; 6959 bcopy(addr, tudr+1, addrlen); 6960 } 6961 return (mp); 6962 } 6963 6964 /* 6965 * Entry point for sockfs when udp is in "direct sockfs" mode. This mode 6966 * is valid when we are directly beneath the stream head, and thus sockfs 6967 * is able to bypass STREAMS and directly call us, passing along the sockaddr 6968 * structure without the cumbersome T_UNITDATA_REQ interface. Note that 6969 * this is done for both connected and non-connected endpoint. 6970 */ 6971 void 6972 udp_wput_data(queue_t *q, mblk_t *mp, struct sockaddr *addr, socklen_t addrlen) 6973 { 6974 conn_t *connp; 6975 udp_t *udp; 6976 6977 q = UDP_WR(q); 6978 connp = Q_TO_CONN(q); 6979 udp = connp->conn_udp; 6980 6981 /* udpsockfs should only send down M_DATA for this entry point */ 6982 ASSERT(DB_TYPE(mp) == M_DATA); 6983 6984 mutex_enter(&connp->conn_lock); 6985 UDP_MODE_ASSERTIONS(udp, UDP_ENTER); 6986 6987 if (udp->udp_mode != UDP_MT_HOT) { 6988 /* 6989 * We can't enter this conn right away because another 6990 * thread is currently executing as writer; therefore we 6991 * need to deposit the message into the squeue to be 6992 * drained later. If a socket address is present, we 6993 * need to create a T_UNITDATA_REQ message as placeholder. 6994 */ 6995 if (addr != NULL && addrlen != 0) { 6996 mblk_t *tudr_mp = udp_tudr_alloc(addr, addrlen); 6997 6998 if (tudr_mp == NULL) { 6999 mutex_exit(&connp->conn_lock); 7000 BUMP_MIB(&udp_mib, udpOutErrors); 7001 UDP_STAT(udp_out_err_tudr); 7002 freemsg(mp); 7003 return; 7004 } 7005 /* Tag the packet with T_UNITDATA_REQ */ 7006 tudr_mp->b_cont = mp; 7007 mp = tudr_mp; 7008 } 7009 mutex_exit(&connp->conn_lock); 7010 udp_enter(connp, mp, udp_output_wrapper, SQTAG_UDP_WPUT); 7011 return; 7012 } 7013 7014 /* We can execute as reader right away. */ 7015 UDP_READERS_INCREF(udp); 7016 mutex_exit(&connp->conn_lock); 7017 7018 udp_output(connp, mp, addr, addrlen); 7019 7020 udp_exit(connp); 7021 } 7022 7023 /* 7024 * udp_output_v6(): 7025 * Assumes that udp_wput did some sanity checking on the destination 7026 * address. 7027 */ 7028 static mblk_t * 7029 udp_output_v6(conn_t *connp, mblk_t *mp, sin6_t *sin6, int *error) 7030 { 7031 ip6_t *ip6h; 7032 ip6i_t *ip6i; /* mp1->b_rptr even if no ip6i_t */ 7033 mblk_t *mp1 = mp; 7034 mblk_t *mp2; 7035 int udp_ip_hdr_len = IPV6_HDR_LEN + UDPH_SIZE; 7036 size_t ip_len; 7037 udpha_t *udph; 7038 udp_t *udp = connp->conn_udp; 7039 queue_t *q = connp->conn_wq; 7040 ip6_pkt_t ipp_s; /* For ancillary data options */ 7041 ip6_pkt_t *ipp = &ipp_s; 7042 ip6_pkt_t *tipp; /* temporary ipp */ 7043 uint32_t csum = 0; 7044 uint_t ignore = 0; 7045 uint_t option_exists = 0, is_sticky = 0; 7046 uint8_t *cp; 7047 uint8_t *nxthdr_ptr; 7048 in6_addr_t ip6_dst; 7049 udpattrs_t attrs; 7050 boolean_t opt_present; 7051 7052 *error = 0; 7053 7054 /* 7055 * If the local address is a mapped address return 7056 * an error. 7057 * It would be possible to send an IPv6 packet but the 7058 * response would never make it back to the application 7059 * since it is bound to a mapped address. 7060 */ 7061 if (IN6_IS_ADDR_V4MAPPED(&udp->udp_v6src)) { 7062 *error = EADDRNOTAVAIL; 7063 goto done; 7064 } 7065 7066 ipp->ipp_fields = 0; 7067 ipp->ipp_sticky_ignored = 0; 7068 7069 /* 7070 * If TPI options passed in, feed it for verification and handling 7071 */ 7072 attrs.udpattr_credset = B_FALSE; 7073 opt_present = B_FALSE; 7074 if (DB_TYPE(mp) != M_DATA) { 7075 mp1 = mp->b_cont; 7076 if (((struct T_unitdata_req *)mp->b_rptr)->OPT_length != 0) { 7077 attrs.udpattr_ipp = ipp; 7078 attrs.udpattr_mb = mp; 7079 if (udp_unitdata_opt_process(q, mp, error, &attrs) < 0) 7080 goto done; 7081 ASSERT(*error == 0); 7082 opt_present = B_TRUE; 7083 } 7084 } 7085 ignore = ipp->ipp_sticky_ignored; 7086 7087 /* mp1 points to the M_DATA mblk carrying the packet */ 7088 ASSERT(mp1 != NULL && DB_TYPE(mp1) == M_DATA); 7089 7090 if (sin6->sin6_scope_id != 0 && 7091 IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 7092 /* 7093 * IPPF_SCOPE_ID is special. It's neither a sticky 7094 * option nor ancillary data. It needs to be 7095 * explicitly set in options_exists. 7096 */ 7097 option_exists |= IPPF_SCOPE_ID; 7098 } 7099 7100 /* 7101 * Compute the destination address 7102 */ 7103 ip6_dst = sin6->sin6_addr; 7104 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 7105 ip6_dst = ipv6_loopback; 7106 7107 /* 7108 * If we're not going to the same destination as last time, then 7109 * recompute the label required. This is done in a separate routine to 7110 * avoid blowing up our stack here. 7111 */ 7112 if (is_system_labeled()) { 7113 /* Using UDP MLP requires SCM_UCRED from user */ 7114 if (connp->conn_mlp_type != mlptSingle && 7115 !attrs.udpattr_credset) { 7116 DTRACE_PROBE4( 7117 tx__ip__log__info__output__udp6, 7118 char *, "MLP mp(1) lacks SCM_UCRED attr(2) on q(3)", 7119 mblk_t *, mp1, udpattrs_t *, &attrs, queue_t *, q); 7120 *error = ECONNREFUSED; 7121 goto done; 7122 } 7123 if ((opt_present || 7124 !IN6_ARE_ADDR_EQUAL(&udp->udp_v6lastdst, &ip6_dst)) && 7125 (*error = udp_update_label_v6(q, mp, &ip6_dst)) != 0) 7126 goto done; 7127 } 7128 7129 /* 7130 * If there's a security label here, then we ignore any options the 7131 * user may try to set. We keep the peer's label as a hidden sticky 7132 * option. 7133 */ 7134 if (udp->udp_label_len_v6 > 0) { 7135 ignore &= ~IPPF_HOPOPTS; 7136 ipp->ipp_fields &= ~IPPF_HOPOPTS; 7137 } 7138 7139 if ((udp->udp_sticky_ipp.ipp_fields == 0) && (ipp->ipp_fields == 0)) { 7140 /* No sticky options nor ancillary data. */ 7141 goto no_options; 7142 } 7143 7144 /* 7145 * Go through the options figuring out where each is going to 7146 * come from and build two masks. The first mask indicates if 7147 * the option exists at all. The second mask indicates if the 7148 * option is sticky or ancillary. 7149 */ 7150 if (!(ignore & IPPF_HOPOPTS)) { 7151 if (ipp->ipp_fields & IPPF_HOPOPTS) { 7152 option_exists |= IPPF_HOPOPTS; 7153 udp_ip_hdr_len += ipp->ipp_hopoptslen; 7154 } else if (udp->udp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) { 7155 option_exists |= IPPF_HOPOPTS; 7156 is_sticky |= IPPF_HOPOPTS; 7157 udp_ip_hdr_len += udp->udp_sticky_ipp.ipp_hopoptslen; 7158 } 7159 } 7160 7161 if (!(ignore & IPPF_RTHDR)) { 7162 if (ipp->ipp_fields & IPPF_RTHDR) { 7163 option_exists |= IPPF_RTHDR; 7164 udp_ip_hdr_len += ipp->ipp_rthdrlen; 7165 } else if (udp->udp_sticky_ipp.ipp_fields & IPPF_RTHDR) { 7166 option_exists |= IPPF_RTHDR; 7167 is_sticky |= IPPF_RTHDR; 7168 udp_ip_hdr_len += udp->udp_sticky_ipp.ipp_rthdrlen; 7169 } 7170 } 7171 7172 if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) { 7173 if (ipp->ipp_fields & IPPF_RTDSTOPTS) { 7174 option_exists |= IPPF_RTDSTOPTS; 7175 udp_ip_hdr_len += ipp->ipp_rtdstoptslen; 7176 } else if (udp->udp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) { 7177 option_exists |= IPPF_RTDSTOPTS; 7178 is_sticky |= IPPF_RTDSTOPTS; 7179 udp_ip_hdr_len += udp->udp_sticky_ipp.ipp_rtdstoptslen; 7180 } 7181 } 7182 7183 if (!(ignore & IPPF_DSTOPTS)) { 7184 if (ipp->ipp_fields & IPPF_DSTOPTS) { 7185 option_exists |= IPPF_DSTOPTS; 7186 udp_ip_hdr_len += ipp->ipp_dstoptslen; 7187 } else if (udp->udp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) { 7188 option_exists |= IPPF_DSTOPTS; 7189 is_sticky |= IPPF_DSTOPTS; 7190 udp_ip_hdr_len += udp->udp_sticky_ipp.ipp_dstoptslen; 7191 } 7192 } 7193 7194 if (!(ignore & IPPF_IFINDEX)) { 7195 if (ipp->ipp_fields & IPPF_IFINDEX) { 7196 option_exists |= IPPF_IFINDEX; 7197 } else if (udp->udp_sticky_ipp.ipp_fields & IPPF_IFINDEX) { 7198 option_exists |= IPPF_IFINDEX; 7199 is_sticky |= IPPF_IFINDEX; 7200 } 7201 } 7202 7203 if (!(ignore & IPPF_ADDR)) { 7204 if (ipp->ipp_fields & IPPF_ADDR) { 7205 option_exists |= IPPF_ADDR; 7206 } else if (udp->udp_sticky_ipp.ipp_fields & IPPF_ADDR) { 7207 option_exists |= IPPF_ADDR; 7208 is_sticky |= IPPF_ADDR; 7209 } 7210 } 7211 7212 if (!(ignore & IPPF_DONTFRAG)) { 7213 if (ipp->ipp_fields & IPPF_DONTFRAG) { 7214 option_exists |= IPPF_DONTFRAG; 7215 } else if (udp->udp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) { 7216 option_exists |= IPPF_DONTFRAG; 7217 is_sticky |= IPPF_DONTFRAG; 7218 } 7219 } 7220 7221 if (!(ignore & IPPF_USE_MIN_MTU)) { 7222 if (ipp->ipp_fields & IPPF_USE_MIN_MTU) { 7223 option_exists |= IPPF_USE_MIN_MTU; 7224 } else if (udp->udp_sticky_ipp.ipp_fields & 7225 IPPF_USE_MIN_MTU) { 7226 option_exists |= IPPF_USE_MIN_MTU; 7227 is_sticky |= IPPF_USE_MIN_MTU; 7228 } 7229 } 7230 7231 if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT)) 7232 option_exists |= IPPF_HOPLIMIT; 7233 /* IPV6_HOPLIMIT can never be sticky */ 7234 ASSERT(!(udp->udp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT)); 7235 7236 if (!(ignore & IPPF_UNICAST_HOPS) && 7237 (udp->udp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) { 7238 option_exists |= IPPF_UNICAST_HOPS; 7239 is_sticky |= IPPF_UNICAST_HOPS; 7240 } 7241 7242 if (!(ignore & IPPF_MULTICAST_HOPS) && 7243 (udp->udp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) { 7244 option_exists |= IPPF_MULTICAST_HOPS; 7245 is_sticky |= IPPF_MULTICAST_HOPS; 7246 } 7247 7248 if (!(ignore & IPPF_TCLASS)) { 7249 if (ipp->ipp_fields & IPPF_TCLASS) { 7250 option_exists |= IPPF_TCLASS; 7251 } else if (udp->udp_sticky_ipp.ipp_fields & IPPF_TCLASS) { 7252 option_exists |= IPPF_TCLASS; 7253 is_sticky |= IPPF_TCLASS; 7254 } 7255 } 7256 7257 no_options: 7258 7259 /* 7260 * If any options carried in the ip6i_t were specified, we 7261 * need to account for the ip6i_t in the data we'll be sending 7262 * down. 7263 */ 7264 if (option_exists & IPPF_HAS_IP6I) 7265 udp_ip_hdr_len += sizeof (ip6i_t); 7266 7267 /* check/fix buffer config, setup pointers into it */ 7268 ip6h = (ip6_t *)&mp1->b_rptr[-udp_ip_hdr_len]; 7269 if (DB_REF(mp1) != 1 || ((unsigned char *)ip6h < DB_BASE(mp1)) || 7270 !OK_32PTR(ip6h)) { 7271 /* Try to get everything in a single mblk next time */ 7272 if (udp_ip_hdr_len > udp->udp_max_hdr_len) { 7273 udp->udp_max_hdr_len = udp_ip_hdr_len; 7274 (void) mi_set_sth_wroff(UDP_RD(q), 7275 udp->udp_max_hdr_len + udp_wroff_extra); 7276 } 7277 mp2 = allocb(udp_ip_hdr_len + udp_wroff_extra, BPRI_LO); 7278 if (mp2 == NULL) { 7279 *error = ENOMEM; 7280 goto done; 7281 } 7282 mp2->b_wptr = DB_LIM(mp2); 7283 mp2->b_cont = mp1; 7284 mp1 = mp2; 7285 if (DB_TYPE(mp) != M_DATA) 7286 mp->b_cont = mp1; 7287 else 7288 mp = mp1; 7289 7290 ip6h = (ip6_t *)(mp1->b_wptr - udp_ip_hdr_len); 7291 } 7292 mp1->b_rptr = (unsigned char *)ip6h; 7293 ip6i = (ip6i_t *)ip6h; 7294 7295 #define ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &udp->udp_sticky_ipp : ipp) 7296 if (option_exists & IPPF_HAS_IP6I) { 7297 ip6h = (ip6_t *)&ip6i[1]; 7298 ip6i->ip6i_flags = 0; 7299 ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 7300 7301 /* sin6_scope_id takes precendence over IPPF_IFINDEX */ 7302 if (option_exists & IPPF_SCOPE_ID) { 7303 ip6i->ip6i_flags |= IP6I_IFINDEX; 7304 ip6i->ip6i_ifindex = sin6->sin6_scope_id; 7305 } else if (option_exists & IPPF_IFINDEX) { 7306 tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX); 7307 ASSERT(tipp->ipp_ifindex != 0); 7308 ip6i->ip6i_flags |= IP6I_IFINDEX; 7309 ip6i->ip6i_ifindex = tipp->ipp_ifindex; 7310 } 7311 7312 if (option_exists & IPPF_ADDR) { 7313 /* 7314 * Enable per-packet source address verification if 7315 * IPV6_PKTINFO specified the source address. 7316 * ip6_src is set in the transport's _wput function. 7317 */ 7318 ip6i->ip6i_flags |= IP6I_VERIFY_SRC; 7319 } 7320 7321 if (option_exists & IPPF_DONTFRAG) { 7322 ip6i->ip6i_flags |= IP6I_DONTFRAG; 7323 } 7324 7325 if (option_exists & IPPF_USE_MIN_MTU) { 7326 ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU( 7327 ip6i->ip6i_flags, ipp->ipp_use_min_mtu); 7328 } 7329 7330 if (option_exists & IPPF_NEXTHOP) { 7331 tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP); 7332 ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop)); 7333 ip6i->ip6i_flags |= IP6I_NEXTHOP; 7334 ip6i->ip6i_nexthop = tipp->ipp_nexthop; 7335 } 7336 7337 /* 7338 * tell IP this is an ip6i_t private header 7339 */ 7340 ip6i->ip6i_nxt = IPPROTO_RAW; 7341 } 7342 7343 /* Initialize IPv6 header */ 7344 ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 7345 bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src)); 7346 7347 /* Set the hoplimit of the outgoing packet. */ 7348 if (option_exists & IPPF_HOPLIMIT) { 7349 /* IPV6_HOPLIMIT ancillary data overrides all other settings. */ 7350 ip6h->ip6_hops = ipp->ipp_hoplimit; 7351 ip6i->ip6i_flags |= IP6I_HOPLIMIT; 7352 } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 7353 ip6h->ip6_hops = udp->udp_multicast_ttl; 7354 if (option_exists & IPPF_MULTICAST_HOPS) 7355 ip6i->ip6i_flags |= IP6I_HOPLIMIT; 7356 } else { 7357 ip6h->ip6_hops = udp->udp_ttl; 7358 if (option_exists & IPPF_UNICAST_HOPS) 7359 ip6i->ip6i_flags |= IP6I_HOPLIMIT; 7360 } 7361 7362 if (option_exists & IPPF_ADDR) { 7363 tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR); 7364 ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr)); 7365 ip6h->ip6_src = tipp->ipp_addr; 7366 } else { 7367 /* 7368 * The source address was not set using IPV6_PKTINFO. 7369 * First look at the bound source. 7370 * If unspecified fallback to __sin6_src_id. 7371 */ 7372 ip6h->ip6_src = udp->udp_v6src; 7373 if (sin6->__sin6_src_id != 0 && 7374 IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) { 7375 ip_srcid_find_id(sin6->__sin6_src_id, 7376 &ip6h->ip6_src, connp->conn_zoneid); 7377 } 7378 } 7379 7380 nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt; 7381 cp = (uint8_t *)&ip6h[1]; 7382 7383 /* 7384 * Here's where we have to start stringing together 7385 * any extension headers in the right order: 7386 * Hop-by-hop, destination, routing, and final destination opts. 7387 */ 7388 if (option_exists & IPPF_HOPOPTS) { 7389 /* Hop-by-hop options */ 7390 ip6_hbh_t *hbh = (ip6_hbh_t *)cp; 7391 tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS); 7392 7393 *nxthdr_ptr = IPPROTO_HOPOPTS; 7394 nxthdr_ptr = &hbh->ip6h_nxt; 7395 7396 bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen); 7397 cp += tipp->ipp_hopoptslen; 7398 } 7399 /* 7400 * En-route destination options 7401 * Only do them if there's a routing header as well 7402 */ 7403 if (option_exists & IPPF_RTDSTOPTS) { 7404 ip6_dest_t *dst = (ip6_dest_t *)cp; 7405 tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS); 7406 7407 *nxthdr_ptr = IPPROTO_DSTOPTS; 7408 nxthdr_ptr = &dst->ip6d_nxt; 7409 7410 bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen); 7411 cp += tipp->ipp_rtdstoptslen; 7412 } 7413 /* 7414 * Routing header next 7415 */ 7416 if (option_exists & IPPF_RTHDR) { 7417 ip6_rthdr_t *rt = (ip6_rthdr_t *)cp; 7418 tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR); 7419 7420 *nxthdr_ptr = IPPROTO_ROUTING; 7421 nxthdr_ptr = &rt->ip6r_nxt; 7422 7423 bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen); 7424 cp += tipp->ipp_rthdrlen; 7425 } 7426 /* 7427 * Do ultimate destination options 7428 */ 7429 if (option_exists & IPPF_DSTOPTS) { 7430 ip6_dest_t *dest = (ip6_dest_t *)cp; 7431 tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS); 7432 7433 *nxthdr_ptr = IPPROTO_DSTOPTS; 7434 nxthdr_ptr = &dest->ip6d_nxt; 7435 7436 bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen); 7437 cp += tipp->ipp_dstoptslen; 7438 } 7439 /* 7440 * Now set the last header pointer to the proto passed in 7441 */ 7442 ASSERT((int)(cp - (uint8_t *)ip6i) == (udp_ip_hdr_len - UDPH_SIZE)); 7443 *nxthdr_ptr = IPPROTO_UDP; 7444 7445 /* Update UDP header */ 7446 udph = (udpha_t *)((uchar_t *)ip6i + udp_ip_hdr_len - UDPH_SIZE); 7447 udph->uha_dst_port = sin6->sin6_port; 7448 udph->uha_src_port = udp->udp_port; 7449 7450 /* 7451 * Copy in the destination address 7452 */ 7453 ip6h->ip6_dst = ip6_dst; 7454 7455 ip6h->ip6_vcf = 7456 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 7457 (sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 7458 7459 if (option_exists & IPPF_TCLASS) { 7460 tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS); 7461 ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf, 7462 tipp->ipp_tclass); 7463 } 7464 7465 if (option_exists & IPPF_RTHDR) { 7466 ip6_rthdr_t *rth; 7467 7468 /* 7469 * Perform any processing needed for source routing. 7470 * We know that all extension headers will be in the same mblk 7471 * as the IPv6 header. 7472 */ 7473 rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr); 7474 if (rth != NULL && rth->ip6r_segleft != 0) { 7475 if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) { 7476 /* 7477 * Drop packet - only support Type 0 routing. 7478 * Notify the application as well. 7479 */ 7480 *error = EPROTO; 7481 goto done; 7482 } 7483 7484 /* 7485 * rth->ip6r_len is twice the number of 7486 * addresses in the header. Thus it must be even. 7487 */ 7488 if (rth->ip6r_len & 0x1) { 7489 *error = EPROTO; 7490 goto done; 7491 } 7492 /* 7493 * Shuffle the routing header and ip6_dst 7494 * addresses, and get the checksum difference 7495 * between the first hop (in ip6_dst) and 7496 * the destination (in the last routing hdr entry). 7497 */ 7498 csum = ip_massage_options_v6(ip6h, rth); 7499 /* 7500 * Verify that the first hop isn't a mapped address. 7501 * Routers along the path need to do this verification 7502 * for subsequent hops. 7503 */ 7504 if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) { 7505 *error = EADDRNOTAVAIL; 7506 goto done; 7507 } 7508 7509 cp += (rth->ip6r_len + 1)*8; 7510 } 7511 } 7512 7513 /* count up length of UDP packet */ 7514 ip_len = (mp1->b_wptr - (unsigned char *)ip6h) - IPV6_HDR_LEN; 7515 if ((mp2 = mp1->b_cont) != NULL) { 7516 do { 7517 ASSERT((uintptr_t)MBLKL(mp2) <= (uintptr_t)UINT_MAX); 7518 ip_len += (uint32_t)MBLKL(mp2); 7519 } while ((mp2 = mp2->b_cont) != NULL); 7520 } 7521 7522 /* 7523 * If the size of the packet is greater than the maximum allowed by 7524 * ip, return an error. Passing this down could cause panics because 7525 * the size will have wrapped and be inconsistent with the msg size. 7526 */ 7527 if (ip_len > IP_MAXPACKET) { 7528 *error = EMSGSIZE; 7529 goto done; 7530 } 7531 7532 /* Store the UDP length. Subtract length of extension hdrs */ 7533 udph->uha_length = htons(ip_len + IPV6_HDR_LEN - 7534 (int)((uchar_t *)udph - (uchar_t *)ip6h)); 7535 7536 /* 7537 * We make it easy for IP to include our pseudo header 7538 * by putting our length in uh_checksum, modified (if 7539 * we have a routing header) by the checksum difference 7540 * between the ultimate destination and first hop addresses. 7541 * Note: UDP over IPv6 must always checksum the packet. 7542 */ 7543 csum += udph->uha_length; 7544 csum = (csum & 0xFFFF) + (csum >> 16); 7545 udph->uha_checksum = (uint16_t)csum; 7546 7547 #ifdef _LITTLE_ENDIAN 7548 ip_len = htons(ip_len); 7549 #endif 7550 ip6h->ip6_plen = ip_len; 7551 if (DB_CRED(mp) != NULL) 7552 mblk_setcred(mp1, DB_CRED(mp)); 7553 7554 if (DB_TYPE(mp) != M_DATA) { 7555 ASSERT(mp != mp1); 7556 freeb(mp); 7557 } 7558 7559 /* mp has been consumed and we'll return success */ 7560 ASSERT(*error == 0); 7561 mp = NULL; 7562 7563 /* We're done. Pass the packet to IP */ 7564 BUMP_MIB(&udp_mib, udpOutDatagrams); 7565 ip_output_v6(connp, mp1, q, IP_WPUT); 7566 7567 done: 7568 if (*error != 0) { 7569 ASSERT(mp != NULL); 7570 BUMP_MIB(&udp_mib, udpOutErrors); 7571 } 7572 return (mp); 7573 } 7574 7575 static void 7576 udp_wput_other(queue_t *q, mblk_t *mp) 7577 { 7578 uchar_t *rptr = mp->b_rptr; 7579 struct datab *db; 7580 struct iocblk *iocp; 7581 cred_t *cr; 7582 conn_t *connp = Q_TO_CONN(q); 7583 udp_t *udp = connp->conn_udp; 7584 7585 TRACE_1(TR_FAC_UDP, TR_UDP_WPUT_OTHER_START, 7586 "udp_wput_other_start: q %p", q); 7587 7588 db = mp->b_datap; 7589 7590 cr = DB_CREDDEF(mp, connp->conn_cred); 7591 7592 switch (db->db_type) { 7593 case M_PROTO: 7594 case M_PCPROTO: 7595 if (mp->b_wptr - rptr < sizeof (t_scalar_t)) { 7596 freemsg(mp); 7597 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7598 "udp_wput_other_end: q %p (%S)", 7599 q, "protoshort"); 7600 return; 7601 } 7602 switch (((t_primp_t)rptr)->type) { 7603 case T_ADDR_REQ: 7604 udp_addr_req(q, mp); 7605 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7606 "udp_wput_other_end: q %p (%S)", q, "addrreq"); 7607 return; 7608 case O_T_BIND_REQ: 7609 case T_BIND_REQ: 7610 udp_bind(q, mp); 7611 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7612 "udp_wput_other_end: q %p (%S)", q, "bindreq"); 7613 return; 7614 case T_CONN_REQ: 7615 udp_connect(q, mp); 7616 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7617 "udp_wput_other_end: q %p (%S)", q, "connreq"); 7618 return; 7619 case T_CAPABILITY_REQ: 7620 udp_capability_req(q, mp); 7621 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7622 "udp_wput_other_end: q %p (%S)", q, "capabreq"); 7623 return; 7624 case T_INFO_REQ: 7625 udp_info_req(q, mp); 7626 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7627 "udp_wput_other_end: q %p (%S)", q, "inforeq"); 7628 return; 7629 case T_UNITDATA_REQ: 7630 /* 7631 * If a T_UNITDATA_REQ gets here, the address must 7632 * be bad. Valid T_UNITDATA_REQs are handled 7633 * in udp_wput. 7634 */ 7635 udp_ud_err(q, mp, NULL, 0, EADDRNOTAVAIL); 7636 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7637 "udp_wput_other_end: q %p (%S)", 7638 q, "unitdatareq"); 7639 return; 7640 case T_UNBIND_REQ: 7641 udp_unbind(q, mp); 7642 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7643 "udp_wput_other_end: q %p (%S)", q, "unbindreq"); 7644 return; 7645 case T_SVR4_OPTMGMT_REQ: 7646 if (!snmpcom_req(q, mp, udp_snmp_set, udp_snmp_get, cr)) 7647 /* 7648 * Use upper queue for option processing in 7649 * case the request is not handled at this 7650 * level and needs to be passed down to IP. 7651 */ 7652 (void) svr4_optcom_req(_WR(UDP_RD(q)), 7653 mp, cr, &udp_opt_obj); 7654 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7655 "udp_wput_other_end: q %p (%S)", 7656 q, "optmgmtreq"); 7657 return; 7658 7659 case T_OPTMGMT_REQ: 7660 /* 7661 * Use upper queue for option processing in 7662 * case the request is not handled at this 7663 * level and needs to be passed down to IP. 7664 */ 7665 (void) tpi_optcom_req(_WR(UDP_RD(q)), 7666 mp, cr, &udp_opt_obj); 7667 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7668 "udp_wput_other_end: q %p (%S)", 7669 q, "optmgmtreq"); 7670 return; 7671 7672 case T_DISCON_REQ: 7673 udp_disconnect(q, mp); 7674 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7675 "udp_wput_other_end: q %p (%S)", 7676 q, "disconreq"); 7677 return; 7678 7679 /* The following TPI message is not supported by udp. */ 7680 case O_T_CONN_RES: 7681 case T_CONN_RES: 7682 udp_err_ack(q, mp, TNOTSUPPORT, 0); 7683 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7684 "udp_wput_other_end: q %p (%S)", 7685 q, "connres/disconreq"); 7686 return; 7687 7688 /* The following 3 TPI messages are illegal for udp. */ 7689 case T_DATA_REQ: 7690 case T_EXDATA_REQ: 7691 case T_ORDREL_REQ: 7692 udp_err_ack(q, mp, TNOTSUPPORT, 0); 7693 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7694 "udp_wput_other_end: q %p (%S)", 7695 q, "data/exdata/ordrel"); 7696 return; 7697 default: 7698 break; 7699 } 7700 break; 7701 case M_FLUSH: 7702 if (*rptr & FLUSHW) 7703 flushq(q, FLUSHDATA); 7704 break; 7705 case M_IOCTL: 7706 iocp = (struct iocblk *)mp->b_rptr; 7707 switch (iocp->ioc_cmd) { 7708 case TI_GETPEERNAME: 7709 if (udp->udp_state != TS_DATA_XFER) { 7710 /* 7711 * If a default destination address has not 7712 * been associated with the stream, then we 7713 * don't know the peer's name. 7714 */ 7715 iocp->ioc_error = ENOTCONN; 7716 iocp->ioc_count = 0; 7717 mp->b_datap->db_type = M_IOCACK; 7718 putnext(UDP_RD(q), mp); 7719 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7720 "udp_wput_other_end: q %p (%S)", 7721 q, "getpeername"); 7722 return; 7723 } 7724 /* FALLTHRU */ 7725 case TI_GETMYNAME: { 7726 /* 7727 * For TI_GETPEERNAME and TI_GETMYNAME, we first 7728 * need to copyin the user's strbuf structure. 7729 * Processing will continue in the M_IOCDATA case 7730 * below. 7731 */ 7732 mi_copyin(q, mp, NULL, 7733 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 7734 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7735 "udp_wput_other_end: q %p (%S)", 7736 q, "getmyname"); 7737 return; 7738 } 7739 case ND_SET: 7740 /* nd_getset performs the necessary checking */ 7741 case ND_GET: 7742 if (nd_getset(q, udp_g_nd, mp)) { 7743 putnext(UDP_RD(q), mp); 7744 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7745 "udp_wput_other_end: q %p (%S)", 7746 q, "get"); 7747 return; 7748 } 7749 break; 7750 case _SIOCSOCKFALLBACK: 7751 /* 7752 * Either sockmod is about to be popped and the 7753 * socket would now be treated as a plain stream, 7754 * or a module is about to be pushed so we could 7755 * no longer use read-side synchronous stream. 7756 * Drain any queued data and disable direct sockfs 7757 * interface from now on. 7758 */ 7759 if (!udp->udp_issocket) { 7760 DB_TYPE(mp) = M_IOCNAK; 7761 iocp->ioc_error = EINVAL; 7762 } else { 7763 udp->udp_issocket = B_FALSE; 7764 if (udp->udp_direct_sockfs) { 7765 /* 7766 * Disable read-side synchronous 7767 * stream interface and drain any 7768 * queued data. 7769 */ 7770 udp_rcv_drain(UDP_RD(q), udp, 7771 B_FALSE); 7772 ASSERT(!udp->udp_direct_sockfs); 7773 UDP_STAT(udp_sock_fallback); 7774 } 7775 DB_TYPE(mp) = M_IOCACK; 7776 iocp->ioc_error = 0; 7777 } 7778 iocp->ioc_count = 0; 7779 iocp->ioc_rval = 0; 7780 putnext(UDP_RD(q), mp); 7781 return; 7782 default: 7783 break; 7784 } 7785 break; 7786 case M_IOCDATA: 7787 udp_wput_iocdata(q, mp); 7788 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7789 "udp_wput_other_end: q %p (%S)", q, "iocdata"); 7790 return; 7791 default: 7792 /* Unrecognized messages are passed through without change. */ 7793 break; 7794 } 7795 TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END, 7796 "udp_wput_other_end: q %p (%S)", q, "end"); 7797 ip_output(connp, mp, q, IP_WPUT); 7798 } 7799 7800 /* ARGSUSED */ 7801 static void 7802 udp_wput_other_wrapper(void *arg, mblk_t *mp, void *arg2) 7803 { 7804 udp_wput_other(((conn_t *)arg)->conn_wq, mp); 7805 udp_exit((conn_t *)arg); 7806 } 7807 7808 /* 7809 * udp_wput_iocdata is called by udp_wput_other to handle all M_IOCDATA 7810 * messages. 7811 */ 7812 static void 7813 udp_wput_iocdata(queue_t *q, mblk_t *mp) 7814 { 7815 mblk_t *mp1; 7816 STRUCT_HANDLE(strbuf, sb); 7817 uint16_t port; 7818 in6_addr_t v6addr; 7819 ipaddr_t v4addr; 7820 uint32_t flowinfo = 0; 7821 int addrlen; 7822 udp_t *udp = Q_TO_UDP(q); 7823 7824 /* Make sure it is one of ours. */ 7825 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 7826 case TI_GETMYNAME: 7827 case TI_GETPEERNAME: 7828 break; 7829 default: 7830 ip_output(Q_TO_CONN(q), mp, q, IP_WPUT); 7831 return; 7832 } 7833 7834 q = WR(UDP_RD(q)); 7835 switch (mi_copy_state(q, mp, &mp1)) { 7836 case -1: 7837 return; 7838 case MI_COPY_CASE(MI_COPY_IN, 1): 7839 break; 7840 case MI_COPY_CASE(MI_COPY_OUT, 1): 7841 /* 7842 * The address has been copied out, so now 7843 * copyout the strbuf. 7844 */ 7845 mi_copyout(q, mp); 7846 return; 7847 case MI_COPY_CASE(MI_COPY_OUT, 2): 7848 /* 7849 * The address and strbuf have been copied out. 7850 * We're done, so just acknowledge the original 7851 * M_IOCTL. 7852 */ 7853 mi_copy_done(q, mp, 0); 7854 return; 7855 default: 7856 /* 7857 * Something strange has happened, so acknowledge 7858 * the original M_IOCTL with an EPROTO error. 7859 */ 7860 mi_copy_done(q, mp, EPROTO); 7861 return; 7862 } 7863 7864 /* 7865 * Now we have the strbuf structure for TI_GETMYNAME 7866 * and TI_GETPEERNAME. Next we copyout the requested 7867 * address and then we'll copyout the strbuf. 7868 */ 7869 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 7870 (void *)mp1->b_rptr); 7871 if (udp->udp_family == AF_INET) 7872 addrlen = sizeof (sin_t); 7873 else 7874 addrlen = sizeof (sin6_t); 7875 7876 if (STRUCT_FGET(sb, maxlen) < addrlen) { 7877 mi_copy_done(q, mp, EINVAL); 7878 return; 7879 } 7880 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 7881 case TI_GETMYNAME: 7882 if (udp->udp_family == AF_INET) { 7883 ASSERT(udp->udp_ipversion == IPV4_VERSION); 7884 if (!IN6_IS_ADDR_V4MAPPED_ANY(&udp->udp_v6src) && 7885 !IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src)) { 7886 v4addr = V4_PART_OF_V6(udp->udp_v6src); 7887 } else { 7888 /* 7889 * INADDR_ANY 7890 * udp_v6src is not set, we might be bound to 7891 * broadcast/multicast. Use udp_bound_v6src as 7892 * local address instead (that could 7893 * also still be INADDR_ANY) 7894 */ 7895 v4addr = V4_PART_OF_V6(udp->udp_bound_v6src); 7896 } 7897 } else { 7898 /* udp->udp_family == AF_INET6 */ 7899 if (!IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src)) { 7900 v6addr = udp->udp_v6src; 7901 } else { 7902 /* 7903 * UNSPECIFIED 7904 * udp_v6src is not set, we might be bound to 7905 * broadcast/multicast. Use udp_bound_v6src as 7906 * local address instead (that could 7907 * also still be UNSPECIFIED) 7908 */ 7909 v6addr = udp->udp_bound_v6src; 7910 } 7911 } 7912 port = udp->udp_port; 7913 break; 7914 case TI_GETPEERNAME: 7915 if (udp->udp_state != TS_DATA_XFER) { 7916 mi_copy_done(q, mp, ENOTCONN); 7917 return; 7918 } 7919 if (udp->udp_family == AF_INET) { 7920 ASSERT(udp->udp_ipversion == IPV4_VERSION); 7921 v4addr = V4_PART_OF_V6(udp->udp_v6dst); 7922 } else { 7923 /* udp->udp_family == AF_INET6) */ 7924 v6addr = udp->udp_v6dst; 7925 flowinfo = udp->udp_flowinfo; 7926 } 7927 port = udp->udp_dstport; 7928 break; 7929 default: 7930 mi_copy_done(q, mp, EPROTO); 7931 return; 7932 } 7933 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 7934 if (!mp1) 7935 return; 7936 7937 if (udp->udp_family == AF_INET) { 7938 sin_t *sin; 7939 7940 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 7941 sin = (sin_t *)mp1->b_rptr; 7942 mp1->b_wptr = (uchar_t *)&sin[1]; 7943 *sin = sin_null; 7944 sin->sin_family = AF_INET; 7945 sin->sin_addr.s_addr = v4addr; 7946 sin->sin_port = port; 7947 } else { 7948 /* udp->udp_family == AF_INET6 */ 7949 sin6_t *sin6; 7950 7951 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 7952 sin6 = (sin6_t *)mp1->b_rptr; 7953 mp1->b_wptr = (uchar_t *)&sin6[1]; 7954 *sin6 = sin6_null; 7955 sin6->sin6_family = AF_INET6; 7956 sin6->sin6_flowinfo = flowinfo; 7957 sin6->sin6_addr = v6addr; 7958 sin6->sin6_port = port; 7959 } 7960 /* Copy out the address */ 7961 mi_copyout(q, mp); 7962 } 7963 7964 7965 static int 7966 udp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp, 7967 udpattrs_t *udpattrs) 7968 { 7969 struct T_unitdata_req *udreqp; 7970 int is_absreq_failure; 7971 cred_t *cr; 7972 conn_t *connp = Q_TO_CONN(q); 7973 7974 ASSERT(((t_primp_t)mp->b_rptr)->type); 7975 7976 cr = DB_CREDDEF(mp, connp->conn_cred); 7977 7978 udreqp = (struct T_unitdata_req *)mp->b_rptr; 7979 7980 /* 7981 * Use upper queue for option processing since the callback 7982 * routines expect to be called in UDP instance instead of IP. 7983 */ 7984 *errorp = tpi_optcom_buf(_WR(UDP_RD(q)), mp, &udreqp->OPT_length, 7985 udreqp->OPT_offset, cr, &udp_opt_obj, 7986 udpattrs, &is_absreq_failure); 7987 7988 if (*errorp != 0) { 7989 /* 7990 * Note: No special action needed in this 7991 * module for "is_absreq_failure" 7992 */ 7993 return (-1); /* failure */ 7994 } 7995 ASSERT(is_absreq_failure == 0); 7996 return (0); /* success */ 7997 } 7998 7999 void 8000 udp_ddi_init(void) 8001 { 8002 int i; 8003 8004 UDP6_MAJ = ddi_name_to_major(UDP6); 8005 8006 udp_max_optsize = optcom_max_optsize(udp_opt_obj.odb_opt_des_arr, 8007 udp_opt_obj.odb_opt_arr_cnt); 8008 8009 if (udp_bind_fanout_size & (udp_bind_fanout_size - 1)) { 8010 /* Not a power of two. Round up to nearest power of two */ 8011 for (i = 0; i < 31; i++) { 8012 if (udp_bind_fanout_size < (1 << i)) 8013 break; 8014 } 8015 udp_bind_fanout_size = 1 << i; 8016 } 8017 udp_bind_fanout = kmem_zalloc(udp_bind_fanout_size * 8018 sizeof (udp_fanout_t), KM_SLEEP); 8019 for (i = 0; i < udp_bind_fanout_size; i++) { 8020 mutex_init(&udp_bind_fanout[i].uf_lock, NULL, MUTEX_DEFAULT, 8021 NULL); 8022 } 8023 (void) udp_param_register(udp_param_arr, A_CNT(udp_param_arr)); 8024 8025 udp_kstat_init(); 8026 8027 udp_cache = kmem_cache_create("udp_cache", sizeof (udp_t), 8028 CACHE_ALIGN_SIZE, NULL, NULL, NULL, NULL, NULL, 0); 8029 } 8030 8031 void 8032 udp_ddi_destroy(void) 8033 { 8034 int i; 8035 8036 nd_free(&udp_g_nd); 8037 8038 for (i = 0; i < udp_bind_fanout_size; i++) { 8039 mutex_destroy(&udp_bind_fanout[i].uf_lock); 8040 } 8041 8042 kmem_free(udp_bind_fanout, udp_bind_fanout_size * 8043 sizeof (udp_fanout_t)); 8044 8045 udp_kstat_fini(); 8046 8047 kmem_cache_destroy(udp_cache); 8048 } 8049 8050 static void 8051 udp_kstat_init(void) 8052 { 8053 udp_named_kstat_t template = { 8054 { "inDatagrams", KSTAT_DATA_UINT32, 0 }, 8055 { "inErrors", KSTAT_DATA_UINT32, 0 }, 8056 { "outDatagrams", KSTAT_DATA_UINT32, 0 }, 8057 { "entrySize", KSTAT_DATA_INT32, 0 }, 8058 { "entry6Size", KSTAT_DATA_INT32, 0 }, 8059 { "outErrors", KSTAT_DATA_UINT32, 0 }, 8060 }; 8061 8062 udp_mibkp = kstat_create(UDP_MOD_NAME, 0, UDP_MOD_NAME, 8063 "mib2", KSTAT_TYPE_NAMED, NUM_OF_FIELDS(udp_named_kstat_t), 0); 8064 8065 if (udp_mibkp == NULL) 8066 return; 8067 8068 template.entrySize.value.ui32 = sizeof (mib2_udpEntry_t); 8069 template.entry6Size.value.ui32 = sizeof (mib2_udp6Entry_t); 8070 8071 bcopy(&template, udp_mibkp->ks_data, sizeof (template)); 8072 8073 udp_mibkp->ks_update = udp_kstat_update; 8074 8075 kstat_install(udp_mibkp); 8076 8077 if ((udp_ksp = kstat_create(UDP_MOD_NAME, 0, "udpstat", 8078 "net", KSTAT_TYPE_NAMED, 8079 sizeof (udp_statistics) / sizeof (kstat_named_t), 8080 KSTAT_FLAG_VIRTUAL)) != NULL) { 8081 udp_ksp->ks_data = &udp_statistics; 8082 kstat_install(udp_ksp); 8083 } 8084 } 8085 8086 static void 8087 udp_kstat_fini(void) 8088 { 8089 if (udp_ksp != NULL) { 8090 kstat_delete(udp_ksp); 8091 udp_ksp = NULL; 8092 } 8093 if (udp_mibkp != NULL) { 8094 kstat_delete(udp_mibkp); 8095 udp_mibkp = NULL; 8096 } 8097 } 8098 8099 static int 8100 udp_kstat_update(kstat_t *kp, int rw) 8101 { 8102 udp_named_kstat_t *udpkp; 8103 8104 if ((kp == NULL) || (kp->ks_data == NULL)) 8105 return (EIO); 8106 8107 if (rw == KSTAT_WRITE) 8108 return (EACCES); 8109 8110 udpkp = (udp_named_kstat_t *)kp->ks_data; 8111 8112 udpkp->inDatagrams.value.ui32 = udp_mib.udpInDatagrams; 8113 udpkp->inErrors.value.ui32 = udp_mib.udpInErrors; 8114 udpkp->outDatagrams.value.ui32 = udp_mib.udpOutDatagrams; 8115 udpkp->outErrors.value.ui32 = udp_mib.udpOutErrors; 8116 8117 return (0); 8118 } 8119 8120 /* ARGSUSED */ 8121 static void 8122 udp_rput(queue_t *q, mblk_t *mp) 8123 { 8124 /* 8125 * We get here whenever we do qreply() from IP, 8126 * i.e as part of handlings ioctls, etc. 8127 */ 8128 putnext(q, mp); 8129 } 8130 8131 /* 8132 * Read-side synchronous stream info entry point, called as a 8133 * result of handling certain STREAMS ioctl operations. 8134 */ 8135 static int 8136 udp_rinfop(queue_t *q, infod_t *dp) 8137 { 8138 mblk_t *mp; 8139 uint_t cmd = dp->d_cmd; 8140 int res = 0; 8141 int error = 0; 8142 udp_t *udp = Q_TO_UDP(RD(UDP_WR(q))); 8143 struct stdata *stp = STREAM(q); 8144 8145 mutex_enter(&udp->udp_drain_lock); 8146 /* If shutdown on read has happened, return nothing */ 8147 mutex_enter(&stp->sd_lock); 8148 if (stp->sd_flag & STREOF) { 8149 mutex_exit(&stp->sd_lock); 8150 goto done; 8151 } 8152 mutex_exit(&stp->sd_lock); 8153 8154 if ((mp = udp->udp_rcv_list_head) == NULL) 8155 goto done; 8156 8157 ASSERT(DB_TYPE(mp) != M_DATA && mp->b_cont != NULL); 8158 8159 if (cmd & INFOD_COUNT) { 8160 /* 8161 * Return the number of messages. 8162 */ 8163 dp->d_count += udp->udp_rcv_msgcnt; 8164 res |= INFOD_COUNT; 8165 } 8166 if (cmd & INFOD_BYTES) { 8167 /* 8168 * Return size of all data messages. 8169 */ 8170 dp->d_bytes += udp->udp_rcv_cnt; 8171 res |= INFOD_BYTES; 8172 } 8173 if (cmd & INFOD_FIRSTBYTES) { 8174 /* 8175 * Return size of first data message. 8176 */ 8177 dp->d_bytes = msgdsize(mp); 8178 res |= INFOD_FIRSTBYTES; 8179 dp->d_cmd &= ~INFOD_FIRSTBYTES; 8180 } 8181 if (cmd & INFOD_COPYOUT) { 8182 mblk_t *mp1 = mp->b_cont; 8183 int n; 8184 /* 8185 * Return data contents of first message. 8186 */ 8187 ASSERT(DB_TYPE(mp1) == M_DATA); 8188 while (mp1 != NULL && dp->d_uiop->uio_resid > 0) { 8189 n = MIN(dp->d_uiop->uio_resid, MBLKL(mp1)); 8190 if (n != 0 && (error = uiomove((char *)mp1->b_rptr, n, 8191 UIO_READ, dp->d_uiop)) != 0) { 8192 goto done; 8193 } 8194 mp1 = mp1->b_cont; 8195 } 8196 res |= INFOD_COPYOUT; 8197 dp->d_cmd &= ~INFOD_COPYOUT; 8198 } 8199 done: 8200 mutex_exit(&udp->udp_drain_lock); 8201 8202 dp->d_res |= res; 8203 8204 return (error); 8205 } 8206 8207 /* 8208 * Read-side synchronous stream entry point. This is called as a result 8209 * of recv/read operation done at sockfs, and is guaranteed to execute 8210 * outside of the interrupt thread context. It returns a single datagram 8211 * (b_cont chain of T_UNITDATA_IND plus data) to the upper layer. 8212 */ 8213 static int 8214 udp_rrw(queue_t *q, struiod_t *dp) 8215 { 8216 mblk_t *mp; 8217 udp_t *udp = Q_TO_UDP(_RD(UDP_WR(q))); 8218 8219 /* We should never get here when we're in SNMP mode */ 8220 ASSERT(!(udp->udp_connp->conn_flags & IPCL_UDPMOD)); 8221 8222 /* 8223 * Dequeue datagram from the head of the list and return 8224 * it to caller; also ensure that RSLEEP sd_wakeq flag is 8225 * set/cleared depending on whether or not there's data 8226 * remaining in the list. 8227 */ 8228 mutex_enter(&udp->udp_drain_lock); 8229 if (!udp->udp_direct_sockfs) { 8230 mutex_exit(&udp->udp_drain_lock); 8231 UDP_STAT(udp_rrw_busy); 8232 return (EBUSY); 8233 } 8234 if ((mp = udp->udp_rcv_list_head) != NULL) { 8235 uint_t size = msgdsize(mp); 8236 8237 /* Last datagram in the list? */ 8238 if ((udp->udp_rcv_list_head = mp->b_next) == NULL) 8239 udp->udp_rcv_list_tail = NULL; 8240 mp->b_next = NULL; 8241 8242 udp->udp_rcv_cnt -= size; 8243 udp->udp_rcv_msgcnt--; 8244 UDP_STAT(udp_rrw_msgcnt); 8245 8246 /* No longer flow-controlling? */ 8247 if (udp->udp_rcv_cnt < udp->udp_rcv_hiwat && 8248 udp->udp_rcv_msgcnt < udp->udp_rcv_hiwat) 8249 udp->udp_drain_qfull = B_FALSE; 8250 } 8251 if (udp->udp_rcv_list_head == NULL) { 8252 /* 8253 * Either we just dequeued the last datagram or 8254 * we get here from sockfs and have nothing to 8255 * return; in this case clear RSLEEP. 8256 */ 8257 ASSERT(udp->udp_rcv_cnt == 0); 8258 ASSERT(udp->udp_rcv_msgcnt == 0); 8259 ASSERT(udp->udp_rcv_list_tail == NULL); 8260 STR_WAKEUP_CLEAR(STREAM(q)); 8261 } else { 8262 /* 8263 * More data follows; we need udp_rrw() to be 8264 * called in future to pick up the rest. 8265 */ 8266 STR_WAKEUP_SET(STREAM(q)); 8267 } 8268 mutex_exit(&udp->udp_drain_lock); 8269 dp->d_mp = mp; 8270 return (0); 8271 } 8272 8273 /* 8274 * Enqueue a completely-built T_UNITDATA_IND message into the receive 8275 * list; this is typically executed within the interrupt thread context 8276 * and so we do things as quickly as possible. 8277 */ 8278 static void 8279 udp_rcv_enqueue(queue_t *q, udp_t *udp, mblk_t *mp, uint_t pkt_len) 8280 { 8281 ASSERT(q == RD(q)); 8282 ASSERT(pkt_len == msgdsize(mp)); 8283 ASSERT(mp->b_next == NULL && mp->b_cont != NULL); 8284 ASSERT(DB_TYPE(mp) == M_PROTO && DB_TYPE(mp->b_cont) == M_DATA); 8285 ASSERT(MBLKL(mp) >= sizeof (struct T_unitdata_ind)); 8286 8287 mutex_enter(&udp->udp_drain_lock); 8288 /* 8289 * Wake up and signal the receiving app; it is okay to do this 8290 * before enqueueing the mp because we are holding the drain lock. 8291 * One of the advantages of synchronous stream is the ability for 8292 * us to find out when the application performs a read on the 8293 * socket by way of udp_rrw() entry point being called. We need 8294 * to generate SIGPOLL/SIGIO for each received data in the case 8295 * of asynchronous socket just as in the strrput() case. However, 8296 * we only wake the application up when necessary, i.e. during the 8297 * first enqueue. When udp_rrw() is called, we send up a single 8298 * datagram upstream and call STR_WAKEUP_SET() again when there 8299 * are still data remaining in our receive queue. 8300 */ 8301 if (udp->udp_rcv_list_head == NULL) { 8302 STR_WAKEUP_SET(STREAM(q)); 8303 udp->udp_rcv_list_head = mp; 8304 } else { 8305 udp->udp_rcv_list_tail->b_next = mp; 8306 } 8307 udp->udp_rcv_list_tail = mp; 8308 udp->udp_rcv_cnt += pkt_len; 8309 udp->udp_rcv_msgcnt++; 8310 8311 /* Need to flow-control? */ 8312 if (udp->udp_rcv_cnt >= udp->udp_rcv_hiwat || 8313 udp->udp_rcv_msgcnt >= udp->udp_rcv_hiwat) 8314 udp->udp_drain_qfull = B_TRUE; 8315 8316 /* Update poll events and send SIGPOLL/SIGIO if necessary */ 8317 STR_SENDSIG(STREAM(q)); 8318 mutex_exit(&udp->udp_drain_lock); 8319 } 8320 8321 /* 8322 * Drain the contents of receive list to the module upstream; we do 8323 * this during close or when we fallback to the slow mode due to 8324 * sockmod being popped or a module being pushed on top of us. 8325 */ 8326 static void 8327 udp_rcv_drain(queue_t *q, udp_t *udp, boolean_t closing) 8328 { 8329 mblk_t *mp; 8330 8331 ASSERT(q == RD(q)); 8332 8333 mutex_enter(&udp->udp_drain_lock); 8334 /* 8335 * There is no race with a concurrent udp_input() sending 8336 * up packets using putnext() after we have cleared the 8337 * udp_direct_sockfs flag but before we have completed 8338 * sending up the packets in udp_rcv_list, since we are 8339 * either a writer or we have quiesced the conn. 8340 */ 8341 udp->udp_direct_sockfs = B_FALSE; 8342 mutex_exit(&udp->udp_drain_lock); 8343 8344 if (udp->udp_rcv_list_head != NULL) 8345 UDP_STAT(udp_drain); 8346 8347 /* 8348 * Send up everything via putnext(); note here that we 8349 * don't need the udp_drain_lock to protect us since 8350 * nothing can enter udp_rrw() and that we currently 8351 * have exclusive access to this udp. 8352 */ 8353 while ((mp = udp->udp_rcv_list_head) != NULL) { 8354 udp->udp_rcv_list_head = mp->b_next; 8355 mp->b_next = NULL; 8356 udp->udp_rcv_cnt -= msgdsize(mp); 8357 udp->udp_rcv_msgcnt--; 8358 if (closing) { 8359 freemsg(mp); 8360 } else { 8361 putnext(q, mp); 8362 } 8363 } 8364 ASSERT(udp->udp_rcv_cnt == 0); 8365 ASSERT(udp->udp_rcv_msgcnt == 0); 8366 ASSERT(udp->udp_rcv_list_head == NULL); 8367 udp->udp_rcv_list_tail = NULL; 8368 udp->udp_drain_qfull = B_FALSE; 8369 } 8370 8371 static size_t 8372 udp_set_rcv_hiwat(udp_t *udp, size_t size) 8373 { 8374 /* We add a bit of extra buffering */ 8375 size += size >> 1; 8376 if (size > udp_max_buf) 8377 size = udp_max_buf; 8378 8379 udp->udp_rcv_hiwat = size; 8380 return (size); 8381 } 8382 8383 /* 8384 * Little helper for IPsec's NAT-T processing. 8385 */ 8386 boolean_t 8387 udp_compute_checksum(void) 8388 { 8389 return (udp_do_checksum); 8390 } 8391