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