summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_nlm.c
blob: 45c23ee1163c6097342e97401f30505cb10d5f9c (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 (c) 1991, 1998, 2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

/*
 * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
 * Copyright (c) 2012 by Delphix. All rights reserved.
 */

#include <sys/types.h>
#include <setjmp.h>
#include <string.h>

#ifdef notdef
#include <rpc/xdr.h>
#include <rpc/auth.h>
#include <rpc/rpc_msg.h>
#endif /* notdef */
#include <rpcsvc/nlm_prot.h>
#include "snoop.h"

extern char *dlc_header;
extern jmp_buf xdr_err;

extern void check_retransmit();
static void interpret_nlm_1();
static void interpret_nlm_3();
static void interpret_nlm_4();
static char *nameof_access();
static char *nameof_mode();
static char *nameof_stat();
static char *nameof_stat4();
static void show_cancargs();
static void show_cancargs4();
static void show_lock();
static void show_lock4();
static void show_lockargs();
static void show_lockargs4();
static void show_netobj();
static void show_nlm_access();
static void show_nlm_mode();
static void show_notify();
static void show_res();
static void show_res4();
static void show_share();
static void show_shareargs();
static void show_shareres();
static void show_shareres4();
static enum nlm_stats show_stat();
static enum nlm4_stats show_stat4();
static void show_testargs();
static void show_testargs4();
static void show_testres();
static void show_testres4();
static void show_unlockargs();
static void show_unlockargs4();
static void skip_netobj();
static char *sum_lock();
static char *sum_lock4();
static char *sum_netobj();
static char *sum_notify();
static char *sum_share();

void
interpret_nlm(flags, type, xid, vers, proc, data, len)
	int flags, type, xid, vers, proc;
	char *data;
	int len;
{
	switch (vers) {
	case 1:	interpret_nlm_1(flags, type, xid, vers, proc, data, len);
		break;
	case 3:	interpret_nlm_3(flags, type, xid, vers, proc, data, len);
		break;
	case 4:	interpret_nlm_4(flags, type, xid, vers, proc, data, len);
		break;
	}
}


/* ------------  V E R S I O N   1  ---------------------------------- */

static char *procnames_short_1[] = {
	"Null1",	/* 0 */
	"TEST1",	/* 1 */
	"LOCK1",	/* 2 */
	"CANCEL1",	/* 3 */
	"UNLOCK1",	/* 4 */
	"GRANTED1",	/* 5 */
	"TEST MSG1",	/* 6 */
	"LOCK MSG1",	/* 7 */
	"CANCEL MSG1",	/* 8 */
	"UNLOCK MSG1",	/* 9 */
	"GRANTED MSG1",	/* 10 */
	"TEST RES1",	/* 11 */
	"LOCK RES1",	/* 12 */
	"CANCEL RES1",	/* 13 */
	"UNLOCK RES1",	/* 14 */
	"GRANTED RES1",	/* 15 */
};

static char *procnames_long_1[] = {
	"Null procedure",	/* 0 */
	"Test",			/* 1 */
	"Lock",			/* 2 */
	"Cancel",		/* 3 */
	"Unlock",		/* 4 */
	"Granted",		/* 5 */
	"Test message",		/* 6 */
	"Lock message",		/* 7 */
	"Cancel message",	/* 8 */
	"Unlock message",	/* 9 */
	"Granted message",	/* 10 */
	"Test result",		/* 11 */
	"Lock result",		/* 12 */
	"Cancel result",	/* 13 */
	"Unlock result",	/* 14 */
	"Granted result",	/* 15 */
};

/* Highest procedure number that officially belongs to version 1. */
#define	MAXPROC_1	15

/* ARGSUSED */
static void
interpret_nlm_1(flags, type, xid, vers, proc, data, len)
	int flags, type, xid, vers, proc;
	char *data;
	int len;
{
	char *line;

	if (proc < 0 || proc > MAXPROC_1)
		return;

	if (flags & F_SUM) {
		if (setjmp(xdr_err)) {
			return;
		}

		line = get_sum_line();

		if (type == CALL) {
			(void) sprintf(line,
				"NLM C %s",
				procnames_short_1[proc]);
			line += strlen(line);
			switch (proc) {
			case NLM_TEST:
			case NLM_GRANTED:
			case NLM_TEST_MSG:
			case NLM_GRANTED_MSG:
				/* testargs */
				(void) strcat(line, sum_netobj("OH"));
				(void) getxdr_bool();	/* Excl */
				(void) strcat(line, sum_lock());
				break;
			case NLM_LOCK:
			case NLM_LOCK_MSG:
				/* lockargs */
				(void) strcat(line, sum_netobj("OH"));
				(void) getxdr_bool();	/* Block */
				(void) getxdr_bool();	/* Excl */
				(void) strcat(line, sum_lock());
				break;
			case NLM_CANCEL:
			case NLM_CANCEL_MSG:
				/* cancargs */
				(void) strcat(line, sum_netobj("OH"));
				(void) getxdr_bool();	/* Block */
				(void) getxdr_bool();	/* Excl */
				(void) strcat(line, sum_lock());
				break;
			case NLM_UNLOCK:
			case NLM_UNLOCK_MSG:
				/* unlockargs */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, sum_lock());
				break;
			case NLM_TEST_RES:
				/* testres */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, " ");
				(void) strcat(line,
				    nameof_stat(getxdr_u_long()));
				break;
			case NLM_LOCK_RES:
			case NLM_CANCEL_RES:
			case NLM_UNLOCK_RES:
			case NLM_GRANTED_RES:
				/* res */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, " ");
				(void) strcat(line,
					nameof_stat(getxdr_u_long()));
				break;
			}
			check_retransmit(line, (ulong_t)xid);
		} else {
			(void) sprintf(line, "NLM R %s",
				procnames_short_1[proc]);
			line += strlen(line);
			switch (proc) {
			case NLM_TEST:
				/* testres */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, " ");
				(void) strcat(line,
				    nameof_stat(getxdr_u_long()));
				break;
			case NLM_LOCK:
			case NLM_CANCEL:
			case NLM_UNLOCK:
			case NLM_GRANTED:
				/* res */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, " ");
				(void) strcat(line,
					nameof_stat(getxdr_u_long()));
				break;
			}
		}
	}

	if (flags & F_DTAIL) {
		show_header("NLM:  ", "Network Lock Manager", len);
		show_space();
		if (setjmp(xdr_err)) {
			return;
		}
		(void) sprintf(get_line(0, 0),
			"Proc = %d (%s)",
			proc, procnames_long_1[proc]);
		if (type == CALL) {
			switch (proc) {
			case NLM_TEST:
			case NLM_GRANTED:
			case NLM_TEST_MSG:
			case NLM_GRANTED_MSG:
				show_testargs();
				break;
			case NLM_LOCK:
			case NLM_LOCK_MSG:
				show_lockargs();
				break;
			case NLM_CANCEL:
			case NLM_CANCEL_MSG:
				show_cancargs();
				break;
			case NLM_UNLOCK:
			case NLM_UNLOCK_MSG:
				show_unlockargs();
				break;
			case NLM_TEST_RES:
				show_testres();
				break;
			case NLM_LOCK_RES:
			case NLM_CANCEL_RES:
			case NLM_UNLOCK_RES:
			case NLM_GRANTED_RES:
				show_res();
				break;
			}
		} else {
			switch (proc) {
			case NLM_TEST:
				show_testres();
				break;
			case NLM_LOCK:
			case NLM_CANCEL:
			case NLM_UNLOCK:
			case NLM_GRANTED:
				show_res();
				break;
			case NLM_TEST_MSG:
			case NLM_LOCK_MSG:
			case NLM_CANCEL_MSG:
			case NLM_UNLOCK_MSG:
			case NLM_GRANTED_MSG:
			case NLM_TEST_RES:
			case NLM_LOCK_RES:
			case NLM_CANCEL_RES:
			case NLM_UNLOCK_RES:
			case NLM_GRANTED_RES:
				break;
			}
		}
		show_trailer();
	}
}

