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