print-rip.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1996
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. /* \summary: Routing Information Protocol (RIP) printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <netdissect-stdinc.h>
  26. #include <stdio.h>
  27. #include "netdissect.h"
  28. #include "addrtoname.h"
  29. #include "extract.h"
  30. #include "af.h"
  31. static const char tstr[] = "[|rip]";
  32. struct rip {
  33. uint8_t rip_cmd; /* request/response */
  34. uint8_t rip_vers; /* protocol version # */
  35. uint8_t unused[2]; /* unused */
  36. };
  37. #define RIPCMD_REQUEST 1 /* want info */
  38. #define RIPCMD_RESPONSE 2 /* responding to request */
  39. #define RIPCMD_TRACEON 3 /* turn tracing on */
  40. #define RIPCMD_TRACEOFF 4 /* turn it off */
  41. #define RIPCMD_POLL 5 /* want info from everybody */
  42. #define RIPCMD_POLLENTRY 6 /* poll for entry */
  43. static const struct tok rip_cmd_values[] = {
  44. { RIPCMD_REQUEST, "Request" },
  45. { RIPCMD_RESPONSE, "Response" },
  46. { RIPCMD_TRACEON, "Trace on" },
  47. { RIPCMD_TRACEOFF, "Trace off" },
  48. { RIPCMD_POLL, "Poll" },
  49. { RIPCMD_POLLENTRY, "Poll Entry" },
  50. { 0, NULL}
  51. };
  52. #define RIP_AUTHLEN 16
  53. #define RIP_ROUTELEN 20
  54. /*
  55. * rfc 1723
  56. *
  57. * 0 1 2 3 3
  58. * 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
  59. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  60. * | Command (1) | Version (1) | unused |
  61. * +---------------+---------------+-------------------------------+
  62. * | Address Family Identifier (2) | Route Tag (2) |
  63. * +-------------------------------+-------------------------------+
  64. * | IP Address (4) |
  65. * +---------------------------------------------------------------+
  66. * | Subnet Mask (4) |
  67. * +---------------------------------------------------------------+
  68. * | Next Hop (4) |
  69. * +---------------------------------------------------------------+
  70. * | Metric (4) |
  71. * +---------------------------------------------------------------+
  72. *
  73. */
  74. struct rip_netinfo {
  75. uint16_t rip_family;
  76. uint16_t rip_tag;
  77. uint32_t rip_dest;
  78. uint32_t rip_dest_mask;
  79. uint32_t rip_router;
  80. uint32_t rip_metric; /* cost of route */
  81. };
  82. static void
  83. rip_entry_print_v1(netdissect_options *ndo,
  84. register const struct rip_netinfo *ni)
  85. {
  86. register u_short family;
  87. /* RFC 1058 */
  88. family = EXTRACT_16BITS(&ni->rip_family);
  89. if (family != BSD_AFNUM_INET && family != 0) {
  90. ND_PRINT((ndo, "\n\t AFI %s, ", tok2str(bsd_af_values, "Unknown (%u)", family)));
  91. print_unknown_data(ndo, (const uint8_t *)&ni->rip_family, "\n\t ", RIP_ROUTELEN);
  92. return;
  93. }
  94. if (EXTRACT_16BITS(&ni->rip_tag) ||
  95. EXTRACT_32BITS(&ni->rip_dest_mask) ||
  96. EXTRACT_32BITS(&ni->rip_router)) {
  97. /* MBZ fields not zero */
  98. print_unknown_data(ndo, (const uint8_t *)&ni->rip_family, "\n\t ", RIP_ROUTELEN);
  99. return;
  100. }
  101. if (family == 0) {
  102. ND_PRINT((ndo, "\n\t AFI 0, %s, metric: %u",
  103. ipaddr_string(ndo, &ni->rip_dest),
  104. EXTRACT_32BITS(&ni->rip_metric)));
  105. return;
  106. } /* BSD_AFNUM_INET */
  107. ND_PRINT((ndo, "\n\t %s, metric: %u",
  108. ipaddr_string(ndo, &ni->rip_dest),
  109. EXTRACT_32BITS(&ni->rip_metric)));
  110. }
  111. static unsigned
  112. rip_entry_print_v2(netdissect_options *ndo,
  113. register const struct rip_netinfo *ni, const unsigned remaining)
  114. {
  115. register u_short family;
  116. family = EXTRACT_16BITS(&ni->rip_family);
  117. if (family == 0xFFFF) { /* variable-sized authentication structures */
  118. uint16_t auth_type = EXTRACT_16BITS(&ni->rip_tag);
  119. if (auth_type == 2) {
  120. register const u_char *p = (const u_char *)&ni->rip_dest;
  121. u_int i = 0;
  122. ND_PRINT((ndo, "\n\t Simple Text Authentication data: "));
  123. for (; i < RIP_AUTHLEN; p++, i++)
  124. ND_PRINT((ndo, "%c", ND_ISPRINT(*p) ? *p : '.'));
  125. } else if (auth_type == 3) {
  126. ND_PRINT((ndo, "\n\t Auth header:"));
  127. ND_PRINT((ndo, " Packet Len %u,", EXTRACT_16BITS((const uint8_t *)ni + 4)));
  128. ND_PRINT((ndo, " Key-ID %u,", *((const uint8_t *)ni + 6)));
  129. ND_PRINT((ndo, " Auth Data Len %u,", *((const uint8_t *)ni + 7)));
  130. ND_PRINT((ndo, " SeqNo %u,", EXTRACT_32BITS(&ni->rip_dest_mask)));
  131. ND_PRINT((ndo, " MBZ %u,", EXTRACT_32BITS(&ni->rip_router)));
  132. ND_PRINT((ndo, " MBZ %u", EXTRACT_32BITS(&ni->rip_metric)));
  133. } else if (auth_type == 1) {
  134. ND_PRINT((ndo, "\n\t Auth trailer:"));
  135. print_unknown_data(ndo, (const uint8_t *)&ni->rip_dest, "\n\t ", remaining);
  136. return remaining; /* AT spans till the packet end */
  137. } else {
  138. ND_PRINT((ndo, "\n\t Unknown (%u) Authentication data:",
  139. EXTRACT_16BITS(&ni->rip_tag)));
  140. print_unknown_data(ndo, (const uint8_t *)&ni->rip_dest, "\n\t ", remaining);
  141. }
  142. } else if (family != BSD_AFNUM_INET && family != 0) {
  143. ND_PRINT((ndo, "\n\t AFI %s", tok2str(bsd_af_values, "Unknown (%u)", family)));
  144. print_unknown_data(ndo, (const uint8_t *)&ni->rip_tag, "\n\t ", RIP_ROUTELEN-2);
  145. } else { /* BSD_AFNUM_INET or AFI 0 */
  146. ND_PRINT((ndo, "\n\t AFI %s, %15s/%-2d, tag 0x%04x, metric: %u, next-hop: ",
  147. tok2str(bsd_af_values, "%u", family),
  148. ipaddr_string(ndo, &ni->rip_dest),
  149. mask2plen(EXTRACT_32BITS(&ni->rip_dest_mask)),
  150. EXTRACT_16BITS(&ni->rip_tag),
  151. EXTRACT_32BITS(&ni->rip_metric)));
  152. if (EXTRACT_32BITS(&ni->rip_router))
  153. ND_PRINT((ndo, "%s", ipaddr_string(ndo, &ni->rip_router)));
  154. else
  155. ND_PRINT((ndo, "self"));
  156. }
  157. return sizeof (*ni);
  158. }
  159. void
  160. rip_print(netdissect_options *ndo,
  161. const u_char *dat, u_int length)
  162. {
  163. register const struct rip *rp;
  164. register const struct rip_netinfo *ni;
  165. register u_int i, j;
  166. if (ndo->ndo_snapend < dat) {
  167. ND_PRINT((ndo, " %s", tstr));
  168. return;
  169. }
  170. i = ndo->ndo_snapend - dat;
  171. if (i > length)
  172. i = length;
  173. if (i < sizeof(*rp)) {
  174. ND_PRINT((ndo, " %s", tstr));
  175. return;
  176. }
  177. i -= sizeof(*rp);
  178. rp = (const struct rip *)dat;
  179. ND_PRINT((ndo, "%sRIPv%u",
  180. (ndo->ndo_vflag >= 1) ? "\n\t" : "",
  181. rp->rip_vers));
  182. switch (rp->rip_vers) {
  183. case 0:
  184. /*
  185. * RFC 1058.
  186. *
  187. * XXX - RFC 1058 says
  188. *
  189. * 0 Datagrams whose version number is zero are to be ignored.
  190. * These are from a previous version of the protocol, whose
  191. * packet format was machine-specific.
  192. *
  193. * so perhaps we should just dump the packet, in hex.
  194. */
  195. print_unknown_data(ndo, (const uint8_t *)&rp->rip_cmd, "\n\t", length);
  196. break;
  197. default:
  198. /* dump version and lets see if we know the commands name*/
  199. ND_PRINT((ndo, ", %s, length: %u",
  200. tok2str(rip_cmd_values,
  201. "unknown command (%u)",
  202. rp->rip_cmd),
  203. length));
  204. if (ndo->ndo_vflag < 1)
  205. return;
  206. switch (rp->rip_cmd) {
  207. case RIPCMD_REQUEST:
  208. case RIPCMD_RESPONSE:
  209. j = length / sizeof(*ni);
  210. ND_PRINT((ndo, ", routes: %u%s", j, rp->rip_vers == 2 ? " or less" : ""));
  211. ni = (const struct rip_netinfo *)(rp + 1);
  212. for (; i >= sizeof(*ni); ++ni) {
  213. if (rp->rip_vers == 1)
  214. {
  215. rip_entry_print_v1(ndo, ni);
  216. i -= sizeof(*ni);
  217. }
  218. else if (rp->rip_vers == 2)
  219. i -= rip_entry_print_v2(ndo, ni, i);
  220. else
  221. break;
  222. }
  223. if (i)
  224. ND_PRINT((ndo, "%s", tstr));
  225. break;
  226. case RIPCMD_TRACEOFF:
  227. case RIPCMD_POLL:
  228. case RIPCMD_POLLENTRY:
  229. break;
  230. case RIPCMD_TRACEON:
  231. /* fall through */
  232. default:
  233. if (ndo->ndo_vflag <= 1) {
  234. if(!print_unknown_data(ndo, (const uint8_t *)rp, "\n\t", length))
  235. return;
  236. }
  237. break;
  238. }
  239. /* do we want to see an additionally hexdump ? */
  240. if (ndo->ndo_vflag> 1) {
  241. if(!print_unknown_data(ndo, (const uint8_t *)rp, "\n\t", length))
  242. return;
  243. }
  244. }
  245. }