-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdp.c
378 lines (332 loc) · 11.8 KB
/
xdp.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (c) 2022 Lars-Christian Schulz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "common.h"
#include "constants.h"
#include "maps.h"
#include "headers.h"
#include "parser.h"
#include "path_processing.h"
#include "fib_lookup.h"
#include "rewrite.h"
#include "debug.h"
#ifdef ENABLE_HF_CHECK
#include "aes/aes.h"
#endif
#include "bpf/types.h"
#include "bpf/builtins.h"
#include "bpf/scion.h"
#include "bpf_helpers.h"
#include <linux/bpf.h>
#include <linux/types.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
char _license[] SEC("license") = "Dual MIT/GPL";
//////////////////////////
// BPF Global Functions //
//////////////////////////
/// \brief Update the port statistics with the current packet.
/// \param[in] ctx
/// \param[in] verdict Final verdict on the packet.
int record_verdict(struct xdp_md *ctx, enum verdict verdict)
{
if (!ctx) return XDP_ABORTED;
#ifdef XDP_DEBUG_PRINT
switch (verdict)
{
case VERDICT_ABORT:
printf("VERDICT: ABORT (XDP_ABORTED)\n");
break;
case VERDICT_DROP:
printf("VERDICT: DROP (XDP_DROP)\n");
break;
case VERDICT_PASS:
printf("VERDICT: PASS (XDP_PASS)\n");
break;
case VERDICT_TX:
printf("VERDICT: TX (XDP_TX)\n");
break;
case VERDICT_SCION_FORWARD:
printf("VERDICT: SCION_FORWARD (XDP_REDIRECT)\n");
break;
case VERDICT_PARSE_ERROR:
printf("VERDICT: PARSE_ERROR (XDP_DROP)\n");
break;
case VERDICT_NOT_SCION:
printf("VERDICT: NOT_SCION (XDP_PASS)\n");
break;
case VERDICT_NOT_IMPLEMENTED:
printf("VERDICT: NOT_IMPLEMENTED (XDP_PASS)\n");
break;
case VERDICT_NO_INTERFACE:
printf("VERDICT: NO_INTERFACE (XDP_DROP)\n");
break;
case VERDICT_UNDERLAY_MISMATCH:
printf("VERDICT: UNDERLAY_MISMATCH (XDP_PASS)\n");
break;
case VERDICT_ROUTER_ALERT:
printf("VERDICT: ROUTER_ALERT (XDP_PASS)\n");
break;
case VERDICT_FIB_LKUP_DROP:
printf("VERDICT: FIB_LKUP_DROP (XDP_DROP)\n");
break;
case VERDICT_FIB_LKUP_PASS:
printf("VERDICT: FIB_LKUP_PASSORT (XDP_PASS)\n");
break;
case VERDICT_INVALID_HF:
printf("VERDICT: INVALID_HF (XDP_DROP)\n");
break;
}
#endif
// We don't need atomic operations since we are using a percpu map.
u32 ingress_ifindex = ctx->ingress_ifindex;
struct port_stats *stats = bpf_map_lookup_elem(&port_stats_map, &ingress_ifindex);
unsigned int index = (verdict >> 3) & 0x0f;
if (stats && index < COUNTER_ENUM_COUNT)
{
stats->verdict_bytes[index] += (ctx->data_end - ctx->data);
stats->verdict_pkts[index] += 1;
}
return (verdict & 0x07);
}
#ifdef ENABLE_HF_CHECK
/// \brief Verify a SCION path hop field.
/// \param[in] input 16 byte HF calculation input block
/// \param[in] expected Expected MAC truncated to 48 bits
/// \return Nonzero if hop field is valid
int verify_hop_field(struct macinput *input, u64 expected)
{
if (!input) return false;
// Key lookup
u32 index = 0;
struct hop_key *key = bpf_map_lookup_elem(&mac_key_map, &index);
if (!key)
{
printf(" ERROR: No verification key\n");
return false; // can't verify hop field without a key
}
struct aes_cmac mac;
aes_cmac_16bytes((struct aes_block*)input, &key->key, &key->subkey, &mac);
u64 actual = *(u64*)mac.w & 0x0000ffffffffffff;
printf(" Computed MAC %012x (expected: %012x)\n", actual, expected);
return actual == expected;
}
#endif // ENABLE_HF_CHECK
/// \brief Main packet processing function, does almost everything except hop field verification
/// and setting up the packet redirection.
/// \return Negative value if the packet should be forwarded, nonnegative verdict
/// (VERDICT_ABORT, VERDICT_DROP, VERDICT_PASS) if the packet should be dropped/passed to userspace.
int process_packet(struct xdp_md* ctx, struct scratchpad *this)
{
if (!ctx || !this) return -1;
void *data = (void*)(long)ctx->data;
void *data_end = (void*)(long)ctx->data_end;
printf(" packet length = %d\n", data_end - data);
// Headers pointers must be kept on the stack
struct headers hdr = {};
this->last_hop = 0;
this->ip_residual = 0;
this->udp_residual = 0;
this->egress_ifindex = -1;
#ifdef ENABLE_HF_CHECK
this->verify_mac_mask = 0;
#endif
/////////////
// Parsing //
/////////////
printf(" === Parser ===\n");
data = parse_underlay(this, &hdr, data, data_end);
if (!data) return record_verdict(ctx, this->verdict);
data = parse_scion(this, &hdr, data, data_end);
if (!data) return record_verdict(ctx, this->verdict);
////////////////////////////////////
// Determine AS Ingress Interface //
////////////////////////////////////
printf(" === Ingress ===\n");
u32 as_ing_ifid = INTERNAL_IFACE;
u32 key = ctx->ingress_ifindex;
if (!bpf_map_lookup_elem(&int_iface_map, &key))
{
// This lookup is necessary, because there can be multiple logical interfaces using
// different UDP ports behind the same physical interface.
struct ingress_addr ingress = {
.ifindex = ctx->ingress_ifindex,
.port = this->udp.dst,
};
#ifdef ENABLE_IPV4
ingress.ipv4 = this->ip.v4.dst;
#endif
#ifdef ENABLE_IPV6
memcpy(ingress.ipv6, &this->ip.v6.dst, sizeof(ingress.ipv6));
#endif
u32 *ifid = bpf_map_lookup_elem(&ingress_map, &ingress);
if (!ifid) return record_verdict(ctx, VERDICT_NO_INTERFACE);
as_ing_ifid = *ifid;
// Make sure the packet entered through the same ingress interface as specified in the hop
// field.
u16 hf_ingress;
if (INF_GET_CONS(hdr.scion_path.inf))
hf_ingress = hdr.scion_path.hf->ingress;
else
hf_ingress = hdr.scion_path.hf->egress;
printf(" hf ingress ifid = %d\n", as_ing_ifid);
if (ntohs(hf_ingress) != as_ing_ifid)
return record_verdict(ctx, VERDICT_NO_INTERFACE);
}
printf(" ingress ifid = %d\n", as_ing_ifid);
///////////////////////////
// AS Ingress Processing //
///////////////////////////
if (as_ing_ifid != INTERNAL_IFACE)
{
// Perform ingress processing if the packet came from another AS
switch (this->path_type)
{
#ifdef ENABLE_SCION_PATH
case SC_PATH_TYPE_SCION:
if (!scion_as_ingress(this, &hdr, data_end))
return record_verdict(ctx, this->verdict);
break;
#endif
default:
break;
}
}
///////////////////////////////
// FIB Lookup and Forwarding //
///////////////////////////////
printf(" === Forwarding ===\n");
int lkup_res = -1;
init_fib_lookup(this, &hdr, ctx);
if (this->last_hop)
{
// Forward to the destination host in our AS
printf(" ## FIB lookup for destination host in local AS ##\n");
lkup_res = forward_to_endhost(this, &hdr, ctx);
}
else
{
// Determine AS egress interface
printf(" ## Determine AS egress interface ##\n");
struct infofield *inf = hdr.scion_path.inf;
if (this->path.scion.segment_switch)
{
++inf;
if ((void*)(inf + 1) > data_end) return false;
}
key = ntohs(INF_GET_CONS(inf)
? hdr.scion_path.hf->egress
: hdr.scion_path.hf->ingress);
struct fwd_info *fwd = bpf_map_lookup_elem(&egress_map, &key);
if (!fwd)
{
printf(" ERROR: Egress interface not found\n");
return record_verdict(ctx, VERDICT_DROP);
}
printf(" fwd_external = %d\n", fwd->fwd_external);
// Forwarding
printf(" ## FIB lookup ##\n");
if (fwd->fwd_external)
{
// Forward to next AS on path
printf(" Forward to next AS on path\n");
switch (this->path_type)
{
#ifdef ENABLE_SCION_PATH
case SC_PATH_TYPE_SCION:
if (!scion_as_egress(this, &hdr, as_ing_ifid, data_end))
return record_verdict(ctx, this->verdict);
break;
#endif
default:
break;
}
if (fwd->link.ip_family != this->ip.family)
return record_verdict(ctx, VERDICT_UNDERLAY_MISMATCH);
lkup_res = forward_scion_link(this, ctx, &fwd->link);
}
else
{
if (as_ing_ifid != INTERNAL_IFACE)
{
// Forward packet from another AS to another border router in our AS
printf(" Forward to sibling\n");
if (fwd->sibling.ip_family != this->ip.family)
return record_verdict(ctx, VERDICT_UNDERLAY_MISMATCH);
lkup_res = forward_internal(this, ctx, &fwd->sibling);
}
else
{
// Forward a SCION packet between other (border) routers in our AS
printf(" Forward between siblings\n");
lkup_res = forward_outer_ip(this, ctx);
}
}
}
printf(" egress interface = %d\n", lkup_res);
if (lkup_res == LKUP_RES_RETURN)
return record_verdict(ctx, this->verdict);
//////////////////////
// Packet Rewriting //
//////////////////////
printf(" === Rewrite ===\n");
rewrite(this, &hdr, data_end);
this->egress_ifindex = lkup_res;
return -1;
}
/// \brief Entry point of the XDP border router.
SEC("xdp")
int border_router(struct xdp_md* ctx)
{
printf("=== Packet ingress at port %d ===\n", ctx->ingress_ifindex);
u32 key = 0;
struct scratchpad *this = bpf_map_lookup_elem(&scratchpad_map, &key);
if (!this) return XDP_ABORTED;
int verdict = process_packet(ctx, this);
if (verdict > 0) return verdict;
#ifdef ENABLE_HF_CHECK
//////////////////////
// MAC Verification //
//////////////////////
printf(" === MAC Verification ===\n");
if (this->verify_mac_mask & 0x01)
{
printf(" Verify first HF\n");
if(!verify_hop_field(&this->macinput[0], this->mac[0]))
return record_verdict(ctx, VERDICT_INVALID_HF);
}
if (this->verify_mac_mask & 0x02)
{
printf(" Verify second HF\n");
if(!verify_hop_field(&this->macinput[1], this->mac[1]))
return record_verdict(ctx, VERDICT_INVALID_HF);
}
#endif
////////////
// Output //
////////////
printf("Redirect to port %d\n", this->egress_ifindex);
verdict = XDP_ABORTED;
if (bpf_redirect_map(&tx_port_map, this->egress_ifindex, XDP_ABORTED) == XDP_REDIRECT)
verdict = VERDICT_SCION_FORWARD;
else
printf("ERROR: Egress port not found\n");
return record_verdict(ctx, verdict);
}