#define	roundup(sz) ((sz / 4 + (sz % 4 > 0)) * 4)

/*
 * Skip a netobj.
 * Make sure an integral number of words
 * are skipped.
 */
static void
skip_netobj()
{
	int sz = getxdr_u_long();

	xdr_skip(roundup(sz));
}

static char *
sum_netobj(handle)
	char *handle;
{
	int i, l, sz;
	int sum = 0;
	static char buff[32];

	sz = getxdr_u_long();
	for (i = 0; i < sz; i += 4) {
		l =  getxdr_long();
		sum ^= (l >> 16) ^ l;
	}
	(void) sprintf(buff, " %s=%04X", handle, sum & 0xFFFF);
	return (buff);
}

static void
show_netobj(fmt)
	char *fmt;
{
	int sz, chunk;
	char *p;
	char buff[64];
	int needspace;

	sz = getxdr_u_long();		/* size of the netobj */

	if (sz == 0) {
		(void) sprintf(get_line(0, 0), fmt, "<null>");
	} else {
		needspace = sz > 16;
		(void) strcpy(buff, fmt);
		while (sz > 0) {
			chunk = sz > 16 ? 16 : sz;
			sz -= 16;
			(void) showxdr_hex(chunk, buff);
			/*
			 * For every line after the first, blank out
			 * everything in the format string before the "%s".
			 */
			for (p = buff; *p != '%'; p++)
				*p = ' ';
		}
		if (needspace)
			show_space();
	}
}

