bridge: set priority of STP packets
[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 900static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
6d1fafca
FW
901 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
902 .len = NF_CT_HELPER_NAME_LEN - 1 },
d0b0268f
PM
903};
904
c1d10adb 905static inline int
ae243bee
PNA
906ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
907 struct nlattr **helpinfo)
c1d10adb 908{
df6fb868 909 struct nlattr *tb[CTA_HELP_MAX+1];
c1d10adb 910
d0b0268f 911 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
c1d10adb 912
df6fb868 913 if (!tb[CTA_HELP_NAME])
c1d10adb
PNA
914 return -EINVAL;
915
df6fb868 916 *helper_name = nla_data(tb[CTA_HELP_NAME]);
c1d10adb 917
ae243bee
PNA
918 if (tb[CTA_HELP_INFO])
919 *helpinfo = tb[CTA_HELP_INFO];
920
c1d10adb
PNA
921 return 0;
922}
923
f73e924c 924static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
d0b0268f
PM
925 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
926 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
f73e924c 927 [CTA_STATUS] = { .type = NLA_U32 },
d0b0268f
PM
928 [CTA_PROTOINFO] = { .type = NLA_NESTED },
929 [CTA_HELP] = { .type = NLA_NESTED },
930 [CTA_NAT_SRC] = { .type = NLA_NESTED },
f73e924c
PM
931 [CTA_TIMEOUT] = { .type = NLA_U32 },
932 [CTA_MARK] = { .type = NLA_U32 },
f73e924c 933 [CTA_ID] = { .type = NLA_U32 },
d0b0268f
PM
934 [CTA_NAT_DST] = { .type = NLA_NESTED },
935 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
6d1fafca
FW
936 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
937 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
ef00f89f 938 [CTA_ZONE] = { .type = NLA_U16 },
0f298a28 939 [CTA_MARK_MASK] = { .type = NLA_U32 },
c1d10adb
PNA
940};
941
942static int
601e68e1 943ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
944 const struct nlmsghdr *nlh,
945 const struct nlattr * const cda[])
c1d10adb 946{
9592a5c0 947 struct net *net = sock_net(ctnl);
c1d10adb
PNA
948 struct nf_conntrack_tuple_hash *h;
949 struct nf_conntrack_tuple tuple;
950 struct nf_conn *ct;
96bcf938 951 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 952 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
953 u16 zone;
954 int err;
955
956 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
957 if (err < 0)
958 return err;
c1d10adb 959
df6fb868 960 if (cda[CTA_TUPLE_ORIG])
c1d10adb 961 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
df6fb868 962 else if (cda[CTA_TUPLE_REPLY])
c1d10adb
PNA
963 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
964 else {
965 /* Flush the whole table */
9592a5c0 966 nf_conntrack_flush_report(net,
15e47304 967 NETLINK_CB(skb).portid,
274d383b 968 nlmsg_report(nlh));
c1d10adb
PNA
969 return 0;
970 }
971
972 if (err < 0)
973 return err;
974
ef00f89f 975 h = nf_conntrack_find_get(net, zone, &tuple);
9ea8cfd6 976 if (!h)
c1d10adb 977 return -ENOENT;
c1d10adb
PNA
978
979 ct = nf_ct_tuplehash_to_ctrack(h);
601e68e1 980
df6fb868 981 if (cda[CTA_ID]) {
77236b6e 982 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
7f85f914 983 if (id != (u32)(unsigned long)ct) {
c1d10adb
PNA
984 nf_ct_put(ct);
985 return -ENOENT;
986 }
601e68e1 987 }
c1d10adb 988
a16a1647
PNA
989 if (del_timer(&ct->timeout)) {
990 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
15e47304 991 NETLINK_CB(skb).portid,
a16a1647
PNA
992 nlmsg_report(nlh)) < 0) {
993 nf_ct_delete_from_lists(ct);
994 /* we failed to report the event, try later */
04dac011 995 nf_ct_dying_timeout(ct);
a16a1647
PNA
996 nf_ct_put(ct);
997 return 0;
998 }
999 /* death_by_timeout would report the event again */
1000 set_bit(IPS_DYING_BIT, &ct->status);
dd7669a9 1001 nf_ct_delete_from_lists(ct);
dd7669a9 1002 nf_ct_put(ct);
dd7669a9 1003 }
c1d10adb 1004 nf_ct_put(ct);
c1d10adb
PNA
1005
1006 return 0;
1007}
1008
1009static int
601e68e1 1010ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1011 const struct nlmsghdr *nlh,
1012 const struct nlattr * const cda[])
c1d10adb 1013{
9592a5c0 1014 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1015 struct nf_conntrack_tuple_hash *h;
1016 struct nf_conntrack_tuple tuple;
1017 struct nf_conn *ct;
1018 struct sk_buff *skb2 = NULL;
96bcf938 1019 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 1020 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1021 u16 zone;
1022 int err;
c1d10adb 1023
80d326fa
PNA
1024 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1025 struct netlink_dump_control c = {
1026 .dump = ctnetlink_dump_table,
1027 .done = ctnetlink_done,
1028 };
0f298a28
PNA
1029#ifdef CONFIG_NF_CONNTRACK_MARK
1030 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1031 struct ctnetlink_dump_filter *filter;
1032
1033 filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1034 GFP_ATOMIC);
1035 if (filter == NULL)
1036 return -ENOMEM;
1037
1038 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1039 filter->mark.mask =
1040 ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1041 c.data = filter;
1042 }
1043#endif
80d326fa
PNA
1044 return netlink_dump_start(ctnl, skb, nlh, &c);
1045 }
c1d10adb 1046
ef00f89f
PM
1047 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1048 if (err < 0)
1049 return err;
1050
df6fb868 1051 if (cda[CTA_TUPLE_ORIG])
c1d10adb 1052 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
df6fb868 1053 else if (cda[CTA_TUPLE_REPLY])
c1d10adb
PNA
1054 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1055 else
1056 return -EINVAL;
1057
1058 if (err < 0)
1059 return err;
1060
ef00f89f 1061 h = nf_conntrack_find_get(net, zone, &tuple);
9ea8cfd6 1062 if (!h)
c1d10adb 1063 return -ENOENT;
9ea8cfd6 1064
c1d10adb
PNA
1065 ct = nf_ct_tuplehash_to_ctrack(h);
1066
1067 err = -ENOMEM;
96bcf938
PNA
1068 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1069 if (skb2 == NULL) {
c1d10adb
PNA
1070 nf_ct_put(ct);
1071 return -ENOMEM;
1072 }
c1d10adb 1073
528a3a6f 1074 rcu_read_lock();
15e47304 1075 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
80e60e67 1076 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
528a3a6f 1077 rcu_read_unlock();
c1d10adb
PNA
1078 nf_ct_put(ct);
1079 if (err <= 0)
1080 goto free;
1081
15e47304 1082 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
c1d10adb
PNA
1083 if (err < 0)
1084 goto out;
1085
c1d10adb
PNA
1086 return 0;
1087
1088free:
1089 kfree_skb(skb2);
1090out:
f31e8d49
PNA
1091 /* this avoids a loop in nfnetlink. */
1092 return err == -EAGAIN ? -ENOBUFS : err;
c1d10adb
PNA
1093}
1094
d871befe
PNA
1095static int ctnetlink_done_list(struct netlink_callback *cb)
1096{
1097 if (cb->args[1])
1098 nf_ct_put((struct nf_conn *)cb->args[1]);
1099 return 0;
1100}
1101
1102static int
1103ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb,
1104 struct hlist_nulls_head *list)
1105{
1106 struct nf_conn *ct, *last;
1107 struct nf_conntrack_tuple_hash *h;
1108 struct hlist_nulls_node *n;
1109 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1110 u_int8_t l3proto = nfmsg->nfgen_family;
1111 int res;
1112
1113 if (cb->args[2])
1114 return 0;
1115
1116 spin_lock_bh(&nf_conntrack_lock);
1117 last = (struct nf_conn *)cb->args[1];
1118restart:
1119 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1120 ct = nf_ct_tuplehash_to_ctrack(h);
1121 if (l3proto && nf_ct_l3num(ct) != l3proto)
1122 continue;
1123 if (cb->args[1]) {
1124 if (ct != last)
1125 continue;
1126 cb->args[1] = 0;
1127 }
1128 rcu_read_lock();
1129 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1130 cb->nlh->nlmsg_seq,
1131 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1132 ct);
1133 rcu_read_unlock();
1134 if (res < 0) {
1135 nf_conntrack_get(&ct->ct_general);
1136 cb->args[1] = (unsigned long)ct;
1137 goto out;
1138 }
1139 }
1140 if (cb->args[1]) {
1141 cb->args[1] = 0;
1142 goto restart;
1143 } else
1144 cb->args[2] = 1;
1145out:
1146 spin_unlock_bh(&nf_conntrack_lock);
1147 if (last)
1148 nf_ct_put(last);
1149
1150 return skb->len;
1151}
1152
1153static int
1154ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1155{
1156 struct net *net = sock_net(skb->sk);
1157
1158 return ctnetlink_dump_list(skb, cb, &net->ct.dying);
1159}
1160
1161static int
1162ctnetlink_get_ct_dying(struct sock *ctnl, struct sk_buff *skb,
1163 const struct nlmsghdr *nlh,
1164 const struct nlattr * const cda[])
1165{
1166 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1167 struct netlink_dump_control c = {
1168 .dump = ctnetlink_dump_dying,
1169 .done = ctnetlink_done_list,
1170 };
1171 return netlink_dump_start(ctnl, skb, nlh, &c);
1172 }
1173
1174 return -EOPNOTSUPP;
1175}
1176
1177static int
1178ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1179{
1180 struct net *net = sock_net(skb->sk);
1181
1182 return ctnetlink_dump_list(skb, cb, &net->ct.unconfirmed);
1183}
1184
1185static int
1186ctnetlink_get_ct_unconfirmed(struct sock *ctnl, struct sk_buff *skb,
1187 const struct nlmsghdr *nlh,
1188 const struct nlattr * const cda[])
1189{
1190 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1191 struct netlink_dump_control c = {
1192 .dump = ctnetlink_dump_unconfirmed,
1193 .done = ctnetlink_done_list,
1194 };
1195 return netlink_dump_start(ctnl, skb, nlh, &c);
1196 }
1197
1198 return -EOPNOTSUPP;
1199}
1200
67671841 1201#ifdef CONFIG_NF_NAT_NEEDED
e6a7d3c0
PNA
1202static int
1203ctnetlink_parse_nat_setup(struct nf_conn *ct,
1204 enum nf_nat_manip_type manip,
39938324 1205 const struct nlattr *attr)
e6a7d3c0
PNA
1206{
1207 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
c7232c99 1208 int err;
e6a7d3c0
PNA
1209
1210 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1211 if (!parse_nat_setup) {
95a5afca 1212#ifdef CONFIG_MODULES
e6a7d3c0
PNA
1213 rcu_read_unlock();
1214 nfnl_unlock();
c7232c99 1215 if (request_module("nf-nat") < 0) {
e6a7d3c0
PNA
1216 nfnl_lock();
1217 rcu_read_lock();
1218 return -EOPNOTSUPP;
1219 }
1220 nfnl_lock();
1221 rcu_read_lock();
1222 if (nfnetlink_parse_nat_setup_hook)
1223 return -EAGAIN;
1224#endif
1225 return -EOPNOTSUPP;
1226 }
1227
c7232c99
PM
1228 err = parse_nat_setup(ct, manip, attr);
1229 if (err == -EAGAIN) {
1230#ifdef CONFIG_MODULES
1231 rcu_read_unlock();
c7232c99
PM
1232 nfnl_unlock();
1233 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1234 nfnl_lock();
c7232c99
PM
1235 rcu_read_lock();
1236 return -EOPNOTSUPP;
1237 }
1238 nfnl_lock();
c7232c99
PM
1239 rcu_read_lock();
1240#else
1241 err = -EOPNOTSUPP;
1242#endif
1243 }
1244 return err;
e6a7d3c0 1245}
67671841 1246#endif
e6a7d3c0 1247
bb5cf80e 1248static int
39938324 1249ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
1250{
1251 unsigned long d;
77236b6e 1252 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
c1d10adb
PNA
1253 d = ct->status ^ status;
1254
1255 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1256 /* unchangeable */
0adf9d67 1257 return -EBUSY;
601e68e1 1258
c1d10adb
PNA
1259 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1260 /* SEEN_REPLY bit can only be set */
0adf9d67 1261 return -EBUSY;
601e68e1 1262
c1d10adb
PNA
1263 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1264 /* ASSURED bit can only be set */
0adf9d67 1265 return -EBUSY;
c1d10adb 1266
c1d10adb
PNA
1267 /* Be careful here, modifying NAT bits can screw up things,
1268 * so don't let users modify them directly if they don't pass
5b1158e9 1269 * nf_nat_range. */
c1d10adb
PNA
1270 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1271 return 0;
1272}
1273
e6a7d3c0 1274static int
39938324 1275ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
e6a7d3c0
PNA
1276{
1277#ifdef CONFIG_NF_NAT_NEEDED
1278 int ret;
1279
1280 if (cda[CTA_NAT_DST]) {
1281 ret = ctnetlink_parse_nat_setup(ct,
cbc9f2f4 1282 NF_NAT_MANIP_DST,
e6a7d3c0
PNA
1283 cda[CTA_NAT_DST]);
1284 if (ret < 0)
1285 return ret;
1286 }
1287 if (cda[CTA_NAT_SRC]) {
1288 ret = ctnetlink_parse_nat_setup(ct,
cbc9f2f4 1289 NF_NAT_MANIP_SRC,
e6a7d3c0
PNA
1290 cda[CTA_NAT_SRC]);
1291 if (ret < 0)
1292 return ret;
1293 }
1294 return 0;
1295#else
1296 return -EOPNOTSUPP;
1297#endif
1298}
c1d10adb
PNA
1299
1300static inline int
39938324 1301ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
1302{
1303 struct nf_conntrack_helper *helper;
dc808fe2 1304 struct nf_conn_help *help = nfct_help(ct);
29fe1b48 1305 char *helpname = NULL;
ae243bee 1306 struct nlattr *helpinfo = NULL;
c1d10adb
PNA
1307 int err;
1308
c1d10adb
PNA
1309 /* don't change helper of sibling connections */
1310 if (ct->master)
0adf9d67 1311 return -EBUSY;
c1d10adb 1312
ae243bee 1313 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
c1d10adb
PNA
1314 if (err < 0)
1315 return err;
1316
df293bbb
YK
1317 if (!strcmp(helpname, "")) {
1318 if (help && help->helper) {
c1d10adb
PNA
1319 /* we had a helper before ... */
1320 nf_ct_remove_expectations(ct);
a9b3cd7f 1321 RCU_INIT_POINTER(help->helper, NULL);
c1d10adb 1322 }
df293bbb
YK
1323
1324 return 0;
c1d10adb 1325 }
601e68e1 1326
794e6871
PM
1327 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1328 nf_ct_protonum(ct));
226c0c0e
PNA
1329 if (helper == NULL) {
1330#ifdef CONFIG_MODULES
1331 spin_unlock_bh(&nf_conntrack_lock);
1332
1333 if (request_module("nfct-helper-%s", helpname) < 0) {
1334 spin_lock_bh(&nf_conntrack_lock);
1335 return -EOPNOTSUPP;
1336 }
1337
1338 spin_lock_bh(&nf_conntrack_lock);
794e6871
PM
1339 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1340 nf_ct_protonum(ct));
226c0c0e
PNA
1341 if (helper)
1342 return -EAGAIN;
1343#endif
0adf9d67 1344 return -EOPNOTSUPP;
226c0c0e 1345 }
df293bbb 1346
ceceae1b 1347 if (help) {
ae243bee
PNA
1348 if (help->helper == helper) {
1349 /* update private helper data if allowed. */
7be54ca4 1350 if (helper->from_nlattr)
ae243bee 1351 helper->from_nlattr(helpinfo, ct);
ceceae1b 1352 return 0;
fd7462de 1353 } else
ceceae1b 1354 return -EBUSY;
ceceae1b 1355 }
df293bbb 1356
fd7462de
PNA
1357 /* we cannot set a helper for an existing conntrack */
1358 return -EOPNOTSUPP;
c1d10adb
PNA
1359}
1360
1361static inline int
39938324 1362ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1363{
77236b6e 1364 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
601e68e1 1365
c1d10adb
PNA
1366 if (!del_timer(&ct->timeout))
1367 return -ETIME;
1368
1369 ct->timeout.expires = jiffies + timeout * HZ;
1370 add_timer(&ct->timeout);
1371
1372 return 0;
1373}
1374
d0b0268f
PM
1375static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1376 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1377 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1378 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1379};
1380
c1d10adb 1381static inline int
39938324 1382ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1383{
39938324
PM
1384 const struct nlattr *attr = cda[CTA_PROTOINFO];
1385 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
605dcad6 1386 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
1387 int err = 0;
1388
d0b0268f 1389 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
c1d10adb 1390
cd91566e
FW
1391 rcu_read_lock();
1392 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
fdf70832
PM
1393 if (l4proto->from_nlattr)
1394 err = l4proto->from_nlattr(tb, ct);
cd91566e 1395 rcu_read_unlock();
c1d10adb
PNA
1396
1397 return err;
1398}
1399
13eae15a 1400#ifdef CONFIG_NF_NAT_NEEDED
d0b0268f
PM
1401static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1402 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1403 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1404 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1405};
1406
13eae15a 1407static inline int
39938324 1408change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
13eae15a
PNA
1409{
1410 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1411
d0b0268f 1412 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
13eae15a
PNA
1413
1414 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1415 return -EINVAL;
1416
1417 natseq->correction_pos =
77236b6e 1418 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
13eae15a
PNA
1419
1420 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1421 return -EINVAL;
1422
1423 natseq->offset_before =
77236b6e 1424 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
13eae15a
PNA
1425
1426 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1427 return -EINVAL;
1428
1429 natseq->offset_after =
77236b6e 1430 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
13eae15a
PNA
1431
1432 return 0;
1433}
1434
1435static int
39938324
PM
1436ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1437 const struct nlattr * const cda[])
13eae15a
PNA
1438{
1439 int ret = 0;
1440 struct nf_conn_nat *nat = nfct_nat(ct);
1441
1442 if (!nat)
1443 return 0;
1444
1445 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1446 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1447 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1448 if (ret < 0)
1449 return ret;
1450
1451 ct->status |= IPS_SEQ_ADJUST;
1452 }
1453
1454 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1455 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1456 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1457 if (ret < 0)
1458 return ret;
1459
1460 ct->status |= IPS_SEQ_ADJUST;
1461 }
1462
1463 return 0;
1464}
1465#endif
1466
c1d10adb 1467static int
39938324
PM
1468ctnetlink_change_conntrack(struct nf_conn *ct,
1469 const struct nlattr * const cda[])
c1d10adb
PNA
1470{
1471 int err;
1472
e098360f
PNA
1473 /* only allow NAT changes and master assignation for new conntracks */
1474 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1475 return -EOPNOTSUPP;
1476
df6fb868 1477 if (cda[CTA_HELP]) {
c1d10adb
PNA
1478 err = ctnetlink_change_helper(ct, cda);
1479 if (err < 0)
1480 return err;
1481 }
1482
df6fb868 1483 if (cda[CTA_TIMEOUT]) {
c1d10adb
PNA
1484 err = ctnetlink_change_timeout(ct, cda);
1485 if (err < 0)
1486 return err;
1487 }
1488
df6fb868 1489 if (cda[CTA_STATUS]) {
c1d10adb
PNA
1490 err = ctnetlink_change_status(ct, cda);
1491 if (err < 0)
1492 return err;
1493 }
1494
df6fb868 1495 if (cda[CTA_PROTOINFO]) {
c1d10adb
PNA
1496 err = ctnetlink_change_protoinfo(ct, cda);
1497 if (err < 0)
1498 return err;
1499 }
1500
bcd1e830 1501#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1502 if (cda[CTA_MARK])
77236b6e 1503 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1504#endif
1505
13eae15a
PNA
1506#ifdef CONFIG_NF_NAT_NEEDED
1507 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1508 err = ctnetlink_change_nat_seq_adj(ct, cda);
1509 if (err < 0)
1510 return err;
1511 }
1512#endif
1513
c1d10adb
PNA
1514 return 0;
1515}
1516
f0a3c086 1517static struct nf_conn *
ef00f89f 1518ctnetlink_create_conntrack(struct net *net, u16 zone,
9592a5c0 1519 const struct nlattr * const cda[],
c1d10adb 1520 struct nf_conntrack_tuple *otuple,
5faa1f4c 1521 struct nf_conntrack_tuple *rtuple,
7ec47496 1522 u8 u3)
c1d10adb
PNA
1523{
1524 struct nf_conn *ct;
1525 int err = -EINVAL;
ceceae1b 1526 struct nf_conntrack_helper *helper;
315c34da 1527 struct nf_conn_tstamp *tstamp;
c1d10adb 1528
ef00f89f 1529 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
cd7fcbf1 1530 if (IS_ERR(ct))
f0a3c086 1531 return ERR_PTR(-ENOMEM);
c1d10adb 1532
df6fb868 1533 if (!cda[CTA_TIMEOUT])
0f5b3e85 1534 goto err1;
77236b6e 1535 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
c1d10adb
PNA
1536
1537 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
c1d10adb 1538
1575e7ea 1539 rcu_read_lock();
226c0c0e 1540 if (cda[CTA_HELP]) {
29fe1b48 1541 char *helpname = NULL;
ae243bee
PNA
1542 struct nlattr *helpinfo = NULL;
1543
1544 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
0f5b3e85
PM
1545 if (err < 0)
1546 goto err2;
226c0c0e 1547
794e6871
PM
1548 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1549 nf_ct_protonum(ct));
226c0c0e
PNA
1550 if (helper == NULL) {
1551 rcu_read_unlock();
1552#ifdef CONFIG_MODULES
1553 if (request_module("nfct-helper-%s", helpname) < 0) {
1554 err = -EOPNOTSUPP;
0f5b3e85 1555 goto err1;
226c0c0e
PNA
1556 }
1557
1558 rcu_read_lock();
794e6871
PM
1559 helper = __nf_conntrack_helper_find(helpname,
1560 nf_ct_l3num(ct),
1561 nf_ct_protonum(ct));
226c0c0e 1562 if (helper) {
226c0c0e 1563 err = -EAGAIN;
0f5b3e85 1564 goto err2;
226c0c0e
PNA
1565 }
1566 rcu_read_unlock();
1567#endif
1568 err = -EOPNOTSUPP;
0f5b3e85 1569 goto err1;
226c0c0e
PNA
1570 } else {
1571 struct nf_conn_help *help;
1572
1afc5679 1573 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
226c0c0e 1574 if (help == NULL) {
226c0c0e 1575 err = -ENOMEM;
0f5b3e85 1576 goto err2;
226c0c0e 1577 }
ae243bee 1578 /* set private helper data if allowed. */
7be54ca4 1579 if (helper->from_nlattr)
ae243bee 1580 helper->from_nlattr(helpinfo, ct);
226c0c0e
PNA
1581
1582 /* not in hash table yet so not strictly necessary */
a9b3cd7f 1583 RCU_INIT_POINTER(help->helper, helper);
226c0c0e
PNA
1584 }
1585 } else {
1586 /* try an implicit helper assignation */
b2a15a60 1587 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
0f5b3e85
PM
1588 if (err < 0)
1589 goto err2;
1575e7ea
PNA
1590 }
1591
a88e22ad
PNA
1592 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1593 err = ctnetlink_change_nat(ct, cda);
0f5b3e85
PM
1594 if (err < 0)
1595 goto err2;
e6a7d3c0
PNA
1596 }
1597
a88e22ad 1598 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
a992ca2a 1599 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
a88e22ad
PNA
1600 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1601 /* we must add conntrack extensions before confirmation. */
1602 ct->status |= IPS_CONFIRMED;
1603
1604 if (cda[CTA_STATUS]) {
1605 err = ctnetlink_change_status(ct, cda);
0f5b3e85
PM
1606 if (err < 0)
1607 goto err2;
bbb3357d 1608 }
c1d10adb 1609
c969aa7d
PNA
1610#ifdef CONFIG_NF_NAT_NEEDED
1611 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1612 err = ctnetlink_change_nat_seq_adj(ct, cda);
0f5b3e85
PM
1613 if (err < 0)
1614 goto err2;
c969aa7d
PNA
1615 }
1616#endif
1617
e5fc9e7a 1618 memset(&ct->proto, 0, sizeof(ct->proto));
df6fb868 1619 if (cda[CTA_PROTOINFO]) {
c1d10adb 1620 err = ctnetlink_change_protoinfo(ct, cda);
0f5b3e85
PM
1621 if (err < 0)
1622 goto err2;
c1d10adb
PNA
1623 }
1624
bcd1e830 1625#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1626 if (cda[CTA_MARK])
77236b6e 1627 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1628#endif
1629
5faa1f4c 1630 /* setup master conntrack: this is a confirmed expectation */
7ec47496
PNA
1631 if (cda[CTA_TUPLE_MASTER]) {
1632 struct nf_conntrack_tuple master;
1633 struct nf_conntrack_tuple_hash *master_h;
1634 struct nf_conn *master_ct;
1635
1636 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1637 if (err < 0)
0f5b3e85 1638 goto err2;
7ec47496 1639
ef00f89f 1640 master_h = nf_conntrack_find_get(net, zone, &master);
7ec47496
PNA
1641 if (master_h == NULL) {
1642 err = -ENOENT;
0f5b3e85 1643 goto err2;
7ec47496
PNA
1644 }
1645 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
f2a89004 1646 __set_bit(IPS_EXPECTED_BIT, &ct->status);
5faa1f4c 1647 ct->master = master_ct;
f2a89004 1648 }
315c34da
PNA
1649 tstamp = nf_conn_tstamp_find(ct);
1650 if (tstamp)
1651 tstamp->start = ktime_to_ns(ktime_get_real());
5faa1f4c 1652
7d367e06
JK
1653 err = nf_conntrack_hash_check_insert(ct);
1654 if (err < 0)
1655 goto err2;
1656
58a3c9bb 1657 rcu_read_unlock();
dafc741c 1658
f0a3c086 1659 return ct;
c1d10adb 1660
0f5b3e85
PM
1661err2:
1662 rcu_read_unlock();
1663err1:
c1d10adb 1664 nf_conntrack_free(ct);
f0a3c086 1665 return ERR_PTR(err);
c1d10adb
PNA
1666}
1667
601e68e1
YH
1668static int
1669ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1670 const struct nlmsghdr *nlh,
1671 const struct nlattr * const cda[])
c1d10adb 1672{
9592a5c0 1673 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1674 struct nf_conntrack_tuple otuple, rtuple;
1675 struct nf_conntrack_tuple_hash *h = NULL;
96bcf938 1676 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7d367e06 1677 struct nf_conn *ct;
c1d10adb 1678 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1679 u16 zone;
1680 int err;
1681
1682 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1683 if (err < 0)
1684 return err;
c1d10adb 1685
df6fb868 1686 if (cda[CTA_TUPLE_ORIG]) {
c1d10adb
PNA
1687 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1688 if (err < 0)
1689 return err;
1690 }
1691
df6fb868 1692 if (cda[CTA_TUPLE_REPLY]) {
c1d10adb
PNA
1693 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1694 if (err < 0)
1695 return err;
1696 }
1697
df6fb868 1698 if (cda[CTA_TUPLE_ORIG])
7d367e06 1699 h = nf_conntrack_find_get(net, zone, &otuple);
df6fb868 1700 else if (cda[CTA_TUPLE_REPLY])
7d367e06 1701 h = nf_conntrack_find_get(net, zone, &rtuple);
c1d10adb
PNA
1702
1703 if (h == NULL) {
c1d10adb 1704 err = -ENOENT;
f0a3c086 1705 if (nlh->nlmsg_flags & NLM_F_CREATE) {
fecc1133 1706 enum ip_conntrack_events events;
5faa1f4c 1707
ef00f89f 1708 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
f0a3c086 1709 &rtuple, u3);
7d367e06
JK
1710 if (IS_ERR(ct))
1711 return PTR_ERR(ct);
1712
f0a3c086 1713 err = 0;
fecc1133
PNA
1714 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1715 events = IPCT_RELATED;
1716 else
1717 events = IPCT_NEW;
1718
858b3133
PM
1719 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1720 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1721 (1 << IPCT_HELPER) |
1722 (1 << IPCT_PROTOINFO) |
1723 (1 << IPCT_NATSEQADJ) |
1724 (1 << IPCT_MARK) | events,
15e47304 1725 ct, NETLINK_CB(skb).portid,
a0891aa6 1726 nlmsg_report(nlh));
f0a3c086 1727 nf_ct_put(ct);
7d367e06 1728 }
5faa1f4c 1729
c1d10adb
PNA
1730 return err;
1731 }
1732 /* implicit 'else' */
1733
c1d10adb 1734 err = -EEXIST;
7d367e06 1735 ct = nf_ct_tuplehash_to_ctrack(h);
ff4ca827 1736 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
7d367e06 1737 spin_lock_bh(&nf_conntrack_lock);
19abb7b0 1738 err = ctnetlink_change_conntrack(ct, cda);
7d367e06 1739 spin_unlock_bh(&nf_conntrack_lock);
19abb7b0 1740 if (err == 0) {
858b3133
PM
1741 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1742 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1743 (1 << IPCT_HELPER) |
1744 (1 << IPCT_PROTOINFO) |
1745 (1 << IPCT_NATSEQADJ) |
1746 (1 << IPCT_MARK),
15e47304 1747 ct, NETLINK_CB(skb).portid,
a0891aa6 1748 nlmsg_report(nlh));
7d367e06 1749 }
ff4ca827 1750 }
c1d10adb 1751
7d367e06 1752 nf_ct_put(ct);
c1d10adb
PNA
1753 return err;
1754}
1755
392025f8 1756static int
15e47304 1757ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
392025f8
PNA
1758 __u16 cpu, const struct ip_conntrack_stat *st)
1759{
1760 struct nlmsghdr *nlh;
1761 struct nfgenmsg *nfmsg;
15e47304 1762 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
392025f8
PNA
1763
1764 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
15e47304 1765 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
392025f8
PNA
1766 if (nlh == NULL)
1767 goto nlmsg_failure;
1768
1769 nfmsg = nlmsg_data(nlh);
1770 nfmsg->nfgen_family = AF_UNSPEC;
1771 nfmsg->version = NFNETLINK_V0;
1772 nfmsg->res_id = htons(cpu);
1773
1774 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1775 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1776 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1777 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1778 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1779 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1780 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1781 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1782 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1783 htonl(st->insert_failed)) ||
1784 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1785 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1786 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1787 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1788 htonl(st->search_restart)))
1789 goto nla_put_failure;
1790
1791 nlmsg_end(skb, nlh);
1792 return skb->len;
1793
1794nla_put_failure:
1795nlmsg_failure:
1796 nlmsg_cancel(skb, nlh);
1797 return -1;
1798}
1799
1800static int
1801ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
1802{
1803 int cpu;
1804 struct net *net = sock_net(skb->sk);
1805
1806 if (cb->args[0] == nr_cpu_ids)
1807 return 0;
1808
1809 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1810 const struct ip_conntrack_stat *st;
1811
1812 if (!cpu_possible(cpu))
1813 continue;
1814
1815 st = per_cpu_ptr(net->ct.stat, cpu);
1816 if (ctnetlink_ct_stat_cpu_fill_info(skb,
15e47304 1817 NETLINK_CB(cb->skb).portid,
392025f8
PNA
1818 cb->nlh->nlmsg_seq,
1819 cpu, st) < 0)
1820 break;
1821 }
1822 cb->args[0] = cpu;
1823
1824 return skb->len;
1825}
1826
1827static int
1828ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb,
1829 const struct nlmsghdr *nlh,
1830 const struct nlattr * const cda[])
1831{
1832 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1833 struct netlink_dump_control c = {
1834 .dump = ctnetlink_ct_stat_cpu_dump,
1835 };
1836 return netlink_dump_start(ctnl, skb, nlh, &c);
1837 }
1838
1839 return 0;
1840}
1841
1842static int
15e47304 1843ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
392025f8
PNA
1844 struct net *net)
1845{
1846 struct nlmsghdr *nlh;
1847 struct nfgenmsg *nfmsg;
15e47304 1848 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
392025f8
PNA
1849 unsigned int nr_conntracks = atomic_read(&net->ct.count);
1850
1851 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
15e47304 1852 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
392025f8
PNA
1853 if (nlh == NULL)
1854 goto nlmsg_failure;
1855
1856 nfmsg = nlmsg_data(nlh);
1857 nfmsg->nfgen_family = AF_UNSPEC;
1858 nfmsg->version = NFNETLINK_V0;
1859 nfmsg->res_id = 0;
1860
1861 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
1862 goto nla_put_failure;
1863
1864 nlmsg_end(skb, nlh);
1865 return skb->len;
1866
1867nla_put_failure:
1868nlmsg_failure:
1869 nlmsg_cancel(skb, nlh);
1870 return -1;
1871}
1872
1873static int
1874ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb,
1875 const struct nlmsghdr *nlh,
1876 const struct nlattr * const cda[])
1877{
1878 struct sk_buff *skb2;
1879 int err;
1880
1881 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1882 if (skb2 == NULL)
1883 return -ENOMEM;
1884
15e47304 1885 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
392025f8
PNA
1886 nlh->nlmsg_seq,
1887 NFNL_MSG_TYPE(nlh->nlmsg_type),
1888 sock_net(skb->sk));
1889 if (err <= 0)
1890 goto free;
1891
15e47304 1892 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
392025f8
PNA
1893 if (err < 0)
1894 goto out;
1895
1896 return 0;
1897
1898free:
1899 kfree_skb(skb2);
1900out:
1901 /* this avoids a loop in nfnetlink. */
1902 return err == -EAGAIN ? -ENOBUFS : err;
1903}
1904
7c622345 1905#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
9cb01766
PNA
1906static size_t
1907ctnetlink_nfqueue_build_size(const struct nf_conn *ct)
1908{
1909 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
1910 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
1911 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
1912 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
1913 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
1914 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
1915 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
1916 + nla_total_size(0) /* CTA_PROTOINFO */
1917 + nla_total_size(0) /* CTA_HELP */
1918 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
1919 + ctnetlink_secctx_size(ct)
1920#ifdef CONFIG_NF_NAT_NEEDED
1921 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
1922 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
1923#endif
1924#ifdef CONFIG_NF_CONNTRACK_MARK
1925 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
1926#endif
1927 + ctnetlink_proto_size(ct)
1928 ;
1929}
1930
1931static int
1932ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct)
1933{
1934 struct nlattr *nest_parms;
1935
1936 rcu_read_lock();
1937 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
1938 if (!nest_parms)
1939 goto nla_put_failure;
1940 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
1941 goto nla_put_failure;
1942 nla_nest_end(skb, nest_parms);
1943
1944 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
1945 if (!nest_parms)
1946 goto nla_put_failure;
1947 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
1948 goto nla_put_failure;
1949 nla_nest_end(skb, nest_parms);
1950
1951 if (nf_ct_zone(ct)) {
1952 if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
1953 goto nla_put_failure;
1954 }
1955
1956 if (ctnetlink_dump_id(skb, ct) < 0)
1957 goto nla_put_failure;
1958
1959 if (ctnetlink_dump_status(skb, ct) < 0)
1960 goto nla_put_failure;
1961
1962 if (ctnetlink_dump_timeout(skb, ct) < 0)
1963 goto nla_put_failure;
1964
1965 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
1966 goto nla_put_failure;
1967
1968 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
1969 goto nla_put_failure;
1970
1971#ifdef CONFIG_NF_CONNTRACK_SECMARK
1972 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
1973 goto nla_put_failure;
1974#endif
1975 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
1976 goto nla_put_failure;
1977
1978 if ((ct->status & IPS_SEQ_ADJUST) &&
1979 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
1980 goto nla_put_failure;
1981
1982#ifdef CONFIG_NF_CONNTRACK_MARK
1983 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
1984 goto nla_put_failure;
1985#endif
1986 rcu_read_unlock();
1987 return 0;
1988
1989nla_put_failure:
1990 rcu_read_unlock();
1991 return -ENOSPC;
1992}
1993
1994static int
1995ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
1996{
1997 int err;
1998
1999 if (cda[CTA_TIMEOUT]) {
2000 err = ctnetlink_change_timeout(ct, cda);
2001 if (err < 0)
2002 return err;
2003 }
2004 if (cda[CTA_STATUS]) {
2005 err = ctnetlink_change_status(ct, cda);
2006 if (err < 0)
2007 return err;
2008 }
2009 if (cda[CTA_HELP]) {
2010 err = ctnetlink_change_helper(ct, cda);
2011 if (err < 0)
2012 return err;
2013 }
2014#if defined(CONFIG_NF_CONNTRACK_MARK)
2015 if (cda[CTA_MARK])
2016 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2017#endif
2018 return 0;
2019}
2020
2021static int
2022ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct)
2023{
2024 struct nlattr *cda[CTA_MAX+1];
68e035c9 2025 int ret;
9cb01766
PNA
2026
2027 nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2028
68e035c9
PNA
2029 spin_lock_bh(&nf_conntrack_lock);
2030 ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct);
2031 spin_unlock_bh(&nf_conntrack_lock);
2032
2033 return ret;
9cb01766
PNA
2034}
2035
2036static struct nfq_ct_hook ctnetlink_nfqueue_hook = {
2037 .build_size = ctnetlink_nfqueue_build_size,
2038 .build = ctnetlink_nfqueue_build,
2039 .parse = ctnetlink_nfqueue_parse,
2040};
7c622345 2041#endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
9cb01766 2042
601e68e1
YH
2043/***********************************************************************
2044 * EXPECT
2045 ***********************************************************************/
c1d10adb
PNA
2046
2047static inline int
2048ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2049 const struct nf_conntrack_tuple *tuple,
2050 enum ctattr_expect type)
2051{
df6fb868 2052 struct nlattr *nest_parms;
601e68e1 2053
df6fb868
PM
2054 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2055 if (!nest_parms)
2056 goto nla_put_failure;
c1d10adb 2057 if (ctnetlink_dump_tuples(skb, tuple) < 0)
df6fb868
PM
2058 goto nla_put_failure;
2059 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
2060
2061 return 0;
2062
df6fb868 2063nla_put_failure:
c1d10adb 2064 return -1;
601e68e1 2065}
c1d10adb 2066
1cde6436
PNA
2067static inline int
2068ctnetlink_exp_dump_mask(struct sk_buff *skb,
2069 const struct nf_conntrack_tuple *tuple,
d4156e8c 2070 const struct nf_conntrack_tuple_mask *mask)
1cde6436
PNA
2071{
2072 int ret;
2073 struct nf_conntrack_l3proto *l3proto;
605dcad6 2074 struct nf_conntrack_l4proto *l4proto;
d4156e8c 2075 struct nf_conntrack_tuple m;
df6fb868 2076 struct nlattr *nest_parms;
d4156e8c
PM
2077
2078 memset(&m, 0xFF, sizeof(m));
d4156e8c 2079 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
e578756c
PM
2080 m.src.u.all = mask->src.u.all;
2081 m.dst.protonum = tuple->dst.protonum;
d4156e8c 2082
df6fb868
PM
2083 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2084 if (!nest_parms)
2085 goto nla_put_failure;
1cde6436 2086
3b988ece 2087 rcu_read_lock();
528a3a6f 2088 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
d4156e8c 2089 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
3b988ece
HS
2090 if (ret >= 0) {
2091 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2092 tuple->dst.protonum);
d4156e8c 2093 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
3b988ece
HS
2094 }
2095 rcu_read_unlock();
2096
1cde6436 2097 if (unlikely(ret < 0))
df6fb868 2098 goto nla_put_failure;
1cde6436 2099
df6fb868 2100 nla_nest_end(skb, nest_parms);
1cde6436
PNA
2101
2102 return 0;
2103
df6fb868 2104nla_put_failure:
1cde6436
PNA
2105 return -1;
2106}
2107
c7232c99
PM
2108static const union nf_inet_addr any_addr;
2109
bb5cf80e 2110static int
c1d10adb 2111ctnetlink_exp_dump_expect(struct sk_buff *skb,
601e68e1 2112 const struct nf_conntrack_expect *exp)
c1d10adb
PNA
2113{
2114 struct nf_conn *master = exp->master;
c1216382 2115 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
bc01befd 2116 struct nf_conn_help *help;
076a0ca0
PNA
2117#ifdef CONFIG_NF_NAT_NEEDED
2118 struct nlattr *nest_parms;
2119 struct nf_conntrack_tuple nat_tuple = {};
2120#endif
544d5c7d
PNA
2121 struct nf_ct_helper_expectfn *expfn;
2122
d978e5da
PM
2123 if (timeout < 0)
2124 timeout = 0;
c1d10adb
PNA
2125
2126 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
df6fb868 2127 goto nla_put_failure;
1cde6436 2128 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
df6fb868 2129 goto nla_put_failure;
c1d10adb
PNA
2130 if (ctnetlink_exp_dump_tuple(skb,
2131 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2132 CTA_EXPECT_MASTER) < 0)
df6fb868 2133 goto nla_put_failure;
601e68e1 2134
076a0ca0 2135#ifdef CONFIG_NF_NAT_NEEDED
c7232c99
PM
2136 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2137 exp->saved_proto.all) {
076a0ca0
PNA
2138 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2139 if (!nest_parms)
2140 goto nla_put_failure;
2141
cc1eb431
DM
2142 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2143 goto nla_put_failure;
076a0ca0
PNA
2144
2145 nat_tuple.src.l3num = nf_ct_l3num(master);
c7232c99 2146 nat_tuple.src.u3 = exp->saved_addr;
076a0ca0
PNA
2147 nat_tuple.dst.protonum = nf_ct_protonum(master);
2148 nat_tuple.src.u = exp->saved_proto;
2149
2150 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2151 CTA_EXPECT_NAT_TUPLE) < 0)
2152 goto nla_put_failure;
2153 nla_nest_end(skb, nest_parms);
2154 }
2155#endif
cc1eb431
DM
2156 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2157 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2158 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2159 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2160 goto nla_put_failure;
bc01befd
PNA
2161 help = nfct_help(master);
2162 if (help) {
2163 struct nf_conntrack_helper *helper;
2164
2165 helper = rcu_dereference(help->helper);
cc1eb431
DM
2166 if (helper &&
2167 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2168 goto nla_put_failure;
bc01befd 2169 }
544d5c7d 2170 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
cc1eb431
DM
2171 if (expfn != NULL &&
2172 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2173 goto nla_put_failure;
c1d10adb
PNA
2174
2175 return 0;
601e68e1 2176
df6fb868 2177nla_put_failure:
c1d10adb
PNA
2178 return -1;
2179}
2180
2181static int
15e47304 2182ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
8b0a231d 2183 int event, const struct nf_conntrack_expect *exp)
c1d10adb
PNA
2184{
2185 struct nlmsghdr *nlh;
2186 struct nfgenmsg *nfmsg;
15e47304 2187 unsigned int flags = portid ? NLM_F_MULTI : 0;
c1d10adb
PNA
2188
2189 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
15e47304 2190 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
96bcf938
PNA
2191 if (nlh == NULL)
2192 goto nlmsg_failure;
c1d10adb 2193
96bcf938 2194 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
2195 nfmsg->nfgen_family = exp->tuple.src.l3num;
2196 nfmsg->version = NFNETLINK_V0;
2197 nfmsg->res_id = 0;
2198
2199 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 2200 goto nla_put_failure;
c1d10adb 2201
96bcf938 2202 nlmsg_end(skb, nlh);
c1d10adb
PNA
2203 return skb->len;
2204
2205nlmsg_failure:
df6fb868 2206nla_put_failure:
96bcf938 2207 nlmsg_cancel(skb, nlh);
c1d10adb
PNA
2208 return -1;
2209}
2210
2211#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
2212static int
2213ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
c1d10adb 2214{
9592a5c0
AD
2215 struct nf_conntrack_expect *exp = item->exp;
2216 struct net *net = nf_ct_exp_net(exp);
c1d10adb
PNA
2217 struct nlmsghdr *nlh;
2218 struct nfgenmsg *nfmsg;
c1d10adb 2219 struct sk_buff *skb;
ebbf41df 2220 unsigned int type, group;
c1d10adb
PNA
2221 int flags = 0;
2222
ebbf41df
PNA
2223 if (events & (1 << IPEXP_DESTROY)) {
2224 type = IPCTNL_MSG_EXP_DELETE;
2225 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2226 } else if (events & (1 << IPEXP_NEW)) {
c1d10adb
PNA
2227 type = IPCTNL_MSG_EXP_NEW;
2228 flags = NLM_F_CREATE|NLM_F_EXCL;
ebbf41df 2229 group = NFNLGRP_CONNTRACK_EXP_NEW;
c1d10adb 2230 } else
e34d5c1a 2231 return 0;
c1d10adb 2232
ebbf41df 2233 if (!item->report && !nfnetlink_has_listeners(net, group))
e34d5c1a 2234 return 0;
b3a27bfb 2235
96bcf938
PNA
2236 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2237 if (skb == NULL)
150ace0d 2238 goto errout;
c1d10adb 2239
b633ad5f 2240 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
15e47304 2241 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
96bcf938
PNA
2242 if (nlh == NULL)
2243 goto nlmsg_failure;
c1d10adb 2244
96bcf938 2245 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
2246 nfmsg->nfgen_family = exp->tuple.src.l3num;
2247 nfmsg->version = NFNETLINK_V0;
2248 nfmsg->res_id = 0;
2249
528a3a6f 2250 rcu_read_lock();
c1d10adb 2251 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 2252 goto nla_put_failure;
528a3a6f 2253 rcu_read_unlock();
c1d10adb 2254
96bcf938 2255 nlmsg_end(skb, nlh);
15e47304 2256 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
e34d5c1a 2257 return 0;
c1d10adb 2258
df6fb868 2259nla_put_failure:
528a3a6f 2260 rcu_read_unlock();
96bcf938 2261 nlmsg_cancel(skb, nlh);
528a3a6f 2262nlmsg_failure:
c1d10adb 2263 kfree_skb(skb);
150ace0d 2264errout:
9592a5c0 2265 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
e34d5c1a 2266 return 0;
c1d10adb
PNA
2267}
2268#endif
cf6994c2
PM
2269static int ctnetlink_exp_done(struct netlink_callback *cb)
2270{
31f15875
PM
2271 if (cb->args[1])
2272 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
cf6994c2
PM
2273 return 0;
2274}
c1d10adb
PNA
2275
2276static int
2277ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2278{
9592a5c0 2279 struct net *net = sock_net(skb->sk);
cf6994c2 2280 struct nf_conntrack_expect *exp, *last;
96bcf938 2281 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
31f15875 2282 struct hlist_node *n;
87711cb8 2283 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 2284
7d0742da 2285 rcu_read_lock();
31f15875
PM
2286 last = (struct nf_conntrack_expect *)cb->args[1];
2287 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
cf6994c2 2288restart:
9b03f38d 2289 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
31f15875
PM
2290 hnode) {
2291 if (l3proto && exp->tuple.src.l3num != l3proto)
cf6994c2 2292 continue;
31f15875
PM
2293 if (cb->args[1]) {
2294 if (exp != last)
2295 continue;
2296 cb->args[1] = 0;
2297 }
8b0a231d 2298 if (ctnetlink_exp_fill_info(skb,
15e47304 2299 NETLINK_CB(cb->skb).portid,
31f15875
PM
2300 cb->nlh->nlmsg_seq,
2301 IPCTNL_MSG_EXP_NEW,
8b0a231d 2302 exp) < 0) {
7d0742da
PM
2303 if (!atomic_inc_not_zero(&exp->use))
2304 continue;
31f15875
PM
2305 cb->args[1] = (unsigned long)exp;
2306 goto out;
2307 }
cf6994c2 2308 }
31f15875
PM
2309 if (cb->args[1]) {
2310 cb->args[1] = 0;
2311 goto restart;
cf6994c2
PM
2312 }
2313 }
601e68e1 2314out:
7d0742da 2315 rcu_read_unlock();
cf6994c2
PM
2316 if (last)
2317 nf_ct_expect_put(last);
c1d10adb 2318
c1d10adb
PNA
2319 return skb->len;
2320}
2321
f73e924c 2322static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
d0b0268f
PM
2323 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2324 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2325 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
f73e924c
PM
2326 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2327 [CTA_EXPECT_ID] = { .type = NLA_U32 },
6d1fafca
FW
2328 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2329 .len = NF_CT_HELPER_NAME_LEN - 1 },
bcac0dfa 2330 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
8b008faf 2331 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
b8c5e52c 2332 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
076a0ca0 2333 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
544d5c7d 2334 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
c1d10adb
PNA
2335};
2336
2337static int
601e68e1 2338ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2339 const struct nlmsghdr *nlh,
2340 const struct nlattr * const cda[])
c1d10adb 2341{
9592a5c0 2342 struct net *net = sock_net(ctnl);
c1d10adb
PNA
2343 struct nf_conntrack_tuple tuple;
2344 struct nf_conntrack_expect *exp;
2345 struct sk_buff *skb2;
96bcf938 2346 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 2347 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
2348 u16 zone;
2349 int err;
c1d10adb 2350
b8f3ab42 2351 if (nlh->nlmsg_flags & NLM_F_DUMP) {
80d326fa
PNA
2352 struct netlink_dump_control c = {
2353 .dump = ctnetlink_exp_dump_table,
2354 .done = ctnetlink_exp_done,
2355 };
2356 return netlink_dump_start(ctnl, skb, nlh, &c);
c1d10adb
PNA
2357 }
2358
ef00f89f
PM
2359 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2360 if (err < 0)
2361 return err;
2362
35dba1d7
PNA
2363 if (cda[CTA_EXPECT_TUPLE])
2364 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2365 else if (cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
2366 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2367 else
2368 return -EINVAL;
2369
2370 if (err < 0)
2371 return err;
2372
ef00f89f 2373 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
2374 if (!exp)
2375 return -ENOENT;
2376
df6fb868 2377 if (cda[CTA_EXPECT_ID]) {
77236b6e 2378 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 2379 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 2380 nf_ct_expect_put(exp);
c1d10adb
PNA
2381 return -ENOENT;
2382 }
601e68e1 2383 }
c1d10adb
PNA
2384
2385 err = -ENOMEM;
96bcf938 2386 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
81378f72
PNA
2387 if (skb2 == NULL) {
2388 nf_ct_expect_put(exp);
c1d10adb 2389 goto out;
81378f72 2390 }
4e9b8269 2391
528a3a6f 2392 rcu_read_lock();
15e47304 2393 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
8b0a231d 2394 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
528a3a6f 2395 rcu_read_unlock();
81378f72 2396 nf_ct_expect_put(exp);
c1d10adb
PNA
2397 if (err <= 0)
2398 goto free;
2399
15e47304 2400 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
81378f72
PNA
2401 if (err < 0)
2402 goto out;
c1d10adb 2403
81378f72 2404 return 0;
c1d10adb
PNA
2405
2406free:
2407 kfree_skb(skb2);
2408out:
81378f72
PNA
2409 /* this avoids a loop in nfnetlink. */
2410 return err == -EAGAIN ? -ENOBUFS : err;
c1d10adb
PNA
2411}
2412
2413static int
601e68e1 2414ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2415 const struct nlmsghdr *nlh,
2416 const struct nlattr * const cda[])
c1d10adb 2417{
9592a5c0 2418 struct net *net = sock_net(ctnl);
31f15875 2419 struct nf_conntrack_expect *exp;
c1d10adb 2420 struct nf_conntrack_tuple tuple;
96bcf938 2421 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
31f15875 2422 struct hlist_node *n, *next;
c1d10adb 2423 u_int8_t u3 = nfmsg->nfgen_family;
31f15875 2424 unsigned int i;
ef00f89f 2425 u16 zone;
c1d10adb
PNA
2426 int err;
2427
df6fb868 2428 if (cda[CTA_EXPECT_TUPLE]) {
c1d10adb 2429 /* delete a single expect by tuple */
ef00f89f
PM
2430 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2431 if (err < 0)
2432 return err;
2433
c1d10adb
PNA
2434 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2435 if (err < 0)
2436 return err;
2437
2438 /* bump usage count to 2 */
ef00f89f 2439 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
2440 if (!exp)
2441 return -ENOENT;
2442
df6fb868 2443 if (cda[CTA_EXPECT_ID]) {
77236b6e 2444 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 2445 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 2446 nf_ct_expect_put(exp);
c1d10adb
PNA
2447 return -ENOENT;
2448 }
2449 }
2450
2451 /* after list removal, usage count == 1 */
ebbf41df
PNA
2452 spin_lock_bh(&nf_conntrack_lock);
2453 if (del_timer(&exp->timeout)) {
15e47304 2454 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
ebbf41df
PNA
2455 nlmsg_report(nlh));
2456 nf_ct_expect_put(exp);
2457 }
2458 spin_unlock_bh(&nf_conntrack_lock);
601e68e1 2459 /* have to put what we 'get' above.
c1d10adb 2460 * after this line usage count == 0 */
6823645d 2461 nf_ct_expect_put(exp);
df6fb868
PM
2462 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2463 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
31f15875 2464 struct nf_conn_help *m_help;
c1d10adb
PNA
2465
2466 /* delete all expectations for this helper */
f8ba1aff 2467 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
2468 for (i = 0; i < nf_ct_expect_hsize; i++) {
2469 hlist_for_each_entry_safe(exp, n, next,
9592a5c0 2470 &net->ct.expect_hash[i],
31f15875
PM
2471 hnode) {
2472 m_help = nfct_help(exp->master);
794e6871
PM
2473 if (!strcmp(m_help->helper->name, name) &&
2474 del_timer(&exp->timeout)) {
ebbf41df 2475 nf_ct_unlink_expect_report(exp,
15e47304 2476 NETLINK_CB(skb).portid,
ebbf41df 2477 nlmsg_report(nlh));
31f15875
PM
2478 nf_ct_expect_put(exp);
2479 }
c1d10adb
PNA
2480 }
2481 }
f8ba1aff 2482 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
2483 } else {
2484 /* This basically means we have to flush everything*/
f8ba1aff 2485 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
2486 for (i = 0; i < nf_ct_expect_hsize; i++) {
2487 hlist_for_each_entry_safe(exp, n, next,
9592a5c0 2488 &net->ct.expect_hash[i],
31f15875
PM
2489 hnode) {
2490 if (del_timer(&exp->timeout)) {
ebbf41df 2491 nf_ct_unlink_expect_report(exp,
15e47304 2492 NETLINK_CB(skb).portid,
ebbf41df 2493 nlmsg_report(nlh));
31f15875
PM
2494 nf_ct_expect_put(exp);
2495 }
c1d10adb
PNA
2496 }
2497 }
f8ba1aff 2498 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
2499 }
2500
2501 return 0;
2502}
2503static int
39938324
PM
2504ctnetlink_change_expect(struct nf_conntrack_expect *x,
2505 const struct nlattr * const cda[])
c1d10adb 2506{
9768e1ac
KW
2507 if (cda[CTA_EXPECT_TIMEOUT]) {
2508 if (!del_timer(&x->timeout))
2509 return -ETIME;
2510
2511 x->timeout.expires = jiffies +
2512 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2513 add_timer(&x->timeout);
2514 }
2515 return 0;
c1d10adb
PNA
2516}
2517
076a0ca0
PNA
2518static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2519 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2520 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2521};
2522
2523static int
2524ctnetlink_parse_expect_nat(const struct nlattr *attr,
2525 struct nf_conntrack_expect *exp,
2526 u_int8_t u3)
2527{
2528#ifdef CONFIG_NF_NAT_NEEDED
2529 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2530 struct nf_conntrack_tuple nat_tuple = {};
2531 int err;
2532
2533 nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2534
2535 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2536 return -EINVAL;
2537
2538 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2539 &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2540 if (err < 0)
2541 return err;
2542
c7232c99 2543 exp->saved_addr = nat_tuple.src.u3;
076a0ca0
PNA
2544 exp->saved_proto = nat_tuple.src.u;
2545 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2546
2547 return 0;
2548#else
2549 return -EOPNOTSUPP;
2550#endif
2551}
2552
c1d10adb 2553static int
ef00f89f
PM
2554ctnetlink_create_expect(struct net *net, u16 zone,
2555 const struct nlattr * const cda[],
9592a5c0 2556 u_int8_t u3,
15e47304 2557 u32 portid, int report)
c1d10adb
PNA
2558{
2559 struct nf_conntrack_tuple tuple, mask, master_tuple;
2560 struct nf_conntrack_tuple_hash *h = NULL;
2561 struct nf_conntrack_expect *exp;
2562 struct nf_conn *ct;
dc808fe2 2563 struct nf_conn_help *help;
660fdb2a 2564 struct nf_conntrack_helper *helper = NULL;
b8c5e52c 2565 u_int32_t class = 0;
c1d10adb
PNA
2566 int err = 0;
2567
c1d10adb
PNA
2568 /* caller guarantees that those three CTA_EXPECT_* exist */
2569 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2570 if (err < 0)
2571 return err;
2572 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2573 if (err < 0)
2574 return err;
2575 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2576 if (err < 0)
2577 return err;
2578
2579 /* Look for master conntrack of this expectation */
ef00f89f 2580 h = nf_conntrack_find_get(net, zone, &master_tuple);
c1d10adb
PNA
2581 if (!h)
2582 return -ENOENT;
2583 ct = nf_ct_tuplehash_to_ctrack(h);
660fdb2a
PNA
2584
2585 /* Look for helper of this expectation */
2586 if (cda[CTA_EXPECT_HELP_NAME]) {
2587 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2588
2589 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2590 nf_ct_protonum(ct));
2591 if (helper == NULL) {
2592#ifdef CONFIG_MODULES
2593 if (request_module("nfct-helper-%s", helpname) < 0) {
2594 err = -EOPNOTSUPP;
2595 goto out;
2596 }
2597
2598 helper = __nf_conntrack_helper_find(helpname,
2599 nf_ct_l3num(ct),
2600 nf_ct_protonum(ct));
2601 if (helper) {
2602 err = -EAGAIN;
2603 goto out;
2604 }
2605#endif
2606 err = -EOPNOTSUPP;
2607 goto out;
2608 }
2609 }
2610
b8c5e52c
PNA
2611 if (cda[CTA_EXPECT_CLASS] && helper) {
2612 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2613 if (class > helper->expect_class_max) {
2614 err = -EINVAL;
2615 goto out;
2616 }
2617 }
6823645d 2618 exp = nf_ct_expect_alloc(ct);
c1d10adb
PNA
2619 if (!exp) {
2620 err = -ENOMEM;
2621 goto out;
2622 }
bc01befd
PNA
2623 help = nfct_help(ct);
2624 if (!help) {
2625 if (!cda[CTA_EXPECT_TIMEOUT]) {
2626 err = -EINVAL;
1310b955 2627 goto err_out;
bc01befd
PNA
2628 }
2629 exp->timeout.expires =
2630 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
601e68e1 2631
bc01befd
PNA
2632 exp->flags = NF_CT_EXPECT_USERSPACE;
2633 if (cda[CTA_EXPECT_FLAGS]) {
2634 exp->flags |=
2635 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2636 }
2637 } else {
2638 if (cda[CTA_EXPECT_FLAGS]) {
2639 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2640 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2641 } else
2642 exp->flags = 0;
2643 }
544d5c7d
PNA
2644 if (cda[CTA_EXPECT_FN]) {
2645 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2646 struct nf_ct_helper_expectfn *expfn;
2647
2648 expfn = nf_ct_helper_expectfn_find_by_name(name);
2649 if (expfn == NULL) {
2650 err = -EINVAL;
2651 goto err_out;
2652 }
2653 exp->expectfn = expfn->expectfn;
2654 } else
2655 exp->expectfn = NULL;
601e68e1 2656
b8c5e52c 2657 exp->class = class;
c1d10adb 2658 exp->master = ct;
660fdb2a 2659 exp->helper = helper;
c1d10adb 2660 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
d4156e8c
PM
2661 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2662 exp->mask.src.u.all = mask.src.u.all;
c1d10adb 2663
076a0ca0
PNA
2664 if (cda[CTA_EXPECT_NAT]) {
2665 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2666 exp, u3);
2667 if (err < 0)
2668 goto err_out;
2669 }
15e47304 2670 err = nf_ct_expect_related_report(exp, portid, report);
076a0ca0 2671err_out:
6823645d 2672 nf_ct_expect_put(exp);
601e68e1 2673out:
c1d10adb
PNA
2674 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2675 return err;
2676}
2677
2678static int
2679ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2680 const struct nlmsghdr *nlh,
2681 const struct nlattr * const cda[])
c1d10adb 2682{
9592a5c0 2683 struct net *net = sock_net(ctnl);
c1d10adb
PNA
2684 struct nf_conntrack_tuple tuple;
2685 struct nf_conntrack_expect *exp;
96bcf938 2686 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 2687 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
2688 u16 zone;
2689 int err;
c1d10adb 2690
df6fb868
PM
2691 if (!cda[CTA_EXPECT_TUPLE]
2692 || !cda[CTA_EXPECT_MASK]
2693 || !cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
2694 return -EINVAL;
2695
ef00f89f
PM
2696 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2697 if (err < 0)
2698 return err;
2699
c1d10adb
PNA
2700 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2701 if (err < 0)
2702 return err;
2703
f8ba1aff 2704 spin_lock_bh(&nf_conntrack_lock);
ef00f89f 2705 exp = __nf_ct_expect_find(net, zone, &tuple);
c1d10adb
PNA
2706
2707 if (!exp) {
f8ba1aff 2708 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 2709 err = -ENOENT;
19abb7b0 2710 if (nlh->nlmsg_flags & NLM_F_CREATE) {
ef00f89f 2711 err = ctnetlink_create_expect(net, zone, cda,
19abb7b0 2712 u3,
15e47304 2713 NETLINK_CB(skb).portid,
19abb7b0
PNA
2714 nlmsg_report(nlh));
2715 }
c1d10adb
PNA
2716 return err;
2717 }
2718
2719 err = -EEXIST;
2720 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2721 err = ctnetlink_change_expect(exp, cda);
f8ba1aff 2722 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 2723
c1d10adb
PNA
2724 return err;
2725}
2726
392025f8 2727static int
15e47304 2728ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
392025f8
PNA
2729 const struct ip_conntrack_stat *st)
2730{
2731 struct nlmsghdr *nlh;
2732 struct nfgenmsg *nfmsg;
15e47304 2733 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
392025f8
PNA
2734
2735 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
15e47304 2736 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
392025f8
PNA
2737 if (nlh == NULL)
2738 goto nlmsg_failure;
2739
2740 nfmsg = nlmsg_data(nlh);
2741 nfmsg->nfgen_family = AF_UNSPEC;
2742 nfmsg->version = NFNETLINK_V0;
2743 nfmsg->res_id = htons(cpu);
2744
2745 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
2746 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
2747 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
2748 goto nla_put_failure;
2749
2750 nlmsg_end(skb, nlh);
2751 return skb->len;
2752
2753nla_put_failure:
2754nlmsg_failure:
2755 nlmsg_cancel(skb, nlh);
2756 return -1;
2757}
2758
2759static int
2760ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2761{
2762 int cpu;
2763 struct net *net = sock_net(skb->sk);
2764
2765 if (cb->args[0] == nr_cpu_ids)
2766 return 0;
2767
2768 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2769 const struct ip_conntrack_stat *st;
2770
2771 if (!cpu_possible(cpu))
2772 continue;
2773
2774 st = per_cpu_ptr(net->ct.stat, cpu);
15e47304 2775 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
392025f8
PNA
2776 cb->nlh->nlmsg_seq,
2777 cpu, st) < 0)
2778 break;
2779 }
2780 cb->args[0] = cpu;
2781
2782 return skb->len;
2783}
2784
2785static int
2786ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb,
2787 const struct nlmsghdr *nlh,
2788 const struct nlattr * const cda[])
2789{
2790 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2791 struct netlink_dump_control c = {
2792 .dump = ctnetlink_exp_stat_cpu_dump,
2793 };
2794 return netlink_dump_start(ctnl, skb, nlh, &c);
2795 }
2796
2797 return 0;
2798}
2799
c1d10adb 2800#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
2801static struct nf_ct_event_notifier ctnl_notifier = {
2802 .fcn = ctnetlink_conntrack_event,
c1d10adb
PNA
2803};
2804
e34d5c1a
PNA
2805static struct nf_exp_event_notifier ctnl_notifier_exp = {
2806 .fcn = ctnetlink_expect_event,
c1d10adb
PNA
2807};
2808#endif
2809
7c8d4cb4 2810static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
c1d10adb 2811 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
f73e924c
PM
2812 .attr_count = CTA_MAX,
2813 .policy = ct_nla_policy },
c1d10adb 2814 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
2815 .attr_count = CTA_MAX,
2816 .policy = ct_nla_policy },
c1d10adb 2817 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
f73e924c
PM
2818 .attr_count = CTA_MAX,
2819 .policy = ct_nla_policy },
c1d10adb 2820 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
2821 .attr_count = CTA_MAX,
2822 .policy = ct_nla_policy },
392025f8
PNA
2823 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
2824 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
d871befe
PNA
2825 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
2826 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
c1d10adb
PNA
2827};
2828
7c8d4cb4 2829static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
c1d10adb 2830 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
f73e924c
PM
2831 .attr_count = CTA_EXPECT_MAX,
2832 .policy = exp_nla_policy },
c1d10adb 2833 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
f73e924c
PM
2834 .attr_count = CTA_EXPECT_MAX,
2835 .policy = exp_nla_policy },
c1d10adb 2836 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
f73e924c
PM
2837 .attr_count = CTA_EXPECT_MAX,
2838 .policy = exp_nla_policy },
392025f8 2839 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
c1d10adb
PNA
2840};
2841
7c8d4cb4 2842static const struct nfnetlink_subsystem ctnl_subsys = {
c1d10adb
PNA
2843 .name = "conntrack",
2844 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2845 .cb_count = IPCTNL_MSG_MAX,
2846 .cb = ctnl_cb,
2847};
2848
7c8d4cb4 2849static const struct nfnetlink_subsystem ctnl_exp_subsys = {
c1d10adb
PNA
2850 .name = "conntrack_expect",
2851 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2852 .cb_count = IPCTNL_MSG_EXP_MAX,
2853 .cb = ctnl_exp_cb,
2854};
2855
d2483dde 2856MODULE_ALIAS("ip_conntrack_netlink");
c1d10adb 2857MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
34f9a2e4 2858MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
c1d10adb 2859
70e9942f
PNA
2860static int __net_init ctnetlink_net_init(struct net *net)
2861{
2862#ifdef CONFIG_NF_CONNTRACK_EVENTS
2863 int ret;
2864
2865 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2866 if (ret < 0) {
2867 pr_err("ctnetlink_init: cannot register notifier.\n");
2868 goto err_out;
2869 }
2870
2871 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2872 if (ret < 0) {
2873 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2874 goto err_unreg_notifier;
2875 }
2876#endif
2877 return 0;
2878
2879#ifdef CONFIG_NF_CONNTRACK_EVENTS
2880err_unreg_notifier:
2881 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2882err_out:
2883 return ret;
2884#endif
2885}
2886
2887static void ctnetlink_net_exit(struct net *net)
2888{
2889#ifdef CONFIG_NF_CONNTRACK_EVENTS
2890 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2891 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2892#endif
2893}
2894
2895static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2896{
2897 struct net *net;
2898
2899 list_for_each_entry(net, net_exit_list, exit_list)
2900 ctnetlink_net_exit(net);
2901}
2902
2903static struct pernet_operations ctnetlink_net_ops = {
2904 .init = ctnetlink_net_init,
2905 .exit_batch = ctnetlink_net_exit_batch,
2906};
2907
c1d10adb
PNA
2908static int __init ctnetlink_init(void)
2909{
2910 int ret;
2911
654d0fbd 2912 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
c1d10adb
PNA
2913 ret = nfnetlink_subsys_register(&ctnl_subsys);
2914 if (ret < 0) {
654d0fbd 2915 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
c1d10adb
PNA
2916 goto err_out;
2917 }
2918
2919 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2920 if (ret < 0) {
654d0fbd 2921 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
c1d10adb
PNA
2922 goto err_unreg_subsys;
2923 }
2924
ef6acf68
JL
2925 ret = register_pernet_subsys(&ctnetlink_net_ops);
2926 if (ret < 0) {
70e9942f 2927 pr_err("ctnetlink_init: cannot register pernet operations\n");
c1d10adb
PNA
2928 goto err_unreg_exp_subsys;
2929 }
7c622345 2930#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
9cb01766
PNA
2931 /* setup interaction between nf_queue and nf_conntrack_netlink. */
2932 RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook);
2933#endif
c1d10adb
PNA
2934 return 0;
2935
c1d10adb
PNA
2936err_unreg_exp_subsys:
2937 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
c1d10adb
PNA
2938err_unreg_subsys:
2939 nfnetlink_subsys_unregister(&ctnl_subsys);
2940err_out:
2941 return ret;
2942}
2943
2944static void __exit ctnetlink_exit(void)
2945{
654d0fbd 2946 pr_info("ctnetlink: unregistering from nfnetlink.\n");
c1d10adb 2947
70e9942f 2948 unregister_pernet_subsys(&ctnetlink_net_ops);
c1d10adb
PNA
2949 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2950 nfnetlink_subsys_unregister(&ctnl_subsys);
7c622345 2951#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
9cb01766
PNA
2952 RCU_INIT_POINTER(nfq_ct_hook, NULL);
2953#endif
c1d10adb
PNA
2954}
2955
2956module_init(ctnetlink_init);
2957module_exit(ctnetlink_exit);