]> The Tcpdump Group git mirrors - tcpdump/blob - print-lisp.c
327e99e24924e7a37c78e1fc8d749f126ec4f61f
[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 /*
30 * tcpdump filter for LISP - Locator/Identifier Separation Protocol
31 * RFC 6830
32 *
33 *
34 * The Map-Register message format is:
35 *
36 * 0 1 2 3
37 * 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
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * |Type=3 |P|S|I|R| Reserved |M| Record Count |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Nonce . . . |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | . . . Nonce |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | Key ID | Authentication Data Length |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * ~ Authentication Data ~
48 * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * | | Record TTL |
50 * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * R | Locator Count | EID mask-len | ACT |A| Reserved |
52 * e +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * c | Rsvd | Map-Version Number | EID-Prefix-AFI |
54 * o +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * r | EID-Prefix |
56 * d +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 * | /| Priority | Weight | M Priority | M Weight |
58 * | L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 * | o | Unused Flags |L|p|R| Loc-AFI |
60 * | c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 * | \| Locator |
62 * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 *
64 *
65 * The Map-Notify message format is:
66 *
67 * 0 1 2 3
68 * 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
69 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 * |Type=4 |I|R| Reserved | Record Count |
71 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72 * | Nonce . . . |
73 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 * | . . . Nonce |
75 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76 * | Key ID | Authentication Data Length |
77 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78 * ~ Authentication Data ~
79 * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80 * | | Record TTL |
81 * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82 * R | Locator Count | EID mask-len | ACT |A| Reserved |
83 * e +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
84 * c | Rsvd | Map-Version Number | EID-Prefix-AFI |
85 * o +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 * r | EID-Prefix |
87 * d +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
88 * | /| Priority | Weight | M Priority | M Weight |
89 * | L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 * | o | Unused Flags |L|p|R| Loc-AFI |
91 * | c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 * | \| Locator |
93 * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 */
95
96
97 #ifdef HAVE_CONFIG_H
98 #include "config.h"
99 #endif
100
101 #include <netdissect-stdinc.h>
102 #include <netdissect.h>
103 #include <string.h>
104 #include <stdlib.h>
105
106 #include "ip.h"
107 #ifdef INET6
108 #include "ip6.h"
109 #endif
110
111 #include "extract.h"
112 #include "interface.h"
113 #include "addrtoname.h"
114
115 #define IPv4_AFI 1
116 #define IPv6_AFI 2
117 #define TYPE_INDEX 4
118 #define LISP_MAP_NOTIFY_IBIT_MASK 8
119 #define LISP_MAP_REGISTER_IBIT_MASK 2
120
121 enum {
122 LISP_MAP_REQUEST = 1,
123 LISP_MAP_REPLY,
124 LISP_MAP_REGISTER,
125 LISP_MAP_NOTIFY,
126 LISP_ENCAPSULATED_CONTROL_MESSAGE = 8
127 };
128
129 enum {
130 LISP_AUTH_NONE,
131 LISP_AUTH_SHA1,
132 LISP_AUTH_SHA256
133 };
134
135 static const struct tok lisp_type [] = {
136 { 0, "LISP-Reserved" },
137 { 1, "LISP-Map-Request" },
138 { 2, "LISP-Map-Reply" },
139 { 3, "LISP-Map-Register" },
140 { 4, "LISP-Map-Notify" },
141 { 8, "LISP-Encapsulated-Contol-Message" },
142 { 0, NULL }
143 };
144
145 /*
146 * P-Bit : Request for Proxy Map-Reply from the MS/MR
147 * S-Bit : Security Enhancement. ETR is LISP-SEC enabled. draft-ietf-lisp-sec
148 * I-Bit : 128 bit xTR-ID and 64 bit Site-ID present.
149 * xTR-ID and Site-ID help in differentiation of xTRs in multi xTR
150 * and multi Site deployment scenarios.
151 * R-Bit : Built for a Reencapsulating-Tunnel-Router. Used in Traffic
152 * Engineering and Service Chaining
153 */
154 static const struct tok map_register_hdr_flag[] = {
155 { 0x08000000, "P-Proxy-Map-Reply" },
156 { 0x04000000, "S-LISP-SEC-Capable" },
157 { 0x02000000, "I-xTR-ID-Present" },
158 { 0x01000000, "R-Build-For-RTR" },
159 { 0x00000100, "M-Want-Map-Notify" },
160 { 0, NULL }
161 };
162
163 static const struct tok map_notify_hdr_flag[] = {
164 { 0x08000000, "I-xTR-ID-Present" },
165 { 0x04000000, "R-Build-For-RTR" },
166 { 0, NULL }
167 };
168
169 static const struct tok auth_type[] = {
170 { LISP_AUTH_NONE, "None" },
171 { LISP_AUTH_SHA1, "SHA1" },
172 { LISP_AUTH_SHA256, "SHA256" },
173 { 0, NULL}
174 };
175
176 static const struct tok lisp_eid_action[] = {
177 { 0, "No-Action" },
178 { 1, "Natively-Forward" },
179 { 2, "Send-Map-Request" },
180 { 3, "Drop" },
181 { 0, NULL}
182 };
183
184 static const struct tok lisp_loc_flag[] = {
185 { 0x0004, "Local-Locator" },
186 { 0x0002, "RLoc-Probed" },
187 { 0x0001, "Reachable" },
188 { 0, NULL }
189 };
190
191 typedef struct map_register_hdr {
192 uint8_t type_and_flag;
193 uint8_t reserved;
194 uint8_t reserved_and_flag2;
195 uint8_t record_count;
196 uint64_t nonce;
197 uint16_t key_id;
198 uint16_t auth_data_len;
199 } UNALIGNED lisp_map_register_hdr;
200
201 #define MAP_REGISTER_HDR_LEN sizeof(lisp_map_register_hdr)
202
203 typedef struct map_register_eid {
204 uint32_t ttl;
205 uint8_t locator_count;
206 uint8_t eid_prefix_mask_length;
207 uint8_t act_auth_inc_res;
208 uint8_t reserved;
209 uint8_t reserved_version_hi;
210 uint8_t version_low;
211 uint16_t eid_prefix_afi;
212 } UNALIGNED lisp_map_register_eid;
213
214 #define MAP_REGISTER_EID_LEN sizeof(lisp_map_register_eid)
215
216 typedef struct map_register_loc {
217 uint8_t priority;
218 uint8_t weight;
219 uint8_t m_priority;
220 uint8_t m_weight;
221 uint16_t unused_and_flag;
222 uint16_t locator_afi;
223 } UNALIGNED lisp_map_register_loc;
224
225 #define MAP_REGISTER_LOC_LEN sizeof(lisp_map_register_loc)
226
227 static inline uint8_t extract_lisp_type(uint8_t);
228 static inline uint8_t is_xtr_data_present(uint8_t , uint8_t);
229 static void lisp_hdr_flag(netdissect_options *, const lisp_map_register_hdr *);
230 static void action_flag(netdissect_options *, uint8_t);
231 static void loc_hdr_flag(netdissect_options *, uint16_t);
232
233 void lisp_print(netdissect_options *ndo, const u_char *bp, u_int length)
234 {
235 uint8_t type;
236 uint8_t mask_len;
237 uint8_t loc_count;
238 uint8_t xtr_present;
239 uint8_t record_count;
240 uint16_t key_id;
241 uint16_t eid_afi;
242 uint16_t loc_afi;
243 uint16_t map_version;
244 uint16_t packet_offset;
245 uint16_t auth_data_len;
246 uint32_t ttl;
247 const u_char *packet_iterator;
248 const u_char *loc_ip_pointer;
249 const lisp_map_register_hdr *lisp_hdr;
250 const lisp_map_register_eid *lisp_eid;
251 const lisp_map_register_loc *lisp_loc;
252
253 /* Check if enough bytes for header are available */
254 ND_TCHECK2(*bp, MAP_REGISTER_HDR_LEN);
255 lisp_hdr = (const lisp_map_register_hdr *) bp;
256 lisp_hdr_flag(ndo, lisp_hdr);
257 /* Supporting only MAP NOTIFY and MAP REGISTER LISP packets */
258 type = extract_lisp_type(lisp_hdr->type_and_flag);
259 if ((type != LISP_MAP_REGISTER) && (type != LISP_MAP_NOTIFY))
260 return;
261
262 /* Find if the packet contains xTR and Site-ID data */
263 xtr_present = is_xtr_data_present(type, lisp_hdr->type_and_flag);
264
265 /* Extract the number of EID records present */
266 auth_data_len = EXTRACT_16BITS(&lisp_hdr->auth_data_len);
267 packet_iterator = (const u_char *)(lisp_hdr);
268 packet_offset = MAP_REGISTER_HDR_LEN;
269 record_count = lisp_hdr->record_count;
270
271 if (ndo->ndo_vflag) {
272 key_id = EXTRACT_16BITS(&lisp_hdr->key_id);
273 ND_PRINT((ndo, "\n %u record(s), ", record_count));
274 ND_PRINT((ndo, "Authentication %s,",
275 tok2str(auth_type, "unknown-type", key_id)));
276 hex_print(ndo, "\n Authentication-Data: ", packet_iterator +
277 packet_offset, auth_data_len);
278 } else {
279 ND_PRINT((ndo, " %u record(s),", record_count));
280 }
281 packet_offset += auth_data_len;
282
283 if (record_count == 0)
284 goto malformed;
285
286 /* Print all the EID records */
287 while ((length > packet_offset) && (record_count--)) {
288
289 ND_TCHECK2(*(packet_iterator + packet_offset), MAP_REGISTER_EID_LEN);
290 ND_PRINT((ndo, "\n"));
291 lisp_eid = (const lisp_map_register_eid *)
292 ((const u_char *)lisp_hdr + packet_offset);
293 packet_offset += MAP_REGISTER_EID_LEN;
294 mask_len = lisp_eid->eid_prefix_mask_length;
295 eid_afi = EXTRACT_16BITS(&lisp_eid->eid_prefix_afi);
296 loc_count = lisp_eid->locator_count;
297
298 if (ndo->ndo_vflag) {
299 ttl = EXTRACT_32BITS(&lisp_eid->ttl);
300 ND_PRINT((ndo, " Record TTL %u,", ttl));
301 action_flag(ndo, lisp_eid->act_auth_inc_res);
302 map_version = (((lisp_eid->reserved_version_hi) & 15 ) * 255) +
303 lisp_eid->version_low;
304 ND_PRINT((ndo, " Map Version: %u,", map_version));
305 }
306
307 switch (eid_afi) {
308 case IPv4_AFI:
309 ND_TCHECK2(*(packet_iterator + packet_offset), 4);
310 ND_PRINT((ndo, " EID %s/%u,", getname(ndo,
311 packet_iterator + packet_offset), mask_len));
312 packet_offset += 4;
313 break;
314 case IPv6_AFI:
315 ND_TCHECK2(*(packet_iterator + packet_offset), 16);
316 #ifdef INET6
317 ND_PRINT((ndo, " EID %s/%u,", getname6(ndo,
318 packet_iterator + packet_offset), mask_len));
319 #else
320 ND_PRINT((ndo, " IPv6 EID,"));
321 #endif
322 packet_offset += 16;
323 break;
324 default:
325 /*
326 * No support for LCAF right now.
327 */
328 return;
329 break;
330 }
331
332 ND_PRINT((ndo, " %u locator(s)", loc_count));
333
334 while (loc_count--) {
335 ND_TCHECK2(*(packet_iterator + packet_offset), MAP_REGISTER_LOC_LEN);
336 lisp_loc = (const lisp_map_register_loc *) (packet_iterator + packet_offset);
337 loc_ip_pointer = (const u_char *) (lisp_loc + 1);
338 packet_offset += MAP_REGISTER_LOC_LEN;
339 loc_afi = EXTRACT_16BITS(&lisp_loc->locator_afi);
340
341 if (ndo->ndo_vflag)
342 ND_PRINT((ndo, "\n "));
343
344 switch (loc_afi) {
345 case IPv4_AFI:
346 ND_TCHECK2(*(packet_iterator + packet_offset), 4);
347 ND_PRINT((ndo, " LOC %s", getname(ndo, loc_ip_pointer)));
348 packet_offset += 4;
349 break;
350 case IPv6_AFI:
351 ND_TCHECK2(*(packet_iterator + packet_offset), 16);
352 #ifdef INET6
353 ND_PRINT((ndo, " LOC %s", getname6(ndo, loc_ip_pointer)));
354 #else
355 ND_PRINT((ndo, " LOC IPv6"));
356 #endif
357 packet_offset += 16;
358 break;
359 default:
360 break;
361 }
362 if (ndo->ndo_vflag) {
363 ND_PRINT((ndo, "\n Priority/Weight %u/%u,"
364 " Multicast Priority/Weight %u/%u,",
365 lisp_loc->priority, lisp_loc->weight,
366 lisp_loc->m_priority, lisp_loc->m_weight));
367 loc_hdr_flag(ndo, EXTRACT_16BITS(&lisp_loc->unused_and_flag));
368 }
369 }
370 }
371
372 /*
373 * Print xTR and Site ID. Handle the fact that the packet could be malformed.
374 * If the xTR_ID_Present bit is not set, and we still have data to display,
375 * show it as hex data.
376 */
377 if (xtr_present) {
378 if (!ND_TTEST2(*(packet_iterator + packet_offset), 24))
379 goto malformed;
380 hex_print_with_offset(ndo, "\n xTR-ID: ", packet_iterator + packet_offset, 16, 0);
381 ND_PRINT((ndo, "\n SITE-ID: %" PRIu64,
382 EXTRACT_64BITS(packet_iterator + packet_offset + 16)));
383 } else {
384 /* Check if packet isn't over yet */
385 if (packet_iterator + packet_offset < ndo->ndo_snapend) {
386 hex_print_with_offset(ndo, "\n Data: ", packet_iterator + packet_offset,
387 (ndo->ndo_snapend - (packet_iterator + packet_offset)), 0);
388 }
389 }
390 return;
391 trunc:
392 ND_PRINT((ndo, "\n [|LISP]"));
393 return;
394 malformed:
395 ND_PRINT((ndo, "\n (malformed-packet)"));
396 return;
397 }
398
399
400 static inline uint8_t extract_lisp_type(uint8_t lisp_hdr_flags)
401 {
402 return (lisp_hdr_flags) >> TYPE_INDEX;
403 }
404
405 static inline uint8_t is_xtr_data_present(uint8_t type, uint8_t lisp_hdr_flags)
406 {
407 uint8_t xtr_present;
408
409 if (type == LISP_MAP_REGISTER)
410 xtr_present = (lisp_hdr_flags) & LISP_MAP_REGISTER_IBIT_MASK;
411 else if (type == LISP_MAP_NOTIFY)
412 xtr_present = (lisp_hdr_flags) & LISP_MAP_NOTIFY_IBIT_MASK;
413
414 return xtr_present;
415 }
416
417 static void lisp_hdr_flag(netdissect_options *ndo, const lisp_map_register_hdr *lisp_hdr)
418 {
419 uint8_t type = extract_lisp_type(lisp_hdr->type_and_flag);
420
421 if (!ndo->ndo_vflag) {
422 ND_PRINT((ndo, "%s,", tok2str(lisp_type, "unknown-type-%u", type)));
423 return;
424 } else {
425 ND_PRINT((ndo, "%s,", tok2str(lisp_type, "unknown-type-%u", type)));
426 }
427
428 if (type == LISP_MAP_REGISTER) {
429 ND_PRINT((ndo, " flags [%s],", bittok2str(map_register_hdr_flag,
430 "none", EXTRACT_32BITS(lisp_hdr))));
431 } else if (type == LISP_MAP_NOTIFY) {
432 ND_PRINT((ndo, " flags [%s],", bittok2str(map_notify_hdr_flag,
433 "none", EXTRACT_32BITS(lisp_hdr))));
434 }
435
436 return;
437 }
438
439
440 static void action_flag(netdissect_options *ndo, uint8_t act_auth_inc_res)
441 {
442 uint8_t action;
443 uint8_t authoritative;
444
445 authoritative = ((act_auth_inc_res >> 4) & 1);
446
447 if (authoritative)
448 ND_PRINT((ndo, " Authoritative,"));
449 else
450 ND_PRINT((ndo, " Non-Authoritative,"));
451
452 action = act_auth_inc_res >> 5;
453 ND_PRINT((ndo, " %s,", tok2str(lisp_eid_action, "unknown", action)));
454 }
455
456
457 static void loc_hdr_flag(netdissect_options *ndo, uint16_t flag)
458 {
459 ND_PRINT((ndo, " flags [%s],", bittok2str(lisp_loc_flag, "none", flag)));
460 }
461