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