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