Fix markup. Fix backslashes to surive roff.
[netbsd-mini2440.git] / dist / tcpdump / print-olsr.c
blob48b9e47c6c4367b2681a3c5239fda12167fd8e1d
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1998-2007 The TCPDUMP project
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code
8 * distributions retain the above copyright notice and this paragraph
9 * in its entirety, and (2) distributions including binary code include
10 * the above copyright notice and this paragraph in its entirety in
11 * the documentation or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
13 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
14 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 * FOR A PARTICULAR PURPOSE.
17 * Optimized Link State Protocl (OLSR) as per rfc3626
19 * Original code by Hannes Gredler <hannes@juniper.net>
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <tcpdump-stdinc.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include "interface.h"
32 #include "addrtoname.h"
33 #include "extract.h"
34 #include "ip.h"
37 * RFC 3626 common header
39 * 0 1 2 3
40 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Packet Length | Packet Sequence Number |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Message Type | Vtime | Message Size |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Originator Address |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | Time To Live | Hop Count | Message Sequence Number |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | |
51 * : MESSAGE :
52 * | |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | Message Type | Vtime | Message Size |
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * | Originator Address |
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * | Time To Live | Hop Count | Message Sequence Number |
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 * | |
61 * : MESSAGE :
62 * | |
63 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 * : :
67 struct olsr_common {
68 u_int8_t packet_len[2];
69 u_int8_t packet_seq[2];
72 #define OLSR_HELLO_MSG 1 /* rfc3626 */
73 #define OLSR_TC_MSG 2 /* rfc3626 */
74 #define OLSR_MID_MSG 3 /* rfc3626 */
75 #define OLSR_HNA_MSG 4 /* rfc3626 */
76 #define OLSR_POWERINFO_MSG 128
77 #define OLSR_NAMESERVICE_MSG 130
78 #define OLSR_HELLO_LQ_MSG 201 /* LQ extensions olsr.org */
79 #define OLSR_TC_LQ_MSG 202 /* LQ extensions olsr.org */
81 static struct tok olsr_msg_values[] = {
82 { OLSR_HELLO_MSG, "Hello" },
83 { OLSR_TC_MSG, "TC" },
84 { OLSR_MID_MSG, "MID" },
85 { OLSR_HNA_MSG, "HNA" },
86 { OLSR_POWERINFO_MSG, "Powerinfo" },
87 { OLSR_NAMESERVICE_MSG, "Nameservice" },
88 { OLSR_HELLO_LQ_MSG, "Hello-LQ" },
89 { OLSR_TC_LQ_MSG, "TC-LQ" },
90 { 0, NULL}
93 struct olsr_msg {
94 u_int8_t msg_type;
95 u_int8_t vtime;
96 u_int8_t msg_len[2];
97 u_int8_t originator[4];
98 u_int8_t ttl;
99 u_int8_t hopcount;
100 u_int8_t msg_seq[2];
103 struct olsr_hello {
104 u_int8_t res[2];
105 u_int8_t htime;
106 u_int8_t will;
109 struct olsr_hello_link {
110 u_int8_t link_code;
111 u_int8_t res;
112 u_int8_t len[2];
115 struct olsr_tc {
116 u_int8_t ans_seq[2];
117 u_int8_t res[2];
120 struct olsr_hna {
121 u_int8_t network[4];
122 u_int8_t mask[4];
126 #define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
127 #define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
129 static struct tok olsr_link_type_values[] = {
130 { 0, "Unspecified" },
131 { 1, "Asymmetric" },
132 { 2, "Symmetric" },
133 { 3, "Lost" },
134 { 0, NULL}
137 static struct tok olsr_neighbor_type_values[] = {
138 { 0, "Not-Neighbor" },
139 { 1, "Symmetric" },
140 { 2, "Symmetric-MPR" },
141 { 0, NULL}
144 struct olsr_lq_neighbor {
145 u_int8_t neighbor[4];
146 u_int8_t link_quality;
147 u_int8_t neighbor_link_quality;
148 u_int8_t res[2];
152 * macro to convert the 8-bit mantissa/exponent to a double float
153 * taken from olsr.org.
155 #define VTIME_SCALE_FACTOR 0.0625
156 #define ME_TO_DOUBLE(me) \
157 (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
160 * print a neighbor list with LQ extensions.
162 static void
163 olsr_print_lq_neighbor (const u_char *msg_data, u_int hello_len)
165 struct olsr_lq_neighbor *lq_neighbor;
167 while (hello_len >= sizeof(struct olsr_lq_neighbor)) {
169 lq_neighbor = (struct olsr_lq_neighbor *)msg_data;
171 printf("\n\t neighbor %s, link-quality %.2lf%%"
172 ", neighbor-link-quality %.2lf%%",
173 ipaddr_string(lq_neighbor->neighbor),
174 ((double)lq_neighbor->link_quality/2.55),
175 ((double)lq_neighbor->neighbor_link_quality/2.55));
177 msg_data += sizeof(struct olsr_lq_neighbor);
178 hello_len -= sizeof(struct olsr_lq_neighbor);
183 * print a neighbor list.
185 static void
186 olsr_print_neighbor (const u_char *msg_data, u_int hello_len)
188 int neighbor;
190 printf("\n\t neighbor\n\t\t");
191 neighbor = 1;
193 while (hello_len >= sizeof(struct in_addr)) {
195 /* print 4 neighbors per line */
197 printf("%s%s", ipaddr_string(msg_data),
198 neighbor % 4 == 0 ? "\n\t\t" : " ");
200 msg_data += sizeof(struct in_addr);
201 hello_len -= sizeof(struct in_addr);
206 void
207 olsr_print (const u_char *pptr, u_int length)
209 union {
210 const struct olsr_common *common;
211 const struct olsr_msg *msg;
212 const struct olsr_hello *hello;
213 const struct olsr_hello_link *hello_link;
214 const struct olsr_lq_neighbor *lq_neighbor;
215 const struct olsr_tc *tc;
216 const struct olsr_hna *hna;
217 } ptr;
219 u_int msg_type, msg_len, msg_tlen, hello_len, prefix;
220 u_int8_t link_type, neighbor_type;
221 const u_char *tptr, *msg_data;
223 tptr = pptr;
225 if (length < sizeof(struct olsr_common)) {
226 goto trunc;
229 if (!TTEST2(*tptr, sizeof(struct olsr_common))) {
230 goto trunc;
233 ptr.common = (struct olsr_common *)tptr;
234 length = MIN(length, EXTRACT_16BITS(ptr.common->packet_len));
236 printf("OLSR, seq 0x%04x, length %u",
237 EXTRACT_16BITS(ptr.common->packet_seq),
238 length);
240 tptr += sizeof(struct olsr_common);
243 * In non-verbose mode, just print version.
245 if (vflag < 1) {
246 return;
249 while (tptr < (pptr+length)) {
251 if (!TTEST2(*tptr, sizeof(struct olsr_msg)))
252 goto trunc;
254 ptr.msg = (struct olsr_msg *)tptr;
256 msg_type = ptr.msg->msg_type;
257 msg_len = EXTRACT_16BITS(ptr.msg->msg_len);
259 /* infinite loop check */
260 if (msg_type == 0 || msg_len == 0) {
261 return;
264 printf("\n\t%s Message (%u), originator %s, ttl %u, hop %u"
265 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u",
266 tok2str(olsr_msg_values, "Unknown", msg_type),
267 msg_type, ipaddr_string(ptr.msg->originator),
268 ptr.msg->ttl,
269 ptr.msg->hopcount,
270 ME_TO_DOUBLE(ptr.msg->vtime),
271 EXTRACT_16BITS(ptr.msg->msg_seq),
272 msg_len);
274 msg_tlen = msg_len - sizeof(struct olsr_msg);
275 msg_data = tptr + sizeof(struct olsr_msg);
277 switch (msg_type) {
278 case OLSR_HELLO_MSG:
279 case OLSR_HELLO_LQ_MSG:
280 if (!TTEST2(*msg_data, sizeof(struct olsr_hello)))
281 goto trunc;
283 ptr.hello = (struct olsr_hello *)msg_data;
284 printf("\n\t hello-time %.3lfs, MPR willingness %u",
285 ME_TO_DOUBLE(ptr.hello->htime), ptr.hello->will);
286 msg_data += sizeof(struct olsr_hello);
287 msg_tlen -= sizeof(struct olsr_hello);
289 while (msg_tlen >= sizeof(struct olsr_hello_link)) {
292 * link-type.
294 if (!TTEST2(*msg_data, sizeof(struct olsr_hello_link)))
295 goto trunc;
297 ptr.hello_link = (struct olsr_hello_link *)msg_data;
299 hello_len = EXTRACT_16BITS(ptr.hello_link->len);
300 link_type = OLSR_EXTRACT_LINK_TYPE(ptr.hello_link->link_code);
301 neighbor_type = OLSR_EXTRACT_NEIGHBOR_TYPE(ptr.hello_link->link_code);
303 printf("\n\t link-type %s, neighbor-type %s, len %u",
304 tok2str(olsr_link_type_values, "Unknown", link_type),
305 tok2str(olsr_neighbor_type_values, "Unknown", neighbor_type),
306 hello_len);
308 msg_data += sizeof(struct olsr_hello_link);
309 msg_tlen -= sizeof(struct olsr_hello_link);
310 hello_len -= sizeof(struct olsr_hello_link);
312 if (msg_type == OLSR_HELLO_MSG) {
313 olsr_print_neighbor(msg_data, hello_len);
314 } else {
315 olsr_print_lq_neighbor(msg_data, hello_len);
318 msg_data += hello_len;
319 msg_tlen -= hello_len;
321 break;
323 case OLSR_TC_MSG:
324 case OLSR_TC_LQ_MSG:
325 if (!TTEST2(*msg_data, sizeof(struct olsr_tc)))
326 goto trunc;
328 ptr.tc = (struct olsr_tc *)msg_data;
329 printf("\n\t advertised neighbor seq 0x%04x",
330 EXTRACT_16BITS(ptr.tc->ans_seq));
331 msg_data += sizeof(struct olsr_tc);
332 msg_tlen -= sizeof(struct olsr_tc);
334 if (msg_type == OLSR_TC_MSG) {
335 olsr_print_neighbor(msg_data, msg_tlen);
336 } else {
337 olsr_print_lq_neighbor(msg_data, msg_tlen);
339 break;
341 case OLSR_MID_MSG:
342 if (!TTEST2(*msg_data, sizeof(struct in_addr)))
343 goto trunc;
345 while (msg_tlen >= sizeof(struct in_addr)) {
346 printf("\n\t interface address %s", ipaddr_string(msg_data));
347 msg_data += sizeof(struct in_addr);
348 msg_tlen -= sizeof(struct in_addr);
350 break;
352 case OLSR_HNA_MSG:
353 prefix = 1;
354 printf("\n\t advertised networks\n\t ");
355 while (msg_tlen >= sizeof(struct olsr_hna)) {
356 if (!TTEST2(*msg_data, sizeof(struct olsr_hna)))
357 goto trunc;
359 ptr.hna = (struct olsr_hna *)msg_data;
361 /* print 4 prefixes per line */
363 printf("%s/%u%s",
364 ipaddr_string(ptr.hna->network),
365 mask2plen(EXTRACT_32BITS(ptr.hna->mask)),
366 prefix % 4 == 0 ? "\n\t " : " ");
368 msg_data += sizeof(struct olsr_hna);
369 msg_tlen -= sizeof(struct olsr_hna);
370 prefix ++;
372 break;
375 * FIXME those are the defined messages that lack a decoder
376 * you are welcome to contribute code ;-)
379 case OLSR_POWERINFO_MSG:
380 case OLSR_NAMESERVICE_MSG:
381 default:
382 print_unknown_data(msg_data, "\n\t ", msg_tlen);
383 break;
385 tptr += msg_len;
388 return;
390 trunc:
391 printf("[|olsr]");
395 * Local Variables:
396 * c-style: whitesmith
397 * c-basic-offset: 4
398 * End: