xref: /netbsd-src/external/bsd/tcpdump/dist/print-igrp.c (revision 26ba0b503b498a5194a71ac319838b7f5497f3fe)
10f74e101Schristos /*
20f74e101Schristos  * Copyright (c) 1996, 1997
30f74e101Schristos  *	The Regents of the University of California.  All rights reserved.
40f74e101Schristos  *
50f74e101Schristos  * Redistribution and use in source and binary forms, with or without
60f74e101Schristos  * modification, are permitted provided that: (1) source code distributions
70f74e101Schristos  * retain the above copyright notice and this paragraph in its entirety, (2)
80f74e101Schristos  * distributions including binary code include the above copyright notice and
90f74e101Schristos  * this paragraph in its entirety in the documentation or other materials
100f74e101Schristos  * provided with the distribution, and (3) all advertising materials mentioning
110f74e101Schristos  * features or use of this software display the following acknowledgement:
120f74e101Schristos  * ``This product includes software developed by the University of California,
130f74e101Schristos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
140f74e101Schristos  * the University nor the names of its contributors may be used to endorse
150f74e101Schristos  * or promote products derived from this software without specific prior
160f74e101Schristos  * written permission.
170f74e101Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
180f74e101Schristos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
190f74e101Schristos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
200f74e101Schristos  *
210f74e101Schristos  * Initial contribution from Francis Dupont (francis.dupont@inria.fr)
220f74e101Schristos  */
230f74e101Schristos 
2411b3aaa1Schristos #include <sys/cdefs.h>
250f74e101Schristos #ifndef lint
26*26ba0b50Schristos __RCSID("$NetBSD: print-igrp.c,v 1.9 2024/09/02 16:15:31 christos Exp $");
270f74e101Schristos #endif
280f74e101Schristos 
29dc860a36Sspz /* \summary: Interior Gateway Routing Protocol (IGRP) printer */
30dc860a36Sspz 
31c74ad251Schristos #include <config.h>
320f74e101Schristos 
33c74ad251Schristos #include "netdissect-stdinc.h"
340f74e101Schristos 
35fdccd7e4Schristos #include "netdissect.h"
36fdccd7e4Schristos #include "extract.h"
370f74e101Schristos 
38b3a00663Schristos /* Cisco IGRP definitions */
39b3a00663Schristos 
40b3a00663Schristos /* IGRP Header */
41b3a00663Schristos 
42b3a00663Schristos struct igrphdr {
43c74ad251Schristos 	nd_uint8_t ig_vop;	/* protocol version number / opcode */
44b3a00663Schristos #define IGRP_V(x)	(((x) & 0xf0) >> 4)
45b3a00663Schristos #define IGRP_OP(x)	((x) & 0x0f)
46c74ad251Schristos 	nd_uint8_t ig_ed;	/* edition number */
47c74ad251Schristos 	nd_uint16_t ig_as;	/* autonomous system number */
48c74ad251Schristos 	nd_uint16_t ig_ni;	/* number of subnet in local net */
49c74ad251Schristos 	nd_uint16_t ig_ns;	/* number of networks in AS */
50c74ad251Schristos 	nd_uint16_t ig_nx;	/* number of networks outside AS */
51c74ad251Schristos 	nd_uint16_t ig_sum;	/* checksum of IGRP header & data */
52b3a00663Schristos };
53b3a00663Schristos 
54b3a00663Schristos #define IGRP_UPDATE	1
55b3a00663Schristos #define IGRP_REQUEST	2
56b3a00663Schristos 
57b3a00663Schristos /* IGRP routing entry */
58b3a00663Schristos 
59b3a00663Schristos struct igrprte {
60c74ad251Schristos 	nd_byte igr_net[3];	/* 3 significant octets of IP address */
61c74ad251Schristos 	nd_uint24_t igr_dly;	/* delay in tens of microseconds */
62c74ad251Schristos 	nd_uint24_t igr_bw;	/* bandwidth in units of 1 kb/s */
63c74ad251Schristos 	nd_uint16_t igr_mtu;	/* MTU in octets */
64c74ad251Schristos 	nd_uint8_t igr_rel;	/* percent packets successfully tx/rx */
65c74ad251Schristos 	nd_uint8_t igr_ld;	/* percent of channel occupied */
66c74ad251Schristos 	nd_uint8_t igr_hct;	/* hop count */
67b3a00663Schristos };
68b3a00663Schristos 
69c74ad251Schristos #define IGRP_RTE_SIZE	14	/* sizeof() is accurate now */
70b3a00663Schristos 
710f74e101Schristos static void
72c74ad251Schristos igrp_entry_print(netdissect_options *ndo, const struct igrprte *igr)
730f74e101Schristos {
74c74ad251Schristos 	u_int delay, bandwidth;
750f74e101Schristos 	u_int metric, mtu;
760f74e101Schristos 
77c74ad251Schristos 	delay = GET_BE_U_3(igr->igr_dly);
78c74ad251Schristos 	bandwidth = GET_BE_U_3(igr->igr_bw);
79c74ad251Schristos 	metric = ND_MIN(bandwidth + delay, 0xffffff);
80c74ad251Schristos 	mtu = GET_BE_U_2(igr->igr_mtu);
810f74e101Schristos 
82c74ad251Schristos 	ND_PRINT(" d=%u b=%u r=%u l=%u M=%u mtu=%u in %u hops",
830f74e101Schristos 	    10 * delay, bandwidth == 0 ? 0 : 10000000 / bandwidth,
84c74ad251Schristos 	    GET_U_1(igr->igr_rel), GET_U_1(igr->igr_ld), metric,
85c74ad251Schristos 	    mtu, GET_U_1(igr->igr_hct));
860f74e101Schristos }
870f74e101Schristos 
88870189d2Schristos static const struct tok op2str[] = {
890f74e101Schristos 	{ IGRP_UPDATE,		"update" },
900f74e101Schristos 	{ IGRP_REQUEST,		"request" },
910f74e101Schristos 	{ 0,			NULL }
920f74e101Schristos };
930f74e101Schristos 
940f74e101Schristos void
95c74ad251Schristos igrp_print(netdissect_options *ndo, const u_char *bp, u_int length)
960f74e101Schristos {
97c74ad251Schristos 	const struct igrphdr *hdr;
98c74ad251Schristos 	const u_char *cp;
990f74e101Schristos 	u_int nint, nsys, next;
100c74ad251Schristos 	uint16_t cksum;
1010f74e101Schristos 
102c74ad251Schristos 	ndo->ndo_protocol = "igrp";
103fdccd7e4Schristos 	hdr = (const struct igrphdr *)bp;
104fdccd7e4Schristos 	cp = (const u_char *)(hdr + 1);
105c74ad251Schristos 	ND_PRINT("igrp:");
1060f74e101Schristos 
1070f74e101Schristos 	/* Header */
108c74ad251Schristos 	nint = GET_BE_U_2(hdr->ig_ni);
109c74ad251Schristos 	nsys = GET_BE_U_2(hdr->ig_ns);
110c74ad251Schristos 	next = GET_BE_U_2(hdr->ig_nx);
1110f74e101Schristos 
112c74ad251Schristos 	ND_PRINT(" %s V%u edit=%u AS=%u (%u/%u/%u)",
113c74ad251Schristos 	    tok2str(op2str, "op-#%u", IGRP_OP(GET_U_1(hdr->ig_vop))),
114c74ad251Schristos 	    IGRP_V(GET_U_1(hdr->ig_vop)),
115c74ad251Schristos 	    GET_U_1(hdr->ig_ed),
116c74ad251Schristos 	    GET_BE_U_2(hdr->ig_as),
1170f74e101Schristos 	    nint,
1180f74e101Schristos 	    nsys,
119c74ad251Schristos 	    next);
120c74ad251Schristos 	cksum = GET_BE_U_2(hdr->ig_sum);
121c74ad251Schristos 	if (ndo->ndo_vflag)
122c74ad251Schristos 		ND_PRINT(" checksum=0x%04x", cksum);
1230f74e101Schristos 
1240f74e101Schristos 	length -= sizeof(*hdr);
1250f74e101Schristos 	while (length >= IGRP_RTE_SIZE) {
126c74ad251Schristos 		const struct igrprte *igr = (const struct igrprte *)cp;
127c74ad251Schristos 		uint8_t net0 = GET_U_1(&igr->igr_net[0]);
128c74ad251Schristos 		uint8_t net1 = GET_U_1(&igr->igr_net[1]);
129c74ad251Schristos 		uint8_t net2 = GET_U_1(&igr->igr_net[2]);
130c74ad251Schristos 
1310f74e101Schristos 		if (nint > 0) {
132c74ad251Schristos 			ND_PRINT(" *.%u.%u.%u", net0, net1, net2);
133c74ad251Schristos 			igrp_entry_print(ndo, igr);
1340f74e101Schristos 			--nint;
1350f74e101Schristos 		} else if (nsys > 0) {
136c74ad251Schristos 			ND_PRINT(" %u.%u.%u.0", net0, net1, net2);
137c74ad251Schristos 			igrp_entry_print(ndo, igr);
1380f74e101Schristos 			--nsys;
1390f74e101Schristos 		} else if (next > 0) {
140c74ad251Schristos 			ND_PRINT(" X%u.%u.%u.0", net0, net1, net2);
141c74ad251Schristos 			igrp_entry_print(ndo, igr);
1420f74e101Schristos 			--next;
1430f74e101Schristos 		} else {
144c74ad251Schristos 			ND_PRINT(" [extra bytes %u]", length);
1450f74e101Schristos 			break;
1460f74e101Schristos 		}
1470f74e101Schristos 		cp += IGRP_RTE_SIZE;
1480f74e101Schristos 		length -= IGRP_RTE_SIZE;
1490f74e101Schristos 	}
150c74ad251Schristos 	if (nint || nsys || next || length)
151c74ad251Schristos 		nd_print_invalid(ndo);
1520f74e101Schristos }
153