-
Notifications
You must be signed in to change notification settings - Fork 0
/
libseg6.c
379 lines (305 loc) · 9.09 KB
/
libseg6.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
379
#ifndef LIBSEG6_H_
#define LIBSEG6_H_
#ifndef VMLINUX_H_
#define VMLINUX_H_
#include <linux/bpf.h>
#endif
#ifndef BPF_HELPERS_H_
#define BPF_HELPERS_H_
#include <bpf/bpf_helpers.h>
#endif
#include "headers/proto.h"
#include "headers/all.h"
#define TLV_ITERATIONS 16
static __always_inline struct ip6_srh_t *seg6_get_srh(struct __sk_buff *skb)
{
void *cursor, *data_end;
struct ip6_srh_t *srh;
struct ip6_t *ip;
__u8 *ipver;
data_end = (void *)(long)skb->data_end;
cursor = (void *)(long)skb->data;
ipver = (__u8 *)cursor;
if ((void *)ipver + sizeof(*ipver) > data_end)
return 0;
if ((*ipver >> 4) != 6)
return 0;
ip = cursor_advance(cursor, sizeof(*ip));
if ((void *)ip + sizeof(*ip) > data_end)
return 0;
if (ip->next_header != 43)
return 0;
srh = cursor_advance(cursor, sizeof(*srh));
if ((void *)srh + sizeof(*srh) > data_end)
return 0;
if (srh->type != 4)
return 0;
return srh;
}
static __always_inline int __update_tlv_pad(struct __sk_buff *skb, __u32 new_pad,
__u32 old_pad, __u32 pad_off)
{
int err;
if (new_pad != old_pad) {
err = bpf_lwt_seg6_adjust_srh(skb, pad_off,
(int) new_pad - (int) old_pad);
if (err)
return err;
}
if (new_pad > 0) {
char pad_tlv_buf[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0};
struct sr6_tlv_t *pad_tlv = (struct sr6_tlv_t *) pad_tlv_buf;
pad_tlv->type = SR6_TLV_PADDING;
pad_tlv->len = new_pad - 2;
err = bpf_lwt_seg6_store_bytes(skb, pad_off,
(void *)pad_tlv_buf, new_pad);
if (err)
return err;
}
return 0;
}
static __always_inline int __is_valid_tlv_boundary(struct __sk_buff *skb, struct ip6_srh_t *srh,
__u32 *tlv_off, __u32 *pad_size,
__u32 *pad_off)
{
__u32 srh_off, cur_off;
int offset_valid = 0;
int err;
srh_off = (char *)srh - (char *)(long)skb->data;
// cur_off = end of segments, start of possible TLVs
cur_off = srh_off + sizeof(*srh) +
sizeof(struct ip6_addr_t) * (srh->first_segment + 1);
*pad_off = 0;
// we can only go as far as ~10 TLVs due to the BPF max stack size
#pragma clang loop unroll(full)
for (int i = 0; i < 10; i++) {
struct sr6_tlv_t tlv;
if (cur_off == *tlv_off)
offset_valid = 1;
if (cur_off >= srh_off + ((srh->hdrlen + 1) << 3))
break;
err = bpf_skb_load_bytes(skb, cur_off, &tlv, sizeof(tlv));
if (err)
return err;
if (tlv.type == SR6_TLV_PADDING) {
*pad_size = tlv.len + sizeof(tlv);
*pad_off = cur_off;
if (*tlv_off == srh_off) {
*tlv_off = cur_off;
offset_valid = 1;
}
break;
} else if (tlv.type == SR6_TLV_HMAC) {
break;
}
cur_off += sizeof(tlv) + tlv.len;
} // we reached the padding or HMAC TLVs, or the end of the SRH
if (*pad_off == 0)
*pad_off = cur_off;
if (*tlv_off == -1)
*tlv_off = cur_off;
else if (!offset_valid)
return -1;
return 0;
}
static __always_inline int seg6_add_tlv(struct __sk_buff *skb, struct ip6_srh_t *srh, __u32 tlv_off,
struct sr6_tlv_t *itlv, __u8 tlv_size)
{
__u32 srh_off = (char *)srh - (char *)(long)skb->data;
__u8 len_remaining, new_pad;
__u32 pad_off = 0;
__u32 pad_size = 0;
__u32 partial_srh_len;
int err;
if (tlv_off != -1)
tlv_off += srh_off;
if (itlv->type == SR6_TLV_PADDING || itlv->type == SR6_TLV_HMAC) {
return -1;
}
err = __is_valid_tlv_boundary(skb, srh, &tlv_off, &pad_size, &pad_off);
if (err) {
return err;
}
err = bpf_lwt_seg6_adjust_srh(skb, tlv_off, sizeof(*itlv) + itlv->len);
if (err) {
return err;
}
err = bpf_lwt_seg6_store_bytes(skb, tlv_off, (void *)itlv, tlv_size);
if (err) {
return err;
}
// the following can't be moved inside update_tlv_pad because the
// bpf verifier has some issues with it
pad_off += sizeof(*itlv) + itlv->len;
partial_srh_len = pad_off - srh_off;
len_remaining = partial_srh_len % 8;
new_pad = 8 - len_remaining;
if (new_pad == 1) // cannot pad for 1 byte only
new_pad = 9;
else if (new_pad == 8)
new_pad = 0;
return __update_tlv_pad(skb, new_pad, pad_size, pad_off);
}
static __always_inline int seg6_delete_tlv(struct __sk_buff *skb, struct ip6_srh_t *srh,
__u32 tlv_off)
{
__u32 srh_off = (char *)srh - (char *)(long)skb->data;
__u8 len_remaining, new_pad;
__u32 partial_srh_len;
__u32 pad_off = 0;
__u32 pad_size = 0;
struct sr6_tlv_t tlv;
int err;
tlv_off += srh_off;
err = __is_valid_tlv_boundary(skb, srh, &tlv_off, &pad_size, &pad_off);
if (err)
return err;
err = bpf_skb_load_bytes(skb, tlv_off, &tlv, sizeof(tlv));
if (err)
return err;
err = bpf_lwt_seg6_adjust_srh(skb, tlv_off, -(sizeof(tlv) + tlv.len));
if (err)
return err;
pad_off -= sizeof(tlv) + tlv.len;
partial_srh_len = pad_off - srh_off;
len_remaining = partial_srh_len % 8;
new_pad = 8 - len_remaining;
if (new_pad == 1) // cannot pad for 1 byte only
new_pad = 9;
else if (new_pad == 8)
new_pad = 0;
return __update_tlv_pad(skb, new_pad, pad_size, pad_off);
}
static __always_inline int seg6_find_tlv(struct __sk_buff *skb, struct ip6_srh_t *srh, __u8 type,
__u8 len)
{
int srh_offset = (char *)srh - (char *)(long)skb->data;
// initial cursor = end of segments, start of possible TLVs
int cursor = srh_offset + sizeof(struct ip6_srh_t) +
((srh->first_segment + 1) << 4);
#pragma clang loop unroll(full)
for(int i=0; i < 10; i++) { // TODO limitation
if (cursor >= srh_offset + ((srh->hdrlen + 1) << 3))
return -1;
struct sr6_tlv_t tlv;
if (bpf_skb_load_bytes(skb, cursor, &tlv, sizeof(struct sr6_tlv_t)))
return -1;
//bpf_trace_printk("TLV type=%d len=%d found at offset %d\n", tlv.type, tlv.len, cursor);
if (tlv.type == type && tlv.len + sizeof(struct sr6_tlv_t) == len)
return cursor;
cursor += sizeof(tlv) + tlv.len;
}
return -1;
}
static __always_inline int seg6_delete_tlv2(struct __sk_buff *skb, struct ip6_srh_t *srh,
__u32 tlv_off)
{
__u32 srh_off = (char *)srh - (char *)(long)skb->data;
__u8 len_remaining, new_pad;
__u32 partial_srh_len;
__u32 pad_off = 0;
__u32 pad_size = 0;
struct sr6_tlv_t tlv;
int err;
//tlv_off += srh_off;
err = __is_valid_tlv_boundary(skb, srh, &tlv_off, &pad_size, &pad_off);
if (err)
return err;
err = bpf_skb_load_bytes(skb, tlv_off, &tlv, sizeof(tlv));
if (err)
return err;
err = bpf_lwt_seg6_adjust_srh(skb, tlv_off, -(sizeof(tlv) + tlv.len));
if (err)
return err;
pad_off -= sizeof(tlv) + tlv.len;
partial_srh_len = pad_off - srh_off;
len_remaining = partial_srh_len % 8;
new_pad = 8 - len_remaining;
if (new_pad == 1) // cannot pad for 1 byte only
new_pad = 9;
else if (new_pad == 8)
new_pad = 0;
return __update_tlv_pad(skb, new_pad, pad_size, pad_off);
}
static __always_inline int seg6_find_tlv2(struct __sk_buff *skb, struct ip6_srh_t *srh, __u8 *tlv_type, __u8 source_tlv_length, __u8 repair_tlv_length)
// TODO: replace all args by structure for typeX, lenX
{
int srh_offset = (char *)srh - (char *)(long)skb->data;
// initial cursor = end of segments, start of possible TLVs
int cursor = srh_offset + sizeof(struct ip6_srh_t) +
((srh->first_segment + 1) << 4);
#pragma clang loop unroll(full)
for(int i=0; i < TLV_ITERATIONS; i++) {
if (cursor >= srh_offset + ((srh->hdrlen + 1) << 3))
return -1;
struct sr6_tlv_t tlv;
if (bpf_skb_load_bytes(skb, cursor, &tlv, sizeof(struct sr6_tlv_t)))
return -1;
//bpf_trace_printk("TLV type=%d len=%d found at offset %d\n", tlv.type, tlv.len, cursor);
if ((tlv.type == 28 && tlv.len + sizeof(struct sr6_tlv_t) == source_tlv_length) ||
(tlv.type == 29 && tlv.len + sizeof(struct sr6_tlv_t) == repair_tlv_length)) {
*tlv_type = tlv.type;
return cursor;
}
cursor += sizeof(tlv) + tlv.len;
}
return -1;
}
/*
* Finds the address where the payload starts.
* For now, consider only TCP, UDP and SMTP as transport payload information.
* Either directly after the Segment Routing header, or after a series of IPv6 headers
*/
static __always_inline void *seg6_find_payload(struct __sk_buff *skb, struct ip6_srh_t *srh) {
void *data_end = (void *)(long)skb->data_end;
__u8 nexthdr = srh->nexthdr;
__u8 hdrlen = (srh->hdrlen + 1) << 3;
__u16 opt_len;
int err;
void *cursor = (void *)srh + hdrlen;
if (cursor > data_end) {
return 0;
}
if (nexthdr == 6 || nexthdr == 17 || nexthdr == 132) {
return cursor;
}
// First next header is not Transport => iterate
const int MAX_HEADERS = 10;
int i;
for (i = 0; i < MAX_HEADERS; ++i) {
// Cursor is at the next header already
if ((void *) cursor + 2 > data_end) {
return 0;
}
if ((void *)cursor + opt_len > data_end)
return 0;
opt_len = (1 + (__u16) *((__u8 *) cursor + 1)) << 3;
nexthdr = (__u16) *((__u8 *)cursor);
if (nexthdr == 6 || nexthdr == 17 || nexthdr == 132) {
cursor_advance(cursor, opt_len);
return cursor;
}
cursor_advance(cursor, opt_len);
}
return 0; // Not found
}
static inline struct ip6_t *seg6_get_ipv6(struct __sk_buff *skb)
{
void *cursor, *data_end;
struct ip6_srh_t *srh;
struct ip6_t *ip;
__u8 *ipver;
data_end = (void *)(long)skb->data_end;
cursor = (void *)(long)skb->data;
ipver = (__u8 *)cursor;
if ((void *)ipver + sizeof(*ipver) > data_end)
return 0;
if ((*ipver >> 4) != 6)
return 0;
ip = cursor_advance(cursor, sizeof(*ip));
if ((void *)ip + sizeof(*ip) > data_end)
return 0;
return ip;
}
#endif