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