static char *
sum_lock()
{
	static char buff[LM_MAXSTRLEN + 1];
	char *cp = buff;
	long id;
	ulong_t off, len;

	(void) getxdr_string(buff, LM_MAXSTRLEN);	/* Caller */
	(void) strcpy(buff, sum_netobj("FH"));		/* Fh */
	cp += strlen(buff);
	skip_netobj();					/* Owner */
	id  = getxdr_long();
	off = getxdr_u_long();
	len = getxdr_u_long();
	(void) sprintf(cp, " PID=%ld Region=%lu:%lu", id, off, len);
	return (buff);
}

static void
show_lock()
{
	showxdr_string(LM_MAXSTRLEN, "Caller = %s");
	show_netobj("Filehandle = %s");
	show_netobj("Lock owner = %s");
	showxdr_long("Svid = %ld (process id)");
	showxdr_u_long("Offset = %lu bytes");
	showxdr_u_long("Length = %lu bytes");
}

static void
show_cancargs()
{
	show_netobj("Cookie = %s");
	showxdr_bool("Block = %s");
	showxdr_bool("Exclusive = %s");
	show_lock();
}

static void
show_lockargs()
{
	show_netobj("Cookie = %s");
	showxdr_bool("Block = %s");
	showxdr_bool("Exclusive = %s");
	show_lock();
	showxdr_bool("Reclaim = %s");
	showxdr_long("State = %ld");
}

static void
show_unlockargs()
{
	show_netobj("Cookie = %s");
	show_lock();
}

static void
show_testargs()
{
	show_netobj("Cookie = %s");
	showxdr_bool("Exclusive = %s");
	show_lock();
}

static void
show_res()
{
	show_netobj("Cookie = %s");
	(void) show_stat();
}

static char *
nameof_stat(s)
	ulong_t s;
{
	switch ((enum nlm_stats) s) {
	case nlm_granted:	return ("granted");
	case nlm_denied:	return ("denied");
	case nlm_denied_nolocks:return ("denied (no locks)");
	case nlm_blocked:	return ("blocked");
	case nlm_denied_grace_period: return ("denied (grace period)");
	case nlm_deadlck:	return ("deadlock");
	default:		return ("?");
	}
}

static enum nlm_stats
show_stat()
{
	enum nlm_stats s;

	s = (enum nlm_stats) getxdr_u_long();
	(void) sprintf(get_line(0, 0),
	    "Status = %d (%s)",
	    s, nameof_stat((ulong_t)s));

	return (s);
}

static void
show_testres()
{
	show_netobj("Cookie = %s");
	if (show_stat() == nlm_denied) {
		showxdr_bool("Exclusive = %s");
		showxdr_long("Svid = %ld (process id)");
		show_netobj("Owner handle = %s");
		showxdr_u_long("Offset = %lu bytes");
		showxdr_u_long("Length = %lu bytes");
	}
}


/* ------------  V E R S I O N   3  ---------------------------------- */

static char *procnames_short_3[] = {
	"SHARE3",	/* 20 */
	"UNSHARE3",	/* 21 */
	"NM_LOCK3",	/* 22 */
	"FREEALL3",	/* 23 */
};

static char *procnames_long_3[] = {
	"Share",		/* 20 */
	"Unshare",		/* 21 */
	"Unmonitored lock",	/* 22 */
	"Free all",		/* 23 */
};

