xref: /netbsd-src/external/bsd/tcpdump/dist/print-igrp.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
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.9 2024/09/02 16:15:31 christos Exp $");
27 #endif
28 
29 /* \summary: Interior Gateway Routing Protocol (IGRP) printer */
30 
31 #include <config.h>
32 
33 #include "netdissect-stdinc.h"
34 
35 #include "netdissect.h"
36 #include "extract.h"
37 
38 /* Cisco IGRP definitions */
39 
40 /* IGRP Header */
41 
42 struct igrphdr {
43 	nd_uint8_t ig_vop;	/* protocol version number / opcode */
44 #define IGRP_V(x)	(((x) & 0xf0) >> 4)
45 #define IGRP_OP(x)	((x) & 0x0f)
46 	nd_uint8_t ig_ed;	/* edition number */
47 	nd_uint16_t ig_as;	/* autonomous system number */
48 	nd_uint16_t ig_ni;	/* number of subnet in local net */
49 	nd_uint16_t ig_ns;	/* number of networks in AS */
50 	nd_uint16_t ig_nx;	/* number of networks outside AS */
51 	nd_uint16_t ig_sum;	/* checksum of IGRP header & data */
52 };
53 
54 #define IGRP_UPDATE	1
55 #define IGRP_REQUEST	2
56 
57 /* IGRP routing entry */
58 
59 struct igrprte {
60 	nd_byte igr_net[3];	/* 3 significant octets of IP address */
61 	nd_uint24_t igr_dly;	/* delay in tens of microseconds */
62 	nd_uint24_t igr_bw;	/* bandwidth in units of 1 kb/s */
63 	nd_uint16_t igr_mtu;	/* MTU in octets */
64 	nd_uint8_t igr_rel;	/* percent packets successfully tx/rx */
65 	nd_uint8_t igr_ld;	/* percent of channel occupied */
66 	nd_uint8_t igr_hct;	/* hop count */
67 };
68 
69 #define IGRP_RTE_SIZE	14	/* sizeof() is accurate now */
70 
71 static void
72 igrp_entry_print(netdissect_options *ndo, const struct igrprte *igr)
73 {
74 	u_int delay, bandwidth;
75 	u_int metric, mtu;
76 
77 	delay = GET_BE_U_3(igr->igr_dly);
78 	bandwidth = GET_BE_U_3(igr->igr_bw);
79 	metric = ND_MIN(bandwidth + delay, 0xffffff);
80 	mtu = GET_BE_U_2(igr->igr_mtu);
81 
82 	ND_PRINT(" d=%u b=%u r=%u l=%u M=%u mtu=%u in %u hops",
83 	    10 * delay, bandwidth == 0 ? 0 : 10000000 / bandwidth,
84 	    GET_U_1(igr->igr_rel), GET_U_1(igr->igr_ld), metric,
85 	    mtu, GET_U_1(igr->igr_hct));
86 }
87 
88 static const struct tok op2str[] = {
89 	{ IGRP_UPDATE,		"update" },
90 	{ IGRP_REQUEST,		"request" },
91 	{ 0,			NULL }
92 };
93 
94 void
95 igrp_print(netdissect_options *ndo, const u_char *bp, u_int length)
96 {
97 	const struct igrphdr *hdr;
98 	const u_char *cp;
99 	u_int nint, nsys, next;
100 	uint16_t cksum;
101 
102 	ndo->ndo_protocol = "igrp";
103 	hdr = (const struct igrphdr *)bp;
104 	cp = (const u_char *)(hdr + 1);
105 	ND_PRINT("igrp:");
106 
107 	/* Header */
108 	nint = GET_BE_U_2(hdr->ig_ni);
109 	nsys = GET_BE_U_2(hdr->ig_ns);
110 	next = GET_BE_U_2(hdr->ig_nx);
111 
112 	ND_PRINT(" %s V%u edit=%u AS=%u (%u/%u/%u)",
113 	    tok2str(op2str, "op-#%u", IGRP_OP(GET_U_1(hdr->ig_vop))),
114 	    IGRP_V(GET_U_1(hdr->ig_vop)),
115 	    GET_U_1(hdr->ig_ed),
116 	    GET_BE_U_2(hdr->ig_as),
117 	    nint,
118 	    nsys,
119 	    next);
120 	cksum = GET_BE_U_2(hdr->ig_sum);
121 	if (ndo->ndo_vflag)
122 		ND_PRINT(" checksum=0x%04x", cksum);
123 
124 	length -= sizeof(*hdr);
125 	while (length >= IGRP_RTE_SIZE) {
126 		const struct igrprte *igr = (const struct igrprte *)cp;
127 		uint8_t net0 = GET_U_1(&igr->igr_net[0]);
128 		uint8_t net1 = GET_U_1(&igr->igr_net[1]);
129 		uint8_t net2 = GET_U_1(&igr->igr_net[2]);
130 
131 		if (nint > 0) {
132 			ND_PRINT(" *.%u.%u.%u", net0, net1, net2);
133 			igrp_entry_print(ndo, igr);
134 			--nint;
135 		} else if (nsys > 0) {
136 			ND_PRINT(" %u.%u.%u.0", net0, net1, net2);
137 			igrp_entry_print(ndo, igr);
138 			--nsys;
139 		} else if (next > 0) {
140 			ND_PRINT(" X%u.%u.%u.0", net0, net1, net2);
141 			igrp_entry_print(ndo, igr);
142 			--next;
143 		} else {
144 			ND_PRINT(" [extra bytes %u]", length);
145 			break;
146 		}
147 		cp += IGRP_RTE_SIZE;
148 		length -= IGRP_RTE_SIZE;
149 	}
150 	if (nint || nsys || next || length)
151 		nd_print_invalid(ndo);
152 }
153