-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.c
1990 lines (1734 loc) · 48.3 KB
/
text.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
#include <linux/string.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/net.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/icmp.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/version.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/list.h>
#include <linux/ctype.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/genetlink.h>
#include <net/netfilter/nf_nat_helper.h>
#include <net/netfilter/nf_conntrack.h>
#include <net/netfilter/nf_nat.h>
#include <linux/netfilter_ipv4/igd_filter/igd_filter.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4/igd_filter/nc_netlink.h>
#include "nf_conntrack_text.h"
//#define AD_DEBUG
#ifdef AD_DEBUG
#define ad_debug(fmt, args...) \
printk("text_replace: " fmt, ## args)
#else
#define ad_debug(fmt, ...)
#endif
#define MAX_EXTEND_NUM 2
#define MAX_EXTEND_SIZE 64
#define MAX_MATCH_RESULT 8
#define TEXT_GROUP_MAX 128
#define TEXT_GROUP_NAME 16
enum {
HTTP_FILTER_DISMATCH_BIT,
HTTP_FILTER_ORIGINAL_MATCHED_BIT,
HTTP_FILTER_REPLY_MATCHED_BIT,
HTTP_FILTER_NON_REPLACE_BIT,
HTTP_FILTER_PROXY_BIT,
HTTP_FILTER_RESET_BIT,
HTTP_FILTER_DROP_BIT,
HTTP_FILTER_SEQ_ADJUST_BIT,
HTTP_FILTER_MISS_HOST_BIT,
HTTP_FILTER_OWN_HOST_BIT,
HTTP_FILTER_MATCH_HOST_BIT,
};
struct text_replace_group {
struct list_head list;
struct list_head url_head;
int16_t priority;
uint16_t id;
pid_t tgid;
char name[TEXT_GROUP_NAME];
struct dns_tree_head root;
};
struct url_item {
struct url_tree url;
uint16_t mode;
uint16_t code; /* HTTP status code */
uint32_t flags;
uint32_t private_size;
uint32_t id;
void *private;
struct list_head list; /* link to group url_head */
struct list_head option_head[IP_CT_DIR_MAX]; /* link match option */
};
struct match_option;
struct match_result {
uint16_t type;
uint16_t flags;
uint16_t match_len;
uint16_t match_offset;
uint16_t rep_len;
unsigned char *rep_data;
};
struct match_param {
bool hotdrop;
unsigned int rep_len;
unsigned int match_len;
unsigned char *next;
/* record extend number */
int extend;
/* recode index of res */
int i;
/* record already processed number by target func */
int finished;
/* only extend packet is need record in res */
struct match_result res[MAX_MATCH_RESULT];
};
struct match_ops {
const char *name;
uint16_t type;
bool (*match)(struct sk_buff *skb, struct match_option *opt, struct match_param *param);
/* return NF_ACCEPT ,NF_STOP,NF_REPEAT
* NF_STOP:stop match packet
*/
int (*target)(struct sk_buff *skb, struct match_option *opt, struct match_param *param);
struct list_head list;
};
struct match_option {
struct list_head list;
struct match_ops *ops;
struct k_text_option opt[0];
};
static int white_num = 0;
static int black_num = 0;
module_param(white_num, int, 0444);
module_param(black_num, int, 0444);
static DECLARE_BITMAP(text_group, TEXT_GROUP_MAX);
/* we use global lock, so need optimization in SMP */
static DEFINE_SPINLOCK(g_lock);
static LIST_HEAD(grp_list);
static struct list_head __rcu *grp_entry = NULL;
static atomic_t ad_rule_id = ATOMIC_INIT(0);
static atomic_t white_rule_num = ATOMIC_INIT(0);
static atomic_t black_rule_num = ATOMIC_INIT(0);
static LIST_HEAD(ops_list);
static DEFINE_PER_CPU(struct match_param, param);
#define HTTP_RESPONCE_MAX 256
struct http_response {
char data[HTTP_RESPONCE_MAX];
};
static DEFINE_PER_CPU(struct http_response, http_response);
struct match_ops *find_match_ops_by_type(int type);
/* the netlink family */
static struct genl_family text_replace_fam = {
.id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
.name = GENL_HTTP_FILTER_NAME, /* have users key off the name instead */
.hdrsize = 0, /* no private header */
.version = 1, /* no particular meaning now */
.maxattr = HTTP_FILTER_ATTR_MAX,
};
static struct genl_multicast_group text_replace_mcgrp = {
.name = TEXT_REPLACE_GENL_MCAST_GROUP_NAME,
};
static int text_replace_mc_event(const unsigned char *mac, char *dns, uint32_t id, uint16_t gid)
{
struct sk_buff *nskb;
void *hdr;
char buf[64];
int err = 0;
/*plus 256 for header */
nskb = nlmsg_new(256, GFP_ATOMIC);
if (nskb == NULL) {
err = -ENOMEM;
goto out;
}
hdr = genlmsg_put(nskb, 0, 1, &text_replace_fam, 0, HTTP_FILTER_CMD_REPORT_STATIS);
if (hdr == NULL) {
err = -ENOMEM;
goto out;
}
dns_2_str(dns, buf, sizeof(buf));
NLA_PUT(nskb, HTTP_FILTER_ATTR_URL, strlen(buf) + 1, buf);
NLA_PUT(nskb, HTTP_FILTER_ATTR_HOST_MAC, ETH_ALEN, mac);
NLA_PUT_U32(nskb, HTTP_FILTER_ATTR_AD_ID, id);
NLA_PUT_U16(nskb, HTTP_FILTER_ATTR_GROUP_ID, gid);
genlmsg_end(nskb, hdr);
genlmsg_multicast(nskb, 0, text_replace_mcgrp.id, GFP_ATOMIC);
return 0;
nla_put_failure:
err = -ENOMEM;
out:
if (nskb)
nlmsg_free(nskb);
return err;
}
static inline int get_current_rule_id(void)
{
return atomic_read(&ad_rule_id);
}
static inline bool rule_is_expired(struct nf_text_replace *ad)
{
return ad->rule_id != get_current_rule_id();
}
static inline void flush_rule_id(void)
{
atomic_inc(&ad_rule_id);
}
static void group_lock(void)
{
spin_lock_bh(&g_lock);
}
static void group_unlock(void)
{
spin_unlock_bh(&g_lock);
}
static void free_match_option(struct url_item *u_item)
{
struct match_option *option, *n;
int i;
for (i = 0; i < IP_CT_DIR_MAX; i++) {
list_for_each_entry_safe(option, n, &u_item->option_head[i], list) {
list_del(&option->list);
kfree(option);
}
}
}
static inline void free_url_item(struct url_item *u_item)
{
free_match_option(u_item);
if (u_item->private)
kfree(u_item->private);
kfree(u_item);
}
static inline void free_text_replace_group(struct text_replace_group *grp)
{
struct url_item *item, *n;
list_for_each_entry_safe(item, n, &grp->url_head, list) {
list_del(&item->list);
if (item->mode == HTTP_FILTER_ACCEPT_MODE) {
white_num--;
atomic_dec(&white_rule_num);
} else {
black_num--;
atomic_dec(&black_rule_num);
}
free_url_item(item);
}
dns_tree_head_free(&grp->root);
kfree(grp);
}
static struct text_replace_group *find_group_by_name(const char *name)
{
struct text_replace_group *grp;
list_for_each_entry_rcu(grp, &grp_list, list) {
if (!strcmp(grp->name, name) && current->tgid == grp->tgid)
return grp;
}
return NULL;
}
static struct text_replace_group *find_group_by_id(uint16_t id)
{
struct text_replace_group *grp;
list_for_each_entry_rcu(grp, &grp_list, list) {
if (grp->id == id && current->tgid == grp->tgid)
return grp;
}
return NULL;
}
static int register_text_replace_group(struct text_replace_group *reg)
{
struct text_replace_group *grp;
int err = 0;
int id;
group_lock();
if (find_group_by_name(reg->name)) {
err = -EEXIST;
goto out;
}
id = find_first_zero_bit(text_group, TEXT_GROUP_MAX);
if (id >= TEXT_GROUP_MAX) {
err = -ENOMEM;
goto out;
}
list_for_each_entry_rcu(grp, &grp_list, list) {
if (reg->priority < grp->priority)
break;
}
err = reg->id = id;
set_bit(id, text_group);
list_add_rcu(®->list, grp->list.prev);
out:
group_unlock();
return err;
}
static void unregister_text_replace_group(struct text_replace_group *reg)
{
list_del_rcu(®->list);
clear_bit(reg->id, text_group);
}
static int text_replace_delete_group_by_id(uint16_t id)
{
struct text_replace_group *grp;
int err = 0;
group_lock();
grp = find_group_by_id(id);
if (!grp) {
err = -EINVAL;
group_unlock();
goto out;
}
unregister_text_replace_group(grp);
group_unlock();
/* we must be wait rcu, because conntrack cached the url_item pointer */
rcu_assign_pointer(grp_entry, NULL);
synchronize_rcu();
/* invalid the url_item pointer of conntrack */
flush_rule_id();
/* now it is safe free group */
free_text_replace_group(grp);
/* reactivate the entry point of group */
rcu_assign_pointer(grp_entry, &grp_list);
out:
return err;
}
static int text_replace_mc_get_id(struct sk_buff *skb, struct genl_info *info)
{
struct sk_buff *nskb;
void *hdr;
int err = 0;
/*plus 256 for header */
nskb = nlmsg_new(128, GFP_ATOMIC);
if (nskb == NULL) {
err = -ENOMEM;
goto out;
}
hdr = genlmsg_put(nskb, 0, 1, &text_replace_fam, 0, HTTP_FILTER_CMD_GET_MC_ID);
if (hdr == NULL) {
err = -ENOMEM;
goto out;
}
NLA_PUT_U32(nskb, HTTP_FILTER_ATTR_MC_ID, text_replace_mcgrp.id);
genlmsg_end(nskb, hdr);
return genlmsg_reply(nskb, info);
nla_put_failure:
err = -ENOMEM;
out:
if (nskb)
nlmsg_free(nskb);
return err;
}
static int send_group_id(struct genl_info *info, uint16_t id)
{
struct sk_buff *skb;
void *hdr;
int err = 0;
skb = nlmsg_new(512, GFP_KERNEL);
if (skb == NULL) {
err = -ENOMEM;
goto out;
}
hdr = genlmsg_put(skb, info->snd_pid, info->snd_seq, &text_replace_fam, 0, HTTP_FILTER_CMD_NEW_GROUP);
if (hdr == NULL) {
err = -ENOMEM;
goto out;
}
NLA_PUT_U16(skb, HTTP_FILTER_ATTR_GROUP_ID, id);
genlmsg_end(skb, hdr);
return genlmsg_reply(skb, info);
nla_put_failure:
err = -ENOMEM;
out:
if (skb)
nlmsg_free(skb);
return err;
}
static inline void update_match_offset(struct match_param *par, int index,
uint16_t match_offset, const int offset)
{
if (!offset)
return;
for (; index < par->i; index++) {
if (par->res[index].match_offset >= match_offset)
par->res[index].match_offset += offset;
}
}
static int text_replace_send_client_ack(struct sk_buff *skb)
{
struct tcphdr otcph, *oth;
struct iphdr *iph;
int len;
iph = ip_hdr(skb);
oth = skb_header_pointer(skb, iph->ihl * 4,
sizeof(otcph), &otcph);
if (!oth)
return -1;
len = skb->len - iph->ihl * 4 - oth->doff * 4;
if (oth->fin || (oth->ack && len > 0))
text_replace_send_tcp(skb, NF_INET_FORWARD, 1, oth->fin ? TCP_FLAG_FIN : 0, 0, NULL);
return 0;
}
static const char *get_reason_phase_by_code(uint16_t status_code)
{
switch (status_code) {
case 200:
return "OK";
case 400:
return "Bad Request";
case 401:
return "Unauthorized";
case 403:
return "Forbidden";
case 404:
return "Not Found";
case 501:
return "Not Implemented";
case 503:
return "Service Unavailable";
default:
return "Unkonwn";
}
}
static inline int text_replace_send_client_rst(struct sk_buff *skb)
{
return text_replace_send_tcp(skb, NF_INET_FORWARD, 1, TCP_FLAG_RST, 0, NULL);
}
static int text_replace_send_client_http_200(struct sk_buff *skb)
{
int len;
static const char data[] = "HTTP/1.1 200 OK\r\n"
"Content-Length: 9\r\n"
"Connection: close\r\n"
"Server: nginx/1.8.0\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n\r\n<!--//-->";
len = sizeof(data) - 1;
text_replace_send_tcp(skb, NF_INET_FORWARD, 1, 0, len, data);
return 0;
}
static int text_replace_send_client_http_403(struct sk_buff *skb)
{
int len;
static const char data[] = "HTTP/1.1 403 Forbidden\r\n"
"Content-Length: 0\r\n"
"Connection: close\r\n"
"Server: nginx/1.8.0\r\n"
"Content-Type: image/gif\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n\r\n";
len = sizeof(data) - 1;
text_replace_send_tcp(skb, NF_INET_FORWARD, 1, 0, len, data);
return 0;
}
//not use in process context
static int text_replace_send_client_http_response(struct sk_buff *skb, struct url_item *item)
{
struct http_response *resp;
int len;
if (item->private)
return text_replace_send_tcp(skb, NF_INET_FORWARD, 1, 0, item->private_size, item->private);
if (item->code == 200)
return text_replace_send_client_http_200(skb);
else if (item->code == 403)
return text_replace_send_client_http_403(skb);
resp = this_cpu_ptr(&http_response);
len = snprintf(resp->data, HTTP_RESPONCE_MAX, "HTTP/1.1 %hu %s\r\n"
"Content-Length: 0\r\n"
"Connection: close\r\n"
"Server: nginx/1.8.0\r\n"
"Content-Type: image/gif\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n\r\n", item->code, get_reason_phase_by_code(item->code));
return text_replace_send_tcp(skb, NF_INET_FORWARD, 1, 0, len, resp->data);
}
int text_replace_proxy(struct sk_buff *skb)
{
struct nf_conn *ct;
struct nf_text_replace *ad;
enum ip_conntrack_info ctinfo;
ct = nf_ct_get(skb, &ctinfo);
if (!ct)
return NF_DROP;
ad = nfct_text_replace(ct);
if (!ad)
return NF_DROP;
/* drop the server packet */
if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
return NF_DROP;
if (test_bit(HTTP_FILTER_RESET_BIT, &ad->status))
//send rst again ?
text_replace_send_client_rst(skb);
else if (test_bit(HTTP_FILTER_PROXY_BIT, &ad->status))
text_replace_send_client_ack(skb);
//else DROP it only
return NF_DROP;
}
static int copy_uri(struct nf_http_log *log, int data_len, unsigned char *data)
{
if (data_len >= 3 && !strncasecmp(data, "GET", 3)) {
data_len -= 3;
data += 3;
} else if (data_len >= 4 && !strncasecmp(data, "POST", 4)) {
data_len -= 4;
data += 4;
} else
return 0;
if (data_len) {
while (data_len > 0 && isspace(*data)) {
data_len--;
data++;
}
if (!data_len)
return 0;
if (log->uri_len && !memcmp(data, log->uri, min((int)data_len, (int)log->uri_len)))
return log->uri_len;
data_len = min((int)data_len, (int)(sizeof(log->uri) - 1));
log->uri_len = __igd_strcpy_end(log->uri, data, data_len, ' ');
return log->uri_len;
}
return 0;
}
static bool generic_match(struct sk_buff *skb, struct match_option *opt,
struct match_param *par)
{
struct match_result *res;
struct iphdr *iph;
struct tcphdr *tcph;
unsigned char *data, *match, *start;
int i;
uint16_t len;
unsigned int match_len;
iph = ip_hdr(skb);
tcph = (void *)iph + iph->ihl*4;
data = (void *)tcph + tcph->doff*4;
match = data;
if (par->next && (opt->opt->flags & HTTP_FILTER_MATCH_REPEAT))
match = par->next;
len = GET_MATCH_LEN(opt->opt);
if (!len || par->i == MAX_MATCH_RESULT)
return false;
/* match data alg
* support wildcard '*' and '?'
*/
start = text_replace_fnmatch_alg(match, skb_tail_pointer(skb) - match, GET_MATCH_DATA(opt->opt), len, &match_len);
if (!start)
return false;
res = &par->res[par->i];
res->type = opt->opt->type;
res->flags = opt->opt->flags;
res->match_len = match_len;
res->match_offset = start - data;
res->rep_len = GET_REPLACE_LEN(opt->opt);
res->rep_data = GET_REPLACE_DATA(opt->opt);
for (i = 0; i < par->i; i++) {
if (res->match_offset + res->match_len <= par->res[i].match_offset
|| res->match_offset >= par->res[i].match_offset +
par->res[i].match_len) {
continue;
} else {
/* matched area is overlap, can't process */
//param->hotdrop = true;
printk(KERN_ERR "Warning: matched area is overlap, can't process\n");
return false;
}
}
return true;
}
static int generic_target(struct sk_buff *skb, struct match_option *opt,
struct match_param *par)
{
struct iphdr *iph;
struct tcphdr *tcph;
unsigned char *data;
struct match_result *res;
int offset = 0;
iph = ip_hdr(skb);
tcph = (void *)iph + iph->ihl*4;
data = (void *)tcph + tcph->doff*4;
res = &par->res[par->i];
/* mangle tcp data */
if (res->rep_len <= res->match_len ||
res->rep_len - res->match_len <= skb_tailroom(skb)) {
if (!skb_make_writable(skb, skb->len))
return NF_STOP;
process:
iph = ip_hdr(skb);
tcph = (void *)iph + iph->ihl*4;
data = (void *)tcph + tcph->doff*4;
if (res->rep_len != res->match_len)
memmove(data + res->match_offset + res->rep_len,
data + res->match_offset + res->match_len,
skb_tail_pointer(skb) - (data + res->match_offset + res->match_len));
if (res->rep_len)
memcpy(data + res->match_offset, res->rep_data, res->rep_len);
if (res->rep_len > res->match_len) {
ad_debug("%s: Extending packet by "
"%u from %u bytes\n", __func__, res->rep_len - res->match_len, skb->len);
skb_put(skb, res->rep_len - res->match_len);
} else {
ad_debug("%s: Shrinking packet from "
"%u from %u bytes\n", __func__, res->match_len - res->rep_len, skb->len);
__skb_trim(skb, skb->len + res->rep_len - res->match_len);
}
offset = (int)res->rep_len - (int)res->match_len;
update_match_offset(par, 0, res->match_offset, offset);
par->finished++;
} else if (par->extend < MAX_EXTEND_NUM) {
/* try expand room */
par->extend++;
if (!skb_make_writable(skb, skb->len))
return NF_STOP;
if (skb->len + res->rep_len - res->match_len > 65535)
return NF_STOP;
if (pskb_expand_head(skb, 0, res->rep_len - res->match_len + MAX_EXTEND_SIZE, GFP_ATOMIC))
return NF_STOP;
goto process;
} else {
/* badly! record matched result
* and realloc room later
*/
par->rep_len += res->rep_len;
par->match_len += res->match_len;
par->i++;
}
/* skip the matched area and match left data */
if (res->flags & HTTP_FILTER_MATCH_REPEAT) {
par->next = data + res->match_offset + res->match_len + offset;
return NF_REPEAT;
}
return NF_ACCEPT;
}
static int insert_target(struct sk_buff *skb, struct match_option *opt,
struct match_param *par)
{
struct iphdr *iph;
struct tcphdr *tcph;
unsigned char *data;
struct match_result *res;
int offset = 0;
iph = ip_hdr(skb);
tcph = (void *)iph + iph->ihl*4;
data = (void *)tcph + tcph->doff*4;
res = &par->res[par->i];
if (res->rep_len <= skb_tailroom(skb)) {
if (!skb_make_writable(skb, skb->len))
return NF_STOP;
process:
iph = ip_hdr(skb);
tcph = (void *)iph + iph->ihl*4;
data = (void *)tcph + tcph->doff*4;
if (res->flags & HTTP_FILTER_MATCH_INSERT_BEFORE)
offset = 0; /* insert before */
else
offset = res->match_len; /* insert after */
memmove(data + res->match_offset + res->rep_len + offset,
data + res->match_offset + offset,
skb_tail_pointer(skb) - (data + res->match_offset + offset));
memcpy(data + res->match_offset + offset, res->rep_data, res->rep_len);
ad_debug("%s: Extending packet by "
"%u from %u bytes\n", __func__, res->rep_len, skb->len);
skb_put(skb, res->rep_len);
/* update offset */
offset = res->rep_len;
update_match_offset(par, 0, res->match_offset, offset);
par->finished++;
} else if (par->extend < MAX_EXTEND_NUM) {
par->extend++;
if (!skb_make_writable(skb, skb->len))
return NF_STOP;
if (skb->len + res->rep_len > 65535)
return NF_STOP;
if (pskb_expand_head(skb, 0, res->rep_len + MAX_EXTEND_SIZE, GFP_ATOMIC))
return NF_STOP;
goto process;
} else {
/* badly! record matched result
* and realloc room later
*/
par->rep_len += res->rep_len;
par->i++;
}
/* skip the matched area and match left data */
if (res->flags & HTTP_FILTER_MATCH_REPEAT) {
par->next = data + res->match_offset + res->match_len + offset;
return NF_REPEAT;
}
return NF_ACCEPT;
}
static inline void match_param_init(struct match_param *par)
{
par->hotdrop = false;
par->rep_len = par->match_len = 0;
par->next = NULL;
par->extend = 0;
par->i = par->finished = 0;
}
static int text_replace_match_option(unsigned int hooknum, struct sk_buff *skb,
struct nf_conn *ct, struct nf_text_replace *ad,
struct url_item *u_item, enum ip_conntrack_info ctinfo)
{
struct match_option *op;
struct match_result *res;
unsigned char *data;
struct iphdr *iph;
struct tcphdr *tcph;
unsigned int oldlen, newlen;
struct match_param *par;
int i;
int len;
int offset;
int verdict = NF_ACCEPT;
if (skb_is_nonlinear(skb) || unlikely(skb_shared(skb)))
goto dismatch;
iph = ip_hdr(skb);
tcph = (void *)iph + iph->ihl*4;
/* record old len , because target function maybe mangle tcp data */
oldlen = skb->len - iph->ihl*4 - tcph->doff*4;
/* lock-free */
par = this_cpu_ptr(¶m);
match_param_init(par);
/* match the packet */
list_for_each_entry(op, &u_item->option_head[CTINFO2DIR(ctinfo)], list) {
repeat:
if (op->ops->match(skb, op, par)) {
verdict = op->ops->target(skb, op, par);
if (verdict != NF_ACCEPT) {
if (verdict == NF_REPEAT) {
verdict = NF_ACCEPT;
if (par->i == MAX_MATCH_RESULT)
break;
goto repeat;
}
break;
}
}
if (par->hotdrop)
break;
if (par->i == MAX_MATCH_RESULT)
break;
par->next = NULL;
}
/* set NF_ACCEPT
* target func direct drop packet not permited
*
*/
verdict = NF_ACCEPT;
if (par->i == 0 && par->finished == 0)
goto next_match;
/* sure the skb is linear
* sure alloc new skb data if skb is cloned
*/
if (!skb_make_writable(skb, skb->len))
goto drop;
len = par->rep_len - par->match_len;
if (len > 0 && skb->len + len > 65535)
goto drop;
/* reload ptr */
iph = ip_hdr(skb);
tcph = (void *)iph + iph->ihl*4;
data = (void *)tcph + tcph->doff*4;
if (len > skb_tailroom(skb)) {
if (pskb_expand_head(skb, 0, len - skb_tailroom(skb) + 16, GFP_ATOMIC))
goto drop;
/* reload ptr */
iph = ip_hdr(skb);
tcph = (void *)iph + iph->ihl*4;
data = (void *)tcph + tcph->doff*4;
}
for (i = 0; i < par->i; i++) {
res = &par->res[i];
switch (res->type) {
case HTTP_FILTER_TYPE_REPLACE:
/* get here
* the res->rep_len > res->match_len
*/
memmove(data + res->match_offset + res->rep_len,
data + res->match_offset + res->match_len,
skb_tail_pointer(skb) - (data + res->match_offset + res->match_len));
update_match_offset(par, i + 1, res->match_offset,
(int)res->rep_len - (int)res->match_len);
if (res->rep_len > res->match_len) {
ad_debug("%s: Extending packet by "
"%u from %u bytes\n", __func__, res->rep_len - res->match_len, skb->len);
skb_put(skb, res->rep_len - res->match_len);
} else {
ad_debug("%s: Shrinking packet from "
"%u from %u bytes\n", __func__, res->match_len - res->rep_len, skb->len);
__skb_trim(skb, skb->len + res->rep_len - res->match_len);
}
break;
case HTTP_FILTER_TYPE_DELETE:
/* delete data is already processed by target func */
break;
case HTTP_FILTER_TYPE_INSERT:
if (res->flags & HTTP_FILTER_MATCH_INSERT_BEFORE)
offset = 0; /* insert before */
else
offset = res->match_len; /* insert after */
memmove(data + res->match_offset + res->rep_len + offset,
data + res->match_offset + offset,
skb_tail_pointer(skb) - (data + res->match_offset + offset));
memcpy(data + res->match_offset + offset, res->rep_data, res->rep_len);
ad_debug("%s: Extending packet by "
"%u from %u bytes\n", __func__, res->rep_len, skb->len);
skb_put(skb, res->rep_len);
update_match_offset(par, i + 1, res->match_offset, res->rep_len);
break;
default:
break;
}
}
/* MTU check , plus 8 for pppoe */
if (skb->len + 8 > dst_mtu(skb_dst(skb)) && iph->frag_off & htons(IP_DF))
iph->frag_off &= ~htons(IP_DF);
/* NOT think about tcp window */
/* adjust checksum .. */
iph->tot_len = htons(skb->len);
ip_send_check(iph);
tcph->check = 0;
tcph->check = tcp_v4_check(skb->len - iph->ihl*4,
iph->saddr, iph->daddr,
csum_partial((char *)tcph, skb->len - iph->ihl*4, 0));
/* seq adjust */
newlen = skb->len - iph->ihl*4 - tcph->doff*4;
if (newlen != oldlen && nfct_nat(ct)) {
nf_nat_set_seq_adjust(ct, ctinfo, tcph->seq,
(int)newlen - (int)oldlen);
clear_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
set_bit(HTTP_FILTER_SEQ_ADJUST_BIT, &ad->status);
}
next_match:
clear_bit(IGD_MATCH_URL_BW_BIT, ct_igdflag(ct));
return verdict;
drop:
verdict = NF_DROP;
dismatch:
set_bit(HTTP_FILTER_DISMATCH_BIT, &ad->status);
return verdict;
}
static bool http_pre_process(struct nf_conn *ct, struct nf_text_replace *ad,
struct nf_http_log *log, int data_len, unsigned char *data,
enum ip_conntrack_dir dir)
{
int len;
unsigned char *start;
if (test_bit(HTTP_FILTER_OWN_HOST_BIT, &ad->status))
return true;
else if (test_bit(HTTP_FILTER_MISS_HOST_BIT, &ad->status)) {
if (dir == IP_CT_DIR_REPLY)
return false;
start = strnstr(data, "Host:", data_len);
if (!start) {
start = strnstr(data, "\r\n\r", data_len);
/* miss HTTP request */
if (start)
set_bit(HTTP_FILTER_DISMATCH_BIT, &ad->status);
return false;
}
start += strlen("Host:");
len = data_len - (start - data);
while (len > 0 && isspace(*start)) {
len--;
start++;
}
if (!len) {
set_bit(HTTP_FILTER_DISMATCH_BIT, &ad->status);
return false;
}
len = min((int)len, (int)(sizeof(log->host) - 1));
spin_lock(&ct->lock);
log->host_len = __igd_strcpy_end(log->host, start, len, '\r');
spin_unlock(&ct->lock);
clear_bit(HTTP_FILTER_MISS_HOST_BIT, &ad->status);
set_bit(HTTP_FILTER_OWN_HOST_BIT, &ad->status);
return true;
}
spin_lock(&ct->lock);
if (!log->host_len) {
set_bit(HTTP_FILTER_MISS_HOST_BIT, &ad->status);
if (!log->uri_len)
copy_uri(log, data_len, data);
spin_unlock(&ct->lock);
return false;
} else
set_bit(HTTP_FILTER_OWN_HOST_BIT, &ad->status);
spin_unlock(&ct->lock);
return true;
}