Home | History | Annotate | Line # | Download | only in dist
print-igmp.c revision 1.1
      1  1.1  christos /*
      2  1.1  christos  * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
      3  1.1  christos  *  The Regents of the University of California.  All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Redistribution and use in source and binary forms, with or without
      6  1.1  christos  * modification, are permitted provided that: (1) source code distributions
      7  1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
      8  1.1  christos  * distributions including binary code include the above copyright notice and
      9  1.1  christos  * this paragraph in its entirety in the documentation or other materials
     10  1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     11  1.1  christos  * features or use of this software display the following acknowledgement:
     12  1.1  christos  * ``This product includes software developed by the University of California,
     13  1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     14  1.1  christos  * the University nor the names of its contributors may be used to endorse
     15  1.1  christos  * or promote products derived from this software without specific prior
     16  1.1  christos  * written permission.
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     18  1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     19  1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     20  1.1  christos  */
     21  1.1  christos 
     22  1.1  christos #ifndef lint
     23  1.1  christos static const char rcsid[] _U_ =
     24  1.1  christos     "@(#) Header: /tcpdump/master/tcpdump/print-igmp.c,v 1.15 2004-03-24 00:59:16 guy Exp (LBL)";
     25  1.1  christos #endif
     26  1.1  christos 
     27  1.1  christos #ifdef HAVE_CONFIG_H
     28  1.1  christos #include "config.h"
     29  1.1  christos #endif
     30  1.1  christos 
     31  1.1  christos #include <tcpdump-stdinc.h>
     32  1.1  christos 
     33  1.1  christos #include <stdio.h>
     34  1.1  christos #include <string.h>
     35  1.1  christos 
     36  1.1  christos #include "interface.h"
     37  1.1  christos #include "addrtoname.h"
     38  1.1  christos #include "extract.h"            /* must come after interface.h */
     39  1.1  christos 
     40  1.1  christos #ifndef IN_CLASSD
     41  1.1  christos #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
     42  1.1  christos #endif
     43  1.1  christos 
     44  1.1  christos /* (following from ipmulti/mrouted/prune.h) */
     45  1.1  christos 
     46  1.1  christos /*
     47  1.1  christos  * The packet format for a traceroute request.
     48  1.1  christos  */
     49  1.1  christos struct tr_query {
     50  1.1  christos     u_int32_t  tr_src;          /* traceroute source */
     51  1.1  christos     u_int32_t  tr_dst;          /* traceroute destination */
     52  1.1  christos     u_int32_t  tr_raddr;        /* traceroute response address */
     53  1.1  christos     u_int32_t  tr_rttlqid;      /* response ttl and qid */
     54  1.1  christos };
     55  1.1  christos 
     56  1.1  christos #define TR_GETTTL(x)        (int)(((x) >> 24) & 0xff)
     57  1.1  christos #define TR_GETQID(x)        ((x) & 0x00ffffff)
     58  1.1  christos 
     59  1.1  christos /*
     60  1.1  christos  * Traceroute response format.  A traceroute response has a tr_query at the
     61  1.1  christos  * beginning, followed by one tr_resp for each hop taken.
     62  1.1  christos  */
     63  1.1  christos struct tr_resp {
     64  1.1  christos     u_int32_t tr_qarr;          /* query arrival time */
     65  1.1  christos     u_int32_t tr_inaddr;        /* incoming interface address */
     66  1.1  christos     u_int32_t tr_outaddr;       /* outgoing interface address */
     67  1.1  christos     u_int32_t tr_rmtaddr;       /* parent address in source tree */
     68  1.1  christos     u_int32_t tr_vifin;         /* input packet count on interface */
     69  1.1  christos     u_int32_t tr_vifout;        /* output packet count on interface */
     70  1.1  christos     u_int32_t tr_pktcnt;        /* total incoming packets for src-grp */
     71  1.1  christos     u_int8_t  tr_rproto;      /* routing proto deployed on router */
     72  1.1  christos     u_int8_t  tr_fttl;        /* ttl required to forward on outvif */
     73  1.1  christos     u_int8_t  tr_smask;       /* subnet mask for src addr */
     74  1.1  christos     u_int8_t  tr_rflags;      /* forwarding error codes */
     75  1.1  christos };
     76  1.1  christos 
     77  1.1  christos /* defs within mtrace */
     78  1.1  christos #define TR_QUERY 1
     79  1.1  christos #define TR_RESP 2
     80  1.1  christos 
     81  1.1  christos /* fields for tr_rflags (forwarding error codes) */
     82  1.1  christos #define TR_NO_ERR   0
     83  1.1  christos #define TR_WRONG_IF 1
     84  1.1  christos #define TR_PRUNED   2
     85  1.1  christos #define TR_OPRUNED  3
     86  1.1  christos #define TR_SCOPED   4
     87  1.1  christos #define TR_NO_RTE   5
     88  1.1  christos #define TR_NO_FWD   7
     89  1.1  christos #define TR_NO_SPACE 0x81
     90  1.1  christos #define TR_OLD_ROUTER   0x82
     91  1.1  christos 
     92  1.1  christos /* fields for tr_rproto (routing protocol) */
     93  1.1  christos #define TR_PROTO_DVMRP  1
     94  1.1  christos #define TR_PROTO_MOSPF  2
     95  1.1  christos #define TR_PROTO_PIM    3
     96  1.1  christos #define TR_PROTO_CBT    4
     97  1.1  christos 
     98  1.1  christos /* igmpv3 report types */
     99  1.1  christos static struct tok igmpv3report2str[] = {
    100  1.1  christos 	{ 1,	"is_in" },
    101  1.1  christos 	{ 2,	"is_ex" },
    102  1.1  christos 	{ 3,	"to_in" },
    103  1.1  christos 	{ 4,	"to_ex" },
    104  1.1  christos 	{ 5,	"allow" },
    105  1.1  christos 	{ 6,	"block" },
    106  1.1  christos 	{ 0,	NULL }
    107  1.1  christos };
    108  1.1  christos 
    109  1.1  christos static void
    110  1.1  christos print_mtrace(register const u_char *bp, register u_int len)
    111  1.1  christos {
    112  1.1  christos     register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
    113  1.1  christos 
    114  1.1  christos     TCHECK(*tr);
    115  1.1  christos     if (len < 8 + sizeof (struct tr_query)) {
    116  1.1  christos 	(void)printf(" [invalid len %d]", len);
    117  1.1  christos 	return;
    118  1.1  christos     }
    119  1.1  christos     printf("mtrace %u: %s to %s reply-to %s",
    120  1.1  christos         TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
    121  1.1  christos         ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
    122  1.1  christos         ipaddr_string(&tr->tr_raddr));
    123  1.1  christos     if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
    124  1.1  christos         printf(" with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid)));
    125  1.1  christos     return;
    126  1.1  christos trunc:
    127  1.1  christos     (void)printf("[|igmp]");
    128  1.1  christos     return;
    129  1.1  christos }
    130  1.1  christos 
    131  1.1  christos static void
    132  1.1  christos print_mresp(register const u_char *bp, register u_int len)
    133  1.1  christos {
    134  1.1  christos     register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
    135  1.1  christos 
    136  1.1  christos     TCHECK(*tr);
    137  1.1  christos     if (len < 8 + sizeof (struct tr_query)) {
    138  1.1  christos 	(void)printf(" [invalid len %d]", len);
    139  1.1  christos 	return;
    140  1.1  christos     }
    141  1.1  christos     printf("mresp %lu: %s to %s reply-to %s",
    142  1.1  christos         (u_long)TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
    143  1.1  christos         ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
    144  1.1  christos         ipaddr_string(&tr->tr_raddr));
    145  1.1  christos     if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
    146  1.1  christos         printf(" with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid)));
    147  1.1  christos     return;
    148  1.1  christos trunc:
    149  1.1  christos     (void)printf("[|igmp]");
    150  1.1  christos     return;
    151  1.1  christos }
    152  1.1  christos 
    153  1.1  christos static void
    154  1.1  christos print_igmpv3_report(register const u_char *bp, register u_int len)
    155  1.1  christos {
    156  1.1  christos     u_int group, nsrcs, ngroups;
    157  1.1  christos     register u_int i, j;
    158  1.1  christos 
    159  1.1  christos     /* Minimum len is 16, and should be a multiple of 4 */
    160  1.1  christos     if (len < 16 || len & 0x03) {
    161  1.1  christos 	(void)printf(" [invalid len %d]", len);
    162  1.1  christos 	return;
    163  1.1  christos     }
    164  1.1  christos     TCHECK2(bp[6], 2);
    165  1.1  christos     ngroups = EXTRACT_16BITS(&bp[6]);
    166  1.1  christos     (void)printf(", %d group record(s)", ngroups);
    167  1.1  christos     if (vflag > 0) {
    168  1.1  christos 	/* Print the group records */
    169  1.1  christos 	group = 8;
    170  1.1  christos         for (i=0; i<ngroups; i++) {
    171  1.1  christos 	    if (len < group+8) {
    172  1.1  christos 		(void)printf(" [invalid number of groups]");
    173  1.1  christos 		return;
    174  1.1  christos 	    }
    175  1.1  christos 	    TCHECK2(bp[group+4], 4);
    176  1.1  christos             (void)printf(" [gaddr %s", ipaddr_string(&bp[group+4]));
    177  1.1  christos 	    (void)printf(" %s", tok2str(igmpv3report2str, " [v3-report-#%d]",
    178  1.1  christos 								bp[group]));
    179  1.1  christos             nsrcs = EXTRACT_16BITS(&bp[group+2]);
    180  1.1  christos 	    /* Check the number of sources and print them */
    181  1.1  christos 	    if (len < group+8+(nsrcs<<2)) {
    182  1.1  christos 		(void)printf(" [invalid number of sources %d]", nsrcs);
    183  1.1  christos 		return;
    184  1.1  christos 	    }
    185  1.1  christos             if (vflag == 1)
    186  1.1  christos                 (void)printf(", %d source(s)", nsrcs);
    187  1.1  christos             else {
    188  1.1  christos 		/* Print the sources */
    189  1.1  christos                 (void)printf(" {");
    190  1.1  christos                 for (j=0; j<nsrcs; j++) {
    191  1.1  christos 		    TCHECK2(bp[group+8+(j<<2)], 4);
    192  1.1  christos 		    (void)printf(" %s", ipaddr_string(&bp[group+8+(j<<2)]));
    193  1.1  christos 		}
    194  1.1  christos                 (void)printf(" }");
    195  1.1  christos             }
    196  1.1  christos 	    /* Next group record */
    197  1.1  christos             group += 8 + (nsrcs << 2);
    198  1.1  christos 	    (void)printf("]");
    199  1.1  christos         }
    200  1.1  christos     }
    201  1.1  christos     return;
    202  1.1  christos trunc:
    203  1.1  christos     (void)printf("[|igmp]");
    204  1.1  christos     return;
    205  1.1  christos }
    206  1.1  christos 
    207  1.1  christos static void
    208  1.1  christos print_igmpv3_query(register const u_char *bp, register u_int len)
    209  1.1  christos {
    210  1.1  christos     u_int mrc;
    211  1.1  christos     int mrt;
    212  1.1  christos     u_int nsrcs;
    213  1.1  christos     register u_int i;
    214  1.1  christos 
    215  1.1  christos     (void)printf(" v3");
    216  1.1  christos     /* Minimum len is 12, and should be a multiple of 4 */
    217  1.1  christos     if (len < 12 || len & 0x03) {
    218  1.1  christos 	(void)printf(" [invalid len %d]", len);
    219  1.1  christos 	return;
    220  1.1  christos     }
    221  1.1  christos     TCHECK(bp[1]);
    222  1.1  christos     mrc = bp[1];
    223  1.1  christos     if (mrc < 128) {
    224  1.1  christos 	mrt = mrc;
    225  1.1  christos     } else {
    226  1.1  christos         mrt = ((mrc & 0x0f) | 0x10) << (((mrc & 0x70) >> 4) + 3);
    227  1.1  christos     }
    228  1.1  christos     if (mrc != 100) {
    229  1.1  christos 	(void)printf(" [max resp time ");
    230  1.1  christos 	relts_print(mrt);
    231  1.1  christos 	(void)printf("]");
    232  1.1  christos     }
    233  1.1  christos     TCHECK2(bp[4], 4);
    234  1.1  christos     if (EXTRACT_32BITS(&bp[4]) == 0)
    235  1.1  christos 	return;
    236  1.1  christos     (void)printf(" [gaddr %s", ipaddr_string(&bp[4]));
    237  1.1  christos     TCHECK2(bp[10], 2);
    238  1.1  christos     nsrcs = EXTRACT_16BITS(&bp[10]);
    239  1.1  christos     if (nsrcs > 0) {
    240  1.1  christos 	if (len < 12 + (nsrcs << 2))
    241  1.1  christos 	    (void)printf(" [invalid number of sources]");
    242  1.1  christos 	else if (vflag > 1) {
    243  1.1  christos 	    (void)printf(" {");
    244  1.1  christos 	    for (i=0; i<nsrcs; i++) {
    245  1.1  christos 		TCHECK2(bp[12+(i<<2)], 4);
    246  1.1  christos 		(void)printf(" %s", ipaddr_string(&bp[12+(i<<2)]));
    247  1.1  christos 	    }
    248  1.1  christos 	    (void)printf(" }");
    249  1.1  christos 	} else
    250  1.1  christos 	    (void)printf(", %d source(s)", nsrcs);
    251  1.1  christos     }
    252  1.1  christos     (void)printf("]");
    253  1.1  christos     return;
    254  1.1  christos trunc:
    255  1.1  christos     (void)printf("[|igmp]");
    256  1.1  christos     return;
    257  1.1  christos }
    258  1.1  christos 
    259  1.1  christos void
    260  1.1  christos igmp_print(register const u_char *bp, register u_int len)
    261  1.1  christos {
    262  1.1  christos     if (qflag) {
    263  1.1  christos         (void)printf("igmp");
    264  1.1  christos         return;
    265  1.1  christos     }
    266  1.1  christos 
    267  1.1  christos     TCHECK(bp[0]);
    268  1.1  christos     switch (bp[0]) {
    269  1.1  christos     case 0x11:
    270  1.1  christos         (void)printf("igmp query");
    271  1.1  christos 	if (len >= 12)
    272  1.1  christos 	    print_igmpv3_query(bp, len);
    273  1.1  christos 	else {
    274  1.1  christos             TCHECK(bp[1]);
    275  1.1  christos 	    if (bp[1]) {
    276  1.1  christos 		(void)printf(" v2");
    277  1.1  christos 		if (bp[1] != 100)
    278  1.1  christos 		    (void)printf(" [max resp time %d]", bp[1]);
    279  1.1  christos 	    } else
    280  1.1  christos 		(void)printf(" v1");
    281  1.1  christos             TCHECK2(bp[4], 4);
    282  1.1  christos 	    if (EXTRACT_32BITS(&bp[4]))
    283  1.1  christos                 (void)printf(" [gaddr %s]", ipaddr_string(&bp[4]));
    284  1.1  christos             if (len != 8)
    285  1.1  christos                 (void)printf(" [len %d]", len);
    286  1.1  christos 	}
    287  1.1  christos         break;
    288  1.1  christos     case 0x12:
    289  1.1  christos         TCHECK2(bp[4], 4);
    290  1.1  christos         (void)printf("igmp v1 report %s", ipaddr_string(&bp[4]));
    291  1.1  christos         if (len != 8)
    292  1.1  christos             (void)printf(" [len %d]", len);
    293  1.1  christos         break;
    294  1.1  christos     case 0x16:
    295  1.1  christos         TCHECK2(bp[4], 4);
    296  1.1  christos         (void)printf("igmp v2 report %s", ipaddr_string(&bp[4]));
    297  1.1  christos         break;
    298  1.1  christos     case 0x22:
    299  1.1  christos         (void)printf("igmp v3 report");
    300  1.1  christos 	print_igmpv3_report(bp, len);
    301  1.1  christos         break;
    302  1.1  christos     case 0x17:
    303  1.1  christos         TCHECK2(bp[4], 4);
    304  1.1  christos         (void)printf("igmp leave %s", ipaddr_string(&bp[4]));
    305  1.1  christos         break;
    306  1.1  christos     case 0x13:
    307  1.1  christos         (void)printf("igmp dvmrp");
    308  1.1  christos         if (len < 8)
    309  1.1  christos             (void)printf(" [len %d]", len);
    310  1.1  christos         else
    311  1.1  christos             dvmrp_print(bp, len);
    312  1.1  christos         break;
    313  1.1  christos     case 0x14:
    314  1.1  christos         (void)printf("igmp pimv1");
    315  1.1  christos         pimv1_print(bp, len);
    316  1.1  christos         break;
    317  1.1  christos     case 0x1e:
    318  1.1  christos         print_mresp(bp, len);
    319  1.1  christos         break;
    320  1.1  christos     case 0x1f:
    321  1.1  christos         print_mtrace(bp, len);
    322  1.1  christos         break;
    323  1.1  christos     default:
    324  1.1  christos         (void)printf("igmp-%d", bp[0]);
    325  1.1  christos         break;
    326  1.1  christos     }
    327  1.1  christos 
    328  1.1  christos     if (vflag && TTEST2(bp[0], len)) {
    329  1.1  christos         /* Check the IGMP checksum */
    330  1.1  christos         if (in_cksum((const u_short*)bp, len, 0))
    331  1.1  christos             printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp[2]));
    332  1.1  christos     }
    333  1.1  christos     return;
    334  1.1  christos trunc:
    335  1.1  christos     fputs("[|igmp]", stdout);
    336  1.1  christos }
    337