xref: /titanic_50/usr/src/uts/common/inet/ip/conn_opt.c (revision 35a5a3587fd94b666239c157d3722745250ccbd7)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1990 Mentat Inc. */
27 
28 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/strsun.h>
31 #define	_SUN_TPI_VERSION 2
32 #include <sys/tihdr.h>
33 #include <sys/xti_inet.h>
34 #include <sys/ucred.h>
35 #include <sys/zone.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 #include <sys/cmn_err.h>
39 #include <sys/debug.h>
40 #include <sys/atomic.h>
41 #include <sys/policy.h>
42 
43 #include <sys/systm.h>
44 #include <sys/param.h>
45 #include <sys/kmem.h>
46 #include <sys/sdt.h>
47 #include <sys/socket.h>
48 #include <sys/ethernet.h>
49 #include <sys/mac.h>
50 #include <net/if.h>
51 #include <net/if_types.h>
52 #include <net/if_arp.h>
53 #include <net/route.h>
54 #include <sys/sockio.h>
55 #include <netinet/in.h>
56 #include <net/if_dl.h>
57 
58 #include <inet/common.h>
59 #include <inet/mi.h>
60 #include <inet/mib2.h>
61 #include <inet/nd.h>
62 #include <inet/arp.h>
63 #include <inet/snmpcom.h>
64 #include <inet/kstatcom.h>
65 
66 #include <netinet/igmp_var.h>
67 #include <netinet/ip6.h>
68 #include <netinet/icmp6.h>
69 #include <netinet/sctp.h>
70 
71 #include <inet/ip.h>
72 #include <inet/ip_impl.h>
73 #include <inet/ip6.h>
74 #include <inet/ip6_asp.h>
75 #include <inet/tcp.h>
76 #include <inet/ip_multi.h>
77 #include <inet/ip_if.h>
78 #include <inet/ip_ire.h>
79 #include <inet/ip_ftable.h>
80 #include <inet/ip_rts.h>
81 #include <inet/optcom.h>
82 #include <inet/ip_ndp.h>
83 #include <inet/ip_listutils.h>
84 #include <netinet/igmp.h>
85 #include <netinet/ip_mroute.h>
86 #include <netinet/udp.h>
87 #include <inet/ipp_common.h>
88 
89 #include <net/pfkeyv2.h>
90 #include <inet/sadb.h>
91 #include <inet/ipsec_impl.h>
92 #include <inet/ipdrop.h>
93 #include <inet/ip_netinfo.h>
94 
95 #include <inet/ipclassifier.h>
96 #include <inet/sctp_ip.h>
97 #include <inet/sctp/sctp_impl.h>
98 #include <inet/udp_impl.h>
99 #include <sys/sunddi.h>
100 
101 #include <sys/tsol/label.h>
102 #include <sys/tsol/tnet.h>
103 
104 static	sin_t	sin_null;	/* Zero address for quick clears */
105 static	sin6_t	sin6_null;	/* Zero address for quick clears */
106 
107 /*
108  * Return how much size is needed for the different ancillary data items
109  */
110 uint_t
111 conn_recvancillary_size(conn_t *connp, crb_t recv_ancillary,
112     ip_recv_attr_t *ira, mblk_t *mp, ip_pkt_t *ipp)
113 {
114 	uint_t		ancil_size;
115 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
116 
117 	/*
118 	 * If IP_RECVDSTADDR is set we include the destination IP
119 	 * address as an option. With IP_RECVOPTS we include all
120 	 * the IP options.
121 	 */
122 	ancil_size = 0;
123 	if (recv_ancillary.crb_recvdstaddr &&
124 	    (ira->ira_flags & IRAF_IS_IPV4)) {
125 		ancil_size += sizeof (struct T_opthdr) +
126 		    sizeof (struct in_addr);
127 		IP_STAT(ipst, conn_in_recvdstaddr);
128 	}
129 
130 	/*
131 	 * ip_recvpktinfo is used for both AF_INET and AF_INET6 but
132 	 * are different
133 	 */
134 	if (recv_ancillary.crb_ip_recvpktinfo &&
135 	    connp->conn_family == AF_INET) {
136 		ancil_size += sizeof (struct T_opthdr) +
137 		    sizeof (struct in_pktinfo);
138 		IP_STAT(ipst, conn_in_recvpktinfo);
139 	}
140 
141 	if ((recv_ancillary.crb_recvopts) &&
142 	    (ipp->ipp_fields & IPPF_IPV4_OPTIONS)) {
143 		ancil_size += sizeof (struct T_opthdr) +
144 		    ipp->ipp_ipv4_options_len;
145 		IP_STAT(ipst, conn_in_recvopts);
146 	}
147 
148 	if (recv_ancillary.crb_recvslla) {
149 		ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
150 		ill_t *ill;
151 
152 		/* Make sure ira_l2src is setup if not already */
153 		if (!(ira->ira_flags & IRAF_L2SRC_SET)) {
154 			ill = ill_lookup_on_ifindex(ira->ira_rifindex, B_FALSE,
155 			    ipst);
156 			if (ill != NULL) {
157 				ip_setl2src(mp, ira, ill);
158 				ill_refrele(ill);
159 			}
160 		}
161 		ancil_size += sizeof (struct T_opthdr) +
162 		    sizeof (struct sockaddr_dl);
163 		IP_STAT(ipst, conn_in_recvslla);
164 	}
165 
166 	if (recv_ancillary.crb_recvif) {
167 		ancil_size += sizeof (struct T_opthdr) + sizeof (uint_t);
168 		IP_STAT(ipst, conn_in_recvif);
169 	}
170 
171 	/*
172 	 * ip_recvpktinfo is used for both AF_INET and AF_INET6 but
173 	 * are different
174 	 */
175 	if (recv_ancillary.crb_ip_recvpktinfo &&
176 	    connp->conn_family == AF_INET6) {
177 		ancil_size += sizeof (struct T_opthdr) +
178 		    sizeof (struct in6_pktinfo);
179 		IP_STAT(ipst, conn_in_recvpktinfo);
180 	}
181 
182 	if (recv_ancillary.crb_ipv6_recvhoplimit) {
183 		ancil_size += sizeof (struct T_opthdr) + sizeof (int);
184 		IP_STAT(ipst, conn_in_recvhoplimit);
185 	}
186 
187 	if (recv_ancillary.crb_ipv6_recvtclass) {
188 		ancil_size += sizeof (struct T_opthdr) + sizeof (int);
189 		IP_STAT(ipst, conn_in_recvtclass);
190 	}
191 
192 	if (recv_ancillary.crb_ipv6_recvhopopts &&
193 	    (ipp->ipp_fields & IPPF_HOPOPTS)) {
194 		ancil_size += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen;
195 		IP_STAT(ipst, conn_in_recvhopopts);
196 	}
197 	/*
198 	 * To honor RFC3542 when an application asks for both IPV6_RECVDSTOPTS
199 	 * and IPV6_RECVRTHDR, we pass up the item rthdrdstopts (the destination
200 	 * options that appear before a routing header.
201 	 * We also pass them up if IPV6_RECVRTHDRDSTOPTS is set.
202 	 */
203 	if (ipp->ipp_fields & IPPF_RTHDRDSTOPTS) {
204 		if (recv_ancillary.crb_ipv6_recvrthdrdstopts ||
205 		    (recv_ancillary.crb_ipv6_recvdstopts &&
206 		    recv_ancillary.crb_ipv6_recvrthdr)) {
207 			ancil_size += sizeof (struct T_opthdr) +
208 			    ipp->ipp_rthdrdstoptslen;
209 			IP_STAT(ipst, conn_in_recvrthdrdstopts);
210 		}
211 	}
212 	if ((recv_ancillary.crb_ipv6_recvrthdr) &&
213 	    (ipp->ipp_fields & IPPF_RTHDR)) {
214 		ancil_size += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen;
215 		IP_STAT(ipst, conn_in_recvrthdr);
216 	}
217 	if ((recv_ancillary.crb_ipv6_recvdstopts ||
218 	    recv_ancillary.crb_old_ipv6_recvdstopts) &&
219 	    (ipp->ipp_fields & IPPF_DSTOPTS)) {
220 		ancil_size += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen;
221 		IP_STAT(ipst, conn_in_recvdstopts);
222 	}
223 	if (recv_ancillary.crb_recvucred && ira->ira_cred != NULL) {
224 		ancil_size += sizeof (struct T_opthdr) +
225 		    ucredminsize(ira->ira_cred);
226 		IP_STAT(ipst, conn_in_recvucred);
227 	}
228 
229 	/*
230 	 * If SO_TIMESTAMP is set allocate the appropriate sized
231 	 * buffer. Since gethrestime() expects a pointer aligned
232 	 * argument, we allocate space necessary for extra
233 	 * alignment (even though it might not be used).
234 	 */
235 	if (recv_ancillary.crb_timestamp) {
236 		ancil_size += sizeof (struct T_opthdr) +
237 		    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
238 		IP_STAT(ipst, conn_in_timestamp);
239 	}
240 
241 	/*
242 	 * If IP_RECVTTL is set allocate the appropriate sized buffer
243 	 */
244 	if (recv_ancillary.crb_recvttl &&
245 	    (ira->ira_flags & IRAF_IS_IPV4)) {
246 		ancil_size += sizeof (struct T_opthdr) + sizeof (uint8_t);
247 		IP_STAT(ipst, conn_in_recvttl);
248 	}
249 
250 	return (ancil_size);
251 }
252 
253 /*
254  * Lay down the ancillary data items at "ancil_buf".
255  * Assumes caller has used conn_recvancillary_size to allocate a sufficiently
256  * large buffer - ancil_size.
257  */
258 void
259 conn_recvancillary_add(conn_t *connp, crb_t recv_ancillary,
260     ip_recv_attr_t *ira, ip_pkt_t *ipp, uchar_t *ancil_buf, uint_t ancil_size)
261 {
262 	/*
263 	 * Copy in destination address before options to avoid
264 	 * any padding issues.
265 	 */
266 	if (recv_ancillary.crb_recvdstaddr &&
267 	    (ira->ira_flags & IRAF_IS_IPV4)) {
268 		struct T_opthdr *toh;
269 		ipaddr_t *dstptr;
270 
271 		toh = (struct T_opthdr *)ancil_buf;
272 		toh->level = IPPROTO_IP;
273 		toh->name = IP_RECVDSTADDR;
274 		toh->len = sizeof (struct T_opthdr) + sizeof (ipaddr_t);
275 		toh->status = 0;
276 		ancil_buf += sizeof (struct T_opthdr);
277 		dstptr = (ipaddr_t *)ancil_buf;
278 		*dstptr = ipp->ipp_addr_v4;
279 		ancil_buf += sizeof (ipaddr_t);
280 		ancil_size -= toh->len;
281 	}
282 
283 	/*
284 	 * ip_recvpktinfo is used for both AF_INET and AF_INET6 but
285 	 * are different
286 	 */
287 	if (recv_ancillary.crb_ip_recvpktinfo &&
288 	    connp->conn_family == AF_INET) {
289 		ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
290 		struct T_opthdr *toh;
291 		struct in_pktinfo *pktinfop;
292 		ill_t *ill;
293 		ipif_t *ipif;
294 
295 		toh = (struct T_opthdr *)ancil_buf;
296 		toh->level = IPPROTO_IP;
297 		toh->name = IP_PKTINFO;
298 		toh->len = sizeof (struct T_opthdr) + sizeof (*pktinfop);
299 		toh->status = 0;
300 		ancil_buf += sizeof (struct T_opthdr);
301 		pktinfop = (struct in_pktinfo *)ancil_buf;
302 
303 		pktinfop->ipi_ifindex = ira->ira_ruifindex;
304 		pktinfop->ipi_spec_dst.s_addr = INADDR_ANY;
305 
306 		/* Find a good address to report */
307 		ill = ill_lookup_on_ifindex(ira->ira_ruifindex, B_FALSE, ipst);
308 		if (ill != NULL) {
309 			ipif = ipif_good_addr(ill, IPCL_ZONEID(connp));
310 			if (ipif != NULL) {
311 				pktinfop->ipi_spec_dst.s_addr =
312 				    ipif->ipif_lcl_addr;
313 				ipif_refrele(ipif);
314 			}
315 			ill_refrele(ill);
316 		}
317 		pktinfop->ipi_addr.s_addr = ipp->ipp_addr_v4;
318 		ancil_buf += sizeof (struct in_pktinfo);
319 		ancil_size -= toh->len;
320 	}
321 
322 	if ((recv_ancillary.crb_recvopts) &&
323 	    (ipp->ipp_fields & IPPF_IPV4_OPTIONS)) {
324 		struct T_opthdr *toh;
325 
326 		toh = (struct T_opthdr *)ancil_buf;
327 		toh->level = IPPROTO_IP;
328 		toh->name = IP_RECVOPTS;
329 		toh->len = sizeof (struct T_opthdr) + ipp->ipp_ipv4_options_len;
330 		toh->status = 0;
331 		ancil_buf += sizeof (struct T_opthdr);
332 		bcopy(ipp->ipp_ipv4_options, ancil_buf,
333 		    ipp->ipp_ipv4_options_len);
334 		ancil_buf += ipp->ipp_ipv4_options_len;
335 		ancil_size -= toh->len;
336 	}
337 
338 	if (recv_ancillary.crb_recvslla) {
339 		ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
340 		struct T_opthdr *toh;
341 		struct sockaddr_dl *dstptr;
342 		ill_t *ill;
343 		int alen = 0;
344 
345 		ill = ill_lookup_on_ifindex(ira->ira_rifindex, B_FALSE, ipst);
346 		if (ill != NULL)
347 			alen = ill->ill_phys_addr_length;
348 
349 		/*
350 		 * For loopback multicast and broadcast the packet arrives
351 		 * with ira_ruifdex being the physical interface, but
352 		 * ira_l2src is all zero since ip_postfrag_loopback doesn't
353 		 * know our l2src. We don't report the address in that case.
354 		 */
355 		if (ira->ira_flags & IRAF_LOOPBACK)
356 			alen = 0;
357 
358 		toh = (struct T_opthdr *)ancil_buf;
359 		toh->level = IPPROTO_IP;
360 		toh->name = IP_RECVSLLA;
361 		toh->len = sizeof (struct T_opthdr) +
362 		    sizeof (struct sockaddr_dl);
363 		toh->status = 0;
364 		ancil_buf += sizeof (struct T_opthdr);
365 		dstptr = (struct sockaddr_dl *)ancil_buf;
366 		dstptr->sdl_family = AF_LINK;
367 		dstptr->sdl_index = ira->ira_ruifindex;
368 		if (ill != NULL)
369 			dstptr->sdl_type = ill->ill_type;
370 		else
371 			dstptr->sdl_type = 0;
372 		dstptr->sdl_nlen = 0;
373 		dstptr->sdl_alen = alen;
374 		dstptr->sdl_slen = 0;
375 		bcopy(ira->ira_l2src, dstptr->sdl_data, alen);
376 		ancil_buf += sizeof (struct sockaddr_dl);
377 		ancil_size -= toh->len;
378 		if (ill != NULL)
379 			ill_refrele(ill);
380 	}
381 
382 	if (recv_ancillary.crb_recvif) {
383 		struct T_opthdr *toh;
384 		uint_t		*dstptr;
385 
386 		toh = (struct T_opthdr *)ancil_buf;
387 		toh->level = IPPROTO_IP;
388 		toh->name = IP_RECVIF;
389 		toh->len = sizeof (struct T_opthdr) + sizeof (uint_t);
390 		toh->status = 0;
391 		ancil_buf += sizeof (struct T_opthdr);
392 		dstptr = (uint_t *)ancil_buf;
393 		*dstptr = ira->ira_ruifindex;
394 		ancil_buf += sizeof (uint_t);
395 		ancil_size -= toh->len;
396 	}
397 
398 	/*
399 	 * ip_recvpktinfo is used for both AF_INET and AF_INET6 but
400 	 * are different
401 	 */
402 	if (recv_ancillary.crb_ip_recvpktinfo &&
403 	    connp->conn_family == AF_INET6) {
404 		struct T_opthdr *toh;
405 		struct in6_pktinfo *pkti;
406 
407 		toh = (struct T_opthdr *)ancil_buf;
408 		toh->level = IPPROTO_IPV6;
409 		toh->name = IPV6_PKTINFO;
410 		toh->len = sizeof (struct T_opthdr) + sizeof (*pkti);
411 		toh->status = 0;
412 		ancil_buf += sizeof (struct T_opthdr);
413 		pkti = (struct in6_pktinfo *)ancil_buf;
414 		if (ira->ira_flags & IRAF_IS_IPV4) {
415 			IN6_IPADDR_TO_V4MAPPED(ipp->ipp_addr_v4,
416 			    &pkti->ipi6_addr);
417 		} else {
418 			pkti->ipi6_addr = ipp->ipp_addr;
419 		}
420 		pkti->ipi6_ifindex = ira->ira_ruifindex;
421 
422 		ancil_buf += sizeof (*pkti);
423 		ancil_size -= toh->len;
424 	}
425 	if (recv_ancillary.crb_ipv6_recvhoplimit) {
426 		struct T_opthdr *toh;
427 
428 		toh = (struct T_opthdr *)ancil_buf;
429 		toh->level = IPPROTO_IPV6;
430 		toh->name = IPV6_HOPLIMIT;
431 		toh->len = sizeof (struct T_opthdr) + sizeof (uint_t);
432 		toh->status = 0;
433 		ancil_buf += sizeof (struct T_opthdr);
434 		*(uint_t *)ancil_buf = ipp->ipp_hoplimit;
435 		ancil_buf += sizeof (uint_t);
436 		ancil_size -= toh->len;
437 	}
438 	if (recv_ancillary.crb_ipv6_recvtclass) {
439 		struct T_opthdr *toh;
440 
441 		toh = (struct T_opthdr *)ancil_buf;
442 		toh->level = IPPROTO_IPV6;
443 		toh->name = IPV6_TCLASS;
444 		toh->len = sizeof (struct T_opthdr) + sizeof (uint_t);
445 		toh->status = 0;
446 		ancil_buf += sizeof (struct T_opthdr);
447 
448 		if (ira->ira_flags & IRAF_IS_IPV4)
449 			*(uint_t *)ancil_buf = ipp->ipp_type_of_service;
450 		else
451 			*(uint_t *)ancil_buf = ipp->ipp_tclass;
452 		ancil_buf += sizeof (uint_t);
453 		ancil_size -= toh->len;
454 	}
455 	if (recv_ancillary.crb_ipv6_recvhopopts &&
456 	    (ipp->ipp_fields & IPPF_HOPOPTS)) {
457 		struct T_opthdr *toh;
458 
459 		toh = (struct T_opthdr *)ancil_buf;
460 		toh->level = IPPROTO_IPV6;
461 		toh->name = IPV6_HOPOPTS;
462 		toh->len = sizeof (struct T_opthdr) + ipp->ipp_hopoptslen;
463 		toh->status = 0;
464 		ancil_buf += sizeof (struct T_opthdr);
465 		bcopy(ipp->ipp_hopopts, ancil_buf, ipp->ipp_hopoptslen);
466 		ancil_buf += ipp->ipp_hopoptslen;
467 		ancil_size -= toh->len;
468 	}
469 	/*
470 	 * To honor RFC3542 when an application asks for both IPV6_RECVDSTOPTS
471 	 * and IPV6_RECVRTHDR, we pass up the item rthdrdstopts (the destination
472 	 * options that appear before a routing header.
473 	 * We also pass them up if IPV6_RECVRTHDRDSTOPTS is set.
474 	 */
475 	if (ipp->ipp_fields & IPPF_RTHDRDSTOPTS) {
476 		if (recv_ancillary.crb_ipv6_recvrthdrdstopts ||
477 		    (recv_ancillary.crb_ipv6_recvdstopts &&
478 		    recv_ancillary.crb_ipv6_recvrthdr)) {
479 			struct T_opthdr *toh;
480 
481 			toh = (struct T_opthdr *)ancil_buf;
482 			toh->level = IPPROTO_IPV6;
483 			toh->name = IPV6_DSTOPTS;
484 			toh->len = sizeof (struct T_opthdr) +
485 			    ipp->ipp_rthdrdstoptslen;
486 			toh->status = 0;
487 			ancil_buf += sizeof (struct T_opthdr);
488 			bcopy(ipp->ipp_rthdrdstopts, ancil_buf,
489 			    ipp->ipp_rthdrdstoptslen);
490 			ancil_buf += ipp->ipp_rthdrdstoptslen;
491 			ancil_size -= toh->len;
492 		}
493 	}
494 	if (recv_ancillary.crb_ipv6_recvrthdr &&
495 	    (ipp->ipp_fields & IPPF_RTHDR)) {
496 		struct T_opthdr *toh;
497 
498 		toh = (struct T_opthdr *)ancil_buf;
499 		toh->level = IPPROTO_IPV6;
500 		toh->name = IPV6_RTHDR;
501 		toh->len = sizeof (struct T_opthdr) + ipp->ipp_rthdrlen;
502 		toh->status = 0;
503 		ancil_buf += sizeof (struct T_opthdr);
504 		bcopy(ipp->ipp_rthdr, ancil_buf, ipp->ipp_rthdrlen);
505 		ancil_buf += ipp->ipp_rthdrlen;
506 		ancil_size -= toh->len;
507 	}
508 	if ((recv_ancillary.crb_ipv6_recvdstopts ||
509 	    recv_ancillary.crb_old_ipv6_recvdstopts) &&
510 	    (ipp->ipp_fields & IPPF_DSTOPTS)) {
511 		struct T_opthdr *toh;
512 
513 		toh = (struct T_opthdr *)ancil_buf;
514 		toh->level = IPPROTO_IPV6;
515 		toh->name = IPV6_DSTOPTS;
516 		toh->len = sizeof (struct T_opthdr) + ipp->ipp_dstoptslen;
517 		toh->status = 0;
518 		ancil_buf += sizeof (struct T_opthdr);
519 		bcopy(ipp->ipp_dstopts, ancil_buf, ipp->ipp_dstoptslen);
520 		ancil_buf += ipp->ipp_dstoptslen;
521 		ancil_size -= toh->len;
522 	}
523 
524 	if (recv_ancillary.crb_recvucred && ira->ira_cred != NULL) {
525 		struct T_opthdr *toh;
526 		cred_t		*rcr = connp->conn_cred;
527 
528 		toh = (struct T_opthdr *)ancil_buf;
529 		toh->level = SOL_SOCKET;
530 		toh->name = SCM_UCRED;
531 		toh->len = sizeof (struct T_opthdr) +
532 		    ucredminsize(ira->ira_cred);
533 		toh->status = 0;
534 		(void) cred2ucred(ira->ira_cred, ira->ira_cpid, &toh[1], rcr);
535 		ancil_buf += toh->len;
536 		ancil_size -= toh->len;
537 	}
538 	if (recv_ancillary.crb_timestamp) {
539 		struct	T_opthdr *toh;
540 
541 		toh = (struct T_opthdr *)ancil_buf;
542 		toh->level = SOL_SOCKET;
543 		toh->name = SCM_TIMESTAMP;
544 		toh->len = sizeof (struct T_opthdr) +
545 		    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
546 		toh->status = 0;
547 		ancil_buf += sizeof (struct T_opthdr);
548 		/* Align for gethrestime() */
549 		ancil_buf = (uchar_t *)P2ROUNDUP((intptr_t)ancil_buf,
550 		    sizeof (intptr_t));
551 		gethrestime((timestruc_t *)ancil_buf);
552 		ancil_buf = (uchar_t *)toh + toh->len;
553 		ancil_size -= toh->len;
554 	}
555 
556 	/*
557 	 * CAUTION:
558 	 * Due to aligment issues
559 	 * Processing of IP_RECVTTL option
560 	 * should always be the last. Adding
561 	 * any option processing after this will
562 	 * cause alignment panic.
563 	 */
564 	if (recv_ancillary.crb_recvttl &&
565 	    (ira->ira_flags & IRAF_IS_IPV4)) {
566 		struct	T_opthdr *toh;
567 		uint8_t	*dstptr;
568 
569 		toh = (struct T_opthdr *)ancil_buf;
570 		toh->level = IPPROTO_IP;
571 		toh->name = IP_RECVTTL;
572 		toh->len = sizeof (struct T_opthdr) + sizeof (uint8_t);
573 		toh->status = 0;
574 		ancil_buf += sizeof (struct T_opthdr);
575 		dstptr = (uint8_t *)ancil_buf;
576 		*dstptr = ipp->ipp_hoplimit;
577 		ancil_buf += sizeof (uint8_t);
578 		ancil_size -= toh->len;
579 	}
580 
581 	/* Consumed all of allocated space */
582 	ASSERT(ancil_size == 0);
583 
584 }
585 
586 /*
587  * This routine retrieves the current status of socket options.
588  * It returns the size of the option retrieved, or -1.
589  */
590 int
591 conn_opt_get(conn_opt_arg_t *coa, t_scalar_t level, t_scalar_t name,
592     uchar_t *ptr)
593 {
594 	int		*i1 = (int *)ptr;
595 	conn_t		*connp = coa->coa_connp;
596 	ip_xmit_attr_t	*ixa = coa->coa_ixa;
597 	ip_pkt_t	*ipp = coa->coa_ipp;
598 	ip_stack_t	*ipst = ixa->ixa_ipst;
599 	uint_t		len;
600 
601 	ASSERT(MUTEX_HELD(&coa->coa_connp->conn_lock));
602 
603 	switch (level) {
604 	case SOL_SOCKET:
605 		switch (name) {
606 		case SO_DEBUG:
607 			*i1 = connp->conn_debug ? SO_DEBUG : 0;
608 			break;	/* goto sizeof (int) option return */
609 		case SO_KEEPALIVE:
610 			*i1 = connp->conn_keepalive ? SO_KEEPALIVE : 0;
611 			break;
612 		case SO_LINGER:	{
613 			struct linger *lgr = (struct linger *)ptr;
614 
615 			lgr->l_onoff = connp->conn_linger ? SO_LINGER : 0;
616 			lgr->l_linger = connp->conn_lingertime;
617 			}
618 			return (sizeof (struct linger));
619 
620 		case SO_OOBINLINE:
621 			*i1 = connp->conn_oobinline ? SO_OOBINLINE : 0;
622 			break;
623 		case SO_REUSEADDR:
624 			*i1 = connp->conn_reuseaddr ? SO_REUSEADDR : 0;
625 			break;	/* goto sizeof (int) option return */
626 		case SO_TYPE:
627 			*i1 = connp->conn_so_type;
628 			break;	/* goto sizeof (int) option return */
629 		case SO_DONTROUTE:
630 			*i1 = (ixa->ixa_flags & IXAF_DONTROUTE) ?
631 			    SO_DONTROUTE : 0;
632 			break;	/* goto sizeof (int) option return */
633 		case SO_USELOOPBACK:
634 			*i1 = connp->conn_useloopback ? SO_USELOOPBACK : 0;
635 			break;	/* goto sizeof (int) option return */
636 		case SO_BROADCAST:
637 			*i1 = connp->conn_broadcast ? SO_BROADCAST : 0;
638 			break;	/* goto sizeof (int) option return */
639 
640 		case SO_SNDBUF:
641 			*i1 = connp->conn_sndbuf;
642 			break;	/* goto sizeof (int) option return */
643 		case SO_RCVBUF:
644 			*i1 = connp->conn_rcvbuf;
645 			break;	/* goto sizeof (int) option return */
646 		case SO_RCVTIMEO:
647 		case SO_SNDTIMEO:
648 			/*
649 			 * Pass these two options in order for third part
650 			 * protocol usage. Here just return directly.
651 			 */
652 			*i1 = 0;
653 			break;
654 		case SO_DGRAM_ERRIND:
655 			*i1 = connp->conn_dgram_errind ? SO_DGRAM_ERRIND : 0;
656 			break;	/* goto sizeof (int) option return */
657 		case SO_RECVUCRED:
658 			*i1 = connp->conn_recv_ancillary.crb_recvucred;
659 			break;	/* goto sizeof (int) option return */
660 		case SO_TIMESTAMP:
661 			*i1 = connp->conn_recv_ancillary.crb_timestamp;
662 			break;	/* goto sizeof (int) option return */
663 		case SO_VRRP:
664 			*i1 = connp->conn_isvrrp;
665 			break;	/* goto sizeof (int) option return */
666 		case SO_ANON_MLP:
667 			*i1 = connp->conn_anon_mlp;
668 			break;	/* goto sizeof (int) option return */
669 		case SO_MAC_EXEMPT:
670 			*i1 = (connp->conn_mac_mode == CONN_MAC_AWARE);
671 			break;	/* goto sizeof (int) option return */
672 		case SO_MAC_IMPLICIT:
673 			*i1 = (connp->conn_mac_mode == CONN_MAC_IMPLICIT);
674 			break;	/* goto sizeof (int) option return */
675 		case SO_ALLZONES:
676 			*i1 = connp->conn_allzones;
677 			break;	/* goto sizeof (int) option return */
678 		case SO_EXCLBIND:
679 			*i1 = connp->conn_exclbind ? SO_EXCLBIND : 0;
680 			break;
681 		case SO_PROTOTYPE:
682 			*i1 = connp->conn_proto;
683 			break;
684 
685 		case SO_DOMAIN:
686 			*i1 = connp->conn_family;
687 			break;
688 		default:
689 			return (-1);
690 		}
691 		break;
692 	case IPPROTO_IP:
693 		if (connp->conn_family != AF_INET)
694 			return (-1);
695 		switch (name) {
696 		case IP_OPTIONS:
697 		case T_IP_OPTIONS:
698 			if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
699 				return (0);
700 
701 			len = ipp->ipp_ipv4_options_len;
702 			if (len > 0) {
703 				bcopy(ipp->ipp_ipv4_options, ptr, len);
704 			}
705 			return (len);
706 
707 		case IP_PKTINFO: {
708 			/*
709 			 * This also handles IP_RECVPKTINFO.
710 			 * IP_PKTINFO and IP_RECVPKTINFO have same value.
711 			 * Differentiation is based on the size of the
712 			 * argument passed in.
713 			 */
714 			struct in_pktinfo *pktinfo;
715 
716 #ifdef notdef
717 			/* optcom doesn't provide a length with "get" */
718 			if (inlen == sizeof (int)) {
719 				/* This is IP_RECVPKTINFO option. */
720 				*i1 = connp->conn_recv_ancillary.
721 				    crb_ip_recvpktinfo;
722 				return (sizeof (int));
723 			}
724 #endif
725 			/* XXX assumes that caller has room for max size! */
726 
727 			pktinfo = (struct in_pktinfo *)ptr;
728 			pktinfo->ipi_ifindex = ixa->ixa_ifindex;
729 			if (ipp->ipp_fields & IPPF_ADDR)
730 				pktinfo->ipi_spec_dst.s_addr = ipp->ipp_addr_v4;
731 			else
732 				pktinfo->ipi_spec_dst.s_addr = INADDR_ANY;
733 			return (sizeof (struct in_pktinfo));
734 		}
735 		case IP_DONTFRAG:
736 			*i1 = (ixa->ixa_flags & IXAF_DONTFRAG) != 0;
737 			return (sizeof (int));
738 		case IP_TOS:
739 		case T_IP_TOS:
740 			*i1 = (int)ipp->ipp_type_of_service;
741 			break;	/* goto sizeof (int) option return */
742 		case IP_TTL:
743 			*i1 = (int)ipp->ipp_unicast_hops;
744 			break;	/* goto sizeof (int) option return */
745 		case IP_DHCPINIT_IF:
746 			return (-1);
747 		case IP_NEXTHOP:
748 			if (ixa->ixa_flags & IXAF_NEXTHOP_SET) {
749 				*(ipaddr_t *)ptr = ixa->ixa_nexthop_v4;
750 				return (sizeof (ipaddr_t));
751 			} else {
752 				return (0);
753 			}
754 
755 		case IP_MULTICAST_IF:
756 			/* 0 address if not set */
757 			*(ipaddr_t *)ptr = ixa->ixa_multicast_ifaddr;
758 			return (sizeof (ipaddr_t));
759 		case IP_MULTICAST_TTL:
760 			*(uchar_t *)ptr = ixa->ixa_multicast_ttl;
761 			return (sizeof (uchar_t));
762 		case IP_MULTICAST_LOOP:
763 			*ptr = (ixa->ixa_flags & IXAF_MULTICAST_LOOP) ? 1 : 0;
764 			return (sizeof (uint8_t));
765 		case IP_RECVOPTS:
766 			*i1 = connp->conn_recv_ancillary.crb_recvopts;
767 			break;	/* goto sizeof (int) option return */
768 		case IP_RECVDSTADDR:
769 			*i1 = connp->conn_recv_ancillary.crb_recvdstaddr;
770 			break;	/* goto sizeof (int) option return */
771 		case IP_RECVIF:
772 			*i1 = connp->conn_recv_ancillary.crb_recvif;
773 			break;	/* goto sizeof (int) option return */
774 		case IP_RECVSLLA:
775 			*i1 = connp->conn_recv_ancillary.crb_recvslla;
776 			break;	/* goto sizeof (int) option return */
777 		case IP_RECVTTL:
778 			*i1 = connp->conn_recv_ancillary.crb_recvttl;
779 			break;	/* goto sizeof (int) option return */
780 		case IP_ADD_MEMBERSHIP:
781 		case IP_DROP_MEMBERSHIP:
782 		case MCAST_JOIN_GROUP:
783 		case MCAST_LEAVE_GROUP:
784 		case IP_BLOCK_SOURCE:
785 		case IP_UNBLOCK_SOURCE:
786 		case IP_ADD_SOURCE_MEMBERSHIP:
787 		case IP_DROP_SOURCE_MEMBERSHIP:
788 		case MCAST_BLOCK_SOURCE:
789 		case MCAST_UNBLOCK_SOURCE:
790 		case MCAST_JOIN_SOURCE_GROUP:
791 		case MCAST_LEAVE_SOURCE_GROUP:
792 		case MRT_INIT:
793 		case MRT_DONE:
794 		case MRT_ADD_VIF:
795 		case MRT_DEL_VIF:
796 		case MRT_ADD_MFC:
797 		case MRT_DEL_MFC:
798 			/* cannot "get" the value for these */
799 			return (-1);
800 		case MRT_VERSION:
801 		case MRT_ASSERT:
802 			(void) ip_mrouter_get(name, connp, ptr);
803 			return (sizeof (int));
804 		case IP_SEC_OPT:
805 			return (ipsec_req_from_conn(connp, (ipsec_req_t	*)ptr,
806 			    IPSEC_AF_V4));
807 		case IP_BOUND_IF:
808 			/* Zero if not set */
809 			*i1 = connp->conn_bound_if;
810 			break;	/* goto sizeof (int) option return */
811 		case IP_UNSPEC_SRC:
812 			*i1 = connp->conn_unspec_src;
813 			break;	/* goto sizeof (int) option return */
814 		case IP_BROADCAST_TTL:
815 			if (ixa->ixa_flags & IXAF_BROADCAST_TTL_SET)
816 				*(uchar_t *)ptr = ixa->ixa_broadcast_ttl;
817 			else
818 				*(uchar_t *)ptr = ipst->ips_ip_broadcast_ttl;
819 			return (sizeof (uchar_t));
820 		default:
821 			return (-1);
822 		}
823 		break;
824 	case IPPROTO_IPV6:
825 		if (connp->conn_family != AF_INET6)
826 			return (-1);
827 		switch (name) {
828 		case IPV6_UNICAST_HOPS:
829 			*i1 = (int)ipp->ipp_unicast_hops;
830 			break;	/* goto sizeof (int) option return */
831 		case IPV6_MULTICAST_IF:
832 			/* 0 index if not set */
833 			*i1 = ixa->ixa_multicast_ifindex;
834 			break;	/* goto sizeof (int) option return */
835 		case IPV6_MULTICAST_HOPS:
836 			*i1 = ixa->ixa_multicast_ttl;
837 			break;	/* goto sizeof (int) option return */
838 		case IPV6_MULTICAST_LOOP:
839 			*i1 = (ixa->ixa_flags & IXAF_MULTICAST_LOOP) ? 1 : 0;
840 			break;	/* goto sizeof (int) option return */
841 		case IPV6_JOIN_GROUP:
842 		case IPV6_LEAVE_GROUP:
843 		case MCAST_JOIN_GROUP:
844 		case MCAST_LEAVE_GROUP:
845 		case MCAST_BLOCK_SOURCE:
846 		case MCAST_UNBLOCK_SOURCE:
847 		case MCAST_JOIN_SOURCE_GROUP:
848 		case MCAST_LEAVE_SOURCE_GROUP:
849 			/* cannot "get" the value for these */
850 			return (-1);
851 		case IPV6_BOUND_IF:
852 			/* Zero if not set */
853 			*i1 = connp->conn_bound_if;
854 			break;	/* goto sizeof (int) option return */
855 		case IPV6_UNSPEC_SRC:
856 			*i1 = connp->conn_unspec_src;
857 			break;	/* goto sizeof (int) option return */
858 		case IPV6_RECVPKTINFO:
859 			*i1 = connp->conn_recv_ancillary.crb_ip_recvpktinfo;
860 			break;	/* goto sizeof (int) option return */
861 		case IPV6_RECVTCLASS:
862 			*i1 = connp->conn_recv_ancillary.crb_ipv6_recvtclass;
863 			break;	/* goto sizeof (int) option return */
864 		case IPV6_RECVPATHMTU:
865 			*i1 = connp->conn_ipv6_recvpathmtu;
866 			break;	/* goto sizeof (int) option return */
867 		case IPV6_RECVHOPLIMIT:
868 			*i1 = connp->conn_recv_ancillary.crb_ipv6_recvhoplimit;
869 			break;	/* goto sizeof (int) option return */
870 		case IPV6_RECVHOPOPTS:
871 			*i1 = connp->conn_recv_ancillary.crb_ipv6_recvhopopts;
872 			break;	/* goto sizeof (int) option return */
873 		case IPV6_RECVDSTOPTS:
874 			*i1 = connp->conn_recv_ancillary.crb_ipv6_recvdstopts;
875 			break;	/* goto sizeof (int) option return */
876 		case _OLD_IPV6_RECVDSTOPTS:
877 			*i1 =
878 			    connp->conn_recv_ancillary.crb_old_ipv6_recvdstopts;
879 			break;	/* goto sizeof (int) option return */
880 		case IPV6_RECVRTHDRDSTOPTS:
881 			*i1 = connp->conn_recv_ancillary.
882 			    crb_ipv6_recvrthdrdstopts;
883 			break;	/* goto sizeof (int) option return */
884 		case IPV6_RECVRTHDR:
885 			*i1 = connp->conn_recv_ancillary.crb_ipv6_recvrthdr;
886 			break;	/* goto sizeof (int) option return */
887 		case IPV6_PKTINFO: {
888 			/* XXX assumes that caller has room for max size! */
889 			struct in6_pktinfo *pkti;
890 
891 			pkti = (struct in6_pktinfo *)ptr;
892 			pkti->ipi6_ifindex = ixa->ixa_ifindex;
893 			if (ipp->ipp_fields & IPPF_ADDR)
894 				pkti->ipi6_addr = ipp->ipp_addr;
895 			else
896 				pkti->ipi6_addr = ipv6_all_zeros;
897 			return (sizeof (struct in6_pktinfo));
898 		}
899 		case IPV6_TCLASS:
900 			*i1 = ipp->ipp_tclass;
901 			break;	/* goto sizeof (int) option return */
902 		case IPV6_NEXTHOP: {
903 			sin6_t *sin6 = (sin6_t *)ptr;
904 
905 			if (ixa->ixa_flags & IXAF_NEXTHOP_SET)
906 				return (0);
907 
908 			*sin6 = sin6_null;
909 			sin6->sin6_family = AF_INET6;
910 			sin6->sin6_addr = ixa->ixa_nexthop_v6;
911 
912 			return (sizeof (sin6_t));
913 		}
914 		case IPV6_HOPOPTS:
915 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
916 				return (0);
917 			bcopy(ipp->ipp_hopopts, ptr,
918 			    ipp->ipp_hopoptslen);
919 			return (ipp->ipp_hopoptslen);
920 		case IPV6_RTHDRDSTOPTS:
921 			if (!(ipp->ipp_fields & IPPF_RTHDRDSTOPTS))
922 				return (0);
923 			bcopy(ipp->ipp_rthdrdstopts, ptr,
924 			    ipp->ipp_rthdrdstoptslen);
925 			return (ipp->ipp_rthdrdstoptslen);
926 		case IPV6_RTHDR:
927 			if (!(ipp->ipp_fields & IPPF_RTHDR))
928 				return (0);
929 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
930 			return (ipp->ipp_rthdrlen);
931 		case IPV6_DSTOPTS:
932 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
933 				return (0);
934 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
935 			return (ipp->ipp_dstoptslen);
936 		case IPV6_PATHMTU:
937 			return (ip_fill_mtuinfo(connp, ixa,
938 			    (struct ip6_mtuinfo *)ptr));
939 		case IPV6_SEC_OPT:
940 			return (ipsec_req_from_conn(connp, (ipsec_req_t	*)ptr,
941 			    IPSEC_AF_V6));
942 		case IPV6_SRC_PREFERENCES:
943 			return (ip6_get_src_preferences(ixa, (uint32_t *)ptr));
944 		case IPV6_DONTFRAG:
945 			*i1 = (ixa->ixa_flags & IXAF_DONTFRAG) != 0;
946 			return (sizeof (int));
947 		case IPV6_USE_MIN_MTU:
948 			if (ixa->ixa_flags & IXAF_USE_MIN_MTU)
949 				*i1 = ixa->ixa_use_min_mtu;
950 			else
951 				*i1 = IPV6_USE_MIN_MTU_MULTICAST;
952 			break;
953 		case IPV6_V6ONLY:
954 			*i1 = connp->conn_ipv6_v6only;
955 			return (sizeof (int));
956 		default:
957 			return (-1);
958 		}
959 		break;
960 	case IPPROTO_UDP:
961 		switch (name) {
962 		case UDP_ANONPRIVBIND:
963 			*i1 = connp->conn_anon_priv_bind;
964 			break;
965 		case UDP_EXCLBIND:
966 			*i1 = connp->conn_exclbind ? UDP_EXCLBIND : 0;
967 			break;
968 		default:
969 			return (-1);
970 		}
971 		break;
972 	case IPPROTO_TCP:
973 		switch (name) {
974 		case TCP_RECVDSTADDR:
975 			*i1 = connp->conn_recv_ancillary.crb_recvdstaddr;
976 			break;
977 		case TCP_ANONPRIVBIND:
978 			*i1 = connp->conn_anon_priv_bind;
979 			break;
980 		case TCP_EXCLBIND:
981 			*i1 = connp->conn_exclbind ? TCP_EXCLBIND : 0;
982 			break;
983 		default:
984 			return (-1);
985 		}
986 		break;
987 	default:
988 		return (-1);
989 	}
990 	return (sizeof (int));
991 }
992 
993 static int conn_opt_set_socket(conn_opt_arg_t *coa, t_scalar_t name,
994     uint_t inlen, uchar_t *invalp, boolean_t checkonly, cred_t *cr);
995 static int conn_opt_set_ip(conn_opt_arg_t *coa, t_scalar_t name,
996     uint_t inlen, uchar_t *invalp, boolean_t checkonly, cred_t *cr);
997 static int conn_opt_set_ipv6(conn_opt_arg_t *coa, t_scalar_t name,
998     uint_t inlen, uchar_t *invalp, boolean_t checkonly, cred_t *cr);
999 static int conn_opt_set_udp(conn_opt_arg_t *coa, t_scalar_t name,
1000     uint_t inlen, uchar_t *invalp, boolean_t checkonly, cred_t *cr);
1001 static int conn_opt_set_tcp(conn_opt_arg_t *coa, t_scalar_t name,
1002     uint_t inlen, uchar_t *invalp, boolean_t checkonly, cred_t *cr);
1003 
1004 /*
1005  * This routine sets the most common socket options including some
1006  * that are transport/ULP specific.
1007  * It returns errno or zero.
1008  *
1009  * For fixed length options, there is no sanity check
1010  * of passed in length is done. It is assumed *_optcom_req()
1011  * routines do the right thing.
1012  */
1013 int
1014 conn_opt_set(conn_opt_arg_t *coa, t_scalar_t level, t_scalar_t name,
1015     uint_t inlen, uchar_t *invalp, boolean_t checkonly, cred_t *cr)
1016 {
1017 	ASSERT(MUTEX_NOT_HELD(&coa->coa_connp->conn_lock));
1018 
1019 	/* We have different functions for different levels */
1020 	switch (level) {
1021 	case SOL_SOCKET:
1022 		return (conn_opt_set_socket(coa, name, inlen, invalp,
1023 		    checkonly, cr));
1024 	case IPPROTO_IP:
1025 		return (conn_opt_set_ip(coa, name, inlen, invalp,
1026 		    checkonly, cr));
1027 	case IPPROTO_IPV6:
1028 		return (conn_opt_set_ipv6(coa, name, inlen, invalp,
1029 		    checkonly, cr));
1030 	case IPPROTO_UDP:
1031 		return (conn_opt_set_udp(coa, name, inlen, invalp,
1032 		    checkonly, cr));
1033 	case IPPROTO_TCP:
1034 		return (conn_opt_set_tcp(coa, name, inlen, invalp,
1035 		    checkonly, cr));
1036 	default:
1037 		return (0);
1038 	}
1039 }
1040 
1041 /*
1042  * Handle SOL_SOCKET
1043  * Note that we do not handle SO_PROTOTYPE here. The ULPs that support
1044  * it implement their own checks and setting of conn_proto.
1045  */
1046 /* ARGSUSED1 */
1047 static int
1048 conn_opt_set_socket(conn_opt_arg_t *coa, t_scalar_t name, uint_t inlen,
1049     uchar_t *invalp, boolean_t checkonly, cred_t *cr)
1050 {
1051 	conn_t		*connp = coa->coa_connp;
1052 	ip_xmit_attr_t	*ixa = coa->coa_ixa;
1053 	int		*i1 = (int *)invalp;
1054 	boolean_t	onoff = (*i1 == 0) ? 0 : 1;
1055 
1056 	switch (name) {
1057 	case SO_ALLZONES:
1058 		if (IPCL_IS_BOUND(connp))
1059 			return (EINVAL);
1060 		break;
1061 	case SO_VRRP:
1062 		if (secpolicy_ip_config(cr, checkonly) != 0)
1063 			return (EACCES);
1064 		break;
1065 	case SO_MAC_EXEMPT:
1066 		if (secpolicy_net_mac_aware(cr) != 0)
1067 			return (EACCES);
1068 		if (IPCL_IS_BOUND(connp))
1069 			return (EINVAL);
1070 		break;
1071 	case SO_MAC_IMPLICIT:
1072 		if (secpolicy_net_mac_implicit(cr) != 0)
1073 			return (EACCES);
1074 		break;
1075 	}
1076 	if (checkonly)
1077 		return (0);
1078 
1079 	mutex_enter(&connp->conn_lock);
1080 	/* Here we set the actual option value */
1081 	switch (name) {
1082 	case SO_DEBUG:
1083 		connp->conn_debug = onoff;
1084 		break;
1085 	case SO_KEEPALIVE:
1086 		connp->conn_keepalive = onoff;
1087 		break;
1088 	case SO_LINGER: {
1089 		struct linger *lgr = (struct linger *)invalp;
1090 
1091 		if (lgr->l_onoff) {
1092 			connp->conn_linger = 1;
1093 			connp->conn_lingertime = lgr->l_linger;
1094 		} else {
1095 			connp->conn_linger = 0;
1096 			connp->conn_lingertime = 0;
1097 		}
1098 		break;
1099 	}
1100 	case SO_OOBINLINE:
1101 		connp->conn_oobinline = onoff;
1102 		coa->coa_changed |= COA_OOBINLINE_CHANGED;
1103 		break;
1104 	case SO_REUSEADDR:
1105 		connp->conn_reuseaddr = onoff;
1106 		break;
1107 	case SO_DONTROUTE:
1108 		if (onoff)
1109 			ixa->ixa_flags |= IXAF_DONTROUTE;
1110 		else
1111 			ixa->ixa_flags &= ~IXAF_DONTROUTE;
1112 		coa->coa_changed |= COA_ROUTE_CHANGED;
1113 		break;
1114 	case SO_USELOOPBACK:
1115 		connp->conn_useloopback = onoff;
1116 		break;
1117 	case SO_BROADCAST:
1118 		connp->conn_broadcast = onoff;
1119 		break;
1120 	case SO_SNDBUF:
1121 		/* ULP has range checked the value */
1122 		connp->conn_sndbuf = *i1;
1123 		coa->coa_changed |= COA_SNDBUF_CHANGED;
1124 		break;
1125 	case SO_RCVBUF:
1126 		/* ULP has range checked the value */
1127 		connp->conn_rcvbuf = *i1;
1128 		coa->coa_changed |= COA_RCVBUF_CHANGED;
1129 		break;
1130 	case SO_RCVTIMEO:
1131 	case SO_SNDTIMEO:
1132 		/*
1133 		 * Pass these two options in order for third part
1134 		 * protocol usage.
1135 		 */
1136 		break;
1137 	case SO_DGRAM_ERRIND:
1138 		connp->conn_dgram_errind = onoff;
1139 		break;
1140 	case SO_RECVUCRED:
1141 		connp->conn_recv_ancillary.crb_recvucred = onoff;
1142 		break;
1143 	case SO_ALLZONES:
1144 		connp->conn_allzones = onoff;
1145 		coa->coa_changed |= COA_ROUTE_CHANGED;
1146 		if (onoff)
1147 			ixa->ixa_zoneid = ALL_ZONES;
1148 		else
1149 			ixa->ixa_zoneid = connp->conn_zoneid;
1150 		break;
1151 	case SO_TIMESTAMP:
1152 		connp->conn_recv_ancillary.crb_timestamp = onoff;
1153 		break;
1154 	case SO_VRRP:
1155 		connp->conn_isvrrp = onoff;
1156 		break;
1157 	case SO_ANON_MLP:
1158 		connp->conn_anon_mlp = onoff;
1159 		break;
1160 	case SO_MAC_EXEMPT:
1161 		connp->conn_mac_mode = onoff ?
1162 		    CONN_MAC_AWARE : CONN_MAC_DEFAULT;
1163 		break;
1164 	case SO_MAC_IMPLICIT:
1165 		connp->conn_mac_mode = onoff ?
1166 		    CONN_MAC_IMPLICIT : CONN_MAC_DEFAULT;
1167 		break;
1168 	case SO_EXCLBIND:
1169 		connp->conn_exclbind = onoff;
1170 		break;
1171 	}
1172 	mutex_exit(&connp->conn_lock);
1173 	return (0);
1174 }
1175 
1176 /* Handle IPPROTO_IP */
1177 static int
1178 conn_opt_set_ip(conn_opt_arg_t *coa, t_scalar_t name, uint_t inlen,
1179     uchar_t *invalp, boolean_t checkonly, cred_t *cr)
1180 {
1181 	conn_t		*connp = coa->coa_connp;
1182 	ip_xmit_attr_t	*ixa = coa->coa_ixa;
1183 	ip_pkt_t	*ipp = coa->coa_ipp;
1184 	int		*i1 = (int *)invalp;
1185 	boolean_t	onoff = (*i1 == 0) ? 0 : 1;
1186 	ipaddr_t	addr = (ipaddr_t)*i1;
1187 	uint_t		ifindex;
1188 	zoneid_t	zoneid = IPCL_ZONEID(connp);
1189 	ipif_t		*ipif;
1190 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
1191 	int		error;
1192 
1193 	if (connp->conn_family != AF_INET)
1194 		return (EINVAL);
1195 
1196 	switch (name) {
1197 	case IP_TTL:
1198 		/* Don't allow zero */
1199 		if (*i1 < 1 || *i1 > 255)
1200 			return (EINVAL);
1201 		break;
1202 	case IP_MULTICAST_IF:
1203 		if (addr == INADDR_ANY) {
1204 			/* Clear */
1205 			ifindex = 0;
1206 			break;
1207 		}
1208 		ipif = ipif_lookup_addr(addr, NULL, zoneid, ipst);
1209 		if (ipif == NULL)
1210 			return (EHOSTUNREACH);
1211 		/* not supported by the virtual network iface */
1212 		if (IS_VNI(ipif->ipif_ill)) {
1213 			ipif_refrele(ipif);
1214 			return (EINVAL);
1215 		}
1216 		ifindex = ipif->ipif_ill->ill_phyint->phyint_ifindex;
1217 		ipif_refrele(ipif);
1218 		break;
1219 	case IP_NEXTHOP: {
1220 		ire_t	*ire;
1221 
1222 		if (addr == INADDR_ANY) {
1223 			/* Clear */
1224 			break;
1225 		}
1226 		/* Verify that the next-hop is on-link */
1227 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_ONLINK, NULL, zoneid,
1228 		    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
1229 		if (ire == NULL)
1230 			return (EHOSTUNREACH);
1231 		ire_refrele(ire);
1232 		break;
1233 	}
1234 	case IP_OPTIONS:
1235 	case T_IP_OPTIONS: {
1236 		uint_t newlen;
1237 
1238 		if (ipp->ipp_fields & IPPF_LABEL_V4)
1239 			newlen = inlen + (ipp->ipp_label_len_v4 + 3) & ~3;
1240 		else
1241 			newlen = inlen;
1242 		if ((inlen & 0x3) || newlen > IP_MAX_OPT_LENGTH) {
1243 			return (EINVAL);
1244 		}
1245 		break;
1246 	}
1247 	case IP_PKTINFO: {
1248 		struct in_pktinfo *pktinfo;
1249 
1250 		/* Two different valid lengths */
1251 		if (inlen != sizeof (int) &&
1252 		    inlen != sizeof (struct in_pktinfo))
1253 			return (EINVAL);
1254 		if (inlen == sizeof (int))
1255 			break;
1256 
1257 		pktinfo = (struct in_pktinfo *)invalp;
1258 		if (pktinfo->ipi_spec_dst.s_addr != INADDR_ANY) {
1259 			switch (ip_laddr_verify_v4(pktinfo->ipi_spec_dst.s_addr,
1260 			    zoneid, ipst, B_FALSE)) {
1261 			case IPVL_UNICAST_UP:
1262 			case IPVL_UNICAST_DOWN:
1263 				break;
1264 			default:
1265 				return (EADDRNOTAVAIL);
1266 			}
1267 		}
1268 		if (!ip_ifindex_valid(pktinfo->ipi_ifindex, B_FALSE, ipst))
1269 			return (ENXIO);
1270 		break;
1271 	}
1272 	case IP_BOUND_IF:
1273 		ifindex = *(uint_t *)i1;
1274 
1275 		/* Just check it is ok. */
1276 		if (!ip_ifindex_valid(ifindex, B_FALSE, ipst))
1277 			return (ENXIO);
1278 		break;
1279 	}
1280 	if (checkonly)
1281 		return (0);
1282 
1283 	/* Here we set the actual option value */
1284 	/*
1285 	 * conn_lock protects the bitfields, and is used to
1286 	 * set the fields atomically. Not needed for ixa settings since
1287 	 * the caller has an exclusive copy of the ixa.
1288 	 * We can not hold conn_lock across the multicast options though.
1289 	 */
1290 	switch (name) {
1291 	case IP_OPTIONS:
1292 	case T_IP_OPTIONS:
1293 		/* Save options for use by IP. */
1294 		mutex_enter(&connp->conn_lock);
1295 		error = optcom_pkt_set(invalp, inlen,
1296 		    (uchar_t **)&ipp->ipp_ipv4_options,
1297 		    &ipp->ipp_ipv4_options_len);
1298 		if (error != 0) {
1299 			mutex_exit(&connp->conn_lock);
1300 			return (error);
1301 		}
1302 		if (ipp->ipp_ipv4_options_len == 0) {
1303 			ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
1304 		} else {
1305 			ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
1306 		}
1307 		mutex_exit(&connp->conn_lock);
1308 		coa->coa_changed |= COA_HEADER_CHANGED;
1309 		coa->coa_changed |= COA_WROFF_CHANGED;
1310 		break;
1311 
1312 	case IP_TTL:
1313 		mutex_enter(&connp->conn_lock);
1314 		ipp->ipp_unicast_hops = *i1;
1315 		mutex_exit(&connp->conn_lock);
1316 		coa->coa_changed |= COA_HEADER_CHANGED;
1317 		break;
1318 	case IP_TOS:
1319 	case T_IP_TOS:
1320 		mutex_enter(&connp->conn_lock);
1321 		if (*i1 == -1) {
1322 			ipp->ipp_type_of_service = 0;
1323 		} else {
1324 			ipp->ipp_type_of_service = *i1;
1325 		}
1326 		mutex_exit(&connp->conn_lock);
1327 		coa->coa_changed |= COA_HEADER_CHANGED;
1328 		break;
1329 	case IP_MULTICAST_IF:
1330 		ixa->ixa_multicast_ifindex = ifindex;
1331 		ixa->ixa_multicast_ifaddr = addr;
1332 		coa->coa_changed |= COA_ROUTE_CHANGED;
1333 		break;
1334 	case IP_MULTICAST_TTL:
1335 		ixa->ixa_multicast_ttl = *invalp;
1336 		/* Handled automatically by ip_output */
1337 		break;
1338 	case IP_MULTICAST_LOOP:
1339 		if (*invalp != 0)
1340 			ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
1341 		else
1342 			ixa->ixa_flags &= ~IXAF_MULTICAST_LOOP;
1343 		/* Handled automatically by ip_output */
1344 		break;
1345 	case IP_RECVOPTS:
1346 		mutex_enter(&connp->conn_lock);
1347 		connp->conn_recv_ancillary.crb_recvopts = onoff;
1348 		mutex_exit(&connp->conn_lock);
1349 		break;
1350 	case IP_RECVDSTADDR:
1351 		mutex_enter(&connp->conn_lock);
1352 		connp->conn_recv_ancillary.crb_recvdstaddr = onoff;
1353 		mutex_exit(&connp->conn_lock);
1354 		break;
1355 	case IP_RECVIF:
1356 		mutex_enter(&connp->conn_lock);
1357 		connp->conn_recv_ancillary.crb_recvif = onoff;
1358 		mutex_exit(&connp->conn_lock);
1359 		break;
1360 	case IP_RECVSLLA:
1361 		mutex_enter(&connp->conn_lock);
1362 		connp->conn_recv_ancillary.crb_recvslla = onoff;
1363 		mutex_exit(&connp->conn_lock);
1364 		break;
1365 	case IP_RECVTTL:
1366 		mutex_enter(&connp->conn_lock);
1367 		connp->conn_recv_ancillary.crb_recvttl = onoff;
1368 		mutex_exit(&connp->conn_lock);
1369 		break;
1370 	case IP_PKTINFO: {
1371 		/*
1372 		 * This also handles IP_RECVPKTINFO.
1373 		 * IP_PKTINFO and IP_RECVPKTINFO have same value.
1374 		 * Differentiation is based on the size of the
1375 		 * argument passed in.
1376 		 */
1377 		struct in_pktinfo *pktinfo;
1378 
1379 		if (inlen == sizeof (int)) {
1380 			/* This is IP_RECVPKTINFO option. */
1381 			mutex_enter(&connp->conn_lock);
1382 			connp->conn_recv_ancillary.crb_ip_recvpktinfo =
1383 			    onoff;
1384 			mutex_exit(&connp->conn_lock);
1385 			break;
1386 		}
1387 
1388 		/* This is IP_PKTINFO option. */
1389 		mutex_enter(&connp->conn_lock);
1390 		pktinfo = (struct in_pktinfo *)invalp;
1391 		if (ipp->ipp_addr_v4 != INADDR_ANY) {
1392 			ipp->ipp_fields |= IPPF_ADDR;
1393 			IN6_INADDR_TO_V4MAPPED(&pktinfo->ipi_spec_dst,
1394 			    &ipp->ipp_addr);
1395 		} else {
1396 			ipp->ipp_fields &= ~IPPF_ADDR;
1397 			ipp->ipp_addr = ipv6_all_zeros;
1398 		}
1399 		mutex_exit(&connp->conn_lock);
1400 		ixa->ixa_ifindex = pktinfo->ipi_ifindex;
1401 		coa->coa_changed |= COA_ROUTE_CHANGED;
1402 		coa->coa_changed |= COA_HEADER_CHANGED;
1403 		break;
1404 	}
1405 	case IP_DONTFRAG:
1406 		if (onoff) {
1407 			ixa->ixa_flags |= (IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
1408 			ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
1409 		} else {
1410 			ixa->ixa_flags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
1411 			ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
1412 		}
1413 		/* Need to redo ip_attr_connect */
1414 		coa->coa_changed |= COA_ROUTE_CHANGED;
1415 		break;
1416 	case IP_ADD_MEMBERSHIP:
1417 	case IP_DROP_MEMBERSHIP:
1418 	case MCAST_JOIN_GROUP:
1419 	case MCAST_LEAVE_GROUP:
1420 		return (ip_opt_set_multicast_group(connp, name,
1421 		    invalp, B_FALSE, checkonly));
1422 
1423 	case IP_BLOCK_SOURCE:
1424 	case IP_UNBLOCK_SOURCE:
1425 	case IP_ADD_SOURCE_MEMBERSHIP:
1426 	case IP_DROP_SOURCE_MEMBERSHIP:
1427 	case MCAST_BLOCK_SOURCE:
1428 	case MCAST_UNBLOCK_SOURCE:
1429 	case MCAST_JOIN_SOURCE_GROUP:
1430 	case MCAST_LEAVE_SOURCE_GROUP:
1431 		return (ip_opt_set_multicast_sources(connp, name,
1432 		    invalp, B_FALSE, checkonly));
1433 
1434 	case IP_SEC_OPT:
1435 		mutex_enter(&connp->conn_lock);
1436 		error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp);
1437 		mutex_exit(&connp->conn_lock);
1438 		if (error != 0) {
1439 			return (error);
1440 		}
1441 		/* This is an IPsec policy change - redo ip_attr_connect */
1442 		coa->coa_changed |= COA_ROUTE_CHANGED;
1443 		break;
1444 	case IP_NEXTHOP:
1445 		ixa->ixa_nexthop_v4 = addr;
1446 		if (addr != INADDR_ANY)
1447 			ixa->ixa_flags |= IXAF_NEXTHOP_SET;
1448 		else
1449 			ixa->ixa_flags &= ~IXAF_NEXTHOP_SET;
1450 		coa->coa_changed |= COA_ROUTE_CHANGED;
1451 		break;
1452 
1453 	case IP_BOUND_IF:
1454 		ixa->ixa_ifindex = ifindex;		/* Send */
1455 		mutex_enter(&connp->conn_lock);
1456 		connp->conn_incoming_ifindex = ifindex;	/* Receive */
1457 		connp->conn_bound_if = ifindex;		/* getsockopt */
1458 		mutex_exit(&connp->conn_lock);
1459 		coa->coa_changed |= COA_ROUTE_CHANGED;
1460 		break;
1461 	case IP_UNSPEC_SRC:
1462 		mutex_enter(&connp->conn_lock);
1463 		connp->conn_unspec_src = onoff;
1464 		if (onoff)
1465 			ixa->ixa_flags &= ~IXAF_VERIFY_SOURCE;
1466 		else
1467 			ixa->ixa_flags |= IXAF_VERIFY_SOURCE;
1468 
1469 		mutex_exit(&connp->conn_lock);
1470 		break;
1471 	case IP_BROADCAST_TTL:
1472 		ixa->ixa_broadcast_ttl = *invalp;
1473 		ixa->ixa_flags |= IXAF_BROADCAST_TTL_SET;
1474 		/* Handled automatically by ip_output */
1475 		break;
1476 	case MRT_INIT:
1477 	case MRT_DONE:
1478 	case MRT_ADD_VIF:
1479 	case MRT_DEL_VIF:
1480 	case MRT_ADD_MFC:
1481 	case MRT_DEL_MFC:
1482 	case MRT_ASSERT:
1483 		if ((error = secpolicy_ip_config(cr, B_FALSE)) != 0) {
1484 			return (error);
1485 		}
1486 		error = ip_mrouter_set((int)name, connp, checkonly,
1487 		    (uchar_t *)invalp, inlen);
1488 		if (error) {
1489 			return (error);
1490 		}
1491 		return (0);
1492 
1493 	}
1494 	return (0);
1495 }
1496 
1497 /* Handle IPPROTO_IPV6 */
1498 static int
1499 conn_opt_set_ipv6(conn_opt_arg_t *coa, t_scalar_t name, uint_t inlen,
1500     uchar_t *invalp, boolean_t checkonly, cred_t *cr)
1501 {
1502 	conn_t		*connp = coa->coa_connp;
1503 	ip_xmit_attr_t	*ixa = coa->coa_ixa;
1504 	ip_pkt_t	*ipp = coa->coa_ipp;
1505 	int		*i1 = (int *)invalp;
1506 	boolean_t	onoff = (*i1 == 0) ? 0 : 1;
1507 	uint_t		ifindex;
1508 	zoneid_t	zoneid = IPCL_ZONEID(connp);
1509 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
1510 	int		error;
1511 
1512 	if (connp->conn_family != AF_INET6)
1513 		return (EINVAL);
1514 
1515 	switch (name) {
1516 	case IPV6_MULTICAST_IF:
1517 		/*
1518 		 * The only possible error is EINVAL.
1519 		 * We call this option on both V4 and V6
1520 		 * If both fail, then this call returns
1521 		 * EINVAL. If at least one of them succeeds we
1522 		 * return success.
1523 		 */
1524 		ifindex = *(uint_t *)i1;
1525 
1526 		if (!ip_ifindex_valid(ifindex, B_TRUE, ipst) &&
1527 		    !ip_ifindex_valid(ifindex, B_FALSE, ipst))
1528 			return (EINVAL);
1529 		break;
1530 	case IPV6_UNICAST_HOPS:
1531 		/* Don't allow zero. -1 means to use default */
1532 		if (*i1 < -1 || *i1 == 0 || *i1 > IPV6_MAX_HOPS)
1533 			return (EINVAL);
1534 		break;
1535 	case IPV6_MULTICAST_HOPS:
1536 		/* -1 means use default */
1537 		if (*i1 < -1 || *i1 > IPV6_MAX_HOPS)
1538 			return (EINVAL);
1539 		break;
1540 	case IPV6_MULTICAST_LOOP:
1541 		if (*i1 != 0 && *i1 != 1)
1542 			return (EINVAL);
1543 		break;
1544 	case IPV6_BOUND_IF:
1545 		ifindex = *(uint_t *)i1;
1546 
1547 		if (!ip_ifindex_valid(ifindex, B_TRUE, ipst))
1548 			return (ENXIO);
1549 		break;
1550 	case IPV6_PKTINFO: {
1551 		struct in6_pktinfo *pkti;
1552 		boolean_t isv6;
1553 
1554 		if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
1555 			return (EINVAL);
1556 		if (inlen == 0)
1557 			break;	/* Clear values below */
1558 
1559 		/*
1560 		 * Verify the source address and ifindex. Privileged users
1561 		 * can use any source address.
1562 		 */
1563 		pkti = (struct in6_pktinfo *)invalp;
1564 
1565 		/*
1566 		 * For link-local addresses we use the ipi6_ifindex when
1567 		 * we verify the local address.
1568 		 * If net_rawaccess then any source address can be used.
1569 		 */
1570 		if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr) &&
1571 		    secpolicy_net_rawaccess(cr) != 0) {
1572 			uint_t scopeid = 0;
1573 			in6_addr_t *v6src = &pkti->ipi6_addr;
1574 			ipaddr_t v4src;
1575 			ip_laddr_t laddr_type = IPVL_UNICAST_UP;
1576 
1577 			if (IN6_IS_ADDR_V4MAPPED(v6src)) {
1578 				IN6_V4MAPPED_TO_IPADDR(v6src, v4src);
1579 				if (v4src != INADDR_ANY) {
1580 					laddr_type = ip_laddr_verify_v4(v4src,
1581 					    zoneid, ipst, B_FALSE);
1582 				}
1583 			} else {
1584 				if (IN6_IS_ADDR_LINKSCOPE(v6src))
1585 					scopeid = pkti->ipi6_ifindex;
1586 
1587 				laddr_type = ip_laddr_verify_v6(v6src, zoneid,
1588 				    ipst, B_FALSE, scopeid);
1589 			}
1590 			switch (laddr_type) {
1591 			case IPVL_UNICAST_UP:
1592 			case IPVL_UNICAST_DOWN:
1593 				break;
1594 			default:
1595 				return (EADDRNOTAVAIL);
1596 			}
1597 			ixa->ixa_flags |= IXAF_VERIFY_SOURCE;
1598 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) {
1599 			/* Allow any source */
1600 			ixa->ixa_flags &= ~IXAF_VERIFY_SOURCE;
1601 		}
1602 		isv6 = !(IN6_IS_ADDR_V4MAPPED(&pkti->ipi6_addr));
1603 		if (!ip_ifindex_valid(pkti->ipi6_ifindex, isv6, ipst))
1604 			return (ENXIO);
1605 		break;
1606 	}
1607 	case IPV6_HOPLIMIT:
1608 		/* It is only allowed as ancilary data */
1609 		if (!coa->coa_ancillary)
1610 			return (EINVAL);
1611 
1612 		if (inlen != 0 && inlen != sizeof (int))
1613 			return (EINVAL);
1614 		if (inlen == sizeof (int)) {
1615 			if (*i1 > 255 || *i1 < -1 || *i1 == 0)
1616 				return (EINVAL);
1617 		}
1618 		break;
1619 	case IPV6_TCLASS:
1620 		if (inlen != 0 && inlen != sizeof (int))
1621 			return (EINVAL);
1622 		if (inlen == sizeof (int)) {
1623 			if (*i1 > 255 || *i1 < -1)
1624 				return (EINVAL);
1625 		}
1626 		break;
1627 	case IPV6_NEXTHOP:
1628 		if (inlen != 0 && inlen != sizeof (sin6_t))
1629 			return (EINVAL);
1630 		if (inlen == sizeof (sin6_t)) {
1631 			sin6_t *sin6 = (sin6_t *)invalp;
1632 			ire_t	*ire;
1633 
1634 			if (sin6->sin6_family != AF_INET6)
1635 				return (EAFNOSUPPORT);
1636 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
1637 				return (EADDRNOTAVAIL);
1638 
1639 			/* Verify that the next-hop is on-link */
1640 			ire = ire_ftable_lookup_v6(&sin6->sin6_addr,
1641 			    0, 0, IRE_ONLINK, NULL, zoneid,
1642 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
1643 			if (ire == NULL)
1644 				return (EHOSTUNREACH);
1645 			ire_refrele(ire);
1646 			break;
1647 		}
1648 		break;
1649 	case IPV6_RTHDR:
1650 	case IPV6_DSTOPTS:
1651 	case IPV6_RTHDRDSTOPTS:
1652 	case IPV6_HOPOPTS: {
1653 		/* All have the length field in the same place */
1654 		ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
1655 		/*
1656 		 * Sanity checks - minimum size, size a multiple of
1657 		 * eight bytes, and matching size passed in.
1658 		 */
1659 		if (inlen != 0 &&
1660 		    inlen != (8 * (hopts->ip6h_len + 1)))
1661 			return (EINVAL);
1662 		break;
1663 	}
1664 	case IPV6_PATHMTU:
1665 		/* Can't be set */
1666 		return (EINVAL);
1667 
1668 	case IPV6_USE_MIN_MTU:
1669 		if (inlen != sizeof (int))
1670 			return (EINVAL);
1671 		if (*i1 < -1 || *i1 > 1)
1672 			return (EINVAL);
1673 		break;
1674 	case IPV6_SRC_PREFERENCES:
1675 		if (inlen != sizeof (uint32_t))
1676 			return (EINVAL);
1677 		break;
1678 	case IPV6_V6ONLY:
1679 		if (*i1 < 0 || *i1 > 1) {
1680 			return (EINVAL);
1681 		}
1682 		break;
1683 	}
1684 	if (checkonly)
1685 		return (0);
1686 
1687 	/* Here we set the actual option value */
1688 	/*
1689 	 * conn_lock protects the bitfields, and is used to
1690 	 * set the fields atomically. Not needed for ixa settings since
1691 	 * the caller has an exclusive copy of the ixa.
1692 	 * We can not hold conn_lock across the multicast options though.
1693 	 */
1694 	ASSERT(MUTEX_NOT_HELD(&coa->coa_connp->conn_lock));
1695 	switch (name) {
1696 	case IPV6_MULTICAST_IF:
1697 		ixa->ixa_multicast_ifindex = ifindex;
1698 		/* Need to redo ip_attr_connect */
1699 		coa->coa_changed |= COA_ROUTE_CHANGED;
1700 		break;
1701 	case IPV6_UNICAST_HOPS:
1702 		/* -1 means use default */
1703 		mutex_enter(&connp->conn_lock);
1704 		if (*i1 == -1) {
1705 			ipp->ipp_unicast_hops = connp->conn_default_ttl;
1706 		} else {
1707 			ipp->ipp_unicast_hops = (uint8_t)*i1;
1708 		}
1709 		mutex_exit(&connp->conn_lock);
1710 		coa->coa_changed |= COA_HEADER_CHANGED;
1711 		break;
1712 	case IPV6_MULTICAST_HOPS:
1713 		/* -1 means use default */
1714 		if (*i1 == -1) {
1715 			ixa->ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1716 		} else {
1717 			ixa->ixa_multicast_ttl = (uint8_t)*i1;
1718 		}
1719 		/* Handled automatically by ip_output */
1720 		break;
1721 	case IPV6_MULTICAST_LOOP:
1722 		if (*i1 != 0)
1723 			ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
1724 		else
1725 			ixa->ixa_flags &= ~IXAF_MULTICAST_LOOP;
1726 		/* Handled automatically by ip_output */
1727 		break;
1728 	case IPV6_JOIN_GROUP:
1729 	case IPV6_LEAVE_GROUP:
1730 	case MCAST_JOIN_GROUP:
1731 	case MCAST_LEAVE_GROUP:
1732 		return (ip_opt_set_multicast_group(connp, name,
1733 		    invalp, B_TRUE, checkonly));
1734 
1735 	case MCAST_BLOCK_SOURCE:
1736 	case MCAST_UNBLOCK_SOURCE:
1737 	case MCAST_JOIN_SOURCE_GROUP:
1738 	case MCAST_LEAVE_SOURCE_GROUP:
1739 		return (ip_opt_set_multicast_sources(connp, name,
1740 		    invalp, B_TRUE, checkonly));
1741 
1742 	case IPV6_BOUND_IF:
1743 		ixa->ixa_ifindex = ifindex;		/* Send */
1744 		mutex_enter(&connp->conn_lock);
1745 		connp->conn_incoming_ifindex = ifindex;	/* Receive */
1746 		connp->conn_bound_if = ifindex;		/* getsockopt */
1747 		mutex_exit(&connp->conn_lock);
1748 		coa->coa_changed |= COA_ROUTE_CHANGED;
1749 		break;
1750 	case IPV6_UNSPEC_SRC:
1751 		mutex_enter(&connp->conn_lock);
1752 		connp->conn_unspec_src = onoff;
1753 		if (onoff)
1754 			ixa->ixa_flags &= ~IXAF_VERIFY_SOURCE;
1755 		else
1756 			ixa->ixa_flags |= IXAF_VERIFY_SOURCE;
1757 		mutex_exit(&connp->conn_lock);
1758 		break;
1759 	case IPV6_RECVPKTINFO:
1760 		mutex_enter(&connp->conn_lock);
1761 		connp->conn_recv_ancillary.crb_ip_recvpktinfo = onoff;
1762 		mutex_exit(&connp->conn_lock);
1763 		break;
1764 	case IPV6_RECVTCLASS:
1765 		mutex_enter(&connp->conn_lock);
1766 		connp->conn_recv_ancillary.crb_ipv6_recvtclass = onoff;
1767 		mutex_exit(&connp->conn_lock);
1768 		break;
1769 	case IPV6_RECVPATHMTU:
1770 		mutex_enter(&connp->conn_lock);
1771 		connp->conn_ipv6_recvpathmtu = onoff;
1772 		mutex_exit(&connp->conn_lock);
1773 		break;
1774 	case IPV6_RECVHOPLIMIT:
1775 		mutex_enter(&connp->conn_lock);
1776 		connp->conn_recv_ancillary.crb_ipv6_recvhoplimit =
1777 		    onoff;
1778 		mutex_exit(&connp->conn_lock);
1779 		break;
1780 	case IPV6_RECVHOPOPTS:
1781 		mutex_enter(&connp->conn_lock);
1782 		connp->conn_recv_ancillary.crb_ipv6_recvhopopts = onoff;
1783 		mutex_exit(&connp->conn_lock);
1784 		break;
1785 	case IPV6_RECVDSTOPTS:
1786 		mutex_enter(&connp->conn_lock);
1787 		connp->conn_recv_ancillary.crb_ipv6_recvdstopts = onoff;
1788 		mutex_exit(&connp->conn_lock);
1789 		break;
1790 	case _OLD_IPV6_RECVDSTOPTS:
1791 		mutex_enter(&connp->conn_lock);
1792 		connp->conn_recv_ancillary.crb_old_ipv6_recvdstopts =
1793 		    onoff;
1794 		mutex_exit(&connp->conn_lock);
1795 		break;
1796 	case IPV6_RECVRTHDRDSTOPTS:
1797 		mutex_enter(&connp->conn_lock);
1798 		connp->conn_recv_ancillary.crb_ipv6_recvrthdrdstopts =
1799 		    onoff;
1800 		mutex_exit(&connp->conn_lock);
1801 		break;
1802 	case IPV6_RECVRTHDR:
1803 		mutex_enter(&connp->conn_lock);
1804 		connp->conn_recv_ancillary.crb_ipv6_recvrthdr = onoff;
1805 		mutex_exit(&connp->conn_lock);
1806 		break;
1807 	case IPV6_PKTINFO:
1808 		mutex_enter(&connp->conn_lock);
1809 		if (inlen == 0) {
1810 			ipp->ipp_fields &= ~IPPF_ADDR;
1811 			ipp->ipp_addr = ipv6_all_zeros;
1812 			ixa->ixa_ifindex = 0;
1813 		} else {
1814 			struct in6_pktinfo *pkti;
1815 
1816 			pkti = (struct in6_pktinfo *)invalp;
1817 			ipp->ipp_addr = pkti->ipi6_addr;
1818 			if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr))
1819 				ipp->ipp_fields |= IPPF_ADDR;
1820 			else
1821 				ipp->ipp_fields &= ~IPPF_ADDR;
1822 			ixa->ixa_ifindex = pkti->ipi6_ifindex;
1823 		}
1824 		mutex_exit(&connp->conn_lock);
1825 		/* Source and ifindex might have changed */
1826 		coa->coa_changed |= COA_HEADER_CHANGED;
1827 		coa->coa_changed |= COA_ROUTE_CHANGED;
1828 		break;
1829 	case IPV6_HOPLIMIT:
1830 		mutex_enter(&connp->conn_lock);
1831 		if (inlen == 0 || *i1 == -1) {
1832 			/* Revert to default */
1833 			ipp->ipp_fields &= ~IPPF_HOPLIMIT;
1834 			ixa->ixa_flags &= ~IXAF_NO_TTL_CHANGE;
1835 		} else {
1836 			ipp->ipp_hoplimit = *i1;
1837 			ipp->ipp_fields |= IPPF_HOPLIMIT;
1838 			/* Ensure that it sticks for multicast packets */
1839 			ixa->ixa_flags |= IXAF_NO_TTL_CHANGE;
1840 		}
1841 		mutex_exit(&connp->conn_lock);
1842 		coa->coa_changed |= COA_HEADER_CHANGED;
1843 		break;
1844 	case IPV6_TCLASS:
1845 		/*
1846 		 * IPV6_TCLASS accepts -1 as use kernel default
1847 		 * and [0, 255] as the actualy traffic class.
1848 		 */
1849 		mutex_enter(&connp->conn_lock);
1850 		if (inlen == 0 || *i1 == -1) {
1851 			ipp->ipp_tclass = 0;
1852 			ipp->ipp_fields &= ~IPPF_TCLASS;
1853 		} else {
1854 			ipp->ipp_tclass = *i1;
1855 			ipp->ipp_fields |= IPPF_TCLASS;
1856 		}
1857 		mutex_exit(&connp->conn_lock);
1858 		coa->coa_changed |= COA_HEADER_CHANGED;
1859 		break;
1860 	case IPV6_NEXTHOP:
1861 		if (inlen == 0) {
1862 			ixa->ixa_flags &= ~IXAF_NEXTHOP_SET;
1863 		} else {
1864 			sin6_t *sin6 = (sin6_t *)invalp;
1865 
1866 			ixa->ixa_nexthop_v6 = sin6->sin6_addr;
1867 			if (!IN6_IS_ADDR_UNSPECIFIED(&ixa->ixa_nexthop_v6))
1868 				ixa->ixa_flags |= IXAF_NEXTHOP_SET;
1869 			else
1870 				ixa->ixa_flags &= ~IXAF_NEXTHOP_SET;
1871 		}
1872 		coa->coa_changed |= COA_ROUTE_CHANGED;
1873 		break;
1874 	case IPV6_HOPOPTS:
1875 		mutex_enter(&connp->conn_lock);
1876 		error = optcom_pkt_set(invalp, inlen,
1877 		    (uchar_t **)&ipp->ipp_hopopts, &ipp->ipp_hopoptslen);
1878 		if (error != 0) {
1879 			mutex_exit(&connp->conn_lock);
1880 			return (error);
1881 		}
1882 		if (ipp->ipp_hopoptslen == 0) {
1883 			ipp->ipp_fields &= ~IPPF_HOPOPTS;
1884 		} else {
1885 			ipp->ipp_fields |= IPPF_HOPOPTS;
1886 		}
1887 		mutex_exit(&connp->conn_lock);
1888 		coa->coa_changed |= COA_HEADER_CHANGED;
1889 		coa->coa_changed |= COA_WROFF_CHANGED;
1890 		break;
1891 	case IPV6_RTHDRDSTOPTS:
1892 		mutex_enter(&connp->conn_lock);
1893 		error = optcom_pkt_set(invalp, inlen,
1894 		    (uchar_t **)&ipp->ipp_rthdrdstopts,
1895 		    &ipp->ipp_rthdrdstoptslen);
1896 		if (error != 0) {
1897 			mutex_exit(&connp->conn_lock);
1898 			return (error);
1899 		}
1900 		if (ipp->ipp_rthdrdstoptslen == 0) {
1901 			ipp->ipp_fields &= ~IPPF_RTHDRDSTOPTS;
1902 		} else {
1903 			ipp->ipp_fields |= IPPF_RTHDRDSTOPTS;
1904 		}
1905 		mutex_exit(&connp->conn_lock);
1906 		coa->coa_changed |= COA_HEADER_CHANGED;
1907 		coa->coa_changed |= COA_WROFF_CHANGED;
1908 		break;
1909 	case IPV6_DSTOPTS:
1910 		mutex_enter(&connp->conn_lock);
1911 		error = optcom_pkt_set(invalp, inlen,
1912 		    (uchar_t **)&ipp->ipp_dstopts, &ipp->ipp_dstoptslen);
1913 		if (error != 0) {
1914 			mutex_exit(&connp->conn_lock);
1915 			return (error);
1916 		}
1917 		if (ipp->ipp_dstoptslen == 0) {
1918 			ipp->ipp_fields &= ~IPPF_DSTOPTS;
1919 		} else {
1920 			ipp->ipp_fields |= IPPF_DSTOPTS;
1921 		}
1922 		mutex_exit(&connp->conn_lock);
1923 		coa->coa_changed |= COA_HEADER_CHANGED;
1924 		coa->coa_changed |= COA_WROFF_CHANGED;
1925 		break;
1926 	case IPV6_RTHDR:
1927 		mutex_enter(&connp->conn_lock);
1928 		error = optcom_pkt_set(invalp, inlen,
1929 		    (uchar_t **)&ipp->ipp_rthdr, &ipp->ipp_rthdrlen);
1930 		if (error != 0) {
1931 			mutex_exit(&connp->conn_lock);
1932 			return (error);
1933 		}
1934 		if (ipp->ipp_rthdrlen == 0) {
1935 			ipp->ipp_fields &= ~IPPF_RTHDR;
1936 		} else {
1937 			ipp->ipp_fields |= IPPF_RTHDR;
1938 		}
1939 		mutex_exit(&connp->conn_lock);
1940 		coa->coa_changed |= COA_HEADER_CHANGED;
1941 		coa->coa_changed |= COA_WROFF_CHANGED;
1942 		break;
1943 
1944 	case IPV6_DONTFRAG:
1945 		if (onoff) {
1946 			ixa->ixa_flags |= IXAF_DONTFRAG;
1947 			ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
1948 		} else {
1949 			ixa->ixa_flags &= ~IXAF_DONTFRAG;
1950 			ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
1951 		}
1952 		/* Need to redo ip_attr_connect */
1953 		coa->coa_changed |= COA_ROUTE_CHANGED;
1954 		break;
1955 
1956 	case IPV6_USE_MIN_MTU:
1957 		ixa->ixa_flags |= IXAF_USE_MIN_MTU;
1958 		ixa->ixa_use_min_mtu = *i1;
1959 		/* Need to redo ip_attr_connect */
1960 		coa->coa_changed |= COA_ROUTE_CHANGED;
1961 		break;
1962 
1963 	case IPV6_SEC_OPT:
1964 		mutex_enter(&connp->conn_lock);
1965 		error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp);
1966 		mutex_exit(&connp->conn_lock);
1967 		if (error != 0) {
1968 			return (error);
1969 		}
1970 		/* This is an IPsec policy change - redo ip_attr_connect */
1971 		coa->coa_changed |= COA_ROUTE_CHANGED;
1972 		break;
1973 	case IPV6_SRC_PREFERENCES:
1974 		/*
1975 		 * This socket option only affects connected
1976 		 * sockets that haven't already bound to a specific
1977 		 * IPv6 address.  In other words, sockets that
1978 		 * don't call bind() with an address other than the
1979 		 * unspecified address and that call connect().
1980 		 * ip_set_destination_v6() passes these preferences
1981 		 * to the ipif_select_source_v6() function.
1982 		 */
1983 		mutex_enter(&connp->conn_lock);
1984 		error = ip6_set_src_preferences(ixa, *(uint32_t *)invalp);
1985 		mutex_exit(&connp->conn_lock);
1986 		if (error != 0) {
1987 			return (error);
1988 		}
1989 		break;
1990 	case IPV6_V6ONLY:
1991 		mutex_enter(&connp->conn_lock);
1992 		connp->conn_ipv6_v6only = onoff;
1993 		mutex_exit(&connp->conn_lock);
1994 		break;
1995 	}
1996 	return (0);
1997 }
1998 
1999 /* Handle IPPROTO_UDP */
2000 /* ARGSUSED1 */
2001 static int
2002 conn_opt_set_udp(conn_opt_arg_t *coa, t_scalar_t name, uint_t inlen,
2003     uchar_t *invalp, boolean_t checkonly, cred_t *cr)
2004 {
2005 	conn_t		*connp = coa->coa_connp;
2006 	int		*i1 = (int *)invalp;
2007 	boolean_t	onoff = (*i1 == 0) ? 0 : 1;
2008 	int		error;
2009 
2010 	switch (name) {
2011 	case UDP_ANONPRIVBIND:
2012 		if ((error = secpolicy_net_privaddr(cr, 0, IPPROTO_UDP)) != 0) {
2013 			return (error);
2014 		}
2015 		break;
2016 	}
2017 	if (checkonly)
2018 		return (0);
2019 
2020 	/* Here we set the actual option value */
2021 	mutex_enter(&connp->conn_lock);
2022 	switch (name) {
2023 	case UDP_ANONPRIVBIND:
2024 		connp->conn_anon_priv_bind = onoff;
2025 		break;
2026 	case UDP_EXCLBIND:
2027 		connp->conn_exclbind = onoff;
2028 		break;
2029 	}
2030 	mutex_exit(&connp->conn_lock);
2031 	return (0);
2032 }
2033 
2034 /* Handle IPPROTO_TCP */
2035 /* ARGSUSED1 */
2036 static int
2037 conn_opt_set_tcp(conn_opt_arg_t *coa, t_scalar_t name, uint_t inlen,
2038     uchar_t *invalp, boolean_t checkonly, cred_t *cr)
2039 {
2040 	conn_t		*connp = coa->coa_connp;
2041 	int		*i1 = (int *)invalp;
2042 	boolean_t	onoff = (*i1 == 0) ? 0 : 1;
2043 	int		error;
2044 
2045 	switch (name) {
2046 	case TCP_ANONPRIVBIND:
2047 		if ((error = secpolicy_net_privaddr(cr, 0, IPPROTO_TCP)) != 0) {
2048 			return (error);
2049 		}
2050 		break;
2051 	}
2052 	if (checkonly)
2053 		return (0);
2054 
2055 	/* Here we set the actual option value */
2056 	mutex_enter(&connp->conn_lock);
2057 	switch (name) {
2058 	case TCP_ANONPRIVBIND:
2059 		connp->conn_anon_priv_bind = onoff;
2060 		break;
2061 	case TCP_EXCLBIND:
2062 		connp->conn_exclbind = onoff;
2063 		break;
2064 	case TCP_RECVDSTADDR:
2065 		connp->conn_recv_ancillary.crb_recvdstaddr = onoff;
2066 		break;
2067 	}
2068 	mutex_exit(&connp->conn_lock);
2069 	return (0);
2070 }
2071 
2072 int
2073 conn_getsockname(conn_t *connp, struct sockaddr *sa, uint_t *salenp)
2074 {
2075 	sin_t		*sin;
2076 	sin6_t		*sin6;
2077 
2078 	if (connp->conn_family == AF_INET) {
2079 		if (*salenp < sizeof (sin_t))
2080 			return (EINVAL);
2081 
2082 		*salenp = sizeof (sin_t);
2083 		/* Fill zeroes and then initialize non-zero fields */
2084 		sin = (sin_t *)sa;
2085 		*sin = sin_null;
2086 		sin->sin_family = AF_INET;
2087 		if (!IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_saddr_v6) &&
2088 		    !IN6_IS_ADDR_UNSPECIFIED(&connp->conn_saddr_v6)) {
2089 			sin->sin_addr.s_addr = connp->conn_saddr_v4;
2090 		} else {
2091 			/*
2092 			 * INADDR_ANY
2093 			 * conn_saddr is not set, we might be bound to
2094 			 * broadcast/multicast. Use conn_bound_addr as
2095 			 * local address instead (that could
2096 			 * also still be INADDR_ANY)
2097 			 */
2098 			sin->sin_addr.s_addr = connp->conn_bound_addr_v4;
2099 		}
2100 		sin->sin_port = connp->conn_lport;
2101 	} else {
2102 		if (*salenp < sizeof (sin6_t))
2103 			return (EINVAL);
2104 
2105 		*salenp = sizeof (sin6_t);
2106 		/* Fill zeroes and then initialize non-zero fields */
2107 		sin6 = (sin6_t *)sa;
2108 		*sin6 = sin6_null;
2109 		sin6->sin6_family = AF_INET6;
2110 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_saddr_v6)) {
2111 			sin6->sin6_addr = connp->conn_saddr_v6;
2112 		} else {
2113 			/*
2114 			 * conn_saddr is not set, we might be bound to
2115 			 * broadcast/multicast. Use conn_bound_addr as
2116 			 * local address instead (which could
2117 			 * also still be unspecified)
2118 			 */
2119 			sin6->sin6_addr = connp->conn_bound_addr_v6;
2120 		}
2121 		sin6->sin6_port = connp->conn_lport;
2122 		if (IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr) &&
2123 		    (connp->conn_ixa->ixa_flags & IXAF_SCOPEID_SET))
2124 			sin6->sin6_scope_id = connp->conn_ixa->ixa_scopeid;
2125 	}
2126 	return (0);
2127 }
2128 
2129 int
2130 conn_getpeername(conn_t *connp, struct sockaddr *sa, uint_t *salenp)
2131 {
2132 	struct sockaddr_in	*sin;
2133 	struct sockaddr_in6	*sin6;
2134 
2135 	if (connp->conn_family == AF_INET) {
2136 		if (*salenp < sizeof (sin_t))
2137 			return (EINVAL);
2138 
2139 		*salenp = sizeof (sin_t);
2140 		/* initialize */
2141 		sin = (sin_t *)sa;
2142 		*sin = sin_null;
2143 		sin->sin_family = AF_INET;
2144 		sin->sin_addr.s_addr = connp->conn_faddr_v4;
2145 		sin->sin_port = connp->conn_fport;
2146 	} else {
2147 		if (*salenp < sizeof (sin6_t))
2148 			return (EINVAL);
2149 
2150 		*salenp = sizeof (sin6_t);
2151 		/* initialize */
2152 		sin6 = (sin6_t *)sa;
2153 		*sin6 = sin6_null;
2154 		sin6->sin6_family = AF_INET6;
2155 		sin6->sin6_addr = connp->conn_faddr_v6;
2156 		sin6->sin6_port =  connp->conn_fport;
2157 		sin6->sin6_flowinfo = connp->conn_flowinfo;
2158 		if (IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr) &&
2159 		    (connp->conn_ixa->ixa_flags & IXAF_SCOPEID_SET))
2160 			sin6->sin6_scope_id = connp->conn_ixa->ixa_scopeid;
2161 	}
2162 	return (0);
2163 }
2164 
2165 static uint32_t	cksum_massage_options_v4(ipha_t *, netstack_t *);
2166 static uint32_t cksum_massage_options_v6(ip6_t *, uint_t, netstack_t *);
2167 
2168 /*
2169  * Allocate and fill in conn_ht_iphc based on the current information
2170  * in the conn.
2171  * Normally used when we bind() and connect().
2172  * Returns failure if can't allocate memory, or if there is a problem
2173  * with a routing header/option.
2174  *
2175  * We allocate space for the transport header (ulp_hdr_len + extra) and
2176  * indicate the offset of the ulp header by setting ixa_ip_hdr_length.
2177  * The extra is there for transports that want some spare room for future
2178  * options. conn_ht_iphc_allocated is what was allocated; conn_ht_iphc_len
2179  * excludes the extra part.
2180  *
2181  * We massage an routing option/header and store the ckecksum difference
2182  * in conn_sum.
2183  *
2184  * Caller needs to update conn_wroff if desired.
2185  */
2186 int
2187 conn_build_hdr_template(conn_t *connp, uint_t ulp_hdr_length, uint_t extra,
2188     const in6_addr_t *v6src, const in6_addr_t *v6dst, uint32_t flowinfo)
2189 {
2190 	ip_xmit_attr_t	*ixa = connp->conn_ixa;
2191 	ip_pkt_t	*ipp = &connp->conn_xmit_ipp;
2192 	uint_t		ip_hdr_length;
2193 	uchar_t		*hdrs;
2194 	uint_t		hdrs_len;
2195 
2196 	ASSERT(MUTEX_HELD(&connp->conn_lock));
2197 
2198 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
2199 		ip_hdr_length = ip_total_hdrs_len_v4(ipp);
2200 		/* In case of TX label and IP options it can be too much */
2201 		if (ip_hdr_length > IP_MAX_HDR_LENGTH) {
2202 			/* Preserves existing TX errno for this */
2203 			return (EHOSTUNREACH);
2204 		}
2205 	} else {
2206 		ip_hdr_length = ip_total_hdrs_len_v6(ipp);
2207 	}
2208 	ixa->ixa_ip_hdr_length = ip_hdr_length;
2209 	hdrs_len = ip_hdr_length + ulp_hdr_length + extra;
2210 	ASSERT(hdrs_len != 0);
2211 
2212 	if (hdrs_len != connp->conn_ht_iphc_allocated) {
2213 		/* Allocate new before we free any old */
2214 		hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
2215 		if (hdrs == NULL)
2216 			return (ENOMEM);
2217 
2218 		if (connp->conn_ht_iphc != NULL) {
2219 			kmem_free(connp->conn_ht_iphc,
2220 			    connp->conn_ht_iphc_allocated);
2221 		}
2222 		connp->conn_ht_iphc = hdrs;
2223 		connp->conn_ht_iphc_allocated = hdrs_len;
2224 	} else {
2225 		hdrs = connp->conn_ht_iphc;
2226 	}
2227 	hdrs_len -= extra;
2228 	connp->conn_ht_iphc_len = hdrs_len;
2229 
2230 	connp->conn_ht_ulp = hdrs + ip_hdr_length;
2231 	connp->conn_ht_ulp_len = ulp_hdr_length;
2232 
2233 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
2234 		ipha_t	*ipha = (ipha_t *)hdrs;
2235 
2236 		IN6_V4MAPPED_TO_IPADDR(v6src, ipha->ipha_src);
2237 		IN6_V4MAPPED_TO_IPADDR(v6dst, ipha->ipha_dst);
2238 		ip_build_hdrs_v4(hdrs, ip_hdr_length, ipp, connp->conn_proto);
2239 		ipha->ipha_length = htons(hdrs_len);
2240 		if (ixa->ixa_flags & IXAF_PMTU_IPV4_DF)
2241 			ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
2242 		else
2243 			ipha->ipha_fragment_offset_and_flags &= ~IPH_DF_HTONS;
2244 
2245 		if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
2246 			connp->conn_sum = cksum_massage_options_v4(ipha,
2247 			    connp->conn_netstack);
2248 		} else {
2249 			connp->conn_sum = 0;
2250 		}
2251 	} else {
2252 		ip6_t	*ip6h = (ip6_t *)hdrs;
2253 
2254 		ip6h->ip6_src = *v6src;
2255 		ip6h->ip6_dst = *v6dst;
2256 		ip_build_hdrs_v6(hdrs, ip_hdr_length, ipp, connp->conn_proto,
2257 		    flowinfo);
2258 		ip6h->ip6_plen = htons(hdrs_len - IPV6_HDR_LEN);
2259 
2260 		if (ipp->ipp_fields & IPPF_RTHDR) {
2261 			connp->conn_sum = cksum_massage_options_v6(ip6h,
2262 			    ip_hdr_length, connp->conn_netstack);
2263 
2264 			/*
2265 			 * Verify that the first hop isn't a mapped address.
2266 			 * Routers along the path need to do this verification
2267 			 * for subsequent hops.
2268 			 */
2269 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst))
2270 				return (EADDRNOTAVAIL);
2271 
2272 		} else {
2273 			connp->conn_sum = 0;
2274 		}
2275 	}
2276 	return (0);
2277 }
2278 
2279 /*
2280  * Prepend a header template to data_mp based on the ip_pkt_t
2281  * and the passed in source, destination and protocol.
2282  *
2283  * Returns failure if can't allocate memory, in which case data_mp is freed.
2284  * We allocate space for the transport header (ulp_hdr_len) and
2285  * indicate the offset of the ulp header by setting ixa_ip_hdr_length.
2286  *
2287  * We massage an routing option/header and return the ckecksum difference
2288  * in *sump. This is in host byte order.
2289  *
2290  * Caller needs to update conn_wroff if desired.
2291  */
2292 mblk_t *
2293 conn_prepend_hdr(ip_xmit_attr_t *ixa, const ip_pkt_t *ipp,
2294     const in6_addr_t *v6src, const in6_addr_t *v6dst,
2295     uint8_t protocol, uint32_t flowinfo, uint_t ulp_hdr_length, mblk_t *data_mp,
2296     uint_t data_length, uint_t wroff_extra, uint32_t *sump, int *errorp)
2297 {
2298 	uint_t		ip_hdr_length;
2299 	uchar_t		*hdrs;
2300 	uint_t		hdrs_len;
2301 	mblk_t		*mp;
2302 
2303 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
2304 		ip_hdr_length = ip_total_hdrs_len_v4(ipp);
2305 		ASSERT(ip_hdr_length <= IP_MAX_HDR_LENGTH);
2306 	} else {
2307 		ip_hdr_length = ip_total_hdrs_len_v6(ipp);
2308 	}
2309 	hdrs_len = ip_hdr_length + ulp_hdr_length;
2310 	ASSERT(hdrs_len != 0);
2311 
2312 	ixa->ixa_ip_hdr_length = ip_hdr_length;
2313 
2314 	/* Can we prepend to data_mp? */
2315 	if (data_mp != NULL &&
2316 	    data_mp->b_rptr - data_mp->b_datap->db_base >= hdrs_len &&
2317 	    data_mp->b_datap->db_ref == 1) {
2318 		hdrs = data_mp->b_rptr - hdrs_len;
2319 		data_mp->b_rptr = hdrs;
2320 		mp = data_mp;
2321 	} else {
2322 		mp = allocb(hdrs_len + wroff_extra, BPRI_MED);
2323 		if (mp == NULL) {
2324 			freemsg(data_mp);
2325 			*errorp = ENOMEM;
2326 			return (NULL);
2327 		}
2328 		mp->b_wptr = mp->b_datap->db_lim;
2329 		hdrs = mp->b_rptr = mp->b_wptr - hdrs_len;
2330 		mp->b_cont = data_mp;
2331 	}
2332 
2333 	/*
2334 	 * Set the source in the header. ip_build_hdrs_v4/v6 will overwrite it
2335 	 * if PKTINFO (aka IPPF_ADDR) was set.
2336 	 */
2337 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
2338 		ipha_t *ipha = (ipha_t *)hdrs;
2339 
2340 		ASSERT(IN6_IS_ADDR_V4MAPPED(v6dst));
2341 		IN6_V4MAPPED_TO_IPADDR(v6src, ipha->ipha_src);
2342 		IN6_V4MAPPED_TO_IPADDR(v6dst, ipha->ipha_dst);
2343 		ip_build_hdrs_v4(hdrs, ip_hdr_length, ipp, protocol);
2344 		ipha->ipha_length = htons(hdrs_len + data_length);
2345 		if (ixa->ixa_flags & IXAF_PMTU_IPV4_DF)
2346 			ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
2347 		else
2348 			ipha->ipha_fragment_offset_and_flags &= ~IPH_DF_HTONS;
2349 
2350 		if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
2351 			*sump = cksum_massage_options_v4(ipha,
2352 			    ixa->ixa_ipst->ips_netstack);
2353 		} else {
2354 			*sump = 0;
2355 		}
2356 	} else {
2357 		ip6_t *ip6h = (ip6_t *)hdrs;
2358 
2359 		ip6h->ip6_src = *v6src;
2360 		ip6h->ip6_dst = *v6dst;
2361 		ip_build_hdrs_v6(hdrs, ip_hdr_length, ipp, protocol, flowinfo);
2362 		ip6h->ip6_plen = htons(hdrs_len + data_length - IPV6_HDR_LEN);
2363 
2364 		if (ipp->ipp_fields & IPPF_RTHDR) {
2365 			*sump = cksum_massage_options_v6(ip6h,
2366 			    ip_hdr_length, ixa->ixa_ipst->ips_netstack);
2367 
2368 			/*
2369 			 * Verify that the first hop isn't a mapped address.
2370 			 * Routers along the path need to do this verification
2371 			 * for subsequent hops.
2372 			 */
2373 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
2374 				*errorp = EADDRNOTAVAIL;
2375 				freemsg(mp);
2376 				return (NULL);
2377 			}
2378 		} else {
2379 			*sump = 0;
2380 		}
2381 	}
2382 	return (mp);
2383 }
2384 
2385 /*
2386  * Massage a source route if any putting the first hop
2387  * in ipha_dst. Compute a starting value for the checksum which
2388  * takes into account that the original ipha_dst should be
2389  * included in the checksum but that IP will include the
2390  * first hop from the source route in the tcp checksum.
2391  */
2392 static uint32_t
2393 cksum_massage_options_v4(ipha_t *ipha, netstack_t *ns)
2394 {
2395 	in_addr_t	dst;
2396 	uint32_t	cksum;
2397 
2398 	/* Get last hop then diff against first hop */
2399 	cksum = ip_massage_options(ipha, ns);
2400 	cksum = (cksum & 0xFFFF) + (cksum >> 16);
2401 	dst = ipha->ipha_dst;
2402 	cksum -= ((dst >> 16) + (dst & 0xffff));
2403 	if ((int)cksum < 0)
2404 		cksum--;
2405 	cksum = (cksum & 0xFFFF) + (cksum >> 16);
2406 	cksum = (cksum & 0xFFFF) + (cksum >> 16);
2407 	ASSERT(cksum < 0x10000);
2408 	return (ntohs(cksum));
2409 }
2410 
2411 static uint32_t
2412 cksum_massage_options_v6(ip6_t *ip6h, uint_t ip_hdr_len, netstack_t *ns)
2413 {
2414 	uint8_t		*end;
2415 	ip6_rthdr_t	*rth;
2416 	uint32_t	cksum;
2417 
2418 	end = (uint8_t *)ip6h + ip_hdr_len;
2419 	rth = ip_find_rthdr_v6(ip6h, end);
2420 	if (rth == NULL)
2421 		return (0);
2422 
2423 	cksum = ip_massage_options_v6(ip6h, rth, ns);
2424 	cksum = (cksum & 0xFFFF) + (cksum >> 16);
2425 	ASSERT(cksum < 0x10000);
2426 	return (ntohs(cksum));
2427 }
2428 
2429 /*
2430  * ULPs that change the destination address need to call this for each
2431  * change to discard any state about a previous destination that might
2432  * have been multicast or multirt.
2433  */
2434 void
2435 ip_attr_newdst(ip_xmit_attr_t *ixa)
2436 {
2437 	ixa->ixa_flags &= ~(IXAF_LOOPBACK_COPY | IXAF_NO_HW_CKSUM |
2438 	    IXAF_NO_TTL_CHANGE | IXAF_IPV6_ADD_FRAGHDR |
2439 	    IXAF_NO_LOOP_ZONEID_SET);
2440 }
2441 
2442 /*
2443  * Determine the nexthop which will be used.
2444  * Normally this is just the destination, but if a IPv4 source route, or
2445  * IPv6 routing header, is in the ip_pkt_t then we extract the nexthop from
2446  * there.
2447  */
2448 void
2449 ip_attr_nexthop(const ip_pkt_t *ipp, const ip_xmit_attr_t *ixa,
2450     const in6_addr_t *dst, in6_addr_t *nexthop)
2451 {
2452 	if (!(ipp->ipp_fields & (IPPF_IPV4_OPTIONS|IPPF_RTHDR))) {
2453 		*nexthop = *dst;
2454 		return;
2455 	}
2456 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
2457 		ipaddr_t v4dst;
2458 		ipaddr_t v4nexthop;
2459 
2460 		IN6_V4MAPPED_TO_IPADDR(dst, v4dst);
2461 		v4nexthop = ip_pkt_source_route_v4(ipp);
2462 		if (v4nexthop == INADDR_ANY)
2463 			v4nexthop = v4dst;
2464 
2465 		IN6_IPADDR_TO_V4MAPPED(v4nexthop, nexthop);
2466 	} else {
2467 		const in6_addr_t *v6nexthop;
2468 
2469 		v6nexthop = ip_pkt_source_route_v6(ipp);
2470 		if (v6nexthop == NULL)
2471 			v6nexthop = dst;
2472 
2473 		*nexthop = *v6nexthop;
2474 	}
2475 }
2476 
2477 /*
2478  * Update the ip_xmit_attr_t based the addresses, conn_xmit_ipp and conn_ixa.
2479  * If IPDF_IPSEC is set we cache the IPsec policy to handle the unconnected
2480  * case (connected latching is done in conn_connect).
2481  * Note that IPsec policy lookup requires conn_proto and conn_laddr to be
2482  * set, but doesn't otherwise use the conn_t.
2483  *
2484  * Caller must set/clear IXAF_IS_IPV4 as appropriately.
2485  * Caller must use ip_attr_nexthop() to determine the nexthop argument.
2486  *
2487  * The caller must NOT hold conn_lock (to avoid problems with ill_refrele
2488  * causing the squeue to run doing ipcl_walk grabbing conn_lock.)
2489  *
2490  * Updates laddrp and uinfo if they are non-NULL.
2491  *
2492  * TSOL notes: The callers if ip_attr_connect must check if the destination
2493  * is different than before and in that case redo conn_update_label.
2494  * The callers of conn_connect do not need that since conn_connect
2495  * performs the conn_update_label.
2496  */
2497 int
2498 ip_attr_connect(const conn_t *connp, ip_xmit_attr_t *ixa,
2499     const in6_addr_t *v6src, const in6_addr_t *v6dst,
2500     const in6_addr_t *v6nexthop, in_port_t dstport, in6_addr_t *laddrp,
2501     iulp_t *uinfo, uint32_t flags)
2502 {
2503 	in6_addr_t		laddr = *v6src;
2504 	int			error;
2505 
2506 	ASSERT(MUTEX_NOT_HELD(&connp->conn_lock));
2507 
2508 	if (connp->conn_zone_is_global)
2509 		flags |= IPDF_ZONE_IS_GLOBAL;
2510 	else
2511 		flags &= ~IPDF_ZONE_IS_GLOBAL;
2512 
2513 	/*
2514 	 * Lookup the route to determine a source address and the uinfo.
2515 	 * If the ULP has a source route option then the caller will
2516 	 * have set v6nexthop to be the first hop.
2517 	 */
2518 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
2519 		ipaddr_t v4dst;
2520 		ipaddr_t v4src, v4nexthop;
2521 
2522 		IN6_V4MAPPED_TO_IPADDR(v6dst, v4dst);
2523 		IN6_V4MAPPED_TO_IPADDR(v6nexthop, v4nexthop);
2524 		IN6_V4MAPPED_TO_IPADDR(v6src, v4src);
2525 
2526 		if (connp->conn_unspec_src || v4src != INADDR_ANY)
2527 			flags &= ~IPDF_SELECT_SRC;
2528 		else
2529 			flags |= IPDF_SELECT_SRC;
2530 
2531 		error = ip_set_destination_v4(&v4src, v4dst, v4nexthop, ixa,
2532 		    uinfo, flags, connp->conn_mac_mode);
2533 		IN6_IPADDR_TO_V4MAPPED(v4src, &laddr);
2534 	} else {
2535 		if (connp->conn_unspec_src || !IN6_IS_ADDR_UNSPECIFIED(v6src))
2536 			flags &= ~IPDF_SELECT_SRC;
2537 		else
2538 			flags |= IPDF_SELECT_SRC;
2539 
2540 		error = ip_set_destination_v6(&laddr, v6dst, v6nexthop, ixa,
2541 		    uinfo, flags, connp->conn_mac_mode);
2542 	}
2543 	/* Pass out some address even if we hit a RTF_REJECT etc */
2544 	if (laddrp != NULL)
2545 		*laddrp = laddr;
2546 
2547 	if (error != 0)
2548 		return (error);
2549 
2550 	if (flags & IPDF_IPSEC) {
2551 		/*
2552 		 * Set any IPsec policy in ixa. Routine also looks at ULP
2553 		 * ports.
2554 		 */
2555 		ipsec_cache_outbound_policy(connp, v6src, v6dst, dstport, ixa);
2556 	}
2557 	return (0);
2558 }
2559 
2560 /*
2561  * Connect the conn based on the addresses, conn_xmit_ipp and conn_ixa.
2562  * Assumes that conn_faddr and conn_fport are already set. As such it is not
2563  * usable for SCTP, since SCTP has multiple faddrs.
2564  *
2565  * Caller must hold conn_lock to provide atomic constency between the
2566  * conn_t's addresses and the ixa.
2567  * NOTE: this function drops and reaquires conn_lock since it can't be
2568  * held across ip_attr_connect/ip_set_destination.
2569  *
2570  * The caller needs to handle inserting in the receive-side fanout when
2571  * appropriate after conn_connect returns.
2572  */
2573 int
2574 conn_connect(conn_t *connp, iulp_t *uinfo, uint32_t flags)
2575 {
2576 	ip_xmit_attr_t	*ixa = connp->conn_ixa;
2577 	in6_addr_t	nexthop;
2578 	in6_addr_t	saddr, faddr;
2579 	in_port_t	fport;
2580 	int		error;
2581 
2582 	ASSERT(MUTEX_HELD(&connp->conn_lock));
2583 
2584 	if (connp->conn_ipversion == IPV4_VERSION)
2585 		ixa->ixa_flags |= IXAF_IS_IPV4;
2586 	else
2587 		ixa->ixa_flags &= ~IXAF_IS_IPV4;
2588 
2589 	/* We do IPsec latching below - hence no caching in ip_attr_connect */
2590 	flags &= ~IPDF_IPSEC;
2591 
2592 	/* In case we had previously done an ip_attr_connect */
2593 	ip_attr_newdst(ixa);
2594 
2595 	/*
2596 	 * Determine the nexthop and copy the addresses before dropping
2597 	 * conn_lock.
2598 	 */
2599 	ip_attr_nexthop(&connp->conn_xmit_ipp, connp->conn_ixa,
2600 	    &connp->conn_faddr_v6, &nexthop);
2601 	saddr = connp->conn_saddr_v6;
2602 	faddr = connp->conn_faddr_v6;
2603 	fport = connp->conn_fport;
2604 
2605 	mutex_exit(&connp->conn_lock);
2606 	error = ip_attr_connect(connp, ixa, &saddr, &faddr, &nexthop, fport,
2607 	    &saddr, uinfo, flags | IPDF_VERIFY_DST);
2608 	mutex_enter(&connp->conn_lock);
2609 
2610 	/* Could have changed even if an error */
2611 	connp->conn_saddr_v6 = saddr;
2612 	if (error != 0)
2613 		return (error);
2614 
2615 	/*
2616 	 * Check whether Trusted Solaris policy allows communication with this
2617 	 * host, and pretend that the destination is unreachable if not.
2618 	 * Compute any needed label and place it in ipp_label_v4/v6.
2619 	 *
2620 	 * Later conn_build_hdr_template() takes ipp_label_v4/v6 to form
2621 	 * the packet.
2622 	 *
2623 	 * TSOL Note: Any concurrent threads would pick a different ixa
2624 	 * (and ipp if they are to change the ipp)  so we
2625 	 * don't have to worry about concurrent threads.
2626 	 */
2627 	if (is_system_labeled()) {
2628 		if (connp->conn_mlp_type != mlptSingle)
2629 			return (ECONNREFUSED);
2630 
2631 		/*
2632 		 * conn_update_label will set ipp_label* which will later
2633 		 * be used by conn_build_hdr_template.
2634 		 */
2635 		error = conn_update_label(connp, ixa,
2636 		    &connp->conn_faddr_v6, &connp->conn_xmit_ipp);
2637 		if (error != 0)
2638 			return (error);
2639 	}
2640 
2641 	/*
2642 	 * Ensure that we match on the selected local address.
2643 	 * This overrides conn_laddr in the case we had earlier bound to a
2644 	 * multicast or broadcast address.
2645 	 */
2646 	connp->conn_laddr_v6 = connp->conn_saddr_v6;
2647 
2648 	/*
2649 	 * Allow setting new policies.
2650 	 * The addresses/ports are already set, thus the IPsec policy calls
2651 	 * can handle their passed-in conn's.
2652 	 */
2653 	connp->conn_policy_cached = B_FALSE;
2654 
2655 	/*
2656 	 * Cache IPsec policy in this conn.  If we have per-socket policy,
2657 	 * we'll cache that.  If we don't, we'll inherit global policy.
2658 	 *
2659 	 * This is done before the caller inserts in the receive-side fanout.
2660 	 * Note that conn_policy_cached is set by ipsec_conn_cache_policy() even
2661 	 * for connections where we don't have a policy. This is to prevent
2662 	 * global policy lookups in the inbound path.
2663 	 *
2664 	 * If we insert before we set conn_policy_cached,
2665 	 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true
2666 	 * because global policy cound be non-empty. We normally call
2667 	 * ipsec_check_policy() for conn_policy_cached connections only if
2668 	 * conn_in_enforce_policy is set. But in this case,
2669 	 * conn_policy_cached can get set anytime since we made the
2670 	 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is
2671 	 * called, which will make the above assumption false.  Thus, we
2672 	 * need to insert after we set conn_policy_cached.
2673 	 */
2674 	error = ipsec_conn_cache_policy(connp,
2675 	    connp->conn_ipversion == IPV4_VERSION);
2676 	if (error != 0)
2677 		return (error);
2678 
2679 	/*
2680 	 * We defer to do LSO check until here since now we have better idea
2681 	 * whether IPsec is present. If the underlying ill is LSO capable,
2682 	 * copy its capability in so the ULP can decide whether to enable LSO
2683 	 * on this connection. So far, only TCP/IPv4 is implemented, so won't
2684 	 * claim LSO for IPv6.
2685 	 *
2686 	 * Currently, won't enable LSO for IRE_LOOPBACK or IRE_LOCAL, because
2687 	 * the receiver can not handle it. Also not to enable LSO for MULTIRT.
2688 	 */
2689 	ixa->ixa_flags &= ~IXAF_LSO_CAPAB;
2690 
2691 	ASSERT(ixa->ixa_ire != NULL);
2692 	if (ixa->ixa_ipst->ips_ip_lso_outbound && (flags & IPDF_LSO) &&
2693 	    !(ixa->ixa_flags & IXAF_IPSEC_SECURE) &&
2694 	    !(ixa->ixa_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) &&
2695 	    !(ixa->ixa_ire->ire_flags & RTF_MULTIRT) &&
2696 	    (ixa->ixa_nce != NULL) &&
2697 	    ((ixa->ixa_flags & IXAF_IS_IPV4) ?
2698 	    ILL_LSO_TCP_IPV4_USABLE(ixa->ixa_nce->nce_ill) :
2699 	    ILL_LSO_TCP_IPV6_USABLE(ixa->ixa_nce->nce_ill))) {
2700 		ixa->ixa_lso_capab = *ixa->ixa_nce->nce_ill->ill_lso_capab;
2701 		ixa->ixa_flags |= IXAF_LSO_CAPAB;
2702 	}
2703 
2704 	/* Check whether ZEROCOPY capability is usable for this connection. */
2705 	ixa->ixa_flags &= ~IXAF_ZCOPY_CAPAB;
2706 
2707 	if ((flags & IPDF_ZCOPY) &&
2708 	    !(ixa->ixa_flags & IXAF_IPSEC_SECURE) &&
2709 	    !(ixa->ixa_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) &&
2710 	    !(ixa->ixa_ire->ire_flags & RTF_MULTIRT) &&
2711 	    (ixa->ixa_nce != NULL) &&
2712 	    ILL_ZCOPY_USABLE(ixa->ixa_nce->nce_ill)) {
2713 		ixa->ixa_flags |= IXAF_ZCOPY_CAPAB;
2714 	}
2715 	return (0);
2716 }
2717 
2718 /*
2719  * Predicates to check if the addresses match conn_last*
2720  */
2721 
2722 /*
2723  * Compare the conn against an address.
2724  * If using mapped addresses on AF_INET6 sockets, use the _v6 function
2725  */
2726 boolean_t
2727 conn_same_as_last_v4(conn_t *connp, sin_t *sin)
2728 {
2729 	ASSERT(connp->conn_family == AF_INET);
2730 	return (sin->sin_addr.s_addr == connp->conn_v4lastdst &&
2731 	    sin->sin_port == connp->conn_lastdstport);
2732 }
2733 
2734 /*
2735  * Compare, including for mapped addresses
2736  */
2737 boolean_t
2738 conn_same_as_last_v6(conn_t *connp, sin6_t *sin6)
2739 {
2740 	return (IN6_ARE_ADDR_EQUAL(&connp->conn_v6lastdst, &sin6->sin6_addr) &&
2741 	    sin6->sin6_port == connp->conn_lastdstport &&
2742 	    sin6->sin6_flowinfo == connp->conn_lastflowinfo &&
2743 	    sin6->sin6_scope_id == connp->conn_lastscopeid);
2744 }
2745 
2746 /*
2747  * Compute a label and place it in the ip_packet_t.
2748  * Handles IPv4 and IPv6.
2749  * The caller should have a correct ixa_tsl and ixa_zoneid and have
2750  * already called conn_connect or ip_attr_connect to ensure that tsol_check_dest
2751  * has been called.
2752  */
2753 int
2754 conn_update_label(const conn_t *connp, const ip_xmit_attr_t *ixa,
2755     const in6_addr_t *v6dst, ip_pkt_t *ipp)
2756 {
2757 	int		err;
2758 	ipaddr_t	v4dst;
2759 
2760 	if (IN6_IS_ADDR_V4MAPPED(v6dst)) {
2761 		uchar_t		opt_storage[IP_MAX_OPT_LENGTH];
2762 
2763 		IN6_V4MAPPED_TO_IPADDR(v6dst, v4dst);
2764 
2765 		err = tsol_compute_label_v4(ixa->ixa_tsl, ixa->ixa_zoneid,
2766 		    v4dst, opt_storage, ixa->ixa_ipst);
2767 		if (err == 0) {
2768 			/* Length contained in opt_storage[IPOPT_OLEN] */
2769 			err = optcom_pkt_set(opt_storage,
2770 			    opt_storage[IPOPT_OLEN],
2771 			    (uchar_t **)&ipp->ipp_label_v4,
2772 			    &ipp->ipp_label_len_v4);
2773 		}
2774 		if (err != 0) {
2775 			DTRACE_PROBE4(tx__ip__log__info__updatelabel,
2776 			    char *, "conn(1) failed to update options(2) "
2777 			    "on ixa(3)",
2778 			    conn_t *, connp, char *, opt_storage,
2779 			    ip_xmit_attr_t *, ixa);
2780 		}
2781 		if (ipp->ipp_label_len_v4 != 0)
2782 			ipp->ipp_fields |= IPPF_LABEL_V4;
2783 		else
2784 			ipp->ipp_fields &= ~IPPF_LABEL_V4;
2785 	} else {
2786 		uchar_t		opt_storage[TSOL_MAX_IPV6_OPTION];
2787 		uint_t		optlen;
2788 
2789 		err = tsol_compute_label_v6(ixa->ixa_tsl, ixa->ixa_zoneid,
2790 		    v6dst, opt_storage, ixa->ixa_ipst);
2791 		if (err == 0) {
2792 			/*
2793 			 * Note that ipp_label_v6 is just the option - not
2794 			 * the hopopts extension header.
2795 			 *
2796 			 * Length contained in opt_storage[IPOPT_OLEN], but
2797 			 * that doesn't include the two byte options header.
2798 			 */
2799 			optlen = opt_storage[IPOPT_OLEN];
2800 			if (optlen != 0)
2801 				optlen += 2;
2802 
2803 			err = optcom_pkt_set(opt_storage, optlen,
2804 			    (uchar_t **)&ipp->ipp_label_v6,
2805 			    &ipp->ipp_label_len_v6);
2806 		}
2807 		if (err != 0) {
2808 			DTRACE_PROBE4(tx__ip__log__info__updatelabel,
2809 			    char *, "conn(1) failed to update options(2) "
2810 			    "on ixa(3)",
2811 			    conn_t *, connp, char *, opt_storage,
2812 			    ip_xmit_attr_t *, ixa);
2813 		}
2814 		if (ipp->ipp_label_len_v6 != 0)
2815 			ipp->ipp_fields |= IPPF_LABEL_V6;
2816 		else
2817 			ipp->ipp_fields &= ~IPPF_LABEL_V6;
2818 	}
2819 	return (err);
2820 }
2821 
2822 /*
2823  * Inherit all options settings from the parent/listener to the eager.
2824  * Returns zero on success; ENOMEM if memory allocation failed.
2825  *
2826  * We assume that the eager has not had any work done i.e., the conn_ixa
2827  * and conn_xmit_ipp are all zero.
2828  * Furthermore we assume that no other thread can access the eager (because
2829  * it isn't inserted in any fanout list).
2830  */
2831 int
2832 conn_inherit_parent(conn_t *lconnp, conn_t *econnp)
2833 {
2834 	cred_t	*credp;
2835 	int	err;
2836 	void	*notify_cookie;
2837 
2838 	econnp->conn_family = lconnp->conn_family;
2839 	econnp->conn_ipv6_v6only = lconnp->conn_ipv6_v6only;
2840 	econnp->conn_wq = lconnp->conn_wq;
2841 	econnp->conn_rq = lconnp->conn_rq;
2842 
2843 	/*
2844 	 * Make a safe copy of the transmit attributes.
2845 	 * conn_connect will later be used by the caller to setup the ire etc.
2846 	 */
2847 	ASSERT(econnp->conn_ixa->ixa_refcnt == 1);
2848 	ASSERT(econnp->conn_ixa->ixa_ire == NULL);
2849 	ASSERT(econnp->conn_ixa->ixa_dce == NULL);
2850 	ASSERT(econnp->conn_ixa->ixa_nce == NULL);
2851 
2852 	/* Preserve ixa_notify_cookie */
2853 	notify_cookie = econnp->conn_ixa->ixa_notify_cookie;
2854 	ixa_safe_copy(lconnp->conn_ixa, econnp->conn_ixa);
2855 	econnp->conn_ixa->ixa_notify_cookie = notify_cookie;
2856 
2857 	econnp->conn_bound_if = lconnp->conn_bound_if;
2858 	econnp->conn_incoming_ifindex = lconnp->conn_incoming_ifindex;
2859 
2860 	/* Inherit all RECV options */
2861 	econnp->conn_recv_ancillary = lconnp->conn_recv_ancillary;
2862 
2863 	err = ip_pkt_copy(&lconnp->conn_xmit_ipp, &econnp->conn_xmit_ipp,
2864 	    KM_NOSLEEP);
2865 	if (err != 0)
2866 		return (err);
2867 
2868 	econnp->conn_zoneid = lconnp->conn_zoneid;
2869 	econnp->conn_allzones = lconnp->conn_allzones;
2870 
2871 	/* This is odd. Pick a flowlabel for each connection instead? */
2872 	econnp->conn_flowinfo = lconnp->conn_flowinfo;
2873 
2874 	econnp->conn_default_ttl = lconnp->conn_default_ttl;
2875 
2876 	/*
2877 	 * TSOL: tsol_input_proc() needs the eager's cred before the
2878 	 * eager is accepted
2879 	 */
2880 	ASSERT(lconnp->conn_cred != NULL);
2881 	econnp->conn_cred = credp = lconnp->conn_cred;
2882 	crhold(credp);
2883 	econnp->conn_cpid = lconnp->conn_cpid;
2884 	econnp->conn_open_time = ddi_get_lbolt64();
2885 
2886 	/*
2887 	 * Cache things in the ixa without any refhold.
2888 	 * Listener might not have set up ixa_cred
2889 	 */
2890 	econnp->conn_ixa->ixa_cred = econnp->conn_cred;
2891 	econnp->conn_ixa->ixa_cpid = econnp->conn_cpid;
2892 	if (is_system_labeled())
2893 		econnp->conn_ixa->ixa_tsl = crgetlabel(econnp->conn_cred);
2894 
2895 	/*
2896 	 * If the caller has the process-wide flag set, then default to MAC
2897 	 * exempt mode.  This allows read-down to unlabeled hosts.
2898 	 */
2899 	if (getpflags(NET_MAC_AWARE, credp) != 0)
2900 		econnp->conn_mac_mode = CONN_MAC_AWARE;
2901 
2902 	econnp->conn_zone_is_global = lconnp->conn_zone_is_global;
2903 
2904 	/*
2905 	 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ
2906 	 * via soaccept()->soinheritoptions() which essentially applies
2907 	 * all the listener options to the new connection. The options that we
2908 	 * need to take care of are:
2909 	 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
2910 	 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER,
2911 	 * SO_SNDBUF, SO_RCVBUF.
2912 	 *
2913 	 * SO_RCVBUF:	conn_rcvbuf is set.
2914 	 * SO_SNDBUF:	conn_sndbuf is set.
2915 	 */
2916 
2917 	/* Could we define a struct and use a struct copy for this? */
2918 	econnp->conn_sndbuf = lconnp->conn_sndbuf;
2919 	econnp->conn_rcvbuf = lconnp->conn_rcvbuf;
2920 	econnp->conn_sndlowat = lconnp->conn_sndlowat;
2921 	econnp->conn_rcvlowat = lconnp->conn_rcvlowat;
2922 	econnp->conn_dgram_errind = lconnp->conn_dgram_errind;
2923 	econnp->conn_oobinline = lconnp->conn_oobinline;
2924 	econnp->conn_debug = lconnp->conn_debug;
2925 	econnp->conn_keepalive = lconnp->conn_keepalive;
2926 	econnp->conn_linger = lconnp->conn_linger;
2927 	econnp->conn_lingertime = lconnp->conn_lingertime;
2928 
2929 	/* Set the IP options */
2930 	econnp->conn_broadcast = lconnp->conn_broadcast;
2931 	econnp->conn_useloopback = lconnp->conn_useloopback;
2932 	econnp->conn_reuseaddr = lconnp->conn_reuseaddr;
2933 	return (0);
2934 }
2935