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