print-ether.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
  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: Ethernet 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 "addrtoname.h"
  29. #include "ethertype.h"
  30. #include "ether.h"
  31. const struct tok ethertype_values[] = {
  32. { ETHERTYPE_IP, "IPv4" },
  33. { ETHERTYPE_MPLS, "MPLS unicast" },
  34. { ETHERTYPE_MPLS_MULTI, "MPLS multicast" },
  35. { ETHERTYPE_IPV6, "IPv6" },
  36. { ETHERTYPE_8021Q, "802.1Q" },
  37. { ETHERTYPE_8021Q9100, "802.1Q-9100" },
  38. { ETHERTYPE_8021QinQ, "802.1Q-QinQ" },
  39. { ETHERTYPE_8021Q9200, "802.1Q-9200" },
  40. { ETHERTYPE_VMAN, "VMAN" },
  41. { ETHERTYPE_PUP, "PUP" },
  42. { ETHERTYPE_ARP, "ARP"},
  43. { ETHERTYPE_REVARP, "Reverse ARP"},
  44. { ETHERTYPE_NS, "NS" },
  45. { ETHERTYPE_SPRITE, "Sprite" },
  46. { ETHERTYPE_TRAIL, "Trail" },
  47. { ETHERTYPE_MOPDL, "MOP DL" },
  48. { ETHERTYPE_MOPRC, "MOP RC" },
  49. { ETHERTYPE_DN, "DN" },
  50. { ETHERTYPE_LAT, "LAT" },
  51. { ETHERTYPE_SCA, "SCA" },
  52. { ETHERTYPE_TEB, "TEB" },
  53. { ETHERTYPE_LANBRIDGE, "Lanbridge" },
  54. { ETHERTYPE_DECDNS, "DEC DNS" },
  55. { ETHERTYPE_DECDTS, "DEC DTS" },
  56. { ETHERTYPE_VEXP, "VEXP" },
  57. { ETHERTYPE_VPROD, "VPROD" },
  58. { ETHERTYPE_ATALK, "Appletalk" },
  59. { ETHERTYPE_AARP, "Appletalk ARP" },
  60. { ETHERTYPE_IPX, "IPX" },
  61. { ETHERTYPE_PPP, "PPP" },
  62. { ETHERTYPE_MPCP, "MPCP" },
  63. { ETHERTYPE_SLOW, "Slow Protocols" },
  64. { ETHERTYPE_PPPOED, "PPPoE D" },
  65. { ETHERTYPE_PPPOES, "PPPoE S" },
  66. { ETHERTYPE_EAPOL, "EAPOL" },
  67. { ETHERTYPE_RRCP, "RRCP" },
  68. { ETHERTYPE_MS_NLB_HB, "MS NLB heartbeat" },
  69. { ETHERTYPE_JUMBO, "Jumbo" },
  70. { ETHERTYPE_LOOPBACK, "Loopback" },
  71. { ETHERTYPE_ISO, "OSI" },
  72. { ETHERTYPE_GRE_ISO, "GRE-OSI" },
  73. { ETHERTYPE_CFM_OLD, "CFM (old)" },
  74. { ETHERTYPE_CFM, "CFM" },
  75. { ETHERTYPE_IEEE1905_1, "IEEE1905.1" },
  76. { ETHERTYPE_LLDP, "LLDP" },
  77. { ETHERTYPE_TIPC, "TIPC"},
  78. { ETHERTYPE_GEONET_OLD, "GeoNet (old)"},
  79. { ETHERTYPE_GEONET, "GeoNet"},
  80. { ETHERTYPE_CALM_FAST, "CALM FAST"},
  81. { ETHERTYPE_AOE, "AoE" },
  82. { ETHERTYPE_MEDSA, "MEDSA" },
  83. { 0, NULL}
  84. };
  85. static inline void
  86. ether_hdr_print(netdissect_options *ndo,
  87. const u_char *bp, u_int length)
  88. {
  89. register const struct ether_header *ep;
  90. uint16_t length_type;
  91. ep = (const struct ether_header *)bp;
  92. ND_PRINT((ndo, "%s > %s",
  93. etheraddr_string(ndo, ESRC(ep)),
  94. etheraddr_string(ndo, EDST(ep))));
  95. length_type = EXTRACT_16BITS(&ep->ether_length_type);
  96. if (!ndo->ndo_qflag) {
  97. if (length_type <= ETHERMTU) {
  98. ND_PRINT((ndo, ", 802.3"));
  99. length = length_type;
  100. } else
  101. ND_PRINT((ndo, ", ethertype %s (0x%04x)",
  102. tok2str(ethertype_values,"Unknown", length_type),
  103. length_type));
  104. } else {
  105. if (length_type <= ETHERMTU) {
  106. ND_PRINT((ndo, ", 802.3"));
  107. length = length_type;
  108. } else
  109. ND_PRINT((ndo, ", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", length_type)));
  110. }
  111. ND_PRINT((ndo, ", length %u: ", length));
  112. }
  113. /*
  114. * Print an Ethernet frame.
  115. * This might be encapsulated within another frame; we might be passed
  116. * a pointer to a function that can print header information for that
  117. * frame's protocol, and an argument to pass to that function.
  118. *
  119. * FIXME: caplen can and should be derived from ndo->ndo_snapend and p.
  120. */
  121. u_int
  122. ether_print(netdissect_options *ndo,
  123. const u_char *p, u_int length, u_int caplen,
  124. void (*print_encap_header)(netdissect_options *ndo, const u_char *), const u_char *encap_header_arg)
  125. {
  126. const struct ether_header *ep;
  127. u_int orig_length;
  128. u_short length_type;
  129. u_int hdrlen;
  130. int llc_hdrlen;
  131. struct lladdr_info src, dst;
  132. if (caplen < ETHER_HDRLEN) {
  133. ND_PRINT((ndo, "[|ether]"));
  134. return (caplen);
  135. }
  136. if (length < ETHER_HDRLEN) {
  137. ND_PRINT((ndo, "[|ether]"));
  138. return (length);
  139. }
  140. if (ndo->ndo_eflag) {
  141. if (print_encap_header != NULL)
  142. (*print_encap_header)(ndo, encap_header_arg);
  143. ether_hdr_print(ndo, p, length);
  144. }
  145. orig_length = length;
  146. length -= ETHER_HDRLEN;
  147. caplen -= ETHER_HDRLEN;
  148. ep = (const struct ether_header *)p;
  149. p += ETHER_HDRLEN;
  150. hdrlen = ETHER_HDRLEN;
  151. src.addr = ESRC(ep);
  152. src.addr_string = etheraddr_string;
  153. dst.addr = EDST(ep);
  154. dst.addr_string = etheraddr_string;
  155. length_type = EXTRACT_16BITS(&ep->ether_length_type);
  156. recurse:
  157. /*
  158. * Is it (gag) an 802.3 encapsulation?
  159. */
  160. if (length_type <= ETHERMTU) {
  161. /* Try to print the LLC-layer header & higher layers */
  162. llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
  163. if (llc_hdrlen < 0) {
  164. /* packet type not known, print raw packet */
  165. if (!ndo->ndo_suppress_default_print)
  166. ND_DEFAULTPRINT(p, caplen);
  167. llc_hdrlen = -llc_hdrlen;
  168. }
  169. hdrlen += llc_hdrlen;
  170. } else if (length_type == ETHERTYPE_8021Q ||
  171. length_type == ETHERTYPE_8021Q9100 ||
  172. length_type == ETHERTYPE_8021Q9200 ||
  173. length_type == ETHERTYPE_8021QinQ) {
  174. /*
  175. * Print VLAN information, and then go back and process
  176. * the enclosed type field.
  177. */
  178. if (caplen < 4) {
  179. ND_PRINT((ndo, "[|vlan]"));
  180. return (hdrlen + caplen);
  181. }
  182. if (length < 4) {
  183. ND_PRINT((ndo, "[|vlan]"));
  184. return (hdrlen + length);
  185. }
  186. if (ndo->ndo_eflag) {
  187. uint16_t tag = EXTRACT_16BITS(p);
  188. ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
  189. }
  190. length_type = EXTRACT_16BITS(p + 2);
  191. if (ndo->ndo_eflag && length_type > ETHERMTU)
  192. ND_PRINT((ndo, "ethertype %s, ", tok2str(ethertype_values,"0x%04x", length_type)));
  193. p += 4;
  194. length -= 4;
  195. caplen -= 4;
  196. hdrlen += 4;
  197. goto recurse;
  198. } else if (length_type == ETHERTYPE_JUMBO) {
  199. /*
  200. * Alteon jumbo frames.
  201. * See
  202. *
  203. * http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
  204. *
  205. * which indicates that, following the type field,
  206. * there's an LLC header and payload.
  207. */
  208. /* Try to print the LLC-layer header & higher layers */
  209. llc_hdrlen = llc_print(ndo, p, length, caplen, &src, &dst);
  210. if (llc_hdrlen < 0) {
  211. /* packet type not known, print raw packet */
  212. if (!ndo->ndo_suppress_default_print)
  213. ND_DEFAULTPRINT(p, caplen);
  214. llc_hdrlen = -llc_hdrlen;
  215. }
  216. hdrlen += llc_hdrlen;
  217. } else {
  218. if (ethertype_print(ndo, length_type, p, length, caplen, &src, &dst) == 0) {
  219. /* type not known, print raw packet */
  220. if (!ndo->ndo_eflag) {
  221. if (print_encap_header != NULL)
  222. (*print_encap_header)(ndo, encap_header_arg);
  223. ether_hdr_print(ndo, (const u_char *)ep, orig_length);
  224. }
  225. if (!ndo->ndo_suppress_default_print)
  226. ND_DEFAULTPRINT(p, caplen);
  227. }
  228. }
  229. return (hdrlen);
  230. }
  231. /*
  232. * This is the top level routine of the printer. 'p' points
  233. * to the ether header of the packet, 'h->len' is the length
  234. * of the packet off the wire, and 'h->caplen' is the number
  235. * of bytes actually captured.
  236. */
  237. u_int
  238. ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
  239. const u_char *p)
  240. {
  241. return (ether_print(ndo, p, h->len, h->caplen, NULL, NULL));
  242. }
  243. /*
  244. * This is the top level routine of the printer. 'p' points
  245. * to the ether header of the packet, 'h->len' is the length
  246. * of the packet off the wire, and 'h->caplen' is the number
  247. * of bytes actually captured.
  248. *
  249. * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
  250. * before the Ethernet header.
  251. */
  252. u_int
  253. netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
  254. const u_char *p)
  255. {
  256. /*
  257. * Fail if we don't have enough data for the Hilscher pseudo-header.
  258. */
  259. if (h->len < 4 || h->caplen < 4) {
  260. ND_PRINT((ndo, "[|netanalyzer]"));
  261. return (h->caplen);
  262. }
  263. /* Skip the pseudo-header. */
  264. return (4 + ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL));
  265. }
  266. /*
  267. * This is the top level routine of the printer. 'p' points
  268. * to the ether header of the packet, 'h->len' is the length
  269. * of the packet off the wire, and 'h->caplen' is the number
  270. * of bytes actually captured.
  271. *
  272. * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
  273. * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
  274. * before the Ethernet header.
  275. */
  276. u_int
  277. netanalyzer_transparent_if_print(netdissect_options *ndo,
  278. const struct pcap_pkthdr *h,
  279. const u_char *p)
  280. {
  281. /*
  282. * Fail if we don't have enough data for the Hilscher pseudo-header,
  283. * preamble, and SOF.
  284. */
  285. if (h->len < 12 || h->caplen < 12) {
  286. ND_PRINT((ndo, "[|netanalyzer-transparent]"));
  287. return (h->caplen);
  288. }
  289. /* Skip the pseudo-header, preamble, and SOF. */
  290. return (12 + ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL));
  291. }
  292. /*
  293. * Prints the packet payload, given an Ethernet type code for the payload's
  294. * protocol.
  295. *
  296. * Returns non-zero if it can do so, zero if the ethertype is unknown.
  297. */
  298. int
  299. ethertype_print(netdissect_options *ndo,
  300. u_short ether_type, const u_char *p,
  301. u_int length, u_int caplen,
  302. const struct lladdr_info *src, const struct lladdr_info *dst)
  303. {
  304. switch (ether_type) {
  305. case ETHERTYPE_IP:
  306. ip_print(ndo, p, length);
  307. return (1);
  308. case ETHERTYPE_IPV6:
  309. ip6_print(ndo, p, length);
  310. return (1);
  311. case ETHERTYPE_ARP:
  312. case ETHERTYPE_REVARP:
  313. arp_print(ndo, p, length, caplen);
  314. return (1);
  315. case ETHERTYPE_DN:
  316. decnet_print(ndo, p, length, caplen);
  317. return (1);
  318. case ETHERTYPE_ATALK:
  319. if (ndo->ndo_vflag)
  320. ND_PRINT((ndo, "et1 "));
  321. atalk_print(ndo, p, length);
  322. return (1);
  323. case ETHERTYPE_AARP:
  324. aarp_print(ndo, p, length);
  325. return (1);
  326. case ETHERTYPE_IPX:
  327. ND_PRINT((ndo, "(NOV-ETHII) "));
  328. ipx_print(ndo, p, length);
  329. return (1);
  330. case ETHERTYPE_ISO:
  331. if (length == 0 || caplen == 0) {
  332. ND_PRINT((ndo, " [|osi]"));
  333. return (1);
  334. }
  335. isoclns_print(ndo, p + 1, length - 1);
  336. return(1);
  337. case ETHERTYPE_PPPOED:
  338. case ETHERTYPE_PPPOES:
  339. case ETHERTYPE_PPPOED2:
  340. case ETHERTYPE_PPPOES2:
  341. pppoe_print(ndo, p, length);
  342. return (1);
  343. case ETHERTYPE_EAPOL:
  344. eap_print(ndo, p, length);
  345. return (1);
  346. case ETHERTYPE_RRCP:
  347. rrcp_print(ndo, p, length, src, dst);
  348. return (1);
  349. case ETHERTYPE_PPP:
  350. if (length) {
  351. ND_PRINT((ndo, ": "));
  352. ppp_print(ndo, p, length);
  353. }
  354. return (1);
  355. case ETHERTYPE_MPCP:
  356. mpcp_print(ndo, p, length);
  357. return (1);
  358. case ETHERTYPE_SLOW:
  359. slow_print(ndo, p, length);
  360. return (1);
  361. case ETHERTYPE_CFM:
  362. case ETHERTYPE_CFM_OLD:
  363. cfm_print(ndo, p, length);
  364. return (1);
  365. case ETHERTYPE_LLDP:
  366. lldp_print(ndo, p, length);
  367. return (1);
  368. case ETHERTYPE_LOOPBACK:
  369. loopback_print(ndo, p, length);
  370. return (1);
  371. case ETHERTYPE_MPLS:
  372. case ETHERTYPE_MPLS_MULTI:
  373. mpls_print(ndo, p, length);
  374. return (1);
  375. case ETHERTYPE_TIPC:
  376. tipc_print(ndo, p, length, caplen);
  377. return (1);
  378. case ETHERTYPE_MS_NLB_HB:
  379. msnlb_print(ndo, p);
  380. return (1);
  381. case ETHERTYPE_GEONET_OLD:
  382. case ETHERTYPE_GEONET:
  383. geonet_print(ndo, p, length, src);
  384. return (1);
  385. case ETHERTYPE_CALM_FAST:
  386. calm_fast_print(ndo, p, length, src);
  387. return (1);
  388. case ETHERTYPE_AOE:
  389. aoe_print(ndo, p, length);
  390. return (1);
  391. case ETHERTYPE_MEDSA:
  392. medsa_print(ndo, p, length, caplen, src, dst);
  393. return (1);
  394. case ETHERTYPE_LAT:
  395. case ETHERTYPE_SCA:
  396. case ETHERTYPE_MOPRC:
  397. case ETHERTYPE_MOPDL:
  398. case ETHERTYPE_IEEE1905_1:
  399. /* default_print for now */
  400. default:
  401. return (0);
  402. }
  403. }
  404. /*
  405. * Local Variables:
  406. * c-style: whitesmith
  407. * c-basic-offset: 8
  408. * End:
  409. */