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