summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/inet/ip/rts.c
blob: 2751b199939c87e0cfff5291dc930600821cd72e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/stropts.h>
#include <sys/strsun.h>
#include <sys/strlog.h>
#define	_SUN_TPI_VERSION 2
#include <sys/tihdr.h>
#include <sys/timod.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/cmn_err.h>
#include <sys/proc.h>
#include <sys/suntpi.h>
#include <sys/policy.h>
#include <sys/zone.h>
#include <sys/disp.h>

#include <sys/socket.h>
#include <sys/socketvar.h>
#include <netinet/in.h>

#include <inet/common.h>
#include <netinet/ip6.h>
#include <inet/ip.h>
#include <inet/ipclassifier.h>
#include <inet/proto_set.h>
#include <inet/nd.h>
#include <inet/optcom.h>
#include <netinet/ip_mroute.h>
#include <sys/isa_defs.h>
#include <net/route.h>

#include <inet/rts_impl.h>
#include <inet/ip_rts.h>

/*
 * This is a transport provider for routing sockets.  Downstream messages are
 * wrapped with a IP_IOCTL header, and ip_wput_ioctl calls the appropriate entry
 * in the ip_ioctl_ftbl callout table to pass the routing socket data into IP.
 * Upstream messages are generated for listeners of the routing socket as well
 * as the message sender (unless they have turned off their end using
 * SO_USELOOPBACK or shutdown(3n)).  Upstream messages may also be generated
 * asynchronously when:
 *
 *	Interfaces are brought up or down.
 *	Addresses are assigned to interfaces.
 *	ICMP redirects are processed and a IRE_HOST/RTF_DYNAMIC is installed.
 *	No route is found while sending a packet.
 *	When TCP requests IP to remove an IRE_CACHE of a troubled destination.
 *
 * Since all we do is reformat the messages between routing socket and
 * ioctl forms, no synchronization is necessary in this module; all
 * the dirty work is done down in ip.
 */

/* Default structure copied into T_INFO_ACK messages */
static struct T_info_ack rts_g_t_info_ack = {
	T_INFO_ACK,
	T_INFINITE,	/* TSDU_size. Maximum size messages. */
	T_INVALID,	/* ETSDU_size. No expedited data. */
	T_INVALID,	/* CDATA_size. No connect data. */
	T_INVALID,	/* DDATA_size. No disconnect data. */
	0,		/* ADDR_size. */
	0,		/* OPT_size - not initialized here */
	64 * 1024,	/* TIDU_size. rts allows maximum size messages. */
	T_COTS,		/* SERV_type. rts supports connection oriented. */
	TS_UNBND,	/* CURRENT_state. This is set from rts_state. */
	(XPG4_1)	/* PROVIDER_flag */
};

/*
 * Table of ND variables supported by rts. These are loaded into rts_g_nd
 * in rts_open.
 * All of these are alterable, within the min/max values given, at run time.
 */
static rtsparam_t	lcl_param_arr[] = {
	/* min		max		value		name */
	{ 4096,		65536,		8192,		"rts_xmit_hiwat"},
	{ 0,		65536,		1024,		"rts_xmit_lowat"},
	{ 4096,		65536,		8192,		"rts_recv_hiwat"},
	{ 65536,	1024*1024*1024, 256*1024,	"rts_max_buf"},
};
#define	rtss_xmit_hiwat		rtss_params[0].rts_param_value
#define	rtss_xmit_lowat		rtss_params[1].rts_param_value
#define	rtss_recv_hiwat		rtss_params[2].rts_param_value
#define	rtss_max_buf		rtss_params[3].rts_param_value

static void 	rts_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
    int sys_error);
static void	rts_input(void *, mblk_t *, void *);
static mblk_t	*rts_ioctl_alloc(mblk_t *data, cred_t *cr);
static int	rts_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr);
static boolean_t rts_param_register(IDP *ndp, rtsparam_t *rtspa, int cnt);
static int	rts_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp,
    cred_t *cr);
static void	rts_rsrv(queue_t *q);
static void	*rts_stack_init(netstackid_t stackid, netstack_t *ns);
static void	rts_stack_fini(netstackid_t stackid, void *arg);
static void	rts_wput(queue_t *q, mblk_t *mp);
static void	rts_wput_iocdata(queue_t *q, mblk_t *mp);
static void 	rts_wput_other(queue_t *q, mblk_t *mp);
static int	rts_wrw(queue_t *q, struiod_t *dp);

static int	rts_stream_open(queue_t *q, dev_t *devp, int flag, int sflag,
		    cred_t *credp);
static conn_t	*rts_open(int flag, cred_t *credp);

static int	rts_stream_close(queue_t *q);
static int	rts_close(sock_lower_handle_t proto_handle, int flags,
		    cred_t *cr);

static struct module_info rts_mod_info = {
	129, "rts", 1, INFPSZ, 512, 128
};

static struct qinit rtsrinit = {
	NULL, (pfi_t)rts_rsrv, rts_stream_open, rts_stream_close, NULL,
	&rts_mod_info
};

static struct qinit rtswinit = {
	(pfi_t)rts_wput, NULL, NULL, NULL, NULL, &rts_mod_info,
	NULL, (pfi_t)rts_wrw, NULL, STRUIOT_STANDARD
};

struct streamtab rtsinfo = {
	&rtsrinit, &rtswinit
};

/*
 * This routine allocates the necessary
 * message blocks for IOCTL wrapping the
 * user data.
 */
static mblk_t *
rts_ioctl_alloc(mblk_t *data, cred_t *cr)
{
	mblk_t	*mp = NULL;
	mblk_t	*mp1 = NULL;
	ipllc_t	*ipllc;
	struct iocblk	*ioc;

	mp = allocb_cred(sizeof (ipllc_t), cr);
	if (mp == NULL)
		return (NULL);
	mp1 = allocb_cred(sizeof (struct iocblk), cr);
	if (mp1 == NULL) {
		freeb(mp);
		return (NULL);
	}

	ipllc = (ipllc_t *)mp->b_rptr;
	ipllc->ipllc_cmd = IP_IOC_RTS_REQUEST;
	ipllc->ipllc_name_offset = 0;
	ipllc->ipllc_name_length = 0;
	mp->b_wptr += sizeof (ipllc_t);
	mp->b_cont = data;

	ioc = (struct iocblk *)mp1->b_rptr;
	ioc->ioc_cmd = IP_IOCTL;
	ioc->ioc_error = 0;
	ioc->ioc_cr = NULL;
	ioc->ioc_count = msgdsize(mp);
	mp1->b_wptr += sizeof (struct iocblk);
	mp1->b_datap->db_type = M_IOCTL;
	mp1->b_cont = mp;

	return (mp1);
}

