writepcap.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * PCAP capture file writer
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include <pcap.h>
  10. #include <pcap-bpf.h>
  11. #include "utils/common.h"
  12. #include "wlantest.h"
  13. #include "common/qca-vendor.h"
  14. int write_pcap_init(struct wlantest *wt, const char *fname)
  15. {
  16. wt->write_pcap = pcap_open_dead(DLT_IEEE802_11_RADIO, 4000);
  17. if (wt->write_pcap == NULL)
  18. return -1;
  19. wt->write_pcap_dumper = pcap_dump_open(wt->write_pcap, fname);
  20. if (wt->write_pcap_dumper == NULL) {
  21. pcap_close(wt->write_pcap);
  22. wt->write_pcap = NULL;
  23. return -1;
  24. }
  25. wpa_printf(MSG_DEBUG, "Writing PCAP dump to '%s'", fname);
  26. return 0;
  27. }
  28. void write_pcap_deinit(struct wlantest *wt)
  29. {
  30. if (wt->write_pcap_dumper) {
  31. pcap_dump_close(wt->write_pcap_dumper);
  32. wt->write_pcap_dumper = NULL;
  33. }
  34. if (wt->write_pcap) {
  35. pcap_close(wt->write_pcap);
  36. wt->write_pcap = NULL;
  37. }
  38. }
  39. void write_pcap_captured(struct wlantest *wt, const u8 *buf, size_t len)
  40. {
  41. struct pcap_pkthdr h;
  42. if (!wt->write_pcap_dumper)
  43. return;
  44. os_memset(&h, 0, sizeof(h));
  45. gettimeofday(&wt->write_pcap_time, NULL);
  46. h.ts = wt->write_pcap_time;
  47. h.caplen = len;
  48. h.len = len;
  49. pcap_dump(wt->write_pcap_dumper, &h, buf);
  50. }
  51. void write_pcap_decrypted(struct wlantest *wt, const u8 *buf1, size_t len1,
  52. const u8 *buf2, size_t len2)
  53. {
  54. struct pcap_pkthdr h;
  55. u8 rtap[] = {
  56. 0x00 /* rev */,
  57. 0x00 /* pad */,
  58. 0x0e, 0x00, /* header len */
  59. 0x00, 0x00, 0x00, 0x40, /* present flags */
  60. 0x00, 0x13, 0x74, QCA_RADIOTAP_VID_WLANTEST,
  61. 0x00, 0x00
  62. };
  63. u8 *buf;
  64. size_t len;
  65. if (!wt->write_pcap_dumper && !wt->pcapng)
  66. return;
  67. os_free(wt->decrypted);
  68. len = sizeof(rtap) + len1 + len2;
  69. wt->decrypted = buf = os_malloc(len);
  70. if (buf == NULL)
  71. return;
  72. wt->decrypted_len = len;
  73. os_memcpy(buf, rtap, sizeof(rtap));
  74. if (buf1) {
  75. os_memcpy(buf + sizeof(rtap), buf1, len1);
  76. buf[sizeof(rtap) + 1] &= ~0x40; /* Clear Protected flag */
  77. }
  78. if (buf2)
  79. os_memcpy(buf + sizeof(rtap) + len1, buf2, len2);
  80. if (!wt->write_pcap_dumper)
  81. return;
  82. os_memset(&h, 0, sizeof(h));
  83. h.ts = wt->write_pcap_time;
  84. h.caplen = len;
  85. h.len = len;
  86. pcap_dump(wt->write_pcap_dumper, &h, buf);
  87. }
  88. struct pcapng_section_header {
  89. u32 block_type; /* 0x0a0d0d0a */
  90. u32 block_total_len;
  91. u32 byte_order_magic;
  92. u16 major_version;
  93. u16 minor_version;
  94. u64 section_len;
  95. u32 block_total_len2;
  96. } STRUCT_PACKED;
  97. struct pcapng_interface_description {
  98. u32 block_type; /* 0x00000001 */
  99. u32 block_total_len;
  100. u16 link_type;
  101. u16 reserved;
  102. u32 snap_len;
  103. u32 block_total_len2;
  104. } STRUCT_PACKED;
  105. struct pcapng_enhanced_packet {
  106. u32 block_type; /* 0x00000006 */
  107. u32 block_total_len;
  108. u32 interface_id;
  109. u32 timestamp_high;
  110. u32 timestamp_low;
  111. u32 captured_len;
  112. u32 packet_len;
  113. /* Packet data - aligned to 32 bits */
  114. /* Options (variable) */
  115. /* Block Total Length copy */
  116. } STRUCT_PACKED;
  117. #define PCAPNG_BYTE_ORDER_MAGIC 0x1a2b3c4d
  118. #define PCAPNG_BLOCK_IFACE_DESC 0x00000001
  119. #define PCAPNG_BLOCK_PACKET 0x00000002
  120. #define PCAPNG_BLOCK_SIMPLE_PACKET 0x00000003
  121. #define PCAPNG_BLOCK_NAME_RESOLUTION 0x00000004
  122. #define PCAPNG_BLOCK_INTERFACE_STATISTICS 0x00000005
  123. #define PCAPNG_BLOCK_ENHANCED_PACKET 0x00000006
  124. #define PCAPNG_BLOCK_SECTION_HEADER 0x0a0d0d0a
  125. #define LINKTYPE_IEEE802_11 105
  126. #define LINKTYPE_IEEE802_11_RADIO 127
  127. #define PAD32(a) ((4 - ((a) & 3)) & 3)
  128. #define ALIGN32(a) ((a) + PAD32((a)))
  129. int write_pcapng_init(struct wlantest *wt, const char *fname)
  130. {
  131. struct pcapng_section_header hdr;
  132. struct pcapng_interface_description desc;
  133. wt->pcapng = fopen(fname, "wb");
  134. if (wt->pcapng == NULL)
  135. return -1;
  136. wpa_printf(MSG_DEBUG, "Writing PCAPNG dump to '%s'", fname);
  137. os_memset(&hdr, 0, sizeof(hdr));
  138. hdr.block_type = PCAPNG_BLOCK_SECTION_HEADER;
  139. hdr.block_total_len = sizeof(hdr);
  140. hdr.byte_order_magic = PCAPNG_BYTE_ORDER_MAGIC;
  141. hdr.major_version = 1;
  142. hdr.minor_version = 0;
  143. hdr.section_len = -1;
  144. hdr.block_total_len2 = hdr.block_total_len;
  145. fwrite(&hdr, sizeof(hdr), 1, wt->pcapng);
  146. os_memset(&desc, 0, sizeof(desc));
  147. desc.block_type = PCAPNG_BLOCK_IFACE_DESC;
  148. desc.block_total_len = sizeof(desc);
  149. desc.block_total_len2 = desc.block_total_len;
  150. desc.link_type = LINKTYPE_IEEE802_11_RADIO;
  151. desc.snap_len = 65535;
  152. fwrite(&desc, sizeof(desc), 1, wt->pcapng);
  153. return 0;
  154. }
  155. void write_pcapng_deinit(struct wlantest *wt)
  156. {
  157. if (wt->pcapng) {
  158. fclose(wt->pcapng);
  159. wt->pcapng = NULL;
  160. }
  161. }
  162. static u8 * pcapng_add_comments(struct wlantest *wt, u8 *pos)
  163. {
  164. size_t i;
  165. u16 *len;
  166. if (!wt->num_notes)
  167. return pos;
  168. *((u16 *) pos) = 1 /* opt_comment */;
  169. pos += 2;
  170. len = (u16 *) pos /* length to be filled in */;
  171. pos += 2;
  172. for (i = 0; i < wt->num_notes; i++) {
  173. size_t nlen = os_strlen(wt->notes[i]);
  174. if (i > 0)
  175. *pos++ = '\n';
  176. os_memcpy(pos, wt->notes[i], nlen);
  177. pos += nlen;
  178. }
  179. *len = pos - (u8 *) len - 2;
  180. pos += PAD32(*len);
  181. *((u16 *) pos) = 0 /* opt_endofopt */;
  182. pos += 2;
  183. *((u16 *) pos) = 0;
  184. pos += 2;
  185. return pos;
  186. }
  187. static void write_pcapng_decrypted(struct wlantest *wt)
  188. {
  189. size_t len;
  190. struct pcapng_enhanced_packet *pkt;
  191. u8 *pos;
  192. u32 *block_len;
  193. if (!wt->pcapng || wt->decrypted == NULL)
  194. return;
  195. add_note(wt, MSG_EXCESSIVE, "decrypted version of the previous frame");
  196. len = sizeof(*pkt) + wt->decrypted_len + 100 + notes_len(wt, 32);
  197. pkt = os_zalloc(len);
  198. if (pkt == NULL)
  199. return;
  200. pkt->block_type = PCAPNG_BLOCK_ENHANCED_PACKET;
  201. pkt->interface_id = 0;
  202. pkt->timestamp_high = wt->write_pcapng_time_high;
  203. pkt->timestamp_low = wt->write_pcapng_time_low;
  204. pkt->captured_len = wt->decrypted_len;
  205. pkt->packet_len = wt->decrypted_len;
  206. pos = (u8 *) (pkt + 1);
  207. os_memcpy(pos, wt->decrypted, wt->decrypted_len);
  208. pos += ALIGN32(wt->decrypted_len);
  209. pos = pcapng_add_comments(wt, pos);
  210. block_len = (u32 *) pos;
  211. pos += 4;
  212. *block_len = pkt->block_total_len = pos - (u8 *) pkt;
  213. fwrite(pkt, pos - (u8 *) pkt, 1, wt->pcapng);
  214. os_free(pkt);
  215. }
  216. void write_pcapng_write_read(struct wlantest *wt, int dlt,
  217. struct pcap_pkthdr *hdr, const u8 *data)
  218. {
  219. struct pcapng_enhanced_packet *pkt;
  220. u8 *pos;
  221. u32 *block_len;
  222. u64 timestamp;
  223. size_t len, datalen = hdr->caplen;
  224. u8 rtap[] = {
  225. 0x00 /* rev */,
  226. 0x00 /* pad */,
  227. 0x0a, 0x00, /* header len */
  228. 0x02, 0x00, 0x00, 0x00, /* present flags */
  229. 0x00, /* flags */
  230. 0x00 /* pad */
  231. };
  232. if (wt->assume_fcs)
  233. rtap[8] |= 0x10;
  234. if (!wt->pcapng)
  235. return;
  236. len = sizeof(*pkt) + hdr->len + 100 + notes_len(wt, 32) + sizeof(rtap);
  237. pkt = os_zalloc(len);
  238. if (pkt == NULL)
  239. return;
  240. pkt->block_type = PCAPNG_BLOCK_ENHANCED_PACKET;
  241. pkt->interface_id = 0;
  242. timestamp = 1000000 * hdr->ts.tv_sec + hdr->ts.tv_usec;
  243. pkt->timestamp_high = timestamp >> 32;
  244. pkt->timestamp_low = timestamp & 0xffffffff;
  245. wt->write_pcapng_time_high = pkt->timestamp_high;
  246. wt->write_pcapng_time_low = pkt->timestamp_low;
  247. pkt->captured_len = hdr->caplen;
  248. pkt->packet_len = hdr->len;
  249. pos = (u8 *) (pkt + 1);
  250. switch (dlt) {
  251. case DLT_IEEE802_11_RADIO:
  252. break;
  253. case DLT_PRISM_HEADER:
  254. /* remove prism header (could be kept ... lazy) */
  255. pkt->captured_len -= WPA_GET_LE32(data + 4);
  256. pkt->packet_len -= WPA_GET_LE32(data + 4);
  257. datalen -= WPA_GET_LE32(data + 4);
  258. data += WPA_GET_LE32(data + 4);
  259. /* fall through */
  260. case DLT_IEEE802_11:
  261. pkt->captured_len += sizeof(rtap);
  262. pkt->packet_len += sizeof(rtap);
  263. os_memcpy(pos, &rtap, sizeof(rtap));
  264. pos += sizeof(rtap);
  265. break;
  266. default:
  267. return;
  268. }
  269. os_memcpy(pos, data, datalen);
  270. pos += datalen + PAD32(pkt->captured_len);
  271. pos = pcapng_add_comments(wt, pos);
  272. block_len = (u32 *) pos;
  273. pos += 4;
  274. *block_len = pkt->block_total_len = pos - (u8 *) pkt;
  275. fwrite(pkt, pos - (u8 *) pkt, 1, wt->pcapng);
  276. os_free(pkt);
  277. write_pcapng_decrypted(wt);
  278. }
  279. void write_pcapng_captured(struct wlantest *wt, const u8 *buf, size_t len)
  280. {
  281. struct pcap_pkthdr h;
  282. if (!wt->pcapng)
  283. return;
  284. os_memset(&h, 0, sizeof(h));
  285. gettimeofday(&h.ts, NULL);
  286. h.caplen = len;
  287. h.len = len;
  288. write_pcapng_write_read(wt, DLT_IEEE802_11_RADIO, &h, buf);
  289. }