Linux 3.16-rc1
[deliverable/linux.git] / net / netfilter / nf_conntrack_netlink.c
CommitLineData
c1d10adb
PNA
1/* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
3 *
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
dc808fe2 5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
c1d10adb 6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
392025f8 7 * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
c1d10adb 8 *
601e68e1 9 * Initial connection tracking via netlink development funded and
c1d10adb
PNA
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
11 *
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
13 *
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
c1d10adb
PNA
16 */
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
711bbdd6 21#include <linux/rculist.h>
ea781f19 22#include <linux/rculist_nulls.h>
c1d10adb
PNA
23#include <linux/types.h>
24#include <linux/timer.h>
1cc63249 25#include <linux/security.h>
c1d10adb
PNA
26#include <linux/skbuff.h>
27#include <linux/errno.h>
28#include <linux/netlink.h>
29#include <linux/spinlock.h>
40a839fd 30#include <linux/interrupt.h>
5a0e3ad6 31#include <linux/slab.h>
c1d10adb
PNA
32
33#include <linux/netfilter.h>
dc5fc579 34#include <net/netlink.h>
9592a5c0 35#include <net/sock.h>
c1d10adb
PNA
36#include <net/netfilter/nf_conntrack.h>
37#include <net/netfilter/nf_conntrack_core.h>
77ab9cff 38#include <net/netfilter/nf_conntrack_expect.h>
c1d10adb 39#include <net/netfilter/nf_conntrack_helper.h>
41d73ec0 40#include <net/netfilter/nf_conntrack_seqadj.h>
c1d10adb 41#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 42#include <net/netfilter/nf_conntrack_l4proto.h>
5b1158e9 43#include <net/netfilter/nf_conntrack_tuple.h>
58401572 44#include <net/netfilter/nf_conntrack_acct.h>
ef00f89f 45#include <net/netfilter/nf_conntrack_zones.h>
a992ca2a 46#include <net/netfilter/nf_conntrack_timestamp.h>
c539f017 47#include <net/netfilter/nf_conntrack_labels.h>
5b1158e9
JK
48#ifdef CONFIG_NF_NAT_NEEDED
49#include <net/netfilter/nf_nat_core.h>
c7232c99 50#include <net/netfilter/nf_nat_l4proto.h>
8c88f87c 51#include <net/netfilter/nf_nat_helper.h>
5b1158e9 52#endif
c1d10adb
PNA
53
54#include <linux/netfilter/nfnetlink.h>
55#include <linux/netfilter/nfnetlink_conntrack.h>
56
57MODULE_LICENSE("GPL");
58
dc808fe2 59static char __initdata version[] = "0.93";
c1d10adb 60
c1d10adb 61static inline int
601e68e1 62ctnetlink_dump_tuples_proto(struct sk_buff *skb,
1cde6436 63 const struct nf_conntrack_tuple *tuple,
605dcad6 64 struct nf_conntrack_l4proto *l4proto)
c1d10adb 65{
c1d10adb 66 int ret = 0;
df6fb868 67 struct nlattr *nest_parms;
c1d10adb 68
df6fb868
PM
69 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
70 if (!nest_parms)
71 goto nla_put_failure;
cc1eb431
DM
72 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
73 goto nla_put_failure;
c1d10adb 74
fdf70832
PM
75 if (likely(l4proto->tuple_to_nlattr))
76 ret = l4proto->tuple_to_nlattr(skb, tuple);
601e68e1 77
df6fb868 78 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
79
80 return ret;
81
df6fb868 82nla_put_failure:
c1d10adb
PNA
83 return -1;
84}
85
86static inline int
1cde6436
PNA
87ctnetlink_dump_tuples_ip(struct sk_buff *skb,
88 const struct nf_conntrack_tuple *tuple,
89 struct nf_conntrack_l3proto *l3proto)
c1d10adb 90{
c1d10adb 91 int ret = 0;
df6fb868
PM
92 struct nlattr *nest_parms;
93
94 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
95 if (!nest_parms)
96 goto nla_put_failure;
1cde6436 97
fdf70832
PM
98 if (likely(l3proto->tuple_to_nlattr))
99 ret = l3proto->tuple_to_nlattr(skb, tuple);
1cde6436 100
df6fb868 101 nla_nest_end(skb, nest_parms);
c1d10adb 102
1cde6436
PNA
103 return ret;
104
df6fb868 105nla_put_failure:
1cde6436
PNA
106 return -1;
107}
108
bb5cf80e 109static int
1cde6436
PNA
110ctnetlink_dump_tuples(struct sk_buff *skb,
111 const struct nf_conntrack_tuple *tuple)
112{
113 int ret;
114 struct nf_conntrack_l3proto *l3proto;
605dcad6 115 struct nf_conntrack_l4proto *l4proto;
1cde6436 116
3b988ece 117 rcu_read_lock();
528a3a6f 118 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1cde6436 119 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
c1d10adb 120
3b988ece
HS
121 if (ret >= 0) {
122 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
123 tuple->dst.protonum);
124 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
125 }
126 rcu_read_unlock();
c1d10adb 127 return ret;
c1d10adb
PNA
128}
129
130static inline int
131ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
132{
cc1eb431
DM
133 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
134 goto nla_put_failure;
c1d10adb
PNA
135 return 0;
136
df6fb868 137nla_put_failure:
c1d10adb
PNA
138 return -1;
139}
140
141static inline int
142ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
143{
c1216382 144 long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ;
c1d10adb 145
77236b6e 146 if (timeout < 0)
c1d10adb 147 timeout = 0;
601e68e1 148
cc1eb431
DM
149 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
150 goto nla_put_failure;
c1d10adb
PNA
151 return 0;
152
df6fb868 153nla_put_failure:
c1d10adb
PNA
154 return -1;
155}
156
157static inline int
440f0d58 158ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
c1d10adb 159{
5e8fbe2a 160 struct nf_conntrack_l4proto *l4proto;
df6fb868 161 struct nlattr *nest_proto;
c1d10adb
PNA
162 int ret;
163
528a3a6f
PNA
164 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
165 if (!l4proto->to_nlattr)
c1d10adb 166 return 0;
601e68e1 167
df6fb868
PM
168 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
169 if (!nest_proto)
170 goto nla_put_failure;
c1d10adb 171
fdf70832 172 ret = l4proto->to_nlattr(skb, nest_proto, ct);
c1d10adb 173
df6fb868 174 nla_nest_end(skb, nest_proto);
c1d10adb
PNA
175
176 return ret;
177
df6fb868 178nla_put_failure:
c1d10adb
PNA
179 return -1;
180}
181
182static inline int
183ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
184{
df6fb868 185 struct nlattr *nest_helper;
dc808fe2 186 const struct nf_conn_help *help = nfct_help(ct);
3c158f7f 187 struct nf_conntrack_helper *helper;
c1d10adb 188
3c158f7f 189 if (!help)
c1d10adb 190 return 0;
601e68e1 191
3c158f7f
PM
192 helper = rcu_dereference(help->helper);
193 if (!helper)
194 goto out;
195
df6fb868
PM
196 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
197 if (!nest_helper)
198 goto nla_put_failure;
cc1eb431
DM
199 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
200 goto nla_put_failure;
c1d10adb 201
fdf70832
PM
202 if (helper->to_nlattr)
203 helper->to_nlattr(skb, ct);
c1d10adb 204
df6fb868 205 nla_nest_end(skb, nest_helper);
3c158f7f 206out:
c1d10adb
PNA
207 return 0;
208
df6fb868 209nla_put_failure:
c1d10adb
PNA
210 return -1;
211}
212
bb5cf80e 213static int
4542fa47
HE
214dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
215 enum ip_conntrack_dir dir, int type)
c1d10adb 216{
4542fa47
HE
217 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
218 struct nf_conn_counter *counter = acct->counter;
df6fb868 219 struct nlattr *nest_count;
4542fa47 220 u64 pkts, bytes;
c1d10adb 221
4542fa47
HE
222 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
223 pkts = atomic64_xchg(&counter[dir].packets, 0);
224 bytes = atomic64_xchg(&counter[dir].bytes, 0);
225 } else {
226 pkts = atomic64_read(&counter[dir].packets);
227 bytes = atomic64_read(&counter[dir].bytes);
228 }
229
230 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
df6fb868
PM
231 if (!nest_count)
232 goto nla_put_failure;
233
cc1eb431
DM
234 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)) ||
235 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes)))
236 goto nla_put_failure;
c1d10adb 237
df6fb868 238 nla_nest_end(skb, nest_count);
c1d10adb
PNA
239
240 return 0;
241
df6fb868 242nla_put_failure:
c1d10adb
PNA
243 return -1;
244}
c1d10adb 245
80e60e67 246static int
4542fa47 247ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
80e60e67 248{
4542fa47 249 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
80e60e67 250
80e60e67
PNA
251 if (!acct)
252 return 0;
253
4542fa47
HE
254 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
255 return -1;
256 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
257 return -1;
258
259 return 0;
80e60e67
PNA
260}
261
a992ca2a
PNA
262static int
263ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
264{
265 struct nlattr *nest_count;
266 const struct nf_conn_tstamp *tstamp;
267
268 tstamp = nf_conn_tstamp_find(ct);
269 if (!tstamp)
270 return 0;
271
272 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
273 if (!nest_count)
274 goto nla_put_failure;
275
cc1eb431
DM
276 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)) ||
277 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
278 cpu_to_be64(tstamp->stop))))
279 goto nla_put_failure;
a992ca2a
PNA
280 nla_nest_end(skb, nest_count);
281
282 return 0;
283
284nla_put_failure:
285 return -1;
286}
287
c1d10adb
PNA
288#ifdef CONFIG_NF_CONNTRACK_MARK
289static inline int
290ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
291{
cc1eb431
DM
292 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
293 goto nla_put_failure;
c1d10adb
PNA
294 return 0;
295
df6fb868 296nla_put_failure:
c1d10adb
PNA
297 return -1;
298}
299#else
300#define ctnetlink_dump_mark(a, b) (0)
301#endif
302
37fccd85
PNA
303#ifdef CONFIG_NF_CONNTRACK_SECMARK
304static inline int
1cc63249 305ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
37fccd85 306{
1cc63249
EP
307 struct nlattr *nest_secctx;
308 int len, ret;
309 char *secctx;
310
311 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
312 if (ret)
cba85b53 313 return 0;
1cc63249
EP
314
315 ret = -1;
316 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
317 if (!nest_secctx)
318 goto nla_put_failure;
37fccd85 319
cc1eb431
DM
320 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
321 goto nla_put_failure;
1cc63249
EP
322 nla_nest_end(skb, nest_secctx);
323
324 ret = 0;
37fccd85 325nla_put_failure:
1cc63249
EP
326 security_release_secctx(secctx, len);
327 return ret;
37fccd85
PNA
328}
329#else
1cc63249 330#define ctnetlink_dump_secctx(a, b) (0)
37fccd85
PNA
331#endif
332
0ceabd83
FW
333#ifdef CONFIG_NF_CONNTRACK_LABELS
334static int ctnetlink_label_size(const struct nf_conn *ct)
335{
336 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
337
338 if (!labels)
339 return 0;
340 return nla_total_size(labels->words * sizeof(long));
341}
342
343static int
344ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
345{
346 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
347 unsigned int len, i;
348
349 if (!labels)
350 return 0;
351
352 len = labels->words * sizeof(long);
353 i = 0;
354 do {
355 if (labels->bits[i] != 0)
356 return nla_put(skb, CTA_LABELS, len, labels->bits);
357 i++;
358 } while (i < labels->words);
359
360 return 0;
361}
362#else
363#define ctnetlink_dump_labels(a, b) (0)
364#define ctnetlink_label_size(a) (0)
365#endif
366
0f417ce9
PNA
367#define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
368
369static inline int
370ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
371{
372 struct nlattr *nest_parms;
373
374 if (!(ct->status & IPS_EXPECTED))
375 return 0;
376
377 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
378 if (!nest_parms)
379 goto nla_put_failure;
380 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
381 goto nla_put_failure;
382 nla_nest_end(skb, nest_parms);
383
384 return 0;
385
386nla_put_failure:
387 return -1;
388}
389
bb5cf80e 390static int
41d73ec0 391dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
13eae15a 392{
13eae15a
PNA
393 struct nlattr *nest_parms;
394
395 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
396 if (!nest_parms)
397 goto nla_put_failure;
398
41d73ec0
PM
399 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
400 htonl(seq->correction_pos)) ||
401 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
402 htonl(seq->offset_before)) ||
403 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
404 htonl(seq->offset_after)))
cc1eb431 405 goto nla_put_failure;
13eae15a
PNA
406
407 nla_nest_end(skb, nest_parms);
408
409 return 0;
410
411nla_put_failure:
412 return -1;
413}
414
415static inline int
41d73ec0 416ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
13eae15a 417{
41d73ec0
PM
418 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
419 struct nf_ct_seqadj *seq;
13eae15a 420
41d73ec0 421 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
13eae15a
PNA
422 return 0;
423
41d73ec0
PM
424 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
425 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
13eae15a
PNA
426 return -1;
427
41d73ec0
PM
428 seq = &seqadj->seq[IP_CT_DIR_REPLY];
429 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
13eae15a
PNA
430 return -1;
431
432 return 0;
433}
13eae15a 434
c1d10adb
PNA
435static inline int
436ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
437{
cc1eb431
DM
438 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
439 goto nla_put_failure;
c1d10adb
PNA
440 return 0;
441
df6fb868 442nla_put_failure:
c1d10adb
PNA
443 return -1;
444}
445
446static inline int
447ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
448{
cc1eb431
DM
449 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
450 goto nla_put_failure;
c1d10adb
PNA
451 return 0;
452
df6fb868 453nla_put_failure:
c1d10adb
PNA
454 return -1;
455}
456
c1d10adb 457static int
15e47304 458ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
80e60e67 459 struct nf_conn *ct)
c1d10adb
PNA
460{
461 struct nlmsghdr *nlh;
462 struct nfgenmsg *nfmsg;
df6fb868 463 struct nlattr *nest_parms;
15e47304 464 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
c1d10adb 465
80e60e67 466 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
15e47304 467 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
96bcf938
PNA
468 if (nlh == NULL)
469 goto nlmsg_failure;
c1d10adb 470
96bcf938 471 nfmsg = nlmsg_data(nlh);
5e8fbe2a 472 nfmsg->nfgen_family = nf_ct_l3num(ct);
c1d10adb
PNA
473 nfmsg->version = NFNETLINK_V0;
474 nfmsg->res_id = 0;
475
df6fb868
PM
476 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
477 if (!nest_parms)
478 goto nla_put_failure;
f2f3e38c 479 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
df6fb868
PM
480 goto nla_put_failure;
481 nla_nest_end(skb, nest_parms);
601e68e1 482
df6fb868
PM
483 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
484 if (!nest_parms)
485 goto nla_put_failure;
f2f3e38c 486 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
df6fb868
PM
487 goto nla_put_failure;
488 nla_nest_end(skb, nest_parms);
c1d10adb 489
cc1eb431
DM
490 if (nf_ct_zone(ct) &&
491 nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
492 goto nla_put_failure;
ef00f89f 493
c1d10adb
PNA
494 if (ctnetlink_dump_status(skb, ct) < 0 ||
495 ctnetlink_dump_timeout(skb, ct) < 0 ||
4542fa47 496 ctnetlink_dump_acct(skb, ct, type) < 0 ||
a992ca2a 497 ctnetlink_dump_timestamp(skb, ct) < 0 ||
c1d10adb
PNA
498 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
499 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
500 ctnetlink_dump_mark(skb, ct) < 0 ||
1cc63249 501 ctnetlink_dump_secctx(skb, ct) < 0 ||
0ceabd83 502 ctnetlink_dump_labels(skb, ct) < 0 ||
c1d10adb 503 ctnetlink_dump_id(skb, ct) < 0 ||
13eae15a 504 ctnetlink_dump_use(skb, ct) < 0 ||
0f417ce9 505 ctnetlink_dump_master(skb, ct) < 0 ||
41d73ec0 506 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
df6fb868 507 goto nla_put_failure;
c1d10adb 508
96bcf938 509 nlmsg_end(skb, nlh);
c1d10adb
PNA
510 return skb->len;
511
512nlmsg_failure:
df6fb868 513nla_put_failure:
96bcf938 514 nlmsg_cancel(skb, nlh);
c1d10adb
PNA
515 return -1;
516}
517
03b64f51
PNA
518static inline size_t
519ctnetlink_proto_size(const struct nf_conn *ct)
2732c4e4
HE
520{
521 struct nf_conntrack_l3proto *l3proto;
522 struct nf_conntrack_l4proto *l4proto;
03b64f51
PNA
523 size_t len = 0;
524
525 rcu_read_lock();
526 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
527 len += l3proto->nla_size;
528
529 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
530 len += l4proto->nla_size;
531 rcu_read_unlock();
532
533 return len;
534}
535
d26e6a02 536static inline size_t
f7b13e43 537ctnetlink_acct_size(const struct nf_conn *ct)
d26e6a02
JP
538{
539 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
540 return 0;
541 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
542 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
543 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
544 ;
545}
546
cba85b53
PNA
547static inline int
548ctnetlink_secctx_size(const struct nf_conn *ct)
1cc63249 549{
cba85b53
PNA
550#ifdef CONFIG_NF_CONNTRACK_SECMARK
551 int len, ret;
1cc63249 552
cba85b53
PNA
553 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
554 if (ret)
555 return 0;
1cc63249 556
cba85b53
PNA
557 return nla_total_size(0) /* CTA_SECCTX */
558 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
559#else
560 return 0;
1cc63249 561#endif
cba85b53 562}
1cc63249 563
a992ca2a
PNA
564static inline size_t
565ctnetlink_timestamp_size(const struct nf_conn *ct)
566{
567#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
568 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
569 return 0;
570 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
571#else
572 return 0;
573#endif
574}
575
03b64f51
PNA
576static inline size_t
577ctnetlink_nlmsg_size(const struct nf_conn *ct)
578{
579 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
580 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
581 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
582 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
583 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
584 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
585 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
f7b13e43 586 + ctnetlink_acct_size(ct)
a992ca2a 587 + ctnetlink_timestamp_size(ct)
03b64f51
PNA
588 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
589 + nla_total_size(0) /* CTA_PROTOINFO */
590 + nla_total_size(0) /* CTA_HELP */
591 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
cba85b53 592 + ctnetlink_secctx_size(ct)
d271e8bd 593#ifdef CONFIG_NF_NAT_NEEDED
03b64f51
PNA
594 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
595 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
d271e8bd
HE
596#endif
597#ifdef CONFIG_NF_CONNTRACK_MARK
03b64f51 598 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
d271e8bd 599#endif
03b64f51 600 + ctnetlink_proto_size(ct)
0ceabd83 601 + ctnetlink_label_size(ct)
03b64f51 602 ;
2732c4e4
HE
603}
604
8e36c4b5 605#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
606static int
607ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
c1d10adb 608{
9592a5c0 609 struct net *net;
c1d10adb
PNA
610 struct nlmsghdr *nlh;
611 struct nfgenmsg *nfmsg;
df6fb868 612 struct nlattr *nest_parms;
19abb7b0 613 struct nf_conn *ct = item->ct;
c1d10adb
PNA
614 struct sk_buff *skb;
615 unsigned int type;
c1d10adb 616 unsigned int flags = 0, group;
dd7669a9 617 int err;
c1d10adb
PNA
618
619 /* ignore our fake conntrack entry */
5bfddbd4 620 if (nf_ct_is_untracked(ct))
e34d5c1a 621 return 0;
c1d10adb 622
a0891aa6 623 if (events & (1 << IPCT_DESTROY)) {
c1d10adb
PNA
624 type = IPCTNL_MSG_CT_DELETE;
625 group = NFNLGRP_CONNTRACK_DESTROY;
a0891aa6 626 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
c1d10adb
PNA
627 type = IPCTNL_MSG_CT_NEW;
628 flags = NLM_F_CREATE|NLM_F_EXCL;
c1d10adb 629 group = NFNLGRP_CONNTRACK_NEW;
17e6e4ea 630 } else if (events) {
c1d10adb
PNA
631 type = IPCTNL_MSG_CT_NEW;
632 group = NFNLGRP_CONNTRACK_UPDATE;
633 } else
e34d5c1a 634 return 0;
a2427692 635
9592a5c0
AD
636 net = nf_ct_net(ct);
637 if (!item->report && !nfnetlink_has_listeners(net, group))
e34d5c1a 638 return 0;
a2427692 639
03b64f51
PNA
640 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
641 if (skb == NULL)
150ace0d 642 goto errout;
c1d10adb 643
c1d10adb 644 type |= NFNL_SUBSYS_CTNETLINK << 8;
15e47304 645 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
96bcf938
PNA
646 if (nlh == NULL)
647 goto nlmsg_failure;
c1d10adb 648
96bcf938 649 nfmsg = nlmsg_data(nlh);
5e8fbe2a 650 nfmsg->nfgen_family = nf_ct_l3num(ct);
c1d10adb
PNA
651 nfmsg->version = NFNETLINK_V0;
652 nfmsg->res_id = 0;
653
528a3a6f 654 rcu_read_lock();
df6fb868
PM
655 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
656 if (!nest_parms)
657 goto nla_put_failure;
f2f3e38c 658 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
df6fb868
PM
659 goto nla_put_failure;
660 nla_nest_end(skb, nest_parms);
601e68e1 661
df6fb868
PM
662 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
663 if (!nest_parms)
664 goto nla_put_failure;
f2f3e38c 665 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
df6fb868
PM
666 goto nla_put_failure;
667 nla_nest_end(skb, nest_parms);
c1d10adb 668
cc1eb431
DM
669 if (nf_ct_zone(ct) &&
670 nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
671 goto nla_put_failure;
ef00f89f 672
1eedf699
EL
673 if (ctnetlink_dump_id(skb, ct) < 0)
674 goto nla_put_failure;
675
e57dce60
FH
676 if (ctnetlink_dump_status(skb, ct) < 0)
677 goto nla_put_failure;
678
a0891aa6 679 if (events & (1 << IPCT_DESTROY)) {
4542fa47 680 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
a992ca2a 681 ctnetlink_dump_timestamp(skb, ct) < 0)
df6fb868 682 goto nla_put_failure;
7b621c1e 683 } else {
7b621c1e 684 if (ctnetlink_dump_timeout(skb, ct) < 0)
df6fb868 685 goto nla_put_failure;
7b621c1e 686
a0891aa6 687 if (events & (1 << IPCT_PROTOINFO)
7b621c1e 688 && ctnetlink_dump_protoinfo(skb, ct) < 0)
df6fb868 689 goto nla_put_failure;
7b621c1e 690
a0891aa6 691 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
7b621c1e 692 && ctnetlink_dump_helpinfo(skb, ct) < 0)
df6fb868 693 goto nla_put_failure;
7b621c1e 694
ff660c80 695#ifdef CONFIG_NF_CONNTRACK_SECMARK
a0891aa6 696 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
1cc63249 697 && ctnetlink_dump_secctx(skb, ct) < 0)
37fccd85 698 goto nla_put_failure;
ff660c80 699#endif
0ceabd83
FW
700 if (events & (1 << IPCT_LABEL) &&
701 ctnetlink_dump_labels(skb, ct) < 0)
702 goto nla_put_failure;
7b621c1e 703
a0891aa6 704 if (events & (1 << IPCT_RELATED) &&
0f417ce9
PNA
705 ctnetlink_dump_master(skb, ct) < 0)
706 goto nla_put_failure;
707
41d73ec0
PM
708 if (events & (1 << IPCT_SEQADJ) &&
709 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
13eae15a 710 goto nla_put_failure;
7b621c1e 711 }
b9a37e0c 712
a83099a6 713#ifdef CONFIG_NF_CONNTRACK_MARK
a0891aa6 714 if ((events & (1 << IPCT_MARK) || ct->mark)
a83099a6
EL
715 && ctnetlink_dump_mark(skb, ct) < 0)
716 goto nla_put_failure;
717#endif
528a3a6f 718 rcu_read_unlock();
a83099a6 719
96bcf938 720 nlmsg_end(skb, nlh);
15e47304 721 err = nfnetlink_send(skb, net, item->portid, group, item->report,
cd8c20b6 722 GFP_ATOMIC);
dd7669a9
PNA
723 if (err == -ENOBUFS || err == -EAGAIN)
724 return -ENOBUFS;
725
e34d5c1a 726 return 0;
c1d10adb 727
df6fb868 728nla_put_failure:
528a3a6f 729 rcu_read_unlock();
96bcf938 730 nlmsg_cancel(skb, nlh);
528a3a6f 731nlmsg_failure:
c1d10adb 732 kfree_skb(skb);
150ace0d 733errout:
37b7ef72
PNA
734 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
735 return -ENOBUFS;
736
e34d5c1a 737 return 0;
c1d10adb
PNA
738}
739#endif /* CONFIG_NF_CONNTRACK_EVENTS */
740
741static int ctnetlink_done(struct netlink_callback *cb)
742{
89f2e218
PM
743 if (cb->args[1])
744 nf_ct_put((struct nf_conn *)cb->args[1]);
0f298a28
PNA
745 if (cb->data)
746 kfree(cb->data);
c1d10adb
PNA
747 return 0;
748}
749
0f298a28
PNA
750struct ctnetlink_dump_filter {
751 struct {
752 u_int32_t val;
753 u_int32_t mask;
754 } mark;
755};
756
c1d10adb
PNA
757static int
758ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
759{
9592a5c0 760 struct net *net = sock_net(skb->sk);
89f2e218 761 struct nf_conn *ct, *last;
c1d10adb 762 struct nf_conntrack_tuple_hash *h;
ea781f19 763 struct hlist_nulls_node *n;
96bcf938 764 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
87711cb8 765 u_int8_t l3proto = nfmsg->nfgen_family;
3b988ece 766 int res;
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
0eba801b 1334ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
e6a7d3c0
PNA
1335{
1336#ifdef CONFIG_NF_NAT_NEEDED
1337 int ret;
1338
fe337ac2
FW
1339 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1340 return 0;
1341
0eba801b
PNA
1342 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1343 cda[CTA_NAT_DST]);
1344 if (ret < 0)
1345 return ret;
1346
1347 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1348 cda[CTA_NAT_SRC]);
1349 return ret;
e6a7d3c0 1350#else
0eba801b
PNA
1351 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1352 return 0;
e6a7d3c0
PNA
1353 return -EOPNOTSUPP;
1354#endif
1355}
c1d10adb
PNA
1356
1357static inline int
39938324 1358ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
1359{
1360 struct nf_conntrack_helper *helper;
dc808fe2 1361 struct nf_conn_help *help = nfct_help(ct);
29fe1b48 1362 char *helpname = NULL;
ae243bee 1363 struct nlattr *helpinfo = NULL;
c1d10adb
PNA
1364 int err;
1365
c1d10adb
PNA
1366 /* don't change helper of sibling connections */
1367 if (ct->master)
0adf9d67 1368 return -EBUSY;
c1d10adb 1369
ae243bee 1370 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
c1d10adb
PNA
1371 if (err < 0)
1372 return err;
1373
df293bbb
YK
1374 if (!strcmp(helpname, "")) {
1375 if (help && help->helper) {
c1d10adb
PNA
1376 /* we had a helper before ... */
1377 nf_ct_remove_expectations(ct);
a9b3cd7f 1378 RCU_INIT_POINTER(help->helper, NULL);
c1d10adb 1379 }
df293bbb
YK
1380
1381 return 0;
c1d10adb 1382 }
601e68e1 1383
794e6871
PM
1384 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1385 nf_ct_protonum(ct));
226c0c0e
PNA
1386 if (helper == NULL) {
1387#ifdef CONFIG_MODULES
ca7433df 1388 spin_unlock_bh(&nf_conntrack_expect_lock);
226c0c0e
PNA
1389
1390 if (request_module("nfct-helper-%s", helpname) < 0) {
ca7433df 1391 spin_lock_bh(&nf_conntrack_expect_lock);
226c0c0e
PNA
1392 return -EOPNOTSUPP;
1393 }
1394
ca7433df 1395 spin_lock_bh(&nf_conntrack_expect_lock);
794e6871
PM
1396 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1397 nf_ct_protonum(ct));
226c0c0e
PNA
1398 if (helper)
1399 return -EAGAIN;
1400#endif
0adf9d67 1401 return -EOPNOTSUPP;
226c0c0e 1402 }
df293bbb 1403
ceceae1b 1404 if (help) {
ae243bee
PNA
1405 if (help->helper == helper) {
1406 /* update private helper data if allowed. */
7be54ca4 1407 if (helper->from_nlattr)
ae243bee 1408 helper->from_nlattr(helpinfo, ct);
ceceae1b 1409 return 0;
fd7462de 1410 } else
ceceae1b 1411 return -EBUSY;
ceceae1b 1412 }
df293bbb 1413
fd7462de
PNA
1414 /* we cannot set a helper for an existing conntrack */
1415 return -EOPNOTSUPP;
c1d10adb
PNA
1416}
1417
1418static inline int
39938324 1419ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1420{
77236b6e 1421 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
601e68e1 1422
c1d10adb
PNA
1423 if (!del_timer(&ct->timeout))
1424 return -ETIME;
1425
1426 ct->timeout.expires = jiffies + timeout * HZ;
1427 add_timer(&ct->timeout);
1428
1429 return 0;
1430}
1431
d0b0268f
PM
1432static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1433 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1434 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1435 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1436};
1437
c1d10adb 1438static inline int
39938324 1439ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1440{
39938324
PM
1441 const struct nlattr *attr = cda[CTA_PROTOINFO];
1442 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
605dcad6 1443 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
1444 int err = 0;
1445
130ffbc2
DB
1446 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1447 if (err < 0)
1448 return err;
c1d10adb 1449
cd91566e
FW
1450 rcu_read_lock();
1451 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
fdf70832
PM
1452 if (l4proto->from_nlattr)
1453 err = l4proto->from_nlattr(tb, ct);
cd91566e 1454 rcu_read_unlock();
c1d10adb
PNA
1455
1456 return err;
1457}
1458
41d73ec0
PM
1459static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1460 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1461 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1462 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
d0b0268f
PM
1463};
1464
13eae15a 1465static inline int
41d73ec0 1466change_seq_adj(struct nf_ct_seqadj *seq, const struct nlattr * const attr)
13eae15a 1467{
130ffbc2 1468 int err;
41d73ec0 1469 struct nlattr *cda[CTA_SEQADJ_MAX+1];
13eae15a 1470
41d73ec0 1471 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy);
130ffbc2
DB
1472 if (err < 0)
1473 return err;
13eae15a 1474
41d73ec0 1475 if (!cda[CTA_SEQADJ_CORRECTION_POS])
13eae15a
PNA
1476 return -EINVAL;
1477
41d73ec0
PM
1478 seq->correction_pos =
1479 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
13eae15a 1480
41d73ec0 1481 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
13eae15a
PNA
1482 return -EINVAL;
1483
41d73ec0
PM
1484 seq->offset_before =
1485 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
13eae15a 1486
41d73ec0 1487 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
13eae15a
PNA
1488 return -EINVAL;
1489
41d73ec0
PM
1490 seq->offset_after =
1491 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
13eae15a
PNA
1492
1493 return 0;
1494}
1495
1496static int
41d73ec0
PM
1497ctnetlink_change_seq_adj(struct nf_conn *ct,
1498 const struct nlattr * const cda[])
13eae15a 1499{
41d73ec0 1500 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
13eae15a 1501 int ret = 0;
13eae15a 1502
41d73ec0 1503 if (!seqadj)
13eae15a
PNA
1504 return 0;
1505
41d73ec0
PM
1506 if (cda[CTA_SEQ_ADJ_ORIG]) {
1507 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1508 cda[CTA_SEQ_ADJ_ORIG]);
13eae15a
PNA
1509 if (ret < 0)
1510 return ret;
1511
1512 ct->status |= IPS_SEQ_ADJUST;
1513 }
1514
41d73ec0
PM
1515 if (cda[CTA_SEQ_ADJ_REPLY]) {
1516 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1517 cda[CTA_SEQ_ADJ_REPLY]);
13eae15a
PNA
1518 if (ret < 0)
1519 return ret;
1520
1521 ct->status |= IPS_SEQ_ADJUST;
1522 }
1523
1524 return 0;
1525}
13eae15a 1526
9b21f6a9
FW
1527static int
1528ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1529{
1530#ifdef CONFIG_NF_CONNTRACK_LABELS
1531 size_t len = nla_len(cda[CTA_LABELS]);
1532 const void *mask = cda[CTA_LABELS_MASK];
1533
1534 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1535 return -EINVAL;
1536
1537 if (mask) {
1538 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1539 nla_len(cda[CTA_LABELS_MASK]) != len)
1540 return -EINVAL;
1541 mask = nla_data(cda[CTA_LABELS_MASK]);
1542 }
1543
1544 len /= sizeof(u32);
1545
1546 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1547#else
1548 return -EOPNOTSUPP;
1549#endif
1550}
1551
c1d10adb 1552static int
39938324
PM
1553ctnetlink_change_conntrack(struct nf_conn *ct,
1554 const struct nlattr * const cda[])
c1d10adb
PNA
1555{
1556 int err;
1557
e098360f
PNA
1558 /* only allow NAT changes and master assignation for new conntracks */
1559 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1560 return -EOPNOTSUPP;
1561
df6fb868 1562 if (cda[CTA_HELP]) {
c1d10adb
PNA
1563 err = ctnetlink_change_helper(ct, cda);
1564 if (err < 0)
1565 return err;
1566 }
1567
df6fb868 1568 if (cda[CTA_TIMEOUT]) {
c1d10adb
PNA
1569 err = ctnetlink_change_timeout(ct, cda);
1570 if (err < 0)
1571 return err;
1572 }
1573
df6fb868 1574 if (cda[CTA_STATUS]) {
c1d10adb
PNA
1575 err = ctnetlink_change_status(ct, cda);
1576 if (err < 0)
1577 return err;
1578 }
1579
df6fb868 1580 if (cda[CTA_PROTOINFO]) {
c1d10adb
PNA
1581 err = ctnetlink_change_protoinfo(ct, cda);
1582 if (err < 0)
1583 return err;
1584 }
1585
bcd1e830 1586#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1587 if (cda[CTA_MARK])
77236b6e 1588 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1589#endif
1590
41d73ec0
PM
1591 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1592 err = ctnetlink_change_seq_adj(ct, cda);
13eae15a
PNA
1593 if (err < 0)
1594 return err;
1595 }
41d73ec0 1596
9b21f6a9
FW
1597 if (cda[CTA_LABELS]) {
1598 err = ctnetlink_attach_labels(ct, cda);
1599 if (err < 0)
1600 return err;
1601 }
13eae15a 1602
c1d10adb
PNA
1603 return 0;
1604}
1605
f0a3c086 1606static struct nf_conn *
ef00f89f 1607ctnetlink_create_conntrack(struct net *net, u16 zone,
9592a5c0 1608 const struct nlattr * const cda[],
c1d10adb 1609 struct nf_conntrack_tuple *otuple,
5faa1f4c 1610 struct nf_conntrack_tuple *rtuple,
7ec47496 1611 u8 u3)
c1d10adb
PNA
1612{
1613 struct nf_conn *ct;
1614 int err = -EINVAL;
ceceae1b 1615 struct nf_conntrack_helper *helper;
315c34da 1616 struct nf_conn_tstamp *tstamp;
c1d10adb 1617
ef00f89f 1618 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
cd7fcbf1 1619 if (IS_ERR(ct))
f0a3c086 1620 return ERR_PTR(-ENOMEM);
c1d10adb 1621
df6fb868 1622 if (!cda[CTA_TIMEOUT])
0f5b3e85 1623 goto err1;
77236b6e 1624 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
c1d10adb
PNA
1625
1626 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
c1d10adb 1627
1575e7ea 1628 rcu_read_lock();
226c0c0e 1629 if (cda[CTA_HELP]) {
29fe1b48 1630 char *helpname = NULL;
ae243bee
PNA
1631 struct nlattr *helpinfo = NULL;
1632
1633 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
0f5b3e85
PM
1634 if (err < 0)
1635 goto err2;
226c0c0e 1636
794e6871
PM
1637 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1638 nf_ct_protonum(ct));
226c0c0e
PNA
1639 if (helper == NULL) {
1640 rcu_read_unlock();
1641#ifdef CONFIG_MODULES
1642 if (request_module("nfct-helper-%s", helpname) < 0) {
1643 err = -EOPNOTSUPP;
0f5b3e85 1644 goto err1;
226c0c0e
PNA
1645 }
1646
1647 rcu_read_lock();
794e6871
PM
1648 helper = __nf_conntrack_helper_find(helpname,
1649 nf_ct_l3num(ct),
1650 nf_ct_protonum(ct));
226c0c0e 1651 if (helper) {
226c0c0e 1652 err = -EAGAIN;
0f5b3e85 1653 goto err2;
226c0c0e
PNA
1654 }
1655 rcu_read_unlock();
1656#endif
1657 err = -EOPNOTSUPP;
0f5b3e85 1658 goto err1;
226c0c0e
PNA
1659 } else {
1660 struct nf_conn_help *help;
1661
1afc5679 1662 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
226c0c0e 1663 if (help == NULL) {
226c0c0e 1664 err = -ENOMEM;
0f5b3e85 1665 goto err2;
226c0c0e 1666 }
ae243bee 1667 /* set private helper data if allowed. */
7be54ca4 1668 if (helper->from_nlattr)
ae243bee 1669 helper->from_nlattr(helpinfo, ct);
226c0c0e
PNA
1670
1671 /* not in hash table yet so not strictly necessary */
a9b3cd7f 1672 RCU_INIT_POINTER(help->helper, helper);
226c0c0e
PNA
1673 }
1674 } else {
1675 /* try an implicit helper assignation */
b2a15a60 1676 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
0f5b3e85
PM
1677 if (err < 0)
1678 goto err2;
1575e7ea
PNA
1679 }
1680
0eba801b
PNA
1681 err = ctnetlink_setup_nat(ct, cda);
1682 if (err < 0)
1683 goto err2;
e6a7d3c0 1684
a88e22ad 1685 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
a992ca2a 1686 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
a88e22ad 1687 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
c539f017
FW
1688 nf_ct_labels_ext_add(ct);
1689
a88e22ad
PNA
1690 /* we must add conntrack extensions before confirmation. */
1691 ct->status |= IPS_CONFIRMED;
1692
1693 if (cda[CTA_STATUS]) {
1694 err = ctnetlink_change_status(ct, cda);
0f5b3e85
PM
1695 if (err < 0)
1696 goto err2;
bbb3357d 1697 }
c1d10adb 1698
41d73ec0
PM
1699 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1700 err = ctnetlink_change_seq_adj(ct, cda);
0f5b3e85
PM
1701 if (err < 0)
1702 goto err2;
c969aa7d 1703 }
c969aa7d 1704
e5fc9e7a 1705 memset(&ct->proto, 0, sizeof(ct->proto));
df6fb868 1706 if (cda[CTA_PROTOINFO]) {
c1d10adb 1707 err = ctnetlink_change_protoinfo(ct, cda);
0f5b3e85
PM
1708 if (err < 0)
1709 goto err2;
c1d10adb
PNA
1710 }
1711
bcd1e830 1712#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1713 if (cda[CTA_MARK])
77236b6e 1714 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1715#endif
1716
5faa1f4c 1717 /* setup master conntrack: this is a confirmed expectation */
7ec47496
PNA
1718 if (cda[CTA_TUPLE_MASTER]) {
1719 struct nf_conntrack_tuple master;
1720 struct nf_conntrack_tuple_hash *master_h;
1721 struct nf_conn *master_ct;
1722
1723 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1724 if (err < 0)
0f5b3e85 1725 goto err2;
7ec47496 1726
ef00f89f 1727 master_h = nf_conntrack_find_get(net, zone, &master);
7ec47496
PNA
1728 if (master_h == NULL) {
1729 err = -ENOENT;
0f5b3e85 1730 goto err2;
7ec47496
PNA
1731 }
1732 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
f2a89004 1733 __set_bit(IPS_EXPECTED_BIT, &ct->status);
5faa1f4c 1734 ct->master = master_ct;
f2a89004 1735 }
315c34da
PNA
1736 tstamp = nf_conn_tstamp_find(ct);
1737 if (tstamp)
1738 tstamp->start = ktime_to_ns(ktime_get_real());
5faa1f4c 1739
7d367e06
JK
1740 err = nf_conntrack_hash_check_insert(ct);
1741 if (err < 0)
1742 goto err2;
1743
58a3c9bb 1744 rcu_read_unlock();
dafc741c 1745
f0a3c086 1746 return ct;
c1d10adb 1747
0f5b3e85
PM
1748err2:
1749 rcu_read_unlock();
1750err1:
c1d10adb 1751 nf_conntrack_free(ct);
f0a3c086 1752 return ERR_PTR(err);
c1d10adb
PNA
1753}
1754
601e68e1
YH
1755static int
1756ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1757 const struct nlmsghdr *nlh,
1758 const struct nlattr * const cda[])
c1d10adb 1759{
9592a5c0 1760 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1761 struct nf_conntrack_tuple otuple, rtuple;
1762 struct nf_conntrack_tuple_hash *h = NULL;
96bcf938 1763 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7d367e06 1764 struct nf_conn *ct;
c1d10adb 1765 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1766 u16 zone;
1767 int err;
1768
1769 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1770 if (err < 0)
1771 return err;
c1d10adb 1772
df6fb868 1773 if (cda[CTA_TUPLE_ORIG]) {
c1d10adb
PNA
1774 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1775 if (err < 0)
1776 return err;
1777 }
1778
df6fb868 1779 if (cda[CTA_TUPLE_REPLY]) {
c1d10adb
PNA
1780 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1781 if (err < 0)
1782 return err;
1783 }
1784
df6fb868 1785 if (cda[CTA_TUPLE_ORIG])
7d367e06 1786 h = nf_conntrack_find_get(net, zone, &otuple);
df6fb868 1787 else if (cda[CTA_TUPLE_REPLY])
7d367e06 1788 h = nf_conntrack_find_get(net, zone, &rtuple);
c1d10adb
PNA
1789
1790 if (h == NULL) {
c1d10adb 1791 err = -ENOENT;
f0a3c086 1792 if (nlh->nlmsg_flags & NLM_F_CREATE) {
fecc1133 1793 enum ip_conntrack_events events;
5faa1f4c 1794
442fad94
FW
1795 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1796 return -EINVAL;
1797
ef00f89f 1798 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
f0a3c086 1799 &rtuple, u3);
7d367e06
JK
1800 if (IS_ERR(ct))
1801 return PTR_ERR(ct);
1802
f0a3c086 1803 err = 0;
fecc1133
PNA
1804 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1805 events = IPCT_RELATED;
1806 else
1807 events = IPCT_NEW;
1808
9b21f6a9
FW
1809 if (cda[CTA_LABELS] &&
1810 ctnetlink_attach_labels(ct, cda) == 0)
1811 events |= (1 << IPCT_LABEL);
1812
858b3133
PM
1813 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1814 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1815 (1 << IPCT_HELPER) |
1816 (1 << IPCT_PROTOINFO) |
41d73ec0 1817 (1 << IPCT_SEQADJ) |
a0891aa6 1818 (1 << IPCT_MARK) | events,
15e47304 1819 ct, NETLINK_CB(skb).portid,
a0891aa6 1820 nlmsg_report(nlh));
f0a3c086 1821 nf_ct_put(ct);
7d367e06 1822 }
5faa1f4c 1823
c1d10adb
PNA
1824 return err;
1825 }
1826 /* implicit 'else' */
1827
c1d10adb 1828 err = -EEXIST;
7d367e06 1829 ct = nf_ct_tuplehash_to_ctrack(h);
ff4ca827 1830 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
ca7433df 1831 spin_lock_bh(&nf_conntrack_expect_lock);
19abb7b0 1832 err = ctnetlink_change_conntrack(ct, cda);
ca7433df 1833 spin_unlock_bh(&nf_conntrack_expect_lock);
19abb7b0 1834 if (err == 0) {
858b3133
PM
1835 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1836 (1 << IPCT_ASSURED) |
a0891aa6 1837 (1 << IPCT_HELPER) |
797a7d66 1838 (1 << IPCT_LABEL) |
a0891aa6 1839 (1 << IPCT_PROTOINFO) |
41d73ec0 1840 (1 << IPCT_SEQADJ) |
a0891aa6 1841 (1 << IPCT_MARK),
15e47304 1842 ct, NETLINK_CB(skb).portid,
a0891aa6 1843 nlmsg_report(nlh));
7d367e06 1844 }
ff4ca827 1845 }
c1d10adb 1846
7d367e06 1847 nf_ct_put(ct);
c1d10adb
PNA
1848 return err;
1849}
1850
392025f8 1851static int
15e47304 1852ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
392025f8
PNA
1853 __u16 cpu, const struct ip_conntrack_stat *st)
1854{
1855 struct nlmsghdr *nlh;
1856 struct nfgenmsg *nfmsg;
15e47304 1857 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
392025f8
PNA
1858
1859 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
15e47304 1860 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
392025f8
PNA
1861 if (nlh == NULL)
1862 goto nlmsg_failure;
1863
1864 nfmsg = nlmsg_data(nlh);
1865 nfmsg->nfgen_family = AF_UNSPEC;
1866 nfmsg->version = NFNETLINK_V0;
1867 nfmsg->res_id = htons(cpu);
1868
1869 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1870 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1871 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1872 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1873 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1874 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1875 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1876 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1877 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1878 htonl(st->insert_failed)) ||
1879 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1880 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1881 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1882 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1883 htonl(st->search_restart)))
1884 goto nla_put_failure;
1885
1886 nlmsg_end(skb, nlh);
1887 return skb->len;
1888
1889nla_put_failure:
1890nlmsg_failure:
1891 nlmsg_cancel(skb, nlh);
1892 return -1;
1893}
1894
1895static int
1896ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
1897{
1898 int cpu;
1899 struct net *net = sock_net(skb->sk);
1900
1901 if (cb->args[0] == nr_cpu_ids)
1902 return 0;
1903
1904 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1905 const struct ip_conntrack_stat *st;
1906
1907 if (!cpu_possible(cpu))
1908 continue;
1909
1910 st = per_cpu_ptr(net->ct.stat, cpu);
1911 if (ctnetlink_ct_stat_cpu_fill_info(skb,
15e47304 1912 NETLINK_CB(cb->skb).portid,
392025f8
PNA
1913 cb->nlh->nlmsg_seq,
1914 cpu, st) < 0)
1915 break;
1916 }
1917 cb->args[0] = cpu;
1918
1919 return skb->len;
1920}
1921
1922static int
1923ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb,
1924 const struct nlmsghdr *nlh,
1925 const struct nlattr * const cda[])
1926{
1927 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1928 struct netlink_dump_control c = {
1929 .dump = ctnetlink_ct_stat_cpu_dump,
1930 };
1931 return netlink_dump_start(ctnl, skb, nlh, &c);
1932 }
1933
1934 return 0;
1935}
1936
1937static int
15e47304 1938ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
392025f8
PNA
1939 struct net *net)
1940{
1941 struct nlmsghdr *nlh;
1942 struct nfgenmsg *nfmsg;
15e47304 1943 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
392025f8
PNA
1944 unsigned int nr_conntracks = atomic_read(&net->ct.count);
1945
1946 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
15e47304 1947 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
392025f8
PNA
1948 if (nlh == NULL)
1949 goto nlmsg_failure;
1950
1951 nfmsg = nlmsg_data(nlh);
1952 nfmsg->nfgen_family = AF_UNSPEC;
1953 nfmsg->version = NFNETLINK_V0;
1954 nfmsg->res_id = 0;
1955
1956 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
1957 goto nla_put_failure;
1958
1959 nlmsg_end(skb, nlh);
1960 return skb->len;
1961
1962nla_put_failure:
1963nlmsg_failure:
1964 nlmsg_cancel(skb, nlh);
1965 return -1;
1966}
1967
1968static int
1969ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb,
1970 const struct nlmsghdr *nlh,
1971 const struct nlattr * const cda[])
1972{
1973 struct sk_buff *skb2;
1974 int err;
1975
1976 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1977 if (skb2 == NULL)
1978 return -ENOMEM;
1979
15e47304 1980 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
392025f8
PNA
1981 nlh->nlmsg_seq,
1982 NFNL_MSG_TYPE(nlh->nlmsg_type),
1983 sock_net(skb->sk));
1984 if (err <= 0)
1985 goto free;
1986
15e47304 1987 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
392025f8
PNA
1988 if (err < 0)
1989 goto out;
1990
1991 return 0;
1992
1993free:
1994 kfree_skb(skb2);
1995out:
1996 /* this avoids a loop in nfnetlink. */
1997 return err == -EAGAIN ? -ENOBUFS : err;
1998}
1999
bd077937
PNA
2000static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2001 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2002 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2003 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2004 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2005 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2006 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2007 .len = NF_CT_HELPER_NAME_LEN - 1 },
2008 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2009 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2010 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2011 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2012 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2013};
2014
2015static struct nf_conntrack_expect *
2016ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2017 struct nf_conntrack_helper *helper,
2018 struct nf_conntrack_tuple *tuple,
2019 struct nf_conntrack_tuple *mask);
2020
7c622345 2021#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
9cb01766
PNA
2022static size_t
2023ctnetlink_nfqueue_build_size(const struct nf_conn *ct)
2024{
2025 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2026 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2027 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2028 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2029 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2030 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2031 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2032 + nla_total_size(0) /* CTA_PROTOINFO */
2033 + nla_total_size(0) /* CTA_HELP */
2034 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2035 + ctnetlink_secctx_size(ct)
2036#ifdef CONFIG_NF_NAT_NEEDED
2037 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2038 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2039#endif
2040#ifdef CONFIG_NF_CONNTRACK_MARK
2041 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2042#endif
2043 + ctnetlink_proto_size(ct)
2044 ;
2045}
2046
2047static int
2048ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct)
2049{
2050 struct nlattr *nest_parms;
2051
2052 rcu_read_lock();
2053 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2054 if (!nest_parms)
2055 goto nla_put_failure;
2056 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2057 goto nla_put_failure;
2058 nla_nest_end(skb, nest_parms);
2059
2060 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2061 if (!nest_parms)
2062 goto nla_put_failure;
2063 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2064 goto nla_put_failure;
2065 nla_nest_end(skb, nest_parms);
2066
2067 if (nf_ct_zone(ct)) {
2068 if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
2069 goto nla_put_failure;
2070 }
2071
2072 if (ctnetlink_dump_id(skb, ct) < 0)
2073 goto nla_put_failure;
2074
2075 if (ctnetlink_dump_status(skb, ct) < 0)
2076 goto nla_put_failure;
2077
2078 if (ctnetlink_dump_timeout(skb, ct) < 0)
2079 goto nla_put_failure;
2080
2081 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2082 goto nla_put_failure;
2083
2084 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2085 goto nla_put_failure;
2086
2087#ifdef CONFIG_NF_CONNTRACK_SECMARK
2088 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2089 goto nla_put_failure;
2090#endif
2091 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2092 goto nla_put_failure;
2093
2094 if ((ct->status & IPS_SEQ_ADJUST) &&
41d73ec0 2095 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
9cb01766
PNA
2096 goto nla_put_failure;
2097
2098#ifdef CONFIG_NF_CONNTRACK_MARK
2099 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2100 goto nla_put_failure;
2101#endif
0ceabd83
FW
2102 if (ctnetlink_dump_labels(skb, ct) < 0)
2103 goto nla_put_failure;
9cb01766
PNA
2104 rcu_read_unlock();
2105 return 0;
2106
2107nla_put_failure:
2108 rcu_read_unlock();
2109 return -ENOSPC;
2110}
2111
2112static int
2113ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2114{
2115 int err;
2116
2117 if (cda[CTA_TIMEOUT]) {
2118 err = ctnetlink_change_timeout(ct, cda);
2119 if (err < 0)
2120 return err;
2121 }
2122 if (cda[CTA_STATUS]) {
2123 err = ctnetlink_change_status(ct, cda);
2124 if (err < 0)
2125 return err;
2126 }
2127 if (cda[CTA_HELP]) {
2128 err = ctnetlink_change_helper(ct, cda);
2129 if (err < 0)
2130 return err;
2131 }
9b21f6a9
FW
2132 if (cda[CTA_LABELS]) {
2133 err = ctnetlink_attach_labels(ct, cda);
2134 if (err < 0)
2135 return err;
2136 }
9cb01766 2137#if defined(CONFIG_NF_CONNTRACK_MARK)
534473c6
FW
2138 if (cda[CTA_MARK]) {
2139 u32 mask = 0, mark, newmark;
2140 if (cda[CTA_MARK_MASK])
2141 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2142
2143 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2144 newmark = (ct->mark & mask) ^ mark;
2145 if (newmark != ct->mark)
2146 ct->mark = newmark;
2147 }
9cb01766
PNA
2148#endif
2149 return 0;
2150}
2151
2152static int
2153ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct)
2154{
2155 struct nlattr *cda[CTA_MAX+1];
68e035c9 2156 int ret;
9cb01766 2157
130ffbc2
DB
2158 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2159 if (ret < 0)
2160 return ret;
9cb01766 2161
ca7433df 2162 spin_lock_bh(&nf_conntrack_expect_lock);
68e035c9 2163 ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct);
ca7433df 2164 spin_unlock_bh(&nf_conntrack_expect_lock);
68e035c9
PNA
2165
2166 return ret;
9cb01766
PNA
2167}
2168
bd077937
PNA
2169static int ctnetlink_nfqueue_exp_parse(const struct nlattr * const *cda,
2170 const struct nf_conn *ct,
2171 struct nf_conntrack_tuple *tuple,
2172 struct nf_conntrack_tuple *mask)
2173{
2174 int err;
2175
2176 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2177 nf_ct_l3num(ct));
2178 if (err < 0)
2179 return err;
2180
2181 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2182 nf_ct_l3num(ct));
2183}
2184
2185static int
2186ctnetlink_nfqueue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2187 u32 portid, u32 report)
2188{
2189 struct nlattr *cda[CTA_EXPECT_MAX+1];
2190 struct nf_conntrack_tuple tuple, mask;
b7e092c0 2191 struct nf_conntrack_helper *helper = NULL;
bd077937
PNA
2192 struct nf_conntrack_expect *exp;
2193 int err;
2194
2195 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy);
2196 if (err < 0)
2197 return err;
2198
2199 err = ctnetlink_nfqueue_exp_parse((const struct nlattr * const *)cda,
2200 ct, &tuple, &mask);
2201 if (err < 0)
2202 return err;
2203
2204 if (cda[CTA_EXPECT_HELP_NAME]) {
2205 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2206
2207 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2208 nf_ct_protonum(ct));
2209 if (helper == NULL)
2210 return -EOPNOTSUPP;
2211 }
2212
2213 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2214 helper, &tuple, &mask);
2215 if (IS_ERR(exp))
2216 return PTR_ERR(exp);
2217
2218 err = nf_ct_expect_related_report(exp, portid, report);
2219 if (err < 0) {
2220 nf_ct_expect_put(exp);
2221 return err;
2222 }
2223
2224 return 0;
2225}
2226
9cb01766
PNA
2227static struct nfq_ct_hook ctnetlink_nfqueue_hook = {
2228 .build_size = ctnetlink_nfqueue_build_size,
2229 .build = ctnetlink_nfqueue_build,
2230 .parse = ctnetlink_nfqueue_parse,
bd077937 2231 .attach_expect = ctnetlink_nfqueue_attach_expect,
41d73ec0 2232 .seq_adjust = nf_ct_tcp_seqadj_set,
9cb01766 2233};
7c622345 2234#endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
9cb01766 2235
601e68e1
YH
2236/***********************************************************************
2237 * EXPECT
2238 ***********************************************************************/
c1d10adb
PNA
2239
2240static inline int
2241ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2242 const struct nf_conntrack_tuple *tuple,
2243 enum ctattr_expect type)
2244{
df6fb868 2245 struct nlattr *nest_parms;
601e68e1 2246
df6fb868
PM
2247 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2248 if (!nest_parms)
2249 goto nla_put_failure;
c1d10adb 2250 if (ctnetlink_dump_tuples(skb, tuple) < 0)
df6fb868
PM
2251 goto nla_put_failure;
2252 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
2253
2254 return 0;
2255
df6fb868 2256nla_put_failure:
c1d10adb 2257 return -1;
601e68e1 2258}
c1d10adb 2259
1cde6436
PNA
2260static inline int
2261ctnetlink_exp_dump_mask(struct sk_buff *skb,
2262 const struct nf_conntrack_tuple *tuple,
d4156e8c 2263 const struct nf_conntrack_tuple_mask *mask)
1cde6436
PNA
2264{
2265 int ret;
2266 struct nf_conntrack_l3proto *l3proto;
605dcad6 2267 struct nf_conntrack_l4proto *l4proto;
d4156e8c 2268 struct nf_conntrack_tuple m;
df6fb868 2269 struct nlattr *nest_parms;
d4156e8c
PM
2270
2271 memset(&m, 0xFF, sizeof(m));
d4156e8c 2272 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
e578756c
PM
2273 m.src.u.all = mask->src.u.all;
2274 m.dst.protonum = tuple->dst.protonum;
d4156e8c 2275
df6fb868
PM
2276 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2277 if (!nest_parms)
2278 goto nla_put_failure;
1cde6436 2279
3b988ece 2280 rcu_read_lock();
528a3a6f 2281 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
d4156e8c 2282 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
3b988ece
HS
2283 if (ret >= 0) {
2284 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2285 tuple->dst.protonum);
d4156e8c 2286 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
3b988ece
HS
2287 }
2288 rcu_read_unlock();
2289
1cde6436 2290 if (unlikely(ret < 0))
df6fb868 2291 goto nla_put_failure;
1cde6436 2292
df6fb868 2293 nla_nest_end(skb, nest_parms);
1cde6436
PNA
2294
2295 return 0;
2296
df6fb868 2297nla_put_failure:
1cde6436
PNA
2298 return -1;
2299}
2300
c7232c99
PM
2301static const union nf_inet_addr any_addr;
2302
bb5cf80e 2303static int
c1d10adb 2304ctnetlink_exp_dump_expect(struct sk_buff *skb,
601e68e1 2305 const struct nf_conntrack_expect *exp)
c1d10adb
PNA
2306{
2307 struct nf_conn *master = exp->master;
c1216382 2308 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
bc01befd 2309 struct nf_conn_help *help;
076a0ca0
PNA
2310#ifdef CONFIG_NF_NAT_NEEDED
2311 struct nlattr *nest_parms;
2312 struct nf_conntrack_tuple nat_tuple = {};
2313#endif
544d5c7d
PNA
2314 struct nf_ct_helper_expectfn *expfn;
2315
d978e5da
PM
2316 if (timeout < 0)
2317 timeout = 0;
c1d10adb
PNA
2318
2319 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
df6fb868 2320 goto nla_put_failure;
1cde6436 2321 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
df6fb868 2322 goto nla_put_failure;
c1d10adb
PNA
2323 if (ctnetlink_exp_dump_tuple(skb,
2324 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2325 CTA_EXPECT_MASTER) < 0)
df6fb868 2326 goto nla_put_failure;
601e68e1 2327
076a0ca0 2328#ifdef CONFIG_NF_NAT_NEEDED
c7232c99
PM
2329 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2330 exp->saved_proto.all) {
076a0ca0
PNA
2331 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2332 if (!nest_parms)
2333 goto nla_put_failure;
2334
cc1eb431
DM
2335 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2336 goto nla_put_failure;
076a0ca0
PNA
2337
2338 nat_tuple.src.l3num = nf_ct_l3num(master);
c7232c99 2339 nat_tuple.src.u3 = exp->saved_addr;
076a0ca0
PNA
2340 nat_tuple.dst.protonum = nf_ct_protonum(master);
2341 nat_tuple.src.u = exp->saved_proto;
2342
2343 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2344 CTA_EXPECT_NAT_TUPLE) < 0)
2345 goto nla_put_failure;
2346 nla_nest_end(skb, nest_parms);
2347 }
2348#endif
cc1eb431
DM
2349 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2350 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2351 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2352 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2353 goto nla_put_failure;
bc01befd
PNA
2354 help = nfct_help(master);
2355 if (help) {
2356 struct nf_conntrack_helper *helper;
2357
2358 helper = rcu_dereference(help->helper);
cc1eb431
DM
2359 if (helper &&
2360 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2361 goto nla_put_failure;
bc01befd 2362 }
544d5c7d 2363 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
cc1eb431
DM
2364 if (expfn != NULL &&
2365 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2366 goto nla_put_failure;
c1d10adb
PNA
2367
2368 return 0;
601e68e1 2369
df6fb868 2370nla_put_failure:
c1d10adb
PNA
2371 return -1;
2372}
2373
2374static int
15e47304 2375ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
8b0a231d 2376 int event, const struct nf_conntrack_expect *exp)
c1d10adb
PNA
2377{
2378 struct nlmsghdr *nlh;
2379 struct nfgenmsg *nfmsg;
15e47304 2380 unsigned int flags = portid ? NLM_F_MULTI : 0;
c1d10adb
PNA
2381
2382 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
15e47304 2383 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
96bcf938
PNA
2384 if (nlh == NULL)
2385 goto nlmsg_failure;
c1d10adb 2386
96bcf938 2387 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
2388 nfmsg->nfgen_family = exp->tuple.src.l3num;
2389 nfmsg->version = NFNETLINK_V0;
2390 nfmsg->res_id = 0;
2391
2392 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 2393 goto nla_put_failure;
c1d10adb 2394
96bcf938 2395 nlmsg_end(skb, nlh);
c1d10adb
PNA
2396 return skb->len;
2397
2398nlmsg_failure:
df6fb868 2399nla_put_failure:
96bcf938 2400 nlmsg_cancel(skb, nlh);
c1d10adb
PNA
2401 return -1;
2402}
2403
2404#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
2405static int
2406ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
c1d10adb 2407{
9592a5c0
AD
2408 struct nf_conntrack_expect *exp = item->exp;
2409 struct net *net = nf_ct_exp_net(exp);
c1d10adb
PNA
2410 struct nlmsghdr *nlh;
2411 struct nfgenmsg *nfmsg;
c1d10adb 2412 struct sk_buff *skb;
ebbf41df 2413 unsigned int type, group;
c1d10adb
PNA
2414 int flags = 0;
2415
ebbf41df
PNA
2416 if (events & (1 << IPEXP_DESTROY)) {
2417 type = IPCTNL_MSG_EXP_DELETE;
2418 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2419 } else if (events & (1 << IPEXP_NEW)) {
c1d10adb
PNA
2420 type = IPCTNL_MSG_EXP_NEW;
2421 flags = NLM_F_CREATE|NLM_F_EXCL;
ebbf41df 2422 group = NFNLGRP_CONNTRACK_EXP_NEW;
c1d10adb 2423 } else
e34d5c1a 2424 return 0;
c1d10adb 2425
ebbf41df 2426 if (!item->report && !nfnetlink_has_listeners(net, group))
e34d5c1a 2427 return 0;
b3a27bfb 2428
96bcf938
PNA
2429 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2430 if (skb == NULL)
150ace0d 2431 goto errout;
c1d10adb 2432
b633ad5f 2433 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
15e47304 2434 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
96bcf938
PNA
2435 if (nlh == NULL)
2436 goto nlmsg_failure;
c1d10adb 2437
96bcf938 2438 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
2439 nfmsg->nfgen_family = exp->tuple.src.l3num;
2440 nfmsg->version = NFNETLINK_V0;
2441 nfmsg->res_id = 0;
2442
528a3a6f 2443 rcu_read_lock();
c1d10adb 2444 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 2445 goto nla_put_failure;
528a3a6f 2446 rcu_read_unlock();
c1d10adb 2447
96bcf938 2448 nlmsg_end(skb, nlh);
15e47304 2449 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
e34d5c1a 2450 return 0;
c1d10adb 2451
df6fb868 2452nla_put_failure:
528a3a6f 2453 rcu_read_unlock();
96bcf938 2454 nlmsg_cancel(skb, nlh);
528a3a6f 2455nlmsg_failure:
c1d10adb 2456 kfree_skb(skb);
150ace0d 2457errout:
9592a5c0 2458 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
e34d5c1a 2459 return 0;
c1d10adb
PNA
2460}
2461#endif
cf6994c2
PM
2462static int ctnetlink_exp_done(struct netlink_callback *cb)
2463{
31f15875
PM
2464 if (cb->args[1])
2465 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
cf6994c2
PM
2466 return 0;
2467}
c1d10adb
PNA
2468
2469static int
2470ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2471{
9592a5c0 2472 struct net *net = sock_net(skb->sk);
cf6994c2 2473 struct nf_conntrack_expect *exp, *last;
96bcf938 2474 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
87711cb8 2475 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 2476
7d0742da 2477 rcu_read_lock();
31f15875
PM
2478 last = (struct nf_conntrack_expect *)cb->args[1];
2479 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
cf6994c2 2480restart:
b67bfe0d 2481 hlist_for_each_entry(exp, &net->ct.expect_hash[cb->args[0]],
31f15875
PM
2482 hnode) {
2483 if (l3proto && exp->tuple.src.l3num != l3proto)
cf6994c2 2484 continue;
31f15875
PM
2485 if (cb->args[1]) {
2486 if (exp != last)
2487 continue;
2488 cb->args[1] = 0;
2489 }
8b0a231d 2490 if (ctnetlink_exp_fill_info(skb,
15e47304 2491 NETLINK_CB(cb->skb).portid,
31f15875
PM
2492 cb->nlh->nlmsg_seq,
2493 IPCTNL_MSG_EXP_NEW,
8b0a231d 2494 exp) < 0) {
7d0742da
PM
2495 if (!atomic_inc_not_zero(&exp->use))
2496 continue;
31f15875
PM
2497 cb->args[1] = (unsigned long)exp;
2498 goto out;
2499 }
cf6994c2 2500 }
31f15875
PM
2501 if (cb->args[1]) {
2502 cb->args[1] = 0;
2503 goto restart;
cf6994c2
PM
2504 }
2505 }
601e68e1 2506out:
7d0742da 2507 rcu_read_unlock();
cf6994c2
PM
2508 if (last)
2509 nf_ct_expect_put(last);
c1d10adb 2510
c1d10adb
PNA
2511 return skb->len;
2512}
2513
e844a928
PNA
2514static int
2515ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2516{
2517 struct nf_conntrack_expect *exp, *last;
2518 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2519 struct nf_conn *ct = cb->data;
2520 struct nf_conn_help *help = nfct_help(ct);
2521 u_int8_t l3proto = nfmsg->nfgen_family;
2522
2523 if (cb->args[0])
2524 return 0;
2525
2526 rcu_read_lock();
2527 last = (struct nf_conntrack_expect *)cb->args[1];
2528restart:
2529 hlist_for_each_entry(exp, &help->expectations, lnode) {
2530 if (l3proto && exp->tuple.src.l3num != l3proto)
2531 continue;
2532 if (cb->args[1]) {
2533 if (exp != last)
2534 continue;
2535 cb->args[1] = 0;
2536 }
2537 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2538 cb->nlh->nlmsg_seq,
2539 IPCTNL_MSG_EXP_NEW,
2540 exp) < 0) {
2541 if (!atomic_inc_not_zero(&exp->use))
2542 continue;
2543 cb->args[1] = (unsigned long)exp;
2544 goto out;
2545 }
2546 }
2547 if (cb->args[1]) {
2548 cb->args[1] = 0;
2549 goto restart;
2550 }
2551 cb->args[0] = 1;
2552out:
2553 rcu_read_unlock();
2554 if (last)
2555 nf_ct_expect_put(last);
2556
2557 return skb->len;
2558}
2559
2560static int ctnetlink_dump_exp_ct(struct sock *ctnl, struct sk_buff *skb,
2561 const struct nlmsghdr *nlh,
2562 const struct nlattr * const cda[])
2563{
2564 int err;
2565 struct net *net = sock_net(ctnl);
2566 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2567 u_int8_t u3 = nfmsg->nfgen_family;
2568 struct nf_conntrack_tuple tuple;
2569 struct nf_conntrack_tuple_hash *h;
2570 struct nf_conn *ct;
2571 u16 zone = 0;
2572 struct netlink_dump_control c = {
2573 .dump = ctnetlink_exp_ct_dump_table,
2574 .done = ctnetlink_exp_done,
2575 };
2576
2577 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2578 if (err < 0)
2579 return err;
2580
2581 if (cda[CTA_EXPECT_ZONE]) {
2582 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2583 if (err < 0)
2584 return err;
2585 }
2586
2587 h = nf_conntrack_find_get(net, zone, &tuple);
2588 if (!h)
2589 return -ENOENT;
2590
2591 ct = nf_ct_tuplehash_to_ctrack(h);
2592 c.data = ct;
2593
2594 err = netlink_dump_start(ctnl, skb, nlh, &c);
2595 nf_ct_put(ct);
2596
2597 return err;
2598}
2599
c1d10adb 2600static int
601e68e1 2601ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2602 const struct nlmsghdr *nlh,
2603 const struct nlattr * const cda[])
c1d10adb 2604{
9592a5c0 2605 struct net *net = sock_net(ctnl);
c1d10adb
PNA
2606 struct nf_conntrack_tuple tuple;
2607 struct nf_conntrack_expect *exp;
2608 struct sk_buff *skb2;
96bcf938 2609 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 2610 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
2611 u16 zone;
2612 int err;
c1d10adb 2613
b8f3ab42 2614 if (nlh->nlmsg_flags & NLM_F_DUMP) {
e844a928
PNA
2615 if (cda[CTA_EXPECT_MASTER])
2616 return ctnetlink_dump_exp_ct(ctnl, skb, nlh, cda);
2617 else {
2618 struct netlink_dump_control c = {
2619 .dump = ctnetlink_exp_dump_table,
2620 .done = ctnetlink_exp_done,
2621 };
2622 return netlink_dump_start(ctnl, skb, nlh, &c);
2623 }
c1d10adb
PNA
2624 }
2625
ef00f89f
PM
2626 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2627 if (err < 0)
2628 return err;
2629
35dba1d7
PNA
2630 if (cda[CTA_EXPECT_TUPLE])
2631 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2632 else if (cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
2633 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2634 else
2635 return -EINVAL;
2636
2637 if (err < 0)
2638 return err;
2639
ef00f89f 2640 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
2641 if (!exp)
2642 return -ENOENT;
2643
df6fb868 2644 if (cda[CTA_EXPECT_ID]) {
77236b6e 2645 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 2646 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 2647 nf_ct_expect_put(exp);
c1d10adb
PNA
2648 return -ENOENT;
2649 }
601e68e1 2650 }
c1d10adb
PNA
2651
2652 err = -ENOMEM;
96bcf938 2653 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
81378f72
PNA
2654 if (skb2 == NULL) {
2655 nf_ct_expect_put(exp);
c1d10adb 2656 goto out;
81378f72 2657 }
4e9b8269 2658
528a3a6f 2659 rcu_read_lock();
15e47304 2660 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
8b0a231d 2661 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
528a3a6f 2662 rcu_read_unlock();
81378f72 2663 nf_ct_expect_put(exp);
c1d10adb
PNA
2664 if (err <= 0)
2665 goto free;
2666
15e47304 2667 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
81378f72
PNA
2668 if (err < 0)
2669 goto out;
c1d10adb 2670
81378f72 2671 return 0;
c1d10adb
PNA
2672
2673free:
2674 kfree_skb(skb2);
2675out:
81378f72
PNA
2676 /* this avoids a loop in nfnetlink. */
2677 return err == -EAGAIN ? -ENOBUFS : err;
c1d10adb
PNA
2678}
2679
2680static int
601e68e1 2681ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2682 const struct nlmsghdr *nlh,
2683 const struct nlattr * const cda[])
c1d10adb 2684{
9592a5c0 2685 struct net *net = sock_net(ctnl);
31f15875 2686 struct nf_conntrack_expect *exp;
c1d10adb 2687 struct nf_conntrack_tuple tuple;
96bcf938 2688 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
b67bfe0d 2689 struct hlist_node *next;
c1d10adb 2690 u_int8_t u3 = nfmsg->nfgen_family;
31f15875 2691 unsigned int i;
ef00f89f 2692 u16 zone;
c1d10adb
PNA
2693 int err;
2694
df6fb868 2695 if (cda[CTA_EXPECT_TUPLE]) {
c1d10adb 2696 /* delete a single expect by tuple */
ef00f89f
PM
2697 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2698 if (err < 0)
2699 return err;
2700
c1d10adb
PNA
2701 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2702 if (err < 0)
2703 return err;
2704
2705 /* bump usage count to 2 */
ef00f89f 2706 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
2707 if (!exp)
2708 return -ENOENT;
2709
df6fb868 2710 if (cda[CTA_EXPECT_ID]) {
77236b6e 2711 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 2712 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 2713 nf_ct_expect_put(exp);
c1d10adb
PNA
2714 return -ENOENT;
2715 }
2716 }
2717
2718 /* after list removal, usage count == 1 */
ca7433df 2719 spin_lock_bh(&nf_conntrack_expect_lock);
ebbf41df 2720 if (del_timer(&exp->timeout)) {
15e47304 2721 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
ebbf41df
PNA
2722 nlmsg_report(nlh));
2723 nf_ct_expect_put(exp);
2724 }
ca7433df 2725 spin_unlock_bh(&nf_conntrack_expect_lock);
601e68e1 2726 /* have to put what we 'get' above.
c1d10adb 2727 * after this line usage count == 0 */
6823645d 2728 nf_ct_expect_put(exp);
df6fb868
PM
2729 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2730 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
31f15875 2731 struct nf_conn_help *m_help;
c1d10adb
PNA
2732
2733 /* delete all expectations for this helper */
ca7433df 2734 spin_lock_bh(&nf_conntrack_expect_lock);
31f15875 2735 for (i = 0; i < nf_ct_expect_hsize; i++) {
b67bfe0d 2736 hlist_for_each_entry_safe(exp, next,
9592a5c0 2737 &net->ct.expect_hash[i],
31f15875
PM
2738 hnode) {
2739 m_help = nfct_help(exp->master);
794e6871
PM
2740 if (!strcmp(m_help->helper->name, name) &&
2741 del_timer(&exp->timeout)) {
ebbf41df 2742 nf_ct_unlink_expect_report(exp,
15e47304 2743 NETLINK_CB(skb).portid,
ebbf41df 2744 nlmsg_report(nlh));
31f15875
PM
2745 nf_ct_expect_put(exp);
2746 }
c1d10adb
PNA
2747 }
2748 }
ca7433df 2749 spin_unlock_bh(&nf_conntrack_expect_lock);
c1d10adb
PNA
2750 } else {
2751 /* This basically means we have to flush everything*/
ca7433df 2752 spin_lock_bh(&nf_conntrack_expect_lock);
31f15875 2753 for (i = 0; i < nf_ct_expect_hsize; i++) {
b67bfe0d 2754 hlist_for_each_entry_safe(exp, next,
9592a5c0 2755 &net->ct.expect_hash[i],
31f15875
PM
2756 hnode) {
2757 if (del_timer(&exp->timeout)) {
ebbf41df 2758 nf_ct_unlink_expect_report(exp,
15e47304 2759 NETLINK_CB(skb).portid,
ebbf41df 2760 nlmsg_report(nlh));
31f15875
PM
2761 nf_ct_expect_put(exp);
2762 }
c1d10adb
PNA
2763 }
2764 }
ca7433df 2765 spin_unlock_bh(&nf_conntrack_expect_lock);
c1d10adb
PNA
2766 }
2767
2768 return 0;
2769}
2770static int
39938324
PM
2771ctnetlink_change_expect(struct nf_conntrack_expect *x,
2772 const struct nlattr * const cda[])
c1d10adb 2773{
9768e1ac
KW
2774 if (cda[CTA_EXPECT_TIMEOUT]) {
2775 if (!del_timer(&x->timeout))
2776 return -ETIME;
2777
2778 x->timeout.expires = jiffies +
2779 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2780 add_timer(&x->timeout);
2781 }
2782 return 0;
c1d10adb
PNA
2783}
2784
076a0ca0
PNA
2785static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2786 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2787 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2788};
2789
2790static int
2791ctnetlink_parse_expect_nat(const struct nlattr *attr,
2792 struct nf_conntrack_expect *exp,
2793 u_int8_t u3)
2794{
2795#ifdef CONFIG_NF_NAT_NEEDED
2796 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2797 struct nf_conntrack_tuple nat_tuple = {};
2798 int err;
2799
130ffbc2
DB
2800 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2801 if (err < 0)
2802 return err;
076a0ca0
PNA
2803
2804 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2805 return -EINVAL;
2806
2807 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2808 &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2809 if (err < 0)
2810 return err;
2811
c7232c99 2812 exp->saved_addr = nat_tuple.src.u3;
076a0ca0
PNA
2813 exp->saved_proto = nat_tuple.src.u;
2814 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2815
2816 return 0;
2817#else
2818 return -EOPNOTSUPP;
2819#endif
2820}
2821
0ef71ee1
PNA
2822static struct nf_conntrack_expect *
2823ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
2824 struct nf_conntrack_helper *helper,
2825 struct nf_conntrack_tuple *tuple,
2826 struct nf_conntrack_tuple *mask)
c1d10adb 2827{
0ef71ee1 2828 u_int32_t class = 0;
c1d10adb 2829 struct nf_conntrack_expect *exp;
dc808fe2 2830 struct nf_conn_help *help;
0ef71ee1 2831 int err;
660fdb2a 2832
b8c5e52c
PNA
2833 if (cda[CTA_EXPECT_CLASS] && helper) {
2834 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
0ef71ee1
PNA
2835 if (class > helper->expect_class_max)
2836 return ERR_PTR(-EINVAL);
b8c5e52c 2837 }
6823645d 2838 exp = nf_ct_expect_alloc(ct);
0ef71ee1
PNA
2839 if (!exp)
2840 return ERR_PTR(-ENOMEM);
2841
bc01befd
PNA
2842 help = nfct_help(ct);
2843 if (!help) {
2844 if (!cda[CTA_EXPECT_TIMEOUT]) {
2845 err = -EINVAL;
1310b955 2846 goto err_out;
bc01befd
PNA
2847 }
2848 exp->timeout.expires =
2849 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
601e68e1 2850
bc01befd
PNA
2851 exp->flags = NF_CT_EXPECT_USERSPACE;
2852 if (cda[CTA_EXPECT_FLAGS]) {
2853 exp->flags |=
2854 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2855 }
2856 } else {
2857 if (cda[CTA_EXPECT_FLAGS]) {
2858 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2859 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2860 } else
2861 exp->flags = 0;
2862 }
544d5c7d
PNA
2863 if (cda[CTA_EXPECT_FN]) {
2864 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2865 struct nf_ct_helper_expectfn *expfn;
2866
2867 expfn = nf_ct_helper_expectfn_find_by_name(name);
2868 if (expfn == NULL) {
2869 err = -EINVAL;
2870 goto err_out;
2871 }
2872 exp->expectfn = expfn->expectfn;
2873 } else
2874 exp->expectfn = NULL;
601e68e1 2875
b8c5e52c 2876 exp->class = class;
c1d10adb 2877 exp->master = ct;
660fdb2a 2878 exp->helper = helper;
0ef71ee1
PNA
2879 exp->tuple = *tuple;
2880 exp->mask.src.u3 = mask->src.u3;
2881 exp->mask.src.u.all = mask->src.u.all;
c1d10adb 2882
076a0ca0
PNA
2883 if (cda[CTA_EXPECT_NAT]) {
2884 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
0ef71ee1 2885 exp, nf_ct_l3num(ct));
076a0ca0
PNA
2886 if (err < 0)
2887 goto err_out;
2888 }
0ef71ee1 2889 return exp;
076a0ca0 2890err_out:
6823645d 2891 nf_ct_expect_put(exp);
0ef71ee1
PNA
2892 return ERR_PTR(err);
2893}
2894
2895static int
2896ctnetlink_create_expect(struct net *net, u16 zone,
2897 const struct nlattr * const cda[],
2898 u_int8_t u3, u32 portid, int report)
2899{
2900 struct nf_conntrack_tuple tuple, mask, master_tuple;
2901 struct nf_conntrack_tuple_hash *h = NULL;
2902 struct nf_conntrack_helper *helper = NULL;
2903 struct nf_conntrack_expect *exp;
2904 struct nf_conn *ct;
2905 int err;
2906
2907 /* caller guarantees that those three CTA_EXPECT_* exist */
2908 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2909 if (err < 0)
2910 return err;
2911 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2912 if (err < 0)
2913 return err;
2914 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2915 if (err < 0)
2916 return err;
2917
2918 /* Look for master conntrack of this expectation */
2919 h = nf_conntrack_find_get(net, zone, &master_tuple);
2920 if (!h)
2921 return -ENOENT;
2922 ct = nf_ct_tuplehash_to_ctrack(h);
2923
2924 if (cda[CTA_EXPECT_HELP_NAME]) {
2925 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2926
2927 helper = __nf_conntrack_helper_find(helpname, u3,
2928 nf_ct_protonum(ct));
2929 if (helper == NULL) {
2930#ifdef CONFIG_MODULES
2931 if (request_module("nfct-helper-%s", helpname) < 0) {
2932 err = -EOPNOTSUPP;
2933 goto err_ct;
2934 }
2935 helper = __nf_conntrack_helper_find(helpname, u3,
2936 nf_ct_protonum(ct));
2937 if (helper) {
2938 err = -EAGAIN;
2939 goto err_ct;
2940 }
2941#endif
2942 err = -EOPNOTSUPP;
2943 goto err_ct;
2944 }
2945 }
2946
2947 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
2948 if (IS_ERR(exp)) {
2949 err = PTR_ERR(exp);
2950 goto err_ct;
2951 }
2952
2953 err = nf_ct_expect_related_report(exp, portid, report);
2954 if (err < 0)
2955 goto err_exp;
2956
2957 return 0;
2958err_exp:
2959 nf_ct_expect_put(exp);
2960err_ct:
2961 nf_ct_put(ct);
c1d10adb
PNA
2962 return err;
2963}
2964
2965static int
2966ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2967 const struct nlmsghdr *nlh,
2968 const struct nlattr * const cda[])
c1d10adb 2969{
9592a5c0 2970 struct net *net = sock_net(ctnl);
c1d10adb
PNA
2971 struct nf_conntrack_tuple tuple;
2972 struct nf_conntrack_expect *exp;
96bcf938 2973 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 2974 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
2975 u16 zone;
2976 int err;
c1d10adb 2977
df6fb868
PM
2978 if (!cda[CTA_EXPECT_TUPLE]
2979 || !cda[CTA_EXPECT_MASK]
2980 || !cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
2981 return -EINVAL;
2982
ef00f89f
PM
2983 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2984 if (err < 0)
2985 return err;
2986
c1d10adb
PNA
2987 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2988 if (err < 0)
2989 return err;
2990
ca7433df 2991 spin_lock_bh(&nf_conntrack_expect_lock);
ef00f89f 2992 exp = __nf_ct_expect_find(net, zone, &tuple);
c1d10adb
PNA
2993
2994 if (!exp) {
ca7433df 2995 spin_unlock_bh(&nf_conntrack_expect_lock);
c1d10adb 2996 err = -ENOENT;
19abb7b0 2997 if (nlh->nlmsg_flags & NLM_F_CREATE) {
ef00f89f 2998 err = ctnetlink_create_expect(net, zone, cda,
19abb7b0 2999 u3,
15e47304 3000 NETLINK_CB(skb).portid,
19abb7b0
PNA
3001 nlmsg_report(nlh));
3002 }
c1d10adb
PNA
3003 return err;
3004 }
3005
3006 err = -EEXIST;
3007 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3008 err = ctnetlink_change_expect(exp, cda);
ca7433df 3009 spin_unlock_bh(&nf_conntrack_expect_lock);
c1d10adb 3010
c1d10adb
PNA
3011 return err;
3012}
3013
392025f8 3014static int
15e47304 3015ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
392025f8
PNA
3016 const struct ip_conntrack_stat *st)
3017{
3018 struct nlmsghdr *nlh;
3019 struct nfgenmsg *nfmsg;
15e47304 3020 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
392025f8
PNA
3021
3022 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
15e47304 3023 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
392025f8
PNA
3024 if (nlh == NULL)
3025 goto nlmsg_failure;
3026
3027 nfmsg = nlmsg_data(nlh);
3028 nfmsg->nfgen_family = AF_UNSPEC;
3029 nfmsg->version = NFNETLINK_V0;
3030 nfmsg->res_id = htons(cpu);
3031
3032 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3033 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3034 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3035 goto nla_put_failure;
3036
3037 nlmsg_end(skb, nlh);
3038 return skb->len;
3039
3040nla_put_failure:
3041nlmsg_failure:
3042 nlmsg_cancel(skb, nlh);
3043 return -1;
3044}
3045
3046static int
3047ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3048{
3049 int cpu;
3050 struct net *net = sock_net(skb->sk);
3051
3052 if (cb->args[0] == nr_cpu_ids)
3053 return 0;
3054
3055 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3056 const struct ip_conntrack_stat *st;
3057
3058 if (!cpu_possible(cpu))
3059 continue;
3060
3061 st = per_cpu_ptr(net->ct.stat, cpu);
15e47304 3062 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
392025f8
PNA
3063 cb->nlh->nlmsg_seq,
3064 cpu, st) < 0)
3065 break;
3066 }
3067 cb->args[0] = cpu;
3068
3069 return skb->len;
3070}
3071
3072static int
3073ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb,
3074 const struct nlmsghdr *nlh,
3075 const struct nlattr * const cda[])
3076{
3077 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3078 struct netlink_dump_control c = {
3079 .dump = ctnetlink_exp_stat_cpu_dump,
3080 };
3081 return netlink_dump_start(ctnl, skb, nlh, &c);
3082 }
3083
3084 return 0;
3085}
3086
c1d10adb 3087#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
3088static struct nf_ct_event_notifier ctnl_notifier = {
3089 .fcn = ctnetlink_conntrack_event,
c1d10adb
PNA
3090};
3091
e34d5c1a
PNA
3092static struct nf_exp_event_notifier ctnl_notifier_exp = {
3093 .fcn = ctnetlink_expect_event,
c1d10adb
PNA
3094};
3095#endif
3096
7c8d4cb4 3097static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
c1d10adb 3098 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
f73e924c
PM
3099 .attr_count = CTA_MAX,
3100 .policy = ct_nla_policy },
c1d10adb 3101 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
3102 .attr_count = CTA_MAX,
3103 .policy = ct_nla_policy },
c1d10adb 3104 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
f73e924c
PM
3105 .attr_count = CTA_MAX,
3106 .policy = ct_nla_policy },
c1d10adb 3107 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
3108 .attr_count = CTA_MAX,
3109 .policy = ct_nla_policy },
392025f8
PNA
3110 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3111 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
d871befe
PNA
3112 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3113 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
c1d10adb
PNA
3114};
3115
7c8d4cb4 3116static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
c1d10adb 3117 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
f73e924c
PM
3118 .attr_count = CTA_EXPECT_MAX,
3119 .policy = exp_nla_policy },
c1d10adb 3120 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
f73e924c
PM
3121 .attr_count = CTA_EXPECT_MAX,
3122 .policy = exp_nla_policy },
c1d10adb 3123 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
f73e924c
PM
3124 .attr_count = CTA_EXPECT_MAX,
3125 .policy = exp_nla_policy },
392025f8 3126 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
c1d10adb
PNA
3127};
3128
7c8d4cb4 3129static const struct nfnetlink_subsystem ctnl_subsys = {
c1d10adb
PNA
3130 .name = "conntrack",
3131 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3132 .cb_count = IPCTNL_MSG_MAX,
3133 .cb = ctnl_cb,
3134};
3135
7c8d4cb4 3136static const struct nfnetlink_subsystem ctnl_exp_subsys = {
c1d10adb
PNA
3137 .name = "conntrack_expect",
3138 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3139 .cb_count = IPCTNL_MSG_EXP_MAX,
3140 .cb = ctnl_exp_cb,
3141};
3142
d2483dde 3143MODULE_ALIAS("ip_conntrack_netlink");
c1d10adb 3144MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
34f9a2e4 3145MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
c1d10adb 3146
70e9942f
PNA
3147static int __net_init ctnetlink_net_init(struct net *net)
3148{
3149#ifdef CONFIG_NF_CONNTRACK_EVENTS
3150 int ret;
3151
3152 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3153 if (ret < 0) {
3154 pr_err("ctnetlink_init: cannot register notifier.\n");
3155 goto err_out;
3156 }
3157
3158 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3159 if (ret < 0) {
3160 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3161 goto err_unreg_notifier;
3162 }
3163#endif
3164 return 0;
3165
3166#ifdef CONFIG_NF_CONNTRACK_EVENTS
3167err_unreg_notifier:
3168 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3169err_out:
3170 return ret;
3171#endif
3172}
3173
3174static void ctnetlink_net_exit(struct net *net)
3175{
3176#ifdef CONFIG_NF_CONNTRACK_EVENTS
3177 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3178 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3179#endif
3180}
3181
3182static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3183{
3184 struct net *net;
3185
3186 list_for_each_entry(net, net_exit_list, exit_list)
3187 ctnetlink_net_exit(net);
3188}
3189
3190static struct pernet_operations ctnetlink_net_ops = {
3191 .init = ctnetlink_net_init,
3192 .exit_batch = ctnetlink_net_exit_batch,
3193};
3194
c1d10adb
PNA
3195static int __init ctnetlink_init(void)
3196{
3197 int ret;
3198
654d0fbd 3199 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
c1d10adb
PNA
3200 ret = nfnetlink_subsys_register(&ctnl_subsys);
3201 if (ret < 0) {
654d0fbd 3202 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
c1d10adb
PNA
3203 goto err_out;
3204 }
3205
3206 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3207 if (ret < 0) {
654d0fbd 3208 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
c1d10adb
PNA
3209 goto err_unreg_subsys;
3210 }
3211
ef6acf68
JL
3212 ret = register_pernet_subsys(&ctnetlink_net_ops);
3213 if (ret < 0) {
70e9942f 3214 pr_err("ctnetlink_init: cannot register pernet operations\n");
c1d10adb
PNA
3215 goto err_unreg_exp_subsys;
3216 }
7c622345 3217#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
9cb01766
PNA
3218 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3219 RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook);
3220#endif
c1d10adb
PNA
3221 return 0;
3222
c1d10adb
PNA
3223err_unreg_exp_subsys:
3224 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
c1d10adb
PNA
3225err_unreg_subsys:
3226 nfnetlink_subsys_unregister(&ctnl_subsys);
3227err_out:
3228 return ret;
3229}
3230
3231static void __exit ctnetlink_exit(void)
3232{
654d0fbd 3233 pr_info("ctnetlink: unregistering from nfnetlink.\n");
c1d10adb 3234
70e9942f 3235 unregister_pernet_subsys(&ctnetlink_net_ops);
c1d10adb
PNA
3236 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3237 nfnetlink_subsys_unregister(&ctnl_subsys);
7c622345 3238#ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
9cb01766
PNA
3239 RCU_INIT_POINTER(nfq_ct_hook, NULL);
3240#endif
c1d10adb
PNA
3241}
3242
3243module_init(ctnetlink_init);
3244module_exit(ctnetlink_exit);
This page took 1.225702 seconds and 5 git commands to generate.