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