netsniff-ng: enable jumbo frames on default in tpacket_v3
[netsniff-ng.git] / ring.c
blobc5d2d99434bd16d19644d278cd3a29f4a66f16df
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/mman.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 #include <linux/if_ether.h>
17 #include "xmalloc.h"
18 #include "die.h"
19 #include "ring.h"
20 #include "built_in.h"
22 void mmap_ring_generic(int sock, struct ring *ring)
24 ring->mm_space = mmap(0, 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)
32 int i;
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)
46 int ret;
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;
54 ring->s_ll.sll_protocol = htons(ETH_P_ALL);
55 ring->s_ll.sll_ifindex = ifindex;
56 ring->s_ll.sll_hatype = 0;
57 ring->s_ll.sll_halen = 0;
58 ring->s_ll.sll_pkttype = 0;
60 ret = bind(sock, (struct sockaddr *) &ring->s_ll, sizeof(ring->s_ll));
61 if (ret < 0)
62 panic("Cannot bind {TX,RX}_RING!\n");