xref: /titanic_51/usr/src/uts/common/inet/ip/rts.c (revision 25540de2e9a31b620c68d87ce2f3bbe8a196ad93)
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  *	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_free_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 		 * The following three items are available here,
566 		 * but are only meaningful to IP.
567 		 */
568 		case SO_DONTROUTE:
569 			*i1 = rts->rts_dontroute;
570 			break;
571 		case SO_USELOOPBACK:
572 			*i1 = rts->rts_useloopback;
573 			break;
574 		case SO_BROADCAST:
575 			*i1 = rts->rts_broadcast;
576 			break;
577 		case SO_PROTOTYPE:
578 			*i1 = rts->rts_proto;
579 			break;
580 		/*
581 		 * The following two items can be manipulated,
582 		 * but changing them should do nothing.
583 		 */
584 		case SO_SNDBUF:
585 			ASSERT(rts->rts_xmit_hiwat <= INT_MAX);
586 			*i1 = (int)(rts->rts_xmit_hiwat);
587 			break;
588 		case SO_RCVBUF:
589 			ASSERT(rts->rts_recv_hiwat <= INT_MAX);
590 			*i1 = (int)(rts->rts_recv_hiwat);
591 			break;
592 		case SO_DOMAIN:
593 			*i1 = PF_ROUTE;
594 			break;
595 		default:
596 			return (-1);
597 		}
598 		break;
599 	case SOL_ROUTE:
600 		switch (name) {
601 		case RT_AWARE:
602 			mutex_enter(&connp->conn_lock);
603 			*i1 = connp->conn_rtaware;
604 			mutex_exit(&connp->conn_lock);
605 			break;
606 		}
607 		break;
608 	default:
609 		return (-1);
610 	}
611 	return ((int)sizeof (int));
612 }
613 
614 /* ARGSUSED */
615 static int
616 rts_do_opt_set(conn_t *connp, int level, int name, uint_t inlen,
617     uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, cred_t *cr,
618     void *thisdg_attrs, boolean_t checkonly)
619 {
620 	int	*i1 = (int *)invalp;
621 	rts_t	*rts = connp->conn_rts;
622 	rts_stack_t	*rtss = rts->rts_rtss;
623 
624 	ASSERT(RW_WRITE_HELD(&rts->rts_rwlock));
625 
626 	/*
627 	 * For rts, we should have no ancillary data sent down
628 	 * (rts_wput doesn't handle options).
629 	 */
630 	ASSERT(thisdg_attrs == NULL);
631 
632 	/*
633 	 * For fixed length options, no sanity check
634 	 * of passed in length is done. It is assumed *_optcom_req()
635 	 * routines do the right thing.
636 	 */
637 
638 	switch (level) {
639 	case SOL_SOCKET:
640 		switch (name) {
641 		case SO_REUSEADDR:
642 			if (!checkonly)
643 				rts->rts_reuseaddr = *i1;
644 			break;	/* goto sizeof (int) option return */
645 		case SO_DEBUG:
646 			if (!checkonly)
647 				rts->rts_debug = *i1;
648 			break;	/* goto sizeof (int) option return */
649 		/*
650 		 * The following three items are available here,
651 		 * but are only meaningful to IP.
652 		 */
653 		case SO_DONTROUTE:
654 			if (!checkonly)
655 				rts->rts_dontroute = *i1;
656 			break;	/* goto sizeof (int) option return */
657 		case SO_USELOOPBACK:
658 			if (!checkonly)
659 				rts->rts_useloopback = *i1;
660 			break;	/* goto sizeof (int) option return */
661 		case SO_BROADCAST:
662 			if (!checkonly)
663 				rts->rts_broadcast = *i1;
664 			break;	/* goto sizeof (int) option return */
665 		case SO_PROTOTYPE:
666 			/*
667 			 * Routing socket applications that call socket() with
668 			 * a third argument can filter which messages will be
669 			 * sent upstream thanks to sockfs.  so_socket() sends
670 			 * down the SO_PROTOTYPE and rts_queue_input()
671 			 * implements the filtering.
672 			 */
673 			if (*i1 != AF_INET && *i1 != AF_INET6)
674 				return (EPROTONOSUPPORT);
675 			if (!checkonly)
676 				rts->rts_proto = *i1;
677 			break;	/* goto sizeof (int) option return */
678 		/*
679 		 * The following two items can be manipulated,
680 		 * but changing them should do nothing.
681 		 */
682 		case SO_SNDBUF:
683 			if (*i1 > rtss->rtss_max_buf) {
684 				*outlenp = 0;
685 				return (ENOBUFS);
686 			}
687 			if (!checkonly) {
688 				rts->rts_xmit_hiwat = *i1;
689 				if (!IPCL_IS_NONSTR(connp))
690 					connp->conn_wq->q_hiwat = *i1;
691 			}
692 			break;	/* goto sizeof (int) option return */
693 		case SO_RCVBUF:
694 			if (*i1 > rtss->rtss_max_buf) {
695 				*outlenp = 0;
696 				return (ENOBUFS);
697 			}
698 			if (!checkonly) {
699 				rts->rts_recv_hiwat = *i1;
700 				rw_exit(&rts->rts_rwlock);
701 				(void) proto_set_rx_hiwat(connp->conn_rq, connp,
702 				    *i1);
703 				rw_enter(&rts->rts_rwlock, RW_WRITER);
704 			}
705 
706 			break;	/* goto sizeof (int) option return */
707 		default:
708 			*outlenp = 0;
709 			return (EINVAL);
710 		}
711 		break;
712 	case SOL_ROUTE:
713 		switch (name) {
714 		case RT_AWARE:
715 			if (!checkonly) {
716 				mutex_enter(&connp->conn_lock);
717 				connp->conn_rtaware = *i1;
718 				mutex_exit(&connp->conn_lock);
719 			}
720 			break;	/* goto sizeof (int) option return */
721 		default:
722 			*outlenp = 0;
723 			return (EINVAL);
724 		}
725 		break;
726 	default:
727 		*outlenp = 0;
728 		return (EINVAL);
729 	}
730 	/*
731 	 * Common case of return from an option that is sizeof (int)
732 	 */
733 	if (invalp != outvalp) {
734 		/* don't trust bcopy for identical src/dst */
735 		(void) bcopy(invalp, outvalp, inlen);
736 	}
737 	*outlenp = (t_uscalar_t)sizeof (int);
738 	return (0);
739 }
740 
741 static int
742 rts_opt_set(conn_t *connp, uint_t optset_context, int level, int name,
743     uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
744     void *thisdg_attrs, cred_t *cr)
745 {
746 	boolean_t 	checkonly = B_FALSE;
747 
748 	if (optset_context) {
749 		switch (optset_context) {
750 		case SETFN_OPTCOM_CHECKONLY:
751 			checkonly = B_TRUE;
752 			/*
753 			 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
754 			 * inlen != 0 implies value supplied and
755 			 * 	we have to "pretend" to set it.
756 			 * inlen == 0 implies that there is no value part
757 			 * 	in T_CHECK request and just validation
758 			 * done elsewhere should be enough, we just return here.
759 			 */
760 			if (inlen == 0) {
761 				*outlenp = 0;
762 				return (0);
763 			}
764 			break;
765 		case SETFN_OPTCOM_NEGOTIATE:
766 			checkonly = B_FALSE;
767 			break;
768 		case SETFN_UD_NEGOTIATE:
769 		case SETFN_CONN_NEGOTIATE:
770 			checkonly = B_FALSE;
771 			/*
772 			 * Negotiating local and "association-related" options
773 			 * through T_UNITDATA_REQ or T_CONN_{REQ,CON}
774 			 * Not allowed in this module.
775 			 */
776 			return (EINVAL);
777 		default:
778 			/*
779 			 * We should never get here
780 			 */
781 			*outlenp = 0;
782 			return (EINVAL);
783 		}
784 
785 		ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
786 		    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
787 
788 	}
789 	return (rts_do_opt_set(connp, level, name, inlen, invalp, outlenp,
790 	    outvalp, cr, thisdg_attrs, checkonly));
791 
792 }
793 
794 /*
795  * This routine retrieves the current status of socket options.
796  * It returns the size of the option retrieved.
797  */
798 int
799 rts_tpi_opt_get(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
800 {
801 	rts_t	*rts;
802 	int	err;
803 
804 	rts = Q_TO_RTS(q);
805 	rw_enter(&rts->rts_rwlock, RW_READER);
806 	err = rts_opt_get(Q_TO_CONN(q), level, name, ptr);
807 	rw_exit(&rts->rts_rwlock);
808 	return (err);
809 }
810 
811 /*
812  * This routine sets socket options.
813  */
814 /*ARGSUSED*/
815 int
816 rts_tpi_opt_set(queue_t *q, uint_t optset_context, int level,
817     int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp,
818     uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
819 {
820 	conn_t	*connp = Q_TO_CONN(q);
821 	int	error;
822 	rts_t	*rts = connp->conn_rts;
823 
824 
825 	rw_enter(&rts->rts_rwlock, RW_WRITER);
826 	error = rts_opt_set(connp, optset_context, level, name, inlen, invalp,
827 	    outlenp, outvalp, thisdg_attrs, cr);
828 	rw_exit(&rts->rts_rwlock);
829 	return (error);
830 }
831 
832 /*
833  * This routine retrieves the value of an ND variable in a rtsparam_t
834  * structure. It is called through nd_getset when a user reads the
835  * variable.
836  */
837 /* ARGSUSED */
838 static int
839 rts_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
840 {
841 	rtsparam_t	*rtspa = (rtsparam_t *)cp;
842 
843 	(void) mi_mpprintf(mp, "%u", rtspa->rts_param_value);
844 	return (0);
845 }
846 
847 /*
848  * Walk through the param array specified registering each element with the
849  * named dispatch (ND) handler.
850  */
851 static boolean_t
852 rts_param_register(IDP *ndp, rtsparam_t *rtspa, int cnt)
853 {
854 	for (; cnt-- > 0; rtspa++) {
855 		if (rtspa->rts_param_name != NULL && rtspa->rts_param_name[0]) {
856 			if (!nd_load(ndp, rtspa->rts_param_name,
857 			    rts_param_get, rts_param_set, (caddr_t)rtspa)) {
858 				nd_free(ndp);
859 				return (B_FALSE);
860 			}
861 		}
862 	}
863 	return (B_TRUE);
864 }
865 
866 /* This routine sets an ND variable in a rtsparam_t structure. */
867 /* ARGSUSED */
868 static int
869 rts_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
870 {
871 	ulong_t	new_value;
872 	rtsparam_t	*rtspa = (rtsparam_t *)cp;
873 
874 	/*
875 	 * Fail the request if the new value does not lie within the
876 	 * required bounds.
877 	 */
878 	if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
879 	    new_value < rtspa->rts_param_min ||
880 	    new_value > rtspa->rts_param_max) {
881 		return (EINVAL);
882 	}
883 
884 	/* Set the new value */
885 	rtspa->rts_param_value = new_value;
886 	return (0);
887 }
888 
889 /*
890  * Empty rsrv routine which is used by rts_input to cause a wakeup
891  * of a thread in qwait.
892  */
893 /*ARGSUSED*/
894 static void
895 rts_rsrv(queue_t *q)
896 {
897 }
898 
899 /*
900  * This routine handles synchronous messages passed downstream. It either
901  * consumes the message or passes it downstream; it never queues a
902  * a message. The data messages that go down are wrapped in an IOCTL
903  * message.
904  *
905  * Since it is synchronous, it waits for the M_IOCACK/M_IOCNAK so that
906  * it can return an immediate error (such as ENETUNREACH when adding a route).
907  * It uses the RTS_WRW_PENDING to ensure that each rts instance has only
908  * one M_IOCTL outstanding at any given time.
909  */
910 static int
911 rts_wrw(queue_t *q, struiod_t *dp)
912 {
913 	mblk_t	*mp = dp->d_mp;
914 	mblk_t	*mp1;
915 	int	error;
916 	rt_msghdr_t	*rtm;
917 	conn_t	*connp = Q_TO_CONN(q);
918 	rts_t	*rts = connp->conn_rts;
919 
920 	while (rts->rts_flag & RTS_WRW_PENDING) {
921 		if (qwait_rw(q)) {
922 			rts->rts_error = EINTR;
923 			goto err_ret;
924 		}
925 	}
926 	rts->rts_flag |= RTS_WRW_PENDING;
927 
928 	if (isuioq(q) && (error = struioget(q, mp, dp, 0))) {
929 		/*
930 		 * Uio error of some sort, so just return the error.
931 		 */
932 		rts->rts_error = error;
933 		goto err_ret;
934 	}
935 	/*
936 	 * Pass the mblk (chain) onto wput().
937 	 */
938 	dp->d_mp = 0;
939 
940 	switch (mp->b_datap->db_type) {
941 	case M_PROTO:
942 	case M_PCPROTO:
943 		/* Expedite other than T_DATA_REQ to below the switch */
944 		if (((mp->b_wptr - mp->b_rptr) !=
945 		    sizeof (struct T_data_req)) ||
946 		    (((union T_primitives *)mp->b_rptr)->type != T_DATA_REQ))
947 			break;
948 		if ((mp1 = mp->b_cont) == NULL) {
949 			rts->rts_error = EINVAL;
950 			goto err_ret;
951 		}
952 		freeb(mp);
953 		mp = mp1;
954 		/* FALLTHRU */
955 	case M_DATA:
956 		/*
957 		 * The semantics of the routing socket is such that the rtm_pid
958 		 * field is automatically filled in during requests with the
959 		 * current process' pid.  We do this here (where we still have
960 		 * user context) after checking we have at least a message the
961 		 * size of a routing message header.
962 		 */
963 		if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
964 			if (!pullupmsg(mp, sizeof (rt_msghdr_t))) {
965 				rts->rts_error = EINVAL;
966 				goto err_ret;
967 			}
968 		}
969 		rtm = (rt_msghdr_t *)mp->b_rptr;
970 		rtm->rtm_pid = curproc->p_pid;
971 		break;
972 	default:
973 		break;
974 	}
975 	rts->rts_flag |= RTS_WPUT_PENDING;
976 	rts_wput(q, mp);
977 	while (rts->rts_flag & RTS_WPUT_PENDING)
978 		if (qwait_rw(q)) {
979 			/* RTS_WPUT_PENDING will be cleared below */
980 			rts->rts_error = EINTR;
981 			break;
982 		}
983 err_ret:
984 	rts->rts_flag &= ~(RTS_WPUT_PENDING | RTS_WRW_PENDING);
985 	return (rts->rts_error);
986 }
987 
988 /*
989  * This routine handles all messages passed downstream. It either
990  * consumes the message or passes it downstream; it never queues a
991  * a message. The data messages that go down are wrapped in an IOCTL
992  * message.
993  *
994  * FIXME? Should we call IP rts_request directly? Could punt on returning
995  * errno in the case when it defers processing due to
996  * IPIF_CHANGING/ILL_CHANGING???
997  */
998 static void
999 rts_wput(queue_t *q, mblk_t *mp)
1000 {
1001 	uchar_t	*rptr = mp->b_rptr;
1002 	mblk_t	*mp1;
1003 	conn_t	*connp = Q_TO_CONN(q);
1004 	rts_t	*rts = connp->conn_rts;
1005 
1006 	switch (mp->b_datap->db_type) {
1007 	case M_DATA:
1008 		break;
1009 	case M_PROTO:
1010 	case M_PCPROTO:
1011 		if ((mp->b_wptr - rptr) == sizeof (struct T_data_req)) {
1012 			/* Expedite valid T_DATA_REQ to below the switch */
1013 			if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
1014 				mp1 = mp->b_cont;
1015 				freeb(mp);
1016 				if (mp1 == NULL)
1017 					return;
1018 				mp = mp1;
1019 				break;
1020 			}
1021 		}
1022 		/* FALLTHRU */
1023 	default:
1024 		rts_wput_other(q, mp);
1025 		return;
1026 	}
1027 
1028 
1029 	mp1 = rts_ioctl_alloc(mp, DB_CRED(mp));
1030 	if (mp1 == NULL) {
1031 		ASSERT(rts != NULL);
1032 		freemsg(mp);
1033 		if (rts->rts_flag & RTS_WPUT_PENDING) {
1034 			rts->rts_error = ENOMEM;
1035 			rts->rts_flag &= ~RTS_WPUT_PENDING;
1036 		}
1037 		return;
1038 	}
1039 	ip_output(connp, mp1, q, IP_WPUT);
1040 }
1041 
1042 
1043 /*
1044  * Handles all the control message, if it
1045  * can not understand it, it will
1046  * pass down stream.
1047  */
1048 static void
1049 rts_wput_other(queue_t *q, mblk_t *mp)
1050 {
1051 	conn_t	*connp = Q_TO_CONN(q);
1052 	rts_t	*rts = connp->conn_rts;
1053 	uchar_t	*rptr = mp->b_rptr;
1054 	struct iocblk	*iocp;
1055 	cred_t	*cr;
1056 	rts_stack_t	*rtss;
1057 
1058 	rtss = rts->rts_rtss;
1059 
1060 	cr = DB_CREDDEF(mp, connp->conn_cred);
1061 
1062 	switch (mp->b_datap->db_type) {
1063 	case M_PROTO:
1064 	case M_PCPROTO:
1065 		if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) {
1066 			/*
1067 			 * If the message does not contain a PRIM_type,
1068 			 * throw it away.
1069 			 */
1070 			freemsg(mp);
1071 			return;
1072 		}
1073 		switch (((union T_primitives *)rptr)->type) {
1074 		case T_BIND_REQ:
1075 		case O_T_BIND_REQ:
1076 			rts_tpi_bind(q, mp);
1077 			return;
1078 		case T_UNBIND_REQ:
1079 			rts_tpi_unbind(q, mp);
1080 			return;
1081 		case T_CAPABILITY_REQ:
1082 			rts_capability_req(q, mp);
1083 			return;
1084 		case T_INFO_REQ:
1085 			rts_info_req(q, mp);
1086 			return;
1087 		case T_SVR4_OPTMGMT_REQ:
1088 			(void) svr4_optcom_req(q, mp, cr, &rts_opt_obj,
1089 			    B_TRUE);
1090 			return;
1091 		case T_OPTMGMT_REQ:
1092 			(void) tpi_optcom_req(q, mp, cr, &rts_opt_obj, B_TRUE);
1093 			return;
1094 		case O_T_CONN_RES:
1095 		case T_CONN_RES:
1096 		case T_DISCON_REQ:
1097 			/* Not supported by rts. */
1098 			rts_err_ack(q, mp, TNOTSUPPORT, 0);
1099 			return;
1100 		case T_DATA_REQ:
1101 		case T_EXDATA_REQ:
1102 		case T_ORDREL_REQ:
1103 			/* Illegal for rts. */
1104 			freemsg(mp);
1105 			(void) putnextctl1(RD(q), M_ERROR, EPROTO);
1106 			return;
1107 
1108 		default:
1109 			break;
1110 		}
1111 		break;
1112 	case M_IOCTL:
1113 		iocp = (struct iocblk *)mp->b_rptr;
1114 		switch (iocp->ioc_cmd) {
1115 		case ND_SET:
1116 		case ND_GET:
1117 			if (nd_getset(q, rtss->rtss_g_nd, mp)) {
1118 				qreply(q, mp);
1119 				return;
1120 			}
1121 			break;
1122 		case TI_GETPEERNAME:
1123 			mi_copyin(q, mp, NULL,
1124 			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
1125 			return;
1126 		default:
1127 			break;
1128 		}
1129 	case M_IOCDATA:
1130 		rts_wput_iocdata(q, mp);
1131 		return;
1132 	default:
1133 		break;
1134 	}
1135 	ip_output(connp, mp, q, IP_WPUT);
1136 }
1137 
1138 /*
1139  * Called by rts_wput_other to handle all M_IOCDATA messages.
1140  */
1141 static void
1142 rts_wput_iocdata(queue_t *q, mblk_t *mp)
1143 {
1144 	conn_t *connp = Q_TO_CONN(q);
1145 	struct sockaddr	*rtsaddr;
1146 	mblk_t	*mp1;
1147 	STRUCT_HANDLE(strbuf, sb);
1148 	struct iocblk	*iocp	= (struct iocblk *)mp->b_rptr;
1149 
1150 	/* Make sure it is one of ours. */
1151 	switch (iocp->ioc_cmd) {
1152 	case TI_GETPEERNAME:
1153 		break;
1154 	default:
1155 		ip_output(connp, mp, q, IP_WPUT);
1156 		return;
1157 	}
1158 	switch (mi_copy_state(q, mp, &mp1)) {
1159 	case -1:
1160 		return;
1161 	case MI_COPY_CASE(MI_COPY_IN, 1):
1162 		break;
1163 	case MI_COPY_CASE(MI_COPY_OUT, 1):
1164 		/* Copy out the strbuf. */
1165 		mi_copyout(q, mp);
1166 		return;
1167 	case MI_COPY_CASE(MI_COPY_OUT, 2):
1168 		/* All done. */
1169 		mi_copy_done(q, mp, 0);
1170 		return;
1171 	default:
1172 		mi_copy_done(q, mp, EPROTO);
1173 		return;
1174 	}
1175 	STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr);
1176 	if (STRUCT_FGET(sb, maxlen) < (int)sizeof (sin_t)) {
1177 		mi_copy_done(q, mp, EINVAL);
1178 		return;
1179 	}
1180 	switch (iocp->ioc_cmd) {
1181 	case TI_GETPEERNAME:
1182 		break;
1183 	default:
1184 		mi_copy_done(q, mp, EPROTO);
1185 		return;
1186 	}
1187 	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), sizeof (sin_t),
1188 	    B_TRUE);
1189 	if (mp1 == NULL)
1190 		return;
1191 	STRUCT_FSET(sb, len, (int)sizeof (sin_t));
1192 	rtsaddr = (struct sockaddr *)mp1->b_rptr;
1193 	mp1->b_wptr = (uchar_t *)&rtsaddr[1];
1194 	bzero(rtsaddr, sizeof (struct sockaddr));
1195 	rtsaddr->sa_family = AF_ROUTE;
1196 	/* Copy out the address */
1197 	mi_copyout(q, mp);
1198 }
1199 
1200 /*ARGSUSED2*/
1201 static void
1202 rts_input(void *arg1, mblk_t *mp, void *arg2)
1203 {
1204 	conn_t *connp = (conn_t *)arg1;
1205 	rts_t	*rts = connp->conn_rts;
1206 	struct iocblk	*iocp;
1207 	mblk_t *mp1;
1208 	struct T_data_ind *tdi;
1209 	int	error;
1210 
1211 	switch (mp->b_datap->db_type) {
1212 	case M_IOCACK:
1213 	case M_IOCNAK:
1214 		iocp = (struct iocblk *)mp->b_rptr;
1215 		if (IPCL_IS_NONSTR(connp)) {
1216 			ASSERT(rts->rts_flag & (RTS_REQ_PENDING));
1217 			mutex_enter(&rts->rts_send_mutex);
1218 			rts->rts_flag &= ~RTS_REQ_INPROG;
1219 			rts->rts_error = iocp->ioc_error;
1220 			cv_signal(&rts->rts_io_cv);
1221 			mutex_exit(&rts->rts_send_mutex);
1222 			freemsg(mp);
1223 			return;
1224 		} else {
1225 			if (rts->rts_flag & (RTS_WPUT_PENDING)) {
1226 				rts->rts_flag &= ~RTS_WPUT_PENDING;
1227 				rts->rts_error = iocp->ioc_error;
1228 				/*
1229 				 * Tell rts_wvw/qwait that we are done.
1230 				 * Note: there is no qwait_wakeup() we can use.
1231 				 */
1232 				qenable(connp->conn_rq);
1233 				freemsg(mp);
1234 				return;
1235 			}
1236 		}
1237 		break;
1238 	case M_DATA:
1239 		/*
1240 		 * Prepend T_DATA_IND to prevent the stream head from
1241 		 * consolidating multiple messages together.
1242 		 * If the allocation fails just send up the M_DATA.
1243 		 */
1244 		mp1 = allocb(sizeof (*tdi), BPRI_MED);
1245 		if (mp1 != NULL) {
1246 			mp1->b_cont = mp;
1247 			mp = mp1;
1248 
1249 			mp->b_datap->db_type = M_PROTO;
1250 			mp->b_wptr += sizeof (*tdi);
1251 			tdi = (struct T_data_ind *)mp->b_rptr;
1252 			tdi->PRIM_type = T_DATA_IND;
1253 			tdi->MORE_flag = 0;
1254 		}
1255 		break;
1256 	default:
1257 		break;
1258 	}
1259 
1260 	if (IPCL_IS_NONSTR(connp)) {
1261 		if ((*connp->conn_upcalls->su_recv)
1262 		    (connp->conn_upper_handle, mp, msgdsize(mp), 0,
1263 		    &error, NULL) < 0) {
1264 			ASSERT(error == ENOSPC);
1265 			/*
1266 			 * Let's confirm hoding the lock that
1267 			 * we are out of recv space.
1268 			 */
1269 			mutex_enter(&rts->rts_recv_mutex);
1270 			if ((*connp->conn_upcalls->su_recv)
1271 			    (connp->conn_upper_handle, NULL, 0, 0,
1272 			    &error, NULL) < 0) {
1273 				ASSERT(error == ENOSPC);
1274 				connp->conn_flow_cntrld = B_TRUE;
1275 			}
1276 			mutex_exit(&rts->rts_recv_mutex);
1277 		}
1278 	} else {
1279 		putnext(connp->conn_rq, mp);
1280 	}
1281 }
1282 
1283 
1284 void
1285 rts_ddi_g_init(void)
1286 {
1287 	rts_max_optsize = optcom_max_optsize(rts_opt_obj.odb_opt_des_arr,
1288 	    rts_opt_obj.odb_opt_arr_cnt);
1289 
1290 	/*
1291 	 * We want to be informed each time a stack is created or
1292 	 * destroyed in the kernel, so we can maintain the
1293 	 * set of rts_stack_t's.
1294 	 */
1295 	netstack_register(NS_RTS, rts_stack_init, NULL, rts_stack_fini);
1296 }
1297 
1298 void
1299 rts_ddi_g_destroy(void)
1300 {
1301 	netstack_unregister(NS_RTS);
1302 }
1303 
1304 #define	INET_NAME	"ip"
1305 
1306 /*
1307  * Initialize the RTS stack instance.
1308  */
1309 /* ARGSUSED */
1310 static void *
1311 rts_stack_init(netstackid_t stackid, netstack_t *ns)
1312 {
1313 	rts_stack_t	*rtss;
1314 	rtsparam_t	*pa;
1315 	int		error = 0;
1316 	major_t		major;
1317 
1318 	rtss = (rts_stack_t *)kmem_zalloc(sizeof (*rtss), KM_SLEEP);
1319 	rtss->rtss_netstack = ns;
1320 
1321 	pa = (rtsparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
1322 	rtss->rtss_params = pa;
1323 	bcopy(lcl_param_arr, rtss->rtss_params, sizeof (lcl_param_arr));
1324 
1325 	(void) rts_param_register(&rtss->rtss_g_nd,
1326 	    rtss->rtss_params, A_CNT(lcl_param_arr));
1327 
1328 	major = mod_name_to_major(INET_NAME);
1329 	error = ldi_ident_from_major(major, &rtss->rtss_ldi_ident);
1330 	ASSERT(error == 0);
1331 	return (rtss);
1332 }
1333 
1334 /*
1335  * Free the RTS stack instance.
1336  */
1337 /* ARGSUSED */
1338 static void
1339 rts_stack_fini(netstackid_t stackid, void *arg)
1340 {
1341 	rts_stack_t *rtss = (rts_stack_t *)arg;
1342 
1343 	nd_free(&rtss->rtss_g_nd);
1344 	kmem_free(rtss->rtss_params, sizeof (lcl_param_arr));
1345 	rtss->rtss_params = NULL;
1346 	ldi_ident_release(rtss->rtss_ldi_ident);
1347 	kmem_free(rtss, sizeof (*rtss));
1348 }
1349 
1350 /* ARGSUSED */
1351 int
1352 rts_accept(sock_lower_handle_t lproto_handle,
1353     sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle,
1354     cred_t *cr)
1355 {
1356 	return (EINVAL);
1357 }
1358 
1359 /* ARGSUSED */
1360 static int
1361 rts_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa,
1362     socklen_t len, cred_t *cr)
1363 {
1364 	/*
1365 	 * rebind not allowed
1366 	 */
1367 	return (EINVAL);
1368 }
1369 
1370 /* ARGSUSED */
1371 int
1372 rts_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr)
1373 {
1374 	return (EINVAL);
1375 }
1376 
1377 /* ARGSUSED */
1378 int
1379 rts_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa,
1380     socklen_t len, sock_connid_t *id, cred_t *cr)
1381 {
1382 	/*
1383 	 * rts sockets start out as bound and connected
1384 	 */
1385 	*id = 0;
1386 	return (EISCONN);
1387 }
1388 
1389 /* ARGSUSED */
1390 int
1391 rts_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr,
1392     socklen_t *addrlen, cred_t *cr)
1393 {
1394 	conn_t *connp = (conn_t *)proto_handle;
1395 	rts_t *rts = connp->conn_rts;
1396 
1397 	ASSERT(rts != NULL);
1398 
1399 	bzero(addr, sizeof (struct sockaddr));
1400 	addr->sa_family = AF_ROUTE;
1401 	*addrlen = sizeof (struct sockaddr);
1402 
1403 	return (0);
1404 }
1405 
1406 /* ARGSUSED */
1407 int
1408 rts_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr,
1409     socklen_t *addrlen, cred_t *cr)
1410 {
1411 	return (EOPNOTSUPP);
1412 }
1413 
1414 static int
1415 rts_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
1416     void *optvalp, socklen_t *optlen, cred_t *cr)
1417 {
1418 	conn_t  	*connp = (conn_t *)proto_handle;
1419 	rts_t		*rts = connp->conn_rts;
1420 	int		error;
1421 	t_uscalar_t	max_optbuf_len;
1422 	void		*optvalp_buf;
1423 	int		len;
1424 
1425 	error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len,
1426 	    rts_opt_obj.odb_opt_des_arr,
1427 	    rts_opt_obj.odb_opt_arr_cnt,
1428 	    rts_opt_obj.odb_topmost_tpiprovider,
1429 	    B_FALSE, B_TRUE, cr);
1430 	if (error != 0) {
1431 		if (error < 0)
1432 			error = proto_tlitosyserr(-error);
1433 		return (error);
1434 	}
1435 
1436 	optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP);
1437 	rw_enter(&rts->rts_rwlock, RW_READER);
1438 	len = rts_opt_get(connp, level, option_name, optvalp_buf);
1439 	rw_exit(&rts->rts_rwlock);
1440 
1441 	if (len < 0) {
1442 		/*
1443 		 * Pass on to IP
1444 		 */
1445 		error = ip_get_options(connp, level, option_name,
1446 		    optvalp, optlen, cr);
1447 	} else {
1448 		/*
1449 		 * update optlen and copy option value
1450 		 */
1451 		t_uscalar_t size = MIN(len, *optlen);
1452 		bcopy(optvalp_buf, optvalp, size);
1453 		bcopy(&size, optlen, sizeof (size));
1454 		error = 0;
1455 	}
1456 
1457 	kmem_free(optvalp_buf, max_optbuf_len);
1458 	return (error);
1459 }
1460 
1461 static int
1462 rts_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
1463     const void *optvalp, socklen_t optlen, cred_t *cr)
1464 {
1465 	conn_t	*connp = (conn_t *)proto_handle;
1466 	rts_t	*rts = connp->conn_rts;
1467 	int	error;
1468 
1469 	error = proto_opt_check(level, option_name, optlen, NULL,
1470 	    rts_opt_obj.odb_opt_des_arr,
1471 	    rts_opt_obj.odb_opt_arr_cnt,
1472 	    rts_opt_obj.odb_topmost_tpiprovider,
1473 	    B_TRUE, B_FALSE, cr);
1474 
1475 	if (error != 0) {
1476 		if (error < 0)
1477 			error = proto_tlitosyserr(-error);
1478 		return (error);
1479 	}
1480 
1481 	rw_enter(&rts->rts_rwlock, RW_WRITER);
1482 	error = rts_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name,
1483 	    optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp,
1484 	    NULL, cr);
1485 	rw_exit(&rts->rts_rwlock);
1486 
1487 	ASSERT(error >= 0);
1488 
1489 	return (error);
1490 }
1491 
1492 /* ARGSUSED */
1493 static int
1494 rts_send(sock_lower_handle_t proto_handle, mblk_t *mp,
1495     struct nmsghdr *msg, cred_t *cr)
1496 {
1497 	mblk_t  *mp1;
1498 	conn_t  *connp = (conn_t *)proto_handle;
1499 	rts_t   *rts = connp->conn_rts;
1500 	rt_msghdr_t	*rtm;
1501 	int error;
1502 
1503 	ASSERT(DB_TYPE(mp) == M_DATA);
1504 	/*
1505 	 * The semantics of the routing socket is such that the rtm_pid
1506 	 * field is automatically filled in during requests with the
1507 	 * current process' pid.  We do this here (where we still have
1508 	 * user context) after checking we have at least a message the
1509 	 * size of a routing message header.
1510 	 */
1511 	if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
1512 		if (!pullupmsg(mp, sizeof (rt_msghdr_t))) {
1513 			rts->rts_error = EINVAL;
1514 			freemsg(mp);
1515 			return (rts->rts_error);
1516 		}
1517 	}
1518 	rtm = (rt_msghdr_t *)mp->b_rptr;
1519 	rtm->rtm_pid = curproc->p_pid;
1520 
1521 	mp1 = rts_ioctl_alloc(mp, DB_CRED(mp));
1522 	if (mp1 == NULL) {
1523 		ASSERT(rts != NULL);
1524 		freemsg(mp);
1525 		return (ENOMEM);
1526 	}
1527 
1528 	/*
1529 	 * Allow only one outstanding request(ioctl) at any given time
1530 	 */
1531 	mutex_enter(&rts->rts_send_mutex);
1532 	while (rts->rts_flag & RTS_REQ_PENDING) {
1533 		int ret;
1534 
1535 		ret = cv_wait_sig(&rts->rts_send_cv, &rts->rts_send_mutex);
1536 		if (ret <= 0) {
1537 			mutex_exit(&rts->rts_send_mutex);
1538 			freemsg(mp);
1539 			return (EINTR);
1540 		}
1541 	}
1542 
1543 	rts->rts_flag |= RTS_REQ_PENDING;
1544 
1545 	rts->rts_flag |= RTS_REQ_INPROG;
1546 
1547 	mutex_exit(&rts->rts_send_mutex);
1548 
1549 	CONN_INC_REF(connp);
1550 
1551 	error = ip_rts_request_common(rts->rts_connp->conn_wq, mp1, connp,
1552 	    DB_CREDDEF(mp, connp->conn_cred));
1553 
1554 	mutex_enter(&rts->rts_send_mutex);
1555 	if (error == EINPROGRESS) {
1556 		ASSERT(rts->rts_flag & RTS_REQ_INPROG);
1557 		if (rts->rts_flag & RTS_REQ_INPROG) {
1558 			/*
1559 			 * Once the request has been issued we wait for
1560 			 * completion
1561 			 */
1562 			cv_wait(&rts->rts_io_cv, &rts->rts_send_mutex);
1563 			error = rts->rts_error;
1564 		}
1565 	}
1566 
1567 	ASSERT((error != 0) || !(rts->rts_flag & RTS_REQ_INPROG));
1568 	ASSERT(MUTEX_HELD(&rts->rts_send_mutex));
1569 
1570 	rts->rts_flag &= ~(RTS_REQ_PENDING | RTS_REQ_INPROG);
1571 	cv_signal(&rts->rts_send_cv);
1572 	mutex_exit(&rts->rts_send_mutex);
1573 	return (error);
1574 }
1575 
1576 /* ARGSUSED */
1577 sock_lower_handle_t
1578 rts_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls,
1579     uint_t *smodep, int *errorp, int flags, cred_t *credp)
1580 {
1581 	conn_t	*connp;
1582 	rts_t	*rts;
1583 	rts_stack_t *rtss;
1584 
1585 	if (family != AF_ROUTE || type != SOCK_RAW ||
1586 	    (proto != 0 && proto != AF_INET && proto != AF_INET6)) {
1587 		*errorp = EPROTONOSUPPORT;
1588 		return (NULL);
1589 	}
1590 
1591 	connp = rts_open(flags, credp);
1592 	ASSERT(connp != NULL);
1593 	connp->conn_flags |= IPCL_NONSTR;
1594 
1595 	rts = connp->conn_rts;
1596 	rtss = rts->rts_rtss;
1597 
1598 	rts->rts_xmit_hiwat = rtss->rtss_xmit_hiwat;
1599 	rts->rts_xmit_lowat = rtss->rtss_xmit_lowat;
1600 	rts->rts_recv_hiwat = rtss->rtss_recv_hiwat;
1601 	rts->rts_recv_lowat = rts_mod_info.mi_lowat;
1602 
1603 	ASSERT(rtss->rtss_ldi_ident != NULL);
1604 
1605 	*errorp = ip_create_helper_stream(connp, rtss->rtss_ldi_ident);
1606 	if (*errorp != 0) {
1607 #ifdef DEBUG
1608 		cmn_err(CE_CONT, "rts_create: create of IP helper stream"
1609 		    " failed\n");
1610 #endif
1611 		(void) rts_close((sock_lower_handle_t)connp, 0, credp);
1612 		return (NULL);
1613 	}
1614 
1615 	mutex_enter(&connp->conn_lock);
1616 	connp->conn_state_flags &= ~CONN_INCIPIENT;
1617 	mutex_exit(&connp->conn_lock);
1618 
1619 	*errorp = 0;
1620 	*smodep = SM_ATOMIC;
1621 	*sock_downcalls = &sock_rts_downcalls;
1622 	return ((sock_lower_handle_t)connp);
1623 }
1624 
1625 /* ARGSUSED */
1626 void
1627 rts_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle,
1628     sock_upcalls_t *sock_upcalls, int flags, cred_t *cr)
1629 {
1630 	conn_t  *connp = (conn_t *)proto_handle;
1631 	rts_t	*rts = connp->conn_rts;
1632 	rts_stack_t *rtss = rts->rts_rtss;
1633 	struct sock_proto_props sopp;
1634 
1635 	connp->conn_upcalls = sock_upcalls;
1636 	connp->conn_upper_handle = sock_handle;
1637 
1638 	sopp.sopp_flags = SOCKOPT_WROFF | SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT |
1639 	    SOCKOPT_MAXBLK | SOCKOPT_MAXPSZ | SOCKOPT_MINPSZ;
1640 	sopp.sopp_wroff = 0;
1641 	sopp.sopp_rxhiwat = rtss->rtss_recv_hiwat;
1642 	sopp.sopp_rxlowat = rts_mod_info.mi_lowat;
1643 	sopp.sopp_maxblk = INFPSZ;
1644 	sopp.sopp_maxpsz = rts_mod_info.mi_maxpsz;
1645 	sopp.sopp_minpsz = (rts_mod_info.mi_minpsz == 1) ? 0 :
1646 	    rts_mod_info.mi_minpsz;
1647 
1648 	(*connp->conn_upcalls->su_set_proto_props)
1649 	    (connp->conn_upper_handle, &sopp);
1650 
1651 	/*
1652 	 * We treat it as already connected for routing socket.
1653 	 */
1654 	(*connp->conn_upcalls->su_connected)
1655 	    (connp->conn_upper_handle, 0, NULL, -1);
1656 
1657 	/*
1658 	 * Indicate the down IP module that this is a routing socket
1659 	 * client by sending an RTS IOCTL without any user data. Although
1660 	 * this is just a notification message (without any real routing
1661 	 * request), we pass in any credential for correctness sake.
1662 	 */
1663 	ip_rts_register(connp);
1664 }
1665 
1666 /* ARGSUSED */
1667 int
1668 rts_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr)
1669 {
1670 	conn_t  *connp = (conn_t *)proto_handle;
1671 
1672 	ASSERT(connp != NULL && IPCL_IS_RTS(connp));
1673 	return (rts_common_close(NULL, connp));
1674 }
1675 
1676 /* ARGSUSED */
1677 int
1678 rts_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr)
1679 {
1680 	conn_t  *connp = (conn_t *)proto_handle;
1681 
1682 	/* shut down the send side */
1683 	if (how != SHUT_RD)
1684 		(*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
1685 		    SOCK_OPCTL_SHUT_SEND, 0);
1686 	/* shut down the recv side */
1687 	if (how != SHUT_WR)
1688 		(*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
1689 		    SOCK_OPCTL_SHUT_RECV, 0);
1690 	return (0);
1691 }
1692 
1693 void
1694 rts_clr_flowctrl(sock_lower_handle_t proto_handle)
1695 {
1696 	conn_t  *connp = (conn_t *)proto_handle;
1697 	rts_t	*rts = connp->conn_rts;
1698 
1699 	mutex_enter(&rts->rts_recv_mutex);
1700 	connp->conn_flow_cntrld = B_FALSE;
1701 	mutex_exit(&rts->rts_recv_mutex);
1702 }
1703 
1704 int
1705 rts_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg,
1706     int mode, int32_t *rvalp, cred_t *cr)
1707 {
1708 	conn_t		*connp = (conn_t *)proto_handle;
1709 	int		error;
1710 
1711 	switch (cmd) {
1712 	case ND_SET:
1713 	case ND_GET:
1714 	case TI_GETPEERNAME:
1715 	case TI_GETMYNAME:
1716 #ifdef DEUG
1717 		cmn_err(CE_CONT, "rts_ioctl cmd 0x%x on non sreams"
1718 		    " socket", cmd);
1719 #endif
1720 		error = EINVAL;
1721 		break;
1722 	default:
1723 		/*
1724 		 * Pass on to IP using helper stream
1725 		 */
1726 		error = ldi_ioctl(connp->conn_helper_info->iphs_handle,
1727 		    cmd, arg, mode, cr, rvalp);
1728 		break;
1729 	}
1730 
1731 	return (error);
1732 }
1733 
1734 sock_downcalls_t sock_rts_downcalls = {
1735 	rts_activate,
1736 	rts_accept,
1737 	rts_bind,
1738 	rts_listen,
1739 	rts_connect,
1740 	rts_getpeername,
1741 	rts_getsockname,
1742 	rts_getsockopt,
1743 	rts_setsockopt,
1744 	rts_send,
1745 	NULL,
1746 	NULL,
1747 	NULL,
1748 	rts_shutdown,
1749 	rts_clr_flowctrl,
1750 	rts_ioctl,
1751 	rts_close
1752 };
1753