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 (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 24 * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #include <sys/sysmacros.h> 29 #include <sys/types.h> 30 #include <sys/stream.h> 31 #include <sys/stropts.h> 32 #include <sys/strlog.h> 33 #include <sys/strsun.h> 34 #define _SUN_TPI_VERSION 2 35 #include <sys/tihdr.h> 36 #include <sys/timod.h> 37 #include <sys/ddi.h> 38 #include <sys/sunddi.h> 39 #include <sys/strsubr.h> 40 #include <sys/suntpi.h> 41 #include <sys/xti_inet.h> 42 #include <sys/kmem.h> 43 #include <sys/cred_impl.h> 44 #include <sys/policy.h> 45 #include <sys/priv.h> 46 #include <sys/ucred.h> 47 #include <sys/zone.h> 48 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/sockio.h> 52 #include <sys/vtrace.h> 53 #include <sys/sdt.h> 54 #include <sys/debug.h> 55 #include <sys/isa_defs.h> 56 #include <sys/random.h> 57 #include <netinet/in.h> 58 #include <netinet/ip6.h> 59 #include <netinet/icmp6.h> 60 #include <netinet/udp.h> 61 62 #include <inet/common.h> 63 #include <inet/ip.h> 64 #include <inet/ip_impl.h> 65 #include <inet/ipsec_impl.h> 66 #include <inet/ip6.h> 67 #include <inet/ip_ire.h> 68 #include <inet/ip_if.h> 69 #include <inet/ip_multi.h> 70 #include <inet/ip_ndp.h> 71 #include <inet/proto_set.h> 72 #include <inet/mib2.h> 73 #include <inet/optcom.h> 74 #include <inet/snmpcom.h> 75 #include <inet/kstatcom.h> 76 #include <inet/ipclassifier.h> 77 #include <sys/squeue_impl.h> 78 #include <inet/ipnet.h> 79 #include <sys/ethernet.h> 80 81 #include <sys/tsol/label.h> 82 #include <sys/tsol/tnet.h> 83 #include <rpc/pmap_prot.h> 84 85 #include <inet/udp_impl.h> 86 87 /* 88 * Synchronization notes: 89 * 90 * UDP is MT and uses the usual kernel synchronization primitives. There are 2 91 * locks, the fanout lock (uf_lock) and conn_lock. conn_lock 92 * protects the contents of the udp_t. uf_lock protects the address and the 93 * fanout information. 94 * The lock order is conn_lock -> uf_lock. 95 * 96 * The fanout lock uf_lock: 97 * When a UDP endpoint is bound to a local port, it is inserted into 98 * a bind hash list. The list consists of an array of udp_fanout_t buckets. 99 * The size of the array is controlled by the udp_bind_fanout_size variable. 100 * This variable can be changed in /etc/system if the default value is 101 * not large enough. Each bind hash bucket is protected by a per bucket 102 * lock. It protects the udp_bind_hash and udp_ptpbhn fields in the udp_t 103 * structure and a few other fields in the udp_t. A UDP endpoint is removed 104 * from the bind hash list only when it is being unbound or being closed. 105 * The per bucket lock also protects a UDP endpoint's state changes. 106 * 107 * Plumbing notes: 108 * UDP is always a device driver. For compatibility with mibopen() code 109 * it is possible to I_PUSH "udp", but that results in pushing a passthrough 110 * dummy module. 111 * 112 * The above implies that we don't support any intermediate module to 113 * reside in between /dev/ip and udp -- in fact, we never supported such 114 * scenario in the past as the inter-layer communication semantics have 115 * always been private. 116 */ 117 118 /* For /etc/system control */ 119 uint_t udp_bind_fanout_size = UDP_BIND_FANOUT_SIZE; 120 121 static void udp_addr_req(queue_t *q, mblk_t *mp); 122 static void udp_tpi_bind(queue_t *q, mblk_t *mp); 123 static void udp_bind_hash_insert(udp_fanout_t *uf, udp_t *udp); 124 static void udp_bind_hash_remove(udp_t *udp, boolean_t caller_holds_lock); 125 static int udp_build_hdr_template(conn_t *, const in6_addr_t *, 126 const in6_addr_t *, in_port_t, uint32_t); 127 static void udp_capability_req(queue_t *q, mblk_t *mp); 128 static int udp_tpi_close(queue_t *q, int flags, cred_t *); 129 static void udp_close_free(conn_t *); 130 static void udp_tpi_connect(queue_t *q, mblk_t *mp); 131 static void udp_tpi_disconnect(queue_t *q, mblk_t *mp); 132 static void udp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, 133 int sys_error); 134 static void udp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive, 135 t_scalar_t tlierr, int sys_error); 136 static int udp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 137 cred_t *cr); 138 static int udp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 139 char *value, caddr_t cp, cred_t *cr); 140 static int udp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 141 char *value, caddr_t cp, cred_t *cr); 142 static void udp_icmp_input(void *, mblk_t *, void *, ip_recv_attr_t *); 143 static void udp_icmp_error_ipv6(conn_t *connp, mblk_t *mp, 144 ip_recv_attr_t *ira); 145 static void udp_info_req(queue_t *q, mblk_t *mp); 146 static void udp_input(void *, mblk_t *, void *, ip_recv_attr_t *); 147 static int udp_lrput(queue_t *, mblk_t *); 148 static int udp_lwput(queue_t *, mblk_t *); 149 static int udp_open(queue_t *q, dev_t *devp, int flag, int sflag, 150 cred_t *credp, boolean_t isv6); 151 static int udp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, 152 cred_t *credp); 153 static int udp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, 154 cred_t *credp); 155 static boolean_t udp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name); 156 int udp_opt_set(conn_t *connp, uint_t optset_context, 157 int level, int name, uint_t inlen, 158 uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 159 void *thisdg_attrs, cred_t *cr); 160 int udp_opt_get(conn_t *connp, int level, int name, 161 uchar_t *ptr); 162 static int udp_output_connected(conn_t *connp, mblk_t *mp, cred_t *cr, 163 pid_t pid); 164 static int udp_output_lastdst(conn_t *connp, mblk_t *mp, cred_t *cr, 165 pid_t pid, ip_xmit_attr_t *ixa); 166 static int udp_output_newdst(conn_t *connp, mblk_t *data_mp, sin_t *sin, 167 sin6_t *sin6, ushort_t ipversion, cred_t *cr, pid_t, 168 ip_xmit_attr_t *ixa); 169 static mblk_t *udp_prepend_hdr(conn_t *, ip_xmit_attr_t *, const ip_pkt_t *, 170 const in6_addr_t *, const in6_addr_t *, in_port_t, uint32_t, mblk_t *, 171 int *); 172 static mblk_t *udp_prepend_header_template(conn_t *, ip_xmit_attr_t *, 173 mblk_t *, const in6_addr_t *, in_port_t, uint32_t, int *); 174 static void udp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err); 175 static void udp_ud_err_connected(conn_t *, t_scalar_t); 176 static void udp_tpi_unbind(queue_t *q, mblk_t *mp); 177 static in_port_t udp_update_next_port(udp_t *udp, in_port_t port, 178 boolean_t random); 179 static void udp_wput_other(queue_t *q, mblk_t *mp); 180 static void udp_wput_iocdata(queue_t *q, mblk_t *mp); 181 static int udp_wput_fallback(queue_t *q, mblk_t *mp); 182 static size_t udp_set_rcv_hiwat(udp_t *udp, size_t size); 183 184 static void *udp_stack_init(netstackid_t stackid, netstack_t *ns); 185 static void udp_stack_fini(netstackid_t stackid, void *arg); 186 187 /* Common routines for TPI and socket module */ 188 static void udp_ulp_recv(conn_t *, mblk_t *, uint_t, ip_recv_attr_t *); 189 190 /* Common routine for TPI and socket module */ 191 static conn_t *udp_do_open(cred_t *, boolean_t, int, int *); 192 static void udp_do_close(conn_t *); 193 static int udp_do_bind(conn_t *, struct sockaddr *, socklen_t, cred_t *, 194 boolean_t); 195 static int udp_do_unbind(conn_t *); 196 197 int udp_getsockname(sock_lower_handle_t, 198 struct sockaddr *, socklen_t *, cred_t *); 199 int udp_getpeername(sock_lower_handle_t, 200 struct sockaddr *, socklen_t *, cred_t *); 201 static int udp_do_connect(conn_t *, const struct sockaddr *, socklen_t, 202 cred_t *, pid_t); 203 204 #pragma inline(udp_output_connected, udp_output_newdst, udp_output_lastdst) 205 206 /* 207 * Checks if the given destination addr/port is allowed out. 208 * If allowed, registers the (dest_addr/port, node_ID) mapping at Cluster. 209 * Called for each connect() and for sendto()/sendmsg() to a different 210 * destination. 211 * For connect(), called in udp_connect(). 212 * For sendto()/sendmsg(), called in udp_output_newdst(). 213 * 214 * This macro assumes that the cl_inet_connect2 hook is not NULL. 215 * Please check this before calling this macro. 216 * 217 * void 218 * CL_INET_UDP_CONNECT(conn_t cp, udp_t *udp, boolean_t is_outgoing, 219 * in6_addr_t *faddrp, in_port_t (or uint16_t) fport, int err); 220 */ 221 #define CL_INET_UDP_CONNECT(cp, is_outgoing, faddrp, fport, err) { \ 222 (err) = 0; \ 223 /* \ 224 * Running in cluster mode - check and register active \ 225 * "connection" information \ 226 */ \ 227 if ((cp)->conn_ipversion == IPV4_VERSION) \ 228 (err) = (*cl_inet_connect2)( \ 229 (cp)->conn_netstack->netstack_stackid, \ 230 IPPROTO_UDP, is_outgoing, AF_INET, \ 231 (uint8_t *)&((cp)->conn_laddr_v4), \ 232 (cp)->conn_lport, \ 233 (uint8_t *)&(V4_PART_OF_V6(*faddrp)), \ 234 (in_port_t)(fport), NULL); \ 235 else \ 236 (err) = (*cl_inet_connect2)( \ 237 (cp)->conn_netstack->netstack_stackid, \ 238 IPPROTO_UDP, is_outgoing, AF_INET6, \ 239 (uint8_t *)&((cp)->conn_laddr_v6), \ 240 (cp)->conn_lport, \ 241 (uint8_t *)(faddrp), (in_port_t)(fport), NULL); \ 242 } 243 244 static struct module_info udp_mod_info = { 245 UDP_MOD_ID, UDP_MOD_NAME, 1, INFPSZ, UDP_RECV_HIWATER, UDP_RECV_LOWATER 246 }; 247 248 /* 249 * Entry points for UDP as a device. 250 * We have separate open functions for the /dev/udp and /dev/udp6 devices. 251 */ 252 static struct qinit udp_rinitv4 = { 253 NULL, NULL, udp_openv4, udp_tpi_close, NULL, &udp_mod_info, NULL 254 }; 255 256 static struct qinit udp_rinitv6 = { 257 NULL, NULL, udp_openv6, udp_tpi_close, NULL, &udp_mod_info, NULL 258 }; 259 260 static struct qinit udp_winit = { 261 udp_wput, ip_wsrv, NULL, NULL, NULL, &udp_mod_info 262 }; 263 264 /* UDP entry point during fallback */ 265 struct qinit udp_fallback_sock_winit = { 266 udp_wput_fallback, NULL, NULL, NULL, NULL, &udp_mod_info 267 }; 268 269 /* 270 * UDP needs to handle I_LINK and I_PLINK since ifconfig 271 * likes to use it as a place to hang the various streams. 272 */ 273 static struct qinit udp_lrinit = { 274 udp_lrput, NULL, udp_openv4, udp_tpi_close, NULL, &udp_mod_info 275 }; 276 277 static struct qinit udp_lwinit = { 278 udp_lwput, NULL, udp_openv4, udp_tpi_close, NULL, &udp_mod_info 279 }; 280 281 /* For AF_INET aka /dev/udp */ 282 struct streamtab udpinfov4 = { 283 &udp_rinitv4, &udp_winit, &udp_lrinit, &udp_lwinit 284 }; 285 286 /* For AF_INET6 aka /dev/udp6 */ 287 struct streamtab udpinfov6 = { 288 &udp_rinitv6, &udp_winit, &udp_lrinit, &udp_lwinit 289 }; 290 291 #define UDP_MAXPACKET_IPV4 (IP_MAXPACKET - UDPH_SIZE - IP_SIMPLE_HDR_LENGTH) 292 293 /* Default structure copied into T_INFO_ACK messages */ 294 static struct T_info_ack udp_g_t_info_ack_ipv4 = { 295 T_INFO_ACK, 296 UDP_MAXPACKET_IPV4, /* TSDU_size. Excl. headers */ 297 T_INVALID, /* ETSU_size. udp does not support expedited data. */ 298 T_INVALID, /* CDATA_size. udp does not support connect data. */ 299 T_INVALID, /* DDATA_size. udp does not support disconnect data. */ 300 sizeof (sin_t), /* ADDR_size. */ 301 0, /* OPT_size - not initialized here */ 302 UDP_MAXPACKET_IPV4, /* TIDU_size. Excl. headers */ 303 T_CLTS, /* SERV_type. udp supports connection-less. */ 304 TS_UNBND, /* CURRENT_state. This is set from udp_state. */ 305 (XPG4_1|SENDZERO) /* PROVIDER_flag */ 306 }; 307 308 #define UDP_MAXPACKET_IPV6 (IP_MAXPACKET - UDPH_SIZE - IPV6_HDR_LEN) 309 310 static struct T_info_ack udp_g_t_info_ack_ipv6 = { 311 T_INFO_ACK, 312 UDP_MAXPACKET_IPV6, /* TSDU_size. Excl. headers */ 313 T_INVALID, /* ETSU_size. udp does not support expedited data. */ 314 T_INVALID, /* CDATA_size. udp does not support connect data. */ 315 T_INVALID, /* DDATA_size. udp does not support disconnect data. */ 316 sizeof (sin6_t), /* ADDR_size. */ 317 0, /* OPT_size - not initialized here */ 318 UDP_MAXPACKET_IPV6, /* TIDU_size. Excl. headers */ 319 T_CLTS, /* SERV_type. udp supports connection-less. */ 320 TS_UNBND, /* CURRENT_state. This is set from udp_state. */ 321 (XPG4_1|SENDZERO) /* PROVIDER_flag */ 322 }; 323 324 /* 325 * UDP tunables related declarations. Definitions are in udp_tunables.c 326 */ 327 extern mod_prop_info_t udp_propinfo_tbl[]; 328 extern int udp_propinfo_count; 329 330 /* Setable in /etc/system */ 331 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 332 uint32_t udp_random_anon_port = 1; 333 334 /* 335 * Hook functions to enable cluster networking. 336 * On non-clustered systems these vectors must always be NULL 337 */ 338 339 void (*cl_inet_bind)(netstackid_t stack_id, uchar_t protocol, 340 sa_family_t addr_family, uint8_t *laddrp, in_port_t lport, 341 void *args) = NULL; 342 void (*cl_inet_unbind)(netstackid_t stack_id, uint8_t protocol, 343 sa_family_t addr_family, uint8_t *laddrp, in_port_t lport, 344 void *args) = NULL; 345 346 typedef union T_primitives *t_primp_t; 347 348 /* 349 * Return the next anonymous port in the privileged port range for 350 * bind checking. 351 * 352 * Trusted Extension (TX) notes: TX allows administrator to mark or 353 * reserve ports as Multilevel ports (MLP). MLP has special function 354 * on TX systems. Once a port is made MLP, it's not available as 355 * ordinary port. This creates "holes" in the port name space. It 356 * may be necessary to skip the "holes" find a suitable anon port. 357 */ 358 static in_port_t 359 udp_get_next_priv_port(udp_t *udp) 360 { 361 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 362 in_port_t nextport; 363 boolean_t restart = B_FALSE; 364 udp_stack_t *us = udp->udp_us; 365 366 retry: 367 if (next_priv_port < us->us_min_anonpriv_port || 368 next_priv_port >= IPPORT_RESERVED) { 369 next_priv_port = IPPORT_RESERVED - 1; 370 if (restart) 371 return (0); 372 restart = B_TRUE; 373 } 374 375 if (is_system_labeled() && 376 (nextport = tsol_next_port(crgetzone(udp->udp_connp->conn_cred), 377 next_priv_port, IPPROTO_UDP, B_FALSE)) != 0) { 378 next_priv_port = nextport; 379 goto retry; 380 } 381 382 return (next_priv_port--); 383 } 384 385 /* 386 * Hash list removal routine for udp_t structures. 387 */ 388 static void 389 udp_bind_hash_remove(udp_t *udp, boolean_t caller_holds_lock) 390 { 391 udp_t *udpnext; 392 kmutex_t *lockp; 393 udp_stack_t *us = udp->udp_us; 394 conn_t *connp = udp->udp_connp; 395 396 if (udp->udp_ptpbhn == NULL) 397 return; 398 399 /* 400 * Extract the lock pointer in case there are concurrent 401 * hash_remove's for this instance. 402 */ 403 ASSERT(connp->conn_lport != 0); 404 if (!caller_holds_lock) { 405 lockp = &us->us_bind_fanout[UDP_BIND_HASH(connp->conn_lport, 406 us->us_bind_fanout_size)].uf_lock; 407 ASSERT(lockp != NULL); 408 mutex_enter(lockp); 409 } 410 if (udp->udp_ptpbhn != NULL) { 411 udpnext = udp->udp_bind_hash; 412 if (udpnext != NULL) { 413 udpnext->udp_ptpbhn = udp->udp_ptpbhn; 414 udp->udp_bind_hash = NULL; 415 } 416 *udp->udp_ptpbhn = udpnext; 417 udp->udp_ptpbhn = NULL; 418 } 419 if (!caller_holds_lock) { 420 mutex_exit(lockp); 421 } 422 } 423 424 static void 425 udp_bind_hash_insert(udp_fanout_t *uf, udp_t *udp) 426 { 427 conn_t *connp = udp->udp_connp; 428 udp_t **udpp; 429 udp_t *udpnext; 430 conn_t *connext; 431 432 ASSERT(MUTEX_HELD(&uf->uf_lock)); 433 ASSERT(udp->udp_ptpbhn == NULL); 434 udpp = &uf->uf_udp; 435 udpnext = udpp[0]; 436 if (udpnext != NULL) { 437 /* 438 * If the new udp bound to the INADDR_ANY address 439 * and the first one in the list is not bound to 440 * INADDR_ANY we skip all entries until we find the 441 * first one bound to INADDR_ANY. 442 * This makes sure that applications binding to a 443 * specific address get preference over those binding to 444 * INADDR_ANY. 445 */ 446 connext = udpnext->udp_connp; 447 if (V6_OR_V4_INADDR_ANY(connp->conn_bound_addr_v6) && 448 !V6_OR_V4_INADDR_ANY(connext->conn_bound_addr_v6)) { 449 while ((udpnext = udpp[0]) != NULL && 450 !V6_OR_V4_INADDR_ANY(connext->conn_bound_addr_v6)) { 451 udpp = &(udpnext->udp_bind_hash); 452 } 453 if (udpnext != NULL) 454 udpnext->udp_ptpbhn = &udp->udp_bind_hash; 455 } else { 456 udpnext->udp_ptpbhn = &udp->udp_bind_hash; 457 } 458 } 459 udp->udp_bind_hash = udpnext; 460 udp->udp_ptpbhn = udpp; 461 udpp[0] = udp; 462 } 463 464 /* 465 * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message 466 * passed to udp_wput. 467 * It associates a port number and local address with the stream. 468 * It calls IP to verify the local IP address, and calls IP to insert 469 * the conn_t in the fanout table. 470 * If everything is ok it then sends the T_BIND_ACK back up. 471 * 472 * Note that UDP over IPv4 and IPv6 sockets can use the same port number 473 * without setting SO_REUSEADDR. This is needed so that they 474 * can be viewed as two independent transport protocols. 475 * However, anonymouns ports are allocated from the same range to avoid 476 * duplicating the us->us_next_port_to_try. 477 */ 478 static void 479 udp_tpi_bind(queue_t *q, mblk_t *mp) 480 { 481 sin_t *sin; 482 sin6_t *sin6; 483 mblk_t *mp1; 484 struct T_bind_req *tbr; 485 conn_t *connp; 486 udp_t *udp; 487 int error; 488 struct sockaddr *sa; 489 cred_t *cr; 490 491 /* 492 * All Solaris components should pass a db_credp 493 * for this TPI message, hence we ASSERT. 494 * But in case there is some other M_PROTO that looks 495 * like a TPI message sent by some other kernel 496 * component, we check and return an error. 497 */ 498 cr = msg_getcred(mp, NULL); 499 ASSERT(cr != NULL); 500 if (cr == NULL) { 501 udp_err_ack(q, mp, TSYSERR, EINVAL); 502 return; 503 } 504 505 connp = Q_TO_CONN(q); 506 udp = connp->conn_udp; 507 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 508 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 509 "udp_bind: bad req, len %u", 510 (uint_t)(mp->b_wptr - mp->b_rptr)); 511 udp_err_ack(q, mp, TPROTO, 0); 512 return; 513 } 514 if (udp->udp_state != TS_UNBND) { 515 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 516 "udp_bind: bad state, %u", udp->udp_state); 517 udp_err_ack(q, mp, TOUTSTATE, 0); 518 return; 519 } 520 /* 521 * Reallocate the message to make sure we have enough room for an 522 * address. 523 */ 524 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t), 1); 525 if (mp1 == NULL) { 526 udp_err_ack(q, mp, TSYSERR, ENOMEM); 527 return; 528 } 529 530 mp = mp1; 531 532 /* Reset the message type in preparation for shipping it back. */ 533 DB_TYPE(mp) = M_PCPROTO; 534 535 tbr = (struct T_bind_req *)mp->b_rptr; 536 switch (tbr->ADDR_length) { 537 case 0: /* Request for a generic port */ 538 tbr->ADDR_offset = sizeof (struct T_bind_req); 539 if (connp->conn_family == AF_INET) { 540 tbr->ADDR_length = sizeof (sin_t); 541 sin = (sin_t *)&tbr[1]; 542 *sin = sin_null; 543 sin->sin_family = AF_INET; 544 mp->b_wptr = (uchar_t *)&sin[1]; 545 sa = (struct sockaddr *)sin; 546 } else { 547 ASSERT(connp->conn_family == AF_INET6); 548 tbr->ADDR_length = sizeof (sin6_t); 549 sin6 = (sin6_t *)&tbr[1]; 550 *sin6 = sin6_null; 551 sin6->sin6_family = AF_INET6; 552 mp->b_wptr = (uchar_t *)&sin6[1]; 553 sa = (struct sockaddr *)sin6; 554 } 555 break; 556 557 case sizeof (sin_t): /* Complete IPv4 address */ 558 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 559 sizeof (sin_t)); 560 if (sa == NULL || !OK_32PTR((char *)sa)) { 561 udp_err_ack(q, mp, TSYSERR, EINVAL); 562 return; 563 } 564 if (connp->conn_family != AF_INET || 565 sa->sa_family != AF_INET) { 566 udp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); 567 return; 568 } 569 break; 570 571 case sizeof (sin6_t): /* complete IPv6 address */ 572 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 573 sizeof (sin6_t)); 574 if (sa == NULL || !OK_32PTR((char *)sa)) { 575 udp_err_ack(q, mp, TSYSERR, EINVAL); 576 return; 577 } 578 if (connp->conn_family != AF_INET6 || 579 sa->sa_family != AF_INET6) { 580 udp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT); 581 return; 582 } 583 break; 584 585 default: /* Invalid request */ 586 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 587 "udp_bind: bad ADDR_length length %u", tbr->ADDR_length); 588 udp_err_ack(q, mp, TBADADDR, 0); 589 return; 590 } 591 592 error = udp_do_bind(connp, sa, tbr->ADDR_length, cr, 593 tbr->PRIM_type != O_T_BIND_REQ); 594 595 if (error != 0) { 596 if (error > 0) { 597 udp_err_ack(q, mp, TSYSERR, error); 598 } else { 599 udp_err_ack(q, mp, -error, 0); 600 } 601 } else { 602 tbr->PRIM_type = T_BIND_ACK; 603 qreply(q, mp); 604 } 605 } 606 607 /* 608 * This routine handles each T_CONN_REQ message passed to udp. It 609 * associates a default destination address with the stream. 610 * 611 * After various error checks are completed, udp_connect() lays 612 * the target address and port into the composite header template. 613 * Then we ask IP for information, including a source address if we didn't 614 * already have one. Finally we send up the T_OK_ACK reply message. 615 */ 616 static void 617 udp_tpi_connect(queue_t *q, mblk_t *mp) 618 { 619 conn_t *connp = Q_TO_CONN(q); 620 int error; 621 socklen_t len; 622 struct sockaddr *sa; 623 struct T_conn_req *tcr; 624 cred_t *cr; 625 pid_t pid; 626 /* 627 * All Solaris components should pass a db_credp 628 * for this TPI message, hence we ASSERT. 629 * But in case there is some other M_PROTO that looks 630 * like a TPI message sent by some other kernel 631 * component, we check and return an error. 632 */ 633 cr = msg_getcred(mp, &pid); 634 ASSERT(cr != NULL); 635 if (cr == NULL) { 636 udp_err_ack(q, mp, TSYSERR, EINVAL); 637 return; 638 } 639 640 tcr = (struct T_conn_req *)mp->b_rptr; 641 642 /* A bit of sanity checking */ 643 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_req)) { 644 udp_err_ack(q, mp, TPROTO, 0); 645 return; 646 } 647 648 if (tcr->OPT_length != 0) { 649 udp_err_ack(q, mp, TBADOPT, 0); 650 return; 651 } 652 653 /* 654 * Determine packet type based on type of address passed in 655 * the request should contain an IPv4 or IPv6 address. 656 * Make sure that address family matches the type of 657 * family of the address passed down. 658 */ 659 len = tcr->DEST_length; 660 switch (tcr->DEST_length) { 661 default: 662 udp_err_ack(q, mp, TBADADDR, 0); 663 return; 664 665 case sizeof (sin_t): 666 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 667 sizeof (sin_t)); 668 break; 669 670 case sizeof (sin6_t): 671 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 672 sizeof (sin6_t)); 673 break; 674 } 675 676 error = proto_verify_ip_addr(connp->conn_family, sa, len); 677 if (error != 0) { 678 udp_err_ack(q, mp, TSYSERR, error); 679 return; 680 } 681 682 error = udp_do_connect(connp, sa, len, cr, pid); 683 if (error != 0) { 684 if (error < 0) 685 udp_err_ack(q, mp, -error, 0); 686 else 687 udp_err_ack(q, mp, TSYSERR, error); 688 } else { 689 mblk_t *mp1; 690 /* 691 * We have to send a connection confirmation to 692 * keep TLI happy. 693 */ 694 if (connp->conn_family == AF_INET) { 695 mp1 = mi_tpi_conn_con(NULL, (char *)sa, 696 sizeof (sin_t), NULL, 0); 697 } else { 698 mp1 = mi_tpi_conn_con(NULL, (char *)sa, 699 sizeof (sin6_t), NULL, 0); 700 } 701 if (mp1 == NULL) { 702 udp_err_ack(q, mp, TSYSERR, ENOMEM); 703 return; 704 } 705 706 /* 707 * Send ok_ack for T_CONN_REQ 708 */ 709 mp = mi_tpi_ok_ack_alloc(mp); 710 if (mp == NULL) { 711 /* Unable to reuse the T_CONN_REQ for the ack. */ 712 udp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM); 713 return; 714 } 715 716 putnext(connp->conn_rq, mp); 717 putnext(connp->conn_rq, mp1); 718 } 719 } 720 721 /* ARGSUSED */ 722 static int 723 udp_tpi_close(queue_t *q, int flags, cred_t *credp __unused) 724 { 725 conn_t *connp; 726 727 if (flags & SO_FALLBACK) { 728 /* 729 * stream is being closed while in fallback 730 * simply free the resources that were allocated 731 */ 732 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr)); 733 qprocsoff(q); 734 goto done; 735 } 736 737 connp = Q_TO_CONN(q); 738 udp_do_close(connp); 739 done: 740 q->q_ptr = WR(q)->q_ptr = NULL; 741 return (0); 742 } 743 744 static void 745 udp_close_free(conn_t *connp) 746 { 747 udp_t *udp = connp->conn_udp; 748 749 /* If there are any options associated with the stream, free them. */ 750 if (udp->udp_recv_ipp.ipp_fields != 0) 751 ip_pkt_free(&udp->udp_recv_ipp); 752 753 /* 754 * Clear any fields which the kmem_cache constructor clears. 755 * Only udp_connp needs to be preserved. 756 * TBD: We should make this more efficient to avoid clearing 757 * everything. 758 */ 759 ASSERT(udp->udp_connp == connp); 760 bzero(udp, sizeof (udp_t)); 761 udp->udp_connp = connp; 762 } 763 764 static int 765 udp_do_disconnect(conn_t *connp) 766 { 767 udp_t *udp; 768 udp_fanout_t *udpf; 769 udp_stack_t *us; 770 int error; 771 772 udp = connp->conn_udp; 773 us = udp->udp_us; 774 mutex_enter(&connp->conn_lock); 775 if (udp->udp_state != TS_DATA_XFER) { 776 mutex_exit(&connp->conn_lock); 777 return (-TOUTSTATE); 778 } 779 udpf = &us->us_bind_fanout[UDP_BIND_HASH(connp->conn_lport, 780 us->us_bind_fanout_size)]; 781 mutex_enter(&udpf->uf_lock); 782 if (connp->conn_mcbc_bind) 783 connp->conn_saddr_v6 = ipv6_all_zeros; 784 else 785 connp->conn_saddr_v6 = connp->conn_bound_addr_v6; 786 connp->conn_laddr_v6 = connp->conn_bound_addr_v6; 787 connp->conn_faddr_v6 = ipv6_all_zeros; 788 connp->conn_fport = 0; 789 udp->udp_state = TS_IDLE; 790 mutex_exit(&udpf->uf_lock); 791 792 /* Remove any remnants of mapped address binding */ 793 if (connp->conn_family == AF_INET6) 794 connp->conn_ipversion = IPV6_VERSION; 795 796 connp->conn_v6lastdst = ipv6_all_zeros; 797 error = udp_build_hdr_template(connp, &connp->conn_saddr_v6, 798 &connp->conn_faddr_v6, connp->conn_fport, connp->conn_flowinfo); 799 mutex_exit(&connp->conn_lock); 800 if (error != 0) 801 return (error); 802 803 /* 804 * Tell IP to remove the full binding and revert 805 * to the local address binding. 806 */ 807 return (ip_laddr_fanout_insert(connp)); 808 } 809 810 static void 811 udp_tpi_disconnect(queue_t *q, mblk_t *mp) 812 { 813 conn_t *connp = Q_TO_CONN(q); 814 int error; 815 816 /* 817 * Allocate the largest primitive we need to send back 818 * T_error_ack is > than T_ok_ack 819 */ 820 mp = reallocb(mp, sizeof (struct T_error_ack), 1); 821 if (mp == NULL) { 822 /* Unable to reuse the T_DISCON_REQ for the ack. */ 823 udp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, ENOMEM); 824 return; 825 } 826 827 error = udp_do_disconnect(connp); 828 829 if (error != 0) { 830 if (error < 0) { 831 udp_err_ack(q, mp, -error, 0); 832 } else { 833 udp_err_ack(q, mp, TSYSERR, error); 834 } 835 } else { 836 mp = mi_tpi_ok_ack_alloc(mp); 837 ASSERT(mp != NULL); 838 qreply(q, mp); 839 } 840 } 841 842 int 843 udp_disconnect(conn_t *connp) 844 { 845 int error; 846 847 connp->conn_dgram_errind = B_FALSE; 848 error = udp_do_disconnect(connp); 849 if (error < 0) 850 error = proto_tlitosyserr(-error); 851 852 return (error); 853 } 854 855 /* This routine creates a T_ERROR_ACK message and passes it upstream. */ 856 static void 857 udp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error) 858 { 859 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 860 qreply(q, mp); 861 } 862 863 /* Shorthand to generate and send TPI error acks to our client */ 864 static void 865 udp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive, 866 t_scalar_t t_error, int sys_error) 867 { 868 struct T_error_ack *teackp; 869 870 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 871 M_PCPROTO, T_ERROR_ACK)) != NULL) { 872 teackp = (struct T_error_ack *)mp->b_rptr; 873 teackp->ERROR_prim = primitive; 874 teackp->TLI_error = t_error; 875 teackp->UNIX_error = sys_error; 876 qreply(q, mp); 877 } 878 } 879 880 /* At minimum we need 4 bytes of UDP header */ 881 #define ICMP_MIN_UDP_HDR 4 882 883 /* 884 * udp_icmp_input is called as conn_recvicmp to process ICMP messages. 885 * Generates the appropriate T_UDERROR_IND for permanent (non-transient) errors. 886 * Assumes that IP has pulled up everything up to and including the ICMP header. 887 */ 888 /* ARGSUSED2 */ 889 static void 890 udp_icmp_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira) 891 { 892 conn_t *connp = (conn_t *)arg1; 893 icmph_t *icmph; 894 ipha_t *ipha; 895 int iph_hdr_length; 896 udpha_t *udpha; 897 sin_t sin; 898 sin6_t sin6; 899 mblk_t *mp1; 900 int error = 0; 901 udp_t *udp = connp->conn_udp; 902 903 ipha = (ipha_t *)mp->b_rptr; 904 905 ASSERT(OK_32PTR(mp->b_rptr)); 906 907 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) { 908 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION); 909 udp_icmp_error_ipv6(connp, mp, ira); 910 return; 911 } 912 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 913 914 /* Skip past the outer IP and ICMP headers */ 915 ASSERT(IPH_HDR_LENGTH(ipha) == ira->ira_ip_hdr_length); 916 iph_hdr_length = ira->ira_ip_hdr_length; 917 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 918 ipha = (ipha_t *)&icmph[1]; /* Inner IP header */ 919 920 /* Skip past the inner IP and find the ULP header */ 921 iph_hdr_length = IPH_HDR_LENGTH(ipha); 922 udpha = (udpha_t *)((char *)ipha + iph_hdr_length); 923 924 switch (icmph->icmph_type) { 925 case ICMP_DEST_UNREACHABLE: 926 switch (icmph->icmph_code) { 927 case ICMP_FRAGMENTATION_NEEDED: { 928 ipha_t *ipha; 929 ip_xmit_attr_t *ixa; 930 /* 931 * IP has already adjusted the path MTU. 932 * But we need to adjust DF for IPv4. 933 */ 934 if (connp->conn_ipversion != IPV4_VERSION) 935 break; 936 937 ixa = conn_get_ixa(connp, B_FALSE); 938 if (ixa == NULL || ixa->ixa_ire == NULL) { 939 /* 940 * Some other thread holds conn_ixa. We will 941 * redo this on the next ICMP too big. 942 */ 943 if (ixa != NULL) 944 ixa_refrele(ixa); 945 break; 946 } 947 (void) ip_get_pmtu(ixa); 948 949 mutex_enter(&connp->conn_lock); 950 ipha = (ipha_t *)connp->conn_ht_iphc; 951 if (ixa->ixa_flags & IXAF_PMTU_IPV4_DF) { 952 ipha->ipha_fragment_offset_and_flags |= 953 IPH_DF_HTONS; 954 } else { 955 ipha->ipha_fragment_offset_and_flags &= 956 ~IPH_DF_HTONS; 957 } 958 mutex_exit(&connp->conn_lock); 959 ixa_refrele(ixa); 960 break; 961 } 962 case ICMP_PORT_UNREACHABLE: 963 case ICMP_PROTOCOL_UNREACHABLE: 964 error = ECONNREFUSED; 965 break; 966 default: 967 /* Transient errors */ 968 break; 969 } 970 break; 971 default: 972 /* Transient errors */ 973 break; 974 } 975 if (error == 0) { 976 freemsg(mp); 977 return; 978 } 979 980 /* 981 * Deliver T_UDERROR_IND when the application has asked for it. 982 * The socket layer enables this automatically when connected. 983 */ 984 if (!connp->conn_dgram_errind) { 985 freemsg(mp); 986 return; 987 } 988 989 switch (connp->conn_family) { 990 case AF_INET: 991 sin = sin_null; 992 sin.sin_family = AF_INET; 993 sin.sin_addr.s_addr = ipha->ipha_dst; 994 sin.sin_port = udpha->uha_dst_port; 995 if (IPCL_IS_NONSTR(connp)) { 996 mutex_enter(&connp->conn_lock); 997 if (udp->udp_state == TS_DATA_XFER) { 998 if (sin.sin_port == connp->conn_fport && 999 sin.sin_addr.s_addr == 1000 connp->conn_faddr_v4) { 1001 mutex_exit(&connp->conn_lock); 1002 (*connp->conn_upcalls->su_set_error) 1003 (connp->conn_upper_handle, error); 1004 goto done; 1005 } 1006 } else { 1007 udp->udp_delayed_error = error; 1008 *((sin_t *)&udp->udp_delayed_addr) = sin; 1009 } 1010 mutex_exit(&connp->conn_lock); 1011 } else { 1012 mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), 1013 NULL, 0, error); 1014 if (mp1 != NULL) 1015 putnext(connp->conn_rq, mp1); 1016 } 1017 break; 1018 case AF_INET6: 1019 sin6 = sin6_null; 1020 sin6.sin6_family = AF_INET6; 1021 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr); 1022 sin6.sin6_port = udpha->uha_dst_port; 1023 if (IPCL_IS_NONSTR(connp)) { 1024 mutex_enter(&connp->conn_lock); 1025 if (udp->udp_state == TS_DATA_XFER) { 1026 if (sin6.sin6_port == connp->conn_fport && 1027 IN6_ARE_ADDR_EQUAL(&sin6.sin6_addr, 1028 &connp->conn_faddr_v6)) { 1029 mutex_exit(&connp->conn_lock); 1030 (*connp->conn_upcalls->su_set_error) 1031 (connp->conn_upper_handle, error); 1032 goto done; 1033 } 1034 } else { 1035 udp->udp_delayed_error = error; 1036 *((sin6_t *)&udp->udp_delayed_addr) = sin6; 1037 } 1038 mutex_exit(&connp->conn_lock); 1039 } else { 1040 mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), 1041 NULL, 0, error); 1042 if (mp1 != NULL) 1043 putnext(connp->conn_rq, mp1); 1044 } 1045 break; 1046 } 1047 done: 1048 freemsg(mp); 1049 } 1050 1051 /* 1052 * udp_icmp_error_ipv6 is called by udp_icmp_error to process ICMP for IPv6. 1053 * Generates the appropriate T_UDERROR_IND for permanent (non-transient) errors. 1054 * Assumes that IP has pulled up all the extension headers as well as the 1055 * ICMPv6 header. 1056 */ 1057 static void 1058 udp_icmp_error_ipv6(conn_t *connp, mblk_t *mp, ip_recv_attr_t *ira) 1059 { 1060 icmp6_t *icmp6; 1061 ip6_t *ip6h, *outer_ip6h; 1062 uint16_t iph_hdr_length; 1063 uint8_t *nexthdrp; 1064 udpha_t *udpha; 1065 sin6_t sin6; 1066 mblk_t *mp1; 1067 int error = 0; 1068 udp_t *udp = connp->conn_udp; 1069 udp_stack_t *us = udp->udp_us; 1070 1071 outer_ip6h = (ip6_t *)mp->b_rptr; 1072 #ifdef DEBUG 1073 if (outer_ip6h->ip6_nxt != IPPROTO_ICMPV6) 1074 iph_hdr_length = ip_hdr_length_v6(mp, outer_ip6h); 1075 else 1076 iph_hdr_length = IPV6_HDR_LEN; 1077 ASSERT(iph_hdr_length == ira->ira_ip_hdr_length); 1078 #endif 1079 /* Skip past the outer IP and ICMP headers */ 1080 iph_hdr_length = ira->ira_ip_hdr_length; 1081 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 1082 1083 /* Skip past the inner IP and find the ULP header */ 1084 ip6h = (ip6_t *)&icmp6[1]; /* Inner IP header */ 1085 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) { 1086 freemsg(mp); 1087 return; 1088 } 1089 udpha = (udpha_t *)((char *)ip6h + iph_hdr_length); 1090 1091 switch (icmp6->icmp6_type) { 1092 case ICMP6_DST_UNREACH: 1093 switch (icmp6->icmp6_code) { 1094 case ICMP6_DST_UNREACH_NOPORT: 1095 error = ECONNREFUSED; 1096 break; 1097 case ICMP6_DST_UNREACH_ADMIN: 1098 case ICMP6_DST_UNREACH_NOROUTE: 1099 case ICMP6_DST_UNREACH_BEYONDSCOPE: 1100 case ICMP6_DST_UNREACH_ADDR: 1101 /* Transient errors */ 1102 break; 1103 default: 1104 break; 1105 } 1106 break; 1107 case ICMP6_PACKET_TOO_BIG: { 1108 struct T_unitdata_ind *tudi; 1109 struct T_opthdr *toh; 1110 size_t udi_size; 1111 mblk_t *newmp; 1112 t_scalar_t opt_length = sizeof (struct T_opthdr) + 1113 sizeof (struct ip6_mtuinfo); 1114 sin6_t *sin6; 1115 struct ip6_mtuinfo *mtuinfo; 1116 1117 /* 1118 * If the application has requested to receive path mtu 1119 * information, send up an empty message containing an 1120 * IPV6_PATHMTU ancillary data item. 1121 */ 1122 if (!connp->conn_ipv6_recvpathmtu) 1123 break; 1124 1125 udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) + 1126 opt_length; 1127 if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) { 1128 UDPS_BUMP_MIB(us, udpInErrors); 1129 break; 1130 } 1131 1132 /* 1133 * newmp->b_cont is left to NULL on purpose. This is an 1134 * empty message containing only ancillary data. 1135 */ 1136 newmp->b_datap->db_type = M_PROTO; 1137 tudi = (struct T_unitdata_ind *)newmp->b_rptr; 1138 newmp->b_wptr = (uchar_t *)tudi + udi_size; 1139 tudi->PRIM_type = T_UNITDATA_IND; 1140 tudi->SRC_length = sizeof (sin6_t); 1141 tudi->SRC_offset = sizeof (struct T_unitdata_ind); 1142 tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t); 1143 tudi->OPT_length = opt_length; 1144 1145 sin6 = (sin6_t *)&tudi[1]; 1146 bzero(sin6, sizeof (sin6_t)); 1147 sin6->sin6_family = AF_INET6; 1148 sin6->sin6_addr = connp->conn_faddr_v6; 1149 1150 toh = (struct T_opthdr *)&sin6[1]; 1151 toh->level = IPPROTO_IPV6; 1152 toh->name = IPV6_PATHMTU; 1153 toh->len = opt_length; 1154 toh->status = 0; 1155 1156 mtuinfo = (struct ip6_mtuinfo *)&toh[1]; 1157 bzero(mtuinfo, sizeof (struct ip6_mtuinfo)); 1158 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 1159 mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst; 1160 mtuinfo->ip6m_mtu = icmp6->icmp6_mtu; 1161 /* 1162 * We've consumed everything we need from the original 1163 * message. Free it, then send our empty message. 1164 */ 1165 freemsg(mp); 1166 udp_ulp_recv(connp, newmp, msgdsize(newmp), ira); 1167 return; 1168 } 1169 case ICMP6_TIME_EXCEEDED: 1170 /* Transient errors */ 1171 break; 1172 case ICMP6_PARAM_PROB: 1173 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 1174 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 1175 (uchar_t *)ip6h + icmp6->icmp6_pptr == 1176 (uchar_t *)nexthdrp) { 1177 error = ECONNREFUSED; 1178 break; 1179 } 1180 break; 1181 } 1182 if (error == 0) { 1183 freemsg(mp); 1184 return; 1185 } 1186 1187 /* 1188 * Deliver T_UDERROR_IND when the application has asked for it. 1189 * The socket layer enables this automatically when connected. 1190 */ 1191 if (!connp->conn_dgram_errind) { 1192 freemsg(mp); 1193 return; 1194 } 1195 1196 sin6 = sin6_null; 1197 sin6.sin6_family = AF_INET6; 1198 sin6.sin6_addr = ip6h->ip6_dst; 1199 sin6.sin6_port = udpha->uha_dst_port; 1200 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 1201 1202 if (IPCL_IS_NONSTR(connp)) { 1203 mutex_enter(&connp->conn_lock); 1204 if (udp->udp_state == TS_DATA_XFER) { 1205 if (sin6.sin6_port == connp->conn_fport && 1206 IN6_ARE_ADDR_EQUAL(&sin6.sin6_addr, 1207 &connp->conn_faddr_v6)) { 1208 mutex_exit(&connp->conn_lock); 1209 (*connp->conn_upcalls->su_set_error) 1210 (connp->conn_upper_handle, error); 1211 goto done; 1212 } 1213 } else { 1214 udp->udp_delayed_error = error; 1215 *((sin6_t *)&udp->udp_delayed_addr) = sin6; 1216 } 1217 mutex_exit(&connp->conn_lock); 1218 } else { 1219 mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), 1220 NULL, 0, error); 1221 if (mp1 != NULL) 1222 putnext(connp->conn_rq, mp1); 1223 } 1224 done: 1225 freemsg(mp); 1226 } 1227 1228 /* 1229 * This routine responds to T_ADDR_REQ messages. It is called by udp_wput. 1230 * The local address is filled in if endpoint is bound. The remote address 1231 * is filled in if remote address has been precified ("connected endpoint") 1232 * (The concept of connected CLTS sockets is alien to published TPI 1233 * but we support it anyway). 1234 */ 1235 static void 1236 udp_addr_req(queue_t *q, mblk_t *mp) 1237 { 1238 struct sockaddr *sa; 1239 mblk_t *ackmp; 1240 struct T_addr_ack *taa; 1241 udp_t *udp = Q_TO_UDP(q); 1242 conn_t *connp = udp->udp_connp; 1243 uint_t addrlen; 1244 1245 /* Make it large enough for worst case */ 1246 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 1247 2 * sizeof (sin6_t), 1); 1248 if (ackmp == NULL) { 1249 udp_err_ack(q, mp, TSYSERR, ENOMEM); 1250 return; 1251 } 1252 taa = (struct T_addr_ack *)ackmp->b_rptr; 1253 1254 bzero(taa, sizeof (struct T_addr_ack)); 1255 ackmp->b_wptr = (uchar_t *)&taa[1]; 1256 1257 taa->PRIM_type = T_ADDR_ACK; 1258 ackmp->b_datap->db_type = M_PCPROTO; 1259 1260 if (connp->conn_family == AF_INET) 1261 addrlen = sizeof (sin_t); 1262 else 1263 addrlen = sizeof (sin6_t); 1264 1265 mutex_enter(&connp->conn_lock); 1266 /* 1267 * Note: Following code assumes 32 bit alignment of basic 1268 * data structures like sin_t and struct T_addr_ack. 1269 */ 1270 if (udp->udp_state != TS_UNBND) { 1271 /* 1272 * Fill in local address first 1273 */ 1274 taa->LOCADDR_offset = sizeof (*taa); 1275 taa->LOCADDR_length = addrlen; 1276 sa = (struct sockaddr *)&taa[1]; 1277 (void) conn_getsockname(connp, sa, &addrlen); 1278 ackmp->b_wptr += addrlen; 1279 } 1280 if (udp->udp_state == TS_DATA_XFER) { 1281 /* 1282 * connected, fill remote address too 1283 */ 1284 taa->REMADDR_length = addrlen; 1285 /* assumed 32-bit alignment */ 1286 taa->REMADDR_offset = taa->LOCADDR_offset + taa->LOCADDR_length; 1287 sa = (struct sockaddr *)(ackmp->b_rptr + taa->REMADDR_offset); 1288 (void) conn_getpeername(connp, sa, &addrlen); 1289 ackmp->b_wptr += addrlen; 1290 } 1291 mutex_exit(&connp->conn_lock); 1292 ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim); 1293 qreply(q, ackmp); 1294 } 1295 1296 static void 1297 udp_copy_info(struct T_info_ack *tap, udp_t *udp) 1298 { 1299 conn_t *connp = udp->udp_connp; 1300 1301 if (connp->conn_family == AF_INET) { 1302 *tap = udp_g_t_info_ack_ipv4; 1303 } else { 1304 *tap = udp_g_t_info_ack_ipv6; 1305 } 1306 tap->CURRENT_state = udp->udp_state; 1307 tap->OPT_size = udp_max_optsize; 1308 } 1309 1310 static void 1311 udp_do_capability_ack(udp_t *udp, struct T_capability_ack *tcap, 1312 t_uscalar_t cap_bits1) 1313 { 1314 tcap->CAP_bits1 = 0; 1315 1316 if (cap_bits1 & TC1_INFO) { 1317 udp_copy_info(&tcap->INFO_ack, udp); 1318 tcap->CAP_bits1 |= TC1_INFO; 1319 } 1320 } 1321 1322 /* 1323 * This routine responds to T_CAPABILITY_REQ messages. It is called by 1324 * udp_wput. Much of the T_CAPABILITY_ACK information is copied from 1325 * udp_g_t_info_ack. The current state of the stream is copied from 1326 * udp_state. 1327 */ 1328 static void 1329 udp_capability_req(queue_t *q, mblk_t *mp) 1330 { 1331 t_uscalar_t cap_bits1; 1332 struct T_capability_ack *tcap; 1333 udp_t *udp = Q_TO_UDP(q); 1334 1335 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 1336 1337 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 1338 mp->b_datap->db_type, T_CAPABILITY_ACK); 1339 if (!mp) 1340 return; 1341 1342 tcap = (struct T_capability_ack *)mp->b_rptr; 1343 udp_do_capability_ack(udp, tcap, cap_bits1); 1344 1345 qreply(q, mp); 1346 } 1347 1348 /* 1349 * This routine responds to T_INFO_REQ messages. It is called by udp_wput. 1350 * Most of the T_INFO_ACK information is copied from udp_g_t_info_ack. 1351 * The current state of the stream is copied from udp_state. 1352 */ 1353 static void 1354 udp_info_req(queue_t *q, mblk_t *mp) 1355 { 1356 udp_t *udp = Q_TO_UDP(q); 1357 1358 /* Create a T_INFO_ACK message. */ 1359 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 1360 T_INFO_ACK); 1361 if (!mp) 1362 return; 1363 udp_copy_info((struct T_info_ack *)mp->b_rptr, udp); 1364 qreply(q, mp); 1365 } 1366 1367 /* For /dev/udp aka AF_INET open */ 1368 static int 1369 udp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 1370 { 1371 return (udp_open(q, devp, flag, sflag, credp, B_FALSE)); 1372 } 1373 1374 /* For /dev/udp6 aka AF_INET6 open */ 1375 static int 1376 udp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 1377 { 1378 return (udp_open(q, devp, flag, sflag, credp, B_TRUE)); 1379 } 1380 1381 /* 1382 * This is the open routine for udp. It allocates a udp_t structure for 1383 * the stream and, on the first open of the module, creates an ND table. 1384 */ 1385 static int 1386 udp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 1387 boolean_t isv6) 1388 { 1389 udp_t *udp; 1390 conn_t *connp; 1391 dev_t conn_dev; 1392 vmem_t *minor_arena; 1393 int err; 1394 1395 /* If the stream is already open, return immediately. */ 1396 if (q->q_ptr != NULL) 1397 return (0); 1398 1399 if (sflag == MODOPEN) 1400 return (EINVAL); 1401 1402 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 1403 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 1404 minor_arena = ip_minor_arena_la; 1405 } else { 1406 /* 1407 * Either minor numbers in the large arena were exhausted 1408 * or a non socket application is doing the open. 1409 * Try to allocate from the small arena. 1410 */ 1411 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) 1412 return (EBUSY); 1413 1414 minor_arena = ip_minor_arena_sa; 1415 } 1416 1417 if (flag & SO_FALLBACK) { 1418 /* 1419 * Non streams socket needs a stream to fallback to 1420 */ 1421 RD(q)->q_ptr = (void *)conn_dev; 1422 WR(q)->q_qinfo = &udp_fallback_sock_winit; 1423 WR(q)->q_ptr = (void *)minor_arena; 1424 qprocson(q); 1425 return (0); 1426 } 1427 1428 connp = udp_do_open(credp, isv6, KM_SLEEP, &err); 1429 if (connp == NULL) { 1430 inet_minor_free(minor_arena, conn_dev); 1431 return (err); 1432 } 1433 udp = connp->conn_udp; 1434 1435 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 1436 connp->conn_dev = conn_dev; 1437 connp->conn_minor_arena = minor_arena; 1438 1439 /* 1440 * Initialize the udp_t structure for this stream. 1441 */ 1442 q->q_ptr = connp; 1443 WR(q)->q_ptr = connp; 1444 connp->conn_rq = q; 1445 connp->conn_wq = WR(q); 1446 1447 /* 1448 * Since this conn_t/udp_t is not yet visible to anybody else we don't 1449 * need to lock anything. 1450 */ 1451 ASSERT(connp->conn_proto == IPPROTO_UDP); 1452 ASSERT(connp->conn_udp == udp); 1453 ASSERT(udp->udp_connp == connp); 1454 1455 if (flag & SO_SOCKSTR) { 1456 udp->udp_issocket = B_TRUE; 1457 } 1458 1459 WR(q)->q_hiwat = connp->conn_sndbuf; 1460 WR(q)->q_lowat = connp->conn_sndlowat; 1461 1462 qprocson(q); 1463 1464 /* Set the Stream head write offset and high watermark. */ 1465 (void) proto_set_tx_wroff(q, connp, connp->conn_wroff); 1466 (void) proto_set_rx_hiwat(q, connp, 1467 udp_set_rcv_hiwat(udp, connp->conn_rcvbuf)); 1468 1469 mutex_enter(&connp->conn_lock); 1470 connp->conn_state_flags &= ~CONN_INCIPIENT; 1471 mutex_exit(&connp->conn_lock); 1472 return (0); 1473 } 1474 1475 /* 1476 * Which UDP options OK to set through T_UNITDATA_REQ... 1477 */ 1478 /* ARGSUSED */ 1479 static boolean_t 1480 udp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name) 1481 { 1482 return (B_TRUE); 1483 } 1484 1485 /* 1486 * This routine gets default values of certain options whose default 1487 * values are maintained by protcol specific code 1488 */ 1489 int 1490 udp_opt_default(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr) 1491 { 1492 udp_t *udp = Q_TO_UDP(q); 1493 udp_stack_t *us = udp->udp_us; 1494 int *i1 = (int *)ptr; 1495 1496 switch (level) { 1497 case IPPROTO_IP: 1498 switch (name) { 1499 case IP_MULTICAST_TTL: 1500 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 1501 return (sizeof (uchar_t)); 1502 case IP_MULTICAST_LOOP: 1503 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 1504 return (sizeof (uchar_t)); 1505 } 1506 break; 1507 case IPPROTO_IPV6: 1508 switch (name) { 1509 case IPV6_MULTICAST_HOPS: 1510 *i1 = IP_DEFAULT_MULTICAST_TTL; 1511 return (sizeof (int)); 1512 case IPV6_MULTICAST_LOOP: 1513 *i1 = IP_DEFAULT_MULTICAST_LOOP; 1514 return (sizeof (int)); 1515 case IPV6_UNICAST_HOPS: 1516 *i1 = us->us_ipv6_hoplimit; 1517 return (sizeof (int)); 1518 } 1519 break; 1520 } 1521 return (-1); 1522 } 1523 1524 /* 1525 * This routine retrieves the current status of socket options. 1526 * It returns the size of the option retrieved, or -1. 1527 */ 1528 int 1529 udp_opt_get(conn_t *connp, t_scalar_t level, t_scalar_t name, 1530 uchar_t *ptr) 1531 { 1532 int *i1 = (int *)ptr; 1533 udp_t *udp = connp->conn_udp; 1534 int len; 1535 conn_opt_arg_t coas; 1536 int retval; 1537 1538 coas.coa_connp = connp; 1539 coas.coa_ixa = connp->conn_ixa; 1540 coas.coa_ipp = &connp->conn_xmit_ipp; 1541 coas.coa_ancillary = B_FALSE; 1542 coas.coa_changed = 0; 1543 1544 /* 1545 * We assume that the optcom framework has checked for the set 1546 * of levels and names that are supported, hence we don't worry 1547 * about rejecting based on that. 1548 * First check for UDP specific handling, then pass to common routine. 1549 */ 1550 switch (level) { 1551 case IPPROTO_IP: 1552 /* 1553 * Only allow IPv4 option processing on IPv4 sockets. 1554 */ 1555 if (connp->conn_family != AF_INET) 1556 return (-1); 1557 1558 switch (name) { 1559 case IP_OPTIONS: 1560 case T_IP_OPTIONS: 1561 mutex_enter(&connp->conn_lock); 1562 if (!(udp->udp_recv_ipp.ipp_fields & 1563 IPPF_IPV4_OPTIONS)) { 1564 mutex_exit(&connp->conn_lock); 1565 return (0); 1566 } 1567 1568 len = udp->udp_recv_ipp.ipp_ipv4_options_len; 1569 ASSERT(len != 0); 1570 bcopy(udp->udp_recv_ipp.ipp_ipv4_options, ptr, len); 1571 mutex_exit(&connp->conn_lock); 1572 return (len); 1573 } 1574 break; 1575 case IPPROTO_UDP: 1576 switch (name) { 1577 case UDP_NAT_T_ENDPOINT: 1578 mutex_enter(&connp->conn_lock); 1579 *i1 = udp->udp_nat_t_endpoint; 1580 mutex_exit(&connp->conn_lock); 1581 return (sizeof (int)); 1582 case UDP_RCVHDR: 1583 mutex_enter(&connp->conn_lock); 1584 *i1 = udp->udp_rcvhdr ? 1 : 0; 1585 mutex_exit(&connp->conn_lock); 1586 return (sizeof (int)); 1587 } 1588 } 1589 mutex_enter(&connp->conn_lock); 1590 retval = conn_opt_get(&coas, level, name, ptr); 1591 mutex_exit(&connp->conn_lock); 1592 return (retval); 1593 } 1594 1595 /* 1596 * This routine retrieves the current status of socket options. 1597 * It returns the size of the option retrieved, or -1. 1598 */ 1599 int 1600 udp_tpi_opt_get(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr) 1601 { 1602 conn_t *connp = Q_TO_CONN(q); 1603 int err; 1604 1605 err = udp_opt_get(connp, level, name, ptr); 1606 return (err); 1607 } 1608 1609 /* 1610 * This routine sets socket options. 1611 */ 1612 int 1613 udp_do_opt_set(conn_opt_arg_t *coa, int level, int name, 1614 uint_t inlen, uchar_t *invalp, cred_t *cr, boolean_t checkonly) 1615 { 1616 conn_t *connp = coa->coa_connp; 1617 ip_xmit_attr_t *ixa = coa->coa_ixa; 1618 udp_t *udp = connp->conn_udp; 1619 udp_stack_t *us = udp->udp_us; 1620 int *i1 = (int *)invalp; 1621 boolean_t onoff = (*i1 == 0) ? 0 : 1; 1622 int error; 1623 1624 ASSERT(MUTEX_NOT_HELD(&coa->coa_connp->conn_lock)); 1625 /* 1626 * First do UDP specific sanity checks and handle UDP specific 1627 * options. Note that some IPPROTO_UDP options are handled 1628 * by conn_opt_set. 1629 */ 1630 switch (level) { 1631 case SOL_SOCKET: 1632 switch (name) { 1633 case SO_SNDBUF: 1634 if (*i1 > us->us_max_buf) { 1635 return (ENOBUFS); 1636 } 1637 break; 1638 case SO_RCVBUF: 1639 if (*i1 > us->us_max_buf) { 1640 return (ENOBUFS); 1641 } 1642 break; 1643 1644 case SCM_UCRED: { 1645 struct ucred_s *ucr; 1646 cred_t *newcr; 1647 ts_label_t *tsl; 1648 1649 /* 1650 * Only sockets that have proper privileges and are 1651 * bound to MLPs will have any other value here, so 1652 * this implicitly tests for privilege to set label. 1653 */ 1654 if (connp->conn_mlp_type == mlptSingle) 1655 break; 1656 1657 ucr = (struct ucred_s *)invalp; 1658 if (inlen < sizeof (*ucr) + sizeof (bslabel_t) || 1659 ucr->uc_labeloff < sizeof (*ucr) || 1660 ucr->uc_labeloff + sizeof (bslabel_t) > inlen) 1661 return (EINVAL); 1662 if (!checkonly) { 1663 /* 1664 * Set ixa_tsl to the new label. 1665 * We assume that crgetzoneid doesn't change 1666 * as part of the SCM_UCRED. 1667 */ 1668 ASSERT(cr != NULL); 1669 if ((tsl = crgetlabel(cr)) == NULL) 1670 return (EINVAL); 1671 newcr = copycred_from_bslabel(cr, UCLABEL(ucr), 1672 tsl->tsl_doi, KM_NOSLEEP); 1673 if (newcr == NULL) 1674 return (ENOSR); 1675 ASSERT(newcr->cr_label != NULL); 1676 /* 1677 * Move the hold on the cr_label to ixa_tsl by 1678 * setting cr_label to NULL. Then release newcr. 1679 */ 1680 ip_xmit_attr_replace_tsl(ixa, newcr->cr_label); 1681 ixa->ixa_flags |= IXAF_UCRED_TSL; 1682 newcr->cr_label = NULL; 1683 crfree(newcr); 1684 coa->coa_changed |= COA_HEADER_CHANGED; 1685 coa->coa_changed |= COA_WROFF_CHANGED; 1686 } 1687 /* Fully handled this option. */ 1688 return (0); 1689 } 1690 } 1691 break; 1692 case IPPROTO_UDP: 1693 switch (name) { 1694 case UDP_NAT_T_ENDPOINT: 1695 if ((error = secpolicy_ip_config(cr, B_FALSE)) != 0) { 1696 return (error); 1697 } 1698 1699 /* 1700 * Use conn_family instead so we can avoid ambiguitites 1701 * with AF_INET6 sockets that may switch from IPv4 1702 * to IPv6. 1703 */ 1704 if (connp->conn_family != AF_INET) { 1705 return (EAFNOSUPPORT); 1706 } 1707 1708 if (!checkonly) { 1709 mutex_enter(&connp->conn_lock); 1710 udp->udp_nat_t_endpoint = onoff; 1711 mutex_exit(&connp->conn_lock); 1712 coa->coa_changed |= COA_HEADER_CHANGED; 1713 coa->coa_changed |= COA_WROFF_CHANGED; 1714 } 1715 /* Fully handled this option. */ 1716 return (0); 1717 case UDP_RCVHDR: 1718 mutex_enter(&connp->conn_lock); 1719 udp->udp_rcvhdr = onoff; 1720 mutex_exit(&connp->conn_lock); 1721 return (0); 1722 } 1723 break; 1724 } 1725 error = conn_opt_set(coa, level, name, inlen, invalp, 1726 checkonly, cr); 1727 return (error); 1728 } 1729 1730 /* 1731 * This routine sets socket options. 1732 */ 1733 int 1734 udp_opt_set(conn_t *connp, uint_t optset_context, int level, 1735 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 1736 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr) 1737 { 1738 udp_t *udp = connp->conn_udp; 1739 int err; 1740 conn_opt_arg_t coas, *coa; 1741 boolean_t checkonly; 1742 udp_stack_t *us = udp->udp_us; 1743 1744 switch (optset_context) { 1745 case SETFN_OPTCOM_CHECKONLY: 1746 checkonly = B_TRUE; 1747 /* 1748 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 1749 * inlen != 0 implies value supplied and 1750 * we have to "pretend" to set it. 1751 * inlen == 0 implies that there is no 1752 * value part in T_CHECK request and just validation 1753 * done elsewhere should be enough, we just return here. 1754 */ 1755 if (inlen == 0) { 1756 *outlenp = 0; 1757 return (0); 1758 } 1759 break; 1760 case SETFN_OPTCOM_NEGOTIATE: 1761 checkonly = B_FALSE; 1762 break; 1763 case SETFN_UD_NEGOTIATE: 1764 case SETFN_CONN_NEGOTIATE: 1765 checkonly = B_FALSE; 1766 /* 1767 * Negotiating local and "association-related" options 1768 * through T_UNITDATA_REQ. 1769 * 1770 * Following routine can filter out ones we do not 1771 * want to be "set" this way. 1772 */ 1773 if (!udp_opt_allow_udr_set(level, name)) { 1774 *outlenp = 0; 1775 return (EINVAL); 1776 } 1777 break; 1778 default: 1779 /* 1780 * We should never get here 1781 */ 1782 *outlenp = 0; 1783 return (EINVAL); 1784 } 1785 1786 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 1787 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 1788 1789 if (thisdg_attrs != NULL) { 1790 /* Options from T_UNITDATA_REQ */ 1791 coa = (conn_opt_arg_t *)thisdg_attrs; 1792 ASSERT(coa->coa_connp == connp); 1793 ASSERT(coa->coa_ixa != NULL); 1794 ASSERT(coa->coa_ipp != NULL); 1795 ASSERT(coa->coa_ancillary); 1796 } else { 1797 coa = &coas; 1798 coas.coa_connp = connp; 1799 /* Get a reference on conn_ixa to prevent concurrent mods */ 1800 coas.coa_ixa = conn_get_ixa(connp, B_TRUE); 1801 if (coas.coa_ixa == NULL) { 1802 *outlenp = 0; 1803 return (ENOMEM); 1804 } 1805 coas.coa_ipp = &connp->conn_xmit_ipp; 1806 coas.coa_ancillary = B_FALSE; 1807 coas.coa_changed = 0; 1808 } 1809 1810 err = udp_do_opt_set(coa, level, name, inlen, invalp, 1811 cr, checkonly); 1812 if (err != 0) { 1813 errout: 1814 if (!coa->coa_ancillary) 1815 ixa_refrele(coa->coa_ixa); 1816 *outlenp = 0; 1817 return (err); 1818 } 1819 /* Handle DHCPINIT here outside of lock */ 1820 if (level == IPPROTO_IP && name == IP_DHCPINIT_IF) { 1821 uint_t ifindex; 1822 ill_t *ill; 1823 1824 ifindex = *(uint_t *)invalp; 1825 if (ifindex == 0) { 1826 ill = NULL; 1827 } else { 1828 ill = ill_lookup_on_ifindex(ifindex, B_FALSE, 1829 coa->coa_ixa->ixa_ipst); 1830 if (ill == NULL) { 1831 err = ENXIO; 1832 goto errout; 1833 } 1834 1835 mutex_enter(&ill->ill_lock); 1836 if (ill->ill_state_flags & ILL_CONDEMNED) { 1837 mutex_exit(&ill->ill_lock); 1838 ill_refrele(ill); 1839 err = ENXIO; 1840 goto errout; 1841 } 1842 if (IS_VNI(ill)) { 1843 mutex_exit(&ill->ill_lock); 1844 ill_refrele(ill); 1845 err = EINVAL; 1846 goto errout; 1847 } 1848 } 1849 mutex_enter(&connp->conn_lock); 1850 1851 if (connp->conn_dhcpinit_ill != NULL) { 1852 /* 1853 * We've locked the conn so conn_cleanup_ill() 1854 * cannot clear conn_dhcpinit_ill -- so it's 1855 * safe to access the ill. 1856 */ 1857 ill_t *oill = connp->conn_dhcpinit_ill; 1858 1859 ASSERT(oill->ill_dhcpinit != 0); 1860 atomic_dec_32(&oill->ill_dhcpinit); 1861 ill_set_inputfn(connp->conn_dhcpinit_ill); 1862 connp->conn_dhcpinit_ill = NULL; 1863 } 1864 1865 if (ill != NULL) { 1866 connp->conn_dhcpinit_ill = ill; 1867 atomic_inc_32(&ill->ill_dhcpinit); 1868 ill_set_inputfn(ill); 1869 mutex_exit(&connp->conn_lock); 1870 mutex_exit(&ill->ill_lock); 1871 ill_refrele(ill); 1872 } else { 1873 mutex_exit(&connp->conn_lock); 1874 } 1875 } 1876 1877 /* 1878 * Common case of OK return with outval same as inval. 1879 */ 1880 if (invalp != outvalp) { 1881 /* don't trust bcopy for identical src/dst */ 1882 (void) bcopy(invalp, outvalp, inlen); 1883 } 1884 *outlenp = inlen; 1885 1886 /* 1887 * If this was not ancillary data, then we rebuild the headers, 1888 * update the IRE/NCE, and IPsec as needed. 1889 * Since the label depends on the destination we go through 1890 * ip_set_destination first. 1891 */ 1892 if (coa->coa_ancillary) { 1893 return (0); 1894 } 1895 1896 if (coa->coa_changed & COA_ROUTE_CHANGED) { 1897 in6_addr_t saddr, faddr, nexthop; 1898 in_port_t fport; 1899 1900 /* 1901 * We clear lastdst to make sure we pick up the change 1902 * next time sending. 1903 * If we are connected we re-cache the information. 1904 * We ignore errors to preserve BSD behavior. 1905 * Note that we don't redo IPsec policy lookup here 1906 * since the final destination (or source) didn't change. 1907 */ 1908 mutex_enter(&connp->conn_lock); 1909 connp->conn_v6lastdst = ipv6_all_zeros; 1910 1911 ip_attr_nexthop(coa->coa_ipp, coa->coa_ixa, 1912 &connp->conn_faddr_v6, &nexthop); 1913 saddr = connp->conn_saddr_v6; 1914 faddr = connp->conn_faddr_v6; 1915 fport = connp->conn_fport; 1916 mutex_exit(&connp->conn_lock); 1917 1918 if (!IN6_IS_ADDR_UNSPECIFIED(&faddr) && 1919 !IN6_IS_ADDR_V4MAPPED_ANY(&faddr)) { 1920 (void) ip_attr_connect(connp, coa->coa_ixa, 1921 &saddr, &faddr, &nexthop, fport, NULL, NULL, 1922 IPDF_ALLOW_MCBC | IPDF_VERIFY_DST); 1923 } 1924 } 1925 1926 ixa_refrele(coa->coa_ixa); 1927 1928 if (coa->coa_changed & COA_HEADER_CHANGED) { 1929 /* 1930 * Rebuild the header template if we are connected. 1931 * Otherwise clear conn_v6lastdst so we rebuild the header 1932 * in the data path. 1933 */ 1934 mutex_enter(&connp->conn_lock); 1935 if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6) && 1936 !IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_faddr_v6)) { 1937 err = udp_build_hdr_template(connp, 1938 &connp->conn_saddr_v6, &connp->conn_faddr_v6, 1939 connp->conn_fport, connp->conn_flowinfo); 1940 if (err != 0) { 1941 mutex_exit(&connp->conn_lock); 1942 return (err); 1943 } 1944 } else { 1945 connp->conn_v6lastdst = ipv6_all_zeros; 1946 } 1947 mutex_exit(&connp->conn_lock); 1948 } 1949 if (coa->coa_changed & COA_RCVBUF_CHANGED) { 1950 (void) proto_set_rx_hiwat(connp->conn_rq, connp, 1951 connp->conn_rcvbuf); 1952 } 1953 if ((coa->coa_changed & COA_SNDBUF_CHANGED) && !IPCL_IS_NONSTR(connp)) { 1954 connp->conn_wq->q_hiwat = connp->conn_sndbuf; 1955 } 1956 if (coa->coa_changed & COA_WROFF_CHANGED) { 1957 /* Increase wroff if needed */ 1958 uint_t wroff; 1959 1960 mutex_enter(&connp->conn_lock); 1961 wroff = connp->conn_ht_iphc_allocated + us->us_wroff_extra; 1962 if (udp->udp_nat_t_endpoint) 1963 wroff += sizeof (uint32_t); 1964 if (wroff > connp->conn_wroff) { 1965 connp->conn_wroff = wroff; 1966 mutex_exit(&connp->conn_lock); 1967 (void) proto_set_tx_wroff(connp->conn_rq, connp, wroff); 1968 } else { 1969 mutex_exit(&connp->conn_lock); 1970 } 1971 } 1972 return (err); 1973 } 1974 1975 /* This routine sets socket options. */ 1976 int 1977 udp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 1978 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 1979 void *thisdg_attrs, cred_t *cr) 1980 { 1981 conn_t *connp = Q_TO_CONN(q); 1982 int error; 1983 1984 error = udp_opt_set(connp, optset_context, level, name, inlen, invalp, 1985 outlenp, outvalp, thisdg_attrs, cr); 1986 return (error); 1987 } 1988 1989 /* 1990 * Setup IP and UDP headers. 1991 * Returns NULL on allocation failure, in which case data_mp is freed. 1992 */ 1993 mblk_t * 1994 udp_prepend_hdr(conn_t *connp, ip_xmit_attr_t *ixa, const ip_pkt_t *ipp, 1995 const in6_addr_t *v6src, const in6_addr_t *v6dst, in_port_t dstport, 1996 uint32_t flowinfo, mblk_t *data_mp, int *errorp) 1997 { 1998 mblk_t *mp; 1999 udpha_t *udpha; 2000 udp_stack_t *us = connp->conn_netstack->netstack_udp; 2001 uint_t data_len; 2002 uint32_t cksum; 2003 udp_t *udp = connp->conn_udp; 2004 boolean_t insert_spi = udp->udp_nat_t_endpoint; 2005 uint_t ulp_hdr_len; 2006 2007 data_len = msgdsize(data_mp); 2008 ulp_hdr_len = UDPH_SIZE; 2009 if (insert_spi) 2010 ulp_hdr_len += sizeof (uint32_t); 2011 2012 mp = conn_prepend_hdr(ixa, ipp, v6src, v6dst, IPPROTO_UDP, flowinfo, 2013 ulp_hdr_len, data_mp, data_len, us->us_wroff_extra, &cksum, errorp); 2014 if (mp == NULL) { 2015 ASSERT(*errorp != 0); 2016 return (NULL); 2017 } 2018 2019 data_len += ulp_hdr_len; 2020 ixa->ixa_pktlen = data_len + ixa->ixa_ip_hdr_length; 2021 2022 udpha = (udpha_t *)(mp->b_rptr + ixa->ixa_ip_hdr_length); 2023 udpha->uha_src_port = connp->conn_lport; 2024 udpha->uha_dst_port = dstport; 2025 udpha->uha_checksum = 0; 2026 udpha->uha_length = htons(data_len); 2027 2028 /* 2029 * If there was a routing option/header then conn_prepend_hdr 2030 * has massaged it and placed the pseudo-header checksum difference 2031 * in the cksum argument. 2032 * 2033 * Setup header length and prepare for ULP checksum done in IP. 2034 * 2035 * We make it easy for IP to include our pseudo header 2036 * by putting our length in uha_checksum. 2037 * The IP source, destination, and length have already been set by 2038 * conn_prepend_hdr. 2039 */ 2040 cksum += data_len; 2041 cksum = (cksum >> 16) + (cksum & 0xFFFF); 2042 ASSERT(cksum < 0x10000); 2043 2044 if (ixa->ixa_flags & IXAF_IS_IPV4) { 2045 ipha_t *ipha = (ipha_t *)mp->b_rptr; 2046 2047 ASSERT(ntohs(ipha->ipha_length) == ixa->ixa_pktlen); 2048 2049 /* IP does the checksum if uha_checksum is non-zero */ 2050 if (us->us_do_checksum) { 2051 if (cksum == 0) 2052 udpha->uha_checksum = 0xffff; 2053 else 2054 udpha->uha_checksum = htons(cksum); 2055 } else { 2056 udpha->uha_checksum = 0; 2057 } 2058 } else { 2059 ip6_t *ip6h = (ip6_t *)mp->b_rptr; 2060 2061 ASSERT(ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN == ixa->ixa_pktlen); 2062 if (cksum == 0) 2063 udpha->uha_checksum = 0xffff; 2064 else 2065 udpha->uha_checksum = htons(cksum); 2066 } 2067 2068 /* Insert all-0s SPI now. */ 2069 if (insert_spi) 2070 *((uint32_t *)(udpha + 1)) = 0; 2071 2072 return (mp); 2073 } 2074 2075 static int 2076 udp_build_hdr_template(conn_t *connp, const in6_addr_t *v6src, 2077 const in6_addr_t *v6dst, in_port_t dstport, uint32_t flowinfo) 2078 { 2079 udpha_t *udpha; 2080 int error; 2081 2082 ASSERT(MUTEX_HELD(&connp->conn_lock)); 2083 /* 2084 * We clear lastdst to make sure we don't use the lastdst path 2085 * next time sending since we might not have set v6dst yet. 2086 */ 2087 connp->conn_v6lastdst = ipv6_all_zeros; 2088 2089 error = conn_build_hdr_template(connp, UDPH_SIZE, 0, v6src, v6dst, 2090 flowinfo); 2091 if (error != 0) 2092 return (error); 2093 2094 /* 2095 * Any routing header/option has been massaged. The checksum difference 2096 * is stored in conn_sum. 2097 */ 2098 udpha = (udpha_t *)connp->conn_ht_ulp; 2099 udpha->uha_src_port = connp->conn_lport; 2100 udpha->uha_dst_port = dstport; 2101 udpha->uha_checksum = 0; 2102 udpha->uha_length = htons(UDPH_SIZE); /* Filled in later */ 2103 return (0); 2104 } 2105 2106 static mblk_t * 2107 udp_queue_fallback(udp_t *udp, mblk_t *mp) 2108 { 2109 ASSERT(MUTEX_HELD(&udp->udp_recv_lock)); 2110 if (IPCL_IS_NONSTR(udp->udp_connp)) { 2111 /* 2112 * fallback has started but messages have not been moved yet 2113 */ 2114 if (udp->udp_fallback_queue_head == NULL) { 2115 ASSERT(udp->udp_fallback_queue_tail == NULL); 2116 udp->udp_fallback_queue_head = mp; 2117 udp->udp_fallback_queue_tail = mp; 2118 } else { 2119 ASSERT(udp->udp_fallback_queue_tail != NULL); 2120 udp->udp_fallback_queue_tail->b_next = mp; 2121 udp->udp_fallback_queue_tail = mp; 2122 } 2123 return (NULL); 2124 } else { 2125 /* 2126 * Fallback completed, let the caller putnext() the mblk. 2127 */ 2128 return (mp); 2129 } 2130 } 2131 2132 /* 2133 * Deliver data to ULP. In case we have a socket, and it's falling back to 2134 * TPI, then we'll queue the mp for later processing. 2135 */ 2136 static void 2137 udp_ulp_recv(conn_t *connp, mblk_t *mp, uint_t len, ip_recv_attr_t *ira) 2138 { 2139 if (IPCL_IS_NONSTR(connp)) { 2140 udp_t *udp = connp->conn_udp; 2141 int error; 2142 2143 ASSERT(len == msgdsize(mp)); 2144 if ((*connp->conn_upcalls->su_recv) 2145 (connp->conn_upper_handle, mp, len, 0, &error, NULL) < 0) { 2146 mutex_enter(&udp->udp_recv_lock); 2147 if (error == ENOSPC) { 2148 /* 2149 * let's confirm while holding the lock 2150 */ 2151 if ((*connp->conn_upcalls->su_recv) 2152 (connp->conn_upper_handle, NULL, 0, 0, 2153 &error, NULL) < 0) { 2154 ASSERT(error == ENOSPC); 2155 if (error == ENOSPC) { 2156 connp->conn_flow_cntrld = 2157 B_TRUE; 2158 } 2159 } 2160 mutex_exit(&udp->udp_recv_lock); 2161 } else { 2162 ASSERT(error == EOPNOTSUPP); 2163 mp = udp_queue_fallback(udp, mp); 2164 mutex_exit(&udp->udp_recv_lock); 2165 if (mp != NULL) 2166 putnext(connp->conn_rq, mp); 2167 } 2168 } 2169 ASSERT(MUTEX_NOT_HELD(&udp->udp_recv_lock)); 2170 } else { 2171 if (is_system_labeled()) { 2172 ASSERT(ira->ira_cred != NULL); 2173 /* 2174 * Provide for protocols above UDP such as RPC 2175 * NOPID leaves db_cpid unchanged. 2176 */ 2177 mblk_setcred(mp, ira->ira_cred, NOPID); 2178 } 2179 2180 putnext(connp->conn_rq, mp); 2181 } 2182 } 2183 2184 /* 2185 * This is the inbound data path. 2186 * IP has already pulled up the IP plus UDP headers and verified alignment 2187 * etc. 2188 */ 2189 /* ARGSUSED2 */ 2190 static void 2191 udp_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira) 2192 { 2193 conn_t *connp = (conn_t *)arg1; 2194 struct T_unitdata_ind *tudi; 2195 uchar_t *rptr; /* Pointer to IP header */ 2196 int hdr_length; /* Length of IP+UDP headers */ 2197 int udi_size; /* Size of T_unitdata_ind */ 2198 int pkt_len; 2199 udp_t *udp; 2200 udpha_t *udpha; 2201 ip_pkt_t ipps; 2202 ip6_t *ip6h; 2203 mblk_t *mp1; 2204 uint32_t udp_ipv4_options_len; 2205 crb_t recv_ancillary; 2206 udp_stack_t *us; 2207 2208 ASSERT(connp->conn_flags & IPCL_UDPCONN); 2209 2210 udp = connp->conn_udp; 2211 us = udp->udp_us; 2212 rptr = mp->b_rptr; 2213 2214 ASSERT(DB_TYPE(mp) == M_DATA); 2215 ASSERT(OK_32PTR(rptr)); 2216 ASSERT(ira->ira_pktlen == msgdsize(mp)); 2217 pkt_len = ira->ira_pktlen; 2218 2219 /* 2220 * Get a snapshot of these and allow other threads to change 2221 * them after that. We need the same recv_ancillary when determining 2222 * the size as when adding the ancillary data items. 2223 */ 2224 mutex_enter(&connp->conn_lock); 2225 udp_ipv4_options_len = udp->udp_recv_ipp.ipp_ipv4_options_len; 2226 recv_ancillary = connp->conn_recv_ancillary; 2227 mutex_exit(&connp->conn_lock); 2228 2229 hdr_length = ira->ira_ip_hdr_length; 2230 2231 /* 2232 * IP inspected the UDP header thus all of it must be in the mblk. 2233 * UDP length check is performed for IPv6 packets and IPv4 packets 2234 * to check if the size of the packet as specified 2235 * by the UDP header is the same as the length derived from the IP 2236 * header. 2237 */ 2238 udpha = (udpha_t *)(rptr + hdr_length); 2239 if (pkt_len != ntohs(udpha->uha_length) + hdr_length) 2240 goto tossit; 2241 2242 hdr_length += UDPH_SIZE; 2243 ASSERT(MBLKL(mp) >= hdr_length); /* IP did a pullup */ 2244 2245 /* Initialize regardless of IP version */ 2246 ipps.ipp_fields = 0; 2247 2248 if (((ira->ira_flags & IRAF_IPV4_OPTIONS) || 2249 udp_ipv4_options_len > 0) && 2250 connp->conn_family == AF_INET) { 2251 int err; 2252 2253 /* 2254 * Record/update udp_recv_ipp with the lock 2255 * held. Not needed for AF_INET6 sockets 2256 * since they don't support a getsockopt of IP_OPTIONS. 2257 */ 2258 mutex_enter(&connp->conn_lock); 2259 err = ip_find_hdr_v4((ipha_t *)rptr, &udp->udp_recv_ipp, 2260 B_TRUE); 2261 if (err != 0) { 2262 /* Allocation failed. Drop packet */ 2263 mutex_exit(&connp->conn_lock); 2264 freemsg(mp); 2265 UDPS_BUMP_MIB(us, udpInErrors); 2266 return; 2267 } 2268 mutex_exit(&connp->conn_lock); 2269 } 2270 2271 if (recv_ancillary.crb_all != 0) { 2272 /* 2273 * Record packet information in the ip_pkt_t 2274 */ 2275 if (ira->ira_flags & IRAF_IS_IPV4) { 2276 ASSERT(IPH_HDR_VERSION(rptr) == IPV4_VERSION); 2277 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 2278 ASSERT(((ipha_t *)rptr)->ipha_protocol == IPPROTO_UDP); 2279 ASSERT(ira->ira_ip_hdr_length == IPH_HDR_LENGTH(rptr)); 2280 2281 (void) ip_find_hdr_v4((ipha_t *)rptr, &ipps, B_FALSE); 2282 } else { 2283 uint8_t nexthdrp; 2284 2285 ASSERT(IPH_HDR_VERSION(rptr) == IPV6_VERSION); 2286 /* 2287 * IPv6 packets can only be received by applications 2288 * that are prepared to receive IPv6 addresses. 2289 * The IP fanout must ensure this. 2290 */ 2291 ASSERT(connp->conn_family == AF_INET6); 2292 2293 ip6h = (ip6_t *)rptr; 2294 2295 /* We don't care about the length, but need the ipp */ 2296 hdr_length = ip_find_hdr_v6(mp, ip6h, B_TRUE, &ipps, 2297 &nexthdrp); 2298 ASSERT(hdr_length == ira->ira_ip_hdr_length); 2299 /* Restore */ 2300 hdr_length = ira->ira_ip_hdr_length + UDPH_SIZE; 2301 ASSERT(nexthdrp == IPPROTO_UDP); 2302 } 2303 } 2304 2305 /* 2306 * This is the inbound data path. Packets are passed upstream as 2307 * T_UNITDATA_IND messages. 2308 */ 2309 if (connp->conn_family == AF_INET) { 2310 sin_t *sin; 2311 2312 ASSERT(IPH_HDR_VERSION((ipha_t *)rptr) == IPV4_VERSION); 2313 2314 /* 2315 * Normally only send up the source address. 2316 * If any ancillary data items are wanted we add those. 2317 */ 2318 udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin_t); 2319 if (recv_ancillary.crb_all != 0) { 2320 udi_size += conn_recvancillary_size(connp, 2321 recv_ancillary, ira, mp, &ipps); 2322 } 2323 2324 /* Allocate a message block for the T_UNITDATA_IND structure. */ 2325 mp1 = allocb(udi_size, BPRI_MED); 2326 if (mp1 == NULL) { 2327 freemsg(mp); 2328 UDPS_BUMP_MIB(us, udpInErrors); 2329 return; 2330 } 2331 mp1->b_cont = mp; 2332 mp1->b_datap->db_type = M_PROTO; 2333 tudi = (struct T_unitdata_ind *)mp1->b_rptr; 2334 mp1->b_wptr = (uchar_t *)tudi + udi_size; 2335 tudi->PRIM_type = T_UNITDATA_IND; 2336 tudi->SRC_length = sizeof (sin_t); 2337 tudi->SRC_offset = sizeof (struct T_unitdata_ind); 2338 tudi->OPT_offset = sizeof (struct T_unitdata_ind) + 2339 sizeof (sin_t); 2340 udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t)); 2341 tudi->OPT_length = udi_size; 2342 sin = (sin_t *)&tudi[1]; 2343 sin->sin_addr.s_addr = ((ipha_t *)rptr)->ipha_src; 2344 sin->sin_port = udpha->uha_src_port; 2345 sin->sin_family = connp->conn_family; 2346 *(uint32_t *)&sin->sin_zero[0] = 0; 2347 *(uint32_t *)&sin->sin_zero[4] = 0; 2348 2349 /* 2350 * Add options if IP_RECVDSTADDR, IP_RECVIF, IP_RECVSLLA or 2351 * IP_RECVTTL has been set. 2352 */ 2353 if (udi_size != 0) { 2354 conn_recvancillary_add(connp, recv_ancillary, ira, 2355 &ipps, (uchar_t *)&sin[1], udi_size); 2356 } 2357 } else { 2358 sin6_t *sin6; 2359 2360 /* 2361 * Handle both IPv4 and IPv6 packets for IPv6 sockets. 2362 * 2363 * Normally we only send up the address. If receiving of any 2364 * optional receive side information is enabled, we also send 2365 * that up as options. 2366 */ 2367 udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t); 2368 2369 if (recv_ancillary.crb_all != 0) { 2370 udi_size += conn_recvancillary_size(connp, 2371 recv_ancillary, ira, mp, &ipps); 2372 } 2373 2374 mp1 = allocb(udi_size, BPRI_MED); 2375 if (mp1 == NULL) { 2376 freemsg(mp); 2377 UDPS_BUMP_MIB(us, udpInErrors); 2378 return; 2379 } 2380 mp1->b_cont = mp; 2381 mp1->b_datap->db_type = M_PROTO; 2382 tudi = (struct T_unitdata_ind *)mp1->b_rptr; 2383 mp1->b_wptr = (uchar_t *)tudi + udi_size; 2384 tudi->PRIM_type = T_UNITDATA_IND; 2385 tudi->SRC_length = sizeof (sin6_t); 2386 tudi->SRC_offset = sizeof (struct T_unitdata_ind); 2387 tudi->OPT_offset = sizeof (struct T_unitdata_ind) + 2388 sizeof (sin6_t); 2389 udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t)); 2390 tudi->OPT_length = udi_size; 2391 sin6 = (sin6_t *)&tudi[1]; 2392 if (ira->ira_flags & IRAF_IS_IPV4) { 2393 in6_addr_t v6dst; 2394 2395 IN6_IPADDR_TO_V4MAPPED(((ipha_t *)rptr)->ipha_src, 2396 &sin6->sin6_addr); 2397 IN6_IPADDR_TO_V4MAPPED(((ipha_t *)rptr)->ipha_dst, 2398 &v6dst); 2399 sin6->sin6_flowinfo = 0; 2400 sin6->sin6_scope_id = 0; 2401 sin6->__sin6_src_id = ip_srcid_find_addr(&v6dst, 2402 IPCL_ZONEID(connp), us->us_netstack); 2403 } else { 2404 ip6h = (ip6_t *)rptr; 2405 2406 sin6->sin6_addr = ip6h->ip6_src; 2407 /* No sin6_flowinfo per API */ 2408 sin6->sin6_flowinfo = 0; 2409 /* For link-scope pass up scope id */ 2410 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 2411 sin6->sin6_scope_id = ira->ira_ruifindex; 2412 else 2413 sin6->sin6_scope_id = 0; 2414 sin6->__sin6_src_id = ip_srcid_find_addr( 2415 &ip6h->ip6_dst, IPCL_ZONEID(connp), 2416 us->us_netstack); 2417 } 2418 sin6->sin6_port = udpha->uha_src_port; 2419 sin6->sin6_family = connp->conn_family; 2420 2421 if (udi_size != 0) { 2422 conn_recvancillary_add(connp, recv_ancillary, ira, 2423 &ipps, (uchar_t *)&sin6[1], udi_size); 2424 } 2425 } 2426 2427 /* 2428 * DTrace this UDP input as udp:::receive (this is for IPv4, IPv6 and 2429 * loopback traffic). 2430 */ 2431 DTRACE_UDP5(receive, mblk_t *, NULL, ip_xmit_attr_t *, connp->conn_ixa, 2432 void_ip_t *, rptr, udp_t *, udp, udpha_t *, udpha); 2433 2434 /* Walk past the headers unless IP_RECVHDR was set. */ 2435 if (!udp->udp_rcvhdr) { 2436 mp->b_rptr = rptr + hdr_length; 2437 pkt_len -= hdr_length; 2438 } 2439 2440 UDPS_BUMP_MIB(us, udpHCInDatagrams); 2441 udp_ulp_recv(connp, mp1, pkt_len, ira); 2442 return; 2443 2444 tossit: 2445 freemsg(mp); 2446 UDPS_BUMP_MIB(us, udpInErrors); 2447 } 2448 2449 /* 2450 * This routine creates a T_UDERROR_IND message and passes it upstream. 2451 * The address and options are copied from the T_UNITDATA_REQ message 2452 * passed in mp. This message is freed. 2453 */ 2454 static void 2455 udp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err) 2456 { 2457 struct T_unitdata_req *tudr; 2458 mblk_t *mp1; 2459 uchar_t *destaddr; 2460 t_scalar_t destlen; 2461 uchar_t *optaddr; 2462 t_scalar_t optlen; 2463 2464 if ((mp->b_wptr < mp->b_rptr) || 2465 (MBLKL(mp)) < sizeof (struct T_unitdata_req)) { 2466 goto done; 2467 } 2468 tudr = (struct T_unitdata_req *)mp->b_rptr; 2469 destaddr = mp->b_rptr + tudr->DEST_offset; 2470 if (destaddr < mp->b_rptr || destaddr >= mp->b_wptr || 2471 destaddr + tudr->DEST_length < mp->b_rptr || 2472 destaddr + tudr->DEST_length > mp->b_wptr) { 2473 goto done; 2474 } 2475 optaddr = mp->b_rptr + tudr->OPT_offset; 2476 if (optaddr < mp->b_rptr || optaddr >= mp->b_wptr || 2477 optaddr + tudr->OPT_length < mp->b_rptr || 2478 optaddr + tudr->OPT_length > mp->b_wptr) { 2479 goto done; 2480 } 2481 destlen = tudr->DEST_length; 2482 optlen = tudr->OPT_length; 2483 2484 mp1 = mi_tpi_uderror_ind((char *)destaddr, destlen, 2485 (char *)optaddr, optlen, err); 2486 if (mp1 != NULL) 2487 qreply(q, mp1); 2488 2489 done: 2490 freemsg(mp); 2491 } 2492 2493 /* 2494 * This routine removes a port number association from a stream. It 2495 * is called by udp_wput to handle T_UNBIND_REQ messages. 2496 */ 2497 static void 2498 udp_tpi_unbind(queue_t *q, mblk_t *mp) 2499 { 2500 conn_t *connp = Q_TO_CONN(q); 2501 int error; 2502 2503 error = udp_do_unbind(connp); 2504 if (error) { 2505 if (error < 0) 2506 udp_err_ack(q, mp, -error, 0); 2507 else 2508 udp_err_ack(q, mp, TSYSERR, error); 2509 return; 2510 } 2511 2512 mp = mi_tpi_ok_ack_alloc(mp); 2513 ASSERT(mp != NULL); 2514 ASSERT(((struct T_ok_ack *)mp->b_rptr)->PRIM_type == T_OK_ACK); 2515 qreply(q, mp); 2516 } 2517 2518 /* 2519 * Don't let port fall into the privileged range. 2520 * Since the extra privileged ports can be arbitrary we also 2521 * ensure that we exclude those from consideration. 2522 * us->us_epriv_ports is not sorted thus we loop over it until 2523 * there are no changes. 2524 */ 2525 static in_port_t 2526 udp_update_next_port(udp_t *udp, in_port_t port, boolean_t random) 2527 { 2528 int i, bump; 2529 in_port_t nextport; 2530 boolean_t restart = B_FALSE; 2531 udp_stack_t *us = udp->udp_us; 2532 2533 if (random && udp_random_anon_port != 0) { 2534 (void) random_get_pseudo_bytes((uint8_t *)&port, 2535 sizeof (in_port_t)); 2536 /* 2537 * Unless changed by a sys admin, the smallest anon port 2538 * is 32768 and the largest anon port is 65535. It is 2539 * very likely (50%) for the random port to be smaller 2540 * than the smallest anon port. When that happens, 2541 * add port % (anon port range) to the smallest anon 2542 * port to get the random port. It should fall into the 2543 * valid anon port range. 2544 */ 2545 if ((port < us->us_smallest_anon_port) || 2546 (port > us->us_largest_anon_port)) { 2547 if (us->us_smallest_anon_port == 2548 us->us_largest_anon_port) { 2549 bump = 0; 2550 } else { 2551 bump = port % (us->us_largest_anon_port - 2552 us->us_smallest_anon_port); 2553 } 2554 2555 port = us->us_smallest_anon_port + bump; 2556 } 2557 } 2558 2559 retry: 2560 if (port < us->us_smallest_anon_port) 2561 port = us->us_smallest_anon_port; 2562 2563 if (port > us->us_largest_anon_port) { 2564 port = us->us_smallest_anon_port; 2565 if (restart) 2566 return (0); 2567 restart = B_TRUE; 2568 } 2569 2570 if (port < us->us_smallest_nonpriv_port) 2571 port = us->us_smallest_nonpriv_port; 2572 2573 for (i = 0; i < us->us_num_epriv_ports; i++) { 2574 if (port == us->us_epriv_ports[i]) { 2575 port++; 2576 /* 2577 * Make sure that the port is in the 2578 * valid range. 2579 */ 2580 goto retry; 2581 } 2582 } 2583 2584 if (is_system_labeled() && 2585 (nextport = tsol_next_port(crgetzone(udp->udp_connp->conn_cred), 2586 port, IPPROTO_UDP, B_TRUE)) != 0) { 2587 port = nextport; 2588 goto retry; 2589 } 2590 2591 return (port); 2592 } 2593 2594 /* 2595 * Handle T_UNITDATA_REQ with options. Both IPv4 and IPv6 2596 * Either tudr_mp or msg is set. If tudr_mp we take ancillary data from 2597 * the TPI options, otherwise we take them from msg_control. 2598 * If both sin and sin6 is set it is a connected socket and we use conn_faddr. 2599 * Always consumes mp; never consumes tudr_mp. 2600 */ 2601 static int 2602 udp_output_ancillary(conn_t *connp, sin_t *sin, sin6_t *sin6, mblk_t *mp, 2603 mblk_t *tudr_mp, struct nmsghdr *msg, cred_t *cr, pid_t pid) 2604 { 2605 udp_t *udp = connp->conn_udp; 2606 udp_stack_t *us = udp->udp_us; 2607 int error; 2608 ip_xmit_attr_t *ixa; 2609 ip_pkt_t *ipp; 2610 in6_addr_t v6src; 2611 in6_addr_t v6dst; 2612 in6_addr_t v6nexthop; 2613 in_port_t dstport; 2614 uint32_t flowinfo; 2615 uint_t srcid; 2616 int is_absreq_failure = 0; 2617 conn_opt_arg_t coas, *coa; 2618 2619 ASSERT(tudr_mp != NULL || msg != NULL); 2620 2621 /* 2622 * Get ixa before checking state to handle a disconnect race. 2623 * 2624 * We need an exclusive copy of conn_ixa since the ancillary data 2625 * options might modify it. That copy has no pointers hence we 2626 * need to set them up once we've parsed the ancillary data. 2627 */ 2628 ixa = conn_get_ixa_exclusive(connp); 2629 if (ixa == NULL) { 2630 UDPS_BUMP_MIB(us, udpOutErrors); 2631 freemsg(mp); 2632 return (ENOMEM); 2633 } 2634 ASSERT(cr != NULL); 2635 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 2636 ixa->ixa_cred = cr; 2637 ixa->ixa_cpid = pid; 2638 if (is_system_labeled()) { 2639 /* We need to restart with a label based on the cred */ 2640 ip_xmit_attr_restore_tsl(ixa, ixa->ixa_cred); 2641 } 2642 2643 /* In case previous destination was multicast or multirt */ 2644 ip_attr_newdst(ixa); 2645 2646 /* Get a copy of conn_xmit_ipp since the options might change it */ 2647 ipp = kmem_zalloc(sizeof (*ipp), KM_NOSLEEP); 2648 if (ipp == NULL) { 2649 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 2650 ixa->ixa_cred = connp->conn_cred; /* Restore */ 2651 ixa->ixa_cpid = connp->conn_cpid; 2652 ixa_refrele(ixa); 2653 UDPS_BUMP_MIB(us, udpOutErrors); 2654 freemsg(mp); 2655 return (ENOMEM); 2656 } 2657 mutex_enter(&connp->conn_lock); 2658 error = ip_pkt_copy(&connp->conn_xmit_ipp, ipp, KM_NOSLEEP); 2659 mutex_exit(&connp->conn_lock); 2660 if (error != 0) { 2661 UDPS_BUMP_MIB(us, udpOutErrors); 2662 freemsg(mp); 2663 goto done; 2664 } 2665 2666 /* 2667 * Parse the options and update ixa and ipp as a result. 2668 * Note that ixa_tsl can be updated if SCM_UCRED. 2669 * ixa_refrele/ixa_inactivate will release any reference on ixa_tsl. 2670 */ 2671 2672 coa = &coas; 2673 coa->coa_connp = connp; 2674 coa->coa_ixa = ixa; 2675 coa->coa_ipp = ipp; 2676 coa->coa_ancillary = B_TRUE; 2677 coa->coa_changed = 0; 2678 2679 if (msg != NULL) { 2680 error = process_auxiliary_options(connp, msg->msg_control, 2681 msg->msg_controllen, coa, &udp_opt_obj, udp_opt_set, cr); 2682 } else { 2683 struct T_unitdata_req *tudr; 2684 2685 tudr = (struct T_unitdata_req *)tudr_mp->b_rptr; 2686 ASSERT(tudr->PRIM_type == T_UNITDATA_REQ); 2687 error = tpi_optcom_buf(connp->conn_wq, tudr_mp, 2688 &tudr->OPT_length, tudr->OPT_offset, cr, &udp_opt_obj, 2689 coa, &is_absreq_failure); 2690 } 2691 if (error != 0) { 2692 /* 2693 * Note: No special action needed in this 2694 * module for "is_absreq_failure" 2695 */ 2696 freemsg(mp); 2697 UDPS_BUMP_MIB(us, udpOutErrors); 2698 goto done; 2699 } 2700 ASSERT(is_absreq_failure == 0); 2701 2702 mutex_enter(&connp->conn_lock); 2703 /* 2704 * If laddr is unspecified then we look at sin6_src_id. 2705 * We will give precedence to a source address set with IPV6_PKTINFO 2706 * (aka IPPF_ADDR) but that is handled in build_hdrs. However, we don't 2707 * want ip_attr_connect to select a source (since it can fail) when 2708 * IPV6_PKTINFO is specified. 2709 * If this doesn't result in a source address then we get a source 2710 * from ip_attr_connect() below. 2711 */ 2712 v6src = connp->conn_saddr_v6; 2713 if (sin != NULL) { 2714 IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &v6dst); 2715 dstport = sin->sin_port; 2716 flowinfo = 0; 2717 ixa->ixa_flags &= ~IXAF_SCOPEID_SET; 2718 ixa->ixa_flags |= IXAF_IS_IPV4; 2719 } else if (sin6 != NULL) { 2720 boolean_t v4mapped; 2721 2722 v6dst = sin6->sin6_addr; 2723 dstport = sin6->sin6_port; 2724 flowinfo = sin6->sin6_flowinfo; 2725 srcid = sin6->__sin6_src_id; 2726 if (IN6_IS_ADDR_LINKSCOPE(&v6dst) && sin6->sin6_scope_id != 0) { 2727 ixa->ixa_scopeid = sin6->sin6_scope_id; 2728 ixa->ixa_flags |= IXAF_SCOPEID_SET; 2729 } else { 2730 ixa->ixa_flags &= ~IXAF_SCOPEID_SET; 2731 } 2732 v4mapped = IN6_IS_ADDR_V4MAPPED(&v6dst); 2733 if (v4mapped) 2734 ixa->ixa_flags |= IXAF_IS_IPV4; 2735 else 2736 ixa->ixa_flags &= ~IXAF_IS_IPV4; 2737 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 2738 if (!ip_srcid_find_id(srcid, &v6src, IPCL_ZONEID(connp), 2739 v4mapped, connp->conn_netstack)) { 2740 /* Mismatch - v4mapped/v6 specified by srcid. */ 2741 mutex_exit(&connp->conn_lock); 2742 error = EADDRNOTAVAIL; 2743 goto failed; /* Does freemsg() and mib. */ 2744 } 2745 } 2746 } else { 2747 /* Connected case */ 2748 v6dst = connp->conn_faddr_v6; 2749 dstport = connp->conn_fport; 2750 flowinfo = connp->conn_flowinfo; 2751 } 2752 mutex_exit(&connp->conn_lock); 2753 2754 /* Handle IP_PKTINFO/IPV6_PKTINFO setting source address. */ 2755 if (ipp->ipp_fields & IPPF_ADDR) { 2756 if (ixa->ixa_flags & IXAF_IS_IPV4) { 2757 if (IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr)) 2758 v6src = ipp->ipp_addr; 2759 } else { 2760 if (!IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr)) 2761 v6src = ipp->ipp_addr; 2762 } 2763 } 2764 2765 ip_attr_nexthop(ipp, ixa, &v6dst, &v6nexthop); 2766 error = ip_attr_connect(connp, ixa, &v6src, &v6dst, &v6nexthop, dstport, 2767 &v6src, NULL, IPDF_ALLOW_MCBC | IPDF_VERIFY_DST | IPDF_IPSEC); 2768 2769 switch (error) { 2770 case 0: 2771 break; 2772 case EADDRNOTAVAIL: 2773 /* 2774 * IXAF_VERIFY_SOURCE tells us to pick a better source. 2775 * Don't have the application see that errno 2776 */ 2777 error = ENETUNREACH; 2778 goto failed; 2779 case ENETDOWN: 2780 /* 2781 * Have !ipif_addr_ready address; drop packet silently 2782 * until we can get applications to not send until we 2783 * are ready. 2784 */ 2785 error = 0; 2786 goto failed; 2787 case EHOSTUNREACH: 2788 case ENETUNREACH: 2789 if (ixa->ixa_ire != NULL) { 2790 /* 2791 * Let conn_ip_output/ire_send_noroute return 2792 * the error and send any local ICMP error. 2793 */ 2794 error = 0; 2795 break; 2796 } 2797 /* FALLTHRU */ 2798 default: 2799 failed: 2800 freemsg(mp); 2801 UDPS_BUMP_MIB(us, udpOutErrors); 2802 goto done; 2803 } 2804 2805 /* 2806 * We might be going to a different destination than last time, 2807 * thus check that TX allows the communication and compute any 2808 * needed label. 2809 * 2810 * TSOL Note: We have an exclusive ipp and ixa for this thread so we 2811 * don't have to worry about concurrent threads. 2812 */ 2813 if (is_system_labeled()) { 2814 /* Using UDP MLP requires SCM_UCRED from user */ 2815 if (connp->conn_mlp_type != mlptSingle && 2816 !((ixa->ixa_flags & IXAF_UCRED_TSL))) { 2817 UDPS_BUMP_MIB(us, udpOutErrors); 2818 error = ECONNREFUSED; 2819 freemsg(mp); 2820 goto done; 2821 } 2822 /* 2823 * Check whether Trusted Solaris policy allows communication 2824 * with this host, and pretend that the destination is 2825 * unreachable if not. 2826 * Compute any needed label and place it in ipp_label_v4/v6. 2827 * 2828 * Later conn_build_hdr_template/conn_prepend_hdr takes 2829 * ipp_label_v4/v6 to form the packet. 2830 * 2831 * Tsol note: We have ipp structure local to this thread so 2832 * no locking is needed. 2833 */ 2834 error = conn_update_label(connp, ixa, &v6dst, ipp); 2835 if (error != 0) { 2836 freemsg(mp); 2837 UDPS_BUMP_MIB(us, udpOutErrors); 2838 goto done; 2839 } 2840 } 2841 mp = udp_prepend_hdr(connp, ixa, ipp, &v6src, &v6dst, dstport, 2842 flowinfo, mp, &error); 2843 if (mp == NULL) { 2844 ASSERT(error != 0); 2845 UDPS_BUMP_MIB(us, udpOutErrors); 2846 goto done; 2847 } 2848 if (ixa->ixa_pktlen > IP_MAXPACKET) { 2849 error = EMSGSIZE; 2850 UDPS_BUMP_MIB(us, udpOutErrors); 2851 freemsg(mp); 2852 goto done; 2853 } 2854 /* We're done. Pass the packet to ip. */ 2855 UDPS_BUMP_MIB(us, udpHCOutDatagrams); 2856 2857 DTRACE_UDP5(send, mblk_t *, NULL, ip_xmit_attr_t *, ixa, 2858 void_ip_t *, mp->b_rptr, udp_t *, udp, udpha_t *, 2859 &mp->b_rptr[ixa->ixa_ip_hdr_length]); 2860 2861 error = conn_ip_output(mp, ixa); 2862 /* No udpOutErrors if an error since IP increases its error counter */ 2863 switch (error) { 2864 case 0: 2865 break; 2866 case EWOULDBLOCK: 2867 (void) ixa_check_drain_insert(connp, ixa); 2868 error = 0; 2869 break; 2870 case EADDRNOTAVAIL: 2871 /* 2872 * IXAF_VERIFY_SOURCE tells us to pick a better source. 2873 * Don't have the application see that errno 2874 */ 2875 error = ENETUNREACH; 2876 /* FALLTHRU */ 2877 default: 2878 mutex_enter(&connp->conn_lock); 2879 /* 2880 * Clear the source and v6lastdst so we call ip_attr_connect 2881 * for the next packet and try to pick a better source. 2882 */ 2883 if (connp->conn_mcbc_bind) 2884 connp->conn_saddr_v6 = ipv6_all_zeros; 2885 else 2886 connp->conn_saddr_v6 = connp->conn_bound_addr_v6; 2887 connp->conn_v6lastdst = ipv6_all_zeros; 2888 mutex_exit(&connp->conn_lock); 2889 break; 2890 } 2891 done: 2892 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 2893 ixa->ixa_cred = connp->conn_cred; /* Restore */ 2894 ixa->ixa_cpid = connp->conn_cpid; 2895 ixa_refrele(ixa); 2896 ip_pkt_free(ipp); 2897 kmem_free(ipp, sizeof (*ipp)); 2898 return (error); 2899 } 2900 2901 /* 2902 * Handle sending an M_DATA for a connected socket. 2903 * Handles both IPv4 and IPv6. 2904 */ 2905 static int 2906 udp_output_connected(conn_t *connp, mblk_t *mp, cred_t *cr, pid_t pid) 2907 { 2908 udp_t *udp = connp->conn_udp; 2909 udp_stack_t *us = udp->udp_us; 2910 int error; 2911 ip_xmit_attr_t *ixa; 2912 2913 /* 2914 * If no other thread is using conn_ixa this just gets a reference to 2915 * conn_ixa. Otherwise we get a safe copy of conn_ixa. 2916 */ 2917 ixa = conn_get_ixa(connp, B_FALSE); 2918 if (ixa == NULL) { 2919 UDPS_BUMP_MIB(us, udpOutErrors); 2920 freemsg(mp); 2921 return (ENOMEM); 2922 } 2923 2924 ASSERT(cr != NULL); 2925 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 2926 ixa->ixa_cred = cr; 2927 ixa->ixa_cpid = pid; 2928 2929 mutex_enter(&connp->conn_lock); 2930 mp = udp_prepend_header_template(connp, ixa, mp, &connp->conn_saddr_v6, 2931 connp->conn_fport, connp->conn_flowinfo, &error); 2932 2933 if (mp == NULL) { 2934 ASSERT(error != 0); 2935 mutex_exit(&connp->conn_lock); 2936 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 2937 ixa->ixa_cred = connp->conn_cred; /* Restore */ 2938 ixa->ixa_cpid = connp->conn_cpid; 2939 ixa_refrele(ixa); 2940 UDPS_BUMP_MIB(us, udpOutErrors); 2941 freemsg(mp); 2942 return (error); 2943 } 2944 2945 /* 2946 * In case we got a safe copy of conn_ixa, or if opt_set made us a new 2947 * safe copy, then we need to fill in any pointers in it. 2948 */ 2949 if (ixa->ixa_ire == NULL) { 2950 in6_addr_t faddr, saddr; 2951 in6_addr_t nexthop; 2952 in_port_t fport; 2953 2954 saddr = connp->conn_saddr_v6; 2955 faddr = connp->conn_faddr_v6; 2956 fport = connp->conn_fport; 2957 ip_attr_nexthop(&connp->conn_xmit_ipp, ixa, &faddr, &nexthop); 2958 mutex_exit(&connp->conn_lock); 2959 2960 error = ip_attr_connect(connp, ixa, &saddr, &faddr, &nexthop, 2961 fport, NULL, NULL, IPDF_ALLOW_MCBC | IPDF_VERIFY_DST | 2962 IPDF_IPSEC); 2963 switch (error) { 2964 case 0: 2965 break; 2966 case EADDRNOTAVAIL: 2967 /* 2968 * IXAF_VERIFY_SOURCE tells us to pick a better source. 2969 * Don't have the application see that errno 2970 */ 2971 error = ENETUNREACH; 2972 goto failed; 2973 case ENETDOWN: 2974 /* 2975 * Have !ipif_addr_ready address; drop packet silently 2976 * until we can get applications to not send until we 2977 * are ready. 2978 */ 2979 error = 0; 2980 goto failed; 2981 case EHOSTUNREACH: 2982 case ENETUNREACH: 2983 if (ixa->ixa_ire != NULL) { 2984 /* 2985 * Let conn_ip_output/ire_send_noroute return 2986 * the error and send any local ICMP error. 2987 */ 2988 error = 0; 2989 break; 2990 } 2991 /* FALLTHRU */ 2992 default: 2993 failed: 2994 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 2995 ixa->ixa_cred = connp->conn_cred; /* Restore */ 2996 ixa->ixa_cpid = connp->conn_cpid; 2997 ixa_refrele(ixa); 2998 freemsg(mp); 2999 UDPS_BUMP_MIB(us, udpOutErrors); 3000 return (error); 3001 } 3002 } else { 3003 /* Done with conn_t */ 3004 mutex_exit(&connp->conn_lock); 3005 } 3006 ASSERT(ixa->ixa_ire != NULL); 3007 3008 /* We're done. Pass the packet to ip. */ 3009 UDPS_BUMP_MIB(us, udpHCOutDatagrams); 3010 3011 DTRACE_UDP5(send, mblk_t *, NULL, ip_xmit_attr_t *, ixa, 3012 void_ip_t *, mp->b_rptr, udp_t *, udp, udpha_t *, 3013 &mp->b_rptr[ixa->ixa_ip_hdr_length]); 3014 3015 error = conn_ip_output(mp, ixa); 3016 /* No udpOutErrors if an error since IP increases its error counter */ 3017 switch (error) { 3018 case 0: 3019 break; 3020 case EWOULDBLOCK: 3021 (void) ixa_check_drain_insert(connp, ixa); 3022 error = 0; 3023 break; 3024 case EADDRNOTAVAIL: 3025 /* 3026 * IXAF_VERIFY_SOURCE tells us to pick a better source. 3027 * Don't have the application see that errno 3028 */ 3029 error = ENETUNREACH; 3030 break; 3031 } 3032 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 3033 ixa->ixa_cred = connp->conn_cred; /* Restore */ 3034 ixa->ixa_cpid = connp->conn_cpid; 3035 ixa_refrele(ixa); 3036 return (error); 3037 } 3038 3039 /* 3040 * Handle sending an M_DATA to the last destination. 3041 * Handles both IPv4 and IPv6. 3042 * 3043 * NOTE: The caller must hold conn_lock and we drop it here. 3044 */ 3045 static int 3046 udp_output_lastdst(conn_t *connp, mblk_t *mp, cred_t *cr, pid_t pid, 3047 ip_xmit_attr_t *ixa) 3048 { 3049 udp_t *udp = connp->conn_udp; 3050 udp_stack_t *us = udp->udp_us; 3051 int error; 3052 3053 ASSERT(MUTEX_HELD(&connp->conn_lock)); 3054 ASSERT(ixa != NULL); 3055 3056 ASSERT(cr != NULL); 3057 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 3058 ixa->ixa_cred = cr; 3059 ixa->ixa_cpid = pid; 3060 3061 mp = udp_prepend_header_template(connp, ixa, mp, &connp->conn_v6lastsrc, 3062 connp->conn_lastdstport, connp->conn_lastflowinfo, &error); 3063 3064 if (mp == NULL) { 3065 ASSERT(error != 0); 3066 mutex_exit(&connp->conn_lock); 3067 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 3068 ixa->ixa_cred = connp->conn_cred; /* Restore */ 3069 ixa->ixa_cpid = connp->conn_cpid; 3070 ixa_refrele(ixa); 3071 UDPS_BUMP_MIB(us, udpOutErrors); 3072 freemsg(mp); 3073 return (error); 3074 } 3075 3076 /* 3077 * In case we got a safe copy of conn_ixa, or if opt_set made us a new 3078 * safe copy, then we need to fill in any pointers in it. 3079 */ 3080 if (ixa->ixa_ire == NULL) { 3081 in6_addr_t lastdst, lastsrc; 3082 in6_addr_t nexthop; 3083 in_port_t lastport; 3084 3085 lastsrc = connp->conn_v6lastsrc; 3086 lastdst = connp->conn_v6lastdst; 3087 lastport = connp->conn_lastdstport; 3088 ip_attr_nexthop(&connp->conn_xmit_ipp, ixa, &lastdst, &nexthop); 3089 mutex_exit(&connp->conn_lock); 3090 3091 error = ip_attr_connect(connp, ixa, &lastsrc, &lastdst, 3092 &nexthop, lastport, NULL, NULL, IPDF_ALLOW_MCBC | 3093 IPDF_VERIFY_DST | IPDF_IPSEC); 3094 switch (error) { 3095 case 0: 3096 break; 3097 case EADDRNOTAVAIL: 3098 /* 3099 * IXAF_VERIFY_SOURCE tells us to pick a better source. 3100 * Don't have the application see that errno 3101 */ 3102 error = ENETUNREACH; 3103 goto failed; 3104 case ENETDOWN: 3105 /* 3106 * Have !ipif_addr_ready address; drop packet silently 3107 * until we can get applications to not send until we 3108 * are ready. 3109 */ 3110 error = 0; 3111 goto failed; 3112 case EHOSTUNREACH: 3113 case ENETUNREACH: 3114 if (ixa->ixa_ire != NULL) { 3115 /* 3116 * Let conn_ip_output/ire_send_noroute return 3117 * the error and send any local ICMP error. 3118 */ 3119 error = 0; 3120 break; 3121 } 3122 /* FALLTHRU */ 3123 default: 3124 failed: 3125 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 3126 ixa->ixa_cred = connp->conn_cred; /* Restore */ 3127 ixa->ixa_cpid = connp->conn_cpid; 3128 ixa_refrele(ixa); 3129 freemsg(mp); 3130 UDPS_BUMP_MIB(us, udpOutErrors); 3131 return (error); 3132 } 3133 } else { 3134 /* Done with conn_t */ 3135 mutex_exit(&connp->conn_lock); 3136 } 3137 3138 /* We're done. Pass the packet to ip. */ 3139 UDPS_BUMP_MIB(us, udpHCOutDatagrams); 3140 3141 DTRACE_UDP5(send, mblk_t *, NULL, ip_xmit_attr_t *, ixa, 3142 void_ip_t *, mp->b_rptr, udp_t *, udp, udpha_t *, 3143 &mp->b_rptr[ixa->ixa_ip_hdr_length]); 3144 3145 error = conn_ip_output(mp, ixa); 3146 /* No udpOutErrors if an error since IP increases its error counter */ 3147 switch (error) { 3148 case 0: 3149 break; 3150 case EWOULDBLOCK: 3151 (void) ixa_check_drain_insert(connp, ixa); 3152 error = 0; 3153 break; 3154 case EADDRNOTAVAIL: 3155 /* 3156 * IXAF_VERIFY_SOURCE tells us to pick a better source. 3157 * Don't have the application see that errno 3158 */ 3159 error = ENETUNREACH; 3160 /* FALLTHRU */ 3161 default: 3162 mutex_enter(&connp->conn_lock); 3163 /* 3164 * Clear the source and v6lastdst so we call ip_attr_connect 3165 * for the next packet and try to pick a better source. 3166 */ 3167 if (connp->conn_mcbc_bind) 3168 connp->conn_saddr_v6 = ipv6_all_zeros; 3169 else 3170 connp->conn_saddr_v6 = connp->conn_bound_addr_v6; 3171 connp->conn_v6lastdst = ipv6_all_zeros; 3172 mutex_exit(&connp->conn_lock); 3173 break; 3174 } 3175 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 3176 ixa->ixa_cred = connp->conn_cred; /* Restore */ 3177 ixa->ixa_cpid = connp->conn_cpid; 3178 ixa_refrele(ixa); 3179 return (error); 3180 } 3181 3182 3183 /* 3184 * Prepend the header template and then fill in the source and 3185 * flowinfo. The caller needs to handle the destination address since 3186 * it's setting is different if rthdr or source route. 3187 * 3188 * Returns NULL is allocation failed or if the packet would exceed IP_MAXPACKET. 3189 * When it returns NULL it sets errorp. 3190 */ 3191 static mblk_t * 3192 udp_prepend_header_template(conn_t *connp, ip_xmit_attr_t *ixa, mblk_t *mp, 3193 const in6_addr_t *v6src, in_port_t dstport, uint32_t flowinfo, int *errorp) 3194 { 3195 udp_t *udp = connp->conn_udp; 3196 udp_stack_t *us = udp->udp_us; 3197 boolean_t insert_spi = udp->udp_nat_t_endpoint; 3198 uint_t pktlen; 3199 uint_t alloclen; 3200 uint_t copylen; 3201 uint8_t *iph; 3202 uint_t ip_hdr_length; 3203 udpha_t *udpha; 3204 uint32_t cksum; 3205 ip_pkt_t *ipp; 3206 3207 ASSERT(MUTEX_HELD(&connp->conn_lock)); 3208 3209 /* 3210 * Copy the header template and leave space for an SPI 3211 */ 3212 copylen = connp->conn_ht_iphc_len; 3213 alloclen = copylen + (insert_spi ? sizeof (uint32_t) : 0); 3214 pktlen = alloclen + msgdsize(mp); 3215 if (pktlen > IP_MAXPACKET) { 3216 freemsg(mp); 3217 *errorp = EMSGSIZE; 3218 return (NULL); 3219 } 3220 ixa->ixa_pktlen = pktlen; 3221 3222 /* check/fix buffer config, setup pointers into it */ 3223 iph = mp->b_rptr - alloclen; 3224 if (DB_REF(mp) != 1 || iph < DB_BASE(mp) || !OK_32PTR(iph)) { 3225 mblk_t *mp1; 3226 3227 mp1 = allocb(alloclen + us->us_wroff_extra, BPRI_MED); 3228 if (mp1 == NULL) { 3229 freemsg(mp); 3230 *errorp = ENOMEM; 3231 return (NULL); 3232 } 3233 mp1->b_wptr = DB_LIM(mp1); 3234 mp1->b_cont = mp; 3235 mp = mp1; 3236 iph = (mp->b_wptr - alloclen); 3237 } 3238 mp->b_rptr = iph; 3239 bcopy(connp->conn_ht_iphc, iph, copylen); 3240 ip_hdr_length = (uint_t)(connp->conn_ht_ulp - connp->conn_ht_iphc); 3241 3242 ixa->ixa_ip_hdr_length = ip_hdr_length; 3243 udpha = (udpha_t *)(iph + ip_hdr_length); 3244 3245 /* 3246 * Setup header length and prepare for ULP checksum done in IP. 3247 * udp_build_hdr_template has already massaged any routing header 3248 * and placed the result in conn_sum. 3249 * 3250 * We make it easy for IP to include our pseudo header 3251 * by putting our length in uha_checksum. 3252 */ 3253 cksum = pktlen - ip_hdr_length; 3254 udpha->uha_length = htons(cksum); 3255 3256 cksum += connp->conn_sum; 3257 cksum = (cksum >> 16) + (cksum & 0xFFFF); 3258 ASSERT(cksum < 0x10000); 3259 3260 ipp = &connp->conn_xmit_ipp; 3261 if (ixa->ixa_flags & IXAF_IS_IPV4) { 3262 ipha_t *ipha = (ipha_t *)iph; 3263 3264 ipha->ipha_length = htons((uint16_t)pktlen); 3265 3266 /* IP does the checksum if uha_checksum is non-zero */ 3267 if (us->us_do_checksum) 3268 udpha->uha_checksum = htons(cksum); 3269 3270 /* if IP_PKTINFO specified an addres it wins over bind() */ 3271 if ((ipp->ipp_fields & IPPF_ADDR) && 3272 IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr)) { 3273 ASSERT(ipp->ipp_addr_v4 != INADDR_ANY); 3274 ipha->ipha_src = ipp->ipp_addr_v4; 3275 } else { 3276 IN6_V4MAPPED_TO_IPADDR(v6src, ipha->ipha_src); 3277 } 3278 } else { 3279 ip6_t *ip6h = (ip6_t *)iph; 3280 3281 ip6h->ip6_plen = htons((uint16_t)(pktlen - IPV6_HDR_LEN)); 3282 udpha->uha_checksum = htons(cksum); 3283 3284 /* if IP_PKTINFO specified an addres it wins over bind() */ 3285 if ((ipp->ipp_fields & IPPF_ADDR) && 3286 !IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr)) { 3287 ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)); 3288 ip6h->ip6_src = ipp->ipp_addr; 3289 } else { 3290 ip6h->ip6_src = *v6src; 3291 } 3292 ip6h->ip6_vcf = 3293 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 3294 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 3295 if (ipp->ipp_fields & IPPF_TCLASS) { 3296 /* Overrides the class part of flowinfo */ 3297 ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf, 3298 ipp->ipp_tclass); 3299 } 3300 } 3301 3302 /* Insert all-0s SPI now. */ 3303 if (insert_spi) 3304 *((uint32_t *)(udpha + 1)) = 0; 3305 3306 udpha->uha_dst_port = dstport; 3307 return (mp); 3308 } 3309 3310 /* 3311 * Send a T_UDERR_IND in response to an M_DATA 3312 */ 3313 static void 3314 udp_ud_err_connected(conn_t *connp, t_scalar_t error) 3315 { 3316 struct sockaddr_storage ss; 3317 sin_t *sin; 3318 sin6_t *sin6; 3319 struct sockaddr *addr; 3320 socklen_t addrlen; 3321 mblk_t *mp1; 3322 3323 mutex_enter(&connp->conn_lock); 3324 /* Initialize addr and addrlen as if they're passed in */ 3325 if (connp->conn_family == AF_INET) { 3326 sin = (sin_t *)&ss; 3327 *sin = sin_null; 3328 sin->sin_family = AF_INET; 3329 sin->sin_port = connp->conn_fport; 3330 sin->sin_addr.s_addr = connp->conn_faddr_v4; 3331 addr = (struct sockaddr *)sin; 3332 addrlen = sizeof (*sin); 3333 } else { 3334 sin6 = (sin6_t *)&ss; 3335 *sin6 = sin6_null; 3336 sin6->sin6_family = AF_INET6; 3337 sin6->sin6_port = connp->conn_fport; 3338 sin6->sin6_flowinfo = connp->conn_flowinfo; 3339 sin6->sin6_addr = connp->conn_faddr_v6; 3340 if (IN6_IS_ADDR_LINKSCOPE(&connp->conn_faddr_v6) && 3341 (connp->conn_ixa->ixa_flags & IXAF_SCOPEID_SET)) { 3342 sin6->sin6_scope_id = connp->conn_ixa->ixa_scopeid; 3343 } else { 3344 sin6->sin6_scope_id = 0; 3345 } 3346 sin6->__sin6_src_id = 0; 3347 addr = (struct sockaddr *)sin6; 3348 addrlen = sizeof (*sin6); 3349 } 3350 mutex_exit(&connp->conn_lock); 3351 3352 mp1 = mi_tpi_uderror_ind((char *)addr, addrlen, NULL, 0, error); 3353 if (mp1 != NULL) 3354 putnext(connp->conn_rq, mp1); 3355 } 3356 3357 /* 3358 * This routine handles all messages passed downstream. It either 3359 * consumes the message or passes it downstream; it never queues a 3360 * a message. 3361 * 3362 * Also entry point for sockfs when udp is in "direct sockfs" mode. This mode 3363 * is valid when we are directly beneath the stream head, and thus sockfs 3364 * is able to bypass STREAMS and directly call us, passing along the sockaddr 3365 * structure without the cumbersome T_UNITDATA_REQ interface for the case of 3366 * connected endpoints. 3367 */ 3368 int 3369 udp_wput(queue_t *q, mblk_t *mp) 3370 { 3371 sin6_t *sin6; 3372 sin_t *sin = NULL; 3373 uint_t srcid; 3374 conn_t *connp = Q_TO_CONN(q); 3375 udp_t *udp = connp->conn_udp; 3376 int error = 0; 3377 struct sockaddr *addr = NULL; 3378 socklen_t addrlen; 3379 udp_stack_t *us = udp->udp_us; 3380 struct T_unitdata_req *tudr; 3381 mblk_t *data_mp; 3382 ushort_t ipversion; 3383 cred_t *cr; 3384 pid_t pid; 3385 3386 /* 3387 * We directly handle several cases here: T_UNITDATA_REQ message 3388 * coming down as M_PROTO/M_PCPROTO and M_DATA messages for connected 3389 * socket. 3390 */ 3391 switch (DB_TYPE(mp)) { 3392 case M_DATA: 3393 if (!udp->udp_issocket || udp->udp_state != TS_DATA_XFER) { 3394 /* Not connected; address is required */ 3395 UDPS_BUMP_MIB(us, udpOutErrors); 3396 UDP_DBGSTAT(us, udp_data_notconn); 3397 UDP_STAT(us, udp_out_err_notconn); 3398 freemsg(mp); 3399 return (0); 3400 } 3401 /* 3402 * All Solaris components should pass a db_credp 3403 * for this message, hence we ASSERT. 3404 * On production kernels we return an error to be robust against 3405 * random streams modules sitting on top of us. 3406 */ 3407 cr = msg_getcred(mp, &pid); 3408 ASSERT(cr != NULL); 3409 if (cr == NULL) { 3410 UDPS_BUMP_MIB(us, udpOutErrors); 3411 freemsg(mp); 3412 return (0); 3413 } 3414 ASSERT(udp->udp_issocket); 3415 UDP_DBGSTAT(us, udp_data_conn); 3416 error = udp_output_connected(connp, mp, cr, pid); 3417 if (error != 0) { 3418 UDP_STAT(us, udp_out_err_output); 3419 if (connp->conn_rq != NULL) 3420 udp_ud_err_connected(connp, (t_scalar_t)error); 3421 #ifdef DEBUG 3422 printf("udp_output_connected returned %d\n", error); 3423 #endif 3424 } 3425 return (0); 3426 3427 case M_PROTO: 3428 case M_PCPROTO: 3429 tudr = (struct T_unitdata_req *)mp->b_rptr; 3430 if (MBLKL(mp) < sizeof (*tudr) || 3431 ((t_primp_t)mp->b_rptr)->type != T_UNITDATA_REQ) { 3432 udp_wput_other(q, mp); 3433 return (0); 3434 } 3435 break; 3436 3437 default: 3438 udp_wput_other(q, mp); 3439 return (0); 3440 } 3441 3442 /* Handle valid T_UNITDATA_REQ here */ 3443 data_mp = mp->b_cont; 3444 if (data_mp == NULL) { 3445 error = EPROTO; 3446 goto ud_error2; 3447 } 3448 mp->b_cont = NULL; 3449 3450 if (!MBLKIN(mp, 0, tudr->DEST_offset + tudr->DEST_length)) { 3451 error = EADDRNOTAVAIL; 3452 goto ud_error2; 3453 } 3454 3455 /* 3456 * All Solaris components should pass a db_credp 3457 * for this TPI message, hence we should ASSERT. 3458 * However, RPC (svc_clts_ksend) does this odd thing where it 3459 * passes the options from a T_UNITDATA_IND unchanged in a 3460 * T_UNITDATA_REQ. While that is the right thing to do for 3461 * some options, SCM_UCRED being the key one, this also makes it 3462 * pass down IP_RECVDSTADDR. Hence we can't ASSERT here. 3463 */ 3464 cr = msg_getcred(mp, &pid); 3465 if (cr == NULL) { 3466 cr = connp->conn_cred; 3467 pid = connp->conn_cpid; 3468 } 3469 3470 /* 3471 * If a port has not been bound to the stream, fail. 3472 * This is not a problem when sockfs is directly 3473 * above us, because it will ensure that the socket 3474 * is first bound before allowing data to be sent. 3475 */ 3476 if (udp->udp_state == TS_UNBND) { 3477 error = EPROTO; 3478 goto ud_error2; 3479 } 3480 addr = (struct sockaddr *)&mp->b_rptr[tudr->DEST_offset]; 3481 addrlen = tudr->DEST_length; 3482 3483 switch (connp->conn_family) { 3484 case AF_INET6: 3485 sin6 = (sin6_t *)addr; 3486 if (!OK_32PTR((char *)sin6) || (addrlen != sizeof (sin6_t)) || 3487 (sin6->sin6_family != AF_INET6)) { 3488 error = EADDRNOTAVAIL; 3489 goto ud_error2; 3490 } 3491 3492 srcid = sin6->__sin6_src_id; 3493 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 3494 /* 3495 * Destination is a non-IPv4-compatible IPv6 address. 3496 * Send out an IPv6 format packet. 3497 */ 3498 3499 /* 3500 * If the local address is a mapped address return 3501 * an error. 3502 * It would be possible to send an IPv6 packet but the 3503 * response would never make it back to the application 3504 * since it is bound to a mapped address. 3505 */ 3506 if (IN6_IS_ADDR_V4MAPPED(&connp->conn_saddr_v6)) { 3507 error = EADDRNOTAVAIL; 3508 goto ud_error2; 3509 } 3510 3511 UDP_DBGSTAT(us, udp_out_ipv6); 3512 3513 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 3514 sin6->sin6_addr = ipv6_loopback; 3515 ipversion = IPV6_VERSION; 3516 } else { 3517 if (connp->conn_ipv6_v6only) { 3518 error = EADDRNOTAVAIL; 3519 goto ud_error2; 3520 } 3521 3522 /* 3523 * If the local address is not zero or a mapped address 3524 * return an error. It would be possible to send an 3525 * IPv4 packet but the response would never make it 3526 * back to the application since it is bound to a 3527 * non-mapped address. 3528 */ 3529 if (!IN6_IS_ADDR_V4MAPPED(&connp->conn_saddr_v6) && 3530 !IN6_IS_ADDR_UNSPECIFIED(&connp->conn_saddr_v6)) { 3531 error = EADDRNOTAVAIL; 3532 goto ud_error2; 3533 } 3534 UDP_DBGSTAT(us, udp_out_mapped); 3535 3536 if (V4_PART_OF_V6(sin6->sin6_addr) == INADDR_ANY) { 3537 V4_PART_OF_V6(sin6->sin6_addr) = 3538 htonl(INADDR_LOOPBACK); 3539 } 3540 ipversion = IPV4_VERSION; 3541 } 3542 3543 if (tudr->OPT_length != 0) { 3544 /* 3545 * If we are connected then the destination needs to be 3546 * the same as the connected one. 3547 */ 3548 if (udp->udp_state == TS_DATA_XFER && 3549 !conn_same_as_last_v6(connp, sin6)) { 3550 error = EISCONN; 3551 goto ud_error2; 3552 } 3553 UDP_STAT(us, udp_out_opt); 3554 error = udp_output_ancillary(connp, NULL, sin6, 3555 data_mp, mp, NULL, cr, pid); 3556 } else { 3557 ip_xmit_attr_t *ixa; 3558 3559 /* 3560 * We have to allocate an ip_xmit_attr_t before we grab 3561 * conn_lock and we need to hold conn_lock once we've 3562 * checked conn_same_as_last_v6 to handle concurrent 3563 * send* calls on a socket. 3564 */ 3565 ixa = conn_get_ixa(connp, B_FALSE); 3566 if (ixa == NULL) { 3567 error = ENOMEM; 3568 goto ud_error2; 3569 } 3570 mutex_enter(&connp->conn_lock); 3571 3572 if (conn_same_as_last_v6(connp, sin6) && 3573 connp->conn_lastsrcid == srcid && 3574 ipsec_outbound_policy_current(ixa)) { 3575 UDP_DBGSTAT(us, udp_out_lastdst); 3576 /* udp_output_lastdst drops conn_lock */ 3577 error = udp_output_lastdst(connp, data_mp, cr, 3578 pid, ixa); 3579 } else { 3580 UDP_DBGSTAT(us, udp_out_diffdst); 3581 /* udp_output_newdst drops conn_lock */ 3582 error = udp_output_newdst(connp, data_mp, NULL, 3583 sin6, ipversion, cr, pid, ixa); 3584 } 3585 ASSERT(MUTEX_NOT_HELD(&connp->conn_lock)); 3586 } 3587 if (error == 0) { 3588 freeb(mp); 3589 return (0); 3590 } 3591 break; 3592 3593 case AF_INET: 3594 sin = (sin_t *)addr; 3595 if ((!OK_32PTR((char *)sin) || addrlen != sizeof (sin_t)) || 3596 (sin->sin_family != AF_INET)) { 3597 error = EADDRNOTAVAIL; 3598 goto ud_error2; 3599 } 3600 UDP_DBGSTAT(us, udp_out_ipv4); 3601 if (sin->sin_addr.s_addr == INADDR_ANY) 3602 sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 3603 ipversion = IPV4_VERSION; 3604 3605 srcid = 0; 3606 if (tudr->OPT_length != 0) { 3607 /* 3608 * If we are connected then the destination needs to be 3609 * the same as the connected one. 3610 */ 3611 if (udp->udp_state == TS_DATA_XFER && 3612 !conn_same_as_last_v4(connp, sin)) { 3613 error = EISCONN; 3614 goto ud_error2; 3615 } 3616 UDP_STAT(us, udp_out_opt); 3617 error = udp_output_ancillary(connp, sin, NULL, 3618 data_mp, mp, NULL, cr, pid); 3619 } else { 3620 ip_xmit_attr_t *ixa; 3621 3622 /* 3623 * We have to allocate an ip_xmit_attr_t before we grab 3624 * conn_lock and we need to hold conn_lock once we've 3625 * checked conn_same_as_last_v4 to handle concurrent 3626 * send* calls on a socket. 3627 */ 3628 ixa = conn_get_ixa(connp, B_FALSE); 3629 if (ixa == NULL) { 3630 error = ENOMEM; 3631 goto ud_error2; 3632 } 3633 mutex_enter(&connp->conn_lock); 3634 3635 if (conn_same_as_last_v4(connp, sin) && 3636 ipsec_outbound_policy_current(ixa)) { 3637 UDP_DBGSTAT(us, udp_out_lastdst); 3638 /* udp_output_lastdst drops conn_lock */ 3639 error = udp_output_lastdst(connp, data_mp, cr, 3640 pid, ixa); 3641 } else { 3642 UDP_DBGSTAT(us, udp_out_diffdst); 3643 /* udp_output_newdst drops conn_lock */ 3644 error = udp_output_newdst(connp, data_mp, sin, 3645 NULL, ipversion, cr, pid, ixa); 3646 } 3647 ASSERT(MUTEX_NOT_HELD(&connp->conn_lock)); 3648 } 3649 if (error == 0) { 3650 freeb(mp); 3651 return (0); 3652 } 3653 break; 3654 } 3655 UDP_STAT(us, udp_out_err_output); 3656 ASSERT(mp != NULL); 3657 /* mp is freed by the following routine */ 3658 udp_ud_err(q, mp, (t_scalar_t)error); 3659 return (0); 3660 3661 ud_error2: 3662 UDPS_BUMP_MIB(us, udpOutErrors); 3663 freemsg(data_mp); 3664 UDP_STAT(us, udp_out_err_output); 3665 ASSERT(mp != NULL); 3666 /* mp is freed by the following routine */ 3667 udp_ud_err(q, mp, (t_scalar_t)error); 3668 return (0); 3669 } 3670 3671 /* 3672 * Handle the case of the IP address, port, flow label being different 3673 * for both IPv4 and IPv6. 3674 * 3675 * NOTE: The caller must hold conn_lock and we drop it here. 3676 */ 3677 static int 3678 udp_output_newdst(conn_t *connp, mblk_t *data_mp, sin_t *sin, sin6_t *sin6, 3679 ushort_t ipversion, cred_t *cr, pid_t pid, ip_xmit_attr_t *ixa) 3680 { 3681 uint_t srcid; 3682 uint32_t flowinfo; 3683 udp_t *udp = connp->conn_udp; 3684 int error = 0; 3685 ip_xmit_attr_t *oldixa; 3686 udp_stack_t *us = udp->udp_us; 3687 in6_addr_t v6src; 3688 in6_addr_t v6dst; 3689 in6_addr_t v6nexthop; 3690 in_port_t dstport; 3691 3692 ASSERT(MUTEX_HELD(&connp->conn_lock)); 3693 ASSERT(ixa != NULL); 3694 /* 3695 * We hold conn_lock across all the use and modifications of 3696 * the conn_lastdst, conn_ixa, and conn_xmit_ipp to ensure that they 3697 * stay consistent. 3698 */ 3699 3700 ASSERT(cr != NULL); 3701 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 3702 ixa->ixa_cred = cr; 3703 ixa->ixa_cpid = pid; 3704 if (is_system_labeled()) { 3705 /* We need to restart with a label based on the cred */ 3706 ip_xmit_attr_restore_tsl(ixa, ixa->ixa_cred); 3707 } 3708 3709 /* 3710 * If we are connected then the destination needs to be the 3711 * same as the connected one, which is not the case here since we 3712 * checked for that above. 3713 */ 3714 if (udp->udp_state == TS_DATA_XFER) { 3715 mutex_exit(&connp->conn_lock); 3716 error = EISCONN; 3717 goto ud_error; 3718 } 3719 3720 /* In case previous destination was multicast or multirt */ 3721 ip_attr_newdst(ixa); 3722 3723 /* 3724 * If laddr is unspecified then we look at sin6_src_id. 3725 * We will give precedence to a source address set with IPV6_PKTINFO 3726 * (aka IPPF_ADDR) but that is handled in build_hdrs. However, we don't 3727 * want ip_attr_connect to select a source (since it can fail) when 3728 * IPV6_PKTINFO is specified. 3729 * If this doesn't result in a source address then we get a source 3730 * from ip_attr_connect() below. 3731 */ 3732 v6src = connp->conn_saddr_v6; 3733 if (sin != NULL) { 3734 IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &v6dst); 3735 dstport = sin->sin_port; 3736 flowinfo = 0; 3737 /* Don't bother with ip_srcid_find_id(), but indicate anyway. */ 3738 srcid = 0; 3739 ixa->ixa_flags &= ~IXAF_SCOPEID_SET; 3740 ixa->ixa_flags |= IXAF_IS_IPV4; 3741 } else { 3742 boolean_t v4mapped; 3743 3744 v6dst = sin6->sin6_addr; 3745 dstport = sin6->sin6_port; 3746 flowinfo = sin6->sin6_flowinfo; 3747 srcid = sin6->__sin6_src_id; 3748 if (IN6_IS_ADDR_LINKSCOPE(&v6dst) && sin6->sin6_scope_id != 0) { 3749 ixa->ixa_scopeid = sin6->sin6_scope_id; 3750 ixa->ixa_flags |= IXAF_SCOPEID_SET; 3751 } else { 3752 ixa->ixa_flags &= ~IXAF_SCOPEID_SET; 3753 } 3754 v4mapped = IN6_IS_ADDR_V4MAPPED(&v6dst); 3755 if (v4mapped) 3756 ixa->ixa_flags |= IXAF_IS_IPV4; 3757 else 3758 ixa->ixa_flags &= ~IXAF_IS_IPV4; 3759 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 3760 if (!ip_srcid_find_id(srcid, &v6src, IPCL_ZONEID(connp), 3761 v4mapped, connp->conn_netstack)) { 3762 /* Mismatched v4mapped/v6 specified by srcid. */ 3763 mutex_exit(&connp->conn_lock); 3764 error = EADDRNOTAVAIL; 3765 goto ud_error; 3766 } 3767 } 3768 } 3769 /* Handle IP_PKTINFO/IPV6_PKTINFO setting source address. */ 3770 if (connp->conn_xmit_ipp.ipp_fields & IPPF_ADDR) { 3771 ip_pkt_t *ipp = &connp->conn_xmit_ipp; 3772 3773 if (ixa->ixa_flags & IXAF_IS_IPV4) { 3774 if (IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr)) 3775 v6src = ipp->ipp_addr; 3776 } else { 3777 if (!IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr)) 3778 v6src = ipp->ipp_addr; 3779 } 3780 } 3781 3782 ip_attr_nexthop(&connp->conn_xmit_ipp, ixa, &v6dst, &v6nexthop); 3783 mutex_exit(&connp->conn_lock); 3784 3785 error = ip_attr_connect(connp, ixa, &v6src, &v6dst, &v6nexthop, dstport, 3786 &v6src, NULL, IPDF_ALLOW_MCBC | IPDF_VERIFY_DST | IPDF_IPSEC); 3787 switch (error) { 3788 case 0: 3789 break; 3790 case EADDRNOTAVAIL: 3791 /* 3792 * IXAF_VERIFY_SOURCE tells us to pick a better source. 3793 * Don't have the application see that errno 3794 */ 3795 error = ENETUNREACH; 3796 goto failed; 3797 case ENETDOWN: 3798 /* 3799 * Have !ipif_addr_ready address; drop packet silently 3800 * until we can get applications to not send until we 3801 * are ready. 3802 */ 3803 error = 0; 3804 goto failed; 3805 case EHOSTUNREACH: 3806 case ENETUNREACH: 3807 if (ixa->ixa_ire != NULL) { 3808 /* 3809 * Let conn_ip_output/ire_send_noroute return 3810 * the error and send any local ICMP error. 3811 */ 3812 error = 0; 3813 break; 3814 } 3815 /* FALLTHRU */ 3816 failed: 3817 default: 3818 goto ud_error; 3819 } 3820 3821 3822 /* 3823 * Cluster note: we let the cluster hook know that we are sending to a 3824 * new address and/or port. 3825 */ 3826 if (cl_inet_connect2 != NULL) { 3827 CL_INET_UDP_CONNECT(connp, B_TRUE, &v6dst, dstport, error); 3828 if (error != 0) { 3829 error = EHOSTUNREACH; 3830 goto ud_error; 3831 } 3832 } 3833 3834 mutex_enter(&connp->conn_lock); 3835 /* 3836 * While we dropped the lock some other thread might have connected 3837 * this socket. If so we bail out with EISCONN to ensure that the 3838 * connecting thread is the one that updates conn_ixa, conn_ht_* 3839 * and conn_*last*. 3840 */ 3841 if (udp->udp_state == TS_DATA_XFER) { 3842 mutex_exit(&connp->conn_lock); 3843 error = EISCONN; 3844 goto ud_error; 3845 } 3846 3847 /* 3848 * We need to rebuild the headers if 3849 * - we are labeling packets (could be different for different 3850 * destinations) 3851 * - we have a source route (or routing header) since we need to 3852 * massage that to get the pseudo-header checksum 3853 * - the IP version is different than the last time 3854 * - a socket option with COA_HEADER_CHANGED has been set which 3855 * set conn_v6lastdst to zero. 3856 * 3857 * Otherwise the prepend function will just update the src, dst, 3858 * dstport, and flow label. 3859 */ 3860 if (is_system_labeled()) { 3861 /* TX MLP requires SCM_UCRED and don't have that here */ 3862 if (connp->conn_mlp_type != mlptSingle) { 3863 mutex_exit(&connp->conn_lock); 3864 error = ECONNREFUSED; 3865 goto ud_error; 3866 } 3867 /* 3868 * Check whether Trusted Solaris policy allows communication 3869 * with this host, and pretend that the destination is 3870 * unreachable if not. 3871 * Compute any needed label and place it in ipp_label_v4/v6. 3872 * 3873 * Later conn_build_hdr_template/conn_prepend_hdr takes 3874 * ipp_label_v4/v6 to form the packet. 3875 * 3876 * Tsol note: Since we hold conn_lock we know no other 3877 * thread manipulates conn_xmit_ipp. 3878 */ 3879 error = conn_update_label(connp, ixa, &v6dst, 3880 &connp->conn_xmit_ipp); 3881 if (error != 0) { 3882 mutex_exit(&connp->conn_lock); 3883 goto ud_error; 3884 } 3885 /* Rebuild the header template */ 3886 error = udp_build_hdr_template(connp, &v6src, &v6dst, dstport, 3887 flowinfo); 3888 if (error != 0) { 3889 mutex_exit(&connp->conn_lock); 3890 goto ud_error; 3891 } 3892 } else if ((connp->conn_xmit_ipp.ipp_fields & 3893 (IPPF_IPV4_OPTIONS|IPPF_RTHDR)) || 3894 ipversion != connp->conn_lastipversion || 3895 IN6_IS_ADDR_UNSPECIFIED(&connp->conn_v6lastdst)) { 3896 /* Rebuild the header template */ 3897 error = udp_build_hdr_template(connp, &v6src, &v6dst, dstport, 3898 flowinfo); 3899 if (error != 0) { 3900 mutex_exit(&connp->conn_lock); 3901 goto ud_error; 3902 } 3903 } else { 3904 /* Simply update the destination address if no source route */ 3905 if (ixa->ixa_flags & IXAF_IS_IPV4) { 3906 ipha_t *ipha = (ipha_t *)connp->conn_ht_iphc; 3907 3908 IN6_V4MAPPED_TO_IPADDR(&v6dst, ipha->ipha_dst); 3909 if (ixa->ixa_flags & IXAF_PMTU_IPV4_DF) { 3910 ipha->ipha_fragment_offset_and_flags |= 3911 IPH_DF_HTONS; 3912 } else { 3913 ipha->ipha_fragment_offset_and_flags &= 3914 ~IPH_DF_HTONS; 3915 } 3916 } else { 3917 ip6_t *ip6h = (ip6_t *)connp->conn_ht_iphc; 3918 ip6h->ip6_dst = v6dst; 3919 } 3920 } 3921 3922 /* 3923 * Remember the dst/dstport etc which corresponds to the built header 3924 * template and conn_ixa. 3925 */ 3926 oldixa = conn_replace_ixa(connp, ixa); 3927 connp->conn_v6lastdst = v6dst; 3928 connp->conn_lastipversion = ipversion; 3929 connp->conn_lastdstport = dstport; 3930 connp->conn_lastflowinfo = flowinfo; 3931 connp->conn_lastscopeid = ixa->ixa_scopeid; 3932 connp->conn_lastsrcid = srcid; 3933 /* Also remember a source to use together with lastdst */ 3934 connp->conn_v6lastsrc = v6src; 3935 3936 data_mp = udp_prepend_header_template(connp, ixa, data_mp, &v6src, 3937 dstport, flowinfo, &error); 3938 3939 /* Done with conn_t */ 3940 mutex_exit(&connp->conn_lock); 3941 ixa_refrele(oldixa); 3942 3943 if (data_mp == NULL) { 3944 ASSERT(error != 0); 3945 goto ud_error; 3946 } 3947 3948 /* We're done. Pass the packet to ip. */ 3949 UDPS_BUMP_MIB(us, udpHCOutDatagrams); 3950 3951 DTRACE_UDP5(send, mblk_t *, NULL, ip_xmit_attr_t *, ixa, 3952 void_ip_t *, data_mp->b_rptr, udp_t *, udp, udpha_t *, 3953 &data_mp->b_rptr[ixa->ixa_ip_hdr_length]); 3954 3955 error = conn_ip_output(data_mp, ixa); 3956 /* No udpOutErrors if an error since IP increases its error counter */ 3957 switch (error) { 3958 case 0: 3959 break; 3960 case EWOULDBLOCK: 3961 (void) ixa_check_drain_insert(connp, ixa); 3962 error = 0; 3963 break; 3964 case EADDRNOTAVAIL: 3965 /* 3966 * IXAF_VERIFY_SOURCE tells us to pick a better source. 3967 * Don't have the application see that errno 3968 */ 3969 error = ENETUNREACH; 3970 /* FALLTHRU */ 3971 default: 3972 mutex_enter(&connp->conn_lock); 3973 /* 3974 * Clear the source and v6lastdst so we call ip_attr_connect 3975 * for the next packet and try to pick a better source. 3976 */ 3977 if (connp->conn_mcbc_bind) 3978 connp->conn_saddr_v6 = ipv6_all_zeros; 3979 else 3980 connp->conn_saddr_v6 = connp->conn_bound_addr_v6; 3981 connp->conn_v6lastdst = ipv6_all_zeros; 3982 mutex_exit(&connp->conn_lock); 3983 break; 3984 } 3985 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 3986 ixa->ixa_cred = connp->conn_cred; /* Restore */ 3987 ixa->ixa_cpid = connp->conn_cpid; 3988 ixa_refrele(ixa); 3989 return (error); 3990 3991 ud_error: 3992 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 3993 ixa->ixa_cred = connp->conn_cred; /* Restore */ 3994 ixa->ixa_cpid = connp->conn_cpid; 3995 ixa_refrele(ixa); 3996 3997 freemsg(data_mp); 3998 UDPS_BUMP_MIB(us, udpOutErrors); 3999 UDP_STAT(us, udp_out_err_output); 4000 return (error); 4001 } 4002 4003 /* ARGSUSED */ 4004 static int 4005 udp_wput_fallback(queue_t *wq, mblk_t *mp) 4006 { 4007 #ifdef DEBUG 4008 cmn_err(CE_CONT, "udp_wput_fallback: Message in fallback \n"); 4009 #endif 4010 freemsg(mp); 4011 return (0); 4012 } 4013 4014 4015 /* 4016 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 4017 */ 4018 static void 4019 udp_wput_cmdblk(queue_t *q, mblk_t *mp) 4020 { 4021 void *data; 4022 mblk_t *datamp = mp->b_cont; 4023 conn_t *connp = Q_TO_CONN(q); 4024 udp_t *udp = connp->conn_udp; 4025 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 4026 4027 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 4028 cmdp->cb_error = EPROTO; 4029 qreply(q, mp); 4030 return; 4031 } 4032 data = datamp->b_rptr; 4033 4034 mutex_enter(&connp->conn_lock); 4035 switch (cmdp->cb_cmd) { 4036 case TI_GETPEERNAME: 4037 if (udp->udp_state != TS_DATA_XFER) 4038 cmdp->cb_error = ENOTCONN; 4039 else 4040 cmdp->cb_error = conn_getpeername(connp, data, 4041 &cmdp->cb_len); 4042 break; 4043 case TI_GETMYNAME: 4044 cmdp->cb_error = conn_getsockname(connp, data, &cmdp->cb_len); 4045 break; 4046 default: 4047 cmdp->cb_error = EINVAL; 4048 break; 4049 } 4050 mutex_exit(&connp->conn_lock); 4051 4052 qreply(q, mp); 4053 } 4054 4055 static void 4056 udp_use_pure_tpi(udp_t *udp) 4057 { 4058 conn_t *connp = udp->udp_connp; 4059 4060 mutex_enter(&connp->conn_lock); 4061 udp->udp_issocket = B_FALSE; 4062 mutex_exit(&connp->conn_lock); 4063 UDP_STAT(udp->udp_us, udp_sock_fallback); 4064 } 4065 4066 static void 4067 udp_wput_other(queue_t *q, mblk_t *mp) 4068 { 4069 uchar_t *rptr = mp->b_rptr; 4070 struct iocblk *iocp; 4071 conn_t *connp = Q_TO_CONN(q); 4072 udp_t *udp = connp->conn_udp; 4073 cred_t *cr; 4074 4075 switch (mp->b_datap->db_type) { 4076 case M_CMD: 4077 udp_wput_cmdblk(q, mp); 4078 return; 4079 4080 case M_PROTO: 4081 case M_PCPROTO: 4082 if (mp->b_wptr - rptr < sizeof (t_scalar_t)) { 4083 /* 4084 * If the message does not contain a PRIM_type, 4085 * throw it away. 4086 */ 4087 freemsg(mp); 4088 return; 4089 } 4090 switch (((t_primp_t)rptr)->type) { 4091 case T_ADDR_REQ: 4092 udp_addr_req(q, mp); 4093 return; 4094 case O_T_BIND_REQ: 4095 case T_BIND_REQ: 4096 udp_tpi_bind(q, mp); 4097 return; 4098 case T_CONN_REQ: 4099 udp_tpi_connect(q, mp); 4100 return; 4101 case T_CAPABILITY_REQ: 4102 udp_capability_req(q, mp); 4103 return; 4104 case T_INFO_REQ: 4105 udp_info_req(q, mp); 4106 return; 4107 case T_UNITDATA_REQ: 4108 /* 4109 * If a T_UNITDATA_REQ gets here, the address must 4110 * be bad. Valid T_UNITDATA_REQs are handled 4111 * in udp_wput. 4112 */ 4113 udp_ud_err(q, mp, EADDRNOTAVAIL); 4114 return; 4115 case T_UNBIND_REQ: 4116 udp_tpi_unbind(q, mp); 4117 return; 4118 case T_SVR4_OPTMGMT_REQ: 4119 /* 4120 * All Solaris components should pass a db_credp 4121 * for this TPI message, hence we ASSERT. 4122 * But in case there is some other M_PROTO that looks 4123 * like a TPI message sent by some other kernel 4124 * component, we check and return an error. 4125 */ 4126 cr = msg_getcred(mp, NULL); 4127 ASSERT(cr != NULL); 4128 if (cr == NULL) { 4129 udp_err_ack(q, mp, TSYSERR, EINVAL); 4130 return; 4131 } 4132 if (!snmpcom_req(q, mp, udp_snmp_set, ip_snmp_get, 4133 cr)) { 4134 svr4_optcom_req(q, mp, cr, &udp_opt_obj); 4135 } 4136 return; 4137 4138 case T_OPTMGMT_REQ: 4139 /* 4140 * All Solaris components should pass a db_credp 4141 * for this TPI message, hence we ASSERT. 4142 * But in case there is some other M_PROTO that looks 4143 * like a TPI message sent by some other kernel 4144 * component, we check and return an error. 4145 */ 4146 cr = msg_getcred(mp, NULL); 4147 ASSERT(cr != NULL); 4148 if (cr == NULL) { 4149 udp_err_ack(q, mp, TSYSERR, EINVAL); 4150 return; 4151 } 4152 tpi_optcom_req(q, mp, cr, &udp_opt_obj); 4153 return; 4154 4155 case T_DISCON_REQ: 4156 udp_tpi_disconnect(q, mp); 4157 return; 4158 4159 /* The following TPI message is not supported by udp. */ 4160 case O_T_CONN_RES: 4161 case T_CONN_RES: 4162 udp_err_ack(q, mp, TNOTSUPPORT, 0); 4163 return; 4164 4165 /* The following 3 TPI requests are illegal for udp. */ 4166 case T_DATA_REQ: 4167 case T_EXDATA_REQ: 4168 case T_ORDREL_REQ: 4169 udp_err_ack(q, mp, TNOTSUPPORT, 0); 4170 return; 4171 default: 4172 break; 4173 } 4174 break; 4175 case M_FLUSH: 4176 if (*rptr & FLUSHW) 4177 flushq(q, FLUSHDATA); 4178 break; 4179 case M_IOCTL: 4180 iocp = (struct iocblk *)mp->b_rptr; 4181 switch (iocp->ioc_cmd) { 4182 case TI_GETPEERNAME: 4183 if (udp->udp_state != TS_DATA_XFER) { 4184 /* 4185 * If a default destination address has not 4186 * been associated with the stream, then we 4187 * don't know the peer's name. 4188 */ 4189 iocp->ioc_error = ENOTCONN; 4190 iocp->ioc_count = 0; 4191 mp->b_datap->db_type = M_IOCACK; 4192 qreply(q, mp); 4193 return; 4194 } 4195 /* FALLTHRU */ 4196 case TI_GETMYNAME: 4197 /* 4198 * For TI_GETPEERNAME and TI_GETMYNAME, we first 4199 * need to copyin the user's strbuf structure. 4200 * Processing will continue in the M_IOCDATA case 4201 * below. 4202 */ 4203 mi_copyin(q, mp, NULL, 4204 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 4205 return; 4206 case _SIOCSOCKFALLBACK: 4207 /* 4208 * Either sockmod is about to be popped and the 4209 * socket would now be treated as a plain stream, 4210 * or a module is about to be pushed so we have 4211 * to follow pure TPI semantics. 4212 */ 4213 if (!udp->udp_issocket) { 4214 DB_TYPE(mp) = M_IOCNAK; 4215 iocp->ioc_error = EINVAL; 4216 } else { 4217 udp_use_pure_tpi(udp); 4218 4219 DB_TYPE(mp) = M_IOCACK; 4220 iocp->ioc_error = 0; 4221 } 4222 iocp->ioc_count = 0; 4223 iocp->ioc_rval = 0; 4224 qreply(q, mp); 4225 return; 4226 default: 4227 break; 4228 } 4229 break; 4230 case M_IOCDATA: 4231 udp_wput_iocdata(q, mp); 4232 return; 4233 default: 4234 /* Unrecognized messages are passed through without change. */ 4235 break; 4236 } 4237 ip_wput_nondata(q, mp); 4238 } 4239 4240 /* 4241 * udp_wput_iocdata is called by udp_wput_other to handle all M_IOCDATA 4242 * messages. 4243 */ 4244 static void 4245 udp_wput_iocdata(queue_t *q, mblk_t *mp) 4246 { 4247 mblk_t *mp1; 4248 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 4249 STRUCT_HANDLE(strbuf, sb); 4250 uint_t addrlen; 4251 conn_t *connp = Q_TO_CONN(q); 4252 udp_t *udp = connp->conn_udp; 4253 4254 /* Make sure it is one of ours. */ 4255 switch (iocp->ioc_cmd) { 4256 case TI_GETMYNAME: 4257 case TI_GETPEERNAME: 4258 break; 4259 default: 4260 ip_wput_nondata(q, mp); 4261 return; 4262 } 4263 4264 switch (mi_copy_state(q, mp, &mp1)) { 4265 case -1: 4266 return; 4267 case MI_COPY_CASE(MI_COPY_IN, 1): 4268 break; 4269 case MI_COPY_CASE(MI_COPY_OUT, 1): 4270 /* 4271 * The address has been copied out, so now 4272 * copyout the strbuf. 4273 */ 4274 mi_copyout(q, mp); 4275 return; 4276 case MI_COPY_CASE(MI_COPY_OUT, 2): 4277 /* 4278 * The address and strbuf have been copied out. 4279 * We're done, so just acknowledge the original 4280 * M_IOCTL. 4281 */ 4282 mi_copy_done(q, mp, 0); 4283 return; 4284 default: 4285 /* 4286 * Something strange has happened, so acknowledge 4287 * the original M_IOCTL with an EPROTO error. 4288 */ 4289 mi_copy_done(q, mp, EPROTO); 4290 return; 4291 } 4292 4293 /* 4294 * Now we have the strbuf structure for TI_GETMYNAME 4295 * and TI_GETPEERNAME. Next we copyout the requested 4296 * address and then we'll copyout the strbuf. 4297 */ 4298 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 4299 4300 if (connp->conn_family == AF_INET) 4301 addrlen = sizeof (sin_t); 4302 else 4303 addrlen = sizeof (sin6_t); 4304 4305 if (STRUCT_FGET(sb, maxlen) < addrlen) { 4306 mi_copy_done(q, mp, EINVAL); 4307 return; 4308 } 4309 4310 switch (iocp->ioc_cmd) { 4311 case TI_GETMYNAME: 4312 break; 4313 case TI_GETPEERNAME: 4314 if (udp->udp_state != TS_DATA_XFER) { 4315 mi_copy_done(q, mp, ENOTCONN); 4316 return; 4317 } 4318 break; 4319 } 4320 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 4321 if (!mp1) 4322 return; 4323 4324 STRUCT_FSET(sb, len, addrlen); 4325 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 4326 case TI_GETMYNAME: 4327 (void) conn_getsockname(connp, (struct sockaddr *)mp1->b_wptr, 4328 &addrlen); 4329 break; 4330 case TI_GETPEERNAME: 4331 (void) conn_getpeername(connp, (struct sockaddr *)mp1->b_wptr, 4332 &addrlen); 4333 break; 4334 } 4335 mp1->b_wptr += addrlen; 4336 /* Copy out the address */ 4337 mi_copyout(q, mp); 4338 } 4339 4340 void 4341 udp_ddi_g_init(void) 4342 { 4343 udp_max_optsize = optcom_max_optsize(udp_opt_obj.odb_opt_des_arr, 4344 udp_opt_obj.odb_opt_arr_cnt); 4345 4346 /* 4347 * We want to be informed each time a stack is created or 4348 * destroyed in the kernel, so we can maintain the 4349 * set of udp_stack_t's. 4350 */ 4351 netstack_register(NS_UDP, udp_stack_init, NULL, udp_stack_fini); 4352 } 4353 4354 void 4355 udp_ddi_g_destroy(void) 4356 { 4357 netstack_unregister(NS_UDP); 4358 } 4359 4360 #define INET_NAME "ip" 4361 4362 /* 4363 * Initialize the UDP stack instance. 4364 */ 4365 static void * 4366 udp_stack_init(netstackid_t stackid, netstack_t *ns) 4367 { 4368 udp_stack_t *us; 4369 int i; 4370 int error = 0; 4371 major_t major; 4372 size_t arrsz; 4373 4374 us = (udp_stack_t *)kmem_zalloc(sizeof (*us), KM_SLEEP); 4375 us->us_netstack = ns; 4376 4377 mutex_init(&us->us_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 4378 us->us_num_epriv_ports = UDP_NUM_EPRIV_PORTS; 4379 us->us_epriv_ports[0] = ULP_DEF_EPRIV_PORT1; 4380 us->us_epriv_ports[1] = ULP_DEF_EPRIV_PORT2; 4381 4382 /* 4383 * The smallest anonymous port in the priviledged port range which UDP 4384 * looks for free port. Use in the option UDP_ANONPRIVBIND. 4385 */ 4386 us->us_min_anonpriv_port = 512; 4387 4388 us->us_bind_fanout_size = udp_bind_fanout_size; 4389 4390 /* Roundup variable that might have been modified in /etc/system */ 4391 if (!ISP2(us->us_bind_fanout_size)) { 4392 /* Not a power of two. Round up to nearest power of two */ 4393 for (i = 0; i < 31; i++) { 4394 if (us->us_bind_fanout_size < (1 << i)) 4395 break; 4396 } 4397 us->us_bind_fanout_size = 1 << i; 4398 } 4399 us->us_bind_fanout = kmem_zalloc(us->us_bind_fanout_size * 4400 sizeof (udp_fanout_t), KM_SLEEP); 4401 for (i = 0; i < us->us_bind_fanout_size; i++) { 4402 mutex_init(&us->us_bind_fanout[i].uf_lock, NULL, MUTEX_DEFAULT, 4403 NULL); 4404 } 4405 4406 arrsz = udp_propinfo_count * sizeof (mod_prop_info_t); 4407 us->us_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, 4408 KM_SLEEP); 4409 bcopy(udp_propinfo_tbl, us->us_propinfo_tbl, arrsz); 4410 4411 /* Allocate the per netstack stats */ 4412 mutex_enter(&cpu_lock); 4413 us->us_sc_cnt = MAX(ncpus, boot_ncpus); 4414 mutex_exit(&cpu_lock); 4415 us->us_sc = kmem_zalloc(max_ncpus * sizeof (udp_stats_cpu_t *), 4416 KM_SLEEP); 4417 for (i = 0; i < us->us_sc_cnt; i++) { 4418 us->us_sc[i] = kmem_zalloc(sizeof (udp_stats_cpu_t), 4419 KM_SLEEP); 4420 } 4421 4422 us->us_kstat = udp_kstat2_init(stackid); 4423 us->us_mibkp = udp_kstat_init(stackid); 4424 4425 major = mod_name_to_major(INET_NAME); 4426 error = ldi_ident_from_major(major, &us->us_ldi_ident); 4427 ASSERT(error == 0); 4428 return (us); 4429 } 4430 4431 /* 4432 * Free the UDP stack instance. 4433 */ 4434 static void 4435 udp_stack_fini(netstackid_t stackid, void *arg) 4436 { 4437 udp_stack_t *us = (udp_stack_t *)arg; 4438 int i; 4439 4440 for (i = 0; i < us->us_bind_fanout_size; i++) { 4441 mutex_destroy(&us->us_bind_fanout[i].uf_lock); 4442 } 4443 4444 kmem_free(us->us_bind_fanout, us->us_bind_fanout_size * 4445 sizeof (udp_fanout_t)); 4446 4447 us->us_bind_fanout = NULL; 4448 4449 for (i = 0; i < us->us_sc_cnt; i++) 4450 kmem_free(us->us_sc[i], sizeof (udp_stats_cpu_t)); 4451 kmem_free(us->us_sc, max_ncpus * sizeof (udp_stats_cpu_t *)); 4452 4453 kmem_free(us->us_propinfo_tbl, 4454 udp_propinfo_count * sizeof (mod_prop_info_t)); 4455 us->us_propinfo_tbl = NULL; 4456 4457 udp_kstat_fini(stackid, us->us_mibkp); 4458 us->us_mibkp = NULL; 4459 4460 udp_kstat2_fini(stackid, us->us_kstat); 4461 us->us_kstat = NULL; 4462 4463 mutex_destroy(&us->us_epriv_port_lock); 4464 ldi_ident_release(us->us_ldi_ident); 4465 kmem_free(us, sizeof (*us)); 4466 } 4467 4468 static size_t 4469 udp_set_rcv_hiwat(udp_t *udp, size_t size) 4470 { 4471 udp_stack_t *us = udp->udp_us; 4472 4473 /* We add a bit of extra buffering */ 4474 size += size >> 1; 4475 if (size > us->us_max_buf) 4476 size = us->us_max_buf; 4477 4478 udp->udp_rcv_hiwat = size; 4479 return (size); 4480 } 4481 4482 /* 4483 * For the lower queue so that UDP can be a dummy mux. 4484 * Nobody should be sending 4485 * packets up this stream 4486 */ 4487 static int 4488 udp_lrput(queue_t *q, mblk_t *mp) 4489 { 4490 switch (mp->b_datap->db_type) { 4491 case M_FLUSH: 4492 /* Turn around */ 4493 if (*mp->b_rptr & FLUSHW) { 4494 *mp->b_rptr &= ~FLUSHR; 4495 qreply(q, mp); 4496 return (0); 4497 } 4498 break; 4499 } 4500 freemsg(mp); 4501 return (0); 4502 } 4503 4504 /* 4505 * For the lower queue so that UDP can be a dummy mux. 4506 * Nobody should be sending packets down this stream. 4507 */ 4508 /* ARGSUSED */ 4509 int 4510 udp_lwput(queue_t *q, mblk_t *mp) 4511 { 4512 freemsg(mp); 4513 return (0); 4514 } 4515 4516 /* 4517 * When a CPU is added, we need to allocate the per CPU stats struct. 4518 */ 4519 void 4520 udp_stack_cpu_add(udp_stack_t *us, processorid_t cpu_seqid) 4521 { 4522 int i; 4523 4524 if (cpu_seqid < us->us_sc_cnt) 4525 return; 4526 for (i = us->us_sc_cnt; i <= cpu_seqid; i++) { 4527 ASSERT(us->us_sc[i] == NULL); 4528 us->us_sc[i] = kmem_zalloc(sizeof (udp_stats_cpu_t), 4529 KM_SLEEP); 4530 } 4531 membar_producer(); 4532 us->us_sc_cnt = cpu_seqid + 1; 4533 } 4534 4535 /* 4536 * Below routines for UDP socket module. 4537 */ 4538 4539 static conn_t * 4540 udp_do_open(cred_t *credp, boolean_t isv6, int flags, int *errorp) 4541 { 4542 udp_t *udp; 4543 conn_t *connp; 4544 zoneid_t zoneid; 4545 netstack_t *ns; 4546 udp_stack_t *us; 4547 int len; 4548 4549 ASSERT(errorp != NULL); 4550 4551 if ((*errorp = secpolicy_basic_net_access(credp)) != 0) 4552 return (NULL); 4553 4554 ns = netstack_find_by_cred(credp); 4555 ASSERT(ns != NULL); 4556 us = ns->netstack_udp; 4557 ASSERT(us != NULL); 4558 4559 /* 4560 * For exclusive stacks we set the zoneid to zero 4561 * to make UDP operate as if in the global zone. 4562 */ 4563 if (ns->netstack_stackid != GLOBAL_NETSTACKID) 4564 zoneid = GLOBAL_ZONEID; 4565 else 4566 zoneid = crgetzoneid(credp); 4567 4568 ASSERT(flags == KM_SLEEP || flags == KM_NOSLEEP); 4569 4570 connp = ipcl_conn_create(IPCL_UDPCONN, flags, ns); 4571 if (connp == NULL) { 4572 netstack_rele(ns); 4573 *errorp = ENOMEM; 4574 return (NULL); 4575 } 4576 udp = connp->conn_udp; 4577 4578 /* 4579 * ipcl_conn_create did a netstack_hold. Undo the hold that was 4580 * done by netstack_find_by_cred() 4581 */ 4582 netstack_rele(ns); 4583 4584 /* 4585 * Since this conn_t/udp_t is not yet visible to anybody else we don't 4586 * need to lock anything. 4587 */ 4588 ASSERT(connp->conn_proto == IPPROTO_UDP); 4589 ASSERT(connp->conn_udp == udp); 4590 ASSERT(udp->udp_connp == connp); 4591 4592 /* Set the initial state of the stream and the privilege status. */ 4593 udp->udp_state = TS_UNBND; 4594 connp->conn_ixa->ixa_flags |= IXAF_VERIFY_SOURCE; 4595 if (isv6) { 4596 connp->conn_family = AF_INET6; 4597 connp->conn_ipversion = IPV6_VERSION; 4598 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4; 4599 connp->conn_default_ttl = us->us_ipv6_hoplimit; 4600 len = sizeof (ip6_t) + UDPH_SIZE; 4601 } else { 4602 connp->conn_family = AF_INET; 4603 connp->conn_ipversion = IPV4_VERSION; 4604 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4; 4605 connp->conn_default_ttl = us->us_ipv4_ttl; 4606 len = sizeof (ipha_t) + UDPH_SIZE; 4607 } 4608 4609 ASSERT(connp->conn_ixa->ixa_protocol == connp->conn_proto); 4610 connp->conn_xmit_ipp.ipp_unicast_hops = connp->conn_default_ttl; 4611 4612 connp->conn_ixa->ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 4613 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM; 4614 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */ 4615 connp->conn_ixa->ixa_zoneid = zoneid; 4616 4617 connp->conn_zoneid = zoneid; 4618 4619 /* 4620 * If the caller has the process-wide flag set, then default to MAC 4621 * exempt mode. This allows read-down to unlabeled hosts. 4622 */ 4623 if (getpflags(NET_MAC_AWARE, credp) != 0) 4624 connp->conn_mac_mode = CONN_MAC_AWARE; 4625 4626 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID); 4627 4628 udp->udp_us = us; 4629 4630 connp->conn_rcvbuf = us->us_recv_hiwat; 4631 connp->conn_sndbuf = us->us_xmit_hiwat; 4632 connp->conn_sndlowat = us->us_xmit_lowat; 4633 connp->conn_rcvlowat = udp_mod_info.mi_lowat; 4634 4635 connp->conn_wroff = len + us->us_wroff_extra; 4636 connp->conn_so_type = SOCK_DGRAM; 4637 4638 connp->conn_recv = udp_input; 4639 connp->conn_recvicmp = udp_icmp_input; 4640 crhold(credp); 4641 connp->conn_cred = credp; 4642 connp->conn_cpid = curproc->p_pid; 4643 connp->conn_open_time = ddi_get_lbolt64(); 4644 /* Cache things in ixa without an extra refhold */ 4645 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED)); 4646 connp->conn_ixa->ixa_cred = connp->conn_cred; 4647 connp->conn_ixa->ixa_cpid = connp->conn_cpid; 4648 if (is_system_labeled()) 4649 connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred); 4650 4651 *((sin6_t *)&udp->udp_delayed_addr) = sin6_null; 4652 4653 if (us->us_pmtu_discovery) 4654 connp->conn_ixa->ixa_flags |= IXAF_PMTU_DISCOVERY; 4655 4656 return (connp); 4657 } 4658 4659 sock_lower_handle_t 4660 udp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 4661 uint_t *smodep, int *errorp, int flags, cred_t *credp) 4662 { 4663 udp_t *udp = NULL; 4664 udp_stack_t *us; 4665 conn_t *connp; 4666 boolean_t isv6; 4667 4668 if (type != SOCK_DGRAM || (family != AF_INET && family != AF_INET6) || 4669 (proto != 0 && proto != IPPROTO_UDP)) { 4670 *errorp = EPROTONOSUPPORT; 4671 return (NULL); 4672 } 4673 4674 if (family == AF_INET6) 4675 isv6 = B_TRUE; 4676 else 4677 isv6 = B_FALSE; 4678 4679 connp = udp_do_open(credp, isv6, flags, errorp); 4680 if (connp == NULL) 4681 return (NULL); 4682 4683 udp = connp->conn_udp; 4684 ASSERT(udp != NULL); 4685 us = udp->udp_us; 4686 ASSERT(us != NULL); 4687 4688 udp->udp_issocket = B_TRUE; 4689 connp->conn_flags |= IPCL_NONSTR; 4690 4691 /* 4692 * Set flow control 4693 * Since this conn_t/udp_t is not yet visible to anybody else we don't 4694 * need to lock anything. 4695 */ 4696 (void) udp_set_rcv_hiwat(udp, connp->conn_rcvbuf); 4697 udp->udp_rcv_disply_hiwat = connp->conn_rcvbuf; 4698 4699 connp->conn_flow_cntrld = B_FALSE; 4700 4701 mutex_enter(&connp->conn_lock); 4702 connp->conn_state_flags &= ~CONN_INCIPIENT; 4703 mutex_exit(&connp->conn_lock); 4704 4705 *errorp = 0; 4706 *smodep = SM_ATOMIC; 4707 *sock_downcalls = &sock_udp_downcalls; 4708 return ((sock_lower_handle_t)connp); 4709 } 4710 4711 /* ARGSUSED3 */ 4712 void 4713 udp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 4714 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 4715 { 4716 conn_t *connp = (conn_t *)proto_handle; 4717 struct sock_proto_props sopp; 4718 4719 /* All Solaris components should pass a cred for this operation. */ 4720 ASSERT(cr != NULL); 4721 4722 connp->conn_upcalls = sock_upcalls; 4723 connp->conn_upper_handle = sock_handle; 4724 4725 sopp.sopp_flags = SOCKOPT_WROFF | SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 4726 SOCKOPT_MAXBLK | SOCKOPT_MAXPSZ | SOCKOPT_MINPSZ; 4727 sopp.sopp_wroff = connp->conn_wroff; 4728 sopp.sopp_maxblk = INFPSZ; 4729 sopp.sopp_rxhiwat = connp->conn_rcvbuf; 4730 sopp.sopp_rxlowat = connp->conn_rcvlowat; 4731 sopp.sopp_maxaddrlen = sizeof (sin6_t); 4732 sopp.sopp_maxpsz = 4733 (connp->conn_family == AF_INET) ? UDP_MAXPACKET_IPV4 : 4734 UDP_MAXPACKET_IPV6; 4735 sopp.sopp_minpsz = (udp_mod_info.mi_minpsz == 1) ? 0 : 4736 udp_mod_info.mi_minpsz; 4737 4738 (*connp->conn_upcalls->su_set_proto_props)(connp->conn_upper_handle, 4739 &sopp); 4740 } 4741 4742 static void 4743 udp_do_close(conn_t *connp) 4744 { 4745 udp_t *udp; 4746 4747 ASSERT(connp != NULL && IPCL_IS_UDP(connp)); 4748 udp = connp->conn_udp; 4749 4750 if (cl_inet_unbind != NULL && udp->udp_state == TS_IDLE) { 4751 /* 4752 * Running in cluster mode - register unbind information 4753 */ 4754 if (connp->conn_ipversion == IPV4_VERSION) { 4755 (*cl_inet_unbind)( 4756 connp->conn_netstack->netstack_stackid, 4757 IPPROTO_UDP, AF_INET, 4758 (uint8_t *)(&V4_PART_OF_V6(connp->conn_laddr_v6)), 4759 (in_port_t)connp->conn_lport, NULL); 4760 } else { 4761 (*cl_inet_unbind)( 4762 connp->conn_netstack->netstack_stackid, 4763 IPPROTO_UDP, AF_INET6, 4764 (uint8_t *)&(connp->conn_laddr_v6), 4765 (in_port_t)connp->conn_lport, NULL); 4766 } 4767 } 4768 4769 udp_bind_hash_remove(udp, B_FALSE); 4770 4771 ip_quiesce_conn(connp); 4772 4773 if (!IPCL_IS_NONSTR(connp)) { 4774 ASSERT(connp->conn_wq != NULL); 4775 ASSERT(connp->conn_rq != NULL); 4776 qprocsoff(connp->conn_rq); 4777 } 4778 4779 udp_close_free(connp); 4780 4781 /* 4782 * Now we are truly single threaded on this stream, and can 4783 * delete the things hanging off the connp, and finally the connp. 4784 * We removed this connp from the fanout list, it cannot be 4785 * accessed thru the fanouts, and we already waited for the 4786 * conn_ref to drop to 0. We are already in close, so 4787 * there cannot be any other thread from the top. qprocsoff 4788 * has completed, and service has completed or won't run in 4789 * future. 4790 */ 4791 ASSERT(connp->conn_ref == 1); 4792 4793 if (!IPCL_IS_NONSTR(connp)) { 4794 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 4795 } else { 4796 ip_free_helper_stream(connp); 4797 } 4798 4799 connp->conn_ref--; 4800 ipcl_conn_destroy(connp); 4801 } 4802 4803 /* ARGSUSED1 */ 4804 int 4805 udp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 4806 { 4807 conn_t *connp = (conn_t *)proto_handle; 4808 4809 /* All Solaris components should pass a cred for this operation. */ 4810 ASSERT(cr != NULL); 4811 4812 udp_do_close(connp); 4813 return (0); 4814 } 4815 4816 static int 4817 udp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 4818 boolean_t bind_to_req_port_only) 4819 { 4820 sin_t *sin; 4821 sin6_t *sin6; 4822 udp_t *udp = connp->conn_udp; 4823 int error = 0; 4824 ip_laddr_t laddr_type = IPVL_UNICAST_UP; /* INADDR_ANY */ 4825 in_port_t port; /* Host byte order */ 4826 in_port_t requested_port; /* Host byte order */ 4827 int count; 4828 ipaddr_t v4src; /* Set if AF_INET */ 4829 in6_addr_t v6src; 4830 int loopmax; 4831 udp_fanout_t *udpf; 4832 in_port_t lport; /* Network byte order */ 4833 uint_t scopeid = 0; 4834 zoneid_t zoneid = IPCL_ZONEID(connp); 4835 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 4836 boolean_t is_inaddr_any; 4837 mlp_type_t addrtype, mlptype; 4838 udp_stack_t *us = udp->udp_us; 4839 4840 switch (len) { 4841 case sizeof (sin_t): /* Complete IPv4 address */ 4842 sin = (sin_t *)sa; 4843 4844 if (sin == NULL || !OK_32PTR((char *)sin)) 4845 return (EINVAL); 4846 4847 if (connp->conn_family != AF_INET || 4848 sin->sin_family != AF_INET) { 4849 return (EAFNOSUPPORT); 4850 } 4851 v4src = sin->sin_addr.s_addr; 4852 IN6_IPADDR_TO_V4MAPPED(v4src, &v6src); 4853 if (v4src != INADDR_ANY) { 4854 laddr_type = ip_laddr_verify_v4(v4src, zoneid, ipst, 4855 B_TRUE); 4856 } 4857 port = ntohs(sin->sin_port); 4858 break; 4859 4860 case sizeof (sin6_t): /* complete IPv6 address */ 4861 sin6 = (sin6_t *)sa; 4862 4863 if (sin6 == NULL || !OK_32PTR((char *)sin6)) 4864 return (EINVAL); 4865 4866 if (connp->conn_family != AF_INET6 || 4867 sin6->sin6_family != AF_INET6) { 4868 return (EAFNOSUPPORT); 4869 } 4870 v6src = sin6->sin6_addr; 4871 if (IN6_IS_ADDR_V4MAPPED(&v6src)) { 4872 if (connp->conn_ipv6_v6only) 4873 return (EADDRNOTAVAIL); 4874 4875 IN6_V4MAPPED_TO_IPADDR(&v6src, v4src); 4876 if (v4src != INADDR_ANY) { 4877 laddr_type = ip_laddr_verify_v4(v4src, 4878 zoneid, ipst, B_FALSE); 4879 } 4880 } else { 4881 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 4882 if (IN6_IS_ADDR_LINKSCOPE(&v6src)) 4883 scopeid = sin6->sin6_scope_id; 4884 laddr_type = ip_laddr_verify_v6(&v6src, 4885 zoneid, ipst, B_TRUE, scopeid); 4886 } 4887 } 4888 port = ntohs(sin6->sin6_port); 4889 break; 4890 4891 default: /* Invalid request */ 4892 (void) strlog(UDP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 4893 "udp_bind: bad ADDR_length length %u", len); 4894 return (-TBADADDR); 4895 } 4896 4897 /* Is the local address a valid unicast, multicast, or broadcast? */ 4898 if (laddr_type == IPVL_BAD) 4899 return (EADDRNOTAVAIL); 4900 4901 requested_port = port; 4902 4903 if (requested_port == 0 || !bind_to_req_port_only) 4904 bind_to_req_port_only = B_FALSE; 4905 else /* T_BIND_REQ and requested_port != 0 */ 4906 bind_to_req_port_only = B_TRUE; 4907 4908 if (requested_port == 0) { 4909 /* 4910 * If the application passed in zero for the port number, it 4911 * doesn't care which port number we bind to. Get one in the 4912 * valid range. 4913 */ 4914 if (connp->conn_anon_priv_bind) { 4915 port = udp_get_next_priv_port(udp); 4916 } else { 4917 port = udp_update_next_port(udp, 4918 us->us_next_port_to_try, B_TRUE); 4919 } 4920 } else { 4921 /* 4922 * If the port is in the well-known privileged range, 4923 * make sure the caller was privileged. 4924 */ 4925 int i; 4926 boolean_t priv = B_FALSE; 4927 4928 if (port < us->us_smallest_nonpriv_port) { 4929 priv = B_TRUE; 4930 } else { 4931 for (i = 0; i < us->us_num_epriv_ports; i++) { 4932 if (port == us->us_epriv_ports[i]) { 4933 priv = B_TRUE; 4934 break; 4935 } 4936 } 4937 } 4938 4939 if (priv) { 4940 if (secpolicy_net_privaddr(cr, port, IPPROTO_UDP) != 0) 4941 return (-TACCES); 4942 } 4943 } 4944 4945 if (port == 0) 4946 return (-TNOADDR); 4947 4948 /* 4949 * The state must be TS_UNBND. TPI mandates that users must send 4950 * TPI primitives only 1 at a time and wait for the response before 4951 * sending the next primitive. 4952 */ 4953 mutex_enter(&connp->conn_lock); 4954 if (udp->udp_state != TS_UNBND) { 4955 mutex_exit(&connp->conn_lock); 4956 (void) strlog(UDP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 4957 "udp_bind: bad state, %u", udp->udp_state); 4958 return (-TOUTSTATE); 4959 } 4960 /* 4961 * Copy the source address into our udp structure. This address 4962 * may still be zero; if so, IP will fill in the correct address 4963 * each time an outbound packet is passed to it. Since the udp is 4964 * not yet in the bind hash list, we don't grab the uf_lock to 4965 * change conn_ipversion 4966 */ 4967 if (connp->conn_family == AF_INET) { 4968 ASSERT(sin != NULL); 4969 ASSERT(connp->conn_ixa->ixa_flags & IXAF_IS_IPV4); 4970 } else { 4971 if (IN6_IS_ADDR_V4MAPPED(&v6src)) { 4972 /* 4973 * no need to hold the uf_lock to set the conn_ipversion 4974 * since we are not yet in the fanout list 4975 */ 4976 connp->conn_ipversion = IPV4_VERSION; 4977 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4; 4978 } else { 4979 connp->conn_ipversion = IPV6_VERSION; 4980 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4; 4981 } 4982 } 4983 4984 /* 4985 * If conn_reuseaddr is not set, then we have to make sure that 4986 * the IP address and port number the application requested 4987 * (or we selected for the application) is not being used by 4988 * another stream. If another stream is already using the 4989 * requested IP address and port, the behavior depends on 4990 * "bind_to_req_port_only". If set the bind fails; otherwise we 4991 * search for any unused port to bind to the stream. 4992 * 4993 * As per the BSD semantics, as modified by the Deering multicast 4994 * changes, if conn_reuseaddr is set, then we allow multiple binds 4995 * to the same port independent of the local IP address. 4996 * 4997 * This is slightly different than in SunOS 4.X which did not 4998 * support IP multicast. Note that the change implemented by the 4999 * Deering multicast code effects all binds - not only binding 5000 * to IP multicast addresses. 5001 * 5002 * Note that when binding to port zero we ignore SO_REUSEADDR in 5003 * order to guarantee a unique port. 5004 */ 5005 5006 count = 0; 5007 if (connp->conn_anon_priv_bind) { 5008 /* 5009 * loopmax = (IPPORT_RESERVED-1) - 5010 * us->us_min_anonpriv_port + 1 5011 */ 5012 loopmax = IPPORT_RESERVED - us->us_min_anonpriv_port; 5013 } else { 5014 loopmax = us->us_largest_anon_port - 5015 us->us_smallest_anon_port + 1; 5016 } 5017 5018 is_inaddr_any = V6_OR_V4_INADDR_ANY(v6src); 5019 5020 for (;;) { 5021 udp_t *udp1; 5022 boolean_t found_exclbind = B_FALSE; 5023 conn_t *connp1; 5024 5025 /* 5026 * Walk through the list of udp streams bound to 5027 * requested port with the same IP address. 5028 */ 5029 lport = htons(port); 5030 udpf = &us->us_bind_fanout[UDP_BIND_HASH(lport, 5031 us->us_bind_fanout_size)]; 5032 mutex_enter(&udpf->uf_lock); 5033 for (udp1 = udpf->uf_udp; udp1 != NULL; 5034 udp1 = udp1->udp_bind_hash) { 5035 connp1 = udp1->udp_connp; 5036 5037 if (lport != connp1->conn_lport) 5038 continue; 5039 5040 /* 5041 * On a labeled system, we must treat bindings to ports 5042 * on shared IP addresses by sockets with MAC exemption 5043 * privilege as being in all zones, as there's 5044 * otherwise no way to identify the right receiver. 5045 */ 5046 if (!IPCL_BIND_ZONE_MATCH(connp1, connp)) 5047 continue; 5048 5049 /* 5050 * If UDP_EXCLBIND is set for either the bound or 5051 * binding endpoint, the semantics of bind 5052 * is changed according to the following chart. 5053 * 5054 * spec = specified address (v4 or v6) 5055 * unspec = unspecified address (v4 or v6) 5056 * A = specified addresses are different for endpoints 5057 * 5058 * bound bind to allowed? 5059 * ------------------------------------- 5060 * unspec unspec no 5061 * unspec spec no 5062 * spec unspec no 5063 * spec spec yes if A 5064 * 5065 * For labeled systems, SO_MAC_EXEMPT behaves the same 5066 * as UDP_EXCLBIND, except that zoneid is ignored. 5067 */ 5068 if (connp1->conn_exclbind || connp->conn_exclbind || 5069 IPCL_CONNS_MAC(udp1->udp_connp, connp)) { 5070 if (V6_OR_V4_INADDR_ANY( 5071 connp1->conn_bound_addr_v6) || 5072 is_inaddr_any || 5073 IN6_ARE_ADDR_EQUAL( 5074 &connp1->conn_bound_addr_v6, 5075 &v6src)) { 5076 found_exclbind = B_TRUE; 5077 break; 5078 } 5079 continue; 5080 } 5081 5082 /* 5083 * Check ipversion to allow IPv4 and IPv6 sockets to 5084 * have disjoint port number spaces. 5085 */ 5086 if (connp->conn_ipversion != connp1->conn_ipversion) { 5087 5088 /* 5089 * On the first time through the loop, if the 5090 * the user intentionally specified a 5091 * particular port number, then ignore any 5092 * bindings of the other protocol that may 5093 * conflict. This allows the user to bind IPv6 5094 * alone and get both v4 and v6, or bind both 5095 * both and get each seperately. On subsequent 5096 * times through the loop, we're checking a 5097 * port that we chose (not the user) and thus 5098 * we do not allow casual duplicate bindings. 5099 */ 5100 if (count == 0 && requested_port != 0) 5101 continue; 5102 } 5103 5104 /* 5105 * No difference depending on SO_REUSEADDR. 5106 * 5107 * If existing port is bound to a 5108 * non-wildcard IP address and 5109 * the requesting stream is bound to 5110 * a distinct different IP addresses 5111 * (non-wildcard, also), keep going. 5112 */ 5113 if (!is_inaddr_any && 5114 !V6_OR_V4_INADDR_ANY(connp1->conn_bound_addr_v6) && 5115 !IN6_ARE_ADDR_EQUAL(&connp1->conn_laddr_v6, 5116 &v6src)) { 5117 continue; 5118 } 5119 break; 5120 } 5121 5122 if (!found_exclbind && 5123 (connp->conn_reuseaddr && requested_port != 0)) { 5124 break; 5125 } 5126 5127 if (udp1 == NULL) { 5128 /* 5129 * No other stream has this IP address 5130 * and port number. We can use it. 5131 */ 5132 break; 5133 } 5134 mutex_exit(&udpf->uf_lock); 5135 if (bind_to_req_port_only) { 5136 /* 5137 * We get here only when requested port 5138 * is bound (and only first of the for() 5139 * loop iteration). 5140 * 5141 * The semantics of this bind request 5142 * require it to fail so we return from 5143 * the routine (and exit the loop). 5144 * 5145 */ 5146 mutex_exit(&connp->conn_lock); 5147 return (-TADDRBUSY); 5148 } 5149 5150 if (connp->conn_anon_priv_bind) { 5151 port = udp_get_next_priv_port(udp); 5152 } else { 5153 if ((count == 0) && (requested_port != 0)) { 5154 /* 5155 * If the application wants us to find 5156 * a port, get one to start with. Set 5157 * requested_port to 0, so that we will 5158 * update us->us_next_port_to_try below. 5159 */ 5160 port = udp_update_next_port(udp, 5161 us->us_next_port_to_try, B_TRUE); 5162 requested_port = 0; 5163 } else { 5164 port = udp_update_next_port(udp, port + 1, 5165 B_FALSE); 5166 } 5167 } 5168 5169 if (port == 0 || ++count >= loopmax) { 5170 /* 5171 * We've tried every possible port number and 5172 * there are none available, so send an error 5173 * to the user. 5174 */ 5175 mutex_exit(&connp->conn_lock); 5176 return (-TNOADDR); 5177 } 5178 } 5179 5180 /* 5181 * Copy the source address into our udp structure. This address 5182 * may still be zero; if so, ip_attr_connect will fill in the correct 5183 * address when a packet is about to be sent. 5184 * If we are binding to a broadcast or multicast address then 5185 * we just set the conn_bound_addr since we don't want to use 5186 * that as the source address when sending. 5187 */ 5188 connp->conn_bound_addr_v6 = v6src; 5189 connp->conn_laddr_v6 = v6src; 5190 if (scopeid != 0) { 5191 connp->conn_ixa->ixa_flags |= IXAF_SCOPEID_SET; 5192 connp->conn_ixa->ixa_scopeid = scopeid; 5193 connp->conn_incoming_ifindex = scopeid; 5194 } else { 5195 connp->conn_ixa->ixa_flags &= ~IXAF_SCOPEID_SET; 5196 connp->conn_incoming_ifindex = connp->conn_bound_if; 5197 } 5198 5199 switch (laddr_type) { 5200 case IPVL_UNICAST_UP: 5201 case IPVL_UNICAST_DOWN: 5202 connp->conn_saddr_v6 = v6src; 5203 connp->conn_mcbc_bind = B_FALSE; 5204 break; 5205 case IPVL_MCAST: 5206 case IPVL_BCAST: 5207 /* ip_set_destination will pick a source address later */ 5208 connp->conn_saddr_v6 = ipv6_all_zeros; 5209 connp->conn_mcbc_bind = B_TRUE; 5210 break; 5211 } 5212 5213 /* Any errors after this point should use late_error */ 5214 connp->conn_lport = lport; 5215 5216 /* 5217 * Now reset the next anonymous port if the application requested 5218 * an anonymous port, or we handed out the next anonymous port. 5219 */ 5220 if ((requested_port == 0) && (!connp->conn_anon_priv_bind)) { 5221 us->us_next_port_to_try = port + 1; 5222 } 5223 5224 /* Initialize the T_BIND_ACK. */ 5225 if (connp->conn_family == AF_INET) { 5226 sin->sin_port = connp->conn_lport; 5227 } else { 5228 sin6->sin6_port = connp->conn_lport; 5229 } 5230 udp->udp_state = TS_IDLE; 5231 udp_bind_hash_insert(udpf, udp); 5232 mutex_exit(&udpf->uf_lock); 5233 mutex_exit(&connp->conn_lock); 5234 5235 if (cl_inet_bind) { 5236 /* 5237 * Running in cluster mode - register bind information 5238 */ 5239 if (connp->conn_ipversion == IPV4_VERSION) { 5240 (*cl_inet_bind)(connp->conn_netstack->netstack_stackid, 5241 IPPROTO_UDP, AF_INET, (uint8_t *)&v4src, 5242 (in_port_t)connp->conn_lport, NULL); 5243 } else { 5244 (*cl_inet_bind)(connp->conn_netstack->netstack_stackid, 5245 IPPROTO_UDP, AF_INET6, (uint8_t *)&v6src, 5246 (in_port_t)connp->conn_lport, NULL); 5247 } 5248 } 5249 5250 mutex_enter(&connp->conn_lock); 5251 connp->conn_anon_port = (is_system_labeled() && requested_port == 0); 5252 if (is_system_labeled() && (!connp->conn_anon_port || 5253 connp->conn_anon_mlp)) { 5254 uint16_t mlpport; 5255 zone_t *zone; 5256 5257 zone = crgetzone(cr); 5258 connp->conn_mlp_type = 5259 connp->conn_recv_ancillary.crb_recvucred ? mlptBoth : 5260 mlptSingle; 5261 addrtype = tsol_mlp_addr_type( 5262 connp->conn_allzones ? ALL_ZONES : zone->zone_id, 5263 IPV6_VERSION, &v6src, us->us_netstack->netstack_ip); 5264 if (addrtype == mlptSingle) { 5265 error = -TNOADDR; 5266 mutex_exit(&connp->conn_lock); 5267 goto late_error; 5268 } 5269 mlpport = connp->conn_anon_port ? PMAPPORT : port; 5270 mlptype = tsol_mlp_port_type(zone, IPPROTO_UDP, mlpport, 5271 addrtype); 5272 5273 /* 5274 * It is a coding error to attempt to bind an MLP port 5275 * without first setting SOL_SOCKET/SCM_UCRED. 5276 */ 5277 if (mlptype != mlptSingle && 5278 connp->conn_mlp_type == mlptSingle) { 5279 error = EINVAL; 5280 mutex_exit(&connp->conn_lock); 5281 goto late_error; 5282 } 5283 5284 /* 5285 * It is an access violation to attempt to bind an MLP port 5286 * without NET_BINDMLP privilege. 5287 */ 5288 if (mlptype != mlptSingle && 5289 secpolicy_net_bindmlp(cr) != 0) { 5290 if (connp->conn_debug) { 5291 (void) strlog(UDP_MOD_ID, 0, 1, 5292 SL_ERROR|SL_TRACE, 5293 "udp_bind: no priv for multilevel port %d", 5294 mlpport); 5295 } 5296 error = -TACCES; 5297 mutex_exit(&connp->conn_lock); 5298 goto late_error; 5299 } 5300 5301 /* 5302 * If we're specifically binding a shared IP address and the 5303 * port is MLP on shared addresses, then check to see if this 5304 * zone actually owns the MLP. Reject if not. 5305 */ 5306 if (mlptype == mlptShared && addrtype == mlptShared) { 5307 /* 5308 * No need to handle exclusive-stack zones since 5309 * ALL_ZONES only applies to the shared stack. 5310 */ 5311 zoneid_t mlpzone; 5312 5313 mlpzone = tsol_mlp_findzone(IPPROTO_UDP, 5314 htons(mlpport)); 5315 if (connp->conn_zoneid != mlpzone) { 5316 if (connp->conn_debug) { 5317 (void) strlog(UDP_MOD_ID, 0, 1, 5318 SL_ERROR|SL_TRACE, 5319 "udp_bind: attempt to bind port " 5320 "%d on shared addr in zone %d " 5321 "(should be %d)", 5322 mlpport, connp->conn_zoneid, 5323 mlpzone); 5324 } 5325 error = -TACCES; 5326 mutex_exit(&connp->conn_lock); 5327 goto late_error; 5328 } 5329 } 5330 if (connp->conn_anon_port) { 5331 error = tsol_mlp_anon(zone, mlptype, connp->conn_proto, 5332 port, B_TRUE); 5333 if (error != 0) { 5334 if (connp->conn_debug) { 5335 (void) strlog(UDP_MOD_ID, 0, 1, 5336 SL_ERROR|SL_TRACE, 5337 "udp_bind: cannot establish anon " 5338 "MLP for port %d", port); 5339 } 5340 error = -TACCES; 5341 mutex_exit(&connp->conn_lock); 5342 goto late_error; 5343 } 5344 } 5345 connp->conn_mlp_type = mlptype; 5346 } 5347 5348 /* 5349 * We create an initial header template here to make a subsequent 5350 * sendto have a starting point. Since conn_last_dst is zero the 5351 * first sendto will always follow the 'dst changed' code path. 5352 * Note that we defer massaging options and the related checksum 5353 * adjustment until we have a destination address. 5354 */ 5355 error = udp_build_hdr_template(connp, &connp->conn_saddr_v6, 5356 &connp->conn_faddr_v6, connp->conn_fport, connp->conn_flowinfo); 5357 if (error != 0) { 5358 mutex_exit(&connp->conn_lock); 5359 goto late_error; 5360 } 5361 /* Just in case */ 5362 connp->conn_faddr_v6 = ipv6_all_zeros; 5363 connp->conn_fport = 0; 5364 connp->conn_v6lastdst = ipv6_all_zeros; 5365 mutex_exit(&connp->conn_lock); 5366 5367 error = ip_laddr_fanout_insert(connp); 5368 if (error != 0) 5369 goto late_error; 5370 5371 /* Bind succeeded */ 5372 return (0); 5373 5374 late_error: 5375 /* We had already picked the port number, and then the bind failed */ 5376 mutex_enter(&connp->conn_lock); 5377 udpf = &us->us_bind_fanout[ 5378 UDP_BIND_HASH(connp->conn_lport, 5379 us->us_bind_fanout_size)]; 5380 mutex_enter(&udpf->uf_lock); 5381 connp->conn_saddr_v6 = ipv6_all_zeros; 5382 connp->conn_bound_addr_v6 = ipv6_all_zeros; 5383 connp->conn_laddr_v6 = ipv6_all_zeros; 5384 if (scopeid != 0) { 5385 connp->conn_ixa->ixa_flags &= ~IXAF_SCOPEID_SET; 5386 connp->conn_incoming_ifindex = connp->conn_bound_if; 5387 } 5388 udp->udp_state = TS_UNBND; 5389 udp_bind_hash_remove(udp, B_TRUE); 5390 connp->conn_lport = 0; 5391 mutex_exit(&udpf->uf_lock); 5392 connp->conn_anon_port = B_FALSE; 5393 connp->conn_mlp_type = mlptSingle; 5394 5395 connp->conn_v6lastdst = ipv6_all_zeros; 5396 5397 /* Restore the header that was built above - different source address */ 5398 (void) udp_build_hdr_template(connp, &connp->conn_saddr_v6, 5399 &connp->conn_faddr_v6, connp->conn_fport, connp->conn_flowinfo); 5400 mutex_exit(&connp->conn_lock); 5401 return (error); 5402 } 5403 5404 int 5405 udp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 5406 socklen_t len, cred_t *cr) 5407 { 5408 int error; 5409 conn_t *connp; 5410 5411 /* All Solaris components should pass a cred for this operation. */ 5412 ASSERT(cr != NULL); 5413 5414 connp = (conn_t *)proto_handle; 5415 5416 if (sa == NULL) 5417 error = udp_do_unbind(connp); 5418 else 5419 error = udp_do_bind(connp, sa, len, cr, B_TRUE); 5420 5421 if (error < 0) { 5422 if (error == -TOUTSTATE) 5423 error = EINVAL; 5424 else 5425 error = proto_tlitosyserr(-error); 5426 } 5427 5428 return (error); 5429 } 5430 5431 static int 5432 udp_implicit_bind(conn_t *connp, cred_t *cr) 5433 { 5434 sin6_t sin6addr; 5435 sin_t *sin; 5436 sin6_t *sin6; 5437 socklen_t len; 5438 int error; 5439 5440 /* All Solaris components should pass a cred for this operation. */ 5441 ASSERT(cr != NULL); 5442 5443 if (connp->conn_family == AF_INET) { 5444 len = sizeof (struct sockaddr_in); 5445 sin = (sin_t *)&sin6addr; 5446 *sin = sin_null; 5447 sin->sin_family = AF_INET; 5448 sin->sin_addr.s_addr = INADDR_ANY; 5449 } else { 5450 ASSERT(connp->conn_family == AF_INET6); 5451 len = sizeof (sin6_t); 5452 sin6 = (sin6_t *)&sin6addr; 5453 *sin6 = sin6_null; 5454 sin6->sin6_family = AF_INET6; 5455 V6_SET_ZERO(sin6->sin6_addr); 5456 } 5457 5458 error = udp_do_bind(connp, (struct sockaddr *)&sin6addr, len, 5459 cr, B_FALSE); 5460 return ((error < 0) ? proto_tlitosyserr(-error) : error); 5461 } 5462 5463 /* 5464 * This routine removes a port number association from a stream. It 5465 * is called by udp_unbind and udp_tpi_unbind. 5466 */ 5467 static int 5468 udp_do_unbind(conn_t *connp) 5469 { 5470 udp_t *udp = connp->conn_udp; 5471 udp_fanout_t *udpf; 5472 udp_stack_t *us = udp->udp_us; 5473 5474 if (cl_inet_unbind != NULL) { 5475 /* 5476 * Running in cluster mode - register unbind information 5477 */ 5478 if (connp->conn_ipversion == IPV4_VERSION) { 5479 (*cl_inet_unbind)( 5480 connp->conn_netstack->netstack_stackid, 5481 IPPROTO_UDP, AF_INET, 5482 (uint8_t *)(&V4_PART_OF_V6(connp->conn_laddr_v6)), 5483 (in_port_t)connp->conn_lport, NULL); 5484 } else { 5485 (*cl_inet_unbind)( 5486 connp->conn_netstack->netstack_stackid, 5487 IPPROTO_UDP, AF_INET6, 5488 (uint8_t *)&(connp->conn_laddr_v6), 5489 (in_port_t)connp->conn_lport, NULL); 5490 } 5491 } 5492 5493 mutex_enter(&connp->conn_lock); 5494 /* If a bind has not been done, we can't unbind. */ 5495 if (udp->udp_state == TS_UNBND) { 5496 mutex_exit(&connp->conn_lock); 5497 return (-TOUTSTATE); 5498 } 5499 udpf = &us->us_bind_fanout[UDP_BIND_HASH(connp->conn_lport, 5500 us->us_bind_fanout_size)]; 5501 mutex_enter(&udpf->uf_lock); 5502 udp_bind_hash_remove(udp, B_TRUE); 5503 connp->conn_saddr_v6 = ipv6_all_zeros; 5504 connp->conn_bound_addr_v6 = ipv6_all_zeros; 5505 connp->conn_laddr_v6 = ipv6_all_zeros; 5506 connp->conn_mcbc_bind = B_FALSE; 5507 connp->conn_lport = 0; 5508 /* In case we were also connected */ 5509 connp->conn_faddr_v6 = ipv6_all_zeros; 5510 connp->conn_fport = 0; 5511 mutex_exit(&udpf->uf_lock); 5512 5513 connp->conn_v6lastdst = ipv6_all_zeros; 5514 udp->udp_state = TS_UNBND; 5515 5516 (void) udp_build_hdr_template(connp, &connp->conn_saddr_v6, 5517 &connp->conn_faddr_v6, connp->conn_fport, connp->conn_flowinfo); 5518 mutex_exit(&connp->conn_lock); 5519 5520 ip_unbind(connp); 5521 5522 return (0); 5523 } 5524 5525 /* 5526 * It associates a default destination address with the stream. 5527 */ 5528 static int 5529 udp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 5530 cred_t *cr, pid_t pid) 5531 { 5532 sin6_t *sin6; 5533 sin_t *sin; 5534 in6_addr_t v6dst; 5535 ipaddr_t v4dst; 5536 uint16_t dstport; 5537 uint32_t flowinfo; 5538 udp_fanout_t *udpf; 5539 udp_t *udp, *udp1; 5540 ushort_t ipversion; 5541 udp_stack_t *us; 5542 int error; 5543 conn_t *connp1; 5544 ip_xmit_attr_t *ixa; 5545 ip_xmit_attr_t *oldixa; 5546 uint_t scopeid = 0; 5547 uint_t srcid = 0; 5548 in6_addr_t v6src = connp->conn_saddr_v6; 5549 boolean_t v4mapped; 5550 5551 udp = connp->conn_udp; 5552 us = udp->udp_us; 5553 5554 /* 5555 * Address has been verified by the caller 5556 */ 5557 switch (len) { 5558 default: 5559 /* 5560 * Should never happen 5561 */ 5562 return (EINVAL); 5563 5564 case sizeof (sin_t): 5565 sin = (sin_t *)sa; 5566 v4dst = sin->sin_addr.s_addr; 5567 dstport = sin->sin_port; 5568 IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst); 5569 ASSERT(connp->conn_ipversion == IPV4_VERSION); 5570 ipversion = IPV4_VERSION; 5571 break; 5572 5573 case sizeof (sin6_t): 5574 sin6 = (sin6_t *)sa; 5575 v6dst = sin6->sin6_addr; 5576 dstport = sin6->sin6_port; 5577 srcid = sin6->__sin6_src_id; 5578 v4mapped = IN6_IS_ADDR_V4MAPPED(&v6dst); 5579 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 5580 if (!ip_srcid_find_id(srcid, &v6src, IPCL_ZONEID(connp), 5581 v4mapped, connp->conn_netstack)) { 5582 /* Mismatch v4mapped/v6 specified by srcid. */ 5583 return (EADDRNOTAVAIL); 5584 } 5585 } 5586 if (v4mapped) { 5587 if (connp->conn_ipv6_v6only) 5588 return (EADDRNOTAVAIL); 5589 5590 /* 5591 * Destination adress is mapped IPv6 address. 5592 * Source bound address should be unspecified or 5593 * IPv6 mapped address as well. 5594 */ 5595 if (!IN6_IS_ADDR_UNSPECIFIED( 5596 &connp->conn_bound_addr_v6) && 5597 !IN6_IS_ADDR_V4MAPPED(&connp->conn_bound_addr_v6)) { 5598 return (EADDRNOTAVAIL); 5599 } 5600 IN6_V4MAPPED_TO_IPADDR(&v6dst, v4dst); 5601 ipversion = IPV4_VERSION; 5602 flowinfo = 0; 5603 } else { 5604 ipversion = IPV6_VERSION; 5605 flowinfo = sin6->sin6_flowinfo; 5606 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 5607 scopeid = sin6->sin6_scope_id; 5608 } 5609 break; 5610 } 5611 5612 if (dstport == 0) 5613 return (-TBADADDR); 5614 5615 /* 5616 * If there is a different thread using conn_ixa then we get a new 5617 * copy and cut the old one loose from conn_ixa. Otherwise we use 5618 * conn_ixa and prevent any other thread from using/changing it. 5619 * Once connect() is done other threads can use conn_ixa since the 5620 * refcnt will be back at one. 5621 * We defer updating conn_ixa until later to handle any concurrent 5622 * conn_ixa_cleanup thread. 5623 */ 5624 ixa = conn_get_ixa(connp, B_FALSE); 5625 if (ixa == NULL) 5626 return (ENOMEM); 5627 5628 mutex_enter(&connp->conn_lock); 5629 /* 5630 * This udp_t must have bound to a port already before doing a connect. 5631 * Reject if a connect is in progress (we drop conn_lock during 5632 * udp_do_connect). 5633 */ 5634 if (udp->udp_state == TS_UNBND || udp->udp_state == TS_WCON_CREQ) { 5635 mutex_exit(&connp->conn_lock); 5636 (void) strlog(UDP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 5637 "udp_connect: bad state, %u", udp->udp_state); 5638 ixa_refrele(ixa); 5639 return (-TOUTSTATE); 5640 } 5641 ASSERT(connp->conn_lport != 0 && udp->udp_ptpbhn != NULL); 5642 5643 udpf = &us->us_bind_fanout[UDP_BIND_HASH(connp->conn_lport, 5644 us->us_bind_fanout_size)]; 5645 5646 mutex_enter(&udpf->uf_lock); 5647 if (udp->udp_state == TS_DATA_XFER) { 5648 /* Already connected - clear out state */ 5649 if (connp->conn_mcbc_bind) 5650 connp->conn_saddr_v6 = ipv6_all_zeros; 5651 else 5652 connp->conn_saddr_v6 = connp->conn_bound_addr_v6; 5653 connp->conn_laddr_v6 = connp->conn_bound_addr_v6; 5654 connp->conn_faddr_v6 = ipv6_all_zeros; 5655 connp->conn_fport = 0; 5656 udp->udp_state = TS_IDLE; 5657 } 5658 5659 connp->conn_fport = dstport; 5660 connp->conn_ipversion = ipversion; 5661 if (ipversion == IPV4_VERSION) { 5662 /* 5663 * Interpret a zero destination to mean loopback. 5664 * Update the T_CONN_REQ (sin/sin6) since it is used to 5665 * generate the T_CONN_CON. 5666 */ 5667 if (v4dst == INADDR_ANY) { 5668 v4dst = htonl(INADDR_LOOPBACK); 5669 IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst); 5670 if (connp->conn_family == AF_INET) { 5671 sin->sin_addr.s_addr = v4dst; 5672 } else { 5673 sin6->sin6_addr = v6dst; 5674 } 5675 } 5676 connp->conn_faddr_v6 = v6dst; 5677 connp->conn_flowinfo = 0; 5678 } else { 5679 ASSERT(connp->conn_ipversion == IPV6_VERSION); 5680 /* 5681 * Interpret a zero destination to mean loopback. 5682 * Update the T_CONN_REQ (sin/sin6) since it is used to 5683 * generate the T_CONN_CON. 5684 */ 5685 if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) { 5686 v6dst = ipv6_loopback; 5687 sin6->sin6_addr = v6dst; 5688 } 5689 connp->conn_faddr_v6 = v6dst; 5690 connp->conn_flowinfo = flowinfo; 5691 } 5692 mutex_exit(&udpf->uf_lock); 5693 5694 /* 5695 * We update our cred/cpid based on the caller of connect 5696 */ 5697 if (connp->conn_cred != cr) { 5698 crhold(cr); 5699 crfree(connp->conn_cred); 5700 connp->conn_cred = cr; 5701 } 5702 connp->conn_cpid = pid; 5703 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED)); 5704 ixa->ixa_cred = cr; 5705 ixa->ixa_cpid = pid; 5706 if (is_system_labeled()) { 5707 /* We need to restart with a label based on the cred */ 5708 ip_xmit_attr_restore_tsl(ixa, ixa->ixa_cred); 5709 } 5710 5711 if (scopeid != 0) { 5712 ixa->ixa_flags |= IXAF_SCOPEID_SET; 5713 ixa->ixa_scopeid = scopeid; 5714 connp->conn_incoming_ifindex = scopeid; 5715 } else { 5716 ixa->ixa_flags &= ~IXAF_SCOPEID_SET; 5717 connp->conn_incoming_ifindex = connp->conn_bound_if; 5718 } 5719 /* 5720 * conn_connect will drop conn_lock and reacquire it. 5721 * To prevent a send* from messing with this udp_t while the lock 5722 * is dropped we set udp_state and clear conn_v6lastdst. 5723 * That will make all send* fail with EISCONN. 5724 */ 5725 connp->conn_v6lastdst = ipv6_all_zeros; 5726 udp->udp_state = TS_WCON_CREQ; 5727 5728 error = conn_connect(connp, NULL, IPDF_ALLOW_MCBC); 5729 mutex_exit(&connp->conn_lock); 5730 if (error != 0) 5731 goto connect_failed; 5732 5733 /* 5734 * The addresses have been verified. Time to insert in 5735 * the correct fanout list. 5736 */ 5737 error = ipcl_conn_insert(connp); 5738 if (error != 0) 5739 goto connect_failed; 5740 5741 mutex_enter(&connp->conn_lock); 5742 error = udp_build_hdr_template(connp, &connp->conn_saddr_v6, 5743 &connp->conn_faddr_v6, connp->conn_fport, connp->conn_flowinfo); 5744 if (error != 0) { 5745 mutex_exit(&connp->conn_lock); 5746 goto connect_failed; 5747 } 5748 5749 udp->udp_state = TS_DATA_XFER; 5750 /* Record this as the "last" send even though we haven't sent any */ 5751 connp->conn_v6lastdst = connp->conn_faddr_v6; 5752 connp->conn_lastipversion = connp->conn_ipversion; 5753 connp->conn_lastdstport = connp->conn_fport; 5754 connp->conn_lastflowinfo = connp->conn_flowinfo; 5755 connp->conn_lastscopeid = scopeid; 5756 connp->conn_lastsrcid = srcid; 5757 /* Also remember a source to use together with lastdst */ 5758 connp->conn_v6lastsrc = v6src; 5759 5760 oldixa = conn_replace_ixa(connp, ixa); 5761 mutex_exit(&connp->conn_lock); 5762 ixa_refrele(oldixa); 5763 5764 /* 5765 * We've picked a source address above. Now we can 5766 * verify that the src/port/dst/port is unique for all 5767 * connections in TS_DATA_XFER, skipping ourselves. 5768 */ 5769 mutex_enter(&udpf->uf_lock); 5770 for (udp1 = udpf->uf_udp; udp1 != NULL; udp1 = udp1->udp_bind_hash) { 5771 if (udp1->udp_state != TS_DATA_XFER) 5772 continue; 5773 5774 if (udp1 == udp) 5775 continue; 5776 5777 connp1 = udp1->udp_connp; 5778 if (connp->conn_lport != connp1->conn_lport || 5779 connp->conn_ipversion != connp1->conn_ipversion || 5780 dstport != connp1->conn_fport || 5781 !IN6_ARE_ADDR_EQUAL(&connp->conn_laddr_v6, 5782 &connp1->conn_laddr_v6) || 5783 !IN6_ARE_ADDR_EQUAL(&v6dst, &connp1->conn_faddr_v6) || 5784 !(IPCL_ZONE_MATCH(connp, connp1->conn_zoneid) || 5785 IPCL_ZONE_MATCH(connp1, connp->conn_zoneid))) 5786 continue; 5787 mutex_exit(&udpf->uf_lock); 5788 error = -TBADADDR; 5789 goto connect_failed; 5790 } 5791 if (cl_inet_connect2 != NULL) { 5792 CL_INET_UDP_CONNECT(connp, B_TRUE, &v6dst, dstport, error); 5793 if (error != 0) { 5794 mutex_exit(&udpf->uf_lock); 5795 error = -TBADADDR; 5796 goto connect_failed; 5797 } 5798 } 5799 mutex_exit(&udpf->uf_lock); 5800 5801 ixa_refrele(ixa); 5802 return (0); 5803 5804 connect_failed: 5805 if (ixa != NULL) 5806 ixa_refrele(ixa); 5807 mutex_enter(&connp->conn_lock); 5808 mutex_enter(&udpf->uf_lock); 5809 udp->udp_state = TS_IDLE; 5810 connp->conn_faddr_v6 = ipv6_all_zeros; 5811 connp->conn_fport = 0; 5812 /* In case the source address was set above */ 5813 if (connp->conn_mcbc_bind) 5814 connp->conn_saddr_v6 = ipv6_all_zeros; 5815 else 5816 connp->conn_saddr_v6 = connp->conn_bound_addr_v6; 5817 connp->conn_laddr_v6 = connp->conn_bound_addr_v6; 5818 mutex_exit(&udpf->uf_lock); 5819 5820 connp->conn_v6lastdst = ipv6_all_zeros; 5821 connp->conn_flowinfo = 0; 5822 5823 (void) udp_build_hdr_template(connp, &connp->conn_saddr_v6, 5824 &connp->conn_faddr_v6, connp->conn_fport, connp->conn_flowinfo); 5825 mutex_exit(&connp->conn_lock); 5826 return (error); 5827 } 5828 5829 static int 5830 udp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 5831 socklen_t len, sock_connid_t *id, cred_t *cr) 5832 { 5833 conn_t *connp = (conn_t *)proto_handle; 5834 udp_t *udp = connp->conn_udp; 5835 int error; 5836 boolean_t did_bind = B_FALSE; 5837 pid_t pid = curproc->p_pid; 5838 5839 /* All Solaris components should pass a cred for this operation. */ 5840 ASSERT(cr != NULL); 5841 5842 if (sa == NULL) { 5843 /* 5844 * Disconnect 5845 * Make sure we are connected 5846 */ 5847 if (udp->udp_state != TS_DATA_XFER) 5848 return (EINVAL); 5849 5850 error = udp_disconnect(connp); 5851 return (error); 5852 } 5853 5854 error = proto_verify_ip_addr(connp->conn_family, sa, len); 5855 if (error != 0) 5856 goto done; 5857 5858 /* do an implicit bind if necessary */ 5859 if (udp->udp_state == TS_UNBND) { 5860 error = udp_implicit_bind(connp, cr); 5861 /* 5862 * We could be racing with an actual bind, in which case 5863 * we would see EPROTO. We cross our fingers and try 5864 * to connect. 5865 */ 5866 if (!(error == 0 || error == EPROTO)) 5867 goto done; 5868 did_bind = B_TRUE; 5869 } 5870 /* 5871 * set SO_DGRAM_ERRIND 5872 */ 5873 connp->conn_dgram_errind = B_TRUE; 5874 5875 error = udp_do_connect(connp, sa, len, cr, pid); 5876 5877 if (error != 0 && did_bind) { 5878 int unbind_err; 5879 5880 unbind_err = udp_do_unbind(connp); 5881 ASSERT(unbind_err == 0); 5882 } 5883 5884 if (error == 0) { 5885 *id = 0; 5886 (*connp->conn_upcalls->su_connected) 5887 (connp->conn_upper_handle, 0, NULL, -1); 5888 } else if (error < 0) { 5889 error = proto_tlitosyserr(-error); 5890 } 5891 5892 done: 5893 if (error != 0 && udp->udp_state == TS_DATA_XFER) { 5894 /* 5895 * No need to hold locks to set state 5896 * after connect failure socket state is undefined 5897 * We set the state only to imitate old sockfs behavior 5898 */ 5899 udp->udp_state = TS_IDLE; 5900 } 5901 return (error); 5902 } 5903 5904 int 5905 udp_send(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 5906 cred_t *cr) 5907 { 5908 sin6_t *sin6; 5909 sin_t *sin = NULL; 5910 uint_t srcid; 5911 conn_t *connp = (conn_t *)proto_handle; 5912 udp_t *udp = connp->conn_udp; 5913 int error = 0; 5914 udp_stack_t *us = udp->udp_us; 5915 ushort_t ipversion; 5916 pid_t pid = curproc->p_pid; 5917 ip_xmit_attr_t *ixa; 5918 5919 ASSERT(DB_TYPE(mp) == M_DATA); 5920 5921 /* All Solaris components should pass a cred for this operation. */ 5922 ASSERT(cr != NULL); 5923 5924 /* do an implicit bind if necessary */ 5925 if (udp->udp_state == TS_UNBND) { 5926 error = udp_implicit_bind(connp, cr); 5927 /* 5928 * We could be racing with an actual bind, in which case 5929 * we would see EPROTO. We cross our fingers and try 5930 * to connect. 5931 */ 5932 if (!(error == 0 || error == EPROTO)) { 5933 freemsg(mp); 5934 return (error); 5935 } 5936 } 5937 5938 /* Connected? */ 5939 if (msg->msg_name == NULL) { 5940 if (udp->udp_state != TS_DATA_XFER) { 5941 UDPS_BUMP_MIB(us, udpOutErrors); 5942 return (EDESTADDRREQ); 5943 } 5944 if (msg->msg_controllen != 0) { 5945 error = udp_output_ancillary(connp, NULL, NULL, mp, 5946 NULL, msg, cr, pid); 5947 } else { 5948 error = udp_output_connected(connp, mp, cr, pid); 5949 } 5950 if (us->us_sendto_ignerr) 5951 return (0); 5952 else 5953 return (error); 5954 } 5955 if (udp->udp_state == TS_DATA_XFER) { 5956 UDPS_BUMP_MIB(us, udpOutErrors); 5957 return (EISCONN); 5958 } 5959 error = proto_verify_ip_addr(connp->conn_family, 5960 (struct sockaddr *)msg->msg_name, msg->msg_namelen); 5961 if (error != 0) { 5962 UDPS_BUMP_MIB(us, udpOutErrors); 5963 return (error); 5964 } 5965 switch (connp->conn_family) { 5966 case AF_INET6: 5967 sin6 = (sin6_t *)msg->msg_name; 5968 5969 srcid = sin6->__sin6_src_id; 5970 5971 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 5972 /* 5973 * Destination is a non-IPv4-compatible IPv6 address. 5974 * Send out an IPv6 format packet. 5975 */ 5976 5977 /* 5978 * If the local address is a mapped address return 5979 * an error. 5980 * It would be possible to send an IPv6 packet but the 5981 * response would never make it back to the application 5982 * since it is bound to a mapped address. 5983 */ 5984 if (IN6_IS_ADDR_V4MAPPED(&connp->conn_saddr_v6)) { 5985 UDPS_BUMP_MIB(us, udpOutErrors); 5986 return (EADDRNOTAVAIL); 5987 } 5988 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 5989 sin6->sin6_addr = ipv6_loopback; 5990 ipversion = IPV6_VERSION; 5991 } else { 5992 if (connp->conn_ipv6_v6only) { 5993 UDPS_BUMP_MIB(us, udpOutErrors); 5994 return (EADDRNOTAVAIL); 5995 } 5996 5997 /* 5998 * If the local address is not zero or a mapped address 5999 * return an error. It would be possible to send an 6000 * IPv4 packet but the response would never make it 6001 * back to the application since it is bound to a 6002 * non-mapped address. 6003 */ 6004 if (!IN6_IS_ADDR_V4MAPPED(&connp->conn_saddr_v6) && 6005 !IN6_IS_ADDR_UNSPECIFIED(&connp->conn_saddr_v6)) { 6006 UDPS_BUMP_MIB(us, udpOutErrors); 6007 return (EADDRNOTAVAIL); 6008 } 6009 6010 if (V4_PART_OF_V6(sin6->sin6_addr) == INADDR_ANY) { 6011 V4_PART_OF_V6(sin6->sin6_addr) = 6012 htonl(INADDR_LOOPBACK); 6013 } 6014 ipversion = IPV4_VERSION; 6015 } 6016 6017 /* 6018 * We have to allocate an ip_xmit_attr_t before we grab 6019 * conn_lock and we need to hold conn_lock once we've check 6020 * conn_same_as_last_v6 to handle concurrent send* calls on a 6021 * socket. 6022 */ 6023 if (msg->msg_controllen == 0) { 6024 ixa = conn_get_ixa(connp, B_FALSE); 6025 if (ixa == NULL) { 6026 UDPS_BUMP_MIB(us, udpOutErrors); 6027 return (ENOMEM); 6028 } 6029 } else { 6030 ixa = NULL; 6031 } 6032 mutex_enter(&connp->conn_lock); 6033 if (udp->udp_delayed_error != 0) { 6034 sin6_t *sin2 = (sin6_t *)&udp->udp_delayed_addr; 6035 6036 error = udp->udp_delayed_error; 6037 udp->udp_delayed_error = 0; 6038 6039 /* Compare IP address, port, and family */ 6040 6041 if (sin6->sin6_port == sin2->sin6_port && 6042 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 6043 &sin2->sin6_addr) && 6044 sin6->sin6_family == sin2->sin6_family) { 6045 mutex_exit(&connp->conn_lock); 6046 UDPS_BUMP_MIB(us, udpOutErrors); 6047 if (ixa != NULL) 6048 ixa_refrele(ixa); 6049 return (error); 6050 } 6051 } 6052 6053 if (msg->msg_controllen != 0) { 6054 mutex_exit(&connp->conn_lock); 6055 ASSERT(ixa == NULL); 6056 error = udp_output_ancillary(connp, NULL, sin6, mp, 6057 NULL, msg, cr, pid); 6058 } else if (conn_same_as_last_v6(connp, sin6) && 6059 connp->conn_lastsrcid == srcid && 6060 ipsec_outbound_policy_current(ixa)) { 6061 /* udp_output_lastdst drops conn_lock */ 6062 error = udp_output_lastdst(connp, mp, cr, pid, ixa); 6063 } else { 6064 /* udp_output_newdst drops conn_lock */ 6065 error = udp_output_newdst(connp, mp, NULL, sin6, 6066 ipversion, cr, pid, ixa); 6067 } 6068 ASSERT(MUTEX_NOT_HELD(&connp->conn_lock)); 6069 if (us->us_sendto_ignerr) 6070 return (0); 6071 else 6072 return (error); 6073 case AF_INET: 6074 sin = (sin_t *)msg->msg_name; 6075 6076 ipversion = IPV4_VERSION; 6077 6078 if (sin->sin_addr.s_addr == INADDR_ANY) 6079 sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 6080 6081 /* 6082 * We have to allocate an ip_xmit_attr_t before we grab 6083 * conn_lock and we need to hold conn_lock once we've check 6084 * conn_same_as_last_v6 to handle concurrent send* on a socket. 6085 */ 6086 if (msg->msg_controllen == 0) { 6087 ixa = conn_get_ixa(connp, B_FALSE); 6088 if (ixa == NULL) { 6089 UDPS_BUMP_MIB(us, udpOutErrors); 6090 return (ENOMEM); 6091 } 6092 } else { 6093 ixa = NULL; 6094 } 6095 mutex_enter(&connp->conn_lock); 6096 if (udp->udp_delayed_error != 0) { 6097 sin_t *sin2 = (sin_t *)&udp->udp_delayed_addr; 6098 6099 error = udp->udp_delayed_error; 6100 udp->udp_delayed_error = 0; 6101 6102 /* Compare IP address and port */ 6103 6104 if (sin->sin_port == sin2->sin_port && 6105 sin->sin_addr.s_addr == sin2->sin_addr.s_addr) { 6106 mutex_exit(&connp->conn_lock); 6107 UDPS_BUMP_MIB(us, udpOutErrors); 6108 if (ixa != NULL) 6109 ixa_refrele(ixa); 6110 return (error); 6111 } 6112 } 6113 if (msg->msg_controllen != 0) { 6114 mutex_exit(&connp->conn_lock); 6115 ASSERT(ixa == NULL); 6116 error = udp_output_ancillary(connp, sin, NULL, mp, 6117 NULL, msg, cr, pid); 6118 } else if (conn_same_as_last_v4(connp, sin) && 6119 ipsec_outbound_policy_current(ixa)) { 6120 /* udp_output_lastdst drops conn_lock */ 6121 error = udp_output_lastdst(connp, mp, cr, pid, ixa); 6122 } else { 6123 /* udp_output_newdst drops conn_lock */ 6124 error = udp_output_newdst(connp, mp, sin, NULL, 6125 ipversion, cr, pid, ixa); 6126 } 6127 ASSERT(MUTEX_NOT_HELD(&connp->conn_lock)); 6128 if (us->us_sendto_ignerr) 6129 return (0); 6130 else 6131 return (error); 6132 default: 6133 return (EINVAL); 6134 } 6135 } 6136 6137 int 6138 udp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 6139 boolean_t issocket, so_proto_quiesced_cb_t quiesced_cb, 6140 sock_quiesce_arg_t *arg) 6141 { 6142 conn_t *connp = (conn_t *)proto_handle; 6143 udp_t *udp; 6144 struct T_capability_ack tca; 6145 struct sockaddr_in6 laddr, faddr; 6146 socklen_t laddrlen, faddrlen; 6147 short opts; 6148 struct stroptions *stropt; 6149 mblk_t *mp, *stropt_mp; 6150 int error; 6151 6152 udp = connp->conn_udp; 6153 6154 stropt_mp = allocb_wait(sizeof (*stropt), BPRI_HI, STR_NOSIG, NULL); 6155 6156 /* 6157 * setup the fallback stream that was allocated 6158 */ 6159 connp->conn_dev = (dev_t)RD(q)->q_ptr; 6160 connp->conn_minor_arena = WR(q)->q_ptr; 6161 6162 RD(q)->q_ptr = WR(q)->q_ptr = connp; 6163 6164 WR(q)->q_qinfo = &udp_winit; 6165 6166 connp->conn_rq = RD(q); 6167 connp->conn_wq = WR(q); 6168 6169 /* Notify stream head about options before sending up data */ 6170 stropt_mp->b_datap->db_type = M_SETOPTS; 6171 stropt_mp->b_wptr += sizeof (*stropt); 6172 stropt = (struct stroptions *)stropt_mp->b_rptr; 6173 stropt->so_flags = SO_WROFF | SO_HIWAT; 6174 stropt->so_wroff = connp->conn_wroff; 6175 stropt->so_hiwat = udp->udp_rcv_disply_hiwat; 6176 putnext(RD(q), stropt_mp); 6177 6178 /* 6179 * Free the helper stream 6180 */ 6181 ip_free_helper_stream(connp); 6182 6183 if (!issocket) 6184 udp_use_pure_tpi(udp); 6185 6186 /* 6187 * Collect the information needed to sync with the sonode 6188 */ 6189 udp_do_capability_ack(udp, &tca, TC1_INFO); 6190 6191 laddrlen = faddrlen = sizeof (sin6_t); 6192 (void) udp_getsockname((sock_lower_handle_t)connp, 6193 (struct sockaddr *)&laddr, &laddrlen, CRED()); 6194 error = udp_getpeername((sock_lower_handle_t)connp, 6195 (struct sockaddr *)&faddr, &faddrlen, CRED()); 6196 if (error != 0) 6197 faddrlen = 0; 6198 6199 opts = 0; 6200 if (connp->conn_dgram_errind) 6201 opts |= SO_DGRAM_ERRIND; 6202 if (connp->conn_ixa->ixa_flags & IXAF_DONTROUTE) 6203 opts |= SO_DONTROUTE; 6204 6205 mp = (*quiesced_cb)(connp->conn_upper_handle, arg, &tca, 6206 (struct sockaddr *)&laddr, laddrlen, 6207 (struct sockaddr *)&faddr, faddrlen, opts); 6208 6209 mutex_enter(&udp->udp_recv_lock); 6210 /* 6211 * Attempts to send data up during fallback will result in it being 6212 * queued in udp_t. First push up the datagrams obtained from the 6213 * socket, then any packets queued in udp_t. 6214 */ 6215 if (mp != NULL) { 6216 mp->b_next = udp->udp_fallback_queue_head; 6217 udp->udp_fallback_queue_head = mp; 6218 } 6219 while (udp->udp_fallback_queue_head != NULL) { 6220 mp = udp->udp_fallback_queue_head; 6221 udp->udp_fallback_queue_head = mp->b_next; 6222 mutex_exit(&udp->udp_recv_lock); 6223 mp->b_next = NULL; 6224 putnext(RD(q), mp); 6225 mutex_enter(&udp->udp_recv_lock); 6226 } 6227 udp->udp_fallback_queue_tail = udp->udp_fallback_queue_head; 6228 /* 6229 * No longer a streams less socket 6230 */ 6231 mutex_enter(&connp->conn_lock); 6232 connp->conn_flags &= ~IPCL_NONSTR; 6233 mutex_exit(&connp->conn_lock); 6234 6235 mutex_exit(&udp->udp_recv_lock); 6236 6237 ASSERT(connp->conn_ref >= 1); 6238 6239 return (0); 6240 } 6241 6242 /* ARGSUSED3 */ 6243 int 6244 udp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *sa, 6245 socklen_t *salenp, cred_t *cr) 6246 { 6247 conn_t *connp = (conn_t *)proto_handle; 6248 udp_t *udp = connp->conn_udp; 6249 int error; 6250 6251 /* All Solaris components should pass a cred for this operation. */ 6252 ASSERT(cr != NULL); 6253 6254 mutex_enter(&connp->conn_lock); 6255 if (udp->udp_state != TS_DATA_XFER) 6256 error = ENOTCONN; 6257 else 6258 error = conn_getpeername(connp, sa, salenp); 6259 mutex_exit(&connp->conn_lock); 6260 return (error); 6261 } 6262 6263 /* ARGSUSED3 */ 6264 int 6265 udp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *sa, 6266 socklen_t *salenp, cred_t *cr) 6267 { 6268 conn_t *connp = (conn_t *)proto_handle; 6269 int error; 6270 6271 /* All Solaris components should pass a cred for this operation. */ 6272 ASSERT(cr != NULL); 6273 6274 mutex_enter(&connp->conn_lock); 6275 error = conn_getsockname(connp, sa, salenp); 6276 mutex_exit(&connp->conn_lock); 6277 return (error); 6278 } 6279 6280 int 6281 udp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 6282 void *optvalp, socklen_t *optlen, cred_t *cr) 6283 { 6284 conn_t *connp = (conn_t *)proto_handle; 6285 int error; 6286 t_uscalar_t max_optbuf_len; 6287 void *optvalp_buf; 6288 int len; 6289 6290 /* All Solaris components should pass a cred for this operation. */ 6291 ASSERT(cr != NULL); 6292 6293 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 6294 udp_opt_obj.odb_opt_des_arr, 6295 udp_opt_obj.odb_opt_arr_cnt, 6296 B_FALSE, B_TRUE, cr); 6297 if (error != 0) { 6298 if (error < 0) 6299 error = proto_tlitosyserr(-error); 6300 return (error); 6301 } 6302 6303 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 6304 len = udp_opt_get(connp, level, option_name, optvalp_buf); 6305 if (len == -1) { 6306 kmem_free(optvalp_buf, max_optbuf_len); 6307 return (EINVAL); 6308 } 6309 6310 /* 6311 * update optlen and copy option value 6312 */ 6313 t_uscalar_t size = MIN(len, *optlen); 6314 6315 bcopy(optvalp_buf, optvalp, size); 6316 bcopy(&size, optlen, sizeof (size)); 6317 6318 kmem_free(optvalp_buf, max_optbuf_len); 6319 return (0); 6320 } 6321 6322 int 6323 udp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 6324 const void *optvalp, socklen_t optlen, cred_t *cr) 6325 { 6326 conn_t *connp = (conn_t *)proto_handle; 6327 int error; 6328 6329 /* All Solaris components should pass a cred for this operation. */ 6330 ASSERT(cr != NULL); 6331 6332 error = proto_opt_check(level, option_name, optlen, NULL, 6333 udp_opt_obj.odb_opt_des_arr, 6334 udp_opt_obj.odb_opt_arr_cnt, 6335 B_TRUE, B_FALSE, cr); 6336 6337 if (error != 0) { 6338 if (error < 0) 6339 error = proto_tlitosyserr(-error); 6340 return (error); 6341 } 6342 6343 error = udp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 6344 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 6345 NULL, cr); 6346 6347 ASSERT(error >= 0); 6348 6349 return (error); 6350 } 6351 6352 void 6353 udp_clr_flowctrl(sock_lower_handle_t proto_handle) 6354 { 6355 conn_t *connp = (conn_t *)proto_handle; 6356 udp_t *udp = connp->conn_udp; 6357 6358 mutex_enter(&udp->udp_recv_lock); 6359 connp->conn_flow_cntrld = B_FALSE; 6360 mutex_exit(&udp->udp_recv_lock); 6361 } 6362 6363 /* ARGSUSED2 */ 6364 int 6365 udp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 6366 { 6367 conn_t *connp = (conn_t *)proto_handle; 6368 6369 /* All Solaris components should pass a cred for this operation. */ 6370 ASSERT(cr != NULL); 6371 6372 /* shut down the send side */ 6373 if (how != SHUT_RD) 6374 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 6375 SOCK_OPCTL_SHUT_SEND, 0); 6376 /* shut down the recv side */ 6377 if (how != SHUT_WR) 6378 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 6379 SOCK_OPCTL_SHUT_RECV, 0); 6380 return (0); 6381 } 6382 6383 int 6384 udp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 6385 int mode, int32_t *rvalp, cred_t *cr) 6386 { 6387 conn_t *connp = (conn_t *)proto_handle; 6388 int error; 6389 6390 /* All Solaris components should pass a cred for this operation. */ 6391 ASSERT(cr != NULL); 6392 6393 /* 6394 * If we don't have a helper stream then create one. 6395 * ip_create_helper_stream takes care of locking the conn_t, 6396 * so this check for NULL is just a performance optimization. 6397 */ 6398 if (connp->conn_helper_info == NULL) { 6399 udp_stack_t *us = connp->conn_udp->udp_us; 6400 6401 ASSERT(us->us_ldi_ident != NULL); 6402 6403 /* 6404 * Create a helper stream for non-STREAMS socket. 6405 */ 6406 error = ip_create_helper_stream(connp, us->us_ldi_ident); 6407 if (error != 0) { 6408 ip0dbg(("tcp_ioctl: create of IP helper stream " 6409 "failed %d\n", error)); 6410 return (error); 6411 } 6412 } 6413 6414 switch (cmd) { 6415 case _SIOCSOCKFALLBACK: 6416 case TI_GETPEERNAME: 6417 case TI_GETMYNAME: 6418 ip1dbg(("udp_ioctl: cmd 0x%x on non streams socket", 6419 cmd)); 6420 error = EINVAL; 6421 break; 6422 default: 6423 /* 6424 * Pass on to IP using helper stream 6425 */ 6426 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 6427 cmd, arg, mode, cr, rvalp); 6428 break; 6429 } 6430 return (error); 6431 } 6432 6433 /* ARGSUSED */ 6434 int 6435 udp_accept(sock_lower_handle_t lproto_handle, 6436 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 6437 cred_t *cr) 6438 { 6439 return (EOPNOTSUPP); 6440 } 6441 6442 /* ARGSUSED */ 6443 int 6444 udp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 6445 { 6446 return (EOPNOTSUPP); 6447 } 6448 6449 sock_downcalls_t sock_udp_downcalls = { 6450 udp_activate, /* sd_activate */ 6451 udp_accept, /* sd_accept */ 6452 udp_bind, /* sd_bind */ 6453 udp_listen, /* sd_listen */ 6454 udp_connect, /* sd_connect */ 6455 udp_getpeername, /* sd_getpeername */ 6456 udp_getsockname, /* sd_getsockname */ 6457 udp_getsockopt, /* sd_getsockopt */ 6458 udp_setsockopt, /* sd_setsockopt */ 6459 udp_send, /* sd_send */ 6460 NULL, /* sd_send_uio */ 6461 NULL, /* sd_recv_uio */ 6462 NULL, /* sd_poll */ 6463 udp_shutdown, /* sd_shutdown */ 6464 udp_clr_flowctrl, /* sd_setflowctrl */ 6465 udp_ioctl, /* sd_ioctl */ 6466 udp_close /* sd_close */ 6467 }; 6468