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