2 * Kernel iptables module to track stats for packets based on user tags.
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.
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.
17 #include <linux/file.h>
18 #include <linux/inetdevice.h>
19 #include <linux/module.h>
20 #include <linux/miscdevice.h>
21 #include <linux/netfilter/x_tables.h>
22 #include <linux/netfilter/xt_qtaguid.h>
23 #include <linux/ratelimit.h>
24 #include <linux/seq_file.h>
25 #include <linux/skbuff.h>
26 #include <linux/workqueue.h>
27 #include <net/addrconf.h>
32 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
33 #include <linux/netfilter_ipv6/ip6_tables.h>
36 #include <linux/netfilter/xt_socket.h>
37 #include "xt_qtaguid_internal.h"
38 #include "xt_qtaguid_print.h"
39 #include "../../fs/proc/internal.h"
42 * We only use the xt_socket funcs within a similar context to avoid unexpected
45 #define XT_SOCKET_SUPPORTED_HOOKS \
46 ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_IN))
49 static const char *module_procdirname = "xt_qtaguid";
50 static struct proc_dir_entry *xt_qtaguid_procdir;
52 static unsigned int proc_iface_perms = S_IRUGO;
53 module_param_named(iface_perms, proc_iface_perms, uint, S_IRUGO | S_IWUSR);
55 static struct proc_dir_entry *xt_qtaguid_stats_file;
56 static unsigned int proc_stats_perms = S_IRUGO;
57 module_param_named(stats_perms, proc_stats_perms, uint, S_IRUGO | S_IWUSR);
59 static struct proc_dir_entry *xt_qtaguid_ctrl_file;
61 /* Everybody can write. But proc_ctrl_write_limited is true by default which
62 * limits what can be controlled. See the can_*() functions.
64 static unsigned int proc_ctrl_perms = S_IRUGO | S_IWUGO;
65 module_param_named(ctrl_perms, proc_ctrl_perms, uint, S_IRUGO | S_IWUSR);
67 /* Limited by default, so the gid of the ctrl and stats proc entries
68 * will limit what can be done. See the can_*() functions.
70 static bool proc_stats_readall_limited = true;
71 static bool proc_ctrl_write_limited = true;
73 module_param_named(stats_readall_limited, proc_stats_readall_limited, bool,
75 module_param_named(ctrl_write_limited, proc_ctrl_write_limited, bool,
79 * Limit the number of active tags (via socket tags) for a given UID.
80 * Multiple processes could share the UID.
82 static int max_sock_tags = DEFAULT_MAX_SOCK_TAGS;
83 module_param(max_sock_tags, int, S_IRUGO | S_IWUSR);
86 * After the kernel has initiallized this module, it is still possible
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.
95 static bool module_passive;
96 module_param_named(passive, module_passive, bool, S_IRUGO | S_IWUSR);
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.
106 static bool qtu_proc_handling_passive;
107 module_param_named(tag_tracking_passive, qtu_proc_handling_passive, bool,
110 #define QTU_DEV_NAME "xt_qtaguid"
112 uint qtaguid_debug_mask = DEFAULT_DEBUG_MASK;
113 module_param_named(debug_mask, qtaguid_debug_mask, uint, S_IRUGO | S_IWUSR);
115 /*---------------------------------------------------------------------------*/
116 static const char *iface_stat_procdirname = "iface_stat";
117 static struct proc_dir_entry *iface_stat_procdir;
119 * The iface_stat_all* will go away once userspace gets use to the new fields
120 * that have a format line.
122 static const char *iface_stat_all_procfilename = "iface_stat_all";
123 static struct proc_dir_entry *iface_stat_all_procfile;
124 static const char *iface_stat_fmt_procfilename = "iface_stat_fmt";
125 static struct proc_dir_entry *iface_stat_fmt_procfile;
128 static LIST_HEAD(iface_stat_list);
129 static DEFINE_SPINLOCK(iface_stat_list_lock);
131 static struct rb_root sock_tag_tree = RB_ROOT;
132 static DEFINE_SPINLOCK(sock_tag_list_lock);
134 static struct rb_root tag_counter_set_tree = RB_ROOT;
135 static DEFINE_SPINLOCK(tag_counter_set_list_lock);
137 static struct rb_root uid_tag_data_tree = RB_ROOT;
138 static DEFINE_SPINLOCK(uid_tag_data_tree_lock);
140 static struct rb_root proc_qtu_data_tree = RB_ROOT;
141 /* No proc_qtu_data_tree_lock; use uid_tag_data_tree_lock */
143 static struct qtaguid_event_counts qtu_events;
144 /*----------------------------------------------*/
145 static bool can_manipulate_uids(void)
148 return in_egroup_p(xt_qtaguid_ctrl_file->gid)
149 || unlikely(!from_kuid(&init_user_ns, current_fsuid())) || unlikely(!proc_ctrl_write_limited)
150 || unlikely(uid_eq(current_fsuid(), xt_qtaguid_ctrl_file->uid));
153 static bool can_impersonate_uid(kuid_t uid)
155 return uid_eq(uid, current_fsuid()) || can_manipulate_uids();
158 static bool can_read_other_uid_stats(kuid_t uid)
161 return in_egroup_p(xt_qtaguid_stats_file->gid)
162 || unlikely(!from_kuid(&init_user_ns, current_fsuid())) || uid_eq(uid, current_fsuid())
163 || unlikely(!proc_stats_readall_limited)
164 || unlikely(uid_eq(current_fsuid(), xt_qtaguid_ctrl_file->uid));
167 static inline void dc_add_byte_packets(struct data_counters *counters, int set,
168 enum ifs_tx_rx direction,
169 enum ifs_proto ifs_proto,
173 counters->bpc[set][direction][ifs_proto].bytes += bytes;
174 counters->bpc[set][direction][ifs_proto].packets += packets;
177 static struct tag_node *tag_node_tree_search(struct rb_root *root, tag_t tag)
179 struct rb_node *node = root->rb_node;
182 struct tag_node *data = rb_entry(node, struct tag_node, node);
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);
191 node = node->rb_left;
193 node = node->rb_right;
200 static void tag_node_tree_insert(struct tag_node *data, struct rb_root *root)
202 struct rb_node **new = &(root->rb_node), *parent = NULL;
204 /* Figure out where to put new node */
206 struct tag_node *this = rb_entry(*new, struct tag_node,
208 int result = tag_compare(data->tag, this->tag);
209 RB_DEBUG("qtaguid: %s(): tag=0x%llx"
210 " (uid=%u)\n", __func__,
212 get_uid_from_tag(this->tag));
215 new = &((*new)->rb_left);
217 new = &((*new)->rb_right);
222 /* Add new node and rebalance tree. */
223 rb_link_node(&data->node, parent, new);
224 rb_insert_color(&data->node, root);
227 static void tag_stat_tree_insert(struct tag_stat *data, struct rb_root *root)
229 tag_node_tree_insert(&data->tn, root);
232 static struct tag_stat *tag_stat_tree_search(struct rb_root *root, tag_t tag)
234 struct tag_node *node = tag_node_tree_search(root, tag);
237 return rb_entry(&node->node, struct tag_stat, tn.node);
240 static void tag_counter_set_tree_insert(struct tag_counter_set *data,
241 struct rb_root *root)
243 tag_node_tree_insert(&data->tn, root);
246 static struct tag_counter_set *tag_counter_set_tree_search(struct rb_root *root,
249 struct tag_node *node = tag_node_tree_search(root, tag);
252 return rb_entry(&node->node, struct tag_counter_set, tn.node);
256 static void tag_ref_tree_insert(struct tag_ref *data, struct rb_root *root)
258 tag_node_tree_insert(&data->tn, root);
261 static struct tag_ref *tag_ref_tree_search(struct rb_root *root, tag_t tag)
263 struct tag_node *node = tag_node_tree_search(root, tag);
266 return rb_entry(&node->node, struct tag_ref, tn.node);
269 static struct sock_tag *sock_tag_tree_search(struct rb_root *root,
270 const struct sock *sk)
272 struct rb_node *node = root->rb_node;
275 struct sock_tag *data = rb_entry(node, struct sock_tag,
278 node = node->rb_left;
279 else if (sk > data->sk)
280 node = node->rb_right;
287 static void sock_tag_tree_insert(struct sock_tag *data, struct rb_root *root)
289 struct rb_node **new = &(root->rb_node), *parent = NULL;
291 /* Figure out where to put new node */
293 struct sock_tag *this = rb_entry(*new, struct sock_tag,
296 if (data->sk < this->sk)
297 new = &((*new)->rb_left);
298 else if (data->sk > this->sk)
299 new = &((*new)->rb_right);
304 /* Add new node and rebalance tree. */
305 rb_link_node(&data->sock_node, parent, new);
306 rb_insert_color(&data->sock_node, root);
309 static void sock_tag_tree_erase(struct rb_root *st_to_free_tree)
311 struct rb_node *node;
312 struct sock_tag *st_entry;
314 node = rb_first(st_to_free_tree);
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__,
322 get_uid_from_tag(st_entry->tag));
323 rb_erase(&st_entry->sock_node, st_to_free_tree);
324 sockfd_put(st_entry->socket);
329 static struct proc_qtu_data *proc_qtu_data_tree_search(struct rb_root *root,
332 struct rb_node *node = root->rb_node;
335 struct proc_qtu_data *data = rb_entry(node,
336 struct proc_qtu_data,
339 node = node->rb_left;
340 else if (pid > data->pid)
341 node = node->rb_right;
348 static void proc_qtu_data_tree_insert(struct proc_qtu_data *data,
349 struct rb_root *root)
351 struct rb_node **new = &(root->rb_node), *parent = NULL;
353 /* Figure out where to put new node */
355 struct proc_qtu_data *this = rb_entry(*new,
356 struct proc_qtu_data,
359 if (data->pid < this->pid)
360 new = &((*new)->rb_left);
361 else if (data->pid > this->pid)
362 new = &((*new)->rb_right);
367 /* Add new node and rebalance tree. */
368 rb_link_node(&data->node, parent, new);
369 rb_insert_color(&data->node, root);
372 static void uid_tag_data_tree_insert(struct uid_tag_data *data,
373 struct rb_root *root)
375 struct rb_node **new = &(root->rb_node), *parent = NULL;
377 /* Figure out where to put new node */
379 struct uid_tag_data *this = rb_entry(*new,
383 if (data->uid < this->uid)
384 new = &((*new)->rb_left);
385 else if (data->uid > this->uid)
386 new = &((*new)->rb_right);
391 /* Add new node and rebalance tree. */
392 rb_link_node(&data->node, parent, new);
393 rb_insert_color(&data->node, root);
396 static struct uid_tag_data *uid_tag_data_tree_search(struct rb_root *root,
399 struct rb_node *node = root->rb_node;
402 struct uid_tag_data *data = rb_entry(node,
406 node = node->rb_left;
407 else if (uid > data->uid)
408 node = node->rb_right;
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.
423 struct uid_tag_data *get_uid_data(uid_t uid, bool *found_res)
425 struct uid_tag_data *utd_entry;
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);
432 *found_res = utd_entry;
436 utd_entry = kzalloc(sizeof(*utd_entry), GFP_ATOMIC);
438 pr_err("qtaguid: get_uid_data(%u): "
439 "tag data alloc failed\n", uid);
440 return ERR_PTR(-ENOMEM);
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);
450 /* Never returns NULL. Either PTR_ERR or a valid ptr. */
451 static struct tag_ref *new_tag_ref(tag_t new_tag,
452 struct uid_tag_data *utd_entry)
454 struct tag_ref *tr_entry;
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);
466 tr_entry = kzalloc(sizeof(*tr_entry), GFP_ATOMIC);
468 pr_err("qtaguid: new_tag_ref(0x%llx): "
469 "tag ref alloc failed\n",
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",
487 static struct tag_ref *lookup_tag_ref(tag_t full_tag,
488 struct uid_tag_data **utd_res)
490 struct uid_tag_data *utd_entry;
491 struct tag_ref *tr_entry;
493 uid_t uid = get_uid_from_tag(full_tag);
495 DR_DEBUG("qtaguid: lookup_tag_ref(tag=0x%llx (uid=%u))\n",
498 utd_entry = get_uid_data(uid, &found_utd);
499 if (IS_ERR_OR_NULL(utd_entry)) {
501 *utd_res = utd_entry;
505 tr_entry = tag_ref_tree_search(&utd_entry->tag_ref_tree, full_tag);
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);
513 /* Never returns NULL. Either PTR_ERR or a valid ptr. */
514 static struct tag_ref *get_tag_ref(tag_t full_tag,
515 struct uid_tag_data **utd_res)
517 struct uid_tag_data *utd_entry;
518 struct tag_ref *tr_entry;
520 DR_DEBUG("qtaguid: get_tag_ref(0x%llx)\n",
522 spin_lock_bh(&uid_tag_data_tree_lock);
523 tr_entry = lookup_tag_ref(full_tag, &utd_entry);
524 BUG_ON(IS_ERR_OR_NULL(utd_entry));
526 tr_entry = new_tag_ref(full_tag, utd_entry);
528 spin_unlock_bh(&uid_tag_data_tree_lock);
530 *utd_res = utd_entry;
531 DR_DEBUG("qtaguid: get_tag_ref(0x%llx) utd=%p tr=%p\n",
532 full_tag, utd_entry, tr_entry);
536 /* Checks and maybe frees the UID Tag Data entry */
537 static void put_utd_entry(struct uid_tag_data *utd_entry)
539 /* Are we done with the UID tag data entry? */
540 if (RB_EMPTY_ROOT(&utd_entry->tag_ref_tree) &&
541 !utd_entry->num_pqd) {
542 DR_DEBUG("qtaguid: %s(): "
543 "erase utd_entry=%p uid=%u "
544 "by pid=%u tgid=%u uid=%u\n", __func__,
545 utd_entry, utd_entry->uid,
546 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
547 BUG_ON(utd_entry->num_active_tags);
548 rb_erase(&utd_entry->node, &uid_tag_data_tree);
551 DR_DEBUG("qtaguid: %s(): "
552 "utd_entry=%p still has %d tags %d proc_qtu_data\n",
553 __func__, utd_entry, utd_entry->num_active_tags,
555 BUG_ON(!(utd_entry->num_active_tags ||
556 utd_entry->num_pqd));
561 * If no sock_tags are using this tag_ref,
562 * decrements refcount of utd_entry, removes tr_entry
563 * from utd_entry->tag_ref_tree and frees.
565 static void free_tag_ref_from_utd_entry(struct tag_ref *tr_entry,
566 struct uid_tag_data *utd_entry)
568 DR_DEBUG("qtaguid: %s(): %p tag=0x%llx (uid=%u)\n", __func__,
569 tr_entry, tr_entry->tn.tag,
570 get_uid_from_tag(tr_entry->tn.tag));
571 if (!tr_entry->num_sock_tags) {
572 BUG_ON(!utd_entry->num_active_tags);
573 utd_entry->num_active_tags--;
574 rb_erase(&tr_entry->tn.node, &utd_entry->tag_ref_tree);
575 DR_DEBUG("qtaguid: %s(): erased %p\n", __func__, tr_entry);
580 static void put_tag_ref_tree(tag_t full_tag, struct uid_tag_data *utd_entry)
582 struct rb_node *node;
583 struct tag_ref *tr_entry;
586 DR_DEBUG("qtaguid: %s(tag=0x%llx (uid=%u))\n", __func__,
587 full_tag, get_uid_from_tag(full_tag));
588 acct_tag = get_atag_from_tag(full_tag);
589 node = rb_first(&utd_entry->tag_ref_tree);
591 tr_entry = rb_entry(node, struct tag_ref, tn.node);
592 node = rb_next(node);
593 if (!acct_tag || tr_entry->tn.tag == full_tag)
594 free_tag_ref_from_utd_entry(tr_entry, utd_entry);
598 static ssize_t read_proc_u64(struct file *file, char __user *buf,
599 size_t size, loff_t *ppos)
601 uint64_t *valuep = PDE_DATA(file_inode(file));
605 tmp_size = scnprintf(tmp, sizeof(tmp), "%llu\n", *valuep);
606 return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size);
609 static ssize_t read_proc_bool(struct file *file, char __user *buf,
610 size_t size, loff_t *ppos)
612 bool *valuep = PDE_DATA(file_inode(file));
616 tmp_size = scnprintf(tmp, sizeof(tmp), "%u\n", *valuep);
617 return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size);
620 static int get_active_counter_set(tag_t tag)
623 struct tag_counter_set *tcs;
625 MT_DEBUG("qtaguid: get_active_counter_set(tag=0x%llx)"
627 tag, get_uid_from_tag(tag));
628 /* For now we only handle UID tags for active sets */
629 tag = get_utag_from_tag(tag);
630 spin_lock_bh(&tag_counter_set_list_lock);
631 tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
633 active_set = tcs->active_set;
634 spin_unlock_bh(&tag_counter_set_list_lock);
639 * Find the entry for tracking the specified interface.
640 * Caller must hold iface_stat_list_lock
642 static struct iface_stat *get_iface_entry(const char *ifname)
644 struct iface_stat *iface_entry;
646 /* Find the entry for tracking the specified tag within the interface */
647 if (ifname == NULL) {
648 pr_info("qtaguid: iface_stat: get() NULL device name\n");
652 /* Iterate over interfaces */
653 list_for_each_entry(iface_entry, &iface_stat_list, list) {
654 if (!strcmp(ifname, iface_entry->ifname))
662 /* This is for fmt2 only */
663 static void pp_iface_stat_header(struct seq_file *m)
667 "total_skb_rx_bytes total_skb_rx_packets "
668 "total_skb_tx_bytes total_skb_tx_packets "
669 "rx_tcp_bytes rx_tcp_packets "
670 "rx_udp_bytes rx_udp_packets "
671 "rx_other_bytes rx_other_packets "
672 "tx_tcp_bytes tx_tcp_packets "
673 "tx_udp_bytes tx_udp_packets "
674 "tx_other_bytes tx_other_packets\n"
678 static void pp_iface_stat_line(struct seq_file *m,
679 struct iface_stat *iface_entry)
681 struct data_counters *cnts;
682 int cnt_set = 0; /* We only use one set for the device */
683 cnts = &iface_entry->totals_via_skb;
684 seq_printf(m, "%s %llu %llu %llu %llu %llu %llu %llu %llu "
685 "%llu %llu %llu %llu %llu %llu %llu %llu\n",
687 dc_sum_bytes(cnts, cnt_set, IFS_RX),
688 dc_sum_packets(cnts, cnt_set, IFS_RX),
689 dc_sum_bytes(cnts, cnt_set, IFS_TX),
690 dc_sum_packets(cnts, cnt_set, IFS_TX),
691 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes,
692 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets,
693 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes,
694 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets,
695 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes,
696 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets,
697 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes,
698 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets,
699 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes,
700 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets,
701 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes,
702 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets);
705 struct proc_iface_stat_fmt_info {
709 static void *iface_stat_fmt_proc_start(struct seq_file *m, loff_t *pos)
711 struct proc_iface_stat_fmt_info *p = m->private;
715 * This lock will prevent iface_stat_update() from changing active,
716 * and in turn prevent an interface from unregistering itself.
718 spin_lock_bh(&iface_stat_list_lock);
720 if (unlikely(module_passive))
723 if (!n && p->fmt == 2)
724 pp_iface_stat_header(m);
726 return seq_list_start(&iface_stat_list, n);
729 static void *iface_stat_fmt_proc_next(struct seq_file *m, void *p, loff_t *pos)
731 return seq_list_next(p, &iface_stat_list, pos);
734 static void iface_stat_fmt_proc_stop(struct seq_file *m, void *p)
736 spin_unlock_bh(&iface_stat_list_lock);
739 static int iface_stat_fmt_proc_show(struct seq_file *m, void *v)
741 struct proc_iface_stat_fmt_info *p = m->private;
742 struct iface_stat *iface_entry;
743 struct rtnl_link_stats64 dev_stats, *stats;
744 struct rtnl_link_stats64 no_dev_stats = {0};
747 CT_DEBUG("qtaguid:proc iface_stat_fmt pid=%u tgid=%u uid=%u\n",
748 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
750 iface_entry = list_entry(v, struct iface_stat, list);
752 if (iface_entry->active) {
753 stats = dev_get_stats(iface_entry->net_dev,
756 stats = &no_dev_stats;
759 * If the meaning of the data changes, then update the fmtX
763 seq_printf(m, "%s %d %llu %llu %llu %llu %llu %llu %llu %llu\n",
766 iface_entry->totals_via_dev[IFS_RX].bytes,
767 iface_entry->totals_via_dev[IFS_RX].packets,
768 iface_entry->totals_via_dev[IFS_TX].bytes,
769 iface_entry->totals_via_dev[IFS_TX].packets,
770 stats->rx_bytes, stats->rx_packets,
771 stats->tx_bytes, stats->tx_packets
774 pp_iface_stat_line(m, iface_entry);
779 static const struct file_operations read_u64_fops = {
780 .read = read_proc_u64,
781 .llseek = default_llseek,
784 static const struct file_operations read_bool_fops = {
785 .read = read_proc_bool,
786 .llseek = default_llseek,
789 static void iface_create_proc_worker(struct work_struct *work)
791 struct proc_dir_entry *proc_entry;
792 struct iface_stat_work *isw = container_of(work, struct iface_stat_work,
794 struct iface_stat *new_iface = isw->iface_entry;
796 /* iface_entries are not deleted, so safe to manipulate. */
797 proc_entry = proc_mkdir(new_iface->ifname, iface_stat_procdir);
798 if (IS_ERR_OR_NULL(proc_entry)) {
799 pr_err("qtaguid: iface_stat: create_proc(): alloc failed.\n");
804 new_iface->proc_ptr = proc_entry;
806 proc_create_data("tx_bytes", proc_iface_perms, proc_entry,
808 &new_iface->totals_via_dev[IFS_TX].bytes);
809 proc_create_data("rx_bytes", proc_iface_perms, proc_entry,
811 &new_iface->totals_via_dev[IFS_RX].bytes);
812 proc_create_data("tx_packets", proc_iface_perms, proc_entry,
814 &new_iface->totals_via_dev[IFS_TX].packets);
815 proc_create_data("rx_packets", proc_iface_perms, proc_entry,
817 &new_iface->totals_via_dev[IFS_RX].packets);
818 proc_create_data("active", proc_iface_perms, proc_entry,
819 &read_bool_fops, &new_iface->active);
821 IF_DEBUG("qtaguid: iface_stat: create_proc(): done "
822 "entry=%p dev=%s\n", new_iface, new_iface->ifname);
827 * Will set the entry's active state, and
828 * update the net_dev accordingly also.
830 static void _iface_stat_set_active(struct iface_stat *entry,
831 struct net_device *net_dev,
835 entry->net_dev = net_dev;
836 entry->active = true;
837 IF_DEBUG("qtaguid: %s(%s): "
838 "enable tracking. rfcnt=%d\n", __func__,
840 __this_cpu_read(*net_dev->pcpu_refcnt));
842 entry->active = false;
843 entry->net_dev = NULL;
844 IF_DEBUG("qtaguid: %s(%s): "
845 "disable tracking. rfcnt=%d\n", __func__,
847 __this_cpu_read(*net_dev->pcpu_refcnt));
852 /* Caller must hold iface_stat_list_lock */
853 static struct iface_stat *iface_alloc(struct net_device *net_dev)
855 struct iface_stat *new_iface;
856 struct iface_stat_work *isw;
858 new_iface = kzalloc(sizeof(*new_iface), GFP_ATOMIC);
859 if (new_iface == NULL) {
860 pr_err("qtaguid: iface_stat: create(%s): "
861 "iface_stat alloc failed\n", net_dev->name);
864 new_iface->ifname = kstrdup(net_dev->name, GFP_ATOMIC);
865 if (new_iface->ifname == NULL) {
866 pr_err("qtaguid: iface_stat: create(%s): "
867 "ifname alloc failed\n", net_dev->name);
871 spin_lock_init(&new_iface->tag_stat_list_lock);
872 new_iface->tag_stat_tree = RB_ROOT;
873 _iface_stat_set_active(new_iface, net_dev, true);
876 * ipv6 notifier chains are atomic :(
877 * No create_proc_read_entry() for you!
879 isw = kmalloc(sizeof(*isw), GFP_ATOMIC);
881 pr_err("qtaguid: iface_stat: create(%s): "
882 "work alloc failed\n", new_iface->ifname);
883 _iface_stat_set_active(new_iface, net_dev, false);
884 kfree(new_iface->ifname);
888 isw->iface_entry = new_iface;
889 INIT_WORK(&isw->iface_work, iface_create_proc_worker);
890 schedule_work(&isw->iface_work);
891 list_add(&new_iface->list, &iface_stat_list);
895 static void iface_check_stats_reset_and_adjust(struct net_device *net_dev,
896 struct iface_stat *iface)
898 struct rtnl_link_stats64 dev_stats, *stats;
901 stats = dev_get_stats(net_dev, &dev_stats);
902 /* No empty packets */
904 (stats->rx_bytes < iface->last_known[IFS_RX].bytes)
905 || (stats->tx_bytes < iface->last_known[IFS_TX].bytes);
907 IF_DEBUG("qtaguid: %s(%s): iface=%p netdev=%p "
908 "bytes rx/tx=%llu/%llu "
909 "active=%d last_known=%d "
910 "stats_rewound=%d\n", __func__,
911 net_dev ? net_dev->name : "?",
913 stats->rx_bytes, stats->tx_bytes,
914 iface->active, iface->last_known_valid, stats_rewound);
916 if (iface->active && iface->last_known_valid && stats_rewound) {
917 pr_warn_once("qtaguid: iface_stat: %s(%s): "
918 "iface reset its stats unexpectedly\n", __func__,
921 iface->totals_via_dev[IFS_TX].bytes +=
922 iface->last_known[IFS_TX].bytes;
923 iface->totals_via_dev[IFS_TX].packets +=
924 iface->last_known[IFS_TX].packets;
925 iface->totals_via_dev[IFS_RX].bytes +=
926 iface->last_known[IFS_RX].bytes;
927 iface->totals_via_dev[IFS_RX].packets +=
928 iface->last_known[IFS_RX].packets;
929 iface->last_known_valid = false;
930 IF_DEBUG("qtaguid: %s(%s): iface=%p "
931 "used last known bytes rx/tx=%llu/%llu\n", __func__,
932 iface->ifname, iface, iface->last_known[IFS_RX].bytes,
933 iface->last_known[IFS_TX].bytes);
938 * Create a new entry for tracking the specified interface.
939 * Do nothing if the entry already exists.
940 * Called when an interface is configured with a valid IP address.
942 static void iface_stat_create(struct net_device *net_dev,
943 struct in_ifaddr *ifa)
945 struct in_device *in_dev = NULL;
947 struct iface_stat *entry;
949 struct iface_stat *new_iface;
951 IF_DEBUG("qtaguid: iface_stat: create(%s): ifa=%p netdev=%p\n",
952 net_dev ? net_dev->name : "?",
955 pr_err("qtaguid: iface_stat: create(): no net dev\n");
959 ifname = net_dev->name;
961 in_dev = in_dev_get(net_dev);
963 pr_err("qtaguid: iface_stat: create(%s): no inet dev\n",
967 IF_DEBUG("qtaguid: iface_stat: create(%s): in_dev=%p\n",
969 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
970 IF_DEBUG("qtaguid: iface_stat: create(%s): "
971 "ifa=%p ifa_label=%s\n",
973 ifa->ifa_label ? ifa->ifa_label : "(null)");
974 if (ifa->ifa_label && !strcmp(ifname, ifa->ifa_label))
980 IF_DEBUG("qtaguid: iface_stat: create(%s): no matching IP\n",
984 ipaddr = ifa->ifa_local;
986 spin_lock_bh(&iface_stat_list_lock);
987 entry = get_iface_entry(ifname);
989 IF_DEBUG("qtaguid: iface_stat: create(%s): entry=%p\n",
991 iface_check_stats_reset_and_adjust(net_dev, entry);
992 _iface_stat_set_active(entry, net_dev, true);
993 IF_DEBUG("qtaguid: %s(%s): "
994 "tracking now %d on ip=%pI4\n", __func__,
995 entry->ifname, true, &ipaddr);
996 goto done_unlock_put;
999 new_iface = iface_alloc(net_dev);
1000 IF_DEBUG("qtaguid: iface_stat: create(%s): done "
1001 "entry=%p ip=%pI4\n", ifname, new_iface, &ipaddr);
1003 spin_unlock_bh(&iface_stat_list_lock);
1009 static void iface_stat_create_ipv6(struct net_device *net_dev,
1010 struct inet6_ifaddr *ifa)
1012 struct in_device *in_dev;
1014 struct iface_stat *entry;
1015 struct iface_stat *new_iface;
1018 IF_DEBUG("qtaguid: iface_stat: create6(): ifa=%p netdev=%p->name=%s\n",
1019 ifa, net_dev, net_dev ? net_dev->name : "");
1021 pr_err("qtaguid: iface_stat: create6(): no net dev!\n");
1024 ifname = net_dev->name;
1026 in_dev = in_dev_get(net_dev);
1028 pr_err("qtaguid: iface_stat: create6(%s): no inet dev\n",
1033 IF_DEBUG("qtaguid: iface_stat: create6(%s): in_dev=%p\n",
1037 IF_DEBUG("qtaguid: iface_stat: create6(%s): no matching IP\n",
1041 addr_type = ipv6_addr_type(&ifa->addr);
1043 spin_lock_bh(&iface_stat_list_lock);
1044 entry = get_iface_entry(ifname);
1045 if (entry != NULL) {
1046 IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
1048 iface_check_stats_reset_and_adjust(net_dev, entry);
1049 _iface_stat_set_active(entry, net_dev, true);
1050 IF_DEBUG("qtaguid: %s(%s): "
1051 "tracking now %d on ip=%pI6c\n", __func__,
1052 entry->ifname, true, &ifa->addr);
1053 goto done_unlock_put;
1056 new_iface = iface_alloc(net_dev);
1057 IF_DEBUG("qtaguid: iface_stat: create6(%s): done "
1058 "entry=%p ip=%pI6c\n", ifname, new_iface, &ifa->addr);
1061 spin_unlock_bh(&iface_stat_list_lock);
1066 static struct sock_tag *get_sock_stat_nl(const struct sock *sk)
1068 MT_DEBUG("qtaguid: get_sock_stat_nl(sk=%p)\n", sk);
1069 return sock_tag_tree_search(&sock_tag_tree, sk);
1072 static struct sock_tag *get_sock_stat(const struct sock *sk)
1074 struct sock_tag *sock_tag_entry;
1075 MT_DEBUG("qtaguid: get_sock_stat(sk=%p)\n", sk);
1078 spin_lock_bh(&sock_tag_list_lock);
1079 sock_tag_entry = get_sock_stat_nl(sk);
1080 spin_unlock_bh(&sock_tag_list_lock);
1081 return sock_tag_entry;
1084 static int ipx_proto(const struct sk_buff *skb,
1085 struct xt_action_param *par)
1087 int thoff = 0, tproto;
1089 switch (par->family) {
1091 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
1093 MT_DEBUG("%s(): transport header not found in ipv6"
1094 " skb=%p\n", __func__, skb);
1097 tproto = ip_hdr(skb)->protocol;
1100 tproto = IPPROTO_RAW;
1106 data_counters_update(struct data_counters *dc, int set,
1107 enum ifs_tx_rx direction, int proto, int bytes)
1111 dc_add_byte_packets(dc, set, direction, IFS_TCP, bytes, 1);
1114 dc_add_byte_packets(dc, set, direction, IFS_UDP, bytes, 1);
1118 dc_add_byte_packets(dc, set, direction, IFS_PROTO_OTHER, bytes,
1125 * Update stats for the specified interface. Do nothing if the entry
1126 * does not exist (when a device was never configured with an IP address).
1127 * Called when an device is being unregistered.
1129 static void iface_stat_update(struct net_device *net_dev, bool stash_only)
1131 struct rtnl_link_stats64 dev_stats, *stats;
1132 struct iface_stat *entry;
1134 stats = dev_get_stats(net_dev, &dev_stats);
1135 spin_lock_bh(&iface_stat_list_lock);
1136 entry = get_iface_entry(net_dev->name);
1137 if (entry == NULL) {
1138 IF_DEBUG("qtaguid: iface_stat: update(%s): not tracked\n",
1140 spin_unlock_bh(&iface_stat_list_lock);
1144 IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
1145 net_dev->name, entry);
1146 if (!entry->active) {
1147 IF_DEBUG("qtaguid: %s(%s): already disabled\n", __func__,
1149 spin_unlock_bh(&iface_stat_list_lock);
1154 entry->last_known[IFS_TX].bytes = stats->tx_bytes;
1155 entry->last_known[IFS_TX].packets = stats->tx_packets;
1156 entry->last_known[IFS_RX].bytes = stats->rx_bytes;
1157 entry->last_known[IFS_RX].packets = stats->rx_packets;
1158 entry->last_known_valid = true;
1159 IF_DEBUG("qtaguid: %s(%s): "
1160 "dev stats stashed rx/tx=%llu/%llu\n", __func__,
1161 net_dev->name, stats->rx_bytes, stats->tx_bytes);
1162 spin_unlock_bh(&iface_stat_list_lock);
1165 entry->totals_via_dev[IFS_TX].bytes += stats->tx_bytes;
1166 entry->totals_via_dev[IFS_TX].packets += stats->tx_packets;
1167 entry->totals_via_dev[IFS_RX].bytes += stats->rx_bytes;
1168 entry->totals_via_dev[IFS_RX].packets += stats->rx_packets;
1169 /* We don't need the last_known[] anymore */
1170 entry->last_known_valid = false;
1171 _iface_stat_set_active(entry, net_dev, false);
1172 IF_DEBUG("qtaguid: %s(%s): "
1173 "disable tracking. rx/tx=%llu/%llu\n", __func__,
1174 net_dev->name, stats->rx_bytes, stats->tx_bytes);
1175 spin_unlock_bh(&iface_stat_list_lock);
1179 * Update stats for the specified interface from the skb.
1180 * Do nothing if the entry
1181 * does not exist (when a device was never configured with an IP address).
1182 * Called on each sk.
1184 static void iface_stat_update_from_skb(const struct sk_buff *skb,
1185 struct xt_action_param *par)
1187 struct iface_stat *entry;
1188 const struct net_device *el_dev;
1189 enum ifs_tx_rx direction = par->in ? IFS_RX : IFS_TX;
1190 int bytes = skb->len;
1194 MT_DEBUG("qtaguid[%d]: no skb->dev\n", par->hooknum);
1195 el_dev = par->in ? : par->out;
1197 const struct net_device *other_dev;
1199 other_dev = par->in ? : par->out;
1200 if (el_dev != other_dev) {
1201 MT_DEBUG("qtaguid[%d]: skb->dev=%p %s vs "
1202 "par->(in/out)=%p %s\n",
1203 par->hooknum, el_dev, el_dev->name, other_dev,
1208 if (unlikely(!el_dev)) {
1209 pr_err_ratelimited("qtaguid[%d]: %s(): no par->in/out?!!\n",
1210 par->hooknum, __func__);
1212 } else if (unlikely(!el_dev->name)) {
1213 pr_err_ratelimited("qtaguid[%d]: %s(): no dev->name?!!\n",
1214 par->hooknum, __func__);
1217 proto = ipx_proto(skb, par);
1218 MT_DEBUG("qtaguid[%d]: dev name=%s type=%d fam=%d proto=%d\n",
1219 par->hooknum, el_dev->name, el_dev->type,
1220 par->family, proto);
1223 spin_lock_bh(&iface_stat_list_lock);
1224 entry = get_iface_entry(el_dev->name);
1225 if (entry == NULL) {
1226 IF_DEBUG("qtaguid: iface_stat: %s(%s): not tracked\n",
1227 __func__, el_dev->name);
1228 spin_unlock_bh(&iface_stat_list_lock);
1232 IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
1233 el_dev->name, entry);
1235 data_counters_update(&entry->totals_via_skb, 0, direction, proto,
1237 spin_unlock_bh(&iface_stat_list_lock);
1240 static void tag_stat_update(struct tag_stat *tag_entry,
1241 enum ifs_tx_rx direction, int proto, int bytes)
1244 active_set = get_active_counter_set(tag_entry->tn.tag);
1245 MT_DEBUG("qtaguid: tag_stat_update(tag=0x%llx (uid=%u) set=%d "
1246 "dir=%d proto=%d bytes=%d)\n",
1247 tag_entry->tn.tag, get_uid_from_tag(tag_entry->tn.tag),
1248 active_set, direction, proto, bytes);
1249 data_counters_update(&tag_entry->counters, active_set, direction,
1251 if (tag_entry->parent_counters)
1252 data_counters_update(tag_entry->parent_counters, active_set,
1253 direction, proto, bytes);
1257 * Create a new entry for tracking the specified {acct_tag,uid_tag} within
1259 * iface_entry->tag_stat_list_lock should be held.
1261 static struct tag_stat *create_if_tag_stat(struct iface_stat *iface_entry,
1264 struct tag_stat *new_tag_stat_entry = NULL;
1265 IF_DEBUG("qtaguid: iface_stat: %s(): ife=%p tag=0x%llx"
1266 " (uid=%u)\n", __func__,
1267 iface_entry, tag, get_uid_from_tag(tag));
1268 new_tag_stat_entry = kzalloc(sizeof(*new_tag_stat_entry), GFP_ATOMIC);
1269 if (!new_tag_stat_entry) {
1270 pr_err("qtaguid: iface_stat: tag stat alloc failed\n");
1273 new_tag_stat_entry->tn.tag = tag;
1274 tag_stat_tree_insert(new_tag_stat_entry, &iface_entry->tag_stat_tree);
1276 return new_tag_stat_entry;
1279 static void if_tag_stat_update(const char *ifname, uid_t uid,
1280 const struct sock *sk, enum ifs_tx_rx direction,
1281 int proto, int bytes)
1283 struct tag_stat *tag_stat_entry;
1284 tag_t tag, acct_tag;
1286 struct data_counters *uid_tag_counters;
1287 struct sock_tag *sock_tag_entry;
1288 struct iface_stat *iface_entry;
1289 struct tag_stat *new_tag_stat = NULL;
1290 MT_DEBUG("qtaguid: if_tag_stat_update(ifname=%s "
1291 "uid=%u sk=%p dir=%d proto=%d bytes=%d)\n",
1292 ifname, uid, sk, direction, proto, bytes);
1294 spin_lock_bh(&iface_stat_list_lock);
1295 iface_entry = get_iface_entry(ifname);
1297 pr_err_ratelimited("qtaguid: iface_stat: stat_update() "
1298 "%s not found\n", ifname);
1299 spin_unlock_bh(&iface_stat_list_lock);
1302 /* It is ok to process data when an iface_entry is inactive */
1304 MT_DEBUG("qtaguid: iface_stat: stat_update() dev=%s entry=%p\n",
1305 ifname, iface_entry);
1308 * Look for a tagged sock.
1309 * It will have an acct_uid.
1311 sock_tag_entry = get_sock_stat(sk);
1312 if (sock_tag_entry) {
1313 tag = sock_tag_entry->tag;
1314 acct_tag = get_atag_from_tag(tag);
1315 uid_tag = get_utag_from_tag(tag);
1317 acct_tag = make_atag_from_value(0);
1318 tag = combine_atag_with_uid(acct_tag, uid);
1319 uid_tag = make_tag_from_uid(uid);
1321 MT_DEBUG("qtaguid: iface_stat: stat_update(): "
1322 " looking for tag=0x%llx (uid=%u) in ife=%p\n",
1323 tag, get_uid_from_tag(tag), iface_entry);
1324 /* Loop over tag list under this interface for {acct_tag,uid_tag} */
1325 spin_lock_bh(&iface_entry->tag_stat_list_lock);
1327 tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree,
1329 if (tag_stat_entry) {
1331 * Updating the {acct_tag, uid_tag} entry handles both stats:
1332 * {0, uid_tag} will also get updated.
1334 tag_stat_update(tag_stat_entry, direction, proto, bytes);
1338 /* Loop over tag list under this interface for {0,uid_tag} */
1339 tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree,
1341 if (!tag_stat_entry) {
1342 /* Here: the base uid_tag did not exist */
1344 * No parent counters. So
1345 * - No {0, uid_tag} stats and no {acc_tag, uid_tag} stats.
1347 new_tag_stat = create_if_tag_stat(iface_entry, uid_tag);
1350 uid_tag_counters = &new_tag_stat->counters;
1352 uid_tag_counters = &tag_stat_entry->counters;
1356 /* Create the child {acct_tag, uid_tag} and hook up parent. */
1357 new_tag_stat = create_if_tag_stat(iface_entry, tag);
1360 new_tag_stat->parent_counters = uid_tag_counters;
1363 * For new_tag_stat to be still NULL here would require:
1364 * {0, uid_tag} exists
1365 * and {acct_tag, uid_tag} doesn't exist
1366 * AND acct_tag == 0.
1367 * Impossible. This reassures us that new_tag_stat
1368 * below will always be assigned.
1370 BUG_ON(!new_tag_stat);
1372 tag_stat_update(new_tag_stat, direction, proto, bytes);
1374 spin_unlock_bh(&iface_entry->tag_stat_list_lock);
1375 spin_unlock_bh(&iface_stat_list_lock);
1378 static int iface_netdev_event_handler(struct notifier_block *nb,
1379 unsigned long event, void *ptr) {
1380 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1382 if (unlikely(module_passive))
1385 IF_DEBUG("qtaguid: iface_stat: netdev_event(): "
1386 "ev=0x%lx/%s netdev=%p->name=%s\n",
1387 event, netdev_evt_str(event), dev, dev ? dev->name : "");
1391 iface_stat_create(dev, NULL);
1392 atomic64_inc(&qtu_events.iface_events);
1395 case NETDEV_UNREGISTER:
1396 iface_stat_update(dev, event == NETDEV_DOWN);
1397 atomic64_inc(&qtu_events.iface_events);
1403 static int iface_inet6addr_event_handler(struct notifier_block *nb,
1404 unsigned long event, void *ptr)
1406 struct inet6_ifaddr *ifa = ptr;
1407 struct net_device *dev;
1409 if (unlikely(module_passive))
1412 IF_DEBUG("qtaguid: iface_stat: inet6addr_event(): "
1413 "ev=0x%lx/%s ifa=%p\n",
1414 event, netdev_evt_str(event), ifa);
1418 BUG_ON(!ifa || !ifa->idev);
1419 dev = (struct net_device *)ifa->idev->dev;
1420 iface_stat_create_ipv6(dev, ifa);
1421 atomic64_inc(&qtu_events.iface_events);
1424 case NETDEV_UNREGISTER:
1425 BUG_ON(!ifa || !ifa->idev);
1426 dev = (struct net_device *)ifa->idev->dev;
1427 iface_stat_update(dev, event == NETDEV_DOWN);
1428 atomic64_inc(&qtu_events.iface_events);
1434 static int iface_inetaddr_event_handler(struct notifier_block *nb,
1435 unsigned long event, void *ptr)
1437 struct in_ifaddr *ifa = ptr;
1438 struct net_device *dev;
1440 if (unlikely(module_passive))
1443 IF_DEBUG("qtaguid: iface_stat: inetaddr_event(): "
1444 "ev=0x%lx/%s ifa=%p\n",
1445 event, netdev_evt_str(event), ifa);
1449 BUG_ON(!ifa || !ifa->ifa_dev);
1450 dev = ifa->ifa_dev->dev;
1451 iface_stat_create(dev, ifa);
1452 atomic64_inc(&qtu_events.iface_events);
1455 case NETDEV_UNREGISTER:
1456 BUG_ON(!ifa || !ifa->ifa_dev);
1457 dev = ifa->ifa_dev->dev;
1458 iface_stat_update(dev, event == NETDEV_DOWN);
1459 atomic64_inc(&qtu_events.iface_events);
1465 static struct notifier_block iface_netdev_notifier_blk = {
1466 .notifier_call = iface_netdev_event_handler,
1469 static struct notifier_block iface_inetaddr_notifier_blk = {
1470 .notifier_call = iface_inetaddr_event_handler,
1473 static struct notifier_block iface_inet6addr_notifier_blk = {
1474 .notifier_call = iface_inet6addr_event_handler,
1477 static const struct seq_operations iface_stat_fmt_proc_seq_ops = {
1478 .start = iface_stat_fmt_proc_start,
1479 .next = iface_stat_fmt_proc_next,
1480 .stop = iface_stat_fmt_proc_stop,
1481 .show = iface_stat_fmt_proc_show,
1484 static int proc_iface_stat_fmt_open(struct inode *inode, struct file *file)
1486 struct proc_iface_stat_fmt_info *s;
1488 s = __seq_open_private(file, &iface_stat_fmt_proc_seq_ops,
1489 sizeof(struct proc_iface_stat_fmt_info));
1493 s->fmt = (uintptr_t)PDE_DATA(inode);
1497 static const struct file_operations proc_iface_stat_fmt_fops = {
1498 .open = proc_iface_stat_fmt_open,
1500 .llseek = seq_lseek,
1501 .release = seq_release_private,
1504 static int __init iface_stat_init(struct proc_dir_entry *parent_procdir)
1508 iface_stat_procdir = proc_mkdir(iface_stat_procdirname, parent_procdir);
1509 if (!iface_stat_procdir) {
1510 pr_err("qtaguid: iface_stat: init failed to create proc entry\n");
1515 iface_stat_all_procfile = proc_create_data(iface_stat_all_procfilename,
1518 &proc_iface_stat_fmt_fops,
1519 (void *)1 /* fmt1 */);
1520 if (!iface_stat_all_procfile) {
1521 pr_err("qtaguid: iface_stat: init "
1522 " failed to create stat_old proc entry\n");
1527 iface_stat_fmt_procfile = proc_create_data(iface_stat_fmt_procfilename,
1530 &proc_iface_stat_fmt_fops,
1531 (void *)2 /* fmt2 */);
1532 if (!iface_stat_fmt_procfile) {
1533 pr_err("qtaguid: iface_stat: init "
1534 " failed to create stat_all proc entry\n");
1536 goto err_zap_all_stats_entry;
1540 err = register_netdevice_notifier(&iface_netdev_notifier_blk);
1542 pr_err("qtaguid: iface_stat: init "
1543 "failed to register dev event handler\n");
1544 goto err_zap_all_stats_entries;
1546 err = register_inetaddr_notifier(&iface_inetaddr_notifier_blk);
1548 pr_err("qtaguid: iface_stat: init "
1549 "failed to register ipv4 dev event handler\n");
1553 err = register_inet6addr_notifier(&iface_inet6addr_notifier_blk);
1555 pr_err("qtaguid: iface_stat: init "
1556 "failed to register ipv6 dev event handler\n");
1557 goto err_unreg_ip4_addr;
1562 unregister_inetaddr_notifier(&iface_inetaddr_notifier_blk);
1564 unregister_netdevice_notifier(&iface_netdev_notifier_blk);
1565 err_zap_all_stats_entries:
1566 remove_proc_entry(iface_stat_fmt_procfilename, parent_procdir);
1567 err_zap_all_stats_entry:
1568 remove_proc_entry(iface_stat_all_procfilename, parent_procdir);
1570 remove_proc_entry(iface_stat_procdirname, parent_procdir);
1575 static struct sock *qtaguid_find_sk(const struct sk_buff *skb,
1576 struct xt_action_param *par)
1579 unsigned int hook_mask = (1 << par->hooknum);
1581 MT_DEBUG("qtaguid: find_sk(skb=%p) hooknum=%d family=%d\n", skb,
1582 par->hooknum, par->family);
1585 * Let's not abuse the the xt_socket_get*_sk(), or else it will
1586 * return garbage SKs.
1588 if (!(hook_mask & XT_SOCKET_SUPPORTED_HOOKS))
1591 switch (par->family) {
1593 sk = xt_socket_lookup_slow_v6(dev_net(skb->dev), skb, par->in);
1596 sk = xt_socket_lookup_slow_v4(dev_net(skb->dev), skb, par->in);
1603 MT_DEBUG("qtaguid: %p->sk_proto=%u "
1604 "->sk_state=%d\n", sk, sk->sk_protocol, sk->sk_state);
1606 * When in TCP_TIME_WAIT the sk is not a "struct sock" but
1607 * "struct inet_timewait_sock" which is missing fields.
1609 if (!sk_fullsock(sk) || sk->sk_state == TCP_TIME_WAIT) {
1617 static void account_for_uid(const struct sk_buff *skb,
1618 const struct sock *alternate_sk, uid_t uid,
1619 struct xt_action_param *par)
1621 const struct net_device *el_dev;
1624 MT_DEBUG("qtaguid[%d]: no skb->dev\n", par->hooknum);
1625 el_dev = par->in ? : par->out;
1627 const struct net_device *other_dev;
1629 other_dev = par->in ? : par->out;
1630 if (el_dev != other_dev) {
1631 MT_DEBUG("qtaguid[%d]: skb->dev=%p %s vs "
1632 "par->(in/out)=%p %s\n",
1633 par->hooknum, el_dev, el_dev->name, other_dev,
1638 if (unlikely(!el_dev)) {
1639 pr_info("qtaguid[%d]: no par->in/out?!!\n", par->hooknum);
1640 } else if (unlikely(!el_dev->name)) {
1641 pr_info("qtaguid[%d]: no dev->name?!!\n", par->hooknum);
1643 int proto = ipx_proto(skb, par);
1644 MT_DEBUG("qtaguid[%d]: dev name=%s type=%d fam=%d proto=%d\n",
1645 par->hooknum, el_dev->name, el_dev->type,
1646 par->family, proto);
1648 if_tag_stat_update(el_dev->name, uid,
1649 skb->sk ? skb->sk : alternate_sk,
1650 par->in ? IFS_RX : IFS_TX,
1655 static bool qtaguid_mt(const struct sk_buff *skb, struct xt_action_param *par)
1657 const struct xt_qtaguid_match_info *info = par->matchinfo;
1658 const struct file *filp;
1659 bool got_sock = false;
1663 bool set_sk_callback_lock = false;
1665 if (unlikely(module_passive))
1666 return (info->match ^ info->invert) == 0;
1668 MT_DEBUG("qtaguid[%d]: entered skb=%p par->in=%p/out=%p fam=%d\n",
1669 par->hooknum, skb, par->in, par->out, par->family);
1671 atomic64_inc(&qtu_events.match_calls);
1673 res = (info->match ^ info->invert) == 0;
1677 switch (par->hooknum) {
1678 case NF_INET_PRE_ROUTING:
1679 case NF_INET_POST_ROUTING:
1680 atomic64_inc(&qtu_events.match_calls_prepost);
1681 iface_stat_update_from_skb(skb, par);
1683 * We are done in pre/post. The skb will get processed
1686 res = (info->match ^ info->invert);
1689 /* default: Fall through and do UID releated work */
1692 sk = skb_to_full_sk(skb);
1694 * When in TCP_TIME_WAIT the sk is not a "struct sock" but
1695 * "struct inet_timewait_sock" which is missing fields.
1698 if (sk && sk->sk_state == TCP_TIME_WAIT)
1702 * A missing sk->sk_socket happens when packets are in-flight
1703 * and the matching socket is already closed and gone.
1705 sk = qtaguid_find_sk(skb, par);
1707 * If we got the socket from the find_sk(), we will need to put
1708 * it back, as nf_tproxy_get_sock_v4() got it.
1712 atomic64_inc(&qtu_events.match_found_sk_in_ct);
1714 atomic64_inc(&qtu_events.match_found_no_sk_in_ct);
1716 atomic64_inc(&qtu_events.match_found_sk);
1718 MT_DEBUG("qtaguid[%d]: sk=%p got_sock=%d fam=%d proto=%d\n",
1719 par->hooknum, sk, got_sock, par->family, ipx_proto(skb, par));
1721 set_sk_callback_lock = true;
1722 read_lock_bh(&sk->sk_callback_lock);
1723 MT_DEBUG("qtaguid[%d]: sk=%p->sk_socket=%p->file=%p\n",
1724 par->hooknum, sk, sk->sk_socket,
1725 sk->sk_socket ? sk->sk_socket->file : (void *)-1LL);
1726 filp = sk->sk_socket ? sk->sk_socket->file : NULL;
1727 MT_DEBUG("qtaguid[%d]: filp...uid=%u\n",
1728 par->hooknum, filp ? from_kuid(&init_user_ns, filp->f_cred->fsuid) : -1);
1731 if (sk == NULL || sk->sk_socket == NULL) {
1733 * Here, the qtaguid_find_sk() using connection tracking
1734 * couldn't find the owner, so for now we just count them
1735 * against the system.
1738 * TODO: unhack how to force just accounting.
1739 * For now we only do iface stats when the uid-owner is not
1742 if (!(info->match & XT_QTAGUID_UID))
1743 account_for_uid(skb, sk, 0, par);
1744 MT_DEBUG("qtaguid[%d]: leaving (sk?sk->sk_socket)=%p\n",
1746 sk ? sk->sk_socket : NULL);
1747 res = (info->match ^ info->invert) == 0;
1748 atomic64_inc(&qtu_events.match_no_sk);
1749 goto put_sock_ret_res;
1750 } else if (info->match & info->invert & XT_QTAGUID_SOCKET) {
1752 goto put_sock_ret_res;
1754 filp = sk->sk_socket->file;
1756 MT_DEBUG("qtaguid[%d]: leaving filp=NULL\n", par->hooknum);
1757 account_for_uid(skb, sk, 0, par);
1758 res = ((info->match ^ info->invert) &
1759 (XT_QTAGUID_UID | XT_QTAGUID_GID)) == 0;
1760 atomic64_inc(&qtu_events.match_no_sk_file);
1761 goto put_sock_ret_res;
1763 sock_uid = filp->f_cred->fsuid;
1765 * TODO: unhack how to force just accounting.
1766 * For now we only do iface stats when the uid-owner is not requested
1768 if (!(info->match & XT_QTAGUID_UID))
1769 account_for_uid(skb, sk, from_kuid(&init_user_ns, sock_uid), par);
1772 * The following two tests fail the match when:
1773 * id not in range AND no inverted condition requested
1774 * or id in range AND inverted condition requested
1775 * Thus (!a && b) || (a && !b) == a ^ b
1777 if (info->match & XT_QTAGUID_UID) {
1778 kuid_t uid_min = make_kuid(&init_user_ns, info->uid_min);
1779 kuid_t uid_max = make_kuid(&init_user_ns, info->uid_max);
1781 if ((uid_gte(filp->f_cred->fsuid, uid_min) &&
1782 uid_lte(filp->f_cred->fsuid, uid_max)) ^
1783 !(info->invert & XT_QTAGUID_UID)) {
1784 MT_DEBUG("qtaguid[%d]: leaving uid not matching\n",
1787 goto put_sock_ret_res;
1790 if (info->match & XT_QTAGUID_GID) {
1791 kgid_t gid_min = make_kgid(&init_user_ns, info->gid_min);
1792 kgid_t gid_max = make_kgid(&init_user_ns, info->gid_max);
1794 if ((gid_gte(filp->f_cred->fsgid, gid_min) &&
1795 gid_lte(filp->f_cred->fsgid, gid_max)) ^
1796 !(info->invert & XT_QTAGUID_GID)) {
1797 MT_DEBUG("qtaguid[%d]: leaving gid not matching\n",
1800 goto put_sock_ret_res;
1803 MT_DEBUG("qtaguid[%d]: leaving matched\n", par->hooknum);
1809 if (set_sk_callback_lock)
1810 read_unlock_bh(&sk->sk_callback_lock);
1812 MT_DEBUG("qtaguid[%d]: left %d\n", par->hooknum, res);
1817 /* This function is not in xt_qtaguid_print.c because of locks visibility */
1818 static void prdebug_full_state(int indent_level, const char *fmt, ...)
1824 if (!unlikely(qtaguid_debug_mask & DDEBUG_MASK))
1827 fmt_buff = kasprintf(GFP_ATOMIC,
1828 "qtaguid: %s(): %s {\n", __func__, fmt);
1830 va_start(args, fmt);
1831 buff = kvasprintf(GFP_ATOMIC,
1834 pr_debug("%s", buff);
1839 spin_lock_bh(&sock_tag_list_lock);
1840 prdebug_sock_tag_tree(indent_level, &sock_tag_tree);
1841 spin_unlock_bh(&sock_tag_list_lock);
1843 spin_lock_bh(&sock_tag_list_lock);
1844 spin_lock_bh(&uid_tag_data_tree_lock);
1845 prdebug_uid_tag_data_tree(indent_level, &uid_tag_data_tree);
1846 prdebug_proc_qtu_data_tree(indent_level, &proc_qtu_data_tree);
1847 spin_unlock_bh(&uid_tag_data_tree_lock);
1848 spin_unlock_bh(&sock_tag_list_lock);
1850 spin_lock_bh(&iface_stat_list_lock);
1851 prdebug_iface_stat_list(indent_level, &iface_stat_list);
1852 spin_unlock_bh(&iface_stat_list_lock);
1854 pr_debug("qtaguid: %s(): }\n", __func__);
1857 static void prdebug_full_state(int indent_level, const char *fmt, ...) {}
1860 struct proc_ctrl_print_info {
1861 struct sock *sk; /* socket found by reading to sk_pos */
1865 static void *qtaguid_ctrl_proc_next(struct seq_file *m, void *v, loff_t *pos)
1867 struct proc_ctrl_print_info *pcpi = m->private;
1868 struct sock_tag *sock_tag_entry = v;
1869 struct rb_node *node;
1873 if (!v || v == SEQ_START_TOKEN)
1876 node = rb_next(&sock_tag_entry->sock_node);
1879 sock_tag_entry = SEQ_START_TOKEN;
1881 sock_tag_entry = rb_entry(node, struct sock_tag, sock_node);
1882 pcpi->sk = sock_tag_entry->sk;
1884 pcpi->sk_pos = *pos;
1885 return sock_tag_entry;
1888 static void *qtaguid_ctrl_proc_start(struct seq_file *m, loff_t *pos)
1890 struct proc_ctrl_print_info *pcpi = m->private;
1891 struct sock_tag *sock_tag_entry;
1892 struct rb_node *node;
1894 spin_lock_bh(&sock_tag_list_lock);
1896 if (unlikely(module_passive))
1901 node = rb_first(&sock_tag_tree);
1904 return SEQ_START_TOKEN;
1906 sock_tag_entry = rb_entry(node, struct sock_tag, sock_node);
1907 pcpi->sk = sock_tag_entry->sk;
1909 sock_tag_entry = (pcpi->sk ? get_sock_stat_nl(pcpi->sk) :
1910 NULL) ?: SEQ_START_TOKEN;
1911 if (*pos != pcpi->sk_pos) {
1912 /* seq_read skipped a next call */
1913 *pos = pcpi->sk_pos;
1914 return qtaguid_ctrl_proc_next(m, sock_tag_entry, pos);
1917 return sock_tag_entry;
1920 static void qtaguid_ctrl_proc_stop(struct seq_file *m, void *v)
1922 spin_unlock_bh(&sock_tag_list_lock);
1926 * Procfs reader to get all active socket tags using style "1)" as described in
1929 static int qtaguid_ctrl_proc_show(struct seq_file *m, void *v)
1931 struct sock_tag *sock_tag_entry = v;
1935 CT_DEBUG("qtaguid: proc ctrl pid=%u tgid=%u uid=%u\n",
1936 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
1938 if (sock_tag_entry != SEQ_START_TOKEN) {
1939 uid = get_uid_from_tag(sock_tag_entry->tag);
1940 CT_DEBUG("qtaguid: proc_read(): sk=%p tag=0x%llx (uid=%u) "
1943 sock_tag_entry->tag,
1947 f_count = atomic_long_read(
1948 &sock_tag_entry->socket->file->f_count);
1949 seq_printf(m, "sock=%pK tag=0x%llx (uid=%u) pid=%u "
1952 sock_tag_entry->tag, uid,
1953 sock_tag_entry->pid, f_count);
1955 seq_printf(m, "events: sockets_tagged=%llu "
1956 "sockets_untagged=%llu "
1957 "counter_set_changes=%llu "
1959 "iface_events=%llu "
1961 "match_calls_prepost=%llu "
1962 "match_found_sk=%llu "
1963 "match_found_sk_in_ct=%llu "
1964 "match_found_no_sk_in_ct=%llu "
1966 "match_no_sk_file=%llu\n",
1967 (u64)atomic64_read(&qtu_events.sockets_tagged),
1968 (u64)atomic64_read(&qtu_events.sockets_untagged),
1969 (u64)atomic64_read(&qtu_events.counter_set_changes),
1970 (u64)atomic64_read(&qtu_events.delete_cmds),
1971 (u64)atomic64_read(&qtu_events.iface_events),
1972 (u64)atomic64_read(&qtu_events.match_calls),
1973 (u64)atomic64_read(&qtu_events.match_calls_prepost),
1974 (u64)atomic64_read(&qtu_events.match_found_sk),
1975 (u64)atomic64_read(&qtu_events.match_found_sk_in_ct),
1976 (u64)atomic64_read(&qtu_events.match_found_no_sk_in_ct),
1977 (u64)atomic64_read(&qtu_events.match_no_sk),
1978 (u64)atomic64_read(&qtu_events.match_no_sk_file));
1980 /* Count the following as part of the last item_index */
1981 prdebug_full_state(0, "proc ctrl");
1988 * Delete socket tags, and stat tags associated with a given
1989 * accouting tag and uid.
1991 static int ctrl_cmd_delete(const char *input)
2000 struct iface_stat *iface_entry;
2001 struct rb_node *node;
2002 struct sock_tag *st_entry;
2003 struct rb_root st_to_free_tree = RB_ROOT;
2004 struct tag_stat *ts_entry;
2005 struct tag_counter_set *tcs_entry;
2006 struct tag_ref *tr_entry;
2007 struct uid_tag_data *utd_entry;
2009 argc = sscanf(input, "%c %llu %u", &cmd, &acct_tag, &uid_int);
2010 uid = make_kuid(&init_user_ns, uid_int);
2011 CT_DEBUG("qtaguid: ctrl_delete(%s): argc=%d cmd=%c "
2012 "user_tag=0x%llx uid=%u\n", input, argc, cmd,
2018 if (!valid_atag(acct_tag)) {
2019 pr_info("qtaguid: ctrl_delete(%s): invalid tag\n", input);
2024 uid = current_fsuid();
2025 uid_int = from_kuid(&init_user_ns, uid);
2026 } else if (!can_impersonate_uid(uid)) {
2027 pr_info("qtaguid: ctrl_delete(%s): "
2028 "insufficient priv from pid=%u tgid=%u uid=%u\n",
2029 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
2034 tag = combine_atag_with_uid(acct_tag, uid_int);
2035 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2036 "looking for tag=0x%llx (uid=%u)\n",
2037 input, tag, uid_int);
2039 /* Delete socket tags */
2040 spin_lock_bh(&sock_tag_list_lock);
2041 node = rb_first(&sock_tag_tree);
2043 st_entry = rb_entry(node, struct sock_tag, sock_node);
2044 entry_uid = get_uid_from_tag(st_entry->tag);
2045 node = rb_next(node);
2046 if (entry_uid != uid_int)
2049 CT_DEBUG("qtaguid: ctrl_delete(%s): st tag=0x%llx (uid=%u)\n",
2050 input, st_entry->tag, entry_uid);
2052 if (!acct_tag || st_entry->tag == tag) {
2053 rb_erase(&st_entry->sock_node, &sock_tag_tree);
2054 /* Can't sockfd_put() within spinlock, do it later. */
2055 sock_tag_tree_insert(st_entry, &st_to_free_tree);
2056 tr_entry = lookup_tag_ref(st_entry->tag, NULL);
2057 BUG_ON(tr_entry->num_sock_tags <= 0);
2058 tr_entry->num_sock_tags--;
2060 * TODO: remove if, and start failing.
2061 * This is a hack to work around the fact that in some
2062 * places we have "if (IS_ERR_OR_NULL(pqd_entry))"
2063 * and are trying to work around apps
2064 * that didn't open the /dev/xt_qtaguid.
2066 if (st_entry->list.next && st_entry->list.prev)
2067 list_del(&st_entry->list);
2070 spin_unlock_bh(&sock_tag_list_lock);
2072 sock_tag_tree_erase(&st_to_free_tree);
2074 /* Delete tag counter-sets */
2075 spin_lock_bh(&tag_counter_set_list_lock);
2076 /* Counter sets are only on the uid tag, not full tag */
2077 tcs_entry = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
2079 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2080 "erase tcs: tag=0x%llx (uid=%u) set=%d\n",
2083 get_uid_from_tag(tcs_entry->tn.tag),
2084 tcs_entry->active_set);
2085 rb_erase(&tcs_entry->tn.node, &tag_counter_set_tree);
2088 spin_unlock_bh(&tag_counter_set_list_lock);
2091 * If acct_tag is 0, then all entries belonging to uid are
2094 spin_lock_bh(&iface_stat_list_lock);
2095 list_for_each_entry(iface_entry, &iface_stat_list, list) {
2096 spin_lock_bh(&iface_entry->tag_stat_list_lock);
2097 node = rb_first(&iface_entry->tag_stat_tree);
2099 ts_entry = rb_entry(node, struct tag_stat, tn.node);
2100 entry_uid = get_uid_from_tag(ts_entry->tn.tag);
2101 node = rb_next(node);
2103 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2104 "ts tag=0x%llx (uid=%u)\n",
2105 input, ts_entry->tn.tag, entry_uid);
2107 if (entry_uid != uid_int)
2109 if (!acct_tag || ts_entry->tn.tag == tag) {
2110 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2111 "erase ts: %s 0x%llx %u\n",
2112 input, iface_entry->ifname,
2113 get_atag_from_tag(ts_entry->tn.tag),
2115 rb_erase(&ts_entry->tn.node,
2116 &iface_entry->tag_stat_tree);
2120 spin_unlock_bh(&iface_entry->tag_stat_list_lock);
2122 spin_unlock_bh(&iface_stat_list_lock);
2124 /* Cleanup the uid_tag_data */
2125 spin_lock_bh(&uid_tag_data_tree_lock);
2126 node = rb_first(&uid_tag_data_tree);
2128 utd_entry = rb_entry(node, struct uid_tag_data, node);
2129 entry_uid = utd_entry->uid;
2130 node = rb_next(node);
2132 CT_DEBUG("qtaguid: ctrl_delete(%s): "
2136 if (entry_uid != uid_int)
2139 * Go over the tag_refs, and those that don't have
2140 * sock_tags using them are freed.
2142 put_tag_ref_tree(tag, utd_entry);
2143 put_utd_entry(utd_entry);
2145 spin_unlock_bh(&uid_tag_data_tree_lock);
2147 atomic64_inc(&qtu_events.delete_cmds);
2154 static int ctrl_cmd_counter_set(const char *input)
2160 struct tag_counter_set *tcs;
2163 argc = sscanf(input, "%c %d %u", &cmd, &counter_set, &uid);
2164 CT_DEBUG("qtaguid: ctrl_counterset(%s): argc=%d cmd=%c "
2165 "set=%d uid=%u\n", input, argc, cmd,
2171 if (counter_set < 0 || counter_set >= IFS_MAX_COUNTER_SETS) {
2172 pr_info("qtaguid: ctrl_counterset(%s): invalid counter_set range\n",
2177 if (!can_manipulate_uids()) {
2178 pr_info("qtaguid: ctrl_counterset(%s): "
2179 "insufficient priv from pid=%u tgid=%u uid=%u\n",
2180 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
2185 tag = make_tag_from_uid(uid);
2186 spin_lock_bh(&tag_counter_set_list_lock);
2187 tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
2189 tcs = kzalloc(sizeof(*tcs), GFP_ATOMIC);
2191 spin_unlock_bh(&tag_counter_set_list_lock);
2192 pr_err("qtaguid: ctrl_counterset(%s): "
2193 "failed to alloc counter set\n",
2199 tag_counter_set_tree_insert(tcs, &tag_counter_set_tree);
2200 CT_DEBUG("qtaguid: ctrl_counterset(%s): added tcs tag=0x%llx "
2201 "(uid=%u) set=%d\n",
2202 input, tag, get_uid_from_tag(tag), counter_set);
2204 tcs->active_set = counter_set;
2205 spin_unlock_bh(&tag_counter_set_list_lock);
2206 atomic64_inc(&qtu_events.counter_set_changes);
2213 static int ctrl_cmd_tag(const char *input)
2218 unsigned int uid_int = 0;
2219 tag_t acct_tag = make_atag_from_value(0);
2221 struct socket *el_socket;
2223 struct sock_tag *sock_tag_entry;
2224 struct tag_ref *tag_ref_entry;
2225 struct uid_tag_data *uid_tag_data_entry;
2226 struct proc_qtu_data *pqd_entry;
2228 /* Unassigned args will get defaulted later. */
2229 argc = sscanf(input, "%c %d %llu %u", &cmd, &sock_fd, &acct_tag, &uid_int);
2230 uid = make_kuid(&init_user_ns, uid_int);
2231 CT_DEBUG("qtaguid: ctrl_tag(%s): argc=%d cmd=%c sock_fd=%d "
2232 "acct_tag=0x%llx uid=%u\n", input, argc, cmd, sock_fd,
2238 el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */
2240 pr_info("qtaguid: ctrl_tag(%s): failed to lookup"
2241 " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n",
2242 input, sock_fd, res, current->pid, current->tgid,
2243 from_kuid(&init_user_ns, current_fsuid()));
2246 CT_DEBUG("qtaguid: ctrl_tag(%s): socket->...->f_count=%ld ->sk=%p\n",
2247 input, atomic_long_read(&el_socket->file->f_count),
2250 acct_tag = make_atag_from_value(0);
2251 } else if (!valid_atag(acct_tag)) {
2252 pr_info("qtaguid: ctrl_tag(%s): invalid tag\n", input);
2256 CT_DEBUG("qtaguid: ctrl_tag(%s): "
2257 "pid=%u tgid=%u uid=%u euid=%u fsuid=%u "
2258 "ctrl.gid=%u in_group()=%d in_egroup()=%d\n",
2259 input, current->pid, current->tgid,
2260 from_kuid(&init_user_ns, current_uid()),
2261 from_kuid(&init_user_ns, current_euid()),
2262 from_kuid(&init_user_ns, current_fsuid()),
2263 from_kgid(&init_user_ns, xt_qtaguid_ctrl_file->gid),
2264 in_group_p(xt_qtaguid_ctrl_file->gid),
2265 in_egroup_p(xt_qtaguid_ctrl_file->gid));
2267 uid = current_fsuid();
2268 uid_int = from_kuid(&init_user_ns, uid);
2269 } else if (!can_impersonate_uid(uid)) {
2270 pr_info("qtaguid: ctrl_tag(%s): "
2271 "insufficient priv from pid=%u tgid=%u uid=%u\n",
2272 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
2276 full_tag = combine_atag_with_uid(acct_tag, uid_int);
2278 spin_lock_bh(&sock_tag_list_lock);
2279 sock_tag_entry = get_sock_stat_nl(el_socket->sk);
2280 tag_ref_entry = get_tag_ref(full_tag, &uid_tag_data_entry);
2281 if (IS_ERR(tag_ref_entry)) {
2282 res = PTR_ERR(tag_ref_entry);
2283 spin_unlock_bh(&sock_tag_list_lock);
2286 tag_ref_entry->num_sock_tags++;
2287 if (sock_tag_entry) {
2288 struct tag_ref *prev_tag_ref_entry;
2290 CT_DEBUG("qtaguid: ctrl_tag(%s): retag for sk=%p "
2291 "st@%p ...->f_count=%ld\n",
2292 input, el_socket->sk, sock_tag_entry,
2293 atomic_long_read(&el_socket->file->f_count));
2295 * This is a re-tagging, so release the sock_fd that was
2296 * locked at the time of the 1st tagging.
2297 * There is still the ref from this call's sockfd_lookup() so
2298 * it can be done within the spinlock.
2300 sockfd_put(sock_tag_entry->socket);
2301 prev_tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag,
2302 &uid_tag_data_entry);
2303 BUG_ON(IS_ERR_OR_NULL(prev_tag_ref_entry));
2304 BUG_ON(prev_tag_ref_entry->num_sock_tags <= 0);
2305 prev_tag_ref_entry->num_sock_tags--;
2306 sock_tag_entry->tag = full_tag;
2308 CT_DEBUG("qtaguid: ctrl_tag(%s): newtag for sk=%p\n",
2309 input, el_socket->sk);
2310 sock_tag_entry = kzalloc(sizeof(*sock_tag_entry),
2312 if (!sock_tag_entry) {
2313 pr_err("qtaguid: ctrl_tag(%s): "
2314 "socket tag alloc failed\n",
2316 spin_unlock_bh(&sock_tag_list_lock);
2318 goto err_tag_unref_put;
2320 sock_tag_entry->sk = el_socket->sk;
2321 sock_tag_entry->socket = el_socket;
2322 sock_tag_entry->pid = current->tgid;
2323 sock_tag_entry->tag = combine_atag_with_uid(acct_tag, uid_int);
2324 spin_lock_bh(&uid_tag_data_tree_lock);
2325 pqd_entry = proc_qtu_data_tree_search(
2326 &proc_qtu_data_tree, current->tgid);
2328 * TODO: remove if, and start failing.
2329 * At first, we want to catch user-space code that is not
2330 * opening the /dev/xt_qtaguid.
2332 if (IS_ERR_OR_NULL(pqd_entry))
2335 "User space forgot to open /dev/xt_qtaguid? "
2336 "pid=%u tgid=%u uid=%u\n", __func__,
2337 current->pid, current->tgid,
2338 from_kuid(&init_user_ns, current_fsuid()));
2340 list_add(&sock_tag_entry->list,
2341 &pqd_entry->sock_tag_list);
2342 spin_unlock_bh(&uid_tag_data_tree_lock);
2344 sock_tag_tree_insert(sock_tag_entry, &sock_tag_tree);
2345 atomic64_inc(&qtu_events.sockets_tagged);
2347 spin_unlock_bh(&sock_tag_list_lock);
2348 /* We keep the ref to the socket (file) until it is untagged */
2349 CT_DEBUG("qtaguid: ctrl_tag(%s): done st@%p ...->f_count=%ld\n",
2350 input, sock_tag_entry,
2351 atomic_long_read(&el_socket->file->f_count));
2355 BUG_ON(tag_ref_entry->num_sock_tags <= 0);
2356 tag_ref_entry->num_sock_tags--;
2357 free_tag_ref_from_utd_entry(tag_ref_entry, uid_tag_data_entry);
2359 CT_DEBUG("qtaguid: ctrl_tag(%s): done. ...->f_count=%ld\n",
2360 input, atomic_long_read(&el_socket->file->f_count) - 1);
2361 /* Release the sock_fd that was grabbed by sockfd_lookup(). */
2362 sockfd_put(el_socket);
2366 CT_DEBUG("qtaguid: ctrl_tag(%s): done.\n", input);
2370 static int ctrl_cmd_untag(const char *input)
2374 struct socket *el_socket;
2376 struct sock_tag *sock_tag_entry;
2377 struct tag_ref *tag_ref_entry;
2378 struct uid_tag_data *utd_entry;
2379 struct proc_qtu_data *pqd_entry;
2381 argc = sscanf(input, "%c %d", &cmd, &sock_fd);
2382 CT_DEBUG("qtaguid: ctrl_untag(%s): argc=%d cmd=%c sock_fd=%d\n",
2383 input, argc, cmd, sock_fd);
2388 el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */
2390 pr_info("qtaguid: ctrl_untag(%s): failed to lookup"
2391 " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n",
2392 input, sock_fd, res, current->pid, current->tgid,
2393 from_kuid(&init_user_ns, current_fsuid()));
2396 CT_DEBUG("qtaguid: ctrl_untag(%s): socket->...->f_count=%ld ->sk=%p\n",
2397 input, atomic_long_read(&el_socket->file->f_count),
2399 spin_lock_bh(&sock_tag_list_lock);
2400 sock_tag_entry = get_sock_stat_nl(el_socket->sk);
2401 if (!sock_tag_entry) {
2402 spin_unlock_bh(&sock_tag_list_lock);
2407 * The socket already belongs to the current process
2408 * so it can do whatever it wants to it.
2410 rb_erase(&sock_tag_entry->sock_node, &sock_tag_tree);
2412 tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag, &utd_entry);
2413 BUG_ON(!tag_ref_entry);
2414 BUG_ON(tag_ref_entry->num_sock_tags <= 0);
2415 spin_lock_bh(&uid_tag_data_tree_lock);
2416 pqd_entry = proc_qtu_data_tree_search(
2417 &proc_qtu_data_tree, current->tgid);
2419 * TODO: remove if, and start failing.
2420 * At first, we want to catch user-space code that is not
2421 * opening the /dev/xt_qtaguid.
2423 if (IS_ERR_OR_NULL(pqd_entry))
2424 pr_warn_once("qtaguid: %s(): "
2425 "User space forgot to open /dev/xt_qtaguid? "
2426 "pid=%u tgid=%u uid=%u\n", __func__,
2427 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
2429 list_del(&sock_tag_entry->list);
2430 spin_unlock_bh(&uid_tag_data_tree_lock);
2432 * We don't free tag_ref from the utd_entry here,
2433 * only during a cmd_delete().
2435 tag_ref_entry->num_sock_tags--;
2436 spin_unlock_bh(&sock_tag_list_lock);
2438 * Release the sock_fd that was grabbed at tag time,
2439 * and once more for the sockfd_lookup() here.
2441 sockfd_put(sock_tag_entry->socket);
2442 CT_DEBUG("qtaguid: ctrl_untag(%s): done. st@%p ...->f_count=%ld\n",
2443 input, sock_tag_entry,
2444 atomic_long_read(&el_socket->file->f_count) - 1);
2445 sockfd_put(el_socket);
2447 kfree(sock_tag_entry);
2448 atomic64_inc(&qtu_events.sockets_untagged);
2453 CT_DEBUG("qtaguid: ctrl_untag(%s): done. socket->...->f_count=%ld\n",
2454 input, atomic_long_read(&el_socket->file->f_count) - 1);
2455 /* Release the sock_fd that was grabbed by sockfd_lookup(). */
2456 sockfd_put(el_socket);
2460 CT_DEBUG("qtaguid: ctrl_untag(%s): done.\n", input);
2464 static ssize_t qtaguid_ctrl_parse(const char *input, size_t count)
2469 CT_DEBUG("qtaguid: ctrl(%s): pid=%u tgid=%u uid=%u\n",
2470 input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
2473 /* Collect params for commands */
2476 res = ctrl_cmd_delete(input);
2480 res = ctrl_cmd_counter_set(input);
2484 res = ctrl_cmd_tag(input);
2488 res = ctrl_cmd_untag(input);
2498 CT_DEBUG("qtaguid: ctrl(%s): res=%zd\n", input, res);
2502 #define MAX_QTAGUID_CTRL_INPUT_LEN 255
2503 static ssize_t qtaguid_ctrl_proc_write(struct file *file, const char __user *buffer,
2504 size_t count, loff_t *offp)
2506 char input_buf[MAX_QTAGUID_CTRL_INPUT_LEN];
2508 if (unlikely(module_passive))
2511 if (count >= MAX_QTAGUID_CTRL_INPUT_LEN)
2514 if (copy_from_user(input_buf, buffer, count))
2517 input_buf[count] = '\0';
2518 return qtaguid_ctrl_parse(input_buf, count);
2521 struct proc_print_info {
2522 struct iface_stat *iface_entry;
2524 tag_t tag; /* tag found by reading to tag_pos */
2529 static void pp_stats_header(struct seq_file *m)
2532 "idx iface acct_tag_hex uid_tag_int cnt_set "
2533 "rx_bytes rx_packets "
2534 "tx_bytes tx_packets "
2535 "rx_tcp_bytes rx_tcp_packets "
2536 "rx_udp_bytes rx_udp_packets "
2537 "rx_other_bytes rx_other_packets "
2538 "tx_tcp_bytes tx_tcp_packets "
2539 "tx_udp_bytes tx_udp_packets "
2540 "tx_other_bytes tx_other_packets\n");
2543 static int pp_stats_line(struct seq_file *m, struct tag_stat *ts_entry,
2546 struct data_counters *cnts;
2547 tag_t tag = ts_entry->tn.tag;
2548 uid_t stat_uid = get_uid_from_tag(tag);
2549 struct proc_print_info *ppi = m->private;
2550 /* Detailed tags are not available to everybody */
2551 if (!can_read_other_uid_stats(make_kuid(&init_user_ns,stat_uid))) {
2552 CT_DEBUG("qtaguid: stats line: "
2553 "%s 0x%llx %u: insufficient priv "
2554 "from pid=%u tgid=%u uid=%u stats.gid=%u\n",
2555 ppi->iface_entry->ifname,
2556 get_atag_from_tag(tag), stat_uid,
2557 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()),
2558 from_kgid(&init_user_ns,xt_qtaguid_stats_file->gid));
2562 cnts = &ts_entry->counters;
2563 seq_printf(m, "%d %s 0x%llx %u %u "
2573 ppi->iface_entry->ifname,
2574 get_atag_from_tag(tag),
2577 dc_sum_bytes(cnts, cnt_set, IFS_RX),
2578 dc_sum_packets(cnts, cnt_set, IFS_RX),
2579 dc_sum_bytes(cnts, cnt_set, IFS_TX),
2580 dc_sum_packets(cnts, cnt_set, IFS_TX),
2581 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes,
2582 cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets,
2583 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes,
2584 cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets,
2585 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes,
2586 cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets,
2587 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes,
2588 cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets,
2589 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes,
2590 cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets,
2591 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes,
2592 cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets);
2593 return seq_has_overflowed(m) ? -ENOSPC : 1;
2596 static bool pp_sets(struct seq_file *m, struct tag_stat *ts_entry)
2600 for (counter_set = 0; counter_set < IFS_MAX_COUNTER_SETS;
2602 ret = pp_stats_line(m, ts_entry, counter_set);
2609 static int qtaguid_stats_proc_iface_stat_ptr_valid(struct iface_stat *ptr)
2611 struct iface_stat *iface_entry;
2616 list_for_each_entry(iface_entry, &iface_stat_list, list)
2617 if (iface_entry == ptr)
2622 static void qtaguid_stats_proc_next_iface_entry(struct proc_print_info *ppi)
2624 spin_unlock_bh(&ppi->iface_entry->tag_stat_list_lock);
2625 list_for_each_entry_continue(ppi->iface_entry, &iface_stat_list, list) {
2626 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2629 ppi->iface_entry = NULL;
2632 static void *qtaguid_stats_proc_next(struct seq_file *m, void *v, loff_t *pos)
2634 struct proc_print_info *ppi = m->private;
2635 struct tag_stat *ts_entry;
2636 struct rb_node *node;
2639 pr_err("qtaguid: %s(): unexpected v: NULL\n", __func__);
2645 if (!ppi->iface_entry || unlikely(module_passive))
2648 if (v == SEQ_START_TOKEN)
2649 node = rb_first(&ppi->iface_entry->tag_stat_tree);
2651 node = rb_next(&((struct tag_stat *)v)->tn.node);
2654 qtaguid_stats_proc_next_iface_entry(ppi);
2655 if (!ppi->iface_entry)
2657 node = rb_first(&ppi->iface_entry->tag_stat_tree);
2660 ts_entry = rb_entry(node, struct tag_stat, tn.node);
2661 ppi->tag = ts_entry->tn.tag;
2662 ppi->tag_pos = *pos;
2663 ppi->tag_item_index = ppi->item_index;
2667 static void *qtaguid_stats_proc_start(struct seq_file *m, loff_t *pos)
2669 struct proc_print_info *ppi = m->private;
2670 struct tag_stat *ts_entry = NULL;
2672 spin_lock_bh(&iface_stat_list_lock);
2675 ppi->item_index = 1;
2677 if (list_empty(&iface_stat_list)) {
2678 ppi->iface_entry = NULL;
2680 ppi->iface_entry = list_first_entry(&iface_stat_list,
2683 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2685 return SEQ_START_TOKEN;
2687 if (!qtaguid_stats_proc_iface_stat_ptr_valid(ppi->iface_entry)) {
2688 if (ppi->iface_entry) {
2689 pr_err("qtaguid: %s(): iface_entry %p not found\n",
2690 __func__, ppi->iface_entry);
2691 ppi->iface_entry = NULL;
2696 spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock);
2698 if (!ppi->tag_pos) {
2699 /* seq_read skipped first next call */
2700 ts_entry = SEQ_START_TOKEN;
2702 ts_entry = tag_stat_tree_search(
2703 &ppi->iface_entry->tag_stat_tree, ppi->tag);
2705 pr_info("qtaguid: %s(): tag_stat.tag 0x%llx not found. Abort.\n",
2706 __func__, ppi->tag);
2711 if (*pos == ppi->tag_pos) { /* normal resume */
2712 ppi->item_index = ppi->tag_item_index;
2714 /* seq_read skipped a next call */
2715 *pos = ppi->tag_pos;
2716 ts_entry = qtaguid_stats_proc_next(m, ts_entry, pos);
2722 static void qtaguid_stats_proc_stop(struct seq_file *m, void *v)
2724 struct proc_print_info *ppi = m->private;
2725 if (ppi->iface_entry)
2726 spin_unlock_bh(&ppi->iface_entry->tag_stat_list_lock);
2727 spin_unlock_bh(&iface_stat_list_lock);
2731 * Procfs reader to get all tag stats using style "1)" as described in
2733 * Groups all protocols tx/rx bytes.
2735 static int qtaguid_stats_proc_show(struct seq_file *m, void *v)
2737 struct tag_stat *ts_entry = v;
2739 if (v == SEQ_START_TOKEN)
2742 pp_sets(m, ts_entry);
2747 /*------------------------------------------*/
2748 static int qtudev_open(struct inode *inode, struct file *file)
2750 struct uid_tag_data *utd_entry;
2751 struct proc_qtu_data *pqd_entry;
2752 struct proc_qtu_data *new_pqd_entry;
2754 bool utd_entry_found;
2756 if (unlikely(qtu_proc_handling_passive))
2759 DR_DEBUG("qtaguid: qtudev_open(): pid=%u tgid=%u uid=%u\n",
2760 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
2762 spin_lock_bh(&uid_tag_data_tree_lock);
2764 /* Look for existing uid data, or alloc one. */
2765 utd_entry = get_uid_data(from_kuid(&init_user_ns, current_fsuid()), &utd_entry_found);
2766 if (IS_ERR_OR_NULL(utd_entry)) {
2767 res = PTR_ERR(utd_entry);
2771 /* Look for existing PID based proc_data */
2772 pqd_entry = proc_qtu_data_tree_search(&proc_qtu_data_tree,
2775 pr_err("qtaguid: qtudev_open(): %u/%u %u "
2776 "%s already opened\n",
2777 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()),
2780 goto err_unlock_free_utd;
2783 new_pqd_entry = kzalloc(sizeof(*new_pqd_entry), GFP_ATOMIC);
2784 if (!new_pqd_entry) {
2785 pr_err("qtaguid: qtudev_open(): %u/%u %u: "
2786 "proc data alloc failed\n",
2787 current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
2789 goto err_unlock_free_utd;
2791 new_pqd_entry->pid = current->tgid;
2792 INIT_LIST_HEAD(&new_pqd_entry->sock_tag_list);
2793 new_pqd_entry->parent_tag_data = utd_entry;
2794 utd_entry->num_pqd++;
2796 proc_qtu_data_tree_insert(new_pqd_entry,
2797 &proc_qtu_data_tree);
2799 spin_unlock_bh(&uid_tag_data_tree_lock);
2800 DR_DEBUG("qtaguid: tracking data for uid=%u in pqd=%p\n",
2801 from_kuid(&init_user_ns, current_fsuid()), new_pqd_entry);
2802 file->private_data = new_pqd_entry;
2805 err_unlock_free_utd:
2806 if (!utd_entry_found) {
2807 rb_erase(&utd_entry->node, &uid_tag_data_tree);
2811 spin_unlock_bh(&uid_tag_data_tree_lock);
2815 static int qtudev_release(struct inode *inode, struct file *file)
2817 struct proc_qtu_data *pqd_entry = file->private_data;
2818 struct uid_tag_data *utd_entry = pqd_entry->parent_tag_data;
2819 struct sock_tag *st_entry;
2820 struct rb_root st_to_free_tree = RB_ROOT;
2821 struct list_head *entry, *next;
2824 if (unlikely(qtu_proc_handling_passive))
2828 * Do not trust the current->pid, it might just be a kworker cleaning
2829 * up after a dead proc.
2831 DR_DEBUG("qtaguid: qtudev_release(): "
2832 "pid=%u tgid=%u uid=%u "
2833 "pqd_entry=%p->pid=%u utd_entry=%p->active_tags=%d\n",
2834 current->pid, current->tgid, pqd_entry->parent_tag_data->uid,
2835 pqd_entry, pqd_entry->pid, utd_entry,
2836 utd_entry->num_active_tags);
2838 spin_lock_bh(&sock_tag_list_lock);
2839 spin_lock_bh(&uid_tag_data_tree_lock);
2841 list_for_each_safe(entry, next, &pqd_entry->sock_tag_list) {
2842 st_entry = list_entry(entry, struct sock_tag, list);
2843 DR_DEBUG("qtaguid: %s(): "
2844 "erase sock_tag=%p->sk=%p pid=%u tgid=%u uid=%u\n",
2846 st_entry, st_entry->sk,
2847 current->pid, current->tgid,
2848 pqd_entry->parent_tag_data->uid);
2850 utd_entry = uid_tag_data_tree_search(
2852 get_uid_from_tag(st_entry->tag));
2853 BUG_ON(IS_ERR_OR_NULL(utd_entry));
2854 DR_DEBUG("qtaguid: %s(): "
2855 "looking for tag=0x%llx in utd_entry=%p\n", __func__,
2856 st_entry->tag, utd_entry);
2857 tr = tag_ref_tree_search(&utd_entry->tag_ref_tree,
2860 BUG_ON(tr->num_sock_tags <= 0);
2861 tr->num_sock_tags--;
2862 free_tag_ref_from_utd_entry(tr, utd_entry);
2864 rb_erase(&st_entry->sock_node, &sock_tag_tree);
2865 list_del(&st_entry->list);
2866 /* Can't sockfd_put() within spinlock, do it later. */
2867 sock_tag_tree_insert(st_entry, &st_to_free_tree);
2870 * Try to free the utd_entry if no other proc_qtu_data is
2871 * using it (num_pqd is 0) and it doesn't have active tags
2872 * (num_active_tags is 0).
2874 put_utd_entry(utd_entry);
2877 rb_erase(&pqd_entry->node, &proc_qtu_data_tree);
2878 BUG_ON(pqd_entry->parent_tag_data->num_pqd < 1);
2879 pqd_entry->parent_tag_data->num_pqd--;
2880 put_utd_entry(pqd_entry->parent_tag_data);
2882 file->private_data = NULL;
2884 spin_unlock_bh(&uid_tag_data_tree_lock);
2885 spin_unlock_bh(&sock_tag_list_lock);
2888 sock_tag_tree_erase(&st_to_free_tree);
2890 prdebug_full_state(0, "%s(): pid=%u tgid=%u", __func__,
2891 current->pid, current->tgid);
2895 /*------------------------------------------*/
2896 static const struct file_operations qtudev_fops = {
2897 .owner = THIS_MODULE,
2898 .open = qtudev_open,
2899 .release = qtudev_release,
2902 static struct miscdevice qtu_device = {
2903 .minor = MISC_DYNAMIC_MINOR,
2904 .name = QTU_DEV_NAME,
2905 .fops = &qtudev_fops,
2906 /* How sad it doesn't allow for defaults: .mode = S_IRUGO | S_IWUSR */
2909 static const struct seq_operations proc_qtaguid_ctrl_seqops = {
2910 .start = qtaguid_ctrl_proc_start,
2911 .next = qtaguid_ctrl_proc_next,
2912 .stop = qtaguid_ctrl_proc_stop,
2913 .show = qtaguid_ctrl_proc_show,
2916 static int proc_qtaguid_ctrl_open(struct inode *inode, struct file *file)
2918 return seq_open_private(file, &proc_qtaguid_ctrl_seqops,
2919 sizeof(struct proc_ctrl_print_info));
2922 static const struct file_operations proc_qtaguid_ctrl_fops = {
2923 .open = proc_qtaguid_ctrl_open,
2925 .write = qtaguid_ctrl_proc_write,
2926 .llseek = seq_lseek,
2927 .release = seq_release_private,
2930 static const struct seq_operations proc_qtaguid_stats_seqops = {
2931 .start = qtaguid_stats_proc_start,
2932 .next = qtaguid_stats_proc_next,
2933 .stop = qtaguid_stats_proc_stop,
2934 .show = qtaguid_stats_proc_show,
2937 static int proc_qtaguid_stats_open(struct inode *inode, struct file *file)
2939 return seq_open_private(file, &proc_qtaguid_stats_seqops,
2940 sizeof(struct proc_print_info));
2943 static const struct file_operations proc_qtaguid_stats_fops = {
2944 .open = proc_qtaguid_stats_open,
2946 .llseek = seq_lseek,
2947 .release = seq_release_private,
2950 /*------------------------------------------*/
2951 static int __init qtaguid_proc_register(struct proc_dir_entry **res_procdir)
2954 *res_procdir = proc_mkdir(module_procdirname, init_net.proc_net);
2955 if (!*res_procdir) {
2956 pr_err("qtaguid: failed to create proc/.../xt_qtaguid\n");
2961 xt_qtaguid_ctrl_file = proc_create_data("ctrl", proc_ctrl_perms,
2963 &proc_qtaguid_ctrl_fops,
2965 if (!xt_qtaguid_ctrl_file) {
2966 pr_err("qtaguid: failed to create xt_qtaguid/ctrl "
2972 xt_qtaguid_stats_file = proc_create_data("stats", proc_stats_perms,
2974 &proc_qtaguid_stats_fops,
2976 if (!xt_qtaguid_stats_file) {
2977 pr_err("qtaguid: failed to create xt_qtaguid/stats "
2980 goto no_stats_entry;
2983 * TODO: add support counter hacking
2984 * xt_qtaguid_stats_file->write_proc = qtaguid_stats_proc_write;
2989 remove_proc_entry("ctrl", *res_procdir);
2991 remove_proc_entry("xt_qtaguid", NULL);
2996 static struct xt_match qtaguid_mt_reg __read_mostly = {
2998 * This module masquerades as the "owner" module so that iptables
2999 * tools can deal with it.
3003 .family = NFPROTO_UNSPEC,
3004 .match = qtaguid_mt,
3005 .matchsize = sizeof(struct xt_qtaguid_match_info),
3009 static int __init qtaguid_mt_init(void)
3011 if (qtaguid_proc_register(&xt_qtaguid_procdir)
3012 || iface_stat_init(xt_qtaguid_procdir)
3013 || xt_register_match(&qtaguid_mt_reg)
3014 || misc_register(&qtu_device))
3020 * TODO: allow unloading of the module.
3021 * For now stats are permanent.
3022 * Kconfig forces'y/n' and never an 'm'.
3025 module_init(qtaguid_mt_init);
3026 MODULE_AUTHOR("jpa <jpa@google.com>");
3027 MODULE_DESCRIPTION("Xtables: socket owner+tag matching and associated stats");
3028 MODULE_LICENSE("GPL");
3029 MODULE_ALIAS("ipt_owner");
3030 MODULE_ALIAS("ip6t_owner");
3031 MODULE_ALIAS("ipt_qtaguid");
3032 MODULE_ALIAS("ip6t_qtaguid");