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