PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/net/netfilter/xt_qtaguid_internal.h

https://gitlab.com/webhaikal/SenseiOneplus3
C Header | 352 lines | 188 code | 40 blank | 124 comment | 3 complexity | d5879f5f5a1f7b7311ed9ed5262c09bc MD5 | raw file
  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. #ifndef __XT_QTAGUID_INTERNAL_H__
  11. #define __XT_QTAGUID_INTERNAL_H__
  12. #include <linux/types.h>
  13. #include <linux/rbtree.h>
  14. #include <linux/spinlock_types.h>
  15. #include <linux/workqueue.h>
  16. /* Iface handling */
  17. #define IDEBUG_MASK (1<<0)
  18. /* Iptable Matching. Per packet. */
  19. #define MDEBUG_MASK (1<<1)
  20. /* Red-black tree handling. Per packet. */
  21. #define RDEBUG_MASK (1<<2)
  22. /* procfs ctrl/stats handling */
  23. #define CDEBUG_MASK (1<<3)
  24. /* dev and resource tracking */
  25. #define DDEBUG_MASK (1<<4)
  26. /* E.g (IDEBUG_MASK | CDEBUG_MASK | DDEBUG_MASK) */
  27. #define DEFAULT_DEBUG_MASK 0
  28. /*
  29. * (Un)Define these *DEBUG to compile out/in the pr_debug calls.
  30. * All undef: text size ~ 0x3030; all def: ~ 0x4404.
  31. */
  32. #define IDEBUG
  33. #define MDEBUG
  34. #define RDEBUG
  35. #define CDEBUG
  36. #define DDEBUG
  37. #define MSK_DEBUG(mask, ...) do { \
  38. if (unlikely(qtaguid_debug_mask & (mask))) \
  39. pr_debug(__VA_ARGS__); \
  40. } while (0)
  41. #ifdef IDEBUG
  42. #define IF_DEBUG(...) MSK_DEBUG(IDEBUG_MASK, __VA_ARGS__)
  43. #else
  44. #define IF_DEBUG(...) no_printk(__VA_ARGS__)
  45. #endif
  46. #ifdef MDEBUG
  47. #define MT_DEBUG(...) MSK_DEBUG(MDEBUG_MASK, __VA_ARGS__)
  48. #else
  49. #define MT_DEBUG(...) no_printk(__VA_ARGS__)
  50. #endif
  51. #ifdef RDEBUG
  52. #define RB_DEBUG(...) MSK_DEBUG(RDEBUG_MASK, __VA_ARGS__)
  53. #else
  54. #define RB_DEBUG(...) no_printk(__VA_ARGS__)
  55. #endif
  56. #ifdef CDEBUG
  57. #define CT_DEBUG(...) MSK_DEBUG(CDEBUG_MASK, __VA_ARGS__)
  58. #else
  59. #define CT_DEBUG(...) no_printk(__VA_ARGS__)
  60. #endif
  61. #ifdef DDEBUG
  62. #define DR_DEBUG(...) MSK_DEBUG(DDEBUG_MASK, __VA_ARGS__)
  63. #else
  64. #define DR_DEBUG(...) no_printk(__VA_ARGS__)
  65. #endif
  66. extern uint qtaguid_debug_mask;
  67. /*---------------------------------------------------------------------------*/
  68. /*
  69. * Tags:
  70. *
  71. * They represent what the data usage counters will be tracked against.
  72. * By default a tag is just based on the UID.
  73. * The UID is used as the base for policing, and can not be ignored.
  74. * So a tag will always at least represent a UID (uid_tag).
  75. *
  76. * A tag can be augmented with an "accounting tag" which is associated
  77. * with a UID.
  78. * User space can set the acct_tag portion of the tag which is then used
  79. * with sockets: all data belonging to that socket will be counted against the
  80. * tag. The policing is then based on the tag's uid_tag portion,
  81. * and stats are collected for the acct_tag portion separately.
  82. *
  83. * There could be
  84. * a: {acct_tag=1, uid_tag=10003}
  85. * b: {acct_tag=2, uid_tag=10003}
  86. * c: {acct_tag=3, uid_tag=10003}
  87. * d: {acct_tag=0, uid_tag=10003}
  88. * a, b, and c represent tags associated with specific sockets.
  89. * d is for the totals for that uid, including all untagged traffic.
  90. * Typically d is used with policing/quota rules.
  91. *
  92. * We want tag_t big enough to distinguish uid_t and acct_tag.
  93. * It might become a struct if needed.
  94. * Nothing should be using it as an int.
  95. */
  96. typedef uint64_t tag_t; /* Only used via accessors */
  97. #define TAG_UID_MASK 0xFFFFFFFFULL
  98. #define TAG_ACCT_MASK (~0xFFFFFFFFULL)
  99. static inline int tag_compare(tag_t t1, tag_t t2)
  100. {
  101. return t1 < t2 ? -1 : t1 == t2 ? 0 : 1;
  102. }
  103. static inline tag_t combine_atag_with_uid(tag_t acct_tag, uid_t uid)
  104. {
  105. return acct_tag | uid;
  106. }
  107. static inline tag_t make_tag_from_uid(uid_t uid)
  108. {
  109. return uid;
  110. }
  111. static inline uid_t get_uid_from_tag(tag_t tag)
  112. {
  113. return tag & TAG_UID_MASK;
  114. }
  115. static inline tag_t get_utag_from_tag(tag_t tag)
  116. {
  117. return tag & TAG_UID_MASK;
  118. }
  119. static inline tag_t get_atag_from_tag(tag_t tag)
  120. {
  121. return tag & TAG_ACCT_MASK;
  122. }
  123. static inline bool valid_atag(tag_t tag)
  124. {
  125. return !(tag & TAG_UID_MASK);
  126. }
  127. static inline tag_t make_atag_from_value(uint32_t value)
  128. {
  129. return (uint64_t)value << 32;
  130. }
  131. /*---------------------------------------------------------------------------*/
  132. /*
  133. * Maximum number of socket tags that a UID is allowed to have active.
  134. * Multiple processes belonging to the same UID contribute towards this limit.
  135. * Special UIDs that can impersonate a UID also contribute (e.g. download
  136. * manager, ...)
  137. */
  138. #define DEFAULT_MAX_SOCK_TAGS 1024
  139. /*
  140. * For now we only track 2 sets of counters.
  141. * The default set is 0.
  142. * Userspace can activate another set for a given uid being tracked.
  143. */
  144. #define IFS_MAX_COUNTER_SETS 2
  145. enum ifs_tx_rx {
  146. IFS_TX,
  147. IFS_RX,
  148. IFS_MAX_DIRECTIONS
  149. };
  150. /* For now, TCP, UDP, the rest */
  151. enum ifs_proto {
  152. IFS_TCP,
  153. IFS_UDP,
  154. IFS_PROTO_OTHER,
  155. IFS_MAX_PROTOS
  156. };
  157. struct byte_packet_counters {
  158. uint64_t bytes;
  159. uint64_t packets;
  160. };
  161. struct data_counters {
  162. struct byte_packet_counters bpc[IFS_MAX_COUNTER_SETS][IFS_MAX_DIRECTIONS][IFS_MAX_PROTOS];
  163. };
  164. static inline uint64_t dc_sum_bytes(struct data_counters *counters,
  165. int set,
  166. enum ifs_tx_rx direction)
  167. {
  168. return counters->bpc[set][direction][IFS_TCP].bytes
  169. + counters->bpc[set][direction][IFS_UDP].bytes
  170. + counters->bpc[set][direction][IFS_PROTO_OTHER].bytes;
  171. }
  172. static inline uint64_t dc_sum_packets(struct data_counters *counters,
  173. int set,
  174. enum ifs_tx_rx direction)
  175. {
  176. return counters->bpc[set][direction][IFS_TCP].packets
  177. + counters->bpc[set][direction][IFS_UDP].packets
  178. + counters->bpc[set][direction][IFS_PROTO_OTHER].packets;
  179. }
  180. /* Generic X based nodes used as a base for rb_tree ops */
  181. struct tag_node {
  182. struct rb_node node;
  183. tag_t tag;
  184. };
  185. struct tag_stat {
  186. struct tag_node tn;
  187. struct data_counters counters;
  188. /*
  189. * If this tag is acct_tag based, we need to count against the
  190. * matching parent uid_tag.
  191. */
  192. struct data_counters *parent_counters;
  193. };
  194. struct iface_stat {
  195. struct list_head list; /* in iface_stat_list */
  196. char *ifname;
  197. bool active;
  198. /* net_dev is only valid for active iface_stat */
  199. struct net_device *net_dev;
  200. struct byte_packet_counters totals_via_dev[IFS_MAX_DIRECTIONS];
  201. struct data_counters totals_via_skb;
  202. /*
  203. * We keep the last_known, because some devices reset their counters
  204. * just before NETDEV_UP, while some will reset just before
  205. * NETDEV_REGISTER (which is more normal).
  206. * So now, if the device didn't do a NETDEV_UNREGISTER and we see
  207. * its current dev stats smaller that what was previously known, we
  208. * assume an UNREGISTER and just use the last_known.
  209. */
  210. struct byte_packet_counters last_known[IFS_MAX_DIRECTIONS];
  211. /* last_known is usable when last_known_valid is true */
  212. bool last_known_valid;
  213. struct proc_dir_entry *proc_ptr;
  214. struct rb_root tag_stat_tree;
  215. spinlock_t tag_stat_list_lock;
  216. };
  217. /* This is needed to create proc_dir_entries from atomic context. */
  218. struct iface_stat_work {
  219. struct work_struct iface_work;
  220. struct iface_stat *iface_entry;
  221. };
  222. /*
  223. * Track tag that this socket is transferring data for, and not necessarily
  224. * the uid that owns the socket.
  225. * This is the tag against which tag_stat.counters will be billed.
  226. * These structs need to be looked up by sock and pid.
  227. */
  228. struct sock_tag {
  229. struct rb_node sock_node;
  230. struct sock *sk; /* Only used as a number, never dereferenced */
  231. /* The socket is needed for sockfd_put() */
  232. struct socket *socket;
  233. /* Used to associate with a given pid */
  234. struct list_head list; /* in proc_qtu_data.sock_tag_list */
  235. pid_t pid;
  236. tag_t tag;
  237. };
  238. struct qtaguid_event_counts {
  239. /* Various successful events */
  240. atomic64_t sockets_tagged;
  241. atomic64_t sockets_untagged;
  242. atomic64_t counter_set_changes;
  243. atomic64_t delete_cmds;
  244. atomic64_t iface_events; /* Number of NETDEV_* events handled */
  245. atomic64_t match_calls; /* Number of times iptables called mt */
  246. /* Number of times iptables called mt from pre or post routing hooks */
  247. atomic64_t match_calls_prepost;
  248. /*
  249. * match_found_sk_*: numbers related to the netfilter matching
  250. * function finding a sock for the sk_buff.
  251. * Total skbs processed is sum(match_found*).
  252. */
  253. atomic64_t match_found_sk; /* An sk was already in the sk_buff. */
  254. /* The connection tracker had or didn't have the sk. */
  255. atomic64_t match_found_sk_in_ct;
  256. atomic64_t match_found_no_sk_in_ct;
  257. /*
  258. * No sk could be found. No apparent owner. Could happen with
  259. * unsolicited traffic.
  260. */
  261. atomic64_t match_no_sk;
  262. /*
  263. * The file ptr in the sk_socket wasn't there.
  264. * This might happen for traffic while the socket is being closed.
  265. */
  266. atomic64_t match_no_sk_file;
  267. };
  268. /* Track the set active_set for the given tag. */
  269. struct tag_counter_set {
  270. struct tag_node tn;
  271. int active_set;
  272. };
  273. /*----------------------------------------------*/
  274. /*
  275. * The qtu uid data is used to track resources that are created directly or
  276. * indirectly by processes (uid tracked).
  277. * It is shared by the processes with the same uid.
  278. * Some of the resource will be counted to prevent further rogue allocations,
  279. * some will need freeing once the owner process (uid) exits.
  280. */
  281. struct uid_tag_data {
  282. struct rb_node node;
  283. uid_t uid;
  284. /*
  285. * For the uid, how many accounting tags have been set.
  286. */
  287. int num_active_tags;
  288. /* Track the number of proc_qtu_data that reference it */
  289. int num_pqd;
  290. struct rb_root tag_ref_tree;
  291. /* No tag_node_tree_lock; use uid_tag_data_tree_lock */
  292. };
  293. struct tag_ref {
  294. struct tag_node tn;
  295. /*
  296. * This tracks the number of active sockets that have a tag on them
  297. * which matches this tag_ref.tn.tag.
  298. * A tag ref can live on after the sockets are untagged.
  299. * A tag ref can only be removed during a tag delete command.
  300. */
  301. int num_sock_tags;
  302. };
  303. struct proc_qtu_data {
  304. struct rb_node node;
  305. pid_t pid;
  306. struct uid_tag_data *parent_tag_data;
  307. /* Tracks the sock_tags that need freeing upon this proc's death */
  308. struct list_head sock_tag_list;
  309. /* No spinlock_t sock_tag_list_lock; use the global one. */
  310. };
  311. /*----------------------------------------------*/
  312. #endif /* ifndef __XT_QTAGUID_INTERNAL_H__ */