/* Maximum procedure number for version 3. */
#define	MAXPROC_3	23

static void
interpret_nlm_3(flags, type, xid, vers, proc, data, len)
	int flags, type, xid, vers, proc;
	char *data;
	int len;
{
	char *line, *pl;
	ulong_t i;

	if (proc < 0 || proc > MAXPROC_3)
		return;

	/*
	 * Version 3 is a superset of version 1
	 */
	if (proc >= 0 && proc <= MAXPROC_1) {
		interpret_nlm_1(flags, type, xid, vers, proc, data, len);
		return;
	}

	if (flags & F_SUM) {
		if (setjmp(xdr_err)) {
			return;
		}

		line = get_sum_line();

		if (type == CALL) {
			(void) sprintf(line,
				"NLM C %s",
				procnames_short_3[proc-20]);
			line += strlen(line);
			switch (proc) {
			case NLM_SHARE:
			case NLM_UNSHARE:
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, sum_share());
				break;
			case NLM_NM_LOCK:
				/* lockargs */
				skip_netobj();
				(void) getxdr_u_long(); /* Block */
				(void) getxdr_u_long(); /* Excl */
				(void) strcat(line, sum_lock());
				break;
			case NLM_FREE_ALL:
				(void) sprintf(line,
					" %s", sum_notify());
				break;
			}
			check_retransmit(line, (ulong_t)xid);
		} else {
			(void) sprintf(line, "NLM R %s",
				procnames_short_3[proc-20]);
			line += strlen(line);
			switch (proc) {
			case NLM_SHARE:
			case NLM_UNSHARE:
				pl = sum_netobj("OH");
				i = getxdr_u_long();
				sprintf(line, "%s %s %ld",
					pl, nameof_stat(i), getxdr_long());
				break;
			case NLM_NM_LOCK:
				/* res */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, " ");
				(void) strcat(line,
					nameof_stat(getxdr_u_long()));
				break;
			case NLM_FREE_ALL:
				break;
			}
		}
	}

	if (flags & F_DTAIL) {
		show_header("NLM:  ", "Network Lock Manager", len);
		show_space();
		if (setjmp(xdr_err)) {
			return;
		}
		(void) sprintf(get_line(0, 0),
			"Proc = %d (%s)",
			proc, procnames_long_3[proc-20]);
		if (type == CALL) {
			switch (proc) {
			case NLM_SHARE:
			case NLM_UNSHARE:
				show_shareargs();
				break;
			case NLM_NM_LOCK:
				show_lockargs();
				break;
			case NLM_FREE_ALL:
				show_notify();
				break;
			}
		} else {
			switch (proc) {
			case NLM_SHARE:
			case NLM_UNSHARE:
				show_shareres();
				break;
			case NLM_NM_LOCK:
				show_res();
				break;
			case NLM_FREE_ALL:
				break;
			}
		}
		show_trailer();
	}
}

static char *
nameof_mode(m)
	uint_t m;
{
	switch ((enum fsh_mode) m) {
	case fsm_DN:	return ("deny none");
	case fsm_DR:	return ("deny read");
	case fsm_DW:	return ("deny write");
	case fsm_DRW:	return ("deny read/write");
	default:	return ("?");
	}
}

static char *
nameof_access(a)
	uint_t a;
{
	switch ((enum fsh_access) a) {
	case fsa_NONE:	return ("?");
	case fsa_R:	return ("read only");
	case fsa_W:	return ("write only");
	case fsa_RW:	return ("read/write");
	default:	return ("?");
	}
}

static void
show_nlm_mode()
{
	enum fsh_mode m;

	m = (enum fsh_mode) getxdr_u_long();
	(void) sprintf(get_line(0, 0),
	    "Mode = %d (%s)",
	    m, nameof_mode((uint_t)m));
}

static void
show_nlm_access()
{
	enum fsh_access a;

	a = (enum fsh_access) getxdr_u_long();
	(void) sprintf(get_line(0, 0),
	    "Access = %d (%s)",
	    a, nameof_access((uint_t)a));
}

static char *
sum_share()
{
	static char buff[LM_MAXSTRLEN + 1];
	char *cp = buff;
	ulong_t mode, access;

	(void) getxdr_string(buff, LM_MAXSTRLEN);	/* Caller */
	(void) strcpy(buff, sum_netobj("FH"));		/* Fh */
	cp += strlen(buff);
	skip_netobj();					/* Owner */
	mode = getxdr_u_long();
	access = getxdr_u_long();
	(void) sprintf(cp, " Mode=%lu Access=%lu", mode, access);
	return (buff);
}

