xref: /netbsd-src/external/bsd/tcpdump/dist/print-igrp.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*
2  * Copyright (c) 1996, 1997
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  * Initial contribution from Francis Dupont (francis.dupont@inria.fr)
22  */
23 
24 #include <sys/cdefs.h>
25 #ifndef lint
26 __RCSID("$NetBSD: print-igrp.c,v 1.7 2017/02/05 04:05:05 spz Exp $");
27 #endif
28 
29 /* \summary: Interior Gateway Routing Protocol (IGRP) printer */
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <netdissect-stdinc.h>
36 
37 #include "netdissect.h"
38 #include "extract.h"
39 
40 /* Cisco IGRP definitions */
41 
42 /* IGRP Header */
43 
44 struct igrphdr {
45 	uint8_t ig_vop;	/* protocol version number / opcode */
46 #define IGRP_V(x)	(((x) & 0xf0) >> 4)
47 #define IGRP_OP(x)	((x) & 0x0f)
48 	uint8_t ig_ed;		/* edition number */
49 	uint16_t ig_as;	/* autonomous system number */
50 	uint16_t ig_ni;	/* number of subnet in local net */
51 	uint16_t ig_ns;	/* number of networks in AS */
52 	uint16_t ig_nx;	/* number of networks ouside AS */
53 	uint16_t ig_sum;	/* checksum of IGRP header & data */
54 };
55 
56 #define IGRP_UPDATE	1
57 #define IGRP_REQUEST	2
58 
59 /* IGRP routing entry */
60 
61 struct igrprte {
62 	uint8_t igr_net[3];	/* 3 significant octets of IP address */
63 	uint8_t igr_dly[3];	/* delay in tens of microseconds */
64 	uint8_t igr_bw[3];	/* bandwidth in units of 1 kb/s */
65 	uint8_t igr_mtu[2];	/* MTU in octets */
66 	uint8_t igr_rel;	/* percent packets successfully tx/rx */
67 	uint8_t igr_ld;	/* percent of channel occupied */
68 	uint8_t igr_hct;	/* hop count */
69 };
70 
71 #define IGRP_RTE_SIZE	14	/* don't believe sizeof ! */
72 
73 static void
74 igrp_entry_print(netdissect_options *ndo, register const struct igrprte *igr,
75     register int is_interior, register int is_exterior)
76 {
77 	register u_int delay, bandwidth;
78 	u_int metric, mtu;
79 
80 	if (is_interior)
81 		ND_PRINT((ndo, " *.%d.%d.%d", igr->igr_net[0],
82 		    igr->igr_net[1], igr->igr_net[2]));
83 	else if (is_exterior)
84 		ND_PRINT((ndo, " X%d.%d.%d.0", igr->igr_net[0],
85 		    igr->igr_net[1], igr->igr_net[2]));
86 	else
87 		ND_PRINT((ndo, " %d.%d.%d.0", igr->igr_net[0],
88 		    igr->igr_net[1], igr->igr_net[2]));
89 
90 	delay = EXTRACT_24BITS(igr->igr_dly);
91 	bandwidth = EXTRACT_24BITS(igr->igr_bw);
92 	metric = bandwidth + delay;
93 	if (metric > 0xffffff)
94 		metric = 0xffffff;
95 	mtu = EXTRACT_16BITS(igr->igr_mtu);
96 
97 	ND_PRINT((ndo, " d=%d b=%d r=%d l=%d M=%d mtu=%d in %d hops",
98 	    10 * delay, bandwidth == 0 ? 0 : 10000000 / bandwidth,
99 	    igr->igr_rel, igr->igr_ld, metric,
100 	    mtu, igr->igr_hct));
101 }
102 
103 static const struct tok op2str[] = {
104 	{ IGRP_UPDATE,		"update" },
105 	{ IGRP_REQUEST,		"request" },
106 	{ 0,			NULL }
107 };
108 
109 void
110 igrp_print(netdissect_options *ndo, register const u_char *bp, u_int length)
111 {
112 	register const struct igrphdr *hdr;
113 	register const u_char *cp;
114 	u_int nint, nsys, next;
115 
116 	hdr = (const struct igrphdr *)bp;
117 	cp = (const u_char *)(hdr + 1);
118 	ND_PRINT((ndo, "igrp:"));
119 
120 	/* Header */
121 	ND_TCHECK(*hdr);
122 	nint = EXTRACT_16BITS(&hdr->ig_ni);
123 	nsys = EXTRACT_16BITS(&hdr->ig_ns);
124 	next = EXTRACT_16BITS(&hdr->ig_nx);
125 
126 	ND_PRINT((ndo, " %s V%d edit=%d AS=%d (%d/%d/%d)",
127 	    tok2str(op2str, "op-#%d", IGRP_OP(hdr->ig_vop)),
128 	    IGRP_V(hdr->ig_vop),
129 	    hdr->ig_ed,
130 	    EXTRACT_16BITS(&hdr->ig_as),
131 	    nint,
132 	    nsys,
133 	    next));
134 
135 	length -= sizeof(*hdr);
136 	while (length >= IGRP_RTE_SIZE) {
137 		if (nint > 0) {
138 			ND_TCHECK2(*cp, IGRP_RTE_SIZE);
139 			igrp_entry_print(ndo, (const struct igrprte *)cp, 1, 0);
140 			--nint;
141 		} else if (nsys > 0) {
142 			ND_TCHECK2(*cp, IGRP_RTE_SIZE);
143 			igrp_entry_print(ndo, (const struct igrprte *)cp, 0, 0);
144 			--nsys;
145 		} else if (next > 0) {
146 			ND_TCHECK2(*cp, IGRP_RTE_SIZE);
147 			igrp_entry_print(ndo, (const struct igrprte *)cp, 0, 1);
148 			--next;
149 		} else {
150 			ND_PRINT((ndo, " [extra bytes %d]", length));
151 			break;
152 		}
153 		cp += IGRP_RTE_SIZE;
154 		length -= IGRP_RTE_SIZE;
155 	}
156 	if (nint == 0 && nsys == 0 && next == 0)
157 		return;
158 trunc:
159 	ND_PRINT((ndo, " [|igrp]"));
160 }
161