2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
16 #include <linux/if_packet.h>
21 #define TCPDUMP_MAGIC 0xa1b2c3d4
22 #define PCAP_VERSION_MAJOR 2
23 #define PCAP_VERSION_MINOR 4
24 #define PCAP_DEFAULT_SNAPSHOT_LEN 65535
26 #define LINKTYPE_NULL 0 /* BSD loopback encapsulation */
27 #define LINKTYPE_EN10MB 1 /* Ethernet (10Mb) */
28 #define LINKTYPE_EN3MB 2 /* Experimental Ethernet (3Mb) */
29 #define LINKTYPE_AX25 3 /* Amateur Radio AX.25 */
30 #define LINKTYPE_PRONET 4 /* Proteon ProNET Token Ring */
31 #define LINKTYPE_CHAOS 5 /* Chaos */
32 #define LINKTYPE_IEEE802 6 /* 802.5 Token Ring */
33 #define LINKTYPE_ARCNET 7 /* ARCNET, with BSD-style header */
34 #define LINKTYPE_SLIP 8 /* Serial Line IP */
35 #define LINKTYPE_PPP 9 /* Point-to-point Protocol */
36 #define LINKTYPE_FDDI 10 /* FDDI */
40 uint16_t version_major
;
41 uint16_t version_minor
;
53 struct pcap_nsf_pkthdr
{
60 struct pcap_timeval ts
;
65 static inline void tpacket_hdr_to_pcap_pkthdr(struct tpacket_hdr
*thdr
,
66 struct pcap_pkthdr
*phdr
)
68 phdr
->ts
.tv_sec
= thdr
->tp_sec
;
69 phdr
->ts
.tv_usec
= thdr
->tp_usec
;
70 phdr
->caplen
= thdr
->tp_snaplen
;
71 phdr
->len
= thdr
->tp_len
;
74 static inline void pcap_pkthdr_to_tpacket_hdr(struct pcap_pkthdr
*phdr
,
75 struct tpacket_hdr
*thdr
)
77 thdr
->tp_sec
= phdr
->ts
.tv_sec
;
78 thdr
->tp_usec
= phdr
->ts
.tv_usec
;
79 thdr
->tp_snaplen
= phdr
->caplen
;
80 thdr
->tp_len
= phdr
->len
;
83 enum pcap_ops_groups
{
85 #define PCAP_OPS_RW PCAP_OPS_RW
87 #define PCAP_OPS_SG PCAP_OPS_SG
89 #define PCAP_OPS_MMAP PCAP_OPS_MMAP
92 #define PCAP_OPS_MAX (__PCAP_OPS_MAX - 1)
93 #define PCAP_OPS_SIZ (__PCAP_OPS_MAX)
100 struct pcap_file_ops
{
102 int (*pull_file_header
)(int fd
);
103 int (*push_file_header
)(int fd
);
104 int (*prepare_writing_pcap
)(int fd
);
105 ssize_t (*write_pcap_pkt
)(int fd
, struct pcap_pkthdr
*hdr
,
106 uint8_t *packet
, size_t len
);
107 void (*fsync_pcap
)(int fd
);
108 int (*prepare_reading_pcap
)(int fd
);
109 ssize_t (*read_pcap_pkt
)(int fd
, struct pcap_pkthdr
*hdr
,
110 uint8_t *packet
, size_t len
);
111 void (*prepare_close_pcap
)(int fd
, enum pcap_mode mode
);
114 extern struct pcap_file_ops
*pcap_ops
[PCAP_OPS_SIZ
];
116 extern int pcap_ops_group_register(struct pcap_file_ops
*ops
,
117 enum pcap_ops_groups group
);
118 extern void pcap_ops_group_unregister(enum pcap_ops_groups group
);
120 static inline struct pcap_file_ops
*
121 pcap_ops_group_get(enum pcap_ops_groups group
)
123 return pcap_ops
[group
];
126 static inline void pcap_prepare_header(struct pcap_filehdr
*hdr
,
128 int32_t thiszone
, uint32_t snaplen
)
130 hdr
->magic
= TCPDUMP_MAGIC
;
131 hdr
->version_major
= PCAP_VERSION_MAJOR
;
132 hdr
->version_minor
= PCAP_VERSION_MINOR
;
133 hdr
->thiszone
= thiszone
;
135 hdr
->snaplen
= snaplen
;
136 hdr
->linktype
= linktype
;
139 static inline int pcap_validate_header(struct pcap_filehdr
*hdr
)
141 if (unlikely(hdr
->magic
!= TCPDUMP_MAGIC
||
142 hdr
->version_major
!= PCAP_VERSION_MAJOR
||
143 hdr
->version_minor
!= PCAP_VERSION_MINOR
||
144 hdr
->linktype
!= LINKTYPE_EN10MB
)) {
145 /* don't panic, but only whine */
146 whine("This file has not a valid pcap header, continuing ..\n");
153 extern int init_pcap_mmap(int jumbo_support
);
154 extern int init_pcap_rw(int jumbo_support
);
155 extern int init_pcap_sg(int jumbo_support
);
157 extern void cleanup_pcap_mmap(void);
158 extern void cleanup_pcap_rw(void);
159 extern void cleanup_pcap_sg(void);
161 static inline int init_pcap(int jumbo_support
)
163 init_pcap_rw(jumbo_support
);
164 init_pcap_sg(jumbo_support
);
165 init_pcap_mmap(jumbo_support
);
170 static inline void cleanup_pcap(void)