xref: /titanic_41/usr/src/uts/common/inet/udp/udp.c (revision 179184d3e9bc6ab146407a62a8461338687b1908)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 const char udp_version[] = "%Z%%M%	%I%	%E% SMI";
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/pattr.h>
35 #include <sys/stropts.h>
36 #include <sys/strlog.h>
37 #include <sys/strsun.h>
38 #include <sys/time.h>
39 #define	_SUN_TPI_VERSION 2
40 #include <sys/tihdr.h>
41 #include <sys/timod.h>
42 #include <sys/ddi.h>
43 #include <sys/sunddi.h>
44 #include <sys/strsubr.h>
45 #include <sys/suntpi.h>
46 #include <sys/xti_inet.h>
47 #include <sys/cmn_err.h>
48 #include <sys/kmem.h>
49 #include <sys/policy.h>
50 #include <sys/ucred.h>
51 #include <sys/zone.h>
52 
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/vtrace.h>
56 #include <sys/debug.h>
57 #include <sys/isa_defs.h>
58 #include <sys/random.h>
59 #include <netinet/in.h>
60 #include <netinet/ip6.h>
61 #include <netinet/icmp6.h>
62 #include <netinet/udp.h>
63 #include <net/if.h>
64 #include <net/route.h>
65 
66 #include <inet/common.h>
67 #include <inet/ip.h>
68 #include <inet/ip_impl.h>
69 #include <inet/ip6.h>
70 #include <inet/ip_ire.h>
71 #include <inet/ip_if.h>
72 #include <inet/ip_multi.h>
73 #include <inet/mi.h>
74 #include <inet/mib2.h>
75 #include <inet/nd.h>
76 #include <inet/optcom.h>
77 #include <inet/snmpcom.h>
78 #include <inet/kstatcom.h>
79 #include <inet/udp_impl.h>
80 #include <inet/ipclassifier.h>
81 #include <inet/ipsec_impl.h>
82 #include <inet/ipp_common.h>
83 
84 /*
85  * The ipsec_info.h header file is here since it has the definition for the
86  * M_CTL message types used by IP to convey information to the ULP. The
87  * ipsec_info.h needs the pfkeyv2.h, hence the latter's presence.
88  */
89 #include <net/pfkeyv2.h>
90 #include <inet/ipsec_info.h>
91 
92 #include <sys/tsol/label.h>
93 #include <sys/tsol/tnet.h>
94 #include <rpc/pmap_prot.h>
95 
96 /*
97  * Synchronization notes:
98  *
99  * UDP uses a combination of its internal perimeter, a global lock and
100  * a set of bind hash locks to protect its data structures.  Please see
101  * the note above udp_mode_assertions for details about the internal
102  * perimeter.
103  *
104  * When a UDP endpoint is bound to a local port, it is inserted into
105  * a bind hash list.  The list consists of an array of udp_fanout_t buckets.
106  * The size of the array is controlled by the udp_bind_fanout_size variable.
107  * This variable can be changed in /etc/system if the default value is
108  * not large enough.  Each bind hash bucket is protected by a per bucket
109  * lock.  It protects the udp_bind_hash and udp_ptpbhn fields in the udp_t
110  * structure.  An UDP endpoint is removed from the bind hash list only
111  * when it is being unbound or being closed.  The per bucket lock also
112  * protects a UDP endpoint's state changes.
113  *
114  * Plumbing notes:
115  *
116  * Both udp and ip are merged, but the streams plumbing is kept unchanged
117  * in that udp is always pushed atop /dev/ip.  This is done to preserve
118  * backwards compatibility for certain applications which rely on such
119  * plumbing geometry to do things such as issuing I_POP on the stream
120  * in order to obtain direct access to /dev/ip, etc.
121  *
122  * All UDP processings happen in the /dev/ip instance; the udp module
123  * instance does not possess any state about the endpoint, and merely
124  * acts as a dummy module whose presence is to keep the streams plumbing
125  * appearance unchanged.  At open time /dev/ip allocates a conn_t that
126  * happens to embed a udp_t.  This stays dormant until the time udp is
127  * pushed, which indicates to /dev/ip that it must convert itself from
128  * an IP to a UDP endpoint.
129  *
130  * We only allow for the following plumbing cases:
131  *
132  * Normal:
133  *	/dev/ip is first opened and later udp is pushed directly on top.
134  *	This is the default action that happens when a udp socket or
135  *	/dev/udp is opened.  The conn_t created by /dev/ip instance is
136  *	now shared and is marked with IPCL_UDP.
137  *
138  * SNMP-only:
139  *	udp is pushed on top of a module other than /dev/ip.  When this
140  *	happens it will support only SNMP semantics.  A new conn_t is
141  *	allocated and marked with IPCL_UDPMOD.
142  *
143  * The above cases imply that we don't support any intermediate module to
144  * reside in between /dev/ip and udp -- in fact, we never supported such
145  * scenario in the past as the inter-layer communication semantics have
146  * always been private.  Also note that the normal case allows for SNMP
147  * requests to be processed in addition to the rest of UDP operations.
148  *
149  * The normal case plumbing is depicted by the following diagram:
150  *
151  *	+---------------+---------------+
152  *	|		|		| udp
153  *	|     udp_wq	|    udp_rq	|
154  *	|		|    UDP_RD	|
155  *	|		|		|
156  *	+---------------+---------------+
157  *		|		^
158  *		v		|
159  *	+---------------+---------------+
160  *	|		|		| /dev/ip
161  *	|     ip_wq	|     ip_rq	| conn_t
162  *	|     UDP_WR	|		|
163  *	|		|		|
164  *	+---------------+---------------+
165  *
166  * Messages arriving at udp_wq from above will end up in ip_wq before
167  * it gets processed, i.e. udp write entry points will advance udp_wq
168  * and use its q_next value as ip_wq in order to use the conn_t that
169  * is stored in its q_ptr.  Likewise, messages generated by ip to the
170  * module above udp will appear as if they are originated from udp_rq,
171  * i.e. putnext() calls to the module above udp is done using the
172  * udp_rq instead of ip_rq in order to avoid udp_rput() which does
173  * nothing more than calling putnext().
174  *
175  * The above implies the following rule of thumb:
176  *
177  *   1. udp_t is obtained from conn_t, which is created by the /dev/ip
178  *	instance and is stored in q_ptr of both ip_wq and ip_rq.  There
179  *	is no direct reference to conn_t from either udp_wq or udp_rq.
180  *
181  *   2. Write-side entry points of udp can obtain the conn_t via the
182  *	Q_TO_CONN() macro, using the queue value obtain from UDP_WR().
183  *
184  *   3. While in /dev/ip context, putnext() to the module above udp can
185  *	be done by supplying the queue value obtained from UDP_RD().
186  *
187  */
188 
189 static queue_t *UDP_WR(queue_t *);
190 static queue_t *UDP_RD(queue_t *);
191 
192 udp_stat_t udp_statistics = {
193 	{ "udp_ip_send",		KSTAT_DATA_UINT64 },
194 	{ "udp_ip_ire_send",		KSTAT_DATA_UINT64 },
195 	{ "udp_ire_null",		KSTAT_DATA_UINT64 },
196 	{ "udp_drain",			KSTAT_DATA_UINT64 },
197 	{ "udp_sock_fallback",		KSTAT_DATA_UINT64 },
198 	{ "udp_rrw_busy",		KSTAT_DATA_UINT64 },
199 	{ "udp_rrw_msgcnt",		KSTAT_DATA_UINT64 },
200 	{ "udp_out_sw_cksum",		KSTAT_DATA_UINT64 },
201 	{ "udp_out_sw_cksum_bytes",	KSTAT_DATA_UINT64 },
202 	{ "udp_out_opt",		KSTAT_DATA_UINT64 },
203 	{ "udp_out_err_notconn",	KSTAT_DATA_UINT64 },
204 	{ "udp_out_err_output",		KSTAT_DATA_UINT64 },
205 	{ "udp_out_err_tudr",		KSTAT_DATA_UINT64 },
206 	{ "udp_in_pktinfo",		KSTAT_DATA_UINT64 },
207 	{ "udp_in_recvdstaddr",		KSTAT_DATA_UINT64 },
208 	{ "udp_in_recvopts",		KSTAT_DATA_UINT64 },
209 	{ "udp_in_recvif",		KSTAT_DATA_UINT64 },
210 	{ "udp_in_recvslla",		KSTAT_DATA_UINT64 },
211 	{ "udp_in_recvucred",		KSTAT_DATA_UINT64 },
212 	{ "udp_in_recvttl",		KSTAT_DATA_UINT64 },
213 	{ "udp_in_recvhopopts",		KSTAT_DATA_UINT64 },
214 	{ "udp_in_recvhoplimit",	KSTAT_DATA_UINT64 },
215 	{ "udp_in_recvdstopts",		KSTAT_DATA_UINT64 },
216 	{ "udp_in_recvrtdstopts",	KSTAT_DATA_UINT64 },
217 	{ "udp_in_recvrthdr",		KSTAT_DATA_UINT64 },
218 	{ "udp_in_recvpktinfo",		KSTAT_DATA_UINT64 },
219 	{ "udp_in_recvtclass",		KSTAT_DATA_UINT64 },
220 	{ "udp_in_timestamp",		KSTAT_DATA_UINT64 },
221 #ifdef DEBUG
222 	{ "udp_data_conn",		KSTAT_DATA_UINT64 },
223 	{ "udp_data_notconn",		KSTAT_DATA_UINT64 },
224 #endif
225 };
226 
227 static kstat_t *udp_ksp;
228 struct kmem_cache *udp_cache;
229 
230 /*
231  * Bind hash list size and hash function.  It has to be a power of 2 for
232  * hashing.
233  */
234 #define	UDP_BIND_FANOUT_SIZE	512
235 #define	UDP_BIND_HASH(lport) \
236 	((ntohs((uint16_t)lport)) & (udp_bind_fanout_size - 1))
237 
238 /* UDP bind fanout hash structure. */
239 typedef struct udp_fanout_s {
240 	udp_t *uf_udp;
241 	kmutex_t uf_lock;
242 #if defined(_LP64) || defined(_I32LPx)
243 	char	uf_pad[48];
244 #else
245 	char	uf_pad[56];
246 #endif
247 } udp_fanout_t;
248 
249 uint_t udp_bind_fanout_size = UDP_BIND_FANOUT_SIZE;
250 /* udp_fanout_t *udp_bind_fanout. */
251 static udp_fanout_t *udp_bind_fanout;
252 
253 /*
254  * This controls the rate some ndd info report functions can be used
255  * by non-privileged users.  It stores the last time such info is
256  * requested.  When those report functions are called again, this
257  * is checked with the current time and compare with the ndd param
258  * udp_ndd_get_info_interval.
259  */
260 static clock_t udp_last_ndd_get_info_time;
261 #define	NDD_TOO_QUICK_MSG \
262 	"ndd get info rate too high for non-privileged users, try again " \
263 	"later.\n"
264 #define	NDD_OUT_OF_BUF_MSG	"<< Out of buffer >>\n"
265 
266 /* Option processing attrs */
267 typedef struct udpattrs_s {
268 	ip6_pkt_t	*udpattr_ipp;
269 	mblk_t		*udpattr_mb;
270 	boolean_t	udpattr_credset;
271 } udpattrs_t;
272 
273 static void	udp_addr_req(queue_t *q, mblk_t *mp);
274 static void	udp_bind(queue_t *q, mblk_t *mp);
275 static void	udp_bind_hash_insert(udp_fanout_t *uf, udp_t *udp);
276 static void	udp_bind_hash_remove(udp_t *udp, boolean_t caller_holds_lock);
277 static int	udp_build_hdrs(queue_t *q, udp_t *udp);
278 static void	udp_capability_req(queue_t *q, mblk_t *mp);
279 static int	udp_close(queue_t *q);
280 static void	udp_connect(queue_t *q, mblk_t *mp);
281 static void	udp_disconnect(queue_t *q, mblk_t *mp);
282 static void	udp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
283 		    int sys_error);
284 static void	udp_err_ack_prim(queue_t *q, mblk_t *mp, int primitive,
285 		    t_scalar_t tlierr, int unixerr);
286 static int	udp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp,
287 		    cred_t *cr);
288 static int	udp_extra_priv_ports_add(queue_t *q, mblk_t *mp,
289 		    char *value, caddr_t cp, cred_t *cr);
290 static int	udp_extra_priv_ports_del(queue_t *q, mblk_t *mp,
291 		    char *value, caddr_t cp, cred_t *cr);
292 static void	udp_icmp_error(queue_t *q, mblk_t *mp);
293 static void	udp_icmp_error_ipv6(queue_t *q, mblk_t *mp);
294 static void	udp_info_req(queue_t *q, mblk_t *mp);
295 static mblk_t	*udp_ip_bind_mp(udp_t *udp, t_scalar_t bind_prim,
296 		    t_scalar_t addr_length);
297 static int	udp_open(queue_t *q, dev_t *devp, int flag, int sflag,
298 		    cred_t *credp);
299 static  int	udp_unitdata_opt_process(queue_t *q, mblk_t *mp,
300 		    int *errorp, udpattrs_t *udpattrs);
301 static boolean_t udp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name);
302 static int	udp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr);
303 static boolean_t udp_param_register(udpparam_t *udppa, int cnt);
304 static int	udp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp,
305 		    cred_t *cr);
306 static void	udp_report_item(mblk_t *mp, udp_t *udp);
307 static void	udp_rput(queue_t *q, mblk_t *mp);
308 static void	udp_rput_other(queue_t *, mblk_t *);
309 static int	udp_rinfop(queue_t *q, infod_t *dp);
310 static int	udp_rrw(queue_t *q, struiod_t *dp);
311 static	void	udp_rput_bind_ack(queue_t *q, mblk_t *mp);
312 static int	udp_status_report(queue_t *q, mblk_t *mp, caddr_t cp,
313 		    cred_t *cr);
314 static void	udp_send_data(udp_t *udp, queue_t *q, mblk_t *mp, ipha_t *ipha);
315 static void	udp_ud_err(queue_t *q, mblk_t *mp, uchar_t *destaddr,
316 		    t_scalar_t destlen, t_scalar_t err);
317 static void	udp_unbind(queue_t *q, mblk_t *mp);
318 static in_port_t udp_update_next_port(udp_t *udp, in_port_t port,
319     boolean_t random);
320 static void	udp_wput(queue_t *q, mblk_t *mp);
321 static mblk_t	*udp_output_v4(conn_t *, mblk_t *mp, ipaddr_t v4dst,
322 		    uint16_t port, uint_t srcid, int *error);
323 static mblk_t	*udp_output_v6(conn_t *connp, mblk_t *mp, sin6_t *sin6,
324 		    int *error);
325 static void	udp_wput_other(queue_t *q, mblk_t *mp);
326 static void	udp_wput_iocdata(queue_t *q, mblk_t *mp);
327 static void	udp_output(conn_t *connp, mblk_t *mp, struct sockaddr *addr,
328 		    socklen_t addrlen);
329 static size_t	udp_set_rcv_hiwat(udp_t *udp, size_t size);
330 
331 static void	udp_kstat_init(void);
332 static void	udp_kstat_fini(void);
333 static int	udp_kstat_update(kstat_t *kp, int rw);
334 static void	udp_input_wrapper(void *arg, mblk_t *mp, void *arg2);
335 static void	udp_rput_other_wrapper(void *arg, mblk_t *mp, void *arg2);
336 static void	udp_wput_other_wrapper(void *arg, mblk_t *mp, void *arg2);
337 static void	udp_resume_bind_cb(void *arg, mblk_t *mp, void *arg2);
338 
339 static void	udp_rcv_enqueue(queue_t *q, udp_t *udp, mblk_t *mp,
340 		    uint_t pkt_len);
341 static void	udp_rcv_drain(queue_t *q, udp_t *udp, boolean_t closing);
342 static void	udp_enter(conn_t *, mblk_t *, sqproc_t, uint8_t);
343 static void	udp_exit(conn_t *);
344 static void	udp_become_writer(conn_t *, mblk_t *, sqproc_t, uint8_t);
345 #ifdef DEBUG
346 static void	udp_mode_assertions(udp_t *, int);
347 #endif /* DEBUG */
348 
349 major_t UDP6_MAJ;
350 #define	UDP6 "udp6"
351 
352 #define	UDP_RECV_HIWATER	(56 * 1024)
353 #define	UDP_RECV_LOWATER	128
354 #define	UDP_XMIT_HIWATER	(56 * 1024)
355 #define	UDP_XMIT_LOWATER	1024
356 
357 static struct module_info udp_info =  {
358 	UDP_MOD_ID, UDP_MOD_NAME, 1, INFPSZ, UDP_RECV_HIWATER, UDP_RECV_LOWATER
359 };
360 
361 static struct qinit udp_rinit = {
362 	(pfi_t)udp_rput, NULL, udp_open, udp_close, NULL,
363 	&udp_info, NULL, udp_rrw, udp_rinfop, STRUIOT_STANDARD
364 };
365 
366 static struct qinit udp_winit = {
367 	(pfi_t)udp_wput, NULL, NULL, NULL, NULL,
368 	&udp_info, NULL, NULL, NULL, STRUIOT_NONE
369 };
370 
371 static struct qinit winit = {
372 	(pfi_t)putnext, NULL, NULL, NULL, NULL,
373 	&udp_info, NULL, NULL, NULL, STRUIOT_NONE
374 };
375 
376 /* Support for just SNMP if UDP is not pushed directly over device IP */
377 struct qinit udp_snmp_rinit = {
378 	(pfi_t)putnext, NULL, udp_open, ip_snmpmod_close, NULL,
379 	&udp_info, NULL, NULL, NULL, STRUIOT_NONE
380 };
381 
382 struct qinit udp_snmp_winit = {
383 	(pfi_t)ip_snmpmod_wput, NULL, udp_open, ip_snmpmod_close, NULL,
384 	&udp_info, NULL, NULL, NULL, STRUIOT_NONE
385 };
386 
387 struct streamtab udpinfo = {
388 	&udp_rinit, &winit
389 };
390 
391 static	sin_t	sin_null;	/* Zero address for quick clears */
392 static	sin6_t	sin6_null;	/* Zero address for quick clears */
393 
394 /* Hint not protected by any lock */
395 static in_port_t	udp_g_next_port_to_try;
396 
397 /*
398  * Extra privileged ports. In host byte order.
399  */
400 #define	UDP_NUM_EPRIV_PORTS	64
401 static int	udp_g_num_epriv_ports = UDP_NUM_EPRIV_PORTS;
402 static in_port_t udp_g_epriv_ports[UDP_NUM_EPRIV_PORTS] = { 2049, 4045 };
403 
404 /* Only modified during _init and _fini thus no locking is needed. */
405 static IDP	udp_g_nd;	/* Points to table of UDP ND variables. */
406 
407 /* MIB-2 stuff for SNMP */
408 static mib2_udp_t	udp_mib;	/* SNMP fixed size info */
409 static kstat_t		*udp_mibkp;	/* kstat exporting udp_mib data */
410 
411 #define	UDP_MAXPACKET_IPV4 (IP_MAXPACKET - UDPH_SIZE - IP_SIMPLE_HDR_LENGTH)
412 
413 /* Default structure copied into T_INFO_ACK messages */
414 static struct T_info_ack udp_g_t_info_ack_ipv4 = {
415 	T_INFO_ACK,
416 	UDP_MAXPACKET_IPV4,	/* TSDU_size. Excl. headers */
417 	T_INVALID,	/* ETSU_size.  udp does not support expedited data. */
418 	T_INVALID,	/* CDATA_size. udp does not support connect data. */
419 	T_INVALID,	/* DDATA_size. udp does not support disconnect data. */
420 	sizeof (sin_t),	/* ADDR_size. */
421 	0,		/* OPT_size - not initialized here */
422 	UDP_MAXPACKET_IPV4,	/* TIDU_size.  Excl. headers */
423 	T_CLTS,		/* SERV_type.  udp supports connection-less. */
424 	TS_UNBND,	/* CURRENT_state.  This is set from udp_state. */
425 	(XPG4_1|SENDZERO) /* PROVIDER_flag */
426 };
427 
428 #define	UDP_MAXPACKET_IPV6 (IP_MAXPACKET - UDPH_SIZE - IPV6_HDR_LEN)
429 
430 static	struct T_info_ack udp_g_t_info_ack_ipv6 = {
431 	T_INFO_ACK,
432 	UDP_MAXPACKET_IPV6,	/* TSDU_size.  Excl. headers */
433 	T_INVALID,	/* ETSU_size.  udp does not support expedited data. */
434 	T_INVALID,	/* CDATA_size. udp does not support connect data. */
435 	T_INVALID,	/* DDATA_size. udp does not support disconnect data. */
436 	sizeof (sin6_t), /* ADDR_size. */
437 	0,		/* OPT_size - not initialized here */
438 	UDP_MAXPACKET_IPV6,	/* TIDU_size. Excl. headers */
439 	T_CLTS,		/* SERV_type.  udp supports connection-less. */
440 	TS_UNBND,	/* CURRENT_state.  This is set from udp_state. */
441 	(XPG4_1|SENDZERO) /* PROVIDER_flag */
442 };
443 
444 /* largest UDP port number */
445 #define	UDP_MAX_PORT	65535
446 
447 /*
448  * Table of ND variables supported by udp.  These are loaded into udp_g_nd
449  * in udp_open.
450  * All of these are alterable, within the min/max values given, at run time.
451  */
452 /* BEGIN CSTYLED */
453 udpparam_t udp_param_arr[] = {
454  /*min		max		value		name */
455  { 0L,		256,		32,		"udp_wroff_extra" },
456  { 1L,		255,		255,		"udp_ipv4_ttl" },
457  { 0,		IPV6_MAX_HOPS,	IPV6_DEFAULT_HOPS, "udp_ipv6_hoplimit"},
458  { 1024,	(32 * 1024),	1024,		"udp_smallest_nonpriv_port" },
459  { 0,		1,		1,		"udp_do_checksum" },
460  { 1024,	UDP_MAX_PORT,	(32 * 1024),	"udp_smallest_anon_port" },
461  { 1024,	UDP_MAX_PORT,	UDP_MAX_PORT,	"udp_largest_anon_port" },
462  { UDP_XMIT_LOWATER, (1<<30), UDP_XMIT_HIWATER,	"udp_xmit_hiwat"},
463  { 0,		     (1<<30), UDP_XMIT_LOWATER, "udp_xmit_lowat"},
464  { UDP_RECV_LOWATER, (1<<30), UDP_RECV_HIWATER,	"udp_recv_hiwat"},
465  { 65536,	(1<<30),	2*1024*1024,	"udp_max_buf"},
466  { 100,		60000,		1000,		"udp_ndd_get_info_interval"},
467 };
468 /* END CSTYLED */
469 
470 /*
471  * The smallest anonymous port in the privileged port range which UDP
472  * looks for free port.  Use in the option UDP_ANONPRIVBIND.
473  */
474 static in_port_t udp_min_anonpriv_port = 512;
475 
476 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */
477 uint32_t udp_random_anon_port = 1;
478 
479 /*
480  * Hook functions to enable cluster networking.
481  * On non-clustered systems these vectors must always be NULL
482  */
483 
484 void (*cl_inet_bind)(uchar_t protocol, sa_family_t addr_family,
485     uint8_t *laddrp, in_port_t lport) = NULL;
486 void (*cl_inet_unbind)(uint8_t protocol, sa_family_t addr_family,
487     uint8_t *laddrp, in_port_t lport) = NULL;
488 
489 typedef union T_primitives *t_primp_t;
490 
491 #define	UDP_ENQUEUE_MP(udp, mp, proc, tag) {			\
492 	ASSERT((mp)->b_prev == NULL && (mp)->b_queue == NULL);	\
493 	ASSERT(MUTEX_HELD(&(udp)->udp_connp->conn_lock));	\
494 	(mp)->b_queue = (queue_t *)((uintptr_t)tag);		\
495 	(mp)->b_prev = (mblk_t *)proc;				\
496 	if ((udp)->udp_mphead == NULL)				\
497 		(udp)->udp_mphead = (mp);			\
498 	else							\
499 		(udp)->udp_mptail->b_next = (mp);		\
500 	(udp)->udp_mptail = (mp);				\
501 	(udp)->udp_mpcount++;					\
502 }
503 
504 #define	UDP_READERS_INCREF(udp) {				\
505 	ASSERT(MUTEX_HELD(&(udp)->udp_connp->conn_lock));	\
506 	(udp)->udp_reader_count++;				\
507 }
508 
509 #define	UDP_READERS_DECREF(udp) {				\
510 	ASSERT(MUTEX_HELD(&(udp)->udp_connp->conn_lock));	\
511 	(udp)->udp_reader_count--;				\
512 	if ((udp)->udp_reader_count == 0)			\
513 		cv_broadcast(&(udp)->udp_connp->conn_cv);	\
514 }
515 
516 #define	UDP_SQUEUE_DECREF(udp) {				\
517 	ASSERT(MUTEX_HELD(&(udp)->udp_connp->conn_lock));	\
518 	(udp)->udp_squeue_count--;				\
519 	if ((udp)->udp_squeue_count == 0)			\
520 		cv_broadcast(&(udp)->udp_connp->conn_cv);	\
521 }
522 
523 /*
524  * Notes on UDP endpoint synchronization:
525  *
526  * UDP needs exclusive operation on a per endpoint basis, when executing
527  * functions that modify the endpoint state.  udp_rput_other() deals with
528  * packets with IP options, and processing these packets end up having
529  * to update the endpoint's option related state.  udp_wput_other() deals
530  * with control operations from the top, e.g. connect() that needs to
531  * update the endpoint state.  These could be synchronized using locks,
532  * but the current version uses squeues for this purpose.  squeues may
533  * give performance improvement for certain cases such as connected UDP
534  * sockets; thus the framework allows for using squeues.
535  *
536  * The perimeter routines are described as follows:
537  *
538  * udp_enter():
539  *	Enter the UDP endpoint perimeter.
540  *
541  * udp_become_writer():
542  *	Become exclusive on the UDP endpoint.  Specifies a function
543  *	that will be called exclusively either immediately or later
544  *	when the perimeter is available exclusively.
545  *
546  * udp_exit():
547  *	Exit the UDP perimeter.
548  *
549  * Entering UDP from the top or from the bottom must be done using
550  * udp_enter().  No lock must be held while attempting to enter the UDP
551  * perimeter.  When finished, udp_exit() must be called to get out of
552  * the perimeter.
553  *
554  * UDP operates in either MT_HOT mode or in SQUEUE mode.  In MT_HOT mode,
555  * multiple threads may enter a UDP endpoint concurrently.  This is used
556  * for sending and/or receiving normal data.  Control operations and other
557  * special cases call udp_become_writer() to become exclusive on a per
558  * endpoint basis and this results in transitioning to SQUEUE mode.  squeue
559  * by definition serializes access to the conn_t.  When there are no more
560  * pending messages on the squeue for the UDP connection, the endpoint
561  * reverts to MT_HOT mode.  During the interregnum when not all MT threads
562  * of an endpoint have finished, messages are queued in the UDP endpoint
563  * and the UDP is in UDP_MT_QUEUED mode or UDP_QUEUED_SQUEUE mode.
564  *
565  * These modes have the following analogs:
566  *
567  *	UDP_MT_HOT/udp_reader_count==0		none
568  *	UDP_MT_HOT/udp_reader_count>0		RW_READ_LOCK
569  *	UDP_MT_QUEUED				RW_WRITE_WANTED
570  *	UDP_SQUEUE or UDP_QUEUED_SQUEUE		RW_WRITE_LOCKED
571  *
572  * Stable modes:	UDP_MT_HOT, UDP_SQUEUE
573  * Transient modes:	UDP_MT_QUEUED, UDP_QUEUED_SQUEUE
574  *
575  * While in stable modes, UDP keeps track of the number of threads
576  * operating on the endpoint.  The udp_reader_count variable represents
577  * the number of threads entering the endpoint as readers while it is
578  * in UDP_MT_HOT mode.  Transitioning to UDP_SQUEUE happens when there
579  * is only a single reader, i.e. when this counter drops to 1.  Likewise,
580  * udp_squeue_count represents the number of threads operating on the
581  * endpoint's squeue while it is in UDP_SQUEUE mode.  The mode transition
582  * to UDP_MT_HOT happens after the last thread exits the endpoint, i.e.
583  * when this counter drops to 0.
584  *
585  * The default mode is set to UDP_MT_HOT and UDP alternates between
586  * UDP_MT_HOT and UDP_SQUEUE as shown in the state transition below.
587  *
588  * Mode transition:
589  * ----------------------------------------------------------------
590  * old mode		Event				New mode
591  * ----------------------------------------------------------------
592  * UDP_MT_HOT		Call to udp_become_writer()	UDP_SQUEUE
593  *			and udp_reader_count == 1
594  *
595  * UDP_MT_HOT		Call to udp_become_writer()	UDP_MT_QUEUED
596  *			and udp_reader_count > 1
597  *
598  * UDP_MT_QUEUED	udp_reader_count drops to zero	UDP_QUEUED_SQUEUE
599  *
600  * UDP_QUEUED_SQUEUE	All messages enqueued on the	UDP_SQUEUE
601  *			internal UDP queue successfully
602  *			moved to squeue AND udp_squeue_count != 0
603  *
604  * UDP_QUEUED_SQUEUE	All messages enqueued on the	UDP_MT_HOT
605  *			internal UDP queue successfully
606  *			moved to squeue AND udp_squeue_count
607  *			drops to zero
608  *
609  * UDP_SQUEUE		udp_squeue_count drops to zero	UDP_MT_HOT
610  * ----------------------------------------------------------------
611  */
612 
613 static queue_t *
614 UDP_WR(queue_t *q)
615 {
616 	ASSERT(q->q_ptr == NULL && _OTHERQ(q)->q_ptr == NULL);
617 	ASSERT(WR(q)->q_next != NULL && WR(q)->q_next->q_ptr != NULL);
618 	ASSERT(IPCL_IS_UDP(Q_TO_CONN(WR(q)->q_next)));
619 
620 	return (_WR(q)->q_next);
621 }
622 
623 static queue_t *
624 UDP_RD(queue_t *q)
625 {
626 	ASSERT(q->q_ptr != NULL && _OTHERQ(q)->q_ptr != NULL);
627 	ASSERT(IPCL_IS_UDP(Q_TO_CONN(q)));
628 	ASSERT(RD(q)->q_next != NULL && RD(q)->q_next->q_ptr == NULL);
629 
630 	return (_RD(q)->q_next);
631 }
632 
633 #ifdef DEBUG
634 #define	UDP_MODE_ASSERTIONS(udp, caller) udp_mode_assertions(udp, caller)
635 #else
636 #define	UDP_MODE_ASSERTIONS(udp, caller)
637 #endif
638 
639 /* Invariants */
640 #ifdef DEBUG
641 
642 uint32_t udp_count[4];
643 
644 /* Context of udp_mode_assertions */
645 #define	UDP_ENTER		1
646 #define	UDP_BECOME_WRITER	2
647 #define	UDP_EXIT		3
648 
649 static void
650 udp_mode_assertions(udp_t *udp, int caller)
651 {
652 	ASSERT(MUTEX_HELD(&udp->udp_connp->conn_lock));
653 
654 	switch (udp->udp_mode) {
655 	case UDP_MT_HOT:
656 		/*
657 		 * Messages have not yet been enqueued on the internal queue,
658 		 * otherwise we would have switched to UDP_MT_QUEUED. Likewise
659 		 * by definition, there can't be any messages enqueued on the
660 		 * squeue. The UDP could be quiescent, so udp_reader_count
661 		 * could be zero at entry.
662 		 */
663 		ASSERT(udp->udp_mphead == NULL && udp->udp_mpcount == 0 &&
664 		    udp->udp_squeue_count == 0);
665 		ASSERT(caller == UDP_ENTER || udp->udp_reader_count != 0);
666 		udp_count[0]++;
667 		break;
668 
669 	case UDP_MT_QUEUED:
670 		/*
671 		 * The last MT thread to exit the udp perimeter empties the
672 		 * internal queue and then switches the UDP to
673 		 * UDP_QUEUED_SQUEUE mode. Since we are still in UDP_MT_QUEUED
674 		 * mode, it means there must be at least 1 MT thread still in
675 		 * the perimeter and at least 1 message on the internal queue.
676 		 */
677 		ASSERT(udp->udp_reader_count >= 1 && udp->udp_mphead != NULL &&
678 		    udp->udp_mpcount != 0 && udp->udp_squeue_count == 0);
679 		udp_count[1]++;
680 		break;
681 
682 	case UDP_QUEUED_SQUEUE:
683 		/*
684 		 * The switch has happened from MT to SQUEUE. So there can't
685 		 * any MT threads. Messages could still pile up on the internal
686 		 * queue until the transition is complete and we move to
687 		 * UDP_SQUEUE mode. We can't assert on nonzero udp_squeue_count
688 		 * since the squeue could drain any time.
689 		 */
690 		ASSERT(udp->udp_reader_count == 0);
691 		udp_count[2]++;
692 		break;
693 
694 	case UDP_SQUEUE:
695 		/*
696 		 * The transition is complete. Thre can't be any messages on
697 		 * the internal queue. The udp could be quiescent or the squeue
698 		 * could drain any time, so we can't assert on nonzero
699 		 * udp_squeue_count during entry. Nor can we assert that
700 		 * udp_reader_count is zero, since, a reader thread could have
701 		 * directly become writer in line by calling udp_become_writer
702 		 * without going through the queued states.
703 		 */
704 		ASSERT(udp->udp_mphead == NULL && udp->udp_mpcount == 0);
705 		ASSERT(caller == UDP_ENTER || udp->udp_squeue_count != 0);
706 		udp_count[3]++;
707 		break;
708 	}
709 }
710 #endif
711 
712 #define	_UDP_ENTER(connp, mp, proc, tag) {				\
713 	udp_t *_udp = (connp)->conn_udp;				\
714 									\
715 	mutex_enter(&(connp)->conn_lock);				\
716 	if ((connp)->conn_state_flags & CONN_CLOSING) {			\
717 		mutex_exit(&(connp)->conn_lock);			\
718 		freemsg(mp);						\
719 	} else {							\
720 		UDP_MODE_ASSERTIONS(_udp, UDP_ENTER);			\
721 									\
722 		switch (_udp->udp_mode) {				\
723 		case UDP_MT_HOT:					\
724 			/* We can execute as reader right away. */	\
725 			UDP_READERS_INCREF(_udp);			\
726 			mutex_exit(&(connp)->conn_lock);		\
727 			(*(proc))(connp, mp, (connp)->conn_sqp);	\
728 			break;						\
729 									\
730 		case UDP_SQUEUE:					\
731 			/*						\
732 			 * We are in squeue mode, send the		\
733 			 * packet to the squeue				\
734 			 */						\
735 			_udp->udp_squeue_count++;			\
736 			CONN_INC_REF_LOCKED(connp);			\
737 			mutex_exit(&(connp)->conn_lock);		\
738 			squeue_enter((connp)->conn_sqp, mp, proc,	\
739 			    connp, tag);				\
740 			break;						\
741 									\
742 		case UDP_MT_QUEUED:					\
743 		case UDP_QUEUED_SQUEUE:					\
744 			/*						\
745 			 * Some messages may have been enqueued		\
746 			 * ahead of us.  Enqueue the new message	\
747 			 * at the tail of the internal queue to		\
748 			 * preserve message ordering.			\
749 			 */						\
750 			UDP_ENQUEUE_MP(_udp, mp, proc, tag);		\
751 			mutex_exit(&(connp)->conn_lock);		\
752 			break;						\
753 		}							\
754 	}								\
755 }
756 
757 static void
758 udp_enter(conn_t *connp, mblk_t *mp, sqproc_t proc, uint8_t tag)
759 {
760 	_UDP_ENTER(connp, mp, proc, tag);
761 }
762 
763 static void
764 udp_become_writer(conn_t *connp, mblk_t *mp, sqproc_t proc, uint8_t tag)
765 {
766 	udp_t	*udp;
767 
768 	udp = connp->conn_udp;
769 
770 	mutex_enter(&connp->conn_lock);
771 
772 	UDP_MODE_ASSERTIONS(udp, UDP_BECOME_WRITER);
773 
774 	switch (udp->udp_mode) {
775 	case UDP_MT_HOT:
776 		if (udp->udp_reader_count == 1) {
777 			/*
778 			 * We are the only MT thread. Switch to squeue mode
779 			 * immediately.
780 			 */
781 			udp->udp_mode = UDP_SQUEUE;
782 			udp->udp_squeue_count = 1;
783 			CONN_INC_REF_LOCKED(connp);
784 			mutex_exit(&connp->conn_lock);
785 			squeue_enter(connp->conn_sqp, mp, proc, connp, tag);
786 			return;
787 		}
788 		/* FALLTHRU */
789 
790 	case UDP_MT_QUEUED:
791 		/* Enqueue the packet internally in UDP */
792 		udp->udp_mode = UDP_MT_QUEUED;
793 		UDP_ENQUEUE_MP(udp, mp, proc, tag);
794 		mutex_exit(&connp->conn_lock);
795 		return;
796 
797 	case UDP_SQUEUE:
798 	case UDP_QUEUED_SQUEUE:
799 		/*
800 		 * We are already exclusive. i.e. we are already
801 		 * writer. Simply call the desired function.
802 		 */
803 		udp->udp_squeue_count++;
804 		mutex_exit(&connp->conn_lock);
805 		(*proc)(connp, mp, connp->conn_sqp);
806 		return;
807 	}
808 }
809 
810 /*
811  * Transition from MT mode to SQUEUE mode, when the last MT thread
812  * is exiting the UDP perimeter. Move all messages from the internal
813  * udp queue to the squeue. A better way would be to move all the
814  * messages in one shot, this needs more support from the squeue framework
815  */
816 static void
817 udp_switch_to_squeue(udp_t *udp)
818 {
819 	mblk_t *mp;
820 	mblk_t	*mp_next;
821 	sqproc_t proc;
822 	uint8_t	tag;
823 	conn_t	*connp = udp->udp_connp;
824 
825 	ASSERT(MUTEX_HELD(&connp->conn_lock));
826 	ASSERT(udp->udp_mode == UDP_MT_QUEUED);
827 	while (udp->udp_mphead != NULL) {
828 		mp = udp->udp_mphead;
829 		udp->udp_mphead = NULL;
830 		udp->udp_mptail = NULL;
831 		udp->udp_mpcount = 0;
832 		udp->udp_mode = UDP_QUEUED_SQUEUE;
833 		mutex_exit(&connp->conn_lock);
834 		/*
835 		 * It is best not to hold any locks across the calls
836 		 * to squeue functions. Since we drop the lock we
837 		 * need to go back and check the udp_mphead once again
838 		 * after the squeue_fill and hence the while loop at
839 		 * the top of this function
840 		 */
841 		for (; mp != NULL; mp = mp_next) {
842 			mp_next = mp->b_next;
843 			proc = (sqproc_t)mp->b_prev;
844 			tag = (uint8_t)((uintptr_t)mp->b_queue);
845 			mp->b_next = NULL;
846 			mp->b_prev = NULL;
847 			mp->b_queue = NULL;
848 			CONN_INC_REF(connp);
849 			udp->udp_squeue_count++;
850 			squeue_fill(connp->conn_sqp, mp, proc, connp,
851 			    tag);
852 		}
853 		mutex_enter(&connp->conn_lock);
854 	}
855 	/*
856 	 * udp_squeue_count of zero implies that the squeue has drained
857 	 * even before we arrived here (i.e. after the squeue_fill above)
858 	 */
859 	udp->udp_mode = (udp->udp_squeue_count != 0) ?
860 	    UDP_SQUEUE : UDP_MT_HOT;
861 }
862 
863 #define	_UDP_EXIT(connp) {						\
864 	udp_t *_udp = (connp)->conn_udp;				\
865 									\
866 	mutex_enter(&(connp)->conn_lock);				\
867 	UDP_MODE_ASSERTIONS(_udp, UDP_EXIT);				\
868 									\
869 	switch (_udp->udp_mode) {					\
870 	case UDP_MT_HOT:						\
871 		UDP_READERS_DECREF(_udp);				\
872 		mutex_exit(&(connp)->conn_lock);			\
873 		break;							\
874 									\
875 	case UDP_SQUEUE:						\
876 		UDP_SQUEUE_DECREF(_udp);				\
877 		if (_udp->udp_squeue_count == 0)			\
878 		    _udp->udp_mode = UDP_MT_HOT;			\
879 		mutex_exit(&(connp)->conn_lock);			\
880 		break;							\
881 									\
882 	case UDP_MT_QUEUED:						\
883 		/*							\
884 		 * If this is the last MT thread, we need to		\
885 		 * switch to squeue mode				\
886 		 */							\
887 		UDP_READERS_DECREF(_udp);				\
888 		if (_udp->udp_reader_count == 0)			\
889 			udp_switch_to_squeue(_udp);			\
890 		mutex_exit(&(connp)->conn_lock);			\
891 		break;							\
892 									\
893 	case UDP_QUEUED_SQUEUE:						\
894 		UDP_SQUEUE_DECREF(_udp);				\
895 		/*							\
896 		 * Even if the udp_squeue_count drops to zero, we	\
897 		 * don't want to change udp_mode to UDP_MT_HOT here.	\
898 		 * The thread in udp_switch_to_squeue will take care	\
899 		 * of the transition to UDP_MT_HOT, after emptying	\
900 		 * any more new messages that have been enqueued in	\
901 		 * udp_mphead.						\
902 		 */							\
903 		mutex_exit(&(connp)->conn_lock);			\
904 		break;							\
905 	}								\
906 }
907 
908 static void
909 udp_exit(conn_t *connp)
910 {
911 	_UDP_EXIT(connp);
912 }
913 
914 /*
915  * Return the next anonymous port in the privileged port range for
916  * bind checking.
917  *
918  * Trusted Extension (TX) notes: TX allows administrator to mark or
919  * reserve ports as Multilevel ports (MLP). MLP has special function
920  * on TX systems. Once a port is made MLP, it's not available as
921  * ordinary port. This creates "holes" in the port name space. It
922  * may be necessary to skip the "holes" find a suitable anon port.
923  */
924 static in_port_t
925 udp_get_next_priv_port(udp_t *udp)
926 {
927 	static in_port_t next_priv_port = IPPORT_RESERVED - 1;
928 	in_port_t nextport;
929 	boolean_t restart = B_FALSE;
930 
931 retry:
932 	if (next_priv_port < udp_min_anonpriv_port ||
933 	    next_priv_port >= IPPORT_RESERVED) {
934 		next_priv_port = IPPORT_RESERVED - 1;
935 		if (restart)
936 			return (0);
937 		restart = B_TRUE;
938 	}
939 
940 	if (is_system_labeled() &&
941 	    (nextport = tsol_next_port(crgetzone(udp->udp_connp->conn_cred),
942 	    next_priv_port, IPPROTO_UDP, B_FALSE)) != 0) {
943 		next_priv_port = nextport;
944 		goto retry;
945 	}
946 
947 	return (next_priv_port--);
948 }
949 
950 /* UDP bind hash report triggered via the Named Dispatch mechanism. */
951 /* ARGSUSED */
952 static int
953 udp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
954 {
955 	udp_fanout_t	*udpf;
956 	int		i;
957 	zoneid_t	zoneid;
958 	conn_t		*connp;
959 	udp_t		*udp;
960 
961 	connp = Q_TO_CONN(q);
962 	udp = connp->conn_udp;
963 
964 	/* Refer to comments in udp_status_report(). */
965 	if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) {
966 		if (ddi_get_lbolt() - udp_last_ndd_get_info_time <
967 		    drv_usectohz(udp_ndd_get_info_interval * 1000)) {
968 			(void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG);
969 			return (0);
970 		}
971 	}
972 	if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) {
973 		/* The following may work even if we cannot get a large buf. */
974 		(void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG);
975 		return (0);
976 	}
977 
978 	(void) mi_mpprintf(mp,
979 	    "UDP     " MI_COL_HDRPAD_STR
980 	/*   12345678[89ABCDEF] */
981 	    " zone lport src addr        dest addr       port  state");
982 	/*    1234 12345 xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx 12345 UNBOUND */
983 
984 	zoneid = connp->conn_zoneid;
985 
986 	for (i = 0; i < udp_bind_fanout_size; i++) {
987 		udpf = &udp_bind_fanout[i];
988 		mutex_enter(&udpf->uf_lock);
989 
990 		/* Print the hash index. */
991 		udp = udpf->uf_udp;
992 		if (zoneid != GLOBAL_ZONEID) {
993 			/* skip to first entry in this zone; might be none */
994 			while (udp != NULL &&
995 			    udp->udp_connp->conn_zoneid != zoneid)
996 				udp = udp->udp_bind_hash;
997 		}
998 		if (udp != NULL) {
999 			uint_t print_len, buf_len;
1000 
1001 			buf_len = mp->b_cont->b_datap->db_lim -
1002 			    mp->b_cont->b_wptr;
1003 			print_len = snprintf((char *)mp->b_cont->b_wptr,
1004 			    buf_len, "%d\n", i);
1005 			if (print_len < buf_len) {
1006 				mp->b_cont->b_wptr += print_len;
1007 			} else {
1008 				mp->b_cont->b_wptr += buf_len;
1009 			}
1010 			for (; udp != NULL; udp = udp->udp_bind_hash) {
1011 				if (zoneid == GLOBAL_ZONEID ||
1012 				    zoneid == udp->udp_connp->conn_zoneid)
1013 					udp_report_item(mp->b_cont, udp);
1014 			}
1015 		}
1016 		mutex_exit(&udpf->uf_lock);
1017 	}
1018 	udp_last_ndd_get_info_time = ddi_get_lbolt();
1019 	return (0);
1020 }
1021 
1022 /*
1023  * Hash list removal routine for udp_t structures.
1024  */
1025 static void
1026 udp_bind_hash_remove(udp_t *udp, boolean_t caller_holds_lock)
1027 {
1028 	udp_t	*udpnext;
1029 	kmutex_t *lockp;
1030 
1031 	if (udp->udp_ptpbhn == NULL)
1032 		return;
1033 
1034 	/*
1035 	 * Extract the lock pointer in case there are concurrent
1036 	 * hash_remove's for this instance.
1037 	 */
1038 	ASSERT(udp->udp_port != 0);
1039 	if (!caller_holds_lock) {
1040 		lockp = &udp_bind_fanout[UDP_BIND_HASH(udp->udp_port)].uf_lock;
1041 		ASSERT(lockp != NULL);
1042 		mutex_enter(lockp);
1043 	}
1044 	if (udp->udp_ptpbhn != NULL) {
1045 		udpnext = udp->udp_bind_hash;
1046 		if (udpnext != NULL) {
1047 			udpnext->udp_ptpbhn = udp->udp_ptpbhn;
1048 			udp->udp_bind_hash = NULL;
1049 		}
1050 		*udp->udp_ptpbhn = udpnext;
1051 		udp->udp_ptpbhn = NULL;
1052 	}
1053 	if (!caller_holds_lock) {
1054 		mutex_exit(lockp);
1055 	}
1056 }
1057 
1058 static void
1059 udp_bind_hash_insert(udp_fanout_t *uf, udp_t *udp)
1060 {
1061 	udp_t	**udpp;
1062 	udp_t	*udpnext;
1063 
1064 	ASSERT(MUTEX_HELD(&uf->uf_lock));
1065 	if (udp->udp_ptpbhn != NULL) {
1066 		udp_bind_hash_remove(udp, B_TRUE);
1067 	}
1068 	udpp = &uf->uf_udp;
1069 	udpnext = udpp[0];
1070 	if (udpnext != NULL) {
1071 		/*
1072 		 * If the new udp bound to the INADDR_ANY address
1073 		 * and the first one in the list is not bound to
1074 		 * INADDR_ANY we skip all entries until we find the
1075 		 * first one bound to INADDR_ANY.
1076 		 * This makes sure that applications binding to a
1077 		 * specific address get preference over those binding to
1078 		 * INADDR_ANY.
1079 		 */
1080 		if (V6_OR_V4_INADDR_ANY(udp->udp_bound_v6src) &&
1081 		    !V6_OR_V4_INADDR_ANY(udpnext->udp_bound_v6src)) {
1082 			while ((udpnext = udpp[0]) != NULL &&
1083 			    !V6_OR_V4_INADDR_ANY(
1084 			    udpnext->udp_bound_v6src)) {
1085 				udpp = &(udpnext->udp_bind_hash);
1086 			}
1087 			if (udpnext != NULL)
1088 				udpnext->udp_ptpbhn = &udp->udp_bind_hash;
1089 		} else {
1090 			udpnext->udp_ptpbhn = &udp->udp_bind_hash;
1091 		}
1092 	}
1093 	udp->udp_bind_hash = udpnext;
1094 	udp->udp_ptpbhn = udpp;
1095 	udpp[0] = udp;
1096 }
1097 
1098 /*
1099  * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message
1100  * passed to udp_wput.
1101  * It associates a port number and local address with the stream.
1102  * The O_T_BIND_REQ/T_BIND_REQ is passed downstream to ip with the UDP
1103  * protocol type (IPPROTO_UDP) placed in the message following the address.
1104  * A T_BIND_ACK message is passed upstream when ip acknowledges the request.
1105  * (Called as writer.)
1106  *
1107  * Note that UDP over IPv4 and IPv6 sockets can use the same port number
1108  * without setting SO_REUSEADDR. This is needed so that they
1109  * can be viewed as two independent transport protocols.
1110  * However, anonymouns ports are allocated from the same range to avoid
1111  * duplicating the udp_g_next_port_to_try.
1112  */
1113 static void
1114 udp_bind(queue_t *q, mblk_t *mp)
1115 {
1116 	sin_t		*sin;
1117 	sin6_t		*sin6;
1118 	mblk_t		*mp1;
1119 	in_port_t	port;		/* Host byte order */
1120 	in_port_t	requested_port;	/* Host byte order */
1121 	struct T_bind_req *tbr;
1122 	int		count;
1123 	in6_addr_t	v6src;
1124 	boolean_t	bind_to_req_port_only;
1125 	int		loopmax;
1126 	udp_fanout_t	*udpf;
1127 	in_port_t	lport;		/* Network byte order */
1128 	zoneid_t	zoneid;
1129 	conn_t		*connp;
1130 	udp_t		*udp;
1131 	boolean_t	is_inaddr_any;
1132 	mlp_type_t	addrtype, mlptype;
1133 
1134 	connp = Q_TO_CONN(q);
1135 	udp = connp->conn_udp;
1136 	if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
1137 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
1138 		    "udp_bind: bad req, len %u",
1139 		    (uint_t)(mp->b_wptr - mp->b_rptr));
1140 		udp_err_ack(q, mp, TPROTO, 0);
1141 		return;
1142 	}
1143 
1144 	if (udp->udp_state != TS_UNBND) {
1145 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
1146 		    "udp_bind: bad state, %u", udp->udp_state);
1147 		udp_err_ack(q, mp, TOUTSTATE, 0);
1148 		return;
1149 	}
1150 	/*
1151 	 * Reallocate the message to make sure we have enough room for an
1152 	 * address and the protocol type.
1153 	 */
1154 	mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1);
1155 	if (!mp1) {
1156 		udp_err_ack(q, mp, TSYSERR, ENOMEM);
1157 		return;
1158 	}
1159 
1160 	mp = mp1;
1161 	tbr = (struct T_bind_req *)mp->b_rptr;
1162 	switch (tbr->ADDR_length) {
1163 	case 0:			/* Request for a generic port */
1164 		tbr->ADDR_offset = sizeof (struct T_bind_req);
1165 		if (udp->udp_family == AF_INET) {
1166 			tbr->ADDR_length = sizeof (sin_t);
1167 			sin = (sin_t *)&tbr[1];
1168 			*sin = sin_null;
1169 			sin->sin_family = AF_INET;
1170 			mp->b_wptr = (uchar_t *)&sin[1];
1171 		} else {
1172 			ASSERT(udp->udp_family == AF_INET6);
1173 			tbr->ADDR_length = sizeof (sin6_t);
1174 			sin6 = (sin6_t *)&tbr[1];
1175 			*sin6 = sin6_null;
1176 			sin6->sin6_family = AF_INET6;
1177 			mp->b_wptr = (uchar_t *)&sin6[1];
1178 		}
1179 		port = 0;
1180 		break;
1181 
1182 	case sizeof (sin_t):	/* Complete IPv4 address */
1183 		sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset,
1184 		    sizeof (sin_t));
1185 		if (sin == NULL || !OK_32PTR((char *)sin)) {
1186 			udp_err_ack(q, mp, TSYSERR, EINVAL);
1187 			return;
1188 		}
1189 		if (udp->udp_family != AF_INET ||
1190 		    sin->sin_family != AF_INET) {
1191 			udp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
1192 			return;
1193 		}
1194 		port = ntohs(sin->sin_port);
1195 		break;
1196 
1197 	case sizeof (sin6_t):	/* complete IPv6 address */
1198 		sin6 = (sin6_t *)mi_offset_param(mp, tbr->ADDR_offset,
1199 		    sizeof (sin6_t));
1200 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
1201 			udp_err_ack(q, mp, TSYSERR, EINVAL);
1202 			return;
1203 		}
1204 		if (udp->udp_family != AF_INET6 ||
1205 		    sin6->sin6_family != AF_INET6) {
1206 			udp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
1207 			return;
1208 		}
1209 		port = ntohs(sin6->sin6_port);
1210 		break;
1211 
1212 	default:		/* Invalid request */
1213 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
1214 		    "udp_bind: bad ADDR_length length %u", tbr->ADDR_length);
1215 		udp_err_ack(q, mp, TBADADDR, 0);
1216 		return;
1217 	}
1218 
1219 	requested_port = port;
1220 
1221 	if (requested_port == 0 || tbr->PRIM_type == O_T_BIND_REQ)
1222 		bind_to_req_port_only = B_FALSE;
1223 	else			/* T_BIND_REQ and requested_port != 0 */
1224 		bind_to_req_port_only = B_TRUE;
1225 
1226 	if (requested_port == 0) {
1227 		/*
1228 		 * If the application passed in zero for the port number, it
1229 		 * doesn't care which port number we bind to. Get one in the
1230 		 * valid range.
1231 		 */
1232 		if (udp->udp_anon_priv_bind) {
1233 			port = udp_get_next_priv_port(udp);
1234 		} else {
1235 			port = udp_update_next_port(udp,
1236 			    udp_g_next_port_to_try, B_TRUE);
1237 		}
1238 	} else {
1239 		/*
1240 		 * If the port is in the well-known privileged range,
1241 		 * make sure the caller was privileged.
1242 		 */
1243 		int i;
1244 		boolean_t priv = B_FALSE;
1245 
1246 		if (port < udp_smallest_nonpriv_port) {
1247 			priv = B_TRUE;
1248 		} else {
1249 			for (i = 0; i < udp_g_num_epriv_ports; i++) {
1250 				if (port == udp_g_epriv_ports[i]) {
1251 					priv = B_TRUE;
1252 					break;
1253 				}
1254 			}
1255 		}
1256 
1257 		if (priv) {
1258 			cred_t *cr = DB_CREDDEF(mp, connp->conn_cred);
1259 
1260 			if (secpolicy_net_privaddr(cr, port) != 0) {
1261 				udp_err_ack(q, mp, TACCES, 0);
1262 				return;
1263 			}
1264 		}
1265 	}
1266 
1267 	if (port == 0) {
1268 		udp_err_ack(q, mp, TNOADDR, 0);
1269 		return;
1270 	}
1271 
1272 	/*
1273 	 * Copy the source address into our udp structure. This address
1274 	 * may still be zero; if so, IP will fill in the correct address
1275 	 * each time an outbound packet is passed to it.
1276 	 */
1277 	if (udp->udp_family == AF_INET) {
1278 		ASSERT(sin != NULL);
1279 		ASSERT(udp->udp_ipversion == IPV4_VERSION);
1280 		udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + UDPH_SIZE +
1281 		    udp->udp_ip_snd_options_len;
1282 		IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &v6src);
1283 	} else {
1284 		ASSERT(sin6 != NULL);
1285 		v6src = sin6->sin6_addr;
1286 		if (IN6_IS_ADDR_V4MAPPED(&v6src)) {
1287 			udp->udp_ipversion = IPV4_VERSION;
1288 			udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
1289 			    UDPH_SIZE + udp->udp_ip_snd_options_len;
1290 		} else {
1291 			udp->udp_ipversion = IPV6_VERSION;
1292 			udp->udp_max_hdr_len = udp->udp_sticky_hdrs_len;
1293 		}
1294 	}
1295 
1296 	/*
1297 	 * If udp_reuseaddr is not set, then we have to make sure that
1298 	 * the IP address and port number the application requested
1299 	 * (or we selected for the application) is not being used by
1300 	 * another stream.  If another stream is already using the
1301 	 * requested IP address and port, the behavior depends on
1302 	 * "bind_to_req_port_only". If set the bind fails; otherwise we
1303 	 * search for any an unused port to bind to the the stream.
1304 	 *
1305 	 * As per the BSD semantics, as modified by the Deering multicast
1306 	 * changes, if udp_reuseaddr is set, then we allow multiple binds
1307 	 * to the same port independent of the local IP address.
1308 	 *
1309 	 * This is slightly different than in SunOS 4.X which did not
1310 	 * support IP multicast. Note that the change implemented by the
1311 	 * Deering multicast code effects all binds - not only binding
1312 	 * to IP multicast addresses.
1313 	 *
1314 	 * Note that when binding to port zero we ignore SO_REUSEADDR in
1315 	 * order to guarantee a unique port.
1316 	 */
1317 
1318 	count = 0;
1319 	if (udp->udp_anon_priv_bind) {
1320 		/* loopmax = (IPPORT_RESERVED-1) - udp_min_anonpriv_port + 1 */
1321 		loopmax = IPPORT_RESERVED - udp_min_anonpriv_port;
1322 	} else {
1323 		loopmax = udp_largest_anon_port - udp_smallest_anon_port + 1;
1324 	}
1325 
1326 	is_inaddr_any = V6_OR_V4_INADDR_ANY(v6src);
1327 	zoneid = connp->conn_zoneid;
1328 
1329 	for (;;) {
1330 		udp_t		*udp1;
1331 		boolean_t	found_exclbind = B_FALSE;
1332 
1333 		/*
1334 		 * Walk through the list of udp streams bound to
1335 		 * requested port with the same IP address.
1336 		 */
1337 		lport = htons(port);
1338 		udpf = &udp_bind_fanout[UDP_BIND_HASH(lport)];
1339 		mutex_enter(&udpf->uf_lock);
1340 		for (udp1 = udpf->uf_udp; udp1 != NULL;
1341 		    udp1 = udp1->udp_bind_hash) {
1342 			if (lport != udp1->udp_port)
1343 				continue;
1344 
1345 			/*
1346 			 * On a labeled system, we must treat bindings to ports
1347 			 * on shared IP addresses by sockets with MAC exemption
1348 			 * privilege as being in all zones, as there's
1349 			 * otherwise no way to identify the right receiver.
1350 			 */
1351 			if (zoneid != udp1->udp_connp->conn_zoneid &&
1352 			    !udp->udp_mac_exempt && !udp1->udp_mac_exempt)
1353 				continue;
1354 
1355 			/*
1356 			 * If UDP_EXCLBIND is set for either the bound or
1357 			 * binding endpoint, the semantics of bind
1358 			 * is changed according to the following chart.
1359 			 *
1360 			 * spec = specified address (v4 or v6)
1361 			 * unspec = unspecified address (v4 or v6)
1362 			 * A = specified addresses are different for endpoints
1363 			 *
1364 			 * bound	bind to		allowed?
1365 			 * -------------------------------------
1366 			 * unspec	unspec		no
1367 			 * unspec	spec		no
1368 			 * spec		unspec		no
1369 			 * spec		spec		yes if A
1370 			 *
1371 			 * For labeled systems, SO_MAC_EXEMPT behaves the same
1372 			 * as UDP_EXCLBIND, except that zoneid is ignored.
1373 			 */
1374 			if (udp1->udp_exclbind || udp->udp_exclbind ||
1375 			    udp1->udp_mac_exempt || udp->udp_mac_exempt) {
1376 				if (V6_OR_V4_INADDR_ANY(
1377 				    udp1->udp_bound_v6src) ||
1378 				    is_inaddr_any ||
1379 				    IN6_ARE_ADDR_EQUAL(&udp1->udp_bound_v6src,
1380 				    &v6src)) {
1381 					found_exclbind = B_TRUE;
1382 					break;
1383 				}
1384 				continue;
1385 			}
1386 
1387 			/*
1388 			 * Check ipversion to allow IPv4 and IPv6 sockets to
1389 			 * have disjoint port number spaces.
1390 			 */
1391 			if (udp->udp_ipversion != udp1->udp_ipversion) {
1392 
1393 				/*
1394 				 * On the first time through the loop, if the
1395 				 * the user intentionally specified a
1396 				 * particular port number, then ignore any
1397 				 * bindings of the other protocol that may
1398 				 * conflict. This allows the user to bind IPv6
1399 				 * alone and get both v4 and v6, or bind both
1400 				 * both and get each seperately. On subsequent
1401 				 * times through the loop, we're checking a
1402 				 * port that we chose (not the user) and thus
1403 				 * we do not allow casual duplicate bindings.
1404 				 */
1405 				if (count == 0 && requested_port != 0)
1406 					continue;
1407 			}
1408 
1409 			/*
1410 			 * No difference depending on SO_REUSEADDR.
1411 			 *
1412 			 * If existing port is bound to a
1413 			 * non-wildcard IP address and
1414 			 * the requesting stream is bound to
1415 			 * a distinct different IP addresses
1416 			 * (non-wildcard, also), keep going.
1417 			 */
1418 			if (!is_inaddr_any &&
1419 			    !V6_OR_V4_INADDR_ANY(udp1->udp_bound_v6src) &&
1420 			    !IN6_ARE_ADDR_EQUAL(&udp1->udp_bound_v6src,
1421 			    &v6src)) {
1422 				continue;
1423 			}
1424 			break;
1425 		}
1426 
1427 		if (!found_exclbind &&
1428 		    (udp->udp_reuseaddr && requested_port != 0)) {
1429 			break;
1430 		}
1431 
1432 		if (udp1 == NULL) {
1433 			/*
1434 			 * No other stream has this IP address
1435 			 * and port number. We can use it.
1436 			 */
1437 			break;
1438 		}
1439 		mutex_exit(&udpf->uf_lock);
1440 		if (bind_to_req_port_only) {
1441 			/*
1442 			 * We get here only when requested port
1443 			 * is bound (and only first  of the for()
1444 			 * loop iteration).
1445 			 *
1446 			 * The semantics of this bind request
1447 			 * require it to fail so we return from
1448 			 * the routine (and exit the loop).
1449 			 *
1450 			 */
1451 			udp_err_ack(q, mp, TADDRBUSY, 0);
1452 			return;
1453 		}
1454 
1455 		if (udp->udp_anon_priv_bind) {
1456 			port = udp_get_next_priv_port(udp);
1457 		} else {
1458 			if ((count == 0) && (requested_port != 0)) {
1459 				/*
1460 				 * If the application wants us to find
1461 				 * a port, get one to start with. Set
1462 				 * requested_port to 0, so that we will
1463 				 * update udp_g_next_port_to_try below.
1464 				 */
1465 				port = udp_update_next_port(udp,
1466 				    udp_g_next_port_to_try, B_TRUE);
1467 				requested_port = 0;
1468 			} else {
1469 				port = udp_update_next_port(udp, port + 1,
1470 				    B_FALSE);
1471 			}
1472 		}
1473 
1474 		if (port == 0 || ++count >= loopmax) {
1475 			/*
1476 			 * We've tried every possible port number and
1477 			 * there are none available, so send an error
1478 			 * to the user.
1479 			 */
1480 			udp_err_ack(q, mp, TNOADDR, 0);
1481 			return;
1482 		}
1483 	}
1484 
1485 	/*
1486 	 * Copy the source address into our udp structure.  This address
1487 	 * may still be zero; if so, ip will fill in the correct address
1488 	 * each time an outbound packet is passed to it.
1489 	 * If we are binding to a broadcast or multicast address udp_rput
1490 	 * will clear the source address when it receives the T_BIND_ACK.
1491 	 */
1492 	udp->udp_v6src = udp->udp_bound_v6src = v6src;
1493 	udp->udp_port = lport;
1494 	/*
1495 	 * Now reset the the next anonymous port if the application requested
1496 	 * an anonymous port, or we handed out the next anonymous port.
1497 	 */
1498 	if ((requested_port == 0) && (!udp->udp_anon_priv_bind)) {
1499 		udp_g_next_port_to_try = port + 1;
1500 	}
1501 
1502 	/* Initialize the O_T_BIND_REQ/T_BIND_REQ for ip. */
1503 	if (udp->udp_family == AF_INET) {
1504 		sin->sin_port = udp->udp_port;
1505 	} else {
1506 		int error;
1507 
1508 		sin6->sin6_port = udp->udp_port;
1509 		/* Rebuild the header template */
1510 		error = udp_build_hdrs(q, udp);
1511 		if (error != 0) {
1512 			mutex_exit(&udpf->uf_lock);
1513 			udp_err_ack(q, mp, TSYSERR, error);
1514 			return;
1515 		}
1516 	}
1517 	udp->udp_state = TS_IDLE;
1518 	udp_bind_hash_insert(udpf, udp);
1519 	mutex_exit(&udpf->uf_lock);
1520 
1521 	if (cl_inet_bind) {
1522 		/*
1523 		 * Running in cluster mode - register bind information
1524 		 */
1525 		if (udp->udp_ipversion == IPV4_VERSION) {
1526 			(*cl_inet_bind)(IPPROTO_UDP, AF_INET,
1527 			    (uint8_t *)(&V4_PART_OF_V6(udp->udp_v6src)),
1528 			    (in_port_t)udp->udp_port);
1529 		} else {
1530 			(*cl_inet_bind)(IPPROTO_UDP, AF_INET6,
1531 			    (uint8_t *)&(udp->udp_v6src),
1532 			    (in_port_t)udp->udp_port);
1533 		}
1534 
1535 	}
1536 
1537 	connp->conn_anon_port = (is_system_labeled() && requested_port == 0);
1538 	if (is_system_labeled() && (!connp->conn_anon_port ||
1539 	    connp->conn_anon_mlp)) {
1540 		uint16_t mlpport;
1541 		cred_t *cr = connp->conn_cred;
1542 		zone_t *zone;
1543 
1544 		connp->conn_mlp_type = udp->udp_recvucred ? mlptBoth :
1545 		    mlptSingle;
1546 		addrtype = tsol_mlp_addr_type(zoneid, IPV6_VERSION, &v6src);
1547 		if (addrtype == mlptSingle) {
1548 			udp_err_ack(q, mp, TNOADDR, 0);
1549 			connp->conn_anon_port = B_FALSE;
1550 			connp->conn_mlp_type = mlptSingle;
1551 			return;
1552 		}
1553 		mlpport = connp->conn_anon_port ? PMAPPORT : port;
1554 		zone = crgetzone(cr);
1555 		mlptype = tsol_mlp_port_type(zone, IPPROTO_UDP, mlpport,
1556 		    addrtype);
1557 		if (mlptype != mlptSingle &&
1558 		    (connp->conn_mlp_type == mlptSingle ||
1559 		    secpolicy_net_bindmlp(cr) != 0)) {
1560 			if (udp->udp_debug) {
1561 				(void) strlog(UDP_MOD_ID, 0, 1,
1562 				    SL_ERROR|SL_TRACE,
1563 				    "udp_bind: no priv for multilevel port %d",
1564 				    mlpport);
1565 			}
1566 			udp_err_ack(q, mp, TACCES, 0);
1567 			connp->conn_anon_port = B_FALSE;
1568 			connp->conn_mlp_type = mlptSingle;
1569 			return;
1570 		}
1571 
1572 		/*
1573 		 * If we're specifically binding a shared IP address and the
1574 		 * port is MLP on shared addresses, then check to see if this
1575 		 * zone actually owns the MLP.  Reject if not.
1576 		 */
1577 		if (mlptype == mlptShared && addrtype == mlptShared) {
1578 			zoneid_t mlpzone;
1579 
1580 			mlpzone = tsol_mlp_findzone(IPPROTO_UDP,
1581 			    htons(mlpport));
1582 			if (connp->conn_zoneid != mlpzone) {
1583 				if (udp->udp_debug) {
1584 					(void) strlog(UDP_MOD_ID, 0, 1,
1585 					    SL_ERROR|SL_TRACE,
1586 					    "udp_bind: attempt to bind port "
1587 					    "%d on shared addr in zone %d "
1588 					    "(should be %d)",
1589 					    mlpport, connp->conn_zoneid,
1590 					    mlpzone);
1591 				}
1592 				udp_err_ack(q, mp, TACCES, 0);
1593 				connp->conn_anon_port = B_FALSE;
1594 				connp->conn_mlp_type = mlptSingle;
1595 				return;
1596 			}
1597 		}
1598 		if (connp->conn_anon_port) {
1599 			int error;
1600 
1601 			error = tsol_mlp_anon(zone, mlptype, connp->conn_ulp,
1602 			    port, B_TRUE);
1603 			if (error != 0) {
1604 				if (udp->udp_debug) {
1605 					(void) strlog(UDP_MOD_ID, 0, 1,
1606 					    SL_ERROR|SL_TRACE,
1607 					    "udp_bind: cannot establish anon "
1608 					    "MLP for port %d", port);
1609 				}
1610 				udp_err_ack(q, mp, TACCES, 0);
1611 				connp->conn_anon_port = B_FALSE;
1612 				connp->conn_mlp_type = mlptSingle;
1613 				return;
1614 			}
1615 		}
1616 		connp->conn_mlp_type = mlptype;
1617 	}
1618 
1619 	/* Pass the protocol number in the message following the address. */
1620 	*mp->b_wptr++ = IPPROTO_UDP;
1621 	if (!V6_OR_V4_INADDR_ANY(udp->udp_v6src)) {
1622 		/*
1623 		 * Append a request for an IRE if udp_v6src not
1624 		 * zero (IPv4 - INADDR_ANY, or IPv6 - all-zeroes address).
1625 		 */
1626 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
1627 		if (!mp->b_cont) {
1628 			udp_err_ack(q, mp, TSYSERR, ENOMEM);
1629 			return;
1630 		}
1631 		mp->b_cont->b_wptr += sizeof (ire_t);
1632 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
1633 	}
1634 	if (udp->udp_family == AF_INET6)
1635 		mp = ip_bind_v6(q, mp, connp, NULL);
1636 	else
1637 		mp = ip_bind_v4(q, mp, connp);
1638 
1639 	if (mp != NULL)
1640 		udp_rput_other(_RD(q), mp);
1641 	else
1642 		CONN_INC_REF(connp);
1643 }
1644 
1645 
1646 void
1647 udp_resume_bind(conn_t *connp, mblk_t *mp)
1648 {
1649 	udp_enter(connp, mp, udp_resume_bind_cb, SQTAG_BIND_RETRY);
1650 }
1651 
1652 /*
1653  * This is called from ip_wput_nondata to resume a deferred UDP bind.
1654  */
1655 /* ARGSUSED */
1656 static void
1657 udp_resume_bind_cb(void *arg, mblk_t *mp, void *arg2)
1658 {
1659 	conn_t *connp = arg;
1660 
1661 	ASSERT(connp != NULL && IPCL_IS_UDP(connp));
1662 
1663 	udp_rput_other(connp->conn_rq, mp);
1664 
1665 	CONN_OPER_PENDING_DONE(connp);
1666 	udp_exit(connp);
1667 }
1668 
1669 /*
1670  * This routine handles each T_CONN_REQ message passed to udp.  It
1671  * associates a default destination address with the stream.
1672  *
1673  * This routine sends down a T_BIND_REQ to IP with the following mblks:
1674  *	T_BIND_REQ	- specifying local and remote address/port
1675  *	IRE_DB_REQ_TYPE	- to get an IRE back containing ire_type and src
1676  *	T_OK_ACK	- for the T_CONN_REQ
1677  *	T_CONN_CON	- to keep the TPI user happy
1678  *
1679  * The connect completes in udp_rput.
1680  * When a T_BIND_ACK is received information is extracted from the IRE
1681  * and the two appended messages are sent to the TPI user.
1682  * Should udp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert
1683  * it to an error ack for the appropriate primitive.
1684  */
1685 static void
1686 udp_connect(queue_t *q, mblk_t *mp)
1687 {
1688 	sin6_t	*sin6;
1689 	sin_t	*sin;
1690 	struct T_conn_req	*tcr;
1691 	in6_addr_t v6dst;
1692 	ipaddr_t v4dst;
1693 	uint16_t dstport;
1694 	uint32_t flowinfo;
1695 	mblk_t	*mp1, *mp2;
1696 	udp_fanout_t	*udpf;
1697 	udp_t	*udp, *udp1;
1698 
1699 	udp = Q_TO_UDP(q);
1700 
1701 	tcr = (struct T_conn_req *)mp->b_rptr;
1702 
1703 	/* A bit of sanity checking */
1704 	if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_req)) {
1705 		udp_err_ack(q, mp, TPROTO, 0);
1706 		return;
1707 	}
1708 	/*
1709 	 * This UDP must have bound to a port already before doing
1710 	 * a connect.
1711 	 */
1712 	if (udp->udp_state == TS_UNBND) {
1713 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
1714 		    "udp_connect: bad state, %u", udp->udp_state);
1715 		udp_err_ack(q, mp, TOUTSTATE, 0);
1716 		return;
1717 	}
1718 	ASSERT(udp->udp_port != 0 && udp->udp_ptpbhn != NULL);
1719 
1720 	udpf = &udp_bind_fanout[UDP_BIND_HASH(udp->udp_port)];
1721 
1722 	if (udp->udp_state == TS_DATA_XFER) {
1723 		/* Already connected - clear out state */
1724 		mutex_enter(&udpf->uf_lock);
1725 		udp->udp_v6src = udp->udp_bound_v6src;
1726 		udp->udp_state = TS_IDLE;
1727 		mutex_exit(&udpf->uf_lock);
1728 	}
1729 
1730 	if (tcr->OPT_length != 0) {
1731 		udp_err_ack(q, mp, TBADOPT, 0);
1732 		return;
1733 	}
1734 
1735 	/*
1736 	 * Determine packet type based on type of address passed in
1737 	 * the request should contain an IPv4 or IPv6 address.
1738 	 * Make sure that address family matches the type of
1739 	 * family of the the address passed down
1740 	 */
1741 	switch (tcr->DEST_length) {
1742 	default:
1743 		udp_err_ack(q, mp, TBADADDR, 0);
1744 		return;
1745 
1746 	case sizeof (sin_t):
1747 		sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset,
1748 		    sizeof (sin_t));
1749 		if (sin == NULL || !OK_32PTR((char *)sin)) {
1750 			udp_err_ack(q, mp, TSYSERR, EINVAL);
1751 			return;
1752 		}
1753 		if (udp->udp_family != AF_INET ||
1754 		    sin->sin_family != AF_INET) {
1755 			udp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
1756 			return;
1757 		}
1758 		v4dst = sin->sin_addr.s_addr;
1759 		dstport = sin->sin_port;
1760 		IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
1761 		ASSERT(udp->udp_ipversion == IPV4_VERSION);
1762 		udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + UDPH_SIZE +
1763 		    udp->udp_ip_snd_options_len;
1764 		break;
1765 
1766 	case sizeof (sin6_t):
1767 		sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset,
1768 		    sizeof (sin6_t));
1769 		if (sin6 == NULL || !OK_32PTR((char *)sin6)) {
1770 			udp_err_ack(q, mp, TSYSERR, EINVAL);
1771 			return;
1772 		}
1773 		if (udp->udp_family != AF_INET6 ||
1774 		    sin6->sin6_family != AF_INET6) {
1775 			udp_err_ack(q, mp, TSYSERR, EAFNOSUPPORT);
1776 			return;
1777 		}
1778 		v6dst = sin6->sin6_addr;
1779 		if (IN6_IS_ADDR_V4MAPPED(&v6dst)) {
1780 			IN6_V4MAPPED_TO_IPADDR(&v6dst, v4dst);
1781 			udp->udp_ipversion = IPV4_VERSION;
1782 			udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
1783 			    UDPH_SIZE + udp->udp_ip_snd_options_len;
1784 			flowinfo = 0;
1785 		} else {
1786 			udp->udp_ipversion = IPV6_VERSION;
1787 			udp->udp_max_hdr_len = udp->udp_sticky_hdrs_len;
1788 			flowinfo = sin6->sin6_flowinfo;
1789 		}
1790 		dstport = sin6->sin6_port;
1791 		break;
1792 	}
1793 	if (dstport == 0) {
1794 		udp_err_ack(q, mp, TBADADDR, 0);
1795 		return;
1796 	}
1797 
1798 	/*
1799 	 * Create a default IP header with no IP options.
1800 	 */
1801 	udp->udp_dstport = dstport;
1802 	if (udp->udp_ipversion == IPV4_VERSION) {
1803 		/*
1804 		 * Interpret a zero destination to mean loopback.
1805 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
1806 		 * generate the T_CONN_CON.
1807 		 */
1808 		if (v4dst == INADDR_ANY) {
1809 			v4dst = htonl(INADDR_LOOPBACK);
1810 			IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
1811 			if (udp->udp_family == AF_INET) {
1812 				sin->sin_addr.s_addr = v4dst;
1813 			} else {
1814 				sin6->sin6_addr = v6dst;
1815 			}
1816 		}
1817 		udp->udp_v6dst = v6dst;
1818 		udp->udp_flowinfo = 0;
1819 
1820 		/*
1821 		 * If the destination address is multicast and
1822 		 * an outgoing multicast interface has been set,
1823 		 * use the address of that interface as our
1824 		 * source address if no source address has been set.
1825 		 */
1826 		if (V4_PART_OF_V6(udp->udp_v6src) == INADDR_ANY &&
1827 		    CLASSD(v4dst) &&
1828 		    udp->udp_multicast_if_addr != INADDR_ANY) {
1829 			IN6_IPADDR_TO_V4MAPPED(udp->udp_multicast_if_addr,
1830 			    &udp->udp_v6src);
1831 		}
1832 	} else {
1833 		ASSERT(udp->udp_ipversion == IPV6_VERSION);
1834 		/*
1835 		 * Interpret a zero destination to mean loopback.
1836 		 * Update the T_CONN_REQ (sin/sin6) since it is used to
1837 		 * generate the T_CONN_CON.
1838 		 */
1839 		if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) {
1840 			v6dst = ipv6_loopback;
1841 			sin6->sin6_addr = v6dst;
1842 		}
1843 		udp->udp_v6dst = v6dst;
1844 		udp->udp_flowinfo = flowinfo;
1845 		/*
1846 		 * If the destination address is multicast and
1847 		 * an outgoing multicast interface has been set,
1848 		 * then the ip bind logic will pick the correct source
1849 		 * address (i.e. matching the outgoing multicast interface).
1850 		 */
1851 	}
1852 
1853 	/*
1854 	 * Verify that the src/port/dst/port is unique for all
1855 	 * connections in TS_DATA_XFER
1856 	 */
1857 	mutex_enter(&udpf->uf_lock);
1858 	for (udp1 = udpf->uf_udp; udp1 != NULL; udp1 = udp1->udp_bind_hash) {
1859 		if (udp1->udp_state != TS_DATA_XFER)
1860 			continue;
1861 		if (udp->udp_port != udp1->udp_port ||
1862 		    udp->udp_ipversion != udp1->udp_ipversion ||
1863 		    dstport != udp1->udp_dstport ||
1864 		    !IN6_ARE_ADDR_EQUAL(&udp->udp_v6src, &udp1->udp_v6src) ||
1865 		    !IN6_ARE_ADDR_EQUAL(&v6dst, &udp1->udp_v6dst))
1866 			continue;
1867 		mutex_exit(&udpf->uf_lock);
1868 		udp_err_ack(q, mp, TBADADDR, 0);
1869 		return;
1870 	}
1871 	udp->udp_state = TS_DATA_XFER;
1872 	mutex_exit(&udpf->uf_lock);
1873 
1874 	/*
1875 	 * Send down bind to IP to verify that there is a route
1876 	 * and to determine the source address.
1877 	 * This will come back as T_BIND_ACK with an IRE_DB_TYPE in rput.
1878 	 */
1879 	if (udp->udp_family == AF_INET)
1880 		mp1 = udp_ip_bind_mp(udp, O_T_BIND_REQ, sizeof (ipa_conn_t));
1881 	else
1882 		mp1 = udp_ip_bind_mp(udp, O_T_BIND_REQ, sizeof (ipa6_conn_t));
1883 	if (mp1 == NULL) {
1884 		udp_err_ack(q, mp, TSYSERR, ENOMEM);
1885 bind_failed:
1886 		mutex_enter(&udpf->uf_lock);
1887 		udp->udp_state = TS_IDLE;
1888 		mutex_exit(&udpf->uf_lock);
1889 		return;
1890 	}
1891 
1892 	/*
1893 	 * We also have to send a connection confirmation to
1894 	 * keep TLI happy. Prepare it for udp_rput.
1895 	 */
1896 	if (udp->udp_family == AF_INET)
1897 		mp2 = mi_tpi_conn_con(NULL, (char *)sin,
1898 		    sizeof (*sin), NULL, 0);
1899 	else
1900 		mp2 = mi_tpi_conn_con(NULL, (char *)sin6,
1901 		    sizeof (*sin6), NULL, 0);
1902 	if (mp2 == NULL) {
1903 		freemsg(mp1);
1904 		udp_err_ack(q, mp, TSYSERR, ENOMEM);
1905 		goto bind_failed;
1906 	}
1907 
1908 	mp = mi_tpi_ok_ack_alloc(mp);
1909 	if (mp == NULL) {
1910 		/* Unable to reuse the T_CONN_REQ for the ack. */
1911 		freemsg(mp2);
1912 		udp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM);
1913 		goto bind_failed;
1914 	}
1915 
1916 	/* Hang onto the T_OK_ACK and T_CONN_CON for later. */
1917 	linkb(mp1, mp);
1918 	linkb(mp1, mp2);
1919 
1920 	mblk_setcred(mp1, udp->udp_connp->conn_cred);
1921 	if (udp->udp_family == AF_INET)
1922 		mp1 = ip_bind_v4(q, mp1, udp->udp_connp);
1923 	else
1924 		mp1 = ip_bind_v6(q, mp1, udp->udp_connp, NULL);
1925 
1926 	if (mp1 != NULL)
1927 		udp_rput_other(_RD(q), mp1);
1928 	else
1929 		CONN_INC_REF(udp->udp_connp);
1930 }
1931 
1932 static int
1933 udp_close(queue_t *q)
1934 {
1935 	conn_t	*connp = Q_TO_CONN(UDP_WR(q));
1936 	udp_t	*udp;
1937 	queue_t	*ip_rq = RD(UDP_WR(q));
1938 
1939 	ASSERT(connp != NULL && IPCL_IS_UDP(connp));
1940 	udp = connp->conn_udp;
1941 
1942 	ip_quiesce_conn(connp);
1943 	/*
1944 	 * Disable read-side synchronous stream
1945 	 * interface and drain any queued data.
1946 	 */
1947 	udp_rcv_drain(q, udp, B_TRUE);
1948 	ASSERT(!udp->udp_direct_sockfs);
1949 
1950 	qprocsoff(q);
1951 
1952 	/* restore IP module's high and low water marks to default values */
1953 	ip_rq->q_hiwat = ip_rq->q_qinfo->qi_minfo->mi_hiwat;
1954 	WR(ip_rq)->q_hiwat = WR(ip_rq)->q_qinfo->qi_minfo->mi_hiwat;
1955 	WR(ip_rq)->q_lowat = WR(ip_rq)->q_qinfo->qi_minfo->mi_lowat;
1956 
1957 	ASSERT(udp->udp_rcv_cnt == 0);
1958 	ASSERT(udp->udp_rcv_msgcnt == 0);
1959 	ASSERT(udp->udp_rcv_list_head == NULL);
1960 	ASSERT(udp->udp_rcv_list_tail == NULL);
1961 
1962 	/* connp is now single threaded. */
1963 	udp_close_free(connp);
1964 	/*
1965 	 * Restore connp as an IP endpoint.  We don't need
1966 	 * any locks since we are now single threaded
1967 	 */
1968 	connp->conn_flags &= ~IPCL_UDP;
1969 	connp->conn_state_flags &=
1970 	    ~(CONN_CLOSING | CONN_CONDEMNED | CONN_QUIESCED);
1971 	connp->conn_ulp_labeled = B_FALSE;
1972 	return (0);
1973 }
1974 
1975 /*
1976  * Called in the close path from IP (ip_quiesce_conn) to quiesce the conn
1977  */
1978 void
1979 udp_quiesce_conn(conn_t *connp)
1980 {
1981 	udp_t	*udp = connp->conn_udp;
1982 
1983 	if (cl_inet_unbind != NULL && udp->udp_state == TS_IDLE) {
1984 		/*
1985 		 * Running in cluster mode - register unbind information
1986 		 */
1987 		if (udp->udp_ipversion == IPV4_VERSION) {
1988 			(*cl_inet_unbind)(IPPROTO_UDP, AF_INET,
1989 			    (uint8_t *)(&(V4_PART_OF_V6(udp->udp_v6src))),
1990 			    (in_port_t)udp->udp_port);
1991 		} else {
1992 			(*cl_inet_unbind)(IPPROTO_UDP, AF_INET6,
1993 			    (uint8_t *)(&(udp->udp_v6src)),
1994 			    (in_port_t)udp->udp_port);
1995 		}
1996 	}
1997 
1998 	udp_bind_hash_remove(udp, B_FALSE);
1999 
2000 	mutex_enter(&connp->conn_lock);
2001 	while (udp->udp_reader_count != 0 || udp->udp_squeue_count != 0 ||
2002 	    udp->udp_mode != UDP_MT_HOT) {
2003 		cv_wait(&connp->conn_cv, &connp->conn_lock);
2004 	}
2005 	mutex_exit(&connp->conn_lock);
2006 }
2007 
2008 void
2009 udp_close_free(conn_t *connp)
2010 {
2011 	udp_t *udp = connp->conn_udp;
2012 
2013 	/* If there are any options associated with the stream, free them. */
2014 	if (udp->udp_ip_snd_options) {
2015 		mi_free((char *)udp->udp_ip_snd_options);
2016 		udp->udp_ip_snd_options = NULL;
2017 	}
2018 
2019 	if (udp->udp_ip_rcv_options) {
2020 		mi_free((char *)udp->udp_ip_rcv_options);
2021 		udp->udp_ip_rcv_options = NULL;
2022 	}
2023 
2024 	/* Free memory associated with sticky options */
2025 	if (udp->udp_sticky_hdrs_len != 0) {
2026 		kmem_free(udp->udp_sticky_hdrs,
2027 		    udp->udp_sticky_hdrs_len);
2028 		udp->udp_sticky_hdrs = NULL;
2029 		udp->udp_sticky_hdrs_len = 0;
2030 	}
2031 
2032 	ip6_pkt_free(&udp->udp_sticky_ipp);
2033 
2034 	udp->udp_connp = NULL;
2035 	connp->conn_udp = NULL;
2036 	kmem_cache_free(udp_cache, udp);
2037 }
2038 
2039 /*
2040  * This routine handles each T_DISCON_REQ message passed to udp
2041  * as an indicating that UDP is no longer connected. This results
2042  * in sending a T_BIND_REQ to IP to restore the binding to just
2043  * the local address/port.
2044  *
2045  * This routine sends down a T_BIND_REQ to IP with the following mblks:
2046  *	T_BIND_REQ	- specifying just the local address/port
2047  *	T_OK_ACK	- for the T_DISCON_REQ
2048  *
2049  * The disconnect completes in udp_rput.
2050  * When a T_BIND_ACK is received the appended T_OK_ACK is sent to the TPI user.
2051  * Should udp_rput receive T_ERROR_ACK for the T_BIND_REQ it will convert
2052  * it to an error ack for the appropriate primitive.
2053  */
2054 static void
2055 udp_disconnect(queue_t *q, mblk_t *mp)
2056 {
2057 	udp_t	*udp = Q_TO_UDP(q);
2058 	mblk_t	*mp1;
2059 	udp_fanout_t *udpf;
2060 
2061 	if (udp->udp_state != TS_DATA_XFER) {
2062 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
2063 		    "udp_disconnect: bad state, %u", udp->udp_state);
2064 		udp_err_ack(q, mp, TOUTSTATE, 0);
2065 		return;
2066 	}
2067 	udpf = &udp_bind_fanout[UDP_BIND_HASH(udp->udp_port)];
2068 	mutex_enter(&udpf->uf_lock);
2069 	udp->udp_v6src = udp->udp_bound_v6src;
2070 	udp->udp_state = TS_IDLE;
2071 	mutex_exit(&udpf->uf_lock);
2072 
2073 	/*
2074 	 * Send down bind to IP to remove the full binding and revert
2075 	 * to the local address binding.
2076 	 */
2077 	if (udp->udp_family == AF_INET)
2078 		mp1 = udp_ip_bind_mp(udp, O_T_BIND_REQ, sizeof (sin_t));
2079 	else
2080 		mp1 = udp_ip_bind_mp(udp, O_T_BIND_REQ, sizeof (sin6_t));
2081 	if (mp1 == NULL) {
2082 		udp_err_ack(q, mp, TSYSERR, ENOMEM);
2083 		return;
2084 	}
2085 	mp = mi_tpi_ok_ack_alloc(mp);
2086 	if (mp == NULL) {
2087 		/* Unable to reuse the T_DISCON_REQ for the ack. */
2088 		udp_err_ack_prim(q, mp1, T_DISCON_REQ, TSYSERR, ENOMEM);
2089 		return;
2090 	}
2091 
2092 	if (udp->udp_family == AF_INET6) {
2093 		int error;
2094 
2095 		/* Rebuild the header template */
2096 		error = udp_build_hdrs(q, udp);
2097 		if (error != 0) {
2098 			udp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, error);
2099 			freemsg(mp1);
2100 			return;
2101 		}
2102 	}
2103 	mutex_enter(&udpf->uf_lock);
2104 	udp->udp_discon_pending = 1;
2105 	mutex_exit(&udpf->uf_lock);
2106 
2107 	/* Append the T_OK_ACK to the T_BIND_REQ for udp_rput */
2108 	linkb(mp1, mp);
2109 
2110 	if (udp->udp_family == AF_INET6)
2111 		mp1 = ip_bind_v6(q, mp1, udp->udp_connp, NULL);
2112 	else
2113 		mp1 = ip_bind_v4(q, mp1, udp->udp_connp);
2114 
2115 	if (mp1 != NULL)
2116 		udp_rput_other(_RD(q), mp1);
2117 	else
2118 		CONN_INC_REF(udp->udp_connp);
2119 }
2120 
2121 /* This routine creates a T_ERROR_ACK message and passes it upstream. */
2122 static void
2123 udp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
2124 {
2125 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
2126 		putnext(UDP_RD(q), mp);
2127 }
2128 
2129 /* Shorthand to generate and send TPI error acks to our client */
2130 static void
2131 udp_err_ack_prim(queue_t *q, mblk_t *mp, int primitive, t_scalar_t t_error,
2132     int sys_error)
2133 {
2134 	struct T_error_ack	*teackp;
2135 
2136 	if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
2137 	    M_PCPROTO, T_ERROR_ACK)) != NULL) {
2138 		teackp = (struct T_error_ack *)mp->b_rptr;
2139 		teackp->ERROR_prim = primitive;
2140 		teackp->TLI_error = t_error;
2141 		teackp->UNIX_error = sys_error;
2142 		putnext(UDP_RD(q), mp);
2143 	}
2144 }
2145 
2146 /*ARGSUSED*/
2147 static int
2148 udp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
2149 {
2150 	int i;
2151 
2152 	for (i = 0; i < udp_g_num_epriv_ports; i++) {
2153 		if (udp_g_epriv_ports[i] != 0)
2154 			(void) mi_mpprintf(mp, "%d ", udp_g_epriv_ports[i]);
2155 	}
2156 	return (0);
2157 }
2158 
2159 /* ARGSUSED */
2160 static int
2161 udp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp,
2162     cred_t *cr)
2163 {
2164 	long	new_value;
2165 	int	i;
2166 
2167 	/*
2168 	 * Fail the request if the new value does not lie within the
2169 	 * port number limits.
2170 	 */
2171 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
2172 	    new_value <= 0 || new_value >= 65536) {
2173 		return (EINVAL);
2174 	}
2175 
2176 	/* Check if the value is already in the list */
2177 	for (i = 0; i < udp_g_num_epriv_ports; i++) {
2178 		if (new_value == udp_g_epriv_ports[i]) {
2179 			return (EEXIST);
2180 		}
2181 	}
2182 	/* Find an empty slot */
2183 	for (i = 0; i < udp_g_num_epriv_ports; i++) {
2184 		if (udp_g_epriv_ports[i] == 0)
2185 			break;
2186 	}
2187 	if (i == udp_g_num_epriv_ports) {
2188 		return (EOVERFLOW);
2189 	}
2190 
2191 	/* Set the new value */
2192 	udp_g_epriv_ports[i] = (in_port_t)new_value;
2193 	return (0);
2194 }
2195 
2196 /* ARGSUSED */
2197 static int
2198 udp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp,
2199     cred_t *cr)
2200 {
2201 	long	new_value;
2202 	int	i;
2203 
2204 	/*
2205 	 * Fail the request if the new value does not lie within the
2206 	 * port number limits.
2207 	 */
2208 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
2209 	    new_value <= 0 || new_value >= 65536) {
2210 		return (EINVAL);
2211 	}
2212 
2213 	/* Check that the value is already in the list */
2214 	for (i = 0; i < udp_g_num_epriv_ports; i++) {
2215 		if (udp_g_epriv_ports[i] == new_value)
2216 			break;
2217 	}
2218 	if (i == udp_g_num_epriv_ports) {
2219 		return (ESRCH);
2220 	}
2221 
2222 	/* Clear the value */
2223 	udp_g_epriv_ports[i] = 0;
2224 	return (0);
2225 }
2226 
2227 /* At minimum we need 4 bytes of UDP header */
2228 #define	ICMP_MIN_UDP_HDR	4
2229 
2230 /*
2231  * udp_icmp_error is called by udp_rput to process ICMP msgs. passed up by IP.
2232  * Generates the appropriate T_UDERROR_IND for permanent (non-transient) errors.
2233  * Assumes that IP has pulled up everything up to and including the ICMP header.
2234  * An M_CTL could potentially come here from some other module (i.e. if UDP
2235  * is pushed on some module other than IP). Thus, if we find that the M_CTL
2236  * does not have enough ICMP information , following STREAMS conventions,
2237  * we send it upstream assuming it is an M_CTL we don't understand.
2238  */
2239 static void
2240 udp_icmp_error(queue_t *q, mblk_t *mp)
2241 {
2242 	icmph_t *icmph;
2243 	ipha_t	*ipha;
2244 	int	iph_hdr_length;
2245 	udpha_t	*udpha;
2246 	sin_t	sin;
2247 	sin6_t	sin6;
2248 	mblk_t	*mp1;
2249 	int	error = 0;
2250 	size_t	mp_size = MBLKL(mp);
2251 	udp_t	*udp = Q_TO_UDP(q);
2252 
2253 	/*
2254 	 * Assume IP provides aligned packets - otherwise toss
2255 	 */
2256 	if (!OK_32PTR(mp->b_rptr)) {
2257 		freemsg(mp);
2258 		return;
2259 	}
2260 
2261 	/*
2262 	 * Verify that we have a complete IP header and the application has
2263 	 * asked for errors. If not, send it upstream.
2264 	 */
2265 	if (!udp->udp_dgram_errind || mp_size < sizeof (ipha_t)) {
2266 noticmpv4:
2267 		putnext(UDP_RD(q), mp);
2268 		return;
2269 	}
2270 
2271 	ipha = (ipha_t *)mp->b_rptr;
2272 	/*
2273 	 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent
2274 	 * upstream. ICMPv6  is handled in udp_icmp_error_ipv6.
2275 	 */
2276 	switch (IPH_HDR_VERSION(ipha)) {
2277 	case IPV6_VERSION:
2278 		udp_icmp_error_ipv6(q, mp);
2279 		return;
2280 	case IPV4_VERSION:
2281 		break;
2282 	default:
2283 		goto noticmpv4;
2284 	}
2285 
2286 	/* Skip past the outer IP and ICMP headers */
2287 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
2288 	icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2289 	/*
2290 	 * If we don't have the correct outer IP header length or if the ULP
2291 	 * is not IPPROTO_ICMP or if we don't have a complete inner IP header
2292 	 * send the packet upstream.
2293 	 */
2294 	if (iph_hdr_length < sizeof (ipha_t) ||
2295 	    ipha->ipha_protocol != IPPROTO_ICMP ||
2296 	    (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) {
2297 		goto noticmpv4;
2298 	}
2299 	ipha = (ipha_t *)&icmph[1];
2300 
2301 	/* Skip past the inner IP and find the ULP header */
2302 	iph_hdr_length = IPH_HDR_LENGTH(ipha);
2303 	udpha = (udpha_t *)((char *)ipha + iph_hdr_length);
2304 	/*
2305 	 * If we don't have the correct inner IP header length or if the ULP
2306 	 * is not IPPROTO_UDP or if we don't have at least ICMP_MIN_UDP_HDR
2307 	 * bytes of UDP header, send it upstream.
2308 	 */
2309 	if (iph_hdr_length < sizeof (ipha_t) ||
2310 	    ipha->ipha_protocol != IPPROTO_UDP ||
2311 	    (uchar_t *)udpha + ICMP_MIN_UDP_HDR > mp->b_wptr) {
2312 		goto noticmpv4;
2313 	}
2314 
2315 	switch (icmph->icmph_type) {
2316 	case ICMP_DEST_UNREACHABLE:
2317 		switch (icmph->icmph_code) {
2318 		case ICMP_FRAGMENTATION_NEEDED:
2319 			/*
2320 			 * IP has already adjusted the path MTU.
2321 			 * XXX Somehow pass MTU indication to application?
2322 			 */
2323 			break;
2324 		case ICMP_PORT_UNREACHABLE:
2325 		case ICMP_PROTOCOL_UNREACHABLE:
2326 			error = ECONNREFUSED;
2327 			break;
2328 		default:
2329 			/* Transient errors */
2330 			break;
2331 		}
2332 		break;
2333 	default:
2334 		/* Transient errors */
2335 		break;
2336 	}
2337 	if (error == 0) {
2338 		freemsg(mp);
2339 		return;
2340 	}
2341 
2342 	switch (udp->udp_family) {
2343 	case AF_INET:
2344 		sin = sin_null;
2345 		sin.sin_family = AF_INET;
2346 		sin.sin_addr.s_addr = ipha->ipha_dst;
2347 		sin.sin_port = udpha->uha_dst_port;
2348 		mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0,
2349 		    error);
2350 		break;
2351 	case AF_INET6:
2352 		sin6 = sin6_null;
2353 		sin6.sin6_family = AF_INET6;
2354 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &sin6.sin6_addr);
2355 		sin6.sin6_port = udpha->uha_dst_port;
2356 
2357 		mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t),
2358 		    NULL, 0, error);
2359 		break;
2360 	}
2361 	if (mp1)
2362 		putnext(UDP_RD(q), mp1);
2363 	freemsg(mp);
2364 }
2365 
2366 /*
2367  * udp_icmp_error_ipv6 is called by udp_icmp_error to process ICMP for IPv6.
2368  * Generates the appropriate T_UDERROR_IND for permanent (non-transient) errors.
2369  * Assumes that IP has pulled up all the extension headers as well as the
2370  * ICMPv6 header.
2371  * An M_CTL could potentially come here from some other module (i.e. if UDP
2372  * is pushed on some module other than IP). Thus, if we find that the M_CTL
2373  * does not have enough ICMP information , following STREAMS conventions,
2374  * we send it upstream assuming it is an M_CTL we don't understand. The reason
2375  * it might get here is if the non-ICMP M_CTL accidently has 6 in the version
2376  * field (when cast to ipha_t in udp_icmp_error).
2377  */
2378 static void
2379 udp_icmp_error_ipv6(queue_t *q, mblk_t *mp)
2380 {
2381 	icmp6_t		*icmp6;
2382 	ip6_t		*ip6h, *outer_ip6h;
2383 	uint16_t	hdr_length;
2384 	uint8_t		*nexthdrp;
2385 	udpha_t		*udpha;
2386 	sin6_t		sin6;
2387 	mblk_t		*mp1;
2388 	int		error = 0;
2389 	size_t		mp_size = MBLKL(mp);
2390 	udp_t		*udp = Q_TO_UDP(q);
2391 
2392 	/*
2393 	 * Verify that we have a complete IP header. If not, send it upstream.
2394 	 */
2395 	if (mp_size < sizeof (ip6_t)) {
2396 noticmpv6:
2397 		putnext(UDP_RD(q), mp);
2398 		return;
2399 	}
2400 
2401 	outer_ip6h = (ip6_t *)mp->b_rptr;
2402 	/*
2403 	 * Verify this is an ICMPV6 packet, else send it upstream
2404 	 */
2405 	if (outer_ip6h->ip6_nxt == IPPROTO_ICMPV6) {
2406 		hdr_length = IPV6_HDR_LEN;
2407 	} else if (!ip_hdr_length_nexthdr_v6(mp, outer_ip6h, &hdr_length,
2408 	    &nexthdrp) ||
2409 	    *nexthdrp != IPPROTO_ICMPV6) {
2410 		goto noticmpv6;
2411 	}
2412 	icmp6 = (icmp6_t *)&mp->b_rptr[hdr_length];
2413 	ip6h = (ip6_t *)&icmp6[1];
2414 	/*
2415 	 * Verify we have a complete ICMP and inner IP header.
2416 	 */
2417 	if ((uchar_t *)&ip6h[1] > mp->b_wptr)
2418 		goto noticmpv6;
2419 
2420 	if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp))
2421 		goto noticmpv6;
2422 	udpha = (udpha_t *)((char *)ip6h + hdr_length);
2423 	/*
2424 	 * Validate inner header. If the ULP is not IPPROTO_UDP or if we don't
2425 	 * have at least ICMP_MIN_UDP_HDR bytes of  UDP header send the
2426 	 * packet upstream.
2427 	 */
2428 	if ((*nexthdrp != IPPROTO_UDP) ||
2429 	    ((uchar_t *)udpha + ICMP_MIN_UDP_HDR) > mp->b_wptr) {
2430 		goto noticmpv6;
2431 	}
2432 
2433 	switch (icmp6->icmp6_type) {
2434 	case ICMP6_DST_UNREACH:
2435 		switch (icmp6->icmp6_code) {
2436 		case ICMP6_DST_UNREACH_NOPORT:
2437 			error = ECONNREFUSED;
2438 			break;
2439 		case ICMP6_DST_UNREACH_ADMIN:
2440 		case ICMP6_DST_UNREACH_NOROUTE:
2441 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
2442 		case ICMP6_DST_UNREACH_ADDR:
2443 			/* Transient errors */
2444 			break;
2445 		default:
2446 			break;
2447 		}
2448 		break;
2449 	case ICMP6_PACKET_TOO_BIG: {
2450 		struct T_unitdata_ind	*tudi;
2451 		struct T_opthdr		*toh;
2452 		size_t			udi_size;
2453 		mblk_t			*newmp;
2454 		t_scalar_t		opt_length = sizeof (struct T_opthdr) +
2455 		    sizeof (struct ip6_mtuinfo);
2456 		sin6_t			*sin6;
2457 		struct ip6_mtuinfo	*mtuinfo;
2458 
2459 		/*
2460 		 * If the application has requested to receive path mtu
2461 		 * information, send up an empty message containing an
2462 		 * IPV6_PATHMTU ancillary data item.
2463 		 */
2464 		if (!udp->udp_ipv6_recvpathmtu)
2465 			break;
2466 
2467 		udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) +
2468 		    opt_length;
2469 		if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) {
2470 			BUMP_MIB(&udp_mib, udpInErrors);
2471 			break;
2472 		}
2473 
2474 		/*
2475 		 * newmp->b_cont is left to NULL on purpose.  This is an
2476 		 * empty message containing only ancillary data.
2477 		 */
2478 		newmp->b_datap->db_type = M_PROTO;
2479 		tudi = (struct T_unitdata_ind *)newmp->b_rptr;
2480 		newmp->b_wptr = (uchar_t *)tudi + udi_size;
2481 		tudi->PRIM_type = T_UNITDATA_IND;
2482 		tudi->SRC_length = sizeof (sin6_t);
2483 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
2484 		tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t);
2485 		tudi->OPT_length = opt_length;
2486 
2487 		sin6 = (sin6_t *)&tudi[1];
2488 		bzero(sin6, sizeof (sin6_t));
2489 		sin6->sin6_family = AF_INET6;
2490 		sin6->sin6_addr = udp->udp_v6dst;
2491 
2492 		toh = (struct T_opthdr *)&sin6[1];
2493 		toh->level = IPPROTO_IPV6;
2494 		toh->name = IPV6_PATHMTU;
2495 		toh->len = opt_length;
2496 		toh->status = 0;
2497 
2498 		mtuinfo = (struct ip6_mtuinfo *)&toh[1];
2499 		bzero(mtuinfo, sizeof (struct ip6_mtuinfo));
2500 		mtuinfo->ip6m_addr.sin6_family = AF_INET6;
2501 		mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst;
2502 		mtuinfo->ip6m_mtu = icmp6->icmp6_mtu;
2503 		/*
2504 		 * We've consumed everything we need from the original
2505 		 * message.  Free it, then send our empty message.
2506 		 */
2507 		freemsg(mp);
2508 		putnext(UDP_RD(q), newmp);
2509 		return;
2510 	}
2511 	case ICMP6_TIME_EXCEEDED:
2512 		/* Transient errors */
2513 		break;
2514 	case ICMP6_PARAM_PROB:
2515 		/* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
2516 		if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
2517 		    (uchar_t *)ip6h + icmp6->icmp6_pptr ==
2518 		    (uchar_t *)nexthdrp) {
2519 			error = ECONNREFUSED;
2520 			break;
2521 		}
2522 		break;
2523 	}
2524 	if (error == 0) {
2525 		freemsg(mp);
2526 		return;
2527 	}
2528 
2529 	sin6 = sin6_null;
2530 	sin6.sin6_family = AF_INET6;
2531 	sin6.sin6_addr = ip6h->ip6_dst;
2532 	sin6.sin6_port = udpha->uha_dst_port;
2533 	sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
2534 
2535 	mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t), NULL, 0,
2536 	    error);
2537 	if (mp1)
2538 		putnext(UDP_RD(q), mp1);
2539 	freemsg(mp);
2540 }
2541 
2542 /*
2543  * This routine responds to T_ADDR_REQ messages.  It is called by udp_wput.
2544  * The local address is filled in if endpoint is bound. The remote address
2545  * is filled in if remote address has been precified ("connected endpoint")
2546  * (The concept of connected CLTS sockets is alien to published TPI
2547  *  but we support it anyway).
2548  */
2549 static void
2550 udp_addr_req(queue_t *q, mblk_t *mp)
2551 {
2552 	sin_t	*sin;
2553 	sin6_t	*sin6;
2554 	mblk_t	*ackmp;
2555 	struct T_addr_ack *taa;
2556 	udp_t	*udp = Q_TO_UDP(q);
2557 
2558 	/* Make it large enough for worst case */
2559 	ackmp = reallocb(mp, sizeof (struct T_addr_ack) +
2560 	    2 * sizeof (sin6_t), 1);
2561 	if (ackmp == NULL) {
2562 		udp_err_ack(q, mp, TSYSERR, ENOMEM);
2563 		return;
2564 	}
2565 	taa = (struct T_addr_ack *)ackmp->b_rptr;
2566 
2567 	bzero(taa, sizeof (struct T_addr_ack));
2568 	ackmp->b_wptr = (uchar_t *)&taa[1];
2569 
2570 	taa->PRIM_type = T_ADDR_ACK;
2571 	ackmp->b_datap->db_type = M_PCPROTO;
2572 	/*
2573 	 * Note: Following code assumes 32 bit alignment of basic
2574 	 * data structures like sin_t and struct T_addr_ack.
2575 	 */
2576 	if (udp->udp_state != TS_UNBND) {
2577 		/*
2578 		 * Fill in local address first
2579 		 */
2580 		taa->LOCADDR_offset = sizeof (*taa);
2581 		if (udp->udp_family == AF_INET) {
2582 			taa->LOCADDR_length = sizeof (sin_t);
2583 			sin = (sin_t *)&taa[1];
2584 			/* Fill zeroes and then initialize non-zero fields */
2585 			*sin = sin_null;
2586 			sin->sin_family = AF_INET;
2587 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&udp->udp_v6src) &&
2588 			    !IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src)) {
2589 				IN6_V4MAPPED_TO_IPADDR(&udp->udp_v6src,
2590 				    sin->sin_addr.s_addr);
2591 			} else {
2592 				/*
2593 				 * INADDR_ANY
2594 				 * udp_v6src is not set, we might be bound to
2595 				 * broadcast/multicast. Use udp_bound_v6src as
2596 				 * local address instead (that could
2597 				 * also still be INADDR_ANY)
2598 				 */
2599 				IN6_V4MAPPED_TO_IPADDR(&udp->udp_bound_v6src,
2600 				    sin->sin_addr.s_addr);
2601 			}
2602 			sin->sin_port = udp->udp_port;
2603 			ackmp->b_wptr = (uchar_t *)&sin[1];
2604 			if (udp->udp_state == TS_DATA_XFER) {
2605 				/*
2606 				 * connected, fill remote address too
2607 				 */
2608 				taa->REMADDR_length = sizeof (sin_t);
2609 				/* assumed 32-bit alignment */
2610 				taa->REMADDR_offset = taa->LOCADDR_offset +
2611 				    taa->LOCADDR_length;
2612 
2613 				sin = (sin_t *)(ackmp->b_rptr +
2614 				    taa->REMADDR_offset);
2615 				/* initialize */
2616 				*sin = sin_null;
2617 				sin->sin_family = AF_INET;
2618 				sin->sin_addr.s_addr =
2619 				    V4_PART_OF_V6(udp->udp_v6dst);
2620 				sin->sin_port = udp->udp_dstport;
2621 				ackmp->b_wptr = (uchar_t *)&sin[1];
2622 			}
2623 		} else {
2624 			taa->LOCADDR_length = sizeof (sin6_t);
2625 			sin6 = (sin6_t *)&taa[1];
2626 			/* Fill zeroes and then initialize non-zero fields */
2627 			*sin6 = sin6_null;
2628 			sin6->sin6_family = AF_INET6;
2629 			if (!IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src)) {
2630 				sin6->sin6_addr = udp->udp_v6src;
2631 			} else {
2632 				/*
2633 				 * UNSPECIFIED
2634 				 * udp_v6src is not set, we might be bound to
2635 				 * broadcast/multicast. Use udp_bound_v6src as
2636 				 * local address instead (that could
2637 				 * also still be UNSPECIFIED)
2638 				 */
2639 				sin6->sin6_addr =
2640 				    udp->udp_bound_v6src;
2641 			}
2642 			sin6->sin6_port = udp->udp_port;
2643 			ackmp->b_wptr = (uchar_t *)&sin6[1];
2644 			if (udp->udp_state == TS_DATA_XFER) {
2645 				/*
2646 				 * connected, fill remote address too
2647 				 */
2648 				taa->REMADDR_length = sizeof (sin6_t);
2649 				/* assumed 32-bit alignment */
2650 				taa->REMADDR_offset = taa->LOCADDR_offset +
2651 				    taa->LOCADDR_length;
2652 
2653 				sin6 = (sin6_t *)(ackmp->b_rptr +
2654 				    taa->REMADDR_offset);
2655 				/* initialize */
2656 				*sin6 = sin6_null;
2657 				sin6->sin6_family = AF_INET6;
2658 				sin6->sin6_addr = udp->udp_v6dst;
2659 				sin6->sin6_port =  udp->udp_dstport;
2660 				ackmp->b_wptr = (uchar_t *)&sin6[1];
2661 			}
2662 			ackmp->b_wptr = (uchar_t *)&sin6[1];
2663 		}
2664 	}
2665 	ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim);
2666 	putnext(UDP_RD(q), ackmp);
2667 }
2668 
2669 static void
2670 udp_copy_info(struct T_info_ack *tap, udp_t *udp)
2671 {
2672 	if (udp->udp_family == AF_INET) {
2673 		*tap = udp_g_t_info_ack_ipv4;
2674 	} else {
2675 		*tap = udp_g_t_info_ack_ipv6;
2676 	}
2677 	tap->CURRENT_state = udp->udp_state;
2678 	tap->OPT_size = udp_max_optsize;
2679 }
2680 
2681 /*
2682  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
2683  * udp_wput.  Much of the T_CAPABILITY_ACK information is copied from
2684  * udp_g_t_info_ack.  The current state of the stream is copied from
2685  * udp_state.
2686  */
2687 static void
2688 udp_capability_req(queue_t *q, mblk_t *mp)
2689 {
2690 	t_uscalar_t		cap_bits1;
2691 	struct T_capability_ack	*tcap;
2692 	udp_t	*udp = Q_TO_UDP(q);
2693 
2694 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
2695 
2696 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
2697 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
2698 	if (!mp)
2699 		return;
2700 
2701 	tcap = (struct T_capability_ack *)mp->b_rptr;
2702 	tcap->CAP_bits1 = 0;
2703 
2704 	if (cap_bits1 & TC1_INFO) {
2705 		udp_copy_info(&tcap->INFO_ack, udp);
2706 		tcap->CAP_bits1 |= TC1_INFO;
2707 	}
2708 
2709 	putnext(UDP_RD(q), mp);
2710 }
2711 
2712 /*
2713  * This routine responds to T_INFO_REQ messages.  It is called by udp_wput.
2714  * Most of the T_INFO_ACK information is copied from udp_g_t_info_ack.
2715  * The current state of the stream is copied from udp_state.
2716  */
2717 static void
2718 udp_info_req(queue_t *q, mblk_t *mp)
2719 {
2720 	udp_t *udp = Q_TO_UDP(q);
2721 
2722 	/* Create a T_INFO_ACK message. */
2723 	mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
2724 	    T_INFO_ACK);
2725 	if (!mp)
2726 		return;
2727 	udp_copy_info((struct T_info_ack *)mp->b_rptr, udp);
2728 	putnext(UDP_RD(q), mp);
2729 }
2730 
2731 /*
2732  * IP recognizes seven kinds of bind requests:
2733  *
2734  * - A zero-length address binds only to the protocol number.
2735  *
2736  * - A 4-byte address is treated as a request to
2737  * validate that the address is a valid local IPv4
2738  * address, appropriate for an application to bind to.
2739  * IP does the verification, but does not make any note
2740  * of the address at this time.
2741  *
2742  * - A 16-byte address contains is treated as a request
2743  * to validate a local IPv6 address, as the 4-byte
2744  * address case above.
2745  *
2746  * - A 16-byte sockaddr_in to validate the local IPv4 address and also
2747  * use it for the inbound fanout of packets.
2748  *
2749  * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also
2750  * use it for the inbound fanout of packets.
2751  *
2752  * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout
2753  * information consisting of local and remote addresses
2754  * and ports.  In this case, the addresses are both
2755  * validated as appropriate for this operation, and, if
2756  * so, the information is retained for use in the
2757  * inbound fanout.
2758  *
2759  * - A 36-byte address address (ipa6_conn_t) containing complete IPv6
2760  * fanout information, like the 12-byte case above.
2761  *
2762  * IP will also fill in the IRE request mblk with information
2763  * regarding our peer.  In all cases, we notify IP of our protocol
2764  * type by appending a single protocol byte to the bind request.
2765  */
2766 static mblk_t *
2767 udp_ip_bind_mp(udp_t *udp, t_scalar_t bind_prim, t_scalar_t addr_length)
2768 {
2769 	char	*cp;
2770 	mblk_t	*mp;
2771 	struct T_bind_req *tbr;
2772 	ipa_conn_t	*ac;
2773 	ipa6_conn_t	*ac6;
2774 	sin_t		*sin;
2775 	sin6_t		*sin6;
2776 
2777 	ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ);
2778 
2779 	mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI);
2780 	if (!mp)
2781 		return (mp);
2782 	mp->b_datap->db_type = M_PROTO;
2783 	tbr = (struct T_bind_req *)mp->b_rptr;
2784 	tbr->PRIM_type = bind_prim;
2785 	tbr->ADDR_offset = sizeof (*tbr);
2786 	tbr->CONIND_number = 0;
2787 	tbr->ADDR_length = addr_length;
2788 	cp = (char *)&tbr[1];
2789 	switch (addr_length) {
2790 	case sizeof (ipa_conn_t):
2791 		ASSERT(udp->udp_family == AF_INET);
2792 		/* Append a request for an IRE */
2793 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
2794 		if (!mp->b_cont) {
2795 			freemsg(mp);
2796 			return (NULL);
2797 		}
2798 		mp->b_cont->b_wptr += sizeof (ire_t);
2799 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
2800 
2801 		/* cp known to be 32 bit aligned */
2802 		ac = (ipa_conn_t *)cp;
2803 		ac->ac_laddr = V4_PART_OF_V6(udp->udp_v6src);
2804 		ac->ac_faddr = V4_PART_OF_V6(udp->udp_v6dst);
2805 		ac->ac_fport = udp->udp_dstport;
2806 		ac->ac_lport = udp->udp_port;
2807 		break;
2808 
2809 	case sizeof (ipa6_conn_t):
2810 		ASSERT(udp->udp_family == AF_INET6);
2811 		/* Append a request for an IRE */
2812 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
2813 		if (!mp->b_cont) {
2814 			freemsg(mp);
2815 			return (NULL);
2816 		}
2817 		mp->b_cont->b_wptr += sizeof (ire_t);
2818 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
2819 
2820 		/* cp known to be 32 bit aligned */
2821 		ac6 = (ipa6_conn_t *)cp;
2822 		ac6->ac6_laddr = udp->udp_v6src;
2823 		ac6->ac6_faddr = udp->udp_v6dst;
2824 		ac6->ac6_fport = udp->udp_dstport;
2825 		ac6->ac6_lport = udp->udp_port;
2826 		break;
2827 
2828 	case sizeof (sin_t):
2829 		ASSERT(udp->udp_family == AF_INET);
2830 		/* Append a request for an IRE */
2831 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
2832 		if (!mp->b_cont) {
2833 			freemsg(mp);
2834 			return (NULL);
2835 		}
2836 		mp->b_cont->b_wptr += sizeof (ire_t);
2837 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
2838 
2839 		sin = (sin_t *)cp;
2840 		*sin = sin_null;
2841 		sin->sin_family = AF_INET;
2842 		sin->sin_addr.s_addr = V4_PART_OF_V6(udp->udp_bound_v6src);
2843 		sin->sin_port = udp->udp_port;
2844 		break;
2845 
2846 	case sizeof (sin6_t):
2847 		ASSERT(udp->udp_family == AF_INET6);
2848 		/* Append a request for an IRE */
2849 		mp->b_cont = allocb(sizeof (ire_t), BPRI_HI);
2850 		if (!mp->b_cont) {
2851 			freemsg(mp);
2852 			return (NULL);
2853 		}
2854 		mp->b_cont->b_wptr += sizeof (ire_t);
2855 		mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE;
2856 
2857 		sin6 = (sin6_t *)cp;
2858 		*sin6 = sin6_null;
2859 		sin6->sin6_family = AF_INET6;
2860 		sin6->sin6_addr = udp->udp_bound_v6src;
2861 		sin6->sin6_port = udp->udp_port;
2862 		break;
2863 	}
2864 	/* Add protocol number to end */
2865 	cp[addr_length] = (char)IPPROTO_UDP;
2866 	mp->b_wptr = (uchar_t *)&cp[addr_length + 1];
2867 	return (mp);
2868 }
2869 
2870 /*
2871  * This is the open routine for udp.  It allocates a udp_t structure for
2872  * the stream and, on the first open of the module, creates an ND table.
2873  */
2874 /* ARGSUSED */
2875 static int
2876 udp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2877 {
2878 	int	err;
2879 	udp_t	*udp;
2880 	conn_t *connp;
2881 	zoneid_t zoneid = getzoneid();
2882 	queue_t	*ip_wq;
2883 	char	*name;
2884 
2885 	TRACE_1(TR_FAC_UDP, TR_UDP_OPEN, "udp_open: q %p", q);
2886 
2887 	/* If the stream is already open, return immediately. */
2888 	if (q->q_ptr != NULL)
2889 		return (0);
2890 
2891 	/* If this is not a push of udp as a module, fail. */
2892 	if (sflag != MODOPEN)
2893 		return (EINVAL);
2894 
2895 	q->q_hiwat = udp_recv_hiwat;
2896 	WR(q)->q_hiwat = udp_xmit_hiwat;
2897 	WR(q)->q_lowat = udp_xmit_lowat;
2898 
2899 	/* Insert ourselves in the stream since we're about to walk q_next */
2900 	qprocson(q);
2901 
2902 	udp = kmem_cache_alloc(udp_cache, KM_SLEEP);
2903 	bzero(udp, sizeof (*udp));
2904 
2905 	/*
2906 	 * UDP is supported only as a module and it has to be pushed directly
2907 	 * above the device instance of IP. If UDP is pushed anywhere else
2908 	 * on a stream, it will support just T_SVR4_OPTMGMT_REQ for the
2909 	 * sake of MIB browsers and fail everything else.
2910 	 */
2911 	ip_wq = WR(q)->q_next;
2912 	if (ip_wq->q_next != NULL ||
2913 	    (name = ip_wq->q_qinfo->qi_minfo->mi_idname) == NULL ||
2914 	    strcmp(name, IP_MOD_NAME) != 0 ||
2915 	    ip_wq->q_qinfo->qi_minfo->mi_idnum != IP_MOD_ID) {
2916 		/* Support just SNMP for MIB browsers */
2917 		connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP);
2918 		connp->conn_rq = q;
2919 		connp->conn_wq = WR(q);
2920 		connp->conn_flags |= IPCL_UDPMOD;
2921 		connp->conn_cred = credp;
2922 		connp->conn_zoneid = zoneid;
2923 		connp->conn_udp = udp;
2924 		udp->udp_connp = connp;
2925 		q->q_ptr = WR(q)->q_ptr = connp;
2926 		crhold(credp);
2927 		q->q_qinfo = &udp_snmp_rinit;
2928 		WR(q)->q_qinfo = &udp_snmp_winit;
2929 		return (0);
2930 	}
2931 
2932 	/*
2933 	 * Initialize the udp_t structure for this stream.
2934 	 */
2935 	q = RD(ip_wq);
2936 	connp = Q_TO_CONN(q);
2937 	mutex_enter(&connp->conn_lock);
2938 	connp->conn_proto = IPPROTO_UDP;
2939 	connp->conn_flags |= IPCL_UDP;
2940 	connp->conn_sqp = IP_SQUEUE_GET(lbolt);
2941 	connp->conn_udp = udp;
2942 
2943 	/* Set the initial state of the stream and the privilege status. */
2944 	udp->udp_connp = connp;
2945 	udp->udp_state = TS_UNBND;
2946 	udp->udp_mode = UDP_MT_HOT;
2947 	if (getmajor(*devp) == (major_t)UDP6_MAJ) {
2948 		udp->udp_family = AF_INET6;
2949 		udp->udp_ipversion = IPV6_VERSION;
2950 		udp->udp_max_hdr_len = IPV6_HDR_LEN + UDPH_SIZE;
2951 		udp->udp_ttl = udp_ipv6_hoplimit;
2952 		connp->conn_af_isv6 = B_TRUE;
2953 		connp->conn_flags |= IPCL_ISV6;
2954 	} else {
2955 		udp->udp_family = AF_INET;
2956 		udp->udp_ipversion = IPV4_VERSION;
2957 		udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH + UDPH_SIZE;
2958 		udp->udp_ttl = udp_ipv4_ttl;
2959 		connp->conn_af_isv6 = B_FALSE;
2960 		connp->conn_flags &= ~IPCL_ISV6;
2961 	}
2962 
2963 	udp->udp_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2964 	connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
2965 	connp->conn_zoneid = zoneid;
2966 
2967 	/*
2968 	 * If the caller has the process-wide flag set, then default to MAC
2969 	 * exempt mode.  This allows read-down to unlabeled hosts.
2970 	 */
2971 	if (getpflags(NET_MAC_AWARE, credp) != 0)
2972 		udp->udp_mac_exempt = B_TRUE;
2973 
2974 	if (connp->conn_flags & IPCL_SOCKET) {
2975 		udp->udp_issocket = B_TRUE;
2976 		udp->udp_direct_sockfs = B_TRUE;
2977 	}
2978 
2979 	connp->conn_ulp_labeled = is_system_labeled();
2980 
2981 	mutex_exit(&connp->conn_lock);
2982 
2983 	/*
2984 	 * The transmit hiwat/lowat is only looked at on IP's queue.
2985 	 * Store in q_hiwat in order to return on SO_SNDBUF/SO_RCVBUF
2986 	 * getsockopts.
2987 	 */
2988 	q->q_hiwat = udp_recv_hiwat;
2989 	WR(q)->q_hiwat = udp_xmit_hiwat;
2990 	WR(q)->q_lowat = udp_xmit_lowat;
2991 
2992 	if (udp->udp_family == AF_INET6) {
2993 		/* Build initial header template for transmit */
2994 		if ((err = udp_build_hdrs(q, udp)) != 0) {
2995 error:
2996 			qprocsoff(UDP_RD(q));
2997 			udp->udp_connp = NULL;
2998 			connp->conn_udp = NULL;
2999 			kmem_cache_free(udp_cache, udp);
3000 			return (err);
3001 		}
3002 	}
3003 
3004 	/* Set the Stream head write offset and high watermark. */
3005 	(void) mi_set_sth_wroff(UDP_RD(q),
3006 	    udp->udp_max_hdr_len + udp_wroff_extra);
3007 	(void) mi_set_sth_hiwat(UDP_RD(q), udp_set_rcv_hiwat(udp, q->q_hiwat));
3008 
3009 	WR(UDP_RD(q))->q_qinfo = &udp_winit;
3010 
3011 	return (0);
3012 }
3013 
3014 /*
3015  * Which UDP options OK to set through T_UNITDATA_REQ...
3016  */
3017 /* ARGSUSED */
3018 static boolean_t
3019 udp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name)
3020 {
3021 	return (B_TRUE);
3022 }
3023 
3024 /*
3025  * This routine gets default values of certain options whose default
3026  * values are maintained by protcol specific code
3027  */
3028 /* ARGSUSED */
3029 int
3030 udp_opt_default(queue_t	*q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
3031 {
3032 	int *i1 = (int *)ptr;
3033 
3034 	switch (level) {
3035 	case IPPROTO_IP:
3036 		switch (name) {
3037 		case IP_MULTICAST_TTL:
3038 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL;
3039 			return (sizeof (uchar_t));
3040 		case IP_MULTICAST_LOOP:
3041 			*ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP;
3042 			return (sizeof (uchar_t));
3043 		}
3044 		break;
3045 	case IPPROTO_IPV6:
3046 		switch (name) {
3047 		case IPV6_MULTICAST_HOPS:
3048 			*i1 = IP_DEFAULT_MULTICAST_TTL;
3049 			return (sizeof (int));
3050 		case IPV6_MULTICAST_LOOP:
3051 			*i1 = IP_DEFAULT_MULTICAST_LOOP;
3052 			return (sizeof (int));
3053 		case IPV6_UNICAST_HOPS:
3054 			*i1 = udp_ipv6_hoplimit;
3055 			return (sizeof (int));
3056 		}
3057 		break;
3058 	}
3059 	return (-1);
3060 }
3061 
3062 /*
3063  * This routine retrieves the current status of socket options
3064  * and expects the caller to pass in the queue pointer of the
3065  * upper instance.  It returns the size of the option retrieved.
3066  */
3067 int
3068 udp_opt_get(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
3069 {
3070 	int	*i1 = (int *)ptr;
3071 	conn_t	*connp;
3072 	udp_t	*udp;
3073 	ip6_pkt_t *ipp;
3074 	int	len;
3075 
3076 	q = UDP_WR(q);
3077 	connp = Q_TO_CONN(q);
3078 	udp = connp->conn_udp;
3079 	ipp = &udp->udp_sticky_ipp;
3080 
3081 	switch (level) {
3082 	case SOL_SOCKET:
3083 		switch (name) {
3084 		case SO_DEBUG:
3085 			*i1 = udp->udp_debug;
3086 			break;	/* goto sizeof (int) option return */
3087 		case SO_REUSEADDR:
3088 			*i1 = udp->udp_reuseaddr;
3089 			break;	/* goto sizeof (int) option return */
3090 		case SO_TYPE:
3091 			*i1 = SOCK_DGRAM;
3092 			break;	/* goto sizeof (int) option return */
3093 
3094 		/*
3095 		 * The following three items are available here,
3096 		 * but are only meaningful to IP.
3097 		 */
3098 		case SO_DONTROUTE:
3099 			*i1 = udp->udp_dontroute;
3100 			break;	/* goto sizeof (int) option return */
3101 		case SO_USELOOPBACK:
3102 			*i1 = udp->udp_useloopback;
3103 			break;	/* goto sizeof (int) option return */
3104 		case SO_BROADCAST:
3105 			*i1 = udp->udp_broadcast;
3106 			break;	/* goto sizeof (int) option return */
3107 
3108 		case SO_SNDBUF:
3109 			*i1 = q->q_hiwat;
3110 			break;	/* goto sizeof (int) option return */
3111 		case SO_RCVBUF:
3112 			*i1 = RD(q)->q_hiwat;
3113 			break;	/* goto sizeof (int) option return */
3114 		case SO_DGRAM_ERRIND:
3115 			*i1 = udp->udp_dgram_errind;
3116 			break;	/* goto sizeof (int) option return */
3117 		case SO_RECVUCRED:
3118 			*i1 = udp->udp_recvucred;
3119 			break;	/* goto sizeof (int) option return */
3120 		case SO_TIMESTAMP:
3121 			*i1 = udp->udp_timestamp;
3122 			break;	/* goto sizeof (int) option return */
3123 		case SO_ANON_MLP:
3124 			*i1 = udp->udp_anon_mlp;
3125 			break;	/* goto sizeof (int) option return */
3126 		case SO_MAC_EXEMPT:
3127 			*i1 = udp->udp_mac_exempt;
3128 			break;	/* goto sizeof (int) option return */
3129 		case SO_ALLZONES:
3130 			*i1 = connp->conn_allzones;
3131 			break;	/* goto sizeof (int) option return */
3132 		default:
3133 			return (-1);
3134 		}
3135 		break;
3136 	case IPPROTO_IP:
3137 		if (udp->udp_family != AF_INET)
3138 			return (-1);
3139 		switch (name) {
3140 		case IP_OPTIONS:
3141 		case T_IP_OPTIONS:
3142 			len = udp->udp_ip_rcv_options_len - udp->udp_label_len;
3143 			if (len > 0) {
3144 				bcopy(udp->udp_ip_rcv_options +
3145 				    udp->udp_label_len, ptr, len);
3146 			}
3147 			return (len);
3148 		case IP_TOS:
3149 		case T_IP_TOS:
3150 			*i1 = (int)udp->udp_type_of_service;
3151 			break;	/* goto sizeof (int) option return */
3152 		case IP_TTL:
3153 			*i1 = (int)udp->udp_ttl;
3154 			break;	/* goto sizeof (int) option return */
3155 		case IP_NEXTHOP:
3156 			/* Handled at IP level */
3157 			return (-EINVAL);
3158 		case IP_MULTICAST_IF:
3159 			/* 0 address if not set */
3160 			*(ipaddr_t *)ptr = udp->udp_multicast_if_addr;
3161 			return (sizeof (ipaddr_t));
3162 		case IP_MULTICAST_TTL:
3163 			*(uchar_t *)ptr = udp->udp_multicast_ttl;
3164 			return (sizeof (uchar_t));
3165 		case IP_MULTICAST_LOOP:
3166 			*ptr = connp->conn_multicast_loop;
3167 			return (sizeof (uint8_t));
3168 		case IP_RECVOPTS:
3169 			*i1 = udp->udp_recvopts;
3170 			break;	/* goto sizeof (int) option return */
3171 		case IP_RECVDSTADDR:
3172 			*i1 = udp->udp_recvdstaddr;
3173 			break;	/* goto sizeof (int) option return */
3174 		case IP_RECVIF:
3175 			*i1 = udp->udp_recvif;
3176 			break;	/* goto sizeof (int) option return */
3177 		case IP_RECVSLLA:
3178 			*i1 = udp->udp_recvslla;
3179 			break;	/* goto sizeof (int) option return */
3180 		case IP_RECVTTL:
3181 			*i1 = udp->udp_recvttl;
3182 			break;	/* goto sizeof (int) option return */
3183 		case IP_ADD_MEMBERSHIP:
3184 		case IP_DROP_MEMBERSHIP:
3185 		case IP_BLOCK_SOURCE:
3186 		case IP_UNBLOCK_SOURCE:
3187 		case IP_ADD_SOURCE_MEMBERSHIP:
3188 		case IP_DROP_SOURCE_MEMBERSHIP:
3189 		case MCAST_JOIN_GROUP:
3190 		case MCAST_LEAVE_GROUP:
3191 		case MCAST_BLOCK_SOURCE:
3192 		case MCAST_UNBLOCK_SOURCE:
3193 		case MCAST_JOIN_SOURCE_GROUP:
3194 		case MCAST_LEAVE_SOURCE_GROUP:
3195 		case IP_DONTFAILOVER_IF:
3196 			/* cannot "get" the value for these */
3197 			return (-1);
3198 		case IP_BOUND_IF:
3199 			/* Zero if not set */
3200 			*i1 = udp->udp_bound_if;
3201 			break;	/* goto sizeof (int) option return */
3202 		case IP_UNSPEC_SRC:
3203 			*i1 = udp->udp_unspec_source;
3204 			break;	/* goto sizeof (int) option return */
3205 		case IP_XMIT_IF:
3206 			*i1 = udp->udp_xmit_if;
3207 			break; /* goto sizeof (int) option return */
3208 		default:
3209 			return (-1);
3210 		}
3211 		break;
3212 	case IPPROTO_IPV6:
3213 		if (udp->udp_family != AF_INET6)
3214 			return (-1);
3215 		switch (name) {
3216 		case IPV6_UNICAST_HOPS:
3217 			*i1 = (unsigned int)udp->udp_ttl;
3218 			break;	/* goto sizeof (int) option return */
3219 		case IPV6_MULTICAST_IF:
3220 			/* 0 index if not set */
3221 			*i1 = udp->udp_multicast_if_index;
3222 			break;	/* goto sizeof (int) option return */
3223 		case IPV6_MULTICAST_HOPS:
3224 			*i1 = udp->udp_multicast_ttl;
3225 			break;	/* goto sizeof (int) option return */
3226 		case IPV6_MULTICAST_LOOP:
3227 			*i1 = connp->conn_multicast_loop;
3228 			break;	/* goto sizeof (int) option return */
3229 		case IPV6_JOIN_GROUP:
3230 		case IPV6_LEAVE_GROUP:
3231 		case MCAST_JOIN_GROUP:
3232 		case MCAST_LEAVE_GROUP:
3233 		case MCAST_BLOCK_SOURCE:
3234 		case MCAST_UNBLOCK_SOURCE:
3235 		case MCAST_JOIN_SOURCE_GROUP:
3236 		case MCAST_LEAVE_SOURCE_GROUP:
3237 			/* cannot "get" the value for these */
3238 			return (-1);
3239 		case IPV6_BOUND_IF:
3240 			/* Zero if not set */
3241 			*i1 = udp->udp_bound_if;
3242 			break;	/* goto sizeof (int) option return */
3243 		case IPV6_UNSPEC_SRC:
3244 			*i1 = udp->udp_unspec_source;
3245 			break;	/* goto sizeof (int) option return */
3246 		case IPV6_RECVPKTINFO:
3247 			*i1 = udp->udp_ipv6_recvpktinfo;
3248 			break;	/* goto sizeof (int) option return */
3249 		case IPV6_RECVTCLASS:
3250 			*i1 = udp->udp_ipv6_recvtclass;
3251 			break;	/* goto sizeof (int) option return */
3252 		case IPV6_RECVPATHMTU:
3253 			*i1 = udp->udp_ipv6_recvpathmtu;
3254 			break;	/* goto sizeof (int) option return */
3255 		case IPV6_RECVHOPLIMIT:
3256 			*i1 = udp->udp_ipv6_recvhoplimit;
3257 			break;	/* goto sizeof (int) option return */
3258 		case IPV6_RECVHOPOPTS:
3259 			*i1 = udp->udp_ipv6_recvhopopts;
3260 			break;	/* goto sizeof (int) option return */
3261 		case IPV6_RECVDSTOPTS:
3262 			*i1 = udp->udp_ipv6_recvdstopts;
3263 			break;	/* goto sizeof (int) option return */
3264 		case _OLD_IPV6_RECVDSTOPTS:
3265 			*i1 = udp->udp_old_ipv6_recvdstopts;
3266 			break;	/* goto sizeof (int) option return */
3267 		case IPV6_RECVRTHDRDSTOPTS:
3268 			*i1 = udp->udp_ipv6_recvrthdrdstopts;
3269 			break;	/* goto sizeof (int) option return */
3270 		case IPV6_RECVRTHDR:
3271 			*i1 = udp->udp_ipv6_recvrthdr;
3272 			break;	/* goto sizeof (int) option return */
3273 		case IPV6_PKTINFO: {
3274 			/* XXX assumes that caller has room for max size! */
3275 			struct in6_pktinfo *pkti;
3276 
3277 			pkti = (struct in6_pktinfo *)ptr;
3278 			if (ipp->ipp_fields & IPPF_IFINDEX)
3279 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
3280 			else
3281 				pkti->ipi6_ifindex = 0;
3282 			if (ipp->ipp_fields & IPPF_ADDR)
3283 				pkti->ipi6_addr = ipp->ipp_addr;
3284 			else
3285 				pkti->ipi6_addr = ipv6_all_zeros;
3286 			return (sizeof (struct in6_pktinfo));
3287 		}
3288 		case IPV6_TCLASS:
3289 			if (ipp->ipp_fields & IPPF_TCLASS)
3290 				*i1 = ipp->ipp_tclass;
3291 			else
3292 				*i1 = IPV6_FLOW_TCLASS(
3293 				    IPV6_DEFAULT_VERS_AND_FLOW);
3294 			break;	/* goto sizeof (int) option return */
3295 		case IPV6_NEXTHOP: {
3296 			sin6_t *sin6 = (sin6_t *)ptr;
3297 
3298 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
3299 				return (0);
3300 			*sin6 = sin6_null;
3301 			sin6->sin6_family = AF_INET6;
3302 			sin6->sin6_addr = ipp->ipp_nexthop;
3303 			return (sizeof (sin6_t));
3304 		}
3305 		case IPV6_HOPOPTS:
3306 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
3307 				return (0);
3308 			if (ipp->ipp_hopoptslen <= udp->udp_label_len_v6)
3309 				return (0);
3310 			/*
3311 			 * The cipso/label option is added by kernel.
3312 			 * User is not usually aware of this option.
3313 			 * We copy out the hbh opt after the label option.
3314 			 */
3315 			bcopy((char *)ipp->ipp_hopopts + udp->udp_label_len_v6,
3316 			    ptr, ipp->ipp_hopoptslen - udp->udp_label_len_v6);
3317 			if (udp->udp_label_len_v6 > 0) {
3318 				ptr[0] = ((char *)ipp->ipp_hopopts)[0];
3319 				ptr[1] = (ipp->ipp_hopoptslen -
3320 				    udp->udp_label_len_v6 + 7) / 8 - 1;
3321 			}
3322 			return (ipp->ipp_hopoptslen - udp->udp_label_len_v6);
3323 		case IPV6_RTHDRDSTOPTS:
3324 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
3325 				return (0);
3326 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
3327 			return (ipp->ipp_rtdstoptslen);
3328 		case IPV6_RTHDR:
3329 			if (!(ipp->ipp_fields & IPPF_RTHDR))
3330 				return (0);
3331 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
3332 			return (ipp->ipp_rthdrlen);
3333 		case IPV6_DSTOPTS:
3334 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
3335 				return (0);
3336 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
3337 			return (ipp->ipp_dstoptslen);
3338 		case IPV6_PATHMTU:
3339 			return (ip_fill_mtuinfo(&udp->udp_v6dst,
3340 				udp->udp_dstport, (struct ip6_mtuinfo *)ptr));
3341 		default:
3342 			return (-1);
3343 		}
3344 		break;
3345 	case IPPROTO_UDP:
3346 		switch (name) {
3347 		case UDP_ANONPRIVBIND:
3348 			*i1 = udp->udp_anon_priv_bind;
3349 			break;
3350 		case UDP_EXCLBIND:
3351 			*i1 = udp->udp_exclbind ? UDP_EXCLBIND : 0;
3352 			break;
3353 		case UDP_RCVHDR:
3354 			*i1 = udp->udp_rcvhdr ? 1 : 0;
3355 			break;
3356 		default:
3357 			return (-1);
3358 		}
3359 		break;
3360 	default:
3361 		return (-1);
3362 	}
3363 	return (sizeof (int));
3364 }
3365 
3366 /*
3367  * This routine sets socket options; it expects the caller
3368  * to pass in the queue pointer of the upper instance.
3369  */
3370 /* ARGSUSED */
3371 int
3372 udp_opt_set(queue_t *q, uint_t optset_context, int level,
3373     int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp,
3374     uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
3375 {
3376 	udpattrs_t *attrs = thisdg_attrs;
3377 	int	*i1 = (int *)invalp;
3378 	boolean_t onoff = (*i1 == 0) ? 0 : 1;
3379 	boolean_t checkonly;
3380 	int	error;
3381 	conn_t	*connp;
3382 	udp_t	*udp;
3383 	uint_t	newlen;
3384 
3385 	q = UDP_WR(q);
3386 	connp = Q_TO_CONN(q);
3387 	udp = connp->conn_udp;
3388 
3389 	switch (optset_context) {
3390 	case SETFN_OPTCOM_CHECKONLY:
3391 		checkonly = B_TRUE;
3392 		/*
3393 		 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
3394 		 * inlen != 0 implies value supplied and
3395 		 * 	we have to "pretend" to set it.
3396 		 * inlen == 0 implies that there is no
3397 		 * 	value part in T_CHECK request and just validation
3398 		 * done elsewhere should be enough, we just return here.
3399 		 */
3400 		if (inlen == 0) {
3401 			*outlenp = 0;
3402 			return (0);
3403 		}
3404 		break;
3405 	case SETFN_OPTCOM_NEGOTIATE:
3406 		checkonly = B_FALSE;
3407 		break;
3408 	case SETFN_UD_NEGOTIATE:
3409 	case SETFN_CONN_NEGOTIATE:
3410 		checkonly = B_FALSE;
3411 		/*
3412 		 * Negotiating local and "association-related" options
3413 		 * through T_UNITDATA_REQ.
3414 		 *
3415 		 * Following routine can filter out ones we do not
3416 		 * want to be "set" this way.
3417 		 */
3418 		if (!udp_opt_allow_udr_set(level, name)) {
3419 			*outlenp = 0;
3420 			return (EINVAL);
3421 		}
3422 		break;
3423 	default:
3424 		/*
3425 		 * We should never get here
3426 		 */
3427 		*outlenp = 0;
3428 		return (EINVAL);
3429 	}
3430 
3431 	ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
3432 	    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
3433 
3434 	/*
3435 	 * For fixed length options, no sanity check
3436 	 * of passed in length is done. It is assumed *_optcom_req()
3437 	 * routines do the right thing.
3438 	 */
3439 
3440 	switch (level) {
3441 	case SOL_SOCKET:
3442 		switch (name) {
3443 		case SO_REUSEADDR:
3444 			if (!checkonly)
3445 				udp->udp_reuseaddr = onoff;
3446 			break;
3447 		case SO_DEBUG:
3448 			if (!checkonly)
3449 				udp->udp_debug = onoff;
3450 			break;
3451 		/*
3452 		 * The following three items are available here,
3453 		 * but are only meaningful to IP.
3454 		 */
3455 		case SO_DONTROUTE:
3456 			if (!checkonly)
3457 				udp->udp_dontroute = onoff;
3458 			break;
3459 		case SO_USELOOPBACK:
3460 			if (!checkonly)
3461 				udp->udp_useloopback = onoff;
3462 			break;
3463 		case SO_BROADCAST:
3464 			if (!checkonly)
3465 				udp->udp_broadcast = onoff;
3466 			break;
3467 
3468 		case SO_SNDBUF:
3469 			if (*i1 > udp_max_buf) {
3470 				*outlenp = 0;
3471 				return (ENOBUFS);
3472 			}
3473 			if (!checkonly) {
3474 				q->q_hiwat = *i1;
3475 				WR(UDP_RD(q))->q_hiwat = *i1;
3476 			}
3477 			break;
3478 		case SO_RCVBUF:
3479 			if (*i1 > udp_max_buf) {
3480 				*outlenp = 0;
3481 				return (ENOBUFS);
3482 			}
3483 			if (!checkonly) {
3484 				RD(q)->q_hiwat = *i1;
3485 				UDP_RD(q)->q_hiwat = *i1;
3486 				(void) mi_set_sth_hiwat(UDP_RD(q),
3487 				    udp_set_rcv_hiwat(udp, *i1));
3488 			}
3489 			break;
3490 		case SO_DGRAM_ERRIND:
3491 			if (!checkonly)
3492 				udp->udp_dgram_errind = onoff;
3493 			break;
3494 		case SO_RECVUCRED:
3495 			if (!checkonly)
3496 				udp->udp_recvucred = onoff;
3497 			break;
3498 		case SO_ALLZONES:
3499 			/*
3500 			 * "soft" error (negative)
3501 			 * option not handled at this level
3502 			 * Do not modify *outlenp.
3503 			 */
3504 			return (-EINVAL);
3505 		case SO_TIMESTAMP:
3506 			if (!checkonly)
3507 				udp->udp_timestamp = onoff;
3508 			break;
3509 		case SO_ANON_MLP:
3510 			if (!checkonly)
3511 				udp->udp_anon_mlp = onoff;
3512 			break;
3513 		case SO_MAC_EXEMPT:
3514 			if (secpolicy_net_mac_aware(cr) != 0 ||
3515 			    udp->udp_state != TS_UNBND)
3516 				return (EACCES);
3517 			if (!checkonly)
3518 				udp->udp_mac_exempt = onoff;
3519 			break;
3520 		case SCM_UCRED: {
3521 			struct ucred_s *ucr;
3522 			cred_t *cr, *newcr;
3523 			ts_label_t *tsl;
3524 
3525 			/*
3526 			 * Only sockets that have proper privileges and are
3527 			 * bound to MLPs will have any other value here, so
3528 			 * this implicitly tests for privilege to set label.
3529 			 */
3530 			if (connp->conn_mlp_type == mlptSingle)
3531 				break;
3532 			ucr = (struct ucred_s *)invalp;
3533 			if (inlen != ucredsize ||
3534 			    ucr->uc_labeloff < sizeof (*ucr) ||
3535 			    ucr->uc_labeloff + sizeof (bslabel_t) > inlen)
3536 				return (EINVAL);
3537 			if (!checkonly) {
3538 				mblk_t *mb;
3539 
3540 				if (attrs == NULL ||
3541 				    (mb = attrs->udpattr_mb) == NULL)
3542 					return (EINVAL);
3543 				if ((cr = DB_CRED(mb)) == NULL)
3544 					cr = udp->udp_connp->conn_cred;
3545 				ASSERT(cr != NULL);
3546 				if ((tsl = crgetlabel(cr)) == NULL)
3547 					return (EINVAL);
3548 				newcr = copycred_from_bslabel(cr, UCLABEL(ucr),
3549 				    tsl->tsl_doi, KM_NOSLEEP);
3550 				if (newcr == NULL)
3551 					return (ENOSR);
3552 				mblk_setcred(mb, newcr);
3553 				attrs->udpattr_credset = B_TRUE;
3554 				crfree(newcr);
3555 			}
3556 			break;
3557 		}
3558 		default:
3559 			*outlenp = 0;
3560 			return (EINVAL);
3561 		}
3562 		break;
3563 	case IPPROTO_IP:
3564 		if (udp->udp_family != AF_INET) {
3565 			*outlenp = 0;
3566 			return (ENOPROTOOPT);
3567 		}
3568 		switch (name) {
3569 		case IP_OPTIONS:
3570 		case T_IP_OPTIONS:
3571 			/* Save options for use by IP. */
3572 			newlen = inlen + udp->udp_label_len;
3573 			if ((inlen & 0x3) || newlen > IP_MAX_OPT_LENGTH) {
3574 				*outlenp = 0;
3575 				return (EINVAL);
3576 			}
3577 			if (checkonly)
3578 				break;
3579 
3580 			if (!tsol_option_set(&udp->udp_ip_snd_options,
3581 			    &udp->udp_ip_snd_options_len,
3582 			    udp->udp_label_len, invalp, inlen)) {
3583 				*outlenp = 0;
3584 				return (ENOMEM);
3585 			}
3586 
3587 			udp->udp_max_hdr_len = IP_SIMPLE_HDR_LENGTH +
3588 			    UDPH_SIZE + udp->udp_ip_snd_options_len;
3589 			(void) mi_set_sth_wroff(RD(q), udp->udp_max_hdr_len +
3590 			    udp_wroff_extra);
3591 			break;
3592 
3593 		case IP_TTL:
3594 			if (!checkonly) {
3595 				udp->udp_ttl = (uchar_t)*i1;
3596 			}
3597 			break;
3598 		case IP_TOS:
3599 		case T_IP_TOS:
3600 			if (!checkonly) {
3601 				udp->udp_type_of_service = (uchar_t)*i1;
3602 			}
3603 			break;
3604 		case IP_MULTICAST_IF: {
3605 			/*
3606 			 * TODO should check OPTMGMT reply and undo this if
3607 			 * there is an error.
3608 			 */
3609 			struct in_addr *inap = (struct in_addr *)invalp;
3610 			if (!checkonly) {
3611 				udp->udp_multicast_if_addr =
3612 				    inap->s_addr;
3613 			}
3614 			break;
3615 		}
3616 		case IP_MULTICAST_TTL:
3617 			if (!checkonly)
3618 				udp->udp_multicast_ttl = *invalp;
3619 			break;
3620 		case IP_MULTICAST_LOOP:
3621 			if (!checkonly)
3622 				connp->conn_multicast_loop = *invalp;
3623 			break;
3624 		case IP_RECVOPTS:
3625 			if (!checkonly)
3626 				udp->udp_recvopts = onoff;
3627 			break;
3628 		case IP_RECVDSTADDR:
3629 			if (!checkonly)
3630 				udp->udp_recvdstaddr = onoff;
3631 			break;
3632 		case IP_RECVIF:
3633 			if (!checkonly)
3634 				udp->udp_recvif = onoff;
3635 			break;
3636 		case IP_RECVSLLA:
3637 			if (!checkonly)
3638 				udp->udp_recvslla = onoff;
3639 			break;
3640 		case IP_RECVTTL:
3641 			if (!checkonly)
3642 				udp->udp_recvttl = onoff;
3643 			break;
3644 		case IP_ADD_MEMBERSHIP:
3645 		case IP_DROP_MEMBERSHIP:
3646 		case IP_BLOCK_SOURCE:
3647 		case IP_UNBLOCK_SOURCE:
3648 		case IP_ADD_SOURCE_MEMBERSHIP:
3649 		case IP_DROP_SOURCE_MEMBERSHIP:
3650 		case MCAST_JOIN_GROUP:
3651 		case MCAST_LEAVE_GROUP:
3652 		case MCAST_BLOCK_SOURCE:
3653 		case MCAST_UNBLOCK_SOURCE:
3654 		case MCAST_JOIN_SOURCE_GROUP:
3655 		case MCAST_LEAVE_SOURCE_GROUP:
3656 		case IP_SEC_OPT:
3657 		case IP_NEXTHOP:
3658 			/*
3659 			 * "soft" error (negative)
3660 			 * option not handled at this level
3661 			 * Do not modify *outlenp.
3662 			 */
3663 			return (-EINVAL);
3664 		case IP_BOUND_IF:
3665 			if (!checkonly)
3666 				udp->udp_bound_if = *i1;
3667 			break;
3668 		case IP_UNSPEC_SRC:
3669 			if (!checkonly)
3670 				udp->udp_unspec_source = onoff;
3671 			break;
3672 		case IP_XMIT_IF:
3673 			if (!checkonly)
3674 				udp->udp_xmit_if = *i1;
3675 			break;
3676 		default:
3677 			*outlenp = 0;
3678 			return (EINVAL);
3679 		}
3680 		break;
3681 	case IPPROTO_IPV6: {
3682 		ip6_pkt_t		*ipp;
3683 		boolean_t		sticky;
3684 
3685 		if (udp->udp_family != AF_INET6) {
3686 			*outlenp = 0;
3687 			return (ENOPROTOOPT);
3688 		}
3689 		/*
3690 		 * Deal with both sticky options and ancillary data
3691 		 */
3692 		sticky = B_FALSE;
3693 		if (attrs == NULL || (ipp = attrs->udpattr_ipp) == NULL) {
3694 			/* sticky options, or none */
3695 			ipp = &udp->udp_sticky_ipp;
3696 			sticky = B_TRUE;
3697 		}
3698 
3699 		switch (name) {
3700 		case IPV6_MULTICAST_IF:
3701 			if (!checkonly)
3702 				udp->udp_multicast_if_index = *i1;
3703 			break;
3704 		case IPV6_UNICAST_HOPS:
3705 			/* -1 means use default */
3706 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
3707 				*outlenp = 0;
3708 				return (EINVAL);
3709 			}
3710 			if (!checkonly) {
3711 				if (*i1 == -1) {
3712 					udp->udp_ttl = ipp->ipp_unicast_hops =
3713 					    udp_ipv6_hoplimit;
3714 					ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
3715 					/* Pass modified value to IP. */
3716 					*i1 = udp->udp_ttl;
3717 				} else {
3718 					udp->udp_ttl = ipp->ipp_unicast_hops =
3719 					    (uint8_t)*i1;
3720 					ipp->ipp_fields |= IPPF_UNICAST_HOPS;
3721 				}
3722 				/* Rebuild the header template */
3723 				error = udp_build_hdrs(q, udp);
3724 				if (error != 0) {
3725 					*outlenp = 0;
3726 					return (error);
3727 				}
3728 			}
3729 			break;
3730 		case IPV6_MULTICAST_HOPS:
3731 			/* -1 means use default */
3732 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
3733 				*outlenp = 0;
3734 				return (EINVAL);
3735 			}
3736 			if (!checkonly) {
3737 				if (*i1 == -1) {
3738 					udp->udp_multicast_ttl =
3739 					    ipp->ipp_multicast_hops =
3740 					    IP_DEFAULT_MULTICAST_TTL;
3741 					ipp->ipp_fields &= ~IPPF_MULTICAST_HOPS;
3742 					/* Pass modified value to IP. */
3743 					*i1 = udp->udp_multicast_ttl;
3744 				} else {
3745 					udp->udp_multicast_ttl =
3746 					    ipp->ipp_multicast_hops =
3747 					    (uint8_t)*i1;
3748 					ipp->ipp_fields |= IPPF_MULTICAST_HOPS;
3749 				}
3750 			}
3751 			break;
3752 		case IPV6_MULTICAST_LOOP:
3753 			if (*i1 != 0 && *i1 != 1) {
3754 				*outlenp = 0;
3755 				return (EINVAL);
3756 			}
3757 			if (!checkonly)
3758 				connp->conn_multicast_loop = *i1;
3759 			break;
3760 		case IPV6_JOIN_GROUP:
3761 		case IPV6_LEAVE_GROUP:
3762 		case MCAST_JOIN_GROUP:
3763 		case MCAST_LEAVE_GROUP:
3764 		case MCAST_BLOCK_SOURCE:
3765 		case MCAST_UNBLOCK_SOURCE:
3766 		case MCAST_JOIN_SOURCE_GROUP:
3767 		case MCAST_LEAVE_SOURCE_GROUP:
3768 			/*
3769 			 * "soft" error (negative)
3770 			 * option not handled at this level
3771 			 * Note: Do not modify *outlenp
3772 			 */
3773 			return (-EINVAL);
3774 		case IPV6_BOUND_IF:
3775 			if (!checkonly)
3776 				udp->udp_bound_if = *i1;
3777 			break;
3778 		case IPV6_UNSPEC_SRC:
3779 			if (!checkonly)
3780 				udp->udp_unspec_source = onoff;
3781 			break;
3782 		/*
3783 		 * Set boolean switches for ancillary data delivery
3784 		 */
3785 		case IPV6_RECVPKTINFO:
3786 			if (!checkonly)
3787 				udp->udp_ipv6_recvpktinfo = onoff;
3788 			break;
3789 		case IPV6_RECVTCLASS:
3790 			if (!checkonly) {
3791 				udp->udp_ipv6_recvtclass = onoff;
3792 			}
3793 			break;
3794 		case IPV6_RECVPATHMTU:
3795 			if (!checkonly) {
3796 				udp->udp_ipv6_recvpathmtu = onoff;
3797 			}
3798 			break;
3799 		case IPV6_RECVHOPLIMIT:
3800 			if (!checkonly)
3801 				udp->udp_ipv6_recvhoplimit = onoff;
3802 			break;
3803 		case IPV6_RECVHOPOPTS:
3804 			if (!checkonly)
3805 				udp->udp_ipv6_recvhopopts = onoff;
3806 			break;
3807 		case IPV6_RECVDSTOPTS:
3808 			if (!checkonly)
3809 				udp->udp_ipv6_recvdstopts = onoff;
3810 			break;
3811 		case _OLD_IPV6_RECVDSTOPTS:
3812 			if (!checkonly)
3813 				udp->udp_old_ipv6_recvdstopts = onoff;
3814 			break;
3815 		case IPV6_RECVRTHDRDSTOPTS:
3816 			if (!checkonly)
3817 				udp->udp_ipv6_recvrthdrdstopts = onoff;
3818 			break;
3819 		case IPV6_RECVRTHDR:
3820 			if (!checkonly)
3821 				udp->udp_ipv6_recvrthdr = onoff;
3822 			break;
3823 		/*
3824 		 * Set sticky options or ancillary data.
3825 		 * If sticky options, (re)build any extension headers
3826 		 * that might be needed as a result.
3827 		 */
3828 		case IPV6_PKTINFO:
3829 			/*
3830 			 * The source address and ifindex are verified
3831 			 * in ip_opt_set(). For ancillary data the
3832 			 * source address is checked in ip_wput_v6.
3833 			 */
3834 			if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
3835 				return (EINVAL);
3836 			if (checkonly)
3837 				break;
3838 
3839 			if (inlen == 0) {
3840 				ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR);
3841 				ipp->ipp_sticky_ignored |=
3842 				    (IPPF_IFINDEX|IPPF_ADDR);
3843 			} else {
3844 				struct in6_pktinfo *pkti;
3845 
3846 				pkti = (struct in6_pktinfo *)invalp;
3847 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
3848 				ipp->ipp_addr = pkti->ipi6_addr;
3849 				if (ipp->ipp_ifindex != 0)
3850 					ipp->ipp_fields |= IPPF_IFINDEX;
3851 				else
3852 					ipp->ipp_fields &= ~IPPF_IFINDEX;
3853 				if (!IN6_IS_ADDR_UNSPECIFIED(
3854 				    &ipp->ipp_addr))
3855 					ipp->ipp_fields |= IPPF_ADDR;
3856 				else
3857 					ipp->ipp_fields &= ~IPPF_ADDR;
3858 			}
3859 			if (sticky) {
3860 				error = udp_build_hdrs(q, udp);
3861 				if (error != 0)
3862 					return (error);
3863 			}
3864 			break;
3865 		case IPV6_HOPLIMIT:
3866 			if (sticky)
3867 				return (EINVAL);
3868 			if (inlen != 0 && inlen != sizeof (int))
3869 				return (EINVAL);
3870 			if (checkonly)
3871 				break;
3872 
3873 			if (inlen == 0) {
3874 				ipp->ipp_fields &= ~IPPF_HOPLIMIT;
3875 				ipp->ipp_sticky_ignored |= IPPF_HOPLIMIT;
3876 			} else {
3877 				if (*i1 > 255 || *i1 < -1)
3878 					return (EINVAL);
3879 				if (*i1 == -1)
3880 					ipp->ipp_hoplimit = udp_ipv6_hoplimit;
3881 				else
3882 					ipp->ipp_hoplimit = *i1;
3883 				ipp->ipp_fields |= IPPF_HOPLIMIT;
3884 			}
3885 			break;
3886 		case IPV6_TCLASS:
3887 			if (inlen != 0 && inlen != sizeof (int))
3888 				return (EINVAL);
3889 			if (checkonly)
3890 				break;
3891 
3892 			if (inlen == 0) {
3893 				ipp->ipp_fields &= ~IPPF_TCLASS;
3894 				ipp->ipp_sticky_ignored |= IPPF_TCLASS;
3895 			} else {
3896 				if (*i1 > 255 || *i1 < -1)
3897 					return (EINVAL);
3898 				if (*i1 == -1)
3899 					ipp->ipp_tclass = 0;
3900 				else
3901 					ipp->ipp_tclass = *i1;
3902 				ipp->ipp_fields |= IPPF_TCLASS;
3903 			}
3904 			if (sticky) {
3905 				error = udp_build_hdrs(q, udp);
3906 				if (error != 0)
3907 					return (error);
3908 			}
3909 			break;
3910 		case IPV6_NEXTHOP:
3911 			/*
3912 			 * IP will verify that the nexthop is reachable
3913 			 * and fail for sticky options.
3914 			 */
3915 			if (inlen != 0 && inlen != sizeof (sin6_t))
3916 				return (EINVAL);
3917 			if (checkonly)
3918 				break;
3919 
3920 			if (inlen == 0) {
3921 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
3922 				ipp->ipp_sticky_ignored |= IPPF_NEXTHOP;
3923 			} else {
3924 				sin6_t *sin6 = (sin6_t *)invalp;
3925 
3926 				if (sin6->sin6_family != AF_INET6)
3927 					return (EAFNOSUPPORT);
3928 				if (IN6_IS_ADDR_V4MAPPED(
3929 				    &sin6->sin6_addr))
3930 					return (EADDRNOTAVAIL);
3931 				ipp->ipp_nexthop = sin6->sin6_addr;
3932 				if (!IN6_IS_ADDR_UNSPECIFIED(
3933 				    &ipp->ipp_nexthop))
3934 					ipp->ipp_fields |= IPPF_NEXTHOP;
3935 				else
3936 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
3937 			}
3938 			if (sticky) {
3939 				error = udp_build_hdrs(q, udp);
3940 				if (error != 0)
3941 					return (error);
3942 			}
3943 			break;
3944 		case IPV6_HOPOPTS: {
3945 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
3946 			/*
3947 			 * Sanity checks - minimum size, size a multiple of
3948 			 * eight bytes, and matching size passed in.
3949 			 */
3950 			if (inlen != 0 &&
3951 			    inlen != (8 * (hopts->ip6h_len + 1)))
3952 				return (EINVAL);
3953 
3954 			if (checkonly)
3955 				break;
3956 
3957 			error = optcom_pkt_set(invalp, inlen, sticky,
3958 			    (uchar_t **)&ipp->ipp_hopopts,
3959 			    &ipp->ipp_hopoptslen,
3960 			    sticky ? udp->udp_label_len_v6 : 0);
3961 			if (error != 0)
3962 				return (error);
3963 			if (ipp->ipp_hopoptslen == 0) {
3964 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
3965 				ipp->ipp_sticky_ignored |= IPPF_HOPOPTS;
3966 			} else {
3967 				ipp->ipp_fields |= IPPF_HOPOPTS;
3968 			}
3969 			if (sticky) {
3970 				error = udp_build_hdrs(q, udp);
3971 				if (error != 0)
3972 					return (error);
3973 			}
3974 			break;
3975 		}
3976 		case IPV6_RTHDRDSTOPTS: {
3977 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
3978 
3979 			/*
3980 			 * Sanity checks - minimum size, size a multiple of
3981 			 * eight bytes, and matching size passed in.
3982 			 */
3983 			if (inlen != 0 &&
3984 			    inlen != (8 * (dopts->ip6d_len + 1)))
3985 				return (EINVAL);
3986 
3987 			if (checkonly)
3988 				break;
3989 
3990 			if (inlen == 0) {
3991 				if (sticky &&
3992 				    (ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) {
3993 					kmem_free(ipp->ipp_rtdstopts,
3994 					    ipp->ipp_rtdstoptslen);
3995 					ipp->ipp_rtdstopts = NULL;
3996 					ipp->ipp_rtdstoptslen = 0;
3997 				}
3998 
3999 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
4000 				ipp->ipp_sticky_ignored |= IPPF_RTDSTOPTS;
4001 			} else {
4002 				error = optcom_pkt_set(invalp, inlen, sticky,
4003 				    (uchar_t **)&ipp->ipp_rtdstopts,
4004 				    &ipp->ipp_rtdstoptslen, 0);
4005 				if (error != 0)
4006 					return (error);
4007 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
4008 			}
4009 			if (sticky) {
4010 				error = udp_build_hdrs(q, udp);
4011 				if (error != 0)
4012 					return (error);
4013 			}
4014 			break;
4015 		}
4016 		case IPV6_DSTOPTS: {
4017 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
4018 
4019 			/*
4020 			 * Sanity checks - minimum size, size a multiple of
4021 			 * eight bytes, and matching size passed in.
4022 			 */
4023 			if (inlen != 0 &&
4024 			    inlen != (8 * (dopts->ip6d_len + 1)))
4025 				return (EINVAL);
4026 
4027 			if (checkonly)
4028 				break;
4029 
4030 			if (inlen == 0) {
4031 				if (sticky &&
4032 				    (ipp->ipp_fields & IPPF_DSTOPTS) != 0) {
4033 					kmem_free(ipp->ipp_dstopts,
4034 					    ipp->ipp_dstoptslen);
4035 					ipp->ipp_dstopts = NULL;
4036 					ipp->ipp_dstoptslen = 0;
4037 				}
4038 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
4039 				ipp->ipp_sticky_ignored |= IPPF_DSTOPTS;
4040 			} else {
4041 				error = optcom_pkt_set(invalp, inlen, sticky,
4042 				    (uchar_t **)&ipp->ipp_dstopts,
4043 				    &ipp->ipp_dstoptslen, 0);
4044 				if (error != 0)
4045 					return (error);
4046 				ipp->ipp_fields |= IPPF_DSTOPTS;
4047 			}
4048 			if (sticky) {
4049 				error = udp_build_hdrs(q, udp);
4050 				if (error != 0)
4051 					return (error);
4052 			}
4053 			break;
4054 		}
4055 		case IPV6_RTHDR: {
4056 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
4057 
4058 			/*
4059 			 * Sanity checks - minimum size, size a multiple of
4060 			 * eight bytes, and matching size passed in.
4061 			 */
4062 			if (inlen != 0 &&
4063 			    inlen != (8 * (rt->ip6r_len + 1)))
4064 				return (EINVAL);
4065 
4066 			if (checkonly)
4067 				break;
4068 
4069 			if (inlen == 0) {
4070 				if (sticky &&
4071 				    (ipp->ipp_fields & IPPF_RTHDR) != 0) {
4072 					kmem_free(ipp->ipp_rthdr,
4073 					    ipp->ipp_rthdrlen);
4074 					ipp->ipp_rthdr = NULL;
4075 					ipp->ipp_rthdrlen = 0;
4076 				}
4077 				ipp->ipp_fields &= ~IPPF_RTHDR;
4078 				ipp->ipp_sticky_ignored |= IPPF_RTHDR;
4079 			} else {
4080 				error = optcom_pkt_set(invalp, inlen, sticky,
4081 				    (uchar_t **)&ipp->ipp_rthdr,
4082 				    &ipp->ipp_rthdrlen, 0);
4083 				if (error != 0)
4084 					return (error);
4085 				ipp->ipp_fields |= IPPF_RTHDR;
4086 			}
4087 			if (sticky) {
4088 				error = udp_build_hdrs(q, udp);
4089 				if (error != 0)
4090 					return (error);
4091 			}
4092 			break;
4093 		}
4094 
4095 		case IPV6_DONTFRAG:
4096 			if (checkonly)
4097 				break;
4098 
4099 			if (onoff) {
4100 				ipp->ipp_fields |= IPPF_DONTFRAG;
4101 			} else {
4102 				ipp->ipp_fields &= ~IPPF_DONTFRAG;
4103 			}
4104 			break;
4105 
4106 		case IPV6_USE_MIN_MTU:
4107 			if (inlen != sizeof (int))
4108 				return (EINVAL);
4109 
4110 			if (*i1 < -1 || *i1 > 1)
4111 				return (EINVAL);
4112 
4113 			if (checkonly)
4114 				break;
4115 
4116 			ipp->ipp_fields |= IPPF_USE_MIN_MTU;
4117 			ipp->ipp_use_min_mtu = *i1;
4118 			break;
4119 
4120 		case IPV6_BOUND_PIF:
4121 		case IPV6_SEC_OPT:
4122 		case IPV6_DONTFAILOVER_IF:
4123 		case IPV6_SRC_PREFERENCES:
4124 		case IPV6_V6ONLY:
4125 			/* Handled at the IP level */
4126 			return (-EINVAL);
4127 		default:
4128 			*outlenp = 0;
4129 			return (EINVAL);
4130 		}
4131 		break;
4132 		}		/* end IPPROTO_IPV6 */
4133 	case IPPROTO_UDP:
4134 		switch (name) {
4135 		case UDP_ANONPRIVBIND:
4136 			if ((error = secpolicy_net_privaddr(cr, 0)) != 0) {
4137 				*outlenp = 0;
4138 				return (error);
4139 			}
4140 			if (!checkonly) {
4141 				udp->udp_anon_priv_bind = onoff;
4142 			}
4143 			break;
4144 		case UDP_EXCLBIND:
4145 			if (!checkonly)
4146 				udp->udp_exclbind = onoff;
4147 			break;
4148 		case UDP_RCVHDR:
4149 			if (!checkonly)
4150 				udp->udp_rcvhdr = onoff;
4151 			break;
4152 		default:
4153 			*outlenp = 0;
4154 			return (EINVAL);
4155 		}
4156 		break;
4157 	default:
4158 		*outlenp = 0;
4159 		return (EINVAL);
4160 	}
4161 	/*
4162 	 * Common case of OK return with outval same as inval.
4163 	 */
4164 	if (invalp != outvalp) {
4165 		/* don't trust bcopy for identical src/dst */
4166 		(void) bcopy(invalp, outvalp, inlen);
4167 	}
4168 	*outlenp = inlen;
4169 	return (0);
4170 }
4171 
4172 /*
4173  * Update udp_sticky_hdrs based on udp_sticky_ipp, udp_v6src, and udp_ttl.
4174  * The headers include ip6i_t (if needed), ip6_t, any sticky extension
4175  * headers, and the udp header.
4176  * Returns failure if can't allocate memory.
4177  */
4178 static int
4179 udp_build_hdrs(queue_t *q, udp_t *udp)
4180 {
4181 	uchar_t	*hdrs;
4182 	uint_t	hdrs_len;
4183 	ip6_t	*ip6h;
4184 	ip6i_t	*ip6i;
4185 	udpha_t	*udpha;
4186 	ip6_pkt_t *ipp = &udp->udp_sticky_ipp;
4187 
4188 	hdrs_len = ip_total_hdrs_len_v6(ipp) + UDPH_SIZE;
4189 	ASSERT(hdrs_len != 0);
4190 	if (hdrs_len != udp->udp_sticky_hdrs_len) {
4191 		/* Need to reallocate */
4192 		hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
4193 		if (hdrs == NULL)
4194 			return (ENOMEM);
4195 
4196 		if (udp->udp_sticky_hdrs_len != 0) {
4197 			kmem_free(udp->udp_sticky_hdrs,
4198 			    udp->udp_sticky_hdrs_len);
4199 		}
4200 		udp->udp_sticky_hdrs = hdrs;
4201 		udp->udp_sticky_hdrs_len = hdrs_len;
4202 	}
4203 	ip_build_hdrs_v6(udp->udp_sticky_hdrs,
4204 	    udp->udp_sticky_hdrs_len - UDPH_SIZE, ipp, IPPROTO_UDP);
4205 
4206 	/* Set header fields not in ipp */
4207 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
4208 		ip6i = (ip6i_t *)udp->udp_sticky_hdrs;
4209 		ip6h = (ip6_t *)&ip6i[1];
4210 	} else {
4211 		ip6h = (ip6_t *)udp->udp_sticky_hdrs;
4212 	}
4213 
4214 	if (!(ipp->ipp_fields & IPPF_ADDR))
4215 		ip6h->ip6_src = udp->udp_v6src;
4216 
4217 	udpha = (udpha_t *)(udp->udp_sticky_hdrs + hdrs_len - UDPH_SIZE);
4218 	udpha->uha_src_port = udp->udp_port;
4219 
4220 	/* Try to get everything in a single mblk */
4221 	if (hdrs_len > udp->udp_max_hdr_len) {
4222 		udp->udp_max_hdr_len = hdrs_len;
4223 		(void) mi_set_sth_wroff(RD(q), udp->udp_max_hdr_len +
4224 		    udp_wroff_extra);
4225 	}
4226 	return (0);
4227 }
4228 
4229 /*
4230  * This routine retrieves the value of an ND variable in a udpparam_t
4231  * structure.  It is called through nd_getset when a user reads the
4232  * variable.
4233  */
4234 /* ARGSUSED */
4235 static int
4236 udp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
4237 {
4238 	udpparam_t *udppa = (udpparam_t *)cp;
4239 
4240 	(void) mi_mpprintf(mp, "%d", udppa->udp_param_value);
4241 	return (0);
4242 }
4243 
4244 /*
4245  * Walk through the param array specified registering each element with the
4246  * named dispatch (ND) handler.
4247  */
4248 static boolean_t
4249 udp_param_register(udpparam_t *udppa, int cnt)
4250 {
4251 	for (; cnt-- > 0; udppa++) {
4252 		if (udppa->udp_param_name && udppa->udp_param_name[0]) {
4253 			if (!nd_load(&udp_g_nd, udppa->udp_param_name,
4254 			    udp_param_get, udp_param_set,
4255 			    (caddr_t)udppa)) {
4256 				nd_free(&udp_g_nd);
4257 				return (B_FALSE);
4258 			}
4259 		}
4260 	}
4261 	if (!nd_load(&udp_g_nd, "udp_extra_priv_ports",
4262 	    udp_extra_priv_ports_get, NULL, NULL)) {
4263 		nd_free(&udp_g_nd);
4264 		return (B_FALSE);
4265 	}
4266 	if (!nd_load(&udp_g_nd, "udp_extra_priv_ports_add",
4267 	    NULL, udp_extra_priv_ports_add, NULL)) {
4268 		nd_free(&udp_g_nd);
4269 		return (B_FALSE);
4270 	}
4271 	if (!nd_load(&udp_g_nd, "udp_extra_priv_ports_del",
4272 	    NULL, udp_extra_priv_ports_del, NULL)) {
4273 		nd_free(&udp_g_nd);
4274 		return (B_FALSE);
4275 	}
4276 	if (!nd_load(&udp_g_nd, "udp_status", udp_status_report, NULL,
4277 	    NULL)) {
4278 		nd_free(&udp_g_nd);
4279 		return (B_FALSE);
4280 	}
4281 	if (!nd_load(&udp_g_nd, "udp_bind_hash", udp_bind_hash_report, NULL,
4282 	    NULL)) {
4283 		nd_free(&udp_g_nd);
4284 		return (B_FALSE);
4285 	}
4286 	return (B_TRUE);
4287 }
4288 
4289 /* This routine sets an ND variable in a udpparam_t structure. */
4290 /* ARGSUSED */
4291 static int
4292 udp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
4293 {
4294 	long		new_value;
4295 	udpparam_t	*udppa = (udpparam_t *)cp;
4296 
4297 	/*
4298 	 * Fail the request if the new value does not lie within the
4299 	 * required bounds.
4300 	 */
4301 	if (ddi_strtol(value, NULL, 10, &new_value) != 0 ||
4302 	    new_value < udppa->udp_param_min ||
4303 	    new_value > udppa->udp_param_max) {
4304 		return (EINVAL);
4305 	}
4306 
4307 	/* Set the new value */
4308 	udppa->udp_param_value = new_value;
4309 	return (0);
4310 }
4311 
4312 /*
4313  * Copy hop-by-hop option from ipp->ipp_hopopts to the buffer provided (with
4314  * T_opthdr) and return the number of bytes copied.  'dbuf' may be NULL to
4315  * just count the length needed for allocation.  If 'dbuf' is non-NULL,
4316  * then it's assumed to be allocated to be large enough.
4317  *
4318  * Returns zero if trimming of the security option causes all options to go
4319  * away.
4320  */
4321 static size_t
4322 copy_hop_opts(const ip6_pkt_t *ipp, uchar_t *dbuf)
4323 {
4324 	struct T_opthdr *toh;
4325 	size_t hol = ipp->ipp_hopoptslen;
4326 	ip6_hbh_t *dstopt = NULL;
4327 	const ip6_hbh_t *srcopt = ipp->ipp_hopopts;
4328 	size_t tlen, olen, plen;
4329 	boolean_t deleting;
4330 	const struct ip6_opt *sopt, *lastpad;
4331 	struct ip6_opt *dopt;
4332 
4333 	if ((toh = (struct T_opthdr *)dbuf) != NULL) {
4334 		toh->level = IPPROTO_IPV6;
4335 		toh->name = IPV6_HOPOPTS;
4336 		toh->status = 0;
4337 		dstopt = (ip6_hbh_t *)(toh + 1);
4338 	}
4339 
4340 	/*
4341 	 * If labeling is enabled, then skip the label option
4342 	 * but get other options if there are any.
4343 	 */
4344 	if (is_system_labeled()) {
4345 		dopt = NULL;
4346 		if (dstopt != NULL) {
4347 			/* will fill in ip6h_len later */
4348 			dstopt->ip6h_nxt = srcopt->ip6h_nxt;
4349 			dopt = (struct ip6_opt *)(dstopt + 1);
4350 		}
4351 		sopt = (const struct ip6_opt *)(srcopt + 1);
4352 		hol -= sizeof (*srcopt);
4353 		tlen = sizeof (*dstopt);
4354 		lastpad = NULL;
4355 		deleting = B_FALSE;
4356 		/*
4357 		 * This loop finds the first (lastpad pointer) of any number of
4358 		 * pads that preceeds the security option, then treats the
4359 		 * security option as though it were a pad, and then finds the
4360 		 * next non-pad option (or end of list).
4361 		 *
4362 		 * It then treats the entire block as one big pad.  To preserve
4363 		 * alignment of any options that follow, or just the end of the
4364 		 * list, it computes a minimal new padding size that keeps the
4365 		 * same alignment for the next option.
4366 		 *
4367 		 * If it encounters just a sequence of pads with no security
4368 		 * option, those are copied as-is rather than collapsed.
4369 		 *
4370 		 * Note that to handle the end of list case, the code makes one
4371 		 * loop with 'hol' set to zero.
4372 		 */
4373 		for (;;) {
4374 			if (hol > 0) {
4375 				if (sopt->ip6o_type == IP6OPT_PAD1) {
4376 					if (lastpad == NULL)
4377 						lastpad = sopt;
4378 					sopt = (const struct ip6_opt *)
4379 					    &sopt->ip6o_len;
4380 					hol--;
4381 					continue;
4382 				}
4383 				olen = sopt->ip6o_len + sizeof (*sopt);
4384 				if (olen > hol)
4385 					olen = hol;
4386 				if (sopt->ip6o_type == IP6OPT_PADN ||
4387 				    sopt->ip6o_type == ip6opt_ls) {
4388 					if (sopt->ip6o_type == ip6opt_ls)
4389 						deleting = B_TRUE;
4390 					if (lastpad == NULL)
4391 						lastpad = sopt;
4392 					sopt = (const struct ip6_opt *)
4393 					    ((const char *)sopt + olen);
4394 					hol -= olen;
4395 					continue;
4396 				}
4397 			} else {
4398 				/* if nothing was copied at all, then delete */
4399 				if (tlen == sizeof (*dstopt))
4400 					return (0);
4401 				/* last pass; pick up any trailing padding */
4402 				olen = 0;
4403 			}
4404 			if (deleting) {
4405 				/*
4406 				 * compute aligning effect of deleted material
4407 				 * to reproduce with pad.
4408 				 */
4409 				plen = ((const char *)sopt -
4410 				    (const char *)lastpad) & 7;
4411 				tlen += plen;
4412 				if (dopt != NULL) {
4413 					if (plen == 1) {
4414 						dopt->ip6o_type = IP6OPT_PAD1;
4415 					} else if (plen > 1) {
4416 						plen -= sizeof (*dopt);
4417 						dopt->ip6o_type = IP6OPT_PADN;
4418 						dopt->ip6o_len = plen;
4419 						if (plen > 0)
4420 							bzero(dopt + 1, plen);
4421 					}
4422 					dopt = (struct ip6_opt *)
4423 					    ((char *)dopt + plen);
4424 				}
4425 				deleting = B_FALSE;
4426 				lastpad = NULL;
4427 			}
4428 			/* if there's uncopied padding, then copy that now */
4429 			if (lastpad != NULL) {
4430 				olen += (const char *)sopt -
4431 				    (const char *)lastpad;
4432 				sopt = lastpad;
4433 				lastpad = NULL;
4434 			}
4435 			if (dopt != NULL && olen > 0) {
4436 				bcopy(sopt, dopt, olen);
4437 				dopt = (struct ip6_opt *)((char *)dopt + olen);
4438 			}
4439 			if (hol == 0)
4440 				break;
4441 			tlen += olen;
4442 			sopt = (const struct ip6_opt *)
4443 			    ((const char *)sopt + olen);
4444 			hol -= olen;
4445 		}
4446 		/* go back and patch up the length value, rounded upward */
4447 		if (dstopt != NULL)
4448 			dstopt->ip6h_len = (tlen - 1) >> 3;
4449 	} else {
4450 		tlen = hol;
4451 		if (dstopt != NULL)
4452 			bcopy(srcopt, dstopt, hol);
4453 	}
4454 
4455 	tlen += sizeof (*toh);
4456 	if (toh != NULL)
4457 		toh->len = tlen;
4458 
4459 	return (tlen);
4460 }
4461 
4462 static void
4463 udp_input(conn_t *connp, mblk_t *mp)
4464 {
4465 	struct T_unitdata_ind	*tudi;
4466 	uchar_t			*rptr;		/* Pointer to IP header */
4467 	int			hdr_length;	/* Length of IP+UDP headers */
4468 	int			udi_size;	/* Size of T_unitdata_ind */
4469 	int			mp_len;
4470 	udp_t			*udp;
4471 	udpha_t			*udpha;
4472 	int			ipversion;
4473 	ip6_pkt_t		ipp;
4474 	ip6_t			*ip6h;
4475 	ip6i_t			*ip6i;
4476 	mblk_t			*mp1;
4477 	mblk_t			*options_mp = NULL;
4478 	in_pktinfo_t		*pinfo = NULL;
4479 	cred_t			*cr = NULL;
4480 	queue_t			*q = connp->conn_rq;
4481 	pid_t			cpid;
4482 	cred_t			*rcr = connp->conn_cred;
4483 
4484 	TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_START,
4485 	    "udp_rput_start: q %p mp %p", q, mp);
4486 
4487 	udp = connp->conn_udp;
4488 	rptr = mp->b_rptr;
4489 	ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_CTL);
4490 	ASSERT(OK_32PTR(rptr));
4491 
4492 	/*
4493 	 * IP should have prepended the options data in an M_CTL
4494 	 * Check M_CTL "type" to make sure are not here bcos of
4495 	 * a valid ICMP message
4496 	 */
4497 	if (DB_TYPE(mp) == M_CTL) {
4498 		if (MBLKL(mp) == sizeof (in_pktinfo_t) &&
4499 		    ((in_pktinfo_t *)mp->b_rptr)->in_pkt_ulp_type ==
4500 		    IN_PKTINFO) {
4501 			/*
4502 			 * IP_RECVIF or IP_RECVSLLA information has been
4503 			 * appended to the packet by IP. We need to
4504 			 * extract the mblk and adjust the rptr
4505 			 */
4506 			pinfo = (in_pktinfo_t *)mp->b_rptr;
4507 			options_mp = mp;
4508 			mp = mp->b_cont;
4509 			rptr = mp->b_rptr;
4510 			UDP_STAT(udp_in_pktinfo);
4511 		} else {
4512 			/*
4513 			 * ICMP messages.
4514 			 */
4515 			udp_icmp_error(q, mp);
4516 			TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END,
4517 				"udp_rput_end: q %p (%S)", q, "m_ctl");
4518 			return;
4519 		}
4520 	}
4521 
4522 	mp_len = msgdsize(mp);
4523 	/*
4524 	 * This is the inbound data path.
4525 	 * First, we check to make sure the IP version number is correct,
4526 	 * and then pull the IP and UDP headers into the first mblk.
4527 	 * Assume IP provides aligned packets - otherwise toss.
4528 	 * Also, check if we have a complete IP header.
4529 	 */
4530 
4531 	/* Initialize regardless if ipversion is IPv4 or IPv6 */
4532 	ipp.ipp_fields = 0;
4533 
4534 	ipversion = IPH_HDR_VERSION(rptr);
4535 	switch (ipversion) {
4536 	case IPV4_VERSION:
4537 		ASSERT(MBLKL(mp) >= sizeof (ipha_t));
4538 		ASSERT(((ipha_t *)rptr)->ipha_protocol == IPPROTO_UDP);
4539 		hdr_length = IPH_HDR_LENGTH(rptr) + UDPH_SIZE;
4540 		if ((hdr_length > IP_SIMPLE_HDR_LENGTH + UDPH_SIZE) ||
4541 		    (udp->udp_ip_rcv_options_len)) {
4542 			/*
4543 			 * Handle IPv4 packets with options outside of the
4544 			 * main data path. Not needed for AF_INET6 sockets
4545 			 * since they don't support a getsockopt of IP_OPTIONS.
4546 			 */
4547 			if (udp->udp_family == AF_INET6)
4548 				break;
4549 			/*
4550 			 * UDP length check performed for IPv4 packets with
4551 			 * options to check whether UDP length specified in
4552 			 * the header is the same as the physical length of
4553 			 * the packet.
4554 			 */
4555 			udpha = (udpha_t *)(rptr + (hdr_length - UDPH_SIZE));
4556 			if (mp_len != (ntohs(udpha->uha_length) +
4557 			    hdr_length - UDPH_SIZE)) {
4558 				goto tossit;
4559 			}
4560 			/*
4561 			 * Handle the case where the packet has IP options
4562 			 * and the IP_RECVSLLA & IP_RECVIF are set
4563 			 */
4564 			if (pinfo != NULL)
4565 				mp = options_mp;
4566 			udp_become_writer(connp, mp, udp_rput_other_wrapper,
4567 			    SQTAG_UDP_INPUT);
4568 			TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END,
4569 				"udp_rput_end: q %p (%S)", q, "end");
4570 			return;
4571 		}
4572 
4573 		/* Handle IPV6_RECVHOPLIMIT. */
4574 		if ((udp->udp_family == AF_INET6) && (pinfo != NULL) &&
4575 		    udp->udp_ipv6_recvpktinfo) {
4576 			if (pinfo->in_pkt_flags & IPF_RECVIF) {
4577 				ipp.ipp_fields |= IPPF_IFINDEX;
4578 				ipp.ipp_ifindex = pinfo->in_pkt_ifindex;
4579 			}
4580 		}
4581 		break;
4582 	case IPV6_VERSION:
4583 		/*
4584 		 * IPv6 packets can only be received by applications
4585 		 * that are prepared to receive IPv6 addresses.
4586 		 * The IP fanout must ensure this.
4587 		 */
4588 		ASSERT(udp->udp_family == AF_INET6);
4589 
4590 		ip6h = (ip6_t *)rptr;
4591 		ASSERT((uchar_t *)&ip6h[1] <= mp->b_wptr);
4592 
4593 		if (ip6h->ip6_nxt != IPPROTO_UDP) {
4594 			uint8_t nexthdrp;
4595 			/* Look for ifindex information */
4596 			if (ip6h->ip6_nxt == IPPROTO_RAW) {
4597 				ip6i = (ip6i_t *)ip6h;
4598 				if ((uchar_t *)&ip6i[1] > mp->b_wptr)
4599 					goto tossit;
4600 
4601 				if (ip6i->ip6i_flags & IP6I_IFINDEX) {
4602 					ASSERT(ip6i->ip6i_ifindex != 0);
4603 					ipp.ipp_fields |= IPPF_IFINDEX;
4604 					ipp.ipp_ifindex = ip6i->ip6i_ifindex;
4605 				}
4606 				rptr = (uchar_t *)&ip6i[1];
4607 				mp->b_rptr = rptr;
4608 				if (rptr == mp->b_wptr) {
4609 					mp1 = mp->b_cont;
4610 					freeb(mp);
4611 					mp = mp1;
4612 					rptr = mp->b_rptr;
4613 				}
4614 				if (MBLKL(mp) < (IPV6_HDR_LEN + UDPH_SIZE))
4615 					goto tossit;
4616 				ip6h = (ip6_t *)rptr;
4617 				mp_len = msgdsize(mp);
4618 			}
4619 			/*
4620 			 * Find any potentially interesting extension headers
4621 			 * as well as the length of the IPv6 + extension
4622 			 * headers.
4623 			 */
4624 			hdr_length = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp) +
4625 			    UDPH_SIZE;
4626 			ASSERT(nexthdrp == IPPROTO_UDP);
4627 		} else {
4628 			hdr_length = IPV6_HDR_LEN + UDPH_SIZE;
4629 			ip6i = NULL;
4630 		}
4631 		break;
4632 	default:
4633 		ASSERT(0);
4634 	}
4635 
4636 	/*
4637 	 * IP inspected the UDP header thus all of it must be in the mblk.
4638 	 * UDP length check is performed for IPv6 packets and IPv4 packets
4639 	 * without options to check if the size of the packet as specified
4640 	 * by the header is the same as the physical size of the packet.
4641 	 */
4642 	udpha = (udpha_t *)(rptr + (hdr_length - UDPH_SIZE));
4643 	if ((MBLKL(mp) < hdr_length) ||
4644 	    (mp_len != (ntohs(udpha->uha_length) + hdr_length - UDPH_SIZE))) {
4645 		goto tossit;
4646 	}
4647 
4648 	/* Walk past the headers. */
4649 	if (!udp->udp_rcvhdr) {
4650 		mp->b_rptr = rptr + hdr_length;
4651 		mp_len -= hdr_length;
4652 	}
4653 
4654 	/*
4655 	 * This is the inbound data path.  Packets are passed upstream as
4656 	 * T_UNITDATA_IND messages with full IP headers still attached.
4657 	 */
4658 	if (udp->udp_family == AF_INET) {
4659 		sin_t *sin;
4660 
4661 		ASSERT(IPH_HDR_VERSION((ipha_t *)rptr) == IPV4_VERSION);
4662 
4663 		/*
4664 		 * Normally only send up the address.
4665 		 * If IP_RECVDSTADDR is set we include the destination IP
4666 		 * address as an option. With IP_RECVOPTS we include all
4667 		 * the IP options. Only ip_rput_other() handles packets
4668 		 * that contain IP options.
4669 		 */
4670 		udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin_t);
4671 		if (udp->udp_recvdstaddr) {
4672 			udi_size += sizeof (struct T_opthdr) +
4673 			    sizeof (struct in_addr);
4674 			UDP_STAT(udp_in_recvdstaddr);
4675 		}
4676 
4677 		/*
4678 		 * If the IP_RECVSLLA or the IP_RECVIF is set then allocate
4679 		 * space accordingly
4680 		 */
4681 		if (udp->udp_recvif && (pinfo != NULL) &&
4682 		    (pinfo->in_pkt_flags & IPF_RECVIF)) {
4683 			udi_size += sizeof (struct T_opthdr) + sizeof (uint_t);
4684 			UDP_STAT(udp_in_recvif);
4685 		}
4686 
4687 		if (udp->udp_recvslla && (pinfo != NULL) &&
4688 		    (pinfo->in_pkt_flags & IPF_RECVSLLA)) {
4689 			udi_size += sizeof (struct T_opthdr) +
4690 			    sizeof (struct sockaddr_dl);
4691 			UDP_STAT(udp_in_recvslla);
4692 		}
4693 
4694 		if (udp->udp_recvucred && (cr = DB_CRED(mp)) != NULL) {
4695 			udi_size += sizeof (struct T_opthdr) + ucredsize;
4696 			cpid = DB_CPID(mp);
4697 			UDP_STAT(udp_in_recvucred);
4698 		}
4699 
4700 		/*
4701 		 * If SO_TIMESTAMP is set allocate the appropriate sized
4702 		 * buffer. Since gethrestime() expects a pointer aligned
4703 		 * argument, we allocate space necessary for extra
4704 		 * alignment (even though it might not be used).
4705 		 */
4706 		if (udp->udp_timestamp) {
4707 			udi_size += sizeof (struct T_opthdr) +
4708 			    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
4709 			UDP_STAT(udp_in_timestamp);
4710 		}
4711 
4712 		/*
4713 		 * If IP_RECVTTL is set allocate the appropriate sized buffer
4714 		 */
4715 		if (udp->udp_recvttl) {
4716 			udi_size += sizeof (struct T_opthdr) + sizeof (uint8_t);
4717 			UDP_STAT(udp_in_recvttl);
4718 		}
4719 		ASSERT(IPH_HDR_LENGTH((ipha_t *)rptr) == IP_SIMPLE_HDR_LENGTH);
4720 
4721 		/* Allocate a message block for the T_UNITDATA_IND structure. */
4722 		mp1 = allocb(udi_size, BPRI_MED);
4723 		if (mp1 == NULL) {
4724 			freemsg(mp);
4725 			if (options_mp != NULL)
4726 				freeb(options_mp);
4727 			TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END,
4728 				"udp_rput_end: q %p (%S)", q, "allocbfail");
4729 			BUMP_MIB(&udp_mib, udpInErrors);
4730 			return;
4731 		}
4732 		mp1->b_cont = mp;
4733 		mp = mp1;
4734 		mp->b_datap->db_type = M_PROTO;
4735 		tudi = (struct T_unitdata_ind *)mp->b_rptr;
4736 		mp->b_wptr = (uchar_t *)tudi + udi_size;
4737 		tudi->PRIM_type = T_UNITDATA_IND;
4738 		tudi->SRC_length = sizeof (sin_t);
4739 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
4740 		tudi->OPT_offset = sizeof (struct T_unitdata_ind) +
4741 		    sizeof (sin_t);
4742 		udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
4743 		tudi->OPT_length = udi_size;
4744 		sin = (sin_t *)&tudi[1];
4745 		sin->sin_addr.s_addr = ((ipha_t *)rptr)->ipha_src;
4746 		sin->sin_port =	udpha->uha_src_port;
4747 		sin->sin_family = udp->udp_family;
4748 		*(uint32_t *)&sin->sin_zero[0] = 0;
4749 		*(uint32_t *)&sin->sin_zero[4] = 0;
4750 
4751 		/*
4752 		 * Add options if IP_RECVDSTADDR, IP_RECVIF, IP_RECVSLLA or
4753 		 * IP_RECVTTL has been set.
4754 		 */
4755 		if (udi_size != 0) {
4756 			/*
4757 			 * Copy in destination address before options to avoid
4758 			 * any padding issues.
4759 			 */
4760 			char *dstopt;
4761 
4762 			dstopt = (char *)&sin[1];
4763 			if (udp->udp_recvdstaddr) {
4764 				struct T_opthdr *toh;
4765 				ipaddr_t *dstptr;
4766 
4767 				toh = (struct T_opthdr *)dstopt;
4768 				toh->level = IPPROTO_IP;
4769 				toh->name = IP_RECVDSTADDR;
4770 				toh->len = sizeof (struct T_opthdr) +
4771 				    sizeof (ipaddr_t);
4772 				toh->status = 0;
4773 				dstopt += sizeof (struct T_opthdr);
4774 				dstptr = (ipaddr_t *)dstopt;
4775 				*dstptr = ((ipha_t *)rptr)->ipha_dst;
4776 				dstopt = (char *)toh + toh->len;
4777 				udi_size -= toh->len;
4778 			}
4779 
4780 			if (udp->udp_recvslla && (pinfo != NULL) &&
4781 			    (pinfo->in_pkt_flags & IPF_RECVSLLA)) {
4782 
4783 				struct T_opthdr *toh;
4784 				struct sockaddr_dl	*dstptr;
4785 
4786 				toh = (struct T_opthdr *)dstopt;
4787 				toh->level = IPPROTO_IP;
4788 				toh->name = IP_RECVSLLA;
4789 				toh->len = sizeof (struct T_opthdr) +
4790 					sizeof (struct sockaddr_dl);
4791 				toh->status = 0;
4792 				dstopt += sizeof (struct T_opthdr);
4793 				dstptr = (struct sockaddr_dl *)dstopt;
4794 				bcopy(&pinfo->in_pkt_slla, dstptr,
4795 				    sizeof (struct sockaddr_dl));
4796 				dstopt = (char *)toh + toh->len;
4797 				udi_size -= toh->len;
4798 			}
4799 
4800 			if (udp->udp_recvif && (pinfo != NULL) &&
4801 			    (pinfo->in_pkt_flags & IPF_RECVIF)) {
4802 
4803 				struct T_opthdr *toh;
4804 				uint_t		*dstptr;
4805 
4806 				toh = (struct T_opthdr *)dstopt;
4807 				toh->level = IPPROTO_IP;
4808 				toh->name = IP_RECVIF;
4809 				toh->len = sizeof (struct T_opthdr) +
4810 					sizeof (uint_t);
4811 				toh->status = 0;
4812 				dstopt += sizeof (struct T_opthdr);
4813 				dstptr = (uint_t *)dstopt;
4814 				*dstptr = pinfo->in_pkt_ifindex;
4815 				dstopt = (char *)toh + toh->len;
4816 				udi_size -= toh->len;
4817 			}
4818 
4819 			if (cr != NULL) {
4820 				struct T_opthdr *toh;
4821 
4822 				toh = (struct T_opthdr *)dstopt;
4823 				toh->level = SOL_SOCKET;
4824 				toh->name = SCM_UCRED;
4825 				toh->len = sizeof (struct T_opthdr) + ucredsize;
4826 				toh->status = 0;
4827 				(void) cred2ucred(cr, cpid, &toh[1], rcr);
4828 				dstopt = (char *)toh + toh->len;
4829 				udi_size -= toh->len;
4830 			}
4831 
4832 			if (udp->udp_timestamp) {
4833 				struct	T_opthdr *toh;
4834 
4835 				toh = (struct T_opthdr *)dstopt;
4836 				toh->level = SOL_SOCKET;
4837 				toh->name = SCM_TIMESTAMP;
4838 				toh->len = sizeof (struct T_opthdr) +
4839 				    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
4840 				toh->status = 0;
4841 				dstopt += sizeof (struct T_opthdr);
4842 				/* Align for gethrestime() */
4843 				dstopt = (char *)P2ROUNDUP((intptr_t)dstopt,
4844 				    sizeof (intptr_t));
4845 				gethrestime((timestruc_t *)dstopt);
4846 				dstopt = (char *)toh + toh->len;
4847 				udi_size -= toh->len;
4848 			}
4849 
4850 			/*
4851 			 * CAUTION:
4852 			 * Due to aligment issues
4853 			 * Processing of IP_RECVTTL option
4854 			 * should always be the last. Adding
4855 			 * any option processing after this will
4856 			 * cause alignment panic.
4857 			 */
4858 			if (udp->udp_recvttl) {
4859 				struct	T_opthdr *toh;
4860 				uint8_t	*dstptr;
4861 
4862 				toh = (struct T_opthdr *)dstopt;
4863 				toh->level = IPPROTO_IP;
4864 				toh->name = IP_RECVTTL;
4865 				toh->len = sizeof (struct T_opthdr) +
4866 				    sizeof (uint8_t);
4867 				toh->status = 0;
4868 				dstopt += sizeof (struct T_opthdr);
4869 				dstptr = (uint8_t *)dstopt;
4870 				*dstptr = ((ipha_t *)rptr)->ipha_ttl;
4871 				dstopt = (char *)toh + toh->len;
4872 				udi_size -= toh->len;
4873 			}
4874 
4875 			/* Consumed all of allocated space */
4876 			ASSERT(udi_size == 0);
4877 		}
4878 	} else {
4879 		sin6_t *sin6;
4880 
4881 		/*
4882 		 * Handle both IPv4 and IPv6 packets for IPv6 sockets.
4883 		 *
4884 		 * Normally we only send up the address. If receiving of any
4885 		 * optional receive side information is enabled, we also send
4886 		 * that up as options.
4887 		 * [ Only udp_rput_other() handles packets that contain IP
4888 		 * options so code to account for does not appear immediately
4889 		 * below but elsewhere ]
4890 		 */
4891 		udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
4892 
4893 		if (ipp.ipp_fields & (IPPF_HOPOPTS|IPPF_DSTOPTS|IPPF_RTDSTOPTS|
4894 		    IPPF_RTHDR|IPPF_IFINDEX)) {
4895 			if (udp->udp_ipv6_recvhopopts &&
4896 			    (ipp.ipp_fields & IPPF_HOPOPTS)) {
4897 				size_t hlen;
4898 
4899 				UDP_STAT(udp_in_recvhopopts);
4900 				hlen = copy_hop_opts(&ipp, NULL);
4901 				if (hlen == 0)
4902 					ipp.ipp_fields &= ~IPPF_HOPOPTS;
4903 				udi_size += hlen;
4904 			}
4905 			if ((udp->udp_ipv6_recvdstopts ||
4906 				udp->udp_old_ipv6_recvdstopts) &&
4907 			    (ipp.ipp_fields & IPPF_DSTOPTS)) {
4908 				udi_size += sizeof (struct T_opthdr) +
4909 				    ipp.ipp_dstoptslen;
4910 				UDP_STAT(udp_in_recvdstopts);
4911 			}
4912 			if (((udp->udp_ipv6_recvdstopts &&
4913 			    udp->udp_ipv6_recvrthdr &&
4914 			    (ipp.ipp_fields & IPPF_RTHDR)) ||
4915 			    udp->udp_ipv6_recvrthdrdstopts) &&
4916 			    (ipp.ipp_fields & IPPF_RTDSTOPTS)) {
4917 				udi_size += sizeof (struct T_opthdr) +
4918 				    ipp.ipp_rtdstoptslen;
4919 				UDP_STAT(udp_in_recvrtdstopts);
4920 			}
4921 			if (udp->udp_ipv6_recvrthdr &&
4922 			    (ipp.ipp_fields & IPPF_RTHDR)) {
4923 				udi_size += sizeof (struct T_opthdr) +
4924 				    ipp.ipp_rthdrlen;
4925 				UDP_STAT(udp_in_recvrthdr);
4926 			}
4927 			if (udp->udp_ipv6_recvpktinfo &&
4928 			    (ipp.ipp_fields & IPPF_IFINDEX)) {
4929 				udi_size += sizeof (struct T_opthdr) +
4930 				    sizeof (struct in6_pktinfo);
4931 				UDP_STAT(udp_in_recvpktinfo);
4932 			}
4933 
4934 		}
4935 		if (udp->udp_recvucred && (cr = DB_CRED(mp)) != NULL) {
4936 			udi_size += sizeof (struct T_opthdr) + ucredsize;
4937 			cpid = DB_CPID(mp);
4938 			UDP_STAT(udp_in_recvucred);
4939 		}
4940 
4941 		if (udp->udp_ipv6_recvhoplimit) {
4942 			udi_size += sizeof (struct T_opthdr) + sizeof (int);
4943 			UDP_STAT(udp_in_recvhoplimit);
4944 		}
4945 
4946 		if (udp->udp_ipv6_recvtclass) {
4947 			udi_size += sizeof (struct T_opthdr) + sizeof (int);
4948 			UDP_STAT(udp_in_recvtclass);
4949 		}
4950 
4951 		mp1 = allocb(udi_size, BPRI_MED);
4952 		if (mp1 == NULL) {
4953 			freemsg(mp);
4954 			if (options_mp != NULL)
4955 				freeb(options_mp);
4956 			TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END,
4957 				"udp_rput_end: q %p (%S)", q, "allocbfail");
4958 			BUMP_MIB(&udp_mib, udpInErrors);
4959 			return;
4960 		}
4961 		mp1->b_cont = mp;
4962 		mp = mp1;
4963 		mp->b_datap->db_type = M_PROTO;
4964 		tudi = (struct T_unitdata_ind *)mp->b_rptr;
4965 		mp->b_wptr = (uchar_t *)tudi + udi_size;
4966 		tudi->PRIM_type = T_UNITDATA_IND;
4967 		tudi->SRC_length = sizeof (sin6_t);
4968 		tudi->SRC_offset = sizeof (struct T_unitdata_ind);
4969 		tudi->OPT_offset = sizeof (struct T_unitdata_ind) +
4970 		    sizeof (sin6_t);
4971 		udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t));
4972 		tudi->OPT_length = udi_size;
4973 		sin6 = (sin6_t *)&tudi[1];
4974 		if (ipversion == IPV4_VERSION) {
4975 			in6_addr_t v6dst;
4976 
4977 			IN6_IPADDR_TO_V4MAPPED(((ipha_t *)rptr)->ipha_src,
4978 			    &sin6->sin6_addr);
4979 			IN6_IPADDR_TO_V4MAPPED(((ipha_t *)rptr)->ipha_dst,
4980 			    &v6dst);
4981 			sin6->sin6_flowinfo = 0;
4982 			sin6->sin6_scope_id = 0;
4983 			sin6->__sin6_src_id = ip_srcid_find_addr(&v6dst,
4984 			    connp->conn_zoneid);
4985 		} else {
4986 			sin6->sin6_addr = ip6h->ip6_src;
4987 			/* No sin6_flowinfo per API */
4988 			sin6->sin6_flowinfo = 0;
4989 			/* For link-scope source pass up scope id */
4990 			if ((ipp.ipp_fields & IPPF_IFINDEX) &&
4991 			    IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src))
4992 				sin6->sin6_scope_id = ipp.ipp_ifindex;
4993 			else
4994 				sin6->sin6_scope_id = 0;
4995 			sin6->__sin6_src_id = ip_srcid_find_addr(
4996 			    &ip6h->ip6_dst, connp->conn_zoneid);
4997 		}
4998 		sin6->sin6_port = udpha->uha_src_port;
4999 		sin6->sin6_family = udp->udp_family;
5000 
5001 		if (udi_size != 0) {
5002 			uchar_t *dstopt;
5003 
5004 			dstopt = (uchar_t *)&sin6[1];
5005 			if (udp->udp_ipv6_recvpktinfo &&
5006 			    (ipp.ipp_fields & IPPF_IFINDEX)) {
5007 				struct T_opthdr *toh;
5008 				struct in6_pktinfo *pkti;
5009 
5010 				toh = (struct T_opthdr *)dstopt;
5011 				toh->level = IPPROTO_IPV6;
5012 				toh->name = IPV6_PKTINFO;
5013 				toh->len = sizeof (struct T_opthdr) +
5014 				    sizeof (*pkti);
5015 				toh->status = 0;
5016 				dstopt += sizeof (struct T_opthdr);
5017 				pkti = (struct in6_pktinfo *)dstopt;
5018 				if (ipversion == IPV6_VERSION)
5019 					pkti->ipi6_addr = ip6h->ip6_dst;
5020 				else
5021 					IN6_IPADDR_TO_V4MAPPED(
5022 						((ipha_t *)rptr)->ipha_dst,
5023 						    &pkti->ipi6_addr);
5024 				pkti->ipi6_ifindex = ipp.ipp_ifindex;
5025 				dstopt += sizeof (*pkti);
5026 				udi_size -= toh->len;
5027 			}
5028 			if (udp->udp_ipv6_recvhoplimit) {
5029 				struct T_opthdr *toh;
5030 
5031 				toh = (struct T_opthdr *)dstopt;
5032 				toh->level = IPPROTO_IPV6;
5033 				toh->name = IPV6_HOPLIMIT;
5034 				toh->len = sizeof (struct T_opthdr) +
5035 				    sizeof (uint_t);
5036 				toh->status = 0;
5037 				dstopt += sizeof (struct T_opthdr);
5038 				if (ipversion == IPV6_VERSION)
5039 					*(uint_t *)dstopt = ip6h->ip6_hops;
5040 				else
5041 					*(uint_t *)dstopt =
5042 					    ((ipha_t *)rptr)->ipha_ttl;
5043 				dstopt += sizeof (uint_t);
5044 				udi_size -= toh->len;
5045 			}
5046 			if (udp->udp_ipv6_recvtclass) {
5047 				struct T_opthdr *toh;
5048 
5049 				toh = (struct T_opthdr *)dstopt;
5050 				toh->level = IPPROTO_IPV6;
5051 				toh->name = IPV6_TCLASS;
5052 				toh->len = sizeof (struct T_opthdr) +
5053 				    sizeof (uint_t);
5054 				toh->status = 0;
5055 				dstopt += sizeof (struct T_opthdr);
5056 				if (ipversion == IPV6_VERSION) {
5057 					*(uint_t *)dstopt =
5058 					IPV6_FLOW_TCLASS(ip6h->ip6_flow);
5059 				} else {
5060 					ipha_t *ipha = (ipha_t *)rptr;
5061 					*(uint_t *)dstopt =
5062 					    ipha->ipha_type_of_service;
5063 				}
5064 				dstopt += sizeof (uint_t);
5065 				udi_size -= toh->len;
5066 			}
5067 			if (udp->udp_ipv6_recvhopopts &&
5068 			    (ipp.ipp_fields & IPPF_HOPOPTS)) {
5069 				size_t hlen;
5070 
5071 				hlen = copy_hop_opts(&ipp, dstopt);
5072 				dstopt += hlen;
5073 				udi_size -= hlen;
5074 			}
5075 			if (udp->udp_ipv6_recvdstopts &&
5076 			    udp->udp_ipv6_recvrthdr &&
5077 			    (ipp.ipp_fields & IPPF_RTHDR) &&
5078 			    (ipp.ipp_fields & IPPF_RTDSTOPTS)) {
5079 				struct T_opthdr *toh;
5080 
5081 				toh = (struct T_opthdr *)dstopt;
5082 				toh->level = IPPROTO_IPV6;
5083 				toh->name = IPV6_DSTOPTS;
5084 				toh->len = sizeof (struct T_opthdr) +
5085 				    ipp.ipp_rtdstoptslen;
5086 				toh->status = 0;
5087 				dstopt += sizeof (struct T_opthdr);
5088 				bcopy(ipp.ipp_rtdstopts, dstopt,
5089 				    ipp.ipp_rtdstoptslen);
5090 				dstopt += ipp.ipp_rtdstoptslen;
5091 				udi_size -= toh->len;
5092 			}
5093 			if (udp->udp_ipv6_recvrthdr &&
5094 			    (ipp.ipp_fields & IPPF_RTHDR)) {
5095 				struct T_opthdr *toh;
5096 
5097 				toh = (struct T_opthdr *)dstopt;
5098 				toh->level = IPPROTO_IPV6;
5099 				toh->name = IPV6_RTHDR;
5100 				toh->len = sizeof (struct T_opthdr) +
5101 				    ipp.ipp_rthdrlen;
5102 				toh->status = 0;
5103 				dstopt += sizeof (struct T_opthdr);
5104 				bcopy(ipp.ipp_rthdr, dstopt, ipp.ipp_rthdrlen);
5105 				dstopt += ipp.ipp_rthdrlen;
5106 				udi_size -= toh->len;
5107 			}
5108 			if (udp->udp_ipv6_recvdstopts &&
5109 			    (ipp.ipp_fields & IPPF_DSTOPTS)) {
5110 				struct T_opthdr *toh;
5111 
5112 				toh = (struct T_opthdr *)dstopt;
5113 				toh->level = IPPROTO_IPV6;
5114 				toh->name = IPV6_DSTOPTS;
5115 				toh->len = sizeof (struct T_opthdr) +
5116 				    ipp.ipp_dstoptslen;
5117 				toh->status = 0;
5118 				dstopt += sizeof (struct T_opthdr);
5119 				bcopy(ipp.ipp_dstopts, dstopt,
5120 				    ipp.ipp_dstoptslen);
5121 				dstopt += ipp.ipp_dstoptslen;
5122 				udi_size -= toh->len;
5123 			}
5124 
5125 			if (cr != NULL) {
5126 				struct T_opthdr *toh;
5127 
5128 				toh = (struct T_opthdr *)dstopt;
5129 				toh->level = SOL_SOCKET;
5130 				toh->name = SCM_UCRED;
5131 				toh->len = sizeof (struct T_opthdr) + ucredsize;
5132 				toh->status = 0;
5133 				(void) cred2ucred(cr, cpid, &toh[1], rcr);
5134 				dstopt += toh->len;
5135 				udi_size -= toh->len;
5136 			}
5137 			/* Consumed all of allocated space */
5138 			ASSERT(udi_size == 0);
5139 		}
5140 #undef	sin6
5141 		/* No IP_RECVDSTADDR for IPv6. */
5142 	}
5143 
5144 	BUMP_MIB(&udp_mib, udpInDatagrams);
5145 	TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END,
5146 		"udp_rput_end: q %p (%S)", q, "end");
5147 	if (options_mp != NULL)
5148 		freeb(options_mp);
5149 
5150 	if (udp->udp_direct_sockfs) {
5151 		/*
5152 		 * There is nothing above us except for the stream head;
5153 		 * use the read-side synchronous stream interface in
5154 		 * order to reduce the time spent in interrupt thread.
5155 		 */
5156 		ASSERT(udp->udp_issocket);
5157 		udp_rcv_enqueue(UDP_RD(q), udp, mp, mp_len);
5158 	} else {
5159 		/*
5160 		 * Use regular STREAMS interface to pass data upstream
5161 		 * if this is not a socket endpoint, or if we have
5162 		 * switched over to the slow mode due to sockmod being
5163 		 * popped or a module being pushed on top of us.
5164 		 */
5165 		putnext(UDP_RD(q), mp);
5166 	}
5167 	return;
5168 
5169 tossit:
5170 	freemsg(mp);
5171 	if (options_mp != NULL)
5172 		freeb(options_mp);
5173 	BUMP_MIB(&udp_mib, udpInErrors);
5174 }
5175 
5176 void
5177 udp_conn_recv(conn_t *connp, mblk_t *mp)
5178 {
5179 	_UDP_ENTER(connp, mp, udp_input_wrapper, SQTAG_UDP_FANOUT);
5180 }
5181 
5182 /* ARGSUSED */
5183 static void
5184 udp_input_wrapper(void *arg, mblk_t *mp, void *arg2)
5185 {
5186 	udp_input((conn_t *)arg, mp);
5187 	_UDP_EXIT((conn_t *)arg);
5188 }
5189 
5190 /*
5191  * Process non-M_DATA messages as well as M_DATA messages that requires
5192  * modifications to udp_ip_rcv_options i.e. IPv4 packets with IP options.
5193  */
5194 static void
5195 udp_rput_other(queue_t *q, mblk_t *mp)
5196 {
5197 	struct T_unitdata_ind	*tudi;
5198 	mblk_t			*mp1;
5199 	uchar_t			*rptr;
5200 	uchar_t			*new_rptr;
5201 	int			hdr_length;
5202 	int			udi_size;	/* Size of T_unitdata_ind */
5203 	int			opt_len;	/* Length of IP options */
5204 	sin_t			*sin;
5205 	struct T_error_ack	*tea;
5206 	mblk_t			*options_mp = NULL;
5207 	in_pktinfo_t		*pinfo;
5208 	boolean_t		recv_on = B_FALSE;
5209 	cred_t			*cr = NULL;
5210 	udp_t			*udp = Q_TO_UDP(q);
5211 	pid_t			cpid;
5212 	cred_t			*rcr = udp->udp_connp->conn_cred;
5213 
5214 	TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_START,
5215 	    "udp_rput_other: q %p mp %p", q, mp);
5216 
5217 	ASSERT(OK_32PTR(mp->b_rptr));
5218 	rptr = mp->b_rptr;
5219 
5220 	switch (mp->b_datap->db_type) {
5221 	case M_CTL:
5222 		/*
5223 		 * We are here only if IP_RECVSLLA and/or IP_RECVIF are set
5224 		 */
5225 		recv_on = B_TRUE;
5226 		options_mp = mp;
5227 		pinfo = (in_pktinfo_t *)options_mp->b_rptr;
5228 
5229 		/*
5230 		 * The actual data is in mp->b_cont
5231 		 */
5232 		mp = mp->b_cont;
5233 		ASSERT(OK_32PTR(mp->b_rptr));
5234 		rptr = mp->b_rptr;
5235 		break;
5236 	case M_DATA:
5237 		/*
5238 		 * M_DATA messages contain IPv4 datagrams.  They are handled
5239 		 * after this switch.
5240 		 */
5241 		break;
5242 	case M_PROTO:
5243 	case M_PCPROTO:
5244 		/* M_PROTO messages contain some type of TPI message. */
5245 		ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
5246 		if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
5247 			freemsg(mp);
5248 			TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END,
5249 			    "udp_rput_other_end: q %p (%S)", q, "protoshort");
5250 			return;
5251 		}
5252 		tea = (struct T_error_ack *)rptr;
5253 
5254 		switch (tea->PRIM_type) {
5255 		case T_ERROR_ACK:
5256 			switch (tea->ERROR_prim) {
5257 			case O_T_BIND_REQ:
5258 			case T_BIND_REQ: {
5259 				/*
5260 				 * If our O_T_BIND_REQ/T_BIND_REQ fails,
5261 				 * clear out the associated port and source
5262 				 * address before passing the message
5263 				 * upstream. If this was caused by a T_CONN_REQ
5264 				 * revert back to bound state.
5265 				 */
5266 				udp_fanout_t	*udpf;
5267 
5268 				udpf = &udp_bind_fanout[
5269 				    UDP_BIND_HASH(udp->udp_port)];
5270 				mutex_enter(&udpf->uf_lock);
5271 				if (udp->udp_state == TS_DATA_XFER) {
5272 					/* Connect failed */
5273 					tea->ERROR_prim = T_CONN_REQ;
5274 					/* Revert back to the bound source */
5275 					udp->udp_v6src = udp->udp_bound_v6src;
5276 					udp->udp_state = TS_IDLE;
5277 					mutex_exit(&udpf->uf_lock);
5278 					if (udp->udp_family == AF_INET6)
5279 						(void) udp_build_hdrs(q, udp);
5280 					break;
5281 				}
5282 
5283 				if (udp->udp_discon_pending) {
5284 					tea->ERROR_prim = T_DISCON_REQ;
5285 					udp->udp_discon_pending = 0;
5286 				}
5287 				V6_SET_ZERO(udp->udp_v6src);
5288 				V6_SET_ZERO(udp->udp_bound_v6src);
5289 				udp->udp_state = TS_UNBND;
5290 				udp_bind_hash_remove(udp, B_TRUE);
5291 				udp->udp_port = 0;
5292 				mutex_exit(&udpf->uf_lock);
5293 				if (udp->udp_family == AF_INET6)
5294 					(void) udp_build_hdrs(q, udp);
5295 				break;
5296 			}
5297 			default:
5298 				break;
5299 			}
5300 			break;
5301 		case T_BIND_ACK:
5302 			udp_rput_bind_ack(q, mp);
5303 			return;
5304 
5305 		case T_OPTMGMT_ACK:
5306 		case T_OK_ACK:
5307 			break;
5308 		default:
5309 			freemsg(mp);
5310 			return;
5311 		}
5312 		putnext(UDP_RD(q), mp);
5313 		return;
5314 	}
5315 
5316 	/*
5317 	 * This is the inbound data path.
5318 	 * First, we make sure the data contains both IP and UDP headers.
5319 	 *
5320 	 * This handle IPv4 packets for only AF_INET sockets.
5321 	 * AF_INET6 sockets can never access udp_ip_rcv_options thus there
5322 	 * is no need saving the options.
5323 	 */
5324 	ASSERT(IPH_HDR_VERSION((ipha_t *)rptr) == IPV4_VERSION);
5325 	hdr_length = IPH_HDR_LENGTH(rptr) + UDPH_SIZE;
5326 	if (mp->b_wptr - rptr < hdr_length) {
5327 		if (!pullupmsg(mp, hdr_length)) {
5328 			freemsg(mp);
5329 			if (options_mp != NULL)
5330 				freeb(options_mp);
5331 			BUMP_MIB(&udp_mib, udpInErrors);
5332 			TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END,
5333 			    "udp_rput_other_end: q %p (%S)", q, "hdrshort");
5334 			BUMP_MIB(&udp_mib, udpInErrors);
5335 			return;
5336 		}
5337 		rptr = mp->b_rptr;
5338 	}
5339 	/* Walk past the headers. */
5340 	new_rptr = rptr + hdr_length;
5341 	if (!udp->udp_rcvhdr)
5342 		mp->b_rptr = new_rptr;
5343 
5344 	/* Save the options if any */
5345 	opt_len = hdr_length - (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE);
5346 	if (opt_len > 0) {
5347 		if (opt_len > udp->udp_ip_rcv_options_len) {
5348 			if (udp->udp_ip_rcv_options_len)
5349 				mi_free((char *)udp->udp_ip_rcv_options);
5350 			udp->udp_ip_rcv_options_len = 0;
5351 			udp->udp_ip_rcv_options =
5352 			    (uchar_t *)mi_alloc(opt_len, BPRI_HI);
5353 			if (udp->udp_ip_rcv_options)
5354 				udp->udp_ip_rcv_options_len = opt_len;
5355 		}
5356 		if (udp->udp_ip_rcv_options_len) {
5357 			bcopy(rptr + IP_SIMPLE_HDR_LENGTH,
5358 			    udp->udp_ip_rcv_options, opt_len);
5359 			/* Adjust length if we are resusing the space */
5360 			udp->udp_ip_rcv_options_len = opt_len;
5361 		}
5362 	} else if (udp->udp_ip_rcv_options_len) {
5363 		mi_free((char *)udp->udp_ip_rcv_options);
5364 		udp->udp_ip_rcv_options = NULL;
5365 		udp->udp_ip_rcv_options_len = 0;
5366 	}
5367 
5368 	/*
5369 	 * Normally only send up the address.
5370 	 * If IP_RECVDSTADDR is set we include the destination IP
5371 	 * address as an option. With IP_RECVOPTS we include all
5372 	 * the IP options.
5373 	 */
5374 	udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin_t);
5375 	if (udp->udp_recvdstaddr) {
5376 		udi_size += sizeof (struct T_opthdr) + sizeof (struct in_addr);
5377 		UDP_STAT(udp_in_recvdstaddr);
5378 	}
5379 	if (udp->udp_recvopts && opt_len > 0) {
5380 		udi_size += sizeof (struct T_opthdr) + opt_len;
5381 		UDP_STAT(udp_in_recvopts);
5382 	}
5383 
5384 	/*
5385 	 * If the IP_RECVSLLA or the IP_RECVIF is set then allocate
5386 	 * space accordingly
5387 	 */
5388 	if (udp->udp_recvif && recv_on &&
5389 	    (pinfo->in_pkt_flags & IPF_RECVIF)) {
5390 		udi_size += sizeof (struct T_opthdr) + sizeof (uint_t);
5391 		UDP_STAT(udp_in_recvif);
5392 	}
5393 
5394 	if (udp->udp_recvslla && recv_on &&
5395 	    (pinfo->in_pkt_flags & IPF_RECVSLLA)) {
5396 		udi_size += sizeof (struct T_opthdr) +
5397 		    sizeof (struct sockaddr_dl);
5398 		UDP_STAT(udp_in_recvslla);
5399 	}
5400 
5401 	if (udp->udp_recvucred && (cr = DB_CRED(mp)) != NULL) {
5402 		udi_size += sizeof (struct T_opthdr) + ucredsize;
5403 		cpid = DB_CPID(mp);
5404 		UDP_STAT(udp_in_recvucred);
5405 	}
5406 	/*
5407 	 * If IP_RECVTTL is set allocate the appropriate sized buffer
5408 	 */
5409 	if (udp->udp_recvttl) {
5410 		udi_size += sizeof (struct T_opthdr) + sizeof (uint8_t);
5411 		UDP_STAT(udp_in_recvttl);
5412 	}
5413 
5414 	/* Allocate a message block for the T_UNITDATA_IND structure. */
5415 	mp1 = allocb(udi_size, BPRI_MED);
5416 	if (mp1 == NULL) {
5417 		freemsg(mp);
5418 		if (options_mp != NULL)
5419 			freeb(options_mp);
5420 		TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END,
5421 			"udp_rput_other_end: q %p (%S)", q, "allocbfail");
5422 		BUMP_MIB(&udp_mib, udpInErrors);
5423 		return;
5424 	}
5425 	mp1->b_cont = mp;
5426 	mp = mp1;
5427 	mp->b_datap->db_type = M_PROTO;
5428 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
5429 	mp->b_wptr = (uchar_t *)tudi + udi_size;
5430 	tudi->PRIM_type = T_UNITDATA_IND;
5431 	tudi->SRC_length = sizeof (sin_t);
5432 	tudi->SRC_offset = sizeof (struct T_unitdata_ind);
5433 	tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin_t);
5434 	udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
5435 	tudi->OPT_length = udi_size;
5436 
5437 	sin = (sin_t *)&tudi[1];
5438 	sin->sin_addr.s_addr = ((ipha_t *)rptr)->ipha_src;
5439 	sin->sin_port =	((in_port_t *)
5440 	    new_rptr)[-(UDPH_SIZE/sizeof (in_port_t))];
5441 	sin->sin_family = AF_INET;
5442 	*(uint32_t *)&sin->sin_zero[0] = 0;
5443 	*(uint32_t *)&sin->sin_zero[4] = 0;
5444 
5445 	/*
5446 	 * Add options if IP_RECVDSTADDR, IP_RECVIF, IP_RECVSLLA or
5447 	 * IP_RECVTTL has been set.
5448 	 */
5449 	if (udi_size != 0) {
5450 		/*
5451 		 * Copy in destination address before options to avoid any
5452 		 * padding issues.
5453 		 */
5454 		char *dstopt;
5455 
5456 		dstopt = (char *)&sin[1];
5457 		if (udp->udp_recvdstaddr) {
5458 			struct T_opthdr *toh;
5459 			ipaddr_t *dstptr;
5460 
5461 			toh = (struct T_opthdr *)dstopt;
5462 			toh->level = IPPROTO_IP;
5463 			toh->name = IP_RECVDSTADDR;
5464 			toh->len = sizeof (struct T_opthdr) + sizeof (ipaddr_t);
5465 			toh->status = 0;
5466 			dstopt += sizeof (struct T_opthdr);
5467 			dstptr = (ipaddr_t *)dstopt;
5468 			*dstptr = (((ipaddr_t *)rptr)[4]);
5469 			dstopt += sizeof (ipaddr_t);
5470 			udi_size -= toh->len;
5471 		}
5472 		if (udp->udp_recvopts && udi_size != 0) {
5473 			struct T_opthdr *toh;
5474 
5475 			toh = (struct T_opthdr *)dstopt;
5476 			toh->level = IPPROTO_IP;
5477 			toh->name = IP_RECVOPTS;
5478 			toh->len = sizeof (struct T_opthdr) + opt_len;
5479 			toh->status = 0;
5480 			dstopt += sizeof (struct T_opthdr);
5481 			bcopy(rptr + IP_SIMPLE_HDR_LENGTH, dstopt, opt_len);
5482 			dstopt += opt_len;
5483 			udi_size -= toh->len;
5484 		}
5485 
5486 		if (udp->udp_recvslla && recv_on &&
5487 		    (pinfo->in_pkt_flags & IPF_RECVSLLA)) {
5488 
5489 			struct T_opthdr *toh;
5490 			struct sockaddr_dl	*dstptr;
5491 
5492 			toh = (struct T_opthdr *)dstopt;
5493 			toh->level = IPPROTO_IP;
5494 			toh->name = IP_RECVSLLA;
5495 			toh->len = sizeof (struct T_opthdr) +
5496 			    sizeof (struct sockaddr_dl);
5497 			toh->status = 0;
5498 			dstopt += sizeof (struct T_opthdr);
5499 			dstptr = (struct sockaddr_dl *)dstopt;
5500 			bcopy(&pinfo->in_pkt_slla, dstptr,
5501 			    sizeof (struct sockaddr_dl));
5502 			dstopt += sizeof (struct sockaddr_dl);
5503 			udi_size -= toh->len;
5504 		}
5505 
5506 		if (udp->udp_recvif && recv_on &&
5507 		    (pinfo->in_pkt_flags & IPF_RECVIF)) {
5508 
5509 			struct T_opthdr *toh;
5510 			uint_t		*dstptr;
5511 
5512 			toh = (struct T_opthdr *)dstopt;
5513 			toh->level = IPPROTO_IP;
5514 			toh->name = IP_RECVIF;
5515 			toh->len = sizeof (struct T_opthdr) +
5516 			    sizeof (uint_t);
5517 			toh->status = 0;
5518 			dstopt += sizeof (struct T_opthdr);
5519 			dstptr = (uint_t *)dstopt;
5520 			*dstptr = pinfo->in_pkt_ifindex;
5521 			dstopt += sizeof (uint_t);
5522 			udi_size -= toh->len;
5523 		}
5524 
5525 		if (cr != NULL) {
5526 			struct T_opthdr *toh;
5527 
5528 			toh = (struct T_opthdr *)dstopt;
5529 			toh->level = SOL_SOCKET;
5530 			toh->name = SCM_UCRED;
5531 			toh->len = sizeof (struct T_opthdr) + ucredsize;
5532 			toh->status = 0;
5533 			(void) cred2ucred(cr, cpid, &toh[1], rcr);
5534 			dstopt += toh->len;
5535 			udi_size -= toh->len;
5536 		}
5537 
5538 		if (udp->udp_recvttl) {
5539 			struct	T_opthdr *toh;
5540 			uint8_t	*dstptr;
5541 
5542 			toh = (struct T_opthdr *)dstopt;
5543 			toh->level = IPPROTO_IP;
5544 			toh->name = IP_RECVTTL;
5545 			toh->len = sizeof (struct T_opthdr) +
5546 			    sizeof (uint8_t);
5547 			toh->status = 0;
5548 			dstopt += sizeof (struct T_opthdr);
5549 			dstptr = (uint8_t *)dstopt;
5550 			*dstptr = ((ipha_t *)rptr)->ipha_ttl;
5551 			dstopt += sizeof (uint8_t);
5552 			udi_size -= toh->len;
5553 		}
5554 
5555 		ASSERT(udi_size == 0);	/* "Consumed" all of allocated space */
5556 	}
5557 	BUMP_MIB(&udp_mib, udpInDatagrams);
5558 	TRACE_2(TR_FAC_UDP, TR_UDP_RPUT_END,
5559 	    "udp_rput_other_end: q %p (%S)", q, "end");
5560 	if (options_mp != NULL)
5561 		freeb(options_mp);
5562 
5563 	if (udp->udp_direct_sockfs) {
5564 		/*
5565 		 * There is nothing above us except for the stream head;
5566 		 * use the read-side synchronous stream interface in
5567 		 * order to reduce the time spent in interrupt thread.
5568 		 */
5569 		ASSERT(udp->udp_issocket);
5570 		udp_rcv_enqueue(UDP_RD(q), udp, mp, msgdsize(mp));
5571 	} else {
5572 		/*
5573 		 * Use regular STREAMS interface to pass data upstream
5574 		 * if this is not a socket endpoint, or if we have
5575 		 * switched over to the slow mode due to sockmod being
5576 		 * popped or a module being pushed on top of us.
5577 		 */
5578 		putnext(UDP_RD(q), mp);
5579 	}
5580 }
5581 
5582 /* ARGSUSED */
5583 static void
5584 udp_rput_other_wrapper(void *arg, mblk_t *mp, void *arg2)
5585 {
5586 	conn_t *connp = arg;
5587 
5588 	udp_rput_other(connp->conn_rq, mp);
5589 	udp_exit(connp);
5590 }
5591 
5592 /*
5593  * Process a T_BIND_ACK
5594  */
5595 static void
5596 udp_rput_bind_ack(queue_t *q, mblk_t *mp)
5597 {
5598 	udp_t	*udp = Q_TO_UDP(q);
5599 	mblk_t	*mp1;
5600 	ire_t	*ire;
5601 	struct T_bind_ack *tba;
5602 	uchar_t *addrp;
5603 	ipa_conn_t	*ac;
5604 	ipa6_conn_t	*ac6;
5605 
5606 	if (udp->udp_discon_pending)
5607 		udp->udp_discon_pending = 0;
5608 
5609 	/*
5610 	 * If a broadcast/multicast address was bound set
5611 	 * the source address to 0.
5612 	 * This ensures no datagrams with broadcast address
5613 	 * as source address are emitted (which would violate
5614 	 * RFC1122 - Hosts requirements)
5615 	 *
5616 	 * Note that when connecting the returned IRE is
5617 	 * for the destination address and we only perform
5618 	 * the broadcast check for the source address (it
5619 	 * is OK to connect to a broadcast/multicast address.)
5620 	 */
5621 	mp1 = mp->b_cont;
5622 	if (mp1 != NULL && mp1->b_datap->db_type == IRE_DB_TYPE) {
5623 		ire = (ire_t *)mp1->b_rptr;
5624 
5625 		/*
5626 		 * Note: we get IRE_BROADCAST for IPv6 to "mark" a multicast
5627 		 * local address.
5628 		 */
5629 		if (ire->ire_type == IRE_BROADCAST &&
5630 		    udp->udp_state != TS_DATA_XFER) {
5631 			/* This was just a local bind to a broadcast addr */
5632 			V6_SET_ZERO(udp->udp_v6src);
5633 			if (udp->udp_family == AF_INET6)
5634 				(void) udp_build_hdrs(q, udp);
5635 		} else if (V6_OR_V4_INADDR_ANY(udp->udp_v6src)) {
5636 			/*
5637 			 * Local address not yet set - pick it from the
5638 			 * T_bind_ack
5639 			 */
5640 			tba = (struct T_bind_ack *)mp->b_rptr;
5641 			addrp = &mp->b_rptr[tba->ADDR_offset];
5642 			switch (udp->udp_family) {
5643 			case AF_INET:
5644 				if (tba->ADDR_length == sizeof (ipa_conn_t)) {
5645 					ac = (ipa_conn_t *)addrp;
5646 				} else {
5647 					ASSERT(tba->ADDR_length ==
5648 					    sizeof (ipa_conn_x_t));
5649 					ac = &((ipa_conn_x_t *)addrp)->acx_conn;
5650 				}
5651 				IN6_IPADDR_TO_V4MAPPED(ac->ac_laddr,
5652 				    &udp->udp_v6src);
5653 				break;
5654 			case AF_INET6:
5655 				if (tba->ADDR_length == sizeof (ipa6_conn_t)) {
5656 					ac6 = (ipa6_conn_t *)addrp;
5657 				} else {
5658 					ASSERT(tba->ADDR_length ==
5659 					    sizeof (ipa6_conn_x_t));
5660 					ac6 = &((ipa6_conn_x_t *)
5661 					    addrp)->ac6x_conn;
5662 				}
5663 				udp->udp_v6src = ac6->ac6_laddr;
5664 				(void) udp_build_hdrs(q, udp);
5665 				break;
5666 			}
5667 		}
5668 		mp1 = mp1->b_cont;
5669 	}
5670 	/*
5671 	 * Look for one or more appended ACK message added by
5672 	 * udp_connect or udp_disconnect.
5673 	 * If none found just send up the T_BIND_ACK.
5674 	 * udp_connect has appended a T_OK_ACK and a T_CONN_CON.
5675 	 * udp_disconnect has appended a T_OK_ACK.
5676 	 */
5677 	if (mp1 != NULL) {
5678 		if (mp->b_cont == mp1)
5679 			mp->b_cont = NULL;
5680 		else {
5681 			ASSERT(mp->b_cont->b_cont == mp1);
5682 			mp->b_cont->b_cont = NULL;
5683 		}
5684 		freemsg(mp);
5685 		mp = mp1;
5686 		while (mp != NULL) {
5687 			mp1 = mp->b_cont;
5688 			mp->b_cont = NULL;
5689 			putnext(UDP_RD(q), mp);
5690 			mp = mp1;
5691 		}
5692 		return;
5693 	}
5694 	freemsg(mp->b_cont);
5695 	mp->b_cont = NULL;
5696 	putnext(UDP_RD(q), mp);
5697 }
5698 
5699 /*
5700  * return SNMP stuff in buffer in mpdata
5701  */
5702 int
5703 udp_snmp_get(queue_t *q, mblk_t *mpctl)
5704 {
5705 	mblk_t			*mpdata;
5706 	mblk_t			*mp_conn_ctl;
5707 	mblk_t			*mp_attr_ctl;
5708 	mblk_t			*mp6_conn_ctl;
5709 	mblk_t			*mp6_attr_ctl;
5710 	mblk_t			*mp_conn_tail;
5711 	mblk_t			*mp_attr_tail;
5712 	mblk_t			*mp6_conn_tail;
5713 	mblk_t			*mp6_attr_tail;
5714 	struct opthdr		*optp;
5715 	mib2_udpEntry_t		ude;
5716 	mib2_udp6Entry_t	ude6;
5717 	mib2_transportMLPEntry_t mlp;
5718 	int			state;
5719 	zoneid_t		zoneid;
5720 	int			i;
5721 	connf_t			*connfp;
5722 	conn_t			*connp = Q_TO_CONN(q);
5723 	udp_t			*udp = connp->conn_udp;
5724 	int			v4_conn_idx;
5725 	int			v6_conn_idx;
5726 	boolean_t		needattr;
5727 
5728 	mp_conn_ctl = mp_attr_ctl = mp6_conn_ctl = NULL;
5729 	if (mpctl == NULL ||
5730 	    (mpdata = mpctl->b_cont) == NULL ||
5731 	    (mp_conn_ctl = copymsg(mpctl)) == NULL ||
5732 	    (mp_attr_ctl = copymsg(mpctl)) == NULL ||
5733 	    (mp6_conn_ctl = copymsg(mpctl)) == NULL ||
5734 	    (mp6_attr_ctl = copymsg(mpctl)) == NULL) {
5735 		freemsg(mp_conn_ctl);
5736 		freemsg(mp_attr_ctl);
5737 		freemsg(mp6_conn_ctl);
5738 		return (0);
5739 	}
5740 
5741 	zoneid = connp->conn_zoneid;
5742 
5743 	/* fixed length structure for IPv4 and IPv6 counters */
5744 	SET_MIB(udp_mib.udpEntrySize, sizeof (mib2_udpEntry_t));
5745 	SET_MIB(udp_mib.udp6EntrySize, sizeof (mib2_udp6Entry_t));
5746 	optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
5747 	optp->level = MIB2_UDP;
5748 	optp->name = 0;
5749 	(void) snmp_append_data(mpdata, (char *)&udp_mib, sizeof (udp_mib));
5750 	optp->len = msgdsize(mpdata);
5751 	qreply(q, mpctl);
5752 
5753 	mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL;
5754 	v4_conn_idx = v6_conn_idx = 0;
5755 
5756 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
5757 		connfp = &ipcl_globalhash_fanout[i];
5758 		connp = NULL;
5759 
5760 		while ((connp = ipcl_get_next_conn(connfp, connp,
5761 		    IPCL_UDP))) {
5762 			udp = connp->conn_udp;
5763 			if (zoneid != connp->conn_zoneid)
5764 				continue;
5765 
5766 			/*
5767 			 * Note that the port numbers are sent in
5768 			 * host byte order
5769 			 */
5770 
5771 			if (udp->udp_state == TS_UNBND)
5772 				state = MIB2_UDP_unbound;
5773 			else if (udp->udp_state == TS_IDLE)
5774 				state = MIB2_UDP_idle;
5775 			else if (udp->udp_state == TS_DATA_XFER)
5776 				state = MIB2_UDP_connected;
5777 			else
5778 				state = MIB2_UDP_unknown;
5779 
5780 			needattr = B_FALSE;
5781 			bzero(&mlp, sizeof (mlp));
5782 			if (connp->conn_mlp_type != mlptSingle) {
5783 				if (connp->conn_mlp_type == mlptShared ||
5784 				    connp->conn_mlp_type == mlptBoth)
5785 					mlp.tme_flags |= MIB2_TMEF_SHARED;
5786 				if (connp->conn_mlp_type == mlptPrivate ||
5787 				    connp->conn_mlp_type == mlptBoth)
5788 					mlp.tme_flags |= MIB2_TMEF_PRIVATE;
5789 				needattr = B_TRUE;
5790 			}
5791 
5792 			/*
5793 			 * Create an IPv4 table entry for IPv4 entries and also
5794 			 * any IPv6 entries which are bound to in6addr_any
5795 			 * (i.e. anything a IPv4 peer could connect/send to).
5796 			 */
5797 			if (udp->udp_ipversion == IPV4_VERSION ||
5798 			    (udp->udp_state <= TS_IDLE &&
5799 			    IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src))) {
5800 				ude.udpEntryInfo.ue_state = state;
5801 				/*
5802 				 * If in6addr_any this will set it to
5803 				 * INADDR_ANY
5804 				 */
5805 				ude.udpLocalAddress =
5806 				    V4_PART_OF_V6(udp->udp_v6src);
5807 				ude.udpLocalPort = ntohs(udp->udp_port);
5808 				if (udp->udp_state == TS_DATA_XFER) {
5809 					/*
5810 					 * Can potentially get here for
5811 					 * v6 socket if another process
5812 					 * (say, ping) has just done a
5813 					 * sendto(), changing the state
5814 					 * from the TS_IDLE above to
5815 					 * TS_DATA_XFER by the time we hit
5816 					 * this part of the code.
5817 					 */
5818 					ude.udpEntryInfo.ue_RemoteAddress =
5819 					    V4_PART_OF_V6(udp->udp_v6dst);
5820 					ude.udpEntryInfo.ue_RemotePort =
5821 					    ntohs(udp->udp_dstport);
5822 				} else {
5823 					ude.udpEntryInfo.ue_RemoteAddress = 0;
5824 					ude.udpEntryInfo.ue_RemotePort = 0;
5825 				}
5826 				(void) snmp_append_data2(mp_conn_ctl->b_cont,
5827 				    &mp_conn_tail, (char *)&ude, sizeof (ude));
5828 				mlp.tme_connidx = v4_conn_idx++;
5829 				if (needattr)
5830 					(void) snmp_append_data2(
5831 					    mp_attr_ctl->b_cont, &mp_attr_tail,
5832 					    (char *)&mlp, sizeof (mlp));
5833 			}
5834 			if (udp->udp_ipversion == IPV6_VERSION) {
5835 				ude6.udp6EntryInfo.ue_state  = state;
5836 				ude6.udp6LocalAddress = udp->udp_v6src;
5837 				ude6.udp6LocalPort = ntohs(udp->udp_port);
5838 				ude6.udp6IfIndex = udp->udp_bound_if;
5839 				if (udp->udp_state == TS_DATA_XFER) {
5840 					ude6.udp6EntryInfo.ue_RemoteAddress =
5841 					    udp->udp_v6dst;
5842 					ude6.udp6EntryInfo.ue_RemotePort =
5843 					    ntohs(udp->udp_dstport);
5844 				} else {
5845 					ude6.udp6EntryInfo.ue_RemoteAddress =
5846 					    sin6_null.sin6_addr;
5847 					ude6.udp6EntryInfo.ue_RemotePort = 0;
5848 				}
5849 				(void) snmp_append_data2(mp6_conn_ctl->b_cont,
5850 				    &mp6_conn_tail, (char *)&ude6,
5851 				    sizeof (ude6));
5852 				mlp.tme_connidx = v6_conn_idx++;
5853 				if (needattr)
5854 					(void) snmp_append_data2(
5855 					    mp6_attr_ctl->b_cont,
5856 					    &mp6_attr_tail, (char *)&mlp,
5857 					    sizeof (mlp));
5858 			}
5859 		}
5860 	}
5861 
5862 	/* IPv4 UDP endpoints */
5863 	optp = (struct opthdr *)&mp_conn_ctl->b_rptr[
5864 	    sizeof (struct T_optmgmt_ack)];
5865 	optp->level = MIB2_UDP;
5866 	optp->name = MIB2_UDP_ENTRY;
5867 	optp->len = msgdsize(mp_conn_ctl->b_cont);
5868 	qreply(q, mp_conn_ctl);
5869 
5870 	/* table of MLP attributes... */
5871 	optp = (struct opthdr *)&mp_attr_ctl->b_rptr[
5872 	    sizeof (struct T_optmgmt_ack)];
5873 	optp->level = MIB2_UDP;
5874 	optp->name = EXPER_XPORT_MLP;
5875 	optp->len = msgdsize(mp_attr_ctl->b_cont);
5876 	if (optp->len == 0)
5877 		freemsg(mp_attr_ctl);
5878 	else
5879 		qreply(q, mp_attr_ctl);
5880 
5881 	/* IPv6 UDP endpoints */
5882 	optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[
5883 	    sizeof (struct T_optmgmt_ack)];
5884 	optp->level = MIB2_UDP6;
5885 	optp->name = MIB2_UDP6_ENTRY;
5886 	optp->len = msgdsize(mp6_conn_ctl->b_cont);
5887 	qreply(q, mp6_conn_ctl);
5888 
5889 	/* table of MLP attributes... */
5890 	optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[
5891 	    sizeof (struct T_optmgmt_ack)];
5892 	optp->level = MIB2_UDP6;
5893 	optp->name = EXPER_XPORT_MLP;
5894 	optp->len = msgdsize(mp6_attr_ctl->b_cont);
5895 	if (optp->len == 0)
5896 		freemsg(mp6_attr_ctl);
5897 	else
5898 		qreply(q, mp6_attr_ctl);
5899 
5900 	return (1);
5901 }
5902 
5903 /*
5904  * Return 0 if invalid set request, 1 otherwise, including non-udp requests.
5905  * NOTE: Per MIB-II, UDP has no writable data.
5906  * TODO:  If this ever actually tries to set anything, it needs to be
5907  * to do the appropriate locking.
5908  */
5909 /* ARGSUSED */
5910 int
5911 udp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
5912     uchar_t *ptr, int len)
5913 {
5914 	switch (level) {
5915 	case MIB2_UDP:
5916 		return (0);
5917 	default:
5918 		return (1);
5919 	}
5920 }
5921 
5922 static void
5923 udp_report_item(mblk_t *mp, udp_t *udp)
5924 {
5925 	char *state;
5926 	char addrbuf1[INET6_ADDRSTRLEN];
5927 	char addrbuf2[INET6_ADDRSTRLEN];
5928 	uint_t print_len, buf_len;
5929 
5930 	buf_len = mp->b_datap->db_lim - mp->b_wptr;
5931 	ASSERT(buf_len >= 0);
5932 	if (buf_len == 0)
5933 		return;
5934 
5935 	if (udp->udp_state == TS_UNBND)
5936 		state = "UNBOUND";
5937 	else if (udp->udp_state == TS_IDLE)
5938 		state = "IDLE";
5939 	else if (udp->udp_state == TS_DATA_XFER)
5940 		state = "CONNECTED";
5941 	else
5942 		state = "UnkState";
5943 	print_len = snprintf((char *)mp->b_wptr, buf_len,
5944 	    MI_COL_PTRFMT_STR "%4d %5u %s %s %5u %s\n",
5945 	    (void *)udp, udp->udp_connp->conn_zoneid, ntohs(udp->udp_port),
5946 	    inet_ntop(AF_INET6, &udp->udp_v6src,
5947 		addrbuf1, sizeof (addrbuf1)),
5948 	    inet_ntop(AF_INET6, &udp->udp_v6dst,
5949 		addrbuf2, sizeof (addrbuf2)),
5950 	    ntohs(udp->udp_dstport), state);
5951 	if (print_len < buf_len) {
5952 		mp->b_wptr += print_len;
5953 	} else {
5954 		mp->b_wptr += buf_len;
5955 	}
5956 }
5957 
5958 /* Report for ndd "udp_status" */
5959 /* ARGSUSED */
5960 static int
5961 udp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
5962 {
5963 	zoneid_t zoneid;
5964 	connf_t	*connfp;
5965 	conn_t	*connp = Q_TO_CONN(q);
5966 	udp_t	*udp = connp->conn_udp;
5967 	int	i;
5968 
5969 	/*
5970 	 * Because of the ndd constraint, at most we can have 64K buffer
5971 	 * to put in all UDP info.  So to be more efficient, just
5972 	 * allocate a 64K buffer here, assuming we need that large buffer.
5973 	 * This may be a problem as any user can read udp_status.  Therefore
5974 	 * we limit the rate of doing this using udp_ndd_get_info_interval.
5975 	 * This should be OK as normal users should not do this too often.
5976 	 */
5977 	if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) {
5978 		if (ddi_get_lbolt() - udp_last_ndd_get_info_time <
5979 		    drv_usectohz(udp_ndd_get_info_interval * 1000)) {
5980 			(void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG);
5981 			return (0);
5982 		}
5983 	}
5984 	if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) {
5985 		/* The following may work even if we cannot get a large buf. */
5986 		(void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG);
5987 		return (0);
5988 	}
5989 	(void) mi_mpprintf(mp,
5990 	    "UDP     " MI_COL_HDRPAD_STR
5991 	/*   12345678[89ABCDEF] */
5992 	    " zone lport src addr        dest addr       port  state");
5993 	/*    1234 12345 xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx 12345 UNBOUND */
5994 
5995 	zoneid = connp->conn_zoneid;
5996 
5997 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
5998 		connfp = &ipcl_globalhash_fanout[i];
5999 		connp = NULL;
6000 
6001 		while ((connp = ipcl_get_next_conn(connfp, connp,
6002 		    IPCL_UDP))) {
6003 			udp = connp->conn_udp;
6004 			if (zoneid != GLOBAL_ZONEID &&
6005 			    zoneid != connp->conn_zoneid)
6006 				continue;
6007 
6008 			udp_report_item(mp->b_cont, udp);
6009 		}
6010 	}
6011 	udp_last_ndd_get_info_time = ddi_get_lbolt();
6012 	return (0);
6013 }
6014 
6015 /*
6016  * This routine creates a T_UDERROR_IND message and passes it upstream.
6017  * The address and options are copied from the T_UNITDATA_REQ message
6018  * passed in mp.  This message is freed.
6019  */
6020 static void
6021 udp_ud_err(queue_t *q, mblk_t *mp, uchar_t *destaddr, t_scalar_t destlen,
6022     t_scalar_t err)
6023 {
6024 	struct T_unitdata_req *tudr;
6025 	mblk_t	*mp1;
6026 	uchar_t	*optaddr;
6027 	t_scalar_t optlen;
6028 
6029 	if (DB_TYPE(mp) == M_DATA) {
6030 		ASSERT(destaddr != NULL && destlen != 0);
6031 		optaddr = NULL;
6032 		optlen = 0;
6033 	} else {
6034 		if ((mp->b_wptr < mp->b_rptr) ||
6035 		    (MBLKL(mp)) < sizeof (struct T_unitdata_req)) {
6036 			goto done;
6037 		}
6038 		tudr = (struct T_unitdata_req *)mp->b_rptr;
6039 		destaddr = mp->b_rptr + tudr->DEST_offset;
6040 		if (destaddr < mp->b_rptr || destaddr >= mp->b_wptr ||
6041 		    destaddr + tudr->DEST_length < mp->b_rptr ||
6042 		    destaddr + tudr->DEST_length > mp->b_wptr) {
6043 			goto done;
6044 		}
6045 		optaddr = mp->b_rptr + tudr->OPT_offset;
6046 		if (optaddr < mp->b_rptr || optaddr >= mp->b_wptr ||
6047 		    optaddr + tudr->OPT_length < mp->b_rptr ||
6048 		    optaddr + tudr->OPT_length > mp->b_wptr) {
6049 			goto done;
6050 		}
6051 		destlen = tudr->DEST_length;
6052 		optlen = tudr->OPT_length;
6053 	}
6054 
6055 	mp1 = mi_tpi_uderror_ind((char *)destaddr, destlen,
6056 	    (char *)optaddr, optlen, err);
6057 	if (mp1 != NULL)
6058 		putnext(UDP_RD(q), mp1);
6059 
6060 done:
6061 	freemsg(mp);
6062 }
6063 
6064 /*
6065  * This routine removes a port number association from a stream.  It
6066  * is called by udp_wput to handle T_UNBIND_REQ messages.
6067  */
6068 static void
6069 udp_unbind(queue_t *q, mblk_t *mp)
6070 {
6071 	udp_t *udp = Q_TO_UDP(q);
6072 
6073 	/* If a bind has not been done, we can't unbind. */
6074 	if (udp->udp_state == TS_UNBND) {
6075 		udp_err_ack(q, mp, TOUTSTATE, 0);
6076 		return;
6077 	}
6078 	if (cl_inet_unbind != NULL) {
6079 		/*
6080 		 * Running in cluster mode - register unbind information
6081 		 */
6082 		if (udp->udp_ipversion == IPV4_VERSION) {
6083 			(*cl_inet_unbind)(IPPROTO_UDP, AF_INET,
6084 			    (uint8_t *)(&V4_PART_OF_V6(udp->udp_v6src)),
6085 			    (in_port_t)udp->udp_port);
6086 		} else {
6087 			(*cl_inet_unbind)(IPPROTO_UDP, AF_INET6,
6088 			    (uint8_t *)&(udp->udp_v6src),
6089 			    (in_port_t)udp->udp_port);
6090 		}
6091 	}
6092 
6093 	udp_bind_hash_remove(udp, B_FALSE);
6094 	V6_SET_ZERO(udp->udp_v6src);
6095 	V6_SET_ZERO(udp->udp_bound_v6src);
6096 	udp->udp_port = 0;
6097 	udp->udp_state = TS_UNBND;
6098 
6099 	if (udp->udp_family == AF_INET6) {
6100 		int error;
6101 
6102 		/* Rebuild the header template */
6103 		error = udp_build_hdrs(q, udp);
6104 		if (error != 0) {
6105 			udp_err_ack(q, mp, TSYSERR, error);
6106 			return;
6107 		}
6108 	}
6109 	/*
6110 	 * Pass the unbind to IP; T_UNBIND_REQ is larger than T_OK_ACK
6111 	 * and therefore ip_unbind must never return NULL.
6112 	 */
6113 	mp = ip_unbind(q, mp);
6114 	ASSERT(mp != NULL);
6115 	putnext(UDP_RD(q), mp);
6116 }
6117 
6118 /*
6119  * Don't let port fall into the privileged range.
6120  * Since the extra privileged ports can be arbitrary we also
6121  * ensure that we exclude those from consideration.
6122  * udp_g_epriv_ports is not sorted thus we loop over it until
6123  * there are no changes.
6124  */
6125 static in_port_t
6126 udp_update_next_port(udp_t *udp, in_port_t port, boolean_t random)
6127 {
6128 	int i;
6129 	in_port_t nextport;
6130 	boolean_t restart = B_FALSE;
6131 
6132 	if (random && udp_random_anon_port != 0) {
6133 		(void) random_get_pseudo_bytes((uint8_t *)&port,
6134 		    sizeof (in_port_t));
6135 		/*
6136 		 * Unless changed by a sys admin, the smallest anon port
6137 		 * is 32768 and the largest anon port is 65535.  It is
6138 		 * very likely (50%) for the random port to be smaller
6139 		 * than the smallest anon port.  When that happens,
6140 		 * add port % (anon port range) to the smallest anon
6141 		 * port to get the random port.  It should fall into the
6142 		 * valid anon port range.
6143 		 */
6144 		if (port < udp_smallest_anon_port) {
6145 			port = udp_smallest_anon_port +
6146 			    port % (udp_largest_anon_port -
6147 			    udp_smallest_anon_port);
6148 		}
6149 	}
6150 
6151 retry:
6152 	if (port < udp_smallest_anon_port)
6153 		port = udp_smallest_anon_port;
6154 
6155 	if (port > udp_largest_anon_port) {
6156 		port = udp_smallest_anon_port;
6157 		if (restart)
6158 			return (0);
6159 		restart = B_TRUE;
6160 	}
6161 
6162 	if (port < udp_smallest_nonpriv_port)
6163 		port = udp_smallest_nonpriv_port;
6164 
6165 	for (i = 0; i < udp_g_num_epriv_ports; i++) {
6166 		if (port == udp_g_epriv_ports[i]) {
6167 			port++;
6168 			/*
6169 			 * Make sure that the port is in the
6170 			 * valid range.
6171 			 */
6172 			goto retry;
6173 		}
6174 	}
6175 
6176 	if (is_system_labeled() &&
6177 	    (nextport = tsol_next_port(crgetzone(udp->udp_connp->conn_cred),
6178 	    port, IPPROTO_UDP, B_TRUE)) != 0) {
6179 		port = nextport;
6180 		goto retry;
6181 	}
6182 
6183 	return (port);
6184 }
6185 
6186 static int
6187 udp_update_label(queue_t *wq, mblk_t *mp, ipaddr_t dst)
6188 {
6189 	int err;
6190 	uchar_t opt_storage[IP_MAX_OPT_LENGTH];
6191 	udp_t *udp = Q_TO_UDP(wq);
6192 
6193 	err = tsol_compute_label(DB_CREDDEF(mp, udp->udp_connp->conn_cred), dst,
6194 	    opt_storage, udp->udp_mac_exempt);
6195 	if (err == 0) {
6196 		err = tsol_update_options(&udp->udp_ip_snd_options,
6197 		    &udp->udp_ip_snd_options_len, &udp->udp_label_len,
6198 		    opt_storage);
6199 	}
6200 	if (err != 0) {
6201 		DTRACE_PROBE4(
6202 		    tx__ip__log__info__updatelabel__udp,
6203 		    char *, "queue(1) failed to update options(2) on mp(3)",
6204 		    queue_t *, wq, char *, opt_storage, mblk_t *, mp);
6205 	} else {
6206 		IN6_IPADDR_TO_V4MAPPED(dst, &udp->udp_v6lastdst);
6207 	}
6208 	return (err);
6209 }
6210 
6211 static mblk_t *
6212 udp_output_v4(conn_t *connp, mblk_t *mp, ipaddr_t v4dst, uint16_t port,
6213     uint_t srcid, int *error)
6214 {
6215 	udp_t	*udp = connp->conn_udp;
6216 	queue_t	*q = connp->conn_wq;
6217 	mblk_t	*mp1 = mp;
6218 	mblk_t	*mp2;
6219 	ipha_t	*ipha;
6220 	int	ip_hdr_length;
6221 	uint32_t ip_len;
6222 	udpha_t	*udpha;
6223 	udpattrs_t	attrs;
6224 
6225 	*error = 0;
6226 
6227 	if (v4dst == INADDR_ANY)
6228 		v4dst = htonl(INADDR_LOOPBACK);
6229 
6230 	/*
6231 	 * If options passed in, feed it for verification and handling
6232 	 */
6233 	attrs.udpattr_credset = B_FALSE;
6234 	if (DB_TYPE(mp) != M_DATA) {
6235 		mp1 = mp->b_cont;
6236 		if (((struct T_unitdata_req *)mp->b_rptr)->OPT_length != 0) {
6237 			attrs.udpattr_ipp = NULL;
6238 			attrs.udpattr_mb = mp;
6239 			if (udp_unitdata_opt_process(q, mp, error, &attrs) < 0)
6240 				goto done;
6241 			/*
6242 			 * Note: success in processing options.
6243 			 * mp option buffer represented by
6244 			 * OPT_length/offset now potentially modified
6245 			 * and contain option setting results
6246 			 */
6247 			ASSERT(*error == 0);
6248 		}
6249 	}
6250 
6251 	/* mp1 points to the M_DATA mblk carrying the packet */
6252 	ASSERT(mp1 != NULL && DB_TYPE(mp1) == M_DATA);
6253 
6254 	/* Check if our saved options are valid; update if not */
6255 	if (is_system_labeled()) {
6256 		/* Using UDP MLP requires SCM_UCRED from user */
6257 		if (connp->conn_mlp_type != mlptSingle &&
6258 		    !attrs.udpattr_credset) {
6259 			DTRACE_PROBE4(
6260 			    tx__ip__log__info__output__udp,
6261 			    char *, "MLP mp(1) lacks SCM_UCRED attr(2) on q(3)",
6262 			    mblk_t *, mp1, udpattrs_t *, &attrs, queue_t *, q);
6263 			*error = ECONNREFUSED;
6264 			goto done;
6265 		}
6266 		if ((!IN6_IS_ADDR_V4MAPPED(&udp->udp_v6lastdst) ||
6267 		    V4_PART_OF_V6(udp->udp_v6lastdst) != v4dst) &&
6268 		    (*error = udp_update_label(q, mp, v4dst)) != 0)
6269 			goto done;
6270 	}
6271 
6272 	/* Add an IP header */
6273 	ip_hdr_length = IP_SIMPLE_HDR_LENGTH + UDPH_SIZE +
6274 	    udp->udp_ip_snd_options_len;
6275 	ipha = (ipha_t *)&mp1->b_rptr[-ip_hdr_length];
6276 	if (DB_REF(mp1) != 1 || (uchar_t *)ipha < DB_BASE(mp1) ||
6277 	    !OK_32PTR(ipha)) {
6278 		mp2 = allocb(ip_hdr_length + udp_wroff_extra, BPRI_LO);
6279 		if (mp2 == NULL) {
6280 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6281 			    "udp_wput_end: q %p (%S)", q, "allocbfail2");
6282 			*error = ENOMEM;
6283 			goto done;
6284 		}
6285 		mp2->b_wptr = DB_LIM(mp2);
6286 		mp2->b_cont = mp1;
6287 		mp1 = mp2;
6288 		if (DB_TYPE(mp) != M_DATA)
6289 			mp->b_cont = mp1;
6290 		else
6291 			mp = mp1;
6292 
6293 		ipha = (ipha_t *)(mp1->b_wptr - ip_hdr_length);
6294 	}
6295 	ip_hdr_length -= UDPH_SIZE;
6296 #ifdef	_BIG_ENDIAN
6297 	/* Set version, header length, and tos */
6298 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
6299 	    ((((IP_VERSION << 4) | (ip_hdr_length>>2)) << 8) |
6300 		udp->udp_type_of_service);
6301 	/* Set ttl and protocol */
6302 	*(uint16_t *)&ipha->ipha_ttl = (udp->udp_ttl << 8) | IPPROTO_UDP;
6303 #else
6304 	/* Set version, header length, and tos */
6305 	*(uint16_t *)&ipha->ipha_version_and_hdr_length =
6306 		((udp->udp_type_of_service << 8) |
6307 		    ((IP_VERSION << 4) | (ip_hdr_length>>2)));
6308 	/* Set ttl and protocol */
6309 	*(uint16_t *)&ipha->ipha_ttl = (IPPROTO_UDP << 8) | udp->udp_ttl;
6310 #endif
6311 	/*
6312 	 * Copy our address into the packet.  If this is zero,
6313 	 * first look at __sin6_src_id for a hint. If we leave the source
6314 	 * as INADDR_ANY then ip will fill in the real source address.
6315 	 */
6316 	IN6_V4MAPPED_TO_IPADDR(&udp->udp_v6src, ipha->ipha_src);
6317 	if (srcid != 0 && ipha->ipha_src == INADDR_ANY) {
6318 		in6_addr_t v6src;
6319 
6320 		ip_srcid_find_id(srcid, &v6src, connp->conn_zoneid);
6321 		IN6_V4MAPPED_TO_IPADDR(&v6src, ipha->ipha_src);
6322 	}
6323 
6324 	ipha->ipha_fragment_offset_and_flags = 0;
6325 	ipha->ipha_ident = 0;
6326 
6327 	mp1->b_rptr = (uchar_t *)ipha;
6328 
6329 	ASSERT((uintptr_t)(mp1->b_wptr - (uchar_t *)ipha) <=
6330 	    (uintptr_t)UINT_MAX);
6331 
6332 	/* Determine length of packet */
6333 	ip_len = (uint32_t)(mp1->b_wptr - (uchar_t *)ipha);
6334 	if ((mp2 = mp1->b_cont) != NULL) {
6335 		do {
6336 			ASSERT((uintptr_t)MBLKL(mp2) <= (uintptr_t)UINT_MAX);
6337 			ip_len += (uint32_t)MBLKL(mp2);
6338 		} while ((mp2 = mp2->b_cont) != NULL);
6339 	}
6340 	/*
6341 	 * If the size of the packet is greater than the maximum allowed by
6342 	 * ip, return an error. Passing this down could cause panics because
6343 	 * the size will have wrapped and be inconsistent with the msg size.
6344 	 */
6345 	if (ip_len > IP_MAXPACKET) {
6346 		TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6347 		    "udp_wput_end: q %p (%S)", q, "IP length exceeded");
6348 		*error = EMSGSIZE;
6349 		goto done;
6350 	}
6351 	ipha->ipha_length = htons((uint16_t)ip_len);
6352 	ip_len -= ip_hdr_length;
6353 	ip_len = htons((uint16_t)ip_len);
6354 	udpha = (udpha_t *)(((uchar_t *)ipha) + ip_hdr_length);
6355 
6356 	/*
6357 	 * Copy in the destination address
6358 	 */
6359 	ipha->ipha_dst = v4dst;
6360 
6361 	/*
6362 	 * Set ttl based on IP_MULTICAST_TTL to match IPv6 logic.
6363 	 */
6364 	if (CLASSD(v4dst))
6365 		ipha->ipha_ttl = udp->udp_multicast_ttl;
6366 
6367 	udpha->uha_dst_port = port;
6368 	udpha->uha_src_port = udp->udp_port;
6369 
6370 	if (ip_hdr_length > IP_SIMPLE_HDR_LENGTH) {
6371 		uint32_t	cksum;
6372 
6373 		bcopy(udp->udp_ip_snd_options, &ipha[1],
6374 		    udp->udp_ip_snd_options_len);
6375 		/*
6376 		 * Massage source route putting first source route in ipha_dst.
6377 		 * Ignore the destination in T_unitdata_req.
6378 		 * Create a checksum adjustment for a source route, if any.
6379 		 */
6380 		cksum = ip_massage_options(ipha);
6381 		cksum = (cksum & 0xFFFF) + (cksum >> 16);
6382 		cksum -= ((ipha->ipha_dst >> 16) & 0xFFFF) +
6383 		    (ipha->ipha_dst & 0xFFFF);
6384 		if ((int)cksum < 0)
6385 			cksum--;
6386 		cksum = (cksum & 0xFFFF) + (cksum >> 16);
6387 		/*
6388 		 * IP does the checksum if uha_checksum is non-zero,
6389 		 * We make it easy for IP to include our pseudo header
6390 		 * by putting our length in uha_checksum.
6391 		 */
6392 		cksum += ip_len;
6393 		cksum = (cksum & 0xFFFF) + (cksum >> 16);
6394 		/* There might be a carry. */
6395 		cksum = (cksum & 0xFFFF) + (cksum >> 16);
6396 #ifdef _LITTLE_ENDIAN
6397 		if (udp_do_checksum)
6398 			ip_len = (cksum << 16) | ip_len;
6399 #else
6400 		if (udp_do_checksum)
6401 			ip_len = (ip_len << 16) | cksum;
6402 		else
6403 			ip_len <<= 16;
6404 #endif
6405 	} else {
6406 		/*
6407 		 * IP does the checksum if uha_checksum is non-zero,
6408 		 * We make it easy for IP to include our pseudo header
6409 		 * by putting our length in uha_checksum.
6410 		 */
6411 		if (udp_do_checksum)
6412 			ip_len |= (ip_len << 16);
6413 #ifndef _LITTLE_ENDIAN
6414 		else
6415 			ip_len <<= 16;
6416 #endif
6417 	}
6418 	/* Set UDP length and checksum */
6419 	*((uint32_t *)&udpha->uha_length) = ip_len;
6420 	if (DB_CRED(mp) != NULL)
6421 		mblk_setcred(mp1, DB_CRED(mp));
6422 
6423 	if (DB_TYPE(mp) != M_DATA) {
6424 		ASSERT(mp != mp1);
6425 		freeb(mp);
6426 	}
6427 
6428 	/* mp has been consumed and we'll return success */
6429 	ASSERT(*error == 0);
6430 	mp = NULL;
6431 
6432 	/* We're done.  Pass the packet to ip. */
6433 	BUMP_MIB(&udp_mib, udpOutDatagrams);
6434 	TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6435 		"udp_wput_end: q %p (%S)", q, "end");
6436 
6437 	if ((connp->conn_flags & IPCL_CHECK_POLICY) != 0 ||
6438 	    CONN_OUTBOUND_POLICY_PRESENT(connp) ||
6439 	    connp->conn_dontroute || connp->conn_xmit_if_ill != NULL ||
6440 	    connp->conn_nofailover_ill != NULL ||
6441 	    connp->conn_outgoing_ill != NULL ||
6442 	    ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION ||
6443 	    IPP_ENABLED(IPP_LOCAL_OUT) || ip_g_mrouter != NULL) {
6444 		UDP_STAT(udp_ip_send);
6445 		ip_output(connp, mp1, connp->conn_wq, IP_WPUT);
6446 	} else {
6447 		udp_send_data(udp, connp->conn_wq, mp1, ipha);
6448 	}
6449 
6450 done:
6451 	if (*error != 0) {
6452 		ASSERT(mp != NULL);
6453 		BUMP_MIB(&udp_mib, udpOutErrors);
6454 	}
6455 	return (mp);
6456 }
6457 
6458 static void
6459 udp_send_data(udp_t *udp, queue_t *q, mblk_t *mp, ipha_t *ipha)
6460 {
6461 	conn_t	*connp = udp->udp_connp;
6462 	ipaddr_t src, dst;
6463 	ill_t	*ill;
6464 	ire_t	*ire;
6465 	ipif_t	*ipif = NULL;
6466 	mblk_t	*ire_fp_mp;
6467 	uint_t	ire_fp_mp_len;
6468 	uint16_t *up;
6469 	uint32_t cksum, hcksum_txflags;
6470 	queue_t	*dev_q;
6471 	boolean_t retry_caching;
6472 
6473 	dst = ipha->ipha_dst;
6474 	src = ipha->ipha_src;
6475 	ASSERT(ipha->ipha_ident == 0);
6476 
6477 	if (CLASSD(dst)) {
6478 		int err;
6479 
6480 		ipif = conn_get_held_ipif(connp,
6481 		    &connp->conn_multicast_ipif, &err);
6482 
6483 		if (ipif == NULL || ipif->ipif_isv6 ||
6484 		    (ipif->ipif_ill->ill_phyint->phyint_flags &
6485 		    PHYI_LOOPBACK)) {
6486 			if (ipif != NULL)
6487 				ipif_refrele(ipif);
6488 			UDP_STAT(udp_ip_send);
6489 			ip_output(connp, mp, q, IP_WPUT);
6490 			return;
6491 		}
6492 	}
6493 
6494 	retry_caching = B_FALSE;
6495 	mutex_enter(&connp->conn_lock);
6496 	ire = connp->conn_ire_cache;
6497 	ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT));
6498 
6499 	if (ire == NULL || ire->ire_addr != dst ||
6500 	    (ire->ire_marks & IRE_MARK_CONDEMNED)) {
6501 		retry_caching = B_TRUE;
6502 	} else if (CLASSD(dst) && (ire->ire_type & IRE_CACHE)) {
6503 		ill_t *stq_ill = (ill_t *)ire->ire_stq->q_ptr;
6504 
6505 		ASSERT(ipif != NULL);
6506 		if (stq_ill != ipif->ipif_ill && (stq_ill->ill_group == NULL ||
6507 		    stq_ill->ill_group != ipif->ipif_ill->ill_group))
6508 			retry_caching = B_TRUE;
6509 	}
6510 
6511 	if (!retry_caching) {
6512 		ASSERT(ire != NULL);
6513 		IRE_REFHOLD(ire);
6514 		mutex_exit(&connp->conn_lock);
6515 	} else {
6516 		boolean_t cached = B_FALSE;
6517 
6518 		connp->conn_ire_cache = NULL;
6519 		mutex_exit(&connp->conn_lock);
6520 
6521 		/* Release the old ire */
6522 		if (ire != NULL) {
6523 			IRE_REFRELE_NOTR(ire);
6524 			ire = NULL;
6525 		}
6526 
6527 		if (CLASSD(dst)) {
6528 			ASSERT(ipif != NULL);
6529 			ire = ire_ctable_lookup(dst, 0, 0, ipif,
6530 			    connp->conn_zoneid, MBLK_GETLABEL(mp),
6531 			    MATCH_IRE_ILL_GROUP);
6532 		} else {
6533 			ASSERT(ipif == NULL);
6534 			ire = ire_cache_lookup(dst, connp->conn_zoneid,
6535 			    MBLK_GETLABEL(mp));
6536 		}
6537 
6538 		if (ire == NULL) {
6539 			if (ipif != NULL)
6540 				ipif_refrele(ipif);
6541 			UDP_STAT(udp_ire_null);
6542 			ip_output(connp, mp, q, IP_WPUT);
6543 			return;
6544 		}
6545 		IRE_REFHOLD_NOTR(ire);
6546 
6547 		mutex_enter(&connp->conn_lock);
6548 		if (!(connp->conn_state_flags & CONN_CLOSING) &&
6549 		    connp->conn_ire_cache == NULL) {
6550 			rw_enter(&ire->ire_bucket->irb_lock, RW_READER);
6551 			if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) {
6552 				connp->conn_ire_cache = ire;
6553 				cached = B_TRUE;
6554 			}
6555 			rw_exit(&ire->ire_bucket->irb_lock);
6556 		}
6557 		mutex_exit(&connp->conn_lock);
6558 
6559 		/*
6560 		 * We can continue to use the ire but since it was not
6561 		 * cached, we should drop the extra reference.
6562 		 */
6563 		if (!cached)
6564 			IRE_REFRELE_NOTR(ire);
6565 	}
6566 	ASSERT(ire != NULL && ire->ire_ipversion == IPV4_VERSION);
6567 	ASSERT(!CLASSD(dst) || ipif != NULL);
6568 
6569 	if ((ire->ire_type & (IRE_BROADCAST|IRE_LOCAL|IRE_LOOPBACK)) ||
6570 	    (ire->ire_flags & RTF_MULTIRT) || ire->ire_stq == NULL ||
6571 	    ire->ire_max_frag < ntohs(ipha->ipha_length) ||
6572 	    (ire_fp_mp = ire->ire_fp_mp) == NULL ||
6573 	    (connp->conn_nexthop_set) ||
6574 	    (ire_fp_mp_len = MBLKL(ire_fp_mp)) > MBLKHEAD(mp)) {
6575 		if (ipif != NULL)
6576 			ipif_refrele(ipif);
6577 		UDP_STAT(udp_ip_ire_send);
6578 		IRE_REFRELE(ire);
6579 		ip_output(connp, mp, q, IP_WPUT);
6580 		return;
6581 	}
6582 
6583 	BUMP_MIB(&ip_mib, ipOutRequests);
6584 
6585 	ill = ire_to_ill(ire);
6586 	ASSERT(ill != NULL);
6587 
6588 	dev_q = ire->ire_stq->q_next;
6589 	ASSERT(dev_q != NULL);
6590 	/*
6591 	 * If the service thread is already running, or if the driver
6592 	 * queue is currently flow-controlled, queue this packet.
6593 	 */
6594 	if ((q->q_first != NULL || connp->conn_draining) ||
6595 	    ((dev_q->q_next || dev_q->q_first) && !canput(dev_q))) {
6596 		if (ip_output_queue) {
6597 			(void) putq(q, mp);
6598 		} else {
6599 			BUMP_MIB(&ip_mib, ipOutDiscards);
6600 			freemsg(mp);
6601 		}
6602 		if (ipif != NULL)
6603 			ipif_refrele(ipif);
6604 		IRE_REFRELE(ire);
6605 		return;
6606 	}
6607 
6608 	ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1);
6609 #ifndef _BIG_ENDIAN
6610 	ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8);
6611 #endif
6612 
6613 	if (src == INADDR_ANY && !connp->conn_unspec_src) {
6614 		if (CLASSD(dst) && !(ire->ire_flags & RTF_SETSRC))
6615 			src = ipha->ipha_src = ipif->ipif_src_addr;
6616 		else
6617 			src = ipha->ipha_src = ire->ire_src_addr;
6618 	}
6619 
6620 	if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) {
6621 		ASSERT(ill->ill_hcksum_capab != NULL);
6622 		hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags;
6623 	} else {
6624 		hcksum_txflags = 0;
6625 	}
6626 
6627 	/* pseudo-header checksum (do it in parts for IP header checksum) */
6628 	cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF);
6629 
6630 	ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION);
6631 	up = IPH_UDPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH);
6632 	if (*up != 0) {
6633 		IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags,
6634 		    mp, ipha, up, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH,
6635 		    ntohs(ipha->ipha_length), cksum);
6636 
6637 		/* Software checksum? */
6638 		if (DB_CKSUMFLAGS(mp) == 0) {
6639 			UDP_STAT(udp_out_sw_cksum);
6640 			UDP_STAT_UPDATE(udp_out_sw_cksum_bytes,
6641 			    ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH);
6642 		}
6643 	}
6644 
6645 	ipha->ipha_fragment_offset_and_flags |=
6646 	    (uint32_t)htons(ire->ire_frag_flag);
6647 
6648 	/* Calculate IP header checksum if hardware isn't capable */
6649 	if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) {
6650 		IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0],
6651 		    ((uint16_t *)ipha)[4]);
6652 	}
6653 
6654 	if (CLASSD(dst)) {
6655 		ilm_t *ilm;
6656 
6657 		ILM_WALKER_HOLD(ill);
6658 		ilm = ilm_lookup_ill(ill, dst, ALL_ZONES);
6659 		ILM_WALKER_RELE(ill);
6660 		if (ilm != NULL) {
6661 			ip_multicast_loopback(q, ill, mp,
6662 			    connp->conn_multicast_loop ? 0 :
6663 			    IP_FF_NO_MCAST_LOOP, connp->conn_zoneid);
6664 		}
6665 
6666 		/* If multicast TTL is 0 then we are done */
6667 		if (ipha->ipha_ttl == 0) {
6668 			if (ipif != NULL)
6669 				ipif_refrele(ipif);
6670 			freemsg(mp);
6671 			IRE_REFRELE(ire);
6672 			return;
6673 		}
6674 	}
6675 
6676 	ASSERT(DB_TYPE(ire_fp_mp) == M_DATA);
6677 	mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len;
6678 	bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len);
6679 
6680 	UPDATE_OB_PKT_COUNT(ire);
6681 	ire->ire_last_used_time = lbolt;
6682 
6683 	if (ILL_DLS_CAPABLE(ill)) {
6684 		/*
6685 		 * Send the packet directly to DLD, where it may be queued
6686 		 * depending on the availability of transmit resources at
6687 		 * the media layer.
6688 		 */
6689 		IP_DLS_ILL_TX(ill, mp);
6690 	} else {
6691 		putnext(ire->ire_stq, mp);
6692 	}
6693 
6694 	if (ipif != NULL)
6695 		ipif_refrele(ipif);
6696 	IRE_REFRELE(ire);
6697 }
6698 
6699 static boolean_t
6700 udp_update_label_v6(queue_t *wq, mblk_t *mp, in6_addr_t *dst)
6701 {
6702 	udp_t *udp = Q_TO_UDP(wq);
6703 	int err;
6704 	uchar_t opt_storage[TSOL_MAX_IPV6_OPTION];
6705 
6706 	err = tsol_compute_label_v6(DB_CREDDEF(mp, udp->udp_connp->conn_cred),
6707 	    dst, opt_storage, udp->udp_mac_exempt);
6708 	if (err == 0) {
6709 		err = tsol_update_sticky(&udp->udp_sticky_ipp,
6710 		    &udp->udp_label_len_v6, opt_storage);
6711 	}
6712 	if (err != 0) {
6713 		DTRACE_PROBE4(
6714 		    tx__ip__log__drop__updatelabel__udp6,
6715 		    char *, "queue(1) failed to update options(2) on mp(3)",
6716 		    queue_t *, wq, char *, opt_storage, mblk_t *, mp);
6717 	} else {
6718 		udp->udp_v6lastdst = *dst;
6719 	}
6720 	return (err);
6721 }
6722 
6723 /*
6724  * This routine handles all messages passed downstream.  It either
6725  * consumes the message or passes it downstream; it never queues a
6726  * a message.
6727  */
6728 static void
6729 udp_output(conn_t *connp, mblk_t *mp, struct sockaddr *addr, socklen_t addrlen)
6730 {
6731 	sin6_t		*sin6;
6732 	sin_t		*sin;
6733 	ipaddr_t	v4dst;
6734 	uint16_t	port;
6735 	uint_t		srcid;
6736 	queue_t		*q = connp->conn_wq;
6737 	udp_t		*udp = connp->conn_udp;
6738 	int		error = 0;
6739 	struct sockaddr_storage ss;
6740 
6741 	TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_START,
6742 	    "udp_wput_start: connp %p mp %p", connp, mp);
6743 
6744 	/*
6745 	 * We directly handle several cases here: T_UNITDATA_REQ message
6746 	 * coming down as M_PROTO/M_PCPROTO and M_DATA messages for both
6747 	 * connected and non-connected socket.  The latter carries the
6748 	 * address structure along when this routine gets called.
6749 	 */
6750 	switch (DB_TYPE(mp)) {
6751 	case M_DATA:
6752 		if (!udp->udp_direct_sockfs || udp->udp_state != TS_DATA_XFER) {
6753 			if (!udp->udp_direct_sockfs ||
6754 			    addr == NULL || addrlen == 0) {
6755 				/* Not connected; address is required */
6756 				BUMP_MIB(&udp_mib, udpOutErrors);
6757 				UDP_STAT(udp_out_err_notconn);
6758 				freemsg(mp);
6759 				TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6760 				    "udp_wput_end: connp %p (%S)", connp,
6761 				    "not-connected; address required");
6762 				return;
6763 			}
6764 			ASSERT(udp->udp_issocket);
6765 			UDP_DBGSTAT(udp_data_notconn);
6766 			/* Not connected; do some more checks below */
6767 			break;
6768 		}
6769 		/* M_DATA for connected socket */
6770 		UDP_DBGSTAT(udp_data_conn);
6771 		IN6_V4MAPPED_TO_IPADDR(&udp->udp_v6dst, v4dst);
6772 
6773 		/* Initialize addr and addrlen as if they're passed in */
6774 		if (udp->udp_family == AF_INET) {
6775 			sin = (sin_t *)&ss;
6776 			sin->sin_family = AF_INET;
6777 			sin->sin_port = udp->udp_dstport;
6778 			sin->sin_addr.s_addr = v4dst;
6779 			addr = (struct sockaddr *)sin;
6780 			addrlen = sizeof (*sin);
6781 		} else {
6782 			sin6 = (sin6_t *)&ss;
6783 			sin6->sin6_family = AF_INET6;
6784 			sin6->sin6_port = udp->udp_dstport;
6785 			sin6->sin6_flowinfo = udp->udp_flowinfo;
6786 			sin6->sin6_addr = udp->udp_v6dst;
6787 			sin6->sin6_scope_id = 0;
6788 			sin6->__sin6_src_id = 0;
6789 			addr = (struct sockaddr *)sin6;
6790 			addrlen = sizeof (*sin6);
6791 		}
6792 
6793 		if (udp->udp_family == AF_INET ||
6794 		    IN6_IS_ADDR_V4MAPPED(&udp->udp_v6dst)) {
6795 			/*
6796 			 * Handle both AF_INET and AF_INET6; the latter
6797 			 * for IPV4 mapped destination addresses.  Note
6798 			 * here that both addr and addrlen point to the
6799 			 * corresponding struct depending on the address
6800 			 * family of the socket.
6801 			 */
6802 			mp = udp_output_v4(connp, mp, v4dst,
6803 			    udp->udp_dstport, 0, &error);
6804 		} else {
6805 			mp = udp_output_v6(connp, mp, sin6, &error);
6806 		}
6807 		if (error != 0) {
6808 			ASSERT(addr != NULL && addrlen != 0);
6809 			goto ud_error;
6810 		}
6811 		return;
6812 	case M_PROTO:
6813 	case M_PCPROTO: {
6814 		struct T_unitdata_req *tudr;
6815 
6816 		ASSERT((uintptr_t)MBLKL(mp) <= (uintptr_t)INT_MAX);
6817 		tudr = (struct T_unitdata_req *)mp->b_rptr;
6818 
6819 		/* Handle valid T_UNITDATA_REQ here */
6820 		if (MBLKL(mp) >= sizeof (*tudr) &&
6821 		    ((t_primp_t)mp->b_rptr)->type == T_UNITDATA_REQ) {
6822 			if (mp->b_cont == NULL) {
6823 				TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6824 				    "udp_wput_end: q %p (%S)", q, "badaddr");
6825 				error = EPROTO;
6826 				goto ud_error;
6827 			}
6828 
6829 			if (!MBLKIN(mp, 0, tudr->DEST_offset +
6830 			    tudr->DEST_length)) {
6831 				TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6832 				    "udp_wput_end: q %p (%S)", q, "badaddr");
6833 				error = EADDRNOTAVAIL;
6834 				goto ud_error;
6835 			}
6836 			/*
6837 			 * If a port has not been bound to the stream, fail.
6838 			 * This is not a problem when sockfs is directly
6839 			 * above us, because it will ensure that the socket
6840 			 * is first bound before allowing data to be sent.
6841 			 */
6842 			if (udp->udp_state == TS_UNBND) {
6843 				TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6844 				    "udp_wput_end: q %p (%S)", q, "outstate");
6845 				error = EPROTO;
6846 				goto ud_error;
6847 			}
6848 			addr = (struct sockaddr *)
6849 			    &mp->b_rptr[tudr->DEST_offset];
6850 			addrlen = tudr->DEST_length;
6851 			if (tudr->OPT_length != 0)
6852 				UDP_STAT(udp_out_opt);
6853 			break;
6854 		}
6855 		/* FALLTHRU */
6856 	}
6857 	default:
6858 		udp_become_writer(connp, mp, udp_wput_other_wrapper,
6859 		    SQTAG_UDP_OUTPUT);
6860 		return;
6861 	}
6862 	ASSERT(addr != NULL);
6863 
6864 	switch (udp->udp_family) {
6865 	case AF_INET6:
6866 		sin6 = (sin6_t *)addr;
6867 		if (!OK_32PTR((char *)sin6) || addrlen != sizeof (sin6_t) ||
6868 		    sin6->sin6_family != AF_INET6) {
6869 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6870 			    "udp_wput_end: q %p (%S)", q, "badaddr");
6871 			error = EADDRNOTAVAIL;
6872 			goto ud_error;
6873 		}
6874 
6875 		if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6876 			/*
6877 			 * Destination is a non-IPv4-compatible IPv6 address.
6878 			 * Send out an IPv6 format packet.
6879 			 */
6880 			mp = udp_output_v6(connp, mp, sin6, &error);
6881 			if (error != 0)
6882 				goto ud_error;
6883 
6884 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6885 			    "udp_wput_end: q %p (%S)", q, "udp_output_v6");
6886 			return;
6887 		}
6888 		/*
6889 		 * If the local address is not zero or a mapped address
6890 		 * return an error.  It would be possible to send an IPv4
6891 		 * packet but the response would never make it back to the
6892 		 * application since it is bound to a non-mapped address.
6893 		 */
6894 		if (!IN6_IS_ADDR_V4MAPPED(&udp->udp_v6src) &&
6895 		    !IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src)) {
6896 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6897 			    "udp_wput_end: q %p (%S)", q, "badaddr");
6898 			error = EADDRNOTAVAIL;
6899 			goto ud_error;
6900 		}
6901 		/* Send IPv4 packet without modifying udp_ipversion */
6902 		/* Extract port and ipaddr */
6903 		port = sin6->sin6_port;
6904 		IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr, v4dst);
6905 		srcid = sin6->__sin6_src_id;
6906 		break;
6907 
6908 	case AF_INET:
6909 		sin = (sin_t *)addr;
6910 		if (!OK_32PTR((char *)sin) || addrlen != sizeof (sin_t) ||
6911 		    sin->sin_family != AF_INET) {
6912 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_END,
6913 			    "udp_wput_end: q %p (%S)", q, "badaddr");
6914 			error = EADDRNOTAVAIL;
6915 			goto ud_error;
6916 		}
6917 		/* Extract port and ipaddr */
6918 		port = sin->sin_port;
6919 		v4dst = sin->sin_addr.s_addr;
6920 		srcid = 0;
6921 		break;
6922 	}
6923 
6924 	mp = udp_output_v4(connp, mp, v4dst, port, srcid, &error);
6925 	if (error != 0) {
6926 ud_error:
6927 		UDP_STAT(udp_out_err_output);
6928 		ASSERT(mp != NULL);
6929 		/* mp is freed by the following routine */
6930 		udp_ud_err(q, mp, (uchar_t *)addr, (t_scalar_t)addrlen,
6931 		    (t_scalar_t)error);
6932 	}
6933 }
6934 
6935 /* ARGSUSED */
6936 static void
6937 udp_output_wrapper(void *arg, mblk_t *mp, void *arg2)
6938 {
6939 	udp_output((conn_t *)arg, mp, NULL, 0);
6940 	_UDP_EXIT((conn_t *)arg);
6941 }
6942 
6943 static void
6944 udp_wput(queue_t *q, mblk_t *mp)
6945 {
6946 	_UDP_ENTER(Q_TO_CONN(UDP_WR(q)), mp, udp_output_wrapper,
6947 	    SQTAG_UDP_WPUT);
6948 }
6949 
6950 /*
6951  * Allocate and prepare a T_UNITDATA_REQ message.
6952  */
6953 static mblk_t *
6954 udp_tudr_alloc(struct sockaddr *addr, socklen_t addrlen)
6955 {
6956 	struct T_unitdata_req *tudr;
6957 	mblk_t *mp;
6958 
6959 	mp = allocb(sizeof (*tudr) + addrlen, BPRI_MED);
6960 	if (mp != NULL) {
6961 		mp->b_wptr += sizeof (*tudr) + addrlen;
6962 		DB_TYPE(mp) = M_PROTO;
6963 
6964 		tudr = (struct T_unitdata_req *)mp->b_rptr;
6965 		tudr->PRIM_type = T_UNITDATA_REQ;
6966 		tudr->DEST_length = addrlen;
6967 		tudr->DEST_offset = (t_scalar_t)sizeof (*tudr);
6968 		tudr->OPT_length = 0;
6969 		tudr->OPT_offset = 0;
6970 		bcopy(addr, tudr+1, addrlen);
6971 	}
6972 	return (mp);
6973 }
6974 
6975 /*
6976  * Entry point for sockfs when udp is in "direct sockfs" mode.  This mode
6977  * is valid when we are directly beneath the stream head, and thus sockfs
6978  * is able to bypass STREAMS and directly call us, passing along the sockaddr
6979  * structure without the cumbersome T_UNITDATA_REQ interface.  Note that
6980  * this is done for both connected and non-connected endpoint.
6981  */
6982 void
6983 udp_wput_data(queue_t *q, mblk_t *mp, struct sockaddr *addr, socklen_t addrlen)
6984 {
6985 	conn_t	*connp;
6986 	udp_t	*udp;
6987 
6988 	q = UDP_WR(q);
6989 	connp = Q_TO_CONN(q);
6990 	udp = connp->conn_udp;
6991 
6992 	/* udpsockfs should only send down M_DATA for this entry point */
6993 	ASSERT(DB_TYPE(mp) == M_DATA);
6994 
6995 	mutex_enter(&connp->conn_lock);
6996 	UDP_MODE_ASSERTIONS(udp, UDP_ENTER);
6997 
6998 	if (udp->udp_mode != UDP_MT_HOT) {
6999 		/*
7000 		 * We can't enter this conn right away because another
7001 		 * thread is currently executing as writer; therefore we
7002 		 * need to deposit the message into the squeue to be
7003 		 * drained later.  If a socket address is present, we
7004 		 * need to create a T_UNITDATA_REQ message as placeholder.
7005 		 */
7006 		if (addr != NULL && addrlen != 0) {
7007 			mblk_t *tudr_mp = udp_tudr_alloc(addr, addrlen);
7008 
7009 			if (tudr_mp == NULL) {
7010 				mutex_exit(&connp->conn_lock);
7011 				BUMP_MIB(&udp_mib, udpOutErrors);
7012 				UDP_STAT(udp_out_err_tudr);
7013 				freemsg(mp);
7014 				return;
7015 			}
7016 			/* Tag the packet with T_UNITDATA_REQ */
7017 			tudr_mp->b_cont = mp;
7018 			mp = tudr_mp;
7019 		}
7020 		mutex_exit(&connp->conn_lock);
7021 		udp_enter(connp, mp, udp_output_wrapper, SQTAG_UDP_WPUT);
7022 		return;
7023 	}
7024 
7025 	/* We can execute as reader right away. */
7026 	UDP_READERS_INCREF(udp);
7027 	mutex_exit(&connp->conn_lock);
7028 
7029 	udp_output(connp, mp, addr, addrlen);
7030 
7031 	udp_exit(connp);
7032 }
7033 
7034 /*
7035  * udp_output_v6():
7036  * Assumes that udp_wput did some sanity checking on the destination
7037  * address.
7038  */
7039 static mblk_t *
7040 udp_output_v6(conn_t *connp, mblk_t *mp, sin6_t *sin6, int *error)
7041 {
7042 	ip6_t		*ip6h;
7043 	ip6i_t		*ip6i;	/* mp1->b_rptr even if no ip6i_t */
7044 	mblk_t		*mp1 = mp;
7045 	mblk_t		*mp2;
7046 	int		udp_ip_hdr_len = IPV6_HDR_LEN + UDPH_SIZE;
7047 	size_t		ip_len;
7048 	udpha_t		*udph;
7049 	udp_t		*udp = connp->conn_udp;
7050 	queue_t		*q = connp->conn_wq;
7051 	ip6_pkt_t	ipp_s;	/* For ancillary data options */
7052 	ip6_pkt_t	*ipp = &ipp_s;
7053 	ip6_pkt_t	*tipp;	/* temporary ipp */
7054 	uint32_t	csum = 0;
7055 	uint_t		ignore = 0;
7056 	uint_t		option_exists = 0, is_sticky = 0;
7057 	uint8_t		*cp;
7058 	uint8_t		*nxthdr_ptr;
7059 	in6_addr_t	ip6_dst;
7060 	udpattrs_t	attrs;
7061 	boolean_t	opt_present;
7062 
7063 	*error = 0;
7064 
7065 	/*
7066 	 * If the local address is a mapped address return
7067 	 * an error.
7068 	 * It would be possible to send an IPv6 packet but the
7069 	 * response would never make it back to the application
7070 	 * since it is bound to a mapped address.
7071 	 */
7072 	if (IN6_IS_ADDR_V4MAPPED(&udp->udp_v6src)) {
7073 		*error = EADDRNOTAVAIL;
7074 		goto done;
7075 	}
7076 
7077 	ipp->ipp_fields = 0;
7078 	ipp->ipp_sticky_ignored = 0;
7079 
7080 	/*
7081 	 * If TPI options passed in, feed it for verification and handling
7082 	 */
7083 	attrs.udpattr_credset = B_FALSE;
7084 	opt_present = B_FALSE;
7085 	if (DB_TYPE(mp) != M_DATA) {
7086 		mp1 = mp->b_cont;
7087 		if (((struct T_unitdata_req *)mp->b_rptr)->OPT_length != 0) {
7088 			attrs.udpattr_ipp = ipp;
7089 			attrs.udpattr_mb = mp;
7090 			if (udp_unitdata_opt_process(q, mp, error, &attrs) < 0)
7091 				goto done;
7092 			ASSERT(*error == 0);
7093 			opt_present = B_TRUE;
7094 		}
7095 	}
7096 	ignore = ipp->ipp_sticky_ignored;
7097 
7098 	/* mp1 points to the M_DATA mblk carrying the packet */
7099 	ASSERT(mp1 != NULL && DB_TYPE(mp1) == M_DATA);
7100 
7101 	if (sin6->sin6_scope_id != 0 &&
7102 	    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
7103 		/*
7104 		 * IPPF_SCOPE_ID is special.  It's neither a sticky
7105 		 * option nor ancillary data.  It needs to be
7106 		 * explicitly set in options_exists.
7107 		 */
7108 		option_exists |= IPPF_SCOPE_ID;
7109 	}
7110 
7111 	/*
7112 	 * Compute the destination address
7113 	 */
7114 	ip6_dst = sin6->sin6_addr;
7115 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
7116 		ip6_dst = ipv6_loopback;
7117 
7118 	/*
7119 	 * If we're not going to the same destination as last time, then
7120 	 * recompute the label required.  This is done in a separate routine to
7121 	 * avoid blowing up our stack here.
7122 	 */
7123 	if (is_system_labeled()) {
7124 		/* Using UDP MLP requires SCM_UCRED from user */
7125 		if (connp->conn_mlp_type != mlptSingle &&
7126 		    !attrs.udpattr_credset) {
7127 			DTRACE_PROBE4(
7128 			    tx__ip__log__info__output__udp6,
7129 			    char *, "MLP mp(1) lacks SCM_UCRED attr(2) on q(3)",
7130 			    mblk_t *, mp1, udpattrs_t *, &attrs, queue_t *, q);
7131 			*error = ECONNREFUSED;
7132 			goto done;
7133 		}
7134 		if ((opt_present ||
7135 		    !IN6_ARE_ADDR_EQUAL(&udp->udp_v6lastdst, &ip6_dst)) &&
7136 		    (*error = udp_update_label_v6(q, mp, &ip6_dst)) != 0)
7137 			goto done;
7138 	}
7139 
7140 	/*
7141 	 * If there's a security label here, then we ignore any options the
7142 	 * user may try to set.  We keep the peer's label as a hidden sticky
7143 	 * option.
7144 	 */
7145 	if (udp->udp_label_len_v6 > 0) {
7146 		ignore &= ~IPPF_HOPOPTS;
7147 		ipp->ipp_fields &= ~IPPF_HOPOPTS;
7148 	}
7149 
7150 	if ((udp->udp_sticky_ipp.ipp_fields == 0) && (ipp->ipp_fields == 0)) {
7151 		/* No sticky options nor ancillary data. */
7152 		goto no_options;
7153 	}
7154 
7155 	/*
7156 	 * Go through the options figuring out where each is going to
7157 	 * come from and build two masks.  The first mask indicates if
7158 	 * the option exists at all.  The second mask indicates if the
7159 	 * option is sticky or ancillary.
7160 	 */
7161 	if (!(ignore & IPPF_HOPOPTS)) {
7162 		if (ipp->ipp_fields & IPPF_HOPOPTS) {
7163 			option_exists |= IPPF_HOPOPTS;
7164 			udp_ip_hdr_len += ipp->ipp_hopoptslen;
7165 		} else if (udp->udp_sticky_ipp.ipp_fields & IPPF_HOPOPTS) {
7166 			option_exists |= IPPF_HOPOPTS;
7167 			is_sticky |= IPPF_HOPOPTS;
7168 			udp_ip_hdr_len += udp->udp_sticky_ipp.ipp_hopoptslen;
7169 		}
7170 	}
7171 
7172 	if (!(ignore & IPPF_RTHDR)) {
7173 		if (ipp->ipp_fields & IPPF_RTHDR) {
7174 			option_exists |= IPPF_RTHDR;
7175 			udp_ip_hdr_len += ipp->ipp_rthdrlen;
7176 		} else if (udp->udp_sticky_ipp.ipp_fields & IPPF_RTHDR) {
7177 			option_exists |= IPPF_RTHDR;
7178 			is_sticky |= IPPF_RTHDR;
7179 			udp_ip_hdr_len += udp->udp_sticky_ipp.ipp_rthdrlen;
7180 		}
7181 	}
7182 
7183 	if (!(ignore & IPPF_RTDSTOPTS) && (option_exists & IPPF_RTHDR)) {
7184 		if (ipp->ipp_fields & IPPF_RTDSTOPTS) {
7185 			option_exists |= IPPF_RTDSTOPTS;
7186 			udp_ip_hdr_len += ipp->ipp_rtdstoptslen;
7187 		} else if (udp->udp_sticky_ipp.ipp_fields & IPPF_RTDSTOPTS) {
7188 			option_exists |= IPPF_RTDSTOPTS;
7189 			is_sticky |= IPPF_RTDSTOPTS;
7190 			udp_ip_hdr_len += udp->udp_sticky_ipp.ipp_rtdstoptslen;
7191 		}
7192 	}
7193 
7194 	if (!(ignore & IPPF_DSTOPTS)) {
7195 		if (ipp->ipp_fields & IPPF_DSTOPTS) {
7196 			option_exists |= IPPF_DSTOPTS;
7197 			udp_ip_hdr_len += ipp->ipp_dstoptslen;
7198 		} else if (udp->udp_sticky_ipp.ipp_fields & IPPF_DSTOPTS) {
7199 			option_exists |= IPPF_DSTOPTS;
7200 			is_sticky |= IPPF_DSTOPTS;
7201 			udp_ip_hdr_len += udp->udp_sticky_ipp.ipp_dstoptslen;
7202 		}
7203 	}
7204 
7205 	if (!(ignore & IPPF_IFINDEX)) {
7206 		if (ipp->ipp_fields & IPPF_IFINDEX) {
7207 			option_exists |= IPPF_IFINDEX;
7208 		} else if (udp->udp_sticky_ipp.ipp_fields & IPPF_IFINDEX) {
7209 			option_exists |= IPPF_IFINDEX;
7210 			is_sticky |= IPPF_IFINDEX;
7211 		}
7212 	}
7213 
7214 	if (!(ignore & IPPF_ADDR)) {
7215 		if (ipp->ipp_fields & IPPF_ADDR) {
7216 			option_exists |= IPPF_ADDR;
7217 		} else if (udp->udp_sticky_ipp.ipp_fields & IPPF_ADDR) {
7218 			option_exists |= IPPF_ADDR;
7219 			is_sticky |= IPPF_ADDR;
7220 		}
7221 	}
7222 
7223 	if (!(ignore & IPPF_DONTFRAG)) {
7224 		if (ipp->ipp_fields & IPPF_DONTFRAG) {
7225 			option_exists |= IPPF_DONTFRAG;
7226 		} else if (udp->udp_sticky_ipp.ipp_fields & IPPF_DONTFRAG) {
7227 			option_exists |= IPPF_DONTFRAG;
7228 			is_sticky |= IPPF_DONTFRAG;
7229 		}
7230 	}
7231 
7232 	if (!(ignore & IPPF_USE_MIN_MTU)) {
7233 		if (ipp->ipp_fields & IPPF_USE_MIN_MTU) {
7234 			option_exists |= IPPF_USE_MIN_MTU;
7235 		} else if (udp->udp_sticky_ipp.ipp_fields &
7236 		    IPPF_USE_MIN_MTU) {
7237 			option_exists |= IPPF_USE_MIN_MTU;
7238 			is_sticky |= IPPF_USE_MIN_MTU;
7239 		}
7240 	}
7241 
7242 	if (!(ignore & IPPF_HOPLIMIT) && (ipp->ipp_fields & IPPF_HOPLIMIT))
7243 		option_exists |= IPPF_HOPLIMIT;
7244 	/* IPV6_HOPLIMIT can never be sticky */
7245 	ASSERT(!(udp->udp_sticky_ipp.ipp_fields & IPPF_HOPLIMIT));
7246 
7247 	if (!(ignore & IPPF_UNICAST_HOPS) &&
7248 	    (udp->udp_sticky_ipp.ipp_fields & IPPF_UNICAST_HOPS)) {
7249 		option_exists |= IPPF_UNICAST_HOPS;
7250 		is_sticky |= IPPF_UNICAST_HOPS;
7251 	}
7252 
7253 	if (!(ignore & IPPF_MULTICAST_HOPS) &&
7254 	    (udp->udp_sticky_ipp.ipp_fields & IPPF_MULTICAST_HOPS)) {
7255 		option_exists |= IPPF_MULTICAST_HOPS;
7256 		is_sticky |= IPPF_MULTICAST_HOPS;
7257 	}
7258 
7259 	if (!(ignore & IPPF_TCLASS)) {
7260 		if (ipp->ipp_fields & IPPF_TCLASS) {
7261 			option_exists |= IPPF_TCLASS;
7262 		} else if (udp->udp_sticky_ipp.ipp_fields & IPPF_TCLASS) {
7263 			option_exists |= IPPF_TCLASS;
7264 			is_sticky |= IPPF_TCLASS;
7265 		}
7266 	}
7267 
7268 no_options:
7269 
7270 	/*
7271 	 * If any options carried in the ip6i_t were specified, we
7272 	 * need to account for the ip6i_t in the data we'll be sending
7273 	 * down.
7274 	 */
7275 	if (option_exists & IPPF_HAS_IP6I)
7276 		udp_ip_hdr_len += sizeof (ip6i_t);
7277 
7278 	/* check/fix buffer config, setup pointers into it */
7279 	ip6h = (ip6_t *)&mp1->b_rptr[-udp_ip_hdr_len];
7280 	if (DB_REF(mp1) != 1 || ((unsigned char *)ip6h < DB_BASE(mp1)) ||
7281 	    !OK_32PTR(ip6h)) {
7282 		/* Try to get everything in a single mblk next time */
7283 		if (udp_ip_hdr_len > udp->udp_max_hdr_len) {
7284 			udp->udp_max_hdr_len = udp_ip_hdr_len;
7285 			(void) mi_set_sth_wroff(UDP_RD(q),
7286 			    udp->udp_max_hdr_len + udp_wroff_extra);
7287 		}
7288 		mp2 = allocb(udp_ip_hdr_len + udp_wroff_extra, BPRI_LO);
7289 		if (mp2 == NULL) {
7290 			*error = ENOMEM;
7291 			goto done;
7292 		}
7293 		mp2->b_wptr = DB_LIM(mp2);
7294 		mp2->b_cont = mp1;
7295 		mp1 = mp2;
7296 		if (DB_TYPE(mp) != M_DATA)
7297 			mp->b_cont = mp1;
7298 		else
7299 			mp = mp1;
7300 
7301 		ip6h = (ip6_t *)(mp1->b_wptr - udp_ip_hdr_len);
7302 	}
7303 	mp1->b_rptr = (unsigned char *)ip6h;
7304 	ip6i = (ip6i_t *)ip6h;
7305 
7306 #define	ANCIL_OR_STICKY_PTR(f) ((is_sticky & f) ? &udp->udp_sticky_ipp : ipp)
7307 	if (option_exists & IPPF_HAS_IP6I) {
7308 		ip6h = (ip6_t *)&ip6i[1];
7309 		ip6i->ip6i_flags = 0;
7310 		ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
7311 
7312 		/* sin6_scope_id takes precendence over IPPF_IFINDEX */
7313 		if (option_exists & IPPF_SCOPE_ID) {
7314 			ip6i->ip6i_flags |= IP6I_IFINDEX;
7315 			ip6i->ip6i_ifindex = sin6->sin6_scope_id;
7316 		} else if (option_exists & IPPF_IFINDEX) {
7317 			tipp = ANCIL_OR_STICKY_PTR(IPPF_IFINDEX);
7318 			ASSERT(tipp->ipp_ifindex != 0);
7319 			ip6i->ip6i_flags |= IP6I_IFINDEX;
7320 			ip6i->ip6i_ifindex = tipp->ipp_ifindex;
7321 		}
7322 
7323 		if (option_exists & IPPF_ADDR) {
7324 			/*
7325 			 * Enable per-packet source address verification if
7326 			 * IPV6_PKTINFO specified the source address.
7327 			 * ip6_src is set in the transport's _wput function.
7328 			 */
7329 			ip6i->ip6i_flags |= IP6I_VERIFY_SRC;
7330 		}
7331 
7332 		if (option_exists & IPPF_DONTFRAG) {
7333 			ip6i->ip6i_flags |= IP6I_DONTFRAG;
7334 		}
7335 
7336 		if (option_exists & IPPF_USE_MIN_MTU) {
7337 			ip6i->ip6i_flags = IP6I_API_USE_MIN_MTU(
7338 			    ip6i->ip6i_flags, ipp->ipp_use_min_mtu);
7339 		}
7340 
7341 		if (option_exists & IPPF_NEXTHOP) {
7342 			tipp = ANCIL_OR_STICKY_PTR(IPPF_NEXTHOP);
7343 			ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_nexthop));
7344 			ip6i->ip6i_flags |= IP6I_NEXTHOP;
7345 			ip6i->ip6i_nexthop = tipp->ipp_nexthop;
7346 		}
7347 
7348 		/*
7349 		 * tell IP this is an ip6i_t private header
7350 		 */
7351 		ip6i->ip6i_nxt = IPPROTO_RAW;
7352 	}
7353 
7354 	/* Initialize IPv6 header */
7355 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
7356 	bzero(&ip6h->ip6_src, sizeof (ip6h->ip6_src));
7357 
7358 	/* Set the hoplimit of the outgoing packet. */
7359 	if (option_exists & IPPF_HOPLIMIT) {
7360 		/* IPV6_HOPLIMIT ancillary data overrides all other settings. */
7361 		ip6h->ip6_hops = ipp->ipp_hoplimit;
7362 		ip6i->ip6i_flags |= IP6I_HOPLIMIT;
7363 	} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
7364 		ip6h->ip6_hops = udp->udp_multicast_ttl;
7365 		if (option_exists & IPPF_MULTICAST_HOPS)
7366 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
7367 	} else {
7368 		ip6h->ip6_hops = udp->udp_ttl;
7369 		if (option_exists & IPPF_UNICAST_HOPS)
7370 			ip6i->ip6i_flags |= IP6I_HOPLIMIT;
7371 	}
7372 
7373 	if (option_exists & IPPF_ADDR) {
7374 		tipp = ANCIL_OR_STICKY_PTR(IPPF_ADDR);
7375 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&tipp->ipp_addr));
7376 		ip6h->ip6_src = tipp->ipp_addr;
7377 	} else {
7378 		/*
7379 		 * The source address was not set using IPV6_PKTINFO.
7380 		 * First look at the bound source.
7381 		 * If unspecified fallback to __sin6_src_id.
7382 		 */
7383 		ip6h->ip6_src = udp->udp_v6src;
7384 		if (sin6->__sin6_src_id != 0 &&
7385 		    IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) {
7386 			ip_srcid_find_id(sin6->__sin6_src_id,
7387 			    &ip6h->ip6_src, connp->conn_zoneid);
7388 		}
7389 	}
7390 
7391 	nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt;
7392 	cp = (uint8_t *)&ip6h[1];
7393 
7394 	/*
7395 	 * Here's where we have to start stringing together
7396 	 * any extension headers in the right order:
7397 	 * Hop-by-hop, destination, routing, and final destination opts.
7398 	 */
7399 	if (option_exists & IPPF_HOPOPTS) {
7400 		/* Hop-by-hop options */
7401 		ip6_hbh_t *hbh = (ip6_hbh_t *)cp;
7402 		tipp = ANCIL_OR_STICKY_PTR(IPPF_HOPOPTS);
7403 
7404 		*nxthdr_ptr = IPPROTO_HOPOPTS;
7405 		nxthdr_ptr = &hbh->ip6h_nxt;
7406 
7407 		bcopy(tipp->ipp_hopopts, cp, tipp->ipp_hopoptslen);
7408 		cp += tipp->ipp_hopoptslen;
7409 	}
7410 	/*
7411 	 * En-route destination options
7412 	 * Only do them if there's a routing header as well
7413 	 */
7414 	if (option_exists & IPPF_RTDSTOPTS) {
7415 		ip6_dest_t *dst = (ip6_dest_t *)cp;
7416 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTDSTOPTS);
7417 
7418 		*nxthdr_ptr = IPPROTO_DSTOPTS;
7419 		nxthdr_ptr = &dst->ip6d_nxt;
7420 
7421 		bcopy(tipp->ipp_rtdstopts, cp, tipp->ipp_rtdstoptslen);
7422 		cp += tipp->ipp_rtdstoptslen;
7423 	}
7424 	/*
7425 	 * Routing header next
7426 	 */
7427 	if (option_exists & IPPF_RTHDR) {
7428 		ip6_rthdr_t *rt = (ip6_rthdr_t *)cp;
7429 		tipp = ANCIL_OR_STICKY_PTR(IPPF_RTHDR);
7430 
7431 		*nxthdr_ptr = IPPROTO_ROUTING;
7432 		nxthdr_ptr = &rt->ip6r_nxt;
7433 
7434 		bcopy(tipp->ipp_rthdr, cp, tipp->ipp_rthdrlen);
7435 		cp += tipp->ipp_rthdrlen;
7436 	}
7437 	/*
7438 	 * Do ultimate destination options
7439 	 */
7440 	if (option_exists & IPPF_DSTOPTS) {
7441 		ip6_dest_t *dest = (ip6_dest_t *)cp;
7442 		tipp = ANCIL_OR_STICKY_PTR(IPPF_DSTOPTS);
7443 
7444 		*nxthdr_ptr = IPPROTO_DSTOPTS;
7445 		nxthdr_ptr = &dest->ip6d_nxt;
7446 
7447 		bcopy(tipp->ipp_dstopts, cp, tipp->ipp_dstoptslen);
7448 		cp += tipp->ipp_dstoptslen;
7449 	}
7450 	/*
7451 	 * Now set the last header pointer to the proto passed in
7452 	 */
7453 	ASSERT((int)(cp - (uint8_t *)ip6i) == (udp_ip_hdr_len - UDPH_SIZE));
7454 	*nxthdr_ptr = IPPROTO_UDP;
7455 
7456 	/* Update UDP header */
7457 	udph = (udpha_t *)((uchar_t *)ip6i + udp_ip_hdr_len - UDPH_SIZE);
7458 	udph->uha_dst_port = sin6->sin6_port;
7459 	udph->uha_src_port = udp->udp_port;
7460 
7461 	/*
7462 	 * Copy in the destination address
7463 	 */
7464 	ip6h->ip6_dst = ip6_dst;
7465 
7466 	ip6h->ip6_vcf =
7467 	    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
7468 	    (sin6->sin6_flowinfo & ~IPV6_VERS_AND_FLOW_MASK);
7469 
7470 	if (option_exists & IPPF_TCLASS) {
7471 		tipp = ANCIL_OR_STICKY_PTR(IPPF_TCLASS);
7472 		ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf,
7473 		    tipp->ipp_tclass);
7474 	}
7475 
7476 	if (option_exists & IPPF_RTHDR) {
7477 		ip6_rthdr_t	*rth;
7478 
7479 		/*
7480 		 * Perform any processing needed for source routing.
7481 		 * We know that all extension headers will be in the same mblk
7482 		 * as the IPv6 header.
7483 		 */
7484 		rth = ip_find_rthdr_v6(ip6h, mp1->b_wptr);
7485 		if (rth != NULL && rth->ip6r_segleft != 0) {
7486 			if (rth->ip6r_type != IPV6_RTHDR_TYPE_0) {
7487 				/*
7488 				 * Drop packet - only support Type 0 routing.
7489 				 * Notify the application as well.
7490 				 */
7491 				*error = EPROTO;
7492 				goto done;
7493 			}
7494 
7495 			/*
7496 			 * rth->ip6r_len is twice the number of
7497 			 * addresses in the header. Thus it must be even.
7498 			 */
7499 			if (rth->ip6r_len & 0x1) {
7500 				*error = EPROTO;
7501 				goto done;
7502 			}
7503 			/*
7504 			 * Shuffle the routing header and ip6_dst
7505 			 * addresses, and get the checksum difference
7506 			 * between the first hop (in ip6_dst) and
7507 			 * the destination (in the last routing hdr entry).
7508 			 */
7509 			csum = ip_massage_options_v6(ip6h, rth);
7510 			/*
7511 			 * Verify that the first hop isn't a mapped address.
7512 			 * Routers along the path need to do this verification
7513 			 * for subsequent hops.
7514 			 */
7515 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
7516 				*error = EADDRNOTAVAIL;
7517 				goto done;
7518 			}
7519 
7520 			cp += (rth->ip6r_len + 1)*8;
7521 		}
7522 	}
7523 
7524 	/* count up length of UDP packet */
7525 	ip_len = (mp1->b_wptr - (unsigned char *)ip6h) - IPV6_HDR_LEN;
7526 	if ((mp2 = mp1->b_cont) != NULL) {
7527 		do {
7528 			ASSERT((uintptr_t)MBLKL(mp2) <= (uintptr_t)UINT_MAX);
7529 			ip_len += (uint32_t)MBLKL(mp2);
7530 		} while ((mp2 = mp2->b_cont) != NULL);
7531 	}
7532 
7533 	/*
7534 	 * If the size of the packet is greater than the maximum allowed by
7535 	 * ip, return an error. Passing this down could cause panics because
7536 	 * the size will have wrapped and be inconsistent with the msg size.
7537 	 */
7538 	if (ip_len > IP_MAXPACKET) {
7539 		*error = EMSGSIZE;
7540 		goto done;
7541 	}
7542 
7543 	/* Store the UDP length. Subtract length of extension hdrs */
7544 	udph->uha_length = htons(ip_len + IPV6_HDR_LEN -
7545 	    (int)((uchar_t *)udph - (uchar_t *)ip6h));
7546 
7547 	/*
7548 	 * We make it easy for IP to include our pseudo header
7549 	 * by putting our length in uh_checksum, modified (if
7550 	 * we have a routing header) by the checksum difference
7551 	 * between the ultimate destination and first hop addresses.
7552 	 * Note: UDP over IPv6 must always checksum the packet.
7553 	 */
7554 	csum += udph->uha_length;
7555 	csum = (csum & 0xFFFF) + (csum >> 16);
7556 	udph->uha_checksum = (uint16_t)csum;
7557 
7558 #ifdef _LITTLE_ENDIAN
7559 	ip_len = htons(ip_len);
7560 #endif
7561 	ip6h->ip6_plen = ip_len;
7562 	if (DB_CRED(mp) != NULL)
7563 		mblk_setcred(mp1, DB_CRED(mp));
7564 
7565 	if (DB_TYPE(mp) != M_DATA) {
7566 		ASSERT(mp != mp1);
7567 		freeb(mp);
7568 	}
7569 
7570 	/* mp has been consumed and we'll return success */
7571 	ASSERT(*error == 0);
7572 	mp = NULL;
7573 
7574 	/* We're done. Pass the packet to IP */
7575 	BUMP_MIB(&udp_mib, udpOutDatagrams);
7576 	ip_output_v6(connp, mp1, q, IP_WPUT);
7577 
7578 done:
7579 	if (*error != 0) {
7580 		ASSERT(mp != NULL);
7581 		BUMP_MIB(&udp_mib, udpOutErrors);
7582 	}
7583 	return (mp);
7584 }
7585 
7586 static void
7587 udp_wput_other(queue_t *q, mblk_t *mp)
7588 {
7589 	uchar_t	*rptr = mp->b_rptr;
7590 	struct datab *db;
7591 	struct iocblk *iocp;
7592 	cred_t	*cr;
7593 	conn_t	*connp = Q_TO_CONN(q);
7594 	udp_t	*udp = connp->conn_udp;
7595 
7596 	TRACE_1(TR_FAC_UDP, TR_UDP_WPUT_OTHER_START,
7597 		"udp_wput_other_start: q %p", q);
7598 
7599 	db = mp->b_datap;
7600 
7601 	cr = DB_CREDDEF(mp, connp->conn_cred);
7602 
7603 	switch (db->db_type) {
7604 	case M_PROTO:
7605 	case M_PCPROTO:
7606 		if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
7607 			freemsg(mp);
7608 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7609 				"udp_wput_other_end: q %p (%S)",
7610 				q, "protoshort");
7611 			return;
7612 		}
7613 		switch (((t_primp_t)rptr)->type) {
7614 		case T_ADDR_REQ:
7615 			udp_addr_req(q, mp);
7616 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7617 				"udp_wput_other_end: q %p (%S)", q, "addrreq");
7618 			return;
7619 		case O_T_BIND_REQ:
7620 		case T_BIND_REQ:
7621 			udp_bind(q, mp);
7622 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7623 				"udp_wput_other_end: q %p (%S)", q, "bindreq");
7624 			return;
7625 		case T_CONN_REQ:
7626 			udp_connect(q, mp);
7627 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7628 				"udp_wput_other_end: q %p (%S)", q, "connreq");
7629 			return;
7630 		case T_CAPABILITY_REQ:
7631 			udp_capability_req(q, mp);
7632 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7633 				"udp_wput_other_end: q %p (%S)", q, "capabreq");
7634 			return;
7635 		case T_INFO_REQ:
7636 			udp_info_req(q, mp);
7637 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7638 				"udp_wput_other_end: q %p (%S)", q, "inforeq");
7639 			return;
7640 		case T_UNITDATA_REQ:
7641 			/*
7642 			 * If a T_UNITDATA_REQ gets here, the address must
7643 			 * be bad.  Valid T_UNITDATA_REQs are handled
7644 			 * in udp_wput.
7645 			 */
7646 			udp_ud_err(q, mp, NULL, 0, EADDRNOTAVAIL);
7647 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7648 				"udp_wput_other_end: q %p (%S)",
7649 				q, "unitdatareq");
7650 			return;
7651 		case T_UNBIND_REQ:
7652 			udp_unbind(q, mp);
7653 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7654 			    "udp_wput_other_end: q %p (%S)", q, "unbindreq");
7655 			return;
7656 		case T_SVR4_OPTMGMT_REQ:
7657 			if (!snmpcom_req(q, mp, udp_snmp_set, udp_snmp_get, cr))
7658 				/*
7659 				 * Use upper queue for option processing in
7660 				 * case the request is not handled at this
7661 				 * level and needs to be passed down to IP.
7662 				 */
7663 				(void) svr4_optcom_req(_WR(UDP_RD(q)),
7664 				    mp, cr, &udp_opt_obj);
7665 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7666 			    "udp_wput_other_end: q %p (%S)",
7667 			    q, "optmgmtreq");
7668 			return;
7669 
7670 		case T_OPTMGMT_REQ:
7671 			/*
7672 			 * Use upper queue for option processing in
7673 			 * case the request is not handled at this
7674 			 * level and needs to be passed down to IP.
7675 			 */
7676 			(void) tpi_optcom_req(_WR(UDP_RD(q)),
7677 			    mp, cr, &udp_opt_obj);
7678 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7679 				"udp_wput_other_end: q %p (%S)",
7680 				q, "optmgmtreq");
7681 			return;
7682 
7683 		case T_DISCON_REQ:
7684 			udp_disconnect(q, mp);
7685 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7686 				"udp_wput_other_end: q %p (%S)",
7687 				q, "disconreq");
7688 			return;
7689 
7690 		/* The following TPI message is not supported by udp. */
7691 		case O_T_CONN_RES:
7692 		case T_CONN_RES:
7693 			udp_err_ack(q, mp, TNOTSUPPORT, 0);
7694 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7695 				"udp_wput_other_end: q %p (%S)",
7696 				q, "connres/disconreq");
7697 			return;
7698 
7699 		/* The following 3 TPI messages are illegal for udp. */
7700 		case T_DATA_REQ:
7701 		case T_EXDATA_REQ:
7702 		case T_ORDREL_REQ:
7703 			udp_err_ack(q, mp, TNOTSUPPORT, 0);
7704 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7705 				"udp_wput_other_end: q %p (%S)",
7706 				q, "data/exdata/ordrel");
7707 			return;
7708 		default:
7709 			break;
7710 		}
7711 		break;
7712 	case M_FLUSH:
7713 		if (*rptr & FLUSHW)
7714 			flushq(q, FLUSHDATA);
7715 		break;
7716 	case M_IOCTL:
7717 		iocp = (struct iocblk *)mp->b_rptr;
7718 		switch (iocp->ioc_cmd) {
7719 		case TI_GETPEERNAME:
7720 			if (udp->udp_state != TS_DATA_XFER) {
7721 				/*
7722 				 * If a default destination address has not
7723 				 * been associated with the stream, then we
7724 				 * don't know the peer's name.
7725 				 */
7726 				iocp->ioc_error = ENOTCONN;
7727 				iocp->ioc_count = 0;
7728 				mp->b_datap->db_type = M_IOCACK;
7729 				putnext(UDP_RD(q), mp);
7730 				TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7731 					"udp_wput_other_end: q %p (%S)",
7732 					q, "getpeername");
7733 				return;
7734 			}
7735 			/* FALLTHRU */
7736 		case TI_GETMYNAME: {
7737 			/*
7738 			 * For TI_GETPEERNAME and TI_GETMYNAME, we first
7739 			 * need to copyin the user's strbuf structure.
7740 			 * Processing will continue in the M_IOCDATA case
7741 			 * below.
7742 			 */
7743 			mi_copyin(q, mp, NULL,
7744 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
7745 			TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7746 				"udp_wput_other_end: q %p (%S)",
7747 				q, "getmyname");
7748 			return;
7749 			}
7750 		case ND_SET:
7751 			/* nd_getset performs the necessary checking */
7752 		case ND_GET:
7753 			if (nd_getset(q, udp_g_nd, mp)) {
7754 				putnext(UDP_RD(q), mp);
7755 				TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7756 					"udp_wput_other_end: q %p (%S)",
7757 					q, "get");
7758 				return;
7759 			}
7760 			break;
7761 		case _SIOCSOCKFALLBACK:
7762 			/*
7763 			 * Either sockmod is about to be popped and the
7764 			 * socket would now be treated as a plain stream,
7765 			 * or a module is about to be pushed so we could
7766 			 * no longer use read-side synchronous stream.
7767 			 * Drain any queued data and disable direct sockfs
7768 			 * interface from now on.
7769 			 */
7770 			if (!udp->udp_issocket) {
7771 				DB_TYPE(mp) = M_IOCNAK;
7772 				iocp->ioc_error = EINVAL;
7773 			} else {
7774 				udp->udp_issocket = B_FALSE;
7775 				if (udp->udp_direct_sockfs) {
7776 					/*
7777 					 * Disable read-side synchronous
7778 					 * stream interface and drain any
7779 					 * queued data.
7780 					 */
7781 					udp_rcv_drain(UDP_RD(q), udp,
7782 					    B_FALSE);
7783 					ASSERT(!udp->udp_direct_sockfs);
7784 					UDP_STAT(udp_sock_fallback);
7785 				}
7786 				DB_TYPE(mp) = M_IOCACK;
7787 				iocp->ioc_error = 0;
7788 			}
7789 			iocp->ioc_count = 0;
7790 			iocp->ioc_rval = 0;
7791 			putnext(UDP_RD(q), mp);
7792 			return;
7793 		default:
7794 			break;
7795 		}
7796 		break;
7797 	case M_IOCDATA:
7798 		udp_wput_iocdata(q, mp);
7799 		TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7800 			"udp_wput_other_end: q %p (%S)", q, "iocdata");
7801 		return;
7802 	default:
7803 		/* Unrecognized messages are passed through without change. */
7804 		break;
7805 	}
7806 	TRACE_2(TR_FAC_UDP, TR_UDP_WPUT_OTHER_END,
7807 		"udp_wput_other_end: q %p (%S)", q, "end");
7808 	ip_output(connp, mp, q, IP_WPUT);
7809 }
7810 
7811 /* ARGSUSED */
7812 static void
7813 udp_wput_other_wrapper(void *arg, mblk_t *mp, void *arg2)
7814 {
7815 	udp_wput_other(((conn_t *)arg)->conn_wq, mp);
7816 	udp_exit((conn_t *)arg);
7817 }
7818 
7819 /*
7820  * udp_wput_iocdata is called by udp_wput_other to handle all M_IOCDATA
7821  * messages.
7822  */
7823 static void
7824 udp_wput_iocdata(queue_t *q, mblk_t *mp)
7825 {
7826 	mblk_t	*mp1;
7827 	STRUCT_HANDLE(strbuf, sb);
7828 	uint16_t port;
7829 	in6_addr_t	v6addr;
7830 	ipaddr_t	v4addr;
7831 	uint32_t	flowinfo = 0;
7832 	int		addrlen;
7833 	udp_t		*udp = Q_TO_UDP(q);
7834 
7835 	/* Make sure it is one of ours. */
7836 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
7837 	case TI_GETMYNAME:
7838 	case TI_GETPEERNAME:
7839 		break;
7840 	default:
7841 		ip_output(Q_TO_CONN(q), mp, q, IP_WPUT);
7842 		return;
7843 	}
7844 
7845 	q = WR(UDP_RD(q));
7846 	switch (mi_copy_state(q, mp, &mp1)) {
7847 	case -1:
7848 		return;
7849 	case MI_COPY_CASE(MI_COPY_IN, 1):
7850 		break;
7851 	case MI_COPY_CASE(MI_COPY_OUT, 1):
7852 		/*
7853 		 * The address has been copied out, so now
7854 		 * copyout the strbuf.
7855 		 */
7856 		mi_copyout(q, mp);
7857 		return;
7858 	case MI_COPY_CASE(MI_COPY_OUT, 2):
7859 		/*
7860 		 * The address and strbuf have been copied out.
7861 		 * We're done, so just acknowledge the original
7862 		 * M_IOCTL.
7863 		 */
7864 		mi_copy_done(q, mp, 0);
7865 		return;
7866 	default:
7867 		/*
7868 		 * Something strange has happened, so acknowledge
7869 		 * the original M_IOCTL with an EPROTO error.
7870 		 */
7871 		mi_copy_done(q, mp, EPROTO);
7872 		return;
7873 	}
7874 
7875 	/*
7876 	 * Now we have the strbuf structure for TI_GETMYNAME
7877 	 * and TI_GETPEERNAME.  Next we copyout the requested
7878 	 * address and then we'll copyout the strbuf.
7879 	 */
7880 	STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
7881 	    (void *)mp1->b_rptr);
7882 	if (udp->udp_family == AF_INET)
7883 		addrlen = sizeof (sin_t);
7884 	else
7885 		addrlen = sizeof (sin6_t);
7886 
7887 	if (STRUCT_FGET(sb, maxlen) < addrlen) {
7888 		mi_copy_done(q, mp, EINVAL);
7889 		return;
7890 	}
7891 	switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
7892 	case TI_GETMYNAME:
7893 		if (udp->udp_family == AF_INET) {
7894 			ASSERT(udp->udp_ipversion == IPV4_VERSION);
7895 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&udp->udp_v6src) &&
7896 			    !IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src)) {
7897 				v4addr = V4_PART_OF_V6(udp->udp_v6src);
7898 			} else {
7899 				/*
7900 				 * INADDR_ANY
7901 				 * udp_v6src is not set, we might be bound to
7902 				 * broadcast/multicast. Use udp_bound_v6src as
7903 				 * local address instead (that could
7904 				 * also still be INADDR_ANY)
7905 				 */
7906 				v4addr = V4_PART_OF_V6(udp->udp_bound_v6src);
7907 			}
7908 		} else {
7909 			/* udp->udp_family == AF_INET6 */
7910 			if (!IN6_IS_ADDR_UNSPECIFIED(&udp->udp_v6src)) {
7911 				v6addr = udp->udp_v6src;
7912 			} else {
7913 				/*
7914 				 * UNSPECIFIED
7915 				 * udp_v6src is not set, we might be bound to
7916 				 * broadcast/multicast. Use udp_bound_v6src as
7917 				 * local address instead (that could
7918 				 * also still be UNSPECIFIED)
7919 				 */
7920 				v6addr = udp->udp_bound_v6src;
7921 			}
7922 		}
7923 		port = udp->udp_port;
7924 		break;
7925 	case TI_GETPEERNAME:
7926 		if (udp->udp_state != TS_DATA_XFER) {
7927 			mi_copy_done(q, mp, ENOTCONN);
7928 			return;
7929 		}
7930 		if (udp->udp_family == AF_INET) {
7931 			ASSERT(udp->udp_ipversion == IPV4_VERSION);
7932 			v4addr = V4_PART_OF_V6(udp->udp_v6dst);
7933 		} else {
7934 			/* udp->udp_family == AF_INET6) */
7935 			v6addr = udp->udp_v6dst;
7936 			flowinfo = udp->udp_flowinfo;
7937 		}
7938 		port = udp->udp_dstport;
7939 		break;
7940 	default:
7941 		mi_copy_done(q, mp, EPROTO);
7942 		return;
7943 	}
7944 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
7945 	if (!mp1)
7946 		return;
7947 
7948 	if (udp->udp_family == AF_INET) {
7949 		sin_t *sin;
7950 
7951 		STRUCT_FSET(sb, len, (int)sizeof (sin_t));
7952 		sin = (sin_t *)mp1->b_rptr;
7953 		mp1->b_wptr = (uchar_t *)&sin[1];
7954 		*sin = sin_null;
7955 		sin->sin_family = AF_INET;
7956 		sin->sin_addr.s_addr = v4addr;
7957 		sin->sin_port = port;
7958 	} else {
7959 		/* udp->udp_family == AF_INET6 */
7960 		sin6_t *sin6;
7961 
7962 		STRUCT_FSET(sb, len, (int)sizeof (sin6_t));
7963 		sin6 = (sin6_t *)mp1->b_rptr;
7964 		mp1->b_wptr = (uchar_t *)&sin6[1];
7965 		*sin6 = sin6_null;
7966 		sin6->sin6_family = AF_INET6;
7967 		sin6->sin6_flowinfo = flowinfo;
7968 		sin6->sin6_addr = v6addr;
7969 		sin6->sin6_port = port;
7970 	}
7971 	/* Copy out the address */
7972 	mi_copyout(q, mp);
7973 }
7974 
7975 
7976 static int
7977 udp_unitdata_opt_process(queue_t *q, mblk_t *mp, int *errorp,
7978     udpattrs_t *udpattrs)
7979 {
7980 	struct T_unitdata_req *udreqp;
7981 	int is_absreq_failure;
7982 	cred_t *cr;
7983 	conn_t	*connp = Q_TO_CONN(q);
7984 
7985 	ASSERT(((t_primp_t)mp->b_rptr)->type);
7986 
7987 	cr = DB_CREDDEF(mp, connp->conn_cred);
7988 
7989 	udreqp = (struct T_unitdata_req *)mp->b_rptr;
7990 
7991 	/*
7992 	 * Use upper queue for option processing since the callback
7993 	 * routines expect to be called in UDP instance instead of IP.
7994 	 */
7995 	*errorp = tpi_optcom_buf(_WR(UDP_RD(q)), mp, &udreqp->OPT_length,
7996 	    udreqp->OPT_offset, cr, &udp_opt_obj,
7997 	    udpattrs, &is_absreq_failure);
7998 
7999 	if (*errorp != 0) {
8000 		/*
8001 		 * Note: No special action needed in this
8002 		 * module for "is_absreq_failure"
8003 		 */
8004 		return (-1);		/* failure */
8005 	}
8006 	ASSERT(is_absreq_failure == 0);
8007 	return (0);	/* success */
8008 }
8009 
8010 void
8011 udp_ddi_init(void)
8012 {
8013 	int i;
8014 
8015 	UDP6_MAJ = ddi_name_to_major(UDP6);
8016 
8017 	udp_max_optsize = optcom_max_optsize(udp_opt_obj.odb_opt_des_arr,
8018 	    udp_opt_obj.odb_opt_arr_cnt);
8019 
8020 	if (udp_bind_fanout_size & (udp_bind_fanout_size - 1)) {
8021 		/* Not a power of two. Round up to nearest power of two */
8022 		for (i = 0; i < 31; i++) {
8023 			if (udp_bind_fanout_size < (1 << i))
8024 				break;
8025 		}
8026 		udp_bind_fanout_size = 1 << i;
8027 	}
8028 	udp_bind_fanout = kmem_zalloc(udp_bind_fanout_size *
8029 	    sizeof (udp_fanout_t), KM_SLEEP);
8030 	for (i = 0; i < udp_bind_fanout_size; i++) {
8031 		mutex_init(&udp_bind_fanout[i].uf_lock, NULL, MUTEX_DEFAULT,
8032 		    NULL);
8033 	}
8034 	(void) udp_param_register(udp_param_arr, A_CNT(udp_param_arr));
8035 
8036 	udp_kstat_init();
8037 
8038 	udp_cache = kmem_cache_create("udp_cache", sizeof (udp_t),
8039 	    CACHE_ALIGN_SIZE, NULL, NULL, NULL, NULL, NULL, 0);
8040 }
8041 
8042 void
8043 udp_ddi_destroy(void)
8044 {
8045 	int i;
8046 
8047 	nd_free(&udp_g_nd);
8048 
8049 	for (i = 0; i < udp_bind_fanout_size; i++) {
8050 		mutex_destroy(&udp_bind_fanout[i].uf_lock);
8051 	}
8052 
8053 	kmem_free(udp_bind_fanout, udp_bind_fanout_size *
8054 	    sizeof (udp_fanout_t));
8055 
8056 	udp_kstat_fini();
8057 
8058 	kmem_cache_destroy(udp_cache);
8059 }
8060 
8061 static void
8062 udp_kstat_init(void)
8063 {
8064 	udp_named_kstat_t template = {
8065 		{ "inDatagrams",	KSTAT_DATA_UINT32, 0 },
8066 		{ "inErrors",		KSTAT_DATA_UINT32, 0 },
8067 		{ "outDatagrams",	KSTAT_DATA_UINT32, 0 },
8068 		{ "entrySize",		KSTAT_DATA_INT32, 0 },
8069 		{ "entry6Size",		KSTAT_DATA_INT32, 0 },
8070 		{ "outErrors",		KSTAT_DATA_UINT32, 0 },
8071 	};
8072 
8073 	udp_mibkp = kstat_create(UDP_MOD_NAME, 0, UDP_MOD_NAME,
8074 	    "mib2", KSTAT_TYPE_NAMED, NUM_OF_FIELDS(udp_named_kstat_t), 0);
8075 
8076 	if (udp_mibkp == NULL)
8077 		return;
8078 
8079 	template.entrySize.value.ui32 = sizeof (mib2_udpEntry_t);
8080 	template.entry6Size.value.ui32 = sizeof (mib2_udp6Entry_t);
8081 
8082 	bcopy(&template, udp_mibkp->ks_data, sizeof (template));
8083 
8084 	udp_mibkp->ks_update = udp_kstat_update;
8085 
8086 	kstat_install(udp_mibkp);
8087 
8088 	if ((udp_ksp = kstat_create(UDP_MOD_NAME, 0, "udpstat",
8089 	    "net", KSTAT_TYPE_NAMED,
8090 	    sizeof (udp_statistics) / sizeof (kstat_named_t),
8091 	    KSTAT_FLAG_VIRTUAL)) != NULL) {
8092 		udp_ksp->ks_data = &udp_statistics;
8093 		kstat_install(udp_ksp);
8094 	}
8095 }
8096 
8097 static void
8098 udp_kstat_fini(void)
8099 {
8100 	if (udp_ksp != NULL) {
8101 		kstat_delete(udp_ksp);
8102 		udp_ksp = NULL;
8103 	}
8104 	if (udp_mibkp != NULL) {
8105 		kstat_delete(udp_mibkp);
8106 		udp_mibkp = NULL;
8107 	}
8108 }
8109 
8110 static int
8111 udp_kstat_update(kstat_t *kp, int rw)
8112 {
8113 	udp_named_kstat_t *udpkp;
8114 
8115 	if ((kp == NULL) || (kp->ks_data == NULL))
8116 		return (EIO);
8117 
8118 	if (rw == KSTAT_WRITE)
8119 		return (EACCES);
8120 
8121 	udpkp = (udp_named_kstat_t *)kp->ks_data;
8122 
8123 	udpkp->inDatagrams.value.ui32 =	udp_mib.udpInDatagrams;
8124 	udpkp->inErrors.value.ui32 =	udp_mib.udpInErrors;
8125 	udpkp->outDatagrams.value.ui32 = udp_mib.udpOutDatagrams;
8126 	udpkp->outErrors.value.ui32 =	udp_mib.udpOutErrors;
8127 
8128 	return (0);
8129 }
8130 
8131 /* ARGSUSED */
8132 static void
8133 udp_rput(queue_t *q, mblk_t *mp)
8134 {
8135 	/*
8136 	 * We get here whenever we do qreply() from IP,
8137 	 * i.e as part of handlings ioctls, etc.
8138 	 */
8139 	putnext(q, mp);
8140 }
8141 
8142 /*
8143  * Read-side synchronous stream info entry point, called as a
8144  * result of handling certain STREAMS ioctl operations.
8145  */
8146 static int
8147 udp_rinfop(queue_t *q, infod_t *dp)
8148 {
8149 	mblk_t	*mp;
8150 	uint_t	cmd = dp->d_cmd;
8151 	int	res = 0;
8152 	int	error = 0;
8153 	udp_t	*udp = Q_TO_UDP(RD(UDP_WR(q)));
8154 	struct stdata *stp = STREAM(q);
8155 
8156 	mutex_enter(&udp->udp_drain_lock);
8157 	/* If shutdown on read has happened, return nothing */
8158 	mutex_enter(&stp->sd_lock);
8159 	if (stp->sd_flag & STREOF) {
8160 		mutex_exit(&stp->sd_lock);
8161 		goto done;
8162 	}
8163 	mutex_exit(&stp->sd_lock);
8164 
8165 	if ((mp = udp->udp_rcv_list_head) == NULL)
8166 		goto done;
8167 
8168 	ASSERT(DB_TYPE(mp) != M_DATA && mp->b_cont != NULL);
8169 
8170 	if (cmd & INFOD_COUNT) {
8171 		/*
8172 		 * Return the number of messages.
8173 		 */
8174 		dp->d_count += udp->udp_rcv_msgcnt;
8175 		res |= INFOD_COUNT;
8176 	}
8177 	if (cmd & INFOD_BYTES) {
8178 		/*
8179 		 * Return size of all data messages.
8180 		 */
8181 		dp->d_bytes += udp->udp_rcv_cnt;
8182 		res |= INFOD_BYTES;
8183 	}
8184 	if (cmd & INFOD_FIRSTBYTES) {
8185 		/*
8186 		 * Return size of first data message.
8187 		 */
8188 		dp->d_bytes = msgdsize(mp);
8189 		res |= INFOD_FIRSTBYTES;
8190 		dp->d_cmd &= ~INFOD_FIRSTBYTES;
8191 	}
8192 	if (cmd & INFOD_COPYOUT) {
8193 		mblk_t *mp1 = mp->b_cont;
8194 		int n;
8195 		/*
8196 		 * Return data contents of first message.
8197 		 */
8198 		ASSERT(DB_TYPE(mp1) == M_DATA);
8199 		while (mp1 != NULL && dp->d_uiop->uio_resid > 0) {
8200 			n = MIN(dp->d_uiop->uio_resid, MBLKL(mp1));
8201 			if (n != 0 && (error = uiomove((char *)mp1->b_rptr, n,
8202 			    UIO_READ, dp->d_uiop)) != 0) {
8203 				goto done;
8204 			}
8205 			mp1 = mp1->b_cont;
8206 		}
8207 		res |= INFOD_COPYOUT;
8208 		dp->d_cmd &= ~INFOD_COPYOUT;
8209 	}
8210 done:
8211 	mutex_exit(&udp->udp_drain_lock);
8212 
8213 	dp->d_res |= res;
8214 
8215 	return (error);
8216 }
8217 
8218 /*
8219  * Read-side synchronous stream entry point.  This is called as a result
8220  * of recv/read operation done at sockfs, and is guaranteed to execute
8221  * outside of the interrupt thread context.  It returns a single datagram
8222  * (b_cont chain of T_UNITDATA_IND plus data) to the upper layer.
8223  */
8224 static int
8225 udp_rrw(queue_t *q, struiod_t *dp)
8226 {
8227 	mblk_t	*mp;
8228 	udp_t	*udp = Q_TO_UDP(_RD(UDP_WR(q)));
8229 
8230 	/* We should never get here when we're in SNMP mode */
8231 	ASSERT(!(udp->udp_connp->conn_flags & IPCL_UDPMOD));
8232 
8233 	/*
8234 	 * Dequeue datagram from the head of the list and return
8235 	 * it to caller; also ensure that RSLEEP sd_wakeq flag is
8236 	 * set/cleared depending on whether or not there's data
8237 	 * remaining in the list.
8238 	 */
8239 	mutex_enter(&udp->udp_drain_lock);
8240 	if (!udp->udp_direct_sockfs) {
8241 		mutex_exit(&udp->udp_drain_lock);
8242 		UDP_STAT(udp_rrw_busy);
8243 		return (EBUSY);
8244 	}
8245 	if ((mp = udp->udp_rcv_list_head) != NULL) {
8246 		uint_t size = msgdsize(mp);
8247 
8248 		/* Last datagram in the list? */
8249 		if ((udp->udp_rcv_list_head = mp->b_next) == NULL)
8250 			udp->udp_rcv_list_tail = NULL;
8251 		mp->b_next = NULL;
8252 
8253 		udp->udp_rcv_cnt -= size;
8254 		udp->udp_rcv_msgcnt--;
8255 		UDP_STAT(udp_rrw_msgcnt);
8256 
8257 		/* No longer flow-controlling? */
8258 		if (udp->udp_rcv_cnt < udp->udp_rcv_hiwat &&
8259 		    udp->udp_rcv_msgcnt < udp->udp_rcv_hiwat)
8260 			udp->udp_drain_qfull = B_FALSE;
8261 	}
8262 	if (udp->udp_rcv_list_head == NULL) {
8263 		/*
8264 		 * Either we just dequeued the last datagram or
8265 		 * we get here from sockfs and have nothing to
8266 		 * return; in this case clear RSLEEP.
8267 		 */
8268 		ASSERT(udp->udp_rcv_cnt == 0);
8269 		ASSERT(udp->udp_rcv_msgcnt == 0);
8270 		ASSERT(udp->udp_rcv_list_tail == NULL);
8271 		STR_WAKEUP_CLEAR(STREAM(q));
8272 	} else {
8273 		/*
8274 		 * More data follows; we need udp_rrw() to be
8275 		 * called in future to pick up the rest.
8276 		 */
8277 		STR_WAKEUP_SET(STREAM(q));
8278 	}
8279 	mutex_exit(&udp->udp_drain_lock);
8280 	dp->d_mp = mp;
8281 	return (0);
8282 }
8283 
8284 /*
8285  * Enqueue a completely-built T_UNITDATA_IND message into the receive
8286  * list; this is typically executed within the interrupt thread context
8287  * and so we do things as quickly as possible.
8288  */
8289 static void
8290 udp_rcv_enqueue(queue_t *q, udp_t *udp, mblk_t *mp, uint_t pkt_len)
8291 {
8292 	ASSERT(q == RD(q));
8293 	ASSERT(pkt_len == msgdsize(mp));
8294 	ASSERT(mp->b_next == NULL && mp->b_cont != NULL);
8295 	ASSERT(DB_TYPE(mp) == M_PROTO && DB_TYPE(mp->b_cont) == M_DATA);
8296 	ASSERT(MBLKL(mp) >= sizeof (struct T_unitdata_ind));
8297 
8298 	mutex_enter(&udp->udp_drain_lock);
8299 	/*
8300 	 * Wake up and signal the receiving app; it is okay to do this
8301 	 * before enqueueing the mp because we are holding the drain lock.
8302 	 * One of the advantages of synchronous stream is the ability for
8303 	 * us to find out when the application performs a read on the
8304 	 * socket by way of udp_rrw() entry point being called.  We need
8305 	 * to generate SIGPOLL/SIGIO for each received data in the case
8306 	 * of asynchronous socket just as in the strrput() case.  However,
8307 	 * we only wake the application up when necessary, i.e. during the
8308 	 * first enqueue.  When udp_rrw() is called, we send up a single
8309 	 * datagram upstream and call STR_WAKEUP_SET() again when there
8310 	 * are still data remaining in our receive queue.
8311 	 */
8312 	if (udp->udp_rcv_list_head == NULL) {
8313 		STR_WAKEUP_SET(STREAM(q));
8314 		udp->udp_rcv_list_head = mp;
8315 	} else {
8316 		udp->udp_rcv_list_tail->b_next = mp;
8317 	}
8318 	udp->udp_rcv_list_tail = mp;
8319 	udp->udp_rcv_cnt += pkt_len;
8320 	udp->udp_rcv_msgcnt++;
8321 
8322 	/* Need to flow-control? */
8323 	if (udp->udp_rcv_cnt >= udp->udp_rcv_hiwat ||
8324 	    udp->udp_rcv_msgcnt >= udp->udp_rcv_hiwat)
8325 		udp->udp_drain_qfull = B_TRUE;
8326 
8327 	/* Update poll events and send SIGPOLL/SIGIO if necessary */
8328 	STR_SENDSIG(STREAM(q));
8329 	mutex_exit(&udp->udp_drain_lock);
8330 }
8331 
8332 /*
8333  * Drain the contents of receive list to the module upstream; we do
8334  * this during close or when we fallback to the slow mode due to
8335  * sockmod being popped or a module being pushed on top of us.
8336  */
8337 static void
8338 udp_rcv_drain(queue_t *q, udp_t *udp, boolean_t closing)
8339 {
8340 	mblk_t *mp;
8341 
8342 	ASSERT(q == RD(q));
8343 
8344 	mutex_enter(&udp->udp_drain_lock);
8345 	/*
8346 	 * There is no race with a concurrent udp_input() sending
8347 	 * up packets using putnext() after we have cleared the
8348 	 * udp_direct_sockfs flag but before we have completed
8349 	 * sending up the packets in udp_rcv_list, since we are
8350 	 * either a writer or we have quiesced the conn.
8351 	 */
8352 	udp->udp_direct_sockfs = B_FALSE;
8353 	mutex_exit(&udp->udp_drain_lock);
8354 
8355 	if (udp->udp_rcv_list_head != NULL)
8356 		UDP_STAT(udp_drain);
8357 
8358 	/*
8359 	 * Send up everything via putnext(); note here that we
8360 	 * don't need the udp_drain_lock to protect us since
8361 	 * nothing can enter udp_rrw() and that we currently
8362 	 * have exclusive access to this udp.
8363 	 */
8364 	while ((mp = udp->udp_rcv_list_head) != NULL) {
8365 		udp->udp_rcv_list_head = mp->b_next;
8366 		mp->b_next = NULL;
8367 		udp->udp_rcv_cnt -= msgdsize(mp);
8368 		udp->udp_rcv_msgcnt--;
8369 		if (closing) {
8370 			freemsg(mp);
8371 		} else {
8372 			putnext(q, mp);
8373 		}
8374 	}
8375 	ASSERT(udp->udp_rcv_cnt == 0);
8376 	ASSERT(udp->udp_rcv_msgcnt == 0);
8377 	ASSERT(udp->udp_rcv_list_head == NULL);
8378 	udp->udp_rcv_list_tail = NULL;
8379 	udp->udp_drain_qfull = B_FALSE;
8380 }
8381 
8382 static size_t
8383 udp_set_rcv_hiwat(udp_t *udp, size_t size)
8384 {
8385 	/* We add a bit of extra buffering */
8386 	size += size >> 1;
8387 	if (size > udp_max_buf)
8388 		size = udp_max_buf;
8389 
8390 	udp->udp_rcv_hiwat = size;
8391 	return (size);
8392 }
8393 
8394 /*
8395  * Little helper for IPsec's NAT-T processing.
8396  */
8397 boolean_t
8398 udp_compute_checksum(void)
8399 {
8400 	return (udp_do_checksum);
8401 }
8402