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