aboutsummaryrefslogtreecommitdiffstats
path: root/packet-pim.c
blob: d24767eb77f34fd139ddd68da0cddc68764f1337 (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
/* packet-pim.c
 * Routines for PIM disassembly
 * (c) Copyright Jun-ichiro itojun Hagino <itojun@itojun.org>
 *
 * $Id: packet-pim.c,v 1.17 2000/08/13 14:08:37 deniel Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * Copyright 1998 Gerald Combs
 * 
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H 
# include "config.h"
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

#include <glib.h>

#ifdef NEED_SNPRINTF_H
# include "snprintf.h"
#endif

#include "packet.h"
#include "packet-ip.h"
#include "packet-ipv6.h"

#ifndef offsetof
#define	offsetof(type, member)	((size_t)(&((type *)0)->member))
#endif

struct pim {
	guint8	pim_typever;
#define PIM_TYPE(x)	((x) & 0x0f)
#define PIM_VER(x)	(((x) & 0xf0) >> 4)
	guint8  pim_rsv;	/* Reserved */
	guint16	pim_cksum;	/* IP style check sum */
};

enum pimv2_addrtype {
	pimv2_unicast, pimv2_group, pimv2_source
};

static int proto_pim = -1;
static int hf_pim_version = -1;
static int hf_pim_type = -1;
static int hf_pim_cksum = -1;

static gint ett_pim = -1;

static const char *
dissect_pim_addr(const u_char *bp, const u_char *ep, enum pimv2_addrtype at,
	int *advance) {
    static char buf[512];
    int af;
    int len = 0;

    if (bp >= ep)
	return NULL;

    switch (bp[0]) {
    case 1:
	af = 4;
	break;
    case 2:
	af = 6;
	break;
    default:
	return NULL;
    }

    if (bp[1] != 0)
	return NULL;

    switch (at) {
    case pimv2_unicast:
	if (af == 4) {
	    len = 4;
	    if (bp + 2 + len > ep)
		return NULL;
	    (void)snprintf(buf, sizeof(buf), "%s", ip_to_str(bp + 2));
	}
	else if (af == 6) {
	    len = 16;
	    if (bp + 2 + len > ep)
		return NULL;
	    (void)snprintf(buf, sizeof(buf), "%s",
		ip6_to_str((struct e_in6_addr *)(bp + 2)));
	}
	if (advance)
	    *advance = 2 + len;
	break;

    case pimv2_group:
	if (af == 4) {
	    len = 4;
	    if (bp + 4 + len > ep)
		return NULL;
	    (void)snprintf(buf, sizeof(buf), "%s/%u", ip_to_str(bp + 4), bp[3]);
	}
	else if (af == 6) {
	    len = 16;
	    if (bp + 4 + len > ep)
		return NULL;
	    (void)snprintf(buf, sizeof(buf), "%s/%u",
		ip6_to_str((struct e_in6_addr *)(bp + 4)), bp[3]);
	}
	if (advance)
	    *advance = 4 + len;
	break;
    case pimv2_source:
	if (af == 4) {
	    len = 4;
	    if (bp + 4 + len > ep)
		return NULL;
	    (void)snprintf(buf, sizeof(buf), "%s/%u", ip_to_str(bp + 4), bp[3]);
	}
	else if (af == 6) {
	    len = 16;
	    if (bp + 4 + len > ep)
		return NULL;
	    (void)snprintf(buf, sizeof(buf), "%s/%u",
		ip6_to_str((struct e_in6_addr *)(bp + 4)), bp[3]);
	}
	if (bp[2]) {
	    (void)snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
		" (%s%s%s)",
		bp[2] & 0x04 ? "S" : "",
		bp[2] & 0x02 ? "W" : "",
		bp[2] & 0x01 ? "R" : "");
	}
	if (advance)
	    *advance = 4 + len;
	break;
    default:
	return NULL;
    }

    return buf;
}

