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