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