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
|
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#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/socket.h>
#include <netinet/in.h>
#include <inet/common.h>
#include <netinet/ip6.h>
#include <inet/ip.h>
#include <inet/mi.h>
#include <inet/nd.h>
#include <inet/optcom.h>
#include <netinet/ip_mroute.h>
#include <sys/isa_defs.h>
#include <net/route.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_REDIRECT 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.
*/
/*
* RTS stack instances
*/
struct rts_stack {
netstack_t *rtss_netstack; /* Common netstack */
caddr_t rtss_g_nd;
struct rtsparam_s *rtss_params;
};
typedef struct rts_stack rts_stack_t;
/*
* Object to represent database of options to search passed to
* {sock,tpi}optcom_req() interface routine to take care of option
* management and associated methods.
* XXX. These and other externs should really move to a rts header.
*/
extern optdb_obj_t rts_opt_obj;
extern uint_t rts_max_optsize;
/* Internal routing socket stream control structure, one per open stream */
typedef struct rts_s {
cred_t *rts_credp; /* Opener's credentials */
uint_t rts_state; /* Provider interface state */
uint_t rts_error; /* Routing socket error code */
uint_t rts_flag; /* Pending I/O state */
uint_t rts_proto; /* SO_PROTOTYPE "socket" option. */
uint_t rts_debug : 1, /* SO_DEBUG "socket" option. */
rts_dontroute : 1, /* SO_DONTROUTE "socket" option. */
rts_broadcast : 1, /* SO_BROADCAST "socket" option. */
rts_reuseaddr : 1, /* SO_REUSEADDR "socket" option. */
rts_useloopback : 1, /* SO_USELOOPBACK "socket" option. */
rts_multicast_loop : 1, /* IP_MULTICAST_LOOP option */
rts_hdrincl : 1, /* IP_HDRINCL option + RAW and IGMP */
: 0;
rts_stack_t *rts_rtss;
} rts_t;
#define RTS_WPUT_PENDING 0x1 /* Waiting for write-side to complete */
#define RTS_WRW_PENDING 0x2 /* Routing socket write in progress */
#define RTS_OPEN_PENDING 0x4 /* Routing socket open in progress */
/* 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 */
};
/* Named Dispatch Parameter Management Structure */
typedef struct rtsparam_s {
uint_t rts_param_min;
uint_t rts_param_max;
uint_t rts_param_value;
char *rts_param_name;
} rtsparam_t;
/*
* 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 int rts_close(queue_t *q);
static void rts_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
int sys_error);
static mblk_t *rts_ioctl_alloc(mblk_t *data, cred_t *cr);
static int rts_open(queue_t *q, dev_t *devp, int flag, int sflag,
cred_t *credp);
int rts_opt_default(queue_t *q, t_scalar_t level, t_scalar_t name,
uchar_t *ptr);
int rts_opt_get(queue_t *q, t_scalar_t level, t_scalar_t name,
uchar_t *ptr);
int rts_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);
static void rts_param_cleanup(IDP *ndp);
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_rput(queue_t *q, mblk_t *mp);
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 struct module_info info = {
129, "rts", 1, INFPSZ, 512, 128
};
static struct qinit rinit = {
(pfi_t)rts_rput, NULL, rts_open, rts_close, NULL, &info
};
static struct qinit winit = {
(pfi_t)rts_wput, NULL, NULL, NULL, NULL, &info,
NULL, (pfi_t)rts_wrw, NULL, STRUIOT_STANDARD
};
struct streamtab rtsinfo = {
&rinit, &winit
};
/*
* 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_close(queue_t *q)
{
rts_t *rts = (rts_t *)q->q_ptr;
qprocsoff(q);
crfree(rts->rts_credp);
netstack_rele(rts->rts_rtss->rtss_netstack);
mi_free(q->q_ptr);
return (0);
}
/*
* This is the open routine for routing socket. It allocates
* rts_t structure for the stream and sends an IOCTL to
* the down module to indicate that it is a routing socket
* stream.
*/
/* ARGSUSED */
static int
rts_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
{
mblk_t *mp = NULL;
rts_t *rts;
netstack_t *ns;
rts_stack_t *rtss;
/* If the stream is already open, return immediately. */
if (q->q_ptr != NULL)
return (0);
/* If this is not a push of rts as a module, fail. */
if (sflag != MODOPEN)
return (EINVAL);
ns = netstack_find_by_cred(credp);
ASSERT(ns != NULL);
rtss = ns->netstack_rts;
ASSERT(rtss != NULL);
q->q_ptr = mi_zalloc_sleep(sizeof (rts_t));
WR(q)->q_ptr = q->q_ptr;
rts = (rts_t *)q->q_ptr;
rts->rts_rtss = rtss;
rts->rts_credp = credp;
crhold(credp);
/*
* The receive hiwat is only looked at on the stream head queue.
* Store in q_hiwat in order to return on SO_RCVBUF getsockopts.
*/
q->q_hiwat = rtss->rtss_recv_hiwat;
/*
* The transmit hiwat/lowat is only looked at on IP's queue.
* Store in q_hiwat/q_lowat in order to return on SO_SNDBUF/SO_SNDLOWAT
* getsockopts.
*/
WR(q)->q_hiwat = rtss->rtss_xmit_hiwat;
WR(q)->q_lowat = rtss->rtss_xmit_lowat;
qprocson(q);
/*
* 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.
*/
mp = rts_ioctl_alloc(NULL, credp);
if (mp == NULL) {
qprocsoff(q);
ASSERT(q->q_ptr != NULL);
netstack_rele(rtss->rtss_netstack);
mi_free(q->q_ptr);
crfree(credp);
return (ENOMEM);
}
rts->rts_flag |= RTS_OPEN_PENDING;
putnext(WR(q), mp);
while (rts->rts_flag & RTS_OPEN_PENDING) {
if (!qwait_sig(q)) {
(void) rts_close(q);
return (EINTR);
}
}
if (rts->rts_error != 0) {
(void) rts_close(q);
return (ENOTSUP);
}
rts->rts_state = TS_UNBND;
return (0);
}
/*
* 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.
* After some error checking, the message is passed downstream to ip.
*/
static void
rts_unbind(queue_t *q, mblk_t *mp)
{
rts_t *rts;
rts = (rts_t *)q->q_ptr;
/* 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_bind(queue_t *q, mblk_t *mp)
{
mblk_t *mp1;
struct T_bind_req *tbr;
rts_t *rts;
rts = (rts_t *)q->q_ptr;
if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
(void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
"rts_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_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_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)
{
rts_t *rts = (rts_t *)q->q_ptr;
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)
{
rts_t *rts = (rts_t *)q->q_ptr;
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);
}
/*
* This routine retrieves the current status of socket options.
* It returns the size of the option retrieved.
*/
int
rts_opt_get(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
{
int *i1 = (int *)ptr;
rts_t *rts = (rts_t *)q->q_ptr;
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(q->q_hiwat <= INT_MAX);
*i1 = (int)(q->q_hiwat);
break;
case SO_RCVBUF:
ASSERT(q->q_hiwat <= INT_MAX);
*i1 = (int)(RD(q)->q_hiwat);
break;
case SO_DOMAIN:
*i1 = PF_ROUTE;
break;
default:
return (-1);
}
break;
default:
return (-1);
}
return ((int)sizeof (int));
}
/*
* This routine sets socket options.
*/
/*ARGSUSED*/
int
rts_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)
{
int *i1 = (int *)invalp;
rts_t *rts = (rts_t *)q->q_ptr;
boolean_t checkonly;
rts_stack_t *rtss = rts->rts_rtss;
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));
/*
* 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) {
q->q_hiwat = *i1;
q->q_next->q_hiwat = *i1;
}
break; /* goto sizeof (int) option return */
case SO_RCVBUF:
if (*i1 > rtss->rtss_max_buf) {
*outlenp = 0;
return (ENOBUFS);
}
if (!checkonly) {
RD(q)->q_hiwat = *i1;
(void) mi_set_sth_hiwat(RD(q), *i1);
}
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)
*/
*(int *)outvalp = *i1;
*outlenp = (t_uscalar_t)sizeof (int);
return (0);
}
/*
* This routine frees the ND table if all streams have been closed.
* It is called by rts_close and rts_open.
*/
static void
rts_param_cleanup(IDP *ndp)
{
nd_free(ndp);
}
/*
* 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);
}
/*
* 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;
rts_t *rts;
rts = (rts_t *)q->q_ptr;
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.
*/
static void
rts_wput(queue_t *q, mblk_t *mp)
{
uchar_t *rptr = mp->b_rptr;
mblk_t *mp1;
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) {
rts_t *rts = (rts_t *)q->q_ptr;
ASSERT(rts != NULL);
freemsg(mp);
if (rts->rts_flag & RTS_WPUT_PENDING) {
rts->rts_error = ENOMEM;
rts->rts_flag &= ~RTS_WPUT_PENDING;
}
return;
}
putnext(q, mp1);
}
/*
* 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)
{
uchar_t *rptr = mp->b_rptr;
rts_t *rts;
struct iocblk *iocp;
cred_t *cr;
rts_stack_t *rtss;
rts = (rts_t *)q->q_ptr;
rtss = rts->rts_rtss;
cr = DB_CREDDEF(mp, rts->rts_credp);
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_bind(q, mp);
return;
case T_UNBIND_REQ:
rts_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);
return;
case T_OPTMGMT_REQ:
(void) tpi_optcom_req(q, mp, cr, &rts_opt_obj);
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;
}
putnext(q, mp);
}
/*
* Called by rts_wput_other to handle all M_IOCDATA messages.
*/
static void
rts_wput_iocdata(queue_t *q, mblk_t *mp)
{
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:
putnext(q, mp);
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);
}
static void
rts_rput(queue_t *q, mblk_t *mp)
{
rts_t *rts;
struct iocblk *iocp;
mblk_t *mp1;
struct T_data_ind *tdi;
rts = (rts_t *)q->q_ptr;
switch (mp->b_datap->db_type) {
case M_IOCACK:
case M_IOCNAK:
iocp = (struct iocblk *)mp->b_rptr;
if (rts->rts_flag & (RTS_WPUT_PENDING|RTS_OPEN_PENDING)) {
if (rts->rts_flag & RTS_WPUT_PENDING)
rts->rts_flag &= ~RTS_WPUT_PENDING;
else
rts->rts_flag &= ~RTS_OPEN_PENDING;
rts->rts_error = iocp->ioc_error;
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;
}
putnext(q, mp);
}
void
rts_ddi_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_destroy(void)
{
netstack_unregister(NS_RTS);
}
/*
* Initialize the RTS stack instance.
*/
/* ARGSUSED */
static void *
rts_stack_init(netstackid_t stackid, netstack_t *ns)
{
rts_stack_t *rtss;
rtsparam_t *pa;
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));
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;
rts_param_cleanup(&rtss->rtss_g_nd);
kmem_free(rtss->rtss_params, sizeof (lcl_param_arr));
rtss->rtss_params = NULL;
kmem_free(rtss, sizeof (*rtss));
}
|