/*
 * This routine closes rts stream, by disabling
 * put/srv routines and freeing the this module
 * internal datastructure.
 */
static int
rts_common_close(queue_t *q, conn_t *connp)
{

	ASSERT(connp != NULL && IPCL_IS_RTS(connp));

	ip_rts_unregister(connp);

	ip_quiesce_conn(connp);

	if (!IPCL_IS_NONSTR(connp)) {
		qprocsoff(q);

		/*
		 * Now we are truly single threaded on this stream, and can
		 * delete the things hanging off the connp, and finally the
		 * connp.
		 * We removed this connp from the fanout list, it cannot be
		 * accessed thru the fanouts, and we already waited for the
		 * conn_ref to drop to 0. We are already in close, so
		 * there cannot be any other thread from the top. qprocsoff
		 * has completed, and service has completed or won't run in
		 * future.
		 */
		inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
	} else {
		ip_free_helper_stream(connp);
	}
	ASSERT(connp->conn_ref == 1);


	connp->conn_ref--;
	ipcl_conn_destroy(connp);

	return (0);
}

static int
rts_stream_close(queue_t *q)
{
	conn_t  *connp = Q_TO_CONN(q);

	(void) rts_common_close(q, connp);
	q->q_ptr = WR(q)->q_ptr = NULL;
	return (0);
}

/*
 * This is the open routine for routing socket. It allocates
 * rts_t structure for the stream and tells IP that it is a routing socket.
 */
/* ARGSUSED */
static int
rts_stream_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
{
	conn_t *connp;
	dev_t	conn_dev;
	rts_stack_t *rtss;
	rts_t   *rts;

	/* If the stream is already open, return immediately. */
	if (q->q_ptr != NULL)
		return (0);

	if (sflag == MODOPEN)
		return (EINVAL);


	/*
	 * Since RTS is not used so heavily, allocating from the small
	 * arena should be sufficient.
	 */
	if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) {
		return (EBUSY);
	}

	connp = rts_open(flag, credp);
	ASSERT(connp != NULL);


	*devp = makedevice(getemajor(*devp), (minor_t)conn_dev);

	rts = connp->conn_rts;

	rw_enter(&rts->rts_rwlock, RW_WRITER);
	connp->conn_dev = conn_dev;
	connp->conn_minor_arena = ip_minor_arena_sa;

	/*
	 * Initialize the rts_t structure for this stream.
	 */
	q->q_ptr = connp;
	WR(q)->q_ptr = connp;
	connp->conn_rq = q;
	connp->conn_wq = WR(q);

	rtss = rts->rts_rtss;
	q->q_hiwat = rtss->rtss_recv_hiwat;
	WR(q)->q_hiwat = rtss->rtss_xmit_hiwat;
	WR(q)->q_lowat = rtss->rtss_xmit_lowat;



	mutex_enter(&connp->conn_lock);
	connp->conn_state_flags &= ~CONN_INCIPIENT;
	mutex_exit(&connp->conn_lock);

	qprocson(q);
	rw_exit(&rts->rts_rwlock);
	/*
	 * Indicate the down IP module that this is a routing socket
	 * client by sending an RTS IOCTL without any user data. Although
	 * this is just a notification message (without any real routing
	 * request), we pass in any credential for correctness sake.
	 */
	ip_rts_register(connp);

	return (0);
}

/* ARGSUSED */
static conn_t *
rts_open(int flag, cred_t *credp)
{
	netstack_t *ns;
	rts_stack_t *rtss;
	rts_t	*rts;
	conn_t	*connp;
	zoneid_t zoneid;

	ns = netstack_find_by_cred(credp);
	ASSERT(ns != NULL);
	rtss = ns->netstack_rts;
	ASSERT(rtss != NULL);

	/*
	 * For exclusive stacks we set the zoneid to zero
	 * to make RTS operate as if in the global zone.
	 */
	if (ns->netstack_stackid != GLOBAL_NETSTACKID)
		zoneid = GLOBAL_ZONEID;
	else
		zoneid = crgetzoneid(credp);

	connp = ipcl_conn_create(IPCL_RTSCONN, KM_SLEEP, ns);
	rts = connp->conn_rts;

	/*
	 * ipcl_conn_create did a netstack_hold. Undo the hold that was
	 * done by netstack_find_by_cred()
	 */
	netstack_rele(ns);


	rw_enter(&rts->rts_rwlock, RW_WRITER);
	ASSERT(connp->conn_rts == rts);
	ASSERT(rts->rts_connp == connp);

	connp->conn_zoneid = zoneid;
	connp->conn_flow_cntrld = B_FALSE;

	connp->conn_ulp_labeled = is_system_labeled();

	rts->rts_rtss = rtss;
	rts->rts_xmit_hiwat = rtss->rtss_xmit_hiwat;

	connp->conn_recv = rts_input;
	crhold(credp);
	connp->conn_cred = credp;

	/*
	 * rts sockets start out as bound and connected
	 * For streams based sockets, socket state is set to
	 * SS_ISBOUND | SS_ISCONNECTED in so_strinit.
	 */
	rts->rts_state = TS_DATA_XFER;
	rw_exit(&rts->rts_rwlock);

	return (connp);
}

/*
 * This routine creates a T_ERROR_ACK message and passes it upstream.
 */
static void
rts_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
{
	if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
		qreply(q, mp);
}

/*
 * This routine creates a T_OK_ACK message and passes it upstream.
 */
static void
rts_ok_ack(queue_t *q, mblk_t *mp)
{
	if ((mp = mi_tpi_ok_ack_alloc(mp)) != NULL)
		qreply(q, mp);
}

/*
 * This routine is called by rts_wput to handle T_UNBIND_REQ messages.
 */
static void
rts_tpi_unbind(queue_t *q, mblk_t *mp)
{
	conn_t	*connp = Q_TO_CONN(q);
	rts_t	*rts = connp->conn_rts;

	/* If a bind has not been done, we can't unbind. */
	if (rts->rts_state != TS_IDLE) {
		rts_err_ack(q, mp, TOUTSTATE, 0);
		return;
	}
	rts->rts_state = TS_UNBND;
	rts_ok_ack(q, mp);
}

/*
 * This routine is called to handle each
 * O_T_BIND_REQ/T_BIND_REQ message passed to
 * rts_wput. Note: This routine works with both
 * O_T_BIND_REQ and T_BIND_REQ semantics.
 */
