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); |
| 522 | spin_lock_bh(&uid_tag_data_tree_lock); |
| 523 | tr_entry = lookup_tag_ref(full_tag, &utd_entry); |
| 524 | BUG_ON(IS_ERR_OR_NULL(utd_entry)); |
| 525 | if (!tr_entry) |
| 526 | tr_entry = new_tag_ref(full_tag, utd_entry); |
| 527 | |
| 528 | spin_unlock_bh(&uid_tag_data_tree_lock); |
| 529 | if (utd_res) |
| 530 | *utd_res = utd_entry; |
| 531 | DR_DEBUG("qtaguid: get_tag_ref(0x%llx) utd=%p tr=%p\n", |
| 532 | full_tag, utd_entry, tr_entry); |
| 533 | return tr_entry; |
| 534 | } |
| 535 | |
| 536 | /* Checks and maybe frees the UID Tag Data entry */ |
| 537 | static void put_utd_entry(struct uid_tag_data *utd_entry) |
| 538 | { |
| 539 | /* Are we done with the UID tag data entry? */ |
| 540 | if (RB_EMPTY_ROOT(&utd_entry->tag_ref_tree) && |
| 541 | !utd_entry->num_pqd) { |
| 542 | DR_DEBUG("qtaguid: %s(): " |
| 543 | "erase utd_entry=%p uid=%u " |
| 544 | "by pid=%u tgid=%u uid=%u\n", __func__, |
| 545 | utd_entry, utd_entry->uid, |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 546 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 547 | BUG_ON(utd_entry->num_active_tags); |
| 548 | rb_erase(&utd_entry->node, &uid_tag_data_tree); |
| 549 | kfree(utd_entry); |
| 550 | } else { |
| 551 | DR_DEBUG("qtaguid: %s(): " |
| 552 | "utd_entry=%p still has %d tags %d proc_qtu_data\n", |
| 553 | __func__, utd_entry, utd_entry->num_active_tags, |
| 554 | utd_entry->num_pqd); |
| 555 | BUG_ON(!(utd_entry->num_active_tags || |
| 556 | utd_entry->num_pqd)); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * If no sock_tags are using this tag_ref, |
| 562 | * decrements refcount of utd_entry, removes tr_entry |
| 563 | * from utd_entry->tag_ref_tree and frees. |
| 564 | */ |
| 565 | static void free_tag_ref_from_utd_entry(struct tag_ref *tr_entry, |
| 566 | struct uid_tag_data *utd_entry) |
| 567 | { |
| 568 | DR_DEBUG("qtaguid: %s(): %p tag=0x%llx (uid=%u)\n", __func__, |
| 569 | tr_entry, tr_entry->tn.tag, |
| 570 | get_uid_from_tag(tr_entry->tn.tag)); |
| 571 | if (!tr_entry->num_sock_tags) { |
| 572 | BUG_ON(!utd_entry->num_active_tags); |
| 573 | utd_entry->num_active_tags--; |
| 574 | rb_erase(&tr_entry->tn.node, &utd_entry->tag_ref_tree); |
| 575 | DR_DEBUG("qtaguid: %s(): erased %p\n", __func__, tr_entry); |
| 576 | kfree(tr_entry); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | static void put_tag_ref_tree(tag_t full_tag, struct uid_tag_data *utd_entry) |
| 581 | { |
| 582 | struct rb_node *node; |
| 583 | struct tag_ref *tr_entry; |
| 584 | tag_t acct_tag; |
| 585 | |
| 586 | DR_DEBUG("qtaguid: %s(tag=0x%llx (uid=%u))\n", __func__, |
| 587 | full_tag, get_uid_from_tag(full_tag)); |
| 588 | acct_tag = get_atag_from_tag(full_tag); |
| 589 | node = rb_first(&utd_entry->tag_ref_tree); |
| 590 | while (node) { |
| 591 | tr_entry = rb_entry(node, struct tag_ref, tn.node); |
| 592 | node = rb_next(node); |
| 593 | if (!acct_tag || tr_entry->tn.tag == full_tag) |
| 594 | free_tag_ref_from_utd_entry(tr_entry, utd_entry); |
| 595 | } |
| 596 | } |
| 597 | |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 598 | 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] | 599 | size_t size, loff_t *ppos) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 600 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 601 | uint64_t *valuep = PDE_DATA(file_inode(file)); |
| 602 | char tmp[24]; |
| 603 | size_t tmp_size; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 604 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 605 | tmp_size = scnprintf(tmp, sizeof(tmp), "%llu\n", *valuep); |
| 606 | return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 609 | 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] | 610 | size_t size, loff_t *ppos) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 611 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 612 | bool *valuep = PDE_DATA(file_inode(file)); |
| 613 | char tmp[24]; |
| 614 | size_t tmp_size; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 615 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 616 | tmp_size = scnprintf(tmp, sizeof(tmp), "%u\n", *valuep); |
| 617 | return simple_read_from_buffer(buf, size, ppos, tmp, tmp_size); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static int get_active_counter_set(tag_t tag) |
| 621 | { |
| 622 | int active_set = 0; |
| 623 | struct tag_counter_set *tcs; |
| 624 | |
| 625 | MT_DEBUG("qtaguid: get_active_counter_set(tag=0x%llx)" |
| 626 | " (uid=%u)\n", |
| 627 | tag, get_uid_from_tag(tag)); |
| 628 | /* For now we only handle UID tags for active sets */ |
| 629 | tag = get_utag_from_tag(tag); |
| 630 | spin_lock_bh(&tag_counter_set_list_lock); |
| 631 | tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag); |
| 632 | if (tcs) |
| 633 | active_set = tcs->active_set; |
| 634 | spin_unlock_bh(&tag_counter_set_list_lock); |
| 635 | return active_set; |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Find the entry for tracking the specified interface. |
| 640 | * Caller must hold iface_stat_list_lock |
| 641 | */ |
| 642 | static struct iface_stat *get_iface_entry(const char *ifname) |
| 643 | { |
| 644 | struct iface_stat *iface_entry; |
| 645 | |
| 646 | /* Find the entry for tracking the specified tag within the interface */ |
| 647 | if (ifname == NULL) { |
| 648 | pr_info("qtaguid: iface_stat: get() NULL device name\n"); |
| 649 | return NULL; |
| 650 | } |
| 651 | |
| 652 | /* Iterate over interfaces */ |
| 653 | list_for_each_entry(iface_entry, &iface_stat_list, list) { |
| 654 | if (!strcmp(ifname, iface_entry->ifname)) |
| 655 | goto done; |
| 656 | } |
| 657 | iface_entry = NULL; |
| 658 | done: |
| 659 | return iface_entry; |
| 660 | } |
| 661 | |
JP Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 662 | /* This is for fmt2 only */ |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 663 | static void pp_iface_stat_header(struct seq_file *m) |
JP Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 664 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 665 | seq_puts(m, |
| 666 | "ifname " |
| 667 | "total_skb_rx_bytes total_skb_rx_packets " |
| 668 | "total_skb_tx_bytes total_skb_tx_packets " |
| 669 | "rx_tcp_bytes rx_tcp_packets " |
| 670 | "rx_udp_bytes rx_udp_packets " |
| 671 | "rx_other_bytes rx_other_packets " |
| 672 | "tx_tcp_bytes tx_tcp_packets " |
| 673 | "tx_udp_bytes tx_udp_packets " |
| 674 | "tx_other_bytes tx_other_packets\n" |
| 675 | ); |
JP Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 678 | static void pp_iface_stat_line(struct seq_file *m, |
| 679 | struct iface_stat *iface_entry) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 680 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 681 | struct data_counters *cnts; |
| 682 | int cnt_set = 0; /* We only use one set for the device */ |
| 683 | cnts = &iface_entry->totals_via_skb; |
| 684 | seq_printf(m, "%s %llu %llu %llu %llu %llu %llu %llu %llu " |
| 685 | "%llu %llu %llu %llu %llu %llu %llu %llu\n", |
| 686 | iface_entry->ifname, |
| 687 | dc_sum_bytes(cnts, cnt_set, IFS_RX), |
| 688 | dc_sum_packets(cnts, cnt_set, IFS_RX), |
| 689 | dc_sum_bytes(cnts, cnt_set, IFS_TX), |
| 690 | dc_sum_packets(cnts, cnt_set, IFS_TX), |
| 691 | cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes, |
| 692 | cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets, |
| 693 | cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes, |
| 694 | cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets, |
| 695 | cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes, |
| 696 | cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets, |
| 697 | cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes, |
| 698 | cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets, |
| 699 | cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes, |
| 700 | cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets, |
| 701 | cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes, |
| 702 | cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets); |
| 703 | } |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 704 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 705 | struct proc_iface_stat_fmt_info { |
| 706 | int fmt; |
| 707 | }; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 708 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 709 | static void *iface_stat_fmt_proc_start(struct seq_file *m, loff_t *pos) |
| 710 | { |
| 711 | struct proc_iface_stat_fmt_info *p = m->private; |
| 712 | loff_t n = *pos; |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 713 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 714 | /* |
| 715 | * This lock will prevent iface_stat_update() from changing active, |
| 716 | * and in turn prevent an interface from unregistering itself. |
| 717 | */ |
| 718 | spin_lock_bh(&iface_stat_list_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 719 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 720 | if (unlikely(module_passive)) |
| 721 | return NULL; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 722 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 723 | if (!n && p->fmt == 2) |
| 724 | pp_iface_stat_header(m); |
| 725 | |
| 726 | return seq_list_start(&iface_stat_list, n); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 729 | static void *iface_stat_fmt_proc_next(struct seq_file *m, void *p, loff_t *pos) |
| 730 | { |
| 731 | return seq_list_next(p, &iface_stat_list, pos); |
| 732 | } |
| 733 | |
| 734 | static void iface_stat_fmt_proc_stop(struct seq_file *m, void *p) |
| 735 | { |
| 736 | spin_unlock_bh(&iface_stat_list_lock); |
| 737 | } |
| 738 | |
| 739 | static int iface_stat_fmt_proc_show(struct seq_file *m, void *v) |
| 740 | { |
| 741 | struct proc_iface_stat_fmt_info *p = m->private; |
| 742 | struct iface_stat *iface_entry; |
| 743 | struct rtnl_link_stats64 dev_stats, *stats; |
| 744 | struct rtnl_link_stats64 no_dev_stats = {0}; |
| 745 | |
| 746 | |
| 747 | CT_DEBUG("qtaguid:proc iface_stat_fmt pid=%u tgid=%u uid=%u\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 748 | 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] | 749 | |
| 750 | iface_entry = list_entry(v, struct iface_stat, list); |
| 751 | |
| 752 | if (iface_entry->active) { |
| 753 | stats = dev_get_stats(iface_entry->net_dev, |
| 754 | &dev_stats); |
| 755 | } else { |
| 756 | stats = &no_dev_stats; |
| 757 | } |
| 758 | /* |
| 759 | * If the meaning of the data changes, then update the fmtX |
| 760 | * string. |
| 761 | */ |
| 762 | if (p->fmt == 1) { |
| 763 | seq_printf(m, "%s %d %llu %llu %llu %llu %llu %llu %llu %llu\n", |
| 764 | iface_entry->ifname, |
| 765 | iface_entry->active, |
| 766 | iface_entry->totals_via_dev[IFS_RX].bytes, |
| 767 | iface_entry->totals_via_dev[IFS_RX].packets, |
| 768 | iface_entry->totals_via_dev[IFS_TX].bytes, |
| 769 | iface_entry->totals_via_dev[IFS_TX].packets, |
| 770 | stats->rx_bytes, stats->rx_packets, |
| 771 | stats->tx_bytes, stats->tx_packets |
| 772 | ); |
| 773 | } else { |
| 774 | pp_iface_stat_line(m, iface_entry); |
| 775 | } |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | static const struct file_operations read_u64_fops = { |
| 780 | .read = read_proc_u64, |
| 781 | .llseek = default_llseek, |
| 782 | }; |
| 783 | |
| 784 | static const struct file_operations read_bool_fops = { |
| 785 | .read = read_proc_bool, |
| 786 | .llseek = default_llseek, |
| 787 | }; |
| 788 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 789 | static void iface_create_proc_worker(struct work_struct *work) |
| 790 | { |
| 791 | struct proc_dir_entry *proc_entry; |
| 792 | struct iface_stat_work *isw = container_of(work, struct iface_stat_work, |
| 793 | iface_work); |
| 794 | struct iface_stat *new_iface = isw->iface_entry; |
| 795 | |
| 796 | /* iface_entries are not deleted, so safe to manipulate. */ |
| 797 | proc_entry = proc_mkdir(new_iface->ifname, iface_stat_procdir); |
| 798 | if (IS_ERR_OR_NULL(proc_entry)) { |
| 799 | pr_err("qtaguid: iface_stat: create_proc(): alloc failed.\n"); |
| 800 | kfree(isw); |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | new_iface->proc_ptr = proc_entry; |
| 805 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 806 | proc_create_data("tx_bytes", proc_iface_perms, proc_entry, |
| 807 | &read_u64_fops, |
| 808 | &new_iface->totals_via_dev[IFS_TX].bytes); |
| 809 | proc_create_data("rx_bytes", proc_iface_perms, proc_entry, |
| 810 | &read_u64_fops, |
| 811 | &new_iface->totals_via_dev[IFS_RX].bytes); |
| 812 | proc_create_data("tx_packets", proc_iface_perms, proc_entry, |
| 813 | &read_u64_fops, |
| 814 | &new_iface->totals_via_dev[IFS_TX].packets); |
| 815 | proc_create_data("rx_packets", proc_iface_perms, proc_entry, |
| 816 | &read_u64_fops, |
| 817 | &new_iface->totals_via_dev[IFS_RX].packets); |
| 818 | proc_create_data("active", proc_iface_perms, proc_entry, |
| 819 | &read_bool_fops, &new_iface->active); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 820 | |
| 821 | IF_DEBUG("qtaguid: iface_stat: create_proc(): done " |
| 822 | "entry=%p dev=%s\n", new_iface, new_iface->ifname); |
| 823 | kfree(isw); |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Will set the entry's active state, and |
| 828 | * update the net_dev accordingly also. |
| 829 | */ |
| 830 | static void _iface_stat_set_active(struct iface_stat *entry, |
| 831 | struct net_device *net_dev, |
| 832 | bool activate) |
| 833 | { |
| 834 | if (activate) { |
| 835 | entry->net_dev = net_dev; |
| 836 | entry->active = true; |
| 837 | IF_DEBUG("qtaguid: %s(%s): " |
| 838 | "enable tracking. rfcnt=%d\n", __func__, |
| 839 | entry->ifname, |
| 840 | __this_cpu_read(*net_dev->pcpu_refcnt)); |
| 841 | } else { |
| 842 | entry->active = false; |
| 843 | entry->net_dev = NULL; |
| 844 | IF_DEBUG("qtaguid: %s(%s): " |
| 845 | "disable tracking. rfcnt=%d\n", __func__, |
| 846 | entry->ifname, |
| 847 | __this_cpu_read(*net_dev->pcpu_refcnt)); |
| 848 | |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | /* Caller must hold iface_stat_list_lock */ |
| 853 | static struct iface_stat *iface_alloc(struct net_device *net_dev) |
| 854 | { |
| 855 | struct iface_stat *new_iface; |
| 856 | struct iface_stat_work *isw; |
| 857 | |
| 858 | new_iface = kzalloc(sizeof(*new_iface), GFP_ATOMIC); |
| 859 | if (new_iface == NULL) { |
| 860 | pr_err("qtaguid: iface_stat: create(%s): " |
| 861 | "iface_stat alloc failed\n", net_dev->name); |
| 862 | return NULL; |
| 863 | } |
| 864 | new_iface->ifname = kstrdup(net_dev->name, GFP_ATOMIC); |
| 865 | if (new_iface->ifname == NULL) { |
| 866 | pr_err("qtaguid: iface_stat: create(%s): " |
| 867 | "ifname alloc failed\n", net_dev->name); |
| 868 | kfree(new_iface); |
| 869 | return NULL; |
| 870 | } |
| 871 | spin_lock_init(&new_iface->tag_stat_list_lock); |
| 872 | new_iface->tag_stat_tree = RB_ROOT; |
| 873 | _iface_stat_set_active(new_iface, net_dev, true); |
| 874 | |
| 875 | /* |
| 876 | * ipv6 notifier chains are atomic :( |
| 877 | * No create_proc_read_entry() for you! |
| 878 | */ |
| 879 | isw = kmalloc(sizeof(*isw), GFP_ATOMIC); |
| 880 | if (!isw) { |
| 881 | pr_err("qtaguid: iface_stat: create(%s): " |
| 882 | "work alloc failed\n", new_iface->ifname); |
| 883 | _iface_stat_set_active(new_iface, net_dev, false); |
| 884 | kfree(new_iface->ifname); |
| 885 | kfree(new_iface); |
| 886 | return NULL; |
| 887 | } |
| 888 | isw->iface_entry = new_iface; |
| 889 | INIT_WORK(&isw->iface_work, iface_create_proc_worker); |
| 890 | schedule_work(&isw->iface_work); |
| 891 | list_add(&new_iface->list, &iface_stat_list); |
| 892 | return new_iface; |
| 893 | } |
| 894 | |
| 895 | static void iface_check_stats_reset_and_adjust(struct net_device *net_dev, |
| 896 | struct iface_stat *iface) |
| 897 | { |
| 898 | struct rtnl_link_stats64 dev_stats, *stats; |
| 899 | bool stats_rewound; |
| 900 | |
| 901 | stats = dev_get_stats(net_dev, &dev_stats); |
| 902 | /* No empty packets */ |
| 903 | stats_rewound = |
| 904 | (stats->rx_bytes < iface->last_known[IFS_RX].bytes) |
| 905 | || (stats->tx_bytes < iface->last_known[IFS_TX].bytes); |
| 906 | |
| 907 | IF_DEBUG("qtaguid: %s(%s): iface=%p netdev=%p " |
| 908 | "bytes rx/tx=%llu/%llu " |
| 909 | "active=%d last_known=%d " |
| 910 | "stats_rewound=%d\n", __func__, |
| 911 | net_dev ? net_dev->name : "?", |
| 912 | iface, net_dev, |
| 913 | stats->rx_bytes, stats->tx_bytes, |
| 914 | iface->active, iface->last_known_valid, stats_rewound); |
| 915 | |
| 916 | if (iface->active && iface->last_known_valid && stats_rewound) { |
| 917 | pr_warn_once("qtaguid: iface_stat: %s(%s): " |
| 918 | "iface reset its stats unexpectedly\n", __func__, |
| 919 | net_dev->name); |
| 920 | |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 921 | iface->totals_via_dev[IFS_TX].bytes += |
| 922 | iface->last_known[IFS_TX].bytes; |
| 923 | iface->totals_via_dev[IFS_TX].packets += |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 924 | iface->last_known[IFS_TX].packets; |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 925 | iface->totals_via_dev[IFS_RX].bytes += |
| 926 | iface->last_known[IFS_RX].bytes; |
| 927 | iface->totals_via_dev[IFS_RX].packets += |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 928 | iface->last_known[IFS_RX].packets; |
| 929 | iface->last_known_valid = false; |
| 930 | IF_DEBUG("qtaguid: %s(%s): iface=%p " |
| 931 | "used last known bytes rx/tx=%llu/%llu\n", __func__, |
| 932 | iface->ifname, iface, iface->last_known[IFS_RX].bytes, |
| 933 | iface->last_known[IFS_TX].bytes); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | /* |
| 938 | * Create a new entry for tracking the specified interface. |
| 939 | * Do nothing if the entry already exists. |
| 940 | * Called when an interface is configured with a valid IP address. |
| 941 | */ |
| 942 | static void iface_stat_create(struct net_device *net_dev, |
| 943 | struct in_ifaddr *ifa) |
| 944 | { |
| 945 | struct in_device *in_dev = NULL; |
| 946 | const char *ifname; |
| 947 | struct iface_stat *entry; |
| 948 | __be32 ipaddr = 0; |
| 949 | struct iface_stat *new_iface; |
| 950 | |
| 951 | IF_DEBUG("qtaguid: iface_stat: create(%s): ifa=%p netdev=%p\n", |
| 952 | net_dev ? net_dev->name : "?", |
| 953 | ifa, net_dev); |
| 954 | if (!net_dev) { |
| 955 | pr_err("qtaguid: iface_stat: create(): no net dev\n"); |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | ifname = net_dev->name; |
| 960 | if (!ifa) { |
| 961 | in_dev = in_dev_get(net_dev); |
| 962 | if (!in_dev) { |
| 963 | pr_err("qtaguid: iface_stat: create(%s): no inet dev\n", |
| 964 | ifname); |
| 965 | return; |
| 966 | } |
| 967 | IF_DEBUG("qtaguid: iface_stat: create(%s): in_dev=%p\n", |
| 968 | ifname, in_dev); |
| 969 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { |
| 970 | IF_DEBUG("qtaguid: iface_stat: create(%s): " |
| 971 | "ifa=%p ifa_label=%s\n", |
Greg Hackmann | 3729385 | 2017-04-24 16:16:15 -0700 | [diff] [blame] | 972 | ifname, ifa, ifa->ifa_label); |
| 973 | if (!strcmp(ifname, ifa->ifa_label)) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 974 | break; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | if (!ifa) { |
| 979 | IF_DEBUG("qtaguid: iface_stat: create(%s): no matching IP\n", |
| 980 | ifname); |
| 981 | goto done_put; |
| 982 | } |
| 983 | ipaddr = ifa->ifa_local; |
| 984 | |
| 985 | spin_lock_bh(&iface_stat_list_lock); |
| 986 | entry = get_iface_entry(ifname); |
| 987 | if (entry != NULL) { |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 988 | IF_DEBUG("qtaguid: iface_stat: create(%s): entry=%p\n", |
| 989 | ifname, entry); |
| 990 | iface_check_stats_reset_and_adjust(net_dev, entry); |
JP Abgrall | cf47f36 | 2013-02-06 17:40:07 -0800 | [diff] [blame] | 991 | _iface_stat_set_active(entry, net_dev, true); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 992 | IF_DEBUG("qtaguid: %s(%s): " |
| 993 | "tracking now %d on ip=%pI4\n", __func__, |
JP Abgrall | cf47f36 | 2013-02-06 17:40:07 -0800 | [diff] [blame] | 994 | entry->ifname, true, &ipaddr); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 995 | goto done_unlock_put; |
| 996 | } |
| 997 | |
| 998 | new_iface = iface_alloc(net_dev); |
| 999 | IF_DEBUG("qtaguid: iface_stat: create(%s): done " |
| 1000 | "entry=%p ip=%pI4\n", ifname, new_iface, &ipaddr); |
| 1001 | done_unlock_put: |
| 1002 | spin_unlock_bh(&iface_stat_list_lock); |
| 1003 | done_put: |
| 1004 | if (in_dev) |
| 1005 | in_dev_put(in_dev); |
| 1006 | } |
| 1007 | |
| 1008 | static void iface_stat_create_ipv6(struct net_device *net_dev, |
| 1009 | struct inet6_ifaddr *ifa) |
| 1010 | { |
| 1011 | struct in_device *in_dev; |
| 1012 | const char *ifname; |
| 1013 | struct iface_stat *entry; |
| 1014 | struct iface_stat *new_iface; |
| 1015 | int addr_type; |
| 1016 | |
| 1017 | IF_DEBUG("qtaguid: iface_stat: create6(): ifa=%p netdev=%p->name=%s\n", |
| 1018 | ifa, net_dev, net_dev ? net_dev->name : ""); |
| 1019 | if (!net_dev) { |
| 1020 | pr_err("qtaguid: iface_stat: create6(): no net dev!\n"); |
| 1021 | return; |
| 1022 | } |
| 1023 | ifname = net_dev->name; |
| 1024 | |
| 1025 | in_dev = in_dev_get(net_dev); |
| 1026 | if (!in_dev) { |
| 1027 | pr_err("qtaguid: iface_stat: create6(%s): no inet dev\n", |
| 1028 | ifname); |
| 1029 | return; |
| 1030 | } |
| 1031 | |
| 1032 | IF_DEBUG("qtaguid: iface_stat: create6(%s): in_dev=%p\n", |
| 1033 | ifname, in_dev); |
| 1034 | |
| 1035 | if (!ifa) { |
| 1036 | IF_DEBUG("qtaguid: iface_stat: create6(%s): no matching IP\n", |
| 1037 | ifname); |
| 1038 | goto done_put; |
| 1039 | } |
| 1040 | addr_type = ipv6_addr_type(&ifa->addr); |
| 1041 | |
| 1042 | spin_lock_bh(&iface_stat_list_lock); |
| 1043 | entry = get_iface_entry(ifname); |
| 1044 | if (entry != NULL) { |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1045 | IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__, |
| 1046 | ifname, entry); |
| 1047 | iface_check_stats_reset_and_adjust(net_dev, entry); |
JP Abgrall | cf47f36 | 2013-02-06 17:40:07 -0800 | [diff] [blame] | 1048 | _iface_stat_set_active(entry, net_dev, true); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1049 | IF_DEBUG("qtaguid: %s(%s): " |
| 1050 | "tracking now %d on ip=%pI6c\n", __func__, |
JP Abgrall | cf47f36 | 2013-02-06 17:40:07 -0800 | [diff] [blame] | 1051 | entry->ifname, true, &ifa->addr); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1052 | goto done_unlock_put; |
| 1053 | } |
| 1054 | |
| 1055 | new_iface = iface_alloc(net_dev); |
| 1056 | IF_DEBUG("qtaguid: iface_stat: create6(%s): done " |
| 1057 | "entry=%p ip=%pI6c\n", ifname, new_iface, &ifa->addr); |
| 1058 | |
| 1059 | done_unlock_put: |
| 1060 | spin_unlock_bh(&iface_stat_list_lock); |
| 1061 | done_put: |
| 1062 | in_dev_put(in_dev); |
| 1063 | } |
| 1064 | |
| 1065 | static struct sock_tag *get_sock_stat_nl(const struct sock *sk) |
| 1066 | { |
| 1067 | MT_DEBUG("qtaguid: get_sock_stat_nl(sk=%p)\n", sk); |
| 1068 | return sock_tag_tree_search(&sock_tag_tree, sk); |
| 1069 | } |
| 1070 | |
| 1071 | static struct sock_tag *get_sock_stat(const struct sock *sk) |
| 1072 | { |
| 1073 | struct sock_tag *sock_tag_entry; |
| 1074 | MT_DEBUG("qtaguid: get_sock_stat(sk=%p)\n", sk); |
| 1075 | if (!sk) |
| 1076 | return NULL; |
| 1077 | spin_lock_bh(&sock_tag_list_lock); |
| 1078 | sock_tag_entry = get_sock_stat_nl(sk); |
| 1079 | spin_unlock_bh(&sock_tag_list_lock); |
| 1080 | return sock_tag_entry; |
| 1081 | } |
| 1082 | |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1083 | static int ipx_proto(const struct sk_buff *skb, |
| 1084 | struct xt_action_param *par) |
| 1085 | { |
| 1086 | int thoff = 0, tproto; |
| 1087 | |
| 1088 | switch (par->family) { |
| 1089 | case NFPROTO_IPV6: |
| 1090 | tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL); |
| 1091 | if (tproto < 0) |
| 1092 | MT_DEBUG("%s(): transport header not found in ipv6" |
| 1093 | " skb=%p\n", __func__, skb); |
| 1094 | break; |
| 1095 | case NFPROTO_IPV4: |
| 1096 | tproto = ip_hdr(skb)->protocol; |
| 1097 | break; |
| 1098 | default: |
| 1099 | tproto = IPPROTO_RAW; |
| 1100 | } |
| 1101 | return tproto; |
| 1102 | } |
| 1103 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1104 | static void |
| 1105 | data_counters_update(struct data_counters *dc, int set, |
| 1106 | enum ifs_tx_rx direction, int proto, int bytes) |
| 1107 | { |
| 1108 | switch (proto) { |
| 1109 | case IPPROTO_TCP: |
| 1110 | dc_add_byte_packets(dc, set, direction, IFS_TCP, bytes, 1); |
| 1111 | break; |
| 1112 | case IPPROTO_UDP: |
| 1113 | dc_add_byte_packets(dc, set, direction, IFS_UDP, bytes, 1); |
| 1114 | break; |
| 1115 | case IPPROTO_IP: |
| 1116 | default: |
| 1117 | dc_add_byte_packets(dc, set, direction, IFS_PROTO_OTHER, bytes, |
| 1118 | 1); |
| 1119 | break; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * Update stats for the specified interface. Do nothing if the entry |
| 1125 | * does not exist (when a device was never configured with an IP address). |
| 1126 | * Called when an device is being unregistered. |
| 1127 | */ |
| 1128 | static void iface_stat_update(struct net_device *net_dev, bool stash_only) |
| 1129 | { |
| 1130 | struct rtnl_link_stats64 dev_stats, *stats; |
| 1131 | struct iface_stat *entry; |
| 1132 | |
| 1133 | stats = dev_get_stats(net_dev, &dev_stats); |
| 1134 | spin_lock_bh(&iface_stat_list_lock); |
| 1135 | entry = get_iface_entry(net_dev->name); |
| 1136 | if (entry == NULL) { |
| 1137 | IF_DEBUG("qtaguid: iface_stat: update(%s): not tracked\n", |
| 1138 | net_dev->name); |
| 1139 | spin_unlock_bh(&iface_stat_list_lock); |
| 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__, |
| 1144 | net_dev->name, entry); |
| 1145 | if (!entry->active) { |
| 1146 | IF_DEBUG("qtaguid: %s(%s): already disabled\n", __func__, |
| 1147 | net_dev->name); |
| 1148 | spin_unlock_bh(&iface_stat_list_lock); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | if (stash_only) { |
| 1153 | entry->last_known[IFS_TX].bytes = stats->tx_bytes; |
| 1154 | entry->last_known[IFS_TX].packets = stats->tx_packets; |
| 1155 | entry->last_known[IFS_RX].bytes = stats->rx_bytes; |
| 1156 | entry->last_known[IFS_RX].packets = stats->rx_packets; |
| 1157 | entry->last_known_valid = true; |
| 1158 | IF_DEBUG("qtaguid: %s(%s): " |
| 1159 | "dev stats stashed rx/tx=%llu/%llu\n", __func__, |
| 1160 | net_dev->name, stats->rx_bytes, stats->tx_bytes); |
| 1161 | spin_unlock_bh(&iface_stat_list_lock); |
| 1162 | return; |
| 1163 | } |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1164 | entry->totals_via_dev[IFS_TX].bytes += stats->tx_bytes; |
| 1165 | entry->totals_via_dev[IFS_TX].packets += stats->tx_packets; |
| 1166 | entry->totals_via_dev[IFS_RX].bytes += stats->rx_bytes; |
| 1167 | entry->totals_via_dev[IFS_RX].packets += stats->rx_packets; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1168 | /* We don't need the last_known[] anymore */ |
| 1169 | entry->last_known_valid = false; |
| 1170 | _iface_stat_set_active(entry, net_dev, false); |
| 1171 | IF_DEBUG("qtaguid: %s(%s): " |
| 1172 | "disable tracking. rx/tx=%llu/%llu\n", __func__, |
| 1173 | net_dev->name, stats->rx_bytes, stats->tx_bytes); |
| 1174 | spin_unlock_bh(&iface_stat_list_lock); |
| 1175 | } |
| 1176 | |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1177 | /* |
| 1178 | * Update stats for the specified interface from the skb. |
| 1179 | * Do nothing if the entry |
| 1180 | * does not exist (when a device was never configured with an IP address). |
| 1181 | * Called on each sk. |
| 1182 | */ |
| 1183 | static void iface_stat_update_from_skb(const struct sk_buff *skb, |
| 1184 | struct xt_action_param *par) |
| 1185 | { |
| 1186 | struct iface_stat *entry; |
| 1187 | const struct net_device *el_dev; |
| 1188 | enum ifs_tx_rx direction = par->in ? IFS_RX : IFS_TX; |
| 1189 | int bytes = skb->len; |
JP Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 1190 | int proto; |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1191 | |
| 1192 | if (!skb->dev) { |
| 1193 | MT_DEBUG("qtaguid[%d]: no skb->dev\n", par->hooknum); |
| 1194 | el_dev = par->in ? : par->out; |
| 1195 | } else { |
| 1196 | const struct net_device *other_dev; |
| 1197 | el_dev = skb->dev; |
| 1198 | other_dev = par->in ? : par->out; |
| 1199 | if (el_dev != other_dev) { |
| 1200 | MT_DEBUG("qtaguid[%d]: skb->dev=%p %s vs " |
| 1201 | "par->(in/out)=%p %s\n", |
| 1202 | par->hooknum, el_dev, el_dev->name, other_dev, |
| 1203 | other_dev->name); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | if (unlikely(!el_dev)) { |
JP Abgrall | a2e371b | 2013-04-08 15:09:26 -0700 | [diff] [blame] | 1208 | pr_err_ratelimited("qtaguid[%d]: %s(): no par->in/out?!!\n", |
| 1209 | par->hooknum, __func__); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1210 | BUG(); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1211 | } else { |
JP Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 1212 | proto = ipx_proto(skb, par); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1213 | MT_DEBUG("qtaguid[%d]: dev name=%s type=%d fam=%d proto=%d\n", |
| 1214 | par->hooknum, el_dev->name, el_dev->type, |
| 1215 | par->family, proto); |
| 1216 | } |
| 1217 | |
| 1218 | spin_lock_bh(&iface_stat_list_lock); |
| 1219 | entry = get_iface_entry(el_dev->name); |
| 1220 | if (entry == NULL) { |
| 1221 | IF_DEBUG("qtaguid: iface_stat: %s(%s): not tracked\n", |
| 1222 | __func__, el_dev->name); |
| 1223 | spin_unlock_bh(&iface_stat_list_lock); |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__, |
| 1228 | el_dev->name, entry); |
| 1229 | |
JP Abgrall | 87f93e8 | 2013-01-28 16:50:44 -0800 | [diff] [blame] | 1230 | data_counters_update(&entry->totals_via_skb, 0, direction, proto, |
| 1231 | bytes); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1232 | spin_unlock_bh(&iface_stat_list_lock); |
| 1233 | } |
| 1234 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1235 | static void tag_stat_update(struct tag_stat *tag_entry, |
| 1236 | enum ifs_tx_rx direction, int proto, int bytes) |
| 1237 | { |
| 1238 | int active_set; |
| 1239 | active_set = get_active_counter_set(tag_entry->tn.tag); |
| 1240 | MT_DEBUG("qtaguid: tag_stat_update(tag=0x%llx (uid=%u) set=%d " |
| 1241 | "dir=%d proto=%d bytes=%d)\n", |
| 1242 | tag_entry->tn.tag, get_uid_from_tag(tag_entry->tn.tag), |
| 1243 | active_set, direction, proto, bytes); |
| 1244 | data_counters_update(&tag_entry->counters, active_set, direction, |
| 1245 | proto, bytes); |
| 1246 | if (tag_entry->parent_counters) |
| 1247 | data_counters_update(tag_entry->parent_counters, active_set, |
| 1248 | direction, proto, bytes); |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | * Create a new entry for tracking the specified {acct_tag,uid_tag} within |
| 1253 | * the interface. |
| 1254 | * iface_entry->tag_stat_list_lock should be held. |
| 1255 | */ |
| 1256 | static struct tag_stat *create_if_tag_stat(struct iface_stat *iface_entry, |
| 1257 | tag_t tag) |
| 1258 | { |
| 1259 | struct tag_stat *new_tag_stat_entry = NULL; |
| 1260 | IF_DEBUG("qtaguid: iface_stat: %s(): ife=%p tag=0x%llx" |
| 1261 | " (uid=%u)\n", __func__, |
| 1262 | iface_entry, tag, get_uid_from_tag(tag)); |
| 1263 | new_tag_stat_entry = kzalloc(sizeof(*new_tag_stat_entry), GFP_ATOMIC); |
| 1264 | if (!new_tag_stat_entry) { |
| 1265 | pr_err("qtaguid: iface_stat: tag stat alloc failed\n"); |
| 1266 | goto done; |
| 1267 | } |
| 1268 | new_tag_stat_entry->tn.tag = tag; |
| 1269 | tag_stat_tree_insert(new_tag_stat_entry, &iface_entry->tag_stat_tree); |
| 1270 | done: |
| 1271 | return new_tag_stat_entry; |
| 1272 | } |
| 1273 | |
| 1274 | static void if_tag_stat_update(const char *ifname, uid_t uid, |
| 1275 | const struct sock *sk, enum ifs_tx_rx direction, |
| 1276 | int proto, int bytes) |
| 1277 | { |
| 1278 | struct tag_stat *tag_stat_entry; |
| 1279 | tag_t tag, acct_tag; |
| 1280 | tag_t uid_tag; |
| 1281 | struct data_counters *uid_tag_counters; |
| 1282 | struct sock_tag *sock_tag_entry; |
| 1283 | struct iface_stat *iface_entry; |
JP Abgrall | e5d7986 | 2012-04-13 19:22:35 -0700 | [diff] [blame] | 1284 | struct tag_stat *new_tag_stat = NULL; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1285 | MT_DEBUG("qtaguid: if_tag_stat_update(ifname=%s " |
| 1286 | "uid=%u sk=%p dir=%d proto=%d bytes=%d)\n", |
| 1287 | ifname, uid, sk, direction, proto, bytes); |
| 1288 | |
liping.zhang | f84e6a1 | 2016-01-11 13:31:01 +0800 | [diff] [blame] | 1289 | spin_lock_bh(&iface_stat_list_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1290 | iface_entry = get_iface_entry(ifname); |
| 1291 | if (!iface_entry) { |
JP Abgrall | a2e371b | 2013-04-08 15:09:26 -0700 | [diff] [blame] | 1292 | pr_err_ratelimited("qtaguid: iface_stat: stat_update() " |
| 1293 | "%s not found\n", ifname); |
liping.zhang | f84e6a1 | 2016-01-11 13:31:01 +0800 | [diff] [blame] | 1294 | spin_unlock_bh(&iface_stat_list_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1295 | return; |
| 1296 | } |
| 1297 | /* It is ok to process data when an iface_entry is inactive */ |
| 1298 | |
| 1299 | MT_DEBUG("qtaguid: iface_stat: stat_update() dev=%s entry=%p\n", |
| 1300 | ifname, iface_entry); |
| 1301 | |
| 1302 | /* |
| 1303 | * Look for a tagged sock. |
| 1304 | * It will have an acct_uid. |
| 1305 | */ |
| 1306 | sock_tag_entry = get_sock_stat(sk); |
| 1307 | if (sock_tag_entry) { |
| 1308 | tag = sock_tag_entry->tag; |
| 1309 | acct_tag = get_atag_from_tag(tag); |
| 1310 | uid_tag = get_utag_from_tag(tag); |
| 1311 | } else { |
| 1312 | acct_tag = make_atag_from_value(0); |
| 1313 | tag = combine_atag_with_uid(acct_tag, uid); |
| 1314 | uid_tag = make_tag_from_uid(uid); |
| 1315 | } |
| 1316 | MT_DEBUG("qtaguid: iface_stat: stat_update(): " |
| 1317 | " looking for tag=0x%llx (uid=%u) in ife=%p\n", |
| 1318 | tag, get_uid_from_tag(tag), iface_entry); |
| 1319 | /* Loop over tag list under this interface for {acct_tag,uid_tag} */ |
| 1320 | spin_lock_bh(&iface_entry->tag_stat_list_lock); |
| 1321 | |
| 1322 | tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree, |
| 1323 | tag); |
| 1324 | if (tag_stat_entry) { |
| 1325 | /* |
| 1326 | * Updating the {acct_tag, uid_tag} entry handles both stats: |
| 1327 | * {0, uid_tag} will also get updated. |
| 1328 | */ |
| 1329 | tag_stat_update(tag_stat_entry, direction, proto, bytes); |
liping.zhang | f84e6a1 | 2016-01-11 13:31:01 +0800 | [diff] [blame] | 1330 | goto unlock; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | /* Loop over tag list under this interface for {0,uid_tag} */ |
| 1334 | tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree, |
| 1335 | uid_tag); |
| 1336 | if (!tag_stat_entry) { |
| 1337 | /* Here: the base uid_tag did not exist */ |
| 1338 | /* |
| 1339 | * No parent counters. So |
| 1340 | * - No {0, uid_tag} stats and no {acc_tag, uid_tag} stats. |
| 1341 | */ |
| 1342 | new_tag_stat = create_if_tag_stat(iface_entry, uid_tag); |
Pontus Fuchs | b842ea5 | 2012-11-19 11:44:51 -0800 | [diff] [blame] | 1343 | if (!new_tag_stat) |
| 1344 | goto unlock; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1345 | uid_tag_counters = &new_tag_stat->counters; |
| 1346 | } else { |
| 1347 | uid_tag_counters = &tag_stat_entry->counters; |
| 1348 | } |
| 1349 | |
| 1350 | if (acct_tag) { |
JP Abgrall | e5d7986 | 2012-04-13 19:22:35 -0700 | [diff] [blame] | 1351 | /* Create the child {acct_tag, uid_tag} and hook up parent. */ |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1352 | new_tag_stat = create_if_tag_stat(iface_entry, tag); |
Pontus Fuchs | b842ea5 | 2012-11-19 11:44:51 -0800 | [diff] [blame] | 1353 | if (!new_tag_stat) |
| 1354 | goto unlock; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1355 | new_tag_stat->parent_counters = uid_tag_counters; |
JP Abgrall | e5d7986 | 2012-04-13 19:22:35 -0700 | [diff] [blame] | 1356 | } else { |
| 1357 | /* |
| 1358 | * For new_tag_stat to be still NULL here would require: |
| 1359 | * {0, uid_tag} exists |
| 1360 | * and {acct_tag, uid_tag} doesn't exist |
| 1361 | * AND acct_tag == 0. |
| 1362 | * Impossible. This reassures us that new_tag_stat |
| 1363 | * below will always be assigned. |
| 1364 | */ |
| 1365 | BUG_ON(!new_tag_stat); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1366 | } |
| 1367 | tag_stat_update(new_tag_stat, direction, proto, bytes); |
Pontus Fuchs | b842ea5 | 2012-11-19 11:44:51 -0800 | [diff] [blame] | 1368 | unlock: |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1369 | spin_unlock_bh(&iface_entry->tag_stat_list_lock); |
liping.zhang | f84e6a1 | 2016-01-11 13:31:01 +0800 | [diff] [blame] | 1370 | spin_unlock_bh(&iface_stat_list_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | static int iface_netdev_event_handler(struct notifier_block *nb, |
| 1374 | unsigned long event, void *ptr) { |
Jon Medhurst (Tixy) | a347e8e | 2014-04-14 21:20:49 -0700 | [diff] [blame] | 1375 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1376 | |
| 1377 | if (unlikely(module_passive)) |
| 1378 | return NOTIFY_DONE; |
| 1379 | |
| 1380 | IF_DEBUG("qtaguid: iface_stat: netdev_event(): " |
| 1381 | "ev=0x%lx/%s netdev=%p->name=%s\n", |
| 1382 | event, netdev_evt_str(event), dev, dev ? dev->name : ""); |
| 1383 | |
| 1384 | switch (event) { |
| 1385 | case NETDEV_UP: |
| 1386 | iface_stat_create(dev, NULL); |
| 1387 | atomic64_inc(&qtu_events.iface_events); |
| 1388 | break; |
| 1389 | case NETDEV_DOWN: |
| 1390 | case NETDEV_UNREGISTER: |
| 1391 | iface_stat_update(dev, event == NETDEV_DOWN); |
| 1392 | atomic64_inc(&qtu_events.iface_events); |
| 1393 | break; |
| 1394 | } |
| 1395 | return NOTIFY_DONE; |
| 1396 | } |
| 1397 | |
| 1398 | static int iface_inet6addr_event_handler(struct notifier_block *nb, |
| 1399 | unsigned long event, void *ptr) |
| 1400 | { |
| 1401 | struct inet6_ifaddr *ifa = ptr; |
| 1402 | struct net_device *dev; |
| 1403 | |
| 1404 | if (unlikely(module_passive)) |
| 1405 | return NOTIFY_DONE; |
| 1406 | |
| 1407 | IF_DEBUG("qtaguid: iface_stat: inet6addr_event(): " |
| 1408 | "ev=0x%lx/%s ifa=%p\n", |
| 1409 | event, netdev_evt_str(event), ifa); |
| 1410 | |
| 1411 | switch (event) { |
| 1412 | case NETDEV_UP: |
| 1413 | BUG_ON(!ifa || !ifa->idev); |
| 1414 | dev = (struct net_device *)ifa->idev->dev; |
| 1415 | iface_stat_create_ipv6(dev, ifa); |
| 1416 | atomic64_inc(&qtu_events.iface_events); |
| 1417 | break; |
| 1418 | case NETDEV_DOWN: |
| 1419 | case NETDEV_UNREGISTER: |
| 1420 | BUG_ON(!ifa || !ifa->idev); |
| 1421 | dev = (struct net_device *)ifa->idev->dev; |
| 1422 | iface_stat_update(dev, event == NETDEV_DOWN); |
| 1423 | atomic64_inc(&qtu_events.iface_events); |
| 1424 | break; |
| 1425 | } |
| 1426 | return NOTIFY_DONE; |
| 1427 | } |
| 1428 | |
| 1429 | static int iface_inetaddr_event_handler(struct notifier_block *nb, |
| 1430 | unsigned long event, void *ptr) |
| 1431 | { |
| 1432 | struct in_ifaddr *ifa = ptr; |
| 1433 | struct net_device *dev; |
| 1434 | |
| 1435 | if (unlikely(module_passive)) |
| 1436 | return NOTIFY_DONE; |
| 1437 | |
| 1438 | IF_DEBUG("qtaguid: iface_stat: inetaddr_event(): " |
| 1439 | "ev=0x%lx/%s ifa=%p\n", |
| 1440 | event, netdev_evt_str(event), ifa); |
| 1441 | |
| 1442 | switch (event) { |
| 1443 | case NETDEV_UP: |
| 1444 | BUG_ON(!ifa || !ifa->ifa_dev); |
| 1445 | dev = ifa->ifa_dev->dev; |
| 1446 | iface_stat_create(dev, ifa); |
| 1447 | atomic64_inc(&qtu_events.iface_events); |
| 1448 | break; |
| 1449 | case NETDEV_DOWN: |
| 1450 | case NETDEV_UNREGISTER: |
| 1451 | BUG_ON(!ifa || !ifa->ifa_dev); |
| 1452 | dev = ifa->ifa_dev->dev; |
| 1453 | iface_stat_update(dev, event == NETDEV_DOWN); |
| 1454 | atomic64_inc(&qtu_events.iface_events); |
| 1455 | break; |
| 1456 | } |
| 1457 | return NOTIFY_DONE; |
| 1458 | } |
| 1459 | |
| 1460 | static struct notifier_block iface_netdev_notifier_blk = { |
| 1461 | .notifier_call = iface_netdev_event_handler, |
| 1462 | }; |
| 1463 | |
| 1464 | static struct notifier_block iface_inetaddr_notifier_blk = { |
| 1465 | .notifier_call = iface_inetaddr_event_handler, |
| 1466 | }; |
| 1467 | |
| 1468 | static struct notifier_block iface_inet6addr_notifier_blk = { |
| 1469 | .notifier_call = iface_inet6addr_event_handler, |
| 1470 | }; |
| 1471 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1472 | static const struct seq_operations iface_stat_fmt_proc_seq_ops = { |
| 1473 | .start = iface_stat_fmt_proc_start, |
| 1474 | .next = iface_stat_fmt_proc_next, |
| 1475 | .stop = iface_stat_fmt_proc_stop, |
| 1476 | .show = iface_stat_fmt_proc_show, |
| 1477 | }; |
| 1478 | |
| 1479 | static int proc_iface_stat_fmt_open(struct inode *inode, struct file *file) |
| 1480 | { |
| 1481 | struct proc_iface_stat_fmt_info *s; |
| 1482 | |
| 1483 | s = __seq_open_private(file, &iface_stat_fmt_proc_seq_ops, |
| 1484 | sizeof(struct proc_iface_stat_fmt_info)); |
| 1485 | if (!s) |
| 1486 | return -ENOMEM; |
| 1487 | |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 1488 | s->fmt = (uintptr_t)PDE_DATA(inode); |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | static const struct file_operations proc_iface_stat_fmt_fops = { |
| 1493 | .open = proc_iface_stat_fmt_open, |
| 1494 | .read = seq_read, |
| 1495 | .llseek = seq_lseek, |
Greg Hackmann | 5647291 | 2013-12-04 17:39:27 -0800 | [diff] [blame] | 1496 | .release = seq_release_private, |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1497 | }; |
| 1498 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1499 | static int __init iface_stat_init(struct proc_dir_entry *parent_procdir) |
| 1500 | { |
| 1501 | int err; |
| 1502 | |
| 1503 | iface_stat_procdir = proc_mkdir(iface_stat_procdirname, parent_procdir); |
| 1504 | if (!iface_stat_procdir) { |
| 1505 | pr_err("qtaguid: iface_stat: init failed to create proc entry\n"); |
| 1506 | err = -1; |
| 1507 | goto err; |
| 1508 | } |
| 1509 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1510 | iface_stat_all_procfile = proc_create_data(iface_stat_all_procfilename, |
| 1511 | proc_iface_perms, |
| 1512 | parent_procdir, |
| 1513 | &proc_iface_stat_fmt_fops, |
| 1514 | (void *)1 /* fmt1 */); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1515 | if (!iface_stat_all_procfile) { |
| 1516 | pr_err("qtaguid: iface_stat: init " |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1517 | " failed to create stat_old proc entry\n"); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1518 | err = -1; |
| 1519 | goto err_zap_entry; |
| 1520 | } |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1521 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1522 | iface_stat_fmt_procfile = proc_create_data(iface_stat_fmt_procfilename, |
| 1523 | proc_iface_perms, |
| 1524 | parent_procdir, |
| 1525 | &proc_iface_stat_fmt_fops, |
| 1526 | (void *)2 /* fmt2 */); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1527 | if (!iface_stat_fmt_procfile) { |
| 1528 | pr_err("qtaguid: iface_stat: init " |
| 1529 | " failed to create stat_all proc entry\n"); |
| 1530 | err = -1; |
| 1531 | goto err_zap_all_stats_entry; |
| 1532 | } |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1533 | |
| 1534 | |
| 1535 | err = register_netdevice_notifier(&iface_netdev_notifier_blk); |
| 1536 | if (err) { |
| 1537 | pr_err("qtaguid: iface_stat: init " |
| 1538 | "failed to register dev event handler\n"); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1539 | goto err_zap_all_stats_entries; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1540 | } |
| 1541 | err = register_inetaddr_notifier(&iface_inetaddr_notifier_blk); |
| 1542 | if (err) { |
| 1543 | pr_err("qtaguid: iface_stat: init " |
| 1544 | "failed to register ipv4 dev event handler\n"); |
| 1545 | goto err_unreg_nd; |
| 1546 | } |
| 1547 | |
| 1548 | err = register_inet6addr_notifier(&iface_inet6addr_notifier_blk); |
| 1549 | if (err) { |
| 1550 | pr_err("qtaguid: iface_stat: init " |
| 1551 | "failed to register ipv6 dev event handler\n"); |
| 1552 | goto err_unreg_ip4_addr; |
| 1553 | } |
| 1554 | return 0; |
| 1555 | |
| 1556 | err_unreg_ip4_addr: |
| 1557 | unregister_inetaddr_notifier(&iface_inetaddr_notifier_blk); |
| 1558 | err_unreg_nd: |
| 1559 | unregister_netdevice_notifier(&iface_netdev_notifier_blk); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1560 | err_zap_all_stats_entries: |
| 1561 | remove_proc_entry(iface_stat_fmt_procfilename, parent_procdir); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1562 | err_zap_all_stats_entry: |
| 1563 | remove_proc_entry(iface_stat_all_procfilename, parent_procdir); |
| 1564 | err_zap_entry: |
| 1565 | remove_proc_entry(iface_stat_procdirname, parent_procdir); |
| 1566 | err: |
| 1567 | return err; |
| 1568 | } |
| 1569 | |
| 1570 | static struct sock *qtaguid_find_sk(const struct sk_buff *skb, |
| 1571 | struct xt_action_param *par) |
| 1572 | { |
| 1573 | struct sock *sk; |
| 1574 | unsigned int hook_mask = (1 << par->hooknum); |
| 1575 | |
| 1576 | MT_DEBUG("qtaguid: find_sk(skb=%p) hooknum=%d family=%d\n", skb, |
| 1577 | par->hooknum, par->family); |
| 1578 | |
| 1579 | /* |
| 1580 | * Let's not abuse the the xt_socket_get*_sk(), or else it will |
| 1581 | * return garbage SKs. |
| 1582 | */ |
| 1583 | if (!(hook_mask & XT_SOCKET_SUPPORTED_HOOKS)) |
| 1584 | return NULL; |
| 1585 | |
| 1586 | switch (par->family) { |
| 1587 | case NFPROTO_IPV6: |
Amit Pundir | 7de1bb8 | 2015-11-20 14:45:40 +0530 | [diff] [blame] | 1588 | 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] | 1589 | break; |
| 1590 | case NFPROTO_IPV4: |
Amit Pundir | 7de1bb8 | 2015-11-20 14:45:40 +0530 | [diff] [blame] | 1591 | 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] | 1592 | break; |
| 1593 | default: |
| 1594 | return NULL; |
| 1595 | } |
| 1596 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1597 | if (sk) { |
| 1598 | MT_DEBUG("qtaguid: %p->sk_proto=%u " |
| 1599 | "->sk_state=%d\n", sk, sk->sk_protocol, sk->sk_state); |
JP Abgrall | d1fd397 | 2013-02-20 16:38:34 -0800 | [diff] [blame] | 1600 | /* |
| 1601 | * When in TCP_TIME_WAIT the sk is not a "struct sock" but |
| 1602 | * "struct inet_timewait_sock" which is missing fields. |
| 1603 | */ |
John Stultz | 6978766 | 2016-05-12 11:17:52 -0700 | [diff] [blame] | 1604 | if (!sk_fullsock(sk) || sk->sk_state == TCP_TIME_WAIT) { |
Amit Pundir | 2879b6e | 2015-01-29 01:16:23 +0530 | [diff] [blame] | 1605 | sock_gen_put(sk); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1606 | sk = NULL; |
| 1607 | } |
| 1608 | } |
| 1609 | return sk; |
| 1610 | } |
| 1611 | |
| 1612 | static void account_for_uid(const struct sk_buff *skb, |
| 1613 | const struct sock *alternate_sk, uid_t uid, |
| 1614 | struct xt_action_param *par) |
| 1615 | { |
| 1616 | const struct net_device *el_dev; |
| 1617 | |
| 1618 | if (!skb->dev) { |
| 1619 | MT_DEBUG("qtaguid[%d]: no skb->dev\n", par->hooknum); |
| 1620 | el_dev = par->in ? : par->out; |
| 1621 | } else { |
| 1622 | const struct net_device *other_dev; |
| 1623 | el_dev = skb->dev; |
| 1624 | other_dev = par->in ? : par->out; |
| 1625 | if (el_dev != other_dev) { |
| 1626 | MT_DEBUG("qtaguid[%d]: skb->dev=%p %s vs " |
| 1627 | "par->(in/out)=%p %s\n", |
| 1628 | par->hooknum, el_dev, el_dev->name, other_dev, |
| 1629 | other_dev->name); |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | if (unlikely(!el_dev)) { |
| 1634 | pr_info("qtaguid[%d]: no par->in/out?!!\n", par->hooknum); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1635 | } else { |
JP Abgrall | 4bb20aa | 2012-04-17 16:00:07 -0700 | [diff] [blame] | 1636 | int proto = ipx_proto(skb, par); |
| 1637 | MT_DEBUG("qtaguid[%d]: dev name=%s type=%d fam=%d proto=%d\n", |
| 1638 | par->hooknum, el_dev->name, el_dev->type, |
| 1639 | par->family, proto); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1640 | |
| 1641 | if_tag_stat_update(el_dev->name, uid, |
| 1642 | skb->sk ? skb->sk : alternate_sk, |
| 1643 | par->in ? IFS_RX : IFS_TX, |
JP Abgrall | 4bb20aa | 2012-04-17 16:00:07 -0700 | [diff] [blame] | 1644 | proto, skb->len); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | static bool qtaguid_mt(const struct sk_buff *skb, struct xt_action_param *par) |
| 1649 | { |
| 1650 | const struct xt_qtaguid_match_info *info = par->matchinfo; |
| 1651 | const struct file *filp; |
| 1652 | bool got_sock = false; |
| 1653 | struct sock *sk; |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1654 | kuid_t sock_uid; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1655 | bool res; |
Mohamad Ayyash | e8101fd | 2015-01-13 19:20:44 -0800 | [diff] [blame] | 1656 | bool set_sk_callback_lock = false; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1657 | |
| 1658 | if (unlikely(module_passive)) |
| 1659 | return (info->match ^ info->invert) == 0; |
| 1660 | |
| 1661 | MT_DEBUG("qtaguid[%d]: entered skb=%p par->in=%p/out=%p fam=%d\n", |
| 1662 | par->hooknum, skb, par->in, par->out, par->family); |
| 1663 | |
| 1664 | atomic64_inc(&qtu_events.match_calls); |
| 1665 | if (skb == NULL) { |
| 1666 | res = (info->match ^ info->invert) == 0; |
| 1667 | goto ret_res; |
| 1668 | } |
| 1669 | |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 1670 | switch (par->hooknum) { |
| 1671 | case NF_INET_PRE_ROUTING: |
| 1672 | case NF_INET_POST_ROUTING: |
| 1673 | atomic64_inc(&qtu_events.match_calls_prepost); |
| 1674 | iface_stat_update_from_skb(skb, par); |
| 1675 | /* |
| 1676 | * We are done in pre/post. The skb will get processed |
| 1677 | * further alter. |
| 1678 | */ |
| 1679 | res = (info->match ^ info->invert); |
| 1680 | goto ret_res; |
| 1681 | break; |
| 1682 | /* default: Fall through and do UID releated work */ |
| 1683 | } |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1684 | |
John Stultz | 551dae6 | 2016-04-22 17:12:57 -0700 | [diff] [blame] | 1685 | sk = skb_to_full_sk(skb); |
JP Abgrall | d1fd397 | 2013-02-20 16:38:34 -0800 | [diff] [blame] | 1686 | /* |
| 1687 | * When in TCP_TIME_WAIT the sk is not a "struct sock" but |
| 1688 | * "struct inet_timewait_sock" which is missing fields. |
| 1689 | * So we ignore it. |
| 1690 | */ |
| 1691 | if (sk && sk->sk_state == TCP_TIME_WAIT) |
| 1692 | sk = NULL; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1693 | if (sk == NULL) { |
| 1694 | /* |
| 1695 | * A missing sk->sk_socket happens when packets are in-flight |
| 1696 | * and the matching socket is already closed and gone. |
| 1697 | */ |
| 1698 | sk = qtaguid_find_sk(skb, par); |
| 1699 | /* |
| 1700 | * If we got the socket from the find_sk(), we will need to put |
| 1701 | * it back, as nf_tproxy_get_sock_v4() got it. |
| 1702 | */ |
| 1703 | got_sock = sk; |
| 1704 | if (sk) |
| 1705 | atomic64_inc(&qtu_events.match_found_sk_in_ct); |
| 1706 | else |
| 1707 | atomic64_inc(&qtu_events.match_found_no_sk_in_ct); |
| 1708 | } else { |
| 1709 | atomic64_inc(&qtu_events.match_found_sk); |
| 1710 | } |
JP Abgrall | 4bb20aa | 2012-04-17 16:00:07 -0700 | [diff] [blame] | 1711 | MT_DEBUG("qtaguid[%d]: sk=%p got_sock=%d fam=%d proto=%d\n", |
| 1712 | par->hooknum, sk, got_sock, par->family, ipx_proto(skb, par)); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1713 | if (sk != NULL) { |
Mohamad Ayyash | e8101fd | 2015-01-13 19:20:44 -0800 | [diff] [blame] | 1714 | set_sk_callback_lock = true; |
| 1715 | read_lock_bh(&sk->sk_callback_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1716 | MT_DEBUG("qtaguid[%d]: sk=%p->sk_socket=%p->file=%p\n", |
| 1717 | par->hooknum, sk, sk->sk_socket, |
| 1718 | sk->sk_socket ? sk->sk_socket->file : (void *)-1LL); |
| 1719 | filp = sk->sk_socket ? sk->sk_socket->file : NULL; |
| 1720 | MT_DEBUG("qtaguid[%d]: filp...uid=%u\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1721 | par->hooknum, filp ? from_kuid(&init_user_ns, filp->f_cred->fsuid) : -1); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | if (sk == NULL || sk->sk_socket == NULL) { |
| 1725 | /* |
| 1726 | * Here, the qtaguid_find_sk() using connection tracking |
| 1727 | * couldn't find the owner, so for now we just count them |
| 1728 | * against the system. |
| 1729 | */ |
| 1730 | /* |
| 1731 | * TODO: unhack how to force just accounting. |
| 1732 | * For now we only do iface stats when the uid-owner is not |
| 1733 | * requested. |
| 1734 | */ |
| 1735 | if (!(info->match & XT_QTAGUID_UID)) |
| 1736 | account_for_uid(skb, sk, 0, par); |
| 1737 | MT_DEBUG("qtaguid[%d]: leaving (sk?sk->sk_socket)=%p\n", |
| 1738 | par->hooknum, |
| 1739 | sk ? sk->sk_socket : NULL); |
| 1740 | res = (info->match ^ info->invert) == 0; |
| 1741 | atomic64_inc(&qtu_events.match_no_sk); |
| 1742 | goto put_sock_ret_res; |
| 1743 | } else if (info->match & info->invert & XT_QTAGUID_SOCKET) { |
| 1744 | res = false; |
| 1745 | goto put_sock_ret_res; |
| 1746 | } |
| 1747 | filp = sk->sk_socket->file; |
| 1748 | if (filp == NULL) { |
| 1749 | MT_DEBUG("qtaguid[%d]: leaving filp=NULL\n", par->hooknum); |
| 1750 | account_for_uid(skb, sk, 0, par); |
| 1751 | res = ((info->match ^ info->invert) & |
| 1752 | (XT_QTAGUID_UID | XT_QTAGUID_GID)) == 0; |
| 1753 | atomic64_inc(&qtu_events.match_no_sk_file); |
| 1754 | goto put_sock_ret_res; |
| 1755 | } |
| 1756 | sock_uid = filp->f_cred->fsuid; |
| 1757 | /* |
| 1758 | * TODO: unhack how to force just accounting. |
| 1759 | * For now we only do iface stats when the uid-owner is not requested |
| 1760 | */ |
| 1761 | if (!(info->match & XT_QTAGUID_UID)) |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1762 | account_for_uid(skb, sk, from_kuid(&init_user_ns, sock_uid), par); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1763 | |
| 1764 | /* |
| 1765 | * The following two tests fail the match when: |
| 1766 | * id not in range AND no inverted condition requested |
| 1767 | * or id in range AND inverted condition requested |
| 1768 | * Thus (!a && b) || (a && !b) == a ^ b |
| 1769 | */ |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1770 | if (info->match & XT_QTAGUID_UID) { |
| 1771 | kuid_t uid_min = make_kuid(&init_user_ns, info->uid_min); |
| 1772 | kuid_t uid_max = make_kuid(&init_user_ns, info->uid_max); |
| 1773 | |
Amit Pundir | 070eff8 | 2015-01-20 16:13:08 +0530 | [diff] [blame] | 1774 | if ((uid_gte(filp->f_cred->fsuid, uid_min) && |
| 1775 | uid_lte(filp->f_cred->fsuid, uid_max)) ^ |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1776 | !(info->invert & XT_QTAGUID_UID)) { |
| 1777 | MT_DEBUG("qtaguid[%d]: leaving uid not matching\n", |
| 1778 | par->hooknum); |
| 1779 | res = false; |
| 1780 | goto put_sock_ret_res; |
| 1781 | } |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1782 | } |
| 1783 | if (info->match & XT_QTAGUID_GID) { |
| 1784 | kgid_t gid_min = make_kgid(&init_user_ns, info->gid_min); |
| 1785 | kgid_t gid_max = make_kgid(&init_user_ns, info->gid_max); |
| 1786 | |
Amit Pundir | 070eff8 | 2015-01-20 16:13:08 +0530 | [diff] [blame] | 1787 | if ((gid_gte(filp->f_cred->fsgid, gid_min) && |
| 1788 | gid_lte(filp->f_cred->fsgid, gid_max)) ^ |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1789 | !(info->invert & XT_QTAGUID_GID)) { |
| 1790 | MT_DEBUG("qtaguid[%d]: leaving gid not matching\n", |
| 1791 | par->hooknum); |
| 1792 | res = false; |
| 1793 | goto put_sock_ret_res; |
| 1794 | } |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1795 | } |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1796 | MT_DEBUG("qtaguid[%d]: leaving matched\n", par->hooknum); |
| 1797 | res = true; |
| 1798 | |
| 1799 | put_sock_ret_res: |
| 1800 | if (got_sock) |
Amit Pundir | 2879b6e | 2015-01-29 01:16:23 +0530 | [diff] [blame] | 1801 | sock_gen_put(sk); |
Mohamad Ayyash | e8101fd | 2015-01-13 19:20:44 -0800 | [diff] [blame] | 1802 | if (set_sk_callback_lock) |
| 1803 | read_unlock_bh(&sk->sk_callback_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1804 | ret_res: |
| 1805 | MT_DEBUG("qtaguid[%d]: left %d\n", par->hooknum, res); |
| 1806 | return res; |
| 1807 | } |
| 1808 | |
| 1809 | #ifdef DDEBUG |
Chenbo Feng | 64157f4 | 2017-03-23 13:51:24 -0700 | [diff] [blame] | 1810 | /* |
| 1811 | * This function is not in xt_qtaguid_print.c because of locks visibility. |
| 1812 | * The lock of sock_tag_list must be aquired before calling this function |
| 1813 | */ |
| 1814 | static void prdebug_full_state_locked(int indent_level, const char *fmt, ...) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1815 | { |
| 1816 | va_list args; |
| 1817 | char *fmt_buff; |
| 1818 | char *buff; |
| 1819 | |
| 1820 | if (!unlikely(qtaguid_debug_mask & DDEBUG_MASK)) |
| 1821 | return; |
| 1822 | |
| 1823 | fmt_buff = kasprintf(GFP_ATOMIC, |
| 1824 | "qtaguid: %s(): %s {\n", __func__, fmt); |
| 1825 | BUG_ON(!fmt_buff); |
| 1826 | va_start(args, fmt); |
| 1827 | buff = kvasprintf(GFP_ATOMIC, |
| 1828 | fmt_buff, args); |
| 1829 | BUG_ON(!buff); |
| 1830 | pr_debug("%s", buff); |
| 1831 | kfree(fmt_buff); |
| 1832 | kfree(buff); |
| 1833 | va_end(args); |
| 1834 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1835 | prdebug_sock_tag_tree(indent_level, &sock_tag_tree); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1836 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1837 | spin_lock_bh(&uid_tag_data_tree_lock); |
| 1838 | prdebug_uid_tag_data_tree(indent_level, &uid_tag_data_tree); |
| 1839 | prdebug_proc_qtu_data_tree(indent_level, &proc_qtu_data_tree); |
| 1840 | spin_unlock_bh(&uid_tag_data_tree_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1841 | |
| 1842 | spin_lock_bh(&iface_stat_list_lock); |
| 1843 | prdebug_iface_stat_list(indent_level, &iface_stat_list); |
| 1844 | spin_unlock_bh(&iface_stat_list_lock); |
| 1845 | |
| 1846 | pr_debug("qtaguid: %s(): }\n", __func__); |
| 1847 | } |
| 1848 | #else |
Chenbo Feng | 64157f4 | 2017-03-23 13:51:24 -0700 | [diff] [blame] | 1849 | static void prdebug_full_state_locked(int indent_level, const char *fmt, ...) {} |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1850 | #endif |
| 1851 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1852 | struct proc_ctrl_print_info { |
| 1853 | struct sock *sk; /* socket found by reading to sk_pos */ |
| 1854 | loff_t sk_pos; |
| 1855 | }; |
| 1856 | |
| 1857 | static void *qtaguid_ctrl_proc_next(struct seq_file *m, void *v, loff_t *pos) |
| 1858 | { |
| 1859 | struct proc_ctrl_print_info *pcpi = m->private; |
| 1860 | struct sock_tag *sock_tag_entry = v; |
| 1861 | struct rb_node *node; |
| 1862 | |
| 1863 | (*pos)++; |
| 1864 | |
| 1865 | if (!v || v == SEQ_START_TOKEN) |
| 1866 | return NULL; |
| 1867 | |
| 1868 | node = rb_next(&sock_tag_entry->sock_node); |
| 1869 | if (!node) { |
| 1870 | pcpi->sk = NULL; |
| 1871 | sock_tag_entry = SEQ_START_TOKEN; |
| 1872 | } else { |
| 1873 | sock_tag_entry = rb_entry(node, struct sock_tag, sock_node); |
| 1874 | pcpi->sk = sock_tag_entry->sk; |
| 1875 | } |
| 1876 | pcpi->sk_pos = *pos; |
| 1877 | return sock_tag_entry; |
| 1878 | } |
| 1879 | |
| 1880 | static void *qtaguid_ctrl_proc_start(struct seq_file *m, loff_t *pos) |
| 1881 | { |
| 1882 | struct proc_ctrl_print_info *pcpi = m->private; |
| 1883 | struct sock_tag *sock_tag_entry; |
| 1884 | struct rb_node *node; |
| 1885 | |
| 1886 | spin_lock_bh(&sock_tag_list_lock); |
| 1887 | |
| 1888 | if (unlikely(module_passive)) |
| 1889 | return NULL; |
| 1890 | |
| 1891 | if (*pos == 0) { |
| 1892 | pcpi->sk_pos = 0; |
| 1893 | node = rb_first(&sock_tag_tree); |
| 1894 | if (!node) { |
| 1895 | pcpi->sk = NULL; |
| 1896 | return SEQ_START_TOKEN; |
| 1897 | } |
| 1898 | sock_tag_entry = rb_entry(node, struct sock_tag, sock_node); |
| 1899 | pcpi->sk = sock_tag_entry->sk; |
| 1900 | } else { |
| 1901 | sock_tag_entry = (pcpi->sk ? get_sock_stat_nl(pcpi->sk) : |
| 1902 | NULL) ?: SEQ_START_TOKEN; |
| 1903 | if (*pos != pcpi->sk_pos) { |
| 1904 | /* seq_read skipped a next call */ |
| 1905 | *pos = pcpi->sk_pos; |
| 1906 | return qtaguid_ctrl_proc_next(m, sock_tag_entry, pos); |
| 1907 | } |
| 1908 | } |
| 1909 | return sock_tag_entry; |
| 1910 | } |
| 1911 | |
| 1912 | static void qtaguid_ctrl_proc_stop(struct seq_file *m, void *v) |
| 1913 | { |
| 1914 | spin_unlock_bh(&sock_tag_list_lock); |
| 1915 | } |
| 1916 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1917 | /* |
| 1918 | * Procfs reader to get all active socket tags using style "1)" as described in |
| 1919 | * fs/proc/generic.c |
| 1920 | */ |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1921 | static int qtaguid_ctrl_proc_show(struct seq_file *m, void *v) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1922 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1923 | struct sock_tag *sock_tag_entry = v; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1924 | uid_t uid; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1925 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1926 | CT_DEBUG("qtaguid: proc ctrl pid=%u tgid=%u uid=%u\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1927 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1928 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1929 | if (sock_tag_entry != SEQ_START_TOKEN) { |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 1930 | int sk_ref_count; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1931 | uid = get_uid_from_tag(sock_tag_entry->tag); |
| 1932 | CT_DEBUG("qtaguid: proc_read(): sk=%p tag=0x%llx (uid=%u) " |
| 1933 | "pid=%u\n", |
| 1934 | sock_tag_entry->sk, |
| 1935 | sock_tag_entry->tag, |
| 1936 | uid, |
| 1937 | sock_tag_entry->pid |
| 1938 | ); |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 1939 | sk_ref_count = atomic_read( |
| 1940 | &sock_tag_entry->sk->sk_refcnt); |
Mohamad Ayyash | 8613d93 | 2016-05-11 13:18:35 -0700 | [diff] [blame] | 1941 | seq_printf(m, "sock=%pK tag=0x%llx (uid=%u) pid=%u " |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 1942 | "f_count=%d\n", |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1943 | sock_tag_entry->sk, |
| 1944 | sock_tag_entry->tag, uid, |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 1945 | sock_tag_entry->pid, sk_ref_count); |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1946 | } else { |
| 1947 | seq_printf(m, "events: sockets_tagged=%llu " |
| 1948 | "sockets_untagged=%llu " |
| 1949 | "counter_set_changes=%llu " |
| 1950 | "delete_cmds=%llu " |
| 1951 | "iface_events=%llu " |
| 1952 | "match_calls=%llu " |
| 1953 | "match_calls_prepost=%llu " |
| 1954 | "match_found_sk=%llu " |
| 1955 | "match_found_sk_in_ct=%llu " |
| 1956 | "match_found_no_sk_in_ct=%llu " |
| 1957 | "match_no_sk=%llu " |
| 1958 | "match_no_sk_file=%llu\n", |
Sherman Yin | da5ea99 | 2014-06-12 14:35:38 -0700 | [diff] [blame] | 1959 | (u64)atomic64_read(&qtu_events.sockets_tagged), |
| 1960 | (u64)atomic64_read(&qtu_events.sockets_untagged), |
| 1961 | (u64)atomic64_read(&qtu_events.counter_set_changes), |
| 1962 | (u64)atomic64_read(&qtu_events.delete_cmds), |
| 1963 | (u64)atomic64_read(&qtu_events.iface_events), |
| 1964 | (u64)atomic64_read(&qtu_events.match_calls), |
| 1965 | (u64)atomic64_read(&qtu_events.match_calls_prepost), |
| 1966 | (u64)atomic64_read(&qtu_events.match_found_sk), |
| 1967 | (u64)atomic64_read(&qtu_events.match_found_sk_in_ct), |
| 1968 | (u64)atomic64_read(&qtu_events.match_found_no_sk_in_ct), |
| 1969 | (u64)atomic64_read(&qtu_events.match_no_sk), |
| 1970 | (u64)atomic64_read(&qtu_events.match_no_sk_file)); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1971 | |
Chenbo Feng | 64157f4 | 2017-03-23 13:51:24 -0700 | [diff] [blame] | 1972 | /* Count the following as part of the last item_index. No need |
| 1973 | * to lock the sock_tag_list here since it is already locked when |
| 1974 | * starting the seq_file operation |
| 1975 | */ |
| 1976 | prdebug_full_state_locked(0, "proc ctrl"); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 1979 | return 0; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | /* |
| 1983 | * Delete socket tags, and stat tags associated with a given |
| 1984 | * accouting tag and uid. |
| 1985 | */ |
| 1986 | static int ctrl_cmd_delete(const char *input) |
| 1987 | { |
| 1988 | char cmd; |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 1989 | int uid_int; |
| 1990 | kuid_t uid; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 1991 | uid_t entry_uid; |
| 1992 | tag_t acct_tag; |
| 1993 | tag_t tag; |
| 1994 | int res, argc; |
| 1995 | struct iface_stat *iface_entry; |
| 1996 | struct rb_node *node; |
| 1997 | struct sock_tag *st_entry; |
| 1998 | struct rb_root st_to_free_tree = RB_ROOT; |
| 1999 | struct tag_stat *ts_entry; |
| 2000 | struct tag_counter_set *tcs_entry; |
| 2001 | struct tag_ref *tr_entry; |
| 2002 | struct uid_tag_data *utd_entry; |
| 2003 | |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2004 | argc = sscanf(input, "%c %llu %u", &cmd, &acct_tag, &uid_int); |
| 2005 | uid = make_kuid(&init_user_ns, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2006 | CT_DEBUG("qtaguid: ctrl_delete(%s): argc=%d cmd=%c " |
| 2007 | "user_tag=0x%llx uid=%u\n", input, argc, cmd, |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2008 | acct_tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2009 | if (argc < 2) { |
| 2010 | res = -EINVAL; |
| 2011 | goto err; |
| 2012 | } |
| 2013 | if (!valid_atag(acct_tag)) { |
| 2014 | pr_info("qtaguid: ctrl_delete(%s): invalid tag\n", input); |
| 2015 | res = -EINVAL; |
| 2016 | goto err; |
| 2017 | } |
| 2018 | if (argc < 3) { |
| 2019 | uid = current_fsuid(); |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2020 | uid_int = from_kuid(&init_user_ns, uid); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2021 | } else if (!can_impersonate_uid(uid)) { |
| 2022 | pr_info("qtaguid: ctrl_delete(%s): " |
| 2023 | "insufficient priv from pid=%u tgid=%u uid=%u\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2024 | input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2025 | res = -EPERM; |
| 2026 | goto err; |
| 2027 | } |
| 2028 | |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2029 | tag = combine_atag_with_uid(acct_tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2030 | CT_DEBUG("qtaguid: ctrl_delete(%s): " |
| 2031 | "looking for tag=0x%llx (uid=%u)\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2032 | input, tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2033 | |
| 2034 | /* Delete socket tags */ |
| 2035 | spin_lock_bh(&sock_tag_list_lock); |
| 2036 | node = rb_first(&sock_tag_tree); |
| 2037 | while (node) { |
| 2038 | st_entry = rb_entry(node, struct sock_tag, sock_node); |
| 2039 | entry_uid = get_uid_from_tag(st_entry->tag); |
| 2040 | node = rb_next(node); |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2041 | if (entry_uid != uid_int) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2042 | continue; |
| 2043 | |
| 2044 | CT_DEBUG("qtaguid: ctrl_delete(%s): st tag=0x%llx (uid=%u)\n", |
| 2045 | input, st_entry->tag, entry_uid); |
| 2046 | |
| 2047 | if (!acct_tag || st_entry->tag == tag) { |
| 2048 | rb_erase(&st_entry->sock_node, &sock_tag_tree); |
| 2049 | /* Can't sockfd_put() within spinlock, do it later. */ |
| 2050 | sock_tag_tree_insert(st_entry, &st_to_free_tree); |
| 2051 | tr_entry = lookup_tag_ref(st_entry->tag, NULL); |
| 2052 | BUG_ON(tr_entry->num_sock_tags <= 0); |
| 2053 | tr_entry->num_sock_tags--; |
| 2054 | /* |
| 2055 | * TODO: remove if, and start failing. |
| 2056 | * This is a hack to work around the fact that in some |
| 2057 | * places we have "if (IS_ERR_OR_NULL(pqd_entry))" |
| 2058 | * and are trying to work around apps |
| 2059 | * that didn't open the /dev/xt_qtaguid. |
| 2060 | */ |
| 2061 | if (st_entry->list.next && st_entry->list.prev) |
| 2062 | list_del(&st_entry->list); |
| 2063 | } |
| 2064 | } |
| 2065 | spin_unlock_bh(&sock_tag_list_lock); |
| 2066 | |
| 2067 | sock_tag_tree_erase(&st_to_free_tree); |
| 2068 | |
| 2069 | /* Delete tag counter-sets */ |
| 2070 | spin_lock_bh(&tag_counter_set_list_lock); |
| 2071 | /* Counter sets are only on the uid tag, not full tag */ |
| 2072 | tcs_entry = tag_counter_set_tree_search(&tag_counter_set_tree, tag); |
| 2073 | if (tcs_entry) { |
| 2074 | CT_DEBUG("qtaguid: ctrl_delete(%s): " |
| 2075 | "erase tcs: tag=0x%llx (uid=%u) set=%d\n", |
| 2076 | input, |
| 2077 | tcs_entry->tn.tag, |
| 2078 | get_uid_from_tag(tcs_entry->tn.tag), |
| 2079 | tcs_entry->active_set); |
| 2080 | rb_erase(&tcs_entry->tn.node, &tag_counter_set_tree); |
| 2081 | kfree(tcs_entry); |
| 2082 | } |
| 2083 | spin_unlock_bh(&tag_counter_set_list_lock); |
| 2084 | |
| 2085 | /* |
| 2086 | * If acct_tag is 0, then all entries belonging to uid are |
| 2087 | * erased. |
| 2088 | */ |
| 2089 | spin_lock_bh(&iface_stat_list_lock); |
| 2090 | list_for_each_entry(iface_entry, &iface_stat_list, list) { |
| 2091 | spin_lock_bh(&iface_entry->tag_stat_list_lock); |
| 2092 | node = rb_first(&iface_entry->tag_stat_tree); |
| 2093 | while (node) { |
| 2094 | ts_entry = rb_entry(node, struct tag_stat, tn.node); |
| 2095 | entry_uid = get_uid_from_tag(ts_entry->tn.tag); |
| 2096 | node = rb_next(node); |
| 2097 | |
| 2098 | CT_DEBUG("qtaguid: ctrl_delete(%s): " |
| 2099 | "ts tag=0x%llx (uid=%u)\n", |
| 2100 | input, ts_entry->tn.tag, entry_uid); |
| 2101 | |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2102 | if (entry_uid != uid_int) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2103 | continue; |
| 2104 | if (!acct_tag || ts_entry->tn.tag == tag) { |
| 2105 | CT_DEBUG("qtaguid: ctrl_delete(%s): " |
| 2106 | "erase ts: %s 0x%llx %u\n", |
| 2107 | input, iface_entry->ifname, |
| 2108 | get_atag_from_tag(ts_entry->tn.tag), |
| 2109 | entry_uid); |
| 2110 | rb_erase(&ts_entry->tn.node, |
| 2111 | &iface_entry->tag_stat_tree); |
| 2112 | kfree(ts_entry); |
| 2113 | } |
| 2114 | } |
| 2115 | spin_unlock_bh(&iface_entry->tag_stat_list_lock); |
| 2116 | } |
| 2117 | spin_unlock_bh(&iface_stat_list_lock); |
| 2118 | |
| 2119 | /* Cleanup the uid_tag_data */ |
| 2120 | spin_lock_bh(&uid_tag_data_tree_lock); |
| 2121 | node = rb_first(&uid_tag_data_tree); |
| 2122 | while (node) { |
| 2123 | utd_entry = rb_entry(node, struct uid_tag_data, node); |
| 2124 | entry_uid = utd_entry->uid; |
| 2125 | node = rb_next(node); |
| 2126 | |
| 2127 | CT_DEBUG("qtaguid: ctrl_delete(%s): " |
| 2128 | "utd uid=%u\n", |
| 2129 | input, entry_uid); |
| 2130 | |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2131 | if (entry_uid != uid_int) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2132 | continue; |
| 2133 | /* |
| 2134 | * Go over the tag_refs, and those that don't have |
| 2135 | * sock_tags using them are freed. |
| 2136 | */ |
| 2137 | put_tag_ref_tree(tag, utd_entry); |
| 2138 | put_utd_entry(utd_entry); |
| 2139 | } |
| 2140 | spin_unlock_bh(&uid_tag_data_tree_lock); |
| 2141 | |
| 2142 | atomic64_inc(&qtu_events.delete_cmds); |
| 2143 | res = 0; |
| 2144 | |
| 2145 | err: |
| 2146 | return res; |
| 2147 | } |
| 2148 | |
| 2149 | static int ctrl_cmd_counter_set(const char *input) |
| 2150 | { |
| 2151 | char cmd; |
| 2152 | uid_t uid = 0; |
| 2153 | tag_t tag; |
| 2154 | int res, argc; |
| 2155 | struct tag_counter_set *tcs; |
| 2156 | int counter_set; |
| 2157 | |
| 2158 | argc = sscanf(input, "%c %d %u", &cmd, &counter_set, &uid); |
| 2159 | CT_DEBUG("qtaguid: ctrl_counterset(%s): argc=%d cmd=%c " |
| 2160 | "set=%d uid=%u\n", input, argc, cmd, |
| 2161 | counter_set, uid); |
| 2162 | if (argc != 3) { |
| 2163 | res = -EINVAL; |
| 2164 | goto err; |
| 2165 | } |
| 2166 | if (counter_set < 0 || counter_set >= IFS_MAX_COUNTER_SETS) { |
| 2167 | pr_info("qtaguid: ctrl_counterset(%s): invalid counter_set range\n", |
| 2168 | input); |
| 2169 | res = -EINVAL; |
| 2170 | goto err; |
| 2171 | } |
| 2172 | if (!can_manipulate_uids()) { |
| 2173 | pr_info("qtaguid: ctrl_counterset(%s): " |
| 2174 | "insufficient priv from pid=%u tgid=%u uid=%u\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2175 | input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2176 | res = -EPERM; |
| 2177 | goto err; |
| 2178 | } |
| 2179 | |
| 2180 | tag = make_tag_from_uid(uid); |
| 2181 | spin_lock_bh(&tag_counter_set_list_lock); |
| 2182 | tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag); |
| 2183 | if (!tcs) { |
| 2184 | tcs = kzalloc(sizeof(*tcs), GFP_ATOMIC); |
| 2185 | if (!tcs) { |
| 2186 | spin_unlock_bh(&tag_counter_set_list_lock); |
| 2187 | pr_err("qtaguid: ctrl_counterset(%s): " |
| 2188 | "failed to alloc counter set\n", |
| 2189 | input); |
| 2190 | res = -ENOMEM; |
| 2191 | goto err; |
| 2192 | } |
| 2193 | tcs->tn.tag = tag; |
| 2194 | tag_counter_set_tree_insert(tcs, &tag_counter_set_tree); |
| 2195 | CT_DEBUG("qtaguid: ctrl_counterset(%s): added tcs tag=0x%llx " |
| 2196 | "(uid=%u) set=%d\n", |
| 2197 | input, tag, get_uid_from_tag(tag), counter_set); |
| 2198 | } |
| 2199 | tcs->active_set = counter_set; |
| 2200 | spin_unlock_bh(&tag_counter_set_list_lock); |
| 2201 | atomic64_inc(&qtu_events.counter_set_changes); |
| 2202 | res = 0; |
| 2203 | |
| 2204 | err: |
| 2205 | return res; |
| 2206 | } |
| 2207 | |
| 2208 | static int ctrl_cmd_tag(const char *input) |
| 2209 | { |
| 2210 | char cmd; |
| 2211 | int sock_fd = 0; |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2212 | kuid_t uid; |
| 2213 | unsigned int uid_int = 0; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2214 | tag_t acct_tag = make_atag_from_value(0); |
| 2215 | tag_t full_tag; |
| 2216 | struct socket *el_socket; |
| 2217 | int res, argc; |
| 2218 | struct sock_tag *sock_tag_entry; |
| 2219 | struct tag_ref *tag_ref_entry; |
| 2220 | struct uid_tag_data *uid_tag_data_entry; |
| 2221 | struct proc_qtu_data *pqd_entry; |
| 2222 | |
| 2223 | /* Unassigned args will get defaulted later. */ |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2224 | argc = sscanf(input, "%c %d %llu %u", &cmd, &sock_fd, &acct_tag, &uid_int); |
| 2225 | uid = make_kuid(&init_user_ns, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2226 | CT_DEBUG("qtaguid: ctrl_tag(%s): argc=%d cmd=%c sock_fd=%d " |
| 2227 | "acct_tag=0x%llx uid=%u\n", input, argc, cmd, sock_fd, |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2228 | acct_tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2229 | if (argc < 2) { |
| 2230 | res = -EINVAL; |
| 2231 | goto err; |
| 2232 | } |
| 2233 | el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */ |
| 2234 | if (!el_socket) { |
| 2235 | pr_info("qtaguid: ctrl_tag(%s): failed to lookup" |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 2236 | " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n", |
| 2237 | input, sock_fd, res, current->pid, current->tgid, |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2238 | from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2239 | goto err; |
| 2240 | } |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2241 | CT_DEBUG("qtaguid: ctrl_tag(%s): socket->...->sk_refcnt=%d ->sk=%p\n", |
| 2242 | input, atomic_read(&el_socket->sk->sk_refcnt), |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2243 | el_socket->sk); |
| 2244 | if (argc < 3) { |
| 2245 | acct_tag = make_atag_from_value(0); |
| 2246 | } else if (!valid_atag(acct_tag)) { |
| 2247 | pr_info("qtaguid: ctrl_tag(%s): invalid tag\n", input); |
| 2248 | res = -EINVAL; |
| 2249 | goto err_put; |
| 2250 | } |
| 2251 | CT_DEBUG("qtaguid: ctrl_tag(%s): " |
| 2252 | "pid=%u tgid=%u uid=%u euid=%u fsuid=%u " |
JP Abgrall | 90414bc | 2013-01-04 18:18:36 -0800 | [diff] [blame] | 2253 | "ctrl.gid=%u in_group()=%d in_egroup()=%d\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2254 | input, current->pid, current->tgid, |
| 2255 | from_kuid(&init_user_ns, current_uid()), |
| 2256 | from_kuid(&init_user_ns, current_euid()), |
| 2257 | from_kuid(&init_user_ns, current_fsuid()), |
| 2258 | from_kgid(&init_user_ns, xt_qtaguid_ctrl_file->gid), |
JP Abgrall | 90414bc | 2013-01-04 18:18:36 -0800 | [diff] [blame] | 2259 | in_group_p(xt_qtaguid_ctrl_file->gid), |
| 2260 | in_egroup_p(xt_qtaguid_ctrl_file->gid)); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2261 | if (argc < 4) { |
| 2262 | uid = current_fsuid(); |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2263 | uid_int = from_kuid(&init_user_ns, uid); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2264 | } else if (!can_impersonate_uid(uid)) { |
| 2265 | pr_info("qtaguid: ctrl_tag(%s): " |
| 2266 | "insufficient priv from pid=%u tgid=%u uid=%u\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2267 | input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2268 | res = -EPERM; |
| 2269 | goto err_put; |
| 2270 | } |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2271 | full_tag = combine_atag_with_uid(acct_tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2272 | |
| 2273 | spin_lock_bh(&sock_tag_list_lock); |
| 2274 | sock_tag_entry = get_sock_stat_nl(el_socket->sk); |
| 2275 | tag_ref_entry = get_tag_ref(full_tag, &uid_tag_data_entry); |
| 2276 | if (IS_ERR(tag_ref_entry)) { |
| 2277 | res = PTR_ERR(tag_ref_entry); |
| 2278 | spin_unlock_bh(&sock_tag_list_lock); |
| 2279 | goto err_put; |
| 2280 | } |
| 2281 | tag_ref_entry->num_sock_tags++; |
| 2282 | if (sock_tag_entry) { |
| 2283 | struct tag_ref *prev_tag_ref_entry; |
| 2284 | |
| 2285 | CT_DEBUG("qtaguid: ctrl_tag(%s): retag for sk=%p " |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2286 | "st@%p ...->sk_refcnt=%d\n", |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2287 | input, el_socket->sk, sock_tag_entry, |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2288 | atomic_read(&el_socket->sk->sk_refcnt)); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2289 | prev_tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag, |
| 2290 | &uid_tag_data_entry); |
| 2291 | BUG_ON(IS_ERR_OR_NULL(prev_tag_ref_entry)); |
| 2292 | BUG_ON(prev_tag_ref_entry->num_sock_tags <= 0); |
| 2293 | prev_tag_ref_entry->num_sock_tags--; |
| 2294 | sock_tag_entry->tag = full_tag; |
| 2295 | } else { |
| 2296 | CT_DEBUG("qtaguid: ctrl_tag(%s): newtag for sk=%p\n", |
| 2297 | input, el_socket->sk); |
| 2298 | sock_tag_entry = kzalloc(sizeof(*sock_tag_entry), |
| 2299 | GFP_ATOMIC); |
| 2300 | if (!sock_tag_entry) { |
| 2301 | pr_err("qtaguid: ctrl_tag(%s): " |
| 2302 | "socket tag alloc failed\n", |
| 2303 | input); |
| 2304 | spin_unlock_bh(&sock_tag_list_lock); |
| 2305 | res = -ENOMEM; |
| 2306 | goto err_tag_unref_put; |
| 2307 | } |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2308 | /* |
| 2309 | * Hold the sk refcount here to make sure the sk pointer cannot |
| 2310 | * be freed and reused |
| 2311 | */ |
| 2312 | sock_hold(el_socket->sk); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2313 | sock_tag_entry->sk = el_socket->sk; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2314 | sock_tag_entry->pid = current->tgid; |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2315 | sock_tag_entry->tag = combine_atag_with_uid(acct_tag, uid_int); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2316 | spin_lock_bh(&uid_tag_data_tree_lock); |
| 2317 | pqd_entry = proc_qtu_data_tree_search( |
| 2318 | &proc_qtu_data_tree, current->tgid); |
| 2319 | /* |
| 2320 | * TODO: remove if, and start failing. |
| 2321 | * At first, we want to catch user-space code that is not |
| 2322 | * opening the /dev/xt_qtaguid. |
| 2323 | */ |
| 2324 | if (IS_ERR_OR_NULL(pqd_entry)) |
| 2325 | pr_warn_once( |
| 2326 | "qtaguid: %s(): " |
| 2327 | "User space forgot to open /dev/xt_qtaguid? " |
| 2328 | "pid=%u tgid=%u uid=%u\n", __func__, |
| 2329 | current->pid, current->tgid, |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2330 | from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2331 | else |
| 2332 | list_add(&sock_tag_entry->list, |
| 2333 | &pqd_entry->sock_tag_list); |
| 2334 | spin_unlock_bh(&uid_tag_data_tree_lock); |
| 2335 | |
| 2336 | sock_tag_tree_insert(sock_tag_entry, &sock_tag_tree); |
| 2337 | atomic64_inc(&qtu_events.sockets_tagged); |
| 2338 | } |
| 2339 | spin_unlock_bh(&sock_tag_list_lock); |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2340 | /* We keep the ref to the sk until it is untagged */ |
| 2341 | CT_DEBUG("qtaguid: ctrl_tag(%s): done st@%p ...->sk_refcnt=%d\n", |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2342 | input, sock_tag_entry, |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2343 | atomic_read(&el_socket->sk->sk_refcnt)); |
| 2344 | sockfd_put(el_socket); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2345 | return 0; |
| 2346 | |
| 2347 | err_tag_unref_put: |
| 2348 | BUG_ON(tag_ref_entry->num_sock_tags <= 0); |
| 2349 | tag_ref_entry->num_sock_tags--; |
| 2350 | free_tag_ref_from_utd_entry(tag_ref_entry, uid_tag_data_entry); |
| 2351 | err_put: |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2352 | CT_DEBUG("qtaguid: ctrl_tag(%s): done. ...->sk_refcnt=%d\n", |
| 2353 | input, atomic_read(&el_socket->sk->sk_refcnt) - 1); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2354 | /* Release the sock_fd that was grabbed by sockfd_lookup(). */ |
| 2355 | sockfd_put(el_socket); |
| 2356 | return res; |
| 2357 | |
| 2358 | err: |
| 2359 | CT_DEBUG("qtaguid: ctrl_tag(%s): done.\n", input); |
| 2360 | return res; |
| 2361 | } |
| 2362 | |
| 2363 | static int ctrl_cmd_untag(const char *input) |
| 2364 | { |
| 2365 | char cmd; |
| 2366 | int sock_fd = 0; |
| 2367 | struct socket *el_socket; |
| 2368 | int res, argc; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2369 | |
| 2370 | argc = sscanf(input, "%c %d", &cmd, &sock_fd); |
| 2371 | CT_DEBUG("qtaguid: ctrl_untag(%s): argc=%d cmd=%c sock_fd=%d\n", |
| 2372 | input, argc, cmd, sock_fd); |
| 2373 | if (argc < 2) { |
| 2374 | res = -EINVAL; |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2375 | return res; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2376 | } |
| 2377 | el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */ |
| 2378 | if (!el_socket) { |
| 2379 | pr_info("qtaguid: ctrl_untag(%s): failed to lookup" |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 2380 | " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n", |
| 2381 | input, sock_fd, res, current->pid, current->tgid, |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2382 | from_kuid(&init_user_ns, current_fsuid())); |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2383 | return res; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2384 | } |
| 2385 | CT_DEBUG("qtaguid: ctrl_untag(%s): socket->...->f_count=%ld ->sk=%p\n", |
| 2386 | input, atomic_long_read(&el_socket->file->f_count), |
| 2387 | el_socket->sk); |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2388 | res = qtaguid_untag(el_socket, false); |
| 2389 | sockfd_put(el_socket); |
| 2390 | return res; |
| 2391 | } |
| 2392 | |
| 2393 | int qtaguid_untag(struct socket *el_socket, bool kernel) |
| 2394 | { |
| 2395 | int res; |
| 2396 | pid_t pid; |
| 2397 | struct sock_tag *sock_tag_entry; |
| 2398 | struct tag_ref *tag_ref_entry; |
| 2399 | struct uid_tag_data *utd_entry; |
| 2400 | struct proc_qtu_data *pqd_entry; |
| 2401 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2402 | spin_lock_bh(&sock_tag_list_lock); |
| 2403 | sock_tag_entry = get_sock_stat_nl(el_socket->sk); |
| 2404 | if (!sock_tag_entry) { |
| 2405 | spin_unlock_bh(&sock_tag_list_lock); |
| 2406 | res = -EINVAL; |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2407 | return res; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2408 | } |
| 2409 | /* |
| 2410 | * The socket already belongs to the current process |
| 2411 | * so it can do whatever it wants to it. |
| 2412 | */ |
| 2413 | rb_erase(&sock_tag_entry->sock_node, &sock_tag_tree); |
| 2414 | |
| 2415 | tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag, &utd_entry); |
| 2416 | BUG_ON(!tag_ref_entry); |
| 2417 | BUG_ON(tag_ref_entry->num_sock_tags <= 0); |
| 2418 | spin_lock_bh(&uid_tag_data_tree_lock); |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2419 | if (kernel) |
| 2420 | pid = sock_tag_entry->pid; |
| 2421 | else |
| 2422 | pid = current->tgid; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2423 | pqd_entry = proc_qtu_data_tree_search( |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2424 | &proc_qtu_data_tree, pid); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2425 | /* |
| 2426 | * TODO: remove if, and start failing. |
| 2427 | * At first, we want to catch user-space code that is not |
| 2428 | * opening the /dev/xt_qtaguid. |
| 2429 | */ |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2430 | if (IS_ERR_OR_NULL(pqd_entry) || !sock_tag_entry->list.next) { |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2431 | pr_warn_once("qtaguid: %s(): " |
| 2432 | "User space forgot to open /dev/xt_qtaguid? " |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2433 | "pid=%u tgid=%u sk_pid=%u, uid=%u\n", __func__, |
| 2434 | current->pid, current->tgid, sock_tag_entry->pid, |
| 2435 | from_kuid(&init_user_ns, current_fsuid())); |
| 2436 | } else { |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2437 | list_del(&sock_tag_entry->list); |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2438 | } |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2439 | spin_unlock_bh(&uid_tag_data_tree_lock); |
| 2440 | /* |
| 2441 | * We don't free tag_ref from the utd_entry here, |
| 2442 | * only during a cmd_delete(). |
| 2443 | */ |
| 2444 | tag_ref_entry->num_sock_tags--; |
| 2445 | spin_unlock_bh(&sock_tag_list_lock); |
| 2446 | /* |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2447 | * Release the sock_fd that was grabbed at tag time. |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2448 | */ |
Chenbo Feng | 875e526 | 2017-04-19 14:22:47 -0700 | [diff] [blame] | 2449 | sock_put(sock_tag_entry->sk); |
| 2450 | CT_DEBUG("qtaguid: done. st@%p ...->sk_refcnt=%d\n", |
| 2451 | sock_tag_entry, |
| 2452 | atomic_read(&el_socket->sk->sk_refcnt)); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2453 | |
| 2454 | kfree(sock_tag_entry); |
| 2455 | atomic64_inc(&qtu_events.sockets_untagged); |
| 2456 | |
| 2457 | return 0; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 2460 | static ssize_t qtaguid_ctrl_parse(const char *input, size_t count) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2461 | { |
| 2462 | char cmd; |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 2463 | ssize_t res; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2464 | |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 2465 | CT_DEBUG("qtaguid: ctrl(%s): pid=%u tgid=%u uid=%u\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2466 | input, current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | 9e0858c | 2012-04-27 12:57:39 -0700 | [diff] [blame] | 2467 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2468 | cmd = input[0]; |
| 2469 | /* Collect params for commands */ |
| 2470 | switch (cmd) { |
| 2471 | case 'd': |
| 2472 | res = ctrl_cmd_delete(input); |
| 2473 | break; |
| 2474 | |
| 2475 | case 's': |
| 2476 | res = ctrl_cmd_counter_set(input); |
| 2477 | break; |
| 2478 | |
| 2479 | case 't': |
| 2480 | res = ctrl_cmd_tag(input); |
| 2481 | break; |
| 2482 | |
| 2483 | case 'u': |
| 2484 | res = ctrl_cmd_untag(input); |
| 2485 | break; |
| 2486 | |
| 2487 | default: |
| 2488 | res = -EINVAL; |
| 2489 | goto err; |
| 2490 | } |
| 2491 | if (!res) |
| 2492 | res = count; |
| 2493 | err: |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 2494 | CT_DEBUG("qtaguid: ctrl(%s): res=%zd\n", input, res); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2495 | return res; |
| 2496 | } |
| 2497 | |
| 2498 | #define MAX_QTAGUID_CTRL_INPUT_LEN 255 |
Greg Hackmann | 85a2eb5 | 2014-02-24 09:39:46 -0800 | [diff] [blame] | 2499 | 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] | 2500 | size_t count, loff_t *offp) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2501 | { |
| 2502 | char input_buf[MAX_QTAGUID_CTRL_INPUT_LEN]; |
| 2503 | |
| 2504 | if (unlikely(module_passive)) |
| 2505 | return count; |
| 2506 | |
| 2507 | if (count >= MAX_QTAGUID_CTRL_INPUT_LEN) |
| 2508 | return -EINVAL; |
| 2509 | |
| 2510 | if (copy_from_user(input_buf, buffer, count)) |
| 2511 | return -EFAULT; |
| 2512 | |
| 2513 | input_buf[count] = '\0'; |
| 2514 | return qtaguid_ctrl_parse(input_buf, count); |
| 2515 | } |
| 2516 | |
| 2517 | struct proc_print_info { |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2518 | struct iface_stat *iface_entry; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2519 | int item_index; |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2520 | tag_t tag; /* tag found by reading to tag_pos */ |
| 2521 | off_t tag_pos; |
| 2522 | int tag_item_index; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2523 | }; |
| 2524 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2525 | static void pp_stats_header(struct seq_file *m) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2526 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2527 | seq_puts(m, |
| 2528 | "idx iface acct_tag_hex uid_tag_int cnt_set " |
| 2529 | "rx_bytes rx_packets " |
| 2530 | "tx_bytes tx_packets " |
| 2531 | "rx_tcp_bytes rx_tcp_packets " |
| 2532 | "rx_udp_bytes rx_udp_packets " |
| 2533 | "rx_other_bytes rx_other_packets " |
| 2534 | "tx_tcp_bytes tx_tcp_packets " |
| 2535 | "tx_udp_bytes tx_udp_packets " |
| 2536 | "tx_other_bytes tx_other_packets\n"); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2539 | static int pp_stats_line(struct seq_file *m, struct tag_stat *ts_entry, |
| 2540 | int cnt_set) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2541 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2542 | struct data_counters *cnts; |
| 2543 | tag_t tag = ts_entry->tn.tag; |
| 2544 | uid_t stat_uid = get_uid_from_tag(tag); |
| 2545 | struct proc_print_info *ppi = m->private; |
| 2546 | /* Detailed tags are not available to everybody */ |
Mohamad Ayyash | 8613d93 | 2016-05-11 13:18:35 -0700 | [diff] [blame] | 2547 | 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] | 2548 | CT_DEBUG("qtaguid: stats line: " |
| 2549 | "%s 0x%llx %u: insufficient priv " |
| 2550 | "from pid=%u tgid=%u uid=%u stats.gid=%u\n", |
| 2551 | ppi->iface_entry->ifname, |
| 2552 | get_atag_from_tag(tag), stat_uid, |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2553 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()), |
| 2554 | from_kgid(&init_user_ns,xt_qtaguid_stats_file->gid)); |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2555 | return 0; |
| 2556 | } |
| 2557 | ppi->item_index++; |
| 2558 | cnts = &ts_entry->counters; |
Amit Pundir | 5b5ab94 | 2015-10-01 10:44:36 +0530 | [diff] [blame] | 2559 | seq_printf(m, "%d %s 0x%llx %u %u " |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2560 | "%llu %llu " |
| 2561 | "%llu %llu " |
| 2562 | "%llu %llu " |
| 2563 | "%llu %llu " |
| 2564 | "%llu %llu " |
| 2565 | "%llu %llu " |
| 2566 | "%llu %llu " |
| 2567 | "%llu %llu\n", |
| 2568 | ppi->item_index, |
| 2569 | ppi->iface_entry->ifname, |
| 2570 | get_atag_from_tag(tag), |
| 2571 | stat_uid, |
| 2572 | cnt_set, |
| 2573 | dc_sum_bytes(cnts, cnt_set, IFS_RX), |
| 2574 | dc_sum_packets(cnts, cnt_set, IFS_RX), |
| 2575 | dc_sum_bytes(cnts, cnt_set, IFS_TX), |
| 2576 | dc_sum_packets(cnts, cnt_set, IFS_TX), |
| 2577 | cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes, |
| 2578 | cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets, |
| 2579 | cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes, |
| 2580 | cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets, |
| 2581 | cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes, |
| 2582 | cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets, |
| 2583 | cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes, |
| 2584 | cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets, |
| 2585 | cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes, |
| 2586 | cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets, |
| 2587 | cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes, |
| 2588 | cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets); |
Amit Pundir | 5b5ab94 | 2015-10-01 10:44:36 +0530 | [diff] [blame] | 2589 | return seq_has_overflowed(m) ? -ENOSPC : 1; |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | static bool pp_sets(struct seq_file *m, struct tag_stat *ts_entry) |
| 2593 | { |
| 2594 | int ret; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2595 | int counter_set; |
| 2596 | for (counter_set = 0; counter_set < IFS_MAX_COUNTER_SETS; |
| 2597 | counter_set++) { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2598 | ret = pp_stats_line(m, ts_entry, counter_set); |
| 2599 | if (ret < 0) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2600 | return false; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2601 | } |
| 2602 | return true; |
| 2603 | } |
| 2604 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2605 | static int qtaguid_stats_proc_iface_stat_ptr_valid(struct iface_stat *ptr) |
| 2606 | { |
| 2607 | struct iface_stat *iface_entry; |
| 2608 | |
| 2609 | if (!ptr) |
| 2610 | return false; |
| 2611 | |
| 2612 | list_for_each_entry(iface_entry, &iface_stat_list, list) |
| 2613 | if (iface_entry == ptr) |
| 2614 | return true; |
| 2615 | return false; |
| 2616 | } |
| 2617 | |
| 2618 | static void qtaguid_stats_proc_next_iface_entry(struct proc_print_info *ppi) |
| 2619 | { |
| 2620 | spin_unlock_bh(&ppi->iface_entry->tag_stat_list_lock); |
| 2621 | list_for_each_entry_continue(ppi->iface_entry, &iface_stat_list, list) { |
| 2622 | spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock); |
| 2623 | return; |
| 2624 | } |
| 2625 | ppi->iface_entry = NULL; |
| 2626 | } |
| 2627 | |
| 2628 | static void *qtaguid_stats_proc_next(struct seq_file *m, void *v, loff_t *pos) |
| 2629 | { |
| 2630 | struct proc_print_info *ppi = m->private; |
| 2631 | struct tag_stat *ts_entry; |
| 2632 | struct rb_node *node; |
| 2633 | |
| 2634 | if (!v) { |
| 2635 | pr_err("qtaguid: %s(): unexpected v: NULL\n", __func__); |
| 2636 | return NULL; |
| 2637 | } |
| 2638 | |
| 2639 | (*pos)++; |
| 2640 | |
| 2641 | if (!ppi->iface_entry || unlikely(module_passive)) |
| 2642 | return NULL; |
| 2643 | |
| 2644 | if (v == SEQ_START_TOKEN) |
| 2645 | node = rb_first(&ppi->iface_entry->tag_stat_tree); |
| 2646 | else |
| 2647 | node = rb_next(&((struct tag_stat *)v)->tn.node); |
| 2648 | |
| 2649 | while (!node) { |
| 2650 | qtaguid_stats_proc_next_iface_entry(ppi); |
| 2651 | if (!ppi->iface_entry) |
| 2652 | return NULL; |
| 2653 | node = rb_first(&ppi->iface_entry->tag_stat_tree); |
| 2654 | } |
| 2655 | |
| 2656 | ts_entry = rb_entry(node, struct tag_stat, tn.node); |
| 2657 | ppi->tag = ts_entry->tn.tag; |
| 2658 | ppi->tag_pos = *pos; |
| 2659 | ppi->tag_item_index = ppi->item_index; |
| 2660 | return ts_entry; |
| 2661 | } |
| 2662 | |
| 2663 | static void *qtaguid_stats_proc_start(struct seq_file *m, loff_t *pos) |
| 2664 | { |
| 2665 | struct proc_print_info *ppi = m->private; |
| 2666 | struct tag_stat *ts_entry = NULL; |
| 2667 | |
| 2668 | spin_lock_bh(&iface_stat_list_lock); |
| 2669 | |
| 2670 | if (*pos == 0) { |
| 2671 | ppi->item_index = 1; |
| 2672 | ppi->tag_pos = 0; |
| 2673 | if (list_empty(&iface_stat_list)) { |
| 2674 | ppi->iface_entry = NULL; |
| 2675 | } else { |
| 2676 | ppi->iface_entry = list_first_entry(&iface_stat_list, |
| 2677 | struct iface_stat, |
| 2678 | list); |
| 2679 | spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock); |
| 2680 | } |
| 2681 | return SEQ_START_TOKEN; |
| 2682 | } |
| 2683 | if (!qtaguid_stats_proc_iface_stat_ptr_valid(ppi->iface_entry)) { |
| 2684 | if (ppi->iface_entry) { |
| 2685 | pr_err("qtaguid: %s(): iface_entry %p not found\n", |
| 2686 | __func__, ppi->iface_entry); |
| 2687 | ppi->iface_entry = NULL; |
| 2688 | } |
| 2689 | return NULL; |
| 2690 | } |
| 2691 | |
| 2692 | spin_lock_bh(&ppi->iface_entry->tag_stat_list_lock); |
| 2693 | |
| 2694 | if (!ppi->tag_pos) { |
| 2695 | /* seq_read skipped first next call */ |
| 2696 | ts_entry = SEQ_START_TOKEN; |
| 2697 | } else { |
| 2698 | ts_entry = tag_stat_tree_search( |
| 2699 | &ppi->iface_entry->tag_stat_tree, ppi->tag); |
| 2700 | if (!ts_entry) { |
| 2701 | pr_info("qtaguid: %s(): tag_stat.tag 0x%llx not found. Abort.\n", |
| 2702 | __func__, ppi->tag); |
| 2703 | return NULL; |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | if (*pos == ppi->tag_pos) { /* normal resume */ |
| 2708 | ppi->item_index = ppi->tag_item_index; |
| 2709 | } else { |
| 2710 | /* seq_read skipped a next call */ |
| 2711 | *pos = ppi->tag_pos; |
| 2712 | ts_entry = qtaguid_stats_proc_next(m, ts_entry, pos); |
| 2713 | } |
| 2714 | |
| 2715 | return ts_entry; |
| 2716 | } |
| 2717 | |
| 2718 | static void qtaguid_stats_proc_stop(struct seq_file *m, void *v) |
| 2719 | { |
| 2720 | struct proc_print_info *ppi = m->private; |
| 2721 | if (ppi->iface_entry) |
| 2722 | spin_unlock_bh(&ppi->iface_entry->tag_stat_list_lock); |
| 2723 | spin_unlock_bh(&iface_stat_list_lock); |
| 2724 | } |
| 2725 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2726 | /* |
| 2727 | * Procfs reader to get all tag stats using style "1)" as described in |
| 2728 | * fs/proc/generic.c |
| 2729 | * Groups all protocols tx/rx bytes. |
| 2730 | */ |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2731 | static int qtaguid_stats_proc_show(struct seq_file *m, void *v) |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2732 | { |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2733 | struct tag_stat *ts_entry = v; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2734 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2735 | if (v == SEQ_START_TOKEN) |
| 2736 | pp_stats_header(m); |
| 2737 | else |
| 2738 | pp_sets(m, ts_entry); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2739 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2740 | return 0; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | /*------------------------------------------*/ |
| 2744 | static int qtudev_open(struct inode *inode, struct file *file) |
| 2745 | { |
| 2746 | struct uid_tag_data *utd_entry; |
| 2747 | struct proc_qtu_data *pqd_entry; |
| 2748 | struct proc_qtu_data *new_pqd_entry; |
| 2749 | int res; |
| 2750 | bool utd_entry_found; |
| 2751 | |
| 2752 | if (unlikely(qtu_proc_handling_passive)) |
| 2753 | return 0; |
| 2754 | |
| 2755 | DR_DEBUG("qtaguid: qtudev_open(): pid=%u tgid=%u uid=%u\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2756 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2757 | |
| 2758 | spin_lock_bh(&uid_tag_data_tree_lock); |
| 2759 | |
| 2760 | /* Look for existing uid data, or alloc one. */ |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2761 | 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] | 2762 | if (IS_ERR_OR_NULL(utd_entry)) { |
| 2763 | res = PTR_ERR(utd_entry); |
JP Abgrall | b79c36f1 | 2012-10-09 20:38:21 -0700 | [diff] [blame] | 2764 | goto err_unlock; |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | /* Look for existing PID based proc_data */ |
| 2768 | pqd_entry = proc_qtu_data_tree_search(&proc_qtu_data_tree, |
| 2769 | current->tgid); |
| 2770 | if (pqd_entry) { |
| 2771 | pr_err("qtaguid: qtudev_open(): %u/%u %u " |
| 2772 | "%s already opened\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2773 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()), |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2774 | QTU_DEV_NAME); |
| 2775 | res = -EBUSY; |
| 2776 | goto err_unlock_free_utd; |
| 2777 | } |
| 2778 | |
| 2779 | new_pqd_entry = kzalloc(sizeof(*new_pqd_entry), GFP_ATOMIC); |
| 2780 | if (!new_pqd_entry) { |
| 2781 | pr_err("qtaguid: qtudev_open(): %u/%u %u: " |
| 2782 | "proc data alloc failed\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2783 | current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid())); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2784 | res = -ENOMEM; |
| 2785 | goto err_unlock_free_utd; |
| 2786 | } |
| 2787 | new_pqd_entry->pid = current->tgid; |
| 2788 | INIT_LIST_HEAD(&new_pqd_entry->sock_tag_list); |
| 2789 | new_pqd_entry->parent_tag_data = utd_entry; |
| 2790 | utd_entry->num_pqd++; |
| 2791 | |
| 2792 | proc_qtu_data_tree_insert(new_pqd_entry, |
| 2793 | &proc_qtu_data_tree); |
| 2794 | |
| 2795 | spin_unlock_bh(&uid_tag_data_tree_lock); |
| 2796 | DR_DEBUG("qtaguid: tracking data for uid=%u in pqd=%p\n", |
John Stultz | bd1bca4 | 2014-03-28 16:23:48 -0700 | [diff] [blame] | 2797 | from_kuid(&init_user_ns, current_fsuid()), new_pqd_entry); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2798 | file->private_data = new_pqd_entry; |
| 2799 | return 0; |
| 2800 | |
| 2801 | err_unlock_free_utd: |
| 2802 | if (!utd_entry_found) { |
| 2803 | rb_erase(&utd_entry->node, &uid_tag_data_tree); |
| 2804 | kfree(utd_entry); |
| 2805 | } |
JP Abgrall | b79c36f1 | 2012-10-09 20:38:21 -0700 | [diff] [blame] | 2806 | err_unlock: |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2807 | spin_unlock_bh(&uid_tag_data_tree_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2808 | return res; |
| 2809 | } |
| 2810 | |
| 2811 | static int qtudev_release(struct inode *inode, struct file *file) |
| 2812 | { |
| 2813 | struct proc_qtu_data *pqd_entry = file->private_data; |
| 2814 | struct uid_tag_data *utd_entry = pqd_entry->parent_tag_data; |
| 2815 | struct sock_tag *st_entry; |
| 2816 | struct rb_root st_to_free_tree = RB_ROOT; |
| 2817 | struct list_head *entry, *next; |
| 2818 | struct tag_ref *tr; |
| 2819 | |
| 2820 | if (unlikely(qtu_proc_handling_passive)) |
| 2821 | return 0; |
| 2822 | |
| 2823 | /* |
| 2824 | * Do not trust the current->pid, it might just be a kworker cleaning |
| 2825 | * up after a dead proc. |
| 2826 | */ |
| 2827 | DR_DEBUG("qtaguid: qtudev_release(): " |
| 2828 | "pid=%u tgid=%u uid=%u " |
| 2829 | "pqd_entry=%p->pid=%u utd_entry=%p->active_tags=%d\n", |
| 2830 | current->pid, current->tgid, pqd_entry->parent_tag_data->uid, |
| 2831 | pqd_entry, pqd_entry->pid, utd_entry, |
| 2832 | utd_entry->num_active_tags); |
| 2833 | |
| 2834 | spin_lock_bh(&sock_tag_list_lock); |
| 2835 | spin_lock_bh(&uid_tag_data_tree_lock); |
| 2836 | |
| 2837 | list_for_each_safe(entry, next, &pqd_entry->sock_tag_list) { |
| 2838 | st_entry = list_entry(entry, struct sock_tag, list); |
| 2839 | DR_DEBUG("qtaguid: %s(): " |
| 2840 | "erase sock_tag=%p->sk=%p pid=%u tgid=%u uid=%u\n", |
| 2841 | __func__, |
| 2842 | st_entry, st_entry->sk, |
| 2843 | current->pid, current->tgid, |
| 2844 | pqd_entry->parent_tag_data->uid); |
| 2845 | |
| 2846 | utd_entry = uid_tag_data_tree_search( |
| 2847 | &uid_tag_data_tree, |
| 2848 | get_uid_from_tag(st_entry->tag)); |
| 2849 | BUG_ON(IS_ERR_OR_NULL(utd_entry)); |
| 2850 | DR_DEBUG("qtaguid: %s(): " |
| 2851 | "looking for tag=0x%llx in utd_entry=%p\n", __func__, |
| 2852 | st_entry->tag, utd_entry); |
| 2853 | tr = tag_ref_tree_search(&utd_entry->tag_ref_tree, |
| 2854 | st_entry->tag); |
| 2855 | BUG_ON(!tr); |
| 2856 | BUG_ON(tr->num_sock_tags <= 0); |
| 2857 | tr->num_sock_tags--; |
| 2858 | free_tag_ref_from_utd_entry(tr, utd_entry); |
| 2859 | |
| 2860 | rb_erase(&st_entry->sock_node, &sock_tag_tree); |
| 2861 | list_del(&st_entry->list); |
| 2862 | /* Can't sockfd_put() within spinlock, do it later. */ |
| 2863 | sock_tag_tree_insert(st_entry, &st_to_free_tree); |
| 2864 | |
| 2865 | /* |
| 2866 | * Try to free the utd_entry if no other proc_qtu_data is |
| 2867 | * using it (num_pqd is 0) and it doesn't have active tags |
| 2868 | * (num_active_tags is 0). |
| 2869 | */ |
| 2870 | put_utd_entry(utd_entry); |
| 2871 | } |
| 2872 | |
| 2873 | rb_erase(&pqd_entry->node, &proc_qtu_data_tree); |
| 2874 | BUG_ON(pqd_entry->parent_tag_data->num_pqd < 1); |
| 2875 | pqd_entry->parent_tag_data->num_pqd--; |
| 2876 | put_utd_entry(pqd_entry->parent_tag_data); |
| 2877 | kfree(pqd_entry); |
| 2878 | file->private_data = NULL; |
| 2879 | |
| 2880 | spin_unlock_bh(&uid_tag_data_tree_lock); |
| 2881 | spin_unlock_bh(&sock_tag_list_lock); |
| 2882 | |
| 2883 | |
| 2884 | sock_tag_tree_erase(&st_to_free_tree); |
| 2885 | |
Chenbo Feng | 64157f4 | 2017-03-23 13:51:24 -0700 | [diff] [blame] | 2886 | spin_lock_bh(&sock_tag_list_lock); |
| 2887 | prdebug_full_state_locked(0, "%s(): pid=%u tgid=%u", __func__, |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2888 | current->pid, current->tgid); |
Chenbo Feng | 64157f4 | 2017-03-23 13:51:24 -0700 | [diff] [blame] | 2889 | spin_unlock_bh(&sock_tag_list_lock); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2890 | return 0; |
| 2891 | } |
| 2892 | |
| 2893 | /*------------------------------------------*/ |
| 2894 | static const struct file_operations qtudev_fops = { |
| 2895 | .owner = THIS_MODULE, |
| 2896 | .open = qtudev_open, |
| 2897 | .release = qtudev_release, |
| 2898 | }; |
| 2899 | |
| 2900 | static struct miscdevice qtu_device = { |
| 2901 | .minor = MISC_DYNAMIC_MINOR, |
| 2902 | .name = QTU_DEV_NAME, |
| 2903 | .fops = &qtudev_fops, |
| 2904 | /* How sad it doesn't allow for defaults: .mode = S_IRUGO | S_IWUSR */ |
| 2905 | }; |
| 2906 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2907 | static const struct seq_operations proc_qtaguid_ctrl_seqops = { |
| 2908 | .start = qtaguid_ctrl_proc_start, |
| 2909 | .next = qtaguid_ctrl_proc_next, |
| 2910 | .stop = qtaguid_ctrl_proc_stop, |
| 2911 | .show = qtaguid_ctrl_proc_show, |
| 2912 | }; |
| 2913 | |
| 2914 | static int proc_qtaguid_ctrl_open(struct inode *inode, struct file *file) |
| 2915 | { |
| 2916 | return seq_open_private(file, &proc_qtaguid_ctrl_seqops, |
| 2917 | sizeof(struct proc_ctrl_print_info)); |
| 2918 | } |
| 2919 | |
| 2920 | static const struct file_operations proc_qtaguid_ctrl_fops = { |
| 2921 | .open = proc_qtaguid_ctrl_open, |
| 2922 | .read = seq_read, |
| 2923 | .write = qtaguid_ctrl_proc_write, |
| 2924 | .llseek = seq_lseek, |
Greg Hackmann | 5647291 | 2013-12-04 17:39:27 -0800 | [diff] [blame] | 2925 | .release = seq_release_private, |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2926 | }; |
| 2927 | |
| 2928 | static const struct seq_operations proc_qtaguid_stats_seqops = { |
| 2929 | .start = qtaguid_stats_proc_start, |
| 2930 | .next = qtaguid_stats_proc_next, |
| 2931 | .stop = qtaguid_stats_proc_stop, |
| 2932 | .show = qtaguid_stats_proc_show, |
| 2933 | }; |
| 2934 | |
| 2935 | static int proc_qtaguid_stats_open(struct inode *inode, struct file *file) |
| 2936 | { |
| 2937 | return seq_open_private(file, &proc_qtaguid_stats_seqops, |
| 2938 | sizeof(struct proc_print_info)); |
| 2939 | } |
| 2940 | |
| 2941 | static const struct file_operations proc_qtaguid_stats_fops = { |
| 2942 | .open = proc_qtaguid_stats_open, |
| 2943 | .read = seq_read, |
| 2944 | .llseek = seq_lseek, |
| 2945 | .release = seq_release_private, |
| 2946 | }; |
| 2947 | |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2948 | /*------------------------------------------*/ |
| 2949 | static int __init qtaguid_proc_register(struct proc_dir_entry **res_procdir) |
| 2950 | { |
| 2951 | int ret; |
| 2952 | *res_procdir = proc_mkdir(module_procdirname, init_net.proc_net); |
| 2953 | if (!*res_procdir) { |
| 2954 | pr_err("qtaguid: failed to create proc/.../xt_qtaguid\n"); |
| 2955 | ret = -ENOMEM; |
| 2956 | goto no_dir; |
| 2957 | } |
| 2958 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2959 | xt_qtaguid_ctrl_file = proc_create_data("ctrl", proc_ctrl_perms, |
| 2960 | *res_procdir, |
| 2961 | &proc_qtaguid_ctrl_fops, |
| 2962 | NULL); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2963 | if (!xt_qtaguid_ctrl_file) { |
| 2964 | pr_err("qtaguid: failed to create xt_qtaguid/ctrl " |
| 2965 | " file\n"); |
| 2966 | ret = -ENOMEM; |
| 2967 | goto no_ctrl_entry; |
| 2968 | } |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2969 | |
Arve Hjønnevåg | 287076e | 2013-05-13 20:45:02 -0700 | [diff] [blame] | 2970 | xt_qtaguid_stats_file = proc_create_data("stats", proc_stats_perms, |
| 2971 | *res_procdir, |
| 2972 | &proc_qtaguid_stats_fops, |
| 2973 | NULL); |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2974 | if (!xt_qtaguid_stats_file) { |
| 2975 | pr_err("qtaguid: failed to create xt_qtaguid/stats " |
| 2976 | "file\n"); |
| 2977 | ret = -ENOMEM; |
| 2978 | goto no_stats_entry; |
| 2979 | } |
JP Abgrall | baf0db4 | 2011-06-20 12:41:46 -0700 | [diff] [blame] | 2980 | /* |
| 2981 | * TODO: add support counter hacking |
| 2982 | * xt_qtaguid_stats_file->write_proc = qtaguid_stats_proc_write; |
| 2983 | */ |
| 2984 | return 0; |
| 2985 | |
| 2986 | no_stats_entry: |
| 2987 | remove_proc_entry("ctrl", *res_procdir); |
| 2988 | no_ctrl_entry: |
| 2989 | remove_proc_entry("xt_qtaguid", NULL); |
| 2990 | no_dir: |
| 2991 | return ret; |
| 2992 | } |
| 2993 | |
| 2994 | static struct xt_match qtaguid_mt_reg __read_mostly = { |
| 2995 | /* |
| 2996 | * This module masquerades as the "owner" module so that iptables |
| 2997 | * tools can deal with it. |
| 2998 | */ |
| 2999 | .name = "owner", |
| 3000 | .revision = 1, |
| 3001 | .family = NFPROTO_UNSPEC, |
| 3002 | .match = qtaguid_mt, |
| 3003 | .matchsize = sizeof(struct xt_qtaguid_match_info), |
| 3004 | .me = THIS_MODULE, |
| 3005 | }; |
| 3006 | |
| 3007 | static int __init qtaguid_mt_init(void) |
| 3008 | { |
| 3009 | if (qtaguid_proc_register(&xt_qtaguid_procdir) |
| 3010 | || iface_stat_init(xt_qtaguid_procdir) |
| 3011 | || xt_register_match(&qtaguid_mt_reg) |
| 3012 | || misc_register(&qtu_device)) |
| 3013 | return -1; |
| 3014 | return 0; |
| 3015 | } |
| 3016 | |
| 3017 | /* |
| 3018 | * TODO: allow unloading of the module. |
| 3019 | * For now stats are permanent. |
| 3020 | * Kconfig forces'y/n' and never an 'm'. |
| 3021 | */ |
| 3022 | |
| 3023 | module_init(qtaguid_mt_init); |
| 3024 | MODULE_AUTHOR("jpa <jpa@google.com>"); |
| 3025 | MODULE_DESCRIPTION("Xtables: socket owner+tag matching and associated stats"); |
| 3026 | MODULE_LICENSE("GPL"); |
| 3027 | MODULE_ALIAS("ipt_owner"); |
| 3028 | MODULE_ALIAS("ip6t_owner"); |
| 3029 | MODULE_ALIAS("ipt_qtaguid"); |
| 3030 | MODULE_ALIAS("ip6t_qtaguid"); |