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