]> The Tcpdump Group git mirrors - tcpdump/blob - print-ripng.c
Use nd_ipv6 rather than struct in6_addr in packet-layout structures.
[tcpdump] / print-ripng.c
1 /*
2 * Copyright (c) 1989, 1990, 1991, 1993, 1994
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
22 /* \summary: IPv6 Routing Information Protocol (RIPng) printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include "netdissect.h"
31 #include "addrtoname.h"
32 #include "extract.h"
33
34 /*
35 * Copyright (C) 1995, 1996, 1997 and 1998 WIDE Project.
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the project nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62 #define RIP6_VERSION 1
63
64 #define RIP6_REQUEST 1
65 #define RIP6_RESPONSE 2
66
67 struct netinfo6 {
68 nd_ipv6 rip6_dest;
69 nd_uint16_t rip6_tag;
70 nd_uint8_t rip6_plen;
71 nd_uint8_t rip6_metric;
72 };
73
74 struct rip6 {
75 nd_uint8_t rip6_cmd;
76 nd_uint8_t rip6_vers;
77 nd_byte rip6_res1[2];
78 union {
79 struct netinfo6 ru6_nets[1];
80 nd_byte ru6_tracefile[1];
81 } rip6un;
82 #define rip6_nets rip6un.ru6_nets
83 #define rip6_tracefile rip6un.ru6_tracefile
84 };
85
86 #define HOPCNT_INFINITY6 16
87
88 static int ND_IN6_IS_ADDR_UNSPECIFIED(const nd_ipv6 *addr)
89 {
90 static const struct in6_addr in6addr_any; /* :: */
91 return (memcmp(addr, &in6addr_any, sizeof(*addr)) == 0);
92 }
93
94 static int
95 rip6_entry_print(netdissect_options *ndo, const struct netinfo6 *ni, u_int metric)
96 {
97 int l;
98 uint16_t tag;
99
100 l = ND_PRINT("%s/%u", ip6addr_string(ndo, &ni->rip6_dest), EXTRACT_U_1(ni->rip6_plen));
101 tag = EXTRACT_BE_U_2(ni->rip6_tag);
102 if (tag)
103 l += ND_PRINT(" [%u]", tag);
104 if (metric)
105 l += ND_PRINT(" (%u)", metric);
106 return l;
107 }
108
109 void
110 ripng_print(netdissect_options *ndo, const u_char *dat, unsigned int length)
111 {
112 const struct rip6 *rp = (const struct rip6 *)dat;
113 uint8_t cmd;
114 const struct netinfo6 *ni;
115 unsigned int length_left;
116 u_int j;
117
118 ND_TCHECK_1(rp->rip6_cmd);
119 cmd = EXTRACT_U_1(rp->rip6_cmd);
120 switch (cmd) {
121
122 case RIP6_REQUEST:
123 length_left = length;
124 if (length_left < (sizeof(struct rip6) - sizeof(struct netinfo6)))
125 goto trunc;
126 length_left -= (sizeof(struct rip6) - sizeof(struct netinfo6));
127 j = length_left / sizeof(*ni);
128 if (j == 1) {
129 ND_TCHECK_SIZE(rp->rip6_nets);
130 if (EXTRACT_U_1(rp->rip6_nets->rip6_metric) == HOPCNT_INFINITY6
131 && ND_IN6_IS_ADDR_UNSPECIFIED(&rp->rip6_nets->rip6_dest)) {
132 ND_PRINT(" ripng-req dump");
133 break;
134 }
135 }
136 if (j * sizeof(*ni) != length_left)
137 ND_PRINT(" ripng-req %u[%u]:", j, length);
138 else
139 ND_PRINT(" ripng-req %u:", j);
140 for (ni = rp->rip6_nets; length_left >= sizeof(*ni);
141 length_left -= sizeof(*ni), ++ni) {
142 ND_TCHECK_SIZE(ni);
143 if (ndo->ndo_vflag > 1)
144 ND_PRINT("\n\t");
145 else
146 ND_PRINT(" ");
147 rip6_entry_print(ndo, ni, 0);
148 }
149 if (length_left != 0)
150 goto trunc;
151 break;
152 case RIP6_RESPONSE:
153 length_left = length;
154 if (length_left < (sizeof(struct rip6) - sizeof(struct netinfo6)))
155 goto trunc;
156 length_left -= (sizeof(struct rip6) - sizeof(struct netinfo6));
157 j = length_left / sizeof(*ni);
158 if (j * sizeof(*ni) != length_left)
159 ND_PRINT(" ripng-resp %u[%u]:", j, length);
160 else
161 ND_PRINT(" ripng-resp %u:", j);
162 for (ni = rp->rip6_nets; length_left >= sizeof(*ni);
163 length_left -= sizeof(*ni), ++ni) {
164 ND_TCHECK_SIZE(ni);
165 if (ndo->ndo_vflag > 1)
166 ND_PRINT("\n\t");
167 else
168 ND_PRINT(" ");
169 rip6_entry_print(ndo, ni, EXTRACT_U_1(ni->rip6_metric));
170 }
171 if (length_left != 0)
172 goto trunc;
173 break;
174 default:
175 ND_PRINT(" ripng-%u ?? %u", cmd, length);
176 break;
177 }
178 ND_TCHECK_1(rp->rip6_vers);
179 if (EXTRACT_U_1(rp->rip6_vers) != RIP6_VERSION)
180 ND_PRINT(" [vers %u]", EXTRACT_U_1(rp->rip6_vers));
181 return;
182
183 trunc:
184 ND_PRINT("[|ripng]");
185 return;
186 }