trafgen: Add checksum helpers for TCP/UDP over IPv6
[netsniff-ng.git] / ring.c
blobe42828e4a33f67033dadc96f6960e0404509e572
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(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)
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, bool tx_only)
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 if (tx_only)
55 ring->s_ll.sll_protocol = 0;
56 else
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));
64 if (ret < 0)
65 panic("Cannot bind {TX,RX}_RING!\n");