print-ripng.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 1989, 1990, 1991, 1993, 1994
  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: IPv6 Routing Information Protocol (RIPng) printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <netdissect-stdinc.h>
  26. #include "netdissect.h"
  27. #include "addrtoname.h"
  28. #include "extract.h"
  29. /*
  30. * Copyright (C) 1995, 1996, 1997 and 1998 WIDE Project.
  31. * All rights reserved.
  32. *
  33. * Redistribution and use in source and binary forms, with or without
  34. * modification, are permitted provided that the following conditions
  35. * are met:
  36. * 1. Redistributions of source code must retain the above copyright
  37. * notice, this list of conditions and the following disclaimer.
  38. * 2. Redistributions in binary form must reproduce the above copyright
  39. * notice, this list of conditions and the following disclaimer in the
  40. * documentation and/or other materials provided with the distribution.
  41. * 3. Neither the name of the project nor the names of its contributors
  42. * may be used to endorse or promote products derived from this software
  43. * without specific prior written permission.
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  46. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  48. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  49. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  50. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  51. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  52. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  53. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  54. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  55. * SUCH DAMAGE.
  56. */
  57. #define RIP6_VERSION 1
  58. #define RIP6_REQUEST 1
  59. #define RIP6_RESPONSE 2
  60. struct netinfo6 {
  61. struct in6_addr rip6_dest;
  62. uint16_t rip6_tag;
  63. uint8_t rip6_plen;
  64. uint8_t rip6_metric;
  65. };
  66. struct rip6 {
  67. uint8_t rip6_cmd;
  68. uint8_t rip6_vers;
  69. uint8_t rip6_res1[2];
  70. union {
  71. struct netinfo6 ru6_nets[1];
  72. char ru6_tracefile[1];
  73. } rip6un;
  74. #define rip6_nets rip6un.ru6_nets
  75. #define rip6_tracefile rip6un.ru6_tracefile
  76. };
  77. #define HOPCNT_INFINITY6 16
  78. #if !defined(IN6_IS_ADDR_UNSPECIFIED) && !defined(_MSC_VER) /* MSVC inline */
  79. static int IN6_IS_ADDR_UNSPECIFIED(const struct in6_addr *addr)
  80. {
  81. static const struct in6_addr in6addr_any; /* :: */
  82. return (memcmp(addr, &in6addr_any, sizeof(*addr)) == 0);
  83. }
  84. #endif
  85. static int
  86. rip6_entry_print(netdissect_options *ndo, register const struct netinfo6 *ni, int metric)
  87. {
  88. int l;
  89. l = ND_PRINT((ndo, "%s/%d", ip6addr_string(ndo, &ni->rip6_dest), ni->rip6_plen));
  90. if (ni->rip6_tag)
  91. l += ND_PRINT((ndo, " [%d]", EXTRACT_16BITS(&ni->rip6_tag)));
  92. if (metric)
  93. l += ND_PRINT((ndo, " (%d)", ni->rip6_metric));
  94. return l;
  95. }
  96. void
  97. ripng_print(netdissect_options *ndo, const u_char *dat, unsigned int length)
  98. {
  99. register const struct rip6 *rp = (const struct rip6 *)dat;
  100. register const struct netinfo6 *ni;
  101. unsigned int length_left;
  102. u_int j;
  103. ND_TCHECK(rp->rip6_cmd);
  104. switch (rp->rip6_cmd) {
  105. case RIP6_REQUEST:
  106. length_left = length;
  107. if (length_left < (sizeof(struct rip6) - sizeof(struct netinfo6)))
  108. goto trunc;
  109. length_left -= (sizeof(struct rip6) - sizeof(struct netinfo6));
  110. j = length_left / sizeof(*ni);
  111. if (j == 1) {
  112. ND_TCHECK(rp->rip6_nets);
  113. if (rp->rip6_nets->rip6_metric == HOPCNT_INFINITY6
  114. && IN6_IS_ADDR_UNSPECIFIED(&rp->rip6_nets->rip6_dest)) {
  115. ND_PRINT((ndo, " ripng-req dump"));
  116. break;
  117. }
  118. }
  119. if (j * sizeof(*ni) != length_left)
  120. ND_PRINT((ndo, " ripng-req %u[%u]:", j, length));
  121. else
  122. ND_PRINT((ndo, " ripng-req %u:", j));
  123. for (ni = rp->rip6_nets; length_left >= sizeof(*ni);
  124. length_left -= sizeof(*ni), ++ni) {
  125. ND_TCHECK(*ni);
  126. if (ndo->ndo_vflag > 1)
  127. ND_PRINT((ndo, "\n\t"));
  128. else
  129. ND_PRINT((ndo, " "));
  130. rip6_entry_print(ndo, ni, 0);
  131. }
  132. if (length_left != 0)
  133. goto trunc;
  134. break;
  135. case RIP6_RESPONSE:
  136. length_left = length;
  137. if (length_left < (sizeof(struct rip6) - sizeof(struct netinfo6)))
  138. goto trunc;
  139. length_left -= (sizeof(struct rip6) - sizeof(struct netinfo6));
  140. j = length_left / sizeof(*ni);
  141. if (j * sizeof(*ni) != length_left)
  142. ND_PRINT((ndo, " ripng-resp %d[%u]:", j, length));
  143. else
  144. ND_PRINT((ndo, " ripng-resp %d:", j));
  145. for (ni = rp->rip6_nets; length_left >= sizeof(*ni);
  146. length_left -= sizeof(*ni), ++ni) {
  147. ND_TCHECK(*ni);
  148. if (ndo->ndo_vflag > 1)
  149. ND_PRINT((ndo, "\n\t"));
  150. else
  151. ND_PRINT((ndo, " "));
  152. rip6_entry_print(ndo, ni, ni->rip6_metric);
  153. }
  154. if (length_left != 0)
  155. goto trunc;
  156. break;
  157. default:
  158. ND_PRINT((ndo, " ripng-%d ?? %u", rp->rip6_cmd, length));
  159. break;
  160. }
  161. ND_TCHECK(rp->rip6_vers);
  162. if (rp->rip6_vers != RIP6_VERSION)
  163. ND_PRINT((ndo, " [vers %d]", rp->rip6_vers));
  164. return;
  165. trunc:
  166. ND_PRINT((ndo, "[|ripng]"));
  167. return;
  168. }