static void
rts_tpi_bind(queue_t *q, mblk_t *mp)
{
	conn_t	*connp = Q_TO_CONN(q);
	rts_t	*rts = connp->conn_rts;
	mblk_t	*mp1;
	struct T_bind_req *tbr;

	if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
		    "rts_tpi_bind: bad data, %d", rts->rts_state);
		rts_err_ack(q, mp, TBADADDR, 0);
		return;
	}
	if (rts->rts_state != TS_UNBND) {
		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
		    "rts_tpi_bind: bad state, %d", rts->rts_state);
		rts_err_ack(q, mp, TOUTSTATE, 0);
		return;
	}
	/*
	 * Reallocate the message to make sure we have enough room for an
	 * address and the protocol type.
	 */
	mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin_t), 1);
	if (mp1 == NULL) {
		rts_err_ack(q, mp, TSYSERR, ENOMEM);
		return;
	}
	mp = mp1;
	tbr = (struct T_bind_req *)mp->b_rptr;
	if (tbr->ADDR_length != 0) {
		(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
		    "rts_tpi_bind: bad ADDR_length %d", tbr->ADDR_length);
		rts_err_ack(q, mp, TBADADDR, 0);
		return;
	}
	/* Generic request */
	tbr->ADDR_offset = (t_scalar_t)sizeof (struct T_bind_req);
	tbr->ADDR_length = 0;
	tbr->PRIM_type = T_BIND_ACK;
	rts->rts_state = TS_IDLE;
	qreply(q, mp);
}

static void
rts_copy_info(struct T_info_ack *tap, rts_t *rts)
{
	*tap = rts_g_t_info_ack;
	tap->CURRENT_state = rts->rts_state;
	tap->OPT_size = rts_max_optsize;
}

/*
 * This routine responds to T_CAPABILITY_REQ messages.  It is called by
 * rts_wput.  Much of the T_CAPABILITY_ACK information is copied from
 * rts_g_t_info_ack.  The current state of the stream is copied from
 * rts_state.
 */
static void
rts_capability_req(queue_t *q, mblk_t *mp)
{
	conn_t	*connp = Q_TO_CONN(q);
	rts_t	*rts = connp->conn_rts;
	t_uscalar_t		cap_bits1;
	struct T_capability_ack	*tcap;

	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;

	mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
	    mp->b_datap->db_type, T_CAPABILITY_ACK);
	if (mp == NULL)
		return;

	tcap = (struct T_capability_ack *)mp->b_rptr;
	tcap->CAP_bits1 = 0;

	if (cap_bits1 & TC1_INFO) {
		rts_copy_info(&tcap->INFO_ack, rts);
		tcap->CAP_bits1 |= TC1_INFO;
	}

	qreply(q, mp);
}

/*
 * This routine responds to T_INFO_REQ messages.  It is called by rts_wput.
 * Most of the T_INFO_ACK information is copied from rts_g_t_info_ack.
 * The current state of the stream is copied from rts_state.
 */
static void
rts_info_req(queue_t *q, mblk_t *mp)
{
	conn_t	*connp = Q_TO_CONN(q);
	rts_t	*rts = connp->conn_rts;

	mp = tpi_ack_alloc(mp, sizeof (rts_g_t_info_ack), M_PCPROTO,
	    T_INFO_ACK);
	if (mp == NULL)
		return;
	rts_copy_info((struct T_info_ack *)mp->b_rptr, rts);
	qreply(q, mp);
}

/*
 * This routine gets default values of certain options whose default
 * values are maintained by protcol specific code
 */
