JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Pundir | 6e2b405 | 2015-07-07 00:28:49 +0530 | [diff] [blame] | 20 | #include <linux/miscdevice.h> |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 21 | #include <linux/netfilter/x_tables.h> |
| 22 | #include <linux/netfilter/xt_qtaguid.h> |
JP Abgrall | a2e371b | 2013-04-08 15:09:26 -0700 | [diff] [blame] | 23 | #include <linux/ratelimit.h> |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 24 | #include <linux/seq_file.h> |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 25 | #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 Abgrall | 4bb20aa | 2012-04-17 16:00:07 -0700 | [diff] [blame] | 32 | #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) |
| 33 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 34 | #endif |
| 35 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 36 | #include <linux/netfilter/xt_socket.h> |
| 37 | #include "xt_qtaguid_internal.h" |
| 38 | #include "xt_qtaguid_print.h" |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 39 | #include "../../fs/proc/internal.h" |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 40 | |
| 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 | |
| 49 | static const char *module_procdirname = "xt_qtaguid"; |
| 50 | static struct proc_dir_entry *xt_qtaguid_procdir; |
| 51 | |
| 52 | static unsigned int proc_iface_perms = S_IRUGO; |
| 53 | module_param_named(iface_perms, proc_iface_perms, uint, S_IRUGO | S_IWUSR); |
| 54 | |
| 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); |
| 58 | |
| 59 | static struct proc_dir_entry *xt_qtaguid_ctrl_file; |
JP Abgrall | 90414bc | 2013-01-04 18:18:36 -0800 | [diff] [blame] | 60 | |
| 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 64 | static unsigned int proc_ctrl_perms = S_IRUGO | S_IWUGO; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 65 | module_param_named(ctrl_perms, proc_ctrl_perms, uint, S_IRUGO | S_IWUSR); |
| 66 | |
JP Abgrall | 90414bc | 2013-01-04 18:18:36 -0800 | [diff] [blame] | 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. |
| 69 | */ |
| 70 | static bool proc_stats_readall_limited = true; |
| 71 | static bool proc_ctrl_write_limited = true; |
| 72 | |
| 73 | module_param_named(stats_readall_limited, proc_stats_readall_limited, bool, |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 74 | S_IRUGO | S_IWUSR); |
JP Abgrall | 90414bc | 2013-01-04 18:18:36 -0800 | [diff] [blame] | 75 | module_param_named(ctrl_write_limited, proc_ctrl_write_limited, bool, |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 76 | 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 | */ |
| 82 | static int max_sock_tags = DEFAULT_MAX_SOCK_TAGS; |
| 83 | module_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 | */ |
| 95 | static bool module_passive; |
| 96 | module_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 | */ |
| 106 | static bool qtu_proc_handling_passive; |
| 107 | module_param_named(tag_tracking_passive, qtu_proc_handling_passive, bool, |
| 108 | S_IRUGO | S_IWUSR); |
| 109 | |
| 110 | #define QTU_DEV_NAME "xt_qtaguid" |
| 111 | |
| 112 | uint qtaguid_debug_mask = DEFAULT_DEBUG_MASK; |
| 113 | module_param_named(debug_mask, qtaguid_debug_mask, uint, S_IRUGO | S_IWUSR); |
| 114 | |
| 115 | /*---------------------------------------------------------------------------*/ |
| 116 | static const char *iface_stat_procdirname = "iface_stat"; |
| 117 | static struct proc_dir_entry *iface_stat_procdir; |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 118 | /* |
| 119 | * The iface_stat_all* will go away once userspace gets use to the new fields |
| 120 | * that have a format line. |
| 121 | */ |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 122 | static const char *iface_stat_all_procfilename = "iface_stat_all"; |
| 123 | static struct proc_dir_entry *iface_stat_all_procfile; |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 124 | static const char *iface_stat_fmt_procfilename = "iface_stat_fmt"; |
| 125 | static struct proc_dir_entry *iface_stat_fmt_procfile; |
| 126 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 127 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 128 | static LIST_HEAD(iface_stat_list); |
| 129 | static DEFINE_SPINLOCK(iface_stat_list_lock); |
| 130 | |
| 131 | static struct rb_root sock_tag_tree = RB_ROOT; |
| 132 | static DEFINE_SPINLOCK(sock_tag_list_lock); |
| 133 | |
| 134 | static struct rb_root tag_counter_set_tree = RB_ROOT; |
| 135 | static DEFINE_SPINLOCK(tag_counter_set_list_lock); |
| 136 | |
| 137 | static struct rb_root uid_tag_data_tree = RB_ROOT; |
| 138 | static DEFINE_SPINLOCK(uid_tag_data_tree_lock); |
| 139 | |
| 140 | static struct rb_root proc_qtu_data_tree = RB_ROOT; |
| 141 | /* No proc_qtu_data_tree_lock; use uid_tag_data_tree_lock */ |
| 142 | |
| 143 | static struct qtaguid_event_counts qtu_events; |
| 144 | /*----------------------------------------------*/ |
| 145 | static bool can_manipulate_uids(void) |
| 146 | { |
| 147 | /* root pwnd */ |
JP Abgrall | 90414bc | 2013-01-04 18:18:36 -0800 | [diff] [blame] | 148 | return in_egroup_p(xt_qtaguid_ctrl_file->gid) |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 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)); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 151 | } |
| 152 | |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 153 | static bool can_impersonate_uid(kuid_t uid) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 154 | { |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 155 | return uid_eq(uid, current_fsuid()) || can_manipulate_uids(); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 156 | } |
| 157 | |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 158 | static bool can_read_other_uid_stats(kuid_t uid) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 159 | { |
| 160 | /* root pwnd */ |
JP Abgrall | 90414bc | 2013-01-04 18:18:36 -0800 | [diff] [blame] | 161 | return in_egroup_p(xt_qtaguid_stats_file->gid) |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 162 | || unlikely(!from_kuid(&init_user_ns, current_fsuid())) || uid_eq(uid, current_fsuid()) |
JP Abgrall | 90414bc | 2013-01-04 18:18:36 -0800 | [diff] [blame] | 163 | || unlikely(!proc_stats_readall_limited) |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 164 | || unlikely(uid_eq(current_fsuid(), xt_qtaguid_ctrl_file->uid)); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 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, |
| 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 177 | static 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 | |
| 200 | static 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 | |
| 227 | static void tag_stat_tree_insert(struct tag_stat *data, struct rb_root *root) |
| 228 | { |
| 229 | tag_node_tree_insert(&data->tn, root); |
| 230 | } |
| 231 | |
| 232 | static 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 | |
| 240 | static 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 | |
| 246 | static 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 | |
| 256 | static void tag_ref_tree_insert(struct tag_ref *data, struct rb_root *root) |
| 257 | { |
| 258 | tag_node_tree_insert(&data->tn, root); |
| 259 | } |
| 260 | |
| 261 | static 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 | |
| 269 | static 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 | |
| 287 | static 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 | |
| 309 | static 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 Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 324 | sock_put(st_entry->sk); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 325 | kfree(st_entry); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | static 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 | |
| 348 | static 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 | |
| 372 | static 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 | |
| 396 | static 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 | */ |
| 423 | struct 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. */ |
| 451 | static 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 | |
| 483 | err_res: |
| 484 | return ERR_PTR(res); |
| 485 | } |
| 486 | |
| 487 | static 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. */ |
| 514 | static 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 522 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 527 | 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 */ |
| 535 | static 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 544 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 545 | 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 | */ |
| 563 | static 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 | |
| 578 | static 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 Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 596 | static ssize_t read_proc_u64(struct file *file, char __user *buf, |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 597 | size_t size, loff_t *ppos) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 598 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 599 | uint64_t *valuep = PDE_DATA(file_inode(file)); |
| 600 | char tmp[24]; |
| 601 | size_t tmp_size; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 602 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 603 | tmp_size = scnprintf(tmp, sizeof(tmp), "%llu\n", *valuep); |
| 604 | return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 607 | static ssize_t read_proc_bool(struct file *file, char __user *buf, |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 608 | size_t size, loff_t *ppos) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 609 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 610 | bool *valuep = PDE_DATA(file_inode(file)); |
| 611 | char tmp[24]; |
| 612 | size_t tmp_size; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 613 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 614 | tmp_size = scnprintf(tmp, sizeof(tmp), "%u\n", *valuep); |
| 615 | return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static 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 | */ |
| 640 | static 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; |
| 656 | done: |
| 657 | return iface_entry; |
| 658 | } |
| 659 | |
JP Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 660 | /* This is for fmt2 only */ |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 661 | static void pp_iface_stat_header(struct seq_file *m) |
JP Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 662 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 663 | 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 Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 676 | static void pp_iface_stat_line(struct seq_file *m, |
| 677 | struct iface_stat *iface_entry) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 678 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 679 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 702 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 703 | struct proc_iface_stat_fmt_info { |
| 704 | int fmt; |
| 705 | }; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 706 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 707 | static 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 711 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 712 | /* |
| 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 717 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 718 | if (unlikely(module_passive)) |
| 719 | return NULL; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 720 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 721 | if (!n && p->fmt == 2) |
| 722 | pp_iface_stat_header(m); |
| 723 | |
| 724 | return seq_list_start(&iface_stat_list, n); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 727 | static 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 | |
| 732 | static void iface_stat_fmt_proc_stop(struct seq_file *m, void *p) |
| 733 | { |
| 734 | spin_unlock_bh(&iface_stat_list_lock); |
| 735 | } |
| 736 | |
| 737 | static 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 746 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 747 | |
| 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 | |
| 777 | static const struct file_operations read_u64_fops = { |
| 778 | .read = read_proc_u64, |
| 779 | .llseek = default_llseek, |
| 780 | }; |
| 781 | |
| 782 | static const struct file_operations read_bool_fops = { |
| 783 | .read = read_proc_bool, |
| 784 | .llseek = default_llseek, |
| 785 | }; |
| 786 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 787 | static 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åg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 804 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 818 | |
| 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 | */ |
| 828 | static 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 */ |
| 851 | static 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 | |
| 893 | static 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 919 | iface->totals_via_dev[IFS_TX].bytes += |
| 920 | iface->last_known[IFS_TX].bytes; |
| 921 | iface->totals_via_dev[IFS_TX].packets += |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 922 | iface->last_known[IFS_TX].packets; |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 923 | iface->totals_via_dev[IFS_RX].bytes += |
| 924 | iface->last_known[IFS_RX].bytes; |
| 925 | iface->totals_via_dev[IFS_RX].packets += |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 926 | 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 | */ |
| 940 | static 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 Hackmann | 3729385 | 2017-04-24 16:16:15 -0700 | [diff] [blame] | 970 | ifname, ifa, ifa->ifa_label); |
| 971 | if (!strcmp(ifname, ifa->ifa_label)) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 972 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 986 | IF_DEBUG("qtaguid: iface_stat: create(%s): entry=%p\n", |
| 987 | ifname, entry); |
| 988 | iface_check_stats_reset_and_adjust(net_dev, entry); |
JP Abgrall | cf47f36 | 2013-02-06 17:40:07 -0800 | [diff] [blame] | 989 | _iface_stat_set_active(entry, net_dev, true); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 990 | IF_DEBUG("qtaguid: %s(%s): " |
| 991 | "tracking now %d on ip=%pI4\n", __func__, |
JP Abgrall | cf47f36 | 2013-02-06 17:40:07 -0800 | [diff] [blame] | 992 | entry->ifname, true, &ipaddr); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 993 | 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); |
| 999 | done_unlock_put: |
| 1000 | spin_unlock_bh(&iface_stat_list_lock); |
| 1001 | done_put: |
| 1002 | if (in_dev) |
| 1003 | in_dev_put(in_dev); |
| 1004 | } |
| 1005 | |
| 1006 | static 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1043 | IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__, |
| 1044 | ifname, entry); |
| 1045 | iface_check_stats_reset_and_adjust(net_dev, entry); |
JP Abgrall | cf47f36 | 2013-02-06 17:40:07 -0800 | [diff] [blame] | 1046 | _iface_stat_set_active(entry, net_dev, true); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1047 | IF_DEBUG("qtaguid: %s(%s): " |
| 1048 | "tracking now %d on ip=%pI6c\n", __func__, |
JP Abgrall | cf47f36 | 2013-02-06 17:40:07 -0800 | [diff] [blame] | 1049 | entry->ifname, true, &ifa->addr); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1050 | 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 | |
| 1057 | done_unlock_put: |
| 1058 | spin_unlock_bh(&iface_stat_list_lock); |
| 1059 | done_put: |
| 1060 | in_dev_put(in_dev); |
| 1061 | } |
| 1062 | |
| 1063 | static 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 | |
| 1069 | static 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1081 | static 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1102 | static void |
| 1103 | data_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 | */ |
| 1126 | static 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1162 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1166 | /* 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 Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1175 | /* Guarantied to return a net_device that has a name */ |
| 1176 | static 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 Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1194 | 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1202 | /* |
| 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 | */ |
| 1208 | static 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 Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1213 | enum ifs_tx_rx direction; |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1214 | int bytes = skb->len; |
JP Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 1215 | int proto; |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1216 | |
JP Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1217 | 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1223 | |
| 1224 | spin_lock_bh(&iface_stat_list_lock); |
| 1225 | entry = get_iface_entry(el_dev->name); |
| 1226 | if (entry == NULL) { |
JP Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1227 | IF_DEBUG("qtaguid[%d]: iface_stat: %s(%s): not tracked\n", |
| 1228 | par->hooknum, __func__, el_dev->name); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1229 | spin_unlock_bh(&iface_stat_list_lock); |
| 1230 | return; |
| 1231 | } |
| 1232 | |
JP Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1233 | IF_DEBUG("qtaguid[%d]: %s(%s): entry=%p\n", par->hooknum, __func__, |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1234 | el_dev->name, entry); |
| 1235 | |
JP Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 1236 | data_counters_update(&entry->totals_via_skb, 0, direction, proto, |
| 1237 | bytes); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1238 | spin_unlock_bh(&iface_stat_list_lock); |
| 1239 | } |
| 1240 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1241 | static 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 | */ |
| 1262 | static 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); |
| 1276 | done: |
| 1277 | return new_tag_stat_entry; |
| 1278 | } |
| 1279 | |
| 1280 | static 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 Abgrall | e5d7986 | 2012-04-13 19:22:35 -0700 | [diff] [blame] | 1290 | struct tag_stat *new_tag_stat = NULL; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1291 | 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.zhang | f84e6a1 | 2016-01-11 13:31:01 +0800 | [diff] [blame] | 1295 | spin_lock_bh(&iface_stat_list_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1296 | iface_entry = get_iface_entry(ifname); |
| 1297 | if (!iface_entry) { |
JP Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1298 | pr_err_ratelimited("qtaguid: tag_stat: stat_update() " |
JP Abgrall | a2e371b | 2013-04-08 15:09:26 -0700 | [diff] [blame] | 1299 | "%s not found\n", ifname); |
liping.zhang | f84e6a1 | 2016-01-11 13:31:01 +0800 | [diff] [blame] | 1300 | spin_unlock_bh(&iface_stat_list_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1301 | return; |
| 1302 | } |
| 1303 | /* It is ok to process data when an iface_entry is inactive */ |
| 1304 | |
JP Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1305 | MT_DEBUG("qtaguid: tag_stat: stat_update() dev=%s entry=%p\n", |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1306 | 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 Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1322 | MT_DEBUG("qtaguid: tag_stat: stat_update(): " |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1323 | " 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.zhang | f84e6a1 | 2016-01-11 13:31:01 +0800 | [diff] [blame] | 1336 | goto unlock; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1337 | } |
| 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 Fuchs | b842ea5 | 2012-11-19 11:44:51 -0800 | [diff] [blame] | 1349 | if (!new_tag_stat) |
| 1350 | goto unlock; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1351 | uid_tag_counters = &new_tag_stat->counters; |
| 1352 | } else { |
| 1353 | uid_tag_counters = &tag_stat_entry->counters; |
| 1354 | } |
| 1355 | |
| 1356 | if (acct_tag) { |
JP Abgrall | e5d7986 | 2012-04-13 19:22:35 -0700 | [diff] [blame] | 1357 | /* Create the child {acct_tag, uid_tag} and hook up parent. */ |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1358 | new_tag_stat = create_if_tag_stat(iface_entry, tag); |
Pontus Fuchs | b842ea5 | 2012-11-19 11:44:51 -0800 | [diff] [blame] | 1359 | if (!new_tag_stat) |
| 1360 | goto unlock; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1361 | new_tag_stat->parent_counters = uid_tag_counters; |
JP Abgrall | e5d7986 | 2012-04-13 19:22:35 -0700 | [diff] [blame] | 1362 | } 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1372 | } |
| 1373 | tag_stat_update(new_tag_stat, direction, proto, bytes); |
Pontus Fuchs | b842ea5 | 2012-11-19 11:44:51 -0800 | [diff] [blame] | 1374 | unlock: |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1375 | spin_unlock_bh(&iface_entry->tag_stat_list_lock); |
liping.zhang | f84e6a1 | 2016-01-11 13:31:01 +0800 | [diff] [blame] | 1376 | spin_unlock_bh(&iface_stat_list_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | static int iface_netdev_event_handler(struct notifier_block *nb, |
| 1380 | unsigned long event, void *ptr) { |
Jon Medhurst (Tixy) | a347e8e | 2014-04-14 21:20:49 -0700 | [diff] [blame] | 1381 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1382 | |
| 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 | |
| 1404 | static 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 | |
| 1435 | static 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 | |
| 1466 | static struct notifier_block iface_netdev_notifier_blk = { |
| 1467 | .notifier_call = iface_netdev_event_handler, |
| 1468 | }; |
| 1469 | |
| 1470 | static struct notifier_block iface_inetaddr_notifier_blk = { |
| 1471 | .notifier_call = iface_inetaddr_event_handler, |
| 1472 | }; |
| 1473 | |
| 1474 | static struct notifier_block iface_inet6addr_notifier_blk = { |
| 1475 | .notifier_call = iface_inet6addr_event_handler, |
| 1476 | }; |
| 1477 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1478 | static 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 | |
| 1485 | static 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 Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 1494 | s->fmt = (uintptr_t)PDE_DATA(inode); |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1495 | return 0; |
| 1496 | } |
| 1497 | |
| 1498 | static 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 Hackmann | 5647291 | 2013-12-04 17:39:27 -0800 | [diff] [blame] | 1502 | .release = seq_release_private, |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1503 | }; |
| 1504 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1505 | static 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åg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1516 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1521 | if (!iface_stat_all_procfile) { |
| 1522 | pr_err("qtaguid: iface_stat: init " |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1523 | " failed to create stat_old proc entry\n"); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1524 | err = -1; |
| 1525 | goto err_zap_entry; |
| 1526 | } |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1527 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1528 | 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1533 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1539 | |
| 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1545 | goto err_zap_all_stats_entries; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1546 | } |
| 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 | |
| 1562 | err_unreg_ip4_addr: |
| 1563 | unregister_inetaddr_notifier(&iface_inetaddr_notifier_blk); |
| 1564 | err_unreg_nd: |
| 1565 | unregister_netdevice_notifier(&iface_netdev_notifier_blk); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1566 | err_zap_all_stats_entries: |
| 1567 | remove_proc_entry(iface_stat_fmt_procfilename, parent_procdir); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1568 | err_zap_all_stats_entry: |
| 1569 | remove_proc_entry(iface_stat_all_procfilename, parent_procdir); |
| 1570 | err_zap_entry: |
| 1571 | remove_proc_entry(iface_stat_procdirname, parent_procdir); |
| 1572 | err: |
| 1573 | return err; |
| 1574 | } |
| 1575 | |
| 1576 | static 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 Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1582 | MT_DEBUG("qtaguid[%d]: find_sk(skb=%p) family=%d\n", |
| 1583 | par->hooknum, skb, par->family); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1584 | |
| 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 Pundir | 7de1bb8 | 2015-11-20 14:45:40 +0530 | [diff] [blame] | 1594 | sk = xt_socket_lookup_slow_v6(dev_net(skb->dev), skb, par->in); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1595 | break; |
| 1596 | case NFPROTO_IPV4: |
Amit Pundir | 7de1bb8 | 2015-11-20 14:45:40 +0530 | [diff] [blame] | 1597 | sk = xt_socket_lookup_slow_v4(dev_net(skb->dev), skb, par->in); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1598 | break; |
| 1599 | default: |
| 1600 | return NULL; |
| 1601 | } |
| 1602 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1603 | if (sk) { |
JP Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1604 | MT_DEBUG("qtaguid[%d]: %p->sk_proto=%u->sk_state=%d\n", |
| 1605 | par->hooknum, sk, sk->sk_protocol, sk->sk_state); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1606 | } |
| 1607 | return sk; |
| 1608 | } |
| 1609 | |
| 1610 | static 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 Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1615 | enum ifs_tx_rx direction; |
| 1616 | int proto; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1617 | |
JP Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1618 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1623 | |
JP Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1624 | if_tag_stat_update(el_dev->name, uid, |
| 1625 | skb->sk ? skb->sk : alternate_sk, |
| 1626 | direction, |
| 1627 | proto, skb->len); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | static 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1636 | kuid_t sock_uid; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1637 | bool res; |
Mohamad Ayyash | e8101fd | 2015-01-13 19:20:44 -0800 | [diff] [blame] | 1638 | bool set_sk_callback_lock = false; |
JP Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1639 | /* |
| 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1644 | |
| 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1657 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1671 | |
John Stultz | 551dae6 | 2016-04-22 17:12:57 -0700 | [diff] [blame] | 1672 | sk = skb_to_full_sk(skb); |
JP Abgrall | d1fd397 | 2013-02-20 16:38:34 -0800 | [diff] [blame] | 1673 | /* |
| 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1680 | 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 Dubray | ce863f8 | 2017-08-01 18:22:47 +0200 | [diff] [blame] | 1687 | * 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1692 | */ |
Simon Dubray | ce863f8 | 2017-08-01 18:22:47 +0200 | [diff] [blame] | 1693 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1706 | 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 Abgrall | 4bb20aa | 2012-04-17 16:00:07 -0700 | [diff] [blame] | 1713 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1715 | |
Chenbo Feng | 5d534f4 | 2017-04-20 18:54:13 -0700 | [diff] [blame] | 1716 | if (!sk) { |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1717 | /* |
| 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 Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1722 | if (do_tag_stat) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1723 | account_for_uid(skb, sk, 0, par); |
Chenbo Feng | 5d534f4 | 2017-04-20 18:54:13 -0700 | [diff] [blame] | 1724 | MT_DEBUG("qtaguid[%d]: leaving (sk=NULL)\n", par->hooknum); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1725 | 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 Feng | 5d534f4 | 2017-04-20 18:54:13 -0700 | [diff] [blame] | 1732 | sock_uid = sk->sk_uid; |
JP Abgrall | 89d32dc | 2013-12-20 16:51:11 -0800 | [diff] [blame] | 1733 | if (do_tag_stat) |
Chenbo Feng | 5d534f4 | 2017-04-20 18:54:13 -0700 | [diff] [blame] | 1734 | account_for_uid(skb, sk, from_kuid(&init_user_ns, sock_uid), |
| 1735 | par); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1736 | |
| 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1743 | 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 Feng | 5d534f4 | 2017-04-20 18:54:13 -0700 | [diff] [blame] | 1747 | if ((uid_gte(sock_uid, uid_min) && |
| 1748 | uid_lte(sock_uid, uid_max)) ^ |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1749 | !(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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1755 | } |
| 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 Feng | 5d534f4 | 2017-04-20 18:54:13 -0700 | [diff] [blame] | 1759 | 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 Pundir | 070eff8 | 2015-01-20 16:13:08 +0530 | [diff] [blame] | 1774 | if ((gid_gte(filp->f_cred->fsgid, gid_min) && |
| 1775 | gid_lte(filp->f_cred->fsgid, gid_max)) ^ |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1776 | !(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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1782 | } |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1783 | MT_DEBUG("qtaguid[%d]: leaving matched\n", par->hooknum); |
| 1784 | res = true; |
| 1785 | |
| 1786 | put_sock_ret_res: |
| 1787 | if (got_sock) |
Amit Pundir | 2879b6e | 2015-01-29 01:16:23 +0530 | [diff] [blame] | 1788 | sock_gen_put(sk); |
Mohamad Ayyash | e8101fd | 2015-01-13 19:20:44 -0800 | [diff] [blame] | 1789 | if (set_sk_callback_lock) |
| 1790 | read_unlock_bh(&sk->sk_callback_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1791 | ret_res: |
| 1792 | MT_DEBUG("qtaguid[%d]: left %d\n", par->hooknum, res); |
| 1793 | return res; |
| 1794 | } |
| 1795 | |
| 1796 | #ifdef DDEBUG |
Chenbo Feng | 64157f4 | 2017-03-23 13:51:24 -0700 | [diff] [blame] | 1797 | /* |
| 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 | */ |
| 1801 | static void prdebug_full_state_locked(int indent_level, const char *fmt, ...) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1802 | { |
| 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1822 | prdebug_sock_tag_tree(indent_level, &sock_tag_tree); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1823 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1824 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1828 | |
| 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 Feng | 64157f4 | 2017-03-23 13:51:24 -0700 | [diff] [blame] | 1836 | static void prdebug_full_state_locked(int indent_level, const char *fmt, ...) {} |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1837 | #endif |
| 1838 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1839 | struct proc_ctrl_print_info { |
| 1840 | struct sock *sk; /* socket found by reading to sk_pos */ |
| 1841 | loff_t sk_pos; |
| 1842 | }; |
| 1843 | |
| 1844 | static 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 | |
| 1867 | static 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 | |
| 1899 | static void qtaguid_ctrl_proc_stop(struct seq_file *m, void *v) |
| 1900 | { |
| 1901 | spin_unlock_bh(&sock_tag_list_lock); |
| 1902 | } |
| 1903 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1904 | /* |
| 1905 | * Procfs reader to get all active socket tags using style "1)" as described in |
| 1906 | * fs/proc/generic.c |
| 1907 | */ |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1908 | static int qtaguid_ctrl_proc_show(struct seq_file *m, void *v) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1909 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1910 | struct sock_tag *sock_tag_entry = v; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1911 | uid_t uid; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1912 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1913 | CT_DEBUG("qtaguid: proc ctrl pid=%u tgid=%u uid=%u\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1914 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1915 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1916 | if (sock_tag_entry != SEQ_START_TOKEN) { |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 1917 | int sk_ref_count; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1918 | 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 Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 1926 | sk_ref_count = atomic_read( |
| 1927 | &sock_tag_entry->sk->sk_refcnt); |
Mohamad Ayyash | 8613d93 | 2016-05-11 13:18:35 -0700 | [diff] [blame] | 1928 | seq_printf(m, "sock=%pK tag=0x%llx (uid=%u) pid=%u " |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 1929 | "f_count=%d\n", |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1930 | sock_tag_entry->sk, |
| 1931 | sock_tag_entry->tag, uid, |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 1932 | sock_tag_entry->pid, sk_ref_count); |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1933 | } 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 Feng | 5d534f4 | 2017-04-20 18:54:13 -0700 | [diff] [blame] | 1945 | "match_no_sk_gid=%llu\n", |
Sherman Yin | da5ea99 | 2014-06-12 14:35:38 -0700 | [diff] [blame] | 1946 | (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 Feng | 5d534f4 | 2017-04-20 18:54:13 -0700 | [diff] [blame] | 1957 | (u64)atomic64_read(&qtu_events.match_no_sk_gid)); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1958 | |
Chenbo Feng | 64157f4 | 2017-03-23 13:51:24 -0700 | [diff] [blame] | 1959 | /* 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1966 | return 0; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | /* |
| 1970 | * Delete socket tags, and stat tags associated with a given |
| 1971 | * accouting tag and uid. |
| 1972 | */ |
| 1973 | static int ctrl_cmd_delete(const char *input) |
| 1974 | { |
| 1975 | char cmd; |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1976 | int uid_int; |
| 1977 | kuid_t uid; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1978 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1991 | argc = sscanf(input, "%c %llu %u", &cmd, &acct_tag, &uid_int); |
| 1992 | uid = make_kuid(&init_user_ns, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1993 | CT_DEBUG("qtaguid: ctrl_delete(%s): argc=%d cmd=%c " |
| 1994 | "user_tag=0x%llx uid=%u\n", input, argc, cmd, |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1995 | acct_tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1996 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2007 | uid_int = from_kuid(&init_user_ns, uid); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2008 | } 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2011 | input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2012 | res = -EPERM; |
| 2013 | goto err; |
| 2014 | } |
| 2015 | |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2016 | tag = combine_atag_with_uid(acct_tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2017 | CT_DEBUG("qtaguid: ctrl_delete(%s): " |
| 2018 | "looking for tag=0x%llx (uid=%u)\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2019 | input, tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2020 | |
| 2021 | /* Delete socket tags */ |
| 2022 | spin_lock_bh(&sock_tag_list_lock); |
Chenbo Feng | 20c8a00 | 2017-11-28 18:22:11 -0800 | [diff] [blame] | 2023 | spin_lock_bh(&uid_tag_data_tree_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2024 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2029 | if (entry_uid != uid_int) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2030 | 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 Feng | 20c8a00 | 2017-11-28 18:22:11 -0800 | [diff] [blame] | 2053 | spin_unlock_bh(&uid_tag_data_tree_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2054 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2091 | if (entry_uid != uid_int) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2092 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2120 | if (entry_uid != uid_int) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2121 | 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 | |
| 2134 | err: |
| 2135 | return res; |
| 2136 | } |
| 2137 | |
| 2138 | static 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2164 | input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2165 | 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 | |
| 2193 | err: |
| 2194 | return res; |
| 2195 | } |
| 2196 | |
| 2197 | static int ctrl_cmd_tag(const char *input) |
| 2198 | { |
| 2199 | char cmd; |
| 2200 | int sock_fd = 0; |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2201 | kuid_t uid; |
| 2202 | unsigned int uid_int = 0; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2203 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2213 | argc = sscanf(input, "%c %d %llu %u", &cmd, &sock_fd, &acct_tag, &uid_int); |
| 2214 | uid = make_kuid(&init_user_ns, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2215 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2217 | acct_tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2218 | 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 2225 | " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n", |
| 2226 | input, sock_fd, res, current->pid, current->tgid, |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2227 | from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2228 | goto err; |
| 2229 | } |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2230 | CT_DEBUG("qtaguid: ctrl_tag(%s): socket->...->sk_refcnt=%d ->sk=%p\n", |
| 2231 | input, atomic_read(&el_socket->sk->sk_refcnt), |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2232 | 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 Abgrall | 90414bc | 2013-01-04 18:18:36 -0800 | [diff] [blame] | 2242 | "ctrl.gid=%u in_group()=%d in_egroup()=%d\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2243 | 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 Abgrall | 90414bc | 2013-01-04 18:18:36 -0800 | [diff] [blame] | 2248 | in_group_p(xt_qtaguid_ctrl_file->gid), |
| 2249 | in_egroup_p(xt_qtaguid_ctrl_file->gid)); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2250 | if (argc < 4) { |
| 2251 | uid = current_fsuid(); |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2252 | uid_int = from_kuid(&init_user_ns, uid); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2253 | } 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2256 | input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2257 | res = -EPERM; |
| 2258 | goto err_put; |
| 2259 | } |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2260 | full_tag = combine_atag_with_uid(acct_tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2261 | |
| 2262 | spin_lock_bh(&sock_tag_list_lock); |
Chenbo Feng | 20c8a00 | 2017-11-28 18:22:11 -0800 | [diff] [blame] | 2263 | spin_lock_bh(&uid_tag_data_tree_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2264 | 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 Feng | 20c8a00 | 2017-11-28 18:22:11 -0800 | [diff] [blame] | 2268 | spin_unlock_bh(&uid_tag_data_tree_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2269 | 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 Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2277 | "st@%p ...->sk_refcnt=%d\n", |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2278 | input, el_socket->sk, sock_tag_entry, |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2279 | atomic_read(&el_socket->sk->sk_refcnt)); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2280 | 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 Feng | 20c8a00 | 2017-11-28 18:22:11 -0800 | [diff] [blame] | 2295 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2300 | spin_unlock_bh(&sock_tag_list_lock); |
| 2301 | res = -ENOMEM; |
Chenbo Feng | 20c8a00 | 2017-11-28 18:22:11 -0800 | [diff] [blame] | 2302 | goto err_put; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2303 | } |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2304 | /* |
| 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2309 | sock_tag_entry->sk = el_socket->sk; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2310 | sock_tag_entry->pid = current->tgid; |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2311 | sock_tag_entry->tag = combine_atag_with_uid(acct_tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2312 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2325 | from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2326 | else |
| 2327 | list_add(&sock_tag_entry->list, |
| 2328 | &pqd_entry->sock_tag_list); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2329 | |
| 2330 | sock_tag_tree_insert(sock_tag_entry, &sock_tag_tree); |
| 2331 | atomic64_inc(&qtu_events.sockets_tagged); |
| 2332 | } |
Chenbo Feng | 20c8a00 | 2017-11-28 18:22:11 -0800 | [diff] [blame] | 2333 | spin_unlock_bh(&uid_tag_data_tree_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2334 | spin_unlock_bh(&sock_tag_list_lock); |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2335 | /* 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2337 | input, sock_tag_entry, |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2338 | atomic_read(&el_socket->sk->sk_refcnt)); |
| 2339 | sockfd_put(el_socket); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2340 | return 0; |
| 2341 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2342 | err_put: |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2343 | CT_DEBUG("qtaguid: ctrl_tag(%s): done. ...->sk_refcnt=%d\n", |
| 2344 | input, atomic_read(&el_socket->sk->sk_refcnt) - 1); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2345 | /* Release the sock_fd that was grabbed by sockfd_lookup(). */ |
| 2346 | sockfd_put(el_socket); |
| 2347 | return res; |
| 2348 | |
| 2349 | err: |
| 2350 | CT_DEBUG("qtaguid: ctrl_tag(%s): done.\n", input); |
| 2351 | return res; |
| 2352 | } |
| 2353 | |
| 2354 | static 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2360 | |
| 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 Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2366 | return res; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2367 | } |
| 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 Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 2371 | " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n", |
| 2372 | input, sock_fd, res, current->pid, current->tgid, |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2373 | from_kuid(&init_user_ns, current_fsuid())); |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2374 | return res; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2375 | } |
| 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 Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2379 | res = qtaguid_untag(el_socket, false); |
| 2380 | sockfd_put(el_socket); |
| 2381 | return res; |
| 2382 | } |
| 2383 | |
| 2384 | int 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2393 | 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 Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2398 | return res; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2399 | } |
| 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 Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2410 | if (kernel) |
| 2411 | pid = sock_tag_entry->pid; |
| 2412 | else |
| 2413 | pid = current->tgid; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2414 | pqd_entry = proc_qtu_data_tree_search( |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2415 | &proc_qtu_data_tree, pid); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2416 | /* |
| 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 Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2421 | if (IS_ERR_OR_NULL(pqd_entry) || !sock_tag_entry->list.next) { |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2422 | pr_warn_once("qtaguid: %s(): " |
| 2423 | "User space forgot to open /dev/xt_qtaguid? " |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2424 | "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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2428 | list_del(&sock_tag_entry->list); |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2429 | } |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2430 | 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 Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2438 | * Release the sock_fd that was grabbed at tag time. |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2439 | */ |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2440 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2444 | |
| 2445 | kfree(sock_tag_entry); |
| 2446 | atomic64_inc(&qtu_events.sockets_untagged); |
| 2447 | |
| 2448 | return 0; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 2451 | static ssize_t qtaguid_ctrl_parse(const char *input, size_t count) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2452 | { |
| 2453 | char cmd; |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 2454 | ssize_t res; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2455 | |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 2456 | CT_DEBUG("qtaguid: ctrl(%s): pid=%u tgid=%u uid=%u\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2457 | input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 2458 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2459 | 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; |
| 2484 | err: |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 2485 | CT_DEBUG("qtaguid: ctrl(%s): res=%zd\n", input, res); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2486 | return res; |
| 2487 | } |
| 2488 | |
| 2489 | #define MAX_QTAGUID_CTRL_INPUT_LEN 255 |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 2490 | static ssize_t qtaguid_ctrl_proc_write(struct file *file, const char __user *buffer, |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2491 | size_t count, loff_t *offp) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2492 | { |
| 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 | |
| 2508 | struct proc_print_info { |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2509 | struct iface_stat *iface_entry; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2510 | int item_index; |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2511 | tag_t tag; /* tag found by reading to tag_pos */ |
| 2512 | off_t tag_pos; |
| 2513 | int tag_item_index; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2514 | }; |
| 2515 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2516 | static void pp_stats_header(struct seq_file *m) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2517 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2518 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2528 | } |
| 2529 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2530 | static int pp_stats_line(struct seq_file *m, struct tag_stat *ts_entry, |
| 2531 | int cnt_set) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2532 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2533 | 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 Ayyash | 8613d93 | 2016-05-11 13:18:35 -0700 | [diff] [blame] | 2538 | if (!can_read_other_uid_stats(make_kuid(&init_user_ns,stat_uid))) { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2539 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2544 | 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åg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2546 | return 0; |
| 2547 | } |
| 2548 | ppi->item_index++; |
| 2549 | cnts = &ts_entry->counters; |
Amit Pundir | 5b5ab94 | 2015-10-01 10:44:36 +0530 | [diff] [blame] | 2550 | seq_printf(m, "%d %s 0x%llx %u %u " |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2551 | "%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 Pundir | 5b5ab94 | 2015-10-01 10:44:36 +0530 | [diff] [blame] | 2580 | return seq_has_overflowed(m) ? -ENOSPC : 1; |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | static bool pp_sets(struct seq_file *m, struct tag_stat *ts_entry) |
| 2584 | { |
| 2585 | int ret; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2586 | int counter_set; |
| 2587 | for (counter_set = 0; counter_set < IFS_MAX_COUNTER_SETS; |
| 2588 | counter_set++) { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2589 | ret = pp_stats_line(m, ts_entry, counter_set); |
| 2590 | if (ret < 0) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2591 | return false; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2592 | } |
| 2593 | return true; |
| 2594 | } |
| 2595 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2596 | static 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 | |
| 2609 | static 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 | |
| 2619 | static 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 | |
| 2654 | static 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 | |
| 2709 | static 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2717 | /* |
| 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åg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2722 | static int qtaguid_stats_proc_show(struct seq_file *m, void *v) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2723 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2724 | struct tag_stat *ts_entry = v; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2725 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2726 | if (v == SEQ_START_TOKEN) |
| 2727 | pp_stats_header(m); |
| 2728 | else |
| 2729 | pp_sets(m, ts_entry); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2730 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2731 | return 0; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | /*------------------------------------------*/ |
| 2735 | static 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2747 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2748 | |
| 2749 | spin_lock_bh(&uid_tag_data_tree_lock); |
| 2750 | |
| 2751 | /* Look for existing uid data, or alloc one. */ |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2752 | utd_entry = get_uid_data(from_kuid(&init_user_ns, current_fsuid()), &utd_entry_found); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2753 | if (IS_ERR_OR_NULL(utd_entry)) { |
| 2754 | res = PTR_ERR(utd_entry); |
JP Abgrall | b79c36f1 | 2012-10-09 20:38:21 -0700 | [diff] [blame] | 2755 | goto err_unlock; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2756 | } |
| 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2764 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()), |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2765 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2774 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2775 | 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 Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2788 | from_kuid(&init_user_ns, current_fsuid()), new_pqd_entry); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2789 | file->private_data = new_pqd_entry; |
| 2790 | return 0; |
| 2791 | |
| 2792 | err_unlock_free_utd: |
| 2793 | if (!utd_entry_found) { |
| 2794 | rb_erase(&utd_entry->node, &uid_tag_data_tree); |
| 2795 | kfree(utd_entry); |
| 2796 | } |
JP Abgrall | b79c36f1 | 2012-10-09 20:38:21 -0700 | [diff] [blame] | 2797 | err_unlock: |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2798 | spin_unlock_bh(&uid_tag_data_tree_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2799 | return res; |
| 2800 | } |
| 2801 | |
| 2802 | static 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 Feng | 64157f4 | 2017-03-23 13:51:24 -0700 | [diff] [blame] | 2877 | spin_lock_bh(&sock_tag_list_lock); |
| 2878 | prdebug_full_state_locked(0, "%s(): pid=%u tgid=%u", __func__, |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2879 | current->pid, current->tgid); |
Chenbo Feng | 64157f4 | 2017-03-23 13:51:24 -0700 | [diff] [blame] | 2880 | spin_unlock_bh(&sock_tag_list_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2881 | return 0; |
| 2882 | } |
| 2883 | |
| 2884 | /*------------------------------------------*/ |
| 2885 | static const struct file_operations qtudev_fops = { |
| 2886 | .owner = THIS_MODULE, |
| 2887 | .open = qtudev_open, |
| 2888 | .release = qtudev_release, |
| 2889 | }; |
| 2890 | |
| 2891 | static 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åg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2898 | static 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 | |
| 2905 | static 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 | |
| 2911 | static 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 Hackmann | 5647291 | 2013-12-04 17:39:27 -0800 | [diff] [blame] | 2916 | .release = seq_release_private, |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2917 | }; |
| 2918 | |
| 2919 | static 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 | |
| 2926 | static 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 | |
| 2932 | static 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2939 | /*------------------------------------------*/ |
| 2940 | static 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åg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2950 | xt_qtaguid_ctrl_file = proc_create_data("ctrl", proc_ctrl_perms, |
| 2951 | *res_procdir, |
| 2952 | &proc_qtaguid_ctrl_fops, |
| 2953 | NULL); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2954 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2960 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2961 | xt_qtaguid_stats_file = proc_create_data("stats", proc_stats_perms, |
| 2962 | *res_procdir, |
| 2963 | &proc_qtaguid_stats_fops, |
| 2964 | NULL); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2965 | 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 Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2971 | /* |
| 2972 | * TODO: add support counter hacking |
| 2973 | * xt_qtaguid_stats_file->write_proc = qtaguid_stats_proc_write; |
| 2974 | */ |
| 2975 | return 0; |
| 2976 | |
| 2977 | no_stats_entry: |
| 2978 | remove_proc_entry("ctrl", *res_procdir); |
| 2979 | no_ctrl_entry: |
| 2980 | remove_proc_entry("xt_qtaguid", NULL); |
| 2981 | no_dir: |
| 2982 | return ret; |
| 2983 | } |
| 2984 | |
| 2985 | static 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 | |
| 2998 | static 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 | |
| 3014 | module_init(qtaguid_mt_init); |
| 3015 | MODULE_AUTHOR("jpa <jpa@google.com>"); |
| 3016 | MODULE_DESCRIPTION("Xtables: socket owner+tag matching and associated stats"); |
| 3017 | MODULE_LICENSE("GPL"); |
| 3018 | MODULE_ALIAS("ipt_owner"); |
| 3019 | MODULE_ALIAS("ip6t_owner"); |
| 3020 | MODULE_ALIAS("ipt_qtaguid"); |
| 3021 | MODULE_ALIAS("ip6t_qtaguid"); |