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