2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 #include <linux/if_ether.h>
22 void mmap_ring_generic(int sock
, struct ring
*ring
)
24 ring
->mm_space
= mmap(NULL
, ring
->mm_len
, PROT_READ
| PROT_WRITE
,
25 MAP_SHARED
| MAP_LOCKED
| MAP_POPULATE
, sock
, 0);
26 if (ring
->mm_space
== MAP_FAILED
)
27 panic("Cannot mmap {TX,RX}_RING!\n");
30 void alloc_ring_frames_generic(struct ring
*ring
, int num
, size_t size
)
33 size_t len
= num
* sizeof(*ring
->frames
);
35 ring
->frames
= xmalloc_aligned(len
, CO_CACHE_LINE_SIZE
);
36 fmemset(ring
->frames
, 0, len
);
38 for (i
= 0; i
< num
; ++i
) {
39 ring
->frames
[i
].iov_len
= size
;
40 ring
->frames
[i
].iov_base
= ring
->mm_space
+ (i
* size
);
44 void bind_ring_generic(int sock
, struct ring
*ring
, int ifindex
, bool tx_only
)
47 /* The {TX,RX}_RING registers itself to the networking stack with
48 * dev_add_pack(), so we have one single RX_RING for all devs
49 * otherwise you'll get the packet twice.
51 fmemset(&ring
->s_ll
, 0, sizeof(ring
->s_ll
));
53 ring
->s_ll
.sll_family
= AF_PACKET
;
55 ring
->s_ll
.sll_protocol
= 0;
57 ring
->s_ll
.sll_protocol
= htons(ETH_P_ALL
);
58 ring
->s_ll
.sll_ifindex
= ifindex
;
59 ring
->s_ll
.sll_hatype
= 0;
60 ring
->s_ll
.sll_halen
= 0;
61 ring
->s_ll
.sll_pkttype
= 0;
63 ret
= bind(sock
, (struct sockaddr
*) &ring
->s_ll
, sizeof(ring
->s_ll
));
65 panic("Cannot bind {TX,RX}_RING!\n");