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