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