2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
12 * "I love the smell of 10GbE in the morning. Smells like ... victory."
13 * - W. Richard Stevens, "Secret Teachings of the UNIX Environment"
18 #include <linux/if_packet.h>
22 #include "dissector.h"
26 #define RING_SIZE_FALLBACK (1 << 26)
28 static char *packet_types
[]={
38 struct tpacket_hdr tp_h
__attribute__((aligned(TPACKET_ALIGNMENT
)));
39 struct sockaddr_ll s_ll
__attribute__((aligned(TPACKET_ALIGNMENT
)));
44 struct tpacket_req layout
;
45 struct sockaddr_ll s_ll
;
48 } __cacheline_aligned
;
50 static inline void next_slot(unsigned int *it
, struct ring
*ring
)
53 if (*it
>= ring
->layout
.tp_frame_nr
)
57 static inline void next_rnd_slot(unsigned int *it
, struct ring
*ring
)
59 *it
= mt_rand_int32() % ring
->layout
.tp_frame_nr
;
62 static inline unsigned int ring_size(char *ifname
, unsigned int size
)
68 * Device bitrate in bytes times two as ring size.
69 * Fallback => ~ 64,00 MB
70 * 10 MBit => ~ 2,38 MB
71 * 54 MBit => ~ 12,88 MB
72 * 100 MBit => ~ 23,84 MB
73 * 300 MBit => ~ 71,52 MB
74 * 1.000 MBit => ~ 238,42 MB
75 * 10.000 MBit => ~ 2.384.18 MB
77 size
= device_bitrate(ifname
);
78 size
= (size
* 1000000) / 8;
81 size
= RING_SIZE_FALLBACK
;
91 static inline void show_frame_hdr(struct frame_map
*hdr
, int mode
,
94 if (mode
== FNTTYPE_PRINT_NONE
)
97 case FNTTYPE_PRINT_PAAC
:
98 case FNTTYPE_PRINT_NOPA
:
99 case FNTTYPE_PRINT_HEX1
:
100 case FNTTYPE_PRINT_HEX2
:
101 case FNTTYPE_PRINT_NORM
:
103 if (rmode
== RING_MODE_INGRESS
) {
104 tprintf("%s %u %u %u.%06u\n",
105 packet_types
[hdr
->s_ll
.sll_pkttype
],
106 hdr
->s_ll
.sll_ifindex
, hdr
->tp_h
.tp_len
,
107 hdr
->tp_h
.tp_sec
, hdr
->tp_h
.tp_usec
);
109 tprintf("%u %u.%06u\n", hdr
->tp_h
.tp_len
,
110 hdr
->tp_h
.tp_sec
, hdr
->tp_h
.tp_usec
);
113 case FNTTYPE_PRINT_LESS
:
114 if (rmode
== RING_MODE_INGRESS
) {
116 packet_types
[hdr
->s_ll
.sll_pkttype
],
117 hdr
->s_ll
.sll_ifindex
, hdr
->tp_h
.tp_len
);
119 tprintf("%u ", hdr
->tp_h
.tp_len
);
125 static inline unsigned int ring_frame_size(struct ring
*ring
)
127 return ring
->layout
.tp_frame_size
;
130 static inline void tpacket_hdr_clone(struct tpacket_hdr
*thdr_d
,
131 struct tpacket_hdr
*thdr_s
)
133 thdr_d
->tp_sec
= thdr_s
->tp_sec
;
134 thdr_d
->tp_usec
= thdr_s
->tp_usec
;
135 thdr_d
->tp_snaplen
= thdr_s
->tp_snaplen
;
136 thdr_d
->tp_len
= thdr_s
->tp_len
;