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