static void
show_share()
{
	showxdr_string(LM_MAXSTRLEN, "Caller = %s");
	show_netobj("Filehandle = %s");
	show_netobj("Lock owner = %s");
	show_nlm_mode();
	show_nlm_access();
}

static void
show_shareargs()
{
	show_netobj("Cookie = %s");
	show_share();
	showxdr_bool("Reclaim = %s");
}

static void
show_shareres()
{
	show_netobj("Cookie = %s");
	(void) show_stat();
	showxdr_long("Sequence = %d");
}

static void
show_notify()
{
	showxdr_string(LM_MAXNAMELEN, "Name = %s");
	showxdr_long("State = %d");
}

#define	NOTIFY_PAD	(sizeof (" State=-2147483648") + 1)

static char *
sum_notify()
{
	static char buff[LM_MAXNAMELEN + NOTIFY_PAD];
	char *cp = buff;
	long state;

	(void) getxdr_string(buff, LM_MAXNAMELEN);
	cp += strlen(buff);
	state  = getxdr_long();
	(void) sprintf(cp, " State=%ld", state);
	return (buff);
}

/* ------------  V E R S I O N   4  ---------------------------------- */

static char *procnames_short_4[] = {
	"Null4",	/* 0 */
	"TEST4",	/* 1 */
	"LOCK4",	/* 2 */
	"CANCEL4",	/* 3 */
	"UNLOCK4",	/* 4 */
	"GRANTED4",	/* 5 */
	"TEST MSG4",	/* 6 */
	"LOCK MSG4",	/* 7 */
	"CANCEL MSG4",	/* 8 */
	"UNLOCK MSG4",	/* 9 */
	"GRANTED MSG4",	/* 10 */
	"TEST RES4",	/* 11 */
	"LOCK RES4",	/* 12 */
	"CANCEL RES4",	/* 13 */
	"UNLOCK RES4",	/* 14 */
	"GRANTED RES4",	/* 15 */
	"PROC 16 v4",	/* 16 */
	"PROC 17 v4",	/* 17 */
	"PROC 18 v4",	/* 18 */
	"PROC 19 v4",	/* 19 */
	"SHARE4",	/* 20 */
	"UNSHARE4",	/* 21 */
	"NM_LOCK4",	/* 22 */
	"FREEALL4",	/* 23 */
};

static char *procnames_long_4[] = {
	"Null procedure",	/* 0 */
	"Test",			/* 1 */
	"Lock",			/* 2 */
	"Cancel",		/* 3 */
	"Unlock",		/* 4 */
	"Granted",		/* 5 */
	"Test message",		/* 6 */
	"Lock message",		/* 7 */
	"Cancel message",	/* 8 */
	"Unlock message",	/* 9 */
	"Granted message",	/* 10 */
	"Test result",		/* 11 */
	"Lock result",		/* 12 */
	"Cancel result",	/* 13 */
	"Unlock result",	/* 14 */
	"Granted result",	/* 15 */
	"Procedure 16",		/* 16 */
	"Procedure 17",		/* 17 */
	"Procedure 18",		/* 18 */
	"Procedure 19",		/* 19 */
	"Share",		/* 20 */
	"Unshare",		/* 21 */
	"Unmonitored lock",	/* 22 */
	"Free all",		/* 23 */
};

/* Maximum procedure number for version 4. */
#define	MAXPROC_4	23

