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