blob: e67988dfd8cb65177c4cab5948ee0deb269dfc45 [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
2 * Copyright (c) 1998-2007 The TCPDUMP project
JP Abgrall53f17a92014-02-12 14:02:41 -08003 * Copyright (c) 2009 Florian Forster
The Android Open Source Project2949f582009-03-03 19:30:46 -08004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code
7 * distributions retain the above copyright notice and this paragraph
8 * in its entirety, and (2) distributions including binary code include
9 * the above copyright notice and this paragraph in its entirety in
10 * the documentation or other materials provided with the distribution.
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
12 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
13 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 * FOR A PARTICULAR PURPOSE.
15 *
Elliott Hughescec480a2017-12-19 16:54:57 -080016 * Original code by Hannes Gredler <hannes@gredler.at>
JP Abgrall53f17a92014-02-12 14:02:41 -080017 * IPv6 additions by Florian Forster <octo at verplant.org>
The Android Open Source Project2949f582009-03-03 19:30:46 -080018 */
19
Elliott Hughese2e3bd12017-05-15 10:59:29 -070020/* \summary: Optimized Link State Routing Protocol (OLSR) printer */
21
22/* specification: RFC 3626 */
23
The Android Open Source Project2949f582009-03-03 19:30:46 -080024#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
Elliott Hughese2e3bd12017-05-15 10:59:29 -070028#include <netdissect-stdinc.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080029
Elliott Hughese2e3bd12017-05-15 10:59:29 -070030#include "netdissect.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080031#include "addrtoname.h"
JP Abgrall53f17a92014-02-12 14:02:41 -080032#include "extract.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080033
34/*
35 * RFC 3626 common header
36 *
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 * : :
63 */
64
65struct olsr_common {
Elliott Hughes892a68b2015-10-19 14:43:53 -070066 uint8_t packet_len[2];
67 uint8_t packet_seq[2];
The Android Open Source Project2949f582009-03-03 19:30:46 -080068};
69
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 */
78
JP Abgrall53f17a92014-02-12 14:02:41 -080079static const struct tok olsr_msg_values[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080080 { 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}
89};
90
JP Abgrall53f17a92014-02-12 14:02:41 -080091struct olsr_msg4 {
Elliott Hughes892a68b2015-10-19 14:43:53 -070092 uint8_t msg_type;
93 uint8_t vtime;
94 uint8_t msg_len[2];
95 uint8_t originator[4];
96 uint8_t ttl;
97 uint8_t hopcount;
98 uint8_t msg_seq[2];
The Android Open Source Project2949f582009-03-03 19:30:46 -080099};
100
JP Abgrall53f17a92014-02-12 14:02:41 -0800101struct olsr_msg6 {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700102 uint8_t msg_type;
103 uint8_t vtime;
104 uint8_t msg_len[2];
105 uint8_t originator[16];
106 uint8_t ttl;
107 uint8_t hopcount;
108 uint8_t msg_seq[2];
JP Abgrall53f17a92014-02-12 14:02:41 -0800109};
110
The Android Open Source Project2949f582009-03-03 19:30:46 -0800111struct olsr_hello {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700112 uint8_t res[2];
113 uint8_t htime;
114 uint8_t will;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800115};
116
117struct olsr_hello_link {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700118 uint8_t link_code;
119 uint8_t res;
120 uint8_t len[2];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800121};
122
123struct olsr_tc {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700124 uint8_t ans_seq[2];
125 uint8_t res[2];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800126};
127
JP Abgrall53f17a92014-02-12 14:02:41 -0800128struct olsr_hna4 {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700129 uint8_t network[4];
130 uint8_t mask[4];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800131};
132
JP Abgrall53f17a92014-02-12 14:02:41 -0800133struct olsr_hna6 {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700134 uint8_t network[16];
135 uint8_t mask[16];
JP Abgrall53f17a92014-02-12 14:02:41 -0800136};
137
The Android Open Source Project2949f582009-03-03 19:30:46 -0800138
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700139/** gateway HNA flags */
140enum gateway_hna_flags {
141 GW_HNA_FLAG_LINKSPEED = 1 << 0,
142 GW_HNA_FLAG_IPV4 = 1 << 1,
143 GW_HNA_FLAG_IPV4_NAT = 1 << 2,
144 GW_HNA_FLAG_IPV6 = 1 << 3,
145 GW_HNA_FLAG_IPV6PREFIX = 1 << 4
146};
147
148/** gateway HNA field byte offsets in the netmask field of the HNA */
149enum gateway_hna_fields {
150 GW_HNA_PAD = 0,
151 GW_HNA_FLAGS = 1,
152 GW_HNA_UPLINK = 2,
153 GW_HNA_DOWNLINK = 3,
154 GW_HNA_V6PREFIXLEN = 4,
155 GW_HNA_V6PREFIX = 5
156};
157
158
The Android Open Source Project2949f582009-03-03 19:30:46 -0800159#define OLSR_EXTRACT_LINK_TYPE(link_code) (link_code & 0x3)
160#define OLSR_EXTRACT_NEIGHBOR_TYPE(link_code) (link_code >> 2)
161
JP Abgrall53f17a92014-02-12 14:02:41 -0800162static const struct tok olsr_link_type_values[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800163 { 0, "Unspecified" },
164 { 1, "Asymmetric" },
165 { 2, "Symmetric" },
166 { 3, "Lost" },
167 { 0, NULL}
168};
169
JP Abgrall53f17a92014-02-12 14:02:41 -0800170static const struct tok olsr_neighbor_type_values[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800171 { 0, "Not-Neighbor" },
172 { 1, "Symmetric" },
173 { 2, "Symmetric-MPR" },
174 { 0, NULL}
175};
176
JP Abgrall53f17a92014-02-12 14:02:41 -0800177struct olsr_lq_neighbor4 {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700178 uint8_t neighbor[4];
179 uint8_t link_quality;
180 uint8_t neighbor_link_quality;
181 uint8_t res[2];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800182};
183
JP Abgrall53f17a92014-02-12 14:02:41 -0800184struct olsr_lq_neighbor6 {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700185 uint8_t neighbor[16];
186 uint8_t link_quality;
187 uint8_t neighbor_link_quality;
188 uint8_t res[2];
JP Abgrall53f17a92014-02-12 14:02:41 -0800189};
190
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700191#define MAX_SMARTGW_SPEED 320000000
192
193/**
194 * Convert an encoded 1 byte transport value (5 bits mantissa, 3 bits exponent)
195 * to an uplink/downlink speed value
196 *
197 * @param value the encoded 1 byte transport value
198 * @return the uplink/downlink speed value (in kbit/s)
199 */
200static uint32_t deserialize_gw_speed(uint8_t value) {
201 uint32_t speed;
202 uint32_t exp;
203
204 if (!value) {
205 return 0;
206 }
207
208 if (value == UINT8_MAX) {
209 /* maximum value: also return maximum value */
210 return MAX_SMARTGW_SPEED;
211 }
212
213 speed = (value >> 3) + 1;
214 exp = value & 7;
215
216 while (exp-- > 0) {
217 speed *= 10;
218 }
219 return speed;
220}
221
The Android Open Source Project2949f582009-03-03 19:30:46 -0800222/*
223 * macro to convert the 8-bit mantissa/exponent to a double float
224 * taken from olsr.org.
225 */
226#define VTIME_SCALE_FACTOR 0.0625
227#define ME_TO_DOUBLE(me) \
228 (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
229
230/*
231 * print a neighbor list with LQ extensions.
232 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700233static int
234olsr_print_lq_neighbor4(netdissect_options *ndo,
235 const u_char *msg_data, u_int hello_len)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800236{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700237 const struct olsr_lq_neighbor4 *lq_neighbor;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800238
JP Abgrall53f17a92014-02-12 14:02:41 -0800239 while (hello_len >= sizeof(struct olsr_lq_neighbor4)) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800240
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700241 lq_neighbor = (const struct olsr_lq_neighbor4 *)msg_data;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700242 if (!ND_TTEST(*lq_neighbor))
243 return (-1);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800244
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700245 ND_PRINT((ndo, "\n\t neighbor %s, link-quality %.2f%%"
246 ", neighbor-link-quality %.2f%%",
Elliott Hughes892a68b2015-10-19 14:43:53 -0700247 ipaddr_string(ndo, lq_neighbor->neighbor),
The Android Open Source Project2949f582009-03-03 19:30:46 -0800248 ((double)lq_neighbor->link_quality/2.55),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700249 ((double)lq_neighbor->neighbor_link_quality/2.55)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800250
JP Abgrall53f17a92014-02-12 14:02:41 -0800251 msg_data += sizeof(struct olsr_lq_neighbor4);
252 hello_len -= sizeof(struct olsr_lq_neighbor4);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800253 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700254 return (0);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800255}
256
Elliott Hughes892a68b2015-10-19 14:43:53 -0700257static int
258olsr_print_lq_neighbor6(netdissect_options *ndo,
259 const u_char *msg_data, u_int hello_len)
JP Abgrall53f17a92014-02-12 14:02:41 -0800260{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700261 const struct olsr_lq_neighbor6 *lq_neighbor;
JP Abgrall53f17a92014-02-12 14:02:41 -0800262
263 while (hello_len >= sizeof(struct olsr_lq_neighbor6)) {
264
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700265 lq_neighbor = (const struct olsr_lq_neighbor6 *)msg_data;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700266 if (!ND_TTEST(*lq_neighbor))
267 return (-1);
JP Abgrall53f17a92014-02-12 14:02:41 -0800268
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700269 ND_PRINT((ndo, "\n\t neighbor %s, link-quality %.2f%%"
270 ", neighbor-link-quality %.2f%%",
Elliott Hughes892a68b2015-10-19 14:43:53 -0700271 ip6addr_string(ndo, lq_neighbor->neighbor),
JP Abgrall53f17a92014-02-12 14:02:41 -0800272 ((double)lq_neighbor->link_quality/2.55),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700273 ((double)lq_neighbor->neighbor_link_quality/2.55)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800274
275 msg_data += sizeof(struct olsr_lq_neighbor6);
276 hello_len -= sizeof(struct olsr_lq_neighbor6);
277 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700278 return (0);
JP Abgrall53f17a92014-02-12 14:02:41 -0800279}
JP Abgrall53f17a92014-02-12 14:02:41 -0800280
The Android Open Source Project2949f582009-03-03 19:30:46 -0800281/*
282 * print a neighbor list.
283 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700284static int
285olsr_print_neighbor(netdissect_options *ndo,
286 const u_char *msg_data, u_int hello_len)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800287{
288 int neighbor;
289
Elliott Hughes892a68b2015-10-19 14:43:53 -0700290 ND_PRINT((ndo, "\n\t neighbor\n\t\t"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800291 neighbor = 1;
292
293 while (hello_len >= sizeof(struct in_addr)) {
294
Elliott Hughes892a68b2015-10-19 14:43:53 -0700295 if (!ND_TTEST2(*msg_data, sizeof(struct in_addr)))
296 return (-1);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800297 /* print 4 neighbors per line */
298
Elliott Hughes892a68b2015-10-19 14:43:53 -0700299 ND_PRINT((ndo, "%s%s", ipaddr_string(ndo, msg_data),
300 neighbor % 4 == 0 ? "\n\t\t" : " "));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800301
302 msg_data += sizeof(struct in_addr);
303 hello_len -= sizeof(struct in_addr);
304 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700305 return (0);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800306}
307
308
309void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700310olsr_print(netdissect_options *ndo,
311 const u_char *pptr, u_int length, int is_ipv6)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800312{
313 union {
314 const struct olsr_common *common;
JP Abgrall53f17a92014-02-12 14:02:41 -0800315 const struct olsr_msg4 *msg4;
316 const struct olsr_msg6 *msg6;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800317 const struct olsr_hello *hello;
318 const struct olsr_hello_link *hello_link;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800319 const struct olsr_tc *tc;
JP Abgrall53f17a92014-02-12 14:02:41 -0800320 const struct olsr_hna4 *hna;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800321 } ptr;
322
JP Abgrall53f17a92014-02-12 14:02:41 -0800323 u_int msg_type, msg_len, msg_tlen, hello_len;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700324 uint16_t name_entry_type, name_entry_len;
JP Abgrall53f17a92014-02-12 14:02:41 -0800325 u_int name_entry_padding;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700326 uint8_t link_type, neighbor_type;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800327 const u_char *tptr, *msg_data;
328
JP Abgrall53f17a92014-02-12 14:02:41 -0800329 tptr = pptr;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800330
331 if (length < sizeof(struct olsr_common)) {
332 goto trunc;
333 }
334
Elliott Hughes892a68b2015-10-19 14:43:53 -0700335 ND_TCHECK2(*tptr, sizeof(struct olsr_common));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800336
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700337 ptr.common = (const struct olsr_common *)tptr;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700338 length = min(length, EXTRACT_16BITS(ptr.common->packet_len));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800339
Elliott Hughes892a68b2015-10-19 14:43:53 -0700340 ND_PRINT((ndo, "OLSRv%i, seq 0x%04x, length %u",
JP Abgrall53f17a92014-02-12 14:02:41 -0800341 (is_ipv6 == 0) ? 4 : 6,
342 EXTRACT_16BITS(ptr.common->packet_seq),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700343 length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800344
345 tptr += sizeof(struct olsr_common);
346
347 /*
348 * In non-verbose mode, just print version.
349 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700350 if (ndo->ndo_vflag < 1) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800351 return;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800352 }
353
354 while (tptr < (pptr+length)) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800355 union
356 {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700357 const struct olsr_msg4 *v4;
358 const struct olsr_msg6 *v6;
JP Abgrall53f17a92014-02-12 14:02:41 -0800359 } msgptr;
360 int msg_len_valid = 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800361
JP Abgrall53f17a92014-02-12 14:02:41 -0800362 if (is_ipv6)
363 {
Elliott Hughescec480a2017-12-19 16:54:57 -0800364 ND_TCHECK2(*tptr, sizeof(struct olsr_msg6));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700365 msgptr.v6 = (const struct olsr_msg6 *) tptr;
JP Abgrall53f17a92014-02-12 14:02:41 -0800366 msg_type = msgptr.v6->msg_type;
367 msg_len = EXTRACT_16BITS(msgptr.v6->msg_len);
368 if ((msg_len >= sizeof (struct olsr_msg6))
369 && (msg_len <= length))
370 msg_len_valid = 1;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800371
JP Abgrall53f17a92014-02-12 14:02:41 -0800372 /* infinite loop check */
373 if (msg_type == 0 || msg_len == 0) {
374 return;
375 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800376
Elliott Hughes892a68b2015-10-19 14:43:53 -0700377 ND_PRINT((ndo, "\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700378 "\n\t vtime %.3fs, msg-seq 0x%04x, length %u%s",
JP Abgrall53f17a92014-02-12 14:02:41 -0800379 tok2str(olsr_msg_values, "Unknown", msg_type),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700380 msg_type, ip6addr_string(ndo, msgptr.v6->originator),
JP Abgrall53f17a92014-02-12 14:02:41 -0800381 msgptr.v6->ttl,
382 msgptr.v6->hopcount,
383 ME_TO_DOUBLE(msgptr.v6->vtime),
384 EXTRACT_16BITS(msgptr.v6->msg_seq),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700385 msg_len, (msg_len_valid == 0) ? " (invalid)" : ""));
386 if (!msg_len_valid) {
387 return;
388 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800389
390 msg_tlen = msg_len - sizeof(struct olsr_msg6);
391 msg_data = tptr + sizeof(struct olsr_msg6);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800392 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800393 else /* (!is_ipv6) */
JP Abgrall53f17a92014-02-12 14:02:41 -0800394 {
Elliott Hughescec480a2017-12-19 16:54:57 -0800395 ND_TCHECK2(*tptr, sizeof(struct olsr_msg4));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700396 msgptr.v4 = (const struct olsr_msg4 *) tptr;
JP Abgrall53f17a92014-02-12 14:02:41 -0800397 msg_type = msgptr.v4->msg_type;
398 msg_len = EXTRACT_16BITS(msgptr.v4->msg_len);
399 if ((msg_len >= sizeof (struct olsr_msg4))
400 && (msg_len <= length))
401 msg_len_valid = 1;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800402
JP Abgrall53f17a92014-02-12 14:02:41 -0800403 /* infinite loop check */
404 if (msg_type == 0 || msg_len == 0) {
405 return;
406 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800407
Elliott Hughes892a68b2015-10-19 14:43:53 -0700408 ND_PRINT((ndo, "\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700409 "\n\t vtime %.3fs, msg-seq 0x%04x, length %u%s",
JP Abgrall53f17a92014-02-12 14:02:41 -0800410 tok2str(olsr_msg_values, "Unknown", msg_type),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700411 msg_type, ipaddr_string(ndo, msgptr.v4->originator),
JP Abgrall53f17a92014-02-12 14:02:41 -0800412 msgptr.v4->ttl,
413 msgptr.v4->hopcount,
414 ME_TO_DOUBLE(msgptr.v4->vtime),
415 EXTRACT_16BITS(msgptr.v4->msg_seq),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700416 msg_len, (msg_len_valid == 0) ? " (invalid)" : ""));
417 if (!msg_len_valid) {
418 return;
419 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800420
421 msg_tlen = msg_len - sizeof(struct olsr_msg4);
422 msg_data = tptr + sizeof(struct olsr_msg4);
423 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800424
425 switch (msg_type) {
426 case OLSR_HELLO_MSG:
427 case OLSR_HELLO_LQ_MSG:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700428 if (msg_tlen < sizeof(struct olsr_hello))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800429 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700430 ND_TCHECK2(*msg_data, sizeof(struct olsr_hello));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800431
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700432 ptr.hello = (const struct olsr_hello *)msg_data;
433 ND_PRINT((ndo, "\n\t hello-time %.3fs, MPR willingness %u",
Elliott Hughes892a68b2015-10-19 14:43:53 -0700434 ME_TO_DOUBLE(ptr.hello->htime), ptr.hello->will));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800435 msg_data += sizeof(struct olsr_hello);
436 msg_tlen -= sizeof(struct olsr_hello);
437
438 while (msg_tlen >= sizeof(struct olsr_hello_link)) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800439 int hello_len_valid = 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800440
441 /*
442 * link-type.
443 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700444 ND_TCHECK2(*msg_data, sizeof(struct olsr_hello_link));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800445
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700446 ptr.hello_link = (const struct olsr_hello_link *)msg_data;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800447
448 hello_len = EXTRACT_16BITS(ptr.hello_link->len);
449 link_type = OLSR_EXTRACT_LINK_TYPE(ptr.hello_link->link_code);
450 neighbor_type = OLSR_EXTRACT_NEIGHBOR_TYPE(ptr.hello_link->link_code);
451
JP Abgrall53f17a92014-02-12 14:02:41 -0800452 if ((hello_len <= msg_tlen)
453 && (hello_len >= sizeof(struct olsr_hello_link)))
454 hello_len_valid = 1;
455
Elliott Hughes892a68b2015-10-19 14:43:53 -0700456 ND_PRINT((ndo, "\n\t link-type %s, neighbor-type %s, len %u%s",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800457 tok2str(olsr_link_type_values, "Unknown", link_type),
458 tok2str(olsr_neighbor_type_values, "Unknown", neighbor_type),
JP Abgrall53f17a92014-02-12 14:02:41 -0800459 hello_len,
Elliott Hughes892a68b2015-10-19 14:43:53 -0700460 (hello_len_valid == 0) ? " (invalid)" : ""));
JP Abgrall53f17a92014-02-12 14:02:41 -0800461
462 if (hello_len_valid == 0)
463 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800464
465 msg_data += sizeof(struct olsr_hello_link);
466 msg_tlen -= sizeof(struct olsr_hello_link);
467 hello_len -= sizeof(struct olsr_hello_link);
468
Elliott Hughes892a68b2015-10-19 14:43:53 -0700469 ND_TCHECK2(*msg_data, hello_len);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800470 if (msg_type == OLSR_HELLO_MSG) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700471 if (olsr_print_neighbor(ndo, msg_data, hello_len) == -1)
472 goto trunc;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800473 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700474 if (is_ipv6) {
475 if (olsr_print_lq_neighbor6(ndo, msg_data, hello_len) == -1)
476 goto trunc;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700477 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700478 if (olsr_print_lq_neighbor4(ndo, msg_data, hello_len) == -1)
479 goto trunc;
480 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800481 }
482
483 msg_data += hello_len;
484 msg_tlen -= hello_len;
485 }
486 break;
487
488 case OLSR_TC_MSG:
489 case OLSR_TC_LQ_MSG:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700490 if (msg_tlen < sizeof(struct olsr_tc))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800491 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700492 ND_TCHECK2(*msg_data, sizeof(struct olsr_tc));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800493
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700494 ptr.tc = (const struct olsr_tc *)msg_data;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700495 ND_PRINT((ndo, "\n\t advertised neighbor seq 0x%04x",
496 EXTRACT_16BITS(ptr.tc->ans_seq)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800497 msg_data += sizeof(struct olsr_tc);
498 msg_tlen -= sizeof(struct olsr_tc);
499
500 if (msg_type == OLSR_TC_MSG) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700501 if (olsr_print_neighbor(ndo, msg_data, msg_tlen) == -1)
502 goto trunc;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800503 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700504 if (is_ipv6) {
505 if (olsr_print_lq_neighbor6(ndo, msg_data, msg_tlen) == -1)
506 goto trunc;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700507 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700508 if (olsr_print_lq_neighbor4(ndo, msg_data, msg_tlen) == -1)
509 goto trunc;
510 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800511 }
512 break;
513
514 case OLSR_MID_MSG:
JP Abgrall53f17a92014-02-12 14:02:41 -0800515 {
516 size_t addr_size = sizeof(struct in_addr);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800517
JP Abgrall53f17a92014-02-12 14:02:41 -0800518 if (is_ipv6)
519 addr_size = sizeof(struct in6_addr);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800520
JP Abgrall53f17a92014-02-12 14:02:41 -0800521 while (msg_tlen >= addr_size) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700522 ND_TCHECK2(*msg_data, addr_size);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700523 ND_PRINT((ndo, "\n\t interface address %s",
524 is_ipv6 ? ip6addr_string(ndo, msg_data) :
525 ipaddr_string(ndo, msg_data)));
Elliott Hughes892a68b2015-10-19 14:43:53 -0700526
JP Abgrall53f17a92014-02-12 14:02:41 -0800527 msg_data += addr_size;
528 msg_tlen -= addr_size;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800529 }
530 break;
JP Abgrall53f17a92014-02-12 14:02:41 -0800531 }
532
533 case OLSR_HNA_MSG:
JP Abgrall53f17a92014-02-12 14:02:41 -0800534 if (is_ipv6)
535 {
536 int i = 0;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700537
538 ND_PRINT((ndo, "\n\t Advertised networks (total %u)",
539 (unsigned int) (msg_tlen / sizeof(struct olsr_hna6))));
540
JP Abgrall53f17a92014-02-12 14:02:41 -0800541 while (msg_tlen >= sizeof(struct olsr_hna6)) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700542 const struct olsr_hna6 *hna6;
JP Abgrall53f17a92014-02-12 14:02:41 -0800543
Elliott Hughes892a68b2015-10-19 14:43:53 -0700544 ND_TCHECK2(*msg_data, sizeof(struct olsr_hna6));
JP Abgrall53f17a92014-02-12 14:02:41 -0800545
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700546 hna6 = (const struct olsr_hna6 *)msg_data;
JP Abgrall53f17a92014-02-12 14:02:41 -0800547
Elliott Hughes892a68b2015-10-19 14:43:53 -0700548 ND_PRINT((ndo, "\n\t #%i: %s/%u",
549 i, ip6addr_string(ndo, hna6->network),
550 mask62plen (hna6->mask)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800551
552 msg_data += sizeof(struct olsr_hna6);
553 msg_tlen -= sizeof(struct olsr_hna6);
554 }
555 }
556 else
JP Abgrall53f17a92014-02-12 14:02:41 -0800557 {
558 int col = 0;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700559
560 ND_PRINT((ndo, "\n\t Advertised networks (total %u)",
561 (unsigned int) (msg_tlen / sizeof(struct olsr_hna4))));
562
JP Abgrall53f17a92014-02-12 14:02:41 -0800563 while (msg_tlen >= sizeof(struct olsr_hna4)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700564 ND_TCHECK2(*msg_data, sizeof(struct olsr_hna4));
JP Abgrall53f17a92014-02-12 14:02:41 -0800565
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700566 ptr.hna = (const struct olsr_hna4 *)msg_data;
JP Abgrall53f17a92014-02-12 14:02:41 -0800567
568 /* print 4 prefixes per line */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700569 if (!ptr.hna->network[0] && !ptr.hna->network[1] &&
570 !ptr.hna->network[2] && !ptr.hna->network[3] &&
571 !ptr.hna->mask[GW_HNA_PAD] &&
572 ptr.hna->mask[GW_HNA_FLAGS]) {
573 /* smart gateway */
574 ND_PRINT((ndo, "%sSmart-Gateway:%s%s%s%s%s %u/%u",
575 col == 0 ? "\n\t " : ", ", /* indent */
576 /* sgw */
577 /* LINKSPEED */
578 (ptr.hna->mask[GW_HNA_FLAGS] &
579 GW_HNA_FLAG_LINKSPEED) ? " LINKSPEED" : "",
580 /* IPV4 */
581 (ptr.hna->mask[GW_HNA_FLAGS] &
582 GW_HNA_FLAG_IPV4) ? " IPV4" : "",
583 /* IPV4-NAT */
584 (ptr.hna->mask[GW_HNA_FLAGS] &
585 GW_HNA_FLAG_IPV4_NAT) ? " IPV4-NAT" : "",
586 /* IPV6 */
587 (ptr.hna->mask[GW_HNA_FLAGS] &
588 GW_HNA_FLAG_IPV6) ? " IPV6" : "",
589 /* IPv6PREFIX */
590 (ptr.hna->mask[GW_HNA_FLAGS] &
591 GW_HNA_FLAG_IPV6PREFIX) ? " IPv6-PREFIX" : "",
592 /* uplink */
593 (ptr.hna->mask[GW_HNA_FLAGS] &
594 GW_HNA_FLAG_LINKSPEED) ?
595 deserialize_gw_speed(ptr.hna->mask[GW_HNA_UPLINK]) : 0,
596 /* downlink */
597 (ptr.hna->mask[GW_HNA_FLAGS] &
598 GW_HNA_FLAG_LINKSPEED) ?
599 deserialize_gw_speed(ptr.hna->mask[GW_HNA_DOWNLINK]) : 0
600 ));
601 } else {
602 /* normal route */
603 ND_PRINT((ndo, "%s%s/%u",
604 col == 0 ? "\n\t " : ", ",
605 ipaddr_string(ndo, ptr.hna->network),
606 mask2plen(EXTRACT_32BITS(ptr.hna->mask))));
607 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800608
609 msg_data += sizeof(struct olsr_hna4);
610 msg_tlen -= sizeof(struct olsr_hna4);
611
612 col = (col + 1) % 4;
613 }
614 }
615 break;
616
617 case OLSR_NAMESERVICE_MSG:
618 {
Elliott Hughescec480a2017-12-19 16:54:57 -0800619 u_int name_entries;
620 u_int addr_size;
621 int name_entries_valid;
JP Abgrall53f17a92014-02-12 14:02:41 -0800622 u_int i;
623
JP Abgrall53f17a92014-02-12 14:02:41 -0800624 if (msg_tlen < 4)
625 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700626 ND_TCHECK2(*msg_data, 4);
JP Abgrall53f17a92014-02-12 14:02:41 -0800627
Elliott Hughescec480a2017-12-19 16:54:57 -0800628 name_entries = EXTRACT_16BITS(msg_data+2);
629 addr_size = 4;
630 if (is_ipv6)
631 addr_size = 16;
632
633 name_entries_valid = 0;
634 if ((name_entries > 0)
635 && ((name_entries * (4 + addr_size)) <= msg_tlen))
636 name_entries_valid = 1;
637
Elliott Hughes892a68b2015-10-19 14:43:53 -0700638 ND_PRINT((ndo, "\n\t Version %u, Entries %u%s",
JP Abgrall53f17a92014-02-12 14:02:41 -0800639 EXTRACT_16BITS(msg_data),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700640 name_entries, (name_entries_valid == 0) ? " (invalid)" : ""));
JP Abgrall53f17a92014-02-12 14:02:41 -0800641
642 if (name_entries_valid == 0)
643 break;
644
645 msg_data += 4;
646 msg_tlen -= 4;
647
648 for (i = 0; i < name_entries; i++) {
649 int name_entry_len_valid = 0;
650
651 if (msg_tlen < 4)
652 break;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700653 ND_TCHECK2(*msg_data, 4);
JP Abgrall53f17a92014-02-12 14:02:41 -0800654
655 name_entry_type = EXTRACT_16BITS(msg_data);
656 name_entry_len = EXTRACT_16BITS(msg_data+2);
657
658 msg_data += 4;
659 msg_tlen -= 4;
660
661 if ((name_entry_len > 0) && ((addr_size + name_entry_len) <= msg_tlen))
662 name_entry_len_valid = 1;
663
Elliott Hughes892a68b2015-10-19 14:43:53 -0700664 ND_PRINT((ndo, "\n\t #%u: type %#06x, length %u%s",
JP Abgrall53f17a92014-02-12 14:02:41 -0800665 (unsigned int) i, name_entry_type,
Elliott Hughes892a68b2015-10-19 14:43:53 -0700666 name_entry_len, (name_entry_len_valid == 0) ? " (invalid)" : ""));
JP Abgrall53f17a92014-02-12 14:02:41 -0800667
668 if (name_entry_len_valid == 0)
669 break;
670
671 /* 32-bit alignment */
672 name_entry_padding = 0;
673 if (name_entry_len%4 != 0)
674 name_entry_padding = 4-(name_entry_len%4);
675
676 if (msg_tlen < addr_size + name_entry_len + name_entry_padding)
677 goto trunc;
678
Elliott Hughes892a68b2015-10-19 14:43:53 -0700679 ND_TCHECK2(*msg_data, addr_size + name_entry_len + name_entry_padding);
JP Abgrall53f17a92014-02-12 14:02:41 -0800680
JP Abgrall53f17a92014-02-12 14:02:41 -0800681 if (is_ipv6)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700682 ND_PRINT((ndo, ", address %s, name \"",
683 ip6addr_string(ndo, msg_data)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800684 else
Elliott Hughes892a68b2015-10-19 14:43:53 -0700685 ND_PRINT((ndo, ", address %s, name \"",
686 ipaddr_string(ndo, msg_data)));
687 (void)fn_printn(ndo, msg_data + addr_size, name_entry_len, NULL);
688 ND_PRINT((ndo, "\""));
JP Abgrall53f17a92014-02-12 14:02:41 -0800689
690 msg_data += addr_size + name_entry_len + name_entry_padding;
691 msg_tlen -= addr_size + name_entry_len + name_entry_padding;
692 } /* for (i = 0; i < name_entries; i++) */
693 break;
694 } /* case OLSR_NAMESERVICE_MSG */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800695
696 /*
697 * FIXME those are the defined messages that lack a decoder
698 * you are welcome to contribute code ;-)
699 */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800700 case OLSR_POWERINFO_MSG:
The Android Open Source Project2949f582009-03-03 19:30:46 -0800701 default:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700702 print_unknown_data(ndo, msg_data, "\n\t ", msg_tlen);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800703 break;
JP Abgrall53f17a92014-02-12 14:02:41 -0800704 } /* switch (msg_type) */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800705 tptr += msg_len;
JP Abgrall53f17a92014-02-12 14:02:41 -0800706 } /* while (tptr < (pptr+length)) */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800707
708 return;
709
710 trunc:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700711 ND_PRINT((ndo, "[|olsr]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800712}
713
714/*
715 * Local Variables:
716 * c-style: whitesmith
717 * c-basic-offset: 4
718 * End:
719 */