/* ARGSUSED */
int
rts_opt_default(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
{
	/* no default value processed by protocol specific code currently */
	return (-1);
}


static int
rts_opt_get(conn_t *connp, int level, int name, uchar_t *ptr)
{
	rts_t	*rts = connp->conn_rts;
	int	*i1 = (int *)ptr;

	ASSERT(RW_READ_HELD(&rts->rts_rwlock));

	switch (level) {
	case SOL_SOCKET:
		switch (name) {
		case SO_DEBUG:
			*i1 = rts->rts_debug;
			break;
		case SO_REUSEADDR:
			*i1 = rts->rts_reuseaddr;
			break;
		case SO_TYPE:
			*i1 = SOCK_RAW;
			break;
		/*
		 * The following three items are available here,
		 * but are only meaningful to IP.
		 */
		case SO_DONTROUTE:
			*i1 = rts->rts_dontroute;
			break;
		case SO_USELOOPBACK:
			*i1 = rts->rts_useloopback;
			break;
		case SO_BROADCAST:
			*i1 = rts->rts_broadcast;
			break;
		case SO_PROTOTYPE:
			*i1 = rts->rts_proto;
			break;
		/*
		 * The following two items can be manipulated,
		 * but changing them should do nothing.
		 */
		case SO_SNDBUF:
			ASSERT(rts->rts_xmit_hiwat <= INT_MAX);
			*i1 = (int)(rts->rts_xmit_hiwat);
			break;
		case SO_RCVBUF:
			ASSERT(rts->rts_recv_hiwat <= INT_MAX);
			*i1 = (int)(rts->rts_recv_hiwat);
			break;
		case SO_DOMAIN:
			*i1 = PF_ROUTE;
			break;
		default:
			return (-1);
		}
		break;
	case SOL_ROUTE:
		switch (name) {
		case RT_AWARE:
			mutex_enter(&connp->conn_lock);
			*i1 = connp->conn_rtaware;
			mutex_exit(&connp->conn_lock);
			break;
		}
		break;
	default:
		return (-1);
	}
	return ((int)sizeof (int));
}

/* ARGSUSED */
static int
rts_do_opt_set(conn_t *connp, int level, int name, uint_t inlen,
    uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, cred_t *cr,
    void *thisdg_attrs, boolean_t checkonly)
{
	int	*i1 = (int *)invalp;
	rts_t	*rts = connp->conn_rts;
	rts_stack_t	*rtss = rts->rts_rtss;

	ASSERT(RW_WRITE_HELD(&rts->rts_rwlock));

	/*
	 * For rts, we should have no ancillary data sent down
	 * (rts_wput doesn't handle options).
	 */
	ASSERT(thisdg_attrs == NULL);

	/*
	 * For fixed length options, no sanity check
	 * of passed in length is done. It is assumed *_optcom_req()
	 * routines do the right thing.
	 */

	switch (level) {
	case SOL_SOCKET:
		switch (name) {
		case SO_REUSEADDR:
			if (!checkonly)
				rts->rts_reuseaddr = *i1;
			break;	/* goto sizeof (int) option return */
		case SO_DEBUG:
			if (!checkonly)
				rts->rts_debug = *i1;
			break;	/* goto sizeof (int) option return */
		/*
		 * The following three items are available here,
		 * but are only meaningful to IP.
		 */
		case SO_DONTROUTE:
			if (!checkonly)
				rts->rts_dontroute = *i1;
			break;	/* goto sizeof (int) option return */
		case SO_USELOOPBACK:
			if (!checkonly)
				rts->rts_useloopback = *i1;
			break;	/* goto sizeof (int) option return */
		case SO_BROADCAST:
			if (!checkonly)
				rts->rts_broadcast = *i1;
			break;	/* goto sizeof (int) option return */
		case SO_PROTOTYPE:
			/*
			 * Routing socket applications that call socket() with
			 * a third argument can filter which messages will be
			 * sent upstream thanks to sockfs.  so_socket() sends
			 * down the SO_PROTOTYPE and rts_queue_input()
			 * implements the filtering.
			 */
			if (*i1 != AF_INET && *i1 != AF_INET6)
				return (EPROTONOSUPPORT);
			if (!checkonly)
				rts->rts_proto = *i1;
			break;	/* goto sizeof (int) option return */
		/*
		 * The following two items can be manipulated,
		 * but changing them should do nothing.
		 */
		case SO_SNDBUF:
			if (*i1 > rtss->rtss_max_buf) {
				*outlenp = 0;
				return (ENOBUFS);
			}
			if (!checkonly) {
				rts->rts_xmit_hiwat = *i1;
				if (!IPCL_IS_NONSTR(connp))
					connp->conn_wq->q_hiwat = *i1;
			}
			break;	/* goto sizeof (int) option return */
		case SO_RCVBUF:
			if (*i1 > rtss->rtss_max_buf) {
				*outlenp = 0;
				return (ENOBUFS);
			}
			if (!checkonly) {
				rts->rts_recv_hiwat = *i1;
				rw_exit(&rts->rts_rwlock);
				(void) proto_set_rx_hiwat(connp->conn_rq, connp,
				    *i1);
				rw_enter(&rts->rts_rwlock, RW_WRITER);
			}

			break;	/* goto sizeof (int) option return */
		default:
			*outlenp = 0;
			return (EINVAL);
		}
		break;
	case SOL_ROUTE:
		switch (name) {
		case RT_AWARE:
			if (!checkonly) {
				mutex_enter(&connp->conn_lock);
				connp->conn_rtaware = *i1;
				mutex_exit(&connp->conn_lock);
			}
			break;	/* goto sizeof (int) option return */
		default:
			*outlenp = 0;
			return (EINVAL);
		}
		break;
	default:
		*outlenp = 0;
		return (EINVAL);
	}
	/*
	 * Common case of return from an option that is sizeof (int)
	 */
	if (invalp != outvalp) {
		/* don't trust bcopy for identical src/dst */
		(void) bcopy(invalp, outvalp, inlen);
	}
	*outlenp = (t_uscalar_t)sizeof (int);
	return (0);
}

static int
rts_opt_set(conn_t *connp, uint_t optset_context, int level, int name,
    uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
    void *thisdg_attrs, cred_t *cr)
{
	boolean_t 	checkonly = B_FALSE;

	if (optset_context) {
		switch (optset_context) {
		case SETFN_OPTCOM_CHECKONLY:
			checkonly = B_TRUE;
			/*
			 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
			 * inlen != 0 implies value supplied and
			 * 	we have to "pretend" to set it.
			 * inlen == 0 implies that there is no value part
			 * 	in T_CHECK request and just validation
			 * done elsewhere should be enough, we just return here.
			 */
			if (inlen == 0) {
				*outlenp = 0;
				return (0);
			}
			break;
		case SETFN_OPTCOM_NEGOTIATE:
			checkonly = B_FALSE;
			break;
		case SETFN_UD_NEGOTIATE:
		case SETFN_CONN_NEGOTIATE:
			checkonly = B_FALSE;
			/*
			 * Negotiating local and "association-related" options
			 * through T_UNITDATA_REQ or T_CONN_{REQ,CON}
			 * Not allowed in this module.
			 */
			return (EINVAL);
		default:
			/*
			 * We should never get here
			 */
			*outlenp = 0;
			return (EINVAL);
		}

		ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
		    (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));

	}
	return (rts_do_opt_set(connp, level, name, inlen, invalp, outlenp,
	    outvalp, cr, thisdg_attrs, checkonly));

}

/*
 * This routine retrieves the current status of socket options.
 * It returns the size of the option retrieved.
 */
int
rts_tpi_opt_get(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
{
	rts_t	*rts;
	int	err;

	rts = Q_TO_RTS(q);
	rw_enter(&rts->rts_rwlock, RW_READER);
	err = rts_opt_get(Q_TO_CONN(q), level, name, ptr);
	rw_exit(&rts->rts_rwlock);
	return (err);
}

/*
 * This routine sets socket options.
 */
/*ARGSUSED*/
int
rts_tpi_opt_set(queue_t *q, uint_t optset_context, int level,
    int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp,
    uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, mblk_t *mblk)
{
	conn_t	*connp = Q_TO_CONN(q);
	int	error;
	rts_t	*rts = connp->conn_rts;


	rw_enter(&rts->rts_rwlock, RW_WRITER);
	error = rts_opt_set(connp, optset_context, level, name, inlen, invalp,
	    outlenp, outvalp, thisdg_attrs, cr);
	rw_exit(&rts->rts_rwlock);
	return (error);
}

/*
 * This routine retrieves the value of an ND variable in a rtsparam_t
 * structure. It is called through nd_getset when a user reads the
 * variable.
 */
/* ARGSUSED */
static int
rts_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr)
{
	rtsparam_t	*rtspa = (rtsparam_t *)cp;

	(void) mi_mpprintf(mp, "%u", rtspa->rts_param_value);
	return (0);
}

/*
 * Walk through the param array specified registering each element with the
 * named dispatch (ND) handler.
 */
static boolean_t
rts_param_register(IDP *ndp, rtsparam_t *rtspa, int cnt)
{
	for (; cnt-- > 0; rtspa++) {
		if (rtspa->rts_param_name != NULL && rtspa->rts_param_name[0]) {
			if (!nd_load(ndp, rtspa->rts_param_name,
			    rts_param_get, rts_param_set, (caddr_t)rtspa)) {
				nd_free(ndp);
				return (B_FALSE);
			}
		}
	}
	return (B_TRUE);
}

/* This routine sets an ND variable in a rtsparam_t structure. */
/* ARGSUSED */
static int
rts_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr)
{
	ulong_t	new_value;
	rtsparam_t	*rtspa = (rtsparam_t *)cp;

	/*
	 * Fail the request if the new value does not lie within the
	 * required bounds.
	 */
	if (ddi_strtoul(value, NULL, 10, &new_value) != 0 ||
	    new_value < rtspa->rts_param_min ||
	    new_value > rtspa->rts_param_max) {
		return (EINVAL);
	}

	/* Set the new value */
	rtspa->rts_param_value = new_value;
	return (0);
}

/*
 * Empty rsrv routine which is used by rts_input to cause a wakeup
 * of a thread in qwait.
 */
/*ARGSUSED*/
static void
rts_rsrv(queue_t *q)
{
}

/*
 * This routine handles synchronous messages passed downstream. It either
 * consumes the message or passes it downstream; it never queues a
 * a message. The data messages that go down are wrapped in an IOCTL
 * message.
 *
 * Since it is synchronous, it waits for the M_IOCACK/M_IOCNAK so that
 * it can return an immediate error (such as ENETUNREACH when adding a route).
 * It uses the RTS_WRW_PENDING to ensure that each rts instance has only
 * one M_IOCTL outstanding at any given time.
 */
static int
rts_wrw(queue_t *q, struiod_t *dp)
{
	mblk_t	*mp = dp->d_mp;
	mblk_t	*mp1;
	int	error;
	rt_msghdr_t	*rtm;
	conn_t	*connp = Q_TO_CONN(q);
	rts_t	*rts = connp->conn_rts;

	while (rts->rts_flag & RTS_WRW_PENDING) {
		if (qwait_rw(q)) {
			rts->rts_error = EINTR;
			goto err_ret;
		}
	}
	rts->rts_flag |= RTS_WRW_PENDING;

	if (isuioq(q) && (error = struioget(q, mp, dp, 0))) {
		/*
		 * Uio error of some sort, so just return the error.
		 */
		rts->rts_error = error;
		goto err_ret;
	}
	/*
	 * Pass the mblk (chain) onto wput().
	 */
	dp->d_mp = 0;

	switch (mp->b_datap->db_type) {
	case M_PROTO:
	case M_PCPROTO:
		/* Expedite other than T_DATA_REQ to below the switch */
		if (((mp->b_wptr - mp->b_rptr) !=
		    sizeof (struct T_data_req)) ||
		    (((union T_primitives *)mp->b_rptr)->type != T_DATA_REQ))
			break;
		if ((mp1 = mp->b_cont) == NULL) {
			rts->rts_error = EINVAL;
			goto err_ret;
		}
		freeb(mp);
		mp = mp1;
		/* FALLTHRU */
	case M_DATA:
		/*
		 * The semantics of the routing socket is such that the rtm_pid
		 * field is automatically filled in during requests with the
		 * current process' pid.  We do this here (where we still have
		 * user context) after checking we have at least a message the
		 * size of a routing message header.
		 */
		if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
			if (!pullupmsg(mp, sizeof (rt_msghdr_t))) {
				rts->rts_error = EINVAL;
				goto err_ret;
			}
		}
		rtm = (rt_msghdr_t *)mp->b_rptr;
		rtm->rtm_pid = curproc->p_pid;
		break;
	default:
		break;
	}
	rts->rts_flag |= RTS_WPUT_PENDING;
	rts_wput(q, mp);
	while (rts->rts_flag & RTS_WPUT_PENDING)
		if (qwait_rw(q)) {
			/* RTS_WPUT_PENDING will be cleared below */
			rts->rts_error = EINTR;
			break;
		}
err_ret:
	rts->rts_flag &= ~(RTS_WPUT_PENDING | RTS_WRW_PENDING);
	return (rts->rts_error);
}

/*
 * This routine handles all messages passed downstream. It either
 * consumes the message or passes it downstream; it never queues a
 * a message. The data messages that go down are wrapped in an IOCTL
 * message.
 *
 * FIXME? Should we call IP rts_request directly? Could punt on returning
 * errno in the case when it defers processing due to
 * IPIF_CHANGING/ILL_CHANGING???
 */
static void
rts_wput(queue_t *q, mblk_t *mp)
{
	uchar_t	*rptr = mp->b_rptr;
	mblk_t	*mp1;
	conn_t	*connp = Q_TO_CONN(q);
	rts_t	*rts = connp->conn_rts;

	switch (mp->b_datap->db_type) {
	case M_DATA:
		break;
	case M_PROTO:
	case M_PCPROTO:
		if ((mp->b_wptr - rptr) == sizeof (struct T_data_req)) {
			/* Expedite valid T_DATA_REQ to below the switch */
			if (((union T_primitives *)rptr)->type == T_DATA_REQ) {
				mp1 = mp->b_cont;
				freeb(mp);
				if (mp1 == NULL)
					return;
				mp = mp1;
				break;
			}
		}
		/* FALLTHRU */
	default:
		rts_wput_other(q, mp);
		return;
	}


	mp1 = rts_ioctl_alloc(mp, DB_CRED(mp));
	if (mp1 == NULL) {
		ASSERT(rts != NULL);
		freemsg(mp);
		if (rts->rts_flag & RTS_WPUT_PENDING) {
			rts->rts_error = ENOMEM;
			rts->rts_flag &= ~RTS_WPUT_PENDING;
		}
		return;
	}
	ip_output(connp, mp1, q, IP_WPUT);
}


/*
 * Handles all the control message, if it
 * can not understand it, it will
 * pass down stream.
 */
static void
rts_wput_other(queue_t *q, mblk_t *mp)
{
	conn_t	*connp = Q_TO_CONN(q);
	rts_t	*rts = connp->conn_rts;
	uchar_t	*rptr = mp->b_rptr;
	struct iocblk	*iocp;
	cred_t	*cr;
	rts_stack_t	*rtss;

	rtss = rts->rts_rtss;

	cr = DB_CREDDEF(mp, connp->conn_cred);

	switch (mp->b_datap->db_type) {
	case M_PROTO:
	case M_PCPROTO:
		if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) {
			/*
			 * If the message does not contain a PRIM_type,
			 * throw it away.
			 */
			freemsg(mp);
			return;
		}
		switch (((union T_primitives *)rptr)->type) {
		case T_BIND_REQ:
		case O_T_BIND_REQ:
			rts_tpi_bind(q, mp);
			return;
		case T_UNBIND_REQ:
			rts_tpi_unbind(q, mp);
			return;
		case T_CAPABILITY_REQ:
			rts_capability_req(q, mp);
			return;
		case T_INFO_REQ:
			rts_info_req(q, mp);
			return;
		case T_SVR4_OPTMGMT_REQ:
			(void) svr4_optcom_req(q, mp, cr, &rts_opt_obj,
			    B_TRUE);
			return;
		case T_OPTMGMT_REQ:
			(void) tpi_optcom_req(q, mp, cr, &rts_opt_obj, B_TRUE);
			return;
		case O_T_CONN_RES:
		case T_CONN_RES:
		case T_DISCON_REQ:
			/* Not supported by rts. */
			rts_err_ack(q, mp, TNOTSUPPORT, 0);
			return;
		case T_DATA_REQ:
		case T_EXDATA_REQ:
		case T_ORDREL_REQ:
			/* Illegal for rts. */
			freemsg(mp);
			(void) putnextctl1(RD(q), M_ERROR, EPROTO);
			return;

		default:
			break;
		}
		break;
	case M_IOCTL:
		iocp = (struct iocblk *)mp->b_rptr;
		switch (iocp->ioc_cmd) {
		case ND_SET:
		case ND_GET:
			if (nd_getset(q, rtss->rtss_g_nd, mp)) {
				qreply(q, mp);
				return;
			}
			break;
		case TI_GETPEERNAME:
			mi_copyin(q, mp, NULL,
			    SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
			return;
		default:
			break;
		}
	case M_IOCDATA:
		rts_wput_iocdata(q, mp);
		return;
	default:
		break;
	}
	ip_output(connp, mp, q, IP_WPUT);
}

/*
 * Called by rts_wput_other to handle all M_IOCDATA messages.
 */
static void
rts_wput_iocdata(queue_t *q, mblk_t *mp)
{
	conn_t *connp = Q_TO_CONN(q);
	struct sockaddr	*rtsaddr;
	mblk_t	*mp1;
	STRUCT_HANDLE(strbuf, sb);
	struct iocblk	*iocp	= (struct iocblk *)mp->b_rptr;

	/* Make sure it is one of ours. */
	switch (iocp->ioc_cmd) {
	case TI_GETPEERNAME:
		break;
	default:
		ip_output(connp, mp, q, IP_WPUT);
		return;
	}
	switch (mi_copy_state(q, mp, &mp1)) {
	case -1:
		return;
	case MI_COPY_CASE(MI_COPY_IN, 1):
		break;
	case MI_COPY_CASE(MI_COPY_OUT, 1):
		/* Copy out the strbuf. */
		mi_copyout(q, mp);
		return;
	case MI_COPY_CASE(MI_COPY_OUT, 2):
		/* All done. */
		mi_copy_done(q, mp, 0);
		return;
	default:
		mi_copy_done(q, mp, EPROTO);
		return;
	}
	STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr);
	if (STRUCT_FGET(sb, maxlen) < (int)sizeof (sin_t)) {
		mi_copy_done(q, mp, EINVAL);
		return;
	}
	switch (iocp->ioc_cmd) {
	case TI_GETPEERNAME:
		break;
	default:
		mi_copy_done(q, mp, EPROTO);
		return;
	}
	mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), sizeof (sin_t),
	    B_TRUE);
	if (mp1 == NULL)
		return;
	STRUCT_FSET(sb, len, (int)sizeof (sin_t));
	rtsaddr = (struct sockaddr *)mp1->b_rptr;
	mp1->b_wptr = (uchar_t *)&rtsaddr[1];
	bzero(rtsaddr, sizeof (struct sockaddr));
	rtsaddr->sa_family = AF_ROUTE;
	/* Copy out the address */
	mi_copyout(q, mp);
}

/*ARGSUSED2*/
static void
rts_input(void *arg1, mblk_t *mp, void *arg2)
{
	conn_t *connp = (conn_t *)arg1;
	rts_t	*rts = connp->conn_rts;
	struct iocblk	*iocp;
	mblk_t *mp1;
	struct T_data_ind *tdi;
	int	error;

	switch (mp->b_datap->db_type) {
	case M_IOCACK:
	case M_IOCNAK:
		iocp = (struct iocblk *)mp->b_rptr;
		if (IPCL_IS_NONSTR(connp)) {
			ASSERT(rts->rts_flag & (RTS_REQ_PENDING));
			mutex_enter(&rts->rts_send_mutex);
			rts->rts_flag &= ~RTS_REQ_INPROG;
			rts->rts_error = iocp->ioc_error;
			cv_signal(&rts->rts_io_cv);
			mutex_exit(&rts->rts_send_mutex);
			freemsg(mp);
			return;
		} else {
			if (rts->rts_flag & (RTS_WPUT_PENDING)) {
				rts->rts_flag &= ~RTS_WPUT_PENDING;
				rts->rts_error = iocp->ioc_error;
				/*
				 * Tell rts_wvw/qwait that we are done.
				 * Note: there is no qwait_wakeup() we can use.
				 */
				qenable(connp->conn_rq);
				freemsg(mp);
				return;
			}
		}
		break;
	case M_DATA:
		/*
		 * Prepend T_DATA_IND to prevent the stream head from
		 * consolidating multiple messages together.
		 * If the allocation fails just send up the M_DATA.
		 */
		mp1 = allocb(sizeof (*tdi), BPRI_MED);
		if (mp1 != NULL) {
			mp1->b_cont = mp;
			mp = mp1;

			mp->b_datap->db_type = M_PROTO;
			mp->b_wptr += sizeof (*tdi);
			tdi = (struct T_data_ind *)mp->b_rptr;
			tdi->PRIM_type = T_DATA_IND;
			tdi->MORE_flag = 0;
		}
		break;
	default:
		break;
	}

	if (IPCL_IS_NONSTR(connp)) {
		if ((*connp->conn_upcalls->su_recv)
		    (connp->conn_upper_handle, mp, msgdsize(mp), 0,
		    &error, NULL) < 0) {
			ASSERT(error == ENOSPC);
			/*
			 * Let's confirm hoding the lock that
			 * we are out of recv space.
			 */
			mutex_enter(&rts->rts_recv_mutex);
			if ((*connp->conn_upcalls->su_recv)
			    (connp->conn_upper_handle, NULL, 0, 0,
			    &error, NULL) < 0) {
				ASSERT(error == ENOSPC);
				connp->conn_flow_cntrld = B_TRUE;
			}
			mutex_exit(&rts->rts_recv_mutex);
		}
	} else {
		putnext(connp->conn_rq, mp);
	}
}


void
rts_ddi_g_init(void)
{
	rts_max_optsize = optcom_max_optsize(rts_opt_obj.odb_opt_des_arr,
	    rts_opt_obj.odb_opt_arr_cnt);

	/*
	 * We want to be informed each time a stack is created or
	 * destroyed in the kernel, so we can maintain the
	 * set of rts_stack_t's.
	 */
	netstack_register(NS_RTS, rts_stack_init, NULL, rts_stack_fini);
}

void
rts_ddi_g_destroy(void)
{
	netstack_unregister(NS_RTS);
}

#define	INET_NAME	"ip"

/*
 * Initialize the RTS stack instance.
 */
