Home | History | Annotate | Line # | Download | only in dist
print-igmp.c revision 1.5.4.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.2  christos #include <sys/cdefs.h>
     23      1.1  christos #ifndef lint
     24  1.5.4.1    bouyer __RCSID("$NetBSD: print-igmp.c,v 1.5.4.1 2017/04/21 16:52:35 bouyer Exp $");
     25      1.1  christos #endif
     26      1.1  christos 
     27  1.5.4.1    bouyer /* \summary: Internet Group Management Protocol (IGMP) printer */
     28  1.5.4.1    bouyer 
     29      1.1  christos #ifdef HAVE_CONFIG_H
     30      1.1  christos #include "config.h"
     31      1.1  christos #endif
     32      1.1  christos 
     33  1.5.4.1    bouyer #include <netdissect-stdinc.h>
     34      1.1  christos 
     35  1.5.4.1    bouyer #include "netdissect.h"
     36      1.1  christos #include "addrtoname.h"
     37  1.5.4.1    bouyer #include "extract.h"
     38      1.1  christos 
     39      1.1  christos #ifndef IN_CLASSD
     40      1.1  christos #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
     41      1.1  christos #endif
     42      1.1  christos 
     43      1.5  christos static const char tstr[] = "[|igmp]";
     44      1.5  christos 
     45      1.1  christos /* (following from ipmulti/mrouted/prune.h) */
     46      1.1  christos 
     47      1.1  christos /*
     48      1.1  christos  * The packet format for a traceroute request.
     49      1.1  christos  */
     50      1.1  christos struct tr_query {
     51      1.5  christos     uint32_t  tr_src;          /* traceroute source */
     52      1.5  christos     uint32_t  tr_dst;          /* traceroute destination */
     53      1.5  christos     uint32_t  tr_raddr;        /* traceroute response address */
     54      1.5  christos     uint32_t  tr_rttlqid;      /* response ttl and qid */
     55      1.1  christos };
     56      1.1  christos 
     57      1.1  christos #define TR_GETTTL(x)        (int)(((x) >> 24) & 0xff)
     58      1.1  christos #define TR_GETQID(x)        ((x) & 0x00ffffff)
     59      1.1  christos 
     60      1.1  christos /*
     61      1.1  christos  * Traceroute response format.  A traceroute response has a tr_query at the
     62      1.1  christos  * beginning, followed by one tr_resp for each hop taken.
     63      1.1  christos  */
     64      1.1  christos struct tr_resp {
     65      1.5  christos     uint32_t tr_qarr;          /* query arrival time */
     66      1.5  christos     uint32_t tr_inaddr;        /* incoming interface address */
     67      1.5  christos     uint32_t tr_outaddr;       /* outgoing interface address */
     68      1.5  christos     uint32_t tr_rmtaddr;       /* parent address in source tree */
     69      1.5  christos     uint32_t tr_vifin;         /* input packet count on interface */
     70      1.5  christos     uint32_t tr_vifout;        /* output packet count on interface */
     71      1.5  christos     uint32_t tr_pktcnt;        /* total incoming packets for src-grp */
     72      1.5  christos     uint8_t  tr_rproto;      /* routing proto deployed on router */
     73      1.5  christos     uint8_t  tr_fttl;        /* ttl required to forward on outvif */
     74      1.5  christos     uint8_t  tr_smask;       /* subnet mask for src addr */
     75      1.5  christos     uint8_t  tr_rflags;      /* forwarding error codes */
     76      1.1  christos };
     77      1.1  christos 
     78      1.1  christos /* defs within mtrace */
     79      1.1  christos #define TR_QUERY 1
     80      1.1  christos #define TR_RESP 2
     81      1.1  christos 
     82      1.1  christos /* fields for tr_rflags (forwarding error codes) */
     83      1.1  christos #define TR_NO_ERR   0
     84      1.1  christos #define TR_WRONG_IF 1
     85      1.1  christos #define TR_PRUNED   2
     86      1.1  christos #define TR_OPRUNED  3
     87      1.1  christos #define TR_SCOPED   4
     88      1.1  christos #define TR_NO_RTE   5
     89      1.1  christos #define TR_NO_FWD   7
     90      1.1  christos #define TR_NO_SPACE 0x81
     91      1.1  christos #define TR_OLD_ROUTER   0x82
     92      1.1  christos 
     93      1.1  christos /* fields for tr_rproto (routing protocol) */
     94      1.1  christos #define TR_PROTO_DVMRP  1
     95      1.1  christos #define TR_PROTO_MOSPF  2
     96      1.1  christos #define TR_PROTO_PIM    3
     97      1.1  christos #define TR_PROTO_CBT    4
     98      1.1  christos 
     99      1.1  christos /* igmpv3 report types */
    100      1.4  christos static const struct tok igmpv3report2str[] = {
    101      1.1  christos 	{ 1,	"is_in" },
    102      1.1  christos 	{ 2,	"is_ex" },
    103      1.1  christos 	{ 3,	"to_in" },
    104      1.1  christos 	{ 4,	"to_ex" },
    105      1.1  christos 	{ 5,	"allow" },
    106      1.1  christos 	{ 6,	"block" },
    107      1.1  christos 	{ 0,	NULL }
    108      1.1  christos };
    109      1.1  christos 
    110      1.1  christos static void
    111      1.5  christos print_mtrace(netdissect_options *ndo,
    112      1.5  christos              register const u_char *bp, register u_int len)
    113      1.1  christos {
    114      1.1  christos     register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
    115      1.1  christos 
    116      1.5  christos     ND_TCHECK(*tr);
    117      1.1  christos     if (len < 8 + sizeof (struct tr_query)) {
    118      1.5  christos 	ND_PRINT((ndo, " [invalid len %d]", len));
    119      1.1  christos 	return;
    120      1.1  christos     }
    121      1.5  christos     ND_PRINT((ndo, "mtrace %u: %s to %s reply-to %s",
    122      1.1  christos         TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
    123      1.5  christos         ipaddr_string(ndo, &tr->tr_src), ipaddr_string(ndo, &tr->tr_dst),
    124      1.5  christos         ipaddr_string(ndo, &tr->tr_raddr)));
    125      1.1  christos     if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
    126      1.5  christos         ND_PRINT((ndo, " with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid))));
    127      1.1  christos     return;
    128      1.1  christos trunc:
    129      1.5  christos     ND_PRINT((ndo, "%s", tstr));
    130      1.1  christos }
    131      1.1  christos 
    132      1.1  christos static void
    133      1.5  christos print_mresp(netdissect_options *ndo,
    134      1.5  christos             register const u_char *bp, register u_int len)
    135      1.1  christos {
    136      1.1  christos     register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
    137      1.1  christos 
    138      1.5  christos     ND_TCHECK(*tr);
    139      1.1  christos     if (len < 8 + sizeof (struct tr_query)) {
    140      1.5  christos 	ND_PRINT((ndo, " [invalid len %d]", len));
    141      1.1  christos 	return;
    142      1.1  christos     }
    143      1.5  christos     ND_PRINT((ndo, "mresp %lu: %s to %s reply-to %s",
    144      1.1  christos         (u_long)TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
    145      1.5  christos         ipaddr_string(ndo, &tr->tr_src), ipaddr_string(ndo, &tr->tr_dst),
    146      1.5  christos         ipaddr_string(ndo, &tr->tr_raddr)));
    147      1.1  christos     if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
    148      1.5  christos         ND_PRINT((ndo, " with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid))));
    149      1.1  christos     return;
    150      1.1  christos trunc:
    151      1.5  christos     ND_PRINT((ndo, "%s", tstr));
    152      1.1  christos }
    153      1.1  christos 
    154      1.1  christos static void
    155      1.5  christos print_igmpv3_report(netdissect_options *ndo,
    156      1.5  christos                     register const u_char *bp, register u_int len)
    157      1.1  christos {
    158      1.1  christos     u_int group, nsrcs, ngroups;
    159      1.1  christos     register u_int i, j;
    160      1.1  christos 
    161      1.1  christos     /* Minimum len is 16, and should be a multiple of 4 */
    162      1.1  christos     if (len < 16 || len & 0x03) {
    163      1.5  christos 	ND_PRINT((ndo, " [invalid len %d]", len));
    164      1.1  christos 	return;
    165      1.1  christos     }
    166      1.5  christos     ND_TCHECK2(bp[6], 2);
    167      1.1  christos     ngroups = EXTRACT_16BITS(&bp[6]);
    168      1.5  christos     ND_PRINT((ndo, ", %d group record(s)", ngroups));
    169      1.5  christos     if (ndo->ndo_vflag > 0) {
    170      1.1  christos 	/* Print the group records */
    171      1.1  christos 	group = 8;
    172      1.1  christos         for (i=0; i<ngroups; i++) {
    173      1.1  christos 	    if (len < group+8) {
    174      1.5  christos 		ND_PRINT((ndo, " [invalid number of groups]"));
    175      1.1  christos 		return;
    176      1.1  christos 	    }
    177      1.5  christos 	    ND_TCHECK2(bp[group+4], 4);
    178      1.5  christos             ND_PRINT((ndo, " [gaddr %s", ipaddr_string(ndo, &bp[group+4])));
    179      1.5  christos 	    ND_PRINT((ndo, " %s", tok2str(igmpv3report2str, " [v3-report-#%d]",
    180      1.5  christos 								bp[group])));
    181      1.1  christos             nsrcs = EXTRACT_16BITS(&bp[group+2]);
    182      1.1  christos 	    /* Check the number of sources and print them */
    183      1.1  christos 	    if (len < group+8+(nsrcs<<2)) {
    184      1.5  christos 		ND_PRINT((ndo, " [invalid number of sources %d]", nsrcs));
    185      1.1  christos 		return;
    186      1.1  christos 	    }
    187      1.5  christos             if (ndo->ndo_vflag == 1)
    188      1.5  christos                 ND_PRINT((ndo, ", %d source(s)", nsrcs));
    189      1.1  christos             else {
    190      1.1  christos 		/* Print the sources */
    191      1.5  christos                 ND_PRINT((ndo, " {"));
    192      1.1  christos                 for (j=0; j<nsrcs; j++) {
    193      1.5  christos 		    ND_TCHECK2(bp[group+8+(j<<2)], 4);
    194      1.5  christos 		    ND_PRINT((ndo, " %s", ipaddr_string(ndo, &bp[group+8+(j<<2)])));
    195      1.1  christos 		}
    196      1.5  christos                 ND_PRINT((ndo, " }"));
    197      1.1  christos             }
    198      1.1  christos 	    /* Next group record */
    199      1.1  christos             group += 8 + (nsrcs << 2);
    200      1.5  christos 	    ND_PRINT((ndo, "]"));
    201      1.1  christos         }
    202      1.1  christos     }
    203      1.1  christos     return;
    204      1.1  christos trunc:
    205      1.5  christos     ND_PRINT((ndo, "%s", tstr));
    206      1.1  christos }
    207      1.1  christos 
    208      1.1  christos static void
    209      1.5  christos print_igmpv3_query(netdissect_options *ndo,
    210      1.5  christos                    register const u_char *bp, register u_int len)
    211      1.1  christos {
    212      1.1  christos     u_int mrc;
    213  1.5.4.1    bouyer     u_int mrt;
    214      1.1  christos     u_int nsrcs;
    215      1.1  christos     register u_int i;
    216      1.1  christos 
    217      1.5  christos     ND_PRINT((ndo, " v3"));
    218      1.1  christos     /* Minimum len is 12, and should be a multiple of 4 */
    219      1.1  christos     if (len < 12 || len & 0x03) {
    220      1.5  christos 	ND_PRINT((ndo, " [invalid len %d]", len));
    221      1.1  christos 	return;
    222      1.1  christos     }
    223      1.5  christos     ND_TCHECK(bp[1]);
    224      1.1  christos     mrc = bp[1];
    225      1.1  christos     if (mrc < 128) {
    226      1.1  christos 	mrt = mrc;
    227      1.1  christos     } else {
    228      1.1  christos         mrt = ((mrc & 0x0f) | 0x10) << (((mrc & 0x70) >> 4) + 3);
    229      1.1  christos     }
    230      1.1  christos     if (mrc != 100) {
    231      1.5  christos 	ND_PRINT((ndo, " [max resp time "));
    232      1.3  christos         if (mrt < 600) {
    233      1.5  christos             ND_PRINT((ndo, "%.1fs", mrt * 0.1));
    234      1.3  christos         } else {
    235  1.5.4.1    bouyer             unsigned_relts_print(ndo, mrt / 10);
    236      1.3  christos         }
    237      1.5  christos 	ND_PRINT((ndo, "]"));
    238      1.1  christos     }
    239      1.5  christos     ND_TCHECK2(bp[4], 4);
    240      1.1  christos     if (EXTRACT_32BITS(&bp[4]) == 0)
    241      1.1  christos 	return;
    242      1.5  christos     ND_PRINT((ndo, " [gaddr %s", ipaddr_string(ndo, &bp[4])));
    243      1.5  christos     ND_TCHECK2(bp[10], 2);
    244      1.1  christos     nsrcs = EXTRACT_16BITS(&bp[10]);
    245      1.1  christos     if (nsrcs > 0) {
    246      1.1  christos 	if (len < 12 + (nsrcs << 2))
    247      1.5  christos 	    ND_PRINT((ndo, " [invalid number of sources]"));
    248      1.5  christos 	else if (ndo->ndo_vflag > 1) {
    249      1.5  christos 	    ND_PRINT((ndo, " {"));
    250      1.1  christos 	    for (i=0; i<nsrcs; i++) {
    251      1.5  christos 		ND_TCHECK2(bp[12+(i<<2)], 4);
    252      1.5  christos 		ND_PRINT((ndo, " %s", ipaddr_string(ndo, &bp[12+(i<<2)])));
    253      1.1  christos 	    }
    254      1.5  christos 	    ND_PRINT((ndo, " }"));
    255      1.1  christos 	} else
    256      1.5  christos 	    ND_PRINT((ndo, ", %d source(s)", nsrcs));
    257      1.1  christos     }
    258      1.5  christos     ND_PRINT((ndo, "]"));
    259      1.1  christos     return;
    260      1.1  christos trunc:
    261      1.5  christos     ND_PRINT((ndo, "%s", tstr));
    262      1.1  christos }
    263      1.1  christos 
    264      1.1  christos void
    265      1.5  christos igmp_print(netdissect_options *ndo,
    266      1.5  christos            register const u_char *bp, register u_int len)
    267      1.1  christos {
    268      1.3  christos     struct cksum_vec vec[1];
    269      1.3  christos 
    270      1.5  christos     if (ndo->ndo_qflag) {
    271      1.5  christos         ND_PRINT((ndo, "igmp"));
    272      1.1  christos         return;
    273      1.1  christos     }
    274      1.1  christos 
    275      1.5  christos     ND_TCHECK(bp[0]);
    276      1.1  christos     switch (bp[0]) {
    277      1.1  christos     case 0x11:
    278      1.5  christos         ND_PRINT((ndo, "igmp query"));
    279      1.1  christos 	if (len >= 12)
    280      1.5  christos 	    print_igmpv3_query(ndo, bp, len);
    281      1.1  christos 	else {
    282      1.5  christos             ND_TCHECK(bp[1]);
    283      1.1  christos 	    if (bp[1]) {
    284      1.5  christos 		ND_PRINT((ndo, " v2"));
    285      1.1  christos 		if (bp[1] != 100)
    286      1.5  christos 		    ND_PRINT((ndo, " [max resp time %d]", bp[1]));
    287      1.1  christos 	    } else
    288      1.5  christos 		ND_PRINT((ndo, " v1"));
    289      1.5  christos             ND_TCHECK2(bp[4], 4);
    290      1.1  christos 	    if (EXTRACT_32BITS(&bp[4]))
    291      1.5  christos                 ND_PRINT((ndo, " [gaddr %s]", ipaddr_string(ndo, &bp[4])));
    292      1.1  christos             if (len != 8)
    293      1.5  christos                 ND_PRINT((ndo, " [len %d]", len));
    294      1.1  christos 	}
    295      1.1  christos         break;
    296      1.1  christos     case 0x12:
    297      1.5  christos         ND_TCHECK2(bp[4], 4);
    298      1.5  christos         ND_PRINT((ndo, "igmp v1 report %s", ipaddr_string(ndo, &bp[4])));
    299      1.1  christos         if (len != 8)
    300      1.5  christos             ND_PRINT((ndo, " [len %d]", len));
    301      1.1  christos         break;
    302      1.1  christos     case 0x16:
    303      1.5  christos         ND_TCHECK2(bp[4], 4);
    304      1.5  christos         ND_PRINT((ndo, "igmp v2 report %s", ipaddr_string(ndo, &bp[4])));
    305      1.1  christos         break;
    306      1.1  christos     case 0x22:
    307      1.5  christos         ND_PRINT((ndo, "igmp v3 report"));
    308      1.5  christos 	print_igmpv3_report(ndo, bp, len);
    309      1.1  christos         break;
    310      1.1  christos     case 0x17:
    311      1.5  christos         ND_TCHECK2(bp[4], 4);
    312      1.5  christos         ND_PRINT((ndo, "igmp leave %s", ipaddr_string(ndo, &bp[4])));
    313      1.1  christos         break;
    314      1.1  christos     case 0x13:
    315      1.5  christos         ND_PRINT((ndo, "igmp dvmrp"));
    316      1.1  christos         if (len < 8)
    317      1.5  christos             ND_PRINT((ndo, " [len %d]", len));
    318      1.1  christos         else
    319      1.5  christos             dvmrp_print(ndo, bp, len);
    320      1.1  christos         break;
    321      1.1  christos     case 0x14:
    322      1.5  christos         ND_PRINT((ndo, "igmp pimv1"));
    323      1.5  christos         pimv1_print(ndo, bp, len);
    324      1.1  christos         break;
    325      1.1  christos     case 0x1e:
    326      1.5  christos         print_mresp(ndo, bp, len);
    327      1.1  christos         break;
    328      1.1  christos     case 0x1f:
    329      1.5  christos         print_mtrace(ndo, bp, len);
    330      1.1  christos         break;
    331      1.1  christos     default:
    332      1.5  christos         ND_PRINT((ndo, "igmp-%d", bp[0]));
    333      1.1  christos         break;
    334      1.1  christos     }
    335      1.1  christos 
    336  1.5.4.1    bouyer     if (ndo->ndo_vflag && len >= 4 && ND_TTEST2(bp[0], len)) {
    337      1.1  christos         /* Check the IGMP checksum */
    338      1.3  christos         vec[0].ptr = bp;
    339      1.3  christos         vec[0].len = len;
    340      1.3  christos         if (in_cksum(vec, 1))
    341      1.5  christos             ND_PRINT((ndo, " bad igmp cksum %x!", EXTRACT_16BITS(&bp[2])));
    342      1.1  christos     }
    343      1.1  christos     return;
    344      1.1  christos trunc:
    345      1.5  christos     ND_PRINT((ndo, "%s", tstr));
    346      1.1  christos }
    347