hammer.8: Fix typo.
[dragonfly.git] / contrib / tcpdump-3.9 / print-olsr.c
blob174a51eac9437ff1f18d1d6d61b121c5d3a03096
1 /*
2 * Copyright (c) 1998-2007 The TCPDUMP project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code
6 * distributions retain the above copyright notice and this paragraph
7 * in its entirety, and (2) distributions including binary code include
8 * the above copyright notice and this paragraph in its entirety in
9 * the documentation or other materials provided with the distribution.
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
15 * Optimized Link State Protocl (OLSR) as per rfc3626
17 * Original code by Hannes Gredler <hannes@juniper.net>
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <tcpdump-stdinc.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "interface.h"
30 #include "addrtoname.h"
31 #include "extract.h"
32 #include "ip.h"
35 * RFC 3626 common header
37 * 0 1 2 3
38 * 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
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | Packet Length | Packet Sequence Number |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Message Type | Vtime | Message Size |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Originator Address |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Time To Live | Hop Count | Message Sequence Number |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | |
49 * : MESSAGE :
50 * | |
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * | Message Type | Vtime | Message Size |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | Originator Address |
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * | Time To Live | Hop Count | Message Sequence Number |
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * | |
59 * : MESSAGE :
60 * | |
61 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 * : :
65 struct olsr_common {
66 u_int8_t packet_len[2];
67 u_int8_t packet_seq[2];
70 #define OLSR_HELLO_MSG 1 /* rfc3626 */
71 #define OLSR_TC_MSG 2 /* rfc3626 */
72 #define OLSR_MID_MSG 3 /* rfc3626 */
73 #define OLSR_HNA_MSG 4 /* rfc3626 */
74 #define OLSR_POWERINFO_MSG 128
75 #define OLSR_NAMESERVICE_MSG 130
76 #define OLSR_HELLO_LQ_MSG 201 /* LQ extensions olsr.org */
77 #define OLSR_TC_LQ_MSG 202 /* LQ extensions olsr.org */
79 static struct tok olsr_msg_values[] = {
80 { OLSR_HELLO_MSG, "Hello" },
81 { OLSR_TC_MSG, "TC" },
82 { OLSR_MID_MSG, "MID" },
83 { OLSR_HNA_MSG, "HNA" },
84 { OLSR_POWERINFO_MSG, "Powerinfo" },
85 { OLSR_NAMESERVICE_MSG, "Nameservice" },
86 { OLSR_HELLO_LQ_MSG, "Hello-LQ" },
87 { OLSR_TC_LQ_MSG, "TC-LQ" },
88 { 0, NULL}
91 struct olsr_msg {
92 u_int8_t msg_type;
93 u_int8_t vtime;
94 u_int8_t msg_len[2];
95 u_int8_t originator[4];
96 u_int8_t ttl;
97 u_int8_t hopcount;
98 u_int8_t msg_seq[2];
101 struct olsr_hello {
102 u_int8_t res[2];
103 u_int8_t htime;
104 u_int8_t will;
107 struct olsr_hello_link {
108 u_int8_t link_code;
109 u_int8_t res;
110 u_int8_t len[2];
113 struct olsr_tc {
114 u_int8_t ans_seq[2];
115 u_int8_t res[2];
118 struct olsr_hna {
119 u_int8_t network[4];
120 u_int8_t mask[4];
124 #define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
125 #define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
127 static struct tok olsr_link_type_values[] = {
128 { 0, "Unspecified" },
129 { 1, "Asymmetric" },
130 { 2, "Symmetric" },
131 { 3, "Lost" },
132 { 0, NULL}
135 static struct tok olsr_neighbor_type_values[] = {
136 { 0, "Not-Neighbor" },
137 { 1, "Symmetric" },
138 { 2, "Symmetric-MPR" },
139 { 0, NULL}
142 struct olsr_lq_neighbor {
143 u_int8_t neighbor[4];
144 u_int8_t link_quality;
145 u_int8_t neighbor_link_quality;
146 u_int8_t res[2];
150 * macro to convert the 8-bit mantissa/exponent to a double float
151 * taken from olsr.org.
153 #define VTIME_SCALE_FACTOR 0.0625
154 #define ME_TO_DOUBLE(me) \
155 (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
158 * print a neighbor list with LQ extensions.
160 static void
161 olsr_print_lq_neighbor (const u_char *msg_data, u_int hello_len)
163 struct olsr_lq_neighbor *lq_neighbor;
165 while (hello_len >= sizeof(struct olsr_lq_neighbor)) {
167 lq_neighbor = (struct olsr_lq_neighbor *)msg_data;
169 printf("\n\t neighbor %s, link-quality %.2lf%%"
170 ", neighbor-link-quality %.2lf%%",
171 ipaddr_string(lq_neighbor->neighbor),
172 ((double)lq_neighbor->link_quality/2.55),
173 ((double)lq_neighbor->neighbor_link_quality/2.55));
175 msg_data += sizeof(struct olsr_lq_neighbor);
176 hello_len -= sizeof(struct olsr_lq_neighbor);
181 * print a neighbor list.
183 static void
184 olsr_print_neighbor (const u_char *msg_data, u_int hello_len)
186 int neighbor;
188 printf("\n\t neighbor\n\t\t");
189 neighbor = 1;
191 while (hello_len >= sizeof(struct in_addr)) {
193 /* print 4 neighbors per line */
195 printf("%s%s", ipaddr_string(msg_data),
196 neighbor % 4 == 0 ? "\n\t\t" : " ");
198 msg_data += sizeof(struct in_addr);
199 hello_len -= sizeof(struct in_addr);
204 void
205 olsr_print (const u_char *pptr, u_int length)
207 union {
208 const struct olsr_common *common;
209 const struct olsr_msg *msg;
210 const struct olsr_hello *hello;
211 const struct olsr_hello_link *hello_link;
212 const struct olsr_lq_neighbor *lq_neighbor;
213 const struct olsr_tc *tc;
214 const struct olsr_hna *hna;
215 } ptr;
217 u_int msg_type, msg_len, msg_tlen, hello_len, prefix;
218 u_int8_t link_type, neighbor_type;
219 const u_char *tptr, *msg_data;
221 tptr = pptr;
223 if (length < sizeof(struct olsr_common)) {
224 goto trunc;
227 if (!TTEST2(*tptr, sizeof(struct olsr_common))) {
228 goto trunc;
231 ptr.common = (struct olsr_common *)tptr;
232 length = MIN(length, EXTRACT_16BITS(ptr.common->packet_len));
234 printf("OLSR, seq 0x%04x, length %u",
235 EXTRACT_16BITS(ptr.common->packet_seq),
236 length);
238 tptr += sizeof(struct olsr_common);
241 * In non-verbose mode, just print version.
243 if (vflag < 1) {
244 return;
247 while (tptr < (pptr+length)) {
249 if (!TTEST2(*tptr, sizeof(struct olsr_msg)))
250 goto trunc;
252 ptr.msg = (struct olsr_msg *)tptr;
254 msg_type = ptr.msg->msg_type;
255 msg_len = EXTRACT_16BITS(ptr.msg->msg_len);
257 /* infinite loop check */
258 if (msg_type == 0 || msg_len == 0) {
259 return;
262 printf("\n\t%s Message (%u), originator %s, ttl %u, hop %u"
263 "\n\t vtime %.3lfs, msg-seq 0x%04x, length %u",
264 tok2str(olsr_msg_values, "Unknown", msg_type),
265 msg_type, ipaddr_string(ptr.msg->originator),
266 ptr.msg->ttl,
267 ptr.msg->hopcount,
268 ME_TO_DOUBLE(ptr.msg->vtime),
269 EXTRACT_16BITS(ptr.msg->msg_seq),
270 msg_len);
272 msg_tlen = msg_len - sizeof(struct olsr_msg);
273 msg_data = tptr + sizeof(struct olsr_msg);
275 switch (msg_type) {
276 case OLSR_HELLO_MSG:
277 case OLSR_HELLO_LQ_MSG:
278 if (!TTEST2(*msg_data, sizeof(struct olsr_hello)))
279 goto trunc;
281 ptr.hello = (struct olsr_hello *)msg_data;
282 printf("\n\t hello-time %.3lfs, MPR willingness %u",
283 ME_TO_DOUBLE(ptr.hello->htime), ptr.hello->will);
284 msg_data += sizeof(struct olsr_hello);
285 msg_tlen -= sizeof(struct olsr_hello);
287 while (msg_tlen >= sizeof(struct olsr_hello_link)) {
290 * link-type.
292 if (!TTEST2(*msg_data, sizeof(struct olsr_hello_link)))
293 goto trunc;
295 ptr.hello_link = (struct olsr_hello_link *)msg_data;
297 hello_len = EXTRACT_16BITS(ptr.hello_link->len);
298 link_type = OLSR_EXTRACT_LINK_TYPE(ptr.hello_link->link_code);
299 neighbor_type = OLSR_EXTRACT_NEIGHBOR_TYPE(ptr.hello_link->link_code);
301 printf("\n\t link-type %s, neighbor-type %s, len %u",
302 tok2str(olsr_link_type_values, "Unknown", link_type),
303 tok2str(olsr_neighbor_type_values, "Unknown", neighbor_type),
304 hello_len);
306 msg_data += sizeof(struct olsr_hello_link);
307 msg_tlen -= sizeof(struct olsr_hello_link);
308 hello_len -= sizeof(struct olsr_hello_link);
310 if (msg_type == OLSR_HELLO_MSG) {
311 olsr_print_neighbor(msg_data, hello_len);
312 } else {
313 olsr_print_lq_neighbor(msg_data, hello_len);
316 msg_data += hello_len;
317 msg_tlen -= hello_len;
319 break;
321 case OLSR_TC_MSG:
322 case OLSR_TC_LQ_MSG:
323 if (!TTEST2(*msg_data, sizeof(struct olsr_tc)))
324 goto trunc;
326 ptr.tc = (struct olsr_tc *)msg_data;
327 printf("\n\t advertised neighbor seq 0x%04x",
328 EXTRACT_16BITS(ptr.tc->ans_seq));
329 msg_data += sizeof(struct olsr_tc);
330 msg_tlen -= sizeof(struct olsr_tc);
332 if (msg_type == OLSR_TC_MSG) {
333 olsr_print_neighbor(msg_data, msg_tlen);
334 } else {
335 olsr_print_lq_neighbor(msg_data, msg_tlen);
337 break;
339 case OLSR_MID_MSG:
340 if (!TTEST2(*msg_data, sizeof(struct in_addr)))
341 goto trunc;
343 while (msg_tlen >= sizeof(struct in_addr)) {
344 printf("\n\t interface address %s", ipaddr_string(msg_data));
345 msg_data += sizeof(struct in_addr);
346 msg_tlen -= sizeof(struct in_addr);
348 break;
350 case OLSR_HNA_MSG:
351 prefix = 1;
352 printf("\n\t advertised networks\n\t ");
353 while (msg_tlen >= sizeof(struct olsr_hna)) {
354 if (!TTEST2(*msg_data, sizeof(struct olsr_hna)))
355 goto trunc;
357 ptr.hna = (struct olsr_hna *)msg_data;
359 /* print 4 prefixes per line */
361 printf("%s/%u%s",
362 ipaddr_string(ptr.hna->network),
363 mask2plen(EXTRACT_32BITS(ptr.hna->mask)),
364 prefix % 4 == 0 ? "\n\t " : " ");
366 msg_data += sizeof(struct olsr_hna);
367 msg_tlen -= sizeof(struct olsr_hna);
368 prefix ++;
370 break;
373 * FIXME those are the defined messages that lack a decoder
374 * you are welcome to contribute code ;-)
377 case OLSR_POWERINFO_MSG:
378 case OLSR_NAMESERVICE_MSG:
379 default:
380 print_unknown_data(msg_data, "\n\t ", msg_tlen);
381 break;
383 tptr += msg_len;
386 return;
388 trunc:
389 printf("[|olsr]");
393 * Local Variables:
394 * c-style: whitesmith
395 * c-basic-offset: 4
396 * End: