GoogleGit

blob: 1b60fdd532d6e3f9a4b25f89fd75314a31d21038 [file] [log] [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. * There are run-time debug flags enabled via the debug_mask module param, or
  12. * via the DEFAULT_DEBUG_MASK. See xt_qtaguid_internal.h.
  13. */
  14. #define DEBUG
  15. #include <linux/file.h>
  16. #include <linux/inetdevice.h>
  17. #include <linux/module.h>
  18. #include <linux/netfilter/x_tables.h>
  19. #include <linux/netfilter/xt_qtaguid.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/workqueue.h>
  22. #include <net/addrconf.h>
  23. #include <net/sock.h>
  24. #include <net/tcp.h>
  25. #include <net/udp.h>
  26. #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
  27. #include <linux/netfilter_ipv6/ip6_tables.h>
  28. #endif
  29. #include <linux/netfilter/xt_socket.h>
  30. #include "xt_qtaguid_internal.h"
  31. #include "xt_qtaguid_print.h"
  32. /*
  33. * We only use the xt_socket funcs within a similar context to avoid unexpected
  34. * return values.
  35. */
  36. #define XT_SOCKET_SUPPORTED_HOOKS \
  37. ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_IN))
  38. static const char *module_procdirname = "xt_qtaguid";
  39. static struct proc_dir_entry *xt_qtaguid_procdir;
  40. static unsigned int proc_iface_perms = S_IRUGO;
  41. module_param_named(iface_perms, proc_iface_perms, uint, S_IRUGO | S_IWUSR);
  42. static struct proc_dir_entry *xt_qtaguid_stats_file;
  43. static unsigned int proc_stats_perms = S_IRUGO;
  44. module_param_named(stats_perms, proc_stats_perms, uint, S_IRUGO | S_IWUSR);
  45. static struct proc_dir_entry *xt_qtaguid_ctrl_file;
  46. /* Everybody can write. But proc_ctrl_write_limited is true by default which
  47. * limits what can be controlled. See the can_*() functions.
  48. */
  49. static unsigned int proc_ctrl_perms = S_IRUGO | S_IWUGO;
  50. module_param_named(ctrl_perms, proc_ctrl_perms, uint, S_IRUGO | S_IWUSR);
  51. /* Limited by default, so the gid of the ctrl and stats proc entries
  52. * will limit what can be done. See the can_*() functions.
  53. */
  54. static bool proc_stats_readall_limited = true;
  55. static bool proc_ctrl_write_limited = true;
  56. module_param_named(stats_readall_limited, proc_stats_readall_limited, bool,
  57. S_IRUGO | S_IWUSR);
  58. module_param_named(ctrl_write_limited, proc_ctrl_write_limited, bool,
  59. S_IRUGO | S_IWUSR);
  60. /*
  61. * Limit the number of active tags (via socket tags) for a given UID.
  62. * Multiple processes could share the UID.
  63. */
  64. static int max_sock_tags = DEFAULT_MAX_SOCK_TAGS;
  65. module_param(max_sock_tags, int, S_IRUGO | S_IWUSR);
  66. /*
  67. * After the kernel has initiallized this module, it is still possible
  68. * to make it passive.
  69. * Setting passive to Y:
  70. * - the iface stats handling will not act on notifications.
  71. * - iptables matches will never match.
  72. * - ctrl commands silently succeed.
  73. * - stats are always empty.
  74. * This is mostly usefull when a bug is suspected.
  75. */
  76. static bool module_passive;
  77. module_param_named(passive, module_passive, bool, S_IRUGO | S_IWUSR);
  78. /*
  79. * Control how qtaguid data is tracked per proc/uid.
  80. * Setting tag_tracking_passive to Y:
  81. * - don't create proc specific structs to track tags
  82. * - don't check that active tag stats exceed some limits.
  83. * - don't clean up socket tags on process exits.
  84. * This is mostly usefull when a bug is suspected.
  85. */
  86. static bool qtu_proc_handling_passive;
  87. module_param_named(tag_tracking_passive, qtu_proc_handling_passive, bool,
  88. S_IRUGO | S_IWUSR);
  89. #define QTU_DEV_NAME "xt_qtaguid"
  90. uint qtaguid_debug_mask = DEFAULT_DEBUG_MASK;
  91. module_param_named(debug_mask, qtaguid_debug_mask, uint, S_IRUGO | S_IWUSR);
  92. /*---------------------------------------------------------------------------*/
  93. static const char *iface_stat_procdirname = "iface_stat";
  94. static struct proc_dir_entry *iface_stat_procdir;
  95. /*
  96. * The iface_stat_all* will go away once userspace gets use to the new fields
  97. * that have a format line.
  98. */
  99. static const char *iface_stat_all_procfilename = "iface_stat_all";
  100. static struct proc_dir_entry *iface_stat_all_procfile;
  101. static const char *iface_stat_fmt_procfilename = "iface_stat_fmt";
  102. static struct proc_dir_entry *iface_stat_fmt_procfile;
  103. /*
  104. * Ordering of locks:
  105. * outer locks:
  106. * iface_stat_list_lock
  107. * sock_tag_list_lock
  108. * inner locks:
  109. * uid_tag_data_tree_lock
  110. * tag_counter_set_list_lock
  111. * Notice how sock_tag_list_lock is held sometimes when uid_tag_data_tree_lock
  112. * is acquired.
  113. *
  114. * Call tree with all lock holders as of 2012-04-27:
  115. *
  116. * iface_stat_fmt_proc_read()
  117. * iface_stat_list_lock
  118. * (struct iface_stat)
  119. *
  120. * qtaguid_ctrl_proc_read()
  121. * sock_tag_list_lock
  122. * (sock_tag_tree)
  123. * (struct proc_qtu_data->sock_tag_list)
  124. * prdebug_full_state()
  125. * sock_tag_list_lock
  126. * (sock_tag_tree)
  127. * uid_tag_data_tree_lock
  128. * (uid_tag_data_tree)
  129. * (proc_qtu_data_tree)
  130. * iface_stat_list_lock
  131. *
  132. * qtaguid_stats_proc_read()
  133. * iface_stat_list_lock
  134. * struct iface_stat->tag_stat_list_lock
  135. *
  136. * qtudev_open()
  137. * uid_tag_data_tree_lock
  138. *
  139. * qtudev_release()
  140. * sock_tag_data_list_lock
  141. * uid_tag_data_tree_lock
  142. * prdebug_full_state()
  143. * sock_tag_list_lock
  144. * uid_tag_data_tree_lock
  145. * iface_stat_list_lock
  146. *
  147. * iface_netdev_event_handler()
  148. * iface_stat_create()
  149. * iface_stat_list_lock
  150. * iface_stat_update()
  151. * iface_stat_list_lock
  152. *
  153. * iface_inetaddr_event_handler()
  154. * iface_stat_create()
  155. * iface_stat_list_lock
  156. * iface_stat_update()
  157. * iface_stat_list_lock
  158. *
  159. * iface_inet6addr_event_handler()
  160. * iface_stat_create_ipv6()
  161. * iface_stat_list_lock
  162. * iface_stat_update()
  163. * iface_stat_list_lock
  164. *
  165. * qtaguid_mt()
  166. * account_for_uid()
  167. * if_tag_stat_update()
  168. * get_sock_stat()
  169. * sock_tag_list_lock
  170. * struct iface_stat->tag_stat_list_lock
  171. * tag_stat_update()
  172. * get_active_counter_set()
  173. * tag_counter_set_list_lock
  174. * tag_stat_update()
  175. * get_active_counter_set()
  176. * tag_counter_set_list_lock
  177. *
  178. *
  179. * qtaguid_ctrl_parse()
  180. * ctrl_cmd_delete()
  181. * sock_tag_list_lock
  182. * tag_counter_set_list_lock
  183. * iface_stat_list_lock
  184. * struct iface_stat->tag_stat_list_lock
  185. * uid_tag_data_tree_lock
  186. * ctrl_cmd_counter_set()
  187. * tag_counter_set_list_lock
  188. * ctrl_cmd_tag()
  189. * sock_tag_list_lock
  190. * (sock_tag_tree)
  191. * get_tag_ref()
  192. * uid_tag_data_tree_lock
  193. * (uid_tag_data_tree)
  194. * uid_tag_data_tree_lock
  195. * (proc_qtu_data_tree)
  196. * ctrl_cmd_untag()
  197. * sock_tag_list_lock
  198. * uid_tag_data_tree_lock
  199. *
  200. */
  201. static LIST_HEAD(iface_stat_list);
  202. static DEFINE_SPINLOCK(iface_stat_list_lock);
  203. static struct rb_root sock_tag_tree = RB_ROOT;
  204. static DEFINE_SPINLOCK(sock_tag_list_lock);
  205. static struct rb_root tag_counter_set_tree = RB_ROOT;
  206. static DEFINE_SPINLOCK(tag_counter_set_list_lock);
  207. static struct rb_root uid_tag_data_tree = RB_ROOT;
  208. static DEFINE_SPINLOCK(uid_tag_data_tree_lock);
  209. static struct rb_root proc_qtu_data_tree = RB_ROOT;
  210. /* No proc_qtu_data_tree_lock; use uid_tag_data_tree_lock */
  211. static struct qtaguid_event_counts qtu_events;
  212. /*----------------------------------------------*/
  213. static bool can_manipulate_uids(void)
  214. {
  215. /* root pwnd */
  216. return in_egroup_p(xt_qtaguid_ctrl_file->gid)
  217. || unlikely(!current_fsuid()) || unlikely(!proc_ctrl_write_limited)
  218. || unlikely(current_fsuid() == xt_qtaguid_ctrl_file->uid);
  219. }
  220. static bool can_impersonate_uid(uid_t uid)
  221. {
  222. return uid == current_fsuid() || can_manipulate_uids();
  223. }
  224. static bool can_read_other_uid_stats(uid_t uid)
  225. {
  226. /* root pwnd */
  227. return in_egroup_p(xt_qtaguid_stats_file->gid)
  228. || unlikely(!current_fsuid()) || uid == current_fsuid()
  229. || unlikely(!proc_stats_readall_limited)
  230. || unlikely(current_fsuid() == xt_qtaguid_ctrl_file->uid);
  231. }
  232. static inline void dc_add_byte_packets(struct data_counters *counters, int set,
  233. enum ifs_tx_rx direction,
  234. enum ifs_proto ifs_proto,
  235. int bytes,
  236. int packets)
  237. {
  238. counters->bpc[set][direction][ifs_proto].bytes += bytes;
  239. counters->bpc[set][direction][ifs_proto].packets += packets;
  240. }
  241. static struct tag_node *tag_node_tree_search(struct rb_root *root, tag_t tag)
  242. {
  243. struct rb_node *node = root->rb_node;
  244. while (node) {
  245. struct tag_node *data = rb_entry(node, struct tag_node, node);
  246. int result;
  247. RB_DEBUG("qtaguid: tag_node_tree_search(0x%llx): "
  248. " node=%p data=%p\n", tag, node, data);
  249. result = tag_compare(tag, data->tag);
  250. RB_DEBUG("qtaguid: tag_node_tree_search(0x%llx): "
  251. " data.tag=0x%llx (uid=%u) res=%d\n",
  252. tag, data->tag, get_uid_from_tag(data->tag), result);
  253. if (result < 0)
  254. node = node->rb_left;
  255. else if (result > 0)
  256. node = node->rb_right;
  257. else
  258. return data;
  259. }
  260. return NULL;
  261. }
  262. static void tag_node_tree_insert(struct tag_node *data, struct rb_root *root)
  263. {
  264. struct rb_node **new = &(root->rb_node), *parent = NULL;
  265. /* Figure out where to put new node */
  266. while (*new) {
  267. struct tag_node *this = rb_entry(*new, struct tag_node,
  268. node);
  269. int result = tag_compare(data->tag, this->tag);
  270. RB_DEBUG("qtaguid: %s(): tag=0x%llx"
  271. " (uid=%u)\n", __func__,
  272. this->tag,
  273. get_uid_from_tag(this->tag));
  274. parent = *new;
  275. if (result < 0)
  276. new = &((*new)->rb_left);
  277. else if (result > 0)
  278. new = &((*new)->rb_right);
  279. else
  280. BUG();
  281. }
  282. /* Add new node and rebalance tree. */
  283. rb_link_node(&data->node, parent, new);
  284. rb_insert_color(&data->node, root);
  285. }
  286. static void tag_stat_tree_insert(struct tag_stat *data, struct rb_root *root)
  287. {
  288. tag_node_tree_insert(&data->tn, root);
  289. }
  290. static struct tag_stat *tag_stat_tree_search(struct rb_root *root, tag_t tag)
  291. {
  292. struct tag_node *node = tag_node_tree_search(root, tag);
  293. if (!node)
  294. return NULL;
  295. return rb_entry(&node->node, struct tag_stat, tn.node);
  296. }
  297. static void tag_counter_set_tree_insert(struct tag_counter_set *data,
  298. struct rb_root *root)
  299. {
  300. tag_node_tree_insert(&data->tn, root);
  301. }
  302. static struct tag_counter_set *tag_counter_set_tree_search(struct rb_root *root,
  303. tag_t tag)
  304. {
  305. struct tag_node *node = tag_node_tree_search(root, tag);
  306. if (!node)
  307. return NULL;
  308. return rb_entry(&node->node, struct tag_counter_set, tn.node);
  309. }
  310. static void tag_ref_tree_insert(struct tag_ref *data, struct rb_root *root)
  311. {
  312. tag_node_tree_insert(&data->tn, root);
  313. }
  314. static struct tag_ref *tag_ref_tree_search(struct rb_root *root, tag_t tag)
  315. {
  316. struct tag_node *node = tag_node_tree_search(root, tag);
  317. if (!node)
  318. return NULL;
  319. return rb_entry(&node->node, struct tag_ref, tn.node);
  320. }
  321. static struct sock_tag *sock_tag_tree_search(struct rb_root *root,
  322. const struct sock *sk)
  323. {
  324. struct rb_node *node = root->rb_node;
  325. while (node) {
  326. struct sock_tag *data = rb_entry(node, struct sock_tag,
  327. sock_node);
  328. if (sk < data->sk)
  329. node = node->rb_left;
  330. else if (sk > data->sk)
  331. node = node->rb_right;
  332. else
  333. return data;
  334. }
  335. return NULL;
  336. }
  337. static void sock_tag_tree_insert(struct sock_tag *data, struct rb_root *root)
  338. {
  339. struct rb_node **new = &(root->rb_node), *parent = NULL;
  340. /* Figure out where to put new node */
  341. while (*new) {
  342. struct sock_tag *this = rb_entry(*new, struct sock_tag,
  343. sock_node);
  344. parent = *new;
  345. if (data->sk < this->sk)
  346. new = &((*new)->rb_left);
  347. else if (data->sk > this->sk)
  348. new = &((*new)->rb_right);
  349. else
  350. BUG();
  351. }
  352. /* Add new node and rebalance tree. */
  353. rb_link_node(&data->sock_node, parent, new);
  354. rb_insert_color(&data->sock_node, root);
  355. }
  356. static void sock_tag_tree_erase(struct rb_root *st_to_free_tree)
  357. {
  358. struct rb_node *node;
  359. struct sock_tag *st_entry;
  360. node = rb_first(st_to_free_tree);
  361. while (node) {
  362. st_entry = rb_entry(node, struct sock_tag, sock_node);
  363. node = rb_next(node);
  364. CT_DEBUG("qtaguid: %s(): "
  365. "erase st: sk=%p tag=0x%llx (uid=%u)\n", __func__,
  366. st_entry->sk,
  367. st_entry->tag,
  368. get_uid_from_tag(st_entry->tag));
  369. rb_erase(&st_entry->sock_node, st_to_free_tree);
  370. sockfd_put(st_entry->socket);
  371. kfree(st_entry);
  372. }
  373. }
  374. static struct proc_qtu_data *proc_qtu_data_tree_search(struct rb_root *root,
  375. const pid_t pid)
  376. {
  377. struct rb_node *node = root->rb_node;
  378. while (node) {
  379. struct proc_qtu_data *data = rb_entry(node,
  380. struct proc_qtu_data,
  381. node);
  382. if (pid < data->pid)
  383. node = node->rb_left;
  384. else if (pid > data->pid)
  385. node = node->rb_right;
  386. else
  387. return data;
  388. }
  389. return NULL;
  390. }
  391. static void proc_qtu_data_tree_insert(struct proc_qtu_data *data,
  392. struct rb_root *root)
  393. {
  394. struct rb_node **new = &(root->rb_node), *parent = NULL;
  395. /* Figure out where to put new node */
  396. while (*new) {
  397. struct proc_qtu_data *this = rb_entry(*new,
  398. struct proc_qtu_data,
  399. node);
  400. parent = *new;
  401. if (data->pid < this->pid)
  402. new = &((*new)->rb_left);
  403. else if (data->pid > this->pid)
  404. new = &((*new)->rb_right);
  405. else
  406. BUG();
  407. }
  408. /* Add new node and rebalance tree. */
  409. rb_link_node(&data->node, parent, new);
  410. rb_insert_color(&data->node, root);
  411. }
  412. static void uid_tag_data_tree_insert(struct uid_tag_data *data,
  413. struct rb_root *root)
  414. {
  415. struct rb_node **new = &(root->rb_node), *parent = NULL;
  416. /* Figure out where to put new node */
  417. while (*new) {
  418. struct uid_tag_data *this = rb_entry(*new,
  419. struct uid_tag_data,
  420. node);
  421. parent = *new;
  422. if (data->uid < this->uid)
  423. new = &((*new)->rb_left);
  424. else if (data->uid > this->uid)
  425. new = &((*new)->rb_right);
  426. else
  427. BUG();
  428. }
  429. /* Add new node and rebalance tree. */
  430. rb_link_node(&data->node, parent, new);
  431. rb_insert_color(&data->node, root);
  432. }
  433. static struct uid_tag_data *uid_tag_data_tree_search(struct rb_root *root,
  434. uid_t uid)
  435. {
  436. struct rb_node *node = root->rb_node;
  437. while (node) {
  438. struct uid_tag_data *data = rb_entry(node,
  439. struct uid_tag_data,
  440. node);
  441. if (uid < data->uid)
  442. node = node->rb_left;
  443. else if (uid > data->uid)
  444. node = node->rb_right;
  445. else
  446. return data;
  447. }
  448. return NULL;
  449. }
  450. /*
  451. * Allocates a new uid_tag_data struct if needed.
  452. * Returns a pointer to the found or allocated uid_tag_data.
  453. * Returns a PTR_ERR on failures, and lock is not held.
  454. * If found is not NULL:
  455. * sets *found to true if not allocated.
  456. * sets *found to false if allocated.
  457. */
  458. struct uid_tag_data *get_uid_data(uid_t uid, bool *found_res)
  459. {
  460. struct uid_tag_data *utd_entry;
  461. /* Look for top level uid_tag_data for the UID */
  462. utd_entry = uid_tag_data_tree_search(&uid_tag_data_tree, uid);
  463. DR_DEBUG("qtaguid: get_uid_data(%u) utd=%p\n", uid, utd_entry);
  464. if (found_res)
  465. *found_res = utd_entry;
  466. if (utd_entry)
  467. return utd_entry;
  468. utd_entry = kzalloc(sizeof(*utd_entry), GFP_ATOMIC);
  469. if (!utd_entry) {
  470. pr_err("qtaguid: get_uid_data(%u): "
  471. "tag data alloc failed\n", uid);
  472. return ERR_PTR(-ENOMEM);
  473. }
  474. utd_entry->uid = uid;
  475. utd_entry->tag_ref_tree = RB_ROOT;
  476. uid_tag_data_tree_insert(utd_entry, &uid_tag_data_tree);
  477. DR_DEBUG("qtaguid: get_uid_data(%u) new utd=%p\n", uid, utd_entry);
  478. return utd_entry;
  479. }
  480. /* Never returns NULL. Either PTR_ERR or a valid ptr. */
  481. static struct tag_ref *new_tag_ref(tag_t new_tag,
  482. struct uid_tag_data *utd_entry)
  483. {
  484. struct tag_ref *tr_entry;
  485. int res;
  486. if (utd_entry->num_active_tags + 1 > max_sock_tags) {
  487. pr_info("qtaguid: new_tag_ref(0x%llx): "
  488. "tag ref alloc quota exceeded. max=%d\n",
  489. new_tag, max_sock_tags);
  490. res = -EMFILE;
  491. goto err_res;
  492. }
  493. tr_entry = kzalloc(sizeof(*tr_entry), GFP_ATOMIC);
  494. if (!tr_entry) {
  495. pr_err("qtaguid: new_tag_ref(0x%llx): "
  496. "tag ref alloc failed\n",
  497. new_tag);
  498. res = -ENOMEM;
  499. goto err_res;
  500. }
  501. tr_entry->tn.tag = new_tag;
  502. /* tr_entry->num_sock_tags handled by caller */
  503. utd_entry->num_active_tags++;
  504. tag_ref_tree_insert(tr_entry, &utd_entry->tag_ref_tree);
  505. DR_DEBUG("qtaguid: new_tag_ref(0x%llx): "
  506. " inserted new tag ref %p\n",
  507. new_tag, tr_entry);
  508. return tr_entry;
  509. err_res:
  510. return ERR_PTR(res);
  511. }
  512. static struct tag_ref *lookup_tag_ref(tag_t full_tag,
  513. struct uid_tag_data **utd_res)
  514. {
  515. struct uid_tag_data *utd_entry;
  516. struct tag_ref *tr_entry;
  517. bool found_utd;
  518. uid_t uid = get_uid_from_tag(full_tag);
  519. DR_DEBUG("qtaguid: lookup_tag_ref(tag=0x%llx (uid=%u))\n",
  520. full_tag, uid);
  521. utd_entry = get_uid_data(uid, &found_utd);
  522. if (IS_ERR_OR_NULL(utd_entry)) {
  523. if (utd_res)
  524. *utd_res = utd_entry;
  525. return NULL;
  526. }
  527. tr_entry = tag_ref_tree_search(&utd_entry->tag_ref_tree, full_tag);
  528. if (utd_res)
  529. *utd_res = utd_entry;
  530. DR_DEBUG("qtaguid: lookup_tag_ref(0x%llx) utd_entry=%p tr_entry=%p\n",
  531. full_tag, utd_entry, tr_entry);
  532. return tr_entry;
  533. }
  534. /* Never returns NULL. Either PTR_ERR or a valid ptr. */
  535. static struct tag_ref *get_tag_ref(tag_t full_tag,
  536. struct uid_tag_data **utd_res)
  537. {
  538. struct uid_tag_data *utd_entry;
  539. struct tag_ref *tr_entry;
  540. DR_DEBUG("qtaguid: get_tag_ref(0x%llx)\n",
  541. full_tag);
  542. spin_lock_bh(&uid_tag_data_tree_lock);
  543. tr_entry = lookup_tag_ref(full_tag, &utd_entry);
  544. BUG_ON(IS_ERR_OR_NULL(utd_entry));
  545. if (!tr_entry)
  546. tr_entry = new_tag_ref(full_tag, utd_entry);
  547. spin_unlock_bh(&uid_tag_data_tree_lock);
  548. if (utd_res)
  549. *utd_res = utd_entry;
  550. DR_DEBUG("qtaguid: get_tag_ref(0x%llx) utd=%p tr=%p\n",
  551. full_tag, utd_entry, tr_entry);
  552. return tr_entry;
  553. }
  554. /* Checks and maybe frees the UID Tag Data entry */
  555. static void put_utd_entry(struct uid_tag_data *utd_entry)
  556. {
  557. /* Are we done with the UID tag data entry? */
  558. if (RB_EMPTY_ROOT(&utd_entry->tag_ref_tree) &&
  559. !utd_entry->num_pqd) {
  560. DR_DEBUG("qtaguid: %s(): "
  561. "erase utd_entry=%p uid=%u "
  562. "by pid=%u tgid=%u uid=%u\n", __func__,
  563. utd_entry, utd_entry->uid,
  564. current->pid, current->tgid, current_fsuid());
  565. BUG_ON(utd_entry->num_active_tags);
  566. rb_erase(&utd_entry->node, &uid_tag_data_tree);
  567. kfree(utd_entry);
  568. } else {
  569. DR_DEBUG("qtaguid: %s(): "
  570. "utd_entry=%p still has %d tags %d proc_qtu_data\n",
  571. __func__, utd_entry, utd_entry->num_active_tags,
  572. utd_entry->num_pqd);
  573. BUG_ON(!(utd_entry->num_active_tags ||
  574. utd_entry->num_pqd));
  575. }
  576. }
  577. /*
  578. * If no sock_tags are using this tag_ref,
  579. * decrements refcount of utd_entry, removes tr_entry
  580. * from utd_entry->tag_ref_tree and frees.
  581. */
  582. static void free_tag_ref_from_utd_entry(struct tag_ref *tr_entry,
  583. struct uid_tag_data *utd_entry)
  584. {
  585. DR_DEBUG("qtaguid: %s(): %p tag=0x%llx (uid=%u)\n", __func__,
  586. tr_entry, tr_entry->tn.tag,
  587. get_uid_from_tag(tr_entry->tn.tag));
  588. if (!tr_entry->num_sock_tags) {
  589. BUG_ON(!utd_entry->num_active_tags);
  590. utd_entry->num_active_tags--;
  591. rb_erase(&tr_entry->tn.node, &utd_entry->tag_ref_tree);
  592. DR_DEBUG("qtaguid: %s(): erased %p\n", __func__, tr_entry);
  593. kfree(tr_entry);
  594. }
  595. }
  596. static void put_tag_ref_tree(tag_t full_tag, struct uid_tag_data *utd_entry)
  597. {
  598. struct rb_node *node;
  599. struct tag_ref *tr_entry;
  600. tag_t acct_tag;
  601. DR_DEBUG("qtaguid: %s(tag=0x%llx (uid=%u))\n", __func__,
  602. full_tag, get_uid_from_tag(full_tag));
  603. acct_tag = get_atag_from_tag(full_tag);
  604. node = rb_first(&utd_entry->tag_ref_tree);
  605. while (node) {
  606. tr_entry = rb_entry(node, struct tag_ref, tn.node);
  607. node = rb_next(node);
  608. if (!acct_tag || tr_entry->tn.tag == full_tag)
  609. free_tag_ref_from_utd_entry(tr_entry, utd_entry);
  610. }
  611. }
  612. static int read_proc_u64(char *page, char **start, off_t off,
  613. int count, int *eof, void *data)
  614. {
  615. int len;
  616. uint64_t value;
  617. char *p = page;
  618. uint64_t *iface_entry = data;
  619. if (!data)
  620. return 0;
  621. value = *iface_entry;
  622. p += sprintf(p, "%llu\n", value);
  623. len = (p - page) - off;
  624. *eof = (len <= count) ? 1 : 0;
  625. *start = page + off;
  626. return len;
  627. }
  628. static int read_proc_bool(char *page, char **start, off_t off,
  629. int count, int *eof, void *data)
  630. {
  631. int len;
  632. bool value;
  633. char *p = page;
  634. bool *bool_entry = data;
  635. if (!data)
  636. return 0;
  637. value = *bool_entry;
  638. p += sprintf(p, "%u\n", value);
  639. len = (p - page) - off;
  640. *eof = (len <= count) ? 1 : 0;
  641. *start = page + off;
  642. return len;
  643. }
  644. static int get_active_counter_set(tag_t tag)
  645. {
  646. int active_set = 0;
  647. struct tag_counter_set *tcs;
  648. MT_DEBUG("qtaguid: get_active_counter_set(tag=0x%llx)"
  649. " (uid=%u)\n",
  650. tag, get_uid_from_tag(tag));
  651. /* For now we only handle UID tags for active sets */
  652. tag = get_utag_from_tag(tag);
  653. spin_lock_bh(&tag_counter_set_list_lock);
  654. tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
  655. if (tcs)
  656. active_set = tcs->active_set;
  657. spin_unlock_bh(&tag_counter_set_list_lock);
  658. return active_set;
  659. }
  660. /*
  661. * Find the entry for tracking the specified interface.
  662. * Caller must hold iface_stat_list_lock
  663. */
  664. static struct iface_stat *get_iface_entry(const char *ifname)
  665. {
  666. struct iface_stat *iface_entry;
  667. /* Find the entry for tracking the specified tag within the interface */
  668. if (ifname == NULL) {
  669. pr_info("qtaguid: iface_stat: get() NULL device name\n");
  670. return NULL;
  671. }
  672. /* Iterate over interfaces */
  673. list_for_each_entry(iface_entry, &iface_stat_list, list) {
  674. if (!strcmp(ifname, iface_entry->ifname))
  675. goto done;
  676. }
  677. iface_entry = NULL;
  678. done:
  679. return iface_entry;
  680. }
  681. /* This is for fmt2 only */
  682. static int pp_iface_stat_line(bool header, char *outp,
  683. int char_count, struct iface_stat *iface_entry)
  684. {
  685. int len;
  686. if (header) {
  687. len = snprintf(outp, char_count,
  688. "ifname "
  689. "total_skb_rx_bytes total_skb_rx_packets "
  690. "total_skb_tx_bytes total_skb_tx_packets "
  691. "rx_tcp_bytes rx_tcp_packets "
  692. "rx_udp_bytes rx_udp_packets "
  693. "rx_other_bytes rx_other_packets "
  694. "tx_tcp_bytes tx_tcp_packets "
  695. "tx_udp_bytes tx_udp_packets "
  696. "tx_other_bytes tx_other_packets\n"
  697. );
  698. } else {
  699. struct data_counters *cnts;
  700. int cnt_set = 0; /* We only use one set for the device */
  701. cnts = &iface_entry->totals_via_skb;
  702. len = snprintf(
  703. outp, char_count,
  704. "%s "
  705. "%llu %llu %llu %llu %llu %llu %llu %llu "
  706. "%llu %llu %llu %llu %llu %llu %llu %llu\n",
  707. iface_entry->ifname,
  708. dc_sum_bytes(cnts, cnt_set, IFS_RX),
  709. dc_sum_packets(cnts, cnt_set, IFS_RX),
  710. dc_sum_bytes(cnts, cnt_set, IFS_TX),
  711. dc_sum_packets(cnts, cnt_set, IFS_TX),
  712. cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes,
  713. cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets,
  714. cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes,
  715. cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets,
  716. cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes,
  717. cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets,
  718. cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes,
  719. cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets,
  720. cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes,
  721. cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets,
  722. cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes,
  723. cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets);
  724. }
  725. return len;
  726. }
  727. static int iface_stat_fmt_proc_read(char *page, char **num_items_returned,
  728. off_t items_to_skip, int char_count,
  729. int *eof, void *data)
  730. {
  731. char *outp = page;
  732. int item_index = 0;
  733. int len;
  734. int fmt = (int)data; /* The data is just 1 (old) or 2 (uses fmt) */
  735. struct iface_stat *iface_entry;
  736. struct rtnl_link_stats64 dev_stats, *stats;
  737. struct rtnl_link_stats64 no_dev_stats = {0};
  738. if (unlikely(module_passive)) {
  739. *eof = 1;
  740. return 0;
  741. }
  742. CT_DEBUG("qtaguid:proc iface_stat_fmt "
  743. "pid=%u tgid=%u uid=%u "
  744. "page=%p *num_items_returned=%p off=%ld "
  745. "char_count=%d *eof=%d\n",
  746. current->pid, current->tgid, current_fsuid(),
  747. page, *num_items_returned,
  748. items_to_skip, char_count, *eof);
  749. if (*eof)
  750. return 0;
  751. if (fmt == 2 && item_index++ >= items_to_skip) {
  752. len = pp_iface_stat_line(true, outp, char_count, NULL);
  753. if (len >= char_count) {
  754. *outp = '\0';
  755. return outp - page;
  756. }
  757. outp += len;
  758. char_count -= len;
  759. (*num_items_returned)++;
  760. }
  761. /*
  762. * This lock will prevent iface_stat_update() from changing active,
  763. * and in turn prevent an interface from unregistering itself.
  764. */
  765. spin_lock_bh(&iface_stat_list_lock);
  766. list_for_each_entry(iface_entry, &iface_stat_list, list) {
  767. if (item_index++ < items_to_skip)
  768. continue;
  769. if (iface_entry->active) {
  770. stats = dev_get_stats(iface_entry->net_dev,
  771. &dev_stats);
  772. } else {
  773. stats = &no_dev_stats;
  774. }
  775. /*
  776. * If the meaning of the data changes, then update the fmtX
  777. * string.
  778. */
  779. if (fmt == 1) {
  780. len = snprintf(
  781. outp, char_count,
  782. "%s %d "
  783. "%llu %llu %llu %llu "
  784. "%llu %llu %llu %llu\n",
  785. iface_entry->ifname,
  786. iface_entry->active,
  787. iface_entry->totals_via_dev[IFS_RX].bytes,
  788. iface_entry->totals_via_dev[IFS_RX].packets,
  789. iface_entry->totals_via_dev[IFS_TX].bytes,
  790. iface_entry->totals_via_dev[IFS_TX].packets,
  791. stats->rx_bytes, stats->rx_packets,
  792. stats->tx_bytes, stats->tx_packets
  793. );
  794. } else {
  795. len = pp_iface_stat_line(false, outp, char_count,
  796. iface_entry);
  797. }
  798. if (len >= char_count) {
  799. spin_unlock_bh(&iface_stat_list_lock);
  800. *outp = '\0';
  801. return outp - page;
  802. }
  803. outp += len;
  804. char_count -= len;
  805. (*num_items_returned)++;
  806. }
  807. spin_unlock_bh(&iface_stat_list_lock);
  808. *eof = 1;
  809. return outp - page;
  810. }
  811. static void iface_create_proc_worker(struct work_struct *work)
  812. {
  813. struct proc_dir_entry *proc_entry;
  814. struct iface_stat_work *isw = container_of(work, struct iface_stat_work,
  815. iface_work);
  816. struct iface_stat *new_iface = isw->iface_entry;
  817. /* iface_entries are not deleted, so safe to manipulate. */
  818. proc_entry = proc_mkdir(new_iface->ifname, iface_stat_procdir);
  819. if (IS_ERR_OR_NULL(proc_entry)) {
  820. pr_err("qtaguid: iface_stat: create_proc(): alloc failed.\n");
  821. kfree(isw);
  822. return;
  823. }
  824. new_iface->proc_ptr = proc_entry;
  825. create_proc_read_entry("tx_bytes", proc_iface_perms, proc_entry,
  826. read_proc_u64,
  827. &new_iface->totals_via_dev[IFS_TX].bytes);
  828. create_proc_read_entry("rx_bytes", proc_iface_perms, proc_entry,
  829. read_proc_u64,
  830. &new_iface->totals_via_dev[IFS_RX].bytes);
  831. create_proc_read_entry("tx_packets", proc_iface_perms, proc_entry,
  832. read_proc_u64,
  833. &new_iface->totals_via_dev[IFS_TX].packets);
  834. create_proc_read_entry("rx_packets", proc_iface_perms, proc_entry,
  835. read_proc_u64,
  836. &new_iface->totals_via_dev[IFS_RX].packets);
  837. create_proc_read_entry("active", proc_iface_perms, proc_entry,
  838. read_proc_bool, &new_iface->active);
  839. IF_DEBUG("qtaguid: iface_stat: create_proc(): done "
  840. "entry=%p dev=%s\n", new_iface, new_iface->ifname);
  841. kfree(isw);
  842. }
  843. /*
  844. * Will set the entry's active state, and
  845. * update the net_dev accordingly also.
  846. */
  847. static void _iface_stat_set_active(struct iface_stat *entry,
  848. struct net_device *net_dev,
  849. bool activate)
  850. {
  851. if (activate) {
  852. entry->net_dev = net_dev;
  853. entry->active = true;
  854. IF_DEBUG("qtaguid: %s(%s): "
  855. "enable tracking. rfcnt=%d\n", __func__,
  856. entry->ifname,
  857. __this_cpu_read(*net_dev->pcpu_refcnt));
  858. } else {
  859. entry->active = false;
  860. entry->net_dev = NULL;
  861. IF_DEBUG("qtaguid: %s(%s): "
  862. "disable tracking. rfcnt=%d\n", __func__,
  863. entry->ifname,
  864. __this_cpu_read(*net_dev->pcpu_refcnt));
  865. }
  866. }
  867. /* Caller must hold iface_stat_list_lock */
  868. static struct iface_stat *iface_alloc(struct net_device *net_dev)
  869. {
  870. struct iface_stat *new_iface;
  871. struct iface_stat_work *isw;
  872. new_iface = kzalloc(sizeof(*new_iface), GFP_ATOMIC);
  873. if (new_iface == NULL) {
  874. pr_err("qtaguid: iface_stat: create(%s): "
  875. "iface_stat alloc failed\n", net_dev->name);
  876. return NULL;
  877. }
  878. new_iface->ifname = kstrdup(net_dev->name, GFP_ATOMIC);
  879. if (new_iface->ifname == NULL) {
  880. pr_err("qtaguid: iface_stat: create(%s): "
  881. "ifname alloc failed\n", net_dev->name);
  882. kfree(new_iface);
  883. return NULL;
  884. }
  885. spin_lock_init(&new_iface->tag_stat_list_lock);
  886. new_iface->tag_stat_tree = RB_ROOT;
  887. _iface_stat_set_active(new_iface, net_dev, true);
  888. /*
  889. * ipv6 notifier chains are atomic :(
  890. * No create_proc_read_entry() for you!
  891. */
  892. isw = kmalloc(sizeof(*isw), GFP_ATOMIC);
  893. if (!isw) {
  894. pr_err("qtaguid: iface_stat: create(%s): "
  895. "work alloc failed\n", new_iface->ifname);
  896. _iface_stat_set_active(new_iface, net_dev, false);
  897. kfree(new_iface->ifname);
  898. kfree(new_iface);
  899. return NULL;
  900. }
  901. isw->iface_entry = new_iface;
  902. INIT_WORK(&isw->iface_work, iface_create_proc_worker);
  903. schedule_work(&isw->iface_work);
  904. list_add(&new_iface->list, &iface_stat_list);
  905. return new_iface;
  906. }
  907. static void iface_check_stats_reset_and_adjust(struct net_device *net_dev,
  908. struct iface_stat *iface)
  909. {
  910. struct rtnl_link_stats64 dev_stats, *stats;
  911. bool stats_rewound;
  912. stats = dev_get_stats(net_dev, &dev_stats);
  913. /* No empty packets */
  914. stats_rewound =
  915. (stats->rx_bytes < iface->last_known[IFS_RX].bytes)
  916. || (stats->tx_bytes < iface->last_known[IFS_TX].bytes);
  917. IF_DEBUG("qtaguid: %s(%s): iface=%p netdev=%p "
  918. "bytes rx/tx=%llu/%llu "
  919. "active=%d last_known=%d "
  920. "stats_rewound=%d\n", __func__,
  921. net_dev ? net_dev->name : "?",
  922. iface, net_dev,
  923. stats->rx_bytes, stats->tx_bytes,
  924. iface->active, iface->last_known_valid, stats_rewound);
  925. if (iface->active && iface->last_known_valid && stats_rewound) {
  926. pr_warn_once("qtaguid: iface_stat: %s(%s): "
  927. "iface reset its stats unexpectedly\n", __func__,
  928. net_dev->name);
  929. iface->totals_via_dev[IFS_TX].bytes +=
  930. iface->last_known[IFS_TX].bytes;
  931. iface->totals_via_dev[IFS_TX].packets +=
  932. iface->last_known[IFS_TX].packets;
  933. iface->totals_via_dev[IFS_RX].bytes +=
  934. iface->last_known[IFS_RX].bytes;
  935. iface->totals_via_dev[IFS_RX].packets +=
  936. iface->last_known[IFS_RX].packets;
  937. iface->last_known_valid = false;
  938. IF_DEBUG("qtaguid: %s(%s): iface=%p "
  939. "used last known bytes rx/tx=%llu/%llu\n", __func__,
  940. iface->ifname, iface, iface->last_known[IFS_RX].bytes,
  941. iface->last_known[IFS_TX].bytes);
  942. }
  943. }
  944. /*
  945. * Create a new entry for tracking the specified interface.
  946. * Do nothing if the entry already exists.
  947. * Called when an interface is configured with a valid IP address.
  948. */
  949. static void iface_stat_create(struct net_device *net_dev,
  950. struct in_ifaddr *ifa)
  951. {
  952. struct in_device *in_dev = NULL;
  953. const char *ifname;
  954. struct iface_stat *entry;
  955. __be32 ipaddr = 0;
  956. struct iface_stat *new_iface;
  957. IF_DEBUG("qtaguid: iface_stat: create(%s): ifa=%p netdev=%p\n",
  958. net_dev ? net_dev->name : "?",
  959. ifa, net_dev);
  960. if (!net_dev) {
  961. pr_err("qtaguid: iface_stat: create(): no net dev\n");
  962. return;
  963. }
  964. ifname = net_dev->name;
  965. if (!ifa) {
  966. in_dev = in_dev_get(net_dev);
  967. if (!in_dev) {
  968. pr_err("qtaguid: iface_stat: create(%s): no inet dev\n",
  969. ifname);
  970. return;
  971. }
  972. IF_DEBUG("qtaguid: iface_stat: create(%s): in_dev=%p\n",
  973. ifname, in_dev);
  974. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  975. IF_DEBUG("qtaguid: iface_stat: create(%s): "
  976. "ifa=%p ifa_label=%s\n",
  977. ifname, ifa,
  978. ifa->ifa_label ? ifa->ifa_label : "(null)");
  979. if (ifa->ifa_label && !strcmp(ifname, ifa->ifa_label))
  980. break;
  981. }
  982. }
  983. if (!ifa) {
  984. IF_DEBUG("qtaguid: iface_stat: create(%s): no matching IP\n",
  985. ifname);
  986. goto done_put;
  987. }
  988. ipaddr = ifa->ifa_local;
  989. spin_lock_bh(&iface_stat_list_lock);
  990. entry = get_iface_entry(ifname);
  991. if (entry != NULL) {
  992. IF_DEBUG("qtaguid: iface_stat: create(%s): entry=%p\n",
  993. ifname, entry);
  994. iface_check_stats_reset_and_adjust(net_dev, entry);
  995. _iface_stat_set_active(entry, net_dev, true);
  996. IF_DEBUG("qtaguid: %s(%s): "
  997. "tracking now %d on ip=%pI4\n", __func__,
  998. entry->ifname, true, &ipaddr);
  999. goto done_unlock_put;
  1000. }
  1001. new_iface = iface_alloc(net_dev);
  1002. IF_DEBUG("qtaguid: iface_stat: create(%s): done "
  1003. "entry=%p ip=%pI4\n", ifname, new_iface, &ipaddr);
  1004. done_unlock_put:
  1005. spin_unlock_bh(&iface_stat_list_lock);
  1006. done_put:
  1007. if (in_dev)
  1008. in_dev_put(in_dev);
  1009. }
  1010. static void iface_stat_create_ipv6(struct net_device *net_dev,
  1011. struct inet6_ifaddr *ifa)
  1012. {
  1013. struct in_device *in_dev;
  1014. const char *ifname;
  1015. struct iface_stat *entry;
  1016. struct iface_stat *new_iface;
  1017. int addr_type;
  1018. IF_DEBUG("qtaguid: iface_stat: create6(): ifa=%p netdev=%p->name=%s\n",
  1019. ifa, net_dev, net_dev ? net_dev->name : "");
  1020. if (!net_dev) {
  1021. pr_err("qtaguid: iface_stat: create6(): no net dev!\n");
  1022. return;
  1023. }
  1024. ifname = net_dev->name;
  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. IF_DEBUG("qtaguid: iface_stat: create6(%s): in_dev=%p\n",
  1032. ifname, in_dev);
  1033. if (!ifa) {
  1034. IF_DEBUG("qtaguid: iface_stat: create6(%s): no matching IP\n",
  1035. ifname);
  1036. goto done_put;
  1037. }
  1038. addr_type = ipv6_addr_type(&ifa->addr);
  1039. spin_lock_bh(&iface_stat_list_lock);
  1040. entry = get_iface_entry(ifname);
  1041. if (entry != NULL) {
  1042. IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
  1043. ifname, entry);
  1044. iface_check_stats_reset_and_adjust(net_dev, entry);
  1045. _iface_stat_set_active(entry, net_dev, true);
  1046. IF_DEBUG("qtaguid: %s(%s): "
  1047. "tracking now %d on ip=%pI6c\n", __func__,
  1048. entry->ifname, true, &ifa->addr);
  1049. goto done_unlock_put;
  1050. }
  1051. new_iface = iface_alloc(net_dev);
  1052. IF_DEBUG("qtaguid: iface_stat: create6(%s): done "
  1053. "entry=%p ip=%pI6c\n", ifname, new_iface, &ifa->addr);
  1054. done_unlock_put:
  1055. spin_unlock_bh(&iface_stat_list_lock);
  1056. done_put:
  1057. in_dev_put(in_dev);
  1058. }
  1059. static struct sock_tag *get_sock_stat_nl(const struct sock *sk)
  1060. {
  1061. MT_DEBUG("qtaguid: get_sock_stat_nl(sk=%p)\n", sk);
  1062. return sock_tag_tree_search(&sock_tag_tree, sk);
  1063. }
  1064. static struct sock_tag *get_sock_stat(const struct sock *sk)
  1065. {
  1066. struct sock_tag *sock_tag_entry;
  1067. MT_DEBUG("qtaguid: get_sock_stat(sk=%p)\n", sk);
  1068. if (!sk)
  1069. return NULL;
  1070. spin_lock_bh(&sock_tag_list_lock);
  1071. sock_tag_entry = get_sock_stat_nl(sk);
  1072. spin_unlock_bh(&sock_tag_list_lock);
  1073. return sock_tag_entry;
  1074. }
  1075. static int ipx_proto(const struct sk_buff *skb,
  1076. struct xt_action_param *par)
  1077. {
  1078. int thoff = 0, tproto;
  1079. switch (par->family) {
  1080. case NFPROTO_IPV6:
  1081. tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
  1082. if (tproto < 0)
  1083. MT_DEBUG("%s(): transport header not found in ipv6"
  1084. " skb=%p\n", __func__, skb);
  1085. break;
  1086. case NFPROTO_IPV4:
  1087. tproto = ip_hdr(skb)->protocol;
  1088. break;
  1089. default:
  1090. tproto = IPPROTO_RAW;
  1091. }
  1092. return tproto;
  1093. }
  1094. static void
  1095. data_counters_update(struct data_counters *dc, int set,
  1096. enum ifs_tx_rx direction, int proto, int bytes)
  1097. {
  1098. switch (proto) {
  1099. case IPPROTO_TCP:
  1100. dc_add_byte_packets(dc, set, direction, IFS_TCP, bytes, 1);
  1101. break;
  1102. case IPPROTO_UDP:
  1103. dc_add_byte_packets(dc, set, direction, IFS_UDP, bytes, 1);
  1104. break;
  1105. case IPPROTO_IP:
  1106. default:
  1107. dc_add_byte_packets(dc, set, direction, IFS_PROTO_OTHER, bytes,
  1108. 1);
  1109. break;
  1110. }
  1111. }
  1112. /*
  1113. * Update stats for the specified interface. Do nothing if the entry
  1114. * does not exist (when a device was never configured with an IP address).
  1115. * Called when an device is being unregistered.
  1116. */
  1117. static void iface_stat_update(struct net_device *net_dev, bool stash_only)
  1118. {
  1119. struct rtnl_link_stats64 dev_stats, *stats;
  1120. struct iface_stat *entry;
  1121. stats = dev_get_stats(net_dev, &dev_stats);
  1122. spin_lock_bh(&iface_stat_list_lock);
  1123. entry = get_iface_entry(net_dev->name);
  1124. if (entry == NULL) {
  1125. IF_DEBUG("qtaguid: iface_stat: update(%s): not tracked\n",
  1126. net_dev->name);
  1127. spin_unlock_bh(&iface_stat_list_lock);
  1128. return;
  1129. }
  1130. IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
  1131. net_dev->name, entry);
  1132. if (!entry->active) {
  1133. IF_DEBUG("qtaguid: %s(%s): already disabled\n", __func__,
  1134. net_dev->name);
  1135. spin_unlock_bh(&iface_stat_list_lock);
  1136. return;
  1137. }
  1138. if (stash_only) {
  1139. entry->last_known[IFS_TX].bytes = stats->tx_bytes;
  1140. entry->last_known[IFS_TX].packets = stats->tx_packets;
  1141. entry->last_known[IFS_RX].bytes = stats->rx_bytes;
  1142. entry->last_known[IFS_RX].packets = stats->rx_packets;
  1143. entry->last_known_valid = true;
  1144. IF_DEBUG("qtaguid: %s(%s): "
  1145. "dev stats stashed rx/tx=%llu/%llu\n", __func__,
  1146. net_dev->name, stats->rx_bytes, stats->tx_bytes);
  1147. spin_unlock_bh(&iface_stat_list_lock);
  1148. return;
  1149. }
  1150. entry->totals_via_dev[IFS_TX].bytes += stats->tx_bytes;
  1151. entry->totals_via_dev[IFS_TX].packets += stats->tx_packets;
  1152. entry->totals_via_dev[IFS_RX].bytes += stats->rx_bytes;
  1153. entry->totals_via_dev[IFS_RX].packets += stats->rx_packets;
  1154. /* We don't need the last_known[] anymore */
  1155. entry->last_known_valid = false;
  1156. _iface_stat_set_active(entry, net_dev, false);
  1157. IF_DEBUG("qtaguid: %s(%s): "
  1158. "disable tracking. rx/tx=%llu/%llu\n", __func__,
  1159. net_dev->name, stats->rx_bytes, stats->tx_bytes);
  1160. spin_unlock_bh(&iface_stat_list_lock);
  1161. }
  1162. /*
  1163. * Update stats for the specified interface from the skb.
  1164. * Do nothing if the entry
  1165. * does not exist (when a device was never configured with an IP address).
  1166. * Called on each sk.
  1167. */
  1168. static void iface_stat_update_from_skb(const struct sk_buff *skb,
  1169. struct xt_action_param *par)
  1170. {
  1171. struct iface_stat *entry;
  1172. const struct net_device *el_dev;
  1173. enum ifs_tx_rx direction = par->in ? IFS_RX : IFS_TX;
  1174. int bytes = skb->len;
  1175. int proto;
  1176. if (!skb->dev) {
  1177. MT_DEBUG("qtaguid[%d]: no skb->dev\n", par->hooknum);
  1178. el_dev = par->in ? : par->out;
  1179. } else {
  1180. const struct net_device *other_dev;
  1181. el_dev = skb->dev;
  1182. other_dev = par->in ? : par->out;
  1183. if (el_dev != other_dev) {
  1184. MT_DEBUG("qtaguid[%d]: skb->dev=%p %s vs "
  1185. "par->(in/out)=%p %s\n",
  1186. par->hooknum, el_dev, el_dev->name, other_dev,
  1187. other_dev->name);
  1188. }
  1189. }
  1190. if (unlikely(!el_dev)) {
  1191. pr_err("qtaguid[%d]: %s(): no par->in/out?!!\n",
  1192. par->hooknum, __func__);
  1193. BUG();
  1194. } else if (unlikely(!el_dev->name)) {
  1195. pr_err("qtaguid[%d]: %s(): no dev->name?!!\n",
  1196. par->hooknum, __func__);
  1197. BUG();
  1198. } else {
  1199. proto = ipx_proto(skb, par);
  1200. MT_DEBUG("qtaguid[%d]: dev name=%s type=%d fam=%d proto=%d\n",
  1201. par->hooknum, el_dev->name, el_dev->type,
  1202. par->family, proto);
  1203. }
  1204. spin_lock_bh(&iface_stat_list_lock);
  1205. entry = get_iface_entry(el_dev->name);
  1206. if (entry == NULL) {
  1207. IF_DEBUG("qtaguid: iface_stat: %s(%s): not tracked\n",
  1208. __func__, el_dev->name);
  1209. spin_unlock_bh(&iface_stat_list_lock);
  1210. return;
  1211. }
  1212. IF_DEBUG("qtaguid: %s(%s): entry=%p\n", __func__,
  1213. el_dev->name, entry);
  1214. data_counters_update(&entry->totals_via_skb, 0, direction, proto,
  1215. bytes);
  1216. spin_unlock_bh(&iface_stat_list_lock);
  1217. }
  1218. static void tag_stat_update(struct tag_stat *tag_entry,
  1219. enum ifs_tx_rx direction, int proto, int bytes)
  1220. {
  1221. int active_set;
  1222. active_set = get_active_counter_set(tag_entry->tn.tag);
  1223. MT_DEBUG("qtaguid: tag_stat_update(tag=0x%llx (uid=%u) set=%d "
  1224. "dir=%d proto=%d bytes=%d)\n",
  1225. tag_entry->tn.tag, get_uid_from_tag(tag_entry->tn.tag),
  1226. active_set, direction, proto, bytes);
  1227. data_counters_update(&tag_entry->counters, active_set, direction,
  1228. proto, bytes);
  1229. if (tag_entry->parent_counters)
  1230. data_counters_update(tag_entry->parent_counters, active_set,
  1231. direction, proto, bytes);
  1232. }
  1233. /*
  1234. * Create a new entry for tracking the specified {acct_tag,uid_tag} within
  1235. * the interface.
  1236. * iface_entry->tag_stat_list_lock should be held.
  1237. */
  1238. static struct tag_stat *create_if_tag_stat(struct iface_stat *iface_entry,
  1239. tag_t tag)
  1240. {
  1241. struct tag_stat *new_tag_stat_entry = NULL;
  1242. IF_DEBUG("qtaguid: iface_stat: %s(): ife=%p tag=0x%llx"
  1243. " (uid=%u)\n", __func__,
  1244. iface_entry, tag, get_uid_from_tag(tag));
  1245. new_tag_stat_entry = kzalloc(sizeof(*new_tag_stat_entry), GFP_ATOMIC);
  1246. if (!new_tag_stat_entry) {
  1247. pr_err("qtaguid: iface_stat: tag stat alloc failed\n");
  1248. goto done;
  1249. }
  1250. new_tag_stat_entry->tn.tag = tag;
  1251. tag_stat_tree_insert(new_tag_stat_entry, &iface_entry->tag_stat_tree);
  1252. done:
  1253. return new_tag_stat_entry;
  1254. }
  1255. static void if_tag_stat_update(const char *ifname, uid_t uid,
  1256. const struct sock *sk, enum ifs_tx_rx direction,
  1257. int proto, int bytes)
  1258. {
  1259. struct tag_stat *tag_stat_entry;
  1260. tag_t tag, acct_tag;
  1261. tag_t uid_tag;
  1262. struct data_counters *uid_tag_counters;
  1263. struct sock_tag *sock_tag_entry;
  1264. struct iface_stat *iface_entry;
  1265. struct tag_stat *new_tag_stat = NULL;
  1266. MT_DEBUG("qtaguid: if_tag_stat_update(ifname=%s "
  1267. "uid=%u sk=%p dir=%d proto=%d bytes=%d)\n",
  1268. ifname, uid, sk, direction, proto, bytes);
  1269. iface_entry = get_iface_entry(ifname);
  1270. if (!iface_entry) {
  1271. pr_err("qtaguid: iface_stat: stat_update() %s not found\n",
  1272. ifname);
  1273. return;
  1274. }
  1275. /* It is ok to process data when an iface_entry is inactive */
  1276. MT_DEBUG("qtaguid: iface_stat: stat_update() dev=%s entry=%p\n",
  1277. ifname, iface_entry);
  1278. /*
  1279. * Look for a tagged sock.
  1280. * It will have an acct_uid.
  1281. */
  1282. sock_tag_entry = get_sock_stat(sk);
  1283. if (sock_tag_entry) {
  1284. tag = sock_tag_entry->tag;
  1285. acct_tag = get_atag_from_tag(tag);
  1286. uid_tag = get_utag_from_tag(tag);
  1287. } else {
  1288. acct_tag = make_atag_from_value(0);
  1289. tag = combine_atag_with_uid(acct_tag, uid);
  1290. uid_tag = make_tag_from_uid(uid);
  1291. }
  1292. MT_DEBUG("qtaguid: iface_stat: stat_update(): "
  1293. " looking for tag=0x%llx (uid=%u) in ife=%p\n",
  1294. tag, get_uid_from_tag(tag), iface_entry);
  1295. /* Loop over tag list under this interface for {acct_tag,uid_tag} */
  1296. spin_lock_bh(&iface_entry->tag_stat_list_lock);
  1297. tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree,
  1298. tag);
  1299. if (tag_stat_entry) {
  1300. /*
  1301. * Updating the {acct_tag, uid_tag} entry handles both stats:
  1302. * {0, uid_tag} will also get updated.
  1303. */
  1304. tag_stat_update(tag_stat_entry, direction, proto, bytes);
  1305. spin_unlock_bh(&iface_entry->tag_stat_list_lock);
  1306. return;
  1307. }
  1308. /* Loop over tag list under this interface for {0,uid_tag} */
  1309. tag_stat_entry = tag_stat_tree_search(&iface_entry->tag_stat_tree,
  1310. uid_tag);
  1311. if (!tag_stat_entry) {
  1312. /* Here: the base uid_tag did not exist */
  1313. /*
  1314. * No parent counters. So
  1315. * - No {0, uid_tag} stats and no {acc_tag, uid_tag} stats.
  1316. */
  1317. new_tag_stat = create_if_tag_stat(iface_entry, uid_tag);
  1318. if (!new_tag_stat)
  1319. goto unlock;
  1320. uid_tag_counters = &new_tag_stat->counters;
  1321. } else {
  1322. uid_tag_counters = &tag_stat_entry->counters;
  1323. }
  1324. if (acct_tag) {
  1325. /* Create the child {acct_tag, uid_tag} and hook up parent. */
  1326. new_tag_stat = create_if_tag_stat(iface_entry, tag);
  1327. if (!new_tag_stat)
  1328. goto unlock;
  1329. new_tag_stat->parent_counters = uid_tag_counters;
  1330. } else {
  1331. /*
  1332. * For new_tag_stat to be still NULL here would require:
  1333. * {0, uid_tag} exists
  1334. * and {acct_tag, uid_tag} doesn't exist
  1335. * AND acct_tag == 0.
  1336. * Impossible. This reassures us that new_tag_stat
  1337. * below will always be assigned.
  1338. */
  1339. BUG_ON(!new_tag_stat);
  1340. }
  1341. tag_stat_update(new_tag_stat, direction, proto, bytes);
  1342. unlock:
  1343. spin_unlock_bh(&iface_entry->tag_stat_list_lock);
  1344. }
  1345. static int iface_netdev_event_handler(struct notifier_block *nb,
  1346. unsigned long event, void *ptr) {
  1347. struct net_device *dev = ptr;
  1348. if (unlikely(module_passive))
  1349. return NOTIFY_DONE;
  1350. IF_DEBUG("qtaguid: iface_stat: netdev_event(): "
  1351. "ev=0x%lx/%s netdev=%p->name=%s\n",
  1352. event, netdev_evt_str(event), dev, dev ? dev->name : "");
  1353. switch (event) {
  1354. case NETDEV_UP:
  1355. iface_stat_create(dev, NULL);
  1356. atomic64_inc(&qtu_events.iface_events);
  1357. break;
  1358. case NETDEV_DOWN:
  1359. case NETDEV_UNREGISTER:
  1360. iface_stat_update(dev, event == NETDEV_DOWN);
  1361. atomic64_inc(&qtu_events.iface_events);
  1362. break;
  1363. }
  1364. return NOTIFY_DONE;
  1365. }
  1366. static int iface_inet6addr_event_handler(struct notifier_block *nb,
  1367. unsigned long event, void *ptr)
  1368. {
  1369. struct inet6_ifaddr *ifa = ptr;
  1370. struct net_device *dev;
  1371. if (unlikely(module_passive))
  1372. return NOTIFY_DONE;
  1373. IF_DEBUG("qtaguid: iface_stat: inet6addr_event(): "
  1374. "ev=0x%lx/%s ifa=%p\n",
  1375. event, netdev_evt_str(event), ifa);
  1376. switch (event) {
  1377. case NETDEV_UP:
  1378. BUG_ON(!ifa || !ifa->idev);
  1379. dev = (struct net_device *)ifa->idev->dev;
  1380. iface_stat_create_ipv6(dev, ifa);
  1381. atomic64_inc(&qtu_events.iface_events);
  1382. break;
  1383. case NETDEV_DOWN:
  1384. case NETDEV_UNREGISTER:
  1385. BUG_ON(!ifa || !ifa->idev);
  1386. dev = (struct net_device *)ifa->idev->dev;
  1387. iface_stat_update(dev, event == NETDEV_DOWN);
  1388. atomic64_inc(&qtu_events.iface_events);
  1389. break;
  1390. }
  1391. return NOTIFY_DONE;
  1392. }
  1393. static int iface_inetaddr_event_handler(struct notifier_block *nb,
  1394. unsigned long event, void *ptr)
  1395. {
  1396. struct in_ifaddr *ifa = ptr;
  1397. struct net_device *dev;
  1398. if (unlikely(module_passive))
  1399. return NOTIFY_DONE;
  1400. IF_DEBUG("qtaguid: iface_stat: inetaddr_event(): "
  1401. "ev=0x%lx/%s ifa=%p\n",
  1402. event, netdev_evt_str(event), ifa);
  1403. switch (event) {
  1404. case NETDEV_UP:
  1405. BUG_ON(!ifa || !ifa->ifa_dev);
  1406. dev = ifa->ifa_dev->dev;
  1407. iface_stat_create(dev, ifa);
  1408. atomic64_inc(&qtu_events.iface_events);
  1409. break;
  1410. case NETDEV_DOWN:
  1411. case NETDEV_UNREGISTER:
  1412. BUG_ON(!ifa || !ifa->ifa_dev);
  1413. dev = ifa->ifa_dev->dev;
  1414. iface_stat_update(dev, event == NETDEV_DOWN);
  1415. atomic64_inc(&qtu_events.iface_events);
  1416. break;
  1417. }
  1418. return NOTIFY_DONE;
  1419. }
  1420. static struct notifier_block iface_netdev_notifier_blk = {
  1421. .notifier_call = iface_netdev_event_handler,
  1422. };
  1423. static struct notifier_block iface_inetaddr_notifier_blk = {
  1424. .notifier_call = iface_inetaddr_event_handler,
  1425. };
  1426. static struct notifier_block iface_inet6addr_notifier_blk = {
  1427. .notifier_call = iface_inet6addr_event_handler,
  1428. };
  1429. static int __init iface_stat_init(struct proc_dir_entry *parent_procdir)
  1430. {
  1431. int err;
  1432. iface_stat_procdir = proc_mkdir(iface_stat_procdirname, parent_procdir);
  1433. if (!iface_stat_procdir) {
  1434. pr_err("qtaguid: iface_stat: init failed to create proc entry\n");
  1435. err = -1;
  1436. goto err;
  1437. }
  1438. iface_stat_all_procfile = create_proc_entry(iface_stat_all_procfilename,
  1439. proc_iface_perms,
  1440. parent_procdir);
  1441. if (!iface_stat_all_procfile) {
  1442. pr_err("qtaguid: iface_stat: init "
  1443. " failed to create stat_old proc entry\n");
  1444. err = -1;
  1445. goto err_zap_entry;
  1446. }
  1447. iface_stat_all_procfile->read_proc = iface_stat_fmt_proc_read;
  1448. iface_stat_all_procfile->data = (void *)1; /* fmt1 */
  1449. iface_stat_fmt_procfile = create_proc_entry(iface_stat_fmt_procfilename,
  1450. proc_iface_perms,
  1451. parent_procdir);
  1452. if (!iface_stat_fmt_procfile) {
  1453. pr_err("qtaguid: iface_stat: init "
  1454. " failed to create stat_all proc entry\n");
  1455. err = -1;
  1456. goto err_zap_all_stats_entry;
  1457. }
  1458. iface_stat_fmt_procfile->read_proc = iface_stat_fmt_proc_read;
  1459. iface_stat_fmt_procfile->data = (void *)2; /* fmt2 */
  1460. err = register_netdevice_notifier(&iface_netdev_notifier_blk);
  1461. if (err) {
  1462. pr_err("qtaguid: iface_stat: init "
  1463. "failed to register dev event handler\n");
  1464. goto err_zap_all_stats_entries;
  1465. }
  1466. err = register_inetaddr_notifier(&iface_inetaddr_notifier_blk);
  1467. if (err) {
  1468. pr_err("qtaguid: iface_stat: init "
  1469. "failed to register ipv4 dev event handler\n");
  1470. goto err_unreg_nd;
  1471. }
  1472. err = register_inet6addr_notifier(&iface_inet6addr_notifier_blk);
  1473. if (err) {
  1474. pr_err("qtaguid: iface_stat: init "
  1475. "failed to register ipv6 dev event handler\n");
  1476. goto err_unreg_ip4_addr;
  1477. }
  1478. return 0;
  1479. err_unreg_ip4_addr:
  1480. unregister_inetaddr_notifier(&iface_inetaddr_notifier_blk);
  1481. err_unreg_nd:
  1482. unregister_netdevice_notifier(&iface_netdev_notifier_blk);
  1483. err_zap_all_stats_entries:
  1484. remove_proc_entry(iface_stat_fmt_procfilename, parent_procdir);
  1485. err_zap_all_stats_entry:
  1486. remove_proc_entry(iface_stat_all_procfilename, parent_procdir);
  1487. err_zap_entry:
  1488. remove_proc_entry(iface_stat_procdirname, parent_procdir);
  1489. err:
  1490. return err;
  1491. }
  1492. static struct sock *qtaguid_find_sk(const struct sk_buff *skb,
  1493. struct xt_action_param *par)
  1494. {
  1495. struct sock *sk;
  1496. unsigned int hook_mask = (1 << par->hooknum);
  1497. MT_DEBUG("qtaguid: find_sk(skb=%p) hooknum=%d family=%d\n", skb,
  1498. par->hooknum, par->family);
  1499. /*
  1500. * Let's not abuse the the xt_socket_get*_sk(), or else it will
  1501. * return garbage SKs.
  1502. */
  1503. if (!(hook_mask & XT_SOCKET_SUPPORTED_HOOKS))
  1504. return NULL;
  1505. switch (par->family) {
  1506. case NFPROTO_IPV6:
  1507. sk = xt_socket_get6_sk(skb, par);
  1508. break;
  1509. case NFPROTO_IPV4:
  1510. sk = xt_socket_get4_sk(skb, par);
  1511. break;
  1512. default:
  1513. return NULL;
  1514. }
  1515. if (sk) {
  1516. MT_DEBUG("qtaguid: %p->sk_proto=%u "
  1517. "->sk_state=%d\n", sk, sk->sk_protocol, sk->sk_state);
  1518. /*
  1519. * When in TCP_TIME_WAIT the sk is not a "struct sock" but
  1520. * "struct inet_timewait_sock" which is missing fields.
  1521. */
  1522. if (sk->sk_state == TCP_TIME_WAIT) {
  1523. xt_socket_put_sk(sk);
  1524. sk = NULL;
  1525. }
  1526. }
  1527. return sk;
  1528. }
  1529. static void account_for_uid(const struct sk_buff *skb,
  1530. const struct sock *alternate_sk, uid_t uid,
  1531. struct xt_action_param *par)
  1532. {
  1533. const struct net_device *el_dev;
  1534. if (!skb->dev) {
  1535. MT_DEBUG("qtaguid[%d]: no skb->dev\n", par->hooknum);
  1536. el_dev = par->in ? : par->out;
  1537. } else {
  1538. const struct net_device *other_dev;
  1539. el_dev = skb->dev;
  1540. other_dev = par->in ? : par->out;
  1541. if (el_dev != other_dev) {
  1542. MT_DEBUG("qtaguid[%d]: skb->dev=%p %s vs "
  1543. "par->(in/out)=%p %s\n",
  1544. par->hooknum, el_dev, el_dev->name, other_dev,
  1545. other_dev->name);
  1546. }
  1547. }
  1548. if (unlikely(!el_dev)) {
  1549. pr_info("qtaguid[%d]: no par->in/out?!!\n", par->hooknum);
  1550. } else if (unlikely(!el_dev->name)) {
  1551. pr_info("qtaguid[%d]: no dev->name?!!\n", par->hooknum);
  1552. } else {
  1553. int proto = ipx_proto(skb, par);
  1554. MT_DEBUG("qtaguid[%d]: dev name=%s type=%d fam=%d proto=%d\n",
  1555. par->hooknum, el_dev->name, el_dev->type,
  1556. par->family, proto);
  1557. if_tag_stat_update(el_dev->name, uid,
  1558. skb->sk ? skb->sk : alternate_sk,
  1559. par->in ? IFS_RX : IFS_TX,
  1560. proto, skb->len);
  1561. }
  1562. }
  1563. static bool qtaguid_mt(const struct sk_buff *skb, struct xt_action_param *par)
  1564. {
  1565. const struct xt_qtaguid_match_info *info = par->matchinfo;
  1566. const struct file *filp;
  1567. bool got_sock = false;
  1568. struct sock *sk;
  1569. uid_t sock_uid;
  1570. bool res;
  1571. if (unlikely(module_passive))
  1572. return (info->match ^ info->invert) == 0;
  1573. MT_DEBUG("qtaguid[%d]: entered skb=%p par->in=%p/out=%p fam=%d\n",
  1574. par->hooknum, skb, par->in, par->out, par->family);
  1575. atomic64_inc(&qtu_events.match_calls);
  1576. if (skb == NULL) {
  1577. res = (info->match ^ info->invert) == 0;
  1578. goto ret_res;
  1579. }
  1580. switch (par->hooknum) {
  1581. case NF_INET_PRE_ROUTING:
  1582. case NF_INET_POST_ROUTING:
  1583. atomic64_inc(&qtu_events.match_calls_prepost);
  1584. iface_stat_update_from_skb(skb, par);
  1585. /*
  1586. * We are done in pre/post. The skb will get processed
  1587. * further alter.
  1588. */
  1589. res = (info->match ^ info->invert);
  1590. goto ret_res;
  1591. break;
  1592. /* default: Fall through and do UID releated work */
  1593. }
  1594. sk = skb->sk;
  1595. /*
  1596. * When in TCP_TIME_WAIT the sk is not a "struct sock" but
  1597. * "struct inet_timewait_sock" which is missing fields.
  1598. * So we ignore it.
  1599. */
  1600. if (sk && sk->sk_state == TCP_TIME_WAIT)
  1601. sk = NULL;
  1602. if (sk == NULL) {
  1603. /*
  1604. * A missing sk->sk_socket happens when packets are in-flight
  1605. * and the matching socket is already closed and gone.
  1606. */
  1607. sk = qtaguid_find_sk(skb, par);
  1608. /*
  1609. * If we got the socket from the find_sk(), we will need to put
  1610. * it back, as nf_tproxy_get_sock_v4() got it.
  1611. */
  1612. got_sock = sk;
  1613. if (sk)
  1614. atomic64_inc(&qtu_events.match_found_sk_in_ct);
  1615. else
  1616. atomic64_inc(&qtu_events.match_found_no_sk_in_ct);
  1617. } else {
  1618. atomic64_inc(&qtu_events.match_found_sk);
  1619. }
  1620. MT_DEBUG("qtaguid[%d]: sk=%p got_sock=%d fam=%d proto=%d\n",
  1621. par->hooknum, sk, got_sock, par->family, ipx_proto(skb, par));
  1622. if (sk != NULL) {
  1623. MT_DEBUG("qtaguid[%d]: sk=%p->sk_socket=%p->file=%p\n",
  1624. par->hooknum, sk, sk->sk_socket,
  1625. sk->sk_socket ? sk->sk_socket->file : (void *)-1LL);
  1626. filp = sk->sk_socket ? sk->sk_socket->file : NULL;
  1627. MT_DEBUG("qtaguid[%d]: filp...uid=%u\n",
  1628. par->hooknum, filp ? filp->f_cred->fsuid : -1);
  1629. }
  1630. if (sk == NULL || sk->sk_socket == NULL) {
  1631. /*
  1632. * Here, the qtaguid_find_sk() using connection tracking
  1633. * couldn't find the owner, so for now we just count them
  1634. * against the system.
  1635. */
  1636. /*
  1637. * TODO: unhack how to force just accounting.
  1638. * For now we only do iface stats when the uid-owner is not
  1639. * requested.
  1640. */
  1641. if (!(info->match & XT_QTAGUID_UID))
  1642. account_for_uid(skb, sk, 0, par);
  1643. MT_DEBUG("qtaguid[%d]: leaving (sk?sk->sk_socket)=%p\n",
  1644. par->hooknum,
  1645. sk ? sk->sk_socket : NULL);
  1646. res = (info->match ^ info->invert) == 0;
  1647. atomic64_inc(&qtu_events.match_no_sk);
  1648. goto put_sock_ret_res;
  1649. } else if (info->match & info->invert & XT_QTAGUID_SOCKET) {
  1650. res = false;
  1651. goto put_sock_ret_res;
  1652. }
  1653. filp = sk->sk_socket->file;
  1654. if (filp == NULL) {
  1655. MT_DEBUG("qtaguid[%d]: leaving filp=NULL\n", par->hooknum);
  1656. account_for_uid(skb, sk, 0, par);
  1657. res = ((info->match ^ info->invert) &
  1658. (XT_QTAGUID_UID | XT_QTAGUID_GID)) == 0;
  1659. atomic64_inc(&qtu_events.match_no_sk_file);
  1660. goto put_sock_ret_res;
  1661. }
  1662. sock_uid = filp->f_cred->fsuid;
  1663. /*
  1664. * TODO: unhack how to force just accounting.
  1665. * For now we only do iface stats when the uid-owner is not requested
  1666. */
  1667. if (!(info->match & XT_QTAGUID_UID))
  1668. account_for_uid(skb, sk, sock_uid, par);
  1669. /*
  1670. * The following two tests fail the match when:
  1671. * id not in range AND no inverted condition requested
  1672. * or id in range AND inverted condition requested
  1673. * Thus (!a && b) || (a && !b) == a ^ b
  1674. */
  1675. if (info->match & XT_QTAGUID_UID)
  1676. if ((filp->f_cred->fsuid >= info->uid_min &&
  1677. filp->f_cred->fsuid <= info->uid_max) ^
  1678. !(info->invert & XT_QTAGUID_UID)) {
  1679. MT_DEBUG("qtaguid[%d]: leaving uid not matching\n",
  1680. par->hooknum);
  1681. res = false;
  1682. goto put_sock_ret_res;
  1683. }
  1684. if (info->match & XT_QTAGUID_GID)
  1685. if ((filp->f_cred->fsgid >= info->gid_min &&
  1686. filp->f_cred->fsgid <= info->gid_max) ^
  1687. !(info->invert & XT_QTAGUID_GID)) {
  1688. MT_DEBUG("qtaguid[%d]: leaving gid not matching\n",
  1689. par->hooknum);
  1690. res = false;
  1691. goto put_sock_ret_res;
  1692. }
  1693. MT_DEBUG("qtaguid[%d]: leaving matched\n", par->hooknum);
  1694. res = true;
  1695. put_sock_ret_res:
  1696. if (got_sock)
  1697. xt_socket_put_sk(sk);
  1698. ret_res:
  1699. MT_DEBUG("qtaguid[%d]: left %d\n", par->hooknum, res);
  1700. return res;
  1701. }
  1702. #ifdef DDEBUG
  1703. /* This function is not in xt_qtaguid_print.c because of locks visibility */
  1704. static void prdebug_full_state(int indent_level, const char *fmt, ...)
  1705. {
  1706. va_list args;
  1707. char *fmt_buff;
  1708. char *buff;
  1709. if (!unlikely(qtaguid_debug_mask & DDEBUG_MASK))
  1710. return;
  1711. fmt_buff = kasprintf(GFP_ATOMIC,
  1712. "qtaguid: %s(): %s {\n", __func__, fmt);
  1713. BUG_ON(!fmt_buff);
  1714. va_start(args, fmt);
  1715. buff = kvasprintf(GFP_ATOMIC,
  1716. fmt_buff, args);
  1717. BUG_ON(!buff);
  1718. pr_debug("%s", buff);
  1719. kfree(fmt_buff);
  1720. kfree(buff);
  1721. va_end(args);
  1722. spin_lock_bh(&sock_tag_list_lock);
  1723. prdebug_sock_tag_tree(indent_level, &sock_tag_tree);
  1724. spin_unlock_bh(&sock_tag_list_lock);
  1725. spin_lock_bh(&sock_tag_list_lock);
  1726. spin_lock_bh(&uid_tag_data_tree_lock);
  1727. prdebug_uid_tag_data_tree(indent_level, &uid_tag_data_tree);
  1728. prdebug_proc_qtu_data_tree(indent_level, &proc_qtu_data_tree);
  1729. spin_unlock_bh(&uid_tag_data_tree_lock);
  1730. spin_unlock_bh(&sock_tag_list_lock);
  1731. spin_lock_bh(&iface_stat_list_lock);
  1732. prdebug_iface_stat_list(indent_level, &iface_stat_list);
  1733. spin_unlock_bh(&iface_stat_list_lock);
  1734. pr_debug("qtaguid: %s(): }\n", __func__);
  1735. }
  1736. #else
  1737. static void prdebug_full_state(int indent_level, const char *fmt, ...) {}
  1738. #endif
  1739. /*
  1740. * Procfs reader to get all active socket tags using style "1)" as described in
  1741. * fs/proc/generic.c
  1742. */
  1743. static int qtaguid_ctrl_proc_read(char *page, char **num_items_returned,
  1744. off_t items_to_skip, int char_count, int *eof,
  1745. void *data)
  1746. {
  1747. char *outp = page;
  1748. int len;
  1749. uid_t uid;
  1750. struct rb_node *node;
  1751. struct sock_tag *sock_tag_entry;
  1752. int item_index = 0;
  1753. int indent_level = 0;
  1754. long f_count;
  1755. if (unlikely(module_passive)) {
  1756. *eof = 1;
  1757. return 0;
  1758. }
  1759. if (*eof)
  1760. return 0;
  1761. CT_DEBUG("qtaguid: proc ctrl pid=%u tgid=%u uid=%u "
  1762. "page=%p off=%ld char_count=%d *eof=%d\n",
  1763. current->pid, current->tgid, current_fsuid(),
  1764. page, items_to_skip, char_count, *eof);
  1765. spin_lock_bh(&sock_tag_list_lock);
  1766. for (node = rb_first(&sock_tag_tree);
  1767. node;
  1768. node = rb_next(node)) {
  1769. if (item_index++ < items_to_skip)
  1770. continue;
  1771. sock_tag_entry = rb_entry(node, struct sock_tag, sock_node);
  1772. uid = get_uid_from_tag(sock_tag_entry->tag);
  1773. CT_DEBUG("qtaguid: proc_read(): sk=%p tag=0x%llx (uid=%u) "
  1774. "pid=%u\n",
  1775. sock_tag_entry->sk,
  1776. sock_tag_entry->tag,
  1777. uid,
  1778. sock_tag_entry->pid
  1779. );
  1780. f_count = atomic_long_read(
  1781. &sock_tag_entry->socket->file->f_count);
  1782. len = snprintf(outp, char_count,
  1783. "sock=%p tag=0x%llx (uid=%u) pid=%u "
  1784. "f_count=%lu\n",
  1785. sock_tag_entry->sk,
  1786. sock_tag_entry->tag, uid,
  1787. sock_tag_entry->pid, f_count);
  1788. if (len >= char_count) {
  1789. spin_unlock_bh(&sock_tag_list_lock);
  1790. *outp = '\0';
  1791. return outp - page;
  1792. }
  1793. outp += len;
  1794. char_count -= len;
  1795. (*num_items_returned)++;
  1796. }
  1797. spin_unlock_bh(&sock_tag_list_lock);
  1798. if (item_index++ >= items_to_skip) {
  1799. len = snprintf(outp, char_count,
  1800. "events: sockets_tagged=%llu "
  1801. "sockets_untagged=%llu "
  1802. "counter_set_changes=%llu "
  1803. "delete_cmds=%llu "
  1804. "iface_events=%llu "
  1805. "match_calls=%llu "
  1806. "match_calls_prepost=%llu "
  1807. "match_found_sk=%llu "
  1808. "match_found_sk_in_ct=%llu "
  1809. "match_found_no_sk_in_ct=%llu "
  1810. "match_no_sk=%llu "
  1811. "match_no_sk_file=%llu\n",
  1812. atomic64_read(&qtu_events.sockets_tagged),
  1813. atomic64_read(&qtu_events.sockets_untagged),
  1814. atomic64_read(&qtu_events.counter_set_changes),
  1815. atomic64_read(&qtu_events.delete_cmds),
  1816. atomic64_read(&qtu_events.iface_events),
  1817. atomic64_read(&qtu_events.match_calls),
  1818. atomic64_read(&qtu_events.match_calls_prepost),
  1819. atomic64_read(&qtu_events.match_found_sk),
  1820. atomic64_read(&qtu_events.match_found_sk_in_ct),
  1821. atomic64_read(
  1822. &qtu_events.match_found_no_sk_in_ct),
  1823. atomic64_read(&qtu_events.match_no_sk),
  1824. atomic64_read(&qtu_events.match_no_sk_file));
  1825. if (len >= char_count) {
  1826. *outp = '\0';
  1827. return outp - page;
  1828. }
  1829. outp += len;
  1830. char_count -= len;
  1831. (*num_items_returned)++;
  1832. }
  1833. /* Count the following as part of the last item_index */
  1834. if (item_index > items_to_skip) {
  1835. prdebug_full_state(indent_level, "proc ctrl");
  1836. }
  1837. *eof = 1;
  1838. return outp - page;
  1839. }
  1840. /*
  1841. * Delete socket tags, and stat tags associated with a given
  1842. * accouting tag and uid.
  1843. */
  1844. static int ctrl_cmd_delete(const char *input)
  1845. {
  1846. char cmd;
  1847. uid_t uid;
  1848. uid_t entry_uid;
  1849. tag_t acct_tag;
  1850. tag_t tag;
  1851. int res, argc;
  1852. struct iface_stat *iface_entry;
  1853. struct rb_node *node;
  1854. struct sock_tag *st_entry;
  1855. struct rb_root st_to_free_tree = RB_ROOT;
  1856. struct tag_stat *ts_entry;
  1857. struct tag_counter_set *tcs_entry;
  1858. struct tag_ref *tr_entry;
  1859. struct uid_tag_data *utd_entry;
  1860. argc = sscanf(input, "%c %llu %u", &cmd, &acct_tag, &uid);
  1861. CT_DEBUG("qtaguid: ctrl_delete(%s): argc=%d cmd=%c "
  1862. "user_tag=0x%llx uid=%u\n", input, argc, cmd,
  1863. acct_tag, uid);
  1864. if (argc < 2) {
  1865. res = -EINVAL;
  1866. goto err;
  1867. }
  1868. if (!valid_atag(acct_tag)) {
  1869. pr_info("qtaguid: ctrl_delete(%s): invalid tag\n", input);
  1870. res = -EINVAL;
  1871. goto err;
  1872. }
  1873. if (argc < 3) {
  1874. uid = current_fsuid();
  1875. } else if (!can_impersonate_uid(uid)) {
  1876. pr_info("qtaguid: ctrl_delete(%s): "
  1877. "insufficient priv from pid=%u tgid=%u uid=%u\n",
  1878. input, current->pid, current->tgid, current_fsuid());
  1879. res = -EPERM;
  1880. goto err;
  1881. }
  1882. tag = combine_atag_with_uid(acct_tag, uid);
  1883. CT_DEBUG("qtaguid: ctrl_delete(%s): "
  1884. "looking for tag=0x%llx (uid=%u)\n",
  1885. input, tag, uid);
  1886. /* Delete socket tags */
  1887. spin_lock_bh(&sock_tag_list_lock);
  1888. node = rb_first(&sock_tag_tree);
  1889. while (node) {
  1890. st_entry = rb_entry(node, struct sock_tag, sock_node);
  1891. entry_uid = get_uid_from_tag(st_entry->tag);
  1892. node = rb_next(node);
  1893. if (entry_uid != uid)
  1894. continue;
  1895. CT_DEBUG("qtaguid: ctrl_delete(%s): st tag=0x%llx (uid=%u)\n",
  1896. input, st_entry->tag, entry_uid);
  1897. if (!acct_tag || st_entry->tag == tag) {
  1898. rb_erase(&st_entry->sock_node, &sock_tag_tree);
  1899. /* Can't sockfd_put() within spinlock, do it later. */
  1900. sock_tag_tree_insert(st_entry, &st_to_free_tree);
  1901. tr_entry = lookup_tag_ref(st_entry->tag, NULL);
  1902. BUG_ON(tr_entry->num_sock_tags <= 0);
  1903. tr_entry->num_sock_tags--;
  1904. /*
  1905. * TODO: remove if, and start failing.
  1906. * This is a hack to work around the fact that in some
  1907. * places we have "if (IS_ERR_OR_NULL(pqd_entry))"
  1908. * and are trying to work around apps
  1909. * that didn't open the /dev/xt_qtaguid.
  1910. */
  1911. if (st_entry->list.next && st_entry->list.prev)
  1912. list_del(&st_entry->list);
  1913. }
  1914. }
  1915. spin_unlock_bh(&sock_tag_list_lock);
  1916. sock_tag_tree_erase(&st_to_free_tree);
  1917. /* Delete tag counter-sets */
  1918. spin_lock_bh(&tag_counter_set_list_lock);
  1919. /* Counter sets are only on the uid tag, not full tag */
  1920. tcs_entry = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
  1921. if (tcs_entry) {
  1922. CT_DEBUG("qtaguid: ctrl_delete(%s): "
  1923. "erase tcs: tag=0x%llx (uid=%u) set=%d\n",
  1924. input,
  1925. tcs_entry->tn.tag,
  1926. get_uid_from_tag(tcs_entry->tn.tag),
  1927. tcs_entry->active_set);
  1928. rb_erase(&tcs_entry->tn.node, &tag_counter_set_tree);
  1929. kfree(tcs_entry);
  1930. }
  1931. spin_unlock_bh(&tag_counter_set_list_lock);
  1932. /*
  1933. * If acct_tag is 0, then all entries belonging to uid are
  1934. * erased.
  1935. */
  1936. spin_lock_bh(&iface_stat_list_lock);
  1937. list_for_each_entry(iface_entry, &iface_stat_list, list) {
  1938. spin_lock_bh(&iface_entry->tag_stat_list_lock);
  1939. node = rb_first(&iface_entry->tag_stat_tree);
  1940. while (node) {
  1941. ts_entry = rb_entry(node, struct tag_stat, tn.node);
  1942. entry_uid = get_uid_from_tag(ts_entry->tn.tag);
  1943. node = rb_next(node);
  1944. CT_DEBUG("qtaguid: ctrl_delete(%s): "
  1945. "ts tag=0x%llx (uid=%u)\n",
  1946. input, ts_entry->tn.tag, entry_uid);
  1947. if (entry_uid != uid)
  1948. continue;
  1949. if (!acct_tag || ts_entry->tn.tag == tag) {
  1950. CT_DEBUG("qtaguid: ctrl_delete(%s): "
  1951. "erase ts: %s 0x%llx %u\n",
  1952. input, iface_entry->ifname,
  1953. get_atag_from_tag(ts_entry->tn.tag),
  1954. entry_uid);
  1955. rb_erase(&ts_entry->tn.node,
  1956. &iface_entry->tag_stat_tree);
  1957. kfree(ts_entry);
  1958. }
  1959. }
  1960. spin_unlock_bh(&iface_entry->tag_stat_list_lock);
  1961. }
  1962. spin_unlock_bh(&iface_stat_list_lock);
  1963. /* Cleanup the uid_tag_data */
  1964. spin_lock_bh(&uid_tag_data_tree_lock);
  1965. node = rb_first(&uid_tag_data_tree);
  1966. while (node) {
  1967. utd_entry = rb_entry(node, struct uid_tag_data, node);
  1968. entry_uid = utd_entry->uid;
  1969. node = rb_next(node);
  1970. CT_DEBUG("qtaguid: ctrl_delete(%s): "
  1971. "utd uid=%u\n",
  1972. input, entry_uid);
  1973. if (entry_uid != uid)
  1974. continue;
  1975. /*
  1976. * Go over the tag_refs, and those that don't have
  1977. * sock_tags using them are freed.
  1978. */
  1979. put_tag_ref_tree(tag, utd_entry);
  1980. put_utd_entry(utd_entry);
  1981. }
  1982. spin_unlock_bh(&uid_tag_data_tree_lock);
  1983. atomic64_inc(&qtu_events.delete_cmds);
  1984. res = 0;
  1985. err:
  1986. return res;
  1987. }
  1988. static int ctrl_cmd_counter_set(const char *input)
  1989. {
  1990. char cmd;
  1991. uid_t uid = 0;
  1992. tag_t tag;
  1993. int res, argc;
  1994. struct tag_counter_set *tcs;
  1995. int counter_set;
  1996. argc = sscanf(input, "%c %d %u", &cmd, &counter_set, &uid);
  1997. CT_DEBUG("qtaguid: ctrl_counterset(%s): argc=%d cmd=%c "
  1998. "set=%d uid=%u\n", input, argc, cmd,
  1999. counter_set, uid);
  2000. if (argc != 3) {
  2001. res = -EINVAL;
  2002. goto err;
  2003. }
  2004. if (counter_set < 0 || counter_set >= IFS_MAX_COUNTER_SETS) {
  2005. pr_info("qtaguid: ctrl_counterset(%s): invalid counter_set range\n",
  2006. input);
  2007. res = -EINVAL;
  2008. goto err;
  2009. }
  2010. if (!can_manipulate_uids()) {
  2011. pr_info("qtaguid: ctrl_counterset(%s): "
  2012. "insufficient priv from pid=%u tgid=%u uid=%u\n",
  2013. input, current->pid, current->tgid, current_fsuid());
  2014. res = -EPERM;
  2015. goto err;
  2016. }
  2017. tag = make_tag_from_uid(uid);
  2018. spin_lock_bh(&tag_counter_set_list_lock);
  2019. tcs = tag_counter_set_tree_search(&tag_counter_set_tree, tag);
  2020. if (!tcs) {
  2021. tcs = kzalloc(sizeof(*tcs), GFP_ATOMIC);
  2022. if (!tcs) {
  2023. spin_unlock_bh(&tag_counter_set_list_lock);
  2024. pr_err("qtaguid: ctrl_counterset(%s): "
  2025. "failed to alloc counter set\n",
  2026. input);
  2027. res = -ENOMEM;
  2028. goto err;
  2029. }
  2030. tcs->tn.tag = tag;
  2031. tag_counter_set_tree_insert(tcs, &tag_counter_set_tree);
  2032. CT_DEBUG("qtaguid: ctrl_counterset(%s): added tcs tag=0x%llx "
  2033. "(uid=%u) set=%d\n",
  2034. input, tag, get_uid_from_tag(tag), counter_set);
  2035. }
  2036. tcs->active_set = counter_set;
  2037. spin_unlock_bh(&tag_counter_set_list_lock);
  2038. atomic64_inc(&qtu_events.counter_set_changes);
  2039. res = 0;
  2040. err:
  2041. return res;
  2042. }
  2043. static int ctrl_cmd_tag(const char *input)
  2044. {
  2045. char cmd;
  2046. int sock_fd = 0;
  2047. uid_t uid = 0;
  2048. tag_t acct_tag = make_atag_from_value(0);
  2049. tag_t full_tag;
  2050. struct socket *el_socket;
  2051. int res, argc;
  2052. struct sock_tag *sock_tag_entry;
  2053. struct tag_ref *tag_ref_entry;
  2054. struct uid_tag_data *uid_tag_data_entry;
  2055. struct proc_qtu_data *pqd_entry;
  2056. /* Unassigned args will get defaulted later. */
  2057. argc = sscanf(input, "%c %d %llu %u", &cmd, &sock_fd, &acct_tag, &uid);
  2058. CT_DEBUG("qtaguid: ctrl_tag(%s): argc=%d cmd=%c sock_fd=%d "
  2059. "acct_tag=0x%llx uid=%u\n", input, argc, cmd, sock_fd,
  2060. acct_tag, uid);
  2061. if (argc < 2) {
  2062. res = -EINVAL;
  2063. goto err;
  2064. }
  2065. el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */
  2066. if (!el_socket) {
  2067. pr_info("qtaguid: ctrl_tag(%s): failed to lookup"
  2068. " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n",
  2069. input, sock_fd, res, current->pid, current->tgid,
  2070. current_fsuid());
  2071. goto err;
  2072. }
  2073. CT_DEBUG("qtaguid: ctrl_tag(%s): socket->...->f_count=%ld ->sk=%p\n",
  2074. input, atomic_long_read(&el_socket->file->f_count),
  2075. el_socket->sk);
  2076. if (argc < 3) {
  2077. acct_tag = make_atag_from_value(0);
  2078. } else if (!valid_atag(acct_tag)) {
  2079. pr_info("qtaguid: ctrl_tag(%s): invalid tag\n", input);
  2080. res = -EINVAL;
  2081. goto err_put;
  2082. }
  2083. CT_DEBUG("qtaguid: ctrl_tag(%s): "
  2084. "pid=%u tgid=%u uid=%u euid=%u fsuid=%u "
  2085. "ctrl.gid=%u in_group()=%d in_egroup()=%d\n",
  2086. input, current->pid, current->tgid, current_uid(),
  2087. current_euid(), current_fsuid(),
  2088. xt_qtaguid_ctrl_file->gid,
  2089. in_group_p(xt_qtaguid_ctrl_file->gid),
  2090. in_egroup_p(xt_qtaguid_ctrl_file->gid));
  2091. if (argc < 4) {
  2092. uid = current_fsuid();
  2093. } else if (!can_impersonate_uid(uid)) {
  2094. pr_info("qtaguid: ctrl_tag(%s): "
  2095. "insufficient priv from pid=%u tgid=%u uid=%u\n",
  2096. input, current->pid, current->tgid, current_fsuid());
  2097. res = -EPERM;
  2098. goto err_put;
  2099. }
  2100. full_tag = combine_atag_with_uid(acct_tag, uid);
  2101. spin_lock_bh(&sock_tag_list_lock);
  2102. sock_tag_entry = get_sock_stat_nl(el_socket->sk);
  2103. tag_ref_entry = get_tag_ref(full_tag, &uid_tag_data_entry);
  2104. if (IS_ERR(tag_ref_entry)) {
  2105. res = PTR_ERR(tag_ref_entry);
  2106. spin_unlock_bh(&sock_tag_list_lock);
  2107. goto err_put;
  2108. }
  2109. tag_ref_entry->num_sock_tags++;
  2110. if (sock_tag_entry) {
  2111. struct tag_ref *prev_tag_ref_entry;
  2112. CT_DEBUG("qtaguid: ctrl_tag(%s): retag for sk=%p "
  2113. "st@%p ...->f_count=%ld\n",
  2114. input, el_socket->sk, sock_tag_entry,
  2115. atomic_long_read(&el_socket->file->f_count));
  2116. /*
  2117. * This is a re-tagging, so release the sock_fd that was
  2118. * locked at the time of the 1st tagging.
  2119. * There is still the ref from this call's sockfd_lookup() so
  2120. * it can be done within the spinlock.
  2121. */
  2122. sockfd_put(sock_tag_entry->socket);
  2123. prev_tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag,
  2124. &uid_tag_data_entry);
  2125. BUG_ON(IS_ERR_OR_NULL(prev_tag_ref_entry));
  2126. BUG_ON(prev_tag_ref_entry->num_sock_tags <= 0);
  2127. prev_tag_ref_entry->num_sock_tags--;
  2128. sock_tag_entry->tag = full_tag;
  2129. } else {
  2130. CT_DEBUG("qtaguid: ctrl_tag(%s): newtag for sk=%p\n",
  2131. input, el_socket->sk);
  2132. sock_tag_entry = kzalloc(sizeof(*sock_tag_entry),
  2133. GFP_ATOMIC);
  2134. if (!sock_tag_entry) {
  2135. pr_err("qtaguid: ctrl_tag(%s): "
  2136. "socket tag alloc failed\n",
  2137. input);
  2138. spin_unlock_bh(&sock_tag_list_lock);
  2139. res = -ENOMEM;
  2140. goto err_tag_unref_put;
  2141. }
  2142. sock_tag_entry->sk = el_socket->sk;
  2143. sock_tag_entry->socket = el_socket;
  2144. sock_tag_entry->pid = current->tgid;
  2145. sock_tag_entry->tag = combine_atag_with_uid(acct_tag,
  2146. uid);
  2147. spin_lock_bh(&uid_tag_data_tree_lock);
  2148. pqd_entry = proc_qtu_data_tree_search(
  2149. &proc_qtu_data_tree, current->tgid);
  2150. /*
  2151. * TODO: remove if, and start failing.
  2152. * At first, we want to catch user-space code that is not
  2153. * opening the /dev/xt_qtaguid.
  2154. */
  2155. if (IS_ERR_OR_NULL(pqd_entry))
  2156. pr_warn_once(
  2157. "qtaguid: %s(): "
  2158. "User space forgot to open /dev/xt_qtaguid? "
  2159. "pid=%u tgid=%u uid=%u\n", __func__,
  2160. current->pid, current->tgid,
  2161. current_fsuid());
  2162. else
  2163. list_add(&sock_tag_entry->list,
  2164. &pqd_entry->sock_tag_list);
  2165. spin_unlock_bh(&uid_tag_data_tree_lock);
  2166. sock_tag_tree_insert(sock_tag_entry, &sock_tag_tree);
  2167. atomic64_inc(&qtu_events.sockets_tagged);
  2168. }
  2169. spin_unlock_bh(&sock_tag_list_lock);
  2170. /* We keep the ref to the socket (file) until it is untagged */
  2171. CT_DEBUG("qtaguid: ctrl_tag(%s): done st@%p ...->f_count=%ld\n",
  2172. input, sock_tag_entry,
  2173. atomic_long_read(&el_socket->file->f_count));
  2174. return 0;
  2175. err_tag_unref_put:
  2176. BUG_ON(tag_ref_entry->num_sock_tags <= 0);
  2177. tag_ref_entry->num_sock_tags--;
  2178. free_tag_ref_from_utd_entry(tag_ref_entry, uid_tag_data_entry);
  2179. err_put:
  2180. CT_DEBUG("qtaguid: ctrl_tag(%s): done. ...->f_count=%ld\n",
  2181. input, atomic_long_read(&el_socket->file->f_count) - 1);
  2182. /* Release the sock_fd that was grabbed by sockfd_lookup(). */
  2183. sockfd_put(el_socket);
  2184. return res;
  2185. err:
  2186. CT_DEBUG("qtaguid: ctrl_tag(%s): done.\n", input);
  2187. return res;
  2188. }
  2189. static int ctrl_cmd_untag(const char *input)
  2190. {
  2191. char cmd;
  2192. int sock_fd = 0;
  2193. struct socket *el_socket;
  2194. int res, argc;
  2195. struct sock_tag *sock_tag_entry;
  2196. struct tag_ref *tag_ref_entry;
  2197. struct uid_tag_data *utd_entry;
  2198. struct proc_qtu_data *pqd_entry;
  2199. argc = sscanf(input, "%c %d", &cmd, &sock_fd);
  2200. CT_DEBUG("qtaguid: ctrl_untag(%s): argc=%d cmd=%c sock_fd=%d\n",
  2201. input, argc, cmd, sock_fd);
  2202. if (argc < 2) {
  2203. res = -EINVAL;
  2204. goto err;
  2205. }
  2206. el_socket = sockfd_lookup(sock_fd, &res); /* This locks the file */
  2207. if (!el_socket) {
  2208. pr_info("qtaguid: ctrl_untag(%s): failed to lookup"
  2209. " sock_fd=%d err=%d pid=%u tgid=%u uid=%u\n",
  2210. input, sock_fd, res, current->pid, current->tgid,
  2211. current_fsuid());
  2212. goto err;
  2213. }
  2214. CT_DEBUG("qtaguid: ctrl_untag(%s): socket->...->f_count=%ld ->sk=%p\n",
  2215. input, atomic_long_read(&el_socket->file->f_count),
  2216. el_socket->sk);
  2217. spin_lock_bh(&sock_tag_list_lock);
  2218. sock_tag_entry = get_sock_stat_nl(el_socket->sk);
  2219. if (!sock_tag_entry) {
  2220. spin_unlock_bh(&sock_tag_list_lock);
  2221. res = -EINVAL;
  2222. goto err_put;
  2223. }
  2224. /*
  2225. * The socket already belongs to the current process
  2226. * so it can do whatever it wants to it.
  2227. */
  2228. rb_erase(&sock_tag_entry->sock_node, &sock_tag_tree);
  2229. tag_ref_entry = lookup_tag_ref(sock_tag_entry->tag, &utd_entry);
  2230. BUG_ON(!tag_ref_entry);
  2231. BUG_ON(tag_ref_entry->num_sock_tags <= 0);
  2232. spin_lock_bh(&uid_tag_data_tree_lock);
  2233. pqd_entry = proc_qtu_data_tree_search(
  2234. &proc_qtu_data_tree, current->tgid);
  2235. /*
  2236. * TODO: remove if, and start failing.
  2237. * At first, we want to catch user-space code that is not
  2238. * opening the /dev/xt_qtaguid.
  2239. */
  2240. if (IS_ERR_OR_NULL(pqd_entry))
  2241. pr_warn_once("qtaguid: %s(): "
  2242. "User space forgot to open /dev/xt_qtaguid? "
  2243. "pid=%u tgid=%u uid=%u\n", __func__,
  2244. current->pid, current->tgid, current_fsuid());
  2245. else
  2246. list_del(&sock_tag_entry->list);
  2247. spin_unlock_bh(&uid_tag_data_tree_lock);
  2248. /*
  2249. * We don't free tag_ref from the utd_entry here,
  2250. * only during a cmd_delete().
  2251. */
  2252. tag_ref_entry->num_sock_tags--;
  2253. spin_unlock_bh(&sock_tag_list_lock);
  2254. /*
  2255. * Release the sock_fd that was grabbed at tag time,
  2256. * and once more for the sockfd_lookup() here.
  2257. */
  2258. sockfd_put(sock_tag_entry->socket);
  2259. CT_DEBUG("qtaguid: ctrl_untag(%s): done. st@%p ...->f_count=%ld\n",
  2260. input, sock_tag_entry,
  2261. atomic_long_read(&el_socket->file->f_count) - 1);
  2262. sockfd_put(el_socket);
  2263. kfree(sock_tag_entry);
  2264. atomic64_inc(&qtu_events.sockets_untagged);
  2265. return 0;
  2266. err_put:
  2267. CT_DEBUG("qtaguid: ctrl_untag(%s): done. socket->...->f_count=%ld\n",
  2268. input, atomic_long_read(&el_socket->file->f_count) - 1);
  2269. /* Release the sock_fd that was grabbed by sockfd_lookup(). */
  2270. sockfd_put(el_socket);
  2271. return res;
  2272. err:
  2273. CT_DEBUG("qtaguid: ctrl_untag(%s): done.\n", input);
  2274. return res;
  2275. }
  2276. static int qtaguid_ctrl_parse(const char *input, int count)
  2277. {
  2278. char cmd;
  2279. int res;
  2280. CT_DEBUG("qtaguid: ctrl(%s): pid=%u tgid=%u uid=%u\n",
  2281. input, current->pid, current->tgid, current_fsuid());
  2282. cmd = input[0];
  2283. /* Collect params for commands */
  2284. switch (cmd) {
  2285. case 'd':
  2286. res = ctrl_cmd_delete(input);
  2287. break;
  2288. case 's':
  2289. res = ctrl_cmd_counter_set(input);
  2290. break;
  2291. case 't':
  2292. res = ctrl_cmd_tag(input);
  2293. break;
  2294. case 'u':
  2295. res = ctrl_cmd_untag(input);
  2296. break;
  2297. default:
  2298. res = -EINVAL;
  2299. goto err;
  2300. }
  2301. if (!res)
  2302. res = count;
  2303. err:
  2304. CT_DEBUG("qtaguid: ctrl(%s): res=%d\n", input, res);
  2305. return res;
  2306. }
  2307. #define MAX_QTAGUID_CTRL_INPUT_LEN 255
  2308. static int qtaguid_ctrl_proc_write(struct file *file, const char __user *buffer,
  2309. unsigned long count, void *data)
  2310. {
  2311. char input_buf[MAX_QTAGUID_CTRL_INPUT_LEN];
  2312. if (unlikely(module_passive))
  2313. return count;
  2314. if (count >= MAX_QTAGUID_CTRL_INPUT_LEN)
  2315. return -EINVAL;
  2316. if (copy_from_user(input_buf, buffer, count))
  2317. return -EFAULT;
  2318. input_buf[count] = '\0';
  2319. return qtaguid_ctrl_parse(input_buf, count);
  2320. }
  2321. struct proc_print_info {
  2322. char *outp;
  2323. char **num_items_returned;
  2324. struct iface_stat *iface_entry;
  2325. struct tag_stat *ts_entry;
  2326. int item_index;
  2327. int items_to_skip;
  2328. int char_count;
  2329. };
  2330. static int pp_stats_line(struct proc_print_info *ppi, int cnt_set)
  2331. {
  2332. int len;
  2333. struct data_counters *cnts;
  2334. if (!ppi->item_index) {
  2335. if (ppi->item_index++ < ppi->items_to_skip)
  2336. return 0;
  2337. len = snprintf(ppi->outp, ppi->char_count,
  2338. "idx iface acct_tag_hex uid_tag_int cnt_set "
  2339. "rx_bytes rx_packets "
  2340. "tx_bytes tx_packets "
  2341. "rx_tcp_bytes rx_tcp_packets "
  2342. "rx_udp_bytes rx_udp_packets "
  2343. "rx_other_bytes rx_other_packets "
  2344. "tx_tcp_bytes tx_tcp_packets "
  2345. "tx_udp_bytes tx_udp_packets "
  2346. "tx_other_bytes tx_other_packets\n");
  2347. } else {
  2348. tag_t tag = ppi->ts_entry->tn.tag;
  2349. uid_t stat_uid = get_uid_from_tag(tag);
  2350. /* Detailed tags are not available to everybody */
  2351. if (get_atag_from_tag(tag)
  2352. && !can_read_other_uid_stats(stat_uid)) {
  2353. CT_DEBUG("qtaguid: stats line: "
  2354. "%s 0x%llx %u: insufficient priv "
  2355. "from pid=%u tgid=%u uid=%u stats.gid=%u\n",
  2356. ppi->iface_entry->ifname,
  2357. get_atag_from_tag(tag), stat_uid,
  2358. current->pid, current->tgid, current_fsuid(),
  2359. xt_qtaguid_stats_file->gid);
  2360. return 0;
  2361. }
  2362. if (ppi->item_index++ < ppi->items_to_skip)
  2363. return 0;
  2364. cnts = &ppi->ts_entry->counters;
  2365. len = snprintf(
  2366. ppi->outp, ppi->char_count,
  2367. "%d %s 0x%llx %u %u "
  2368. "%llu %llu "
  2369. "%llu %llu "
  2370. "%llu %llu "
  2371. "%llu %llu "
  2372. "%llu %llu "
  2373. "%llu %llu "
  2374. "%llu %llu "
  2375. "%llu %llu\n",
  2376. ppi->item_index,
  2377. ppi->iface_entry->ifname,
  2378. get_atag_from_tag(tag),
  2379. stat_uid,
  2380. cnt_set,
  2381. dc_sum_bytes(cnts, cnt_set, IFS_RX),
  2382. dc_sum_packets(cnts, cnt_set, IFS_RX),
  2383. dc_sum_bytes(cnts, cnt_set, IFS_TX),
  2384. dc_sum_packets(cnts, cnt_set, IFS_TX),
  2385. cnts->bpc[cnt_set][IFS_RX][IFS_TCP].bytes,
  2386. cnts->bpc[cnt_set][IFS_RX][IFS_TCP].packets,
  2387. cnts->bpc[cnt_set][IFS_RX][IFS_UDP].bytes,
  2388. cnts->bpc[cnt_set][IFS_RX][IFS_UDP].packets,
  2389. cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].bytes,
  2390. cnts->bpc[cnt_set][IFS_RX][IFS_PROTO_OTHER].packets,
  2391. cnts->bpc[cnt_set][IFS_TX][IFS_TCP].bytes,
  2392. cnts->bpc[cnt_set][IFS_TX][IFS_TCP].packets,
  2393. cnts->bpc[cnt_set][IFS_TX][IFS_UDP].bytes,
  2394. cnts->bpc[cnt_set][IFS_TX][IFS_UDP].packets,
  2395. cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].bytes,
  2396. cnts->bpc[cnt_set][IFS_TX][IFS_PROTO_OTHER].packets);
  2397. }
  2398. return len;
  2399. }
  2400. static bool pp_sets(struct proc_print_info *ppi)
  2401. {
  2402. int len;
  2403. int counter_set;
  2404. for (counter_set = 0; counter_set < IFS_MAX_COUNTER_SETS;
  2405. counter_set++) {
  2406. len = pp_stats_line(ppi, counter_set);
  2407. if (len >= ppi->char_count) {
  2408. *ppi->outp = '\0';
  2409. return false;
  2410. }
  2411. if (len) {
  2412. ppi->outp += len;
  2413. ppi->char_count -= len;
  2414. (*ppi->num_items_returned)++;
  2415. }
  2416. }
  2417. return true;
  2418. }
  2419. /*
  2420. * Procfs reader to get all tag stats using style "1)" as described in
  2421. * fs/proc/generic.c
  2422. * Groups all protocols tx/rx bytes.
  2423. */
  2424. static int qtaguid_stats_proc_read(char *page, char **num_items_returned,
  2425. off_t items_to_skip, int char_count, int *eof,
  2426. void *data)
  2427. {
  2428. struct proc_print_info ppi;
  2429. int len;
  2430. ppi.outp = page;
  2431. ppi.item_index = 0;
  2432. ppi.char_count = char_count;
  2433. ppi.num_items_returned = num_items_returned;
  2434. ppi.items_to_skip = items_to_skip;
  2435. if (unlikely(module_passive)) {
  2436. len = pp_stats_line(&ppi, 0);
  2437. /* The header should always be shorter than the buffer. */
  2438. BUG_ON(len >= ppi.char_count);
  2439. (*num_items_returned)++;
  2440. *eof = 1;
  2441. return len;
  2442. }
  2443. CT_DEBUG("qtaguid:proc stats pid=%u tgid=%u uid=%u "
  2444. "page=%p *num_items_returned=%p off=%ld "
  2445. "char_count=%d *eof=%d\n",
  2446. current->pid, current->tgid, current_fsuid(),
  2447. page, *num_items_returned,
  2448. items_to_skip, char_count, *eof);
  2449. if (*eof)
  2450. return 0;
  2451. /* The idx is there to help debug when things go belly up. */
  2452. len = pp_stats_line(&ppi, 0);
  2453. /* Don't advance the outp unless the whole line was printed */
  2454. if (len >= ppi.char_count) {
  2455. *ppi.outp = '\0';
  2456. return ppi.outp - page;
  2457. }
  2458. if (len) {
  2459. ppi.outp += len;
  2460. ppi.char_count -= len;
  2461. (*num_items_returned)++;
  2462. }
  2463. spin_lock_bh(&iface_stat_list_lock);
  2464. list_for_each_entry(ppi.iface_entry, &iface_stat_list, list) {
  2465. struct rb_node *node;
  2466. spin_lock_bh(&ppi.iface_entry->tag_stat_list_lock);
  2467. for (node = rb_first(&ppi.iface_entry->tag_stat_tree);
  2468. node;
  2469. node = rb_next(node)) {
  2470. ppi.ts_entry = rb_entry(node, struct tag_stat, tn.node);
  2471. if (!pp_sets(&ppi)) {
  2472. spin_unlock_bh(
  2473. &ppi.iface_entry->tag_stat_list_lock);
  2474. spin_unlock_bh(&iface_stat_list_lock);
  2475. return ppi.outp - page;
  2476. }
  2477. }
  2478. spin_unlock_bh(&ppi.iface_entry->tag_stat_list_lock);
  2479. }
  2480. spin_unlock_bh(&iface_stat_list_lock);
  2481. *eof = 1;
  2482. return ppi.outp - page;
  2483. }
  2484. /*------------------------------------------*/
  2485. static int qtudev_open(struct inode *inode, struct file *file)
  2486. {
  2487. struct uid_tag_data *utd_entry;
  2488. struct proc_qtu_data *pqd_entry;
  2489. struct proc_qtu_data *new_pqd_entry;
  2490. int res;
  2491. bool utd_entry_found;
  2492. if (unlikely(qtu_proc_handling_passive))
  2493. return 0;
  2494. DR_DEBUG("qtaguid: qtudev_open(): pid=%u tgid=%u uid=%u\n",
  2495. current->pid, current->tgid, current_fsuid());
  2496. spin_lock_bh(&uid_tag_data_tree_lock);
  2497. /* Look for existing uid data, or alloc one. */
  2498. utd_entry = get_uid_data(current_fsuid(), &utd_entry_found);
  2499. if (IS_ERR_OR_NULL(utd_entry)) {
  2500. res = PTR_ERR(utd_entry);
  2501. goto err_unlock;
  2502. }
  2503. /* Look for existing PID based proc_data */
  2504. pqd_entry = proc_qtu_data_tree_search(&proc_qtu_data_tree,
  2505. current->tgid);
  2506. if (pqd_entry) {
  2507. pr_err("qtaguid: qtudev_open(): %u/%u %u "
  2508. "%s already opened\n",
  2509. current->pid, current->tgid, current_fsuid(),
  2510. QTU_DEV_NAME);
  2511. res = -EBUSY;
  2512. goto err_unlock_free_utd;
  2513. }
  2514. new_pqd_entry = kzalloc(sizeof(*new_pqd_entry), GFP_ATOMIC);
  2515. if (!new_pqd_entry) {
  2516. pr_err("qtaguid: qtudev_open(): %u/%u %u: "
  2517. "proc data alloc failed\n",
  2518. current->pid, current->tgid, current_fsuid());
  2519. res = -ENOMEM;
  2520. goto err_unlock_free_utd;
  2521. }
  2522. new_pqd_entry->pid = current->tgid;
  2523. INIT_LIST_HEAD(&new_pqd_entry->sock_tag_list);
  2524. new_pqd_entry->parent_tag_data = utd_entry;
  2525. utd_entry->num_pqd++;
  2526. proc_qtu_data_tree_insert(new_pqd_entry,
  2527. &proc_qtu_data_tree);
  2528. spin_unlock_bh(&uid_tag_data_tree_lock);
  2529. DR_DEBUG("qtaguid: tracking data for uid=%u in pqd=%p\n",
  2530. current_fsuid(), new_pqd_entry);
  2531. file->private_data = new_pqd_entry;
  2532. return 0;
  2533. err_unlock_free_utd:
  2534. if (!utd_entry_found) {
  2535. rb_erase(&utd_entry->node, &uid_tag_data_tree);
  2536. kfree(utd_entry);
  2537. }
  2538. err_unlock:
  2539. spin_unlock_bh(&uid_tag_data_tree_lock);
  2540. return res;
  2541. }
  2542. static int qtudev_release(struct inode *inode, struct file *file)
  2543. {
  2544. struct proc_qtu_data *pqd_entry = file->private_data;
  2545. struct uid_tag_data *utd_entry = pqd_entry->parent_tag_data;
  2546. struct sock_tag *st_entry;
  2547. struct rb_root st_to_free_tree = RB_ROOT;
  2548. struct list_head *entry, *next;
  2549. struct tag_ref *tr;
  2550. if (unlikely(qtu_proc_handling_passive))
  2551. return 0;
  2552. /*
  2553. * Do not trust the current->pid, it might just be a kworker cleaning
  2554. * up after a dead proc.
  2555. */
  2556. DR_DEBUG("qtaguid: qtudev_release(): "
  2557. "pid=%u tgid=%u uid=%u "
  2558. "pqd_entry=%p->pid=%u utd_entry=%p->active_tags=%d\n",
  2559. current->pid, current->tgid, pqd_entry->parent_tag_data->uid,
  2560. pqd_entry, pqd_entry->pid, utd_entry,
  2561. utd_entry->num_active_tags);
  2562. spin_lock_bh(&sock_tag_list_lock);
  2563. spin_lock_bh(&uid_tag_data_tree_lock);
  2564. list_for_each_safe(entry, next, &pqd_entry->sock_tag_list) {
  2565. st_entry = list_entry(entry, struct sock_tag, list);
  2566. DR_DEBUG("qtaguid: %s(): "
  2567. "erase sock_tag=%p->sk=%p pid=%u tgid=%u uid=%u\n",
  2568. __func__,
  2569. st_entry, st_entry->sk,
  2570. current->pid, current->tgid,
  2571. pqd_entry->parent_tag_data->uid);
  2572. utd_entry = uid_tag_data_tree_search(
  2573. &uid_tag_data_tree,
  2574. get_uid_from_tag(st_entry->tag));
  2575. BUG_ON(IS_ERR_OR_NULL(utd_entry));
  2576. DR_DEBUG("qtaguid: %s(): "
  2577. "looking for tag=0x%llx in utd_entry=%p\n", __func__,
  2578. st_entry->tag, utd_entry);
  2579. tr = tag_ref_tree_search(&utd_entry->tag_ref_tree,
  2580. st_entry->tag);
  2581. BUG_ON(!tr);
  2582. BUG_ON(tr->num_sock_tags <= 0);
  2583. tr->num_sock_tags--;
  2584. free_tag_ref_from_utd_entry(tr, utd_entry);
  2585. rb_erase(&st_entry->sock_node, &sock_tag_tree);
  2586. list_del(&st_entry->list);
  2587. /* Can't sockfd_put() within spinlock, do it later. */
  2588. sock_tag_tree_insert(st_entry, &st_to_free_tree);
  2589. /*
  2590. * Try to free the utd_entry if no other proc_qtu_data is
  2591. * using it (num_pqd is 0) and it doesn't have active tags
  2592. * (num_active_tags is 0).
  2593. */
  2594. put_utd_entry(utd_entry);
  2595. }
  2596. rb_erase(&pqd_entry->node, &proc_qtu_data_tree);
  2597. BUG_ON(pqd_entry->parent_tag_data->num_pqd < 1);
  2598. pqd_entry->parent_tag_data->num_pqd--;
  2599. put_utd_entry(pqd_entry->parent_tag_data);
  2600. kfree(pqd_entry);
  2601. file->private_data = NULL;
  2602. spin_unlock_bh(&uid_tag_data_tree_lock);
  2603. spin_unlock_bh(&sock_tag_list_lock);
  2604. sock_tag_tree_erase(&st_to_free_tree);
  2605. prdebug_full_state(0, "%s(): pid=%u tgid=%u", __func__,
  2606. current->pid, current->tgid);
  2607. return 0;
  2608. }
  2609. /*------------------------------------------*/
  2610. static const struct file_operations qtudev_fops = {
  2611. .owner = THIS_MODULE,
  2612. .open = qtudev_open,
  2613. .release = qtudev_release,
  2614. };
  2615. static struct miscdevice qtu_device = {
  2616. .minor = MISC_DYNAMIC_MINOR,
  2617. .name = QTU_DEV_NAME,
  2618. .fops = &qtudev_fops,
  2619. /* How sad it doesn't allow for defaults: .mode = S_IRUGO | S_IWUSR */
  2620. };
  2621. /*------------------------------------------*/
  2622. static int __init qtaguid_proc_register(struct proc_dir_entry **res_procdir)
  2623. {
  2624. int ret;
  2625. *res_procdir = proc_mkdir(module_procdirname, init_net.proc_net);
  2626. if (!*res_procdir) {
  2627. pr_err("qtaguid: failed to create proc/.../xt_qtaguid\n");
  2628. ret = -ENOMEM;
  2629. goto no_dir;
  2630. }
  2631. xt_qtaguid_ctrl_file = create_proc_entry("ctrl", proc_ctrl_perms,
  2632. *res_procdir);
  2633. if (!xt_qtaguid_ctrl_file) {
  2634. pr_err("qtaguid: failed to create xt_qtaguid/ctrl "
  2635. " file\n");
  2636. ret = -ENOMEM;
  2637. goto no_ctrl_entry;
  2638. }
  2639. xt_qtaguid_ctrl_file->read_proc = qtaguid_ctrl_proc_read;
  2640. xt_qtaguid_ctrl_file->write_proc = qtaguid_ctrl_proc_write;
  2641. xt_qtaguid_stats_file = create_proc_entry("stats", proc_stats_perms,
  2642. *res_procdir);
  2643. if (!xt_qtaguid_stats_file) {
  2644. pr_err("qtaguid: failed to create xt_qtaguid/stats "
  2645. "file\n");
  2646. ret = -ENOMEM;
  2647. goto no_stats_entry;
  2648. }
  2649. xt_qtaguid_stats_file->read_proc = qtaguid_stats_proc_read;
  2650. /*
  2651. * TODO: add support counter hacking
  2652. * xt_qtaguid_stats_file->write_proc = qtaguid_stats_proc_write;
  2653. */
  2654. return 0;
  2655. no_stats_entry:
  2656. remove_proc_entry("ctrl", *res_procdir);
  2657. no_ctrl_entry:
  2658. remove_proc_entry("xt_qtaguid", NULL);
  2659. no_dir:
  2660. return ret;
  2661. }
  2662. static struct xt_match qtaguid_mt_reg __read_mostly = {
  2663. /*
  2664. * This module masquerades as the "owner" module so that iptables
  2665. * tools can deal with it.
  2666. */
  2667. .name = "owner",
  2668. .revision = 1,
  2669. .family = NFPROTO_UNSPEC,
  2670. .match = qtaguid_mt,
  2671. .matchsize = sizeof(struct xt_qtaguid_match_info),
  2672. .me = THIS_MODULE,
  2673. };
  2674. static int __init qtaguid_mt_init(void)
  2675. {
  2676. if (qtaguid_proc_register(&xt_qtaguid_procdir)
  2677. || iface_stat_init(xt_qtaguid_procdir)
  2678. || xt_register_match(&qtaguid_mt_reg)
  2679. || misc_register(&qtu_device))
  2680. return -1;
  2681. return 0;
  2682. }
  2683. /*
  2684. * TODO: allow unloading of the module.
  2685. * For now stats are permanent.
  2686. * Kconfig forces'y/n' and never an 'm'.
  2687. */
  2688. module_init(qtaguid_mt_init);
  2689. MODULE_AUTHOR("jpa <jpa@google.com>");
  2690. MODULE_DESCRIPTION("Xtables: socket owner+tag matching and associated stats");
  2691. MODULE_LICENSE("GPL");
  2692. MODULE_ALIAS("ipt_owner");
  2693. MODULE_ALIAS("ip6t_owner");
  2694. MODULE_ALIAS("ipt_qtaguid");
  2695. MODULE_ALIAS("ip6t_qtaguid");