blob: 8f049237167b98f2f1643d40011beea17823171f [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
Elliott Hughes820eced2021-08-20 18:00:50 -070025#include <config.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080026#endif
27
Elliott Hughes820eced2021-08-20 18:00:50 -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 Hughes820eced2021-08-20 18:00:50 -070066 nd_uint16_t packet_len;
67 nd_uint16_t packet_seq;
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 Hughes820eced2021-08-20 18:00:50 -070092 nd_uint8_t msg_type;
93 nd_uint8_t vtime;
94 nd_uint16_t msg_len;
95 nd_ipv4 originator;
96 nd_uint8_t ttl;
97 nd_uint8_t hopcount;
98 nd_uint16_t msg_seq;
The Android Open Source Project2949f582009-03-03 19:30:46 -080099};
100
JP Abgrall53f17a92014-02-12 14:02:41 -0800101struct olsr_msg6 {
Elliott Hughes820eced2021-08-20 18:00:50 -0700102 nd_uint8_t msg_type;
103 nd_uint8_t vtime;
104 nd_uint16_t msg_len;
105 nd_ipv6 originator;
106 nd_uint8_t ttl;
107 nd_uint8_t hopcount;
108 nd_uint16_t msg_seq;
JP Abgrall53f17a92014-02-12 14:02:41 -0800109};
110
The Android Open Source Project2949f582009-03-03 19:30:46 -0800111struct olsr_hello {
Elliott Hughes820eced2021-08-20 18:00:50 -0700112 nd_byte res[2];
113 nd_uint8_t htime;
114 nd_uint8_t will;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800115};
116
117struct olsr_hello_link {
Elliott Hughes820eced2021-08-20 18:00:50 -0700118 nd_uint8_t link_code;
119 nd_byte res;
120 nd_uint16_t len;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800121};
122
123struct olsr_tc {
Elliott Hughes820eced2021-08-20 18:00:50 -0700124 nd_uint16_t ans_seq;
125 nd_byte 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 Hughes820eced2021-08-20 18:00:50 -0700129 nd_ipv4 network;
130 nd_ipv4 mask;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800131};
132
JP Abgrall53f17a92014-02-12 14:02:41 -0800133struct olsr_hna6 {
Elliott Hughes820eced2021-08-20 18:00:50 -0700134 nd_ipv6 network;
135 nd_ipv6 mask;
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 Hughes820eced2021-08-20 18:00:50 -0700178 nd_ipv4 neighbor;
179 nd_uint8_t link_quality;
180 nd_uint8_t neighbor_link_quality;
181 nd_byte 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 Hughes820eced2021-08-20 18:00:50 -0700185 nd_ipv6 neighbor;
186 nd_uint8_t link_quality;
187 nd_uint8_t neighbor_link_quality;
188 nd_byte 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
Elliott Hughes820eced2021-08-20 18:00:50 -0700216 while (exp != 0) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700217 speed *= 10;
Elliott Hughes820eced2021-08-20 18:00:50 -0700218 exp--;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700219 }
220 return speed;
221}
222
The Android Open Source Project2949f582009-03-03 19:30:46 -0800223/*
224 * macro to convert the 8-bit mantissa/exponent to a double float
225 * taken from olsr.org.
226 */
227#define VTIME_SCALE_FACTOR 0.0625
228#define ME_TO_DOUBLE(me) \
229 (double)(VTIME_SCALE_FACTOR*(1+(double)(me>>4)/16)*(double)(1<<(me&0x0F)))
230
231/*
232 * print a neighbor list with LQ extensions.
233 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700234static int
235olsr_print_lq_neighbor4(netdissect_options *ndo,
236 const u_char *msg_data, u_int hello_len)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800237{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700238 const struct olsr_lq_neighbor4 *lq_neighbor;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800239
JP Abgrall53f17a92014-02-12 14:02:41 -0800240 while (hello_len >= sizeof(struct olsr_lq_neighbor4)) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800241
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700242 lq_neighbor = (const struct olsr_lq_neighbor4 *)msg_data;
Elliott Hughes820eced2021-08-20 18:00:50 -0700243 ND_TCHECK_SIZE(lq_neighbor);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800244
Elliott Hughes820eced2021-08-20 18:00:50 -0700245 ND_PRINT("\n\t neighbor %s, link-quality %.2f%%"
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700246 ", neighbor-link-quality %.2f%%",
Elliott Hughes820eced2021-08-20 18:00:50 -0700247 GET_IPADDR_STRING(lq_neighbor->neighbor),
248 ((double) GET_U_1(lq_neighbor->link_quality)/2.55),
249 ((double) GET_U_1(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);
Elliott Hughes820eced2021-08-20 18:00:50 -0700255trunc:
256 return -1;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800257}
258
Elliott Hughes892a68b2015-10-19 14:43:53 -0700259static int
260olsr_print_lq_neighbor6(netdissect_options *ndo,
261 const u_char *msg_data, u_int hello_len)
JP Abgrall53f17a92014-02-12 14:02:41 -0800262{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700263 const struct olsr_lq_neighbor6 *lq_neighbor;
JP Abgrall53f17a92014-02-12 14:02:41 -0800264
265 while (hello_len >= sizeof(struct olsr_lq_neighbor6)) {
266
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700267 lq_neighbor = (const struct olsr_lq_neighbor6 *)msg_data;
Elliott Hughes820eced2021-08-20 18:00:50 -0700268 ND_TCHECK_SIZE(lq_neighbor);
JP Abgrall53f17a92014-02-12 14:02:41 -0800269
Elliott Hughes820eced2021-08-20 18:00:50 -0700270 ND_PRINT("\n\t neighbor %s, link-quality %.2f%%"
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700271 ", neighbor-link-quality %.2f%%",
Elliott Hughes820eced2021-08-20 18:00:50 -0700272 GET_IP6ADDR_STRING(lq_neighbor->neighbor),
273 ((double) GET_U_1(lq_neighbor->link_quality)/2.55),
274 ((double) GET_U_1(lq_neighbor->neighbor_link_quality)/2.55));
JP Abgrall53f17a92014-02-12 14:02:41 -0800275
276 msg_data += sizeof(struct olsr_lq_neighbor6);
277 hello_len -= sizeof(struct olsr_lq_neighbor6);
278 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700279 return (0);
Elliott Hughes820eced2021-08-20 18:00:50 -0700280trunc:
281 return -1;
JP Abgrall53f17a92014-02-12 14:02:41 -0800282}
JP Abgrall53f17a92014-02-12 14:02:41 -0800283
The Android Open Source Project2949f582009-03-03 19:30:46 -0800284/*
285 * print a neighbor list.
286 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700287static int
288olsr_print_neighbor(netdissect_options *ndo,
289 const u_char *msg_data, u_int hello_len)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800290{
291 int neighbor;
292
Elliott Hughes820eced2021-08-20 18:00:50 -0700293 ND_PRINT("\n\t neighbor\n\t\t");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800294 neighbor = 1;
295
Elliott Hughes820eced2021-08-20 18:00:50 -0700296 while (hello_len >= sizeof(nd_ipv4)) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800297 /* print 4 neighbors per line */
Elliott Hughes820eced2021-08-20 18:00:50 -0700298 ND_PRINT("%s%s", GET_IPADDR_STRING(msg_data),
299 neighbor % 4 == 0 ? "\n\t\t" : " ");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800300
Elliott Hughes820eced2021-08-20 18:00:50 -0700301 msg_data += sizeof(nd_ipv4);
302 hello_len -= sizeof(nd_ipv4);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800303 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700304 return (0);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800305}
306
307
308void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700309olsr_print(netdissect_options *ndo,
310 const u_char *pptr, u_int length, int is_ipv6)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800311{
312 union {
313 const struct olsr_common *common;
JP Abgrall53f17a92014-02-12 14:02:41 -0800314 const struct olsr_msg4 *msg4;
315 const struct olsr_msg6 *msg6;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800316 const struct olsr_hello *hello;
317 const struct olsr_hello_link *hello_link;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800318 const struct olsr_tc *tc;
JP Abgrall53f17a92014-02-12 14:02:41 -0800319 const struct olsr_hna4 *hna;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800320 } ptr;
321
JP Abgrall53f17a92014-02-12 14:02:41 -0800322 u_int msg_type, msg_len, msg_tlen, hello_len;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700323 uint16_t name_entry_type, name_entry_len;
JP Abgrall53f17a92014-02-12 14:02:41 -0800324 u_int name_entry_padding;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700325 uint8_t link_type, neighbor_type;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800326 const u_char *tptr, *msg_data;
327
Elliott Hughes820eced2021-08-20 18:00:50 -0700328 ndo->ndo_protocol = "olsr";
JP Abgrall53f17a92014-02-12 14:02:41 -0800329 tptr = pptr;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800330
Elliott Hughes820eced2021-08-20 18:00:50 -0700331 nd_print_protocol_caps(ndo);
332 ND_PRINT("v%u", (is_ipv6) ? 6 : 4);
333
The Android Open Source Project2949f582009-03-03 19:30:46 -0800334 if (length < sizeof(struct olsr_common)) {
335 goto trunc;
336 }
337
Elliott Hughes820eced2021-08-20 18:00:50 -0700338 ND_TCHECK_LEN(tptr, sizeof(struct olsr_common));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800339
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700340 ptr.common = (const struct olsr_common *)tptr;
Elliott Hughes820eced2021-08-20 18:00:50 -0700341 length = ND_MIN(length, GET_BE_U_2(ptr.common->packet_len));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800342
Elliott Hughes820eced2021-08-20 18:00:50 -0700343 ND_PRINT(", seq 0x%04x, length %u",
344 GET_BE_U_2(ptr.common->packet_seq),
345 length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800346
347 tptr += sizeof(struct olsr_common);
348
349 /*
350 * In non-verbose mode, just print version.
351 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700352 if (ndo->ndo_vflag < 1) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800353 return;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800354 }
355
356 while (tptr < (pptr+length)) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800357 union
358 {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700359 const struct olsr_msg4 *v4;
360 const struct olsr_msg6 *v6;
JP Abgrall53f17a92014-02-12 14:02:41 -0800361 } msgptr;
362 int msg_len_valid = 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800363
JP Abgrall53f17a92014-02-12 14:02:41 -0800364 if (is_ipv6)
365 {
Elliott Hughes820eced2021-08-20 18:00:50 -0700366 ND_TCHECK_LEN(tptr, sizeof(struct olsr_msg6));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700367 msgptr.v6 = (const struct olsr_msg6 *) tptr;
Elliott Hughes820eced2021-08-20 18:00:50 -0700368 msg_type = GET_U_1(msgptr.v6->msg_type);
369 msg_len = GET_BE_U_2(msgptr.v6->msg_len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800370 if ((msg_len >= sizeof (struct olsr_msg6))
371 && (msg_len <= length))
372 msg_len_valid = 1;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800373
JP Abgrall53f17a92014-02-12 14:02:41 -0800374 /* infinite loop check */
375 if (msg_type == 0 || msg_len == 0) {
376 return;
377 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800378
Elliott Hughes820eced2021-08-20 18:00:50 -0700379 ND_PRINT("\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700380 "\n\t vtime %.3fs, msg-seq 0x%04x, length %u%s",
JP Abgrall53f17a92014-02-12 14:02:41 -0800381 tok2str(olsr_msg_values, "Unknown", msg_type),
Elliott Hughes820eced2021-08-20 18:00:50 -0700382 msg_type, GET_IP6ADDR_STRING(msgptr.v6->originator),
383 GET_U_1(msgptr.v6->ttl),
384 GET_U_1(msgptr.v6->hopcount),
385 ME_TO_DOUBLE(GET_U_1(msgptr.v6->vtime)),
386 GET_BE_U_2(msgptr.v6->msg_seq),
387 msg_len, (msg_len_valid == 0) ? " (invalid)" : "");
Elliott Hughes892a68b2015-10-19 14:43:53 -0700388 if (!msg_len_valid) {
389 return;
390 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800391
392 msg_tlen = msg_len - sizeof(struct olsr_msg6);
393 msg_data = tptr + sizeof(struct olsr_msg6);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800394 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800395 else /* (!is_ipv6) */
JP Abgrall53f17a92014-02-12 14:02:41 -0800396 {
Elliott Hughes820eced2021-08-20 18:00:50 -0700397 ND_TCHECK_LEN(tptr, sizeof(struct olsr_msg4));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700398 msgptr.v4 = (const struct olsr_msg4 *) tptr;
Elliott Hughes820eced2021-08-20 18:00:50 -0700399 msg_type = GET_U_1(msgptr.v4->msg_type);
400 msg_len = GET_BE_U_2(msgptr.v4->msg_len);
JP Abgrall53f17a92014-02-12 14:02:41 -0800401 if ((msg_len >= sizeof (struct olsr_msg4))
402 && (msg_len <= length))
403 msg_len_valid = 1;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800404
JP Abgrall53f17a92014-02-12 14:02:41 -0800405 /* infinite loop check */
406 if (msg_type == 0 || msg_len == 0) {
407 return;
408 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800409
Elliott Hughes820eced2021-08-20 18:00:50 -0700410 ND_PRINT("\n\t%s Message (%#04x), originator %s, ttl %u, hop %u"
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700411 "\n\t vtime %.3fs, msg-seq 0x%04x, length %u%s",
JP Abgrall53f17a92014-02-12 14:02:41 -0800412 tok2str(olsr_msg_values, "Unknown", msg_type),
Elliott Hughes820eced2021-08-20 18:00:50 -0700413 msg_type, GET_IPADDR_STRING(msgptr.v4->originator),
414 GET_U_1(msgptr.v4->ttl),
415 GET_U_1(msgptr.v4->hopcount),
416 ME_TO_DOUBLE(GET_U_1(msgptr.v4->vtime)),
417 GET_BE_U_2(msgptr.v4->msg_seq),
418 msg_len, (msg_len_valid == 0) ? " (invalid)" : "");
Elliott Hughes892a68b2015-10-19 14:43:53 -0700419 if (!msg_len_valid) {
420 return;
421 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800422
423 msg_tlen = msg_len - sizeof(struct olsr_msg4);
424 msg_data = tptr + sizeof(struct olsr_msg4);
425 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800426
427 switch (msg_type) {
428 case OLSR_HELLO_MSG:
429 case OLSR_HELLO_LQ_MSG:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700430 if (msg_tlen < sizeof(struct olsr_hello))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800431 goto trunc;
Elliott Hughes820eced2021-08-20 18:00:50 -0700432 ND_TCHECK_LEN(msg_data, sizeof(struct olsr_hello));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800433
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700434 ptr.hello = (const struct olsr_hello *)msg_data;
Elliott Hughes820eced2021-08-20 18:00:50 -0700435 ND_PRINT("\n\t hello-time %.3fs, MPR willingness %u",
436 ME_TO_DOUBLE(GET_U_1(ptr.hello->htime)),
437 GET_U_1(ptr.hello->will));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800438 msg_data += sizeof(struct olsr_hello);
439 msg_tlen -= sizeof(struct olsr_hello);
440
441 while (msg_tlen >= sizeof(struct olsr_hello_link)) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800442 int hello_len_valid = 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800443
444 /*
445 * link-type.
446 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700447 ND_TCHECK_LEN(msg_data, sizeof(struct olsr_hello_link));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800448
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700449 ptr.hello_link = (const struct olsr_hello_link *)msg_data;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800450
Elliott Hughes820eced2021-08-20 18:00:50 -0700451 hello_len = GET_BE_U_2(ptr.hello_link->len);
452 link_type = OLSR_EXTRACT_LINK_TYPE(GET_U_1(ptr.hello_link->link_code));
453 neighbor_type = OLSR_EXTRACT_NEIGHBOR_TYPE(GET_U_1(ptr.hello_link->link_code));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800454
JP Abgrall53f17a92014-02-12 14:02:41 -0800455 if ((hello_len <= msg_tlen)
456 && (hello_len >= sizeof(struct olsr_hello_link)))
457 hello_len_valid = 1;
458
Elliott Hughes820eced2021-08-20 18:00:50 -0700459 ND_PRINT("\n\t link-type %s, neighbor-type %s, len %u%s",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800460 tok2str(olsr_link_type_values, "Unknown", link_type),
461 tok2str(olsr_neighbor_type_values, "Unknown", neighbor_type),
JP Abgrall53f17a92014-02-12 14:02:41 -0800462 hello_len,
Elliott Hughes820eced2021-08-20 18:00:50 -0700463 (hello_len_valid == 0) ? " (invalid)" : "");
JP Abgrall53f17a92014-02-12 14:02:41 -0800464
465 if (hello_len_valid == 0)
466 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800467
468 msg_data += sizeof(struct olsr_hello_link);
469 msg_tlen -= sizeof(struct olsr_hello_link);
470 hello_len -= sizeof(struct olsr_hello_link);
471
Elliott Hughes820eced2021-08-20 18:00:50 -0700472 ND_TCHECK_LEN(msg_data, hello_len);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800473 if (msg_type == OLSR_HELLO_MSG) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700474 if (olsr_print_neighbor(ndo, msg_data, hello_len) == -1)
475 goto trunc;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800476 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700477 if (is_ipv6) {
478 if (olsr_print_lq_neighbor6(ndo, msg_data, hello_len) == -1)
479 goto trunc;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700480 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700481 if (olsr_print_lq_neighbor4(ndo, msg_data, hello_len) == -1)
482 goto trunc;
483 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800484 }
485
486 msg_data += hello_len;
487 msg_tlen -= hello_len;
488 }
489 break;
490
491 case OLSR_TC_MSG:
492 case OLSR_TC_LQ_MSG:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700493 if (msg_tlen < sizeof(struct olsr_tc))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800494 goto trunc;
Elliott Hughes820eced2021-08-20 18:00:50 -0700495 ND_TCHECK_LEN(msg_data, sizeof(struct olsr_tc));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800496
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700497 ptr.tc = (const struct olsr_tc *)msg_data;
Elliott Hughes820eced2021-08-20 18:00:50 -0700498 ND_PRINT("\n\t advertised neighbor seq 0x%04x",
499 GET_BE_U_2(ptr.tc->ans_seq));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800500 msg_data += sizeof(struct olsr_tc);
501 msg_tlen -= sizeof(struct olsr_tc);
502
503 if (msg_type == OLSR_TC_MSG) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700504 if (olsr_print_neighbor(ndo, msg_data, msg_tlen) == -1)
505 goto trunc;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800506 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700507 if (is_ipv6) {
508 if (olsr_print_lq_neighbor6(ndo, msg_data, msg_tlen) == -1)
509 goto trunc;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700510 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700511 if (olsr_print_lq_neighbor4(ndo, msg_data, msg_tlen) == -1)
512 goto trunc;
513 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800514 }
515 break;
516
517 case OLSR_MID_MSG:
JP Abgrall53f17a92014-02-12 14:02:41 -0800518 {
Elliott Hughes820eced2021-08-20 18:00:50 -0700519 u_int addr_size = (u_int)sizeof(nd_ipv4);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800520
JP Abgrall53f17a92014-02-12 14:02:41 -0800521 if (is_ipv6)
Elliott Hughes820eced2021-08-20 18:00:50 -0700522 addr_size = (u_int)sizeof(nd_ipv6);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800523
JP Abgrall53f17a92014-02-12 14:02:41 -0800524 while (msg_tlen >= addr_size) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700525 ND_TCHECK_LEN(msg_data, addr_size);
526 ND_PRINT("\n\t interface address %s",
527 is_ipv6 ? GET_IP6ADDR_STRING(msg_data) :
528 GET_IPADDR_STRING(msg_data));
Elliott Hughes892a68b2015-10-19 14:43:53 -0700529
JP Abgrall53f17a92014-02-12 14:02:41 -0800530 msg_data += addr_size;
531 msg_tlen -= addr_size;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800532 }
533 break;
JP Abgrall53f17a92014-02-12 14:02:41 -0800534 }
535
536 case OLSR_HNA_MSG:
JP Abgrall53f17a92014-02-12 14:02:41 -0800537 if (is_ipv6)
538 {
539 int i = 0;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700540
Elliott Hughes820eced2021-08-20 18:00:50 -0700541 ND_PRINT("\n\t Advertised networks (total %u)",
542 (unsigned int) (msg_tlen / sizeof(struct olsr_hna6)));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700543
JP Abgrall53f17a92014-02-12 14:02:41 -0800544 while (msg_tlen >= sizeof(struct olsr_hna6)) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700545 const struct olsr_hna6 *hna6;
JP Abgrall53f17a92014-02-12 14:02:41 -0800546
Elliott Hughes820eced2021-08-20 18:00:50 -0700547 ND_TCHECK_LEN(msg_data, sizeof(struct olsr_hna6));
JP Abgrall53f17a92014-02-12 14:02:41 -0800548
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700549 hna6 = (const struct olsr_hna6 *)msg_data;
JP Abgrall53f17a92014-02-12 14:02:41 -0800550
Elliott Hughes820eced2021-08-20 18:00:50 -0700551 ND_PRINT("\n\t #%i: %s/%u",
552 i, GET_IP6ADDR_STRING(hna6->network),
553 mask62plen (hna6->mask));
JP Abgrall53f17a92014-02-12 14:02:41 -0800554
555 msg_data += sizeof(struct olsr_hna6);
556 msg_tlen -= sizeof(struct olsr_hna6);
557 }
558 }
559 else
JP Abgrall53f17a92014-02-12 14:02:41 -0800560 {
561 int col = 0;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700562
Elliott Hughes820eced2021-08-20 18:00:50 -0700563 ND_PRINT("\n\t Advertised networks (total %u)",
564 (unsigned int) (msg_tlen / sizeof(struct olsr_hna4)));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700565
JP Abgrall53f17a92014-02-12 14:02:41 -0800566 while (msg_tlen >= sizeof(struct olsr_hna4)) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700567 ND_TCHECK_LEN(msg_data, sizeof(struct olsr_hna4));
JP Abgrall53f17a92014-02-12 14:02:41 -0800568
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700569 ptr.hna = (const struct olsr_hna4 *)msg_data;
JP Abgrall53f17a92014-02-12 14:02:41 -0800570
571 /* print 4 prefixes per line */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700572 if (!ptr.hna->network[0] && !ptr.hna->network[1] &&
573 !ptr.hna->network[2] && !ptr.hna->network[3] &&
574 !ptr.hna->mask[GW_HNA_PAD] &&
575 ptr.hna->mask[GW_HNA_FLAGS]) {
576 /* smart gateway */
Elliott Hughes820eced2021-08-20 18:00:50 -0700577 ND_PRINT("%sSmart-Gateway:%s%s%s%s%s %u/%u",
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700578 col == 0 ? "\n\t " : ", ", /* indent */
579 /* sgw */
580 /* LINKSPEED */
581 (ptr.hna->mask[GW_HNA_FLAGS] &
582 GW_HNA_FLAG_LINKSPEED) ? " LINKSPEED" : "",
583 /* IPV4 */
584 (ptr.hna->mask[GW_HNA_FLAGS] &
585 GW_HNA_FLAG_IPV4) ? " IPV4" : "",
586 /* IPV4-NAT */
587 (ptr.hna->mask[GW_HNA_FLAGS] &
588 GW_HNA_FLAG_IPV4_NAT) ? " IPV4-NAT" : "",
589 /* IPV6 */
590 (ptr.hna->mask[GW_HNA_FLAGS] &
591 GW_HNA_FLAG_IPV6) ? " IPV6" : "",
592 /* IPv6PREFIX */
593 (ptr.hna->mask[GW_HNA_FLAGS] &
594 GW_HNA_FLAG_IPV6PREFIX) ? " IPv6-PREFIX" : "",
595 /* uplink */
596 (ptr.hna->mask[GW_HNA_FLAGS] &
597 GW_HNA_FLAG_LINKSPEED) ?
598 deserialize_gw_speed(ptr.hna->mask[GW_HNA_UPLINK]) : 0,
599 /* downlink */
600 (ptr.hna->mask[GW_HNA_FLAGS] &
601 GW_HNA_FLAG_LINKSPEED) ?
602 deserialize_gw_speed(ptr.hna->mask[GW_HNA_DOWNLINK]) : 0
Elliott Hughes820eced2021-08-20 18:00:50 -0700603 );
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700604 } else {
605 /* normal route */
Elliott Hughes820eced2021-08-20 18:00:50 -0700606 ND_PRINT("%s%s/%u",
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700607 col == 0 ? "\n\t " : ", ",
Elliott Hughes820eced2021-08-20 18:00:50 -0700608 GET_IPADDR_STRING(ptr.hna->network),
609 mask2plen(GET_BE_U_4(ptr.hna->mask)));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700610 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800611
612 msg_data += sizeof(struct olsr_hna4);
613 msg_tlen -= sizeof(struct olsr_hna4);
614
615 col = (col + 1) % 4;
616 }
617 }
618 break;
619
620 case OLSR_NAMESERVICE_MSG:
621 {
Elliott Hughescec480a2017-12-19 16:54:57 -0800622 u_int name_entries;
623 u_int addr_size;
624 int name_entries_valid;
JP Abgrall53f17a92014-02-12 14:02:41 -0800625 u_int i;
626
JP Abgrall53f17a92014-02-12 14:02:41 -0800627 if (msg_tlen < 4)
628 goto trunc;
JP Abgrall53f17a92014-02-12 14:02:41 -0800629
Elliott Hughes820eced2021-08-20 18:00:50 -0700630 name_entries = GET_BE_U_2(msg_data + 2);
Elliott Hughescec480a2017-12-19 16:54:57 -0800631 addr_size = 4;
632 if (is_ipv6)
633 addr_size = 16;
634
635 name_entries_valid = 0;
636 if ((name_entries > 0)
637 && ((name_entries * (4 + addr_size)) <= msg_tlen))
638 name_entries_valid = 1;
639
Elliott Hughes820eced2021-08-20 18:00:50 -0700640 ND_PRINT("\n\t Version %u, Entries %u%s",
641 GET_BE_U_2(msg_data),
642 name_entries, (name_entries_valid == 0) ? " (invalid)" : "");
JP Abgrall53f17a92014-02-12 14:02:41 -0800643
644 if (name_entries_valid == 0)
645 break;
646
647 msg_data += 4;
648 msg_tlen -= 4;
649
650 for (i = 0; i < name_entries; i++) {
651 int name_entry_len_valid = 0;
652
653 if (msg_tlen < 4)
654 break;
JP Abgrall53f17a92014-02-12 14:02:41 -0800655
Elliott Hughes820eced2021-08-20 18:00:50 -0700656 name_entry_type = GET_BE_U_2(msg_data);
657 name_entry_len = GET_BE_U_2(msg_data + 2);
JP Abgrall53f17a92014-02-12 14:02:41 -0800658
659 msg_data += 4;
660 msg_tlen -= 4;
661
662 if ((name_entry_len > 0) && ((addr_size + name_entry_len) <= msg_tlen))
663 name_entry_len_valid = 1;
664
Elliott Hughes820eced2021-08-20 18:00:50 -0700665 ND_PRINT("\n\t #%u: type %#06x, length %u%s",
JP Abgrall53f17a92014-02-12 14:02:41 -0800666 (unsigned int) i, name_entry_type,
Elliott Hughes820eced2021-08-20 18:00:50 -0700667 name_entry_len, (name_entry_len_valid == 0) ? " (invalid)" : "");
JP Abgrall53f17a92014-02-12 14:02:41 -0800668
669 if (name_entry_len_valid == 0)
670 break;
671
672 /* 32-bit alignment */
673 name_entry_padding = 0;
674 if (name_entry_len%4 != 0)
675 name_entry_padding = 4-(name_entry_len%4);
676
677 if (msg_tlen < addr_size + name_entry_len + name_entry_padding)
678 goto trunc;
679
Elliott Hughes820eced2021-08-20 18:00:50 -0700680 ND_TCHECK_LEN(msg_data,
681 addr_size + name_entry_len + name_entry_padding);
JP Abgrall53f17a92014-02-12 14:02:41 -0800682
JP Abgrall53f17a92014-02-12 14:02:41 -0800683 if (is_ipv6)
Elliott Hughes820eced2021-08-20 18:00:50 -0700684 ND_PRINT(", address %s, name \"",
685 GET_IP6ADDR_STRING(msg_data));
JP Abgrall53f17a92014-02-12 14:02:41 -0800686 else
Elliott Hughes820eced2021-08-20 18:00:50 -0700687 ND_PRINT(", address %s, name \"",
688 GET_IPADDR_STRING(msg_data));
689 (void)nd_printn(ndo, msg_data + addr_size, name_entry_len, NULL);
690 ND_PRINT("\"");
JP Abgrall53f17a92014-02-12 14:02:41 -0800691
692 msg_data += addr_size + name_entry_len + name_entry_padding;
693 msg_tlen -= addr_size + name_entry_len + name_entry_padding;
694 } /* for (i = 0; i < name_entries; i++) */
695 break;
696 } /* case OLSR_NAMESERVICE_MSG */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800697
698 /*
699 * FIXME those are the defined messages that lack a decoder
700 * you are welcome to contribute code ;-)
701 */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800702 case OLSR_POWERINFO_MSG:
The Android Open Source Project2949f582009-03-03 19:30:46 -0800703 default:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700704 print_unknown_data(ndo, msg_data, "\n\t ", msg_tlen);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800705 break;
JP Abgrall53f17a92014-02-12 14:02:41 -0800706 } /* switch (msg_type) */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800707 tptr += msg_len;
JP Abgrall53f17a92014-02-12 14:02:41 -0800708 } /* while (tptr < (pptr+length)) */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800709
710 return;
711
712 trunc:
Elliott Hughes820eced2021-08-20 18:00:50 -0700713 nd_print_trunc(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800714}