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