static void 
dissect_pim(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
    struct pim pim;
    static const value_string type1vals[] = {
	{ 0, "Query" },
	{ 1, "Register" },
	{ 2, "Register-stop" },
	{ 3, "Join/Prune" },
	{ 4, "RP-Reachable" },
	{ 5, "Assert" },
	{ 6, "Graft" },
	{ 7, "Graft-Ack" },
	{ 8, "Mode" },
	{ 0, NULL },
    };
    static const value_string type2vals[] = {
	{ 0, "Hello" },
	{ 1, "Register" },
	{ 2, "Register-stop" },
	{ 3, "Join/Prune" },
	{ 4, "Bootstrap" },
	{ 5, "Assert" },
	{ 6, "Graft" },
	{ 7, "Graft-Ack" },
	{ 8, "Candidate-RP-Advertisement" },
	{ 0, NULL },
    };
    char *typestr;
    proto_tree *pim_tree = NULL;
	proto_item *ti; 
    proto_tree *pimopt_tree = NULL;
	proto_item *tiopt; 

    OLD_CHECK_DISPLAY_AS_DATA(proto_pim, pd, offset, fd, tree);

    /* avoid alignment problem */
    memcpy(&pim, &pd[offset], sizeof(pim));

    switch (PIM_VER(pim.pim_typever)) {
    case 1:
	typestr = val_to_str(PIM_TYPE(pim.pim_typever), type1vals, "Unknown");
	break;
    case 2:
	typestr = val_to_str(PIM_TYPE(pim.pim_typever), type2vals, "Unknown");
	break;
    default:
	typestr = "Unknown";
	break;
    }

    if (check_col(fd, COL_PROTOCOL)) {
        col_add_fstr(fd, COL_PROTOCOL, "PIM version %d",
	    PIM_VER(pim.pim_typever));
    }
    if (check_col(fd, COL_INFO))
	col_add_fstr(fd, COL_INFO, "%s", typestr); 

    if (tree) {
	ti = proto_tree_add_item(tree, proto_pim, NullTVB, offset, END_OF_FRAME, FALSE);
	pim_tree = proto_item_add_subtree(ti, ett_pim);

	proto_tree_add_uint(pim_tree, hf_pim_version, NullTVB, offset, 1,
	    PIM_VER(pim.pim_typever)); 
	proto_tree_add_uint_format(pim_tree, hf_pim_type, NullTVB, offset, 1,
	    PIM_TYPE(pim.pim_typever),
	    "Type: %s (%u)", typestr, PIM_TYPE(pim.pim_typever)); 

	proto_tree_add_uint(pim_tree, hf_pim_cksum, NullTVB,
	    offset + offsetof(struct pim, pim_cksum), sizeof(pim.pim_cksum),
	    ntohs(pim.pim_cksum));

	if (sizeof(struct pim) < END_OF_FRAME) {
	    tiopt = proto_tree_add_text(pim_tree, NullTVB,
		offset + sizeof(struct pim), END_OF_FRAME,
		"PIM parameters");
	    pimopt_tree = proto_item_add_subtree(tiopt, ett_pim);
	} else
	    goto done;

	if (PIM_VER(pim.pim_typever) != 2)
	    goto done;

	/* version 2 decoder */
	switch (PIM_TYPE(pim.pim_typever)) {
	case 0:	/*hello*/
	  {
	    guint16 *w;
	    w = (guint16 *)&pd[offset + sizeof(struct pim)];
	    while ((guint8 *)w < &pd[offset + END_OF_FRAME]) {
		if (pntohs(&w[0]) == 1 && pntohs(&w[1]) == 2
		 && (guint8 *)&w[3] <= &pd[offset + END_OF_FRAME]) {
		    proto_tree_add_text(pimopt_tree, NullTVB, (guint8 *)w - pd, 6,
			"Holdtime: %u%s", pntohs(&w[2]),
			pntohs(&w[2]) == 0xffff ? " (infty)" : "");
		    w += 3;
		} else
		    break;
	    }
	    break;
	  }

	case 1:	/* register */
	  {
	    const guint8 *ip;
	    proto_tree *flag_tree = NULL;
		proto_item *tiflag; 
	    int flagoff;

	    flagoff = offset + sizeof(struct pim);
	    tiflag = proto_tree_add_text(pimopt_tree, NullTVB, flagoff, 4,
		"Flags: 0x%08x", pntohl(&pd[flagoff]));
	    flag_tree = proto_item_add_subtree(tiflag, ett_pim);
	    proto_tree_add_text(flag_tree, NullTVB, flagoff, 1, "%s",
		decode_boolean_bitfield(pd[flagoff], 0x80000000, 32,
		    "Border", "Not border"));
	    proto_tree_add_text(flag_tree, NullTVB, flagoff, 1, "%s",
		decode_boolean_bitfield(pd[flagoff], 0x40000000, 32,
		    "Null-Register", "Not Null-Register"));

	    ip = &pd[flagoff + sizeof(guint32)];
	    switch((*ip & 0xf0) >> 4) {
	    case 4:	/* IPv4 */
#if 0
		    dissect_ip(pd, ip - pd, fd, tree);
#else
		    dissect_ip(pd, ip - pd, fd, pimopt_tree);
#endif
		    break;
	    case 6:	/* IPv6 */
#if 0
		    dissect_ipv6(pd, ip - pd, fd, tree);
#else
		    dissect_ipv6(pd, ip - pd, fd, pimopt_tree);
#endif
		    break;
	    default:
		    proto_tree_add_text(pimopt_tree, NullTVB,
			ip - pd, END_OF_FRAME,
			"Unknown IP version %d", (*ip & 0xf0) >> 4);
		    break;
	    }
	    break;
	  }

	case 2:	/* register-stop */
	  {
	    int advance;
	    const guint8 *ep;
	    const char *s;

	    ep = &pd[offset + END_OF_FRAME];
	    offset += sizeof(struct pim);
	    s = dissect_pim_addr(&pd[offset], ep, pimv2_group, &advance);
	    if (s == NULL)
		break;
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, advance, "Group: %s", s);
	    offset += advance;
	    s = dissect_pim_addr(&pd[offset], ep, pimv2_unicast, &advance);
	    if (s == NULL)
		break;
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, advance, "Source: %s", s);
	    break;
	  }

	case 3:	/* join/prune */
	case 6:	/* graft */
	case 7:	/* graft-ack */
	  {
	    int advance;
	    const guint8 *ep;
	    int off;
	    const char *s;
	    int ngroup, i, njoin, nprune, j;
	    proto_tree *grouptree = NULL;
		proto_item *tigroup; 
	    proto_tree *subtree = NULL;
		proto_item *tisub; 

	    ep = &pd[offset + END_OF_FRAME];
	    offset += sizeof(struct pim);
	    if (PIM_TYPE(pim.pim_typever) != 7)	{
		/* not graft-ack */
		s = dissect_pim_addr(&pd[offset], ep, pimv2_unicast, &advance);
		if (s == NULL)
		    break;
		proto_tree_add_text(pimopt_tree, NullTVB, offset, advance,
		    "Upstream-neighbor: %s", s);
		offset += advance;
	    }

	    if (&pd[offset + 2] > ep)
		break;
	    ngroup = pd[offset + 1];
	    proto_tree_add_text(pimopt_tree, NullTVB, offset + 1, 1,
		"Groups: %u", pd[offset + 1]);
	    offset += 2;

	    if (&pd[offset + 2] > ep)
		break;
	    if (PIM_TYPE(pim.pim_typever) != 7)	{
		/* not graft-ack */
		proto_tree_add_text(pimopt_tree, NullTVB, offset, 2,
		    "Holdtime: %u%s", pntohs(&pd[offset]),
		    pntohs(&pd[offset]) == 0xffff ? " (infty)" : "");
	    }
	    offset += 2;

	    for (i = 0; i < ngroup; i++) {
		if (&pd[offset] >= ep)
		    goto breakbreak3;

		s = dissect_pim_addr(&pd[offset], ep, pimv2_group, &advance);
		if (s == NULL)
		    goto breakbreak3;
		tigroup = proto_tree_add_text(pimopt_tree, NullTVB, offset, advance,
		    "Group %d: %s", i, s);
		grouptree = proto_item_add_subtree(tigroup, ett_pim);
		offset += advance;

		if (&pd[offset + 4] > ep)
		    goto breakbreak3;
		njoin = pntohs(&pd[offset]);
		nprune = pntohs(&pd[offset + 2]);

		tisub = proto_tree_add_text(grouptree, NullTVB, offset, 2,
		    "Join: %d", njoin);
		subtree = proto_item_add_subtree(tisub, ett_pim);
		off = offset + 4;
		for (j = 0; j < nprune; j++) {
		    s = dissect_pim_addr(&pd[off], ep, pimv2_source,
			&advance);
		    if (s == NULL)
			goto breakbreak3;
		    proto_tree_add_text(subtree, NullTVB, off, advance,
			"IP address: %s", s);
		    off += advance;
		}

		tisub = proto_tree_add_text(grouptree, NullTVB, offset + 2, 2,
		    "Prune: %d", nprune);
		subtree = proto_item_add_subtree(tisub, ett_pim);
		for (j = 0; j < nprune; j++) {
		    s = dissect_pim_addr(&pd[off], ep, pimv2_source,
			&advance);
		    if (s == NULL)
			goto breakbreak3;
		    proto_tree_add_text(subtree, NullTVB, off, advance,
			"IP address: %s", s);
		    off += advance;
		}
	    }
    breakbreak3:
	    break;
	  }

	case 4:	/* bootstrap */
	  {
	    const char *s;
	    int advance;
	    int i, j;
	    int frpcnt;
	    proto_tree *grouptree = NULL;
		proto_item *tigroup; 

	    offset += sizeof(struct pim);

	    if (END_OF_FRAME < 2)
		break;
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, 2,
		"Fragment tag: 0x%04x", pntohs(&pd[offset]));
	    offset += 2;

	    if (END_OF_FRAME < 2)
		break;
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, 1,
		"Hash mask len: %u", pd[offset]);
	    proto_tree_add_text(pimopt_tree, NullTVB, offset + 1, 1,
		"BSR priority: %u", pd[offset + 1]);
	    offset += 2;

	    s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
		pimv2_unicast, &advance);
	    if (s == NULL)
		break;
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, advance, "BSR: %s", s);
	    offset += advance;

	    for (i = 0; END_OF_FRAME > 0; i++) {
		s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
		    pimv2_group, &advance);
		if (s == NULL)
		    goto breakbreak4;
		tigroup = proto_tree_add_text(pimopt_tree, NullTVB, offset, advance,
		    "Group %d: %s", i, s);
		grouptree = proto_item_add_subtree(tigroup, ett_pim);
		offset += advance;

		if (END_OF_FRAME < 2)
		    goto breakbreak4;
		proto_tree_add_text(grouptree, NullTVB, offset, 1,
		    "RP count: %u", pd[offset]);
		proto_tree_add_text(grouptree, NullTVB, offset + 1, 1,
		    "FRP count: %u", pd[offset + 1]);
		frpcnt = pd[offset + 1];
		offset += 4;

		for (j = 0; j < frpcnt && END_OF_FRAME > 0; j++) {
		    s = dissect_pim_addr(&pd[offset],
			&pd[offset + END_OF_FRAME], pimv2_unicast, &advance);
		    if (s == NULL)
			goto breakbreak4;
		    proto_tree_add_text(grouptree, NullTVB, offset, advance,
			"RP %d: %s", j, s);
		    offset += advance;

		    if (END_OF_FRAME < 4)
			goto breakbreak4;
		    proto_tree_add_text(grouptree, NullTVB, offset, 2,
			"Holdtime: %u%s", pntohs(&pd[offset]),
			pntohs(&pd[offset]) == 0xffff ? " (infty)" : "");
		    proto_tree_add_text(grouptree, NullTVB, offset + 3, 1,
			"Priority: %u", pd[offset + 3]);

		    offset += 4;
		}
	    }

    breakbreak4:
	    break;
	  }

	case 5:	/* assert */
	  {
	    const char *s;
	    int advance;

	    offset += sizeof(struct pim);

	    s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
		pimv2_group, &advance);
	    if (s == NULL)
		break;
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, advance, "Group: %s", s);
	    offset += advance;

	    s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
		pimv2_unicast, &advance);
	    if (s == NULL)
		break;
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, advance, "Source: %s", s);
	    offset += advance;

	    if (END_OF_FRAME < 4)
		break;
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, 1, "%s",
		decode_boolean_bitfield(pd[offset], 0x80, 8,
		    "RP Tree", "Not RP Tree"));
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, 4, "Preference: %u",
		pntohl(&pd[offset]) & 0x7fffffff);
	    offset += 4;

	    if (END_OF_FRAME < 4)
		break;
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, 4, "Metric: %u",
		pntohl(&pd[offset]));

	    break;
	  }

	case 8:	/* Candidate-RP-Advertisement */
	  {
	    const char *s;
	    int advance;
	    int pfxcnt;
	    int i;

	    offset += sizeof(struct pim);
	    if (END_OF_FRAME < 4)
		break;
	    pfxcnt = pd[offset];
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, 1,
		"Prefix-count: %u", pd[offset]);
	    proto_tree_add_text(pimopt_tree, NullTVB, offset + 1, 1,
		"Priority: %u", pd[offset + 1]);
	    proto_tree_add_text(pimopt_tree, NullTVB, offset + 2, 2,
		"Holdtime: %u%s", pntohs(&pd[offset + 2]),
		pntohs(&pd[offset + 2]) == 0xffff ? " (infty)" : "");
	    offset += 4;

	    if (END_OF_FRAME <= 0)
		break;
	    s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
		pimv2_unicast, &advance);
	    if (s == NULL)
		break;
	    proto_tree_add_text(pimopt_tree, NullTVB, offset, advance, "RP: %s", s);
	    offset += advance;

	    for (i = 0; i < pfxcnt && END_OF_FRAME > 0; i++) {
		s = dissect_pim_addr(&pd[offset], &pd[offset + END_OF_FRAME],
		    pimv2_group, &advance);
		if (s == NULL)
		    goto breakbreak8;
		proto_tree_add_text(pimopt_tree, NullTVB, offset, advance,
		    "Group %d: %s", i, s);
		offset += advance;
	    }
    breakbreak8:
	    break;
	  }

	default:
	    break;
	}
    }
done:;
}

void
proto_register_pim(void)
{
    static hf_register_info hf[] = {
      { &hf_pim_version,
	{ "Version",		"pim.version",
				FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
      { &hf_pim_type,
	{ "Type",			"pim.type",
				FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
      { &hf_pim_cksum,
	{ "Checksum",		"pim.cksum",
				FT_UINT16, BASE_HEX, NULL, 0x0, "" }},
    };
    static gint *ett[] = {
        &ett_pim,
    };

    proto_pim = proto_register_protocol("Protocol Independent Multicast",
	"pim");
    proto_register_field_array(proto_pim, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
}

void
proto_reg_handoff_pim(void)
{
    old_dissector_add("ip.proto", IP_PROTO_PIM, dissect_pim);
}