nf_conntrack_ecache.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * connection tracking event cache.
  3. */
  4. #ifndef _NF_CONNTRACK_ECACHE_H
  5. #define _NF_CONNTRACK_ECACHE_H
  6. #include <net/netfilter/nf_conntrack.h>
  7. #include <net/net_namespace.h>
  8. #include <net/netfilter/nf_conntrack_expect.h>
  9. #include <linux/netfilter/nf_conntrack_common.h>
  10. #include <linux/netfilter/nf_conntrack_tuple_common.h>
  11. #include <net/netfilter/nf_conntrack_extend.h>
  12. struct nf_conntrack_ecache {
  13. unsigned long cache; /* bitops want long */
  14. unsigned long missed; /* missed events */
  15. u16 ctmask; /* bitmask of ct events to be delivered */
  16. u16 expmask; /* bitmask of expect events to be delivered */
  17. u32 portid; /* netlink portid of destroyer */
  18. };
  19. static inline struct nf_conntrack_ecache *
  20. nf_ct_ecache_find(const struct nf_conn *ct)
  21. {
  22. #ifdef CONFIG_NF_CONNTRACK_EVENTS
  23. return nf_ct_ext_find(ct, NF_CT_EXT_ECACHE);
  24. #else
  25. return NULL;
  26. #endif
  27. }
  28. static inline struct nf_conntrack_ecache *
  29. nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp)
  30. {
  31. #ifdef CONFIG_NF_CONNTRACK_EVENTS
  32. struct net *net = nf_ct_net(ct);
  33. struct nf_conntrack_ecache *e;
  34. if (!ctmask && !expmask && net->ct.sysctl_events) {
  35. ctmask = ~0;
  36. expmask = ~0;
  37. }
  38. if (!ctmask && !expmask)
  39. return NULL;
  40. e = nf_ct_ext_add(ct, NF_CT_EXT_ECACHE, gfp);
  41. if (e) {
  42. e->ctmask = ctmask;
  43. e->expmask = expmask;
  44. }
  45. return e;
  46. #else
  47. return NULL;
  48. #endif
  49. };
  50. #ifdef CONFIG_NF_CONNTRACK_EVENTS
  51. /* This structure is passed to event handler */
  52. struct nf_ct_event {
  53. struct nf_conn *ct;
  54. u32 portid;
  55. int report;
  56. };
  57. struct nf_ct_event_notifier {
  58. int (*fcn)(unsigned int events, struct nf_ct_event *item);
  59. };
  60. int nf_conntrack_register_notifier(struct net *net,
  61. struct nf_ct_event_notifier *nb);
  62. void nf_conntrack_unregister_notifier(struct net *net,
  63. struct nf_ct_event_notifier *nb);
  64. void nf_ct_deliver_cached_events(struct nf_conn *ct);
  65. static inline void
  66. nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
  67. {
  68. struct net *net = nf_ct_net(ct);
  69. struct nf_conntrack_ecache *e;
  70. if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb))
  71. return;
  72. e = nf_ct_ecache_find(ct);
  73. if (e == NULL)
  74. return;
  75. set_bit(event, &e->cache);
  76. }
  77. static inline int
  78. nf_conntrack_eventmask_report(unsigned int eventmask,
  79. struct nf_conn *ct,
  80. u32 portid,
  81. int report)
  82. {
  83. int ret = 0;
  84. struct net *net = nf_ct_net(ct);
  85. struct nf_ct_event_notifier *notify;
  86. struct nf_conntrack_ecache *e;
  87. rcu_read_lock();
  88. notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
  89. if (notify == NULL)
  90. goto out_unlock;
  91. e = nf_ct_ecache_find(ct);
  92. if (e == NULL)
  93. goto out_unlock;
  94. if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
  95. struct nf_ct_event item = {
  96. .ct = ct,
  97. .portid = e->portid ? e->portid : portid,
  98. .report = report
  99. };
  100. /* This is a resent of a destroy event? If so, skip missed */
  101. unsigned long missed = e->portid ? 0 : e->missed;
  102. if (!((eventmask | missed) & e->ctmask))
  103. goto out_unlock;
  104. ret = notify->fcn(eventmask | missed, &item);
  105. if (unlikely(ret < 0 || missed)) {
  106. spin_lock_bh(&ct->lock);
  107. if (ret < 0) {
  108. /* This is a destroy event that has been
  109. * triggered by a process, we store the PORTID
  110. * to include it in the retransmission. */
  111. if (eventmask & (1 << IPCT_DESTROY) &&
  112. e->portid == 0 && portid != 0)
  113. e->portid = portid;
  114. else
  115. e->missed |= eventmask;
  116. } else
  117. e->missed &= ~missed;
  118. spin_unlock_bh(&ct->lock);
  119. }
  120. }
  121. out_unlock:
  122. rcu_read_unlock();
  123. return ret;
  124. }
  125. static inline int
  126. nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
  127. u32 portid, int report)
  128. {
  129. return nf_conntrack_eventmask_report(1 << event, ct, portid, report);
  130. }
  131. static inline int
  132. nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
  133. {
  134. return nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
  135. }
  136. struct nf_exp_event {
  137. struct nf_conntrack_expect *exp;
  138. u32 portid;
  139. int report;
  140. };
  141. struct nf_exp_event_notifier {
  142. int (*fcn)(unsigned int events, struct nf_exp_event *item);
  143. };
  144. int nf_ct_expect_register_notifier(struct net *net,
  145. struct nf_exp_event_notifier *nb);
  146. void nf_ct_expect_unregister_notifier(struct net *net,
  147. struct nf_exp_event_notifier *nb);
  148. static inline void
  149. nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
  150. struct nf_conntrack_expect *exp,
  151. u32 portid,
  152. int report)
  153. {
  154. struct net *net = nf_ct_exp_net(exp);
  155. struct nf_exp_event_notifier *notify;
  156. struct nf_conntrack_ecache *e;
  157. rcu_read_lock();
  158. notify = rcu_dereference(net->ct.nf_expect_event_cb);
  159. if (notify == NULL)
  160. goto out_unlock;
  161. e = nf_ct_ecache_find(exp->master);
  162. if (e == NULL)
  163. goto out_unlock;
  164. if (e->expmask & (1 << event)) {
  165. struct nf_exp_event item = {
  166. .exp = exp,
  167. .portid = portid,
  168. .report = report
  169. };
  170. notify->fcn(1 << event, &item);
  171. }
  172. out_unlock:
  173. rcu_read_unlock();
  174. }
  175. static inline void
  176. nf_ct_expect_event(enum ip_conntrack_expect_events event,
  177. struct nf_conntrack_expect *exp)
  178. {
  179. nf_ct_expect_event_report(event, exp, 0, 0);
  180. }
  181. int nf_conntrack_ecache_pernet_init(struct net *net);
  182. void nf_conntrack_ecache_pernet_fini(struct net *net);
  183. int nf_conntrack_ecache_init(void);
  184. void nf_conntrack_ecache_fini(void);
  185. static inline void nf_conntrack_ecache_delayed_work(struct net *net)
  186. {
  187. if (!delayed_work_pending(&net->ct.ecache_dwork)) {
  188. schedule_delayed_work(&net->ct.ecache_dwork, HZ);
  189. net->ct.ecache_dwork_pending = true;
  190. }
  191. }
  192. static inline void nf_conntrack_ecache_work(struct net *net)
  193. {
  194. if (net->ct.ecache_dwork_pending) {
  195. net->ct.ecache_dwork_pending = false;
  196. mod_delayed_work(system_wq, &net->ct.ecache_dwork, 0);
  197. }
  198. }
  199. #else /* CONFIG_NF_CONNTRACK_EVENTS */
  200. static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
  201. struct nf_conn *ct) {}
  202. static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
  203. struct nf_conn *ct,
  204. u32 portid,
  205. int report) { return 0; }
  206. static inline int nf_conntrack_event(enum ip_conntrack_events event,
  207. struct nf_conn *ct) { return 0; }
  208. static inline int nf_conntrack_event_report(enum ip_conntrack_events event,
  209. struct nf_conn *ct,
  210. u32 portid,
  211. int report) { return 0; }
  212. static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
  213. static inline void nf_ct_expect_event(enum ip_conntrack_expect_events event,
  214. struct nf_conntrack_expect *exp) {}
  215. static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
  216. struct nf_conntrack_expect *exp,
  217. u32 portid,
  218. int report) {}
  219. static inline int nf_conntrack_ecache_pernet_init(struct net *net)
  220. {
  221. return 0;
  222. }
  223. static inline void nf_conntrack_ecache_pernet_fini(struct net *net)
  224. {
  225. }
  226. static inline int nf_conntrack_ecache_init(void)
  227. {
  228. return 0;
  229. }
  230. static inline void nf_conntrack_ecache_fini(void)
  231. {
  232. }
  233. static inline void nf_conntrack_ecache_delayed_work(struct net *net)
  234. {
  235. }
  236. static inline void nf_conntrack_ecache_work(struct net *net)
  237. {
  238. }
  239. #endif /* CONFIG_NF_CONNTRACK_EVENTS */
  240. #endif /*_NF_CONNTRACK_ECACHE_H*/