blob: 1fbe4b6ad462b0234cf9072f0cfadcc18d27fe3b [file] [log] [blame]
JP Abgrallbaf0db42011-06-20 12:41:46 -07001/*
2 * Kernel iptables module to track stats for packets based on user tags.
3 *
4 * (C) 2011 Google, Inc
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11/*
12 * There are run-time debug flags enabled via the debug_mask module param, or
13 * via the DEFAULT_DEBUG_MASK. See xt_qtaguid_internal.h.
14 */
15#define DEBUG
16
17#include <linux/file.h>
18#include <linux/inetdevice.h>
19#include <linux/module.h>
Amit Pundir6e2b4052015-07-07 00:28:49 +053020#include <linux/miscdevice.h>
JP Abgrallbaf0db42011-06-20 12:41:46 -070021#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter/xt_qtaguid.h>
JP Abgralla2e371b2013-04-08 15:09:26 -070023#include <linux/ratelimit.h>
Arve Hjønnevåg287076e2013-05-13 20:45:02 -070024#include <linux/seq_file.h>
JP Abgrallbaf0db42011-06-20 12:41:46 -070025#include <linux/skbuff.h>
26#include <linux/workqueue.h>
27#include <net/addrconf.h>
28#include <net/sock.h>
29#include <net/tcp.h>
30#include <net/udp.h>
31
JP Abgrall4bb20aa2012-04-17 16:00:07 -070032#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
33#include <linux/netfilter_ipv6/ip6_tables.h>
34#endif
35
JP Abgrallbaf0db42011-06-20 12:41:46 -070036#include <linux/netfilter/xt_socket.h>
37#include "xt_qtaguid_internal.h"
38#include "xt_qtaguid_print.h"
Arve Hjønnevåg287076e2013-05-13 20:45:02 -070039#include "../../fs/proc/internal.h"
JP Abgrallbaf0db42011-06-20 12:41:46 -070040
41/*
42 * We only use the xt_socket funcs within a similar context to avoid unexpected
43 * return values.
44 */
45#define XT_SOCKET_SUPPORTED_HOOKS \
46 ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_IN))
47
48
49static const char *module_procdirname = "xt_qtaguid";
50static struct proc_dir_entry *xt_qtaguid_procdir;
51
52static unsigned int proc_iface_perms = S_IRUGO;
53module_param_named(iface_perms, proc_iface_perms, uint, S_IRUGO | S_IWUSR);
54
55static struct proc_dir_entry *xt_qtaguid_stats_file;
56static unsigned int proc_stats_perms = S_IRUGO;
57module_param_named(stats_perms, proc_stats_perms, uint, S_IRUGO | S_IWUSR);
58
59static struct proc_dir_entry *xt_qtaguid_ctrl_file;
JP Abgrall90414bc2013-01-04 18:18:36 -080060
61/* Everybody can write. But proc_ctrl_write_limited is true by default which
62 * limits what can be controlled. See the can_*() functions.
63 */
JP Abgrallbaf0db42011-06-20 12:41:46 -070064static unsigned int proc_ctrl_perms = S_IRUGO | S_IWUGO;
JP Abgrallbaf0db42011-06-20 12:41:46 -070065module_param_named(ctrl_perms, proc_ctrl_perms, uint, S_IRUGO | S_IWUSR);
66
JP Abgrall90414bc2013-01-04 18:18:36 -080067/* Limited by default, so the gid of the ctrl and stats proc entries
68 * will limit what can be done. See the can_*() functions.
69 */
70static bool proc_stats_readall_limited = true;
71static bool proc_ctrl_write_limited = true;
72
73module_param_named(stats_readall_limited, proc_stats_readall_limited, bool,
JP Abgrallbaf0db42011-06-20 12:41:46 -070074 S_IRUGO | S_IWUSR);
JP Abgrall90414bc2013-01-04 18:18:36 -080075module_param_named(ctrl_write_limited, proc_ctrl_write_limited, bool,
JP Abgrallbaf0db42011-06-20 12:41:46 -070076 S_IRUGO | S_IWUSR);
77
78/*
79 * Limit the number of active tags (via socket tags) for a given UID.
80 * Multiple processes could share the UID.
81 */
82static int max_sock_tags = DEFAULT_MAX_SOCK_TAGS;
83module_param(max_sock_tags, int, S_IRUGO | S_IWUSR);
84
85/*
86 * After the kernel has initiallized this module, it is still possible
87 * to make it passive.
88 * Setting passive to Y:
89 * - the iface stats handling will not act on notifications.
90 * - iptables matches will never match.
91 * - ctrl commands silently succeed.
92 * - stats are always empty.
93 * This is mostly usefull when a bug is suspected.
94 */
95static bool module_passive;
96module_param_named(passive, module_passive, bool, S_IRUGO | S_IWUSR);
97
98/*
99 * Control how qtaguid data is tracked per proc/uid.
100 * Setting tag_tracking_passive to Y:
101 * - don't create proc specific structs to track tags
102 * - don't check that active tag stats exceed some limits.
103 * - don't clean up socket tags on process exits.
104 * This is mostly usefull when a bug is suspected.
105 */
106static bool qtu_proc_handling_passive;
107module_param_named(tag_tracking_passive, qtu_proc_handling_passive, bool,
108 S_IRUGO | S_IWUSR);
109
110#define QTU_DEV_NAME "xt_qtaguid"
111
112uint qtaguid_debug_mask = DEFAULT_DEBUG_MASK;
113module_param_named(debug_mask, qtaguid_debug_mask, uint, S_IRUGO | S_IWUSR);
114
115/*---------------------------------------------------------------------------*/
116static const char *iface_stat_procdirname = "iface_stat";
117static struct proc_dir_entry *iface_stat_procdir;
JP Abgrall9e0858c2012-04-27 12:57:39 -0700118/*
119 * The iface_stat_all* will go away once userspace gets use to the new fields
120 * that have a format line.
121 */
JP Abgrallbaf0db42011-06-20 12:41:46 -0700122static const char *iface_stat_all_procfilename = "iface_stat_all";
123static struct proc_dir_entry *iface_stat_all_procfile;
JP Abgrall9e0858c2012-04-27 12:57:39 -0700124static const char *iface_stat_fmt_procfilename = "iface_stat_fmt";
125static struct proc_dir_entry *iface_stat_fmt_procfile;
126
JP Abgrallbaf0db42011-06-20 12:41:46 -0700127
JP Abgrallbaf0db42011-06-20 12:41:46 -0700128static LIST_HEAD(iface_stat_list);
129static DEFINE_SPINLOCK(iface_stat_list_lock);
130
131static struct rb_root sock_tag_tree = RB_ROOT;
132static DEFINE_SPINLOCK(sock_tag_list_lock);
133
134static struct rb_root tag_counter_set_tree = RB_ROOT;
135static DEFINE_SPINLOCK(tag_counter_set_list_lock);
136
137static struct rb_root uid_tag_data_tree = RB_ROOT;
138static DEFINE_SPINLOCK(uid_tag_data_tree_lock);
139
140static struct rb_root proc_qtu_data_tree = RB_ROOT;
141/* No proc_qtu_data_tree_lock; use uid_tag_data_tree_lock */
142
143static struct qtaguid_event_counts qtu_events;
144/*----------------------------------------------*/
145static bool can_manipulate_uids(void)
146{
147 /* root pwnd */
JP Abgrall90414bc2013-01-04 18:18:36 -0800148 return in_egroup_p(xt_qtaguid_ctrl_file->gid)
John Stultzbd1bca42014-03-28 16:23:48 -0700149 || unlikely(!from_kuid(&init_user_ns, current_fsuid())) || unlikely(!proc_ctrl_write_limited)
150 || unlikely(uid_eq(current_fsuid(), xt_qtaguid_ctrl_file->uid));
JP Abgrallbaf0db42011-06-20 12:41:46 -0700151}
152
John Stultzbd1bca42014-03-28 16:23:48 -0700153static bool can_impersonate_uid(kuid_t uid)
JP Abgrallbaf0db42011-06-20 12:41:46 -0700154{
John Stultzbd1bca42014-03-28 16:23:48 -0700155 return uid_eq(uid, current_fsuid()) || can_manipulate_uids();
JP Abgrallbaf0db42011-06-20 12:41:46 -0700156}
157
John Stultzbd1bca42014-03-28 16:23:48 -0700158static bool can_read_other_uid_stats(kuid_t uid)
JP Abgrallbaf0db42011-06-20 12:41:46 -0700159{
160 /* root pwnd */
JP Abgrall90414bc2013-01-04 18:18:36 -0800161 return in_egroup_p(xt_qtaguid_stats_file->gid)
John Stultzbd1bca42014-03-28 16:23:48 -0700162 || unlikely(!from_kuid(&init_user_ns, current_fsuid())) || uid_eq(uid, current_fsuid())
JP Abgrall90414bc2013-01-04 18:18:36 -0800163 || unlikely(!proc_stats_readall_limited)
John Stultzbd1bca42014-03-28 16:23:48 -0700164 || unlikely(uid_eq(current_fsuid(), xt_qtaguid_ctrl_file->uid));
JP Abgrallbaf0db42011-06-20 12:41:46 -0700165}
166
167static inline void dc_add_byte_packets(struct data_counters *counters, int set,
168 enum ifs_tx_rx direction,
169 enum ifs_proto ifs_proto,
170 int bytes,
171 int packets)
172{
173 counters->bpc[set][direction][ifs_proto].bytes += bytes;
174 counters->bpc[set][direction][ifs_proto].packets += packets;
175}
176
JP Abgrallbaf0db42011-06-20 12:41:46 -0700177static struct tag_node *tag_node_tree_search(struct rb_root *root, tag_t tag)
178{
179 struct rb_node *node = root->rb_node;
180
181 while (node) {
182 struct tag_node *data = rb_entry(node, struct tag_node, node);
183 int result;
184 RB_DEBUG("qtaguid: tag_node_tree_search(0x%llx): "
185 " node=%p data=%p\n", tag, node, data);
186 result = tag_compare(tag, data->tag);
187 RB_DEBUG("qtaguid: tag_node_tree_search(0x%llx): "
188 " data.tag=0x%llx (uid=%u) res=%d\n",
189 tag, data->tag, get_uid_from_tag(data->tag), result);
190 if (result < 0)
191 node = node->rb_left;
192 else if (result > 0)
193 node = node->rb_right;
194 else
195 return data;
196 }
197 return NULL;
198}
199
200static void tag_node_tree_insert(struct tag_node *data, struct rb_root *root)
201{
202 struct rb_node **new = &(root->rb_node), *parent = NULL;
203
204 /* Figure out where to put new node */
205 while (*new) {
206 struct tag_node *this = rb_entry(*new, struct tag_node,
207 node);
208 int result = tag_compare(data->tag, this->tag);
209 RB_DEBUG("qtaguid: %s(): tag=0x%llx"
210 " (uid=%u)\n", __func__,
211 this->tag,
212 get_uid_from_tag(this->tag));
213 parent = *new;
214 if (result < 0)
215 new = &((*new)->rb_left);
216 else if (result > 0)
217 new = &((*new)->rb_right);
218 else
219 BUG();
220 }
221
222 /* Add new node and rebalance tree. */
223 rb_link_node(&data->node, parent, new);
224 rb_insert_color(&data->node, root);
225}
226
227static void tag_stat_tree_insert(struct tag_stat *data, struct rb_root *root)
228{
229 tag_node_tree_insert(&data->tn, root);
230}
231
232static struct tag_stat *tag_stat_tree_search(struct rb_root *root, tag_t tag)
233{
234 struct tag_node *node = tag_node_tree_search(root, tag);
235 if (!node)
236 return NULL;
237 return rb_entry(&node->node, struct tag_stat, tn.node);
238}
239
240static void tag_counter_set_tree_insert(struct tag_counter_set *data,
241 struct rb_root *root)
242{
243 tag_node_tree_insert(&data->tn, root);
244}
245
246static struct tag_counter_set *tag_counter_set_tree_search(struct rb_root *root,
247 tag_t tag)
248{
249 struct tag_node *node = tag_node_tree_search(root, tag);
250 if (!node)
251 return NULL;
252 return rb_entry(&node->node, struct tag_counter_set, tn.node);
253
254}
255
256static void tag_ref_tree_insert(struct tag_ref *data, struct rb_root *root)
257{
258 tag_node_tree_insert(&data->tn, root);
259}
260
261static struct tag_ref *tag_ref_tree_search(struct rb_root *root, tag_t tag)
262{
263 struct tag_node *node = tag_node_tree_search(root, tag);
264 if (!node)
265 return NULL;
266 return rb_entry(&node->node, struct tag_ref, tn.node);
267}
268
269static struct sock_tag *sock_tag_tree_search(struct rb_root *root,
270 const struct sock *sk)
271{
272 struct rb_node *node = root->rb_node;
273
274 while (node) {
275 struct sock_tag *data = rb_entry(node, struct sock_tag,
276 sock_node);
277 if (sk < data->sk)
278 node = node->rb_left;
279 else if (sk > data->sk)
280 node = node->rb_right;
281 else
282 return data;
283 }
284 return NULL;
285}
286
287static void sock_tag_tree_insert(struct sock_tag *data, struct rb_root *root)
288{
289 struct rb_node **new = &(root->rb_node), *parent = NULL;
290
291 /* Figure out where to put new node */
292 while (*new) {
293 struct sock_tag *this = rb_entry(*new, struct sock_tag,
294 sock_node);
295 parent = *new;
296 if (data->sk < this->sk)
297 new = &((*new)->rb_left);
298 else if (data->sk > this->sk)
299 new = &((*new)->rb_right);
300 else
301 BUG();
302 }
303
304 /* Add new node and rebalance tree. */
305 rb_link_node(&data->sock_node, parent, new);
306 rb_insert_color(&data->sock_node, root);
307}
308
309static void sock_tag_tree_erase(struct rb_root *st_to_free_tree)
310{
311 struct rb_node *node;
312 struct sock_tag *st_entry;
313
314 node = rb_first(st_to_free_tree);
315 while (node) {
316 st_entry = rb_entry(node, struct sock_tag, sock_node);
317 node = rb_next(node);
318 CT_DEBUG("qtaguid: %s(): "
319 "erase st: sk=%p tag=0x%llx (uid=%u)\n", __func__,
320 st_entry->sk,
321 st_entry->tag,
322 get_uid_from_tag(st_entry->tag));
323 rb_erase(&st_entry->sock_node, st_to_free_tree);
Chenbo Feng875e5262017-04-19 14:22:47 -0700324 sock_put(st_entry->sk);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700325 kfree(st_entry);
326 }
327}
328
329static struct proc_qtu_data *proc_qtu_data_tree_search(struct rb_root *root,
330 const pid_t pid)
331{
332 struct rb_node *node = root->rb_node;
333
334 while (node) {
335 struct proc_qtu_data *data = rb_entry(node,
336 struct proc_qtu_data,
337 node);
338 if (pid < data->pid)
339 node = node->rb_left;
340 else if (pid > data->pid)
341 node = node->rb_right;
342 else
343 return data;
344 }
345 return NULL;
346}
347
348static void proc_qtu_data_tree_insert(struct proc_qtu_data *data,
349 struct rb_root *root)
350{
351 struct rb_node **new = &(root->rb_node), *parent = NULL;
352
353 /* Figure out where to put new node */
354 while (*new) {
355 struct proc_qtu_data *this = rb_entry(*new,
356 struct proc_qtu_data,
357 node);
358 parent = *new;
359 if (data->pid < this->pid)
360 new = &((*new)->rb_left);
361 else if (data->pid > this->pid)
362 new = &((*new)->rb_right);
363 else
364 BUG();
365 }
366
367 /* Add new node and rebalance tree. */
368 rb_link_node(&data->node, parent, new);
369 rb_insert_color(&data->node, root);
370}
371
372static void uid_tag_data_tree_insert(struct uid_tag_data *data,
373 struct rb_root *root)
374{
375 struct rb_node **new = &(root->rb_node), *parent = NULL;
376
377 /* Figure out where to put new node */
378 while (*new) {
379 struct uid_tag_data *this = rb_entry(*new,
380 struct uid_tag_data,
381 node);
382 parent = *new;
383 if (data->uid < this->uid)
384 new = &((*new)->rb_left);
385 else if (data->uid > this->uid)
386 new = &((*new)->rb_right);
387 else
388 BUG();
389 }
390
391 /* Add new node and rebalance tree. */
392 rb_link_node(&data->node, parent, new);
393 rb_insert_color(&data->node, root);
394}
395
396static struct uid_tag_data *uid_tag_data_tree_search(struct rb_root *root,
397 uid_t uid)
398{
399 struct rb_node *node = root->rb_node;
400
401 while (node) {
402 struct uid_tag_data *data = rb_entry(node,
403 struct uid_tag_data,
404 node);
405 if (uid < data->uid)
406 node = node->rb_left;
407 else if (uid > data->uid)
408 node = node->rb_right;
409 else
410 return data;
411 }
412 return NULL;
413}
414
415/*
416 * Allocates a new uid_tag_data struct if needed.
417 * Returns a pointer to the found or allocated uid_tag_data.
418 * Returns a PTR_ERR on failures, and lock is not held.
419 * If found is not NULL:
420 * sets *found to true if not allocated.
421 * sets *found to false if allocated.
422 */
423struct uid_tag_data *get_uid_data(uid_t uid, bool *found_res)
424{
425 struct uid_tag_data *utd_entry;
426
427 /* Look for top level uid_tag_data for the UID */
428 utd_entry = uid_tag_data_tree_search(&uid_tag_data_tree, uid);
429 DR_DEBUG("qtaguid: get_uid_data(%u) utd=%p\n", uid, utd_entry);
430
431 if (found_res)
432 *found_res = utd_entry;
433 if (utd_entry)
434 return utd_entry;
435
436 utd_entry = kzalloc(sizeof(*utd_entry), GFP_ATOMIC);
437 if (!utd_entry) {
438 pr_err("qtaguid: get_uid_data(%u): "
439 "tag data alloc failed\n", uid);
440 return ERR_PTR(-ENOMEM);
441 }
442
443 utd_entry->uid = uid;
444 utd_entry->tag_ref_tree = RB_ROOT;
445 uid_tag_data_tree_insert(utd_entry, &uid_tag_data_tree);
446 DR_DEBUG("qtaguid: get_uid_data(%u) new utd=%p\n", uid, utd_entry);
447 return utd_entry;
448}
449
450/* Never returns NULL. Either PTR_ERR or a valid ptr. */
451static struct tag_ref *new_tag_ref(tag_t new_tag,
452 struct uid_tag_data *utd_entry)
453{
454 struct tag_ref *tr_entry;
455 int res;
456
457 if (utd_entry->num_active_tags + 1 > max_sock_tags) {
458 pr_info("qtaguid: new_tag_ref(0x%llx): "
459 "tag ref alloc quota exceeded. max=%d\n",
460 new_tag, max_sock_tags);
461 res = -EMFILE;
462 goto err_res;
463
464 }
465
466 tr_entry = kzalloc(sizeof(*tr_entry), GFP_ATOMIC);
467 if (!tr_entry) {
468 pr_err("qtaguid: new_tag_ref(0x%llx): "
469 "tag ref alloc failed\n",
470 new_tag);
471 res = -ENOMEM;
472 goto err_res;
473 }
474 tr_entry->tn.tag = new_tag;
475 /* tr_entry->num_sock_tags handled by caller */
476 utd_entry->num_active_tags++;
477 tag_ref_tree_insert(tr_entry, &utd_entry->tag_ref_tree);
478 DR_DEBUG("qtaguid: new_tag_ref(0x%llx): "
479 " inserted new tag ref %p\n",
480 new_tag, tr_entry);
481 return tr_entry;
482
483err_res:
484 return ERR_PTR(res);
485}
486
487static struct tag_ref *lookup_tag_ref(tag_t full_tag,
488 struct uid_tag_data **utd_res)
489{
490 struct uid_tag_data *utd_entry;
491 struct tag_ref *tr_entry;
492 bool found_utd;
493 uid_t uid = get_uid_from_tag(full_tag);
494
495 DR_DEBUG("qtaguid: lookup_tag_ref(tag=0x%llx (uid=%u))\n",
496 full_tag, uid);
497
498 utd_entry = get_uid_data(uid, &found_utd);
499 if (IS_ERR_OR_NULL(utd_entry)) {
500 if (utd_res)
501 *utd_res = utd_entry;
502 return NULL;
503 }
504
505 tr_entry = tag_ref_tree_search(&utd_entry->tag_ref_tree, full_tag);
506 if (utd_res)
507 *utd_res = utd_entry;
508 DR_DEBUG("qtaguid: lookup_tag_ref(0x%llx) utd_entry=%p tr_entry=%p\n",
509 full_tag, utd_entry, tr_entry);
510 return tr_entry;
511}
512
513/* Never returns NULL. Either PTR_ERR or a valid ptr. */
514static struct tag_ref *get_tag_ref(tag_t full_tag,
515 struct uid_tag_data **utd_res)
516{
517 struct uid_tag_data *utd_entry;
518 struct tag_ref *tr_entry;
519
520 DR_DEBUG("qtaguid: get_tag_ref(0x%llx)\n",
521 full_tag);
522 spin_lock_bh(&uid_tag_data_tree_lock);
523 tr_entry = lookup_tag_ref(full_tag, &utd_entry);
524 BUG_ON(IS_ERR_OR_NULL(utd_entry));
525 if (!tr_entry)
526 tr_entry = new_tag_ref(full_tag, utd_entry);
527
528 spin_unlock_bh(&uid_tag_data_tree_lock);
529 if (utd_res)
530 *utd_res = utd_entry;
531 DR_DEBUG("qtaguid: get_tag_ref(0x%llx) utd=%p tr=%p\n",
532 full_tag, utd_entry, tr_entry);
533 return tr_entry;
534}
535
536/* Checks and maybe frees the UID Tag Data entry */
537static void put_utd_entry(struct uid_tag_data *utd_entry)
538{
539 /* Are we done with the UID tag data entry? */
540 if (RB_EMPTY_ROOT(&utd_entry->tag_ref_tree) &&
541 !utd_entry->num_pqd) {
542 DR_DEBUG("qtaguid: %s(): "
543 "erase utd_entry=%p uid=%u "
544 "by pid=%u tgid=%u uid=%u\n", __func__,
545 utd_entry, utd_entry->uid,
John Stultzbd1bca42014-03-28 16:23:48 -0700546 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -0700547 BUG_ON(utd_entry->num_active_tags);
548 rb_erase(&utd_entry->node, &uid_tag_data_tree);
549 kfree(utd_entry);
550 } else {
551 DR_DEBUG("qtaguid: %s(): "
552 "utd_entry=%p still has %d tags %d proc_qtu_data\n",
553 __func__, utd_entry, utd_entry->num_active_tags,
554 utd_entry->num_pqd);
555 BUG_ON(!(utd_entry->num_active_tags ||
556 utd_entry->num_pqd));
557 }
558}
559
560/*
561 * If no sock_tags are using this tag_ref,
562 * decrements refcount of utd_entry, removes tr_entry
563 * from utd_entry->tag_ref_tree and frees.
564 */
565static void free_tag_ref_from_utd_entry(struct tag_ref *tr_entry,
566 struct uid_tag_data *utd_entry)
567{
568 DR_DEBUG("qtaguid: %s(): %p tag=0x%llx (uid=%u)\n", __func__,
569 tr_entry, tr_entry->tn.tag,
570 get_uid_from_tag(tr_entry->tn.tag));
571 if (!tr_entry->num_sock_tags) {
572 BUG_ON(!utd_entry->num_active_tags);
573 utd_entry->num_active_tags--;
574 rb_erase(&tr_entry->tn.node, &utd_entry->tag_ref_tree);
575 DR_DEBUG("qtaguid: %s(): erased %p\n", __func__, tr_entry);
576 kfree(tr_entry);
577 }
578}
579
580static void put_tag_ref_tree(tag_t full_tag, struct uid_tag_data *utd_entry)
581{
582 struct rb_node *node;
583 struct tag_ref *tr_entry;
584 tag_t acct_tag;
585
586 DR_DEBUG("qtaguid: %s(tag=0x%llx (uid=%u))\n", __func__,
587 full_tag, get_uid_from_tag(full_tag));
588 acct_tag = get_atag_from_tag(full_tag);
589 node = rb_first(&utd_entry->tag_ref_tree);
590 while (node) {
591 tr_entry = rb_entry(node, struct tag_ref, tn.node);
592 node = rb_next(node);
593 if (!acct_tag || tr_entry->tn.tag == full_tag)
594 free_tag_ref_from_utd_entry(tr_entry, utd_entry);
595 }
596}
597
Greg Hackmann85a2eb52014-02-24 09:39:46 -0800598static ssize_t read_proc_u64(struct file *file, char __user *buf,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700599 size_t size, loff_t *ppos)
JP Abgrallbaf0db42011-06-20 12:41:46 -0700600{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700601 uint64_t *valuep = PDE_DATA(file_inode(file));
602 char tmp[24];
603 size_t tmp_size;
JP Abgrallbaf0db42011-06-20 12:41:46 -0700604
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700605 tmp_size = scnprintf(tmp, sizeof(tmp), "%llu\n", *valuep);
606 return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700607}
608
Greg Hackmann85a2eb52014-02-24 09:39:46 -0800609static ssize_t read_proc_bool(struct file *file, char __user *buf,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700610 size_t size, loff_t *ppos)
JP Abgrallbaf0db42011-06-20 12:41:46 -0700611{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700612 bool *valuep = PDE_DATA(file_inode(file));
613 char tmp[24];
614 size_t tmp_size;
JP Abgrallbaf0db42011-06-20 12:41:46 -0700615
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700616 tmp_size = scnprintf(tmp, sizeof(tmp), "%u\n", *valuep);
617 return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700618}
619
620static int get_active_counter_set(tag_t tag)
621{
622 int active_set = 0;
623 struct tag_counter_set *tcs;
624
625 MT_DEBUG("qtaguid: get_active_counter_set(tag=0x%llx)"
626 " (uid=%u)\n",
627 tag, get_uid_from_tag(tag));
628 /* For now we only handle UID tags for active sets */
629 tag = get_utag_from_tag(tag);
630 spin_lock_bh(&tag_counter_set_list_lock);
631 tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
632 if (tcs)
633 active_set = tcs->active_set;
634 spin_unlock_bh(&tag_counter_set_list_lock);
635 return active_set;
636}
637
638/*
639 * Find the entry for tracking the specified interface.
640 * Caller must hold iface_stat_list_lock
641 */
642static struct iface_stat *get_iface_entry(const char *ifname)
643{
644 struct iface_stat *iface_entry;
645
646 /* Find the entry for tracking the specified tag within the interface */
647 if (ifname == NULL) {
648 pr_info("qtaguid: iface_stat: get() NULL device name\n");
649 return NULL;
650 }
651
652 /* Iterate over interfaces */
653 list_for_each_entry(iface_entry, &iface_stat_list, list) {
654 if (!strcmp(ifname, iface_entry->ifname))
655 goto done;
656 }
657 iface_entry = NULL;
658done:
659 return iface_entry;
660}
661
JP Abgrall87f93e82013-01-28 16:50:44 -0800662/* This is for fmt2 only */
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700663static void pp_iface_stat_header(struct seq_file *m)
JP Abgrall87f93e82013-01-28 16:50:44 -0800664{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700665 seq_puts(m,
666 "ifname "
667 "total_skb_rx_bytes total_skb_rx_packets "
668 "total_skb_tx_bytes total_skb_tx_packets "
669 "rx_tcp_bytes rx_tcp_packets "
670 "rx_udp_bytes rx_udp_packets "
671 "rx_other_bytes rx_other_packets "
672 "tx_tcp_bytes tx_tcp_packets "
673 "tx_udp_bytes tx_udp_packets "
674 "tx_other_bytes tx_other_packets\n"
675 );
JP Abgrall87f93e82013-01-28 16:50:44 -0800676}
677
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700678static void pp_iface_stat_line(struct seq_file *m,
679 struct iface_stat *iface_entry)
JP Abgrallbaf0db42011-06-20 12:41:46 -0700680{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700681 struct data_counters *cnts;
682 int cnt_set = 0; /* We only use one set for the device */
683 cnts = &iface_entry->totals_via_skb;
684 seq_printf(m, "%s %llu %llu %llu %llu %llu %llu %llu %llu "
685 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
686 iface_entry->ifname,
687 dc_sum_bytes(cnts, cnt_set, IFS_RX),
688 dc_sum_packets(cnts, cnt_set, IFS_RX),
689 dc_sum_bytes(cnts, cnt_set, IFS_TX),
690 dc_sum_packets(cnts, cnt_set, IFS_TX),
691 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes,
692 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets,
693 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes,
694 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets,
695 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes,
696 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets,
697 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes,
698 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets,
699 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes,
700 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets,
701 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes,
702 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets);
703}
JP Abgrallbaf0db42011-06-20 12:41:46 -0700704
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700705struct proc_iface_stat_fmt_info {
706 int fmt;
707};
JP Abgrallbaf0db42011-06-20 12:41:46 -0700708
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700709static void *iface_stat_fmt_proc_start(struct seq_file *m, loff_t *pos)
710{
711 struct proc_iface_stat_fmt_info *p = m->private;
712 loff_t n = *pos;
JP Abgrall9e0858c2012-04-27 12:57:39 -0700713
JP Abgrallbaf0db42011-06-20 12:41:46 -0700714 /*
715 * This lock will prevent iface_stat_update() from changing active,
716 * and in turn prevent an interface from unregistering itself.
717 */
718 spin_lock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700719
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700720 if (unlikely(module_passive))
721 return NULL;
JP Abgrallbaf0db42011-06-20 12:41:46 -0700722
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700723 if (!n && p->fmt == 2)
724 pp_iface_stat_header(m);
725
726 return seq_list_start(&iface_stat_list, n);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700727}
728
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700729static void *iface_stat_fmt_proc_next(struct seq_file *m, void *p, loff_t *pos)
730{
731 return seq_list_next(p, &iface_stat_list, pos);
732}
733
734static void iface_stat_fmt_proc_stop(struct seq_file *m, void *p)
735{
736 spin_unlock_bh(&iface_stat_list_lock);
737}
738
739static int iface_stat_fmt_proc_show(struct seq_file *m, void *v)
740{
741 struct proc_iface_stat_fmt_info *p = m->private;
742 struct iface_stat *iface_entry;
743 struct rtnl_link_stats64 dev_stats, *stats;
744 struct rtnl_link_stats64 no_dev_stats = {0};
745
746
747 CT_DEBUG("qtaguid:proc iface_stat_fmt pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -0700748 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700749
750 iface_entry = list_entry(v, struct iface_stat, list);
751
752 if (iface_entry->active) {
753 stats = dev_get_stats(iface_entry->net_dev,
754 &dev_stats);
755 } else {
756 stats = &no_dev_stats;
757 }
758 /*
759 * If the meaning of the data changes, then update the fmtX
760 * string.
761 */
762 if (p->fmt == 1) {
763 seq_printf(m, "%s %d %llu %llu %llu %llu %llu %llu %llu %llu\n",
764 iface_entry->ifname,
765 iface_entry->active,
766 iface_entry->totals_via_dev[IFS_RX].bytes,
767 iface_entry->totals_via_dev[IFS_RX].packets,
768 iface_entry->totals_via_dev[IFS_TX].bytes,
769 iface_entry->totals_via_dev[IFS_TX].packets,
770 stats->rx_bytes, stats->rx_packets,
771 stats->tx_bytes, stats->tx_packets
772 );
773 } else {
774 pp_iface_stat_line(m, iface_entry);
775 }
776 return 0;
777}
778
779static const struct file_operations read_u64_fops = {
780 .read = read_proc_u64,
781 .llseek = default_llseek,
782};
783
784static const struct file_operations read_bool_fops = {
785 .read = read_proc_bool,
786 .llseek = default_llseek,
787};
788
JP Abgrallbaf0db42011-06-20 12:41:46 -0700789static void iface_create_proc_worker(struct work_struct *work)
790{
791 struct proc_dir_entry *proc_entry;
792 struct iface_stat_work *isw = container_of(work, struct iface_stat_work,
793 iface_work);
794 struct iface_stat *new_iface = isw->iface_entry;
795
796 /* iface_entries are not deleted, so safe to manipulate. */
797 proc_entry = proc_mkdir(new_iface->ifname, iface_stat_procdir);
798 if (IS_ERR_OR_NULL(proc_entry)) {
799 pr_err("qtaguid: iface_stat: create_proc(): alloc failed.\n");
800 kfree(isw);
801 return;
802 }
803
804 new_iface->proc_ptr = proc_entry;
805
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700806 proc_create_data("tx_bytes", proc_iface_perms, proc_entry,
807 &read_u64_fops,
808 &new_iface->totals_via_dev[IFS_TX].bytes);
809 proc_create_data("rx_bytes", proc_iface_perms, proc_entry,
810 &read_u64_fops,
811 &new_iface->totals_via_dev[IFS_RX].bytes);
812 proc_create_data("tx_packets", proc_iface_perms, proc_entry,
813 &read_u64_fops,
814 &new_iface->totals_via_dev[IFS_TX].packets);
815 proc_create_data("rx_packets", proc_iface_perms, proc_entry,
816 &read_u64_fops,
817 &new_iface->totals_via_dev[IFS_RX].packets);
818 proc_create_data("active", proc_iface_perms, proc_entry,
819 &read_bool_fops, &new_iface->active);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700820
821 IF_DEBUG("qtaguid: iface_stat: create_proc(): done "
822 "entry=%p dev=%s\n", new_iface, new_iface->ifname);
823 kfree(isw);
824}
825
826/*
827 * Will set the entry's active state, and
828 * update the net_dev accordingly also.
829 */
830static void _iface_stat_set_active(struct iface_stat *entry,
831 struct net_device *net_dev,
832 bool activate)
833{
834 if (activate) {
835 entry->net_dev = net_dev;
836 entry->active = true;
837 IF_DEBUG("qtaguid: %s(%s): "
838 "enable tracking. rfcnt=%d\n", __func__,
839 entry->ifname,
840 __this_cpu_read(*net_dev->pcpu_refcnt));
841 } else {
842 entry->active = false;
843 entry->net_dev = NULL;
844 IF_DEBUG("qtaguid: %s(%s): "
845 "disable tracking. rfcnt=%d\n", __func__,
846 entry->ifname,
847 __this_cpu_read(*net_dev->pcpu_refcnt));
848
849 }
850}
851
852/* Caller must hold iface_stat_list_lock */
853static struct iface_stat *iface_alloc(struct net_device *net_dev)
854{
855 struct iface_stat *new_iface;
856 struct iface_stat_work *isw;
857
858 new_iface = kzalloc(sizeof(*new_iface), GFP_ATOMIC);
859 if (new_iface == NULL) {
860 pr_err("qtaguid: iface_stat: create(%s): "
861 "iface_stat alloc failed\n", net_dev->name);
862 return NULL;
863 }
864 new_iface->ifname = kstrdup(net_dev->name, GFP_ATOMIC);
865 if (new_iface->ifname == NULL) {
866 pr_err("qtaguid: iface_stat: create(%s): "
867 "ifname alloc failed\n", net_dev->name);
868 kfree(new_iface);
869 return NULL;
870 }
871 spin_lock_init(&new_iface->tag_stat_list_lock);
872 new_iface->tag_stat_tree = RB_ROOT;
873 _iface_stat_set_active(new_iface, net_dev, true);
874
875 /*
876 * ipv6 notifier chains are atomic :(
877 * No create_proc_read_entry() for you!
878 */
879 isw = kmalloc(sizeof(*isw), GFP_ATOMIC);
880 if (!isw) {
881 pr_err("qtaguid: iface_stat: create(%s): "
882 "work alloc failed\n", new_iface->ifname);
883 _iface_stat_set_active(new_iface, net_dev, false);
884 kfree(new_iface->ifname);
885 kfree(new_iface);
886 return NULL;
887 }
888 isw->iface_entry = new_iface;
889 INIT_WORK(&isw->iface_work, iface_create_proc_worker);
890 schedule_work(&isw->iface_work);
891 list_add(&new_iface->list, &iface_stat_list);
892 return new_iface;
893}
894
895static void iface_check_stats_reset_and_adjust(struct net_device *net_dev,
896 struct iface_stat *iface)
897{
898 struct rtnl_link_stats64 dev_stats, *stats;
899 bool stats_rewound;
900
901 stats = dev_get_stats(net_dev, &dev_stats);
902 /* No empty packets */
903 stats_rewound =
904 (stats->rx_bytes < iface->last_known[IFS_RX].bytes)
905 || (stats->tx_bytes < iface->last_known[IFS_TX].bytes);
906
907 IF_DEBUG("qtaguid: %s(%s): iface=%p netdev=%p "
908 "bytes rx/tx=%llu/%llu "
909 "active=%d last_known=%d "
910 "stats_rewound=%d\n", __func__,
911 net_dev ? net_dev->name : "?",
912 iface, net_dev,
913 stats->rx_bytes, stats->tx_bytes,
914 iface->active, iface->last_known_valid, stats_rewound);
915
916 if (iface->active && iface->last_known_valid && stats_rewound) {
917 pr_warn_once("qtaguid: iface_stat: %s(%s): "
918 "iface reset its stats unexpectedly\n", __func__,
919 net_dev->name);
920
JP Abgrall9e0858c2012-04-27 12:57:39 -0700921 iface->totals_via_dev[IFS_TX].bytes +=
922 iface->last_known[IFS_TX].bytes;
923 iface->totals_via_dev[IFS_TX].packets +=
JP Abgrallbaf0db42011-06-20 12:41:46 -0700924 iface->last_known[IFS_TX].packets;
JP Abgrall9e0858c2012-04-27 12:57:39 -0700925 iface->totals_via_dev[IFS_RX].bytes +=
926 iface->last_known[IFS_RX].bytes;
927 iface->totals_via_dev[IFS_RX].packets +=
JP Abgrallbaf0db42011-06-20 12:41:46 -0700928 iface->last_known[IFS_RX].packets;
929 iface->last_known_valid = false;
930 IF_DEBUG("qtaguid: %s(%s): iface=%p "
931 "used last known bytes rx/tx=%llu/%llu\n", __func__,
932 iface->ifname, iface, iface->last_known[IFS_RX].bytes,
933 iface->last_known[IFS_TX].bytes);
934 }
935}
936
937/*
938 * Create a new entry for tracking the specified interface.
939 * Do nothing if the entry already exists.
940 * Called when an interface is configured with a valid IP address.
941 */
942static void iface_stat_create(struct net_device *net_dev,
943 struct in_ifaddr *ifa)
944{
945 struct in_device *in_dev = NULL;
946 const char *ifname;
947 struct iface_stat *entry;
948 __be32 ipaddr = 0;
949 struct iface_stat *new_iface;
950
951 IF_DEBUG("qtaguid: iface_stat: create(%s): ifa=%p netdev=%p\n",
952 net_dev ? net_dev->name : "?",
953 ifa, net_dev);
954 if (!net_dev) {
955 pr_err("qtaguid: iface_stat: create(): no net dev\n");
956 return;
957 }
958
959 ifname = net_dev->name;
960 if (!ifa) {
961 in_dev = in_dev_get(net_dev);
962 if (!in_dev) {
963 pr_err("qtaguid: iface_stat: create(%s): no inet dev\n",
964 ifname);
965 return;
966 }
967 IF_DEBUG("qtaguid: iface_stat: create(%s): in_dev=%p\n",
968 ifname, in_dev);
969 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
970 IF_DEBUG("qtaguid: iface_stat: create(%s): "
971 "ifa=%p ifa_label=%s\n",
Greg Hackmann37293852017-04-24 16:16:15 -0700972 ifname, ifa, ifa->ifa_label);
973 if (!strcmp(ifname, ifa->ifa_label))
JP Abgrallbaf0db42011-06-20 12:41:46 -0700974 break;
975 }
976 }
977
978 if (!ifa) {
979 IF_DEBUG("qtaguid: iface_stat: create(%s): no matching IP\n",
980 ifname);
981 goto done_put;
982 }
983 ipaddr = ifa->ifa_local;
984
985 spin_lock_bh(&iface_stat_list_lock);
986 entry = get_iface_entry(ifname);
987 if (entry != NULL) {
JP Abgrallbaf0db42011-06-20 12:41:46 -0700988 IF_DEBUG("qtaguid: iface_stat: create(%s): entry=%p\n",
989 ifname, entry);
990 iface_check_stats_reset_and_adjust(net_dev, entry);
JP Abgrallcf47f362013-02-06 17:40:07 -0800991 _iface_stat_set_active(entry, net_dev, true);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700992 IF_DEBUG("qtaguid: %s(%s): "
993 "tracking now %d on ip=%pI4\n", __func__,
JP Abgrallcf47f362013-02-06 17:40:07 -0800994 entry->ifname, true, &ipaddr);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700995 goto done_unlock_put;
996 }
997
998 new_iface = iface_alloc(net_dev);
999 IF_DEBUG("qtaguid: iface_stat: create(%s): done "
1000 "entry=%p ip=%pI4\n", ifname, new_iface, &ipaddr);
1001done_unlock_put:
1002 spin_unlock_bh(&iface_stat_list_lock);
1003done_put:
1004 if (in_dev)
1005 in_dev_put(in_dev);
1006}
1007
1008static void iface_stat_create_ipv6(struct net_device *net_dev,
1009 struct inet6_ifaddr *ifa)
1010{
1011 struct in_device *in_dev;
1012 const char *ifname;
1013 struct iface_stat *entry;
1014 struct iface_stat *new_iface;
1015 int addr_type;
1016
1017 IF_DEBUG("qtaguid: iface_stat: create6(): ifa=%p netdev=%p->name=%s\n",
1018 ifa, net_dev, net_dev ? net_dev->name : "");
1019 if (!net_dev) {
1020 pr_err("qtaguid: iface_stat: create6(): no net dev!\n");
1021 return;
1022 }
1023 ifname = net_dev->name;
1024
1025 in_dev = in_dev_get(net_dev);
1026 if (!in_dev) {
1027 pr_err("qtaguid: iface_stat: create6(%s): no inet dev\n",
1028 ifname);
1029 return;
1030 }
1031
1032 IF_DEBUG("qtaguid: iface_stat: create6(%s): in_dev=%p\n",
1033 ifname, in_dev);
1034
1035 if (!ifa) {
1036 IF_DEBUG("qtaguid: iface_stat: create6(%s): no matching IP\n",
1037 ifname);
1038 goto done_put;
1039 }
1040 addr_type = ipv6_addr_type(&ifa->addr);
1041
1042 spin_lock_bh(&iface_stat_list_lock);
1043 entry = get_iface_entry(ifname);
1044 if (entry != NULL) {
JP Abgrallbaf0db42011-06-20 12:41:46 -07001045 IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
1046 ifname, entry);
1047 iface_check_stats_reset_and_adjust(net_dev, entry);
JP Abgrallcf47f362013-02-06 17:40:07 -08001048 _iface_stat_set_active(entry, net_dev, true);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001049 IF_DEBUG("qtaguid: %s(%s): "
1050 "tracking now %d on ip=%pI6c\n", __func__,
JP Abgrallcf47f362013-02-06 17:40:07 -08001051 entry->ifname, true, &ifa->addr);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001052 goto done_unlock_put;
1053 }
1054
1055 new_iface = iface_alloc(net_dev);
1056 IF_DEBUG("qtaguid: iface_stat: create6(%s): done "
1057 "entry=%p ip=%pI6c\n", ifname, new_iface, &ifa->addr);
1058
1059done_unlock_put:
1060 spin_unlock_bh(&iface_stat_list_lock);
1061done_put:
1062 in_dev_put(in_dev);
1063}
1064
1065static struct sock_tag *get_sock_stat_nl(const struct sock *sk)
1066{
1067 MT_DEBUG("qtaguid: get_sock_stat_nl(sk=%p)\n", sk);
1068 return sock_tag_tree_search(&sock_tag_tree, sk);
1069}
1070
1071static struct sock_tag *get_sock_stat(const struct sock *sk)
1072{
1073 struct sock_tag *sock_tag_entry;
1074 MT_DEBUG("qtaguid: get_sock_stat(sk=%p)\n", sk);
1075 if (!sk)
1076 return NULL;
1077 spin_lock_bh(&sock_tag_list_lock);
1078 sock_tag_entry = get_sock_stat_nl(sk);
1079 spin_unlock_bh(&sock_tag_list_lock);
1080 return sock_tag_entry;
1081}
1082
JP Abgrall9e0858c2012-04-27 12:57:39 -07001083static int ipx_proto(const struct sk_buff *skb,
1084 struct xt_action_param *par)
1085{
1086 int thoff = 0, tproto;
1087
1088 switch (par->family) {
1089 case NFPROTO_IPV6:
1090 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
1091 if (tproto < 0)
1092 MT_DEBUG("%s(): transport header not found in ipv6"
1093 " skb=%p\n", __func__, skb);
1094 break;
1095 case NFPROTO_IPV4:
1096 tproto = ip_hdr(skb)->protocol;
1097 break;
1098 default:
1099 tproto = IPPROTO_RAW;
1100 }
1101 return tproto;
1102}
1103
JP Abgrallbaf0db42011-06-20 12:41:46 -07001104static void
1105data_counters_update(struct data_counters *dc, int set,
1106 enum ifs_tx_rx direction, int proto, int bytes)
1107{
1108 switch (proto) {
1109 case IPPROTO_TCP:
1110 dc_add_byte_packets(dc, set, direction, IFS_TCP, bytes, 1);
1111 break;
1112 case IPPROTO_UDP:
1113 dc_add_byte_packets(dc, set, direction, IFS_UDP, bytes, 1);
1114 break;
1115 case IPPROTO_IP:
1116 default:
1117 dc_add_byte_packets(dc, set, direction, IFS_PROTO_OTHER, bytes,
1118 1);
1119 break;
1120 }
1121}
1122
1123/*
1124 * Update stats for the specified interface. Do nothing if the entry
1125 * does not exist (when a device was never configured with an IP address).
1126 * Called when an device is being unregistered.
1127 */
1128static void iface_stat_update(struct net_device *net_dev, bool stash_only)
1129{
1130 struct rtnl_link_stats64 dev_stats, *stats;
1131 struct iface_stat *entry;
1132
1133 stats = dev_get_stats(net_dev, &dev_stats);
1134 spin_lock_bh(&iface_stat_list_lock);
1135 entry = get_iface_entry(net_dev->name);
1136 if (entry == NULL) {
1137 IF_DEBUG("qtaguid: iface_stat: update(%s): not tracked\n",
1138 net_dev->name);
1139 spin_unlock_bh(&iface_stat_list_lock);
1140 return;
1141 }
1142
1143 IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
1144 net_dev->name, entry);
1145 if (!entry->active) {
1146 IF_DEBUG("qtaguid: %s(%s): already disabled\n", __func__,
1147 net_dev->name);
1148 spin_unlock_bh(&iface_stat_list_lock);
1149 return;
1150 }
1151
1152 if (stash_only) {
1153 entry->last_known[IFS_TX].bytes = stats->tx_bytes;
1154 entry->last_known[IFS_TX].packets = stats->tx_packets;
1155 entry->last_known[IFS_RX].bytes = stats->rx_bytes;
1156 entry->last_known[IFS_RX].packets = stats->rx_packets;
1157 entry->last_known_valid = true;
1158 IF_DEBUG("qtaguid: %s(%s): "
1159 "dev stats stashed rx/tx=%llu/%llu\n", __func__,
1160 net_dev->name, stats->rx_bytes, stats->tx_bytes);
1161 spin_unlock_bh(&iface_stat_list_lock);
1162 return;
1163 }
JP Abgrall9e0858c2012-04-27 12:57:39 -07001164 entry->totals_via_dev[IFS_TX].bytes += stats->tx_bytes;
1165 entry->totals_via_dev[IFS_TX].packets += stats->tx_packets;
1166 entry->totals_via_dev[IFS_RX].bytes += stats->rx_bytes;
1167 entry->totals_via_dev[IFS_RX].packets += stats->rx_packets;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001168 /* We don't need the last_known[] anymore */
1169 entry->last_known_valid = false;
1170 _iface_stat_set_active(entry, net_dev, false);
1171 IF_DEBUG("qtaguid: %s(%s): "
1172 "disable tracking. rx/tx=%llu/%llu\n", __func__,
1173 net_dev->name, stats->rx_bytes, stats->tx_bytes);
1174 spin_unlock_bh(&iface_stat_list_lock);
1175}
1176
JP Abgrall9e0858c2012-04-27 12:57:39 -07001177/*
1178 * Update stats for the specified interface from the skb.
1179 * Do nothing if the entry
1180 * does not exist (when a device was never configured with an IP address).
1181 * Called on each sk.
1182 */
1183static void iface_stat_update_from_skb(const struct sk_buff *skb,
1184 struct xt_action_param *par)
1185{
1186 struct iface_stat *entry;
1187 const struct net_device *el_dev;
1188 enum ifs_tx_rx direction = par->in ? IFS_RX : IFS_TX;
1189 int bytes = skb->len;
JP Abgrall87f93e82013-01-28 16:50:44 -08001190 int proto;
JP Abgrall9e0858c2012-04-27 12:57:39 -07001191
1192 if (!skb->dev) {
1193 MT_DEBUG("qtaguid[%d]: no skb->dev\n", par->hooknum);
1194 el_dev = par->in ? : par->out;
1195 } else {
1196 const struct net_device *other_dev;
1197 el_dev = skb->dev;
1198 other_dev = par->in ? : par->out;
1199 if (el_dev != other_dev) {
1200 MT_DEBUG("qtaguid[%d]: skb->dev=%p %s vs "
1201 "par->(in/out)=%p %s\n",
1202 par->hooknum, el_dev, el_dev->name, other_dev,
1203 other_dev->name);
1204 }
1205 }
1206
1207 if (unlikely(!el_dev)) {
JP Abgralla2e371b2013-04-08 15:09:26 -07001208 pr_err_ratelimited("qtaguid[%d]: %s(): no par->in/out?!!\n",
1209 par->hooknum, __func__);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001210 BUG();
JP Abgrall9e0858c2012-04-27 12:57:39 -07001211 } else {
JP Abgrall87f93e82013-01-28 16:50:44 -08001212 proto = ipx_proto(skb, par);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001213 MT_DEBUG("qtaguid[%d]: dev name=%s type=%d fam=%d proto=%d\n",
1214 par->hooknum, el_dev->name, el_dev->type,
1215 par->family, proto);
1216 }
1217
1218 spin_lock_bh(&iface_stat_list_lock);
1219 entry = get_iface_entry(el_dev->name);
1220 if (entry == NULL) {
1221 IF_DEBUG("qtaguid: iface_stat: %s(%s): not tracked\n",
1222 __func__, el_dev->name);
1223 spin_unlock_bh(&iface_stat_list_lock);
1224 return;
1225 }
1226
1227 IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
1228 el_dev->name, entry);
1229
JP Abgrall87f93e82013-01-28 16:50:44 -08001230 data_counters_update(&entry->totals_via_skb, 0, direction, proto,
1231 bytes);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001232 spin_unlock_bh(&iface_stat_list_lock);
1233}
1234
JP Abgrallbaf0db42011-06-20 12:41:46 -07001235static void tag_stat_update(struct tag_stat *tag_entry,
1236 enum ifs_tx_rx direction, int proto, int bytes)
1237{
1238 int active_set;
1239 active_set = get_active_counter_set(tag_entry->tn.tag);
1240 MT_DEBUG("qtaguid: tag_stat_update(tag=0x%llx (uid=%u) set=%d "
1241 "dir=%d proto=%d bytes=%d)\n",
1242 tag_entry->tn.tag, get_uid_from_tag(tag_entry->tn.tag),
1243 active_set, direction, proto, bytes);
1244 data_counters_update(&tag_entry->counters, active_set, direction,
1245 proto, bytes);
1246 if (tag_entry->parent_counters)
1247 data_counters_update(tag_entry->parent_counters, active_set,
1248 direction, proto, bytes);
1249}
1250
1251/*
1252 * Create a new entry for tracking the specified {acct_tag,uid_tag} within
1253 * the interface.
1254 * iface_entry->tag_stat_list_lock should be held.
1255 */
1256static struct tag_stat *create_if_tag_stat(struct iface_stat *iface_entry,
1257 tag_t tag)
1258{
1259 struct tag_stat *new_tag_stat_entry = NULL;
1260 IF_DEBUG("qtaguid: iface_stat: %s(): ife=%p tag=0x%llx"
1261 " (uid=%u)\n", __func__,
1262 iface_entry, tag, get_uid_from_tag(tag));
1263 new_tag_stat_entry = kzalloc(sizeof(*new_tag_stat_entry), GFP_ATOMIC);
1264 if (!new_tag_stat_entry) {
1265 pr_err("qtaguid: iface_stat: tag stat alloc failed\n");
1266 goto done;
1267 }
1268 new_tag_stat_entry->tn.tag = tag;
1269 tag_stat_tree_insert(new_tag_stat_entry, &iface_entry->tag_stat_tree);
1270done:
1271 return new_tag_stat_entry;
1272}
1273
1274static void if_tag_stat_update(const char *ifname, uid_t uid,
1275 const struct sock *sk, enum ifs_tx_rx direction,
1276 int proto, int bytes)
1277{
1278 struct tag_stat *tag_stat_entry;
1279 tag_t tag, acct_tag;
1280 tag_t uid_tag;
1281 struct data_counters *uid_tag_counters;
1282 struct sock_tag *sock_tag_entry;
1283 struct iface_stat *iface_entry;
JP Abgralle5d79862012-04-13 19:22:35 -07001284 struct tag_stat *new_tag_stat = NULL;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001285 MT_DEBUG("qtaguid: if_tag_stat_update(ifname=%s "
1286 "uid=%u sk=%p dir=%d proto=%d bytes=%d)\n",
1287 ifname, uid, sk, direction, proto, bytes);
1288
liping.zhangf84e6a12016-01-11 13:31:01 +08001289 spin_lock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001290 iface_entry = get_iface_entry(ifname);
1291 if (!iface_entry) {
JP Abgralla2e371b2013-04-08 15:09:26 -07001292 pr_err_ratelimited("qtaguid: iface_stat: stat_update() "
1293 "%s not found\n", ifname);
liping.zhangf84e6a12016-01-11 13:31:01 +08001294 spin_unlock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001295 return;
1296 }
1297 /* It is ok to process data when an iface_entry is inactive */
1298
1299 MT_DEBUG("qtaguid: iface_stat: stat_update() dev=%s entry=%p\n",
1300 ifname, iface_entry);
1301
1302 /*
1303 * Look for a tagged sock.
1304 * It will have an acct_uid.
1305 */
1306 sock_tag_entry = get_sock_stat(sk);
1307 if (sock_tag_entry) {
1308 tag = sock_tag_entry->tag;
1309 acct_tag = get_atag_from_tag(tag);
1310 uid_tag = get_utag_from_tag(tag);
1311 } else {
1312 acct_tag = make_atag_from_value(0);
1313 tag = combine_atag_with_uid(acct_tag, uid);
1314 uid_tag = make_tag_from_uid(uid);
1315 }
1316 MT_DEBUG("qtaguid: iface_stat: stat_update(): "
1317 " looking for tag=0x%llx (uid=%u) in ife=%p\n",
1318 tag, get_uid_from_tag(tag), iface_entry);
1319 /* Loop over tag list under this interface for {acct_tag,uid_tag} */
1320 spin_lock_bh(&iface_entry->tag_stat_list_lock);
1321
1322 tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree,
1323 tag);
1324 if (tag_stat_entry) {
1325 /*
1326 * Updating the {acct_tag, uid_tag} entry handles both stats:
1327 * {0, uid_tag} will also get updated.
1328 */
1329 tag_stat_update(tag_stat_entry, direction, proto, bytes);
liping.zhangf84e6a12016-01-11 13:31:01 +08001330 goto unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001331 }
1332
1333 /* Loop over tag list under this interface for {0,uid_tag} */
1334 tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree,
1335 uid_tag);
1336 if (!tag_stat_entry) {
1337 /* Here: the base uid_tag did not exist */
1338 /*
1339 * No parent counters. So
1340 * - No {0, uid_tag} stats and no {acc_tag, uid_tag} stats.
1341 */
1342 new_tag_stat = create_if_tag_stat(iface_entry, uid_tag);
Pontus Fuchsb842ea52012-11-19 11:44:51 -08001343 if (!new_tag_stat)
1344 goto unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001345 uid_tag_counters = &new_tag_stat->counters;
1346 } else {
1347 uid_tag_counters = &tag_stat_entry->counters;
1348 }
1349
1350 if (acct_tag) {
JP Abgralle5d79862012-04-13 19:22:35 -07001351 /* Create the child {acct_tag, uid_tag} and hook up parent. */
JP Abgrallbaf0db42011-06-20 12:41:46 -07001352 new_tag_stat = create_if_tag_stat(iface_entry, tag);
Pontus Fuchsb842ea52012-11-19 11:44:51 -08001353 if (!new_tag_stat)
1354 goto unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001355 new_tag_stat->parent_counters = uid_tag_counters;
JP Abgralle5d79862012-04-13 19:22:35 -07001356 } else {
1357 /*
1358 * For new_tag_stat to be still NULL here would require:
1359 * {0, uid_tag} exists
1360 * and {acct_tag, uid_tag} doesn't exist
1361 * AND acct_tag == 0.
1362 * Impossible. This reassures us that new_tag_stat
1363 * below will always be assigned.
1364 */
1365 BUG_ON(!new_tag_stat);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001366 }
1367 tag_stat_update(new_tag_stat, direction, proto, bytes);
Pontus Fuchsb842ea52012-11-19 11:44:51 -08001368unlock:
JP Abgrallbaf0db42011-06-20 12:41:46 -07001369 spin_unlock_bh(&iface_entry->tag_stat_list_lock);
liping.zhangf84e6a12016-01-11 13:31:01 +08001370 spin_unlock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001371}
1372
1373static int iface_netdev_event_handler(struct notifier_block *nb,
1374 unsigned long event, void *ptr) {
Jon Medhurst (Tixy)a347e8e2014-04-14 21:20:49 -07001375 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001376
1377 if (unlikely(module_passive))
1378 return NOTIFY_DONE;
1379
1380 IF_DEBUG("qtaguid: iface_stat: netdev_event(): "
1381 "ev=0x%lx/%s netdev=%p->name=%s\n",
1382 event, netdev_evt_str(event), dev, dev ? dev->name : "");
1383
1384 switch (event) {
1385 case NETDEV_UP:
1386 iface_stat_create(dev, NULL);
1387 atomic64_inc(&qtu_events.iface_events);
1388 break;
1389 case NETDEV_DOWN:
1390 case NETDEV_UNREGISTER:
1391 iface_stat_update(dev, event == NETDEV_DOWN);
1392 atomic64_inc(&qtu_events.iface_events);
1393 break;
1394 }
1395 return NOTIFY_DONE;
1396}
1397
1398static int iface_inet6addr_event_handler(struct notifier_block *nb,
1399 unsigned long event, void *ptr)
1400{
1401 struct inet6_ifaddr *ifa = ptr;
1402 struct net_device *dev;
1403
1404 if (unlikely(module_passive))
1405 return NOTIFY_DONE;
1406
1407 IF_DEBUG("qtaguid: iface_stat: inet6addr_event(): "
1408 "ev=0x%lx/%s ifa=%p\n",
1409 event, netdev_evt_str(event), ifa);
1410
1411 switch (event) {
1412 case NETDEV_UP:
1413 BUG_ON(!ifa || !ifa->idev);
1414 dev = (struct net_device *)ifa->idev->dev;
1415 iface_stat_create_ipv6(dev, ifa);
1416 atomic64_inc(&qtu_events.iface_events);
1417 break;
1418 case NETDEV_DOWN:
1419 case NETDEV_UNREGISTER:
1420 BUG_ON(!ifa || !ifa->idev);
1421 dev = (struct net_device *)ifa->idev->dev;
1422 iface_stat_update(dev, event == NETDEV_DOWN);
1423 atomic64_inc(&qtu_events.iface_events);
1424 break;
1425 }
1426 return NOTIFY_DONE;
1427}
1428
1429static int iface_inetaddr_event_handler(struct notifier_block *nb,
1430 unsigned long event, void *ptr)
1431{
1432 struct in_ifaddr *ifa = ptr;
1433 struct net_device *dev;
1434
1435 if (unlikely(module_passive))
1436 return NOTIFY_DONE;
1437
1438 IF_DEBUG("qtaguid: iface_stat: inetaddr_event(): "
1439 "ev=0x%lx/%s ifa=%p\n",
1440 event, netdev_evt_str(event), ifa);
1441
1442 switch (event) {
1443 case NETDEV_UP:
1444 BUG_ON(!ifa || !ifa->ifa_dev);
1445 dev = ifa->ifa_dev->dev;
1446 iface_stat_create(dev, ifa);
1447 atomic64_inc(&qtu_events.iface_events);
1448 break;
1449 case NETDEV_DOWN:
1450 case NETDEV_UNREGISTER:
1451 BUG_ON(!ifa || !ifa->ifa_dev);
1452 dev = ifa->ifa_dev->dev;
1453 iface_stat_update(dev, event == NETDEV_DOWN);
1454 atomic64_inc(&qtu_events.iface_events);
1455 break;
1456 }
1457 return NOTIFY_DONE;
1458}
1459
1460static struct notifier_block iface_netdev_notifier_blk = {
1461 .notifier_call = iface_netdev_event_handler,
1462};
1463
1464static struct notifier_block iface_inetaddr_notifier_blk = {
1465 .notifier_call = iface_inetaddr_event_handler,
1466};
1467
1468static struct notifier_block iface_inet6addr_notifier_blk = {
1469 .notifier_call = iface_inet6addr_event_handler,
1470};
1471
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001472static const struct seq_operations iface_stat_fmt_proc_seq_ops = {
1473 .start = iface_stat_fmt_proc_start,
1474 .next = iface_stat_fmt_proc_next,
1475 .stop = iface_stat_fmt_proc_stop,
1476 .show = iface_stat_fmt_proc_show,
1477};
1478
1479static int proc_iface_stat_fmt_open(struct inode *inode, struct file *file)
1480{
1481 struct proc_iface_stat_fmt_info *s;
1482
1483 s = __seq_open_private(file, &iface_stat_fmt_proc_seq_ops,
1484 sizeof(struct proc_iface_stat_fmt_info));
1485 if (!s)
1486 return -ENOMEM;
1487
Greg Hackmann85a2eb52014-02-24 09:39:46 -08001488 s->fmt = (uintptr_t)PDE_DATA(inode);
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001489 return 0;
1490}
1491
1492static const struct file_operations proc_iface_stat_fmt_fops = {
1493 .open = proc_iface_stat_fmt_open,
1494 .read = seq_read,
1495 .llseek = seq_lseek,
Greg Hackmann56472912013-12-04 17:39:27 -08001496 .release = seq_release_private,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001497};
1498
JP Abgrallbaf0db42011-06-20 12:41:46 -07001499static int __init iface_stat_init(struct proc_dir_entry *parent_procdir)
1500{
1501 int err;
1502
1503 iface_stat_procdir = proc_mkdir(iface_stat_procdirname, parent_procdir);
1504 if (!iface_stat_procdir) {
1505 pr_err("qtaguid: iface_stat: init failed to create proc entry\n");
1506 err = -1;
1507 goto err;
1508 }
1509
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001510 iface_stat_all_procfile = proc_create_data(iface_stat_all_procfilename,
1511 proc_iface_perms,
1512 parent_procdir,
1513 &proc_iface_stat_fmt_fops,
1514 (void *)1 /* fmt1 */);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001515 if (!iface_stat_all_procfile) {
1516 pr_err("qtaguid: iface_stat: init "
JP Abgrall9e0858c2012-04-27 12:57:39 -07001517 " failed to create stat_old proc entry\n");
JP Abgrallbaf0db42011-06-20 12:41:46 -07001518 err = -1;
1519 goto err_zap_entry;
1520 }
JP Abgrall9e0858c2012-04-27 12:57:39 -07001521
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001522 iface_stat_fmt_procfile = proc_create_data(iface_stat_fmt_procfilename,
1523 proc_iface_perms,
1524 parent_procdir,
1525 &proc_iface_stat_fmt_fops,
1526 (void *)2 /* fmt2 */);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001527 if (!iface_stat_fmt_procfile) {
1528 pr_err("qtaguid: iface_stat: init "
1529 " failed to create stat_all proc entry\n");
1530 err = -1;
1531 goto err_zap_all_stats_entry;
1532 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001533
1534
1535 err = register_netdevice_notifier(&iface_netdev_notifier_blk);
1536 if (err) {
1537 pr_err("qtaguid: iface_stat: init "
1538 "failed to register dev event handler\n");
JP Abgrall9e0858c2012-04-27 12:57:39 -07001539 goto err_zap_all_stats_entries;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001540 }
1541 err = register_inetaddr_notifier(&iface_inetaddr_notifier_blk);
1542 if (err) {
1543 pr_err("qtaguid: iface_stat: init "
1544 "failed to register ipv4 dev event handler\n");
1545 goto err_unreg_nd;
1546 }
1547
1548 err = register_inet6addr_notifier(&iface_inet6addr_notifier_blk);
1549 if (err) {
1550 pr_err("qtaguid: iface_stat: init "
1551 "failed to register ipv6 dev event handler\n");
1552 goto err_unreg_ip4_addr;
1553 }
1554 return 0;
1555
1556err_unreg_ip4_addr:
1557 unregister_inetaddr_notifier(&iface_inetaddr_notifier_blk);
1558err_unreg_nd:
1559 unregister_netdevice_notifier(&iface_netdev_notifier_blk);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001560err_zap_all_stats_entries:
1561 remove_proc_entry(iface_stat_fmt_procfilename, parent_procdir);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001562err_zap_all_stats_entry:
1563 remove_proc_entry(iface_stat_all_procfilename, parent_procdir);
1564err_zap_entry:
1565 remove_proc_entry(iface_stat_procdirname, parent_procdir);
1566err:
1567 return err;
1568}
1569
1570static struct sock *qtaguid_find_sk(const struct sk_buff *skb,
1571 struct xt_action_param *par)
1572{
1573 struct sock *sk;
1574 unsigned int hook_mask = (1 << par->hooknum);
1575
1576 MT_DEBUG("qtaguid: find_sk(skb=%p) hooknum=%d family=%d\n", skb,
1577 par->hooknum, par->family);
1578
1579 /*
1580 * Let's not abuse the the xt_socket_get*_sk(), or else it will
1581 * return garbage SKs.
1582 */
1583 if (!(hook_mask & XT_SOCKET_SUPPORTED_HOOKS))
1584 return NULL;
1585
1586 switch (par->family) {
1587 case NFPROTO_IPV6:
Amit Pundir7de1bb82015-11-20 14:45:40 +05301588 sk = xt_socket_lookup_slow_v6(dev_net(skb->dev), skb, par->in);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001589 break;
1590 case NFPROTO_IPV4:
Amit Pundir7de1bb82015-11-20 14:45:40 +05301591 sk = xt_socket_lookup_slow_v4(dev_net(skb->dev), skb, par->in);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001592 break;
1593 default:
1594 return NULL;
1595 }
1596
JP Abgrallbaf0db42011-06-20 12:41:46 -07001597 if (sk) {
1598 MT_DEBUG("qtaguid: %p->sk_proto=%u "
1599 "->sk_state=%d\n", sk, sk->sk_protocol, sk->sk_state);
JP Abgralld1fd3972013-02-20 16:38:34 -08001600 /*
1601 * When in TCP_TIME_WAIT the sk is not a "struct sock" but
1602 * "struct inet_timewait_sock" which is missing fields.
1603 */
John Stultz69787662016-05-12 11:17:52 -07001604 if (!sk_fullsock(sk) || sk->sk_state == TCP_TIME_WAIT) {
Amit Pundir2879b6e2015-01-29 01:16:23 +05301605 sock_gen_put(sk);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001606 sk = NULL;
1607 }
1608 }
1609 return sk;
1610}
1611
1612static void account_for_uid(const struct sk_buff *skb,
1613 const struct sock *alternate_sk, uid_t uid,
1614 struct xt_action_param *par)
1615{
1616 const struct net_device *el_dev;
1617
1618 if (!skb->dev) {
1619 MT_DEBUG("qtaguid[%d]: no skb->dev\n", par->hooknum);
1620 el_dev = par->in ? : par->out;
1621 } else {
1622 const struct net_device *other_dev;
1623 el_dev = skb->dev;
1624 other_dev = par->in ? : par->out;
1625 if (el_dev != other_dev) {
1626 MT_DEBUG("qtaguid[%d]: skb->dev=%p %s vs "
1627 "par->(in/out)=%p %s\n",
1628 par->hooknum, el_dev, el_dev->name, other_dev,
1629 other_dev->name);
1630 }
1631 }
1632
1633 if (unlikely(!el_dev)) {
1634 pr_info("qtaguid[%d]: no par->in/out?!!\n", par->hooknum);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001635 } else {
JP Abgrall4bb20aa2012-04-17 16:00:07 -07001636 int proto = ipx_proto(skb, par);
1637 MT_DEBUG("qtaguid[%d]: dev name=%s type=%d fam=%d proto=%d\n",
1638 par->hooknum, el_dev->name, el_dev->type,
1639 par->family, proto);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001640
1641 if_tag_stat_update(el_dev->name, uid,
1642 skb->sk ? skb->sk : alternate_sk,
1643 par->in ? IFS_RX : IFS_TX,
JP Abgrall4bb20aa2012-04-17 16:00:07 -07001644 proto, skb->len);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001645 }
1646}
1647
1648static bool qtaguid_mt(const struct sk_buff *skb, struct xt_action_param *par)
1649{
1650 const struct xt_qtaguid_match_info *info = par->matchinfo;
1651 const struct file *filp;
1652 bool got_sock = false;
1653 struct sock *sk;
John Stultzbd1bca42014-03-28 16:23:48 -07001654 kuid_t sock_uid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001655 bool res;
Mohamad Ayyashe8101fd2015-01-13 19:20:44 -08001656 bool set_sk_callback_lock = false;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001657
1658 if (unlikely(module_passive))
1659 return (info->match ^ info->invert) == 0;
1660
1661 MT_DEBUG("qtaguid[%d]: entered skb=%p par->in=%p/out=%p fam=%d\n",
1662 par->hooknum, skb, par->in, par->out, par->family);
1663
1664 atomic64_inc(&qtu_events.match_calls);
1665 if (skb == NULL) {
1666 res = (info->match ^ info->invert) == 0;
1667 goto ret_res;
1668 }
1669
JP Abgrall9e0858c2012-04-27 12:57:39 -07001670 switch (par->hooknum) {
1671 case NF_INET_PRE_ROUTING:
1672 case NF_INET_POST_ROUTING:
1673 atomic64_inc(&qtu_events.match_calls_prepost);
1674 iface_stat_update_from_skb(skb, par);
1675 /*
1676 * We are done in pre/post. The skb will get processed
1677 * further alter.
1678 */
1679 res = (info->match ^ info->invert);
1680 goto ret_res;
1681 break;
1682 /* default: Fall through and do UID releated work */
1683 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001684
John Stultz551dae62016-04-22 17:12:57 -07001685 sk = skb_to_full_sk(skb);
JP Abgralld1fd3972013-02-20 16:38:34 -08001686 /*
1687 * When in TCP_TIME_WAIT the sk is not a "struct sock" but
1688 * "struct inet_timewait_sock" which is missing fields.
1689 * So we ignore it.
1690 */
1691 if (sk && sk->sk_state == TCP_TIME_WAIT)
1692 sk = NULL;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001693 if (sk == NULL) {
1694 /*
1695 * A missing sk->sk_socket happens when packets are in-flight
1696 * and the matching socket is already closed and gone.
1697 */
1698 sk = qtaguid_find_sk(skb, par);
1699 /*
1700 * If we got the socket from the find_sk(), we will need to put
1701 * it back, as nf_tproxy_get_sock_v4() got it.
1702 */
1703 got_sock = sk;
1704 if (sk)
1705 atomic64_inc(&qtu_events.match_found_sk_in_ct);
1706 else
1707 atomic64_inc(&qtu_events.match_found_no_sk_in_ct);
1708 } else {
1709 atomic64_inc(&qtu_events.match_found_sk);
1710 }
JP Abgrall4bb20aa2012-04-17 16:00:07 -07001711 MT_DEBUG("qtaguid[%d]: sk=%p got_sock=%d fam=%d proto=%d\n",
1712 par->hooknum, sk, got_sock, par->family, ipx_proto(skb, par));
JP Abgrallbaf0db42011-06-20 12:41:46 -07001713 if (sk != NULL) {
Mohamad Ayyashe8101fd2015-01-13 19:20:44 -08001714 set_sk_callback_lock = true;
1715 read_lock_bh(&sk->sk_callback_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001716 MT_DEBUG("qtaguid[%d]: sk=%p->sk_socket=%p->file=%p\n",
1717 par->hooknum, sk, sk->sk_socket,
1718 sk->sk_socket ? sk->sk_socket->file : (void *)-1LL);
1719 filp = sk->sk_socket ? sk->sk_socket->file : NULL;
1720 MT_DEBUG("qtaguid[%d]: filp...uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07001721 par->hooknum, filp ? from_kuid(&init_user_ns, filp->f_cred->fsuid) : -1);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001722 }
1723
1724 if (sk == NULL || sk->sk_socket == NULL) {
1725 /*
1726 * Here, the qtaguid_find_sk() using connection tracking
1727 * couldn't find the owner, so for now we just count them
1728 * against the system.
1729 */
1730 /*
1731 * TODO: unhack how to force just accounting.
1732 * For now we only do iface stats when the uid-owner is not
1733 * requested.
1734 */
1735 if (!(info->match & XT_QTAGUID_UID))
1736 account_for_uid(skb, sk, 0, par);
1737 MT_DEBUG("qtaguid[%d]: leaving (sk?sk->sk_socket)=%p\n",
1738 par->hooknum,
1739 sk ? sk->sk_socket : NULL);
1740 res = (info->match ^ info->invert) == 0;
1741 atomic64_inc(&qtu_events.match_no_sk);
1742 goto put_sock_ret_res;
1743 } else if (info->match & info->invert & XT_QTAGUID_SOCKET) {
1744 res = false;
1745 goto put_sock_ret_res;
1746 }
1747 filp = sk->sk_socket->file;
1748 if (filp == NULL) {
1749 MT_DEBUG("qtaguid[%d]: leaving filp=NULL\n", par->hooknum);
1750 account_for_uid(skb, sk, 0, par);
1751 res = ((info->match ^ info->invert) &
1752 (XT_QTAGUID_UID | XT_QTAGUID_GID)) == 0;
1753 atomic64_inc(&qtu_events.match_no_sk_file);
1754 goto put_sock_ret_res;
1755 }
1756 sock_uid = filp->f_cred->fsuid;
1757 /*
1758 * TODO: unhack how to force just accounting.
1759 * For now we only do iface stats when the uid-owner is not requested
1760 */
1761 if (!(info->match & XT_QTAGUID_UID))
John Stultzbd1bca42014-03-28 16:23:48 -07001762 account_for_uid(skb, sk, from_kuid(&init_user_ns, sock_uid), par);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001763
1764 /*
1765 * The following two tests fail the match when:
1766 * id not in range AND no inverted condition requested
1767 * or id in range AND inverted condition requested
1768 * Thus (!a && b) || (a && !b) == a ^ b
1769 */
John Stultzbd1bca42014-03-28 16:23:48 -07001770 if (info->match & XT_QTAGUID_UID) {
1771 kuid_t uid_min = make_kuid(&init_user_ns, info->uid_min);
1772 kuid_t uid_max = make_kuid(&init_user_ns, info->uid_max);
1773
Amit Pundir070eff82015-01-20 16:13:08 +05301774 if ((uid_gte(filp->f_cred->fsuid, uid_min) &&
1775 uid_lte(filp->f_cred->fsuid, uid_max)) ^
JP Abgrallbaf0db42011-06-20 12:41:46 -07001776 !(info->invert & XT_QTAGUID_UID)) {
1777 MT_DEBUG("qtaguid[%d]: leaving uid not matching\n",
1778 par->hooknum);
1779 res = false;
1780 goto put_sock_ret_res;
1781 }
John Stultzbd1bca42014-03-28 16:23:48 -07001782 }
1783 if (info->match & XT_QTAGUID_GID) {
1784 kgid_t gid_min = make_kgid(&init_user_ns, info->gid_min);
1785 kgid_t gid_max = make_kgid(&init_user_ns, info->gid_max);
1786
Amit Pundir070eff82015-01-20 16:13:08 +05301787 if ((gid_gte(filp->f_cred->fsgid, gid_min) &&
1788 gid_lte(filp->f_cred->fsgid, gid_max)) ^
JP Abgrallbaf0db42011-06-20 12:41:46 -07001789 !(info->invert & XT_QTAGUID_GID)) {
1790 MT_DEBUG("qtaguid[%d]: leaving gid not matching\n",
1791 par->hooknum);
1792 res = false;
1793 goto put_sock_ret_res;
1794 }
John Stultzbd1bca42014-03-28 16:23:48 -07001795 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001796 MT_DEBUG("qtaguid[%d]: leaving matched\n", par->hooknum);
1797 res = true;
1798
1799put_sock_ret_res:
1800 if (got_sock)
Amit Pundir2879b6e2015-01-29 01:16:23 +05301801 sock_gen_put(sk);
Mohamad Ayyashe8101fd2015-01-13 19:20:44 -08001802 if (set_sk_callback_lock)
1803 read_unlock_bh(&sk->sk_callback_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001804ret_res:
1805 MT_DEBUG("qtaguid[%d]: left %d\n", par->hooknum, res);
1806 return res;
1807}
1808
1809#ifdef DDEBUG
Chenbo Feng64157f42017-03-23 13:51:24 -07001810/*
1811 * This function is not in xt_qtaguid_print.c because of locks visibility.
1812 * The lock of sock_tag_list must be aquired before calling this function
1813 */
1814static void prdebug_full_state_locked(int indent_level, const char *fmt, ...)
JP Abgrallbaf0db42011-06-20 12:41:46 -07001815{
1816 va_list args;
1817 char *fmt_buff;
1818 char *buff;
1819
1820 if (!unlikely(qtaguid_debug_mask & DDEBUG_MASK))
1821 return;
1822
1823 fmt_buff = kasprintf(GFP_ATOMIC,
1824 "qtaguid: %s(): %s {\n", __func__, fmt);
1825 BUG_ON(!fmt_buff);
1826 va_start(args, fmt);
1827 buff = kvasprintf(GFP_ATOMIC,
1828 fmt_buff, args);
1829 BUG_ON(!buff);
1830 pr_debug("%s", buff);
1831 kfree(fmt_buff);
1832 kfree(buff);
1833 va_end(args);
1834
JP Abgrallbaf0db42011-06-20 12:41:46 -07001835 prdebug_sock_tag_tree(indent_level, &sock_tag_tree);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001836
JP Abgrallbaf0db42011-06-20 12:41:46 -07001837 spin_lock_bh(&uid_tag_data_tree_lock);
1838 prdebug_uid_tag_data_tree(indent_level, &uid_tag_data_tree);
1839 prdebug_proc_qtu_data_tree(indent_level, &proc_qtu_data_tree);
1840 spin_unlock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001841
1842 spin_lock_bh(&iface_stat_list_lock);
1843 prdebug_iface_stat_list(indent_level, &iface_stat_list);
1844 spin_unlock_bh(&iface_stat_list_lock);
1845
1846 pr_debug("qtaguid: %s(): }\n", __func__);
1847}
1848#else
Chenbo Feng64157f42017-03-23 13:51:24 -07001849static void prdebug_full_state_locked(int indent_level, const char *fmt, ...) {}
JP Abgrallbaf0db42011-06-20 12:41:46 -07001850#endif
1851
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001852struct proc_ctrl_print_info {
1853 struct sock *sk; /* socket found by reading to sk_pos */
1854 loff_t sk_pos;
1855};
1856
1857static void *qtaguid_ctrl_proc_next(struct seq_file *m, void *v, loff_t *pos)
1858{
1859 struct proc_ctrl_print_info *pcpi = m->private;
1860 struct sock_tag *sock_tag_entry = v;
1861 struct rb_node *node;
1862
1863 (*pos)++;
1864
1865 if (!v || v == SEQ_START_TOKEN)
1866 return NULL;
1867
1868 node = rb_next(&sock_tag_entry->sock_node);
1869 if (!node) {
1870 pcpi->sk = NULL;
1871 sock_tag_entry = SEQ_START_TOKEN;
1872 } else {
1873 sock_tag_entry = rb_entry(node, struct sock_tag, sock_node);
1874 pcpi->sk = sock_tag_entry->sk;
1875 }
1876 pcpi->sk_pos = *pos;
1877 return sock_tag_entry;
1878}
1879
1880static void *qtaguid_ctrl_proc_start(struct seq_file *m, loff_t *pos)
1881{
1882 struct proc_ctrl_print_info *pcpi = m->private;
1883 struct sock_tag *sock_tag_entry;
1884 struct rb_node *node;
1885
1886 spin_lock_bh(&sock_tag_list_lock);
1887
1888 if (unlikely(module_passive))
1889 return NULL;
1890
1891 if (*pos == 0) {
1892 pcpi->sk_pos = 0;
1893 node = rb_first(&sock_tag_tree);
1894 if (!node) {
1895 pcpi->sk = NULL;
1896 return SEQ_START_TOKEN;
1897 }
1898 sock_tag_entry = rb_entry(node, struct sock_tag, sock_node);
1899 pcpi->sk = sock_tag_entry->sk;
1900 } else {
1901 sock_tag_entry = (pcpi->sk ? get_sock_stat_nl(pcpi->sk) :
1902 NULL) ?: SEQ_START_TOKEN;
1903 if (*pos != pcpi->sk_pos) {
1904 /* seq_read skipped a next call */
1905 *pos = pcpi->sk_pos;
1906 return qtaguid_ctrl_proc_next(m, sock_tag_entry, pos);
1907 }
1908 }
1909 return sock_tag_entry;
1910}
1911
1912static void qtaguid_ctrl_proc_stop(struct seq_file *m, void *v)
1913{
1914 spin_unlock_bh(&sock_tag_list_lock);
1915}
1916
JP Abgrallbaf0db42011-06-20 12:41:46 -07001917/*
1918 * Procfs reader to get all active socket tags using style "1)" as described in
1919 * fs/proc/generic.c
1920 */
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001921static int qtaguid_ctrl_proc_show(struct seq_file *m, void *v)
JP Abgrallbaf0db42011-06-20 12:41:46 -07001922{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001923 struct sock_tag *sock_tag_entry = v;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001924 uid_t uid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001925
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001926 CT_DEBUG("qtaguid: proc ctrl pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07001927 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07001928
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001929 if (sock_tag_entry != SEQ_START_TOKEN) {
Chenbo Feng875e5262017-04-19 14:22:47 -07001930 int sk_ref_count;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001931 uid = get_uid_from_tag(sock_tag_entry->tag);
1932 CT_DEBUG("qtaguid: proc_read(): sk=%p tag=0x%llx (uid=%u) "
1933 "pid=%u\n",
1934 sock_tag_entry->sk,
1935 sock_tag_entry->tag,
1936 uid,
1937 sock_tag_entry->pid
1938 );
Chenbo Feng875e5262017-04-19 14:22:47 -07001939 sk_ref_count = atomic_read(
1940 &sock_tag_entry->sk->sk_refcnt);
Mohamad Ayyash8613d932016-05-11 13:18:35 -07001941 seq_printf(m, "sock=%pK tag=0x%llx (uid=%u) pid=%u "
Chenbo Feng875e5262017-04-19 14:22:47 -07001942 "f_count=%d\n",
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001943 sock_tag_entry->sk,
1944 sock_tag_entry->tag, uid,
Chenbo Feng875e5262017-04-19 14:22:47 -07001945 sock_tag_entry->pid, sk_ref_count);
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001946 } else {
1947 seq_printf(m, "events: sockets_tagged=%llu "
1948 "sockets_untagged=%llu "
1949 "counter_set_changes=%llu "
1950 "delete_cmds=%llu "
1951 "iface_events=%llu "
1952 "match_calls=%llu "
1953 "match_calls_prepost=%llu "
1954 "match_found_sk=%llu "
1955 "match_found_sk_in_ct=%llu "
1956 "match_found_no_sk_in_ct=%llu "
1957 "match_no_sk=%llu "
1958 "match_no_sk_file=%llu\n",
Sherman Yinda5ea992014-06-12 14:35:38 -07001959 (u64)atomic64_read(&qtu_events.sockets_tagged),
1960 (u64)atomic64_read(&qtu_events.sockets_untagged),
1961 (u64)atomic64_read(&qtu_events.counter_set_changes),
1962 (u64)atomic64_read(&qtu_events.delete_cmds),
1963 (u64)atomic64_read(&qtu_events.iface_events),
1964 (u64)atomic64_read(&qtu_events.match_calls),
1965 (u64)atomic64_read(&qtu_events.match_calls_prepost),
1966 (u64)atomic64_read(&qtu_events.match_found_sk),
1967 (u64)atomic64_read(&qtu_events.match_found_sk_in_ct),
1968 (u64)atomic64_read(&qtu_events.match_found_no_sk_in_ct),
1969 (u64)atomic64_read(&qtu_events.match_no_sk),
1970 (u64)atomic64_read(&qtu_events.match_no_sk_file));
JP Abgrallbaf0db42011-06-20 12:41:46 -07001971
Chenbo Feng64157f42017-03-23 13:51:24 -07001972 /* Count the following as part of the last item_index. No need
1973 * to lock the sock_tag_list here since it is already locked when
1974 * starting the seq_file operation
1975 */
1976 prdebug_full_state_locked(0, "proc ctrl");
JP Abgrallbaf0db42011-06-20 12:41:46 -07001977 }
1978
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001979 return 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001980}
1981
1982/*
1983 * Delete socket tags, and stat tags associated with a given
1984 * accouting tag and uid.
1985 */
1986static int ctrl_cmd_delete(const char *input)
1987{
1988 char cmd;
John Stultzbd1bca42014-03-28 16:23:48 -07001989 int uid_int;
1990 kuid_t uid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001991 uid_t entry_uid;
1992 tag_t acct_tag;
1993 tag_t tag;
1994 int res, argc;
1995 struct iface_stat *iface_entry;
1996 struct rb_node *node;
1997 struct sock_tag *st_entry;
1998 struct rb_root st_to_free_tree = RB_ROOT;
1999 struct tag_stat *ts_entry;
2000 struct tag_counter_set *tcs_entry;
2001 struct tag_ref *tr_entry;
2002 struct uid_tag_data *utd_entry;
2003
John Stultzbd1bca42014-03-28 16:23:48 -07002004 argc = sscanf(input, "%c %llu %u", &cmd, &acct_tag, &uid_int);
2005 uid = make_kuid(&init_user_ns, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002006 CT_DEBUG("qtaguid: ctrl_delete(%s): argc=%d cmd=%c "
2007 "user_tag=0x%llx uid=%u\n", input, argc, cmd,
John Stultzbd1bca42014-03-28 16:23:48 -07002008 acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002009 if (argc < 2) {
2010 res = -EINVAL;
2011 goto err;
2012 }
2013 if (!valid_atag(acct_tag)) {
2014 pr_info("qtaguid: ctrl_delete(%s): invalid tag\n", input);
2015 res = -EINVAL;
2016 goto err;
2017 }
2018 if (argc < 3) {
2019 uid = current_fsuid();
John Stultzbd1bca42014-03-28 16:23:48 -07002020 uid_int = from_kuid(&init_user_ns, uid);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002021 } else if (!can_impersonate_uid(uid)) {
2022 pr_info("qtaguid: ctrl_delete(%s): "
2023 "insufficient priv from pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002024 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002025 res = -EPERM;
2026 goto err;
2027 }
2028
John Stultzbd1bca42014-03-28 16:23:48 -07002029 tag = combine_atag_with_uid(acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002030 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2031 "looking for tag=0x%llx (uid=%u)\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002032 input, tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002033
2034 /* Delete socket tags */
2035 spin_lock_bh(&sock_tag_list_lock);
2036 node = rb_first(&sock_tag_tree);
2037 while (node) {
2038 st_entry = rb_entry(node, struct sock_tag, sock_node);
2039 entry_uid = get_uid_from_tag(st_entry->tag);
2040 node = rb_next(node);
John Stultzbd1bca42014-03-28 16:23:48 -07002041 if (entry_uid != uid_int)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002042 continue;
2043
2044 CT_DEBUG("qtaguid: ctrl_delete(%s): st tag=0x%llx (uid=%u)\n",
2045 input, st_entry->tag, entry_uid);
2046
2047 if (!acct_tag || st_entry->tag == tag) {
2048 rb_erase(&st_entry->sock_node, &sock_tag_tree);
2049 /* Can't sockfd_put() within spinlock, do it later. */
2050 sock_tag_tree_insert(st_entry, &st_to_free_tree);
2051 tr_entry = lookup_tag_ref(st_entry->tag, NULL);
2052 BUG_ON(tr_entry->num_sock_tags <= 0);
2053 tr_entry->num_sock_tags--;
2054 /*
2055 * TODO: remove if, and start failing.
2056 * This is a hack to work around the fact that in some
2057 * places we have "if (IS_ERR_OR_NULL(pqd_entry))"
2058 * and are trying to work around apps
2059 * that didn't open the /dev/xt_qtaguid.
2060 */
2061 if (st_entry->list.next && st_entry->list.prev)
2062 list_del(&st_entry->list);
2063 }
2064 }
2065 spin_unlock_bh(&sock_tag_list_lock);
2066
2067 sock_tag_tree_erase(&st_to_free_tree);
2068
2069 /* Delete tag counter-sets */
2070 spin_lock_bh(&tag_counter_set_list_lock);
2071 /* Counter sets are only on the uid tag, not full tag */
2072 tcs_entry = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
2073 if (tcs_entry) {
2074 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2075 "erase tcs: tag=0x%llx (uid=%u) set=%d\n",
2076 input,
2077 tcs_entry->tn.tag,
2078 get_uid_from_tag(tcs_entry->tn.tag),
2079 tcs_entry->active_set);
2080 rb_erase(&tcs_entry->tn.node, &tag_counter_set_tree);
2081 kfree(tcs_entry);
2082 }
2083 spin_unlock_bh(&tag_counter_set_list_lock);
2084
2085 /*
2086 * If acct_tag is 0, then all entries belonging to uid are
2087 * erased.
2088 */
2089 spin_lock_bh(&iface_stat_list_lock);
2090 list_for_each_entry(iface_entry, &iface_stat_list, list) {
2091 spin_lock_bh(&iface_entry->tag_stat_list_lock);
2092 node = rb_first(&iface_entry->tag_stat_tree);
2093 while (node) {
2094 ts_entry = rb_entry(node, struct tag_stat, tn.node);
2095 entry_uid = get_uid_from_tag(ts_entry->tn.tag);
2096 node = rb_next(node);
2097
2098 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2099 "ts tag=0x%llx (uid=%u)\n",
2100 input, ts_entry->tn.tag, entry_uid);
2101
John Stultzbd1bca42014-03-28 16:23:48 -07002102 if (entry_uid != uid_int)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002103 continue;
2104 if (!acct_tag || ts_entry->tn.tag == tag) {
2105 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2106 "erase ts: %s 0x%llx %u\n",
2107 input, iface_entry->ifname,
2108 get_atag_from_tag(ts_entry->tn.tag),
2109 entry_uid);
2110 rb_erase(&ts_entry->tn.node,
2111 &iface_entry->tag_stat_tree);
2112 kfree(ts_entry);
2113 }
2114 }
2115 spin_unlock_bh(&iface_entry->tag_stat_list_lock);
2116 }
2117 spin_unlock_bh(&iface_stat_list_lock);
2118
2119 /* Cleanup the uid_tag_data */
2120 spin_lock_bh(&uid_tag_data_tree_lock);
2121 node = rb_first(&uid_tag_data_tree);
2122 while (node) {
2123 utd_entry = rb_entry(node, struct uid_tag_data, node);
2124 entry_uid = utd_entry->uid;
2125 node = rb_next(node);
2126
2127 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2128 "utd uid=%u\n",
2129 input, entry_uid);
2130
John Stultzbd1bca42014-03-28 16:23:48 -07002131 if (entry_uid != uid_int)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002132 continue;
2133 /*
2134 * Go over the tag_refs, and those that don't have
2135 * sock_tags using them are freed.
2136 */
2137 put_tag_ref_tree(tag, utd_entry);
2138 put_utd_entry(utd_entry);
2139 }
2140 spin_unlock_bh(&uid_tag_data_tree_lock);
2141
2142 atomic64_inc(&qtu_events.delete_cmds);
2143 res = 0;
2144
2145err:
2146 return res;
2147}
2148
2149static int ctrl_cmd_counter_set(const char *input)
2150{
2151 char cmd;
2152 uid_t uid = 0;
2153 tag_t tag;
2154 int res, argc;
2155 struct tag_counter_set *tcs;
2156 int counter_set;
2157
2158 argc = sscanf(input, "%c %d %u", &cmd, &counter_set, &uid);
2159 CT_DEBUG("qtaguid: ctrl_counterset(%s): argc=%d cmd=%c "
2160 "set=%d uid=%u\n", input, argc, cmd,
2161 counter_set, uid);
2162 if (argc != 3) {
2163 res = -EINVAL;
2164 goto err;
2165 }
2166 if (counter_set < 0 || counter_set >= IFS_MAX_COUNTER_SETS) {
2167 pr_info("qtaguid: ctrl_counterset(%s): invalid counter_set range\n",
2168 input);
2169 res = -EINVAL;
2170 goto err;
2171 }
2172 if (!can_manipulate_uids()) {
2173 pr_info("qtaguid: ctrl_counterset(%s): "
2174 "insufficient priv from pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002175 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002176 res = -EPERM;
2177 goto err;
2178 }
2179
2180 tag = make_tag_from_uid(uid);
2181 spin_lock_bh(&tag_counter_set_list_lock);
2182 tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
2183 if (!tcs) {
2184 tcs = kzalloc(sizeof(*tcs), GFP_ATOMIC);
2185 if (!tcs) {
2186 spin_unlock_bh(&tag_counter_set_list_lock);
2187 pr_err("qtaguid: ctrl_counterset(%s): "
2188 "failed to alloc counter set\n",
2189 input);
2190 res = -ENOMEM;
2191 goto err;
2192 }
2193 tcs->tn.tag = tag;
2194 tag_counter_set_tree_insert(tcs, &tag_counter_set_tree);
2195 CT_DEBUG("qtaguid: ctrl_counterset(%s): added tcs tag=0x%llx "
2196 "(uid=%u) set=%d\n",
2197 input, tag, get_uid_from_tag(tag), counter_set);
2198 }
2199 tcs->active_set = counter_set;
2200 spin_unlock_bh(&tag_counter_set_list_lock);
2201 atomic64_inc(&qtu_events.counter_set_changes);
2202 res = 0;
2203
2204err:
2205 return res;
2206}
2207
2208static int ctrl_cmd_tag(const char *input)
2209{
2210 char cmd;
2211 int sock_fd = 0;
John Stultzbd1bca42014-03-28 16:23:48 -07002212 kuid_t uid;
2213 unsigned int uid_int = 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002214 tag_t acct_tag = make_atag_from_value(0);
2215 tag_t full_tag;
2216 struct socket *el_socket;
2217 int res, argc;
2218 struct sock_tag *sock_tag_entry;
2219 struct tag_ref *tag_ref_entry;
2220 struct uid_tag_data *uid_tag_data_entry;
2221 struct proc_qtu_data *pqd_entry;
2222
2223 /* Unassigned args will get defaulted later. */
John Stultzbd1bca42014-03-28 16:23:48 -07002224 argc = sscanf(input, "%c %d %llu %u", &cmd, &sock_fd, &acct_tag, &uid_int);
2225 uid = make_kuid(&init_user_ns, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002226 CT_DEBUG("qtaguid: ctrl_tag(%s): argc=%d cmd=%c sock_fd=%d "
2227 "acct_tag=0x%llx uid=%u\n", input, argc, cmd, sock_fd,
John Stultzbd1bca42014-03-28 16:23:48 -07002228 acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002229 if (argc < 2) {
2230 res = -EINVAL;
2231 goto err;
2232 }
2233 el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */
2234 if (!el_socket) {
2235 pr_info("qtaguid: ctrl_tag(%s): failed to lookup"
JP Abgrall9e0858c2012-04-27 12:57:39 -07002236 " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n",
2237 input, sock_fd, res, current->pid, current->tgid,
John Stultzbd1bca42014-03-28 16:23:48 -07002238 from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002239 goto err;
2240 }
Chenbo Feng875e5262017-04-19 14:22:47 -07002241 CT_DEBUG("qtaguid: ctrl_tag(%s): socket->...->sk_refcnt=%d ->sk=%p\n",
2242 input, atomic_read(&el_socket->sk->sk_refcnt),
JP Abgrallbaf0db42011-06-20 12:41:46 -07002243 el_socket->sk);
2244 if (argc < 3) {
2245 acct_tag = make_atag_from_value(0);
2246 } else if (!valid_atag(acct_tag)) {
2247 pr_info("qtaguid: ctrl_tag(%s): invalid tag\n", input);
2248 res = -EINVAL;
2249 goto err_put;
2250 }
2251 CT_DEBUG("qtaguid: ctrl_tag(%s): "
2252 "pid=%u tgid=%u uid=%u euid=%u fsuid=%u "
JP Abgrall90414bc2013-01-04 18:18:36 -08002253 "ctrl.gid=%u in_group()=%d in_egroup()=%d\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002254 input, current->pid, current->tgid,
2255 from_kuid(&init_user_ns, current_uid()),
2256 from_kuid(&init_user_ns, current_euid()),
2257 from_kuid(&init_user_ns, current_fsuid()),
2258 from_kgid(&init_user_ns, xt_qtaguid_ctrl_file->gid),
JP Abgrall90414bc2013-01-04 18:18:36 -08002259 in_group_p(xt_qtaguid_ctrl_file->gid),
2260 in_egroup_p(xt_qtaguid_ctrl_file->gid));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002261 if (argc < 4) {
2262 uid = current_fsuid();
John Stultzbd1bca42014-03-28 16:23:48 -07002263 uid_int = from_kuid(&init_user_ns, uid);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002264 } else if (!can_impersonate_uid(uid)) {
2265 pr_info("qtaguid: ctrl_tag(%s): "
2266 "insufficient priv from pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002267 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002268 res = -EPERM;
2269 goto err_put;
2270 }
John Stultzbd1bca42014-03-28 16:23:48 -07002271 full_tag = combine_atag_with_uid(acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002272
2273 spin_lock_bh(&sock_tag_list_lock);
2274 sock_tag_entry = get_sock_stat_nl(el_socket->sk);
2275 tag_ref_entry = get_tag_ref(full_tag, &uid_tag_data_entry);
2276 if (IS_ERR(tag_ref_entry)) {
2277 res = PTR_ERR(tag_ref_entry);
2278 spin_unlock_bh(&sock_tag_list_lock);
2279 goto err_put;
2280 }
2281 tag_ref_entry->num_sock_tags++;
2282 if (sock_tag_entry) {
2283 struct tag_ref *prev_tag_ref_entry;
2284
2285 CT_DEBUG("qtaguid: ctrl_tag(%s): retag for sk=%p "
Chenbo Feng875e5262017-04-19 14:22:47 -07002286 "st@%p ...->sk_refcnt=%d\n",
JP Abgrallbaf0db42011-06-20 12:41:46 -07002287 input, el_socket->sk, sock_tag_entry,
Chenbo Feng875e5262017-04-19 14:22:47 -07002288 atomic_read(&el_socket->sk->sk_refcnt));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002289 prev_tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag,
2290 &uid_tag_data_entry);
2291 BUG_ON(IS_ERR_OR_NULL(prev_tag_ref_entry));
2292 BUG_ON(prev_tag_ref_entry->num_sock_tags <= 0);
2293 prev_tag_ref_entry->num_sock_tags--;
2294 sock_tag_entry->tag = full_tag;
2295 } else {
2296 CT_DEBUG("qtaguid: ctrl_tag(%s): newtag for sk=%p\n",
2297 input, el_socket->sk);
2298 sock_tag_entry = kzalloc(sizeof(*sock_tag_entry),
2299 GFP_ATOMIC);
2300 if (!sock_tag_entry) {
2301 pr_err("qtaguid: ctrl_tag(%s): "
2302 "socket tag alloc failed\n",
2303 input);
2304 spin_unlock_bh(&sock_tag_list_lock);
2305 res = -ENOMEM;
2306 goto err_tag_unref_put;
2307 }
Chenbo Feng875e5262017-04-19 14:22:47 -07002308 /*
2309 * Hold the sk refcount here to make sure the sk pointer cannot
2310 * be freed and reused
2311 */
2312 sock_hold(el_socket->sk);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002313 sock_tag_entry->sk = el_socket->sk;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002314 sock_tag_entry->pid = current->tgid;
John Stultzbd1bca42014-03-28 16:23:48 -07002315 sock_tag_entry->tag = combine_atag_with_uid(acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002316 spin_lock_bh(&uid_tag_data_tree_lock);
2317 pqd_entry = proc_qtu_data_tree_search(
2318 &proc_qtu_data_tree, current->tgid);
2319 /*
2320 * TODO: remove if, and start failing.
2321 * At first, we want to catch user-space code that is not
2322 * opening the /dev/xt_qtaguid.
2323 */
2324 if (IS_ERR_OR_NULL(pqd_entry))
2325 pr_warn_once(
2326 "qtaguid: %s(): "
2327 "User space forgot to open /dev/xt_qtaguid? "
2328 "pid=%u tgid=%u uid=%u\n", __func__,
2329 current->pid, current->tgid,
John Stultzbd1bca42014-03-28 16:23:48 -07002330 from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002331 else
2332 list_add(&sock_tag_entry->list,
2333 &pqd_entry->sock_tag_list);
2334 spin_unlock_bh(&uid_tag_data_tree_lock);
2335
2336 sock_tag_tree_insert(sock_tag_entry, &sock_tag_tree);
2337 atomic64_inc(&qtu_events.sockets_tagged);
2338 }
2339 spin_unlock_bh(&sock_tag_list_lock);
Chenbo Feng875e5262017-04-19 14:22:47 -07002340 /* We keep the ref to the sk until it is untagged */
2341 CT_DEBUG("qtaguid: ctrl_tag(%s): done st@%p ...->sk_refcnt=%d\n",
JP Abgrallbaf0db42011-06-20 12:41:46 -07002342 input, sock_tag_entry,
Chenbo Feng875e5262017-04-19 14:22:47 -07002343 atomic_read(&el_socket->sk->sk_refcnt));
2344 sockfd_put(el_socket);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002345 return 0;
2346
2347err_tag_unref_put:
2348 BUG_ON(tag_ref_entry->num_sock_tags <= 0);
2349 tag_ref_entry->num_sock_tags--;
2350 free_tag_ref_from_utd_entry(tag_ref_entry, uid_tag_data_entry);
2351err_put:
Chenbo Feng875e5262017-04-19 14:22:47 -07002352 CT_DEBUG("qtaguid: ctrl_tag(%s): done. ...->sk_refcnt=%d\n",
2353 input, atomic_read(&el_socket->sk->sk_refcnt) - 1);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002354 /* Release the sock_fd that was grabbed by sockfd_lookup(). */
2355 sockfd_put(el_socket);
2356 return res;
2357
2358err:
2359 CT_DEBUG("qtaguid: ctrl_tag(%s): done.\n", input);
2360 return res;
2361}
2362
2363static int ctrl_cmd_untag(const char *input)
2364{
2365 char cmd;
2366 int sock_fd = 0;
2367 struct socket *el_socket;
2368 int res, argc;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002369
2370 argc = sscanf(input, "%c %d", &cmd, &sock_fd);
2371 CT_DEBUG("qtaguid: ctrl_untag(%s): argc=%d cmd=%c sock_fd=%d\n",
2372 input, argc, cmd, sock_fd);
2373 if (argc < 2) {
2374 res = -EINVAL;
Chenbo Feng875e5262017-04-19 14:22:47 -07002375 return res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002376 }
2377 el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */
2378 if (!el_socket) {
2379 pr_info("qtaguid: ctrl_untag(%s): failed to lookup"
JP Abgrall9e0858c2012-04-27 12:57:39 -07002380 " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n",
2381 input, sock_fd, res, current->pid, current->tgid,
John Stultzbd1bca42014-03-28 16:23:48 -07002382 from_kuid(&init_user_ns, current_fsuid()));
Chenbo Feng875e5262017-04-19 14:22:47 -07002383 return res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002384 }
2385 CT_DEBUG("qtaguid: ctrl_untag(%s): socket->...->f_count=%ld ->sk=%p\n",
2386 input, atomic_long_read(&el_socket->file->f_count),
2387 el_socket->sk);
Chenbo Feng875e5262017-04-19 14:22:47 -07002388 res = qtaguid_untag(el_socket, false);
2389 sockfd_put(el_socket);
2390 return res;
2391}
2392
2393int qtaguid_untag(struct socket *el_socket, bool kernel)
2394{
2395 int res;
2396 pid_t pid;
2397 struct sock_tag *sock_tag_entry;
2398 struct tag_ref *tag_ref_entry;
2399 struct uid_tag_data *utd_entry;
2400 struct proc_qtu_data *pqd_entry;
2401
JP Abgrallbaf0db42011-06-20 12:41:46 -07002402 spin_lock_bh(&sock_tag_list_lock);
2403 sock_tag_entry = get_sock_stat_nl(el_socket->sk);
2404 if (!sock_tag_entry) {
2405 spin_unlock_bh(&sock_tag_list_lock);
2406 res = -EINVAL;
Chenbo Feng875e5262017-04-19 14:22:47 -07002407 return res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002408 }
2409 /*
2410 * The socket already belongs to the current process
2411 * so it can do whatever it wants to it.
2412 */
2413 rb_erase(&sock_tag_entry->sock_node, &sock_tag_tree);
2414
2415 tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag, &utd_entry);
2416 BUG_ON(!tag_ref_entry);
2417 BUG_ON(tag_ref_entry->num_sock_tags <= 0);
2418 spin_lock_bh(&uid_tag_data_tree_lock);
Chenbo Feng875e5262017-04-19 14:22:47 -07002419 if (kernel)
2420 pid = sock_tag_entry->pid;
2421 else
2422 pid = current->tgid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002423 pqd_entry = proc_qtu_data_tree_search(
Chenbo Feng875e5262017-04-19 14:22:47 -07002424 &proc_qtu_data_tree, pid);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002425 /*
2426 * TODO: remove if, and start failing.
2427 * At first, we want to catch user-space code that is not
2428 * opening the /dev/xt_qtaguid.
2429 */
Chenbo Feng875e5262017-04-19 14:22:47 -07002430 if (IS_ERR_OR_NULL(pqd_entry) || !sock_tag_entry->list.next) {
JP Abgrallbaf0db42011-06-20 12:41:46 -07002431 pr_warn_once("qtaguid: %s(): "
2432 "User space forgot to open /dev/xt_qtaguid? "
Chenbo Feng875e5262017-04-19 14:22:47 -07002433 "pid=%u tgid=%u sk_pid=%u, uid=%u\n", __func__,
2434 current->pid, current->tgid, sock_tag_entry->pid,
2435 from_kuid(&init_user_ns, current_fsuid()));
2436 } else {
JP Abgrallbaf0db42011-06-20 12:41:46 -07002437 list_del(&sock_tag_entry->list);
Chenbo Feng875e5262017-04-19 14:22:47 -07002438 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07002439 spin_unlock_bh(&uid_tag_data_tree_lock);
2440 /*
2441 * We don't free tag_ref from the utd_entry here,
2442 * only during a cmd_delete().
2443 */
2444 tag_ref_entry->num_sock_tags--;
2445 spin_unlock_bh(&sock_tag_list_lock);
2446 /*
Chenbo Feng875e5262017-04-19 14:22:47 -07002447 * Release the sock_fd that was grabbed at tag time.
JP Abgrallbaf0db42011-06-20 12:41:46 -07002448 */
Chenbo Feng875e5262017-04-19 14:22:47 -07002449 sock_put(sock_tag_entry->sk);
2450 CT_DEBUG("qtaguid: done. st@%p ...->sk_refcnt=%d\n",
2451 sock_tag_entry,
2452 atomic_read(&el_socket->sk->sk_refcnt));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002453
2454 kfree(sock_tag_entry);
2455 atomic64_inc(&qtu_events.sockets_untagged);
2456
2457 return 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002458}
2459
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002460static ssize_t qtaguid_ctrl_parse(const char *input, size_t count)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002461{
2462 char cmd;
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002463 ssize_t res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002464
JP Abgrall9e0858c2012-04-27 12:57:39 -07002465 CT_DEBUG("qtaguid: ctrl(%s): pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002466 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrall9e0858c2012-04-27 12:57:39 -07002467
JP Abgrallbaf0db42011-06-20 12:41:46 -07002468 cmd = input[0];
2469 /* Collect params for commands */
2470 switch (cmd) {
2471 case 'd':
2472 res = ctrl_cmd_delete(input);
2473 break;
2474
2475 case 's':
2476 res = ctrl_cmd_counter_set(input);
2477 break;
2478
2479 case 't':
2480 res = ctrl_cmd_tag(input);
2481 break;
2482
2483 case 'u':
2484 res = ctrl_cmd_untag(input);
2485 break;
2486
2487 default:
2488 res = -EINVAL;
2489 goto err;
2490 }
2491 if (!res)
2492 res = count;
2493err:
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002494 CT_DEBUG("qtaguid: ctrl(%s): res=%zd\n", input, res);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002495 return res;
2496}
2497
2498#define MAX_QTAGUID_CTRL_INPUT_LEN 255
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002499static ssize_t qtaguid_ctrl_proc_write(struct file *file, const char __user *buffer,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002500 size_t count, loff_t *offp)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002501{
2502 char input_buf[MAX_QTAGUID_CTRL_INPUT_LEN];
2503
2504 if (unlikely(module_passive))
2505 return count;
2506
2507 if (count >= MAX_QTAGUID_CTRL_INPUT_LEN)
2508 return -EINVAL;
2509
2510 if (copy_from_user(input_buf, buffer, count))
2511 return -EFAULT;
2512
2513 input_buf[count] = '\0';
2514 return qtaguid_ctrl_parse(input_buf, count);
2515}
2516
2517struct proc_print_info {
JP Abgrallbaf0db42011-06-20 12:41:46 -07002518 struct iface_stat *iface_entry;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002519 int item_index;
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002520 tag_t tag; /* tag found by reading to tag_pos */
2521 off_t tag_pos;
2522 int tag_item_index;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002523};
2524
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002525static void pp_stats_header(struct seq_file *m)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002526{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002527 seq_puts(m,
2528 "idx iface acct_tag_hex uid_tag_int cnt_set "
2529 "rx_bytes rx_packets "
2530 "tx_bytes tx_packets "
2531 "rx_tcp_bytes rx_tcp_packets "
2532 "rx_udp_bytes rx_udp_packets "
2533 "rx_other_bytes rx_other_packets "
2534 "tx_tcp_bytes tx_tcp_packets "
2535 "tx_udp_bytes tx_udp_packets "
2536 "tx_other_bytes tx_other_packets\n");
JP Abgrallbaf0db42011-06-20 12:41:46 -07002537}
2538
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002539static int pp_stats_line(struct seq_file *m, struct tag_stat *ts_entry,
2540 int cnt_set)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002541{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002542 struct data_counters *cnts;
2543 tag_t tag = ts_entry->tn.tag;
2544 uid_t stat_uid = get_uid_from_tag(tag);
2545 struct proc_print_info *ppi = m->private;
2546 /* Detailed tags are not available to everybody */
Mohamad Ayyash8613d932016-05-11 13:18:35 -07002547 if (!can_read_other_uid_stats(make_kuid(&init_user_ns,stat_uid))) {
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002548 CT_DEBUG("qtaguid: stats line: "
2549 "%s 0x%llx %u: insufficient priv "
2550 "from pid=%u tgid=%u uid=%u stats.gid=%u\n",
2551 ppi->iface_entry->ifname,
2552 get_atag_from_tag(tag), stat_uid,
John Stultzbd1bca42014-03-28 16:23:48 -07002553 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()),
2554 from_kgid(&init_user_ns,xt_qtaguid_stats_file->gid));
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002555 return 0;
2556 }
2557 ppi->item_index++;
2558 cnts = &ts_entry->counters;
Amit Pundir5b5ab942015-10-01 10:44:36 +05302559 seq_printf(m, "%d %s 0x%llx %u %u "
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002560 "%llu %llu "
2561 "%llu %llu "
2562 "%llu %llu "
2563 "%llu %llu "
2564 "%llu %llu "
2565 "%llu %llu "
2566 "%llu %llu "
2567 "%llu %llu\n",
2568 ppi->item_index,
2569 ppi->iface_entry->ifname,
2570 get_atag_from_tag(tag),
2571 stat_uid,
2572 cnt_set,
2573 dc_sum_bytes(cnts, cnt_set, IFS_RX),
2574 dc_sum_packets(cnts, cnt_set, IFS_RX),
2575 dc_sum_bytes(cnts, cnt_set, IFS_TX),
2576 dc_sum_packets(cnts, cnt_set, IFS_TX),
2577 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes,
2578 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets,
2579 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes,
2580 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets,
2581 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes,
2582 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets,
2583 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes,
2584 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets,
2585 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes,
2586 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets,
2587 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes,
2588 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets);
Amit Pundir5b5ab942015-10-01 10:44:36 +05302589 return seq_has_overflowed(m) ? -ENOSPC : 1;
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002590}
2591
2592static bool pp_sets(struct seq_file *m, struct tag_stat *ts_entry)
2593{
2594 int ret;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002595 int counter_set;
2596 for (counter_set = 0; counter_set < IFS_MAX_COUNTER_SETS;
2597 counter_set++) {
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002598 ret = pp_stats_line(m, ts_entry, counter_set);
2599 if (ret < 0)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002600 return false;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002601 }
2602 return true;
2603}
2604
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002605static int qtaguid_stats_proc_iface_stat_ptr_valid(struct iface_stat *ptr)
2606{
2607 struct iface_stat *iface_entry;
2608
2609 if (!ptr)
2610 return false;
2611
2612 list_for_each_entry(iface_entry, &iface_stat_list, list)
2613 if (iface_entry == ptr)
2614 return true;
2615 return false;
2616}
2617
2618static void qtaguid_stats_proc_next_iface_entry(struct proc_print_info *ppi)
2619{
2620 spin_unlock_bh(&ppi->iface_entry->tag_stat_list_lock);
2621 list_for_each_entry_continue(ppi->iface_entry, &iface_stat_list, list) {
2622 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2623 return;
2624 }
2625 ppi->iface_entry = NULL;
2626}
2627
2628static void *qtaguid_stats_proc_next(struct seq_file *m, void *v, loff_t *pos)
2629{
2630 struct proc_print_info *ppi = m->private;
2631 struct tag_stat *ts_entry;
2632 struct rb_node *node;
2633
2634 if (!v) {
2635 pr_err("qtaguid: %s(): unexpected v: NULL\n", __func__);
2636 return NULL;
2637 }
2638
2639 (*pos)++;
2640
2641 if (!ppi->iface_entry || unlikely(module_passive))
2642 return NULL;
2643
2644 if (v == SEQ_START_TOKEN)
2645 node = rb_first(&ppi->iface_entry->tag_stat_tree);
2646 else
2647 node = rb_next(&((struct tag_stat *)v)->tn.node);
2648
2649 while (!node) {
2650 qtaguid_stats_proc_next_iface_entry(ppi);
2651 if (!ppi->iface_entry)
2652 return NULL;
2653 node = rb_first(&ppi->iface_entry->tag_stat_tree);
2654 }
2655
2656 ts_entry = rb_entry(node, struct tag_stat, tn.node);
2657 ppi->tag = ts_entry->tn.tag;
2658 ppi->tag_pos = *pos;
2659 ppi->tag_item_index = ppi->item_index;
2660 return ts_entry;
2661}
2662
2663static void *qtaguid_stats_proc_start(struct seq_file *m, loff_t *pos)
2664{
2665 struct proc_print_info *ppi = m->private;
2666 struct tag_stat *ts_entry = NULL;
2667
2668 spin_lock_bh(&iface_stat_list_lock);
2669
2670 if (*pos == 0) {
2671 ppi->item_index = 1;
2672 ppi->tag_pos = 0;
2673 if (list_empty(&iface_stat_list)) {
2674 ppi->iface_entry = NULL;
2675 } else {
2676 ppi->iface_entry = list_first_entry(&iface_stat_list,
2677 struct iface_stat,
2678 list);
2679 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2680 }
2681 return SEQ_START_TOKEN;
2682 }
2683 if (!qtaguid_stats_proc_iface_stat_ptr_valid(ppi->iface_entry)) {
2684 if (ppi->iface_entry) {
2685 pr_err("qtaguid: %s(): iface_entry %p not found\n",
2686 __func__, ppi->iface_entry);
2687 ppi->iface_entry = NULL;
2688 }
2689 return NULL;
2690 }
2691
2692 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2693
2694 if (!ppi->tag_pos) {
2695 /* seq_read skipped first next call */
2696 ts_entry = SEQ_START_TOKEN;
2697 } else {
2698 ts_entry = tag_stat_tree_search(
2699 &ppi->iface_entry->tag_stat_tree, ppi->tag);
2700 if (!ts_entry) {
2701 pr_info("qtaguid: %s(): tag_stat.tag 0x%llx not found. Abort.\n",
2702 __func__, ppi->tag);
2703 return NULL;
2704 }
2705 }
2706
2707 if (*pos == ppi->tag_pos) { /* normal resume */
2708 ppi->item_index = ppi->tag_item_index;
2709 } else {
2710 /* seq_read skipped a next call */
2711 *pos = ppi->tag_pos;
2712 ts_entry = qtaguid_stats_proc_next(m, ts_entry, pos);
2713 }
2714
2715 return ts_entry;
2716}
2717
2718static void qtaguid_stats_proc_stop(struct seq_file *m, void *v)
2719{
2720 struct proc_print_info *ppi = m->private;
2721 if (ppi->iface_entry)
2722 spin_unlock_bh(&ppi->iface_entry->tag_stat_list_lock);
2723 spin_unlock_bh(&iface_stat_list_lock);
2724}
2725
JP Abgrallbaf0db42011-06-20 12:41:46 -07002726/*
2727 * Procfs reader to get all tag stats using style "1)" as described in
2728 * fs/proc/generic.c
2729 * Groups all protocols tx/rx bytes.
2730 */
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002731static int qtaguid_stats_proc_show(struct seq_file *m, void *v)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002732{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002733 struct tag_stat *ts_entry = v;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002734
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002735 if (v == SEQ_START_TOKEN)
2736 pp_stats_header(m);
2737 else
2738 pp_sets(m, ts_entry);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002739
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002740 return 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002741}
2742
2743/*------------------------------------------*/
2744static int qtudev_open(struct inode *inode, struct file *file)
2745{
2746 struct uid_tag_data *utd_entry;
2747 struct proc_qtu_data *pqd_entry;
2748 struct proc_qtu_data *new_pqd_entry;
2749 int res;
2750 bool utd_entry_found;
2751
2752 if (unlikely(qtu_proc_handling_passive))
2753 return 0;
2754
2755 DR_DEBUG("qtaguid: qtudev_open(): pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002756 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002757
2758 spin_lock_bh(&uid_tag_data_tree_lock);
2759
2760 /* Look for existing uid data, or alloc one. */
John Stultzbd1bca42014-03-28 16:23:48 -07002761 utd_entry = get_uid_data(from_kuid(&init_user_ns, current_fsuid()), &utd_entry_found);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002762 if (IS_ERR_OR_NULL(utd_entry)) {
2763 res = PTR_ERR(utd_entry);
JP Abgrallb79c36f12012-10-09 20:38:21 -07002764 goto err_unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002765 }
2766
2767 /* Look for existing PID based proc_data */
2768 pqd_entry = proc_qtu_data_tree_search(&proc_qtu_data_tree,
2769 current->tgid);
2770 if (pqd_entry) {
2771 pr_err("qtaguid: qtudev_open(): %u/%u %u "
2772 "%s already opened\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002773 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()),
JP Abgrallbaf0db42011-06-20 12:41:46 -07002774 QTU_DEV_NAME);
2775 res = -EBUSY;
2776 goto err_unlock_free_utd;
2777 }
2778
2779 new_pqd_entry = kzalloc(sizeof(*new_pqd_entry), GFP_ATOMIC);
2780 if (!new_pqd_entry) {
2781 pr_err("qtaguid: qtudev_open(): %u/%u %u: "
2782 "proc data alloc failed\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002783 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002784 res = -ENOMEM;
2785 goto err_unlock_free_utd;
2786 }
2787 new_pqd_entry->pid = current->tgid;
2788 INIT_LIST_HEAD(&new_pqd_entry->sock_tag_list);
2789 new_pqd_entry->parent_tag_data = utd_entry;
2790 utd_entry->num_pqd++;
2791
2792 proc_qtu_data_tree_insert(new_pqd_entry,
2793 &proc_qtu_data_tree);
2794
2795 spin_unlock_bh(&uid_tag_data_tree_lock);
2796 DR_DEBUG("qtaguid: tracking data for uid=%u in pqd=%p\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002797 from_kuid(&init_user_ns, current_fsuid()), new_pqd_entry);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002798 file->private_data = new_pqd_entry;
2799 return 0;
2800
2801err_unlock_free_utd:
2802 if (!utd_entry_found) {
2803 rb_erase(&utd_entry->node, &uid_tag_data_tree);
2804 kfree(utd_entry);
2805 }
JP Abgrallb79c36f12012-10-09 20:38:21 -07002806err_unlock:
JP Abgrallbaf0db42011-06-20 12:41:46 -07002807 spin_unlock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002808 return res;
2809}
2810
2811static int qtudev_release(struct inode *inode, struct file *file)
2812{
2813 struct proc_qtu_data *pqd_entry = file->private_data;
2814 struct uid_tag_data *utd_entry = pqd_entry->parent_tag_data;
2815 struct sock_tag *st_entry;
2816 struct rb_root st_to_free_tree = RB_ROOT;
2817 struct list_head *entry, *next;
2818 struct tag_ref *tr;
2819
2820 if (unlikely(qtu_proc_handling_passive))
2821 return 0;
2822
2823 /*
2824 * Do not trust the current->pid, it might just be a kworker cleaning
2825 * up after a dead proc.
2826 */
2827 DR_DEBUG("qtaguid: qtudev_release(): "
2828 "pid=%u tgid=%u uid=%u "
2829 "pqd_entry=%p->pid=%u utd_entry=%p->active_tags=%d\n",
2830 current->pid, current->tgid, pqd_entry->parent_tag_data->uid,
2831 pqd_entry, pqd_entry->pid, utd_entry,
2832 utd_entry->num_active_tags);
2833
2834 spin_lock_bh(&sock_tag_list_lock);
2835 spin_lock_bh(&uid_tag_data_tree_lock);
2836
2837 list_for_each_safe(entry, next, &pqd_entry->sock_tag_list) {
2838 st_entry = list_entry(entry, struct sock_tag, list);
2839 DR_DEBUG("qtaguid: %s(): "
2840 "erase sock_tag=%p->sk=%p pid=%u tgid=%u uid=%u\n",
2841 __func__,
2842 st_entry, st_entry->sk,
2843 current->pid, current->tgid,
2844 pqd_entry->parent_tag_data->uid);
2845
2846 utd_entry = uid_tag_data_tree_search(
2847 &uid_tag_data_tree,
2848 get_uid_from_tag(st_entry->tag));
2849 BUG_ON(IS_ERR_OR_NULL(utd_entry));
2850 DR_DEBUG("qtaguid: %s(): "
2851 "looking for tag=0x%llx in utd_entry=%p\n", __func__,
2852 st_entry->tag, utd_entry);
2853 tr = tag_ref_tree_search(&utd_entry->tag_ref_tree,
2854 st_entry->tag);
2855 BUG_ON(!tr);
2856 BUG_ON(tr->num_sock_tags <= 0);
2857 tr->num_sock_tags--;
2858 free_tag_ref_from_utd_entry(tr, utd_entry);
2859
2860 rb_erase(&st_entry->sock_node, &sock_tag_tree);
2861 list_del(&st_entry->list);
2862 /* Can't sockfd_put() within spinlock, do it later. */
2863 sock_tag_tree_insert(st_entry, &st_to_free_tree);
2864
2865 /*
2866 * Try to free the utd_entry if no other proc_qtu_data is
2867 * using it (num_pqd is 0) and it doesn't have active tags
2868 * (num_active_tags is 0).
2869 */
2870 put_utd_entry(utd_entry);
2871 }
2872
2873 rb_erase(&pqd_entry->node, &proc_qtu_data_tree);
2874 BUG_ON(pqd_entry->parent_tag_data->num_pqd < 1);
2875 pqd_entry->parent_tag_data->num_pqd--;
2876 put_utd_entry(pqd_entry->parent_tag_data);
2877 kfree(pqd_entry);
2878 file->private_data = NULL;
2879
2880 spin_unlock_bh(&uid_tag_data_tree_lock);
2881 spin_unlock_bh(&sock_tag_list_lock);
2882
2883
2884 sock_tag_tree_erase(&st_to_free_tree);
2885
Chenbo Feng64157f42017-03-23 13:51:24 -07002886 spin_lock_bh(&sock_tag_list_lock);
2887 prdebug_full_state_locked(0, "%s(): pid=%u tgid=%u", __func__,
JP Abgrallbaf0db42011-06-20 12:41:46 -07002888 current->pid, current->tgid);
Chenbo Feng64157f42017-03-23 13:51:24 -07002889 spin_unlock_bh(&sock_tag_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002890 return 0;
2891}
2892
2893/*------------------------------------------*/
2894static const struct file_operations qtudev_fops = {
2895 .owner = THIS_MODULE,
2896 .open = qtudev_open,
2897 .release = qtudev_release,
2898};
2899
2900static struct miscdevice qtu_device = {
2901 .minor = MISC_DYNAMIC_MINOR,
2902 .name = QTU_DEV_NAME,
2903 .fops = &qtudev_fops,
2904 /* How sad it doesn't allow for defaults: .mode = S_IRUGO | S_IWUSR */
2905};
2906
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002907static const struct seq_operations proc_qtaguid_ctrl_seqops = {
2908 .start = qtaguid_ctrl_proc_start,
2909 .next = qtaguid_ctrl_proc_next,
2910 .stop = qtaguid_ctrl_proc_stop,
2911 .show = qtaguid_ctrl_proc_show,
2912};
2913
2914static int proc_qtaguid_ctrl_open(struct inode *inode, struct file *file)
2915{
2916 return seq_open_private(file, &proc_qtaguid_ctrl_seqops,
2917 sizeof(struct proc_ctrl_print_info));
2918}
2919
2920static const struct file_operations proc_qtaguid_ctrl_fops = {
2921 .open = proc_qtaguid_ctrl_open,
2922 .read = seq_read,
2923 .write = qtaguid_ctrl_proc_write,
2924 .llseek = seq_lseek,
Greg Hackmann56472912013-12-04 17:39:27 -08002925 .release = seq_release_private,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002926};
2927
2928static const struct seq_operations proc_qtaguid_stats_seqops = {
2929 .start = qtaguid_stats_proc_start,
2930 .next = qtaguid_stats_proc_next,
2931 .stop = qtaguid_stats_proc_stop,
2932 .show = qtaguid_stats_proc_show,
2933};
2934
2935static int proc_qtaguid_stats_open(struct inode *inode, struct file *file)
2936{
2937 return seq_open_private(file, &proc_qtaguid_stats_seqops,
2938 sizeof(struct proc_print_info));
2939}
2940
2941static const struct file_operations proc_qtaguid_stats_fops = {
2942 .open = proc_qtaguid_stats_open,
2943 .read = seq_read,
2944 .llseek = seq_lseek,
2945 .release = seq_release_private,
2946};
2947
JP Abgrallbaf0db42011-06-20 12:41:46 -07002948/*------------------------------------------*/
2949static int __init qtaguid_proc_register(struct proc_dir_entry **res_procdir)
2950{
2951 int ret;
2952 *res_procdir = proc_mkdir(module_procdirname, init_net.proc_net);
2953 if (!*res_procdir) {
2954 pr_err("qtaguid: failed to create proc/.../xt_qtaguid\n");
2955 ret = -ENOMEM;
2956 goto no_dir;
2957 }
2958
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002959 xt_qtaguid_ctrl_file = proc_create_data("ctrl", proc_ctrl_perms,
2960 *res_procdir,
2961 &proc_qtaguid_ctrl_fops,
2962 NULL);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002963 if (!xt_qtaguid_ctrl_file) {
2964 pr_err("qtaguid: failed to create xt_qtaguid/ctrl "
2965 " file\n");
2966 ret = -ENOMEM;
2967 goto no_ctrl_entry;
2968 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07002969
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002970 xt_qtaguid_stats_file = proc_create_data("stats", proc_stats_perms,
2971 *res_procdir,
2972 &proc_qtaguid_stats_fops,
2973 NULL);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002974 if (!xt_qtaguid_stats_file) {
2975 pr_err("qtaguid: failed to create xt_qtaguid/stats "
2976 "file\n");
2977 ret = -ENOMEM;
2978 goto no_stats_entry;
2979 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07002980 /*
2981 * TODO: add support counter hacking
2982 * xt_qtaguid_stats_file->write_proc = qtaguid_stats_proc_write;
2983 */
2984 return 0;
2985
2986no_stats_entry:
2987 remove_proc_entry("ctrl", *res_procdir);
2988no_ctrl_entry:
2989 remove_proc_entry("xt_qtaguid", NULL);
2990no_dir:
2991 return ret;
2992}
2993
2994static struct xt_match qtaguid_mt_reg __read_mostly = {
2995 /*
2996 * This module masquerades as the "owner" module so that iptables
2997 * tools can deal with it.
2998 */
2999 .name = "owner",
3000 .revision = 1,
3001 .family = NFPROTO_UNSPEC,
3002 .match = qtaguid_mt,
3003 .matchsize = sizeof(struct xt_qtaguid_match_info),
3004 .me = THIS_MODULE,
3005};
3006
3007static int __init qtaguid_mt_init(void)
3008{
3009 if (qtaguid_proc_register(&xt_qtaguid_procdir)
3010 || iface_stat_init(xt_qtaguid_procdir)
3011 || xt_register_match(&qtaguid_mt_reg)
3012 || misc_register(&qtu_device))
3013 return -1;
3014 return 0;
3015}
3016
3017/*
3018 * TODO: allow unloading of the module.
3019 * For now stats are permanent.
3020 * Kconfig forces'y/n' and never an 'm'.
3021 */
3022
3023module_init(qtaguid_mt_init);
3024MODULE_AUTHOR("jpa <jpa@google.com>");
3025MODULE_DESCRIPTION("Xtables: socket owner+tag matching and associated stats");
3026MODULE_LICENSE("GPL");
3027MODULE_ALIAS("ipt_owner");
3028MODULE_ALIAS("ip6t_owner");
3029MODULE_ALIAS("ipt_qtaguid");
3030MODULE_ALIAS("ip6t_qtaguid");