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