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