blob: 822be06f7c918d201af7b52a351f1564bef93708 [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 Abgrall89d32dc2013-12-20 16:51:11 -08001177/* Guarantied to return a net_device that has a name */
1178static void get_dev_and_dir(const struct sk_buff *skb,
1179 struct xt_action_param *par,
1180 enum ifs_tx_rx *direction,
1181 const struct net_device **el_dev)
1182{
1183 BUG_ON(!direction || !el_dev);
1184
1185 if (par->in) {
1186 *el_dev = par->in;
1187 *direction = IFS_RX;
1188 } else if (par->out) {
1189 *el_dev = par->out;
1190 *direction = IFS_TX;
1191 } else {
1192 pr_err("qtaguid[%d]: %s(): no par->in/out?!!\n",
1193 par->hooknum, __func__);
1194 BUG();
1195 }
1196 if (unlikely(!(*el_dev)->name)) {
1197 pr_err("qtaguid[%d]: %s(): no dev->name?!!\n",
1198 par->hooknum, __func__);
1199 BUG();
1200 }
1201 if (skb->dev && *el_dev != skb->dev) {
Kyle Yancd02e632017-10-06 10:33:18 -07001202 MT_DEBUG("qtaguid[%d]: skb->dev=%pK %s vs par->%s=%pK %s\n",
JP Abgrall89d32dc2013-12-20 16:51:11 -08001203 par->hooknum, skb->dev, skb->dev->name,
1204 *direction == IFS_RX ? "in" : "out", *el_dev,
1205 (*el_dev)->name);
1206 }
1207}
1208
JP Abgrall9e0858c2012-04-27 12:57:39 -07001209/*
1210 * Update stats for the specified interface from the skb.
1211 * Do nothing if the entry
1212 * does not exist (when a device was never configured with an IP address).
1213 * Called on each sk.
1214 */
1215static void iface_stat_update_from_skb(const struct sk_buff *skb,
1216 struct xt_action_param *par)
1217{
1218 struct iface_stat *entry;
1219 const struct net_device *el_dev;
JP Abgrall89d32dc2013-12-20 16:51:11 -08001220 enum ifs_tx_rx direction;
JP Abgrall9e0858c2012-04-27 12:57:39 -07001221 int bytes = skb->len;
JP Abgrall87f93e82013-01-28 16:50:44 -08001222 int proto;
JP Abgrall9e0858c2012-04-27 12:57:39 -07001223
JP Abgrall89d32dc2013-12-20 16:51:11 -08001224 get_dev_and_dir(skb, par, &direction, &el_dev);
1225 proto = ipx_proto(skb, par);
1226 MT_DEBUG("qtaguid[%d]: iface_stat: %s(%s): "
1227 "type=%d fam=%d proto=%d dir=%d\n",
1228 par->hooknum, __func__, el_dev->name, el_dev->type,
1229 par->family, proto, direction);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001230
1231 spin_lock_bh(&iface_stat_list_lock);
1232 entry = get_iface_entry(el_dev->name);
1233 if (entry == NULL) {
JP Abgrall89d32dc2013-12-20 16:51:11 -08001234 IF_DEBUG("qtaguid[%d]: iface_stat: %s(%s): not tracked\n",
1235 par->hooknum, __func__, el_dev->name);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001236 spin_unlock_bh(&iface_stat_list_lock);
1237 return;
1238 }
1239
JP Abgrall89d32dc2013-12-20 16:51:11 -08001240 IF_DEBUG("qtaguid[%d]: %s(%s): entry=%p\n", par->hooknum, __func__,
JP Abgrall9e0858c2012-04-27 12:57:39 -07001241 el_dev->name, entry);
1242
JP Abgrall87f93e82013-01-28 16:50:44 -08001243 data_counters_update(&entry->totals_via_skb, 0, direction, proto,
1244 bytes);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001245 spin_unlock_bh(&iface_stat_list_lock);
1246}
1247
JP Abgrallbaf0db42011-06-20 12:41:46 -07001248static void tag_stat_update(struct tag_stat *tag_entry,
1249 enum ifs_tx_rx direction, int proto, int bytes)
1250{
1251 int active_set;
1252 active_set = get_active_counter_set(tag_entry->tn.tag);
1253 MT_DEBUG("qtaguid: tag_stat_update(tag=0x%llx (uid=%u) set=%d "
1254 "dir=%d proto=%d bytes=%d)\n",
1255 tag_entry->tn.tag, get_uid_from_tag(tag_entry->tn.tag),
1256 active_set, direction, proto, bytes);
1257 data_counters_update(&tag_entry->counters, active_set, direction,
1258 proto, bytes);
1259 if (tag_entry->parent_counters)
1260 data_counters_update(tag_entry->parent_counters, active_set,
1261 direction, proto, bytes);
1262}
1263
1264/*
1265 * Create a new entry for tracking the specified {acct_tag,uid_tag} within
1266 * the interface.
1267 * iface_entry->tag_stat_list_lock should be held.
1268 */
1269static struct tag_stat *create_if_tag_stat(struct iface_stat *iface_entry,
1270 tag_t tag)
1271{
1272 struct tag_stat *new_tag_stat_entry = NULL;
1273 IF_DEBUG("qtaguid: iface_stat: %s(): ife=%p tag=0x%llx"
1274 " (uid=%u)\n", __func__,
1275 iface_entry, tag, get_uid_from_tag(tag));
1276 new_tag_stat_entry = kzalloc(sizeof(*new_tag_stat_entry), GFP_ATOMIC);
1277 if (!new_tag_stat_entry) {
1278 pr_err("qtaguid: iface_stat: tag stat alloc failed\n");
1279 goto done;
1280 }
1281 new_tag_stat_entry->tn.tag = tag;
1282 tag_stat_tree_insert(new_tag_stat_entry, &iface_entry->tag_stat_tree);
1283done:
1284 return new_tag_stat_entry;
1285}
1286
1287static void if_tag_stat_update(const char *ifname, uid_t uid,
1288 const struct sock *sk, enum ifs_tx_rx direction,
1289 int proto, int bytes)
1290{
1291 struct tag_stat *tag_stat_entry;
1292 tag_t tag, acct_tag;
1293 tag_t uid_tag;
1294 struct data_counters *uid_tag_counters;
1295 struct sock_tag *sock_tag_entry;
1296 struct iface_stat *iface_entry;
JP Abgralle5d79862012-04-13 19:22:35 -07001297 struct tag_stat *new_tag_stat = NULL;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001298 MT_DEBUG("qtaguid: if_tag_stat_update(ifname=%s "
1299 "uid=%u sk=%p dir=%d proto=%d bytes=%d)\n",
1300 ifname, uid, sk, direction, proto, bytes);
1301
liping.zhangf84e6a12016-01-11 13:31:01 +08001302 spin_lock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001303 iface_entry = get_iface_entry(ifname);
1304 if (!iface_entry) {
JP Abgrall89d32dc2013-12-20 16:51:11 -08001305 pr_err_ratelimited("qtaguid: tag_stat: stat_update() "
JP Abgralla2e371b2013-04-08 15:09:26 -07001306 "%s not found\n", ifname);
liping.zhangf84e6a12016-01-11 13:31:01 +08001307 spin_unlock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001308 return;
1309 }
1310 /* It is ok to process data when an iface_entry is inactive */
1311
Kyle Yancd02e632017-10-06 10:33:18 -07001312 MT_DEBUG("qtaguid: tag_stat: stat_update() dev=%s entry=%pK\n",
JP Abgrallbaf0db42011-06-20 12:41:46 -07001313 ifname, iface_entry);
1314
1315 /*
1316 * Look for a tagged sock.
1317 * It will have an acct_uid.
1318 */
1319 sock_tag_entry = get_sock_stat(sk);
1320 if (sock_tag_entry) {
1321 tag = sock_tag_entry->tag;
1322 acct_tag = get_atag_from_tag(tag);
1323 uid_tag = get_utag_from_tag(tag);
1324 } else {
1325 acct_tag = make_atag_from_value(0);
1326 tag = combine_atag_with_uid(acct_tag, uid);
1327 uid_tag = make_tag_from_uid(uid);
1328 }
JP Abgrall89d32dc2013-12-20 16:51:11 -08001329 MT_DEBUG("qtaguid: tag_stat: stat_update(): "
Kyle Yancd02e632017-10-06 10:33:18 -07001330 " looking for tag=0x%llx (uid=%u) in ife=%pK\n",
JP Abgrallbaf0db42011-06-20 12:41:46 -07001331 tag, get_uid_from_tag(tag), iface_entry);
1332 /* Loop over tag list under this interface for {acct_tag,uid_tag} */
1333 spin_lock_bh(&iface_entry->tag_stat_list_lock);
1334
1335 tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree,
1336 tag);
1337 if (tag_stat_entry) {
1338 /*
1339 * Updating the {acct_tag, uid_tag} entry handles both stats:
1340 * {0, uid_tag} will also get updated.
1341 */
1342 tag_stat_update(tag_stat_entry, direction, proto, bytes);
liping.zhangf84e6a12016-01-11 13:31:01 +08001343 goto unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001344 }
1345
1346 /* Loop over tag list under this interface for {0,uid_tag} */
1347 tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree,
1348 uid_tag);
1349 if (!tag_stat_entry) {
1350 /* Here: the base uid_tag did not exist */
1351 /*
1352 * No parent counters. So
1353 * - No {0, uid_tag} stats and no {acc_tag, uid_tag} stats.
1354 */
1355 new_tag_stat = create_if_tag_stat(iface_entry, uid_tag);
Pontus Fuchsb842ea52012-11-19 11:44:51 -08001356 if (!new_tag_stat)
1357 goto unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001358 uid_tag_counters = &new_tag_stat->counters;
1359 } else {
1360 uid_tag_counters = &tag_stat_entry->counters;
1361 }
1362
1363 if (acct_tag) {
JP Abgralle5d79862012-04-13 19:22:35 -07001364 /* Create the child {acct_tag, uid_tag} and hook up parent. */
JP Abgrallbaf0db42011-06-20 12:41:46 -07001365 new_tag_stat = create_if_tag_stat(iface_entry, tag);
Pontus Fuchsb842ea52012-11-19 11:44:51 -08001366 if (!new_tag_stat)
1367 goto unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001368 new_tag_stat->parent_counters = uid_tag_counters;
JP Abgralle5d79862012-04-13 19:22:35 -07001369 } else {
1370 /*
1371 * For new_tag_stat to be still NULL here would require:
1372 * {0, uid_tag} exists
1373 * and {acct_tag, uid_tag} doesn't exist
1374 * AND acct_tag == 0.
1375 * Impossible. This reassures us that new_tag_stat
1376 * below will always be assigned.
1377 */
1378 BUG_ON(!new_tag_stat);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001379 }
1380 tag_stat_update(new_tag_stat, direction, proto, bytes);
Pontus Fuchsb842ea52012-11-19 11:44:51 -08001381unlock:
JP Abgrallbaf0db42011-06-20 12:41:46 -07001382 spin_unlock_bh(&iface_entry->tag_stat_list_lock);
liping.zhangf84e6a12016-01-11 13:31:01 +08001383 spin_unlock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001384}
1385
1386static int iface_netdev_event_handler(struct notifier_block *nb,
1387 unsigned long event, void *ptr) {
Jon Medhurst (Tixy)a347e8e2014-04-14 21:20:49 -07001388 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001389
1390 if (unlikely(module_passive))
1391 return NOTIFY_DONE;
1392
1393 IF_DEBUG("qtaguid: iface_stat: netdev_event(): "
1394 "ev=0x%lx/%s netdev=%p->name=%s\n",
1395 event, netdev_evt_str(event), dev, dev ? dev->name : "");
1396
1397 switch (event) {
1398 case NETDEV_UP:
1399 iface_stat_create(dev, NULL);
1400 atomic64_inc(&qtu_events.iface_events);
1401 break;
1402 case NETDEV_DOWN:
1403 case NETDEV_UNREGISTER:
1404 iface_stat_update(dev, event == NETDEV_DOWN);
1405 atomic64_inc(&qtu_events.iface_events);
1406 break;
1407 }
1408 return NOTIFY_DONE;
1409}
1410
1411static int iface_inet6addr_event_handler(struct notifier_block *nb,
1412 unsigned long event, void *ptr)
1413{
1414 struct inet6_ifaddr *ifa = ptr;
1415 struct net_device *dev;
1416
1417 if (unlikely(module_passive))
1418 return NOTIFY_DONE;
1419
1420 IF_DEBUG("qtaguid: iface_stat: inet6addr_event(): "
1421 "ev=0x%lx/%s ifa=%p\n",
1422 event, netdev_evt_str(event), ifa);
1423
1424 switch (event) {
1425 case NETDEV_UP:
1426 BUG_ON(!ifa || !ifa->idev);
1427 dev = (struct net_device *)ifa->idev->dev;
1428 iface_stat_create_ipv6(dev, ifa);
1429 atomic64_inc(&qtu_events.iface_events);
1430 break;
1431 case NETDEV_DOWN:
1432 case NETDEV_UNREGISTER:
1433 BUG_ON(!ifa || !ifa->idev);
1434 dev = (struct net_device *)ifa->idev->dev;
1435 iface_stat_update(dev, event == NETDEV_DOWN);
1436 atomic64_inc(&qtu_events.iface_events);
1437 break;
1438 }
1439 return NOTIFY_DONE;
1440}
1441
1442static int iface_inetaddr_event_handler(struct notifier_block *nb,
1443 unsigned long event, void *ptr)
1444{
1445 struct in_ifaddr *ifa = ptr;
1446 struct net_device *dev;
1447
1448 if (unlikely(module_passive))
1449 return NOTIFY_DONE;
1450
1451 IF_DEBUG("qtaguid: iface_stat: inetaddr_event(): "
1452 "ev=0x%lx/%s ifa=%p\n",
1453 event, netdev_evt_str(event), ifa);
1454
1455 switch (event) {
1456 case NETDEV_UP:
1457 BUG_ON(!ifa || !ifa->ifa_dev);
1458 dev = ifa->ifa_dev->dev;
1459 iface_stat_create(dev, ifa);
1460 atomic64_inc(&qtu_events.iface_events);
1461 break;
1462 case NETDEV_DOWN:
1463 case NETDEV_UNREGISTER:
1464 BUG_ON(!ifa || !ifa->ifa_dev);
1465 dev = ifa->ifa_dev->dev;
1466 iface_stat_update(dev, event == NETDEV_DOWN);
1467 atomic64_inc(&qtu_events.iface_events);
1468 break;
1469 }
1470 return NOTIFY_DONE;
1471}
1472
1473static struct notifier_block iface_netdev_notifier_blk = {
1474 .notifier_call = iface_netdev_event_handler,
1475};
1476
1477static struct notifier_block iface_inetaddr_notifier_blk = {
1478 .notifier_call = iface_inetaddr_event_handler,
1479};
1480
1481static struct notifier_block iface_inet6addr_notifier_blk = {
1482 .notifier_call = iface_inet6addr_event_handler,
1483};
1484
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001485static const struct seq_operations iface_stat_fmt_proc_seq_ops = {
1486 .start = iface_stat_fmt_proc_start,
1487 .next = iface_stat_fmt_proc_next,
1488 .stop = iface_stat_fmt_proc_stop,
1489 .show = iface_stat_fmt_proc_show,
1490};
1491
1492static int proc_iface_stat_fmt_open(struct inode *inode, struct file *file)
1493{
1494 struct proc_iface_stat_fmt_info *s;
1495
1496 s = __seq_open_private(file, &iface_stat_fmt_proc_seq_ops,
1497 sizeof(struct proc_iface_stat_fmt_info));
1498 if (!s)
1499 return -ENOMEM;
1500
Greg Hackmann85a2eb52014-02-24 09:39:46 -08001501 s->fmt = (uintptr_t)PDE_DATA(inode);
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001502 return 0;
1503}
1504
1505static const struct file_operations proc_iface_stat_fmt_fops = {
1506 .open = proc_iface_stat_fmt_open,
1507 .read = seq_read,
1508 .llseek = seq_lseek,
Greg Hackmann56472912013-12-04 17:39:27 -08001509 .release = seq_release_private,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001510};
1511
JP Abgrallbaf0db42011-06-20 12:41:46 -07001512static int __init iface_stat_init(struct proc_dir_entry *parent_procdir)
1513{
1514 int err;
1515
1516 iface_stat_procdir = proc_mkdir(iface_stat_procdirname, parent_procdir);
1517 if (!iface_stat_procdir) {
1518 pr_err("qtaguid: iface_stat: init failed to create proc entry\n");
1519 err = -1;
1520 goto err;
1521 }
1522
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001523 iface_stat_all_procfile = proc_create_data(iface_stat_all_procfilename,
1524 proc_iface_perms,
1525 parent_procdir,
1526 &proc_iface_stat_fmt_fops,
1527 (void *)1 /* fmt1 */);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001528 if (!iface_stat_all_procfile) {
1529 pr_err("qtaguid: iface_stat: init "
JP Abgrall9e0858c2012-04-27 12:57:39 -07001530 " failed to create stat_old proc entry\n");
JP Abgrallbaf0db42011-06-20 12:41:46 -07001531 err = -1;
1532 goto err_zap_entry;
1533 }
JP Abgrall9e0858c2012-04-27 12:57:39 -07001534
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001535 iface_stat_fmt_procfile = proc_create_data(iface_stat_fmt_procfilename,
1536 proc_iface_perms,
1537 parent_procdir,
1538 &proc_iface_stat_fmt_fops,
1539 (void *)2 /* fmt2 */);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001540 if (!iface_stat_fmt_procfile) {
1541 pr_err("qtaguid: iface_stat: init "
1542 " failed to create stat_all proc entry\n");
1543 err = -1;
1544 goto err_zap_all_stats_entry;
1545 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001546
1547
1548 err = register_netdevice_notifier(&iface_netdev_notifier_blk);
1549 if (err) {
1550 pr_err("qtaguid: iface_stat: init "
1551 "failed to register dev event handler\n");
JP Abgrall9e0858c2012-04-27 12:57:39 -07001552 goto err_zap_all_stats_entries;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001553 }
1554 err = register_inetaddr_notifier(&iface_inetaddr_notifier_blk);
1555 if (err) {
1556 pr_err("qtaguid: iface_stat: init "
1557 "failed to register ipv4 dev event handler\n");
1558 goto err_unreg_nd;
1559 }
1560
1561 err = register_inet6addr_notifier(&iface_inet6addr_notifier_blk);
1562 if (err) {
1563 pr_err("qtaguid: iface_stat: init "
1564 "failed to register ipv6 dev event handler\n");
1565 goto err_unreg_ip4_addr;
1566 }
1567 return 0;
1568
1569err_unreg_ip4_addr:
1570 unregister_inetaddr_notifier(&iface_inetaddr_notifier_blk);
1571err_unreg_nd:
1572 unregister_netdevice_notifier(&iface_netdev_notifier_blk);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001573err_zap_all_stats_entries:
1574 remove_proc_entry(iface_stat_fmt_procfilename, parent_procdir);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001575err_zap_all_stats_entry:
1576 remove_proc_entry(iface_stat_all_procfilename, parent_procdir);
1577err_zap_entry:
1578 remove_proc_entry(iface_stat_procdirname, parent_procdir);
1579err:
1580 return err;
1581}
1582
1583static struct sock *qtaguid_find_sk(const struct sk_buff *skb,
1584 struct xt_action_param *par)
1585{
1586 struct sock *sk;
1587 unsigned int hook_mask = (1 << par->hooknum);
1588
Kyle Yancd02e632017-10-06 10:33:18 -07001589 MT_DEBUG("qtaguid[%d]: find_sk(skb=%pK) family=%d\n",
JP Abgrall89d32dc2013-12-20 16:51:11 -08001590 par->hooknum, skb, par->family);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001591
1592 /*
1593 * Let's not abuse the the xt_socket_get*_sk(), or else it will
1594 * return garbage SKs.
1595 */
1596 if (!(hook_mask & XT_SOCKET_SUPPORTED_HOOKS))
1597 return NULL;
1598
1599 switch (par->family) {
1600 case NFPROTO_IPV6:
Amit Pundir7de1bb82015-11-20 14:45:40 +05301601 sk = xt_socket_lookup_slow_v6(dev_net(skb->dev), skb, par->in);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001602 break;
1603 case NFPROTO_IPV4:
Amit Pundir7de1bb82015-11-20 14:45:40 +05301604 sk = xt_socket_lookup_slow_v4(dev_net(skb->dev), skb, par->in);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001605 break;
1606 default:
1607 return NULL;
1608 }
1609
JP Abgrallbaf0db42011-06-20 12:41:46 -07001610 if (sk) {
Kyle Yancd02e632017-10-06 10:33:18 -07001611 MT_DEBUG("qtaguid[%d]: %pK->sk_proto=%u->sk_state=%d\n",
JP Abgrall89d32dc2013-12-20 16:51:11 -08001612 par->hooknum, sk, sk->sk_protocol, sk->sk_state);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001613 }
1614 return sk;
1615}
1616
1617static void account_for_uid(const struct sk_buff *skb,
1618 const struct sock *alternate_sk, uid_t uid,
1619 struct xt_action_param *par)
1620{
1621 const struct net_device *el_dev;
JP Abgrall89d32dc2013-12-20 16:51:11 -08001622 enum ifs_tx_rx direction;
1623 int proto;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001624
JP Abgrall89d32dc2013-12-20 16:51:11 -08001625 get_dev_and_dir(skb, par, &direction, &el_dev);
1626 proto = ipx_proto(skb, par);
1627 MT_DEBUG("qtaguid[%d]: dev name=%s type=%d fam=%d proto=%d dir=%d\n",
1628 par->hooknum, el_dev->name, el_dev->type,
1629 par->family, proto, direction);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001630
JP Abgrall89d32dc2013-12-20 16:51:11 -08001631 if_tag_stat_update(el_dev->name, uid,
1632 skb->sk ? skb->sk : alternate_sk,
1633 direction,
1634 proto, skb->len);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001635}
1636
1637static bool qtaguid_mt(const struct sk_buff *skb, struct xt_action_param *par)
1638{
1639 const struct xt_qtaguid_match_info *info = par->matchinfo;
1640 const struct file *filp;
1641 bool got_sock = false;
1642 struct sock *sk;
John Stultzbd1bca42014-03-28 16:23:48 -07001643 kuid_t sock_uid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001644 bool res;
Mohamad Ayyashe8101fd2015-01-13 19:20:44 -08001645 bool set_sk_callback_lock = false;
JP Abgrall89d32dc2013-12-20 16:51:11 -08001646 /*
1647 * TODO: unhack how to force just accounting.
1648 * For now we only do tag stats when the uid-owner is not requested
1649 */
1650 bool do_tag_stat = !(info->match & XT_QTAGUID_UID);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001651
1652 if (unlikely(module_passive))
1653 return (info->match ^ info->invert) == 0;
1654
1655 MT_DEBUG("qtaguid[%d]: entered skb=%p par->in=%p/out=%p fam=%d\n",
1656 par->hooknum, skb, par->in, par->out, par->family);
1657
1658 atomic64_inc(&qtu_events.match_calls);
1659 if (skb == NULL) {
1660 res = (info->match ^ info->invert) == 0;
1661 goto ret_res;
1662 }
1663
JP Abgrall9e0858c2012-04-27 12:57:39 -07001664 switch (par->hooknum) {
1665 case NF_INET_PRE_ROUTING:
1666 case NF_INET_POST_ROUTING:
1667 atomic64_inc(&qtu_events.match_calls_prepost);
1668 iface_stat_update_from_skb(skb, par);
1669 /*
1670 * We are done in pre/post. The skb will get processed
1671 * further alter.
1672 */
1673 res = (info->match ^ info->invert);
1674 goto ret_res;
1675 break;
1676 /* default: Fall through and do UID releated work */
1677 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001678
John Stultz551dae62016-04-22 17:12:57 -07001679 sk = skb_to_full_sk(skb);
JP Abgralld1fd3972013-02-20 16:38:34 -08001680 /*
1681 * When in TCP_TIME_WAIT the sk is not a "struct sock" but
1682 * "struct inet_timewait_sock" which is missing fields.
1683 * So we ignore it.
1684 */
1685 if (sk && sk->sk_state == TCP_TIME_WAIT)
1686 sk = NULL;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001687 if (sk == NULL) {
1688 /*
1689 * A missing sk->sk_socket happens when packets are in-flight
1690 * and the matching socket is already closed and gone.
1691 */
1692 sk = qtaguid_find_sk(skb, par);
1693 /*
Simon Dubrayce863f82017-08-01 18:22:47 +02001694 * TCP_NEW_SYN_RECV are not "struct sock" but "struct request_sock"
1695 * where we can get a pointer to a full socket to retrieve uid/gid.
1696 * When in TCP_TIME_WAIT, sk is a struct inet_timewait_sock
1697 * which is missing fields and does not contain any reference
1698 * to a full socket, so just ignore the socket.
JP Abgrallbaf0db42011-06-20 12:41:46 -07001699 */
Simon Dubrayce863f82017-08-01 18:22:47 +02001700 if (sk && sk->sk_state == TCP_NEW_SYN_RECV) {
1701 sock_gen_put(sk);
1702 sk = sk_to_full_sk(sk);
1703 } else if (sk && (!sk_fullsock(sk) || sk->sk_state == TCP_TIME_WAIT)) {
1704 sock_gen_put(sk);
1705 sk = NULL;
1706 } else {
1707 /*
1708 * If we got the socket from the find_sk(), we will need to put
1709 * it back, as nf_tproxy_get_sock_v4() got it.
1710 */
1711 got_sock = sk;
1712 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001713 if (sk)
1714 atomic64_inc(&qtu_events.match_found_sk_in_ct);
1715 else
1716 atomic64_inc(&qtu_events.match_found_no_sk_in_ct);
1717 } else {
1718 atomic64_inc(&qtu_events.match_found_sk);
1719 }
JP Abgrall4bb20aa2012-04-17 16:00:07 -07001720 MT_DEBUG("qtaguid[%d]: sk=%p got_sock=%d fam=%d proto=%d\n",
1721 par->hooknum, sk, got_sock, par->family, ipx_proto(skb, par));
JP Abgrallbaf0db42011-06-20 12:41:46 -07001722
Chenbo Feng5d534f42017-04-20 18:54:13 -07001723 if (!sk) {
JP Abgrallbaf0db42011-06-20 12:41:46 -07001724 /*
1725 * Here, the qtaguid_find_sk() using connection tracking
1726 * couldn't find the owner, so for now we just count them
1727 * against the system.
1728 */
JP Abgrall89d32dc2013-12-20 16:51:11 -08001729 if (do_tag_stat)
JP Abgrallbaf0db42011-06-20 12:41:46 -07001730 account_for_uid(skb, sk, 0, par);
Chenbo Fenge63ae192017-04-20 18:54:13 -07001731 MT_DEBUG("qtaguid[%d]: leaving (sk=NULL)\n", par->hooknum);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001732 res = (info->match ^ info->invert) == 0;
1733 atomic64_inc(&qtu_events.match_no_sk);
1734 goto put_sock_ret_res;
1735 } else if (info->match & info->invert & XT_QTAGUID_SOCKET) {
1736 res = false;
1737 goto put_sock_ret_res;
1738 }
Chenbo Fenge63ae192017-04-20 18:54:13 -07001739 sock_uid = sk->sk_uid;
JP Abgrall89d32dc2013-12-20 16:51:11 -08001740 if (do_tag_stat)
Chenbo Feng5d534f42017-04-20 18:54:13 -07001741 account_for_uid(skb, sk, from_kuid(&init_user_ns, sock_uid),
1742 par);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001743
1744 /*
1745 * The following two tests fail the match when:
1746 * id not in range AND no inverted condition requested
1747 * or id in range AND inverted condition requested
1748 * Thus (!a && b) || (a && !b) == a ^ b
1749 */
John Stultzbd1bca42014-03-28 16:23:48 -07001750 if (info->match & XT_QTAGUID_UID) {
1751 kuid_t uid_min = make_kuid(&init_user_ns, info->uid_min);
1752 kuid_t uid_max = make_kuid(&init_user_ns, info->uid_max);
1753
Chenbo Feng5d534f42017-04-20 18:54:13 -07001754 if ((uid_gte(sock_uid, uid_min) &&
1755 uid_lte(sock_uid, uid_max)) ^
JP Abgrallbaf0db42011-06-20 12:41:46 -07001756 !(info->invert & XT_QTAGUID_UID)) {
1757 MT_DEBUG("qtaguid[%d]: leaving uid not matching\n",
1758 par->hooknum);
1759 res = false;
1760 goto put_sock_ret_res;
1761 }
John Stultzbd1bca42014-03-28 16:23:48 -07001762 }
1763 if (info->match & XT_QTAGUID_GID) {
1764 kgid_t gid_min = make_kgid(&init_user_ns, info->gid_min);
1765 kgid_t gid_max = make_kgid(&init_user_ns, info->gid_max);
Chenbo Fenge63ae192017-04-20 18:54:13 -07001766 set_sk_callback_lock = true;
1767 read_lock_bh(&sk->sk_callback_lock);
Kyle Yane5f5ad12017-09-19 12:05:36 -07001768 MT_DEBUG("qtaguid[%d]: sk=%pK->sk_socket=%pK->file=%pK\n",
Chenbo Feng5d534f42017-04-20 18:54:13 -07001769 par->hooknum, sk, sk->sk_socket,
1770 sk->sk_socket ? sk->sk_socket->file : (void *)-1LL);
Chenbo Fenge63ae192017-04-20 18:54:13 -07001771 filp = sk->sk_socket ? sk->sk_socket->file : NULL;
1772 if (!filp) {
Chenbo Feng5d534f42017-04-20 18:54:13 -07001773 res = ((info->match ^ info->invert) &
1774 XT_QTAGUID_GID) == 0;
Chenbo Fenge63ae192017-04-20 18:54:13 -07001775 atomic64_inc(&qtu_events.match_no_sk_gid);
1776 goto put_sock_ret_res;
1777 }
1778 MT_DEBUG("qtaguid[%d]: filp...uid=%u\n",
Chenbo Feng5d534f42017-04-20 18:54:13 -07001779 par->hooknum, filp ?
1780 from_kuid(&init_user_ns, filp->f_cred->fsuid) : -1);
Amit Pundir070eff82015-01-20 16:13:08 +05301781 if ((gid_gte(filp->f_cred->fsgid, gid_min) &&
1782 gid_lte(filp->f_cred->fsgid, gid_max)) ^
JP Abgrallbaf0db42011-06-20 12:41:46 -07001783 !(info->invert & XT_QTAGUID_GID)) {
1784 MT_DEBUG("qtaguid[%d]: leaving gid not matching\n",
1785 par->hooknum);
1786 res = false;
1787 goto put_sock_ret_res;
1788 }
John Stultzbd1bca42014-03-28 16:23:48 -07001789 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001790 MT_DEBUG("qtaguid[%d]: leaving matched\n", par->hooknum);
1791 res = true;
1792
1793put_sock_ret_res:
1794 if (got_sock)
Amit Pundir2879b6e2015-01-29 01:16:23 +05301795 sock_gen_put(sk);
Mohamad Ayyashe8101fd2015-01-13 19:20:44 -08001796 if (set_sk_callback_lock)
1797 read_unlock_bh(&sk->sk_callback_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001798ret_res:
1799 MT_DEBUG("qtaguid[%d]: left %d\n", par->hooknum, res);
1800 return res;
1801}
1802
1803#ifdef DDEBUG
Chenbo Feng64157f42017-03-23 13:51:24 -07001804/*
1805 * This function is not in xt_qtaguid_print.c because of locks visibility.
1806 * The lock of sock_tag_list must be aquired before calling this function
1807 */
1808static void prdebug_full_state_locked(int indent_level, const char *fmt, ...)
JP Abgrallbaf0db42011-06-20 12:41:46 -07001809{
1810 va_list args;
1811 char *fmt_buff;
1812 char *buff;
1813
1814 if (!unlikely(qtaguid_debug_mask & DDEBUG_MASK))
1815 return;
1816
1817 fmt_buff = kasprintf(GFP_ATOMIC,
1818 "qtaguid: %s(): %s {\n", __func__, fmt);
1819 BUG_ON(!fmt_buff);
1820 va_start(args, fmt);
1821 buff = kvasprintf(GFP_ATOMIC,
1822 fmt_buff, args);
1823 BUG_ON(!buff);
1824 pr_debug("%s", buff);
1825 kfree(fmt_buff);
1826 kfree(buff);
1827 va_end(args);
1828
JP Abgrallbaf0db42011-06-20 12:41:46 -07001829 prdebug_sock_tag_tree(indent_level, &sock_tag_tree);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001830
JP Abgrallbaf0db42011-06-20 12:41:46 -07001831 spin_lock_bh(&uid_tag_data_tree_lock);
1832 prdebug_uid_tag_data_tree(indent_level, &uid_tag_data_tree);
1833 prdebug_proc_qtu_data_tree(indent_level, &proc_qtu_data_tree);
1834 spin_unlock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001835
1836 spin_lock_bh(&iface_stat_list_lock);
1837 prdebug_iface_stat_list(indent_level, &iface_stat_list);
1838 spin_unlock_bh(&iface_stat_list_lock);
1839
1840 pr_debug("qtaguid: %s(): }\n", __func__);
1841}
1842#else
Chenbo Feng64157f42017-03-23 13:51:24 -07001843static void prdebug_full_state_locked(int indent_level, const char *fmt, ...) {}
JP Abgrallbaf0db42011-06-20 12:41:46 -07001844#endif
1845
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001846struct proc_ctrl_print_info {
1847 struct sock *sk; /* socket found by reading to sk_pos */
1848 loff_t sk_pos;
1849};
1850
1851static void *qtaguid_ctrl_proc_next(struct seq_file *m, void *v, loff_t *pos)
1852{
1853 struct proc_ctrl_print_info *pcpi = m->private;
1854 struct sock_tag *sock_tag_entry = v;
1855 struct rb_node *node;
1856
1857 (*pos)++;
1858
1859 if (!v || v == SEQ_START_TOKEN)
1860 return NULL;
1861
1862 node = rb_next(&sock_tag_entry->sock_node);
1863 if (!node) {
1864 pcpi->sk = NULL;
1865 sock_tag_entry = SEQ_START_TOKEN;
1866 } else {
1867 sock_tag_entry = rb_entry(node, struct sock_tag, sock_node);
1868 pcpi->sk = sock_tag_entry->sk;
1869 }
1870 pcpi->sk_pos = *pos;
1871 return sock_tag_entry;
1872}
1873
1874static void *qtaguid_ctrl_proc_start(struct seq_file *m, loff_t *pos)
1875{
1876 struct proc_ctrl_print_info *pcpi = m->private;
1877 struct sock_tag *sock_tag_entry;
1878 struct rb_node *node;
1879
1880 spin_lock_bh(&sock_tag_list_lock);
1881
1882 if (unlikely(module_passive))
1883 return NULL;
1884
1885 if (*pos == 0) {
1886 pcpi->sk_pos = 0;
1887 node = rb_first(&sock_tag_tree);
1888 if (!node) {
1889 pcpi->sk = NULL;
1890 return SEQ_START_TOKEN;
1891 }
1892 sock_tag_entry = rb_entry(node, struct sock_tag, sock_node);
1893 pcpi->sk = sock_tag_entry->sk;
1894 } else {
1895 sock_tag_entry = (pcpi->sk ? get_sock_stat_nl(pcpi->sk) :
1896 NULL) ?: SEQ_START_TOKEN;
1897 if (*pos != pcpi->sk_pos) {
1898 /* seq_read skipped a next call */
1899 *pos = pcpi->sk_pos;
1900 return qtaguid_ctrl_proc_next(m, sock_tag_entry, pos);
1901 }
1902 }
1903 return sock_tag_entry;
1904}
1905
1906static void qtaguid_ctrl_proc_stop(struct seq_file *m, void *v)
1907{
1908 spin_unlock_bh(&sock_tag_list_lock);
1909}
1910
JP Abgrallbaf0db42011-06-20 12:41:46 -07001911/*
1912 * Procfs reader to get all active socket tags using style "1)" as described in
1913 * fs/proc/generic.c
1914 */
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001915static int qtaguid_ctrl_proc_show(struct seq_file *m, void *v)
JP Abgrallbaf0db42011-06-20 12:41:46 -07001916{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001917 struct sock_tag *sock_tag_entry = v;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001918 uid_t uid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001919
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001920 CT_DEBUG("qtaguid: proc ctrl pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07001921 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07001922
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001923 if (sock_tag_entry != SEQ_START_TOKEN) {
Chenbo Feng875e5262017-04-19 14:22:47 -07001924 int sk_ref_count;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001925 uid = get_uid_from_tag(sock_tag_entry->tag);
1926 CT_DEBUG("qtaguid: proc_read(): sk=%p tag=0x%llx (uid=%u) "
1927 "pid=%u\n",
1928 sock_tag_entry->sk,
1929 sock_tag_entry->tag,
1930 uid,
1931 sock_tag_entry->pid
1932 );
Chenbo Feng875e5262017-04-19 14:22:47 -07001933 sk_ref_count = atomic_read(
1934 &sock_tag_entry->sk->sk_refcnt);
Mohamad Ayyash8613d932016-05-11 13:18:35 -07001935 seq_printf(m, "sock=%pK tag=0x%llx (uid=%u) pid=%u "
Chenbo Feng875e5262017-04-19 14:22:47 -07001936 "f_count=%d\n",
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001937 sock_tag_entry->sk,
1938 sock_tag_entry->tag, uid,
Chenbo Feng875e5262017-04-19 14:22:47 -07001939 sock_tag_entry->pid, sk_ref_count);
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001940 } else {
1941 seq_printf(m, "events: sockets_tagged=%llu "
1942 "sockets_untagged=%llu "
1943 "counter_set_changes=%llu "
1944 "delete_cmds=%llu "
1945 "iface_events=%llu "
1946 "match_calls=%llu "
1947 "match_calls_prepost=%llu "
1948 "match_found_sk=%llu "
1949 "match_found_sk_in_ct=%llu "
1950 "match_found_no_sk_in_ct=%llu "
1951 "match_no_sk=%llu "
Chenbo Fenge63ae192017-04-20 18:54:13 -07001952 "match_no_sk_gid=%llu\n",
Sherman Yinda5ea992014-06-12 14:35:38 -07001953 (u64)atomic64_read(&qtu_events.sockets_tagged),
1954 (u64)atomic64_read(&qtu_events.sockets_untagged),
1955 (u64)atomic64_read(&qtu_events.counter_set_changes),
1956 (u64)atomic64_read(&qtu_events.delete_cmds),
1957 (u64)atomic64_read(&qtu_events.iface_events),
1958 (u64)atomic64_read(&qtu_events.match_calls),
1959 (u64)atomic64_read(&qtu_events.match_calls_prepost),
1960 (u64)atomic64_read(&qtu_events.match_found_sk),
1961 (u64)atomic64_read(&qtu_events.match_found_sk_in_ct),
1962 (u64)atomic64_read(&qtu_events.match_found_no_sk_in_ct),
1963 (u64)atomic64_read(&qtu_events.match_no_sk),
Chenbo Fenge63ae192017-04-20 18:54:13 -07001964 (u64)atomic64_read(&qtu_events.match_no_sk_gid));
JP Abgrallbaf0db42011-06-20 12:41:46 -07001965
Chenbo Feng64157f42017-03-23 13:51:24 -07001966 /* Count the following as part of the last item_index. No need
1967 * to lock the sock_tag_list here since it is already locked when
1968 * starting the seq_file operation
1969 */
1970 prdebug_full_state_locked(0, "proc ctrl");
JP Abgrallbaf0db42011-06-20 12:41:46 -07001971 }
1972
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001973 return 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001974}
1975
1976/*
1977 * Delete socket tags, and stat tags associated with a given
1978 * accouting tag and uid.
1979 */
1980static int ctrl_cmd_delete(const char *input)
1981{
1982 char cmd;
John Stultzbd1bca42014-03-28 16:23:48 -07001983 int uid_int;
1984 kuid_t uid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001985 uid_t entry_uid;
1986 tag_t acct_tag;
1987 tag_t tag;
1988 int res, argc;
1989 struct iface_stat *iface_entry;
1990 struct rb_node *node;
1991 struct sock_tag *st_entry;
1992 struct rb_root st_to_free_tree = RB_ROOT;
1993 struct tag_stat *ts_entry;
1994 struct tag_counter_set *tcs_entry;
1995 struct tag_ref *tr_entry;
1996 struct uid_tag_data *utd_entry;
1997
John Stultzbd1bca42014-03-28 16:23:48 -07001998 argc = sscanf(input, "%c %llu %u", &cmd, &acct_tag, &uid_int);
1999 uid = make_kuid(&init_user_ns, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002000 CT_DEBUG("qtaguid: ctrl_delete(%s): argc=%d cmd=%c "
2001 "user_tag=0x%llx uid=%u\n", input, argc, cmd,
John Stultzbd1bca42014-03-28 16:23:48 -07002002 acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002003 if (argc < 2) {
2004 res = -EINVAL;
2005 goto err;
2006 }
2007 if (!valid_atag(acct_tag)) {
2008 pr_info("qtaguid: ctrl_delete(%s): invalid tag\n", input);
2009 res = -EINVAL;
2010 goto err;
2011 }
2012 if (argc < 3) {
2013 uid = current_fsuid();
John Stultzbd1bca42014-03-28 16:23:48 -07002014 uid_int = from_kuid(&init_user_ns, uid);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002015 } else if (!can_impersonate_uid(uid)) {
2016 pr_info("qtaguid: ctrl_delete(%s): "
2017 "insufficient priv from pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002018 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002019 res = -EPERM;
2020 goto err;
2021 }
2022
John Stultzbd1bca42014-03-28 16:23:48 -07002023 tag = combine_atag_with_uid(acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002024 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2025 "looking for tag=0x%llx (uid=%u)\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002026 input, tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002027
2028 /* Delete socket tags */
2029 spin_lock_bh(&sock_tag_list_lock);
2030 node = rb_first(&sock_tag_tree);
2031 while (node) {
2032 st_entry = rb_entry(node, struct sock_tag, sock_node);
2033 entry_uid = get_uid_from_tag(st_entry->tag);
2034 node = rb_next(node);
John Stultzbd1bca42014-03-28 16:23:48 -07002035 if (entry_uid != uid_int)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002036 continue;
2037
2038 CT_DEBUG("qtaguid: ctrl_delete(%s): st tag=0x%llx (uid=%u)\n",
2039 input, st_entry->tag, entry_uid);
2040
2041 if (!acct_tag || st_entry->tag == tag) {
2042 rb_erase(&st_entry->sock_node, &sock_tag_tree);
2043 /* Can't sockfd_put() within spinlock, do it later. */
2044 sock_tag_tree_insert(st_entry, &st_to_free_tree);
2045 tr_entry = lookup_tag_ref(st_entry->tag, NULL);
2046 BUG_ON(tr_entry->num_sock_tags <= 0);
2047 tr_entry->num_sock_tags--;
2048 /*
2049 * TODO: remove if, and start failing.
2050 * This is a hack to work around the fact that in some
2051 * places we have "if (IS_ERR_OR_NULL(pqd_entry))"
2052 * and are trying to work around apps
2053 * that didn't open the /dev/xt_qtaguid.
2054 */
2055 if (st_entry->list.next && st_entry->list.prev)
2056 list_del(&st_entry->list);
2057 }
2058 }
2059 spin_unlock_bh(&sock_tag_list_lock);
2060
2061 sock_tag_tree_erase(&st_to_free_tree);
2062
2063 /* Delete tag counter-sets */
2064 spin_lock_bh(&tag_counter_set_list_lock);
2065 /* Counter sets are only on the uid tag, not full tag */
2066 tcs_entry = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
2067 if (tcs_entry) {
2068 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2069 "erase tcs: tag=0x%llx (uid=%u) set=%d\n",
2070 input,
2071 tcs_entry->tn.tag,
2072 get_uid_from_tag(tcs_entry->tn.tag),
2073 tcs_entry->active_set);
2074 rb_erase(&tcs_entry->tn.node, &tag_counter_set_tree);
2075 kfree(tcs_entry);
2076 }
2077 spin_unlock_bh(&tag_counter_set_list_lock);
2078
2079 /*
2080 * If acct_tag is 0, then all entries belonging to uid are
2081 * erased.
2082 */
2083 spin_lock_bh(&iface_stat_list_lock);
2084 list_for_each_entry(iface_entry, &iface_stat_list, list) {
2085 spin_lock_bh(&iface_entry->tag_stat_list_lock);
2086 node = rb_first(&iface_entry->tag_stat_tree);
2087 while (node) {
2088 ts_entry = rb_entry(node, struct tag_stat, tn.node);
2089 entry_uid = get_uid_from_tag(ts_entry->tn.tag);
2090 node = rb_next(node);
2091
2092 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2093 "ts tag=0x%llx (uid=%u)\n",
2094 input, ts_entry->tn.tag, entry_uid);
2095
John Stultzbd1bca42014-03-28 16:23:48 -07002096 if (entry_uid != uid_int)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002097 continue;
2098 if (!acct_tag || ts_entry->tn.tag == tag) {
2099 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2100 "erase ts: %s 0x%llx %u\n",
2101 input, iface_entry->ifname,
2102 get_atag_from_tag(ts_entry->tn.tag),
2103 entry_uid);
2104 rb_erase(&ts_entry->tn.node,
2105 &iface_entry->tag_stat_tree);
2106 kfree(ts_entry);
2107 }
2108 }
2109 spin_unlock_bh(&iface_entry->tag_stat_list_lock);
2110 }
2111 spin_unlock_bh(&iface_stat_list_lock);
2112
2113 /* Cleanup the uid_tag_data */
2114 spin_lock_bh(&uid_tag_data_tree_lock);
2115 node = rb_first(&uid_tag_data_tree);
2116 while (node) {
2117 utd_entry = rb_entry(node, struct uid_tag_data, node);
2118 entry_uid = utd_entry->uid;
2119 node = rb_next(node);
2120
2121 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2122 "utd uid=%u\n",
2123 input, entry_uid);
2124
John Stultzbd1bca42014-03-28 16:23:48 -07002125 if (entry_uid != uid_int)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002126 continue;
2127 /*
2128 * Go over the tag_refs, and those that don't have
2129 * sock_tags using them are freed.
2130 */
2131 put_tag_ref_tree(tag, utd_entry);
2132 put_utd_entry(utd_entry);
2133 }
2134 spin_unlock_bh(&uid_tag_data_tree_lock);
2135
2136 atomic64_inc(&qtu_events.delete_cmds);
2137 res = 0;
2138
2139err:
2140 return res;
2141}
2142
2143static int ctrl_cmd_counter_set(const char *input)
2144{
2145 char cmd;
2146 uid_t uid = 0;
2147 tag_t tag;
2148 int res, argc;
2149 struct tag_counter_set *tcs;
2150 int counter_set;
2151
2152 argc = sscanf(input, "%c %d %u", &cmd, &counter_set, &uid);
2153 CT_DEBUG("qtaguid: ctrl_counterset(%s): argc=%d cmd=%c "
2154 "set=%d uid=%u\n", input, argc, cmd,
2155 counter_set, uid);
2156 if (argc != 3) {
2157 res = -EINVAL;
2158 goto err;
2159 }
2160 if (counter_set < 0 || counter_set >= IFS_MAX_COUNTER_SETS) {
2161 pr_info("qtaguid: ctrl_counterset(%s): invalid counter_set range\n",
2162 input);
2163 res = -EINVAL;
2164 goto err;
2165 }
2166 if (!can_manipulate_uids()) {
2167 pr_info("qtaguid: ctrl_counterset(%s): "
2168 "insufficient priv from pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002169 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002170 res = -EPERM;
2171 goto err;
2172 }
2173
2174 tag = make_tag_from_uid(uid);
2175 spin_lock_bh(&tag_counter_set_list_lock);
2176 tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
2177 if (!tcs) {
2178 tcs = kzalloc(sizeof(*tcs), GFP_ATOMIC);
2179 if (!tcs) {
2180 spin_unlock_bh(&tag_counter_set_list_lock);
2181 pr_err("qtaguid: ctrl_counterset(%s): "
2182 "failed to alloc counter set\n",
2183 input);
2184 res = -ENOMEM;
2185 goto err;
2186 }
2187 tcs->tn.tag = tag;
2188 tag_counter_set_tree_insert(tcs, &tag_counter_set_tree);
2189 CT_DEBUG("qtaguid: ctrl_counterset(%s): added tcs tag=0x%llx "
2190 "(uid=%u) set=%d\n",
2191 input, tag, get_uid_from_tag(tag), counter_set);
2192 }
2193 tcs->active_set = counter_set;
2194 spin_unlock_bh(&tag_counter_set_list_lock);
2195 atomic64_inc(&qtu_events.counter_set_changes);
2196 res = 0;
2197
2198err:
2199 return res;
2200}
2201
2202static int ctrl_cmd_tag(const char *input)
2203{
2204 char cmd;
2205 int sock_fd = 0;
John Stultzbd1bca42014-03-28 16:23:48 -07002206 kuid_t uid;
2207 unsigned int uid_int = 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002208 tag_t acct_tag = make_atag_from_value(0);
2209 tag_t full_tag;
2210 struct socket *el_socket;
2211 int res, argc;
2212 struct sock_tag *sock_tag_entry;
2213 struct tag_ref *tag_ref_entry;
2214 struct uid_tag_data *uid_tag_data_entry;
2215 struct proc_qtu_data *pqd_entry;
2216
2217 /* Unassigned args will get defaulted later. */
John Stultzbd1bca42014-03-28 16:23:48 -07002218 argc = sscanf(input, "%c %d %llu %u", &cmd, &sock_fd, &acct_tag, &uid_int);
2219 uid = make_kuid(&init_user_ns, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002220 CT_DEBUG("qtaguid: ctrl_tag(%s): argc=%d cmd=%c sock_fd=%d "
2221 "acct_tag=0x%llx uid=%u\n", input, argc, cmd, sock_fd,
John Stultzbd1bca42014-03-28 16:23:48 -07002222 acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002223 if (argc < 2) {
2224 res = -EINVAL;
2225 goto err;
2226 }
2227 el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */
2228 if (!el_socket) {
2229 pr_info("qtaguid: ctrl_tag(%s): failed to lookup"
JP Abgrall9e0858c2012-04-27 12:57:39 -07002230 " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n",
2231 input, sock_fd, res, current->pid, current->tgid,
John Stultzbd1bca42014-03-28 16:23:48 -07002232 from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002233 goto err;
2234 }
Chenbo Feng875e5262017-04-19 14:22:47 -07002235 CT_DEBUG("qtaguid: ctrl_tag(%s): socket->...->sk_refcnt=%d ->sk=%p\n",
2236 input, atomic_read(&el_socket->sk->sk_refcnt),
JP Abgrallbaf0db42011-06-20 12:41:46 -07002237 el_socket->sk);
2238 if (argc < 3) {
2239 acct_tag = make_atag_from_value(0);
2240 } else if (!valid_atag(acct_tag)) {
2241 pr_info("qtaguid: ctrl_tag(%s): invalid tag\n", input);
2242 res = -EINVAL;
2243 goto err_put;
2244 }
2245 CT_DEBUG("qtaguid: ctrl_tag(%s): "
2246 "pid=%u tgid=%u uid=%u euid=%u fsuid=%u "
JP Abgrall90414bc2013-01-04 18:18:36 -08002247 "ctrl.gid=%u in_group()=%d in_egroup()=%d\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002248 input, current->pid, current->tgid,
2249 from_kuid(&init_user_ns, current_uid()),
2250 from_kuid(&init_user_ns, current_euid()),
2251 from_kuid(&init_user_ns, current_fsuid()),
2252 from_kgid(&init_user_ns, xt_qtaguid_ctrl_file->gid),
JP Abgrall90414bc2013-01-04 18:18:36 -08002253 in_group_p(xt_qtaguid_ctrl_file->gid),
2254 in_egroup_p(xt_qtaguid_ctrl_file->gid));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002255 if (argc < 4) {
2256 uid = current_fsuid();
John Stultzbd1bca42014-03-28 16:23:48 -07002257 uid_int = from_kuid(&init_user_ns, uid);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002258 } else if (!can_impersonate_uid(uid)) {
2259 pr_info("qtaguid: ctrl_tag(%s): "
2260 "insufficient priv from pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002261 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002262 res = -EPERM;
2263 goto err_put;
2264 }
John Stultzbd1bca42014-03-28 16:23:48 -07002265 full_tag = combine_atag_with_uid(acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002266
2267 spin_lock_bh(&sock_tag_list_lock);
2268 sock_tag_entry = get_sock_stat_nl(el_socket->sk);
2269 tag_ref_entry = get_tag_ref(full_tag, &uid_tag_data_entry);
2270 if (IS_ERR(tag_ref_entry)) {
2271 res = PTR_ERR(tag_ref_entry);
2272 spin_unlock_bh(&sock_tag_list_lock);
2273 goto err_put;
2274 }
2275 tag_ref_entry->num_sock_tags++;
2276 if (sock_tag_entry) {
2277 struct tag_ref *prev_tag_ref_entry;
2278
2279 CT_DEBUG("qtaguid: ctrl_tag(%s): retag for sk=%p "
Chenbo Feng875e5262017-04-19 14:22:47 -07002280 "st@%p ...->sk_refcnt=%d\n",
JP Abgrallbaf0db42011-06-20 12:41:46 -07002281 input, el_socket->sk, sock_tag_entry,
Chenbo Feng875e5262017-04-19 14:22:47 -07002282 atomic_read(&el_socket->sk->sk_refcnt));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002283 prev_tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag,
2284 &uid_tag_data_entry);
2285 BUG_ON(IS_ERR_OR_NULL(prev_tag_ref_entry));
2286 BUG_ON(prev_tag_ref_entry->num_sock_tags <= 0);
2287 prev_tag_ref_entry->num_sock_tags--;
2288 sock_tag_entry->tag = full_tag;
2289 } else {
2290 CT_DEBUG("qtaguid: ctrl_tag(%s): newtag for sk=%p\n",
2291 input, el_socket->sk);
2292 sock_tag_entry = kzalloc(sizeof(*sock_tag_entry),
2293 GFP_ATOMIC);
2294 if (!sock_tag_entry) {
2295 pr_err("qtaguid: ctrl_tag(%s): "
2296 "socket tag alloc failed\n",
2297 input);
2298 spin_unlock_bh(&sock_tag_list_lock);
2299 res = -ENOMEM;
2300 goto err_tag_unref_put;
2301 }
Chenbo Feng875e5262017-04-19 14:22:47 -07002302 /*
2303 * Hold the sk refcount here to make sure the sk pointer cannot
2304 * be freed and reused
2305 */
2306 sock_hold(el_socket->sk);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002307 sock_tag_entry->sk = el_socket->sk;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002308 sock_tag_entry->pid = current->tgid;
John Stultzbd1bca42014-03-28 16:23:48 -07002309 sock_tag_entry->tag = combine_atag_with_uid(acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002310 spin_lock_bh(&uid_tag_data_tree_lock);
2311 pqd_entry = proc_qtu_data_tree_search(
2312 &proc_qtu_data_tree, current->tgid);
2313 /*
2314 * TODO: remove if, and start failing.
2315 * At first, we want to catch user-space code that is not
2316 * opening the /dev/xt_qtaguid.
2317 */
2318 if (IS_ERR_OR_NULL(pqd_entry))
2319 pr_warn_once(
2320 "qtaguid: %s(): "
2321 "User space forgot to open /dev/xt_qtaguid? "
2322 "pid=%u tgid=%u uid=%u\n", __func__,
2323 current->pid, current->tgid,
John Stultzbd1bca42014-03-28 16:23:48 -07002324 from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002325 else
2326 list_add(&sock_tag_entry->list,
2327 &pqd_entry->sock_tag_list);
2328 spin_unlock_bh(&uid_tag_data_tree_lock);
2329
2330 sock_tag_tree_insert(sock_tag_entry, &sock_tag_tree);
2331 atomic64_inc(&qtu_events.sockets_tagged);
2332 }
2333 spin_unlock_bh(&sock_tag_list_lock);
Chenbo Feng875e5262017-04-19 14:22:47 -07002334 /* We keep the ref to the sk until it is untagged */
2335 CT_DEBUG("qtaguid: ctrl_tag(%s): done st@%p ...->sk_refcnt=%d\n",
JP Abgrallbaf0db42011-06-20 12:41:46 -07002336 input, sock_tag_entry,
Chenbo Feng875e5262017-04-19 14:22:47 -07002337 atomic_read(&el_socket->sk->sk_refcnt));
2338 sockfd_put(el_socket);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002339 return 0;
2340
2341err_tag_unref_put:
2342 BUG_ON(tag_ref_entry->num_sock_tags <= 0);
2343 tag_ref_entry->num_sock_tags--;
2344 free_tag_ref_from_utd_entry(tag_ref_entry, uid_tag_data_entry);
2345err_put:
Chenbo Feng875e5262017-04-19 14:22:47 -07002346 CT_DEBUG("qtaguid: ctrl_tag(%s): done. ...->sk_refcnt=%d\n",
2347 input, atomic_read(&el_socket->sk->sk_refcnt) - 1);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002348 /* Release the sock_fd that was grabbed by sockfd_lookup(). */
2349 sockfd_put(el_socket);
2350 return res;
2351
2352err:
2353 CT_DEBUG("qtaguid: ctrl_tag(%s): done.\n", input);
2354 return res;
2355}
2356
2357static int ctrl_cmd_untag(const char *input)
2358{
2359 char cmd;
2360 int sock_fd = 0;
2361 struct socket *el_socket;
2362 int res, argc;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002363
2364 argc = sscanf(input, "%c %d", &cmd, &sock_fd);
2365 CT_DEBUG("qtaguid: ctrl_untag(%s): argc=%d cmd=%c sock_fd=%d\n",
2366 input, argc, cmd, sock_fd);
2367 if (argc < 2) {
2368 res = -EINVAL;
Chenbo Feng875e5262017-04-19 14:22:47 -07002369 return res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002370 }
2371 el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */
2372 if (!el_socket) {
2373 pr_info("qtaguid: ctrl_untag(%s): failed to lookup"
JP Abgrall9e0858c2012-04-27 12:57:39 -07002374 " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n",
2375 input, sock_fd, res, current->pid, current->tgid,
John Stultzbd1bca42014-03-28 16:23:48 -07002376 from_kuid(&init_user_ns, current_fsuid()));
Chenbo Feng875e5262017-04-19 14:22:47 -07002377 return res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002378 }
2379 CT_DEBUG("qtaguid: ctrl_untag(%s): socket->...->f_count=%ld ->sk=%p\n",
2380 input, atomic_long_read(&el_socket->file->f_count),
2381 el_socket->sk);
Chenbo Feng875e5262017-04-19 14:22:47 -07002382 res = qtaguid_untag(el_socket, false);
2383 sockfd_put(el_socket);
2384 return res;
2385}
2386
2387int qtaguid_untag(struct socket *el_socket, bool kernel)
2388{
2389 int res;
2390 pid_t pid;
2391 struct sock_tag *sock_tag_entry;
2392 struct tag_ref *tag_ref_entry;
2393 struct uid_tag_data *utd_entry;
2394 struct proc_qtu_data *pqd_entry;
2395
JP Abgrallbaf0db42011-06-20 12:41:46 -07002396 spin_lock_bh(&sock_tag_list_lock);
2397 sock_tag_entry = get_sock_stat_nl(el_socket->sk);
2398 if (!sock_tag_entry) {
2399 spin_unlock_bh(&sock_tag_list_lock);
2400 res = -EINVAL;
Chenbo Feng875e5262017-04-19 14:22:47 -07002401 return res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002402 }
2403 /*
2404 * The socket already belongs to the current process
2405 * so it can do whatever it wants to it.
2406 */
2407 rb_erase(&sock_tag_entry->sock_node, &sock_tag_tree);
2408
2409 tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag, &utd_entry);
2410 BUG_ON(!tag_ref_entry);
2411 BUG_ON(tag_ref_entry->num_sock_tags <= 0);
2412 spin_lock_bh(&uid_tag_data_tree_lock);
Chenbo Feng875e5262017-04-19 14:22:47 -07002413 if (kernel)
2414 pid = sock_tag_entry->pid;
2415 else
2416 pid = current->tgid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002417 pqd_entry = proc_qtu_data_tree_search(
Chenbo Feng875e5262017-04-19 14:22:47 -07002418 &proc_qtu_data_tree, pid);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002419 /*
2420 * TODO: remove if, and start failing.
2421 * At first, we want to catch user-space code that is not
2422 * opening the /dev/xt_qtaguid.
2423 */
Chenbo Feng875e5262017-04-19 14:22:47 -07002424 if (IS_ERR_OR_NULL(pqd_entry) || !sock_tag_entry->list.next) {
JP Abgrallbaf0db42011-06-20 12:41:46 -07002425 pr_warn_once("qtaguid: %s(): "
2426 "User space forgot to open /dev/xt_qtaguid? "
Chenbo Feng875e5262017-04-19 14:22:47 -07002427 "pid=%u tgid=%u sk_pid=%u, uid=%u\n", __func__,
2428 current->pid, current->tgid, sock_tag_entry->pid,
2429 from_kuid(&init_user_ns, current_fsuid()));
2430 } else {
JP Abgrallbaf0db42011-06-20 12:41:46 -07002431 list_del(&sock_tag_entry->list);
Chenbo Feng875e5262017-04-19 14:22:47 -07002432 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07002433 spin_unlock_bh(&uid_tag_data_tree_lock);
2434 /*
2435 * We don't free tag_ref from the utd_entry here,
2436 * only during a cmd_delete().
2437 */
2438 tag_ref_entry->num_sock_tags--;
2439 spin_unlock_bh(&sock_tag_list_lock);
2440 /*
Chenbo Feng875e5262017-04-19 14:22:47 -07002441 * Release the sock_fd that was grabbed at tag time.
JP Abgrallbaf0db42011-06-20 12:41:46 -07002442 */
Chenbo Feng875e5262017-04-19 14:22:47 -07002443 sock_put(sock_tag_entry->sk);
2444 CT_DEBUG("qtaguid: done. st@%p ...->sk_refcnt=%d\n",
2445 sock_tag_entry,
2446 atomic_read(&el_socket->sk->sk_refcnt));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002447
2448 kfree(sock_tag_entry);
2449 atomic64_inc(&qtu_events.sockets_untagged);
2450
2451 return 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002452}
2453
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002454static ssize_t qtaguid_ctrl_parse(const char *input, size_t count)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002455{
2456 char cmd;
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002457 ssize_t res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002458
JP Abgrall9e0858c2012-04-27 12:57:39 -07002459 CT_DEBUG("qtaguid: ctrl(%s): pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002460 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrall9e0858c2012-04-27 12:57:39 -07002461
JP Abgrallbaf0db42011-06-20 12:41:46 -07002462 cmd = input[0];
2463 /* Collect params for commands */
2464 switch (cmd) {
2465 case 'd':
2466 res = ctrl_cmd_delete(input);
2467 break;
2468
2469 case 's':
2470 res = ctrl_cmd_counter_set(input);
2471 break;
2472
2473 case 't':
2474 res = ctrl_cmd_tag(input);
2475 break;
2476
2477 case 'u':
2478 res = ctrl_cmd_untag(input);
2479 break;
2480
2481 default:
2482 res = -EINVAL;
2483 goto err;
2484 }
2485 if (!res)
2486 res = count;
2487err:
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002488 CT_DEBUG("qtaguid: ctrl(%s): res=%zd\n", input, res);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002489 return res;
2490}
2491
2492#define MAX_QTAGUID_CTRL_INPUT_LEN 255
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002493static ssize_t qtaguid_ctrl_proc_write(struct file *file, const char __user *buffer,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002494 size_t count, loff_t *offp)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002495{
2496 char input_buf[MAX_QTAGUID_CTRL_INPUT_LEN];
2497
2498 if (unlikely(module_passive))
2499 return count;
2500
2501 if (count >= MAX_QTAGUID_CTRL_INPUT_LEN)
2502 return -EINVAL;
2503
2504 if (copy_from_user(input_buf, buffer, count))
2505 return -EFAULT;
2506
2507 input_buf[count] = '\0';
2508 return qtaguid_ctrl_parse(input_buf, count);
2509}
2510
2511struct proc_print_info {
JP Abgrallbaf0db42011-06-20 12:41:46 -07002512 struct iface_stat *iface_entry;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002513 int item_index;
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002514 tag_t tag; /* tag found by reading to tag_pos */
2515 off_t tag_pos;
2516 int tag_item_index;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002517};
2518
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002519static void pp_stats_header(struct seq_file *m)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002520{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002521 seq_puts(m,
2522 "idx iface acct_tag_hex uid_tag_int cnt_set "
2523 "rx_bytes rx_packets "
2524 "tx_bytes tx_packets "
2525 "rx_tcp_bytes rx_tcp_packets "
2526 "rx_udp_bytes rx_udp_packets "
2527 "rx_other_bytes rx_other_packets "
2528 "tx_tcp_bytes tx_tcp_packets "
2529 "tx_udp_bytes tx_udp_packets "
2530 "tx_other_bytes tx_other_packets\n");
JP Abgrallbaf0db42011-06-20 12:41:46 -07002531}
2532
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002533static int pp_stats_line(struct seq_file *m, struct tag_stat *ts_entry,
2534 int cnt_set)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002535{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002536 struct data_counters *cnts;
2537 tag_t tag = ts_entry->tn.tag;
2538 uid_t stat_uid = get_uid_from_tag(tag);
2539 struct proc_print_info *ppi = m->private;
2540 /* Detailed tags are not available to everybody */
Mohamad Ayyash8613d932016-05-11 13:18:35 -07002541 if (!can_read_other_uid_stats(make_kuid(&init_user_ns,stat_uid))) {
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002542 CT_DEBUG("qtaguid: stats line: "
2543 "%s 0x%llx %u: insufficient priv "
2544 "from pid=%u tgid=%u uid=%u stats.gid=%u\n",
2545 ppi->iface_entry->ifname,
2546 get_atag_from_tag(tag), stat_uid,
John Stultzbd1bca42014-03-28 16:23:48 -07002547 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()),
2548 from_kgid(&init_user_ns,xt_qtaguid_stats_file->gid));
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002549 return 0;
2550 }
2551 ppi->item_index++;
2552 cnts = &ts_entry->counters;
Amit Pundir5b5ab942015-10-01 10:44:36 +05302553 seq_printf(m, "%d %s 0x%llx %u %u "
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002554 "%llu %llu "
2555 "%llu %llu "
2556 "%llu %llu "
2557 "%llu %llu "
2558 "%llu %llu "
2559 "%llu %llu "
2560 "%llu %llu "
2561 "%llu %llu\n",
2562 ppi->item_index,
2563 ppi->iface_entry->ifname,
2564 get_atag_from_tag(tag),
2565 stat_uid,
2566 cnt_set,
2567 dc_sum_bytes(cnts, cnt_set, IFS_RX),
2568 dc_sum_packets(cnts, cnt_set, IFS_RX),
2569 dc_sum_bytes(cnts, cnt_set, IFS_TX),
2570 dc_sum_packets(cnts, cnt_set, IFS_TX),
2571 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes,
2572 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets,
2573 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes,
2574 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets,
2575 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes,
2576 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets,
2577 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes,
2578 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets,
2579 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes,
2580 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets,
2581 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes,
2582 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets);
Amit Pundir5b5ab942015-10-01 10:44:36 +05302583 return seq_has_overflowed(m) ? -ENOSPC : 1;
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002584}
2585
2586static bool pp_sets(struct seq_file *m, struct tag_stat *ts_entry)
2587{
2588 int ret;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002589 int counter_set;
2590 for (counter_set = 0; counter_set < IFS_MAX_COUNTER_SETS;
2591 counter_set++) {
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002592 ret = pp_stats_line(m, ts_entry, counter_set);
2593 if (ret < 0)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002594 return false;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002595 }
2596 return true;
2597}
2598
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002599static int qtaguid_stats_proc_iface_stat_ptr_valid(struct iface_stat *ptr)
2600{
2601 struct iface_stat *iface_entry;
2602
2603 if (!ptr)
2604 return false;
2605
2606 list_for_each_entry(iface_entry, &iface_stat_list, list)
2607 if (iface_entry == ptr)
2608 return true;
2609 return false;
2610}
2611
2612static void qtaguid_stats_proc_next_iface_entry(struct proc_print_info *ppi)
2613{
2614 spin_unlock_bh(&ppi->iface_entry->tag_stat_list_lock);
2615 list_for_each_entry_continue(ppi->iface_entry, &iface_stat_list, list) {
2616 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2617 return;
2618 }
2619 ppi->iface_entry = NULL;
2620}
2621
2622static void *qtaguid_stats_proc_next(struct seq_file *m, void *v, loff_t *pos)
2623{
2624 struct proc_print_info *ppi = m->private;
2625 struct tag_stat *ts_entry;
2626 struct rb_node *node;
2627
2628 if (!v) {
2629 pr_err("qtaguid: %s(): unexpected v: NULL\n", __func__);
2630 return NULL;
2631 }
2632
2633 (*pos)++;
2634
2635 if (!ppi->iface_entry || unlikely(module_passive))
2636 return NULL;
2637
2638 if (v == SEQ_START_TOKEN)
2639 node = rb_first(&ppi->iface_entry->tag_stat_tree);
2640 else
2641 node = rb_next(&((struct tag_stat *)v)->tn.node);
2642
2643 while (!node) {
2644 qtaguid_stats_proc_next_iface_entry(ppi);
2645 if (!ppi->iface_entry)
2646 return NULL;
2647 node = rb_first(&ppi->iface_entry->tag_stat_tree);
2648 }
2649
2650 ts_entry = rb_entry(node, struct tag_stat, tn.node);
2651 ppi->tag = ts_entry->tn.tag;
2652 ppi->tag_pos = *pos;
2653 ppi->tag_item_index = ppi->item_index;
2654 return ts_entry;
2655}
2656
2657static void *qtaguid_stats_proc_start(struct seq_file *m, loff_t *pos)
2658{
2659 struct proc_print_info *ppi = m->private;
2660 struct tag_stat *ts_entry = NULL;
2661
2662 spin_lock_bh(&iface_stat_list_lock);
2663
2664 if (*pos == 0) {
2665 ppi->item_index = 1;
2666 ppi->tag_pos = 0;
2667 if (list_empty(&iface_stat_list)) {
2668 ppi->iface_entry = NULL;
2669 } else {
2670 ppi->iface_entry = list_first_entry(&iface_stat_list,
2671 struct iface_stat,
2672 list);
2673 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2674 }
2675 return SEQ_START_TOKEN;
2676 }
2677 if (!qtaguid_stats_proc_iface_stat_ptr_valid(ppi->iface_entry)) {
2678 if (ppi->iface_entry) {
2679 pr_err("qtaguid: %s(): iface_entry %p not found\n",
2680 __func__, ppi->iface_entry);
2681 ppi->iface_entry = NULL;
2682 }
2683 return NULL;
2684 }
2685
2686 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2687
2688 if (!ppi->tag_pos) {
2689 /* seq_read skipped first next call */
2690 ts_entry = SEQ_START_TOKEN;
2691 } else {
2692 ts_entry = tag_stat_tree_search(
2693 &ppi->iface_entry->tag_stat_tree, ppi->tag);
2694 if (!ts_entry) {
2695 pr_info("qtaguid: %s(): tag_stat.tag 0x%llx not found. Abort.\n",
2696 __func__, ppi->tag);
2697 return NULL;
2698 }
2699 }
2700
2701 if (*pos == ppi->tag_pos) { /* normal resume */
2702 ppi->item_index = ppi->tag_item_index;
2703 } else {
2704 /* seq_read skipped a next call */
2705 *pos = ppi->tag_pos;
2706 ts_entry = qtaguid_stats_proc_next(m, ts_entry, pos);
2707 }
2708
2709 return ts_entry;
2710}
2711
2712static void qtaguid_stats_proc_stop(struct seq_file *m, void *v)
2713{
2714 struct proc_print_info *ppi = m->private;
2715 if (ppi->iface_entry)
2716 spin_unlock_bh(&ppi->iface_entry->tag_stat_list_lock);
2717 spin_unlock_bh(&iface_stat_list_lock);
2718}
2719
JP Abgrallbaf0db42011-06-20 12:41:46 -07002720/*
2721 * Procfs reader to get all tag stats using style "1)" as described in
2722 * fs/proc/generic.c
2723 * Groups all protocols tx/rx bytes.
2724 */
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002725static int qtaguid_stats_proc_show(struct seq_file *m, void *v)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002726{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002727 struct tag_stat *ts_entry = v;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002728
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002729 if (v == SEQ_START_TOKEN)
2730 pp_stats_header(m);
2731 else
2732 pp_sets(m, ts_entry);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002733
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002734 return 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002735}
2736
2737/*------------------------------------------*/
2738static int qtudev_open(struct inode *inode, struct file *file)
2739{
2740 struct uid_tag_data *utd_entry;
2741 struct proc_qtu_data *pqd_entry;
2742 struct proc_qtu_data *new_pqd_entry;
2743 int res;
2744 bool utd_entry_found;
2745
2746 if (unlikely(qtu_proc_handling_passive))
2747 return 0;
2748
2749 DR_DEBUG("qtaguid: qtudev_open(): pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002750 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002751
2752 spin_lock_bh(&uid_tag_data_tree_lock);
2753
2754 /* Look for existing uid data, or alloc one. */
John Stultzbd1bca42014-03-28 16:23:48 -07002755 utd_entry = get_uid_data(from_kuid(&init_user_ns, current_fsuid()), &utd_entry_found);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002756 if (IS_ERR_OR_NULL(utd_entry)) {
2757 res = PTR_ERR(utd_entry);
JP Abgrallb79c36f12012-10-09 20:38:21 -07002758 goto err_unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002759 }
2760
2761 /* Look for existing PID based proc_data */
2762 pqd_entry = proc_qtu_data_tree_search(&proc_qtu_data_tree,
2763 current->tgid);
2764 if (pqd_entry) {
2765 pr_err("qtaguid: qtudev_open(): %u/%u %u "
2766 "%s already opened\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002767 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()),
JP Abgrallbaf0db42011-06-20 12:41:46 -07002768 QTU_DEV_NAME);
2769 res = -EBUSY;
2770 goto err_unlock_free_utd;
2771 }
2772
2773 new_pqd_entry = kzalloc(sizeof(*new_pqd_entry), GFP_ATOMIC);
2774 if (!new_pqd_entry) {
2775 pr_err("qtaguid: qtudev_open(): %u/%u %u: "
2776 "proc data alloc failed\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002777 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002778 res = -ENOMEM;
2779 goto err_unlock_free_utd;
2780 }
2781 new_pqd_entry->pid = current->tgid;
2782 INIT_LIST_HEAD(&new_pqd_entry->sock_tag_list);
2783 new_pqd_entry->parent_tag_data = utd_entry;
2784 utd_entry->num_pqd++;
2785
2786 proc_qtu_data_tree_insert(new_pqd_entry,
2787 &proc_qtu_data_tree);
2788
2789 spin_unlock_bh(&uid_tag_data_tree_lock);
2790 DR_DEBUG("qtaguid: tracking data for uid=%u in pqd=%p\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002791 from_kuid(&init_user_ns, current_fsuid()), new_pqd_entry);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002792 file->private_data = new_pqd_entry;
2793 return 0;
2794
2795err_unlock_free_utd:
2796 if (!utd_entry_found) {
2797 rb_erase(&utd_entry->node, &uid_tag_data_tree);
2798 kfree(utd_entry);
2799 }
JP Abgrallb79c36f12012-10-09 20:38:21 -07002800err_unlock:
JP Abgrallbaf0db42011-06-20 12:41:46 -07002801 spin_unlock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002802 return res;
2803}
2804
2805static int qtudev_release(struct inode *inode, struct file *file)
2806{
2807 struct proc_qtu_data *pqd_entry = file->private_data;
2808 struct uid_tag_data *utd_entry = pqd_entry->parent_tag_data;
2809 struct sock_tag *st_entry;
2810 struct rb_root st_to_free_tree = RB_ROOT;
2811 struct list_head *entry, *next;
2812 struct tag_ref *tr;
2813
2814 if (unlikely(qtu_proc_handling_passive))
2815 return 0;
2816
2817 /*
2818 * Do not trust the current->pid, it might just be a kworker cleaning
2819 * up after a dead proc.
2820 */
2821 DR_DEBUG("qtaguid: qtudev_release(): "
2822 "pid=%u tgid=%u uid=%u "
2823 "pqd_entry=%p->pid=%u utd_entry=%p->active_tags=%d\n",
2824 current->pid, current->tgid, pqd_entry->parent_tag_data->uid,
2825 pqd_entry, pqd_entry->pid, utd_entry,
2826 utd_entry->num_active_tags);
2827
2828 spin_lock_bh(&sock_tag_list_lock);
2829 spin_lock_bh(&uid_tag_data_tree_lock);
2830
2831 list_for_each_safe(entry, next, &pqd_entry->sock_tag_list) {
2832 st_entry = list_entry(entry, struct sock_tag, list);
2833 DR_DEBUG("qtaguid: %s(): "
2834 "erase sock_tag=%p->sk=%p pid=%u tgid=%u uid=%u\n",
2835 __func__,
2836 st_entry, st_entry->sk,
2837 current->pid, current->tgid,
2838 pqd_entry->parent_tag_data->uid);
2839
2840 utd_entry = uid_tag_data_tree_search(
2841 &uid_tag_data_tree,
2842 get_uid_from_tag(st_entry->tag));
2843 BUG_ON(IS_ERR_OR_NULL(utd_entry));
2844 DR_DEBUG("qtaguid: %s(): "
2845 "looking for tag=0x%llx in utd_entry=%p\n", __func__,
2846 st_entry->tag, utd_entry);
2847 tr = tag_ref_tree_search(&utd_entry->tag_ref_tree,
2848 st_entry->tag);
2849 BUG_ON(!tr);
2850 BUG_ON(tr->num_sock_tags <= 0);
2851 tr->num_sock_tags--;
2852 free_tag_ref_from_utd_entry(tr, utd_entry);
2853
2854 rb_erase(&st_entry->sock_node, &sock_tag_tree);
2855 list_del(&st_entry->list);
2856 /* Can't sockfd_put() within spinlock, do it later. */
2857 sock_tag_tree_insert(st_entry, &st_to_free_tree);
2858
2859 /*
2860 * Try to free the utd_entry if no other proc_qtu_data is
2861 * using it (num_pqd is 0) and it doesn't have active tags
2862 * (num_active_tags is 0).
2863 */
2864 put_utd_entry(utd_entry);
2865 }
2866
2867 rb_erase(&pqd_entry->node, &proc_qtu_data_tree);
2868 BUG_ON(pqd_entry->parent_tag_data->num_pqd < 1);
2869 pqd_entry->parent_tag_data->num_pqd--;
2870 put_utd_entry(pqd_entry->parent_tag_data);
2871 kfree(pqd_entry);
2872 file->private_data = NULL;
2873
2874 spin_unlock_bh(&uid_tag_data_tree_lock);
2875 spin_unlock_bh(&sock_tag_list_lock);
2876
2877
2878 sock_tag_tree_erase(&st_to_free_tree);
2879
Chenbo Feng64157f42017-03-23 13:51:24 -07002880 spin_lock_bh(&sock_tag_list_lock);
2881 prdebug_full_state_locked(0, "%s(): pid=%u tgid=%u", __func__,
JP Abgrallbaf0db42011-06-20 12:41:46 -07002882 current->pid, current->tgid);
Chenbo Feng64157f42017-03-23 13:51:24 -07002883 spin_unlock_bh(&sock_tag_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002884 return 0;
2885}
2886
2887/*------------------------------------------*/
2888static const struct file_operations qtudev_fops = {
2889 .owner = THIS_MODULE,
2890 .open = qtudev_open,
2891 .release = qtudev_release,
2892};
2893
2894static struct miscdevice qtu_device = {
2895 .minor = MISC_DYNAMIC_MINOR,
2896 .name = QTU_DEV_NAME,
2897 .fops = &qtudev_fops,
2898 /* How sad it doesn't allow for defaults: .mode = S_IRUGO | S_IWUSR */
2899};
2900
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002901static const struct seq_operations proc_qtaguid_ctrl_seqops = {
2902 .start = qtaguid_ctrl_proc_start,
2903 .next = qtaguid_ctrl_proc_next,
2904 .stop = qtaguid_ctrl_proc_stop,
2905 .show = qtaguid_ctrl_proc_show,
2906};
2907
2908static int proc_qtaguid_ctrl_open(struct inode *inode, struct file *file)
2909{
2910 return seq_open_private(file, &proc_qtaguid_ctrl_seqops,
2911 sizeof(struct proc_ctrl_print_info));
2912}
2913
2914static const struct file_operations proc_qtaguid_ctrl_fops = {
2915 .open = proc_qtaguid_ctrl_open,
2916 .read = seq_read,
2917 .write = qtaguid_ctrl_proc_write,
2918 .llseek = seq_lseek,
Greg Hackmann56472912013-12-04 17:39:27 -08002919 .release = seq_release_private,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002920};
2921
2922static const struct seq_operations proc_qtaguid_stats_seqops = {
2923 .start = qtaguid_stats_proc_start,
2924 .next = qtaguid_stats_proc_next,
2925 .stop = qtaguid_stats_proc_stop,
2926 .show = qtaguid_stats_proc_show,
2927};
2928
2929static int proc_qtaguid_stats_open(struct inode *inode, struct file *file)
2930{
2931 return seq_open_private(file, &proc_qtaguid_stats_seqops,
2932 sizeof(struct proc_print_info));
2933}
2934
2935static const struct file_operations proc_qtaguid_stats_fops = {
2936 .open = proc_qtaguid_stats_open,
2937 .read = seq_read,
2938 .llseek = seq_lseek,
2939 .release = seq_release_private,
2940};
2941
JP Abgrallbaf0db42011-06-20 12:41:46 -07002942/*------------------------------------------*/
2943static int __init qtaguid_proc_register(struct proc_dir_entry **res_procdir)
2944{
2945 int ret;
2946 *res_procdir = proc_mkdir(module_procdirname, init_net.proc_net);
2947 if (!*res_procdir) {
2948 pr_err("qtaguid: failed to create proc/.../xt_qtaguid\n");
2949 ret = -ENOMEM;
2950 goto no_dir;
2951 }
2952
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002953 xt_qtaguid_ctrl_file = proc_create_data("ctrl", proc_ctrl_perms,
2954 *res_procdir,
2955 &proc_qtaguid_ctrl_fops,
2956 NULL);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002957 if (!xt_qtaguid_ctrl_file) {
2958 pr_err("qtaguid: failed to create xt_qtaguid/ctrl "
2959 " file\n");
2960 ret = -ENOMEM;
2961 goto no_ctrl_entry;
2962 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07002963
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002964 xt_qtaguid_stats_file = proc_create_data("stats", proc_stats_perms,
2965 *res_procdir,
2966 &proc_qtaguid_stats_fops,
2967 NULL);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002968 if (!xt_qtaguid_stats_file) {
2969 pr_err("qtaguid: failed to create xt_qtaguid/stats "
2970 "file\n");
2971 ret = -ENOMEM;
2972 goto no_stats_entry;
2973 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07002974 /*
2975 * TODO: add support counter hacking
2976 * xt_qtaguid_stats_file->write_proc = qtaguid_stats_proc_write;
2977 */
2978 return 0;
2979
2980no_stats_entry:
2981 remove_proc_entry("ctrl", *res_procdir);
2982no_ctrl_entry:
2983 remove_proc_entry("xt_qtaguid", NULL);
2984no_dir:
2985 return ret;
2986}
2987
2988static struct xt_match qtaguid_mt_reg __read_mostly = {
2989 /*
2990 * This module masquerades as the "owner" module so that iptables
2991 * tools can deal with it.
2992 */
2993 .name = "owner",
2994 .revision = 1,
2995 .family = NFPROTO_UNSPEC,
2996 .match = qtaguid_mt,
2997 .matchsize = sizeof(struct xt_qtaguid_match_info),
2998 .me = THIS_MODULE,
2999};
3000
3001static int __init qtaguid_mt_init(void)
3002{
3003 if (qtaguid_proc_register(&xt_qtaguid_procdir)
3004 || iface_stat_init(xt_qtaguid_procdir)
3005 || xt_register_match(&qtaguid_mt_reg)
3006 || misc_register(&qtu_device))
3007 return -1;
3008 return 0;
3009}
3010
3011/*
3012 * TODO: allow unloading of the module.
3013 * For now stats are permanent.
3014 * Kconfig forces'y/n' and never an 'm'.
3015 */
3016
3017module_init(qtaguid_mt_init);
3018MODULE_AUTHOR("jpa <jpa@google.com>");
3019MODULE_DESCRIPTION("Xtables: socket owner+tag matching and associated stats");
3020MODULE_LICENSE("GPL");
3021MODULE_ALIAS("ipt_owner");
3022MODULE_ALIAS("ip6t_owner");
3023MODULE_ALIAS("ipt_qtaguid");
3024MODULE_ALIAS("ip6t_qtaguid");