2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann.
5 * Subject to the GPL, version 2.
10 #include <sys/types.h>
22 #define DEFAULT_SLOTS 1000
24 #define PAGE_SIZE (getpagesize())
25 #define PAGE_MASK (~(PAGE_SIZE - 1))
26 #define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
28 static struct spinlock lock
;
29 static off_t map_size
= 0;
30 static char *pstart
, *pcurr
;
31 static int jumbo_frames
= 0;
33 static inline off_t
get_map_size(void)
35 int allocsz
= jumbo_frames
? 16 : 3;
36 return PAGE_ALIGN(sizeof(struct pcap_filehdr
) +
37 (PAGE_SIZE
* allocsz
) * DEFAULT_SLOTS
);
40 static int pcap_mmap_pull_file_header(int fd
, uint32_t *linktype
)
43 struct pcap_filehdr hdr
;
45 ret
= read(fd
, &hdr
, sizeof(hdr
));
46 if (unlikely(ret
!= sizeof(hdr
)))
49 pcap_validate_header(&hdr
);
51 *linktype
= hdr
.linktype
;
56 static int pcap_mmap_push_file_header(int fd
, uint32_t linktype
)
59 struct pcap_filehdr hdr
;
61 fmemset(&hdr
, 0, sizeof(hdr
));
62 pcap_prepare_header(&hdr
, linktype
, 0, PCAP_DEFAULT_SNAPSHOT_LEN
);
64 ret
= write_or_die(fd
, &hdr
, sizeof(hdr
));
65 if (unlikely(ret
!= sizeof(hdr
))) {
66 whine("Failed to write pkt file header!\n");
73 static int pcap_mmap_prepare_writing_pcap(int fd
)
83 map_size
= get_map_size();
87 panic("Cannot fstat pcap file!\n");
88 if (!S_ISREG (sb
.st_mode
))
89 panic("pcap dump file is not a regular file!\n");
91 pos
= lseek(fd
, map_size
, SEEK_SET
);
93 panic("Cannot lseek pcap file!\n");
95 ret
= write_or_die(fd
, "", 1);
97 panic("Cannot write file!\n");
99 pstart
= mmap(0, map_size
, PROT_WRITE
, MAP_SHARED
100 /*| MAP_HUGETLB*/, fd
, 0);
101 if (pstart
== MAP_FAILED
)
102 panic("mmap of file failed!");
104 ret
= madvise(pstart
, map_size
, MADV_SEQUENTIAL
);
106 panic("Failed to give kernel mmap advise!\n");
108 pcurr
= pstart
+ sizeof(struct pcap_filehdr
);
110 spinlock_unlock(&lock
);
115 static ssize_t
pcap_mmap_write_pcap_pkt(int fd
, struct pcap_pkthdr
*hdr
,
116 uint8_t *packet
, size_t len
)
121 spinlock_lock(&lock
);
123 if ((off_t
) (pcurr
- pstart
) + sizeof(*hdr
) + len
> map_size
) {
124 off_t map_size_old
= map_size
;
125 off_t offset
= (pcurr
- pstart
);
127 map_size
= PAGE_ALIGN(map_size_old
* 10 / 8);
129 pos
= lseek(fd
, map_size
, SEEK_SET
);
131 panic("Cannot lseek pcap file!\n");
133 ret
= write_or_die(fd
, "", 1);
135 panic("Cannot write file!\n");
137 pstart
= mremap(pstart
, map_size_old
, map_size
, MREMAP_MAYMOVE
);
138 if (pstart
== MAP_FAILED
)
139 panic("mmap of file failed!");
141 ret
= madvise(pstart
, map_size
, MADV_SEQUENTIAL
);
143 panic("Failed to give kernel mmap advise!\n");
145 pcurr
= pstart
+ offset
;
148 fmemcpy(pcurr
, hdr
, sizeof(*hdr
));
149 pcurr
+= sizeof(*hdr
);
151 fmemcpy(pcurr
, packet
, len
);
154 spinlock_unlock(&lock
);
156 return sizeof(*hdr
) + len
;
159 static int pcap_mmap_prepare_reading_pcap(int fd
)
166 spinlock_lock(&lock
);
168 ret
= fstat(fd
, &sb
);
170 panic("Cannot fstat pcap file!\n");
172 if (!S_ISREG (sb
.st_mode
))
173 panic("pcap dump file is not a regular file!\n");
175 map_size
= sb
.st_size
;
177 pstart
= mmap(0, map_size
, PROT_READ
, MAP_SHARED
| MAP_LOCKED
178 /*| MAP_HUGETLB*/, fd
, 0);
179 if (pstart
== MAP_FAILED
)
180 panic("mmap of file failed!");
182 ret
= madvise(pstart
, map_size
, MADV_SEQUENTIAL
);
184 panic("Failed to give kernel mmap advise!\n");
186 pcurr
= pstart
+ sizeof(struct pcap_filehdr
);
188 spinlock_unlock(&lock
);
193 static ssize_t
pcap_mmap_read_pcap_pkt(int fd
, struct pcap_pkthdr
*hdr
,
194 uint8_t *packet
, size_t len
)
197 spinlock_lock(&lock
);
199 if (unlikely((off_t
) (pcurr
+ sizeof(*hdr
) - pstart
) > map_size
)) {
200 spinlock_unlock(&lock
);
204 fmemcpy(hdr
, pcurr
, sizeof(*hdr
));
205 pcurr
+= sizeof(*hdr
);
207 if (unlikely((off_t
) (pcurr
+ hdr
->caplen
- pstart
) > map_size
)) {
212 if (unlikely(hdr
->caplen
== 0 || hdr
->caplen
> len
)) {
213 ret
= -EINVAL
; /* Bogus packet */
217 fmemcpy(packet
, pcurr
, hdr
->caplen
);
218 pcurr
+= hdr
->caplen
;
220 spinlock_unlock(&lock
);
222 return sizeof(*hdr
) + hdr
->caplen
;
225 spinlock_unlock(&lock
);
229 static void pcap_mmap_fsync_pcap(int fd
)
231 spinlock_lock(&lock
);
233 msync(pstart
, (off_t
) (pcurr
- pstart
), MS_ASYNC
);
235 spinlock_unlock(&lock
);
238 static void pcap_mmap_prepare_close_pcap(int fd
, enum pcap_mode mode
)
240 spinlock_lock(&lock
);
242 int ret
= munmap(pstart
, map_size
);
244 panic("Cannot unmap the pcap file!\n");
246 if (mode
== PCAP_MODE_WRITE
) {
247 ret
= ftruncate(fd
, (off_t
) (pcurr
- pstart
));
249 panic("Cannot truncate the pcap file!\n");
252 spinlock_unlock(&lock
);
255 struct pcap_file_ops pcap_mmap_ops __read_mostly
= {
257 .pull_file_header
= pcap_mmap_pull_file_header
,
258 .push_file_header
= pcap_mmap_push_file_header
,
259 .prepare_writing_pcap
= pcap_mmap_prepare_writing_pcap
,
260 .write_pcap_pkt
= pcap_mmap_write_pcap_pkt
,
261 .prepare_reading_pcap
= pcap_mmap_prepare_reading_pcap
,
262 .read_pcap_pkt
= pcap_mmap_read_pcap_pkt
,
263 .fsync_pcap
= pcap_mmap_fsync_pcap
,
264 .prepare_close_pcap
= pcap_mmap_prepare_close_pcap
,
267 int init_pcap_mmap(int jumbo_support
)
269 spinlock_init(&lock
);
271 jumbo_frames
= jumbo_support
;
273 return pcap_ops_group_register(&pcap_mmap_ops
, PCAP_OPS_MMAP
);
276 void cleanup_pcap_mmap(void)
278 spinlock_destroy(&lock
);
280 pcap_ops_group_unregister(PCAP_OPS_MMAP
);