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