build: misc: Mention the release name in the announcement
[netsniff-ng-new.git] / ring.c
blob4ef0f78f3d96875f9dd4020f4b1e6f3954c92763
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2014, 2015 Tobias Klauser
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <sys/mman.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <arpa/inet.h>
16 #include <linux/if_ether.h>
18 #include "xmalloc.h"
19 #include "die.h"
20 #include "ring.h"
21 #include "built_in.h"
23 void setup_ring_layout_generic(int sock, struct ring *ring, size_t size,
24 bool jumbo_support)
26 fmemset(&ring->layout, 0, sizeof(ring->layout));
28 ring->layout.tp_block_size = (jumbo_support ?
29 RUNTIME_PAGE_SIZE << 4 :
30 RUNTIME_PAGE_SIZE << 2);
32 ring->layout.tp_frame_size = (jumbo_support ?
33 TPACKET_ALIGNMENT << 12 :
34 TPACKET_ALIGNMENT << 7);
36 ring->layout.tp_block_nr = size / ring->layout.tp_block_size;
37 ring->layout.tp_frame_nr = size / ring->layout.tp_frame_size;
40 void mmap_ring_generic(int sock, struct ring *ring)
42 ring->mm_space = mmap(NULL, ring->mm_len, PROT_READ | PROT_WRITE,
43 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
44 if (ring->mm_space == MAP_FAILED)
45 panic("Cannot mmap {TX,RX}_RING!\n");
48 void alloc_ring_frames_generic(struct ring *ring, size_t num, size_t size)
50 size_t i, len = num * sizeof(*ring->frames);
52 ring->frames = xmalloc_aligned(len, CO_CACHE_LINE_SIZE);
53 fmemset(ring->frames, 0, len);
55 for (i = 0; i < num; ++i) {
56 ring->frames[i].iov_len = size;
57 ring->frames[i].iov_base = ring->mm_space + (i * size);
61 void bind_ring_generic(int sock, struct ring *ring, int ifindex, bool tx_only)
63 int ret;
65 /* The {TX,RX}_RING registers itself to the networking stack with
66 * dev_add_pack(), so we have one single RX_RING for all devs
67 * otherwise you'll get the packet twice.
69 fmemset(&ring->s_ll, 0, sizeof(ring->s_ll));
71 ring->s_ll.sll_family = AF_PACKET;
72 ring->s_ll.sll_ifindex = ifindex;
73 ring->s_ll.sll_protocol = tx_only ? 0 : htons(ETH_P_ALL);
75 ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll));
76 if (ret < 0)
77 panic("Cannot bind {TX,RX}_RING!\n");