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