summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_zip.c
blob: b1093b033b05171d39c4627567ff4a1048979aa2 (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
/*
 * 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) 2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <sys/types.h>

#include <at.h>
#include <snoop.h>

char *print_macaddr(uint8_t *, int);

static char *zip_flags(char);
static char *zip_flags_long(char);

void
interpret_ddp_zip(int flags, struct zip_hdr *zip, int len)
{
	int cnt;
	uint16_t net;
	uint16_t range;
	uint8_t *p;
	char zone[33];
	char defzone[60] = "";
	char mcast[50] = "";
	uint8_t gniflags;
	uint8_t *tail = (uint8_t *)zip + len;

	if (flags & F_SUM) {
		if (len < sizeof (struct zip_hdr))
			goto out;

		switch (zip->zip_func) {
		case ZIP_QUERY:
			cnt = zip->zip_netcnt;
			(void) snprintf(get_sum_line(), MAXLINE,
			    "ZIP Query CNT = %d", cnt);
			break;
		case ZIP_REPLY:
		case ZIP_EXT_REPLY:
			cnt = zip->zip_netcnt;
			(void) snprintf(get_sum_line(), MAXLINE,
			    "ZIP Reply CNT = %d", cnt);
			break;
		case ZIP_GET_NET_INFO:
			p = &zip->zip_func;

			if ((p+6 > tail) || (p+7+p[6] > tail))
				goto out;

			(void) snprintf(get_sum_line(), MAXLINE,
			    "ZIP GNI Zone = \"%.*s\"", p[6], &p[7]);
			break;
		case ZIP_GET_NET_INFO_REPLY:
			p = &zip->zip_func;

			gniflags = p[1];
			(void) snprintf(get_sum_line(), MAXLINE,
			    "ZIP GNI Rep Flags 0x%x (%s)",
			    gniflags, zip_flags(gniflags));
			break;
		default:
			(void) snprintf(get_sum_line(), MAXLINE,
			    "ZIP CMD = %d", zip->zip_func);
			break;
		}
	}

	if (flags & F_DTAIL) {
		show_header("ZIP:  ", "ZIP Header", len);
		show_space();

		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Length = %d", len);

		if (len < sizeof (struct zip_hdr))
			goto out;

		switch (zip->zip_func) {
		case ZIP_QUERY:
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Query, Network count = %d", zip->zip_netcnt);
			cnt = zip->zip_netcnt;
			p = (uint8_t *)(zip + 1);
			while (cnt--) {
				if (p+2 > tail)
					goto out;
				net = get_short(p);
				p += 2;
				(void) snprintf(get_line(0, 0),
				    get_line_remain(), "Net = %d", net);
			}
			break;
		case ZIP_REPLY:
		case ZIP_EXT_REPLY:
			cnt = zip->zip_netcnt;
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Reply, Network count = %d", cnt);

			p = (uint8_t *)(zip + 1);
			while (cnt--) {
				if (p+2 > tail)
					goto out;
				net = get_short(p);
				p += 2;
				if (p+1 > tail || (&p[1] + p[0]) > tail)
					goto out;
				(void) snprintf(get_line(0, 0),
				    get_line_remain(),
				    "Network = %d, Zone = \"%.*s\"",
				    net, p[0], &p[1]);
				p += p[0] + 1;
			}
			break;
		case ZIP_GET_NET_INFO:
			p = &zip->zip_func;
			if (p+1 > tail || (&p[1] + p[0]) > tail)
				goto out;
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "GetNetInfo Zone = \"%.*s\"", p[0], &p[1]);
			break;
		case ZIP_GET_NET_INFO_REPLY:
			p = &zip->zip_func;
			if (p+5 > tail)
				goto out;
			gniflags = p[1];
			net = get_short(&p[2]);
			range = get_short(&p[4]);

			if (p+7 > tail || (&p[7] + p[6]) > tail)
				goto out;
			(void) snprintf(zone, sizeof (zone),
			    "%.*s", p[6], &p[7]);
			p = &p[7] + p[6];

			if ((gniflags & ZIP_FLG_USEBRC) == 0) {
				if (p+1 > tail || (&p[1] + p[0]) > tail)
					goto out;
				(void) snprintf(mcast, sizeof (mcast),
				    "Multicast address = %s",
				    print_macaddr(&p[1], p[0]));
			}

			if (gniflags & ZIP_FLG_ZINV) {
				p = &p[1] + p[0];
				if (p+1 > tail || (&p[1] + p[0]) > tail)
					goto out;
				(void) snprintf(defzone, sizeof (defzone),
				    "Default Zone = \"%.*s\"",
				    p[0], &p[1]);
			}
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "GetNetInfo Reply, Flags 0x%x (%s)",
			    gniflags, zip_flags_long(gniflags));

			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Network number = %d-%d", net, range);

			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Zone = \"%s\"", zone);

			if (mcast[0])
				(void) snprintf(get_line(0, 0),
				    get_line_remain(),
				    "%s", mcast);

			if (defzone[0])
				(void) snprintf(get_line(0, 0),
				    get_line_remain(),
				    "%s", defzone);

			break;
		case ZIP_NOTIFY:
			p = &zip->zip_func;
			if (p+5 > tail)
				goto out;

			gniflags = p[1];
			net = get_short(&p[2]);
			range = get_short(&p[4]);

			if (p+7 > tail || (&p[7] + p[6]) > tail)
				goto out;
			(void) snprintf(zone, sizeof (zone),
			    "%.*s", p[6], &p[7]);
			p = &p[7] + p[6];

			if ((gniflags & ZIP_FLG_USEBRC) == 0) {
				if (p+1 > tail || (&p[1] + p[0]) > tail)
					goto out;
				(void) snprintf(mcast, sizeof (mcast),
				    "New Multicast address = %s",
				    print_macaddr(&p[1], p[0]));
			}

			if (p+1 > tail || (&p[1] + p[0]) > tail)
				goto out;

			p = &p[1] + p[0];

			if (p+1 > tail || (&p[1] + p[0]) > tail)
				goto out;

			(void) snprintf(defzone, sizeof (defzone),
			    "New Default Zone = \"%.*s\"",
			    p[0], &p[1]);

			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Notify, Flags 0x%x (%s)",
			    gniflags, zip_flags_long(gniflags));

			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Old Zone = \"%s\"", zone);

			if (mcast[0])
				(void) snprintf(get_line(0, 0),
				    get_line_remain(), "%s", mcast);

			if (defzone[0])
				(void) snprintf(get_line(0, 0),
				    get_line_remain(), "%s", defzone);

			break;
		default:
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "Op = %d", zip->zip_func);
			break;
		}
	}
	return;
out:
	if (flags & F_SUM)
		(void) snprintf(get_sum_line(), MAXLINE,
		    "ZIP (short packet)");
	if (flags & F_DTAIL)
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "ZIP (short packet)");
}

static char *
zip_flags(char flags)
{
	static char buf[50];
	char *p = buf;
	char *tail = &buf[sizeof (buf)];

	buf[0] = '\0';

	if (flags & ZIP_FLG_ZINV)
		p += snprintf(p, tail-p, "IZ");

	if (flags & ZIP_FLG_USEBRC)
		p += snprintf(p, tail-p, p == buf ? "UB" : " UB");

	if (flags & ZIP_FLG_ONEZ)
		(void) snprintf(p, tail-p, p == buf ? "OOZ" : " OOZ");

	return (buf);
}

static char *
zip_flags_long(char flags)
{
	static char buf[50];
	char *p = buf;
	char *tail = &buf[sizeof (buf)];

	buf[0] = '\0';

	if (flags & ZIP_FLG_ZINV)
		p += snprintf(p, tail-p, "ZoneInvalid");

	if (flags & ZIP_FLG_USEBRC)
		p += snprintf(p, tail-p,
		    p == buf ? "UseBroadcast" : " UseBroadcast");

	if (flags & ZIP_FLG_ONEZ)
		(void) snprintf(p, tail-p,
		    p == buf ? "OnlyOneZone" : " OnlyOneZone");

	return (buf);
}

void
interpret_atp_zip(int flags, struct atp_hdr *atp, int len)
{
	int cnt;
	uint8_t *data;
	uint8_t *tail = (uint8_t *)(atp+1) + len;

	if (flags & F_SUM) {
		if (len < 0) {
			(void) snprintf(get_sum_line(), MAXLINE,
			    "ZIP (short packet)");
			return;
		}

		switch (atp_fun(atp->atp_ctrl)) {
		case ATP_TREQ:
			switch (atp->atp_user[0]) {
			case ZIP_ATP_GETMYZONE:
				(void) snprintf(get_sum_line(), MAXLINE,
				    "ZIP GetMyZone");
				break;

			case ZIP_ATP_GETZONELIST:
				(void) snprintf(get_sum_line(), MAXLINE,
				    "ZIP GetZoneList");
				break;

			case ZIP_ATP_GETLOCALZONES:
				(void) snprintf(get_sum_line(), MAXLINE,
				    "ZIP GetLocalZones");
				break;
			}
			break;
		case ATP_TRESP:
			cnt = get_short(&atp->atp_user[2]);
			(void) snprintf(get_sum_line(), MAXLINE,
			    "ZIP ZoneReply, Cnt = %d", cnt);

			break;
		}
	}

	if (flags & F_DTAIL) {
		show_header("ZIP:  ", "ZIP Header", len);
		show_space();

		if (len < 0) {
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "ZIP (short packet)");
			return;
		}

		switch (atp_fun(atp->atp_ctrl)) {
		case ATP_TREQ:
			switch (atp->atp_user[0]) {
			case ZIP_ATP_GETMYZONE:
				(void) snprintf(get_line(0, 0),
				    get_line_remain(),
				    "GetMyZone, Start Index = %d",
				    get_short(&atp->atp_user[2]));
				break;
			case ZIP_ATP_GETZONELIST:
				(void) snprintf(get_line(0, 0),
				    get_line_remain(),
				    "GetZoneList, Start Index = %d",
				    get_short(&atp->atp_user[2]));
				break;
			case ZIP_ATP_GETLOCALZONES:
				(void) snprintf(get_line(0, 0),
				    get_line_remain(),
				    "GetLocalZones, Start Index = %d",
				    get_short(&atp->atp_user[2]));
				break;
			}
			break;
		case ATP_TRESP:
			cnt = get_short(&atp->atp_user[2]);
			(void) snprintf(get_line(0, 0), get_line_remain(),
			    "ZoneReply, Number of Zones = %d, Length = %d",
			    cnt, len);

			data = (uint8_t *)atp + DDPHDR_SIZE + ATPHDR_SIZE;

			while (cnt--) {
				if (data > tail ||
				    (&data[1] + data[0]) > tail) {
					(void) snprintf(get_line(0, 0),
					    get_line_remain(),
					    "ZoneReply (short packet)");
					return;
				}
				(void) snprintf(get_line(0, 0),
				    get_line_remain(),
				    "Zone = \"%.*s\"", data[0], &data[1]);
				data += data[0] + 1;
			}
			break;
		}
	}
}