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