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