-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbgp.c
More file actions
262 lines (226 loc) · 8.46 KB
/
Copy pathbgp.c
File metadata and controls
262 lines (226 loc) · 8.46 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// +build ignore
#include "../headers/vmlinux.h"
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <string.h>
#pragma pack(1)
#include "bgp.h"
char __license[] SEC("license") = "GPL";
#define TC_ACT_OK 0
#define ETH_P_IP 0x0800 /* Internet Protocol packet */
#define ETH_HLEN 14 /*Ethernet Header Length */
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, struct nlri);
__type(value, struct nlri);
} nlri_replace SEC(".maps");
// static inline int bpf_strcmplength(char *s1, char *s2, u32 n);
// static inline int bpf_strncmpoffset(char *s1, char *s2, u32 n, u32 o);
// static __always_inline bool skb_revalidate_data(struct __sk_buff *skb,
// void **head, void **tail,
// const u32 offset) {
// if (*head + offset > *tail) {
// if (bpf_skb_pull_data(skb, offset) < 0) {
// return false;
// }
// *head = (uint8_t *)(long)skb->data;
// *tail = (uint8_t *)(long)skb->data_end;
// if (*head + offset > *tail) {
// return false;
// }
// }
// return true;
// }
static inline int read_bgp(struct __sk_buff *skb) {
void *data_end = (void *)(long)skb->data_end;
void *data = (void *)(long)skb->data;
struct ethhdr *eth = data;
__u16 h_proto;
__u64 nh_off = 0;
nh_off = sizeof(*eth);
if (data + nh_off > data_end) {
return TC_ACT_OK;
}
h_proto = eth->h_proto;
if (h_proto == bpf_htons(ETH_P_IP)) {
struct iphdr *iph = data + nh_off;
if ((void *)(iph + 1) > data_end) {
return 0;
}
if (iph->protocol != IPPROTO_TCP) {
return 0;
}
__u32 tcp_hlen = 0;
__u32 ip_hlen = 0;
__u32 poffset = 0;
__u32 plength = 0;
__u32 ip_total_length = bpf_ntohs(iph->tot_len);
ip_hlen = iph->ihl << 2;
if (ip_hlen < sizeof(*iph)) {
return 0;
}
struct tcphdr *tcph = data + nh_off + sizeof(*iph);
if ((void *)(tcph + 1) > data_end) {
return 0;
}
__u16 src_port = bpf_ntohs(tcph->source);
__u16 dst_port = bpf_ntohs(tcph->dest);
if (src_port == 179 || dst_port == 179) {
tcp_hlen = tcph->doff << 2;
poffset = ETH_HLEN + ip_hlen + tcp_hlen;
plength = ip_total_length - ip_hlen - tcp_hlen;
if (plength >= 19) { // Abitrary length, but this seems to be the minimum
// for a keep alive message
struct bgp_message bgpm;
int ret = bpf_skb_load_bytes(skb, poffset, &bgpm, sizeof(bgpm));
if (ret != 0) {
bpf_printk("error %d", ret);
return 0;
}
poffset += sizeof(bgpm); // remove header
struct bgp_open open_msg;
switch (bgpm.type) {
case BGP_OPEN:
// statements
bpf_skb_load_bytes(skb, poffset, &open_msg, sizeof(open_msg));
if (ret != 0) {
bpf_printk("error %d", ret);
return 0;
}
bpf_printk("BGP open message AS: %d, identifier: %pI4 ",
bpf_ntohs(open_msg.myAS), &open_msg.identifier);
// open_msg.myAS = bpf_htons(65002);
// ret = bpf_skb_store_bytes(skb,poffset, &open_msg, sizeof(open_msg),
// BPF_F_RECOMPUTE_CSUM); if (ret != 0) {
// bpf_printk("error %d",ret);
// return 0;
// }
break;
case BGP_UPDATE:
// statements
bpf_printk("BGP update length %d", bpf_ntohs(bgpm.length));
//__u16 remainingdata = bpf_ntohs(bgpm.length);
// remainingdata -= sizeof(bgpm); // remove header
__u16 withdrawnlen;
int ret = bpf_skb_load_bytes(skb, poffset, &withdrawnlen,
sizeof(withdrawnlen));
if (ret != 0) {
bpf_printk("error %d", ret);
return 0;
}
// remainingdata -= sizeof(withdrawnlen) + bpf_ntohs(withdrawnlen); //
// remove withdrawn routes
bpf_printk("BGP withdrawn routes %d", bpf_ntohs(withdrawnlen));
poffset += sizeof(withdrawnlen) + bpf_ntohs(withdrawnlen);
ret = bpf_skb_load_bytes(skb, poffset, &withdrawnlen,
sizeof(withdrawnlen));
if (ret != 0) {
bpf_printk("error %d", ret);
return 0;
}
bpf_printk(
"BGP path flags size %d, will attempt to parse the first three",
bpf_ntohs(withdrawnlen));
struct bgp_path_attributes bgp_path;
poffset += sizeof(withdrawnlen); // + bpf_ntohs(withdrawnlen);
__u32 pathOffset = poffset;
ret =
bpf_skb_load_bytes(skb, pathOffset, &bgp_path, sizeof(bgp_path));
if (ret != 0) {
bpf_printk("error %d", ret);
return 0;
}
bpf_printk("BGP Origin -> %d / options length %d", bgp_path.type,
bgp_path.len);
// Time for some hideous grossness, but I can't be bothered with loops
// in eBPF today
__u16 lencounter = withdrawnlen;
lencounter -= sizeof(bgp_path) +
bgp_path.len; // shrink the amount of data remaining
if (lencounter != 0) {
pathOffset += sizeof(bgp_path) + bgp_path.len;
ret = bpf_skb_load_bytes(skb, pathOffset, &bgp_path,
sizeof(bgp_path));
if (ret != 0) {
bpf_printk("error %d", ret);
return 0;
}
bpf_printk("BGP Origin -> %d / options length %d", bgp_path.type,
bgp_path.len);
// TODO: this is where we parse the incoming AS
struct bgp_path_as bgp_as;
pathOffset += sizeof(bgp_path);
ret = bpf_skb_load_bytes(skb, pathOffset, &bgp_as, sizeof(bgp_as));
if (ret != 0) {
bpf_printk("error %d", ret);
return 0;
}
bpf_printk("Found the AS %u %u %u", bgp_as.type, bgp_as.lenth,
bpf_ntohl(bgp_as.as));
// bgp_as.as = bpf_htonl(65002);
// ret = bpf_skb_store_bytes(skb, pathOffset, &bgp_as,
// sizeof(bgp_as), BPF_F_RECOMPUTE_CSUM); if (ret != 0) {
// bpf_printk("error %d",ret);
// return 0;
// }
lencounter -= sizeof(bgp_path) +
bgp_path.len; // shrink the amount of data remaining
if (lencounter != 0) {
pathOffset += bgp_path.len;
ret = bpf_skb_load_bytes(skb, pathOffset, &bgp_path,
sizeof(bgp_path));
if (ret != 0) {
bpf_printk("error %d", ret);
return 0;
}
bpf_printk("BGP Origin -> %d / options length %d", bgp_path.type,
bgp_path.len);
}
}
poffset += bpf_ntohs(withdrawnlen); // Skip all options
// bpf_printk("Remaining data %d", remainingdata);
struct nlri nlri;
ret = bpf_skb_load_bytes(skb, poffset, &nlri, sizeof(nlri));
if (ret != 0) {
bpf_printk("error %d", ret);
return 0;
}
bpf_printk("Found NLRI info -> %pI4 / %d", &nlri.prefix,
nlri.prefixlen);
nlri_value *found_nlri;
found_nlri = bpf_map_lookup_elem(&nlri_replace, &nlri);
if (found_nlri) {
// set_tcp_dport(skb, ETH_HLEN + sizeof(struct iphdr),
// bpf_htons(80), bpf_htons(8090));
bpf_printk("Replacing NLRI [%pI4] with [%pI4]", &nlri.prefix,
&found_nlri->prefix);
nlri.prefix = found_nlri->prefix;
ret = bpf_skb_store_bytes(skb, poffset, &nlri, sizeof(nlri),
BPF_F_RECOMPUTE_CSUM);
if (ret != 0) {
bpf_printk("error %d", ret);
return 0;
}
}
break;
case BGP_NOTIFICATION:
bpf_printk("BGP notification");
break;
case BGP_KEEPALIVE:
bpf_printk("BGP keep alive");
break;
default:
// default statements
break;
}
}
}
}
return 0;
}
SEC("tc_in")
int tc_ingress(struct __sk_buff *skb) { return read_bgp(skb); }
SEC("tc_egress")
int tc_egress_(struct __sk_buff *skb) { return read_bgp(skb); }