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