/* ARGSUSED */
static void
interpret_nlm_4(flags, type, xid, vers, proc, data, len)
	int flags, type, xid, vers, proc;
	char *data;
	int len;
{
	char *line;
	char *pl;
	ulong_t i;

	if (proc < 0 || proc > MAXPROC_4)
		return;

	if (flags & F_SUM) {
		if (setjmp(xdr_err)) {
			return;
		}

		line = get_sum_line();

		if (type == CALL) {
			(void) sprintf(line,
				"NLM C %s",
				procnames_short_4[proc]);
			line += strlen(line);
			switch (proc) {
			case NLM4_TEST:
			case NLM4_GRANTED:
			case NLM4_TEST_MSG:
			case NLM4_GRANTED_MSG:
				/* testargs */
				(void) strcat(line, sum_netobj("OH"));
				(void) getxdr_bool();	/* Excl */
				(void) strcat(line, sum_lock4());
				break;
			case NLM4_LOCK:
			case NLM4_LOCK_MSG:
				/* lockargs */
				(void) strcat(line, sum_netobj("OH"));
				(void) getxdr_bool();	/* Block */
				(void) getxdr_bool();	/* Excl */
				(void) strcat(line, sum_lock4());
				/* ignore reclaim, state fields */
				break;
			case NLM4_CANCEL:
			case NLM4_CANCEL_MSG:
				/* cancargs */
				(void) strcat(line, sum_netobj("OH"));
				(void) getxdr_bool();	/* Block */
				(void) getxdr_bool();	/* Excl */
				(void) strcat(line, sum_lock4());
				break;
			case NLM4_UNLOCK:
			case NLM4_UNLOCK_MSG:
				/* unlockargs */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, sum_lock4());
				break;
			case NLM4_TEST_RES:
				/* testres */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, " ");
				(void) strcat(line,
				    nameof_stat4(getxdr_u_long()));
				break;
			case NLM4_LOCK_RES:
			case NLM4_CANCEL_RES:
			case NLM4_UNLOCK_RES:
			case NLM4_GRANTED_RES:
				/* res */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, " ");
				(void) strcat(line,
					nameof_stat4(getxdr_u_long()));
				break;
			case NLM4_SHARE:
			case NLM4_UNSHARE:
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, sum_share());
				break;
			case NLM4_NM_LOCK:
				/* lockargs */
				skip_netobj();		/* Cookie */
				(void) getxdr_bool();	/* Block */
				(void) getxdr_bool();	/* Excl */
				(void) strcat(line, sum_lock4());
				/* skip reclaim & state fields */
				break;
			case NLM4_FREE_ALL:
				(void) sprintf(line,
					" %s", sum_notify());
				break;
			}
			check_retransmit(line, (ulong_t)xid);
		} else {
			(void) sprintf(line, "NLM R %s",
				procnames_short_4[proc]);
			line += strlen(line);
			switch (proc) {
			case NLM4_TEST:
				/* testres */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, " ");
				(void) strcat(line,
				    nameof_stat4(getxdr_u_long()));
				break;
			case NLM4_LOCK:
			case NLM4_CANCEL:
			case NLM4_UNLOCK:
			case NLM4_GRANTED:
			case NLM4_NM_LOCK:
				/* res */
				(void) strcat(line, sum_netobj("OH"));
				(void) strcat(line, " ");
				(void) strcat(line,
					nameof_stat4(getxdr_u_long()));
				break;
			case NLM4_SHARE:
			case NLM4_UNSHARE:
				/* shareres */
				pl = sum_netobj("OH");
				i = getxdr_u_long();
				sprintf(line, "%s %s %ld",
					pl, nameof_stat4(i), getxdr_long());
				break;
			case NLM4_FREE_ALL:
				break;
			}
		}
	}

	if (flags & F_DTAIL) {
		show_header("NLM:  ", "Network Lock Manager", len);
		show_space();
		if (setjmp(xdr_err)) {
			return;
		}
		(void) sprintf(get_line(0, 0),
			"Proc = %d (%s)",
			proc, procnames_long_4[proc]);
		if (type == CALL) {
			switch (proc) {
			case NLM4_TEST:
			case NLM4_GRANTED:
			case NLM4_TEST_MSG:
			case NLM4_GRANTED_MSG:
				show_testargs4();
				break;
			case NLM4_LOCK:
			case NLM4_LOCK_MSG:
			case NLM4_NM_LOCK:
				show_lockargs4();
				break;
			case NLM4_CANCEL:
			case NLM4_CANCEL_MSG:
				show_cancargs4();
				break;
			case NLM4_UNLOCK:
			case NLM4_UNLOCK_MSG:
				show_unlockargs4();
				break;
			case NLM4_TEST_RES:
				show_testres4();
				break;
			case NLM4_LOCK_RES:
			case NLM4_CANCEL_RES:
			case NLM4_UNLOCK_RES:
			case NLM4_GRANTED_RES:
				show_res4();
				break;
			case NLM4_SHARE:
			case NLM4_UNSHARE:
				show_shareargs();
				break;
			case NLM4_FREE_ALL:
				show_notify();
				break;
			}
		} else {
			switch (proc) {
			case NLM4_TEST:
				show_testres4();
				break;
			case NLM4_LOCK:
			case NLM4_CANCEL:
			case NLM4_UNLOCK:
			case NLM4_GRANTED:
			case NLM_NM_LOCK:
				show_res4();
				break;
			case NLM4_TEST_MSG:
			case NLM4_LOCK_MSG:
			case NLM4_CANCEL_MSG:
			case NLM4_UNLOCK_MSG:
			case NLM4_GRANTED_MSG:
			case NLM4_TEST_RES:
			case NLM4_LOCK_RES:
			case NLM4_CANCEL_RES:
			case NLM4_UNLOCK_RES:
			case NLM4_GRANTED_RES:
				break;
			case NLM_SHARE:
			case NLM_UNSHARE:
				show_shareres4();
				break;
			case NLM_FREE_ALL:
				break;
			}
		}
		show_trailer();
	}
}

