From 0f765f49ac231d99f493f00c0f0986fbeff2615d Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Fri, 31 May 2013 11:46:02 +0200 Subject: ring: move duplicate/generic code parts from rx/tx into ring.c We do not want to maintain duplicate code, so move this into a separate file and name those *_generic() helpers. Signed-off-by: Daniel Borkmann --- ring.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 ring.c (limited to 'ring.c') diff --git a/ring.c b/ring.c new file mode 100644 index 0000000..2899df3 --- /dev/null +++ b/ring.c @@ -0,0 +1,64 @@ +/* + * netsniff-ng - the packet sniffing beast + * Copyright 2009, 2010 Daniel Borkmann. + * Subject to the GPL, version 2. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "xmalloc.h" +#include "die.h" +#include "ring.h" +#include "built_in.h" + +void mmap_ring_generic(int sock, struct ring *ring) +{ + ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0); + if (ring->mm_space == MAP_FAILED) + panic("Cannot mmap {TX,RX}_RING!\n"); +} + +void alloc_ring_frames_generic(struct ring *ring) +{ + int i; + size_t len = ring->layout.tp_frame_nr * sizeof(*ring->frames); + + ring->frames = xmalloc_aligned(len, CO_CACHE_LINE_SIZE); + fmemset(ring->frames, 0, len); + + for (i = 0; i < ring->layout.tp_frame_nr; ++i) { + ring->frames[i].iov_len = ring->layout.tp_frame_size; + ring->frames[i].iov_base = ring->mm_space + + (i * ring->layout.tp_frame_size); + } +} + +void bind_ring_generic(int sock, struct ring *ring, int ifindex) +{ + int ret; + /* The {TX,RX}_RING registers itself to the networking stack with + * dev_add_pack(), so we have one single RX_RING for all devs + * otherwise you'll get the packet twice. + */ + fmemset(&ring->s_ll, 0, sizeof(ring->s_ll)); + + ring->s_ll.sll_family = AF_PACKET; + ring->s_ll.sll_protocol = htons(ETH_P_ALL); + ring->s_ll.sll_ifindex = ifindex; + ring->s_ll.sll_hatype = 0; + ring->s_ll.sll_halen = 0; + ring->s_ll.sll_pkttype = 0; + + ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll)); + if (ret < 0) + panic("Cannot bind {TX,RX}_RING!\n"); +} -- cgit v1.2.3-54-g00ecf