PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/epan/dissectors/packet-rip.c

https://gitlab.com/crondaemon/wireshark-legacy
C | 396 lines | 291 code | 72 blank | 33 comment | 18 complexity | aff952fab7ffe97e6981b571d43148aa MD5 | raw file
  1. /* packet-rip.c
  2. * Routines for RIPv1 and RIPv2 packet disassembly
  3. * RFC1058 (STD 34), RFC1388, RFC1723, RFC2453 (STD 56)
  4. * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
  5. *
  6. * RFC2082 ( Keyed Message Digest Algorithm )
  7. * Emanuele Caratti <wiz@iol.it>
  8. *
  9. * Wireshark - Network traffic analyzer
  10. * By Gerald Combs <gerald@wireshark.org>
  11. * Copyright 1998 Gerald Combs
  12. *
  13. * SPDX-License-Identifier: GPL-2.0-or-later
  14. */
  15. #define NEW_PROTO_TREE_API
  16. #include "config.h"
  17. #include <epan/packet.h>
  18. #include <epan/expert.h>
  19. #include <epan/prefs.h>
  20. #include <epan/to_str.h>
  21. #define UDP_PORT_RIP 520
  22. #define RIPv1 1
  23. #define RIPv2 2
  24. void proto_register_rip(void);
  25. void proto_reg_handoff_rip(void);
  26. static const value_string version_vals[] = {
  27. { RIPv1, "RIPv1" },
  28. { RIPv2, "RIPv2" },
  29. { 0, NULL }
  30. };
  31. static const value_string command_vals[] = {
  32. { 1, "Request" },
  33. { 2, "Response" },
  34. { 3, "Traceon" },
  35. { 4, "Traceoff" },
  36. { 5, "Vendor specific (Sun)" },
  37. { 0, NULL }
  38. };
  39. #define AFVAL_UNSPEC 0
  40. #define AFVAL_IP 2
  41. static const value_string family_vals[] = {
  42. { AFVAL_UNSPEC, "Unspecified" },
  43. { AFVAL_IP, "IP" },
  44. { 0, NULL }
  45. };
  46. #define AUTH_IP_ROUTE 1
  47. #define AUTH_PASSWORD 2
  48. #define AUTH_KEYED_MSG_DIGEST 3
  49. static const value_string rip_auth_type[] = {
  50. { AUTH_IP_ROUTE, "IP Route" },
  51. { AUTH_PASSWORD, "Simple Password" },
  52. { AUTH_KEYED_MSG_DIGEST, "Keyed Message Digest" },
  53. { 0, NULL }
  54. };
  55. #define RIP_HEADER_LENGTH 4
  56. #define RIP_ENTRY_LENGTH 20
  57. #define MD5_AUTH_DATA_LEN 16
  58. static gboolean pref_display_routing_domain = FALSE;
  59. static dissector_handle_t rip_handle;
  60. static header_field_info *hfi_rip = NULL;
  61. #define RIP_HFI_INIT HFI_INIT(proto_rip)
  62. static header_field_info hfi_rip_command RIP_HFI_INIT = {
  63. "Command", "rip.command", FT_UINT8, BASE_DEC,
  64. VALS(command_vals), 0, "What type of RIP Command is this", HFILL };
  65. static header_field_info hfi_rip_version RIP_HFI_INIT = {
  66. "Version", "rip.version", FT_UINT8, BASE_DEC,
  67. VALS(version_vals), 0, "Version of the RIP protocol", HFILL };
  68. static header_field_info hfi_rip_routing_domain RIP_HFI_INIT = {
  69. "Routing Domain", "rip.routing_domain", FT_UINT16, BASE_DEC,
  70. NULL, 0, "RIPv2 Routing Domain", HFILL };
  71. static header_field_info hfi_rip_ip RIP_HFI_INIT = {
  72. "IP Address", "rip.ip", FT_IPv4, BASE_NONE,
  73. NULL, 0, NULL, HFILL};
  74. static header_field_info hfi_rip_netmask RIP_HFI_INIT = {
  75. "Netmask", "rip.netmask", FT_IPv4, BASE_NETMASK,
  76. NULL, 0, NULL, HFILL};
  77. static header_field_info hfi_rip_next_hop RIP_HFI_INIT = {
  78. "Next Hop", "rip.next_hop", FT_IPv4, BASE_NONE,
  79. NULL, 0, "Next Hop router for this route", HFILL};
  80. static header_field_info hfi_rip_metric RIP_HFI_INIT = {
  81. "Metric", "rip.metric", FT_UINT16, BASE_DEC,
  82. NULL, 0, "Metric for this route", HFILL };
  83. static header_field_info hfi_rip_auth RIP_HFI_INIT = {
  84. "Authentication type", "rip.auth.type", FT_UINT16, BASE_DEC,
  85. VALS(rip_auth_type), 0, "Type of authentication", HFILL };
  86. static header_field_info hfi_rip_auth_passwd RIP_HFI_INIT = {
  87. "Password", "rip.auth.passwd", FT_STRING, BASE_NONE,
  88. NULL, 0, "Authentication password", HFILL };
  89. static header_field_info hfi_rip_family RIP_HFI_INIT = {
  90. "Address Family", "rip.family", FT_UINT16, BASE_DEC,
  91. VALS(family_vals), 0, NULL, HFILL };
  92. static header_field_info hfi_rip_route_tag RIP_HFI_INIT = {
  93. "Route Tag", "rip.route_tag", FT_UINT16, BASE_DEC,
  94. NULL, 0, NULL, HFILL };
  95. static header_field_info hfi_rip_zero_padding RIP_HFI_INIT = {
  96. "Zero adding", "rip.zero_padding", FT_STRING, BASE_NONE,
  97. NULL, 0, "Authentication password", HFILL };
  98. static header_field_info hfi_rip_digest_offset RIP_HFI_INIT = {
  99. "Digest Offset", "rip.digest_offset", FT_UINT16, BASE_DEC,
  100. NULL, 0, NULL, HFILL };
  101. static header_field_info hfi_rip_key_id RIP_HFI_INIT = {
  102. "Key ID", "rip.key_id", FT_UINT8, BASE_DEC,
  103. NULL, 0, NULL, HFILL };
  104. static header_field_info hfi_rip_auth_data_len RIP_HFI_INIT = {
  105. "Auth Data Len", "rip.auth_data_len", FT_UINT8, BASE_DEC,
  106. NULL, 0, NULL, HFILL };
  107. static header_field_info hfi_rip_auth_seq_num RIP_HFI_INIT = {
  108. "Seq num", "rip.seq_num", FT_UINT32, BASE_DEC,
  109. NULL, 0, NULL, HFILL };
  110. static header_field_info hfi_rip_authentication_data RIP_HFI_INIT = {
  111. "Authentication Data", "rip.authentication_data", FT_BYTES, BASE_NONE,
  112. NULL, 0, NULL, HFILL };
  113. static gint ett_rip = -1;
  114. static gint ett_rip_vec = -1;
  115. static gint ett_auth_vec = -1;
  116. static expert_field ei_rip_unknown_address_family = EI_INIT;
  117. static void dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
  118. proto_tree *tree);
  119. static void dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
  120. proto_tree *tree);
  121. static gint dissect_rip_authentication(tvbuff_t *tvb, int offset,
  122. proto_tree *tree);
  123. static int
  124. dissect_rip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
  125. {
  126. int offset = 0;
  127. proto_tree *rip_tree = NULL;
  128. proto_item *ti;
  129. guint8 command;
  130. guint8 version;
  131. guint16 family;
  132. gint trailer_len = 0;
  133. gboolean is_md5_auth = FALSE;
  134. col_set_str(pinfo->cinfo, COL_PROTOCOL, "RIP");
  135. col_clear(pinfo->cinfo, COL_INFO);
  136. command = tvb_get_guint8(tvb, 0);
  137. version = tvb_get_guint8(tvb, 1);
  138. col_set_str(pinfo->cinfo, COL_PROTOCOL,
  139. val_to_str_const(version, version_vals, "RIP"));
  140. col_add_str(pinfo->cinfo, COL_INFO,
  141. val_to_str(command, command_vals, "Unknown command (%u)"));
  142. ti = proto_tree_add_item(tree, hfi_rip, tvb, 0, -1, ENC_NA);
  143. rip_tree = proto_item_add_subtree(ti, ett_rip);
  144. proto_tree_add_uint(rip_tree, &hfi_rip_command, tvb, 0, 1, command);
  145. proto_tree_add_uint(rip_tree, &hfi_rip_version, tvb, 1, 1, version);
  146. if (version == RIPv2 && pref_display_routing_domain == TRUE)
  147. proto_tree_add_item(rip_tree, &hfi_rip_routing_domain, tvb, 2, 2,
  148. ENC_BIG_ENDIAN);
  149. /* skip header */
  150. offset = RIP_HEADER_LENGTH;
  151. /* zero or more entries */
  152. while (tvb_reported_length_remaining(tvb, offset) > trailer_len ) {
  153. family = tvb_get_ntohs(tvb, offset);
  154. switch (family) {
  155. case AFVAL_UNSPEC: /* Unspecified */
  156. /*
  157. * There should be one entry in the request, and a metric
  158. * of infinity, meaning "show the entire routing table".
  159. */
  160. dissect_unspec_rip_vektor(tvb, offset, version, rip_tree);
  161. break;
  162. case AFVAL_IP: /* IP */
  163. dissect_ip_rip_vektor(tvb, offset, version, rip_tree);
  164. break;
  165. case 0xFFFF:
  166. if( offset == RIP_HEADER_LENGTH ) {
  167. trailer_len=dissect_rip_authentication(tvb, offset, rip_tree);
  168. is_md5_auth = TRUE;
  169. break;
  170. }
  171. if(is_md5_auth && tvb_reported_length_remaining(tvb, offset) == 20)
  172. break;
  173. /* Intentional fall through */ /* auth Entry MUST be the first! */
  174. default:
  175. proto_tree_add_expert_format(rip_tree, pinfo, &ei_rip_unknown_address_family, tvb, offset,
  176. RIP_ENTRY_LENGTH, "Unknown address family %u", family);
  177. break;
  178. }
  179. offset += RIP_ENTRY_LENGTH;
  180. }
  181. return tvb_captured_length(tvb);
  182. }
  183. static void
  184. dissect_unspec_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
  185. proto_tree *tree)
  186. {
  187. proto_tree *rip_vektor_tree;
  188. guint32 metric;
  189. metric = tvb_get_ntohl(tvb, offset+16);
  190. rip_vektor_tree = proto_tree_add_subtree_format(tree, tvb, offset,
  191. RIP_ENTRY_LENGTH, ett_rip_vec, NULL, "Address not specified, Metric: %u",
  192. metric);
  193. proto_tree_add_item(rip_vektor_tree, &hfi_rip_family, tvb, offset, 2, ENC_BIG_ENDIAN);
  194. if (version == RIPv2) {
  195. proto_tree_add_item(rip_vektor_tree, &hfi_rip_route_tag, tvb, offset+2, 2,
  196. ENC_BIG_ENDIAN);
  197. proto_tree_add_item(rip_vektor_tree, &hfi_rip_netmask, tvb, offset+8, 4,
  198. ENC_BIG_ENDIAN);
  199. proto_tree_add_item(rip_vektor_tree, &hfi_rip_next_hop, tvb, offset+12, 4,
  200. ENC_BIG_ENDIAN);
  201. }
  202. proto_tree_add_uint(rip_vektor_tree, &hfi_rip_metric, tvb,
  203. offset+16, 4, metric);
  204. }
  205. static void
  206. dissect_ip_rip_vektor(tvbuff_t *tvb, int offset, guint8 version,
  207. proto_tree *tree)
  208. {
  209. proto_tree *rip_vektor_tree;
  210. guint32 metric;
  211. metric = tvb_get_ntohl(tvb, offset+16);
  212. rip_vektor_tree = proto_tree_add_subtree_format(tree, tvb, offset,
  213. RIP_ENTRY_LENGTH, ett_rip_vec, NULL, "IP Address: %s, Metric: %u",
  214. tvb_ip_to_str(tvb, offset+4), metric);
  215. proto_tree_add_item(rip_vektor_tree, &hfi_rip_family, tvb, offset, 2, ENC_BIG_ENDIAN);
  216. if (version == RIPv2) {
  217. proto_tree_add_item(rip_vektor_tree, &hfi_rip_route_tag, tvb, offset+2, 2,
  218. ENC_BIG_ENDIAN);
  219. }
  220. proto_tree_add_item(rip_vektor_tree, &hfi_rip_ip, tvb, offset+4, 4, ENC_BIG_ENDIAN);
  221. if (version == RIPv2) {
  222. proto_tree_add_item(rip_vektor_tree, &hfi_rip_netmask, tvb, offset+8, 4,
  223. ENC_BIG_ENDIAN);
  224. proto_tree_add_item(rip_vektor_tree, &hfi_rip_next_hop, tvb, offset+12, 4,
  225. ENC_BIG_ENDIAN);
  226. }
  227. proto_tree_add_uint(rip_vektor_tree, &hfi_rip_metric, tvb,
  228. offset+16, 4, metric);
  229. }
  230. static gint
  231. dissect_rip_authentication(tvbuff_t *tvb, int offset, proto_tree *tree)
  232. {
  233. proto_tree *rip_authentication_tree;
  234. guint16 authtype;
  235. guint32 digest_off, auth_data_len;
  236. auth_data_len = 0;
  237. authtype = tvb_get_ntohs(tvb, offset + 2);
  238. rip_authentication_tree = proto_tree_add_subtree_format(tree, tvb, offset, RIP_ENTRY_LENGTH,
  239. ett_rip_vec, NULL, "Authentication: %s", val_to_str( authtype, rip_auth_type, "Unknown (%u)" ) );
  240. proto_tree_add_uint(rip_authentication_tree, &hfi_rip_auth, tvb, offset+2, 2,
  241. authtype);
  242. switch ( authtype ) {
  243. case AUTH_PASSWORD: /* Plain text password */
  244. proto_tree_add_item(rip_authentication_tree, &hfi_rip_auth_passwd,
  245. tvb, offset+4, 16, ENC_ASCII|ENC_NA);
  246. break;
  247. case AUTH_KEYED_MSG_DIGEST: /* Keyed MD5 rfc 2082 */
  248. digest_off = tvb_get_ntohs( tvb, offset+4 );
  249. proto_tree_add_item( rip_authentication_tree, &hfi_rip_digest_offset, tvb, offset+4, 2, ENC_BIG_ENDIAN);
  250. proto_tree_add_item( rip_authentication_tree, &hfi_rip_key_id, tvb, offset+6, 1, ENC_NA);
  251. auth_data_len = tvb_get_guint8( tvb, offset+7 );
  252. proto_tree_add_item( rip_authentication_tree, &hfi_rip_auth_data_len, tvb, offset+7, 1, ENC_NA);
  253. proto_tree_add_item( rip_authentication_tree, &hfi_rip_auth_seq_num, tvb, offset+8, 4, ENC_BIG_ENDIAN);
  254. proto_tree_add_item( rip_authentication_tree, &hfi_rip_zero_padding, tvb, offset+12, 8, ENC_NA);
  255. rip_authentication_tree = proto_tree_add_subtree( rip_authentication_tree, tvb, offset-4+digest_off,
  256. MD5_AUTH_DATA_LEN+4, ett_auth_vec, NULL, "Authentication Data Trailer" );
  257. proto_tree_add_item( rip_authentication_tree, &hfi_rip_authentication_data, tvb, offset-4+digest_off+4,
  258. MD5_AUTH_DATA_LEN, ENC_NA);
  259. break;
  260. }
  261. return auth_data_len;
  262. }
  263. void
  264. proto_register_rip(void)
  265. {
  266. #ifndef HAVE_HFI_SECTION_INIT
  267. static header_field_info *hfi[] = {
  268. &hfi_rip_command,
  269. &hfi_rip_version,
  270. &hfi_rip_routing_domain,
  271. &hfi_rip_ip,
  272. &hfi_rip_netmask,
  273. &hfi_rip_next_hop,
  274. &hfi_rip_metric,
  275. &hfi_rip_auth,
  276. &hfi_rip_auth_passwd,
  277. &hfi_rip_family,
  278. &hfi_rip_route_tag,
  279. &hfi_rip_zero_padding,
  280. &hfi_rip_digest_offset,
  281. &hfi_rip_key_id,
  282. &hfi_rip_auth_data_len,
  283. &hfi_rip_auth_seq_num,
  284. &hfi_rip_authentication_data,
  285. };
  286. #endif /* HAVE_HFI_SECTION_INIT */
  287. static gint *ett[] = {
  288. &ett_rip,
  289. &ett_rip_vec,
  290. &ett_auth_vec,
  291. };
  292. static ei_register_info ei[] = {
  293. { &ei_rip_unknown_address_family, { "rip.unknown_address_family", PI_PROTOCOL, PI_WARN, "Unknown address family", EXPFILL }},
  294. };
  295. expert_module_t* expert_rip;
  296. module_t *rip_module;
  297. int proto_rip;
  298. proto_rip = proto_register_protocol("Routing Information Protocol", "RIP", "rip");
  299. hfi_rip = proto_registrar_get_nth(proto_rip);
  300. proto_register_fields(proto_rip, hfi, array_length(hfi));
  301. proto_register_subtree_array(ett, array_length(ett));
  302. expert_rip = expert_register_protocol(proto_rip);
  303. expert_register_field_array(expert_rip, ei, array_length(ei));
  304. rip_module = prefs_register_protocol(proto_rip, NULL);
  305. prefs_register_bool_preference(rip_module, "display_routing_domain", "Display Routing Domain field", "Display the third and forth bytes of the RIPv2 header as the Routing Domain field (introduced in RFC 1388 [January 1993] and obsolete as of RFC 1723 [November 1994])", &pref_display_routing_domain);
  306. rip_handle = create_dissector_handle(dissect_rip, proto_rip);
  307. }
  308. void
  309. proto_reg_handoff_rip(void)
  310. {
  311. dissector_add_uint_with_preference("udp.port", UDP_PORT_RIP, rip_handle);
  312. }
  313. /*
  314. * Editor modelines - https://www.wireshark.org/tools/modelines.html
  315. *
  316. * Local variables:
  317. * c-basic-offset: 4
  318. * tab-width: 8
  319. * indent-tabs-mode: nil
  320. * End:
  321. *
  322. * vi: set shiftwidth=4 tabstop=8 expandtab:
  323. * :indentSize=4:tabSize=8:noTabs=true:
  324. */