blob: 60af9b6b9e00c92a4a01ce1e024993173db4fdf4 [file] [log] [blame]
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001/* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
3 *
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
Harald Weltedc808fe2006-03-20 17:56:32 -08005 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08006 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08007 * (C) 2005-2006 by Pablo Neira Ayuso <pablo@eurodev.net>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08008 *
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08009 * Initial connection tracking via netlink development funded and
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080010 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
11 *
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
13 *
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080016 */
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/types.h>
22#include <linux/timer.h>
23#include <linux/skbuff.h>
24#include <linux/errno.h>
25#include <linux/netlink.h>
26#include <linux/spinlock.h>
Yasuyuki Kozakai40a839f2006-06-27 03:00:35 -070027#include <linux/interrupt.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080028#include <linux/notifier.h>
29
30#include <linux/netfilter.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070031#include <net/netlink.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080032#include <net/netfilter/nf_conntrack.h>
33#include <net/netfilter/nf_conntrack_core.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010034#include <net/netfilter/nf_conntrack_expect.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080035#include <net/netfilter/nf_conntrack_helper.h>
36#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010037#include <net/netfilter/nf_conntrack_l4proto.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080038#include <net/netfilter/nf_conntrack_tuple.h>
39#ifdef CONFIG_NF_NAT_NEEDED
40#include <net/netfilter/nf_nat_core.h>
41#include <net/netfilter/nf_nat_protocol.h>
42#endif
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080043
44#include <linux/netfilter/nfnetlink.h>
45#include <linux/netfilter/nfnetlink_conntrack.h>
46
47MODULE_LICENSE("GPL");
48
Harald Weltedc808fe2006-03-20 17:56:32 -080049static char __initdata version[] = "0.93";
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080050
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080051static inline int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080052ctnetlink_dump_tuples_proto(struct sk_buff *skb,
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080053 const struct nf_conntrack_tuple *tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +010054 struct nf_conntrack_l4proto *l4proto)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080055{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080056 int ret = 0;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080057 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080058
59 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
60
Martin Josefsson605dcad2006-11-29 02:35:06 +010061 if (likely(l4proto->tuple_to_nfattr))
62 ret = l4proto->tuple_to_nfattr(skb, tuple);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080063
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080064 NFA_NEST_END(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080065
66 return ret;
67
68nfattr_failure:
69 return -1;
70}
71
72static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080073ctnetlink_dump_tuples_ip(struct sk_buff *skb,
74 const struct nf_conntrack_tuple *tuple,
75 struct nf_conntrack_l3proto *l3proto)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080076{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080077 int ret = 0;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080078 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
79
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080080 if (likely(l3proto->tuple_to_nfattr))
81 ret = l3proto->tuple_to_nfattr(skb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080082
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080083 NFA_NEST_END(skb, nest_parms);
84
85 return ret;
86
87nfattr_failure:
88 return -1;
89}
90
91static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080092ctnetlink_dump_tuples(struct sk_buff *skb,
93 const struct nf_conntrack_tuple *tuple)
94{
95 int ret;
96 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +010097 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080098
99 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
100 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
101 nf_ct_l3proto_put(l3proto);
102
103 if (unlikely(ret < 0))
104 return ret;
105
Martin Josefsson605dcad2006-11-29 02:35:06 +0100106 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
107 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
108 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -0800109
110 return ret;
111}
112
113static inline int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800114ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
115{
Patrick McHardybff9a892006-12-02 22:05:08 -0800116 __be32 status = htonl((u_int32_t) ct->status);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800117 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
118 return 0;
119
120nfattr_failure:
121 return -1;
122}
123
124static inline int
125ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
126{
127 long timeout_l = ct->timeout.expires - jiffies;
Patrick McHardybff9a892006-12-02 22:05:08 -0800128 __be32 timeout;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800129
130 if (timeout_l < 0)
131 timeout = 0;
132 else
133 timeout = htonl(timeout_l / HZ);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800134
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800135 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
136 return 0;
137
138nfattr_failure:
139 return -1;
140}
141
142static inline int
143ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
144{
Martin Josefsson605dcad2006-11-29 02:35:06 +0100145 struct nf_conntrack_l4proto *l4proto = nf_ct_l4proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800146 struct nfattr *nest_proto;
147 int ret;
148
Martin Josefsson605dcad2006-11-29 02:35:06 +0100149 if (!l4proto->to_nfattr) {
150 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800151 return 0;
152 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800153
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800154 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
155
Martin Josefsson605dcad2006-11-29 02:35:06 +0100156 ret = l4proto->to_nfattr(skb, nest_proto, ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800157
Martin Josefsson605dcad2006-11-29 02:35:06 +0100158 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800159
160 NFA_NEST_END(skb, nest_proto);
161
162 return ret;
163
164nfattr_failure:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100165 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800166 return -1;
167}
168
169static inline int
170ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
171{
172 struct nfattr *nest_helper;
Harald Weltedc808fe2006-03-20 17:56:32 -0800173 const struct nf_conn_help *help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700174 struct nf_conntrack_helper *helper;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800175
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700176 if (!help)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800177 return 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800178
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700179 rcu_read_lock();
180 helper = rcu_dereference(help->helper);
181 if (!helper)
182 goto out;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800183
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700184 nest_helper = NFA_NEST(skb, CTA_HELP);
185 NFA_PUT(skb, CTA_HELP_NAME, strlen(helper->name), helper->name);
186
187 if (helper->to_nfattr)
188 helper->to_nfattr(skb, ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800189
190 NFA_NEST_END(skb, nest_helper);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700191out:
192 rcu_read_unlock();
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800193 return 0;
194
195nfattr_failure:
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700196 rcu_read_unlock();
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800197 return -1;
198}
199
200#ifdef CONFIG_NF_CT_ACCT
201static inline int
202ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
203 enum ip_conntrack_dir dir)
204{
205 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
206 struct nfattr *nest_count = NFA_NEST(skb, type);
Patrick McHardybff9a892006-12-02 22:05:08 -0800207 __be32 tmp;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800208
209 tmp = htonl(ct->counters[dir].packets);
210 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
211
212 tmp = htonl(ct->counters[dir].bytes);
213 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
214
215 NFA_NEST_END(skb, nest_count);
216
217 return 0;
218
219nfattr_failure:
220 return -1;
221}
222#else
223#define ctnetlink_dump_counters(a, b, c) (0)
224#endif
225
226#ifdef CONFIG_NF_CONNTRACK_MARK
227static inline int
228ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
229{
Patrick McHardybff9a892006-12-02 22:05:08 -0800230 __be32 mark = htonl(ct->mark);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800231
232 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
233 return 0;
234
235nfattr_failure:
236 return -1;
237}
238#else
239#define ctnetlink_dump_mark(a, b) (0)
240#endif
241
242static inline int
243ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
244{
Patrick McHardybff9a892006-12-02 22:05:08 -0800245 __be32 id = htonl(ct->id);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800246 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
247 return 0;
248
249nfattr_failure:
250 return -1;
251}
252
253static inline int
254ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
255{
Patrick McHardybff9a892006-12-02 22:05:08 -0800256 __be32 use = htonl(atomic_read(&ct->ct_general.use));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800257
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800258 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
259 return 0;
260
261nfattr_failure:
262 return -1;
263}
264
265#define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
266
267static int
268ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800269 int event, int nowait,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800270 const struct nf_conn *ct)
271{
272 struct nlmsghdr *nlh;
273 struct nfgenmsg *nfmsg;
274 struct nfattr *nest_parms;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700275 unsigned char *b = skb_tail_pointer(skb);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800276
277 event |= NFNL_SUBSYS_CTNETLINK << 8;
278 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
279 nfmsg = NLMSG_DATA(nlh);
280
281 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800282 nfmsg->nfgen_family =
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800283 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
284 nfmsg->version = NFNETLINK_V0;
285 nfmsg->res_id = 0;
286
287 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
288 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
289 goto nfattr_failure;
290 NFA_NEST_END(skb, nest_parms);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800291
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800292 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
293 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
294 goto nfattr_failure;
295 NFA_NEST_END(skb, nest_parms);
296
297 if (ctnetlink_dump_status(skb, ct) < 0 ||
298 ctnetlink_dump_timeout(skb, ct) < 0 ||
299 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
300 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
301 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
302 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
303 ctnetlink_dump_mark(skb, ct) < 0 ||
304 ctnetlink_dump_id(skb, ct) < 0 ||
305 ctnetlink_dump_use(skb, ct) < 0)
306 goto nfattr_failure;
307
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700308 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800309 return skb->len;
310
311nlmsg_failure:
312nfattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700313 nlmsg_trim(skb, b);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800314 return -1;
315}
316
317#ifdef CONFIG_NF_CONNTRACK_EVENTS
318static int ctnetlink_conntrack_event(struct notifier_block *this,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800319 unsigned long events, void *ptr)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800320{
321 struct nlmsghdr *nlh;
322 struct nfgenmsg *nfmsg;
323 struct nfattr *nest_parms;
324 struct nf_conn *ct = (struct nf_conn *)ptr;
325 struct sk_buff *skb;
326 unsigned int type;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700327 sk_buff_data_t b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800328 unsigned int flags = 0, group;
329
330 /* ignore our fake conntrack entry */
331 if (ct == &nf_conntrack_untracked)
332 return NOTIFY_DONE;
333
334 if (events & IPCT_DESTROY) {
335 type = IPCTNL_MSG_CT_DELETE;
336 group = NFNLGRP_CONNTRACK_DESTROY;
337 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
338 type = IPCTNL_MSG_CT_NEW;
339 flags = NLM_F_CREATE|NLM_F_EXCL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800340 group = NFNLGRP_CONNTRACK_NEW;
Pablo Neira Ayuso1a315262006-08-22 00:32:23 -0700341 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800342 type = IPCTNL_MSG_CT_NEW;
343 group = NFNLGRP_CONNTRACK_UPDATE;
344 } else
345 return NOTIFY_DONE;
Patrick McHardya2427692006-03-20 18:03:59 -0800346
347 if (!nfnetlink_has_listeners(group))
348 return NOTIFY_DONE;
349
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800350 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
351 if (!skb)
352 return NOTIFY_DONE;
353
354 b = skb->tail;
355
356 type |= NFNL_SUBSYS_CTNETLINK << 8;
357 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
358 nfmsg = NLMSG_DATA(nlh);
359
360 nlh->nlmsg_flags = flags;
361 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
362 nfmsg->version = NFNETLINK_V0;
363 nfmsg->res_id = 0;
364
365 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
366 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
367 goto nfattr_failure;
368 NFA_NEST_END(skb, nest_parms);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800369
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800370 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
371 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
372 goto nfattr_failure;
373 NFA_NEST_END(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800374
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100375 if (events & IPCT_DESTROY) {
376 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
377 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
378 goto nfattr_failure;
379 } else {
380 if (ctnetlink_dump_status(skb, ct) < 0)
381 goto nfattr_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800382
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100383 if (ctnetlink_dump_timeout(skb, ct) < 0)
384 goto nfattr_failure;
385
386 if (events & IPCT_PROTOINFO
387 && ctnetlink_dump_protoinfo(skb, ct) < 0)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800388 goto nfattr_failure;
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100389
390 if ((events & IPCT_HELPER || nfct_help(ct))
391 && ctnetlink_dump_helpinfo(skb, ct) < 0)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800392 goto nfattr_failure;
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100393
Patrick McHardy40e0cb02007-02-02 19:33:11 -0800394#ifdef CONFIG_NF_CONNTRACK_MARK
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100395 if ((events & IPCT_MARK || ct->mark)
396 && ctnetlink_dump_mark(skb, ct) < 0)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800397 goto nfattr_failure;
Patrick McHardy40e0cb02007-02-02 19:33:11 -0800398#endif
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100399
400 if (events & IPCT_COUNTER_FILLING &&
401 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
402 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
403 goto nfattr_failure;
404 }
Pablo Neira Ayusob9a37e02006-08-22 00:31:49 -0700405
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800406 nlh->nlmsg_len = skb->tail - b;
407 nfnetlink_send(skb, 0, group, 0);
408 return NOTIFY_DONE;
409
410nlmsg_failure:
411nfattr_failure:
412 kfree_skb(skb);
413 return NOTIFY_DONE;
414}
415#endif /* CONFIG_NF_CONNTRACK_EVENTS */
416
417static int ctnetlink_done(struct netlink_callback *cb)
418{
Patrick McHardy89f2e212006-05-29 18:24:58 -0700419 if (cb->args[1])
420 nf_ct_put((struct nf_conn *)cb->args[1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800421 return 0;
422}
423
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800424#define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
425
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800426static int
427ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
428{
Patrick McHardy89f2e212006-05-29 18:24:58 -0700429 struct nf_conn *ct, *last;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800430 struct nf_conntrack_tuple_hash *h;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700431 struct hlist_node *n;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800432 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
433 u_int8_t l3proto = nfmsg->nfgen_family;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800434
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800435 read_lock_bh(&nf_conntrack_lock);
Patrick McHardyd205dc42006-08-17 18:12:38 -0700436 last = (struct nf_conn *)cb->args[1];
Patrick McHardy89f2e212006-05-29 18:24:58 -0700437 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
438restart:
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700439 hlist_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
440 hnode) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800441 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800442 continue;
443 ct = nf_ct_tuplehash_to_ctrack(h);
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800444 /* Dump entries of a given L3 protocol number.
445 * If it is not specified, ie. l3proto == 0,
446 * then dump everything. */
447 if (l3proto && L3PROTO(ct) != l3proto)
448 continue;
Patrick McHardyd205dc42006-08-17 18:12:38 -0700449 if (cb->args[1]) {
450 if (ct != last)
Patrick McHardy89f2e212006-05-29 18:24:58 -0700451 continue;
Patrick McHardyd205dc42006-08-17 18:12:38 -0700452 cb->args[1] = 0;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700453 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800454 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800455 cb->nlh->nlmsg_seq,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800456 IPCTNL_MSG_CT_NEW,
Patrick McHardy89f2e212006-05-29 18:24:58 -0700457 1, ct) < 0) {
458 nf_conntrack_get(&ct->ct_general);
459 cb->args[1] = (unsigned long)ct;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800460 goto out;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700461 }
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700462#ifdef CONFIG_NF_CT_ACCT
463 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
464 IPCTNL_MSG_CT_GET_CTRZERO)
465 memset(&ct->counters, 0, sizeof(ct->counters));
466#endif
Patrick McHardy89f2e212006-05-29 18:24:58 -0700467 }
Patrick McHardyd205dc42006-08-17 18:12:38 -0700468 if (cb->args[1]) {
Patrick McHardy89f2e212006-05-29 18:24:58 -0700469 cb->args[1] = 0;
470 goto restart;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800471 }
472 }
Patrick McHardy89f2e212006-05-29 18:24:58 -0700473out:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800474 read_unlock_bh(&nf_conntrack_lock);
Patrick McHardyd205dc42006-08-17 18:12:38 -0700475 if (last)
476 nf_ct_put(last);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800477
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800478 return skb->len;
479}
480
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800481static inline int
482ctnetlink_parse_tuple_ip(struct nfattr *attr, struct nf_conntrack_tuple *tuple)
483{
484 struct nfattr *tb[CTA_IP_MAX];
485 struct nf_conntrack_l3proto *l3proto;
486 int ret = 0;
487
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800488 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
489
490 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
491
492 if (likely(l3proto->nfattr_to_tuple))
493 ret = l3proto->nfattr_to_tuple(tb, tuple);
494
495 nf_ct_l3proto_put(l3proto);
496
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800497 return ret;
498}
499
500static const size_t cta_min_proto[CTA_PROTO_MAX] = {
501 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
502};
503
504static inline int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800505ctnetlink_parse_tuple_proto(struct nfattr *attr,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800506 struct nf_conntrack_tuple *tuple)
507{
508 struct nfattr *tb[CTA_PROTO_MAX];
Martin Josefsson605dcad2006-11-29 02:35:06 +0100509 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800510 int ret = 0;
511
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800512 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
513
514 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
515 return -EINVAL;
516
517 if (!tb[CTA_PROTO_NUM-1])
518 return -EINVAL;
519 tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
520
Martin Josefsson605dcad2006-11-29 02:35:06 +0100521 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800522
Martin Josefsson605dcad2006-11-29 02:35:06 +0100523 if (likely(l4proto->nfattr_to_tuple))
524 ret = l4proto->nfattr_to_tuple(tb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800525
Martin Josefsson605dcad2006-11-29 02:35:06 +0100526 nf_ct_l4proto_put(l4proto);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800527
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800528 return ret;
529}
530
531static inline int
532ctnetlink_parse_tuple(struct nfattr *cda[], struct nf_conntrack_tuple *tuple,
533 enum ctattr_tuple type, u_int8_t l3num)
534{
535 struct nfattr *tb[CTA_TUPLE_MAX];
536 int err;
537
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800538 memset(tuple, 0, sizeof(*tuple));
539
540 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
541
542 if (!tb[CTA_TUPLE_IP-1])
543 return -EINVAL;
544
545 tuple->src.l3num = l3num;
546
547 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
548 if (err < 0)
549 return err;
550
551 if (!tb[CTA_TUPLE_PROTO-1])
552 return -EINVAL;
553
554 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
555 if (err < 0)
556 return err;
557
558 /* orig and expect tuples get DIR_ORIGINAL */
559 if (type == CTA_TUPLE_REPLY)
560 tuple->dst.dir = IP_CT_DIR_REPLY;
561 else
562 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
563
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800564 return 0;
565}
566
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800567#ifdef CONFIG_NF_NAT_NEEDED
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800568static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = {
569 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
570 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
571};
572
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800573static int nfnetlink_parse_nat_proto(struct nfattr *attr,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800574 const struct nf_conn *ct,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800575 struct nf_nat_range *range)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800576{
577 struct nfattr *tb[CTA_PROTONAT_MAX];
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800578 struct nf_nat_protocol *npt;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800579
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800580 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
581
582 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
583 return -EINVAL;
584
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800585 npt = nf_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800586
587 if (!npt->nfattr_to_range) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800588 nf_nat_proto_put(npt);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800589 return 0;
590 }
591
592 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
593 if (npt->nfattr_to_range(tb, range) > 0)
594 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
595
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800596 nf_nat_proto_put(npt);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800597
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800598 return 0;
599}
600
601static const size_t cta_min_nat[CTA_NAT_MAX] = {
602 [CTA_NAT_MINIP-1] = sizeof(u_int32_t),
603 [CTA_NAT_MAXIP-1] = sizeof(u_int32_t),
604};
605
606static inline int
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800607nfnetlink_parse_nat(struct nfattr *nat,
608 const struct nf_conn *ct, struct nf_nat_range *range)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800609{
610 struct nfattr *tb[CTA_NAT_MAX];
611 int err;
612
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800613 memset(range, 0, sizeof(*range));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800614
Patrick McHardy3726add2006-05-29 18:24:39 -0700615 nfattr_parse_nested(tb, CTA_NAT_MAX, nat);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800616
617 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
618 return -EINVAL;
619
620 if (tb[CTA_NAT_MINIP-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800621 range->min_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800622
623 if (!tb[CTA_NAT_MAXIP-1])
624 range->max_ip = range->min_ip;
625 else
Patrick McHardybff9a892006-12-02 22:05:08 -0800626 range->max_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800627
628 if (range->min_ip)
629 range->flags |= IP_NAT_RANGE_MAP_IPS;
630
631 if (!tb[CTA_NAT_PROTO-1])
632 return 0;
633
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800634 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800635 if (err < 0)
636 return err;
637
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800638 return 0;
639}
640#endif
641
642static inline int
643ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
644{
645 struct nfattr *tb[CTA_HELP_MAX];
646
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800647 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
648
649 if (!tb[CTA_HELP_NAME-1])
650 return -EINVAL;
651
652 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
653
654 return 0;
655}
656
657static const size_t cta_min[CTA_MAX] = {
658 [CTA_STATUS-1] = sizeof(u_int32_t),
659 [CTA_TIMEOUT-1] = sizeof(u_int32_t),
660 [CTA_MARK-1] = sizeof(u_int32_t),
661 [CTA_USE-1] = sizeof(u_int32_t),
662 [CTA_ID-1] = sizeof(u_int32_t)
663};
664
665static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800666ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700667 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800668{
669 struct nf_conntrack_tuple_hash *h;
670 struct nf_conntrack_tuple tuple;
671 struct nf_conn *ct;
672 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
673 u_int8_t u3 = nfmsg->nfgen_family;
674 int err = 0;
675
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800676 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
677 return -EINVAL;
678
679 if (cda[CTA_TUPLE_ORIG-1])
680 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
681 else if (cda[CTA_TUPLE_REPLY-1])
682 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
683 else {
684 /* Flush the whole table */
685 nf_conntrack_flush();
686 return 0;
687 }
688
689 if (err < 0)
690 return err;
691
Patrick McHardy330f7db2007-07-07 22:28:42 -0700692 h = nf_conntrack_find_get(&tuple);
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700693 if (!h)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800694 return -ENOENT;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800695
696 ct = nf_ct_tuplehash_to_ctrack(h);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800697
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800698 if (cda[CTA_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -0800699 u_int32_t id = ntohl(*(__be32 *)NFA_DATA(cda[CTA_ID-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800700 if (ct->id != id) {
701 nf_ct_put(ct);
702 return -ENOENT;
703 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800704 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800705 if (del_timer(&ct->timeout))
706 ct->timeout.function((unsigned long)ct);
707
708 nf_ct_put(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800709
710 return 0;
711}
712
713static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800714ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700715 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800716{
717 struct nf_conntrack_tuple_hash *h;
718 struct nf_conntrack_tuple tuple;
719 struct nf_conn *ct;
720 struct sk_buff *skb2 = NULL;
721 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
722 u_int8_t u3 = nfmsg->nfgen_family;
723 int err = 0;
724
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800725 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700726#ifndef CONFIG_NF_CT_ACCT
727 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800728 return -ENOTSUPP;
729#endif
Thomas Grafc702e802007-03-22 23:30:55 -0700730 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
731 ctnetlink_done);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800732 }
733
734 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
735 return -EINVAL;
736
737 if (cda[CTA_TUPLE_ORIG-1])
738 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
739 else if (cda[CTA_TUPLE_REPLY-1])
740 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
741 else
742 return -EINVAL;
743
744 if (err < 0)
745 return err;
746
Patrick McHardy330f7db2007-07-07 22:28:42 -0700747 h = nf_conntrack_find_get(&tuple);
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700748 if (!h)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800749 return -ENOENT;
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700750
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800751 ct = nf_ct_tuplehash_to_ctrack(h);
752
753 err = -ENOMEM;
754 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
755 if (!skb2) {
756 nf_ct_put(ct);
757 return -ENOMEM;
758 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800759
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800760 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800761 IPCTNL_MSG_CT_NEW, 1, ct);
762 nf_ct_put(ct);
763 if (err <= 0)
764 goto free;
765
766 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
767 if (err < 0)
768 goto out;
769
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800770 return 0;
771
772free:
773 kfree_skb(skb2);
774out:
775 return err;
776}
777
778static inline int
779ctnetlink_change_status(struct nf_conn *ct, struct nfattr *cda[])
780{
781 unsigned long d;
Patrick McHardybff9a892006-12-02 22:05:08 -0800782 unsigned int status = ntohl(*(__be32 *)NFA_DATA(cda[CTA_STATUS-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800783 d = ct->status ^ status;
784
785 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
786 /* unchangeable */
787 return -EINVAL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800788
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800789 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
790 /* SEEN_REPLY bit can only be set */
791 return -EINVAL;
792
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800793
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800794 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
795 /* ASSURED bit can only be set */
796 return -EINVAL;
797
Patrick McHardy3726add2006-05-29 18:24:39 -0700798 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800799#ifndef CONFIG_NF_NAT_NEEDED
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800800 return -EINVAL;
801#else
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800802 struct nf_nat_range range;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800803
Patrick McHardy3726add2006-05-29 18:24:39 -0700804 if (cda[CTA_NAT_DST-1]) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800805 if (nfnetlink_parse_nat(cda[CTA_NAT_DST-1], ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700806 &range) < 0)
807 return -EINVAL;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800808 if (nf_nat_initialized(ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700809 HOOK2MANIP(NF_IP_PRE_ROUTING)))
810 return -EEXIST;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800811 nf_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
Patrick McHardy3726add2006-05-29 18:24:39 -0700812 }
813 if (cda[CTA_NAT_SRC-1]) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800814 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC-1], ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700815 &range) < 0)
816 return -EINVAL;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800817 if (nf_nat_initialized(ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700818 HOOK2MANIP(NF_IP_POST_ROUTING)))
819 return -EEXIST;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800820 nf_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
Patrick McHardy3726add2006-05-29 18:24:39 -0700821 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800822#endif
823 }
824
825 /* Be careful here, modifying NAT bits can screw up things,
826 * so don't let users modify them directly if they don't pass
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800827 * nf_nat_range. */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800828 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
829 return 0;
830}
831
832
833static inline int
834ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
835{
836 struct nf_conntrack_helper *helper;
Harald Weltedc808fe2006-03-20 17:56:32 -0800837 struct nf_conn_help *help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800838 char *helpname;
839 int err;
840
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800841 /* don't change helper of sibling connections */
842 if (ct->master)
843 return -EINVAL;
844
845 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
846 if (err < 0)
847 return err;
848
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700849 if (!strcmp(helpname, "")) {
850 if (help && help->helper) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800851 /* we had a helper before ... */
852 nf_ct_remove_expectations(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700853 rcu_assign_pointer(help->helper, NULL);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800854 }
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700855
856 return 0;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800857 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800858
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700859 helper = __nf_conntrack_helper_find_byname(helpname);
860 if (helper == NULL)
861 return -EINVAL;
862
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700863 if (help) {
864 if (help->helper == helper)
865 return 0;
866 if (help->helper)
867 return -EBUSY;
868 /* need to zero data of old helper */
869 memset(&help->help, 0, sizeof(help->help));
870 } else {
871 help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, GFP_KERNEL);
872 if (help == NULL)
873 return -ENOMEM;
874 }
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700875
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700876 rcu_assign_pointer(help->helper, helper);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800877
878 return 0;
879}
880
881static inline int
882ctnetlink_change_timeout(struct nf_conn *ct, struct nfattr *cda[])
883{
Patrick McHardybff9a892006-12-02 22:05:08 -0800884 u_int32_t timeout = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800885
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800886 if (!del_timer(&ct->timeout))
887 return -ETIME;
888
889 ct->timeout.expires = jiffies + timeout * HZ;
890 add_timer(&ct->timeout);
891
892 return 0;
893}
894
895static inline int
896ctnetlink_change_protoinfo(struct nf_conn *ct, struct nfattr *cda[])
897{
898 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
Martin Josefsson605dcad2006-11-29 02:35:06 +0100899 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800900 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
901 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
902 int err = 0;
903
904 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
905
Martin Josefsson605dcad2006-11-29 02:35:06 +0100906 l4proto = nf_ct_l4proto_find_get(l3num, npt);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800907
Martin Josefsson605dcad2006-11-29 02:35:06 +0100908 if (l4proto->from_nfattr)
909 err = l4proto->from_nfattr(tb, ct);
910 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800911
912 return err;
913}
914
915static int
916ctnetlink_change_conntrack(struct nf_conn *ct, struct nfattr *cda[])
917{
918 int err;
919
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800920 if (cda[CTA_HELP-1]) {
921 err = ctnetlink_change_helper(ct, cda);
922 if (err < 0)
923 return err;
924 }
925
926 if (cda[CTA_TIMEOUT-1]) {
927 err = ctnetlink_change_timeout(ct, cda);
928 if (err < 0)
929 return err;
930 }
931
932 if (cda[CTA_STATUS-1]) {
933 err = ctnetlink_change_status(ct, cda);
934 if (err < 0)
935 return err;
936 }
937
938 if (cda[CTA_PROTOINFO-1]) {
939 err = ctnetlink_change_protoinfo(ct, cda);
940 if (err < 0)
941 return err;
942 }
943
Martin Josefssonbcd1e832006-04-01 02:23:21 -0800944#if defined(CONFIG_NF_CONNTRACK_MARK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800945 if (cda[CTA_MARK-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800946 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800947#endif
948
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800949 return 0;
950}
951
952static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800953ctnetlink_create_conntrack(struct nfattr *cda[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800954 struct nf_conntrack_tuple *otuple,
955 struct nf_conntrack_tuple *rtuple)
956{
957 struct nf_conn *ct;
958 int err = -EINVAL;
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -0800959 struct nf_conn_help *help;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700960 struct nf_conntrack_helper *helper;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800961
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800962 ct = nf_conntrack_alloc(otuple, rtuple);
963 if (ct == NULL || IS_ERR(ct))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800964 return -ENOMEM;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800965
966 if (!cda[CTA_TIMEOUT-1])
967 goto err;
Patrick McHardybff9a892006-12-02 22:05:08 -0800968 ct->timeout.expires = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800969
970 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
971 ct->status |= IPS_CONFIRMED;
972
Pablo Neira Ayusobbb33572006-11-29 02:35:31 +0100973 if (cda[CTA_STATUS-1]) {
974 err = ctnetlink_change_status(ct, cda);
975 if (err < 0)
976 goto err;
977 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800978
979 if (cda[CTA_PROTOINFO-1]) {
980 err = ctnetlink_change_protoinfo(ct, cda);
981 if (err < 0)
Patrick McHardyc54ea3b2007-01-15 17:16:03 -0800982 goto err;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800983 }
984
Martin Josefssonbcd1e832006-04-01 02:23:21 -0800985#if defined(CONFIG_NF_CONNTRACK_MARK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800986 if (cda[CTA_MARK-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800987 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800988#endif
989
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700990 helper = nf_ct_helper_find_get(rtuple);
991 if (helper) {
992 help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, GFP_KERNEL);
993 if (help == NULL) {
994 nf_ct_helper_put(helper);
995 err = -ENOMEM;
996 goto err;
997 }
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700998 /* not in hash table yet so not strictly necessary */
999 rcu_assign_pointer(help->helper, helper);
1000 }
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -08001001
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001002 add_timer(&ct->timeout);
1003 nf_conntrack_hash_insert(ct);
1004
Patrick McHarrdy3c158f72007-06-05 12:55:27 -07001005 if (helper)
1006 nf_ct_helper_put(helper);
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -08001007
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001008 return 0;
1009
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001010err:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001011 nf_conntrack_free(ct);
1012 return err;
1013}
1014
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001015static int
1016ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001017 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001018{
1019 struct nf_conntrack_tuple otuple, rtuple;
1020 struct nf_conntrack_tuple_hash *h = NULL;
1021 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1022 u_int8_t u3 = nfmsg->nfgen_family;
1023 int err = 0;
1024
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001025 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1026 return -EINVAL;
1027
1028 if (cda[CTA_TUPLE_ORIG-1]) {
1029 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1030 if (err < 0)
1031 return err;
1032 }
1033
1034 if (cda[CTA_TUPLE_REPLY-1]) {
1035 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1036 if (err < 0)
1037 return err;
1038 }
1039
1040 write_lock_bh(&nf_conntrack_lock);
1041 if (cda[CTA_TUPLE_ORIG-1])
1042 h = __nf_conntrack_find(&otuple, NULL);
1043 else if (cda[CTA_TUPLE_REPLY-1])
1044 h = __nf_conntrack_find(&rtuple, NULL);
1045
1046 if (h == NULL) {
1047 write_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001048 err = -ENOENT;
1049 if (nlh->nlmsg_flags & NLM_F_CREATE)
1050 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1051 return err;
1052 }
1053 /* implicit 'else' */
1054
1055 /* we only allow nat config for new conntracks */
Patrick McHardy3726add2006-05-29 18:24:39 -07001056 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001057 err = -EINVAL;
1058 goto out_unlock;
1059 }
1060
1061 /* We manipulate the conntrack inside the global conntrack table lock,
1062 * so there's no need to increase the refcount */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001063 err = -EEXIST;
1064 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1065 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), cda);
1066
1067out_unlock:
1068 write_unlock_bh(&nf_conntrack_lock);
1069 return err;
1070}
1071
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001072/***********************************************************************
1073 * EXPECT
1074 ***********************************************************************/
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001075
1076static inline int
1077ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1078 const struct nf_conntrack_tuple *tuple,
1079 enum ctattr_expect type)
1080{
1081 struct nfattr *nest_parms = NFA_NEST(skb, type);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001082
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001083 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1084 goto nfattr_failure;
1085
1086 NFA_NEST_END(skb, nest_parms);
1087
1088 return 0;
1089
1090nfattr_failure:
1091 return -1;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001092}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001093
1094static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001095ctnetlink_exp_dump_mask(struct sk_buff *skb,
1096 const struct nf_conntrack_tuple *tuple,
Patrick McHardyd4156e82007-07-07 22:31:32 -07001097 const struct nf_conntrack_tuple_mask *mask)
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001098{
1099 int ret;
1100 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001101 struct nf_conntrack_l4proto *l4proto;
Patrick McHardyd4156e82007-07-07 22:31:32 -07001102 struct nf_conntrack_tuple m;
1103 struct nfattr *nest_parms;
1104
1105 memset(&m, 0xFF, sizeof(m));
1106 m.src.u.all = mask->src.u.all;
1107 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1108
1109 nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001110
1111 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
Patrick McHardyd4156e82007-07-07 22:31:32 -07001112 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001113 nf_ct_l3proto_put(l3proto);
1114
1115 if (unlikely(ret < 0))
1116 goto nfattr_failure;
1117
Martin Josefsson605dcad2006-11-29 02:35:06 +01001118 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
Patrick McHardyd4156e82007-07-07 22:31:32 -07001119 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
Martin Josefsson605dcad2006-11-29 02:35:06 +01001120 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001121 if (unlikely(ret < 0))
1122 goto nfattr_failure;
1123
1124 NFA_NEST_END(skb, nest_parms);
1125
1126 return 0;
1127
1128nfattr_failure:
1129 return -1;
1130}
1131
1132static inline int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001133ctnetlink_exp_dump_expect(struct sk_buff *skb,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001134 const struct nf_conntrack_expect *exp)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001135{
1136 struct nf_conn *master = exp->master;
Patrick McHardybff9a892006-12-02 22:05:08 -08001137 __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1138 __be32 id = htonl(exp->id);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001139
1140 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1141 goto nfattr_failure;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001142 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001143 goto nfattr_failure;
1144 if (ctnetlink_exp_dump_tuple(skb,
1145 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1146 CTA_EXPECT_MASTER) < 0)
1147 goto nfattr_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001148
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001149 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1150 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1151
1152 return 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001153
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001154nfattr_failure:
1155 return -1;
1156}
1157
1158static int
1159ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001160 int event,
1161 int nowait,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001162 const struct nf_conntrack_expect *exp)
1163{
1164 struct nlmsghdr *nlh;
1165 struct nfgenmsg *nfmsg;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001166 unsigned char *b = skb_tail_pointer(skb);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001167
1168 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1169 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1170 nfmsg = NLMSG_DATA(nlh);
1171
1172 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1173 nfmsg->nfgen_family = exp->tuple.src.l3num;
1174 nfmsg->version = NFNETLINK_V0;
1175 nfmsg->res_id = 0;
1176
1177 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1178 goto nfattr_failure;
1179
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001180 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001181 return skb->len;
1182
1183nlmsg_failure:
1184nfattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001185 nlmsg_trim(skb, b);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001186 return -1;
1187}
1188
1189#ifdef CONFIG_NF_CONNTRACK_EVENTS
1190static int ctnetlink_expect_event(struct notifier_block *this,
1191 unsigned long events, void *ptr)
1192{
1193 struct nlmsghdr *nlh;
1194 struct nfgenmsg *nfmsg;
1195 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1196 struct sk_buff *skb;
1197 unsigned int type;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001198 sk_buff_data_t b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001199 int flags = 0;
1200
1201 if (events & IPEXP_NEW) {
1202 type = IPCTNL_MSG_EXP_NEW;
1203 flags = NLM_F_CREATE|NLM_F_EXCL;
1204 } else
1205 return NOTIFY_DONE;
1206
Pablo Neira Ayusob3a27bf2006-08-22 00:32:05 -07001207 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1208 return NOTIFY_DONE;
1209
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001210 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1211 if (!skb)
1212 return NOTIFY_DONE;
1213
1214 b = skb->tail;
1215
Marcus Sundbergb633ad52006-02-04 02:11:09 -08001216 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001217 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1218 nfmsg = NLMSG_DATA(nlh);
1219
1220 nlh->nlmsg_flags = flags;
1221 nfmsg->nfgen_family = exp->tuple.src.l3num;
1222 nfmsg->version = NFNETLINK_V0;
1223 nfmsg->res_id = 0;
1224
1225 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1226 goto nfattr_failure;
1227
1228 nlh->nlmsg_len = skb->tail - b;
1229 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1230 return NOTIFY_DONE;
1231
1232nlmsg_failure:
1233nfattr_failure:
1234 kfree_skb(skb);
1235 return NOTIFY_DONE;
1236}
1237#endif
Patrick McHardycf6994c2007-07-07 22:32:34 -07001238static int ctnetlink_exp_done(struct netlink_callback *cb)
1239{
Patrick McHardy31f15872007-07-07 22:35:21 -07001240 if (cb->args[1])
1241 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
Patrick McHardycf6994c2007-07-07 22:32:34 -07001242 return 0;
1243}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001244
1245static int
1246ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1247{
Patrick McHardycf6994c2007-07-07 22:32:34 -07001248 struct nf_conntrack_expect *exp, *last;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -08001249 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
Patrick McHardy31f15872007-07-07 22:35:21 -07001250 struct hlist_node *n;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -08001251 u_int8_t l3proto = nfmsg->nfgen_family;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001252
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001253 read_lock_bh(&nf_conntrack_lock);
Patrick McHardy31f15872007-07-07 22:35:21 -07001254 last = (struct nf_conntrack_expect *)cb->args[1];
1255 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
Patrick McHardycf6994c2007-07-07 22:32:34 -07001256restart:
Patrick McHardy31f15872007-07-07 22:35:21 -07001257 hlist_for_each_entry(exp, n, &nf_ct_expect_hash[cb->args[0]],
1258 hnode) {
1259 if (l3proto && exp->tuple.src.l3num != l3proto)
Patrick McHardycf6994c2007-07-07 22:32:34 -07001260 continue;
Patrick McHardy31f15872007-07-07 22:35:21 -07001261 if (cb->args[1]) {
1262 if (exp != last)
1263 continue;
1264 cb->args[1] = 0;
1265 }
1266 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1267 cb->nlh->nlmsg_seq,
1268 IPCTNL_MSG_EXP_NEW,
1269 1, exp) < 0) {
1270 atomic_inc(&exp->use);
1271 cb->args[1] = (unsigned long)exp;
1272 goto out;
1273 }
Patrick McHardycf6994c2007-07-07 22:32:34 -07001274 }
Patrick McHardy31f15872007-07-07 22:35:21 -07001275 if (cb->args[1]) {
1276 cb->args[1] = 0;
1277 goto restart;
Patrick McHardycf6994c2007-07-07 22:32:34 -07001278 }
1279 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001280out:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001281 read_unlock_bh(&nf_conntrack_lock);
Patrick McHardycf6994c2007-07-07 22:32:34 -07001282 if (last)
1283 nf_ct_expect_put(last);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001284
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001285 return skb->len;
1286}
1287
1288static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1289 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1290 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1291};
1292
1293static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001294ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001295 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001296{
1297 struct nf_conntrack_tuple tuple;
1298 struct nf_conntrack_expect *exp;
1299 struct sk_buff *skb2;
1300 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1301 u_int8_t u3 = nfmsg->nfgen_family;
1302 int err = 0;
1303
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001304 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1305 return -EINVAL;
1306
1307 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafc702e802007-03-22 23:30:55 -07001308 return netlink_dump_start(ctnl, skb, nlh,
1309 ctnetlink_exp_dump_table,
Patrick McHardycf6994c2007-07-07 22:32:34 -07001310 ctnetlink_exp_done);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001311 }
1312
1313 if (cda[CTA_EXPECT_MASTER-1])
1314 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1315 else
1316 return -EINVAL;
1317
1318 if (err < 0)
1319 return err;
1320
Patrick McHardy68236452007-07-07 22:30:49 -07001321 exp = nf_ct_expect_find_get(&tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001322 if (!exp)
1323 return -ENOENT;
1324
1325 if (cda[CTA_EXPECT_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -08001326 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001327 if (exp->id != ntohl(id)) {
Patrick McHardy68236452007-07-07 22:30:49 -07001328 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001329 return -ENOENT;
1330 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001331 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001332
1333 err = -ENOMEM;
1334 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1335 if (!skb2)
1336 goto out;
Thomas Graf4e9b8262006-11-27 09:25:58 -08001337
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001338 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001339 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1340 1, exp);
1341 if (err <= 0)
1342 goto free;
1343
Patrick McHardy68236452007-07-07 22:30:49 -07001344 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001345
1346 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1347
1348free:
1349 kfree_skb(skb2);
1350out:
Patrick McHardy68236452007-07-07 22:30:49 -07001351 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001352 return err;
1353}
1354
1355static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001356ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001357 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001358{
Patrick McHardy31f15872007-07-07 22:35:21 -07001359 struct nf_conntrack_expect *exp;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001360 struct nf_conntrack_tuple tuple;
1361 struct nf_conntrack_helper *h;
1362 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
Patrick McHardy31f15872007-07-07 22:35:21 -07001363 struct hlist_node *n, *next;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001364 u_int8_t u3 = nfmsg->nfgen_family;
Patrick McHardy31f15872007-07-07 22:35:21 -07001365 unsigned int i;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001366 int err;
1367
1368 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1369 return -EINVAL;
1370
1371 if (cda[CTA_EXPECT_TUPLE-1]) {
1372 /* delete a single expect by tuple */
1373 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1374 if (err < 0)
1375 return err;
1376
1377 /* bump usage count to 2 */
Patrick McHardy68236452007-07-07 22:30:49 -07001378 exp = nf_ct_expect_find_get(&tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001379 if (!exp)
1380 return -ENOENT;
1381
1382 if (cda[CTA_EXPECT_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -08001383 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001384 if (exp->id != ntohl(id)) {
Patrick McHardy68236452007-07-07 22:30:49 -07001385 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001386 return -ENOENT;
1387 }
1388 }
1389
1390 /* after list removal, usage count == 1 */
Patrick McHardy68236452007-07-07 22:30:49 -07001391 nf_ct_unexpect_related(exp);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001392 /* have to put what we 'get' above.
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001393 * after this line usage count == 0 */
Patrick McHardy68236452007-07-07 22:30:49 -07001394 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001395 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1396 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
Patrick McHardy31f15872007-07-07 22:35:21 -07001397 struct nf_conn_help *m_help;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001398
1399 /* delete all expectations for this helper */
1400 write_lock_bh(&nf_conntrack_lock);
1401 h = __nf_conntrack_helper_find_byname(name);
1402 if (!h) {
1403 write_unlock_bh(&nf_conntrack_lock);
1404 return -EINVAL;
1405 }
Patrick McHardy31f15872007-07-07 22:35:21 -07001406 for (i = 0; i < nf_ct_expect_hsize; i++) {
1407 hlist_for_each_entry_safe(exp, n, next,
1408 &nf_ct_expect_hash[i],
1409 hnode) {
1410 m_help = nfct_help(exp->master);
1411 if (m_help->helper == h
1412 && del_timer(&exp->timeout)) {
1413 nf_ct_unlink_expect(exp);
1414 nf_ct_expect_put(exp);
1415 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001416 }
1417 }
1418 write_unlock_bh(&nf_conntrack_lock);
1419 } else {
1420 /* This basically means we have to flush everything*/
1421 write_lock_bh(&nf_conntrack_lock);
Patrick McHardy31f15872007-07-07 22:35:21 -07001422 for (i = 0; i < nf_ct_expect_hsize; i++) {
1423 hlist_for_each_entry_safe(exp, n, next,
1424 &nf_ct_expect_hash[i],
1425 hnode) {
1426 if (del_timer(&exp->timeout)) {
1427 nf_ct_unlink_expect(exp);
1428 nf_ct_expect_put(exp);
1429 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001430 }
1431 }
1432 write_unlock_bh(&nf_conntrack_lock);
1433 }
1434
1435 return 0;
1436}
1437static int
1438ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1439{
1440 return -EOPNOTSUPP;
1441}
1442
1443static int
1444ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1445{
1446 struct nf_conntrack_tuple tuple, mask, master_tuple;
1447 struct nf_conntrack_tuple_hash *h = NULL;
1448 struct nf_conntrack_expect *exp;
1449 struct nf_conn *ct;
Harald Weltedc808fe2006-03-20 17:56:32 -08001450 struct nf_conn_help *help;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001451 int err = 0;
1452
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001453 /* caller guarantees that those three CTA_EXPECT_* exist */
1454 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1455 if (err < 0)
1456 return err;
1457 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1458 if (err < 0)
1459 return err;
1460 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1461 if (err < 0)
1462 return err;
1463
1464 /* Look for master conntrack of this expectation */
Patrick McHardy330f7db2007-07-07 22:28:42 -07001465 h = nf_conntrack_find_get(&master_tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001466 if (!h)
1467 return -ENOENT;
1468 ct = nf_ct_tuplehash_to_ctrack(h);
Harald Weltedc808fe2006-03-20 17:56:32 -08001469 help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001470
Harald Weltedc808fe2006-03-20 17:56:32 -08001471 if (!help || !help->helper) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001472 /* such conntrack hasn't got any helper, abort */
1473 err = -EINVAL;
1474 goto out;
1475 }
1476
Patrick McHardy68236452007-07-07 22:30:49 -07001477 exp = nf_ct_expect_alloc(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001478 if (!exp) {
1479 err = -ENOMEM;
1480 goto out;
1481 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001482
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001483 exp->expectfn = NULL;
1484 exp->flags = 0;
1485 exp->master = ct;
Patrick McHardy9457d852006-12-02 22:05:25 -08001486 exp->helper = NULL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001487 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
Patrick McHardyd4156e82007-07-07 22:31:32 -07001488 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1489 exp->mask.src.u.all = mask.src.u.all;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001490
Patrick McHardy68236452007-07-07 22:30:49 -07001491 err = nf_ct_expect_related(exp);
1492 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001493
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001494out:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001495 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1496 return err;
1497}
1498
1499static int
1500ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001501 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001502{
1503 struct nf_conntrack_tuple tuple;
1504 struct nf_conntrack_expect *exp;
1505 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1506 u_int8_t u3 = nfmsg->nfgen_family;
1507 int err = 0;
1508
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001509 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1510 return -EINVAL;
1511
1512 if (!cda[CTA_EXPECT_TUPLE-1]
1513 || !cda[CTA_EXPECT_MASK-1]
1514 || !cda[CTA_EXPECT_MASTER-1])
1515 return -EINVAL;
1516
1517 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1518 if (err < 0)
1519 return err;
1520
1521 write_lock_bh(&nf_conntrack_lock);
Patrick McHardy68236452007-07-07 22:30:49 -07001522 exp = __nf_ct_expect_find(&tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001523
1524 if (!exp) {
1525 write_unlock_bh(&nf_conntrack_lock);
1526 err = -ENOENT;
1527 if (nlh->nlmsg_flags & NLM_F_CREATE)
1528 err = ctnetlink_create_expect(cda, u3);
1529 return err;
1530 }
1531
1532 err = -EEXIST;
1533 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1534 err = ctnetlink_change_expect(exp, cda);
1535 write_unlock_bh(&nf_conntrack_lock);
1536
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001537 return err;
1538}
1539
1540#ifdef CONFIG_NF_CONNTRACK_EVENTS
1541static struct notifier_block ctnl_notifier = {
1542 .notifier_call = ctnetlink_conntrack_event,
1543};
1544
1545static struct notifier_block ctnl_notifier_exp = {
1546 .notifier_call = ctnetlink_expect_event,
1547};
1548#endif
1549
1550static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1551 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1552 .attr_count = CTA_MAX, },
1553 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1554 .attr_count = CTA_MAX, },
1555 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1556 .attr_count = CTA_MAX, },
1557 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1558 .attr_count = CTA_MAX, },
1559};
1560
1561static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1562 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1563 .attr_count = CTA_EXPECT_MAX, },
1564 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1565 .attr_count = CTA_EXPECT_MAX, },
1566 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1567 .attr_count = CTA_EXPECT_MAX, },
1568};
1569
1570static struct nfnetlink_subsystem ctnl_subsys = {
1571 .name = "conntrack",
1572 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1573 .cb_count = IPCTNL_MSG_MAX,
1574 .cb = ctnl_cb,
1575};
1576
1577static struct nfnetlink_subsystem ctnl_exp_subsys = {
1578 .name = "conntrack_expect",
1579 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1580 .cb_count = IPCTNL_MSG_EXP_MAX,
1581 .cb = ctnl_exp_cb,
1582};
1583
Patrick McHardyd2483dd2006-12-02 22:06:05 -08001584MODULE_ALIAS("ip_conntrack_netlink");
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001585MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
Pablo Neira Ayuso34f9a2e2006-02-04 02:11:41 -08001586MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001587
1588static int __init ctnetlink_init(void)
1589{
1590 int ret;
1591
1592 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1593 ret = nfnetlink_subsys_register(&ctnl_subsys);
1594 if (ret < 0) {
1595 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1596 goto err_out;
1597 }
1598
1599 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1600 if (ret < 0) {
1601 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1602 goto err_unreg_subsys;
1603 }
1604
1605#ifdef CONFIG_NF_CONNTRACK_EVENTS
1606 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1607 if (ret < 0) {
1608 printk("ctnetlink_init: cannot register notifier.\n");
1609 goto err_unreg_exp_subsys;
1610 }
1611
Patrick McHardy68236452007-07-07 22:30:49 -07001612 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001613 if (ret < 0) {
1614 printk("ctnetlink_init: cannot expect register notifier.\n");
1615 goto err_unreg_notifier;
1616 }
1617#endif
1618
1619 return 0;
1620
1621#ifdef CONFIG_NF_CONNTRACK_EVENTS
1622err_unreg_notifier:
1623 nf_conntrack_unregister_notifier(&ctnl_notifier);
1624err_unreg_exp_subsys:
1625 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1626#endif
1627err_unreg_subsys:
1628 nfnetlink_subsys_unregister(&ctnl_subsys);
1629err_out:
1630 return ret;
1631}
1632
1633static void __exit ctnetlink_exit(void)
1634{
1635 printk("ctnetlink: unregistering from nfnetlink.\n");
1636
1637#ifdef CONFIG_NF_CONNTRACK_EVENTS
Patrick McHardy68236452007-07-07 22:30:49 -07001638 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001639 nf_conntrack_unregister_notifier(&ctnl_notifier);
1640#endif
1641
1642 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1643 nfnetlink_subsys_unregister(&ctnl_subsys);
1644 return;
1645}
1646
1647module_init(ctnetlink_init);
1648module_exit(ctnetlink_exit);