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