Home | History | Annotate | Download | only in ip
      1      0    stevel /*
      2      0    stevel  * CDDL HEADER START
      3      0    stevel  *
      4      0    stevel  * The contents of this file are subject to the terms of the
      5   3388    kcpoon  * Common Development and Distribution License (the "License").
      6   3388    kcpoon  * You may not use this file except in compliance with the License.
      7      0    stevel  *
      8      0    stevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9      0    stevel  * or http://www.opensolaris.org/os/licensing.
     10      0    stevel  * See the License for the specific language governing permissions
     11      0    stevel  * and limitations under the License.
     12      0    stevel  *
     13      0    stevel  * When distributing Covered Code, include this CDDL HEADER in each
     14      0    stevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15      0    stevel  * If applicable, add the following below this CDDL HEADER, with the
     16      0    stevel  * fields enclosed by brackets "[]" replaced with your own identifying
     17      0    stevel  * information: Portions Copyright [yyyy] [name of copyright owner]
     18      0    stevel  *
     19      0    stevel  * CDDL HEADER END
     20      0    stevel  */
     21      0    stevel /*
     22   8477       Rao  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     23      0    stevel  * Use is subject to license terms.
     24      0    stevel  */
     25      0    stevel 
     26      0    stevel #include <sys/types.h>
     27      0    stevel #include <sys/stream.h>
     28      0    stevel #include <sys/strsubr.h>
     29      0    stevel #include <sys/stropts.h>
     30      0    stevel #include <sys/strsun.h>
     31      0    stevel #include <sys/strlog.h>
     32      0    stevel #define	_SUN_TPI_VERSION 2
     33      0    stevel #include <sys/tihdr.h>
     34      0    stevel #include <sys/timod.h>
     35      0    stevel #include <sys/ddi.h>
     36      0    stevel #include <sys/sunddi.h>
     37      0    stevel #include <sys/cmn_err.h>
     38      0    stevel #include <sys/proc.h>
     39      0    stevel #include <sys/suntpi.h>
     40      0    stevel #include <sys/policy.h>
     41   3448  dh155122 #include <sys/zone.h>
     42   8348      Eric #include <sys/disp.h>
     43      0    stevel 
     44      0    stevel #include <sys/socket.h>
     45   8348      Eric #include <sys/socketvar.h>
     46      0    stevel #include <netinet/in.h>
     47      0    stevel 
     48      0    stevel #include <inet/common.h>
     49      0    stevel #include <netinet/ip6.h>
     50      0    stevel #include <inet/ip.h>
     51   5240  nordmark #include <inet/ipclassifier.h>
     52   8348      Eric #include <inet/proto_set.h>
     53      0    stevel #include <inet/nd.h>
     54      0    stevel #include <inet/optcom.h>
     55      0    stevel #include <netinet/ip_mroute.h>
     56      0    stevel #include <sys/isa_defs.h>
     57      0    stevel #include <net/route.h>
     58   5240  nordmark 
     59   5240  nordmark #include <inet/rts_impl.h>
     60   5240  nordmark #include <inet/ip_rts.h>
     61      0    stevel 
     62      0    stevel /*
     63      0    stevel  * This is a transport provider for routing sockets.  Downstream messages are
     64      0    stevel  * wrapped with a IP_IOCTL header, and ip_wput_ioctl calls the appropriate entry
     65      0    stevel  * in the ip_ioctl_ftbl callout table to pass the routing socket data into IP.
     66      0    stevel  * Upstream messages are generated for listeners of the routing socket as well
     67      0    stevel  * as the message sender (unless they have turned off their end using
     68      0    stevel  * SO_USELOOPBACK or shutdown(3n)).  Upstream messages may also be generated
     69      0    stevel  * asynchronously when:
     70      0    stevel  *
     71      0    stevel  *	Interfaces are brought up or down.
     72      0    stevel  *	Addresses are assigned to interfaces.
     73   4365  nordmark  *	ICMP redirects are processed and a IRE_HOST/RTF_DYNAMIC is installed.
     74      0    stevel  *	No route is found while sending a packet.
     75      0    stevel  *
     76      0    stevel  * Since all we do is reformat the messages between routing socket and
     77      0    stevel  * ioctl forms, no synchronization is necessary in this module; all
     78      0    stevel  * the dirty work is done down in ip.
     79      0    stevel  */
     80      0    stevel 
     81      0    stevel /* Default structure copied into T_INFO_ACK messages */
     82      0    stevel static struct T_info_ack rts_g_t_info_ack = {
     83      0    stevel 	T_INFO_ACK,
     84      0    stevel 	T_INFINITE,	/* TSDU_size. Maximum size messages. */
     85      0    stevel 	T_INVALID,	/* ETSDU_size. No expedited data. */
     86      0    stevel 	T_INVALID,	/* CDATA_size. No connect data. */
     87      0    stevel 	T_INVALID,	/* DDATA_size. No disconnect data. */
     88      0    stevel 	0,		/* ADDR_size. */
     89      0    stevel 	0,		/* OPT_size - not initialized here */
     90      0    stevel 	64 * 1024,	/* TIDU_size. rts allows maximum size messages. */
     91      0    stevel 	T_COTS,		/* SERV_type. rts supports connection oriented. */
     92      0    stevel 	TS_UNBND,	/* CURRENT_state. This is set from rts_state. */
     93      0    stevel 	(XPG4_1)	/* PROVIDER_flag */
     94      0    stevel };
     95      0    stevel 
     96      0    stevel /*
     97      0    stevel  * Table of ND variables supported by rts. These are loaded into rts_g_nd
     98      0    stevel  * in rts_open.
     99      0    stevel  * All of these are alterable, within the min/max values given, at run time.
    100      0    stevel  */
    101   3448  dh155122 static rtsparam_t	lcl_param_arr[] = {
    102      0    stevel 	/* min		max		value		name */
    103      0    stevel 	{ 4096,		65536,		8192,		"rts_xmit_hiwat"},
    104      0    stevel 	{ 0,		65536,		1024,		"rts_xmit_lowat"},
    105      0    stevel 	{ 4096,		65536,		8192,		"rts_recv_hiwat"},
    106      0    stevel 	{ 65536,	1024*1024*1024, 256*1024,	"rts_max_buf"},
    107      0    stevel };
    108   3448  dh155122 #define	rtss_xmit_hiwat		rtss_params[0].rts_param_value
    109   3448  dh155122 #define	rtss_xmit_lowat		rtss_params[1].rts_param_value
    110   3448  dh155122 #define	rtss_recv_hiwat		rtss_params[2].rts_param_value
    111   5240  nordmark #define	rtss_max_buf		rtss_params[3].rts_param_value
    112      0    stevel 
    113      0    stevel static void 	rts_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
    114      0    stevel     int sys_error);
    115  11042      Erik static void	rts_input(void *, mblk_t *, void *, ip_recv_attr_t *);
    116  11042      Erik static void	rts_icmp_input(void *, mblk_t *, void *, ip_recv_attr_t *);
    117   8778      Erik static mblk_t	*rts_ioctl_alloc(mblk_t *data);
    118      0    stevel static int	rts_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr);
    119   3448  dh155122 static boolean_t rts_param_register(IDP *ndp, rtsparam_t *rtspa, int cnt);
    120      0    stevel static int	rts_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp,
    121      0    stevel     cred_t *cr);
    122   5240  nordmark static void	rts_rsrv(queue_t *q);
    123   3448  dh155122 static void	*rts_stack_init(netstackid_t stackid, netstack_t *ns);
    124   3448  dh155122 static void	rts_stack_fini(netstackid_t stackid, void *arg);
    125      0    stevel static void	rts_wput(queue_t *q, mblk_t *mp);
    126      0    stevel static void	rts_wput_iocdata(queue_t *q, mblk_t *mp);
    127      0    stevel static void 	rts_wput_other(queue_t *q, mblk_t *mp);
    128      0    stevel static int	rts_wrw(queue_t *q, struiod_t *dp);
    129      0    stevel 
    130   8348      Eric static int	rts_stream_open(queue_t *q, dev_t *devp, int flag, int sflag,
    131   8348      Eric 		    cred_t *credp);
    132   8348      Eric static conn_t	*rts_open(int flag, cred_t *credp);
    133   8348      Eric 
    134   8348      Eric static int	rts_stream_close(queue_t *q);
    135   8348      Eric static int	rts_close(sock_lower_handle_t proto_handle, int flags,
    136   8348      Eric 		    cred_t *cr);
    137   8348      Eric 
    138   5240  nordmark static struct module_info rts_mod_info = {
    139      0    stevel 	129, "rts", 1, INFPSZ, 512, 128
    140      0    stevel };
    141      0    stevel 
    142   5240  nordmark static struct qinit rtsrinit = {
    143   8348      Eric 	NULL, (pfi_t)rts_rsrv, rts_stream_open, rts_stream_close, NULL,
    144   8348      Eric 	&rts_mod_info
    145      0    stevel };
    146      0    stevel 
    147   5240  nordmark static struct qinit rtswinit = {
    148   5240  nordmark 	(pfi_t)rts_wput, NULL, NULL, NULL, NULL, &rts_mod_info,
    149      0    stevel 	NULL, (pfi_t)rts_wrw, NULL, STRUIOT_STANDARD
    150      0    stevel };
    151      0    stevel 
    152      0    stevel struct streamtab rtsinfo = {
    153   5240  nordmark 	&rtsrinit, &rtswinit
    154      0    stevel };
    155      0    stevel 
    156      0    stevel /*
    157      0    stevel  * This routine allocates the necessary
    158      0    stevel  * message blocks for IOCTL wrapping the
    159      0    stevel  * user data.
    160      0    stevel  */
    161      0    stevel static mblk_t *
    162   8778      Erik rts_ioctl_alloc(mblk_t *data)
    163      0    stevel {
    164      0    stevel 	mblk_t	*mp = NULL;
    165      0    stevel 	mblk_t	*mp1 = NULL;
    166      0    stevel 	ipllc_t	*ipllc;
    167      0    stevel 	struct iocblk	*ioc;
    168      0    stevel 
    169   8778      Erik 	mp = allocb_tmpl(sizeof (ipllc_t), data);
    170      0    stevel 	if (mp == NULL)
    171      0    stevel 		return (NULL);
    172   8778      Erik 	mp1 = allocb_tmpl(sizeof (struct iocblk), data);
    173      0    stevel 	if (mp1 == NULL) {
    174      0    stevel 		freeb(mp);
    175      0    stevel 		return (NULL);
    176      0    stevel 	}
    177      0    stevel 
    178      0    stevel 	ipllc = (ipllc_t *)mp->b_rptr;
    179      0    stevel 	ipllc->ipllc_cmd = IP_IOC_RTS_REQUEST;
    180      0    stevel 	ipllc->ipllc_name_offset = 0;
    181      0    stevel 	ipllc->ipllc_name_length = 0;
    182      0    stevel 	mp->b_wptr += sizeof (ipllc_t);
    183      0    stevel 	mp->b_cont = data;
    184      0    stevel 
    185      0    stevel 	ioc = (struct iocblk *)mp1->b_rptr;
    186      0    stevel 	ioc->ioc_cmd = IP_IOCTL;
    187      0    stevel 	ioc->ioc_error = 0;
    188      0    stevel 	ioc->ioc_cr = NULL;
    189      0    stevel 	ioc->ioc_count = msgdsize(mp);
    190      0    stevel 	mp1->b_wptr += sizeof (struct iocblk);
    191      0    stevel 	mp1->b_datap->db_type = M_IOCTL;
    192      0    stevel 	mp1->b_cont = mp;
    193      0    stevel 
    194      0    stevel 	return (mp1);
    195      0    stevel }
    196      0    stevel 
    197      0    stevel /*
    198      0    stevel  * This routine closes rts stream, by disabling
    199      0    stevel  * put/srv routines and freeing the this module
    200      0    stevel  * internal datastructure.
    201      0    stevel  */
    202      0    stevel static int
    203   8348      Eric rts_common_close(queue_t *q, conn_t *connp)
    204      0    stevel {
    205   5240  nordmark 
    206   5240  nordmark 	ASSERT(connp != NULL && IPCL_IS_RTS(connp));
    207   5240  nordmark 
    208   5240  nordmark 	ip_rts_unregister(connp);
    209   5240  nordmark 
    210   5240  nordmark 	ip_quiesce_conn(connp);
    211   3448  dh155122 
    212   8348      Eric 	if (!IPCL_IS_NONSTR(connp)) {
    213   8348      Eric 		qprocsoff(q);
    214  11042      Erik 	}
    215      0    stevel 
    216  11042      Erik 	/*
    217  11042      Erik 	 * Now we are truly single threaded on this stream, and can
    218  11042      Erik 	 * delete the things hanging off the connp, and finally the connp.
    219  11042      Erik 	 * We removed this connp from the fanout list, it cannot be
    220  11042      Erik 	 * accessed thru the fanouts, and we already waited for the
    221  11042      Erik 	 * conn_ref to drop to 0. We are already in close, so
    222  11042      Erik 	 * there cannot be any other thread from the top. qprocsoff
    223  11042      Erik 	 * has completed, and service has completed or won't run in
    224  11042      Erik 	 * future.
    225  11042      Erik 	 */
    226  11042      Erik 	ASSERT(connp->conn_ref == 1);
    227  11042      Erik 
    228  11042      Erik 	if (!IPCL_IS_NONSTR(connp)) {
    229   8348      Eric 		inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
    230   8348      Eric 	} else {
    231   8477       Rao 		ip_free_helper_stream(connp);
    232   8348      Eric 	}
    233   5240  nordmark 
    234   5240  nordmark 	connp->conn_ref--;
    235   5240  nordmark 	ipcl_conn_destroy(connp);
    236   8348      Eric 	return (0);
    237   8348      Eric }
    238   8348      Eric 
    239   8348      Eric static int
    240   8348      Eric rts_stream_close(queue_t *q)
    241   8348      Eric {
    242   8348      Eric 	conn_t  *connp = Q_TO_CONN(q);
    243   8348      Eric 
    244   8348      Eric 	(void) rts_common_close(q, connp);
    245   5240  nordmark 	q->q_ptr = WR(q)->q_ptr = NULL;
    246      0    stevel 	return (0);
    247      0    stevel }
    248      0    stevel 
    249      0    stevel /*
    250      0    stevel  * This is the open routine for routing socket. It allocates
    251   5240  nordmark  * rts_t structure for the stream and tells IP that it is a routing socket.
    252      0    stevel  */
    253      0    stevel /* ARGSUSED */
    254      0    stevel static int
    255   8348      Eric rts_stream_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
    256      0    stevel {
    257   5240  nordmark 	conn_t *connp;
    258   5240  nordmark 	dev_t	conn_dev;
    259   8348      Eric 	rts_t   *rts;
    260      0    stevel 
    261      0    stevel 	/* If the stream is already open, return immediately. */
    262      0    stevel 	if (q->q_ptr != NULL)
    263      0    stevel 		return (0);
    264      0    stevel 
    265   5240  nordmark 	if (sflag == MODOPEN)
    266      0    stevel 		return (EINVAL);
    267   8348      Eric 
    268   8348      Eric 	/*
    269   8348      Eric 	 * Since RTS is not used so heavily, allocating from the small
    270   8348      Eric 	 * arena should be sufficient.
    271   8348      Eric 	 */
    272   8348      Eric 	if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) {
    273   8348      Eric 		return (EBUSY);
    274   8348      Eric 	}
    275   8348      Eric 
    276   8348      Eric 	connp = rts_open(flag, credp);
    277   8348      Eric 	ASSERT(connp != NULL);
    278   8348      Eric 
    279   8348      Eric 	*devp = makedevice(getemajor(*devp), (minor_t)conn_dev);
    280   8348      Eric 
    281   8348      Eric 	rts = connp->conn_rts;
    282   8348      Eric 	rw_enter(&rts->rts_rwlock, RW_WRITER);
    283   8348      Eric 	connp->conn_dev = conn_dev;
    284   8348      Eric 	connp->conn_minor_arena = ip_minor_arena_sa;
    285   8348      Eric 
    286   8348      Eric 	q->q_ptr = connp;
    287   8348      Eric 	WR(q)->q_ptr = connp;
    288   8348      Eric 	connp->conn_rq = q;
    289   8348      Eric 	connp->conn_wq = WR(q);
    290   8348      Eric 
    291  11042      Erik 	WR(q)->q_hiwat = connp->conn_sndbuf;
    292  11042      Erik 	WR(q)->q_lowat = connp->conn_sndlowat;
    293   8348      Eric 
    294   8348      Eric 	mutex_enter(&connp->conn_lock);
    295   8348      Eric 	connp->conn_state_flags &= ~CONN_INCIPIENT;
    296   8348      Eric 	mutex_exit(&connp->conn_lock);
    297  11042      Erik 	rw_exit(&rts->rts_rwlock);
    298  11042      Erik 
    299  11042      Erik 	/* Indicate to IP that this is a routing socket client */
    300  11042      Erik 	ip_rts_register(connp);
    301   8348      Eric 
    302   8348      Eric 	qprocson(q);
    303   8348      Eric 
    304   8348      Eric 	return (0);
    305   8348      Eric }
    306   8348      Eric 
    307   8348      Eric /* ARGSUSED */
    308   8348      Eric static conn_t *
    309   8348      Eric rts_open(int flag, cred_t *credp)
    310   8348      Eric {
    311   8348      Eric 	netstack_t *ns;
    312   8348      Eric 	rts_stack_t *rtss;
    313   8348      Eric 	rts_t	*rts;
    314   8348      Eric 	conn_t	*connp;
    315   8348      Eric 	zoneid_t zoneid;
    316      0    stevel 
    317   3448  dh155122 	ns = netstack_find_by_cred(credp);
    318   3448  dh155122 	ASSERT(ns != NULL);
    319   3448  dh155122 	rtss = ns->netstack_rts;
    320   3448  dh155122 	ASSERT(rtss != NULL);
    321   3448  dh155122 
    322   5240  nordmark 	/*
    323   5240  nordmark 	 * For exclusive stacks we set the zoneid to zero
    324   5240  nordmark 	 * to make RTS operate as if in the global zone.
    325   5240  nordmark 	 */
    326   5240  nordmark 	if (ns->netstack_stackid != GLOBAL_NETSTACKID)
    327   5240  nordmark 		zoneid = GLOBAL_ZONEID;
    328   5240  nordmark 	else
    329   5240  nordmark 		zoneid = crgetzoneid(credp);
    330   5240  nordmark 
    331   5240  nordmark 	connp = ipcl_conn_create(IPCL_RTSCONN, KM_SLEEP, ns);
    332   5240  nordmark 	rts = connp->conn_rts;
    333   5240  nordmark 
    334   5240  nordmark 	/*
    335   5240  nordmark 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
    336   5240  nordmark 	 * done by netstack_find_by_cred()
    337   5240  nordmark 	 */
    338   5240  nordmark 	netstack_rele(ns);
    339   5240  nordmark 
    340   5240  nordmark 	rw_enter(&rts->rts_rwlock, RW_WRITER);
    341   5240  nordmark 	ASSERT(connp->conn_rts == rts);
    342   5240  nordmark 	ASSERT(rts->rts_connp == connp);
    343   5240  nordmark 
    344  11042      Erik 	connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
    345  11042      Erik 	/* conn_allzones can not be set this early, hence no IPCL_ZONEID */
    346  11042      Erik 	connp->conn_ixa->ixa_zoneid = zoneid;
    347   5240  nordmark 	connp->conn_zoneid = zoneid;
    348   8348      Eric 	connp->conn_flow_cntrld = B_FALSE;
    349   5240  nordmark 
    350  11042      Erik 	rts->rts_rtss = rtss;
    351   3448  dh155122 
    352  11042      Erik 	connp->conn_rcvbuf = rtss->rtss_recv_hiwat;
    353  11042      Erik 	connp->conn_sndbuf = rtss->rtss_xmit_hiwat;
    354  11042      Erik 	connp->conn_sndlowat = rtss->rtss_xmit_lowat;
    355  11042      Erik 	connp->conn_rcvlowat = rts_mod_info.mi_lowat;
    356  11042      Erik 
    357  11042      Erik 	connp->conn_family = PF_ROUTE;
    358  11042      Erik 	connp->conn_so_type = SOCK_RAW;
    359  11042      Erik 	/* SO_PROTOTYPE is always sent down by sockfs setting conn_proto */
    360   5240  nordmark 
    361   5240  nordmark 	connp->conn_recv = rts_input;
    362  11042      Erik 	connp->conn_recvicmp = rts_icmp_input;
    363  11042      Erik 
    364   5240  nordmark 	crhold(credp);
    365   5240  nordmark 	connp->conn_cred = credp;
    366  11042      Erik 	connp->conn_cpid = curproc->p_pid;
    367  11042      Erik 	/* Cache things in ixa without an extra refhold */
    368  11042      Erik 	connp->conn_ixa->ixa_cred = connp->conn_cred;
    369  11042      Erik 	connp->conn_ixa->ixa_cpid = connp->conn_cpid;
    370  11042      Erik 	if (is_system_labeled())
    371  11042      Erik 		connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
    372   5240  nordmark 
    373   8348      Eric 	/*
    374   8348      Eric 	 * rts sockets start out as bound and connected
    375   8348      Eric 	 * For streams based sockets, socket state is set to
    376   8348      Eric 	 * SS_ISBOUND | SS_ISCONNECTED in so_strinit.
    377   8348      Eric 	 */
    378   8348      Eric 	rts->rts_state = TS_DATA_XFER;
    379   5240  nordmark 	rw_exit(&rts->rts_rwlock);
    380   5240  nordmark 
    381   8348      Eric 	return (connp);
    382      0    stevel }
    383      0    stevel 
    384      0    stevel /*
    385      0    stevel  * This routine creates a T_ERROR_ACK message and passes it upstream.
    386      0    stevel  */
    387      0    stevel static void
    388      0    stevel rts_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
    389      0    stevel {
    390      0    stevel 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
    391      0    stevel 		qreply(q, mp);
    392      0    stevel }
    393      0    stevel 
    394      0    stevel /*
    395      0    stevel  * This routine creates a T_OK_ACK message and passes it upstream.
    396      0    stevel  */
    397      0    stevel static void
    398      0    stevel rts_ok_ack(queue_t *q, mblk_t *mp)
    399      0    stevel {
    400      0    stevel 	if ((mp = mi_tpi_ok_ack_alloc(mp)) != NULL)
    401      0    stevel 		qreply(q, mp);
    402      0    stevel }
    403      0    stevel 
    404      0    stevel /*
    405      0    stevel  * This routine is called by rts_wput to handle T_UNBIND_REQ messages.
    406      0    stevel  */
    407      0    stevel static void
    408   8348      Eric rts_tpi_unbind(queue_t *q, mblk_t *mp)
    409      0    stevel {
    410   5240  nordmark 	conn_t	*connp = Q_TO_CONN(q);
    411   5240  nordmark 	rts_t	*rts = connp->conn_rts;
    412      0    stevel 
    413      0    stevel 	/* If a bind has not been done, we can't unbind. */
    414      0    stevel 	if (rts->rts_state != TS_IDLE) {
    415      0    stevel 		rts_err_ack(q, mp, TOUTSTATE, 0);
    416      0    stevel 		return;
    417      0    stevel 	}
    418      0    stevel 	rts->rts_state = TS_UNBND;
    419      0    stevel 	rts_ok_ack(q, mp);
    420      0    stevel }
    421      0    stevel 
    422      0    stevel /*
    423      0    stevel  * This routine is called to handle each
    424      0    stevel  * O_T_BIND_REQ/T_BIND_REQ message passed to
    425      0    stevel  * rts_wput. Note: This routine works with both
    426      0    stevel  * O_T_BIND_REQ and T_BIND_REQ semantics.
    427      0    stevel  */
    428      0    stevel static void
    429   8348      Eric rts_tpi_bind(queue_t *q, mblk_t *mp)
    430      0    stevel {
    431   5240  nordmark 	conn_t	*connp = Q_TO_CONN(q);
    432   5240  nordmark 	rts_t	*rts = connp->conn_rts;
    433      0    stevel 	struct T_bind_req *tbr;
    434      0    stevel 
    435      0    stevel 	if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
    436      0    stevel 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
    437   8348      Eric 		    "rts_tpi_bind: bad data, %d", rts->rts_state);
    438      0    stevel 		rts_err_ack(q, mp, TBADADDR, 0);
    439      0    stevel 		return;
    440      0    stevel 	}
    441      0    stevel 	if (rts->rts_state != TS_UNBND) {
    442      0    stevel 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
    443   8348      Eric 		    "rts_tpi_bind: bad state, %d", rts->rts_state);
    444      0    stevel 		rts_err_ack(q, mp, TOUTSTATE, 0);
    445      0    stevel 		return;
    446      0    stevel 	}
    447      0    stevel 	tbr = (struct T_bind_req *)mp->b_rptr;
    448      0    stevel 	if (tbr->ADDR_length != 0) {
    449      0    stevel 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
    450   8348      Eric 		    "rts_tpi_bind: bad ADDR_length %d", tbr->ADDR_length);
    451      0    stevel 		rts_err_ack(q, mp, TBADADDR, 0);
    452      0    stevel 		return;
    453      0    stevel 	}
    454      0    stevel 	/* Generic request */
    455      0    stevel 	tbr->ADDR_offset = (t_scalar_t)sizeof (struct T_bind_req);
    456      0    stevel 	tbr->ADDR_length = 0;
    457      0    stevel 	tbr->PRIM_type = T_BIND_ACK;
    458  11042      Erik 	mp->b_datap->db_type = M_PCPROTO;
    459      0    stevel 	rts->rts_state = TS_IDLE;
    460      0    stevel 	qreply(q, mp);
    461      0    stevel }
    462      0    stevel 
    463      0    stevel static void
    464      0    stevel rts_copy_info(struct T_info_ack *tap, rts_t *rts)
    465      0    stevel {
    466      0    stevel 	*tap = rts_g_t_info_ack;
    467      0    stevel 	tap->CURRENT_state = rts->rts_state;
    468      0    stevel 	tap->OPT_size = rts_max_optsize;
    469      0    stevel }
    470      0    stevel 
    471      0    stevel /*
    472      0    stevel  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
    473      0    stevel  * rts_wput.  Much of the T_CAPABILITY_ACK information is copied from
    474      0    stevel  * rts_g_t_info_ack.  The current state of the stream is copied from
    475      0    stevel  * rts_state.
    476      0    stevel  */
    477      0    stevel static void
    478      0    stevel rts_capability_req(queue_t *q, mblk_t *mp)
    479      0    stevel {
    480   5240  nordmark 	conn_t	*connp = Q_TO_CONN(q);
    481   5240  nordmark 	rts_t	*rts = connp->conn_rts;
    482      0    stevel 	t_uscalar_t		cap_bits1;
    483      0    stevel 	struct T_capability_ack	*tcap;
    484      0    stevel 
    485      0    stevel 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
    486      0    stevel 
    487      0    stevel 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
    488   5240  nordmark 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
    489      0    stevel 	if (mp == NULL)
    490      0    stevel 		return;
    491      0    stevel 
    492      0    stevel 	tcap = (struct T_capability_ack *)mp->b_rptr;
    493      0    stevel 	tcap->CAP_bits1 = 0;
    494      0    stevel 
    495      0    stevel 	if (cap_bits1 & TC1_INFO) {
    496      0    stevel 		rts_copy_info(&tcap->INFO_ack, rts);
    497      0    stevel 		tcap->CAP_bits1 |= TC1_INFO;
    498      0    stevel 	}
    499      0    stevel 
    500      0    stevel 	qreply(q, mp);
    501      0    stevel }
    502      0    stevel 
    503      0    stevel /*
    504      0    stevel  * This routine responds to T_INFO_REQ messages.  It is called by rts_wput.
    505      0    stevel  * Most of the T_INFO_ACK information is copied from rts_g_t_info_ack.
    506      0    stevel  * The current state of the stream is copied from rts_state.
    507      0    stevel  */
    508      0    stevel static void
    509      0    stevel rts_info_req(queue_t *q, mblk_t *mp)
    510      0    stevel {
    511   5240  nordmark 	conn_t	*connp = Q_TO_CONN(q);
    512   5240  nordmark 	rts_t	*rts = connp->conn_rts;
    513      0    stevel 
    514      0    stevel 	mp = tpi_ack_alloc(mp, sizeof (rts_g_t_info_ack), M_PCPROTO,
    515      0    stevel 	    T_INFO_ACK);
    516      0    stevel 	if (mp == NULL)
    517      0    stevel 		return;
    518      0    stevel 	rts_copy_info((struct T_info_ack *)mp->b_rptr, rts);
    519      0    stevel 	qreply(q, mp);
    520      0    stevel }
    521      0    stevel 
    522      0    stevel /*
    523      0    stevel  * This routine gets default values of certain options whose default
    524      0    stevel  * values are maintained by protcol specific code
    525      0    stevel  */
    526      0    stevel /* ARGSUSED */
    527      0    stevel int
    528      0    stevel rts_opt_default(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
    529      0    stevel {
    530      0    stevel 	/* no default value processed by protocol specific code currently */
    531      0    stevel 	return (-1);
    532      0    stevel }
    533      0    stevel 
    534   8348      Eric 
    535   8348      Eric static int
    536   8348      Eric rts_opt_get(conn_t *connp, int level, int name, uchar_t *ptr)
    537      0    stevel {
    538   8348      Eric 	rts_t	*rts = connp->conn_rts;
    539  11042      Erik 	conn_opt_arg_t	coas;
    540  11042      Erik 	int retval;
    541   8348      Eric 
    542   8348      Eric 	ASSERT(RW_READ_HELD(&rts->rts_rwlock));
    543      0    stevel 
    544      0    stevel 	switch (level) {
    545  11042      Erik 	/* do this in conn_opt_get? */
    546   8485     Peter 	case SOL_ROUTE:
    547   8485     Peter 		switch (name) {
    548   8485     Peter 		case RT_AWARE:
    549   8485     Peter 			mutex_enter(&connp->conn_lock);
    550  11042      Erik 			*(int *)ptr = connp->conn_rtaware;
    551   8485     Peter 			mutex_exit(&connp->conn_lock);
    552  11042      Erik 			return (0);
    553      0    stevel 		}
    554      0    stevel 		break;
    555      0    stevel 	}
    556  11042      Erik 	coas.coa_connp = connp;
    557  11042      Erik 	coas.coa_ixa = connp->conn_ixa;
    558  11042      Erik 	coas.coa_ipp = &connp->conn_xmit_ipp;
    559  11042      Erik 	mutex_enter(&connp->conn_lock);
    560  11042      Erik 	retval = conn_opt_get(&coas, level, name, ptr);
    561  11042      Erik 	mutex_exit(&connp->conn_lock);
    562  11042      Erik 	return (retval);
    563      0    stevel }
    564      0    stevel 
    565   8348      Eric /* ARGSUSED */
    566   8348      Eric static int
    567   8348      Eric rts_do_opt_set(conn_t *connp, int level, int name, uint_t inlen,
    568   8348      Eric     uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, cred_t *cr,
    569   8348      Eric     void *thisdg_attrs, boolean_t checkonly)
    570      0    stevel {
    571      0    stevel 	int	*i1 = (int *)invalp;
    572   5240  nordmark 	rts_t	*rts = connp->conn_rts;
    573   3448  dh155122 	rts_stack_t	*rtss = rts->rts_rtss;
    574  11042      Erik 	int		error;
    575  11042      Erik 	conn_opt_arg_t	coas;
    576  11042      Erik 
    577  11042      Erik 	coas.coa_connp = connp;
    578  11042      Erik 	coas.coa_ixa = connp->conn_ixa;
    579  11042      Erik 	coas.coa_ipp = &connp->conn_xmit_ipp;
    580      0    stevel 
    581   8348      Eric 	ASSERT(RW_WRITE_HELD(&rts->rts_rwlock));
    582      0    stevel 
    583      0    stevel 	/*
    584      0    stevel 	 * For rts, we should have no ancillary data sent down
    585      0    stevel 	 * (rts_wput doesn't handle options).
    586      0    stevel 	 */
    587      0    stevel 	ASSERT(thisdg_attrs == NULL);
    588      0    stevel 
    589      0    stevel 	/*
    590      0    stevel 	 * For fixed length options, no sanity check
    591      0    stevel 	 * of passed in length is done. It is assumed *_optcom_req()
    592      0    stevel 	 * routines do the right thing.
    593      0    stevel 	 */
    594      0    stevel 
    595      0    stevel 	switch (level) {
    596      0    stevel 	case SOL_SOCKET:
    597      0    stevel 		switch (name) {
    598      0    stevel 		case SO_PROTOTYPE:
    599      0    stevel 			/*
    600      0    stevel 			 * Routing socket applications that call socket() with
    601      0    stevel 			 * a third argument can filter which messages will be
    602      0    stevel 			 * sent upstream thanks to sockfs.  so_socket() sends
    603      0    stevel 			 * down the SO_PROTOTYPE and rts_queue_input()
    604      0    stevel 			 * implements the filtering.
    605      0    stevel 			 */
    606  11042      Erik 			if (*i1 != AF_INET && *i1 != AF_INET6) {
    607  11042      Erik 				*outlenp = 0;
    608      0    stevel 				return (EPROTONOSUPPORT);
    609  11042      Erik 			}
    610  11042      Erik 			if (!checkonly)
    611   8552       Rao 				connp->conn_proto = *i1;
    612  11042      Erik 			*outlenp = inlen;
    613  11042      Erik 			return (0);
    614  11042      Erik 
    615      0    stevel 		/*
    616      0    stevel 		 * The following two items can be manipulated,
    617      0    stevel 		 * but changing them should do nothing.
    618      0    stevel 		 */
    619      0    stevel 		case SO_SNDBUF:
    620   3448  dh155122 			if (*i1 > rtss->rtss_max_buf) {
    621      0    stevel 				*outlenp = 0;
    622      0    stevel 				return (ENOBUFS);
    623      0    stevel 			}
    624      0    stevel 			break;	/* goto sizeof (int) option return */
    625      0    stevel 		case SO_RCVBUF:
    626   3448  dh155122 			if (*i1 > rtss->rtss_max_buf) {
    627      0    stevel 				*outlenp = 0;
    628      0    stevel 				return (ENOBUFS);
    629      0    stevel 			}
    630   8485     Peter 			break;	/* goto sizeof (int) option return */
    631   8485     Peter 		}
    632   8485     Peter 		break;
    633   8485     Peter 	case SOL_ROUTE:
    634   8485     Peter 		switch (name) {
    635   8485     Peter 		case RT_AWARE:
    636   8485     Peter 			if (!checkonly) {
    637   8485     Peter 				mutex_enter(&connp->conn_lock);
    638   8485     Peter 				connp->conn_rtaware = *i1;
    639   8485     Peter 				mutex_exit(&connp->conn_lock);
    640   8485     Peter 			}
    641  11042      Erik 			*outlenp = inlen;
    642  11042      Erik 			return (0);
    643      0    stevel 		}
    644      0    stevel 		break;
    645  11042      Erik 	}
    646  11042      Erik 	/* Serialized setsockopt since we are D_MTQPAIR */
    647  11042      Erik 	error = conn_opt_set(&coas, level, name, inlen, invalp,
    648  11042      Erik 	    checkonly, cr);
    649  11042      Erik 	if (error != 0) {
    650      0    stevel 		*outlenp = 0;
    651  11042      Erik 		return (error);
    652      0    stevel 	}
    653      0    stevel 	/*
    654      0    stevel 	 * Common case of return from an option that is sizeof (int)
    655      0    stevel 	 */
    656   8348      Eric 	if (invalp != outvalp) {
    657   8348      Eric 		/* don't trust bcopy for identical src/dst */
    658   8348      Eric 		(void) bcopy(invalp, outvalp, inlen);
    659   8348      Eric 	}
    660      0    stevel 	*outlenp = (t_uscalar_t)sizeof (int);
    661      0    stevel 	return (0);
    662   8348      Eric }
    663   8348      Eric 
    664   8348      Eric static int
    665   8348      Eric rts_opt_set(conn_t *connp, uint_t optset_context, int level, int name,
    666   8348      Eric     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
    667   8348      Eric     void *thisdg_attrs, cred_t *cr)
    668   8348      Eric {
    669   8348      Eric 	boolean_t 	checkonly = B_FALSE;
    670   8348      Eric 
    671   8348      Eric 	if (optset_context) {
    672   8348      Eric 		switch (optset_context) {
    673   8348      Eric 		case SETFN_OPTCOM_CHECKONLY:
    674   8348      Eric 			checkonly = B_TRUE;
    675   8348      Eric 			/*
    676   8348      Eric 			 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
    677   8348      Eric 			 * inlen != 0 implies value supplied and
    678   8348      Eric 			 * 	we have to "pretend" to set it.
    679   8348      Eric 			 * inlen == 0 implies that there is no value part
    680   8348      Eric 			 * 	in T_CHECK request and just validation
    681   8348      Eric 			 * done elsewhere should be enough, we just return here.
    682   8348      Eric 			 */
    683   8348      Eric 			if (inlen == 0) {
    684   8348      Eric 				*outlenp = 0;
    685   8348      Eric 				return (0);
    686   8348      Eric 			}
    687   8348      Eric 			break;
    688   8348      Eric 		case SETFN_OPTCOM_NEGOTIATE:
    689   8348      Eric 			checkonly = B_FALSE;
    690   8348      Eric 			break;
    691   8348      Eric 		case SETFN_UD_NEGOTIATE:
    692   8348      Eric 		case SETFN_CONN_NEGOTIATE:
    693   8348      Eric 			checkonly = B_FALSE;
    694   8348      Eric 			/*
    695   8348      Eric 			 * Negotiating local and "association-related" options
    696   8348      Eric 			 * through T_UNITDATA_REQ or T_CONN_{REQ,CON}
    697   8348      Eric 			 * Not allowed in this module.
    698   8348      Eric 			 */
    699   8348      Eric 			return (EINVAL);
    700   8348      Eric 		default:
    701   8348      Eric 			/*
    702   8348      Eric 			 * We should never get here
    703   8348      Eric 			 */
    704   8348      Eric 			*outlenp = 0;
    705   8348      Eric 			return (EINVAL);
    706   8348      Eric 		}
    707   8348      Eric 
    708   8348      Eric 		ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
    709   8348      Eric 		    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
    710   8348      Eric 
    711   8348      Eric 	}
    712   8348      Eric 	return (rts_do_opt_set(connp, level, name, inlen, invalp, outlenp,
    713   8348      Eric 	    outvalp, cr, thisdg_attrs, checkonly));
    714   8348      Eric 
    715   8348      Eric }
    716   8348      Eric 
    717   8348      Eric /*
    718   8348      Eric  * This routine retrieves the current status of socket options.
    719   8348      Eric  * It returns the size of the option retrieved.
    720   8348      Eric  */
    721   8348      Eric int
    722   8348      Eric rts_tpi_opt_get(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
    723   8348      Eric {
    724   8348      Eric 	rts_t	*rts;
    725   8348      Eric 	int	err;
    726   8348      Eric 
    727   8348      Eric 	rts = Q_TO_RTS(q);
    728   8348      Eric 	rw_enter(&rts->rts_rwlock, RW_READER);
    729   8348      Eric 	err = rts_opt_get(Q_TO_CONN(q), level, name, ptr);
    730   8348      Eric 	rw_exit(&rts->rts_rwlock);
    731   8348      Eric 	return (err);
    732   8348      Eric }
    733   8348      Eric 
    734   8348      Eric /*
    735   8348      Eric  * This routine sets socket options.
    736   8348      Eric  */
    737   8348      Eric /*ARGSUSED*/
    738   8348      Eric int
    739   8348      Eric rts_tpi_opt_set(queue_t *q, uint_t optset_context, int level,
    740   8348      Eric     int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp,
    741  11042      Erik     uchar_t *outvalp, void *thisdg_attrs, cred_t *cr)
    742   8348      Eric {
    743   8348      Eric 	conn_t	*connp = Q_TO_CONN(q);
    744   8348      Eric 	int	error;
    745   8348      Eric 	rts_t	*rts = connp->conn_rts;
    746   8348      Eric 
    747   8348      Eric 
    748   8348      Eric 	rw_enter(&rts->rts_rwlock, RW_WRITER);
    749   8348      Eric 	error = rts_opt_set(connp, optset_context, level, name, inlen, invalp,
    750   8348      Eric 	    outlenp, outvalp, thisdg_attrs, cr);
    751   8348      Eric 	rw_exit(&rts->rts_rwlock);
    752   8348      Eric 	return (error);
    753      0    stevel }
    754      0    stevel 
    755      0    stevel /*
    756      0    stevel  * This routine retrieves the value of an ND variable in a rtsparam_t
    757      0    stevel  * structure. It is called through nd_getset when a user reads the
    758      0    stevel  * variable.
    759      0    stevel  */
    760      0    stevel /* ARGSUSED */
    761      0    stevel static int
    762      0    stevel rts_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
    763      0    stevel {
    764      0    stevel 	rtsparam_t	*rtspa = (rtsparam_t *)cp;
    765      0    stevel 
    766      0    stevel 	(void) mi_mpprintf(mp, "%u", rtspa->rts_param_value);
    767      0    stevel 	return (0);
    768      0    stevel }
    769      0    stevel 
    770      0    stevel /*
    771      0    stevel  * Walk through the param array specified registering each element with the
    772      0    stevel  * named dispatch (ND) handler.
    773      0    stevel  */
    774      0    stevel static boolean_t
    775   3448  dh155122 rts_param_register(IDP *ndp, rtsparam_t *rtspa, int cnt)
    776      0    stevel {
    777      0    stevel 	for (; cnt-- > 0; rtspa++) {
    778      0    stevel 		if (rtspa->rts_param_name != NULL && rtspa->rts_param_name[0]) {
    779   3448  dh155122 			if (!nd_load(ndp, rtspa->rts_param_name,
    780      0    stevel 			    rts_param_get, rts_param_set, (caddr_t)rtspa)) {
    781   3448  dh155122 				nd_free(ndp);
    782      0    stevel 				return (B_FALSE);
    783      0    stevel 			}
    784      0    stevel 		}
    785      0    stevel 	}
    786      0    stevel 	return (B_TRUE);
    787      0    stevel }
    788      0    stevel 
    789      0    stevel /* This routine sets an ND variable in a rtsparam_t structure. */
    790      0    stevel /* ARGSUSED */
    791      0    stevel static int
    792      0    stevel rts_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
    793      0    stevel {
    794      0    stevel 	ulong_t	new_value;
    795      0    stevel 	rtsparam_t	*rtspa = (rtsparam_t *)cp;
    796      0    stevel 
    797      0    stevel 	/*
    798      0    stevel 	 * Fail the request if the new value does not lie within the
    799      0    stevel 	 * required bounds.
    800      0    stevel 	 */
    801      0    stevel 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
    802      0    stevel 	    new_value < rtspa->rts_param_min ||
    803      0    stevel 	    new_value > rtspa->rts_param_max) {
    804      0    stevel 		return (EINVAL);
    805      0    stevel 	}
    806      0    stevel 
    807      0    stevel 	/* Set the new value */
    808      0    stevel 	rtspa->rts_param_value = new_value;
    809      0    stevel 	return (0);
    810      0    stevel }
    811      0    stevel 
    812      0    stevel /*
    813   5240  nordmark  * Empty rsrv routine which is used by rts_input to cause a wakeup
    814   5240  nordmark  * of a thread in qwait.
    815   5240  nordmark  */
    816   5240  nordmark /*ARGSUSED*/
    817   5240  nordmark static void
    818   5240  nordmark rts_rsrv(queue_t *q)
    819   5240  nordmark {
    820   5240  nordmark }
    821   5240  nordmark 
    822   5240  nordmark /*
    823      0    stevel  * This routine handles synchronous messages passed downstream. It either
    824      0    stevel  * consumes the message or passes it downstream; it never queues a
    825      0    stevel  * a message. The data messages that go down are wrapped in an IOCTL
    826      0    stevel  * message.
    827      0    stevel  *
    828      0    stevel  * Since it is synchronous, it waits for the M_IOCACK/M_IOCNAK so that
    829      0    stevel  * it can return an immediate error (such as ENETUNREACH when adding a route).
    830      0    stevel  * It uses the RTS_WRW_PENDING to ensure that each rts instance has only
    831      0    stevel  * one M_IOCTL outstanding at any given time.
    832      0    stevel  */
    833      0    stevel static int
    834      0    stevel rts_wrw(queue_t *q, struiod_t *dp)
    835      0    stevel {
    836      0    stevel 	mblk_t	*mp = dp->d_mp;
    837      0    stevel 	mblk_t	*mp1;
    838      0    stevel 	int	error;
    839      0    stevel 	rt_msghdr_t	*rtm;
    840   5240  nordmark 	conn_t	*connp = Q_TO_CONN(q);
    841   5240  nordmark 	rts_t	*rts = connp->conn_rts;
    842      0    stevel 
    843      0    stevel 	while (rts->rts_flag & RTS_WRW_PENDING) {
    844      0    stevel 		if (qwait_rw(q)) {
    845      0    stevel 			rts->rts_error = EINTR;
    846      0    stevel 			goto err_ret;
    847      0    stevel 		}
    848   8348      Eric 	}
    849      0    stevel 	rts->rts_flag |= RTS_WRW_PENDING;
    850      0    stevel 
    851      0    stevel 	if (isuioq(q) && (error = struioget(q, mp, dp, 0))) {
    852      0    stevel 		/*
    853      0    stevel 		 * Uio error of some sort, so just return the error.
    854      0    stevel 		 */
    855      0    stevel 		rts->rts_error = error;
    856      0    stevel 		goto err_ret;
    857      0    stevel 	}
    858      0    stevel 	/*
    859      0    stevel 	 * Pass the mblk (chain) onto wput().
    860      0    stevel 	 */
    861      0    stevel 	dp->d_mp = 0;
    862      0    stevel 
    863      0    stevel 	switch (mp->b_datap->db_type) {
    864      0    stevel 	case M_PROTO:
    865      0    stevel 	case M_PCPROTO:
    866      0    stevel 		/* Expedite other than T_DATA_REQ to below the switch */
    867      0    stevel 		if (((mp->b_wptr - mp->b_rptr) !=
    868      0    stevel 		    sizeof (struct T_data_req)) ||
    869      0    stevel 		    (((union T_primitives *)mp->b_rptr)->type != T_DATA_REQ))
    870      0    stevel 			break;
    871      0    stevel 		if ((mp1 = mp->b_cont) == NULL) {
    872      0    stevel 			rts->rts_error = EINVAL;
    873   8752     Peter 			freemsg(mp);
    874      0    stevel 			goto err_ret;
    875      0    stevel 		}
    876      0    stevel 		freeb(mp);
    877      0    stevel 		mp = mp1;
    878      0    stevel 		/* FALLTHRU */
    879      0    stevel 	case M_DATA:
    880      0    stevel 		/*
    881      0    stevel 		 * The semantics of the routing socket is such that the rtm_pid
    882      0    stevel 		 * field is automatically filled in during requests with the
    883      0    stevel 		 * current process' pid.  We do this here (where we still have
    884      0    stevel 		 * user context) after checking we have at least a message the
    885      0    stevel 		 * size of a routing message header.
    886      0    stevel 		 */
    887      0    stevel 		if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
    888      0    stevel 			if (!pullupmsg(mp, sizeof (rt_msghdr_t))) {
    889      0    stevel 				rts->rts_error = EINVAL;
    890   8752     Peter 				freemsg(mp);
    891      0    stevel 				goto err_ret;
    892      0    stevel 			}
    893      0    stevel 		}
    894      0    stevel 		rtm = (rt_msghdr_t *)mp->b_rptr;
    895      0    stevel 		rtm->rtm_pid = curproc->p_pid;
    896      0    stevel 		break;
    897      0    stevel 	default:
    898      0    stevel 		break;
    899      0    stevel 	}
    900      0    stevel 	rts->rts_flag |= RTS_WPUT_PENDING;
    901      0    stevel 	rts_wput(q, mp);
    902      0    stevel 	while (rts->rts_flag & RTS_WPUT_PENDING)
    903      0    stevel 		if (qwait_rw(q)) {
    904      0    stevel 			/* RTS_WPUT_PENDING will be cleared below */
    905      0    stevel 			rts->rts_error = EINTR;
    906      0    stevel 			break;
    907      0    stevel 		}
    908      0    stevel err_ret:
    909      0    stevel 	rts->rts_flag &= ~(RTS_WPUT_PENDING | RTS_WRW_PENDING);
    910      0    stevel 	return (rts->rts_error);
    911      0    stevel }
    912      0    stevel 
    913      0    stevel /*
    914      0    stevel  * This routine handles all messages passed downstream. It either
    915      0    stevel  * consumes the message or passes it downstream; it never queues a
    916      0    stevel  * a message. The data messages that go down are wrapped in an IOCTL
    917      0    stevel  * message.
    918      0    stevel  */
    919      0    stevel static void
    920      0    stevel rts_wput(queue_t *q, mblk_t *mp)
    921      0    stevel {
    922      0    stevel 	uchar_t	*rptr = mp->b_rptr;
    923      0    stevel 	mblk_t	*mp1;
    924   5240  nordmark 	conn_t	*connp = Q_TO_CONN(q);
    925   5240  nordmark 	rts_t	*rts = connp->conn_rts;
    926      0    stevel 
    927      0    stevel 	switch (mp->b_datap->db_type) {
    928      0    stevel 	case M_DATA:
    929      0    stevel 		break;
    930      0    stevel 	case M_PROTO:
    931      0    stevel 	case M_PCPROTO:
    932      0    stevel 		if ((mp->b_wptr - rptr) == sizeof (struct T_data_req)) {
    933      0    stevel 			/* Expedite valid T_DATA_REQ to below the switch */
    934      0    stevel 			if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
    935      0    stevel 				mp1 = mp->b_cont;
    936      0    stevel 				freeb(mp);
    937      0    stevel 				if (mp1 == NULL)
    938      0    stevel 					return;
    939      0    stevel 				mp = mp1;
    940      0    stevel 				break;
    941      0    stevel 			}
    942      0    stevel 		}
    943      0    stevel 		/* FALLTHRU */
    944      0    stevel 	default:
    945      0    stevel 		rts_wput_other(q, mp);
    946      0    stevel 		return;
    947      0    stevel 	}
    948      0    stevel 
    949      0    stevel 
    950   8778      Erik 	ASSERT(msg_getcred(mp, NULL) != NULL);
    951   8778      Erik 
    952   8778      Erik 	mp1 = rts_ioctl_alloc(mp);
    953      0    stevel 	if (mp1 == NULL) {
    954      0    stevel 		ASSERT(rts != NULL);
    955      0    stevel 		freemsg(mp);
    956      0    stevel 		if (rts->rts_flag & RTS_WPUT_PENDING) {
    957      0    stevel 			rts->rts_error = ENOMEM;
    958      0    stevel 			rts->rts_flag &= ~RTS_WPUT_PENDING;
    959      0    stevel 		}
    960      0    stevel 		return;
    961      0    stevel 	}
    962  11042      Erik 	ip_wput_nondata(q, mp1);
    963      0    stevel }
    964      0    stevel 
    965      0    stevel 
    966      0    stevel /*
    967      0    stevel  * Handles all the control message, if it
    968      0    stevel  * can not understand it, it will
    969      0    stevel  * pass down stream.
    970      0    stevel  */
    971      0    stevel static void
    972      0    stevel rts_wput_other(queue_t *q, mblk_t *mp)
    973      0    stevel {
    974   5240  nordmark 	conn_t	*connp = Q_TO_CONN(q);
    975   5240  nordmark 	rts_t	*rts = connp->conn_rts;
    976      0    stevel 	uchar_t	*rptr = mp->b_rptr;
    977      0    stevel 	struct iocblk	*iocp;
    978      0    stevel 	cred_t	*cr;
    979   3448  dh155122 	rts_stack_t	*rtss;
    980      0    stevel 
    981   3448  dh155122 	rtss = rts->rts_rtss;
    982      0    stevel 
    983      0    stevel 	switch (mp->b_datap->db_type) {
    984      0    stevel 	case M_PROTO:
    985      0    stevel 	case M_PCPROTO:
    986      0    stevel 		if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) {
    987      0    stevel 			/*
    988      0    stevel 			 * If the message does not contain a PRIM_type,
    989      0    stevel 			 * throw it away.
    990      0    stevel 			 */
    991      0    stevel 			freemsg(mp);
    992      0    stevel 			return;
    993      0    stevel 		}
    994      0    stevel 		switch (((union T_primitives *)rptr)->type) {
    995      0    stevel 		case T_BIND_REQ:
    996      0    stevel 		case O_T_BIND_REQ:
    997   8348      Eric 			rts_tpi_bind(q, mp);
    998      0    stevel 			return;
    999      0    stevel 		case T_UNBIND_REQ:
   1000   8348      Eric 			rts_tpi_unbind(q, mp);
   1001      0    stevel 			return;
   1002      0    stevel 		case T_CAPABILITY_REQ:
   1003      0    stevel 			rts_capability_req(q, mp);
   1004      0    stevel 			return;
   1005      0    stevel 		case T_INFO_REQ:
   1006      0    stevel 			rts_info_req(q, mp);
   1007      0    stevel 			return;
   1008      0    stevel 		case T_SVR4_OPTMGMT_REQ:
   1009      0    stevel 		case T_OPTMGMT_REQ:
   1010   8778      Erik 			/*
   1011   8778      Erik 			 * All Solaris components should pass a db_credp
   1012   8778      Erik 			 * for this TPI message, hence we ASSERT.
   1013   8778      Erik 			 * But in case there is some other M_PROTO that looks
   1014   8778      Erik 			 * like a TPI message sent by some other kernel
   1015   8778      Erik 			 * component, we check and return an error.
   1016   8778      Erik 			 */
   1017   8778      Erik 			cr = msg_getcred(mp, NULL);
   1018   8778      Erik 			ASSERT(cr != NULL);
   1019   8778      Erik 			if (cr == NULL) {
   1020   8778      Erik 				rts_err_ack(q, mp, TSYSERR, EINVAL);
   1021   8778      Erik 				return;
   1022   8778      Erik 			}
   1023   8778      Erik 			if (((union T_primitives *)rptr)->type ==
   1024   8778      Erik 			    T_SVR4_OPTMGMT_REQ) {
   1025  11042      Erik 				svr4_optcom_req(q, mp, cr, &rts_opt_obj);
   1026   8778      Erik 			} else {
   1027  11042      Erik 				tpi_optcom_req(q, mp, cr, &rts_opt_obj);
   1028   8778      Erik 			}
   1029      0    stevel 			return;
   1030      0    stevel 		case O_T_CONN_RES:
   1031      0    stevel 		case T_CONN_RES:
   1032      0    stevel 		case T_DISCON_REQ:
   1033      0    stevel 			/* Not supported by rts. */
   1034      0    stevel 			rts_err_ack(q, mp, TNOTSUPPORT, 0);
   1035      0    stevel 			return;
   1036      0    stevel 		case T_DATA_REQ:
   1037      0    stevel 		case T_EXDATA_REQ:
   1038      0    stevel 		case T_ORDREL_REQ:
   1039      0    stevel 			/* Illegal for rts. */
   1040      0    stevel 			freemsg(mp);
   1041      0    stevel 			(void) putnextctl1(RD(q), M_ERROR, EPROTO);
   1042      0    stevel 			return;
   1043   8348      Eric 
   1044      0    stevel 		default:
   1045      0    stevel 			break;
   1046      0    stevel 		}
   1047      0    stevel 		break;
   1048      0    stevel 	case M_IOCTL:
   1049      0    stevel 		iocp = (struct iocblk *)mp->b_rptr;
   1050      0    stevel 		switch (iocp->ioc_cmd) {
   1051      0    stevel 		case ND_SET:
   1052      0    stevel 		case ND_GET:
   1053   3448  dh155122 			if (nd_getset(q, rtss->rtss_g_nd, mp)) {
   1054      0    stevel 				qreply(q, mp);
   1055      0    stevel 				return;
   1056      0    stevel 			}
   1057      0    stevel 			break;
   1058      0    stevel 		case TI_GETPEERNAME:
   1059      0    stevel 			mi_copyin(q, mp, NULL,
   1060      0    stevel 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
   1061      0    stevel 			return;
   1062      0    stevel 		default:
   1063      0    stevel 			break;
   1064      0    stevel 		}
   1065      0    stevel 	case M_IOCDATA:
   1066      0    stevel 		rts_wput_iocdata(q, mp);
   1067      0    stevel 		return;
   1068      0    stevel 	default:
   1069      0    stevel 		break;
   1070      0    stevel 	}
   1071  11042      Erik 	ip_wput_nondata(q, mp);
   1072      0    stevel }
   1073      0    stevel 
   1074      0    stevel /*
   1075      0    stevel  * Called by rts_wput_other to handle all M_IOCDATA messages.
   1076      0    stevel  */
   1077      0    stevel static void
   1078      0    stevel rts_wput_iocdata(queue_t *q, mblk_t *mp)
   1079      0    stevel {
   1080      0    stevel 	struct sockaddr	*rtsaddr;
   1081      0    stevel 	mblk_t	*mp1;
   1082      0    stevel 	STRUCT_HANDLE(strbuf, sb);
   1083      0    stevel 	struct iocblk	*iocp	= (struct iocblk *)mp->b_rptr;
   1084      0    stevel 
   1085      0    stevel 	/* Make sure it is one of ours. */
   1086      0    stevel 	switch (iocp->ioc_cmd) {
   1087      0    stevel 	case TI_GETPEERNAME:
   1088      0    stevel 		break;
   1089      0    stevel 	default:
   1090  11042      Erik 		ip_wput_nondata(q, mp);
   1091      0    stevel 		return;
   1092      0    stevel 	}
   1093      0    stevel 	switch (mi_copy_state(q, mp, &mp1)) {
   1094      0    stevel 	case -1:
   1095      0    stevel 		return;
   1096      0    stevel 	case MI_COPY_CASE(MI_COPY_IN, 1):
   1097      0    stevel 		break;
   1098      0    stevel 	case MI_COPY_CASE(MI_COPY_OUT, 1):
   1099      0    stevel 		/* Copy out the strbuf. */
   1100      0    stevel 		mi_copyout(q, mp);
   1101      0    stevel 		return;
   1102      0    stevel 	case MI_COPY_CASE(MI_COPY_OUT, 2):
   1103      0    stevel 		/* All done. */
   1104      0    stevel 		mi_copy_done(q, mp, 0);
   1105      0    stevel 		return;
   1106      0    stevel 	default:
   1107      0    stevel 		mi_copy_done(q, mp, EPROTO);
   1108      0    stevel 		return;
   1109      0    stevel 	}
   1110      0    stevel 	STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr);
   1111      0    stevel 	if (STRUCT_FGET(sb, maxlen) < (int)sizeof (sin_t)) {
   1112      0    stevel 		mi_copy_done(q, mp, EINVAL);
   1113      0    stevel 		return;
   1114      0    stevel 	}
   1115      0    stevel 	switch (iocp->ioc_cmd) {
   1116      0    stevel 	case TI_GETPEERNAME:
   1117      0    stevel 		break;
   1118      0    stevel 	default:
   1119      0    stevel 		mi_copy_done(q, mp, EPROTO);
   1120      0    stevel 		return;
   1121      0    stevel 	}
   1122      0    stevel 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), sizeof (sin_t),
   1123      0    stevel 	    B_TRUE);
   1124      0    stevel 	if (mp1 == NULL)
   1125      0    stevel 		return;
   1126      0    stevel 	STRUCT_FSET(sb, len, (int)sizeof (sin_t));
   1127      0    stevel 	rtsaddr = (struct sockaddr *)mp1->b_rptr;
   1128      0    stevel 	mp1->b_wptr = (uchar_t *)&rtsaddr[1];
   1129      0    stevel 	bzero(rtsaddr, sizeof (struct sockaddr));
   1130      0    stevel 	rtsaddr->sa_family = AF_ROUTE;
   1131      0    stevel 	/* Copy out the address */
   1132      0    stevel 	mi_copyout(q, mp);
   1133      0    stevel }
   1134      0    stevel 
   1135  11042      Erik /*
   1136  11042      Erik  * IP passes up a NULL ira.
   1137  11042      Erik  */
   1138   5240  nordmark /*ARGSUSED2*/
   1139      0    stevel static void
   1140  11042      Erik rts_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
   1141      0    stevel {
   1142   5240  nordmark 	conn_t *connp = (conn_t *)arg1;
   1143   5240  nordmark 	rts_t	*rts = connp->conn_rts;
   1144      0    stevel 	struct iocblk	*iocp;
   1145      0    stevel 	mblk_t *mp1;
   1146      0    stevel 	struct T_data_ind *tdi;
   1147   8348      Eric 	int	error;
   1148      0    stevel 
   1149      0    stevel 	switch (mp->b_datap->db_type) {
   1150      0    stevel 	case M_IOCACK:
   1151      0    stevel 	case M_IOCNAK:
   1152      0    stevel 		iocp = (struct iocblk *)mp->b_rptr;
   1153  11042      Erik 		ASSERT(!IPCL_IS_NONSTR(connp));
   1154  11042      Erik 		if (rts->rts_flag & (RTS_WPUT_PENDING)) {
   1155  11042      Erik 			rts->rts_flag &= ~RTS_WPUT_PENDING;
   1156      0    stevel 			rts->rts_error = iocp->ioc_error;
   1157  11042      Erik 			/*
   1158  11042      Erik 			 * Tell rts_wvw/qwait that we are done.
   1159  11042      Erik 			 * Note: there is no qwait_wakeup() we can use.
   1160  11042      Erik 			 */
   1161  11042      Erik 			qenable(connp->conn_rq);
   1162      0    stevel 			freemsg(mp);
   1163      0    stevel 			return;
   1164      0    stevel 		}
   1165      0    stevel 		break;
   1166      0    stevel 	case M_DATA:
   1167      0    stevel 		/*
   1168      0    stevel 		 * Prepend T_DATA_IND to prevent the stream head from
   1169      0    stevel 		 * consolidating multiple messages together.
   1170      0    stevel 		 * If the allocation fails just send up the M_DATA.
   1171      0    stevel 		 */
   1172      0    stevel 		mp1 = allocb(sizeof (*tdi), BPRI_MED);
   1173      0    stevel 		if (mp1 != NULL) {
   1174      0    stevel 			mp1->b_cont = mp;
   1175      0    stevel 			mp = mp1;
   1176      0    stevel 
   1177      0    stevel 			mp->b_datap->db_type = M_PROTO;
   1178      0    stevel 			mp->b_wptr += sizeof (*tdi);
   1179      0    stevel 			tdi = (struct T_data_ind *)mp->b_rptr;
   1180      0    stevel 			tdi->PRIM_type = T_DATA_IND;
   1181      0    stevel 			tdi->MORE_flag = 0;
   1182      0    stevel 		}
   1183      0    stevel 		break;
   1184      0    stevel 	default:
   1185      0    stevel 		break;
   1186      0    stevel 	}
   1187   8348      Eric 
   1188   8348      Eric 	if (IPCL_IS_NONSTR(connp)) {
   1189   8348      Eric 		if ((*connp->conn_upcalls->su_recv)
   1190   8348      Eric 		    (connp->conn_upper_handle, mp, msgdsize(mp), 0,
   1191   8348      Eric 		    &error, NULL) < 0) {
   1192   8348      Eric 			ASSERT(error == ENOSPC);
   1193   8348      Eric 			/*
   1194   8348      Eric 			 * Let's confirm hoding the lock that
   1195   8348      Eric 			 * we are out of recv space.
   1196   8348      Eric 			 */
   1197   8348      Eric 			mutex_enter(&rts->rts_recv_mutex);
   1198   8348      Eric 			if ((*connp->conn_upcalls->su_recv)
   1199   8348      Eric 			    (connp->conn_upper_handle, NULL, 0, 0,
   1200   8348      Eric 			    &error, NULL) < 0) {
   1201   8348      Eric 				ASSERT(error == ENOSPC);
   1202   8348      Eric 				connp->conn_flow_cntrld = B_TRUE;
   1203   8348      Eric 			}
   1204   8348      Eric 			mutex_exit(&rts->rts_recv_mutex);
   1205   8348      Eric 		}
   1206   8348      Eric 	} else {
   1207   8348      Eric 		putnext(connp->conn_rq, mp);
   1208   8348      Eric 	}
   1209      0    stevel }
   1210      0    stevel 
   1211  11042      Erik /*ARGSUSED*/
   1212  11042      Erik static void
   1213  11042      Erik rts_icmp_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
   1214  11042      Erik {
   1215  11042      Erik 	freemsg(mp);
   1216  11042      Erik }
   1217      0    stevel 
   1218      0    stevel void
   1219   8348      Eric rts_ddi_g_init(void)
   1220      0    stevel {
   1221      0    stevel 	rts_max_optsize = optcom_max_optsize(rts_opt_obj.odb_opt_des_arr,
   1222      0    stevel 	    rts_opt_obj.odb_opt_arr_cnt);
   1223   3448  dh155122 
   1224   3448  dh155122 	/*
   1225   3448  dh155122 	 * We want to be informed each time a stack is created or
   1226   3448  dh155122 	 * destroyed in the kernel, so we can maintain the
   1227   3448  dh155122 	 * set of rts_stack_t's.
   1228   3448  dh155122 	 */
   1229   3448  dh155122 	netstack_register(NS_RTS, rts_stack_init, NULL, rts_stack_fini);
   1230      0    stevel }
   1231   3448  dh155122 
   1232   3448  dh155122 void
   1233   8348      Eric rts_ddi_g_destroy(void)
   1234   3448  dh155122 {
   1235   3448  dh155122 	netstack_unregister(NS_RTS);
   1236   3448  dh155122 }
   1237   8348      Eric 
   1238   8348      Eric #define	INET_NAME	"ip"
   1239   3448  dh155122 
   1240   3448  dh155122 /*
   1241   3448  dh155122  * Initialize the RTS stack instance.
   1242   3448  dh155122  */
   1243   3448  dh155122 /* ARGSUSED */
   1244   3448  dh155122 static void *
   1245   3448  dh155122 rts_stack_init(netstackid_t stackid, netstack_t *ns)
   1246   3448  dh155122 {
   1247   3448  dh155122 	rts_stack_t	*rtss;
   1248   3448  dh155122 	rtsparam_t	*pa;
   1249   8348      Eric 	int		error = 0;
   1250   8348      Eric 	major_t		major;
   1251   3448  dh155122 
   1252   3448  dh155122 	rtss = (rts_stack_t *)kmem_zalloc(sizeof (*rtss), KM_SLEEP);
   1253   3448  dh155122 	rtss->rtss_netstack = ns;
   1254   3448  dh155122 
   1255   3448  dh155122 	pa = (rtsparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
   1256   3448  dh155122 	rtss->rtss_params = pa;
   1257   3448  dh155122 	bcopy(lcl_param_arr, rtss->rtss_params, sizeof (lcl_param_arr));
   1258   3448  dh155122 
   1259   3448  dh155122 	(void) rts_param_register(&rtss->rtss_g_nd,
   1260   3448  dh155122 	    rtss->rtss_params, A_CNT(lcl_param_arr));
   1261   8348      Eric 
   1262   8348      Eric 	major = mod_name_to_major(INET_NAME);
   1263   8348      Eric 	error = ldi_ident_from_major(major, &rtss->rtss_ldi_ident);
   1264   8348      Eric 	ASSERT(error == 0);
   1265   3448  dh155122 	return (rtss);
   1266   3448  dh155122 }
   1267   3448  dh155122 
   1268   3448  dh155122 /*
   1269   3448  dh155122  * Free the RTS stack instance.
   1270   3448  dh155122  */
   1271   3448  dh155122 /* ARGSUSED */
   1272   3448  dh155122 static void
   1273   3448  dh155122 rts_stack_fini(netstackid_t stackid, void *arg)
   1274   3448  dh155122 {
   1275   3448  dh155122 	rts_stack_t *rtss = (rts_stack_t *)arg;
   1276   3448  dh155122 
   1277   5240  nordmark 	nd_free(&rtss->rtss_g_nd);
   1278   3448  dh155122 	kmem_free(rtss->rtss_params, sizeof (lcl_param_arr));
   1279   3448  dh155122 	rtss->rtss_params = NULL;
   1280   8348      Eric 	ldi_ident_release(rtss->rtss_ldi_ident);
   1281   3448  dh155122 	kmem_free(rtss, sizeof (*rtss));
   1282   3448  dh155122 }
   1283   8348      Eric 
   1284   8348      Eric /* ARGSUSED */
   1285   8348      Eric int
   1286   8348      Eric rts_accept(sock_lower_handle_t lproto_handle,
   1287   8348      Eric     sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle,
   1288   8348      Eric     cred_t *cr)
   1289   8348      Eric {
   1290   8348      Eric 	return (EINVAL);
   1291   8348      Eric }
   1292   8348      Eric 
   1293   8348      Eric /* ARGSUSED */
   1294   8348      Eric static int
   1295   8348      Eric rts_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa,
   1296   8348      Eric     socklen_t len, cred_t *cr)
   1297   8348      Eric {
   1298   8348      Eric 	/*
   1299   8348      Eric 	 * rebind not allowed
   1300   8348      Eric 	 */
   1301   8348      Eric 	return (EINVAL);
   1302   8348      Eric }
   1303   8348      Eric 
   1304   8348      Eric /* ARGSUSED */
   1305   8348      Eric int
   1306   8348      Eric rts_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr)
   1307   8348      Eric {
   1308   8348      Eric 	return (EINVAL);
   1309   8348      Eric }
   1310   8348      Eric 
   1311   8348      Eric /* ARGSUSED */
   1312   8348      Eric int
   1313   8348      Eric rts_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa,
   1314   8348      Eric     socklen_t len, sock_connid_t *id, cred_t *cr)
   1315   8348      Eric {
   1316   8348      Eric 	/*
   1317   8348      Eric 	 * rts sockets start out as bound and connected
   1318   8348      Eric 	 */
   1319   8348      Eric 	*id = 0;
   1320   8348      Eric 	return (EISCONN);
   1321   8348      Eric }
   1322   8348      Eric 
   1323   8348      Eric /* ARGSUSED */
   1324   8348      Eric int
   1325   8348      Eric rts_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr,
   1326   8348      Eric     socklen_t *addrlen, cred_t *cr)
   1327   8348      Eric {
   1328   8348      Eric 	bzero(addr, sizeof (struct sockaddr));
   1329   8348      Eric 	addr->sa_family = AF_ROUTE;
   1330   8348      Eric 	*addrlen = sizeof (struct sockaddr);
   1331   8348      Eric 
   1332   8348      Eric 	return (0);
   1333   8348      Eric }
   1334   8348      Eric 
   1335   8348      Eric /* ARGSUSED */
   1336   8348      Eric int
   1337   8348      Eric rts_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr,
   1338   8348      Eric     socklen_t *addrlen, cred_t *cr)
   1339   8348      Eric {
   1340  11042      Erik 	bzero(addr, sizeof (struct sockaddr));
   1341  11042      Erik 	addr->sa_family = AF_ROUTE;
   1342  11042      Erik 	*addrlen = sizeof (struct sockaddr);
   1343  11042      Erik 
   1344  11042      Erik 	return (0);
   1345   8348      Eric }
   1346   8348      Eric 
   1347   8348      Eric static int
   1348   8348      Eric rts_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
   1349   8348      Eric     void *optvalp, socklen_t *optlen, cred_t *cr)
   1350   8348      Eric {
   1351   8348      Eric 	conn_t  	*connp = (conn_t *)proto_handle;
   1352   8348      Eric 	rts_t		*rts = connp->conn_rts;
   1353   8348      Eric 	int		error;
   1354   8348      Eric 	t_uscalar_t	max_optbuf_len;
   1355   8348      Eric 	void		*optvalp_buf;
   1356   8348      Eric 	int		len;
   1357   8348      Eric 
   1358   8348      Eric 	error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len,
   1359   8348      Eric 	    rts_opt_obj.odb_opt_des_arr,
   1360   8348      Eric 	    rts_opt_obj.odb_opt_arr_cnt,
   1361   8348      Eric 	    B_FALSE, B_TRUE, cr);
   1362   8348      Eric 	if (error != 0) {
   1363   8348      Eric 		if (error < 0)
   1364   8348      Eric 			error = proto_tlitosyserr(-error);
   1365   8348      Eric 		return (error);
   1366   8348      Eric 	}
   1367   8348      Eric 
   1368   8348      Eric 	optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP);
   1369   8348      Eric 	rw_enter(&rts->rts_rwlock, RW_READER);
   1370   8348      Eric 	len = rts_opt_get(connp, level, option_name, optvalp_buf);
   1371   8348      Eric 	rw_exit(&rts->rts_rwlock);
   1372  11042      Erik 	if (len == -1) {
   1373  11042      Erik 		kmem_free(optvalp_buf, max_optbuf_len);
   1374  11042      Erik 		return (EINVAL);
   1375   8348      Eric 	}
   1376   8348      Eric 
   1377  11042      Erik 	/*
   1378  11042      Erik 	 * update optlen and copy option value
   1379  11042      Erik 	 */
   1380  11042      Erik 	t_uscalar_t size = MIN(len, *optlen);
   1381  11042      Erik 
   1382  11042      Erik 	bcopy(optvalp_buf, optvalp, size);
   1383  11042      Erik 	bcopy(&size, optlen, sizeof (size));
   1384   8348      Eric 	kmem_free(optvalp_buf, max_optbuf_len);
   1385  11042      Erik 	return (0);
   1386   8348      Eric }
   1387   8348      Eric 
   1388   8348      Eric static int
   1389   8348      Eric rts_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
   1390   8348      Eric     const void *optvalp, socklen_t optlen, cred_t *cr)
   1391   8348      Eric {
   1392   8348      Eric 	conn_t	*connp = (conn_t *)proto_handle;
   1393   8348      Eric 	rts_t	*rts = connp->conn_rts;
   1394   8348      Eric 	int	error;
   1395   8348      Eric 
   1396   8348      Eric 	error = proto_opt_check(level, option_name, optlen, NULL,
   1397   8348      Eric 	    rts_opt_obj.odb_opt_des_arr,
   1398   8348      Eric 	    rts_opt_obj.odb_opt_arr_cnt,
   1399   8348      Eric 	    B_TRUE, B_FALSE, cr);
   1400   8348      Eric 
   1401   8348      Eric 	if (error != 0) {
   1402   8348      Eric 		if (error < 0)
   1403   8348      Eric 			error = proto_tlitosyserr(-error);
   1404   8348      Eric 		return (error);
   1405   8348      Eric 	}
   1406   8348      Eric 
   1407   8348      Eric 	rw_enter(&rts->rts_rwlock, RW_WRITER);
   1408   8348      Eric 	error = rts_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name,
   1409   8348      Eric 	    optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp,
   1410   8348      Eric 	    NULL, cr);
   1411   8348      Eric 	rw_exit(&rts->rts_rwlock);
   1412   8348      Eric 
   1413   8348      Eric 	ASSERT(error >= 0);
   1414   8348      Eric 
   1415   8348      Eric 	return (error);
   1416   8348      Eric }
   1417   8348      Eric 
   1418   8348      Eric /* ARGSUSED */
   1419   8348      Eric static int
   1420   8348      Eric rts_send(sock_lower_handle_t proto_handle, mblk_t *mp,
   1421   8348      Eric     struct nmsghdr *msg, cred_t *cr)
   1422   8348      Eric {
   1423   8348      Eric 	conn_t  *connp = (conn_t *)proto_handle;
   1424   8348      Eric 	rt_msghdr_t	*rtm;
   1425   8348      Eric 	int error;
   1426   8348      Eric 
   1427   8348      Eric 	ASSERT(DB_TYPE(mp) == M_DATA);
   1428   8348      Eric 	/*
   1429   8348      Eric 	 * The semantics of the routing socket is such that the rtm_pid
   1430   8348      Eric 	 * field is automatically filled in during requests with the
   1431   8348      Eric 	 * current process' pid.  We do this here (where we still have
   1432   8348      Eric 	 * user context) after checking we have at least a message the
   1433   8348      Eric 	 * size of a routing message header.
   1434   8348      Eric 	 */
   1435   8348      Eric 	if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
   1436   8348      Eric 		if (!pullupmsg(mp, sizeof (rt_msghdr_t))) {
   1437   8348      Eric 			freemsg(mp);
   1438  11042      Erik 			return (EINVAL);
   1439   8348      Eric 		}
   1440   8348      Eric 	}
   1441   8348      Eric 	rtm = (rt_msghdr_t *)mp->b_rptr;
   1442   8348      Eric 	rtm->rtm_pid = curproc->p_pid;
   1443   8348      Eric 
   1444   8348      Eric 	/*
   1445  11042      Erik 	 * We are not constrained by the ioctl interface and
   1446  11042      Erik 	 * ip_rts_request_common processing requests synchronously hence
   1447  11042      Erik 	 * we can send them down concurrently.
   1448   8348      Eric 	 */
   1449  11042      Erik 	error = ip_rts_request_common(mp, connp, cr);
   1450   8348      Eric 	return (error);
   1451   8348      Eric }
   1452   8348      Eric 
   1453   8348      Eric /* ARGSUSED */
   1454   8348      Eric sock_lower_handle_t
   1455   8348      Eric rts_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls,
   1456   8348      Eric     uint_t *smodep, int *errorp, int flags, cred_t *credp)
   1457   8348      Eric {
   1458   8348      Eric 	conn_t	*connp;
   1459   8348      Eric 
   1460   8348      Eric 	if (family != AF_ROUTE || type != SOCK_RAW ||
   1461   8348      Eric 	    (proto != 0 && proto != AF_INET && proto != AF_INET6)) {
   1462   8348      Eric 		*errorp = EPROTONOSUPPORT;
   1463   8348      Eric 		return (NULL);
   1464   8348      Eric 	}
   1465   8348      Eric 
   1466   8348      Eric 	connp = rts_open(flags, credp);
   1467   8348      Eric 	ASSERT(connp != NULL);
   1468   8348      Eric 	connp->conn_flags |= IPCL_NONSTR;
   1469   8348      Eric 
   1470  11042      Erik 	connp->conn_proto = proto;
   1471   8348      Eric 
   1472   8348      Eric 	mutex_enter(&connp->conn_lock);
   1473   8348      Eric 	connp->conn_state_flags &= ~CONN_INCIPIENT;
   1474   8348      Eric 	mutex_exit(&connp->conn_lock);
   1475   8348      Eric 
   1476   8348      Eric 	*errorp = 0;
   1477   8348      Eric 	*smodep = SM_ATOMIC;
   1478   8348      Eric 	*sock_downcalls = &sock_rts_downcalls;
   1479   8348      Eric 	return ((sock_lower_handle_t)connp);
   1480   8348      Eric }
   1481   8348      Eric 
   1482   8348      Eric /* ARGSUSED */
   1483   8348      Eric void
   1484   8348      Eric rts_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle,
   1485   8348      Eric     sock_upcalls_t *sock_upcalls, int flags, cred_t *cr)
   1486   8348      Eric {
   1487   8348      Eric 	conn_t  *connp = (conn_t *)proto_handle;
   1488   8348      Eric 	struct sock_proto_props sopp;
   1489   8348      Eric 
   1490   8348      Eric 	connp->conn_upcalls = sock_upcalls;
   1491   8348      Eric 	connp->conn_upper_handle = sock_handle;
   1492   8348      Eric 
   1493   8348      Eric 	sopp.sopp_flags = SOCKOPT_WROFF | SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT |
   1494   8348      Eric 	    SOCKOPT_MAXBLK | SOCKOPT_MAXPSZ | SOCKOPT_MINPSZ;
   1495   8348      Eric 	sopp.sopp_wroff = 0;
   1496  11042      Erik 	sopp.sopp_rxhiwat = connp->conn_rcvbuf;
   1497  11042      Erik 	sopp.sopp_rxlowat = connp->conn_rcvlowat;
   1498   8348      Eric 	sopp.sopp_maxblk = INFPSZ;
   1499   8348      Eric 	sopp.sopp_maxpsz = rts_mod_info.mi_maxpsz;
   1500   8348      Eric 	sopp.sopp_minpsz = (rts_mod_info.mi_minpsz == 1) ? 0 :
   1501   8348      Eric 	    rts_mod_info.mi_minpsz;
   1502   8348      Eric 
   1503   8348      Eric 	(*connp->conn_upcalls->su_set_proto_props)
   1504   8348      Eric 	    (connp->conn_upper_handle, &sopp);
   1505   8348      Eric 
   1506   8348      Eric 	/*
   1507   8348      Eric 	 * We treat it as already connected for routing socket.
   1508   8348      Eric 	 */
   1509   8348      Eric 	(*connp->conn_upcalls->su_connected)
   1510   8348      Eric 	    (connp->conn_upper_handle, 0, NULL, -1);
   1511   8348      Eric 
   1512  11042      Erik 	/* Indicate to IP that this is a routing socket client */
   1513   8348      Eric 	ip_rts_register(connp);
   1514   8348      Eric }
   1515   8348      Eric 
   1516   8348      Eric /* ARGSUSED */
   1517   8348      Eric int
   1518   8348      Eric rts_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr)
   1519   8348      Eric {
   1520   8348      Eric 	conn_t  *connp = (conn_t *)proto_handle;
   1521   8348      Eric 
   1522   8348      Eric 	ASSERT(connp != NULL && IPCL_IS_RTS(connp));
   1523   8348      Eric 	return (rts_common_close(NULL, connp));
   1524   8348      Eric }
   1525   8348      Eric 
   1526   8348      Eric /* ARGSUSED */
   1527   8348      Eric int
   1528   8348      Eric rts_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr)
   1529   8348      Eric {
   1530   8348      Eric 	conn_t  *connp = (conn_t *)proto_handle;
   1531   8348      Eric 
   1532   8348      Eric 	/* shut down the send side */
   1533   8348      Eric 	if (how != SHUT_RD)
   1534   8348      Eric 		(*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
   1535   8348      Eric 		    SOCK_OPCTL_SHUT_SEND, 0);
   1536   8348      Eric 	/* shut down the recv side */
   1537   8348      Eric 	if (how != SHUT_WR)
   1538   8348      Eric 		(*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
   1539   8348      Eric 		    SOCK_OPCTL_SHUT_RECV, 0);
   1540   8348      Eric 	return (0);
   1541   8348      Eric }
   1542   8348      Eric 
   1543   8348      Eric void
   1544   8348      Eric rts_clr_flowctrl(sock_lower_handle_t proto_handle)
   1545   8348      Eric {
   1546   8348      Eric 	conn_t  *connp = (conn_t *)proto_handle;
   1547   8348      Eric 	rts_t	*rts = connp->conn_rts;
   1548   8348      Eric 
   1549   8348      Eric 	mutex_enter(&rts->rts_recv_mutex);
   1550   8348      Eric 	connp->conn_flow_cntrld = B_FALSE;
   1551   8348      Eric 	mutex_exit(&rts->rts_recv_mutex);
   1552   8348      Eric }
   1553   8348      Eric 
   1554   8348      Eric int
   1555   8348      Eric rts_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg,
   1556   8348      Eric     int mode, int32_t *rvalp, cred_t *cr)
   1557   8348      Eric {
   1558   8348      Eric 	conn_t		*connp = (conn_t *)proto_handle;
   1559   8348      Eric 	int		error;
   1560   8348      Eric 
   1561  11042      Erik 	/*
   1562  11042      Erik 	 * If we don't have a helper stream then create one.
   1563  11042      Erik 	 * ip_create_helper_stream takes care of locking the conn_t,
   1564  11042      Erik 	 * so this check for NULL is just a performance optimization.
   1565  11042      Erik 	 */
   1566  11042      Erik 	if (connp->conn_helper_info == NULL) {
   1567  11042      Erik 		rts_stack_t *rtss = connp->conn_rts->rts_rtss;
   1568  11042      Erik 
   1569  11042      Erik 		ASSERT(rtss->rtss_ldi_ident != NULL);
   1570  11042      Erik 
   1571  11042      Erik 		/*
   1572  11042      Erik 		 * Create a helper stream for non-STREAMS socket.
   1573  11042      Erik 		 */
   1574  11042      Erik 		error = ip_create_helper_stream(connp, rtss->rtss_ldi_ident);
   1575  11042      Erik 		if (error != 0) {
   1576  11042      Erik 			ip0dbg(("rts_ioctl: create of IP helper stream "
   1577  11042      Erik 			    "failed %d\n", error));
   1578  11042      Erik 			return (error);
   1579  11042      Erik 		}
   1580  11042      Erik 	}
   1581  11042      Erik 
   1582   8348      Eric 	switch (cmd) {
   1583   8348      Eric 	case ND_SET:
   1584   8348      Eric 	case ND_GET:
   1585   8348      Eric 	case TI_GETPEERNAME:
   1586   8348      Eric 	case TI_GETMYNAME:
   1587   8348      Eric #ifdef DEUG
   1588   8348      Eric 		cmn_err(CE_CONT, "rts_ioctl cmd 0x%x on non sreams"
   1589   8348      Eric 		    " socket", cmd);
   1590   8348      Eric #endif
   1591   8348      Eric 		error = EINVAL;
   1592   8348      Eric 		break;
   1593   8348      Eric 	default:
   1594   8348      Eric 		/*
   1595   8348      Eric 		 * Pass on to IP using helper stream
   1596   8348      Eric 		 */
   1597   8444       Rao 		error = ldi_ioctl(connp->conn_helper_info->iphs_handle,
   1598   8348      Eric 		    cmd, arg, mode, cr, rvalp);
   1599   8348      Eric 		break;
   1600   8348      Eric 	}
   1601   8348      Eric 
   1602   8348      Eric 	return (error);
   1603   8348      Eric }
   1604   8348      Eric 
   1605   8348      Eric sock_downcalls_t sock_rts_downcalls = {
   1606   8348      Eric 	rts_activate,
   1607   8348      Eric 	rts_accept,
   1608   8348      Eric 	rts_bind,
   1609   8348      Eric 	rts_listen,
   1610   8348      Eric 	rts_connect,
   1611   8348      Eric 	rts_getpeername,
   1612   8348      Eric 	rts_getsockname,
   1613   8348      Eric 	rts_getsockopt,
   1614   8348      Eric 	rts_setsockopt,
   1615   8348      Eric 	rts_send,
   1616   8348      Eric 	NULL,
   1617   8348      Eric 	NULL,
   1618   8348      Eric 	NULL,
   1619   8348      Eric 	rts_shutdown,
   1620   8348      Eric 	rts_clr_flowctrl,
   1621   8348      Eric 	rts_ioctl,
   1622   8348      Eric 	rts_close
   1623   8348      Eric };
   1624