blob: 735ea9c1a96fff2f64c5ba8291ec68c534c57dc2 [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 Ayuso0adf9d62008-06-09 15:56:20 -07007 * (C) 2005-2008 by Pablo Neira Ayuso <pablo@netfilter.org>
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>
Ingo Molnar711bbdd2008-05-17 08:26:25 +020021#include <linux/rculist.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080022#include <linux/types.h>
23#include <linux/timer.h>
24#include <linux/skbuff.h>
25#include <linux/errno.h>
26#include <linux/netlink.h>
27#include <linux/spinlock.h>
Yasuyuki Kozakai40a839f2006-06-27 03:00:35 -070028#include <linux/interrupt.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080029#include <linux/notifier.h>
30
31#include <linux/netfilter.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070032#include <net/netlink.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080033#include <net/netfilter/nf_conntrack.h>
34#include <net/netfilter/nf_conntrack_core.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010035#include <net/netfilter/nf_conntrack_expect.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080036#include <net/netfilter/nf_conntrack_helper.h>
37#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010038#include <net/netfilter/nf_conntrack_l4proto.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080039#include <net/netfilter/nf_conntrack_tuple.h>
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -070040#include <net/netfilter/nf_conntrack_acct.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080041#ifdef CONFIG_NF_NAT_NEEDED
42#include <net/netfilter/nf_nat_core.h>
43#include <net/netfilter/nf_nat_protocol.h>
44#endif
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080045
46#include <linux/netfilter/nfnetlink.h>
47#include <linux/netfilter/nfnetlink_conntrack.h>
48
49MODULE_LICENSE("GPL");
50
Harald Weltedc808fe2006-03-20 17:56:32 -080051static char __initdata version[] = "0.93";
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080052
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080053static inline int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080054ctnetlink_dump_tuples_proto(struct sk_buff *skb,
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080055 const struct nf_conntrack_tuple *tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +010056 struct nf_conntrack_l4proto *l4proto)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080057{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080058 int ret = 0;
Patrick McHardydf6fb862007-09-28 14:37:03 -070059 struct nlattr *nest_parms;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080060
Patrick McHardydf6fb862007-09-28 14:37:03 -070061 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
62 if (!nest_parms)
63 goto nla_put_failure;
Patrick McHardy77236b62007-12-17 22:29:45 -080064 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080065
Patrick McHardyfdf70832007-09-28 14:37:41 -070066 if (likely(l4proto->tuple_to_nlattr))
67 ret = l4proto->tuple_to_nlattr(skb, tuple);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080068
Patrick McHardydf6fb862007-09-28 14:37:03 -070069 nla_nest_end(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080070
71 return ret;
72
Patrick McHardydf6fb862007-09-28 14:37:03 -070073nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080074 return -1;
75}
76
77static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080078ctnetlink_dump_tuples_ip(struct sk_buff *skb,
79 const struct nf_conntrack_tuple *tuple,
80 struct nf_conntrack_l3proto *l3proto)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080081{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080082 int ret = 0;
Patrick McHardydf6fb862007-09-28 14:37:03 -070083 struct nlattr *nest_parms;
84
85 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
86 if (!nest_parms)
87 goto nla_put_failure;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080088
Patrick McHardyfdf70832007-09-28 14:37:41 -070089 if (likely(l3proto->tuple_to_nlattr))
90 ret = l3proto->tuple_to_nlattr(skb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080091
Patrick McHardydf6fb862007-09-28 14:37:03 -070092 nla_nest_end(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080093
94 return ret;
95
Patrick McHardydf6fb862007-09-28 14:37:03 -070096nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080097 return -1;
98}
99
Ilpo Järvinenbb5cf802008-01-05 23:11:31 -0800100static int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -0800101ctnetlink_dump_tuples(struct sk_buff *skb,
102 const struct nf_conntrack_tuple *tuple)
103{
104 int ret;
105 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100106 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -0800107
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +0100108 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -0800109 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -0800110
111 if (unlikely(ret < 0))
112 return ret;
113
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +0100114 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
Martin Josefsson605dcad2006-11-29 02:35:06 +0100115 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -0800116
117 return ret;
118}
119
120static inline int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800121ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
122{
Patrick McHardy77236b62007-12-17 22:29:45 -0800123 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800124 return 0;
125
Patrick McHardydf6fb862007-09-28 14:37:03 -0700126nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800127 return -1;
128}
129
130static inline int
131ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
132{
Patrick McHardy77236b62007-12-17 22:29:45 -0800133 long timeout = (ct->timeout.expires - jiffies) / HZ;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800134
Patrick McHardy77236b62007-12-17 22:29:45 -0800135 if (timeout < 0)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800136 timeout = 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800137
Patrick McHardy77236b62007-12-17 22:29:45 -0800138 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800139 return 0;
140
Patrick McHardydf6fb862007-09-28 14:37:03 -0700141nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800142 return -1;
143}
144
145static inline int
146ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
147{
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200148 struct nf_conntrack_l4proto *l4proto;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700149 struct nlattr *nest_proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800150 int ret;
151
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +0100152 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
153 if (!l4proto->to_nlattr)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800154 return 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800155
Patrick McHardydf6fb862007-09-28 14:37:03 -0700156 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
157 if (!nest_proto)
158 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800159
Patrick McHardyfdf70832007-09-28 14:37:41 -0700160 ret = l4proto->to_nlattr(skb, nest_proto, ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800161
Patrick McHardydf6fb862007-09-28 14:37:03 -0700162 nla_nest_end(skb, nest_proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800163
164 return ret;
165
Patrick McHardydf6fb862007-09-28 14:37:03 -0700166nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800167 return -1;
168}
169
170static inline int
171ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
172{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700173 struct nlattr *nest_helper;
Harald Weltedc808fe2006-03-20 17:56:32 -0800174 const struct nf_conn_help *help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700175 struct nf_conntrack_helper *helper;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800176
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700177 if (!help)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800178 return 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800179
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700180 helper = rcu_dereference(help->helper);
181 if (!helper)
182 goto out;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800183
Patrick McHardydf6fb862007-09-28 14:37:03 -0700184 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
185 if (!nest_helper)
186 goto nla_put_failure;
Patrick McHardy77236b62007-12-17 22:29:45 -0800187 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700188
Patrick McHardyfdf70832007-09-28 14:37:41 -0700189 if (helper->to_nlattr)
190 helper->to_nlattr(skb, ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800191
Patrick McHardydf6fb862007-09-28 14:37:03 -0700192 nla_nest_end(skb, nest_helper);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700193out:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800194 return 0;
195
Patrick McHardydf6fb862007-09-28 14:37:03 -0700196nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800197 return -1;
198}
199
Ilpo Järvinenbb5cf802008-01-05 23:11:31 -0800200static int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800201ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
202 enum ip_conntrack_dir dir)
203{
204 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700205 struct nlattr *nest_count;
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700206 const struct nf_conn_counter *acct;
207
208 acct = nf_conn_acct_find(ct);
209 if (!acct)
210 return 0;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800211
Patrick McHardydf6fb862007-09-28 14:37:03 -0700212 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
213 if (!nest_count)
214 goto nla_put_failure;
215
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700216 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
217 cpu_to_be64(acct[dir].packets));
218 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
219 cpu_to_be64(acct[dir].bytes));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800220
Patrick McHardydf6fb862007-09-28 14:37:03 -0700221 nla_nest_end(skb, nest_count);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800222
223 return 0;
224
Patrick McHardydf6fb862007-09-28 14:37:03 -0700225nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800226 return -1;
227}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800228
229#ifdef CONFIG_NF_CONNTRACK_MARK
230static inline int
231ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
232{
Patrick McHardy77236b62007-12-17 22:29:45 -0800233 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800234 return 0;
235
Patrick McHardydf6fb862007-09-28 14:37:03 -0700236nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800237 return -1;
238}
239#else
240#define ctnetlink_dump_mark(a, b) (0)
241#endif
242
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -0800243#ifdef CONFIG_NF_CONNTRACK_SECMARK
244static inline int
245ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
246{
Patrick McHardy77236b62007-12-17 22:29:45 -0800247 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -0800248 return 0;
249
250nla_put_failure:
251 return -1;
252}
253#else
254#define ctnetlink_dump_secmark(a, b) (0)
255#endif
256
Pablo Neira Ayuso0f417ce2007-12-17 22:28:19 -0800257#define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
258
259static inline int
260ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
261{
262 struct nlattr *nest_parms;
263
264 if (!(ct->status & IPS_EXPECTED))
265 return 0;
266
267 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
268 if (!nest_parms)
269 goto nla_put_failure;
270 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
271 goto nla_put_failure;
272 nla_nest_end(skb, nest_parms);
273
274 return 0;
275
276nla_put_failure:
277 return -1;
278}
279
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -0800280#ifdef CONFIG_NF_NAT_NEEDED
Ilpo Järvinenbb5cf802008-01-05 23:11:31 -0800281static int
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -0800282dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
283{
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -0800284 struct nlattr *nest_parms;
285
286 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
287 if (!nest_parms)
288 goto nla_put_failure;
289
Patrick McHardy77236b62007-12-17 22:29:45 -0800290 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
291 htonl(natseq->correction_pos));
292 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
293 htonl(natseq->offset_before));
294 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
295 htonl(natseq->offset_after));
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -0800296
297 nla_nest_end(skb, nest_parms);
298
299 return 0;
300
301nla_put_failure:
302 return -1;
303}
304
305static inline int
306ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
307{
308 struct nf_nat_seq *natseq;
309 struct nf_conn_nat *nat = nfct_nat(ct);
310
311 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
312 return 0;
313
314 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
315 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
316 return -1;
317
318 natseq = &nat->seq[IP_CT_DIR_REPLY];
319 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
320 return -1;
321
322 return 0;
323}
324#else
325#define ctnetlink_dump_nat_seq_adj(a, b) (0)
326#endif
327
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800328static inline int
329ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
330{
Patrick McHardy77236b62007-12-17 22:29:45 -0800331 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800332 return 0;
333
Patrick McHardydf6fb862007-09-28 14:37:03 -0700334nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800335 return -1;
336}
337
338static inline int
339ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
340{
Patrick McHardy77236b62007-12-17 22:29:45 -0800341 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800342 return 0;
343
Patrick McHardydf6fb862007-09-28 14:37:03 -0700344nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800345 return -1;
346}
347
348#define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
349
350static int
351ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800352 int event, int nowait,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800353 const struct nf_conn *ct)
354{
355 struct nlmsghdr *nlh;
356 struct nfgenmsg *nfmsg;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700357 struct nlattr *nest_parms;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700358 unsigned char *b = skb_tail_pointer(skb);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800359
360 event |= NFNL_SUBSYS_CTNETLINK << 8;
361 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
362 nfmsg = NLMSG_DATA(nlh);
363
364 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200365 nfmsg->nfgen_family = nf_ct_l3num(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800366 nfmsg->version = NFNETLINK_V0;
367 nfmsg->res_id = 0;
368
Patrick McHardydf6fb862007-09-28 14:37:03 -0700369 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
370 if (!nest_parms)
371 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800372 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700373 goto nla_put_failure;
374 nla_nest_end(skb, nest_parms);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800375
Patrick McHardydf6fb862007-09-28 14:37:03 -0700376 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
377 if (!nest_parms)
378 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800379 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700380 goto nla_put_failure;
381 nla_nest_end(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800382
383 if (ctnetlink_dump_status(skb, ct) < 0 ||
384 ctnetlink_dump_timeout(skb, ct) < 0 ||
385 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
386 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
387 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
388 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
389 ctnetlink_dump_mark(skb, ct) < 0 ||
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -0800390 ctnetlink_dump_secmark(skb, ct) < 0 ||
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800391 ctnetlink_dump_id(skb, ct) < 0 ||
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -0800392 ctnetlink_dump_use(skb, ct) < 0 ||
Pablo Neira Ayuso0f417ce2007-12-17 22:28:19 -0800393 ctnetlink_dump_master(skb, ct) < 0 ||
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -0800394 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700395 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800396
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700397 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800398 return skb->len;
399
400nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700401nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700402 nlmsg_trim(skb, b);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800403 return -1;
404}
405
406#ifdef CONFIG_NF_CONNTRACK_EVENTS
407static int ctnetlink_conntrack_event(struct notifier_block *this,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800408 unsigned long events, void *ptr)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800409{
410 struct nlmsghdr *nlh;
411 struct nfgenmsg *nfmsg;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700412 struct nlattr *nest_parms;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100413 struct nf_ct_event *item = (struct nf_ct_event *)ptr;
414 struct nf_conn *ct = item->ct;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800415 struct sk_buff *skb;
416 unsigned int type;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700417 sk_buff_data_t b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800418 unsigned int flags = 0, group;
419
420 /* ignore our fake conntrack entry */
421 if (ct == &nf_conntrack_untracked)
422 return NOTIFY_DONE;
423
424 if (events & IPCT_DESTROY) {
425 type = IPCTNL_MSG_CT_DELETE;
426 group = NFNLGRP_CONNTRACK_DESTROY;
427 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
428 type = IPCTNL_MSG_CT_NEW;
429 flags = NLM_F_CREATE|NLM_F_EXCL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800430 group = NFNLGRP_CONNTRACK_NEW;
Pablo Neira Ayuso1a315262006-08-22 00:32:23 -0700431 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800432 type = IPCTNL_MSG_CT_NEW;
433 group = NFNLGRP_CONNTRACK_UPDATE;
434 } else
435 return NOTIFY_DONE;
Patrick McHardya2427692006-03-20 18:03:59 -0800436
Pablo Neira Ayuso1f9da252009-02-09 14:34:26 -0800437 if (!item->report && !nfnetlink_has_listeners(group))
Patrick McHardya2427692006-03-20 18:03:59 -0800438 return NOTIFY_DONE;
439
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800440 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
441 if (!skb)
442 return NOTIFY_DONE;
443
444 b = skb->tail;
445
446 type |= NFNL_SUBSYS_CTNETLINK << 8;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100447 nlh = NLMSG_PUT(skb, item->pid, 0, type, sizeof(struct nfgenmsg));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800448 nfmsg = NLMSG_DATA(nlh);
449
450 nlh->nlmsg_flags = flags;
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200451 nfmsg->nfgen_family = nf_ct_l3num(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800452 nfmsg->version = NFNETLINK_V0;
453 nfmsg->res_id = 0;
454
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +0100455 rcu_read_lock();
Patrick McHardydf6fb862007-09-28 14:37:03 -0700456 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
457 if (!nest_parms)
458 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800459 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700460 goto nla_put_failure;
461 nla_nest_end(skb, nest_parms);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800462
Patrick McHardydf6fb862007-09-28 14:37:03 -0700463 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
464 if (!nest_parms)
465 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800466 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700467 goto nla_put_failure;
468 nla_nest_end(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800469
Eric Leblond1eedf692008-05-13 23:27:11 -0700470 if (ctnetlink_dump_id(skb, ct) < 0)
471 goto nla_put_failure;
472
Fabian Hugelshofere57dce62008-06-09 15:59:58 -0700473 if (ctnetlink_dump_status(skb, ct) < 0)
474 goto nla_put_failure;
475
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100476 if (events & IPCT_DESTROY) {
477 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
478 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700479 goto nla_put_failure;
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100480 } else {
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100481 if (ctnetlink_dump_timeout(skb, ct) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700482 goto nla_put_failure;
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100483
484 if (events & IPCT_PROTOINFO
485 && ctnetlink_dump_protoinfo(skb, ct) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700486 goto nla_put_failure;
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100487
488 if ((events & IPCT_HELPER || nfct_help(ct))
489 && ctnetlink_dump_helpinfo(skb, ct) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700490 goto nla_put_failure;
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100491
Pablo Neira Ayuso37fccd82007-12-17 22:28:41 -0800492#ifdef CONFIG_NF_CONNTRACK_SECMARK
493 if ((events & IPCT_SECMARK || ct->secmark)
494 && ctnetlink_dump_secmark(skb, ct) < 0)
495 goto nla_put_failure;
496#endif
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100497
Pablo Neira Ayuso0f417ce2007-12-17 22:28:19 -0800498 if (events & IPCT_RELATED &&
499 ctnetlink_dump_master(skb, ct) < 0)
500 goto nla_put_failure;
501
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -0800502 if (events & IPCT_NATSEQADJ &&
503 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
504 goto nla_put_failure;
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100505 }
Pablo Neira Ayusob9a37e02006-08-22 00:31:49 -0700506
Eric Leblonda83099a2008-01-31 04:44:27 -0800507#ifdef CONFIG_NF_CONNTRACK_MARK
508 if ((events & IPCT_MARK || ct->mark)
509 && ctnetlink_dump_mark(skb, ct) < 0)
510 goto nla_put_failure;
511#endif
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +0100512 rcu_read_unlock();
Eric Leblonda83099a2008-01-31 04:44:27 -0800513
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800514 nlh->nlmsg_len = skb->tail - b;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100515 nfnetlink_send(skb, item->pid, group, item->report);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800516 return NOTIFY_DONE;
517
Patrick McHardydf6fb862007-09-28 14:37:03 -0700518nla_put_failure:
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +0100519 rcu_read_unlock();
520nlmsg_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800521 kfree_skb(skb);
522 return NOTIFY_DONE;
523}
524#endif /* CONFIG_NF_CONNTRACK_EVENTS */
525
526static int ctnetlink_done(struct netlink_callback *cb)
527{
Patrick McHardy89f2e212006-05-29 18:24:58 -0700528 if (cb->args[1])
529 nf_ct_put((struct nf_conn *)cb->args[1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800530 return 0;
531}
532
533static int
534ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
535{
Patrick McHardy89f2e212006-05-29 18:24:58 -0700536 struct nf_conn *ct, *last;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800537 struct nf_conntrack_tuple_hash *h;
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700538 struct hlist_node *n;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800539 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
540 u_int8_t l3proto = nfmsg->nfgen_family;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800541
Patrick McHardy76507f62008-01-31 04:38:38 -0800542 rcu_read_lock();
Patrick McHardyd205dc42006-08-17 18:12:38 -0700543 last = (struct nf_conn *)cb->args[1];
Patrick McHardy89f2e212006-05-29 18:24:58 -0700544 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
545restart:
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200546 hlist_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]],
Patrick McHardy76507f62008-01-31 04:38:38 -0800547 hnode) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800548 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800549 continue;
550 ct = nf_ct_tuplehash_to_ctrack(h);
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800551 /* Dump entries of a given L3 protocol number.
552 * If it is not specified, ie. l3proto == 0,
553 * then dump everything. */
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200554 if (l3proto && nf_ct_l3num(ct) != l3proto)
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800555 continue;
Patrick McHardyd205dc42006-08-17 18:12:38 -0700556 if (cb->args[1]) {
557 if (ct != last)
Patrick McHardy89f2e212006-05-29 18:24:58 -0700558 continue;
Patrick McHardyd205dc42006-08-17 18:12:38 -0700559 cb->args[1] = 0;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700560 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800561 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800562 cb->nlh->nlmsg_seq,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800563 IPCTNL_MSG_CT_NEW,
Patrick McHardy89f2e212006-05-29 18:24:58 -0700564 1, ct) < 0) {
Patrick McHardy76507f62008-01-31 04:38:38 -0800565 if (!atomic_inc_not_zero(&ct->ct_general.use))
566 continue;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700567 cb->args[1] = (unsigned long)ct;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800568 goto out;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700569 }
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700570
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700571 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700572 IPCTNL_MSG_CT_GET_CTRZERO) {
573 struct nf_conn_counter *acct;
574
575 acct = nf_conn_acct_find(ct);
576 if (acct)
577 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
578 }
Patrick McHardy89f2e212006-05-29 18:24:58 -0700579 }
Patrick McHardyd205dc42006-08-17 18:12:38 -0700580 if (cb->args[1]) {
Patrick McHardy89f2e212006-05-29 18:24:58 -0700581 cb->args[1] = 0;
582 goto restart;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800583 }
584 }
Patrick McHardy89f2e212006-05-29 18:24:58 -0700585out:
Patrick McHardy76507f62008-01-31 04:38:38 -0800586 rcu_read_unlock();
Patrick McHardyd205dc42006-08-17 18:12:38 -0700587 if (last)
588 nf_ct_put(last);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800589
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800590 return skb->len;
591}
592
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800593static inline int
Patrick McHardydf6fb862007-09-28 14:37:03 -0700594ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800595{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700596 struct nlattr *tb[CTA_IP_MAX+1];
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800597 struct nf_conntrack_l3proto *l3proto;
598 int ret = 0;
599
Patrick McHardydf6fb862007-09-28 14:37:03 -0700600 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800601
Florian Westphalcd915662009-03-18 17:28:37 +0100602 rcu_read_lock();
603 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800604
Patrick McHardyf73e9242007-09-28 14:39:55 -0700605 if (likely(l3proto->nlattr_to_tuple)) {
606 ret = nla_validate_nested(attr, CTA_IP_MAX,
607 l3proto->nla_policy);
608 if (ret == 0)
609 ret = l3proto->nlattr_to_tuple(tb, tuple);
610 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800611
Florian Westphalcd915662009-03-18 17:28:37 +0100612 rcu_read_unlock();
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800613
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800614 return ret;
615}
616
Patrick McHardyf73e9242007-09-28 14:39:55 -0700617static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
618 [CTA_PROTO_NUM] = { .type = NLA_U8 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800619};
620
621static inline int
Patrick McHardydf6fb862007-09-28 14:37:03 -0700622ctnetlink_parse_tuple_proto(struct nlattr *attr,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800623 struct nf_conntrack_tuple *tuple)
624{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700625 struct nlattr *tb[CTA_PROTO_MAX+1];
Martin Josefsson605dcad2006-11-29 02:35:06 +0100626 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800627 int ret = 0;
628
Patrick McHardyf73e9242007-09-28 14:39:55 -0700629 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
630 if (ret < 0)
631 return ret;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800632
Patrick McHardydf6fb862007-09-28 14:37:03 -0700633 if (!tb[CTA_PROTO_NUM])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800634 return -EINVAL;
Patrick McHardy77236b62007-12-17 22:29:45 -0800635 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800636
Florian Westphalcd915662009-03-18 17:28:37 +0100637 rcu_read_lock();
638 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800639
Patrick McHardyf73e9242007-09-28 14:39:55 -0700640 if (likely(l4proto->nlattr_to_tuple)) {
641 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
642 l4proto->nla_policy);
643 if (ret == 0)
644 ret = l4proto->nlattr_to_tuple(tb, tuple);
645 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800646
Florian Westphalcd915662009-03-18 17:28:37 +0100647 rcu_read_unlock();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800648
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800649 return ret;
650}
651
Ilpo Järvinenbb5cf802008-01-05 23:11:31 -0800652static int
Patrick McHardydf6fb862007-09-28 14:37:03 -0700653ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800654 enum ctattr_tuple type, u_int8_t l3num)
655{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700656 struct nlattr *tb[CTA_TUPLE_MAX+1];
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800657 int err;
658
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800659 memset(tuple, 0, sizeof(*tuple));
660
Patrick McHardydf6fb862007-09-28 14:37:03 -0700661 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800662
Patrick McHardydf6fb862007-09-28 14:37:03 -0700663 if (!tb[CTA_TUPLE_IP])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800664 return -EINVAL;
665
666 tuple->src.l3num = l3num;
667
Patrick McHardydf6fb862007-09-28 14:37:03 -0700668 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800669 if (err < 0)
670 return err;
671
Patrick McHardydf6fb862007-09-28 14:37:03 -0700672 if (!tb[CTA_TUPLE_PROTO])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800673 return -EINVAL;
674
Patrick McHardydf6fb862007-09-28 14:37:03 -0700675 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800676 if (err < 0)
677 return err;
678
679 /* orig and expect tuples get DIR_ORIGINAL */
680 if (type == CTA_TUPLE_REPLY)
681 tuple->dst.dir = IP_CT_DIR_REPLY;
682 else
683 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
684
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800685 return 0;
686}
687
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800688static inline int
Patrick McHardydf6fb862007-09-28 14:37:03 -0700689ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800690{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700691 struct nlattr *tb[CTA_HELP_MAX+1];
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800692
Patrick McHardydf6fb862007-09-28 14:37:03 -0700693 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800694
Patrick McHardydf6fb862007-09-28 14:37:03 -0700695 if (!tb[CTA_HELP_NAME])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800696 return -EINVAL;
697
Patrick McHardydf6fb862007-09-28 14:37:03 -0700698 *helper_name = nla_data(tb[CTA_HELP_NAME]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800699
700 return 0;
701}
702
Patrick McHardyf73e9242007-09-28 14:39:55 -0700703static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
704 [CTA_STATUS] = { .type = NLA_U32 },
705 [CTA_TIMEOUT] = { .type = NLA_U32 },
706 [CTA_MARK] = { .type = NLA_U32 },
707 [CTA_USE] = { .type = NLA_U32 },
708 [CTA_ID] = { .type = NLA_U32 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800709};
710
711static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800712ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700713 struct nlmsghdr *nlh, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800714{
715 struct nf_conntrack_tuple_hash *h;
716 struct nf_conntrack_tuple tuple;
717 struct nf_conn *ct;
718 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
719 u_int8_t u3 = nfmsg->nfgen_family;
720 int err = 0;
721
Patrick McHardydf6fb862007-09-28 14:37:03 -0700722 if (cda[CTA_TUPLE_ORIG])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800723 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700724 else if (cda[CTA_TUPLE_REPLY])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800725 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
726 else {
727 /* Flush the whole table */
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100728 nf_conntrack_flush(&init_net,
729 NETLINK_CB(skb).pid,
730 nlmsg_report(nlh));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800731 return 0;
732 }
733
734 if (err < 0)
735 return err;
736
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200737 h = nf_conntrack_find_get(&init_net, &tuple);
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700738 if (!h)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800739 return -ENOENT;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800740
741 ct = nf_ct_tuplehash_to_ctrack(h);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800742
Patrick McHardydf6fb862007-09-28 14:37:03 -0700743 if (cda[CTA_ID]) {
Patrick McHardy77236b62007-12-17 22:29:45 -0800744 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
Patrick McHardy7f85f912007-09-28 14:41:27 -0700745 if (id != (u32)(unsigned long)ct) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800746 nf_ct_put(ct);
747 return -ENOENT;
748 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800749 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800750
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100751 nf_conntrack_event_report(IPCT_DESTROY,
752 ct,
753 NETLINK_CB(skb).pid,
754 nlmsg_report(nlh));
755
756 /* death_by_timeout would report the event again */
757 set_bit(IPS_DYING_BIT, &ct->status);
758
Patrick McHardy51091762008-06-09 15:59:06 -0700759 nf_ct_kill(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800760 nf_ct_put(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800761
762 return 0;
763}
764
765static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800766ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700767 struct nlmsghdr *nlh, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800768{
769 struct nf_conntrack_tuple_hash *h;
770 struct nf_conntrack_tuple tuple;
771 struct nf_conn *ct;
772 struct sk_buff *skb2 = NULL;
773 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
774 u_int8_t u3 = nfmsg->nfgen_family;
775 int err = 0;
776
Krzysztof Piotr Oledzki58401572008-07-21 10:01:34 -0700777 if (nlh->nlmsg_flags & NLM_F_DUMP)
Thomas Grafc702e802007-03-22 23:30:55 -0700778 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
779 ctnetlink_done);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800780
Patrick McHardydf6fb862007-09-28 14:37:03 -0700781 if (cda[CTA_TUPLE_ORIG])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800782 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700783 else if (cda[CTA_TUPLE_REPLY])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800784 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
785 else
786 return -EINVAL;
787
788 if (err < 0)
789 return err;
790
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200791 h = nf_conntrack_find_get(&init_net, &tuple);
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700792 if (!h)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800793 return -ENOENT;
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700794
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800795 ct = nf_ct_tuplehash_to_ctrack(h);
796
797 err = -ENOMEM;
798 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
799 if (!skb2) {
800 nf_ct_put(ct);
801 return -ENOMEM;
802 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800803
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +0100804 rcu_read_lock();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800805 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800806 IPCTNL_MSG_CT_NEW, 1, ct);
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +0100807 rcu_read_unlock();
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800808 nf_ct_put(ct);
809 if (err <= 0)
810 goto free;
811
812 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
813 if (err < 0)
814 goto out;
815
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800816 return 0;
817
818free:
819 kfree_skb(skb2);
820out:
821 return err;
822}
823
Pablo Neira Ayuso67671842008-10-20 03:34:27 -0700824#ifdef CONFIG_NF_NAT_NEEDED
Ilpo Järvinenbb5cf802008-01-05 23:11:31 -0800825static int
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700826ctnetlink_parse_nat_setup(struct nf_conn *ct,
827 enum nf_nat_manip_type manip,
828 struct nlattr *attr)
829{
830 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
831
832 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
833 if (!parse_nat_setup) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700834#ifdef CONFIG_MODULES
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700835 rcu_read_unlock();
Patrick McHardy748085f2009-01-21 12:19:49 -0800836 spin_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700837 nfnl_unlock();
838 if (request_module("nf-nat-ipv4") < 0) {
839 nfnl_lock();
Patrick McHardy748085f2009-01-21 12:19:49 -0800840 spin_lock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700841 rcu_read_lock();
842 return -EOPNOTSUPP;
843 }
844 nfnl_lock();
Patrick McHardy748085f2009-01-21 12:19:49 -0800845 spin_lock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700846 rcu_read_lock();
847 if (nfnetlink_parse_nat_setup_hook)
848 return -EAGAIN;
849#endif
850 return -EOPNOTSUPP;
851 }
852
853 return parse_nat_setup(ct, manip, attr);
854}
Pablo Neira Ayuso67671842008-10-20 03:34:27 -0700855#endif
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700856
857static int
Patrick McHardydf6fb862007-09-28 14:37:03 -0700858ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800859{
860 unsigned long d;
Patrick McHardy77236b62007-12-17 22:29:45 -0800861 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800862 d = ct->status ^ status;
863
864 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
865 /* unchangeable */
Pablo Neira Ayuso0adf9d62008-06-09 15:56:20 -0700866 return -EBUSY;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800867
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800868 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
869 /* SEEN_REPLY bit can only be set */
Pablo Neira Ayuso0adf9d62008-06-09 15:56:20 -0700870 return -EBUSY;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800871
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800872 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
873 /* ASSURED bit can only be set */
Pablo Neira Ayuso0adf9d62008-06-09 15:56:20 -0700874 return -EBUSY;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800875
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800876 /* Be careful here, modifying NAT bits can screw up things,
877 * so don't let users modify them directly if they don't pass
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800878 * nf_nat_range. */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800879 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
880 return 0;
881}
882
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700883static int
884ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
885{
886#ifdef CONFIG_NF_NAT_NEEDED
887 int ret;
888
889 if (cda[CTA_NAT_DST]) {
890 ret = ctnetlink_parse_nat_setup(ct,
891 IP_NAT_MANIP_DST,
892 cda[CTA_NAT_DST]);
893 if (ret < 0)
894 return ret;
895 }
896 if (cda[CTA_NAT_SRC]) {
897 ret = ctnetlink_parse_nat_setup(ct,
898 IP_NAT_MANIP_SRC,
899 cda[CTA_NAT_SRC]);
900 if (ret < 0)
901 return ret;
902 }
903 return 0;
904#else
905 return -EOPNOTSUPP;
906#endif
907}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800908
909static inline int
Patrick McHardydf6fb862007-09-28 14:37:03 -0700910ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800911{
912 struct nf_conntrack_helper *helper;
Harald Weltedc808fe2006-03-20 17:56:32 -0800913 struct nf_conn_help *help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800914 char *helpname;
915 int err;
916
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800917 /* don't change helper of sibling connections */
918 if (ct->master)
Pablo Neira Ayuso0adf9d62008-06-09 15:56:20 -0700919 return -EBUSY;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800920
Patrick McHardydf6fb862007-09-28 14:37:03 -0700921 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800922 if (err < 0)
923 return err;
924
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700925 if (!strcmp(helpname, "")) {
926 if (help && help->helper) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800927 /* we had a helper before ... */
928 nf_ct_remove_expectations(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700929 rcu_assign_pointer(help->helper, NULL);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800930 }
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700931
932 return 0;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800933 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800934
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700935 helper = __nf_conntrack_helper_find_byname(helpname);
Pablo Neira Ayuso226c0c02008-11-18 11:54:05 +0100936 if (helper == NULL) {
937#ifdef CONFIG_MODULES
938 spin_unlock_bh(&nf_conntrack_lock);
939
940 if (request_module("nfct-helper-%s", helpname) < 0) {
941 spin_lock_bh(&nf_conntrack_lock);
942 return -EOPNOTSUPP;
943 }
944
945 spin_lock_bh(&nf_conntrack_lock);
946 helper = __nf_conntrack_helper_find_byname(helpname);
947 if (helper)
948 return -EAGAIN;
949#endif
Pablo Neira Ayuso0adf9d62008-06-09 15:56:20 -0700950 return -EOPNOTSUPP;
Pablo Neira Ayuso226c0c02008-11-18 11:54:05 +0100951 }
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700952
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700953 if (help) {
954 if (help->helper == helper)
955 return 0;
956 if (help->helper)
957 return -EBUSY;
958 /* need to zero data of old helper */
959 memset(&help->help, 0, sizeof(help->help));
960 } else {
Pablo Neira Ayusofab00c52008-08-18 21:31:46 -0700961 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -0700962 if (help == NULL)
963 return -ENOMEM;
964 }
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700965
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700966 rcu_assign_pointer(help->helper, helper);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800967
968 return 0;
969}
970
971static inline int
Patrick McHardydf6fb862007-09-28 14:37:03 -0700972ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800973{
Patrick McHardy77236b62007-12-17 22:29:45 -0800974 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800975
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800976 if (!del_timer(&ct->timeout))
977 return -ETIME;
978
979 ct->timeout.expires = jiffies + timeout * HZ;
980 add_timer(&ct->timeout);
981
982 return 0;
983}
984
985static inline int
Patrick McHardydf6fb862007-09-28 14:37:03 -0700986ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800987{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700988 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
Martin Josefsson605dcad2006-11-29 02:35:06 +0100989 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800990 int err = 0;
991
Patrick McHardydf6fb862007-09-28 14:37:03 -0700992 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800993
Florian Westphalcd915662009-03-18 17:28:37 +0100994 rcu_read_lock();
995 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
Patrick McHardyfdf70832007-09-28 14:37:41 -0700996 if (l4proto->from_nlattr)
997 err = l4proto->from_nlattr(tb, ct);
Florian Westphalcd915662009-03-18 17:28:37 +0100998 rcu_read_unlock();
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800999
1000 return err;
1001}
1002
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -08001003#ifdef CONFIG_NF_NAT_NEEDED
1004static inline int
1005change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1006{
1007 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1008
1009 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1010
1011 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1012 return -EINVAL;
1013
1014 natseq->correction_pos =
Patrick McHardy77236b62007-12-17 22:29:45 -08001015 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -08001016
1017 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1018 return -EINVAL;
1019
1020 natseq->offset_before =
Patrick McHardy77236b62007-12-17 22:29:45 -08001021 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -08001022
1023 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1024 return -EINVAL;
1025
1026 natseq->offset_after =
Patrick McHardy77236b62007-12-17 22:29:45 -08001027 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -08001028
1029 return 0;
1030}
1031
1032static int
1033ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1034{
1035 int ret = 0;
1036 struct nf_conn_nat *nat = nfct_nat(ct);
1037
1038 if (!nat)
1039 return 0;
1040
1041 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1042 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1043 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1044 if (ret < 0)
1045 return ret;
1046
1047 ct->status |= IPS_SEQ_ADJUST;
1048 }
1049
1050 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1051 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1052 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1053 if (ret < 0)
1054 return ret;
1055
1056 ct->status |= IPS_SEQ_ADJUST;
1057 }
1058
1059 return 0;
1060}
1061#endif
1062
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001063static int
Patrick McHardydf6fb862007-09-28 14:37:03 -07001064ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001065{
1066 int err;
1067
Pablo Neira Ayusoe0983602009-03-16 15:27:22 +01001068 /* only allow NAT changes and master assignation for new conntracks */
1069 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1070 return -EOPNOTSUPP;
1071
Patrick McHardydf6fb862007-09-28 14:37:03 -07001072 if (cda[CTA_HELP]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001073 err = ctnetlink_change_helper(ct, cda);
1074 if (err < 0)
1075 return err;
1076 }
1077
Patrick McHardydf6fb862007-09-28 14:37:03 -07001078 if (cda[CTA_TIMEOUT]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001079 err = ctnetlink_change_timeout(ct, cda);
1080 if (err < 0)
1081 return err;
1082 }
1083
Patrick McHardydf6fb862007-09-28 14:37:03 -07001084 if (cda[CTA_STATUS]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001085 err = ctnetlink_change_status(ct, cda);
1086 if (err < 0)
1087 return err;
1088 }
1089
Patrick McHardydf6fb862007-09-28 14:37:03 -07001090 if (cda[CTA_PROTOINFO]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001091 err = ctnetlink_change_protoinfo(ct, cda);
1092 if (err < 0)
1093 return err;
1094 }
1095
Martin Josefssonbcd1e832006-04-01 02:23:21 -08001096#if defined(CONFIG_NF_CONNTRACK_MARK)
Patrick McHardydf6fb862007-09-28 14:37:03 -07001097 if (cda[CTA_MARK])
Patrick McHardy77236b62007-12-17 22:29:45 -08001098 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001099#endif
1100
Pablo Neira Ayuso13eae152007-12-17 22:28:00 -08001101#ifdef CONFIG_NF_NAT_NEEDED
1102 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1103 err = ctnetlink_change_nat_seq_adj(ct, cda);
1104 if (err < 0)
1105 return err;
1106 }
1107#endif
1108
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001109 return 0;
1110}
1111
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +01001112static inline void
1113ctnetlink_event_report(struct nf_conn *ct, u32 pid, int report)
1114{
1115 unsigned int events = 0;
1116
1117 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1118 events |= IPCT_RELATED;
1119 else
1120 events |= IPCT_NEW;
1121
1122 nf_conntrack_event_report(IPCT_STATUS |
1123 IPCT_HELPER |
1124 IPCT_REFRESH |
1125 IPCT_PROTOINFO |
1126 IPCT_NATSEQADJ |
1127 IPCT_MARK |
1128 events,
1129 ct,
1130 pid,
1131 report);
1132}
1133
Pablo Neira Ayusof0a3c082009-03-16 15:28:09 +01001134static struct nf_conn *
Patrick McHardydf6fb862007-09-28 14:37:03 -07001135ctnetlink_create_conntrack(struct nlattr *cda[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001136 struct nf_conntrack_tuple *otuple,
Pablo Neira Ayuso5faa1f42007-09-28 14:43:53 -07001137 struct nf_conntrack_tuple *rtuple,
Pablo Neira Ayuso7ec47492009-03-16 15:25:46 +01001138 u8 u3)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001139{
1140 struct nf_conn *ct;
1141 int err = -EINVAL;
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001142 struct nf_conntrack_helper *helper;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001143
Patrick McHardyb54ad402008-11-24 15:56:17 -08001144 ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC);
Julia Lawallcd7fcbf2009-01-12 00:06:08 +00001145 if (IS_ERR(ct))
Pablo Neira Ayusof0a3c082009-03-16 15:28:09 +01001146 return ERR_PTR(-ENOMEM);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001147
Patrick McHardydf6fb862007-09-28 14:37:03 -07001148 if (!cda[CTA_TIMEOUT])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001149 goto err;
Patrick McHardy77236b62007-12-17 22:29:45 -08001150 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001151
1152 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1153 ct->status |= IPS_CONFIRMED;
1154
Patrick McHardy58a3c9b2008-01-31 04:36:54 -08001155 rcu_read_lock();
Pablo Neira Ayuso226c0c02008-11-18 11:54:05 +01001156 if (cda[CTA_HELP]) {
1157 char *helpname;
1158
1159 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1160 if (err < 0) {
Patrick McHardy58a3c9b2008-01-31 04:36:54 -08001161 rcu_read_unlock();
Yasuyuki Kozakaiceceae12007-07-07 22:23:42 -07001162 goto err;
1163 }
Pablo Neira Ayuso226c0c02008-11-18 11:54:05 +01001164
1165 helper = __nf_conntrack_helper_find_byname(helpname);
1166 if (helper == NULL) {
1167 rcu_read_unlock();
1168#ifdef CONFIG_MODULES
1169 if (request_module("nfct-helper-%s", helpname) < 0) {
1170 err = -EOPNOTSUPP;
1171 goto err;
1172 }
1173
1174 rcu_read_lock();
1175 helper = __nf_conntrack_helper_find_byname(helpname);
1176 if (helper) {
1177 rcu_read_unlock();
1178 err = -EAGAIN;
1179 goto err;
1180 }
1181 rcu_read_unlock();
1182#endif
1183 err = -EOPNOTSUPP;
1184 goto err;
1185 } else {
1186 struct nf_conn_help *help;
1187
1188 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1189 if (help == NULL) {
1190 rcu_read_unlock();
1191 err = -ENOMEM;
1192 goto err;
1193 }
1194
1195 /* not in hash table yet so not strictly necessary */
1196 rcu_assign_pointer(help->helper, helper);
1197 }
1198 } else {
1199 /* try an implicit helper assignation */
1200 err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
1201 if (err < 0) {
1202 rcu_read_unlock();
1203 goto err;
1204 }
Patrick McHarrdy3c158f72007-06-05 12:55:27 -07001205 }
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -08001206
Pablo Neira Ayuso1575e7e2008-08-18 21:30:55 -07001207 if (cda[CTA_STATUS]) {
1208 err = ctnetlink_change_status(ct, cda);
1209 if (err < 0) {
1210 rcu_read_unlock();
1211 goto err;
1212 }
1213 }
1214
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -07001215 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1216 err = ctnetlink_change_nat(ct, cda);
1217 if (err < 0) {
1218 rcu_read_unlock();
1219 goto err;
1220 }
1221 }
1222
Pablo Neira Ayusoc969aa72009-02-09 14:33:57 -08001223#ifdef CONFIG_NF_NAT_NEEDED
1224 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1225 err = ctnetlink_change_nat_seq_adj(ct, cda);
1226 if (err < 0) {
1227 rcu_read_unlock();
1228 goto err;
1229 }
1230 }
1231#endif
1232
Pablo Neira Ayuso1575e7e2008-08-18 21:30:55 -07001233 if (cda[CTA_PROTOINFO]) {
1234 err = ctnetlink_change_protoinfo(ct, cda);
1235 if (err < 0) {
1236 rcu_read_unlock();
1237 goto err;
1238 }
1239 }
1240
Patrick McHardy3ec19252008-11-26 03:57:44 -08001241 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
Pablo Neira Ayuso1575e7e2008-08-18 21:30:55 -07001242
1243#if defined(CONFIG_NF_CONNTRACK_MARK)
1244 if (cda[CTA_MARK])
1245 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1246#endif
1247
Pablo Neira Ayuso5faa1f42007-09-28 14:43:53 -07001248 /* setup master conntrack: this is a confirmed expectation */
Pablo Neira Ayuso7ec47492009-03-16 15:25:46 +01001249 if (cda[CTA_TUPLE_MASTER]) {
1250 struct nf_conntrack_tuple master;
1251 struct nf_conntrack_tuple_hash *master_h;
1252 struct nf_conn *master_ct;
1253
1254 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1255 if (err < 0)
1256 goto err;
1257
1258 master_h = __nf_conntrack_find(&init_net, &master);
1259 if (master_h == NULL) {
1260 err = -ENOENT;
1261 goto err;
1262 }
1263 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1264 nf_conntrack_get(&master_ct->ct_general);
Pablo Neira Ayusof2a89002007-12-12 10:34:29 -08001265 __set_bit(IPS_EXPECTED_BIT, &ct->status);
Pablo Neira Ayuso5faa1f42007-09-28 14:43:53 -07001266 ct->master = master_ct;
Pablo Neira Ayusof2a89002007-12-12 10:34:29 -08001267 }
Pablo Neira Ayuso5faa1f42007-09-28 14:43:53 -07001268
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001269 add_timer(&ct->timeout);
1270 nf_conntrack_hash_insert(ct);
Patrick McHardy58a3c9b2008-01-31 04:36:54 -08001271 rcu_read_unlock();
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -08001272
Pablo Neira Ayusof0a3c082009-03-16 15:28:09 +01001273 return ct;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001274err:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001275 nf_conntrack_free(ct);
Pablo Neira Ayusof0a3c082009-03-16 15:28:09 +01001276 return ERR_PTR(err);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001277}
1278
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001279static int
1280ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -07001281 struct nlmsghdr *nlh, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001282{
1283 struct nf_conntrack_tuple otuple, rtuple;
1284 struct nf_conntrack_tuple_hash *h = NULL;
1285 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1286 u_int8_t u3 = nfmsg->nfgen_family;
1287 int err = 0;
1288
Patrick McHardydf6fb862007-09-28 14:37:03 -07001289 if (cda[CTA_TUPLE_ORIG]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001290 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1291 if (err < 0)
1292 return err;
1293 }
1294
Patrick McHardydf6fb862007-09-28 14:37:03 -07001295 if (cda[CTA_TUPLE_REPLY]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001296 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1297 if (err < 0)
1298 return err;
1299 }
1300
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001301 spin_lock_bh(&nf_conntrack_lock);
Patrick McHardydf6fb862007-09-28 14:37:03 -07001302 if (cda[CTA_TUPLE_ORIG])
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001303 h = __nf_conntrack_find(&init_net, &otuple);
Patrick McHardydf6fb862007-09-28 14:37:03 -07001304 else if (cda[CTA_TUPLE_REPLY])
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001305 h = __nf_conntrack_find(&init_net, &rtuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001306
1307 if (h == NULL) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001308 err = -ENOENT;
Pablo Neira Ayusof0a3c082009-03-16 15:28:09 +01001309 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1310 struct nf_conn *ct;
1311
1312 ct = ctnetlink_create_conntrack(cda, &otuple,
1313 &rtuple, u3);
1314 if (IS_ERR(ct)) {
1315 err = PTR_ERR(ct);
1316 goto out_unlock;
1317 }
1318 err = 0;
1319 nf_conntrack_get(&ct->ct_general);
1320 spin_unlock_bh(&nf_conntrack_lock);
1321 ctnetlink_event_report(ct,
1322 NETLINK_CB(skb).pid,
1323 nlmsg_report(nlh));
1324 nf_ct_put(ct);
1325 } else
1326 spin_unlock_bh(&nf_conntrack_lock);
1327
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001328 return err;
1329 }
1330 /* implicit 'else' */
1331
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001332 /* We manipulate the conntrack inside the global conntrack table lock,
1333 * so there's no need to increase the refcount */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001334 err = -EEXIST;
Pablo Neira Ayusoff4ca822007-08-07 18:11:26 -07001335 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +01001336 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1337
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +01001338 err = ctnetlink_change_conntrack(ct, cda);
1339 if (err == 0) {
1340 nf_conntrack_get(&ct->ct_general);
1341 spin_unlock_bh(&nf_conntrack_lock);
1342 ctnetlink_event_report(ct,
1343 NETLINK_CB(skb).pid,
1344 nlmsg_report(nlh));
1345 nf_ct_put(ct);
1346 } else
1347 spin_unlock_bh(&nf_conntrack_lock);
1348
1349 return err;
Pablo Neira Ayusoff4ca822007-08-07 18:11:26 -07001350 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001351
1352out_unlock:
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001353 spin_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001354 return err;
1355}
1356
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001357/***********************************************************************
1358 * EXPECT
1359 ***********************************************************************/
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001360
1361static inline int
1362ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1363 const struct nf_conntrack_tuple *tuple,
1364 enum ctattr_expect type)
1365{
Patrick McHardydf6fb862007-09-28 14:37:03 -07001366 struct nlattr *nest_parms;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001367
Patrick McHardydf6fb862007-09-28 14:37:03 -07001368 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1369 if (!nest_parms)
1370 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001371 if (ctnetlink_dump_tuples(skb, tuple) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -07001372 goto nla_put_failure;
1373 nla_nest_end(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001374
1375 return 0;
1376
Patrick McHardydf6fb862007-09-28 14:37:03 -07001377nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001378 return -1;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001379}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001380
1381static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001382ctnetlink_exp_dump_mask(struct sk_buff *skb,
1383 const struct nf_conntrack_tuple *tuple,
Patrick McHardyd4156e82007-07-07 22:31:32 -07001384 const struct nf_conntrack_tuple_mask *mask)
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001385{
1386 int ret;
1387 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001388 struct nf_conntrack_l4proto *l4proto;
Patrick McHardyd4156e82007-07-07 22:31:32 -07001389 struct nf_conntrack_tuple m;
Patrick McHardydf6fb862007-09-28 14:37:03 -07001390 struct nlattr *nest_parms;
Patrick McHardyd4156e82007-07-07 22:31:32 -07001391
1392 memset(&m, 0xFF, sizeof(m));
1393 m.src.u.all = mask->src.u.all;
1394 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1395
Patrick McHardydf6fb862007-09-28 14:37:03 -07001396 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1397 if (!nest_parms)
1398 goto nla_put_failure;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001399
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +01001400 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
Patrick McHardyd4156e82007-07-07 22:31:32 -07001401 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001402
1403 if (unlikely(ret < 0))
Patrick McHardydf6fb862007-09-28 14:37:03 -07001404 goto nla_put_failure;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001405
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +01001406 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
Patrick McHardyd4156e82007-07-07 22:31:32 -07001407 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001408 if (unlikely(ret < 0))
Patrick McHardydf6fb862007-09-28 14:37:03 -07001409 goto nla_put_failure;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001410
Patrick McHardydf6fb862007-09-28 14:37:03 -07001411 nla_nest_end(skb, nest_parms);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001412
1413 return 0;
1414
Patrick McHardydf6fb862007-09-28 14:37:03 -07001415nla_put_failure:
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001416 return -1;
1417}
1418
Ilpo Järvinenbb5cf802008-01-05 23:11:31 -08001419static int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001420ctnetlink_exp_dump_expect(struct sk_buff *skb,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001421 const struct nf_conntrack_expect *exp)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001422{
1423 struct nf_conn *master = exp->master;
Patrick McHardyd978e5d2007-12-17 22:37:03 -08001424 long timeout = (exp->timeout.expires - jiffies) / HZ;
1425
1426 if (timeout < 0)
1427 timeout = 0;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001428
1429 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -07001430 goto nla_put_failure;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001431 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -07001432 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001433 if (ctnetlink_exp_dump_tuple(skb,
1434 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1435 CTA_EXPECT_MASTER) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -07001436 goto nla_put_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001437
Patrick McHardyd978e5d2007-12-17 22:37:03 -08001438 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
Patrick McHardy77236b62007-12-17 22:29:45 -08001439 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001440
1441 return 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001442
Patrick McHardydf6fb862007-09-28 14:37:03 -07001443nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001444 return -1;
1445}
1446
1447static int
1448ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001449 int event,
1450 int nowait,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001451 const struct nf_conntrack_expect *exp)
1452{
1453 struct nlmsghdr *nlh;
1454 struct nfgenmsg *nfmsg;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001455 unsigned char *b = skb_tail_pointer(skb);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001456
1457 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1458 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1459 nfmsg = NLMSG_DATA(nlh);
1460
1461 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1462 nfmsg->nfgen_family = exp->tuple.src.l3num;
1463 nfmsg->version = NFNETLINK_V0;
1464 nfmsg->res_id = 0;
1465
1466 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -07001467 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001468
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001469 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001470 return skb->len;
1471
1472nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -07001473nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001474 nlmsg_trim(skb, b);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001475 return -1;
1476}
1477
1478#ifdef CONFIG_NF_CONNTRACK_EVENTS
1479static int ctnetlink_expect_event(struct notifier_block *this,
1480 unsigned long events, void *ptr)
1481{
1482 struct nlmsghdr *nlh;
1483 struct nfgenmsg *nfmsg;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +01001484 struct nf_exp_event *item = (struct nf_exp_event *)ptr;
1485 struct nf_conntrack_expect *exp = item->exp;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001486 struct sk_buff *skb;
1487 unsigned int type;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001488 sk_buff_data_t b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001489 int flags = 0;
1490
1491 if (events & IPEXP_NEW) {
1492 type = IPCTNL_MSG_EXP_NEW;
1493 flags = NLM_F_CREATE|NLM_F_EXCL;
1494 } else
1495 return NOTIFY_DONE;
1496
Pablo Neira Ayuso1f9da252009-02-09 14:34:26 -08001497 if (!item->report &&
1498 !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
Pablo Neira Ayusob3a27bf2006-08-22 00:32:05 -07001499 return NOTIFY_DONE;
1500
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001501 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1502 if (!skb)
1503 return NOTIFY_DONE;
1504
1505 b = skb->tail;
1506
Marcus Sundbergb633ad52006-02-04 02:11:09 -08001507 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +01001508 nlh = NLMSG_PUT(skb, item->pid, 0, type, sizeof(struct nfgenmsg));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001509 nfmsg = NLMSG_DATA(nlh);
1510
1511 nlh->nlmsg_flags = flags;
1512 nfmsg->nfgen_family = exp->tuple.src.l3num;
1513 nfmsg->version = NFNETLINK_V0;
1514 nfmsg->res_id = 0;
1515
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +01001516 rcu_read_lock();
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001517 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
Patrick McHardydf6fb862007-09-28 14:37:03 -07001518 goto nla_put_failure;
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +01001519 rcu_read_unlock();
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001520
1521 nlh->nlmsg_len = skb->tail - b;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +01001522 nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW, item->report);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001523 return NOTIFY_DONE;
1524
Patrick McHardydf6fb862007-09-28 14:37:03 -07001525nla_put_failure:
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +01001526 rcu_read_unlock();
1527nlmsg_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001528 kfree_skb(skb);
1529 return NOTIFY_DONE;
1530}
1531#endif
Patrick McHardycf6994c2007-07-07 22:32:34 -07001532static int ctnetlink_exp_done(struct netlink_callback *cb)
1533{
Patrick McHardy31f15872007-07-07 22:35:21 -07001534 if (cb->args[1])
1535 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
Patrick McHardycf6994c2007-07-07 22:32:34 -07001536 return 0;
1537}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001538
1539static int
1540ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1541{
Alexey Dobriyan9b03f382008-10-08 11:35:03 +02001542 struct net *net = &init_net;
Patrick McHardycf6994c2007-07-07 22:32:34 -07001543 struct nf_conntrack_expect *exp, *last;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -08001544 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
Patrick McHardy31f15872007-07-07 22:35:21 -07001545 struct hlist_node *n;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -08001546 u_int8_t l3proto = nfmsg->nfgen_family;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001547
Patrick McHardy7d0742d2008-01-31 04:38:19 -08001548 rcu_read_lock();
Patrick McHardy31f15872007-07-07 22:35:21 -07001549 last = (struct nf_conntrack_expect *)cb->args[1];
1550 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
Patrick McHardycf6994c2007-07-07 22:32:34 -07001551restart:
Alexey Dobriyan9b03f382008-10-08 11:35:03 +02001552 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
Patrick McHardy31f15872007-07-07 22:35:21 -07001553 hnode) {
1554 if (l3proto && exp->tuple.src.l3num != l3proto)
Patrick McHardycf6994c2007-07-07 22:32:34 -07001555 continue;
Patrick McHardy31f15872007-07-07 22:35:21 -07001556 if (cb->args[1]) {
1557 if (exp != last)
1558 continue;
1559 cb->args[1] = 0;
1560 }
1561 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1562 cb->nlh->nlmsg_seq,
1563 IPCTNL_MSG_EXP_NEW,
1564 1, exp) < 0) {
Patrick McHardy7d0742d2008-01-31 04:38:19 -08001565 if (!atomic_inc_not_zero(&exp->use))
1566 continue;
Patrick McHardy31f15872007-07-07 22:35:21 -07001567 cb->args[1] = (unsigned long)exp;
1568 goto out;
1569 }
Patrick McHardycf6994c2007-07-07 22:32:34 -07001570 }
Patrick McHardy31f15872007-07-07 22:35:21 -07001571 if (cb->args[1]) {
1572 cb->args[1] = 0;
1573 goto restart;
Patrick McHardycf6994c2007-07-07 22:32:34 -07001574 }
1575 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001576out:
Patrick McHardy7d0742d2008-01-31 04:38:19 -08001577 rcu_read_unlock();
Patrick McHardycf6994c2007-07-07 22:32:34 -07001578 if (last)
1579 nf_ct_expect_put(last);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001580
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001581 return skb->len;
1582}
1583
Patrick McHardyf73e9242007-09-28 14:39:55 -07001584static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1585 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1586 [CTA_EXPECT_ID] = { .type = NLA_U32 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001587};
1588
1589static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001590ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -07001591 struct nlmsghdr *nlh, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001592{
1593 struct nf_conntrack_tuple tuple;
1594 struct nf_conntrack_expect *exp;
1595 struct sk_buff *skb2;
1596 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1597 u_int8_t u3 = nfmsg->nfgen_family;
1598 int err = 0;
1599
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001600 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafc702e802007-03-22 23:30:55 -07001601 return netlink_dump_start(ctnl, skb, nlh,
1602 ctnetlink_exp_dump_table,
Patrick McHardycf6994c2007-07-07 22:32:34 -07001603 ctnetlink_exp_done);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001604 }
1605
Patrick McHardydf6fb862007-09-28 14:37:03 -07001606 if (cda[CTA_EXPECT_MASTER])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001607 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1608 else
1609 return -EINVAL;
1610
1611 if (err < 0)
1612 return err;
1613
Alexey Dobriyan9b03f382008-10-08 11:35:03 +02001614 exp = nf_ct_expect_find_get(&init_net, &tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001615 if (!exp)
1616 return -ENOENT;
1617
Patrick McHardydf6fb862007-09-28 14:37:03 -07001618 if (cda[CTA_EXPECT_ID]) {
Patrick McHardy77236b62007-12-17 22:29:45 -08001619 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
Patrick McHardy35832402007-09-28 14:41:50 -07001620 if (ntohl(id) != (u32)(unsigned long)exp) {
Patrick McHardy68236452007-07-07 22:30:49 -07001621 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001622 return -ENOENT;
1623 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001624 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001625
1626 err = -ENOMEM;
1627 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1628 if (!skb2)
1629 goto out;
Thomas Graf4e9b8262006-11-27 09:25:58 -08001630
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +01001631 rcu_read_lock();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001632 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001633 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1634 1, exp);
Pablo Neira Ayuso528a3a62008-11-17 16:00:40 +01001635 rcu_read_unlock();
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001636 if (err <= 0)
1637 goto free;
1638
Patrick McHardy68236452007-07-07 22:30:49 -07001639 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001640
1641 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1642
1643free:
1644 kfree_skb(skb2);
1645out:
Patrick McHardy68236452007-07-07 22:30:49 -07001646 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001647 return err;
1648}
1649
1650static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001651ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -07001652 struct nlmsghdr *nlh, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001653{
Patrick McHardy31f15872007-07-07 22:35:21 -07001654 struct nf_conntrack_expect *exp;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001655 struct nf_conntrack_tuple tuple;
1656 struct nf_conntrack_helper *h;
1657 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
Patrick McHardy31f15872007-07-07 22:35:21 -07001658 struct hlist_node *n, *next;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001659 u_int8_t u3 = nfmsg->nfgen_family;
Patrick McHardy31f15872007-07-07 22:35:21 -07001660 unsigned int i;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001661 int err;
1662
Patrick McHardydf6fb862007-09-28 14:37:03 -07001663 if (cda[CTA_EXPECT_TUPLE]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001664 /* delete a single expect by tuple */
1665 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1666 if (err < 0)
1667 return err;
1668
1669 /* bump usage count to 2 */
Alexey Dobriyan9b03f382008-10-08 11:35:03 +02001670 exp = nf_ct_expect_find_get(&init_net, &tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001671 if (!exp)
1672 return -ENOENT;
1673
Patrick McHardydf6fb862007-09-28 14:37:03 -07001674 if (cda[CTA_EXPECT_ID]) {
Patrick McHardy77236b62007-12-17 22:29:45 -08001675 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
Patrick McHardy35832402007-09-28 14:41:50 -07001676 if (ntohl(id) != (u32)(unsigned long)exp) {
Patrick McHardy68236452007-07-07 22:30:49 -07001677 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001678 return -ENOENT;
1679 }
1680 }
1681
1682 /* after list removal, usage count == 1 */
Patrick McHardy68236452007-07-07 22:30:49 -07001683 nf_ct_unexpect_related(exp);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001684 /* have to put what we 'get' above.
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001685 * after this line usage count == 0 */
Patrick McHardy68236452007-07-07 22:30:49 -07001686 nf_ct_expect_put(exp);
Patrick McHardydf6fb862007-09-28 14:37:03 -07001687 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1688 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
Patrick McHardy31f15872007-07-07 22:35:21 -07001689 struct nf_conn_help *m_help;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001690
1691 /* delete all expectations for this helper */
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001692 spin_lock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001693 h = __nf_conntrack_helper_find_byname(name);
1694 if (!h) {
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001695 spin_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayuso0adf9d62008-06-09 15:56:20 -07001696 return -EOPNOTSUPP;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001697 }
Patrick McHardy31f15872007-07-07 22:35:21 -07001698 for (i = 0; i < nf_ct_expect_hsize; i++) {
1699 hlist_for_each_entry_safe(exp, n, next,
Alexey Dobriyan9b03f382008-10-08 11:35:03 +02001700 &init_net.ct.expect_hash[i],
Patrick McHardy31f15872007-07-07 22:35:21 -07001701 hnode) {
1702 m_help = nfct_help(exp->master);
1703 if (m_help->helper == h
1704 && del_timer(&exp->timeout)) {
1705 nf_ct_unlink_expect(exp);
1706 nf_ct_expect_put(exp);
1707 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001708 }
1709 }
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001710 spin_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001711 } else {
1712 /* This basically means we have to flush everything*/
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001713 spin_lock_bh(&nf_conntrack_lock);
Patrick McHardy31f15872007-07-07 22:35:21 -07001714 for (i = 0; i < nf_ct_expect_hsize; i++) {
1715 hlist_for_each_entry_safe(exp, n, next,
Alexey Dobriyan9b03f382008-10-08 11:35:03 +02001716 &init_net.ct.expect_hash[i],
Patrick McHardy31f15872007-07-07 22:35:21 -07001717 hnode) {
1718 if (del_timer(&exp->timeout)) {
1719 nf_ct_unlink_expect(exp);
1720 nf_ct_expect_put(exp);
1721 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001722 }
1723 }
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001724 spin_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001725 }
1726
1727 return 0;
1728}
1729static int
Patrick McHardydf6fb862007-09-28 14:37:03 -07001730ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001731{
1732 return -EOPNOTSUPP;
1733}
1734
1735static int
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +01001736ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001737{
1738 struct nf_conntrack_tuple tuple, mask, master_tuple;
1739 struct nf_conntrack_tuple_hash *h = NULL;
1740 struct nf_conntrack_expect *exp;
1741 struct nf_conn *ct;
Harald Weltedc808fe2006-03-20 17:56:32 -08001742 struct nf_conn_help *help;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001743 int err = 0;
1744
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001745 /* caller guarantees that those three CTA_EXPECT_* exist */
1746 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1747 if (err < 0)
1748 return err;
1749 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1750 if (err < 0)
1751 return err;
1752 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1753 if (err < 0)
1754 return err;
1755
1756 /* Look for master conntrack of this expectation */
Alexey Dobriyan400dad32008-10-08 11:35:03 +02001757 h = nf_conntrack_find_get(&init_net, &master_tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001758 if (!h)
1759 return -ENOENT;
1760 ct = nf_ct_tuplehash_to_ctrack(h);
Harald Weltedc808fe2006-03-20 17:56:32 -08001761 help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001762
Harald Weltedc808fe2006-03-20 17:56:32 -08001763 if (!help || !help->helper) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001764 /* such conntrack hasn't got any helper, abort */
Pablo Neira Ayusobfe29672008-11-17 15:55:48 +01001765 err = -EOPNOTSUPP;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001766 goto out;
1767 }
1768
Patrick McHardy68236452007-07-07 22:30:49 -07001769 exp = nf_ct_expect_alloc(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001770 if (!exp) {
1771 err = -ENOMEM;
1772 goto out;
1773 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001774
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001775 exp->expectfn = NULL;
1776 exp->flags = 0;
1777 exp->master = ct;
Patrick McHardy9457d852006-12-02 22:05:25 -08001778 exp->helper = NULL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001779 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
Patrick McHardyd4156e82007-07-07 22:31:32 -07001780 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1781 exp->mask.src.u.all = mask.src.u.all;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001782
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +01001783 err = nf_ct_expect_related_report(exp, pid, report);
Patrick McHardy68236452007-07-07 22:30:49 -07001784 nf_ct_expect_put(exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001785
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001786out:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001787 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1788 return err;
1789}
1790
1791static int
1792ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -07001793 struct nlmsghdr *nlh, struct nlattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001794{
1795 struct nf_conntrack_tuple tuple;
1796 struct nf_conntrack_expect *exp;
1797 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1798 u_int8_t u3 = nfmsg->nfgen_family;
1799 int err = 0;
1800
Patrick McHardydf6fb862007-09-28 14:37:03 -07001801 if (!cda[CTA_EXPECT_TUPLE]
1802 || !cda[CTA_EXPECT_MASK]
1803 || !cda[CTA_EXPECT_MASTER])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001804 return -EINVAL;
1805
1806 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1807 if (err < 0)
1808 return err;
1809
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001810 spin_lock_bh(&nf_conntrack_lock);
Alexey Dobriyan9b03f382008-10-08 11:35:03 +02001811 exp = __nf_ct_expect_find(&init_net, &tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001812
1813 if (!exp) {
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001814 spin_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001815 err = -ENOENT;
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +01001816 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1817 err = ctnetlink_create_expect(cda,
1818 u3,
1819 NETLINK_CB(skb).pid,
1820 nlmsg_report(nlh));
1821 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001822 return err;
1823 }
1824
1825 err = -EEXIST;
1826 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1827 err = ctnetlink_change_expect(exp, cda);
Patrick McHardyf8ba1af2008-01-31 04:38:58 -08001828 spin_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001829
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001830 return err;
1831}
1832
1833#ifdef CONFIG_NF_CONNTRACK_EVENTS
1834static struct notifier_block ctnl_notifier = {
1835 .notifier_call = ctnetlink_conntrack_event,
1836};
1837
1838static struct notifier_block ctnl_notifier_exp = {
1839 .notifier_call = ctnetlink_expect_event,
1840};
1841#endif
1842
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -07001843static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001844 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
Patrick McHardyf73e9242007-09-28 14:39:55 -07001845 .attr_count = CTA_MAX,
1846 .policy = ct_nla_policy },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001847 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
Patrick McHardyf73e9242007-09-28 14:39:55 -07001848 .attr_count = CTA_MAX,
1849 .policy = ct_nla_policy },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001850 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
Patrick McHardyf73e9242007-09-28 14:39:55 -07001851 .attr_count = CTA_MAX,
1852 .policy = ct_nla_policy },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001853 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
Patrick McHardyf73e9242007-09-28 14:39:55 -07001854 .attr_count = CTA_MAX,
1855 .policy = ct_nla_policy },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001856};
1857
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -07001858static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001859 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
Patrick McHardyf73e9242007-09-28 14:39:55 -07001860 .attr_count = CTA_EXPECT_MAX,
1861 .policy = exp_nla_policy },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001862 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
Patrick McHardyf73e9242007-09-28 14:39:55 -07001863 .attr_count = CTA_EXPECT_MAX,
1864 .policy = exp_nla_policy },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001865 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
Patrick McHardyf73e9242007-09-28 14:39:55 -07001866 .attr_count = CTA_EXPECT_MAX,
1867 .policy = exp_nla_policy },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001868};
1869
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -07001870static const struct nfnetlink_subsystem ctnl_subsys = {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001871 .name = "conntrack",
1872 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1873 .cb_count = IPCTNL_MSG_MAX,
1874 .cb = ctnl_cb,
1875};
1876
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -07001877static const struct nfnetlink_subsystem ctnl_exp_subsys = {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001878 .name = "conntrack_expect",
1879 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1880 .cb_count = IPCTNL_MSG_EXP_MAX,
1881 .cb = ctnl_exp_cb,
1882};
1883
Patrick McHardyd2483dd2006-12-02 22:06:05 -08001884MODULE_ALIAS("ip_conntrack_netlink");
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001885MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
Pablo Neira Ayuso34f9a2e2006-02-04 02:11:41 -08001886MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001887
1888static int __init ctnetlink_init(void)
1889{
1890 int ret;
1891
1892 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1893 ret = nfnetlink_subsys_register(&ctnl_subsys);
1894 if (ret < 0) {
1895 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1896 goto err_out;
1897 }
1898
1899 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1900 if (ret < 0) {
1901 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1902 goto err_unreg_subsys;
1903 }
1904
1905#ifdef CONFIG_NF_CONNTRACK_EVENTS
1906 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1907 if (ret < 0) {
1908 printk("ctnetlink_init: cannot register notifier.\n");
1909 goto err_unreg_exp_subsys;
1910 }
1911
Patrick McHardy68236452007-07-07 22:30:49 -07001912 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001913 if (ret < 0) {
1914 printk("ctnetlink_init: cannot expect register notifier.\n");
1915 goto err_unreg_notifier;
1916 }
1917#endif
1918
1919 return 0;
1920
1921#ifdef CONFIG_NF_CONNTRACK_EVENTS
1922err_unreg_notifier:
1923 nf_conntrack_unregister_notifier(&ctnl_notifier);
1924err_unreg_exp_subsys:
1925 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1926#endif
1927err_unreg_subsys:
1928 nfnetlink_subsys_unregister(&ctnl_subsys);
1929err_out:
1930 return ret;
1931}
1932
1933static void __exit ctnetlink_exit(void)
1934{
1935 printk("ctnetlink: unregistering from nfnetlink.\n");
1936
1937#ifdef CONFIG_NF_CONNTRACK_EVENTS
Patrick McHardy68236452007-07-07 22:30:49 -07001938 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001939 nf_conntrack_unregister_notifier(&ctnl_notifier);
1940#endif
1941
1942 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1943 nfnetlink_subsys_unregister(&ctnl_subsys);
1944 return;
1945}
1946
1947module_init(ctnetlink_init);
1948module_exit(ctnetlink_exit);