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