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