/* ARGSUSED */
static void *
rts_stack_init(netstackid_t stackid, netstack_t *ns)
{
	rts_stack_t	*rtss;
	rtsparam_t	*pa;
	int		error = 0;
	major_t		major;

	rtss = (rts_stack_t *)kmem_zalloc(sizeof (*rtss), KM_SLEEP);
	rtss->rtss_netstack = ns;

	pa = (rtsparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
	rtss->rtss_params = pa;
	bcopy(lcl_param_arr, rtss->rtss_params, sizeof (lcl_param_arr));

	(void) rts_param_register(&rtss->rtss_g_nd,
	    rtss->rtss_params, A_CNT(lcl_param_arr));

	major = mod_name_to_major(INET_NAME);
	error = ldi_ident_from_major(major, &rtss->rtss_ldi_ident);
	ASSERT(error == 0);
	return (rtss);
}

/*
 * Free the RTS stack instance.
 */
/* ARGSUSED */
static void
rts_stack_fini(netstackid_t stackid, void *arg)
{
	rts_stack_t *rtss = (rts_stack_t *)arg;

	nd_free(&rtss->rtss_g_nd);
	kmem_free(rtss->rtss_params, sizeof (lcl_param_arr));
	rtss->rtss_params = NULL;
	ldi_ident_release(rtss->rtss_ldi_ident);
	kmem_free(rtss, sizeof (*rtss));
}

/* ARGSUSED */
int
rts_accept(sock_lower_handle_t lproto_handle,
    sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle,
    cred_t *cr)
{
	return (EINVAL);
}

/* ARGSUSED */
static int
rts_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa,
    socklen_t len, cred_t *cr)
{
	/*
	 * rebind not allowed
	 */
	return (EINVAL);
}

/* ARGSUSED */
int
rts_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr)
{
	return (EINVAL);
}

/* ARGSUSED */
int
rts_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa,
    socklen_t len, sock_connid_t *id, cred_t *cr)
{
	/*
	 * rts sockets start out as bound and connected
	 */
	*id = 0;
	return (EISCONN);
}

/* ARGSUSED */
int
rts_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr,
    socklen_t *addrlen, cred_t *cr)
{
	conn_t *connp = (conn_t *)proto_handle;
	rts_t *rts = connp->conn_rts;

	ASSERT(rts != NULL);

	bzero(addr, sizeof (struct sockaddr));
	addr->sa_family = AF_ROUTE;
	*addrlen = sizeof (struct sockaddr);

	return (0);
}

/* ARGSUSED */
int
rts_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr,
    socklen_t *addrlen, cred_t *cr)
{
	return (EOPNOTSUPP);
}

static int
rts_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
    void *optvalp, socklen_t *optlen, cred_t *cr)
{
	conn_t  	*connp = (conn_t *)proto_handle;
	rts_t		*rts = connp->conn_rts;
	int		error;
	t_uscalar_t	max_optbuf_len;
	void		*optvalp_buf;
	int		len;

	error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len,
	    rts_opt_obj.odb_opt_des_arr,
	    rts_opt_obj.odb_opt_arr_cnt,
	    rts_opt_obj.odb_topmost_tpiprovider,
	    B_FALSE, B_TRUE, cr);
	if (error != 0) {
		if (error < 0)
			error = proto_tlitosyserr(-error);
		return (error);
	}

	optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP);
	rw_enter(&rts->rts_rwlock, RW_READER);
	len = rts_opt_get(connp, level, option_name, optvalp_buf);
	rw_exit(&rts->rts_rwlock);

	if (len < 0) {
		/*
		 * Pass on to IP
		 */
		error = ip_get_options(connp, level, option_name,
		    optvalp, optlen, cr);
	} else {
		/*
		 * update optlen and copy option value
		 */
		t_uscalar_t size = MIN(len, *optlen);
		bcopy(optvalp_buf, optvalp, size);
		bcopy(&size, optlen, sizeof (size));
		error = 0;
	}

	kmem_free(optvalp_buf, max_optbuf_len);
	return (error);
}

static int
rts_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
    const void *optvalp, socklen_t optlen, cred_t *cr)
{
	conn_t	*connp = (conn_t *)proto_handle;
	rts_t	*rts = connp->conn_rts;
	int	error;

	error = proto_opt_check(level, option_name, optlen, NULL,
	    rts_opt_obj.odb_opt_des_arr,
	    rts_opt_obj.odb_opt_arr_cnt,
	    rts_opt_obj.odb_topmost_tpiprovider,
	    B_TRUE, B_FALSE, cr);

	if (error != 0) {
		if (error < 0)
			error = proto_tlitosyserr(-error);
		return (error);
	}

	rw_enter(&rts->rts_rwlock, RW_WRITER);
	error = rts_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name,
	    optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp,
	    NULL, cr);
	rw_exit(&rts->rts_rwlock);

	ASSERT(error >= 0);

	return (error);
}

/* ARGSUSED */
static int
rts_send(sock_lower_handle_t proto_handle, mblk_t *mp,
    struct nmsghdr *msg, cred_t *cr)
{
	mblk_t  *mp1;
	conn_t  *connp = (conn_t *)proto_handle;
	rts_t   *rts = connp->conn_rts;
	rt_msghdr_t	*rtm;
	int error;

	ASSERT(DB_TYPE(mp) == M_DATA);
	/*
	 * The semantics of the routing socket is such that the rtm_pid
	 * field is automatically filled in during requests with the
	 * current process' pid.  We do this here (where we still have
	 * user context) after checking we have at least a message the
	 * size of a routing message header.
	 */
	if ((mp->b_wptr - mp->b_rptr) < sizeof (rt_msghdr_t)) {
		if (!pullupmsg(mp, sizeof (rt_msghdr_t))) {
			rts->rts_error = EINVAL;
			freemsg(mp);
			return (rts->rts_error);
		}
	}
	rtm = (rt_msghdr_t *)mp->b_rptr;
	rtm->rtm_pid = curproc->p_pid;

	mp1 = rts_ioctl_alloc(mp, DB_CRED(mp));
	if (mp1 == NULL) {
		ASSERT(rts != NULL);
		freemsg(mp);
		return (ENOMEM);
	}

	/*
	 * Allow only one outstanding request(ioctl) at any given time
	 */
	mutex_enter(&rts->rts_send_mutex);
	while (rts->rts_flag & RTS_REQ_PENDING) {
		int ret;

		ret = cv_wait_sig(&rts->rts_send_cv, &rts->rts_send_mutex);
		if (ret <= 0) {
			mutex_exit(&rts->rts_send_mutex);
			freemsg(mp);
			return (EINTR);
		}
	}

	rts->rts_flag |= RTS_REQ_PENDING;

	rts->rts_flag |= RTS_REQ_INPROG;

	mutex_exit(&rts->rts_send_mutex);

