netfilter: ctnetlink: get and zero operations must be atomic
[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>
70e9942f 7 * (C) 2005-2011 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
PNA
39#include <net/netfilter/nf_conntrack_helper.h>
40#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 41#include <net/netfilter/nf_conntrack_l4proto.h>
5b1158e9 42#include <net/netfilter/nf_conntrack_tuple.h>
58401572 43#include <net/netfilter/nf_conntrack_acct.h>
ef00f89f 44#include <net/netfilter/nf_conntrack_zones.h>
a992ca2a 45#include <net/netfilter/nf_conntrack_timestamp.h>
5b1158e9
JK
46#ifdef CONFIG_NF_NAT_NEEDED
47#include <net/netfilter/nf_nat_core.h>
48#include <net/netfilter/nf_nat_protocol.h>
49#endif
c1d10adb
PNA
50
51#include <linux/netfilter/nfnetlink.h>
52#include <linux/netfilter/nfnetlink_conntrack.h>
53
54MODULE_LICENSE("GPL");
55
dc808fe2 56static char __initdata version[] = "0.93";
c1d10adb 57
c1d10adb 58static inline int
601e68e1 59ctnetlink_dump_tuples_proto(struct sk_buff *skb,
1cde6436 60 const struct nf_conntrack_tuple *tuple,
605dcad6 61 struct nf_conntrack_l4proto *l4proto)
c1d10adb 62{
c1d10adb 63 int ret = 0;
df6fb868 64 struct nlattr *nest_parms;
c1d10adb 65
df6fb868
PM
66 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
67 if (!nest_parms)
68 goto nla_put_failure;
77236b6e 69 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
c1d10adb 70
fdf70832
PM
71 if (likely(l4proto->tuple_to_nlattr))
72 ret = l4proto->tuple_to_nlattr(skb, tuple);
601e68e1 73
df6fb868 74 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
75
76 return ret;
77
df6fb868 78nla_put_failure:
c1d10adb
PNA
79 return -1;
80}
81
82static inline int
1cde6436
PNA
83ctnetlink_dump_tuples_ip(struct sk_buff *skb,
84 const struct nf_conntrack_tuple *tuple,
85 struct nf_conntrack_l3proto *l3proto)
c1d10adb 86{
c1d10adb 87 int ret = 0;
df6fb868
PM
88 struct nlattr *nest_parms;
89
90 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
91 if (!nest_parms)
92 goto nla_put_failure;
1cde6436 93
fdf70832
PM
94 if (likely(l3proto->tuple_to_nlattr))
95 ret = l3proto->tuple_to_nlattr(skb, tuple);
1cde6436 96
df6fb868 97 nla_nest_end(skb, nest_parms);
c1d10adb 98
1cde6436
PNA
99 return ret;
100
df6fb868 101nla_put_failure:
1cde6436
PNA
102 return -1;
103}
104
bb5cf80e 105static int
1cde6436
PNA
106ctnetlink_dump_tuples(struct sk_buff *skb,
107 const struct nf_conntrack_tuple *tuple)
108{
109 int ret;
110 struct nf_conntrack_l3proto *l3proto;
605dcad6 111 struct nf_conntrack_l4proto *l4proto;
1cde6436 112
528a3a6f 113 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1cde6436 114 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
c1d10adb
PNA
115
116 if (unlikely(ret < 0))
117 return ret;
118
528a3a6f 119 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
605dcad6 120 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
c1d10adb
PNA
121
122 return ret;
c1d10adb
PNA
123}
124
125static inline int
126ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
127{
77236b6e 128 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
c1d10adb
PNA
129 return 0;
130
df6fb868 131nla_put_failure:
c1d10adb
PNA
132 return -1;
133}
134
135static inline int
136ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
137{
77236b6e 138 long timeout = (ct->timeout.expires - jiffies) / HZ;
c1d10adb 139
77236b6e 140 if (timeout < 0)
c1d10adb 141 timeout = 0;
601e68e1 142
77236b6e 143 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
c1d10adb
PNA
144 return 0;
145
df6fb868 146nla_put_failure:
c1d10adb
PNA
147 return -1;
148}
149
150static inline int
440f0d58 151ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
c1d10adb 152{
5e8fbe2a 153 struct nf_conntrack_l4proto *l4proto;
df6fb868 154 struct nlattr *nest_proto;
c1d10adb
PNA
155 int ret;
156
528a3a6f
PNA
157 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
158 if (!l4proto->to_nlattr)
c1d10adb 159 return 0;
601e68e1 160
df6fb868
PM
161 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
162 if (!nest_proto)
163 goto nla_put_failure;
c1d10adb 164
fdf70832 165 ret = l4proto->to_nlattr(skb, nest_proto, ct);
c1d10adb 166
df6fb868 167 nla_nest_end(skb, nest_proto);
c1d10adb
PNA
168
169 return ret;
170
df6fb868 171nla_put_failure:
c1d10adb
PNA
172 return -1;
173}
174
175static inline int
176ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
177{
df6fb868 178 struct nlattr *nest_helper;
dc808fe2 179 const struct nf_conn_help *help = nfct_help(ct);
3c158f7f 180 struct nf_conntrack_helper *helper;
c1d10adb 181
3c158f7f 182 if (!help)
c1d10adb 183 return 0;
601e68e1 184
3c158f7f
PM
185 helper = rcu_dereference(help->helper);
186 if (!helper)
187 goto out;
188
df6fb868
PM
189 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
190 if (!nest_helper)
191 goto nla_put_failure;
77236b6e 192 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
c1d10adb 193
fdf70832
PM
194 if (helper->to_nlattr)
195 helper->to_nlattr(skb, ct);
c1d10adb 196
df6fb868 197 nla_nest_end(skb, nest_helper);
3c158f7f 198out:
c1d10adb
PNA
199 return 0;
200
df6fb868 201nla_put_failure:
c1d10adb
PNA
202 return -1;
203}
204
bb5cf80e 205static int
80e60e67
PNA
206dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes,
207 enum ip_conntrack_dir dir)
c1d10adb
PNA
208{
209 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
df6fb868 210 struct nlattr *nest_count;
c1d10adb 211
df6fb868
PM
212 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
213 if (!nest_count)
214 goto nla_put_failure;
215
80e60e67
PNA
216 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts));
217 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes));
c1d10adb 218
df6fb868 219 nla_nest_end(skb, nest_count);
c1d10adb
PNA
220
221 return 0;
222
df6fb868 223nla_put_failure:
c1d10adb
PNA
224 return -1;
225}
c1d10adb 226
80e60e67
PNA
227static int
228ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
229 enum ip_conntrack_dir dir, int type)
230{
231 struct nf_conn_counter *acct;
232 u64 pkts, bytes;
233
234 acct = nf_conn_acct_find(ct);
235 if (!acct)
236 return 0;
237
238 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
239 pkts = atomic64_xchg(&acct[dir].packets, 0);
240 bytes = atomic64_xchg(&acct[dir].bytes, 0);
241 } else {
242 pkts = atomic64_read(&acct[dir].packets);
243 bytes = atomic64_read(&acct[dir].bytes);
244 }
245 return dump_counters(skb, pkts, bytes, dir);
246}
247
a992ca2a
PNA
248static int
249ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
250{
251 struct nlattr *nest_count;
252 const struct nf_conn_tstamp *tstamp;
253
254 tstamp = nf_conn_tstamp_find(ct);
255 if (!tstamp)
256 return 0;
257
258 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
259 if (!nest_count)
260 goto nla_put_failure;
261
262 NLA_PUT_BE64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start));
263 if (tstamp->stop != 0) {
264 NLA_PUT_BE64(skb, CTA_TIMESTAMP_STOP,
265 cpu_to_be64(tstamp->stop));
266 }
267 nla_nest_end(skb, nest_count);
268
269 return 0;
270
271nla_put_failure:
272 return -1;
273}
274
c1d10adb
PNA
275#ifdef CONFIG_NF_CONNTRACK_MARK
276static inline int
277ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
278{
77236b6e 279 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
c1d10adb
PNA
280 return 0;
281
df6fb868 282nla_put_failure:
c1d10adb
PNA
283 return -1;
284}
285#else
286#define ctnetlink_dump_mark(a, b) (0)
287#endif
288
37fccd85
PNA
289#ifdef CONFIG_NF_CONNTRACK_SECMARK
290static inline int
1cc63249 291ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
37fccd85 292{
1cc63249
EP
293 struct nlattr *nest_secctx;
294 int len, ret;
295 char *secctx;
296
297 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
298 if (ret)
cba85b53 299 return 0;
1cc63249
EP
300
301 ret = -1;
302 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
303 if (!nest_secctx)
304 goto nla_put_failure;
37fccd85 305
1cc63249
EP
306 NLA_PUT_STRING(skb, CTA_SECCTX_NAME, secctx);
307 nla_nest_end(skb, nest_secctx);
308
309 ret = 0;
37fccd85 310nla_put_failure:
1cc63249
EP
311 security_release_secctx(secctx, len);
312 return ret;
37fccd85
PNA
313}
314#else
1cc63249 315#define ctnetlink_dump_secctx(a, b) (0)
37fccd85
PNA
316#endif
317
0f417ce9
PNA
318#define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
319
320static inline int
321ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
322{
323 struct nlattr *nest_parms;
324
325 if (!(ct->status & IPS_EXPECTED))
326 return 0;
327
328 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
329 if (!nest_parms)
330 goto nla_put_failure;
331 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
332 goto nla_put_failure;
333 nla_nest_end(skb, nest_parms);
334
335 return 0;
336
337nla_put_failure:
338 return -1;
339}
340
13eae15a 341#ifdef CONFIG_NF_NAT_NEEDED
bb5cf80e 342static int
13eae15a
PNA
343dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
344{
13eae15a
PNA
345 struct nlattr *nest_parms;
346
347 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
348 if (!nest_parms)
349 goto nla_put_failure;
350
77236b6e
PM
351 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
352 htonl(natseq->correction_pos));
353 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
354 htonl(natseq->offset_before));
355 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
356 htonl(natseq->offset_after));
13eae15a
PNA
357
358 nla_nest_end(skb, nest_parms);
359
360 return 0;
361
362nla_put_failure:
363 return -1;
364}
365
366static inline int
367ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
368{
369 struct nf_nat_seq *natseq;
370 struct nf_conn_nat *nat = nfct_nat(ct);
371
372 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
373 return 0;
374
375 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
376 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
377 return -1;
378
379 natseq = &nat->seq[IP_CT_DIR_REPLY];
380 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
381 return -1;
382
383 return 0;
384}
385#else
386#define ctnetlink_dump_nat_seq_adj(a, b) (0)
387#endif
388
c1d10adb
PNA
389static inline int
390ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
391{
77236b6e 392 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
c1d10adb
PNA
393 return 0;
394
df6fb868 395nla_put_failure:
c1d10adb
PNA
396 return -1;
397}
398
399static inline int
400ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
401{
77236b6e 402 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
c1d10adb
PNA
403 return 0;
404
df6fb868 405nla_put_failure:
c1d10adb
PNA
406 return -1;
407}
408
c1d10adb 409static int
80e60e67
PNA
410ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, u32 type,
411 struct nf_conn *ct)
c1d10adb
PNA
412{
413 struct nlmsghdr *nlh;
414 struct nfgenmsg *nfmsg;
df6fb868 415 struct nlattr *nest_parms;
80e60e67 416 unsigned int flags = pid ? NLM_F_MULTI : 0, event;
c1d10adb 417
80e60e67 418 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
96bcf938
PNA
419 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
420 if (nlh == NULL)
421 goto nlmsg_failure;
c1d10adb 422
96bcf938 423 nfmsg = nlmsg_data(nlh);
5e8fbe2a 424 nfmsg->nfgen_family = nf_ct_l3num(ct);
c1d10adb
PNA
425 nfmsg->version = NFNETLINK_V0;
426 nfmsg->res_id = 0;
427
df6fb868
PM
428 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
429 if (!nest_parms)
430 goto nla_put_failure;
f2f3e38c 431 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
df6fb868
PM
432 goto nla_put_failure;
433 nla_nest_end(skb, nest_parms);
601e68e1 434
df6fb868
PM
435 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
436 if (!nest_parms)
437 goto nla_put_failure;
f2f3e38c 438 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
df6fb868
PM
439 goto nla_put_failure;
440 nla_nest_end(skb, nest_parms);
c1d10adb 441
ef00f89f
PM
442 if (nf_ct_zone(ct))
443 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
444
c1d10adb
PNA
445 if (ctnetlink_dump_status(skb, ct) < 0 ||
446 ctnetlink_dump_timeout(skb, ct) < 0 ||
80e60e67
PNA
447 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 ||
448 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 ||
a992ca2a 449 ctnetlink_dump_timestamp(skb, ct) < 0 ||
c1d10adb
PNA
450 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
451 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
452 ctnetlink_dump_mark(skb, ct) < 0 ||
1cc63249 453 ctnetlink_dump_secctx(skb, ct) < 0 ||
c1d10adb 454 ctnetlink_dump_id(skb, ct) < 0 ||
13eae15a 455 ctnetlink_dump_use(skb, ct) < 0 ||
0f417ce9 456 ctnetlink_dump_master(skb, ct) < 0 ||
13eae15a 457 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
df6fb868 458 goto nla_put_failure;
c1d10adb 459
96bcf938 460 nlmsg_end(skb, nlh);
c1d10adb
PNA
461 return skb->len;
462
463nlmsg_failure:
df6fb868 464nla_put_failure:
96bcf938 465 nlmsg_cancel(skb, nlh);
c1d10adb
PNA
466 return -1;
467}
468
469#ifdef CONFIG_NF_CONNTRACK_EVENTS
03b64f51
PNA
470static inline size_t
471ctnetlink_proto_size(const struct nf_conn *ct)
2732c4e4
HE
472{
473 struct nf_conntrack_l3proto *l3proto;
474 struct nf_conntrack_l4proto *l4proto;
03b64f51
PNA
475 size_t len = 0;
476
477 rcu_read_lock();
478 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
479 len += l3proto->nla_size;
480
481 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
482 len += l4proto->nla_size;
483 rcu_read_unlock();
484
485 return len;
486}
487
d26e6a02
JP
488static inline size_t
489ctnetlink_counters_size(const struct nf_conn *ct)
490{
491 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
492 return 0;
493 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
494 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
495 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
496 ;
497}
498
cba85b53
PNA
499static inline int
500ctnetlink_secctx_size(const struct nf_conn *ct)
1cc63249 501{
cba85b53
PNA
502#ifdef CONFIG_NF_CONNTRACK_SECMARK
503 int len, ret;
1cc63249 504
cba85b53
PNA
505 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
506 if (ret)
507 return 0;
1cc63249 508
cba85b53
PNA
509 return nla_total_size(0) /* CTA_SECCTX */
510 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
511#else
512 return 0;
1cc63249 513#endif
cba85b53 514}
1cc63249 515
a992ca2a
PNA
516static inline size_t
517ctnetlink_timestamp_size(const struct nf_conn *ct)
518{
519#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
520 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
521 return 0;
522 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
523#else
524 return 0;
525#endif
526}
527
03b64f51
PNA
528static inline size_t
529ctnetlink_nlmsg_size(const struct nf_conn *ct)
530{
531 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
532 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
533 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
534 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
535 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
536 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
537 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
d26e6a02 538 + ctnetlink_counters_size(ct)
a992ca2a 539 + ctnetlink_timestamp_size(ct)
03b64f51
PNA
540 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
541 + nla_total_size(0) /* CTA_PROTOINFO */
542 + nla_total_size(0) /* CTA_HELP */
543 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
cba85b53 544 + ctnetlink_secctx_size(ct)
d271e8bd 545#ifdef CONFIG_NF_NAT_NEEDED
03b64f51
PNA
546 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
547 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
d271e8bd
HE
548#endif
549#ifdef CONFIG_NF_CONNTRACK_MARK
03b64f51 550 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
d271e8bd 551#endif
03b64f51
PNA
552 + ctnetlink_proto_size(ct)
553 ;
2732c4e4
HE
554}
555
e34d5c1a
PNA
556static int
557ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
c1d10adb 558{
9592a5c0 559 struct net *net;
c1d10adb
PNA
560 struct nlmsghdr *nlh;
561 struct nfgenmsg *nfmsg;
df6fb868 562 struct nlattr *nest_parms;
19abb7b0 563 struct nf_conn *ct = item->ct;
c1d10adb
PNA
564 struct sk_buff *skb;
565 unsigned int type;
c1d10adb 566 unsigned int flags = 0, group;
dd7669a9 567 int err;
c1d10adb
PNA
568
569 /* ignore our fake conntrack entry */
5bfddbd4 570 if (nf_ct_is_untracked(ct))
e34d5c1a 571 return 0;
c1d10adb 572
a0891aa6 573 if (events & (1 << IPCT_DESTROY)) {
c1d10adb
PNA
574 type = IPCTNL_MSG_CT_DELETE;
575 group = NFNLGRP_CONNTRACK_DESTROY;
a0891aa6 576 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
c1d10adb
PNA
577 type = IPCTNL_MSG_CT_NEW;
578 flags = NLM_F_CREATE|NLM_F_EXCL;
c1d10adb 579 group = NFNLGRP_CONNTRACK_NEW;
17e6e4ea 580 } else if (events) {
c1d10adb
PNA
581 type = IPCTNL_MSG_CT_NEW;
582 group = NFNLGRP_CONNTRACK_UPDATE;
583 } else
e34d5c1a 584 return 0;
a2427692 585
9592a5c0
AD
586 net = nf_ct_net(ct);
587 if (!item->report && !nfnetlink_has_listeners(net, group))
e34d5c1a 588 return 0;
a2427692 589
03b64f51
PNA
590 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
591 if (skb == NULL)
150ace0d 592 goto errout;
c1d10adb 593
c1d10adb 594 type |= NFNL_SUBSYS_CTNETLINK << 8;
96bcf938
PNA
595 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
596 if (nlh == NULL)
597 goto nlmsg_failure;
c1d10adb 598
96bcf938 599 nfmsg = nlmsg_data(nlh);
5e8fbe2a 600 nfmsg->nfgen_family = nf_ct_l3num(ct);
c1d10adb
PNA
601 nfmsg->version = NFNETLINK_V0;
602 nfmsg->res_id = 0;
603
528a3a6f 604 rcu_read_lock();
df6fb868
PM
605 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
606 if (!nest_parms)
607 goto nla_put_failure;
f2f3e38c 608 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
df6fb868
PM
609 goto nla_put_failure;
610 nla_nest_end(skb, nest_parms);
601e68e1 611
df6fb868
PM
612 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
613 if (!nest_parms)
614 goto nla_put_failure;
f2f3e38c 615 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
df6fb868
PM
616 goto nla_put_failure;
617 nla_nest_end(skb, nest_parms);
c1d10adb 618
ef00f89f
PM
619 if (nf_ct_zone(ct))
620 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
621
1eedf699
EL
622 if (ctnetlink_dump_id(skb, ct) < 0)
623 goto nla_put_failure;
624
e57dce60
FH
625 if (ctnetlink_dump_status(skb, ct) < 0)
626 goto nla_put_failure;
627
a0891aa6 628 if (events & (1 << IPCT_DESTROY)) {
80e60e67
PNA
629 if (ctnetlink_dump_counters(skb, ct,
630 IP_CT_DIR_ORIGINAL, type) < 0 ||
631 ctnetlink_dump_counters(skb, ct,
632 IP_CT_DIR_REPLY, type) < 0 ||
a992ca2a 633 ctnetlink_dump_timestamp(skb, ct) < 0)
df6fb868 634 goto nla_put_failure;
7b621c1e 635 } else {
7b621c1e 636 if (ctnetlink_dump_timeout(skb, ct) < 0)
df6fb868 637 goto nla_put_failure;
7b621c1e 638
a0891aa6 639 if (events & (1 << IPCT_PROTOINFO)
7b621c1e 640 && ctnetlink_dump_protoinfo(skb, ct) < 0)
df6fb868 641 goto nla_put_failure;
7b621c1e 642
a0891aa6 643 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
7b621c1e 644 && ctnetlink_dump_helpinfo(skb, ct) < 0)
df6fb868 645 goto nla_put_failure;
7b621c1e 646
ff660c80 647#ifdef CONFIG_NF_CONNTRACK_SECMARK
a0891aa6 648 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
1cc63249 649 && ctnetlink_dump_secctx(skb, ct) < 0)
37fccd85 650 goto nla_put_failure;
ff660c80 651#endif
7b621c1e 652
a0891aa6 653 if (events & (1 << IPCT_RELATED) &&
0f417ce9
PNA
654 ctnetlink_dump_master(skb, ct) < 0)
655 goto nla_put_failure;
656
a0891aa6 657 if (events & (1 << IPCT_NATSEQADJ) &&
13eae15a
PNA
658 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
659 goto nla_put_failure;
7b621c1e 660 }
b9a37e0c 661
a83099a6 662#ifdef CONFIG_NF_CONNTRACK_MARK
a0891aa6 663 if ((events & (1 << IPCT_MARK) || ct->mark)
a83099a6
EL
664 && ctnetlink_dump_mark(skb, ct) < 0)
665 goto nla_put_failure;
666#endif
528a3a6f 667 rcu_read_unlock();
a83099a6 668
96bcf938 669 nlmsg_end(skb, nlh);
9592a5c0 670 err = nfnetlink_send(skb, net, item->pid, group, item->report,
cd8c20b6 671 GFP_ATOMIC);
dd7669a9
PNA
672 if (err == -ENOBUFS || err == -EAGAIN)
673 return -ENOBUFS;
674
e34d5c1a 675 return 0;
c1d10adb 676
df6fb868 677nla_put_failure:
528a3a6f 678 rcu_read_unlock();
96bcf938 679 nlmsg_cancel(skb, nlh);
528a3a6f 680nlmsg_failure:
c1d10adb 681 kfree_skb(skb);
150ace0d 682errout:
37b7ef72
PNA
683 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
684 return -ENOBUFS;
685
e34d5c1a 686 return 0;
c1d10adb
PNA
687}
688#endif /* CONFIG_NF_CONNTRACK_EVENTS */
689
690static int ctnetlink_done(struct netlink_callback *cb)
691{
89f2e218
PM
692 if (cb->args[1])
693 nf_ct_put((struct nf_conn *)cb->args[1]);
c1d10adb
PNA
694 return 0;
695}
696
697static int
698ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
699{
9592a5c0 700 struct net *net = sock_net(skb->sk);
89f2e218 701 struct nf_conn *ct, *last;
c1d10adb 702 struct nf_conntrack_tuple_hash *h;
ea781f19 703 struct hlist_nulls_node *n;
96bcf938 704 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
87711cb8 705 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 706
13ee6ac5 707 spin_lock_bh(&nf_conntrack_lock);
d205dc40 708 last = (struct nf_conn *)cb->args[1];
9ab99d5a 709 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
89f2e218 710restart:
13ee6ac5 711 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
ea781f19 712 hnnode) {
5b1158e9 713 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
c1d10adb
PNA
714 continue;
715 ct = nf_ct_tuplehash_to_ctrack(h);
87711cb8
PNA
716 /* Dump entries of a given L3 protocol number.
717 * If it is not specified, ie. l3proto == 0,
718 * then dump everything. */
5e8fbe2a 719 if (l3proto && nf_ct_l3num(ct) != l3proto)
13ee6ac5 720 continue;
d205dc40
PM
721 if (cb->args[1]) {
722 if (ct != last)
13ee6ac5 723 continue;
d205dc40 724 cb->args[1] = 0;
89f2e218 725 }
c1d10adb 726 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
601e68e1 727 cb->nlh->nlmsg_seq,
80e60e67
PNA
728 NFNL_MSG_TYPE(
729 cb->nlh->nlmsg_type),
730 ct) < 0) {
c71caf41 731 nf_conntrack_get(&ct->ct_general);
89f2e218 732 cb->args[1] = (unsigned long)ct;
c1d10adb 733 goto out;
89f2e218
PM
734 }
735 }
d205dc40 736 if (cb->args[1]) {
89f2e218
PM
737 cb->args[1] = 0;
738 goto restart;
c1d10adb
PNA
739 }
740 }
89f2e218 741out:
13ee6ac5 742 spin_unlock_bh(&nf_conntrack_lock);
d205dc40
PM
743 if (last)
744 nf_ct_put(last);
c1d10adb 745
c1d10adb
PNA
746 return skb->len;
747}
748
c1d10adb 749static inline int
df6fb868 750ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
c1d10adb 751{
df6fb868 752 struct nlattr *tb[CTA_IP_MAX+1];
c1d10adb
PNA
753 struct nf_conntrack_l3proto *l3proto;
754 int ret = 0;
755
df6fb868 756 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
c1d10adb 757
cd91566e
FW
758 rcu_read_lock();
759 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
c1d10adb 760
f73e924c
PM
761 if (likely(l3proto->nlattr_to_tuple)) {
762 ret = nla_validate_nested(attr, CTA_IP_MAX,
763 l3proto->nla_policy);
764 if (ret == 0)
765 ret = l3proto->nlattr_to_tuple(tb, tuple);
766 }
c1d10adb 767
cd91566e 768 rcu_read_unlock();
c1d10adb 769
c1d10adb
PNA
770 return ret;
771}
772
f73e924c
PM
773static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
774 [CTA_PROTO_NUM] = { .type = NLA_U8 },
c1d10adb
PNA
775};
776
777static inline int
df6fb868 778ctnetlink_parse_tuple_proto(struct nlattr *attr,
c1d10adb
PNA
779 struct nf_conntrack_tuple *tuple)
780{
df6fb868 781 struct nlattr *tb[CTA_PROTO_MAX+1];
605dcad6 782 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
783 int ret = 0;
784
f73e924c
PM
785 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
786 if (ret < 0)
787 return ret;
c1d10adb 788
df6fb868 789 if (!tb[CTA_PROTO_NUM])
c1d10adb 790 return -EINVAL;
77236b6e 791 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
c1d10adb 792
cd91566e
FW
793 rcu_read_lock();
794 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
c1d10adb 795
f73e924c
PM
796 if (likely(l4proto->nlattr_to_tuple)) {
797 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
798 l4proto->nla_policy);
799 if (ret == 0)
800 ret = l4proto->nlattr_to_tuple(tb, tuple);
801 }
c1d10adb 802
cd91566e 803 rcu_read_unlock();
601e68e1 804
c1d10adb
PNA
805 return ret;
806}
807
d0b0268f
PM
808static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
809 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
810 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
811};
812
bb5cf80e 813static int
39938324
PM
814ctnetlink_parse_tuple(const struct nlattr * const cda[],
815 struct nf_conntrack_tuple *tuple,
a00f1f36 816 enum ctattr_type type, u_int8_t l3num)
c1d10adb 817{
df6fb868 818 struct nlattr *tb[CTA_TUPLE_MAX+1];
c1d10adb
PNA
819 int err;
820
c1d10adb
PNA
821 memset(tuple, 0, sizeof(*tuple));
822
d0b0268f 823 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
c1d10adb 824
df6fb868 825 if (!tb[CTA_TUPLE_IP])
c1d10adb
PNA
826 return -EINVAL;
827
828 tuple->src.l3num = l3num;
829
df6fb868 830 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
c1d10adb
PNA
831 if (err < 0)
832 return err;
833
df6fb868 834 if (!tb[CTA_TUPLE_PROTO])
c1d10adb
PNA
835 return -EINVAL;
836
df6fb868 837 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
c1d10adb
PNA
838 if (err < 0)
839 return err;
840
841 /* orig and expect tuples get DIR_ORIGINAL */
842 if (type == CTA_TUPLE_REPLY)
843 tuple->dst.dir = IP_CT_DIR_REPLY;
844 else
845 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
846
c1d10adb
PNA
847 return 0;
848}
849
ef00f89f
PM
850static int
851ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
852{
853 if (attr)
854#ifdef CONFIG_NF_CONNTRACK_ZONES
855 *zone = ntohs(nla_get_be16(attr));
856#else
857 return -EOPNOTSUPP;
858#endif
859 else
860 *zone = 0;
861
862 return 0;
863}
864
d0b0268f
PM
865static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
866 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING },
867};
868
c1d10adb 869static inline int
39938324 870ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
c1d10adb 871{
df6fb868 872 struct nlattr *tb[CTA_HELP_MAX+1];
c1d10adb 873
d0b0268f 874 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
c1d10adb 875
df6fb868 876 if (!tb[CTA_HELP_NAME])
c1d10adb
PNA
877 return -EINVAL;
878
df6fb868 879 *helper_name = nla_data(tb[CTA_HELP_NAME]);
c1d10adb
PNA
880
881 return 0;
882}
883
f73e924c 884static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
d0b0268f
PM
885 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
886 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
f73e924c 887 [CTA_STATUS] = { .type = NLA_U32 },
d0b0268f
PM
888 [CTA_PROTOINFO] = { .type = NLA_NESTED },
889 [CTA_HELP] = { .type = NLA_NESTED },
890 [CTA_NAT_SRC] = { .type = NLA_NESTED },
f73e924c
PM
891 [CTA_TIMEOUT] = { .type = NLA_U32 },
892 [CTA_MARK] = { .type = NLA_U32 },
f73e924c 893 [CTA_ID] = { .type = NLA_U32 },
d0b0268f
PM
894 [CTA_NAT_DST] = { .type = NLA_NESTED },
895 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
ef00f89f 896 [CTA_ZONE] = { .type = NLA_U16 },
c1d10adb
PNA
897};
898
899static int
601e68e1 900ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
901 const struct nlmsghdr *nlh,
902 const struct nlattr * const cda[])
c1d10adb 903{
9592a5c0 904 struct net *net = sock_net(ctnl);
c1d10adb
PNA
905 struct nf_conntrack_tuple_hash *h;
906 struct nf_conntrack_tuple tuple;
907 struct nf_conn *ct;
96bcf938 908 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 909 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
910 u16 zone;
911 int err;
912
913 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
914 if (err < 0)
915 return err;
c1d10adb 916
df6fb868 917 if (cda[CTA_TUPLE_ORIG])
c1d10adb 918 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
df6fb868 919 else if (cda[CTA_TUPLE_REPLY])
c1d10adb
PNA
920 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
921 else {
922 /* Flush the whole table */
9592a5c0 923 nf_conntrack_flush_report(net,
274d383b
PNA
924 NETLINK_CB(skb).pid,
925 nlmsg_report(nlh));
c1d10adb
PNA
926 return 0;
927 }
928
929 if (err < 0)
930 return err;
931
ef00f89f 932 h = nf_conntrack_find_get(net, zone, &tuple);
9ea8cfd6 933 if (!h)
c1d10adb 934 return -ENOENT;
c1d10adb
PNA
935
936 ct = nf_ct_tuplehash_to_ctrack(h);
601e68e1 937
df6fb868 938 if (cda[CTA_ID]) {
77236b6e 939 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
7f85f914 940 if (id != (u32)(unsigned long)ct) {
c1d10adb
PNA
941 nf_ct_put(ct);
942 return -ENOENT;
943 }
601e68e1 944 }
c1d10adb 945
dd7669a9
PNA
946 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
947 NETLINK_CB(skb).pid,
948 nlmsg_report(nlh)) < 0) {
949 nf_ct_delete_from_lists(ct);
950 /* we failed to report the event, try later */
951 nf_ct_insert_dying_list(ct);
952 nf_ct_put(ct);
953 return 0;
954 }
19abb7b0
PNA
955
956 /* death_by_timeout would report the event again */
957 set_bit(IPS_DYING_BIT, &ct->status);
958
51091764 959 nf_ct_kill(ct);
c1d10adb 960 nf_ct_put(ct);
c1d10adb
PNA
961
962 return 0;
963}
964
965static int
601e68e1 966ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
967 const struct nlmsghdr *nlh,
968 const struct nlattr * const cda[])
c1d10adb 969{
9592a5c0 970 struct net *net = sock_net(ctnl);
c1d10adb
PNA
971 struct nf_conntrack_tuple_hash *h;
972 struct nf_conntrack_tuple tuple;
973 struct nf_conn *ct;
974 struct sk_buff *skb2 = NULL;
96bcf938 975 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 976 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
977 u16 zone;
978 int err;
c1d10adb 979
b8f3ab42 980 if (nlh->nlmsg_flags & NLM_F_DUMP)
c702e804 981 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
c7ac8679 982 ctnetlink_done, 0);
c1d10adb 983
ef00f89f
PM
984 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
985 if (err < 0)
986 return err;
987
df6fb868 988 if (cda[CTA_TUPLE_ORIG])
c1d10adb 989 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
df6fb868 990 else if (cda[CTA_TUPLE_REPLY])
c1d10adb
PNA
991 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
992 else
993 return -EINVAL;
994
995 if (err < 0)
996 return err;
997
ef00f89f 998 h = nf_conntrack_find_get(net, zone, &tuple);
9ea8cfd6 999 if (!h)
c1d10adb 1000 return -ENOENT;
9ea8cfd6 1001
c1d10adb
PNA
1002 ct = nf_ct_tuplehash_to_ctrack(h);
1003
1004 err = -ENOMEM;
96bcf938
PNA
1005 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1006 if (skb2 == NULL) {
c1d10adb
PNA
1007 nf_ct_put(ct);
1008 return -ENOMEM;
1009 }
c1d10adb 1010
528a3a6f 1011 rcu_read_lock();
601e68e1 1012 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
80e60e67 1013 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
528a3a6f 1014 rcu_read_unlock();
c1d10adb
PNA
1015 nf_ct_put(ct);
1016 if (err <= 0)
1017 goto free;
1018
1019 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1020 if (err < 0)
1021 goto out;
1022
c1d10adb
PNA
1023 return 0;
1024
1025free:
1026 kfree_skb(skb2);
1027out:
f31e8d49
PNA
1028 /* this avoids a loop in nfnetlink. */
1029 return err == -EAGAIN ? -ENOBUFS : err;
c1d10adb
PNA
1030}
1031
67671841 1032#ifdef CONFIG_NF_NAT_NEEDED
e6a7d3c0
PNA
1033static int
1034ctnetlink_parse_nat_setup(struct nf_conn *ct,
1035 enum nf_nat_manip_type manip,
39938324 1036 const struct nlattr *attr)
e6a7d3c0
PNA
1037{
1038 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1039
1040 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1041 if (!parse_nat_setup) {
95a5afca 1042#ifdef CONFIG_MODULES
e6a7d3c0 1043 rcu_read_unlock();
748085fc 1044 spin_unlock_bh(&nf_conntrack_lock);
e6a7d3c0
PNA
1045 nfnl_unlock();
1046 if (request_module("nf-nat-ipv4") < 0) {
1047 nfnl_lock();
748085fc 1048 spin_lock_bh(&nf_conntrack_lock);
e6a7d3c0
PNA
1049 rcu_read_lock();
1050 return -EOPNOTSUPP;
1051 }
1052 nfnl_lock();
748085fc 1053 spin_lock_bh(&nf_conntrack_lock);
e6a7d3c0
PNA
1054 rcu_read_lock();
1055 if (nfnetlink_parse_nat_setup_hook)
1056 return -EAGAIN;
1057#endif
1058 return -EOPNOTSUPP;
1059 }
1060
1061 return parse_nat_setup(ct, manip, attr);
1062}
67671841 1063#endif
e6a7d3c0 1064
bb5cf80e 1065static int
39938324 1066ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
1067{
1068 unsigned long d;
77236b6e 1069 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
c1d10adb
PNA
1070 d = ct->status ^ status;
1071
1072 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1073 /* unchangeable */
0adf9d67 1074 return -EBUSY;
601e68e1 1075
c1d10adb
PNA
1076 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1077 /* SEEN_REPLY bit can only be set */
0adf9d67 1078 return -EBUSY;
601e68e1 1079
c1d10adb
PNA
1080 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1081 /* ASSURED bit can only be set */
0adf9d67 1082 return -EBUSY;
c1d10adb 1083
c1d10adb
PNA
1084 /* Be careful here, modifying NAT bits can screw up things,
1085 * so don't let users modify them directly if they don't pass
5b1158e9 1086 * nf_nat_range. */
c1d10adb
PNA
1087 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1088 return 0;
1089}
1090
e6a7d3c0 1091static int
39938324 1092ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
e6a7d3c0
PNA
1093{
1094#ifdef CONFIG_NF_NAT_NEEDED
1095 int ret;
1096
1097 if (cda[CTA_NAT_DST]) {
1098 ret = ctnetlink_parse_nat_setup(ct,
cbc9f2f4 1099 NF_NAT_MANIP_DST,
e6a7d3c0
PNA
1100 cda[CTA_NAT_DST]);
1101 if (ret < 0)
1102 return ret;
1103 }
1104 if (cda[CTA_NAT_SRC]) {
1105 ret = ctnetlink_parse_nat_setup(ct,
cbc9f2f4 1106 NF_NAT_MANIP_SRC,
e6a7d3c0
PNA
1107 cda[CTA_NAT_SRC]);
1108 if (ret < 0)
1109 return ret;
1110 }
1111 return 0;
1112#else
1113 return -EOPNOTSUPP;
1114#endif
1115}
c1d10adb
PNA
1116
1117static inline int
39938324 1118ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb
PNA
1119{
1120 struct nf_conntrack_helper *helper;
dc808fe2 1121 struct nf_conn_help *help = nfct_help(ct);
29fe1b48 1122 char *helpname = NULL;
c1d10adb
PNA
1123 int err;
1124
c1d10adb
PNA
1125 /* don't change helper of sibling connections */
1126 if (ct->master)
0adf9d67 1127 return -EBUSY;
c1d10adb 1128
df6fb868 1129 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
c1d10adb
PNA
1130 if (err < 0)
1131 return err;
1132
df293bbb
YK
1133 if (!strcmp(helpname, "")) {
1134 if (help && help->helper) {
c1d10adb
PNA
1135 /* we had a helper before ... */
1136 nf_ct_remove_expectations(ct);
a9b3cd7f 1137 RCU_INIT_POINTER(help->helper, NULL);
c1d10adb 1138 }
df293bbb
YK
1139
1140 return 0;
c1d10adb 1141 }
601e68e1 1142
794e6871
PM
1143 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1144 nf_ct_protonum(ct));
226c0c0e
PNA
1145 if (helper == NULL) {
1146#ifdef CONFIG_MODULES
1147 spin_unlock_bh(&nf_conntrack_lock);
1148
1149 if (request_module("nfct-helper-%s", helpname) < 0) {
1150 spin_lock_bh(&nf_conntrack_lock);
1151 return -EOPNOTSUPP;
1152 }
1153
1154 spin_lock_bh(&nf_conntrack_lock);
794e6871
PM
1155 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1156 nf_ct_protonum(ct));
226c0c0e
PNA
1157 if (helper)
1158 return -EAGAIN;
1159#endif
0adf9d67 1160 return -EOPNOTSUPP;
226c0c0e 1161 }
df293bbb 1162
ceceae1b
YK
1163 if (help) {
1164 if (help->helper == helper)
1165 return 0;
1166 if (help->helper)
1167 return -EBUSY;
1168 /* need to zero data of old helper */
1169 memset(&help->help, 0, sizeof(help->help));
1170 } else {
a88e22ad
PNA
1171 /* we cannot set a helper for an existing conntrack */
1172 return -EOPNOTSUPP;
ceceae1b 1173 }
df293bbb 1174
a9b3cd7f 1175 RCU_INIT_POINTER(help->helper, helper);
c1d10adb
PNA
1176
1177 return 0;
1178}
1179
1180static inline int
39938324 1181ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1182{
77236b6e 1183 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
601e68e1 1184
c1d10adb
PNA
1185 if (!del_timer(&ct->timeout))
1186 return -ETIME;
1187
1188 ct->timeout.expires = jiffies + timeout * HZ;
1189 add_timer(&ct->timeout);
1190
1191 return 0;
1192}
1193
d0b0268f
PM
1194static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1195 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1196 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1197 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1198};
1199
c1d10adb 1200static inline int
39938324 1201ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
c1d10adb 1202{
39938324
PM
1203 const struct nlattr *attr = cda[CTA_PROTOINFO];
1204 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
605dcad6 1205 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
1206 int err = 0;
1207
d0b0268f 1208 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
c1d10adb 1209
cd91566e
FW
1210 rcu_read_lock();
1211 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
fdf70832
PM
1212 if (l4proto->from_nlattr)
1213 err = l4proto->from_nlattr(tb, ct);
cd91566e 1214 rcu_read_unlock();
c1d10adb
PNA
1215
1216 return err;
1217}
1218
13eae15a 1219#ifdef CONFIG_NF_NAT_NEEDED
d0b0268f
PM
1220static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1221 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1222 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1223 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1224};
1225
13eae15a 1226static inline int
39938324 1227change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
13eae15a
PNA
1228{
1229 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1230
d0b0268f 1231 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
13eae15a
PNA
1232
1233 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1234 return -EINVAL;
1235
1236 natseq->correction_pos =
77236b6e 1237 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
13eae15a
PNA
1238
1239 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1240 return -EINVAL;
1241
1242 natseq->offset_before =
77236b6e 1243 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
13eae15a
PNA
1244
1245 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1246 return -EINVAL;
1247
1248 natseq->offset_after =
77236b6e 1249 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
13eae15a
PNA
1250
1251 return 0;
1252}
1253
1254static int
39938324
PM
1255ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1256 const struct nlattr * const cda[])
13eae15a
PNA
1257{
1258 int ret = 0;
1259 struct nf_conn_nat *nat = nfct_nat(ct);
1260
1261 if (!nat)
1262 return 0;
1263
1264 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1265 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1266 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1267 if (ret < 0)
1268 return ret;
1269
1270 ct->status |= IPS_SEQ_ADJUST;
1271 }
1272
1273 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1274 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1275 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1276 if (ret < 0)
1277 return ret;
1278
1279 ct->status |= IPS_SEQ_ADJUST;
1280 }
1281
1282 return 0;
1283}
1284#endif
1285
c1d10adb 1286static int
39938324
PM
1287ctnetlink_change_conntrack(struct nf_conn *ct,
1288 const struct nlattr * const cda[])
c1d10adb
PNA
1289{
1290 int err;
1291
e098360f
PNA
1292 /* only allow NAT changes and master assignation for new conntracks */
1293 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1294 return -EOPNOTSUPP;
1295
df6fb868 1296 if (cda[CTA_HELP]) {
c1d10adb
PNA
1297 err = ctnetlink_change_helper(ct, cda);
1298 if (err < 0)
1299 return err;
1300 }
1301
df6fb868 1302 if (cda[CTA_TIMEOUT]) {
c1d10adb
PNA
1303 err = ctnetlink_change_timeout(ct, cda);
1304 if (err < 0)
1305 return err;
1306 }
1307
df6fb868 1308 if (cda[CTA_STATUS]) {
c1d10adb
PNA
1309 err = ctnetlink_change_status(ct, cda);
1310 if (err < 0)
1311 return err;
1312 }
1313
df6fb868 1314 if (cda[CTA_PROTOINFO]) {
c1d10adb
PNA
1315 err = ctnetlink_change_protoinfo(ct, cda);
1316 if (err < 0)
1317 return err;
1318 }
1319
bcd1e830 1320#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1321 if (cda[CTA_MARK])
77236b6e 1322 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1323#endif
1324
13eae15a
PNA
1325#ifdef CONFIG_NF_NAT_NEEDED
1326 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1327 err = ctnetlink_change_nat_seq_adj(ct, cda);
1328 if (err < 0)
1329 return err;
1330 }
1331#endif
1332
c1d10adb
PNA
1333 return 0;
1334}
1335
f0a3c086 1336static struct nf_conn *
ef00f89f 1337ctnetlink_create_conntrack(struct net *net, u16 zone,
9592a5c0 1338 const struct nlattr * const cda[],
c1d10adb 1339 struct nf_conntrack_tuple *otuple,
5faa1f4c 1340 struct nf_conntrack_tuple *rtuple,
7ec47496 1341 u8 u3)
c1d10adb
PNA
1342{
1343 struct nf_conn *ct;
1344 int err = -EINVAL;
ceceae1b 1345 struct nf_conntrack_helper *helper;
315c34da 1346 struct nf_conn_tstamp *tstamp;
c1d10adb 1347
ef00f89f 1348 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
cd7fcbf1 1349 if (IS_ERR(ct))
f0a3c086 1350 return ERR_PTR(-ENOMEM);
c1d10adb 1351
df6fb868 1352 if (!cda[CTA_TIMEOUT])
0f5b3e85 1353 goto err1;
77236b6e 1354 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
c1d10adb
PNA
1355
1356 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
c1d10adb 1357
1575e7ea 1358 rcu_read_lock();
226c0c0e 1359 if (cda[CTA_HELP]) {
29fe1b48 1360 char *helpname = NULL;
226c0c0e
PNA
1361
1362 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
0f5b3e85
PM
1363 if (err < 0)
1364 goto err2;
226c0c0e 1365
794e6871
PM
1366 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1367 nf_ct_protonum(ct));
226c0c0e
PNA
1368 if (helper == NULL) {
1369 rcu_read_unlock();
1370#ifdef CONFIG_MODULES
1371 if (request_module("nfct-helper-%s", helpname) < 0) {
1372 err = -EOPNOTSUPP;
0f5b3e85 1373 goto err1;
226c0c0e
PNA
1374 }
1375
1376 rcu_read_lock();
794e6871
PM
1377 helper = __nf_conntrack_helper_find(helpname,
1378 nf_ct_l3num(ct),
1379 nf_ct_protonum(ct));
226c0c0e 1380 if (helper) {
226c0c0e 1381 err = -EAGAIN;
0f5b3e85 1382 goto err2;
226c0c0e
PNA
1383 }
1384 rcu_read_unlock();
1385#endif
1386 err = -EOPNOTSUPP;
0f5b3e85 1387 goto err1;
226c0c0e
PNA
1388 } else {
1389 struct nf_conn_help *help;
1390
1391 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1392 if (help == NULL) {
226c0c0e 1393 err = -ENOMEM;
0f5b3e85 1394 goto err2;
226c0c0e
PNA
1395 }
1396
1397 /* not in hash table yet so not strictly necessary */
a9b3cd7f 1398 RCU_INIT_POINTER(help->helper, helper);
226c0c0e
PNA
1399 }
1400 } else {
1401 /* try an implicit helper assignation */
b2a15a60 1402 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
0f5b3e85
PM
1403 if (err < 0)
1404 goto err2;
1575e7ea
PNA
1405 }
1406
a88e22ad
PNA
1407 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1408 err = ctnetlink_change_nat(ct, cda);
0f5b3e85
PM
1409 if (err < 0)
1410 goto err2;
e6a7d3c0
PNA
1411 }
1412
a88e22ad 1413 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
a992ca2a 1414 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
a88e22ad
PNA
1415 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1416 /* we must add conntrack extensions before confirmation. */
1417 ct->status |= IPS_CONFIRMED;
1418
1419 if (cda[CTA_STATUS]) {
1420 err = ctnetlink_change_status(ct, cda);
0f5b3e85
PM
1421 if (err < 0)
1422 goto err2;
bbb3357d 1423 }
c1d10adb 1424
c969aa7d
PNA
1425#ifdef CONFIG_NF_NAT_NEEDED
1426 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1427 err = ctnetlink_change_nat_seq_adj(ct, cda);
0f5b3e85
PM
1428 if (err < 0)
1429 goto err2;
c969aa7d
PNA
1430 }
1431#endif
1432
e5fc9e7a 1433 memset(&ct->proto, 0, sizeof(ct->proto));
df6fb868 1434 if (cda[CTA_PROTOINFO]) {
c1d10adb 1435 err = ctnetlink_change_protoinfo(ct, cda);
0f5b3e85
PM
1436 if (err < 0)
1437 goto err2;
c1d10adb
PNA
1438 }
1439
bcd1e830 1440#if defined(CONFIG_NF_CONNTRACK_MARK)
df6fb868 1441 if (cda[CTA_MARK])
77236b6e 1442 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
c1d10adb
PNA
1443#endif
1444
5faa1f4c 1445 /* setup master conntrack: this is a confirmed expectation */
7ec47496
PNA
1446 if (cda[CTA_TUPLE_MASTER]) {
1447 struct nf_conntrack_tuple master;
1448 struct nf_conntrack_tuple_hash *master_h;
1449 struct nf_conn *master_ct;
1450
1451 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1452 if (err < 0)
0f5b3e85 1453 goto err2;
7ec47496 1454
ef00f89f 1455 master_h = nf_conntrack_find_get(net, zone, &master);
7ec47496
PNA
1456 if (master_h == NULL) {
1457 err = -ENOENT;
0f5b3e85 1458 goto err2;
7ec47496
PNA
1459 }
1460 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
f2a89004 1461 __set_bit(IPS_EXPECTED_BIT, &ct->status);
5faa1f4c 1462 ct->master = master_ct;
f2a89004 1463 }
315c34da
PNA
1464 tstamp = nf_conn_tstamp_find(ct);
1465 if (tstamp)
1466 tstamp->start = ktime_to_ns(ktime_get_real());
5faa1f4c 1467
c1d10adb
PNA
1468 add_timer(&ct->timeout);
1469 nf_conntrack_hash_insert(ct);
58a3c9bb 1470 rcu_read_unlock();
dafc741c 1471
f0a3c086 1472 return ct;
c1d10adb 1473
0f5b3e85
PM
1474err2:
1475 rcu_read_unlock();
1476err1:
c1d10adb 1477 nf_conntrack_free(ct);
f0a3c086 1478 return ERR_PTR(err);
c1d10adb
PNA
1479}
1480
601e68e1
YH
1481static int
1482ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1483 const struct nlmsghdr *nlh,
1484 const struct nlattr * const cda[])
c1d10adb 1485{
9592a5c0 1486 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1487 struct nf_conntrack_tuple otuple, rtuple;
1488 struct nf_conntrack_tuple_hash *h = NULL;
96bcf938 1489 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 1490 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1491 u16 zone;
1492 int err;
1493
1494 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1495 if (err < 0)
1496 return err;
c1d10adb 1497
df6fb868 1498 if (cda[CTA_TUPLE_ORIG]) {
c1d10adb
PNA
1499 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1500 if (err < 0)
1501 return err;
1502 }
1503
df6fb868 1504 if (cda[CTA_TUPLE_REPLY]) {
c1d10adb
PNA
1505 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1506 if (err < 0)
1507 return err;
1508 }
1509
f8ba1aff 1510 spin_lock_bh(&nf_conntrack_lock);
df6fb868 1511 if (cda[CTA_TUPLE_ORIG])
ef00f89f 1512 h = __nf_conntrack_find(net, zone, &otuple);
df6fb868 1513 else if (cda[CTA_TUPLE_REPLY])
ef00f89f 1514 h = __nf_conntrack_find(net, zone, &rtuple);
c1d10adb
PNA
1515
1516 if (h == NULL) {
c1d10adb 1517 err = -ENOENT;
f0a3c086
PNA
1518 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1519 struct nf_conn *ct;
fecc1133 1520 enum ip_conntrack_events events;
5faa1f4c 1521
ef00f89f 1522 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
f0a3c086
PNA
1523 &rtuple, u3);
1524 if (IS_ERR(ct)) {
1525 err = PTR_ERR(ct);
5faa1f4c
PNA
1526 goto out_unlock;
1527 }
f0a3c086
PNA
1528 err = 0;
1529 nf_conntrack_get(&ct->ct_general);
1530 spin_unlock_bh(&nf_conntrack_lock);
fecc1133
PNA
1531 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1532 events = IPCT_RELATED;
1533 else
1534 events = IPCT_NEW;
1535
858b3133
PM
1536 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1537 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1538 (1 << IPCT_HELPER) |
1539 (1 << IPCT_PROTOINFO) |
1540 (1 << IPCT_NATSEQADJ) |
1541 (1 << IPCT_MARK) | events,
1542 ct, NETLINK_CB(skb).pid,
1543 nlmsg_report(nlh));
f0a3c086
PNA
1544 nf_ct_put(ct);
1545 } else
1546 spin_unlock_bh(&nf_conntrack_lock);
5faa1f4c 1547
c1d10adb
PNA
1548 return err;
1549 }
1550 /* implicit 'else' */
1551
c1d10adb
PNA
1552 /* We manipulate the conntrack inside the global conntrack table lock,
1553 * so there's no need to increase the refcount */
c1d10adb 1554 err = -EEXIST;
ff4ca827 1555 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
19abb7b0
PNA
1556 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1557
19abb7b0
PNA
1558 err = ctnetlink_change_conntrack(ct, cda);
1559 if (err == 0) {
1560 nf_conntrack_get(&ct->ct_general);
1561 spin_unlock_bh(&nf_conntrack_lock);
858b3133
PM
1562 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1563 (1 << IPCT_ASSURED) |
a0891aa6
PNA
1564 (1 << IPCT_HELPER) |
1565 (1 << IPCT_PROTOINFO) |
1566 (1 << IPCT_NATSEQADJ) |
1567 (1 << IPCT_MARK),
1568 ct, NETLINK_CB(skb).pid,
1569 nlmsg_report(nlh));
19abb7b0
PNA
1570 nf_ct_put(ct);
1571 } else
1572 spin_unlock_bh(&nf_conntrack_lock);
1573
1574 return err;
ff4ca827 1575 }
c1d10adb
PNA
1576
1577out_unlock:
f8ba1aff 1578 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1579 return err;
1580}
1581
601e68e1
YH
1582/***********************************************************************
1583 * EXPECT
1584 ***********************************************************************/
c1d10adb
PNA
1585
1586static inline int
1587ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1588 const struct nf_conntrack_tuple *tuple,
1589 enum ctattr_expect type)
1590{
df6fb868 1591 struct nlattr *nest_parms;
601e68e1 1592
df6fb868
PM
1593 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1594 if (!nest_parms)
1595 goto nla_put_failure;
c1d10adb 1596 if (ctnetlink_dump_tuples(skb, tuple) < 0)
df6fb868
PM
1597 goto nla_put_failure;
1598 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
1599
1600 return 0;
1601
df6fb868 1602nla_put_failure:
c1d10adb 1603 return -1;
601e68e1 1604}
c1d10adb 1605
1cde6436
PNA
1606static inline int
1607ctnetlink_exp_dump_mask(struct sk_buff *skb,
1608 const struct nf_conntrack_tuple *tuple,
d4156e8c 1609 const struct nf_conntrack_tuple_mask *mask)
1cde6436
PNA
1610{
1611 int ret;
1612 struct nf_conntrack_l3proto *l3proto;
605dcad6 1613 struct nf_conntrack_l4proto *l4proto;
d4156e8c 1614 struct nf_conntrack_tuple m;
df6fb868 1615 struct nlattr *nest_parms;
d4156e8c
PM
1616
1617 memset(&m, 0xFF, sizeof(m));
d4156e8c 1618 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
e578756c
PM
1619 m.src.u.all = mask->src.u.all;
1620 m.dst.protonum = tuple->dst.protonum;
d4156e8c 1621
df6fb868
PM
1622 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1623 if (!nest_parms)
1624 goto nla_put_failure;
1cde6436 1625
528a3a6f 1626 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
d4156e8c 1627 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1cde6436
PNA
1628
1629 if (unlikely(ret < 0))
df6fb868 1630 goto nla_put_failure;
1cde6436 1631
528a3a6f 1632 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
d4156e8c 1633 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1cde6436 1634 if (unlikely(ret < 0))
df6fb868 1635 goto nla_put_failure;
1cde6436 1636
df6fb868 1637 nla_nest_end(skb, nest_parms);
1cde6436
PNA
1638
1639 return 0;
1640
df6fb868 1641nla_put_failure:
1cde6436
PNA
1642 return -1;
1643}
1644
bb5cf80e 1645static int
c1d10adb 1646ctnetlink_exp_dump_expect(struct sk_buff *skb,
601e68e1 1647 const struct nf_conntrack_expect *exp)
c1d10adb
PNA
1648{
1649 struct nf_conn *master = exp->master;
d978e5da 1650 long timeout = (exp->timeout.expires - jiffies) / HZ;
bc01befd 1651 struct nf_conn_help *help;
d978e5da
PM
1652
1653 if (timeout < 0)
1654 timeout = 0;
c1d10adb
PNA
1655
1656 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
df6fb868 1657 goto nla_put_failure;
1cde6436 1658 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
df6fb868 1659 goto nla_put_failure;
c1d10adb
PNA
1660 if (ctnetlink_exp_dump_tuple(skb,
1661 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1662 CTA_EXPECT_MASTER) < 0)
df6fb868 1663 goto nla_put_failure;
601e68e1 1664
d978e5da 1665 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
77236b6e 1666 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
8b008faf 1667 NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
bc01befd
PNA
1668 help = nfct_help(master);
1669 if (help) {
1670 struct nf_conntrack_helper *helper;
1671
1672 helper = rcu_dereference(help->helper);
1673 if (helper)
1674 NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1675 }
c1d10adb
PNA
1676
1677 return 0;
601e68e1 1678
df6fb868 1679nla_put_failure:
c1d10adb
PNA
1680 return -1;
1681}
1682
1683static int
1684ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
8b0a231d 1685 int event, const struct nf_conntrack_expect *exp)
c1d10adb
PNA
1686{
1687 struct nlmsghdr *nlh;
1688 struct nfgenmsg *nfmsg;
96bcf938 1689 unsigned int flags = pid ? NLM_F_MULTI : 0;
c1d10adb
PNA
1690
1691 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
96bcf938
PNA
1692 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1693 if (nlh == NULL)
1694 goto nlmsg_failure;
c1d10adb 1695
96bcf938 1696 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
1697 nfmsg->nfgen_family = exp->tuple.src.l3num;
1698 nfmsg->version = NFNETLINK_V0;
1699 nfmsg->res_id = 0;
1700
1701 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 1702 goto nla_put_failure;
c1d10adb 1703
96bcf938 1704 nlmsg_end(skb, nlh);
c1d10adb
PNA
1705 return skb->len;
1706
1707nlmsg_failure:
df6fb868 1708nla_put_failure:
96bcf938 1709 nlmsg_cancel(skb, nlh);
c1d10adb
PNA
1710 return -1;
1711}
1712
1713#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
1714static int
1715ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
c1d10adb 1716{
9592a5c0
AD
1717 struct nf_conntrack_expect *exp = item->exp;
1718 struct net *net = nf_ct_exp_net(exp);
c1d10adb
PNA
1719 struct nlmsghdr *nlh;
1720 struct nfgenmsg *nfmsg;
c1d10adb 1721 struct sk_buff *skb;
ebbf41df 1722 unsigned int type, group;
c1d10adb
PNA
1723 int flags = 0;
1724
ebbf41df
PNA
1725 if (events & (1 << IPEXP_DESTROY)) {
1726 type = IPCTNL_MSG_EXP_DELETE;
1727 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
1728 } else if (events & (1 << IPEXP_NEW)) {
c1d10adb
PNA
1729 type = IPCTNL_MSG_EXP_NEW;
1730 flags = NLM_F_CREATE|NLM_F_EXCL;
ebbf41df 1731 group = NFNLGRP_CONNTRACK_EXP_NEW;
c1d10adb 1732 } else
e34d5c1a 1733 return 0;
c1d10adb 1734
ebbf41df 1735 if (!item->report && !nfnetlink_has_listeners(net, group))
e34d5c1a 1736 return 0;
b3a27bfb 1737
96bcf938
PNA
1738 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1739 if (skb == NULL)
150ace0d 1740 goto errout;
c1d10adb 1741
b633ad5f 1742 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
96bcf938
PNA
1743 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1744 if (nlh == NULL)
1745 goto nlmsg_failure;
c1d10adb 1746
96bcf938 1747 nfmsg = nlmsg_data(nlh);
c1d10adb
PNA
1748 nfmsg->nfgen_family = exp->tuple.src.l3num;
1749 nfmsg->version = NFNETLINK_V0;
1750 nfmsg->res_id = 0;
1751
528a3a6f 1752 rcu_read_lock();
c1d10adb 1753 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
df6fb868 1754 goto nla_put_failure;
528a3a6f 1755 rcu_read_unlock();
c1d10adb 1756
96bcf938 1757 nlmsg_end(skb, nlh);
ebbf41df 1758 nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC);
e34d5c1a 1759 return 0;
c1d10adb 1760
df6fb868 1761nla_put_failure:
528a3a6f 1762 rcu_read_unlock();
96bcf938 1763 nlmsg_cancel(skb, nlh);
528a3a6f 1764nlmsg_failure:
c1d10adb 1765 kfree_skb(skb);
150ace0d 1766errout:
9592a5c0 1767 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
e34d5c1a 1768 return 0;
c1d10adb
PNA
1769}
1770#endif
cf6994c2
PM
1771static int ctnetlink_exp_done(struct netlink_callback *cb)
1772{
31f15875
PM
1773 if (cb->args[1])
1774 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
cf6994c2
PM
1775 return 0;
1776}
c1d10adb
PNA
1777
1778static int
1779ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1780{
9592a5c0 1781 struct net *net = sock_net(skb->sk);
cf6994c2 1782 struct nf_conntrack_expect *exp, *last;
96bcf938 1783 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
31f15875 1784 struct hlist_node *n;
87711cb8 1785 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 1786
7d0742da 1787 rcu_read_lock();
31f15875
PM
1788 last = (struct nf_conntrack_expect *)cb->args[1];
1789 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
cf6994c2 1790restart:
9b03f38d 1791 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
31f15875
PM
1792 hnode) {
1793 if (l3proto && exp->tuple.src.l3num != l3proto)
cf6994c2 1794 continue;
31f15875
PM
1795 if (cb->args[1]) {
1796 if (exp != last)
1797 continue;
1798 cb->args[1] = 0;
1799 }
8b0a231d
PNA
1800 if (ctnetlink_exp_fill_info(skb,
1801 NETLINK_CB(cb->skb).pid,
31f15875
PM
1802 cb->nlh->nlmsg_seq,
1803 IPCTNL_MSG_EXP_NEW,
8b0a231d 1804 exp) < 0) {
7d0742da
PM
1805 if (!atomic_inc_not_zero(&exp->use))
1806 continue;
31f15875
PM
1807 cb->args[1] = (unsigned long)exp;
1808 goto out;
1809 }
cf6994c2 1810 }
31f15875
PM
1811 if (cb->args[1]) {
1812 cb->args[1] = 0;
1813 goto restart;
cf6994c2
PM
1814 }
1815 }
601e68e1 1816out:
7d0742da 1817 rcu_read_unlock();
cf6994c2
PM
1818 if (last)
1819 nf_ct_expect_put(last);
c1d10adb 1820
c1d10adb
PNA
1821 return skb->len;
1822}
1823
f73e924c 1824static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
d0b0268f
PM
1825 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
1826 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
1827 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
f73e924c
PM
1828 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1829 [CTA_EXPECT_ID] = { .type = NLA_U32 },
d0b0268f 1830 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING },
bcac0dfa 1831 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
8b008faf 1832 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
c1d10adb
PNA
1833};
1834
1835static int
601e68e1 1836ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1837 const struct nlmsghdr *nlh,
1838 const struct nlattr * const cda[])
c1d10adb 1839{
9592a5c0 1840 struct net *net = sock_net(ctnl);
c1d10adb
PNA
1841 struct nf_conntrack_tuple tuple;
1842 struct nf_conntrack_expect *exp;
1843 struct sk_buff *skb2;
96bcf938 1844 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 1845 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
1846 u16 zone;
1847 int err;
c1d10adb 1848
b8f3ab42 1849 if (nlh->nlmsg_flags & NLM_F_DUMP) {
c702e804
TG
1850 return netlink_dump_start(ctnl, skb, nlh,
1851 ctnetlink_exp_dump_table,
c7ac8679 1852 ctnetlink_exp_done, 0);
c1d10adb
PNA
1853 }
1854
ef00f89f
PM
1855 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1856 if (err < 0)
1857 return err;
1858
35dba1d7
PNA
1859 if (cda[CTA_EXPECT_TUPLE])
1860 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1861 else if (cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
1862 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1863 else
1864 return -EINVAL;
1865
1866 if (err < 0)
1867 return err;
1868
ef00f89f 1869 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
1870 if (!exp)
1871 return -ENOENT;
1872
df6fb868 1873 if (cda[CTA_EXPECT_ID]) {
77236b6e 1874 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 1875 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 1876 nf_ct_expect_put(exp);
c1d10adb
PNA
1877 return -ENOENT;
1878 }
601e68e1 1879 }
c1d10adb
PNA
1880
1881 err = -ENOMEM;
96bcf938
PNA
1882 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1883 if (skb2 == NULL)
c1d10adb 1884 goto out;
4e9b8269 1885
528a3a6f 1886 rcu_read_lock();
601e68e1 1887 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
8b0a231d 1888 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
528a3a6f 1889 rcu_read_unlock();
c1d10adb
PNA
1890 if (err <= 0)
1891 goto free;
1892
6823645d 1893 nf_ct_expect_put(exp);
c1d10adb
PNA
1894
1895 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1896
1897free:
1898 kfree_skb(skb2);
1899out:
6823645d 1900 nf_ct_expect_put(exp);
c1d10adb
PNA
1901 return err;
1902}
1903
1904static int
601e68e1 1905ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
1906 const struct nlmsghdr *nlh,
1907 const struct nlattr * const cda[])
c1d10adb 1908{
9592a5c0 1909 struct net *net = sock_net(ctnl);
31f15875 1910 struct nf_conntrack_expect *exp;
c1d10adb 1911 struct nf_conntrack_tuple tuple;
96bcf938 1912 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
31f15875 1913 struct hlist_node *n, *next;
c1d10adb 1914 u_int8_t u3 = nfmsg->nfgen_family;
31f15875 1915 unsigned int i;
ef00f89f 1916 u16 zone;
c1d10adb
PNA
1917 int err;
1918
df6fb868 1919 if (cda[CTA_EXPECT_TUPLE]) {
c1d10adb 1920 /* delete a single expect by tuple */
ef00f89f
PM
1921 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1922 if (err < 0)
1923 return err;
1924
c1d10adb
PNA
1925 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1926 if (err < 0)
1927 return err;
1928
1929 /* bump usage count to 2 */
ef00f89f 1930 exp = nf_ct_expect_find_get(net, zone, &tuple);
c1d10adb
PNA
1931 if (!exp)
1932 return -ENOENT;
1933
df6fb868 1934 if (cda[CTA_EXPECT_ID]) {
77236b6e 1935 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
35832402 1936 if (ntohl(id) != (u32)(unsigned long)exp) {
6823645d 1937 nf_ct_expect_put(exp);
c1d10adb
PNA
1938 return -ENOENT;
1939 }
1940 }
1941
1942 /* after list removal, usage count == 1 */
ebbf41df
PNA
1943 spin_lock_bh(&nf_conntrack_lock);
1944 if (del_timer(&exp->timeout)) {
1945 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid,
1946 nlmsg_report(nlh));
1947 nf_ct_expect_put(exp);
1948 }
1949 spin_unlock_bh(&nf_conntrack_lock);
601e68e1 1950 /* have to put what we 'get' above.
c1d10adb 1951 * after this line usage count == 0 */
6823645d 1952 nf_ct_expect_put(exp);
df6fb868
PM
1953 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1954 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
31f15875 1955 struct nf_conn_help *m_help;
c1d10adb
PNA
1956
1957 /* delete all expectations for this helper */
f8ba1aff 1958 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
1959 for (i = 0; i < nf_ct_expect_hsize; i++) {
1960 hlist_for_each_entry_safe(exp, n, next,
9592a5c0 1961 &net->ct.expect_hash[i],
31f15875
PM
1962 hnode) {
1963 m_help = nfct_help(exp->master);
794e6871
PM
1964 if (!strcmp(m_help->helper->name, name) &&
1965 del_timer(&exp->timeout)) {
ebbf41df
PNA
1966 nf_ct_unlink_expect_report(exp,
1967 NETLINK_CB(skb).pid,
1968 nlmsg_report(nlh));
31f15875
PM
1969 nf_ct_expect_put(exp);
1970 }
c1d10adb
PNA
1971 }
1972 }
f8ba1aff 1973 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1974 } else {
1975 /* This basically means we have to flush everything*/
f8ba1aff 1976 spin_lock_bh(&nf_conntrack_lock);
31f15875
PM
1977 for (i = 0; i < nf_ct_expect_hsize; i++) {
1978 hlist_for_each_entry_safe(exp, n, next,
9592a5c0 1979 &net->ct.expect_hash[i],
31f15875
PM
1980 hnode) {
1981 if (del_timer(&exp->timeout)) {
ebbf41df
PNA
1982 nf_ct_unlink_expect_report(exp,
1983 NETLINK_CB(skb).pid,
1984 nlmsg_report(nlh));
31f15875
PM
1985 nf_ct_expect_put(exp);
1986 }
c1d10adb
PNA
1987 }
1988 }
f8ba1aff 1989 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1990 }
1991
1992 return 0;
1993}
1994static int
39938324
PM
1995ctnetlink_change_expect(struct nf_conntrack_expect *x,
1996 const struct nlattr * const cda[])
c1d10adb
PNA
1997{
1998 return -EOPNOTSUPP;
1999}
2000
2001static int
ef00f89f
PM
2002ctnetlink_create_expect(struct net *net, u16 zone,
2003 const struct nlattr * const cda[],
9592a5c0 2004 u_int8_t u3,
39938324 2005 u32 pid, int report)
c1d10adb
PNA
2006{
2007 struct nf_conntrack_tuple tuple, mask, master_tuple;
2008 struct nf_conntrack_tuple_hash *h = NULL;
2009 struct nf_conntrack_expect *exp;
2010 struct nf_conn *ct;
dc808fe2 2011 struct nf_conn_help *help;
c1d10adb
PNA
2012 int err = 0;
2013
c1d10adb
PNA
2014 /* caller guarantees that those three CTA_EXPECT_* exist */
2015 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2016 if (err < 0)
2017 return err;
2018 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2019 if (err < 0)
2020 return err;
2021 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2022 if (err < 0)
2023 return err;
2024
2025 /* Look for master conntrack of this expectation */
ef00f89f 2026 h = nf_conntrack_find_get(net, zone, &master_tuple);
c1d10adb
PNA
2027 if (!h)
2028 return -ENOENT;
2029 ct = nf_ct_tuplehash_to_ctrack(h);
6823645d 2030 exp = nf_ct_expect_alloc(ct);
c1d10adb
PNA
2031 if (!exp) {
2032 err = -ENOMEM;
2033 goto out;
2034 }
bc01befd
PNA
2035 help = nfct_help(ct);
2036 if (!help) {
3d058d7b
PNA
2037 err = -EOPNOTSUPP;
2038 goto out;
2039 }
2040 if (test_bit(IPS_USERSPACE_HELPER_BIT, &ct->status)) {
bc01befd
PNA
2041 if (!cda[CTA_EXPECT_TIMEOUT]) {
2042 err = -EINVAL;
2043 goto out;
2044 }
2045 exp->timeout.expires =
2046 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
601e68e1 2047
bc01befd
PNA
2048 exp->flags = NF_CT_EXPECT_USERSPACE;
2049 if (cda[CTA_EXPECT_FLAGS]) {
2050 exp->flags |=
2051 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2052 }
2053 } else {
2054 if (cda[CTA_EXPECT_FLAGS]) {
2055 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2056 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2057 } else
2058 exp->flags = 0;
2059 }
601e68e1 2060
626ba8fb 2061 exp->class = 0;
c1d10adb 2062 exp->expectfn = NULL;
c1d10adb 2063 exp->master = ct;
9457d851 2064 exp->helper = NULL;
c1d10adb 2065 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
d4156e8c
PM
2066 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2067 exp->mask.src.u.all = mask.src.u.all;
c1d10adb 2068
19abb7b0 2069 err = nf_ct_expect_related_report(exp, pid, report);
6823645d 2070 nf_ct_expect_put(exp);
c1d10adb 2071
601e68e1 2072out:
c1d10adb
PNA
2073 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2074 return err;
2075}
2076
2077static int
2078ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
2079 const struct nlmsghdr *nlh,
2080 const struct nlattr * const cda[])
c1d10adb 2081{
9592a5c0 2082 struct net *net = sock_net(ctnl);
c1d10adb
PNA
2083 struct nf_conntrack_tuple tuple;
2084 struct nf_conntrack_expect *exp;
96bcf938 2085 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c1d10adb 2086 u_int8_t u3 = nfmsg->nfgen_family;
ef00f89f
PM
2087 u16 zone;
2088 int err;
c1d10adb 2089
df6fb868
PM
2090 if (!cda[CTA_EXPECT_TUPLE]
2091 || !cda[CTA_EXPECT_MASK]
2092 || !cda[CTA_EXPECT_MASTER])
c1d10adb
PNA
2093 return -EINVAL;
2094
ef00f89f
PM
2095 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2096 if (err < 0)
2097 return err;
2098
c1d10adb
PNA
2099 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2100 if (err < 0)
2101 return err;
2102
f8ba1aff 2103 spin_lock_bh(&nf_conntrack_lock);
ef00f89f 2104 exp = __nf_ct_expect_find(net, zone, &tuple);
c1d10adb
PNA
2105
2106 if (!exp) {
f8ba1aff 2107 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 2108 err = -ENOENT;
19abb7b0 2109 if (nlh->nlmsg_flags & NLM_F_CREATE) {
ef00f89f 2110 err = ctnetlink_create_expect(net, zone, cda,
19abb7b0
PNA
2111 u3,
2112 NETLINK_CB(skb).pid,
2113 nlmsg_report(nlh));
2114 }
c1d10adb
PNA
2115 return err;
2116 }
2117
2118 err = -EEXIST;
2119 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2120 err = ctnetlink_change_expect(exp, cda);
f8ba1aff 2121 spin_unlock_bh(&nf_conntrack_lock);
c1d10adb 2122
c1d10adb
PNA
2123 return err;
2124}
2125
2126#ifdef CONFIG_NF_CONNTRACK_EVENTS
e34d5c1a
PNA
2127static struct nf_ct_event_notifier ctnl_notifier = {
2128 .fcn = ctnetlink_conntrack_event,
c1d10adb
PNA
2129};
2130
e34d5c1a
PNA
2131static struct nf_exp_event_notifier ctnl_notifier_exp = {
2132 .fcn = ctnetlink_expect_event,
c1d10adb
PNA
2133};
2134#endif
2135
7c8d4cb4 2136static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
c1d10adb 2137 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
f73e924c
PM
2138 .attr_count = CTA_MAX,
2139 .policy = ct_nla_policy },
c1d10adb 2140 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
2141 .attr_count = CTA_MAX,
2142 .policy = ct_nla_policy },
c1d10adb 2143 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
f73e924c
PM
2144 .attr_count = CTA_MAX,
2145 .policy = ct_nla_policy },
c1d10adb 2146 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
f73e924c
PM
2147 .attr_count = CTA_MAX,
2148 .policy = ct_nla_policy },
c1d10adb
PNA
2149};
2150
7c8d4cb4 2151static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
c1d10adb 2152 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
f73e924c
PM
2153 .attr_count = CTA_EXPECT_MAX,
2154 .policy = exp_nla_policy },
c1d10adb 2155 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
f73e924c
PM
2156 .attr_count = CTA_EXPECT_MAX,
2157 .policy = exp_nla_policy },
c1d10adb 2158 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
f73e924c
PM
2159 .attr_count = CTA_EXPECT_MAX,
2160 .policy = exp_nla_policy },
c1d10adb
PNA
2161};
2162
7c8d4cb4 2163static const struct nfnetlink_subsystem ctnl_subsys = {
c1d10adb
PNA
2164 .name = "conntrack",
2165 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2166 .cb_count = IPCTNL_MSG_MAX,
2167 .cb = ctnl_cb,
2168};
2169
7c8d4cb4 2170static const struct nfnetlink_subsystem ctnl_exp_subsys = {
c1d10adb
PNA
2171 .name = "conntrack_expect",
2172 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2173 .cb_count = IPCTNL_MSG_EXP_MAX,
2174 .cb = ctnl_exp_cb,
2175};
2176
d2483dde 2177MODULE_ALIAS("ip_conntrack_netlink");
c1d10adb 2178MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
34f9a2e4 2179MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
c1d10adb 2180
70e9942f
PNA
2181static int __net_init ctnetlink_net_init(struct net *net)
2182{
2183#ifdef CONFIG_NF_CONNTRACK_EVENTS
2184 int ret;
2185
2186 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2187 if (ret < 0) {
2188 pr_err("ctnetlink_init: cannot register notifier.\n");
2189 goto err_out;
2190 }
2191
2192 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2193 if (ret < 0) {
2194 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2195 goto err_unreg_notifier;
2196 }
2197#endif
2198 return 0;
2199
2200#ifdef CONFIG_NF_CONNTRACK_EVENTS
2201err_unreg_notifier:
2202 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2203err_out:
2204 return ret;
2205#endif
2206}
2207
2208static void ctnetlink_net_exit(struct net *net)
2209{
2210#ifdef CONFIG_NF_CONNTRACK_EVENTS
2211 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2212 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2213#endif
2214}
2215
2216static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2217{
2218 struct net *net;
2219
2220 list_for_each_entry(net, net_exit_list, exit_list)
2221 ctnetlink_net_exit(net);
2222}
2223
2224static struct pernet_operations ctnetlink_net_ops = {
2225 .init = ctnetlink_net_init,
2226 .exit_batch = ctnetlink_net_exit_batch,
2227};
2228
c1d10adb
PNA
2229static int __init ctnetlink_init(void)
2230{
2231 int ret;
2232
654d0fbd 2233 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
c1d10adb
PNA
2234 ret = nfnetlink_subsys_register(&ctnl_subsys);
2235 if (ret < 0) {
654d0fbd 2236 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
c1d10adb
PNA
2237 goto err_out;
2238 }
2239
2240 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2241 if (ret < 0) {
654d0fbd 2242 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
c1d10adb
PNA
2243 goto err_unreg_subsys;
2244 }
2245
70e9942f
PNA
2246 if (register_pernet_subsys(&ctnetlink_net_ops)) {
2247 pr_err("ctnetlink_init: cannot register pernet operations\n");
c1d10adb
PNA
2248 goto err_unreg_exp_subsys;
2249 }
2250
c1d10adb
PNA
2251 return 0;
2252
c1d10adb
PNA
2253err_unreg_exp_subsys:
2254 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
c1d10adb
PNA
2255err_unreg_subsys:
2256 nfnetlink_subsys_unregister(&ctnl_subsys);
2257err_out:
2258 return ret;
2259}
2260
2261static void __exit ctnetlink_exit(void)
2262{
654d0fbd 2263 pr_info("ctnetlink: unregistering from nfnetlink.\n");
c1d10adb 2264
70e9942f 2265 unregister_pernet_subsys(&ctnetlink_net_ops);
c1d10adb
PNA
2266 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2267 nfnetlink_subsys_unregister(&ctnl_subsys);
c1d10adb
PNA
2268}
2269
2270module_init(ctnetlink_init);
2271module_exit(ctnetlink_exit);
This page took 0.966359 seconds and 5 git commands to generate.