print-bfd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Redistribution and use in source and binary forms, with or without
  3. * modification, are permitted provided that: (1) source code
  4. * distributions retain the above copyright notice and this paragraph
  5. * in its entirety, and (2) distributions including binary code include
  6. * the above copyright notice and this paragraph in its entirety in
  7. * the documentation or other materials provided with the distribution.
  8. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  9. * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
  10. * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  11. * FOR A PARTICULAR PURPOSE.
  12. *
  13. * Original code by Hannes Gredler (hannes@gredler.at)
  14. */
  15. /* \summary: Bidirectional Forwarding Detection (BFD) printer */
  16. /* specification: RFC 5880 (for version 1) and RFC 5881 */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <netdissect-stdinc.h>
  21. #include "netdissect.h"
  22. #include "extract.h"
  23. #include "udp.h"
  24. /*
  25. * Control packet, BFDv0, draft-katz-ward-bfd-01.txt
  26. *
  27. * 0 1 2 3
  28. * 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
  29. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  30. * |Vers | Diag |H|D|P|F| Rsvd | Detect Mult | Length |
  31. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  32. * | My Discriminator |
  33. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  34. * | Your Discriminator |
  35. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  36. * | Desired Min TX Interval |
  37. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  38. * | Required Min RX Interval |
  39. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  40. * | Required Min Echo RX Interval |
  41. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  42. */
  43. /*
  44. * Control packet, BFDv1, RFC 5880
  45. *
  46. * 0 1 2 3
  47. * 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
  48. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  49. * |Vers | Diag |Sta|P|F|C|A|D|M| Detect Mult | Length |
  50. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  51. * | My Discriminator |
  52. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  53. * | Your Discriminator |
  54. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  55. * | Desired Min TX Interval |
  56. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  57. * | Required Min RX Interval |
  58. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  59. * | Required Min Echo RX Interval |
  60. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  61. */
  62. struct bfd_header_t {
  63. uint8_t version_diag;
  64. uint8_t flags;
  65. uint8_t detect_time_multiplier;
  66. uint8_t length;
  67. uint8_t my_discriminator[4];
  68. uint8_t your_discriminator[4];
  69. uint8_t desired_min_tx_interval[4];
  70. uint8_t required_min_rx_interval[4];
  71. uint8_t required_min_echo_interval[4];
  72. };
  73. /*
  74. * An optional Authentication Header may be present
  75. *
  76. * 0 1 2 3
  77. * 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
  78. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  79. * | Auth Type | Auth Len | Authentication Data... |
  80. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  81. */
  82. struct bfd_auth_header_t {
  83. uint8_t auth_type;
  84. uint8_t auth_len;
  85. uint8_t auth_data;
  86. uint8_t dummy; /* minimun 4 bytes */
  87. };
  88. enum auth_type {
  89. AUTH_PASSWORD = 1,
  90. AUTH_MD5 = 2,
  91. AUTH_MET_MD5 = 3,
  92. AUTH_SHA1 = 4,
  93. AUTH_MET_SHA1 = 5
  94. };
  95. static const struct tok bfd_v1_authentication_values[] = {
  96. { AUTH_PASSWORD, "Simple Password" },
  97. { AUTH_MD5, "Keyed MD5" },
  98. { AUTH_MET_MD5, "Meticulous Keyed MD5" },
  99. { AUTH_SHA1, "Keyed SHA1" },
  100. { AUTH_MET_SHA1, "Meticulous Keyed SHA1" },
  101. { 0, NULL }
  102. };
  103. enum auth_length {
  104. AUTH_PASSWORD_FIELD_MIN_LEN = 4, /* header + password min: 3 + 1 */
  105. AUTH_PASSWORD_FIELD_MAX_LEN = 19, /* header + password max: 3 + 16 */
  106. AUTH_MD5_FIELD_LEN = 24,
  107. AUTH_MD5_HASH_LEN = 16,
  108. AUTH_SHA1_FIELD_LEN = 28,
  109. AUTH_SHA1_HASH_LEN = 20
  110. };
  111. #define BFD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
  112. #define BFD_EXTRACT_DIAG(x) ((x)&0x1f)
  113. static const struct tok bfd_port_values[] = {
  114. { BFD_CONTROL_PORT, "Control" },
  115. { BFD_ECHO_PORT, "Echo" },
  116. { 0, NULL }
  117. };
  118. static const struct tok bfd_diag_values[] = {
  119. { 0, "No Diagnostic" },
  120. { 1, "Control Detection Time Expired" },
  121. { 2, "Echo Function Failed" },
  122. { 3, "Neighbor Signaled Session Down" },
  123. { 4, "Forwarding Plane Reset" },
  124. { 5, "Path Down" },
  125. { 6, "Concatenated Path Down" },
  126. { 7, "Administratively Down" },
  127. { 8, "Reverse Concatenated Path Down" },
  128. { 0, NULL }
  129. };
  130. static const struct tok bfd_v0_flag_values[] = {
  131. { 0x80, "I Hear You" },
  132. { 0x40, "Demand" },
  133. { 0x20, "Poll" },
  134. { 0x10, "Final" },
  135. { 0x08, "Reserved" },
  136. { 0x04, "Reserved" },
  137. { 0x02, "Reserved" },
  138. { 0x01, "Reserved" },
  139. { 0, NULL }
  140. };
  141. #define BFD_FLAG_AUTH 0x04
  142. static const struct tok bfd_v1_flag_values[] = {
  143. { 0x20, "Poll" },
  144. { 0x10, "Final" },
  145. { 0x08, "Control Plane Independent" },
  146. { BFD_FLAG_AUTH, "Authentication Present" },
  147. { 0x02, "Demand" },
  148. { 0x01, "Multipoint" },
  149. { 0, NULL }
  150. };
  151. static const struct tok bfd_v1_state_values[] = {
  152. { 0, "AdminDown" },
  153. { 1, "Down" },
  154. { 2, "Init" },
  155. { 3, "Up" },
  156. { 0, NULL }
  157. };
  158. static int
  159. auth_print(netdissect_options *ndo, register const u_char *pptr)
  160. {
  161. const struct bfd_auth_header_t *bfd_auth_header;
  162. int i;
  163. pptr += sizeof (const struct bfd_header_t);
  164. bfd_auth_header = (const struct bfd_auth_header_t *)pptr;
  165. ND_TCHECK(*bfd_auth_header);
  166. ND_PRINT((ndo, "\n\tAuthentication: %s (%u), length: %u",
  167. tok2str(bfd_v1_authentication_values,"Unknown",bfd_auth_header->auth_type),
  168. bfd_auth_header->auth_type,
  169. bfd_auth_header->auth_len));
  170. pptr += 2;
  171. ND_PRINT((ndo, "\n\t Auth Key ID: %d", *pptr));
  172. switch(bfd_auth_header->auth_type) {
  173. case AUTH_PASSWORD:
  174. /*
  175. * Simple Password Authentication Section Format
  176. *
  177. * 0 1 2 3
  178. * 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
  179. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  180. * | Auth Type | Auth Len | Auth Key ID | Password... |
  181. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  182. * | ... |
  183. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  184. */
  185. if (bfd_auth_header->auth_len < AUTH_PASSWORD_FIELD_MIN_LEN ||
  186. bfd_auth_header->auth_len > AUTH_PASSWORD_FIELD_MAX_LEN) {
  187. ND_PRINT((ndo, "[invalid length %d]",
  188. bfd_auth_header->auth_len));
  189. break;
  190. }
  191. pptr++;
  192. ND_PRINT((ndo, ", Password: "));
  193. /* the length is equal to the password length plus three */
  194. if (fn_printn(ndo, pptr, bfd_auth_header->auth_len - 3,
  195. ndo->ndo_snapend))
  196. goto trunc;
  197. break;
  198. case AUTH_MD5:
  199. case AUTH_MET_MD5:
  200. /*
  201. * Keyed MD5 and Meticulous Keyed MD5 Authentication Section Format
  202. *
  203. * 0 1 2 3
  204. * 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
  205. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  206. * | Auth Type | Auth Len | Auth Key ID | Reserved |
  207. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  208. * | Sequence Number |
  209. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  210. * | Auth Key/Digest... |
  211. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  212. * | ... |
  213. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  214. */
  215. if (bfd_auth_header->auth_len != AUTH_MD5_FIELD_LEN) {
  216. ND_PRINT((ndo, "[invalid length %d]",
  217. bfd_auth_header->auth_len));
  218. break;
  219. }
  220. pptr += 2;
  221. ND_TCHECK2(*pptr, 4);
  222. ND_PRINT((ndo, ", Sequence Number: 0x%08x", EXTRACT_32BITS(pptr)));
  223. pptr += 4;
  224. ND_TCHECK2(*pptr, AUTH_MD5_HASH_LEN);
  225. ND_PRINT((ndo, "\n\t Digest: "));
  226. for(i = 0; i < AUTH_MD5_HASH_LEN; i++)
  227. ND_PRINT((ndo, "%02x", pptr[i]));
  228. break;
  229. case AUTH_SHA1:
  230. case AUTH_MET_SHA1:
  231. /*
  232. * Keyed SHA1 and Meticulous Keyed SHA1 Authentication Section Format
  233. *
  234. * 0 1 2 3
  235. * 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
  236. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  237. * | Auth Type | Auth Len | Auth Key ID | Reserved |
  238. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  239. * | Sequence Number |
  240. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  241. * | Auth Key/Hash... |
  242. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  243. * | ... |
  244. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  245. */
  246. if (bfd_auth_header->auth_len != AUTH_SHA1_FIELD_LEN) {
  247. ND_PRINT((ndo, "[invalid length %d]",
  248. bfd_auth_header->auth_len));
  249. break;
  250. }
  251. pptr += 2;
  252. ND_TCHECK2(*pptr, 4);
  253. ND_PRINT((ndo, ", Sequence Number: 0x%08x", EXTRACT_32BITS(pptr)));
  254. pptr += 4;
  255. ND_TCHECK2(*pptr, AUTH_SHA1_HASH_LEN);
  256. ND_PRINT((ndo, "\n\t Hash: "));
  257. for(i = 0; i < AUTH_SHA1_HASH_LEN; i++)
  258. ND_PRINT((ndo, "%02x", pptr[i]));
  259. break;
  260. }
  261. return 0;
  262. trunc:
  263. return 1;
  264. }
  265. void
  266. bfd_print(netdissect_options *ndo, register const u_char *pptr,
  267. register u_int len, register u_int port)
  268. {
  269. const struct bfd_header_t *bfd_header;
  270. uint8_t version = 0;
  271. bfd_header = (const struct bfd_header_t *)pptr;
  272. if (port == BFD_CONTROL_PORT) {
  273. ND_TCHECK(*bfd_header);
  274. version = BFD_EXTRACT_VERSION(bfd_header->version_diag);
  275. } else if (port == BFD_ECHO_PORT) {
  276. /* Echo is BFD v1 only */
  277. version = 1;
  278. }
  279. switch ((port << 8) | version) {
  280. /* BFDv0 */
  281. case (BFD_CONTROL_PORT << 8):
  282. if (ndo->ndo_vflag < 1)
  283. {
  284. ND_PRINT((ndo, "BFDv%u, %s, Flags: [%s], length: %u",
  285. version,
  286. tok2str(bfd_port_values, "unknown (%u)", port),
  287. bittok2str(bfd_v0_flag_values, "none", bfd_header->flags),
  288. len));
  289. return;
  290. }
  291. ND_PRINT((ndo, "BFDv%u, length: %u\n\t%s, Flags: [%s], Diagnostic: %s (0x%02x)",
  292. version,
  293. len,
  294. tok2str(bfd_port_values, "unknown (%u)", port),
  295. bittok2str(bfd_v0_flag_values, "none", bfd_header->flags),
  296. tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(bfd_header->version_diag)),
  297. BFD_EXTRACT_DIAG(bfd_header->version_diag)));
  298. ND_PRINT((ndo, "\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
  299. bfd_header->detect_time_multiplier,
  300. bfd_header->detect_time_multiplier * EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000,
  301. bfd_header->length));
  302. ND_PRINT((ndo, "\n\tMy Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->my_discriminator)));
  303. ND_PRINT((ndo, ", Your Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->your_discriminator)));
  304. ND_PRINT((ndo, "\n\t Desired min Tx Interval: %4u ms", EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000));
  305. ND_PRINT((ndo, "\n\t Required min Rx Interval: %4u ms", EXTRACT_32BITS(bfd_header->required_min_rx_interval)/1000));
  306. ND_PRINT((ndo, "\n\t Required min Echo Interval: %4u ms", EXTRACT_32BITS(bfd_header->required_min_echo_interval)/1000));
  307. break;
  308. /* BFDv1 */
  309. case (BFD_CONTROL_PORT << 8 | 1):
  310. if (ndo->ndo_vflag < 1)
  311. {
  312. ND_PRINT((ndo, "BFDv%u, %s, State %s, Flags: [%s], length: %u",
  313. version,
  314. tok2str(bfd_port_values, "unknown (%u)", port),
  315. tok2str(bfd_v1_state_values, "unknown (%u)", (bfd_header->flags & 0xc0) >> 6),
  316. bittok2str(bfd_v1_flag_values, "none", bfd_header->flags & 0x3f),
  317. len));
  318. return;
  319. }
  320. ND_PRINT((ndo, "BFDv%u, length: %u\n\t%s, State %s, Flags: [%s], Diagnostic: %s (0x%02x)",
  321. version,
  322. len,
  323. tok2str(bfd_port_values, "unknown (%u)", port),
  324. tok2str(bfd_v1_state_values, "unknown (%u)", (bfd_header->flags & 0xc0) >> 6),
  325. bittok2str(bfd_v1_flag_values, "none", bfd_header->flags & 0x3f),
  326. tok2str(bfd_diag_values,"unknown",BFD_EXTRACT_DIAG(bfd_header->version_diag)),
  327. BFD_EXTRACT_DIAG(bfd_header->version_diag)));
  328. ND_PRINT((ndo, "\n\tDetection Timer Multiplier: %u (%u ms Detection time), BFD Length: %u",
  329. bfd_header->detect_time_multiplier,
  330. bfd_header->detect_time_multiplier * EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000,
  331. bfd_header->length));
  332. ND_PRINT((ndo, "\n\tMy Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->my_discriminator)));
  333. ND_PRINT((ndo, ", Your Discriminator: 0x%08x", EXTRACT_32BITS(bfd_header->your_discriminator)));
  334. ND_PRINT((ndo, "\n\t Desired min Tx Interval: %4u ms", EXTRACT_32BITS(bfd_header->desired_min_tx_interval)/1000));
  335. ND_PRINT((ndo, "\n\t Required min Rx Interval: %4u ms", EXTRACT_32BITS(bfd_header->required_min_rx_interval)/1000));
  336. ND_PRINT((ndo, "\n\t Required min Echo Interval: %4u ms", EXTRACT_32BITS(bfd_header->required_min_echo_interval)/1000));
  337. if (bfd_header->flags & BFD_FLAG_AUTH) {
  338. if (auth_print(ndo, pptr))
  339. goto trunc;
  340. }
  341. break;
  342. /* BFDv0 */
  343. case (BFD_ECHO_PORT << 8): /* not yet supported - fall through */
  344. /* BFDv1 */
  345. case (BFD_ECHO_PORT << 8 | 1):
  346. default:
  347. ND_PRINT((ndo, "BFD, %s, length: %u",
  348. tok2str(bfd_port_values, "unknown (%u)", port),
  349. len));
  350. if (ndo->ndo_vflag >= 1) {
  351. if(!print_unknown_data(ndo, pptr,"\n\t",len))
  352. return;
  353. }
  354. break;
  355. }
  356. return;
  357. trunc:
  358. ND_PRINT((ndo, "[|BFD]"));
  359. }
  360. /*
  361. * Local Variables:
  362. * c-style: whitesmith
  363. * c-basic-offset: 8
  364. * End:
  365. */