print-token.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 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. * Hacked version of print-ether.c Larry Lile <lile@stdio.com>
  22. *
  23. * Further tweaked to more closely resemble print-fddi.c
  24. * Guy Harris <guy@alum.mit.edu>
  25. */
  26. /* \summary: Token Ring printer */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <netdissect-stdinc.h>
  31. #include <string.h>
  32. #include "netdissect.h"
  33. #include "extract.h"
  34. #include "addrtoname.h"
  35. #include "ether.h"
  36. /*
  37. * Copyright (c) 1998, Larry Lile
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. * 1. Redistributions of source code must retain the above copyright
  44. * notice unmodified, this list of conditions, and the following
  45. * disclaimer.
  46. * 2. Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in the
  48. * documentation and/or other materials provided with the distribution.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  51. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  54. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  56. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  57. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  58. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  59. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  60. * SUCH DAMAGE.
  61. *
  62. */
  63. #define TOKEN_HDRLEN 14
  64. #define TOKEN_RING_MAC_LEN 6
  65. #define ROUTING_SEGMENT_MAX 16
  66. #define IS_SOURCE_ROUTED(trp) ((trp)->token_shost[0] & 0x80)
  67. #define FRAME_TYPE(trp) (((trp)->token_fc & 0xC0) >> 6)
  68. #define TOKEN_FC_LLC 1
  69. #define BROADCAST(trp) ((EXTRACT_16BITS(&(trp)->token_rcf) & 0xE000) >> 13)
  70. #define RIF_LENGTH(trp) ((EXTRACT_16BITS(&(trp)->token_rcf) & 0x1f00) >> 8)
  71. #define DIRECTION(trp) ((EXTRACT_16BITS(&(trp)->token_rcf) & 0x0080) >> 7)
  72. #define LARGEST_FRAME(trp) ((EXTRACT_16BITS(&(trp)->token_rcf) & 0x0070) >> 4)
  73. #define RING_NUMBER(trp, x) ((EXTRACT_16BITS(&(trp)->token_rseg[x]) & 0xfff0) >> 4)
  74. #define BRIDGE_NUMBER(trp, x) ((EXTRACT_16BITS(&(trp)->token_rseg[x]) & 0x000f))
  75. #define SEGMENT_COUNT(trp) ((int)((RIF_LENGTH(trp) - 2) / 2))
  76. struct token_header {
  77. uint8_t token_ac;
  78. uint8_t token_fc;
  79. uint8_t token_dhost[TOKEN_RING_MAC_LEN];
  80. uint8_t token_shost[TOKEN_RING_MAC_LEN];
  81. uint16_t token_rcf;
  82. uint16_t token_rseg[ROUTING_SEGMENT_MAX];
  83. };
  84. static const char tstr[] = "[|token-ring]";
  85. /* Extract src, dst addresses */
  86. static inline void
  87. extract_token_addrs(const struct token_header *trp, char *fsrc, char *fdst)
  88. {
  89. memcpy(fdst, (const char *)trp->token_dhost, 6);
  90. memcpy(fsrc, (const char *)trp->token_shost, 6);
  91. }
  92. /*
  93. * Print the TR MAC header
  94. */
  95. static inline void
  96. token_hdr_print(netdissect_options *ndo,
  97. register const struct token_header *trp, register u_int length,
  98. register const u_char *fsrc, register const u_char *fdst)
  99. {
  100. const char *srcname, *dstname;
  101. srcname = etheraddr_string(ndo, fsrc);
  102. dstname = etheraddr_string(ndo, fdst);
  103. if (!ndo->ndo_qflag)
  104. ND_PRINT((ndo, "%02x %02x ",
  105. trp->token_ac,
  106. trp->token_fc));
  107. ND_PRINT((ndo, "%s > %s, length %u: ",
  108. srcname, dstname,
  109. length));
  110. }
  111. static const char *broadcast_indicator[] = {
  112. "Non-Broadcast", "Non-Broadcast",
  113. "Non-Broadcast", "Non-Broadcast",
  114. "All-routes", "All-routes",
  115. "Single-route", "Single-route"
  116. };
  117. static const char *direction[] = {
  118. "Forward", "Backward"
  119. };
  120. static const char *largest_frame[] = {
  121. "516",
  122. "1500",
  123. "2052",
  124. "4472",
  125. "8144",
  126. "11407",
  127. "17800",
  128. "??"
  129. };
  130. u_int
  131. token_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
  132. {
  133. const struct token_header *trp;
  134. int llc_hdrlen;
  135. struct ether_header ehdr;
  136. struct lladdr_info src, dst;
  137. u_int route_len = 0, hdr_len = TOKEN_HDRLEN;
  138. int seg;
  139. trp = (const struct token_header *)p;
  140. if (caplen < TOKEN_HDRLEN) {
  141. ND_PRINT((ndo, "%s", tstr));
  142. return hdr_len;
  143. }
  144. /*
  145. * Get the TR addresses into a canonical form
  146. */
  147. extract_token_addrs(trp, (char*)ESRC(&ehdr), (char*)EDST(&ehdr));
  148. /* Adjust for source routing information in the MAC header */
  149. if (IS_SOURCE_ROUTED(trp)) {
  150. /* Clear source-routed bit */
  151. *ESRC(&ehdr) &= 0x7f;
  152. if (ndo->ndo_eflag)
  153. token_hdr_print(ndo, trp, length, ESRC(&ehdr), EDST(&ehdr));
  154. if (caplen < TOKEN_HDRLEN + 2) {
  155. ND_PRINT((ndo, "%s", tstr));
  156. return hdr_len;
  157. }
  158. route_len = RIF_LENGTH(trp);
  159. hdr_len += route_len;
  160. if (caplen < hdr_len) {
  161. ND_PRINT((ndo, "%s", tstr));
  162. return hdr_len;
  163. }
  164. if (ndo->ndo_vflag) {
  165. ND_PRINT((ndo, "%s ", broadcast_indicator[BROADCAST(trp)]));
  166. ND_PRINT((ndo, "%s", direction[DIRECTION(trp)]));
  167. for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
  168. ND_PRINT((ndo, " [%d:%d]", RING_NUMBER(trp, seg),
  169. BRIDGE_NUMBER(trp, seg)));
  170. } else {
  171. ND_PRINT((ndo, "rt = %x", EXTRACT_16BITS(&trp->token_rcf)));
  172. for (seg = 0; seg < SEGMENT_COUNT(trp); seg++)
  173. ND_PRINT((ndo, ":%x", EXTRACT_16BITS(&trp->token_rseg[seg])));
  174. }
  175. ND_PRINT((ndo, " (%s) ", largest_frame[LARGEST_FRAME(trp)]));
  176. } else {
  177. if (ndo->ndo_eflag)
  178. token_hdr_print(ndo, trp, length, ESRC(&ehdr), EDST(&ehdr));
  179. }
  180. src.addr = ESRC(&ehdr);
  181. src.addr_string = etheraddr_string;
  182. dst.addr = EDST(&ehdr);
  183. dst.addr_string = etheraddr_string;
  184. /* Skip over token ring MAC header and routing information */
  185. length -= hdr_len;
  186. p += hdr_len;
  187. caplen -= hdr_len;
  188. /* Frame Control field determines interpretation of packet */
  189. if (FRAME_TYPE(trp) == TOKEN_FC_LLC) {
  190. /* Try to print the LLC-layer header & higher layers */
  191. llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
  192. if (llc_hdrlen < 0) {
  193. /* packet type not known, print raw packet */
  194. if (!ndo->ndo_suppress_default_print)
  195. ND_DEFAULTPRINT(p, caplen);
  196. llc_hdrlen = -llc_hdrlen;
  197. }
  198. hdr_len += llc_hdrlen;
  199. } else {
  200. /* Some kinds of TR packet we cannot handle intelligently */
  201. /* XXX - dissect MAC packets if frame type is 0 */
  202. if (!ndo->ndo_eflag)
  203. token_hdr_print(ndo, trp, length + TOKEN_HDRLEN + route_len,
  204. ESRC(&ehdr), EDST(&ehdr));
  205. if (!ndo->ndo_suppress_default_print)
  206. ND_DEFAULTPRINT(p, caplen);
  207. }
  208. return (hdr_len);
  209. }
  210. /*
  211. * This is the top level routine of the printer. 'p' points
  212. * to the TR header of the packet, 'h->ts' is the timestamp,
  213. * 'h->len' is the length of the packet off the wire, and 'h->caplen'
  214. * is the number of bytes actually captured.
  215. */
  216. u_int
  217. token_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
  218. {
  219. return (token_print(ndo, p, h->len, h->caplen));
  220. }