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