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