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