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