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