forked from axbryd/FlowBlaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkt-parser.c
203 lines (167 loc) · 7.13 KB
/
pkt-parser.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <rte_ether.h>
#include <netinet/in.h>
#include "xfsm.h"
#define RTE_LOGTYPE_PARSER RTE_LOGTYPE_USER1
#define GTP_PORT 2152
#define GTP_HEADER_SIZE 8
#define IPV4_ICMP_PROTO 1
const struct xfsm_hdr_fields xfsm_header_fields[NUM_HDR_FIELDS] = {
{SRC_MAC, "SRC_MAC" , 6},
{DST_MAC, "DST_MAC", 6},
{ETH_TYPE, "ETH_TYPE", 2},
{IPv4_SRC, "IPv4_SRC", 4},
{IPv4_DST, "IPv4_DST", 4},
{IPv4_PROTO, "IPv4_PROTO", 1},
{IPv4_TOS, "IPv4_TOS", 1},
{ICMP_CODE, "ICMP_CODE", 1},
{ICMP_TYPE, "ICMP_TYPE", 1},
{ICMP_ID, "ICMP_ID", 2},
{ICMP_SEQ, "ICMP_SEQ", 2},
{ICMP_PAYLOAD, "ICMP_PAYLOAD", 4},
{TCP_SPORT, "TCP_SPORT", 2},
{TCP_DPORT, "TCP_DPORT", 2},
{UDP_SPORT, "UDP_SPORT", 2},
{UDP_DPORT, "UDP_DPORT", 2},
{GTP_TEID, "GTP-U_TEID", 4},
{INNER_IPv4_SRC, "INNER_IPv4_SRC", 4},
{INNER_IPv4_DST, "INNER_IPv4_DST", 4},
{INNER_IPv4_PROTO, "INNER_IPv4_PROTO", 1},
{INNER_TCP_SPORT, "INNER_TCP_SPORT", 2},
{INNER_TCP_DPORT, "INNER_TCP_DPORT", 2},
{INNER_UDP_SPORT, "INNER_UDP_SPORT", 2},
{INNER_UDP_DPORT, "INNER_UDP_DPORT", 2},
{XFSM_IN_PORT, "XFSM_IN_PORT", 1},
{XFSM_METADATA0, "METADATA0", 4},
{XFSM_METADATA1, "METADATA1", 4},
{XFSM_TIMESTAMP, "XFSM_TIMESTAMP", 8},
};
static inline uint64_t
be64ptr2h(const uint8_t *byte) {
uint64_t value = 0;
for (int i = 0; i < 6; i++)
value |= (uint64_t) (byte[i] << (5 - i) * 8);
return value;
}
static inline uint32_t
be32ptr2h(const uint8_t *byte){
return byte[3] | byte[2] << 8 | byte[1] << 16 | byte[0] << 24;
}
static inline uint16_t
be16ptr2h(const uint8_t *byte){
return byte[1] | byte[0] << 8;
}
int parse_pkt_header(xfsm_table_t *xfsm, struct rte_mbuf *packet){
uint8_t *eth, *ipv4, *tp, *gtp_u, *in_ipv4, *in_tp;
uint64_t dport = 0;
if (packet == NULL)
return -1;
xfsm->pkt_fields[XFSM_IN_PORT].value = &xfsm->in_port;
if (packet->userdata != NULL){
xfsm->pkt_fields[XFSM_METADATA0].value = (uint8_t *) &packet->userdata;
xfsm->pkt_fields[XFSM_METADATA1].value = (uint8_t *) &packet->userdata + 4;
} else {
xfsm->pkt_fields[XFSM_METADATA0].value = NULL;
xfsm->pkt_fields[XFSM_METADATA1].value = NULL;
}
eth = rte_pktmbuf_mtod(packet, void *);
// Ethernet
xfsm->pkt_fields[SRC_MAC].value = ð[6];
xfsm->pkt_fields[DST_MAC].value = ð[0];
xfsm->pkt_fields[ETH_TYPE].value = ð[12];
// IPv4
ipv4 = eth + ETHER_HDR_LEN;
xfsm->pkt_fields[IPv4_DST].value = &ipv4[16];
xfsm->pkt_fields[IPv4_SRC].value = &ipv4[12];
xfsm->pkt_fields[IPv4_PROTO].value = &ipv4[9];
xfsm->pkt_fields[IPv4_TOS].value = &ipv4[1];
tp = ipv4 + 20;
if (ipv4[9] == IPPROTO_TCP){
// TODO: FLAGS
xfsm->pkt_fields[TCP_SPORT].value = &tp[0];
xfsm->pkt_fields[TCP_DPORT].value = &tp[2];
xfsm->pkt_fields[UDP_SPORT].value = NULL;
xfsm->pkt_fields[UDP_DPORT].value = NULL;
} else if (ipv4[9] == IPV4_ICMP_PROTO) {
xfsm->pkt_fields[ICMP_TYPE].value = &tp[0];
xfsm->pkt_fields[ICMP_CODE].value = &tp[1];
xfsm->pkt_fields[ICMP_ID].value = &tp[4];
xfsm->pkt_fields[ICMP_SEQ].value = &tp[6];
xfsm->pkt_fields[ICMP_PAYLOAD].value = &tp[8];
// set to null other transport protocols
xfsm->pkt_fields[UDP_SPORT].value = NULL;
xfsm->pkt_fields[UDP_DPORT].value = NULL;
xfsm->pkt_fields[TCP_SPORT].value = NULL;
xfsm->pkt_fields[TCP_DPORT].value = NULL;
} else if (ipv4[9] == IPPROTO_UDP) {
xfsm->pkt_fields[UDP_SPORT].value = &tp[0];
xfsm->pkt_fields[UDP_DPORT].value = &tp[2];
xfsm->pkt_fields[TCP_SPORT].value = NULL;
xfsm->pkt_fields[TCP_DPORT].value = NULL;
get_hdr_value(xfsm->pkt_fields[UDP_DPORT], &dport);
if (dport == GTP_PORT) {
gtp_u = &tp[8];
xfsm->pkt_fields[GTP_TEID].value = >p_u[4];
// Inner IPv4
in_ipv4 = gtp_u + GTP_HEADER_SIZE;
xfsm->pkt_fields[INNER_IPv4_DST].value = &ipv4[16];
xfsm->pkt_fields[INNER_IPv4_SRC].value = &ipv4[12];
xfsm->pkt_fields[INNER_IPv4_PROTO].value = &ipv4[9];
in_tp = in_ipv4 + 20;
if (in_ipv4[9] == IPPROTO_TCP){
// TODO: FLAGS
xfsm->pkt_fields[INNER_TCP_SPORT].value = &in_tp[0];
xfsm->pkt_fields[INNER_TCP_DPORT].value = &in_tp[2];
xfsm->pkt_fields[INNER_UDP_SPORT].value = NULL;
xfsm->pkt_fields[INNER_UDP_DPORT].value = NULL;
} else if (in_ipv4[9] == IPPROTO_UDP) {
xfsm->pkt_fields[INNER_UDP_SPORT].value = &in_tp[0];
xfsm->pkt_fields[INNER_UDP_DPORT].value = &in_tp[2];
xfsm->pkt_fields[INNER_TCP_SPORT].value = NULL;
xfsm->pkt_fields[INNER_TCP_DPORT].value = NULL;
}
} else {
xfsm->pkt_fields[GTP_TEID].value = NULL;
xfsm->pkt_fields[INNER_IPv4_DST].value = NULL;
xfsm->pkt_fields[INNER_IPv4_SRC].value = NULL;
xfsm->pkt_fields[INNER_IPv4_PROTO].value = NULL;
xfsm->pkt_fields[INNER_TCP_SPORT].value = NULL;
xfsm->pkt_fields[INNER_TCP_DPORT].value = NULL;
xfsm->pkt_fields[INNER_UDP_SPORT].value = NULL;
xfsm->pkt_fields[INNER_UDP_DPORT].value = NULL;
}
}
xfsm->pkt_fields[XFSM_TIMESTAMP].value = (uint8_t *) &packet->timestamp;
/*printf("Parsing the packet\n");
for (int i=0; i<NUM_HDR_FIELDS; i++) {
uint64_t val = 0;
get_hdr_value(xfsm->pkt_fields[i], &val);
printf("\t%s: 0x%"PRIX64"\n", xfsm->pkt_fields[i].field->name, val);
}*/
return ipv4[9];
}
int get_hdr_value(xfsm_fields_tlv_t field, uint64_t *value) {
if (field.value == NULL)
return -1;
uint8_t *byte = field.value;
switch (field.field->len) {
case 8:
if (field.field->type == XFSM_TIMESTAMP)
*value = *((uint64_t *) byte);
break;
case 6:
*value = be64ptr2h(byte);
break;
case 4:
*value = be32ptr2h(byte);
break;
case 2:
*value = be16ptr2h(byte);
break;
case 1:
*value = byte[0];
break;
default:
break;
}
return 0;
}