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