print-lisp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. /* \summary: - Locator/Identifier Separation Protocol (LISP) printer */
  29. /*
  30. * specification: RFC 6830
  31. *
  32. *
  33. * The Map-Register message format is:
  34. *
  35. * 0 1 2 3
  36. * 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
  37. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  38. * |Type=3 |P|S|I|R| Reserved |M| Record Count |
  39. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  40. * | Nonce . . . |
  41. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  42. * | . . . Nonce |
  43. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  44. * | Key ID | Authentication Data Length |
  45. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  46. * ~ Authentication Data ~
  47. * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  48. * | | Record TTL |
  49. * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  50. * R | Locator Count | EID mask-len | ACT |A| Reserved |
  51. * e +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  52. * c | Rsvd | Map-Version Number | EID-Prefix-AFI |
  53. * o +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  54. * r | EID-Prefix |
  55. * d +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  56. * | /| Priority | Weight | M Priority | M Weight |
  57. * | L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  58. * | o | Unused Flags |L|p|R| Loc-AFI |
  59. * | c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  60. * | \| Locator |
  61. * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  62. *
  63. *
  64. * The Map-Notify message format is:
  65. *
  66. * 0 1 2 3
  67. * 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
  68. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  69. * |Type=4 |I|R| Reserved | Record Count |
  70. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  71. * | Nonce . . . |
  72. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  73. * | . . . Nonce |
  74. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  75. * | Key ID | Authentication Data Length |
  76. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  77. * ~ Authentication Data ~
  78. * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  79. * | | Record TTL |
  80. * | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  81. * R | Locator Count | EID mask-len | ACT |A| Reserved |
  82. * e +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  83. * c | Rsvd | Map-Version Number | EID-Prefix-AFI |
  84. * o +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  85. * r | EID-Prefix |
  86. * d +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  87. * | /| Priority | Weight | M Priority | M Weight |
  88. * | L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  89. * | o | Unused Flags |L|p|R| Loc-AFI |
  90. * | c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  91. * | \| Locator |
  92. * +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  93. */
  94. #ifdef HAVE_CONFIG_H
  95. #include "config.h"
  96. #endif
  97. #include <netdissect-stdinc.h>
  98. #include <netdissect.h>
  99. #include <string.h>
  100. #include <stdlib.h>
  101. #include "ip.h"
  102. #include "ip6.h"
  103. #include "extract.h"
  104. #include "addrtoname.h"
  105. static const char tstr[] = " [|LISP]";
  106. #define IPv4_AFI 1
  107. #define IPv6_AFI 2
  108. #define TYPE_INDEX 4
  109. #define LISP_MAP_NOTIFY_IBIT_MASK 8
  110. #define LISP_MAP_REGISTER_IBIT_MASK 2
  111. enum {
  112. LISP_MAP_REQUEST = 1,
  113. LISP_MAP_REPLY,
  114. LISP_MAP_REGISTER,
  115. LISP_MAP_NOTIFY,
  116. LISP_ENCAPSULATED_CONTROL_MESSAGE = 8
  117. };
  118. enum {
  119. LISP_AUTH_NONE,
  120. LISP_AUTH_SHA1,
  121. LISP_AUTH_SHA256
  122. };
  123. static const struct tok lisp_type [] = {
  124. { 0, "LISP-Reserved" },
  125. { 1, "LISP-Map-Request" },
  126. { 2, "LISP-Map-Reply" },
  127. { 3, "LISP-Map-Register" },
  128. { 4, "LISP-Map-Notify" },
  129. { 8, "LISP-Encapsulated-Contol-Message" },
  130. { 0, NULL }
  131. };
  132. /*
  133. * P-Bit : Request for Proxy Map-Reply from the MS/MR
  134. * S-Bit : Security Enhancement. ETR is LISP-SEC enabled. draft-ietf-lisp-sec
  135. * I-Bit : 128 bit xTR-ID and 64 bit Site-ID present.
  136. * xTR-ID and Site-ID help in differentiation of xTRs in multi xTR
  137. * and multi Site deployment scenarios.
  138. * R-Bit : Built for a Reencapsulating-Tunnel-Router. Used in Traffic
  139. * Engineering and Service Chaining
  140. */
  141. static const struct tok map_register_hdr_flag[] = {
  142. { 0x08000000, "P-Proxy-Map-Reply" },
  143. { 0x04000000, "S-LISP-SEC-Capable" },
  144. { 0x02000000, "I-xTR-ID-Present" },
  145. { 0x01000000, "R-Build-For-RTR" },
  146. { 0x00000100, "M-Want-Map-Notify" },
  147. { 0, NULL }
  148. };
  149. static const struct tok map_notify_hdr_flag[] = {
  150. { 0x08000000, "I-xTR-ID-Present" },
  151. { 0x04000000, "R-Build-For-RTR" },
  152. { 0, NULL }
  153. };
  154. static const struct tok auth_type[] = {
  155. { LISP_AUTH_NONE, "None" },
  156. { LISP_AUTH_SHA1, "SHA1" },
  157. { LISP_AUTH_SHA256, "SHA256" },
  158. { 0, NULL}
  159. };
  160. static const struct tok lisp_eid_action[] = {
  161. { 0, "No-Action" },
  162. { 1, "Natively-Forward" },
  163. { 2, "Send-Map-Request" },
  164. { 3, "Drop" },
  165. { 0, NULL}
  166. };
  167. static const struct tok lisp_loc_flag[] = {
  168. { 0x0004, "Local-Locator" },
  169. { 0x0002, "RLoc-Probed" },
  170. { 0x0001, "Reachable" },
  171. { 0, NULL }
  172. };
  173. typedef struct map_register_hdr {
  174. nd_uint8_t type_and_flag;
  175. nd_uint8_t reserved;
  176. nd_uint8_t reserved_and_flag2;
  177. nd_uint8_t record_count;
  178. nd_uint64_t nonce;
  179. nd_uint16_t key_id;
  180. nd_uint16_t auth_data_len;
  181. } lisp_map_register_hdr;
  182. #define MAP_REGISTER_HDR_LEN sizeof(lisp_map_register_hdr)
  183. typedef struct map_register_eid {
  184. nd_uint32_t ttl;
  185. nd_uint8_t locator_count;
  186. nd_uint8_t eid_prefix_mask_length;
  187. nd_uint8_t act_auth_inc_res;
  188. nd_uint8_t reserved;
  189. nd_uint8_t reserved_version_hi;
  190. nd_uint8_t version_low;
  191. nd_uint16_t eid_prefix_afi;
  192. } lisp_map_register_eid;
  193. #define MAP_REGISTER_EID_LEN sizeof(lisp_map_register_eid)
  194. typedef struct map_register_loc {
  195. nd_uint8_t priority;
  196. nd_uint8_t weight;
  197. nd_uint8_t m_priority;
  198. nd_uint8_t m_weight;
  199. nd_uint16_t unused_and_flag;
  200. nd_uint16_t locator_afi;
  201. } lisp_map_register_loc;
  202. #define MAP_REGISTER_LOC_LEN sizeof(lisp_map_register_loc)
  203. static inline uint8_t extract_lisp_type(uint8_t);
  204. static inline uint8_t is_xtr_data_present(uint8_t , uint8_t);
  205. static void lisp_hdr_flag(netdissect_options *, const lisp_map_register_hdr *);
  206. static void action_flag(netdissect_options *, uint8_t);
  207. static void loc_hdr_flag(netdissect_options *, uint16_t);
  208. void lisp_print(netdissect_options *ndo, const u_char *bp, u_int length)
  209. {
  210. uint8_t type;
  211. uint8_t mask_len;
  212. uint8_t loc_count;
  213. uint8_t xtr_present;
  214. uint8_t record_count;
  215. uint16_t key_id;
  216. uint16_t eid_afi;
  217. uint16_t loc_afi;
  218. uint16_t map_version;
  219. uint16_t packet_offset;
  220. uint16_t auth_data_len;
  221. uint32_t ttl;
  222. const u_char *packet_iterator;
  223. const u_char *loc_ip_pointer;
  224. const lisp_map_register_hdr *lisp_hdr;
  225. const lisp_map_register_eid *lisp_eid;
  226. const lisp_map_register_loc *lisp_loc;
  227. /* Check if enough bytes for header are available */
  228. ND_TCHECK2(*bp, MAP_REGISTER_HDR_LEN);
  229. lisp_hdr = (const lisp_map_register_hdr *) bp;
  230. lisp_hdr_flag(ndo, lisp_hdr);
  231. /* Supporting only MAP NOTIFY and MAP REGISTER LISP packets */
  232. type = extract_lisp_type(lisp_hdr->type_and_flag);
  233. if ((type != LISP_MAP_REGISTER) && (type != LISP_MAP_NOTIFY))
  234. return;
  235. /* Find if the packet contains xTR and Site-ID data */
  236. xtr_present = is_xtr_data_present(type, lisp_hdr->type_and_flag);
  237. /* Extract the number of EID records present */
  238. auth_data_len = EXTRACT_16BITS(&lisp_hdr->auth_data_len);
  239. packet_iterator = (const u_char *)(lisp_hdr);
  240. packet_offset = MAP_REGISTER_HDR_LEN;
  241. record_count = lisp_hdr->record_count;
  242. if (ndo->ndo_vflag) {
  243. key_id = EXTRACT_16BITS(&lisp_hdr->key_id);
  244. ND_PRINT((ndo, "\n %u record(s), ", record_count));
  245. ND_PRINT((ndo, "Authentication %s,",
  246. tok2str(auth_type, "unknown-type", key_id)));
  247. hex_print(ndo, "\n Authentication-Data: ", packet_iterator +
  248. packet_offset, auth_data_len);
  249. } else {
  250. ND_PRINT((ndo, " %u record(s),", record_count));
  251. }
  252. packet_offset += auth_data_len;
  253. if (record_count == 0)
  254. goto invalid;
  255. /* Print all the EID records */
  256. while ((length > packet_offset) && (record_count--)) {
  257. ND_TCHECK2(*(packet_iterator + packet_offset), MAP_REGISTER_EID_LEN);
  258. ND_PRINT((ndo, "\n"));
  259. lisp_eid = (const lisp_map_register_eid *)
  260. ((const u_char *)lisp_hdr + packet_offset);
  261. packet_offset += MAP_REGISTER_EID_LEN;
  262. mask_len = lisp_eid->eid_prefix_mask_length;
  263. eid_afi = EXTRACT_16BITS(&lisp_eid->eid_prefix_afi);
  264. loc_count = lisp_eid->locator_count;
  265. if (ndo->ndo_vflag) {
  266. ttl = EXTRACT_32BITS(&lisp_eid->ttl);
  267. ND_PRINT((ndo, " Record TTL %u,", ttl));
  268. action_flag(ndo, lisp_eid->act_auth_inc_res);
  269. map_version = (((lisp_eid->reserved_version_hi) & 15 ) * 255) +
  270. lisp_eid->version_low;
  271. ND_PRINT((ndo, " Map Version: %u,", map_version));
  272. }
  273. switch (eid_afi) {
  274. case IPv4_AFI:
  275. ND_TCHECK2(*(packet_iterator + packet_offset), 4);
  276. ND_PRINT((ndo, " EID %s/%u,", ipaddr_string(ndo,
  277. packet_iterator + packet_offset), mask_len));
  278. packet_offset += 4;
  279. break;
  280. case IPv6_AFI:
  281. ND_TCHECK2(*(packet_iterator + packet_offset), 16);
  282. ND_PRINT((ndo, " EID %s/%u,", ip6addr_string(ndo,
  283. packet_iterator + packet_offset), mask_len));
  284. packet_offset += 16;
  285. break;
  286. default:
  287. /*
  288. * No support for LCAF right now.
  289. */
  290. return;
  291. break;
  292. }
  293. ND_PRINT((ndo, " %u locator(s)", loc_count));
  294. while (loc_count--) {
  295. ND_TCHECK2(*(packet_iterator + packet_offset), MAP_REGISTER_LOC_LEN);
  296. lisp_loc = (const lisp_map_register_loc *) (packet_iterator + packet_offset);
  297. loc_ip_pointer = (const u_char *) (lisp_loc + 1);
  298. packet_offset += MAP_REGISTER_LOC_LEN;
  299. loc_afi = EXTRACT_16BITS(&lisp_loc->locator_afi);
  300. if (ndo->ndo_vflag)
  301. ND_PRINT((ndo, "\n "));
  302. switch (loc_afi) {
  303. case IPv4_AFI:
  304. ND_TCHECK2(*(packet_iterator + packet_offset), 4);
  305. ND_PRINT((ndo, " LOC %s", ipaddr_string(ndo, loc_ip_pointer)));
  306. packet_offset += 4;
  307. break;
  308. case IPv6_AFI:
  309. ND_TCHECK2(*(packet_iterator + packet_offset), 16);
  310. ND_PRINT((ndo, " LOC %s", ip6addr_string(ndo, loc_ip_pointer)));
  311. packet_offset += 16;
  312. break;
  313. default:
  314. break;
  315. }
  316. if (ndo->ndo_vflag) {
  317. ND_PRINT((ndo, "\n Priority/Weight %u/%u,"
  318. " Multicast Priority/Weight %u/%u,",
  319. lisp_loc->priority, lisp_loc->weight,
  320. lisp_loc->m_priority, lisp_loc->m_weight));
  321. loc_hdr_flag(ndo, EXTRACT_16BITS(&lisp_loc->unused_and_flag));
  322. }
  323. }
  324. }
  325. /*
  326. * Print xTR and Site ID. Handle the fact that the packet could be invalid.
  327. * If the xTR_ID_Present bit is not set, and we still have data to display,
  328. * show it as hex data.
  329. */
  330. if (xtr_present) {
  331. if (!ND_TTEST2(*(packet_iterator + packet_offset), 24))
  332. goto invalid;
  333. hex_print_with_offset(ndo, "\n xTR-ID: ", packet_iterator + packet_offset, 16, 0);
  334. ND_PRINT((ndo, "\n SITE-ID: %" PRIu64,
  335. EXTRACT_64BITS(packet_iterator + packet_offset + 16)));
  336. } else {
  337. /* Check if packet isn't over yet */
  338. if (packet_iterator + packet_offset < ndo->ndo_snapend) {
  339. hex_print_with_offset(ndo, "\n Data: ", packet_iterator + packet_offset,
  340. (ndo->ndo_snapend - (packet_iterator + packet_offset)), 0);
  341. }
  342. }
  343. return;
  344. trunc:
  345. ND_PRINT((ndo, "\n %s", tstr));
  346. return;
  347. invalid:
  348. ND_PRINT((ndo, "\n %s", istr));
  349. return;
  350. }
  351. static inline uint8_t extract_lisp_type(uint8_t lisp_hdr_flags)
  352. {
  353. return (lisp_hdr_flags) >> TYPE_INDEX;
  354. }
  355. static inline uint8_t is_xtr_data_present(uint8_t type, uint8_t lisp_hdr_flags)
  356. {
  357. uint8_t xtr_present = 0;
  358. if (type == LISP_MAP_REGISTER)
  359. xtr_present = (lisp_hdr_flags) & LISP_MAP_REGISTER_IBIT_MASK;
  360. else if (type == LISP_MAP_NOTIFY)
  361. xtr_present = (lisp_hdr_flags) & LISP_MAP_NOTIFY_IBIT_MASK;
  362. return xtr_present;
  363. }
  364. static void lisp_hdr_flag(netdissect_options *ndo, const lisp_map_register_hdr *lisp_hdr)
  365. {
  366. uint8_t type = extract_lisp_type(lisp_hdr->type_and_flag);
  367. if (!ndo->ndo_vflag) {
  368. ND_PRINT((ndo, "%s,", tok2str(lisp_type, "unknown-type-%u", type)));
  369. return;
  370. } else {
  371. ND_PRINT((ndo, "%s,", tok2str(lisp_type, "unknown-type-%u", type)));
  372. }
  373. if (type == LISP_MAP_REGISTER) {
  374. ND_PRINT((ndo, " flags [%s],", bittok2str(map_register_hdr_flag,
  375. "none", EXTRACT_32BITS(lisp_hdr))));
  376. } else if (type == LISP_MAP_NOTIFY) {
  377. ND_PRINT((ndo, " flags [%s],", bittok2str(map_notify_hdr_flag,
  378. "none", EXTRACT_32BITS(lisp_hdr))));
  379. }
  380. return;
  381. }
  382. static void action_flag(netdissect_options *ndo, uint8_t act_auth_inc_res)
  383. {
  384. uint8_t action;
  385. uint8_t authoritative;
  386. authoritative = ((act_auth_inc_res >> 4) & 1);
  387. if (authoritative)
  388. ND_PRINT((ndo, " Authoritative,"));
  389. else
  390. ND_PRINT((ndo, " Non-Authoritative,"));
  391. action = act_auth_inc_res >> 5;
  392. ND_PRINT((ndo, " %s,", tok2str(lisp_eid_action, "unknown", action)));
  393. }
  394. static void loc_hdr_flag(netdissect_options *ndo, uint16_t flag)
  395. {
  396. ND_PRINT((ndo, " flags [%s],", bittok2str(lisp_loc_flag, "none", flag)));
  397. }