This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
socket.c
1832 lines (1503 loc) · 46.1 KB
/
socket.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2023 Cisco and/or its affiliates. All rights reserved.
*
* SPDX-License-Identifier: MIT OR GPL-2.0-only
*
* Licensed under the MIT license <LICENSE.MIT or https://opensource.org/licenses/MIT> or the GPLv2 license
* <LICENSE.GPL or https://opensource.org/license/gpl-2-0>, at your option. This file may not be copied,
* modified, or distributed except according to those terms.
*/
#define pr_fmt(fmt) "%s: " fmt, KBUILD_MODNAME
#include <linux/tcp.h>
#include <linux/version.h>
#include <linux/uaccess.h>
#include <net/protocol.h>
#include <net/tcp.h>
#include <net/transp_v6.h>
#include <net/tls.h>
#include <net/sock.h>
#include <net/ip.h>
#include <linux/inet.h>
#include <linux/sockptr.h>
#include "bearssl.h"
#include "commands.h"
#include "csr.h"
#include "device_driver.h"
#include "opa.h"
#include "proxywasm.h"
#include "rsa_tools.h"
#include "socket.h"
#include "tls.h"
#include "string.h"
#include "cert_tools.h"
#include "augmentation.h"
#include "json.h"
#include "sd.h"
#include "camblet.h"
#include "trace.h"
#include "picohttpparser.h"
#define CAMBLET_PROTOCOL "camblet"
#define CAMBLET_PASSTHROUGH_PROTOCOL CAMBLET_PROTOCOL "/passthrough"
const char *ALPNs[] = {
CAMBLET_PROTOCOL,
};
const char *ALPNs_passthrough[] = {
CAMBLET_PASSTHROUGH_PROTOCOL,
CAMBLET_PROTOCOL,
};
const size_t ALPNs_NUM = sizeof(ALPNs) / sizeof(ALPNs[0]);
const size_t ALPNs_passthrough_NUM = sizeof(ALPNs_passthrough) / sizeof(ALPNs_passthrough[0]);
extern bool ktls_available;
static struct proto camblet_prot;
static struct proto camblet_ktls_prot;
static struct proto camblet_v6_prot;
static struct proto camblet_v6_ktls_prot;
static br_rsa_private_key *rsa_priv;
static br_rsa_public_key *rsa_pub;
struct camblet_socket;
typedef struct camblet_socket camblet_socket;
typedef int(camblet_sendmsg_t)(camblet_socket *s, void *msg, size_t len);
typedef int(camblet_recvmsg_t)(camblet_socket *s, void *buf, size_t len, int flags);
struct camblet_socket
{
union
{
br_ssl_server_context *sc;
br_ssl_client_context *cc;
};
unsigned char iobuf[BR_SSL_BUFSIZE_BIDI];
br_sslio_context ioc;
br_x509_camblet_context xc;
br_x509_class validator;
br_rsa_private_key *rsa_priv;
br_rsa_public_key *rsa_pub;
csr_parameters *parameters;
x509_certificate *cert;
char *hostname;
proxywasm *p;
proxywasm_context *pc;
i64 direction;
char *alpn;
struct mutex lock;
buffer_t *read_buffer;
buffer_t *write_buffer;
struct sock *sock;
bool sock_closed;
opa_socket_context opa_socket_ctx;
int (*ktls_recvmsg)(struct sock *sock,
struct msghdr *msg,
size_t size,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0)
int noblock,
#endif
int flags,
int *addr_len);
int (*ktls_sendmsg)(struct sock *sock,
struct msghdr *msg,
size_t size);
void (*ktls_close)(struct sock *sk, long timeout);
camblet_sendmsg_t *sendmsg;
camblet_recvmsg_t *recvmsg;
tcp_connection_context *conn_ctx;
};
static int get_read_buffer_capacity(camblet_socket *s);
static br_ssl_engine_context *get_ssl_engine_context(camblet_socket *s)
{
return s->direction == ListenerDirectionInbound ? &s->sc->eng : &s->cc->eng;
}
static int ktls_sendmsg(camblet_socket *s, void *buf, size_t len)
{
#ifdef HTTP_DEBUG
const char *method, *path;
int pret, minor_version;
struct phr_header headers[16];
size_t buflen = 0, prevbuflen = 0, method_len, path_len, num_headers;
if (s->direction == OUTPUT)
{
/* parse the request */
num_headers = sizeof(headers) / sizeof(headers[0]);
pret = phr_parse_request(buf, len, &method, &method_len, &path, &path_len,
&minor_version, headers, &num_headers, prevbuflen);
if (pret > 0)
{
} /* successfully parsed the request */
else if (pret == -1)
return -1;
printk("request is %d bytes long\n", pret);
printk("method is %.*s\n", (int)method_len, method);
printk("path is %.*s\n", (int)path_len, path);
printk("HTTP version is 1.%d\n", minor_version);
printk("headers: [%ld]\n", num_headers);
int i;
for (i = 0; i != num_headers; ++i)
{
printk("%.*s: %.*s\n", (int)headers[i].name_len, headers[i].name,
(int)headers[i].value_len, headers[i].value);
}
}
#endif
struct msghdr hdr = {0};
struct kvec iov = {.iov_base = buf, .iov_len = len};
iov_iter_kvec(&hdr.msg_iter, WRITE, &iov, 1, len);
return s->ktls_sendmsg(s->sock, &hdr, len);
}
static int ktls_recvmsg(camblet_socket *s, void *buf, size_t size, int flags)
{
int buf_len = get_read_buffer_capacity(s);
struct msghdr hdr = {0};
struct kvec iov = {.iov_base = buf, .iov_len = buf_len};
int addr_len = 0;
iov_iter_kvec(&hdr.msg_iter, READ, &iov, 1, buf_len);
return s->ktls_recvmsg(s->sock, &hdr, size,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0)
0,
#endif
flags, &addr_len);
}
static int bearssl_sendmsg(camblet_socket *s, void *src, size_t len)
{
int err = br_sslio_write_all(&s->ioc, src, len);
if (err < 0)
{
const br_ssl_engine_context *ec = get_ssl_engine_context(s);
pr_err("br_sslio_write_all error # command[%s] br_last_err[%d]", current->comm, br_ssl_engine_last_error(ec));
return err;
}
err = br_sslio_flush(&s->ioc);
if (err < 0)
{
pr_err("br_sslio_flush error # command[%s] err[%d]", current->comm, err);
return err;
}
return len;
}
static int
br_sslio_read_with_flags(br_sslio_context *ctx, void *dst, size_t len, int flags)
{
unsigned char *buf;
size_t alen;
bool is_peek = flags & MSG_PEEK;
if (len == 0)
{
return 0;
}
if (br_sslio_run_until(ctx, BR_SSL_RECVAPP) < 0)
{
return -1;
}
buf = br_ssl_engine_recvapp_buf(ctx->engine, &alen);
if (alen > len)
{
alen = len;
}
memcpy(dst, buf, alen);
if (!is_peek)
br_ssl_engine_recvapp_ack(ctx->engine, alen);
return (int)alen;
}
static int bearssl_recvmsg(camblet_socket *s, void *dst, size_t len, int flags)
{
int ret = br_sslio_read_with_flags(&s->ioc, dst, len, flags);
if (ret < 0)
{
const br_ssl_engine_context *ec = get_ssl_engine_context(s);
int last_error = br_ssl_engine_last_error(ec);
if (last_error == 0)
return 0;
if (last_error == BR_ERR_IO && s->sock_closed)
return 0;
if (last_error == BR_ERR_IO)
return -EIO;
pr_err("br_sslio_read error # command[%s] err[%d]", current->comm, last_error);
}
return ret;
}
static int plain_sendmsg(camblet_socket *s, void *msg, size_t len)
{
struct msghdr hdr = {0};
struct kvec iov = {.iov_base = msg, .iov_len = len};
iov_iter_kvec(&hdr.msg_iter, WRITE, &iov, 1, len);
return tcp_sendmsg(s->sock, &hdr, len);
}
static int plain_recvmsg(camblet_socket *s, void *buf, size_t size, int flags)
{
struct msghdr hdr = {0};
struct kvec iov = {.iov_base = buf, .iov_len = size};
int addr_len = 0;
iov_iter_kvec(&hdr.msg_iter, READ, &iov, 1, size);
return tcp_recvmsg(s->sock, &hdr, size,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0)
0,
#endif
flags, &addr_len);
}
static int camblet_socket_read(camblet_socket *s, void *dst, size_t len, int flags)
{
return s->recvmsg(s, dst, len, flags);
}
static int camblet_socket_write(camblet_socket *s, void *src, size_t len)
{
return s->sendmsg(s, src, len);
}
static char *get_read_buffer(camblet_socket *s)
{
return s->read_buffer->data;
}
static char *get_read_buffer_for_read(camblet_socket *s, int len)
{
return buffer_access(s->read_buffer, len);
}
static int get_read_buffer_capacity(camblet_socket *s)
{
return s->read_buffer->capacity - s->read_buffer->size;
}
static int get_read_buffer_size(camblet_socket *s)
{
return s->read_buffer->size;
}
static void set_read_buffer_size(camblet_socket *s, int size)
{
s->read_buffer->size = size;
}
static char *get_write_buffer(camblet_socket *s)
{
return s->write_buffer->data;
}
static char *get_write_buffer_for_write(camblet_socket *s, int len)
{
return buffer_access(s->write_buffer, len);
}
static int get_write_buffer_size(camblet_socket *s)
{
return s->write_buffer->size;
}
static void set_write_buffer_size(camblet_socket *s, int size)
{
s->write_buffer->size = size;
}
static void camblet_socket_free(camblet_socket *s)
{
if (s)
{
pr_debug("free camblet socket # command[%s]", current->comm);
if (s->p)
{
proxywasm_lock(s->p, s->pc);
proxywasm_destroy_context(s->p);
proxywasm_unlock(s->p);
}
if (s->alpn && !s->ktls_sendmsg && !s->opa_socket_ctx.passthrough)
{
// This call runs the SSL closure protocol (sending a close_notify, receiving the response close_notify).
if (br_sslio_close(&s->ioc) != BR_ERR_OK)
{
const br_ssl_engine_context *ec = get_ssl_engine_context(s);
int err = br_ssl_engine_last_error(ec);
if (err != 0 && err != BR_ERR_IO)
pr_err("br_sslio_close error # command[%s] err[%d]", current->comm, err);
}
else
{
pr_debug("br_sslio SSL closed # command[%s]", current->comm);
}
}
if (s->direction == ListenerDirectionInbound)
{
kfree(s->sc);
}
else
{
kfree(s->cc);
}
if (s->hostname)
{
kfree(s->hostname);
}
br_x509_camblet_free(&s->xc);
opa_socket_context_free(s->opa_socket_ctx);
buffer_free(s->read_buffer);
buffer_free(s->write_buffer);
kfree(s->rsa_priv);
kfree(s->rsa_pub);
x509_certificate_put(s->cert);
kfree(s->parameters);
kfree(s->conn_ctx->peer_spiffe_id);
kfree(s->conn_ctx);
kfree(s);
}
}
int proxywasm_attach(proxywasm *p, camblet_socket *s, ListenerDirection direction, buffer_t *upstream_buffer, buffer_t *downstream_buffer)
{
wasm_vm_result res = proxywasm_create_context(p, upstream_buffer, downstream_buffer);
if (res.err)
{
pr_err("could not create proxywasm context # err[%s]", res.err);
return -1;
}
s->pc = proxywasm_get_context(p);
s->p = p;
s->direction = direction;
set_property_v(s->pc, "listener_direction", (char *)&s->direction, sizeof(s->direction));
proxywasm_set_context(p, s->pc);
res = proxy_on_new_connection(p);
if (res.err)
{
pr_err("error during proxy_on_new_connection # err[%s]", res.err);
return -1;
}
return 0;
}
static camblet_socket *camblet_new_server_socket(struct sock *sock, opa_socket_context opa_socket_ctx, tcp_connection_context *conn_ctx)
{
camblet_socket *s = kzalloc(sizeof(camblet_socket), GFP_KERNEL);
s->sc = kzalloc(sizeof(br_ssl_server_context), GFP_KERNEL);
s->rsa_priv = kzalloc(sizeof(br_rsa_private_key), GFP_KERNEL);
s->rsa_pub = kzalloc(sizeof(br_rsa_public_key), GFP_KERNEL);
s->parameters = kzalloc(sizeof(csr_parameters), GFP_KERNEL);
s->read_buffer = buffer_new(16 * 1024);
s->write_buffer = buffer_new(16 * 1024);
s->sock = sock;
s->opa_socket_ctx = opa_socket_ctx;
s->conn_ctx = conn_ctx;
s->direction = INPUT;
mutex_init(&s->lock);
proxywasm *p = this_cpu_proxywasm();
if (p)
{
proxywasm_lock(p, NULL);
int err = proxywasm_attach(p, s, ListenerDirectionInbound, s->write_buffer, s->read_buffer);
proxywasm_unlock(p);
if (err != 0)
{
camblet_socket_free(s);
return NULL;
}
}
return s;
}
static camblet_socket *camblet_new_client_socket(struct sock *sock, opa_socket_context opa_socket_ctx, tcp_connection_context *conn_ctx)
{
camblet_socket *s = kzalloc(sizeof(camblet_socket), GFP_KERNEL);
s->cc = kzalloc(sizeof(br_ssl_client_context), GFP_KERNEL);
s->rsa_priv = kzalloc(sizeof(br_rsa_private_key), GFP_KERNEL);
s->rsa_pub = kzalloc(sizeof(br_rsa_public_key), GFP_KERNEL);
s->parameters = kzalloc(sizeof(csr_parameters), GFP_KERNEL);
s->read_buffer = buffer_new(16 * 1024);
s->write_buffer = buffer_new(16 * 1024);
s->sock = sock;
s->opa_socket_ctx = opa_socket_ctx;
s->conn_ctx = conn_ctx;
s->direction = OUTPUT;
mutex_init(&s->lock);
proxywasm *p = this_cpu_proxywasm();
if (p)
{
proxywasm_lock(p, NULL);
int err = proxywasm_attach(p, s, ListenerDirectionOutbound, s->read_buffer, s->write_buffer);
proxywasm_unlock(p);
if (err != 0)
{
camblet_socket_free(s);
return NULL;
}
}
return s;
}
void dump_array(unsigned char array[], size_t len)
{
size_t u;
for (u = 0; u < len; u++)
{
pr_cont("%x, ", array[u]);
}
}
static int configure_ktls_sock(camblet_socket *s);
static bool camblet_socket_proxywasm_enabled(camblet_socket *s)
{
return s->pc != NULL;
}
static bool msghdr_contains_tls_handshake(struct msghdr *msg)
{
if (msg == NULL)
{
return false;
}
struct iov_iter *iter = &msg->msg_iter;
char first_3_bytes[3];
int ret = copy_from_iter(first_3_bytes, 3, iter);
if (ret < 3)
{
return false;
}
iov_iter_revert(iter, 3);
return is_tls_handshake(first_3_bytes);
}
static int ensure_tls_handshake(camblet_socket *s, struct msghdr *msg)
{
int ret = 0;
char *alpn = READ_ONCE(s->alpn);
const tcp_connection_context *conn_ctx = s->conn_ctx;
if (alpn != NULL)
{
return ret;
}
mutex_lock(&s->lock);
alpn = READ_ONCE(s->alpn);
if (alpn == NULL)
{
// if we are the client, we should check if the transport is already encrypted
if (s->direction == OUTPUT && (s->opa_socket_ctx.passthrough || msghdr_contains_tls_handshake(msg)))
{
trace_info(conn_ctx, "setting passthrough ALPN", 0);
br_ssl_engine_set_protocol_names(&s->sc->eng, ALPNs_passthrough, ALPNs_passthrough_NUM);
br_ssl_client_reset(s->cc, s->hostname, false);
}
ret = br_sslio_flush(&s->ioc);
if (ret == 0)
{
pr_debug("TLS handshake done # command[%s] sk[%p]", current->comm, s->sock);
trace_msg(conn_ctx, "TLS handshake done", 0);
}
else
{
const br_ssl_engine_context *ec = get_ssl_engine_context(s);
int last_err = br_ssl_engine_last_error(ec);
pr_err("TLS handshake error # command[%s] err[%d]", current->comm, last_err);
char last_err_str[8];
snprintf(last_err_str, 8, "%d", last_err);
trace_err(conn_ctx, "TLS handshake error", 2, "error_code", last_err_str);
goto bail;
}
alpn = br_ssl_engine_get_selected_protocol(&s->sc->eng);
if (alpn)
{
trace_debug(conn_ctx, "ALPN selected", 2, "alpn", alpn);
if (camblet_socket_proxywasm_enabled(s))
set_property_v(s->pc, "upstream.negotiated_protocol", alpn, strlen(alpn));
}
else
{
trace_debug(conn_ctx, "ALPN not selected", 0);
alpn = "no-camblet";
}
if (strcmp(alpn, CAMBLET_PASSTHROUGH_PROTOCOL) == 0)
{
trace_debug(conn_ctx, "passthrough enabled", 0);
s->sendmsg = plain_sendmsg;
s->recvmsg = plain_recvmsg;
}
else
{
ret = configure_ktls_sock(s);
if (ret != 0)
{
pr_err("configure_ktls_sock failed # command[%s] err[%d]", current->comm, ret);
trace_err(conn_ctx, "kTLS configuration failed", 0);
goto bail;
}
}
WRITE_ONCE(s->alpn, alpn);
}
bail:
mutex_unlock(&s->lock);
return ret;
}
// returns continue (0), pause (1) or error (-1)
static int camblet_socket_proxywasm_on_data(camblet_socket *s, int data_size, bool end_of_stream, bool send)
{
int action = Pause;
proxywasm_lock(s->p, s->pc);
wasm_vm_result result = (send ? s->direction == ListenerDirectionOutbound : s->direction != ListenerDirectionOutbound)
? proxy_on_downstream_data(s->p, data_size, end_of_stream)
: proxy_on_upstream_data(s->p, data_size, end_of_stream);
proxywasm_unlock(s->p);
if (result.err)
{
pr_err("proxy_on_upstream/downstream_data returned an error # err[%s]", result.err);
action = -1;
goto bail;
}
if (result.data->i32 == Continue || end_of_stream)
{
action = Continue;
}
bail:
return action;
}
int camblet_recvmsg(struct sock *sock,
struct msghdr *msg,
size_t size,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0)
int noblock,
#endif
int flags,
int *addr_len)
{
int ret, len;
camblet_socket *s = sock->sk_user_data;
ret = ensure_tls_handshake(s, msg);
if (ret != 0)
{
goto bail;
}
len = size;
if (len == 0)
{
ret = 0;
goto bail;
}
bool end_of_stream = false;
int action = Pause;
while (action != Continue)
{
ret = camblet_socket_read(s, get_read_buffer_for_read(s, len), len, flags);
if (ret < 0)
{
if (ret == -ERESTARTSYS)
ret = -EINTR;
goto bail;
}
else if (ret == 0)
{
end_of_stream = true;
}
set_read_buffer_size(s, get_read_buffer_size(s) + ret);
if (camblet_socket_proxywasm_enabled(s))
{
action = camblet_socket_proxywasm_on_data(s, ret, end_of_stream, false);
if (action < 0)
{
ret = -1;
goto bail;
}
}
else
{
action = Continue;
}
}
int read_buffer_size = get_read_buffer_size(s);
len = copy_to_iter(get_read_buffer(s), read_buffer_size, &msg->msg_iter);
if (len < read_buffer_size)
{
pr_warn("recvmsg copy_to_iter copied less than requested");
}
set_read_buffer_size(s, read_buffer_size - len);
ret = len;
bail:
return ret;
}
int camblet_sendmsg(struct sock *sock, struct msghdr *msg, size_t size)
{
int ret, len;
camblet_socket *s = sock->sk_user_data;
ret = ensure_tls_handshake(s, msg);
if (ret != 0)
{
goto bail;
}
len = copy_from_iter(get_write_buffer_for_write(s, size), size, &msg->msg_iter);
set_write_buffer_size(s, get_write_buffer_size(s) + len);
if (camblet_socket_proxywasm_enabled(s))
{
ret = camblet_socket_proxywasm_on_data(s, len, false, true);
if (ret < 0)
{
goto bail;
}
if (ret == Pause)
{
ret = len;
goto bail;
}
}
ret = camblet_socket_write(s, get_write_buffer(s), get_write_buffer_size(s));
if (ret < 0)
{
goto bail;
}
set_write_buffer_size(s, get_write_buffer_size(s) - ret);
ret = size;
bail:
return ret;
}
void camblet_close(struct sock *sk, long timeout)
{
void (*close)(struct sock *sk, long timeout) = tcp_close;
camblet_socket *s = READ_ONCE(sk->sk_user_data);
if (s)
{
if (s->ktls_close)
{
close = READ_ONCE(s->ktls_close);
}
pr_debug("free camblet socket # command[%s] sk[%p]", current->comm, sk);
camblet_socket_free(s);
WRITE_ONCE(sk->sk_user_data, NULL);
}
close(sk, timeout);
}
// analyze tls_main.c to find out what we need to implement: check build_protos()
void ensure_camblet_ktls_prot(struct sock *sock, struct proto *camblet_ktls_prot)
{
void (*close)(struct sock *sk, long timeout) = READ_ONCE(camblet_ktls_prot->close);
if (close == NULL)
{
close = sock->sk_prot->close;
int (*setsockopt)(struct sock *sk, int level,
int optname, sockptr_t optval,
unsigned int optlen);
setsockopt = sock->sk_prot->setsockopt;
int (*getsockopt)(struct sock *sk, int level,
int optname, char __user *optval,
int __user *option);
getsockopt = sock->sk_prot->getsockopt;
bool (*sock_is_readable)(struct sock *sk);
sock_is_readable = sock->sk_prot->sock_is_readable;
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 5, 0)
int (*sendpage)(struct sock *sk, struct page *page,
int offset, size_t size, int flags);
sendpage = sock->sk_prot->sendpage;
camblet_ktls_prot->sendpage = sendpage;
#endif
camblet_ktls_prot->setsockopt = setsockopt;
camblet_ktls_prot->getsockopt = getsockopt;
camblet_ktls_prot->sock_is_readable = sock_is_readable;
camblet_ktls_prot->recvmsg = camblet_recvmsg;
camblet_ktls_prot->sendmsg = camblet_sendmsg;
WRITE_ONCE(camblet_ktls_prot->close, camblet_close);
}
}
static int configure_ktls_sock(camblet_socket *s)
{
int ret;
br_ssl_engine_context *eng = get_ssl_engine_context(s);
br_ssl_session_parameters *params = &eng->session;
if (params->cipher_suite != BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 || !ktls_available)
{
if (!ktls_available)
pr_warn("configure kTLS error: kTLS is not available on this system # command[%s]", current->comm);
else
pr_warn("configure kTLS error: only ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 cipher suite is supported # requested_suite[%x]", params->cipher_suite);
s->sendmsg = bearssl_sendmsg;
s->recvmsg = bearssl_recvmsg;
return 0;
}
pr_debug("configure kTLS for output # command[%s] cipher_suite[%x] version[%x] iv[%.*s]", current->comm, params->cipher_suite, params->version, 12, eng->out.chapol.iv);
pr_debug("configure kTLS for input # command[%s] cipher_suite[%x] version[%x] iv[%.*s]", current->comm, params->cipher_suite, params->version, 12, eng->in.chapol.iv);
struct tls12_crypto_info_chacha20_poly1305 crypto_info_tx = {.info = {.version = TLS_1_2_VERSION,
.cipher_type = TLS_CIPHER_CHACHA20_POLY1305}};
memcpy(crypto_info_tx.iv, eng->out.chapol.iv, TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
memcpy(crypto_info_tx.key, eng->out.chapol.key, TLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE);
uint64_t outseq = m3_bswap64(eng->out.chapol.seq);
memcpy(crypto_info_tx.rec_seq, &outseq, TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
// memcpy(crypto_info.salt, eng->out.chapol.salt, TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE);
struct tls12_crypto_info_chacha20_poly1305 crypto_info_rx = {.info = {.version = TLS_1_2_VERSION,
.cipher_type = TLS_CIPHER_CHACHA20_POLY1305}};
memcpy(crypto_info_rx.iv, eng->in.chapol.iv, TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
memcpy(crypto_info_rx.key, eng->in.chapol.key, TLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE);
uint64_t inseq = m3_bswap64(eng->in.chapol.seq);
memcpy(crypto_info_rx.rec_seq, &inseq, TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
// memcpy(crypto_info.salt, eng->out.chapol.salt, TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE);
// We have to set the protocol to the original here because the kTLS proto gets created from the sockets original protocol,
// so if it contains the camblet protocol parts it will spread to places where kTLS is used but camblet is not.
s->sock->sk_prot = s->sock->sk_family == AF_INET6 ? &tcpv6_prot : &tcp_prot;
ret = s->sock->sk_prot->setsockopt(s->sock, SOL_TCP, TCP_ULP, KERNEL_SOCKPTR("tls"), sizeof("tls"));
if (ret != 0)
{
pr_err("could not set sockopt TCP_ULP # command[%s] err[%d]", current->comm, ret);
return ret;
}
ret = s->sock->sk_prot->setsockopt(s->sock, SOL_TLS, TLS_TX, KERNEL_SOCKPTR(&crypto_info_tx), sizeof(crypto_info_tx));
if (ret != 0)
{
pr_err("could not set sockopt TLS_TX # command[%s] err[%d]", current->comm, ret);
return ret;
}
ret = s->sock->sk_prot->setsockopt(s->sock, SOL_TLS, TLS_RX, KERNEL_SOCKPTR(&crypto_info_rx), sizeof(crypto_info_rx));
if (ret != 0)
{
pr_err("could not set sockopt TLS_RX # command[%s] err[%d]", current->comm, ret);
return ret;
}
// unsigned int yes = 1;
// ret = c->sock->sk_prot->setsockopt(c->sock, SOL_TLS, TLS_TX_ZEROCOPY_RO, KERNEL_SOCKPTR(&yes), sizeof(yes));
// if (ret != 0)
// {
// pr_err("could not set sockopt TLS_TX_ZEROCOPY_RO # command[%s] err[%d]", current->comm, ret);
// return ret;
// }
// We have to save the proto here because the setsockopt calls override the TCP protocol.
// later those methods set by ktls has to be used to read and write data, but first we
// need to put back our read and write methods.
s->ktls_recvmsg = s->sock->sk_prot->recvmsg;
s->ktls_sendmsg = s->sock->sk_prot->sendmsg;
s->ktls_close = s->sock->sk_prot->close;
struct proto *ktls_prot;
if (s->sock->sk_family == AF_INET)
{
ktls_prot = &camblet_ktls_prot;
}
else
{
ktls_prot = &camblet_v6_ktls_prot;
}
ensure_camblet_ktls_prot(s->sock, ktls_prot);
WRITE_ONCE(s->sock->sk_prot, ktls_prot);
s->sendmsg = ktls_sendmsg;
s->recvmsg = ktls_recvmsg;
trace_debug(s->conn_ctx, "kTLS configured", 0);
return 0;
}
bool sockptr_is_camblet(sockptr_t sp)
{
char value[4];
copy_from_sockptr(value, sp, 4);
return strncmp(value, "camblet", 4) == 0;
}
// Let's intercept this call to set the socket options for setting up a simple TLS connection.
//
// TODO:
// We should put this method back to the proto after kTLS is configured, because kTLS will override this method.
int camblet_setsockopt(struct sock *sk, int level,
int optname, sockptr_t optval,
unsigned int optlen)
{
if (level == SOL_TCP && optname == TCP_ULP)
{
// check if optval is "camblet"
if (optlen == sizeof("camblet") && sockptr_is_camblet(optval))
{
opa_socket_context opa_socket_ctx = {.allowed = true, .passthrough = false, .mtls = false};
tcp_connection_context conn_ctx = {.direction = OUTPUT};
camblet_socket *s = camblet_new_client_socket(sk, opa_socket_ctx, &conn_ctx);
sk->sk_user_data = s;
return 0;
}
}
else if (level == SOL_CAMBLET)
{
if (optname == CAMBLET_HOSTNAME)
{
camblet_socket *s = sk->sk_user_data;
if (!s)
{
pr_err("camblet_setsockopt error: sk_user_data is NULL # command[%s]", current->comm);
return -1;
}
s->hostname = kzalloc(optlen, GFP_KERNEL);
copy_from_sockptr(s->hostname, optval, optlen);
return 0;
}
}
return tcp_setsockopt(sk, level, optname, optval, optlen);
}
int camblet_getsockopt(struct sock *sk, int level,
int optname, char __user *uoptval,
int __user *uoptlen)
{
if (level != SOL_CAMBLET)
{
goto out;
}
pr_debug("getsockopt # level[%d] optname[%d]", level, optname);
int len;
sockptr_t optlen = USER_SOCKPTR(uoptlen);
sockptr_t optval = USER_SOCKPTR(uoptval);
if (copy_from_sockptr(&len, optlen, sizeof(int)))
return -EFAULT;
len = min_t(unsigned int, len, sizeof(int));
if (len < 0)