print-sl.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997
  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: Compressed Serial Line Internet Protocol printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <netdissect-stdinc.h>
  26. #include "netdissect.h"
  27. #include "extract.h"
  28. #include "ip.h"
  29. #include "tcp.h"
  30. #include "slcompress.h"
  31. /*
  32. * definitions of the pseudo- link-level header attached to slip
  33. * packets grabbed by the packet filter (bpf) traffic monitor.
  34. */
  35. #define SLIP_HDRLEN 16
  36. #define SLX_DIR 0
  37. #define SLX_CHDR 1
  38. #define CHDR_LEN 15
  39. #define SLIPDIR_IN 0
  40. #define SLIPDIR_OUT 1
  41. static const char tstr[] = "[|slip]";
  42. static u_int lastlen[2][256];
  43. static u_int lastconn = 255;
  44. static void sliplink_print(netdissect_options *, const u_char *, const struct ip *, u_int);
  45. static void compressed_sl_print(netdissect_options *, const u_char *, const struct ip *, u_int, int);
  46. u_int
  47. sl_if_print(netdissect_options *ndo,
  48. const struct pcap_pkthdr *h, const u_char *p)
  49. {
  50. register u_int caplen = h->caplen;
  51. register u_int length = h->len;
  52. register const struct ip *ip;
  53. if (caplen < SLIP_HDRLEN || length < SLIP_HDRLEN) {
  54. ND_PRINT((ndo, "%s", tstr));
  55. return (caplen);
  56. }
  57. caplen -= SLIP_HDRLEN;
  58. length -= SLIP_HDRLEN;
  59. ip = (const struct ip *)(p + SLIP_HDRLEN);
  60. if (ndo->ndo_eflag)
  61. sliplink_print(ndo, p, ip, length);
  62. if (caplen < 1 || length < 1) {
  63. ND_PRINT((ndo, "%s", tstr));
  64. return (caplen + SLIP_HDRLEN);
  65. }
  66. switch (IP_V(ip)) {
  67. case 4:
  68. ip_print(ndo, (const u_char *)ip, length);
  69. break;
  70. case 6:
  71. ip6_print(ndo, (const u_char *)ip, length);
  72. break;
  73. default:
  74. ND_PRINT((ndo, "ip v%d", IP_V(ip)));
  75. }
  76. return (SLIP_HDRLEN);
  77. }
  78. u_int
  79. sl_bsdos_if_print(netdissect_options *ndo,
  80. const struct pcap_pkthdr *h, const u_char *p)
  81. {
  82. register u_int caplen = h->caplen;
  83. register u_int length = h->len;
  84. register const struct ip *ip;
  85. if (caplen < SLIP_HDRLEN) {
  86. ND_PRINT((ndo, "%s", tstr));
  87. return (caplen);
  88. }
  89. length -= SLIP_HDRLEN;
  90. ip = (const struct ip *)(p + SLIP_HDRLEN);
  91. #ifdef notdef
  92. if (ndo->ndo_eflag)
  93. sliplink_print(ndo, p, ip, length);
  94. #endif
  95. ip_print(ndo, (const u_char *)ip, length);
  96. return (SLIP_HDRLEN);
  97. }
  98. static void
  99. sliplink_print(netdissect_options *ndo,
  100. register const u_char *p, register const struct ip *ip,
  101. register u_int length)
  102. {
  103. int dir;
  104. u_int hlen;
  105. dir = p[SLX_DIR];
  106. switch (dir) {
  107. case SLIPDIR_IN:
  108. ND_PRINT((ndo, "I "));
  109. break;
  110. case SLIPDIR_OUT:
  111. ND_PRINT((ndo, "O "));
  112. break;
  113. default:
  114. ND_PRINT((ndo, "Invalid direction %d ", dir));
  115. dir = -1;
  116. break;
  117. }
  118. if (ndo->ndo_nflag) {
  119. /* XXX just dump the header */
  120. register int i;
  121. for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i)
  122. ND_PRINT((ndo, "%02x.", p[i]));
  123. ND_PRINT((ndo, "%02x: ", p[SLX_CHDR + CHDR_LEN - 1]));
  124. return;
  125. }
  126. switch (p[SLX_CHDR] & 0xf0) {
  127. case TYPE_IP:
  128. ND_PRINT((ndo, "ip %d: ", length + SLIP_HDRLEN));
  129. break;
  130. case TYPE_UNCOMPRESSED_TCP:
  131. /*
  132. * The connection id is stored in the IP protocol field.
  133. * Get it from the link layer since sl_uncompress_tcp()
  134. * has restored the IP header copy to IPPROTO_TCP.
  135. */
  136. lastconn = ((const struct ip *)&p[SLX_CHDR])->ip_p;
  137. ND_PRINT((ndo, "utcp %d: ", lastconn));
  138. if (dir == -1) {
  139. /* Direction is bogus, don't use it */
  140. return;
  141. }
  142. hlen = IP_HL(ip);
  143. hlen += TH_OFF((const struct tcphdr *)&((const int *)ip)[hlen]);
  144. lastlen[dir][lastconn] = length - (hlen << 2);
  145. break;
  146. default:
  147. if (dir == -1) {
  148. /* Direction is bogus, don't use it */
  149. return;
  150. }
  151. if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
  152. compressed_sl_print(ndo, &p[SLX_CHDR], ip,
  153. length, dir);
  154. ND_PRINT((ndo, ": "));
  155. } else
  156. ND_PRINT((ndo, "slip-%d!: ", p[SLX_CHDR]));
  157. }
  158. }
  159. static const u_char *
  160. print_sl_change(netdissect_options *ndo,
  161. const char *str, register const u_char *cp)
  162. {
  163. register u_int i;
  164. if ((i = *cp++) == 0) {
  165. i = EXTRACT_16BITS(cp);
  166. cp += 2;
  167. }
  168. ND_PRINT((ndo, " %s%d", str, i));
  169. return (cp);
  170. }
  171. static const u_char *
  172. print_sl_winchange(netdissect_options *ndo,
  173. register const u_char *cp)
  174. {
  175. register short i;
  176. if ((i = *cp++) == 0) {
  177. i = EXTRACT_16BITS(cp);
  178. cp += 2;
  179. }
  180. if (i >= 0)
  181. ND_PRINT((ndo, " W+%d", i));
  182. else
  183. ND_PRINT((ndo, " W%d", i));
  184. return (cp);
  185. }
  186. static void
  187. compressed_sl_print(netdissect_options *ndo,
  188. const u_char *chdr, const struct ip *ip,
  189. u_int length, int dir)
  190. {
  191. register const u_char *cp = chdr;
  192. register u_int flags, hlen;
  193. flags = *cp++;
  194. if (flags & NEW_C) {
  195. lastconn = *cp++;
  196. ND_PRINT((ndo, "ctcp %d", lastconn));
  197. } else
  198. ND_PRINT((ndo, "ctcp *"));
  199. /* skip tcp checksum */
  200. cp += 2;
  201. switch (flags & SPECIALS_MASK) {
  202. case SPECIAL_I:
  203. ND_PRINT((ndo, " *SA+%d", lastlen[dir][lastconn]));
  204. break;
  205. case SPECIAL_D:
  206. ND_PRINT((ndo, " *S+%d", lastlen[dir][lastconn]));
  207. break;
  208. default:
  209. if (flags & NEW_U)
  210. cp = print_sl_change(ndo, "U=", cp);
  211. if (flags & NEW_W)
  212. cp = print_sl_winchange(ndo, cp);
  213. if (flags & NEW_A)
  214. cp = print_sl_change(ndo, "A+", cp);
  215. if (flags & NEW_S)
  216. cp = print_sl_change(ndo, "S+", cp);
  217. break;
  218. }
  219. if (flags & NEW_I)
  220. cp = print_sl_change(ndo, "I+", cp);
  221. /*
  222. * 'hlen' is the length of the uncompressed TCP/IP header (in words).
  223. * 'cp - chdr' is the length of the compressed header.
  224. * 'length - hlen' is the amount of data in the packet.
  225. */
  226. hlen = IP_HL(ip);
  227. hlen += TH_OFF((const struct tcphdr *)&((const int32_t *)ip)[hlen]);
  228. lastlen[dir][lastconn] = length - (hlen << 2);
  229. ND_PRINT((ndo, " %d (%ld)", lastlen[dir][lastconn], (long)(cp - chdr)));
  230. }