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