]> The Tcpdump Group git mirrors - tcpdump/blob - print-lisp.c
Include <string.h> only if required.
[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 #ifdef HAVE_CONFIG_H
98 #include <config.h>
99 #endif
100
101 #include "netdissect-stdinc.h"
102 #include "netdissect.h"
103 #include <stdlib.h>
104
105 #include "ip.h"
106 #include "ip6.h"
107
108 #include "extract.h"
109 #include "addrtoname.h"
110
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 nd_uint8_t type_and_flag;
190 nd_uint8_t reserved;
191 nd_uint8_t reserved_and_flag2;
192 nd_uint8_t record_count;
193 nd_uint64_t nonce;
194 nd_uint16_t key_id;
195 nd_uint16_t auth_data_len;
196 } lisp_map_register_hdr;
197
198 #define MAP_REGISTER_HDR_LEN sizeof(lisp_map_register_hdr)
199
200 typedef struct map_register_eid {
201 nd_uint32_t ttl;
202 nd_uint8_t locator_count;
203 nd_uint8_t eid_prefix_mask_length;
204 nd_uint8_t act_auth_inc_res;
205 nd_uint8_t reserved;
206 nd_uint16_t reserved_and_version;
207 nd_uint16_t eid_prefix_afi;
208 } lisp_map_register_eid;
209
210 #define MAP_REGISTER_EID_LEN sizeof(lisp_map_register_eid)
211
212 typedef struct map_register_loc {
213 nd_uint8_t priority;
214 nd_uint8_t weight;
215 nd_uint8_t m_priority;
216 nd_uint8_t m_weight;
217 nd_uint16_t unused_and_flag;
218 nd_uint16_t locator_afi;
219 } lisp_map_register_loc;
220
221 #define MAP_REGISTER_LOC_LEN sizeof(lisp_map_register_loc)
222
223 static uint8_t extract_lisp_type(uint8_t);
224 static uint8_t is_xtr_data_present(uint8_t, uint8_t);
225 static void lisp_hdr_flag(netdissect_options *, const lisp_map_register_hdr *);
226 static void action_flag(netdissect_options *, uint8_t);
227 static void loc_hdr_flag(netdissect_options *, uint16_t);
228
229 void
230 lisp_print(netdissect_options *ndo, const u_char *bp, u_int length)
231 {
232 uint8_t type_and_flag;
233 uint8_t type;
234 uint8_t mask_len;
235 uint8_t loc_count;
236 uint8_t xtr_present;
237 uint8_t record_count;
238 uint16_t key_id;
239 uint16_t eid_afi;
240 uint16_t loc_afi;
241 uint16_t map_version;
242 uint16_t packet_offset;
243 uint16_t auth_data_len;
244 uint32_t ttl;
245 const u_char *packet_iterator;
246 const u_char *loc_ip_pointer;
247 const lisp_map_register_hdr *lisp_hdr;
248 const lisp_map_register_eid *lisp_eid;
249 const lisp_map_register_loc *lisp_loc;
250
251 ndo->ndo_protocol = "lisp";
252 /* Check if enough bytes for header are available */
253 ND_TCHECK_LEN(bp, MAP_REGISTER_HDR_LEN);
254 lisp_hdr = (const lisp_map_register_hdr *) bp;
255 lisp_hdr_flag(ndo, lisp_hdr);
256 /* Supporting only MAP NOTIFY and MAP REGISTER LISP packets */
257 type_and_flag = GET_U_1(lisp_hdr->type_and_flag);
258 type = extract_lisp_type(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, type_and_flag);
264
265 /* Extract the number of EID records present */
266 auth_data_len = GET_BE_U_2(lisp_hdr->auth_data_len);
267 packet_iterator = (const u_char *)(lisp_hdr);
268 packet_offset = MAP_REGISTER_HDR_LEN;
269 record_count = GET_U_1(lisp_hdr->record_count);
270
271 if (ndo->ndo_vflag) {
272 key_id = GET_BE_U_2(lisp_hdr->key_id);
273 ND_PRINT("\n %u record(s), ", record_count);
274 ND_PRINT("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(" %u record(s),", record_count);
280 }
281 packet_offset += auth_data_len;
282
283 if (record_count == 0)
284 goto invalid;
285
286 /* Print all the EID records */
287 while ((length > packet_offset) && (record_count--)) {
288
289 ND_TCHECK_LEN(packet_iterator + packet_offset,
290 MAP_REGISTER_EID_LEN);
291 ND_PRINT("\n");
292 lisp_eid = (const lisp_map_register_eid *)
293 ((const u_char *)lisp_hdr + packet_offset);
294 packet_offset += MAP_REGISTER_EID_LEN;
295 mask_len = GET_U_1(lisp_eid->eid_prefix_mask_length);
296 eid_afi = GET_BE_U_2(lisp_eid->eid_prefix_afi);
297 loc_count = GET_U_1(lisp_eid->locator_count);
298
299 if (ndo->ndo_vflag) {
300 ttl = GET_BE_U_4(lisp_eid->ttl);
301 ND_PRINT(" Record TTL %u,", ttl);
302 action_flag(ndo, GET_U_1(lisp_eid->act_auth_inc_res));
303 map_version = GET_BE_U_2(lisp_eid->reserved_and_version) & 0x0FFF;
304 ND_PRINT(" Map Version: %u,", map_version);
305 }
306
307 switch (eid_afi) {
308 case IPv4_AFI:
309 ND_PRINT(" EID %s/%u,",
310 GET_IPADDR_STRING(packet_iterator + packet_offset),
311 mask_len);
312 packet_offset += 4;
313 break;
314 case IPv6_AFI:
315 ND_PRINT(" EID %s/%u,",
316 GET_IP6ADDR_STRING(packet_iterator + packet_offset),
317 mask_len);
318 packet_offset += 16;
319 break;
320 default:
321 /*
322 * No support for LCAF right now.
323 */
324 return;
325 break;
326 }
327
328 ND_PRINT(" %u locator(s)", loc_count);
329
330 while (loc_count--) {
331 ND_TCHECK_LEN(packet_iterator + packet_offset,
332 MAP_REGISTER_LOC_LEN);
333 lisp_loc = (const lisp_map_register_loc *) (packet_iterator + packet_offset);
334 loc_ip_pointer = (const u_char *) (lisp_loc + 1);
335 packet_offset += MAP_REGISTER_LOC_LEN;
336 loc_afi = GET_BE_U_2(lisp_loc->locator_afi);
337
338 if (ndo->ndo_vflag)
339 ND_PRINT("\n ");
340
341 switch (loc_afi) {
342 case IPv4_AFI:
343 ND_TCHECK_4(packet_iterator + packet_offset);
344 ND_PRINT(" LOC %s", GET_IPADDR_STRING(loc_ip_pointer));
345 packet_offset += 4;
346 break;
347 case IPv6_AFI:
348 ND_TCHECK_16(packet_iterator + packet_offset);
349 ND_PRINT(" LOC %s", GET_IP6ADDR_STRING(loc_ip_pointer));
350 packet_offset += 16;
351 break;
352 default:
353 break;
354 }
355 if (ndo->ndo_vflag) {
356 ND_PRINT("\n Priority/Weight %u/%u,"
357 " Multicast Priority/Weight %u/%u,",
358 GET_U_1(lisp_loc->priority),
359 GET_U_1(lisp_loc->weight),
360 GET_U_1(lisp_loc->m_priority),
361 GET_U_1(lisp_loc->m_weight));
362 loc_hdr_flag(ndo,
363 GET_BE_U_2(lisp_loc->unused_and_flag));
364 }
365 }
366 }
367
368 /*
369 * Print xTR and Site ID. Handle the fact that the packet could be invalid.
370 * If the xTR_ID_Present bit is not set, and we still have data to display,
371 * show it as hex data.
372 */
373 if (xtr_present) {
374 if (!ND_TTEST_LEN(packet_iterator + packet_offset, 24))
375 goto invalid;
376 hex_print(ndo, "\n xTR-ID: ", packet_iterator + packet_offset, 16);
377 ND_PRINT("\n SITE-ID: %" PRIu64,
378 GET_BE_U_8(packet_iterator + packet_offset + 16));
379 } else {
380 /* Check if packet isn't over yet */
381 if (packet_iterator + packet_offset < ndo->ndo_snapend) {
382 hex_print(ndo, "\n Data: ", packet_iterator + packet_offset,
383 ND_BYTES_AVAILABLE_AFTER(packet_iterator + packet_offset));
384 }
385 }
386 return;
387 trunc:
388 nd_print_trunc(ndo);
389 return;
390 invalid:
391 nd_print_invalid(ndo);
392 }
393
394 static uint8_t
395 extract_lisp_type(uint8_t lisp_hdr_flags)
396 {
397 return (lisp_hdr_flags) >> TYPE_INDEX;
398 }
399
400 static uint8_t
401 is_xtr_data_present(uint8_t type, uint8_t lisp_hdr_flags)
402 {
403 uint8_t xtr_present = 0;
404
405 if (type == LISP_MAP_REGISTER)
406 xtr_present = (lisp_hdr_flags) & LISP_MAP_REGISTER_IBIT_MASK;
407 else if (type == LISP_MAP_NOTIFY)
408 xtr_present = (lisp_hdr_flags) & LISP_MAP_NOTIFY_IBIT_MASK;
409
410 return xtr_present;
411 }
412
413 static void lisp_hdr_flag(netdissect_options *ndo, const lisp_map_register_hdr *lisp_hdr)
414 {
415 uint8_t type = extract_lisp_type(GET_U_1(lisp_hdr->type_and_flag));
416
417 if (!ndo->ndo_vflag) {
418 ND_PRINT("%s,", tok2str(lisp_type, "unknown-type-%u", type));
419 return;
420 } else {
421 ND_PRINT("%s,", tok2str(lisp_type, "unknown-type-%u", type));
422 }
423
424 if (type == LISP_MAP_REGISTER) {
425 ND_PRINT(" flags [%s],", bittok2str(map_register_hdr_flag,
426 "none", GET_BE_U_4(lisp_hdr)));
427 } else if (type == LISP_MAP_NOTIFY) {
428 ND_PRINT(" flags [%s],", bittok2str(map_notify_hdr_flag,
429 "none", GET_BE_U_4(lisp_hdr)));
430 }
431 }
432
433 static void action_flag(netdissect_options *ndo, uint8_t act_auth_inc_res)
434 {
435 uint8_t action;
436 uint8_t authoritative;
437
438 authoritative = ((act_auth_inc_res >> 4) & 1);
439
440 if (authoritative)
441 ND_PRINT(" Authoritative,");
442 else
443 ND_PRINT(" Non-Authoritative,");
444
445 action = act_auth_inc_res >> 5;
446 ND_PRINT(" %s,", tok2str(lisp_eid_action, "unknown", action));
447 }
448
449 static void loc_hdr_flag(netdissect_options *ndo, uint16_t flag)
450 {
451 ND_PRINT(" flags [%s],", bittok2str(lisp_loc_flag, "none", flag));
452 }
453