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