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