1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/*
* netsniff-ng - the packet sniffing beast
* Subject to the GPL, version 2.
*/
#include <net/if_arp.h>
#include <linux/if_ether.h>
#include "die.h"
#include "built_in.h"
#include "trafgen_l2.h"
#include "trafgen_proto.h"
static struct proto_field eth_fields[] = {
{ .id = ETH_DST_ADDR, .len = 6, },
{ .id = ETH_SRC_ADDR, .len = 6, .offset = 6 },
{ .id = ETH_TYPE, .len = 2, .offset = 12 },
};
static uint16_t pid_to_eth(enum proto_id pid)
{
switch (pid) {
case PROTO_ARP:
return ETH_P_ARP;
case PROTO_IP4:
return ETH_P_IP;
case PROTO_IP6:
return ETH_P_IPV6;
case PROTO_MPLS:
return ETH_P_MPLS_UC;
case PROTO_VLAN:
return ETH_P_8021Q;
default:
bug();
}
}
static void eth_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
{
proto_field_set_default_be16(hdr, ETH_TYPE, pid_to_eth(pid));
}
static void eth_header_init(struct proto_hdr *hdr)
{
proto_header_fields_add(hdr, eth_fields, array_size(eth_fields));
proto_field_set_default_dev_mac(hdr, ETH_SRC_ADDR);
}
static struct proto_hdr eth_hdr = {
.id = PROTO_ETH,
.layer = PROTO_L2,
.header_init = eth_header_init,
.set_next_proto = eth_set_next_proto,
};
static struct proto_field vlan_fields[] = {
/* TPID overlaps with Ethernet header and points to ether type */
{ .id = VLAN_TPID, .len = 2, .offset = -2 },
{ .id = VLAN_TCI, .len = 2, .offset = 0 },
{ .id = VLAN_PCP, .len = 2, .offset = 0, .shift = 13, .mask = 0xe000 },
{ .id = VLAN_DEI, .len = 2, .offset = 0, .shift = 12, .mask = 0x1000 },
{ .id = VLAN_VID, .len = 2, .offset = 0, .shift = 0, .mask = 0xfff },
/* Original ether type is stored after VLAN header */
{ .id = VLAN_ETYPE, .len = 2, .offset = 2 },
};
static void vlan_header_init(struct proto_hdr *hdr)
{
struct proto_hdr *lower;
uint16_t lower_etype = 0;
lower = proto_lower_default_add(hdr, PROTO_ETH);
proto_header_fields_add(hdr, vlan_fields, array_size(vlan_fields));
if (lower->id == PROTO_ETH)
lower_etype = proto_field_get_u16(lower, ETH_TYPE);
else if (lower->id == PROTO_VLAN)
lower_etype = proto_field_get_u16(lower, VLAN_ETYPE);
proto_field_set_be16(hdr, VLAN_ETYPE, lower_etype);
proto_field_set_default_be16(hdr, VLAN_TPID, pid_to_eth(hdr->id));
}
static void vlan_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
{
if (pid != PROTO_VLAN)
proto_field_set_be16(hdr, VLAN_ETYPE, pid_to_eth(pid));
}
static struct proto_hdr vlan_hdr = {
.id = PROTO_VLAN,
.layer = PROTO_L2,
.header_init = vlan_header_init,
.set_next_proto = vlan_set_next_proto,
};
static struct proto_field arp_fields[] = {
{ .id = ARP_HTYPE, .len = 2 },
{ .id = ARP_PTYPE, .len = 2, .offset = 2 },
{ .id = ARP_HLEN, .len = 1, .offset = 4 },
{ .id = ARP_PLEN, .len = 1, .offset = 5 },
{ .id = ARP_OPER, .len = 2, .offset = 6 },
{ .id = ARP_SHA, .len = 6, .offset = 8 },
{ .id = ARP_SPA, .len = 4, .offset = 14 },
{ .id = ARP_THA, .len = 6, .offset = 18 },
{ .id = ARP_TPA, .len = 4, .offset = 24 },
};
static void arp_header_init(struct proto_hdr *hdr)
{
struct proto_hdr *lower;
lower = proto_lower_default_add(hdr, PROTO_ETH);
if (lower->id == PROTO_ETH) {
const uint8_t bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
proto_field_set_default_bytes(lower, ETH_DST_ADDR, bcast);
}
proto_header_fields_add(hdr, arp_fields, array_size(arp_fields));
/* Generate Announce request by default */
proto_field_set_default_be16(hdr, ARP_HTYPE, ARPHRD_ETHER);
proto_field_set_default_be16(hdr, ARP_PTYPE, ETH_P_IP);
proto_field_set_default_u8(hdr, ARP_HLEN, 6);
proto_field_set_default_u8(hdr, ARP_PLEN, 4);
proto_field_set_default_be16(hdr, ARP_OPER, ARPOP_REQUEST);
proto_field_set_default_dev_mac(hdr, ARP_SHA);
proto_field_set_default_dev_ipv4(hdr, ARP_SPA);
proto_field_set_default_dev_ipv4(hdr, ARP_TPA);
}
static struct proto_hdr arp_hdr = {
.id = PROTO_ARP,
.layer = PROTO_L2,
.header_init = arp_header_init,
};
static struct proto_field mpls_fields[] = {
{ .id = MPLS_LABEL, .len = 4, .shift = 12, .mask = 0xfffff000 },
{ .id = MPLS_TC, .len = 4, .shift = 9, .mask = 0xe00 },
{ .id = MPLS_LAST, .len = 4, .shift = 8, .mask = 0x100 },
{ .id = MPLS_TTL, .len = 4, .shift = 0, .mask = 0xff },
};
static void mpls_header_init(struct proto_hdr *hdr)
{
proto_lower_default_add(hdr, PROTO_ETH);
proto_header_fields_add(hdr, mpls_fields, array_size(mpls_fields));
proto_field_set_default_be32(hdr, MPLS_LAST, 1);
}
static void mpls_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
{
if (pid == PROTO_MPLS)
proto_field_set_default_be32(hdr, MPLS_LAST, 0);
}
static struct proto_hdr mpls_hdr = {
.id = PROTO_MPLS,
.layer = PROTO_L2,
.header_init = mpls_header_init,
.set_next_proto = mpls_set_next_proto,
};
void protos_l2_init(void)
{
proto_header_register(ð_hdr);
proto_header_register(&vlan_hdr);
proto_header_register(&arp_hdr);
proto_header_register(&mpls_hdr);
}
|