print-pktap.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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: Apple's DLT_PKTAP 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. #ifdef DLT_PKTAP
  29. /*
  30. * XXX - these are little-endian in the captures I've seen, but Apple
  31. * no longer make any big-endian machines (Macs use x86, iOS machines
  32. * use ARM and run it little-endian), so that might be by definition
  33. * or they might be host-endian.
  34. *
  35. * If a big-endian PKTAP file ever shows up, and it comes from a
  36. * big-endian machine, presumably these are host-endian, and we need
  37. * to just fetch the fields directly in tcpdump but byte-swap them
  38. * to host byte order in libpcap.
  39. */
  40. typedef struct pktap_header {
  41. uint32_t pkt_len; /* length of pktap header */
  42. uint32_t pkt_rectype; /* type of record */
  43. uint32_t pkt_dlt; /* DLT type of this packet */
  44. char pkt_ifname[24]; /* interface name */
  45. uint32_t pkt_flags;
  46. uint32_t pkt_pfamily; /* "protocol family" */
  47. uint32_t pkt_llhdrlen; /* link-layer header length? */
  48. uint32_t pkt_lltrlrlen; /* link-layer trailer length? */
  49. uint32_t pkt_pid; /* process ID */
  50. char pkt_cmdname[20]; /* command name */
  51. uint32_t pkt_svc_class; /* "service class" */
  52. uint16_t pkt_iftype; /* "interface type" */
  53. uint16_t pkt_ifunit; /* unit number of interface? */
  54. uint32_t pkt_epid; /* "effective process ID" */
  55. char pkt_ecmdname[20]; /* "effective command name" */
  56. } pktap_header_t;
  57. /*
  58. * Record types.
  59. */
  60. #define PKT_REC_NONE 0 /* nothing follows the header */
  61. #define PKT_REC_PACKET 1 /* a packet follows the header */
  62. static inline void
  63. pktap_header_print(netdissect_options *ndo, const u_char *bp, u_int length)
  64. {
  65. const pktap_header_t *hdr;
  66. uint32_t dlt, hdrlen;
  67. const char *dltname;
  68. hdr = (const pktap_header_t *)bp;
  69. dlt = EXTRACT_LE_32BITS(&hdr->pkt_dlt);
  70. hdrlen = EXTRACT_LE_32BITS(&hdr->pkt_len);
  71. dltname = pcap_datalink_val_to_name(dlt);
  72. if (!ndo->ndo_qflag) {
  73. ND_PRINT((ndo,"DLT %s (%d) len %d",
  74. (dltname != NULL ? dltname : "UNKNOWN"), dlt, hdrlen));
  75. } else {
  76. ND_PRINT((ndo,"%s", (dltname != NULL ? dltname : "UNKNOWN")));
  77. }
  78. ND_PRINT((ndo, ", length %u: ", length));
  79. }
  80. /*
  81. * This is the top level routine of the printer. 'p' points
  82. * to the ether header of the packet, 'h->ts' is the timestamp,
  83. * 'h->len' is the length of the packet off the wire, and 'h->caplen'
  84. * is the number of bytes actually captured.
  85. */
  86. u_int
  87. pktap_if_print(netdissect_options *ndo,
  88. const struct pcap_pkthdr *h, const u_char *p)
  89. {
  90. uint32_t dlt, hdrlen, rectype;
  91. u_int caplen = h->caplen;
  92. u_int length = h->len;
  93. if_printer printer;
  94. const pktap_header_t *hdr;
  95. struct pcap_pkthdr nhdr;
  96. if (caplen < sizeof(pktap_header_t) || length < sizeof(pktap_header_t)) {
  97. ND_PRINT((ndo, "[|pktap]"));
  98. return (0);
  99. }
  100. hdr = (const pktap_header_t *)p;
  101. dlt = EXTRACT_LE_32BITS(&hdr->pkt_dlt);
  102. hdrlen = EXTRACT_LE_32BITS(&hdr->pkt_len);
  103. if (hdrlen < sizeof(pktap_header_t)) {
  104. /*
  105. * Claimed header length < structure length.
  106. * XXX - does this just mean some fields aren't
  107. * being supplied, or is it truly an error (i.e.,
  108. * is the length supplied so that the header can
  109. * be expanded in the future)?
  110. */
  111. ND_PRINT((ndo, "[|pktap]"));
  112. return (0);
  113. }
  114. if (caplen < hdrlen || length < hdrlen) {
  115. ND_PRINT((ndo, "[|pktap]"));
  116. return (hdrlen);
  117. }
  118. if (ndo->ndo_eflag)
  119. pktap_header_print(ndo, p, length);
  120. length -= hdrlen;
  121. caplen -= hdrlen;
  122. p += hdrlen;
  123. rectype = EXTRACT_LE_32BITS(&hdr->pkt_rectype);
  124. switch (rectype) {
  125. case PKT_REC_NONE:
  126. ND_PRINT((ndo, "no data"));
  127. break;
  128. case PKT_REC_PACKET:
  129. if ((printer = lookup_printer(dlt)) != NULL) {
  130. nhdr = *h;
  131. nhdr.caplen = caplen;
  132. nhdr.len = length;
  133. hdrlen += printer(ndo, &nhdr, p);
  134. } else {
  135. if (!ndo->ndo_eflag)
  136. pktap_header_print(ndo, (const u_char *)hdr,
  137. length + hdrlen);
  138. if (!ndo->ndo_suppress_default_print)
  139. ND_DEFAULTPRINT(p, caplen);
  140. }
  141. break;
  142. }
  143. return (hdrlen);
  144. }
  145. /*
  146. * Local Variables:
  147. * c-style: whitesmith
  148. * c-basic-offset: 8
  149. * End:
  150. */
  151. #endif /* DLT_PKTAP */