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