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