print-sll.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 1988, 1989, 1990, 1991, 1992, 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: Linux cooked sockets capture 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 "ethertype.h"
  29. #include "extract.h"
  30. #include "ether.h"
  31. /*
  32. * For captures on Linux cooked sockets, we construct a fake header
  33. * that includes:
  34. *
  35. * a 2-byte "packet type" which is one of:
  36. *
  37. * LINUX_SLL_HOST packet was sent to us
  38. * LINUX_SLL_BROADCAST packet was broadcast
  39. * LINUX_SLL_MULTICAST packet was multicast
  40. * LINUX_SLL_OTHERHOST packet was sent to somebody else
  41. * LINUX_SLL_OUTGOING packet was sent *by* us;
  42. *
  43. * a 2-byte Ethernet protocol field;
  44. *
  45. * a 2-byte link-layer type;
  46. *
  47. * a 2-byte link-layer address length;
  48. *
  49. * an 8-byte source link-layer address, whose actual length is
  50. * specified by the previous value.
  51. *
  52. * All fields except for the link-layer address are in network byte order.
  53. *
  54. * DO NOT change the layout of this structure, or change any of the
  55. * LINUX_SLL_ values below. If you must change the link-layer header
  56. * for a "cooked" Linux capture, introduce a new DLT_ type (ask
  57. * "tcpdump-workers@lists.tcpdump.org" for one, so that you don't give it
  58. * a value that collides with a value already being used), and use the
  59. * new header in captures of that type, so that programs that can
  60. * handle DLT_LINUX_SLL captures will continue to handle them correctly
  61. * without any change, and so that capture files with different headers
  62. * can be told apart and programs that read them can dissect the
  63. * packets in them.
  64. *
  65. * This structure, and the #defines below, must be the same in the
  66. * libpcap and tcpdump versions of "sll.h".
  67. */
  68. /*
  69. * A DLT_LINUX_SLL fake link-layer header.
  70. */
  71. #define SLL_HDR_LEN 16 /* total header length */
  72. #define SLL_ADDRLEN 8 /* length of address field */
  73. struct sll_header {
  74. uint16_t sll_pkttype; /* packet type */
  75. uint16_t sll_hatype; /* link-layer address type */
  76. uint16_t sll_halen; /* link-layer address length */
  77. uint8_t sll_addr[SLL_ADDRLEN]; /* link-layer address */
  78. uint16_t sll_protocol; /* protocol */
  79. };
  80. /*
  81. * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the
  82. * PACKET_ values on Linux, but are defined here so that they're
  83. * available even on systems other than Linux, and so that they
  84. * don't change even if the PACKET_ values change.
  85. */
  86. #define LINUX_SLL_HOST 0
  87. #define LINUX_SLL_BROADCAST 1
  88. #define LINUX_SLL_MULTICAST 2
  89. #define LINUX_SLL_OTHERHOST 3
  90. #define LINUX_SLL_OUTGOING 4
  91. /*
  92. * The LINUX_SLL_ values for "sll_protocol"; these correspond to the
  93. * ETH_P_ values on Linux, but are defined here so that they're
  94. * available even on systems other than Linux. We assume, for now,
  95. * that the ETH_P_ values won't change in Linux; if they do, then:
  96. *
  97. * if we don't translate them in "pcap-linux.c", capture files
  98. * won't necessarily be readable if captured on a system that
  99. * defines ETH_P_ values that don't match these values;
  100. *
  101. * if we do translate them in "pcap-linux.c", that makes life
  102. * unpleasant for the BPF code generator, as the values you test
  103. * for in the kernel aren't the values that you test for when
  104. * reading a capture file, so the fixup code run on BPF programs
  105. * handed to the kernel ends up having to do more work.
  106. *
  107. * Add other values here as necessary, for handling packet types that
  108. * might show up on non-Ethernet, non-802.x networks. (Not all the ones
  109. * in the Linux "if_ether.h" will, I suspect, actually show up in
  110. * captures.)
  111. */
  112. #define LINUX_SLL_P_802_3 0x0001 /* Novell 802.3 frames without 802.2 LLC header */
  113. #define LINUX_SLL_P_802_2 0x0004 /* 802.2 frames (not D/I/X Ethernet) */
  114. static const struct tok sll_pkttype_values[] = {
  115. { LINUX_SLL_HOST, "In" },
  116. { LINUX_SLL_BROADCAST, "B" },
  117. { LINUX_SLL_MULTICAST, "M" },
  118. { LINUX_SLL_OTHERHOST, "P" },
  119. { LINUX_SLL_OUTGOING, "Out" },
  120. { 0, NULL}
  121. };
  122. static inline void
  123. sll_print(netdissect_options *ndo, register const struct sll_header *sllp, u_int length)
  124. {
  125. u_short ether_type;
  126. ND_PRINT((ndo, "%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_16BITS(&sllp->sll_pkttype))));
  127. /*
  128. * XXX - check the link-layer address type value?
  129. * For now, we just assume 6 means Ethernet.
  130. * XXX - print others as strings of hex?
  131. */
  132. if (EXTRACT_16BITS(&sllp->sll_halen) == 6)
  133. ND_PRINT((ndo, "%s ", etheraddr_string(ndo, sllp->sll_addr)));
  134. if (!ndo->ndo_qflag) {
  135. ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
  136. if (ether_type <= ETHERMTU) {
  137. /*
  138. * Not an Ethernet type; what type is it?
  139. */
  140. switch (ether_type) {
  141. case LINUX_SLL_P_802_3:
  142. /*
  143. * Ethernet_802.3 IPX frame.
  144. */
  145. ND_PRINT((ndo, "802.3"));
  146. break;
  147. case LINUX_SLL_P_802_2:
  148. /*
  149. * 802.2.
  150. */
  151. ND_PRINT((ndo, "802.2"));
  152. break;
  153. default:
  154. /*
  155. * What is it?
  156. */
  157. ND_PRINT((ndo, "ethertype Unknown (0x%04x)",
  158. ether_type));
  159. break;
  160. }
  161. } else {
  162. ND_PRINT((ndo, "ethertype %s (0x%04x)",
  163. tok2str(ethertype_values, "Unknown", ether_type),
  164. ether_type));
  165. }
  166. ND_PRINT((ndo, ", length %u: ", length));
  167. }
  168. }
  169. /*
  170. * This is the top level routine of the printer. 'p' points to the
  171. * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
  172. * 'h->len' is the length of the packet off the wire, and 'h->caplen'
  173. * is the number of bytes actually captured.
  174. */
  175. u_int
  176. sll_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
  177. {
  178. u_int caplen = h->caplen;
  179. u_int length = h->len;
  180. register const struct sll_header *sllp;
  181. u_short ether_type;
  182. int llc_hdrlen;
  183. u_int hdrlen;
  184. if (caplen < SLL_HDR_LEN) {
  185. /*
  186. * XXX - this "can't happen" because "pcap-linux.c" always
  187. * adds this many bytes of header to every packet in a
  188. * cooked socket capture.
  189. */
  190. ND_PRINT((ndo, "[|sll]"));
  191. return (caplen);
  192. }
  193. sllp = (const struct sll_header *)p;
  194. if (ndo->ndo_eflag)
  195. sll_print(ndo, sllp, length);
  196. /*
  197. * Go past the cooked-mode header.
  198. */
  199. length -= SLL_HDR_LEN;
  200. caplen -= SLL_HDR_LEN;
  201. p += SLL_HDR_LEN;
  202. hdrlen = SLL_HDR_LEN;
  203. ether_type = EXTRACT_16BITS(&sllp->sll_protocol);
  204. recurse:
  205. /*
  206. * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
  207. * packet type?
  208. */
  209. if (ether_type <= ETHERMTU) {
  210. /*
  211. * Yes - what type is it?
  212. */
  213. switch (ether_type) {
  214. case LINUX_SLL_P_802_3:
  215. /*
  216. * Ethernet_802.3 IPX frame.
  217. */
  218. ipx_print(ndo, p, length);
  219. break;
  220. case LINUX_SLL_P_802_2:
  221. /*
  222. * 802.2.
  223. * Try to print the LLC-layer header & higher layers.
  224. */
  225. llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
  226. if (llc_hdrlen < 0)
  227. goto unknown; /* unknown LLC type */
  228. hdrlen += llc_hdrlen;
  229. break;
  230. default:
  231. /*FALLTHROUGH*/
  232. unknown:
  233. /* packet type not known, print raw packet */
  234. if (!ndo->ndo_suppress_default_print)
  235. ND_DEFAULTPRINT(p, caplen);
  236. break;
  237. }
  238. } else if (ether_type == ETHERTYPE_8021Q) {
  239. /*
  240. * Print VLAN information, and then go back and process
  241. * the enclosed type field.
  242. */
  243. if (caplen < 4) {
  244. ND_PRINT((ndo, "[|vlan]"));
  245. return (hdrlen + caplen);
  246. }
  247. if (length < 4) {
  248. ND_PRINT((ndo, "[|vlan]"));
  249. return (hdrlen + length);
  250. }
  251. if (ndo->ndo_eflag) {
  252. uint16_t tag = EXTRACT_16BITS(p);
  253. ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
  254. }
  255. ether_type = EXTRACT_16BITS(p + 2);
  256. if (ether_type <= ETHERMTU)
  257. ether_type = LINUX_SLL_P_802_2;
  258. if (!ndo->ndo_qflag) {
  259. ND_PRINT((ndo, "ethertype %s, ",
  260. tok2str(ethertype_values, "Unknown", ether_type)));
  261. }
  262. p += 4;
  263. length -= 4;
  264. caplen -= 4;
  265. hdrlen += 4;
  266. goto recurse;
  267. } else {
  268. if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
  269. /* ether_type not known, print raw packet */
  270. if (!ndo->ndo_eflag)
  271. sll_print(ndo, sllp, length + SLL_HDR_LEN);
  272. if (!ndo->ndo_suppress_default_print)
  273. ND_DEFAULTPRINT(p, caplen);
  274. }
  275. }
  276. return (hdrlen);
  277. }