print-hsrp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2001 Julian Cowley
  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. Neither the name of the project nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /* \summary: Cisco Hot Standby Router Protocol (HSRP) printer */
  30. /* Cisco Hot Standby Router Protocol (HSRP). */
  31. #ifdef HAVE_CONFIG_H
  32. #include "config.h"
  33. #endif
  34. #include <netdissect-stdinc.h>
  35. #include "netdissect.h"
  36. #include "addrtoname.h"
  37. /* HSRP op code types. */
  38. static const char *op_code_str[] = {
  39. "hello",
  40. "coup",
  41. "resign"
  42. };
  43. /* HSRP states and associated names. */
  44. static const struct tok states[] = {
  45. { 0, "initial" },
  46. { 1, "learn" },
  47. { 2, "listen" },
  48. { 4, "speak" },
  49. { 8, "standby" },
  50. { 16, "active" },
  51. { 0, NULL }
  52. };
  53. /*
  54. * RFC 2281:
  55. *
  56. * 0 1 2 3
  57. * 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
  58. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  59. * | Version | Op Code | State | Hellotime |
  60. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  61. * | Holdtime | Priority | Group | Reserved |
  62. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  63. * | Authentication Data |
  64. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  65. * | Authentication Data |
  66. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  67. * | Virtual IP Address |
  68. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  69. */
  70. #define HSRP_AUTH_SIZE 8
  71. /* HSRP protocol header. */
  72. struct hsrp {
  73. uint8_t hsrp_version;
  74. uint8_t hsrp_op_code;
  75. uint8_t hsrp_state;
  76. uint8_t hsrp_hellotime;
  77. uint8_t hsrp_holdtime;
  78. uint8_t hsrp_priority;
  79. uint8_t hsrp_group;
  80. uint8_t hsrp_reserved;
  81. uint8_t hsrp_authdata[HSRP_AUTH_SIZE];
  82. struct in_addr hsrp_virtaddr;
  83. };
  84. void
  85. hsrp_print(netdissect_options *ndo, register const uint8_t *bp, register u_int len)
  86. {
  87. const struct hsrp *hp = (const struct hsrp *) bp;
  88. ND_TCHECK(hp->hsrp_version);
  89. ND_PRINT((ndo, "HSRPv%d", hp->hsrp_version));
  90. if (hp->hsrp_version != 0)
  91. return;
  92. ND_TCHECK(hp->hsrp_op_code);
  93. ND_PRINT((ndo, "-"));
  94. ND_PRINT((ndo, "%s ", tok2strary(op_code_str, "unknown (%d)", hp->hsrp_op_code)));
  95. ND_PRINT((ndo, "%d: ", len));
  96. ND_TCHECK(hp->hsrp_state);
  97. ND_PRINT((ndo, "state=%s ", tok2str(states, "Unknown (%d)", hp->hsrp_state)));
  98. ND_TCHECK(hp->hsrp_group);
  99. ND_PRINT((ndo, "group=%d ", hp->hsrp_group));
  100. ND_TCHECK(hp->hsrp_reserved);
  101. if (hp->hsrp_reserved != 0) {
  102. ND_PRINT((ndo, "[reserved=%d!] ", hp->hsrp_reserved));
  103. }
  104. ND_TCHECK(hp->hsrp_virtaddr);
  105. ND_PRINT((ndo, "addr=%s", ipaddr_string(ndo, &hp->hsrp_virtaddr)));
  106. if (ndo->ndo_vflag) {
  107. ND_PRINT((ndo, " hellotime="));
  108. unsigned_relts_print(ndo, hp->hsrp_hellotime);
  109. ND_PRINT((ndo, " holdtime="));
  110. unsigned_relts_print(ndo, hp->hsrp_holdtime);
  111. ND_PRINT((ndo, " priority=%d", hp->hsrp_priority));
  112. ND_PRINT((ndo, " auth=\""));
  113. if (fn_printn(ndo, hp->hsrp_authdata, sizeof(hp->hsrp_authdata),
  114. ndo->ndo_snapend)) {
  115. ND_PRINT((ndo, "\""));
  116. goto trunc;
  117. }
  118. ND_PRINT((ndo, "\""));
  119. }
  120. return;
  121. trunc:
  122. ND_PRINT((ndo, "[|hsrp]"));
  123. }