forked from CyanogenDefy/android_device_motorola_jordan
/
xt_qtaguid.c
2748 lines (2462 loc) · 74.9 KB
/
xt_qtaguid.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
/*
* Kernel iptables module to track stats for packets based on user tags.
*
* (C) 2011 Google, Inc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
/*
* There are run-time debug flags enabled via the debug_mask module param, or
* via the DEFAULT_DEBUG_MASK. See xt_qtaguid_internal.h.
*/
#define DEBUG
#define TAG "qtaguid"
#include "atomic.h"
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/version.h>
#include <linux/file.h>
#include <linux/inetdevice.h>
#include <linux/netfilter/x_tables.h>
#include <linux/skbuff.h>
#include <linux/miscdevice.h>
#include <linux/workqueue.h>
#include <net/addrconf.h>
#include <net/sock.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <linux/netfilter/xt_socket.h>
#include "xt_qtaguid_internal.h"
#include "xt_qtaguid_print.h"
/* Compat */
#define pr_warn_once pr_warning
#define xt_socket_put_sk(sk) qtaguid_put_sk(sk)
/* in a separate sockets file */
struct sock *qtaguid_find_sk(const struct sk_buff *skb, const struct xt_match_param *par);
void qtaguid_put_sk(struct sock *sk);
static const char *module_procdirname = "xt_qtaguid";
static struct proc_dir_entry *xt_qtaguid_procdir;
static unsigned int proc_iface_perms = S_IRUGO;
module_param_named(iface_perms, proc_iface_perms, uint, S_IRUGO | S_IWUSR);
static struct proc_dir_entry *xt_qtaguid_stats_file;
static unsigned int proc_stats_perms = S_IRUGO;
module_param_named(stats_perms, proc_stats_perms, uint, S_IRUGO | S_IWUSR);
static struct proc_dir_entry *xt_qtaguid_ctrl_file;
#ifdef CONFIG_ANDROID_PARANOID_NETWORK
static unsigned int proc_ctrl_perms = S_IRUGO | S_IWUGO;
#else
static unsigned int proc_ctrl_perms = S_IRUGO | S_IWUSR;
#endif
module_param_named(ctrl_perms, proc_ctrl_perms, uint, S_IRUGO | S_IWUSR);
#ifdef CONFIG_ANDROID_PARANOID_NETWORK
#include <linux/android_aid.h>
static gid_t proc_stats_readall_gid = AID_NET_BW_STATS;
static gid_t proc_ctrl_write_gid = AID_NET_BW_ACCT;
#else
/* 0 means, don't limit anybody */
static gid_t proc_stats_readall_gid;
static gid_t proc_ctrl_write_gid;
#endif
module_param_named(stats_readall_gid, proc_stats_readall_gid, uint,
S_IRUGO | S_IWUSR);
module_param_named(ctrl_write_gid, proc_ctrl_write_gid, uint,
S_IRUGO | S_IWUSR);
/*
* Limit the number of active tags (via socket tags) for a given UID.
* Multiple processes could share the UID.
*/
static int max_sock_tags = DEFAULT_MAX_SOCK_TAGS;
module_param(max_sock_tags, int, S_IRUGO | S_IWUSR);
/*
* After the kernel has initiallized this module, it is still possible
* to make it passive.
* Setting passive to Y:
* - the iface stats handling will not act on notifications.
* - iptables matches will never match.
* - ctrl commands silently succeed.
* - stats are always empty.
* This is mostly usefull when a bug is suspected.
*/
static bool module_passive;
module_param_named(passive, module_passive, bool, S_IRUGO | S_IWUSR);
/*
* Control how qtaguid data is tracked per proc/uid.
* Setting tag_tracking_passive to Y:
* - don't create proc specific structs to track tags
* - don't check that active tag stats exceed some limits.
* - don't clean up socket tags on process exits.
* This is mostly usefull when a bug is suspected.
*/
static bool qtu_proc_handling_passive;
module_param_named(tag_tracking_passive, qtu_proc_handling_passive, bool,
S_IRUGO | S_IWUSR);
#define QTU_DEV_NAME "xt_qtaguid"
//uint qtaguid_debug_mask = DEFAULT_DEBUG_MASK;
module_param_named(debug_mask, qtaguid_debug_mask, uint, S_IRUGO | S_IWUSR);
/*---------------------------------------------------------------------------*/
static const char *iface_stat_procdirname = "iface_stat";
static struct proc_dir_entry *iface_stat_procdir;
static const char *iface_stat_all_procfilename = "iface_stat_all";
static struct proc_dir_entry *iface_stat_all_procfile;
/*
* Ordering of locks:
* outer locks:
* iface_stat_list_lock
* sock_tag_list_lock
* inner locks:
* uid_tag_data_tree_lock
* tag_counter_set_list_lock
* Notice how sock_tag_list_lock is held sometimes when uid_tag_data_tree_lock
* is acquired.
*
* Call tree with all lock holders as of 2011-09-25:
*
* iface_stat_all_proc_read()
* iface_stat_list_lock
* (struct iface_stat)
*
* qtaguid_ctrl_proc_read()
* sock_tag_list_lock
* (sock_tag_tree)
* (struct proc_qtu_data->sock_tag_list)
* prdebug_full_state()
* sock_tag_list_lock
* (sock_tag_tree)
* uid_tag_data_tree_lock
* (uid_tag_data_tree)
* (proc_qtu_data_tree)
* iface_stat_list_lock
*
* qtaguid_stats_proc_read()
* iface_stat_list_lock
* struct iface_stat->tag_stat_list_lock
*
* qtudev_open()
* uid_tag_data_tree_lock
*
* qtudev_release()
* sock_tag_data_list_lock
* uid_tag_data_tree_lock
* prdebug_full_state()
* sock_tag_list_lock
* uid_tag_data_tree_lock
* iface_stat_list_lock
*
* iface_netdev_event_handler()
* iface_stat_create()
* iface_stat_list_lock
* iface_stat_update()
* iface_stat_list_lock
*
* iface_inetaddr_event_handler()
* iface_stat_create()
* iface_stat_list_lock
* iface_stat_update()
* iface_stat_list_lock
*
* iface_inet6addr_event_handler()
* iface_stat_create_ipv6()
* iface_stat_list_lock
* iface_stat_update()
* iface_stat_list_lock
*
* qtaguid_mt()
* account_for_uid()
* if_tag_stat_update()
* get_sock_stat()
* sock_tag_list_lock
* struct iface_stat->tag_stat_list_lock
* tag_stat_update()
* get_active_counter_set()
* tag_counter_set_list_lock
* tag_stat_update()
* get_active_counter_set()
* tag_counter_set_list_lock
*
*
* qtaguid_ctrl_parse()
* ctrl_cmd_delete()
* sock_tag_list_lock
* tag_counter_set_list_lock
* iface_stat_list_lock
* struct iface_stat->tag_stat_list_lock
* uid_tag_data_tree_lock
* ctrl_cmd_counter_set()
* tag_counter_set_list_lock
* ctrl_cmd_tag()
* sock_tag_list_lock
* (sock_tag_tree)
* get_tag_ref()
* uid_tag_data_tree_lock
* (uid_tag_data_tree)
* uid_tag_data_tree_lock
* (proc_qtu_data_tree)
* ctrl_cmd_untag()
* sock_tag_list_lock
* uid_tag_data_tree_lock
*
*/
static LIST_HEAD(iface_stat_list);
static DEFINE_SPINLOCK(iface_stat_list_lock);
static struct rb_root sock_tag_tree = RB_ROOT;
static DEFINE_SPINLOCK(sock_tag_list_lock);
static struct rb_root tag_counter_set_tree = RB_ROOT;
static DEFINE_SPINLOCK(tag_counter_set_list_lock);
static struct rb_root uid_tag_data_tree = RB_ROOT;
static DEFINE_SPINLOCK(uid_tag_data_tree_lock);
static struct rb_root proc_qtu_data_tree = RB_ROOT;
/* No proc_qtu_data_tree_lock; use uid_tag_data_tree_lock */
static struct qtaguid_event_counts qtu_events;
/*----------------------------------------------*/
static bool can_manipulate_uids(void)
{
/* root pwnd */
return unlikely(!current_fsuid()) || unlikely(!proc_ctrl_write_gid)
|| in_egroup_p(proc_ctrl_write_gid);
}
static bool can_impersonate_uid(uid_t uid)
{
return uid == current_fsuid() || can_manipulate_uids();
}
static bool can_read_other_uid_stats(uid_t uid)
{
/* root pwnd */
return unlikely(!current_fsuid()) || uid == current_fsuid()
|| unlikely(!proc_stats_readall_gid)
|| in_egroup_p(proc_stats_readall_gid);
}
static inline void dc_add_byte_packets(struct data_counters *counters, int set,
enum ifs_tx_rx direction,
enum ifs_proto ifs_proto,
int bytes,
int packets)
{
counters->bpc[set][direction][ifs_proto].bytes += bytes;
counters->bpc[set][direction][ifs_proto].packets += packets;
}
static inline uint64_t dc_sum_bytes(struct data_counters *counters,
int set,
enum ifs_tx_rx direction)
{
return counters->bpc[set][direction][IFS_TCP].bytes
+ counters->bpc[set][direction][IFS_UDP].bytes
+ counters->bpc[set][direction][IFS_PROTO_OTHER].bytes;
}
static inline uint64_t dc_sum_packets(struct data_counters *counters,
int set,
enum ifs_tx_rx direction)
{
return counters->bpc[set][direction][IFS_TCP].packets
+ counters->bpc[set][direction][IFS_UDP].packets
+ counters->bpc[set][direction][IFS_PROTO_OTHER].packets;
}
static struct tag_node *tag_node_tree_search(struct rb_root *root, tag_t tag)
{
struct rb_node *node = root->rb_node;
while (node) {
struct tag_node *data = rb_entry(node, struct tag_node, node);
int result;
RB_DEBUG(TAG": tag_node_tree_search(0x%llx): "
" node=%p data=%p\n", tag, node, data);
result = tag_compare(tag, data->tag);
RB_DEBUG(TAG": tag_node_tree_search(0x%llx): "
" data.tag=0x%llx (uid=%u) res=%d\n",
tag, data->tag, get_uid_from_tag(data->tag), result);
if (result < 0)
node = node->rb_left;
else if (result > 0)
node = node->rb_right;
else
return data;
}
return NULL;
}
static void tag_node_tree_insert(struct tag_node *data, struct rb_root *root)
{
struct rb_node **new = &(root->rb_node), *parent = NULL;
/* Figure out where to put new node */
while (*new) {
struct tag_node *this = rb_entry(*new, struct tag_node,
node);
int result = tag_compare(data->tag, this->tag);
RB_DEBUG(TAG": %s(): tag=0x%llx"
" (uid=%u)\n", __func__,
this->tag,
get_uid_from_tag(this->tag));
parent = *new;
if (result < 0)
new = &((*new)->rb_left);
else if (result > 0)
new = &((*new)->rb_right);
else
BUG();
}
/* Add new node and rebalance tree. */
rb_link_node(&data->node, parent, new);
rb_insert_color(&data->node, root);
}
static void tag_stat_tree_insert(struct tag_stat *data, struct rb_root *root)
{
tag_node_tree_insert(&data->tn, root);
}
static struct tag_stat *tag_stat_tree_search(struct rb_root *root, tag_t tag)
{
struct tag_node *node = tag_node_tree_search(root, tag);
if (!node)
return NULL;
return rb_entry(&node->node, struct tag_stat, tn.node);
}
static void tag_counter_set_tree_insert(struct tag_counter_set *data,
struct rb_root *root)
{
tag_node_tree_insert(&data->tn, root);
}
static struct tag_counter_set *tag_counter_set_tree_search(struct rb_root *root,
tag_t tag)
{
struct tag_node *node = tag_node_tree_search(root, tag);
if (!node)
return NULL;
return rb_entry(&node->node, struct tag_counter_set, tn.node);
}
static void tag_ref_tree_insert(struct tag_ref *data, struct rb_root *root)
{
tag_node_tree_insert(&data->tn, root);
}
static struct tag_ref *tag_ref_tree_search(struct rb_root *root, tag_t tag)
{
struct tag_node *node = tag_node_tree_search(root, tag);
if (!node)
return NULL;
return rb_entry(&node->node, struct tag_ref, tn.node);
}
static struct sock_tag *sock_tag_tree_search(struct rb_root *root,
const struct sock *sk)
{
struct rb_node *node = root->rb_node;
while (node) {
struct sock_tag *data = rb_entry(node, struct sock_tag,
sock_node);
if (sk < data->sk)
node = node->rb_left;
else if (sk > data->sk)
node = node->rb_right;
else
return data;
}
return NULL;
}
static void sock_tag_tree_insert(struct sock_tag *data, struct rb_root *root)
{
struct rb_node **new = &(root->rb_node), *parent = NULL;
/* Figure out where to put new node */
while (*new) {
struct sock_tag *this = rb_entry(*new, struct sock_tag,
sock_node);
parent = *new;
if (data->sk < this->sk)
new = &((*new)->rb_left);
else if (data->sk > this->sk)
new = &((*new)->rb_right);
else
BUG();
}
/* Add new node and rebalance tree. */
rb_link_node(&data->sock_node, parent, new);
rb_insert_color(&data->sock_node, root);
}
static void sock_tag_tree_erase(struct rb_root *st_to_free_tree)
{
struct rb_node *node;
struct sock_tag *st_entry;
node = rb_first(st_to_free_tree);
while (node) {
st_entry = rb_entry(node, struct sock_tag, sock_node);
node = rb_next(node);
CT_DEBUG(TAG": %s(): "
"erase st: sk=%p tag=0x%llx (uid=%u)\n", __func__,
st_entry->sk,
st_entry->tag,
get_uid_from_tag(st_entry->tag));
rb_erase(&st_entry->sock_node, st_to_free_tree);
sockfd_put(st_entry->socket);
kfree(st_entry);
}
}
static struct proc_qtu_data *proc_qtu_data_tree_search(struct rb_root *root,
const pid_t pid)
{
struct rb_node *node = root->rb_node;
while (node) {
struct proc_qtu_data *data = rb_entry(node,
struct proc_qtu_data,
node);
if (pid < data->pid)
node = node->rb_left;
else if (pid > data->pid)
node = node->rb_right;
else
return data;
}
return NULL;
}
static void proc_qtu_data_tree_insert(struct proc_qtu_data *data,
struct rb_root *root)
{
struct rb_node **new = &(root->rb_node), *parent = NULL;
/* Figure out where to put new node */
while (*new) {
struct proc_qtu_data *this = rb_entry(*new,
struct proc_qtu_data,
node);
parent = *new;
if (data->pid < this->pid)
new = &((*new)->rb_left);
else if (data->pid > this->pid)
new = &((*new)->rb_right);
else
BUG();
}
/* Add new node and rebalance tree. */
rb_link_node(&data->node, parent, new);
rb_insert_color(&data->node, root);
}
static void uid_tag_data_tree_insert(struct uid_tag_data *data,
struct rb_root *root)
{
struct rb_node **new = &(root->rb_node), *parent = NULL;
/* Figure out where to put new node */
while (*new) {
struct uid_tag_data *this = rb_entry(*new,
struct uid_tag_data,
node);
parent = *new;
if (data->uid < this->uid)
new = &((*new)->rb_left);
else if (data->uid > this->uid)
new = &((*new)->rb_right);
else
BUG();
}
/* Add new node and rebalance tree. */
rb_link_node(&data->node, parent, new);
rb_insert_color(&data->node, root);
}
static struct uid_tag_data *uid_tag_data_tree_search(struct rb_root *root,
uid_t uid)
{
struct rb_node *node = root->rb_node;
while (node) {
struct uid_tag_data *data = rb_entry(node,
struct uid_tag_data,
node);
if (uid < data->uid)
node = node->rb_left;
else if (uid > data->uid)
node = node->rb_right;
else
return data;
}
return NULL;
}
/*
* Allocates a new uid_tag_data struct if needed.
* Returns a pointer to the found or allocated uid_tag_data.
* Returns a PTR_ERR on failures, and lock is not held.
* If found is not NULL:
* sets *found to true if not allocated.
* sets *found to false if allocated.
*/
struct uid_tag_data *get_uid_data(uid_t uid, bool *found_res)
{
struct uid_tag_data *utd_entry;
/* Look for top level uid_tag_data for the UID */
utd_entry = uid_tag_data_tree_search(&uid_tag_data_tree, uid);
DR_DEBUG(TAG": get_uid_data(%u) utd=%p\n", uid, utd_entry);
if (found_res)
*found_res = utd_entry;
if (utd_entry)
return utd_entry;
utd_entry = kzalloc(sizeof(*utd_entry), GFP_ATOMIC);
if (!utd_entry) {
pr_err(TAG": get_uid_data(%u): "
"tag data alloc failed\n", uid);
return ERR_PTR(-ENOMEM);
}
utd_entry->uid = uid;
utd_entry->tag_ref_tree = RB_ROOT;
uid_tag_data_tree_insert(utd_entry, &uid_tag_data_tree);
DR_DEBUG(TAG": get_uid_data(%u) new utd=%p\n", uid, utd_entry);
return utd_entry;
}
/* Never returns NULL. Either PTR_ERR or a valid ptr. */
static struct tag_ref *new_tag_ref(tag_t new_tag,
struct uid_tag_data *utd_entry)
{
struct tag_ref *tr_entry;
int res;
if (utd_entry->num_active_tags + 1 > max_sock_tags) {
pr_info(TAG": new_tag_ref(0x%llx): "
"tag ref alloc quota exceeded. max=%d\n",
new_tag, max_sock_tags);
res = -EMFILE;
goto err_res;
}
tr_entry = kzalloc(sizeof(*tr_entry), GFP_ATOMIC);
if (!tr_entry) {
pr_err(TAG": new_tag_ref(0x%llx): "
"tag ref alloc failed\n",
new_tag);
res = -ENOMEM;
goto err_res;
}
tr_entry->tn.tag = new_tag;
/* tr_entry->num_sock_tags handled by caller */
utd_entry->num_active_tags++;
tag_ref_tree_insert(tr_entry, &utd_entry->tag_ref_tree);
DR_DEBUG(TAG": new_tag_ref(0x%llx): "
" inserted new tag ref %p\n",
new_tag, tr_entry);
return tr_entry;
err_res:
return ERR_PTR(res);
}
static struct tag_ref *lookup_tag_ref(tag_t full_tag,
struct uid_tag_data **utd_res)
{
struct uid_tag_data *utd_entry;
struct tag_ref *tr_entry;
bool found_utd;
uid_t uid = get_uid_from_tag(full_tag);
DR_DEBUG(TAG": lookup_tag_ref(tag=0x%llx (uid=%u))\n",
full_tag, uid);
utd_entry = get_uid_data(uid, &found_utd);
if (IS_ERR_OR_NULL(utd_entry)) {
if (utd_res)
*utd_res = utd_entry;
return NULL;
}
tr_entry = tag_ref_tree_search(&utd_entry->tag_ref_tree, full_tag);
if (utd_res)
*utd_res = utd_entry;
DR_DEBUG(TAG": lookup_tag_ref(0x%llx) utd_entry=%p tr_entry=%p\n",
full_tag, utd_entry, tr_entry);
return tr_entry;
}
/* Never returns NULL. Either PTR_ERR or a valid ptr. */
static struct tag_ref *get_tag_ref(tag_t full_tag,
struct uid_tag_data **utd_res)
{
struct uid_tag_data *utd_entry;
struct tag_ref *tr_entry;
DR_DEBUG(TAG": get_tag_ref(0x%llx)\n",
full_tag);
spin_lock_bh(&uid_tag_data_tree_lock);
tr_entry = lookup_tag_ref(full_tag, &utd_entry);
BUG_ON(IS_ERR_OR_NULL(utd_entry));
if (!tr_entry)
tr_entry = new_tag_ref(full_tag, utd_entry);
spin_unlock_bh(&uid_tag_data_tree_lock);
if (utd_res)
*utd_res = utd_entry;
DR_DEBUG(TAG": get_tag_ref(0x%llx) utd=%p tr=%p\n",
full_tag, utd_entry, tr_entry);
return tr_entry;
}
/* Checks and maybe frees the UID Tag Data entry */
static void put_utd_entry(struct uid_tag_data *utd_entry)
{
/* Are we done with the UID tag data entry? */
if (RB_EMPTY_ROOT(&utd_entry->tag_ref_tree) &&
!utd_entry->num_pqd) {
DR_DEBUG(TAG": %s(): "
"erase utd_entry=%p uid=%u "
"by pid=%u tgid=%u uid=%u\n", __func__,
utd_entry, utd_entry->uid,
current->pid, current->tgid, current_fsuid());
BUG_ON(utd_entry->num_active_tags);
rb_erase(&utd_entry->node, &uid_tag_data_tree);
kfree(utd_entry);
} else {
DR_DEBUG(TAG": %s(): "
"utd_entry=%p still has %d tags %d proc_qtu_data\n",
__func__, utd_entry, utd_entry->num_active_tags,
utd_entry->num_pqd);
BUG_ON(!(utd_entry->num_active_tags ||
utd_entry->num_pqd));
}
}
/*
* If no sock_tags are using this tag_ref,
* decrements refcount of utd_entry, removes tr_entry
* from utd_entry->tag_ref_tree and frees.
*/
static void free_tag_ref_from_utd_entry(struct tag_ref *tr_entry,
struct uid_tag_data *utd_entry)
{
DR_DEBUG(TAG": %s(): %p tag=0x%llx (uid=%u)\n", __func__,
tr_entry, tr_entry->tn.tag,
get_uid_from_tag(tr_entry->tn.tag));
if (!tr_entry->num_sock_tags) {
BUG_ON(!utd_entry->num_active_tags);
utd_entry->num_active_tags--;
rb_erase(&tr_entry->tn.node, &utd_entry->tag_ref_tree);
DR_DEBUG(TAG": %s(): erased %p\n", __func__, tr_entry);
kfree(tr_entry);
}
}
static void put_tag_ref_tree(tag_t full_tag, struct uid_tag_data *utd_entry)
{
struct rb_node *node;
struct tag_ref *tr_entry;
tag_t acct_tag;
DR_DEBUG(TAG": %s(tag=0x%llx (uid=%u))\n", __func__,
full_tag, get_uid_from_tag(full_tag));
acct_tag = get_atag_from_tag(full_tag);
node = rb_first(&utd_entry->tag_ref_tree);
while (node) {
tr_entry = rb_entry(node, struct tag_ref, tn.node);
node = rb_next(node);
if (!acct_tag || tr_entry->tn.tag == full_tag)
free_tag_ref_from_utd_entry(tr_entry, utd_entry);
}
}
static int read_proc_u64(char *page, char **start, off_t off,
int count, int *eof, void *data)
{
int len;
uint64_t value;
char *p = page;
uint64_t *iface_entry = data;
if (!data)
return 0;
value = *iface_entry;
p += sprintf(p, "%llu\n", value);
len = (p - page) - off;
*eof = (len <= count) ? 1 : 0;
*start = page + off;
return len;
}
static int read_proc_bool(char *page, char **start, off_t off,
int count, int *eof, void *data)
{
int len;
bool value;
char *p = page;
bool *bool_entry = data;
if (!data)
return 0;
value = *bool_entry;
p += sprintf(p, "%u\n", value);
len = (p - page) - off;
*eof = (len <= count) ? 1 : 0;
*start = page + off;
return len;
}
static int get_active_counter_set(tag_t tag)
{
int active_set = 0;
struct tag_counter_set *tcs;
MT_DEBUG(TAG": get_active_counter_set(tag=0x%llx)"
" (uid=%u)\n",
tag, get_uid_from_tag(tag));
/* For now we only handle UID tags for active sets */
tag = get_utag_from_tag(tag);
spin_lock_bh(&tag_counter_set_list_lock);
tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
if (tcs)
active_set = tcs->active_set;
spin_unlock_bh(&tag_counter_set_list_lock);
return active_set;
}
/*
* Find the entry for tracking the specified interface.
* Caller must hold iface_stat_list_lock
*/
static struct iface_stat *get_iface_entry(const char *ifname)
{
struct iface_stat *iface_entry;
/* Find the entry for tracking the specified tag within the interface */
if (ifname == NULL) {
pr_info(TAG": iface_stat: get() NULL device name\n");
return NULL;
}
/* Iterate over interfaces */
list_for_each_entry(iface_entry, &iface_stat_list, list) {
if (!strcmp(ifname, iface_entry->ifname))
goto done;
}
iface_entry = NULL;
done:
return iface_entry;
}
static int iface_stat_all_proc_read(char *page, char **num_items_returned,
off_t items_to_skip, int char_count,
int *eof, void *data)
{
char *outp = page;
int item_index = 0;
int len;
struct iface_stat *iface_entry;
const struct net_device_stats *stats;
struct rtnl_link_stats64 no_dev_stats = {0};
if (unlikely(module_passive)) {
*eof = 1;
return 0;
}
CT_DEBUG(TAG":proc iface_stat_all "
"page=%p *num_items_returned=%p off=%ld "
"char_count=%d *eof=%d\n", page, *num_items_returned,
items_to_skip, char_count, *eof);
if (*eof)
return 0;
/*
* This lock will prevent iface_stat_update() from changing active,
* and in turn prevent an interface from unregistering itself.
*/
spin_lock_bh(&iface_stat_list_lock);
list_for_each_entry(iface_entry, &iface_stat_list, list) {
if (item_index++ < items_to_skip)
continue;
if (iface_entry->active) {
stats = dev_get_stats(iface_entry->net_dev);
} else {
stats = &no_dev_stats;
}
len = snprintf(outp, char_count,
"%s %d "
"%llu %llu %llu %llu "
"%lu %lu %lu %lu\n",
iface_entry->ifname,
iface_entry->active,
iface_entry->totals[IFS_RX].bytes,
iface_entry->totals[IFS_RX].packets,
iface_entry->totals[IFS_TX].bytes,
iface_entry->totals[IFS_TX].packets,
stats->rx_bytes, stats->rx_packets,
stats->tx_bytes, stats->tx_packets);
if (len >= char_count) {
spin_unlock_bh(&iface_stat_list_lock);
*outp = '\0';
return outp - page;
}
outp += len;
char_count -= len;
(*num_items_returned)++;
}
spin_unlock_bh(&iface_stat_list_lock);
*eof = 1;
return outp - page;
}
static void iface_create_proc_worker(struct work_struct *work)
{
struct proc_dir_entry *proc_entry;
struct iface_stat_work *isw = container_of(work, struct iface_stat_work,
iface_work);
struct iface_stat *new_iface = isw->iface_entry;
/* iface_entries are not deleted, so safe to manipulate. */
proc_entry = proc_mkdir(new_iface->ifname, iface_stat_procdir);
if (IS_ERR_OR_NULL(proc_entry)) {
pr_err(TAG": iface_stat: create_proc(): alloc failed.\n");
kfree(isw);
return;
}
new_iface->proc_ptr = proc_entry;
create_proc_read_entry("tx_bytes", proc_iface_perms, proc_entry,
read_proc_u64, &new_iface->totals[IFS_TX].bytes);
create_proc_read_entry("rx_bytes", proc_iface_perms, proc_entry,
read_proc_u64, &new_iface->totals[IFS_RX].bytes);
create_proc_read_entry("tx_packets", proc_iface_perms, proc_entry,
read_proc_u64, &new_iface->totals[IFS_TX].packets);
create_proc_read_entry("rx_packets", proc_iface_perms, proc_entry,
read_proc_u64, &new_iface->totals[IFS_RX].packets);
create_proc_read_entry("active", proc_iface_perms, proc_entry,
read_proc_bool, &new_iface->active);
IF_DEBUG(TAG": iface_stat: create_proc(): done "
"entry=%p dev=%s\n", new_iface, new_iface->ifname);
kfree(isw);
}
/*
* Will set the entry's active state, and
* update the net_dev accordingly also.
*/
static void _iface_stat_set_active(struct iface_stat *entry,
struct net_device *net_dev,
bool activate)
{
if (activate) {
entry->net_dev = net_dev;
entry->active = true;
IF_DEBUG(TAG": %s(%s): "
"enable tracking.\n", __func__,
entry->ifname);
} else {
entry->active = false;
entry->net_dev = NULL;
IF_DEBUG(TAG": %s(%s): "
"disable tracking.\n", __func__,
entry->ifname);
}
}
/* Caller must hold iface_stat_list_lock */
static struct iface_stat *iface_alloc(struct net_device *net_dev)
{
struct iface_stat *new_iface;
struct iface_stat_work *isw;
new_iface = kzalloc(sizeof(*new_iface), GFP_ATOMIC);
if (new_iface == NULL) {
pr_err(TAG": iface_stat: create(%s): "
"iface_stat alloc failed\n", net_dev->name);
return NULL;
}
new_iface->ifname = kstrdup(net_dev->name, GFP_ATOMIC);
if (new_iface->ifname == NULL) {
pr_err(TAG": iface_stat: create(%s): "
"ifname alloc failed\n", net_dev->name);
kfree(new_iface);
return NULL;
}
spin_lock_init(&new_iface->tag_stat_list_lock);
new_iface->tag_stat_tree = RB_ROOT;
_iface_stat_set_active(new_iface, net_dev, true);
/*
* ipv6 notifier chains are atomic :(
* No create_proc_read_entry() for you!
*/
isw = kmalloc(sizeof(*isw), GFP_ATOMIC);
if (!isw) {
pr_err(TAG": iface_stat: create(%s): "
"work alloc failed\n", new_iface->ifname);
_iface_stat_set_active(new_iface, net_dev, false);
kfree(new_iface->ifname);
kfree(new_iface);
return NULL;
}
isw->iface_entry = new_iface;
INIT_WORK(&isw->iface_work, iface_create_proc_worker);
schedule_work(&isw->iface_work);
list_add(&new_iface->list, &iface_stat_list);
return new_iface;
}
static void iface_check_stats_reset_and_adjust(struct net_device *net_dev,
struct iface_stat *iface)
{
const struct net_device_stats *stats;
bool stats_rewound;
stats = dev_get_stats(net_dev);
/* No empty packets */
stats_rewound =
(stats->rx_bytes < iface->last_known[IFS_RX].bytes)
|| (stats->tx_bytes < iface->last_known[IFS_TX].bytes);
IF_DEBUG(TAG": %s(%s): iface=%p netdev=%p "
"bytes rx/tx=%lu/%lu "
"active=%d last_known=%d "
"stats_rewound=%d\n", __func__,
net_dev ? net_dev->name : "?",
iface, net_dev,
stats->rx_bytes, stats->tx_bytes,
iface->active, iface->last_known_valid, stats_rewound);
if (iface->active && iface->last_known_valid && stats_rewound) {
pr_warn_once(TAG": iface_stat: %s(%s): "
"iface reset its stats unexpectedly\n", __func__,
net_dev->name);
iface->totals[IFS_TX].bytes += iface->last_known[IFS_TX].bytes;
iface->totals[IFS_TX].packets +=
iface->last_known[IFS_TX].packets;
iface->totals[IFS_RX].bytes += iface->last_known[IFS_RX].bytes;
iface->totals[IFS_RX].packets +=
iface->last_known[IFS_RX].packets;
iface->last_known_valid = false;
IF_DEBUG(TAG": %s(%s): iface=%p "
"used last known bytes rx/tx=%llu/%llu\n", __func__,
iface->ifname, iface, iface->last_known[IFS_RX].bytes,
iface->last_known[IFS_TX].bytes);
}
}
/*
* Create a new entry for tracking the specified interface.
* Do nothing if the entry already exists.
* Called when an interface is configured with a valid IP address.
*/
static void iface_stat_create(struct net_device *net_dev,
struct in_ifaddr *ifa)
{
struct in_device *in_dev = NULL;