blob: d6779099d4532463f97e34e52dd831fcb57f98f2 [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);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700522 tr_entry = lookup_tag_ref(full_tag, &utd_entry);
523 BUG_ON(IS_ERR_OR_NULL(utd_entry));
524 if (!tr_entry)
525 tr_entry = new_tag_ref(full_tag, utd_entry);
526
JP Abgrallbaf0db42011-06-20 12:41:46 -0700527 if (utd_res)
528 *utd_res = utd_entry;
529 DR_DEBUG("qtaguid: get_tag_ref(0x%llx) utd=%p tr=%p\n",
530 full_tag, utd_entry, tr_entry);
531 return tr_entry;
532}
533
534/* Checks and maybe frees the UID Tag Data entry */
535static void put_utd_entry(struct uid_tag_data *utd_entry)
536{
537 /* Are we done with the UID tag data entry? */
538 if (RB_EMPTY_ROOT(&utd_entry->tag_ref_tree) &&
539 !utd_entry->num_pqd) {
540 DR_DEBUG("qtaguid: %s(): "
541 "erase utd_entry=%p uid=%u "
542 "by pid=%u tgid=%u uid=%u\n", __func__,
543 utd_entry, utd_entry->uid,
John Stultzbd1bca42014-03-28 16:23:48 -0700544 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -0700545 BUG_ON(utd_entry->num_active_tags);
546 rb_erase(&utd_entry->node, &uid_tag_data_tree);
547 kfree(utd_entry);
548 } else {
549 DR_DEBUG("qtaguid: %s(): "
550 "utd_entry=%p still has %d tags %d proc_qtu_data\n",
551 __func__, utd_entry, utd_entry->num_active_tags,
552 utd_entry->num_pqd);
553 BUG_ON(!(utd_entry->num_active_tags ||
554 utd_entry->num_pqd));
555 }
556}
557
558/*
559 * If no sock_tags are using this tag_ref,
560 * decrements refcount of utd_entry, removes tr_entry
561 * from utd_entry->tag_ref_tree and frees.
562 */
563static void free_tag_ref_from_utd_entry(struct tag_ref *tr_entry,
564 struct uid_tag_data *utd_entry)
565{
566 DR_DEBUG("qtaguid: %s(): %p tag=0x%llx (uid=%u)\n", __func__,
567 tr_entry, tr_entry->tn.tag,
568 get_uid_from_tag(tr_entry->tn.tag));
569 if (!tr_entry->num_sock_tags) {
570 BUG_ON(!utd_entry->num_active_tags);
571 utd_entry->num_active_tags--;
572 rb_erase(&tr_entry->tn.node, &utd_entry->tag_ref_tree);
573 DR_DEBUG("qtaguid: %s(): erased %p\n", __func__, tr_entry);
574 kfree(tr_entry);
575 }
576}
577
578static void put_tag_ref_tree(tag_t full_tag, struct uid_tag_data *utd_entry)
579{
580 struct rb_node *node;
581 struct tag_ref *tr_entry;
582 tag_t acct_tag;
583
584 DR_DEBUG("qtaguid: %s(tag=0x%llx (uid=%u))\n", __func__,
585 full_tag, get_uid_from_tag(full_tag));
586 acct_tag = get_atag_from_tag(full_tag);
587 node = rb_first(&utd_entry->tag_ref_tree);
588 while (node) {
589 tr_entry = rb_entry(node, struct tag_ref, tn.node);
590 node = rb_next(node);
591 if (!acct_tag || tr_entry->tn.tag == full_tag)
592 free_tag_ref_from_utd_entry(tr_entry, utd_entry);
593 }
594}
595
Greg Hackmann85a2eb52014-02-24 09:39:46 -0800596static ssize_t read_proc_u64(struct file *file, char __user *buf,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700597 size_t size, loff_t *ppos)
JP Abgrallbaf0db42011-06-20 12:41:46 -0700598{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700599 uint64_t *valuep = PDE_DATA(file_inode(file));
600 char tmp[24];
601 size_t tmp_size;
JP Abgrallbaf0db42011-06-20 12:41:46 -0700602
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700603 tmp_size = scnprintf(tmp, sizeof(tmp), "%llu\n", *valuep);
604 return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700605}
606
Greg Hackmann85a2eb52014-02-24 09:39:46 -0800607static ssize_t read_proc_bool(struct file *file, char __user *buf,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700608 size_t size, loff_t *ppos)
JP Abgrallbaf0db42011-06-20 12:41:46 -0700609{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700610 bool *valuep = PDE_DATA(file_inode(file));
611 char tmp[24];
612 size_t tmp_size;
JP Abgrallbaf0db42011-06-20 12:41:46 -0700613
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700614 tmp_size = scnprintf(tmp, sizeof(tmp), "%u\n", *valuep);
615 return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700616}
617
618static int get_active_counter_set(tag_t tag)
619{
620 int active_set = 0;
621 struct tag_counter_set *tcs;
622
623 MT_DEBUG("qtaguid: get_active_counter_set(tag=0x%llx)"
624 " (uid=%u)\n",
625 tag, get_uid_from_tag(tag));
626 /* For now we only handle UID tags for active sets */
627 tag = get_utag_from_tag(tag);
628 spin_lock_bh(&tag_counter_set_list_lock);
629 tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
630 if (tcs)
631 active_set = tcs->active_set;
632 spin_unlock_bh(&tag_counter_set_list_lock);
633 return active_set;
634}
635
636/*
637 * Find the entry for tracking the specified interface.
638 * Caller must hold iface_stat_list_lock
639 */
640static struct iface_stat *get_iface_entry(const char *ifname)
641{
642 struct iface_stat *iface_entry;
643
644 /* Find the entry for tracking the specified tag within the interface */
645 if (ifname == NULL) {
646 pr_info("qtaguid: iface_stat: get() NULL device name\n");
647 return NULL;
648 }
649
650 /* Iterate over interfaces */
651 list_for_each_entry(iface_entry, &iface_stat_list, list) {
652 if (!strcmp(ifname, iface_entry->ifname))
653 goto done;
654 }
655 iface_entry = NULL;
656done:
657 return iface_entry;
658}
659
JP Abgrall87f93e82013-01-28 16:50:44 -0800660/* This is for fmt2 only */
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700661static void pp_iface_stat_header(struct seq_file *m)
JP Abgrall87f93e82013-01-28 16:50:44 -0800662{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700663 seq_puts(m,
664 "ifname "
665 "total_skb_rx_bytes total_skb_rx_packets "
666 "total_skb_tx_bytes total_skb_tx_packets "
667 "rx_tcp_bytes rx_tcp_packets "
668 "rx_udp_bytes rx_udp_packets "
669 "rx_other_bytes rx_other_packets "
670 "tx_tcp_bytes tx_tcp_packets "
671 "tx_udp_bytes tx_udp_packets "
672 "tx_other_bytes tx_other_packets\n"
673 );
JP Abgrall87f93e82013-01-28 16:50:44 -0800674}
675
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700676static void pp_iface_stat_line(struct seq_file *m,
677 struct iface_stat *iface_entry)
JP Abgrallbaf0db42011-06-20 12:41:46 -0700678{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700679 struct data_counters *cnts;
680 int cnt_set = 0; /* We only use one set for the device */
681 cnts = &iface_entry->totals_via_skb;
682 seq_printf(m, "%s %llu %llu %llu %llu %llu %llu %llu %llu "
683 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
684 iface_entry->ifname,
685 dc_sum_bytes(cnts, cnt_set, IFS_RX),
686 dc_sum_packets(cnts, cnt_set, IFS_RX),
687 dc_sum_bytes(cnts, cnt_set, IFS_TX),
688 dc_sum_packets(cnts, cnt_set, IFS_TX),
689 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes,
690 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets,
691 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes,
692 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets,
693 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes,
694 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets,
695 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes,
696 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets,
697 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes,
698 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets,
699 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes,
700 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets);
701}
JP Abgrallbaf0db42011-06-20 12:41:46 -0700702
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700703struct proc_iface_stat_fmt_info {
704 int fmt;
705};
JP Abgrallbaf0db42011-06-20 12:41:46 -0700706
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700707static void *iface_stat_fmt_proc_start(struct seq_file *m, loff_t *pos)
708{
709 struct proc_iface_stat_fmt_info *p = m->private;
710 loff_t n = *pos;
JP Abgrall9e0858c2012-04-27 12:57:39 -0700711
JP Abgrallbaf0db42011-06-20 12:41:46 -0700712 /*
713 * This lock will prevent iface_stat_update() from changing active,
714 * and in turn prevent an interface from unregistering itself.
715 */
716 spin_lock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700717
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700718 if (unlikely(module_passive))
719 return NULL;
JP Abgrallbaf0db42011-06-20 12:41:46 -0700720
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700721 if (!n && p->fmt == 2)
722 pp_iface_stat_header(m);
723
724 return seq_list_start(&iface_stat_list, n);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700725}
726
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700727static void *iface_stat_fmt_proc_next(struct seq_file *m, void *p, loff_t *pos)
728{
729 return seq_list_next(p, &iface_stat_list, pos);
730}
731
732static void iface_stat_fmt_proc_stop(struct seq_file *m, void *p)
733{
734 spin_unlock_bh(&iface_stat_list_lock);
735}
736
737static int iface_stat_fmt_proc_show(struct seq_file *m, void *v)
738{
739 struct proc_iface_stat_fmt_info *p = m->private;
740 struct iface_stat *iface_entry;
741 struct rtnl_link_stats64 dev_stats, *stats;
742 struct rtnl_link_stats64 no_dev_stats = {0};
743
744
745 CT_DEBUG("qtaguid:proc iface_stat_fmt pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -0700746 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700747
748 iface_entry = list_entry(v, struct iface_stat, list);
749
750 if (iface_entry->active) {
751 stats = dev_get_stats(iface_entry->net_dev,
752 &dev_stats);
753 } else {
754 stats = &no_dev_stats;
755 }
756 /*
757 * If the meaning of the data changes, then update the fmtX
758 * string.
759 */
760 if (p->fmt == 1) {
761 seq_printf(m, "%s %d %llu %llu %llu %llu %llu %llu %llu %llu\n",
762 iface_entry->ifname,
763 iface_entry->active,
764 iface_entry->totals_via_dev[IFS_RX].bytes,
765 iface_entry->totals_via_dev[IFS_RX].packets,
766 iface_entry->totals_via_dev[IFS_TX].bytes,
767 iface_entry->totals_via_dev[IFS_TX].packets,
768 stats->rx_bytes, stats->rx_packets,
769 stats->tx_bytes, stats->tx_packets
770 );
771 } else {
772 pp_iface_stat_line(m, iface_entry);
773 }
774 return 0;
775}
776
777static const struct file_operations read_u64_fops = {
778 .read = read_proc_u64,
779 .llseek = default_llseek,
780};
781
782static const struct file_operations read_bool_fops = {
783 .read = read_proc_bool,
784 .llseek = default_llseek,
785};
786
JP Abgrallbaf0db42011-06-20 12:41:46 -0700787static void iface_create_proc_worker(struct work_struct *work)
788{
789 struct proc_dir_entry *proc_entry;
790 struct iface_stat_work *isw = container_of(work, struct iface_stat_work,
791 iface_work);
792 struct iface_stat *new_iface = isw->iface_entry;
793
794 /* iface_entries are not deleted, so safe to manipulate. */
795 proc_entry = proc_mkdir(new_iface->ifname, iface_stat_procdir);
796 if (IS_ERR_OR_NULL(proc_entry)) {
797 pr_err("qtaguid: iface_stat: create_proc(): alloc failed.\n");
798 kfree(isw);
799 return;
800 }
801
802 new_iface->proc_ptr = proc_entry;
803
Arve Hjønnevåg287076e2013-05-13 20:45:02 -0700804 proc_create_data("tx_bytes", proc_iface_perms, proc_entry,
805 &read_u64_fops,
806 &new_iface->totals_via_dev[IFS_TX].bytes);
807 proc_create_data("rx_bytes", proc_iface_perms, proc_entry,
808 &read_u64_fops,
809 &new_iface->totals_via_dev[IFS_RX].bytes);
810 proc_create_data("tx_packets", proc_iface_perms, proc_entry,
811 &read_u64_fops,
812 &new_iface->totals_via_dev[IFS_TX].packets);
813 proc_create_data("rx_packets", proc_iface_perms, proc_entry,
814 &read_u64_fops,
815 &new_iface->totals_via_dev[IFS_RX].packets);
816 proc_create_data("active", proc_iface_perms, proc_entry,
817 &read_bool_fops, &new_iface->active);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700818
819 IF_DEBUG("qtaguid: iface_stat: create_proc(): done "
820 "entry=%p dev=%s\n", new_iface, new_iface->ifname);
821 kfree(isw);
822}
823
824/*
825 * Will set the entry's active state, and
826 * update the net_dev accordingly also.
827 */
828static void _iface_stat_set_active(struct iface_stat *entry,
829 struct net_device *net_dev,
830 bool activate)
831{
832 if (activate) {
833 entry->net_dev = net_dev;
834 entry->active = true;
835 IF_DEBUG("qtaguid: %s(%s): "
836 "enable tracking. rfcnt=%d\n", __func__,
837 entry->ifname,
838 __this_cpu_read(*net_dev->pcpu_refcnt));
839 } else {
840 entry->active = false;
841 entry->net_dev = NULL;
842 IF_DEBUG("qtaguid: %s(%s): "
843 "disable tracking. rfcnt=%d\n", __func__,
844 entry->ifname,
845 __this_cpu_read(*net_dev->pcpu_refcnt));
846
847 }
848}
849
850/* Caller must hold iface_stat_list_lock */
851static struct iface_stat *iface_alloc(struct net_device *net_dev)
852{
853 struct iface_stat *new_iface;
854 struct iface_stat_work *isw;
855
856 new_iface = kzalloc(sizeof(*new_iface), GFP_ATOMIC);
857 if (new_iface == NULL) {
858 pr_err("qtaguid: iface_stat: create(%s): "
859 "iface_stat alloc failed\n", net_dev->name);
860 return NULL;
861 }
862 new_iface->ifname = kstrdup(net_dev->name, GFP_ATOMIC);
863 if (new_iface->ifname == NULL) {
864 pr_err("qtaguid: iface_stat: create(%s): "
865 "ifname alloc failed\n", net_dev->name);
866 kfree(new_iface);
867 return NULL;
868 }
869 spin_lock_init(&new_iface->tag_stat_list_lock);
870 new_iface->tag_stat_tree = RB_ROOT;
871 _iface_stat_set_active(new_iface, net_dev, true);
872
873 /*
874 * ipv6 notifier chains are atomic :(
875 * No create_proc_read_entry() for you!
876 */
877 isw = kmalloc(sizeof(*isw), GFP_ATOMIC);
878 if (!isw) {
879 pr_err("qtaguid: iface_stat: create(%s): "
880 "work alloc failed\n", new_iface->ifname);
881 _iface_stat_set_active(new_iface, net_dev, false);
882 kfree(new_iface->ifname);
883 kfree(new_iface);
884 return NULL;
885 }
886 isw->iface_entry = new_iface;
887 INIT_WORK(&isw->iface_work, iface_create_proc_worker);
888 schedule_work(&isw->iface_work);
889 list_add(&new_iface->list, &iface_stat_list);
890 return new_iface;
891}
892
893static void iface_check_stats_reset_and_adjust(struct net_device *net_dev,
894 struct iface_stat *iface)
895{
896 struct rtnl_link_stats64 dev_stats, *stats;
897 bool stats_rewound;
898
899 stats = dev_get_stats(net_dev, &dev_stats);
900 /* No empty packets */
901 stats_rewound =
902 (stats->rx_bytes < iface->last_known[IFS_RX].bytes)
903 || (stats->tx_bytes < iface->last_known[IFS_TX].bytes);
904
905 IF_DEBUG("qtaguid: %s(%s): iface=%p netdev=%p "
906 "bytes rx/tx=%llu/%llu "
907 "active=%d last_known=%d "
908 "stats_rewound=%d\n", __func__,
909 net_dev ? net_dev->name : "?",
910 iface, net_dev,
911 stats->rx_bytes, stats->tx_bytes,
912 iface->active, iface->last_known_valid, stats_rewound);
913
914 if (iface->active && iface->last_known_valid && stats_rewound) {
915 pr_warn_once("qtaguid: iface_stat: %s(%s): "
916 "iface reset its stats unexpectedly\n", __func__,
917 net_dev->name);
918
JP Abgrall9e0858c2012-04-27 12:57:39 -0700919 iface->totals_via_dev[IFS_TX].bytes +=
920 iface->last_known[IFS_TX].bytes;
921 iface->totals_via_dev[IFS_TX].packets +=
JP Abgrallbaf0db42011-06-20 12:41:46 -0700922 iface->last_known[IFS_TX].packets;
JP Abgrall9e0858c2012-04-27 12:57:39 -0700923 iface->totals_via_dev[IFS_RX].bytes +=
924 iface->last_known[IFS_RX].bytes;
925 iface->totals_via_dev[IFS_RX].packets +=
JP Abgrallbaf0db42011-06-20 12:41:46 -0700926 iface->last_known[IFS_RX].packets;
927 iface->last_known_valid = false;
928 IF_DEBUG("qtaguid: %s(%s): iface=%p "
929 "used last known bytes rx/tx=%llu/%llu\n", __func__,
930 iface->ifname, iface, iface->last_known[IFS_RX].bytes,
931 iface->last_known[IFS_TX].bytes);
932 }
933}
934
935/*
936 * Create a new entry for tracking the specified interface.
937 * Do nothing if the entry already exists.
938 * Called when an interface is configured with a valid IP address.
939 */
940static void iface_stat_create(struct net_device *net_dev,
941 struct in_ifaddr *ifa)
942{
943 struct in_device *in_dev = NULL;
944 const char *ifname;
945 struct iface_stat *entry;
946 __be32 ipaddr = 0;
947 struct iface_stat *new_iface;
948
949 IF_DEBUG("qtaguid: iface_stat: create(%s): ifa=%p netdev=%p\n",
950 net_dev ? net_dev->name : "?",
951 ifa, net_dev);
952 if (!net_dev) {
953 pr_err("qtaguid: iface_stat: create(): no net dev\n");
954 return;
955 }
956
957 ifname = net_dev->name;
958 if (!ifa) {
959 in_dev = in_dev_get(net_dev);
960 if (!in_dev) {
961 pr_err("qtaguid: iface_stat: create(%s): no inet dev\n",
962 ifname);
963 return;
964 }
965 IF_DEBUG("qtaguid: iface_stat: create(%s): in_dev=%p\n",
966 ifname, in_dev);
967 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
968 IF_DEBUG("qtaguid: iface_stat: create(%s): "
969 "ifa=%p ifa_label=%s\n",
Greg Hackmann37293852017-04-24 16:16:15 -0700970 ifname, ifa, ifa->ifa_label);
971 if (!strcmp(ifname, ifa->ifa_label))
JP Abgrallbaf0db42011-06-20 12:41:46 -0700972 break;
973 }
974 }
975
976 if (!ifa) {
977 IF_DEBUG("qtaguid: iface_stat: create(%s): no matching IP\n",
978 ifname);
979 goto done_put;
980 }
981 ipaddr = ifa->ifa_local;
982
983 spin_lock_bh(&iface_stat_list_lock);
984 entry = get_iface_entry(ifname);
985 if (entry != NULL) {
JP Abgrallbaf0db42011-06-20 12:41:46 -0700986 IF_DEBUG("qtaguid: iface_stat: create(%s): entry=%p\n",
987 ifname, entry);
988 iface_check_stats_reset_and_adjust(net_dev, entry);
JP Abgrallcf47f362013-02-06 17:40:07 -0800989 _iface_stat_set_active(entry, net_dev, true);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700990 IF_DEBUG("qtaguid: %s(%s): "
991 "tracking now %d on ip=%pI4\n", __func__,
JP Abgrallcf47f362013-02-06 17:40:07 -0800992 entry->ifname, true, &ipaddr);
JP Abgrallbaf0db42011-06-20 12:41:46 -0700993 goto done_unlock_put;
994 }
995
996 new_iface = iface_alloc(net_dev);
997 IF_DEBUG("qtaguid: iface_stat: create(%s): done "
998 "entry=%p ip=%pI4\n", ifname, new_iface, &ipaddr);
999done_unlock_put:
1000 spin_unlock_bh(&iface_stat_list_lock);
1001done_put:
1002 if (in_dev)
1003 in_dev_put(in_dev);
1004}
1005
1006static void iface_stat_create_ipv6(struct net_device *net_dev,
1007 struct inet6_ifaddr *ifa)
1008{
1009 struct in_device *in_dev;
1010 const char *ifname;
1011 struct iface_stat *entry;
1012 struct iface_stat *new_iface;
1013 int addr_type;
1014
1015 IF_DEBUG("qtaguid: iface_stat: create6(): ifa=%p netdev=%p->name=%s\n",
1016 ifa, net_dev, net_dev ? net_dev->name : "");
1017 if (!net_dev) {
1018 pr_err("qtaguid: iface_stat: create6(): no net dev!\n");
1019 return;
1020 }
1021 ifname = net_dev->name;
1022
1023 in_dev = in_dev_get(net_dev);
1024 if (!in_dev) {
1025 pr_err("qtaguid: iface_stat: create6(%s): no inet dev\n",
1026 ifname);
1027 return;
1028 }
1029
1030 IF_DEBUG("qtaguid: iface_stat: create6(%s): in_dev=%p\n",
1031 ifname, in_dev);
1032
1033 if (!ifa) {
1034 IF_DEBUG("qtaguid: iface_stat: create6(%s): no matching IP\n",
1035 ifname);
1036 goto done_put;
1037 }
1038 addr_type = ipv6_addr_type(&ifa->addr);
1039
1040 spin_lock_bh(&iface_stat_list_lock);
1041 entry = get_iface_entry(ifname);
1042 if (entry != NULL) {
JP Abgrallbaf0db42011-06-20 12:41:46 -07001043 IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
1044 ifname, entry);
1045 iface_check_stats_reset_and_adjust(net_dev, entry);
JP Abgrallcf47f362013-02-06 17:40:07 -08001046 _iface_stat_set_active(entry, net_dev, true);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001047 IF_DEBUG("qtaguid: %s(%s): "
1048 "tracking now %d on ip=%pI6c\n", __func__,
JP Abgrallcf47f362013-02-06 17:40:07 -08001049 entry->ifname, true, &ifa->addr);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001050 goto done_unlock_put;
1051 }
1052
1053 new_iface = iface_alloc(net_dev);
1054 IF_DEBUG("qtaguid: iface_stat: create6(%s): done "
1055 "entry=%p ip=%pI6c\n", ifname, new_iface, &ifa->addr);
1056
1057done_unlock_put:
1058 spin_unlock_bh(&iface_stat_list_lock);
1059done_put:
1060 in_dev_put(in_dev);
1061}
1062
1063static struct sock_tag *get_sock_stat_nl(const struct sock *sk)
1064{
1065 MT_DEBUG("qtaguid: get_sock_stat_nl(sk=%p)\n", sk);
1066 return sock_tag_tree_search(&sock_tag_tree, sk);
1067}
1068
1069static struct sock_tag *get_sock_stat(const struct sock *sk)
1070{
1071 struct sock_tag *sock_tag_entry;
1072 MT_DEBUG("qtaguid: get_sock_stat(sk=%p)\n", sk);
1073 if (!sk)
1074 return NULL;
1075 spin_lock_bh(&sock_tag_list_lock);
1076 sock_tag_entry = get_sock_stat_nl(sk);
1077 spin_unlock_bh(&sock_tag_list_lock);
1078 return sock_tag_entry;
1079}
1080
JP Abgrall9e0858c2012-04-27 12:57:39 -07001081static int ipx_proto(const struct sk_buff *skb,
1082 struct xt_action_param *par)
1083{
1084 int thoff = 0, tproto;
1085
1086 switch (par->family) {
1087 case NFPROTO_IPV6:
1088 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
1089 if (tproto < 0)
1090 MT_DEBUG("%s(): transport header not found in ipv6"
1091 " skb=%p\n", __func__, skb);
1092 break;
1093 case NFPROTO_IPV4:
1094 tproto = ip_hdr(skb)->protocol;
1095 break;
1096 default:
1097 tproto = IPPROTO_RAW;
1098 }
1099 return tproto;
1100}
1101
JP Abgrallbaf0db42011-06-20 12:41:46 -07001102static void
1103data_counters_update(struct data_counters *dc, int set,
1104 enum ifs_tx_rx direction, int proto, int bytes)
1105{
1106 switch (proto) {
1107 case IPPROTO_TCP:
1108 dc_add_byte_packets(dc, set, direction, IFS_TCP, bytes, 1);
1109 break;
1110 case IPPROTO_UDP:
1111 dc_add_byte_packets(dc, set, direction, IFS_UDP, bytes, 1);
1112 break;
1113 case IPPROTO_IP:
1114 default:
1115 dc_add_byte_packets(dc, set, direction, IFS_PROTO_OTHER, bytes,
1116 1);
1117 break;
1118 }
1119}
1120
1121/*
1122 * Update stats for the specified interface. Do nothing if the entry
1123 * does not exist (when a device was never configured with an IP address).
1124 * Called when an device is being unregistered.
1125 */
1126static void iface_stat_update(struct net_device *net_dev, bool stash_only)
1127{
1128 struct rtnl_link_stats64 dev_stats, *stats;
1129 struct iface_stat *entry;
1130
1131 stats = dev_get_stats(net_dev, &dev_stats);
1132 spin_lock_bh(&iface_stat_list_lock);
1133 entry = get_iface_entry(net_dev->name);
1134 if (entry == NULL) {
1135 IF_DEBUG("qtaguid: iface_stat: update(%s): not tracked\n",
1136 net_dev->name);
1137 spin_unlock_bh(&iface_stat_list_lock);
1138 return;
1139 }
1140
1141 IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
1142 net_dev->name, entry);
1143 if (!entry->active) {
1144 IF_DEBUG("qtaguid: %s(%s): already disabled\n", __func__,
1145 net_dev->name);
1146 spin_unlock_bh(&iface_stat_list_lock);
1147 return;
1148 }
1149
1150 if (stash_only) {
1151 entry->last_known[IFS_TX].bytes = stats->tx_bytes;
1152 entry->last_known[IFS_TX].packets = stats->tx_packets;
1153 entry->last_known[IFS_RX].bytes = stats->rx_bytes;
1154 entry->last_known[IFS_RX].packets = stats->rx_packets;
1155 entry->last_known_valid = true;
1156 IF_DEBUG("qtaguid: %s(%s): "
1157 "dev stats stashed rx/tx=%llu/%llu\n", __func__,
1158 net_dev->name, stats->rx_bytes, stats->tx_bytes);
1159 spin_unlock_bh(&iface_stat_list_lock);
1160 return;
1161 }
JP Abgrall9e0858c2012-04-27 12:57:39 -07001162 entry->totals_via_dev[IFS_TX].bytes += stats->tx_bytes;
1163 entry->totals_via_dev[IFS_TX].packets += stats->tx_packets;
1164 entry->totals_via_dev[IFS_RX].bytes += stats->rx_bytes;
1165 entry->totals_via_dev[IFS_RX].packets += stats->rx_packets;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001166 /* We don't need the last_known[] anymore */
1167 entry->last_known_valid = false;
1168 _iface_stat_set_active(entry, net_dev, false);
1169 IF_DEBUG("qtaguid: %s(%s): "
1170 "disable tracking. rx/tx=%llu/%llu\n", __func__,
1171 net_dev->name, stats->rx_bytes, stats->tx_bytes);
1172 spin_unlock_bh(&iface_stat_list_lock);
1173}
1174
JP Abgrall89d32dc2013-12-20 16:51:11 -08001175/* Guarantied to return a net_device that has a name */
1176static void get_dev_and_dir(const struct sk_buff *skb,
1177 struct xt_action_param *par,
1178 enum ifs_tx_rx *direction,
1179 const struct net_device **el_dev)
1180{
1181 BUG_ON(!direction || !el_dev);
1182
1183 if (par->in) {
1184 *el_dev = par->in;
1185 *direction = IFS_RX;
1186 } else if (par->out) {
1187 *el_dev = par->out;
1188 *direction = IFS_TX;
1189 } else {
1190 pr_err("qtaguid[%d]: %s(): no par->in/out?!!\n",
1191 par->hooknum, __func__);
1192 BUG();
1193 }
JP Abgrall89d32dc2013-12-20 16:51:11 -08001194 if (skb->dev && *el_dev != skb->dev) {
1195 MT_DEBUG("qtaguid[%d]: skb->dev=%p %s vs par->%s=%p %s\n",
1196 par->hooknum, skb->dev, skb->dev->name,
1197 *direction == IFS_RX ? "in" : "out", *el_dev,
1198 (*el_dev)->name);
1199 }
1200}
1201
JP Abgrall9e0858c2012-04-27 12:57:39 -07001202/*
1203 * Update stats for the specified interface from the skb.
1204 * Do nothing if the entry
1205 * does not exist (when a device was never configured with an IP address).
1206 * Called on each sk.
1207 */
1208static void iface_stat_update_from_skb(const struct sk_buff *skb,
1209 struct xt_action_param *par)
1210{
1211 struct iface_stat *entry;
1212 const struct net_device *el_dev;
JP Abgrall89d32dc2013-12-20 16:51:11 -08001213 enum ifs_tx_rx direction;
JP Abgrall9e0858c2012-04-27 12:57:39 -07001214 int bytes = skb->len;
JP Abgrall87f93e82013-01-28 16:50:44 -08001215 int proto;
JP Abgrall9e0858c2012-04-27 12:57:39 -07001216
JP Abgrall89d32dc2013-12-20 16:51:11 -08001217 get_dev_and_dir(skb, par, &direction, &el_dev);
1218 proto = ipx_proto(skb, par);
1219 MT_DEBUG("qtaguid[%d]: iface_stat: %s(%s): "
1220 "type=%d fam=%d proto=%d dir=%d\n",
1221 par->hooknum, __func__, el_dev->name, el_dev->type,
1222 par->family, proto, direction);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001223
1224 spin_lock_bh(&iface_stat_list_lock);
1225 entry = get_iface_entry(el_dev->name);
1226 if (entry == NULL) {
JP Abgrall89d32dc2013-12-20 16:51:11 -08001227 IF_DEBUG("qtaguid[%d]: iface_stat: %s(%s): not tracked\n",
1228 par->hooknum, __func__, el_dev->name);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001229 spin_unlock_bh(&iface_stat_list_lock);
1230 return;
1231 }
1232
JP Abgrall89d32dc2013-12-20 16:51:11 -08001233 IF_DEBUG("qtaguid[%d]: %s(%s): entry=%p\n", par->hooknum, __func__,
JP Abgrall9e0858c2012-04-27 12:57:39 -07001234 el_dev->name, entry);
1235
JP Abgrall87f93e82013-01-28 16:50:44 -08001236 data_counters_update(&entry->totals_via_skb, 0, direction, proto,
1237 bytes);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001238 spin_unlock_bh(&iface_stat_list_lock);
1239}
1240
JP Abgrallbaf0db42011-06-20 12:41:46 -07001241static void tag_stat_update(struct tag_stat *tag_entry,
1242 enum ifs_tx_rx direction, int proto, int bytes)
1243{
1244 int active_set;
1245 active_set = get_active_counter_set(tag_entry->tn.tag);
1246 MT_DEBUG("qtaguid: tag_stat_update(tag=0x%llx (uid=%u) set=%d "
1247 "dir=%d proto=%d bytes=%d)\n",
1248 tag_entry->tn.tag, get_uid_from_tag(tag_entry->tn.tag),
1249 active_set, direction, proto, bytes);
1250 data_counters_update(&tag_entry->counters, active_set, direction,
1251 proto, bytes);
1252 if (tag_entry->parent_counters)
1253 data_counters_update(tag_entry->parent_counters, active_set,
1254 direction, proto, bytes);
1255}
1256
1257/*
1258 * Create a new entry for tracking the specified {acct_tag,uid_tag} within
1259 * the interface.
1260 * iface_entry->tag_stat_list_lock should be held.
1261 */
1262static struct tag_stat *create_if_tag_stat(struct iface_stat *iface_entry,
1263 tag_t tag)
1264{
1265 struct tag_stat *new_tag_stat_entry = NULL;
1266 IF_DEBUG("qtaguid: iface_stat: %s(): ife=%p tag=0x%llx"
1267 " (uid=%u)\n", __func__,
1268 iface_entry, tag, get_uid_from_tag(tag));
1269 new_tag_stat_entry = kzalloc(sizeof(*new_tag_stat_entry), GFP_ATOMIC);
1270 if (!new_tag_stat_entry) {
1271 pr_err("qtaguid: iface_stat: tag stat alloc failed\n");
1272 goto done;
1273 }
1274 new_tag_stat_entry->tn.tag = tag;
1275 tag_stat_tree_insert(new_tag_stat_entry, &iface_entry->tag_stat_tree);
1276done:
1277 return new_tag_stat_entry;
1278}
1279
1280static void if_tag_stat_update(const char *ifname, uid_t uid,
1281 const struct sock *sk, enum ifs_tx_rx direction,
1282 int proto, int bytes)
1283{
1284 struct tag_stat *tag_stat_entry;
1285 tag_t tag, acct_tag;
1286 tag_t uid_tag;
1287 struct data_counters *uid_tag_counters;
1288 struct sock_tag *sock_tag_entry;
1289 struct iface_stat *iface_entry;
JP Abgralle5d79862012-04-13 19:22:35 -07001290 struct tag_stat *new_tag_stat = NULL;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001291 MT_DEBUG("qtaguid: if_tag_stat_update(ifname=%s "
1292 "uid=%u sk=%p dir=%d proto=%d bytes=%d)\n",
1293 ifname, uid, sk, direction, proto, bytes);
1294
liping.zhangf84e6a12016-01-11 13:31:01 +08001295 spin_lock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001296 iface_entry = get_iface_entry(ifname);
1297 if (!iface_entry) {
JP Abgrall89d32dc2013-12-20 16:51:11 -08001298 pr_err_ratelimited("qtaguid: tag_stat: stat_update() "
JP Abgralla2e371b2013-04-08 15:09:26 -07001299 "%s not found\n", ifname);
liping.zhangf84e6a12016-01-11 13:31:01 +08001300 spin_unlock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001301 return;
1302 }
1303 /* It is ok to process data when an iface_entry is inactive */
1304
JP Abgrall89d32dc2013-12-20 16:51:11 -08001305 MT_DEBUG("qtaguid: tag_stat: stat_update() dev=%s entry=%p\n",
JP Abgrallbaf0db42011-06-20 12:41:46 -07001306 ifname, iface_entry);
1307
1308 /*
1309 * Look for a tagged sock.
1310 * It will have an acct_uid.
1311 */
1312 sock_tag_entry = get_sock_stat(sk);
1313 if (sock_tag_entry) {
1314 tag = sock_tag_entry->tag;
1315 acct_tag = get_atag_from_tag(tag);
1316 uid_tag = get_utag_from_tag(tag);
1317 } else {
1318 acct_tag = make_atag_from_value(0);
1319 tag = combine_atag_with_uid(acct_tag, uid);
1320 uid_tag = make_tag_from_uid(uid);
1321 }
JP Abgrall89d32dc2013-12-20 16:51:11 -08001322 MT_DEBUG("qtaguid: tag_stat: stat_update(): "
JP Abgrallbaf0db42011-06-20 12:41:46 -07001323 " looking for tag=0x%llx (uid=%u) in ife=%p\n",
1324 tag, get_uid_from_tag(tag), iface_entry);
1325 /* Loop over tag list under this interface for {acct_tag,uid_tag} */
1326 spin_lock_bh(&iface_entry->tag_stat_list_lock);
1327
1328 tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree,
1329 tag);
1330 if (tag_stat_entry) {
1331 /*
1332 * Updating the {acct_tag, uid_tag} entry handles both stats:
1333 * {0, uid_tag} will also get updated.
1334 */
1335 tag_stat_update(tag_stat_entry, direction, proto, bytes);
liping.zhangf84e6a12016-01-11 13:31:01 +08001336 goto unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001337 }
1338
1339 /* Loop over tag list under this interface for {0,uid_tag} */
1340 tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree,
1341 uid_tag);
1342 if (!tag_stat_entry) {
1343 /* Here: the base uid_tag did not exist */
1344 /*
1345 * No parent counters. So
1346 * - No {0, uid_tag} stats and no {acc_tag, uid_tag} stats.
1347 */
1348 new_tag_stat = create_if_tag_stat(iface_entry, uid_tag);
Pontus Fuchsb842ea52012-11-19 11:44:51 -08001349 if (!new_tag_stat)
1350 goto unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001351 uid_tag_counters = &new_tag_stat->counters;
1352 } else {
1353 uid_tag_counters = &tag_stat_entry->counters;
1354 }
1355
1356 if (acct_tag) {
JP Abgralle5d79862012-04-13 19:22:35 -07001357 /* Create the child {acct_tag, uid_tag} and hook up parent. */
JP Abgrallbaf0db42011-06-20 12:41:46 -07001358 new_tag_stat = create_if_tag_stat(iface_entry, tag);
Pontus Fuchsb842ea52012-11-19 11:44:51 -08001359 if (!new_tag_stat)
1360 goto unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001361 new_tag_stat->parent_counters = uid_tag_counters;
JP Abgralle5d79862012-04-13 19:22:35 -07001362 } else {
1363 /*
1364 * For new_tag_stat to be still NULL here would require:
1365 * {0, uid_tag} exists
1366 * and {acct_tag, uid_tag} doesn't exist
1367 * AND acct_tag == 0.
1368 * Impossible. This reassures us that new_tag_stat
1369 * below will always be assigned.
1370 */
1371 BUG_ON(!new_tag_stat);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001372 }
1373 tag_stat_update(new_tag_stat, direction, proto, bytes);
Pontus Fuchsb842ea52012-11-19 11:44:51 -08001374unlock:
JP Abgrallbaf0db42011-06-20 12:41:46 -07001375 spin_unlock_bh(&iface_entry->tag_stat_list_lock);
liping.zhangf84e6a12016-01-11 13:31:01 +08001376 spin_unlock_bh(&iface_stat_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001377}
1378
1379static int iface_netdev_event_handler(struct notifier_block *nb,
1380 unsigned long event, void *ptr) {
Jon Medhurst (Tixy)a347e8e2014-04-14 21:20:49 -07001381 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001382
1383 if (unlikely(module_passive))
1384 return NOTIFY_DONE;
1385
1386 IF_DEBUG("qtaguid: iface_stat: netdev_event(): "
1387 "ev=0x%lx/%s netdev=%p->name=%s\n",
1388 event, netdev_evt_str(event), dev, dev ? dev->name : "");
1389
1390 switch (event) {
1391 case NETDEV_UP:
1392 iface_stat_create(dev, NULL);
1393 atomic64_inc(&qtu_events.iface_events);
1394 break;
1395 case NETDEV_DOWN:
1396 case NETDEV_UNREGISTER:
1397 iface_stat_update(dev, event == NETDEV_DOWN);
1398 atomic64_inc(&qtu_events.iface_events);
1399 break;
1400 }
1401 return NOTIFY_DONE;
1402}
1403
1404static int iface_inet6addr_event_handler(struct notifier_block *nb,
1405 unsigned long event, void *ptr)
1406{
1407 struct inet6_ifaddr *ifa = ptr;
1408 struct net_device *dev;
1409
1410 if (unlikely(module_passive))
1411 return NOTIFY_DONE;
1412
1413 IF_DEBUG("qtaguid: iface_stat: inet6addr_event(): "
1414 "ev=0x%lx/%s ifa=%p\n",
1415 event, netdev_evt_str(event), ifa);
1416
1417 switch (event) {
1418 case NETDEV_UP:
1419 BUG_ON(!ifa || !ifa->idev);
1420 dev = (struct net_device *)ifa->idev->dev;
1421 iface_stat_create_ipv6(dev, ifa);
1422 atomic64_inc(&qtu_events.iface_events);
1423 break;
1424 case NETDEV_DOWN:
1425 case NETDEV_UNREGISTER:
1426 BUG_ON(!ifa || !ifa->idev);
1427 dev = (struct net_device *)ifa->idev->dev;
1428 iface_stat_update(dev, event == NETDEV_DOWN);
1429 atomic64_inc(&qtu_events.iface_events);
1430 break;
1431 }
1432 return NOTIFY_DONE;
1433}
1434
1435static int iface_inetaddr_event_handler(struct notifier_block *nb,
1436 unsigned long event, void *ptr)
1437{
1438 struct in_ifaddr *ifa = ptr;
1439 struct net_device *dev;
1440
1441 if (unlikely(module_passive))
1442 return NOTIFY_DONE;
1443
1444 IF_DEBUG("qtaguid: iface_stat: inetaddr_event(): "
1445 "ev=0x%lx/%s ifa=%p\n",
1446 event, netdev_evt_str(event), ifa);
1447
1448 switch (event) {
1449 case NETDEV_UP:
1450 BUG_ON(!ifa || !ifa->ifa_dev);
1451 dev = ifa->ifa_dev->dev;
1452 iface_stat_create(dev, ifa);
1453 atomic64_inc(&qtu_events.iface_events);
1454 break;
1455 case NETDEV_DOWN:
1456 case NETDEV_UNREGISTER:
1457 BUG_ON(!ifa || !ifa->ifa_dev);
1458 dev = ifa->ifa_dev->dev;
1459 iface_stat_update(dev, event == NETDEV_DOWN);
1460 atomic64_inc(&qtu_events.iface_events);
1461 break;
1462 }
1463 return NOTIFY_DONE;
1464}
1465
1466static struct notifier_block iface_netdev_notifier_blk = {
1467 .notifier_call = iface_netdev_event_handler,
1468};
1469
1470static struct notifier_block iface_inetaddr_notifier_blk = {
1471 .notifier_call = iface_inetaddr_event_handler,
1472};
1473
1474static struct notifier_block iface_inet6addr_notifier_blk = {
1475 .notifier_call = iface_inet6addr_event_handler,
1476};
1477
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001478static const struct seq_operations iface_stat_fmt_proc_seq_ops = {
1479 .start = iface_stat_fmt_proc_start,
1480 .next = iface_stat_fmt_proc_next,
1481 .stop = iface_stat_fmt_proc_stop,
1482 .show = iface_stat_fmt_proc_show,
1483};
1484
1485static int proc_iface_stat_fmt_open(struct inode *inode, struct file *file)
1486{
1487 struct proc_iface_stat_fmt_info *s;
1488
1489 s = __seq_open_private(file, &iface_stat_fmt_proc_seq_ops,
1490 sizeof(struct proc_iface_stat_fmt_info));
1491 if (!s)
1492 return -ENOMEM;
1493
Greg Hackmann85a2eb52014-02-24 09:39:46 -08001494 s->fmt = (uintptr_t)PDE_DATA(inode);
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001495 return 0;
1496}
1497
1498static const struct file_operations proc_iface_stat_fmt_fops = {
1499 .open = proc_iface_stat_fmt_open,
1500 .read = seq_read,
1501 .llseek = seq_lseek,
Greg Hackmann56472912013-12-04 17:39:27 -08001502 .release = seq_release_private,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001503};
1504
JP Abgrallbaf0db42011-06-20 12:41:46 -07001505static int __init iface_stat_init(struct proc_dir_entry *parent_procdir)
1506{
1507 int err;
1508
1509 iface_stat_procdir = proc_mkdir(iface_stat_procdirname, parent_procdir);
1510 if (!iface_stat_procdir) {
1511 pr_err("qtaguid: iface_stat: init failed to create proc entry\n");
1512 err = -1;
1513 goto err;
1514 }
1515
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001516 iface_stat_all_procfile = proc_create_data(iface_stat_all_procfilename,
1517 proc_iface_perms,
1518 parent_procdir,
1519 &proc_iface_stat_fmt_fops,
1520 (void *)1 /* fmt1 */);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001521 if (!iface_stat_all_procfile) {
1522 pr_err("qtaguid: iface_stat: init "
JP Abgrall9e0858c2012-04-27 12:57:39 -07001523 " failed to create stat_old proc entry\n");
JP Abgrallbaf0db42011-06-20 12:41:46 -07001524 err = -1;
1525 goto err_zap_entry;
1526 }
JP Abgrall9e0858c2012-04-27 12:57:39 -07001527
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001528 iface_stat_fmt_procfile = proc_create_data(iface_stat_fmt_procfilename,
1529 proc_iface_perms,
1530 parent_procdir,
1531 &proc_iface_stat_fmt_fops,
1532 (void *)2 /* fmt2 */);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001533 if (!iface_stat_fmt_procfile) {
1534 pr_err("qtaguid: iface_stat: init "
1535 " failed to create stat_all proc entry\n");
1536 err = -1;
1537 goto err_zap_all_stats_entry;
1538 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001539
1540
1541 err = register_netdevice_notifier(&iface_netdev_notifier_blk);
1542 if (err) {
1543 pr_err("qtaguid: iface_stat: init "
1544 "failed to register dev event handler\n");
JP Abgrall9e0858c2012-04-27 12:57:39 -07001545 goto err_zap_all_stats_entries;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001546 }
1547 err = register_inetaddr_notifier(&iface_inetaddr_notifier_blk);
1548 if (err) {
1549 pr_err("qtaguid: iface_stat: init "
1550 "failed to register ipv4 dev event handler\n");
1551 goto err_unreg_nd;
1552 }
1553
1554 err = register_inet6addr_notifier(&iface_inet6addr_notifier_blk);
1555 if (err) {
1556 pr_err("qtaguid: iface_stat: init "
1557 "failed to register ipv6 dev event handler\n");
1558 goto err_unreg_ip4_addr;
1559 }
1560 return 0;
1561
1562err_unreg_ip4_addr:
1563 unregister_inetaddr_notifier(&iface_inetaddr_notifier_blk);
1564err_unreg_nd:
1565 unregister_netdevice_notifier(&iface_netdev_notifier_blk);
JP Abgrall9e0858c2012-04-27 12:57:39 -07001566err_zap_all_stats_entries:
1567 remove_proc_entry(iface_stat_fmt_procfilename, parent_procdir);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001568err_zap_all_stats_entry:
1569 remove_proc_entry(iface_stat_all_procfilename, parent_procdir);
1570err_zap_entry:
1571 remove_proc_entry(iface_stat_procdirname, parent_procdir);
1572err:
1573 return err;
1574}
1575
1576static struct sock *qtaguid_find_sk(const struct sk_buff *skb,
1577 struct xt_action_param *par)
1578{
1579 struct sock *sk;
1580 unsigned int hook_mask = (1 << par->hooknum);
1581
JP Abgrall89d32dc2013-12-20 16:51:11 -08001582 MT_DEBUG("qtaguid[%d]: find_sk(skb=%p) family=%d\n",
1583 par->hooknum, skb, par->family);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001584
1585 /*
1586 * Let's not abuse the the xt_socket_get*_sk(), or else it will
1587 * return garbage SKs.
1588 */
1589 if (!(hook_mask & XT_SOCKET_SUPPORTED_HOOKS))
1590 return NULL;
1591
1592 switch (par->family) {
1593 case NFPROTO_IPV6:
Amit Pundir7de1bb82015-11-20 14:45:40 +05301594 sk = xt_socket_lookup_slow_v6(dev_net(skb->dev), skb, par->in);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001595 break;
1596 case NFPROTO_IPV4:
Amit Pundir7de1bb82015-11-20 14:45:40 +05301597 sk = xt_socket_lookup_slow_v4(dev_net(skb->dev), skb, par->in);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001598 break;
1599 default:
1600 return NULL;
1601 }
1602
JP Abgrallbaf0db42011-06-20 12:41:46 -07001603 if (sk) {
JP Abgrall89d32dc2013-12-20 16:51:11 -08001604 MT_DEBUG("qtaguid[%d]: %p->sk_proto=%u->sk_state=%d\n",
1605 par->hooknum, sk, sk->sk_protocol, sk->sk_state);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001606 }
1607 return sk;
1608}
1609
1610static void account_for_uid(const struct sk_buff *skb,
1611 const struct sock *alternate_sk, uid_t uid,
1612 struct xt_action_param *par)
1613{
1614 const struct net_device *el_dev;
JP Abgrall89d32dc2013-12-20 16:51:11 -08001615 enum ifs_tx_rx direction;
1616 int proto;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001617
JP Abgrall89d32dc2013-12-20 16:51:11 -08001618 get_dev_and_dir(skb, par, &direction, &el_dev);
1619 proto = ipx_proto(skb, par);
1620 MT_DEBUG("qtaguid[%d]: dev name=%s type=%d fam=%d proto=%d dir=%d\n",
1621 par->hooknum, el_dev->name, el_dev->type,
1622 par->family, proto, direction);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001623
JP Abgrall89d32dc2013-12-20 16:51:11 -08001624 if_tag_stat_update(el_dev->name, uid,
1625 skb->sk ? skb->sk : alternate_sk,
1626 direction,
1627 proto, skb->len);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001628}
1629
1630static bool qtaguid_mt(const struct sk_buff *skb, struct xt_action_param *par)
1631{
1632 const struct xt_qtaguid_match_info *info = par->matchinfo;
1633 const struct file *filp;
1634 bool got_sock = false;
1635 struct sock *sk;
John Stultzbd1bca42014-03-28 16:23:48 -07001636 kuid_t sock_uid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001637 bool res;
Mohamad Ayyashe8101fd2015-01-13 19:20:44 -08001638 bool set_sk_callback_lock = false;
JP Abgrall89d32dc2013-12-20 16:51:11 -08001639 /*
1640 * TODO: unhack how to force just accounting.
1641 * For now we only do tag stats when the uid-owner is not requested
1642 */
1643 bool do_tag_stat = !(info->match & XT_QTAGUID_UID);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001644
1645 if (unlikely(module_passive))
1646 return (info->match ^ info->invert) == 0;
1647
1648 MT_DEBUG("qtaguid[%d]: entered skb=%p par->in=%p/out=%p fam=%d\n",
1649 par->hooknum, skb, par->in, par->out, par->family);
1650
1651 atomic64_inc(&qtu_events.match_calls);
1652 if (skb == NULL) {
1653 res = (info->match ^ info->invert) == 0;
1654 goto ret_res;
1655 }
1656
JP Abgrall9e0858c2012-04-27 12:57:39 -07001657 switch (par->hooknum) {
1658 case NF_INET_PRE_ROUTING:
1659 case NF_INET_POST_ROUTING:
1660 atomic64_inc(&qtu_events.match_calls_prepost);
1661 iface_stat_update_from_skb(skb, par);
1662 /*
1663 * We are done in pre/post. The skb will get processed
1664 * further alter.
1665 */
1666 res = (info->match ^ info->invert);
1667 goto ret_res;
1668 break;
1669 /* default: Fall through and do UID releated work */
1670 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001671
John Stultz551dae62016-04-22 17:12:57 -07001672 sk = skb_to_full_sk(skb);
JP Abgralld1fd3972013-02-20 16:38:34 -08001673 /*
1674 * When in TCP_TIME_WAIT the sk is not a "struct sock" but
1675 * "struct inet_timewait_sock" which is missing fields.
1676 * So we ignore it.
1677 */
1678 if (sk && sk->sk_state == TCP_TIME_WAIT)
1679 sk = NULL;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001680 if (sk == NULL) {
1681 /*
1682 * A missing sk->sk_socket happens when packets are in-flight
1683 * and the matching socket is already closed and gone.
1684 */
1685 sk = qtaguid_find_sk(skb, par);
1686 /*
Simon Dubrayce863f82017-08-01 18:22:47 +02001687 * TCP_NEW_SYN_RECV are not "struct sock" but "struct request_sock"
1688 * where we can get a pointer to a full socket to retrieve uid/gid.
1689 * When in TCP_TIME_WAIT, sk is a struct inet_timewait_sock
1690 * which is missing fields and does not contain any reference
1691 * to a full socket, so just ignore the socket.
JP Abgrallbaf0db42011-06-20 12:41:46 -07001692 */
Simon Dubrayce863f82017-08-01 18:22:47 +02001693 if (sk && sk->sk_state == TCP_NEW_SYN_RECV) {
1694 sock_gen_put(sk);
1695 sk = sk_to_full_sk(sk);
1696 } else if (sk && (!sk_fullsock(sk) || sk->sk_state == TCP_TIME_WAIT)) {
1697 sock_gen_put(sk);
1698 sk = NULL;
1699 } else {
1700 /*
1701 * If we got the socket from the find_sk(), we will need to put
1702 * it back, as nf_tproxy_get_sock_v4() got it.
1703 */
1704 got_sock = sk;
1705 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001706 if (sk)
1707 atomic64_inc(&qtu_events.match_found_sk_in_ct);
1708 else
1709 atomic64_inc(&qtu_events.match_found_no_sk_in_ct);
1710 } else {
1711 atomic64_inc(&qtu_events.match_found_sk);
1712 }
JP Abgrall4bb20aa2012-04-17 16:00:07 -07001713 MT_DEBUG("qtaguid[%d]: sk=%p got_sock=%d fam=%d proto=%d\n",
1714 par->hooknum, sk, got_sock, par->family, ipx_proto(skb, par));
JP Abgrallbaf0db42011-06-20 12:41:46 -07001715
Chenbo Feng5d534f42017-04-20 18:54:13 -07001716 if (!sk) {
JP Abgrallbaf0db42011-06-20 12:41:46 -07001717 /*
1718 * Here, the qtaguid_find_sk() using connection tracking
1719 * couldn't find the owner, so for now we just count them
1720 * against the system.
1721 */
JP Abgrall89d32dc2013-12-20 16:51:11 -08001722 if (do_tag_stat)
JP Abgrallbaf0db42011-06-20 12:41:46 -07001723 account_for_uid(skb, sk, 0, par);
Chenbo Feng5d534f42017-04-20 18:54:13 -07001724 MT_DEBUG("qtaguid[%d]: leaving (sk=NULL)\n", par->hooknum);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001725 res = (info->match ^ info->invert) == 0;
1726 atomic64_inc(&qtu_events.match_no_sk);
1727 goto put_sock_ret_res;
1728 } else if (info->match & info->invert & XT_QTAGUID_SOCKET) {
1729 res = false;
1730 goto put_sock_ret_res;
1731 }
Chenbo Feng5d534f42017-04-20 18:54:13 -07001732 sock_uid = sk->sk_uid;
JP Abgrall89d32dc2013-12-20 16:51:11 -08001733 if (do_tag_stat)
Chenbo Feng5d534f42017-04-20 18:54:13 -07001734 account_for_uid(skb, sk, from_kuid(&init_user_ns, sock_uid),
1735 par);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001736
1737 /*
1738 * The following two tests fail the match when:
1739 * id not in range AND no inverted condition requested
1740 * or id in range AND inverted condition requested
1741 * Thus (!a && b) || (a && !b) == a ^ b
1742 */
John Stultzbd1bca42014-03-28 16:23:48 -07001743 if (info->match & XT_QTAGUID_UID) {
1744 kuid_t uid_min = make_kuid(&init_user_ns, info->uid_min);
1745 kuid_t uid_max = make_kuid(&init_user_ns, info->uid_max);
1746
Chenbo Feng5d534f42017-04-20 18:54:13 -07001747 if ((uid_gte(sock_uid, uid_min) &&
1748 uid_lte(sock_uid, uid_max)) ^
JP Abgrallbaf0db42011-06-20 12:41:46 -07001749 !(info->invert & XT_QTAGUID_UID)) {
1750 MT_DEBUG("qtaguid[%d]: leaving uid not matching\n",
1751 par->hooknum);
1752 res = false;
1753 goto put_sock_ret_res;
1754 }
John Stultzbd1bca42014-03-28 16:23:48 -07001755 }
1756 if (info->match & XT_QTAGUID_GID) {
1757 kgid_t gid_min = make_kgid(&init_user_ns, info->gid_min);
1758 kgid_t gid_max = make_kgid(&init_user_ns, info->gid_max);
Chenbo Feng5d534f42017-04-20 18:54:13 -07001759 set_sk_callback_lock = true;
1760 read_lock_bh(&sk->sk_callback_lock);
1761 MT_DEBUG("qtaguid[%d]: sk=%p->sk_socket=%p->file=%p\n",
1762 par->hooknum, sk, sk->sk_socket,
1763 sk->sk_socket ? sk->sk_socket->file : (void *)-1LL);
1764 filp = sk->sk_socket ? sk->sk_socket->file : NULL;
1765 if (!filp) {
1766 res = ((info->match ^ info->invert) &
1767 XT_QTAGUID_GID) == 0;
1768 atomic64_inc(&qtu_events.match_no_sk_gid);
1769 goto put_sock_ret_res;
1770 }
1771 MT_DEBUG("qtaguid[%d]: filp...uid=%u\n",
1772 par->hooknum, filp ?
1773 from_kuid(&init_user_ns, filp->f_cred->fsuid) : -1);
Amit Pundir070eff82015-01-20 16:13:08 +05301774 if ((gid_gte(filp->f_cred->fsgid, gid_min) &&
1775 gid_lte(filp->f_cred->fsgid, gid_max)) ^
JP Abgrallbaf0db42011-06-20 12:41:46 -07001776 !(info->invert & XT_QTAGUID_GID)) {
1777 MT_DEBUG("qtaguid[%d]: leaving gid not matching\n",
1778 par->hooknum);
1779 res = false;
1780 goto put_sock_ret_res;
1781 }
John Stultzbd1bca42014-03-28 16:23:48 -07001782 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07001783 MT_DEBUG("qtaguid[%d]: leaving matched\n", par->hooknum);
1784 res = true;
1785
1786put_sock_ret_res:
1787 if (got_sock)
Amit Pundir2879b6e2015-01-29 01:16:23 +05301788 sock_gen_put(sk);
Mohamad Ayyashe8101fd2015-01-13 19:20:44 -08001789 if (set_sk_callback_lock)
1790 read_unlock_bh(&sk->sk_callback_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001791ret_res:
1792 MT_DEBUG("qtaguid[%d]: left %d\n", par->hooknum, res);
1793 return res;
1794}
1795
1796#ifdef DDEBUG
Chenbo Feng64157f42017-03-23 13:51:24 -07001797/*
1798 * This function is not in xt_qtaguid_print.c because of locks visibility.
1799 * The lock of sock_tag_list must be aquired before calling this function
1800 */
1801static void prdebug_full_state_locked(int indent_level, const char *fmt, ...)
JP Abgrallbaf0db42011-06-20 12:41:46 -07001802{
1803 va_list args;
1804 char *fmt_buff;
1805 char *buff;
1806
1807 if (!unlikely(qtaguid_debug_mask & DDEBUG_MASK))
1808 return;
1809
1810 fmt_buff = kasprintf(GFP_ATOMIC,
1811 "qtaguid: %s(): %s {\n", __func__, fmt);
1812 BUG_ON(!fmt_buff);
1813 va_start(args, fmt);
1814 buff = kvasprintf(GFP_ATOMIC,
1815 fmt_buff, args);
1816 BUG_ON(!buff);
1817 pr_debug("%s", buff);
1818 kfree(fmt_buff);
1819 kfree(buff);
1820 va_end(args);
1821
JP Abgrallbaf0db42011-06-20 12:41:46 -07001822 prdebug_sock_tag_tree(indent_level, &sock_tag_tree);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001823
JP Abgrallbaf0db42011-06-20 12:41:46 -07001824 spin_lock_bh(&uid_tag_data_tree_lock);
1825 prdebug_uid_tag_data_tree(indent_level, &uid_tag_data_tree);
1826 prdebug_proc_qtu_data_tree(indent_level, &proc_qtu_data_tree);
1827 spin_unlock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001828
1829 spin_lock_bh(&iface_stat_list_lock);
1830 prdebug_iface_stat_list(indent_level, &iface_stat_list);
1831 spin_unlock_bh(&iface_stat_list_lock);
1832
1833 pr_debug("qtaguid: %s(): }\n", __func__);
1834}
1835#else
Chenbo Feng64157f42017-03-23 13:51:24 -07001836static void prdebug_full_state_locked(int indent_level, const char *fmt, ...) {}
JP Abgrallbaf0db42011-06-20 12:41:46 -07001837#endif
1838
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001839struct proc_ctrl_print_info {
1840 struct sock *sk; /* socket found by reading to sk_pos */
1841 loff_t sk_pos;
1842};
1843
1844static void *qtaguid_ctrl_proc_next(struct seq_file *m, void *v, loff_t *pos)
1845{
1846 struct proc_ctrl_print_info *pcpi = m->private;
1847 struct sock_tag *sock_tag_entry = v;
1848 struct rb_node *node;
1849
1850 (*pos)++;
1851
1852 if (!v || v == SEQ_START_TOKEN)
1853 return NULL;
1854
1855 node = rb_next(&sock_tag_entry->sock_node);
1856 if (!node) {
1857 pcpi->sk = NULL;
1858 sock_tag_entry = SEQ_START_TOKEN;
1859 } else {
1860 sock_tag_entry = rb_entry(node, struct sock_tag, sock_node);
1861 pcpi->sk = sock_tag_entry->sk;
1862 }
1863 pcpi->sk_pos = *pos;
1864 return sock_tag_entry;
1865}
1866
1867static void *qtaguid_ctrl_proc_start(struct seq_file *m, loff_t *pos)
1868{
1869 struct proc_ctrl_print_info *pcpi = m->private;
1870 struct sock_tag *sock_tag_entry;
1871 struct rb_node *node;
1872
1873 spin_lock_bh(&sock_tag_list_lock);
1874
1875 if (unlikely(module_passive))
1876 return NULL;
1877
1878 if (*pos == 0) {
1879 pcpi->sk_pos = 0;
1880 node = rb_first(&sock_tag_tree);
1881 if (!node) {
1882 pcpi->sk = NULL;
1883 return SEQ_START_TOKEN;
1884 }
1885 sock_tag_entry = rb_entry(node, struct sock_tag, sock_node);
1886 pcpi->sk = sock_tag_entry->sk;
1887 } else {
1888 sock_tag_entry = (pcpi->sk ? get_sock_stat_nl(pcpi->sk) :
1889 NULL) ?: SEQ_START_TOKEN;
1890 if (*pos != pcpi->sk_pos) {
1891 /* seq_read skipped a next call */
1892 *pos = pcpi->sk_pos;
1893 return qtaguid_ctrl_proc_next(m, sock_tag_entry, pos);
1894 }
1895 }
1896 return sock_tag_entry;
1897}
1898
1899static void qtaguid_ctrl_proc_stop(struct seq_file *m, void *v)
1900{
1901 spin_unlock_bh(&sock_tag_list_lock);
1902}
1903
JP Abgrallbaf0db42011-06-20 12:41:46 -07001904/*
1905 * Procfs reader to get all active socket tags using style "1)" as described in
1906 * fs/proc/generic.c
1907 */
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001908static int qtaguid_ctrl_proc_show(struct seq_file *m, void *v)
JP Abgrallbaf0db42011-06-20 12:41:46 -07001909{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001910 struct sock_tag *sock_tag_entry = v;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001911 uid_t uid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001912
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001913 CT_DEBUG("qtaguid: proc ctrl pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07001914 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07001915
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001916 if (sock_tag_entry != SEQ_START_TOKEN) {
Chenbo Feng875e5262017-04-19 14:22:47 -07001917 int sk_ref_count;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001918 uid = get_uid_from_tag(sock_tag_entry->tag);
1919 CT_DEBUG("qtaguid: proc_read(): sk=%p tag=0x%llx (uid=%u) "
1920 "pid=%u\n",
1921 sock_tag_entry->sk,
1922 sock_tag_entry->tag,
1923 uid,
1924 sock_tag_entry->pid
1925 );
Chenbo Feng875e5262017-04-19 14:22:47 -07001926 sk_ref_count = atomic_read(
1927 &sock_tag_entry->sk->sk_refcnt);
Mohamad Ayyash8613d932016-05-11 13:18:35 -07001928 seq_printf(m, "sock=%pK tag=0x%llx (uid=%u) pid=%u "
Chenbo Feng875e5262017-04-19 14:22:47 -07001929 "f_count=%d\n",
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001930 sock_tag_entry->sk,
1931 sock_tag_entry->tag, uid,
Chenbo Feng875e5262017-04-19 14:22:47 -07001932 sock_tag_entry->pid, sk_ref_count);
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001933 } else {
1934 seq_printf(m, "events: sockets_tagged=%llu "
1935 "sockets_untagged=%llu "
1936 "counter_set_changes=%llu "
1937 "delete_cmds=%llu "
1938 "iface_events=%llu "
1939 "match_calls=%llu "
1940 "match_calls_prepost=%llu "
1941 "match_found_sk=%llu "
1942 "match_found_sk_in_ct=%llu "
1943 "match_found_no_sk_in_ct=%llu "
1944 "match_no_sk=%llu "
Chenbo Feng5d534f42017-04-20 18:54:13 -07001945 "match_no_sk_gid=%llu\n",
Sherman Yinda5ea992014-06-12 14:35:38 -07001946 (u64)atomic64_read(&qtu_events.sockets_tagged),
1947 (u64)atomic64_read(&qtu_events.sockets_untagged),
1948 (u64)atomic64_read(&qtu_events.counter_set_changes),
1949 (u64)atomic64_read(&qtu_events.delete_cmds),
1950 (u64)atomic64_read(&qtu_events.iface_events),
1951 (u64)atomic64_read(&qtu_events.match_calls),
1952 (u64)atomic64_read(&qtu_events.match_calls_prepost),
1953 (u64)atomic64_read(&qtu_events.match_found_sk),
1954 (u64)atomic64_read(&qtu_events.match_found_sk_in_ct),
1955 (u64)atomic64_read(&qtu_events.match_found_no_sk_in_ct),
1956 (u64)atomic64_read(&qtu_events.match_no_sk),
Chenbo Feng5d534f42017-04-20 18:54:13 -07001957 (u64)atomic64_read(&qtu_events.match_no_sk_gid));
JP Abgrallbaf0db42011-06-20 12:41:46 -07001958
Chenbo Feng64157f42017-03-23 13:51:24 -07001959 /* Count the following as part of the last item_index. No need
1960 * to lock the sock_tag_list here since it is already locked when
1961 * starting the seq_file operation
1962 */
1963 prdebug_full_state_locked(0, "proc ctrl");
JP Abgrallbaf0db42011-06-20 12:41:46 -07001964 }
1965
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07001966 return 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001967}
1968
1969/*
1970 * Delete socket tags, and stat tags associated with a given
1971 * accouting tag and uid.
1972 */
1973static int ctrl_cmd_delete(const char *input)
1974{
1975 char cmd;
John Stultzbd1bca42014-03-28 16:23:48 -07001976 int uid_int;
1977 kuid_t uid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07001978 uid_t entry_uid;
1979 tag_t acct_tag;
1980 tag_t tag;
1981 int res, argc;
1982 struct iface_stat *iface_entry;
1983 struct rb_node *node;
1984 struct sock_tag *st_entry;
1985 struct rb_root st_to_free_tree = RB_ROOT;
1986 struct tag_stat *ts_entry;
1987 struct tag_counter_set *tcs_entry;
1988 struct tag_ref *tr_entry;
1989 struct uid_tag_data *utd_entry;
1990
John Stultzbd1bca42014-03-28 16:23:48 -07001991 argc = sscanf(input, "%c %llu %u", &cmd, &acct_tag, &uid_int);
1992 uid = make_kuid(&init_user_ns, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001993 CT_DEBUG("qtaguid: ctrl_delete(%s): argc=%d cmd=%c "
1994 "user_tag=0x%llx uid=%u\n", input, argc, cmd,
John Stultzbd1bca42014-03-28 16:23:48 -07001995 acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07001996 if (argc < 2) {
1997 res = -EINVAL;
1998 goto err;
1999 }
2000 if (!valid_atag(acct_tag)) {
2001 pr_info("qtaguid: ctrl_delete(%s): invalid tag\n", input);
2002 res = -EINVAL;
2003 goto err;
2004 }
2005 if (argc < 3) {
2006 uid = current_fsuid();
John Stultzbd1bca42014-03-28 16:23:48 -07002007 uid_int = from_kuid(&init_user_ns, uid);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002008 } else if (!can_impersonate_uid(uid)) {
2009 pr_info("qtaguid: ctrl_delete(%s): "
2010 "insufficient priv from pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002011 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002012 res = -EPERM;
2013 goto err;
2014 }
2015
John Stultzbd1bca42014-03-28 16:23:48 -07002016 tag = combine_atag_with_uid(acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002017 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2018 "looking for tag=0x%llx (uid=%u)\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002019 input, tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002020
2021 /* Delete socket tags */
2022 spin_lock_bh(&sock_tag_list_lock);
Chenbo Feng20c8a002017-11-28 18:22:11 -08002023 spin_lock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002024 node = rb_first(&sock_tag_tree);
2025 while (node) {
2026 st_entry = rb_entry(node, struct sock_tag, sock_node);
2027 entry_uid = get_uid_from_tag(st_entry->tag);
2028 node = rb_next(node);
John Stultzbd1bca42014-03-28 16:23:48 -07002029 if (entry_uid != uid_int)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002030 continue;
2031
2032 CT_DEBUG("qtaguid: ctrl_delete(%s): st tag=0x%llx (uid=%u)\n",
2033 input, st_entry->tag, entry_uid);
2034
2035 if (!acct_tag || st_entry->tag == tag) {
2036 rb_erase(&st_entry->sock_node, &sock_tag_tree);
2037 /* Can't sockfd_put() within spinlock, do it later. */
2038 sock_tag_tree_insert(st_entry, &st_to_free_tree);
2039 tr_entry = lookup_tag_ref(st_entry->tag, NULL);
2040 BUG_ON(tr_entry->num_sock_tags <= 0);
2041 tr_entry->num_sock_tags--;
2042 /*
2043 * TODO: remove if, and start failing.
2044 * This is a hack to work around the fact that in some
2045 * places we have "if (IS_ERR_OR_NULL(pqd_entry))"
2046 * and are trying to work around apps
2047 * that didn't open the /dev/xt_qtaguid.
2048 */
2049 if (st_entry->list.next && st_entry->list.prev)
2050 list_del(&st_entry->list);
2051 }
2052 }
Chenbo Feng20c8a002017-11-28 18:22:11 -08002053 spin_unlock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002054 spin_unlock_bh(&sock_tag_list_lock);
2055
2056 sock_tag_tree_erase(&st_to_free_tree);
2057
2058 /* Delete tag counter-sets */
2059 spin_lock_bh(&tag_counter_set_list_lock);
2060 /* Counter sets are only on the uid tag, not full tag */
2061 tcs_entry = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
2062 if (tcs_entry) {
2063 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2064 "erase tcs: tag=0x%llx (uid=%u) set=%d\n",
2065 input,
2066 tcs_entry->tn.tag,
2067 get_uid_from_tag(tcs_entry->tn.tag),
2068 tcs_entry->active_set);
2069 rb_erase(&tcs_entry->tn.node, &tag_counter_set_tree);
2070 kfree(tcs_entry);
2071 }
2072 spin_unlock_bh(&tag_counter_set_list_lock);
2073
2074 /*
2075 * If acct_tag is 0, then all entries belonging to uid are
2076 * erased.
2077 */
2078 spin_lock_bh(&iface_stat_list_lock);
2079 list_for_each_entry(iface_entry, &iface_stat_list, list) {
2080 spin_lock_bh(&iface_entry->tag_stat_list_lock);
2081 node = rb_first(&iface_entry->tag_stat_tree);
2082 while (node) {
2083 ts_entry = rb_entry(node, struct tag_stat, tn.node);
2084 entry_uid = get_uid_from_tag(ts_entry->tn.tag);
2085 node = rb_next(node);
2086
2087 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2088 "ts tag=0x%llx (uid=%u)\n",
2089 input, ts_entry->tn.tag, entry_uid);
2090
John Stultzbd1bca42014-03-28 16:23:48 -07002091 if (entry_uid != uid_int)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002092 continue;
2093 if (!acct_tag || ts_entry->tn.tag == tag) {
2094 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2095 "erase ts: %s 0x%llx %u\n",
2096 input, iface_entry->ifname,
2097 get_atag_from_tag(ts_entry->tn.tag),
2098 entry_uid);
2099 rb_erase(&ts_entry->tn.node,
2100 &iface_entry->tag_stat_tree);
2101 kfree(ts_entry);
2102 }
2103 }
2104 spin_unlock_bh(&iface_entry->tag_stat_list_lock);
2105 }
2106 spin_unlock_bh(&iface_stat_list_lock);
2107
2108 /* Cleanup the uid_tag_data */
2109 spin_lock_bh(&uid_tag_data_tree_lock);
2110 node = rb_first(&uid_tag_data_tree);
2111 while (node) {
2112 utd_entry = rb_entry(node, struct uid_tag_data, node);
2113 entry_uid = utd_entry->uid;
2114 node = rb_next(node);
2115
2116 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2117 "utd uid=%u\n",
2118 input, entry_uid);
2119
John Stultzbd1bca42014-03-28 16:23:48 -07002120 if (entry_uid != uid_int)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002121 continue;
2122 /*
2123 * Go over the tag_refs, and those that don't have
2124 * sock_tags using them are freed.
2125 */
2126 put_tag_ref_tree(tag, utd_entry);
2127 put_utd_entry(utd_entry);
2128 }
2129 spin_unlock_bh(&uid_tag_data_tree_lock);
2130
2131 atomic64_inc(&qtu_events.delete_cmds);
2132 res = 0;
2133
2134err:
2135 return res;
2136}
2137
2138static int ctrl_cmd_counter_set(const char *input)
2139{
2140 char cmd;
2141 uid_t uid = 0;
2142 tag_t tag;
2143 int res, argc;
2144 struct tag_counter_set *tcs;
2145 int counter_set;
2146
2147 argc = sscanf(input, "%c %d %u", &cmd, &counter_set, &uid);
2148 CT_DEBUG("qtaguid: ctrl_counterset(%s): argc=%d cmd=%c "
2149 "set=%d uid=%u\n", input, argc, cmd,
2150 counter_set, uid);
2151 if (argc != 3) {
2152 res = -EINVAL;
2153 goto err;
2154 }
2155 if (counter_set < 0 || counter_set >= IFS_MAX_COUNTER_SETS) {
2156 pr_info("qtaguid: ctrl_counterset(%s): invalid counter_set range\n",
2157 input);
2158 res = -EINVAL;
2159 goto err;
2160 }
2161 if (!can_manipulate_uids()) {
2162 pr_info("qtaguid: ctrl_counterset(%s): "
2163 "insufficient priv from pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002164 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002165 res = -EPERM;
2166 goto err;
2167 }
2168
2169 tag = make_tag_from_uid(uid);
2170 spin_lock_bh(&tag_counter_set_list_lock);
2171 tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
2172 if (!tcs) {
2173 tcs = kzalloc(sizeof(*tcs), GFP_ATOMIC);
2174 if (!tcs) {
2175 spin_unlock_bh(&tag_counter_set_list_lock);
2176 pr_err("qtaguid: ctrl_counterset(%s): "
2177 "failed to alloc counter set\n",
2178 input);
2179 res = -ENOMEM;
2180 goto err;
2181 }
2182 tcs->tn.tag = tag;
2183 tag_counter_set_tree_insert(tcs, &tag_counter_set_tree);
2184 CT_DEBUG("qtaguid: ctrl_counterset(%s): added tcs tag=0x%llx "
2185 "(uid=%u) set=%d\n",
2186 input, tag, get_uid_from_tag(tag), counter_set);
2187 }
2188 tcs->active_set = counter_set;
2189 spin_unlock_bh(&tag_counter_set_list_lock);
2190 atomic64_inc(&qtu_events.counter_set_changes);
2191 res = 0;
2192
2193err:
2194 return res;
2195}
2196
2197static int ctrl_cmd_tag(const char *input)
2198{
2199 char cmd;
2200 int sock_fd = 0;
John Stultzbd1bca42014-03-28 16:23:48 -07002201 kuid_t uid;
2202 unsigned int uid_int = 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002203 tag_t acct_tag = make_atag_from_value(0);
2204 tag_t full_tag;
2205 struct socket *el_socket;
2206 int res, argc;
2207 struct sock_tag *sock_tag_entry;
2208 struct tag_ref *tag_ref_entry;
2209 struct uid_tag_data *uid_tag_data_entry;
2210 struct proc_qtu_data *pqd_entry;
2211
2212 /* Unassigned args will get defaulted later. */
John Stultzbd1bca42014-03-28 16:23:48 -07002213 argc = sscanf(input, "%c %d %llu %u", &cmd, &sock_fd, &acct_tag, &uid_int);
2214 uid = make_kuid(&init_user_ns, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002215 CT_DEBUG("qtaguid: ctrl_tag(%s): argc=%d cmd=%c sock_fd=%d "
2216 "acct_tag=0x%llx uid=%u\n", input, argc, cmd, sock_fd,
John Stultzbd1bca42014-03-28 16:23:48 -07002217 acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002218 if (argc < 2) {
2219 res = -EINVAL;
2220 goto err;
2221 }
2222 el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */
2223 if (!el_socket) {
2224 pr_info("qtaguid: ctrl_tag(%s): failed to lookup"
JP Abgrall9e0858c2012-04-27 12:57:39 -07002225 " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n",
2226 input, sock_fd, res, current->pid, current->tgid,
John Stultzbd1bca42014-03-28 16:23:48 -07002227 from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002228 goto err;
2229 }
Chenbo Feng875e5262017-04-19 14:22:47 -07002230 CT_DEBUG("qtaguid: ctrl_tag(%s): socket->...->sk_refcnt=%d ->sk=%p\n",
2231 input, atomic_read(&el_socket->sk->sk_refcnt),
JP Abgrallbaf0db42011-06-20 12:41:46 -07002232 el_socket->sk);
2233 if (argc < 3) {
2234 acct_tag = make_atag_from_value(0);
2235 } else if (!valid_atag(acct_tag)) {
2236 pr_info("qtaguid: ctrl_tag(%s): invalid tag\n", input);
2237 res = -EINVAL;
2238 goto err_put;
2239 }
2240 CT_DEBUG("qtaguid: ctrl_tag(%s): "
2241 "pid=%u tgid=%u uid=%u euid=%u fsuid=%u "
JP Abgrall90414bc2013-01-04 18:18:36 -08002242 "ctrl.gid=%u in_group()=%d in_egroup()=%d\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002243 input, current->pid, current->tgid,
2244 from_kuid(&init_user_ns, current_uid()),
2245 from_kuid(&init_user_ns, current_euid()),
2246 from_kuid(&init_user_ns, current_fsuid()),
2247 from_kgid(&init_user_ns, xt_qtaguid_ctrl_file->gid),
JP Abgrall90414bc2013-01-04 18:18:36 -08002248 in_group_p(xt_qtaguid_ctrl_file->gid),
2249 in_egroup_p(xt_qtaguid_ctrl_file->gid));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002250 if (argc < 4) {
2251 uid = current_fsuid();
John Stultzbd1bca42014-03-28 16:23:48 -07002252 uid_int = from_kuid(&init_user_ns, uid);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002253 } else if (!can_impersonate_uid(uid)) {
2254 pr_info("qtaguid: ctrl_tag(%s): "
2255 "insufficient priv from pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002256 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002257 res = -EPERM;
2258 goto err_put;
2259 }
John Stultzbd1bca42014-03-28 16:23:48 -07002260 full_tag = combine_atag_with_uid(acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002261
2262 spin_lock_bh(&sock_tag_list_lock);
Chenbo Feng20c8a002017-11-28 18:22:11 -08002263 spin_lock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002264 sock_tag_entry = get_sock_stat_nl(el_socket->sk);
2265 tag_ref_entry = get_tag_ref(full_tag, &uid_tag_data_entry);
2266 if (IS_ERR(tag_ref_entry)) {
2267 res = PTR_ERR(tag_ref_entry);
Chenbo Feng20c8a002017-11-28 18:22:11 -08002268 spin_unlock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002269 spin_unlock_bh(&sock_tag_list_lock);
2270 goto err_put;
2271 }
2272 tag_ref_entry->num_sock_tags++;
2273 if (sock_tag_entry) {
2274 struct tag_ref *prev_tag_ref_entry;
2275
2276 CT_DEBUG("qtaguid: ctrl_tag(%s): retag for sk=%p "
Chenbo Feng875e5262017-04-19 14:22:47 -07002277 "st@%p ...->sk_refcnt=%d\n",
JP Abgrallbaf0db42011-06-20 12:41:46 -07002278 input, el_socket->sk, sock_tag_entry,
Chenbo Feng875e5262017-04-19 14:22:47 -07002279 atomic_read(&el_socket->sk->sk_refcnt));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002280 prev_tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag,
2281 &uid_tag_data_entry);
2282 BUG_ON(IS_ERR_OR_NULL(prev_tag_ref_entry));
2283 BUG_ON(prev_tag_ref_entry->num_sock_tags <= 0);
2284 prev_tag_ref_entry->num_sock_tags--;
2285 sock_tag_entry->tag = full_tag;
2286 } else {
2287 CT_DEBUG("qtaguid: ctrl_tag(%s): newtag for sk=%p\n",
2288 input, el_socket->sk);
2289 sock_tag_entry = kzalloc(sizeof(*sock_tag_entry),
2290 GFP_ATOMIC);
2291 if (!sock_tag_entry) {
2292 pr_err("qtaguid: ctrl_tag(%s): "
2293 "socket tag alloc failed\n",
2294 input);
Chenbo Feng20c8a002017-11-28 18:22:11 -08002295 BUG_ON(tag_ref_entry->num_sock_tags <= 0);
2296 tag_ref_entry->num_sock_tags--;
2297 free_tag_ref_from_utd_entry(tag_ref_entry,
2298 uid_tag_data_entry);
2299 spin_unlock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002300 spin_unlock_bh(&sock_tag_list_lock);
2301 res = -ENOMEM;
Chenbo Feng20c8a002017-11-28 18:22:11 -08002302 goto err_put;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002303 }
Chenbo Feng875e5262017-04-19 14:22:47 -07002304 /*
2305 * Hold the sk refcount here to make sure the sk pointer cannot
2306 * be freed and reused
2307 */
2308 sock_hold(el_socket->sk);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002309 sock_tag_entry->sk = el_socket->sk;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002310 sock_tag_entry->pid = current->tgid;
John Stultzbd1bca42014-03-28 16:23:48 -07002311 sock_tag_entry->tag = combine_atag_with_uid(acct_tag, uid_int);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002312 pqd_entry = proc_qtu_data_tree_search(
2313 &proc_qtu_data_tree, current->tgid);
2314 /*
2315 * TODO: remove if, and start failing.
2316 * At first, we want to catch user-space code that is not
2317 * opening the /dev/xt_qtaguid.
2318 */
2319 if (IS_ERR_OR_NULL(pqd_entry))
2320 pr_warn_once(
2321 "qtaguid: %s(): "
2322 "User space forgot to open /dev/xt_qtaguid? "
2323 "pid=%u tgid=%u uid=%u\n", __func__,
2324 current->pid, current->tgid,
John Stultzbd1bca42014-03-28 16:23:48 -07002325 from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002326 else
2327 list_add(&sock_tag_entry->list,
2328 &pqd_entry->sock_tag_list);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002329
2330 sock_tag_tree_insert(sock_tag_entry, &sock_tag_tree);
2331 atomic64_inc(&qtu_events.sockets_tagged);
2332 }
Chenbo Feng20c8a002017-11-28 18:22:11 -08002333 spin_unlock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002334 spin_unlock_bh(&sock_tag_list_lock);
Chenbo Feng875e5262017-04-19 14:22:47 -07002335 /* We keep the ref to the sk until it is untagged */
2336 CT_DEBUG("qtaguid: ctrl_tag(%s): done st@%p ...->sk_refcnt=%d\n",
JP Abgrallbaf0db42011-06-20 12:41:46 -07002337 input, sock_tag_entry,
Chenbo Feng875e5262017-04-19 14:22:47 -07002338 atomic_read(&el_socket->sk->sk_refcnt));
2339 sockfd_put(el_socket);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002340 return 0;
2341
JP Abgrallbaf0db42011-06-20 12:41:46 -07002342err_put:
Chenbo Feng875e5262017-04-19 14:22:47 -07002343 CT_DEBUG("qtaguid: ctrl_tag(%s): done. ...->sk_refcnt=%d\n",
2344 input, atomic_read(&el_socket->sk->sk_refcnt) - 1);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002345 /* Release the sock_fd that was grabbed by sockfd_lookup(). */
2346 sockfd_put(el_socket);
2347 return res;
2348
2349err:
2350 CT_DEBUG("qtaguid: ctrl_tag(%s): done.\n", input);
2351 return res;
2352}
2353
2354static int ctrl_cmd_untag(const char *input)
2355{
2356 char cmd;
2357 int sock_fd = 0;
2358 struct socket *el_socket;
2359 int res, argc;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002360
2361 argc = sscanf(input, "%c %d", &cmd, &sock_fd);
2362 CT_DEBUG("qtaguid: ctrl_untag(%s): argc=%d cmd=%c sock_fd=%d\n",
2363 input, argc, cmd, sock_fd);
2364 if (argc < 2) {
2365 res = -EINVAL;
Chenbo Feng875e5262017-04-19 14:22:47 -07002366 return res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002367 }
2368 el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */
2369 if (!el_socket) {
2370 pr_info("qtaguid: ctrl_untag(%s): failed to lookup"
JP Abgrall9e0858c2012-04-27 12:57:39 -07002371 " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n",
2372 input, sock_fd, res, current->pid, current->tgid,
John Stultzbd1bca42014-03-28 16:23:48 -07002373 from_kuid(&init_user_ns, current_fsuid()));
Chenbo Feng875e5262017-04-19 14:22:47 -07002374 return res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002375 }
2376 CT_DEBUG("qtaguid: ctrl_untag(%s): socket->...->f_count=%ld ->sk=%p\n",
2377 input, atomic_long_read(&el_socket->file->f_count),
2378 el_socket->sk);
Chenbo Feng875e5262017-04-19 14:22:47 -07002379 res = qtaguid_untag(el_socket, false);
2380 sockfd_put(el_socket);
2381 return res;
2382}
2383
2384int qtaguid_untag(struct socket *el_socket, bool kernel)
2385{
2386 int res;
2387 pid_t pid;
2388 struct sock_tag *sock_tag_entry;
2389 struct tag_ref *tag_ref_entry;
2390 struct uid_tag_data *utd_entry;
2391 struct proc_qtu_data *pqd_entry;
2392
JP Abgrallbaf0db42011-06-20 12:41:46 -07002393 spin_lock_bh(&sock_tag_list_lock);
2394 sock_tag_entry = get_sock_stat_nl(el_socket->sk);
2395 if (!sock_tag_entry) {
2396 spin_unlock_bh(&sock_tag_list_lock);
2397 res = -EINVAL;
Chenbo Feng875e5262017-04-19 14:22:47 -07002398 return res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002399 }
2400 /*
2401 * The socket already belongs to the current process
2402 * so it can do whatever it wants to it.
2403 */
2404 rb_erase(&sock_tag_entry->sock_node, &sock_tag_tree);
2405
2406 tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag, &utd_entry);
2407 BUG_ON(!tag_ref_entry);
2408 BUG_ON(tag_ref_entry->num_sock_tags <= 0);
2409 spin_lock_bh(&uid_tag_data_tree_lock);
Chenbo Feng875e5262017-04-19 14:22:47 -07002410 if (kernel)
2411 pid = sock_tag_entry->pid;
2412 else
2413 pid = current->tgid;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002414 pqd_entry = proc_qtu_data_tree_search(
Chenbo Feng875e5262017-04-19 14:22:47 -07002415 &proc_qtu_data_tree, pid);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002416 /*
2417 * TODO: remove if, and start failing.
2418 * At first, we want to catch user-space code that is not
2419 * opening the /dev/xt_qtaguid.
2420 */
Chenbo Feng875e5262017-04-19 14:22:47 -07002421 if (IS_ERR_OR_NULL(pqd_entry) || !sock_tag_entry->list.next) {
JP Abgrallbaf0db42011-06-20 12:41:46 -07002422 pr_warn_once("qtaguid: %s(): "
2423 "User space forgot to open /dev/xt_qtaguid? "
Chenbo Feng875e5262017-04-19 14:22:47 -07002424 "pid=%u tgid=%u sk_pid=%u, uid=%u\n", __func__,
2425 current->pid, current->tgid, sock_tag_entry->pid,
2426 from_kuid(&init_user_ns, current_fsuid()));
2427 } else {
JP Abgrallbaf0db42011-06-20 12:41:46 -07002428 list_del(&sock_tag_entry->list);
Chenbo Feng875e5262017-04-19 14:22:47 -07002429 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07002430 spin_unlock_bh(&uid_tag_data_tree_lock);
2431 /*
2432 * We don't free tag_ref from the utd_entry here,
2433 * only during a cmd_delete().
2434 */
2435 tag_ref_entry->num_sock_tags--;
2436 spin_unlock_bh(&sock_tag_list_lock);
2437 /*
Chenbo Feng875e5262017-04-19 14:22:47 -07002438 * Release the sock_fd that was grabbed at tag time.
JP Abgrallbaf0db42011-06-20 12:41:46 -07002439 */
Chenbo Feng875e5262017-04-19 14:22:47 -07002440 sock_put(sock_tag_entry->sk);
2441 CT_DEBUG("qtaguid: done. st@%p ...->sk_refcnt=%d\n",
2442 sock_tag_entry,
2443 atomic_read(&el_socket->sk->sk_refcnt));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002444
2445 kfree(sock_tag_entry);
2446 atomic64_inc(&qtu_events.sockets_untagged);
2447
2448 return 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002449}
2450
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002451static ssize_t qtaguid_ctrl_parse(const char *input, size_t count)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002452{
2453 char cmd;
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002454 ssize_t res;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002455
JP Abgrall9e0858c2012-04-27 12:57:39 -07002456 CT_DEBUG("qtaguid: ctrl(%s): pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002457 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrall9e0858c2012-04-27 12:57:39 -07002458
JP Abgrallbaf0db42011-06-20 12:41:46 -07002459 cmd = input[0];
2460 /* Collect params for commands */
2461 switch (cmd) {
2462 case 'd':
2463 res = ctrl_cmd_delete(input);
2464 break;
2465
2466 case 's':
2467 res = ctrl_cmd_counter_set(input);
2468 break;
2469
2470 case 't':
2471 res = ctrl_cmd_tag(input);
2472 break;
2473
2474 case 'u':
2475 res = ctrl_cmd_untag(input);
2476 break;
2477
2478 default:
2479 res = -EINVAL;
2480 goto err;
2481 }
2482 if (!res)
2483 res = count;
2484err:
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002485 CT_DEBUG("qtaguid: ctrl(%s): res=%zd\n", input, res);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002486 return res;
2487}
2488
2489#define MAX_QTAGUID_CTRL_INPUT_LEN 255
Greg Hackmann85a2eb52014-02-24 09:39:46 -08002490static ssize_t qtaguid_ctrl_proc_write(struct file *file, const char __user *buffer,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002491 size_t count, loff_t *offp)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002492{
2493 char input_buf[MAX_QTAGUID_CTRL_INPUT_LEN];
2494
2495 if (unlikely(module_passive))
2496 return count;
2497
2498 if (count >= MAX_QTAGUID_CTRL_INPUT_LEN)
2499 return -EINVAL;
2500
2501 if (copy_from_user(input_buf, buffer, count))
2502 return -EFAULT;
2503
2504 input_buf[count] = '\0';
2505 return qtaguid_ctrl_parse(input_buf, count);
2506}
2507
2508struct proc_print_info {
JP Abgrallbaf0db42011-06-20 12:41:46 -07002509 struct iface_stat *iface_entry;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002510 int item_index;
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002511 tag_t tag; /* tag found by reading to tag_pos */
2512 off_t tag_pos;
2513 int tag_item_index;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002514};
2515
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002516static void pp_stats_header(struct seq_file *m)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002517{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002518 seq_puts(m,
2519 "idx iface acct_tag_hex uid_tag_int cnt_set "
2520 "rx_bytes rx_packets "
2521 "tx_bytes tx_packets "
2522 "rx_tcp_bytes rx_tcp_packets "
2523 "rx_udp_bytes rx_udp_packets "
2524 "rx_other_bytes rx_other_packets "
2525 "tx_tcp_bytes tx_tcp_packets "
2526 "tx_udp_bytes tx_udp_packets "
2527 "tx_other_bytes tx_other_packets\n");
JP Abgrallbaf0db42011-06-20 12:41:46 -07002528}
2529
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002530static int pp_stats_line(struct seq_file *m, struct tag_stat *ts_entry,
2531 int cnt_set)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002532{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002533 struct data_counters *cnts;
2534 tag_t tag = ts_entry->tn.tag;
2535 uid_t stat_uid = get_uid_from_tag(tag);
2536 struct proc_print_info *ppi = m->private;
2537 /* Detailed tags are not available to everybody */
Mohamad Ayyash8613d932016-05-11 13:18:35 -07002538 if (!can_read_other_uid_stats(make_kuid(&init_user_ns,stat_uid))) {
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002539 CT_DEBUG("qtaguid: stats line: "
2540 "%s 0x%llx %u: insufficient priv "
2541 "from pid=%u tgid=%u uid=%u stats.gid=%u\n",
2542 ppi->iface_entry->ifname,
2543 get_atag_from_tag(tag), stat_uid,
John Stultzbd1bca42014-03-28 16:23:48 -07002544 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()),
2545 from_kgid(&init_user_ns,xt_qtaguid_stats_file->gid));
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002546 return 0;
2547 }
2548 ppi->item_index++;
2549 cnts = &ts_entry->counters;
Amit Pundir5b5ab942015-10-01 10:44:36 +05302550 seq_printf(m, "%d %s 0x%llx %u %u "
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002551 "%llu %llu "
2552 "%llu %llu "
2553 "%llu %llu "
2554 "%llu %llu "
2555 "%llu %llu "
2556 "%llu %llu "
2557 "%llu %llu "
2558 "%llu %llu\n",
2559 ppi->item_index,
2560 ppi->iface_entry->ifname,
2561 get_atag_from_tag(tag),
2562 stat_uid,
2563 cnt_set,
2564 dc_sum_bytes(cnts, cnt_set, IFS_RX),
2565 dc_sum_packets(cnts, cnt_set, IFS_RX),
2566 dc_sum_bytes(cnts, cnt_set, IFS_TX),
2567 dc_sum_packets(cnts, cnt_set, IFS_TX),
2568 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes,
2569 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets,
2570 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes,
2571 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets,
2572 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes,
2573 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets,
2574 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes,
2575 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets,
2576 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes,
2577 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets,
2578 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes,
2579 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets);
Amit Pundir5b5ab942015-10-01 10:44:36 +05302580 return seq_has_overflowed(m) ? -ENOSPC : 1;
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002581}
2582
2583static bool pp_sets(struct seq_file *m, struct tag_stat *ts_entry)
2584{
2585 int ret;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002586 int counter_set;
2587 for (counter_set = 0; counter_set < IFS_MAX_COUNTER_SETS;
2588 counter_set++) {
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002589 ret = pp_stats_line(m, ts_entry, counter_set);
2590 if (ret < 0)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002591 return false;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002592 }
2593 return true;
2594}
2595
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002596static int qtaguid_stats_proc_iface_stat_ptr_valid(struct iface_stat *ptr)
2597{
2598 struct iface_stat *iface_entry;
2599
2600 if (!ptr)
2601 return false;
2602
2603 list_for_each_entry(iface_entry, &iface_stat_list, list)
2604 if (iface_entry == ptr)
2605 return true;
2606 return false;
2607}
2608
2609static void qtaguid_stats_proc_next_iface_entry(struct proc_print_info *ppi)
2610{
2611 spin_unlock_bh(&ppi->iface_entry->tag_stat_list_lock);
2612 list_for_each_entry_continue(ppi->iface_entry, &iface_stat_list, list) {
2613 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2614 return;
2615 }
2616 ppi->iface_entry = NULL;
2617}
2618
2619static void *qtaguid_stats_proc_next(struct seq_file *m, void *v, loff_t *pos)
2620{
2621 struct proc_print_info *ppi = m->private;
2622 struct tag_stat *ts_entry;
2623 struct rb_node *node;
2624
2625 if (!v) {
2626 pr_err("qtaguid: %s(): unexpected v: NULL\n", __func__);
2627 return NULL;
2628 }
2629
2630 (*pos)++;
2631
2632 if (!ppi->iface_entry || unlikely(module_passive))
2633 return NULL;
2634
2635 if (v == SEQ_START_TOKEN)
2636 node = rb_first(&ppi->iface_entry->tag_stat_tree);
2637 else
2638 node = rb_next(&((struct tag_stat *)v)->tn.node);
2639
2640 while (!node) {
2641 qtaguid_stats_proc_next_iface_entry(ppi);
2642 if (!ppi->iface_entry)
2643 return NULL;
2644 node = rb_first(&ppi->iface_entry->tag_stat_tree);
2645 }
2646
2647 ts_entry = rb_entry(node, struct tag_stat, tn.node);
2648 ppi->tag = ts_entry->tn.tag;
2649 ppi->tag_pos = *pos;
2650 ppi->tag_item_index = ppi->item_index;
2651 return ts_entry;
2652}
2653
2654static void *qtaguid_stats_proc_start(struct seq_file *m, loff_t *pos)
2655{
2656 struct proc_print_info *ppi = m->private;
2657 struct tag_stat *ts_entry = NULL;
2658
2659 spin_lock_bh(&iface_stat_list_lock);
2660
2661 if (*pos == 0) {
2662 ppi->item_index = 1;
2663 ppi->tag_pos = 0;
2664 if (list_empty(&iface_stat_list)) {
2665 ppi->iface_entry = NULL;
2666 } else {
2667 ppi->iface_entry = list_first_entry(&iface_stat_list,
2668 struct iface_stat,
2669 list);
2670 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2671 }
2672 return SEQ_START_TOKEN;
2673 }
2674 if (!qtaguid_stats_proc_iface_stat_ptr_valid(ppi->iface_entry)) {
2675 if (ppi->iface_entry) {
2676 pr_err("qtaguid: %s(): iface_entry %p not found\n",
2677 __func__, ppi->iface_entry);
2678 ppi->iface_entry = NULL;
2679 }
2680 return NULL;
2681 }
2682
2683 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2684
2685 if (!ppi->tag_pos) {
2686 /* seq_read skipped first next call */
2687 ts_entry = SEQ_START_TOKEN;
2688 } else {
2689 ts_entry = tag_stat_tree_search(
2690 &ppi->iface_entry->tag_stat_tree, ppi->tag);
2691 if (!ts_entry) {
2692 pr_info("qtaguid: %s(): tag_stat.tag 0x%llx not found. Abort.\n",
2693 __func__, ppi->tag);
2694 return NULL;
2695 }
2696 }
2697
2698 if (*pos == ppi->tag_pos) { /* normal resume */
2699 ppi->item_index = ppi->tag_item_index;
2700 } else {
2701 /* seq_read skipped a next call */
2702 *pos = ppi->tag_pos;
2703 ts_entry = qtaguid_stats_proc_next(m, ts_entry, pos);
2704 }
2705
2706 return ts_entry;
2707}
2708
2709static void qtaguid_stats_proc_stop(struct seq_file *m, void *v)
2710{
2711 struct proc_print_info *ppi = m->private;
2712 if (ppi->iface_entry)
2713 spin_unlock_bh(&ppi->iface_entry->tag_stat_list_lock);
2714 spin_unlock_bh(&iface_stat_list_lock);
2715}
2716
JP Abgrallbaf0db42011-06-20 12:41:46 -07002717/*
2718 * Procfs reader to get all tag stats using style "1)" as described in
2719 * fs/proc/generic.c
2720 * Groups all protocols tx/rx bytes.
2721 */
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002722static int qtaguid_stats_proc_show(struct seq_file *m, void *v)
JP Abgrallbaf0db42011-06-20 12:41:46 -07002723{
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002724 struct tag_stat *ts_entry = v;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002725
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002726 if (v == SEQ_START_TOKEN)
2727 pp_stats_header(m);
2728 else
2729 pp_sets(m, ts_entry);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002730
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002731 return 0;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002732}
2733
2734/*------------------------------------------*/
2735static int qtudev_open(struct inode *inode, struct file *file)
2736{
2737 struct uid_tag_data *utd_entry;
2738 struct proc_qtu_data *pqd_entry;
2739 struct proc_qtu_data *new_pqd_entry;
2740 int res;
2741 bool utd_entry_found;
2742
2743 if (unlikely(qtu_proc_handling_passive))
2744 return 0;
2745
2746 DR_DEBUG("qtaguid: qtudev_open(): pid=%u tgid=%u uid=%u\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002747 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002748
2749 spin_lock_bh(&uid_tag_data_tree_lock);
2750
2751 /* Look for existing uid data, or alloc one. */
John Stultzbd1bca42014-03-28 16:23:48 -07002752 utd_entry = get_uid_data(from_kuid(&init_user_ns, current_fsuid()), &utd_entry_found);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002753 if (IS_ERR_OR_NULL(utd_entry)) {
2754 res = PTR_ERR(utd_entry);
JP Abgrallb79c36f12012-10-09 20:38:21 -07002755 goto err_unlock;
JP Abgrallbaf0db42011-06-20 12:41:46 -07002756 }
2757
2758 /* Look for existing PID based proc_data */
2759 pqd_entry = proc_qtu_data_tree_search(&proc_qtu_data_tree,
2760 current->tgid);
2761 if (pqd_entry) {
2762 pr_err("qtaguid: qtudev_open(): %u/%u %u "
2763 "%s already opened\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002764 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()),
JP Abgrallbaf0db42011-06-20 12:41:46 -07002765 QTU_DEV_NAME);
2766 res = -EBUSY;
2767 goto err_unlock_free_utd;
2768 }
2769
2770 new_pqd_entry = kzalloc(sizeof(*new_pqd_entry), GFP_ATOMIC);
2771 if (!new_pqd_entry) {
2772 pr_err("qtaguid: qtudev_open(): %u/%u %u: "
2773 "proc data alloc failed\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002774 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
JP Abgrallbaf0db42011-06-20 12:41:46 -07002775 res = -ENOMEM;
2776 goto err_unlock_free_utd;
2777 }
2778 new_pqd_entry->pid = current->tgid;
2779 INIT_LIST_HEAD(&new_pqd_entry->sock_tag_list);
2780 new_pqd_entry->parent_tag_data = utd_entry;
2781 utd_entry->num_pqd++;
2782
2783 proc_qtu_data_tree_insert(new_pqd_entry,
2784 &proc_qtu_data_tree);
2785
2786 spin_unlock_bh(&uid_tag_data_tree_lock);
2787 DR_DEBUG("qtaguid: tracking data for uid=%u in pqd=%p\n",
John Stultzbd1bca42014-03-28 16:23:48 -07002788 from_kuid(&init_user_ns, current_fsuid()), new_pqd_entry);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002789 file->private_data = new_pqd_entry;
2790 return 0;
2791
2792err_unlock_free_utd:
2793 if (!utd_entry_found) {
2794 rb_erase(&utd_entry->node, &uid_tag_data_tree);
2795 kfree(utd_entry);
2796 }
JP Abgrallb79c36f12012-10-09 20:38:21 -07002797err_unlock:
JP Abgrallbaf0db42011-06-20 12:41:46 -07002798 spin_unlock_bh(&uid_tag_data_tree_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002799 return res;
2800}
2801
2802static int qtudev_release(struct inode *inode, struct file *file)
2803{
2804 struct proc_qtu_data *pqd_entry = file->private_data;
2805 struct uid_tag_data *utd_entry = pqd_entry->parent_tag_data;
2806 struct sock_tag *st_entry;
2807 struct rb_root st_to_free_tree = RB_ROOT;
2808 struct list_head *entry, *next;
2809 struct tag_ref *tr;
2810
2811 if (unlikely(qtu_proc_handling_passive))
2812 return 0;
2813
2814 /*
2815 * Do not trust the current->pid, it might just be a kworker cleaning
2816 * up after a dead proc.
2817 */
2818 DR_DEBUG("qtaguid: qtudev_release(): "
2819 "pid=%u tgid=%u uid=%u "
2820 "pqd_entry=%p->pid=%u utd_entry=%p->active_tags=%d\n",
2821 current->pid, current->tgid, pqd_entry->parent_tag_data->uid,
2822 pqd_entry, pqd_entry->pid, utd_entry,
2823 utd_entry->num_active_tags);
2824
2825 spin_lock_bh(&sock_tag_list_lock);
2826 spin_lock_bh(&uid_tag_data_tree_lock);
2827
2828 list_for_each_safe(entry, next, &pqd_entry->sock_tag_list) {
2829 st_entry = list_entry(entry, struct sock_tag, list);
2830 DR_DEBUG("qtaguid: %s(): "
2831 "erase sock_tag=%p->sk=%p pid=%u tgid=%u uid=%u\n",
2832 __func__,
2833 st_entry, st_entry->sk,
2834 current->pid, current->tgid,
2835 pqd_entry->parent_tag_data->uid);
2836
2837 utd_entry = uid_tag_data_tree_search(
2838 &uid_tag_data_tree,
2839 get_uid_from_tag(st_entry->tag));
2840 BUG_ON(IS_ERR_OR_NULL(utd_entry));
2841 DR_DEBUG("qtaguid: %s(): "
2842 "looking for tag=0x%llx in utd_entry=%p\n", __func__,
2843 st_entry->tag, utd_entry);
2844 tr = tag_ref_tree_search(&utd_entry->tag_ref_tree,
2845 st_entry->tag);
2846 BUG_ON(!tr);
2847 BUG_ON(tr->num_sock_tags <= 0);
2848 tr->num_sock_tags--;
2849 free_tag_ref_from_utd_entry(tr, utd_entry);
2850
2851 rb_erase(&st_entry->sock_node, &sock_tag_tree);
2852 list_del(&st_entry->list);
2853 /* Can't sockfd_put() within spinlock, do it later. */
2854 sock_tag_tree_insert(st_entry, &st_to_free_tree);
2855
2856 /*
2857 * Try to free the utd_entry if no other proc_qtu_data is
2858 * using it (num_pqd is 0) and it doesn't have active tags
2859 * (num_active_tags is 0).
2860 */
2861 put_utd_entry(utd_entry);
2862 }
2863
2864 rb_erase(&pqd_entry->node, &proc_qtu_data_tree);
2865 BUG_ON(pqd_entry->parent_tag_data->num_pqd < 1);
2866 pqd_entry->parent_tag_data->num_pqd--;
2867 put_utd_entry(pqd_entry->parent_tag_data);
2868 kfree(pqd_entry);
2869 file->private_data = NULL;
2870
2871 spin_unlock_bh(&uid_tag_data_tree_lock);
2872 spin_unlock_bh(&sock_tag_list_lock);
2873
2874
2875 sock_tag_tree_erase(&st_to_free_tree);
2876
Chenbo Feng64157f42017-03-23 13:51:24 -07002877 spin_lock_bh(&sock_tag_list_lock);
2878 prdebug_full_state_locked(0, "%s(): pid=%u tgid=%u", __func__,
JP Abgrallbaf0db42011-06-20 12:41:46 -07002879 current->pid, current->tgid);
Chenbo Feng64157f42017-03-23 13:51:24 -07002880 spin_unlock_bh(&sock_tag_list_lock);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002881 return 0;
2882}
2883
2884/*------------------------------------------*/
2885static const struct file_operations qtudev_fops = {
2886 .owner = THIS_MODULE,
2887 .open = qtudev_open,
2888 .release = qtudev_release,
2889};
2890
2891static struct miscdevice qtu_device = {
2892 .minor = MISC_DYNAMIC_MINOR,
2893 .name = QTU_DEV_NAME,
2894 .fops = &qtudev_fops,
2895 /* How sad it doesn't allow for defaults: .mode = S_IRUGO | S_IWUSR */
2896};
2897
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002898static const struct seq_operations proc_qtaguid_ctrl_seqops = {
2899 .start = qtaguid_ctrl_proc_start,
2900 .next = qtaguid_ctrl_proc_next,
2901 .stop = qtaguid_ctrl_proc_stop,
2902 .show = qtaguid_ctrl_proc_show,
2903};
2904
2905static int proc_qtaguid_ctrl_open(struct inode *inode, struct file *file)
2906{
2907 return seq_open_private(file, &proc_qtaguid_ctrl_seqops,
2908 sizeof(struct proc_ctrl_print_info));
2909}
2910
2911static const struct file_operations proc_qtaguid_ctrl_fops = {
2912 .open = proc_qtaguid_ctrl_open,
2913 .read = seq_read,
2914 .write = qtaguid_ctrl_proc_write,
2915 .llseek = seq_lseek,
Greg Hackmann56472912013-12-04 17:39:27 -08002916 .release = seq_release_private,
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002917};
2918
2919static const struct seq_operations proc_qtaguid_stats_seqops = {
2920 .start = qtaguid_stats_proc_start,
2921 .next = qtaguid_stats_proc_next,
2922 .stop = qtaguid_stats_proc_stop,
2923 .show = qtaguid_stats_proc_show,
2924};
2925
2926static int proc_qtaguid_stats_open(struct inode *inode, struct file *file)
2927{
2928 return seq_open_private(file, &proc_qtaguid_stats_seqops,
2929 sizeof(struct proc_print_info));
2930}
2931
2932static const struct file_operations proc_qtaguid_stats_fops = {
2933 .open = proc_qtaguid_stats_open,
2934 .read = seq_read,
2935 .llseek = seq_lseek,
2936 .release = seq_release_private,
2937};
2938
JP Abgrallbaf0db42011-06-20 12:41:46 -07002939/*------------------------------------------*/
2940static int __init qtaguid_proc_register(struct proc_dir_entry **res_procdir)
2941{
2942 int ret;
2943 *res_procdir = proc_mkdir(module_procdirname, init_net.proc_net);
2944 if (!*res_procdir) {
2945 pr_err("qtaguid: failed to create proc/.../xt_qtaguid\n");
2946 ret = -ENOMEM;
2947 goto no_dir;
2948 }
2949
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002950 xt_qtaguid_ctrl_file = proc_create_data("ctrl", proc_ctrl_perms,
2951 *res_procdir,
2952 &proc_qtaguid_ctrl_fops,
2953 NULL);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002954 if (!xt_qtaguid_ctrl_file) {
2955 pr_err("qtaguid: failed to create xt_qtaguid/ctrl "
2956 " file\n");
2957 ret = -ENOMEM;
2958 goto no_ctrl_entry;
2959 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07002960
Arve Hjønnevåg287076e2013-05-13 20:45:02 -07002961 xt_qtaguid_stats_file = proc_create_data("stats", proc_stats_perms,
2962 *res_procdir,
2963 &proc_qtaguid_stats_fops,
2964 NULL);
JP Abgrallbaf0db42011-06-20 12:41:46 -07002965 if (!xt_qtaguid_stats_file) {
2966 pr_err("qtaguid: failed to create xt_qtaguid/stats "
2967 "file\n");
2968 ret = -ENOMEM;
2969 goto no_stats_entry;
2970 }
JP Abgrallbaf0db42011-06-20 12:41:46 -07002971 /*
2972 * TODO: add support counter hacking
2973 * xt_qtaguid_stats_file->write_proc = qtaguid_stats_proc_write;
2974 */
2975 return 0;
2976
2977no_stats_entry:
2978 remove_proc_entry("ctrl", *res_procdir);
2979no_ctrl_entry:
2980 remove_proc_entry("xt_qtaguid", NULL);
2981no_dir:
2982 return ret;
2983}
2984
2985static struct xt_match qtaguid_mt_reg __read_mostly = {
2986 /*
2987 * This module masquerades as the "owner" module so that iptables
2988 * tools can deal with it.
2989 */
2990 .name = "owner",
2991 .revision = 1,
2992 .family = NFPROTO_UNSPEC,
2993 .match = qtaguid_mt,
2994 .matchsize = sizeof(struct xt_qtaguid_match_info),
2995 .me = THIS_MODULE,
2996};
2997
2998static int __init qtaguid_mt_init(void)
2999{
3000 if (qtaguid_proc_register(&xt_qtaguid_procdir)
3001 || iface_stat_init(xt_qtaguid_procdir)
3002 || xt_register_match(&qtaguid_mt_reg)
3003 || misc_register(&qtu_device))
3004 return -1;
3005 return 0;
3006}
3007
3008/*
3009 * TODO: allow unloading of the module.
3010 * For now stats are permanent.
3011 * Kconfig forces'y/n' and never an 'm'.
3012 */
3013
3014module_init(qtaguid_mt_init);
3015MODULE_AUTHOR("jpa <jpa@google.com>");
3016MODULE_DESCRIPTION("Xtables: socket owner+tag matching and associated stats");
3017MODULE_LICENSE("GPL");
3018MODULE_ALIAS("ipt_owner");
3019MODULE_ALIAS("ip6t_owner");
3020MODULE_ALIAS("ipt_qtaguid");
3021MODULE_ALIAS("ip6t_qtaguid");