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