]> The Tcpdump Group git mirrors - tcpdump/blob - print-babel.c
8b422b3f1fea05620838c07c7e60455bc8c786a1
[tcpdump] / print-babel.c
1 /*
2 * Copyright (c) 2007-2011 Grégoire Henry, Juliusz Chroboczek
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the project nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "addrtoname.h"
39 #include "interface.h"
40 #include "extract.h"
41
42 static void babel_print_v2(const u_char *cp, u_int length);
43
44 void
45 babel_print(const u_char *cp, u_int length) {
46 printf("babel");
47
48 TCHECK2(*cp, 4);
49
50 if(cp[0] != 42) {
51 printf(" malformed header");
52 return;
53 } else {
54 printf(" %d", cp[1]);
55 }
56
57 switch(cp[1]) {
58 case 2:
59 babel_print_v2(cp,length);
60 break;
61 default:
62 printf(" unknown version");
63 break;
64 }
65
66 return;
67
68 trunc:
69 printf(" [|babel]");
70 return;
71 }
72
73 #define MESSAGE_PAD1 0
74 #define MESSAGE_PADN 1
75 #define MESSAGE_ACK_REQ 2
76 #define MESSAGE_ACK 3
77 #define MESSAGE_HELLO 4
78 #define MESSAGE_IHU 5
79 #define MESSAGE_ROUTER_ID 6
80 #define MESSAGE_NH 7
81 #define MESSAGE_UPDATE 8
82 #define MESSAGE_REQUEST 9
83 #define MESSAGE_MH_REQUEST 10
84 #define MESSAGE_TSPC 11
85 #define MESSAGE_HMAC 12
86
87 static const char *
88 format_id(const u_char *id)
89 {
90 static char buf[25];
91 snprintf(buf, 25, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
92 id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]);
93 buf[24] = '\0';
94 return buf;
95 }
96
97 static const unsigned char v4prefix[16] =
98 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
99
100 static const char *
101 format_prefix(const u_char *prefix, unsigned char plen)
102 {
103 static char buf[50];
104 if(plen >= 96 && memcmp(prefix, v4prefix, 12) == 0)
105 snprintf(buf, 50, "%s/%u", ipaddr_string(prefix + 12), plen - 96);
106 else
107 #ifdef INET6
108 snprintf(buf, 50, "%s/%u", ip6addr_string(prefix), plen);
109 #else
110 snprintf(buf, 50, "IPv6 addresses not supported");
111 #endif
112 buf[49] = '\0';
113 return buf;
114 }
115
116 static const char *
117 format_address(const u_char *prefix)
118 {
119 if(memcmp(prefix, v4prefix, 12) == 0)
120 return ipaddr_string(prefix + 12);
121 else
122 #ifdef INET6
123 return ip6addr_string(prefix);
124 #else
125 return "IPv6 addresses not supported";
126 #endif
127 }
128
129 static int
130 network_prefix(int ae, int plen, unsigned int omitted,
131 const unsigned char *p, const unsigned char *dp,
132 unsigned int len, unsigned char *p_r)
133 {
134 unsigned pb;
135 unsigned char prefix[16];
136
137 if(plen >= 0)
138 pb = (plen + 7) / 8;
139 else if(ae == 1)
140 pb = 4;
141 else
142 pb = 16;
143
144 if(pb > 16)
145 return -1;
146
147 memset(prefix, 0, 16);
148
149 switch(ae) {
150 case 0: break;
151 case 1:
152 if(omitted > 4 || pb > 4 || (pb > omitted && len < pb - omitted))
153 return -1;
154 memcpy(prefix, v4prefix, 12);
155 if(omitted) {
156 if (dp == NULL) return -1;
157 memcpy(prefix, dp, 12 + omitted);
158 }
159 if(pb > omitted) memcpy(prefix + 12 + omitted, p, pb - omitted);
160 break;
161 case 2:
162 if(omitted > 16 || (pb > omitted && len < pb - omitted))
163 return -1;
164 if(omitted) {
165 if (dp == NULL) return -1;
166 memcpy(prefix, dp, omitted);
167 }
168 if(pb > omitted) memcpy(prefix + omitted, p, pb - omitted);
169 break;
170 case 3:
171 if(pb > 8 && len < pb - 8) return -1;
172 prefix[0] = 0xfe;
173 prefix[1] = 0x80;
174 if(pb > 8) memcpy(prefix + 8, p, pb - 8);
175 break;
176 default:
177 return -1;
178 }
179
180 memcpy(p_r, prefix, 16);
181 return 1;
182 }
183
184 static int
185 network_address(int ae, const unsigned char *a, unsigned int len,
186 unsigned char *a_r)
187 {
188 return network_prefix(ae, -1, 0, a, NULL, len, a_r);
189 }
190
191 #define ICHECK(i, l) \
192 if ((i) + (l) > bodylen || (i) + (l) > length) goto corrupt;
193
194 static void
195 babel_print_v2(const u_char *cp, u_int length) {
196 u_int i;
197 u_short bodylen;
198 u_char v4_prefix[16] =
199 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
200 u_char v6_prefix[16] = {0};
201
202 TCHECK2(*cp, 4);
203 if (length < 4)
204 goto corrupt;
205 bodylen = EXTRACT_16BITS(cp + 2);
206 printf(" (%u)", bodylen);
207
208 /* Process the TLVs in the body */
209 i = 0;
210 while(i < bodylen) {
211 const u_char *message;
212 u_int type, len;
213
214 message = cp + 4 + i;
215
216 TCHECK2(*message, 1);
217 if((type = message[0]) == MESSAGE_PAD1) {
218 printf(vflag ? "\n\tPad 1" : " pad1");
219 i += 1;
220 continue;
221 }
222
223 TCHECK2(*message, 2);
224 ICHECK(i, 2);
225 len = message[1];
226
227 TCHECK2(*message, 2 + len);
228 ICHECK(i, 2 + len);
229
230 switch(type) {
231 case MESSAGE_PADN: {
232 if(!vflag)
233 printf(" padN");
234 else
235 printf("\n\tPad %d", len + 2);
236 }
237 break;
238
239 case MESSAGE_ACK_REQ: {
240 u_short nonce, interval;
241 if(!vflag)
242 printf(" ack-req");
243 else {
244 printf("\n\tAcknowledgment Request ");
245 if(len < 6) goto corrupt;
246 nonce = EXTRACT_16BITS(message + 4);
247 interval = EXTRACT_16BITS(message + 6);
248 printf("%04x %d", nonce, interval);
249 }
250 }
251 break;
252
253 case MESSAGE_ACK: {
254 u_short nonce;
255 if(!vflag)
256 printf(" ack");
257 else {
258 printf("\n\tAcknowledgment ");
259 if(len < 2) goto corrupt;
260 nonce = EXTRACT_16BITS(message + 2);
261 printf("%04x", nonce);
262 }
263 }
264 break;
265
266 case MESSAGE_HELLO: {
267 u_short seqno, interval;
268 if(!vflag)
269 printf(" hello");
270 else {
271 printf("\n\tHello ");
272 if(len < 6) goto corrupt;
273 seqno = EXTRACT_16BITS(message + 4);
274 interval = EXTRACT_16BITS(message + 6);
275 printf("seqno %u interval %u", seqno, interval);
276 }
277 }
278 break;
279
280 case MESSAGE_IHU: {
281 unsigned short txcost, interval;
282 if(!vflag)
283 printf(" ihu");
284 else {
285 u_char address[16];
286 int rc;
287 printf("\n\tIHU ");
288 if(len < 6) goto corrupt;
289 txcost = EXTRACT_16BITS(message + 4);
290 interval = EXTRACT_16BITS(message + 6);
291 rc = network_address(message[2], message + 8, len - 6, address);
292 if(rc < 0) { printf("[|babel]"); break; }
293 printf("%s txcost %u interval %d",
294 format_address(address), txcost, interval);
295 }
296 }
297 break;
298
299 case MESSAGE_ROUTER_ID: {
300 if(!vflag)
301 printf(" router-id");
302 else {
303 printf("\n\tRouter Id");
304 if(len < 10) goto corrupt;
305 printf(" %s", format_id(message + 4));
306 }
307 }
308 break;
309
310 case MESSAGE_NH: {
311 if(!vflag)
312 printf(" nh");
313 else {
314 int rc;
315 u_char nh[16];
316 printf("\n\tNext Hop");
317 if(len < 2) goto corrupt;
318 rc = network_address(message[2], message + 4, len - 2, nh);
319 if(rc < 0) goto corrupt;
320 printf(" %s", format_address(nh));
321 }
322 }
323 break;
324
325 case MESSAGE_UPDATE: {
326 if(!vflag) {
327 printf(" update");
328 if(len < 1)
329 printf("/truncated");
330 else
331 printf("%s%s%s",
332 (message[3] & 0x80) ? "/prefix": "",
333 (message[3] & 0x40) ? "/id" : "",
334 (message[3] & 0x3f) ? "/unknown" : "");
335 } else {
336 u_short interval, seqno, metric;
337 u_char plen;
338 int rc;
339 u_char prefix[16];
340 printf("\n\tUpdate");
341 if(len < 10) goto corrupt;
342 plen = message[4] + (message[2] == 1 ? 96 : 0);
343 rc = network_prefix(message[2], message[4], message[5],
344 message + 12,
345 message[2] == 1 ? v4_prefix : v6_prefix,
346 len - 10, prefix);
347 if(rc < 0) goto corrupt;
348 interval = EXTRACT_16BITS(message + 6);
349 seqno = EXTRACT_16BITS(message + 8);
350 metric = EXTRACT_16BITS(message + 10);
351 printf("%s%s%s %s metric %u seqno %u interval %u",
352 (message[3] & 0x80) ? "/prefix": "",
353 (message[3] & 0x40) ? "/id" : "",
354 (message[3] & 0x3f) ? "/unknown" : "",
355 format_prefix(prefix, plen),
356 metric, seqno, interval);
357 if(message[3] & 0x80) {
358 if(message[2] == 1)
359 memcpy(v4_prefix, prefix, 16);
360 else
361 memcpy(v6_prefix, prefix, 16);
362 }
363 }
364 }
365 break;
366
367 case MESSAGE_REQUEST: {
368 if(!vflag)
369 printf(" request");
370 else {
371 int rc;
372 u_char prefix[16], plen;
373 printf("\n\tRequest ");
374 if(len < 2) goto corrupt;
375 plen = message[3] + (message[2] == 1 ? 96 : 0);
376 rc = network_prefix(message[2], message[3], 0,
377 message + 4, NULL, len - 2, prefix);
378 if(rc < 0) goto corrupt;
379 plen = message[3] + (message[2] == 1 ? 96 : 0);
380 printf("for %s",
381 message[2] == 0 ? "any" : format_prefix(prefix, plen));
382 }
383 }
384 break;
385
386 case MESSAGE_MH_REQUEST : {
387 if(!vflag)
388 printf(" mh-request");
389 else {
390 int rc;
391 u_short seqno;
392 u_char prefix[16], plen;
393 printf("\n\tMH-Request ");
394 if(len < 14) goto corrupt;
395 seqno = EXTRACT_16BITS(message + 4);
396 rc = network_prefix(message[2], message[3], 0,
397 message + 16, NULL, len - 14, prefix);
398 if(rc < 0) goto corrupt;
399 plen = message[3] + (message[2] == 1 ? 96 : 0);
400 printf("(%u hops) for %s seqno %u id %s",
401 message[6], format_prefix(prefix, plen),
402 seqno, format_id(message + 8));
403 }
404 }
405 break;
406 case MESSAGE_TSPC :
407 if(!vflag)
408 printf(" tspc");
409 else {
410 printf("\n\tTS/PC ");
411 if(len < 6) goto corrupt;
412 printf("timestamp %u packetcounter %u", EXTRACT_32BITS (message + 4),
413 EXTRACT_16BITS(message + 2));
414 }
415 break;
416 case MESSAGE_HMAC : {
417 if(!vflag)
418 printf(" hmac");
419 else {
420 unsigned j;
421 printf("\n\tHMAC ");
422 if(len < 18) goto corrupt;
423 printf("key-id %u digest-%u ", EXTRACT_16BITS(message + 2), len - 2);
424 for (j = 0; j < len - 2; j++)
425 printf ("%02X", message[4 + j]);
426 }
427 }
428 break;
429 default:
430 if(!vflag)
431 printf(" unknown");
432 else
433 printf("\n\tUnknown message type %d", type);
434 }
435 i += len + 2;
436 }
437 return;
438
439 trunc:
440 printf(" [|babel]");
441 return;
442
443 corrupt:
444 printf(" (corrupt)");
445 return;
446 }