xref: /titanic_53/usr/src/uts/common/inet/optcom.c (revision ff550d0e264b51131fb34e9e83163b348d916640)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*ff550d0eSmasputra  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * This file contains common code for handling Options Management requests.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stream.h>
367c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
377c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
387c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
417c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
427c478bd9Sstevel@tonic-gate #include <sys/timod.h>
437c478bd9Sstevel@tonic-gate #include <sys/socket.h>
447c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
467c478bd9Sstevel@tonic-gate #include <sys/debug.h>		/* for ASSERT */
477c478bd9Sstevel@tonic-gate #include <sys/policy.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <inet/common.h>
507c478bd9Sstevel@tonic-gate #include <inet/mi.h>
517c478bd9Sstevel@tonic-gate #include <inet/nd.h>
527c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
537c478bd9Sstevel@tonic-gate #include <inet/ip.h>
547c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
557c478bd9Sstevel@tonic-gate #include <netinet/in.h>
567c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
577c478bd9Sstevel@tonic-gate #include <netinet/ip_mroute.h>
587c478bd9Sstevel@tonic-gate #include "optcom.h"
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * Function prototypes
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate static t_scalar_t process_topthdrs_first_pass(mblk_t *, cred_t *, optdb_obj_t *,
667c478bd9Sstevel@tonic-gate     boolean_t *, size_t *);
677c478bd9Sstevel@tonic-gate static t_scalar_t do_options_second_pass(queue_t *q, mblk_t *reqmp,
687c478bd9Sstevel@tonic-gate     mblk_t *ack_mp, cred_t *, optdb_obj_t *dbobjp,
697c478bd9Sstevel@tonic-gate     mblk_t *first_mp, boolean_t is_restart, boolean_t *queued_statusp);
707c478bd9Sstevel@tonic-gate static t_uscalar_t get_worst_status(t_uscalar_t, t_uscalar_t);
717c478bd9Sstevel@tonic-gate static int do_opt_default(queue_t *, struct T_opthdr *, uchar_t **,
727c478bd9Sstevel@tonic-gate     t_uscalar_t *, cred_t *, optdb_obj_t *);
737c478bd9Sstevel@tonic-gate static void do_opt_current(queue_t *, struct T_opthdr *, uchar_t **,
747c478bd9Sstevel@tonic-gate     t_uscalar_t *, cred_t *cr, optdb_obj_t *);
757c478bd9Sstevel@tonic-gate static int do_opt_check_or_negotiate(queue_t *q, struct T_opthdr *reqopt,
767c478bd9Sstevel@tonic-gate     uint_t optset_context, uchar_t **resptrp, t_uscalar_t *worst_statusp,
777c478bd9Sstevel@tonic-gate     cred_t *, optdb_obj_t *dbobjp, mblk_t *first_mp);
787c478bd9Sstevel@tonic-gate static opdes_t *opt_chk_lookup(t_uscalar_t, t_uscalar_t, opdes_t *, uint_t);
797c478bd9Sstevel@tonic-gate static boolean_t opt_level_valid(t_uscalar_t, optlevel_t *, uint_t);
807c478bd9Sstevel@tonic-gate static size_t opt_level_allopts_lengths(t_uscalar_t, opdes_t *, uint_t);
817c478bd9Sstevel@tonic-gate static boolean_t opt_length_ok(opdes_t *, struct T_opthdr *);
827c478bd9Sstevel@tonic-gate static t_uscalar_t optcom_max_optbuf_len(opdes_t *, uint_t);
837c478bd9Sstevel@tonic-gate static boolean_t opt_bloated_maxsize(opdes_t *);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /* Common code for sending back a T_ERROR_ACK. */
867c478bd9Sstevel@tonic-gate void
877c478bd9Sstevel@tonic-gate optcom_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
907c478bd9Sstevel@tonic-gate 		qreply(q, mp);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * The option management routines svr4_optcom_req() and tpi_optcom_req() use
957c478bd9Sstevel@tonic-gate  * callback functions as arguments. Here is the expected interfaces
967c478bd9Sstevel@tonic-gate  * assumed from the callback functions
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  *
997c478bd9Sstevel@tonic-gate  * (1) deffn(q, optlevel, optname, optvalp)
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  *	- Function only called when default value comes from protocol
1027c478bd9Sstevel@tonic-gate  *	 specific code and not the option database table (indicated by
1037c478bd9Sstevel@tonic-gate  *	  OP_DEF_FN property in option database.)
1047c478bd9Sstevel@tonic-gate  *	- Error return is -1. Valid returns are >=0.
1057c478bd9Sstevel@tonic-gate  *	- When valid, the return value represents the length used for storing
1067c478bd9Sstevel@tonic-gate  *		the default value of the option.
1077c478bd9Sstevel@tonic-gate  *      - Error return implies the called routine did not recognize this
1087c478bd9Sstevel@tonic-gate  *              option. Something downstream could so input is left unchanged
1097c478bd9Sstevel@tonic-gate  *              in request buffer.
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  * (2) getfn(q, optlevel, optname, optvalp)
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  *	- Error return is -1. Valid returns are >=0.
1147c478bd9Sstevel@tonic-gate  *	- When valid, the return value represents the length used for storing
1157c478bd9Sstevel@tonic-gate  *		the actual value of the option.
1167c478bd9Sstevel@tonic-gate  *      - Error return implies the called routine did not recognize this
1177c478bd9Sstevel@tonic-gate  *              option. Something downstream could so input is left unchanged
1187c478bd9Sstevel@tonic-gate  *              in request buffer.
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * (3) setfn(q, optset_context, optlevel, optname, inlen, invalp,
1217c478bd9Sstevel@tonic-gate  *	outlenp, outvalp, attrp, cr);
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  *	- OK return is 0, Error code is returned as a non-zero argument.
1247c478bd9Sstevel@tonic-gate  *      - If negative it is ignored by svr4_optcom_req(). If positive, error
1257c478bd9Sstevel@tonic-gate  *        is returned. A negative return implies that option, while handled on
1267c478bd9Sstevel@tonic-gate  *	  this stack is not handled at this level and will be handled further
1277c478bd9Sstevel@tonic-gate  *	  downstream.
1287c478bd9Sstevel@tonic-gate  *	- Both negative and positive errors are treats as errors in an
1297c478bd9Sstevel@tonic-gate  *	  identical manner by tpi_optcom_req(). The errors affect "status"
1307c478bd9Sstevel@tonic-gate  *	  field of each option's T_opthdr. If sucessfull, an appropriate sucess
1317c478bd9Sstevel@tonic-gate  *	  result is carried. If error, it instantiated to "failure" at the
1327c478bd9Sstevel@tonic-gate  *	  topmost level and left unchanged at other levels. (This "failure" can
1337c478bd9Sstevel@tonic-gate  *	  turn to a success at another level).
1347c478bd9Sstevel@tonic-gate  *	- optset_context passed for tpi_optcom_req(). It is interpreted as:
1357c478bd9Sstevel@tonic-gate  *        - SETFN_OPTCOM_CHECKONLY
1367c478bd9Sstevel@tonic-gate  *		semantics are to pretend to set the value and report
1377c478bd9Sstevel@tonic-gate  *		back if it would be successful.
1387c478bd9Sstevel@tonic-gate  *		This is used with T_CHECK semantics in XTI
1397c478bd9Sstevel@tonic-gate  *        - SETFN_OPTCOM_NEGOTIATE
1407c478bd9Sstevel@tonic-gate  *		set the value. Call from option management primitive
1417c478bd9Sstevel@tonic-gate  *		T_OPTMGMT_REQ when T_NEGOTIATE flags is used.
1427c478bd9Sstevel@tonic-gate  *	  - SETFN_UD_NEGOTIATE
1437c478bd9Sstevel@tonic-gate  *		option request came riding on UNITDATA primitive most often
1447c478bd9Sstevel@tonic-gate  *		has  "this datagram" semantics to influence prpoerties
1457c478bd9Sstevel@tonic-gate  *		affecting an outgoig datagram or associated with recived
1467c478bd9Sstevel@tonic-gate  *		datagram
1477c478bd9Sstevel@tonic-gate  *		[ Note: XTI permits this use outside of "this datagram"
1487c478bd9Sstevel@tonic-gate  *		semantics also and permits setting "management related"
1497c478bd9Sstevel@tonic-gate  *		options in this	context and its test suite enforces it ]
1507c478bd9Sstevel@tonic-gate  *	  - SETFN_CONN_NEGOTATE
1517c478bd9Sstevel@tonic-gate  *		option request came riding on CONN_REQ/RES primitive and
1527c478bd9Sstevel@tonic-gate  *		most often has "this connection" (negotiation during
1537c478bd9Sstevel@tonic-gate  *		"connection estblishment") semantics.
1547c478bd9Sstevel@tonic-gate  *		[ Note: XTI permits use of these outside of "this connection"
1557c478bd9Sstevel@tonic-gate  *		semantics and permits "management related" options in this
1567c478bd9Sstevel@tonic-gate  *		context and its test suite enforces it. ]
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  *	- inlen, invalp is the option length,value requested to be set.
1597c478bd9Sstevel@tonic-gate  *	- outlenp, outvalp represent return parameters which contain the
1607c478bd9Sstevel@tonic-gate  *	  value set and it might be different from one passed on input.
1617c478bd9Sstevel@tonic-gate  *	- attrp points to a data structure that's used by v6 modules to
1627c478bd9Sstevel@tonic-gate  *	  store ancillary data options or sticky options.
1637c478bd9Sstevel@tonic-gate  *	- cr points to the caller's credentials
1647c478bd9Sstevel@tonic-gate  *	- the caller might pass same buffers for input and output and the
1657c478bd9Sstevel@tonic-gate  *	  routine should protect against this case by not updating output
1667c478bd9Sstevel@tonic-gate  *	  buffers until it is done referencing input buffers and any other
1677c478bd9Sstevel@tonic-gate  *	  issues (e.g. not use bcopy() if we do not trust what it does).
1687c478bd9Sstevel@tonic-gate  *      - If option is not known, it returns error. We randomly pick EINVAL.
1697c478bd9Sstevel@tonic-gate  *        It can however get called with options that are handled downstream
1707c478bd9Sstevel@tonic-gate  *        opr upstream so for svr4_optcom_req(), it does not return error for
1717c478bd9Sstevel@tonic-gate  *        negative return values.
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * Upper Level Protocols call this routine when they receive
1777c478bd9Sstevel@tonic-gate  * a T_SVR4_OPTMGMT_REQ message.  They supply callback functions
1787c478bd9Sstevel@tonic-gate  * for setting a new value for a single options, getting the
1797c478bd9Sstevel@tonic-gate  * current value for a single option, and checking for support
1807c478bd9Sstevel@tonic-gate  * of a single option.  svr4_optcom_req validates the option management
1817c478bd9Sstevel@tonic-gate  * buffer passed in, and calls the appropriate routines to do the
1827c478bd9Sstevel@tonic-gate  * job requested.
1837c478bd9Sstevel@tonic-gate  * XXX Code below needs some restructuring after we have some more
1847c478bd9Sstevel@tonic-gate  * macros to support 'struct opthdr' in the headers.
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * IP-MT notes: The option management framework functions svr4_optcom_req() and
1877c478bd9Sstevel@tonic-gate  * tpi_optcom_req() allocate and prepend an M_CTL mblk to the actual
1887c478bd9Sstevel@tonic-gate  * T_optmgmt_req mblk and pass the chain as an additional parameter to the
1897c478bd9Sstevel@tonic-gate  * protocol set functions. If a protocol set function (such as ip_opt_set)
1907c478bd9Sstevel@tonic-gate  * cannot process the option immediately it can return EINPROGRESS. ip_opt_set
1917c478bd9Sstevel@tonic-gate  * enqueues the message in the appropriate sq and returns EINPROGRESS. Later
1927c478bd9Sstevel@tonic-gate  * the sq framework arranges to restart this operation and passes control to
1937c478bd9Sstevel@tonic-gate  * the restart function ip_restart_optmgmt() which in turn calls
1947c478bd9Sstevel@tonic-gate  * svr4_optcom_req() or tpi_optcom_req() to restart the option processing.
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate int
1977c478bd9Sstevel@tonic-gate svr4_optcom_req(queue_t *q, mblk_t *mp, cred_t *cr, optdb_obj_t *dbobjp)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	pfi_t	deffn = dbobjp->odb_deffn;
2007c478bd9Sstevel@tonic-gate 	pfi_t	getfn = dbobjp->odb_getfn;
2017c478bd9Sstevel@tonic-gate 	opt_set_fn setfn = dbobjp->odb_setfn;
2027c478bd9Sstevel@tonic-gate 	opdes_t	*opt_arr = dbobjp->odb_opt_des_arr;
2037c478bd9Sstevel@tonic-gate 	uint_t opt_arr_cnt = dbobjp->odb_opt_arr_cnt;
2047c478bd9Sstevel@tonic-gate 	boolean_t topmost_tpiprovider = dbobjp->odb_topmost_tpiprovider;
2057c478bd9Sstevel@tonic-gate 	opt_restart_t *or;
2067c478bd9Sstevel@tonic-gate 	struct opthdr *restart_opt;
2077c478bd9Sstevel@tonic-gate 	boolean_t is_restart = B_FALSE;
2087c478bd9Sstevel@tonic-gate 	mblk_t	*first_mp;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	t_uscalar_t max_optbuf_len;
2117c478bd9Sstevel@tonic-gate 	int len;
2127c478bd9Sstevel@tonic-gate 	mblk_t	*mp1 = NULL;
2137c478bd9Sstevel@tonic-gate 	struct opthdr *next_opt;
2147c478bd9Sstevel@tonic-gate 	struct opthdr *opt;
2157c478bd9Sstevel@tonic-gate 	struct opthdr *opt1;
2167c478bd9Sstevel@tonic-gate 	struct opthdr *opt_end;
2177c478bd9Sstevel@tonic-gate 	struct opthdr *opt_start;
2187c478bd9Sstevel@tonic-gate 	opdes_t	*optd;
2197c478bd9Sstevel@tonic-gate 	boolean_t	pass_to_next = B_FALSE;
2207c478bd9Sstevel@tonic-gate 	boolean_t	pass_to_ip = B_FALSE;
221*ff550d0eSmasputra 	boolean_t	is_tcp;
2227c478bd9Sstevel@tonic-gate 	struct T_optmgmt_ack *toa;
2237c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req *tor;
2247c478bd9Sstevel@tonic-gate 
225*ff550d0eSmasputra 	is_tcp = (dbobjp == &tcp_opt_obj);
226*ff550d0eSmasputra 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * Allocate M_CTL and prepend to the packet for restarting this
2297c478bd9Sstevel@tonic-gate 	 * option if needed. IP may need to queue and restart the option
2307c478bd9Sstevel@tonic-gate 	 * if it cannot obtain exclusive conditions immediately. Please see
2317c478bd9Sstevel@tonic-gate 	 * IP-MT notes before the start of svr4_optcom_req
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_CTL) {
2347c478bd9Sstevel@tonic-gate 		is_restart = B_TRUE;
2357c478bd9Sstevel@tonic-gate 		first_mp = mp;
2367c478bd9Sstevel@tonic-gate 		mp = mp->b_cont;
2377c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_wptr - mp->b_rptr >=
2387c478bd9Sstevel@tonic-gate 		    sizeof (struct T_optmgmt_req));
2397c478bd9Sstevel@tonic-gate 		tor = (struct T_optmgmt_req *)mp->b_rptr;
2407c478bd9Sstevel@tonic-gate 		ASSERT(tor->MGMT_flags == T_NEGOTIATE);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		or = (opt_restart_t *)first_mp->b_rptr;
2437c478bd9Sstevel@tonic-gate 		opt_start = or->or_start;
2447c478bd9Sstevel@tonic-gate 		opt_end = or->or_end;
2457c478bd9Sstevel@tonic-gate 		restart_opt = or->or_ropt;
2467c478bd9Sstevel@tonic-gate 		goto restart;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	tor = (struct T_optmgmt_req *)mp->b_rptr;
2507c478bd9Sstevel@tonic-gate 	/* Verify message integrity. */
2517c478bd9Sstevel@tonic-gate 	if (mp->b_wptr - mp->b_rptr < sizeof (struct T_optmgmt_req))
2527c478bd9Sstevel@tonic-gate 		goto bad_opt;
2537c478bd9Sstevel@tonic-gate 	/* Verify MGMT_flags legal */
2547c478bd9Sstevel@tonic-gate 	switch (tor->MGMT_flags) {
2557c478bd9Sstevel@tonic-gate 	case T_DEFAULT:
2567c478bd9Sstevel@tonic-gate 	case T_NEGOTIATE:
2577c478bd9Sstevel@tonic-gate 	case T_CURRENT:
2587c478bd9Sstevel@tonic-gate 	case T_CHECK:
2597c478bd9Sstevel@tonic-gate 		/* OK - legal request flags */
2607c478bd9Sstevel@tonic-gate 		break;
2617c478bd9Sstevel@tonic-gate 	default:
2627c478bd9Sstevel@tonic-gate 		optcom_err_ack(q, mp, TBADFLAG, 0);
2637c478bd9Sstevel@tonic-gate 		return (0);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 	if (tor->MGMT_flags == T_DEFAULT) {
2667c478bd9Sstevel@tonic-gate 		/* Is it a request for default option settings? */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 		/*
2697c478bd9Sstevel@tonic-gate 		 * Note: XXX TLI and TPI specification was unclear about
2707c478bd9Sstevel@tonic-gate 		 * semantics of T_DEFAULT and the following historical note
2717c478bd9Sstevel@tonic-gate 		 * and its interpretation is incorrect (it implies a request
2727c478bd9Sstevel@tonic-gate 		 * for default values of only the identified options not all.
2737c478bd9Sstevel@tonic-gate 		 * The semantics have been explained better in XTI spec.)
2747c478bd9Sstevel@tonic-gate 		 * However, we do not modify (comment or code) here to keep
2757c478bd9Sstevel@tonic-gate 		 * compatibility.
2767c478bd9Sstevel@tonic-gate 		 * We can rethink this if it ever becomes an issue.
2777c478bd9Sstevel@tonic-gate 		 * ----historical comment start------
2787c478bd9Sstevel@tonic-gate 		 * As we understand it, the input buffer is meaningless
2797c478bd9Sstevel@tonic-gate 		 * so we ditch the message.  A T_DEFAULT request is a
2807c478bd9Sstevel@tonic-gate 		 * request to obtain a buffer containing defaults for
2817c478bd9Sstevel@tonic-gate 		 * all supported options, so we allocate a maximum length
2827c478bd9Sstevel@tonic-gate 		 * reply.
2837c478bd9Sstevel@tonic-gate 		 * ----historical comment end -------
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		/* T_DEFAULT not passed down */
2867c478bd9Sstevel@tonic-gate 		ASSERT(topmost_tpiprovider == B_TRUE);
2877c478bd9Sstevel@tonic-gate 		freemsg(mp);
2887c478bd9Sstevel@tonic-gate 		max_optbuf_len = optcom_max_optbuf_len(opt_arr,
2897c478bd9Sstevel@tonic-gate 		    opt_arr_cnt);
2907c478bd9Sstevel@tonic-gate 		mp = allocb(max_optbuf_len, BPRI_MED);
2917c478bd9Sstevel@tonic-gate 		if (!mp) {
2927c478bd9Sstevel@tonic-gate no_mem:;
2937c478bd9Sstevel@tonic-gate 			optcom_err_ack(q, mp, TSYSERR, ENOMEM);
2947c478bd9Sstevel@tonic-gate 			return (0);
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		/* Initialize the T_optmgmt_ack header. */
2987c478bd9Sstevel@tonic-gate 		toa = (struct T_optmgmt_ack *)mp->b_rptr;
2997c478bd9Sstevel@tonic-gate 		bzero((char *)toa, max_optbuf_len);
3007c478bd9Sstevel@tonic-gate 		toa->PRIM_type = T_OPTMGMT_ACK;
3017c478bd9Sstevel@tonic-gate 		toa->OPT_offset = (t_scalar_t)sizeof (struct T_optmgmt_ack);
3027c478bd9Sstevel@tonic-gate 		/* TODO: Is T_DEFAULT the right thing to put in MGMT_flags? */
3037c478bd9Sstevel@tonic-gate 		toa->MGMT_flags = T_DEFAULT;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		/* Now walk the table of options passed in */
3067c478bd9Sstevel@tonic-gate 		opt = (struct opthdr *)&toa[1];
3077c478bd9Sstevel@tonic-gate 		for (optd = opt_arr; optd < &opt_arr[opt_arr_cnt]; optd++) {
3087c478bd9Sstevel@tonic-gate 			/*
3097c478bd9Sstevel@tonic-gate 			 * All the options in the table of options passed
3107c478bd9Sstevel@tonic-gate 			 * in are by definition supported by the protocol
3117c478bd9Sstevel@tonic-gate 			 * calling this function.
3127c478bd9Sstevel@tonic-gate 			 */
3137c478bd9Sstevel@tonic-gate 			if (!OA_READ_PERMISSION(optd, cr))
3147c478bd9Sstevel@tonic-gate 				continue;
3157c478bd9Sstevel@tonic-gate 			opt->level = optd->opdes_level;
3167c478bd9Sstevel@tonic-gate 			opt->name = optd->opdes_name;
3177c478bd9Sstevel@tonic-gate 			if (!(optd->opdes_props & OP_DEF_FN) ||
3187c478bd9Sstevel@tonic-gate 			    ((len = (*deffn)(q, opt->level,
3197c478bd9Sstevel@tonic-gate 				opt->name, (uchar_t *)&opt[1])) < 0)) {
3207c478bd9Sstevel@tonic-gate 				/*
3217c478bd9Sstevel@tonic-gate 				 * Fill length and value from table.
3227c478bd9Sstevel@tonic-gate 				 *
3237c478bd9Sstevel@tonic-gate 				 * Default value not instantiated from function
3247c478bd9Sstevel@tonic-gate 				 * (or the protocol specific function failed it;
3257c478bd9Sstevel@tonic-gate 				 * In this interpretation of T_DEFAULT, this is
3267c478bd9Sstevel@tonic-gate 				 * the best we can do)
3277c478bd9Sstevel@tonic-gate 				 */
3287c478bd9Sstevel@tonic-gate 				switch (optd->opdes_size) {
3297c478bd9Sstevel@tonic-gate 				/*
3307c478bd9Sstevel@tonic-gate 				 * Since options are guaranteed aligned only
3317c478bd9Sstevel@tonic-gate 				 * on a 4 byte boundary (t_scalar_t) any
3327c478bd9Sstevel@tonic-gate 				 * option that is greater in size will default
3337c478bd9Sstevel@tonic-gate 				 * to the bcopy below
3347c478bd9Sstevel@tonic-gate 				 */
3357c478bd9Sstevel@tonic-gate 				case sizeof (int32_t):
3367c478bd9Sstevel@tonic-gate 					*(int32_t *)&opt[1] =
3377c478bd9Sstevel@tonic-gate 					    (int32_t)optd->opdes_default;
3387c478bd9Sstevel@tonic-gate 					break;
3397c478bd9Sstevel@tonic-gate 				case sizeof (int16_t):
3407c478bd9Sstevel@tonic-gate 					*(int16_t *)&opt[1] =
3417c478bd9Sstevel@tonic-gate 					    (int16_t)optd->opdes_default;
3427c478bd9Sstevel@tonic-gate 					break;
3437c478bd9Sstevel@tonic-gate 				case sizeof (int8_t):
3447c478bd9Sstevel@tonic-gate 					*(int8_t *)&opt[1] =
3457c478bd9Sstevel@tonic-gate 					    (int8_t)optd->opdes_default;
3467c478bd9Sstevel@tonic-gate 					break;
3477c478bd9Sstevel@tonic-gate 				default:
3487c478bd9Sstevel@tonic-gate 					/*
3497c478bd9Sstevel@tonic-gate 					 * other length but still assume
3507c478bd9Sstevel@tonic-gate 					 * fixed - use bcopy
3517c478bd9Sstevel@tonic-gate 					 */
3527c478bd9Sstevel@tonic-gate 					bcopy(optd->opdes_defbuf,
3537c478bd9Sstevel@tonic-gate 					    &opt[1], optd->opdes_size);
3547c478bd9Sstevel@tonic-gate 					break;
3557c478bd9Sstevel@tonic-gate 				}
3567c478bd9Sstevel@tonic-gate 				opt->len = optd->opdes_size;
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 			else
3597c478bd9Sstevel@tonic-gate 				opt->len = (t_uscalar_t)len;
3607c478bd9Sstevel@tonic-gate 			opt = (struct opthdr *)((char *)&opt[1] +
3617c478bd9Sstevel@tonic-gate 			    _TPI_ALIGN_OPT(opt->len));
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		/* Now record the final length. */
3657c478bd9Sstevel@tonic-gate 		toa->OPT_length = (t_scalar_t)((char *)opt - (char *)&toa[1]);
3667c478bd9Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)opt;
3677c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_PCPROTO;
3687c478bd9Sstevel@tonic-gate 		/* Ship it back. */
3697c478bd9Sstevel@tonic-gate 		qreply(q, mp);
3707c478bd9Sstevel@tonic-gate 		return (0);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	/* T_DEFAULT processing complete - no more T_DEFAULT */
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * For T_NEGOTIATE, T_CURRENT, and T_CHECK requests, we make a
3767c478bd9Sstevel@tonic-gate 	 * pass through the input buffer validating the details and
3777c478bd9Sstevel@tonic-gate 	 * making sure each option is supported by the protocol.
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	if ((opt_start = (struct opthdr *)mi_offset_param(mp,
3807c478bd9Sstevel@tonic-gate 	    tor->OPT_offset, tor->OPT_length)) == NULL)
3817c478bd9Sstevel@tonic-gate 		goto bad_opt;
3827c478bd9Sstevel@tonic-gate 	if (!__TPI_OPT_ISALIGNED(opt_start))
3837c478bd9Sstevel@tonic-gate 		goto bad_opt;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	opt_end = (struct opthdr *)((uchar_t *)opt_start +
3867c478bd9Sstevel@tonic-gate 	    tor->OPT_length);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	for (opt = opt_start; opt < opt_end; opt = next_opt) {
3897c478bd9Sstevel@tonic-gate 		/*
3907c478bd9Sstevel@tonic-gate 		 * Verify we have room to reference the option header
3917c478bd9Sstevel@tonic-gate 		 * fields in the option buffer.
3927c478bd9Sstevel@tonic-gate 		 */
3937c478bd9Sstevel@tonic-gate 		if ((uchar_t *)opt + sizeof (struct opthdr) >
3947c478bd9Sstevel@tonic-gate 		    (uchar_t *)opt_end)
3957c478bd9Sstevel@tonic-gate 			goto bad_opt;
3967c478bd9Sstevel@tonic-gate 		/*
3977c478bd9Sstevel@tonic-gate 		 * We now compute pointer to next option in buffer 'next_opt'
3987c478bd9Sstevel@tonic-gate 		 * The next_opt computation above below 'opt->len' initialized
3997c478bd9Sstevel@tonic-gate 		 * by application which cannot be trusted. The usual value
4007c478bd9Sstevel@tonic-gate 		 * too large will be captured by the loop termination condition
4017c478bd9Sstevel@tonic-gate 		 * above. We check for the following which it will miss.
4027c478bd9Sstevel@tonic-gate 		 * 	-pointer space wraparound arithmetic overflow
4037c478bd9Sstevel@tonic-gate 		 *	-last option in buffer with 'opt->len' being too large
4047c478bd9Sstevel@tonic-gate 		 *	 (only reason 'next_opt' should equal or exceed
4057c478bd9Sstevel@tonic-gate 		 *	 'opt_end' for last option is roundup unless length is
4067c478bd9Sstevel@tonic-gate 		 *	 too-large/invalid)
4077c478bd9Sstevel@tonic-gate 		 */
4087c478bd9Sstevel@tonic-gate 		next_opt = (struct opthdr *)((uchar_t *)&opt[1] +
4097c478bd9Sstevel@tonic-gate 		    _TPI_ALIGN_OPT(opt->len));
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 		if ((uchar_t *)next_opt < (uchar_t *)&opt[1] ||
4127c478bd9Sstevel@tonic-gate 		    ((next_opt >= opt_end) &&
4137c478bd9Sstevel@tonic-gate 			(((uchar_t *)next_opt - (uchar_t *)opt_end) >=
4147c478bd9Sstevel@tonic-gate 			    __TPI_ALIGN_SIZE)))
4157c478bd9Sstevel@tonic-gate 			goto bad_opt;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		/* sanity check */
4187c478bd9Sstevel@tonic-gate 		if (opt->name == T_ALLOPT)
4197c478bd9Sstevel@tonic-gate 			goto bad_opt;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		/* Find the option in the opt_arr. */
4227c478bd9Sstevel@tonic-gate 		if ((optd = opt_chk_lookup(opt->level, opt->name,
4237c478bd9Sstevel@tonic-gate 		    opt_arr, opt_arr_cnt)) == NULL) {
4247c478bd9Sstevel@tonic-gate 			/*
4257c478bd9Sstevel@tonic-gate 			 * Not found, that is a bad thing if
4267c478bd9Sstevel@tonic-gate 			 * the caller is a tpi provider
4277c478bd9Sstevel@tonic-gate 			 */
4287c478bd9Sstevel@tonic-gate 			if (topmost_tpiprovider)
4297c478bd9Sstevel@tonic-gate 				goto bad_opt;
4307c478bd9Sstevel@tonic-gate 			else
4317c478bd9Sstevel@tonic-gate 				continue; /* skip unmodified */
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		/* Additional checks dependent on operation. */
4357c478bd9Sstevel@tonic-gate 		switch (tor->MGMT_flags) {
4367c478bd9Sstevel@tonic-gate 		case T_NEGOTIATE:
4377c478bd9Sstevel@tonic-gate 			if (!OA_WRITE_OR_EXECUTE(optd, cr)) {
4387c478bd9Sstevel@tonic-gate 				/* can't negotiate option */
4397c478bd9Sstevel@tonic-gate 				if (!(OA_MATCHED_PRIV(optd, cr)) &&
4407c478bd9Sstevel@tonic-gate 				    OA_WX_ANYPRIV(optd)) {
4417c478bd9Sstevel@tonic-gate 					/*
4427c478bd9Sstevel@tonic-gate 					 * not privileged but privilege
4437c478bd9Sstevel@tonic-gate 					 * will help negotiate option.
4447c478bd9Sstevel@tonic-gate 					 */
4457c478bd9Sstevel@tonic-gate 					optcom_err_ack(q, mp, TACCES, 0);
4467c478bd9Sstevel@tonic-gate 					return (0);
4477c478bd9Sstevel@tonic-gate 				} else
4487c478bd9Sstevel@tonic-gate 					goto bad_opt;
4497c478bd9Sstevel@tonic-gate 			}
4507c478bd9Sstevel@tonic-gate 			/*
4517c478bd9Sstevel@tonic-gate 			 * Verify size for options
4527c478bd9Sstevel@tonic-gate 			 * Note: For retaining compatibility with historical
4537c478bd9Sstevel@tonic-gate 			 * behavior, variable lengths options will have their
4547c478bd9Sstevel@tonic-gate 			 * length verified in the setfn() processing.
4557c478bd9Sstevel@tonic-gate 			 * In order to be compatible with SunOS 4.X we return
4567c478bd9Sstevel@tonic-gate 			 * EINVAL errors for bad lengths.
4577c478bd9Sstevel@tonic-gate 			 */
4587c478bd9Sstevel@tonic-gate 			if (!(optd->opdes_props & OP_VARLEN)) {
4597c478bd9Sstevel@tonic-gate 				/* fixed length - size must match */
4607c478bd9Sstevel@tonic-gate 				if (opt->len != optd->opdes_size) {
4617c478bd9Sstevel@tonic-gate 					optcom_err_ack(q, mp, TSYSERR, EINVAL);
4627c478bd9Sstevel@tonic-gate 					return (0);
4637c478bd9Sstevel@tonic-gate 				}
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 			break;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		case T_CHECK:
4687c478bd9Sstevel@tonic-gate 			if (!OA_RWX_ANYPRIV(optd))
4697c478bd9Sstevel@tonic-gate 				/* any of "rwx" permission but not not none */
4707c478bd9Sstevel@tonic-gate 				goto bad_opt;
4717c478bd9Sstevel@tonic-gate 			/*
4727c478bd9Sstevel@tonic-gate 			 * XXX Since T_CURRENT was not there in TLI and the
4737c478bd9Sstevel@tonic-gate 			 * official TLI inspired TPI standard, getsockopt()
4747c478bd9Sstevel@tonic-gate 			 * API uses T_CHECK (for T_CURRENT semantics)
4757c478bd9Sstevel@tonic-gate 			 * The following fallthru makes sense because of its
4767c478bd9Sstevel@tonic-gate 			 * historical use as semantic equivalent to T_CURRENT.
4777c478bd9Sstevel@tonic-gate 			 */
4787c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
4797c478bd9Sstevel@tonic-gate 		case T_CURRENT:
4807c478bd9Sstevel@tonic-gate 			if (!OA_READ_PERMISSION(optd, cr)) {
4817c478bd9Sstevel@tonic-gate 				/* can't read option value */
4827c478bd9Sstevel@tonic-gate 				if (!(OA_MATCHED_PRIV(optd, cr)) &&
4837c478bd9Sstevel@tonic-gate 				    OA_R_ANYPRIV(optd)) {
4847c478bd9Sstevel@tonic-gate 					/*
4857c478bd9Sstevel@tonic-gate 					 * not privileged but privilege
4867c478bd9Sstevel@tonic-gate 					 * will help in reading option value.
4877c478bd9Sstevel@tonic-gate 					 */
4887c478bd9Sstevel@tonic-gate 					optcom_err_ack(q, mp, TACCES, 0);
4897c478bd9Sstevel@tonic-gate 					return (0);
4907c478bd9Sstevel@tonic-gate 				} else
4917c478bd9Sstevel@tonic-gate 					goto bad_opt;
4927c478bd9Sstevel@tonic-gate 			}
4937c478bd9Sstevel@tonic-gate 			break;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		default:
4967c478bd9Sstevel@tonic-gate 			optcom_err_ack(q, mp, TBADFLAG, 0);
4977c478bd9Sstevel@tonic-gate 			return (0);
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		/* We liked it.  Keep going. */
5007c478bd9Sstevel@tonic-gate 	} /* end for loop scanning option buffer */
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/* Now complete the operation as required. */
5037c478bd9Sstevel@tonic-gate 	switch (tor->MGMT_flags) {
5047c478bd9Sstevel@tonic-gate 	case T_CHECK:
5057c478bd9Sstevel@tonic-gate 		/*
5067c478bd9Sstevel@tonic-gate 		 * Historically used same as T_CURRENT (which was added to
5077c478bd9Sstevel@tonic-gate 		 * standard later). Code retained for compatibility.
5087c478bd9Sstevel@tonic-gate 		 */
5097c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
5107c478bd9Sstevel@tonic-gate 	case T_CURRENT:
5117c478bd9Sstevel@tonic-gate 		/*
5127c478bd9Sstevel@tonic-gate 		 * Allocate a maximum size reply.  Perhaps we are supposed to
5137c478bd9Sstevel@tonic-gate 		 * assume that the input buffer includes space for the answers
5147c478bd9Sstevel@tonic-gate 		 * as well as the opthdrs, but we don't know that for sure.
5157c478bd9Sstevel@tonic-gate 		 * So, instead, we create a new output buffer, using the
5167c478bd9Sstevel@tonic-gate 		 * input buffer only as a list of options.
5177c478bd9Sstevel@tonic-gate 		 */
5187c478bd9Sstevel@tonic-gate 		max_optbuf_len = optcom_max_optbuf_len(opt_arr,
5197c478bd9Sstevel@tonic-gate 		    opt_arr_cnt);
5207c478bd9Sstevel@tonic-gate 		mp1 = allocb_cred(max_optbuf_len, cr);
5217c478bd9Sstevel@tonic-gate 		if (!mp1)
5227c478bd9Sstevel@tonic-gate 			goto no_mem;
5237c478bd9Sstevel@tonic-gate 		/* Initialize the header. */
5247c478bd9Sstevel@tonic-gate 		mp1->b_datap->db_type = M_PCPROTO;
5257c478bd9Sstevel@tonic-gate 		mp1->b_wptr = &mp1->b_rptr[sizeof (struct T_optmgmt_ack)];
5267c478bd9Sstevel@tonic-gate 		toa = (struct T_optmgmt_ack *)mp1->b_rptr;
5277c478bd9Sstevel@tonic-gate 		toa->OPT_offset = (t_scalar_t)sizeof (struct T_optmgmt_ack);
5287c478bd9Sstevel@tonic-gate 		toa->MGMT_flags = tor->MGMT_flags;
5297c478bd9Sstevel@tonic-gate 		/*
5307c478bd9Sstevel@tonic-gate 		 * Walk through the input buffer again, this time adding
5317c478bd9Sstevel@tonic-gate 		 * entries to the output buffer for each option requested.
5327c478bd9Sstevel@tonic-gate 		 * Note, sanity of option header, last option etc, verified
5337c478bd9Sstevel@tonic-gate 		 * in first pass.
5347c478bd9Sstevel@tonic-gate 		 */
5357c478bd9Sstevel@tonic-gate 		opt1 = (struct opthdr *)&toa[1];
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		for (opt = opt_start; opt < opt_end; opt = next_opt) {
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		    next_opt = (struct opthdr *)((uchar_t *)&opt[1] +
5407c478bd9Sstevel@tonic-gate 			_TPI_ALIGN_OPT(opt->len));
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 			opt1->name = opt->name;
5437c478bd9Sstevel@tonic-gate 			opt1->level = opt->level;
5447c478bd9Sstevel@tonic-gate 			len = (*getfn)(q, opt->level,
5457c478bd9Sstevel@tonic-gate 			    opt->name, (uchar_t *)&opt1[1]);
5467c478bd9Sstevel@tonic-gate 			/*
5477c478bd9Sstevel@tonic-gate 			 * Failure means option is not recognized. Copy input
5487c478bd9Sstevel@tonic-gate 			 * buffer as is
5497c478bd9Sstevel@tonic-gate 			 */
5507c478bd9Sstevel@tonic-gate 			if (len < 0) {
5517c478bd9Sstevel@tonic-gate 				opt1->len = opt->len;
5527c478bd9Sstevel@tonic-gate 				bcopy(&opt[1], &opt1[1], opt->len);
5537c478bd9Sstevel@tonic-gate 				/*
554*ff550d0eSmasputra 				 * Pass the option down to IP only
555*ff550d0eSmasputra 				 * if TCP hasn't processed it.
5567c478bd9Sstevel@tonic-gate 				 */
557*ff550d0eSmasputra 				if (is_tcp)
5587c478bd9Sstevel@tonic-gate 					pass_to_ip = B_TRUE;
559*ff550d0eSmasputra 			} else {
5607c478bd9Sstevel@tonic-gate 				opt1->len = (t_uscalar_t)len;
561*ff550d0eSmasputra 			}
5627c478bd9Sstevel@tonic-gate 			opt1 = (struct opthdr *)((uchar_t *)&opt1[1] +
5637c478bd9Sstevel@tonic-gate 			    _TPI_ALIGN_OPT(opt1->len));
5647c478bd9Sstevel@tonic-gate 		} /* end for loop */
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 		/* Record the final length. */
5677c478bd9Sstevel@tonic-gate 		toa->OPT_length = (t_scalar_t)((uchar_t *)opt1 -
5687c478bd9Sstevel@tonic-gate 		    (uchar_t *)&toa[1]);
5697c478bd9Sstevel@tonic-gate 		mp1->b_wptr = (uchar_t *)opt1;
5707c478bd9Sstevel@tonic-gate 		/* Ditch the input buffer. */
5717c478bd9Sstevel@tonic-gate 		freemsg(mp);
5727c478bd9Sstevel@tonic-gate 		mp = mp1;
5737c478bd9Sstevel@tonic-gate 		/* Always let the next module look at the option. */
5747c478bd9Sstevel@tonic-gate 		pass_to_next = B_TRUE;
5757c478bd9Sstevel@tonic-gate 		break;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	case T_NEGOTIATE:
5787c478bd9Sstevel@tonic-gate 		first_mp = allocb(sizeof (opt_restart_t), BPRI_LO);
5797c478bd9Sstevel@tonic-gate 		if (first_mp == NULL) {
5807c478bd9Sstevel@tonic-gate 			optcom_err_ack(q, mp, TSYSERR, ENOMEM);
5817c478bd9Sstevel@tonic-gate 			return (0);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 		first_mp->b_datap->db_type = M_CTL;
5847c478bd9Sstevel@tonic-gate 		or = (opt_restart_t *)first_mp->b_rptr;
5857c478bd9Sstevel@tonic-gate 		or->or_start = opt_start;
5867c478bd9Sstevel@tonic-gate 		or->or_end =  opt_end;
5877c478bd9Sstevel@tonic-gate 		or->or_type = T_SVR4_OPTMGMT_REQ;
5887c478bd9Sstevel@tonic-gate 		or->or_private = 0;
5897c478bd9Sstevel@tonic-gate 		first_mp->b_cont = mp;
5907c478bd9Sstevel@tonic-gate restart:
5917c478bd9Sstevel@tonic-gate 		/*
5927c478bd9Sstevel@tonic-gate 		 * Here we are expecting that the response buffer is exactly
5937c478bd9Sstevel@tonic-gate 		 * the same size as the input buffer.  We pass each opthdr
5947c478bd9Sstevel@tonic-gate 		 * to the protocol's set function.  If the protocol doesn't
5957c478bd9Sstevel@tonic-gate 		 * like it, it can update the value in it return argument.
5967c478bd9Sstevel@tonic-gate 		 */
5977c478bd9Sstevel@tonic-gate 		/*
5987c478bd9Sstevel@tonic-gate 		 * Pass each negotiated option through the protocol set
5997c478bd9Sstevel@tonic-gate 		 * function.
6007c478bd9Sstevel@tonic-gate 		 * Note: sanity check on option header values done in first
6017c478bd9Sstevel@tonic-gate 		 * pass and not repeated here.
6027c478bd9Sstevel@tonic-gate 		 */
6037c478bd9Sstevel@tonic-gate 		toa = (struct T_optmgmt_ack *)tor;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		for (opt = is_restart ? restart_opt: opt_start; opt < opt_end;
6067c478bd9Sstevel@tonic-gate 		    opt = next_opt) {
6077c478bd9Sstevel@tonic-gate 			int error;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 			/*
6107c478bd9Sstevel@tonic-gate 			 * Point to the current option in or, in case this
6117c478bd9Sstevel@tonic-gate 			 * option has to be restarted later on
6127c478bd9Sstevel@tonic-gate 			 */
6137c478bd9Sstevel@tonic-gate 			or->or_ropt = opt;
6147c478bd9Sstevel@tonic-gate 			next_opt = (struct opthdr *)((uchar_t *)&opt[1] +
6157c478bd9Sstevel@tonic-gate 			    _TPI_ALIGN_OPT(opt->len));
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 			error = (*setfn)(q, SETFN_OPTCOM_NEGOTIATE,
6187c478bd9Sstevel@tonic-gate 			    opt->level, opt->name,
6197c478bd9Sstevel@tonic-gate 			    opt->len, (uchar_t *)&opt[1],
6207c478bd9Sstevel@tonic-gate 			    &opt->len, (uchar_t *)&opt[1], NULL, cr, first_mp);
6217c478bd9Sstevel@tonic-gate 			/*
6227c478bd9Sstevel@tonic-gate 			 * Treat positive "errors" as real.
6237c478bd9Sstevel@tonic-gate 			 * Note: negative errors are to be treated as
6247c478bd9Sstevel@tonic-gate 			 * non-fatal by svr4_optcom_req() and are
6257c478bd9Sstevel@tonic-gate 			 * returned by setfn() when it is passed an
6267c478bd9Sstevel@tonic-gate 			 * option it does not handle. Since the option
6277c478bd9Sstevel@tonic-gate 			 * passed opt_chk_lookup(), it is implied that
6287c478bd9Sstevel@tonic-gate 			 * it is valid but was either handled upstream
6297c478bd9Sstevel@tonic-gate 			 * or will be handled downstream.
6307c478bd9Sstevel@tonic-gate 			 */
6317c478bd9Sstevel@tonic-gate 			if (error == EINPROGRESS) {
6327c478bd9Sstevel@tonic-gate 				/*
6337c478bd9Sstevel@tonic-gate 				 * The message is queued and will be
6347c478bd9Sstevel@tonic-gate 				 * reprocessed later. Typically ip queued
6357c478bd9Sstevel@tonic-gate 				 * the message to get some exclusive conditions
6367c478bd9Sstevel@tonic-gate 				 * and later on calls this func again.
6377c478bd9Sstevel@tonic-gate 				 */
6387c478bd9Sstevel@tonic-gate 				return (EINPROGRESS);
6397c478bd9Sstevel@tonic-gate 			} else if (error > 0) {
6407c478bd9Sstevel@tonic-gate 				optcom_err_ack(q, mp, TSYSERR, error);
6417c478bd9Sstevel@tonic-gate 				freeb(first_mp);
6427c478bd9Sstevel@tonic-gate 				return (0);
643*ff550d0eSmasputra 			} else if (error < 0 && is_tcp) {
6447c478bd9Sstevel@tonic-gate 				/*
645*ff550d0eSmasputra 				 * Pass the option down to IP only
646*ff550d0eSmasputra 				 * if TCP hasn't processed it.
6477c478bd9Sstevel@tonic-gate 				 */
6487c478bd9Sstevel@tonic-gate 				pass_to_ip = B_TRUE;
6497c478bd9Sstevel@tonic-gate 			}
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		/* Done with the restart control mp. */
6527c478bd9Sstevel@tonic-gate 		freeb(first_mp);
6537c478bd9Sstevel@tonic-gate 		pass_to_next = B_TRUE;
6547c478bd9Sstevel@tonic-gate 		break;
6557c478bd9Sstevel@tonic-gate 	default:
6567c478bd9Sstevel@tonic-gate 		optcom_err_ack(q, mp, TBADFLAG, 0);
6577c478bd9Sstevel@tonic-gate 		return (0);
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (pass_to_next && (q->q_next != NULL || pass_to_ip)) {
6617c478bd9Sstevel@tonic-gate 		/* Send it down to the next module and let it reply */
6627c478bd9Sstevel@tonic-gate 		toa->PRIM_type = T_SVR4_OPTMGMT_REQ; /* Changed by IP to ACK */
6637c478bd9Sstevel@tonic-gate 		if (q->q_next != NULL)
6647c478bd9Sstevel@tonic-gate 			putnext(q, mp);
6657c478bd9Sstevel@tonic-gate 		else
6667c478bd9Sstevel@tonic-gate 			ip_output(Q_TO_CONN(q), mp, q, IP_WPUT);
6677c478bd9Sstevel@tonic-gate 	} else {
6687c478bd9Sstevel@tonic-gate 		/* Set common fields in the header. */
6697c478bd9Sstevel@tonic-gate 		toa->MGMT_flags = T_SUCCESS;
6707c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_PCPROTO;
6717c478bd9Sstevel@tonic-gate 		toa->PRIM_type = T_OPTMGMT_ACK;
6727c478bd9Sstevel@tonic-gate 		qreply(q, mp);
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	return (0);
6757c478bd9Sstevel@tonic-gate bad_opt:;
6767c478bd9Sstevel@tonic-gate 	optcom_err_ack(q, mp, TBADOPT, 0);
6777c478bd9Sstevel@tonic-gate 	return (0);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate /*
6817c478bd9Sstevel@tonic-gate  * New optcom_req inspired by TPI/XTI semantics
6827c478bd9Sstevel@tonic-gate  */
6837c478bd9Sstevel@tonic-gate int
6847c478bd9Sstevel@tonic-gate tpi_optcom_req(queue_t *q, mblk_t *mp, cred_t *cr, optdb_obj_t *dbobjp)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	t_scalar_t t_error;
6877c478bd9Sstevel@tonic-gate 	mblk_t *toa_mp;
6887c478bd9Sstevel@tonic-gate 	boolean_t pass_to_next;
6897c478bd9Sstevel@tonic-gate 	size_t toa_len;
6907c478bd9Sstevel@tonic-gate 	struct T_optmgmt_ack *toa;
6917c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req *tor =
6927c478bd9Sstevel@tonic-gate 	    (struct T_optmgmt_req *)mp->b_rptr;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	opt_restart_t *or;
6957c478bd9Sstevel@tonic-gate 	boolean_t is_restart = B_FALSE;
6967c478bd9Sstevel@tonic-gate 	mblk_t	*first_mp = NULL;
6977c478bd9Sstevel@tonic-gate 	t_uscalar_t worst_status;
6987c478bd9Sstevel@tonic-gate 	boolean_t queued_status;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Allocate M_CTL and prepend to the packet for restarting this
7027c478bd9Sstevel@tonic-gate 	 * option if needed. IP may need to queue and restart the option
7037c478bd9Sstevel@tonic-gate 	 * if it cannot obtain exclusive conditions immediately. Please see
7047c478bd9Sstevel@tonic-gate 	 * IP-MT notes before the start of svr4_optcom_req
7057c478bd9Sstevel@tonic-gate 	 */
7067c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_CTL) {
7077c478bd9Sstevel@tonic-gate 		is_restart = B_TRUE;
7087c478bd9Sstevel@tonic-gate 		first_mp = mp;
7097c478bd9Sstevel@tonic-gate 		toa_mp = mp->b_cont;
7107c478bd9Sstevel@tonic-gate 		mp = toa_mp->b_cont;
7117c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_wptr - mp->b_rptr >=
7127c478bd9Sstevel@tonic-gate 		    sizeof (struct T_optmgmt_req));
7137c478bd9Sstevel@tonic-gate 		tor = (struct T_optmgmt_req *)mp->b_rptr;
7147c478bd9Sstevel@tonic-gate 		ASSERT(tor->MGMT_flags == T_NEGOTIATE);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		or = (opt_restart_t *)first_mp->b_rptr;
7177c478bd9Sstevel@tonic-gate 		goto restart;
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	/* Verify message integrity. */
7217c478bd9Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_optmgmt_req)) {
7227c478bd9Sstevel@tonic-gate 		optcom_err_ack(q, mp, TBADOPT, 0);
7237c478bd9Sstevel@tonic-gate 		return (0);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	/* Verify MGMT_flags legal */
7277c478bd9Sstevel@tonic-gate 	switch (tor->MGMT_flags) {
7287c478bd9Sstevel@tonic-gate 	case T_DEFAULT:
7297c478bd9Sstevel@tonic-gate 	case T_NEGOTIATE:
7307c478bd9Sstevel@tonic-gate 	case T_CURRENT:
7317c478bd9Sstevel@tonic-gate 	case T_CHECK:
7327c478bd9Sstevel@tonic-gate 		/* OK - legal request flags */
7337c478bd9Sstevel@tonic-gate 		break;
7347c478bd9Sstevel@tonic-gate 	default:
7357c478bd9Sstevel@tonic-gate 		optcom_err_ack(q, mp, TBADFLAG, 0);
7367c478bd9Sstevel@tonic-gate 		return (0);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	/*
7407c478bd9Sstevel@tonic-gate 	 * In this design, there are two passes required on the input buffer
7417c478bd9Sstevel@tonic-gate 	 * mostly to accomodate variable length options and "T_ALLOPT" option
7427c478bd9Sstevel@tonic-gate 	 * which has the semantics "all options of the specified level".
7437c478bd9Sstevel@tonic-gate 	 *
7447c478bd9Sstevel@tonic-gate 	 * For T_DEFAULT, T_NEGOTIATE, T_CURRENT, and T_CHECK requests, we make
7457c478bd9Sstevel@tonic-gate 	 * a pass through the input buffer validating the details and making
7467c478bd9Sstevel@tonic-gate 	 * sure each option is supported by the protocol. We also determine the
7477c478bd9Sstevel@tonic-gate 	 * length of the option buffer to return. (Variable length options and
7487c478bd9Sstevel@tonic-gate 	 * T_ALLOPT mean that length can be different for output buffer).
7497c478bd9Sstevel@tonic-gate 	 */
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	pass_to_next = B_FALSE;	/* initial value */
7527c478bd9Sstevel@tonic-gate 	toa_len = 0;		/* initial value */
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/*
7557c478bd9Sstevel@tonic-gate 	 * First pass, we do the following
7567c478bd9Sstevel@tonic-gate 	 *	- estimate cumulative length needed for results
7577c478bd9Sstevel@tonic-gate 	 *	- set "status" field based on permissions, option header check
7587c478bd9Sstevel@tonic-gate 	 *	  etc.
7597c478bd9Sstevel@tonic-gate 	 *	- determine "pass_to_next" whether we need to send request to
7607c478bd9Sstevel@tonic-gate 	 *	  downstream module/driver.
7617c478bd9Sstevel@tonic-gate 	 */
7627c478bd9Sstevel@tonic-gate 	if ((t_error = process_topthdrs_first_pass(mp, cr, dbobjp,
7637c478bd9Sstevel@tonic-gate 	    &pass_to_next, &toa_len)) != 0) {
7647c478bd9Sstevel@tonic-gate 		optcom_err_ack(q, mp, t_error, 0);
7657c478bd9Sstevel@tonic-gate 		return (0);
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	/*
7697c478bd9Sstevel@tonic-gate 	 * A validation phase of the input buffer is done. We have also
7707c478bd9Sstevel@tonic-gate 	 * obtained the length requirement and and other details about the
7717c478bd9Sstevel@tonic-gate 	 * input and we liked input buffer so far.  We make another scan
7727c478bd9Sstevel@tonic-gate 	 * through the input now and generate the output necessary to complete
7737c478bd9Sstevel@tonic-gate 	 * the operation.
7747c478bd9Sstevel@tonic-gate 	 */
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	toa_mp = allocb_cred(toa_len, cr);
7777c478bd9Sstevel@tonic-gate 	if (!toa_mp) {
7787c478bd9Sstevel@tonic-gate 		optcom_err_ack(q, mp, TSYSERR, ENOMEM);
7797c478bd9Sstevel@tonic-gate 		return (0);
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	first_mp = allocb(sizeof (opt_restart_t), BPRI_LO);
7837c478bd9Sstevel@tonic-gate 	if (first_mp == NULL) {
7847c478bd9Sstevel@tonic-gate 		freeb(toa_mp);
7857c478bd9Sstevel@tonic-gate 		optcom_err_ack(q, mp, TSYSERR, ENOMEM);
7867c478bd9Sstevel@tonic-gate 		return (0);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 	first_mp->b_datap->db_type = M_CTL;
7897c478bd9Sstevel@tonic-gate 	or = (opt_restart_t *)first_mp->b_rptr;
7907c478bd9Sstevel@tonic-gate 	/*
7917c478bd9Sstevel@tonic-gate 	 * Set initial values for generating output.
7927c478bd9Sstevel@tonic-gate 	 */
7937c478bd9Sstevel@tonic-gate 	or->or_worst_status = T_SUCCESS;
7947c478bd9Sstevel@tonic-gate 	or->or_type = T_OPTMGMT_REQ;
7957c478bd9Sstevel@tonic-gate 	or->or_private = 0;
7967c478bd9Sstevel@tonic-gate 	/* remaining fields fileed in do_options_second_pass */
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate restart:
7997c478bd9Sstevel@tonic-gate 	/*
8007c478bd9Sstevel@tonic-gate 	 * This routine makes another pass through the option buffer this
8017c478bd9Sstevel@tonic-gate 	 * time acting on the request based on "status" result in the
8027c478bd9Sstevel@tonic-gate 	 * first pass. It also performs "expansion" of T_ALLOPT into
8037c478bd9Sstevel@tonic-gate 	 * all options of a certain level and acts on each for this request.
8047c478bd9Sstevel@tonic-gate 	 */
8057c478bd9Sstevel@tonic-gate 	if ((t_error = do_options_second_pass(q, mp, toa_mp, cr, dbobjp,
8067c478bd9Sstevel@tonic-gate 	    first_mp, is_restart, &queued_status)) != 0) {
8077c478bd9Sstevel@tonic-gate 		freemsg(toa_mp);
8087c478bd9Sstevel@tonic-gate 		optcom_err_ack(q, mp, t_error, 0);
8097c478bd9Sstevel@tonic-gate 		return (0);
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 	if (queued_status) {
8127c478bd9Sstevel@tonic-gate 		/* Option will be restarted */
8137c478bd9Sstevel@tonic-gate 		return (EINPROGRESS);
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 	worst_status = or->or_worst_status;
8167c478bd9Sstevel@tonic-gate 	/* Done with the first mp */
8177c478bd9Sstevel@tonic-gate 	freeb(first_mp);
8187c478bd9Sstevel@tonic-gate 	toa_mp->b_cont = NULL;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	/*
8217c478bd9Sstevel@tonic-gate 	 * Following code relies on the coincidence that T_optmgmt_req
8227c478bd9Sstevel@tonic-gate 	 * and T_optmgmt_ack are identical in binary representation
8237c478bd9Sstevel@tonic-gate 	 */
8247c478bd9Sstevel@tonic-gate 	toa = (struct T_optmgmt_ack *)toa_mp->b_rptr;
8257c478bd9Sstevel@tonic-gate 	toa->OPT_length = (t_scalar_t)(toa_mp->b_wptr - (toa_mp->b_rptr +
8267c478bd9Sstevel@tonic-gate 	    sizeof (struct T_optmgmt_ack)));
8277c478bd9Sstevel@tonic-gate 	toa->OPT_offset = (t_scalar_t)sizeof (struct T_optmgmt_ack);
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	toa->MGMT_flags = tor->MGMT_flags;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	freemsg(mp);		/* free input mblk */
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	/*
8357c478bd9Sstevel@tonic-gate 	 * If there is atleast one option that requires a downstream
8367c478bd9Sstevel@tonic-gate 	 * forwarding and if it is possible, we forward the message
8377c478bd9Sstevel@tonic-gate 	 * downstream. Else we ack it.
8387c478bd9Sstevel@tonic-gate 	 */
8397c478bd9Sstevel@tonic-gate 	if (pass_to_next && (q->q_next != NULL || dbobjp == &tcp_opt_obj)) {
8407c478bd9Sstevel@tonic-gate 		/*
8417c478bd9Sstevel@tonic-gate 		 * We pass it down as T_OPTMGMT_REQ. This code relies
8427c478bd9Sstevel@tonic-gate 		 * on the happy coincidence that T_optmgmt_req and
8437c478bd9Sstevel@tonic-gate 		 * T_optmgmt_ack are identical data structures
8447c478bd9Sstevel@tonic-gate 		 * at the binary representation level.
8457c478bd9Sstevel@tonic-gate 		 */
8467c478bd9Sstevel@tonic-gate 		toa_mp->b_datap->db_type = M_PROTO;
8477c478bd9Sstevel@tonic-gate 		toa->PRIM_type = T_OPTMGMT_REQ;
8487c478bd9Sstevel@tonic-gate 		if (q->q_next != NULL)
8497c478bd9Sstevel@tonic-gate 			putnext(q, toa_mp);
8507c478bd9Sstevel@tonic-gate 		else
8517c478bd9Sstevel@tonic-gate 			ip_output(Q_TO_CONN(q), toa_mp, q, IP_WPUT);
8527c478bd9Sstevel@tonic-gate 	} else {
8537c478bd9Sstevel@tonic-gate 		toa->PRIM_type = T_OPTMGMT_ACK;
8547c478bd9Sstevel@tonic-gate 		toa_mp->b_datap->db_type = M_PCPROTO;
8557c478bd9Sstevel@tonic-gate 		toa->MGMT_flags |= worst_status; /* XXX "worst" or "OR" TPI ? */
8567c478bd9Sstevel@tonic-gate 		qreply(q, toa_mp);
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 	return (0);
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate  * Following routine makes a pass through option buffer in mp and performs the
8647c478bd9Sstevel@tonic-gate  * following tasks.
8657c478bd9Sstevel@tonic-gate  *	- estimate cumulative length needed for results
8667c478bd9Sstevel@tonic-gate  *	- set "status" field based on permissions, option header check
8677c478bd9Sstevel@tonic-gate  *	  etc.
8687c478bd9Sstevel@tonic-gate  *	- determine "pass_to_next" whether we need to send request to
8697c478bd9Sstevel@tonic-gate  *	  downstream module/driver.
8707c478bd9Sstevel@tonic-gate  */
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate static t_scalar_t
8737c478bd9Sstevel@tonic-gate process_topthdrs_first_pass(mblk_t *mp, cred_t *cr, optdb_obj_t *dbobjp,
8747c478bd9Sstevel@tonic-gate     boolean_t *pass_to_nextp, size_t *toa_lenp)
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate 	opdes_t	*opt_arr = dbobjp->odb_opt_des_arr;
8777c478bd9Sstevel@tonic-gate 	uint_t opt_arr_cnt = dbobjp->odb_opt_arr_cnt;
8787c478bd9Sstevel@tonic-gate 	boolean_t topmost_tpiprovider = dbobjp->odb_topmost_tpiprovider;
8797c478bd9Sstevel@tonic-gate 	optlevel_t *valid_level_arr = dbobjp->odb_valid_levels_arr;
8807c478bd9Sstevel@tonic-gate 	uint_t valid_level_arr_cnt = dbobjp->odb_valid_levels_arr_cnt;
8817c478bd9Sstevel@tonic-gate 	struct T_opthdr *opt;
8827c478bd9Sstevel@tonic-gate 	struct T_opthdr *opt_start, *opt_end;
8837c478bd9Sstevel@tonic-gate 	opdes_t	*optd;
8847c478bd9Sstevel@tonic-gate 	size_t allopt_len;
8857c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req *tor =
8867c478bd9Sstevel@tonic-gate 	    (struct T_optmgmt_req *)mp->b_rptr;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	*toa_lenp = sizeof (struct T_optmgmt_ack); /* initial value */
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if ((opt_start = (struct T_opthdr *)
8917c478bd9Sstevel@tonic-gate 	    mi_offset_param(mp, tor->OPT_offset, tor->OPT_length)) == NULL) {
8927c478bd9Sstevel@tonic-gate 		return (TBADOPT);
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 	if (!__TPI_TOPT_ISALIGNED(opt_start))
8957c478bd9Sstevel@tonic-gate 		return (TBADOPT);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	opt_end = (struct T_opthdr *)((uchar_t *)opt_start + tor->OPT_length);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	for (opt = opt_start; opt && (opt < opt_end);
9007c478bd9Sstevel@tonic-gate 	    opt = _TPI_TOPT_NEXTHDR(opt_start, tor->OPT_length, opt)) {
9017c478bd9Sstevel@tonic-gate 		/*
9027c478bd9Sstevel@tonic-gate 		 * Validate the option for length and alignment
9037c478bd9Sstevel@tonic-gate 		 * before accessing anything in it.
9047c478bd9Sstevel@tonic-gate 		 */
9057c478bd9Sstevel@tonic-gate 		if (!(_TPI_TOPT_VALID(opt, opt_start, opt_end)))
9067c478bd9Sstevel@tonic-gate 			return (TBADOPT);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		/* Find the option in the opt_arr. */
9097c478bd9Sstevel@tonic-gate 		if (opt->name != T_ALLOPT) {
9107c478bd9Sstevel@tonic-gate 			optd = opt_chk_lookup(opt->level, opt->name,
9117c478bd9Sstevel@tonic-gate 			    opt_arr, opt_arr_cnt);
9127c478bd9Sstevel@tonic-gate 			if (optd == NULL) {
9137c478bd9Sstevel@tonic-gate 				/*
9147c478bd9Sstevel@tonic-gate 				 * Option not found
9157c478bd9Sstevel@tonic-gate 				 *
9167c478bd9Sstevel@tonic-gate 				 * Verify if level is "valid" or not.
9177c478bd9Sstevel@tonic-gate 				 * Note: This check is required by XTI
9187c478bd9Sstevel@tonic-gate 				 *
9197c478bd9Sstevel@tonic-gate 				 * TPI provider always initializes
9207c478bd9Sstevel@tonic-gate 				 * the "not supported" (or whatever) status
9217c478bd9Sstevel@tonic-gate 				 * for the options. Other levels leave status
9227c478bd9Sstevel@tonic-gate 				 * unchanged if they do not understand an
9237c478bd9Sstevel@tonic-gate 				 * option.
9247c478bd9Sstevel@tonic-gate 				 */
9257c478bd9Sstevel@tonic-gate 				if (topmost_tpiprovider) {
9267c478bd9Sstevel@tonic-gate 					if (!opt_level_valid(opt->level,
9277c478bd9Sstevel@tonic-gate 					    valid_level_arr,
9287c478bd9Sstevel@tonic-gate 					    valid_level_arr_cnt))
9297c478bd9Sstevel@tonic-gate 						return (TBADOPT);
9307c478bd9Sstevel@tonic-gate 					/*
9317c478bd9Sstevel@tonic-gate 					 * level is valid - initialize
9327c478bd9Sstevel@tonic-gate 					 * option as not supported
9337c478bd9Sstevel@tonic-gate 					 */
9347c478bd9Sstevel@tonic-gate 					opt->status = T_NOTSUPPORT;
9357c478bd9Sstevel@tonic-gate 				}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 				*toa_lenp += _TPI_ALIGN_TOPT(opt->len);
9387c478bd9Sstevel@tonic-gate 				continue;
9397c478bd9Sstevel@tonic-gate 			}
9407c478bd9Sstevel@tonic-gate 		} else {
9417c478bd9Sstevel@tonic-gate 			/*
9427c478bd9Sstevel@tonic-gate 			 * Handle T_ALLOPT case as a special case.
9437c478bd9Sstevel@tonic-gate 			 * Note: T_ALLOPT does not mean anything
9447c478bd9Sstevel@tonic-gate 			 * for T_CHECK operation.
9457c478bd9Sstevel@tonic-gate 			 */
9467c478bd9Sstevel@tonic-gate 			allopt_len = 0;
9477c478bd9Sstevel@tonic-gate 			if (tor->MGMT_flags == T_CHECK ||
9487c478bd9Sstevel@tonic-gate 			    !topmost_tpiprovider ||
9497c478bd9Sstevel@tonic-gate 			    ((allopt_len = opt_level_allopts_lengths(opt->level,
9507c478bd9Sstevel@tonic-gate 				opt_arr, opt_arr_cnt)) == 0)) {
9517c478bd9Sstevel@tonic-gate 				/*
9527c478bd9Sstevel@tonic-gate 				 * This is confusing but correct !
9537c478bd9Sstevel@tonic-gate 				 * It is not valid to to use T_ALLOPT with
9547c478bd9Sstevel@tonic-gate 				 * T_CHECK flag.
9557c478bd9Sstevel@tonic-gate 				 *
9567c478bd9Sstevel@tonic-gate 				 * T_ALLOPT is assumed "expanded" at the
9577c478bd9Sstevel@tonic-gate 				 * topmost_tpiprovider level so it should not
9587c478bd9Sstevel@tonic-gate 				 * be there as an "option name" if this is not
9597c478bd9Sstevel@tonic-gate 				 * a topmost_tpiprovider call and we fail it.
9607c478bd9Sstevel@tonic-gate 				 *
9617c478bd9Sstevel@tonic-gate 				 * opt_level_allopts_lengths() is used to verify
9627c478bd9Sstevel@tonic-gate 				 * that "level" associated with the T_ALLOPT is
9637c478bd9Sstevel@tonic-gate 				 * supported.
9647c478bd9Sstevel@tonic-gate 				 *
9657c478bd9Sstevel@tonic-gate 				 */
9667c478bd9Sstevel@tonic-gate 				opt->status = T_FAILURE;
9677c478bd9Sstevel@tonic-gate 				*toa_lenp += _TPI_ALIGN_TOPT(opt->len);
9687c478bd9Sstevel@tonic-gate 				continue;
9697c478bd9Sstevel@tonic-gate 			}
9707c478bd9Sstevel@tonic-gate 			ASSERT(allopt_len != 0); /* remove ? */
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 			*toa_lenp += allopt_len;
9737c478bd9Sstevel@tonic-gate 			opt->status = T_SUCCESS;
9747c478bd9Sstevel@tonic-gate 			/* XXX - always set T_ALLOPT 'pass_to_next' for now */
9757c478bd9Sstevel@tonic-gate 			*pass_to_nextp = B_TRUE;
9767c478bd9Sstevel@tonic-gate 			continue;
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 		/*
9797c478bd9Sstevel@tonic-gate 		 * Check if option wants to flow downstream
9807c478bd9Sstevel@tonic-gate 		 */
9817c478bd9Sstevel@tonic-gate 		if (optd->opdes_props & OP_PASSNEXT)
9827c478bd9Sstevel@tonic-gate 			*pass_to_nextp = B_TRUE;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 		/* Additional checks dependent on operation. */
9857c478bd9Sstevel@tonic-gate 		switch (tor->MGMT_flags) {
9867c478bd9Sstevel@tonic-gate 		case T_DEFAULT:
9877c478bd9Sstevel@tonic-gate 		case T_CURRENT:
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 			/*
9907c478bd9Sstevel@tonic-gate 			 * The opt_chk_lookup() routine call above approved of
9917c478bd9Sstevel@tonic-gate 			 * this option so we can work on the status for it
9927c478bd9Sstevel@tonic-gate 			 * based on the permissions for the operation. (This
9937c478bd9Sstevel@tonic-gate 			 * can override any status for it set at higher levels)
9947c478bd9Sstevel@tonic-gate 			 * We assume this override is OK since chkfn at this
9957c478bd9Sstevel@tonic-gate 			 * level approved of this option.
9967c478bd9Sstevel@tonic-gate 			 *
9977c478bd9Sstevel@tonic-gate 			 * T_CURRENT semantics:
9987c478bd9Sstevel@tonic-gate 			 * The read access is required. Else option
9997c478bd9Sstevel@tonic-gate 			 * status is T_NOTSUPPORT.
10007c478bd9Sstevel@tonic-gate 			 *
10017c478bd9Sstevel@tonic-gate 			 * T_DEFAULT semantics:
10027c478bd9Sstevel@tonic-gate 			 * Note: specification is not clear on this but we
10037c478bd9Sstevel@tonic-gate 			 * interpret T_DEFAULT semantics such that access to
10047c478bd9Sstevel@tonic-gate 			 * read value is required for access even the default
10057c478bd9Sstevel@tonic-gate 			 * value. Otherwise the option status is T_NOTSUPPORT.
10067c478bd9Sstevel@tonic-gate 			 */
10077c478bd9Sstevel@tonic-gate 			if (!OA_READ_PERMISSION(optd, cr)) {
10087c478bd9Sstevel@tonic-gate 				opt->status = T_NOTSUPPORT;
10097c478bd9Sstevel@tonic-gate 				*toa_lenp += _TPI_ALIGN_TOPT(opt->len);
10107c478bd9Sstevel@tonic-gate 				/* skip to next */
10117c478bd9Sstevel@tonic-gate 				continue;
10127c478bd9Sstevel@tonic-gate 			}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 			/*
10157c478bd9Sstevel@tonic-gate 			 * T_DEFAULT/T_CURRENT semantics:
10167c478bd9Sstevel@tonic-gate 			 * We know that read access is set. If no other access
10177c478bd9Sstevel@tonic-gate 			 * is set, then status is T_READONLY.
10187c478bd9Sstevel@tonic-gate 			 */
10197c478bd9Sstevel@tonic-gate 			if (OA_READONLY_PERMISSION(optd, cr))
10207c478bd9Sstevel@tonic-gate 				opt->status = T_READONLY;
10217c478bd9Sstevel@tonic-gate 			else
10227c478bd9Sstevel@tonic-gate 				opt->status = T_SUCCESS;
10237c478bd9Sstevel@tonic-gate 			/*
10247c478bd9Sstevel@tonic-gate 			 * Option passes all checks. Make room for it in the
10257c478bd9Sstevel@tonic-gate 			 * ack. Note: size stored in table does not include
10267c478bd9Sstevel@tonic-gate 			 * space for option header.
10277c478bd9Sstevel@tonic-gate 			 */
10287c478bd9Sstevel@tonic-gate 			*toa_lenp += sizeof (struct T_opthdr) +
10297c478bd9Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(optd->opdes_size);
10307c478bd9Sstevel@tonic-gate 			break;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 		case T_CHECK:
10337c478bd9Sstevel@tonic-gate 		case T_NEGOTIATE:
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 			/*
10367c478bd9Sstevel@tonic-gate 			 * T_NEGOTIATE semantics:
10377c478bd9Sstevel@tonic-gate 			 * If for fixed length option value on input is not the
10387c478bd9Sstevel@tonic-gate 			 * same as value supplied, then status is T_FAILURE.
10397c478bd9Sstevel@tonic-gate 			 *
10407c478bd9Sstevel@tonic-gate 			 * T_CHECK semantics:
10417c478bd9Sstevel@tonic-gate 			 * If value is supplied, semantics same as T_NEGOTIATE.
10427c478bd9Sstevel@tonic-gate 			 * It is however ok not to supply a value with T_CHECK.
10437c478bd9Sstevel@tonic-gate 			 */
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 			if (tor->MGMT_flags == T_NEGOTIATE ||
10467c478bd9Sstevel@tonic-gate 			    (opt->len != sizeof (struct T_opthdr))) {
10477c478bd9Sstevel@tonic-gate 				/*
10487c478bd9Sstevel@tonic-gate 				 * Implies "value" is specified in T_CHECK or
10497c478bd9Sstevel@tonic-gate 				 * it is a T_NEGOTIATE request.
10507c478bd9Sstevel@tonic-gate 				 * Verify size.
10517c478bd9Sstevel@tonic-gate 				 * Note: This can override anything about this
10527c478bd9Sstevel@tonic-gate 				 * option request done at a higher level.
10537c478bd9Sstevel@tonic-gate 				 */
10547c478bd9Sstevel@tonic-gate 				if (!opt_length_ok(optd, opt)) {
10557c478bd9Sstevel@tonic-gate 					/* bad size */
10567c478bd9Sstevel@tonic-gate 					*toa_lenp += _TPI_ALIGN_TOPT(opt->len);
10577c478bd9Sstevel@tonic-gate 					opt->status = T_FAILURE;
10587c478bd9Sstevel@tonic-gate 					continue;
10597c478bd9Sstevel@tonic-gate 				}
10607c478bd9Sstevel@tonic-gate 			}
10617c478bd9Sstevel@tonic-gate 			/*
10627c478bd9Sstevel@tonic-gate 			 * The opt_chk_lookup()  routine above() approved of
10637c478bd9Sstevel@tonic-gate 			 * this option so we can work on the status for it based
10647c478bd9Sstevel@tonic-gate 			 * on the permissions for the operation. (This can
10657c478bd9Sstevel@tonic-gate 			 * override anything set at a higher level).
10667c478bd9Sstevel@tonic-gate 			 *
10677c478bd9Sstevel@tonic-gate 			 * T_CHECK/T_NEGOTIATE semantics:
10687c478bd9Sstevel@tonic-gate 			 * Set status to T_READONLY if read is the only access
10697c478bd9Sstevel@tonic-gate 			 * permitted
10707c478bd9Sstevel@tonic-gate 			 */
10717c478bd9Sstevel@tonic-gate 			if (OA_READONLY_PERMISSION(optd, cr)) {
10727c478bd9Sstevel@tonic-gate 				opt->status = T_READONLY;
10737c478bd9Sstevel@tonic-gate 				*toa_lenp += _TPI_ALIGN_TOPT(opt->len);
10747c478bd9Sstevel@tonic-gate 				/* skip to next */
10757c478bd9Sstevel@tonic-gate 				continue;
10767c478bd9Sstevel@tonic-gate 			}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 			/*
10797c478bd9Sstevel@tonic-gate 			 * T_CHECK/T_NEGOTIATE semantics:
10807c478bd9Sstevel@tonic-gate 			 * If write (or execute) access is not set, then status
10817c478bd9Sstevel@tonic-gate 			 * is T_NOTSUPPORT.
10827c478bd9Sstevel@tonic-gate 			 */
10837c478bd9Sstevel@tonic-gate 			if (!OA_WRITE_OR_EXECUTE(optd, cr)) {
10847c478bd9Sstevel@tonic-gate 				opt->status = T_NOTSUPPORT;
10857c478bd9Sstevel@tonic-gate 				*toa_lenp += _TPI_ALIGN_TOPT(opt->len);
10867c478bd9Sstevel@tonic-gate 				/* skip to next option */
10877c478bd9Sstevel@tonic-gate 				continue;
10887c478bd9Sstevel@tonic-gate 			}
10897c478bd9Sstevel@tonic-gate 			/*
10907c478bd9Sstevel@tonic-gate 			 * Option passes all checks. Make room for it in the
10917c478bd9Sstevel@tonic-gate 			 * ack and set success in status.
10927c478bd9Sstevel@tonic-gate 			 * Note: size stored in table does not include header
10937c478bd9Sstevel@tonic-gate 			 * length.
10947c478bd9Sstevel@tonic-gate 			 */
10957c478bd9Sstevel@tonic-gate 			opt->status = T_SUCCESS;
10967c478bd9Sstevel@tonic-gate 			*toa_lenp += sizeof (struct T_opthdr) +
10977c478bd9Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(optd->opdes_size);
10987c478bd9Sstevel@tonic-gate 			break;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		default:
11017c478bd9Sstevel@tonic-gate 			return (TBADFLAG);
11027c478bd9Sstevel@tonic-gate 		}
11037c478bd9Sstevel@tonic-gate 	} /* for loop scanning input buffer */
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	return (0);		/* OK return */
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate /*
11097c478bd9Sstevel@tonic-gate  * This routine makes another pass through the option buffer this
11107c478bd9Sstevel@tonic-gate  * time acting on the request based on "status" result in the
11117c478bd9Sstevel@tonic-gate  * first pass. It also performs "expansion" of T_ALLOPT into
11127c478bd9Sstevel@tonic-gate  * all options of a certain level and acts on each for this request.
11137c478bd9Sstevel@tonic-gate  */
11147c478bd9Sstevel@tonic-gate static t_scalar_t
11157c478bd9Sstevel@tonic-gate do_options_second_pass(queue_t *q, mblk_t *reqmp, mblk_t *ack_mp, cred_t *cr,
11167c478bd9Sstevel@tonic-gate     optdb_obj_t *dbobjp, mblk_t *first_mp, boolean_t is_restart,
11177c478bd9Sstevel@tonic-gate     boolean_t *queued_statusp)
11187c478bd9Sstevel@tonic-gate {
11197c478bd9Sstevel@tonic-gate 	boolean_t topmost_tpiprovider = dbobjp->odb_topmost_tpiprovider;
11207c478bd9Sstevel@tonic-gate 	int failed_option;
11217c478bd9Sstevel@tonic-gate 	struct T_opthdr *opt;
11227c478bd9Sstevel@tonic-gate 	struct T_opthdr *opt_start, *opt_end, *restart_opt;
11237c478bd9Sstevel@tonic-gate 	uchar_t *optr;
11247c478bd9Sstevel@tonic-gate 	uint_t optset_context;
11257c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req *tor = (struct T_optmgmt_req *)reqmp->b_rptr;
11267c478bd9Sstevel@tonic-gate 	opt_restart_t	*or;
11277c478bd9Sstevel@tonic-gate 	t_uscalar_t	*worst_statusp;
11287c478bd9Sstevel@tonic-gate 	int	err;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	*queued_statusp = B_FALSE;
11317c478bd9Sstevel@tonic-gate 	or = (opt_restart_t *)first_mp->b_rptr;
11327c478bd9Sstevel@tonic-gate 	worst_statusp = &or->or_worst_status;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	optr = (uchar_t *)ack_mp->b_rptr +
11357c478bd9Sstevel@tonic-gate 	    sizeof (struct T_optmgmt_ack); /* assumed int32_t aligned */
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	/*
11387c478bd9Sstevel@tonic-gate 	 * Set initial values for scanning input
11397c478bd9Sstevel@tonic-gate 	 */
11407c478bd9Sstevel@tonic-gate 	if (is_restart) {
11417c478bd9Sstevel@tonic-gate 		opt_start = (struct T_opthdr *)or->or_start;
11427c478bd9Sstevel@tonic-gate 		opt_end = (struct T_opthdr *)or->or_end;
11437c478bd9Sstevel@tonic-gate 		restart_opt = (struct T_opthdr *)or->or_ropt;
11447c478bd9Sstevel@tonic-gate 	} else {
11457c478bd9Sstevel@tonic-gate 		opt_start = (struct T_opthdr *)mi_offset_param(reqmp,
11467c478bd9Sstevel@tonic-gate 		    tor->OPT_offset, tor->OPT_length);
11477c478bd9Sstevel@tonic-gate 		if (opt_start == NULL)
11487c478bd9Sstevel@tonic-gate 			return (TBADOPT);
11497c478bd9Sstevel@tonic-gate 		opt_end = (struct T_opthdr *)((uchar_t *)opt_start +
11507c478bd9Sstevel@tonic-gate 		    tor->OPT_length);
11517c478bd9Sstevel@tonic-gate 		or->or_start = (struct opthdr *)opt_start;
11527c478bd9Sstevel@tonic-gate 		or->or_end = (struct opthdr *)opt_end;
11537c478bd9Sstevel@tonic-gate 		/*
11547c478bd9Sstevel@tonic-gate 		 * construct the mp chain, in case the setfn needs to
11557c478bd9Sstevel@tonic-gate 		 * queue this and restart option processing later on.
11567c478bd9Sstevel@tonic-gate 		 */
11577c478bd9Sstevel@tonic-gate 		first_mp->b_cont = ack_mp;
11587c478bd9Sstevel@tonic-gate 		ack_mp->b_cont = reqmp;
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt_start)); /* verified in first pass */
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	for (opt = is_restart ? restart_opt : opt_start;
11637c478bd9Sstevel@tonic-gate 	    opt && (opt < opt_end);
11647c478bd9Sstevel@tonic-gate 	    opt = _TPI_TOPT_NEXTHDR(opt_start, tor->OPT_length, opt)) {
11657c478bd9Sstevel@tonic-gate 		or->or_ropt = (struct opthdr *)opt;
11667c478bd9Sstevel@tonic-gate 		/* verified in first pass */
11677c478bd9Sstevel@tonic-gate 		ASSERT(_TPI_TOPT_VALID(opt, opt_start, opt_end));
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		/*
11707c478bd9Sstevel@tonic-gate 		 * If the first pass in process_topthdrs_first_pass()
11717c478bd9Sstevel@tonic-gate 		 * has marked the option as a failure case for the MGMT_flags
11727c478bd9Sstevel@tonic-gate 		 * semantics then there is not much to do.
11737c478bd9Sstevel@tonic-gate 		 *
11747c478bd9Sstevel@tonic-gate 		 * Note: For all practical purposes, T_READONLY status is
11757c478bd9Sstevel@tonic-gate 		 * a "success" for T_DEFAULT/T_CURRENT and "failure" for
11767c478bd9Sstevel@tonic-gate 		 * T_CHECK/T_NEGOTIATE
11777c478bd9Sstevel@tonic-gate 		 */
11787c478bd9Sstevel@tonic-gate 		failed_option =
11797c478bd9Sstevel@tonic-gate 		    (opt->status == T_NOTSUPPORT) ||
11807c478bd9Sstevel@tonic-gate 		    (opt->status == T_FAILURE) ||
11817c478bd9Sstevel@tonic-gate 		    ((tor->MGMT_flags & (T_NEGOTIATE|T_CHECK)) &&
11827c478bd9Sstevel@tonic-gate 			(opt->status == T_READONLY));
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 		if (failed_option) {
11857c478bd9Sstevel@tonic-gate 			/*
11867c478bd9Sstevel@tonic-gate 			 * According to T_DEFAULT/T_CURRENT semantics, the
11877c478bd9Sstevel@tonic-gate 			 * input values, even if present, are to be ignored.
11887c478bd9Sstevel@tonic-gate 			 * Note: Specification is not clear on this, but we
11897c478bd9Sstevel@tonic-gate 			 * interpret that even though we ignore the values, we
11907c478bd9Sstevel@tonic-gate 			 * can return them as is. So we process them similar to
11917c478bd9Sstevel@tonic-gate 			 * T_CHECK/T_NEGOTIATE case which has the semantics to
11927c478bd9Sstevel@tonic-gate 			 * return the values as is. XXX If interpretation is
11937c478bd9Sstevel@tonic-gate 			 * ever determined incorrect fill in appropriate code
11947c478bd9Sstevel@tonic-gate 			 * here to treat T_DEFAULT/T_CURRENT differently.
11957c478bd9Sstevel@tonic-gate 			 *
11967c478bd9Sstevel@tonic-gate 			 * According to T_CHECK/T_NEGOTIATE semantics,
11977c478bd9Sstevel@tonic-gate 			 * in the case of T_NOTSUPPORT/T_FAILURE/T_READONLY,
11987c478bd9Sstevel@tonic-gate 			 * the semantics are to return the "value" part of
11997c478bd9Sstevel@tonic-gate 			 * option untouched. So here we copy the option
12007c478bd9Sstevel@tonic-gate 			 * head including value part if any to output.
12017c478bd9Sstevel@tonic-gate 			 */
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 			bcopy(opt, optr, opt->len);
12047c478bd9Sstevel@tonic-gate 			optr += _TPI_ALIGN_TOPT(opt->len);
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 			*worst_statusp = get_worst_status(opt->status,
12077c478bd9Sstevel@tonic-gate 			    *worst_statusp);
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 			/* skip to process next option in buffer */
12107c478bd9Sstevel@tonic-gate 			continue;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 		} /* end if "failed option" */
12137c478bd9Sstevel@tonic-gate 		/*
12147c478bd9Sstevel@tonic-gate 		 * The status is T_SUCCESS or T_READONLY
12157c478bd9Sstevel@tonic-gate 		 * We process the value part here
12167c478bd9Sstevel@tonic-gate 		 */
12177c478bd9Sstevel@tonic-gate 		ASSERT(opt->status == T_SUCCESS || opt->status == T_READONLY);
12187c478bd9Sstevel@tonic-gate 		switch (tor->MGMT_flags) {
12197c478bd9Sstevel@tonic-gate 		case T_DEFAULT:
12207c478bd9Sstevel@tonic-gate 			/*
12217c478bd9Sstevel@tonic-gate 			 * We fill default value from table or protocol specific
12227c478bd9Sstevel@tonic-gate 			 * function. If this call fails, we pass input through.
12237c478bd9Sstevel@tonic-gate 			 */
12247c478bd9Sstevel@tonic-gate 			if (do_opt_default(q, opt, &optr, worst_statusp,
12257c478bd9Sstevel@tonic-gate 			    cr, dbobjp) < 0) {
12267c478bd9Sstevel@tonic-gate 				/* fail or pass transparently */
12277c478bd9Sstevel@tonic-gate 				if (topmost_tpiprovider)
12287c478bd9Sstevel@tonic-gate 					opt->status = T_FAILURE;
12297c478bd9Sstevel@tonic-gate 				bcopy(opt, optr, opt->len);
12307c478bd9Sstevel@tonic-gate 				optr += _TPI_ALIGN_TOPT(opt->len);
12317c478bd9Sstevel@tonic-gate 				*worst_statusp = get_worst_status(opt->status,
12327c478bd9Sstevel@tonic-gate 				    *worst_statusp);
12337c478bd9Sstevel@tonic-gate 			}
12347c478bd9Sstevel@tonic-gate 			break;
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 		case T_CURRENT:
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 			do_opt_current(q, opt, &optr, worst_statusp, cr,
12397c478bd9Sstevel@tonic-gate 			    dbobjp);
12407c478bd9Sstevel@tonic-gate 			break;
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 		case T_CHECK:
12437c478bd9Sstevel@tonic-gate 		case T_NEGOTIATE:
12447c478bd9Sstevel@tonic-gate 			if (tor->MGMT_flags == T_CHECK)
12457c478bd9Sstevel@tonic-gate 				optset_context = SETFN_OPTCOM_CHECKONLY;
12467c478bd9Sstevel@tonic-gate 			else	/* T_NEGOTIATE */
12477c478bd9Sstevel@tonic-gate 				optset_context = SETFN_OPTCOM_NEGOTIATE;
12487c478bd9Sstevel@tonic-gate 			err = do_opt_check_or_negotiate(q, opt, optset_context,
12497c478bd9Sstevel@tonic-gate 			    &optr, worst_statusp, cr, dbobjp, first_mp);
12507c478bd9Sstevel@tonic-gate 			if (err == EINPROGRESS) {
12517c478bd9Sstevel@tonic-gate 				*queued_statusp = B_TRUE;
12527c478bd9Sstevel@tonic-gate 				return (0);
12537c478bd9Sstevel@tonic-gate 			}
12547c478bd9Sstevel@tonic-gate 			break;
12557c478bd9Sstevel@tonic-gate 		default:
12567c478bd9Sstevel@tonic-gate 			return (TBADFLAG);
12577c478bd9Sstevel@tonic-gate 		}
12587c478bd9Sstevel@tonic-gate 	} /* end for loop scanning option buffer */
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	ack_mp->b_wptr = optr;
12617c478bd9Sstevel@tonic-gate 	ASSERT(ack_mp->b_wptr <= ack_mp->b_datap->db_lim);
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	return (0);		/* OK return */
12647c478bd9Sstevel@tonic-gate }
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate static t_uscalar_t
12687c478bd9Sstevel@tonic-gate get_worst_status(t_uscalar_t status, t_uscalar_t current_worst_status)
12697c478bd9Sstevel@tonic-gate {
12707c478bd9Sstevel@tonic-gate 	/*
12717c478bd9Sstevel@tonic-gate 	 * Return the "worst" among the arguments "status" and
12727c478bd9Sstevel@tonic-gate 	 * "current_worst_status".
12737c478bd9Sstevel@tonic-gate 	 *
12747c478bd9Sstevel@tonic-gate 	 * Note: Tracking "worst_status" can be made a bit simpler
12757c478bd9Sstevel@tonic-gate 	 * if we use the property that status codes are bitwise
12767c478bd9Sstevel@tonic-gate 	 * distinct.
12777c478bd9Sstevel@tonic-gate 	 *
12787c478bd9Sstevel@tonic-gate 	 * The pecking order is
12797c478bd9Sstevel@tonic-gate 	 *
12807c478bd9Sstevel@tonic-gate 	 * T_SUCCESS ..... best
12817c478bd9Sstevel@tonic-gate 	 * T_PARTSUCCESS
12827c478bd9Sstevel@tonic-gate 	 * T_FAILURE
12837c478bd9Sstevel@tonic-gate 	 * T_READONLY
12847c478bd9Sstevel@tonic-gate 	 * T_NOTSUPPORT... worst
12857c478bd9Sstevel@tonic-gate 	 */
12867c478bd9Sstevel@tonic-gate 	if (status == current_worst_status)
12877c478bd9Sstevel@tonic-gate 		return (current_worst_status);
12887c478bd9Sstevel@tonic-gate 	switch (current_worst_status) {
12897c478bd9Sstevel@tonic-gate 	case T_SUCCESS:
12907c478bd9Sstevel@tonic-gate 		if (status == T_PARTSUCCESS)
12917c478bd9Sstevel@tonic-gate 			return (T_PARTSUCCESS);
12927c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
12937c478bd9Sstevel@tonic-gate 	case T_PARTSUCCESS:
12947c478bd9Sstevel@tonic-gate 		if (status == T_FAILURE)
12957c478bd9Sstevel@tonic-gate 			return (T_FAILURE);
12967c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
12977c478bd9Sstevel@tonic-gate 	case T_FAILURE:
12987c478bd9Sstevel@tonic-gate 		if (status == T_READONLY)
12997c478bd9Sstevel@tonic-gate 			return (T_READONLY);
13007c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
13017c478bd9Sstevel@tonic-gate 	case T_READONLY:
13027c478bd9Sstevel@tonic-gate 		if (status == T_NOTSUPPORT)
13037c478bd9Sstevel@tonic-gate 			return (T_NOTSUPPORT);
13047c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
13057c478bd9Sstevel@tonic-gate 	case T_NOTSUPPORT:
13067c478bd9Sstevel@tonic-gate 	default:
13077c478bd9Sstevel@tonic-gate 		return (current_worst_status);
13087c478bd9Sstevel@tonic-gate 	}
13097c478bd9Sstevel@tonic-gate }
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate static int
13127c478bd9Sstevel@tonic-gate do_opt_default(queue_t *q, struct T_opthdr *reqopt, uchar_t **resptrp,
13137c478bd9Sstevel@tonic-gate     t_uscalar_t *worst_statusp, cred_t *cr, optdb_obj_t *dbobjp)
13147c478bd9Sstevel@tonic-gate {
13157c478bd9Sstevel@tonic-gate 	pfi_t	deffn = dbobjp->odb_deffn;
13167c478bd9Sstevel@tonic-gate 	opdes_t	*opt_arr = dbobjp->odb_opt_des_arr;
13177c478bd9Sstevel@tonic-gate 	uint_t opt_arr_cnt = dbobjp->odb_opt_arr_cnt;
13187c478bd9Sstevel@tonic-gate 	boolean_t topmost_tpiprovider = dbobjp->odb_topmost_tpiprovider;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	struct T_opthdr *topth;
13217c478bd9Sstevel@tonic-gate 	opdes_t *optd;
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	if (reqopt->name != T_ALLOPT) {
13247c478bd9Sstevel@tonic-gate 		/*
13257c478bd9Sstevel@tonic-gate 		 * lookup the option in the table and fill default value
13267c478bd9Sstevel@tonic-gate 		 */
13277c478bd9Sstevel@tonic-gate 		optd = opt_chk_lookup(reqopt->level, reqopt->name,
13287c478bd9Sstevel@tonic-gate 		    opt_arr, opt_arr_cnt);
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 		if (optd == NULL) {
13317c478bd9Sstevel@tonic-gate 			/*
13327c478bd9Sstevel@tonic-gate 			 * not found - fail this one. Should not happen
13337c478bd9Sstevel@tonic-gate 			 * for topmost_tpiprovider as calling routine
13347c478bd9Sstevel@tonic-gate 			 * should have verified it.
13357c478bd9Sstevel@tonic-gate 			 */
13367c478bd9Sstevel@tonic-gate 			ASSERT(!topmost_tpiprovider);
13377c478bd9Sstevel@tonic-gate 			return (-1);
13387c478bd9Sstevel@tonic-gate 		}
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 		topth = (struct T_opthdr *)(*resptrp);
13417c478bd9Sstevel@tonic-gate 		topth->level = reqopt->level;
13427c478bd9Sstevel@tonic-gate 		topth->name = reqopt->name;
13437c478bd9Sstevel@tonic-gate 		topth->status = reqopt->status;
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 		*worst_statusp = get_worst_status(reqopt->status,
13467c478bd9Sstevel@tonic-gate 		    *worst_statusp);
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 		if (optd->opdes_props & OP_NODEFAULT) {
13497c478bd9Sstevel@tonic-gate 			/* header only, no default "value" part */
13507c478bd9Sstevel@tonic-gate 			topth->len = sizeof (struct T_opthdr);
13517c478bd9Sstevel@tonic-gate 			*resptrp += sizeof (struct T_opthdr);
13527c478bd9Sstevel@tonic-gate 		} else {
13537c478bd9Sstevel@tonic-gate 			int deflen;
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 			if (optd->opdes_props & OP_DEF_FN) {
13567c478bd9Sstevel@tonic-gate 				deflen = (*deffn)(q, reqopt->level,
13577c478bd9Sstevel@tonic-gate 				    reqopt->name, _TPI_TOPT_DATA(topth));
13587c478bd9Sstevel@tonic-gate 				if (deflen >= 0) {
13597c478bd9Sstevel@tonic-gate 					topth->len = (t_uscalar_t)
13607c478bd9Sstevel@tonic-gate 					    (sizeof (struct T_opthdr) + deflen);
13617c478bd9Sstevel@tonic-gate 				} else {
13627c478bd9Sstevel@tonic-gate 					/*
13637c478bd9Sstevel@tonic-gate 					 * return error, this should 'pass
13647c478bd9Sstevel@tonic-gate 					 * through' the option and maybe some
13657c478bd9Sstevel@tonic-gate 					 * other level will fill it in or
13667c478bd9Sstevel@tonic-gate 					 * already did.
13677c478bd9Sstevel@tonic-gate 					 * (No change in 'resptrp' upto here)
13687c478bd9Sstevel@tonic-gate 					 */
13697c478bd9Sstevel@tonic-gate 					return (-1);
13707c478bd9Sstevel@tonic-gate 				}
13717c478bd9Sstevel@tonic-gate 			} else {
13727c478bd9Sstevel@tonic-gate 				/* fill length and value part */
13737c478bd9Sstevel@tonic-gate 				switch (optd->opdes_size) {
13747c478bd9Sstevel@tonic-gate 				/*
13757c478bd9Sstevel@tonic-gate 				 * Since options are guaranteed aligned only
13767c478bd9Sstevel@tonic-gate 				 * on a 4 byte boundary (t_scalar_t) any
13777c478bd9Sstevel@tonic-gate 				 * option that is greater in size will default
13787c478bd9Sstevel@tonic-gate 				 * to the bcopy below
13797c478bd9Sstevel@tonic-gate 				 */
13807c478bd9Sstevel@tonic-gate 				case sizeof (int32_t):
13817c478bd9Sstevel@tonic-gate 					*(int32_t *)_TPI_TOPT_DATA(topth) =
13827c478bd9Sstevel@tonic-gate 					    (int32_t)optd->opdes_default;
13837c478bd9Sstevel@tonic-gate 					break;
13847c478bd9Sstevel@tonic-gate 				case sizeof (int16_t):
13857c478bd9Sstevel@tonic-gate 					*(int16_t *)_TPI_TOPT_DATA(topth) =
13867c478bd9Sstevel@tonic-gate 					    (int16_t)optd->opdes_default;
13877c478bd9Sstevel@tonic-gate 					break;
13887c478bd9Sstevel@tonic-gate 				case sizeof (int8_t):
13897c478bd9Sstevel@tonic-gate 					*(int8_t *)_TPI_TOPT_DATA(topth) =
13907c478bd9Sstevel@tonic-gate 					    (int8_t)optd->opdes_default;
13917c478bd9Sstevel@tonic-gate 					break;
13927c478bd9Sstevel@tonic-gate 				default:
13937c478bd9Sstevel@tonic-gate 					/*
13947c478bd9Sstevel@tonic-gate 					 * other length but still assume
13957c478bd9Sstevel@tonic-gate 					 * fixed - use bcopy
13967c478bd9Sstevel@tonic-gate 					 */
13977c478bd9Sstevel@tonic-gate 					bcopy(optd->opdes_defbuf,
13987c478bd9Sstevel@tonic-gate 					    _TPI_TOPT_DATA(topth),
13997c478bd9Sstevel@tonic-gate 					    optd->opdes_size);
14007c478bd9Sstevel@tonic-gate 					break;
14017c478bd9Sstevel@tonic-gate 				}
14027c478bd9Sstevel@tonic-gate 				topth->len = (t_uscalar_t)(optd->opdes_size +
14037c478bd9Sstevel@tonic-gate 				    sizeof (struct T_opthdr));
14047c478bd9Sstevel@tonic-gate 			}
14057c478bd9Sstevel@tonic-gate 			*resptrp += _TPI_ALIGN_TOPT(topth->len);
14067c478bd9Sstevel@tonic-gate 		}
14077c478bd9Sstevel@tonic-gate 		return (0);	/* OK return */
14087c478bd9Sstevel@tonic-gate 	}
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/*
14117c478bd9Sstevel@tonic-gate 	 * T_ALLOPT processing
14127c478bd9Sstevel@tonic-gate 	 *
14137c478bd9Sstevel@tonic-gate 	 * lookup and stuff default values of all the options of the
14147c478bd9Sstevel@tonic-gate 	 * level specified
14157c478bd9Sstevel@tonic-gate 	 * Note: This expansion of T_ALLOPT should happen in
14167c478bd9Sstevel@tonic-gate 	 * a topmost_tpiprovider.
14177c478bd9Sstevel@tonic-gate 	 */
14187c478bd9Sstevel@tonic-gate 	ASSERT(topmost_tpiprovider);
14197c478bd9Sstevel@tonic-gate 	for (optd = opt_arr; optd < &opt_arr[opt_arr_cnt]; optd++) {
14207c478bd9Sstevel@tonic-gate 		if (reqopt->level != optd->opdes_level)
14217c478bd9Sstevel@tonic-gate 			continue;
14227c478bd9Sstevel@tonic-gate 		/*
14237c478bd9Sstevel@tonic-gate 		 *
14247c478bd9Sstevel@tonic-gate 		 * T_DEFAULT semantics:
14257c478bd9Sstevel@tonic-gate 		 * XXX: we interpret T_DEFAULT semantics such that access to
14267c478bd9Sstevel@tonic-gate 		 * read value is required for access even the default value.
14277c478bd9Sstevel@tonic-gate 		 * Else option is ignored for T_ALLOPT request.
14287c478bd9Sstevel@tonic-gate 		 */
14297c478bd9Sstevel@tonic-gate 		if (!OA_READ_PERMISSION(optd, cr))
14307c478bd9Sstevel@tonic-gate 			/* skip this one */
14317c478bd9Sstevel@tonic-gate 			continue;
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 		/*
14347c478bd9Sstevel@tonic-gate 		 * Found option of same level as T_ALLOPT request
14357c478bd9Sstevel@tonic-gate 		 * that we can return.
14367c478bd9Sstevel@tonic-gate 		 */
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 		topth = (struct T_opthdr *)(*resptrp);
14397c478bd9Sstevel@tonic-gate 		topth->level = optd->opdes_level;
14407c478bd9Sstevel@tonic-gate 		topth->name = optd->opdes_name;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 		/*
14437c478bd9Sstevel@tonic-gate 		 * T_DEFAULT semantics:
14447c478bd9Sstevel@tonic-gate 		 * We know that read access is set. If no other access is set,
14457c478bd9Sstevel@tonic-gate 		 * then status is T_READONLY
14467c478bd9Sstevel@tonic-gate 		 */
14477c478bd9Sstevel@tonic-gate 		if (OA_READONLY_PERMISSION(optd, cr)) {
14487c478bd9Sstevel@tonic-gate 			topth->status = T_READONLY;
14497c478bd9Sstevel@tonic-gate 			*worst_statusp = get_worst_status(T_READONLY,
14507c478bd9Sstevel@tonic-gate 			    *worst_statusp);
14517c478bd9Sstevel@tonic-gate 		} else {
14527c478bd9Sstevel@tonic-gate 			topth->status = T_SUCCESS;
14537c478bd9Sstevel@tonic-gate 			/*
14547c478bd9Sstevel@tonic-gate 			 * Note: *worst_statusp has to be T_SUCCESS or
14557c478bd9Sstevel@tonic-gate 			 * worse so no need to adjust
14567c478bd9Sstevel@tonic-gate 			 */
14577c478bd9Sstevel@tonic-gate 		}
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 		if (optd->opdes_props & OP_NODEFAULT) {
14607c478bd9Sstevel@tonic-gate 			/* header only, no value part */
14617c478bd9Sstevel@tonic-gate 			topth->len = sizeof (struct T_opthdr);
14627c478bd9Sstevel@tonic-gate 			*resptrp += sizeof (struct T_opthdr);
14637c478bd9Sstevel@tonic-gate 		} else {
14647c478bd9Sstevel@tonic-gate 			int deflen;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 			if (optd->opdes_props & OP_DEF_FN) {
14677c478bd9Sstevel@tonic-gate 				deflen = (*deffn)(q, reqopt->level,
14687c478bd9Sstevel@tonic-gate 				    reqopt->name, _TPI_TOPT_DATA(topth));
14697c478bd9Sstevel@tonic-gate 				if (deflen >= 0) {
14707c478bd9Sstevel@tonic-gate 					topth->len = (t_uscalar_t)(deflen +
14717c478bd9Sstevel@tonic-gate 					    sizeof (struct T_opthdr));
14727c478bd9Sstevel@tonic-gate 				} else {
14737c478bd9Sstevel@tonic-gate 					/*
14747c478bd9Sstevel@tonic-gate 					 * deffn failed.
14757c478bd9Sstevel@tonic-gate 					 * return just the header as T_ALLOPT
14767c478bd9Sstevel@tonic-gate 					 * expansion.
14777c478bd9Sstevel@tonic-gate 					 * Some other level deffn may
14787c478bd9Sstevel@tonic-gate 					 * supply value part.
14797c478bd9Sstevel@tonic-gate 					 */
14807c478bd9Sstevel@tonic-gate 					topth->len = sizeof (struct T_opthdr);
14817c478bd9Sstevel@tonic-gate 					topth->status = T_FAILURE;
14827c478bd9Sstevel@tonic-gate 					*worst_statusp =
14837c478bd9Sstevel@tonic-gate 					    get_worst_status(T_FAILURE,
14847c478bd9Sstevel@tonic-gate 						*worst_statusp);
14857c478bd9Sstevel@tonic-gate 				}
14867c478bd9Sstevel@tonic-gate 			} else {
14877c478bd9Sstevel@tonic-gate 				/*
14887c478bd9Sstevel@tonic-gate 				 * fill length and value part from
14897c478bd9Sstevel@tonic-gate 				 * table
14907c478bd9Sstevel@tonic-gate 				 */
14917c478bd9Sstevel@tonic-gate 				switch (optd->opdes_size) {
14927c478bd9Sstevel@tonic-gate 				/*
14937c478bd9Sstevel@tonic-gate 				 * Since options are guaranteed aligned only
14947c478bd9Sstevel@tonic-gate 				 * on a 4 byte boundary (t_scalar_t) any
14957c478bd9Sstevel@tonic-gate 				 * option that is greater in size will default
14967c478bd9Sstevel@tonic-gate 				 * to the bcopy below
14977c478bd9Sstevel@tonic-gate 				 */
14987c478bd9Sstevel@tonic-gate 				case sizeof (int32_t):
14997c478bd9Sstevel@tonic-gate 					*(int32_t *)_TPI_TOPT_DATA(topth) =
15007c478bd9Sstevel@tonic-gate 					    (int32_t)optd->opdes_default;
15017c478bd9Sstevel@tonic-gate 					break;
15027c478bd9Sstevel@tonic-gate 				case sizeof (int16_t):
15037c478bd9Sstevel@tonic-gate 					*(int16_t *)_TPI_TOPT_DATA(topth) =
15047c478bd9Sstevel@tonic-gate 					    (int16_t)optd->opdes_default;
15057c478bd9Sstevel@tonic-gate 					break;
15067c478bd9Sstevel@tonic-gate 				case sizeof (int8_t):
15077c478bd9Sstevel@tonic-gate 					*(int8_t *)_TPI_TOPT_DATA(topth) =
15087c478bd9Sstevel@tonic-gate 					    (int8_t)optd->opdes_default;
15097c478bd9Sstevel@tonic-gate 					break;
15107c478bd9Sstevel@tonic-gate 				default:
15117c478bd9Sstevel@tonic-gate 					/*
15127c478bd9Sstevel@tonic-gate 					 * other length but still assume
15137c478bd9Sstevel@tonic-gate 					 * fixed - use bcopy
15147c478bd9Sstevel@tonic-gate 					 */
15157c478bd9Sstevel@tonic-gate 					bcopy(optd->opdes_defbuf,
15167c478bd9Sstevel@tonic-gate 					    _TPI_TOPT_DATA(topth),
15177c478bd9Sstevel@tonic-gate 					    optd->opdes_size);
15187c478bd9Sstevel@tonic-gate 				}
15197c478bd9Sstevel@tonic-gate 				topth->len = (t_uscalar_t)(optd->opdes_size +
15207c478bd9Sstevel@tonic-gate 				    sizeof (struct T_opthdr));
15217c478bd9Sstevel@tonic-gate 			}
15227c478bd9Sstevel@tonic-gate 			*resptrp += _TPI_ALIGN_TOPT(topth->len);
15237c478bd9Sstevel@tonic-gate 		}
15247c478bd9Sstevel@tonic-gate 	}
15257c478bd9Sstevel@tonic-gate 	return (0);
15267c478bd9Sstevel@tonic-gate }
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate static void
15297c478bd9Sstevel@tonic-gate do_opt_current(queue_t *q, struct T_opthdr *reqopt, uchar_t **resptrp,
15307c478bd9Sstevel@tonic-gate     t_uscalar_t *worst_statusp, cred_t *cr, optdb_obj_t *dbobjp)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate 	pfi_t	getfn = dbobjp->odb_getfn;
15337c478bd9Sstevel@tonic-gate 	opdes_t	*opt_arr = dbobjp->odb_opt_des_arr;
15347c478bd9Sstevel@tonic-gate 	uint_t opt_arr_cnt = dbobjp->odb_opt_arr_cnt;
15357c478bd9Sstevel@tonic-gate 	boolean_t topmost_tpiprovider = dbobjp->odb_topmost_tpiprovider;
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	struct T_opthdr *topth;
15387c478bd9Sstevel@tonic-gate 	opdes_t *optd;
15397c478bd9Sstevel@tonic-gate 	int optlen;
15407c478bd9Sstevel@tonic-gate 	uchar_t *initptr = *resptrp;
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	/*
15437c478bd9Sstevel@tonic-gate 	 * We call getfn to get the current value of an option. The call may
15447c478bd9Sstevel@tonic-gate 	 * fail in which case we copy the values from the input buffer. Maybe
15457c478bd9Sstevel@tonic-gate 	 * something downstream will fill it in or something upstream did.
15467c478bd9Sstevel@tonic-gate 	 */
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	if (reqopt->name != T_ALLOPT) {
15497c478bd9Sstevel@tonic-gate 		topth = (struct T_opthdr *)*resptrp;
15507c478bd9Sstevel@tonic-gate 		*resptrp += sizeof (struct T_opthdr);
15517c478bd9Sstevel@tonic-gate 		optlen = (*getfn)(q, reqopt->level, reqopt->name, *resptrp);
15527c478bd9Sstevel@tonic-gate 		if (optlen >= 0) {
15537c478bd9Sstevel@tonic-gate 			topth->len = (t_uscalar_t)(optlen +
15547c478bd9Sstevel@tonic-gate 			    sizeof (struct T_opthdr));
15557c478bd9Sstevel@tonic-gate 			topth->level = reqopt->level;
15567c478bd9Sstevel@tonic-gate 			topth->name = reqopt->name;
15577c478bd9Sstevel@tonic-gate 			topth->status = reqopt->status;
15587c478bd9Sstevel@tonic-gate 			*resptrp += _TPI_ALIGN_TOPT(optlen);
15597c478bd9Sstevel@tonic-gate 			*worst_statusp = get_worst_status(topth->status,
15607c478bd9Sstevel@tonic-gate 			    *worst_statusp);
15617c478bd9Sstevel@tonic-gate 		} else {
15627c478bd9Sstevel@tonic-gate 			/* failed - reset "*resptrp" pointer */
15637c478bd9Sstevel@tonic-gate 			*resptrp -= sizeof (struct T_opthdr);
15647c478bd9Sstevel@tonic-gate 		}
15657c478bd9Sstevel@tonic-gate 	} else {		/* T_ALLOPT processing */
15667c478bd9Sstevel@tonic-gate 		ASSERT(topmost_tpiprovider == B_TRUE);
15677c478bd9Sstevel@tonic-gate 		/* scan and get all options */
15687c478bd9Sstevel@tonic-gate 		for (optd = opt_arr; optd < &opt_arr[opt_arr_cnt]; optd++) {
15697c478bd9Sstevel@tonic-gate 			/* skip other levels */
15707c478bd9Sstevel@tonic-gate 			if (reqopt->level != optd->opdes_level)
15717c478bd9Sstevel@tonic-gate 				continue;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 			if (!OA_READ_PERMISSION(optd, cr))
15747c478bd9Sstevel@tonic-gate 				/* skip this one */
15757c478bd9Sstevel@tonic-gate 				continue;
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 			topth = (struct T_opthdr *)*resptrp;
15787c478bd9Sstevel@tonic-gate 			*resptrp += sizeof (struct T_opthdr);
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 			/* get option of this level */
15817c478bd9Sstevel@tonic-gate 			optlen = (*getfn)(q, reqopt->level, optd->opdes_name,
15827c478bd9Sstevel@tonic-gate 			    *resptrp);
15837c478bd9Sstevel@tonic-gate 			if (optlen >= 0) {
15847c478bd9Sstevel@tonic-gate 				/* success */
15857c478bd9Sstevel@tonic-gate 				topth->len = (t_uscalar_t)(optlen +
15867c478bd9Sstevel@tonic-gate 				    sizeof (struct T_opthdr));
15877c478bd9Sstevel@tonic-gate 				topth->level = reqopt->level;
15887c478bd9Sstevel@tonic-gate 				topth->name = optd->opdes_name;
15897c478bd9Sstevel@tonic-gate 				if (OA_READONLY_PERMISSION(optd, cr))
15907c478bd9Sstevel@tonic-gate 					topth->status = T_READONLY;
15917c478bd9Sstevel@tonic-gate 				else
15927c478bd9Sstevel@tonic-gate 					topth->status = T_SUCCESS;
15937c478bd9Sstevel@tonic-gate 				*resptrp += _TPI_ALIGN_TOPT(optlen);
15947c478bd9Sstevel@tonic-gate 			} else {
15957c478bd9Sstevel@tonic-gate 				/*
15967c478bd9Sstevel@tonic-gate 				 * failed, return as T_FAILURE and null value
15977c478bd9Sstevel@tonic-gate 				 * part. Maybe something downstream will
15987c478bd9Sstevel@tonic-gate 				 * handle this one and fill in a value. Here
15997c478bd9Sstevel@tonic-gate 				 * it is just part of T_ALLOPT expansion.
16007c478bd9Sstevel@tonic-gate 				 */
16017c478bd9Sstevel@tonic-gate 				topth->len = sizeof (struct T_opthdr);
16027c478bd9Sstevel@tonic-gate 				topth->level = reqopt->level;
16037c478bd9Sstevel@tonic-gate 				topth->name = optd->opdes_name;
16047c478bd9Sstevel@tonic-gate 				topth->status = T_FAILURE;
16057c478bd9Sstevel@tonic-gate 			}
16067c478bd9Sstevel@tonic-gate 			*worst_statusp = get_worst_status(topth->status,
16077c478bd9Sstevel@tonic-gate 			    *worst_statusp);
16087c478bd9Sstevel@tonic-gate 		} /* end for loop */
16097c478bd9Sstevel@tonic-gate 	}
16107c478bd9Sstevel@tonic-gate 	if (*resptrp == initptr) {
16117c478bd9Sstevel@tonic-gate 		/*
16127c478bd9Sstevel@tonic-gate 		 * getfn failed and does not want to handle this option. Maybe
16137c478bd9Sstevel@tonic-gate 		 * something downstream will or something upstream did. (If
16147c478bd9Sstevel@tonic-gate 		 * topmost_tpiprovider, initialize "status" to failure which
16157c478bd9Sstevel@tonic-gate 		 * can possibly change downstream). Copy the input "as is" from
16167c478bd9Sstevel@tonic-gate 		 * input option buffer if any to maintain transparency.
16177c478bd9Sstevel@tonic-gate 		 */
16187c478bd9Sstevel@tonic-gate 		if (topmost_tpiprovider)
16197c478bd9Sstevel@tonic-gate 			reqopt->status = T_FAILURE;
16207c478bd9Sstevel@tonic-gate 		bcopy(reqopt, *resptrp, reqopt->len);
16217c478bd9Sstevel@tonic-gate 		*resptrp += _TPI_ALIGN_TOPT(reqopt->len);
16227c478bd9Sstevel@tonic-gate 		*worst_statusp = get_worst_status(reqopt->status,
16237c478bd9Sstevel@tonic-gate 		    *worst_statusp);
16247c478bd9Sstevel@tonic-gate 	}
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate static int
16307c478bd9Sstevel@tonic-gate do_opt_check_or_negotiate(queue_t *q, struct T_opthdr *reqopt,
16317c478bd9Sstevel@tonic-gate     uint_t optset_context, uchar_t **resptrp, t_uscalar_t *worst_statusp,
16327c478bd9Sstevel@tonic-gate     cred_t *cr, optdb_obj_t *dbobjp, mblk_t *first_mp)
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate 	pfi_t	deffn = dbobjp->odb_deffn;
16357c478bd9Sstevel@tonic-gate 	opt_set_fn setfn = dbobjp->odb_setfn;
16367c478bd9Sstevel@tonic-gate 	opdes_t	*opt_arr = dbobjp->odb_opt_des_arr;
16377c478bd9Sstevel@tonic-gate 	uint_t opt_arr_cnt = dbobjp->odb_opt_arr_cnt;
16387c478bd9Sstevel@tonic-gate 	boolean_t topmost_tpiprovider = dbobjp->odb_topmost_tpiprovider;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	struct T_opthdr *topth;
16417c478bd9Sstevel@tonic-gate 	opdes_t *optd;
16427c478bd9Sstevel@tonic-gate 	int error;
16437c478bd9Sstevel@tonic-gate 	t_uscalar_t optlen;
16447c478bd9Sstevel@tonic-gate 	t_scalar_t optsize;
16457c478bd9Sstevel@tonic-gate 	uchar_t *initptr = *resptrp;
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	ASSERT(reqopt->status == T_SUCCESS);
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	if (reqopt->name != T_ALLOPT) {
16507c478bd9Sstevel@tonic-gate 		topth = (struct T_opthdr *)*resptrp;
16517c478bd9Sstevel@tonic-gate 		*resptrp += sizeof (struct T_opthdr);
16527c478bd9Sstevel@tonic-gate 		error = (*setfn)(q, optset_context, reqopt->level, reqopt->name,
16537c478bd9Sstevel@tonic-gate 		    reqopt->len - sizeof (struct T_opthdr),
16547c478bd9Sstevel@tonic-gate 		    _TPI_TOPT_DATA(reqopt), &optlen, _TPI_TOPT_DATA(topth),
16557c478bd9Sstevel@tonic-gate 		    NULL, cr, first_mp);
16567c478bd9Sstevel@tonic-gate 		if (error) {
16577c478bd9Sstevel@tonic-gate 			/* failed - reset "*resptrp" */
16587c478bd9Sstevel@tonic-gate 			*resptrp -= sizeof (struct T_opthdr);
16597c478bd9Sstevel@tonic-gate 			if (error == EINPROGRESS)
16607c478bd9Sstevel@tonic-gate 				return (error);
16617c478bd9Sstevel@tonic-gate 		} else {
16627c478bd9Sstevel@tonic-gate 			/*
16637c478bd9Sstevel@tonic-gate 			 * success - "value" already filled in setfn()
16647c478bd9Sstevel@tonic-gate 			 */
16657c478bd9Sstevel@tonic-gate 			topth->len = (t_uscalar_t)(optlen +
16667c478bd9Sstevel@tonic-gate 			    sizeof (struct T_opthdr));
16677c478bd9Sstevel@tonic-gate 			topth->level = reqopt->level;
16687c478bd9Sstevel@tonic-gate 			topth->name = reqopt->name;
16697c478bd9Sstevel@tonic-gate 			topth->status = reqopt->status;
16707c478bd9Sstevel@tonic-gate 			*resptrp += _TPI_ALIGN_TOPT(optlen);
16717c478bd9Sstevel@tonic-gate 			*worst_statusp = get_worst_status(topth->status,
16727c478bd9Sstevel@tonic-gate 			    *worst_statusp);
16737c478bd9Sstevel@tonic-gate 		}
16747c478bd9Sstevel@tonic-gate 	} else {		/* T_ALLOPT processing */
16757c478bd9Sstevel@tonic-gate 		/* only for T_NEGOTIATE case */
16767c478bd9Sstevel@tonic-gate 		ASSERT(optset_context == SETFN_OPTCOM_NEGOTIATE);
16777c478bd9Sstevel@tonic-gate 		ASSERT(topmost_tpiprovider == B_TRUE);
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 		/* scan and set all options to default value */
16807c478bd9Sstevel@tonic-gate 		for (optd = opt_arr; optd < &opt_arr[opt_arr_cnt]; optd++) {
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 			/* skip other levels */
16837c478bd9Sstevel@tonic-gate 			if (reqopt->level != optd->opdes_level)
16847c478bd9Sstevel@tonic-gate 				continue;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 			if (OA_EXECUTE_PERMISSION(optd, cr) ||
16877c478bd9Sstevel@tonic-gate 			    OA_NO_PERMISSION(optd, cr)) {
16887c478bd9Sstevel@tonic-gate 				/*
16897c478bd9Sstevel@tonic-gate 				 * skip this one too. Does not make sense to
16907c478bd9Sstevel@tonic-gate 				 * set anything to default value for "execute"
16917c478bd9Sstevel@tonic-gate 				 * options.
16927c478bd9Sstevel@tonic-gate 				 */
16937c478bd9Sstevel@tonic-gate 				continue;
16947c478bd9Sstevel@tonic-gate 			}
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 			if (OA_READONLY_PERMISSION(optd, cr)) {
16977c478bd9Sstevel@tonic-gate 				/*
16987c478bd9Sstevel@tonic-gate 				 * Return with T_READONLY status (and no value
16997c478bd9Sstevel@tonic-gate 				 * part). Note: spec is not clear but
17007c478bd9Sstevel@tonic-gate 				 * XTI test suite needs this.
17017c478bd9Sstevel@tonic-gate 				 */
17027c478bd9Sstevel@tonic-gate 				topth = (struct T_opthdr *)*resptrp;
17037c478bd9Sstevel@tonic-gate 				topth->len = sizeof (struct T_opthdr);
17047c478bd9Sstevel@tonic-gate 				*resptrp += topth->len;
17057c478bd9Sstevel@tonic-gate 				topth->level = reqopt->level;
17067c478bd9Sstevel@tonic-gate 				topth->name = optd->opdes_name;
17077c478bd9Sstevel@tonic-gate 				topth->status = T_READONLY;
17087c478bd9Sstevel@tonic-gate 				*worst_statusp = get_worst_status(topth->status,
17097c478bd9Sstevel@tonic-gate 				    *worst_statusp);
17107c478bd9Sstevel@tonic-gate 				continue;
17117c478bd9Sstevel@tonic-gate 			}
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 			/*
17147c478bd9Sstevel@tonic-gate 			 * It is not read only or execute type
17157c478bd9Sstevel@tonic-gate 			 * the it must have write permission
17167c478bd9Sstevel@tonic-gate 			 */
17177c478bd9Sstevel@tonic-gate 			ASSERT(OA_WRITE_PERMISSION(optd, cr));
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 			topth = (struct T_opthdr *)*resptrp;
17207c478bd9Sstevel@tonic-gate 			*resptrp += sizeof (struct T_opthdr);
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 			topth->len = sizeof (struct T_opthdr);
17237c478bd9Sstevel@tonic-gate 			topth->level = reqopt->level;
17247c478bd9Sstevel@tonic-gate 			topth->name = optd->opdes_name;
17257c478bd9Sstevel@tonic-gate 			if (optd->opdes_props & OP_NODEFAULT) {
17267c478bd9Sstevel@tonic-gate 				/*
17277c478bd9Sstevel@tonic-gate 				 * Option of "no default value" so it does not
17287c478bd9Sstevel@tonic-gate 				 * make sense to try to set it. We just return
17297c478bd9Sstevel@tonic-gate 				 * header with status of T_SUCCESS
17307c478bd9Sstevel@tonic-gate 				 * XXX should this be failure ?
17317c478bd9Sstevel@tonic-gate 				 */
17327c478bd9Sstevel@tonic-gate 				topth->status = T_SUCCESS;
17337c478bd9Sstevel@tonic-gate 				continue; /* skip setting */
17347c478bd9Sstevel@tonic-gate 			}
17357c478bd9Sstevel@tonic-gate 			if (optd->opdes_props & OP_DEF_FN) {
17367c478bd9Sstevel@tonic-gate 				if ((optd->opdes_props & OP_VARLEN) ||
17377c478bd9Sstevel@tonic-gate 				    ((optsize = (*deffn)(q, reqopt->level,
17387c478bd9Sstevel@tonic-gate 					optd->opdes_name,
17397c478bd9Sstevel@tonic-gate 					(uchar_t *)optd->opdes_defbuf)) < 0)) {
17407c478bd9Sstevel@tonic-gate 					/* XXX - skip these too */
17417c478bd9Sstevel@tonic-gate 					topth->status = T_SUCCESS;
17427c478bd9Sstevel@tonic-gate 					continue; /* skip setting */
17437c478bd9Sstevel@tonic-gate 				}
17447c478bd9Sstevel@tonic-gate 			} else {
17457c478bd9Sstevel@tonic-gate 				optsize = optd->opdes_size;
17467c478bd9Sstevel@tonic-gate 			}
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 			/* set option of this level */
17507c478bd9Sstevel@tonic-gate 			error = (*setfn)(q, SETFN_OPTCOM_NEGOTIATE,
17517c478bd9Sstevel@tonic-gate 			    reqopt->level, optd->opdes_name, optsize,
17527c478bd9Sstevel@tonic-gate 			    (uchar_t *)optd->opdes_defbuf, &optlen,
17537c478bd9Sstevel@tonic-gate 			    _TPI_TOPT_DATA(topth), NULL, cr, NULL);
17547c478bd9Sstevel@tonic-gate 			if (error) {
17557c478bd9Sstevel@tonic-gate 				/*
17567c478bd9Sstevel@tonic-gate 				 * failed, return as T_FAILURE and null value
17577c478bd9Sstevel@tonic-gate 				 * part. Maybe something downstream will
17587c478bd9Sstevel@tonic-gate 				 * handle this one and fill in a value. Here
17597c478bd9Sstevel@tonic-gate 				 * it is just part of T_ALLOPT expansion.
17607c478bd9Sstevel@tonic-gate 				 */
17617c478bd9Sstevel@tonic-gate 				topth->status = T_FAILURE;
17627c478bd9Sstevel@tonic-gate 				*worst_statusp = get_worst_status(topth->status,
17637c478bd9Sstevel@tonic-gate 				    *worst_statusp);
17647c478bd9Sstevel@tonic-gate 			} else {
17657c478bd9Sstevel@tonic-gate 				/* success */
17667c478bd9Sstevel@tonic-gate 				topth->len += optlen;
17677c478bd9Sstevel@tonic-gate 				topth->status = T_SUCCESS;
17687c478bd9Sstevel@tonic-gate 				*resptrp += _TPI_ALIGN_TOPT(optlen);
17697c478bd9Sstevel@tonic-gate 			}
17707c478bd9Sstevel@tonic-gate 		} /* end for loop */
17717c478bd9Sstevel@tonic-gate 		/* END T_ALLOPT */
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	if (*resptrp == initptr) {
17757c478bd9Sstevel@tonic-gate 		/*
17767c478bd9Sstevel@tonic-gate 		 * setfn failed and does not want to handle this option. Maybe
17777c478bd9Sstevel@tonic-gate 		 * something downstream will or something upstream
17787c478bd9Sstevel@tonic-gate 		 * did. Copy the input as is from input option buffer if any to
17797c478bd9Sstevel@tonic-gate 		 * maintain transparency (maybe something at a level above
17807c478bd9Sstevel@tonic-gate 		 * did something.
17817c478bd9Sstevel@tonic-gate 		 */
17827c478bd9Sstevel@tonic-gate 		if (topmost_tpiprovider)
17837c478bd9Sstevel@tonic-gate 			reqopt->status = T_FAILURE;
17847c478bd9Sstevel@tonic-gate 		bcopy(reqopt, *resptrp, reqopt->len);
17857c478bd9Sstevel@tonic-gate 		*resptrp += _TPI_ALIGN_TOPT(reqopt->len);
17867c478bd9Sstevel@tonic-gate 		*worst_statusp = get_worst_status(reqopt->status,
17877c478bd9Sstevel@tonic-gate 		    *worst_statusp);
17887c478bd9Sstevel@tonic-gate 	}
17897c478bd9Sstevel@tonic-gate 	return (0);
17907c478bd9Sstevel@tonic-gate }
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate /*
17937c478bd9Sstevel@tonic-gate  * The following routines process options buffer passed with
17947c478bd9Sstevel@tonic-gate  * T_CONN_REQ, T_CONN_RES and T_UNITDATA_REQ.
17957c478bd9Sstevel@tonic-gate  * This routine does the consistency check applied to the
17967c478bd9Sstevel@tonic-gate  * sanity of formatting of multiple options packed in the
17977c478bd9Sstevel@tonic-gate  * buffer.
17987c478bd9Sstevel@tonic-gate  *
17997c478bd9Sstevel@tonic-gate  * XTI brain damage alert:
18007c478bd9Sstevel@tonic-gate  * XTI interface adopts the notion of an option being an
18017c478bd9Sstevel@tonic-gate  * "absolute requirement" from OSI transport service (but applies
18027c478bd9Sstevel@tonic-gate  * it to all transports including Internet transports).
18037c478bd9Sstevel@tonic-gate  * The main effect of that is action on failure to "negotiate" a
18047c478bd9Sstevel@tonic-gate  * requested option to the exact requested value
18057c478bd9Sstevel@tonic-gate  *
18067c478bd9Sstevel@tonic-gate  *          - if the option is an "absolute requirement", the primitive
18077c478bd9Sstevel@tonic-gate  *            is aborted (e.g T_DISCON_REQ or T_UDERR generated)
18087c478bd9Sstevel@tonic-gate  *          - if the option is NOT and "absolute requirement" it can
18097c478bd9Sstevel@tonic-gate  *            just be ignored.
18107c478bd9Sstevel@tonic-gate  *
18117c478bd9Sstevel@tonic-gate  * We would not support "negotiating" of options on connection
18127c478bd9Sstevel@tonic-gate  * primitives for Internet transports. However just in case we
18137c478bd9Sstevel@tonic-gate  * forced to in order to pass strange test suites, the design here
18147c478bd9Sstevel@tonic-gate  * tries to support these notions.
18157c478bd9Sstevel@tonic-gate  *
18167c478bd9Sstevel@tonic-gate  * tpi_optcom_buf(q, mp, opt_lenp, opt_offset, cred, dbobjp, thisdg_attrs,
18177c478bd9Sstevel@tonic-gate  *	*is_absreq_failurep)
18187c478bd9Sstevel@tonic-gate  *
18197c478bd9Sstevel@tonic-gate  * - Verify the option buffer, if formatted badly, return error 1
18207c478bd9Sstevel@tonic-gate  *
18217c478bd9Sstevel@tonic-gate  * - If it is a "permissions" failure (read-only), return error 2
18227c478bd9Sstevel@tonic-gate  *
18237c478bd9Sstevel@tonic-gate  * - Else, process the option "in place", the following can happen,
18247c478bd9Sstevel@tonic-gate  *	     - if a "privileged" option, mark it as "ignored".
18257c478bd9Sstevel@tonic-gate  *	     - if "not supported", mark "ignored"
18267c478bd9Sstevel@tonic-gate  *	     - if "supported" attempt negotiation and fill result in
18277c478bd9Sstevel@tonic-gate  *	       the outcome
18287c478bd9Sstevel@tonic-gate  *			- if "absolute requirement", set "*is_absreq_failurep"
18297c478bd9Sstevel@tonic-gate  *			- if NOT an "absolute requirement", then our
18307c478bd9Sstevel@tonic-gate  *			  interpretation is to mark is at ignored if
18317c478bd9Sstevel@tonic-gate  *			  negotiation fails (Spec allows partial success
18327c478bd9Sstevel@tonic-gate  *			  as in OSI protocols but not failure)
18337c478bd9Sstevel@tonic-gate  *
18347c478bd9Sstevel@tonic-gate  *   Then delete "ignored" options from option buffer and return success.
18357c478bd9Sstevel@tonic-gate  *
18367c478bd9Sstevel@tonic-gate  */
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate int
18397c478bd9Sstevel@tonic-gate tpi_optcom_buf(queue_t *q, mblk_t *mp, t_scalar_t *opt_lenp,
18407c478bd9Sstevel@tonic-gate     t_scalar_t opt_offset, cred_t *cr, optdb_obj_t *dbobjp,
18417c478bd9Sstevel@tonic-gate     void *thisdg_attrs, int *is_absreq_failurep)
18427c478bd9Sstevel@tonic-gate {
18437c478bd9Sstevel@tonic-gate 	opt_set_fn setfn = dbobjp->odb_setfn;
18447c478bd9Sstevel@tonic-gate 	opdes_t *opt_arr = dbobjp->odb_opt_des_arr;
18457c478bd9Sstevel@tonic-gate 	uint_t opt_arr_cnt = dbobjp->odb_opt_arr_cnt;
18467c478bd9Sstevel@tonic-gate 	struct T_opthdr *opt, *opt_start, *opt_end;
18477c478bd9Sstevel@tonic-gate 	mblk_t  *copy_mp_head;
18487c478bd9Sstevel@tonic-gate 	uchar_t *optr, *init_optr;
18497c478bd9Sstevel@tonic-gate 	opdes_t *optd;
18507c478bd9Sstevel@tonic-gate 	uint_t optset_context;
18517c478bd9Sstevel@tonic-gate 	t_uscalar_t olen;
18527c478bd9Sstevel@tonic-gate 	int error = 0;
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	ASSERT((uchar_t *)opt_lenp > mp->b_rptr &&
18557c478bd9Sstevel@tonic-gate 	    (uchar_t *)opt_lenp < mp->b_wptr);
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	copy_mp_head = NULL;
18587c478bd9Sstevel@tonic-gate 	*is_absreq_failurep = 0;
18597c478bd9Sstevel@tonic-gate 	switch (((union T_primitives *)mp->b_rptr)->type) {
18607c478bd9Sstevel@tonic-gate 	case T_CONN_REQ:
18617c478bd9Sstevel@tonic-gate 	case T_CONN_RES:
18627c478bd9Sstevel@tonic-gate 		optset_context = SETFN_CONN_NEGOTIATE;
18637c478bd9Sstevel@tonic-gate 		break;
18647c478bd9Sstevel@tonic-gate 	case T_UNITDATA_REQ:
18657c478bd9Sstevel@tonic-gate 		optset_context = SETFN_UD_NEGOTIATE;
18667c478bd9Sstevel@tonic-gate 		break;
18677c478bd9Sstevel@tonic-gate 	default:
18687c478bd9Sstevel@tonic-gate 		/*
18697c478bd9Sstevel@tonic-gate 		 * should never get here, all possible TPI primitives
18707c478bd9Sstevel@tonic-gate 		 * where this can be called from should be accounted
18717c478bd9Sstevel@tonic-gate 		 * for in the cases above
18727c478bd9Sstevel@tonic-gate 		 */
18737c478bd9Sstevel@tonic-gate 		return (EINVAL);
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	if ((opt_start = (struct T_opthdr *)
18777c478bd9Sstevel@tonic-gate 	    mi_offset_param(mp, opt_offset, *opt_lenp)) == NULL) {
18787c478bd9Sstevel@tonic-gate 		error = ENOPROTOOPT;
18797c478bd9Sstevel@tonic-gate 		goto error_ret;
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 	if (!__TPI_TOPT_ISALIGNED(opt_start)) {
18827c478bd9Sstevel@tonic-gate 		error = ENOPROTOOPT;
18837c478bd9Sstevel@tonic-gate 		goto error_ret;
18847c478bd9Sstevel@tonic-gate 	}
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	opt_end = (struct T_opthdr *)((uchar_t *)opt_start
18877c478bd9Sstevel@tonic-gate 	    + *opt_lenp);
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	if ((copy_mp_head = copyb(mp)) == (mblk_t *)NULL) {
18907c478bd9Sstevel@tonic-gate 		error = ENOMEM;
18917c478bd9Sstevel@tonic-gate 		goto error_ret;
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	init_optr = optr = (uchar_t *)&copy_mp_head->b_rptr[opt_offset];
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	for (opt = opt_start; opt && (opt < opt_end);
18977c478bd9Sstevel@tonic-gate 	    opt = _TPI_TOPT_NEXTHDR(opt_start, *opt_lenp, opt)) {
18987c478bd9Sstevel@tonic-gate 		/*
18997c478bd9Sstevel@tonic-gate 		 * Validate the option for length and alignment
19007c478bd9Sstevel@tonic-gate 		 * before accessing anything in it
19017c478bd9Sstevel@tonic-gate 		 */
19027c478bd9Sstevel@tonic-gate 		if (!_TPI_TOPT_VALID(opt, opt_start, opt_end)) {
19037c478bd9Sstevel@tonic-gate 			error = ENOPROTOOPT;
19047c478bd9Sstevel@tonic-gate 			goto error_ret;
19057c478bd9Sstevel@tonic-gate 		}
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 		/* Find the option in the opt_arr. */
19087c478bd9Sstevel@tonic-gate 		optd = opt_chk_lookup(opt->level, opt->name,
19097c478bd9Sstevel@tonic-gate 		    opt_arr, opt_arr_cnt);
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 		if (optd == NULL) {
19127c478bd9Sstevel@tonic-gate 			/*
19137c478bd9Sstevel@tonic-gate 			 * Option not found
19147c478bd9Sstevel@tonic-gate 			 */
19157c478bd9Sstevel@tonic-gate 			opt->status = T_NOTSUPPORT;
19167c478bd9Sstevel@tonic-gate 			continue;
19177c478bd9Sstevel@tonic-gate 		}
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 		/*
19207c478bd9Sstevel@tonic-gate 		 * Weird but as in XTI spec.
19217c478bd9Sstevel@tonic-gate 		 * Sec 6.3.6 "Privileged and ReadOnly Options"
19227c478bd9Sstevel@tonic-gate 		 * Permission problems (e.g.readonly) fail with bad access
19237c478bd9Sstevel@tonic-gate 		 * BUT "privileged" option request from those NOT PRIVILEGED
19247c478bd9Sstevel@tonic-gate 		 * are to be merely "ignored".
19257c478bd9Sstevel@tonic-gate 		 * XXX Prevents "probing" of privileged options ?
19267c478bd9Sstevel@tonic-gate 		 */
19277c478bd9Sstevel@tonic-gate 		if (OA_READONLY_PERMISSION(optd, cr)) {
19287c478bd9Sstevel@tonic-gate 			error = EACCES;
19297c478bd9Sstevel@tonic-gate 			goto error_ret;
19307c478bd9Sstevel@tonic-gate 		}
19317c478bd9Sstevel@tonic-gate 		if (OA_MATCHED_PRIV(optd, cr)) {
19327c478bd9Sstevel@tonic-gate 			/*
19337c478bd9Sstevel@tonic-gate 			 * For privileged options, we DO perform
19347c478bd9Sstevel@tonic-gate 			 * access checks as is common sense
19357c478bd9Sstevel@tonic-gate 			 */
19367c478bd9Sstevel@tonic-gate 			if (!OA_WX_ANYPRIV(optd)) {
19377c478bd9Sstevel@tonic-gate 				error = EACCES;
19387c478bd9Sstevel@tonic-gate 				goto error_ret;
19397c478bd9Sstevel@tonic-gate 			}
19407c478bd9Sstevel@tonic-gate 		} else {
19417c478bd9Sstevel@tonic-gate 			/*
19427c478bd9Sstevel@tonic-gate 			 * For non privileged, we fail instead following
19437c478bd9Sstevel@tonic-gate 			 * "ignore" semantics dictated by XTI spec for
19447c478bd9Sstevel@tonic-gate 			 * permissions problems.
19457c478bd9Sstevel@tonic-gate 			 * Sec 6.3.6 "Privileged and ReadOnly Options"
19467c478bd9Sstevel@tonic-gate 			 * XXX Should we do "ignore" semantics ?
19477c478bd9Sstevel@tonic-gate 			 */
19487c478bd9Sstevel@tonic-gate 			if (!OA_WX_NOPRIV(optd)) { /* nopriv */
19497c478bd9Sstevel@tonic-gate 				opt->status = T_FAILURE;
19507c478bd9Sstevel@tonic-gate 				continue;
19517c478bd9Sstevel@tonic-gate 			}
19527c478bd9Sstevel@tonic-gate 		}
19537c478bd9Sstevel@tonic-gate 		/*
19547c478bd9Sstevel@tonic-gate 		 *
19557c478bd9Sstevel@tonic-gate 		 * If the negotiation fails, for options that
19567c478bd9Sstevel@tonic-gate 		 * are "absolute requirement", it is a fatal error.
19577c478bd9Sstevel@tonic-gate 		 * For options that are NOT "absolute requirements",
19587c478bd9Sstevel@tonic-gate 		 * and the value fails to negotiate, the XTI spec
19597c478bd9Sstevel@tonic-gate 		 * only considers the possibility of partial success
19607c478bd9Sstevel@tonic-gate 		 * (T_PARTSUCCES - not likely for Internet protocols).
19617c478bd9Sstevel@tonic-gate 		 * The spec is in denial about complete failure
19627c478bd9Sstevel@tonic-gate 		 * (T_FAILURE) to negotiate for options that are
19637c478bd9Sstevel@tonic-gate 		 * carried on T_CONN_REQ/T_CONN_RES/T_UNITDATA
19647c478bd9Sstevel@tonic-gate 		 * We interpret the T_FAILURE to negotiate an option
19657c478bd9Sstevel@tonic-gate 		 * that is NOT an absolute requirement that it is safe
19667c478bd9Sstevel@tonic-gate 		 * to ignore it.
19677c478bd9Sstevel@tonic-gate 		 */
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 		/* verify length */
19707c478bd9Sstevel@tonic-gate 		if (!opt_length_ok(optd, opt)) {
19717c478bd9Sstevel@tonic-gate 			/* bad size */
19727c478bd9Sstevel@tonic-gate 			if ((optd->opdes_props & OP_NOT_ABSREQ) == 0) {
19737c478bd9Sstevel@tonic-gate 				/* option is absolute requirement */
19747c478bd9Sstevel@tonic-gate 				*is_absreq_failurep = 1;
19757c478bd9Sstevel@tonic-gate 				error = EINVAL;
19767c478bd9Sstevel@tonic-gate 				goto error_ret;
19777c478bd9Sstevel@tonic-gate 			}
19787c478bd9Sstevel@tonic-gate 			opt->status = T_FAILURE;
19797c478bd9Sstevel@tonic-gate 			continue;
19807c478bd9Sstevel@tonic-gate 		}
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		/*
19837c478bd9Sstevel@tonic-gate 		 * verified generic attributes. Now call set function.
19847c478bd9Sstevel@tonic-gate 		 * Note: We assume the following to simplify code.
19857c478bd9Sstevel@tonic-gate 		 * XXX If this is found not to be valid, this routine
19867c478bd9Sstevel@tonic-gate 		 * will need to be rewritten. At this point it would
19877c478bd9Sstevel@tonic-gate 		 * be premature to introduce more complexity than is
19887c478bd9Sstevel@tonic-gate 		 * needed.
19897c478bd9Sstevel@tonic-gate 		 * Assumption: For variable length options, we assume
19907c478bd9Sstevel@tonic-gate 		 * that the value returned will be same or less length
19917c478bd9Sstevel@tonic-gate 		 * (size does not increase). This makes it OK to pass the
19927c478bd9Sstevel@tonic-gate 		 * same space for output as it is on input.
19937c478bd9Sstevel@tonic-gate 		 */
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 		error = (*setfn)(q, optset_context, opt->level, opt->name,
19967c478bd9Sstevel@tonic-gate 		    opt->len - (t_uscalar_t)sizeof (struct T_opthdr),
19977c478bd9Sstevel@tonic-gate 		    _TPI_TOPT_DATA(opt), &olen, _TPI_TOPT_DATA(opt),
19987c478bd9Sstevel@tonic-gate 		    thisdg_attrs, cr, NULL);
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 		if (olen > (int)(opt->len - sizeof (struct T_opthdr))) {
20017c478bd9Sstevel@tonic-gate 			/*
20027c478bd9Sstevel@tonic-gate 			 * Space on output more than space on input. Should
20037c478bd9Sstevel@tonic-gate 			 * not happen and we consider it a bug/error.
20047c478bd9Sstevel@tonic-gate 			 * More of a restriction than an error in our
20057c478bd9Sstevel@tonic-gate 			 * implementation. Will see if we can live with this
20067c478bd9Sstevel@tonic-gate 			 * otherwise code will get more hairy with multiple
20077c478bd9Sstevel@tonic-gate 			 * passes.
20087c478bd9Sstevel@tonic-gate 			 */
20097c478bd9Sstevel@tonic-gate 			error = EINVAL;
20107c478bd9Sstevel@tonic-gate 			goto error_ret;
20117c478bd9Sstevel@tonic-gate 		}
20127c478bd9Sstevel@tonic-gate 		if (error != 0) {
20137c478bd9Sstevel@tonic-gate 			if ((optd->opdes_props & OP_NOT_ABSREQ) == 0) {
20147c478bd9Sstevel@tonic-gate 				/* option is absolute requirement. */
20157c478bd9Sstevel@tonic-gate 				*is_absreq_failurep = 1;
20167c478bd9Sstevel@tonic-gate 				goto error_ret;
20177c478bd9Sstevel@tonic-gate 			}
20187c478bd9Sstevel@tonic-gate 			/*
20197c478bd9Sstevel@tonic-gate 			 * failed - but option "not an absolute
20207c478bd9Sstevel@tonic-gate 			 * requirement"
20217c478bd9Sstevel@tonic-gate 			 */
20227c478bd9Sstevel@tonic-gate 			opt->status = T_FAILURE;
20237c478bd9Sstevel@tonic-gate 			continue;
20247c478bd9Sstevel@tonic-gate 		}
20257c478bd9Sstevel@tonic-gate 		/*
20267c478bd9Sstevel@tonic-gate 		 * Fill in the only possible successful result
20277c478bd9Sstevel@tonic-gate 		 * (Note: TPI allows for T_PARTSUCCESS - partial
20287c478bd9Sstevel@tonic-gate 		 * sucess result code which is relevant in OSI world
20297c478bd9Sstevel@tonic-gate 		 * and not possible in Internet code)
20307c478bd9Sstevel@tonic-gate 		 */
20317c478bd9Sstevel@tonic-gate 		opt->status = T_SUCCESS;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 		/*
20347c478bd9Sstevel@tonic-gate 		 * Add T_SUCCESS result code options to the "output" options.
20357c478bd9Sstevel@tonic-gate 		 * No T_FAILURES or T_NOTSUPPORT here as they are to be
20367c478bd9Sstevel@tonic-gate 		 * ignored.
20377c478bd9Sstevel@tonic-gate 		 * This code assumes output option buffer will
20387c478bd9Sstevel@tonic-gate 		 * be <= input option buffer.
20397c478bd9Sstevel@tonic-gate 		 *
20407c478bd9Sstevel@tonic-gate 		 * Copy option header+value
20417c478bd9Sstevel@tonic-gate 		 */
20427c478bd9Sstevel@tonic-gate 		bcopy(opt, optr, opt->len);
20437c478bd9Sstevel@tonic-gate 		optr +=  _TPI_ALIGN_TOPT(opt->len);
20447c478bd9Sstevel@tonic-gate 	}
20457c478bd9Sstevel@tonic-gate 	/*
20467c478bd9Sstevel@tonic-gate 	 * Overwrite the input mblk option buffer now with the output
20477c478bd9Sstevel@tonic-gate 	 * and update length, and contents in original mbl
20487c478bd9Sstevel@tonic-gate 	 * (offset remains unchanged).
20497c478bd9Sstevel@tonic-gate 	 */
20507c478bd9Sstevel@tonic-gate 	*opt_lenp = (t_scalar_t)(optr - init_optr);
20517c478bd9Sstevel@tonic-gate 	if (*opt_lenp > 0) {
20527c478bd9Sstevel@tonic-gate 		bcopy(init_optr, opt_start, *opt_lenp);
20537c478bd9Sstevel@tonic-gate 	}
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate error_ret:
20567c478bd9Sstevel@tonic-gate 	if (copy_mp_head != NULL)
20577c478bd9Sstevel@tonic-gate 		freeb(copy_mp_head);
20587c478bd9Sstevel@tonic-gate 	return (error);
20597c478bd9Sstevel@tonic-gate }
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate static opdes_t *
20627c478bd9Sstevel@tonic-gate opt_chk_lookup(t_uscalar_t level, t_uscalar_t name, opdes_t *opt_arr,
20637c478bd9Sstevel@tonic-gate     uint_t opt_arr_cnt)
20647c478bd9Sstevel@tonic-gate {
20657c478bd9Sstevel@tonic-gate 	opdes_t		*optd;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	for (optd = opt_arr; optd < &opt_arr[opt_arr_cnt];
20687c478bd9Sstevel@tonic-gate 	    optd++) {
20697c478bd9Sstevel@tonic-gate 		if (level == (uint_t)optd->opdes_level &&
20707c478bd9Sstevel@tonic-gate 		    name == (uint_t)optd->opdes_name)
20717c478bd9Sstevel@tonic-gate 			return (optd);
20727c478bd9Sstevel@tonic-gate 	}
20737c478bd9Sstevel@tonic-gate 	return (NULL);
20747c478bd9Sstevel@tonic-gate }
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate static boolean_t
20777c478bd9Sstevel@tonic-gate opt_level_valid(t_uscalar_t level, optlevel_t *valid_level_arr,
20787c478bd9Sstevel@tonic-gate     uint_t valid_level_arr_cnt)
20797c478bd9Sstevel@tonic-gate {
20807c478bd9Sstevel@tonic-gate 	optlevel_t		*olp;
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	for (olp = valid_level_arr;
20837c478bd9Sstevel@tonic-gate 	    olp < &valid_level_arr[valid_level_arr_cnt];
20847c478bd9Sstevel@tonic-gate 	    olp++) {
20857c478bd9Sstevel@tonic-gate 		if (level == (uint_t)(*olp))
20867c478bd9Sstevel@tonic-gate 			return (B_TRUE);
20877c478bd9Sstevel@tonic-gate 	}
20887c478bd9Sstevel@tonic-gate 	return (B_FALSE);
20897c478bd9Sstevel@tonic-gate }
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate /*
20937c478bd9Sstevel@tonic-gate  * Compute largest possible size for an option buffer containing
20947c478bd9Sstevel@tonic-gate  * all options in one buffer.
20957c478bd9Sstevel@tonic-gate  *
20967c478bd9Sstevel@tonic-gate  * XXX TBD, investigate use of opt_bloated_maxsize() to avoid
20977c478bd9Sstevel@tonic-gate  *     wastefully large buffer allocation.
20987c478bd9Sstevel@tonic-gate  */
20997c478bd9Sstevel@tonic-gate static size_t
21007c478bd9Sstevel@tonic-gate opt_level_allopts_lengths(t_uscalar_t level, opdes_t *opt_arr,
21017c478bd9Sstevel@tonic-gate     uint_t opt_arr_cnt)
21027c478bd9Sstevel@tonic-gate {
21037c478bd9Sstevel@tonic-gate 	opdes_t		*optd;
21047c478bd9Sstevel@tonic-gate 	size_t allopt_len = 0;	/* 0 implies no option at this level */
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	/*
21077c478bd9Sstevel@tonic-gate 	 * Scan opt_arr computing aggregate length
21087c478bd9Sstevel@tonic-gate 	 * requirement for storing values of all
21097c478bd9Sstevel@tonic-gate 	 * options.
21107c478bd9Sstevel@tonic-gate 	 * Note: we do not filter for permissions
21117c478bd9Sstevel@tonic-gate 	 * etc. This will be >= the real aggregate
21127c478bd9Sstevel@tonic-gate 	 * length required (upper bound).
21137c478bd9Sstevel@tonic-gate 	 */
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	for (optd = opt_arr; optd < &opt_arr[opt_arr_cnt];
21167c478bd9Sstevel@tonic-gate 	    optd++) {
21177c478bd9Sstevel@tonic-gate 		if (level == optd->opdes_level) {
21187c478bd9Sstevel@tonic-gate 			allopt_len += sizeof (struct T_opthdr) +
21197c478bd9Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(optd->opdes_size);
21207c478bd9Sstevel@tonic-gate 		}
21217c478bd9Sstevel@tonic-gate 	}
21227c478bd9Sstevel@tonic-gate 	return (allopt_len);	/* 0 implies level not found */
21237c478bd9Sstevel@tonic-gate }
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate /*
21267c478bd9Sstevel@tonic-gate  * Compute largest possible size for an option buffer containing
21277c478bd9Sstevel@tonic-gate  * all options in one buffer - a (theoretical?) worst case scenario
21287c478bd9Sstevel@tonic-gate  * for certain cases.
21297c478bd9Sstevel@tonic-gate  */
21307c478bd9Sstevel@tonic-gate t_uscalar_t
21317c478bd9Sstevel@tonic-gate optcom_max_optbuf_len(opdes_t *opt_arr, uint_t opt_arr_cnt)
21327c478bd9Sstevel@tonic-gate {
21337c478bd9Sstevel@tonic-gate 	t_uscalar_t max_optbuf_len = sizeof (struct T_info_ack);
21347c478bd9Sstevel@tonic-gate 	opdes_t		*optd;
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	for (optd = opt_arr; optd < &opt_arr[opt_arr_cnt]; optd++) {
21377c478bd9Sstevel@tonic-gate 		max_optbuf_len += (t_uscalar_t)sizeof (struct T_opthdr) +
21387c478bd9Sstevel@tonic-gate 		    (t_uscalar_t)_TPI_ALIGN_TOPT(optd->opdes_size);
21397c478bd9Sstevel@tonic-gate 	}
21407c478bd9Sstevel@tonic-gate 	return (max_optbuf_len);
21417c478bd9Sstevel@tonic-gate }
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate /*
21447c478bd9Sstevel@tonic-gate  * Compute largest possible size for OPT_size for a transport.
21457c478bd9Sstevel@tonic-gate  * Heuristic used is to add all but certain extremely large
21467c478bd9Sstevel@tonic-gate  * size options; this is done by calling opt_bloated_maxsize().
21477c478bd9Sstevel@tonic-gate  * It affects user level allocations in TLI/XTI code using t_alloc()
21487c478bd9Sstevel@tonic-gate  * and other TLI/XTI implementation instance strucutures.
21497c478bd9Sstevel@tonic-gate  * The large size options excluded are presumed to be
21507c478bd9Sstevel@tonic-gate  * never accessed through the (theoretical?) worst case code paths
21517c478bd9Sstevel@tonic-gate  * through TLI/XTI as they are currently IPv6 specific options.
21527c478bd9Sstevel@tonic-gate  */
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate t_uscalar_t
21557c478bd9Sstevel@tonic-gate optcom_max_optsize(opdes_t *opt_arr, uint_t opt_arr_cnt)
21567c478bd9Sstevel@tonic-gate {
21577c478bd9Sstevel@tonic-gate 	t_uscalar_t max_optbuf_len = sizeof (struct T_info_ack);
21587c478bd9Sstevel@tonic-gate 	opdes_t		*optd;
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	for (optd = opt_arr; optd < &opt_arr[opt_arr_cnt]; optd++) {
21617c478bd9Sstevel@tonic-gate 		if (!opt_bloated_maxsize(optd)) {
21627c478bd9Sstevel@tonic-gate 			max_optbuf_len +=
21637c478bd9Sstevel@tonic-gate 			    (t_uscalar_t)sizeof (struct T_opthdr) +
21647c478bd9Sstevel@tonic-gate 			    (t_uscalar_t)_TPI_ALIGN_TOPT(optd->opdes_size);
21657c478bd9Sstevel@tonic-gate 		}
21667c478bd9Sstevel@tonic-gate 	}
21677c478bd9Sstevel@tonic-gate 	return (max_optbuf_len);
21687c478bd9Sstevel@tonic-gate }
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate /*
21717c478bd9Sstevel@tonic-gate  * The theoretical model used in optcom_max_optsize() and
21727c478bd9Sstevel@tonic-gate  * opt_level_allopts_lengths() accounts for the worst case of all
21737c478bd9Sstevel@tonic-gate  * possible options for the theoretical cases and results in wasteful
21747c478bd9Sstevel@tonic-gate  * memory allocations for certain theoretically correct usage scenarios.
21757c478bd9Sstevel@tonic-gate  * In practice, the "features" they support are rarely, if ever,
21767c478bd9Sstevel@tonic-gate  * used and even then only by test suites for those features (VSU, VST).
21777c478bd9Sstevel@tonic-gate  * However, they result in large allocations due to the increased transport
21787c478bd9Sstevel@tonic-gate  * T_INFO_ACK OPT_size field affecting t_alloc() users and TLI/XTI library
21797c478bd9Sstevel@tonic-gate  * instance data structures for applications.
21807c478bd9Sstevel@tonic-gate  *
21817c478bd9Sstevel@tonic-gate  * The following routine opt_bloated_maxsize() supports a hack that avoids
21827c478bd9Sstevel@tonic-gate  * paying the tax for the bloated options by excluding them and pretending
21837c478bd9Sstevel@tonic-gate  * they don't exist for certain features without affecting features that
21847c478bd9Sstevel@tonic-gate  * do use them.
21857c478bd9Sstevel@tonic-gate  *
21867c478bd9Sstevel@tonic-gate  * XXX Currently implemented only for optcom_max_optsize()
21877c478bd9Sstevel@tonic-gate  *     (to reduce risk late in release).
21887c478bd9Sstevel@tonic-gate  *     TBD for future, investigate use in optcom_level_allopts_lengths() and
21897c478bd9Sstevel@tonic-gate  *     all the instances of T_ALLOPT processing to exclude "bloated options".
21907c478bd9Sstevel@tonic-gate  *     Will not affect VSU/VST tests as they do not test with IPPROTO_IPV6
21917c478bd9Sstevel@tonic-gate  *     level options which are the only ones that fit the "bloated maxsize"
21927c478bd9Sstevel@tonic-gate  *     option profile now.
21937c478bd9Sstevel@tonic-gate  */
21947c478bd9Sstevel@tonic-gate static boolean_t
21957c478bd9Sstevel@tonic-gate opt_bloated_maxsize(opdes_t *optd)
21967c478bd9Sstevel@tonic-gate {
21977c478bd9Sstevel@tonic-gate 	if (optd->opdes_level != IPPROTO_IPV6)
21987c478bd9Sstevel@tonic-gate 		return (B_FALSE);
21997c478bd9Sstevel@tonic-gate 	switch (optd->opdes_name) {
22007c478bd9Sstevel@tonic-gate 	case IPV6_HOPOPTS:
22017c478bd9Sstevel@tonic-gate 	case IPV6_DSTOPTS:
22027c478bd9Sstevel@tonic-gate 	case IPV6_RTHDRDSTOPTS:
22037c478bd9Sstevel@tonic-gate 	case IPV6_RTHDR:
22047c478bd9Sstevel@tonic-gate 	case IPV6_PATHMTU:
22057c478bd9Sstevel@tonic-gate 		return (B_TRUE);
22067c478bd9Sstevel@tonic-gate 	default:
22077c478bd9Sstevel@tonic-gate 		break;
22087c478bd9Sstevel@tonic-gate 	}
22097c478bd9Sstevel@tonic-gate 	return (B_FALSE);
22107c478bd9Sstevel@tonic-gate }
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate static boolean_t
22137c478bd9Sstevel@tonic-gate opt_length_ok(opdes_t *optd, struct T_opthdr *opt)
22147c478bd9Sstevel@tonic-gate {
22157c478bd9Sstevel@tonic-gate 	/*
22167c478bd9Sstevel@tonic-gate 	 * Verify length.
22177c478bd9Sstevel@tonic-gate 	 * Value specified should match length of fixed length option or be
22187c478bd9Sstevel@tonic-gate 	 * less than maxlen of variable length option.
22197c478bd9Sstevel@tonic-gate 	 */
22207c478bd9Sstevel@tonic-gate 	if (optd->opdes_props & OP_VARLEN) {
22217c478bd9Sstevel@tonic-gate 		if (opt->len <= optd->opdes_size +
22227c478bd9Sstevel@tonic-gate 		    (t_uscalar_t)sizeof (struct T_opthdr))
22237c478bd9Sstevel@tonic-gate 			return (B_TRUE);
22247c478bd9Sstevel@tonic-gate 	} else {
22257c478bd9Sstevel@tonic-gate 		/* fixed length option */
22267c478bd9Sstevel@tonic-gate 		if (opt->len == optd->opdes_size +
22277c478bd9Sstevel@tonic-gate 		    (t_uscalar_t)sizeof (struct T_opthdr))
22287c478bd9Sstevel@tonic-gate 			return (B_TRUE);
22297c478bd9Sstevel@tonic-gate 	}
22307c478bd9Sstevel@tonic-gate 	return (B_FALSE);
22317c478bd9Sstevel@tonic-gate }
2232