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