	CONN_INC_REF(connp);

	error = ip_rts_request_common(rts->rts_connp->conn_wq, mp1, connp,
	    DB_CREDDEF(mp, connp->conn_cred));

	mutex_enter(&rts->rts_send_mutex);
	if (error == EINPROGRESS) {
		ASSERT(rts->rts_flag & RTS_REQ_INPROG);
		if (rts->rts_flag & RTS_REQ_INPROG) {
			/*
			 * Once the request has been issued we wait for
			 * completion
			 */
			cv_wait(&rts->rts_io_cv, &rts->rts_send_mutex);
			error = rts->rts_error;
		}
	}

	ASSERT((error != 0) || !(rts->rts_flag & RTS_REQ_INPROG));
	ASSERT(MUTEX_HELD(&rts->rts_send_mutex));

	rts->rts_flag &= ~(RTS_REQ_PENDING | RTS_REQ_INPROG);
	cv_signal(&rts->rts_send_cv);
	mutex_exit(&rts->rts_send_mutex);
	return (error);
}

/* ARGSUSED */
sock_lower_handle_t
rts_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls,
    uint_t *smodep, int *errorp, int flags, cred_t *credp)
{
	conn_t	*connp;
	rts_t	*rts;
	rts_stack_t *rtss;

	if (family != AF_ROUTE || type != SOCK_RAW ||
	    (proto != 0 && proto != AF_INET && proto != AF_INET6)) {
		*errorp = EPROTONOSUPPORT;
		return (NULL);
	}

	connp = rts_open(flags, credp);
	ASSERT(connp != NULL);
	connp->conn_flags |= IPCL_NONSTR;

	rts = connp->conn_rts;
	rtss = rts->rts_rtss;

	rts->rts_xmit_hiwat = rtss->rtss_xmit_hiwat;
	rts->rts_xmit_lowat = rtss->rtss_xmit_lowat;
	rts->rts_recv_hiwat = rtss->rtss_recv_hiwat;
	rts->rts_recv_lowat = rts_mod_info.mi_lowat;

	ASSERT(rtss->rtss_ldi_ident != NULL);

	*errorp = ip_create_helper_stream(connp, rtss->rtss_ldi_ident);
	if (*errorp != 0) {
#ifdef DEBUG
		cmn_err(CE_CONT, "rts_create: create of IP helper stream"
		    " failed\n");
#endif
		(void) rts_close((sock_lower_handle_t)connp, 0, credp);
		return (NULL);
	}

	mutex_enter(&connp->conn_lock);
	connp->conn_state_flags &= ~CONN_INCIPIENT;
	mutex_exit(&connp->conn_lock);

	*errorp = 0;
	*smodep = SM_ATOMIC;
	*sock_downcalls = &sock_rts_downcalls;
	return ((sock_lower_handle_t)connp);
}

/* ARGSUSED */
void
rts_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle,
    sock_upcalls_t *sock_upcalls, int flags, cred_t *cr)
{
	conn_t  *connp = (conn_t *)proto_handle;
	rts_t	*rts = connp->conn_rts;
	rts_stack_t *rtss = rts->rts_rtss;
	struct sock_proto_props sopp;

	connp->conn_upcalls = sock_upcalls;
	connp->conn_upper_handle = sock_handle;

	sopp.sopp_flags = SOCKOPT_WROFF | SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT |
	    SOCKOPT_MAXBLK | SOCKOPT_MAXPSZ | SOCKOPT_MINPSZ;
	sopp.sopp_wroff = 0;
	sopp.sopp_rxhiwat = rtss->rtss_recv_hiwat;
	sopp.sopp_rxlowat = rts_mod_info.mi_lowat;
	sopp.sopp_maxblk = INFPSZ;
	sopp.sopp_maxpsz = rts_mod_info.mi_maxpsz;
	sopp.sopp_minpsz = (rts_mod_info.mi_minpsz == 1) ? 0 :
	    rts_mod_info.mi_minpsz;

	(*connp->conn_upcalls->su_set_proto_props)
	    (connp->conn_upper_handle, &sopp);

	/*
	 * We treat it as already connected for routing socket.
	 */
	(*connp->conn_upcalls->su_connected)
	    (connp->conn_upper_handle, 0, NULL, -1);

	/*
	 * Indicate the down IP module that this is a routing socket
	 * client by sending an RTS IOCTL without any user data. Although
	 * this is just a notification message (without any real routing
	 * request), we pass in any credential for correctness sake.
	 */
	ip_rts_register(connp);
}

/* ARGSUSED */
int
rts_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr)
{
	conn_t  *connp = (conn_t *)proto_handle;

	ASSERT(connp != NULL && IPCL_IS_RTS(connp));
	return (rts_common_close(NULL, connp));
}

/* ARGSUSED */
int
rts_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr)
{
	conn_t  *connp = (conn_t *)proto_handle;

	/* shut down the send side */
	if (how != SHUT_RD)
		(*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
		    SOCK_OPCTL_SHUT_SEND, 0);
	/* shut down the recv side */
	if (how != SHUT_WR)
		(*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
		    SOCK_OPCTL_SHUT_RECV, 0);
	return (0);
}

void
rts_clr_flowctrl(sock_lower_handle_t proto_handle)
{
	conn_t  *connp = (conn_t *)proto_handle;
	rts_t	*rts = connp->conn_rts;

	mutex_enter(&rts->rts_recv_mutex);
	connp->conn_flow_cntrld = B_FALSE;
	mutex_exit(&rts->rts_recv_mutex);
}

int
rts_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg,
    int mode, int32_t *rvalp, cred_t *cr)
{
	conn_t		*connp = (conn_t *)proto_handle;
	int		error;

	switch (cmd) {
	case ND_SET:
	case ND_GET:
	case TI_GETPEERNAME:
	case TI_GETMYNAME:
#ifdef DEUG
		cmn_err(CE_CONT, "rts_ioctl cmd 0x%x on non sreams"
		    " socket", cmd);
#endif
		error = EINVAL;
		break;
	default:
		/*
		 * Pass on to IP using helper stream
		 */
		error = ldi_ioctl(connp->conn_helper_info->iphs_handle,
		    cmd, arg, mode, cr, rvalp);
		break;
	}

	return (error);
}

sock_downcalls_t sock_rts_downcalls = {
	rts_activate,
	rts_accept,
	rts_bind,
	rts_listen,
	rts_connect,
	rts_getpeername,
	rts_getsockname,
	rts_getsockopt,
	rts_setsockopt,
	rts_send,
	NULL,
	NULL,
	NULL,
	rts_shutdown,
	rts_clr_flowctrl,
	rts_ioctl,
	rts_close
};