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