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