-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.c
1240 lines (1095 loc) · 27.6 KB
/
dict.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/version.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/rhashtable.h>
#include <linux/jhash.h>
#include <linux/parser.h>
#include <linux/inet.h>
#include <net/netfilter/nf_conntrack.h>
#include "dict.h"
unsigned int dict_net_id;
struct dict_net {
struct proc_dir_entry *dict_dir;
struct rhltable hlt;
};
struct nf_conn_dict {
struct rhlist_head list;
struct nf_conn_dict_entry_elem * table;
struct nf_conn_dict_entry_elem * key;
struct nf_conn_dict_entry_elem * field;
struct nf_conn_dict_entry_elem * value;
struct rcu_head rcu_head;
};
struct dict_hash_cmp_arg {
u8 *key;
u32 key_len;
char *table;
};
static inline int dict_hash_cmp(struct rhashtable_compare_arg *arg, const void *ptr)
{
struct nf_conn_dict *dict = (struct nf_conn_dict *)ptr;
const struct dict_hash_cmp_arg *x = arg->key;
u32 key_hash, table_hash;
if(x->key_len > sizeof(u32)) {
key_hash = jhash(x->key, x->key_len, 0);
} else {
key_hash = *(u32 *)x->key;
}
if(x->table) {
table_hash = jhash(x->table, strlen(x->table), 0);
key_hash = jhash_2words(table_hash, key_hash, 0);
}
if(key_hash != dict->key->hash) {
return 1;
}
return 0;
}
static inline u32 dict_hash_obj(const void *data, u32 len, u32 seed)
{
const struct nf_conn_dict *dict = data;
return dict->key->hash;
}
static inline u32 dict_hash_key(const void *data, u32 len, u32 seed)
{
const struct dict_hash_cmp_arg *x = data;
u32 key_hash, table_hash;
if(x->key_len > sizeof(u32)) {
key_hash = jhash(x->key, x->key_len, 0);
} else {
key_hash = *(u32 *)x->key;
}
if(x->table) {
table_hash = jhash(x->table, strlen(x->table), 0);
key_hash = jhash_2words(table_hash, key_hash, 0);
}
return key_hash;
}
static const struct rhashtable_params dict_rhashtable_params = {
.head_offset = offsetof(struct nf_conn_dict, list),
.hashfn = dict_hash_key,
.obj_hashfn = dict_hash_obj,
.obj_cmpfn = dict_hash_cmp,
.automatic_shrinking = true,
};
static struct nf_conn_dict_entry_elem * alloc_dict_entry_elem(int size)
{
struct nf_conn_dict_entry_elem * new;
new = kzalloc(sizeof(*new), GFP_KERNEL);
if(!new) {
pr_err("%s: Could not allocate new dictionary entry struct\n", __func__);
goto err;
}
new->buf = kzalloc(size, GFP_KERNEL);
if(!new->buf) {
pr_err("%s: Could not allocate new string\n", __func__);
goto err_free_new;
}
return new;
err_free_new:
kfree(new);
err:
return NULL;
}
static void free_dict_entry_elem(struct nf_conn_dict_entry_elem * elem)
{
kfree(elem->buf);
kfree(elem);
}
static void __free_dict(struct nf_conn_dict * dict)
{
if(!dict)
return;
if(dict->table) {
free_dict_entry_elem(dict->table);
}
free_dict_entry_elem(dict->key);
free_dict_entry_elem(dict->field);
free_dict_entry_elem(dict->value);
kfree(dict);
}
static void dict_context_free(struct rcu_head *head)
{
struct nf_conn_dict *dict = container_of(head, struct nf_conn_dict, rcu_head);
__free_dict(dict);
}
struct nf_conn_dict * new_dict_entry(struct net *net, u8 *key, u32 key_len, char *table, seq_printfn_t key_printfn,
char * field, char * value, u32 len, seq_printfn_t printfn)
{
int ret;
struct rhlist_head * list, * tmp;
struct nf_conn_dict * dict, * temp;
struct dict_net * dn = net_generic(net, dict_net_id);
struct rhltable *hlt = &dn->hlt;
struct dict_hash_cmp_arg arg = {
.key = key,
.key_len = key_len,
.table = table,
};
dict = kzalloc(sizeof(*dict), GFP_KERNEL);
if(!dict) {
pr_err("%s: Could not allocate dict structure\n", __func__);
return NULL;
}
dict->key = alloc_dict_entry_elem(key_len);
if(!dict->key) {
pr_err("%s: Could not allocate dict key structure\n", __func__);
kfree(dict);
return NULL;
}
memcpy(dict->key->buf, key, key_len);
dict->key->len = key_len;
dict->key->printfn = key_printfn;
if(dict->key->len > sizeof(u32)) {
dict->key->hash = jhash(dict->key->buf, dict->key->len, 0);
} else {
dict->key->hash = *(u32 *)dict->key->buf;
}
if(table) {
dict->table = alloc_dict_entry_elem(strlen(table) + 1);
if(!dict->table) {
pr_err("%s: Could not allocate dict key structure\n", __func__);
kfree(dict->key);
kfree(dict);
return NULL;
}
strcpy(dict->table->buf, table);
dict->table->len = strlen(dict->table->buf);
dict->table->hash = jhash(dict->table->buf, dict->table->len, 0);
dict->key->hash = jhash_2words(dict->table->hash, dict->key->hash, 0);
}
dict->field = alloc_dict_entry_elem(strlen(field)+ 1);
if(!dict->field) {
pr_err("%s: Could not allocate dict field\n", __func__);
free_dict_entry_elem(dict->table);
kfree(dict->key);
kfree(dict);
return NULL;
}
strcpy(dict->field->buf, field);
dict->field->len = strlen(dict->field->buf);
dict->field->hash = jhash(dict->field->buf, dict->field->len, 0);
dict->value = alloc_dict_entry_elem(len + 1);
if(!dict->value) {
pr_err("%s: Could not allocate dict value\n", __func__);
free_dict_entry_elem(dict->field);
free_dict_entry_elem(dict->table);
kfree(dict->key);
kfree(dict);
return NULL;
}
memcpy(dict->value->buf, value, len);
dict->value->len = len;
dict->value->hash = jhash(dict->value->buf, dict->value->len, 0);
dict->value->printfn = printfn;
rcu_read_lock();
list = rhltable_lookup(hlt, &arg, dict_rhashtable_params);
rhl_for_each_entry_rcu(temp, tmp, list, list) {
if(dict->field->hash == temp->field->hash) {
ret = rhltable_remove(hlt, &temp->list, dict_rhashtable_params);
if(ret == -ENOENT) {
continue;
}
if(ret < 0) {
pr_err("%s: Unable to remove entry %s from hashtable: %d\n", __func__, field, ret);
continue;
}
call_rcu(&temp->rcu_head, dict_context_free);
}
}
ret = rhltable_insert_key(hlt, &arg, &dict->list, dict_rhashtable_params);
rcu_read_unlock();
if(ret < 0) {
pr_err("%s: Unable to insert dict into hashtable: %d\n", __func__, ret);
__free_dict(dict);
return NULL;
}
return dict;
}
EXPORT_SYMBOL_GPL(new_dict_entry);
static void free_dict(void *ptr, void *arg)
{
struct nf_conn_dict * dict = (struct nf_conn_dict *)ptr;
call_rcu(&dict->rcu_head, dict_context_free);
}
struct nf_conn_dict * find_conntrack_dict(struct net *net, u8 *key, u32 key_len, char *table)
{
struct dict_net *dn = net_generic(net, dict_net_id);
struct rhltable *hlt = &dn->hlt;
struct nf_conn_dict * dict;
struct rhlist_head * list;
struct dict_hash_cmp_arg arg = {
.key = key,
.key_len = key_len,
.table = table,
};
rcu_read_lock();
list = rhltable_lookup(hlt, &arg, dict_rhashtable_params);
dict = container_of(list, struct nf_conn_dict, list);
rcu_read_unlock();
return dict;
}
/* called within rcu read lock */
struct nf_conn_dict_entry_elem * find_conntrack_dict_entry(struct net *net, u8 *key, u32 key_len, char *table, char * field)
{
struct dict_net *dn = net_generic(net, dict_net_id);
struct rhltable *hlt = &dn->hlt;
struct nf_conn_dict * temp;
struct rhlist_head * tmp, * list;
struct dict_hash_cmp_arg arg = {
.key = key,
.key_len = key_len,
.table = table,
};
u32 hash = 0;
if(field) {
hash = jhash(field, strlen(field), 0);
}
list = rhltable_lookup(hlt, &arg, dict_rhashtable_params);
rhl_for_each_entry_rcu(temp, tmp, list, list) {
if(hash == temp->field->hash) {
return temp->value;
}
}
return NULL;
}
EXPORT_SYMBOL_GPL(find_conntrack_dict_entry);
void destroy_dict_entry(struct net *net, u8 *key, u32 key_len, char *table, char *field)
{
struct dict_net *dn = net_generic(net, dict_net_id);
struct rhltable *hlt = &dn->hlt;
struct nf_conn_dict * temp;
struct rhlist_head * tmp, * list;
struct dict_hash_cmp_arg arg = {
.key = key,
.key_len = key_len,
.table = table,
};
u32 hash = 0;
int ret;
if(field) {
hash = jhash(field, strlen(field), 0);
}
rcu_read_lock();
list = rhltable_lookup(hlt, &arg, dict_rhashtable_params);
rhl_for_each_entry_rcu(temp, tmp, list, list) {
if(hash == temp->field->hash) {
ret = rhltable_remove(hlt, tmp, dict_rhashtable_params);
if(ret < 0) {
pr_err("%s: Unable to remove entry from hashtable: %d\n", __func__, ret);
continue;
}
call_rcu(&temp->rcu_head, dict_context_free);
}
}
rcu_read_unlock();
return;
}
void destroy_dict(struct net *net, u8 *key, u32 key_len, char *table)
{
struct dict_net *dn = net_generic(net, dict_net_id);
struct rhltable *hlt = &dn->hlt;
struct nf_conn_dict * dict;
int ret;
rcu_read_lock();
while((dict = find_conntrack_dict(net, key, key_len, table))) {
ret = rhltable_remove(hlt, &dict->list, dict_rhashtable_params);
if(ret < 0) {
pr_err("%s: Unable to remove entry from hashtable: %d\n", __func__, ret);
continue;
}
call_rcu(&dict->rcu_head, dict_context_free);
}
rcu_read_unlock();
}
EXPORT_SYMBOL_GPL(destroy_dict);
static int show_dict(struct seq_file *m, struct nf_conn_dict *dict)
{
if(dict->table) {
seq_printf(m, "table: %s ", dict->table->buf);
} else {
seq_printf(m, "table: None ");
}
if(dict->key->printfn) {
seq_printf(m, "key_");
(dict->key->printfn)(m, dict->key->buf);
seq_printf(m, " ");
}
seq_printf(m, "field: %s ", dict->field->buf);
if(dict->value->printfn) {
(dict->value->printfn)(m, dict->value->buf);
seq_printf(m, "\n");
}
return 0;
}
static int all_dict_show(struct seq_file *m, void *v)
{
struct net *net = (struct net *)m->private;
struct dict_net *dn = net_generic(net, dict_net_id);
struct rhltable *hlt = &dn->hlt;
struct rhashtable_iter hti;
struct nf_conn_dict *dict;
rhltable_walk_enter(hlt, &hti);
rhashtable_walk_start(&hti);
while ((dict = rhashtable_walk_next(&hti)) && !IS_ERR(dict)) {
show_dict(m, dict);
}
rhashtable_walk_stop(&hti);
rhashtable_walk_exit(&hti);
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
static int all_dict_open(struct inode *inode, struct file *file)
{
return single_open_net(inode, file, all_dict_show);
}
static const struct file_operations all_dict_file_ops = {
.owner = THIS_MODULE,
.open = all_dict_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
#endif
static int write_show(struct seq_file *m, void *v)
{
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
static int write_open(struct inode *inode, struct file *file)
{
return single_open_net(inode, file, write_show);
}
#endif
enum {
Opt_key_int,
Opt_key_ip,
Opt_key_ip6,
Opt_key_mac,
Opt_key_string,
Opt_field,
Opt_string,
Opt_ip,
Opt_ip6,
Opt_mac,
Opt_bool,
Opt_int,
Opt_int64,
Opt_table,
Opt_err,
};
static const match_table_t tokens = {
{Opt_key_int, "key_int=%u"},
{Opt_key_ip, "key_ip=%s"},
{Opt_key_ip6, "key_ip6=%s"},
{Opt_key_mac, "key_mac=%s"},
{Opt_key_string, "key_string=%s"},
{Opt_field, "field=%s"},
{Opt_string, "value=%s"},
{Opt_string, "string=%s"},
{Opt_ip, "ip=%s"},
{Opt_ip6, "ip6=%s"},
{Opt_mac, "mac=%s"},
{Opt_bool, "bool=%s"},
{Opt_int, "int=%d"},
{Opt_int64, "int64=%s"},
{Opt_table, "table=%s"},
{Opt_err, NULL},
};
static int match_int64(substring_t *s, u64 *result)
{
char *buf;
int ret;
int64_t val;
size_t len = s->to - s->from;
buf = kmalloc(len + 1, GFP_KERNEL);
if (!buf)
return -ENOMEM;
memcpy(buf, s->from, len);
buf[len] = '\0';
ret = kstrtoll(buf, 0, &val);
if(!ret)
*result = val;
kfree(buf);
return ret;
}
static int match_uint(substring_t *s, unsigned int *result)
{
char *buf;
int ret;
unsigned int val;
size_t len = s->to - s->from;
buf = kmalloc(len + 1, GFP_KERNEL);
if (!buf)
return -ENOMEM;
memcpy(buf, s->from, len);
buf[len] = '\0';
ret = kstrtouint(buf, 0, &val);
if(!ret)
*result = val;
kfree(buf);
return ret;
}
void seq_print_ip(struct seq_file *m, char *buf)
{
seq_printf(m, "ip: %pI4", buf);
}
EXPORT_SYMBOL(seq_print_ip);
void seq_print_ip6(struct seq_file *m, char *buf)
{
seq_printf(m, "ip6: %pI6", buf);
}
EXPORT_SYMBOL(seq_print_ip6);
void seq_print_mac(struct seq_file *m, char *buf)
{
seq_printf(m, "mac: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
}
EXPORT_SYMBOL(seq_print_mac);
void seq_print_string(struct seq_file *m, char *buf)
{
seq_printf(m, "string: %s", buf);
}
EXPORT_SYMBOL(seq_print_string);
void seq_print_bool(struct seq_file *m, char *buf)
{
if(buf[0] == 1) {
seq_printf(m, "bool: true");
} else {
seq_printf(m, "bool: false");
}
}
EXPORT_SYMBOL(seq_print_bool);
void seq_print_integer(struct seq_file *m, char *buf)
{
uint32_t temp = *(uint32_t *)buf;
seq_printf(m, "int: %u", ntohl(temp));
}
EXPORT_SYMBOL(seq_print_integer);
void seq_print_signed_integer(struct seq_file *m, char *buf)
{
seq_printf(m, "int: %d", *(int *)buf);
}
EXPORT_SYMBOL(seq_print_signed_integer);
void seq_print_integer64(struct seq_file *m, char *buf)
{
seq_printf(m, "int64: %lld", *(int64_t *)buf);
}
EXPORT_SYMBOL(seq_print_integer64);
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
static ssize_t write_dict(struct file *file, const char __user *buf, size_t size, loff_t *pos)
#else
static int write_dict(struct file *file, char *buf, size_t size)
#endif
{
struct seq_file *m = (struct seq_file *)file->private_data;
struct net *net = (struct net *)m->private;
u8 key[128];
char *orig, *local_buf, *p, *field = NULL, *value = NULL, *ip = NULL, *table = NULL, *string_value = NULL;
unsigned int len = 0, key_len = 0, integer = 0;
int64_t integer64 = 0;
u8 addr[4] = {0,0,0,0};
u8 addr6[16] = {0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0};
u8 mac[6] = {0,0,0,0,0,0};
u8 val = 0;
substring_t args[MAX_OPT_ARGS];
seq_printfn_t printfn = NULL;
seq_printfn_t key_printfn = NULL;
memset(key, 0, sizeof(key));
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
orig = local_buf = kzalloc(size + 1, GFP_KERNEL);
if(!local_buf) {
pr_err("%s: Could not allocate local buffer!\n", __func__);
goto err;
}
if (copy_from_user(local_buf, buf, size) != 0) {
pr_err("%s: copy_from_user failed!\n", __func__);
goto free_local_buf;
}
#else
orig = local_buf = buf;
#endif
while ((p = strsep(&local_buf, ","))) {
int token;
if (!*p)
continue;
token = match_token(p, tokens, args);
switch (token) {
case Opt_key_int:
{
unsigned int temp, ret = 0;
ret = match_uint(&args[0], &temp);
if(ret) {
pr_err("%s: Opt_key_int failed %d\n", __func__, ret);
goto free_local_buf;
}
temp = htonl(temp);
key_len = sizeof(temp);
memcpy(key, &temp, key_len);
key_printfn = &seq_print_integer;
break;
}
case Opt_key_ip:
{
char *temp;
temp = match_strdup(&args[0]);
in4_pton(temp, -1, key, -1, NULL);
key_len = 4;
kfree(temp);
key_printfn = &seq_print_ip;
break;
}
case Opt_key_ip6:
{
char *temp;
temp = match_strdup(&args[0]);
in6_pton(temp, -1, key, -1, NULL);
key_len = 16;
kfree(temp);
key_printfn = &seq_print_ip6;
break;
}
case Opt_key_mac:
{
char *temp;
temp = match_strdup(&args[0]);
sscanf(temp, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &key[0], &key[1], &key[2], &key[3], &key[4], &key[5]);
key_len = 6;
kfree(temp);
key_printfn = &seq_print_mac;
break;
}
case Opt_key_string:
{
char *temp;
temp = match_strdup(&args[0]);
key_len = strlen(temp);
memcpy(key, temp, key_len);
kfree(temp);
key_printfn = &seq_print_string;
break;
}
case Opt_field:
{
field = match_strdup(&args[0]);
break;
}
case Opt_string:
{
string_value = match_strdup(&args[0]);
value = string_value;
len = strlen(value);
printfn = &seq_print_string;
break;
}
case Opt_ip:
{
ip = match_strdup(&args[0]);
in4_pton(ip, -1, addr, -1, NULL);
value = addr;
len = sizeof(addr);
printfn = &seq_print_ip;
kfree(ip);
break;
}
case Opt_ip6:
{
ip = match_strdup(&args[0]);
in6_pton(ip, -1, addr6, -1, NULL);
value = addr6;
len = sizeof(addr6);
printfn = &seq_print_ip6;
kfree(ip);
break;
}
case Opt_mac:
{
ip = match_strdup(&args[0]);
sscanf(ip, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
value = mac;
len = sizeof(mac);
printfn = &seq_print_mac;
kfree(ip);
break;
}
case Opt_bool:
{
ip = match_strdup(&args[0]);
if(!strcmp(ip, "true")) {
val = 1;
} else if (!strcmp(ip, "false")) {
val = 0;
} else {
kfree(ip);
pr_err("%s: Opt_true true or false required\n", __func__);
goto free_local_buf;
}
value = &val;
len = 1;
printfn = &seq_print_bool;
kfree(ip);
break;
}
case Opt_int:
{
int ret = 0;
ret = match_int(&args[0], &integer);
if(ret) {
pr_err("%s: Opt_int failed %d\n", __func__, ret);
goto free_local_buf;
}
value = (char *)&integer;
len = sizeof(int);
printfn = &seq_print_signed_integer;
break;
}
case Opt_int64:
{
int ret = 0;
ret = match_int64(&args[0], &integer64);
if(ret) {
pr_err("%s: Opt_int64 failed %d\n", __func__, ret);
goto free_local_buf;
}
value = (char *)&integer64;
len = sizeof(int64_t);
printfn = &seq_print_integer64;
break;
}
case Opt_table:
{
table = match_strdup(&args[0]);
break;
}
default:
pr_err("unrecognized option \"%s\" "
"or missing value\n", p);
break;
}
}
if(key_len != 0 && field && value && len != 0) {
new_dict_entry(net, key, key_len, table, key_printfn, field, value, len, printfn);
} else {
char * debug_buffer;
debug_buffer = kzalloc(size + 1, GFP_KERNEL);
if (debug_buffer && (copy_from_user(debug_buffer, buf, size) == 0)) {
pr_err("%s: Insuffient input: %s\n", __func__, debug_buffer);
} else {
pr_err("%s: Insuffient input\n", __func__);
}
if(debug_buffer) {
kfree(debug_buffer);
}
}
kfree(field);
if(string_value)
kfree(string_value);
if(table)
kfree(table);
free_local_buf:
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
kfree(orig);
err:
#endif
return size;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
static const struct file_operations write_file_ops = {
.owner = THIS_MODULE,
.open = write_open,
.read = seq_read,
.write = write_dict,
.llseek = seq_lseek,
.release = single_release,
};
#endif
static char read_key[128];
static int read_key_len = 0;
static char read_table[128];
static u32 read_field_hash = 0;
static int read_show(struct seq_file *m, void *v)
{
struct net *net = (struct net *)m->private;
struct dict_net *dn = net_generic(net, dict_net_id);
struct rhltable *hlt = &dn->hlt;
struct nf_conn_dict * temp;
struct rhlist_head * tmp, * list;
struct dict_hash_cmp_arg arg = {
.key = read_key,
.key_len = read_key_len,
.table = read_table,
};
if (read_key_len != 0) {
rcu_read_lock();
list = rhltable_lookup(hlt, &arg, dict_rhashtable_params);
rhl_for_each_entry_rcu(temp, tmp, list, list) {
if(read_field_hash != 0) {
if(read_field_hash == temp->field->hash) {
show_dict(m, temp);
}
} else {
show_dict(m, temp);
}
}
rcu_read_unlock();
} else {
struct rhashtable_iter hti;
u32 table_hash = jhash(read_table, strlen(read_table), 0);
rhltable_walk_enter(hlt, &hti);
rhashtable_walk_start(&hti);
while ((temp = rhashtable_walk_next(&hti)) && !IS_ERR(temp)) {
if(table_hash == temp->table->hash) {
show_dict(m, temp);
}
}
rhashtable_walk_stop(&hti);
rhashtable_walk_exit(&hti);
}
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
static int read_open(struct inode *inode, struct file *file)
{
return single_open_net(inode, file, read_show);
}
static ssize_t read_id_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
#else
static int read_id_write(struct file *file, char *buf, size_t size)
#endif
{
u8 key[128];
char * orig, * local_buf, * p,* table = NULL, * field = NULL;
substring_t args[MAX_OPT_ARGS];
unsigned int key_len = 0;
memset(key, 0, sizeof(key));
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
orig = local_buf = kzalloc(size + 1, GFP_KERNEL);
if(!local_buf) {
pr_err("%s: Could not allocate local buffer!\n", __func__);
goto err;
}
if (copy_from_user(local_buf, buf, size) != 0) {
pr_err("%s: copy_from_user failed!\n", __func__);
goto free_local_buf;
}
#else
orig = local_buf = buf;
#endif
while ((p = strsep(&local_buf, ","))) {
int token;
if (!*p)
continue;
token = match_token(p, tokens, args);
switch (token) {
case Opt_key_int:
{
unsigned int temp, ret = 0;
ret = match_uint(&args[0], &temp);
if(ret) {
pr_err("%s: Opt_key_int failed %d\n", __func__, ret);
goto free_local_buf;
}
temp = htonl(temp);
key_len = sizeof(temp);
memcpy(key, &temp, key_len);
break;
}
case Opt_key_ip:
{
char *temp;
temp = match_strdup(&args[0]);
in4_pton(temp, -1, key, -1, NULL);
key_len = 4;
kfree(temp);
break;
}
case Opt_key_ip6:
{
char *temp;
temp = match_strdup(&args[0]);
in6_pton(temp, -1, key, -1, NULL);
key_len = 16;
kfree(temp);
break;
}
case Opt_key_mac:
{
char *temp;
temp = match_strdup(&args[0]);
sscanf(temp, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &key[0], &key[1], &key[2], &key[3], &key[4], &key[5]);
key_len = 6;
kfree(temp);
break;
}
case Opt_key_string:
{
char *temp;
temp = match_strdup(&args[0]);
key_len = strlen(temp);
memcpy(key, temp, key_len);
kfree(temp);
break;
}
case Opt_table:
{
table = match_strdup(&args[0]);
break;
}
case Opt_field:
{
field = match_strdup(&args[0]);
break;
}
default:
pr_err("unrecognized option \"%s\" "
"or missing value\n", p);
break;
}
}
memcpy(read_key, key, key_len);
read_key_len = key_len;
strcpy(read_table, table);
if(field) {
read_field_hash = jhash(field, strlen(field), 0);
kfree(field);
} else {
read_field_hash = 0;
}
if(table)
kfree(table);
free_local_buf:
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
kfree(orig);
err:
#endif
return size;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
static const struct file_operations read_file_ops = {
.owner = THIS_MODULE,
.open = read_open,
.read = seq_read,
.write = read_id_write,
.llseek = seq_lseek,
.release = single_release,
};
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0)
static ssize_t delete_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
#else
static int delete_write(struct file *file, char *buf, size_t size)
#endif
{