static char *
sum_lock4()
{
	static char buff[LM_MAXSTRLEN + 1];
	char *cp = buff;
	long id;
	u_longlong_t off, len;

	(void) getxdr_string(buff, LM_MAXSTRLEN);	/* Caller */
	(void) strcpy(buff, sum_netobj("FH"));		/* Fh */
	cp += strlen(buff);
	skip_netobj();					/* Owner */
	id  = getxdr_long();
	off = getxdr_u_longlong();
	len = getxdr_u_longlong();
	(void) sprintf(cp, " PID=%ld Region=%llu:%llu", id, off, len);
	return (buff);
}

static void
show_lock4()
{
	showxdr_string(LM_MAXSTRLEN, "Caller = %s");
	show_netobj("Filehandle = %s");
	show_netobj("Lock owner = %s");
	showxdr_long("Svid = %ld (process id)");
	showxdr_u_longlong("Offset = %llu bytes");
	showxdr_u_longlong("Length = %llu bytes");
}

static void
show_cancargs4()
{
	show_netobj("Cookie = %s");
	showxdr_bool("Block = %s");
	showxdr_bool("Exclusive = %s");
	show_lock4();
}

static void
show_lockargs4()
{
	show_netobj("Cookie = %s");
	showxdr_bool("Block = %s");
	showxdr_bool("Exclusive = %s");
	show_lock4();
	showxdr_bool("Reclaim = %s");
	showxdr_long("State = %ld");
}

static void
show_unlockargs4()
{
	show_netobj("Cookie = %s");
	show_lock4();
}

static void
show_testargs4()
{
	show_netobj("Cookie = %s");
	showxdr_bool("Exclusive = %s");
	show_lock4();
}

static void
show_res4()
{
	show_netobj("Cookie = %s");
	(void) show_stat4();
}

static char *
nameof_stat4(s)
	ulong_t s;
{
	switch ((enum nlm4_stats) s) {
	case nlm4_granted:	return ("granted");
	case nlm4_denied:	return ("denied");
	case nlm4_denied_nolocks:return ("denied (no locks)");
	case nlm4_blocked:	return ("blocked");
	case nlm4_denied_grace_period: return ("denied (grace period)");
	case nlm4_deadlck:	return ("deadlock");
	case nlm4_rofs:		return ("read-only fs");
	case nlm4_stale_fh:	return ("stale fh");
	case nlm4_fbig:		return ("file too big");
	case nlm4_failed:	return ("failed");
	default:		return ("?");
	}
}

static enum nlm4_stats
show_stat4()
{
	enum nlm4_stats s;

	s = (enum nlm4_stats) getxdr_u_long();
	(void) sprintf(get_line(0, 0),
	    "Status = %d (%s)",
	    s, nameof_stat4((ulong_t)s));

	return (s);
}

static void
show_testres4()
{
	show_netobj("Cookie = %s");
	if (show_stat() == nlm_denied) {
		showxdr_bool("Exclusive = %s");
		showxdr_long("Svid = %ld (process id)");
		show_netobj("Owner handle = %s");
		showxdr_u_longlong("Offset = %llu bytes");
		showxdr_u_longlong("Length = %llu bytes");
	}
}

static void
show_shareres4()
{
	show_netobj("Cookie = %s");
	(void) show_stat4();
	showxdr_long("Sequence = %d");
}