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