writepcap.c 7.8 KB

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