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