forked from ptpt52/natflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
natflow_urllogger.c
2118 lines (1905 loc) · 61.3 KB
/
natflow_urllogger.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
/*
* Author: Chen Minqiang <ptpt52@gmail.com>
* Date : Tue, 22 Jun 2021 22:50:41 +0800
*/
#include <linux/ctype.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/seq_file.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/init.h>
#include <linux/ip.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/inetdevice.h>
#include <linux/skbuff.h>
#include <linux/socket.h>
#include <linux/string.h>
#include <linux/syscalls.h>
#include <linux/tcp.h>
#include <linux/uaccess.h>
#include <linux/unistd.h>
#include <linux/version.h>
#include <linux/mman.h>
#include <linux/spinlock.h>
#include <linux/rcupdate.h>
#include <linux/highmem.h>
#include <net/netfilter/nf_conntrack.h>
#include <linux/netfilter/ipset/ip_set.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <net/ip6_checksum.h>
#include "natflow_common.h"
#include "natflow_urllogger.h"
static int urllogger_major = 0;
static int urllogger_minor = 0;
static struct cdev urllogger_cdev;
const char *urllogger_dev_name = "urllogger_queue";
static struct class *urllogger_class;
static struct device *urllogger_dev;
static inline ssize_t urlinfo_copy_host_tolower(unsigned char *dst, unsigned char *src, ssize_t n)
{
ssize_t i = 0;
for (; i < n; i++) {
if (src[i] == '/')
break;
if (src[i] >= 'A' && src[i] <= 'Z')
dst[i] = src[i] - 'A' + 'a';
else
dst[i] = src[i];
}
memcpy(dst + i, src + i, n - i);
return i;
}
struct urlinfo {
struct list_head list;
#define URLINFO_NOW ((jiffies - INITIAL_JIFFIES) / HZ)
#define TIMESTAMP_FREQ 10
unsigned int timestamp;
union {
__be32 sip;
union nf_inet_addr sipv6;
};
union {
__be32 dip;
union nf_inet_addr dipv6;
};
__be16 sport;
__be16 dport;
unsigned char mac[ETH_ALEN];
#define URLINFO_HTTPS 0x01
#define URLINFO_IPV6 0x80
unsigned char flags;
#define NATFLOW_HTTP_NONE 0
#define NATFLOW_HTTP_GET 1
#define NATFLOW_HTTP_POST 2
#define NATFLOW_HTTP_HEAD 3
unsigned char http_method;
unsigned short hits;
unsigned short data_len;
unsigned short host_len;
unsigned char acl_idx;
#define URLINFO_ACL_ACTION_RECORD 0
#define URLINFO_ACL_ACTION_DROP 1
#define URLINFO_ACL_ACTION_RESET 2
#define URLINFO_ACL_ACTION_REDIRECT 3
unsigned char acl_action;
unsigned char data[0];
};
#define __URLINFO_ALIGN 64
static const char *NATFLOW_http_method[] = {
[NATFLOW_HTTP_NONE] = "NONE",
[NATFLOW_HTTP_GET] = "GET",
[NATFLOW_HTTP_POST] = "POST",
[NATFLOW_HTTP_HEAD] = "HEAD",
};
static inline void urlinfo_release(struct urlinfo *url)
{
kfree(url);
}
/*
tuple_type:
0: dir0-src dir0-dst
1: dir0-src dir1-src
2: dir1-dst dir1-src
*/
static unsigned int urllogger_store_tuple_type = 0;
static unsigned int urllogger_store_timestamp_freq = TIMESTAMP_FREQ;
static unsigned int urllogger_store_enable = 0;
static unsigned int urllogger_store_memsize_limit = 1024 * 1024 * 10;
static unsigned int urllogger_store_count_limit = 10000;
static unsigned int urllogger_store_memsize = 0;
static unsigned int urllogger_store_count = 0;
static LIST_HEAD(urllogger_store_list);
static DEFINE_SPINLOCK(urllogger_store_lock);
static void urllogger_store_record(struct urlinfo *url)
{
struct urlinfo *url_i;
struct list_head *pos;
spin_lock(&urllogger_store_lock);
list_for_each_prev(pos, &urllogger_store_list) {
url_i = list_entry(pos, struct urlinfo, list);
/* merge the duplicate url request in 10s */
if (uintmindiff(url_i->timestamp, url->timestamp) > urllogger_store_timestamp_freq)
break;
if (url_i->sip == url->sip && url_i->dip == url->dip && url_i->dport == url->dport &&
url_i->data_len == url->data_len && memcmp(url_i->data, url->data, url_i->data_len) == 0 &&
url_i->flags == url->flags &&
url_i->http_method == url->http_method) {
url_i->hits++;
spin_unlock(&urllogger_store_lock);
urlinfo_release(url);
return;
}
}
urllogger_store_memsize += ALIGN(sizeof(struct urlinfo) + url->data_len, __URLINFO_ALIGN);
urllogger_store_count++;
list_add_tail(&url->list, &urllogger_store_list);
while (urllogger_store_count > urllogger_store_count_limit || urllogger_store_memsize > urllogger_store_memsize_limit) {
pos = urllogger_store_list.next;
url_i = list_entry(pos, struct urlinfo, list);
urllogger_store_memsize -= ALIGN(sizeof(struct urlinfo) + url_i->data_len, __URLINFO_ALIGN);
urllogger_store_count--;
list_del(pos);
urlinfo_release(url_i);
}
spin_unlock(&urllogger_store_lock);
}
static void urllogger_store_clear(void)
{
struct urlinfo *url;
struct list_head *pos, *n;
spin_lock_bh(&urllogger_store_lock);
list_for_each_safe(pos, n, &urllogger_store_list) {
url = list_entry(pos, struct urlinfo, list);
urllogger_store_memsize -= ALIGN(sizeof(struct urlinfo) + url->data_len, __URLINFO_ALIGN);
urllogger_store_count--;
list_del(pos);
urlinfo_release(url);
}
spin_unlock_bh(&urllogger_store_lock);
}
static int hostacl_major = 0;
static int hostacl_minor = 0;
static struct cdev hostacl_cdev;
const char *hostacl_dev_name = "hostacl_ctl";
static struct class *hostacl_class;
static struct device *hostacl_dev;
struct acl_rule {
unsigned char *acl_buffer;
ssize_t acl_buffer_size;
ssize_t acl_buffer_len;
};
#define ACL_RULE_ALLOC_SIZE 256
#define ACL_RULE_MAX 32
static int acl_rule_max = 0;
static struct acl_rule acl_rule_node[ACL_RULE_MAX];
static void acl_rule_init(void)
{
int rule_id;
for (rule_id = 0; rule_id < ACL_RULE_MAX; rule_id++) {
acl_rule_node[rule_id].acl_buffer = NULL;
}
}
static void acl_rule_clear(void)
{
void *tmp;
int rule_id;
for (rule_id = 0; rule_id < ACL_RULE_MAX; rule_id++) {
tmp = acl_rule_node[rule_id].acl_buffer;
if (tmp != NULL) {
acl_rule_node[rule_id].acl_buffer = NULL;
synchronize_rcu();
kfree(tmp);
}
}
acl_rule_max = 0;
}
/* return: 0 = no matched, 1 = matched */
static int urllogger_acl(struct urlinfo *url, int rule_id)
{
int ret = 0;
unsigned char backup_c;
unsigned char *acl_buffer;
acl_buffer = acl_rule_node[rule_id].acl_buffer;
backup_c = url->data[url->host_len];
url->data[url->host_len] = 0;
if (url->host_len >= 1 && acl_buffer != NULL) { /* at least a.b pattern */
int i = 0;
unsigned char b;
unsigned char *ptr = NULL;
while (ptr == NULL) {
ptr = strstr(acl_buffer, url->data + i);
while (ptr != NULL) {
b = *(ptr - 1);
if (((ptr[url->host_len - i] & 0x80) != 0 || ptr[url->host_len - i] == 0) && (b & 0x80) != 0) {
//found
url->acl_idx = (b & 0x1f);
ret = ((b & 0x60) >> 5);
url->acl_action = ret;
ret = 1;
goto __done;
}
if (ptr[url->host_len - i] == 0) {
ptr = NULL;
break;
}
ptr = strstr(ptr + url->host_len - i, url->data + i);
}
while (url->host_len >= i + 1 && url->data[i] != '.') {
i++;
}
if (url->data[i] != '.') {
break;
}
i++;
if (url->host_len < i + 1) {
break;
}
}
}
__done:
url->data[url->host_len] = backup_c;
return ret;
}
static unsigned char *tls_sni_search(unsigned char *data, int *data_len, int *needmore)
{
unsigned char *p = data;
int p_len = *data_len;
int i_data_len = p_len;
unsigned int i = 0;
unsigned short len;
if (p[i + 0] != 0x16) {//Content Type NOT HandShake
return NULL;
}
i += 1 + 2;
if (i >= p_len) return NULL;
len = ntohs(get_byte2(p + i + 0)); //content_len
i += 2;
if (i >= p_len) return NULL;
if (i + len > p_len) {
if (needmore && p[i] == 0x01) //HanShake Type is Client Hello
*needmore = 1;
}
p = p + i;
p_len = len;
i_data_len -= i;
i = 0;
if (p[i + 0] != 0x01) { //HanShake Type NOT Client Hello
return NULL;
}
i += 1;
if (i >= p_len || i >= i_data_len) return NULL;
len = (p[i + 0] << 8) + ntohs(get_byte2(p + i + 0 + 1)); //hanshake_len
i += 1 + 2;
if (i >= p_len || i >= i_data_len) return NULL;
if (i + len > p_len) return NULL;
p = p + i;
p_len = len;
i_data_len -= i;
i = 0;
i += 2 + 32;
if (i >= p_len || i >= i_data_len) return NULL; //tls_v, random
i += 1 + p[i + 0];
if (i >= p_len || i >= i_data_len) return NULL; //session id
i += 2 + ntohs(get_byte2(p + i + 0));
if (i >= p_len || i >= i_data_len) return NULL; //Cipher Suites
i += 1 + p[i + 0];
if (i >= p_len || i >= i_data_len) return NULL; //Compression Methods
len = ntohs(get_byte2(p + i + 0)); //ext_len
i += 2;
if (i + len > p_len) return NULL;
p = p + i;
p_len = len;
i_data_len -= i;
i = 0;
while (i < p_len && i < i_data_len) {
if (get_byte2(p + i + 0) != __constant_htons(0)) {
i += 2 + 2 + ntohs(get_byte2(p + i + 0 + 2));
continue;
}
len = ntohs(get_byte2(p + i + 0 + 2)); //sn_len
i = i + 2 + 2;
if (i + len > p_len || i + len > i_data_len) return NULL;
p = p + i;
p_len = len;
i_data_len -= i;
i = 0;
break;
}
if (i >= p_len || i >= i_data_len) return NULL;
len = ntohs(get_byte2(p + i + 0)); //snl_len
i += 2;
if (i + len > p_len || i + len > i_data_len) return NULL;
p = p + i;
p_len = len;
i_data_len -= i;
i = 0;
while (i < p_len && i < i_data_len) {
if (p[i + 0] != 0) {
i += 1 + 2 + ntohs(get_byte2(p + i + 0 + 1));
continue;
}
len = ntohs(get_byte2(p + i + 0 + 1));
i += 1 + 2;
if (i + len > p_len || i + len > i_data_len) return NULL;
*data_len = len;
return (p + i);
}
return NULL;
}
/* to do it simple:
just assume
1. data begin with 'GET ' or 'POST ' or 'HEAD '
*/
static int http_url_search(unsigned char *data,
int *data_len /*IN: data_len, OUT: host_len */, unsigned char **host,
int *uri_len, unsigned char **uri, int *http_method)
{
unsigned char *p = data;
int p_len = *data_len;
unsigned int i = 0;
if (i + 5 > p_len) return -1;
if ((p[i] == 'G' || p[i] == 'g') &&
(p[i + 1] == 'E' || p[i + 1] == 'e') &&
(p[i + 2] == 'T' || p[i + 2] == 't') &&
(p[i + 3] == ' ' || p[i + 3] == ' ')) {
i += 4;
*http_method = NATFLOW_HTTP_GET;
} else if ((p[i] == 'P' || p[i] == 'p') &&
(p[i + 1] == 'O' || p[i + 1] == 'o') &&
(p[i + 2] == 'S' || p[i + 2] == 's') &&
(p[i + 3] == 'T' || p[i + 3] == 't') &&
(p[i + 4] == ' ' || p[i + 4] == ' ')) {
i += 5;
*http_method = NATFLOW_HTTP_POST;
} else if ((p[i] == 'H' || p[i] == 'h') &&
(p[i + 1] == 'E' || p[i + 1] == 'e') &&
(p[i + 2] == 'A' || p[i + 2] == 'a') &&
(p[i + 3] == 'D' || p[i + 3] == 'd') &&
(p[i + 4] == ' ' || p[i + 4] == ' ')) {
i += 5;
*http_method = NATFLOW_HTTP_HEAD;
} else {
return 0;
}
while (i < p_len && p[i] == ' ') i++;
if (i >= p_len) return -1;
if (p[i] != '/') return -1;
*uri = p + i;
i++;
while (i < p_len && p[i] != ' ') i++;
if (i >= p_len) return -1;
if (p[i] != ' ') return -1;
*uri_len = p + i - *uri;
i++;
while (i < p_len && p[i] != '\n') i++;
if (i >= p_len) return -1;
i++;
do {
if (i + 5 > p_len) return -1;
if ((p[i] == 'H' || p[i] == 'h') &&
(p[i + 1] == 'o' || p[i + 1] == 'O') &&
(p[i + 2] == 's' || p[i + 2] == 'S') &&
(p[i + 3] == 't' || p[i + 3] == 'T') &&
p[i + 4] == ':') {
i += 5;
while (i < p_len && p[i] == ' ') i++;
if (i >= p_len) return -1;
*host = p + i;
i++;
while (i < p_len && p[i] != ' ' && p[i] != '\r' && p[i] != '\n') i++;
if (i >= p_len) return -1;
if (p[i] != ' ' && p[i] != '\r' && p[i] != '\n') return -1;
*data_len = p + i - *host;
return *data_len + *uri_len;
}
while (i < p_len && p[i] != '\n') i++;
i++;
} while (1);
return 0; /* not found */
}
static inline void natflow_urllogger_tcp_reply_rstack(const struct net_device *dev, struct sk_buff *oskb, struct nf_conn *ct, int pppoe_hdr)
{
struct sk_buff *nskb;
struct ethhdr *neth, *oeth;
struct iphdr *niph, *oiph;
struct tcphdr *otcph, *ntcph;
int len;
unsigned int csum;
int offset, header_len;
int pppoe_len = 0;
if (pppoe_hdr) {
pppoe_len = PPPOE_SES_HLEN;
skb_push(oskb, PPPOE_SES_HLEN);
oskb->protocol = __constant_htons(ETH_P_PPP_SES);
}
oeth = (struct ethhdr *)skb_mac_header(oskb);
oiph = ip_hdr(oskb);
otcph = (struct tcphdr *)((void *)oiph + oiph->ihl*4);
offset = pppoe_len + sizeof(struct iphdr) + sizeof(struct tcphdr) - oskb->len;
header_len = offset < 0 ? 0 : offset;
nskb = skb_copy_expand(oskb, skb_headroom(oskb), header_len, GFP_ATOMIC);
if (!nskb) {
NATFLOW_ERROR("alloc_skb fail\n");
goto out;
}
if (offset <= 0) {
if (pskb_trim(nskb, nskb->len + offset)) {
NATFLOW_ERROR("pskb_trim fail: len=%d, offset=%d\n", nskb->len, offset);
consume_skb(nskb);
goto out;
}
} else {
nskb->len += offset;
nskb->tail += offset;
}
//setup mac header
neth = eth_hdr(nskb);
niph = ip_hdr(nskb);
if ((char *)niph - (char *)neth >= ETH_HLEN) {
memcpy(neth->h_dest, oeth->h_source, ETH_ALEN);
memcpy(neth->h_source, oeth->h_dest, ETH_ALEN);
//neth->h_proto = htons(ETH_P_IP);
}
//setup ip header
memset(niph, 0, sizeof(struct iphdr));
niph->saddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip;
niph->daddr = oiph->saddr;
niph->version = oiph->version;
niph->ihl = 5;
niph->tos = 0;
niph->tot_len = htons(nskb->len - pppoe_len);
niph->ttl = 0x80;
niph->protocol = oiph->protocol;
niph->id = __constant_htons(0xDEAD);
niph->frag_off = 0x0;
ip_send_check(niph);
//setup tcp header
ntcph = (struct tcphdr *)((char *)ip_hdr(nskb) + sizeof(struct iphdr));
memset(ntcph, 0, sizeof(struct tcphdr));
ntcph->source = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all;
ntcph->dest = otcph->source;
ntcph->seq = otcph->ack_seq;
ntcph->ack_seq = htonl(ntohl(otcph->seq) + ntohs(oiph->tot_len) - (oiph->ihl<<2) - (otcph->doff<<2));
ntcph->doff = 5;
ntcph->ack = 1;
ntcph->rst = 1;
ntcph->psh = 0;
ntcph->fin = 0;
ntcph->window = 0;
//sum check
len = ntohs(niph->tot_len) - (niph->ihl<<2);
csum = csum_partial((char*)ntcph, len, 0);
ntcph->check = tcp_v4_check(len, niph->saddr, niph->daddr, csum);
//ready to send out
skb_push(nskb, (char *)niph - (char *)neth - pppoe_len);
if (pppoe_hdr) {
struct pppoe_hdr *ph = (struct pppoe_hdr *)((void *)eth_hdr(nskb) + ETH_HLEN);
ph->length = htons(ntohs(ip_hdr(nskb)->tot_len) + 2);
}
nskb->dev = (struct net_device *)dev;
nskb->ip_summed = CHECKSUM_UNNECESSARY;
dev_queue_xmit(nskb);
out:
if (pppoe_hdr) {
oskb->protocol = __constant_htons(ETH_P_IP);
skb_pull(oskb, PPPOE_SES_HLEN);
}
}
static inline void natflow_urllogger_tcp_reply_rstack6(const struct net_device *dev, struct sk_buff *oskb, struct nf_conn *ct, int pppoe_hdr)
{
struct sk_buff *nskb;
struct ethhdr *neth, *oeth;
struct ipv6hdr *niph, *oiph;
struct tcphdr *otcph, *ntcph;
int len;
unsigned int csum;
int offset, header_len;
int pppoe_len = 0;
if (pppoe_hdr) {
pppoe_len = PPPOE_SES_HLEN;
skb_push(oskb, PPPOE_SES_HLEN);
oskb->protocol = __constant_htons(ETH_P_PPP_SES);
}
oeth = (struct ethhdr *)skb_mac_header(oskb);
oiph = ipv6_hdr(oskb);
otcph = (struct tcphdr *)((void *)oiph + sizeof(struct ipv6hdr));
offset = pppoe_len + sizeof(struct ipv6hdr) + sizeof(struct tcphdr) - oskb->len;
header_len = offset < 0 ? 0 : offset;
nskb = skb_copy_expand(oskb, skb_headroom(oskb), header_len, GFP_ATOMIC);
if (!nskb) {
NATFLOW_ERROR("alloc_skb fail\n");
goto out;
}
if (offset <= 0) {
if (pskb_trim(nskb, nskb->len + offset)) {
NATFLOW_ERROR("pskb_trim fail: len=%d, offset=%d\n", nskb->len, offset);
consume_skb(nskb);
goto out;
}
} else {
nskb->len += offset;
nskb->tail += offset;
}
//setup mac header
neth = eth_hdr(nskb);
niph = ipv6_hdr(nskb);
if ((char *)niph - (char *)neth >= ETH_HLEN) {
memcpy(neth->h_dest, oeth->h_source, ETH_ALEN);
memcpy(neth->h_source, oeth->h_dest, ETH_ALEN);
//neth->h_proto = htons(ETH_P_IP);
}
//setup ip header
memset(niph, 0, sizeof(struct iphdr));
niph->version = oiph->version;
niph->priority = oiph->priority;
niph->saddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6;
niph->daddr = oiph->saddr;
niph->flow_lbl[2] = niph->flow_lbl[1] = niph->flow_lbl[0] = 0;
niph->payload_len = htons(sizeof(struct tcphdr));
niph->nexthdr = IPPROTO_TCP;
niph->hop_limit = 255;
//setup tcp header
ntcph = (struct tcphdr *)((char *)ip_hdr(nskb) + sizeof(struct ipv6hdr));
memset(ntcph, 0, sizeof(struct tcphdr));
ntcph->source = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all;
ntcph->dest = otcph->source;
ntcph->seq = otcph->ack_seq;
ntcph->ack_seq = htonl(ntohl(otcph->seq) + ntohs(oiph->payload_len) - (otcph->doff<<2));
ntcph->doff = 5;
ntcph->ack = 1;
ntcph->rst = 1;
ntcph->psh = 0;
ntcph->fin = 0;
ntcph->window = 0;
//sum check
len = ntohs(niph->payload_len);
csum = csum_partial((char*)ntcph, len, 0);
ntcph->check = tcp_v6_check(len, &niph->saddr, &niph->daddr, csum);
//ready to send out
skb_push(nskb, (char *)niph - (char *)neth - pppoe_len);
if (pppoe_hdr) {
struct pppoe_hdr *ph = (struct pppoe_hdr *)((void *)eth_hdr(nskb) + ETH_HLEN);
ph->length = htons(ntohs(ipv6_hdr(nskb)->payload_len) + sizeof(struct ipv6hdr) + 2);
}
nskb->dev = (struct net_device *)dev;
nskb->ip_summed = CHECKSUM_UNNECESSARY;
dev_queue_xmit(nskb);
out:
if (pppoe_hdr) {
oskb->protocol = __constant_htons(ETH_P_IP);
skb_pull(oskb, PPPOE_SES_HLEN);
}
}
struct urllogger_sni_cache_node {
unsigned long active_jiffies;
union {
__be32 src_ip;
struct in6_addr src_ipv6;
};
union {
__be32 dst_ip;
struct in6_addr dst_ipv6;
};
__be16 src_port;
__be16 dst_port;
unsigned short add_data_len;
struct sk_buff *skb;
};
#define URLLOGGER_CACHE_TIMEOUT 4
#define MAX_URLLOGGER_SNI_CACHE_NODE 64
static struct urllogger_sni_cache_node urllogger_sni_cache[NR_CPUS][MAX_URLLOGGER_SNI_CACHE_NODE];
static inline void urllogger_sni_cache_init(void)
{
int i, j;
for (i = 0; i < NR_CPUS; i++) {
for (j = 0; j < MAX_URLLOGGER_SNI_CACHE_NODE; j++) {
urllogger_sni_cache[i][j].skb = NULL;
}
}
}
static inline void urllogger_sni_cache_cleanup(void)
{
int i, j;
for (i = 0; i < NR_CPUS; i++) {
for (j = 0; j < MAX_URLLOGGER_SNI_CACHE_NODE; j++) {
if (urllogger_sni_cache[i][j].skb != NULL) {
consume_skb(urllogger_sni_cache[i][j].skb);
urllogger_sni_cache[i][j].skb = NULL;
}
}
}
}
static inline int urllogger_sni_cache_attach(__be32 src_ip, __be16 src_port, __be32 dst_ip, __be16 dst_port, struct sk_buff *skb, unsigned short add_data_len)
{
int i = smp_processor_id();
int j;
int next_to_use = MAX_URLLOGGER_SNI_CACHE_NODE;
for (j = 0; j < MAX_URLLOGGER_SNI_CACHE_NODE; j++) {
if (urllogger_sni_cache[i][j].skb != NULL &&
urllogger_sni_cache[i][j].src_ip == src_ip &&
urllogger_sni_cache[i][j].src_port == src_port &&
urllogger_sni_cache[i][j].dst_ip == dst_ip &&
urllogger_sni_cache[i][j].dst_port == dst_port) {
return -EEXIST;
} else if (next_to_use == MAX_URLLOGGER_SNI_CACHE_NODE && urllogger_sni_cache[i][j].skb == NULL) {
next_to_use = j;
}
}
if (next_to_use == MAX_URLLOGGER_SNI_CACHE_NODE) {
return -ENOMEM;
}
urllogger_sni_cache[i][next_to_use].src_ip = src_ip;
urllogger_sni_cache[i][next_to_use].src_port = src_port;
urllogger_sni_cache[i][next_to_use].dst_ip = dst_ip;
urllogger_sni_cache[i][next_to_use].dst_port = dst_port;
urllogger_sni_cache[i][next_to_use].add_data_len = add_data_len;
urllogger_sni_cache[i][next_to_use].skb = skb;
urllogger_sni_cache[i][next_to_use].active_jiffies = (unsigned long)jiffies;
return 0;
}
static inline int urllogger_sni_cache_attach6(struct in6_addr *src_ip, __be16 src_port, struct in6_addr *dst_ip, __be16 dst_port, struct sk_buff *skb, unsigned short add_data_len)
{
int i = smp_processor_id();
int j;
int next_to_use = MAX_URLLOGGER_SNI_CACHE_NODE;
for (j = 0; j < MAX_URLLOGGER_SNI_CACHE_NODE; j++) {
if (urllogger_sni_cache[i][j].skb != NULL &&
memcmp(&urllogger_sni_cache[i][j].src_ipv6, src_ip, 16) == 0 &&
urllogger_sni_cache[i][j].src_port == src_port &&
memcmp(&urllogger_sni_cache[i][j].dst_ipv6, dst_ip, 16) == 0 &&
urllogger_sni_cache[i][j].dst_port == dst_port) {
return -EEXIST;
} else if (next_to_use == MAX_URLLOGGER_SNI_CACHE_NODE && urllogger_sni_cache[i][j].skb == NULL) {
next_to_use = j;
}
}
if (next_to_use == MAX_URLLOGGER_SNI_CACHE_NODE) {
return -ENOMEM;
}
memcpy(&urllogger_sni_cache[i][next_to_use].src_ipv6, src_ip, 16);
urllogger_sni_cache[i][next_to_use].src_port = src_port;
memcpy(&urllogger_sni_cache[i][next_to_use].dst_ipv6, dst_ip, 16);
urllogger_sni_cache[i][next_to_use].dst_port = dst_port;
urllogger_sni_cache[i][next_to_use].add_data_len = add_data_len;
urllogger_sni_cache[i][next_to_use].skb = skb;
urllogger_sni_cache[i][next_to_use].active_jiffies = (unsigned long)jiffies;
return 0;
}
static inline struct sk_buff *urllogger_sni_cache_detach(__be32 src_ip, __be16 src_port, __be32 dst_ip, __be16 dst_port, unsigned short *add_data_len)
{
int i = smp_processor_id();
int j = 0;
struct sk_buff *skb = NULL;
for (j = 0; j < MAX_URLLOGGER_SNI_CACHE_NODE; j++) {
if (urllogger_sni_cache[i][j].skb != NULL) {
if (time_after(jiffies, urllogger_sni_cache[i][j].active_jiffies + URLLOGGER_CACHE_TIMEOUT * HZ)) {
consume_skb(urllogger_sni_cache[i][j].skb);
urllogger_sni_cache[i][j].skb = NULL;
} else if (urllogger_sni_cache[i][j].src_ip == src_ip &&
urllogger_sni_cache[i][j].src_port == src_port &&
urllogger_sni_cache[i][j].dst_ip == dst_ip &&
urllogger_sni_cache[i][j].dst_port == dst_port) {
skb = urllogger_sni_cache[i][j].skb;
*add_data_len = urllogger_sni_cache[i][j].add_data_len;
urllogger_sni_cache[i][j].skb = NULL;
break;
}
}
}
for (; j < MAX_URLLOGGER_SNI_CACHE_NODE; j++) {
if (urllogger_sni_cache[i][j].skb != NULL) {
if (time_after(jiffies, urllogger_sni_cache[i][j].active_jiffies + URLLOGGER_CACHE_TIMEOUT * HZ)) {
consume_skb(urllogger_sni_cache[i][j].skb);
urllogger_sni_cache[i][j].skb = NULL;
}
}
}
return skb;
}
static inline struct sk_buff *urllogger_sni_cache_detach6(struct in6_addr *src_ip, __be16 src_port, struct in6_addr *dst_ip, __be16 dst_port, unsigned short *add_data_len)
{
int i = smp_processor_id();
int j = 0;
struct sk_buff *skb = NULL;
for (j = 0; j < MAX_URLLOGGER_SNI_CACHE_NODE; j++) {
if (urllogger_sni_cache[i][j].skb != NULL) {
if (time_after(jiffies, urllogger_sni_cache[i][j].active_jiffies + URLLOGGER_CACHE_TIMEOUT * HZ)) {
consume_skb(urllogger_sni_cache[i][j].skb);
urllogger_sni_cache[i][j].skb = NULL;
} else if (memcmp(&urllogger_sni_cache[i][j].src_ipv6, src_ip, 16) == 0 &&
urllogger_sni_cache[i][j].src_port == src_port &&
memcmp(&urllogger_sni_cache[i][j].dst_ipv6, dst_ip, 16) == 0 &&
urllogger_sni_cache[i][j].dst_port == dst_port) {
skb = urllogger_sni_cache[i][j].skb;
*add_data_len = urllogger_sni_cache[i][j].add_data_len;
urllogger_sni_cache[i][j].skb = NULL;
break;
}
}
}
for (; j < MAX_URLLOGGER_SNI_CACHE_NODE; j++) {
if (urllogger_sni_cache[i][j].skb != NULL) {
if (time_after(jiffies, urllogger_sni_cache[i][j].active_jiffies + URLLOGGER_CACHE_TIMEOUT * HZ)) {
consume_skb(urllogger_sni_cache[i][j].skb);
urllogger_sni_cache[i][j].skb = NULL;
}
}
}
return skb;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
static unsigned int natflow_urllogger_hook_v1(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
static unsigned int natflow_urllogger_hook_v1(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
unsigned int hooknum = ops->hooknum;
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
static unsigned int natflow_urllogger_hook_v1(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct nf_hook_state *state)
{
unsigned int hooknum = state->hook;
const struct net_device *in = state->in;
const struct net_device *out = state->out;
#else
static unsigned int natflow_urllogger_hook_v1(void *priv,
struct sk_buff *skb,
const struct nf_hook_state *state)
{
unsigned int hooknum = state->hook;
const struct net_device *in = state->in;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
const struct net_device *out = state->out;
#endif
#endif
int ret = NF_ACCEPT;
enum ip_conntrack_info ctinfo;
int data_len;
unsigned char *data;
natflow_t *nf;
struct nf_conn *ct;
struct iphdr *iph;
void *l4;
int bridge = 0;
if (!urllogger_store_enable)
return NF_ACCEPT;
if (skb->protocol == __constant_htons(ETH_P_PPP_SES) &&
pppoe_proto(skb) == __constant_htons(PPP_IP) /* Internet Protocol */) {
skb_pull(skb, PPPOE_SES_HLEN);
skb->protocol = __constant_htons(ETH_P_IP);
skb->network_header += PPPOE_SES_HLEN;
bridge = 1;
} else if (skb->protocol == __constant_htons(ETH_P_PPP_SES) &&
pppoe_proto(skb) == __constant_htons(PPP_IPV6) /* Internet Protocol version 6 */) {
skb_pull(skb, PPPOE_SES_HLEN);
skb->protocol = __constant_htons(ETH_P_IPV6);
skb->network_header += PPPOE_SES_HLEN;
bridge = 1;
} else if (skb->protocol != __constant_htons(ETH_P_IP) && skb->protocol != __constant_htons(ETH_P_IPV6)) {
return NF_ACCEPT;
}
ct = nf_ct_get(skb, &ctinfo);
if (NULL == ct)
goto out;
if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
goto out;
if ((ct->status & IPS_NATFLOW_URLLOGGER_HANDLED))
goto out;
if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num == AF_INET6)
goto urllogger_hook_ipv6_main;
iph = ip_hdr(skb);
if (iph->protocol != IPPROTO_TCP) {
goto out;
}
if (skb_try_make_writable(skb, iph->ihl * 4 + sizeof(struct tcphdr))) {
goto out;
}
iph = ip_hdr(skb);
l4 = (void *)iph + iph->ihl * 4;
/* pause fastnat path */
nf = natflow_session_get(ct);
if (nf && !(nf->status & NF_FF_URLLOGGER_USE)) {
/* tell FF -urllogger- need this conn */
simple_set_bit(NF_FF_URLLOGGER_USE_BIT, &nf->status);
}
data_len = ntohs(iph->tot_len) - (iph->ihl * 4 + TCPH(l4)->doff * 4);
if (data_len > 0) {
struct sk_buff *prev_skb = NULL;
unsigned char *host = NULL;
int host_len;
unsigned short add_data_len = 0;
if (skb_try_make_writable(skb, skb->len)) {
goto out;
}
iph = ip_hdr(skb);
l4 = (void *)iph + iph->ihl * 4;
prev_skb = urllogger_sni_cache_detach(iph->saddr, TCPH(l4)->source, iph->daddr, TCPH(l4)->dest, &add_data_len);
if (prev_skb) {
struct iphdr *prev_iph = ip_hdr(prev_skb);
void *prev_l4 = (void *)prev_iph + prev_iph->ihl * 4;
int prev_data_len = ntohs(prev_iph->tot_len) - (prev_iph->ihl * 4 + TCPH(prev_l4)->doff * 4);
data = skb->data + iph->ihl * 4 + TCPH(l4)->doff * 4;
if (ntohl(TCPH(l4)->seq) == ntohl(TCPH(prev_l4)->seq) + prev_data_len + add_data_len) {
int needmore = 0;
if (skb_tailroom(prev_skb) < add_data_len + data_len &&
pskb_expand_head(prev_skb, 0, add_data_len + data_len, GFP_ATOMIC)) {
NATFLOW_ERROR("(NUHv1)" DEBUG_TCP_FMT ": pskb_expand_head failed\n", DEBUG_TCP_ARG(iph,l4));
consume_skb(prev_skb);
return NF_ACCEPT;
}
prev_iph = ip_hdr(prev_skb);
prev_l4 = (void *)prev_iph + prev_iph->ihl * 4;
memcpy(prev_skb->data + prev_skb->len + add_data_len, data, data_len);
add_data_len += data_len;
data = prev_skb->data + prev_iph->ihl * 4 + TCPH(prev_l4)->doff * 4;
host_len = prev_data_len + add_data_len;
host = tls_sni_search(data, &host_len, &needmore);
if (!host && needmore == 1) {
if (add_data_len >= 32 * 1024 ||
urllogger_sni_cache_attach(prev_iph->saddr, TCPH(prev_l4)->source,
prev_iph->daddr, TCPH(prev_l4)->dest, prev_skb, add_data_len) != 0) {
NATFLOW_ERROR("(NUHv1)" DEBUG_TCP_FMT ": urllogger_sni_cache_attach failed with add_data_len=%u\n", DEBUG_TCP_ARG(iph,l4), add_data_len);
consume_skb(prev_skb);
goto __urllogger_ip_skip;
}
return NF_ACCEPT;
}
} else if (ntohl(TCPH(l4)->seq) == ntohl(TCPH(prev_l4)->seq)) {
if (urllogger_sni_cache_attach(prev_iph->saddr, TCPH(prev_l4)->source,
prev_iph->daddr, TCPH(prev_l4)->dest, prev_skb, add_data_len) != 0) {
NATFLOW_ERROR("(NUHv1)" DEBUG_TCP_FMT ": urllogger_sni_cache_attach failed\n", DEBUG_TCP_ARG(iph,l4));
consume_skb(prev_skb);
goto __urllogger_ip_skip;
}
return NF_ACCEPT;
} else {
consume_skb(prev_skb);
goto __urllogger_ip_skip;
}
} else {
int needmore = 0;
data = skb->data + iph->ihl * 4 + TCPH(l4)->doff * 4;
host_len = data_len;
host = tls_sni_search(data, &host_len, &needmore);
if (!host && needmore == 1) {
prev_skb = skb_copy(skb, GFP_ATOMIC);
if (prev_skb) {
if (urllogger_sni_cache_attach(iph->saddr, TCPH(l4)->source, iph->daddr, TCPH(l4)->dest, prev_skb, 0) != 0) {
NATFLOW_ERROR("(NUHv1)" DEBUG_TCP_FMT ": urllogger_sni_cache_attach failed\n", DEBUG_TCP_ARG(iph,l4));
consume_skb(prev_skb);
goto __urllogger_ip_skip;
}
}
return NF_ACCEPT;
}
}
data = skb->data + iph->ihl * 4 + TCPH(l4)->doff * 4;
__urllogger_ip_skip:
/* check one packet only */
set_bit(IPS_NATFLOW_URLLOGGER_HANDLED_BIT, &ct->status);
if (nf && (nf->status & NF_FF_URLLOGGER_USE)) {
/* tell FF -urllogger- has finished it's job */
simple_clear_bit(NF_FF_URLLOGGER_USE_BIT, &nf->status);
}
if (host) {
int rule_id = 0;
struct urlinfo *url = kmalloc(ALIGN(sizeof(struct urlinfo) + host_len + 1, __URLINFO_ALIGN), GFP_ATOMIC);
if (!url)
goto out;
INIT_LIST_HEAD(&url->list);
url->host_len = urlinfo_copy_host_tolower(url->data, host, host_len);
url->data[host_len] = 0;
url->data_len = host_len + 1;
if (urllogger_store_tuple_type == 0) {
/* 0: dir0-src dir0-dst */