writepcap.c 8.2 KB

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