]> The Tcpdump Group git mirrors - tcpdump/blob - print-lisp.c
Fix incompatible pointer types with time functions calls on Windows
[tcpdump] / print-lisp.c
1 /*
2 * Copyright (c) 2015 Ritesh Ranjan (r.ranjan789@gmail.com)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* \summary: - Locator/Identifier Separation Protocol (LISP) printer */
30
31 /*
32 * specification: RFC 6830
33 *
34 *
35 * The Map-Register message format is:
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 * |Type=3 |P|S|I|R| Reserved |M| Record Count |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Nonce . . . |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | . . . Nonce |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Key ID | Authentication Data Length |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * ~ Authentication Data ~
49 * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | | Record TTL |
51 * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * R | Locator Count | EID mask-len | ACT |A| Reserved |
53 * e +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * c | Rsvd | Map-Version Number | EID-Prefix-AFI |
55 * o +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * r | EID-Prefix |
57 * d +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * | /| Priority | Weight | M Priority | M Weight |
59 * | L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 * | o | Unused Flags |L|p|R| Loc-AFI |
61 * | c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 * | \| Locator |
63 * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 *
65 *
66 * The Map-Notify message format is:
67 *
68 * 0 1 2 3
69 * 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
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 * |Type=4 |I|R| Reserved | Record Count |
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 * | Nonce . . . |
74 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75 * | . . . Nonce |
76 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
77 * | Key ID | Authentication Data Length |
78 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79 * ~ Authentication Data ~
80 * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81 * | | Record TTL |
82 * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 * R | Locator Count | EID mask-len | ACT |A| Reserved |
84 * e +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85 * c | Rsvd | Map-Version Number | EID-Prefix-AFI |
86 * o +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87 * r | EID-Prefix |
88 * d +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 * | /| Priority | Weight | M Priority | M Weight |
90 * | L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 * | o | Unused Flags |L|p|R| Loc-AFI |
92 * | c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 * | \| Locator |
94 * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 */
96
97 #include <config.h>
98
99 #include "netdissect-stdinc.h"
100 #include "netdissect.h"
101
102 #include "ip.h"
103 #include "ip6.h"
104
105 #include "extract.h"
106 #include "addrtoname.h"
107
108
109 #define IPv4_AFI 1
110 #define IPv6_AFI 2
111 #define TYPE_INDEX 4
112 #define LISP_MAP_NOTIFY_IBIT_MASK 8
113 #define LISP_MAP_REGISTER_IBIT_MASK 2
114
115 enum {
116 LISP_MAP_REQUEST = 1,
117 LISP_MAP_REPLY,
118 LISP_MAP_REGISTER,
119 LISP_MAP_NOTIFY,
120 LISP_ENCAPSULATED_CONTROL_MESSAGE = 8
121 };
122
123 enum {
124 LISP_AUTH_NONE,
125 LISP_AUTH_SHA1,
126 LISP_AUTH_SHA256
127 };
128
129 static const struct tok lisp_type [] = {
130 { 0, "LISP-Reserved" },
131 { 1, "LISP-Map-Request" },
132 { 2, "LISP-Map-Reply" },
133 { 3, "LISP-Map-Register" },
134 { 4, "LISP-Map-Notify" },
135 { 8, "LISP-Encapsulated-Control-Message" },
136 { 0, NULL }
137 };
138
139 /*
140 * P-Bit : Request for Proxy Map-Reply from the MS/MR
141 * S-Bit : Security Enhancement. ETR is LISP-SEC enabled. draft-ietf-lisp-sec
142 * I-Bit : 128 bit xTR-ID and 64 bit Site-ID present.
143 * xTR-ID and Site-ID help in differentiation of xTRs in multi xTR
144 * and multi Site deployment scenarios.
145 * R-Bit : Built for a Reencapsulating-Tunnel-Router. Used in Traffic
146 * Engineering and Service Chaining
147 */
148 static const struct tok map_register_hdr_flag[] = {
149 { 0x08000000, "P-Proxy-Map-Reply" },
150 { 0x04000000, "S-LISP-SEC-Capable" },
151 { 0x02000000, "I-xTR-ID-Present" },
152 { 0x01000000, "R-Build-For-RTR" },
153 { 0x00000100, "M-Want-Map-Notify" },
154 { 0, NULL }
155 };
156
157 static const struct tok map_notify_hdr_flag[] = {
158 { 0x08000000, "I-xTR-ID-Present" },
159 { 0x04000000, "R-Build-For-RTR" },
160 { 0, NULL }
161 };
162
163 static const struct tok auth_type[] = {
164 { LISP_AUTH_NONE, "None" },
165 { LISP_AUTH_SHA1, "SHA1" },
166 { LISP_AUTH_SHA256, "SHA256" },
167 { 0, NULL}
168 };
169
170 static const struct tok lisp_eid_action[] = {
171 { 0, "No-Action" },
172 { 1, "Natively-Forward" },
173 { 2, "Send-Map-Request" },
174 { 3, "Drop" },
175 { 0, NULL}
176 };
177
178 static const struct tok lisp_loc_flag[] = {
179 { 0x0004, "Local-Locator" },
180 { 0x0002, "RLoc-Probed" },
181 { 0x0001, "Reachable" },
182 { 0, NULL }
183 };
184
185 typedef struct map_register_hdr {
186 nd_uint8_t type_and_flag;
187 nd_uint8_t reserved;
188 nd_uint8_t reserved_and_flag2;
189 nd_uint8_t record_count;
190 nd_uint64_t nonce;
191 nd_uint16_t key_id;
192 nd_uint16_t auth_data_len;
193 } lisp_map_register_hdr;
194
195 #define MAP_REGISTER_HDR_LEN sizeof(lisp_map_register_hdr)
196
197 typedef struct map_register_eid {
198 nd_uint32_t ttl;
199 nd_uint8_t locator_count;
200 nd_uint8_t eid_prefix_mask_length;
201 nd_uint8_t act_auth_inc_res;
202 nd_uint8_t reserved;
203 nd_uint16_t reserved_and_version;
204 nd_uint16_t eid_prefix_afi;
205 } lisp_map_register_eid;
206
207 #define MAP_REGISTER_EID_LEN sizeof(lisp_map_register_eid)
208
209 typedef struct map_register_loc {
210 nd_uint8_t priority;
211 nd_uint8_t weight;
212 nd_uint8_t m_priority;
213 nd_uint8_t m_weight;
214 nd_uint16_t unused_and_flag;
215 nd_uint16_t locator_afi;
216 } lisp_map_register_loc;
217
218 #define MAP_REGISTER_LOC_LEN sizeof(lisp_map_register_loc)
219
220 static uint8_t extract_lisp_type(uint8_t);
221 static uint8_t is_xtr_data_present(uint8_t, uint8_t);
222 static void lisp_hdr_flag(netdissect_options *, const lisp_map_register_hdr *);
223 static void action_flag(netdissect_options *, uint8_t);
224 static void loc_hdr_flag(netdissect_options *, uint16_t);
225
226 void
227 lisp_print(netdissect_options *ndo, const u_char *bp, u_int length)
228 {
229 uint8_t type_and_flag;
230 uint8_t type;
231 uint8_t mask_len;
232 uint8_t loc_count;
233 uint8_t xtr_present;
234 uint8_t record_count;
235 uint16_t key_id;
236 uint16_t eid_afi;
237 uint16_t loc_afi;
238 uint16_t map_version;
239 uint16_t packet_offset;
240 uint16_t auth_data_len;
241 uint32_t ttl;
242 const u_char *packet_iterator;
243 const lisp_map_register_hdr *lisp_hdr;
244 const lisp_map_register_eid *lisp_eid;
245 const lisp_map_register_loc *lisp_loc;
246
247 ndo->ndo_protocol = "lisp";
248 /* Check if enough bytes for header are available */
249 ND_TCHECK_LEN(bp, MAP_REGISTER_HDR_LEN);
250 lisp_hdr = (const lisp_map_register_hdr *) bp;
251 lisp_hdr_flag(ndo, lisp_hdr);
252 /* Supporting only MAP NOTIFY and MAP REGISTER LISP packets */
253 type_and_flag = GET_U_1(lisp_hdr->type_and_flag);
254 type = extract_lisp_type(type_and_flag);
255 if ((type != LISP_MAP_REGISTER) && (type != LISP_MAP_NOTIFY))
256 return;
257
258 /* Find if the packet contains xTR and Site-ID data */
259 xtr_present = is_xtr_data_present(type, type_and_flag);
260
261 /* Extract the number of EID records present */
262 auth_data_len = GET_BE_U_2(lisp_hdr->auth_data_len);
263 packet_iterator = (const u_char *)(lisp_hdr);
264 packet_offset = MAP_REGISTER_HDR_LEN;
265 record_count = GET_U_1(lisp_hdr->record_count);
266
267 if (ndo->ndo_vflag) {
268 key_id = GET_BE_U_2(lisp_hdr->key_id);
269 ND_PRINT("\n %u record(s), ", record_count);
270 ND_PRINT("Authentication %s,",
271 tok2str(auth_type, "unknown-type", key_id));
272 hex_print(ndo, "\n Authentication-Data: ", packet_iterator +
273 packet_offset, auth_data_len);
274 } else {
275 ND_PRINT(" %u record(s),", record_count);
276 }
277 packet_offset += auth_data_len;
278
279 if (record_count == 0)
280 goto invalid;
281
282 /* Print all the EID records */
283 while ((length > packet_offset) && (record_count != 0)) {
284 record_count--;
285 ND_TCHECK_LEN(packet_iterator + packet_offset,
286 MAP_REGISTER_EID_LEN);
287 ND_PRINT("\n");
288 lisp_eid = (const lisp_map_register_eid *)
289 ((const u_char *)lisp_hdr + packet_offset);
290 packet_offset += MAP_REGISTER_EID_LEN;
291 mask_len = GET_U_1(lisp_eid->eid_prefix_mask_length);
292 eid_afi = GET_BE_U_2(lisp_eid->eid_prefix_afi);
293 loc_count = GET_U_1(lisp_eid->locator_count);
294
295 if (ndo->ndo_vflag) {
296 ttl = GET_BE_U_4(lisp_eid->ttl);
297 ND_PRINT(" Record TTL %u,", ttl);
298 action_flag(ndo, GET_U_1(lisp_eid->act_auth_inc_res));
299 map_version = GET_BE_U_2(lisp_eid->reserved_and_version) & 0x0FFF;
300 ND_PRINT(" Map Version: %u,", map_version);
301 }
302
303 switch (eid_afi) {
304 case IPv4_AFI:
305 ND_PRINT(" EID %s/%u,",
306 GET_IPADDR_STRING(packet_iterator + packet_offset),
307 mask_len);
308 packet_offset += 4;
309 break;
310 case IPv6_AFI:
311 ND_PRINT(" EID %s/%u,",
312 GET_IP6ADDR_STRING(packet_iterator + packet_offset),
313 mask_len);
314 packet_offset += 16;
315 break;
316 default:
317 /*
318 * No support for LCAF right now.
319 */
320 return;
321 }
322
323 ND_PRINT(" %u locator(s)", loc_count);
324
325 while (loc_count != 0) {
326 loc_count--;
327 ND_TCHECK_LEN(packet_iterator + packet_offset,
328 MAP_REGISTER_LOC_LEN);
329 lisp_loc = (const lisp_map_register_loc *) (packet_iterator + packet_offset);
330 packet_offset += MAP_REGISTER_LOC_LEN;
331 loc_afi = GET_BE_U_2(lisp_loc->locator_afi);
332
333 if (ndo->ndo_vflag)
334 ND_PRINT("\n ");
335
336 switch (loc_afi) {
337 case IPv4_AFI:
338 ND_PRINT(" LOC %s",
339 GET_IPADDR_STRING(packet_iterator +
340 packet_offset));
341 packet_offset += 4;
342 break;
343 case IPv6_AFI:
344 ND_PRINT(" LOC %s",
345 GET_IP6ADDR_STRING(packet_iterator +
346 packet_offset));
347 packet_offset += 16;
348 break;
349 default:
350 break;
351 }
352 if (ndo->ndo_vflag) {
353 ND_PRINT("\n Priority/Weight %u/%u,"
354 " Multicast Priority/Weight %u/%u,",
355 GET_U_1(lisp_loc->priority),
356 GET_U_1(lisp_loc->weight),
357 GET_U_1(lisp_loc->m_priority),
358 GET_U_1(lisp_loc->m_weight));
359 loc_hdr_flag(ndo,
360 GET_BE_U_2(lisp_loc->unused_and_flag));
361 }
362 }
363 }
364
365 /*
366 * Print xTR and Site ID. Handle the fact that the packet could be invalid.
367 * If the xTR_ID_Present bit is not set, and we still have data to display,
368 * show it as hex data.
369 */
370 if (xtr_present) {
371 if (!ND_TTEST_LEN(packet_iterator + packet_offset, 24))
372 goto invalid;
373 hex_print(ndo, "\n xTR-ID: ", packet_iterator + packet_offset, 16);
374 ND_PRINT("\n SITE-ID: %" PRIu64,
375 GET_BE_U_8(packet_iterator + packet_offset + 16));
376 } else {
377 /* Check if packet isn't over yet */
378 if (packet_iterator + packet_offset < ndo->ndo_snapend) {
379 hex_print(ndo, "\n Data: ", packet_iterator + packet_offset,
380 ND_BYTES_AVAILABLE_AFTER(packet_iterator + packet_offset));
381 }
382 }
383 return;
384 trunc:
385 nd_print_trunc(ndo);
386 return;
387 invalid:
388 nd_print_invalid(ndo);
389 }
390
391 static uint8_t
392 extract_lisp_type(uint8_t lisp_hdr_flags)
393 {
394 return (lisp_hdr_flags) >> TYPE_INDEX;
395 }
396
397 static uint8_t
398 is_xtr_data_present(uint8_t type, uint8_t lisp_hdr_flags)
399 {
400 uint8_t xtr_present = 0;
401
402 if (type == LISP_MAP_REGISTER)
403 xtr_present = (lisp_hdr_flags) & LISP_MAP_REGISTER_IBIT_MASK;
404 else if (type == LISP_MAP_NOTIFY)
405 xtr_present = (lisp_hdr_flags) & LISP_MAP_NOTIFY_IBIT_MASK;
406
407 return xtr_present;
408 }
409
410 static void lisp_hdr_flag(netdissect_options *ndo, const lisp_map_register_hdr *lisp_hdr)
411 {
412 uint8_t type = extract_lisp_type(GET_U_1(lisp_hdr->type_and_flag));
413
414 if (!ndo->ndo_vflag) {
415 ND_PRINT("%s,", tok2str(lisp_type, "unknown-type-%u", type));
416 return;
417 } else {
418 ND_PRINT("%s,", tok2str(lisp_type, "unknown-type-%u", type));
419 }
420
421 if (type == LISP_MAP_REGISTER) {
422 ND_PRINT(" flags [%s],", bittok2str(map_register_hdr_flag,
423 "none", GET_BE_U_4(lisp_hdr)));
424 } else if (type == LISP_MAP_NOTIFY) {
425 ND_PRINT(" flags [%s],", bittok2str(map_notify_hdr_flag,
426 "none", GET_BE_U_4(lisp_hdr)));
427 }
428 }
429
430 static void action_flag(netdissect_options *ndo, uint8_t act_auth_inc_res)
431 {
432 uint8_t action;
433 uint8_t authoritative;
434
435 authoritative = ((act_auth_inc_res >> 4) & 1);
436
437 if (authoritative)
438 ND_PRINT(" Authoritative,");
439 else
440 ND_PRINT(" Non-Authoritative,");
441
442 action = act_auth_inc_res >> 5;
443 ND_PRINT(" %s,", tok2str(lisp_eid_action, "unknown", action));
444 }
445
446 static void loc_hdr_flag(netdissect_options *ndo, uint16_t flag)
447 {
448 ND_PRINT(" flags [%s],", bittok2str(lisp_loc_flag, "none", flag));
449 }
450