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
588cda078Skcpoon * Common Development and Distribution License (the "License").
688cda078Skcpoon * 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 /*
22be4c8f74SErik Nordmark * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24a4ea78eaSToomas Soome * Copyright 2018 Toomas Soome <tsoome@me.com>
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/stream.h>
297c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
307c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
317c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
327c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
337c478bd9Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
347c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
357c478bd9Sstevel@tonic-gate #include <sys/timod.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
397c478bd9Sstevel@tonic-gate #include <sys/proc.h>
407c478bd9Sstevel@tonic-gate #include <sys/suntpi.h>
417c478bd9Sstevel@tonic-gate #include <sys/policy.h>
42f4b3ec61Sdh #include <sys/zone.h>
430f1702c5SYu Xiangning #include <sys/disp.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #include <sys/socket.h>
460f1702c5SYu Xiangning #include <sys/socketvar.h>
477c478bd9Sstevel@tonic-gate #include <netinet/in.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #include <inet/common.h>
507c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
517c478bd9Sstevel@tonic-gate #include <inet/ip.h>
52fc80c0dfSnordmark #include <inet/ipclassifier.h>
530f1702c5SYu Xiangning #include <inet/proto_set.h>
547c478bd9Sstevel@tonic-gate #include <inet/nd.h>
557c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
567c478bd9Sstevel@tonic-gate #include <netinet/ip_mroute.h>
577c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
587c478bd9Sstevel@tonic-gate #include <net/route.h>
597c478bd9Sstevel@tonic-gate
60fc80c0dfSnordmark #include <inet/rts_impl.h>
61fc80c0dfSnordmark #include <inet/ip_rts.h>
62fc80c0dfSnordmark
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * This is a transport provider for routing sockets. Downstream messages are
657c478bd9Sstevel@tonic-gate * wrapped with a IP_IOCTL header, and ip_wput_ioctl calls the appropriate entry
667c478bd9Sstevel@tonic-gate * in the ip_ioctl_ftbl callout table to pass the routing socket data into IP.
677c478bd9Sstevel@tonic-gate * Upstream messages are generated for listeners of the routing socket as well
687c478bd9Sstevel@tonic-gate * as the message sender (unless they have turned off their end using
697c478bd9Sstevel@tonic-gate * SO_USELOOPBACK or shutdown(3n)). Upstream messages may also be generated
707c478bd9Sstevel@tonic-gate * asynchronously when:
717c478bd9Sstevel@tonic-gate *
727c478bd9Sstevel@tonic-gate * Interfaces are brought up or down.
737c478bd9Sstevel@tonic-gate * Addresses are assigned to interfaces.
7427c48ed9Snordmark * ICMP redirects are processed and a IRE_HOST/RTF_DYNAMIC is installed.
757c478bd9Sstevel@tonic-gate * No route is found while sending a packet.
767c478bd9Sstevel@tonic-gate *
777c478bd9Sstevel@tonic-gate * Since all we do is reformat the messages between routing socket and
787c478bd9Sstevel@tonic-gate * ioctl forms, no synchronization is necessary in this module; all
797c478bd9Sstevel@tonic-gate * the dirty work is done down in ip.
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages */
837c478bd9Sstevel@tonic-gate static struct T_info_ack rts_g_t_info_ack = {
847c478bd9Sstevel@tonic-gate T_INFO_ACK,
857c478bd9Sstevel@tonic-gate T_INFINITE, /* TSDU_size. Maximum size messages. */
867c478bd9Sstevel@tonic-gate T_INVALID, /* ETSDU_size. No expedited data. */
877c478bd9Sstevel@tonic-gate T_INVALID, /* CDATA_size. No connect data. */
887c478bd9Sstevel@tonic-gate T_INVALID, /* DDATA_size. No disconnect data. */
897c478bd9Sstevel@tonic-gate 0, /* ADDR_size. */
907c478bd9Sstevel@tonic-gate 0, /* OPT_size - not initialized here */
917c478bd9Sstevel@tonic-gate 64 * 1024, /* TIDU_size. rts allows maximum size messages. */
927c478bd9Sstevel@tonic-gate T_COTS, /* SERV_type. rts supports connection oriented. */
937c478bd9Sstevel@tonic-gate TS_UNBND, /* CURRENT_state. This is set from rts_state. */
947c478bd9Sstevel@tonic-gate (XPG4_1) /* PROVIDER_flag */
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate * Table of ND variables supported by rts. These are loaded into rts_g_nd
997c478bd9Sstevel@tonic-gate * in rts_open.
1007c478bd9Sstevel@tonic-gate * All of these are alterable, within the min/max values given, at run time.
1017c478bd9Sstevel@tonic-gate */
102f4b3ec61Sdh static rtsparam_t lcl_param_arr[] = {
1037c478bd9Sstevel@tonic-gate /* min max value name */
1047c478bd9Sstevel@tonic-gate { 4096, 65536, 8192, "rts_xmit_hiwat"},
1057c478bd9Sstevel@tonic-gate { 0, 65536, 1024, "rts_xmit_lowat"},
1067c478bd9Sstevel@tonic-gate { 4096, 65536, 8192, "rts_recv_hiwat"},
1077c478bd9Sstevel@tonic-gate { 65536, 1024*1024*1024, 256*1024, "rts_max_buf"},
1087c478bd9Sstevel@tonic-gate };
109f4b3ec61Sdh #define rtss_xmit_hiwat rtss_params[0].rts_param_value
110f4b3ec61Sdh #define rtss_xmit_lowat rtss_params[1].rts_param_value
111f4b3ec61Sdh #define rtss_recv_hiwat rtss_params[2].rts_param_value
112fc80c0dfSnordmark #define rtss_max_buf rtss_params[3].rts_param_value
1137c478bd9Sstevel@tonic-gate
114a4ea78eaSToomas Soome static void rts_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
1157c478bd9Sstevel@tonic-gate int sys_error);
116bd670b35SErik Nordmark static void rts_input(void *, mblk_t *, void *, ip_recv_attr_t *);
117bd670b35SErik Nordmark static void rts_icmp_input(void *, mblk_t *, void *, ip_recv_attr_t *);
118de8c4a14SErik Nordmark static mblk_t *rts_ioctl_alloc(mblk_t *data);
1197c478bd9Sstevel@tonic-gate static int rts_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr);
120f4b3ec61Sdh static boolean_t rts_param_register(IDP *ndp, rtsparam_t *rtspa, int cnt);
1217c478bd9Sstevel@tonic-gate static int rts_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp,
1227c478bd9Sstevel@tonic-gate cred_t *cr);
123*8a06b3d6SToomas Soome static int rts_rsrv(queue_t *q);
124f4b3ec61Sdh static void *rts_stack_init(netstackid_t stackid, netstack_t *ns);
125f4b3ec61Sdh static void rts_stack_fini(netstackid_t stackid, void *arg);
126*8a06b3d6SToomas Soome static int rts_wput(queue_t *q, mblk_t *mp);
1277c478bd9Sstevel@tonic-gate static void rts_wput_iocdata(queue_t *q, mblk_t *mp);
128a4ea78eaSToomas Soome static void rts_wput_other(queue_t *q, mblk_t *mp);
1297c478bd9Sstevel@tonic-gate static int rts_wrw(queue_t *q, struiod_t *dp);
1307c478bd9Sstevel@tonic-gate
1310f1702c5SYu Xiangning static int rts_stream_open(queue_t *q, dev_t *devp, int flag, int sflag,
1320f1702c5SYu Xiangning cred_t *credp);
1330f1702c5SYu Xiangning static conn_t *rts_open(int flag, cred_t *credp);
1340f1702c5SYu Xiangning
1355e1743f0SToomas Soome static int rts_stream_close(queue_t *, int, cred_t *);
1360f1702c5SYu Xiangning static int rts_close(sock_lower_handle_t proto_handle, int flags,
1370f1702c5SYu Xiangning cred_t *cr);
1380f1702c5SYu Xiangning
139fc80c0dfSnordmark static struct module_info rts_mod_info = {
1407c478bd9Sstevel@tonic-gate 129, "rts", 1, INFPSZ, 512, 128
1417c478bd9Sstevel@tonic-gate };
1427c478bd9Sstevel@tonic-gate
143fc80c0dfSnordmark static struct qinit rtsrinit = {
144*8a06b3d6SToomas Soome NULL, rts_rsrv, rts_stream_open, rts_stream_close, NULL,
1450f1702c5SYu Xiangning &rts_mod_info
1467c478bd9Sstevel@tonic-gate };
1477c478bd9Sstevel@tonic-gate
148fc80c0dfSnordmark static struct qinit rtswinit = {
149*8a06b3d6SToomas Soome rts_wput, NULL, NULL, NULL, NULL, &rts_mod_info,
150*8a06b3d6SToomas Soome NULL, rts_wrw, NULL, STRUIOT_STANDARD
1517c478bd9Sstevel@tonic-gate };
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate struct streamtab rtsinfo = {
154fc80c0dfSnordmark &rtsrinit, &rtswinit
1557c478bd9Sstevel@tonic-gate };
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate * This routine allocates the necessary
1597c478bd9Sstevel@tonic-gate * message blocks for IOCTL wrapping the
1607c478bd9Sstevel@tonic-gate * user data.
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate static mblk_t *
rts_ioctl_alloc(mblk_t * data)163de8c4a14SErik Nordmark rts_ioctl_alloc(mblk_t *data)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate mblk_t *mp = NULL;
1667c478bd9Sstevel@tonic-gate mblk_t *mp1 = NULL;
1677c478bd9Sstevel@tonic-gate ipllc_t *ipllc;
1687c478bd9Sstevel@tonic-gate struct iocblk *ioc;
1697c478bd9Sstevel@tonic-gate
170de8c4a14SErik Nordmark mp = allocb_tmpl(sizeof (ipllc_t), data);
1717c478bd9Sstevel@tonic-gate if (mp == NULL)
1727c478bd9Sstevel@tonic-gate return (NULL);
173de8c4a14SErik Nordmark mp1 = allocb_tmpl(sizeof (struct iocblk), data);
1747c478bd9Sstevel@tonic-gate if (mp1 == NULL) {
1757c478bd9Sstevel@tonic-gate freeb(mp);
1767c478bd9Sstevel@tonic-gate return (NULL);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate ipllc = (ipllc_t *)mp->b_rptr;
1807c478bd9Sstevel@tonic-gate ipllc->ipllc_cmd = IP_IOC_RTS_REQUEST;
1817c478bd9Sstevel@tonic-gate ipllc->ipllc_name_offset = 0;
1827c478bd9Sstevel@tonic-gate ipllc->ipllc_name_length = 0;
1837c478bd9Sstevel@tonic-gate mp->b_wptr += sizeof (ipllc_t);
1847c478bd9Sstevel@tonic-gate mp->b_cont = data;
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate ioc = (struct iocblk *)mp1->b_rptr;
1877c478bd9Sstevel@tonic-gate ioc->ioc_cmd = IP_IOCTL;
1887c478bd9Sstevel@tonic-gate ioc->ioc_error = 0;
1897c478bd9Sstevel@tonic-gate ioc->ioc_cr = NULL;
1907c478bd9Sstevel@tonic-gate ioc->ioc_count = msgdsize(mp);
1917c478bd9Sstevel@tonic-gate mp1->b_wptr += sizeof (struct iocblk);
1927c478bd9Sstevel@tonic-gate mp1->b_datap->db_type = M_IOCTL;
1937c478bd9Sstevel@tonic-gate mp1->b_cont = mp;
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate return (mp1);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate * This routine closes rts stream, by disabling
2007c478bd9Sstevel@tonic-gate * put/srv routines and freeing the this module
2017c478bd9Sstevel@tonic-gate * internal datastructure.
2027c478bd9Sstevel@tonic-gate */
2037c478bd9Sstevel@tonic-gate static int
rts_common_close(queue_t * q,conn_t * connp)2040f1702c5SYu Xiangning rts_common_close(queue_t *q, conn_t *connp)
2057c478bd9Sstevel@tonic-gate {
206fc80c0dfSnordmark
207fc80c0dfSnordmark ASSERT(connp != NULL && IPCL_IS_RTS(connp));
208fc80c0dfSnordmark
209fc80c0dfSnordmark ip_rts_unregister(connp);
210fc80c0dfSnordmark
211fc80c0dfSnordmark ip_quiesce_conn(connp);
212f4b3ec61Sdh
2130f1702c5SYu Xiangning if (!IPCL_IS_NONSTR(connp)) {
2140f1702c5SYu Xiangning qprocsoff(q);
215bd670b35SErik Nordmark }
2167c478bd9Sstevel@tonic-gate
217bd670b35SErik Nordmark /*
218bd670b35SErik Nordmark * Now we are truly single threaded on this stream, and can
219bd670b35SErik Nordmark * delete the things hanging off the connp, and finally the connp.
220bd670b35SErik Nordmark * We removed this connp from the fanout list, it cannot be
221bd670b35SErik Nordmark * accessed thru the fanouts, and we already waited for the
222bd670b35SErik Nordmark * conn_ref to drop to 0. We are already in close, so
223bd670b35SErik Nordmark * there cannot be any other thread from the top. qprocsoff
224bd670b35SErik Nordmark * has completed, and service has completed or won't run in
225bd670b35SErik Nordmark * future.
226bd670b35SErik Nordmark */
227bd670b35SErik Nordmark ASSERT(connp->conn_ref == 1);
228bd670b35SErik Nordmark
229bd670b35SErik Nordmark if (!IPCL_IS_NONSTR(connp)) {
2300f1702c5SYu Xiangning inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
2310f1702c5SYu Xiangning } else {
232bfcb55b8SRao Shoaib ip_free_helper_stream(connp);
2330f1702c5SYu Xiangning }
234fc80c0dfSnordmark
235fc80c0dfSnordmark connp->conn_ref--;
236fc80c0dfSnordmark ipcl_conn_destroy(connp);
2370f1702c5SYu Xiangning return (0);
2380f1702c5SYu Xiangning }
2390f1702c5SYu Xiangning
2405e1743f0SToomas Soome /* ARGSUSED */
2410f1702c5SYu Xiangning static int
rts_stream_close(queue_t * q,int flags __unused,cred_t * credp __unused)2425e1743f0SToomas Soome rts_stream_close(queue_t *q, int flags __unused, cred_t *credp __unused)
2430f1702c5SYu Xiangning {
2440f1702c5SYu Xiangning conn_t *connp = Q_TO_CONN(q);
2450f1702c5SYu Xiangning
2460f1702c5SYu Xiangning (void) rts_common_close(q, connp);
247fc80c0dfSnordmark q->q_ptr = WR(q)->q_ptr = NULL;
2487c478bd9Sstevel@tonic-gate return (0);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * This is the open routine for routing socket. It allocates
253fc80c0dfSnordmark * rts_t structure for the stream and tells IP that it is a routing socket.
2547c478bd9Sstevel@tonic-gate */
2557c478bd9Sstevel@tonic-gate /* ARGSUSED */
2567c478bd9Sstevel@tonic-gate static int
rts_stream_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)2570f1702c5SYu Xiangning rts_stream_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2587c478bd9Sstevel@tonic-gate {
259fc80c0dfSnordmark conn_t *connp;
260fc80c0dfSnordmark dev_t conn_dev;
2610f1702c5SYu Xiangning rts_t *rts;
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate /* If the stream is already open, return immediately. */
2647c478bd9Sstevel@tonic-gate if (q->q_ptr != NULL)
2657c478bd9Sstevel@tonic-gate return (0);
2667c478bd9Sstevel@tonic-gate
267fc80c0dfSnordmark if (sflag == MODOPEN)
2687c478bd9Sstevel@tonic-gate return (EINVAL);
2697c478bd9Sstevel@tonic-gate
270aa92d85bSgt /*
271aa92d85bSgt * Since RTS is not used so heavily, allocating from the small
272aa92d85bSgt * arena should be sufficient.
273aa92d85bSgt */
274aa92d85bSgt if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) {
275fc80c0dfSnordmark return (EBUSY);
276fc80c0dfSnordmark }
2770f1702c5SYu Xiangning
2780f1702c5SYu Xiangning connp = rts_open(flag, credp);
2790f1702c5SYu Xiangning ASSERT(connp != NULL);
2800f1702c5SYu Xiangning
281fc80c0dfSnordmark *devp = makedevice(getemajor(*devp), (minor_t)conn_dev);
2827c478bd9Sstevel@tonic-gate
283fc80c0dfSnordmark rts = connp->conn_rts;
2840f1702c5SYu Xiangning rw_enter(&rts->rts_rwlock, RW_WRITER);
2850f1702c5SYu Xiangning connp->conn_dev = conn_dev;
2860f1702c5SYu Xiangning connp->conn_minor_arena = ip_minor_arena_sa;
287fc80c0dfSnordmark
288fc80c0dfSnordmark q->q_ptr = connp;
289fc80c0dfSnordmark WR(q)->q_ptr = connp;
290fc80c0dfSnordmark connp->conn_rq = q;
291fc80c0dfSnordmark connp->conn_wq = WR(q);
292fc80c0dfSnordmark
293bd670b35SErik Nordmark WR(q)->q_hiwat = connp->conn_sndbuf;
294bd670b35SErik Nordmark WR(q)->q_lowat = connp->conn_sndlowat;
295fc80c0dfSnordmark
296fc80c0dfSnordmark mutex_enter(&connp->conn_lock);
297fc80c0dfSnordmark connp->conn_state_flags &= ~CONN_INCIPIENT;
298fc80c0dfSnordmark mutex_exit(&connp->conn_lock);
299fc80c0dfSnordmark rw_exit(&rts->rts_rwlock);
300bd670b35SErik Nordmark
301bd670b35SErik Nordmark /* Indicate to IP that this is a routing socket client */
302fc80c0dfSnordmark ip_rts_register(connp);
303fc80c0dfSnordmark
304bd670b35SErik Nordmark qprocson(q);
305bd670b35SErik Nordmark
3067c478bd9Sstevel@tonic-gate return (0);
3070f1702c5SYu Xiangning }
3080f1702c5SYu Xiangning
3090f1702c5SYu Xiangning /* ARGSUSED */
3100f1702c5SYu Xiangning static conn_t *
rts_open(int flag,cred_t * credp)3110f1702c5SYu Xiangning rts_open(int flag, cred_t *credp)
3120f1702c5SYu Xiangning {
3130f1702c5SYu Xiangning netstack_t *ns;
3140f1702c5SYu Xiangning rts_stack_t *rtss;
3150f1702c5SYu Xiangning rts_t *rts;
3160f1702c5SYu Xiangning conn_t *connp;
3170f1702c5SYu Xiangning zoneid_t zoneid;
3180f1702c5SYu Xiangning
3190f1702c5SYu Xiangning ns = netstack_find_by_cred(credp);
3200f1702c5SYu Xiangning ASSERT(ns != NULL);
3210f1702c5SYu Xiangning rtss = ns->netstack_rts;
3220f1702c5SYu Xiangning ASSERT(rtss != NULL);
3230f1702c5SYu Xiangning
3240f1702c5SYu Xiangning /*
3250f1702c5SYu Xiangning * For exclusive stacks we set the zoneid to zero
3260f1702c5SYu Xiangning * to make RTS operate as if in the global zone.
3270f1702c5SYu Xiangning */
3280f1702c5SYu Xiangning if (ns->netstack_stackid != GLOBAL_NETSTACKID)
3290f1702c5SYu Xiangning zoneid = GLOBAL_ZONEID;
3300f1702c5SYu Xiangning else
3310f1702c5SYu Xiangning zoneid = crgetzoneid(credp);
3320f1702c5SYu Xiangning
3330f1702c5SYu Xiangning connp = ipcl_conn_create(IPCL_RTSCONN, KM_SLEEP, ns);
3340f1702c5SYu Xiangning rts = connp->conn_rts;
3350f1702c5SYu Xiangning
3360f1702c5SYu Xiangning /*
3370f1702c5SYu Xiangning * ipcl_conn_create did a netstack_hold. Undo the hold that was
3380f1702c5SYu Xiangning * done by netstack_find_by_cred()
3390f1702c5SYu Xiangning */
3400f1702c5SYu Xiangning netstack_rele(ns);
3410f1702c5SYu Xiangning
3420f1702c5SYu Xiangning rw_enter(&rts->rts_rwlock, RW_WRITER);
3430f1702c5SYu Xiangning ASSERT(connp->conn_rts == rts);
3440f1702c5SYu Xiangning ASSERT(rts->rts_connp == connp);
3450f1702c5SYu Xiangning
346bd670b35SErik Nordmark connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
347bd670b35SErik Nordmark /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
348bd670b35SErik Nordmark connp->conn_ixa->ixa_zoneid = zoneid;
3490f1702c5SYu Xiangning connp->conn_zoneid = zoneid;
3500f1702c5SYu Xiangning connp->conn_flow_cntrld = B_FALSE;
351fc80c0dfSnordmark
3520f1702c5SYu Xiangning rts->rts_rtss = rtss;
353bd670b35SErik Nordmark
354bd670b35SErik Nordmark connp->conn_rcvbuf = rtss->rtss_recv_hiwat;
355bd670b35SErik Nordmark connp->conn_sndbuf = rtss->rtss_xmit_hiwat;
356bd670b35SErik Nordmark connp->conn_sndlowat = rtss->rtss_xmit_lowat;
357bd670b35SErik Nordmark connp->conn_rcvlowat = rts_mod_info.mi_lowat;
358bd670b35SErik Nordmark
359bd670b35SErik Nordmark connp->conn_family = PF_ROUTE;
360bd670b35SErik Nordmark connp->conn_so_type = SOCK_RAW;
361bd670b35SErik Nordmark /* SO_PROTOTYPE is always sent down by sockfs setting conn_proto */
3620f1702c5SYu Xiangning
3630f1702c5SYu Xiangning connp->conn_recv = rts_input;
364bd670b35SErik Nordmark connp->conn_recvicmp = rts_icmp_input;
365bd670b35SErik Nordmark
3660f1702c5SYu Xiangning crhold(credp);
3670f1702c5SYu Xiangning connp->conn_cred = credp;
368bd670b35SErik Nordmark connp->conn_cpid = curproc->p_pid;
369bd670b35SErik Nordmark /* Cache things in ixa without an extra refhold */
370be4c8f74SErik Nordmark ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
371bd670b35SErik Nordmark connp->conn_ixa->ixa_cred = connp->conn_cred;
372bd670b35SErik Nordmark connp->conn_ixa->ixa_cpid = connp->conn_cpid;
373bd670b35SErik Nordmark if (is_system_labeled())
374bd670b35SErik Nordmark connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
3750f1702c5SYu Xiangning
3760f1702c5SYu Xiangning /*
3770f1702c5SYu Xiangning * rts sockets start out as bound and connected
3780f1702c5SYu Xiangning * For streams based sockets, socket state is set to
3790f1702c5SYu Xiangning * SS_ISBOUND | SS_ISCONNECTED in so_strinit.
3800f1702c5SYu Xiangning */
3810f1702c5SYu Xiangning rts->rts_state = TS_DATA_XFER;
3820f1702c5SYu Xiangning rw_exit(&rts->rts_rwlock);
3830f1702c5SYu Xiangning
3840f1702c5SYu Xiangning return (connp);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate * This routine creates a T_ERROR_ACK message and passes it upstream.
3897c478bd9Sstevel@tonic-gate */
3907c478bd9Sstevel@tonic-gate static void
rts_err_ack(queue_t * q,mblk_t * mp,t_scalar_t t_error,int sys_error)3917c478bd9Sstevel@tonic-gate rts_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
3947c478bd9Sstevel@tonic-gate qreply(q, mp);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate * This routine creates a T_OK_ACK message and passes it upstream.
3997c478bd9Sstevel@tonic-gate */
4007c478bd9Sstevel@tonic-gate static void
rts_ok_ack(queue_t * q,mblk_t * mp)4017c478bd9Sstevel@tonic-gate rts_ok_ack(queue_t *q, mblk_t *mp)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate if ((mp = mi_tpi_ok_ack_alloc(mp)) != NULL)
4047c478bd9Sstevel@tonic-gate qreply(q, mp);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate * This routine is called by rts_wput to handle T_UNBIND_REQ messages.
4097c478bd9Sstevel@tonic-gate */
4107c478bd9Sstevel@tonic-gate static void
rts_tpi_unbind(queue_t * q,mblk_t * mp)4110f1702c5SYu Xiangning rts_tpi_unbind(queue_t *q, mblk_t *mp)
4127c478bd9Sstevel@tonic-gate {
413fc80c0dfSnordmark conn_t *connp = Q_TO_CONN(q);
414fc80c0dfSnordmark rts_t *rts = connp->conn_rts;
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate /* If a bind has not been done, we can't unbind. */
4177c478bd9Sstevel@tonic-gate if (rts->rts_state != TS_IDLE) {
4187c478bd9Sstevel@tonic-gate rts_err_ack(q, mp, TOUTSTATE, 0);
4197c478bd9Sstevel@tonic-gate return;
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate rts->rts_state = TS_UNBND;
4227c478bd9Sstevel@tonic-gate rts_ok_ack(q, mp);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate * This routine is called to handle each
4277c478bd9Sstevel@tonic-gate * O_T_BIND_REQ/T_BIND_REQ message passed to
4287c478bd9Sstevel@tonic-gate * rts_wput. Note: This routine works with both
4297c478bd9Sstevel@tonic-gate * O_T_BIND_REQ and T_BIND_REQ semantics.
4307c478bd9Sstevel@tonic-gate */
4317c478bd9Sstevel@tonic-gate static void
rts_tpi_bind(queue_t * q,mblk_t * mp)4320f1702c5SYu Xiangning rts_tpi_bind(queue_t *q, mblk_t *mp)
4337c478bd9Sstevel@tonic-gate {
434fc80c0dfSnordmark conn_t *connp = Q_TO_CONN(q);
435fc80c0dfSnordmark rts_t *rts = connp->conn_rts;
4367c478bd9Sstevel@tonic-gate struct T_bind_req *tbr;
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
4397c478bd9Sstevel@tonic-gate (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
4400f1702c5SYu Xiangning "rts_tpi_bind: bad data, %d", rts->rts_state);
4417c478bd9Sstevel@tonic-gate rts_err_ack(q, mp, TBADADDR, 0);
4427c478bd9Sstevel@tonic-gate return;
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate if (rts->rts_state != TS_UNBND) {
4457c478bd9Sstevel@tonic-gate (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
4460f1702c5SYu Xiangning "rts_tpi_bind: bad state, %d", rts->rts_state);
4477c478bd9Sstevel@tonic-gate rts_err_ack(q, mp, TOUTSTATE, 0);
4487c478bd9Sstevel@tonic-gate return;
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate tbr = (struct T_bind_req *)mp->b_rptr;
4517c478bd9Sstevel@tonic-gate if (tbr->ADDR_length != 0) {
4527c478bd9Sstevel@tonic-gate (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
4530f1702c5SYu Xiangning "rts_tpi_bind: bad ADDR_length %d", tbr->ADDR_length);
4547c478bd9Sstevel@tonic-gate rts_err_ack(q, mp, TBADADDR, 0);
4557c478bd9Sstevel@tonic-gate return;
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate /* Generic request */
4587c478bd9Sstevel@tonic-gate tbr->ADDR_offset = (t_scalar_t)sizeof (struct T_bind_req);
4597c478bd9Sstevel@tonic-gate tbr->ADDR_length = 0;
4607c478bd9Sstevel@tonic-gate tbr->PRIM_type = T_BIND_ACK;
461bd670b35SErik Nordmark mp->b_datap->db_type = M_PCPROTO;
4627c478bd9Sstevel@tonic-gate rts->rts_state = TS_IDLE;
4637c478bd9Sstevel@tonic-gate qreply(q, mp);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate static void
rts_copy_info(struct T_info_ack * tap,rts_t * rts)4677c478bd9Sstevel@tonic-gate rts_copy_info(struct T_info_ack *tap, rts_t *rts)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate *tap = rts_g_t_info_ack;
4707c478bd9Sstevel@tonic-gate tap->CURRENT_state = rts->rts_state;
4717c478bd9Sstevel@tonic-gate tap->OPT_size = rts_max_optsize;
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate * This routine responds to T_CAPABILITY_REQ messages. It is called by
4767c478bd9Sstevel@tonic-gate * rts_wput. Much of the T_CAPABILITY_ACK information is copied from
4777c478bd9Sstevel@tonic-gate * rts_g_t_info_ack. The current state of the stream is copied from
4787c478bd9Sstevel@tonic-gate * rts_state.
4797c478bd9Sstevel@tonic-gate */
4807c478bd9Sstevel@tonic-gate static void
rts_capability_req(queue_t * q,mblk_t * mp)4817c478bd9Sstevel@tonic-gate rts_capability_req(queue_t *q, mblk_t *mp)
4827c478bd9Sstevel@tonic-gate {
483fc80c0dfSnordmark conn_t *connp = Q_TO_CONN(q);
484fc80c0dfSnordmark rts_t *rts = connp->conn_rts;
4857c478bd9Sstevel@tonic-gate t_uscalar_t cap_bits1;
4867c478bd9Sstevel@tonic-gate struct T_capability_ack *tcap;
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
491fc80c0dfSnordmark mp->b_datap->db_type, T_CAPABILITY_ACK);
4927c478bd9Sstevel@tonic-gate if (mp == NULL)
4937c478bd9Sstevel@tonic-gate return;
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate tcap = (struct T_capability_ack *)mp->b_rptr;
4967c478bd9Sstevel@tonic-gate tcap->CAP_bits1 = 0;
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate if (cap_bits1 & TC1_INFO) {
4997c478bd9Sstevel@tonic-gate rts_copy_info(&tcap->INFO_ack, rts);
5007c478bd9Sstevel@tonic-gate tcap->CAP_bits1 |= TC1_INFO;
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate qreply(q, mp);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate * This routine responds to T_INFO_REQ messages. It is called by rts_wput.
5087c478bd9Sstevel@tonic-gate * Most of the T_INFO_ACK information is copied from rts_g_t_info_ack.
5097c478bd9Sstevel@tonic-gate * The current state of the stream is copied from rts_state.
5107c478bd9Sstevel@tonic-gate */
5117c478bd9Sstevel@tonic-gate static void
rts_info_req(queue_t * q,mblk_t * mp)5127c478bd9Sstevel@tonic-gate rts_info_req(queue_t *q, mblk_t *mp)
5137c478bd9Sstevel@tonic-gate {
514fc80c0dfSnordmark conn_t *connp = Q_TO_CONN(q);
515fc80c0dfSnordmark rts_t *rts = connp->conn_rts;
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (rts_g_t_info_ack), M_PCPROTO,
5187c478bd9Sstevel@tonic-gate T_INFO_ACK);
5197c478bd9Sstevel@tonic-gate if (mp == NULL)
5207c478bd9Sstevel@tonic-gate return;
5217c478bd9Sstevel@tonic-gate rts_copy_info((struct T_info_ack *)mp->b_rptr, rts);
5227c478bd9Sstevel@tonic-gate qreply(q, mp);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate /*
5267c478bd9Sstevel@tonic-gate * This routine gets default values of certain options whose default
5277c478bd9Sstevel@tonic-gate * values are maintained by protcol specific code
5287c478bd9Sstevel@tonic-gate */
5297c478bd9Sstevel@tonic-gate /* ARGSUSED */
5307c478bd9Sstevel@tonic-gate int
rts_opt_default(queue_t * q,t_scalar_t level,t_scalar_t name,uchar_t * ptr)5317c478bd9Sstevel@tonic-gate rts_opt_default(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate /* no default value processed by protocol specific code currently */
5347c478bd9Sstevel@tonic-gate return (-1);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate
5370f1702c5SYu Xiangning
5380f1702c5SYu Xiangning static int
rts_opt_get(conn_t * connp,int level,int name,uchar_t * ptr)5390f1702c5SYu Xiangning rts_opt_get(conn_t *connp, int level, int name, uchar_t *ptr)
5407c478bd9Sstevel@tonic-gate {
541fc80c0dfSnordmark rts_t *rts = connp->conn_rts;
542bd670b35SErik Nordmark conn_opt_arg_t coas;
543bd670b35SErik Nordmark int retval;
5440f1702c5SYu Xiangning
5450f1702c5SYu Xiangning ASSERT(RW_READ_HELD(&rts->rts_rwlock));
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate switch (level) {
548bd670b35SErik Nordmark /* do this in conn_opt_get? */
549e11c3f44Smeem case SOL_ROUTE:
550e11c3f44Smeem switch (name) {
551e11c3f44Smeem case RT_AWARE:
552e11c3f44Smeem mutex_enter(&connp->conn_lock);
553bd670b35SErik Nordmark *(int *)ptr = connp->conn_rtaware;
554e11c3f44Smeem mutex_exit(&connp->conn_lock);
555bd670b35SErik Nordmark return (0);
556e11c3f44Smeem }
557e11c3f44Smeem break;
5587c478bd9Sstevel@tonic-gate }
559bd670b35SErik Nordmark coas.coa_connp = connp;
560bd670b35SErik Nordmark coas.coa_ixa = connp->conn_ixa;
561bd670b35SErik Nordmark coas.coa_ipp = &connp->conn_xmit_ipp;
562bd670b35SErik Nordmark mutex_enter(&connp->conn_lock);
563bd670b35SErik Nordmark retval = conn_opt_get(&coas, level, name, ptr);
564bd670b35SErik Nordmark mutex_exit(&connp->conn_lock);
565bd670b35SErik Nordmark return (retval);
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate
5680f1702c5SYu Xiangning /* ARGSUSED */
5690f1702c5SYu Xiangning static int
rts_do_opt_set(conn_t * connp,int level,int name,uint_t inlen,uchar_t * invalp,uint_t * outlenp,uchar_t * outvalp,cred_t * cr,void * thisdg_attrs,boolean_t checkonly)5700f1702c5SYu Xiangning rts_do_opt_set(conn_t *connp, int level, int name, uint_t inlen,
5710f1702c5SYu Xiangning uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, cred_t *cr,
5720f1702c5SYu Xiangning void *thisdg_attrs, boolean_t checkonly)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate int *i1 = (int *)invalp;
575fc80c0dfSnordmark rts_t *rts = connp->conn_rts;
576f4b3ec61Sdh rts_stack_t *rtss = rts->rts_rtss;
577bd670b35SErik Nordmark int error;
578bd670b35SErik Nordmark conn_opt_arg_t coas;
579bd670b35SErik Nordmark
580bd670b35SErik Nordmark coas.coa_connp = connp;
581bd670b35SErik Nordmark coas.coa_ixa = connp->conn_ixa;
582bd670b35SErik Nordmark coas.coa_ipp = &connp->conn_xmit_ipp;
5837c478bd9Sstevel@tonic-gate
5840f1702c5SYu Xiangning ASSERT(RW_WRITE_HELD(&rts->rts_rwlock));
5857c478bd9Sstevel@tonic-gate
5867c478bd9Sstevel@tonic-gate /*
5877c478bd9Sstevel@tonic-gate * For rts, we should have no ancillary data sent down
5887c478bd9Sstevel@tonic-gate * (rts_wput doesn't handle options).
5897c478bd9Sstevel@tonic-gate */
5907c478bd9Sstevel@tonic-gate ASSERT(thisdg_attrs == NULL);
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate /*
5937c478bd9Sstevel@tonic-gate * For fixed length options, no sanity check
5947c478bd9Sstevel@tonic-gate * of passed in length is done. It is assumed *_optcom_req()
5957c478bd9Sstevel@tonic-gate * routines do the right thing.
5967c478bd9Sstevel@tonic-gate */
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate switch (level) {
5997c478bd9Sstevel@tonic-gate case SOL_SOCKET:
6007c478bd9Sstevel@tonic-gate switch (name) {
6017c478bd9Sstevel@tonic-gate case SO_PROTOTYPE:
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate * Routing socket applications that call socket() with
6047c478bd9Sstevel@tonic-gate * a third argument can filter which messages will be
6057c478bd9Sstevel@tonic-gate * sent upstream thanks to sockfs. so_socket() sends
6067c478bd9Sstevel@tonic-gate * down the SO_PROTOTYPE and rts_queue_input()
6077c478bd9Sstevel@tonic-gate * implements the filtering.
6087c478bd9Sstevel@tonic-gate */
609bd670b35SErik Nordmark if (*i1 != AF_INET && *i1 != AF_INET6) {
610bd670b35SErik Nordmark *outlenp = 0;
6117c478bd9Sstevel@tonic-gate return (EPROTONOSUPPORT);
612beb2c8edSRao Shoaib }
613bd670b35SErik Nordmark if (!checkonly)
614bd670b35SErik Nordmark connp->conn_proto = *i1;
615bd670b35SErik Nordmark *outlenp = inlen;
616bd670b35SErik Nordmark return (0);
617bd670b35SErik Nordmark
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate * The following two items can be manipulated,
6207c478bd9Sstevel@tonic-gate * but changing them should do nothing.
6217c478bd9Sstevel@tonic-gate */
6227c478bd9Sstevel@tonic-gate case SO_SNDBUF:
623f4b3ec61Sdh if (*i1 > rtss->rtss_max_buf) {
6247c478bd9Sstevel@tonic-gate *outlenp = 0;
6257c478bd9Sstevel@tonic-gate return (ENOBUFS);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate break; /* goto sizeof (int) option return */
6287c478bd9Sstevel@tonic-gate case SO_RCVBUF:
629f4b3ec61Sdh if (*i1 > rtss->rtss_max_buf) {
6307c478bd9Sstevel@tonic-gate *outlenp = 0;
6317c478bd9Sstevel@tonic-gate return (ENOBUFS);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate break; /* goto sizeof (int) option return */
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate break;
636e11c3f44Smeem case SOL_ROUTE:
637e11c3f44Smeem switch (name) {
638e11c3f44Smeem case RT_AWARE:
639e11c3f44Smeem if (!checkonly) {
640e11c3f44Smeem mutex_enter(&connp->conn_lock);
641e11c3f44Smeem connp->conn_rtaware = *i1;
642e11c3f44Smeem mutex_exit(&connp->conn_lock);
643e11c3f44Smeem }
644bd670b35SErik Nordmark *outlenp = inlen;
645bd670b35SErik Nordmark return (0);
646e11c3f44Smeem }
647e11c3f44Smeem break;
648bd670b35SErik Nordmark }
649bd670b35SErik Nordmark /* Serialized setsockopt since we are D_MTQPAIR */
650bd670b35SErik Nordmark error = conn_opt_set(&coas, level, name, inlen, invalp,
651bd670b35SErik Nordmark checkonly, cr);
652bd670b35SErik Nordmark if (error != 0) {
6537c478bd9Sstevel@tonic-gate *outlenp = 0;
654bd670b35SErik Nordmark return (error);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate /*
6577c478bd9Sstevel@tonic-gate * Common case of return from an option that is sizeof (int)
6587c478bd9Sstevel@tonic-gate */
6590f1702c5SYu Xiangning if (invalp != outvalp) {
6600f1702c5SYu Xiangning /* don't trust bcopy for identical src/dst */
6610f1702c5SYu Xiangning (void) bcopy(invalp, outvalp, inlen);
6620f1702c5SYu Xiangning }
6637c478bd9Sstevel@tonic-gate *outlenp = (t_uscalar_t)sizeof (int);
6647c478bd9Sstevel@tonic-gate return (0);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate
6670f1702c5SYu Xiangning static int
rts_opt_set(conn_t * connp,uint_t optset_context,int level,int name,uint_t inlen,uchar_t * invalp,uint_t * outlenp,uchar_t * outvalp,void * thisdg_attrs,cred_t * cr)6680f1702c5SYu Xiangning rts_opt_set(conn_t *connp, uint_t optset_context, int level, int name,
6690f1702c5SYu Xiangning uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
6700f1702c5SYu Xiangning void *thisdg_attrs, cred_t *cr)
6710f1702c5SYu Xiangning {
672a4ea78eaSToomas Soome boolean_t checkonly = B_FALSE;
6730f1702c5SYu Xiangning
6740f1702c5SYu Xiangning if (optset_context) {
6750f1702c5SYu Xiangning switch (optset_context) {
6760f1702c5SYu Xiangning case SETFN_OPTCOM_CHECKONLY:
6770f1702c5SYu Xiangning checkonly = B_TRUE;
6780f1702c5SYu Xiangning /*
6790f1702c5SYu Xiangning * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
6800f1702c5SYu Xiangning * inlen != 0 implies value supplied and
681a4ea78eaSToomas Soome * we have to "pretend" to set it.
6820f1702c5SYu Xiangning * inlen == 0 implies that there is no value part
683a4ea78eaSToomas Soome * in T_CHECK request and just validation
6840f1702c5SYu Xiangning * done elsewhere should be enough, we just return here.
6850f1702c5SYu Xiangning */
6860f1702c5SYu Xiangning if (inlen == 0) {
6870f1702c5SYu Xiangning *outlenp = 0;
6880f1702c5SYu Xiangning return (0);
6890f1702c5SYu Xiangning }
6900f1702c5SYu Xiangning break;
6910f1702c5SYu Xiangning case SETFN_OPTCOM_NEGOTIATE:
6920f1702c5SYu Xiangning checkonly = B_FALSE;
6930f1702c5SYu Xiangning break;
6940f1702c5SYu Xiangning case SETFN_UD_NEGOTIATE:
6950f1702c5SYu Xiangning case SETFN_CONN_NEGOTIATE:
6960f1702c5SYu Xiangning checkonly = B_FALSE;
6970f1702c5SYu Xiangning /*
6980f1702c5SYu Xiangning * Negotiating local and "association-related" options
6990f1702c5SYu Xiangning * through T_UNITDATA_REQ or T_CONN_{REQ,CON}
7000f1702c5SYu Xiangning * Not allowed in this module.
7010f1702c5SYu Xiangning */
7020f1702c5SYu Xiangning return (EINVAL);
7030f1702c5SYu Xiangning default:
7040f1702c5SYu Xiangning /*
7050f1702c5SYu Xiangning * We should never get here
7060f1702c5SYu Xiangning */
7070f1702c5SYu Xiangning *outlenp = 0;
7080f1702c5SYu Xiangning return (EINVAL);
7090f1702c5SYu Xiangning }
7100f1702c5SYu Xiangning
7110f1702c5SYu Xiangning ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
7120f1702c5SYu Xiangning (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
7130f1702c5SYu Xiangning
7140f1702c5SYu Xiangning }
7150f1702c5SYu Xiangning return (rts_do_opt_set(connp, level, name, inlen, invalp, outlenp,
7160f1702c5SYu Xiangning outvalp, cr, thisdg_attrs, checkonly));
7170f1702c5SYu Xiangning
7180f1702c5SYu Xiangning }
7190f1702c5SYu Xiangning
7200f1702c5SYu Xiangning /*
7210f1702c5SYu Xiangning * This routine retrieves the current status of socket options.
7220f1702c5SYu Xiangning * It returns the size of the option retrieved.
7230f1702c5SYu Xiangning */
7240f1702c5SYu Xiangning int
rts_tpi_opt_get(queue_t * q,t_scalar_t level,t_scalar_t name,uchar_t * ptr)7250f1702c5SYu Xiangning rts_tpi_opt_get(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
7260f1702c5SYu Xiangning {
7270f1702c5SYu Xiangning rts_t *rts;
7280f1702c5SYu Xiangning int err;
7290f1702c5SYu Xiangning
7300f1702c5SYu Xiangning rts = Q_TO_RTS(q);
7310f1702c5SYu Xiangning rw_enter(&rts->rts_rwlock, RW_READER);
7320f1702c5SYu Xiangning err = rts_opt_get(Q_TO_CONN(q), level, name, ptr);
7330f1702c5SYu Xiangning rw_exit(&rts->rts_rwlock);
7340f1702c5SYu Xiangning return (err);
7350f1702c5SYu Xiangning }
7360f1702c5SYu Xiangning
7370f1702c5SYu Xiangning /*
7380f1702c5SYu Xiangning * This routine sets socket options.
7390f1702c5SYu Xiangning */
7400f1702c5SYu Xiangning /*ARGSUSED*/
7410f1702c5SYu Xiangning int
rts_tpi_opt_set(queue_t * q,uint_t optset_context,int level,int name,uint_t inlen,uchar_t * invalp,uint_t * outlenp,uchar_t * outvalp,void * thisdg_attrs,cred_t * cr)7420f1702c5SYu Xiangning rts_tpi_opt_set(queue_t *q, uint_t optset_context, int level,
7430f1702c5SYu Xiangning int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp,
744bd670b35SErik Nordmark uchar_t *outvalp, void *thisdg_attrs, cred_t *cr)
7450f1702c5SYu Xiangning {
7460f1702c5SYu Xiangning conn_t *connp = Q_TO_CONN(q);
7470f1702c5SYu Xiangning int error;
7480f1702c5SYu Xiangning rts_t *rts = connp->conn_rts;
7490f1702c5SYu Xiangning
7500f1702c5SYu Xiangning
7510f1702c5SYu Xiangning rw_enter(&rts->rts_rwlock, RW_WRITER);
7520f1702c5SYu Xiangning error = rts_opt_set(connp, optset_context, level, name, inlen, invalp,
7530f1702c5SYu Xiangning outlenp, outvalp, thisdg_attrs, cr);
7540f1702c5SYu Xiangning rw_exit(&rts->rts_rwlock);
7550f1702c5SYu Xiangning return (error);
7560f1702c5SYu Xiangning }
7570f1702c5SYu Xiangning
7587c478bd9Sstevel@tonic-gate /*
7597c478bd9Sstevel@tonic-gate * This routine retrieves the value of an ND variable in a rtsparam_t
7607c478bd9Sstevel@tonic-gate * structure. It is called through nd_getset when a user reads the
7617c478bd9Sstevel@tonic-gate * variable.
7627c478bd9Sstevel@tonic-gate */
7637c478bd9Sstevel@tonic-gate /* ARGSUSED */
7647c478bd9Sstevel@tonic-gate static int
rts_param_get(queue_t * q,mblk_t * mp,caddr_t cp,cred_t * cr)7657c478bd9Sstevel@tonic-gate rts_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate rtsparam_t *rtspa = (rtsparam_t *)cp;
7687c478bd9Sstevel@tonic-gate
7697c478bd9Sstevel@tonic-gate (void) mi_mpprintf(mp, "%u", rtspa->rts_param_value);
7707c478bd9Sstevel@tonic-gate return (0);
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate
7737c478bd9Sstevel@tonic-gate /*
7747c478bd9Sstevel@tonic-gate * Walk through the param array specified registering each element with the
7757c478bd9Sstevel@tonic-gate * named dispatch (ND) handler.
7767c478bd9Sstevel@tonic-gate */
7777c478bd9Sstevel@tonic-gate static boolean_t
rts_param_register(IDP * ndp,rtsparam_t * rtspa,int cnt)778f4b3ec61Sdh rts_param_register(IDP *ndp, rtsparam_t *rtspa, int cnt)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate for (; cnt-- > 0; rtspa++) {
7817c478bd9Sstevel@tonic-gate if (rtspa->rts_param_name != NULL && rtspa->rts_param_name[0]) {
782f4b3ec61Sdh if (!nd_load(ndp, rtspa->rts_param_name,
7837c478bd9Sstevel@tonic-gate rts_param_get, rts_param_set, (caddr_t)rtspa)) {
784f4b3ec61Sdh nd_free(ndp);
7857c478bd9Sstevel@tonic-gate return (B_FALSE);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate return (B_TRUE);
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate
7927c478bd9Sstevel@tonic-gate /* This routine sets an ND variable in a rtsparam_t structure. */
7937c478bd9Sstevel@tonic-gate /* ARGSUSED */
7947c478bd9Sstevel@tonic-gate static int
rts_param_set(queue_t * q,mblk_t * mp,char * value,caddr_t cp,cred_t * cr)7957c478bd9Sstevel@tonic-gate rts_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate ulong_t new_value;
7987c478bd9Sstevel@tonic-gate rtsparam_t *rtspa = (rtsparam_t *)cp;
7997c478bd9Sstevel@tonic-gate
8007c478bd9Sstevel@tonic-gate /*
8017c478bd9Sstevel@tonic-gate * Fail the request if the new value does not lie within the
8027c478bd9Sstevel@tonic-gate * required bounds.
8037c478bd9Sstevel@tonic-gate */
8047c478bd9Sstevel@tonic-gate if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
8057c478bd9Sstevel@tonic-gate new_value < rtspa->rts_param_min ||
8067c478bd9Sstevel@tonic-gate new_value > rtspa->rts_param_max) {
8077c478bd9Sstevel@tonic-gate return (EINVAL);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate
8107c478bd9Sstevel@tonic-gate /* Set the new value */
8117c478bd9Sstevel@tonic-gate rtspa->rts_param_value = new_value;
8127c478bd9Sstevel@tonic-gate return (0);
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate
815fc80c0dfSnordmark /*
816fc80c0dfSnordmark * Empty rsrv routine which is used by rts_input to cause a wakeup
817fc80c0dfSnordmark * of a thread in qwait.
818fc80c0dfSnordmark */
819fc80c0dfSnordmark /*ARGSUSED*/
820*8a06b3d6SToomas Soome static int
rts_rsrv(queue_t * q)821fc80c0dfSnordmark rts_rsrv(queue_t *q)
822fc80c0dfSnordmark {
823*8a06b3d6SToomas Soome return (0);
824fc80c0dfSnordmark }
825fc80c0dfSnordmark
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate * This routine handles synchronous messages passed downstream. It either
8287c478bd9Sstevel@tonic-gate * consumes the message or passes it downstream; it never queues a
8297c478bd9Sstevel@tonic-gate * a message. The data messages that go down are wrapped in an IOCTL
8307c478bd9Sstevel@tonic-gate * message.
8317c478bd9Sstevel@tonic-gate *
8327c478bd9Sstevel@tonic-gate * Since it is synchronous, it waits for the M_IOCACK/M_IOCNAK so that
8337c478bd9Sstevel@tonic-gate * it can return an immediate error (such as ENETUNREACH when adding a route).
8347c478bd9Sstevel@tonic-gate * It uses the RTS_WRW_PENDING to ensure that each rts instance has only
8357c478bd9Sstevel@tonic-gate * one M_IOCTL outstanding at any given time.
8367c478bd9Sstevel@tonic-gate */
8377c478bd9Sstevel@tonic-gate static int
rts_wrw(queue_t * q,struiod_t * dp)8387c478bd9Sstevel@tonic-gate rts_wrw(queue_t *q, struiod_t *dp)
8397c478bd9Sstevel@tonic-gate {
8407c478bd9Sstevel@tonic-gate mblk_t *mp = dp->d_mp;
8417c478bd9Sstevel@tonic-gate mblk_t *mp1;
8427c478bd9Sstevel@tonic-gate int error;
8437c478bd9Sstevel@tonic-gate rt_msghdr_t *rtm;
844fc80c0dfSnordmark conn_t *connp = Q_TO_CONN(q);
845fc80c0dfSnordmark rts_t *rts = connp->conn_rts;
8467c478bd9Sstevel@tonic-gate
8477c478bd9Sstevel@tonic-gate while (rts->rts_flag & RTS_WRW_PENDING) {
8487c478bd9Sstevel@tonic-gate if (qwait_rw(q)) {
8497c478bd9Sstevel@tonic-gate rts->rts_error = EINTR;
8507c478bd9Sstevel@tonic-gate goto err_ret;
8517c478bd9Sstevel@tonic-gate }
8520f1702c5SYu Xiangning }
8537c478bd9Sstevel@tonic-gate rts->rts_flag |= RTS_WRW_PENDING;
8547c478bd9Sstevel@tonic-gate
8557c478bd9Sstevel@tonic-gate if (isuioq(q) && (error = struioget(q, mp, dp, 0))) {
8567c478bd9Sstevel@tonic-gate /*
8577c478bd9Sstevel@tonic-gate * Uio error of some sort, so just return the error.
8587c478bd9Sstevel@tonic-gate */
8597c478bd9Sstevel@tonic-gate rts->rts_error = error;
8607c478bd9Sstevel@tonic-gate goto err_ret;
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate * Pass the mblk (chain) onto wput().
8647c478bd9Sstevel@tonic-gate */
8657c478bd9Sstevel@tonic-gate dp->d_mp = 0;
8667c478bd9Sstevel@tonic-gate
8677c478bd9Sstevel@tonic-gate switch (mp->b_datap->db_type) {
8687c478bd9Sstevel@tonic-gate case M_PROTO:
8697c478bd9Sstevel@tonic-gate case M_PCPROTO:
8707c478bd9Sstevel@tonic-gate /* Expedite other than T_DATA_REQ to below the switch */
8717c478bd9Sstevel@tonic-gate if (((mp->b_wptr - mp->b_rptr) !=
8727c478bd9Sstevel@tonic-gate sizeof (struct T_data_req)) ||
8737c478bd9Sstevel@tonic-gate (((union T_primitives *)mp->b_rptr)->type != T_DATA_REQ))
8747c478bd9Sstevel@tonic-gate break;
8757c478bd9Sstevel@tonic-gate if ((mp1 = mp->b_cont) == NULL) {
8767c478bd9Sstevel@tonic-gate rts->rts_error = EINVAL;
877a45f3f93Smeem freemsg(mp);
8787c478bd9Sstevel@tonic-gate goto err_ret;
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate freeb(mp);
8817c478bd9Sstevel@tonic-gate mp = mp1;
8827c478bd9Sstevel@tonic-gate /* FALLTHRU */
8837c478bd9Sstevel@tonic-gate case M_DATA:
8847c478bd9Sstevel@tonic-gate /*
8857c478bd9Sstevel@tonic-gate * The semantics of the routing socket is such that the rtm_pid
8867c478bd9Sstevel@tonic-gate * field is automatically filled in during requests with the
8877c478bd9Sstevel@tonic-gate * current process' pid. We do this here (where we still have
8887c478bd9Sstevel@tonic-gate * user context) after checking we have at least a message the
8897c478bd9Sstevel@tonic-gate * size of a routing message header.
8907c478bd9Sstevel@tonic-gate */
8917c478bd9Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
8927c478bd9Sstevel@tonic-gate if (!pullupmsg(mp, sizeof (rt_msghdr_t))) {
8937c478bd9Sstevel@tonic-gate rts->rts_error = EINVAL;
894a45f3f93Smeem freemsg(mp);
8957c478bd9Sstevel@tonic-gate goto err_ret;
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate rtm = (rt_msghdr_t *)mp->b_rptr;
8997c478bd9Sstevel@tonic-gate rtm->rtm_pid = curproc->p_pid;
9007c478bd9Sstevel@tonic-gate break;
9017c478bd9Sstevel@tonic-gate default:
9027c478bd9Sstevel@tonic-gate break;
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate rts->rts_flag |= RTS_WPUT_PENDING;
9057c478bd9Sstevel@tonic-gate rts_wput(q, mp);
9067c478bd9Sstevel@tonic-gate while (rts->rts_flag & RTS_WPUT_PENDING)
9077c478bd9Sstevel@tonic-gate if (qwait_rw(q)) {
9087c478bd9Sstevel@tonic-gate /* RTS_WPUT_PENDING will be cleared below */
9097c478bd9Sstevel@tonic-gate rts->rts_error = EINTR;
9107c478bd9Sstevel@tonic-gate break;
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate err_ret:
9137c478bd9Sstevel@tonic-gate rts->rts_flag &= ~(RTS_WPUT_PENDING | RTS_WRW_PENDING);
9147c478bd9Sstevel@tonic-gate return (rts->rts_error);
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate
9177c478bd9Sstevel@tonic-gate /*
9187c478bd9Sstevel@tonic-gate * This routine handles all messages passed downstream. It either
9197c478bd9Sstevel@tonic-gate * consumes the message or passes it downstream; it never queues a
9207c478bd9Sstevel@tonic-gate * a message. The data messages that go down are wrapped in an IOCTL
9217c478bd9Sstevel@tonic-gate * message.
9227c478bd9Sstevel@tonic-gate */
923*8a06b3d6SToomas Soome static int
rts_wput(queue_t * q,mblk_t * mp)9247c478bd9Sstevel@tonic-gate rts_wput(queue_t *q, mblk_t *mp)
9257c478bd9Sstevel@tonic-gate {
9267c478bd9Sstevel@tonic-gate uchar_t *rptr = mp->b_rptr;
9277c478bd9Sstevel@tonic-gate mblk_t *mp1;
928fc80c0dfSnordmark conn_t *connp = Q_TO_CONN(q);
929fc80c0dfSnordmark rts_t *rts = connp->conn_rts;
9307c478bd9Sstevel@tonic-gate
9317c478bd9Sstevel@tonic-gate switch (mp->b_datap->db_type) {
9327c478bd9Sstevel@tonic-gate case M_DATA:
9337c478bd9Sstevel@tonic-gate break;
9347c478bd9Sstevel@tonic-gate case M_PROTO:
9357c478bd9Sstevel@tonic-gate case M_PCPROTO:
9367c478bd9Sstevel@tonic-gate if ((mp->b_wptr - rptr) == sizeof (struct T_data_req)) {
9377c478bd9Sstevel@tonic-gate /* Expedite valid T_DATA_REQ to below the switch */
9387c478bd9Sstevel@tonic-gate if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
9397c478bd9Sstevel@tonic-gate mp1 = mp->b_cont;
9407c478bd9Sstevel@tonic-gate freeb(mp);
9417c478bd9Sstevel@tonic-gate if (mp1 == NULL)
942*8a06b3d6SToomas Soome return (0);
9437c478bd9Sstevel@tonic-gate mp = mp1;
9447c478bd9Sstevel@tonic-gate break;
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate /* FALLTHRU */
9487c478bd9Sstevel@tonic-gate default:
9497c478bd9Sstevel@tonic-gate rts_wput_other(q, mp);
950*8a06b3d6SToomas Soome return (0);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate
9537c478bd9Sstevel@tonic-gate
954de8c4a14SErik Nordmark ASSERT(msg_getcred(mp, NULL) != NULL);
955de8c4a14SErik Nordmark
956de8c4a14SErik Nordmark mp1 = rts_ioctl_alloc(mp);
9577c478bd9Sstevel@tonic-gate if (mp1 == NULL) {
9587c478bd9Sstevel@tonic-gate ASSERT(rts != NULL);
9597c478bd9Sstevel@tonic-gate freemsg(mp);
9607c478bd9Sstevel@tonic-gate if (rts->rts_flag & RTS_WPUT_PENDING) {
9617c478bd9Sstevel@tonic-gate rts->rts_error = ENOMEM;
9627c478bd9Sstevel@tonic-gate rts->rts_flag &= ~RTS_WPUT_PENDING;
9637c478bd9Sstevel@tonic-gate }
964*8a06b3d6SToomas Soome return (0);
9657c478bd9Sstevel@tonic-gate }
966bd670b35SErik Nordmark ip_wput_nondata(q, mp1);
967*8a06b3d6SToomas Soome return (0);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate
9707c478bd9Sstevel@tonic-gate
9717c478bd9Sstevel@tonic-gate /*
9727c478bd9Sstevel@tonic-gate * Handles all the control message, if it
9737c478bd9Sstevel@tonic-gate * can not understand it, it will
9747c478bd9Sstevel@tonic-gate * pass down stream.
9757c478bd9Sstevel@tonic-gate */
9767c478bd9Sstevel@tonic-gate static void
rts_wput_other(queue_t * q,mblk_t * mp)9777c478bd9Sstevel@tonic-gate rts_wput_other(queue_t *q, mblk_t *mp)
9787c478bd9Sstevel@tonic-gate {
979fc80c0dfSnordmark conn_t *connp = Q_TO_CONN(q);
980fc80c0dfSnordmark rts_t *rts = connp->conn_rts;
9817c478bd9Sstevel@tonic-gate uchar_t *rptr = mp->b_rptr;
9827c478bd9Sstevel@tonic-gate struct iocblk *iocp;
9837c478bd9Sstevel@tonic-gate cred_t *cr;
984f4b3ec61Sdh rts_stack_t *rtss;
9857c478bd9Sstevel@tonic-gate
986f4b3ec61Sdh rtss = rts->rts_rtss;
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate switch (mp->b_datap->db_type) {
9897c478bd9Sstevel@tonic-gate case M_PROTO:
9907c478bd9Sstevel@tonic-gate case M_PCPROTO:
9917c478bd9Sstevel@tonic-gate if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) {
9927c478bd9Sstevel@tonic-gate /*
9937c478bd9Sstevel@tonic-gate * If the message does not contain a PRIM_type,
9947c478bd9Sstevel@tonic-gate * throw it away.
9957c478bd9Sstevel@tonic-gate */
9967c478bd9Sstevel@tonic-gate freemsg(mp);
9977c478bd9Sstevel@tonic-gate return;
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate switch (((union T_primitives *)rptr)->type) {
10007c478bd9Sstevel@tonic-gate case T_BIND_REQ:
10017c478bd9Sstevel@tonic-gate case O_T_BIND_REQ:
10020f1702c5SYu Xiangning rts_tpi_bind(q, mp);
10037c478bd9Sstevel@tonic-gate return;
10047c478bd9Sstevel@tonic-gate case T_UNBIND_REQ:
10050f1702c5SYu Xiangning rts_tpi_unbind(q, mp);
10067c478bd9Sstevel@tonic-gate return;
10077c478bd9Sstevel@tonic-gate case T_CAPABILITY_REQ:
10087c478bd9Sstevel@tonic-gate rts_capability_req(q, mp);
10097c478bd9Sstevel@tonic-gate return;
10107c478bd9Sstevel@tonic-gate case T_INFO_REQ:
10117c478bd9Sstevel@tonic-gate rts_info_req(q, mp);
10127c478bd9Sstevel@tonic-gate return;
10137c478bd9Sstevel@tonic-gate case T_SVR4_OPTMGMT_REQ:
10147c478bd9Sstevel@tonic-gate case T_OPTMGMT_REQ:
1015de8c4a14SErik Nordmark /*
1016de8c4a14SErik Nordmark * All Solaris components should pass a db_credp
1017de8c4a14SErik Nordmark * for this TPI message, hence we ASSERT.
1018de8c4a14SErik Nordmark * But in case there is some other M_PROTO that looks
1019de8c4a14SErik Nordmark * like a TPI message sent by some other kernel
1020de8c4a14SErik Nordmark * component, we check and return an error.
1021de8c4a14SErik Nordmark */
1022de8c4a14SErik Nordmark cr = msg_getcred(mp, NULL);
1023de8c4a14SErik Nordmark ASSERT(cr != NULL);
1024de8c4a14SErik Nordmark if (cr == NULL) {
1025de8c4a14SErik Nordmark rts_err_ack(q, mp, TSYSERR, EINVAL);
1026de8c4a14SErik Nordmark return;
1027de8c4a14SErik Nordmark }
1028de8c4a14SErik Nordmark if (((union T_primitives *)rptr)->type ==
1029de8c4a14SErik Nordmark T_SVR4_OPTMGMT_REQ) {
1030bd670b35SErik Nordmark svr4_optcom_req(q, mp, cr, &rts_opt_obj);
1031de8c4a14SErik Nordmark } else {
1032bd670b35SErik Nordmark tpi_optcom_req(q, mp, cr, &rts_opt_obj);
1033de8c4a14SErik Nordmark }
10347c478bd9Sstevel@tonic-gate return;
10357c478bd9Sstevel@tonic-gate case O_T_CONN_RES:
10367c478bd9Sstevel@tonic-gate case T_CONN_RES:
10377c478bd9Sstevel@tonic-gate case T_DISCON_REQ:
10387c478bd9Sstevel@tonic-gate /* Not supported by rts. */
10397c478bd9Sstevel@tonic-gate rts_err_ack(q, mp, TNOTSUPPORT, 0);
10407c478bd9Sstevel@tonic-gate return;
10417c478bd9Sstevel@tonic-gate case T_DATA_REQ:
10427c478bd9Sstevel@tonic-gate case T_EXDATA_REQ:
10437c478bd9Sstevel@tonic-gate case T_ORDREL_REQ:
10447c478bd9Sstevel@tonic-gate /* Illegal for rts. */
10457c478bd9Sstevel@tonic-gate freemsg(mp);
10467c478bd9Sstevel@tonic-gate (void) putnextctl1(RD(q), M_ERROR, EPROTO);
10477c478bd9Sstevel@tonic-gate return;
10480f1702c5SYu Xiangning
10497c478bd9Sstevel@tonic-gate default:
10507c478bd9Sstevel@tonic-gate break;
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate break;
10537c478bd9Sstevel@tonic-gate case M_IOCTL:
10547c478bd9Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr;
10557c478bd9Sstevel@tonic-gate switch (iocp->ioc_cmd) {
10567c478bd9Sstevel@tonic-gate case ND_SET:
10577c478bd9Sstevel@tonic-gate case ND_GET:
1058f4b3ec61Sdh if (nd_getset(q, rtss->rtss_g_nd, mp)) {
10597c478bd9Sstevel@tonic-gate qreply(q, mp);
10607c478bd9Sstevel@tonic-gate return;
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate break;
10637c478bd9Sstevel@tonic-gate case TI_GETPEERNAME:
10647c478bd9Sstevel@tonic-gate mi_copyin(q, mp, NULL,
10657c478bd9Sstevel@tonic-gate SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
10667c478bd9Sstevel@tonic-gate return;
10677c478bd9Sstevel@tonic-gate default:
10687c478bd9Sstevel@tonic-gate break;
10697c478bd9Sstevel@tonic-gate }
1070a4ea78eaSToomas Soome break;
10717c478bd9Sstevel@tonic-gate case M_IOCDATA:
10727c478bd9Sstevel@tonic-gate rts_wput_iocdata(q, mp);
10737c478bd9Sstevel@tonic-gate return;
10747c478bd9Sstevel@tonic-gate default:
10757c478bd9Sstevel@tonic-gate break;
10767c478bd9Sstevel@tonic-gate }
1077bd670b35SErik Nordmark ip_wput_nondata(q, mp);
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate
10807c478bd9Sstevel@tonic-gate /*
10817c478bd9Sstevel@tonic-gate * Called by rts_wput_other to handle all M_IOCDATA messages.
10827c478bd9Sstevel@tonic-gate */
10837c478bd9Sstevel@tonic-gate static void
rts_wput_iocdata(queue_t * q,mblk_t * mp)10847c478bd9Sstevel@tonic-gate rts_wput_iocdata(queue_t *q, mblk_t *mp)
10857c478bd9Sstevel@tonic-gate {
10867c478bd9Sstevel@tonic-gate struct sockaddr *rtsaddr;
10877c478bd9Sstevel@tonic-gate mblk_t *mp1;
10887c478bd9Sstevel@tonic-gate STRUCT_HANDLE(strbuf, sb);
10897c478bd9Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
10907c478bd9Sstevel@tonic-gate
10917c478bd9Sstevel@tonic-gate /* Make sure it is one of ours. */
10927c478bd9Sstevel@tonic-gate switch (iocp->ioc_cmd) {
10937c478bd9Sstevel@tonic-gate case TI_GETPEERNAME:
10947c478bd9Sstevel@tonic-gate break;
10957c478bd9Sstevel@tonic-gate default:
1096bd670b35SErik Nordmark ip_wput_nondata(q, mp);
10977c478bd9Sstevel@tonic-gate return;
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate switch (mi_copy_state(q, mp, &mp1)) {
11007c478bd9Sstevel@tonic-gate case -1:
11017c478bd9Sstevel@tonic-gate return;
11027c478bd9Sstevel@tonic-gate case MI_COPY_CASE(MI_COPY_IN, 1):
11037c478bd9Sstevel@tonic-gate break;
11047c478bd9Sstevel@tonic-gate case MI_COPY_CASE(MI_COPY_OUT, 1):
11057c478bd9Sstevel@tonic-gate /* Copy out the strbuf. */
11067c478bd9Sstevel@tonic-gate mi_copyout(q, mp);
11077c478bd9Sstevel@tonic-gate return;
11087c478bd9Sstevel@tonic-gate case MI_COPY_CASE(MI_COPY_OUT, 2):
11097c478bd9Sstevel@tonic-gate /* All done. */
11107c478bd9Sstevel@tonic-gate mi_copy_done(q, mp, 0);
11117c478bd9Sstevel@tonic-gate return;
11127c478bd9Sstevel@tonic-gate default:
11137c478bd9Sstevel@tonic-gate mi_copy_done(q, mp, EPROTO);
11147c478bd9Sstevel@tonic-gate return;
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr);
11177c478bd9Sstevel@tonic-gate if (STRUCT_FGET(sb, maxlen) < (int)sizeof (sin_t)) {
11187c478bd9Sstevel@tonic-gate mi_copy_done(q, mp, EINVAL);
11197c478bd9Sstevel@tonic-gate return;
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate switch (iocp->ioc_cmd) {
11227c478bd9Sstevel@tonic-gate case TI_GETPEERNAME:
11237c478bd9Sstevel@tonic-gate break;
11247c478bd9Sstevel@tonic-gate default:
11257c478bd9Sstevel@tonic-gate mi_copy_done(q, mp, EPROTO);
11267c478bd9Sstevel@tonic-gate return;
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), sizeof (sin_t),
11297c478bd9Sstevel@tonic-gate B_TRUE);
11307c478bd9Sstevel@tonic-gate if (mp1 == NULL)
11317c478bd9Sstevel@tonic-gate return;
11327c478bd9Sstevel@tonic-gate STRUCT_FSET(sb, len, (int)sizeof (sin_t));
11337c478bd9Sstevel@tonic-gate rtsaddr = (struct sockaddr *)mp1->b_rptr;
11347c478bd9Sstevel@tonic-gate mp1->b_wptr = (uchar_t *)&rtsaddr[1];
11357c478bd9Sstevel@tonic-gate bzero(rtsaddr, sizeof (struct sockaddr));
11367c478bd9Sstevel@tonic-gate rtsaddr->sa_family = AF_ROUTE;
11377c478bd9Sstevel@tonic-gate /* Copy out the address */
11387c478bd9Sstevel@tonic-gate mi_copyout(q, mp);
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate
1141bd670b35SErik Nordmark /*
1142bd670b35SErik Nordmark * IP passes up a NULL ira.
1143bd670b35SErik Nordmark */
1144fc80c0dfSnordmark /*ARGSUSED2*/
11457c478bd9Sstevel@tonic-gate static void
rts_input(void * arg1,mblk_t * mp,void * arg2,ip_recv_attr_t * ira)1146bd670b35SErik Nordmark rts_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
11477c478bd9Sstevel@tonic-gate {
1148fc80c0dfSnordmark conn_t *connp = (conn_t *)arg1;
1149fc80c0dfSnordmark rts_t *rts = connp->conn_rts;
11507c478bd9Sstevel@tonic-gate struct iocblk *iocp;
11517c478bd9Sstevel@tonic-gate mblk_t *mp1;
11527c478bd9Sstevel@tonic-gate struct T_data_ind *tdi;
11530f1702c5SYu Xiangning int error;
11547c478bd9Sstevel@tonic-gate
11557c478bd9Sstevel@tonic-gate switch (mp->b_datap->db_type) {
11567c478bd9Sstevel@tonic-gate case M_IOCACK:
11577c478bd9Sstevel@tonic-gate case M_IOCNAK:
11587c478bd9Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr;
1159bd670b35SErik Nordmark ASSERT(!IPCL_IS_NONSTR(connp));
1160bd670b35SErik Nordmark if (rts->rts_flag & (RTS_WPUT_PENDING)) {
1161bd670b35SErik Nordmark rts->rts_flag &= ~RTS_WPUT_PENDING;
11627c478bd9Sstevel@tonic-gate rts->rts_error = iocp->ioc_error;
1163bd670b35SErik Nordmark /*
1164bd670b35SErik Nordmark * Tell rts_wvw/qwait that we are done.
1165bd670b35SErik Nordmark * Note: there is no qwait_wakeup() we can use.
1166bd670b35SErik Nordmark */
1167bd670b35SErik Nordmark qenable(connp->conn_rq);
11687c478bd9Sstevel@tonic-gate freemsg(mp);
11697c478bd9Sstevel@tonic-gate return;
11707c478bd9Sstevel@tonic-gate }
11717c478bd9Sstevel@tonic-gate break;
11727c478bd9Sstevel@tonic-gate case M_DATA:
11737c478bd9Sstevel@tonic-gate /*
11747c478bd9Sstevel@tonic-gate * Prepend T_DATA_IND to prevent the stream head from
11757c478bd9Sstevel@tonic-gate * consolidating multiple messages together.
11767c478bd9Sstevel@tonic-gate * If the allocation fails just send up the M_DATA.
11777c478bd9Sstevel@tonic-gate */
11787c478bd9Sstevel@tonic-gate mp1 = allocb(sizeof (*tdi), BPRI_MED);
11797c478bd9Sstevel@tonic-gate if (mp1 != NULL) {
11807c478bd9Sstevel@tonic-gate mp1->b_cont = mp;
11817c478bd9Sstevel@tonic-gate mp = mp1;
11827c478bd9Sstevel@tonic-gate
11837c478bd9Sstevel@tonic-gate mp->b_datap->db_type = M_PROTO;
11847c478bd9Sstevel@tonic-gate mp->b_wptr += sizeof (*tdi);
11857c478bd9Sstevel@tonic-gate tdi = (struct T_data_ind *)mp->b_rptr;
11867c478bd9Sstevel@tonic-gate tdi->PRIM_type = T_DATA_IND;
11877c478bd9Sstevel@tonic-gate tdi->MORE_flag = 0;
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate break;
11907c478bd9Sstevel@tonic-gate default:
11917c478bd9Sstevel@tonic-gate break;
11927c478bd9Sstevel@tonic-gate }
11930f1702c5SYu Xiangning
11940f1702c5SYu Xiangning if (IPCL_IS_NONSTR(connp)) {
11950f1702c5SYu Xiangning if ((*connp->conn_upcalls->su_recv)
11960f1702c5SYu Xiangning (connp->conn_upper_handle, mp, msgdsize(mp), 0,
11970f1702c5SYu Xiangning &error, NULL) < 0) {
11980f1702c5SYu Xiangning ASSERT(error == ENOSPC);
11990f1702c5SYu Xiangning /*
12000f1702c5SYu Xiangning * Let's confirm hoding the lock that
12010f1702c5SYu Xiangning * we are out of recv space.
12020f1702c5SYu Xiangning */
12030f1702c5SYu Xiangning mutex_enter(&rts->rts_recv_mutex);
12040f1702c5SYu Xiangning if ((*connp->conn_upcalls->su_recv)
12050f1702c5SYu Xiangning (connp->conn_upper_handle, NULL, 0, 0,
12060f1702c5SYu Xiangning &error, NULL) < 0) {
12070f1702c5SYu Xiangning ASSERT(error == ENOSPC);
12080f1702c5SYu Xiangning connp->conn_flow_cntrld = B_TRUE;
12090f1702c5SYu Xiangning }
12100f1702c5SYu Xiangning mutex_exit(&rts->rts_recv_mutex);
12110f1702c5SYu Xiangning }
12120f1702c5SYu Xiangning } else {
12130f1702c5SYu Xiangning putnext(connp->conn_rq, mp);
12140f1702c5SYu Xiangning }
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate
1217bd670b35SErik Nordmark /*ARGSUSED*/
1218bd670b35SErik Nordmark static void
rts_icmp_input(void * arg1,mblk_t * mp,void * arg2,ip_recv_attr_t * ira)1219bd670b35SErik Nordmark rts_icmp_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
1220bd670b35SErik Nordmark {
1221bd670b35SErik Nordmark freemsg(mp);
1222bd670b35SErik Nordmark }
12237c478bd9Sstevel@tonic-gate
12247c478bd9Sstevel@tonic-gate void
rts_ddi_g_init(void)12250f1702c5SYu Xiangning rts_ddi_g_init(void)
12267c478bd9Sstevel@tonic-gate {
12277c478bd9Sstevel@tonic-gate rts_max_optsize = optcom_max_optsize(rts_opt_obj.odb_opt_des_arr,
12287c478bd9Sstevel@tonic-gate rts_opt_obj.odb_opt_arr_cnt);
1229f4b3ec61Sdh
1230f4b3ec61Sdh /*
1231f4b3ec61Sdh * We want to be informed each time a stack is created or
1232f4b3ec61Sdh * destroyed in the kernel, so we can maintain the
1233f4b3ec61Sdh * set of rts_stack_t's.
1234f4b3ec61Sdh */
1235f4b3ec61Sdh netstack_register(NS_RTS, rts_stack_init, NULL, rts_stack_fini);
1236f4b3ec61Sdh }
1237f4b3ec61Sdh
1238f4b3ec61Sdh void
rts_ddi_g_destroy(void)12390f1702c5SYu Xiangning rts_ddi_g_destroy(void)
1240f4b3ec61Sdh {
1241f4b3ec61Sdh netstack_unregister(NS_RTS);
1242f4b3ec61Sdh }
1243f4b3ec61Sdh
12440f1702c5SYu Xiangning #define INET_NAME "ip"
12450f1702c5SYu Xiangning
1246f4b3ec61Sdh /*
1247f4b3ec61Sdh * Initialize the RTS stack instance.
1248f4b3ec61Sdh */
1249f4b3ec61Sdh /* ARGSUSED */
1250f4b3ec61Sdh static void *
rts_stack_init(netstackid_t stackid,netstack_t * ns)1251f4b3ec61Sdh rts_stack_init(netstackid_t stackid, netstack_t *ns)
1252f4b3ec61Sdh {
1253f4b3ec61Sdh rts_stack_t *rtss;
1254f4b3ec61Sdh rtsparam_t *pa;
12550f1702c5SYu Xiangning int error = 0;
12560f1702c5SYu Xiangning major_t major;
1257f4b3ec61Sdh
1258f4b3ec61Sdh rtss = (rts_stack_t *)kmem_zalloc(sizeof (*rtss), KM_SLEEP);
1259f4b3ec61Sdh rtss->rtss_netstack = ns;
1260f4b3ec61Sdh
1261f4b3ec61Sdh pa = (rtsparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
1262f4b3ec61Sdh rtss->rtss_params = pa;
1263f4b3ec61Sdh bcopy(lcl_param_arr, rtss->rtss_params, sizeof (lcl_param_arr));
1264f4b3ec61Sdh
1265f4b3ec61Sdh (void) rts_param_register(&rtss->rtss_g_nd,
1266f4b3ec61Sdh rtss->rtss_params, A_CNT(lcl_param_arr));
12670f1702c5SYu Xiangning
12680f1702c5SYu Xiangning major = mod_name_to_major(INET_NAME);
12690f1702c5SYu Xiangning error = ldi_ident_from_major(major, &rtss->rtss_ldi_ident);
12700f1702c5SYu Xiangning ASSERT(error == 0);
1271f4b3ec61Sdh return (rtss);
1272f4b3ec61Sdh }
1273f4b3ec61Sdh
1274f4b3ec61Sdh /*
1275f4b3ec61Sdh * Free the RTS stack instance.
1276f4b3ec61Sdh */
1277f4b3ec61Sdh /* ARGSUSED */
1278f4b3ec61Sdh static void
rts_stack_fini(netstackid_t stackid,void * arg)1279f4b3ec61Sdh rts_stack_fini(netstackid_t stackid, void *arg)
1280f4b3ec61Sdh {
1281f4b3ec61Sdh rts_stack_t *rtss = (rts_stack_t *)arg;
1282f4b3ec61Sdh
1283fc80c0dfSnordmark nd_free(&rtss->rtss_g_nd);
1284f4b3ec61Sdh kmem_free(rtss->rtss_params, sizeof (lcl_param_arr));
1285f4b3ec61Sdh rtss->rtss_params = NULL;
12860f1702c5SYu Xiangning ldi_ident_release(rtss->rtss_ldi_ident);
1287f4b3ec61Sdh kmem_free(rtss, sizeof (*rtss));
12887c478bd9Sstevel@tonic-gate }
12890f1702c5SYu Xiangning
12900f1702c5SYu Xiangning /* ARGSUSED */
12910f1702c5SYu Xiangning int
rts_accept(sock_lower_handle_t lproto_handle,sock_lower_handle_t eproto_handle,sock_upper_handle_t sock_handle,cred_t * cr)12920f1702c5SYu Xiangning rts_accept(sock_lower_handle_t lproto_handle,
12930f1702c5SYu Xiangning sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle,
12940f1702c5SYu Xiangning cred_t *cr)
12950f1702c5SYu Xiangning {
12960f1702c5SYu Xiangning return (EINVAL);
12970f1702c5SYu Xiangning }
12980f1702c5SYu Xiangning
12990f1702c5SYu Xiangning /* ARGSUSED */
13000f1702c5SYu Xiangning static int
rts_bind(sock_lower_handle_t proto_handle,struct sockaddr * sa,socklen_t len,cred_t * cr)13010f1702c5SYu Xiangning rts_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa,
13020f1702c5SYu Xiangning socklen_t len, cred_t *cr)
13030f1702c5SYu Xiangning {
13040f1702c5SYu Xiangning /*
13050f1702c5SYu Xiangning * rebind not allowed
13060f1702c5SYu Xiangning */
13070f1702c5SYu Xiangning return (EINVAL);
13080f1702c5SYu Xiangning }
13090f1702c5SYu Xiangning
13100f1702c5SYu Xiangning /* ARGSUSED */
13110f1702c5SYu Xiangning int
rts_listen(sock_lower_handle_t proto_handle,int backlog,cred_t * cr)13120f1702c5SYu Xiangning rts_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr)
13130f1702c5SYu Xiangning {
13140f1702c5SYu Xiangning return (EINVAL);
13150f1702c5SYu Xiangning }
13160f1702c5SYu Xiangning
13170f1702c5SYu Xiangning /* ARGSUSED */
13180f1702c5SYu Xiangning int
rts_connect(sock_lower_handle_t proto_handle,const struct sockaddr * sa,socklen_t len,sock_connid_t * id,cred_t * cr)13190f1702c5SYu Xiangning rts_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa,
13200f1702c5SYu Xiangning socklen_t len, sock_connid_t *id, cred_t *cr)
13210f1702c5SYu Xiangning {
13220f1702c5SYu Xiangning /*
13230f1702c5SYu Xiangning * rts sockets start out as bound and connected
13240f1702c5SYu Xiangning */
13250f1702c5SYu Xiangning *id = 0;
13260f1702c5SYu Xiangning return (EISCONN);
13270f1702c5SYu Xiangning }
13280f1702c5SYu Xiangning
13290f1702c5SYu Xiangning /* ARGSUSED */
13300f1702c5SYu Xiangning int
rts_getpeername(sock_lower_handle_t proto_handle,struct sockaddr * addr,socklen_t * addrlen,cred_t * cr)13310f1702c5SYu Xiangning rts_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr,
13320f1702c5SYu Xiangning socklen_t *addrlen, cred_t *cr)
13330f1702c5SYu Xiangning {
13340f1702c5SYu Xiangning bzero(addr, sizeof (struct sockaddr));
13350f1702c5SYu Xiangning addr->sa_family = AF_ROUTE;
13360f1702c5SYu Xiangning *addrlen = sizeof (struct sockaddr);
13370f1702c5SYu Xiangning
13380f1702c5SYu Xiangning return (0);
13390f1702c5SYu Xiangning }
13400f1702c5SYu Xiangning
13410f1702c5SYu Xiangning /* ARGSUSED */
13420f1702c5SYu Xiangning int
rts_getsockname(sock_lower_handle_t proto_handle,struct sockaddr * addr,socklen_t * addrlen,cred_t * cr)13430f1702c5SYu Xiangning rts_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr,
13440f1702c5SYu Xiangning socklen_t *addrlen, cred_t *cr)
13450f1702c5SYu Xiangning {
1346bd670b35SErik Nordmark bzero(addr, sizeof (struct sockaddr));
1347bd670b35SErik Nordmark addr->sa_family = AF_ROUTE;
1348bd670b35SErik Nordmark *addrlen = sizeof (struct sockaddr);
1349bd670b35SErik Nordmark
1350bd670b35SErik Nordmark return (0);
13510f1702c5SYu Xiangning }
13520f1702c5SYu Xiangning
13530f1702c5SYu Xiangning static int
rts_getsockopt(sock_lower_handle_t proto_handle,int level,int option_name,void * optvalp,socklen_t * optlen,cred_t * cr)13540f1702c5SYu Xiangning rts_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
13550f1702c5SYu Xiangning void *optvalp, socklen_t *optlen, cred_t *cr)
13560f1702c5SYu Xiangning {
1357a4ea78eaSToomas Soome conn_t *connp = (conn_t *)proto_handle;
13580f1702c5SYu Xiangning rts_t *rts = connp->conn_rts;
13590f1702c5SYu Xiangning int error;
13600f1702c5SYu Xiangning t_uscalar_t max_optbuf_len;
13610f1702c5SYu Xiangning void *optvalp_buf;
13620f1702c5SYu Xiangning int len;
13630f1702c5SYu Xiangning
13640f1702c5SYu Xiangning error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len,
13650f1702c5SYu Xiangning rts_opt_obj.odb_opt_des_arr,
13660f1702c5SYu Xiangning rts_opt_obj.odb_opt_arr_cnt,
13670f1702c5SYu Xiangning B_FALSE, B_TRUE, cr);
13680f1702c5SYu Xiangning if (error != 0) {
13690f1702c5SYu Xiangning if (error < 0)
13700f1702c5SYu Xiangning error = proto_tlitosyserr(-error);
13710f1702c5SYu Xiangning return (error);
13720f1702c5SYu Xiangning }
13730f1702c5SYu Xiangning
13740f1702c5SYu Xiangning optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP);
13750f1702c5SYu Xiangning rw_enter(&rts->rts_rwlock, RW_READER);
13760f1702c5SYu Xiangning len = rts_opt_get(connp, level, option_name, optvalp_buf);
13770f1702c5SYu Xiangning rw_exit(&rts->rts_rwlock);
1378bd670b35SErik Nordmark if (len == -1) {
1379bd670b35SErik Nordmark kmem_free(optvalp_buf, max_optbuf_len);
1380bd670b35SErik Nordmark return (EINVAL);
13810f1702c5SYu Xiangning }
13820f1702c5SYu Xiangning
1383bd670b35SErik Nordmark /*
1384bd670b35SErik Nordmark * update optlen and copy option value
1385bd670b35SErik Nordmark */
1386bd670b35SErik Nordmark t_uscalar_t size = MIN(len, *optlen);
1387bd670b35SErik Nordmark
1388bd670b35SErik Nordmark bcopy(optvalp_buf, optvalp, size);
1389bd670b35SErik Nordmark bcopy(&size, optlen, sizeof (size));
13900f1702c5SYu Xiangning kmem_free(optvalp_buf, max_optbuf_len);
1391bd670b35SErik Nordmark return (0);
13920f1702c5SYu Xiangning }
13930f1702c5SYu Xiangning
13940f1702c5SYu Xiangning static int
rts_setsockopt(sock_lower_handle_t proto_handle,int level,int option_name,const void * optvalp,socklen_t optlen,cred_t * cr)13950f1702c5SYu Xiangning rts_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
13960f1702c5SYu Xiangning const void *optvalp, socklen_t optlen, cred_t *cr)
13970f1702c5SYu Xiangning {
13980f1702c5SYu Xiangning conn_t *connp = (conn_t *)proto_handle;
13990f1702c5SYu Xiangning rts_t *rts = connp->conn_rts;
14000f1702c5SYu Xiangning int error;
14010f1702c5SYu Xiangning
14020f1702c5SYu Xiangning error = proto_opt_check(level, option_name, optlen, NULL,
14030f1702c5SYu Xiangning rts_opt_obj.odb_opt_des_arr,
14040f1702c5SYu Xiangning rts_opt_obj.odb_opt_arr_cnt,
14050f1702c5SYu Xiangning B_TRUE, B_FALSE, cr);
14060f1702c5SYu Xiangning
14070f1702c5SYu Xiangning if (error != 0) {
14080f1702c5SYu Xiangning if (error < 0)
14090f1702c5SYu Xiangning error = proto_tlitosyserr(-error);
14100f1702c5SYu Xiangning return (error);
14110f1702c5SYu Xiangning }
14120f1702c5SYu Xiangning
14130f1702c5SYu Xiangning rw_enter(&rts->rts_rwlock, RW_WRITER);
14140f1702c5SYu Xiangning error = rts_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name,
14150f1702c5SYu Xiangning optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp,
14160f1702c5SYu Xiangning NULL, cr);
14170f1702c5SYu Xiangning rw_exit(&rts->rts_rwlock);
14180f1702c5SYu Xiangning
14190f1702c5SYu Xiangning ASSERT(error >= 0);
14200f1702c5SYu Xiangning
14210f1702c5SYu Xiangning return (error);
14220f1702c5SYu Xiangning }
14230f1702c5SYu Xiangning
14240f1702c5SYu Xiangning /* ARGSUSED */
14250f1702c5SYu Xiangning static int
rts_send(sock_lower_handle_t proto_handle,mblk_t * mp,struct nmsghdr * msg,cred_t * cr)14260f1702c5SYu Xiangning rts_send(sock_lower_handle_t proto_handle, mblk_t *mp,
14270f1702c5SYu Xiangning struct nmsghdr *msg, cred_t *cr)
14280f1702c5SYu Xiangning {
14290f1702c5SYu Xiangning conn_t *connp = (conn_t *)proto_handle;
14300f1702c5SYu Xiangning rt_msghdr_t *rtm;
14310f1702c5SYu Xiangning int error;
14320f1702c5SYu Xiangning
14330f1702c5SYu Xiangning ASSERT(DB_TYPE(mp) == M_DATA);
14340f1702c5SYu Xiangning /*
14350f1702c5SYu Xiangning * The semantics of the routing socket is such that the rtm_pid
14360f1702c5SYu Xiangning * field is automatically filled in during requests with the
14370f1702c5SYu Xiangning * current process' pid. We do this here (where we still have
14380f1702c5SYu Xiangning * user context) after checking we have at least a message the
14390f1702c5SYu Xiangning * size of a routing message header.
14400f1702c5SYu Xiangning */
14410f1702c5SYu Xiangning if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
14420f1702c5SYu Xiangning if (!pullupmsg(mp, sizeof (rt_msghdr_t))) {
14430f1702c5SYu Xiangning freemsg(mp);
1444bd670b35SErik Nordmark return (EINVAL);
14450f1702c5SYu Xiangning }
14460f1702c5SYu Xiangning }
14470f1702c5SYu Xiangning rtm = (rt_msghdr_t *)mp->b_rptr;
14480f1702c5SYu Xiangning rtm->rtm_pid = curproc->p_pid;
14490f1702c5SYu Xiangning
14500f1702c5SYu Xiangning /*
1451bd670b35SErik Nordmark * We are not constrained by the ioctl interface and
1452bd670b35SErik Nordmark * ip_rts_request_common processing requests synchronously hence
1453bd670b35SErik Nordmark * we can send them down concurrently.
14540f1702c5SYu Xiangning */
1455bd670b35SErik Nordmark error = ip_rts_request_common(mp, connp, cr);
14560f1702c5SYu Xiangning return (error);
14570f1702c5SYu Xiangning }
14580f1702c5SYu Xiangning
14590f1702c5SYu Xiangning /* ARGSUSED */
14600f1702c5SYu Xiangning sock_lower_handle_t
rts_create(int family,int type,int proto,sock_downcalls_t ** sock_downcalls,uint_t * smodep,int * errorp,int flags,cred_t * credp)14610f1702c5SYu Xiangning rts_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls,
14620f1702c5SYu Xiangning uint_t *smodep, int *errorp, int flags, cred_t *credp)
14630f1702c5SYu Xiangning {
14640f1702c5SYu Xiangning conn_t *connp;
14650f1702c5SYu Xiangning
14660f1702c5SYu Xiangning if (family != AF_ROUTE || type != SOCK_RAW ||
14670f1702c5SYu Xiangning (proto != 0 && proto != AF_INET && proto != AF_INET6)) {
14680f1702c5SYu Xiangning *errorp = EPROTONOSUPPORT;
14690f1702c5SYu Xiangning return (NULL);
14700f1702c5SYu Xiangning }
14710f1702c5SYu Xiangning
14720f1702c5SYu Xiangning connp = rts_open(flags, credp);
14730f1702c5SYu Xiangning ASSERT(connp != NULL);
14740f1702c5SYu Xiangning connp->conn_flags |= IPCL_NONSTR;
14750f1702c5SYu Xiangning
1476bd670b35SErik Nordmark connp->conn_proto = proto;
14770f1702c5SYu Xiangning
14780f1702c5SYu Xiangning mutex_enter(&connp->conn_lock);
14790f1702c5SYu Xiangning connp->conn_state_flags &= ~CONN_INCIPIENT;
14800f1702c5SYu Xiangning mutex_exit(&connp->conn_lock);
14810f1702c5SYu Xiangning
14820f1702c5SYu Xiangning *errorp = 0;
14830f1702c5SYu Xiangning *smodep = SM_ATOMIC;
14840f1702c5SYu Xiangning *sock_downcalls = &sock_rts_downcalls;
14850f1702c5SYu Xiangning return ((sock_lower_handle_t)connp);
14860f1702c5SYu Xiangning }
14870f1702c5SYu Xiangning
14880f1702c5SYu Xiangning /* ARGSUSED */
14890f1702c5SYu Xiangning void
rts_activate(sock_lower_handle_t proto_handle,sock_upper_handle_t sock_handle,sock_upcalls_t * sock_upcalls,int flags,cred_t * cr)14900f1702c5SYu Xiangning rts_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle,
14910f1702c5SYu Xiangning sock_upcalls_t *sock_upcalls, int flags, cred_t *cr)
14920f1702c5SYu Xiangning {
14930f1702c5SYu Xiangning conn_t *connp = (conn_t *)proto_handle;
14940f1702c5SYu Xiangning struct sock_proto_props sopp;
14950f1702c5SYu Xiangning
14960f1702c5SYu Xiangning connp->conn_upcalls = sock_upcalls;
14970f1702c5SYu Xiangning connp->conn_upper_handle = sock_handle;
14980f1702c5SYu Xiangning
14990f1702c5SYu Xiangning sopp.sopp_flags = SOCKOPT_WROFF | SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT |
15000f1702c5SYu Xiangning SOCKOPT_MAXBLK | SOCKOPT_MAXPSZ | SOCKOPT_MINPSZ;
15010f1702c5SYu Xiangning sopp.sopp_wroff = 0;
1502bd670b35SErik Nordmark sopp.sopp_rxhiwat = connp->conn_rcvbuf;
1503bd670b35SErik Nordmark sopp.sopp_rxlowat = connp->conn_rcvlowat;
15040f1702c5SYu Xiangning sopp.sopp_maxblk = INFPSZ;
15050f1702c5SYu Xiangning sopp.sopp_maxpsz = rts_mod_info.mi_maxpsz;
15060f1702c5SYu Xiangning sopp.sopp_minpsz = (rts_mod_info.mi_minpsz == 1) ? 0 :
15070f1702c5SYu Xiangning rts_mod_info.mi_minpsz;
15080f1702c5SYu Xiangning
15090f1702c5SYu Xiangning (*connp->conn_upcalls->su_set_proto_props)
15100f1702c5SYu Xiangning (connp->conn_upper_handle, &sopp);
15110f1702c5SYu Xiangning
15120f1702c5SYu Xiangning /*
15130f1702c5SYu Xiangning * We treat it as already connected for routing socket.
15140f1702c5SYu Xiangning */
15150f1702c5SYu Xiangning (*connp->conn_upcalls->su_connected)
15160f1702c5SYu Xiangning (connp->conn_upper_handle, 0, NULL, -1);
15170f1702c5SYu Xiangning
1518bd670b35SErik Nordmark /* Indicate to IP that this is a routing socket client */
15190f1702c5SYu Xiangning ip_rts_register(connp);
15200f1702c5SYu Xiangning }
15210f1702c5SYu Xiangning
15220f1702c5SYu Xiangning /* ARGSUSED */
15230f1702c5SYu Xiangning int
rts_close(sock_lower_handle_t proto_handle,int flags,cred_t * cr)15240f1702c5SYu Xiangning rts_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr)
15250f1702c5SYu Xiangning {
15260f1702c5SYu Xiangning conn_t *connp = (conn_t *)proto_handle;
15270f1702c5SYu Xiangning
15280f1702c5SYu Xiangning ASSERT(connp != NULL && IPCL_IS_RTS(connp));
15290f1702c5SYu Xiangning return (rts_common_close(NULL, connp));
15300f1702c5SYu Xiangning }
15310f1702c5SYu Xiangning
15320f1702c5SYu Xiangning /* ARGSUSED */
15330f1702c5SYu Xiangning int
rts_shutdown(sock_lower_handle_t proto_handle,int how,cred_t * cr)15340f1702c5SYu Xiangning rts_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr)
15350f1702c5SYu Xiangning {
15360f1702c5SYu Xiangning conn_t *connp = (conn_t *)proto_handle;
15370f1702c5SYu Xiangning
15380f1702c5SYu Xiangning /* shut down the send side */
15390f1702c5SYu Xiangning if (how != SHUT_RD)
15400f1702c5SYu Xiangning (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
15410f1702c5SYu Xiangning SOCK_OPCTL_SHUT_SEND, 0);
15420f1702c5SYu Xiangning /* shut down the recv side */
15430f1702c5SYu Xiangning if (how != SHUT_WR)
15440f1702c5SYu Xiangning (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
15450f1702c5SYu Xiangning SOCK_OPCTL_SHUT_RECV, 0);
15460f1702c5SYu Xiangning return (0);
15470f1702c5SYu Xiangning }
15480f1702c5SYu Xiangning
15490f1702c5SYu Xiangning void
rts_clr_flowctrl(sock_lower_handle_t proto_handle)15500f1702c5SYu Xiangning rts_clr_flowctrl(sock_lower_handle_t proto_handle)
15510f1702c5SYu Xiangning {
15520f1702c5SYu Xiangning conn_t *connp = (conn_t *)proto_handle;
15530f1702c5SYu Xiangning rts_t *rts = connp->conn_rts;
15540f1702c5SYu Xiangning
15550f1702c5SYu Xiangning mutex_enter(&rts->rts_recv_mutex);
15560f1702c5SYu Xiangning connp->conn_flow_cntrld = B_FALSE;
15570f1702c5SYu Xiangning mutex_exit(&rts->rts_recv_mutex);
15580f1702c5SYu Xiangning }
15590f1702c5SYu Xiangning
15600f1702c5SYu Xiangning int
rts_ioctl(sock_lower_handle_t proto_handle,int cmd,intptr_t arg,int mode,int32_t * rvalp,cred_t * cr)15610f1702c5SYu Xiangning rts_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg,
15620f1702c5SYu Xiangning int mode, int32_t *rvalp, cred_t *cr)
15630f1702c5SYu Xiangning {
15640f1702c5SYu Xiangning conn_t *connp = (conn_t *)proto_handle;
15650f1702c5SYu Xiangning int error;
15660f1702c5SYu Xiangning
1567bd670b35SErik Nordmark /*
1568bd670b35SErik Nordmark * If we don't have a helper stream then create one.
1569bd670b35SErik Nordmark * ip_create_helper_stream takes care of locking the conn_t,
1570bd670b35SErik Nordmark * so this check for NULL is just a performance optimization.
1571bd670b35SErik Nordmark */
1572bd670b35SErik Nordmark if (connp->conn_helper_info == NULL) {
1573bd670b35SErik Nordmark rts_stack_t *rtss = connp->conn_rts->rts_rtss;
1574bd670b35SErik Nordmark
1575bd670b35SErik Nordmark ASSERT(rtss->rtss_ldi_ident != NULL);
1576bd670b35SErik Nordmark
1577bd670b35SErik Nordmark /*
1578bd670b35SErik Nordmark * Create a helper stream for non-STREAMS socket.
1579bd670b35SErik Nordmark */
1580bd670b35SErik Nordmark error = ip_create_helper_stream(connp, rtss->rtss_ldi_ident);
1581bd670b35SErik Nordmark if (error != 0) {
1582bd670b35SErik Nordmark ip0dbg(("rts_ioctl: create of IP helper stream "
1583bd670b35SErik Nordmark "failed %d\n", error));
1584bd670b35SErik Nordmark return (error);
1585bd670b35SErik Nordmark }
1586bd670b35SErik Nordmark }
1587bd670b35SErik Nordmark
15880f1702c5SYu Xiangning switch (cmd) {
15890f1702c5SYu Xiangning case ND_SET:
15900f1702c5SYu Xiangning case ND_GET:
15910f1702c5SYu Xiangning case TI_GETPEERNAME:
15920f1702c5SYu Xiangning case TI_GETMYNAME:
15930f1702c5SYu Xiangning #ifdef DEUG
15940f1702c5SYu Xiangning cmn_err(CE_CONT, "rts_ioctl cmd 0x%x on non sreams"
15950f1702c5SYu Xiangning " socket", cmd);
15960f1702c5SYu Xiangning #endif
15970f1702c5SYu Xiangning error = EINVAL;
15980f1702c5SYu Xiangning break;
15990f1702c5SYu Xiangning default:
16000f1702c5SYu Xiangning /*
16010f1702c5SYu Xiangning * Pass on to IP using helper stream
16020f1702c5SYu Xiangning */
160319a8a986SRao Shoaib error = ldi_ioctl(connp->conn_helper_info->iphs_handle,
16040f1702c5SYu Xiangning cmd, arg, mode, cr, rvalp);
16050f1702c5SYu Xiangning break;
16060f1702c5SYu Xiangning }
16070f1702c5SYu Xiangning
16080f1702c5SYu Xiangning return (error);
16090f1702c5SYu Xiangning }
16100f1702c5SYu Xiangning
16110f1702c5SYu Xiangning sock_downcalls_t sock_rts_downcalls = {
16120f1702c5SYu Xiangning rts_activate,
16130f1702c5SYu Xiangning rts_accept,
16140f1702c5SYu Xiangning rts_bind,
16150f1702c5SYu Xiangning rts_listen,
16160f1702c5SYu Xiangning rts_connect,
16170f1702c5SYu Xiangning rts_getpeername,
16180f1702c5SYu Xiangning rts_getsockname,
16190f1702c5SYu Xiangning rts_getsockopt,
16200f1702c5SYu Xiangning rts_setsockopt,
16210f1702c5SYu Xiangning rts_send,
16220f1702c5SYu Xiangning NULL,
16230f1702c5SYu Xiangning NULL,
16240f1702c5SYu Xiangning NULL,
16250f1702c5SYu Xiangning rts_shutdown,
16260f1702c5SYu Xiangning rts_clr_flowctrl,
16270f1702c5SYu Xiangning rts_ioctl,
16280f1702c5SYu Xiangning rts_close
16290f1702c5SYu Xiangning };
1630