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