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