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