forked from travelping/upg-vpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupf_adf.c
475 lines (391 loc) · 11.7 KB
/
upf_adf.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* Copyright (c) 2020 Travelping GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <inttypes.h>
#include <vppinfra/error.h>
#include <vppinfra/hash.h>
#include <vnet/vnet.h>
#include <vnet/ip/ip.h>
#include <vnet/ip/ip46_address.h>
#include <vnet/fib/ip4_fib.h>
#include <vnet/fib/ip6_fib.h>
#include <vnet/ethernet/ethernet.h>
#include <upf/upf.h>
#include <upf/upf_app_db.h>
#include <upf/upf_pfcp.h>
#include <upf/upf_proxy.h>
#if CLIB_DEBUG > 1
#define upf_debug clib_warning
#else
#define upf_debug(...) \
do { } while (0)
#endif
always_inline adr_result_t
upf_adr_try_tls (u16 port, u8 * p, u8 ** uri)
{
struct tls_record_hdr *hdr = (struct tls_record_hdr *) p;
struct tls_handshake_hdr *hsk = (struct tls_handshake_hdr *) (hdr + 1);
struct tls_client_hello_hdr *hlo =
(struct tls_client_hello_hdr *) (hsk + 1);
u8 *data = (u8 *) (hlo + 1);
word frgmt_len, hsk_len, len;
uword length = vec_len (p);
upf_debug ("Length: %d", length);
if (length < sizeof (*hdr))
return ADR_NEED_MORE_DATA;
upf_debug ("HDR: %u, v: %u.%u, Len: %d",
hdr->type, hdr->major, hdr->minor,
clib_net_to_host_u16 (hdr->length));
if (hdr->type != TLS_HANDSHAKE)
return ADR_FAIL;
if (hdr->major != 3 || hdr->minor < 1 || hdr->minor > 3)
/* TLS 1.0, 1.1 and 1.2 only (for now)
* SSLv2 backward-compatible hello is not supported
*/
return ADR_FAIL;
length -= sizeof (*hdr);
frgmt_len = clib_net_to_host_u16 (hdr->length);
if (length < frgmt_len)
/* TLS fragment is longer that IP payload */
return ADR_NEED_MORE_DATA;
hsk_len = hsk->length[0] << 16 | hsk->length[1] << 8 | hsk->length[2];
upf_debug ("TLS Hello: %u, v: Len: %d", hsk->type, hsk_len);
if (hsk_len + sizeof (*hsk) < frgmt_len)
/* Hello is longer that the current fragment */
return ADR_NEED_MORE_DATA;
if (hsk->type != TLS_CLIENT_HELLO)
return ADR_FAIL;
upf_debug ("TLS Client Hello: %u.%u", hlo->major, hlo->minor);
if (hlo->major != 3 || hlo->minor < 1 || hlo->minor > 3)
/* TLS 1.0, 1.1 and 1.2 only (for now) */
return ADR_FAIL;
len = hsk_len - sizeof (*hlo);
/* Session Id */
if (len < *data + 1)
return ADR_NEED_MORE_DATA;
len -= *data + 1;
data += *data + 1;
/* Cipher Suites */
if (len < clib_net_to_host_unaligned_mem_u16 ((u16 *) data) + 2)
return ADR_NEED_MORE_DATA;
len -= clib_net_to_host_unaligned_mem_u16 ((u16 *) data) + 2;
data += clib_net_to_host_unaligned_mem_u16 ((u16 *) data) + 2;
/* Compression Methods */
if (len < *data + 1)
return ADR_NEED_MORE_DATA;
len -= *data + 1;
data += *data + 1;
/* Extensions */
if (len < clib_net_to_host_unaligned_mem_u16 ((u16 *) data) + 2)
return ADR_NEED_MORE_DATA;
len = clib_net_to_host_unaligned_mem_u16 ((u16 *) data);
data += 2;
while (len > 4)
{
u16 ext_type, ext_len, sni_len, name_len;
ext_type = clib_net_to_host_unaligned_mem_u16 ((u16 *) data);
ext_len = clib_net_to_host_unaligned_mem_u16 ((u16 *) (data + 2));
upf_debug ("TLS Hello Extension: %u, %u", ext_type, ext_len);
if (ext_type != TLS_EXT_SNI)
goto skip_extension;
if (ext_len < 5 || ext_len + 4 > len)
{
upf_debug ("invalid extension len: %u (%u)", ext_len, len);
goto skip_extension;
}
sni_len = clib_net_to_host_unaligned_mem_u16 ((u16 *) (data + 4));
if (sni_len != ext_len - 2)
{
upf_debug ("invalid SNI extension len: %u != %u", sni_len,
ext_len - 2);
goto skip_extension;
}
if (*(data + 6) != 0)
{
upf_debug ("invalid SNI name type: %u", *(data + 6));
goto skip_extension;
}
name_len = clib_net_to_host_unaligned_mem_u16 ((u16 *) (data + 7));
if (name_len != sni_len - 3)
{
upf_debug ("invalid server name len: %u != %u", name_len,
sni_len - 3);
goto skip_extension;
}
vec_add (*uri, "https://", strlen ("https://"));
vec_add (*uri, data + 9, name_len);
if (port != 443)
*uri = format (*uri, ":%u", port);
vec_add1 (*uri, '/');
return ADR_OK;
skip_extension:
len -= ext_len + 4;
data += ext_len + 4;
}
return ADR_FAIL;
}
always_inline adr_result_t
upf_adr_try_http (u16 port, u8 * p, u8 ** uri)
{
word len = vec_len (p);
word uri_len;
u8 *eol;
u8 *s;
int r;
if ((r = is_http_request (&p, &len)) != ADR_OK)
return r;
upf_debug ("p: %*s", len, p);
eol = memchr (p, '\n', len);
upf_debug ("eol %p", eol);
if (!eol)
/* not EOL found */
return ADR_NEED_MORE_DATA;
s = memchr (p, ' ', eol - p);
upf_debug ("s: %p", s);
if (!s)
/* HTTP/0.9 - can find the Host Header */
return ADR_FAIL;
uri_len = s - p;
{
u64 d0 = *(u64 *) (s + 1);
upf_debug ("d0: 0x%016x, 1.0: 0x%016x, 1.1: 0x%016x", d0,
char_to_u64 ('H', 'T', 'T', 'P', '/', '1', '.', '0'),
char_to_u64 ('H', 'T', 'T', 'P', '/', '1', '.', '1'));
if (d0 != char_to_u64 ('H', 'T', 'T', 'P', '/', '1', '.', '0') &&
d0 != char_to_u64 ('H', 'T', 'T', 'P', '/', '1', '.', '1'))
/* not HTTP 1.0 or 1.1 compatible */
return ADR_FAIL;
}
s = eol + 1;
len -= (eol - p) + 1;
while (len > 0)
{
u64 d0 = *(u64 *) s;
uword ll;
eol = memchr (s, '\n', len);
if (!eol)
return ADR_NEED_MORE_DATA;
upf_debug ("l: %*s", eol - s, s);
ll = eol - s;
if (ll == 0 || (ll == 1 && s[0] == '\r'))
/* end of headers */
return ADR_FAIL;
/* upper case 1st 4 characters of header */
if ((d0 & char_to_u64 (0xdf, 0xdf, 0xdf, 0xdf, 0xff, 0, 0, 0))
== char_to_u64 ('H', 'O', 'S', 'T', ':', 0, 0, 0))
{
s += 5;
/* find first non OWS */
for (; s < eol && *s <= ' '; s++)
;
/* find last non OWS */
for (; eol > s && *eol <= ' '; eol--)
;
if (eol == s)
/* there could be a non OWS at *s, but single letter host
* names are not possible, so ignore that
*/
return ADR_FAIL;
vec_add (*uri, "http://", strlen ("http://"));
vec_add (*uri, s, eol - s + 1);
if (port != 80)
*uri = format (*uri, ":%u", port);
vec_add (*uri, p, uri_len);
return ADR_OK;
}
s = eol + 1;
len -= ll + 1;
}
return ADR_NEED_MORE_DATA;
}
static upf_pdr_t *
app_scan_for_uri (u8 * uri, flow_entry_t * flow, struct rules *active,
flow_direction_t direction, upf_pdr_t * adr)
{
upf_pdr_t *pdr;
/*
* see 3GPP TS 23.214 Table 5.2.2-1 for valid ADR combinations
*/
vec_foreach (pdr, active->pdr)
{
/* all non ADR pdrs have already been scanned */
if (!(pdr->pdi.fields & F_PDI_APPLICATION_ID))
{
adf_debug ("skip PDR %u for no ADR\n", pdr->id);
continue;
}
/* only consider ADRs that have higher precedence than the best ACL */
if (adr && pdr->precedence > adr->precedence)
{
adf_debug ("skip PDR %u for lower precedence\n", pdr->id);
continue;
}
if (pdr->pdi.fields & F_PDI_UE_IP_ADDR)
{
const ip46_address_t *addr;
addr =
&flow->key.ip[direction ^ flow->is_reverse ^
!!(pdr->pdi.ue_addr.flags & IE_UE_IP_ADDRESS_SD)];
upf_debug ("Using %U as UE IP, S/D: %u",
format_ip46_address, addr, IP46_TYPE_ANY,
!!(pdr->pdi.ue_addr.flags & IE_UE_IP_ADDRESS_SD));
if (ip46_address_is_ip4 (addr))
{
if (!(pdr->pdi.ue_addr.flags & IE_UE_IP_ADDRESS_V4))
{
adf_debug ("skip PDR %u for no UE IPv4 address\n", pdr->id);
continue;
}
if (!ip4_address_is_equal (&pdr->pdi.ue_addr.ip4, &addr->ip4))
{
adf_debug
("skip PDR %u for UE IPv4 mismatch, S/D: %u, %U != %U\n",
pdr->id, !!(pdr->pdi.ue_addr.flags & IE_UE_IP_ADDRESS_SD),
format_ip4_address, &pdr->pdi.ue_addr.ip4,
format_ip46_address, addr, IP46_TYPE_ANY);
continue;
}
}
else
{
if (!(pdr->pdi.ue_addr.flags & IE_UE_IP_ADDRESS_V6))
{
adf_debug ("skip PDR %u for no UE IPv6 address\n", pdr->id);
continue;
}
if (!ip6_address_is_equal_masked
(&pdr->pdi.ue_addr.ip6, &addr->ip6, &ip6_main.fib_masks[64]))
{
adf_debug
("skip PDR %u for UE IPv6 mismatch, S/D: %u, %U != %U\n",
pdr->id, !!(pdr->pdi.ue_addr.flags & IE_UE_IP_ADDRESS_SD),
format_ip6_address, &pdr->pdi.ue_addr.ip6,
format_ip46_address, addr, IP46_TYPE_ANY);
continue;
}
}
}
if ((pdr->pdi.fields & F_PDI_LOCAL_F_TEID) &&
flow_teid (flow, direction) != pdr->pdi.teid.teid)
{
adf_debug ("skip PDR %u for TEID mismatch\n", pdr->id);
continue;
}
adf_debug ("Scanning PDR %u (%p), db_id %u\n", pdr->id, pdr,
pdr->pdi.adr.db_id);
if (upf_adf_lookup (pdr->pdi.adr.db_id, uri, vec_len (uri), NULL) == 0)
{
adf_debug ("Match!");
adr = pdr;
}
else
adf_debug ("No Match!");
}
return adr;
}
adr_result_t
upf_application_detection (vlib_main_t * vm, u8 * p,
flow_entry_t * flow, struct rules *active)
{
adr_result_t r;
upf_pdr_t *origin = 0, *reverse = 0;
u16 port;
u8 *uri = NULL;
/* this runs after the forward and reverse ACL rules have been established */
/* reverse and origin may not be there yet during flow reclassification */
if (flow_pdr_id (flow, FT_ORIGIN) != 0)
{
origin = pfcp_get_pdr_by_id (active, flow_pdr_id (flow, FT_ORIGIN));
}
if (flow_pdr_id (flow, FT_REVERSE) != ~0)
{
reverse = pfcp_get_pdr_by_id (active, flow_pdr_id (flow, FT_REVERSE));
}
if (!origin && !reverse)
return ADR_FAIL;
adf_debug ("Old PDR Origin: %p %u, Reverse: %p %u\n",
origin, flow_pdr_id (flow, FT_ORIGIN),
reverse, flow_pdr_id (flow, FT_REVERSE));
if (flow->app_detection_done)
{
r = ADR_OK;
if (!flow->app_uri)
goto out;
uri = flow->app_uri;
}
else
{
ASSERT (p);
port =
clib_net_to_host_u16 (flow->key.port[FT_REVERSE ^ flow->is_reverse]);
upf_debug ("Using port %u, instead of %u", port,
clib_net_to_host_u16 (flow->
key.port[FT_ORIGIN ^ flow->
is_reverse]));
if (*p == TLS_HANDSHAKE)
r = upf_adr_try_tls (port, p, &uri);
else
r = upf_adr_try_http (port, p, &uri);
switch (r)
{
case ADR_NEED_MORE_DATA:
return r;
case ADR_FAIL:
goto out;
case ADR_OK:
break;
}
flow->app_uri = uri;
}
adf_debug ("URI: %v", uri);
origin = app_scan_for_uri (uri, flow, active, FT_ORIGIN, origin);
if (origin)
{
upf_far_t *far;
far = pfcp_get_far_by_id (active, origin->far_id);
flow->is_redirect = (far
&& far->
forward.flags & FAR_F_REDIRECT_INFORMATION);
}
reverse = flow->is_redirect ?
origin : app_scan_for_uri (uri, flow, active, FT_REVERSE, reverse);
out:
if (!origin)
return ADR_FAIL;
flow->app_detection_done = 1;
flow_pdr_id (flow, FT_ORIGIN) = origin->id;
if ((origin->pdi.fields & F_PDI_APPLICATION_ID))
flow->application_id = origin->pdi.adr.application_id;
/* we are done with scanning for PDRs */
if (reverse)
{
flow_pdr_id (flow, FT_REVERSE) = reverse->id;
flow_next (flow, FT_REVERSE) = FT_NEXT_PROXY;
}
else
flow_next (flow, FT_REVERSE) = FT_NEXT_CLASSIFY;
flow_next (flow, FT_ORIGIN) = FT_NEXT_PROXY;
adf_debug ("New PDR Origin: %p %u, Reverse: %p %u\n",
origin, flow_pdr_id (flow, FT_ORIGIN),
reverse, flow_pdr_id (flow, FT_REVERSE));
return ADR_OK;
}
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/