]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/netfilter/nf_conntrack_netlink.c
iwlwifi: don't include iwl-dev.h from iwl-devtrace.h
[mirror_ubuntu-zesty-kernel.git] / net / netfilter / nf_conntrack_netlink.c
CommitLineData
c1d10adb
PNA
1/* 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>
dc808fe2 5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
c1d10adb 6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
0adf9d67 7 * (C) 2005-2008 by Pablo Neira Ayuso <pablo@netfilter.org>
c1d10adb 8 *
601e68e1 9 * Initial connection tracking via netlink development funded and
c1d10adb
PNA
10 * 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.
c1d10adb
PNA
16 */
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
711bbdd6 21#include <linux/rculist.h>
ea781f19 22#include <linux/rculist_nulls.h>
c1d10adb
PNA
23#include <linux/types.h>
24#include <linux/timer.h>
25#include <linux/skbuff.h>
26#include <linux/errno.h>
27#include <linux/netlink.h>
28#include <linux/spinlock.h>
40a839fd 29#include <linux/interrupt.h>
c1d10adb
PNA
30
31#include <linux/netfilter.h>
dc5fc579 32#include <net/netlink.h>
9592a5c0 33#include <net/sock.h>
c1d10adb
PNA
34#include <net/netfilter/nf_conntrack.h>
35#include <net/netfilter/nf_conntrack_core.h>
77ab9cff 36#include <net/netfilter/nf_conntrack_expect.h>
c1d10adb
PNA
37#include <net/netfilter/nf_conntrack_helper.h>
38#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 39#include <net/netfilter/nf_conntrack_l4proto.h>
5b1158e9 40#include <net/netfilter/nf_conntrack_tuple.h>
58401572 41#include <net/netfilter/nf_conntrack_acct.h>
ef00f89f 42#include <net/netfilter/nf_conntrack_zones.h>
5b1158e9
JK
43#ifdef CONFIG_NF_NAT_NEEDED
44#include <net/netfilter/nf_nat_core.h>
45#include <net/netfilter/nf_nat_protocol.h>
46#endif
c1d10adb
PNA
47
48#include <linux/netfilter/nfnetlink.h>
49#include <linux/netfilter/nfnetlink_conntrack.h>
50
51MODULE_LICENSE("GPL");
52
dc808fe2 53static char __initdata version[] = "0.93";
c1d10adb 54
c1d10adb 55static inline int
601e68e1 56ctnetlink_dump_tuples_proto(struct sk_buff *skb,
1cde6436 57 const struct nf_conntrack_tuple *tuple,
605dcad6 58 struct nf_conntrack_l4proto *l4proto)
c1d10adb 59{
c1d10adb 60 int ret = 0;
df6fb868 61 struct nlattr *nest_parms;
c1d10adb 62
df6fb868
PM
63 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
64 if (!nest_parms)
65 goto nla_put_failure;
77236b6e 66 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
c1d10adb 67
fdf70832
PM
68 if (likely(l4proto->tuple_to_nlattr))
69 ret = l4proto->tuple_to_nlattr(skb, tuple);
601e68e1 70
df6fb868 71 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
72
73 return ret;
74
df6fb868 75nla_put_failure:
c1d10adb
PNA
76 return -1;
77}
78
79static inline int
1cde6436
PNA
80ctnetlink_dump_tuples_ip(struct sk_buff *skb,
81 const struct nf_conntrack_tuple *tuple,
82 struct nf_conntrack_l3proto *l3proto)
c1d10adb 83{
c1d10adb 84 int ret = 0;
df6fb868
PM
85 struct nlattr *nest_parms;
86
87 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
88 if (!nest_parms)
89 goto nla_put_failure;
1cde6436 90
fdf70832
PM
91 if (likely(l3proto->tuple_to_nlattr))
92 ret = l3proto->tuple_to_nlattr(skb, tuple);
1cde6436 93
df6fb868 94 nla_nest_end(skb, nest_parms);
c1d10adb 95
1cde6436
PNA
96 return ret;
97
df6fb868 98nla_put_failure:
1cde6436
PNA
99 return -1;
100}
101
bb5cf80e 102static int
1cde6436
PNA
103ctnetlink_dump_tuples(struct sk_buff *skb,
104 const struct nf_conntrack_tuple *tuple)
105{
106 int ret;
107 struct nf_conntrack_l3proto *l3proto;
605dcad6 108 struct nf_conntrack_l4proto *l4proto;
1cde6436 109
528a3a6f 110 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1cde6436 111 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
c1d10adb
PNA
112
113 if (unlikely(ret < 0))
114 return ret;
115
528a3a6f 116 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
605dcad6 117 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
c1d10adb
PNA
118
119 return ret;
c1d10adb
PNA
120}
121
122static inline int
123ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
124{
77236b6e 125 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
c1d10adb
PNA
126 return 0;
127
df6fb868 128nla_put_failure:
c1d10adb
PNA
129 return -1;
130}
131
132static inline int
133ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
134{
77236b6e 135 long timeout = (ct->timeout.expires - jiffies) / HZ;
c1d10adb 136
77236b6e 137 if (timeout < 0)
c1d10adb 138 timeout = 0;
601e68e1 139
77236b6e 140 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
c1d10adb
PNA
141 return 0;
142
df6fb868 143nla_put_failure:
c1d10adb
PNA
144 return -1;
145}
146
147static inline int
440f0d58 148ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
c1d10adb 149{
5e8fbe2a 150 struct nf_conntrack_l4proto *l4proto;
df6fb868 151 struct nlattr *nest_proto;
c1d10adb
PNA
152 int ret;
153
528a3a6f
PNA
154 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
155 if (!l4proto->to_nlattr)
c1d10adb 156 return 0;
601e68e1 157
df6fb868
PM
158 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
159 if (!nest_proto)
160 goto nla_put_failure;
c1d10adb 161
fdf70832 162 ret = l4proto->to_nlattr(skb, nest_proto, ct);
c1d10adb 163
df6fb868 164 nla_nest_end(skb, nest_proto);
c1d10adb
PNA
165
166 return ret;
167
df6fb868 168nla_put_failure:
c1d10adb
PNA
169 return -1;
170}
171
172static inline int
173ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
174{
df6fb868 175 struct nlattr *nest_helper;
dc808fe2 176 const struct nf_conn_help *help = nfct_help(ct);
3c158f7f 177 struct nf_conntrack_helper *helper;
c1d10adb 178
3c158f7f 179 if (!help)
c1d10adb 180 return 0;
601e68e1 181
3c158f7f
PM
182 helper = rcu_dereference(help->helper);
183 if (!helper)
184 goto out;
185
df6fb868
PM
186 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
187 if (!nest_helper)
188 goto nla_put_failure;
77236b6e 189 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
c1d10adb 190
fdf70832
PM
191 if (helper->to_nlattr)
192 helper->to_nlattr(skb, ct);
c1d10adb 193
df6fb868 194 nla_nest_end(skb, nest_helper);
3c158f7f 195out:
c1d10adb
PNA
196 return 0;
197
df6fb868 198nla_put_failure:
c1d10adb
PNA
199 return -1;
200}
201
bb5cf80e 202static int
c1d10adb
PNA
203ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
204 enum ip_conntrack_dir dir)
205{
206 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
df6fb868 207 struct nlattr *nest_count;
58401572
KPO
208 const struct nf_conn_counter *acct;
209
210 acct = nf_conn_acct_find(ct);
211 if (!acct)
212 return 0;
c1d10adb 213
df6fb868
PM
214 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
215 if (!nest_count)
216 goto nla_put_failure;
217
58401572
KPO
218 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
219 cpu_to_be64(acct[dir].packets));
220 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
221 cpu_to_be64(acct[dir].bytes));
c1d10adb 222
df6fb868 223 nla_nest_end(skb, nest_count);
c1d10adb
PNA
224
225 return 0;
226
df6fb868 227nla_put_failure:
c1d10adb
PNA
228 return -1;
229}
c1d10adb
PNA
230
231#ifdef CONFIG_NF_CONNTRACK_MARK
232static inline int
233ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
234{
77236b6e 235 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
c1d10adb
PNA
236 return 0;
237
df6fb868 238nla_put_failure:
c1d10adb
PNA
239 return -1;
240}
241#else
242#define ctnetlink_dump_mark(a, b) (0)
243#endif
244
37fccd85
PNA
245#ifdef CONFIG_NF_CONNTRACK_SECMARK
246static inline int
247ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
248{
77236b6e 249 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
37fccd85
PNA
250 return 0;
251
252nla_put_failure:
253 return -1;
254}
255#else
256#define ctnetlink_dump_secmark(a, b) (0)
257#endif
258
0f417ce9
PNA
259#define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
260
261static inline int
262ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
263{
264 struct nlattr *nest_parms;
265
266 if (!(ct->status & IPS_EXPECTED))
267 return 0;
268
269 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
270 if (!nest_parms)
271 goto nla_put_failure;
272 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
273 goto nla_put_failure;
274 nla_nest_end(skb, nest_parms);
275
276 return 0;
277
278nla_put_failure:
279 return -1;
280}
281
13eae15a 282#ifdef CONFIG_NF_NAT_NEEDED
bb5cf80e 283static int
13eae15a
PNA
284dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
285{
13eae15a
PNA
286 struct nlattr *nest_parms;
287
288 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
289 if (!nest_parms)
290 goto nla_put_failure;
291
77236b6e
PM
292 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
293 htonl(natseq->correction_pos));
294 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
295 htonl(natseq->offset_before));
296 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
297 htonl(natseq->offset_after));
13eae15a
PNA
298
299 nla_nest_end(skb, nest_parms);
300
301 return 0;
302
303nla_put_failure:
304 return -1;
305}
306
307static inline int
308ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
309{
310 struct nf_nat_seq *natseq;
311 struct nf_conn_nat *nat = nfct_nat(ct);
312
313 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
314 return 0;
315
316 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
317 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
318 return -1;
319
320 natseq = &nat->seq[IP_CT_DIR_REPLY];
321 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
322 return -1;
323
324 return 0;
325}
326#else
327#define ctnetlink_dump_nat_seq_adj(a, b) (0)
328#endif
329
c1d10adb
PNA
330static inline int
331ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
332{
77236b6e 333 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
c1d10adb
PNA
334 return 0;
335
df6fb868 336nla_put_failure:
c1d10adb
PNA
337 return -1;
338}
339
340static inline int
341ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
342{
77236b6e 343 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
c1d10adb
PNA
344 return 0;
345
df6fb868 346nla_put_failure:
c1d10adb
PNA
347 return -1;
348}
349
c1d10adb
PNA
350static int
351ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
440f0d58 352 int event, struct nf_conn *ct)
c1d10adb
PNA
353{
354 struct nlmsghdr *nlh;
355 struct nfgenmsg *nfmsg;
df6fb868 356 struct nlattr *nest_parms;
96bcf938 357 unsigned int flags = pid ? NLM_F_MULTI : 0;
c1d10adb
PNA
358
359 event |= NFNL_SUBSYS_CTNETLINK << 8;
96bcf938
PNA
360 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
361 if (nlh == NULL)
362 goto nlmsg_failure;
c1d10adb 363
96bcf938 364 nfmsg = nlmsg_data(nlh);
5e8fbe2a 365 nfmsg->nfgen_family = nf_ct_l3num(ct);
c1d10adb
PNA
366 nfmsg->version = NFNETLINK_V0;
367 nfmsg->res_id = 0;
368
df6fb868
PM
369 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
370 if (!nest_parms)
371 goto nla_put_failure;
f2f3e38c 372 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
df6fb868
PM
373 goto nla_put_failure;
374 nla_nest_end(skb, nest_parms);
601e68e1 375
df6fb868
PM
376 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
377 if (!nest_parms)
378 goto nla_put_failure;
f2f3e38c 379 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
df6fb868
PM
380 goto nla_put_failure;
381 nla_nest_end(skb, nest_parms);
c1d10adb 382
ef00f89f
PM
383 if (nf_ct_zone(ct))
384 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
385
c1d10adb
PNA
386 if (ctnetlink_dump_status(skb, ct) < 0 ||
387 ctnetlink_dump_timeout(skb, ct) < 0 ||
388 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
389 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
390 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
391 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
392 ctnetlink_dump_mark(skb, ct) < 0 ||
37fccd85 393 ctnetlink_dump_secmark(skb, ct) < 0 ||
c1d10adb 394 ctnetlink_dump_id(skb, ct) < 0 ||
13eae15a 395 ctnetlink_dump_use(skb, ct) < 0 ||
0f417ce9 396 ctnetlink_dump_master(skb, ct) < 0 ||
13eae15a 397 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
df6fb868 398 goto nla_put_failure;
c1d10adb 399
96bcf938 400 nlmsg_end(skb, nlh);
c1d10adb
PNA
401 return skb->len;
402
403nlmsg_failure:
df6fb868 404nla_put_failure:
96bcf938 405 nlmsg_cancel(skb, nlh);
c1d10adb
PNA
406 return -1;
407}
408
409#ifdef CONFIG_NF_CONNTRACK_EVENTS
03b64f51
PNA
410static inline size_t
411ctnetlink_proto_size(const struct nf_conn *ct)
2732c4e4
HE
412{
413 struct nf_conntrack_l3proto *l3proto;
414 struct nf_conntrack_l4proto *l4proto;
03b64f51
PNA
415 size_t len = 0;
416
417 rcu_read_lock();
418 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
419 len += l3proto->nla_size;
420
421 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
422 len += l4proto->nla_size;
423 rcu_read_unlock();
424
425 return len;
426}
427
428static inline size_t
429ctnetlink_nlmsg_size(const struct nf_conn *ct)
430{
431 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
432 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
433 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
434 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
435 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
436 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
437 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
d271e8bd 438#ifdef CONFIG_NF_CT_ACCT
03b64f51
PNA
439 + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
440 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
441 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
d271e8bd 442#endif
03b64f51
PNA
443 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
444 + nla_total_size(0) /* CTA_PROTOINFO */
445 + nla_total_size(0) /* CTA_HELP */
446 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
d271e8bd 447#ifdef CONFIG_NF_CONNTRACK_SECMARK
03b64f51 448 + nla_total_size(sizeof(u_int32_t)) /* CTA_SECMARK */
d271e8bd
HE
449#endif
450#ifdef CONFIG_NF_NAT_NEEDED
03b64f51
PNA
451 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
452 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
d271e8bd
HE
453#endif
454#ifdef CONFIG_NF_CONNTRACK_MARK
03b64f51 455 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
d271e8bd 456#endif
03b64f51
PNA
457 + ctnetlink_proto_size(ct)
458 ;
2732c4e4
HE
459}
460
e34d5c1a
PNA
461static int
462ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
c1d10adb 463{
9592a5c0 464 struct net *net;
c1d10adb
PNA
465 struct nlmsghdr *nlh;
466 struct nfgenmsg *nfmsg;
df6fb868 467 struct nlattr *nest_parms;
19abb7b0 468 struct nf_conn *ct = item->ct;
c1d10adb
PNA
469 struct sk_buff *skb;
470 unsigned int type;
c1d10adb 471 unsigned int flags = 0, group;
dd7669a9 472 int err;
c1d10adb
PNA
473
474 /* ignore our fake conntrack entry */
475 if (ct == &nf_conntrack_untracked)
e34d5c1a 476 return 0;
c1d10adb 477
a0891aa6 478 if (events & (1 << IPCT_DESTROY)) {
c1d10adb
PNA
479 type = IPCTNL_MSG_CT_DELETE;
480 group = NFNLGRP_CONNTRACK_DESTROY;
a0891aa6 481 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
c1d10adb
PNA
482 type = IPCTNL_MSG_CT_NEW;
483 flags = NLM_F_CREATE|NLM_F_EXCL;
c1d10adb 484 group = NFNLGRP_CONNTRACK_NEW;
17e6e4ea 485 } else if (events) {
c1d10adb
PNA
486 type = IPCTNL_MSG_CT_NEW;
487 group = NFNLGRP_CONNTRACK_UPDATE;
488 } else
e34d5c1a 489 return 0;
a2427692 490
9592a5c0
AD
491 net = nf_ct_net(ct);
492 if (!item->report && !nfnetlink_has_listeners(net, group))
e34d5c1a 493 return 0;
a2427692 494
03b64f51
PNA
495 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
496 if (skb == NULL)
150ace0d 497 goto errout;
c1d10adb 498
c1d10adb 499 type |= NFNL_SUBSYS_CTNETLINK << 8;
96bcf938
PNA
500 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
501 if (nlh == NULL)
502 goto nlmsg_failure;
c1d10adb 503
96bcf938 504 nfmsg = nlmsg_data(nlh);
5e8fbe2a 505 nfmsg->nfgen_family = nf_ct_l3num(ct);
c1d10adb
PNA
506 nfmsg->version = NFNETLINK_V0;
507 nfmsg->res_id = 0;
508
528a3a6f 509 rcu_read_lock();
df6fb868
PM
510 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
511 if (!nest_parms)
512 goto nla_put_failure;
f2f3e38c 513 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
df6fb868
PM
514 goto nla_put_failure;
515 nla_nest_end(skb, nest_parms);
601e68e1 516
df6fb868
PM
517 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
518 if (!nest_parms)
519 goto nla_put_failure;
f2f3e38c 520 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
df6fb868
PM
521 goto nla_put_failure;
522 nla_nest_end(skb, nest_parms);
c1d10adb 523
ef00f89f
PM
524 if (nf_ct_zone(ct))
525 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
526
1eedf699
EL
527 if (ctnetlink_dump_id(skb, ct) < 0)
528 goto nla_put_failure;
529
e57dce60
FH
530 if (ctnetlink_dump_status(skb, ct) < 0)
531 goto nla_put_failure;
532
a0891aa6 533 if (events & (1 << IPCT_DESTROY)) {
7b621c1e
PNA
534 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
535 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
df6fb868 536 goto nla_put_failure;
7b621c1e 537 } else {
7b621c1e 538 if (ctnetlink_dump_timeout(skb, ct) < 0)
df6fb868 539 goto nla_put_failure;
7b621c1e 540
a0891aa6 541 if (events & (1 << IPCT_PROTOINFO)
7b621c1e 542 && ctnetlink_dump_protoinfo(skb, ct) < 0)
df6fb868 543 goto nla_put_failure;
7b621c1e 544
a0891aa6 545 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
7b621c1e 546 && ctnetlink_dump_helpinfo(skb, ct) < 0)
df6fb868 547 goto nla_put_failure;
7b621c1e 548
37fccd85 549#ifdef CONFIG_NF_CONNTRACK_SECMARK
a0891aa6 550 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
37fccd85
PNA
551 && ctnetlink_dump_secmark(skb, ct) < 0)
552 goto nla_put_failure;
553#endif
7b621c1e 554
a0891aa6 555 if (events & (1 << IPCT_RELATED) &&
0f417ce9
PNA
556 ctnetlink_dump_master(skb, ct) < 0)
557 goto nla_put_failure;
558
a0891aa6 559 if (events & (1 << IPCT_NATSEQADJ) &&
13eae15a
PNA
560 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
561 goto nla_put_failure;
7b621c1e 562 }
b9a37e0c 563
a83099a6 564#ifdef CONFIG_NF_CONNTRACK_MARK
a0891aa6 565 if ((events & (1 << IPCT_MARK) || ct->mark)
a83099a6
EL
566 && ctnetlink_dump_mark(skb, ct) < 0)
567 goto nla_put_failure;
568#endif
528a3a6f 569 rcu_read_unlock();
a83099a6 570
96bcf938 571 nlmsg_end(skb, nlh);
9592a5c0 572 err = nfnetlink_send(skb, net, item->pid, group, item->report,
cd8c20b6 573 GFP_ATOMIC);
dd7669a9
PNA
574 if (err == -ENOBUFS || err == -EAGAIN)
575 return -ENOBUFS;
576
e34d5c1a 577 return 0;
c1d10adb 578
df6fb868 579nla_put_failure:
528a3a6f 580 rcu_read_unlock();
96bcf938 581 nlmsg_cancel(skb, nlh);
528a3a6f 582nlmsg_failure:
c1d10adb 583 kfree_skb(skb);
150ace0d 584errout:
37b7ef72
PNA
585 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
586 return -ENOBUFS;
587
e34d5c1a 588 return 0;
c1d10adb
PNA
589}
590#endif /* CONFIG_NF_CONNTRACK_EVENTS */
591
592static int ctnetlink_done(struct netlink_callback *cb)
593{
89f2e218
PM
594 if (cb->args[1])
595 nf_ct_put((struct nf_conn *)cb->args[1]);
c1d10adb
PNA
596 return 0;
597}
598
599static int
600ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
601{
9592a5c0 602 struct net *net = sock_net(skb->sk);
89f2e218 603 struct nf_conn *ct, *last;
c1d10adb 604 struct nf_conntrack_tuple_hash *h;
ea781f19 605 struct hlist_nulls_node *n;
96bcf938 606 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
87711cb8 607 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 608
76507f69 609 rcu_read_lock();
d205dc40 610 last = (struct nf_conn *)cb->args[1];
9ab99d5a 611 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
89f2e218 612restart:
9592a5c0 613 hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[cb->args[0]],
ea781f19 614 hnnode) {
5b1158e9 615 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
c1d10adb
PNA
616 continue;
617 ct = nf_ct_tuplehash_to_ctrack(h);
ea781f19
ED
618 if (!atomic_inc_not_zero(&ct->ct_general.use))
619 continue;
87711cb8
PNA
620 /* Dump entries of a given L3 protocol number.
621 * If it is not specified, ie. l3proto == 0,
622 * then dump everything. */
5e8fbe2a 623 if (l3proto && nf_ct_l3num(ct) != l3proto)
ea781f19 624 goto releasect;
d205dc40
PM
625 if (cb->args[1]) {
626 if (ct != last)
ea781f19 627 goto releasect;
d205dc40 628 cb->args[1] = 0;
89f2e218 629 }
c1d10adb 630 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
601e68e1 631 cb->nlh->nlmsg_seq,
8b0a231d 632 IPCTNL_MSG_CT_NEW, ct) < 0) {
89f2e218 633 cb->args[1] = (unsigned long)ct;
c1d10adb 634 goto out;
89f2e218 635 }
58401572 636
01f34848 637 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
58401572
KPO
638 IPCTNL_MSG_CT_GET_CTRZERO) {
639 struct nf_conn_counter *acct;
640
641 acct = nf_conn_acct_find(ct);
642 if (acct)
643 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
644 }
ea781f19
ED
645releasect:
646 nf_ct_put(ct);
89f2e218 647 }
d205dc40 648 if (cb->args[1]) {
89f2e218
PM
649 cb->args[1] = 0;
650 goto restart;
c1d10adb
PNA
651 }
652 }
89f2e218 653out:
76507f69 654 rcu_read_unlock();
d205dc40
PM
655 if (last)
656 nf_ct_put(last);
c1d10adb 657
c1d10adb
PNA
658 return skb->len;
659}
660
c1d10adb 661static inline int
df6fb868 662ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
c1d10adb 663{
df6fb868 664 struct nlattr *tb[CTA_IP_MAX+1];
c1d10adb
PNA
665 struct nf_conntrack_l3proto *l3proto;
666 int ret = 0;
667
df6fb868 668 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
c1d10adb 669
cd91566e
FW
670 rcu_read_lock();
671 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
c1d10adb 672
f73e924c
PM
673 if (likely(l3proto->nlattr_to_tuple)) {
674 ret = nla_validate_nested(attr, CTA_IP_MAX,
675 l3proto->nla_policy);
676 if (ret == 0)
677 ret = l3proto->nlattr_to_tuple(tb, tuple);
678 }
c1d10adb 679
cd91566e 680 rcu_read_unlock();
c1d10adb 681
c1d10adb
PNA
682 return ret;
683}
684
f73e924c
PM
685static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
686 [CTA_PROTO_NUM] = { .type = NLA_U8 },
c1d10adb
PNA
687};
688
689static inline int
df6fb868 690ctnetlink_parse_tuple_proto(struct nlattr *attr,
c1d10adb
PNA
691 struct nf_conntrack_tuple *tuple)
692{
df6fb868 693 struct nlattr *tb[CTA_PROTO_MAX+1];
605dcad6 694 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
695 int ret = 0;
696
f73e924c
PM
697 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
698 if (ret < 0)
699 return ret;
c1d10adb 700
df6fb868 701 if (!tb[CTA_PROTO_NUM])
c1d10adb 702 return -EINVAL;
77236b6e 703 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
c1d10adb 704
cd91566e
FW
705 rcu_read_lock();
706 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
c1d10adb 707
f73e924c
PM
708 if (likely(l4proto->nlattr_to_tuple)) {
709 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
710 l4proto->nla_policy);
711 if (ret == 0)
712 ret = l4proto->nlattr_to_tuple(tb, tuple);
713 }
c1d10adb 714
cd91566e 715 rcu_read_unlock();
601e68e1 716
c1d10adb
PNA
717 return ret;
718}
719
d0b0268f
PM
720static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
721 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
722 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
723};
724
bb5cf80e 725static int
39938324
PM
726ctnetlink_parse_tuple(const struct nlattr * const cda[],
727 struct nf_conntrack_tuple *tuple,
c1d10adb
PNA
728 enum ctattr_tuple type, u_int8_t l3num)
729{
df6fb868 730 struct nlattr *tb[CTA_TUPLE_MAX+1];
c1d10adb
PNA
731 int err;
732
c1d10adb
PNA
733 memset(tuple, 0, sizeof(*tuple));
734
d0b0268f 735 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
c1d10adb 736
df6fb868 737 if (!tb[CTA_TUPLE_IP])
c1d10adb
PNA
738 return -EINVAL;
739
740 tuple->src.l3num = l3num;
741
df6fb868 742 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
c1d10adb
PNA
743 if (err < 0)
744 return err;
745
df6fb868 746 if (!tb[CTA_TUPLE_PROTO])
c1d10adb
PNA
747 return -EINVAL;
748
df6fb868 749 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
c1d10adb
PNA
750 if (err < 0)
751 return err;
752
753 /* orig and expect tuples get DIR_ORIGINAL */
754 if (type == CTA_TUPLE_REPLY)
755 tuple->dst.dir = IP_CT_DIR_REPLY;
756 else
757 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
758
c1d10adb
PNA
759 return 0;
760}
761
ef00f89f
PM
762static int
763ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
764{
765 if (attr)
766#ifdef CONFIG_NF_CONNTRACK_ZONES
767 *zone = ntohs(nla_get_be16(attr));
768#else
769 return -EOPNOTSUPP;
770#endif
771 else
772 *zone = 0;
773
774 return 0;
775}
776
d0b0268f
PM
777static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
778 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING },
779};
780
c1d10adb 781static inline int
39938324 782ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
c1d10adb 783{
df6fb868 784 struct nlattr *tb[CTA_HELP_MAX+1];
c1d10adb 785
d0b0268f 786 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
c1d10adb 787
df6fb868 788 if (!tb[CTA_HELP_NAME])
c1d10adb
PNA
789 return -EINVAL;
790
df6fb868 791 *helper_name = nla_data(tb[CTA_HELP_NAME]);
c1d10adb
PNA
792
793 return 0;
794}
795
f73e924c 796static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
d0b0268f
PM
797 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
798 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
f73e924c 799 [CTA_STATUS] = { .type = NLA_U32 },
d0b0268f
PM
800 [CTA_PROTOINFO] = { .type = NLA_NESTED },
801 [CTA_HELP] = { .type = NLA_NESTED },
802 [CTA_NAT_SRC] = { .type = NLA_NESTED },
f73e924c
PM
803 [CTA_TIMEOUT] = { .type = NLA_U32 },
804 [CTA_MARK] = { .type = NLA_U32 },
f73e924c 805 [CTA_ID] = { .type = NLA_U32 },
d0b0268f
PM
806 [CTA_NAT_DST] = { .type = NLA_NESTED },
807 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
ef00f89f 808 [CTA_ZONE] = { .type = NLA_U16 },
c1d10adb
PNA
809};
810
811static int
601e68e1 812ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
813 const struct nlmsghdr *nlh,
814 const struct nlattr * const cda[])
c1d10adb 815{
9592a5c0 816 struct net *net = sock_net(ctnl);
c1d10adb
PNA
817 struct nf_conntrack_tuple_hash *h;
818 struct nf_conntrack_tuple tuple;
819 struct nf_conn *ct;
96bcf938 820 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 821 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
822 u16 zone;
823 int err;
824
825 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
826 if (err < 0)
827 return err;
c1d10adb 828
df6fb868 829 if (cda[CTA_TUPLE_ORIG])
c1d10adb 830 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
df6fb868 831 else if (cda[CTA_TUPLE_REPLY])
c1d10adb
PNA
832 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
833 else {
834 /* Flush the whole table */
9592a5c0 835 nf_conntrack_flush_report(net,
274d383b
PNA
836 NETLINK_CB(skb).pid,
837 nlmsg_report(nlh));
c1d10adb
PNA
838 return 0;
839 }
840
841 if (err < 0)
842 return err;
843
ef00f89f 844 h = nf_conntrack_find_get(net, zone, &tuple);
9ea8cfd6 845 if (!h)
c1d10adb 846 return -ENOENT;
c1d10adb
PNA
847
848 ct = nf_ct_tuplehash_to_ctrack(h);
601e68e1 849
df6fb868 850 if (cda[CTA_ID]) {
77236b6e 851 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
7f85f914 852 if (id != (u32)(unsigned long)ct) {
c1d10adb
PNA
853 nf_ct_put(ct);
854 return -ENOENT;
855 }
601e68e1 856 }
c1d10adb 857
dd7669a9
PNA
858 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
859 NETLINK_CB(skb).pid,
860 nlmsg_report(nlh)) < 0) {
861 nf_ct_delete_from_lists(ct);
862 /* we failed to report the event, try later */
863 nf_ct_insert_dying_list(ct);
864 nf_ct_put(ct);
865 return 0;
866 }
19abb7b0
PNA
867
868 /* death_by_timeout would report the event again */
869 set_bit(IPS_DYING_BIT, &ct->status);
870
51091764 871 nf_ct_kill(ct);
c1d10adb 872 nf_ct_put(ct);
c1d10adb
PNA
873
874 return 0;
875}
876
877static int
601e68e1 878ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
879 const struct nlmsghdr *nlh,
880 const struct nlattr * const cda[])
c1d10adb 881{
9592a5c0 882 struct net *net = sock_net(ctnl);
c1d10adb
PNA
883 struct nf_conntrack_tuple_hash *h;
884 struct nf_conntrack_tuple tuple;
885 struct nf_conn *ct;
886 struct sk_buff *skb2 = NULL;
96bcf938 887 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 888 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
889 u16 zone;
890 int err;
c1d10adb 891
58401572 892 if (nlh->nlmsg_flags & NLM_F_DUMP)
c702e804
TG
893 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
894 ctnetlink_done);
c1d10adb 895
ef00f89f
PM
896 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
897 if (err < 0)
898 return err;
899
df6fb868 900 if (cda[CTA_TUPLE_ORIG])
c1d10adb 901 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
df6fb868 902 else if (cda[CTA_TUPLE_REPLY])
c1d10adb
PNA
903 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
904 else
905 return -EINVAL;
906
907 if (err < 0)
908 return err;
909
ef00f89f 910 h = nf_conntrack_find_get(net, zone, &tuple);
9ea8cfd6 911 if (!h)
c1d10adb 912 return -ENOENT;
9ea8cfd6 913
c1d10adb
PNA
914 ct = nf_ct_tuplehash_to_ctrack(h);
915
916 err = -ENOMEM;
96bcf938
PNA
917 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
918 if (skb2 == NULL) {
c1d10adb
PNA
919 nf_ct_put(ct);
920 return -ENOMEM;
921 }
c1d10adb 922
528a3a6f 923 rcu_read_lock();
601e68e1 924 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
8b0a231d 925 IPCTNL_MSG_CT_NEW, ct);
528a3a6f 926 rcu_read_unlock();
c1d10adb
PNA
927 nf_ct_put(ct);
928 if (err <= 0)
929 goto free;
930
931 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
932 if (err < 0)
933 goto out;
934
c1d10adb
PNA
935 return 0;
936
937free:
938 kfree_skb(skb2);
939out:
940 return err;
941}
942
67671841 943#ifdef CONFIG_NF_NAT_NEEDED
e6a7d3c0
PNA
944static int
945ctnetlink_parse_nat_setup(struct nf_conn *ct,
946 enum nf_nat_manip_type manip,
39938324 947 const struct nlattr *attr)
e6a7d3c0
PNA
948{
949 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
950
951 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
952 if (!parse_nat_setup) {
95a5afca 953#ifdef CONFIG_MODULES
e6a7d3c0 954 rcu_read_unlock();
748085fc 955 spin_unlock_bh(&nf_conntrack_lock);
e6a7d3c0
PNA
956 nfnl_unlock();
957 if (request_module("nf-nat-ipv4") < 0) {
958 nfnl_lock();
748085fc 959 spin_lock_bh(&nf_conntrack_lock);
e6a7d3c0
PNA
960 rcu_read_lock();
961 return -EOPNOTSUPP;
962 }
963 nfnl_lock();
748085fc 964 spin_lock_bh(&nf_conntrack_lock);
e6a7d3c0
PNA
965 rcu_read_lock();
966 if (nfnetlink_parse_nat_setup_hook)
967 return -EAGAIN;
968#endif
969 return -EOPNOTSUPP;
970 }
971
972 return parse_nat_setup(ct, manip, attr);
973}
67671841 974#endif
e6a7d3c0 975
bb5cf80e 976static int
39938324 977ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
978{
979 unsigned long d;
77236b6e 980 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
c1d10adb
PNA
981 d = ct->status ^ status;
982
983 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
984 /* unchangeable */
0adf9d67 985 return -EBUSY;
601e68e1 986
c1d10adb
PNA
987 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
988 /* SEEN_REPLY bit can only be set */
0adf9d67 989 return -EBUSY;
601e68e1 990
c1d10adb
PNA
991 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
992 /* ASSURED bit can only be set */
0adf9d67 993 return -EBUSY;
c1d10adb 994
c1d10adb
PNA
995 /* Be careful here, modifying NAT bits can screw up things,
996 * so don't let users modify them directly if they don't pass
5b1158e9 997 * nf_nat_range. */
c1d10adb
PNA
998 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
999 return 0;
1000}
1001
e6a7d3c0 1002static int
39938324 1003ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
e6a7d3c0
PNA
1004{
1005#ifdef CONFIG_NF_NAT_NEEDED
1006 int ret;
1007
1008 if (cda[CTA_NAT_DST]) {
1009 ret = ctnetlink_parse_nat_setup(ct,
1010 IP_NAT_MANIP_DST,
1011 cda[CTA_NAT_DST]);
1012 if (ret < 0)
1013 return ret;
1014 }
1015 if (cda[CTA_NAT_SRC]) {
1016 ret = ctnetlink_parse_nat_setup(ct,
1017 IP_NAT_MANIP_SRC,
1018 cda[CTA_NAT_SRC]);
1019 if (ret < 0)
1020 return ret;
1021 }
1022 return 0;
1023#else
1024 return -EOPNOTSUPP;
1025#endif
1026}
c1d10adb
PNA
1027
1028static inline int
39938324 1029ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
1030{
1031 struct nf_conntrack_helper *helper;
dc808fe2 1032 struct nf_conn_help *help = nfct_help(ct);
29fe1b48 1033 char *helpname = NULL;
c1d10adb
PNA
1034 int err;
1035
c1d10adb
PNA
1036 /* don't change helper of sibling connections */
1037 if (ct->master)
0adf9d67 1038 return -EBUSY;
c1d10adb 1039
df6fb868 1040 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
c1d10adb
PNA
1041 if (err < 0)
1042 return err;
1043
df293bbb
YK
1044 if (!strcmp(helpname, "")) {
1045 if (help && help->helper) {
c1d10adb
PNA
1046 /* we had a helper before ... */
1047 nf_ct_remove_expectations(ct);
3c158f7f 1048 rcu_assign_pointer(help->helper, NULL);
c1d10adb 1049 }
df293bbb
YK
1050
1051 return 0;
c1d10adb 1052 }
601e68e1 1053
794e6871
PM
1054 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1055 nf_ct_protonum(ct));
226c0c0e
PNA
1056 if (helper == NULL) {
1057#ifdef CONFIG_MODULES
1058 spin_unlock_bh(&nf_conntrack_lock);
1059
1060 if (request_module("nfct-helper-%s", helpname) < 0) {
1061 spin_lock_bh(&nf_conntrack_lock);
1062 return -EOPNOTSUPP;
1063 }
1064
1065 spin_lock_bh(&nf_conntrack_lock);
794e6871
PM
1066 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1067 nf_ct_protonum(ct));
226c0c0e
PNA
1068 if (helper)
1069 return -EAGAIN;
1070#endif
0adf9d67 1071 return -EOPNOTSUPP;
226c0c0e 1072 }
df293bbb 1073
ceceae1b
YK
1074 if (help) {
1075 if (help->helper == helper)
1076 return 0;
1077 if (help->helper)
1078 return -EBUSY;
1079 /* need to zero data of old helper */
1080 memset(&help->help, 0, sizeof(help->help));
1081 } else {
a88e22ad
PNA
1082 /* we cannot set a helper for an existing conntrack */
1083 return -EOPNOTSUPP;
ceceae1b 1084 }
df293bbb 1085
3c158f7f 1086 rcu_assign_pointer(help->helper, helper);
c1d10adb
PNA
1087
1088 return 0;
1089}
1090
1091static inline int
39938324 1092ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1093{
77236b6e 1094 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
601e68e1 1095
c1d10adb
PNA
1096 if (!del_timer(&ct->timeout))
1097 return -ETIME;
1098
1099 ct->timeout.expires = jiffies + timeout * HZ;
1100 add_timer(&ct->timeout);
1101
1102 return 0;
1103}
1104
d0b0268f
PM
1105static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1106 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1107 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1108 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1109};
1110
c1d10adb 1111static inline int
39938324 1112ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1113{
39938324
PM
1114 const struct nlattr *attr = cda[CTA_PROTOINFO];
1115 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
605dcad6 1116 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
1117 int err = 0;
1118
d0b0268f 1119 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
c1d10adb 1120
cd91566e
FW
1121 rcu_read_lock();
1122 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
fdf70832
PM
1123 if (l4proto->from_nlattr)
1124 err = l4proto->from_nlattr(tb, ct);
cd91566e 1125 rcu_read_unlock();
c1d10adb
PNA
1126
1127 return err;
1128}
1129
13eae15a 1130#ifdef CONFIG_NF_NAT_NEEDED
d0b0268f
PM
1131static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1132 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1133 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1134 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1135};
1136
13eae15a 1137static inline int
39938324 1138change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
13eae15a
PNA
1139{
1140 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1141
d0b0268f 1142 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
13eae15a
PNA
1143
1144 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1145 return -EINVAL;
1146
1147 natseq->correction_pos =
77236b6e 1148 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
13eae15a
PNA
1149
1150 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1151 return -EINVAL;
1152
1153 natseq->offset_before =
77236b6e 1154 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
13eae15a
PNA
1155
1156 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1157 return -EINVAL;
1158
1159 natseq->offset_after =
77236b6e 1160 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
13eae15a
PNA
1161
1162 return 0;
1163}
1164
1165static int
39938324
PM
1166ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1167 const struct nlattr * const cda[])
13eae15a
PNA
1168{
1169 int ret = 0;
1170 struct nf_conn_nat *nat = nfct_nat(ct);
1171
1172 if (!nat)
1173 return 0;
1174
1175 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1176 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1177 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1178 if (ret < 0)
1179 return ret;
1180
1181 ct->status |= IPS_SEQ_ADJUST;
1182 }
1183
1184 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1185 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1186 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1187 if (ret < 0)
1188 return ret;
1189
1190 ct->status |= IPS_SEQ_ADJUST;
1191 }
1192
1193 return 0;
1194}
1195#endif
1196
c1d10adb 1197static int
39938324
PM
1198ctnetlink_change_conntrack(struct nf_conn *ct,
1199 const struct nlattr * const cda[])
c1d10adb
PNA
1200{
1201 int err;
1202
e098360f
PNA
1203 /* only allow NAT changes and master assignation for new conntracks */
1204 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1205 return -EOPNOTSUPP;
1206
df6fb868 1207 if (cda[CTA_HELP]) {
c1d10adb
PNA
1208 err = ctnetlink_change_helper(ct, cda);
1209 if (err < 0)
1210 return err;
1211 }
1212
df6fb868 1213 if (cda[CTA_TIMEOUT]) {
c1d10adb
PNA
1214 err = ctnetlink_change_timeout(ct, cda);
1215 if (err < 0)
1216 return err;
1217 }
1218
df6fb868 1219 if (cda[CTA_STATUS]) {
c1d10adb
PNA
1220 err = ctnetlink_change_status(ct, cda);
1221 if (err < 0)
1222 return err;
1223 }
1224
df6fb868 1225 if (cda[CTA_PROTOINFO]) {
c1d10adb
PNA
1226 err = ctnetlink_change_protoinfo(ct, cda);
1227 if (err < 0)
1228 return err;
1229 }
1230
bcd1e830 1231#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1232 if (cda[CTA_MARK])
77236b6e 1233 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1234#endif
1235
13eae15a
PNA
1236#ifdef CONFIG_NF_NAT_NEEDED
1237 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1238 err = ctnetlink_change_nat_seq_adj(ct, cda);
1239 if (err < 0)
1240 return err;
1241 }
1242#endif
1243
c1d10adb
PNA
1244 return 0;
1245}
1246
f0a3c086 1247static struct nf_conn *
ef00f89f 1248ctnetlink_create_conntrack(struct net *net, u16 zone,
9592a5c0 1249 const struct nlattr * const cda[],
c1d10adb 1250 struct nf_conntrack_tuple *otuple,
5faa1f4c 1251 struct nf_conntrack_tuple *rtuple,
7ec47496 1252 u8 u3)
c1d10adb
PNA
1253{
1254 struct nf_conn *ct;
1255 int err = -EINVAL;
ceceae1b 1256 struct nf_conntrack_helper *helper;
c1d10adb 1257
ef00f89f 1258 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
cd7fcbf1 1259 if (IS_ERR(ct))
f0a3c086 1260 return ERR_PTR(-ENOMEM);
c1d10adb 1261
df6fb868 1262 if (!cda[CTA_TIMEOUT])
0f5b3e85 1263 goto err1;
77236b6e 1264 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
c1d10adb
PNA
1265
1266 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
c1d10adb 1267
1575e7ea 1268 rcu_read_lock();
226c0c0e 1269 if (cda[CTA_HELP]) {
29fe1b48 1270 char *helpname = NULL;
226c0c0e
PNA
1271
1272 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
0f5b3e85
PM
1273 if (err < 0)
1274 goto err2;
226c0c0e 1275
794e6871
PM
1276 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1277 nf_ct_protonum(ct));
226c0c0e
PNA
1278 if (helper == NULL) {
1279 rcu_read_unlock();
1280#ifdef CONFIG_MODULES
1281 if (request_module("nfct-helper-%s", helpname) < 0) {
1282 err = -EOPNOTSUPP;
0f5b3e85 1283 goto err1;
226c0c0e
PNA
1284 }
1285
1286 rcu_read_lock();
794e6871
PM
1287 helper = __nf_conntrack_helper_find(helpname,
1288 nf_ct_l3num(ct),
1289 nf_ct_protonum(ct));
226c0c0e 1290 if (helper) {
226c0c0e 1291 err = -EAGAIN;
0f5b3e85 1292 goto err2;
226c0c0e
PNA
1293 }
1294 rcu_read_unlock();
1295#endif
1296 err = -EOPNOTSUPP;
0f5b3e85 1297 goto err1;
226c0c0e
PNA
1298 } else {
1299 struct nf_conn_help *help;
1300
1301 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1302 if (help == NULL) {
226c0c0e 1303 err = -ENOMEM;
0f5b3e85 1304 goto err2;
226c0c0e
PNA
1305 }
1306
1307 /* not in hash table yet so not strictly necessary */
1308 rcu_assign_pointer(help->helper, helper);
1309 }
1310 } else {
1311 /* try an implicit helper assignation */
b2a15a60 1312 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
0f5b3e85
PM
1313 if (err < 0)
1314 goto err2;
1575e7ea
PNA
1315 }
1316
a88e22ad
PNA
1317 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1318 err = ctnetlink_change_nat(ct, cda);
0f5b3e85
PM
1319 if (err < 0)
1320 goto err2;
e6a7d3c0
PNA
1321 }
1322
a88e22ad
PNA
1323 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1324 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1325 /* we must add conntrack extensions before confirmation. */
1326 ct->status |= IPS_CONFIRMED;
1327
1328 if (cda[CTA_STATUS]) {
1329 err = ctnetlink_change_status(ct, cda);
0f5b3e85
PM
1330 if (err < 0)
1331 goto err2;
bbb3357d 1332 }
c1d10adb 1333
c969aa7d
PNA
1334#ifdef CONFIG_NF_NAT_NEEDED
1335 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1336 err = ctnetlink_change_nat_seq_adj(ct, cda);
0f5b3e85
PM
1337 if (err < 0)
1338 goto err2;
c969aa7d
PNA
1339 }
1340#endif
1341
df6fb868 1342 if (cda[CTA_PROTOINFO]) {
c1d10adb 1343 err = ctnetlink_change_protoinfo(ct, cda);
0f5b3e85
PM
1344 if (err < 0)
1345 goto err2;
c1d10adb
PNA
1346 }
1347
bcd1e830 1348#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1349 if (cda[CTA_MARK])
77236b6e 1350 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1351#endif
1352
5faa1f4c 1353 /* setup master conntrack: this is a confirmed expectation */
7ec47496
PNA
1354 if (cda[CTA_TUPLE_MASTER]) {
1355 struct nf_conntrack_tuple master;
1356 struct nf_conntrack_tuple_hash *master_h;
1357 struct nf_conn *master_ct;
1358
1359 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1360 if (err < 0)
0f5b3e85 1361 goto err2;
7ec47496 1362
ef00f89f 1363 master_h = nf_conntrack_find_get(net, zone, &master);
7ec47496
PNA
1364 if (master_h == NULL) {
1365 err = -ENOENT;
0f5b3e85 1366 goto err2;
7ec47496
PNA
1367 }
1368 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
f2a89004 1369 __set_bit(IPS_EXPECTED_BIT, &ct->status);
5faa1f4c 1370 ct->master = master_ct;
f2a89004 1371 }
5faa1f4c 1372
c1d10adb
PNA
1373 add_timer(&ct->timeout);
1374 nf_conntrack_hash_insert(ct);
58a3c9bb 1375 rcu_read_unlock();
dafc741c 1376
f0a3c086 1377 return ct;
c1d10adb 1378
0f5b3e85
PM
1379err2:
1380 rcu_read_unlock();
1381err1:
c1d10adb 1382 nf_conntrack_free(ct);
f0a3c086 1383 return ERR_PTR(err);
c1d10adb
PNA
1384}
1385
601e68e1
YH
1386static int
1387ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1388 const struct nlmsghdr *nlh,
1389 const struct nlattr * const cda[])
c1d10adb 1390{
9592a5c0 1391 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1392 struct nf_conntrack_tuple otuple, rtuple;
1393 struct nf_conntrack_tuple_hash *h = NULL;
96bcf938 1394 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 1395 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1396 u16 zone;
1397 int err;
1398
1399 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1400 if (err < 0)
1401 return err;
c1d10adb 1402
df6fb868 1403 if (cda[CTA_TUPLE_ORIG]) {
c1d10adb
PNA
1404 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1405 if (err < 0)
1406 return err;
1407 }
1408
df6fb868 1409 if (cda[CTA_TUPLE_REPLY]) {
c1d10adb
PNA
1410 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1411 if (err < 0)
1412 return err;
1413 }
1414
f8ba1aff 1415 spin_lock_bh(&nf_conntrack_lock);
df6fb868 1416 if (cda[CTA_TUPLE_ORIG])
ef00f89f 1417 h = __nf_conntrack_find(net, zone, &otuple);
df6fb868 1418 else if (cda[CTA_TUPLE_REPLY])
ef00f89f 1419 h = __nf_conntrack_find(net, zone, &rtuple);
c1d10adb
PNA
1420
1421 if (h == NULL) {
c1d10adb 1422 err = -ENOENT;
f0a3c086
PNA
1423 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1424 struct nf_conn *ct;
fecc1133 1425 enum ip_conntrack_events events;
5faa1f4c 1426
ef00f89f 1427 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
f0a3c086
PNA
1428 &rtuple, u3);
1429 if (IS_ERR(ct)) {
1430 err = PTR_ERR(ct);
5faa1f4c
PNA
1431 goto out_unlock;
1432 }
f0a3c086
PNA
1433 err = 0;
1434 nf_conntrack_get(&ct->ct_general);
1435 spin_unlock_bh(&nf_conntrack_lock);
fecc1133
PNA
1436 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1437 events = IPCT_RELATED;
1438 else
1439 events = IPCT_NEW;
1440
858b3133
PM
1441 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1442 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1443 (1 << IPCT_HELPER) |
1444 (1 << IPCT_PROTOINFO) |
1445 (1 << IPCT_NATSEQADJ) |
1446 (1 << IPCT_MARK) | events,
1447 ct, NETLINK_CB(skb).pid,
1448 nlmsg_report(nlh));
f0a3c086
PNA
1449 nf_ct_put(ct);
1450 } else
1451 spin_unlock_bh(&nf_conntrack_lock);
5faa1f4c 1452
c1d10adb
PNA
1453 return err;
1454 }
1455 /* implicit 'else' */
1456
c1d10adb
PNA
1457 /* We manipulate the conntrack inside the global conntrack table lock,
1458 * so there's no need to increase the refcount */
c1d10adb 1459 err = -EEXIST;
ff4ca827 1460 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
19abb7b0
PNA
1461 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1462
19abb7b0
PNA
1463 err = ctnetlink_change_conntrack(ct, cda);
1464 if (err == 0) {
1465 nf_conntrack_get(&ct->ct_general);
1466 spin_unlock_bh(&nf_conntrack_lock);
858b3133
PM
1467 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1468 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1469 (1 << IPCT_HELPER) |
1470 (1 << IPCT_PROTOINFO) |
1471 (1 << IPCT_NATSEQADJ) |
1472 (1 << IPCT_MARK),
1473 ct, NETLINK_CB(skb).pid,
1474 nlmsg_report(nlh));
19abb7b0
PNA
1475 nf_ct_put(ct);
1476 } else
1477 spin_unlock_bh(&nf_conntrack_lock);
1478
1479 return err;
ff4ca827 1480 }
c1d10adb
PNA
1481
1482out_unlock:
f8ba1aff 1483 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1484 return err;
1485}
1486
601e68e1
YH
1487/***********************************************************************
1488 * EXPECT
1489 ***********************************************************************/
c1d10adb
PNA
1490
1491static inline int
1492ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1493 const struct nf_conntrack_tuple *tuple,
1494 enum ctattr_expect type)
1495{
df6fb868 1496 struct nlattr *nest_parms;
601e68e1 1497
df6fb868
PM
1498 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1499 if (!nest_parms)
1500 goto nla_put_failure;
c1d10adb 1501 if (ctnetlink_dump_tuples(skb, tuple) < 0)
df6fb868
PM
1502 goto nla_put_failure;
1503 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
1504
1505 return 0;
1506
df6fb868 1507nla_put_failure:
c1d10adb 1508 return -1;
601e68e1 1509}
c1d10adb 1510
1cde6436
PNA
1511static inline int
1512ctnetlink_exp_dump_mask(struct sk_buff *skb,
1513 const struct nf_conntrack_tuple *tuple,
d4156e8c 1514 const struct nf_conntrack_tuple_mask *mask)
1cde6436
PNA
1515{
1516 int ret;
1517 struct nf_conntrack_l3proto *l3proto;
605dcad6 1518 struct nf_conntrack_l4proto *l4proto;
d4156e8c 1519 struct nf_conntrack_tuple m;
df6fb868 1520 struct nlattr *nest_parms;
d4156e8c
PM
1521
1522 memset(&m, 0xFF, sizeof(m));
d4156e8c 1523 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
e578756c
PM
1524 m.src.u.all = mask->src.u.all;
1525 m.dst.protonum = tuple->dst.protonum;
d4156e8c 1526
df6fb868
PM
1527 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1528 if (!nest_parms)
1529 goto nla_put_failure;
1cde6436 1530
528a3a6f 1531 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
d4156e8c 1532 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1cde6436
PNA
1533
1534 if (unlikely(ret < 0))
df6fb868 1535 goto nla_put_failure;
1cde6436 1536
528a3a6f 1537 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
d4156e8c 1538 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1cde6436 1539 if (unlikely(ret < 0))
df6fb868 1540 goto nla_put_failure;
1cde6436 1541
df6fb868 1542 nla_nest_end(skb, nest_parms);
1cde6436
PNA
1543
1544 return 0;
1545
df6fb868 1546nla_put_failure:
1cde6436
PNA
1547 return -1;
1548}
1549
bb5cf80e 1550static int
c1d10adb 1551ctnetlink_exp_dump_expect(struct sk_buff *skb,
601e68e1 1552 const struct nf_conntrack_expect *exp)
c1d10adb
PNA
1553{
1554 struct nf_conn *master = exp->master;
d1e7a03f 1555 struct nf_conntrack_helper *helper;
d978e5da
PM
1556 long timeout = (exp->timeout.expires - jiffies) / HZ;
1557
1558 if (timeout < 0)
1559 timeout = 0;
c1d10adb
PNA
1560
1561 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
df6fb868 1562 goto nla_put_failure;
1cde6436 1563 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
df6fb868 1564 goto nla_put_failure;
c1d10adb
PNA
1565 if (ctnetlink_exp_dump_tuple(skb,
1566 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1567 CTA_EXPECT_MASTER) < 0)
df6fb868 1568 goto nla_put_failure;
601e68e1 1569
d978e5da 1570 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
77236b6e 1571 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
d1e7a03f
PM
1572 helper = rcu_dereference(nfct_help(master)->helper);
1573 if (helper)
1574 NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
c1d10adb
PNA
1575
1576 return 0;
601e68e1 1577
df6fb868 1578nla_put_failure:
c1d10adb
PNA
1579 return -1;
1580}
1581
1582static int
1583ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
8b0a231d 1584 int event, const struct nf_conntrack_expect *exp)
c1d10adb
PNA
1585{
1586 struct nlmsghdr *nlh;
1587 struct nfgenmsg *nfmsg;
96bcf938 1588 unsigned int flags = pid ? NLM_F_MULTI : 0;
c1d10adb
PNA
1589
1590 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
96bcf938
PNA
1591 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1592 if (nlh == NULL)
1593 goto nlmsg_failure;
c1d10adb 1594
96bcf938 1595 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
1596 nfmsg->nfgen_family = exp->tuple.src.l3num;
1597 nfmsg->version = NFNETLINK_V0;
1598 nfmsg->res_id = 0;
1599
1600 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 1601 goto nla_put_failure;
c1d10adb 1602
96bcf938 1603 nlmsg_end(skb, nlh);
c1d10adb
PNA
1604 return skb->len;
1605
1606nlmsg_failure:
df6fb868 1607nla_put_failure:
96bcf938 1608 nlmsg_cancel(skb, nlh);
c1d10adb
PNA
1609 return -1;
1610}
1611
1612#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
1613static int
1614ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
c1d10adb 1615{
9592a5c0
AD
1616 struct nf_conntrack_expect *exp = item->exp;
1617 struct net *net = nf_ct_exp_net(exp);
c1d10adb
PNA
1618 struct nlmsghdr *nlh;
1619 struct nfgenmsg *nfmsg;
c1d10adb
PNA
1620 struct sk_buff *skb;
1621 unsigned int type;
c1d10adb
PNA
1622 int flags = 0;
1623
a0891aa6 1624 if (events & (1 << IPEXP_NEW)) {
c1d10adb
PNA
1625 type = IPCTNL_MSG_EXP_NEW;
1626 flags = NLM_F_CREATE|NLM_F_EXCL;
1627 } else
e34d5c1a 1628 return 0;
c1d10adb 1629
1f9da256 1630 if (!item->report &&
9592a5c0 1631 !nfnetlink_has_listeners(net, NFNLGRP_CONNTRACK_EXP_NEW))
e34d5c1a 1632 return 0;
b3a27bfb 1633
96bcf938
PNA
1634 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1635 if (skb == NULL)
150ace0d 1636 goto errout;
c1d10adb 1637
b633ad5f 1638 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
96bcf938
PNA
1639 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1640 if (nlh == NULL)
1641 goto nlmsg_failure;
c1d10adb 1642
96bcf938 1643 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
1644 nfmsg->nfgen_family = exp->tuple.src.l3num;
1645 nfmsg->version = NFNETLINK_V0;
1646 nfmsg->res_id = 0;
1647
528a3a6f 1648 rcu_read_lock();
c1d10adb 1649 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 1650 goto nla_put_failure;
528a3a6f 1651 rcu_read_unlock();
c1d10adb 1652
96bcf938 1653 nlmsg_end(skb, nlh);
9592a5c0 1654 nfnetlink_send(skb, net, item->pid, NFNLGRP_CONNTRACK_EXP_NEW,
e34d5c1a
PNA
1655 item->report, GFP_ATOMIC);
1656 return 0;
c1d10adb 1657
df6fb868 1658nla_put_failure:
528a3a6f 1659 rcu_read_unlock();
96bcf938 1660 nlmsg_cancel(skb, nlh);
528a3a6f 1661nlmsg_failure:
c1d10adb 1662 kfree_skb(skb);
150ace0d 1663errout:
9592a5c0 1664 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
e34d5c1a 1665 return 0;
c1d10adb
PNA
1666}
1667#endif
cf6994c2
PM
1668static int ctnetlink_exp_done(struct netlink_callback *cb)
1669{
31f15875
PM
1670 if (cb->args[1])
1671 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
cf6994c2
PM
1672 return 0;
1673}
c1d10adb
PNA
1674
1675static int
1676ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1677{
9592a5c0 1678 struct net *net = sock_net(skb->sk);
cf6994c2 1679 struct nf_conntrack_expect *exp, *last;
96bcf938 1680 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
31f15875 1681 struct hlist_node *n;
87711cb8 1682 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 1683
7d0742da 1684 rcu_read_lock();
31f15875
PM
1685 last = (struct nf_conntrack_expect *)cb->args[1];
1686 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
cf6994c2 1687restart:
9b03f38d 1688 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
31f15875
PM
1689 hnode) {
1690 if (l3proto && exp->tuple.src.l3num != l3proto)
cf6994c2 1691 continue;
31f15875
PM
1692 if (cb->args[1]) {
1693 if (exp != last)
1694 continue;
1695 cb->args[1] = 0;
1696 }
8b0a231d
PNA
1697 if (ctnetlink_exp_fill_info(skb,
1698 NETLINK_CB(cb->skb).pid,
31f15875
PM
1699 cb->nlh->nlmsg_seq,
1700 IPCTNL_MSG_EXP_NEW,
8b0a231d 1701 exp) < 0) {
7d0742da
PM
1702 if (!atomic_inc_not_zero(&exp->use))
1703 continue;
31f15875
PM
1704 cb->args[1] = (unsigned long)exp;
1705 goto out;
1706 }
cf6994c2 1707 }
31f15875
PM
1708 if (cb->args[1]) {
1709 cb->args[1] = 0;
1710 goto restart;
cf6994c2
PM
1711 }
1712 }
601e68e1 1713out:
7d0742da 1714 rcu_read_unlock();
cf6994c2
PM
1715 if (last)
1716 nf_ct_expect_put(last);
c1d10adb 1717
c1d10adb
PNA
1718 return skb->len;
1719}
1720
f73e924c 1721static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
d0b0268f
PM
1722 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
1723 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
1724 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
f73e924c
PM
1725 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1726 [CTA_EXPECT_ID] = { .type = NLA_U32 },
d0b0268f 1727 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING },
c1d10adb
PNA
1728};
1729
1730static int
601e68e1 1731ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1732 const struct nlmsghdr *nlh,
1733 const struct nlattr * const cda[])
c1d10adb 1734{
9592a5c0 1735 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1736 struct nf_conntrack_tuple tuple;
1737 struct nf_conntrack_expect *exp;
1738 struct sk_buff *skb2;
96bcf938 1739 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 1740 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1741 u16 zone;
1742 int err;
c1d10adb 1743
c1d10adb 1744 if (nlh->nlmsg_flags & NLM_F_DUMP) {
c702e804
TG
1745 return netlink_dump_start(ctnl, skb, nlh,
1746 ctnetlink_exp_dump_table,
cf6994c2 1747 ctnetlink_exp_done);
c1d10adb
PNA
1748 }
1749
ef00f89f
PM
1750 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1751 if (err < 0)
1752 return err;
1753
df6fb868 1754 if (cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
1755 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1756 else
1757 return -EINVAL;
1758
1759 if (err < 0)
1760 return err;
1761
ef00f89f 1762 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
1763 if (!exp)
1764 return -ENOENT;
1765
df6fb868 1766 if (cda[CTA_EXPECT_ID]) {
77236b6e 1767 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 1768 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 1769 nf_ct_expect_put(exp);
c1d10adb
PNA
1770 return -ENOENT;
1771 }
601e68e1 1772 }
c1d10adb
PNA
1773
1774 err = -ENOMEM;
96bcf938
PNA
1775 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1776 if (skb2 == NULL)
c1d10adb 1777 goto out;
4e9b8269 1778
528a3a6f 1779 rcu_read_lock();
601e68e1 1780 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
8b0a231d 1781 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
528a3a6f 1782 rcu_read_unlock();
c1d10adb
PNA
1783 if (err <= 0)
1784 goto free;
1785
6823645d 1786 nf_ct_expect_put(exp);
c1d10adb
PNA
1787
1788 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1789
1790free:
1791 kfree_skb(skb2);
1792out:
6823645d 1793 nf_ct_expect_put(exp);
c1d10adb
PNA
1794 return err;
1795}
1796
1797static int
601e68e1 1798ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1799 const struct nlmsghdr *nlh,
1800 const struct nlattr * const cda[])
c1d10adb 1801{
9592a5c0 1802 struct net *net = sock_net(ctnl);
31f15875 1803 struct nf_conntrack_expect *exp;
c1d10adb 1804 struct nf_conntrack_tuple tuple;
96bcf938 1805 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
31f15875 1806 struct hlist_node *n, *next;
c1d10adb 1807 u_int8_t u3 = nfmsg->nfgen_family;
31f15875 1808 unsigned int i;
ef00f89f 1809 u16 zone;
c1d10adb
PNA
1810 int err;
1811
df6fb868 1812 if (cda[CTA_EXPECT_TUPLE]) {
c1d10adb 1813 /* delete a single expect by tuple */
ef00f89f
PM
1814 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1815 if (err < 0)
1816 return err;
1817
c1d10adb
PNA
1818 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1819 if (err < 0)
1820 return err;
1821
1822 /* bump usage count to 2 */
ef00f89f 1823 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
1824 if (!exp)
1825 return -ENOENT;
1826
df6fb868 1827 if (cda[CTA_EXPECT_ID]) {
77236b6e 1828 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 1829 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 1830 nf_ct_expect_put(exp);
c1d10adb
PNA
1831 return -ENOENT;
1832 }
1833 }
1834
1835 /* after list removal, usage count == 1 */
6823645d 1836 nf_ct_unexpect_related(exp);
601e68e1 1837 /* have to put what we 'get' above.
c1d10adb 1838 * after this line usage count == 0 */
6823645d 1839 nf_ct_expect_put(exp);
df6fb868
PM
1840 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1841 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
31f15875 1842 struct nf_conn_help *m_help;
c1d10adb
PNA
1843
1844 /* delete all expectations for this helper */
f8ba1aff 1845 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
1846 for (i = 0; i < nf_ct_expect_hsize; i++) {
1847 hlist_for_each_entry_safe(exp, n, next,
9592a5c0 1848 &net->ct.expect_hash[i],
31f15875
PM
1849 hnode) {
1850 m_help = nfct_help(exp->master);
794e6871
PM
1851 if (!strcmp(m_help->helper->name, name) &&
1852 del_timer(&exp->timeout)) {
31f15875
PM
1853 nf_ct_unlink_expect(exp);
1854 nf_ct_expect_put(exp);
1855 }
c1d10adb
PNA
1856 }
1857 }
f8ba1aff 1858 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1859 } else {
1860 /* This basically means we have to flush everything*/
f8ba1aff 1861 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
1862 for (i = 0; i < nf_ct_expect_hsize; i++) {
1863 hlist_for_each_entry_safe(exp, n, next,
9592a5c0 1864 &net->ct.expect_hash[i],
31f15875
PM
1865 hnode) {
1866 if (del_timer(&exp->timeout)) {
1867 nf_ct_unlink_expect(exp);
1868 nf_ct_expect_put(exp);
1869 }
c1d10adb
PNA
1870 }
1871 }
f8ba1aff 1872 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1873 }
1874
1875 return 0;
1876}
1877static int
39938324
PM
1878ctnetlink_change_expect(struct nf_conntrack_expect *x,
1879 const struct nlattr * const cda[])
c1d10adb
PNA
1880{
1881 return -EOPNOTSUPP;
1882}
1883
1884static int
ef00f89f
PM
1885ctnetlink_create_expect(struct net *net, u16 zone,
1886 const struct nlattr * const cda[],
9592a5c0 1887 u_int8_t u3,
39938324 1888 u32 pid, int report)
c1d10adb
PNA
1889{
1890 struct nf_conntrack_tuple tuple, mask, master_tuple;
1891 struct nf_conntrack_tuple_hash *h = NULL;
1892 struct nf_conntrack_expect *exp;
1893 struct nf_conn *ct;
dc808fe2 1894 struct nf_conn_help *help;
c1d10adb
PNA
1895 int err = 0;
1896
c1d10adb
PNA
1897 /* caller guarantees that those three CTA_EXPECT_* exist */
1898 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1899 if (err < 0)
1900 return err;
1901 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1902 if (err < 0)
1903 return err;
1904 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1905 if (err < 0)
1906 return err;
1907
1908 /* Look for master conntrack of this expectation */
ef00f89f 1909 h = nf_conntrack_find_get(net, zone, &master_tuple);
c1d10adb
PNA
1910 if (!h)
1911 return -ENOENT;
1912 ct = nf_ct_tuplehash_to_ctrack(h);
dc808fe2 1913 help = nfct_help(ct);
c1d10adb 1914
dc808fe2 1915 if (!help || !help->helper) {
c1d10adb 1916 /* such conntrack hasn't got any helper, abort */
bfe29677 1917 err = -EOPNOTSUPP;
c1d10adb
PNA
1918 goto out;
1919 }
1920
6823645d 1921 exp = nf_ct_expect_alloc(ct);
c1d10adb
PNA
1922 if (!exp) {
1923 err = -ENOMEM;
1924 goto out;
1925 }
601e68e1 1926
626ba8fb 1927 exp->class = 0;
c1d10adb
PNA
1928 exp->expectfn = NULL;
1929 exp->flags = 0;
1930 exp->master = ct;
9457d851 1931 exp->helper = NULL;
c1d10adb 1932 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
d4156e8c
PM
1933 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1934 exp->mask.src.u.all = mask.src.u.all;
c1d10adb 1935
19abb7b0 1936 err = nf_ct_expect_related_report(exp, pid, report);
6823645d 1937 nf_ct_expect_put(exp);
c1d10adb 1938
601e68e1 1939out:
c1d10adb
PNA
1940 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1941 return err;
1942}
1943
1944static int
1945ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1946 const struct nlmsghdr *nlh,
1947 const struct nlattr * const cda[])
c1d10adb 1948{
9592a5c0 1949 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1950 struct nf_conntrack_tuple tuple;
1951 struct nf_conntrack_expect *exp;
96bcf938 1952 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 1953 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1954 u16 zone;
1955 int err;
c1d10adb 1956
df6fb868
PM
1957 if (!cda[CTA_EXPECT_TUPLE]
1958 || !cda[CTA_EXPECT_MASK]
1959 || !cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
1960 return -EINVAL;
1961
ef00f89f
PM
1962 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1963 if (err < 0)
1964 return err;
1965
c1d10adb
PNA
1966 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1967 if (err < 0)
1968 return err;
1969
f8ba1aff 1970 spin_lock_bh(&nf_conntrack_lock);
ef00f89f 1971 exp = __nf_ct_expect_find(net, zone, &tuple);
c1d10adb
PNA
1972
1973 if (!exp) {
f8ba1aff 1974 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 1975 err = -ENOENT;
19abb7b0 1976 if (nlh->nlmsg_flags & NLM_F_CREATE) {
ef00f89f 1977 err = ctnetlink_create_expect(net, zone, cda,
19abb7b0
PNA
1978 u3,
1979 NETLINK_CB(skb).pid,
1980 nlmsg_report(nlh));
1981 }
c1d10adb
PNA
1982 return err;
1983 }
1984
1985 err = -EEXIST;
1986 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1987 err = ctnetlink_change_expect(exp, cda);
f8ba1aff 1988 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 1989
c1d10adb
PNA
1990 return err;
1991}
1992
1993#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
1994static struct nf_ct_event_notifier ctnl_notifier = {
1995 .fcn = ctnetlink_conntrack_event,
c1d10adb
PNA
1996};
1997
e34d5c1a
PNA
1998static struct nf_exp_event_notifier ctnl_notifier_exp = {
1999 .fcn = ctnetlink_expect_event,
c1d10adb
PNA
2000};
2001#endif
2002
7c8d4cb4 2003static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
c1d10adb 2004 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
f73e924c
PM
2005 .attr_count = CTA_MAX,
2006 .policy = ct_nla_policy },
c1d10adb 2007 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
2008 .attr_count = CTA_MAX,
2009 .policy = ct_nla_policy },
c1d10adb 2010 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
f73e924c
PM
2011 .attr_count = CTA_MAX,
2012 .policy = ct_nla_policy },
c1d10adb 2013 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
2014 .attr_count = CTA_MAX,
2015 .policy = ct_nla_policy },
c1d10adb
PNA
2016};
2017
7c8d4cb4 2018static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
c1d10adb 2019 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
f73e924c
PM
2020 .attr_count = CTA_EXPECT_MAX,
2021 .policy = exp_nla_policy },
c1d10adb 2022 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
f73e924c
PM
2023 .attr_count = CTA_EXPECT_MAX,
2024 .policy = exp_nla_policy },
c1d10adb 2025 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
f73e924c
PM
2026 .attr_count = CTA_EXPECT_MAX,
2027 .policy = exp_nla_policy },
c1d10adb
PNA
2028};
2029
7c8d4cb4 2030static const struct nfnetlink_subsystem ctnl_subsys = {
c1d10adb
PNA
2031 .name = "conntrack",
2032 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2033 .cb_count = IPCTNL_MSG_MAX,
2034 .cb = ctnl_cb,
2035};
2036
7c8d4cb4 2037static const struct nfnetlink_subsystem ctnl_exp_subsys = {
c1d10adb
PNA
2038 .name = "conntrack_expect",
2039 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2040 .cb_count = IPCTNL_MSG_EXP_MAX,
2041 .cb = ctnl_exp_cb,
2042};
2043
d2483dde 2044MODULE_ALIAS("ip_conntrack_netlink");
c1d10adb 2045MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
34f9a2e4 2046MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
c1d10adb
PNA
2047
2048static int __init ctnetlink_init(void)
2049{
2050 int ret;
2051
2052 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
2053 ret = nfnetlink_subsys_register(&ctnl_subsys);
2054 if (ret < 0) {
2055 printk("ctnetlink_init: cannot register with nfnetlink.\n");
2056 goto err_out;
2057 }
2058
2059 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2060 if (ret < 0) {
2061 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
2062 goto err_unreg_subsys;
2063 }
2064
2065#ifdef CONFIG_NF_CONNTRACK_EVENTS
2066 ret = nf_conntrack_register_notifier(&ctnl_notifier);
2067 if (ret < 0) {
2068 printk("ctnetlink_init: cannot register notifier.\n");
2069 goto err_unreg_exp_subsys;
2070 }
2071
6823645d 2072 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
c1d10adb
PNA
2073 if (ret < 0) {
2074 printk("ctnetlink_init: cannot expect register notifier.\n");
2075 goto err_unreg_notifier;
2076 }
2077#endif
2078
2079 return 0;
2080
2081#ifdef CONFIG_NF_CONNTRACK_EVENTS
2082err_unreg_notifier:
2083 nf_conntrack_unregister_notifier(&ctnl_notifier);
2084err_unreg_exp_subsys:
2085 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2086#endif
2087err_unreg_subsys:
2088 nfnetlink_subsys_unregister(&ctnl_subsys);
2089err_out:
2090 return ret;
2091}
2092
2093static void __exit ctnetlink_exit(void)
2094{
2095 printk("ctnetlink: unregistering from nfnetlink.\n");
2096
2097#ifdef CONFIG_NF_CONNTRACK_EVENTS
6823645d 2098 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
c1d10adb
PNA
2099 nf_conntrack_unregister_notifier(&ctnl_notifier);
2100#endif
2101
2102 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2103 nfnetlink_subsys_unregister(&ctnl_subsys);
2104 return;
2105}
2106
2107module_init(ctnetlink_init);
2108module_exit(ctnetlink_exit);