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
|
/*
* 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.
*/
/* Copyright (c) 1990 Mentat Inc. */
#include <sys/types.h>
#include <inet/common.h> /* for various inet/mi.h and inet/nd.h needs */
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/strsun.h>
#include <sys/sysmacros.h>
#include <inet/nd.h>
#include <inet/mi.h>
#define _SUN_TPI_VERSION 2
#include <sys/tihdr.h>
#include <sys/timod.h>
#include <sys/vtrace.h>
#include <sys/kmem.h>
#include <sys/mkdev.h>
#include <sys/strlog.h>
#include <sys/ddi.h>
#include <sys/suntpi.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/kobj.h>
#include <sys/stropts.h>
#include <sys/strsubr.h>
#include <inet/proto_set.h>
#define ISDIGIT(ch) ((ch) >= '0' && (ch) <= '9')
#define ISUPPER(ch) ((ch) >= 'A' && (ch) <= 'Z')
#define tolower(ch) ('a' + ((ch) - 'A'))
#define MI_IS_TRANSPARENT(mp) (mp->b_cont && \
(mp->b_cont->b_rptr != mp->b_cont->b_wptr))
/*
* NOTE: Whenever anything is allocated by mi_alloc or mi_alloc_sleep (below),
* the size of the requested allocation is increased by one word. This extra
* word is used to store the size of the object being allocated, and is located
* at the beginning of the allocated block. The pointer returned to the caller
* is a pointer to the *second* word in the newly-allocated block. The IP
* module of mdb is aware of this, and will need to be changed if this
* allocation strategy is changed.
*/
typedef struct stroptions *STROPTP;
typedef union T_primitives *TPRIMP;
/* Timer block states. */
#define TB_RUNNING 1
#define TB_IDLE 2
/*
* Could not stop/free before putq
*/
#define TB_RESCHED 3 /* mtb_time_left contains tick count */
#define TB_CANCELLED 4
#define TB_TO_BE_FREED 5
typedef struct mtb_s {
int mtb_state;
timeout_id_t mtb_tid;
queue_t *mtb_q;
MBLKP mtb_mp;
clock_t mtb_time_left;
} MTB, *MTBP;
static int mi_timer_fire(MTBP);
static int mi_iprintf(char *, va_list, pfi_t, char *);
static void mi_tpi_addr_and_opt(MBLKP, char *, t_scalar_t, char *, t_scalar_t);
static MBLKP mi_tpi_trailer_alloc(MBLKP, size_t, t_scalar_t);
/* ARGSUSED1 */
void *
mi_alloc(size_t size, uint_t pri)
{
size_t *ptr;
size += sizeof (size);
if (ptr = kmem_alloc(size, KM_NOSLEEP)) {
*ptr = size;
return (ptr + 1);
}
return (NULL);
}
/* ARGSUSED1 */
void *
mi_alloc_sleep(size_t size, uint_t pri)
{
size_t *ptr;
size += sizeof (size);
ptr = kmem_alloc(size, KM_SLEEP);
*ptr = size;
return (ptr + 1);
}
int
mi_close_comm(void **mi_headp, queue_t *q)
{
IDP ptr;
ptr = q->q_ptr;
mi_close_unlink(mi_headp, ptr);
mi_close_free(ptr);
q->q_ptr = WR(q)->q_ptr = NULL;
return (0);
}
void
mi_close_unlink(void **mi_headp, IDP ptr)
{
mi_head_t *mi_head = *(mi_head_t **)mi_headp;
MI_OP mi_o;
dev_t dev;
mi_o = (MI_OP)ptr;
if (!mi_o)
return;
mi_o--;
if (mi_o->mi_o_next == NULL) {
/* Not in list */
ASSERT(mi_o->mi_o_prev == NULL);
return;
}
/* Free minor number */
dev = mi_o->mi_o_dev;
if ((dev != OPENFAIL) && (dev != 0) && (dev <= MAXMIN))
inet_minor_free(mi_head->mh_arena, dev);
/* Unlink from list */
ASSERT(mi_o->mi_o_next != NULL);
ASSERT(mi_o->mi_o_prev != NULL);
ASSERT(mi_o->mi_o_next->mi_o_prev == mi_o);
ASSERT(mi_o->mi_o_prev->mi_o_next == mi_o);
mi_o->mi_o_next->mi_o_prev = mi_o->mi_o_prev;
mi_o->mi_o_prev->mi_o_next = mi_o->mi_o_next;
mi_o->mi_o_next = mi_o->mi_o_prev = NULL;
mi_o->mi_o_dev = (dev_t)OPENFAIL;
/* If list now empty free the list head */
if (mi_head->mh_o.mi_o_next == &mi_head->mh_o) {
ASSERT(mi_head->mh_o.mi_o_prev == &mi_head->mh_o);
if (mi_head->mh_arena != NULL)
inet_minor_destroy(mi_head->mh_arena);
mi_free((IDP)mi_head);
*mi_headp = NULL;
}
}
void
mi_close_free(IDP ptr)
{
MI_OP mi_o;
mi_o = (MI_OP)ptr;
if (!mi_o)
return;
mi_o--;
ASSERT(mi_o->mi_o_next == NULL && mi_o->mi_o_prev == NULL);
mi_free((IDP)mi_o);
}
/*
* mi_copyin - takes care of transparent or non-transparent ioctl for the
* calling function so that they have to deal with just M_IOCDATA type
* and not worry about M_COPYIN.
*
* mi_copyin checks to see if the ioctl is transparent or non transparent.
* In case of a non_transparent ioctl, it packs the data into a M_IOCDATA
* message and puts it back onto the current queue for further processing.
* In case of transparent ioctl, it sends a M_COPYIN message up to the
* streamhead so that a M_IOCDATA with the information comes back down.
*/
void
mi_copyin(queue_t *q, MBLKP mp, char *uaddr, size_t len)
{
struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
struct copyreq *cq = (struct copyreq *)mp->b_rptr;
struct copyresp *cp = (struct copyresp *)mp->b_rptr;
int err;
MBLKP mp1;
ASSERT(mp->b_datap->db_type == M_IOCTL && !uaddr);
/* A transparent ioctl. Send a M_COPYIN message to the streamhead. */
if (iocp->ioc_count == TRANSPARENT) {
MI_COPY_COUNT(mp) = 1;
MI_COPY_DIRECTION(mp) = MI_COPY_IN;
cq->cq_private = mp->b_cont;
cq->cq_size = len;
cq->cq_flag = 0;
bcopy(mp->b_cont->b_rptr, &cq->cq_addr, sizeof (cq->cq_addr));
mp->b_cont = NULL;
mp->b_datap->db_type = M_COPYIN;
qreply(q, mp);
return;
}
/*
* A non-transparent ioctl. Need to convert into M_IOCDATA message.
*
* We allocate a 0 byte message block and put its address in
* cp_private. It also makes the b_prev field = 1 and b_next
* field = MI_COPY_IN for this 0 byte block. This is done to
* maintain compatibility with old code in mi_copy_state
* (which removes the empty block).
*/
err = miocpullup(mp, len);
if (err != 0)
goto err_ret;
mp1 = allocb(0, BPRI_MED);
if (mp1 == NULL) {
err = ENOMEM;
goto err_ret;
}
/*
* Temporarily insert mp1 between the M_IOCTL and M_DATA blocks so
* that we can use the MI_COPY_COUNT & MI_COPY_DIRECTION macros.
*/
mp1->b_cont = mp->b_cont;
mp->b_cont = mp1;
MI_COPY_COUNT(mp) = 1;
MI_COPY_DIRECTION(mp) = MI_COPY_IN;
mp->b_cont = mp1->b_cont;
mp1->b_cont = NULL;
/*
* Leave a pointer to the 0 byte block in cp_private field for
* future use by the mi_copy_* routines.
*/
mp->b_datap->db_type = M_IOCDATA;
cp->cp_private = mp1;
cp->cp_rval = NULL;
put(q, mp);
return;
err_ret:
iocp->ioc_error = err;
iocp->ioc_count = 0;
if (mp->b_cont) {
freemsg(mp->b_cont);
mp->b_cont = NULL;
}
mp->b_datap->db_type = M_IOCACK;
qreply(q, mp);
}
/*
* Allows transparent IOCTLs to have multiple copyins. This is needed
* for some variable-length structures, where the total size is only known
* after the first part is copied in. Rather than setting MI_COPY_COUNT to
* 1, as in mi_coypin(), it is simply incremented here. This value can
* then be checked in the returned IOCBLK.
*
* As this deals with copyins that follow the initial copyin, the byte
* offset into the user buffer from which copying should begin must be
* passed in in the offset parameter.
*
* Unlike mi_coypin(), this function expects to be passed an mblk chain
* headed by an M_IOCBLK, as that's the chain that will be in use for
* copies after the first one (copies where n != 1).
*/
void
mi_copyin_n(queue_t *q, MBLKP mp, size_t offset, size_t len)
{
struct copyreq *cq = (struct copyreq *)mp->b_rptr;
ASSERT(mp->b_datap->db_type == M_IOCDATA);
MI_COPY_COUNT(mp)++;
MI_COPY_DIRECTION(mp) = MI_COPY_IN;
cq->cq_private = mp->b_cont;
cq->cq_size = len;
cq->cq_flag = 0;
bcopy(mp->b_cont->b_rptr, &cq->cq_addr, sizeof (cq->cq_addr));
cq->cq_addr += offset;
mp->b_cont = NULL;
mp->b_datap->db_type = M_COPYIN;
qreply(q, mp);
}
void
mi_copyout(queue_t *q, MBLKP mp)
{
struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
struct copyreq *cq = (struct copyreq *)iocp;
struct copyresp *cp = (struct copyresp *)cq;
MBLKP mp1;
MBLKP mp2;
if (mp->b_datap->db_type != M_IOCDATA || !mp->b_cont) {
mi_copy_done(q, mp, EPROTO);
return;
}
/* Check completion of previous copyout operation. */
mp1 = mp->b_cont;
if ((int)(uintptr_t)cp->cp_rval || !mp1->b_cont) {
mi_copy_done(q, mp, (int)(uintptr_t)cp->cp_rval);
return;
}
if (!mp1->b_cont->b_cont && !MI_IS_TRANSPARENT(mp)) {
mp1->b_next = NULL;
mp1->b_prev = NULL;
mp->b_cont = mp1->b_cont;
freeb(mp1);
mp1 = mp->b_cont;
mp1->b_next = NULL;
mp1->b_prev = NULL;
iocp->ioc_count = mp1->b_wptr - mp1->b_rptr;
iocp->ioc_error = 0;
mp->b_datap->db_type = M_IOCACK;
qreply(q, mp);
return;
}
if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
/* Set up for first copyout. */
MI_COPY_DIRECTION(mp) = MI_COPY_OUT;
MI_COPY_COUNT(mp) = 1;
} else {
++MI_COPY_COUNT(mp);
}
cq->cq_private = mp1;
/* Find message preceding last. */
for (mp2 = mp1; mp2->b_cont->b_cont; mp2 = mp2->b_cont)
;
if (mp2 == mp1)
bcopy((char *)mp1->b_rptr, (char *)&cq->cq_addr,
sizeof (cq->cq_addr));
else
cq->cq_addr = (char *)mp2->b_cont->b_next;
mp1 = mp2->b_cont;
mp->b_datap->db_type = M_COPYOUT;
mp->b_cont = mp1;
mp2->b_cont = NULL;
mp1->b_next = NULL;
cq->cq_size = mp1->b_wptr - mp1->b_rptr;
cq->cq_flag = 0;
qreply(q, mp);
}
MBLKP
mi_copyout_alloc(queue_t *q, MBLKP mp, char *uaddr, size_t len,
boolean_t free_on_error)
{
struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
MBLKP mp1;
if (mp->b_datap->db_type == M_IOCTL) {
if (iocp->ioc_count != TRANSPARENT) {
mp1 = allocb(0, BPRI_MED);
if (mp1 == NULL) {
if (free_on_error) {
iocp->ioc_error = ENOMEM;
iocp->ioc_count = 0;
freemsg(mp->b_cont);
mp->b_cont = NULL;
mp->b_datap->db_type = M_IOCACK;
qreply(q, mp);
}
return (NULL);
}
mp1->b_cont = mp->b_cont;
mp->b_cont = mp1;
}
MI_COPY_COUNT(mp) = 0;
MI_COPY_DIRECTION(mp) = MI_COPY_OUT;
/* Make sure it looks clean to mi_copyout. */
mp->b_datap->db_type = M_IOCDATA;
((struct copyresp *)iocp)->cp_rval = NULL;
}
mp1 = allocb(len, BPRI_MED);
if (mp1 == NULL) {
if (free_on_error)
mi_copy_done(q, mp, ENOMEM);
return (NULL);
}
linkb(mp, mp1);
mp1->b_next = (MBLKP)uaddr;
return (mp1);
}
void
mi_copy_done(queue_t *q, MBLKP mp, int err)
{
struct iocblk *iocp;
MBLKP mp1;
if (!mp)
return;
if (!q || (mp->b_wptr - mp->b_rptr) < sizeof (struct iocblk)) {
freemsg(mp);
return;
}
iocp = (struct iocblk *)mp->b_rptr;
mp->b_datap->db_type = M_IOCACK;
iocp->ioc_error = err;
iocp->ioc_count = 0;
if ((mp1 = mp->b_cont) != NULL) {
for (; mp1; mp1 = mp1->b_cont) {
mp1->b_prev = NULL;
mp1->b_next = NULL;
}
freemsg(mp->b_cont);
mp->b_cont = NULL;
}
qreply(q, mp);
}
int
mi_copy_state(queue_t *q, MBLKP mp, MBLKP *mpp)
{
struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
struct copyresp *cp = (struct copyresp *)iocp;
MBLKP mp1;
mp1 = mp->b_cont;
mp->b_cont = cp->cp_private;
if (mp1) {
if (mp1->b_cont && !pullupmsg(mp1, -1)) {
mi_copy_done(q, mp, ENOMEM);
return (-1);
}
linkb(mp->b_cont, mp1);
}
if ((int)(uintptr_t)cp->cp_rval) {
mi_copy_done(q, mp, (int)(uintptr_t)cp->cp_rval);
return (-1);
}
if (mpp && MI_COPY_DIRECTION(mp) == MI_COPY_IN)
*mpp = mp1;
return (MI_COPY_STATE(mp));
}
void
mi_free(void *ptr)
{
size_t size;
if (!ptr)
return;
if ((size = ((size_t *)ptr)[-1]) <= 0)
cmn_err(CE_PANIC, "mi_free");
kmem_free((void *) ((size_t *)ptr - 1), size);
}
static int
mi_iprintf(char *fmt, va_list ap, pfi_t putc_func, char *cookie)
{
int base;
char buf[(sizeof (long) * 3) + 1];
static char hex_val[] = "0123456789abcdef";
int ch;
int count;
char *cp1;
int digits;
char *fcp;
boolean_t is_long;
ulong_t uval;
long val;
boolean_t zero_filled;
if (!fmt)
return (-1);
count = 0;
while (*fmt) {
if (*fmt != '%' || *++fmt == '%') {
count += (*putc_func)(cookie, *fmt++);
continue;
}
if (*fmt == '0') {
zero_filled = B_TRUE;
fmt++;
if (!*fmt)
break;
} else
zero_filled = B_FALSE;
base = 0;
for (digits = 0; ISDIGIT(*fmt); fmt++) {
digits *= 10;
digits += (*fmt - '0');
}
if (!*fmt)
break;
is_long = B_FALSE;
if (*fmt == 'l') {
is_long = B_TRUE;
fmt++;
}
if (!*fmt)
break;
ch = *fmt++;
if (ISUPPER(ch)) {
ch = tolower(ch);
is_long = B_TRUE;
}
switch (ch) {
case 'c':
count += (*putc_func)(cookie, va_arg(ap, int *));
continue;
case 'd':
base = 10;
break;
case 'm': /* Print out memory, 2 hex chars per byte */
if (is_long)
fcp = va_arg(ap, char *);
else {
if ((cp1 = va_arg(ap, char *)) != NULL)
fcp = (char *)cp1;
else
fcp = NULL;
}
if (!fcp) {
for (fcp = (char *)"(NULL)"; *fcp; fcp++)
count += (*putc_func)(cookie, *fcp);
} else {
while (digits--) {
int u1 = *fcp++ & 0xFF;
count += (*putc_func)(cookie,
hex_val[(u1>>4)& 0xF]);
count += (*putc_func)(cookie,
hex_val[u1& 0xF]);
}
}
continue;
case 'o':
base = 8;
break;
case 'p':
is_long = B_TRUE;
/* FALLTHRU */
case 'x':
base = 16;
break;
case 's':
if (is_long)
fcp = va_arg(ap, char *);
else {
if ((cp1 = va_arg(ap, char *)) != NULL)
fcp = (char *)cp1;
else
fcp = NULL;
}
if (!fcp)
fcp = (char *)"(NULL)";
while (*fcp) {
count += (*putc_func)(cookie, *fcp++);
if (digits && --digits == 0)
break;
}
while (digits > 0) {
count += (*putc_func)(cookie, ' ');
digits--;
}
continue;
case 'u':
base = 10;
break;
default:
return (count);
}
if (is_long)
val = va_arg(ap, long);
else
val = va_arg(ap, int);
if (base == 10 && ch != 'u') {
if (val < 0) {
count += (*putc_func)(cookie, '-');
val = -val;
}
uval = val;
} else {
if (is_long)
uval = val;
else
uval = (uint_t)val;
}
/* Hand overload/restore the register variable 'fmt' */
cp1 = fmt;
fmt = A_END(buf);
*--fmt = '\0';
do {
if (fmt > buf)
*--fmt = hex_val[uval % base];
if (digits && --digits == 0)
break;
} while (uval /= base);
if (zero_filled) {
while (digits > 0 && fmt > buf) {
*--fmt = '0';
digits--;
}
}
while (*fmt)
count += (*putc_func)(cookie, *fmt++);
fmt = cp1;
}
return (count);
}
/* PRINTFLIKE2 */
int
mi_mpprintf(MBLKP mp, char *fmt, ...)
{
va_list ap;
int count = -1;
va_start(ap, fmt);
if (mp) {
count = mi_iprintf(fmt, ap, (pfi_t)mi_mpprintf_putc,
(char *)mp);
if (count != -1)
(void) mi_mpprintf_putc((char *)mp, '\0');
}
va_end(ap);
return (count);
}
/* PRINTFLIKE2 */
int
mi_mpprintf_nr(MBLKP mp, char *fmt, ...)
{
va_list ap;
int count = -1;
va_start(ap, fmt);
if (mp) {
(void) adjmsg(mp, -1);
count = mi_iprintf(fmt, ap, (pfi_t)mi_mpprintf_putc,
(char *)mp);
if (count != -1)
(void) mi_mpprintf_putc((char *)mp, '\0');
}
va_end(ap);
return (count);
}
int
mi_mpprintf_putc(char *cookie, int ch)
{
MBLKP mp = (MBLKP)cookie;
while (mp->b_cont)
mp = mp->b_cont;
if (mp->b_wptr >= mp->b_datap->db_lim) {
mp->b_cont = allocb(1024, BPRI_HI);
mp = mp->b_cont;
if (!mp)
return (0);
}
*mp->b_wptr++ = (unsigned char)ch;
return (1);
}
IDP
mi_first_ptr(void **mi_headp)
{
mi_head_t *mi_head = *(mi_head_t **)mi_headp;
MI_OP mi_op;
mi_op = mi_head->mh_o.mi_o_next;
if (mi_op && mi_op != &mi_head->mh_o)
return ((IDP)&mi_op[1]);
return (NULL);
}
/*
* Clients can choose to have both module instances and device instances
* in the same list. Return the first device instance in the list.
*/
IDP
mi_first_dev_ptr(void **mi_headp)
{
mi_head_t *mi_head = *(mi_head_t **)mi_headp;
MI_OP mi_op;
mi_op = mi_head->mh_o.mi_o_next;
while ((mi_op != NULL) && (mi_op != &mi_head->mh_o)) {
if (mi_op->mi_o_isdev)
return ((IDP)&mi_op[1]);
mi_op = mi_op->mi_o_next;
}
return (NULL);
}
IDP
mi_next_ptr(void **mi_headp, IDP ptr)
{
mi_head_t *mi_head = *(mi_head_t **)mi_headp;
MI_OP mi_op = ((MI_OP)ptr) - 1;
if ((mi_op = mi_op->mi_o_next) != NULL && mi_op != &mi_head->mh_o)
return ((IDP)&mi_op[1]);
return (NULL);
}
/*
* Clients can choose to have both module instances and device instances
* in the same list. Return the next device instance in the list.
*/
IDP
mi_next_dev_ptr(void **mi_headp, IDP ptr)
{
mi_head_t *mi_head = *(mi_head_t **)mi_headp;
MI_OP mi_op = ((MI_OP)ptr) - 1;
mi_op = mi_op->mi_o_next;
while ((mi_op != NULL) && (mi_op != &mi_head->mh_o)) {
if (mi_op->mi_o_isdev)
return ((IDP)&mi_op[1]);
mi_op = mi_op->mi_o_next;
}
return (NULL);
}
/*
* Self clone the device
* XXX - should we still support clone device
*/
/* ARGSUSED4 */
int
mi_open_comm(void **mi_headp, size_t size, queue_t *q, dev_t *devp,
int flag, int sflag, cred_t *credp)
{
int error;
IDP ptr;
if (q->q_ptr != NULL)
return (0);
ptr = mi_open_alloc_sleep(size);
q->q_ptr = WR(q)->q_ptr = ptr;
error = mi_open_link(mi_headp, ptr, devp, flag, sflag, credp);
if (error != 0) {
q->q_ptr = WR(q)->q_ptr = NULL;
mi_close_free(ptr);
}
return (error);
}
IDP
mi_open_alloc_sleep(size_t size)
{
MI_OP mi_o;
if (size > (UINT_MAX - sizeof (MI_O)))
return (NULL);
mi_o = (MI_OP)mi_zalloc_sleep(size + sizeof (MI_O));
mi_o++;
return ((IDP)mi_o);
}
IDP
mi_open_alloc(size_t size)
{
MI_OP mi_o;
if (size > (UINT_MAX - sizeof (MI_O)))
return (NULL);
if ((mi_o = (MI_OP)mi_zalloc(size + sizeof (MI_O))) == NULL)
return (NULL);
mi_o++;
return ((IDP)mi_o);
}
/*
* MODOPEN means just link in without respect of mi_o_dev.
* A NULL devp can be used to create a detached instance
* Otherwise self-clone the device.
*/
/* ARGSUSED3 */
int
mi_open_link(void **mi_headp, IDP ptr, dev_t *devp, int flag, int sflag,
cred_t *credp)
{
mi_head_t *mi_head = *(mi_head_t **)mi_headp;
MI_OP insert;
MI_OP mi_o;
dev_t dev;
if (mi_head == NULL) {
char arena_name[50];
char *head_name;
ulong_t offset;
head_name = kobj_getsymname((uintptr_t)mi_headp, &offset);
if (head_name != NULL && offset == 0) {
(void) sprintf(arena_name, "%s_", head_name);
} else {
(void) sprintf(arena_name, "Hex0x%p_",
(void *)mi_headp);
}
(void) sprintf(strchr(arena_name, '_') + 1, "minor");
mi_head = (mi_head_t *)mi_zalloc_sleep(sizeof (mi_head_t));
*mi_headp = (void *)mi_head;
/* Setup doubly linked list */
mi_head->mh_o.mi_o_next = &mi_head->mh_o;
mi_head->mh_o.mi_o_prev = &mi_head->mh_o;
mi_head->mh_o.mi_o_dev = 0; /* For asserts only */
mi_head->mh_arena = (vmem_t *)inet_minor_create(arena_name,
INET_MIN_DEV, MAXMIN, KM_SLEEP);
}
ASSERT(ptr != NULL);
mi_o = (MI_OP)ptr;
mi_o--;
if (sflag == MODOPEN) {
devp = NULL;
/*
* Set device number to MAXMIN + incrementing number.
*/
dev = MAXMIN + ++mi_head->mh_module_dev;
/* check for wraparound */
if (dev <= MAXMIN) {
dev = MAXMIN + 1;
mi_head->mh_module_dev = 1;
}
} else if (devp == NULL) {
/* Detached open */
dev = (dev_t)OPENFAIL;
} else if ((dev = inet_minor_alloc(mi_head->mh_arena)) == 0) {
return (EBUSY);
}
mi_o->mi_o_dev = dev;
insert = (&mi_head->mh_o);
mi_o->mi_o_next = insert;
insert->mi_o_prev->mi_o_next = mi_o;
mi_o->mi_o_prev = insert->mi_o_prev;
insert->mi_o_prev = mi_o;
if (sflag == MODOPEN)
mi_o->mi_o_isdev = B_FALSE;
else
mi_o->mi_o_isdev = B_TRUE;
if (devp)
*devp = makedevice(getemajor(*devp), (minor_t)dev);
return (0);
}
uint8_t *
mi_offset_param(mblk_t *mp, size_t offset, size_t len)
{
size_t msg_len;
if (!mp)
return (NULL);
msg_len = mp->b_wptr - mp->b_rptr;
if (msg_len == 0 || offset > msg_len || len > msg_len ||
(offset + len) > msg_len || len == 0)
return (NULL);
return (&mp->b_rptr[offset]);
}
uint8_t *
mi_offset_paramc(mblk_t *mp, size_t offset, size_t len)
{
uint8_t *param;
for (; mp; mp = mp->b_cont) {
int type = mp->b_datap->db_type;
if (datamsg(type)) {
if (param = mi_offset_param(mp, offset, len))
return (param);
if (offset < mp->b_wptr - mp->b_rptr)
break;
offset -= mp->b_wptr - mp->b_rptr;
}
}
return (NULL);
}
int
mi_sprintf(char *buf, char *fmt, ...)
{
va_list ap;
int count = -1;
va_start(ap, fmt);
if (buf) {
count = mi_iprintf(fmt, ap, (pfi_t)mi_sprintf_putc,
(char *)&buf);
if (count != -1)
(void) mi_sprintf_putc((char *)&buf, '\0');
}
va_end(ap);
return (count);
}
/* Used to count without writing data */
/* ARGSUSED1 */
static int
mi_sprintf_noop(char *cookie, int ch)
{
char **cpp = (char **)cookie;
(*cpp)++;
return (1);
}
int
mi_sprintf_putc(char *cookie, int ch)
{
char **cpp = (char **)cookie;
**cpp = (char)ch;
(*cpp)++;
return (1);
}
int
mi_strcmp(const char *cp1, const char *cp2)
{
while (*cp1++ == *cp2++) {
if (!cp2[-1])
return (0);
}
return ((uint_t)cp2[-1] & 0xFF) - ((uint_t)cp1[-1] & 0xFF);
}
size_t
mi_strlen(const char *str)
{
const char *cp = str;
while (*cp != '\0')
cp++;
return ((int)(cp - str));
}
int
mi_strlog(queue_t *q, char level, ushort_t flags, char *fmt, ...)
{
va_list ap;
char buf[200];
char *alloc_buf = buf;
int count = -1;
char *cp;
short mid;
int ret;
short sid;
sid = 0;
mid = 0;
if (q != NULL) {
mid = q->q_qinfo->qi_minfo->mi_idnum;
}
/* Find out how many bytes we need and allocate if necesary */
va_start(ap, fmt);
cp = buf;
count = mi_iprintf(fmt, ap, mi_sprintf_noop, (char *)&cp);
if (count > sizeof (buf) &&
!(alloc_buf = mi_alloc((uint_t)count + 2, BPRI_MED))) {
va_end(ap);
return (-1);
}
va_end(ap);
va_start(ap, fmt);
cp = alloc_buf;
count = mi_iprintf(fmt, ap, mi_sprintf_putc, (char *)&cp);
if (count != -1)
(void) mi_sprintf_putc((char *)&cp, '\0');
else
alloc_buf[0] = '\0';
va_end(ap);
ret = strlog(mid, sid, level, flags, alloc_buf);
if (alloc_buf != buf)
mi_free(alloc_buf);
return (ret);
}
long
mi_strtol(const char *str, char **ptr, int base)
{
const char *cp;
int digits;
long value;
boolean_t is_negative;
cp = str;
while (*cp == ' ' || *cp == '\t' || *cp == '\n')
cp++;
is_negative = (*cp == '-');
if (is_negative)
cp++;
if (base == 0) {
base = 10;
if (*cp == '0') {
base = 8;
cp++;
if (*cp == 'x' || *cp == 'X') {
base = 16;
cp++;
}
}
}
value = 0;
for (; *cp != '\0'; cp++) {
if (*cp >= '0' && *cp <= '9')
digits = *cp - '0';
else if (*cp >= 'a' && *cp <= 'f')
digits = *cp - 'a' + 10;
else if (*cp >= 'A' && *cp <= 'F')
digits = *cp - 'A' + 10;
else
break;
if (digits >= base)
break;
value = (value * base) + digits;
}
/* Note: we cast away const here deliberately */
if (ptr != NULL)
*ptr = (char *)cp;
if (is_negative)
value = -value;
return (value);
}
/*
* mi_timer mechanism.
*
* Each timer is represented by a timer mblk and a (streams) queue. When the
* timer fires the timer mblk will be put on the associated streams queue
* so that the streams module can process the timer even in its service
* procedure.
*
* The interface consists of 4 entry points:
* mi_timer_alloc - create a timer mblk
* mi_timer_free - free a timer mblk
* mi_timer - start, restart, stop, or move the
* timer to a different queue
* mi_timer_valid - called by streams module to verify that
* the timer did indeed fire.
*/
/*
* Start, restart, stop, or move the timer to a new queue.
* If "tim" is -2 the timer is moved to a different queue.
* If "tim" is -1 the timer is stopped.
* Otherwise, the timer is stopped if it is already running, and
* set to fire tim milliseconds from now.
*/
void
mi_timer(queue_t *q, MBLKP mp, clock_t tim)
{
MTBP mtb;
int state;
ASSERT(tim >= -2);
if (!q || !mp || (mp->b_rptr - mp->b_datap->db_base) != sizeof (MTB))
return;
mtb = (MTBP)mp->b_datap->db_base;
ASSERT(mp->b_datap->db_type == M_PCSIG);
if (tim >= 0) {
mtb->mtb_q = q;
state = mtb->mtb_state;
tim = MSEC_TO_TICK(tim);
if (state == TB_RUNNING) {
if (untimeout(mtb->mtb_tid) < 0) {
/* Message has already been putq */
ASSERT(mtb->mtb_q->q_first == mp ||
mp->b_prev || mp->b_next);
mtb->mtb_state = TB_RESCHED;
mtb->mtb_time_left = tim;
/* mi_timer_valid will start timer */
return;
}
} else if (state != TB_IDLE) {
ASSERT(state != TB_TO_BE_FREED);
if (state == TB_CANCELLED) {
ASSERT(mtb->mtb_q->q_first == mp ||
mp->b_prev || mp->b_next);
mtb->mtb_state = TB_RESCHED;
mtb->mtb_time_left = tim;
/* mi_timer_valid will start timer */
return;
}
if (state == TB_RESCHED) {
ASSERT(mtb->mtb_q->q_first == mp ||
mp->b_prev || mp->b_next);
mtb->mtb_time_left = tim;
/* mi_timer_valid will start timer */
return;
}
}
mtb->mtb_state = TB_RUNNING;
mtb->mtb_tid = timeout((pfv_t)mi_timer_fire, mtb, tim);
return;
}
switch (tim) {
case -1:
mi_timer_stop(mp);
break;
case -2:
mi_timer_move(q, mp);
break;
}
}
/*
* Allocate an M_PCSIG timer message. The space between db_base and
* b_rptr is used by the mi_timer mechanism, and after b_rptr there are
* "size" bytes that the caller can use for its own purposes.
*
* Note that db_type has to be a priority message since otherwise
* the putq will not cause the service procedure to run when
* there is flow control.
*/
MBLKP
mi_timer_alloc(size_t size)
{
MBLKP mp;
MTBP mtb;
if ((mp = allocb(size + sizeof (MTB), BPRI_HI)) != NULL) {
mp->b_datap->db_type = M_PCSIG;
mtb = (MTBP)mp->b_datap->db_base;
mp->b_rptr = (uchar_t *)&mtb[1];
mp->b_wptr = mp->b_rptr + size;
mtb->mtb_state = TB_IDLE;
mtb->mtb_mp = mp;
mtb->mtb_q = NULL;
return (mp);
}
return (NULL);
}
/*
* timeout() callback function.
* Put the message on the current queue.
* If the timer is stopped or moved to a different queue after
* it has fired then mi_timer() and mi_timer_valid() will clean
* things up.
*/
static int
mi_timer_fire(MTBP mtb)
{
ASSERT(mtb == (MTBP)mtb->mtb_mp->b_datap->db_base);
ASSERT(mtb->mtb_mp->b_datap->db_type == M_PCSIG);
return (putq(mtb->mtb_q, mtb->mtb_mp));
}
/*
* Logically free a timer mblk (that might have a pending timeout().)
* If the timer has fired and the mblk has been put on the queue then
* mi_timer_valid will free the mblk.
*/
void
mi_timer_free(MBLKP mp)
{
MTBP mtb;
int state;
if (!mp || (mp->b_rptr - mp->b_datap->db_base) != sizeof (MTB))
return;
mtb = (MTBP)mp->b_datap->db_base;
state = mtb->mtb_state;
if (state == TB_RUNNING) {
if (untimeout(mtb->mtb_tid) < 0) {
/* Message has already been putq */
ASSERT(mtb->mtb_q->q_first == mp ||
mp->b_prev || mp->b_next);
mtb->mtb_state = TB_TO_BE_FREED;
/* mi_timer_valid will free the mblk */
return;
}
} else if (state != TB_IDLE) {
/* Message has already been putq */
ASSERT(mtb->mtb_q->q_first == mp ||
mp->b_prev || mp->b_next);
ASSERT(state != TB_TO_BE_FREED);
mtb->mtb_state = TB_TO_BE_FREED;
/* mi_timer_valid will free the mblk */
return;
}
ASSERT(mtb->mtb_q == NULL || mtb->mtb_q->q_first != mp);
freemsg(mp);
}
/*
* Called from mi_timer(,,-2)
*/
void
mi_timer_move(queue_t *q, MBLKP mp)
{
MTBP mtb;
clock_t tim;
if (!q || !mp || (mp->b_rptr - mp->b_datap->db_base) != sizeof (MTB))
return;
mtb = (MTBP)mp->b_datap->db_base;
/*
* Need to untimeout and restart to make
* sure that the mblk is not about to be putq on the old queue
* by mi_timer_fire.
*/
if (mtb->mtb_state == TB_RUNNING) {
if ((tim = untimeout(mtb->mtb_tid)) < 0) {
/*
* Message has already been putq. Move from old queue
* to new queue.
*/
ASSERT(mtb->mtb_q->q_first == mp ||
mp->b_prev || mp->b_next);
rmvq(mtb->mtb_q, mp);
ASSERT(mtb->mtb_q->q_first != mp &&
mp->b_prev == NULL && mp->b_next == NULL);
mtb->mtb_q = q;
(void) putq(mtb->mtb_q, mp);
return;
}
mtb->mtb_q = q;
mtb->mtb_state = TB_RUNNING;
mtb->mtb_tid = timeout((pfv_t)mi_timer_fire, mtb, tim);
} else if (mtb->mtb_state != TB_IDLE) {
ASSERT(mtb->mtb_state != TB_TO_BE_FREED);
/*
* Message is already sitting on queue. Move to new queue.
*/
ASSERT(mtb->mtb_q->q_first == mp ||
mp->b_prev || mp->b_next);
rmvq(mtb->mtb_q, mp);
ASSERT(mtb->mtb_q->q_first != mp &&
mp->b_prev == NULL && mp->b_next == NULL);
mtb->mtb_q = q;
(void) putq(mtb->mtb_q, mp);
} else
mtb->mtb_q = q;
}
/*
* Called from mi_timer(,,-1)
*/
void
mi_timer_stop(MBLKP mp)
{
MTBP mtb;
int state;
if (!mp || (mp->b_rptr - mp->b_datap->db_base) != sizeof (MTB))
return;
mtb = (MTBP)mp->b_datap->db_base;
state = mtb->mtb_state;
if (state == TB_RUNNING) {
if (untimeout(mtb->mtb_tid) < 0) {
/* Message has already been putq */
ASSERT(mtb->mtb_q->q_first == mp ||
mp->b_prev || mp->b_next);
mtb->mtb_state = TB_CANCELLED;
} else {
mtb->mtb_state = TB_IDLE;
}
} else if (state == TB_RESCHED) {
ASSERT(mtb->mtb_q->q_first == mp ||
mp->b_prev || mp->b_next);
mtb->mtb_state = TB_CANCELLED;
}
}
/*
* The user of the mi_timer mechanism is required to call mi_timer_valid() for
* each M_PCSIG message processed in the service procedures.
* mi_timer_valid will return "true" if the timer actually did fire.
*/
boolean_t
mi_timer_valid(MBLKP mp)
{
MTBP mtb;
int state;
if (!mp || (mp->b_rptr - mp->b_datap->db_base) != sizeof (MTB) ||
mp->b_datap->db_type != M_PCSIG)
return (B_FALSE);
mtb = (MTBP)mp->b_datap->db_base;
state = mtb->mtb_state;
if (state != TB_RUNNING) {
ASSERT(state != TB_IDLE);
if (state == TB_TO_BE_FREED) {
/*
* mi_timer_free was called after the message
* was putq'ed.
*/
freemsg(mp);
return (B_FALSE);
}
if (state == TB_CANCELLED) {
/* The timer was stopped after the mblk was putq'ed */
mtb->mtb_state = TB_IDLE;
return (B_FALSE);
}
if (state == TB_RESCHED) {
/*
* The timer was stopped and then restarted after
* the mblk was putq'ed.
* mtb_time_left contains the number of ticks that
* the timer was restarted with.
*/
mtb->mtb_state = TB_RUNNING;
mtb->mtb_tid = timeout((pfv_t)mi_timer_fire,
mtb, mtb->mtb_time_left);
return (B_FALSE);
}
}
mtb->mtb_state = TB_IDLE;
return (B_TRUE);
}
static void
mi_tpi_addr_and_opt(MBLKP mp, char *addr, t_scalar_t addr_length,
char *opt, t_scalar_t opt_length)
{
struct T_unitdata_ind *tudi;
/*
* This code is used more than just for unitdata ind
* (also for T_CONN_IND and T_CONN_CON) and
* relies on correct functioning on the happy
* coincidence that the address and option buffers
* represented by length/offset in all these primitives
* are isomorphic in terms of offset from start of data
* structure
*/
tudi = (struct T_unitdata_ind *)mp->b_rptr;
tudi->SRC_offset = (t_scalar_t)(mp->b_wptr - mp->b_rptr);
tudi->SRC_length = addr_length;
if (addr_length > 0) {
bcopy(addr, (char *)mp->b_wptr, addr_length);
mp->b_wptr += addr_length;
}
tudi->OPT_offset = (t_scalar_t)(mp->b_wptr - mp->b_rptr);
tudi->OPT_length = opt_length;
if (opt_length > 0) {
bcopy(opt, (char *)mp->b_wptr, opt_length);
mp->b_wptr += opt_length;
}
}
MBLKP
mi_tpi_conn_con(MBLKP trailer_mp, char *src, t_scalar_t src_length, char *opt,
t_scalar_t opt_length)
{
size_t len;
MBLKP mp;
len = sizeof (struct T_conn_con) + src_length + opt_length;
if ((mp = mi_tpi_trailer_alloc(trailer_mp, len, T_CONN_CON)) != NULL) {
mp->b_wptr = &mp->b_rptr[sizeof (struct T_conn_con)];
mi_tpi_addr_and_opt(mp, src, src_length, opt, opt_length);
}
return (mp);
}
MBLKP
mi_tpi_conn_ind(MBLKP trailer_mp, char *src, t_scalar_t src_length, char *opt,
t_scalar_t opt_length, t_scalar_t seqnum)
{
size_t len;
MBLKP mp;
len = sizeof (struct T_conn_ind) + src_length + opt_length;
if ((mp = mi_tpi_trailer_alloc(trailer_mp, len, T_CONN_IND)) != NULL) {
mp->b_wptr = &mp->b_rptr[sizeof (struct T_conn_ind)];
mi_tpi_addr_and_opt(mp, src, src_length, opt, opt_length);
((struct T_conn_ind *)mp->b_rptr)->SEQ_number = seqnum;
mp->b_datap->db_type = M_PROTO;
}
return (mp);
}
MBLKP
mi_tpi_extconn_ind(MBLKP trailer_mp, char *src, t_scalar_t src_length,
char *opt, t_scalar_t opt_length, char *dst, t_scalar_t dst_length,
t_scalar_t seqnum)
{
size_t len;
MBLKP mp;
len = sizeof (struct T_extconn_ind) + src_length + opt_length +
dst_length;
if ((mp = mi_tpi_trailer_alloc(trailer_mp, len, T_EXTCONN_IND)) !=
NULL) {
mp->b_wptr = &mp->b_rptr[sizeof (struct T_extconn_ind)];
mi_tpi_addr_and_opt(mp, src, src_length, opt, opt_length);
((struct T_extconn_ind *)mp->b_rptr)->DEST_length = dst_length;
((struct T_extconn_ind *)mp->b_rptr)->DEST_offset =
(t_scalar_t)(mp->b_wptr - mp->b_rptr);
if (dst_length > 0) {
bcopy(dst, (char *)mp->b_wptr, dst_length);
mp->b_wptr += dst_length;
}
((struct T_extconn_ind *)mp->b_rptr)->SEQ_number = seqnum;
mp->b_datap->db_type = M_PROTO;
}
return (mp);
}
MBLKP
mi_tpi_discon_ind(MBLKP trailer_mp, t_scalar_t reason, t_scalar_t seqnum)
{
MBLKP mp;
struct T_discon_ind *tdi;
if ((mp = mi_tpi_trailer_alloc(trailer_mp,
sizeof (struct T_discon_ind), T_DISCON_IND)) != NULL) {
tdi = (struct T_discon_ind *)mp->b_rptr;
tdi->DISCON_reason = reason;
tdi->SEQ_number = seqnum;
}
return (mp);
}
/*
* Allocate and fill in a TPI err ack packet using the 'mp' passed in
* for the 'error_prim' context as well as sacrifice.
*/
MBLKP
mi_tpi_err_ack_alloc(MBLKP mp, t_scalar_t tlierr, int unixerr)
{
struct T_error_ack *teackp;
t_scalar_t error_prim;
if (!mp)
return (NULL);
error_prim = ((TPRIMP)mp->b_rptr)->type;
if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
M_PCPROTO, T_ERROR_ACK)) != NULL) {
teackp = (struct T_error_ack *)mp->b_rptr;
teackp->ERROR_prim = error_prim;
teackp->TLI_error = tlierr;
teackp->UNIX_error = unixerr;
}
return (mp);
}
MBLKP
mi_tpi_ok_ack_alloc_extra(MBLKP mp, int extra)
{
t_scalar_t correct_prim;
if (!mp)
return (NULL);
correct_prim = ((TPRIMP)mp->b_rptr)->type;
if ((mp = tpi_ack_alloc(mp, sizeof (struct T_ok_ack) + extra,
M_PCPROTO, T_OK_ACK)) != NULL) {
((struct T_ok_ack *)mp->b_rptr)->CORRECT_prim = correct_prim;
mp->b_wptr -= extra;
}
return (mp);
}
MBLKP
mi_tpi_ok_ack_alloc(MBLKP mp)
{
return (mi_tpi_ok_ack_alloc_extra(mp, 0));
}
MBLKP
mi_tpi_ordrel_ind(void)
{
MBLKP mp;
if ((mp = allocb(sizeof (struct T_ordrel_ind), BPRI_HI)) != NULL) {
mp->b_datap->db_type = M_PROTO;
((struct T_ordrel_ind *)mp->b_rptr)->PRIM_type = T_ORDREL_IND;
mp->b_wptr += sizeof (struct T_ordrel_ind);
}
return (mp);
}
static MBLKP
mi_tpi_trailer_alloc(MBLKP trailer_mp, size_t size, t_scalar_t type)
{
MBLKP mp;
if ((mp = allocb(size, BPRI_MED)) != NULL) {
mp->b_cont = trailer_mp;
mp->b_datap->db_type = M_PROTO;
((union T_primitives *)mp->b_rptr)->type = type;
mp->b_wptr += size;
}
return (mp);
}
MBLKP
mi_tpi_uderror_ind(char *dest, t_scalar_t dest_length, char *opt,
t_scalar_t opt_length, t_scalar_t error)
{
size_t len;
MBLKP mp;
struct T_uderror_ind *tudei;
len = sizeof (struct T_uderror_ind) + dest_length + opt_length;
if ((mp = allocb(len, BPRI_HI)) != NULL) {
mp->b_datap->db_type = M_PROTO;
tudei = (struct T_uderror_ind *)mp->b_rptr;
tudei->PRIM_type = T_UDERROR_IND;
tudei->ERROR_type = error;
mp->b_wptr = &mp->b_rptr[sizeof (struct T_uderror_ind)];
mi_tpi_addr_and_opt(mp, dest, dest_length, opt, opt_length);
}
return (mp);
}
IDP
mi_zalloc(size_t size)
{
IDP ptr;
if (ptr = mi_alloc(size, BPRI_LO))
bzero(ptr, size);
return (ptr);
}
IDP
mi_zalloc_sleep(size_t size)
{
IDP ptr;
if (ptr = mi_alloc_sleep(size, BPRI_LO))
bzero(ptr, size);
return (ptr);
}
|