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