]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nf_conntrack_netlink.c
[NETFILTER]: nf_conntrack: introduce extension infrastructure
[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>
1cde6436 7 * (C) 2005-2006 by Pablo Neira Ayuso <pablo@eurodev.net>
c1d10adb 8 *
601e68e1 9 * Initial connection tracking via netlink development funded and
c1d10adb
PNA
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
11 *
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
13 *
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
c1d10adb
PNA
16 */
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/types.h>
22#include <linux/timer.h>
23#include <linux/skbuff.h>
24#include <linux/errno.h>
25#include <linux/netlink.h>
26#include <linux/spinlock.h>
40a839fd 27#include <linux/interrupt.h>
c1d10adb
PNA
28#include <linux/notifier.h>
29
30#include <linux/netfilter.h>
dc5fc579 31#include <net/netlink.h>
c1d10adb
PNA
32#include <net/netfilter/nf_conntrack.h>
33#include <net/netfilter/nf_conntrack_core.h>
77ab9cff 34#include <net/netfilter/nf_conntrack_expect.h>
c1d10adb
PNA
35#include <net/netfilter/nf_conntrack_helper.h>
36#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 37#include <net/netfilter/nf_conntrack_l4proto.h>
5b1158e9
JK
38#include <net/netfilter/nf_conntrack_tuple.h>
39#ifdef CONFIG_NF_NAT_NEEDED
40#include <net/netfilter/nf_nat_core.h>
41#include <net/netfilter/nf_nat_protocol.h>
42#endif
c1d10adb
PNA
43
44#include <linux/netfilter/nfnetlink.h>
45#include <linux/netfilter/nfnetlink_conntrack.h>
46
47MODULE_LICENSE("GPL");
48
dc808fe2 49static char __initdata version[] = "0.93";
c1d10adb 50
c1d10adb 51static inline int
601e68e1 52ctnetlink_dump_tuples_proto(struct sk_buff *skb,
1cde6436 53 const struct nf_conntrack_tuple *tuple,
605dcad6 54 struct nf_conntrack_l4proto *l4proto)
c1d10adb 55{
c1d10adb 56 int ret = 0;
1cde6436 57 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
c1d10adb
PNA
58
59 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
60
605dcad6
MJ
61 if (likely(l4proto->tuple_to_nfattr))
62 ret = l4proto->tuple_to_nfattr(skb, tuple);
601e68e1 63
1cde6436 64 NFA_NEST_END(skb, nest_parms);
c1d10adb
PNA
65
66 return ret;
67
68nfattr_failure:
69 return -1;
70}
71
72static inline int
1cde6436
PNA
73ctnetlink_dump_tuples_ip(struct sk_buff *skb,
74 const struct nf_conntrack_tuple *tuple,
75 struct nf_conntrack_l3proto *l3proto)
c1d10adb 76{
c1d10adb 77 int ret = 0;
1cde6436
PNA
78 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
79
c1d10adb
PNA
80 if (likely(l3proto->tuple_to_nfattr))
81 ret = l3proto->tuple_to_nfattr(skb, tuple);
1cde6436 82
c1d10adb
PNA
83 NFA_NEST_END(skb, nest_parms);
84
1cde6436
PNA
85 return ret;
86
87nfattr_failure:
88 return -1;
89}
90
91static inline int
92ctnetlink_dump_tuples(struct sk_buff *skb,
93 const struct nf_conntrack_tuple *tuple)
94{
95 int ret;
96 struct nf_conntrack_l3proto *l3proto;
605dcad6 97 struct nf_conntrack_l4proto *l4proto;
1cde6436
PNA
98
99 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
100 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
c1d10adb
PNA
101 nf_ct_l3proto_put(l3proto);
102
103 if (unlikely(ret < 0))
104 return ret;
105
605dcad6
MJ
106 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
107 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
108 nf_ct_l4proto_put(l4proto);
c1d10adb
PNA
109
110 return ret;
c1d10adb
PNA
111}
112
113static inline int
114ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
115{
bff9a89b 116 __be32 status = htonl((u_int32_t) ct->status);
c1d10adb
PNA
117 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
118 return 0;
119
120nfattr_failure:
121 return -1;
122}
123
124static inline int
125ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
126{
127 long timeout_l = ct->timeout.expires - jiffies;
bff9a89b 128 __be32 timeout;
c1d10adb
PNA
129
130 if (timeout_l < 0)
131 timeout = 0;
132 else
133 timeout = htonl(timeout_l / HZ);
601e68e1 134
c1d10adb
PNA
135 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
136 return 0;
137
138nfattr_failure:
139 return -1;
140}
141
142static inline int
143ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
144{
605dcad6 145 struct nf_conntrack_l4proto *l4proto = nf_ct_l4proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
c1d10adb
PNA
146 struct nfattr *nest_proto;
147 int ret;
148
605dcad6
MJ
149 if (!l4proto->to_nfattr) {
150 nf_ct_l4proto_put(l4proto);
c1d10adb
PNA
151 return 0;
152 }
601e68e1 153
c1d10adb
PNA
154 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
155
605dcad6 156 ret = l4proto->to_nfattr(skb, nest_proto, ct);
c1d10adb 157
605dcad6 158 nf_ct_l4proto_put(l4proto);
c1d10adb
PNA
159
160 NFA_NEST_END(skb, nest_proto);
161
162 return ret;
163
164nfattr_failure:
605dcad6 165 nf_ct_l4proto_put(l4proto);
c1d10adb
PNA
166 return -1;
167}
168
169static inline int
170ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
171{
172 struct nfattr *nest_helper;
dc808fe2 173 const struct nf_conn_help *help = nfct_help(ct);
3c158f7f 174 struct nf_conntrack_helper *helper;
c1d10adb 175
3c158f7f 176 if (!help)
c1d10adb 177 return 0;
601e68e1 178
3c158f7f
PM
179 rcu_read_lock();
180 helper = rcu_dereference(help->helper);
181 if (!helper)
182 goto out;
183
c1d10adb 184 nest_helper = NFA_NEST(skb, CTA_HELP);
3c158f7f 185 NFA_PUT(skb, CTA_HELP_NAME, strlen(helper->name), helper->name);
c1d10adb 186
3c158f7f
PM
187 if (helper->to_nfattr)
188 helper->to_nfattr(skb, ct);
c1d10adb
PNA
189
190 NFA_NEST_END(skb, nest_helper);
3c158f7f
PM
191out:
192 rcu_read_unlock();
c1d10adb
PNA
193 return 0;
194
195nfattr_failure:
3c158f7f 196 rcu_read_unlock();
c1d10adb
PNA
197 return -1;
198}
199
200#ifdef CONFIG_NF_CT_ACCT
201static inline int
202ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
203 enum ip_conntrack_dir dir)
204{
205 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
206 struct nfattr *nest_count = NFA_NEST(skb, type);
bff9a89b 207 __be32 tmp;
c1d10adb
PNA
208
209 tmp = htonl(ct->counters[dir].packets);
210 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
211
212 tmp = htonl(ct->counters[dir].bytes);
213 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
214
215 NFA_NEST_END(skb, nest_count);
216
217 return 0;
218
219nfattr_failure:
220 return -1;
221}
222#else
223#define ctnetlink_dump_counters(a, b, c) (0)
224#endif
225
226#ifdef CONFIG_NF_CONNTRACK_MARK
227static inline int
228ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
229{
bff9a89b 230 __be32 mark = htonl(ct->mark);
c1d10adb
PNA
231
232 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
233 return 0;
234
235nfattr_failure:
236 return -1;
237}
238#else
239#define ctnetlink_dump_mark(a, b) (0)
240#endif
241
242static inline int
243ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
244{
bff9a89b 245 __be32 id = htonl(ct->id);
c1d10adb
PNA
246 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
247 return 0;
248
249nfattr_failure:
250 return -1;
251}
252
253static inline int
254ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
255{
bff9a89b 256 __be32 use = htonl(atomic_read(&ct->ct_general.use));
601e68e1 257
c1d10adb
PNA
258 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
259 return 0;
260
261nfattr_failure:
262 return -1;
263}
264
265#define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
266
267static int
268ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
601e68e1 269 int event, int nowait,
c1d10adb
PNA
270 const struct nf_conn *ct)
271{
272 struct nlmsghdr *nlh;
273 struct nfgenmsg *nfmsg;
274 struct nfattr *nest_parms;
27a884dc 275 unsigned char *b = skb_tail_pointer(skb);
c1d10adb
PNA
276
277 event |= NFNL_SUBSYS_CTNETLINK << 8;
278 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
279 nfmsg = NLMSG_DATA(nlh);
280
281 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
601e68e1 282 nfmsg->nfgen_family =
c1d10adb
PNA
283 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
284 nfmsg->version = NFNETLINK_V0;
285 nfmsg->res_id = 0;
286
287 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
288 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
289 goto nfattr_failure;
290 NFA_NEST_END(skb, nest_parms);
601e68e1 291
c1d10adb
PNA
292 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
293 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
294 goto nfattr_failure;
295 NFA_NEST_END(skb, nest_parms);
296
297 if (ctnetlink_dump_status(skb, ct) < 0 ||
298 ctnetlink_dump_timeout(skb, ct) < 0 ||
299 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
300 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
301 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
302 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
303 ctnetlink_dump_mark(skb, ct) < 0 ||
304 ctnetlink_dump_id(skb, ct) < 0 ||
305 ctnetlink_dump_use(skb, ct) < 0)
306 goto nfattr_failure;
307
27a884dc 308 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
c1d10adb
PNA
309 return skb->len;
310
311nlmsg_failure:
312nfattr_failure:
dc5fc579 313 nlmsg_trim(skb, b);
c1d10adb
PNA
314 return -1;
315}
316
317#ifdef CONFIG_NF_CONNTRACK_EVENTS
318static int ctnetlink_conntrack_event(struct notifier_block *this,
601e68e1 319 unsigned long events, void *ptr)
c1d10adb
PNA
320{
321 struct nlmsghdr *nlh;
322 struct nfgenmsg *nfmsg;
323 struct nfattr *nest_parms;
324 struct nf_conn *ct = (struct nf_conn *)ptr;
325 struct sk_buff *skb;
326 unsigned int type;
27a884dc 327 sk_buff_data_t b;
c1d10adb
PNA
328 unsigned int flags = 0, group;
329
330 /* ignore our fake conntrack entry */
331 if (ct == &nf_conntrack_untracked)
332 return NOTIFY_DONE;
333
334 if (events & IPCT_DESTROY) {
335 type = IPCTNL_MSG_CT_DELETE;
336 group = NFNLGRP_CONNTRACK_DESTROY;
337 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
338 type = IPCTNL_MSG_CT_NEW;
339 flags = NLM_F_CREATE|NLM_F_EXCL;
c1d10adb 340 group = NFNLGRP_CONNTRACK_NEW;
1a31526b 341 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
c1d10adb
PNA
342 type = IPCTNL_MSG_CT_NEW;
343 group = NFNLGRP_CONNTRACK_UPDATE;
344 } else
345 return NOTIFY_DONE;
a2427692
PM
346
347 if (!nfnetlink_has_listeners(group))
348 return NOTIFY_DONE;
349
c1d10adb
PNA
350 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
351 if (!skb)
352 return NOTIFY_DONE;
353
354 b = skb->tail;
355
356 type |= NFNL_SUBSYS_CTNETLINK << 8;
357 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
358 nfmsg = NLMSG_DATA(nlh);
359
360 nlh->nlmsg_flags = flags;
361 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
362 nfmsg->version = NFNETLINK_V0;
363 nfmsg->res_id = 0;
364
365 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
366 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
367 goto nfattr_failure;
368 NFA_NEST_END(skb, nest_parms);
601e68e1 369
c1d10adb
PNA
370 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
371 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
372 goto nfattr_failure;
373 NFA_NEST_END(skb, nest_parms);
c1d10adb 374
7b621c1e
PNA
375 if (events & IPCT_DESTROY) {
376 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
377 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
378 goto nfattr_failure;
379 } else {
380 if (ctnetlink_dump_status(skb, ct) < 0)
381 goto nfattr_failure;
382
383 if (ctnetlink_dump_timeout(skb, ct) < 0)
384 goto nfattr_failure;
385
386 if (events & IPCT_PROTOINFO
387 && ctnetlink_dump_protoinfo(skb, ct) < 0)
601e68e1 388 goto nfattr_failure;
7b621c1e
PNA
389
390 if ((events & IPCT_HELPER || nfct_help(ct))
391 && ctnetlink_dump_helpinfo(skb, ct) < 0)
601e68e1 392 goto nfattr_failure;
7b621c1e 393
40e0cb00 394#ifdef CONFIG_NF_CONNTRACK_MARK
7b621c1e
PNA
395 if ((events & IPCT_MARK || ct->mark)
396 && ctnetlink_dump_mark(skb, ct) < 0)
601e68e1 397 goto nfattr_failure;
40e0cb00 398#endif
7b621c1e
PNA
399
400 if (events & IPCT_COUNTER_FILLING &&
401 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
402 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
403 goto nfattr_failure;
404 }
b9a37e0c 405
c1d10adb
PNA
406 nlh->nlmsg_len = skb->tail - b;
407 nfnetlink_send(skb, 0, group, 0);
408 return NOTIFY_DONE;
409
410nlmsg_failure:
411nfattr_failure:
412 kfree_skb(skb);
413 return NOTIFY_DONE;
414}
415#endif /* CONFIG_NF_CONNTRACK_EVENTS */
416
417static int ctnetlink_done(struct netlink_callback *cb)
418{
89f2e218
PM
419 if (cb->args[1])
420 nf_ct_put((struct nf_conn *)cb->args[1]);
c1d10adb
PNA
421 return 0;
422}
423
87711cb8
PNA
424#define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
425
c1d10adb
PNA
426static int
427ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
428{
89f2e218 429 struct nf_conn *ct, *last;
c1d10adb
PNA
430 struct nf_conntrack_tuple_hash *h;
431 struct list_head *i;
87711cb8
PNA
432 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
433 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 434
c1d10adb 435 read_lock_bh(&nf_conntrack_lock);
d205dc40 436 last = (struct nf_conn *)cb->args[1];
89f2e218
PM
437 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
438restart:
c1d10adb
PNA
439 list_for_each_prev(i, &nf_conntrack_hash[cb->args[0]]) {
440 h = (struct nf_conntrack_tuple_hash *) i;
5b1158e9 441 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
c1d10adb
PNA
442 continue;
443 ct = nf_ct_tuplehash_to_ctrack(h);
87711cb8
PNA
444 /* Dump entries of a given L3 protocol number.
445 * If it is not specified, ie. l3proto == 0,
446 * then dump everything. */
447 if (l3proto && L3PROTO(ct) != l3proto)
448 continue;
d205dc40
PM
449 if (cb->args[1]) {
450 if (ct != last)
89f2e218 451 continue;
d205dc40 452 cb->args[1] = 0;
89f2e218 453 }
c1d10adb 454 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
601e68e1 455 cb->nlh->nlmsg_seq,
c1d10adb 456 IPCTNL_MSG_CT_NEW,
89f2e218
PM
457 1, ct) < 0) {
458 nf_conntrack_get(&ct->ct_general);
459 cb->args[1] = (unsigned long)ct;
c1d10adb 460 goto out;
89f2e218 461 }
01f34848
PNA
462#ifdef CONFIG_NF_CT_ACCT
463 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
464 IPCTNL_MSG_CT_GET_CTRZERO)
465 memset(&ct->counters, 0, sizeof(ct->counters));
466#endif
89f2e218 467 }
d205dc40 468 if (cb->args[1]) {
89f2e218
PM
469 cb->args[1] = 0;
470 goto restart;
c1d10adb
PNA
471 }
472 }
89f2e218 473out:
c1d10adb 474 read_unlock_bh(&nf_conntrack_lock);
d205dc40
PM
475 if (last)
476 nf_ct_put(last);
c1d10adb 477
c1d10adb
PNA
478 return skb->len;
479}
480
c1d10adb
PNA
481static inline int
482ctnetlink_parse_tuple_ip(struct nfattr *attr, struct nf_conntrack_tuple *tuple)
483{
484 struct nfattr *tb[CTA_IP_MAX];
485 struct nf_conntrack_l3proto *l3proto;
486 int ret = 0;
487
c1d10adb
PNA
488 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
489
490 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
491
492 if (likely(l3proto->nfattr_to_tuple))
493 ret = l3proto->nfattr_to_tuple(tb, tuple);
494
495 nf_ct_l3proto_put(l3proto);
496
c1d10adb
PNA
497 return ret;
498}
499
500static const size_t cta_min_proto[CTA_PROTO_MAX] = {
501 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
502};
503
504static inline int
601e68e1 505ctnetlink_parse_tuple_proto(struct nfattr *attr,
c1d10adb
PNA
506 struct nf_conntrack_tuple *tuple)
507{
508 struct nfattr *tb[CTA_PROTO_MAX];
605dcad6 509 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
510 int ret = 0;
511
c1d10adb
PNA
512 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
513
514 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
515 return -EINVAL;
516
517 if (!tb[CTA_PROTO_NUM-1])
518 return -EINVAL;
519 tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
520
605dcad6 521 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
c1d10adb 522
605dcad6
MJ
523 if (likely(l4proto->nfattr_to_tuple))
524 ret = l4proto->nfattr_to_tuple(tb, tuple);
c1d10adb 525
605dcad6 526 nf_ct_l4proto_put(l4proto);
601e68e1 527
c1d10adb
PNA
528 return ret;
529}
530
531static inline int
532ctnetlink_parse_tuple(struct nfattr *cda[], struct nf_conntrack_tuple *tuple,
533 enum ctattr_tuple type, u_int8_t l3num)
534{
535 struct nfattr *tb[CTA_TUPLE_MAX];
536 int err;
537
c1d10adb
PNA
538 memset(tuple, 0, sizeof(*tuple));
539
540 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
541
542 if (!tb[CTA_TUPLE_IP-1])
543 return -EINVAL;
544
545 tuple->src.l3num = l3num;
546
547 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
548 if (err < 0)
549 return err;
550
551 if (!tb[CTA_TUPLE_PROTO-1])
552 return -EINVAL;
553
554 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
555 if (err < 0)
556 return err;
557
558 /* orig and expect tuples get DIR_ORIGINAL */
559 if (type == CTA_TUPLE_REPLY)
560 tuple->dst.dir = IP_CT_DIR_REPLY;
561 else
562 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
563
c1d10adb
PNA
564 return 0;
565}
566
5b1158e9 567#ifdef CONFIG_NF_NAT_NEEDED
c1d10adb
PNA
568static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = {
569 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
570 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
571};
572
5b1158e9 573static int nfnetlink_parse_nat_proto(struct nfattr *attr,
c1d10adb 574 const struct nf_conn *ct,
5b1158e9 575 struct nf_nat_range *range)
c1d10adb
PNA
576{
577 struct nfattr *tb[CTA_PROTONAT_MAX];
5b1158e9 578 struct nf_nat_protocol *npt;
c1d10adb 579
c1d10adb
PNA
580 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
581
582 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
583 return -EINVAL;
584
5b1158e9 585 npt = nf_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
c1d10adb
PNA
586
587 if (!npt->nfattr_to_range) {
5b1158e9 588 nf_nat_proto_put(npt);
c1d10adb
PNA
589 return 0;
590 }
591
592 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
593 if (npt->nfattr_to_range(tb, range) > 0)
594 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
595
5b1158e9 596 nf_nat_proto_put(npt);
c1d10adb 597
c1d10adb
PNA
598 return 0;
599}
600
601static const size_t cta_min_nat[CTA_NAT_MAX] = {
602 [CTA_NAT_MINIP-1] = sizeof(u_int32_t),
603 [CTA_NAT_MAXIP-1] = sizeof(u_int32_t),
604};
605
606static inline int
5b1158e9
JK
607nfnetlink_parse_nat(struct nfattr *nat,
608 const struct nf_conn *ct, struct nf_nat_range *range)
c1d10adb
PNA
609{
610 struct nfattr *tb[CTA_NAT_MAX];
611 int err;
612
c1d10adb 613 memset(range, 0, sizeof(*range));
601e68e1 614
3726add7 615 nfattr_parse_nested(tb, CTA_NAT_MAX, nat);
c1d10adb
PNA
616
617 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
618 return -EINVAL;
619
620 if (tb[CTA_NAT_MINIP-1])
bff9a89b 621 range->min_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
c1d10adb
PNA
622
623 if (!tb[CTA_NAT_MAXIP-1])
624 range->max_ip = range->min_ip;
625 else
bff9a89b 626 range->max_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
c1d10adb
PNA
627
628 if (range->min_ip)
629 range->flags |= IP_NAT_RANGE_MAP_IPS;
630
631 if (!tb[CTA_NAT_PROTO-1])
632 return 0;
633
5b1158e9 634 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
c1d10adb
PNA
635 if (err < 0)
636 return err;
637
c1d10adb
PNA
638 return 0;
639}
640#endif
641
642static inline int
643ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
644{
645 struct nfattr *tb[CTA_HELP_MAX];
646
c1d10adb
PNA
647 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
648
649 if (!tb[CTA_HELP_NAME-1])
650 return -EINVAL;
651
652 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
653
654 return 0;
655}
656
657static const size_t cta_min[CTA_MAX] = {
658 [CTA_STATUS-1] = sizeof(u_int32_t),
659 [CTA_TIMEOUT-1] = sizeof(u_int32_t),
660 [CTA_MARK-1] = sizeof(u_int32_t),
661 [CTA_USE-1] = sizeof(u_int32_t),
662 [CTA_ID-1] = sizeof(u_int32_t)
663};
664
665static int
601e68e1 666ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
1d00a4eb 667 struct nlmsghdr *nlh, struct nfattr *cda[])
c1d10adb
PNA
668{
669 struct nf_conntrack_tuple_hash *h;
670 struct nf_conntrack_tuple tuple;
671 struct nf_conn *ct;
672 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
673 u_int8_t u3 = nfmsg->nfgen_family;
674 int err = 0;
675
c1d10adb
PNA
676 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
677 return -EINVAL;
678
679 if (cda[CTA_TUPLE_ORIG-1])
680 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
681 else if (cda[CTA_TUPLE_REPLY-1])
682 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
683 else {
684 /* Flush the whole table */
685 nf_conntrack_flush();
686 return 0;
687 }
688
689 if (err < 0)
690 return err;
691
692 h = nf_conntrack_find_get(&tuple, NULL);
9ea8cfd6 693 if (!h)
c1d10adb 694 return -ENOENT;
c1d10adb
PNA
695
696 ct = nf_ct_tuplehash_to_ctrack(h);
601e68e1 697
c1d10adb 698 if (cda[CTA_ID-1]) {
bff9a89b 699 u_int32_t id = ntohl(*(__be32 *)NFA_DATA(cda[CTA_ID-1]));
c1d10adb
PNA
700 if (ct->id != id) {
701 nf_ct_put(ct);
702 return -ENOENT;
703 }
601e68e1 704 }
c1d10adb
PNA
705 if (del_timer(&ct->timeout))
706 ct->timeout.function((unsigned long)ct);
707
708 nf_ct_put(ct);
c1d10adb
PNA
709
710 return 0;
711}
712
713static int
601e68e1 714ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
1d00a4eb 715 struct nlmsghdr *nlh, struct nfattr *cda[])
c1d10adb
PNA
716{
717 struct nf_conntrack_tuple_hash *h;
718 struct nf_conntrack_tuple tuple;
719 struct nf_conn *ct;
720 struct sk_buff *skb2 = NULL;
721 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
722 u_int8_t u3 = nfmsg->nfgen_family;
723 int err = 0;
724
c1d10adb 725 if (nlh->nlmsg_flags & NLM_F_DUMP) {
01f34848
PNA
726#ifndef CONFIG_NF_CT_ACCT
727 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
c1d10adb
PNA
728 return -ENOTSUPP;
729#endif
c702e804
TG
730 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
731 ctnetlink_done);
c1d10adb
PNA
732 }
733
734 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
735 return -EINVAL;
736
737 if (cda[CTA_TUPLE_ORIG-1])
738 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
739 else if (cda[CTA_TUPLE_REPLY-1])
740 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
741 else
742 return -EINVAL;
743
744 if (err < 0)
745 return err;
746
747 h = nf_conntrack_find_get(&tuple, NULL);
9ea8cfd6 748 if (!h)
c1d10adb 749 return -ENOENT;
9ea8cfd6 750
c1d10adb
PNA
751 ct = nf_ct_tuplehash_to_ctrack(h);
752
753 err = -ENOMEM;
754 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
755 if (!skb2) {
756 nf_ct_put(ct);
757 return -ENOMEM;
758 }
c1d10adb 759
601e68e1 760 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
c1d10adb
PNA
761 IPCTNL_MSG_CT_NEW, 1, ct);
762 nf_ct_put(ct);
763 if (err <= 0)
764 goto free;
765
766 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
767 if (err < 0)
768 goto out;
769
c1d10adb
PNA
770 return 0;
771
772free:
773 kfree_skb(skb2);
774out:
775 return err;
776}
777
778static inline int
779ctnetlink_change_status(struct nf_conn *ct, struct nfattr *cda[])
780{
781 unsigned long d;
bff9a89b 782 unsigned int status = ntohl(*(__be32 *)NFA_DATA(cda[CTA_STATUS-1]));
c1d10adb
PNA
783 d = ct->status ^ status;
784
785 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
786 /* unchangeable */
787 return -EINVAL;
601e68e1 788
c1d10adb
PNA
789 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
790 /* SEEN_REPLY bit can only be set */
791 return -EINVAL;
792
601e68e1 793
c1d10adb
PNA
794 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
795 /* ASSURED bit can only be set */
796 return -EINVAL;
797
3726add7 798 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
5b1158e9 799#ifndef CONFIG_NF_NAT_NEEDED
c1d10adb
PNA
800 return -EINVAL;
801#else
5b1158e9 802 struct nf_nat_range range;
c1d10adb 803
3726add7 804 if (cda[CTA_NAT_DST-1]) {
5b1158e9 805 if (nfnetlink_parse_nat(cda[CTA_NAT_DST-1], ct,
3726add7
PM
806 &range) < 0)
807 return -EINVAL;
5b1158e9 808 if (nf_nat_initialized(ct,
3726add7
PM
809 HOOK2MANIP(NF_IP_PRE_ROUTING)))
810 return -EEXIST;
5b1158e9 811 nf_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
3726add7
PM
812 }
813 if (cda[CTA_NAT_SRC-1]) {
5b1158e9 814 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC-1], ct,
3726add7
PM
815 &range) < 0)
816 return -EINVAL;
5b1158e9 817 if (nf_nat_initialized(ct,
3726add7
PM
818 HOOK2MANIP(NF_IP_POST_ROUTING)))
819 return -EEXIST;
5b1158e9 820 nf_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
3726add7 821 }
c1d10adb
PNA
822#endif
823 }
824
825 /* Be careful here, modifying NAT bits can screw up things,
826 * so don't let users modify them directly if they don't pass
5b1158e9 827 * nf_nat_range. */
c1d10adb
PNA
828 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
829 return 0;
830}
831
832
833static inline int
834ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
835{
836 struct nf_conntrack_helper *helper;
dc808fe2 837 struct nf_conn_help *help = nfct_help(ct);
c1d10adb
PNA
838 char *helpname;
839 int err;
840
c1d10adb
PNA
841 /* don't change helper of sibling connections */
842 if (ct->master)
843 return -EINVAL;
844
845 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
846 if (err < 0)
847 return err;
848
df293bbb
YK
849 if (!strcmp(helpname, "")) {
850 if (help && help->helper) {
c1d10adb
PNA
851 /* we had a helper before ... */
852 nf_ct_remove_expectations(ct);
3c158f7f 853 rcu_assign_pointer(help->helper, NULL);
c1d10adb 854 }
df293bbb
YK
855
856 return 0;
c1d10adb 857 }
601e68e1 858
df293bbb
YK
859 if (!help) {
860 /* FIXME: we need to reallocate and rehash */
861 return -EBUSY;
862 }
863
864 helper = __nf_conntrack_helper_find_byname(helpname);
865 if (helper == NULL)
866 return -EINVAL;
867
868 if (help->helper == helper)
869 return 0;
870
871 if (help->helper)
e2d8e314 872 return -EBUSY;
df293bbb
YK
873
874 /* need to zero data of old helper */
875 memset(&help->help, 0, sizeof(help->help));
3c158f7f 876 rcu_assign_pointer(help->helper, helper);
c1d10adb
PNA
877
878 return 0;
879}
880
881static inline int
882ctnetlink_change_timeout(struct nf_conn *ct, struct nfattr *cda[])
883{
bff9a89b 884 u_int32_t timeout = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
601e68e1 885
c1d10adb
PNA
886 if (!del_timer(&ct->timeout))
887 return -ETIME;
888
889 ct->timeout.expires = jiffies + timeout * HZ;
890 add_timer(&ct->timeout);
891
892 return 0;
893}
894
895static inline int
896ctnetlink_change_protoinfo(struct nf_conn *ct, struct nfattr *cda[])
897{
898 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
605dcad6 899 struct nf_conntrack_l4proto *l4proto;
c1d10adb
PNA
900 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
901 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
902 int err = 0;
903
904 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
905
605dcad6 906 l4proto = nf_ct_l4proto_find_get(l3num, npt);
c1d10adb 907
605dcad6
MJ
908 if (l4proto->from_nfattr)
909 err = l4proto->from_nfattr(tb, ct);
910 nf_ct_l4proto_put(l4proto);
c1d10adb
PNA
911
912 return err;
913}
914
915static int
916ctnetlink_change_conntrack(struct nf_conn *ct, struct nfattr *cda[])
917{
918 int err;
919
c1d10adb
PNA
920 if (cda[CTA_HELP-1]) {
921 err = ctnetlink_change_helper(ct, cda);
922 if (err < 0)
923 return err;
924 }
925
926 if (cda[CTA_TIMEOUT-1]) {
927 err = ctnetlink_change_timeout(ct, cda);
928 if (err < 0)
929 return err;
930 }
931
932 if (cda[CTA_STATUS-1]) {
933 err = ctnetlink_change_status(ct, cda);
934 if (err < 0)
935 return err;
936 }
937
938 if (cda[CTA_PROTOINFO-1]) {
939 err = ctnetlink_change_protoinfo(ct, cda);
940 if (err < 0)
941 return err;
942 }
943
bcd1e830 944#if defined(CONFIG_NF_CONNTRACK_MARK)
c1d10adb 945 if (cda[CTA_MARK-1])
bff9a89b 946 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
c1d10adb
PNA
947#endif
948
c1d10adb
PNA
949 return 0;
950}
951
952static int
601e68e1 953ctnetlink_create_conntrack(struct nfattr *cda[],
c1d10adb
PNA
954 struct nf_conntrack_tuple *otuple,
955 struct nf_conntrack_tuple *rtuple)
956{
957 struct nf_conn *ct;
958 int err = -EINVAL;
dafc741c 959 struct nf_conn_help *help;
3c158f7f 960 struct nf_conntrack_helper *helper = NULL;
c1d10adb 961
c1d10adb
PNA
962 ct = nf_conntrack_alloc(otuple, rtuple);
963 if (ct == NULL || IS_ERR(ct))
601e68e1 964 return -ENOMEM;
c1d10adb
PNA
965
966 if (!cda[CTA_TIMEOUT-1])
967 goto err;
bff9a89b 968 ct->timeout.expires = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
c1d10adb
PNA
969
970 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
971 ct->status |= IPS_CONFIRMED;
972
bbb3357d
PNA
973 if (cda[CTA_STATUS-1]) {
974 err = ctnetlink_change_status(ct, cda);
975 if (err < 0)
976 goto err;
977 }
c1d10adb
PNA
978
979 if (cda[CTA_PROTOINFO-1]) {
980 err = ctnetlink_change_protoinfo(ct, cda);
981 if (err < 0)
c54ea3b9 982 goto err;
c1d10adb
PNA
983 }
984
bcd1e830 985#if defined(CONFIG_NF_CONNTRACK_MARK)
c1d10adb 986 if (cda[CTA_MARK-1])
bff9a89b 987 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
c1d10adb
PNA
988#endif
989
dafc741c 990 help = nfct_help(ct);
3c158f7f
PM
991 if (help) {
992 helper = nf_ct_helper_find_get(rtuple);
993 /* not in hash table yet so not strictly necessary */
994 rcu_assign_pointer(help->helper, helper);
995 }
dafc741c 996
c1d10adb
PNA
997 add_timer(&ct->timeout);
998 nf_conntrack_hash_insert(ct);
999
3c158f7f
PM
1000 if (helper)
1001 nf_ct_helper_put(helper);
dafc741c 1002
c1d10adb
PNA
1003 return 0;
1004
601e68e1 1005err:
c1d10adb
PNA
1006 nf_conntrack_free(ct);
1007 return err;
1008}
1009
601e68e1
YH
1010static int
1011ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1d00a4eb 1012 struct nlmsghdr *nlh, struct nfattr *cda[])
c1d10adb
PNA
1013{
1014 struct nf_conntrack_tuple otuple, rtuple;
1015 struct nf_conntrack_tuple_hash *h = NULL;
1016 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1017 u_int8_t u3 = nfmsg->nfgen_family;
1018 int err = 0;
1019
c1d10adb
PNA
1020 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1021 return -EINVAL;
1022
1023 if (cda[CTA_TUPLE_ORIG-1]) {
1024 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1025 if (err < 0)
1026 return err;
1027 }
1028
1029 if (cda[CTA_TUPLE_REPLY-1]) {
1030 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1031 if (err < 0)
1032 return err;
1033 }
1034
1035 write_lock_bh(&nf_conntrack_lock);
1036 if (cda[CTA_TUPLE_ORIG-1])
1037 h = __nf_conntrack_find(&otuple, NULL);
1038 else if (cda[CTA_TUPLE_REPLY-1])
1039 h = __nf_conntrack_find(&rtuple, NULL);
1040
1041 if (h == NULL) {
1042 write_unlock_bh(&nf_conntrack_lock);
c1d10adb
PNA
1043 err = -ENOENT;
1044 if (nlh->nlmsg_flags & NLM_F_CREATE)
1045 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1046 return err;
1047 }
1048 /* implicit 'else' */
1049
1050 /* we only allow nat config for new conntracks */
3726add7 1051 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
c1d10adb
PNA
1052 err = -EINVAL;
1053 goto out_unlock;
1054 }
1055
1056 /* We manipulate the conntrack inside the global conntrack table lock,
1057 * so there's no need to increase the refcount */
c1d10adb
PNA
1058 err = -EEXIST;
1059 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1060 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), cda);
1061
1062out_unlock:
1063 write_unlock_bh(&nf_conntrack_lock);
1064 return err;
1065}
1066
601e68e1
YH
1067/***********************************************************************
1068 * EXPECT
1069 ***********************************************************************/
c1d10adb
PNA
1070
1071static inline int
1072ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1073 const struct nf_conntrack_tuple *tuple,
1074 enum ctattr_expect type)
1075{
1076 struct nfattr *nest_parms = NFA_NEST(skb, type);
601e68e1 1077
c1d10adb
PNA
1078 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1079 goto nfattr_failure;
1080
1081 NFA_NEST_END(skb, nest_parms);
1082
1083 return 0;
1084
1085nfattr_failure:
1086 return -1;
601e68e1 1087}
c1d10adb 1088
1cde6436
PNA
1089static inline int
1090ctnetlink_exp_dump_mask(struct sk_buff *skb,
1091 const struct nf_conntrack_tuple *tuple,
1092 const struct nf_conntrack_tuple *mask)
1093{
1094 int ret;
1095 struct nf_conntrack_l3proto *l3proto;
605dcad6 1096 struct nf_conntrack_l4proto *l4proto;
1cde6436
PNA
1097 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1098
1099 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1100 ret = ctnetlink_dump_tuples_ip(skb, mask, l3proto);
1101 nf_ct_l3proto_put(l3proto);
1102
1103 if (unlikely(ret < 0))
1104 goto nfattr_failure;
1105
605dcad6
MJ
1106 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1107 ret = ctnetlink_dump_tuples_proto(skb, mask, l4proto);
1108 nf_ct_l4proto_put(l4proto);
1cde6436
PNA
1109 if (unlikely(ret < 0))
1110 goto nfattr_failure;
1111
1112 NFA_NEST_END(skb, nest_parms);
1113
1114 return 0;
1115
1116nfattr_failure:
1117 return -1;
1118}
1119
c1d10adb
PNA
1120static inline int
1121ctnetlink_exp_dump_expect(struct sk_buff *skb,
601e68e1 1122 const struct nf_conntrack_expect *exp)
c1d10adb
PNA
1123{
1124 struct nf_conn *master = exp->master;
bff9a89b
PM
1125 __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1126 __be32 id = htonl(exp->id);
c1d10adb
PNA
1127
1128 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1129 goto nfattr_failure;
1cde6436 1130 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
c1d10adb
PNA
1131 goto nfattr_failure;
1132 if (ctnetlink_exp_dump_tuple(skb,
1133 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1134 CTA_EXPECT_MASTER) < 0)
1135 goto nfattr_failure;
601e68e1 1136
c1d10adb
PNA
1137 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1138 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1139
1140 return 0;
601e68e1 1141
c1d10adb
PNA
1142nfattr_failure:
1143 return -1;
1144}
1145
1146static int
1147ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
601e68e1
YH
1148 int event,
1149 int nowait,
c1d10adb
PNA
1150 const struct nf_conntrack_expect *exp)
1151{
1152 struct nlmsghdr *nlh;
1153 struct nfgenmsg *nfmsg;
27a884dc 1154 unsigned char *b = skb_tail_pointer(skb);
c1d10adb
PNA
1155
1156 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1157 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1158 nfmsg = NLMSG_DATA(nlh);
1159
1160 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1161 nfmsg->nfgen_family = exp->tuple.src.l3num;
1162 nfmsg->version = NFNETLINK_V0;
1163 nfmsg->res_id = 0;
1164
1165 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1166 goto nfattr_failure;
1167
27a884dc 1168 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
c1d10adb
PNA
1169 return skb->len;
1170
1171nlmsg_failure:
1172nfattr_failure:
dc5fc579 1173 nlmsg_trim(skb, b);
c1d10adb
PNA
1174 return -1;
1175}
1176
1177#ifdef CONFIG_NF_CONNTRACK_EVENTS
1178static int ctnetlink_expect_event(struct notifier_block *this,
1179 unsigned long events, void *ptr)
1180{
1181 struct nlmsghdr *nlh;
1182 struct nfgenmsg *nfmsg;
1183 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1184 struct sk_buff *skb;
1185 unsigned int type;
27a884dc 1186 sk_buff_data_t b;
c1d10adb
PNA
1187 int flags = 0;
1188
1189 if (events & IPEXP_NEW) {
1190 type = IPCTNL_MSG_EXP_NEW;
1191 flags = NLM_F_CREATE|NLM_F_EXCL;
1192 } else
1193 return NOTIFY_DONE;
1194
b3a27bfb
PNA
1195 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1196 return NOTIFY_DONE;
1197
c1d10adb
PNA
1198 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1199 if (!skb)
1200 return NOTIFY_DONE;
1201
1202 b = skb->tail;
1203
b633ad5f 1204 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
c1d10adb
PNA
1205 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1206 nfmsg = NLMSG_DATA(nlh);
1207
1208 nlh->nlmsg_flags = flags;
1209 nfmsg->nfgen_family = exp->tuple.src.l3num;
1210 nfmsg->version = NFNETLINK_V0;
1211 nfmsg->res_id = 0;
1212
1213 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1214 goto nfattr_failure;
1215
1216 nlh->nlmsg_len = skb->tail - b;
1217 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1218 return NOTIFY_DONE;
1219
1220nlmsg_failure:
1221nfattr_failure:
1222 kfree_skb(skb);
1223 return NOTIFY_DONE;
1224}
1225#endif
1226
1227static int
1228ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1229{
1230 struct nf_conntrack_expect *exp = NULL;
1231 struct list_head *i;
1232 u_int32_t *id = (u_int32_t *) &cb->args[0];
87711cb8
PNA
1233 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1234 u_int8_t l3proto = nfmsg->nfgen_family;
c1d10adb 1235
c1d10adb
PNA
1236 read_lock_bh(&nf_conntrack_lock);
1237 list_for_each_prev(i, &nf_conntrack_expect_list) {
1238 exp = (struct nf_conntrack_expect *) i;
87711cb8
PNA
1239 if (l3proto && exp->tuple.src.l3num != l3proto)
1240 continue;
c1d10adb
PNA
1241 if (exp->id <= *id)
1242 continue;
1243 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1244 cb->nlh->nlmsg_seq,
1245 IPCTNL_MSG_EXP_NEW,
1246 1, exp) < 0)
1247 goto out;
1248 *id = exp->id;
1249 }
601e68e1 1250out:
c1d10adb
PNA
1251 read_unlock_bh(&nf_conntrack_lock);
1252
c1d10adb
PNA
1253 return skb->len;
1254}
1255
1256static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1257 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1258 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1259};
1260
1261static int
601e68e1 1262ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1d00a4eb 1263 struct nlmsghdr *nlh, struct nfattr *cda[])
c1d10adb
PNA
1264{
1265 struct nf_conntrack_tuple tuple;
1266 struct nf_conntrack_expect *exp;
1267 struct sk_buff *skb2;
1268 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1269 u_int8_t u3 = nfmsg->nfgen_family;
1270 int err = 0;
1271
c1d10adb
PNA
1272 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1273 return -EINVAL;
1274
1275 if (nlh->nlmsg_flags & NLM_F_DUMP) {
c702e804
TG
1276 return netlink_dump_start(ctnl, skb, nlh,
1277 ctnetlink_exp_dump_table,
1278 ctnetlink_done);
c1d10adb
PNA
1279 }
1280
1281 if (cda[CTA_EXPECT_MASTER-1])
1282 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1283 else
1284 return -EINVAL;
1285
1286 if (err < 0)
1287 return err;
1288
468ec44b 1289 exp = nf_conntrack_expect_find_get(&tuple);
c1d10adb
PNA
1290 if (!exp)
1291 return -ENOENT;
1292
1293 if (cda[CTA_EXPECT_ID-1]) {
bff9a89b 1294 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
c1d10adb
PNA
1295 if (exp->id != ntohl(id)) {
1296 nf_conntrack_expect_put(exp);
1297 return -ENOENT;
1298 }
601e68e1 1299 }
c1d10adb
PNA
1300
1301 err = -ENOMEM;
1302 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1303 if (!skb2)
1304 goto out;
4e9b8269 1305
601e68e1 1306 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
c1d10adb
PNA
1307 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1308 1, exp);
1309 if (err <= 0)
1310 goto free;
1311
1312 nf_conntrack_expect_put(exp);
1313
1314 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1315
1316free:
1317 kfree_skb(skb2);
1318out:
1319 nf_conntrack_expect_put(exp);
1320 return err;
1321}
1322
1323static int
601e68e1 1324ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1d00a4eb 1325 struct nlmsghdr *nlh, struct nfattr *cda[])
c1d10adb
PNA
1326{
1327 struct nf_conntrack_expect *exp, *tmp;
1328 struct nf_conntrack_tuple tuple;
1329 struct nf_conntrack_helper *h;
1330 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1331 u_int8_t u3 = nfmsg->nfgen_family;
1332 int err;
1333
1334 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1335 return -EINVAL;
1336
1337 if (cda[CTA_EXPECT_TUPLE-1]) {
1338 /* delete a single expect by tuple */
1339 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1340 if (err < 0)
1341 return err;
1342
1343 /* bump usage count to 2 */
468ec44b 1344 exp = nf_conntrack_expect_find_get(&tuple);
c1d10adb
PNA
1345 if (!exp)
1346 return -ENOENT;
1347
1348 if (cda[CTA_EXPECT_ID-1]) {
bff9a89b 1349 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
c1d10adb
PNA
1350 if (exp->id != ntohl(id)) {
1351 nf_conntrack_expect_put(exp);
1352 return -ENOENT;
1353 }
1354 }
1355
1356 /* after list removal, usage count == 1 */
1357 nf_conntrack_unexpect_related(exp);
601e68e1 1358 /* have to put what we 'get' above.
c1d10adb
PNA
1359 * after this line usage count == 0 */
1360 nf_conntrack_expect_put(exp);
1361 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1362 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1363
1364 /* delete all expectations for this helper */
1365 write_lock_bh(&nf_conntrack_lock);
1366 h = __nf_conntrack_helper_find_byname(name);
1367 if (!h) {
1368 write_unlock_bh(&nf_conntrack_lock);
1369 return -EINVAL;
1370 }
1371 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1372 list) {
dc808fe2
HW
1373 struct nf_conn_help *m_help = nfct_help(exp->master);
1374 if (m_help->helper == h
c1d10adb
PNA
1375 && del_timer(&exp->timeout)) {
1376 nf_ct_unlink_expect(exp);
1377 nf_conntrack_expect_put(exp);
1378 }
1379 }
1380 write_unlock_bh(&nf_conntrack_lock);
1381 } else {
1382 /* This basically means we have to flush everything*/
1383 write_lock_bh(&nf_conntrack_lock);
1384 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1385 list) {
1386 if (del_timer(&exp->timeout)) {
1387 nf_ct_unlink_expect(exp);
1388 nf_conntrack_expect_put(exp);
1389 }
1390 }
1391 write_unlock_bh(&nf_conntrack_lock);
1392 }
1393
1394 return 0;
1395}
1396static int
1397ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1398{
1399 return -EOPNOTSUPP;
1400}
1401
1402static int
1403ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1404{
1405 struct nf_conntrack_tuple tuple, mask, master_tuple;
1406 struct nf_conntrack_tuple_hash *h = NULL;
1407 struct nf_conntrack_expect *exp;
1408 struct nf_conn *ct;
dc808fe2 1409 struct nf_conn_help *help;
c1d10adb
PNA
1410 int err = 0;
1411
c1d10adb
PNA
1412 /* caller guarantees that those three CTA_EXPECT_* exist */
1413 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1414 if (err < 0)
1415 return err;
1416 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1417 if (err < 0)
1418 return err;
1419 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1420 if (err < 0)
1421 return err;
1422
1423 /* Look for master conntrack of this expectation */
1424 h = nf_conntrack_find_get(&master_tuple, NULL);
1425 if (!h)
1426 return -ENOENT;
1427 ct = nf_ct_tuplehash_to_ctrack(h);
dc808fe2 1428 help = nfct_help(ct);
c1d10adb 1429
dc808fe2 1430 if (!help || !help->helper) {
c1d10adb
PNA
1431 /* such conntrack hasn't got any helper, abort */
1432 err = -EINVAL;
1433 goto out;
1434 }
1435
1436 exp = nf_conntrack_expect_alloc(ct);
1437 if (!exp) {
1438 err = -ENOMEM;
1439 goto out;
1440 }
601e68e1 1441
c1d10adb
PNA
1442 exp->expectfn = NULL;
1443 exp->flags = 0;
1444 exp->master = ct;
9457d851 1445 exp->helper = NULL;
c1d10adb
PNA
1446 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1447 memcpy(&exp->mask, &mask, sizeof(struct nf_conntrack_tuple));
1448
1449 err = nf_conntrack_expect_related(exp);
1450 nf_conntrack_expect_put(exp);
1451
601e68e1 1452out:
c1d10adb
PNA
1453 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1454 return err;
1455}
1456
1457static int
1458ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1d00a4eb 1459 struct nlmsghdr *nlh, struct nfattr *cda[])
c1d10adb
PNA
1460{
1461 struct nf_conntrack_tuple tuple;
1462 struct nf_conntrack_expect *exp;
1463 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1464 u_int8_t u3 = nfmsg->nfgen_family;
1465 int err = 0;
1466
c1d10adb
PNA
1467 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1468 return -EINVAL;
1469
1470 if (!cda[CTA_EXPECT_TUPLE-1]
1471 || !cda[CTA_EXPECT_MASK-1]
1472 || !cda[CTA_EXPECT_MASTER-1])
1473 return -EINVAL;
1474
1475 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1476 if (err < 0)
1477 return err;
1478
1479 write_lock_bh(&nf_conntrack_lock);
1480 exp = __nf_conntrack_expect_find(&tuple);
1481
1482 if (!exp) {
1483 write_unlock_bh(&nf_conntrack_lock);
1484 err = -ENOENT;
1485 if (nlh->nlmsg_flags & NLM_F_CREATE)
1486 err = ctnetlink_create_expect(cda, u3);
1487 return err;
1488 }
1489
1490 err = -EEXIST;
1491 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1492 err = ctnetlink_change_expect(exp, cda);
1493 write_unlock_bh(&nf_conntrack_lock);
1494
c1d10adb
PNA
1495 return err;
1496}
1497
1498#ifdef CONFIG_NF_CONNTRACK_EVENTS
1499static struct notifier_block ctnl_notifier = {
1500 .notifier_call = ctnetlink_conntrack_event,
1501};
1502
1503static struct notifier_block ctnl_notifier_exp = {
1504 .notifier_call = ctnetlink_expect_event,
1505};
1506#endif
1507
1508static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1509 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1510 .attr_count = CTA_MAX, },
1511 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1512 .attr_count = CTA_MAX, },
1513 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1514 .attr_count = CTA_MAX, },
1515 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1516 .attr_count = CTA_MAX, },
1517};
1518
1519static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1520 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1521 .attr_count = CTA_EXPECT_MAX, },
1522 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1523 .attr_count = CTA_EXPECT_MAX, },
1524 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1525 .attr_count = CTA_EXPECT_MAX, },
1526};
1527
1528static struct nfnetlink_subsystem ctnl_subsys = {
1529 .name = "conntrack",
1530 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1531 .cb_count = IPCTNL_MSG_MAX,
1532 .cb = ctnl_cb,
1533};
1534
1535static struct nfnetlink_subsystem ctnl_exp_subsys = {
1536 .name = "conntrack_expect",
1537 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1538 .cb_count = IPCTNL_MSG_EXP_MAX,
1539 .cb = ctnl_exp_cb,
1540};
1541
d2483dde 1542MODULE_ALIAS("ip_conntrack_netlink");
c1d10adb 1543MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
34f9a2e4 1544MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
c1d10adb
PNA
1545
1546static int __init ctnetlink_init(void)
1547{
1548 int ret;
1549
1550 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1551 ret = nfnetlink_subsys_register(&ctnl_subsys);
1552 if (ret < 0) {
1553 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1554 goto err_out;
1555 }
1556
1557 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1558 if (ret < 0) {
1559 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1560 goto err_unreg_subsys;
1561 }
1562
1563#ifdef CONFIG_NF_CONNTRACK_EVENTS
1564 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1565 if (ret < 0) {
1566 printk("ctnetlink_init: cannot register notifier.\n");
1567 goto err_unreg_exp_subsys;
1568 }
1569
1570 ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1571 if (ret < 0) {
1572 printk("ctnetlink_init: cannot expect register notifier.\n");
1573 goto err_unreg_notifier;
1574 }
1575#endif
1576
1577 return 0;
1578
1579#ifdef CONFIG_NF_CONNTRACK_EVENTS
1580err_unreg_notifier:
1581 nf_conntrack_unregister_notifier(&ctnl_notifier);
1582err_unreg_exp_subsys:
1583 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1584#endif
1585err_unreg_subsys:
1586 nfnetlink_subsys_unregister(&ctnl_subsys);
1587err_out:
1588 return ret;
1589}
1590
1591static void __exit ctnetlink_exit(void)
1592{
1593 printk("ctnetlink: unregistering from nfnetlink.\n");
1594
1595#ifdef CONFIG_NF_CONNTRACK_EVENTS
e64a70be 1596 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
c1d10adb
PNA
1597 nf_conntrack_unregister_notifier(&ctnl_notifier);
1598#endif
1599
1600 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1601 nfnetlink_subsys_unregister(&ctnl_subsys);
1602 return;
1603}
1604
1605module_init(ctnetlink_init);
1606module_exit(ctnetlink_exit);