2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2013 Daniel Borkmann.
4 * Copyright 2010 Emmanuel Roullit.
5 * Subject to the GPL, version 2.
16 #include <linux/if_packet.h>
22 #define TCPDUMP_MAGIC 0xa1b2c3d4
23 #define ORIGINAL_TCPDUMP_MAGIC TCPDUMP_MAGIC
24 #define NSEC_TCPDUMP_MAGIC 0xa1b23c4d
25 #define KUZNETZOV_TCPDUMP_MAGIC 0xa1b2cd34
26 #define BORKMANN_TCPDUMP_MAGIC 0xa1e2cb12
28 #define PCAP_VERSION_MAJOR 2
29 #define PCAP_VERSION_MINOR 4
30 #define PCAP_DEFAULT_SNAPSHOT_LEN 65535
32 #define PCAP_TSOURCE_SOFTWARE 1
33 #define PCAP_TSOURCE_SYS_HARDWARE 2
34 #define PCAP_TSOURCE_RAW_HARDWARE 3
36 #define LINKTYPE_EN10MB 1 /* Ethernet (10Mb) */
37 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 wireless */
41 uint16_t version_major
;
42 uint16_t version_minor
;
54 struct pcap_timeval_ns
{
60 struct pcap_timeval ts
;
65 struct pcap_pkthdr_ns
{
66 struct pcap_timeval_ns ts
;
71 struct pcap_pkthdr_kuz
{
72 struct pcap_timeval ts
;
80 struct pcap_pkthdr_bkm
{
81 struct pcap_timeval_ns ts
;
92 struct pcap_pkthdr ppo
;
93 struct pcap_pkthdr_ns ppn
;
94 struct pcap_pkthdr_kuz ppk
;
95 struct pcap_pkthdr_bkm ppb
;
100 DEFAULT
= ORIGINAL_TCPDUMP_MAGIC
,
101 NSEC
= NSEC_TCPDUMP_MAGIC
,
102 KUZNETZOV
= KUZNETZOV_TCPDUMP_MAGIC
,
103 BORKMANN
= BORKMANN_TCPDUMP_MAGIC
,
105 DEFAULT_SWAPPED
= ___constant_swab32(ORIGINAL_TCPDUMP_MAGIC
),
106 NSEC_SWAPPED
= ___constant_swab32(NSEC_TCPDUMP_MAGIC
),
107 KUZNETZOV_SWAPPED
= ___constant_swab32(KUZNETZOV_TCPDUMP_MAGIC
),
108 BORKMANN_SWAPPED
= ___constant_swab32(BORKMANN_TCPDUMP_MAGIC
),
111 enum pcap_ops_groups
{
122 struct pcap_file_ops
{
123 void (*init_once_pcap
)(void);
124 int (*pull_fhdr_pcap
)(int fd
, uint32_t *magic
, uint32_t *linktype
);
125 int (*push_fhdr_pcap
)(int fd
, uint32_t magic
, uint32_t linktype
);
126 int (*prepare_access_pcap
)(int fd
, enum pcap_mode mode
, bool jumbo
);
127 ssize_t (*write_pcap
)(int fd
, pcap_pkthdr_t
*phdr
, enum pcap_type type
,
128 const uint8_t *packet
, size_t len
);
129 ssize_t (*read_pcap
)(int fd
, pcap_pkthdr_t
*phdr
, enum pcap_type type
,
130 uint8_t *packet
, size_t len
);
131 void (*prepare_close_pcap
)(int fd
, enum pcap_mode mode
);
132 void (*fsync_pcap
)(int fd
);
135 extern const struct pcap_file_ops pcap_rw_ops __maybe_unused
;
136 extern const struct pcap_file_ops pcap_sg_ops __maybe_unused
;
137 extern const struct pcap_file_ops pcap_mm_ops __maybe_unused
;
139 static inline uint16_t tp_to_pcap_tsource(uint32_t status
)
141 if (status
& TP_STATUS_TS_RAW_HARDWARE
)
142 return PCAP_TSOURCE_RAW_HARDWARE
;
143 else if (status
& TP_STATUS_TS_SYS_HARDWARE
)
144 return PCAP_TSOURCE_SYS_HARDWARE
;
145 else if (status
& TP_STATUS_TS_SOFTWARE
)
146 return PCAP_TSOURCE_SOFTWARE
;
151 static inline void pcap_check_magic(uint32_t magic
)
155 case ORIGINAL_TCPDUMP_MAGIC
:
156 case NSEC_TCPDUMP_MAGIC
:
157 case KUZNETZOV_TCPDUMP_MAGIC
:
158 case BORKMANN_TCPDUMP_MAGIC
:
160 case ___constant_swab32(ORIGINAL_TCPDUMP_MAGIC
):
161 case ___constant_swab32(NSEC_TCPDUMP_MAGIC
):
162 case ___constant_swab32(KUZNETZOV_TCPDUMP_MAGIC
):
163 case ___constant_swab32(BORKMANN_TCPDUMP_MAGIC
):
167 panic("This file has not a valid pcap header\n");
171 static inline bool pcap_magic_is_swapped(uint32_t magic
)
173 bool swapped
= false;
176 case ___constant_swab32(ORIGINAL_TCPDUMP_MAGIC
):
177 case ___constant_swab32(NSEC_TCPDUMP_MAGIC
):
178 case ___constant_swab32(KUZNETZOV_TCPDUMP_MAGIC
):
179 case ___constant_swab32(BORKMANN_TCPDUMP_MAGIC
):
186 static inline u32
pcap_get_length(pcap_pkthdr_t
*phdr
, enum pcap_type type
)
189 #define CASE_RET_CAPLEN(what, member, swap) \
191 return (swap ? ___constant_swab32(phdr->member.caplen) : \
194 CASE_RET_CAPLEN(DEFAULT
, ppo
, 0);
195 CASE_RET_CAPLEN(NSEC
, ppn
, 0);
196 CASE_RET_CAPLEN(KUZNETZOV
, ppk
, 0);
197 CASE_RET_CAPLEN(BORKMANN
, ppb
, 0);
199 CASE_RET_CAPLEN(DEFAULT_SWAPPED
, ppo
, 1);
200 CASE_RET_CAPLEN(NSEC_SWAPPED
, ppn
, 1);
201 CASE_RET_CAPLEN(KUZNETZOV_SWAPPED
, ppk
, 1);
202 CASE_RET_CAPLEN(BORKMANN_SWAPPED
, ppb
, 1);
209 static inline void pcap_set_length(pcap_pkthdr_t
*phdr
, enum pcap_type type
, u32 len
)
212 #define CASE_SET_CAPLEN(what, member, swap) \
214 phdr->member.caplen = (swap ? ___constant_swab32(len) : len); \
217 CASE_SET_CAPLEN(DEFAULT
, ppo
, 0);
218 CASE_SET_CAPLEN(NSEC
, ppn
, 0);
219 CASE_SET_CAPLEN(KUZNETZOV
, ppk
, 0);
220 CASE_SET_CAPLEN(BORKMANN
, ppb
, 0);
222 CASE_SET_CAPLEN(DEFAULT_SWAPPED
, ppo
, 1);
223 CASE_SET_CAPLEN(NSEC_SWAPPED
, ppn
, 1);
224 CASE_SET_CAPLEN(KUZNETZOV_SWAPPED
, ppk
, 1);
225 CASE_SET_CAPLEN(BORKMANN_SWAPPED
, ppb
, 1);
232 static inline u32
pcap_get_hdr_length(pcap_pkthdr_t
*phdr
, enum pcap_type type
)
235 #define CASE_RET_HDRLEN(what, member) \
237 return sizeof(phdr->member)
239 CASE_RET_HDRLEN(DEFAULT
, ppo
);
240 CASE_RET_HDRLEN(NSEC
, ppn
);
241 CASE_RET_HDRLEN(KUZNETZOV
, ppk
);
242 CASE_RET_HDRLEN(BORKMANN
, ppb
);
244 CASE_RET_HDRLEN(DEFAULT_SWAPPED
, ppo
);
245 CASE_RET_HDRLEN(NSEC_SWAPPED
, ppn
);
246 CASE_RET_HDRLEN(KUZNETZOV_SWAPPED
, ppk
);
247 CASE_RET_HDRLEN(BORKMANN_SWAPPED
, ppb
);
254 static inline u32
pcap_get_total_length(pcap_pkthdr_t
*phdr
, enum pcap_type type
)
257 #define CASE_RET_TOTLEN(what, member, swap) \
259 return ((swap ? ___constant_swab32(phdr->member.caplen) : \
260 phdr->member.caplen) + sizeof(phdr->member))
262 CASE_RET_TOTLEN(DEFAULT
, ppo
, 0);
263 CASE_RET_TOTLEN(NSEC
, ppn
, 0);
264 CASE_RET_TOTLEN(KUZNETZOV
, ppk
, 0);
265 CASE_RET_TOTLEN(BORKMANN
, ppb
, 0);
267 CASE_RET_TOTLEN(DEFAULT_SWAPPED
, ppo
, 1);
268 CASE_RET_TOTLEN(NSEC_SWAPPED
, ppn
, 1);
269 CASE_RET_TOTLEN(KUZNETZOV_SWAPPED
, ppk
, 1);
270 CASE_RET_TOTLEN(BORKMANN_SWAPPED
, ppb
, 1);
277 static inline void tpacket_hdr_to_pcap_pkthdr(struct tpacket2_hdr
*thdr
,
278 struct sockaddr_ll
*sll
,
284 phdr
->ppo
.ts
.tv_sec
= thdr
->tp_sec
;
285 phdr
->ppo
.ts
.tv_usec
= thdr
->tp_nsec
/ 1000;
286 phdr
->ppo
.caplen
= thdr
->tp_snaplen
;
287 phdr
->ppo
.len
= thdr
->tp_len
;
290 case DEFAULT_SWAPPED
:
291 phdr
->ppo
.ts
.tv_sec
= ___constant_swab32(thdr
->tp_sec
);
292 phdr
->ppo
.ts
.tv_usec
= ___constant_swab32(thdr
->tp_nsec
/ 1000);
293 phdr
->ppo
.caplen
= ___constant_swab32(thdr
->tp_snaplen
);
294 phdr
->ppo
.len
= ___constant_swab32(thdr
->tp_len
);
298 phdr
->ppn
.ts
.tv_sec
= thdr
->tp_sec
;
299 phdr
->ppn
.ts
.tv_nsec
= thdr
->tp_nsec
;
300 phdr
->ppn
.caplen
= thdr
->tp_snaplen
;
301 phdr
->ppn
.len
= thdr
->tp_len
;
305 phdr
->ppn
.ts
.tv_sec
= ___constant_swab32(thdr
->tp_sec
);
306 phdr
->ppn
.ts
.tv_nsec
= ___constant_swab32(thdr
->tp_nsec
);
307 phdr
->ppn
.caplen
= ___constant_swab32(thdr
->tp_snaplen
);
308 phdr
->ppn
.len
= ___constant_swab32(thdr
->tp_len
);
312 phdr
->ppk
.ts
.tv_sec
= thdr
->tp_sec
;
313 phdr
->ppk
.ts
.tv_usec
= thdr
->tp_nsec
/ 1000;
314 phdr
->ppk
.caplen
= thdr
->tp_snaplen
;
315 phdr
->ppk
.len
= thdr
->tp_len
;
316 phdr
->ppk
.ifindex
= sll
->sll_ifindex
;
317 phdr
->ppk
.protocol
= sll
->sll_protocol
;
318 phdr
->ppk
.pkttype
= sll
->sll_pkttype
;
321 case KUZNETZOV_SWAPPED
:
322 phdr
->ppk
.ts
.tv_sec
= ___constant_swab32(thdr
->tp_sec
);
323 phdr
->ppk
.ts
.tv_usec
= ___constant_swab32(thdr
->tp_nsec
/ 1000);
324 phdr
->ppk
.caplen
= ___constant_swab32(thdr
->tp_snaplen
);
325 phdr
->ppk
.len
= ___constant_swab32(thdr
->tp_len
);
326 phdr
->ppk
.ifindex
= ___constant_swab32(sll
->sll_ifindex
);
327 phdr
->ppk
.protocol
= ___constant_swab16(sll
->sll_protocol
);
328 phdr
->ppk
.pkttype
= sll
->sll_pkttype
;
332 phdr
->ppb
.ts
.tv_sec
= thdr
->tp_sec
;
333 phdr
->ppb
.ts
.tv_nsec
= thdr
->tp_nsec
;
334 phdr
->ppb
.caplen
= thdr
->tp_snaplen
;
335 phdr
->ppb
.len
= thdr
->tp_len
;
336 phdr
->ppb
.tsource
= tp_to_pcap_tsource(thdr
->tp_status
);
337 phdr
->ppb
.ifindex
= (u16
) sll
->sll_ifindex
;
338 phdr
->ppb
.protocol
= sll
->sll_protocol
;
339 phdr
->ppb
.hatype
= sll
->sll_hatype
;
340 phdr
->ppb
.pkttype
= sll
->sll_pkttype
;
343 case BORKMANN_SWAPPED
:
344 phdr
->ppb
.ts
.tv_sec
= ___constant_swab32(thdr
->tp_sec
);
345 phdr
->ppb
.ts
.tv_nsec
= ___constant_swab32(thdr
->tp_nsec
);
346 phdr
->ppb
.caplen
= ___constant_swab32(thdr
->tp_snaplen
);
347 phdr
->ppb
.len
= ___constant_swab32(thdr
->tp_len
);
348 phdr
->ppb
.tsource
= ___constant_swab16(tp_to_pcap_tsource(thdr
->tp_status
));
349 phdr
->ppb
.ifindex
= ___constant_swab16((u16
) sll
->sll_ifindex
);
350 phdr
->ppb
.protocol
= ___constant_swab16(sll
->sll_protocol
);
351 phdr
->ppb
.hatype
= sll
->sll_hatype
;
352 phdr
->ppb
.pkttype
= sll
->sll_pkttype
;
360 static inline void pcap_pkthdr_to_tpacket_hdr(pcap_pkthdr_t
*phdr
,
362 struct tpacket2_hdr
*thdr
,
363 struct sockaddr_ll
*sll
)
367 thdr
->tp_sec
= phdr
->ppo
.ts
.tv_sec
;
368 thdr
->tp_nsec
= phdr
->ppo
.ts
.tv_usec
* 1000;
369 thdr
->tp_snaplen
= phdr
->ppo
.caplen
;
370 thdr
->tp_len
= phdr
->ppo
.len
;
373 case DEFAULT_SWAPPED
:
374 thdr
->tp_sec
= ___constant_swab32(phdr
->ppo
.ts
.tv_sec
);
375 thdr
->tp_nsec
= ___constant_swab32(phdr
->ppo
.ts
.tv_usec
) * 1000;
376 thdr
->tp_snaplen
= ___constant_swab32(phdr
->ppo
.caplen
);
377 thdr
->tp_len
= ___constant_swab32(phdr
->ppo
.len
);
381 thdr
->tp_sec
= phdr
->ppn
.ts
.tv_sec
;
382 thdr
->tp_nsec
= phdr
->ppn
.ts
.tv_nsec
;
383 thdr
->tp_snaplen
= phdr
->ppn
.caplen
;
384 thdr
->tp_len
= phdr
->ppn
.len
;
388 thdr
->tp_sec
= ___constant_swab32(phdr
->ppn
.ts
.tv_sec
);
389 thdr
->tp_nsec
= ___constant_swab32(phdr
->ppn
.ts
.tv_nsec
);
390 thdr
->tp_snaplen
= ___constant_swab32(phdr
->ppn
.caplen
);
391 thdr
->tp_len
= ___constant_swab32(phdr
->ppn
.len
);
395 thdr
->tp_sec
= phdr
->ppk
.ts
.tv_sec
;
396 thdr
->tp_nsec
= phdr
->ppk
.ts
.tv_usec
* 1000;
397 thdr
->tp_snaplen
= phdr
->ppk
.caplen
;
398 thdr
->tp_len
= phdr
->ppk
.len
;
401 case KUZNETZOV_SWAPPED
:
402 thdr
->tp_sec
= ___constant_swab32(phdr
->ppk
.ts
.tv_sec
);
403 thdr
->tp_nsec
= ___constant_swab32(phdr
->ppk
.ts
.tv_usec
) * 1000;
404 thdr
->tp_snaplen
= ___constant_swab32(phdr
->ppk
.caplen
);
405 thdr
->tp_len
= ___constant_swab32(phdr
->ppk
.len
);
409 thdr
->tp_sec
= phdr
->ppb
.ts
.tv_sec
;
410 thdr
->tp_nsec
= phdr
->ppb
.ts
.tv_nsec
;
411 thdr
->tp_snaplen
= phdr
->ppb
.caplen
;
412 thdr
->tp_len
= phdr
->ppb
.len
;
415 case BORKMANN_SWAPPED
:
416 thdr
->tp_sec
= ___constant_swab32(phdr
->ppb
.ts
.tv_sec
);
417 thdr
->tp_nsec
= ___constant_swab32(phdr
->ppb
.ts
.tv_nsec
);
418 thdr
->tp_snaplen
= ___constant_swab32(phdr
->ppb
.caplen
);
419 thdr
->tp_len
= ___constant_swab32(phdr
->ppb
.len
);
427 #define FEATURE_UNKNOWN (0 << 0)
428 #define FEATURE_TIMEVAL_MS (1 << 0)
429 #define FEATURE_TIMEVAL_NS (1 << 1)
430 #define FEATURE_LEN (1 << 2)
431 #define FEATURE_CAPLEN (1 << 3)
432 #define FEATURE_IFINDEX (1 << 4)
433 #define FEATURE_PROTO (1 << 5)
434 #define FEATURE_HATYPE (1 << 6)
435 #define FEATURE_PKTTYPE (1 << 7)
436 #define FEATURE_TSOURCE (1 << 8)
438 struct pcap_magic_type
{
439 const uint32_t magic
;
441 const uint16_t features
;
444 static const struct pcap_magic_type pcap_magic_types
[] __maybe_unused
= {
446 .magic
= ORIGINAL_TCPDUMP_MAGIC
,
447 .desc
= "tcpdump-capable pcap",
448 .features
= FEATURE_TIMEVAL_MS
|
452 .magic
= NSEC_TCPDUMP_MAGIC
,
453 .desc
= "tcpdump-capable pcap with ns resolution",
454 .features
= FEATURE_TIMEVAL_NS
|
458 .magic
= KUZNETZOV_TCPDUMP_MAGIC
,
459 .desc
= "Alexey Kuznetzov's pcap",
460 .features
= FEATURE_TIMEVAL_MS
|
467 .magic
= BORKMANN_TCPDUMP_MAGIC
,
468 .desc
= "netsniff-ng pcap",
469 .features
= FEATURE_TIMEVAL_NS
|
480 static inline void pcap_dump_type_features(void)
484 for (i
= 0; i
< array_size(pcap_magic_types
); ++i
) {
485 printf("%s:\n", pcap_magic_types
[i
].desc
);
486 printf(" magic: 0x%x (swapped: 0x%x)\n",
487 pcap_magic_types
[i
].magic
,
488 ___constant_swab32(pcap_magic_types
[i
].magic
));
489 printf(" features:\n");
491 if (pcap_magic_types
[i
].features
== FEATURE_UNKNOWN
) {
492 printf(" unknown\n");
496 if (pcap_magic_types
[i
].features
& FEATURE_TIMEVAL_MS
)
497 printf(" timeval in us\n");
498 if (pcap_magic_types
[i
].features
& FEATURE_TIMEVAL_NS
)
499 printf(" timeval in ns\n");
500 if (pcap_magic_types
[i
].features
& FEATURE_TSOURCE
)
501 printf(" timestamp source\n");
502 if (pcap_magic_types
[i
].features
& FEATURE_LEN
)
503 printf(" packet length\n");
504 if (pcap_magic_types
[i
].features
& FEATURE_CAPLEN
)
505 printf(" packet cap-length\n");
506 if (pcap_magic_types
[i
].features
& FEATURE_IFINDEX
)
507 printf(" packet ifindex\n");
508 if (pcap_magic_types
[i
].features
& FEATURE_PROTO
)
509 printf(" packet protocol\n");
510 if (pcap_magic_types
[i
].features
& FEATURE_HATYPE
)
511 printf(" hardware type\n");
512 if (pcap_magic_types
[i
].features
& FEATURE_PKTTYPE
)
513 printf(" packet type\n");
517 static const char *pcap_ops_group_to_str
[] __maybe_unused
= {
518 [PCAP_OPS_RW
] = "rw",
519 [PCAP_OPS_SG
] = "sg",
520 [PCAP_OPS_MM
] = "mm",
523 static const struct pcap_file_ops
*pcap_ops
[] __maybe_unused
= {
524 [PCAP_OPS_RW
] = &pcap_rw_ops
,
525 [PCAP_OPS_SG
] = &pcap_sg_ops
,
526 [PCAP_OPS_MM
] = &pcap_mm_ops
,
529 static inline void pcap_prepare_header(struct pcap_filehdr
*hdr
, uint32_t magic
,
530 uint32_t linktype
, int32_t thiszone
,
533 bool swapped
= pcap_magic_is_swapped(magic
);
536 hdr
->version_major
= swapped
? ___constant_swab16(PCAP_VERSION_MAJOR
) : PCAP_VERSION_MAJOR
;
537 hdr
->version_minor
= swapped
? ___constant_swab16(PCAP_VERSION_MINOR
) : PCAP_VERSION_MINOR
;
538 hdr
->thiszone
= swapped
? ___constant_swab32(thiszone
) : thiszone
;
540 hdr
->snaplen
= swapped
? ___constant_swab32(snaplen
) : snaplen
;
541 hdr
->linktype
= swapped
? ___constant_swab32(linktype
) : linktype
;
544 static inline void pcap_validate_header(const struct pcap_filehdr
*hdr
)
546 pcap_check_magic(hdr
->magic
);
548 switch (hdr
->linktype
) {
549 case LINKTYPE_EN10MB
:
550 case LINKTYPE_IEEE802_11
:
551 case ___constant_swab32(LINKTYPE_EN10MB
):
552 case ___constant_swab32(LINKTYPE_IEEE802_11
):
555 panic("This file has not a valid pcap header\n");
558 if (unlikely(hdr
->version_major
!= PCAP_VERSION_MAJOR
) &&
559 ___constant_swab16(hdr
->version_major
) != PCAP_VERSION_MAJOR
)
560 panic("This file has not a valid pcap header\n");
561 if (unlikely(hdr
->version_minor
!= PCAP_VERSION_MINOR
) &&
562 ___constant_swab16(hdr
->version_minor
) != PCAP_VERSION_MINOR
)
563 panic("This file has not a valid pcap header\n");
566 static int pcap_generic_pull_fhdr(int fd
, uint32_t *magic
,
567 uint32_t *linktype
) __maybe_unused
;
569 static int pcap_generic_pull_fhdr(int fd
, uint32_t *magic
, uint32_t *linktype
)
572 struct pcap_filehdr hdr
;
574 ret
= read(fd
, &hdr
, sizeof(hdr
));
575 if (unlikely(ret
!= sizeof(hdr
)))
578 pcap_validate_header(&hdr
);
581 *linktype
= hdr
.linktype
;
586 static int pcap_generic_push_fhdr(int fd
, uint32_t magic
,
587 uint32_t linktype
) __maybe_unused
;
589 static int pcap_generic_push_fhdr(int fd
, uint32_t magic
, uint32_t linktype
)
592 struct pcap_filehdr hdr
;
594 memset(&hdr
, 0, sizeof(hdr
));
596 pcap_prepare_header(&hdr
, magic
, linktype
, 0, PCAP_DEFAULT_SNAPSHOT_LEN
);
598 ret
= write_or_die(fd
, &hdr
, sizeof(hdr
));
599 if (unlikely(ret
!= sizeof(hdr
)))
600 panic("Failed to write pkt file header!\n");
605 #endif /* PCAP_IO_H */