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