xref: /illumos-gate/usr/src/uts/common/inet/ip/rts.c (revision 11ebc2d06b61e58acd4133a11514c86b48c923c5)
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 2008 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  *	When TCP requests IP to remove an IRE_CACHE of a troubled destination.
76  *
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.
80  */
81 
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 */
95 };
96 
97 /*
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.
101  */
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"},
108 };
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
113 
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 *);
117 static mblk_t	*rts_ioctl_alloc(mblk_t *data, cred_t *cr);
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, cred_t *cr)
163 {
164 	mblk_t	*mp = NULL;
165 	mblk_t	*mp1 = NULL;
166 	ipllc_t	*ipllc;
167 	struct iocblk	*ioc;
168 
169 	mp = allocb_cred(sizeof (ipllc_t), cr);
170 	if (mp == NULL)
171 		return (NULL);
172 	mp1 = allocb_cred(sizeof (struct iocblk), cr);
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 		 * Now we are truly single threaded on this stream, and can
217 		 * delete the things hanging off the connp, and finally the
218 		 * 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 		inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
227 	} else {
228 		ip_close_helper_stream(connp);
229 	}
230 	ASSERT(connp->conn_ref == 1);
231 
232 
233 	connp->conn_ref--;
234 	ipcl_conn_destroy(connp);
235 
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_stack_t *rtss;
260 	rts_t   *rts;
261 
262 	/* If the stream is already open, return immediately. */
263 	if (q->q_ptr != NULL)
264 		return (0);
265 
266 	if (sflag == MODOPEN)
267 		return (EINVAL);
268 
269 
270 	/*
271 	 * Since RTS is not used so heavily, allocating from the small
272 	 * arena should be sufficient.
273 	 */
274 	if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) {
275 		return (EBUSY);
276 	}
277 
278 	connp = rts_open(flag, credp);
279 	ASSERT(connp != NULL);
280 
281 
282 	*devp = makedevice(getemajor(*devp), (minor_t)conn_dev);
283 
284 	rts = connp->conn_rts;
285 
286 	rw_enter(&rts->rts_rwlock, RW_WRITER);
287 	connp->conn_dev = conn_dev;
288 	connp->conn_minor_arena = ip_minor_arena_sa;
289 
290 	/*
291 	 * Initialize the rts_t structure for this stream.
292 	 */
293 	q->q_ptr = connp;
294 	WR(q)->q_ptr = connp;
295 	connp->conn_rq = q;
296 	connp->conn_wq = WR(q);
297 
298 	rtss = rts->rts_rtss;
299 	q->q_hiwat = rtss->rtss_recv_hiwat;
300 	WR(q)->q_hiwat = rtss->rtss_xmit_hiwat;
301 	WR(q)->q_lowat = rtss->rtss_xmit_lowat;
302 
303 
304 
305 	mutex_enter(&connp->conn_lock);
306 	connp->conn_state_flags &= ~CONN_INCIPIENT;
307 	mutex_exit(&connp->conn_lock);
308 
309 	qprocson(q);
310 	rw_exit(&rts->rts_rwlock);
311 	/*
312 	 * Indicate the down IP module that this is a routing socket
313 	 * client by sending an RTS IOCTL without any user data. Although
314 	 * this is just a notification message (without any real routing
315 	 * request), we pass in any credential for correctness sake.
316 	 */
317 	ip_rts_register(connp);
318 
319 	return (0);
320 }
321 
322 /* ARGSUSED */
323 static conn_t *
324 rts_open(int flag, cred_t *credp)
325 {
326 	netstack_t *ns;
327 	rts_stack_t *rtss;
328 	rts_t	*rts;
329 	conn_t	*connp;
330 	zoneid_t zoneid;
331 
332 	ns = netstack_find_by_cred(credp);
333 	ASSERT(ns != NULL);
334 	rtss = ns->netstack_rts;
335 	ASSERT(rtss != NULL);
336 
337 	/*
338 	 * For exclusive stacks we set the zoneid to zero
339 	 * to make RTS operate as if in the global zone.
340 	 */
341 	if (ns->netstack_stackid != GLOBAL_NETSTACKID)
342 		zoneid = GLOBAL_ZONEID;
343 	else
344 		zoneid = crgetzoneid(credp);
345 
346 	connp = ipcl_conn_create(IPCL_RTSCONN, KM_SLEEP, ns);
347 	rts = connp->conn_rts;
348 
349 	/*
350 	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
351 	 * done by netstack_find_by_cred()
352 	 */
353 	netstack_rele(ns);
354 
355 
356 	rw_enter(&rts->rts_rwlock, RW_WRITER);
357 	ASSERT(connp->conn_rts == rts);
358 	ASSERT(rts->rts_connp == connp);
359 
360 	connp->conn_zoneid = zoneid;
361 	connp->conn_flow_cntrld = B_FALSE;
362 
363 	connp->conn_ulp_labeled = is_system_labeled();
364 
365 	rts->rts_rtss = rtss;
366 	rts->rts_xmit_hiwat = rtss->rtss_xmit_hiwat;
367 
368 	connp->conn_recv = rts_input;
369 	crhold(credp);
370 	connp->conn_cred = credp;
371 
372 	/*
373 	 * rts sockets start out as bound and connected
374 	 * For streams based sockets, socket state is set to
375 	 * SS_ISBOUND | SS_ISCONNECTED in so_strinit.
376 	 */
377 	rts->rts_state = TS_DATA_XFER;
378 	rw_exit(&rts->rts_rwlock);
379 
380 	return (connp);
381 }
382 
383 /*
384  * This routine creates a T_ERROR_ACK message and passes it upstream.
385  */
386 static void
387 rts_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
388 {
389 	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
390 		qreply(q, mp);
391 }
392 
393 /*
394  * This routine creates a T_OK_ACK message and passes it upstream.
395  */
396 static void
397 rts_ok_ack(queue_t *q, mblk_t *mp)
398 {
399 	if ((mp = mi_tpi_ok_ack_alloc(mp)) != NULL)
400 		qreply(q, mp);
401 }
402 
403 /*
404  * This routine is called by rts_wput to handle T_UNBIND_REQ messages.
405  */
406 static void
407 rts_tpi_unbind(queue_t *q, mblk_t *mp)
408 {
409 	conn_t	*connp = Q_TO_CONN(q);
410 	rts_t	*rts = connp->conn_rts;
411 
412 	/* If a bind has not been done, we can't unbind. */
413 	if (rts->rts_state != TS_IDLE) {
414 		rts_err_ack(q, mp, TOUTSTATE, 0);
415 		return;
416 	}
417 	rts->rts_state = TS_UNBND;
418 	rts_ok_ack(q, mp);
419 }
420 
421 /*
422  * This routine is called to handle each
423  * O_T_BIND_REQ/T_BIND_REQ message passed to
424  * rts_wput. Note: This routine works with both
425  * O_T_BIND_REQ and T_BIND_REQ semantics.
426  */
427 static void
428 rts_tpi_bind(queue_t *q, mblk_t *mp)
429 {
430 	conn_t	*connp = Q_TO_CONN(q);
431 	rts_t	*rts = connp->conn_rts;
432 	mblk_t	*mp1;
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 	/*
448 	 * Reallocate the message to make sure we have enough room for an
449 	 * address and the protocol type.
450 	 */
451 	mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin_t), 1);
452 	if (mp1 == NULL) {
453 		rts_err_ack(q, mp, TSYSERR, ENOMEM);
454 		return;
455 	}
456 	mp = mp1;
457 	tbr = (struct T_bind_req *)mp->b_rptr;
458 	if (tbr->ADDR_length != 0) {
459 		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
460 		    "rts_tpi_bind: bad ADDR_length %d", tbr->ADDR_length);
461 		rts_err_ack(q, mp, TBADADDR, 0);
462 		return;
463 	}
464 	/* Generic request */
465 	tbr->ADDR_offset = (t_scalar_t)sizeof (struct T_bind_req);
466 	tbr->ADDR_length = 0;
467 	tbr->PRIM_type = T_BIND_ACK;
468 	rts->rts_state = TS_IDLE;
469 	qreply(q, mp);
470 }
471 
472 static void
473 rts_copy_info(struct T_info_ack *tap, rts_t *rts)
474 {
475 	*tap = rts_g_t_info_ack;
476 	tap->CURRENT_state = rts->rts_state;
477 	tap->OPT_size = rts_max_optsize;
478 }
479 
480 /*
481  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
482  * rts_wput.  Much of the T_CAPABILITY_ACK information is copied from
483  * rts_g_t_info_ack.  The current state of the stream is copied from
484  * rts_state.
485  */
486 static void
487 rts_capability_req(queue_t *q, mblk_t *mp)
488 {
489 	conn_t	*connp = Q_TO_CONN(q);
490 	rts_t	*rts = connp->conn_rts;
491 	t_uscalar_t		cap_bits1;
492 	struct T_capability_ack	*tcap;
493 
494 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
495 
496 	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
497 	    mp->b_datap->db_type, T_CAPABILITY_ACK);
498 	if (mp == NULL)
499 		return;
500 
501 	tcap = (struct T_capability_ack *)mp->b_rptr;
502 	tcap->CAP_bits1 = 0;
503 
504 	if (cap_bits1 & TC1_INFO) {
505 		rts_copy_info(&tcap->INFO_ack, rts);
506 		tcap->CAP_bits1 |= TC1_INFO;
507 	}
508 
509 	qreply(q, mp);
510 }
511 
512 /*
513  * This routine responds to T_INFO_REQ messages.  It is called by rts_wput.
514  * Most of the T_INFO_ACK information is copied from rts_g_t_info_ack.
515  * The current state of the stream is copied from rts_state.
516  */
517 static void
518 rts_info_req(queue_t *q, mblk_t *mp)
519 {
520 	conn_t	*connp = Q_TO_CONN(q);
521 	rts_t	*rts = connp->conn_rts;
522 
523 	mp = tpi_ack_alloc(mp, sizeof (rts_g_t_info_ack), M_PCPROTO,
524 	    T_INFO_ACK);
525 	if (mp == NULL)
526 		return;
527 	rts_copy_info((struct T_info_ack *)mp->b_rptr, rts);
528 	qreply(q, mp);
529 }
530 
531 /*
532  * This routine gets default values of certain options whose default
533  * values are maintained by protcol specific code
534  */
535 /* ARGSUSED */
536 int
537 rts_opt_default(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
538 {
539 	/* no default value processed by protocol specific code currently */
540 	return (-1);
541 }
542 
543 
544 static int
545 rts_opt_get(conn_t *connp, int level, int name, uchar_t *ptr)
546 {
547 	rts_t	*rts = connp->conn_rts;
548 	int	*i1 = (int *)ptr;
549 
550 	ASSERT(RW_READ_HELD(&rts->rts_rwlock));
551 
552 	switch (level) {
553 	case SOL_SOCKET:
554 		switch (name) {
555 		case SO_DEBUG:
556 			*i1 = rts->rts_debug;
557 			break;
558 		case SO_REUSEADDR:
559 			*i1 = rts->rts_reuseaddr;
560 			break;
561 		case SO_TYPE:
562 			*i1 = SOCK_RAW;
563 			break;
564 
565 		/*
566 		 * The following three items are available here,
567 		 * but are only meaningful to IP.
568 		 */
569 		case SO_DONTROUTE:
570 			*i1 = rts->rts_dontroute;
571 			break;
572 		case SO_USELOOPBACK:
573 			*i1 = rts->rts_useloopback;
574 			break;
575 		case SO_BROADCAST:
576 			*i1 = rts->rts_broadcast;
577 			break;
578 		case SO_PROTOTYPE:
579 			*i1 = rts->rts_proto;
580 			break;
581 		/*
582 		 * The following two items can be manipulated,
583 		 * but changing them should do nothing.
584 		 */
585 		case SO_SNDBUF:
586 			ASSERT(rts->rts_xmit_hiwat <= INT_MAX);
587 			*i1 = (int)(rts->rts_xmit_hiwat);
588 			break;
589 		case SO_RCVBUF:
590 			ASSERT(rts->rts_recv_hiwat <= INT_MAX);
591 			*i1 = (int)(rts->rts_recv_hiwat);
592 			break;
593 		case SO_DOMAIN:
594 			*i1 = PF_ROUTE;
595 			break;
596 		default:
597 			return (-1);
598 		}
599 		break;
600 	default:
601 		return (-1);
602 	}
603 	return ((int)sizeof (int));
604 }
605 
606 /* ARGSUSED */
607 static int
608 rts_do_opt_set(conn_t *connp, int level, int name, uint_t inlen,
609     uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, cred_t *cr,
610     void *thisdg_attrs, boolean_t checkonly)
611 {
612 	int	*i1 = (int *)invalp;
613 	rts_t	*rts = connp->conn_rts;
614 	rts_stack_t	*rtss = rts->rts_rtss;
615 
616 	ASSERT(RW_WRITE_HELD(&rts->rts_rwlock));
617 
618 	/*
619 	 * For rts, we should have no ancillary data sent down
620 	 * (rts_wput doesn't handle options).
621 	 */
622 	ASSERT(thisdg_attrs == NULL);
623 
624 	/*
625 	 * For fixed length options, no sanity check
626 	 * of passed in length is done. It is assumed *_optcom_req()
627 	 * routines do the right thing.
628 	 */
629 
630 	switch (level) {
631 	case SOL_SOCKET:
632 		switch (name) {
633 		case SO_REUSEADDR:
634 			if (!checkonly)
635 				rts->rts_reuseaddr = *i1;
636 			break;	/* goto sizeof (int) option return */
637 		case SO_DEBUG:
638 			if (!checkonly)
639 				rts->rts_debug = *i1;
640 			break;	/* goto sizeof (int) option return */
641 		/*
642 		 * The following three items are available here,
643 		 * but are only meaningful to IP.
644 		 */
645 		case SO_DONTROUTE:
646 			if (!checkonly)
647 				rts->rts_dontroute = *i1;
648 			break;	/* goto sizeof (int) option return */
649 		case SO_USELOOPBACK:
650 			if (!checkonly)
651 				rts->rts_useloopback = *i1;
652 			break;	/* goto sizeof (int) option return */
653 		case SO_BROADCAST:
654 			if (!checkonly)
655 				rts->rts_broadcast = *i1;
656 			break;	/* goto sizeof (int) option return */
657 		case SO_PROTOTYPE:
658 			/*
659 			 * Routing socket applications that call socket() with
660 			 * a third argument can filter which messages will be
661 			 * sent upstream thanks to sockfs.  so_socket() sends
662 			 * down the SO_PROTOTYPE and rts_queue_input()
663 			 * implements the filtering.
664 			 */
665 			if (*i1 != AF_INET && *i1 != AF_INET6)
666 				return (EPROTONOSUPPORT);
667 			if (!checkonly)
668 				rts->rts_proto = *i1;
669 			break;	/* goto sizeof (int) option return */
670 		/*
671 		 * The following two items can be manipulated,
672 		 * but changing them should do nothing.
673 		 */
674 		case SO_SNDBUF:
675 			if (*i1 > rtss->rtss_max_buf) {
676 				*outlenp = 0;
677 				return (ENOBUFS);
678 			}
679 			if (!checkonly) {
680 				rts->rts_xmit_hiwat = *i1;
681 				if (!IPCL_IS_NONSTR(connp))
682 					connp->conn_wq->q_hiwat = *i1;
683 			}
684 			break;	/* goto sizeof (int) option return */
685 		case SO_RCVBUF:
686 			if (*i1 > rtss->rtss_max_buf) {
687 				*outlenp = 0;
688 				return (ENOBUFS);
689 			}
690 			if (!checkonly) {
691 				rts->rts_recv_hiwat = *i1;
692 				rw_exit(&rts->rts_rwlock);
693 				(void) proto_set_rx_hiwat(connp->conn_rq, connp,
694 				    *i1);
695 				rw_enter(&rts->rts_rwlock, RW_WRITER);
696 			}
697 
698 			break;	/* goto sizeof (int) option return */
699 		default:
700 			*outlenp = 0;
701 			return (EINVAL);
702 		}
703 		break;
704 	default:
705 		*outlenp = 0;
706 		return (EINVAL);
707 	}
708 	/*
709 	 * Common case of return from an option that is sizeof (int)
710 	 */
711 	if (invalp != outvalp) {
712 		/* don't trust bcopy for identical src/dst */
713 		(void) bcopy(invalp, outvalp, inlen);
714 	}
715 	*outlenp = (t_uscalar_t)sizeof (int);
716 	return (0);
717 }
718 
719 static int
720 rts_opt_set(conn_t *connp, uint_t optset_context, int level, int name,
721     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
722     void *thisdg_attrs, cred_t *cr)
723 {
724 	boolean_t 	checkonly = B_FALSE;
725 
726 	if (optset_context) {
727 		switch (optset_context) {
728 		case SETFN_OPTCOM_CHECKONLY:
729 			checkonly = B_TRUE;
730 			/*
731 			 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
732 			 * inlen != 0 implies value supplied and
733 			 * 	we have to "pretend" to set it.
734 			 * inlen == 0 implies that there is no value part
735 			 * 	in T_CHECK request and just validation
736 			 * done elsewhere should be enough, we just return here.
737 			 */
738 			if (inlen == 0) {
739 				*outlenp = 0;
740 				return (0);
741 			}
742 			break;
743 		case SETFN_OPTCOM_NEGOTIATE:
744 			checkonly = B_FALSE;
745 			break;
746 		case SETFN_UD_NEGOTIATE:
747 		case SETFN_CONN_NEGOTIATE:
748 			checkonly = B_FALSE;
749 			/*
750 			 * Negotiating local and "association-related" options
751 			 * through T_UNITDATA_REQ or T_CONN_{REQ,CON}
752 			 * Not allowed in this module.
753 			 */
754 			return (EINVAL);
755 		default:
756 			/*
757 			 * We should never get here
758 			 */
759 			*outlenp = 0;
760 			return (EINVAL);
761 		}
762 
763 		ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
764 		    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
765 
766 	}
767 	return (rts_do_opt_set(connp, level, name, inlen, invalp, outlenp,
768 	    outvalp, cr, thisdg_attrs, checkonly));
769 
770 }
771 
772 /*
773  * This routine retrieves the current status of socket options.
774  * It returns the size of the option retrieved.
775  */
776 int
777 rts_tpi_opt_get(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
778 {
779 	rts_t	*rts;
780 	int	err;
781 
782 	rts = Q_TO_RTS(q);
783 	rw_enter(&rts->rts_rwlock, RW_READER);
784 	err = rts_opt_get(Q_TO_CONN(q), level, name, ptr);
785 	rw_exit(&rts->rts_rwlock);
786 	return (err);
787 }
788 
789 /*
790  * This routine sets socket options.
791  */
792 /*ARGSUSED*/
793 int
794 rts_tpi_opt_set(queue_t *q, uint_t optset_context, int level,
795     int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp,
796     uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
797 {
798 	conn_t	*connp = Q_TO_CONN(q);
799 	int	error;
800 	rts_t	*rts = connp->conn_rts;
801 
802 
803 	rw_enter(&rts->rts_rwlock, RW_WRITER);
804 	error = rts_opt_set(connp, optset_context, level, name, inlen, invalp,
805 	    outlenp, outvalp, thisdg_attrs, cr);
806 	rw_exit(&rts->rts_rwlock);
807 	return (error);
808 }
809 
810 /*
811  * This routine retrieves the value of an ND variable in a rtsparam_t
812  * structure. It is called through nd_getset when a user reads the
813  * variable.
814  */
815 /* ARGSUSED */
816 static int
817 rts_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
818 {
819 	rtsparam_t	*rtspa = (rtsparam_t *)cp;
820 
821 	(void) mi_mpprintf(mp, "%u", rtspa->rts_param_value);
822 	return (0);
823 }
824 
825 /*
826  * Walk through the param array specified registering each element with the
827  * named dispatch (ND) handler.
828  */
829 static boolean_t
830 rts_param_register(IDP *ndp, rtsparam_t *rtspa, int cnt)
831 {
832 	for (; cnt-- > 0; rtspa++) {
833 		if (rtspa->rts_param_name != NULL && rtspa->rts_param_name[0]) {
834 			if (!nd_load(ndp, rtspa->rts_param_name,
835 			    rts_param_get, rts_param_set, (caddr_t)rtspa)) {
836 				nd_free(ndp);
837 				return (B_FALSE);
838 			}
839 		}
840 	}
841 	return (B_TRUE);
842 }
843 
844 /* This routine sets an ND variable in a rtsparam_t structure. */
845 /* ARGSUSED */
846 static int
847 rts_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
848 {
849 	ulong_t	new_value;
850 	rtsparam_t	*rtspa = (rtsparam_t *)cp;
851 
852 	/*
853 	 * Fail the request if the new value does not lie within the
854 	 * required bounds.
855 	 */
856 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
857 	    new_value < rtspa->rts_param_min ||
858 	    new_value > rtspa->rts_param_max) {
859 		return (EINVAL);
860 	}
861 
862 	/* Set the new value */
863 	rtspa->rts_param_value = new_value;
864 	return (0);
865 }
866 
867 /*
868  * Empty rsrv routine which is used by rts_input to cause a wakeup
869  * of a thread in qwait.
870  */
871 /*ARGSUSED*/
872 static void
873 rts_rsrv(queue_t *q)
874 {
875 }
876 
877 /*
878  * This routine handles synchronous messages passed downstream. It either
879  * consumes the message or passes it downstream; it never queues a
880  * a message. The data messages that go down are wrapped in an IOCTL
881  * message.
882  *
883  * Since it is synchronous, it waits for the M_IOCACK/M_IOCNAK so that
884  * it can return an immediate error (such as ENETUNREACH when adding a route).
885  * It uses the RTS_WRW_PENDING to ensure that each rts instance has only
886  * one M_IOCTL outstanding at any given time.
887  */
888 static int
889 rts_wrw(queue_t *q, struiod_t *dp)
890 {
891 	mblk_t	*mp = dp->d_mp;
892 	mblk_t	*mp1;
893 	int	error;
894 	rt_msghdr_t	*rtm;
895 	conn_t	*connp = Q_TO_CONN(q);
896 	rts_t	*rts = connp->conn_rts;
897 
898 	while (rts->rts_flag & RTS_WRW_PENDING) {
899 		if (qwait_rw(q)) {
900 			rts->rts_error = EINTR;
901 			goto err_ret;
902 		}
903 	}
904 	rts->rts_flag |= RTS_WRW_PENDING;
905 
906 	if (isuioq(q) && (error = struioget(q, mp, dp, 0))) {
907 		/*
908 		 * Uio error of some sort, so just return the error.
909 		 */
910 		rts->rts_error = error;
911 		goto err_ret;
912 	}
913 	/*
914 	 * Pass the mblk (chain) onto wput().
915 	 */
916 	dp->d_mp = 0;
917 
918 	switch (mp->b_datap->db_type) {
919 	case M_PROTO:
920 	case M_PCPROTO:
921 		/* Expedite other than T_DATA_REQ to below the switch */
922 		if (((mp->b_wptr - mp->b_rptr) !=
923 		    sizeof (struct T_data_req)) ||
924 		    (((union T_primitives *)mp->b_rptr)->type != T_DATA_REQ))
925 			break;
926 		if ((mp1 = mp->b_cont) == NULL) {
927 			rts->rts_error = EINVAL;
928 			goto err_ret;
929 		}
930 		freeb(mp);
931 		mp = mp1;
932 		/* FALLTHRU */
933 	case M_DATA:
934 		/*
935 		 * The semantics of the routing socket is such that the rtm_pid
936 		 * field is automatically filled in during requests with the
937 		 * current process' pid.  We do this here (where we still have
938 		 * user context) after checking we have at least a message the
939 		 * size of a routing message header.
940 		 */
941 		if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
942 			if (!pullupmsg(mp, sizeof (rt_msghdr_t))) {
943 				rts->rts_error = EINVAL;
944 				goto err_ret;
945 			}
946 		}
947 		rtm = (rt_msghdr_t *)mp->b_rptr;
948 		rtm->rtm_pid = curproc->p_pid;
949 		break;
950 	default:
951 		break;
952 	}
953 	rts->rts_flag |= RTS_WPUT_PENDING;
954 	rts_wput(q, mp);
955 	while (rts->rts_flag & RTS_WPUT_PENDING)
956 		if (qwait_rw(q)) {
957 			/* RTS_WPUT_PENDING will be cleared below */
958 			rts->rts_error = EINTR;
959 			break;
960 		}
961 err_ret:
962 	rts->rts_flag &= ~(RTS_WPUT_PENDING | RTS_WRW_PENDING);
963 	return (rts->rts_error);
964 }
965 
966 /*
967  * This routine handles all messages passed downstream. It either
968  * consumes the message or passes it downstream; it never queues a
969  * a message. The data messages that go down are wrapped in an IOCTL
970  * message.
971  *
972  * FIXME? Should we call IP rts_request directly? Could punt on returning
973  * errno in the case when it defers processing due to
974  * IPIF_CHANGING/ILL_CHANGING???
975  */
976 static void
977 rts_wput(queue_t *q, mblk_t *mp)
978 {
979 	uchar_t	*rptr = mp->b_rptr;
980 	mblk_t	*mp1;
981 	conn_t	*connp = Q_TO_CONN(q);
982 	rts_t	*rts = connp->conn_rts;
983 
984 	switch (mp->b_datap->db_type) {
985 	case M_DATA:
986 		break;
987 	case M_PROTO:
988 	case M_PCPROTO:
989 		if ((mp->b_wptr - rptr) == sizeof (struct T_data_req)) {
990 			/* Expedite valid T_DATA_REQ to below the switch */
991 			if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
992 				mp1 = mp->b_cont;
993 				freeb(mp);
994 				if (mp1 == NULL)
995 					return;
996 				mp = mp1;
997 				break;
998 			}
999 		}
1000 		/* FALLTHRU */
1001 	default:
1002 		rts_wput_other(q, mp);
1003 		return;
1004 	}
1005 
1006 
1007 	mp1 = rts_ioctl_alloc(mp, DB_CRED(mp));
1008 	if (mp1 == NULL) {
1009 		ASSERT(rts != NULL);
1010 		freemsg(mp);
1011 		if (rts->rts_flag & RTS_WPUT_PENDING) {
1012 			rts->rts_error = ENOMEM;
1013 			rts->rts_flag &= ~RTS_WPUT_PENDING;
1014 		}
1015 		return;
1016 	}
1017 	ip_output(connp, mp1, q, IP_WPUT);
1018 }
1019 
1020 
1021 /*
1022  * Handles all the control message, if it
1023  * can not understand it, it will
1024  * pass down stream.
1025  */
1026 static void
1027 rts_wput_other(queue_t *q, mblk_t *mp)
1028 {
1029 	conn_t	*connp = Q_TO_CONN(q);
1030 	rts_t	*rts = connp->conn_rts;
1031 	uchar_t	*rptr = mp->b_rptr;
1032 	struct iocblk	*iocp;
1033 	cred_t	*cr;
1034 	rts_stack_t	*rtss;
1035 
1036 	rtss = rts->rts_rtss;
1037 
1038 	cr = DB_CREDDEF(mp, connp->conn_cred);
1039 
1040 	switch (mp->b_datap->db_type) {
1041 	case M_PROTO:
1042 	case M_PCPROTO:
1043 		if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) {
1044 			/*
1045 			 * If the message does not contain a PRIM_type,
1046 			 * throw it away.
1047 			 */
1048 			freemsg(mp);
1049 			return;
1050 		}
1051 		switch (((union T_primitives *)rptr)->type) {
1052 		case T_BIND_REQ:
1053 		case O_T_BIND_REQ:
1054 			rts_tpi_bind(q, mp);
1055 			return;
1056 		case T_UNBIND_REQ:
1057 			rts_tpi_unbind(q, mp);
1058 			return;
1059 		case T_CAPABILITY_REQ:
1060 			rts_capability_req(q, mp);
1061 			return;
1062 		case T_INFO_REQ:
1063 			rts_info_req(q, mp);
1064 			return;
1065 		case T_SVR4_OPTMGMT_REQ:
1066 			(void) svr4_optcom_req(q, mp, cr, &rts_opt_obj,
1067 			    B_TRUE);
1068 			return;
1069 		case T_OPTMGMT_REQ:
1070 			(void) tpi_optcom_req(q, mp, cr, &rts_opt_obj, B_TRUE);
1071 			return;
1072 		case O_T_CONN_RES:
1073 		case T_CONN_RES:
1074 		case T_DISCON_REQ:
1075 			/* Not supported by rts. */
1076 			rts_err_ack(q, mp, TNOTSUPPORT, 0);
1077 			return;
1078 		case T_DATA_REQ:
1079 		case T_EXDATA_REQ:
1080 		case T_ORDREL_REQ:
1081 			/* Illegal for rts. */
1082 			freemsg(mp);
1083 			(void) putnextctl1(RD(q), M_ERROR, EPROTO);
1084 			return;
1085 
1086 		default:
1087 			break;
1088 		}
1089 		break;
1090 	case M_IOCTL:
1091 		iocp = (struct iocblk *)mp->b_rptr;
1092 		switch (iocp->ioc_cmd) {
1093 		case ND_SET:
1094 		case ND_GET:
1095 			if (nd_getset(q, rtss->rtss_g_nd, mp)) {
1096 				qreply(q, mp);
1097 				return;
1098 			}
1099 			break;
1100 		case TI_GETPEERNAME:
1101 			mi_copyin(q, mp, NULL,
1102 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
1103 			return;
1104 		default:
1105 			break;
1106 		}
1107 	case M_IOCDATA:
1108 		rts_wput_iocdata(q, mp);
1109 		return;
1110 	default:
1111 		break;
1112 	}
1113 	ip_output(connp, mp, q, IP_WPUT);
1114 }
1115 
1116 /*
1117  * Called by rts_wput_other to handle all M_IOCDATA messages.
1118  */
1119 static void
1120 rts_wput_iocdata(queue_t *q, mblk_t *mp)
1121 {
1122 	conn_t *connp = Q_TO_CONN(q);
1123 	struct sockaddr	*rtsaddr;
1124 	mblk_t	*mp1;
1125 	STRUCT_HANDLE(strbuf, sb);
1126 	struct iocblk	*iocp	= (struct iocblk *)mp->b_rptr;
1127 
1128 	/* Make sure it is one of ours. */
1129 	switch (iocp->ioc_cmd) {
1130 	case TI_GETPEERNAME:
1131 		break;
1132 	default:
1133 		ip_output(connp, mp, q, IP_WPUT);
1134 		return;
1135 	}
1136 	switch (mi_copy_state(q, mp, &mp1)) {
1137 	case -1:
1138 		return;
1139 	case MI_COPY_CASE(MI_COPY_IN, 1):
1140 		break;
1141 	case MI_COPY_CASE(MI_COPY_OUT, 1):
1142 		/* Copy out the strbuf. */
1143 		mi_copyout(q, mp);
1144 		return;
1145 	case MI_COPY_CASE(MI_COPY_OUT, 2):
1146 		/* All done. */
1147 		mi_copy_done(q, mp, 0);
1148 		return;
1149 	default:
1150 		mi_copy_done(q, mp, EPROTO);
1151 		return;
1152 	}
1153 	STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr);
1154 	if (STRUCT_FGET(sb, maxlen) < (int)sizeof (sin_t)) {
1155 		mi_copy_done(q, mp, EINVAL);
1156 		return;
1157 	}
1158 	switch (iocp->ioc_cmd) {
1159 	case TI_GETPEERNAME:
1160 		break;
1161 	default:
1162 		mi_copy_done(q, mp, EPROTO);
1163 		return;
1164 	}
1165 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), sizeof (sin_t),
1166 	    B_TRUE);
1167 	if (mp1 == NULL)
1168 		return;
1169 	STRUCT_FSET(sb, len, (int)sizeof (sin_t));
1170 	rtsaddr = (struct sockaddr *)mp1->b_rptr;
1171 	mp1->b_wptr = (uchar_t *)&rtsaddr[1];
1172 	bzero(rtsaddr, sizeof (struct sockaddr));
1173 	rtsaddr->sa_family = AF_ROUTE;
1174 	/* Copy out the address */
1175 	mi_copyout(q, mp);
1176 }
1177 
1178 /*ARGSUSED2*/
1179 static void
1180 rts_input(void *arg1, mblk_t *mp, void *arg2)
1181 {
1182 	conn_t *connp = (conn_t *)arg1;
1183 	rts_t	*rts = connp->conn_rts;
1184 	struct iocblk	*iocp;
1185 	mblk_t *mp1;
1186 	struct T_data_ind *tdi;
1187 	int	error;
1188 
1189 	switch (mp->b_datap->db_type) {
1190 	case M_IOCACK:
1191 	case M_IOCNAK:
1192 		iocp = (struct iocblk *)mp->b_rptr;
1193 		if (IPCL_IS_NONSTR(connp)) {
1194 			ASSERT(rts->rts_flag & (RTS_REQ_PENDING));
1195 			mutex_enter(&rts->rts_send_mutex);
1196 			rts->rts_flag &= ~RTS_REQ_INPROG;
1197 			rts->rts_error = iocp->ioc_error;
1198 			cv_signal(&rts->rts_io_cv);
1199 			mutex_exit(&rts->rts_send_mutex);
1200 			freemsg(mp);
1201 			return;
1202 		} else {
1203 			if (rts->rts_flag & (RTS_WPUT_PENDING)) {
1204 				rts->rts_flag &= ~RTS_WPUT_PENDING;
1205 				rts->rts_error = iocp->ioc_error;
1206 				/*
1207 				 * Tell rts_wvw/qwait that we are done.
1208 				 * Note: there is no qwait_wakeup() we can use.
1209 				 */
1210 				qenable(connp->conn_rq);
1211 				freemsg(mp);
1212 				return;
1213 			}
1214 		}
1215 		break;
1216 	case M_DATA:
1217 		/*
1218 		 * Prepend T_DATA_IND to prevent the stream head from
1219 		 * consolidating multiple messages together.
1220 		 * If the allocation fails just send up the M_DATA.
1221 		 */
1222 		mp1 = allocb(sizeof (*tdi), BPRI_MED);
1223 		if (mp1 != NULL) {
1224 			mp1->b_cont = mp;
1225 			mp = mp1;
1226 
1227 			mp->b_datap->db_type = M_PROTO;
1228 			mp->b_wptr += sizeof (*tdi);
1229 			tdi = (struct T_data_ind *)mp->b_rptr;
1230 			tdi->PRIM_type = T_DATA_IND;
1231 			tdi->MORE_flag = 0;
1232 		}
1233 		break;
1234 	default:
1235 		break;
1236 	}
1237 
1238 	if (IPCL_IS_NONSTR(connp)) {
1239 		if ((*connp->conn_upcalls->su_recv)
1240 		    (connp->conn_upper_handle, mp, msgdsize(mp), 0,
1241 		    &error, NULL) < 0) {
1242 			ASSERT(error == ENOSPC);
1243 			/*
1244 			 * Let's confirm hoding the lock that
1245 			 * we are out of recv space.
1246 			 */
1247 			mutex_enter(&rts->rts_recv_mutex);
1248 			if ((*connp->conn_upcalls->su_recv)
1249 			    (connp->conn_upper_handle, NULL, 0, 0,
1250 			    &error, NULL) < 0) {
1251 				ASSERT(error == ENOSPC);
1252 				connp->conn_flow_cntrld = B_TRUE;
1253 			}
1254 			mutex_exit(&rts->rts_recv_mutex);
1255 		}
1256 	} else {
1257 		putnext(connp->conn_rq, mp);
1258 	}
1259 }
1260 
1261 
1262 void
1263 rts_ddi_g_init(void)
1264 {
1265 	rts_max_optsize = optcom_max_optsize(rts_opt_obj.odb_opt_des_arr,
1266 	    rts_opt_obj.odb_opt_arr_cnt);
1267 
1268 	/*
1269 	 * We want to be informed each time a stack is created or
1270 	 * destroyed in the kernel, so we can maintain the
1271 	 * set of rts_stack_t's.
1272 	 */
1273 	netstack_register(NS_RTS, rts_stack_init, NULL, rts_stack_fini);
1274 }
1275 
1276 void
1277 rts_ddi_g_destroy(void)
1278 {
1279 	netstack_unregister(NS_RTS);
1280 }
1281 
1282 #define	INET_NAME	"ip"
1283 
1284 /*
1285  * Initialize the RTS stack instance.
1286  */
1287 /* ARGSUSED */
1288 static void *
1289 rts_stack_init(netstackid_t stackid, netstack_t *ns)
1290 {
1291 	rts_stack_t	*rtss;
1292 	rtsparam_t	*pa;
1293 	int		error = 0;
1294 	major_t		major;
1295 
1296 	rtss = (rts_stack_t *)kmem_zalloc(sizeof (*rtss), KM_SLEEP);
1297 	rtss->rtss_netstack = ns;
1298 
1299 	pa = (rtsparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
1300 	rtss->rtss_params = pa;
1301 	bcopy(lcl_param_arr, rtss->rtss_params, sizeof (lcl_param_arr));
1302 
1303 	(void) rts_param_register(&rtss->rtss_g_nd,
1304 	    rtss->rtss_params, A_CNT(lcl_param_arr));
1305 
1306 	major = mod_name_to_major(INET_NAME);
1307 	error = ldi_ident_from_major(major, &rtss->rtss_ldi_ident);
1308 	ASSERT(error == 0);
1309 	return (rtss);
1310 }
1311 
1312 /*
1313  * Free the RTS stack instance.
1314  */
1315 /* ARGSUSED */
1316 static void
1317 rts_stack_fini(netstackid_t stackid, void *arg)
1318 {
1319 	rts_stack_t *rtss = (rts_stack_t *)arg;
1320 
1321 	nd_free(&rtss->rtss_g_nd);
1322 	kmem_free(rtss->rtss_params, sizeof (lcl_param_arr));
1323 	rtss->rtss_params = NULL;
1324 	ldi_ident_release(rtss->rtss_ldi_ident);
1325 	kmem_free(rtss, sizeof (*rtss));
1326 }
1327 
1328 /* ARGSUSED */
1329 int
1330 rts_accept(sock_lower_handle_t lproto_handle,
1331     sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle,
1332     cred_t *cr)
1333 {
1334 	return (EINVAL);
1335 }
1336 
1337 /* ARGSUSED */
1338 static int
1339 rts_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa,
1340     socklen_t len, cred_t *cr)
1341 {
1342 	/*
1343 	 * rebind not allowed
1344 	 */
1345 	return (EINVAL);
1346 }
1347 
1348 /* ARGSUSED */
1349 int
1350 rts_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr)
1351 {
1352 	return (EINVAL);
1353 }
1354 
1355 /* ARGSUSED */
1356 int
1357 rts_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa,
1358     socklen_t len, sock_connid_t *id, cred_t *cr)
1359 {
1360 	/*
1361 	 * rts sockets start out as bound and connected
1362 	 */
1363 	*id = 0;
1364 	return (EISCONN);
1365 }
1366 
1367 /* ARGSUSED */
1368 int
1369 rts_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr,
1370     socklen_t *addrlen, cred_t *cr)
1371 {
1372 	conn_t *connp = (conn_t *)proto_handle;
1373 	rts_t *rts = connp->conn_rts;
1374 
1375 	ASSERT(rts != NULL);
1376 
1377 	bzero(addr, sizeof (struct sockaddr));
1378 	addr->sa_family = AF_ROUTE;
1379 	*addrlen = sizeof (struct sockaddr);
1380 
1381 	return (0);
1382 }
1383 
1384 /* ARGSUSED */
1385 int
1386 rts_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr,
1387     socklen_t *addrlen, cred_t *cr)
1388 {
1389 	return (EOPNOTSUPP);
1390 }
1391 
1392 static int
1393 rts_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
1394     void *optvalp, socklen_t *optlen, cred_t *cr)
1395 {
1396 	conn_t  	*connp = (conn_t *)proto_handle;
1397 	rts_t		*rts = connp->conn_rts;
1398 	int		error;
1399 	t_uscalar_t	max_optbuf_len;
1400 	void		*optvalp_buf;
1401 	int		len;
1402 
1403 	error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len,
1404 	    rts_opt_obj.odb_opt_des_arr,
1405 	    rts_opt_obj.odb_opt_arr_cnt,
1406 	    rts_opt_obj.odb_topmost_tpiprovider,
1407 	    B_FALSE, B_TRUE, cr);
1408 	if (error != 0) {
1409 		if (error < 0)
1410 			error = proto_tlitosyserr(-error);
1411 		return (error);
1412 	}
1413 
1414 	optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP);
1415 	rw_enter(&rts->rts_rwlock, RW_READER);
1416 	len = rts_opt_get(connp, level, option_name, optvalp_buf);
1417 	rw_exit(&rts->rts_rwlock);
1418 
1419 	if (len < 0) {
1420 		/*
1421 		 * Pass on to IP
1422 		 */
1423 		error = ip_get_options(connp, level, option_name,
1424 		    optvalp, optlen, cr);
1425 	} else {
1426 		/*
1427 		 * update optlen and copy option value
1428 		 */
1429 		t_uscalar_t size = MIN(len, *optlen);
1430 		bcopy(optvalp_buf, optvalp, size);
1431 		bcopy(&size, optlen, sizeof (size));
1432 		error = 0;
1433 	}
1434 
1435 	kmem_free(optvalp_buf, max_optbuf_len);
1436 	return (error);
1437 }
1438 
1439 static int
1440 rts_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
1441     const void *optvalp, socklen_t optlen, cred_t *cr)
1442 {
1443 	conn_t	*connp = (conn_t *)proto_handle;
1444 	rts_t	*rts = connp->conn_rts;
1445 	int	error;
1446 
1447 	error = proto_opt_check(level, option_name, optlen, NULL,
1448 	    rts_opt_obj.odb_opt_des_arr,
1449 	    rts_opt_obj.odb_opt_arr_cnt,
1450 	    rts_opt_obj.odb_topmost_tpiprovider,
1451 	    B_TRUE, B_FALSE, cr);
1452 
1453 	if (error != 0) {
1454 		if (error < 0)
1455 			error = proto_tlitosyserr(-error);
1456 		return (error);
1457 	}
1458 
1459 	rw_enter(&rts->rts_rwlock, RW_WRITER);
1460 	error = rts_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name,
1461 	    optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp,
1462 	    NULL, cr);
1463 	rw_exit(&rts->rts_rwlock);
1464 
1465 	ASSERT(error >= 0);
1466 
1467 	return (error);
1468 }
1469 
1470 /* ARGSUSED */
1471 static int
1472 rts_send(sock_lower_handle_t proto_handle, mblk_t *mp,
1473     struct nmsghdr *msg, cred_t *cr)
1474 {
1475 	mblk_t  *mp1;
1476 	conn_t  *connp = (conn_t *)proto_handle;
1477 	rts_t   *rts = connp->conn_rts;
1478 	rt_msghdr_t	*rtm;
1479 	int error;
1480 
1481 	ASSERT(DB_TYPE(mp) == M_DATA);
1482 	/*
1483 	 * The semantics of the routing socket is such that the rtm_pid
1484 	 * field is automatically filled in during requests with the
1485 	 * current process' pid.  We do this here (where we still have
1486 	 * user context) after checking we have at least a message the
1487 	 * size of a routing message header.
1488 	 */
1489 	if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
1490 		if (!pullupmsg(mp, sizeof (rt_msghdr_t))) {
1491 			rts->rts_error = EINVAL;
1492 			freemsg(mp);
1493 			return (rts->rts_error);
1494 		}
1495 	}
1496 	rtm = (rt_msghdr_t *)mp->b_rptr;
1497 	rtm->rtm_pid = curproc->p_pid;
1498 
1499 	mp1 = rts_ioctl_alloc(mp, DB_CRED(mp));
1500 	if (mp1 == NULL) {
1501 		ASSERT(rts != NULL);
1502 		freemsg(mp);
1503 		return (ENOMEM);
1504 	}
1505 
1506 	/*
1507 	 * Allow only one outstanding request(ioctl) at any given time
1508 	 */
1509 	mutex_enter(&rts->rts_send_mutex);
1510 	while (rts->rts_flag & RTS_REQ_PENDING) {
1511 		int ret;
1512 
1513 		ret = cv_wait_sig(&rts->rts_send_cv, &rts->rts_send_mutex);
1514 		if (ret <= 0) {
1515 			mutex_exit(&rts->rts_send_mutex);
1516 			freemsg(mp);
1517 			return (EINTR);
1518 		}
1519 	}
1520 
1521 	rts->rts_flag |= RTS_REQ_PENDING;
1522 
1523 	rts->rts_flag |= RTS_REQ_INPROG;
1524 
1525 	mutex_exit(&rts->rts_send_mutex);
1526 
1527 	CONN_INC_REF(connp);
1528 
1529 	error = ip_rts_request_common(rts->rts_connp->conn_wq, mp1, connp,
1530 	    DB_CREDDEF(mp, connp->conn_cred));
1531 
1532 	mutex_enter(&rts->rts_send_mutex);
1533 	if (error == EINPROGRESS) {
1534 		ASSERT(rts->rts_flag & RTS_REQ_INPROG);
1535 		if (rts->rts_flag & RTS_REQ_INPROG) {
1536 			/*
1537 			 * Once the request has been issued we wait for
1538 			 * completion
1539 			 */
1540 			cv_wait(&rts->rts_io_cv, &rts->rts_send_mutex);
1541 			error = rts->rts_error;
1542 		}
1543 	}
1544 
1545 	ASSERT((error != 0) || !(rts->rts_flag & RTS_REQ_INPROG));
1546 	ASSERT(MUTEX_HELD(&rts->rts_send_mutex));
1547 
1548 	rts->rts_flag &= ~(RTS_REQ_PENDING | RTS_REQ_INPROG);
1549 	cv_signal(&rts->rts_send_cv);
1550 	mutex_exit(&rts->rts_send_mutex);
1551 	return (error);
1552 }
1553 
1554 /* ARGSUSED */
1555 sock_lower_handle_t
1556 rts_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls,
1557     uint_t *smodep, int *errorp, int flags, cred_t *credp)
1558 {
1559 	conn_t	*connp;
1560 	rts_t	*rts;
1561 	rts_stack_t *rtss;
1562 
1563 	if (family != AF_ROUTE || type != SOCK_RAW ||
1564 	    (proto != 0 && proto != AF_INET && proto != AF_INET6)) {
1565 		*errorp = EPROTONOSUPPORT;
1566 		return (NULL);
1567 	}
1568 
1569 	connp = rts_open(flags, credp);
1570 	ASSERT(connp != NULL);
1571 	connp->conn_flags |= IPCL_NONSTR;
1572 
1573 	rts = connp->conn_rts;
1574 	rtss = rts->rts_rtss;
1575 
1576 	rts->rts_xmit_hiwat = rtss->rtss_xmit_hiwat;
1577 	rts->rts_xmit_lowat = rtss->rtss_xmit_lowat;
1578 	rts->rts_recv_hiwat = rtss->rtss_recv_hiwat;
1579 	rts->rts_recv_lowat = rts_mod_info.mi_lowat;
1580 
1581 	ASSERT(rtss->rtss_ldi_ident != NULL);
1582 
1583 	*errorp = ip_create_helper_stream(connp, rtss->rtss_ldi_ident);
1584 	if (*errorp != 0) {
1585 #ifdef DEBUG
1586 		cmn_err(CE_CONT, "rts_create: create of IP helper stream"
1587 		    " failed\n");
1588 #endif
1589 		(void) rts_close((sock_lower_handle_t)connp, 0, credp);
1590 		return (NULL);
1591 	}
1592 
1593 	mutex_enter(&connp->conn_lock);
1594 	connp->conn_state_flags &= ~CONN_INCIPIENT;
1595 	mutex_exit(&connp->conn_lock);
1596 
1597 	*errorp = 0;
1598 	*smodep = SM_ATOMIC;
1599 	*sock_downcalls = &sock_rts_downcalls;
1600 	return ((sock_lower_handle_t)connp);
1601 }
1602 
1603 /* ARGSUSED */
1604 void
1605 rts_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle,
1606     sock_upcalls_t *sock_upcalls, int flags, cred_t *cr)
1607 {
1608 	conn_t  *connp = (conn_t *)proto_handle;
1609 	rts_t	*rts = connp->conn_rts;
1610 	rts_stack_t *rtss = rts->rts_rtss;
1611 	struct sock_proto_props sopp;
1612 
1613 	connp->conn_upcalls = sock_upcalls;
1614 	connp->conn_upper_handle = sock_handle;
1615 
1616 	sopp.sopp_flags = SOCKOPT_WROFF | SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT |
1617 	    SOCKOPT_MAXBLK | SOCKOPT_MAXPSZ | SOCKOPT_MINPSZ;
1618 	sopp.sopp_wroff = 0;
1619 	sopp.sopp_rxhiwat = rtss->rtss_recv_hiwat;
1620 	sopp.sopp_rxlowat = rts_mod_info.mi_lowat;
1621 	sopp.sopp_maxblk = INFPSZ;
1622 	sopp.sopp_maxpsz = rts_mod_info.mi_maxpsz;
1623 	sopp.sopp_minpsz = (rts_mod_info.mi_minpsz == 1) ? 0 :
1624 	    rts_mod_info.mi_minpsz;
1625 
1626 	(*connp->conn_upcalls->su_set_proto_props)
1627 	    (connp->conn_upper_handle, &sopp);
1628 
1629 	/*
1630 	 * We treat it as already connected for routing socket.
1631 	 */
1632 	(*connp->conn_upcalls->su_connected)
1633 	    (connp->conn_upper_handle, 0, NULL, -1);
1634 
1635 	/*
1636 	 * Indicate the down IP module that this is a routing socket
1637 	 * client by sending an RTS IOCTL without any user data. Although
1638 	 * this is just a notification message (without any real routing
1639 	 * request), we pass in any credential for correctness sake.
1640 	 */
1641 	ip_rts_register(connp);
1642 }
1643 
1644 /* ARGSUSED */
1645 int
1646 rts_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr)
1647 {
1648 	conn_t  *connp = (conn_t *)proto_handle;
1649 
1650 	ASSERT(connp != NULL && IPCL_IS_RTS(connp));
1651 	return (rts_common_close(NULL, connp));
1652 }
1653 
1654 /* ARGSUSED */
1655 int
1656 rts_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr)
1657 {
1658 	conn_t  *connp = (conn_t *)proto_handle;
1659 
1660 	/* shut down the send side */
1661 	if (how != SHUT_RD)
1662 		(*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
1663 		    SOCK_OPCTL_SHUT_SEND, 0);
1664 	/* shut down the recv side */
1665 	if (how != SHUT_WR)
1666 		(*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
1667 		    SOCK_OPCTL_SHUT_RECV, 0);
1668 	return (0);
1669 }
1670 
1671 void
1672 rts_clr_flowctrl(sock_lower_handle_t proto_handle)
1673 {
1674 	conn_t  *connp = (conn_t *)proto_handle;
1675 	rts_t	*rts = connp->conn_rts;
1676 
1677 	mutex_enter(&rts->rts_recv_mutex);
1678 	connp->conn_flow_cntrld = B_FALSE;
1679 	mutex_exit(&rts->rts_recv_mutex);
1680 }
1681 
1682 int
1683 rts_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg,
1684     int mode, int32_t *rvalp, cred_t *cr)
1685 {
1686 	conn_t		*connp = (conn_t *)proto_handle;
1687 	int		error;
1688 
1689 	switch (cmd) {
1690 	case ND_SET:
1691 	case ND_GET:
1692 	case TI_GETPEERNAME:
1693 	case TI_GETMYNAME:
1694 #ifdef DEUG
1695 		cmn_err(CE_CONT, "rts_ioctl cmd 0x%x on non sreams"
1696 		    " socket", cmd);
1697 #endif
1698 		error = EINVAL;
1699 		break;
1700 	default:
1701 		/*
1702 		 * Pass on to IP using helper stream
1703 		 */
1704 		error = ldi_ioctl(connp->conn_helper_info->iphs_handle,
1705 		    cmd, arg, mode, cr, rvalp);
1706 		break;
1707 	}
1708 
1709 	return (error);
1710 }
1711 
1712 sock_downcalls_t sock_rts_downcalls = {
1713 	rts_activate,
1714 	rts_accept,
1715 	rts_bind,
1716 	rts_listen,
1717 	rts_connect,
1718 	rts_getpeername,
1719 	rts_getsockname,
1720 	rts_getsockopt,
1721 	rts_setsockopt,
1722 	rts_send,
1723 	NULL,
1724 	NULL,
1725 	NULL,
1726 	rts_shutdown,
1727 	rts_clr_flowctrl,
1728 	rts_ioctl,
1729 	rts_close
1730 };
1731