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]
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>
37 #include <sys/sunddi.h>
38 #include <sys/cmn_err.h>
40 #include <sys/suntpi.h>
41 #include <sys/policy.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>
52 #include <inet/ipclassifier.h>
53 #include <inet/proto_set.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
= {
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. */
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
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
,
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
,
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
,
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
,
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
,
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
= {
158 * This routine allocates the necessary
159 * message blocks for IOCTL wrapping the
163 rts_ioctl_alloc(mblk_t
*data
)
170 mp
= allocb_tmpl(sizeof (ipllc_t
), data
);
173 mp1
= allocb_tmpl(sizeof (struct iocblk
), data
);
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
);
186 ioc
= (struct iocblk
*)mp1
->b_rptr
;
187 ioc
->ioc_cmd
= IP_IOCTL
;
190 ioc
->ioc_count
= msgdsize(mp
);
191 mp1
->b_wptr
+= sizeof (struct iocblk
);
192 mp1
->b_datap
->db_type
= M_IOCTL
;
199 * This routine closes rts stream, by disabling
200 * put/srv routines and freeing the this module
201 * internal datastructure.
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
)) {
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
227 ASSERT(connp
->conn_ref
== 1);
229 if (!IPCL_IS_NONSTR(connp
)) {
230 inet_minor_free(connp
->conn_minor_arena
, connp
->conn_dev
);
232 ip_free_helper_stream(connp
);
236 ipcl_conn_destroy(connp
);
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
;
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.
256 rts_stream_open(queue_t
*q
, dev_t
*devp
, int flag
, int sflag
, cred_t
*credp
)
262 /* If the stream is already open, return immediately. */
263 if (q
->q_ptr
!= NULL
)
266 if (sflag
== MODOPEN
)
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) {
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
;
288 WR(q
)->q_ptr
= connp
;
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
);
310 rts_open(int flag
, cred_t
*credp
)
318 ns
= netstack_find_by_cred(credp
);
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
;
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()
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
;
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
);
385 * This routine creates a T_ERROR_ACK message and passes it upstream.
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
)
395 * This routine creates a T_OK_ACK message and passes it upstream.
398 rts_ok_ack(queue_t
*q
, mblk_t
*mp
)
400 if ((mp
= mi_tpi_ok_ack_alloc(mp
)) != NULL
)
405 * This routine is called by rts_wput to handle T_UNBIND_REQ messages.
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);
418 rts
->rts_state
= TS_UNBND
;
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.
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);
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);
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);
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
;
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
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
);
492 tcap
= (struct T_capability_ack
*)mp
->b_rptr
;
495 if (cap_bits1
& TC1_INFO
) {
496 rts_copy_info(&tcap
->INFO_ack
, rts
);
497 tcap
->CAP_bits1
|= TC1_INFO
;
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.
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
,
518 rts_copy_info((struct T_info_ack
*)mp
->b_rptr
, rts
);
523 * This routine gets default values of certain options whose default
524 * values are maintained by protcol specific code
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 */
536 rts_opt_get(conn_t
*connp
, int level
, int name
, uchar_t
*ptr
)
538 rts_t
*rts
= connp
->conn_rts
;
542 ASSERT(RW_READ_HELD(&rts
->rts_rwlock
));
545 /* do this in conn_opt_get? */
549 mutex_enter(&connp
->conn_lock
);
550 *(int *)ptr
= connp
->conn_rtaware
;
551 mutex_exit(&connp
->conn_lock
);
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
);
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
;
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.
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
) {
608 return (EPROTONOSUPPORT
);
611 connp
->conn_proto
= *i1
;
616 * The following two items can be manipulated,
617 * but changing them should do nothing.
620 if (*i1
> rtss
->rtss_max_buf
) {
624 break; /* goto sizeof (int) option return */
626 if (*i1
> rtss
->rtss_max_buf
) {
630 break; /* goto sizeof (int) option return */
637 mutex_enter(&connp
->conn_lock
);
638 connp
->conn_rtaware
= *i1
;
639 mutex_exit(&connp
->conn_lock
);
646 /* Serialized setsockopt since we are D_MTQPAIR */
647 error
= conn_opt_set(&coas
, level
, name
, inlen
, invalp
,
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);
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
:
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.
688 case SETFN_OPTCOM_NEGOTIATE
:
691 case SETFN_UD_NEGOTIATE
:
692 case SETFN_CONN_NEGOTIATE
:
695 * Negotiating local and "association-related" options
696 * through T_UNITDATA_REQ or T_CONN_{REQ,CON}
697 * Not allowed in this module.
702 * We should never get here
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
)
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
);
735 * This routine sets socket options.
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
);
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
);
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
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
);
771 * Walk through the param array specified registering each element with the
772 * named dispatch (ND) handler.
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
)) {
789 /* This routine sets an ND variable in a rtsparam_t structure. */
792 rts_param_set(queue_t
*q
, mblk_t
*mp
, char *value
, caddr_t cp
, cred_t
*cr
)
795 rtsparam_t
*rtspa
= (rtsparam_t
*)cp
;
798 * Fail the request if the new value does not lie within the
801 if (ddi_strtoul(value
, NULL
, 10, &new_value
) != 0 ||
802 new_value
< rtspa
->rts_param_min
||
803 new_value
> rtspa
->rts_param_max
) {
807 /* Set the new value */
808 rtspa
->rts_param_value
= new_value
;
813 * Empty rsrv routine which is used by rts_input to cause a wakeup
814 * of a thread in qwait.
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
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.
834 rts_wrw(queue_t
*q
, struiod_t
*dp
)
836 mblk_t
*mp
= dp
->d_mp
;
840 conn_t
*connp
= Q_TO_CONN(q
);
841 rts_t
*rts
= connp
->conn_rts
;
843 while (rts
->rts_flag
& RTS_WRW_PENDING
) {
845 rts
->rts_error
= EINTR
;
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
;
859 * Pass the mblk (chain) onto wput().
863 switch (mp
->b_datap
->db_type
) {
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
))
871 if ((mp1
= mp
->b_cont
) == NULL
) {
872 rts
->rts_error
= EINVAL
;
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
;
894 rtm
= (rt_msghdr_t
*)mp
->b_rptr
;
895 rtm
->rtm_pid
= curproc
->p_pid
;
900 rts
->rts_flag
|= RTS_WPUT_PENDING
;
902 while (rts
->rts_flag
& RTS_WPUT_PENDING
)
904 /* RTS_WPUT_PENDING will be cleared below */
905 rts
->rts_error
= EINTR
;
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
920 rts_wput(queue_t
*q
, mblk_t
*mp
)
922 uchar_t
*rptr
= mp
->b_rptr
;
924 conn_t
*connp
= Q_TO_CONN(q
);
925 rts_t
*rts
= connp
->conn_rts
;
927 switch (mp
->b_datap
->db_type
) {
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
) {
945 rts_wput_other(q
, mp
);
950 ASSERT(msg_getcred(mp
, NULL
) != NULL
);
952 mp1
= rts_ioctl_alloc(mp
);
956 if (rts
->rts_flag
& RTS_WPUT_PENDING
) {
957 rts
->rts_error
= ENOMEM
;
958 rts
->rts_flag
&= ~RTS_WPUT_PENDING
;
962 ip_wput_nondata(q
, mp1
);
967 * Handles all the control message, if it
968 * can not understand it, it will
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
;
981 rtss
= rts
->rts_rtss
;
983 switch (mp
->b_datap
->db_type
) {
986 if ((mp
->b_wptr
- rptr
) < sizeof (t_scalar_t
)) {
988 * If the message does not contain a PRIM_type,
994 switch (((union T_primitives
*)rptr
)->type
) {
1000 rts_tpi_unbind(q
, mp
);
1002 case T_CAPABILITY_REQ
:
1003 rts_capability_req(q
, mp
);
1006 rts_info_req(q
, mp
);
1008 case T_SVR4_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
);
1020 rts_err_ack(q
, mp
, TSYSERR
, EINVAL
);
1023 if (((union T_primitives
*)rptr
)->type
==
1024 T_SVR4_OPTMGMT_REQ
) {
1025 svr4_optcom_req(q
, mp
, cr
, &rts_opt_obj
);
1027 tpi_optcom_req(q
, mp
, cr
, &rts_opt_obj
);
1033 /* Not supported by rts. */
1034 rts_err_ack(q
, mp
, TNOTSUPPORT
, 0);
1039 /* Illegal for rts. */
1041 (void) putnextctl1(RD(q
), M_ERROR
, EPROTO
);
1049 iocp
= (struct iocblk
*)mp
->b_rptr
;
1050 switch (iocp
->ioc_cmd
) {
1053 if (nd_getset(q
, rtss
->rtss_g_nd
, mp
)) {
1058 case TI_GETPEERNAME
:
1059 mi_copyin(q
, mp
, NULL
,
1060 SIZEOF_STRUCT(strbuf
, iocp
->ioc_flag
));
1067 rts_wput_iocdata(q
, mp
);
1072 ip_wput_nondata(q
, mp
);
1076 * Called by rts_wput_other to handle all M_IOCDATA messages.
1079 rts_wput_iocdata(queue_t
*q
, mblk_t
*mp
)
1081 struct sockaddr
*rtsaddr
;
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
:
1091 ip_wput_nondata(q
, mp
);
1094 switch (mi_copy_state(q
, mp
, &mp1
)) {
1097 case MI_COPY_CASE(MI_COPY_IN
, 1):
1099 case MI_COPY_CASE(MI_COPY_OUT
, 1):
1100 /* Copy out the strbuf. */
1103 case MI_COPY_CASE(MI_COPY_OUT
, 2):
1105 mi_copy_done(q
, mp
, 0);
1108 mi_copy_done(q
, mp
, EPROTO
);
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
);
1116 switch (iocp
->ioc_cmd
) {
1117 case TI_GETPEERNAME
:
1120 mi_copy_done(q
, mp
, EPROTO
);
1123 mp1
= mi_copyout_alloc(q
, mp
, STRUCT_FGETP(sb
, buf
), sizeof (sin_t
),
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 */
1137 * IP passes up a NULL ira.
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
;
1147 struct T_data_ind
*tdi
;
1150 switch (mp
->b_datap
->db_type
) {
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
);
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
);
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
;
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
);
1208 putnext(connp
->conn_rq
, mp
);
1214 rts_icmp_input(void *arg1
, mblk_t
*mp
, void *arg2
, ip_recv_attr_t
*ira
)
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
);
1234 rts_ddi_g_destroy(void)
1236 netstack_unregister(NS_RTS
);
1239 #define INET_NAME "ip"
1242 * Initialize the RTS stack instance.
1246 rts_stack_init(netstackid_t stackid
, netstack_t
*ns
)
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
);
1270 * Free the RTS stack instance.
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
));
1287 rts_accept(sock_lower_handle_t lproto_handle
,
1288 sock_lower_handle_t eproto_handle
, sock_upper_handle_t sock_handle
,
1296 rts_bind(sock_lower_handle_t proto_handle
, struct sockaddr
*sa
,
1297 socklen_t len
, cred_t
*cr
)
1300 * rebind not allowed
1307 rts_listen(sock_lower_handle_t proto_handle
, int backlog
, cred_t
*cr
)
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
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
);
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
);
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
;
1355 t_uscalar_t max_optbuf_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
);
1365 error
= proto_tlitosyserr(-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
);
1374 kmem_free(optvalp_buf
, max_optbuf_len
);
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
);
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
;
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
);
1404 error
= proto_tlitosyserr(-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
,
1412 rw_exit(&rts
->rts_rwlock
);
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
;
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
))) {
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
);
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
)
1461 if (family
!= AF_ROUTE
|| type
!= SOCK_RAW
||
1462 (proto
!= 0 && proto
!= AF_INET
&& proto
!= AF_INET6
)) {
1463 *errorp
= EPROTONOSUPPORT
;
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
);
1478 *smodep
= SM_ATOMIC
;
1479 *sock_downcalls
= &sock_rts_downcalls
;
1480 return ((sock_lower_handle_t
)connp
);
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
);
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
));
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 */
1535 (*connp
->conn_upcalls
->su_opctl
)(connp
->conn_upper_handle
,
1536 SOCK_OPCTL_SHUT_SEND
, 0);
1537 /* shut down the recv side */
1539 (*connp
->conn_upcalls
->su_opctl
)(connp
->conn_upper_handle
,
1540 SOCK_OPCTL_SHUT_RECV
, 0);
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
;
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
);
1577 ip0dbg(("rts_ioctl: create of IP helper stream "
1578 "failed %d\n", error
));
1586 case TI_GETPEERNAME
:
1589 cmn_err(CE_CONT
, "rts_ioctl cmd 0x%x on non sreams"
1596 * Pass on to IP using helper stream
1598 error
= ldi_ioctl(connp
->conn_helper_info
->iphs_handle
,
1599 cmd
, arg
, mode
, cr
, rvalp
);
1606 sock_downcalls_t sock_rts_downcalls
= {