]> The Tcpdump Group git mirrors - tcpdump/blob - print-igrp.c
remove redundant ND_TCHECK, let GET_ routines handle checks
[tcpdump] / print-igrp.c
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 /* \summary: Interior Gateway Routing Protocol (IGRP) printer */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include "netdissect-stdinc.h"
31
32 #include "netdissect.h"
33 #include "extract.h"
34
35 /* Cisco IGRP definitions */
36
37 /* IGRP Header */
38
39 struct igrphdr {
40 nd_uint8_t ig_vop; /* protocol version number / opcode */
41 #define IGRP_V(x) (((x) & 0xf0) >> 4)
42 #define IGRP_OP(x) ((x) & 0x0f)
43 nd_uint8_t ig_ed; /* edition number */
44 nd_uint16_t ig_as; /* autonomous system number */
45 nd_uint16_t ig_ni; /* number of subnet in local net */
46 nd_uint16_t ig_ns; /* number of networks in AS */
47 nd_uint16_t ig_nx; /* number of networks ouside AS */
48 nd_uint16_t ig_sum; /* checksum of IGRP header & data */
49 };
50
51 #define IGRP_UPDATE 1
52 #define IGRP_REQUEST 2
53
54 /* IGRP routing entry */
55
56 struct igrprte {
57 nd_byte igr_net[3]; /* 3 significant octets of IP address */
58 nd_uint24_t igr_dly; /* delay in tens of microseconds */
59 nd_uint24_t igr_bw; /* bandwidth in units of 1 kb/s */
60 nd_uint16_t igr_mtu; /* MTU in octets */
61 nd_uint8_t igr_rel; /* percent packets successfully tx/rx */
62 nd_uint8_t igr_ld; /* percent of channel occupied */
63 nd_uint8_t igr_hct; /* hop count */
64 };
65
66 #define IGRP_RTE_SIZE 14 /* don't believe sizeof ! */
67
68 static void
69 igrp_entry_print(netdissect_options *ndo, const struct igrprte *igr,
70 int is_interior, int is_exterior)
71 {
72 u_int delay, bandwidth;
73 u_int metric, mtu;
74
75 if (is_interior)
76 ND_PRINT(" *.%u.%u.%u", igr->igr_net[0],
77 igr->igr_net[1], igr->igr_net[2]);
78 else if (is_exterior)
79 ND_PRINT(" X%u.%u.%u.0", igr->igr_net[0],
80 igr->igr_net[1], igr->igr_net[2]);
81 else
82 ND_PRINT(" %u.%u.%u.0", igr->igr_net[0],
83 igr->igr_net[1], igr->igr_net[2]);
84
85 delay = GET_BE_U_3(igr->igr_dly);
86 bandwidth = GET_BE_U_3(igr->igr_bw);
87 metric = bandwidth + delay;
88 if (metric > 0xffffff)
89 metric = 0xffffff;
90 mtu = GET_BE_U_2(igr->igr_mtu);
91
92 ND_PRINT(" d=%u b=%u r=%u l=%u M=%u mtu=%u in %u hops",
93 10 * delay, bandwidth == 0 ? 0 : 10000000 / bandwidth,
94 GET_U_1(igr->igr_rel), GET_U_1(igr->igr_ld), metric,
95 mtu, GET_U_1(igr->igr_hct));
96 }
97
98 static const struct tok op2str[] = {
99 { IGRP_UPDATE, "update" },
100 { IGRP_REQUEST, "request" },
101 { 0, NULL }
102 };
103
104 void
105 igrp_print(netdissect_options *ndo, const u_char *bp, u_int length)
106 {
107 const struct igrphdr *hdr;
108 const u_char *cp;
109 u_int nint, nsys, next;
110
111 ndo->ndo_protocol = "igrp";
112 hdr = (const struct igrphdr *)bp;
113 cp = (const u_char *)(hdr + 1);
114 ND_PRINT("igrp:");
115
116 /* Header */
117 ND_TCHECK_SIZE(hdr);
118 nint = GET_BE_U_2(hdr->ig_ni);
119 nsys = GET_BE_U_2(hdr->ig_ns);
120 next = GET_BE_U_2(hdr->ig_nx);
121
122 ND_PRINT(" %s V%u edit=%u AS=%u (%u/%u/%u)",
123 tok2str(op2str, "op-#%u", IGRP_OP(GET_U_1(hdr->ig_vop))),
124 IGRP_V(GET_U_1(hdr->ig_vop)),
125 GET_U_1(hdr->ig_ed),
126 GET_BE_U_2(hdr->ig_as),
127 nint,
128 nsys,
129 next);
130
131 length -= sizeof(*hdr);
132 while (length >= IGRP_RTE_SIZE) {
133 if (nint > 0) {
134 ND_TCHECK_LEN(cp, IGRP_RTE_SIZE);
135 igrp_entry_print(ndo, (const struct igrprte *)cp, 1, 0);
136 --nint;
137 } else if (nsys > 0) {
138 ND_TCHECK_LEN(cp, IGRP_RTE_SIZE);
139 igrp_entry_print(ndo, (const struct igrprte *)cp, 0, 0);
140 --nsys;
141 } else if (next > 0) {
142 ND_TCHECK_LEN(cp, IGRP_RTE_SIZE);
143 igrp_entry_print(ndo, (const struct igrprte *)cp, 0, 1);
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 == 0 && nsys == 0 && next == 0)
153 return;
154 trunc:
155 nd_print_trunc(ndo);
156 }