]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/netfilter/nf_conntrack_netlink.c
netfilter: ctnetlink: remove dead NAT code
[mirror_ubuntu-zesty-kernel.git] / net / netfilter / nf_conntrack_netlink.c
CommitLineData
c1d10adb
PNA
1/* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
3 *
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
dc808fe2 5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
c1d10adb 6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
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
c4042a33
PNA
1018 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO) {
1019 struct nf_conn_counter *acct;
1020
1021 acct = nf_conn_acct_find(ct);
1022 if (acct) {
1023 atomic64_set(&acct[IP_CT_DIR_ORIGINAL].bytes, 0);
1024 atomic64_set(&acct[IP_CT_DIR_ORIGINAL].packets, 0);
1025 atomic64_set(&acct[IP_CT_DIR_REPLY].bytes, 0);
1026 atomic64_set(&acct[IP_CT_DIR_REPLY].packets, 0);
1027 }
1028 }
c1d10adb
PNA
1029 return 0;
1030
1031free:
1032 kfree_skb(skb2);
1033out:
f31e8d49
PNA
1034 /* this avoids a loop in nfnetlink. */
1035 return err == -EAGAIN ? -ENOBUFS : err;
c1d10adb
PNA
1036}
1037
67671841 1038#ifdef CONFIG_NF_NAT_NEEDED
e6a7d3c0
PNA
1039static int
1040ctnetlink_parse_nat_setup(struct nf_conn *ct,
1041 enum nf_nat_manip_type manip,
39938324 1042 const struct nlattr *attr)
e6a7d3c0
PNA
1043{
1044 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1045
1046 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1047 if (!parse_nat_setup) {
95a5afca 1048#ifdef CONFIG_MODULES
e6a7d3c0 1049 rcu_read_unlock();
748085fc 1050 spin_unlock_bh(&nf_conntrack_lock);
e6a7d3c0
PNA
1051 nfnl_unlock();
1052 if (request_module("nf-nat-ipv4") < 0) {
1053 nfnl_lock();
748085fc 1054 spin_lock_bh(&nf_conntrack_lock);
e6a7d3c0
PNA
1055 rcu_read_lock();
1056 return -EOPNOTSUPP;
1057 }
1058 nfnl_lock();
748085fc 1059 spin_lock_bh(&nf_conntrack_lock);
e6a7d3c0
PNA
1060 rcu_read_lock();
1061 if (nfnetlink_parse_nat_setup_hook)
1062 return -EAGAIN;
1063#endif
1064 return -EOPNOTSUPP;
1065 }
1066
1067 return parse_nat_setup(ct, manip, attr);
1068}
67671841 1069#endif
e6a7d3c0 1070
bb5cf80e 1071static int
39938324 1072ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
1073{
1074 unsigned long d;
77236b6e 1075 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
c1d10adb
PNA
1076 d = ct->status ^ status;
1077
1078 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1079 /* unchangeable */
0adf9d67 1080 return -EBUSY;
601e68e1 1081
c1d10adb
PNA
1082 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1083 /* SEEN_REPLY bit can only be set */
0adf9d67 1084 return -EBUSY;
601e68e1 1085
c1d10adb
PNA
1086 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1087 /* ASSURED bit can only be set */
0adf9d67 1088 return -EBUSY;
c1d10adb 1089
c1d10adb
PNA
1090 /* Be careful here, modifying NAT bits can screw up things,
1091 * so don't let users modify them directly if they don't pass
5b1158e9 1092 * nf_nat_range. */
c1d10adb
PNA
1093 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1094 return 0;
1095}
1096
e6a7d3c0 1097static int
39938324 1098ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
e6a7d3c0
PNA
1099{
1100#ifdef CONFIG_NF_NAT_NEEDED
1101 int ret;
1102
1103 if (cda[CTA_NAT_DST]) {
1104 ret = ctnetlink_parse_nat_setup(ct,
cbc9f2f4 1105 NF_NAT_MANIP_DST,
e6a7d3c0
PNA
1106 cda[CTA_NAT_DST]);
1107 if (ret < 0)
1108 return ret;
1109 }
1110 if (cda[CTA_NAT_SRC]) {
1111 ret = ctnetlink_parse_nat_setup(ct,
cbc9f2f4 1112 NF_NAT_MANIP_SRC,
e6a7d3c0
PNA
1113 cda[CTA_NAT_SRC]);
1114 if (ret < 0)
1115 return ret;
1116 }
1117 return 0;
1118#else
1119 return -EOPNOTSUPP;
1120#endif
1121}
c1d10adb
PNA
1122
1123static inline int
39938324 1124ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
1125{
1126 struct nf_conntrack_helper *helper;
dc808fe2 1127 struct nf_conn_help *help = nfct_help(ct);
29fe1b48 1128 char *helpname = NULL;
c1d10adb
PNA
1129 int err;
1130
c1d10adb
PNA
1131 /* don't change helper of sibling connections */
1132 if (ct->master)
0adf9d67 1133 return -EBUSY;
c1d10adb 1134
df6fb868 1135 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
c1d10adb
PNA
1136 if (err < 0)
1137 return err;
1138
df293bbb
YK
1139 if (!strcmp(helpname, "")) {
1140 if (help && help->helper) {
c1d10adb
PNA
1141 /* we had a helper before ... */
1142 nf_ct_remove_expectations(ct);
a9b3cd7f 1143 RCU_INIT_POINTER(help->helper, NULL);
c1d10adb 1144 }
df293bbb
YK
1145
1146 return 0;
c1d10adb 1147 }
601e68e1 1148
794e6871
PM
1149 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1150 nf_ct_protonum(ct));
226c0c0e
PNA
1151 if (helper == NULL) {
1152#ifdef CONFIG_MODULES
1153 spin_unlock_bh(&nf_conntrack_lock);
1154
1155 if (request_module("nfct-helper-%s", helpname) < 0) {
1156 spin_lock_bh(&nf_conntrack_lock);
1157 return -EOPNOTSUPP;
1158 }
1159
1160 spin_lock_bh(&nf_conntrack_lock);
794e6871
PM
1161 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1162 nf_ct_protonum(ct));
226c0c0e
PNA
1163 if (helper)
1164 return -EAGAIN;
1165#endif
0adf9d67 1166 return -EOPNOTSUPP;
226c0c0e 1167 }
df293bbb 1168
ceceae1b
YK
1169 if (help) {
1170 if (help->helper == helper)
1171 return 0;
1172 if (help->helper)
1173 return -EBUSY;
1174 /* need to zero data of old helper */
1175 memset(&help->help, 0, sizeof(help->help));
1176 } else {
a88e22ad
PNA
1177 /* we cannot set a helper for an existing conntrack */
1178 return -EOPNOTSUPP;
ceceae1b 1179 }
df293bbb 1180
a9b3cd7f 1181 RCU_INIT_POINTER(help->helper, helper);
c1d10adb
PNA
1182
1183 return 0;
1184}
1185
1186static inline int
39938324 1187ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1188{
77236b6e 1189 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
601e68e1 1190
c1d10adb
PNA
1191 if (!del_timer(&ct->timeout))
1192 return -ETIME;
1193
1194 ct->timeout.expires = jiffies + timeout * HZ;
1195 add_timer(&ct->timeout);
1196
1197 return 0;
1198}
1199
d0b0268f
PM
1200static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1201 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1202 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1203 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1204};
1205
c1d10adb 1206static inline int
39938324 1207ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1208{
39938324
PM
1209 const struct nlattr *attr = cda[CTA_PROTOINFO];
1210 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
605dcad6 1211 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
1212 int err = 0;
1213
d0b0268f 1214 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
c1d10adb 1215
cd91566e
FW
1216 rcu_read_lock();
1217 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
fdf70832
PM
1218 if (l4proto->from_nlattr)
1219 err = l4proto->from_nlattr(tb, ct);
cd91566e 1220 rcu_read_unlock();
c1d10adb
PNA
1221
1222 return err;
1223}
1224
13eae15a 1225#ifdef CONFIG_NF_NAT_NEEDED
d0b0268f
PM
1226static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1227 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1228 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1229 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1230};
1231
13eae15a 1232static inline int
39938324 1233change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
13eae15a
PNA
1234{
1235 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1236
d0b0268f 1237 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
13eae15a
PNA
1238
1239 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1240 return -EINVAL;
1241
1242 natseq->correction_pos =
77236b6e 1243 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
13eae15a
PNA
1244
1245 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1246 return -EINVAL;
1247
1248 natseq->offset_before =
77236b6e 1249 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
13eae15a
PNA
1250
1251 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1252 return -EINVAL;
1253
1254 natseq->offset_after =
77236b6e 1255 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
13eae15a
PNA
1256
1257 return 0;
1258}
1259
1260static int
39938324
PM
1261ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1262 const struct nlattr * const cda[])
13eae15a
PNA
1263{
1264 int ret = 0;
1265 struct nf_conn_nat *nat = nfct_nat(ct);
1266
1267 if (!nat)
1268 return 0;
1269
1270 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1271 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1272 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1273 if (ret < 0)
1274 return ret;
1275
1276 ct->status |= IPS_SEQ_ADJUST;
1277 }
1278
1279 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1280 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1281 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1282 if (ret < 0)
1283 return ret;
1284
1285 ct->status |= IPS_SEQ_ADJUST;
1286 }
1287
1288 return 0;
1289}
1290#endif
1291
c1d10adb 1292static int
39938324
PM
1293ctnetlink_change_conntrack(struct nf_conn *ct,
1294 const struct nlattr * const cda[])
c1d10adb
PNA
1295{
1296 int err;
1297
e098360f
PNA
1298 /* only allow NAT changes and master assignation for new conntracks */
1299 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1300 return -EOPNOTSUPP;
1301
df6fb868 1302 if (cda[CTA_HELP]) {
c1d10adb
PNA
1303 err = ctnetlink_change_helper(ct, cda);
1304 if (err < 0)
1305 return err;
1306 }
1307
df6fb868 1308 if (cda[CTA_TIMEOUT]) {
c1d10adb
PNA
1309 err = ctnetlink_change_timeout(ct, cda);
1310 if (err < 0)
1311 return err;
1312 }
1313
df6fb868 1314 if (cda[CTA_STATUS]) {
c1d10adb
PNA
1315 err = ctnetlink_change_status(ct, cda);
1316 if (err < 0)
1317 return err;
1318 }
1319
df6fb868 1320 if (cda[CTA_PROTOINFO]) {
c1d10adb
PNA
1321 err = ctnetlink_change_protoinfo(ct, cda);
1322 if (err < 0)
1323 return err;
1324 }
1325
bcd1e830 1326#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1327 if (cda[CTA_MARK])
77236b6e 1328 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1329#endif
1330
13eae15a
PNA
1331#ifdef CONFIG_NF_NAT_NEEDED
1332 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1333 err = ctnetlink_change_nat_seq_adj(ct, cda);
1334 if (err < 0)
1335 return err;
1336 }
1337#endif
1338
c1d10adb
PNA
1339 return 0;
1340}
1341
f0a3c086 1342static struct nf_conn *
ef00f89f 1343ctnetlink_create_conntrack(struct net *net, u16 zone,
9592a5c0 1344 const struct nlattr * const cda[],
c1d10adb 1345 struct nf_conntrack_tuple *otuple,
5faa1f4c 1346 struct nf_conntrack_tuple *rtuple,
7ec47496 1347 u8 u3)
c1d10adb
PNA
1348{
1349 struct nf_conn *ct;
1350 int err = -EINVAL;
ceceae1b 1351 struct nf_conntrack_helper *helper;
315c34da 1352 struct nf_conn_tstamp *tstamp;
c1d10adb 1353
ef00f89f 1354 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
cd7fcbf1 1355 if (IS_ERR(ct))
f0a3c086 1356 return ERR_PTR(-ENOMEM);
c1d10adb 1357
df6fb868 1358 if (!cda[CTA_TIMEOUT])
0f5b3e85 1359 goto err1;
77236b6e 1360 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
c1d10adb
PNA
1361
1362 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
c1d10adb 1363
1575e7ea 1364 rcu_read_lock();
226c0c0e 1365 if (cda[CTA_HELP]) {
29fe1b48 1366 char *helpname = NULL;
226c0c0e
PNA
1367
1368 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
0f5b3e85
PM
1369 if (err < 0)
1370 goto err2;
226c0c0e 1371
794e6871
PM
1372 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1373 nf_ct_protonum(ct));
226c0c0e
PNA
1374 if (helper == NULL) {
1375 rcu_read_unlock();
1376#ifdef CONFIG_MODULES
1377 if (request_module("nfct-helper-%s", helpname) < 0) {
1378 err = -EOPNOTSUPP;
0f5b3e85 1379 goto err1;
226c0c0e
PNA
1380 }
1381
1382 rcu_read_lock();
794e6871
PM
1383 helper = __nf_conntrack_helper_find(helpname,
1384 nf_ct_l3num(ct),
1385 nf_ct_protonum(ct));
226c0c0e 1386 if (helper) {
226c0c0e 1387 err = -EAGAIN;
0f5b3e85 1388 goto err2;
226c0c0e
PNA
1389 }
1390 rcu_read_unlock();
1391#endif
1392 err = -EOPNOTSUPP;
0f5b3e85 1393 goto err1;
226c0c0e
PNA
1394 } else {
1395 struct nf_conn_help *help;
1396
1397 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1398 if (help == NULL) {
226c0c0e 1399 err = -ENOMEM;
0f5b3e85 1400 goto err2;
226c0c0e
PNA
1401 }
1402
1403 /* not in hash table yet so not strictly necessary */
a9b3cd7f 1404 RCU_INIT_POINTER(help->helper, helper);
226c0c0e
PNA
1405 }
1406 } else {
1407 /* try an implicit helper assignation */
b2a15a60 1408 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
0f5b3e85
PM
1409 if (err < 0)
1410 goto err2;
1575e7ea
PNA
1411 }
1412
a88e22ad
PNA
1413 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1414 err = ctnetlink_change_nat(ct, cda);
0f5b3e85
PM
1415 if (err < 0)
1416 goto err2;
e6a7d3c0
PNA
1417 }
1418
a88e22ad 1419 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
a992ca2a 1420 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
a88e22ad
PNA
1421 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1422 /* we must add conntrack extensions before confirmation. */
1423 ct->status |= IPS_CONFIRMED;
1424
1425 if (cda[CTA_STATUS]) {
1426 err = ctnetlink_change_status(ct, cda);
0f5b3e85
PM
1427 if (err < 0)
1428 goto err2;
bbb3357d 1429 }
c1d10adb 1430
c969aa7d
PNA
1431#ifdef CONFIG_NF_NAT_NEEDED
1432 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1433 err = ctnetlink_change_nat_seq_adj(ct, cda);
0f5b3e85
PM
1434 if (err < 0)
1435 goto err2;
c969aa7d
PNA
1436 }
1437#endif
1438
e5fc9e7a 1439 memset(&ct->proto, 0, sizeof(ct->proto));
df6fb868 1440 if (cda[CTA_PROTOINFO]) {
c1d10adb 1441 err = ctnetlink_change_protoinfo(ct, cda);
0f5b3e85
PM
1442 if (err < 0)
1443 goto err2;
c1d10adb
PNA
1444 }
1445
bcd1e830 1446#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1447 if (cda[CTA_MARK])
77236b6e 1448 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1449#endif
1450
5faa1f4c 1451 /* setup master conntrack: this is a confirmed expectation */
7ec47496
PNA
1452 if (cda[CTA_TUPLE_MASTER]) {
1453 struct nf_conntrack_tuple master;
1454 struct nf_conntrack_tuple_hash *master_h;
1455 struct nf_conn *master_ct;
1456
1457 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1458 if (err < 0)
0f5b3e85 1459 goto err2;
7ec47496 1460
ef00f89f 1461 master_h = nf_conntrack_find_get(net, zone, &master);
7ec47496
PNA
1462 if (master_h == NULL) {
1463 err = -ENOENT;
0f5b3e85 1464 goto err2;
7ec47496
PNA
1465 }
1466 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
f2a89004 1467 __set_bit(IPS_EXPECTED_BIT, &ct->status);
5faa1f4c 1468 ct->master = master_ct;
f2a89004 1469 }
315c34da
PNA
1470 tstamp = nf_conn_tstamp_find(ct);
1471 if (tstamp)
1472 tstamp->start = ktime_to_ns(ktime_get_real());
5faa1f4c 1473
c1d10adb
PNA
1474 add_timer(&ct->timeout);
1475 nf_conntrack_hash_insert(ct);
58a3c9bb 1476 rcu_read_unlock();
dafc741c 1477
f0a3c086 1478 return ct;
c1d10adb 1479
0f5b3e85
PM
1480err2:
1481 rcu_read_unlock();
1482err1:
c1d10adb 1483 nf_conntrack_free(ct);
f0a3c086 1484 return ERR_PTR(err);
c1d10adb
PNA
1485}
1486
601e68e1
YH
1487static int
1488ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1489 const struct nlmsghdr *nlh,
1490 const struct nlattr * const cda[])
c1d10adb 1491{
9592a5c0 1492 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1493 struct nf_conntrack_tuple otuple, rtuple;
1494 struct nf_conntrack_tuple_hash *h = NULL;
96bcf938 1495 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 1496 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1497 u16 zone;
1498 int err;
1499
1500 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1501 if (err < 0)
1502 return err;
c1d10adb 1503
df6fb868 1504 if (cda[CTA_TUPLE_ORIG]) {
c1d10adb
PNA
1505 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1506 if (err < 0)
1507 return err;
1508 }
1509
df6fb868 1510 if (cda[CTA_TUPLE_REPLY]) {
c1d10adb
PNA
1511 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1512 if (err < 0)
1513 return err;
1514 }
1515
f8ba1aff 1516 spin_lock_bh(&nf_conntrack_lock);
df6fb868 1517 if (cda[CTA_TUPLE_ORIG])
ef00f89f 1518 h = __nf_conntrack_find(net, zone, &otuple);
df6fb868 1519 else if (cda[CTA_TUPLE_REPLY])
ef00f89f 1520 h = __nf_conntrack_find(net, zone, &rtuple);
c1d10adb
PNA
1521
1522 if (h == NULL) {
c1d10adb 1523 err = -ENOENT;
f0a3c086
PNA
1524 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1525 struct nf_conn *ct;
fecc1133 1526 enum ip_conntrack_events events;
5faa1f4c 1527
ef00f89f 1528 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
f0a3c086
PNA
1529 &rtuple, u3);
1530 if (IS_ERR(ct)) {
1531 err = PTR_ERR(ct);
5faa1f4c
PNA
1532 goto out_unlock;
1533 }
f0a3c086
PNA
1534 err = 0;
1535 nf_conntrack_get(&ct->ct_general);
1536 spin_unlock_bh(&nf_conntrack_lock);
fecc1133
PNA
1537 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1538 events = IPCT_RELATED;
1539 else
1540 events = IPCT_NEW;
1541
858b3133
PM
1542 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1543 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1544 (1 << IPCT_HELPER) |
1545 (1 << IPCT_PROTOINFO) |
1546 (1 << IPCT_NATSEQADJ) |
1547 (1 << IPCT_MARK) | events,
1548 ct, NETLINK_CB(skb).pid,
1549 nlmsg_report(nlh));
f0a3c086
PNA
1550 nf_ct_put(ct);
1551 } else
1552 spin_unlock_bh(&nf_conntrack_lock);
5faa1f4c 1553
c1d10adb
PNA
1554 return err;
1555 }
1556 /* implicit 'else' */
1557
c1d10adb
PNA
1558 /* We manipulate the conntrack inside the global conntrack table lock,
1559 * so there's no need to increase the refcount */
c1d10adb 1560 err = -EEXIST;
ff4ca827 1561 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
19abb7b0
PNA
1562 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1563
19abb7b0
PNA
1564 err = ctnetlink_change_conntrack(ct, cda);
1565 if (err == 0) {
1566 nf_conntrack_get(&ct->ct_general);
1567 spin_unlock_bh(&nf_conntrack_lock);
858b3133
PM
1568 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1569 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1570 (1 << IPCT_HELPER) |
1571 (1 << IPCT_PROTOINFO) |
1572 (1 << IPCT_NATSEQADJ) |
1573 (1 << IPCT_MARK),
1574 ct, NETLINK_CB(skb).pid,
1575 nlmsg_report(nlh));
19abb7b0
PNA
1576 nf_ct_put(ct);
1577 } else
1578 spin_unlock_bh(&nf_conntrack_lock);
1579
1580 return err;
ff4ca827 1581 }
c1d10adb
PNA
1582
1583out_unlock:
f8ba1aff 1584 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1585 return err;
1586}
1587
601e68e1
YH
1588/***********************************************************************
1589 * EXPECT
1590 ***********************************************************************/
c1d10adb
PNA
1591
1592static inline int
1593ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1594 const struct nf_conntrack_tuple *tuple,
1595 enum ctattr_expect type)
1596{
df6fb868 1597 struct nlattr *nest_parms;
601e68e1 1598
df6fb868
PM
1599 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1600 if (!nest_parms)
1601 goto nla_put_failure;
c1d10adb 1602 if (ctnetlink_dump_tuples(skb, tuple) < 0)
df6fb868
PM
1603 goto nla_put_failure;
1604 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
1605
1606 return 0;
1607
df6fb868 1608nla_put_failure:
c1d10adb 1609 return -1;
601e68e1 1610}
c1d10adb 1611
1cde6436
PNA
1612static inline int
1613ctnetlink_exp_dump_mask(struct sk_buff *skb,
1614 const struct nf_conntrack_tuple *tuple,
d4156e8c 1615 const struct nf_conntrack_tuple_mask *mask)
1cde6436
PNA
1616{
1617 int ret;
1618 struct nf_conntrack_l3proto *l3proto;
605dcad6 1619 struct nf_conntrack_l4proto *l4proto;
d4156e8c 1620 struct nf_conntrack_tuple m;
df6fb868 1621 struct nlattr *nest_parms;
d4156e8c
PM
1622
1623 memset(&m, 0xFF, sizeof(m));
d4156e8c 1624 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
e578756c
PM
1625 m.src.u.all = mask->src.u.all;
1626 m.dst.protonum = tuple->dst.protonum;
d4156e8c 1627
df6fb868
PM
1628 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1629 if (!nest_parms)
1630 goto nla_put_failure;
1cde6436 1631
528a3a6f 1632 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
d4156e8c 1633 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1cde6436
PNA
1634
1635 if (unlikely(ret < 0))
df6fb868 1636 goto nla_put_failure;
1cde6436 1637
528a3a6f 1638 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
d4156e8c 1639 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1cde6436 1640 if (unlikely(ret < 0))
df6fb868 1641 goto nla_put_failure;
1cde6436 1642
df6fb868 1643 nla_nest_end(skb, nest_parms);
1cde6436
PNA
1644
1645 return 0;
1646
df6fb868 1647nla_put_failure:
1cde6436
PNA
1648 return -1;
1649}
1650
bb5cf80e 1651static int
c1d10adb 1652ctnetlink_exp_dump_expect(struct sk_buff *skb,
601e68e1 1653 const struct nf_conntrack_expect *exp)
c1d10adb
PNA
1654{
1655 struct nf_conn *master = exp->master;
d978e5da 1656 long timeout = (exp->timeout.expires - jiffies) / HZ;
bc01befd 1657 struct nf_conn_help *help;
d978e5da
PM
1658
1659 if (timeout < 0)
1660 timeout = 0;
c1d10adb
PNA
1661
1662 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
df6fb868 1663 goto nla_put_failure;
1cde6436 1664 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
df6fb868 1665 goto nla_put_failure;
c1d10adb
PNA
1666 if (ctnetlink_exp_dump_tuple(skb,
1667 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1668 CTA_EXPECT_MASTER) < 0)
df6fb868 1669 goto nla_put_failure;
601e68e1 1670
d978e5da 1671 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
77236b6e 1672 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
8b008faf 1673 NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
bc01befd
PNA
1674 help = nfct_help(master);
1675 if (help) {
1676 struct nf_conntrack_helper *helper;
1677
1678 helper = rcu_dereference(help->helper);
1679 if (helper)
1680 NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1681 }
c1d10adb
PNA
1682
1683 return 0;
601e68e1 1684
df6fb868 1685nla_put_failure:
c1d10adb
PNA
1686 return -1;
1687}
1688
1689static int
1690ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
8b0a231d 1691 int event, const struct nf_conntrack_expect *exp)
c1d10adb
PNA
1692{
1693 struct nlmsghdr *nlh;
1694 struct nfgenmsg *nfmsg;
96bcf938 1695 unsigned int flags = pid ? NLM_F_MULTI : 0;
c1d10adb
PNA
1696
1697 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
96bcf938
PNA
1698 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1699 if (nlh == NULL)
1700 goto nlmsg_failure;
c1d10adb 1701
96bcf938 1702 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
1703 nfmsg->nfgen_family = exp->tuple.src.l3num;
1704 nfmsg->version = NFNETLINK_V0;
1705 nfmsg->res_id = 0;
1706
1707 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 1708 goto nla_put_failure;
c1d10adb 1709
96bcf938 1710 nlmsg_end(skb, nlh);
c1d10adb
PNA
1711 return skb->len;
1712
1713nlmsg_failure:
df6fb868 1714nla_put_failure:
96bcf938 1715 nlmsg_cancel(skb, nlh);
c1d10adb
PNA
1716 return -1;
1717}
1718
1719#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
1720static int
1721ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
c1d10adb 1722{
9592a5c0
AD
1723 struct nf_conntrack_expect *exp = item->exp;
1724 struct net *net = nf_ct_exp_net(exp);
c1d10adb
PNA
1725 struct nlmsghdr *nlh;
1726 struct nfgenmsg *nfmsg;
c1d10adb 1727 struct sk_buff *skb;
ebbf41df 1728 unsigned int type, group;
c1d10adb
PNA
1729 int flags = 0;
1730
ebbf41df
PNA
1731 if (events & (1 << IPEXP_DESTROY)) {
1732 type = IPCTNL_MSG_EXP_DELETE;
1733 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
1734 } else if (events & (1 << IPEXP_NEW)) {
c1d10adb
PNA
1735 type = IPCTNL_MSG_EXP_NEW;
1736 flags = NLM_F_CREATE|NLM_F_EXCL;
ebbf41df 1737 group = NFNLGRP_CONNTRACK_EXP_NEW;
c1d10adb 1738 } else
e34d5c1a 1739 return 0;
c1d10adb 1740
ebbf41df 1741 if (!item->report && !nfnetlink_has_listeners(net, group))
e34d5c1a 1742 return 0;
b3a27bfb 1743
96bcf938
PNA
1744 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1745 if (skb == NULL)
150ace0d 1746 goto errout;
c1d10adb 1747
b633ad5f 1748 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
96bcf938
PNA
1749 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1750 if (nlh == NULL)
1751 goto nlmsg_failure;
c1d10adb 1752
96bcf938 1753 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
1754 nfmsg->nfgen_family = exp->tuple.src.l3num;
1755 nfmsg->version = NFNETLINK_V0;
1756 nfmsg->res_id = 0;
1757
528a3a6f 1758 rcu_read_lock();
c1d10adb 1759 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 1760 goto nla_put_failure;
528a3a6f 1761 rcu_read_unlock();
c1d10adb 1762
96bcf938 1763 nlmsg_end(skb, nlh);
ebbf41df 1764 nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC);
e34d5c1a 1765 return 0;
c1d10adb 1766
df6fb868 1767nla_put_failure:
528a3a6f 1768 rcu_read_unlock();
96bcf938 1769 nlmsg_cancel(skb, nlh);
528a3a6f 1770nlmsg_failure:
c1d10adb 1771 kfree_skb(skb);
150ace0d 1772errout:
9592a5c0 1773 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
e34d5c1a 1774 return 0;
c1d10adb
PNA
1775}
1776#endif
cf6994c2
PM
1777static int ctnetlink_exp_done(struct netlink_callback *cb)
1778{
31f15875
PM
1779 if (cb->args[1])
1780 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
cf6994c2
PM
1781 return 0;
1782}
c1d10adb
PNA
1783
1784static int
1785ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1786{
9592a5c0 1787 struct net *net = sock_net(skb->sk);
cf6994c2 1788 struct nf_conntrack_expect *exp, *last;
96bcf938 1789 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
31f15875 1790 struct hlist_node *n;
87711cb8 1791 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 1792
7d0742da 1793 rcu_read_lock();
31f15875
PM
1794 last = (struct nf_conntrack_expect *)cb->args[1];
1795 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
cf6994c2 1796restart:
9b03f38d 1797 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
31f15875
PM
1798 hnode) {
1799 if (l3proto && exp->tuple.src.l3num != l3proto)
cf6994c2 1800 continue;
31f15875
PM
1801 if (cb->args[1]) {
1802 if (exp != last)
1803 continue;
1804 cb->args[1] = 0;
1805 }
8b0a231d
PNA
1806 if (ctnetlink_exp_fill_info(skb,
1807 NETLINK_CB(cb->skb).pid,
31f15875
PM
1808 cb->nlh->nlmsg_seq,
1809 IPCTNL_MSG_EXP_NEW,
8b0a231d 1810 exp) < 0) {
7d0742da
PM
1811 if (!atomic_inc_not_zero(&exp->use))
1812 continue;
31f15875
PM
1813 cb->args[1] = (unsigned long)exp;
1814 goto out;
1815 }
cf6994c2 1816 }
31f15875
PM
1817 if (cb->args[1]) {
1818 cb->args[1] = 0;
1819 goto restart;
cf6994c2
PM
1820 }
1821 }
601e68e1 1822out:
7d0742da 1823 rcu_read_unlock();
cf6994c2
PM
1824 if (last)
1825 nf_ct_expect_put(last);
c1d10adb 1826
c1d10adb
PNA
1827 return skb->len;
1828}
1829
f73e924c 1830static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
d0b0268f
PM
1831 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
1832 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
1833 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
f73e924c
PM
1834 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1835 [CTA_EXPECT_ID] = { .type = NLA_U32 },
d0b0268f 1836 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING },
bcac0dfa 1837 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
8b008faf 1838 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
c1d10adb
PNA
1839};
1840
1841static int
601e68e1 1842ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1843 const struct nlmsghdr *nlh,
1844 const struct nlattr * const cda[])
c1d10adb 1845{
9592a5c0 1846 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1847 struct nf_conntrack_tuple tuple;
1848 struct nf_conntrack_expect *exp;
1849 struct sk_buff *skb2;
96bcf938 1850 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 1851 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1852 u16 zone;
1853 int err;
c1d10adb 1854
b8f3ab42 1855 if (nlh->nlmsg_flags & NLM_F_DUMP) {
c702e804
TG
1856 return netlink_dump_start(ctnl, skb, nlh,
1857 ctnetlink_exp_dump_table,
c7ac8679 1858 ctnetlink_exp_done, 0);
c1d10adb
PNA
1859 }
1860
ef00f89f
PM
1861 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1862 if (err < 0)
1863 return err;
1864
35dba1d7
PNA
1865 if (cda[CTA_EXPECT_TUPLE])
1866 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1867 else if (cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
1868 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1869 else
1870 return -EINVAL;
1871
1872 if (err < 0)
1873 return err;
1874
ef00f89f 1875 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
1876 if (!exp)
1877 return -ENOENT;
1878
df6fb868 1879 if (cda[CTA_EXPECT_ID]) {
77236b6e 1880 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 1881 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 1882 nf_ct_expect_put(exp);
c1d10adb
PNA
1883 return -ENOENT;
1884 }
601e68e1 1885 }
c1d10adb
PNA
1886
1887 err = -ENOMEM;
96bcf938
PNA
1888 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1889 if (skb2 == NULL)
c1d10adb 1890 goto out;
4e9b8269 1891
528a3a6f 1892 rcu_read_lock();
601e68e1 1893 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
8b0a231d 1894 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
528a3a6f 1895 rcu_read_unlock();
c1d10adb
PNA
1896 if (err <= 0)
1897 goto free;
1898
6823645d 1899 nf_ct_expect_put(exp);
c1d10adb
PNA
1900
1901 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1902
1903free:
1904 kfree_skb(skb2);
1905out:
6823645d 1906 nf_ct_expect_put(exp);
c1d10adb
PNA
1907 return err;
1908}
1909
1910static int
601e68e1 1911ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1912 const struct nlmsghdr *nlh,
1913 const struct nlattr * const cda[])
c1d10adb 1914{
9592a5c0 1915 struct net *net = sock_net(ctnl);
31f15875 1916 struct nf_conntrack_expect *exp;
c1d10adb 1917 struct nf_conntrack_tuple tuple;
96bcf938 1918 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
31f15875 1919 struct hlist_node *n, *next;
c1d10adb 1920 u_int8_t u3 = nfmsg->nfgen_family;
31f15875 1921 unsigned int i;
ef00f89f 1922 u16 zone;
c1d10adb
PNA
1923 int err;
1924
df6fb868 1925 if (cda[CTA_EXPECT_TUPLE]) {
c1d10adb 1926 /* delete a single expect by tuple */
ef00f89f
PM
1927 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1928 if (err < 0)
1929 return err;
1930
c1d10adb
PNA
1931 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1932 if (err < 0)
1933 return err;
1934
1935 /* bump usage count to 2 */
ef00f89f 1936 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
1937 if (!exp)
1938 return -ENOENT;
1939
df6fb868 1940 if (cda[CTA_EXPECT_ID]) {
77236b6e 1941 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 1942 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 1943 nf_ct_expect_put(exp);
c1d10adb
PNA
1944 return -ENOENT;
1945 }
1946 }
1947
1948 /* after list removal, usage count == 1 */
ebbf41df
PNA
1949 spin_lock_bh(&nf_conntrack_lock);
1950 if (del_timer(&exp->timeout)) {
1951 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid,
1952 nlmsg_report(nlh));
1953 nf_ct_expect_put(exp);
1954 }
1955 spin_unlock_bh(&nf_conntrack_lock);
601e68e1 1956 /* have to put what we 'get' above.
c1d10adb 1957 * after this line usage count == 0 */
6823645d 1958 nf_ct_expect_put(exp);
df6fb868
PM
1959 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1960 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
31f15875 1961 struct nf_conn_help *m_help;
c1d10adb
PNA
1962
1963 /* delete all expectations for this helper */
f8ba1aff 1964 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
1965 for (i = 0; i < nf_ct_expect_hsize; i++) {
1966 hlist_for_each_entry_safe(exp, n, next,
9592a5c0 1967 &net->ct.expect_hash[i],
31f15875
PM
1968 hnode) {
1969 m_help = nfct_help(exp->master);
794e6871
PM
1970 if (!strcmp(m_help->helper->name, name) &&
1971 del_timer(&exp->timeout)) {
ebbf41df
PNA
1972 nf_ct_unlink_expect_report(exp,
1973 NETLINK_CB(skb).pid,
1974 nlmsg_report(nlh));
31f15875
PM
1975 nf_ct_expect_put(exp);
1976 }
c1d10adb
PNA
1977 }
1978 }
f8ba1aff 1979 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1980 } else {
1981 /* This basically means we have to flush everything*/
f8ba1aff 1982 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
1983 for (i = 0; i < nf_ct_expect_hsize; i++) {
1984 hlist_for_each_entry_safe(exp, n, next,
9592a5c0 1985 &net->ct.expect_hash[i],
31f15875
PM
1986 hnode) {
1987 if (del_timer(&exp->timeout)) {
ebbf41df
PNA
1988 nf_ct_unlink_expect_report(exp,
1989 NETLINK_CB(skb).pid,
1990 nlmsg_report(nlh));
31f15875
PM
1991 nf_ct_expect_put(exp);
1992 }
c1d10adb
PNA
1993 }
1994 }
f8ba1aff 1995 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1996 }
1997
1998 return 0;
1999}
2000static int
39938324
PM
2001ctnetlink_change_expect(struct nf_conntrack_expect *x,
2002 const struct nlattr * const cda[])
c1d10adb
PNA
2003{
2004 return -EOPNOTSUPP;
2005}
2006
2007static int
ef00f89f
PM
2008ctnetlink_create_expect(struct net *net, u16 zone,
2009 const struct nlattr * const cda[],
9592a5c0 2010 u_int8_t u3,
39938324 2011 u32 pid, int report)
c1d10adb
PNA
2012{
2013 struct nf_conntrack_tuple tuple, mask, master_tuple;
2014 struct nf_conntrack_tuple_hash *h = NULL;
2015 struct nf_conntrack_expect *exp;
2016 struct nf_conn *ct;
dc808fe2 2017 struct nf_conn_help *help;
c1d10adb
PNA
2018 int err = 0;
2019
c1d10adb
PNA
2020 /* caller guarantees that those three CTA_EXPECT_* exist */
2021 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2022 if (err < 0)
2023 return err;
2024 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2025 if (err < 0)
2026 return err;
2027 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2028 if (err < 0)
2029 return err;
2030
2031 /* Look for master conntrack of this expectation */
ef00f89f 2032 h = nf_conntrack_find_get(net, zone, &master_tuple);
c1d10adb
PNA
2033 if (!h)
2034 return -ENOENT;
2035 ct = nf_ct_tuplehash_to_ctrack(h);
6823645d 2036 exp = nf_ct_expect_alloc(ct);
c1d10adb
PNA
2037 if (!exp) {
2038 err = -ENOMEM;
2039 goto out;
2040 }
bc01befd
PNA
2041 help = nfct_help(ct);
2042 if (!help) {
3d058d7b
PNA
2043 err = -EOPNOTSUPP;
2044 goto out;
2045 }
2046 if (test_bit(IPS_USERSPACE_HELPER_BIT, &ct->status)) {
bc01befd
PNA
2047 if (!cda[CTA_EXPECT_TIMEOUT]) {
2048 err = -EINVAL;
2049 goto out;
2050 }
2051 exp->timeout.expires =
2052 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
601e68e1 2053
bc01befd
PNA
2054 exp->flags = NF_CT_EXPECT_USERSPACE;
2055 if (cda[CTA_EXPECT_FLAGS]) {
2056 exp->flags |=
2057 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2058 }
2059 } else {
2060 if (cda[CTA_EXPECT_FLAGS]) {
2061 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2062 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2063 } else
2064 exp->flags = 0;
2065 }
601e68e1 2066
626ba8fb 2067 exp->class = 0;
c1d10adb 2068 exp->expectfn = NULL;
c1d10adb 2069 exp->master = ct;
9457d851 2070 exp->helper = NULL;
c1d10adb 2071 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
d4156e8c
PM
2072 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2073 exp->mask.src.u.all = mask.src.u.all;
c1d10adb 2074
19abb7b0 2075 err = nf_ct_expect_related_report(exp, pid, report);
6823645d 2076 nf_ct_expect_put(exp);
c1d10adb 2077
601e68e1 2078out:
c1d10adb
PNA
2079 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2080 return err;
2081}
2082
2083static int
2084ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2085 const struct nlmsghdr *nlh,
2086 const struct nlattr * const cda[])
c1d10adb 2087{
9592a5c0 2088 struct net *net = sock_net(ctnl);
c1d10adb
PNA
2089 struct nf_conntrack_tuple tuple;
2090 struct nf_conntrack_expect *exp;
96bcf938 2091 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 2092 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
2093 u16 zone;
2094 int err;
c1d10adb 2095
df6fb868
PM
2096 if (!cda[CTA_EXPECT_TUPLE]
2097 || !cda[CTA_EXPECT_MASK]
2098 || !cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
2099 return -EINVAL;
2100
ef00f89f
PM
2101 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2102 if (err < 0)
2103 return err;
2104
c1d10adb
PNA
2105 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2106 if (err < 0)
2107 return err;
2108
f8ba1aff 2109 spin_lock_bh(&nf_conntrack_lock);
ef00f89f 2110 exp = __nf_ct_expect_find(net, zone, &tuple);
c1d10adb
PNA
2111
2112 if (!exp) {
f8ba1aff 2113 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 2114 err = -ENOENT;
19abb7b0 2115 if (nlh->nlmsg_flags & NLM_F_CREATE) {
ef00f89f 2116 err = ctnetlink_create_expect(net, zone, cda,
19abb7b0
PNA
2117 u3,
2118 NETLINK_CB(skb).pid,
2119 nlmsg_report(nlh));
2120 }
c1d10adb
PNA
2121 return err;
2122 }
2123
2124 err = -EEXIST;
2125 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2126 err = ctnetlink_change_expect(exp, cda);
f8ba1aff 2127 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 2128
c1d10adb
PNA
2129 return err;
2130}
2131
2132#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
2133static struct nf_ct_event_notifier ctnl_notifier = {
2134 .fcn = ctnetlink_conntrack_event,
c1d10adb
PNA
2135};
2136
e34d5c1a
PNA
2137static struct nf_exp_event_notifier ctnl_notifier_exp = {
2138 .fcn = ctnetlink_expect_event,
c1d10adb
PNA
2139};
2140#endif
2141
7c8d4cb4 2142static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
c1d10adb 2143 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
f73e924c
PM
2144 .attr_count = CTA_MAX,
2145 .policy = ct_nla_policy },
c1d10adb 2146 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
2147 .attr_count = CTA_MAX,
2148 .policy = ct_nla_policy },
c1d10adb 2149 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
f73e924c
PM
2150 .attr_count = CTA_MAX,
2151 .policy = ct_nla_policy },
c1d10adb 2152 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
2153 .attr_count = CTA_MAX,
2154 .policy = ct_nla_policy },
c1d10adb
PNA
2155};
2156
7c8d4cb4 2157static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
c1d10adb 2158 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
f73e924c
PM
2159 .attr_count = CTA_EXPECT_MAX,
2160 .policy = exp_nla_policy },
c1d10adb 2161 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
f73e924c
PM
2162 .attr_count = CTA_EXPECT_MAX,
2163 .policy = exp_nla_policy },
c1d10adb 2164 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
f73e924c
PM
2165 .attr_count = CTA_EXPECT_MAX,
2166 .policy = exp_nla_policy },
c1d10adb
PNA
2167};
2168
7c8d4cb4 2169static const struct nfnetlink_subsystem ctnl_subsys = {
c1d10adb
PNA
2170 .name = "conntrack",
2171 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2172 .cb_count = IPCTNL_MSG_MAX,
2173 .cb = ctnl_cb,
2174};
2175
7c8d4cb4 2176static const struct nfnetlink_subsystem ctnl_exp_subsys = {
c1d10adb
PNA
2177 .name = "conntrack_expect",
2178 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2179 .cb_count = IPCTNL_MSG_EXP_MAX,
2180 .cb = ctnl_exp_cb,
2181};
2182
d2483dde 2183MODULE_ALIAS("ip_conntrack_netlink");
c1d10adb 2184MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
34f9a2e4 2185MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
c1d10adb 2186
70e9942f
PNA
2187static int __net_init ctnetlink_net_init(struct net *net)
2188{
2189#ifdef CONFIG_NF_CONNTRACK_EVENTS
2190 int ret;
2191
2192 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2193 if (ret < 0) {
2194 pr_err("ctnetlink_init: cannot register notifier.\n");
2195 goto err_out;
2196 }
2197
2198 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2199 if (ret < 0) {
2200 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2201 goto err_unreg_notifier;
2202 }
2203#endif
2204 return 0;
2205
2206#ifdef CONFIG_NF_CONNTRACK_EVENTS
2207err_unreg_notifier:
2208 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2209err_out:
2210 return ret;
2211#endif
2212}
2213
2214static void ctnetlink_net_exit(struct net *net)
2215{
2216#ifdef CONFIG_NF_CONNTRACK_EVENTS
2217 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2218 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2219#endif
2220}
2221
2222static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2223{
2224 struct net *net;
2225
2226 list_for_each_entry(net, net_exit_list, exit_list)
2227 ctnetlink_net_exit(net);
2228}
2229
2230static struct pernet_operations ctnetlink_net_ops = {
2231 .init = ctnetlink_net_init,
2232 .exit_batch = ctnetlink_net_exit_batch,
2233};
2234
c1d10adb
PNA
2235static int __init ctnetlink_init(void)
2236{
2237 int ret;
2238
654d0fbd 2239 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
c1d10adb
PNA
2240 ret = nfnetlink_subsys_register(&ctnl_subsys);
2241 if (ret < 0) {
654d0fbd 2242 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
c1d10adb
PNA
2243 goto err_out;
2244 }
2245
2246 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2247 if (ret < 0) {
654d0fbd 2248 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
c1d10adb
PNA
2249 goto err_unreg_subsys;
2250 }
2251
70e9942f
PNA
2252 if (register_pernet_subsys(&ctnetlink_net_ops)) {
2253 pr_err("ctnetlink_init: cannot register pernet operations\n");
c1d10adb
PNA
2254 goto err_unreg_exp_subsys;
2255 }
2256
c1d10adb
PNA
2257 return 0;
2258
c1d10adb
PNA
2259err_unreg_exp_subsys:
2260 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
c1d10adb
PNA
2261err_unreg_subsys:
2262 nfnetlink_subsys_unregister(&ctnl_subsys);
2263err_out:
2264 return ret;
2265}
2266
2267static void __exit ctnetlink_exit(void)
2268{
654d0fbd 2269 pr_info("ctnetlink: unregistering from nfnetlink.\n");
c1d10adb 2270
70e9942f 2271 unregister_pernet_subsys(&ctnetlink_net_ops);
c1d10adb
PNA
2272 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2273 nfnetlink_subsys_unregister(&ctnl_subsys);
c1d10adb
PNA
2274}
2275
2276module_init(ctnetlink_init);
2277module_exit(ctnetlink_exit);