netlink: Rename pid to portid to avoid confusion
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / nf_conntrack_netlink.c
CommitLineData
c1d10adb
PNA
1/* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
3 *
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
dc808fe2 5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
c1d10adb 6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
392025f8 7 * (C) 2005-2012 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>
c7232c99 48#include <net/netfilter/nf_nat_l4proto.h>
8c88f87c 49#include <net/netfilter/nf_nat_helper.h>
5b1158e9 50#endif
c1d10adb
PNA
51
52#include <linux/netfilter/nfnetlink.h>
53#include <linux/netfilter/nfnetlink_conntrack.h>
54
55MODULE_LICENSE("GPL");
56
dc808fe2 57static char __initdata version[] = "0.93";
c1d10adb 58
c1d10adb 59static inline int
601e68e1 60ctnetlink_dump_tuples_proto(struct sk_buff *skb,
1cde6436 61 const struct nf_conntrack_tuple *tuple,
605dcad6 62 struct nf_conntrack_l4proto *l4proto)
c1d10adb 63{
c1d10adb 64 int ret = 0;
df6fb868 65 struct nlattr *nest_parms;
c1d10adb 66
df6fb868
PM
67 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
68 if (!nest_parms)
69 goto nla_put_failure;
cc1eb431
DM
70 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
71 goto nla_put_failure;
c1d10adb 72
fdf70832
PM
73 if (likely(l4proto->tuple_to_nlattr))
74 ret = l4proto->tuple_to_nlattr(skb, tuple);
601e68e1 75
df6fb868 76 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
77
78 return ret;
79
df6fb868 80nla_put_failure:
c1d10adb
PNA
81 return -1;
82}
83
84static inline int
1cde6436
PNA
85ctnetlink_dump_tuples_ip(struct sk_buff *skb,
86 const struct nf_conntrack_tuple *tuple,
87 struct nf_conntrack_l3proto *l3proto)
c1d10adb 88{
c1d10adb 89 int ret = 0;
df6fb868
PM
90 struct nlattr *nest_parms;
91
92 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
93 if (!nest_parms)
94 goto nla_put_failure;
1cde6436 95
fdf70832
PM
96 if (likely(l3proto->tuple_to_nlattr))
97 ret = l3proto->tuple_to_nlattr(skb, tuple);
1cde6436 98
df6fb868 99 nla_nest_end(skb, nest_parms);
c1d10adb 100
1cde6436
PNA
101 return ret;
102
df6fb868 103nla_put_failure:
1cde6436
PNA
104 return -1;
105}
106
bb5cf80e 107static int
1cde6436
PNA
108ctnetlink_dump_tuples(struct sk_buff *skb,
109 const struct nf_conntrack_tuple *tuple)
110{
111 int ret;
112 struct nf_conntrack_l3proto *l3proto;
605dcad6 113 struct nf_conntrack_l4proto *l4proto;
1cde6436 114
3b988ece 115 rcu_read_lock();
528a3a6f 116 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1cde6436 117 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
c1d10adb 118
3b988ece
HS
119 if (ret >= 0) {
120 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
121 tuple->dst.protonum);
122 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
123 }
124 rcu_read_unlock();
c1d10adb 125 return ret;
c1d10adb
PNA
126}
127
128static inline int
129ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
130{
cc1eb431
DM
131 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
132 goto nla_put_failure;
c1d10adb
PNA
133 return 0;
134
df6fb868 135nla_put_failure:
c1d10adb
PNA
136 return -1;
137}
138
139static inline int
140ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
141{
c1216382 142 long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ;
c1d10adb 143
77236b6e 144 if (timeout < 0)
c1d10adb 145 timeout = 0;
601e68e1 146
cc1eb431
DM
147 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
148 goto nla_put_failure;
c1d10adb
PNA
149 return 0;
150
df6fb868 151nla_put_failure:
c1d10adb
PNA
152 return -1;
153}
154
155static inline int
440f0d58 156ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
c1d10adb 157{
5e8fbe2a 158 struct nf_conntrack_l4proto *l4proto;
df6fb868 159 struct nlattr *nest_proto;
c1d10adb
PNA
160 int ret;
161
528a3a6f
PNA
162 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
163 if (!l4proto->to_nlattr)
c1d10adb 164 return 0;
601e68e1 165
df6fb868
PM
166 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
167 if (!nest_proto)
168 goto nla_put_failure;
c1d10adb 169
fdf70832 170 ret = l4proto->to_nlattr(skb, nest_proto, ct);
c1d10adb 171
df6fb868 172 nla_nest_end(skb, nest_proto);
c1d10adb
PNA
173
174 return ret;
175
df6fb868 176nla_put_failure:
c1d10adb
PNA
177 return -1;
178}
179
180static inline int
181ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
182{
df6fb868 183 struct nlattr *nest_helper;
dc808fe2 184 const struct nf_conn_help *help = nfct_help(ct);
3c158f7f 185 struct nf_conntrack_helper *helper;
c1d10adb 186
3c158f7f 187 if (!help)
c1d10adb 188 return 0;
601e68e1 189
3c158f7f
PM
190 helper = rcu_dereference(help->helper);
191 if (!helper)
192 goto out;
193
df6fb868
PM
194 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
195 if (!nest_helper)
196 goto nla_put_failure;
cc1eb431
DM
197 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
198 goto nla_put_failure;
c1d10adb 199
fdf70832
PM
200 if (helper->to_nlattr)
201 helper->to_nlattr(skb, ct);
c1d10adb 202
df6fb868 203 nla_nest_end(skb, nest_helper);
3c158f7f 204out:
c1d10adb
PNA
205 return 0;
206
df6fb868 207nla_put_failure:
c1d10adb
PNA
208 return -1;
209}
210
bb5cf80e 211static int
80e60e67
PNA
212dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes,
213 enum ip_conntrack_dir dir)
c1d10adb
PNA
214{
215 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
df6fb868 216 struct nlattr *nest_count;
c1d10adb 217
df6fb868
PM
218 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
219 if (!nest_count)
220 goto nla_put_failure;
221
cc1eb431
DM
222 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)) ||
223 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes)))
224 goto nla_put_failure;
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
80e60e67
PNA
234static int
235ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
236 enum ip_conntrack_dir dir, int type)
237{
238 struct nf_conn_counter *acct;
239 u64 pkts, bytes;
240
241 acct = nf_conn_acct_find(ct);
242 if (!acct)
243 return 0;
244
245 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
246 pkts = atomic64_xchg(&acct[dir].packets, 0);
247 bytes = atomic64_xchg(&acct[dir].bytes, 0);
248 } else {
249 pkts = atomic64_read(&acct[dir].packets);
250 bytes = atomic64_read(&acct[dir].bytes);
251 }
252 return dump_counters(skb, pkts, bytes, dir);
253}
254
a992ca2a
PNA
255static int
256ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
257{
258 struct nlattr *nest_count;
259 const struct nf_conn_tstamp *tstamp;
260
261 tstamp = nf_conn_tstamp_find(ct);
262 if (!tstamp)
263 return 0;
264
265 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
266 if (!nest_count)
267 goto nla_put_failure;
268
cc1eb431
DM
269 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)) ||
270 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
271 cpu_to_be64(tstamp->stop))))
272 goto nla_put_failure;
a992ca2a
PNA
273 nla_nest_end(skb, nest_count);
274
275 return 0;
276
277nla_put_failure:
278 return -1;
279}
280
c1d10adb
PNA
281#ifdef CONFIG_NF_CONNTRACK_MARK
282static inline int
283ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
284{
cc1eb431
DM
285 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
286 goto nla_put_failure;
c1d10adb
PNA
287 return 0;
288
df6fb868 289nla_put_failure:
c1d10adb
PNA
290 return -1;
291}
292#else
293#define ctnetlink_dump_mark(a, b) (0)
294#endif
295
37fccd85
PNA
296#ifdef CONFIG_NF_CONNTRACK_SECMARK
297static inline int
1cc63249 298ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
37fccd85 299{
1cc63249
EP
300 struct nlattr *nest_secctx;
301 int len, ret;
302 char *secctx;
303
304 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
305 if (ret)
cba85b53 306 return 0;
1cc63249
EP
307
308 ret = -1;
309 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
310 if (!nest_secctx)
311 goto nla_put_failure;
37fccd85 312
cc1eb431
DM
313 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
314 goto nla_put_failure;
1cc63249
EP
315 nla_nest_end(skb, nest_secctx);
316
317 ret = 0;
37fccd85 318nla_put_failure:
1cc63249
EP
319 security_release_secctx(secctx, len);
320 return ret;
37fccd85
PNA
321}
322#else
1cc63249 323#define ctnetlink_dump_secctx(a, b) (0)
37fccd85
PNA
324#endif
325
0f417ce9
PNA
326#define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
327
328static inline int
329ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
330{
331 struct nlattr *nest_parms;
332
333 if (!(ct->status & IPS_EXPECTED))
334 return 0;
335
336 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
337 if (!nest_parms)
338 goto nla_put_failure;
339 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
340 goto nla_put_failure;
341 nla_nest_end(skb, nest_parms);
342
343 return 0;
344
345nla_put_failure:
346 return -1;
347}
348
13eae15a 349#ifdef CONFIG_NF_NAT_NEEDED
bb5cf80e 350static int
13eae15a
PNA
351dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
352{
13eae15a
PNA
353 struct nlattr *nest_parms;
354
355 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
356 if (!nest_parms)
357 goto nla_put_failure;
358
cc1eb431
DM
359 if (nla_put_be32(skb, CTA_NAT_SEQ_CORRECTION_POS,
360 htonl(natseq->correction_pos)) ||
361 nla_put_be32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
362 htonl(natseq->offset_before)) ||
363 nla_put_be32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
364 htonl(natseq->offset_after)))
365 goto nla_put_failure;
13eae15a
PNA
366
367 nla_nest_end(skb, nest_parms);
368
369 return 0;
370
371nla_put_failure:
372 return -1;
373}
374
375static inline int
376ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
377{
378 struct nf_nat_seq *natseq;
379 struct nf_conn_nat *nat = nfct_nat(ct);
380
381 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
382 return 0;
383
384 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
385 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
386 return -1;
387
388 natseq = &nat->seq[IP_CT_DIR_REPLY];
389 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
390 return -1;
391
392 return 0;
393}
394#else
395#define ctnetlink_dump_nat_seq_adj(a, b) (0)
396#endif
397
c1d10adb
PNA
398static inline int
399ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
400{
cc1eb431
DM
401 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
402 goto nla_put_failure;
c1d10adb
PNA
403 return 0;
404
df6fb868 405nla_put_failure:
c1d10adb
PNA
406 return -1;
407}
408
409static inline int
410ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
411{
cc1eb431
DM
412 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
413 goto nla_put_failure;
c1d10adb
PNA
414 return 0;
415
df6fb868 416nla_put_failure:
c1d10adb
PNA
417 return -1;
418}
419
c1d10adb 420static int
15e47304 421ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
80e60e67 422 struct nf_conn *ct)
c1d10adb
PNA
423{
424 struct nlmsghdr *nlh;
425 struct nfgenmsg *nfmsg;
df6fb868 426 struct nlattr *nest_parms;
15e47304 427 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
c1d10adb 428
80e60e67 429 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
15e47304 430 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
96bcf938
PNA
431 if (nlh == NULL)
432 goto nlmsg_failure;
c1d10adb 433
96bcf938 434 nfmsg = nlmsg_data(nlh);
5e8fbe2a 435 nfmsg->nfgen_family = nf_ct_l3num(ct);
c1d10adb
PNA
436 nfmsg->version = NFNETLINK_V0;
437 nfmsg->res_id = 0;
438
df6fb868
PM
439 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
440 if (!nest_parms)
441 goto nla_put_failure;
f2f3e38c 442 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
df6fb868
PM
443 goto nla_put_failure;
444 nla_nest_end(skb, nest_parms);
601e68e1 445
df6fb868
PM
446 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
447 if (!nest_parms)
448 goto nla_put_failure;
f2f3e38c 449 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
df6fb868
PM
450 goto nla_put_failure;
451 nla_nest_end(skb, nest_parms);
c1d10adb 452
cc1eb431
DM
453 if (nf_ct_zone(ct) &&
454 nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
455 goto nla_put_failure;
ef00f89f 456
c1d10adb
PNA
457 if (ctnetlink_dump_status(skb, ct) < 0 ||
458 ctnetlink_dump_timeout(skb, ct) < 0 ||
80e60e67
PNA
459 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 ||
460 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 ||
a992ca2a 461 ctnetlink_dump_timestamp(skb, ct) < 0 ||
c1d10adb
PNA
462 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
463 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
464 ctnetlink_dump_mark(skb, ct) < 0 ||
1cc63249 465 ctnetlink_dump_secctx(skb, ct) < 0 ||
c1d10adb 466 ctnetlink_dump_id(skb, ct) < 0 ||
13eae15a 467 ctnetlink_dump_use(skb, ct) < 0 ||
0f417ce9 468 ctnetlink_dump_master(skb, ct) < 0 ||
13eae15a 469 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
df6fb868 470 goto nla_put_failure;
c1d10adb 471
96bcf938 472 nlmsg_end(skb, nlh);
c1d10adb
PNA
473 return skb->len;
474
475nlmsg_failure:
df6fb868 476nla_put_failure:
96bcf938 477 nlmsg_cancel(skb, nlh);
c1d10adb
PNA
478 return -1;
479}
480
03b64f51
PNA
481static inline size_t
482ctnetlink_proto_size(const struct nf_conn *ct)
2732c4e4
HE
483{
484 struct nf_conntrack_l3proto *l3proto;
485 struct nf_conntrack_l4proto *l4proto;
03b64f51
PNA
486 size_t len = 0;
487
488 rcu_read_lock();
489 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
490 len += l3proto->nla_size;
491
492 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
493 len += l4proto->nla_size;
494 rcu_read_unlock();
495
496 return len;
497}
498
d26e6a02
JP
499static inline size_t
500ctnetlink_counters_size(const struct nf_conn *ct)
501{
502 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
503 return 0;
504 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
505 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
506 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
507 ;
508}
509
cba85b53
PNA
510static inline int
511ctnetlink_secctx_size(const struct nf_conn *ct)
1cc63249 512{
cba85b53
PNA
513#ifdef CONFIG_NF_CONNTRACK_SECMARK
514 int len, ret;
1cc63249 515
cba85b53
PNA
516 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
517 if (ret)
518 return 0;
1cc63249 519
cba85b53
PNA
520 return nla_total_size(0) /* CTA_SECCTX */
521 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
522#else
523 return 0;
1cc63249 524#endif
cba85b53 525}
1cc63249 526
a992ca2a
PNA
527static inline size_t
528ctnetlink_timestamp_size(const struct nf_conn *ct)
529{
530#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
531 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
532 return 0;
533 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
534#else
535 return 0;
536#endif
537}
538
03b64f51
PNA
539static inline size_t
540ctnetlink_nlmsg_size(const struct nf_conn *ct)
541{
542 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
543 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
544 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
545 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
546 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
547 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
548 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
d26e6a02 549 + ctnetlink_counters_size(ct)
a992ca2a 550 + ctnetlink_timestamp_size(ct)
03b64f51
PNA
551 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
552 + nla_total_size(0) /* CTA_PROTOINFO */
553 + nla_total_size(0) /* CTA_HELP */
554 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
cba85b53 555 + ctnetlink_secctx_size(ct)
d271e8bd 556#ifdef CONFIG_NF_NAT_NEEDED
03b64f51
PNA
557 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
558 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
d271e8bd
HE
559#endif
560#ifdef CONFIG_NF_CONNTRACK_MARK
03b64f51 561 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
d271e8bd 562#endif
03b64f51
PNA
563 + ctnetlink_proto_size(ct)
564 ;
2732c4e4
HE
565}
566
8e36c4b5 567#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
568static int
569ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
c1d10adb 570{
9592a5c0 571 struct net *net;
c1d10adb
PNA
572 struct nlmsghdr *nlh;
573 struct nfgenmsg *nfmsg;
df6fb868 574 struct nlattr *nest_parms;
19abb7b0 575 struct nf_conn *ct = item->ct;
c1d10adb
PNA
576 struct sk_buff *skb;
577 unsigned int type;
c1d10adb 578 unsigned int flags = 0, group;
dd7669a9 579 int err;
c1d10adb
PNA
580
581 /* ignore our fake conntrack entry */
5bfddbd4 582 if (nf_ct_is_untracked(ct))
e34d5c1a 583 return 0;
c1d10adb 584
a0891aa6 585 if (events & (1 << IPCT_DESTROY)) {
c1d10adb
PNA
586 type = IPCTNL_MSG_CT_DELETE;
587 group = NFNLGRP_CONNTRACK_DESTROY;
a0891aa6 588 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
c1d10adb
PNA
589 type = IPCTNL_MSG_CT_NEW;
590 flags = NLM_F_CREATE|NLM_F_EXCL;
c1d10adb 591 group = NFNLGRP_CONNTRACK_NEW;
17e6e4ea 592 } else if (events) {
c1d10adb
PNA
593 type = IPCTNL_MSG_CT_NEW;
594 group = NFNLGRP_CONNTRACK_UPDATE;
595 } else
e34d5c1a 596 return 0;
a2427692 597
9592a5c0
AD
598 net = nf_ct_net(ct);
599 if (!item->report && !nfnetlink_has_listeners(net, group))
e34d5c1a 600 return 0;
a2427692 601
03b64f51
PNA
602 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
603 if (skb == NULL)
150ace0d 604 goto errout;
c1d10adb 605
c1d10adb 606 type |= NFNL_SUBSYS_CTNETLINK << 8;
15e47304 607 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
96bcf938
PNA
608 if (nlh == NULL)
609 goto nlmsg_failure;
c1d10adb 610
96bcf938 611 nfmsg = nlmsg_data(nlh);
5e8fbe2a 612 nfmsg->nfgen_family = nf_ct_l3num(ct);
c1d10adb
PNA
613 nfmsg->version = NFNETLINK_V0;
614 nfmsg->res_id = 0;
615
528a3a6f 616 rcu_read_lock();
df6fb868
PM
617 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
618 if (!nest_parms)
619 goto nla_put_failure;
f2f3e38c 620 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
df6fb868
PM
621 goto nla_put_failure;
622 nla_nest_end(skb, nest_parms);
601e68e1 623
df6fb868
PM
624 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
625 if (!nest_parms)
626 goto nla_put_failure;
f2f3e38c 627 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
df6fb868
PM
628 goto nla_put_failure;
629 nla_nest_end(skb, nest_parms);
c1d10adb 630
cc1eb431
DM
631 if (nf_ct_zone(ct) &&
632 nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
633 goto nla_put_failure;
ef00f89f 634
1eedf699
EL
635 if (ctnetlink_dump_id(skb, ct) < 0)
636 goto nla_put_failure;
637
e57dce60
FH
638 if (ctnetlink_dump_status(skb, ct) < 0)
639 goto nla_put_failure;
640
a0891aa6 641 if (events & (1 << IPCT_DESTROY)) {
80e60e67
PNA
642 if (ctnetlink_dump_counters(skb, ct,
643 IP_CT_DIR_ORIGINAL, type) < 0 ||
644 ctnetlink_dump_counters(skb, ct,
645 IP_CT_DIR_REPLY, type) < 0 ||
a992ca2a 646 ctnetlink_dump_timestamp(skb, ct) < 0)
df6fb868 647 goto nla_put_failure;
7b621c1e 648 } else {
7b621c1e 649 if (ctnetlink_dump_timeout(skb, ct) < 0)
df6fb868 650 goto nla_put_failure;
7b621c1e 651
a0891aa6 652 if (events & (1 << IPCT_PROTOINFO)
7b621c1e 653 && ctnetlink_dump_protoinfo(skb, ct) < 0)
df6fb868 654 goto nla_put_failure;
7b621c1e 655
a0891aa6 656 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
7b621c1e 657 && ctnetlink_dump_helpinfo(skb, ct) < 0)
df6fb868 658 goto nla_put_failure;
7b621c1e 659
ff660c80 660#ifdef CONFIG_NF_CONNTRACK_SECMARK
a0891aa6 661 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
1cc63249 662 && ctnetlink_dump_secctx(skb, ct) < 0)
37fccd85 663 goto nla_put_failure;
ff660c80 664#endif
7b621c1e 665
a0891aa6 666 if (events & (1 << IPCT_RELATED) &&
0f417ce9
PNA
667 ctnetlink_dump_master(skb, ct) < 0)
668 goto nla_put_failure;
669
a0891aa6 670 if (events & (1 << IPCT_NATSEQADJ) &&
13eae15a
PNA
671 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
672 goto nla_put_failure;
7b621c1e 673 }
b9a37e0c 674
a83099a6 675#ifdef CONFIG_NF_CONNTRACK_MARK
a0891aa6 676 if ((events & (1 << IPCT_MARK) || ct->mark)
a83099a6
EL
677 && ctnetlink_dump_mark(skb, ct) < 0)
678 goto nla_put_failure;
679#endif
528a3a6f 680 rcu_read_unlock();
a83099a6 681
96bcf938 682 nlmsg_end(skb, nlh);
15e47304 683 err = nfnetlink_send(skb, net, item->portid, group, item->report,
cd8c20b6 684 GFP_ATOMIC);
dd7669a9
PNA
685 if (err == -ENOBUFS || err == -EAGAIN)
686 return -ENOBUFS;
687
e34d5c1a 688 return 0;
c1d10adb 689
df6fb868 690nla_put_failure:
528a3a6f 691 rcu_read_unlock();
96bcf938 692 nlmsg_cancel(skb, nlh);
528a3a6f 693nlmsg_failure:
c1d10adb 694 kfree_skb(skb);
150ace0d 695errout:
37b7ef72
PNA
696 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
697 return -ENOBUFS;
698
e34d5c1a 699 return 0;
c1d10adb
PNA
700}
701#endif /* CONFIG_NF_CONNTRACK_EVENTS */
702
703static int ctnetlink_done(struct netlink_callback *cb)
704{
89f2e218
PM
705 if (cb->args[1])
706 nf_ct_put((struct nf_conn *)cb->args[1]);
0f298a28
PNA
707 if (cb->data)
708 kfree(cb->data);
c1d10adb
PNA
709 return 0;
710}
711
0f298a28
PNA
712struct ctnetlink_dump_filter {
713 struct {
714 u_int32_t val;
715 u_int32_t mask;
716 } mark;
717};
718
c1d10adb
PNA
719static int
720ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
721{
9592a5c0 722 struct net *net = sock_net(skb->sk);
89f2e218 723 struct nf_conn *ct, *last;
c1d10adb 724 struct nf_conntrack_tuple_hash *h;
ea781f19 725 struct hlist_nulls_node *n;
96bcf938 726 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
87711cb8 727 u_int8_t l3proto = nfmsg->nfgen_family;
3b988ece 728 int res;
0f298a28
PNA
729#ifdef CONFIG_NF_CONNTRACK_MARK
730 const struct ctnetlink_dump_filter *filter = cb->data;
731#endif
3b988ece 732
13ee6ac5 733 spin_lock_bh(&nf_conntrack_lock);
d205dc40 734 last = (struct nf_conn *)cb->args[1];
9ab99d5a 735 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
89f2e218 736restart:
13ee6ac5 737 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
ea781f19 738 hnnode) {
5b1158e9 739 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
c1d10adb
PNA
740 continue;
741 ct = nf_ct_tuplehash_to_ctrack(h);
87711cb8
PNA
742 /* Dump entries of a given L3 protocol number.
743 * If it is not specified, ie. l3proto == 0,
744 * then dump everything. */
5e8fbe2a 745 if (l3proto && nf_ct_l3num(ct) != l3proto)
13ee6ac5 746 continue;
d205dc40
PM
747 if (cb->args[1]) {
748 if (ct != last)
13ee6ac5 749 continue;
d205dc40 750 cb->args[1] = 0;
89f2e218 751 }
0f298a28
PNA
752#ifdef CONFIG_NF_CONNTRACK_MARK
753 if (filter && !((ct->mark & filter->mark.mask) ==
754 filter->mark.val)) {
755 continue;
756 }
757#endif
3b988ece
HS
758 rcu_read_lock();
759 res =
15e47304 760 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
3b988ece
HS
761 cb->nlh->nlmsg_seq,
762 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
763 ct);
764 rcu_read_unlock();
765 if (res < 0) {
c71caf41 766 nf_conntrack_get(&ct->ct_general);
89f2e218 767 cb->args[1] = (unsigned long)ct;
c1d10adb 768 goto out;
89f2e218
PM
769 }
770 }
d205dc40 771 if (cb->args[1]) {
89f2e218
PM
772 cb->args[1] = 0;
773 goto restart;
c1d10adb
PNA
774 }
775 }
89f2e218 776out:
13ee6ac5 777 spin_unlock_bh(&nf_conntrack_lock);
d205dc40
PM
778 if (last)
779 nf_ct_put(last);
c1d10adb 780
c1d10adb
PNA
781 return skb->len;
782}
783
c1d10adb 784static inline int
df6fb868 785ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
c1d10adb 786{
df6fb868 787 struct nlattr *tb[CTA_IP_MAX+1];
c1d10adb
PNA
788 struct nf_conntrack_l3proto *l3proto;
789 int ret = 0;
790
df6fb868 791 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
c1d10adb 792
cd91566e
FW
793 rcu_read_lock();
794 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
c1d10adb 795
f73e924c
PM
796 if (likely(l3proto->nlattr_to_tuple)) {
797 ret = nla_validate_nested(attr, CTA_IP_MAX,
798 l3proto->nla_policy);
799 if (ret == 0)
800 ret = l3proto->nlattr_to_tuple(tb, tuple);
801 }
c1d10adb 802
cd91566e 803 rcu_read_unlock();
c1d10adb 804
c1d10adb
PNA
805 return ret;
806}
807
f73e924c
PM
808static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
809 [CTA_PROTO_NUM] = { .type = NLA_U8 },
c1d10adb
PNA
810};
811
812static inline int
df6fb868 813ctnetlink_parse_tuple_proto(struct nlattr *attr,
c1d10adb
PNA
814 struct nf_conntrack_tuple *tuple)
815{
df6fb868 816 struct nlattr *tb[CTA_PROTO_MAX+1];
605dcad6 817 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
818 int ret = 0;
819
f73e924c
PM
820 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
821 if (ret < 0)
822 return ret;
c1d10adb 823
df6fb868 824 if (!tb[CTA_PROTO_NUM])
c1d10adb 825 return -EINVAL;
77236b6e 826 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
c1d10adb 827
cd91566e
FW
828 rcu_read_lock();
829 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
c1d10adb 830
f73e924c
PM
831 if (likely(l4proto->nlattr_to_tuple)) {
832 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
833 l4proto->nla_policy);
834 if (ret == 0)
835 ret = l4proto->nlattr_to_tuple(tb, tuple);
836 }
c1d10adb 837
cd91566e 838 rcu_read_unlock();
601e68e1 839
c1d10adb
PNA
840 return ret;
841}
842
d0b0268f
PM
843static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
844 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
845 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
846};
847
bb5cf80e 848static int
39938324
PM
849ctnetlink_parse_tuple(const struct nlattr * const cda[],
850 struct nf_conntrack_tuple *tuple,
a00f1f36 851 enum ctattr_type type, u_int8_t l3num)
c1d10adb 852{
df6fb868 853 struct nlattr *tb[CTA_TUPLE_MAX+1];
c1d10adb
PNA
854 int err;
855
c1d10adb
PNA
856 memset(tuple, 0, sizeof(*tuple));
857
d0b0268f 858 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
c1d10adb 859
df6fb868 860 if (!tb[CTA_TUPLE_IP])
c1d10adb
PNA
861 return -EINVAL;
862
863 tuple->src.l3num = l3num;
864
df6fb868 865 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
c1d10adb
PNA
866 if (err < 0)
867 return err;
868
df6fb868 869 if (!tb[CTA_TUPLE_PROTO])
c1d10adb
PNA
870 return -EINVAL;
871
df6fb868 872 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
c1d10adb
PNA
873 if (err < 0)
874 return err;
875
876 /* orig and expect tuples get DIR_ORIGINAL */
877 if (type == CTA_TUPLE_REPLY)
878 tuple->dst.dir = IP_CT_DIR_REPLY;
879 else
880 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
881
c1d10adb
PNA
882 return 0;
883}
884
ef00f89f
PM
885static int
886ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
887{
888 if (attr)
889#ifdef CONFIG_NF_CONNTRACK_ZONES
890 *zone = ntohs(nla_get_be16(attr));
891#else
892 return -EOPNOTSUPP;
893#endif
894 else
895 *zone = 0;
896
897 return 0;
898}
899
d0b0268f
PM
900static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
901 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING },
902};
903
c1d10adb 904static inline int
ae243bee
PNA
905ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
906 struct nlattr **helpinfo)
c1d10adb 907{
df6fb868 908 struct nlattr *tb[CTA_HELP_MAX+1];
c1d10adb 909
d0b0268f 910 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
c1d10adb 911
df6fb868 912 if (!tb[CTA_HELP_NAME])
c1d10adb
PNA
913 return -EINVAL;
914
df6fb868 915 *helper_name = nla_data(tb[CTA_HELP_NAME]);
c1d10adb 916
ae243bee
PNA
917 if (tb[CTA_HELP_INFO])
918 *helpinfo = tb[CTA_HELP_INFO];
919
c1d10adb
PNA
920 return 0;
921}
922
f73e924c 923static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
d0b0268f
PM
924 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
925 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
f73e924c 926 [CTA_STATUS] = { .type = NLA_U32 },
d0b0268f
PM
927 [CTA_PROTOINFO] = { .type = NLA_NESTED },
928 [CTA_HELP] = { .type = NLA_NESTED },
929 [CTA_NAT_SRC] = { .type = NLA_NESTED },
f73e924c
PM
930 [CTA_TIMEOUT] = { .type = NLA_U32 },
931 [CTA_MARK] = { .type = NLA_U32 },
f73e924c 932 [CTA_ID] = { .type = NLA_U32 },
d0b0268f
PM
933 [CTA_NAT_DST] = { .type = NLA_NESTED },
934 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
ef00f89f 935 [CTA_ZONE] = { .type = NLA_U16 },
0f298a28 936 [CTA_MARK_MASK] = { .type = NLA_U32 },
c1d10adb
PNA
937};
938
939static int
601e68e1 940ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
941 const struct nlmsghdr *nlh,
942 const struct nlattr * const cda[])
c1d10adb 943{
9592a5c0 944 struct net *net = sock_net(ctnl);
c1d10adb
PNA
945 struct nf_conntrack_tuple_hash *h;
946 struct nf_conntrack_tuple tuple;
947 struct nf_conn *ct;
96bcf938 948 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 949 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
950 u16 zone;
951 int err;
952
953 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
954 if (err < 0)
955 return err;
c1d10adb 956
df6fb868 957 if (cda[CTA_TUPLE_ORIG])
c1d10adb 958 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
df6fb868 959 else if (cda[CTA_TUPLE_REPLY])
c1d10adb
PNA
960 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
961 else {
962 /* Flush the whole table */
9592a5c0 963 nf_conntrack_flush_report(net,
15e47304 964 NETLINK_CB(skb).portid,
274d383b 965 nlmsg_report(nlh));
c1d10adb
PNA
966 return 0;
967 }
968
969 if (err < 0)
970 return err;
971
ef00f89f 972 h = nf_conntrack_find_get(net, zone, &tuple);
9ea8cfd6 973 if (!h)
c1d10adb 974 return -ENOENT;
c1d10adb
PNA
975
976 ct = nf_ct_tuplehash_to_ctrack(h);
601e68e1 977
df6fb868 978 if (cda[CTA_ID]) {
77236b6e 979 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
7f85f914 980 if (id != (u32)(unsigned long)ct) {
c1d10adb
PNA
981 nf_ct_put(ct);
982 return -ENOENT;
983 }
601e68e1 984 }
c1d10adb 985
a16a1647
PNA
986 if (del_timer(&ct->timeout)) {
987 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
15e47304 988 NETLINK_CB(skb).portid,
a16a1647
PNA
989 nlmsg_report(nlh)) < 0) {
990 nf_ct_delete_from_lists(ct);
991 /* we failed to report the event, try later */
992 nf_ct_insert_dying_list(ct);
993 nf_ct_put(ct);
994 return 0;
995 }
996 /* death_by_timeout would report the event again */
997 set_bit(IPS_DYING_BIT, &ct->status);
dd7669a9 998 nf_ct_delete_from_lists(ct);
dd7669a9 999 nf_ct_put(ct);
dd7669a9 1000 }
c1d10adb 1001 nf_ct_put(ct);
c1d10adb
PNA
1002
1003 return 0;
1004}
1005
1006static int
601e68e1 1007ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1008 const struct nlmsghdr *nlh,
1009 const struct nlattr * const cda[])
c1d10adb 1010{
9592a5c0 1011 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1012 struct nf_conntrack_tuple_hash *h;
1013 struct nf_conntrack_tuple tuple;
1014 struct nf_conn *ct;
1015 struct sk_buff *skb2 = NULL;
96bcf938 1016 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 1017 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1018 u16 zone;
1019 int err;
c1d10adb 1020
80d326fa
PNA
1021 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1022 struct netlink_dump_control c = {
1023 .dump = ctnetlink_dump_table,
1024 .done = ctnetlink_done,
1025 };
0f298a28
PNA
1026#ifdef CONFIG_NF_CONNTRACK_MARK
1027 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1028 struct ctnetlink_dump_filter *filter;
1029
1030 filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1031 GFP_ATOMIC);
1032 if (filter == NULL)
1033 return -ENOMEM;
1034
1035 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1036 filter->mark.mask =
1037 ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1038 c.data = filter;
1039 }
1040#endif
80d326fa
PNA
1041 return netlink_dump_start(ctnl, skb, nlh, &c);
1042 }
c1d10adb 1043
ef00f89f
PM
1044 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1045 if (err < 0)
1046 return err;
1047
df6fb868 1048 if (cda[CTA_TUPLE_ORIG])
c1d10adb 1049 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
df6fb868 1050 else if (cda[CTA_TUPLE_REPLY])
c1d10adb
PNA
1051 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1052 else
1053 return -EINVAL;
1054
1055 if (err < 0)
1056 return err;
1057
ef00f89f 1058 h = nf_conntrack_find_get(net, zone, &tuple);
9ea8cfd6 1059 if (!h)
c1d10adb 1060 return -ENOENT;
9ea8cfd6 1061
c1d10adb
PNA
1062 ct = nf_ct_tuplehash_to_ctrack(h);
1063
1064 err = -ENOMEM;
96bcf938
PNA
1065 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1066 if (skb2 == NULL) {
c1d10adb
PNA
1067 nf_ct_put(ct);
1068 return -ENOMEM;
1069 }
c1d10adb 1070
528a3a6f 1071 rcu_read_lock();
15e47304 1072 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
80e60e67 1073 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
528a3a6f 1074 rcu_read_unlock();
c1d10adb
PNA
1075 nf_ct_put(ct);
1076 if (err <= 0)
1077 goto free;
1078
15e47304 1079 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
c1d10adb
PNA
1080 if (err < 0)
1081 goto out;
1082
c1d10adb
PNA
1083 return 0;
1084
1085free:
1086 kfree_skb(skb2);
1087out:
f31e8d49
PNA
1088 /* this avoids a loop in nfnetlink. */
1089 return err == -EAGAIN ? -ENOBUFS : err;
c1d10adb
PNA
1090}
1091
67671841 1092#ifdef CONFIG_NF_NAT_NEEDED
e6a7d3c0
PNA
1093static int
1094ctnetlink_parse_nat_setup(struct nf_conn *ct,
1095 enum nf_nat_manip_type manip,
39938324 1096 const struct nlattr *attr)
e6a7d3c0
PNA
1097{
1098 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
c7232c99 1099 int err;
e6a7d3c0
PNA
1100
1101 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1102 if (!parse_nat_setup) {
95a5afca 1103#ifdef CONFIG_MODULES
e6a7d3c0
PNA
1104 rcu_read_unlock();
1105 nfnl_unlock();
c7232c99 1106 if (request_module("nf-nat") < 0) {
e6a7d3c0
PNA
1107 nfnl_lock();
1108 rcu_read_lock();
1109 return -EOPNOTSUPP;
1110 }
1111 nfnl_lock();
1112 rcu_read_lock();
1113 if (nfnetlink_parse_nat_setup_hook)
1114 return -EAGAIN;
1115#endif
1116 return -EOPNOTSUPP;
1117 }
1118
c7232c99
PM
1119 err = parse_nat_setup(ct, manip, attr);
1120 if (err == -EAGAIN) {
1121#ifdef CONFIG_MODULES
1122 rcu_read_unlock();
1123 spin_unlock_bh(&nf_conntrack_lock);
1124 nfnl_unlock();
1125 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1126 nfnl_lock();
1127 spin_lock_bh(&nf_conntrack_lock);
1128 rcu_read_lock();
1129 return -EOPNOTSUPP;
1130 }
1131 nfnl_lock();
1132 spin_lock_bh(&nf_conntrack_lock);
1133 rcu_read_lock();
1134#else
1135 err = -EOPNOTSUPP;
1136#endif
1137 }
1138 return err;
e6a7d3c0 1139}
67671841 1140#endif
e6a7d3c0 1141
bb5cf80e 1142static int
39938324 1143ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
1144{
1145 unsigned long d;
77236b6e 1146 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
c1d10adb
PNA
1147 d = ct->status ^ status;
1148
1149 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1150 /* unchangeable */
0adf9d67 1151 return -EBUSY;
601e68e1 1152
c1d10adb
PNA
1153 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1154 /* SEEN_REPLY bit can only be set */
0adf9d67 1155 return -EBUSY;
601e68e1 1156
c1d10adb
PNA
1157 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1158 /* ASSURED bit can only be set */
0adf9d67 1159 return -EBUSY;
c1d10adb 1160
c1d10adb
PNA
1161 /* Be careful here, modifying NAT bits can screw up things,
1162 * so don't let users modify them directly if they don't pass
5b1158e9 1163 * nf_nat_range. */
c1d10adb
PNA
1164 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1165 return 0;
1166}
1167
e6a7d3c0 1168static int
39938324 1169ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
e6a7d3c0
PNA
1170{
1171#ifdef CONFIG_NF_NAT_NEEDED
1172 int ret;
1173
1174 if (cda[CTA_NAT_DST]) {
1175 ret = ctnetlink_parse_nat_setup(ct,
cbc9f2f4 1176 NF_NAT_MANIP_DST,
e6a7d3c0
PNA
1177 cda[CTA_NAT_DST]);
1178 if (ret < 0)
1179 return ret;
1180 }
1181 if (cda[CTA_NAT_SRC]) {
1182 ret = ctnetlink_parse_nat_setup(ct,
cbc9f2f4 1183 NF_NAT_MANIP_SRC,
e6a7d3c0
PNA
1184 cda[CTA_NAT_SRC]);
1185 if (ret < 0)
1186 return ret;
1187 }
1188 return 0;
1189#else
1190 return -EOPNOTSUPP;
1191#endif
1192}
c1d10adb
PNA
1193
1194static inline int
39938324 1195ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
1196{
1197 struct nf_conntrack_helper *helper;
dc808fe2 1198 struct nf_conn_help *help = nfct_help(ct);
29fe1b48 1199 char *helpname = NULL;
ae243bee 1200 struct nlattr *helpinfo = NULL;
c1d10adb
PNA
1201 int err;
1202
c1d10adb
PNA
1203 /* don't change helper of sibling connections */
1204 if (ct->master)
0adf9d67 1205 return -EBUSY;
c1d10adb 1206
ae243bee 1207 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
c1d10adb
PNA
1208 if (err < 0)
1209 return err;
1210
df293bbb
YK
1211 if (!strcmp(helpname, "")) {
1212 if (help && help->helper) {
c1d10adb
PNA
1213 /* we had a helper before ... */
1214 nf_ct_remove_expectations(ct);
a9b3cd7f 1215 RCU_INIT_POINTER(help->helper, NULL);
c1d10adb 1216 }
df293bbb
YK
1217
1218 return 0;
c1d10adb 1219 }
601e68e1 1220
794e6871
PM
1221 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1222 nf_ct_protonum(ct));
226c0c0e
PNA
1223 if (helper == NULL) {
1224#ifdef CONFIG_MODULES
1225 spin_unlock_bh(&nf_conntrack_lock);
1226
1227 if (request_module("nfct-helper-%s", helpname) < 0) {
1228 spin_lock_bh(&nf_conntrack_lock);
1229 return -EOPNOTSUPP;
1230 }
1231
1232 spin_lock_bh(&nf_conntrack_lock);
794e6871
PM
1233 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1234 nf_ct_protonum(ct));
226c0c0e
PNA
1235 if (helper)
1236 return -EAGAIN;
1237#endif
0adf9d67 1238 return -EOPNOTSUPP;
226c0c0e 1239 }
df293bbb 1240
ceceae1b 1241 if (help) {
ae243bee
PNA
1242 if (help->helper == helper) {
1243 /* update private helper data if allowed. */
1244 if (helper->from_nlattr && helpinfo)
1245 helper->from_nlattr(helpinfo, ct);
ceceae1b 1246 return 0;
fd7462de 1247 } else
ceceae1b 1248 return -EBUSY;
ceceae1b 1249 }
df293bbb 1250
fd7462de
PNA
1251 /* we cannot set a helper for an existing conntrack */
1252 return -EOPNOTSUPP;
c1d10adb
PNA
1253}
1254
1255static inline int
39938324 1256ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1257{
77236b6e 1258 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
601e68e1 1259
c1d10adb
PNA
1260 if (!del_timer(&ct->timeout))
1261 return -ETIME;
1262
1263 ct->timeout.expires = jiffies + timeout * HZ;
1264 add_timer(&ct->timeout);
1265
1266 return 0;
1267}
1268
d0b0268f
PM
1269static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1270 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1271 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1272 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1273};
1274
c1d10adb 1275static inline int
39938324 1276ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1277{
39938324
PM
1278 const struct nlattr *attr = cda[CTA_PROTOINFO];
1279 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
605dcad6 1280 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
1281 int err = 0;
1282
d0b0268f 1283 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
c1d10adb 1284
cd91566e
FW
1285 rcu_read_lock();
1286 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
fdf70832
PM
1287 if (l4proto->from_nlattr)
1288 err = l4proto->from_nlattr(tb, ct);
cd91566e 1289 rcu_read_unlock();
c1d10adb
PNA
1290
1291 return err;
1292}
1293
13eae15a 1294#ifdef CONFIG_NF_NAT_NEEDED
d0b0268f
PM
1295static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1296 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1297 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1298 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1299};
1300
13eae15a 1301static inline int
39938324 1302change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
13eae15a
PNA
1303{
1304 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1305
d0b0268f 1306 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
13eae15a
PNA
1307
1308 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1309 return -EINVAL;
1310
1311 natseq->correction_pos =
77236b6e 1312 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
13eae15a
PNA
1313
1314 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1315 return -EINVAL;
1316
1317 natseq->offset_before =
77236b6e 1318 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
13eae15a
PNA
1319
1320 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1321 return -EINVAL;
1322
1323 natseq->offset_after =
77236b6e 1324 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
13eae15a
PNA
1325
1326 return 0;
1327}
1328
1329static int
39938324
PM
1330ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1331 const struct nlattr * const cda[])
13eae15a
PNA
1332{
1333 int ret = 0;
1334 struct nf_conn_nat *nat = nfct_nat(ct);
1335
1336 if (!nat)
1337 return 0;
1338
1339 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1340 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1341 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1342 if (ret < 0)
1343 return ret;
1344
1345 ct->status |= IPS_SEQ_ADJUST;
1346 }
1347
1348 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1349 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1350 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1351 if (ret < 0)
1352 return ret;
1353
1354 ct->status |= IPS_SEQ_ADJUST;
1355 }
1356
1357 return 0;
1358}
1359#endif
1360
c1d10adb 1361static int
39938324
PM
1362ctnetlink_change_conntrack(struct nf_conn *ct,
1363 const struct nlattr * const cda[])
c1d10adb
PNA
1364{
1365 int err;
1366
e098360f
PNA
1367 /* only allow NAT changes and master assignation for new conntracks */
1368 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1369 return -EOPNOTSUPP;
1370
df6fb868 1371 if (cda[CTA_HELP]) {
c1d10adb
PNA
1372 err = ctnetlink_change_helper(ct, cda);
1373 if (err < 0)
1374 return err;
1375 }
1376
df6fb868 1377 if (cda[CTA_TIMEOUT]) {
c1d10adb
PNA
1378 err = ctnetlink_change_timeout(ct, cda);
1379 if (err < 0)
1380 return err;
1381 }
1382
df6fb868 1383 if (cda[CTA_STATUS]) {
c1d10adb
PNA
1384 err = ctnetlink_change_status(ct, cda);
1385 if (err < 0)
1386 return err;
1387 }
1388
df6fb868 1389 if (cda[CTA_PROTOINFO]) {
c1d10adb
PNA
1390 err = ctnetlink_change_protoinfo(ct, cda);
1391 if (err < 0)
1392 return err;
1393 }
1394
bcd1e830 1395#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1396 if (cda[CTA_MARK])
77236b6e 1397 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1398#endif
1399
13eae15a
PNA
1400#ifdef CONFIG_NF_NAT_NEEDED
1401 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1402 err = ctnetlink_change_nat_seq_adj(ct, cda);
1403 if (err < 0)
1404 return err;
1405 }
1406#endif
1407
c1d10adb
PNA
1408 return 0;
1409}
1410
f0a3c086 1411static struct nf_conn *
ef00f89f 1412ctnetlink_create_conntrack(struct net *net, u16 zone,
9592a5c0 1413 const struct nlattr * const cda[],
c1d10adb 1414 struct nf_conntrack_tuple *otuple,
5faa1f4c 1415 struct nf_conntrack_tuple *rtuple,
7ec47496 1416 u8 u3)
c1d10adb
PNA
1417{
1418 struct nf_conn *ct;
1419 int err = -EINVAL;
ceceae1b 1420 struct nf_conntrack_helper *helper;
315c34da 1421 struct nf_conn_tstamp *tstamp;
c1d10adb 1422
ef00f89f 1423 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
cd7fcbf1 1424 if (IS_ERR(ct))
f0a3c086 1425 return ERR_PTR(-ENOMEM);
c1d10adb 1426
df6fb868 1427 if (!cda[CTA_TIMEOUT])
0f5b3e85 1428 goto err1;
77236b6e 1429 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
c1d10adb
PNA
1430
1431 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
c1d10adb 1432
1575e7ea 1433 rcu_read_lock();
226c0c0e 1434 if (cda[CTA_HELP]) {
29fe1b48 1435 char *helpname = NULL;
ae243bee
PNA
1436 struct nlattr *helpinfo = NULL;
1437
1438 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
0f5b3e85
PM
1439 if (err < 0)
1440 goto err2;
226c0c0e 1441
794e6871
PM
1442 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1443 nf_ct_protonum(ct));
226c0c0e
PNA
1444 if (helper == NULL) {
1445 rcu_read_unlock();
1446#ifdef CONFIG_MODULES
1447 if (request_module("nfct-helper-%s", helpname) < 0) {
1448 err = -EOPNOTSUPP;
0f5b3e85 1449 goto err1;
226c0c0e
PNA
1450 }
1451
1452 rcu_read_lock();
794e6871
PM
1453 helper = __nf_conntrack_helper_find(helpname,
1454 nf_ct_l3num(ct),
1455 nf_ct_protonum(ct));
226c0c0e 1456 if (helper) {
226c0c0e 1457 err = -EAGAIN;
0f5b3e85 1458 goto err2;
226c0c0e
PNA
1459 }
1460 rcu_read_unlock();
1461#endif
1462 err = -EOPNOTSUPP;
0f5b3e85 1463 goto err1;
226c0c0e
PNA
1464 } else {
1465 struct nf_conn_help *help;
1466
1afc5679 1467 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
226c0c0e 1468 if (help == NULL) {
226c0c0e 1469 err = -ENOMEM;
0f5b3e85 1470 goto err2;
226c0c0e 1471 }
ae243bee
PNA
1472 /* set private helper data if allowed. */
1473 if (helper->from_nlattr && helpinfo)
1474 helper->from_nlattr(helpinfo, ct);
226c0c0e
PNA
1475
1476 /* not in hash table yet so not strictly necessary */
a9b3cd7f 1477 RCU_INIT_POINTER(help->helper, helper);
226c0c0e
PNA
1478 }
1479 } else {
1480 /* try an implicit helper assignation */
b2a15a60 1481 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
0f5b3e85
PM
1482 if (err < 0)
1483 goto err2;
1575e7ea
PNA
1484 }
1485
a88e22ad
PNA
1486 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1487 err = ctnetlink_change_nat(ct, cda);
0f5b3e85
PM
1488 if (err < 0)
1489 goto err2;
e6a7d3c0
PNA
1490 }
1491
a88e22ad 1492 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
a992ca2a 1493 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
a88e22ad
PNA
1494 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1495 /* we must add conntrack extensions before confirmation. */
1496 ct->status |= IPS_CONFIRMED;
1497
1498 if (cda[CTA_STATUS]) {
1499 err = ctnetlink_change_status(ct, cda);
0f5b3e85
PM
1500 if (err < 0)
1501 goto err2;
bbb3357d 1502 }
c1d10adb 1503
c969aa7d
PNA
1504#ifdef CONFIG_NF_NAT_NEEDED
1505 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1506 err = ctnetlink_change_nat_seq_adj(ct, cda);
0f5b3e85
PM
1507 if (err < 0)
1508 goto err2;
c969aa7d
PNA
1509 }
1510#endif
1511
e5fc9e7a 1512 memset(&ct->proto, 0, sizeof(ct->proto));
df6fb868 1513 if (cda[CTA_PROTOINFO]) {
c1d10adb 1514 err = ctnetlink_change_protoinfo(ct, cda);
0f5b3e85
PM
1515 if (err < 0)
1516 goto err2;
c1d10adb
PNA
1517 }
1518
bcd1e830 1519#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1520 if (cda[CTA_MARK])
77236b6e 1521 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1522#endif
1523
5faa1f4c 1524 /* setup master conntrack: this is a confirmed expectation */
7ec47496
PNA
1525 if (cda[CTA_TUPLE_MASTER]) {
1526 struct nf_conntrack_tuple master;
1527 struct nf_conntrack_tuple_hash *master_h;
1528 struct nf_conn *master_ct;
1529
1530 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1531 if (err < 0)
0f5b3e85 1532 goto err2;
7ec47496 1533
ef00f89f 1534 master_h = nf_conntrack_find_get(net, zone, &master);
7ec47496
PNA
1535 if (master_h == NULL) {
1536 err = -ENOENT;
0f5b3e85 1537 goto err2;
7ec47496
PNA
1538 }
1539 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
f2a89004 1540 __set_bit(IPS_EXPECTED_BIT, &ct->status);
5faa1f4c 1541 ct->master = master_ct;
f2a89004 1542 }
315c34da
PNA
1543 tstamp = nf_conn_tstamp_find(ct);
1544 if (tstamp)
1545 tstamp->start = ktime_to_ns(ktime_get_real());
5faa1f4c 1546
7d367e06
JK
1547 err = nf_conntrack_hash_check_insert(ct);
1548 if (err < 0)
1549 goto err2;
1550
58a3c9bb 1551 rcu_read_unlock();
dafc741c 1552
f0a3c086 1553 return ct;
c1d10adb 1554
0f5b3e85
PM
1555err2:
1556 rcu_read_unlock();
1557err1:
c1d10adb 1558 nf_conntrack_free(ct);
f0a3c086 1559 return ERR_PTR(err);
c1d10adb
PNA
1560}
1561
601e68e1
YH
1562static int
1563ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1564 const struct nlmsghdr *nlh,
1565 const struct nlattr * const cda[])
c1d10adb 1566{
9592a5c0 1567 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1568 struct nf_conntrack_tuple otuple, rtuple;
1569 struct nf_conntrack_tuple_hash *h = NULL;
96bcf938 1570 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7d367e06 1571 struct nf_conn *ct;
c1d10adb 1572 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1573 u16 zone;
1574 int err;
1575
1576 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1577 if (err < 0)
1578 return err;
c1d10adb 1579
df6fb868 1580 if (cda[CTA_TUPLE_ORIG]) {
c1d10adb
PNA
1581 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1582 if (err < 0)
1583 return err;
1584 }
1585
df6fb868 1586 if (cda[CTA_TUPLE_REPLY]) {
c1d10adb
PNA
1587 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1588 if (err < 0)
1589 return err;
1590 }
1591
df6fb868 1592 if (cda[CTA_TUPLE_ORIG])
7d367e06 1593 h = nf_conntrack_find_get(net, zone, &otuple);
df6fb868 1594 else if (cda[CTA_TUPLE_REPLY])
7d367e06 1595 h = nf_conntrack_find_get(net, zone, &rtuple);
c1d10adb
PNA
1596
1597 if (h == NULL) {
c1d10adb 1598 err = -ENOENT;
f0a3c086 1599 if (nlh->nlmsg_flags & NLM_F_CREATE) {
fecc1133 1600 enum ip_conntrack_events events;
5faa1f4c 1601
ef00f89f 1602 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
f0a3c086 1603 &rtuple, u3);
7d367e06
JK
1604 if (IS_ERR(ct))
1605 return PTR_ERR(ct);
1606
f0a3c086 1607 err = 0;
fecc1133
PNA
1608 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1609 events = IPCT_RELATED;
1610 else
1611 events = IPCT_NEW;
1612
858b3133
PM
1613 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1614 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1615 (1 << IPCT_HELPER) |
1616 (1 << IPCT_PROTOINFO) |
1617 (1 << IPCT_NATSEQADJ) |
1618 (1 << IPCT_MARK) | events,
15e47304 1619 ct, NETLINK_CB(skb).portid,
a0891aa6 1620 nlmsg_report(nlh));
f0a3c086 1621 nf_ct_put(ct);
7d367e06 1622 }
5faa1f4c 1623
c1d10adb
PNA
1624 return err;
1625 }
1626 /* implicit 'else' */
1627
c1d10adb 1628 err = -EEXIST;
7d367e06 1629 ct = nf_ct_tuplehash_to_ctrack(h);
ff4ca827 1630 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
7d367e06 1631 spin_lock_bh(&nf_conntrack_lock);
19abb7b0 1632 err = ctnetlink_change_conntrack(ct, cda);
7d367e06 1633 spin_unlock_bh(&nf_conntrack_lock);
19abb7b0 1634 if (err == 0) {
858b3133
PM
1635 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1636 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1637 (1 << IPCT_HELPER) |
1638 (1 << IPCT_PROTOINFO) |
1639 (1 << IPCT_NATSEQADJ) |
1640 (1 << IPCT_MARK),
15e47304 1641 ct, NETLINK_CB(skb).portid,
a0891aa6 1642 nlmsg_report(nlh));
7d367e06 1643 }
ff4ca827 1644 }
c1d10adb 1645
7d367e06 1646 nf_ct_put(ct);
c1d10adb
PNA
1647 return err;
1648}
1649
392025f8 1650static int
15e47304 1651ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
392025f8
PNA
1652 __u16 cpu, const struct ip_conntrack_stat *st)
1653{
1654 struct nlmsghdr *nlh;
1655 struct nfgenmsg *nfmsg;
15e47304 1656 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
392025f8
PNA
1657
1658 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
15e47304 1659 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
392025f8
PNA
1660 if (nlh == NULL)
1661 goto nlmsg_failure;
1662
1663 nfmsg = nlmsg_data(nlh);
1664 nfmsg->nfgen_family = AF_UNSPEC;
1665 nfmsg->version = NFNETLINK_V0;
1666 nfmsg->res_id = htons(cpu);
1667
1668 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1669 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1670 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1671 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1672 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1673 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1674 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1675 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1676 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1677 htonl(st->insert_failed)) ||
1678 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1679 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1680 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1681 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1682 htonl(st->search_restart)))
1683 goto nla_put_failure;
1684
1685 nlmsg_end(skb, nlh);
1686 return skb->len;
1687
1688nla_put_failure:
1689nlmsg_failure:
1690 nlmsg_cancel(skb, nlh);
1691 return -1;
1692}
1693
1694static int
1695ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
1696{
1697 int cpu;
1698 struct net *net = sock_net(skb->sk);
1699
1700 if (cb->args[0] == nr_cpu_ids)
1701 return 0;
1702
1703 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1704 const struct ip_conntrack_stat *st;
1705
1706 if (!cpu_possible(cpu))
1707 continue;
1708
1709 st = per_cpu_ptr(net->ct.stat, cpu);
1710 if (ctnetlink_ct_stat_cpu_fill_info(skb,
15e47304 1711 NETLINK_CB(cb->skb).portid,
392025f8
PNA
1712 cb->nlh->nlmsg_seq,
1713 cpu, st) < 0)
1714 break;
1715 }
1716 cb->args[0] = cpu;
1717
1718 return skb->len;
1719}
1720
1721static int
1722ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb,
1723 const struct nlmsghdr *nlh,
1724 const struct nlattr * const cda[])
1725{
1726 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1727 struct netlink_dump_control c = {
1728 .dump = ctnetlink_ct_stat_cpu_dump,
1729 };
1730 return netlink_dump_start(ctnl, skb, nlh, &c);
1731 }
1732
1733 return 0;
1734}
1735
1736static int
15e47304 1737ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
392025f8
PNA
1738 struct net *net)
1739{
1740 struct nlmsghdr *nlh;
1741 struct nfgenmsg *nfmsg;
15e47304 1742 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
392025f8
PNA
1743 unsigned int nr_conntracks = atomic_read(&net->ct.count);
1744
1745 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
15e47304 1746 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
392025f8
PNA
1747 if (nlh == NULL)
1748 goto nlmsg_failure;
1749
1750 nfmsg = nlmsg_data(nlh);
1751 nfmsg->nfgen_family = AF_UNSPEC;
1752 nfmsg->version = NFNETLINK_V0;
1753 nfmsg->res_id = 0;
1754
1755 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
1756 goto nla_put_failure;
1757
1758 nlmsg_end(skb, nlh);
1759 return skb->len;
1760
1761nla_put_failure:
1762nlmsg_failure:
1763 nlmsg_cancel(skb, nlh);
1764 return -1;
1765}
1766
1767static int
1768ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb,
1769 const struct nlmsghdr *nlh,
1770 const struct nlattr * const cda[])
1771{
1772 struct sk_buff *skb2;
1773 int err;
1774
1775 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1776 if (skb2 == NULL)
1777 return -ENOMEM;
1778
15e47304 1779 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
392025f8
PNA
1780 nlh->nlmsg_seq,
1781 NFNL_MSG_TYPE(nlh->nlmsg_type),
1782 sock_net(skb->sk));
1783 if (err <= 0)
1784 goto free;
1785
15e47304 1786 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
392025f8
PNA
1787 if (err < 0)
1788 goto out;
1789
1790 return 0;
1791
1792free:
1793 kfree_skb(skb2);
1794out:
1795 /* this avoids a loop in nfnetlink. */
1796 return err == -EAGAIN ? -ENOBUFS : err;
1797}
1798
7c622345 1799#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
9cb01766
PNA
1800static size_t
1801ctnetlink_nfqueue_build_size(const struct nf_conn *ct)
1802{
1803 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
1804 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
1805 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
1806 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
1807 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
1808 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
1809 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
1810 + nla_total_size(0) /* CTA_PROTOINFO */
1811 + nla_total_size(0) /* CTA_HELP */
1812 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
1813 + ctnetlink_secctx_size(ct)
1814#ifdef CONFIG_NF_NAT_NEEDED
1815 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
1816 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
1817#endif
1818#ifdef CONFIG_NF_CONNTRACK_MARK
1819 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
1820#endif
1821 + ctnetlink_proto_size(ct)
1822 ;
1823}
1824
1825static int
1826ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct)
1827{
1828 struct nlattr *nest_parms;
1829
1830 rcu_read_lock();
1831 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
1832 if (!nest_parms)
1833 goto nla_put_failure;
1834 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
1835 goto nla_put_failure;
1836 nla_nest_end(skb, nest_parms);
1837
1838 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
1839 if (!nest_parms)
1840 goto nla_put_failure;
1841 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
1842 goto nla_put_failure;
1843 nla_nest_end(skb, nest_parms);
1844
1845 if (nf_ct_zone(ct)) {
1846 if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
1847 goto nla_put_failure;
1848 }
1849
1850 if (ctnetlink_dump_id(skb, ct) < 0)
1851 goto nla_put_failure;
1852
1853 if (ctnetlink_dump_status(skb, ct) < 0)
1854 goto nla_put_failure;
1855
1856 if (ctnetlink_dump_timeout(skb, ct) < 0)
1857 goto nla_put_failure;
1858
1859 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
1860 goto nla_put_failure;
1861
1862 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
1863 goto nla_put_failure;
1864
1865#ifdef CONFIG_NF_CONNTRACK_SECMARK
1866 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
1867 goto nla_put_failure;
1868#endif
1869 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
1870 goto nla_put_failure;
1871
1872 if ((ct->status & IPS_SEQ_ADJUST) &&
1873 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
1874 goto nla_put_failure;
1875
1876#ifdef CONFIG_NF_CONNTRACK_MARK
1877 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
1878 goto nla_put_failure;
1879#endif
1880 rcu_read_unlock();
1881 return 0;
1882
1883nla_put_failure:
1884 rcu_read_unlock();
1885 return -ENOSPC;
1886}
1887
1888static int
1889ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
1890{
1891 int err;
1892
1893 if (cda[CTA_TIMEOUT]) {
1894 err = ctnetlink_change_timeout(ct, cda);
1895 if (err < 0)
1896 return err;
1897 }
1898 if (cda[CTA_STATUS]) {
1899 err = ctnetlink_change_status(ct, cda);
1900 if (err < 0)
1901 return err;
1902 }
1903 if (cda[CTA_HELP]) {
1904 err = ctnetlink_change_helper(ct, cda);
1905 if (err < 0)
1906 return err;
1907 }
1908#if defined(CONFIG_NF_CONNTRACK_MARK)
1909 if (cda[CTA_MARK])
1910 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1911#endif
1912 return 0;
1913}
1914
1915static int
1916ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct)
1917{
1918 struct nlattr *cda[CTA_MAX+1];
68e035c9 1919 int ret;
9cb01766
PNA
1920
1921 nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
1922
68e035c9
PNA
1923 spin_lock_bh(&nf_conntrack_lock);
1924 ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct);
1925 spin_unlock_bh(&nf_conntrack_lock);
1926
1927 return ret;
9cb01766
PNA
1928}
1929
1930static struct nfq_ct_hook ctnetlink_nfqueue_hook = {
1931 .build_size = ctnetlink_nfqueue_build_size,
1932 .build = ctnetlink_nfqueue_build,
1933 .parse = ctnetlink_nfqueue_parse,
1934};
7c622345 1935#endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
9cb01766 1936
601e68e1
YH
1937/***********************************************************************
1938 * EXPECT
1939 ***********************************************************************/
c1d10adb
PNA
1940
1941static inline int
1942ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1943 const struct nf_conntrack_tuple *tuple,
1944 enum ctattr_expect type)
1945{
df6fb868 1946 struct nlattr *nest_parms;
601e68e1 1947
df6fb868
PM
1948 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1949 if (!nest_parms)
1950 goto nla_put_failure;
c1d10adb 1951 if (ctnetlink_dump_tuples(skb, tuple) < 0)
df6fb868
PM
1952 goto nla_put_failure;
1953 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
1954
1955 return 0;
1956
df6fb868 1957nla_put_failure:
c1d10adb 1958 return -1;
601e68e1 1959}
c1d10adb 1960
1cde6436
PNA
1961static inline int
1962ctnetlink_exp_dump_mask(struct sk_buff *skb,
1963 const struct nf_conntrack_tuple *tuple,
d4156e8c 1964 const struct nf_conntrack_tuple_mask *mask)
1cde6436
PNA
1965{
1966 int ret;
1967 struct nf_conntrack_l3proto *l3proto;
605dcad6 1968 struct nf_conntrack_l4proto *l4proto;
d4156e8c 1969 struct nf_conntrack_tuple m;
df6fb868 1970 struct nlattr *nest_parms;
d4156e8c
PM
1971
1972 memset(&m, 0xFF, sizeof(m));
d4156e8c 1973 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
e578756c
PM
1974 m.src.u.all = mask->src.u.all;
1975 m.dst.protonum = tuple->dst.protonum;
d4156e8c 1976
df6fb868
PM
1977 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1978 if (!nest_parms)
1979 goto nla_put_failure;
1cde6436 1980
3b988ece 1981 rcu_read_lock();
528a3a6f 1982 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
d4156e8c 1983 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
3b988ece
HS
1984 if (ret >= 0) {
1985 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
1986 tuple->dst.protonum);
d4156e8c 1987 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
3b988ece
HS
1988 }
1989 rcu_read_unlock();
1990
1cde6436 1991 if (unlikely(ret < 0))
df6fb868 1992 goto nla_put_failure;
1cde6436 1993
df6fb868 1994 nla_nest_end(skb, nest_parms);
1cde6436
PNA
1995
1996 return 0;
1997
df6fb868 1998nla_put_failure:
1cde6436
PNA
1999 return -1;
2000}
2001
c7232c99
PM
2002static const union nf_inet_addr any_addr;
2003
bb5cf80e 2004static int
c1d10adb 2005ctnetlink_exp_dump_expect(struct sk_buff *skb,
601e68e1 2006 const struct nf_conntrack_expect *exp)
c1d10adb
PNA
2007{
2008 struct nf_conn *master = exp->master;
c1216382 2009 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
bc01befd 2010 struct nf_conn_help *help;
076a0ca0
PNA
2011#ifdef CONFIG_NF_NAT_NEEDED
2012 struct nlattr *nest_parms;
2013 struct nf_conntrack_tuple nat_tuple = {};
2014#endif
544d5c7d
PNA
2015 struct nf_ct_helper_expectfn *expfn;
2016
d978e5da
PM
2017 if (timeout < 0)
2018 timeout = 0;
c1d10adb
PNA
2019
2020 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
df6fb868 2021 goto nla_put_failure;
1cde6436 2022 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
df6fb868 2023 goto nla_put_failure;
c1d10adb
PNA
2024 if (ctnetlink_exp_dump_tuple(skb,
2025 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2026 CTA_EXPECT_MASTER) < 0)
df6fb868 2027 goto nla_put_failure;
601e68e1 2028
076a0ca0 2029#ifdef CONFIG_NF_NAT_NEEDED
c7232c99
PM
2030 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2031 exp->saved_proto.all) {
076a0ca0
PNA
2032 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2033 if (!nest_parms)
2034 goto nla_put_failure;
2035
cc1eb431
DM
2036 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2037 goto nla_put_failure;
076a0ca0
PNA
2038
2039 nat_tuple.src.l3num = nf_ct_l3num(master);
c7232c99 2040 nat_tuple.src.u3 = exp->saved_addr;
076a0ca0
PNA
2041 nat_tuple.dst.protonum = nf_ct_protonum(master);
2042 nat_tuple.src.u = exp->saved_proto;
2043
2044 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2045 CTA_EXPECT_NAT_TUPLE) < 0)
2046 goto nla_put_failure;
2047 nla_nest_end(skb, nest_parms);
2048 }
2049#endif
cc1eb431
DM
2050 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2051 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2052 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2053 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2054 goto nla_put_failure;
bc01befd
PNA
2055 help = nfct_help(master);
2056 if (help) {
2057 struct nf_conntrack_helper *helper;
2058
2059 helper = rcu_dereference(help->helper);
cc1eb431
DM
2060 if (helper &&
2061 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2062 goto nla_put_failure;
bc01befd 2063 }
544d5c7d 2064 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
cc1eb431
DM
2065 if (expfn != NULL &&
2066 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2067 goto nla_put_failure;
c1d10adb
PNA
2068
2069 return 0;
601e68e1 2070
df6fb868 2071nla_put_failure:
c1d10adb
PNA
2072 return -1;
2073}
2074
2075static int
15e47304 2076ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
8b0a231d 2077 int event, const struct nf_conntrack_expect *exp)
c1d10adb
PNA
2078{
2079 struct nlmsghdr *nlh;
2080 struct nfgenmsg *nfmsg;
15e47304 2081 unsigned int flags = portid ? NLM_F_MULTI : 0;
c1d10adb
PNA
2082
2083 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
15e47304 2084 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
96bcf938
PNA
2085 if (nlh == NULL)
2086 goto nlmsg_failure;
c1d10adb 2087
96bcf938 2088 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
2089 nfmsg->nfgen_family = exp->tuple.src.l3num;
2090 nfmsg->version = NFNETLINK_V0;
2091 nfmsg->res_id = 0;
2092
2093 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 2094 goto nla_put_failure;
c1d10adb 2095
96bcf938 2096 nlmsg_end(skb, nlh);
c1d10adb
PNA
2097 return skb->len;
2098
2099nlmsg_failure:
df6fb868 2100nla_put_failure:
96bcf938 2101 nlmsg_cancel(skb, nlh);
c1d10adb
PNA
2102 return -1;
2103}
2104
2105#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
2106static int
2107ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
c1d10adb 2108{
9592a5c0
AD
2109 struct nf_conntrack_expect *exp = item->exp;
2110 struct net *net = nf_ct_exp_net(exp);
c1d10adb
PNA
2111 struct nlmsghdr *nlh;
2112 struct nfgenmsg *nfmsg;
c1d10adb 2113 struct sk_buff *skb;
ebbf41df 2114 unsigned int type, group;
c1d10adb
PNA
2115 int flags = 0;
2116
ebbf41df
PNA
2117 if (events & (1 << IPEXP_DESTROY)) {
2118 type = IPCTNL_MSG_EXP_DELETE;
2119 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2120 } else if (events & (1 << IPEXP_NEW)) {
c1d10adb
PNA
2121 type = IPCTNL_MSG_EXP_NEW;
2122 flags = NLM_F_CREATE|NLM_F_EXCL;
ebbf41df 2123 group = NFNLGRP_CONNTRACK_EXP_NEW;
c1d10adb 2124 } else
e34d5c1a 2125 return 0;
c1d10adb 2126
ebbf41df 2127 if (!item->report && !nfnetlink_has_listeners(net, group))
e34d5c1a 2128 return 0;
b3a27bfb 2129
96bcf938
PNA
2130 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2131 if (skb == NULL)
150ace0d 2132 goto errout;
c1d10adb 2133
b633ad5f 2134 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
15e47304 2135 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
96bcf938
PNA
2136 if (nlh == NULL)
2137 goto nlmsg_failure;
c1d10adb 2138
96bcf938 2139 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
2140 nfmsg->nfgen_family = exp->tuple.src.l3num;
2141 nfmsg->version = NFNETLINK_V0;
2142 nfmsg->res_id = 0;
2143
528a3a6f 2144 rcu_read_lock();
c1d10adb 2145 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 2146 goto nla_put_failure;
528a3a6f 2147 rcu_read_unlock();
c1d10adb 2148
96bcf938 2149 nlmsg_end(skb, nlh);
15e47304 2150 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
e34d5c1a 2151 return 0;
c1d10adb 2152
df6fb868 2153nla_put_failure:
528a3a6f 2154 rcu_read_unlock();
96bcf938 2155 nlmsg_cancel(skb, nlh);
528a3a6f 2156nlmsg_failure:
c1d10adb 2157 kfree_skb(skb);
150ace0d 2158errout:
9592a5c0 2159 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
e34d5c1a 2160 return 0;
c1d10adb
PNA
2161}
2162#endif
cf6994c2
PM
2163static int ctnetlink_exp_done(struct netlink_callback *cb)
2164{
31f15875
PM
2165 if (cb->args[1])
2166 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
cf6994c2
PM
2167 return 0;
2168}
c1d10adb
PNA
2169
2170static int
2171ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2172{
9592a5c0 2173 struct net *net = sock_net(skb->sk);
cf6994c2 2174 struct nf_conntrack_expect *exp, *last;
96bcf938 2175 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
31f15875 2176 struct hlist_node *n;
87711cb8 2177 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 2178
7d0742da 2179 rcu_read_lock();
31f15875
PM
2180 last = (struct nf_conntrack_expect *)cb->args[1];
2181 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
cf6994c2 2182restart:
9b03f38d 2183 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
31f15875
PM
2184 hnode) {
2185 if (l3proto && exp->tuple.src.l3num != l3proto)
cf6994c2 2186 continue;
31f15875
PM
2187 if (cb->args[1]) {
2188 if (exp != last)
2189 continue;
2190 cb->args[1] = 0;
2191 }
8b0a231d 2192 if (ctnetlink_exp_fill_info(skb,
15e47304 2193 NETLINK_CB(cb->skb).portid,
31f15875
PM
2194 cb->nlh->nlmsg_seq,
2195 IPCTNL_MSG_EXP_NEW,
8b0a231d 2196 exp) < 0) {
7d0742da
PM
2197 if (!atomic_inc_not_zero(&exp->use))
2198 continue;
31f15875
PM
2199 cb->args[1] = (unsigned long)exp;
2200 goto out;
2201 }
cf6994c2 2202 }
31f15875
PM
2203 if (cb->args[1]) {
2204 cb->args[1] = 0;
2205 goto restart;
cf6994c2
PM
2206 }
2207 }
601e68e1 2208out:
7d0742da 2209 rcu_read_unlock();
cf6994c2
PM
2210 if (last)
2211 nf_ct_expect_put(last);
c1d10adb 2212
c1d10adb
PNA
2213 return skb->len;
2214}
2215
f73e924c 2216static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
d0b0268f
PM
2217 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2218 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2219 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
f73e924c
PM
2220 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2221 [CTA_EXPECT_ID] = { .type = NLA_U32 },
d0b0268f 2222 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING },
bcac0dfa 2223 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
8b008faf 2224 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
b8c5e52c 2225 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
076a0ca0 2226 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
544d5c7d 2227 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
c1d10adb
PNA
2228};
2229
2230static int
601e68e1 2231ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2232 const struct nlmsghdr *nlh,
2233 const struct nlattr * const cda[])
c1d10adb 2234{
9592a5c0 2235 struct net *net = sock_net(ctnl);
c1d10adb
PNA
2236 struct nf_conntrack_tuple tuple;
2237 struct nf_conntrack_expect *exp;
2238 struct sk_buff *skb2;
96bcf938 2239 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 2240 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
2241 u16 zone;
2242 int err;
c1d10adb 2243
b8f3ab42 2244 if (nlh->nlmsg_flags & NLM_F_DUMP) {
80d326fa
PNA
2245 struct netlink_dump_control c = {
2246 .dump = ctnetlink_exp_dump_table,
2247 .done = ctnetlink_exp_done,
2248 };
2249 return netlink_dump_start(ctnl, skb, nlh, &c);
c1d10adb
PNA
2250 }
2251
ef00f89f
PM
2252 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2253 if (err < 0)
2254 return err;
2255
35dba1d7
PNA
2256 if (cda[CTA_EXPECT_TUPLE])
2257 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2258 else if (cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
2259 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2260 else
2261 return -EINVAL;
2262
2263 if (err < 0)
2264 return err;
2265
ef00f89f 2266 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
2267 if (!exp)
2268 return -ENOENT;
2269
df6fb868 2270 if (cda[CTA_EXPECT_ID]) {
77236b6e 2271 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 2272 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 2273 nf_ct_expect_put(exp);
c1d10adb
PNA
2274 return -ENOENT;
2275 }
601e68e1 2276 }
c1d10adb
PNA
2277
2278 err = -ENOMEM;
96bcf938 2279 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
81378f72
PNA
2280 if (skb2 == NULL) {
2281 nf_ct_expect_put(exp);
c1d10adb 2282 goto out;
81378f72 2283 }
4e9b8269 2284
528a3a6f 2285 rcu_read_lock();
15e47304 2286 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
8b0a231d 2287 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
528a3a6f 2288 rcu_read_unlock();
81378f72 2289 nf_ct_expect_put(exp);
c1d10adb
PNA
2290 if (err <= 0)
2291 goto free;
2292
15e47304 2293 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
81378f72
PNA
2294 if (err < 0)
2295 goto out;
c1d10adb 2296
81378f72 2297 return 0;
c1d10adb
PNA
2298
2299free:
2300 kfree_skb(skb2);
2301out:
81378f72
PNA
2302 /* this avoids a loop in nfnetlink. */
2303 return err == -EAGAIN ? -ENOBUFS : err;
c1d10adb
PNA
2304}
2305
2306static int
601e68e1 2307ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2308 const struct nlmsghdr *nlh,
2309 const struct nlattr * const cda[])
c1d10adb 2310{
9592a5c0 2311 struct net *net = sock_net(ctnl);
31f15875 2312 struct nf_conntrack_expect *exp;
c1d10adb 2313 struct nf_conntrack_tuple tuple;
96bcf938 2314 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
31f15875 2315 struct hlist_node *n, *next;
c1d10adb 2316 u_int8_t u3 = nfmsg->nfgen_family;
31f15875 2317 unsigned int i;
ef00f89f 2318 u16 zone;
c1d10adb
PNA
2319 int err;
2320
df6fb868 2321 if (cda[CTA_EXPECT_TUPLE]) {
c1d10adb 2322 /* delete a single expect by tuple */
ef00f89f
PM
2323 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2324 if (err < 0)
2325 return err;
2326
c1d10adb
PNA
2327 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2328 if (err < 0)
2329 return err;
2330
2331 /* bump usage count to 2 */
ef00f89f 2332 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
2333 if (!exp)
2334 return -ENOENT;
2335
df6fb868 2336 if (cda[CTA_EXPECT_ID]) {
77236b6e 2337 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 2338 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 2339 nf_ct_expect_put(exp);
c1d10adb
PNA
2340 return -ENOENT;
2341 }
2342 }
2343
2344 /* after list removal, usage count == 1 */
ebbf41df
PNA
2345 spin_lock_bh(&nf_conntrack_lock);
2346 if (del_timer(&exp->timeout)) {
15e47304 2347 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
ebbf41df
PNA
2348 nlmsg_report(nlh));
2349 nf_ct_expect_put(exp);
2350 }
2351 spin_unlock_bh(&nf_conntrack_lock);
601e68e1 2352 /* have to put what we 'get' above.
c1d10adb 2353 * after this line usage count == 0 */
6823645d 2354 nf_ct_expect_put(exp);
df6fb868
PM
2355 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2356 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
31f15875 2357 struct nf_conn_help *m_help;
c1d10adb
PNA
2358
2359 /* delete all expectations for this helper */
f8ba1aff 2360 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
2361 for (i = 0; i < nf_ct_expect_hsize; i++) {
2362 hlist_for_each_entry_safe(exp, n, next,
9592a5c0 2363 &net->ct.expect_hash[i],
31f15875
PM
2364 hnode) {
2365 m_help = nfct_help(exp->master);
794e6871
PM
2366 if (!strcmp(m_help->helper->name, name) &&
2367 del_timer(&exp->timeout)) {
ebbf41df 2368 nf_ct_unlink_expect_report(exp,
15e47304 2369 NETLINK_CB(skb).portid,
ebbf41df 2370 nlmsg_report(nlh));
31f15875
PM
2371 nf_ct_expect_put(exp);
2372 }
c1d10adb
PNA
2373 }
2374 }
f8ba1aff 2375 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
2376 } else {
2377 /* This basically means we have to flush everything*/
f8ba1aff 2378 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
2379 for (i = 0; i < nf_ct_expect_hsize; i++) {
2380 hlist_for_each_entry_safe(exp, n, next,
9592a5c0 2381 &net->ct.expect_hash[i],
31f15875
PM
2382 hnode) {
2383 if (del_timer(&exp->timeout)) {
ebbf41df 2384 nf_ct_unlink_expect_report(exp,
15e47304 2385 NETLINK_CB(skb).portid,
ebbf41df 2386 nlmsg_report(nlh));
31f15875
PM
2387 nf_ct_expect_put(exp);
2388 }
c1d10adb
PNA
2389 }
2390 }
f8ba1aff 2391 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
2392 }
2393
2394 return 0;
2395}
2396static int
39938324
PM
2397ctnetlink_change_expect(struct nf_conntrack_expect *x,
2398 const struct nlattr * const cda[])
c1d10adb 2399{
9768e1ac
KW
2400 if (cda[CTA_EXPECT_TIMEOUT]) {
2401 if (!del_timer(&x->timeout))
2402 return -ETIME;
2403
2404 x->timeout.expires = jiffies +
2405 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2406 add_timer(&x->timeout);
2407 }
2408 return 0;
c1d10adb
PNA
2409}
2410
076a0ca0
PNA
2411static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2412 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2413 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2414};
2415
2416static int
2417ctnetlink_parse_expect_nat(const struct nlattr *attr,
2418 struct nf_conntrack_expect *exp,
2419 u_int8_t u3)
2420{
2421#ifdef CONFIG_NF_NAT_NEEDED
2422 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2423 struct nf_conntrack_tuple nat_tuple = {};
2424 int err;
2425
2426 nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2427
2428 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2429 return -EINVAL;
2430
2431 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2432 &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2433 if (err < 0)
2434 return err;
2435
c7232c99 2436 exp->saved_addr = nat_tuple.src.u3;
076a0ca0
PNA
2437 exp->saved_proto = nat_tuple.src.u;
2438 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2439
2440 return 0;
2441#else
2442 return -EOPNOTSUPP;
2443#endif
2444}
2445
c1d10adb 2446static int
ef00f89f
PM
2447ctnetlink_create_expect(struct net *net, u16 zone,
2448 const struct nlattr * const cda[],
9592a5c0 2449 u_int8_t u3,
15e47304 2450 u32 portid, int report)
c1d10adb
PNA
2451{
2452 struct nf_conntrack_tuple tuple, mask, master_tuple;
2453 struct nf_conntrack_tuple_hash *h = NULL;
2454 struct nf_conntrack_expect *exp;
2455 struct nf_conn *ct;
dc808fe2 2456 struct nf_conn_help *help;
660fdb2a 2457 struct nf_conntrack_helper *helper = NULL;
b8c5e52c 2458 u_int32_t class = 0;
c1d10adb
PNA
2459 int err = 0;
2460
c1d10adb
PNA
2461 /* caller guarantees that those three CTA_EXPECT_* exist */
2462 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2463 if (err < 0)
2464 return err;
2465 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2466 if (err < 0)
2467 return err;
2468 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2469 if (err < 0)
2470 return err;
2471
2472 /* Look for master conntrack of this expectation */
ef00f89f 2473 h = nf_conntrack_find_get(net, zone, &master_tuple);
c1d10adb
PNA
2474 if (!h)
2475 return -ENOENT;
2476 ct = nf_ct_tuplehash_to_ctrack(h);
660fdb2a
PNA
2477
2478 /* Look for helper of this expectation */
2479 if (cda[CTA_EXPECT_HELP_NAME]) {
2480 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2481
2482 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2483 nf_ct_protonum(ct));
2484 if (helper == NULL) {
2485#ifdef CONFIG_MODULES
2486 if (request_module("nfct-helper-%s", helpname) < 0) {
2487 err = -EOPNOTSUPP;
2488 goto out;
2489 }
2490
2491 helper = __nf_conntrack_helper_find(helpname,
2492 nf_ct_l3num(ct),
2493 nf_ct_protonum(ct));
2494 if (helper) {
2495 err = -EAGAIN;
2496 goto out;
2497 }
2498#endif
2499 err = -EOPNOTSUPP;
2500 goto out;
2501 }
2502 }
2503
b8c5e52c
PNA
2504 if (cda[CTA_EXPECT_CLASS] && helper) {
2505 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2506 if (class > helper->expect_class_max) {
2507 err = -EINVAL;
2508 goto out;
2509 }
2510 }
6823645d 2511 exp = nf_ct_expect_alloc(ct);
c1d10adb
PNA
2512 if (!exp) {
2513 err = -ENOMEM;
2514 goto out;
2515 }
bc01befd
PNA
2516 help = nfct_help(ct);
2517 if (!help) {
2518 if (!cda[CTA_EXPECT_TIMEOUT]) {
2519 err = -EINVAL;
2520 goto out;
2521 }
2522 exp->timeout.expires =
2523 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
601e68e1 2524
bc01befd
PNA
2525 exp->flags = NF_CT_EXPECT_USERSPACE;
2526 if (cda[CTA_EXPECT_FLAGS]) {
2527 exp->flags |=
2528 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2529 }
2530 } else {
2531 if (cda[CTA_EXPECT_FLAGS]) {
2532 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2533 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2534 } else
2535 exp->flags = 0;
2536 }
544d5c7d
PNA
2537 if (cda[CTA_EXPECT_FN]) {
2538 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2539 struct nf_ct_helper_expectfn *expfn;
2540
2541 expfn = nf_ct_helper_expectfn_find_by_name(name);
2542 if (expfn == NULL) {
2543 err = -EINVAL;
2544 goto err_out;
2545 }
2546 exp->expectfn = expfn->expectfn;
2547 } else
2548 exp->expectfn = NULL;
601e68e1 2549
b8c5e52c 2550 exp->class = class;
c1d10adb 2551 exp->master = ct;
660fdb2a 2552 exp->helper = helper;
c1d10adb 2553 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
d4156e8c
PM
2554 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2555 exp->mask.src.u.all = mask.src.u.all;
c1d10adb 2556
076a0ca0
PNA
2557 if (cda[CTA_EXPECT_NAT]) {
2558 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2559 exp, u3);
2560 if (err < 0)
2561 goto err_out;
2562 }
15e47304 2563 err = nf_ct_expect_related_report(exp, portid, report);
076a0ca0 2564err_out:
6823645d 2565 nf_ct_expect_put(exp);
601e68e1 2566out:
c1d10adb
PNA
2567 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2568 return err;
2569}
2570
2571static int
2572ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2573 const struct nlmsghdr *nlh,
2574 const struct nlattr * const cda[])
c1d10adb 2575{
9592a5c0 2576 struct net *net = sock_net(ctnl);
c1d10adb
PNA
2577 struct nf_conntrack_tuple tuple;
2578 struct nf_conntrack_expect *exp;
96bcf938 2579 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 2580 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
2581 u16 zone;
2582 int err;
c1d10adb 2583
df6fb868
PM
2584 if (!cda[CTA_EXPECT_TUPLE]
2585 || !cda[CTA_EXPECT_MASK]
2586 || !cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
2587 return -EINVAL;
2588
ef00f89f
PM
2589 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2590 if (err < 0)
2591 return err;
2592
c1d10adb
PNA
2593 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2594 if (err < 0)
2595 return err;
2596
f8ba1aff 2597 spin_lock_bh(&nf_conntrack_lock);
ef00f89f 2598 exp = __nf_ct_expect_find(net, zone, &tuple);
c1d10adb
PNA
2599
2600 if (!exp) {
f8ba1aff 2601 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 2602 err = -ENOENT;
19abb7b0 2603 if (nlh->nlmsg_flags & NLM_F_CREATE) {
ef00f89f 2604 err = ctnetlink_create_expect(net, zone, cda,
19abb7b0 2605 u3,
15e47304 2606 NETLINK_CB(skb).portid,
19abb7b0
PNA
2607 nlmsg_report(nlh));
2608 }
c1d10adb
PNA
2609 return err;
2610 }
2611
2612 err = -EEXIST;
2613 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2614 err = ctnetlink_change_expect(exp, cda);
f8ba1aff 2615 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 2616
c1d10adb
PNA
2617 return err;
2618}
2619
392025f8 2620static int
15e47304 2621ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
392025f8
PNA
2622 const struct ip_conntrack_stat *st)
2623{
2624 struct nlmsghdr *nlh;
2625 struct nfgenmsg *nfmsg;
15e47304 2626 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
392025f8
PNA
2627
2628 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
15e47304 2629 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
392025f8
PNA
2630 if (nlh == NULL)
2631 goto nlmsg_failure;
2632
2633 nfmsg = nlmsg_data(nlh);
2634 nfmsg->nfgen_family = AF_UNSPEC;
2635 nfmsg->version = NFNETLINK_V0;
2636 nfmsg->res_id = htons(cpu);
2637
2638 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
2639 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
2640 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
2641 goto nla_put_failure;
2642
2643 nlmsg_end(skb, nlh);
2644 return skb->len;
2645
2646nla_put_failure:
2647nlmsg_failure:
2648 nlmsg_cancel(skb, nlh);
2649 return -1;
2650}
2651
2652static int
2653ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2654{
2655 int cpu;
2656 struct net *net = sock_net(skb->sk);
2657
2658 if (cb->args[0] == nr_cpu_ids)
2659 return 0;
2660
2661 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2662 const struct ip_conntrack_stat *st;
2663
2664 if (!cpu_possible(cpu))
2665 continue;
2666
2667 st = per_cpu_ptr(net->ct.stat, cpu);
15e47304 2668 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
392025f8
PNA
2669 cb->nlh->nlmsg_seq,
2670 cpu, st) < 0)
2671 break;
2672 }
2673 cb->args[0] = cpu;
2674
2675 return skb->len;
2676}
2677
2678static int
2679ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb,
2680 const struct nlmsghdr *nlh,
2681 const struct nlattr * const cda[])
2682{
2683 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2684 struct netlink_dump_control c = {
2685 .dump = ctnetlink_exp_stat_cpu_dump,
2686 };
2687 return netlink_dump_start(ctnl, skb, nlh, &c);
2688 }
2689
2690 return 0;
2691}
2692
c1d10adb 2693#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
2694static struct nf_ct_event_notifier ctnl_notifier = {
2695 .fcn = ctnetlink_conntrack_event,
c1d10adb
PNA
2696};
2697
e34d5c1a
PNA
2698static struct nf_exp_event_notifier ctnl_notifier_exp = {
2699 .fcn = ctnetlink_expect_event,
c1d10adb
PNA
2700};
2701#endif
2702
7c8d4cb4 2703static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
c1d10adb 2704 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
f73e924c
PM
2705 .attr_count = CTA_MAX,
2706 .policy = ct_nla_policy },
c1d10adb 2707 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
2708 .attr_count = CTA_MAX,
2709 .policy = ct_nla_policy },
c1d10adb 2710 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
f73e924c
PM
2711 .attr_count = CTA_MAX,
2712 .policy = ct_nla_policy },
c1d10adb 2713 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
2714 .attr_count = CTA_MAX,
2715 .policy = ct_nla_policy },
392025f8
PNA
2716 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
2717 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
c1d10adb
PNA
2718};
2719
7c8d4cb4 2720static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
c1d10adb 2721 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
f73e924c
PM
2722 .attr_count = CTA_EXPECT_MAX,
2723 .policy = exp_nla_policy },
c1d10adb 2724 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
f73e924c
PM
2725 .attr_count = CTA_EXPECT_MAX,
2726 .policy = exp_nla_policy },
c1d10adb 2727 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
f73e924c
PM
2728 .attr_count = CTA_EXPECT_MAX,
2729 .policy = exp_nla_policy },
392025f8 2730 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
c1d10adb
PNA
2731};
2732
7c8d4cb4 2733static const struct nfnetlink_subsystem ctnl_subsys = {
c1d10adb
PNA
2734 .name = "conntrack",
2735 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2736 .cb_count = IPCTNL_MSG_MAX,
2737 .cb = ctnl_cb,
2738};
2739
7c8d4cb4 2740static const struct nfnetlink_subsystem ctnl_exp_subsys = {
c1d10adb
PNA
2741 .name = "conntrack_expect",
2742 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2743 .cb_count = IPCTNL_MSG_EXP_MAX,
2744 .cb = ctnl_exp_cb,
2745};
2746
d2483dde 2747MODULE_ALIAS("ip_conntrack_netlink");
c1d10adb 2748MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
34f9a2e4 2749MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
c1d10adb 2750
70e9942f
PNA
2751static int __net_init ctnetlink_net_init(struct net *net)
2752{
2753#ifdef CONFIG_NF_CONNTRACK_EVENTS
2754 int ret;
2755
2756 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2757 if (ret < 0) {
2758 pr_err("ctnetlink_init: cannot register notifier.\n");
2759 goto err_out;
2760 }
2761
2762 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2763 if (ret < 0) {
2764 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2765 goto err_unreg_notifier;
2766 }
2767#endif
2768 return 0;
2769
2770#ifdef CONFIG_NF_CONNTRACK_EVENTS
2771err_unreg_notifier:
2772 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2773err_out:
2774 return ret;
2775#endif
2776}
2777
2778static void ctnetlink_net_exit(struct net *net)
2779{
2780#ifdef CONFIG_NF_CONNTRACK_EVENTS
2781 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2782 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2783#endif
2784}
2785
2786static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2787{
2788 struct net *net;
2789
2790 list_for_each_entry(net, net_exit_list, exit_list)
2791 ctnetlink_net_exit(net);
2792}
2793
2794static struct pernet_operations ctnetlink_net_ops = {
2795 .init = ctnetlink_net_init,
2796 .exit_batch = ctnetlink_net_exit_batch,
2797};
2798
c1d10adb
PNA
2799static int __init ctnetlink_init(void)
2800{
2801 int ret;
2802
654d0fbd 2803 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
c1d10adb
PNA
2804 ret = nfnetlink_subsys_register(&ctnl_subsys);
2805 if (ret < 0) {
654d0fbd 2806 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
c1d10adb
PNA
2807 goto err_out;
2808 }
2809
2810 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2811 if (ret < 0) {
654d0fbd 2812 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
c1d10adb
PNA
2813 goto err_unreg_subsys;
2814 }
2815
ef6acf68
JL
2816 ret = register_pernet_subsys(&ctnetlink_net_ops);
2817 if (ret < 0) {
70e9942f 2818 pr_err("ctnetlink_init: cannot register pernet operations\n");
c1d10adb
PNA
2819 goto err_unreg_exp_subsys;
2820 }
7c622345 2821#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
9cb01766
PNA
2822 /* setup interaction between nf_queue and nf_conntrack_netlink. */
2823 RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook);
2824#endif
c1d10adb
PNA
2825 return 0;
2826
c1d10adb
PNA
2827err_unreg_exp_subsys:
2828 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
c1d10adb
PNA
2829err_unreg_subsys:
2830 nfnetlink_subsys_unregister(&ctnl_subsys);
2831err_out:
2832 return ret;
2833}
2834
2835static void __exit ctnetlink_exit(void)
2836{
654d0fbd 2837 pr_info("ctnetlink: unregistering from nfnetlink.\n");
c1d10adb 2838
70e9942f 2839 unregister_pernet_subsys(&ctnetlink_net_ops);
c1d10adb
PNA
2840 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2841 nfnetlink_subsys_unregister(&ctnl_subsys);
7c622345 2842#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
9cb01766
PNA
2843 RCU_INIT_POINTER(nfq_ct_hook, NULL);
2844#endif
c1d10adb
PNA
2845}
2846
2847module_init(ctnetlink_init);
2848module_exit(ctnetlink_exit);