xref: /illumos-gate/usr/src/uts/common/inet/ip/conn_opt.c (revision bd670b35)
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) + ucredsize;
225 		IP_STAT(ipst, conn_in_recvucred);
226 	}
227 
228 	/*
229 	 * If SO_TIMESTAMP is set allocate the appropriate sized
230 	 * buffer. Since gethrestime() expects a pointer aligned
231 	 * argument, we allocate space necessary for extra
232 	 * alignment (even though it might not be used).
233 	 */
234 	if (recv_ancillary.crb_timestamp) {
235 		ancil_size += sizeof (struct T_opthdr) +
236 		    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
237 		IP_STAT(ipst, conn_in_timestamp);
238 	}
239 
240 	/*
241 	 * If IP_RECVTTL is set allocate the appropriate sized buffer
242 	 */
243 	if (recv_ancillary.crb_recvttl &&
244 	    (ira->ira_flags & IRAF_IS_IPV4)) {
245 		ancil_size += sizeof (struct T_opthdr) + sizeof (uint8_t);
246 		IP_STAT(ipst, conn_in_recvttl);
247 	}
248 
249 	return (ancil_size);
250 }
251 
252 /*
253  * Lay down the ancillary data items at "ancil_buf".
254  * Assumes caller has used conn_recvancillary_size to allocate a sufficiently
255  * large buffer - ancil_size.
256  */
257 void
258 conn_recvancillary_add(conn_t *connp, crb_t recv_ancillary,
259     ip_recv_attr_t *ira, ip_pkt_t *ipp, uchar_t *ancil_buf, uint_t ancil_size)
260 {
261 	/*
262 	 * Copy in destination address before options to avoid
263 	 * any padding issues.
264 	 */
265 	if (recv_ancillary.crb_recvdstaddr &&
266 	    (ira->ira_flags & IRAF_IS_IPV4)) {
267 		struct T_opthdr *toh;
268 		ipaddr_t *dstptr;
269 
270 		toh = (struct T_opthdr *)ancil_buf;
271 		toh->level = IPPROTO_IP;
272 		toh->name = IP_RECVDSTADDR;
273 		toh->len = sizeof (struct T_opthdr) + sizeof (ipaddr_t);
274 		toh->status = 0;
275 		ancil_buf += sizeof (struct T_opthdr);
276 		dstptr = (ipaddr_t *)ancil_buf;
277 		*dstptr = ipp->ipp_addr_v4;
278 		ancil_buf += sizeof (ipaddr_t);
279 		ancil_size -= toh->len;
280 	}
281 
282 	/*
283 	 * ip_recvpktinfo is used for both AF_INET and AF_INET6 but
284 	 * are different
285 	 */
286 	if (recv_ancillary.crb_ip_recvpktinfo &&
287 	    connp->conn_family == AF_INET) {
288 		ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
289 		struct T_opthdr *toh;
290 		struct in_pktinfo *pktinfop;
291 		ill_t *ill;
292 		ipif_t *ipif;
293 
294 		toh = (struct T_opthdr *)ancil_buf;
295 		toh->level = IPPROTO_IP;
296 		toh->name = IP_PKTINFO;
297 		toh->len = sizeof (struct T_opthdr) + sizeof (*pktinfop);
298 		toh->status = 0;
299 		ancil_buf += sizeof (struct T_opthdr);
300 		pktinfop = (struct in_pktinfo *)ancil_buf;
301 
302 		pktinfop->ipi_ifindex = ira->ira_ruifindex;
303 		pktinfop->ipi_spec_dst.s_addr = INADDR_ANY;
304 
305 		/* Find a good address to report */
306 		ill = ill_lookup_on_ifindex(ira->ira_ruifindex, B_FALSE, ipst);
307 		if (ill != NULL) {
308 			ipif = ipif_good_addr(ill, IPCL_ZONEID(connp));
309 			if (ipif != NULL) {
310 				pktinfop->ipi_spec_dst.s_addr =
311 				    ipif->ipif_lcl_addr;
312 				ipif_refrele(ipif);
313 			}
314 			ill_refrele(ill);
315 		}
316 		pktinfop->ipi_addr.s_addr = ipp->ipp_addr_v4;
317 		ancil_buf += sizeof (struct in_pktinfo);
318 		ancil_size -= toh->len;
319 	}
320 
321 	if ((recv_ancillary.crb_recvopts) &&
322 	    (ipp->ipp_fields & IPPF_IPV4_OPTIONS)) {
323 		struct T_opthdr *toh;
324 
325 		toh = (struct T_opthdr *)ancil_buf;
326 		toh->level = IPPROTO_IP;
327 		toh->name = IP_RECVOPTS;
328 		toh->len = sizeof (struct T_opthdr) + ipp->ipp_ipv4_options_len;
329 		toh->status = 0;
330 		ancil_buf += sizeof (struct T_opthdr);
331 		bcopy(ipp->ipp_ipv4_options, ancil_buf,
332 		    ipp->ipp_ipv4_options_len);
333 		ancil_buf += ipp->ipp_ipv4_options_len;
334 		ancil_size -= toh->len;
335 	}
336 
337 	if (recv_ancillary.crb_recvslla) {
338 		ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
339 		struct T_opthdr *toh;
340 		struct sockaddr_dl *dstptr;
341 		ill_t *ill;
342 		int alen = 0;
343 
344 		ill = ill_lookup_on_ifindex(ira->ira_rifindex, B_FALSE, ipst);
345 		if (ill != NULL)
346 			alen = ill->ill_phys_addr_length;
347 
348 		/*
349 		 * For loopback multicast and broadcast the packet arrives
350 		 * with ira_ruifdex being the physical interface, but
351 		 * ira_l2src is all zero since ip_postfrag_loopback doesn't
352 		 * know our l2src. We don't report the address in that case.
353 		 */
354 		if (ira->ira_flags & IRAF_LOOPBACK)
355 			alen = 0;
356 
357 		toh = (struct T_opthdr *)ancil_buf;
358 		toh->level = IPPROTO_IP;
359 		toh->name = IP_RECVSLLA;
360 		toh->len = sizeof (struct T_opthdr) +
361 		    sizeof (struct sockaddr_dl);
362 		toh->status = 0;
363 		ancil_buf += sizeof (struct T_opthdr);
364 		dstptr = (struct sockaddr_dl *)ancil_buf;
365 		dstptr->sdl_family = AF_LINK;
366 		dstptr->sdl_index = ira->ira_ruifindex;
367 		if (ill != NULL)
368 			dstptr->sdl_type = ill->ill_type;
369 		else
370 			dstptr->sdl_type = 0;
371 		dstptr->sdl_nlen = 0;
372 		dstptr->sdl_alen = alen;
373 		dstptr->sdl_slen = 0;
374 		bcopy(ira->ira_l2src, dstptr->sdl_data, alen);
375 		ancil_buf += sizeof (struct sockaddr_dl);
376 		ancil_size -= toh->len;
377 		if (ill != NULL)
378 			ill_refrele(ill);
379 	}
380 
381 	if (recv_ancillary.crb_recvif) {
382 		struct T_opthdr *toh;
383 		uint_t		*dstptr;
384 
385 		toh = (struct T_opthdr *)ancil_buf;
386 		toh->level = IPPROTO_IP;
387 		toh->name = IP_RECVIF;
388 		toh->len = sizeof (struct T_opthdr) + sizeof (uint_t);
389 		toh->status = 0;
390 		ancil_buf += sizeof (struct T_opthdr);
391 		dstptr = (uint_t *)ancil_buf;
392 		*dstptr = ira->ira_ruifindex;
393 		ancil_buf += sizeof (uint_t);
394 		ancil_size -= toh->len;
395 	}
396 
397 	/*
398 	 * ip_recvpktinfo is used for both AF_INET and AF_INET6 but
399 	 * are different
400 	 */
401 	if (recv_ancillary.crb_ip_recvpktinfo &&
402 	    connp->conn_family == AF_INET6) {
403 		struct T_opthdr *toh;
404 		struct in6_pktinfo *pkti;
405 
406 		toh = (struct T_opthdr *)ancil_buf;
407 		toh->level = IPPROTO_IPV6;
408 		toh->name = IPV6_PKTINFO;
409 		toh->len = sizeof (struct T_opthdr) + sizeof (*pkti);
410 		toh->status = 0;
411 		ancil_buf += sizeof (struct T_opthdr);
412 		pkti = (struct in6_pktinfo *)ancil_buf;
413 		if (ira->ira_flags & IRAF_IS_IPV4) {
414 			IN6_IPADDR_TO_V4MAPPED(ipp->ipp_addr_v4,
415 			    &pkti->ipi6_addr);
416 		} else {
417 			pkti->ipi6_addr = ipp->ipp_addr;
418 		}
419 		pkti->ipi6_ifindex = ira->ira_ruifindex;
420 
421 		ancil_buf += sizeof (*pkti);
422 		ancil_size -= toh->len;
423 	}
424 	if (recv_ancillary.crb_ipv6_recvhoplimit) {
425 		struct T_opthdr *toh;
426 
427 		toh = (struct T_opthdr *)ancil_buf;
428 		toh->level = IPPROTO_IPV6;
429 		toh->name = IPV6_HOPLIMIT;
430 		toh->len = sizeof (struct T_opthdr) + sizeof (uint_t);
431 		toh->status = 0;
432 		ancil_buf += sizeof (struct T_opthdr);
433 		*(uint_t *)ancil_buf = ipp->ipp_hoplimit;
434 		ancil_buf += sizeof (uint_t);
435 		ancil_size -= toh->len;
436 	}
437 	if (recv_ancillary.crb_ipv6_recvtclass) {
438 		struct T_opthdr *toh;
439 
440 		toh = (struct T_opthdr *)ancil_buf;
441 		toh->level = IPPROTO_IPV6;
442 		toh->name = IPV6_TCLASS;
443 		toh->len = sizeof (struct T_opthdr) + sizeof (uint_t);
444 		toh->status = 0;
445 		ancil_buf += sizeof (struct T_opthdr);
446 
447 		if (ira->ira_flags & IRAF_IS_IPV4)
448 			*(uint_t *)ancil_buf = ipp->ipp_type_of_service;
449 		else
450 			*(uint_t *)ancil_buf = ipp->ipp_tclass;
451 		ancil_buf += sizeof (uint_t);
452 		ancil_size -= toh->len;
453 	}
454 	if (recv_ancillary.crb_ipv6_recvhopopts &&
455 	    (ipp->ipp_fields & IPPF_HOPOPTS)) {
456 		struct T_opthdr *toh;
457 
458 		toh = (struct T_opthdr *)ancil_buf;
459 		toh->level = IPPROTO_IPV6;
460 		toh->name = IPV6_HOPOPTS;
461 		toh->len = sizeof (struct T_opthdr) + ipp->ipp_hopoptslen;
462 		toh->status = 0;
463 		ancil_buf += sizeof (struct T_opthdr);
464 		bcopy(ipp->ipp_hopopts, ancil_buf, ipp->ipp_hopoptslen);
465 		ancil_buf += ipp->ipp_hopoptslen;
466 		ancil_size -= toh->len;
467 	}
468 	/*
469 	 * To honor RFC3542 when an application asks for both IPV6_RECVDSTOPTS
470 	 * and IPV6_RECVRTHDR, we pass up the item rthdrdstopts (the destination
471 	 * options that appear before a routing header.
472 	 * We also pass them up if IPV6_RECVRTHDRDSTOPTS is set.
473 	 */
474 	if (ipp->ipp_fields & IPPF_RTHDRDSTOPTS) {
475 		if (recv_ancillary.crb_ipv6_recvrthdrdstopts ||
476 		    (recv_ancillary.crb_ipv6_recvdstopts &&
477 		    recv_ancillary.crb_ipv6_recvrthdr)) {
478 			struct T_opthdr *toh;
479 
480 			toh = (struct T_opthdr *)ancil_buf;
481 			toh->level = IPPROTO_IPV6;
482 			toh->name = IPV6_DSTOPTS;
483 			toh->len = sizeof (struct T_opthdr) +
484 			    ipp->ipp_rthdrdstoptslen;
485 			toh->status = 0;
486 			ancil_buf += sizeof (struct T_opthdr);
487 			bcopy(ipp->ipp_rthdrdstopts, ancil_buf,
488 			    ipp->ipp_rthdrdstoptslen);
489 			ancil_buf += ipp->ipp_rthdrdstoptslen;
490 			ancil_size -= toh->len;
491 		}
492 	}
493 	if (recv_ancillary.crb_ipv6_recvrthdr &&
494 	    (ipp->ipp_fields & IPPF_RTHDR)) {
495 		struct T_opthdr *toh;
496 
497 		toh = (struct T_opthdr *)ancil_buf;
498 		toh->level = IPPROTO_IPV6;
499 		toh->name = IPV6_RTHDR;
500 		toh->len = sizeof (struct T_opthdr) + ipp->ipp_rthdrlen;
501 		toh->status = 0;
502 		ancil_buf += sizeof (struct T_opthdr);
503 		bcopy(ipp->ipp_rthdr, ancil_buf, ipp->ipp_rthdrlen);
504 		ancil_buf += ipp->ipp_rthdrlen;
505 		ancil_size -= toh->len;
506 	}
507 	if ((recv_ancillary.crb_ipv6_recvdstopts ||
508 	    recv_ancillary.crb_old_ipv6_recvdstopts) &&
509 	    (ipp->ipp_fields & IPPF_DSTOPTS)) {
510 		struct T_opthdr *toh;
511 
512 		toh = (struct T_opthdr *)ancil_buf;
513 		toh->level = IPPROTO_IPV6;
514 		toh->name = IPV6_DSTOPTS;
515 		toh->len = sizeof (struct T_opthdr) + ipp->ipp_dstoptslen;
516 		toh->status = 0;
517 		ancil_buf += sizeof (struct T_opthdr);
518 		bcopy(ipp->ipp_dstopts, ancil_buf, ipp->ipp_dstoptslen);
519 		ancil_buf += ipp->ipp_dstoptslen;
520 		ancil_size -= toh->len;
521 	}
522 
523 	if (recv_ancillary.crb_recvucred && ira->ira_cred != NULL) {
524 		struct T_opthdr *toh;
525 		cred_t		*rcr = connp->conn_cred;
526 
527 		toh = (struct T_opthdr *)ancil_buf;
528 		toh->level = SOL_SOCKET;
529 		toh->name = SCM_UCRED;
530 		toh->len = sizeof (struct T_opthdr) + ucredsize;
531 		toh->status = 0;
532 		(void) cred2ucred(ira->ira_cred, ira->ira_cpid, &toh[1], rcr);
533 		ancil_buf += toh->len;
534 		ancil_size -= toh->len;
535 	}
536 	if (recv_ancillary.crb_timestamp) {
537 		struct	T_opthdr *toh;
538 
539 		toh = (struct T_opthdr *)ancil_buf;
540 		toh->level = SOL_SOCKET;
541 		toh->name = SCM_TIMESTAMP;
542 		toh->len = sizeof (struct T_opthdr) +
543 		    sizeof (timestruc_t) + _POINTER_ALIGNMENT;
544 		toh->status = 0;
545 		ancil_buf += sizeof (struct T_opthdr);
546 		/* Align for gethrestime() */
547 		ancil_buf = (uchar_t *)P2ROUNDUP((intptr_t)ancil_buf,
548 		    sizeof (intptr_t));
549 		gethrestime((timestruc_t *)ancil_buf);
550 		ancil_buf = (uchar_t *)toh + toh->len;
551 		ancil_size -= toh->len;
552 	}
553 
554 	/*
555 	 * CAUTION:
556 	 * Due to aligment issues
557 	 * Processing of IP_RECVTTL option
558 	 * should always be the last. Adding
559 	 * any option processing after this will
560 	 * cause alignment panic.
561 	 */
562 	if (recv_ancillary.crb_recvttl &&
563 	    (ira->ira_flags & IRAF_IS_IPV4)) {
564 		struct	T_opthdr *toh;
565 		uint8_t	*dstptr;
566 
567 		toh = (struct T_opthdr *)ancil_buf;
568 		toh->level = IPPROTO_IP;
569 		toh->name = IP_RECVTTL;
570 		toh->len = sizeof (struct T_opthdr) + sizeof (uint8_t);
571 		toh->status = 0;
572 		ancil_buf += sizeof (struct T_opthdr);
573 		dstptr = (uint8_t *)ancil_buf;
574 		*dstptr = ipp->ipp_hoplimit;
575 		ancil_buf += sizeof (uint8_t);
576 		ancil_size -= toh->len;
577 	}
578 
579 	/* Consumed all of allocated space */
580 	ASSERT(ancil_size == 0);
581 
582 }
583 
584 /*
585  * This routine retrieves the current status of socket options.
586  * It returns the size of the option retrieved, or -1.
587  */
588 int
589 conn_opt_get(conn_opt_arg_t *coa, t_scalar_t level, t_scalar_t name,
590     uchar_t *ptr)
591 {
592 	int		*i1 = (int *)ptr;
593 	conn_t		*connp = coa->coa_connp;
594 	ip_xmit_attr_t	*ixa = coa->coa_ixa;
595 	ip_pkt_t	*ipp = coa->coa_ipp;
596 	ip_stack_t	*ipst = ixa->ixa_ipst;
597 	uint_t		len;
598 
599 	ASSERT(MUTEX_HELD(&coa->coa_connp->conn_lock));
600 
601 	switch (level) {
602 	case SOL_SOCKET:
603 		switch (name) {
604 		case SO_DEBUG:
605 			*i1 = connp->conn_debug ? SO_DEBUG : 0;
606 			break;	/* goto sizeof (int) option return */
607 		case SO_KEEPALIVE:
608 			*i1 = connp->conn_keepalive ? SO_KEEPALIVE : 0;
609 			break;
610 		case SO_LINGER:	{
611 			struct linger *lgr = (struct linger *)ptr;
612 
613 			lgr->l_onoff = connp->conn_linger ? SO_LINGER : 0;
614 			lgr->l_linger = connp->conn_lingertime;
615 			}
616 			return (sizeof (struct linger));
617 
618 		case SO_OOBINLINE:
619 			*i1 = connp->conn_oobinline ? SO_OOBINLINE : 0;
620 			break;
621 		case SO_REUSEADDR:
622 			*i1 = connp->conn_reuseaddr ? SO_REUSEADDR : 0;
623 			break;	/* goto sizeof (int) option return */
624 		case SO_TYPE:
625 			*i1 = connp->conn_so_type;
626 			break;	/* goto sizeof (int) option return */
627 		case SO_DONTROUTE:
628 			*i1 = (ixa->ixa_flags & IXAF_DONTROUTE) ?
629 			    SO_DONTROUTE : 0;
630 			break;	/* goto sizeof (int) option return */
631 		case SO_USELOOPBACK:
632 			*i1 = connp->conn_useloopback ? SO_USELOOPBACK : 0;
633 			break;	/* goto sizeof (int) option return */
634 		case SO_BROADCAST:
635 			*i1 = connp->conn_broadcast ? SO_BROADCAST : 0;
636 			break;	/* goto sizeof (int) option return */
637 
638 		case SO_SNDBUF:
639 			*i1 = connp->conn_sndbuf;
640 			break;	/* goto sizeof (int) option return */
641 		case SO_RCVBUF:
642 			*i1 = connp->conn_rcvbuf;
643 			break;	/* goto sizeof (int) option return */
644 		case SO_RCVTIMEO:
645 		case SO_SNDTIMEO:
646 			/*
647 			 * Pass these two options in order for third part
648 			 * protocol usage. Here just return directly.
649 			 */
650 			*i1 = 0;
651 			break;
652 		case SO_DGRAM_ERRIND:
653 			*i1 = connp->conn_dgram_errind ? SO_DGRAM_ERRIND : 0;
654 			break;	/* goto sizeof (int) option return */
655 		case SO_RECVUCRED:
656 			*i1 = connp->conn_recv_ancillary.crb_recvucred;
657 			break;	/* goto sizeof (int) option return */
658 		case SO_TIMESTAMP:
659 			*i1 = connp->conn_recv_ancillary.crb_timestamp;
660 			break;	/* goto sizeof (int) option return */
661 #ifdef SO_VRRP
662 		case SO_VRRP:
663 			*i1 = connp->conn_isvrrp;
664 			break;	/* goto sizeof (int) option return */
665 #endif
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 #ifdef SO_VRRP
1062 	case SO_VRRP:
1063 		if (secpolicy_ip_config(cr, checkonly) != 0)
1064 			return (EACCES);
1065 		break;
1066 #endif
1067 	case SO_MAC_EXEMPT:
1068 		if (secpolicy_net_mac_aware(cr) != 0)
1069 			return (EACCES);
1070 		if (IPCL_IS_BOUND(connp))
1071 			return (EINVAL);
1072 		break;
1073 	case SO_MAC_IMPLICIT:
1074 		if (secpolicy_net_mac_implicit(cr) != 0)
1075 			return (EACCES);
1076 		break;
1077 	}
1078 	if (checkonly)
1079 		return (0);
1080 
1081 	mutex_enter(&connp->conn_lock);
1082 	/* Here we set the actual option value */
1083 	switch (name) {
1084 	case SO_DEBUG:
1085 		connp->conn_debug = onoff;
1086 		break;
1087 	case SO_KEEPALIVE:
1088 		connp->conn_keepalive = onoff;
1089 		break;
1090 	case SO_LINGER: {
1091 		struct linger *lgr = (struct linger *)invalp;
1092 
1093 		if (lgr->l_onoff) {
1094 			connp->conn_linger = 1;
1095 			connp->conn_lingertime = lgr->l_linger;
1096 		} else {
1097 			connp->conn_linger = 0;
1098 			connp->conn_lingertime = 0;
1099 		}
1100 		break;
1101 	}
1102 	case SO_OOBINLINE:
1103 		connp->conn_oobinline = onoff;
1104 		coa->coa_changed |= COA_OOBINLINE_CHANGED;
1105 		break;
1106 	case SO_REUSEADDR:
1107 		connp->conn_reuseaddr = onoff;
1108 		break;
1109 	case SO_DONTROUTE:
1110 		if (onoff)
1111 			ixa->ixa_flags |= IXAF_DONTROUTE;
1112 		else
1113 			ixa->ixa_flags &= ~IXAF_DONTROUTE;
1114 		coa->coa_changed |= COA_ROUTE_CHANGED;
1115 		break;
1116 	case SO_USELOOPBACK:
1117 		connp->conn_useloopback = onoff;
1118 		break;
1119 	case SO_BROADCAST:
1120 		connp->conn_broadcast = onoff;
1121 		break;
1122 	case SO_SNDBUF:
1123 		/* ULP has range checked the value */
1124 		connp->conn_sndbuf = *i1;
1125 		coa->coa_changed |= COA_SNDBUF_CHANGED;
1126 		break;
1127 	case SO_RCVBUF:
1128 		/* ULP has range checked the value */
1129 		connp->conn_rcvbuf = *i1;
1130 		coa->coa_changed |= COA_RCVBUF_CHANGED;
1131 		break;
1132 	case SO_RCVTIMEO:
1133 	case SO_SNDTIMEO:
1134 		/*
1135 		 * Pass these two options in order for third part
1136 		 * protocol usage.
1137 		 */
1138 		break;
1139 	case SO_DGRAM_ERRIND:
1140 		connp->conn_dgram_errind = onoff;
1141 		break;
1142 	case SO_RECVUCRED:
1143 		connp->conn_recv_ancillary.crb_recvucred = onoff;
1144 		break;
1145 	case SO_ALLZONES:
1146 		connp->conn_allzones = onoff;
1147 		coa->coa_changed |= COA_ROUTE_CHANGED;
1148 		if (onoff)
1149 			ixa->ixa_zoneid = ALL_ZONES;
1150 		else
1151 			ixa->ixa_zoneid = connp->conn_zoneid;
1152 		break;
1153 	case SO_TIMESTAMP:
1154 		connp->conn_recv_ancillary.crb_timestamp = onoff;
1155 		break;
1156 #ifdef SO_VRRP
1157 	case SO_VRRP:
1158 		connp->conn_isvrrp = onoff;
1159 		break;
1160 #endif
1161 	case SO_ANON_MLP:
1162 		connp->conn_anon_mlp = onoff;
1163 		break;
1164 	case SO_MAC_EXEMPT:
1165 		connp->conn_mac_mode = onoff ?
1166 		    CONN_MAC_AWARE : CONN_MAC_DEFAULT;
1167 		break;
1168 	case SO_MAC_IMPLICIT:
1169 		connp->conn_mac_mode = onoff ?
1170 		    CONN_MAC_IMPLICIT : CONN_MAC_DEFAULT;
1171 		break;
1172 	case SO_EXCLBIND:
1173 		connp->conn_exclbind = onoff;
1174 		break;
1175 	}
1176 	mutex_exit(&connp->conn_lock);
1177 	return (0);
1178 }
1179 
1180 /* Handle IPPROTO_IP */
1181 static int
1182 conn_opt_set_ip(conn_opt_arg_t *coa, t_scalar_t name, uint_t inlen,
1183     uchar_t *invalp, boolean_t checkonly, cred_t *cr)
1184 {
1185 	conn_t		*connp = coa->coa_connp;
1186 	ip_xmit_attr_t	*ixa = coa->coa_ixa;
1187 	ip_pkt_t	*ipp = coa->coa_ipp;
1188 	int		*i1 = (int *)invalp;
1189 	boolean_t	onoff = (*i1 == 0) ? 0 : 1;
1190 	ipaddr_t	addr = (ipaddr_t)*i1;
1191 	uint_t		ifindex;
1192 	zoneid_t	zoneid = IPCL_ZONEID(connp);
1193 	ipif_t		*ipif;
1194 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
1195 	int		error;
1196 
1197 	if (connp->conn_family != AF_INET)
1198 		return (EINVAL);
1199 
1200 	switch (name) {
1201 	case IP_TTL:
1202 		/* Don't allow zero */
1203 		if (*i1 < 1 || *i1 > 255)
1204 			return (EINVAL);
1205 		break;
1206 	case IP_MULTICAST_IF:
1207 		if (addr == INADDR_ANY) {
1208 			/* Clear */
1209 			ifindex = 0;
1210 			break;
1211 		}
1212 		ipif = ipif_lookup_addr(addr, NULL, zoneid, ipst);
1213 		if (ipif == NULL)
1214 			return (EHOSTUNREACH);
1215 		/* not supported by the virtual network iface */
1216 		if (IS_VNI(ipif->ipif_ill)) {
1217 			ipif_refrele(ipif);
1218 			return (EINVAL);
1219 		}
1220 		ifindex = ipif->ipif_ill->ill_phyint->phyint_ifindex;
1221 		ipif_refrele(ipif);
1222 		break;
1223 	case IP_NEXTHOP: {
1224 		ire_t	*ire;
1225 
1226 		if (addr == INADDR_ANY) {
1227 			/* Clear */
1228 			break;
1229 		}
1230 		/* Verify that the next-hop is on-link */
1231 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_ONLINK, NULL, zoneid,
1232 		    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
1233 		if (ire == NULL)
1234 			return (EHOSTUNREACH);
1235 		ire_refrele(ire);
1236 		break;
1237 	}
1238 	case IP_OPTIONS:
1239 	case T_IP_OPTIONS: {
1240 		uint_t newlen;
1241 
1242 		if (ipp->ipp_fields & IPPF_LABEL_V4)
1243 			newlen = inlen + (ipp->ipp_label_len_v4 + 3) & ~3;
1244 		else
1245 			newlen = inlen;
1246 		if ((inlen & 0x3) || newlen > IP_MAX_OPT_LENGTH) {
1247 			return (EINVAL);
1248 		}
1249 		break;
1250 	}
1251 	case IP_PKTINFO: {
1252 		struct in_pktinfo *pktinfo;
1253 
1254 		/* Two different valid lengths */
1255 		if (inlen != sizeof (int) &&
1256 		    inlen != sizeof (struct in_pktinfo))
1257 			return (EINVAL);
1258 		if (inlen == sizeof (int))
1259 			break;
1260 
1261 		pktinfo = (struct in_pktinfo *)invalp;
1262 		if (pktinfo->ipi_spec_dst.s_addr != INADDR_ANY) {
1263 			switch (ip_laddr_verify_v4(pktinfo->ipi_spec_dst.s_addr,
1264 			    zoneid, ipst, B_FALSE)) {
1265 			case IPVL_UNICAST_UP:
1266 			case IPVL_UNICAST_DOWN:
1267 				break;
1268 			default:
1269 				return (EADDRNOTAVAIL);
1270 			}
1271 		}
1272 		if (!ip_ifindex_valid(pktinfo->ipi_ifindex, B_FALSE, ipst))
1273 			return (ENXIO);
1274 		break;
1275 	}
1276 	case IP_BOUND_IF:
1277 		ifindex = *(uint_t *)i1;
1278 
1279 		/* Just check it is ok. */
1280 		if (!ip_ifindex_valid(ifindex, B_FALSE, ipst))
1281 			return (ENXIO);
1282 		break;
1283 	}
1284 	if (checkonly)
1285 		return (0);
1286 
1287 	/* Here we set the actual option value */
1288 	/*
1289 	 * conn_lock protects the bitfields, and is used to
1290 	 * set the fields atomically. Not needed for ixa settings since
1291 	 * the caller has an exclusive copy of the ixa.
1292 	 * We can not hold conn_lock across the multicast options though.
1293 	 */
1294 	switch (name) {
1295 	case IP_OPTIONS:
1296 	case T_IP_OPTIONS:
1297 		/* Save options for use by IP. */
1298 		mutex_enter(&connp->conn_lock);
1299 		error = optcom_pkt_set(invalp, inlen,
1300 		    (uchar_t **)&ipp->ipp_ipv4_options,
1301 		    &ipp->ipp_ipv4_options_len);
1302 		if (error != 0) {
1303 			mutex_exit(&connp->conn_lock);
1304 			return (error);
1305 		}
1306 		if (ipp->ipp_ipv4_options_len == 0) {
1307 			ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
1308 		} else {
1309 			ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
1310 		}
1311 		mutex_exit(&connp->conn_lock);
1312 		coa->coa_changed |= COA_HEADER_CHANGED;
1313 		coa->coa_changed |= COA_WROFF_CHANGED;
1314 		break;
1315 
1316 	case IP_TTL:
1317 		mutex_enter(&connp->conn_lock);
1318 		ipp->ipp_unicast_hops = *i1;
1319 		mutex_exit(&connp->conn_lock);
1320 		coa->coa_changed |= COA_HEADER_CHANGED;
1321 		break;
1322 	case IP_TOS:
1323 	case T_IP_TOS:
1324 		mutex_enter(&connp->conn_lock);
1325 		if (*i1 == -1) {
1326 			ipp->ipp_type_of_service = 0;
1327 		} else {
1328 			ipp->ipp_type_of_service = *i1;
1329 		}
1330 		mutex_exit(&connp->conn_lock);
1331 		coa->coa_changed |= COA_HEADER_CHANGED;
1332 		break;
1333 	case IP_MULTICAST_IF:
1334 		ixa->ixa_multicast_ifindex = ifindex;
1335 		ixa->ixa_multicast_ifaddr = addr;
1336 		coa->coa_changed |= COA_ROUTE_CHANGED;
1337 		break;
1338 	case IP_MULTICAST_TTL:
1339 		ixa->ixa_multicast_ttl = *invalp;
1340 		/* Handled automatically by ip_output */
1341 		break;
1342 	case IP_MULTICAST_LOOP:
1343 		if (*invalp != 0)
1344 			ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
1345 		else
1346 			ixa->ixa_flags &= ~IXAF_MULTICAST_LOOP;
1347 		/* Handled automatically by ip_output */
1348 		break;
1349 	case IP_RECVOPTS:
1350 		mutex_enter(&connp->conn_lock);
1351 		connp->conn_recv_ancillary.crb_recvopts = onoff;
1352 		mutex_exit(&connp->conn_lock);
1353 		break;
1354 	case IP_RECVDSTADDR:
1355 		mutex_enter(&connp->conn_lock);
1356 		connp->conn_recv_ancillary.crb_recvdstaddr = onoff;
1357 		mutex_exit(&connp->conn_lock);
1358 		break;
1359 	case IP_RECVIF:
1360 		mutex_enter(&connp->conn_lock);
1361 		connp->conn_recv_ancillary.crb_recvif = onoff;
1362 		mutex_exit(&connp->conn_lock);
1363 		break;
1364 	case IP_RECVSLLA:
1365 		mutex_enter(&connp->conn_lock);
1366 		connp->conn_recv_ancillary.crb_recvslla = onoff;
1367 		mutex_exit(&connp->conn_lock);
1368 		break;
1369 	case IP_RECVTTL:
1370 		mutex_enter(&connp->conn_lock);
1371 		connp->conn_recv_ancillary.crb_recvttl = onoff;
1372 		mutex_exit(&connp->conn_lock);
1373 		break;
1374 	case IP_PKTINFO: {
1375 		/*
1376 		 * This also handles IP_RECVPKTINFO.
1377 		 * IP_PKTINFO and IP_RECVPKTINFO have same value.
1378 		 * Differentiation is based on the size of the
1379 		 * argument passed in.
1380 		 */
1381 		struct in_pktinfo *pktinfo;
1382 
1383 		if (inlen == sizeof (int)) {
1384 			/* This is IP_RECVPKTINFO option. */
1385 			mutex_enter(&connp->conn_lock);
1386 			connp->conn_recv_ancillary.crb_ip_recvpktinfo =
1387 			    onoff;
1388 			mutex_exit(&connp->conn_lock);
1389 			break;
1390 		}
1391 
1392 		/* This is IP_PKTINFO option. */
1393 		mutex_enter(&connp->conn_lock);
1394 		pktinfo = (struct in_pktinfo *)invalp;
1395 		if (ipp->ipp_addr_v4 != INADDR_ANY) {
1396 			ipp->ipp_fields |= IPPF_ADDR;
1397 			IN6_INADDR_TO_V4MAPPED(&pktinfo->ipi_spec_dst,
1398 			    &ipp->ipp_addr);
1399 		} else {
1400 			ipp->ipp_fields &= ~IPPF_ADDR;
1401 			ipp->ipp_addr = ipv6_all_zeros;
1402 		}
1403 		mutex_exit(&connp->conn_lock);
1404 		ixa->ixa_ifindex = pktinfo->ipi_ifindex;
1405 		coa->coa_changed |= COA_ROUTE_CHANGED;
1406 		coa->coa_changed |= COA_HEADER_CHANGED;
1407 		break;
1408 	}
1409 	case IP_DONTFRAG:
1410 		if (onoff) {
1411 			ixa->ixa_flags |= (IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
1412 			ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
1413 		} else {
1414 			ixa->ixa_flags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
1415 			ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
1416 		}
1417 		/* Need to redo ip_attr_connect */
1418 		coa->coa_changed |= COA_ROUTE_CHANGED;
1419 		break;
1420 	case IP_ADD_MEMBERSHIP:
1421 	case IP_DROP_MEMBERSHIP:
1422 	case MCAST_JOIN_GROUP:
1423 	case MCAST_LEAVE_GROUP:
1424 		return (ip_opt_set_multicast_group(connp, name,
1425 		    invalp, B_FALSE, checkonly));
1426 
1427 	case IP_BLOCK_SOURCE:
1428 	case IP_UNBLOCK_SOURCE:
1429 	case IP_ADD_SOURCE_MEMBERSHIP:
1430 	case IP_DROP_SOURCE_MEMBERSHIP:
1431 	case MCAST_BLOCK_SOURCE:
1432 	case MCAST_UNBLOCK_SOURCE:
1433 	case MCAST_JOIN_SOURCE_GROUP:
1434 	case MCAST_LEAVE_SOURCE_GROUP:
1435 		return (ip_opt_set_multicast_sources(connp, name,
1436 		    invalp, B_FALSE, checkonly));
1437 
1438 	case IP_SEC_OPT:
1439 		mutex_enter(&connp->conn_lock);
1440 		error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp);
1441 		mutex_exit(&connp->conn_lock);
1442 		if (error != 0) {
1443 			return (error);
1444 		}
1445 		/* This is an IPsec policy change - redo ip_attr_connect */
1446 		coa->coa_changed |= COA_ROUTE_CHANGED;
1447 		break;
1448 	case IP_NEXTHOP:
1449 		ixa->ixa_nexthop_v4 = addr;
1450 		if (addr != INADDR_ANY)
1451 			ixa->ixa_flags |= IXAF_NEXTHOP_SET;
1452 		else
1453 			ixa->ixa_flags &= ~IXAF_NEXTHOP_SET;
1454 		coa->coa_changed |= COA_ROUTE_CHANGED;
1455 		break;
1456 
1457 	case IP_BOUND_IF:
1458 		ixa->ixa_ifindex = ifindex;		/* Send */
1459 		mutex_enter(&connp->conn_lock);
1460 		connp->conn_incoming_ifindex = ifindex;	/* Receive */
1461 		connp->conn_bound_if = ifindex;		/* getsockopt */
1462 		mutex_exit(&connp->conn_lock);
1463 		coa->coa_changed |= COA_ROUTE_CHANGED;
1464 		break;
1465 	case IP_UNSPEC_SRC:
1466 		mutex_enter(&connp->conn_lock);
1467 		connp->conn_unspec_src = onoff;
1468 		if (onoff)
1469 			ixa->ixa_flags &= ~IXAF_VERIFY_SOURCE;
1470 		else
1471 			ixa->ixa_flags |= IXAF_VERIFY_SOURCE;
1472 
1473 		mutex_exit(&connp->conn_lock);
1474 		break;
1475 	case IP_BROADCAST_TTL:
1476 		ixa->ixa_broadcast_ttl = *invalp;
1477 		ixa->ixa_flags |= IXAF_BROADCAST_TTL_SET;
1478 		/* Handled automatically by ip_output */
1479 		break;
1480 	case MRT_INIT:
1481 	case MRT_DONE:
1482 	case MRT_ADD_VIF:
1483 	case MRT_DEL_VIF:
1484 	case MRT_ADD_MFC:
1485 	case MRT_DEL_MFC:
1486 	case MRT_ASSERT:
1487 		if ((error = secpolicy_ip_config(cr, B_FALSE)) != 0) {
1488 			return (error);
1489 		}
1490 		error = ip_mrouter_set((int)name, connp, checkonly,
1491 		    (uchar_t *)invalp, inlen);
1492 		if (error) {
1493 			return (error);
1494 		}
1495 		return (0);
1496 
1497 	}
1498 	return (0);
1499 }
1500 
1501 /* Handle IPPROTO_IPV6 */
1502 static int
1503 conn_opt_set_ipv6(conn_opt_arg_t *coa, t_scalar_t name, uint_t inlen,
1504     uchar_t *invalp, boolean_t checkonly, cred_t *cr)
1505 {
1506 	conn_t		*connp = coa->coa_connp;
1507 	ip_xmit_attr_t	*ixa = coa->coa_ixa;
1508 	ip_pkt_t	*ipp = coa->coa_ipp;
1509 	int		*i1 = (int *)invalp;
1510 	boolean_t	onoff = (*i1 == 0) ? 0 : 1;
1511 	uint_t		ifindex;
1512 	zoneid_t	zoneid = IPCL_ZONEID(connp);
1513 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
1514 	int		error;
1515 
1516 	if (connp->conn_family != AF_INET6)
1517 		return (EINVAL);
1518 
1519 	switch (name) {
1520 	case IPV6_MULTICAST_IF:
1521 		/*
1522 		 * The only possible error is EINVAL.
1523 		 * We call this option on both V4 and V6
1524 		 * If both fail, then this call returns
1525 		 * EINVAL. If at least one of them succeeds we
1526 		 * return success.
1527 		 */
1528 		ifindex = *(uint_t *)i1;
1529 
1530 		if (!ip_ifindex_valid(ifindex, B_TRUE, ipst) &&
1531 		    !ip_ifindex_valid(ifindex, B_FALSE, ipst))
1532 			return (EINVAL);
1533 		break;
1534 	case IPV6_UNICAST_HOPS:
1535 		/* Don't allow zero. -1 means to use default */
1536 		if (*i1 < -1 || *i1 == 0 || *i1 > IPV6_MAX_HOPS)
1537 			return (EINVAL);
1538 		break;
1539 	case IPV6_MULTICAST_HOPS:
1540 		/* -1 means use default */
1541 		if (*i1 < -1 || *i1 > IPV6_MAX_HOPS)
1542 			return (EINVAL);
1543 		break;
1544 	case IPV6_MULTICAST_LOOP:
1545 		if (*i1 != 0 && *i1 != 1)
1546 			return (EINVAL);
1547 		break;
1548 	case IPV6_BOUND_IF:
1549 		ifindex = *(uint_t *)i1;
1550 
1551 		if (!ip_ifindex_valid(ifindex, B_TRUE, ipst))
1552 			return (ENXIO);
1553 		break;
1554 	case IPV6_PKTINFO: {
1555 		struct in6_pktinfo *pkti;
1556 		boolean_t isv6;
1557 
1558 		if (inlen != 0 && inlen != sizeof (struct in6_pktinfo))
1559 			return (EINVAL);
1560 		if (inlen == 0)
1561 			break;	/* Clear values below */
1562 
1563 		/*
1564 		 * Verify the source address and ifindex. Privileged users
1565 		 * can use any source address.
1566 		 */
1567 		pkti = (struct in6_pktinfo *)invalp;
1568 
1569 		/*
1570 		 * For link-local addresses we use the ipi6_ifindex when
1571 		 * we verify the local address.
1572 		 * If net_rawaccess then any source address can be used.
1573 		 */
1574 		if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr) &&
1575 		    secpolicy_net_rawaccess(cr) != 0) {
1576 			uint_t scopeid = 0;
1577 			in6_addr_t *v6src = &pkti->ipi6_addr;
1578 			ipaddr_t v4src;
1579 			ip_laddr_t laddr_type = IPVL_UNICAST_UP;
1580 
1581 			if (IN6_IS_ADDR_V4MAPPED(v6src)) {
1582 				IN6_V4MAPPED_TO_IPADDR(v6src, v4src);
1583 				if (v4src != INADDR_ANY) {
1584 					laddr_type = ip_laddr_verify_v4(v4src,
1585 					    zoneid, ipst, B_FALSE);
1586 				}
1587 			} else {
1588 				if (IN6_IS_ADDR_LINKSCOPE(v6src))
1589 					scopeid = pkti->ipi6_ifindex;
1590 
1591 				laddr_type = ip_laddr_verify_v6(v6src, zoneid,
1592 				    ipst, B_FALSE, scopeid);
1593 			}
1594 			switch (laddr_type) {
1595 			case IPVL_UNICAST_UP:
1596 			case IPVL_UNICAST_DOWN:
1597 				break;
1598 			default:
1599 				return (EADDRNOTAVAIL);
1600 			}
1601 			ixa->ixa_flags |= IXAF_VERIFY_SOURCE;
1602 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) {
1603 			/* Allow any source */
1604 			ixa->ixa_flags &= ~IXAF_VERIFY_SOURCE;
1605 		}
1606 		isv6 = !(IN6_IS_ADDR_V4MAPPED(&pkti->ipi6_addr));
1607 		if (!ip_ifindex_valid(pkti->ipi6_ifindex, isv6, ipst))
1608 			return (ENXIO);
1609 		break;
1610 	}
1611 	case IPV6_HOPLIMIT:
1612 		/* It is only allowed as ancilary data */
1613 		if (!coa->coa_ancillary)
1614 			return (EINVAL);
1615 
1616 		if (inlen != 0 && inlen != sizeof (int))
1617 			return (EINVAL);
1618 		if (inlen == sizeof (int)) {
1619 			if (*i1 > 255 || *i1 < -1 || *i1 == 0)
1620 				return (EINVAL);
1621 		}
1622 		break;
1623 	case IPV6_TCLASS:
1624 		if (inlen != 0 && inlen != sizeof (int))
1625 			return (EINVAL);
1626 		if (inlen == sizeof (int)) {
1627 			if (*i1 > 255 || *i1 < -1)
1628 				return (EINVAL);
1629 		}
1630 		break;
1631 	case IPV6_NEXTHOP:
1632 		if (inlen != 0 && inlen != sizeof (sin6_t))
1633 			return (EINVAL);
1634 		if (inlen == sizeof (sin6_t)) {
1635 			sin6_t *sin6 = (sin6_t *)invalp;
1636 			ire_t	*ire;
1637 
1638 			if (sin6->sin6_family != AF_INET6)
1639 				return (EAFNOSUPPORT);
1640 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
1641 				return (EADDRNOTAVAIL);
1642 
1643 			/* Verify that the next-hop is on-link */
1644 			ire = ire_ftable_lookup_v6(&sin6->sin6_addr,
1645 			    0, 0, IRE_ONLINK, NULL, zoneid,
1646 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
1647 			if (ire == NULL)
1648 				return (EHOSTUNREACH);
1649 			ire_refrele(ire);
1650 			break;
1651 		}
1652 		break;
1653 	case IPV6_RTHDR:
1654 	case IPV6_DSTOPTS:
1655 	case IPV6_RTHDRDSTOPTS:
1656 	case IPV6_HOPOPTS: {
1657 		/* All have the length field in the same place */
1658 		ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
1659 		/*
1660 		 * Sanity checks - minimum size, size a multiple of
1661 		 * eight bytes, and matching size passed in.
1662 		 */
1663 		if (inlen != 0 &&
1664 		    inlen != (8 * (hopts->ip6h_len + 1)))
1665 			return (EINVAL);
1666 		break;
1667 	}
1668 	case IPV6_PATHMTU:
1669 		/* Can't be set */
1670 		return (EINVAL);
1671 
1672 	case IPV6_USE_MIN_MTU:
1673 		if (inlen != sizeof (int))
1674 			return (EINVAL);
1675 		if (*i1 < -1 || *i1 > 1)
1676 			return (EINVAL);
1677 		break;
1678 	case IPV6_SRC_PREFERENCES:
1679 		if (inlen != sizeof (uint32_t))
1680 			return (EINVAL);
1681 		break;
1682 	case IPV6_V6ONLY:
1683 		if (*i1 < 0 || *i1 > 1) {
1684 			return (EINVAL);
1685 		}
1686 		break;
1687 	}
1688 	if (checkonly)
1689 		return (0);
1690 
1691 	/* Here we set the actual option value */
1692 	/*
1693 	 * conn_lock protects the bitfields, and is used to
1694 	 * set the fields atomically. Not needed for ixa settings since
1695 	 * the caller has an exclusive copy of the ixa.
1696 	 * We can not hold conn_lock across the multicast options though.
1697 	 */
1698 	ASSERT(MUTEX_NOT_HELD(&coa->coa_connp->conn_lock));
1699 	switch (name) {
1700 	case IPV6_MULTICAST_IF:
1701 		ixa->ixa_multicast_ifindex = ifindex;
1702 		/* Need to redo ip_attr_connect */
1703 		coa->coa_changed |= COA_ROUTE_CHANGED;
1704 		break;
1705 	case IPV6_UNICAST_HOPS:
1706 		/* -1 means use default */
1707 		mutex_enter(&connp->conn_lock);
1708 		if (*i1 == -1) {
1709 			ipp->ipp_unicast_hops = connp->conn_default_ttl;
1710 		} else {
1711 			ipp->ipp_unicast_hops = (uint8_t)*i1;
1712 		}
1713 		mutex_exit(&connp->conn_lock);
1714 		coa->coa_changed |= COA_HEADER_CHANGED;
1715 		break;
1716 	case IPV6_MULTICAST_HOPS:
1717 		/* -1 means use default */
1718 		if (*i1 == -1) {
1719 			ixa->ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1720 		} else {
1721 			ixa->ixa_multicast_ttl = (uint8_t)*i1;
1722 		}
1723 		/* Handled automatically by ip_output */
1724 		break;
1725 	case IPV6_MULTICAST_LOOP:
1726 		if (*i1 != 0)
1727 			ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
1728 		else
1729 			ixa->ixa_flags &= ~IXAF_MULTICAST_LOOP;
1730 		/* Handled automatically by ip_output */
1731 		break;
1732 	case IPV6_JOIN_GROUP:
1733 	case IPV6_LEAVE_GROUP:
1734 	case MCAST_JOIN_GROUP:
1735 	case MCAST_LEAVE_GROUP:
1736 		return (ip_opt_set_multicast_group(connp, name,
1737 		    invalp, B_TRUE, checkonly));
1738 
1739 	case MCAST_BLOCK_SOURCE:
1740 	case MCAST_UNBLOCK_SOURCE:
1741 	case MCAST_JOIN_SOURCE_GROUP:
1742 	case MCAST_LEAVE_SOURCE_GROUP:
1743 		return (ip_opt_set_multicast_sources(connp, name,
1744 		    invalp, B_TRUE, checkonly));
1745 
1746 	case IPV6_BOUND_IF:
1747 		ixa->ixa_ifindex = ifindex;		/* Send */
1748 		mutex_enter(&connp->conn_lock);
1749 		connp->conn_incoming_ifindex = ifindex;	/* Receive */
1750 		connp->conn_bound_if = ifindex;		/* getsockopt */
1751 		mutex_exit(&connp->conn_lock);
1752 		coa->coa_changed |= COA_ROUTE_CHANGED;
1753 		break;
1754 	case IPV6_UNSPEC_SRC:
1755 		mutex_enter(&connp->conn_lock);
1756 		connp->conn_unspec_src = onoff;
1757 		if (onoff)
1758 			ixa->ixa_flags &= ~IXAF_VERIFY_SOURCE;
1759 		else
1760 			ixa->ixa_flags |= IXAF_VERIFY_SOURCE;
1761 		mutex_exit(&connp->conn_lock);
1762 		break;
1763 	case IPV6_RECVPKTINFO:
1764 		mutex_enter(&connp->conn_lock);
1765 		connp->conn_recv_ancillary.crb_ip_recvpktinfo = onoff;
1766 		mutex_exit(&connp->conn_lock);
1767 		break;
1768 	case IPV6_RECVTCLASS:
1769 		mutex_enter(&connp->conn_lock);
1770 		connp->conn_recv_ancillary.crb_ipv6_recvtclass = onoff;
1771 		mutex_exit(&connp->conn_lock);
1772 		break;
1773 	case IPV6_RECVPATHMTU:
1774 		mutex_enter(&connp->conn_lock);
1775 		connp->conn_ipv6_recvpathmtu = onoff;
1776 		mutex_exit(&connp->conn_lock);
1777 		break;
1778 	case IPV6_RECVHOPLIMIT:
1779 		mutex_enter(&connp->conn_lock);
1780 		connp->conn_recv_ancillary.crb_ipv6_recvhoplimit =
1781 		    onoff;
1782 		mutex_exit(&connp->conn_lock);
1783 		break;
1784 	case IPV6_RECVHOPOPTS:
1785 		mutex_enter(&connp->conn_lock);
1786 		connp->conn_recv_ancillary.crb_ipv6_recvhopopts = onoff;
1787 		mutex_exit(&connp->conn_lock);
1788 		break;
1789 	case IPV6_RECVDSTOPTS:
1790 		mutex_enter(&connp->conn_lock);
1791 		connp->conn_recv_ancillary.crb_ipv6_recvdstopts = onoff;
1792 		mutex_exit(&connp->conn_lock);
1793 		break;
1794 	case _OLD_IPV6_RECVDSTOPTS:
1795 		mutex_enter(&connp->conn_lock);
1796 		connp->conn_recv_ancillary.crb_old_ipv6_recvdstopts =
1797 		    onoff;
1798 		mutex_exit(&connp->conn_lock);
1799 		break;
1800 	case IPV6_RECVRTHDRDSTOPTS:
1801 		mutex_enter(&connp->conn_lock);
1802 		connp->conn_recv_ancillary.crb_ipv6_recvrthdrdstopts =
1803 		    onoff;
1804 		mutex_exit(&connp->conn_lock);
1805 		break;
1806 	case IPV6_RECVRTHDR:
1807 		mutex_enter(&connp->conn_lock);
1808 		connp->conn_recv_ancillary.crb_ipv6_recvrthdr = onoff;
1809 		mutex_exit(&connp->conn_lock);
1810 		break;
1811 	case IPV6_PKTINFO:
1812 		mutex_enter(&connp->conn_lock);
1813 		if (inlen == 0) {
1814 			ipp->ipp_fields &= ~IPPF_ADDR;
1815 			ipp->ipp_addr = ipv6_all_zeros;
1816 			ixa->ixa_ifindex = 0;
1817 		} else {
1818 			struct in6_pktinfo *pkti;
1819 
1820 			pkti = (struct in6_pktinfo *)invalp;
1821 			ipp->ipp_addr = pkti->ipi6_addr;
1822 			if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr))
1823 				ipp->ipp_fields |= IPPF_ADDR;
1824 			else
1825 				ipp->ipp_fields &= ~IPPF_ADDR;
1826 			ixa->ixa_ifindex = pkti->ipi6_ifindex;
1827 		}
1828 		mutex_exit(&connp->conn_lock);
1829 		/* Source and ifindex might have changed */
1830 		coa->coa_changed |= COA_HEADER_CHANGED;
1831 		coa->coa_changed |= COA_ROUTE_CHANGED;
1832 		break;
1833 	case IPV6_HOPLIMIT:
1834 		mutex_enter(&connp->conn_lock);
1835 		if (inlen == 0 || *i1 == -1) {
1836 			/* Revert to default */
1837 			ipp->ipp_fields &= ~IPPF_HOPLIMIT;
1838 			ixa->ixa_flags &= ~IXAF_NO_TTL_CHANGE;
1839 		} else {
1840 			ipp->ipp_hoplimit = *i1;
1841 			ipp->ipp_fields |= IPPF_HOPLIMIT;
1842 			/* Ensure that it sticks for multicast packets */
1843 			ixa->ixa_flags |= IXAF_NO_TTL_CHANGE;
1844 		}
1845 		mutex_exit(&connp->conn_lock);
1846 		coa->coa_changed |= COA_HEADER_CHANGED;
1847 		break;
1848 	case IPV6_TCLASS:
1849 		/*
1850 		 * IPV6_TCLASS accepts -1 as use kernel default
1851 		 * and [0, 255] as the actualy traffic class.
1852 		 */
1853 		mutex_enter(&connp->conn_lock);
1854 		if (inlen == 0 || *i1 == -1) {
1855 			ipp->ipp_tclass = 0;
1856 			ipp->ipp_fields &= ~IPPF_TCLASS;
1857 		} else {
1858 			ipp->ipp_tclass = *i1;
1859 			ipp->ipp_fields |= IPPF_TCLASS;
1860 		}
1861 		mutex_exit(&connp->conn_lock);
1862 		coa->coa_changed |= COA_HEADER_CHANGED;
1863 		break;
1864 	case IPV6_NEXTHOP:
1865 		if (inlen == 0) {
1866 			ixa->ixa_flags &= ~IXAF_NEXTHOP_SET;
1867 		} else {
1868 			sin6_t *sin6 = (sin6_t *)invalp;
1869 
1870 			ixa->ixa_nexthop_v6 = sin6->sin6_addr;
1871 			if (!IN6_IS_ADDR_UNSPECIFIED(&ixa->ixa_nexthop_v6))
1872 				ixa->ixa_flags |= IXAF_NEXTHOP_SET;
1873 			else
1874 				ixa->ixa_flags &= ~IXAF_NEXTHOP_SET;
1875 		}
1876 		coa->coa_changed |= COA_ROUTE_CHANGED;
1877 		break;
1878 	case IPV6_HOPOPTS:
1879 		mutex_enter(&connp->conn_lock);
1880 		error = optcom_pkt_set(invalp, inlen,
1881 		    (uchar_t **)&ipp->ipp_hopopts, &ipp->ipp_hopoptslen);
1882 		if (error != 0) {
1883 			mutex_exit(&connp->conn_lock);
1884 			return (error);
1885 		}
1886 		if (ipp->ipp_hopoptslen == 0) {
1887 			ipp->ipp_fields &= ~IPPF_HOPOPTS;
1888 		} else {
1889 			ipp->ipp_fields |= IPPF_HOPOPTS;
1890 		}
1891 		mutex_exit(&connp->conn_lock);
1892 		coa->coa_changed |= COA_HEADER_CHANGED;
1893 		coa->coa_changed |= COA_WROFF_CHANGED;
1894 		break;
1895 	case IPV6_RTHDRDSTOPTS:
1896 		mutex_enter(&connp->conn_lock);
1897 		error = optcom_pkt_set(invalp, inlen,
1898 		    (uchar_t **)&ipp->ipp_rthdrdstopts,
1899 		    &ipp->ipp_rthdrdstoptslen);
1900 		if (error != 0) {
1901 			mutex_exit(&connp->conn_lock);
1902 			return (error);
1903 		}
1904 		if (ipp->ipp_rthdrdstoptslen == 0) {
1905 			ipp->ipp_fields &= ~IPPF_RTHDRDSTOPTS;
1906 		} else {
1907 			ipp->ipp_fields |= IPPF_RTHDRDSTOPTS;
1908 		}
1909 		mutex_exit(&connp->conn_lock);
1910 		coa->coa_changed |= COA_HEADER_CHANGED;
1911 		coa->coa_changed |= COA_WROFF_CHANGED;
1912 		break;
1913 	case IPV6_DSTOPTS:
1914 		mutex_enter(&connp->conn_lock);
1915 		error = optcom_pkt_set(invalp, inlen,
1916 		    (uchar_t **)&ipp->ipp_dstopts, &ipp->ipp_dstoptslen);
1917 		if (error != 0) {
1918 			mutex_exit(&connp->conn_lock);
1919 			return (error);
1920 		}
1921 		if (ipp->ipp_dstoptslen == 0) {
1922 			ipp->ipp_fields &= ~IPPF_DSTOPTS;
1923 		} else {
1924 			ipp->ipp_fields |= IPPF_DSTOPTS;
1925 		}
1926 		mutex_exit(&connp->conn_lock);
1927 		coa->coa_changed |= COA_HEADER_CHANGED;
1928 		coa->coa_changed |= COA_WROFF_CHANGED;
1929 		break;
1930 	case IPV6_RTHDR:
1931 		mutex_enter(&connp->conn_lock);
1932 		error = optcom_pkt_set(invalp, inlen,
1933 		    (uchar_t **)&ipp->ipp_rthdr, &ipp->ipp_rthdrlen);
1934 		if (error != 0) {
1935 			mutex_exit(&connp->conn_lock);
1936 			return (error);
1937 		}
1938 		if (ipp->ipp_rthdrlen == 0) {
1939 			ipp->ipp_fields &= ~IPPF_RTHDR;
1940 		} else {
1941 			ipp->ipp_fields |= IPPF_RTHDR;
1942 		}
1943 		mutex_exit(&connp->conn_lock);
1944 		coa->coa_changed |= COA_HEADER_CHANGED;
1945 		coa->coa_changed |= COA_WROFF_CHANGED;
1946 		break;
1947 
1948 	case IPV6_DONTFRAG:
1949 		if (onoff) {
1950 			ixa->ixa_flags |= IXAF_DONTFRAG;
1951 			ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
1952 		} else {
1953 			ixa->ixa_flags &= ~IXAF_DONTFRAG;
1954 			ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
1955 		}
1956 		/* Need to redo ip_attr_connect */
1957 		coa->coa_changed |= COA_ROUTE_CHANGED;
1958 		break;
1959 
1960 	case IPV6_USE_MIN_MTU:
1961 		ixa->ixa_flags |= IXAF_USE_MIN_MTU;
1962 		ixa->ixa_use_min_mtu = *i1;
1963 		/* Need to redo ip_attr_connect */
1964 		coa->coa_changed |= COA_ROUTE_CHANGED;
1965 		break;
1966 
1967 	case IPV6_SEC_OPT:
1968 		mutex_enter(&connp->conn_lock);
1969 		error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp);
1970 		mutex_exit(&connp->conn_lock);
1971 		if (error != 0) {
1972 			return (error);
1973 		}
1974 		/* This is an IPsec policy change - redo ip_attr_connect */
1975 		coa->coa_changed |= COA_ROUTE_CHANGED;
1976 		break;
1977 	case IPV6_SRC_PREFERENCES:
1978 		/*
1979 		 * This socket option only affects connected
1980 		 * sockets that haven't already bound to a specific
1981 		 * IPv6 address.  In other words, sockets that
1982 		 * don't call bind() with an address other than the
1983 		 * unspecified address and that call connect().
1984 		 * ip_set_destination_v6() passes these preferences
1985 		 * to the ipif_select_source_v6() function.
1986 		 */
1987 		mutex_enter(&connp->conn_lock);
1988 		error = ip6_set_src_preferences(ixa, *(uint32_t *)invalp);
1989 		mutex_exit(&connp->conn_lock);
1990 		if (error != 0) {
1991 			return (error);
1992 		}
1993 		break;
1994 	case IPV6_V6ONLY:
1995 		mutex_enter(&connp->conn_lock);
1996 		connp->conn_ipv6_v6only = onoff;
1997 		mutex_exit(&connp->conn_lock);
1998 		break;
1999 	}
2000 	return (0);
2001 }
2002 
2003 /* Handle IPPROTO_UDP */
2004 /* ARGSUSED1 */
2005 static int
2006 conn_opt_set_udp(conn_opt_arg_t *coa, t_scalar_t name, uint_t inlen,
2007     uchar_t *invalp, boolean_t checkonly, cred_t *cr)
2008 {
2009 	conn_t		*connp = coa->coa_connp;
2010 	int		*i1 = (int *)invalp;
2011 	boolean_t	onoff = (*i1 == 0) ? 0 : 1;
2012 	int		error;
2013 
2014 	switch (name) {
2015 	case UDP_ANONPRIVBIND:
2016 		if ((error = secpolicy_net_privaddr(cr, 0, IPPROTO_UDP)) != 0) {
2017 			return (error);
2018 		}
2019 		break;
2020 	}
2021 	if (checkonly)
2022 		return (0);
2023 
2024 	/* Here we set the actual option value */
2025 	mutex_enter(&connp->conn_lock);
2026 	switch (name) {
2027 	case UDP_ANONPRIVBIND:
2028 		connp->conn_anon_priv_bind = onoff;
2029 		break;
2030 	case UDP_EXCLBIND:
2031 		connp->conn_exclbind = onoff;
2032 		break;
2033 	}
2034 	mutex_exit(&connp->conn_lock);
2035 	return (0);
2036 }
2037 
2038 /* Handle IPPROTO_TCP */
2039 /* ARGSUSED1 */
2040 static int
2041 conn_opt_set_tcp(conn_opt_arg_t *coa, t_scalar_t name, uint_t inlen,
2042     uchar_t *invalp, boolean_t checkonly, cred_t *cr)
2043 {
2044 	conn_t		*connp = coa->coa_connp;
2045 	int		*i1 = (int *)invalp;
2046 	boolean_t	onoff = (*i1 == 0) ? 0 : 1;
2047 	int		error;
2048 
2049 	switch (name) {
2050 	case TCP_ANONPRIVBIND:
2051 		if ((error = secpolicy_net_privaddr(cr, 0, IPPROTO_TCP)) != 0) {
2052 			return (error);
2053 		}
2054 		break;
2055 	}
2056 	if (checkonly)
2057 		return (0);
2058 
2059 	/* Here we set the actual option value */
2060 	mutex_enter(&connp->conn_lock);
2061 	switch (name) {
2062 	case TCP_ANONPRIVBIND:
2063 		connp->conn_anon_priv_bind = onoff;
2064 		break;
2065 	case TCP_EXCLBIND:
2066 		connp->conn_exclbind = onoff;
2067 		break;
2068 	case TCP_RECVDSTADDR:
2069 		connp->conn_recv_ancillary.crb_recvdstaddr = onoff;
2070 		break;
2071 	}
2072 	mutex_exit(&connp->conn_lock);
2073 	return (0);
2074 }
2075 
2076 int
2077 conn_getsockname(conn_t *connp, struct sockaddr *sa, uint_t *salenp)
2078 {
2079 	sin_t		*sin;
2080 	sin6_t		*sin6;
2081 
2082 	if (connp->conn_family == AF_INET) {
2083 		if (*salenp < sizeof (sin_t))
2084 			return (EINVAL);
2085 
2086 		*salenp = sizeof (sin_t);
2087 		/* Fill zeroes and then initialize non-zero fields */
2088 		sin = (sin_t *)sa;
2089 		*sin = sin_null;
2090 		sin->sin_family = AF_INET;
2091 		if (!IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_saddr_v6) &&
2092 		    !IN6_IS_ADDR_UNSPECIFIED(&connp->conn_saddr_v6)) {
2093 			sin->sin_addr.s_addr = connp->conn_saddr_v4;
2094 		} else {
2095 			/*
2096 			 * INADDR_ANY
2097 			 * conn_saddr is not set, we might be bound to
2098 			 * broadcast/multicast. Use conn_bound_addr as
2099 			 * local address instead (that could
2100 			 * also still be INADDR_ANY)
2101 			 */
2102 			sin->sin_addr.s_addr = connp->conn_bound_addr_v4;
2103 		}
2104 		sin->sin_port = connp->conn_lport;
2105 	} else {
2106 		if (*salenp < sizeof (sin6_t))
2107 			return (EINVAL);
2108 
2109 		*salenp = sizeof (sin6_t);
2110 		/* Fill zeroes and then initialize non-zero fields */
2111 		sin6 = (sin6_t *)sa;
2112 		*sin6 = sin6_null;
2113 		sin6->sin6_family = AF_INET6;
2114 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_saddr_v6)) {
2115 			sin6->sin6_addr = connp->conn_saddr_v6;
2116 		} else {
2117 			/*
2118 			 * conn_saddr is not set, we might be bound to
2119 			 * broadcast/multicast. Use conn_bound_addr as
2120 			 * local address instead (which could
2121 			 * also still be unspecified)
2122 			 */
2123 			sin6->sin6_addr = connp->conn_bound_addr_v6;
2124 		}
2125 		sin6->sin6_port = connp->conn_lport;
2126 		if (IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr) &&
2127 		    (connp->conn_ixa->ixa_flags & IXAF_SCOPEID_SET))
2128 			sin6->sin6_scope_id = connp->conn_ixa->ixa_scopeid;
2129 	}
2130 	return (0);
2131 }
2132 
2133 int
2134 conn_getpeername(conn_t *connp, struct sockaddr *sa, uint_t *salenp)
2135 {
2136 	struct sockaddr_in	*sin;
2137 	struct sockaddr_in6	*sin6;
2138 
2139 	if (connp->conn_family == AF_INET) {
2140 		if (*salenp < sizeof (sin_t))
2141 			return (EINVAL);
2142 
2143 		*salenp = sizeof (sin_t);
2144 		/* initialize */
2145 		sin = (sin_t *)sa;
2146 		*sin = sin_null;
2147 		sin->sin_family = AF_INET;
2148 		sin->sin_addr.s_addr = connp->conn_faddr_v4;
2149 		sin->sin_port = connp->conn_fport;
2150 	} else {
2151 		if (*salenp < sizeof (sin6_t))
2152 			return (EINVAL);
2153 
2154 		*salenp = sizeof (sin6_t);
2155 		/* initialize */
2156 		sin6 = (sin6_t *)sa;
2157 		*sin6 = sin6_null;
2158 		sin6->sin6_family = AF_INET6;
2159 		sin6->sin6_addr = connp->conn_faddr_v6;
2160 		sin6->sin6_port =  connp->conn_fport;
2161 		sin6->sin6_flowinfo = connp->conn_flowinfo;
2162 		if (IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr) &&
2163 		    (connp->conn_ixa->ixa_flags & IXAF_SCOPEID_SET))
2164 			sin6->sin6_scope_id = connp->conn_ixa->ixa_scopeid;
2165 	}
2166 	return (0);
2167 }
2168 
2169 static uint32_t	cksum_massage_options_v4(ipha_t *, netstack_t *);
2170 static uint32_t cksum_massage_options_v6(ip6_t *, uint_t, netstack_t *);
2171 
2172 /*
2173  * Allocate and fill in conn_ht_iphc based on the current information
2174  * in the conn.
2175  * Normally used when we bind() and connect().
2176  * Returns failure if can't allocate memory, or if there is a problem
2177  * with a routing header/option.
2178  *
2179  * We allocate space for the transport header (ulp_hdr_len + extra) and
2180  * indicate the offset of the ulp header by setting ixa_ip_hdr_length.
2181  * The extra is there for transports that want some spare room for future
2182  * options. conn_ht_iphc_allocated is what was allocated; conn_ht_iphc_len
2183  * excludes the extra part.
2184  *
2185  * We massage an routing option/header and store the ckecksum difference
2186  * in conn_sum.
2187  *
2188  * Caller needs to update conn_wroff if desired.
2189  */
2190 int
2191 conn_build_hdr_template(conn_t *connp, uint_t ulp_hdr_length, uint_t extra,
2192     const in6_addr_t *v6src, const in6_addr_t *v6dst, uint32_t flowinfo)
2193 {
2194 	ip_xmit_attr_t	*ixa = connp->conn_ixa;
2195 	ip_pkt_t	*ipp = &connp->conn_xmit_ipp;
2196 	uint_t		ip_hdr_length;
2197 	uchar_t		*hdrs;
2198 	uint_t		hdrs_len;
2199 
2200 	ASSERT(MUTEX_HELD(&connp->conn_lock));
2201 
2202 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
2203 		ip_hdr_length = ip_total_hdrs_len_v4(ipp);
2204 		/* In case of TX label and IP options it can be too much */
2205 		if (ip_hdr_length > IP_MAX_HDR_LENGTH) {
2206 			/* Preserves existing TX errno for this */
2207 			return (EHOSTUNREACH);
2208 		}
2209 	} else {
2210 		ip_hdr_length = ip_total_hdrs_len_v6(ipp);
2211 	}
2212 	ixa->ixa_ip_hdr_length = ip_hdr_length;
2213 	hdrs_len = ip_hdr_length + ulp_hdr_length + extra;
2214 	ASSERT(hdrs_len != 0);
2215 
2216 	if (hdrs_len != connp->conn_ht_iphc_allocated) {
2217 		/* Allocate new before we free any old */
2218 		hdrs = kmem_alloc(hdrs_len, KM_NOSLEEP);
2219 		if (hdrs == NULL)
2220 			return (ENOMEM);
2221 
2222 		if (connp->conn_ht_iphc != NULL) {
2223 			kmem_free(connp->conn_ht_iphc,
2224 			    connp->conn_ht_iphc_allocated);
2225 		}
2226 		connp->conn_ht_iphc = hdrs;
2227 		connp->conn_ht_iphc_allocated = hdrs_len;
2228 	} else {
2229 		hdrs = connp->conn_ht_iphc;
2230 	}
2231 	hdrs_len -= extra;
2232 	connp->conn_ht_iphc_len = hdrs_len;
2233 
2234 	connp->conn_ht_ulp = hdrs + ip_hdr_length;
2235 	connp->conn_ht_ulp_len = ulp_hdr_length;
2236 
2237 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
2238 		ipha_t	*ipha = (ipha_t *)hdrs;
2239 
2240 		IN6_V4MAPPED_TO_IPADDR(v6src, ipha->ipha_src);
2241 		IN6_V4MAPPED_TO_IPADDR(v6dst, ipha->ipha_dst);
2242 		ip_build_hdrs_v4(hdrs, ip_hdr_length, ipp, connp->conn_proto);
2243 		ipha->ipha_length = htons(hdrs_len);
2244 		if (ixa->ixa_flags & IXAF_PMTU_IPV4_DF)
2245 			ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
2246 		else
2247 			ipha->ipha_fragment_offset_and_flags &= ~IPH_DF_HTONS;
2248 
2249 		if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
2250 			connp->conn_sum = cksum_massage_options_v4(ipha,
2251 			    connp->conn_netstack);
2252 		} else {
2253 			connp->conn_sum = 0;
2254 		}
2255 	} else {
2256 		ip6_t	*ip6h = (ip6_t *)hdrs;
2257 
2258 		ip6h->ip6_src = *v6src;
2259 		ip6h->ip6_dst = *v6dst;
2260 		ip_build_hdrs_v6(hdrs, ip_hdr_length, ipp, connp->conn_proto,
2261 		    flowinfo);
2262 		ip6h->ip6_plen = htons(hdrs_len - IPV6_HDR_LEN);
2263 
2264 		if (ipp->ipp_fields & IPPF_RTHDR) {
2265 			connp->conn_sum = cksum_massage_options_v6(ip6h,
2266 			    ip_hdr_length, connp->conn_netstack);
2267 
2268 			/*
2269 			 * Verify that the first hop isn't a mapped address.
2270 			 * Routers along the path need to do this verification
2271 			 * for subsequent hops.
2272 			 */
2273 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst))
2274 				return (EADDRNOTAVAIL);
2275 
2276 		} else {
2277 			connp->conn_sum = 0;
2278 		}
2279 	}
2280 	return (0);
2281 }
2282 
2283 /*
2284  * Prepend a header template to data_mp based on the ip_pkt_t
2285  * and the passed in source, destination and protocol.
2286  *
2287  * Returns failure if can't allocate memory, in which case data_mp is freed.
2288  * We allocate space for the transport header (ulp_hdr_len) and
2289  * indicate the offset of the ulp header by setting ixa_ip_hdr_length.
2290  *
2291  * We massage an routing option/header and return the ckecksum difference
2292  * in *sump. This is in host byte order.
2293  *
2294  * Caller needs to update conn_wroff if desired.
2295  */
2296 mblk_t *
2297 conn_prepend_hdr(ip_xmit_attr_t *ixa, const ip_pkt_t *ipp,
2298     const in6_addr_t *v6src, const in6_addr_t *v6dst,
2299     uint8_t protocol, uint32_t flowinfo, uint_t ulp_hdr_length, mblk_t *data_mp,
2300     uint_t data_length, uint_t wroff_extra, uint32_t *sump, int *errorp)
2301 {
2302 	uint_t		ip_hdr_length;
2303 	uchar_t		*hdrs;
2304 	uint_t		hdrs_len;
2305 	mblk_t		*mp;
2306 
2307 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
2308 		ip_hdr_length = ip_total_hdrs_len_v4(ipp);
2309 		ASSERT(ip_hdr_length <= IP_MAX_HDR_LENGTH);
2310 	} else {
2311 		ip_hdr_length = ip_total_hdrs_len_v6(ipp);
2312 	}
2313 	hdrs_len = ip_hdr_length + ulp_hdr_length;
2314 	ASSERT(hdrs_len != 0);
2315 
2316 	ixa->ixa_ip_hdr_length = ip_hdr_length;
2317 
2318 	/* Can we prepend to data_mp? */
2319 	if (data_mp != NULL &&
2320 	    data_mp->b_rptr - data_mp->b_datap->db_base >= hdrs_len &&
2321 	    data_mp->b_datap->db_ref == 1) {
2322 		hdrs = data_mp->b_rptr - hdrs_len;
2323 		data_mp->b_rptr = hdrs;
2324 		mp = data_mp;
2325 	} else {
2326 		mp = allocb(hdrs_len + wroff_extra, BPRI_MED);
2327 		if (mp == NULL) {
2328 			freemsg(data_mp);
2329 			*errorp = ENOMEM;
2330 			return (NULL);
2331 		}
2332 		mp->b_wptr = mp->b_datap->db_lim;
2333 		hdrs = mp->b_rptr = mp->b_wptr - hdrs_len;
2334 		mp->b_cont = data_mp;
2335 	}
2336 
2337 	/*
2338 	 * Set the source in the header. ip_build_hdrs_v4/v6 will overwrite it
2339 	 * if PKTINFO (aka IPPF_ADDR) was set.
2340 	 */
2341 	if (ixa->ixa_flags & IXAF_IS_IPV4) {
2342 		ipha_t *ipha = (ipha_t *)hdrs;
2343 
2344 		ASSERT(IN6_IS_ADDR_V4MAPPED(v6dst));
2345 		IN6_V4MAPPED_TO_IPADDR(v6src, ipha->ipha_src);
2346 		IN6_V4MAPPED_TO_IPADDR(v6dst, ipha->ipha_dst);
2347 		ip_build_hdrs_v4(hdrs, ip_hdr_length, ipp, protocol);
2348 		ipha->ipha_length = htons(hdrs_len + data_length);
2349 		if (ixa->ixa_flags & IXAF_PMTU_IPV4_DF)
2350 			ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
2351 		else
2352 			ipha->ipha_fragment_offset_and_flags &= ~IPH_DF_HTONS;
2353 
2354 		if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
2355 			*sump = cksum_massage_options_v4(ipha,
2356 			    ixa->ixa_ipst->ips_netstack);
2357 		} else {
2358 			*sump = 0;
2359 		}
2360 	} else {
2361 		ip6_t *ip6h = (ip6_t *)hdrs;
2362 
2363 		ip6h->ip6_src = *v6src;
2364 		ip6h->ip6_dst = *v6dst;
2365 		ip_build_hdrs_v6(hdrs, ip_hdr_length, ipp, protocol, flowinfo);
2366 		ip6h->ip6_plen = htons(hdrs_len + data_length - IPV6_HDR_LEN);
2367 
2368 		if (ipp->ipp_fields & IPPF_RTHDR) {
2369 			*sump = cksum_massage_options_v6(ip6h,
2370 			    ip_hdr_length, ixa->ixa_ipst->ips_netstack);
2371 
2372 			/*
2373 			 * Verify that the first hop isn't a mapped address.
2374 			 * Routers along the path need to do this verification
2375 			 * for subsequent hops.
2376 			 */
2377 			if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) {
2378 				*errorp = EADDRNOTAVAIL;
2379 				freemsg(mp);
2380 				return (NULL);
2381 			}
2382 		} else {
2383 			*sump = 0;
2384 		}
2385 	}
2386 	return (mp);
2387 }
2388 
2389 /*
2390  * Massage a source route if any putting the first hop
2391  * in ipha_dst. Compute a starting value for the checksum which
2392  * takes into account that the original ipha_dst should be
2393  * included in the checksum but that IP will include the
2394  * first hop from the source route in the tcp checksum.
2395  */
2396 static uint32_t
2397 cksum_massage_options_v4(ipha_t *ipha, netstack_t *ns)
2398 {
2399 	in_addr_t	dst;
2400 	uint32_t	cksum;
2401 
2402 	/* Get last hop then diff against first hop */
2403 	cksum = ip_massage_options(ipha, ns);
2404 	cksum = (cksum & 0xFFFF) + (cksum >> 16);
2405 	dst = ipha->ipha_dst;
2406 	cksum -= ((dst >> 16) + (dst & 0xffff));
2407 	if ((int)cksum < 0)
2408 		cksum--;
2409 	cksum = (cksum & 0xFFFF) + (cksum >> 16);
2410 	cksum = (cksum & 0xFFFF) + (cksum >> 16);
2411 	ASSERT(cksum < 0x10000);
2412 	return (ntohs(cksum));
2413 }
2414 
2415 static uint32_t
2416 cksum_massage_options_v6(ip6_t *ip6h, uint_t ip_hdr_len, netstack_t *ns)
2417 {
2418 	uint8_t		*end;
2419 	ip6_rthdr_t	*rth;
2420 	uint32_t	cksum;
2421 
2422 	end = (uint8_t *)ip6h + ip_hdr_len;
2423 	rth = ip_find_rthdr_v6(ip6h, end);
2424 	if (rth == NULL)
2425 		return (0);
2426 
2427 	cksum = ip_massage_options_v6(ip6h, rth, ns);
2428 	cksum = (cksum & 0xFFFF) + (cksum >> 16);
2429 	ASSERT(cksum < 0x10000);
2430 	return (ntohs(cksum));
2431 }
2432 
2433 /*
2434  * ULPs that change the destination address need to call this for each
2435  * change to discard any state about a previous destination that might
2436  * have been multicast or multirt.
2437  */
2438 void
2439 ip_attr_newdst(ip_xmit_attr_t *ixa)
2440 {
2441 	ixa->ixa_flags &= ~(IXAF_LOOPBACK_COPY | IXAF_NO_HW_CKSUM |
2442 	    IXAF_NO_TTL_CHANGE | IXAF_IPV6_ADD_FRAGHDR |
2443 	    IXAF_NO_LOOP_ZONEID_SET);
2444 }
2445 
2446 /*
2447  * Determine the nexthop which will be used.
2448  * Normally this is just the destination, but if a IPv4 source route, or
2449  * IPv6 routing header, is in the ip_pkt_t then we extract the nexthop from
2450  * there.
2451  */
2452 void
2453 ip_attr_nexthop(const ip_pkt_t *ipp, const ip_xmit_attr_t *ixa,
2454     const in6_addr_t *dst, in6_addr_t *nexthop)
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 = 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 	econnp->conn_sndbuf = lconnp->conn_sndbuf;
2918 	econnp->conn_rcvbuf = lconnp->conn_rcvbuf;
2919 	econnp->conn_sndlowat = lconnp->conn_sndlowat;
2920 	econnp->conn_rcvlowat = lconnp->conn_rcvlowat;
2921 	econnp->conn_dgram_errind = lconnp->conn_dgram_errind;
2922 	econnp->conn_oobinline = lconnp->conn_oobinline;
2923 	econnp->conn_debug = lconnp->conn_debug;
2924 	econnp->conn_keepalive = lconnp->conn_keepalive;
2925 	econnp->conn_linger = lconnp->conn_linger;
2926 	econnp->conn_lingertime = lconnp->conn_lingertime;
2927 
2928 	/* Set the IP options */
2929 	econnp->conn_broadcast = lconnp->conn_broadcast;
2930 	econnp->conn_useloopback = lconnp->conn_useloopback;
2931 	econnp->conn_reuseaddr = lconnp->conn_reuseaddr;
2932 	return (0);
2933 }
2934